Skip to content

TableScan does not encode the order of its filters, skip and fetch, so each rule must re-derive it #27694

Done

Description

@adriangb

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 say only "Optional number of rows to skip" and "Optional number of rows to read".
  • LogicalPlan::skip() returns None for a TableScan, although #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 (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 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:

    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

Willingness to contribute

Yes, I can open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions