Skip to content

Commit b3a36cc

Browse files
authored
Recompute schemas after union schema change (#1635)
* Recompute schemas after union schema change * Recompute schemas after union schema change
1 parent 8c9d782 commit b3a36cc

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

crates/core-executor/src/datafusion/logical_analyzer/union_schema_analyzer.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,21 @@ impl AnalyzerRule for UnionSchemaAnalyzer {
5555
}
5656

5757
fn 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

6775
fn rewrite_union(union: &Union) -> datafusion_common::Result<LogicalPlan> {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
source: 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
---
55
Ok(
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 numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
)

crates/core-executor/src/tests/sql/logical_analyzer/union_schema_analyzer.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff 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
);

0 commit comments

Comments
 (0)