File tree Expand file tree Collapse file tree
datafusion/logical_analyzer
tests/sql/logical_analyzer
snapshots/union_schema_analyzer Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -55,13 +55,21 @@ impl AnalyzerRule for UnionSchemaAnalyzer {
5555}
5656
5757fn analyze_internal ( plan : & LogicalPlan ) -> DFResult < LogicalPlan > {
58- Ok ( plan
59- . clone ( )
60- . transform_down ( |node| match node {
61- LogicalPlan :: Union ( union) => Ok ( Transformed :: yes ( rewrite_union ( & union) ?) ) ,
62- _ => Ok ( Transformed :: no ( node. clone ( ) ) ) ,
63- } ) ?
64- . data )
58+ let mut new_plan = plan. clone ( ) . transform_up ( |node| match node {
59+ LogicalPlan :: Union ( union) => Ok ( Transformed :: yes ( rewrite_union ( & union) ?) ) ,
60+ _ => Ok ( Transformed :: no ( node. clone ( ) ) ) ,
61+ } ) ?;
62+
63+ // Recompute schemas for Projection and SubqueryAlias nodes above the rewritten Union
64+ if new_plan. transformed {
65+ new_plan = new_plan. data . transform_up ( |node| match node {
66+ LogicalPlan :: Projection ( _) | LogicalPlan :: SubqueryAlias ( _) => {
67+ Ok ( Transformed :: yes ( node. recompute_schema ( ) ?) )
68+ }
69+ _ => Ok ( Transformed :: no ( node) ) ,
70+ } ) ?;
71+ }
72+ Ok ( new_plan. data )
6573}
6674
6775fn rewrite_union ( union : & Union ) -> datafusion_common:: Result < LogicalPlan > {
Original file line number Diff line number Diff line change 11-- -
22source : crates / core - executor / src / tests / sql / logical_analyzer / union_schema_analyzer .rs
3- description : " \" SELECT '2024-12-31 10:00:00.000'::TIMESTAMP as t\n UNION ALL\n SELECT '9999-12-31 00:00:00.000 +0000' AS t; \" "
3+ description : " \" SELECT '2024-12-31 10:00:00.000'::TIMESTAMP as t\n UNION ALL\n SELECT '9999-12-31 00:00:00.000 +0000' AS t\n ORDER BY 1 \" "
44-- -
55Ok (
66 [
77 " +---------------------+" ,
88 " | t |" ,
99 " +---------------------+" ,
10- " | 9999-12-31T00:00:00 |" ,
1110 " | 2024-12-31T10:00:00 |" ,
11+ " | 9999-12-31T00:00:00 |" ,
1212 " +---------------------+" ,
1313 ],
1414)
Original file line number Diff line number Diff line change 1+ -- -
2+ source : crates / core - executor / src / tests / sql / logical_analyzer / union_schema_analyzer .rs
3+ description : " \" WITH res AS (\n SELECT '2024-12-31 10:00:00.000'::TIMESTAMP as t\n UNION ALL\n SELECT '9999-12-31 00:00:00.000 +0000'\n )\n SELECT t FROM res\n ORDER BY t\" "
4+ -- -
5+ Ok (
6+ [
7+ " +---------------------+" ,
8+ " | t |" ,
9+ " +---------------------+" ,
10+ " | 2024-12-31T10:00:00 |" ,
11+ " | 9999-12-31T00:00:00 |" ,
12+ " +---------------------+" ,
13+ ],
14+ )
Original file line number Diff line number Diff line change @@ -9,7 +9,19 @@ test_query!(
99 union_with_timestamp_microseconds,
1010 "SELECT '2024-12-31 10:00:00.000'::TIMESTAMP as t
1111 UNION ALL
12- SELECT '9999-12-31 00:00:00.000 +0000' AS t" ,
13- sort_all = true ,
12+ SELECT '9999-12-31 00:00:00.000 +0000' AS t
13+ ORDER BY 1" ,
14+ snapshot_path = "union_schema_analyzer"
15+ ) ;
16+
17+ test_query ! (
18+ union_with_timestamp_microseconds_cte,
19+ "WITH res AS (
20+ SELECT '2024-12-31 10:00:00.000'::TIMESTAMP as t
21+ UNION ALL
22+ SELECT '9999-12-31 00:00:00.000 +0000'
23+ )
24+ SELECT t FROM res
25+ ORDER BY t" ,
1426 snapshot_path = "union_schema_analyzer"
1527) ;
You can’t perform that action at this time.
0 commit comments