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)
| 55 | /// |
| 56 | /// Which would result in a SQL error, as `table1.foo` is not a valid reference in the context of the UNION. |
| 57 | pub(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. |
| 86 | fn rewrite_sort_expr_for_union(exprs: Vec<SortExpr>) -> Result<Vec<SortExpr>> { |
no test coverage detected
searching dependent graphs…