Skip to content

Commit ee26712

Browse files
committed
fix: rescale decimal results from dispatched functions to the declared type
Since #5692 the codegen dispatcher runs Invoke and StaticInvoke calls, including DSv2 catalog functions with a magic method. Such a function can return a Decimal whose scale differs from its declared result type, and the dispatcher wrote it without rescaling: off by a power of ten for precision up to 18, and a task failure above that. Spark's row writer rescales the value with changePrecision and writes null when it does not fit. The dispatcher's decimal writer now does the same, for top-level and nested outputs. Closes #6425.
1 parent 8e4bded commit ee26712

4 files changed

Lines changed: 124 additions & 9 deletions

File tree

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,37 @@ private[codegen] object CometBatchKernelCodegenOutput extends CometTypeShim {
231231
val set = if (nested) "setSafe" else "set"
232232
OutputEmit("", s"$targetVec.$set($idx, $source);")
233233
case dt: DecimalType =>
234+
// Rescale to the declared type, and write null when the value does not fit, as Spark's
235+
// `UnsafeRowWriter` and `UnsafeArrayWriter` do in `write(ordinal, Decimal, precision,
236+
// scale)`. A Spark expression already produces its declared precision and scale, but a
237+
// DSv2 function called through `Invoke` / `StaticInvoke` can return a `Decimal` of any
238+
// scale (#6425). Like Spark's writers, this rescales the value in place, and leaves it
239+
// untouched when it does not fit.
240+
//
241+
// The precision and scale test repeats `changePrecision`'s own fast path. It keeps the call
242+
// off the common path, so the JIT can still scalar-replace the `Decimal` that an input
243+
// getter allocates. With the bare call, passing a `DECIMAL(18, 2)` column through took
244+
// about half as long again per row.
245+
//
234246
// DecimalOutputShortFastPath: precision <= 18 fits in a signed long, so pass the unscaled
235247
// value to `setSafe(int, long)` and skip the BigDecimal allocation.
248+
val dec = ctx.freshName("dec")
249+
val (precision, scale) = (dt.precision, dt.scale)
236250
val write =
237-
if (dt.precision <= Decimal.MAX_LONG_DIGITS) {
238-
s"$targetVec.setSafe($idx, $source.toUnscaledLong());"
251+
if (precision <= Decimal.MAX_LONG_DIGITS) {
252+
s"$targetVec.setSafe($idx, $dec.toUnscaledLong());"
239253
} else {
240-
s"$targetVec.setSafe($idx, $source.toJavaBigDecimal());"
254+
s"$targetVec.setSafe($idx, $dec.toJavaBigDecimal());"
241255
}
242-
OutputEmit("", write)
256+
OutputEmit(
257+
"",
258+
s"""org.apache.spark.sql.types.Decimal $dec = $source;
259+
|if (($dec.precision() == $precision && $dec.scale() == $scale) ||
260+
| $dec.changePrecision($precision, $scale)) {
261+
| $write
262+
|} else {
263+
| $targetVec.setNull($idx);
264+
|}""".stripMargin)
243265
case _: StringType =>
244266
// Utf8OutputOnHeapShortcut: when the UTF8String is on-heap (Spark's string functions
245267
// allocate results on-heap), pass its backing byte[] directly to `setSafe`, skipping the

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ object CometStaticInvoke extends CometExpressionSerde[StaticInvoke] {
8181
* dispatcher, and at least one of those is not dispatchable. `CometIcebergTruncate` declines a
8282
* decimal because Iceberg's `truncate` can return a value wider than the column's declared
8383
* 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 produces
85-
* the out-of-range value instead of a null. The mixin's contract ("the case must be something
86-
* `doGenCode` can compile") does not cover a limit that lives at the Arrow output boundary, so
87-
* enrollment stays with the individual handlers.
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.
8889
*/
8990
override def getSupportLevel(expr: StaticInvoke): SupportLevel =
9091
handlerFor(expr)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ class CometCodegenSourceSuite extends AnyFunSuite {
401401
!result.body.contains(".toJavaBigDecimal("),
402402
"expected no BigDecimal allocation for short-precision output; got:\n" +
403403
CodeFormatter.format(result.code))
404+
// Rescaled to the declared type first, as Spark's row writer does (#6425).
405+
assert(
406+
result.body.contains(".changePrecision(18, 2)"),
407+
s"expected changePrecision before the write; got:\n${CodeFormatter.format(result.code)}")
404408
}
405409

406410
test("DecimalVector setSafe uses BigDecimal slow path for long-precision output") {
@@ -417,6 +421,9 @@ class CometCodegenSourceSuite extends AnyFunSuite {
417421
!result.body.contains(".toUnscaledLong()"),
418422
"expected no unscaled-long write for long-precision output; got:\n" +
419423
CodeFormatter.format(result.code))
424+
assert(
425+
result.body.contains(".changePrecision(38, 10)"),
426+
s"expected changePrecision before the write; got:\n${CodeFormatter.format(result.code)}")
420427
}
421428

422429
test("VarCharVector setSafe uses on-heap UTF8String shortcut") {

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ import org.scalatest.exceptions.TestFailedException
2525

2626
import org.apache.arrow.vector._
2727
import org.apache.spark.{SparkConf, SparkEnv, TaskContext}
28-
import org.apache.spark.sql.CometTestBase
28+
import org.apache.spark.sql.{CometTestBase, Row}
2929
import org.apache.spark.sql.api.java.UDF1
30+
import org.apache.spark.sql.catalyst.InternalRow
31+
import org.apache.spark.sql.catalyst.analysis.NoSuchFunctionException
3032
import org.apache.spark.sql.catalyst.expressions.{Add, Alias, AttributeReference, BoundReference, Cast, CreateArray, CreateMap, CreateNamedStruct, Expression, Hypot, Literal, MapConcat}
3133
import org.apache.spark.sql.catalyst.expressions.objects.Invoke
3234
import org.apache.spark.sql.comet.CometProjectExec
35+
import org.apache.spark.sql.connector.catalog.{FunctionCatalog, Identifier}
36+
import org.apache.spark.sql.connector.catalog.functions.{BoundFunction, ScalarFunction, UnboundFunction}
3337
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
3438
import org.apache.spark.sql.internal.SQLConf
3539
import org.apache.spark.sql.types._
40+
import org.apache.spark.sql.util.CaseInsensitiveStringMap
3641
import org.apache.spark.unsafe.types.UTF8String
3742

3843
import org.apache.comet.CometSparkSessionExtensions.isSpark41Plus
@@ -2327,6 +2332,48 @@ class CometCodegenSuite
23272332
Invoke(target, "twice", StringType, Seq(Literal(UTF8String.fromString("ab"), StringType)))
23282333
assert(runKernel(folded, 1)(_.getUTF8String(0).toString) === "abab")
23292334
}
2335+
2336+
test("decimal results of a DSv2 function are rescaled to the declared type (#6425)") {
2337+
// Spark lowers a call to a DSv2 function with an instance `invoke` method to `Invoke`, which
2338+
// the dispatcher runs. Both functions return `Decimal(i)` at scale 0, one declaring
2339+
// `DECIMAL(10, 2)` and one `DECIMAL(20, 12)`, which covers both of the dispatcher's decimal
2340+
// writers. Spark's row writer rescales the value with `changePrecision` and writes null when
2341+
// it does not fit: 100000000 has nine integer digits and both types allow eight. Spark adds
2342+
// no overflow check around the call, so that null does not depend on ANSI mode. `map` is
2343+
// itself dispatched, so its value exercises the nested writer.
2344+
def dec(s: String) = new java.math.BigDecimal(s)
2345+
val expected = Seq(
2346+
Row(3, dec("3.00"), dec("3.000000000000"), Map("k" -> dec("3.00"))),
2347+
Row(-7, dec("-7.00"), dec("-7.000000000000"), Map("k" -> dec("-7.00"))),
2348+
Row(null, null, null, Map("k" -> null)),
2349+
Row(
2350+
99999999,
2351+
dec("99999999.00"),
2352+
dec("99999999.000000000000"),
2353+
Map("k" -> dec("99999999.00"))),
2354+
Row(100000000, null, null, Map("k" -> null)))
2355+
withSQLConf(
2356+
"spark.sql.catalog.decfn" -> classOf[CometCodegenSuite.DecimalFunctionCatalog].getName) {
2357+
withTable("t") {
2358+
sql("CREATE TABLE t (i INT) USING parquet")
2359+
// One file, so the kernel sees every row in one batch.
2360+
sql(
2361+
"INSERT INTO t SELECT /*+ REPARTITION(1) */ * FROM " +
2362+
"VALUES (3), (-7), (NULL), (99999999), (100000000) AS v(i)")
2363+
for (ansi <- Seq("true", "false")) {
2364+
withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi) {
2365+
val df = sql(
2366+
"SELECT i, decfn.ns.as_money(i), decfn.ns.as_wide_money(i), " +
2367+
"map('k', decfn.ns.as_money(i)) FROM t")
2368+
assertCodegenRan {
2369+
checkSparkAnswerAndOperator(df)
2370+
}
2371+
checkAnswer(df, expected)
2372+
}
2373+
}
2374+
}
2375+
}
2376+
}
23302377
}
23312378

23322379
/**
@@ -2346,6 +2393,44 @@ object CometCodegenSuite {
23462393
class NotSerializableTarget {
23472394
def twice(s: UTF8String): UTF8String = UTF8String.fromString(s.toString + s.toString)
23482395
}
2396+
2397+
/**
2398+
* DSv2 function catalog for the #6425 test. `as_money` declares `DECIMAL(10, 2)` and
2399+
* `as_wide_money` declares `DECIMAL(20, 12)`.
2400+
*/
2401+
class DecimalFunctionCatalog extends FunctionCatalog {
2402+
private val functions = Map(
2403+
"as_money" -> new ScaleZeroDecimalFunction(10, 2),
2404+
"as_wide_money" -> new ScaleZeroDecimalFunction(20, 12))
2405+
private var catalogName: String = _
2406+
2407+
override def initialize(name: String, options: CaseInsensitiveStringMap): Unit =
2408+
catalogName = name
2409+
2410+
override def name(): String = catalogName
2411+
2412+
override def listFunctions(namespace: Array[String]): Array[Identifier] =
2413+
functions.keys.map(Identifier.of(namespace, _)).toArray
2414+
2415+
override def loadFunction(ident: Identifier): UnboundFunction =
2416+
functions.getOrElse(ident.name(), throw new NoSuchFunctionException(ident))
2417+
}
2418+
2419+
/**
2420+
* Returns its `INT` argument as `Decimal(v)`, at scale 0, whatever scale it declares. `invoke`
2421+
* is an instance method, so Spark lowers a call to `Invoke`. The function binds to itself.
2422+
*/
2423+
class ScaleZeroDecimalFunction(precision: Int, scale: Int)
2424+
extends UnboundFunction
2425+
with ScalarFunction[Decimal] {
2426+
override def name(): String = "scale_zero_decimal"
2427+
override def description(): String = s"int -> decimal($precision, $scale), at scale 0"
2428+
override def bind(inputType: StructType): BoundFunction = this
2429+
override def inputTypes(): Array[DataType] = Array(IntegerType)
2430+
override def resultType(): DataType = DecimalType(precision, scale)
2431+
def invoke(v: Int): Decimal = Decimal(v)
2432+
override def produceResult(input: InternalRow): Decimal = invoke(input.getInt(0))
2433+
}
23492434
}
23502435

23512436
/**

0 commit comments

Comments
 (0)