-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rewrite coerce_plan_expr_for_schema to fix union type coercion #4862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9298733
6ebcd3c
02e6a55
1cf68d7
5825a61
96df497
bfd500c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # 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. | ||
|
|
||
| ########## | ||
| ## UNION Tests | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ########## | ||
|
|
||
| statement ok | ||
| CREATE TABLE t1( | ||
| id INT, | ||
| name TEXT, | ||
| ) as VALUES | ||
| (1, 'Alex'), | ||
| (2, 'Bob'), | ||
| (3, 'Alice') | ||
| ; | ||
|
|
||
| statement ok | ||
| CREATE TABLE t2( | ||
| id TINYINT, | ||
| name TEXT, | ||
| ) as VALUES | ||
| (1, 'Alex'), | ||
| (2, 'Bob'), | ||
| (3, 'John') | ||
| ; | ||
|
|
||
| # union with EXCEPT(JOIN) | ||
| query T | ||
| ( | ||
| SELECT name FROM t1 | ||
| EXCEPT | ||
| SELECT name FROM t2 | ||
| ) | ||
| UNION ALL | ||
| ( | ||
| SELECT name FROM t2 | ||
| EXCEPT | ||
| SELECT name FROM t1 | ||
| ) | ||
| ORDER BY name | ||
| ---- | ||
| Alice | ||
| John | ||
|
|
||
|
|
||
|
|
||
| # union with type coercion | ||
| query T | ||
| ( | ||
| SELECT * FROM t1 | ||
| EXCEPT | ||
| SELECT * FROM t2 | ||
| ) | ||
| UNION ALL | ||
| ( | ||
| SELECT * FROM t2 | ||
| EXCEPT | ||
| SELECT * FROM t1 | ||
| ) | ||
| ORDER BY name | ||
| ---- | ||
| 3 Alice | ||
| 3 John | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,7 @@ use crate::expr::{ | |||||||||||||||||||
| Like, Sort, TryCast, WindowFunction, | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| use crate::logical_plan::{Aggregate, Projection}; | ||||||||||||||||||||
| use crate::utils::{from_plan, grouping_set_to_exprlist}; | ||||||||||||||||||||
| use crate::utils::grouping_set_to_exprlist; | ||||||||||||||||||||
| use crate::{Expr, ExprSchemable, LogicalPlan}; | ||||||||||||||||||||
| use datafusion_common::Result; | ||||||||||||||||||||
| use datafusion_common::{Column, DFSchema}; | ||||||||||||||||||||
|
|
@@ -525,29 +525,55 @@ pub fn coerce_plan_expr_for_schema( | |||||||||||||||||||
| plan: &LogicalPlan, | ||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was somewhat confused about this at first because the name of this function it turns out it is only called for coercing union schemas: https://github.com/search?q=repo%3Aapache%2Farrow-datafusion%20coerce_plan_expr_for_schema&type=code 👍 |
||||||||||||||||||||
| schema: &DFSchema, | ||||||||||||||||||||
| ) -> Result<LogicalPlan> { | ||||||||||||||||||||
| let new_expr = plan | ||||||||||||||||||||
| .expressions() | ||||||||||||||||||||
| .into_iter() | ||||||||||||||||||||
| match plan { | ||||||||||||||||||||
| LogicalPlan::Projection(Projection { expr, input, .. }) => { | ||||||||||||||||||||
ygf11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
| let new_exprs = coerce_exprs_for_schema(expr, input.schema(), schema)?; | ||||||||||||||||||||
| let projection = Projection::try_new(new_exprs, input.clone())?; | ||||||||||||||||||||
| Ok(LogicalPlan::Projection(projection)) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| _ => { | ||||||||||||||||||||
| let exprs: Vec<Expr> = plan | ||||||||||||||||||||
| .schema() | ||||||||||||||||||||
| .fields() | ||||||||||||||||||||
| .iter() | ||||||||||||||||||||
| .map(|field| Expr::Column(field.qualified_column())) | ||||||||||||||||||||
| .collect(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| let new_exprs = coerce_exprs_for_schema(&exprs, plan.schema(), schema)?; | ||||||||||||||||||||
| let add_project = new_exprs.iter().any(|expr| expr.try_into_col().is_err()); | ||||||||||||||||||||
| if add_project { | ||||||||||||||||||||
| let projection = Projection::try_new(new_exprs, Arc::new(plan.clone()))?; | ||||||||||||||||||||
| Ok(LogicalPlan::Projection(projection)) | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| Ok(plan.clone()) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| fn coerce_exprs_for_schema( | ||||||||||||||||||||
| exprs: &[Expr], | ||||||||||||||||||||
|
||||||||||||||||||||
| exprs: &[Expr], | |
| exprs: Vec<Expr>, |
Since the caller owns the Exprs already, I think you can avoid the clones in this function by passing in the Vec directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can write this more concisely like:
| Expr::Alias(e, alias) => { | |
| let new_expr = e.clone().cast_to(new_type, src_schema)?; | |
| Ok(Expr::Alias(Box::new(new_expr), alias.clone())) | |
| } | |
| Expr::Alias(e, alias) => { | |
| Ok(e.clone() | |
| .cast_to(new_type, src_schema)? | |
| .alias(alias)) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as the above test.