Repository navigation
fix: file_row_index() and input_file_name() error under OFFSET n LIMIT m - #27669
cool9850311 wants to merge 2 commits into
Conversation
|
@alamb @jayzhan211 Reopening #27395 as this PR, now scoped to #27494 and its tests. The tests for #27495 follow once this one merges. |
jayzhan211
left a comment
There was a problem hiding this comment.
@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.
| // 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. |
There was a problem hiding this comment.
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:
| // 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. |
| # A leaf expression must also reach the scan when the source pushes an | ||
| # `OFFSET` down. Every query below errors out without that. |
There was a problem hiding this comment.
Optional wording: the two controls and the EXPLAINs below don't error without the fix; only the value queries do.
| # 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. |
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>
d8dd06e to
77da6bc
Compare
alamb
left a comment
There was a problem hiding this comment.
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>> { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
I'll check if they can be removed, moved to slt or merged.
adriangb
left a comment
There was a problem hiding this comment.
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?
| // 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. |
There was a problem hiding this comment.
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?
| /// 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. |
There was a problem hiding this comment.
An example showing the equivalent of EXPLAIN ANALYZE or something other than prose would be helpful.
| /// The hosts between a projection and the child the split places the leaf | ||
| /// projection on. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
It's actually a pass-through node, which move the file_row_index() down to DataSourceExec.
I can rename it to pass-through.
Which issue does this PR close?
file_row_index()/input_file_name()fail underOFFSET n LIMIT m#27494.OFFSET n LIMIT mstopsget_field()from being pushed into the file scan #27495.Rationale for this change
OFFSET n LIMIT magainst a source that pushes the skip down makesfile_row_index()andinput_file_name()fail:Either clause on its own is fine. The pushed-down skip wraps the scan in a
GlobalLimitExec, and with more than one file group aCoalescePartitionsExecunder that. Those nodes land between the scan and the projection holding the
marker, and no projection swaps past a
GlobalLimitExec, so the marker neverreaches 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 leafprojection may descend are now listed in one place,
split_host_child, withGlobalLimitExecandCoalescePartitionsExecadded, andsplit_host_chainwalks 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
LocalLimitExecis 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_fieldreaches the scan under anOFFSETfor the same reason, which iswhat #27495 asks for. That issue's
EXPLAINcoverage is a follow-on PR, so itstays open here.
What is the testing strategy for this PR?
Yes: value and
EXPLAINqueries indatafusion/sqllogictest/test_files/file_row_index.slt(bare and wrappedmarkers, a computed sibling in the same projection, a repartition above the
limit,
input_file_name(), two file groups, a JSON listing table, andOFFSET 0 LIMIT nplusOFFSET nwith no fetch as controls), and eleven unittests in
datafusion/core/tests/physical_optimizer/projection_pushdown.rsshowing 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.sltred.Are there any user-facing changes?
file_row_index()andinput_file_name()underOFFSET n LIMIT mreturn rowsinstead of an error. No API change.
🤖 Generated with Claude Code