Skip to content

Commit 8876a79

Browse files
fix gdal
Signed-off-by: Aykut Bozkurt <aykut.bozkurt@snowflake.com>
1 parent af1c864 commit 8876a79

5 files changed

Lines changed: 23 additions & 16 deletions

File tree

pg_lake_engine/include/pg_lake/pgduck/gdal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
#include "pg_lake/copy/copy_format.h"
2121
#include "nodes/pg_list.h"
2222

23-
extern PGDLLEXPORT char *GDALReadFunctionCall(char *path, CopyDataCompression compression, List *options);
23+
extern PGDLLEXPORT char *GDALReadFunctionCall(char *path, CopyDataCompression compression, List *options,
24+
bool forceKeepWKB);

pg_lake_engine/src/pgduck/gdal.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
* See https://gdal.org/user/virtual_file_systems.html for compression syntax.
3333
*/
3434
char *
35-
GDALReadFunctionCall(char *path, CopyDataCompression compression, List *options)
35+
GDALReadFunctionCall(char *path, CopyDataCompression compression, List *options,
36+
bool forceKeepWKB)
3637
{
3738
DefElem *filenameOption = GetOption(options, "filename");
3839
bool emitFilename =
@@ -92,7 +93,7 @@ GDALReadFunctionCall(char *path, CopyDataCompression compression, List *options)
9293
/* whether to preserve WKB as a raw byte array */
9394
DefElem *keepWKBOption = GetOption(options, "keep_wkb");
9495

95-
if (keepWKBOption != NULL && defGetBoolean(keepWKBOption))
96+
if (forceKeepWKB || (keepWKBOption != NULL && defGetBoolean(keepWKBOption)))
9697
appendStringInfoString(&stRead, ", keep_wkb=true");
9798

9899
/* select a specific layer */

pg_lake_engine/src/pgduck/read_data.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,14 @@ ReadDataSourceFunction(List *sourcePaths,
471471
}
472472

473473
char *path = (char *) linitial(sourcePaths);
474+
475+
/*
476+
* Force keep_wkb so geometry stays as raw WKB BLOB. This
477+
* avoids DuckDB's strict Arrow-to-GEOMETRY conversion that
478+
* throws on types like MULTICURVE.
479+
*/
474480
char *stReadCall =
475-
GDALReadFunctionCall(path, sourceCompression, formatOptions);
481+
GDALReadFunctionCall(path, sourceCompression, formatOptions, true);
476482

477483
appendStringInfoString(&command, stReadCall);
478484
break;
@@ -1482,9 +1488,10 @@ BuildColumnProjection(char *columnName,
14821488

14831489

14841490
/*
1485-
* GDAL format can store geometry as BLOB or GEOMETRY depending on the
1486-
* driver. We need to cast to BLOB and then to hex to get the WKB
1487-
* representation.
1491+
* GDAL data scans always use keep_wkb=true so geometry arrives as raw
1492+
* WKB BLOB. The hex(TRY_CAST(... AS BLOB)) path converts it directly.
1493+
* The ST_AsHexWKB fallback exists for safety but should not be
1494+
* reached.
14881495
*/
14891496
if (sourceFormat == DATA_FORMAT_GDAL)
14901497
return psprintf("COALESCE(hex(TRY_CAST(%s AS BLOB)), ST_AsHexWKB(TRY_CAST(%s AS GEOMETRY)))%s",

pg_lake_spatial/tests/pytests/test_gdal.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,8 @@ def test_gdal_zip_gml(
200200
user_conn,
201201
)
202202

203-
# Querying is currently not supported because WKB is converted by DuckDB
204-
# Error messages reflect different DuckDB spatial versions
205-
error = run_query("SELECT shape FROM test_gdal.fdw", user_conn, raise_error=False)
206-
assert (
207-
"Geometry type 10 not supported" in error
208-
or "'MULTICURVE' is not supported" in error
209-
or "Unsupported geometry type in WKB" in error
210-
)
203+
result = run_query("SELECT shape FROM test_gdal.fdw", user_conn)
204+
assert len(result) == 5
211205

212206
user_conn.rollback()
213207

pg_lake_table/src/describe/describe.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,12 @@ DescribeColumnsQueryForURL(char *url,
375375
}
376376
else if (format == DATA_FORMAT_GDAL)
377377
{
378+
/*
379+
* DESCRIBE needs the native GEOMETRY type so foreign table columns
380+
* are created as PostGIS geometry, not bytea.
381+
*/
378382
appendStringInfo(&command, "FROM %s",
379-
GDALReadFunctionCall(url, compression, copyOptions));
383+
GDALReadFunctionCall(url, compression, copyOptions, false));
380384
}
381385
else
382386
{

0 commit comments

Comments
 (0)