Skip to content

[SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary#38513

Closed
gengliangwang wants to merge 1 commit intoapache:masterfrom
gengliangwang:addCastAfterReorder
Closed

[SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary#38513
gengliangwang wants to merge 1 commit intoapache:masterfrom
gengliangwang:addCastAfterReorder

Conversation

@gengliangwang
Copy link
Member

What changes were proposed in this pull request?

This is a follow-up of #38379. On second thought, if the canonicalized Add has a different type, casting it as the original data type can still match more semantically equivalent Adds

Why are the changes needed?

A better solution for the issue https://issues.apache.org/jira/browse/SPARK-40903. We can avoid regressions on marking on certain semantically equivalent Adds as not equivalent.

Does this PR introduce any user-facing change?

No

How was this patch tested?

New UT

@github-actions github-actions bot added the SQL label Nov 4, 2022
@gengliangwang
Copy link
Member Author

if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {
// SPARK-40903: Append cast for the canonicalization of decimal Add if the result data type is
// changed. Otherwise, it may cause data checking error within ComplexTypeMergingExpression.
Cast(reorderResult, dataType)
Copy link
Contributor

@peter-toth peter-toth Nov 5, 2022

Choose a reason for hiding this comment

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

This seems to be the same idea that has come up previously: #38379 (comment) but my concerns (#38379 (comment)) were wrong, so LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the cast is better after seconds thought.

orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
reorderResult
if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

for safe, can we do this for decimal type only ? e.g

left match {
  case _: DecimalType if resolved && reorderResult.resolved && reorderResult.dataType != dataType =>
    Cast(reorderResult, dataType)
  case _ => reorderResult
}

Copy link
Member Author

Choose a reason for hiding this comment

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

@ulysses-you the current code seems fine. If there is a new data type in the future, we can still avoid the issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not worry about new data types, just want to avoid unnecessary logic for some unrelated data types like integer, float ..

@cloud-fan
Copy link
Contributor

I think canonicalization should not change the data type in the first place. Adding cast only hides the bug. What's worse, it doesn't help with the goal of canonicalization: match plans/expressions that are semantically equal, due to the extra cast.

Can we be stricter on when we can reorder? e.g. add an allowlist and only reorder under certain cases, e.g. integral add with ansi off.

@gengliangwang
Copy link
Member Author

Adding cast only hides the bug

The issue of SPARK-40903 is due to Spark calculating the data type of decimal Add conservatively. What do you mean by "bug"?

@cloud-fan
Copy link
Contributor

We should not change query semantics after reordering, as this is canonicalization. It's hard to convince people that different result types still guarantee the same query semantic.

@gengliangwang
Copy link
Member Author

It's true that having a cast in the canonicalized expression is hacky. I am closing this one and keeping the solution as it is in #38379

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants