From 5815ea9d82d2c61139881ec69e531794f5617d81 Mon Sep 17 00:00:00 2001 From: MrHappyEnding <3507482091@qq.com> Date: Wed, 5 Aug 2026 01:52:13 -0700 Subject: [PATCH 1/4] [SPARK-58578][SQL] Mark ApplyFunctionExpression stateful --- .../expressions/ApplyFunctionExpression.scala | 1 + .../ApplyFunctionExpressionSuite.scala | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpression.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpression.scala index 2cee8303dc57b..78f0f8a7af613 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpression.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpression.scala @@ -35,6 +35,7 @@ case class ApplyFunctionExpression( override lazy val deterministic: Boolean = function.isDeterministic && children.forall(_.deterministic) override def foldable: Boolean = deterministic && children.forall(_.foldable) + override def stateful: Boolean = true private lazy val reusedRow = new SpecificInternalRow(function.inputTypes().toImmutableArraySeq) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala new file mode 100644 index 0000000000000..eaf040555c51d --- /dev/null +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala @@ -0,0 +1,44 @@ +/* + * 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 +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.connector.catalog.functions.ScalarFunction +import org.apache.spark.sql.types.{DataType, IntegerType} + +class ApplyFunctionExpressionSuite extends SparkFunSuite { + + private val intIdentity = new ScalarFunction[Int] { + override def inputTypes(): Array[DataType] = Array(IntegerType) + override def resultType(): DataType = IntegerType + override def name(): String = "int_identity" + override def produceResult(input: InternalRow): Int = input.getInt(0) + } + + test("ApplyFunctionExpression is stateful and produces fresh copies") { + val expr = ApplyFunctionExpression( + intIdentity, Seq(BoundReference(0, IntegerType, nullable = false))) + assert(expr.stateful, "ApplyFunctionExpression.stateful should be true") + val copy = expr.freshCopyIfContainsStatefulExpression() + assert(copy ne expr, + "freshCopyIfContainsStatefulExpression should return a new instance " + + "for ApplyFunctionExpression") + assert(copy.eval(InternalRow(7)) === 7) + } +} From 3368c73e12293ca9f9af8dd7bb56ca512764d8b6 Mon Sep 17 00:00:00 2001 From: MrHappyEnding <3507482091@qq.com> Date: Wed, 5 Aug 2026 01:52:13 -0700 Subject: [PATCH 2/4] [SPARK-58578][SQL][TEST] Add JIRA ID to regression test --- .../sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala index eaf040555c51d..f6ccf46ef9e70 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala @@ -31,7 +31,7 @@ class ApplyFunctionExpressionSuite extends SparkFunSuite { override def produceResult(input: InternalRow): Int = input.getInt(0) } - test("ApplyFunctionExpression is stateful and produces fresh copies") { + test("SPARK-58578: ApplyFunctionExpression is stateful and produces fresh copies") { val expr = ApplyFunctionExpression( intIdentity, Seq(BoundReference(0, IntegerType, nullable = false))) assert(expr.stateful, "ApplyFunctionExpression.stateful should be true") From 28a07ede7cf871f1410498c5703f41575a6471a3 Mon Sep 17 00:00:00 2001 From: MrHappyEnding <3507482091@qq.com> Date: Wed, 5 Aug 2026 02:11:08 -0700 Subject: [PATCH 3/4] [SPARK-58578][SQL][TEST] Add concurrent regression test --- .../ApplyFunctionExpressionSuite.scala | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala index f6ccf46ef9e70..f258b26177b9e 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala @@ -17,10 +17,16 @@ package org.apache.spark.sql.catalyst.expressions +import java.util.concurrent.{CountDownLatch, TimeUnit} + +import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.duration._ + import org.apache.spark.SparkFunSuite import org.apache.spark.sql.catalyst.InternalRow import org.apache.spark.sql.connector.catalog.functions.ScalarFunction import org.apache.spark.sql.types.{DataType, IntegerType} +import org.apache.spark.util.ThreadUtils class ApplyFunctionExpressionSuite extends SparkFunSuite { @@ -41,4 +47,41 @@ class ApplyFunctionExpressionSuite extends SparkFunSuite { "for ApplyFunctionExpression") assert(copy.eval(InternalRow(7)) === 7) } + + test("SPARK-58578: concurrent evaluation uses independent input rows") { + val firstEvaluationStarted = new CountDownLatch(1) + val secondEvaluationStarted = new CountDownLatch(1) + val blockingIdentity = new ScalarFunction[Int] { + override def inputTypes(): Array[DataType] = Array(IntegerType) + override def resultType(): DataType = IntegerType + override def name(): String = "blocking_identity" + override def produceResult(input: InternalRow): Int = { + if (input.getInt(0) == 1) { + firstEvaluationStarted.countDown() + assert(secondEvaluationStarted.await(10, TimeUnit.SECONDS)) + } else { + secondEvaluationStarted.countDown() + } + input.getInt(0) + } + } + + val expr = ApplyFunctionExpression( + blockingIdentity, Seq(BoundReference(0, IntegerType, nullable = false))) + val firstEvaluator = expr.freshCopyIfContainsStatefulExpression() + val secondEvaluator = expr.freshCopyIfContainsStatefulExpression() + + val executor = ThreadUtils.newDaemonFixedThreadPool(2, "apply-function-expression-test") + val executionContext = ExecutionContext.fromExecutorService(executor) + try { + val firstResult = Future(firstEvaluator.eval(InternalRow(1)))(executionContext) + assert(firstEvaluationStarted.await(10, TimeUnit.SECONDS)) + val secondResult = Future(secondEvaluator.eval(InternalRow(2)))(executionContext) + + assert(ThreadUtils.awaitResult(firstResult, 10.seconds) === 1) + assert(ThreadUtils.awaitResult(secondResult, 10.seconds) === 2) + } finally { + executor.shutdownNow() + } + } } From 4242235639f01abf678259ede6676248a51a2f95 Mon Sep 17 00:00:00 2001 From: MrHappyEnding <3507482091@qq.com> Date: Wed, 5 Aug 2026 10:33:55 -0700 Subject: [PATCH 4/4] [SPARK-58578][SQL][TEST] Clarify fresh copy regression test name --- .../sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala index f258b26177b9e..9300f107a6aab 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ApplyFunctionExpressionSuite.scala @@ -48,7 +48,7 @@ class ApplyFunctionExpressionSuite extends SparkFunSuite { assert(copy.eval(InternalRow(7)) === 7) } - test("SPARK-58578: concurrent evaluation uses independent input rows") { + test("SPARK-58578: fresh copies do not share the reused input row") { val firstEvaluationStarted = new CountDownLatch(1) val secondEvaluationStarted = new CountDownLatch(1) val blockingIdentity = new ScalarFunction[Int] {