Skip to content

fix: file_row_index() and input_file_name() error under OFFSET n LIMIT m - #27669

Open
cool9850311 wants to merge 2 commits into
apache:mainfrom
cool9850311:fix-27494-leaf-projection-stranded-above-limit
Open

cool9850311 wants to merge 2 commits into
apache:mainfrom
cool9850311:fix-27494-leaf-projection-stranded-above-limit

Conversation

@cool9850311

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

OFFSET n LIMIT m against a source that pushes the skip down makes
file_row_index() and input_file_name() fail:

SELECT file_row_index() FROM small OFFSET 1 LIMIT 2;
-- Execution error: file_row_index() is source dependent and cannot be evaluated directly

Either clause on its own is fine. The pushed-down skip wraps the scan in a
GlobalLimitExec, and with more than one file group a CoalescePartitionsExec
under that. Those nodes land between the scan and the projection holding the
marker, and no projection swaps past a GlobalLimitExec, so the marker never
reaches the source that can resolve it.

What changes are included in this PR?

#27152 added a split that moves leaf expressions past a round-robin
RepartitionExec, testing for that one node inline. The nodes a leaf
projection may descend are now listed in one place, split_host_child, with
GlobalLimitExec and CoalescePartitionsExec added, and split_host_chain
walks a stack of them so a leaf descends past all of them in one pass. The arms
are an allowlist: a node must neither reference its input's columns nor change
them on the way out, and meeting that is necessary rather than sufficient, so
LocalLimitExec is deliberately left off.

Only leaf expressions descend. Ones that must stay in place are left above the
limit, so they are not evaluated on the rows the skip discards.

get_field reaches the scan under an OFFSET for the same reason, which is
what #27495 asks for. That issue's EXPLAIN coverage is a follow-on PR, so it
stays open here.

What is the testing strategy for this PR?

Yes: value and EXPLAIN queries in
datafusion/sqllogictest/test_files/file_row_index.slt (bare and wrapped
markers, a computed sibling in the same projection, a repartition above the
limit, input_file_name(), two file groups, a JSON listing table, and
OFFSET 0 LIMIT n plus OFFSET n with no fetch as controls), and eleven unit
tests in datafusion/core/tests/physical_optimizer/projection_pushdown.rs
showing each input and output plan.

Reverting the rule and keeping the tests turns 8 of those 11 unit tests and 11
rows of file_row_index.slt red.

Are there any user-facing changes?

file_row_index() and input_file_name() under OFFSET n LIMIT m return rows
instead of an error. No API change.

🤖 Generated with Claude Code

@cool9850311

Copy link
Copy Markdown
Contributor Author

@alamb @jayzhan211 Reopening #27395 as this PR, now scoped to #27494 and its tests. The tests for #27495 follow once this one merges.

@github-actions github-actions Bot added optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Nov 23, 2026

@jayzhan211 jayzhan211 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cool9850311 thanks for splitting this out! Looks good to me. The comments are optional cleanups.

Optional cleanup: in datafusion/core/tests/physical_optimizer/projection_pushdown.rs, limit_split_scan, split_below_a_filter_narrows_on_the_second_run and split_descends_through_global_limit_with_a_marker each build the same parquet scan. A small helper removes 13 lines:

fn parquet_scan(schema: &SchemaRef) -> Arc<dyn ExecutionPlan> {
    let config = FileScanConfigBuilder::new(
        ObjectStoreUrl::parse("test:///").unwrap(),
        Arc::new(ParquetSource::new(Arc::clone(schema))),
    )
    .with_file(PartitionedFile::new("x".to_string(), 100))
    .build();
    DataSourceExec::from_data_source(config)
}

Then limit_split_scan ends with Ok((Arc::clone(&schema), parquet_scan(&schema))), and the other two tests pass parquet_scan(&schema) straight to FilterExec::try_new and GlobalLimitExec::new.

Comment on lines +72 to +78
// Push what can already descend, then split the projections that are
// stuck above a `RepartitionExec`. The split needs no cleanup pass of
// its own: the outer projection it leaves either keeps every expression
// a plain column, and is then no narrower than the widened child, or it
// narrows and holds a computed one. `RepartitionExec` declines both, it
// is not removable because it renames, and nothing else moved.
// stuck above a node that will not take a whole projection. The split
// needs no cleanup pass of its own: the outer projection it leaves
// either keeps every expression a plain column, and is then no narrower
// than the widened child, or it narrows and holds a computed one. Every
// node `split_host_child` allows declines both, the outer projection is
// not removable because it renames, and nothing else moved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional cleanup: this comment says every node split_host_child allows rejects the outer projection, but CoalescePartitionsExec takes a narrowing one with computed expressions. With [s['a'] + s['b'] + s['c'], x] over a coalesce, the first run leaves the outer projection above the coalesce and the second run pushes it into the scan. Maybe say that instead:

Suggested change
// Push what can already descend, then split the projections that are
// stuck above a `RepartitionExec`. The split needs no cleanup pass of
// its own: the outer projection it leaves either keeps every expression
// a plain column, and is then no narrower than the widened child, or it
// narrows and holds a computed one. `RepartitionExec` declines both, it
// is not removable because it renames, and nothing else moved.
// stuck above a node that will not take a whole projection. The split
// needs no cleanup pass of its own: the outer projection it leaves
// either keeps every expression a plain column, and is then no narrower
// than the widened child, or it narrows and holds a computed one. Every
// node `split_host_child` allows declines both, the outer projection is
// not removable because it renames, and nothing else moved.
// Push what can already descend, then split the projections that are
// stuck above a node that will not take a whole projection. The split
// runs no cleanup pass: an outer projection it leaves narrowing on top
// of a `CoalescePartitionsExec` is pushed below it by this rule's second
// run in the physical optimizer.

Comment on lines +317 to +318
# A leaf expression must also reach the scan when the source pushes an
# `OFFSET` down. Every query below errors out without that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional wording: the two controls and the EXPLAINs below don't error without the fix; only the value queries do.

Suggested change
# A leaf expression must also reach the scan when the source pushes an
# `OFFSET` down. Every query below errors out without that.
# A leaf expression must also reach the scan when the source pushes an
# `OFFSET` down. Every value query below errors out without that.

cool9850311 and others added 2 commits November 23, 2026 21:23
With both a skip and a fetch against a source that supports skip pushdown, the
skip is pushed into the scan, which wraps it in a `GlobalLimitExec`, and with
more than one file group a `CoalescePartitionsExec` sits under that limit as
well. Those nodes land between the scan and the projection the leaf-extraction
rules placed directly on it, and no projection swaps past a `GlobalLimitExec`,
so `file_row_index()` and `input_file_name()` are evaluated above the scan,
where they cannot be resolved.

apache#27152 added a split that moves leaf expressions past a round-robin
`RepartitionExec`, testing for that one node inline. The nodes a leaf
projection may descend are now named in one place, `split_host_child`, with
`GlobalLimitExec` and `CoalescePartitionsExec` added, and `split_host_chain`
walks a stack of them so the leaf descends past all of them in one pass. A node
qualifies only if it neither references its input's columns nor changes them on
the way out; the arms are an allowlist, so meeting that is necessary rather
than sufficient, and `LocalLimitExec` is deliberately left off.

Only the leaf expressions descend. Ones that must stay in place are left in the
outer projection above the limit, so they are evaluated on the rows that
survive the skip rather than on the ones it discards.

The same change lets `get_field` reach the scan under an `OFFSET`, which is the
enhancement apache#27495 asks for; its end-to-end coverage is a follow-on PR.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review follow-ups on the previous commit; no behaviour changes.

`limit_split_scan`, `split_below_a_filter_narrows_on_the_second_run` and
`split_descends_through_global_limit_with_a_marker` each built the same
single-file parquet scan inline. They now share `parquet_scan`.

The rule's comment claimed every node `split_host_child` allows declines the
outer projection the split leaves above it. `CoalescePartitionsExec` takes a
narrowing projection, and once the leaves are extracted below it that outer
projection is narrower than the widened child, so this rule's second run in
the physical optimizer pushes it down. The comment now says that instead of
claiming no node moves.

`file_row_index.slt` said every query below the `OFFSET` heading errors out
without the fix. Only the value queries do: the two controls pass either way,
and the other `EXPLAIN`s differ in their plan rather than erroring. The next
sentence also described both controls as setting no fetch, which only one of
them does, and the control's own comment already says so.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@cool9850311
cool9850311 force-pushed the fix-27494-leaf-projection-stranded-above-limit branch from d8dd06e to 77da6bc Compare November 23, 2026 13:23

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jayzhan211 and @cool9850311

@adriangb can you give this a double check before we merge it (I think you worked on projection pushdown code before)

Ok((Arc::clone(&schema), parquet_scan(&schema)))
}

fn limit_split_exprs(schema: &SchemaRef) -> Result<Vec<ProjectionExpr>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need 500 lines of rust test ? Why can't we cover all these cases with .slt tests (the end API that is important)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check if they can be removed, moved to slt or merged.

@adriangb adriangb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the problem correctly, we are ending up with a plan like:

Projection: file_row_index(), col + 1
  GlobalLimitExec (1)
     CoalesceParitionsExec
        DataSourceExec projection=[col]

And we want to / need to end up with a plan of the shape:

Projection: __extracted_1, col + 1
  GlobalLimitExec (1)
     CoalesceParitionsExec
        Projection: file_row_index(), col
           DataSourceExec projection=[col]

So that we can then optimize to:

Projection: __extracted_1, col + 1
  GlobalLimitExec (1)
     CoalesceParitionsExec
         DataSourceExec projection=[file_row_index(), col]

And thus DataSourceExec owns file_row_index() and can evaluate it.

Is that right?

Comment on lines +520 to +523
// Hash and Range partitioning reference columns by index, which would
// have to be remapped against the rewritten child schema. Round robin
// carries no column references, and is what `EnsureRequirements`
// inserts here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that if we have a plan where we end up with:

Projection: file_row_index(), col
  RepartitionExec(hash)
    DataSourceExec: test.parquet

We woudln't be able to push file_row_index() and thus can't evaluate it / it fails at runtime?

Comment on lines +511 to +517
/// Returns the input of `node` if the split may place the leaf projection
/// below it.
///
/// The split gives `node` an input with different columns, so `node` must not
/// reference its input's columns and must output them unchanged, or the outer
/// projection's column indices would point at the wrong columns. The arms are
/// an allowlist: satisfying that is necessary, not sufficient.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example showing the equivalent of EXPLAIN ANALYZE or something other than prose would be helpful.

Comment on lines +541 to +542
/// The hosts between a projection and the child the split places the leaf
/// projection on.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wording was confusing to me. I'm not sure what a host is in this context. It's terminology introduced by this PR, we should be thoughtful about (1) what name to use and (2) define it clearly. Agents will make up names and use them as if it was a term everyone knows, when they've literally pulled it out of their < redacted > context.

@cool9850311 cool9850311 Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually a pass-through node, which move the file_row_index() down to DataSourceExec.
I can rename it to pass-through.

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

Labels

core Core DataFusion crate optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

file_row_index() / input_file_name() fail under OFFSET n LIMIT m

4 participants