Skip to content

Commit b441fe5

Browse files
committed
test: check that a decimal past its precision in an unsafe array still fails
Reads an unsafe array of decimals at a narrower precision than it was written with, holding 10^p or -10^p, for both the unscaled long and the unscaled bytes.
1 parent 0182cc9 commit b441fe5

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎spark/src/test/scala/org/apache/spark/sql/comet/execution/arrow/CometArrowWriterSuite.scala‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,35 @@ class CometArrowWriterSuite extends AnyFunSuite with Matchers {
507507
}
508508
}
509509

510+
test("a decimal past its precision in an unsafe array still fails the row path") {
511+
// Written at a wider precision and read at precision p, so the array holds 10^p or -10^p, the
512+
// smallest values past p. Up to 18 digits the array holds the unscaled long, and past that the
513+
// unscaled bytes.
514+
Seq(
515+
(DecimalType(18, 2), DecimalType(5, 2), "1000.00"),
516+
(DecimalType(38, 0), DecimalType(20, 0), "100000000000000000000")).foreach {
517+
case (written, read, magnitude) =>
518+
Seq(magnitude, s"-$magnitude").foreach { value =>
519+
val decimal = Decimal(new JavaBigDecimal(value), written.precision, written.scale)
520+
val row = UnsafeProjection.create(new StructType().add("a", ArrayType(written)))(
521+
new GenericInternalRow(Array[Any](new GenericArrayData(Array[Any](decimal)))))
522+
val allocator = new RootAllocator(Long.MaxValue)
523+
val root = VectorSchemaRoot.create(
524+
Utils.toArrowSchema(new StructType().add("a", ArrayType(read)), "UTC"),
525+
allocator)
526+
try {
527+
val writer = ArrowWriter.create(root, 1)
528+
withClue(s"$read $value: ") {
529+
intercept[ArithmeticException](writer.write(row))
530+
}
531+
} finally {
532+
root.close()
533+
allocator.close()
534+
}
535+
}
536+
}
537+
}
538+
510539
test("a narrow decimal with more digits than its precision passes through") {
511540
// Spark's getDecimal does not check an int- or long-backed value against the precision, so
512541
// neither path does. Arrow's BigDecimal setter, which the writer used before, threw instead.

0 commit comments

Comments
 (0)