Skip to content

Commit 7e1f6ea

Browse files
rustyconoverclaude
andcommitted
feat: define protocol 3.2 filtered scan planning
Protocol vgi.v3 3.2.0 lets a redirected catalog table see the query's predicates before it chooses what to scan, and lets the client prune the branches it returns (docs/protocol/scan-planning.md): - TableInfo.scan_planning ("bind" | "filtered"). A filtered table's scan redirect is resolved per scan, after the client has collected the predicates; it cannot inline scan_function. - catalog_table_scan_branches_get gains pushdown_filters: a VGI Filter Encoding v2 snapshot over the table's declared columns. Advisory. - ScanBranch gains row_count and column_statistics (conservative min/max/null claims the client prunes by). - Zero branches is a legal result for a filtered table. Python surface: CatalogInterface.table_scan_branches_plan (default delegates to table_scan_branches_get, so 3.1 overrides keep their signatures), ScanPlanningFilters with decode() and may_match(), Table(scan_planning=...). Adds the planned_days* fixture tables used by the native scan_planning tests and protocol 3.2 conformance records. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a52ef1a commit 7e1f6ea

21 files changed

Lines changed: 993 additions & 21 deletions

‎conformance/catalog-contracts.json‎

Lines changed: 6 additions & 1 deletion
Large diffs are not rendered by default.

‎docs/catalog_publication.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Batched catalog contents and reusable publications
22

3-
VGI wire revision **3.1.0** uses the `vgi.v3` route and retains
3+
VGI wire revision **3.2.0** uses the `vgi.v3` route and retains
44
`catalog_contents` with six catalog-wide Arrow IPC streams: schemas, tables,
55
views, functions, macros and indexes. Each stream can contain multiple batches.
66
Null means an empty category. Function and macro subtype fields remain intact.

‎docs/protocol/macro-contract.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Status: development contract for the combined successor protocol. The wire
44
records use one canonical overload-family representation while builds remain
55
on DuckDB 1.5. Native SQL CREATE MACRO remains unsupported; creation through
66
the catalog API and native discovery/execution are distinct paths.
7-
The breaking successor uses `vgi.v3` / `3.1.0`; it is incompatible with
7+
The breaking successor uses `vgi.v3` / `3.2.0`; it is incompatible with
88
released `vgi.v2` implementations.
99

1010
The [October 23 combined review](shared-catalog-values.md#combined-protocol-review--october-23-2026)

‎docs/protocol/projection-expressions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Optional scalar projection expressions
22

33
This contract is part of the combined, unpublished DuckDB 2.0 protocol
4-
transition, identified as `vgi.v3` / `3.1.0`. It can be implemented and tested
4+
transition, identified as `vgi.v3` / `3.2.0`. It can be implemented and tested
55
while the native adapter still builds against DuckDB 1.5. The successor is
66
unpublished and still requires coordinated release qualification.
77

‎docs/protocol/scan-planning.md‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Filtered scan planning
2+
3+
Protocol 3.2 lets a catalog table that redirects its scan to another function
4+
(`read_parquet`, `iceberg_scan`, a VGI table function, an attached catalog
5+
table) see the query's filters before it chooses what to scan, and lets the
6+
client prune the branches it returns.
7+
8+
Protocol 3.1 resolved scan redirects at bind time. `catalog_table_scan_branches_get`
9+
ran while the client was still binding `FROM`, before `WHERE` existed, so a
10+
worker that publishes many files had to return, and sign, every file on every
11+
scan. Pruning then depended entirely on the redirected function (Hive paths or
12+
per-file footers for `read_parquet`). Table functions already received
13+
`pushdown_filters`; redirected tables did not.
14+
15+
Protocol 3.2 adds three independent mechanisms:
16+
17+
1. **Filtered planning.** A table declares `TableInfo.scan_planning = "filtered"`.
18+
The client then defers `catalog_table_scan_branches_get` until it has
19+
collected the query's predicates on that table and sends them as
20+
`pushdown_filters`. The worker may use them to return fewer branches or fewer
21+
files per branch.
22+
2. **Branch statistics.** A `ScanBranch` may carry `row_count` and
23+
`column_statistics`. A client may drop a branch whose statistics prove that
24+
no row can satisfy the query's predicates.
25+
3. **Branch-filter contradiction.** A client may drop a branch whose
26+
`branch_filter` contradicts the query's predicates, before binding it. This
27+
needs no new wire fields.
28+
29+
Every mechanism is pruning only. The client still applies every query predicate
30+
to the rows the remaining branches produce, so a worker that ignores the
31+
filters, or returns more than it needs to, stays correct.
32+
33+
## `TableInfo.scan_planning`
34+
35+
`scan_planning` is a string, `"bind"` (the default) or `"filtered"`.
36+
37+
- `"bind"`: unchanged 3.1 behaviour. The client may call
38+
`catalog_table_scan_branches_get` at bind time with `pushdown_filters = null`,
39+
or use the inlined `scan_function`.
40+
- `"filtered"`: the client calls `catalog_table_scan_branches_get` once per scan
41+
of the table in a query, after it has collected that scan's predicates.
42+
`scan_function` MUST be null; a client rejects a table that sets both. A
43+
client that cannot defer the call (an engine without a planning hook) calls it
44+
at bind time with `pushdown_filters = null`, which is always permitted.
45+
46+
Unknown values are a protocol error.
47+
48+
## `catalog_table_scan_branches_get(pushdown_filters)`
49+
50+
The method gains one nullable parameter, `pushdown_filters` (`binary`): an Arrow
51+
IPC stream holding one VGI Filter Encoding v2 snapshot batch, exactly the shape
52+
table functions receive in `InitRequest.pushdown_filters`
53+
([filter encoding v2](vgi-filter-encoding-v2-spec.md)).
54+
55+
- Column references index the table's declared columns (`TableInfo.columns`),
56+
in declaration order.
57+
- Every predicate is **advisory**. The worker may ignore any of them.
58+
- The client sends only predicates that are constant at planning time: a
59+
predicate containing a prepared-statement parameter, a non-foldable function,
60+
or a reference to another table is omitted, not approximated.
61+
- `null` means no predicates were collected (bind-time planning, or a query
62+
with no usable predicates).
63+
64+
The worker returns the branches needed to produce every row that can satisfy
65+
the predicates. Returning extra branches or files is permitted; omitting a
66+
branch that holds a matching row is a correctness bug in the worker.
67+
68+
`at_unit` / `at_value` are sent unchanged. A client rejects `AT (...)` when the
69+
result has more than one branch, as in 3.1.
70+
71+
### Zero branches
72+
73+
For a `"filtered"` table, a `ScanBranchesResult` with zero branches is legal
74+
and means the scan produces no rows. The client plans an empty scan with the
75+
table's declared columns. For a `"bind"` table zero branches remain a protocol
76+
error.
77+
78+
## `ScanBranch` statistics
79+
80+
Two nullable fields are added to `ScanBranch`:
81+
82+
- `row_count` (`int64`): an estimate of the rows the branch produces before any
83+
predicate. Used for cardinality estimation only; it is never trusted as exact.
84+
- `column_statistics` (`binary`): the same payload as
85+
`TableInfo.column_statistics` / `ScanSplit.column_statistics`, produced by
86+
`serialize_column_statistics`, keyed by the table's declared column names.
87+
Columns may be omitted.
88+
89+
Statistics are **conservative claims** about every row the branch produces:
90+
91+
- every non-null value of a column lies within `[min, max]`;
92+
- `has_null = false` means the column has no NULLs;
93+
- `has_not_null = false` means the column is entirely NULL.
94+
95+
A client prunes a branch only when a collected predicate is provably false
96+
against its statistics. Wrong statistics silently drop rows, so a worker must
97+
leave a column out rather than guess.
98+
99+
Statistics are honoured for both `"bind"` and `"filtered"` tables.
100+
101+
## `branch_filter` contradiction
102+
103+
A client may evaluate a branch's `branch_filter` against the collected
104+
predicates and drop the branch when their conjunction is unsatisfiable (for
105+
example `branch_filter = "event_date = DATE '2026-10-15'"` with
106+
`WHERE event_date >= DATE '2026-11-01'`). `branch_filter` semantics are
107+
otherwise unchanged: it is still AND-ed into every scan of the branch.
108+
109+
`branch_filter` is exactly one SQL expression. A value that parses as more than
110+
one expression (for example containing a top-level comma) is a protocol error;
111+
3.1 clients silently kept only the first expression.
112+
113+
## Client behaviour (DuckDB extension)
114+
115+
The reference client resolves `"filtered"` tables in its pre-optimizer rewrite:
116+
117+
1. At bind time a `"filtered"` table returns a placeholder scan with the table's
118+
declared columns and makes no RPC.
119+
2. The rewrite collects the conjuncts of the filters above the placeholder that
120+
reference only its columns and are constant, serializes them as a filter v2
121+
snapshot, and calls `catalog_table_scan_branches_get` once for that scan.
122+
3. It drops branches by `column_statistics` and `branch_filter` contradiction.
123+
4. It emits one arm per remaining branch (a `UNION ALL` when there is more than
124+
one), or an empty result when none remain. DuckDB's optimizer then runs on
125+
the arms, so the redirected function's own pruning (for example
126+
`read_parquet` Hive partition and row-group pruning) still applies.
127+
128+
Because collection runs before DuckDB pushes filters through joins, the
129+
reference client sees `WHERE` predicates on the table but not predicates DuckDB
130+
would later derive from join conditions. That limitation is a property of the
131+
client, not of the protocol.
132+
133+
## SDK surface
134+
135+
Every SDK exposes:
136+
137+
- `scan_planning` on table descriptors (`"bind"` default);
138+
- a planning hook (`table_scan_branches_plan` in Python) that receives the
139+
decoded `pushdown_filters` together with the raw bytes. Its default ignores
140+
the predicates and returns the existing `table_scan_branches_get` result, so
141+
existing overrides keep their 3.1 signatures and behaviour;
142+
- a helper that tests whether the predicates can match a candidate described by
143+
min/max statistics (`ScanPlanningFilters.may_match` in Python), for pruning a
144+
manifest before returning branches;
145+
- `row_count` and `column_statistics` on its `ScanBranch` builder;
146+
- validation that a `"filtered"` table does not inline `scan_function`.
147+
148+
## Compatibility
149+
150+
Protocol 3.2.0 is a minor bump of `vgi.v3`. Clients and workers match on exact
151+
major and minor, so 3.2 peers reject 3.1 peers. A worker upgraded to a 3.2 SDK
152+
needs no code changes unless it opts in to `"filtered"` planning or branch
153+
statistics.

‎docs/protocol/shared-catalog-values.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ records and bounded reference behavior on the DuckDB 2.0 preparation branch.
55
The combined-design section distinguishes implemented SQL/constraint records
66
and reference proofs from the remaining public API and execution work. Both are
77
part of one combined successor contract. Published main now uses `vgi.v2` / `2.2.0`. The
8-
unpublished successor now uses `vgi.v3` / `3.1.0` for its incompatible changes.
8+
unpublished successor now uses `vgi.v3` / `3.2.0` for its incompatible changes.
99
This selects the protocol identity; final coordinated release qualification remains open. The development snapshot
1010
carries typed schema/table options,
1111
layouts, raw column mutation paths, and capability negotiation. See the

‎docs/protocol/vgi-catalog-query-pushdown-design.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and automatic pushdown remain read-only.
1313
The implementation belongs to the **combined successor protocol release**, alongside the
1414
catalog/type changes in the [manifest](duckdb-2.0-manifest.json). It does not reserve a separate
1515
minor release. Published main now uses `vgi.v2` / `2.2.0`; the prepared successor
16-
now uses `vgi.v3` / `3.1.0`. Separate versioned endpoints/processes, draining old
16+
now uses `vgi.v3` / `3.2.0`. Separate versioned endpoints/processes, draining old
1717
sessions, and reattaching/repreparing remain the migration policy. Coordinated
1818
release qualification is still required. See the
1919
[latest-main integration checkpoint](duckdb-2.0-main-integration-2026-10-24.md).
@@ -1361,7 +1361,7 @@ universal speedup percentage; establish budgets from these measurements before d
13611361
## 13. Compatibility, alternatives, and launch gates
13621362

13631363
Published main now uses **2.2.0** under **`vgi.v2`**. The prepared Python SDK, other SDKs and extension
1364-
use **3.1.0** under **`vgi.v3`**.
1364+
use **3.2.0** under **`vgi.v3`**.
13651365
The query capability, preparation/release RPCs and parameter metadata join the combined breaking
13661366
successor contract. The selected major version and routing/migration policy apply to that package;
13671367
do not publish this development surface as a compatible 2.2.0 update.

‎mkdocs.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ nav:
144144
- Protocol:
145145
- protocol/index.md
146146
- "Partition distribution (normative)": protocol/partition-distribution.md
147+
- "Filtered scan planning (normative)": protocol/scan-planning.md
147148
- "Filter Encoding v2 (normative, proposed)": protocol/vgi-filter-encoding-v2-spec.md
148149
- "Runtime-filter artifacts (optional extension)": protocol/vgi-runtime-filter-artifacts.md
149150
- "Opaque values: sealing (normative)": protocol/vgi-opaque-data-sealing.md

‎tests/conformance/test_protocol_version.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828

2929
# The version the real Python client / C++ extension declares and sends.
3030
CLIENT_PROTOCOL_VERSION = VgiProtocol.protocol_version
31-
PREVIOUS_MINOR_PROTOCOL_VERSION = "3.0.0"
31+
PREVIOUS_MINOR_PROTOCOL_VERSION = "3.1.0"
3232

3333

3434
def test_client_protocol_version_is_canonical() -> None:
3535
"""Sanity-check the constants this test reasons about haven't drifted apart."""
36-
assert CLIENT_PROTOCOL_VERSION == "3.1.0"
36+
assert CLIENT_PROTOCOL_VERSION == "3.2.0"
3737
assert VgiProtocol.protocol_name == "vgi.v3"
3838
# The fixture must be a genuine mismatch in the major/minor that the
3939
# framework compares; a patch-only difference would (correctly) pass.
4040
assert BAD_PROTOCOL_VERSION.split(".")[:2] != CLIENT_PROTOCOL_VERSION.split(".")[:2]
4141

4242

4343
def test_mismatched_worker_protocol_version_raises() -> None:
44-
"""A worker enforcing 99.0.0 must reject the 3.1.0 client at dispatch."""
44+
"""A worker enforcing 99.0.0 must reject the 3.2.0 client at dispatch."""
4545
with Client("vgi-fixture-bad-protocol-worker") as client, pytest.raises(ClientError) as exc_info:
4646
list(
4747
client.table_function(
@@ -56,7 +56,7 @@ def test_mismatched_worker_protocol_version_raises() -> None:
5656
assert "protocol_version mismatch" in message
5757
assert CLIENT_PROTOCOL_VERSION in message
5858
assert BAD_PROTOCOL_VERSION in message
59-
# Directional hint: the client (3.1.0) is older than the worker (99.0.0).
59+
# Directional hint: the client (3.2.0) is older than the worker (99.0.0).
6060
assert "upgrade the VGI extension/client" in message
6161

6262

0 commit comments

Comments
 (0)