Repository navigation
Conversation
|
QA tests have started for PR 1398. This patch merges cleanly. |
|
QA results for PR 1398: |
Contributor
|
Thanks for fixing this - I tested this locally and it worked (though I did have to do a clean build first). |
xiliu82
pushed a commit
to xiliu82/spark
that referenced
this pull request
Sep 4, 2014
Author: Takuya UESHIN <ueshin@happy-camper.st> Closes apache#1398 from ueshin/issues/SPARK-2467 and squashes the following commits: 7f01d58 [Takuya UESHIN] Revert SparkBuild to publish-local to both .m2 and .ivy2.
MaxGekk
added a commit
to MaxGekk/spark
that referenced
this pull request
Nov 8, 2026
…ns (apache#1398) ### What changes were proposed in this pull request? Fuses the "null family" of SQL functions when their input is a FLOAT, DOUBLE, BOOLEAN or TIMESTAMP_NTZ column. The census (`sql/varka/plans/m8/census.tsv`) lists, for every Spark function, the types it accepts and the types Varka fuses today. Eight functions accepted these four types and listed none of them as fused, although the blocks they are made of (`coalesce`, `if`, `CASE WHEN`, the comparisons and `<=>`) already fuse over all four. Spark's optimizer rewrites each of the eight into those blocks: | function | rewritten to | | :--- | :--- | | `nvl(a, b)`, `ifnull(a, b)` | `coalesce(a, b)` | | `nvl2(a, b, c)` | `if(a IS NOT NULL, b, c)` | | `nullif(a, b)` | `if(a = b, NULL, a)` | | `nullifzero(a)` | `if(a = 0, NULL, a)` | | `zeroifnull(a)` | `coalesce(a, 0)` | | `between` | two comparisons and an `AND` | | `equal_null(a, b)` | `a <=> b` | So most pairs needed only a test row. The one piece of code was the `NULL` of `nullif`, `nullifzero` and of a `CASE WHEN` without `ELSE`: a value that is null on every row, which the kernel builds as a broadcast zero whose validity word is all clear (the IR leaf `NullValue`; no consumer reads the data of a lane whose validity is clear, and a kernel holding one always takes the masked body). That leaf was limited to the integral lanes, and the compiler's null leaf declined FLOAT, DOUBLE and TIMESTAMP_NTZ by name. This PR: * lets `NullValue` carry any lane, and lets the compiler's null leaf answer FLOAT, DOUBLE (their own lanes) and TIMESTAMP_NTZ (its microseconds on the long lane). A BOOLEAN choice is a condition whose null already existed. * adds `Lane.pushZero`, the zero of any lane as its own scalar type. The emitter's existing `pushScalar` is integral by contract and refuses a floating lane, so a floating lane pushes `+0.0` of its type, and an integral lane calls `pushScalar(cb, 0)` exactly as the null's broadcast did before. A new test in `VarkaLaneTypeSuite` returns each lane's zero from a generated method of the lane's scalar type, so the verifier checks the type and the boxed equality checks the bits (`+0.0`, not `-0.0`). * adds 37 coverage rows in six families (the function over each type, and the function form of null-safe equality over the types that had only the operator form), and regenerates the coverage table, the byte oracle, the width audit and the census. * adds a fuzz draw of its own for the typed null at the lanes the IR fuzzer's nulling draw never reaches. That draw builds its trees from the int and long grammar, which has no narrow or floating lane. The new draw (`VarkaIrGrammar.drawNullingLaneShape`, seed `NULLING_LANE_FUZZ_SEED`) puts the null at each of its four positions (the missing ELSE, the null branch of `nullif`, an operand of `greatest`/`least`, and twice in one tree) over a TINYINT, SMALLINT, FLOAT or DOUBLE column. It runs through the cast draw's harness and is checked against the reference evaluator under random null patterns, forced masking and options. A FLOAT or DOUBLE column reads the int or bigint column's values as its bits, so the ends of those ranges reach NaN payloads and `-0.0`. The cast harness now picks the long entry point for any 64-bit drive, DOUBLE included. For the cast draw itself this changes nothing, since its columns are all integral and a 64-bit drive there is always LONG. The draw has its own seed and no committed byte oracle reads it, so no committed corpus moves. * adds `nullif(x, y)` and `CASE WHEN x < y THEN x END` to the choice check every `*ConformanceSuite` inherits from `VarkaTypeConformance`. The typed null is then held to Spark's `eval` over every type's special values. * pins the range analysis's answer for a typed null (`VarkaRangeAnalysisSuite`). Only the int lane is ranged: there a null is its vector's zero. At every other lane, integral or floating, the analysis's lane gate answers unknown before the leaf is consulted, so a floating null was already safe; the test now holds both. * rewords declines that still named row 333 for a typed null, now that row 333 has built `NullValue`. The decimal compiler's "a CASE without an ELSE" and "a null decimal literal" declines now say they need a decimal null value and name rows 348 and 359, the rows the census names for the decimal null family. The DATE-to-floating cast decline names row 343 alone. The comments and coverage families this PR added are tagged VARKA-578 instead of VARKA-329. The floating pairs keep Spark's answers because they compile to the comparisons that already implement them: NaN equals NaN and is above every other value, and `-0.0` equals `0.0`. So `nullif(x, x2)` is null for a NaN pair and for the two zeros, and `nullifzero(x)` is null for `-0.0` and not for NaN. After this PR the census lists these counts of fused entries per type (of the entries that accept the type at all; the eight functions alone are in the last column): | type | fused entries before | after | the eight functions, before and after | | :--- | :--- | :--- | :--- | | FLOAT | 26 of 66 | 32 of 66 | 0 to 6 of 8 | | DOUBLE | 32 of 102 | 40 of 102 | 0 to 8 of 8 | | BOOLEAN | 15 of 41 | 21 of 41 | 0 to 6 of 6 | | TIMESTAMP_NTZ | 19 of 54 | 25 of 54 | 0 to 6 of 6 | (`equal_null` gains a row for the integral, TIME and interval types too, so TINYINT and SMALLINT go from 39 to 40 of 75.) The emit cost model needed no change. `VarkaEmitCostTable` holds no price for `NullValue` at any lane, so a group holding a typed null is unpriced at the int lane exactly as at DOUBLE and is grouped by its measured size, and `VarkaEmitBudget` weighs the leaf as one lane call at every lane. **Declined, with the reason recorded:** * `nullifzero(f)` and `zeroifnull(f)` over a FLOAT column. Type coercion compares or coalesces a float with the INT literal `0` in DOUBLE, so the analyzed tree is `IF(CAST(f AS DOUBLE) = CAST(0 AS DOUBLE), CAST(NULL AS FLOAT), f)`, a DOUBLE condition over a FLOAT value (two floating lanes in one kernel), and `coalesce(CAST(f AS DOUBLE), 0.0D)`, whose operand before the last is a conversion and not a bare column. Both are pinned by a test in `VarkaExpressionCompilerSuite` and stated in the census note. The same two over DOUBLE fuse. Closing them needs a rewrite of the widened compare to the column's own lane (`CAST(f AS DOUBLE) = 0.0D` is `f = 0.0F` for every FLOAT), which `VARKA-578.md` 3 records as work for a row of its own. * Every DECIMAL pair, which belongs to the decimal rows. The task is recorded as a new row 578 in `sql/varka/plans/m8/PLAN.md` with a short plan `VARKA-578.md`. ### Why are the changes needed? A query using `nullif`, `nvl`, `between`, `equal_null` and the like over a FLOAT, DOUBLE, BOOLEAN or TIMESTAMP_NTZ column fell back to the row engine, while the same query written as `CASE WHEN`/`coalesce`/`<=>` over those columns ran fused. The census recorded the gap as unfused although nothing but the null leaf was missing. ### Does this PR introduce _any_ user-facing change? No answer changes. More queries over these four column types run in fused kernels when Varka is enabled; the row engine remains the oracle and answers anything a kernel declines. ### How was this patch tested? The 37 new coverage rows each compile through the real compiler (`VarkaCoverageSuite`), and run through `VarkaCoverageDifferentialSuite` against Spark's own result, on fixtures that hold both zeros, NaN with and without its sign bit and the infinities for the floating columns, and null patterns for every column. `VarkaExpressionCompilerSuite` pins the FLOAT declines of `nullifzero` and `zeroifnull` and the decimal choice's decline, reached through `IsNull`. The null-lane fuzz draw, the conformance suites' new choice forms, the range pin and the `pushZero` test are described above. No existing shape's emitted bytes moved: every per-row and per-fuzz-shape hash in `sql/varka/emitted_bytes.json` is the same as on master. Only the `option_arms` digests moved, each one digest over every coverage row at a width, which the 37 new rows enter. `dev/varka_regen_generated.sh --all` on this branch's head regenerates no file. The catalyst suites named above, every `*ConformanceSuite` among them, and `VarkaEmittedBytesSuite`, `VarkaEmitCostSuite`, `VarkaEmitCostAuditSuite` and `VarkaEmitterBudgetSuite` pass, and `VarkaCoverageDifferentialSuite` passes its 58 tests. `dev/varka_census.py --check` and `dev/varka_plan_lint.py` pass. `dev/varka_matrix.sh --defaults --module catalyst --split 4 -j 4` on the head gives 1255 passed and 0 failed, `VarkaIrFuzzSuite` alone 31 passed, and `dev/lint-java` and `dev/varka_errorprone.sh` pass. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 5.5)
MaxGekk
added a commit
to MaxGekk/spark
that referenced
this pull request
Nov 12, 2026
…he#1436) ### What changes were proposed in this pull request? Fuses the "null family" of SQL functions (`nvl`, `ifnull`, `nvl2`, `nullif`, `nullifzero`, `zeroifnull`, `between`, `equal_null`) over DECIMAL columns of precision 18 or less, and `nullifzero` over a FLOAT column. The first part (apache#1398) fused the family over FLOAT, DOUBLE, BOOLEAN and TIMESTAMP_NTZ and left these two gaps. Spark's optimizer rewrites each of these functions into blocks Varka already fuses over decimals within 18 digits: `coalesce`, `IF`, `CASE WHEN`, the comparisons and `<=>`. Coercion gives every operand of one function call the wider decimal type (`widerDecimalType`), so an operand of a smaller scale arrives under a rescaling cast, which is already built. The one missing piece was the `NULL` of `nullif(a, b)` = `IF(a = b, NULL, a)`, of `nullifzero(a)` = `IF(a = 0, NULL, a)` and of a `CASE WHEN` with no `ELSE`. That is a decimal null value. It used to decline with "needs a decimal null value". **The decimal null value.** In `VarkaDecimalCompiler`: * `literal` compiles a null constant to the long lane's existing `NullValue` leaf at the constant's type. `NullValue` is a broadcast zero whose validity word is all clear, so no consumer reads its data, and a kernel holding one always takes the masked body. * `caseWhen` uses that null as the `ELSE` of a `CASE WHEN` that has none. * `valid`, which derives a decimal value's null test from its leaves, answered "not null" for every constant. That was safe only while a null constant declined. It now answers false for the null constant. Without this change, `nullif(d9, d9b) IS NULL` would have been false on the rows where the two are equal. `valid` folds a constant that may be null through the same guarded evaluation `literal` uses, so a constant whose evaluation fails declines instead of throwing. The guard declines on any throw Spark's `NonFatal` admits, a checked one out of Scala code included, as `VarkaConditionCompiler` does for an `IN` element; a fatal one propagates. A constant that cannot be null is answered without evaluating it. **Null tests past 18 digits.** A null test of a decimal wider than 18 digits reads only the value's validity. A probe (`wideProbe`) first checks that every form under the test is one whose null follows from its leaves and conditions, then `valid` builds the test. The probe now also passes the null constant and a `CASE WHEN` with no `ELSE`. Neither needs the pair's two words, and `valid` already answers false for both. So `nullif(c, d) IS NULL`, `nullifzero(c) IS NULL` and `CASE WHEN c < d THEN c END IS NULL` fuse over `DECIMAL(20, 2)`, alone or under a `coalesce`, a choice or a pick. A null constant used as a wide compared value still declines, now with the reason that a pair holds no null value (row 359's part 359b), not that a decimal null value is missing. A negative scale, which only Spark's legacy flag allows, declines in the probe before any of its forms, the null constant and a choice included, as it does everywhere else (the milestone plan's 2.8). The int zero of `nullifzero` is typed `DECIMAL(1, 0)`, the precision the literal needs (`spark.sql.legacy.literal.pickMinimumPrecision`, on by default). So its comparison stays within the column's type at every scale. The zero of `zeroifnull` widens as `DECIMAL(10, 0)`, as does an int beside a decimal in `nvl`, so their wider type passes 18 digits past scale 8. A comparison of either fuses up to scale 8 and then needs row 359's value half. A null test beside an int constant, as `zeroifnull`'s zero, fuses at every scale, past 18 digits through the wide null test described above. Beside an int column past 18 digits it still declines, since the probe passes only a cast from a decimal. **`nullifzero` over FLOAT.** Under ANSI, coercion compares a float with the int `0` in DOUBLE: `IF(CAST(f AS DOUBLE) = 0.0D, NULL, f)`, a DOUBLE condition over FLOAT values. The first part pinned that tree as a decline. A session never hands that tree to Varka. The optimizer's `UnwrapCastInBinaryComparison` moves an up-cast compared with a literal to the literal's side. FLOAT to DOUBLE is exact and keeps Spark's order (NaN highest and equal to itself, `-0.0` equal to `0.0`), so the result is `f = 0.0F` on the float lane, which fuses already. This PR adds the coverage row and corrects the record. The row's tree in `VarkaCoverageSuite` is built by running that rule over the resolved call. The other suites that read `coverage.json` run its SQL form `if(f = 0.0F, NULL, f)`, because their resolver applies no optimizer rule. **Coverage.** 18 new rows: `nullifzero(f)`, and a family "Null family on DECIMAL columns" of 17 rows. They cover each function at one scale and at two (`DECIMAL(9, 2)` with `DECIMAL(9, 4)`, `DECIMAL(16, 9)` with `DECIMAL(9, 4)`), and the null branch under `IS NULL`, under `<=>` and under ordered comparisons. Each row is under a comparison or a null test, because a decimal value is not yet a projection output (row 348's part 348b). The census notes of `nullif`, `nullifzero`, `zeroifnull`, `when` (the `CASE WHEN` entry), `nvl`, `ifnull`, `isnull` and `isnotnull` are updated, and `float` is now among `nullifzero`'s fused types. No census entry gains `dec`: the milestone plan (2.8) counts a decimal entry as fused only once the 128-bit rows 359, 360 and 363 land. **Still declined, each pinned by a test:** * `zeroifnull(f)` over FLOAT under ANSI: `coalesce(CAST(f AS DOUBLE), 0.0D)`. (The legacy coercion keeps FLOAT for a float beside an int, so with ANSI off the call arrives as `coalesce(f, 0.0F)`, the shape the other floating rows fuse.) No rule strips this widening, and the result is a DOUBLE. Fusing it needs the float column's null test crossed into a DOUBLE choice. That is a crossing between two floating widths, which row 343's design leaves out by decision ("one kind, so not crossed"). The decline reason, the census note and `VARKA-578.md` 4 say so, and section 3 there keeps the conversion arm that would close it as a finding. * A decimal choice or null past 18 digits as a compared value: `nvl(c, d) > 0.00`, `zeroifnull(c) = d`, `nullif(c, d) <=> c` and `nullif(c, d) IN (c, d)` over `DECIMAL(20, 2)`, and a null constant compared at a wide type. Row 359's 128-bit pair (apache#1413, merged) compares and null-tests columns, widening casts and constants, but holds no choice and no null as a value. That is its value half, part 359b. Each declines with a reason naming row 359. A rescale into a wide common type, such as `nullif` of `DECIMAL(18, 2)` with `DECIMAL(16, 9)` at `DECIMAL(25, 9)`, also needs row 360's wide rescale and names rows 359 and 360. Row 578 of `sql/varka/plans/m8/PLAN.md` stays started until this merges and the decimals past 18 digits follow row 359, with `VARKA-578.md` 4 recording what was built and what stays declined. ### Why are the changes needed? A filter using `nullif`, `nvl`, `between`, `equal_null` and the rest over a DECIMAL column within 18 digits fell back to the row engine, although the same filter written with `coalesce`, `IF` or `<=>` ran fused. Only the decimal null was missing. `nullifzero` over FLOAT was recorded as declining, but it already ran fused in a session, so the record was wrong. ### Does this PR introduce _any_ user-facing change? No answer changes. More filters over DECIMAL columns within 18 digits run in fused kernels when Varka is enabled. The row engine remains the oracle and answers anything a kernel declines. ### How was this patch tested? * `VarkaCoverageSuite`: every new row compiles through the real compiler, and the generated files are rendered from it. * `VarkaDecimalValueSuite`, two new tests. The first holds 14 forms (`nullif` both ways, `nullifzero`, `CASE` with no `ELSE` and with two branches, `IF` with a `NULL` branch, `zeroifnull`, `nvl`, `nvl2`, `between`, `equal_null`, `greatest` with a `NULL`) over a `DECIMAL(9, 2)` and a `DECIMAL(9, 4)` column to Spark's interpreted `eval`. The rows hold values equal after the rescale, one unit apart, zeros, both bounds of the precision and every null pattern. The second compiles each form past 18 digits that fuses, `zeroifnull(e) IS NULL` and `nvl(e, 0) IS NULL` at `DECIMAL(16, 9)` among them, and pins the reason of each that declines, naming rows 359 or 360. It also builds a wide `CASE` whose branch is a null of negative scale under Spark's legacy flag, which declines as every negative scale does; without the probe's check it fused. * `VarkaDecimalPairSuite`, one new test: 13 null tests of the family (`nullif` and `nullifzero` both ways, `CASE` with no `ELSE` and with two branches, `IF` and `nvl2` with a `NULL` branch, and the null under `coalesce`, `greatest` and `least`) over `DECIMAL(20, 2)` beside `DECIMAL(20, 2)` and `DECIMAL(38, 2)`, held to Spark's interpreted `eval` three-valued on the 128-bit word edges (values equal in the low word and different in the high word, and the reverse) and every null pattern, at two, four and eight long lanes and the preferred species. * `VarkaExpressionCompilerSuite`: the unwrap of `nullifzero(f)` by `UnwrapCastInBinaryComparison` and the compile of the result, `zeroifnull(f)`'s decline, and a decimal `CASE` with no `ELSE` past 18 digits, which declines as a compared value with a reason naming row 359 and fuses under `IS NULL` to the `CASE`'s validity, its missing `ELSE` never valid. A planted break that made the missing `ELSE` valid failed that assertion alone: the test's earlier assertions, `IS NULL` fusing among them, still passed. * `dev/varka_regen_generated.sh --all` regenerates the coverage table, the docs, the width audit, the byte oracle and the census. In `sql/varka/emitted_bytes.json` every existing shape's hash is unchanged. The new rows add their own hashes, and only the `option_arms` digests move, since each is one digest over every coverage row at a width. * catalyst: `VarkaCoverageSuite`, `VarkaExpressionCompilerSuite` (172 passed), `VarkaDecimalValueSuite` (12 passed), `VarkaDecimalPairSuite` and the other `VarkaDecimal*Suite` suites (In, Arithmetic, Scale, Word, Load, Store), `VarkaFamilySwitchSuite`, and the suites that read `coverage.json` or price kernels: `VarkaEmitCostSuite`, `VarkaEmitCostAuditSuite`, `VarkaEmitterBudgetSuite`, `VarkaCoverageCompositionFuzzSuite`, `VarkaGroupingBoundSuite`, `VarkaDivisorProofSuite` and `VarkaFamilySwitchSuite` (67 passed). * sql (`-Phive`): `VarkaCoverageDifferentialSuite` (62 passed), `VarkaDifferentialHarnessSuite` (69 passed) and `VarkaCoverageHarnessSuite` (2262 passed). The last runs every row alone, in the untaken arm of a `CASE`, after a column in a `COALESCE` and behind a false `AND` or a true `OR`. Also `dev/varka_scoped_suites.sh sql --shard 1/3`, the CI share that holds `VarkaCoverageHarnessSuite`, run under the memory sanitizer as CI runs it (2582 passed, 0 failed), and `--shard 0/3` (267 passed, 0 failed). * `dev/varka_census.py --check`, `dev/varka_plan_lint.py`, `dev/varka_precommit.sh` and `dev/varka_errorprone.sh` pass. * One `/code-review high` pass (11 November 2026): 10 found, 7 fixed, 3 left as trade-offs (the null constant fusing outside the family without answer tests, a constant folded more than once at plan time, the `int` verification count; see "Review" below). No IR node is added and none changes, so the suites that list node types are not affected. Test time grows by a few seconds. `VarkaCoverageHarnessSuite` ran its 2262 tests in 41 s, and the 18 new rows account for roughly 70 of them; the two new `VarkaDecimalValueSuite` tests run in well under a second. **Review.** A second review round found that the wide (128-bit) null test still declined every null constant and every `CASE` with no `ELSE` with a stale reason, though `valid` already handled both; that `valid` evaluated constants unguarded; and that the wide decline test's name, assertion and comment disagreed. All three are fixed: the wide null tests now fuse (see "Null tests past 18 digits"), `valid` folds through the guarded evaluation, and the test pins each form's outcome. The first review of the diff found no correctness bugs. It found stale or wrong record text, all fixed: the `CASE WHEN` census note still excluded decimals, the row's status overclaimed "every scale" where a `zeroifnull` comparison stops at scale 8, the reason given for `nullifzero` over `DECIMAL(p, p)` was wrong, and a test comment and three section citations were wrong. Trade-offs kept: * The `nullifzero(f)` row's differential runs the unwrapped SQL a session hands Varka, `if(f = 0.0F, NULL, f)`, rather than `nullifzero(f)` itself, because the catalyst suites that read `coverage.json` apply no optimizer rule. The row's note says so. * The wide `CASE`'s compared-value decline asserts that the reason names row 359, not the reason's exact text, so a rewording of that reason in row 359's later parts does not break it. * `VarkaSqlResolve.replaced` is a small Scala helper beside the resolver it serves, rather than a Java one, since it is a `transformDown` over Catalyst. * `literal` compiles a null constant to `NullValue` wherever a decimal value within 18 digits is compiled, not only in the family's shapes. So forms outside the family fuse without a test of their own answers: `d9 + NULL > d9b` and `nullif(a18, a18b) < c20` (over `DECIMAL(18, 2)` beside `DECIMAL(20, 2)`) were checked to fuse. `d9 <=> NULL` still declines at the null-safe equality's own null-literal check, and an `IN` whose value holds the null was not checked. These forms rest on the two facts the family's tests hold to Spark: the null constant's validity is false, and no consumer reads `NullValue`'s data. * A nullable constant under a null test is folded two or three times: by the probe (`value`, or past 18 digits `wideProbe` and the pair it reads) and again by `valid`. Folding happens once per shape at plan time, never per batch, so the repeated evaluation is not cached. * The generated verification counts in `docs/sql-varka.md` show two more `int` rows (156 to 158), though the new rows are DECIMAL rows. The review traced them to the two `zeroifnull` rows, whose trees hold the int zero of `coalesce(a, 0)`, which the table counts under `int` too. The table's attribution rule is shared by every row and is left as it is. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (claude-opus-5-5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.