Repository navigation
Add the SQL functions for DuckLake's stats pruning - #679
Conversation
890c7cb to
47bbafe
Compare
staticlibs
left a comment
There was a problem hiding this comment.
Hi Ines! The change looks good to me, I have 2 comments:
- Check the constant's type against
column_typeinpostgres_ducklake_value_sql_v1
ValueSql (src/postgres_ducklake_stats.cpp) chooses the output SQL by the column type but formats the value by
its own type. For numeric columns, value.ToString() is emitted unescaped:
postgres_ducklake_value_sql_v1('0 OR 1=1) --', NULL::INTEGER) -> 0 OR 1=1) --
postgres_ducklake_value_sql_v1('1 AS REAL)) OR true --', NULL::FLOAT) -> CAST(1 AS REAL)) OR true -- AS REAL)
Other mismatched types fail on Postgres or prune the wrong files:
(true, NULL::INTEGER)->true:operator does not exist: integer > boolean(1e300::DOUBLE, NULL::FLOAT)->CAST(1e+300 AS REAL):out of range for type real(TIMESTAMP_NS '...0.0000005', NULL::TIMESTAMP): Postgres rounds the nanoseconds instead of flooring them('0189E0B4-...', NULL::UUID): uppercase text sorts differently from the lowercase stats
Possible fix: return NULL unless value.type() == column_type. To still accept numeric constants of another numeric
type (e.g. 42 for BIGINT), cast them strictly with TryCastAs(column_type) and format the result.
- in tests please change:
LOAD postgres_scannerto ATTACH + DETACH instead to trigger the auto-load. unittest runner (auto)loading rules are pretty non-trivial, and this ATTACH + DETACH approach appeared to be the most robust when using functions/settings that are not yet added to engine extensions entries.
|
Hi Alex, suggestions have been applied! |
Closes https://github.com/duckdblabs/duckdb-internal/issues/12202
First postgres PR in the effort of making it a pluggable DuckLake catalog
DuckLake keeps the min/max stats of its data files as DuckDB text. With a Postgres metadata catalog, it prunes files on the server, so it needs Postgres SQL that compares a filter constant with those text bounds. Currently, that SQL is written by
PostgresMetadataManagerin DL. As per duckdb/ducklake#1962, this PR moves it intopostgres_scanner, so that DL can drop its Postgres-specific code.Functions
postgres_ducklake_value_sql_v1(value, column_type): the SQL of a filter constantpostgres_ducklake_stats_sql_v1(stats, column_type, bound): the SQL that casts a stats column as 'ordering', 'min' or 'max'column_typeis passed as a value of the column type, usually a typed NULL such asNULL::DATE. The constant must have that type, or a numeric type that converts to it exactly. A NULL result means the constant or the bounds cannot be compared on Postgres, so the files are not pruned. The_v1suffix names DuckLake's stats text format: if DL changes that format, it asks for new functions instead of pruning with these.Tests
test/sql/misc/postgres_ducklake_stats.testchecks the generated SQL for each type and edge casetest/sql/misc/postgres_ducklake_stats_pruning.testruns that SQL on Postgres against stats rows. It covers invalid dates, nanosecond widening,24:00:00, byte-order text comparison, REAL equality, TIMESTAMPTZ without an offset & ordering