From 1859fa30b3d03319ff6f055da0c3cb68ae03ed56 Mon Sep 17 00:00:00 2001 From: t Date: Fri, 20 Nov 2026 07:56:18 +0100 Subject: [PATCH 01/18] [VARKA-348] Compile every decimal output root through the decimal value compiler VarkaDecimalCompiler.output checks the output's type as before, then compiles any value() and wraps it in WidenDecimal. The call is bracketed by enterDecimal/exitDecimal so the cached choice conditions are dropped before an entry's tables can roll back. As a result, every decimal value within 18 digits is a projection output: rescaling casts, negation, abs, the choices, the picks, and 349's sums, differences and products. The two decimal-valued TIME functions become decimal operands through a value() arm. The census marks dec^ on fifteen value entries; least and try_subtract wait for coverage.json to hold their rows. The pair's conformance suite lists them in declinesInPart. VarkaExpressionCompilerSuite's test of the TIME producers below a root now checks that they fuse there. Generated-by: Claude Code (Claude Opus 5.5) --- .../codegen/VarkaDecimalCompiler.java | 38 ++++++++++--------- .../codegen/VarkaTimeCompiler.java | 37 +++++++++--------- .../varka/VarkaDecimalConformanceSuite.java | 11 +++++- .../VarkaDecimalLongConformanceSuite.java | 7 +++- .../VarkaExpressionCompilerSuite.scala | 18 ++++++--- sql/varka/plans/m8/census.tsv | 34 ++++++++--------- sql/varka/plans/m8/census_curated.txt | 34 ++++++++--------- 7 files changed, 99 insertions(+), 80 deletions(-) diff --git a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaDecimalCompiler.java b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaDecimalCompiler.java index ae8065ebb4832..12269c05bcab8 100644 --- a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaDecimalCompiler.java +++ b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaDecimalCompiler.java @@ -121,8 +121,8 @@ * lowers the scale or the integral digits, or one from an integral type (row 350); a negative * scale (the milestone plan's 2.8); and every operator no arm here claims yet. * - *

A decimal-valued output root is compiled by {@link #output}, which the projection's root - * binding calls, and admitted by {@link #outputRoot}. + *

A decimal-valued output root is any {@link #value}, compiled by {@link #output}, which the + * projection's root binding calls, and admitted by {@link #outputRoot}. */ final class VarkaDecimalCompiler { @@ -208,6 +208,12 @@ static Optional value(Expression e, VarkaSlotTable inputs, VarkaSlotTabl return switch (e) { case BoundReference br -> column(br, type, inputs, sink); case Cast c -> cast(c, type, inputs, literals, sink); + // The two decimal-valued TIME functions (VARKA-157 2.3), under the TIME family's switch, + // which this path never reaches through compileNode: each is within its precision for + // every time of day. + case StaticInvoke si when VarkaTimeCompiler.isDecimalProducer(si) -> + VarkaNodeCompiler.inFamily(VarkaFamily.TIME, + () -> VarkaTimeCompiler.decimalProducer(si, inputs, literals, sink), si, sink); // Neither can fail or leave the precision, |-u| = |u| (VARKA-348 0.5), so both wrap: the // one overflowing long, Long.MIN_VALUE, is outside every decimal's bound. case UnaryMinus m -> value(m.child(), inputs, literals, sink) @@ -646,13 +652,11 @@ private static Optional> compiledOperands(Expression whole, } /** - * A decimal-valued output root, or empty with the reason noted (`VARKA-157.md` 2.1): the - * output's type is asked first, so a precision above 18 or a negative scale declines naming its - * row at plan time, whatever the expression; then the value is compiled and wrapped by - * {@link #outputRoot}. The producers today are the two decimal-valued {@code TIME} functions - * ({@code VarkaTimeCompiler.decimalProducer}); every other decimal value as an output declines - * naming row 348, whose values ({@link #value}) reach this method's {@link #widened} when its - * part 348b admits them. + * A decimal-valued output root, or empty with the reason noted (`VARKA-157.md` 2.1, + * `VARKA-348.md` 2.4): the output's type is asked first, so a precision above 18 or a negative + * scale declines naming its row at plan time, whatever the expression; then the value is + * compiled by {@link #value}, as under a comparison, and wrapped by {@link #widened}. Every + * form {@link #value} builds is within its type's precision, so the store needs no check. */ static Optional output(Expression e, VarkaSlotTable inputs, VarkaSlotTable literals, DeclineSink sink) { @@ -661,16 +665,14 @@ static Optional output(Expression e, VarkaSlotTable inputs, if (refused.isPresent()) { return sink.decline(refused.get(), e); } - if (e instanceof StaticInvoke si && VarkaTimeCompiler.isDecimalProducer(si)) { - // The producers are the TIME family's nodes, and this path never passes through - // compileNode, so it asks the family's switch and notes the family itself. - return VarkaNodeCompiler.inFamily(VarkaFamily.TIME, () -> - VarkaTimeCompiler.decimalProducer(si, inputs, literals, sink) - .flatMap(v -> widened(e, v, sink)), e, sink); + // Bracketed as a comparison's operands are, so the choice conditions it keeps are dropped + // before the entry's tables can be rolled back to its marks. + sink.enterDecimal(); + try { + return value(e, inputs, literals, sink).flatMap(v -> widened(e, v, sink)); + } finally { + sink.exitDecimal(); } - return sink.decline("a " + type.simpleString() + " value as a projection's output: the " - + "decimal store writes it, and the values it admits are the TIME functions " - + "time_to_seconds and extract(SECOND ...); the others are row 348's", e); } /** diff --git a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaTimeCompiler.java b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaTimeCompiler.java index f58852d7e4651..bb12f09c991be 100644 --- a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaTimeCompiler.java +++ b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/VarkaTimeCompiler.java @@ -231,13 +231,12 @@ static VarkaFamilyArm arm( // constructors refuse a tree that mixes it with the int lane anyway. // A decimal column is never a long column, whatever lane the registry gives it // (VARKA-347): its sixteen-byte slots are read by its own leaf, with the check Spark's - // reader makes. Under a comparison or a null test, a decimal value - the column, a - // rescale, a negation, abs, a choice or a pick over it (VARKA-348), a sum, a difference or - // a product (VARKA-349) - is built by VarkaDecimalCompiler, so a decimal column reaching - // here declines, ahead of the long column's arm below, which would read the slots as - // eight-byte longs: with the registry's reason where it has one (a precision or scale off - // the lane), and otherwise as a value outside a - // condition - an output, row 348's (348b), or the operand of a cast to an integral + // reader makes. Under a comparison or a null test and as a decimal output, a decimal + // value - the column, a rescale, a negation, abs, a choice or a pick over it (VARKA-348), + // a sum, a difference or a product (VARKA-349) - is built by VarkaDecimalCompiler, so a + // decimal column reaching here declines, ahead of the long column's arm below, which would + // read the slots as eight-byte longs: with the registry's reason where it has one (a + // precision or scale off the lane), and otherwise as the operand of a cast to an integral // or a floating type, rows 350 and 366. Above 18 digits the value is two words // (VARKA-359), whose operators beyond the comparisons and null tests are later rows'. case BoundReference br when br.dataType() instanceof DecimalType d -> @@ -247,8 +246,8 @@ static VarkaFamilyArm arm( ? "its comparisons and null tests fuse; its negation, abs and choices are row " + "359's, its +, - and * rows 360 to 363, a decimal output its two-word " + "store, row 359's" - : "its comparisons, null tests and the values they read fuse, +, - and * " - + "among them; a decimal output is row 348's") + : "its comparisons, null tests, the values they read and a decimal output " + + "of them fuse, +, - and * among them") + ", its casts to other types rows 350 and 366"), br); case BoundReference br when VarkaTypes.lane(br.dataType()).orElse(null) == LaneType.LONG -> @@ -602,14 +601,14 @@ private static String timeNotLoweredYet(String label) { } /** - * The reason the two decimal-valued {@code TIME} expressions decline below a root: at an output - * root they fuse through the decimal store ({@link #decimalProducer}), and as the operand of - * another expression their value is a decimal operand, which row 348 consumes - * (`VARKA-157.md` 2.1). + * The reason the two decimal-valued {@code TIME} expressions decline where a non-decimal + * operator reads them: as an output root and as a decimal operand they are + * {@code VarkaDecimalCompiler.value}'s ({@link #decimalProducer}), and the casts of a decimal + * to another type are rows 350 and 366. */ private static String decimalOperandNotYet(String label) { - return label + " returns a decimal, which fuses as a projection's output; as the operand of " - + "another expression its value is row 348's"; + return label + " returns a decimal, which fuses as a projection's output and as a decimal " + + "operand; its casts to other types are rows 350 and 366"; } /** The two {@code TIME} functions whose value is a decimal (`VARKA-157.md` 2.3). */ @@ -623,10 +622,10 @@ static boolean isDecimalProducer(StaticInvoke si) { } /** - * The unscaled value of a decimal-valued {@code TIME} function at an output root, or empty with - * the reason noted (`VARKA-157.md` 2.3). Both are non-negative and within their precision for - * every time of day, so neither needs a guard, and every divisor is proven in - * {@code long_divide.smt2}. + * The unscaled value of a decimal-valued {@code TIME} function, at an output root or as a + * decimal operand, or empty with the reason noted (`VARKA-157.md` 2.3). Both are non-negative + * and within their precision for every time of day, so neither needs a guard, and every divisor + * is proven in {@code long_divide.smt2}. * *