@@ -24,7 +24,7 @@ import org.scalatest.funsuite.AnyFunSuite
2424import org .apache .spark .SparkConf
2525import org .apache .spark .serializer .JavaSerializer
2626import org .apache .spark .sql .catalyst .InternalRow
27- import org .apache .spark .sql .catalyst .expressions .{Add , AddMonths , BoundReference , Coalesce , Concat , CreateArray , CreateMap , DateFormatClass , ElementAt , Expression , GetStructField , LeafExpression , Length , Literal , MakeTimestamp , MicrosToTimestamp , MillisToTimestamp , MonthsBetween , Nondeterministic , Rand , Size , ToUnixTimestamp , Unevaluable , UnixMicros , UnixMillis , UnixSeconds , Upper }
27+ import org .apache .spark .sql .catalyst .expressions .{Add , AddMonths , BoundReference , Cast , Coalesce , Concat , CreateArray , CreateMap , DateFormatClass , ElementAt , Expression , GetStructField , LeafExpression , Length , Literal , MakeTimestamp , MicrosToTimestamp , MillisToTimestamp , MonthsBetween , Nondeterministic , Rand , Size , ToUnixTimestamp , Unevaluable , UnixMicros , UnixMillis , UnixSeconds , Upper }
2828import org .apache .spark .sql .catalyst .expressions .codegen .{CodeFormatter , CodegenContext , CodegenFallback , ExprCode }
2929import org .apache .spark .sql .types ._
3030import org .apache .spark .unsafe .types .UTF8String
@@ -41,9 +41,10 @@ import org.apache.comet.udf.codegen.CometScalaUDFCodegen
4141 * assert on the emitted Java directly, without invoking Janino. The goal is to catch regressions
4242 * in the optimizations we claim the dispatcher applies:
4343 *
44- * - `NullIntolerant` short-circuit over a single input ordinal wraps `ev.code` in `if
45- * (input-null) { setNull } else { ev.code; write }`, and is *not* emitted for trees that read
46- * more than one ordinal (see the multi-input test for why).
44+ * - `NullIntolerant` short-circuit wraps `ev.code` in `if (input-null) { setNull } else {
45+ * ev.code; write }`. It is emitted for a tree reading a single ordinal, and for a
46+ * multi-ordinal tree whose root has only leaf children; it is *not* emitted once a subtree
47+ * sits between the root and a leaf (see the multi-input tests for why).
4748 * - Non-nullable column declaration emits `return false;` from `isNullAt(ord)`, and a
4849 * `BoundReference.nullable=false` (Catalyst sets this from schema-declared nullability) makes
4950 * Spark's `doGenCode` skip emitting its own `row.isNullAt(ord)` probe entirely.
@@ -167,17 +168,41 @@ class CometCodegenSourceSuite extends AnyFunSuite {
167168 s " got: \n $src" )
168169 }
169170
170- test(" NullIntolerant short-circuit skipped for a multi-input tree (#5218)" ) {
171+ test(" NullIntolerant short-circuit skipped when a subtree sits between root and leaf (#5218)" ) {
171172 // Even when every node is NullIntolerant, a short-circuit on the *union* of input ordinals is
172- // not equivalent to Spark. Spark's null handling is per-node and left-to-right:
173- // `BinaryExpression.nullSafeCodeGen` emits the left child's code unconditionally, then tests
174- // the left child's null, then the right child's. So for `Add(cast(c0 AS INT), c1)` on a row
175- // where c0 = 'abc' and c1 IS NULL, Spark evaluates the cast first and (under ANSI) raises
176- // CAST_INVALID_INPUT. A union short-circuit would skip the cast and return null, swallowing
177- // the error. See CometCodegenSuite's add_months test for the end-to-end witness.
173+ // not equivalent to Spark once a real subtree sits between the root and a leaf. Spark's null
174+ // handling is per-node and left-to-right: `BinaryExpression.nullSafeCodeGen` emits the left
175+ // child's code unconditionally, then tests the left child's null, then the right child's. So
176+ // for `Add(cast(c0 AS INT), c1)` on a row where c0 = 'abc' and c1 IS NULL, Spark evaluates the
177+ // cast first and (under ANSI) raises CAST_INVALID_INPUT. A union short-circuit would skip the
178+ // cast and return null, swallowing the error. See CometCodegenSuite's add_months test for the
179+ // end-to-end witness.
178180 //
179- // `Add` over two BoundReferences is the minimal shape: NullIntolerant root, NullIntolerant
180- // (leaf-only) children, two distinct ordinals.
181+ // The `Cast` is what disqualifies the short-circuit here: it is a non-leaf child of the root
182+ // that can raise on its own. The sibling test below covers the leaf-only-children shape, which
183+ // keeps the short-circuit.
184+ val strCol = ArrowColumnSpec (varCharVectorClass, nullable = true )
185+ val intCol = ArrowColumnSpec (
186+ CometBatchKernelCodegen .vectorClassBySimpleName(" IntVector" ),
187+ nullable = true )
188+ val expr = Add (
189+ Cast (BoundReference (0 , StringType , nullable = true ), IntegerType , ansiEnabled = true ),
190+ BoundReference (1 , IntegerType , nullable = true ))
191+ val src = gen(expr, strCol, intCol)
192+ assert(
193+ ! src.contains(" this.col0.isNull(i) || this.col1.isNullAt(i)" ),
194+ s " expected no union-of-inputs short-circuit when a Cast sits under the root; got: \n $src" )
195+ assert(
196+ ! src.contains(" if (this.col0.isNull(i))" ) && ! src.contains(" if (this.col1.isNullAt(i))" ),
197+ s " expected no pre-eval input-null short-circuit at all for this shape; got: \n $src" )
198+ }
199+
200+ test(" NullIntolerant short-circuit kept for a multi-input leaf-only tree (#5218)" ) {
201+ // Counterpart to the test above. When every direct child of the root is a `BoundReference` or
202+ // `Literal`, the tree is one level deep: the only code Spark runs ahead of its own null checks
203+ // is leaf reads, which cannot raise. Any error comes from the root's own logic, which runs only
204+ // once every input is known non-null -- in Spark's shape and in ours alike. So the union
205+ // short-circuit is exact and worth keeping for the common no-cast multi-argument case.
181206 val intCol = ArrowColumnSpec (
182207 CometBatchKernelCodegen .vectorClassBySimpleName(" IntVector" ),
183208 nullable = true )
@@ -186,11 +211,8 @@ class CometCodegenSourceSuite extends AnyFunSuite {
186211 BoundReference (1 , IntegerType , nullable = true ))
187212 val src = gen(expr, intCol, intCol)
188213 assert(
189- ! src.contains(" this.col0.isNullAt(i) || this.col1.isNullAt(i)" ),
190- s " expected no union-of-inputs short-circuit for a two-input tree; got: \n $src" )
191- assert(
192- ! src.contains(" if (this.col0.isNullAt(i))" ) && ! src.contains(" if (this.col1.isNullAt(i))" ),
193- s " expected no pre-eval input-null short-circuit at all for a two-input tree; got: \n $src" )
214+ src.contains(" if (this.col0.isNullAt(i) || this.col1.isNullAt(i))" ),
215+ s " expected union-of-inputs short-circuit for a leaf-only two-input tree; got: \n $src" )
194216 }
195217
196218 test(" canHandle accepts CodegenFallback expressions (delegates to eval(row))" ) {
@@ -423,14 +445,17 @@ class CometCodegenSourceSuite extends AnyFunSuite {
423445 CodeFormatter .format(result.code))
424446 }
425447
426- test(" nullable NullIntolerant root keeps post-eval isNull guard (#4554)" ) {
448+ test(" nullable NullIntolerant root keeps post-eval isNull guard inside short-circuit (#4554)" ) {
427449 // `NullIntolerant` only constrains "null in -> null out". An expression can still set
428450 // `ev.isNull = true` from non-null inputs — `MakeTimestamp(failOnError = false)` does this in
429451 // its `doGenCode` catch block when year/month/day/hour/min/sec components are out of range
430452 // (issue #4554). The dispatcher previously assumed NullIntolerant + non-null inputs implied a
431453 // non-null output and dropped the post-eval guard; that wrote stale `ev.value` bytes for
432- // every invalid row. A nullable root must always keep a post-eval `if (ev.isNull) setNull`
433- // check, whether or not the input-null short-circuit also applies.
454+ // every invalid row. The short-circuit on input nulls must coexist with a post-eval
455+ // `if (ev.isNull) setNull` check whenever the expression itself is nullable.
456+ //
457+ // All six children are `BoundReference`s, so this is a leaf-only tree and keeps the
458+ // union-of-ordinals short-circuit (#5218).
434459 val expr = MakeTimestamp (
435460 BoundReference (0 , IntegerType , nullable = true ),
436461 BoundReference (1 , IntegerType , nullable = true ),
@@ -453,17 +478,55 @@ class CometCodegenSourceSuite extends AnyFunSuite {
453478 IndexedSeq (intCol, intCol, intCol, intCol, intCol, decCol))
454479 val src = result.body
455480 val formatted = CodeFormatter .format(result.code)
456- // This tree reads six ordinals, so there is no input-null short-circuit (#5218). Exactly one
457- // setNull site must remain: the post-eval guard that propagates the `ev.isNull = true` set by
458- // MakeTimestamp's catch block on invalid components. Pre-#4554 there were none.
481+ // Two distinct setNull sites must exist: the input-null short-circuit before `ev.code` runs,
482+ // and the post-eval guard that propagates `ev.isNull = true` set by MakeTimestamp's catch
483+ // block on invalid components. Pre-#4554 there was only one (the input short-circuit).
484+ val setNullOccurrences = " output\\ .setNull\\ (i\\ );" .r.findAllIn(src).length
485+ assert(
486+ setNullOccurrences >= 2 ,
487+ " expected at least two setNull sites (input short-circuit + post-eval ev.isNull guard); " +
488+ s " found $setNullOccurrences. Source: \n $formatted" )
489+ // The short-circuit must test every ordinal the tree reads, not just the first.
490+ assert(
491+ src.contains(
492+ " if (this.col0.isNullAt(i) || this.col1.isNullAt(i) || " +
493+ " this.col2.isNullAt(i) || this.col3.isNullAt(i) || this.col4.isNullAt(i) || " +
494+ " this.col5.isNull(i))" ),
495+ s " expected the short-circuit to test all six ordinals; source: \n $formatted" )
496+ }
497+
498+ test(" multi-input tree with a non-leaf child keeps only the post-eval guard (#5218)" ) {
499+ // The counterpart to the test above: the same nullable-NullIntolerant-root shape, but with a
500+ // `Cast` between the root and one leaf. That disqualifies the short-circuit, so exactly one
501+ // setNull site remains -- the post-eval `ev.isNull` guard, which is the actual #4554
502+ // regression and must survive independently of the short-circuit.
503+ val expr = MakeTimestamp (
504+ Cast (BoundReference (0 , StringType , nullable = true ), IntegerType , ansiEnabled = true ),
505+ BoundReference (1 , IntegerType , nullable = true ),
506+ BoundReference (2 , IntegerType , nullable = true ),
507+ Literal (0 ),
508+ Literal (0 ),
509+ Literal (Decimal (0 , 8 , 6 ), DecimalType (8 , 6 )),
510+ timezone = None ,
511+ timeZoneId = Some (" UTC" ),
512+ failOnError = false )
513+ assert(expr.nullable, " MakeTimestamp(failOnError=false) must be nullable for this test" )
514+ val strCol = ArrowColumnSpec (varCharVectorClass, nullable = true )
515+ val intCol = ArrowColumnSpec (
516+ CometBatchKernelCodegen .vectorClassBySimpleName(" IntVector" ),
517+ nullable = true )
518+ val result =
519+ CometBatchKernelCodegen .generateSource(expr, IndexedSeq (strCol, intCol, intCol))
520+ val src = result.body
521+ val formatted = CodeFormatter .format(result.code)
459522 val setNullOccurrences = " output\\ .setNull\\ (i\\ );" .r.findAllIn(src).length
460523 assert(
461524 setNullOccurrences == 1 ,
462- " expected exactly one setNull site (the post-eval ev.isNull guard, with no multi-input " +
463- s " short-circuit); found $setNullOccurrences. Source: \n $formatted" )
525+ " expected exactly one setNull site (the post-eval ev.isNull guard, with no short-circuit); " +
526+ s " found $setNullOccurrences. Source: \n $formatted" )
464527 assert(
465- ! src.contains(" if (this.col0.isNullAt (i))" ),
466- s " expected no input-null short-circuit for a six-input tree ; source: \n $formatted" )
528+ ! src.contains(" if (this.col0.isNull (i))" ),
529+ s " expected no input-null short-circuit when a Cast sits under the root ; source: \n $formatted" )
467530 }
468531
469532 test(" single-input nullable NullIntolerant root emits both short-circuit and post-eval guard" ) {
0 commit comments