MCPcopy Create free account
hub / github.com/apache/datafusion / normalize_union_schema

Function normalize_union_schema

datafusion/sql/src/unparser/rewrite.rs:57–83  ·  view source on GitHub ↗

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/

(plan: &LogicalPlan)

Source from the content-addressed store, hash-verified

55///
56/// Which would result in a SQL error, as `table1.foo` is not a valid reference in the context of the UNION.
57pub(super) fn normalize_union_schema(plan: &LogicalPlan) -> Result<LogicalPlan> {
58 let plan = plan.clone();
59
60 let transformed_plan = plan.transform_up(|plan| match plan {
61 LogicalPlan::Union(mut union) => {
62 let schema = Arc::unwrap_or_clone(union.schema);
63 let schema = schema.strip_qualifiers();
64
65 union.schema = Arc::new(schema);
66 Ok(Transformed::yes(LogicalPlan::Union(union)))
67 }
68 LogicalPlan::Sort(sort) => {
69 // Only rewrite Sort expressions that have a UNION as their input
70 if !matches!(&*sort.input, LogicalPlan::Union(_)) {
71 return Ok(Transformed::no(LogicalPlan::Sort(sort)));
72 }
73
74 Ok(Transformed::yes(LogicalPlan::Sort(Sort {
75 expr: rewrite_sort_expr_for_union(sort.expr)?,
76 input: sort.input,
77 fetch: sort.fetch,
78 })))
79 }
80 _ => Ok(Transformed::no(plan)),
81 });
82 transformed_plan.data()
83}
84
85/// Rewrite sort expressions that have a UNION plan as their input to remove the table reference.
86fn rewrite_sort_expr_for_union(exprs: Vec<SortExpr>) -> Result<Vec<SortExpr>> {

Callers 1

plan_to_sqlMethod · 0.85

Calls 8

newFunction · 0.85
transform_upMethod · 0.80
strip_qualifiersMethod · 0.80
UnionClass · 0.50
SortClass · 0.50
cloneMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…