Repository navigation
Infer the columns of pg_lake_remote tables created without a column list - #933
sfc-gh-agedemenli wants to merge 3 commits into
Conversation
065be52 to
1c96aff
Compare
92ceab0 to
b35d7bc
Compare
1c96aff to
da2b434
Compare
b35d7bc to
fc4ae86
Compare
da2b434 to
c6b2cc2
Compare
7fa1393 to
73127ab
Compare
e371e44 to
8aa2ca7
Compare
73127ab to
6872b8c
Compare
8aa2ca7 to
c335c6e
Compare
6872b8c to
2a962b8
Compare
CREATE FOREIGN TABLE t () SERVER <pg_lake_remote server> OPTIONS (...) now asks the engine for the columns of its table with DESCRIBE, as the user that creates the foreign table, and declares them with the PostgreSQL types of their DuckDB types, as pg_lake does for files. Partitions and tables created with INHERITS still take their columns from their parent. USAGE on the server and the table options, which only superusers can set, are checked before connecting, as CREATE FOREIGN TABLE would. Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
2a962b8 to
df6c4c5
Compare
sfc-gh-mslot
left a comment
There was a problem hiding this comment.
Thanks, this looks good. I checked that a non-superuser can't reach the DESCRIBE, and a wide range of DuckDB types (struct, map, hugeint, enum, timestamp_ns, ...) map and read back correctly with pushdown on and off. Two small things: the IF NOT EXISTS shortcut can produce a table with no columns, and fixed-size arrays are declared as scalars.
|
|
||
| /* let PostgreSQL skip a table that already exists */ | ||
| if (createStmt->base.if_not_exists && | ||
| OidIsValid(RangeVarGetRelid(createStmt->base.relation, NoLock, true))) |
There was a problem hiding this comment.
RangeVarGetRelid searches the whole search_path, so with search_path = a, b and an existing b.events, CREATE FOREIGN TABLE IF NOT EXISTS events () ... skips inference and PostgreSQL then creates a.events with no columns. Could this look up the name in RangeVarGetCreationNamespace(...) with get_relname_relid instead?
| errdetail("Column names are limited to %d bytes.", | ||
| NAMEDATALEN - 1))); | ||
|
|
||
| Oid typeId = GetOrCreatePGTypeForDuckDBTypeName(columnType, &typeMod); |
There was a problem hiding this comment.
A DuckDB fixed-size array like INTEGER[3] comes back as plain integer here, since only a [] suffix counts as an array. The CREATE succeeds but every query then fails with Unimplemented type for cast (INTEGER[3] -> INTEGER). Maybe map it to integer[], or give the "not supported" error at CREATE time?
There was a problem hiding this comment.
Mapped them to arrays: the type parser now takes [n] wherever it takes [], so INTEGER[3] becomes integer[], also inside a STRUCT or MAP, where it failed the CREATE before. Arrays of maps (MAP(...)[] and MAP(...)[2]) were declared as a single map, so they're arrays now too. The test covers these with pushdown on and off.
…s in their schema A DuckDB fixed-size array such as INTEGER[3] was declared as a scalar, so queries on the table failed, and one in a STRUCT or MAP failed the CREATE. The type parser now takes [n] wherever it takes [], since PostgreSQL arrays have no fixed size, and arrays of maps become arrays. CREATE FOREIGN TABLE IF NOT EXISTS without a column list skipped inferring the columns if a table of that name was anywhere on the search_path, and PostgreSQL then created one without columns in the first schema. It now looks in the schema that the table would be created in. Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com> Co-authored-by: Cursor <cursoragent@cursor.com>
Lets a
pg_lake_remoteforeign table be created without a column list, as pg_lake already allows for files: pg_lake asks the engine for the table's columns and declares them with the matching PostgreSQL types.How
DESCRIBE SELECT * FROM <table>to the engine, connecting with the user mapping of the user that creates the table, and maps each DuckDB type with the same function as for files. Columns keep the engine's names and order. A name longer than PostgreSQL's limit is an error rather than being truncated, since the names have to match the engine's.INTEGER[3]is declared as an array (integer[]), also inside aSTRUCTorMAP, since PostgreSQL arrays have no fixed size. Arrays of maps are now declared as arrays too, which also fixes inferring a list of maps from a file.USAGEon the server and the table options are checked before connecting, asCREATE FOREIGN TABLEwould check them later. Otherwise a non-superuser could make the engine describe a table name that it reads as a file.INHERITSstill take their columns from their parent, andIF NOT EXISTSdoes not contact the engine when the table exists in the schema it would be created in.docs/external-query-engines.mdshows the syntax, how fixed-size arrays are declared, and listsDESCRIBEamong what an engine has to provide.Tests
test_external_engine_table.pycovers inferred names, order and types (includingnumeric(p,s),timestamptz, arrays, fixed-size arrays at the top level and inside structs and maps, arrays of structs and maps, and a mixed-case name) with pushdown on and off. It also coversIF NOT EXISTSon an existing table, including one of the same name in a latersearch_pathschema, a table the engine lacks, a missingtable_name, a column name that is too long, and a non-superuser getting PostgreSQL's errors without the engine being contacted. The existing column inference, permissions, complex types and inheritance tests also pass.