Repository navigation
Description
Describe the bug
A TableScan combines several operators in one node. Its rows come out in this order:
source rows → filters (the Exact ones) → skip → fetch → projection
Nothing in the code states or enforces this order:
- The
TableScanfield docs say only "Optional number of rows to skip" and "Optional number of rows to read". LogicalPlan::skip()returnsNonefor aTableScan, although #25404 addedTableScan::skip.LogicalPlan::fetch()does returnscan.fetch.- The scan can hold "filter, then limit", but not "limit, then filter".
Limit(Filter(scan))can become a scan with filters and a fetch.Filter(scan with fetch)cannot absorb the filter, because inside the scan the filter would run before the fetch and keep different rows. Each rule that adds a filter to a scan must know this asymmetry by itself.
The same bug has occurred three times:
| Change | What went wrong |
|---|---|
| #21057 (fixes #21063) | A filter moved into a Sort or TableScan with a fetch. The fix added a guard in PushDownFilter, based on LogicalPlan::fetch() and LogicalPlan::skip(). |
| #25404 | TableScan::skip was added, but LogicalPlan::skip() was not updated. The guard from #21057 does not see the new field. |
| #26281 | A new pushdown path into the scan bypassed the general guard. It needed its own fetch and skip guard. |
Today only two rules write these fields: PushDownFilter writes filters, and PushDownLimit writes skip and fetch. Both are correct, but only because each one knows the order. A third-party optimizer rule gets no help, and LogicalPlan::skip() gives it a wrong answer.
To Reproduce
let LogicalPlan::TableScan(scan) = test_table_scan()? else { unreachable!() };
let plan = LogicalPlan::TableScan(TableScan { skip: Some(3), ..scan });
assert_eq!(plan.skip()?, Some(3)); // fails: returns NoneSQL does not reach a wrong result today. PushDownLimit sets skip only together with fetch, and the fetch guard catches that case.
Expected behavior
-
The
TableScandocs state the row order above. -
LogicalPlan::skip()returnsscan.skipfor aTableScan. -
TableScanhas one method that is the only safe way to add filters, for example:impl TableScan { /// Returns `None` when the scan has a skip or a fetch, because both /// apply after the scan filters. pub fn try_with_filters(self, filters: Vec<Expr>) -> Option<Self>; }
PushDownFilteruses it on both of its scan paths, so the guard lives in one place. Rules outside DataFusion get the same guard.
Additional context
- DuckDB avoids the problem by keeping the limit out of the scan.
LogicalGethastable_filtersand projections, but no limit or offset field. ItsLimitPushdownonly moves aLIMITbelow aPROJECTION. DataFusion puts the limit in the scan on purpose, so the order must be explicit. - A related test gap: no test optimizes a plan that is already optimized. The bugs in Filter pushdown past children with fetch limits causes correctness bug #21063 and fix: push filters above a pure extraction projection into the scan below it #26281 both appear only when a filter is put on an optimized plan and the plan is optimized again.
Willingness to contribute
Yes, I can open a PR.
Activity
fix: encode the TableScan filter, skip and fetch order (apache#27696)