You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by gu...@apache.org on 2024/05/29 02:27:20 UTC
(spark) branch master updated: [SPARK-48445][SQL] Don't inline UDFs with expensive children
This is an automated email from the ASF dual-hosted git repository.
gurwls223 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/master by this push:
new a86bca131028 [SPARK-48445][SQL] Don't inline UDFs with expensive children
a86bca131028 is described below
commit a86bca1310281eab3120eced553eedb5334bcddb
Author: Kelvin Jiang <ke...@databricks.com>
AuthorDate: Wed May 29 11:27:10 2024 +0900
[SPARK-48445][SQL] Don't inline UDFs with expensive children
### What changes were proposed in this pull request?
This PR changes `CollapseProject.isCheap` to avoid inlining UDFs that contain expensive children.
### Why are the changes needed?
In certain cases, such as if the UDF contains an aggregate expression, previously we would also duplicate the aggregate expressions when inlining the UDF. Duplication of the aggregate expressions can cause queries to OOM during execution.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Added a UT.
### Was this patch authored or co-authored using generative AI tooling?
No.
Closes #46780 from kelvinjian-db/SPARK-48445-udf-collapseproject.
Authored-by: Kelvin Jiang <ke...@databricks.com>
Signed-off-by: Hyukjin Kwon <gu...@apache.org>
---
.../spark/sql/catalyst/optimizer/Optimizer.scala | 7 ++++-
.../org/apache/spark/sql/internal/SQLConf.scala | 8 +++++
.../catalyst/optimizer/CollapseProjectSuite.scala | 36 ++++++++++++++++++++--
3 files changed, 48 insertions(+), 3 deletions(-)
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
index 4ee6d9027a9c..e2821a5cff6e 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
@@ -1238,7 +1238,12 @@ object CollapseProject extends Rule[LogicalPlan] with AliasHelper {
case _: Attribute | _: OuterReference => true
case _ if e.foldable => true
// PythonUDF is handled by the rule ExtractPythonUDFs
- case _: PythonUDF => true
+ case _: PythonUDF =>
+ if (conf.getConf(SQLConf.AVOID_COLLAPSE_UDF_WITH_EXPENSIVE_EXPR)) {
+ e.children.forall(isCheap)
+ } else {
+ true
+ }
// Alias and ExtractValue are very cheap.
case _: Alias | _: ExtractValue => e.children.forall(isCheap)
case _ => false
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
index f6f5b23b7f10..d13a59c13578 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
@@ -2587,6 +2587,14 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val AVOID_COLLAPSE_UDF_WITH_EXPENSIVE_EXPR =
+ buildConf("spark.sql.optimizer.avoidCollapseUDFWithExpensiveExpr")
+ .doc("Whether to avoid collapsing projections that would duplicate expensive expressions " +
+ "in UDFs.")
+ .version("4.0.0")
+ .booleanConf
+ .createWithDefault(true)
+
val FILE_SINK_LOG_DELETION = buildConf("spark.sql.streaming.fileSink.log.deletion")
.internal()
.doc("Whether to delete the expired log files in file stream sink.")
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/CollapseProjectSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/CollapseProjectSuite.scala
index c5f506d4d683..e83f231c188e 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/CollapseProjectSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/CollapseProjectSuite.scala
@@ -20,11 +20,12 @@ package org.apache.spark.sql.catalyst.optimizer
import org.apache.spark.sql.catalyst.analysis.EliminateSubqueryAliases
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
-import org.apache.spark.sql.catalyst.expressions.{Alias, Rand, UpdateFields}
+import org.apache.spark.sql.catalyst.expressions.{Alias, CreateArray, Expression, GetArrayItem, PythonUDF, Rand, UpdateFields}
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.RuleExecutor
-import org.apache.spark.sql.types.MetadataBuilder
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{ArrayType, IntegerType, MetadataBuilder}
class CollapseProjectSuite extends PlanTest {
object Optimize extends RuleExecutor[LogicalPlan] {
@@ -266,4 +267,35 @@ class CollapseProjectSuite extends PlanTest {
val expected = relation.select(($"a" + $"b").as("C")).analyze
comparePlans(optimized, expected)
}
+
+ test("ES-1102888: collapse project duplicating aggregate expressions in UDF") {
+ withSQLConf(SQLConf.AVOID_COLLAPSE_UDF_WITH_EXPENSIVE_EXPR.key -> "true") {
+ val pythonUdf = (e: Expression) => {
+ PythonUDF("udf", null, ArrayType(IntegerType), Seq(e), 0, udfDeterministic = true)
+ }
+
+ val query = testRelation
+ .groupBy($"a")(collectList($"b").as("l1"))
+ .select(pythonUdf($"l1").as("l2"))
+ .select(CreateArray(Seq(
+ GetArrayItem($"l2", 0),
+ GetArrayItem($"l2", 1),
+ GetArrayItem($"l2", 2),
+ GetArrayItem($"l2", 3)
+ )))
+ .analyze
+
+ val optimized = Optimize.execute(query)
+ val expected = testRelation
+ .groupBy($"a")(pythonUdf(collectList($"b")).as("l2"))
+ .select(CreateArray(Seq(
+ GetArrayItem($"l2", 0),
+ GetArrayItem($"l2", 1),
+ GetArrayItem($"l2", 2),
+ GetArrayItem($"l2", 3)
+ )))
+ .analyze
+ comparePlans(optimized, expected)
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org