@@ -26,7 +26,9 @@ use datafusion_expr::expr::PlannedReplaceSelectItem;
26
26
use datafusion_expr:: utils:: {
27
27
expand_qualified_wildcard, expand_wildcard, find_base_plan,
28
28
} ;
29
- use datafusion_expr:: { Expr , LogicalPlan , Projection , SubqueryAlias } ;
29
+ use datafusion_expr:: {
30
+ Distinct , DistinctOn , Expr , LogicalPlan , Projection , SubqueryAlias ,
31
+ } ;
30
32
31
33
#[ derive( Default , Debug ) ]
32
34
pub struct ExpandWildcardRule { }
@@ -59,12 +61,25 @@ fn expand_internal(plan: LogicalPlan) -> Result<Transformed<LogicalPlan>> {
59
61
. map ( LogicalPlan :: Projection ) ?,
60
62
) )
61
63
}
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.
63
65
LogicalPlan :: SubqueryAlias ( SubqueryAlias { input, alias, .. } ) => {
64
66
Ok ( Transformed :: yes (
65
67
SubqueryAlias :: try_new ( input, alias) . map ( LogicalPlan :: SubqueryAlias ) ?,
66
68
) )
67
69
}
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
+ }
68
83
_ => Ok ( Transformed :: no ( plan) ) ,
69
84
}
70
85
}
@@ -240,6 +255,18 @@ mod tests {
240
255
assert_plan_eq ( plan, expected)
241
256
}
242
257
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
+
243
270
#[ test]
244
271
fn test_subquery_schema ( ) -> Result < ( ) > {
245
272
let analyzer = Analyzer :: with_rules ( vec ! [ Arc :: new( ExpandWildcardRule :: new( ) ) ] ) ;
0 commit comments