Skip to content

Add the SQL functions for DuckLake's stats pruning - #679

Merged
staticlibs merged 4 commits into
duckdb:mainfrom
xproblematique:12202-stats-pruning-functions
Dec 3, 2026
Merged

staticlibs merged 4 commits into
duckdb:mainfrom
xproblematique:12202-stats-pruning-functions

Conversation

@xproblematique

@xproblematique xproblematique commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

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 PostgresMetadataManager in DL. As per duckdb/ducklake#1962, this PR moves it into postgres_scanner, so that DL can drop its Postgres-specific code.

Functions

  • postgres_ducklake_value_sql_v1(value, column_type): the SQL of a filter constant
  • postgres_ducklake_stats_sql_v1(stats, column_type, bound): the SQL that casts a stats column as 'ordering', 'min' or 'max'

column_type is passed as a value of the column type, usually a typed NULL such as NULL::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 _v1 suffix 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.test checks the generated SQL for each type and edge case
  • test/sql/misc/postgres_ducklake_stats_pruning.test runs 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

@xproblematique
xproblematique force-pushed the 12202-stats-pruning-functions branch from 890c7cb to 47bbafe Compare December 2, 2026 13:13

@staticlibs staticlibs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Ines! The change looks good to me, I have 2 comments:

  1. Check the constant's type against column_type in postgres_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.

  1. in tests please change:
LOAD postgres_scanner

to 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.

@xproblematique

Copy link
Copy Markdown
Member Author

Hi Alex, suggestions have been applied!

@staticlibs
staticlibs merged commit d991bb1 into duckdb:main Dec 3, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants