Skip to content

Commit 6fc7114

Browse files
compheadhaohuaijin
andauthored
[branch-53] fix: FilterExec should drop projection when apply projection pushdown (#21492)
(cherry picked from commit 330d57f) ## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> Co-authored-by: Huaijin <haohuaijin@gmail.com>
1 parent 61d8483 commit 6fc7114

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

datafusion/physical-plan/src/filter.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ impl ExecutionPlan for FilterExec {
606606
return FilterExecBuilder::from(self)
607607
.with_input(make_with_child(projection, self.input())?)
608608
.with_predicate(new_predicate)
609+
// The original FilterExec projection referenced columns from its old
610+
// input. After the swap the new input is the ProjectionExec which
611+
// already handles column selection, so clear the projection here.
612+
.apply_projection(None)?
609613
.build()
610614
.map(|e| Some(Arc::new(e) as _));
611615
}
@@ -2265,4 +2269,68 @@ mod tests {
22652269

22662270
Ok(())
22672271
}
2272+
2273+
/// Regression test: ProjectionExec on top of a FilterExec that already has
2274+
/// an explicit projection must not panic when `try_swapping_with_projection`
2275+
/// attempts to swap the two nodes.
2276+
///
2277+
/// Before the fix, `FilterExecBuilder::from(self)` copied the old projection
2278+
/// (e.g. `[0, 1, 2]`) from the FilterExec. After `.with_input` replaced the
2279+
/// input with the narrower ProjectionExec (2 columns), `.build()` tried to
2280+
/// validate the stale `[0, 1, 2]` projection against the 2-column schema and
2281+
/// panicked with "project index 2 out of bounds, max field 2".
2282+
#[test]
2283+
fn test_filter_with_projection_swap_does_not_panic() -> Result<()> {
2284+
use crate::projection::ProjectionExpr;
2285+
use datafusion_physical_expr::expressions::col;
2286+
2287+
// Schema: [ts: Int64, tokens: Int64, svc: Utf8]
2288+
let schema = Arc::new(Schema::new(vec![
2289+
Field::new("ts", DataType::Int64, false),
2290+
Field::new("tokens", DataType::Int64, false),
2291+
Field::new("svc", DataType::Utf8, false),
2292+
]));
2293+
let input = Arc::new(EmptyExec::new(Arc::clone(&schema)));
2294+
2295+
// FilterExec: ts > 0, projection=[ts@0, tokens@1, svc@2] (all 3 cols)
2296+
let predicate = Arc::new(BinaryExpr::new(
2297+
Arc::new(Column::new("ts", 0)),
2298+
Operator::Gt,
2299+
Arc::new(Literal::new(ScalarValue::Int64(Some(0)))),
2300+
));
2301+
let filter = Arc::new(
2302+
FilterExecBuilder::new(predicate, input)
2303+
.apply_projection(Some(vec![0, 1, 2]))?
2304+
.build()?,
2305+
);
2306+
2307+
// ProjectionExec: narrows to [ts, tokens] (drops svc)
2308+
let proj_exprs = vec![
2309+
ProjectionExpr {
2310+
expr: col("ts", &filter.schema())?,
2311+
alias: "ts".to_string(),
2312+
},
2313+
ProjectionExpr {
2314+
expr: col("tokens", &filter.schema())?,
2315+
alias: "tokens".to_string(),
2316+
},
2317+
];
2318+
let projection = Arc::new(ProjectionExec::try_new(
2319+
proj_exprs,
2320+
Arc::clone(&filter) as _,
2321+
)?);
2322+
2323+
// This must not panic
2324+
let result = filter.try_swapping_with_projection(&projection)?;
2325+
assert!(result.is_some(), "swap should succeed");
2326+
2327+
let new_plan = result.unwrap();
2328+
// Output schema must still be [ts, tokens]
2329+
let out_schema = new_plan.schema();
2330+
assert_eq!(out_schema.fields().len(), 2);
2331+
assert_eq!(out_schema.field(0).name(), "ts");
2332+
assert_eq!(out_schema.field(1).name(), "tokens");
2333+
2334+
Ok(())
2335+
}
22682336
}

0 commit comments

Comments
 (0)