Skip to content

Commit 9663141

Browse files
authored
Expand wildcard expressions in distinct on (#12941)
1 parent 435f959 commit 9663141

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

datafusion/optimizer/src/analyzer/expand_wildcard_rule.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use datafusion_expr::expr::PlannedReplaceSelectItem;
2626
use datafusion_expr::utils::{
2727
expand_qualified_wildcard, expand_wildcard, find_base_plan,
2828
};
29-
use datafusion_expr::{Expr, LogicalPlan, Projection, SubqueryAlias};
29+
use datafusion_expr::{
30+
Distinct, DistinctOn, Expr, LogicalPlan, Projection, SubqueryAlias,
31+
};
3032

3133
#[derive(Default, Debug)]
3234
pub struct ExpandWildcardRule {}
@@ -59,12 +61,25 @@ fn expand_internal(plan: LogicalPlan) -> Result<Transformed<LogicalPlan>> {
5961
.map(LogicalPlan::Projection)?,
6062
))
6163
}
62-
// Teh schema of the plan should also be updated if the child plan is transformed.
64+
// The schema of the plan should also be updated if the child plan is transformed.
6365
LogicalPlan::SubqueryAlias(SubqueryAlias { input, alias, .. }) => {
6466
Ok(Transformed::yes(
6567
SubqueryAlias::try_new(input, alias).map(LogicalPlan::SubqueryAlias)?,
6668
))
6769
}
70+
LogicalPlan::Distinct(Distinct::On(distinct_on)) => {
71+
let projected_expr =
72+
expand_exprlist(&distinct_on.input, distinct_on.select_expr)?;
73+
validate_unique_names("Distinct", projected_expr.iter())?;
74+
Ok(Transformed::yes(LogicalPlan::Distinct(Distinct::On(
75+
DistinctOn::try_new(
76+
distinct_on.on_expr,
77+
projected_expr,
78+
distinct_on.sort_expr,
79+
distinct_on.input,
80+
)?,
81+
))))
82+
}
6883
_ => Ok(Transformed::no(plan)),
6984
}
7085
}
@@ -240,6 +255,18 @@ mod tests {
240255
assert_plan_eq(plan, expected)
241256
}
242257

258+
#[test]
259+
fn test_expand_wildcard_in_distinct_on() -> Result<()> {
260+
let table_scan = test_table_scan()?;
261+
let plan = LogicalPlanBuilder::from(table_scan)
262+
.distinct_on(vec![col("a")], vec![wildcard()], None)?
263+
.build()?;
264+
let expected = "\
265+
DistinctOn: on_expr=[[test.a]], select_expr=[[test.a, test.b, test.c]], sort_expr=[[]] [a:UInt32, b:UInt32, c:UInt32]\
266+
\n TableScan: test [a:UInt32, b:UInt32, c:UInt32]";
267+
assert_plan_eq(plan, expected)
268+
}
269+
243270
#[test]
244271
fn test_subquery_schema() -> Result<()> {
245272
let analyzer = Analyzer::with_rules(vec![Arc::new(ExpandWildcardRule::new())]);

0 commit comments

Comments
 (0)