Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ abstract class Expression extends TreeNode[Expression] {
* This means that the lazy `cannonicalized` is called and computed only on the root of the
* adjacent expressions.
*/
lazy val canonicalized: Expression = {
lazy val canonicalized: Expression = withCanonicalizedChildren

/**
* The default process of canonicalization. It is a one pass, bottum-up expression tree
* computation based oncanonicalizing children before canonicalizing the current node.
*/
final protected def withCanonicalizedChildren: Expression = {
val canonicalizedChildren = children.map(_.canonicalized)
withNewChildren(canonicalizedChildren)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,14 @@ case class Add(
copy(left = newLeft, right = newRight)

override lazy val canonicalized: Expression = {
// TODO: do not reorder consecutive `Add`s with different `evalMode`
orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
// SPARK-40903: Avoid reordering decimal Add for canonicalization, which may change the result
// type and cause error within ComplexTypeMergingExpression.
if (left.resolved && left.dataType.isInstanceOf[DecimalType]) {
withCanonicalizedChildren
} else {
// TODO: do not reorder consecutive `Add`s with different `evalMode`
orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4518,6 +4518,14 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
}
}
}

test("SPARK-40903: Don't reorder Add for canonicalize if it is decimal type") {
val tableName = "decimalTable"
withTable(tableName) {
sql(s"create table $tableName(a decimal(12, 5), b decimal(12, 6)) using orc")
checkAnswer(sql(s"select sum(coalesce(a + b + 1.75, a)) from $tableName"), Row(null))
}
}
}

case class Foo(bar: Option[String])