-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Use Expr::qualified_name() and Column::new() to extract partition keys from window and aggregate operators
#17757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76a4328
Use `Expr::qualified_name()` and `Column::new()` to extract partition…
masonh22 6a65ced
Use `Expr::qualified_name()` to extract group by keys
masonh22 8cb87af
Retrain dataframe tests with filters and aggregates
masonh22 77c7af2
Merge branch 'main' into window-pushdown-col
masonh22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -978,8 +978,11 @@ impl OptimizerRule for PushDownFilter { | |
| let group_expr_columns = agg | ||
| .group_expr | ||
| .iter() | ||
| .map(|e| Ok(Column::from_qualified_name(e.schema_name().to_string()))) | ||
| .collect::<Result<HashSet<_>>>()?; | ||
| .map(|e| { | ||
| let (relation, name) = e.qualified_name(); | ||
| Column::new(relation, name) | ||
| }) | ||
| .collect::<HashSet<_>>(); | ||
|
|
||
| let predicates = split_conjunction_owned(filter.predicate); | ||
|
|
||
|
|
@@ -1047,7 +1050,10 @@ impl OptimizerRule for PushDownFilter { | |
| func.params | ||
| .partition_by | ||
| .iter() | ||
| .map(|c| Column::from_qualified_name(c.schema_name().to_string())) | ||
| .map(|c| { | ||
| let (relation, name) = c.qualified_name(); | ||
| Column::new(relation, name) | ||
| }) | ||
| .collect::<HashSet<_>>() | ||
| }; | ||
| let potential_partition_keys = window | ||
|
|
@@ -1567,6 +1573,30 @@ mod tests { | |
| ) | ||
| } | ||
|
|
||
| /// verifies that filters with unusual column names are pushed down through aggregate operators | ||
| #[test] | ||
| fn filter_move_agg_special() -> Result<()> { | ||
| let schema = Schema::new(vec![ | ||
| Field::new("$a", DataType::UInt32, false), | ||
| Field::new("$b", DataType::UInt32, false), | ||
| Field::new("$c", DataType::UInt32, false), | ||
| ]); | ||
| let table_scan = table_scan(Some("test"), &schema, None)?.build()?; | ||
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .aggregate(vec![col("$a")], vec![sum(col("$b")).alias("total_salary")])? | ||
| .filter(col("$a").gt(lit(10i64)))? | ||
| .build()?; | ||
| // filter of key aggregation is commutative | ||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| Aggregate: groupBy=[[test.$a]], aggr=[[sum(test.$b) AS total_salary]] | ||
| TableScan: test, full_filters=[test.$a > Int64(10)] | ||
| " | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn filter_complex_group_by() -> Result<()> { | ||
| let table_scan = test_table_scan()?; | ||
|
|
@@ -1647,6 +1677,41 @@ mod tests { | |
| ) | ||
| } | ||
|
|
||
| /// verifies that filters with unusual identifier names are pushed down through window functions | ||
| #[test] | ||
| fn filter_window_special_identifier() -> Result<()> { | ||
| let schema = Schema::new(vec![ | ||
| Field::new("$a", DataType::UInt32, false), | ||
| Field::new("$b", DataType::UInt32, false), | ||
| Field::new("$c", DataType::UInt32, false), | ||
| ]); | ||
| let table_scan = table_scan(Some("test"), &schema, None)?.build()?; | ||
|
|
||
| let window = Expr::from(WindowFunction::new( | ||
| WindowFunctionDefinition::WindowUDF( | ||
| datafusion_functions_window::rank::rank_udwf(), | ||
| ), | ||
| vec![], | ||
| )) | ||
| .partition_by(vec![col("$a"), col("$b")]) | ||
| .order_by(vec![col("$c").sort(true, true)]) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let plan = LogicalPlanBuilder::from(table_scan) | ||
| .window(vec![window])? | ||
| .filter(col("$b").gt(lit(10i64)))? | ||
| .build()?; | ||
|
|
||
| assert_optimized_plan_equal!( | ||
| plan, | ||
| @r" | ||
| WindowAggr: windowExpr=[[rank() PARTITION BY [test.$a, test.$b] ORDER BY [test.$c ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]] | ||
| TableScan: test, full_filters=[test.$b > Int64(10)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice to see this pushed down |
||
| " | ||
| ) | ||
| } | ||
|
|
||
| /// verifies that when partitioning by 'a' and 'b', and filtering by 'a' and 'b', both 'a' and | ||
| /// 'b' are pushed | ||
| #[test] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, looks like a small improvement (although removing the aggregate would also be possible in this case 🤔 )