@@ -110,15 +110,27 @@ struct GdalScanLocalState : ArrowScanLocalState {
110110
111111struct GdalScanGlobalState : ArrowScanGlobalState {};
112112
113- struct ScopeGuard {
114- std::function<void ()> f;
115- ScopeGuard (std::function<void ()> f) : f(f) {
113+
114+ struct ScopedOption {
115+ string option;
116+ string original_value;
117+
118+ ScopedOption (string option_p, const char * default_value) : option(option_p) {
119+ // Save current value
120+ original_value = CPLGetConfigOption (option.c_str (), default_value);
121+ }
122+
123+ void Set (const char * new_value) {
124+ CPLSetThreadLocalConfigOption (option.c_str (), new_value);
116125 }
117- ~ScopeGuard () {
118- f ();
126+
127+ ~ScopedOption () {
128+ // Reset
129+ CPLSetThreadLocalConfigOption (option.c_str (), original_value.c_str ());
119130 }
120131};
121132
133+
122134unique_ptr<FunctionData> GdalTableFunction::Bind (ClientContext &context, TableFunctionBindInput &input,
123135 vector<LogicalType> &return_types, vector<string> &names) {
124136
@@ -163,20 +175,29 @@ unique_ptr<FunctionData> GdalTableFunction::Bind(ClientContext &context, TableFu
163175
164176 // HACK: check for XLSX_HEADERS open option
165177 // TODO: Remove this once GDAL 3.8 is released
166- auto xlsx_default = string (CPLGetConfigOption (" OGR_XLSX_HEADERS" , " AUTO" ));
178+ ScopedOption xlsx_headers (" OGR_XLSX_HEADERS" , " AUTO" );
179+ ScopedOption xlsx_field_types (" OGR_XLSX_FIELD_TYPES" , " AUTO" );
167180 for (auto &option : gdal_open_options) {
168181 if (option == nullptr ) {
169182 break ;
170183 }
171- if (strcmp (option, " HEADERS=FORCE" ) == 0 ) {
172- CPLSetThreadLocalConfigOption (" OGR_XLSX_HEADERS" , " FORCE" );
173-
184+ else if (strcmp (option, " HEADERS=FORCE" ) == 0 ) {
185+ xlsx_headers.Set (" FORCE" );
186+ }
187+ else if (strcmp (option, " HEADERS=DISABLE" ) == 0 ) {
188+ xlsx_headers.Set (" DISABLE" );
189+ }
190+ else if (strcmp (option, " HEADERS=AUTO" ) == 0 ) {
191+ xlsx_headers.Set (" AUTO" );
174192 }
175- if (strcmp (option, " HEADERS=DISABLE" ) == 0 ) {
176- CPLSetThreadLocalConfigOption (" OGR_XLSX_HEADERS" , " DISABLE" );
193+ else if (strcmp (option, " FIELD_TYPES=STRING" ) == 0 ) {
194+ xlsx_field_types.Set (" STRING" );
195+ }
196+ else if (strcmp (option, " FIELD_TYPES=AUTO" ) == 0 ) {
197+ xlsx_field_types.Set (" AUTO" );
177198 }
178199 }
179-
200+
180201 // Now we can open the dataset
181202 auto file_name = input.inputs [0 ].GetValue <string>();
182203 auto dataset =
@@ -185,11 +206,6 @@ unique_ptr<FunctionData> GdalTableFunction::Bind(ClientContext &context, TableFu
185206 gdal_open_options.empty () ? nullptr : gdal_open_options.data (),
186207 gdal_sibling_files.empty () ? nullptr : gdal_sibling_files.data ()));
187208
188- // Reset upon exit
189- ScopeGuard _guard ([&] {
190- CPLSetThreadLocalConfigOption (" OGR_XLSX_HEADERS" , xlsx_default.c_str ());
191- });
192-
193209
194210 if (dataset == nullptr ) {
195211 auto error = string (CPLGetLastErrorMsg ());
0 commit comments