adriangb opened a new issue, #27694:
URL: https://github.com/apache/datafusion/issues/27694

   ### 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 [`TableScan` field 
docs](https://github.com/apache/datafusion/blob/4d1a6469547175cb83e0edb3d7c60a580e3cf5d0/datafusion/expr/src/logical_plan/plan.rs#L3266-L3282)
 say only "Optional number of rows to skip" and "Optional number of rows to 
read".
   - 
[`LogicalPlan::skip()`](https://github.com/apache/datafusion/blob/4d1a6469547175cb83e0edb3d7c60a580e3cf5d0/datafusion/expr/src/logical_plan/plan.rs#L1715)
 returns `None` for a `TableScan`, although 
[#25404](https://github.com/apache/datafusion/pull/25404) added 
`TableScan::skip`. `LogicalPlan::fetch()` does return `scan.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](https://github.com/apache/datafusion/pull/21057) (fixes 
[#21063](https://github.com/apache/datafusion/issues/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](https://github.com/apache/datafusion/pull/25404) | 
`TableScan::skip` was added, but `LogicalPlan::skip()` was not updated. The 
guard from #21057 does not see the new field. |
   | [#26281](https://github.com/apache/datafusion/pull/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
   
   ```rust
   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 None
   ```
   
   SQL does not reach a wrong result today. `PushDownLimit` sets `skip` only 
together with `fetch`, and the `fetch` guard catches that case.
   
   ### Expected behavior
   
   1. The `TableScan` docs state the row order above.
   2. `LogicalPlan::skip()` returns `scan.skip` for a `TableScan`.
   3. `TableScan` has one method that is the only safe way to add filters, for 
example:
   
      ```rust
      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>;
      }
      ```
   
      `PushDownFilter` uses 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. 
[`LogicalGet`](https://github.com/duckdb/duckdb/blob/main/src/include/duckdb/planner/operator/logical_get.hpp)
 has `table_filters` and projections, but no limit or offset field. Its 
[`LimitPushdown`](https://github.com/duckdb/duckdb/blob/main/src/optimizer/limit_pushdown.cpp)
 only moves a `LIMIT` below a `PROJECTION`. 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 #21063 and #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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to