-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strip table qualifiers from schema in
UNION ALL
for unparser (#11082)
* Add test cases for issue * Remove test from logical_plan/builder.rs * Remove table qualifiers for sorts following a Union * Use transform_up and add more documentation on why this is needed
- Loading branch information
1 parent
528c4ab
commit ed7c884
Showing
4 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
mod ast; | ||
mod expr; | ||
mod plan; | ||
mod rewrite; | ||
mod utils; | ||
|
||
pub use expr::expr_to_sql; | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use datafusion_common::{ | ||
tree_node::{Transformed, TransformedResult, TreeNode, TreeNodeIterator}, | ||
Result, | ||
}; | ||
use datafusion_expr::{Expr, LogicalPlan, Sort}; | ||
|
||
/// Normalize the schema of a union plan to remove qualifiers from the schema fields and sort expressions. | ||
/// | ||
/// DataFusion will return an error if two columns in the schema have the same name with no table qualifiers. | ||
/// There are certain types of UNION queries that can result in having two columns with the same name, and the | ||
/// solution was to add table qualifiers to the schema fields. | ||
/// See <https://github.com/apache/datafusion/issues/5410> for more context on this decision. | ||
/// | ||
/// However, this causes a problem when unparsing these queries back to SQL - as the table qualifier has | ||
/// logically been erased and is no longer a valid reference. | ||
/// | ||
/// The following input SQL: | ||
/// ```sql | ||
/// SELECT table1.foo FROM table1 | ||
/// UNION ALL | ||
/// SELECT table2.foo FROM table2 | ||
/// ORDER BY foo | ||
/// ``` | ||
/// | ||
/// Would be unparsed into the following invalid SQL without this transformation: | ||
/// ```sql | ||
/// SELECT table1.foo FROM table1 | ||
/// UNION ALL | ||
/// SELECT table2.foo FROM table2 | ||
/// ORDER BY table1.foo | ||
/// ``` | ||
/// | ||
/// Which would result in a SQL error, as `table1.foo` is not a valid reference in the context of the UNION. | ||
pub(super) fn normalize_union_schema(plan: &LogicalPlan) -> Result<LogicalPlan> { | ||
let plan = plan.clone(); | ||
|
||
let transformed_plan = plan.transform_up(|plan| match plan { | ||
LogicalPlan::Union(mut union) => { | ||
let schema = match Arc::try_unwrap(union.schema) { | ||
Ok(inner) => inner, | ||
Err(schema) => (*schema).clone(), | ||
}; | ||
let schema = schema.strip_qualifiers(); | ||
|
||
union.schema = Arc::new(schema); | ||
Ok(Transformed::yes(LogicalPlan::Union(union))) | ||
} | ||
LogicalPlan::Sort(sort) => { | ||
// Only rewrite Sort expressions that have a UNION as their input | ||
if !matches!(&*sort.input, LogicalPlan::Union(_)) { | ||
return Ok(Transformed::no(LogicalPlan::Sort(sort))); | ||
} | ||
|
||
Ok(Transformed::yes(LogicalPlan::Sort(Sort { | ||
expr: rewrite_sort_expr_for_union(sort.expr)?, | ||
input: sort.input, | ||
fetch: sort.fetch, | ||
}))) | ||
} | ||
_ => Ok(Transformed::no(plan)), | ||
}); | ||
transformed_plan.data() | ||
} | ||
|
||
/// Rewrite sort expressions that have a UNION plan as their input to remove the table reference. | ||
fn rewrite_sort_expr_for_union(exprs: Vec<Expr>) -> Result<Vec<Expr>> { | ||
let sort_exprs: Vec<Expr> = exprs | ||
.into_iter() | ||
.map_until_stop_and_collect(|expr| { | ||
expr.transform_up(|expr| { | ||
if let Expr::Column(mut col) = expr { | ||
col.relation = None; | ||
Ok(Transformed::yes(Expr::Column(col))) | ||
} else { | ||
Ok(Transformed::no(expr)) | ||
} | ||
}) | ||
}) | ||
.data()?; | ||
|
||
Ok(sort_exprs) | ||
} |
This file contains 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