-
Notifications
You must be signed in to change notification settings - Fork 28.4k
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-33427][SQL] Add subexpression elimination for interpreted expression evaluation #30341
Changes from 7 commits
c7fdae5
33ac8b4
6bab83c
ddd3a96
e8449ec
4780b65
47bae35
5b631a3
77168fe
db115d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.spark.sql.catalyst.expressions | ||
|
||
import scala.collection.mutable | ||
|
||
import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache} | ||
import com.google.common.util.concurrent.{ExecutionError, UncheckedExecutionException} | ||
|
||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} | ||
import org.apache.spark.sql.types.DataType | ||
|
||
/** | ||
* This class helps subexpression elimination for interpreted evaluation | ||
* such as `InterpretedUnsafeProjection`. It maintains an evaluation cache. | ||
* This class wraps `ExpressionProxy` around given expressions. The `ExpressionProxy` | ||
* intercepts expression evaluation and loads from the cache first. | ||
*/ | ||
class SubExprEvaluationRuntime(cacheMaxEntries: Int) { | ||
|
||
private[sql] val cache: LoadingCache[ExpressionProxy, ResultProxy] = CacheBuilder.newBuilder() | ||
.maximumSize(cacheMaxEntries) | ||
.build( | ||
new CacheLoader[ExpressionProxy, ResultProxy]() { | ||
override def load(expr: ExpressionProxy): ResultProxy = { | ||
ResultProxy(expr.proxyEval(currentInput)) | ||
} | ||
}) | ||
|
||
private var currentInput: InternalRow = null | ||
|
||
def getEval(proxy: ExpressionProxy): Any = try { | ||
cache.get(proxy).result | ||
} catch { | ||
// Cache.get() may wrap the original exception. See the following URL | ||
// http://google.github.io/guava/releases/14.0/api/docs/com/google/common/cache/ | ||
// Cache.html#get(K,%20java.util.concurrent.Callable) | ||
case e @ (_: UncheckedExecutionException | _: ExecutionError) => | ||
throw e.getCause | ||
} | ||
|
||
/** | ||
* Sets given input row as current row for evaluating expressions. This cleans up the cache | ||
* too as new input comes. | ||
*/ | ||
def setInput(input: InternalRow = null): Unit = { | ||
currentInput = input | ||
cache.invalidateAll() | ||
} | ||
|
||
/** | ||
* Recursively replaces expression with its proxy expression in `proxyMap`. | ||
*/ | ||
private def replaceWithProxy( | ||
expr: Expression, | ||
proxyMap: Map[Expression, ExpressionProxy]): Expression = { | ||
proxyMap.getOrElse(expr, expr.mapChildren(replaceWithProxy(_, proxyMap))) | ||
} | ||
|
||
/** | ||
* Finds subexpressions and wraps them with `ExpressionProxy`. | ||
*/ | ||
def proxyExpressions(expressions: Seq[Expression]): Seq[Expression] = { | ||
val equivalentExpressions: EquivalentExpressions = new EquivalentExpressions | ||
|
||
expressions.foreach(equivalentExpressions.addExprTree(_)) | ||
|
||
val proxyMap = mutable.Map.empty[Expression, ExpressionProxy] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it OK to use a simple map here? Two expressions may not equal to each other even if they semantically equal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For semantically equal exprs, we put a pair of expr -> proxy into the map and note the proxy is the same. So later we traverse down into expressions, we look at the map. We don't do semantically comparing when looking at this map. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I get it now. Seems we can use |
||
|
||
val commonExprs = equivalentExpressions.getAllEquivalentExprs.filter(_.size > 1) | ||
commonExprs.foreach { e => | ||
val expr = e.head | ||
val proxy = ExpressionProxy(expr, this) | ||
|
||
proxyMap ++= e.map(_ -> proxy).toMap | ||
} | ||
|
||
// Only adding proxy if we find subexpressions. | ||
if (proxyMap.nonEmpty) { | ||
expressions.map(replaceWithProxy(_, proxyMap.toMap)) | ||
} else { | ||
expressions | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A proxy for an catalyst `Expression`. Given a runtime object `SubExprEvaluationRuntime`, | ||
* when this is asked to evaluate, it will load from the evaluation cache in the runtime first. | ||
*/ | ||
case class ExpressionProxy( | ||
cloud-fan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
child: Expression, | ||
runtime: SubExprEvaluationRuntime) extends Expression { | ||
|
||
final override def dataType: DataType = child.dataType | ||
final override def nullable: Boolean = child.nullable | ||
final override def children: Seq[Expression] = child :: Nil | ||
|
||
// `ExpressionProxy` is for interpreted expression evaluation only. So cannot `doGenCode`. | ||
final override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = | ||
throw new UnsupportedOperationException(s"Cannot generate code for expression: $this") | ||
|
||
def proxyEval(input: InternalRow = null): Any = child.eval(input) | ||
|
||
override def eval(input: InternalRow = null): Any = runtime.getEval(this) | ||
} | ||
|
||
/** | ||
* A simple wrapper for holding `Any` in the cache of `SubExprEvaluationRuntime`. | ||
*/ | ||
case class ResultProxy(result: Any) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.spark.sql.catalyst.expressions | ||
|
||
import org.apache.spark.SparkFunSuite | ||
|
||
class SubExprEvaluationRuntimeSuite extends SparkFunSuite { | ||
|
||
test("Evaluate ExpressionProxy should create cached result") { | ||
val runtime = new SubExprEvaluationRuntime(1) | ||
val proxy = ExpressionProxy(Literal(1), runtime) | ||
assert(runtime.cache.size() == 0) | ||
proxy.eval() | ||
assert(runtime.cache.size() == 1) | ||
assert(runtime.cache.get(proxy) == ResultProxy(1)) | ||
} | ||
|
||
test("SubExprEvaluationRuntime cannot exceed configured max entries") { | ||
val runtime = new SubExprEvaluationRuntime(2) | ||
assert(runtime.cache.size() == 0) | ||
|
||
val proxy1 = ExpressionProxy(Literal(1), runtime) | ||
proxy1.eval() | ||
assert(runtime.cache.size() == 1) | ||
assert(runtime.cache.get(proxy1) == ResultProxy(1)) | ||
|
||
val proxy2 = ExpressionProxy(Literal(2), runtime) | ||
proxy2.eval() | ||
assert(runtime.cache.size() == 2) | ||
assert(runtime.cache.get(proxy2) == ResultProxy(2)) | ||
|
||
val proxy3 = ExpressionProxy(Literal(3), runtime) | ||
proxy3.eval() | ||
assert(runtime.cache.size() == 2) | ||
assert(runtime.cache.get(proxy3) == ResultProxy(3)) | ||
} | ||
|
||
test("setInput should empty cached result") { | ||
val runtime = new SubExprEvaluationRuntime(2) | ||
val proxy1 = ExpressionProxy(Literal(1), runtime) | ||
assert(runtime.cache.size() == 0) | ||
proxy1.eval() | ||
assert(runtime.cache.size() == 1) | ||
assert(runtime.cache.get(proxy1) == ResultProxy(1)) | ||
|
||
val proxy2 = ExpressionProxy(Literal(2), runtime) | ||
proxy2.eval() | ||
assert(runtime.cache.size() == 2) | ||
assert(runtime.cache.get(proxy2) == ResultProxy(2)) | ||
|
||
runtime.setInput() | ||
assert(runtime.cache.size() == 0) | ||
} | ||
|
||
test("Wrap ExpressionProxy on subexpressions") { | ||
val runtime = new SubExprEvaluationRuntime(1) | ||
|
||
val one = Literal(1) | ||
val two = Literal(2) | ||
val mul = Multiply(one, two) | ||
val mul2 = Multiply(mul, mul) | ||
val sqrt = Sqrt(mul2) | ||
val sum = Add(mul2, sqrt) | ||
|
||
// ( (one * two) * (one * two) ) + sqrt( (one * two) * (one * two) ) | ||
val proxyExpressions = runtime.proxyExpressions(Seq(sum)) | ||
val proxys = proxyExpressions.flatMap(_.collect { | ||
case p: ExpressionProxy => p | ||
}) | ||
// ( (one * two) * (one * two) ) | ||
assert(proxys.size == 2) | ||
val expected = ExpressionProxy(mul2, runtime) | ||
assert(proxys.head == expected) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, you're right. |
||
} | ||
|
||
test("ExpressionProxy won't be on non deterministic") { | ||
val runtime = new SubExprEvaluationRuntime(1) | ||
|
||
val sum = Add(Rand(0), Rand(0)) | ||
val proxys = runtime.proxyExpressions(Seq(sum, sum)).flatMap(_.collect { | ||
case p: ExpressionProxy => p | ||
}) | ||
assert(proxys.isEmpty) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we test attributes?
To make sure 2 semantically-equal attributes can be optimized. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a top-down traverse, which means we will recursively replace expr with proxy even for
ExpressionProxy
. Is it expected?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we replace one expression with
ExpressionProxy
, we stop traversing down. We only traverse down to children if cannot find current expression inproxyMap
. Is this for your question?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry I misread the code. You are right.