Skip to content
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

Rewrite coerce_plan_expr_for_schema to fix union type coercion #4862

Merged
merged 7 commits into from
Jan 12, 2023

Conversation

ygf11
Copy link
Contributor

@ygf11 ygf11 commented Jan 9, 2023

Which issue does this PR close?

Closes #4837.

Rationale for this change

The original coerce_plan_expr_for_schema method use the input.expressions() as source of type coercion, which is not correct, because it maybe return other expressions, like join will return its predicates.

What changes are included in this PR?

The source of union type coercion is from input schema or projection exprs now.

Are these changes tested?

Yes.

Are there any user-facing changes?

@github-actions github-actions bot added core Core DataFusion crate logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Jan 9, 2023
@ygf11 ygf11 marked this pull request as ready for review January 9, 2023 10:11
# under the License.

##########
## UNION Tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqllogictest is very easy to use, I like it 👍.

@alamb
Copy link
Contributor

alamb commented Jan 9, 2023

Thanks @ygf11 -- I hope to review this PR more carefully tomorrow

@liukun4515 liukun4515 self-requested a review January 10, 2023 11:25
@liukun4515
Copy link
Contributor

I want to review this pr which is about type coercion.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR carefully both code and tests and it makes sense to me. Thank you @ygf11

I will wait for @liukun4515 's review prior to merging

@@ -525,29 +525,55 @@ pub fn coerce_plan_expr_for_schema(
plan: &LogicalPlan,
Copy link
Contributor

Choose a reason for hiding this comment

The 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

👍

datafusion/expr/src/expr_rewriter.rs Show resolved Hide resolved
Comment on lines 566 to 569
Expr::Alias(e, alias) => {
let new_expr = e.clone().cast_to(new_type, src_schema)?;
Ok(Expr::Alias(Box::new(new_expr), alias.clone()))
}
Copy link
Contributor

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:

Suggested change
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))
}

}

fn coerce_exprs_for_schema(
exprs: &[Expr],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

" Union [name:UInt8;N]",
" LeftAnti Join: t1.name = t2.name [name:UInt8;N]",
" Distinct: [name:UInt8;N]",
" TableScan: t1 projection=[name] [name:UInt8;N]",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This plan is changed after merging master, cause #4849 enable the projection push down of DISTINCT.

Before merge:

"      Distinct: [name:UInt8;N]",
"        Projection: t1.name [name:UInt8;N]",
"          TableScan: t1 projection=[name] [name:UInt8;N]",

After merge:

"      Distinct: [name:UInt8;N]",
"        TableScan: t1 projection=[name] [name:UInt8;N]",

" TableScan: t2 projection=[id, name] [id:UInt8;N, name:UInt8;N]",
" Projection: t1.id, t1.name [id:Int32;N, name:UInt8;N]",
" TableScan: t1 projection=[id, name] [id:Int32;N, name:UInt8;N]",
];
Copy link
Contributor Author

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.

@alamb
Copy link
Contributor

alamb commented Jan 12, 2023

Thanks again @ygf11

@alamb alamb merged commit eb19a67 into apache:master Jan 12, 2023
@ursabot
Copy link

ursabot commented Jan 12, 2023

Benchmark runs are scheduled for baseline = 3b86643 and contender = eb19a67. eb19a67 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@ygf11 ygf11 deleted the union-type-coercion branch January 13, 2023 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL statement (UNION + EXCEPT) causes panic
4 participants