Skip to content

Commit 2949bda

Browse files
committed
fix: dispatch an expression with the DSv2 decimal call it reads, and fall back for aggregates
Spark rescales a DSv2 function's decimal result to its declared type, and writes null when it does not fit, only when it writes a row. An expression around the call reads the Decimal the function returned. The dispatcher writes an Arrow vector of the declared type, so it nulls at its own output, and a native expression over that output read something else: IS NULL was true for a value that does not fit, a cast to string kept the declared scale, and hash read the unscaled value at the declared scale. An expression that takes such a call as an argument now runs in the same kernel as the call, so Spark's own code reads the value the function returned. An aggregate cannot run in the kernel, so count, max and sum over the call fall back to Spark.
1 parent e989715 commit 2949bda

4 files changed

Lines changed: 135 additions & 6 deletions

File tree

‎spark/src/main/scala/org/apache/comet/codegen/CometBatchKernelCodegenOutput.scala‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ private[codegen] object CometBatchKernelCodegenOutput extends CometTypeShim {
240240
// callers write null values themselves, and skip that test only for a type that is not
241241
// nullable.
242242
//
243+
// Only the kernel's own output sees the null. Spark rescales such a value when it writes a
244+
// row, and an expression around the call reads the value the function returned, so
245+
// `QueryPlanSerde` dispatches that expression with the call, and falls an aggregate back.
246+
//
243247
// The precision and scale test repeats `changePrecision`'s own fast path. It keeps the call
244248
// off the common path, so the JIT can still scalar-replace the `Decimal` that an input
245249
// getter allocates. With the bare call, passing a `DECIMAL(18, 2)` column through took

‎spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._
3131
import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, StaticInvoke}
3232
import org.apache.spark.sql.catalyst.expressions.xml.{XPathBoolean, XPathDouble, XPathFloat, XPathInt, XPathList, XPathLong, XPathShort, XPathString}
3333
import org.apache.spark.sql.comet.DecimalPrecision
34+
import org.apache.spark.sql.connector.catalog.functions.ScalarFunction
3435
import org.apache.spark.sql.execution.{ScalarSubquery, SparkPlan}
3536
import org.apache.spark.sql.execution.datasources.parquet.ParquetUtils
3637
import org.apache.spark.sql.internal.SQLConf
@@ -836,6 +837,17 @@ object QueryPlanSerde extends Logging with CometExprShim with CometTypeShim {
836837
}
837838

838839
val fn = aggExpr.aggregateFunction
840+
// An aggregate reads the `Decimal` a DSv2 function returned, whether or not it fits the
841+
// declared type, and the kernel has nulled such a value at its own output: Spark counts it
842+
// and takes it as the maximum, and the row writer nulls the result afterwards. An aggregate
843+
// cannot run in the kernel, so it falls back. See [[readsDispatchedDsv2Decimal]].
844+
if (fn.children.exists(isDispatchedDsv2DecimalCall)) {
845+
withFallbackReason(
846+
aggExpr,
847+
s"${fn.prettyName} aggregates the decimal result of a DSv2 function, which Spark " +
848+
"rescales to the declared type only when it writes a row")
849+
return None
850+
}
839851
val cometExpr = aggrSerdeMap.get(fn.getClass)
840852
val protoAggExprOpt = cometExpr match {
841853
case Some(handler) =>
@@ -1032,6 +1044,12 @@ object QueryPlanSerde extends Logging with CometExprShim with CometTypeShim {
10321044
s"${CometConf.getExprEnabledConfigKey(exprConfName)}=true to enable it.")
10331045
return None
10341046
}
1047+
if (readsDispatchedDsv2Decimal(expr)) {
1048+
// Returns None, tagged with the reason, when the dispatcher declines, which falls the
1049+
// operator back to Spark. Converting `expr` natively is not an alternative: it would read
1050+
// the call's output rather than the value the function returned.
1051+
return CometScalaUDF.emitJvmCodegenDispatch(expr, inputs, binding)
1052+
}
10351053
handler.getSupportLevel(expr) match {
10361054
case Unsupported(notes) =>
10371055
// `CodegenDispatchFallback` serdes have no native path for these cases either, but the
@@ -1141,6 +1159,46 @@ object QueryPlanSerde extends Logging with CometExprShim with CometTypeShim {
11411159
case _ => false
11421160
}
11431161

1162+
/**
1163+
* Whether `expr` takes the decimal result of a DSv2 scalar function call, which the codegen
1164+
* dispatcher runs, as an argument.
1165+
*
1166+
* Spark does not rescale such a result to the type the function declares, or write null when it
1167+
* does not fit, until it writes a row. An expression around the call reads the `Decimal` the
1168+
* function returned. The dispatcher has to write an Arrow vector of the declared type, so it
1169+
* rescales and nulls at its own output, and a native expression over that output would read
1170+
* something else. `IS NULL` of a value that does not fit is false in Spark, a cast to string
1171+
* keeps the function's scale, and `hash` reads the unscaled value at that scale (#6425). So
1172+
* such an expression runs in the same kernel as the call, where Spark's own code reads the
1173+
* value the function returned. `Alias` is skipped because it computes nothing: the call under
1174+
* it is the root, and Spark writes a root as a row.
1175+
*/
1176+
private def readsDispatchedDsv2Decimal(expr: Expression): Boolean =
1177+
!isStructuralExpr(expr) && expr.children.exists(isDispatchedDsv2DecimalCall)
1178+
1179+
private def isDispatchedDsv2DecimalCall(expr: Expression): Boolean = {
1180+
val dispatchedDsv2Call = expr match {
1181+
case i: Invoke =>
1182+
i.targetObject match {
1183+
case Literal(_: ScalarFunction[_], _) => true
1184+
case _ => false
1185+
}
1186+
case s: StaticInvoke =>
1187+
classOf[ScalarFunction[_]].isAssignableFrom(s.staticObject) &&
1188+
CometStaticInvoke.runsInDispatcher(s)
1189+
case _ => false
1190+
}
1191+
dispatchedDsv2Call && containsDecimal(expr.dataType)
1192+
}
1193+
1194+
private def containsDecimal(dataType: DataType): Boolean = dataType match {
1195+
case _: DecimalType => true
1196+
case ArrayType(elementType, _) => containsDecimal(elementType)
1197+
case MapType(keyType, valueType, _) => containsDecimal(keyType) || containsDecimal(valueType)
1198+
case StructType(fields) => fields.exists(f => containsDecimal(f.dataType))
1199+
case _ => false
1200+
}
1201+
11441202
/**
11451203
* Creates a UnaryExpr by calling exprToProtoInternal for the provided child expression and then
11461204
* invokes the supplied function to wrap this UnaryExpr in a top-level Expr.

‎spark/src/main/scala/org/apache/comet/serde/statics.scala‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ object CometStaticInvoke extends CometExpressionSerde[StaticInvoke] {
5858
private def handlerFor(expr: StaticInvoke): Option[CometExpressionSerde[StaticInvoke]] =
5959
staticInvokeExpressions.get((expr.functionName, expr.staticObject.getName))
6060

61+
/** Whether [[convert]] hands `expr` to the codegen dispatcher, because no handler claims it. */
62+
def runsInDispatcher(expr: StaticInvoke): Boolean =
63+
handlerFor(expr).isEmpty && icebergHandlerFor(expr).isEmpty
64+
6165
/**
6266
* Iceberg's system functions are keyed by class name only because both the `StaticInvoke` and
6367
* `ApplyFunctionExpression` lowerings share the same identity class. Consulted after the
@@ -80,12 +84,13 @@ object CometStaticInvoke extends CometExpressionSerde[StaticInvoke] {
8084
* [[CodegenDispatchFallback]]: that would also route a *handler's* `Unsupported` through the
8185
* dispatcher, and at least one of those is not dispatchable. `CometIcebergTruncate` declines a
8286
* decimal because Iceberg's `truncate` can return a value wider than the column's declared
83-
* precision, which Spark nulls only when the row is materialized; the dispatcher writes into an
84-
* Arrow `Decimal128(precision, scale)` vector just like a native kernel does, so it has to null
85-
* the value at its own output, and an enclosing predicate or hash then sees a null where Spark
86-
* sees the oversized value. The mixin's contract ("the case must be something `doGenCode` can
87-
* compile") does not cover a limit that lives at the Arrow output boundary, so enrollment stays
88-
* with the individual handlers.
87+
* precision, which Spark nulls only when the row is materialized. The dispatcher nulls such a
88+
* value at its own output, as the row writer does, and an expression or aggregate that reads a
89+
* dispatched call either runs in the same kernel or falls back (see
90+
* `QueryPlanSerde.readsDispatchedDsv2Decimal`), but the decline predates that and stays. The
91+
* mixin's contract ("the case must be something `doGenCode` can compile") does not cover a
92+
* limit that lives at the Arrow output boundary, so enrollment stays with the individual
93+
* handlers.
8994
*/
9095
override def getSupportLevel(expr: StaticInvoke): SupportLevel =
9196
handlerFor(expr)

‎spark/src/test/scala/org/apache/comet/CometCodegenSuite.scala‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
3939
import org.apache.spark.sql.internal.SQLConf
4040
import org.apache.spark.sql.types._
4141
import org.apache.spark.sql.util.CaseInsensitiveStringMap
42+
import org.apache.spark.unsafe.hash.Murmur3_x86_32
4243
import org.apache.spark.unsafe.types.UTF8String
4344

4445
import org.apache.comet.CometSparkSessionExtensions.isSpark41Plus
@@ -2440,6 +2441,67 @@ class CometCodegenSuite
24402441
s"expected Comet's native projection to reject the null, got $cometError")
24412442
}
24422443
}
2444+
2445+
test("an expression around a DSv2 decimal result reads the value Spark reads (#6425)") {
2446+
// Spark rescales the call's `Decimal` to its declared type, and writes null when it does not
2447+
// fit, only when it writes a row. An expression around the call reads the `Decimal` the
2448+
// function returned, at the scale the function chose. `as_money` returns `Decimal(i)` at scale
2449+
// 0 for a `DECIMAL(10, 2)`:
2450+
// - `IS NULL` is false for 100000000, which needs nine integer digits where the type allows
2451+
// eight, so the row writer's null for it must not reach the expression;
2452+
// - a cast to a wider decimal keeps that value, and so does a comparison, which finds it
2453+
// greater than 5;
2454+
// - `CAST(... AS STRING)` prints "3", not "3.00";
2455+
// - `hash` hashes the unscaled value, so `Decimal(3, 10, 0)` hashes as the long 3.
2456+
// The kernel nulls an oversized decimal at its own output, so each of these expressions has
2457+
// to run in the same kernel as the call.
2458+
val hashes = Seq(3L, null, 100000000L, -100000000L).map {
2459+
case null => 42
2460+
case v: Long => Murmur3_x86_32.hashLong(v, 42)
2461+
}
2462+
withDecimalFunctions(3, null, 100000000, -100000000) {
2463+
for (ansi <- Seq("true", "false")) {
2464+
withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi) {
2465+
val df = sql(
2466+
"SELECT i, decfn.ns.as_money(i) IS NULL, " +
2467+
"CAST(decfn.ns.as_money(i) AS DECIMAL(12, 2)), " +
2468+
"CAST(decfn.ns.as_money(i) AS STRING), hash(decfn.ns.as_money(i)) FROM t")
2469+
assertCodegenRan {
2470+
checkSparkAnswerAndImpl(df, dispatched = Seq("isnull", "cast", "hash"))
2471+
}
2472+
checkAnswer(
2473+
df,
2474+
Seq(
2475+
Row(3, false, dec("3.00"), "3", hashes(0)),
2476+
Row(null, true, null, null, hashes(1)),
2477+
Row(100000000, false, dec("100000000.00"), "100000000", hashes(2)),
2478+
Row(-100000000, false, dec("-100000000.00"), "-100000000", hashes(3))))
2479+
}
2480+
}
2481+
val filtered = sql("SELECT i FROM t WHERE decfn.ns.as_money(i) > 5")
2482+
assertCodegenRan {
2483+
checkSparkAnswerAndImpl(filtered, dispatched = Seq("greaterthan"))
2484+
}
2485+
checkAnswer(filtered, Row(100000000))
2486+
}
2487+
}
2488+
2489+
test(
2490+
"an aggregate over a DSv2 decimal result falls back, as Spark aggregates the value (#6425)") {
2491+
// Spark aggregates the `Decimal` the function returned, and the row writer nulls the result
2492+
// afterwards. `count` counts 100000000, `max` is that value and so is null, and the two
2493+
// values that do not fit cancel in the sum of the even group. An aggregate cannot run in the
2494+
// kernel, which has nulled those values at its output.
2495+
withDecimalFunctions(3, null, 100000000, -100000000) {
2496+
val reason = "aggregates the decimal result of a DSv2 function"
2497+
val global = sql("SELECT count(decfn.ns.as_money(i)), max(decfn.ns.as_money(i)) FROM t")
2498+
checkSparkAnswerAndFallbackReason(global, reason)
2499+
checkAnswer(global, Row(3L, null))
2500+
val grouped = sql("SELECT i % 2, sum(decfn.ns.as_money(i)) FROM t GROUP BY i % 2")
2501+
checkSparkAnswerAndFallbackReason(grouped, reason)
2502+
checkAnswer(grouped, Seq(Row(0, dec("0.00")), Row(1, dec("3.00")), Row(null, null)))
2503+
}
2504+
}
24432505
}
24442506

24452507
/**

0 commit comments

Comments
 (0)