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

[SPARK-49977][SQL] Use stack-based iterative computation to avoid creating many Scala List objects for deep expression trees #48481

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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 @@ -1347,9 +1347,22 @@ 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
f: PartialFunction[CommutativeExpression, Seq[Expression]]): Seq[Expression] = {
val resultBuffer = scala.collection.mutable.Buffer[Expression]()
val stack = scala.collection.mutable.Stack[Expression](e)

// [SPARK-49977]: Use iterative approach to avoid creating many temporary List objects
// for deep expression trees through recursion.
while (stack.nonEmpty) {
val current = stack.pop()
current match {
cloud-fan marked this conversation as resolved.
Show resolved Hide resolved
case c: CommutativeExpression if f.isDefinedAt(c) =>
stack.pushAll(f(c))
case other =>
resultBuffer += other.canonicalized
}
}
resultBuffer.toSeq
}

/**
Expand Down