Skip to content

Commit c362d0f

Browse files
committed
fix: check query stages in the UDF benchmark and reframe when to write a vectorized UDF
- The benchmark's native-plan check stripped only the top-level adaptive node, so it reported a ShuffleQueryStage as a Spark operator. Check the plan inside each query stage, as CometTestBase.checkCometOperatorsInFinalPlan does. - For simple functions, most of the gap to the codegen dispatcher is its overhead around the call rather than the call itself. Reframe the user guide around what a function of one row cannot express, and say so in the benchmark's scaladoc.
1 parent 7ab1aa8 commit c362d0f

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

‎docs/source/user-guide/latest/jvm_udfs.md‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ Arrow vector.
2626
This is different from [Scala UDF and Java UDF Support](scala_java_udfs.md), which covers
2727
ordinary Spark UDFs. Comet runs those through a code generator that calls the function once per
2828
row, converting every value to and from the function's Java or Scala types. A vectorized UDF is
29-
called once per batch and reads and writes Arrow buffers directly, so it suits a function that is
30-
a tight loop over primitive values, or one that makes a single call per batch into a library that
31-
already works on batches.
29+
called once per batch and reads and writes Arrow buffers directly.
3230

3331
> **Experimental.** This feature is experimental. `CometJvmUDF` and the `CometUDF` interface are
3432
> not part of Comet's supported API: they fall under
@@ -48,10 +46,16 @@ already works on batches.
4846
| When Comet does not run the operator | Runs on Spark | Runs on Spark one row at a time, much more slowly |
4947
| Compiled against | Spark | Spark and Comet, rebuilt for each Comet release |
5048

51-
An ordinary UDF needs no Comet-specific code, and Comet already runs it in its pipeline. Write a
52-
vectorized UDF when calling the function once per row is itself the cost: for example when it
53-
reads a primitive column in a loop that benefits from staying in one place, or when it hands each
54-
batch to a library in one call.
49+
An ordinary UDF needs no Comet-specific code, and Comet already runs it in its pipeline. The
50+
code generator compiles the call into a loop that the JVM optimizes well, so rewriting a simple
51+
function of primitive values in vectorized form gains little. Write a vectorized UDF for what a
52+
function of one row cannot express:
53+
54+
- Work done once per batch rather than once per row, such as building a lookup structure from the
55+
batch's values.
56+
- A different representation of the values, such as working on the UTF-8 bytes of a string
57+
column instead of one `String` per row.
58+
- One call per batch into a library that already works on batches.
5559

5660
## Writing a UDF
5761

‎spark/src/test/scala/org/apache/spark/sql/benchmark/CometJvmUdfBenchmark.scala‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package org.apache.spark.sql.benchmark
2222
import org.apache.arrow.vector.{BigIntVector, BitVectorHelper, ValueVector}
2323
import org.apache.spark.benchmark.Benchmark
2424
import org.apache.spark.sql.Row
25+
import org.apache.spark.sql.execution.adaptive.{AQEShuffleReadExec, QueryStageExec}
2526
import org.apache.spark.sql.types.LongType
2627

2728
import org.apache.comet.CometConf
@@ -39,8 +40,11 @@ import org.apache.comet.udf.{CometJvmUDF, CometUDF}
3940
* itself.
4041
*
4142
* The vectorized form reads and writes the value buffers directly and copies the validity bitmap
42-
* in one call, which is what the API exists to allow. Its arguments are evaluated natively, so
43-
* only the column itself crosses into the JVM.
43+
* in one call. Its arguments are evaluated natively, so only the column itself crosses into the
44+
* JVM. For functions this simple, most of the difference from the dispatched form is the
45+
* dispatcher's overhead around the call, not the call itself, which the JIT inlines into the
46+
* generated loop: Spark guards a primitive parameter with a null check that runs natively as a
47+
* `CASE` over the batch, and the dispatcher pays a fixed cost per batch.
4448
*
4549
* A last row runs the query without the UDF. Its time is the floor every Comet row shares, so the
4650
* difference between it and a UDF row is what that form of the UDF costs.
@@ -106,28 +110,35 @@ object CometJvmUdfBenchmark extends CometBenchmarkBase {
106110
* that Spark evaluates runs one row at a time and still returns the right answer.
107111
*/
108112
private def verifyFormsAgree(fn: String): Unit = {
109-
val results = forms.map(f => f.label -> collect(f, fn))
113+
val results = forms.map(f => f.label -> rows(f, fn))
110114
val (_, expected) = results.head
111115
results.tail.foreach { case (label, rows) =>
112116
assert(rows == expected, s"$fn: $label returned $rows, not $expected")
113117
}
114118
}
115119

116120
/** `withSQLConf` returns `Unit` on Spark 3.x, so the rows leave through a local. */
117-
private def collect(form: Form, fn: String): Seq[Row] = {
118-
var rows: Seq[Row] = Nil
121+
private def rows(form: Form, fn: String): Seq[Row] = {
122+
var result: Seq[Row] = Nil
119123
withSQLConf(form.configs: _*) {
120124
val df = spark.sql(query(form.udfPrefix + fn))
121-
rows = df.collect().toSeq
125+
result = df.collect().toSeq
122126
if (form.native) {
123-
val plan = stripAQEPlan(df.queryExecution.executedPlan)
124-
findFirstNonCometOperator(plan).foreach { op =>
125-
throw new IllegalStateException(
126-
s"$fn: ${form.label} ran ${op.nodeName} in Spark:\n${plan.treeString}")
127-
}
127+
// A query stage is a leaf, so the plan inside each stage is checked too, as
128+
// `CometTestBase.checkCometOperatorsInFinalPlan` does.
129+
val plan = df.queryExecution.executedPlan
130+
val stagePlans = collect(plan) { case s: QueryStageExec => s.plan }
131+
(stripAQEPlan(plan) +: stagePlans)
132+
.flatMap(
133+
findFirstNonCometOperator(_, classOf[QueryStageExec], classOf[AQEShuffleReadExec]))
134+
.headOption
135+
.foreach { op =>
136+
throw new IllegalStateException(
137+
s"$fn: ${form.label} ran ${op.nodeName} in Spark:\n${stripAQEPlan(plan)}")
138+
}
128139
}
129140
}
130-
rows
141+
result
131142
}
132143

133144
private def runCase(name: String, fn: String): Unit = {

0 commit comments

Comments
 (0)