Skip to content

Commit f07879f

Browse files
authored
Merge pull request #114 from Maxxen/dev
Allow passing `HEADERS=[AUTO/FORCE/DISABLE]` st_read() to control xlsx behavior.
2 parents dc66594 + 9d8787e commit f07879f

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

duckdb

Submodule duckdb updated 116 files

spatial/src/spatial/gdal/functions/st_read.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ struct GdalScanLocalState : ArrowScanLocalState {
110110

111111
struct GdalScanGlobalState : ArrowScanGlobalState {};
112112

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);
125+
}
126+
127+
~ScopedOption() {
128+
// Reset
129+
CPLSetThreadLocalConfigOption(option.c_str(), original_value.c_str());
130+
}
131+
};
132+
133+
113134
unique_ptr<FunctionData> GdalTableFunction::Bind(ClientContext &context, TableFunctionBindInput &input,
114135
vector<LogicalType> &return_types, vector<string> &names) {
115136

@@ -152,6 +173,31 @@ unique_ptr<FunctionData> GdalTableFunction::Bind(ClientContext &context, TableFu
152173
gdal_sibling_files.push_back(nullptr);
153174
}
154175

176+
// HACK: check for XLSX_HEADERS open option
177+
// TODO: Remove this once GDAL 3.8 is released
178+
ScopedOption xlsx_headers("OGR_XLSX_HEADERS", "AUTO");
179+
ScopedOption xlsx_field_types("OGR_XLSX_FIELD_TYPES", "AUTO");
180+
for (auto &option : gdal_open_options) {
181+
if (option == nullptr) {
182+
break;
183+
}
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");
192+
}
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");
198+
}
199+
}
200+
155201
// Now we can open the dataset
156202
auto file_name = input.inputs[0].GetValue<string>();
157203
auto dataset =
@@ -160,6 +206,7 @@ unique_ptr<FunctionData> GdalTableFunction::Bind(ClientContext &context, TableFu
160206
gdal_open_options.empty() ? nullptr : gdal_open_options.data(),
161207
gdal_sibling_files.empty() ? nullptr : gdal_sibling_files.data()));
162208

209+
163210
if (dataset == nullptr) {
164211
auto error = string(CPLGetLastErrorMsg());
165212
throw IOException("Could not open file: " + file_name + " (" + error + ")");

0 commit comments

Comments
 (0)