|
| 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. |
0 commit comments