@@ -29,7 +29,7 @@ import scala.util.control.NonFatal
2929import org .apache .arrow .vector ._
3030import org .apache .arrow .vector .complex .{ListVector , MapVector , StructVector }
3131import org .apache .arrow .vector .types .pojo .Field
32- import org .apache .spark .{ SparkEnv , TaskContext }
32+ import org .apache .spark .SparkEnv
3333import org .apache .spark .internal .Logging
3434import org .apache .spark .sql .catalyst .expressions .Expression
3535import org .apache .spark .sql .comet .util .Utils
@@ -41,8 +41,8 @@ import org.apache.comet.udf.CometUDF
4141
4242/**
4343 * Arrow-direct codegen dispatcher. For each `(bound expression, input Arrow schema)` pair,
44- * compiles a specialized [[CometBatchKernel ]] on first encounter, initializes it with the task's
45- * partition index , and caches the live instance.
44+ * compiles a specialized [[CometBatchKernel ]] on first encounter, initializes it with the index
45+ * of the partition the calling native plan computes , and caches the live instance.
4646 *
4747 * Arg 0 is a `VarBinaryVector` scalar carrying the closure-serialized bound `Expression` bytes;
4848 * args 1..N are the data columns the `BoundReference`s read in ordinal order.
@@ -56,16 +56,20 @@ import org.apache.comet.udf.CometUDF
5656 * +----------------------------+ +----------------------------+ +----------------------------+
5757 * | Key: generated Java | | Key: task + UDF class | | Key: bound expression + |
5858 * | source | | | | input column shapes |
59- * | Value: compiled Java class | | Value: dispatcher object | | Value: ready-to-run kernel |
60- * | Scope: JVM, all queries | | Scope: one Spark task | | with state primed |
61- * | share it | | | | Scope: one Spark task |
62- * | Owner: Spark | | Owner: Comet | | (lives inside 2) |
59+ * | Value: compiled Java class | | Value: dispatcher object | | (+ native plan if |
60+ * | Scope: JVM, all queries | | Scope: one Spark task | | nondeterministic) |
61+ * | share it | | | | Value: ready-to-run kernel |
62+ * | Owner: Spark | | Owner: Comet | | with state primed |
63+ * | | | | | Scope: one Spark task |
64+ * | | | | | (lives inside 2), |
65+ * | | | | | or one native plan |
66+ * | | | | | if nondeterministic |
6367 * | | | | | Owner: Comet |
6468 * +----------------------------+ +----------------------------+ +----------------------------+
6569 * }}}
6670 *
67- * Stateful expressions (`Rand`, `MonotonicallyIncreasingID`) advance inside the per-task kernel
68- * across batches.
71+ * Stateful expressions (`Rand`, `MonotonicallyIncreasingID`) advance inside the per-plan kernel
72+ * across batches. `CometExecIterator.close` drops a plan's kernels through `releasePlan`.
6973 *
7074 * `evaluate` runs under `this.synchronized` because DataFusion operators like `HashJoinExec`
7175 * pipeline build/probe via `OnceAsync` (`tokio::spawn`), so multiple Tokio worker threads can
@@ -81,13 +85,34 @@ class CometScalaUDFCodegen extends CometUDF with Logging {
8185 * Per-task cache keyed on serialized expression bytes plus per-column specs. The deserialized
8286 * `boundExpr` carries mutable state (`NamedLambdaVariable.value` for HOFs, `Rand`'s
8387 * `XORShiftRandom`) that must not be shared across concurrent tasks running the same query;
84- * keeping the cache per-task gives each task its own copy. Guarded by `this.synchronized`.
88+ * keeping the cache per-task gives each task its own copy. A nondeterministic kernel is seeded
89+ * from the partition its plan computes, so its key also holds the plan: each plan a task runs,
90+ * such as each parent partition of a coalesce, gets its own, dropped by `releasePlan` when the
91+ * plan closes. Guarded by `this.synchronized`.
8592 */
8693 private val kernelCache
8794 : mutable.Map [CometScalaUDFCodegen .CacheKey , CometScalaUDFCodegen .CacheEntry ] =
8895 mutable.HashMap .empty
8996
90- override def evaluate (inputs : Array [ValueVector ], numRows : Int ): ValueVector = {
97+ // Kernels shared by every plan stay until the task ends.
98+ override def releasePlan (planId : Long ): Unit = this .synchronized {
99+ kernelCache.keys.filter(_.planId == planId).toList.foreach(kernelCache.remove)
100+ }
101+
102+ /** Plan ids of the cached kernels, `NoPlan` for a shared one. */
103+ private [comet] def cachedPlanIds : List [Long ] = this .synchronized {
104+ kernelCache.keysIterator.map(_.planId).toList.sorted
105+ }
106+
107+ // Callers that bypass the bridge (unit tests, benchmarks) have no native plan.
108+ override def evaluate (inputs : Array [ValueVector ], numRows : Int ): ValueVector =
109+ evaluate(inputs, numRows, partitionIndex = 0 , planId = CometScalaUDFCodegen .NoPlan )
110+
111+ override def evaluate (
112+ inputs : Array [ValueVector ],
113+ numRows : Int ,
114+ partitionIndex : Int ,
115+ planId : Long ): ValueVector = {
91116 require(
92117 inputs.length >= 1 ,
93118 " CometScalaUDFCodegen requires at least 1 input (serialized expression), " +
@@ -115,12 +140,12 @@ class CometScalaUDFCodegen extends CometUDF with Logging {
115140 val n = numRows
116141 val specsSeq = specs.toIndexedSeq
117142
118- val key = CometScalaUDFCodegen .CacheKey (ByteBuffer .wrap(bytes), specsSeq)
143+ val key = CometScalaUDFCodegen .CacheKey (planId, ByteBuffer .wrap(bytes), specsSeq)
119144
120145 // Cache lookup and `process` run under one lock to serialize concurrent Tokio callers that
121146 // would otherwise race on the kernel's per-batch instance fields.
122147 this .synchronized {
123- val entry = lookupOrCompile(key, bytes, specsSeq)
148+ val entry = lookupOrCompile(key, bytes, specsSeq, partitionIndex )
124149
125150 val out = CometBatchKernelCodegen .allocateOutput(
126151 entry.outputField,
@@ -144,9 +169,14 @@ class CometScalaUDFCodegen extends CometUDF with Logging {
144169 private def lookupOrCompile (
145170 key : CometScalaUDFCodegen .CacheKey ,
146171 bytes : Array [Byte ],
147- specs : IndexedSeq [ArrowColumnSpec ]): CometScalaUDFCodegen .CacheEntry = {
172+ specs : IndexedSeq [ArrowColumnSpec ],
173+ partitionIndex : Int ): CometScalaUDFCodegen .CacheEntry = {
148174 assert(Thread .holdsLock(this ), " lookupOrCompile must run under this.synchronized" )
149- kernelCache.get(key) match {
175+ // A deterministic kernel never reads the partition index, so one instance under the planless
176+ // key serves every plan in the task. Only a kernel with a nondeterministic node is stored per
177+ // plan.
178+ val sharedKey = key.copy(planId = CometScalaUDFCodegen .NoPlan )
179+ kernelCache.get(sharedKey).orElse(kernelCache.get(key)) match {
150180 case Some (entry) =>
151181 CometScalaUDFCodegen .cacheHitCount.incrementAndGet()
152182 entry
@@ -168,14 +198,17 @@ class CometScalaUDFCodegen extends CometUDF with Logging {
168198 }
169199 val compiled = CometBatchKernelCodegen .compile(boundExpr, specs)
170200 val kernel = compiled.newInstance()
171- kernel.init(CometScalaUDFCodegen .currentPartitionIndex() )
201+ kernel.init(partitionIndex )
172202 val outputField = CometBatchKernelCodegen .toFfiArrowField(
173203 " codegen_result" ,
174204 boundExpr.dataType,
175205 boundExpr.nullable)
176206 val entry =
177207 CometScalaUDFCodegen .CacheEntry (compiled, kernel, boundExpr.dataType, outputField)
178- kernelCache.put(key, entry)
208+ // Walks the tree, because `deterministic` is not transitive everywhere. `Invoke` skips its
209+ // `targetObject`, which is where Spark 4's `make_valid_utf8` puts its input.
210+ val perPlan = boundExpr.exists(! _.deterministic)
211+ kernelCache.put(if (perPlan) key else sharedKey, entry)
179212 CometScalaUDFCodegen .compileCount.incrementAndGet()
180213 CometScalaUDFCodegen .recordCompiledSignature(specs, boundExpr.dataType)
181214 entry
@@ -300,21 +333,22 @@ object CometScalaUDFCodegen {
300333 }
301334
302335 /**
303- * Partition index for the kernel's `init`. Expressions whose `doGenCode` calls
304- * `addPartitionInitializationStatement` (`Rand`, `Randn`, `Uuid`) reseed mutable state from
305- * this. Falls back to 0 when the dispatcher is exercised outside a Spark task (unit tests).
336+ * Plan id for kernels shared by every plan in a task, and for callers that bypass the bridge.
306337 */
307- private def currentPartitionIndex (): Int =
308- Option (TaskContext .get()).map(_.partitionId()).getOrElse(0 )
338+ private [comet] val NoPlan = - 1L
309339
310340 /**
311- * Cache key: serialized expression bytes plus per-column compile-time invariants. `hashCode`
312- * walks `bytesKey` per lookup, so for large ScalaUDF closures it scales with closure size.
341+ * Cache key: calling native plan (`NoPlan` for a deterministic expression), serialized
342+ * expression bytes plus per-column compile-time invariants. `hashCode` walks `bytesKey` per
343+ * lookup, so for large ScalaUDF closures it scales with closure size.
313344 *
314345 * TODO(perf-cache-key): if hot, options are a driver-precomputed hash piggybacked through the
315346 * proto, per-instance last-key memoization, or a two-tier cache keyed on the generated source.
316347 */
317- final case class CacheKey (bytesKey : ByteBuffer , specs : IndexedSeq [ArrowColumnSpec ])
348+ final case class CacheKey (
349+ planId : Long ,
350+ bytesKey : ByteBuffer ,
351+ specs : IndexedSeq [ArrowColumnSpec ])
318352
319353 /** Snapshot of dispatcher cache counters and current size. */
320354 final case class DispatcherStats (compileCount : Long , cacheHitCount : Long , cacheSize : Int ) {
0 commit comments