cloud-fan commented on code in PR #37851:
URL: https://github.com/apache/spark/pull/37851#discussion_r969688531
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala:
##########
@@ -1176,3 +1161,21 @@ trait ComplexTypeMergingExpression extends Expression {
trait UserDefinedExpression {
def name: String
}
+
+trait CommutativeExpression extends Expression {
+ /** Collects adjacent commutative operations. */
+ private def gatherCommutative(
+ e: Expression,
+ f: PartialFunction[CommutativeExpression, Seq[Expression]]):
Seq[Expression] = e match {
+ case c: CommutativeExpression if f.isDefinedAt(c) =>
f(c).flatMap(gatherCommutative(_, f))
+ case other => other.canonicalized :: Nil
+ }
+
+ /**
+ * Reorders adjacent commutative operators such as [[And]] in the expression
tree, according to
+ * the `hashCode` of non-commutative nodes, to remove cosmetic variations.
+ */
+ protected def orderCommutative(
+ f: PartialFunction[CommutativeExpression, Seq[Expression]]):
Seq[Expression] =
+ gatherCommutative(this, f).sortBy(_.hashCode())
Review Comment:
nit:
```
protected def isSameOperator(e: CommutativeExpression): Boolean
protected def rebuildCommutativeOperators(input: Seq[Expression]): Expression
private def gatherCommutative(e: Expression): Seq[Expression] = {
if (isSameOperator(e)) e.children.flatMap(gatherCommutative) else
Seq(e.canonicalized)
}
override lazy val canonicalized: Expression = {
val commutativeOperands = gatherCommutative(this)
rebuildCommutativeOperators(commutativeOperands.sortBy(_.hashCode()))
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]