Repository navigation
[SPARK-2540] [SQL] Add HiveDecimal & HiveVarchar support in unwrapping data - #1436
chenghao-intel wants to merge 3 commits into
Conversation
|
QA tests have started for PR 1436. This patch merges cleanly. |
There was a problem hiding this comment.
might be a better way to write this:
case hvoi: HiveVarcharObjectInspector =>
if (data == null) null else hvoi.getPrimitiveJavaObject(data).getValue
case hdoi: HiveDecimalObjectInspector =>
if (data == null) null else BigDecimal(hdoi.getPrimitiveJavaObject(data).bigDecimalValue())There was a problem hiding this comment.
Yeah, cool. :)
|
QA tests have started for PR 1436. This patch merges cleanly. |
|
QA results for PR 1436: |
|
QA results for PR 1436: |
|
Unit test actually failed. |
|
Hey @chenghao-intel -- can you create a JIRA ticket for this? |
|
Jira Ticket are created: https://issues.apache.org/jira/browse/SPARK-2540 |
|
QA tests have started for PR 1436. This patch merges cleanly. |
|
QA results for PR 1436: |
|
@rxin I think this is ready to be merged. |
|
Thanks! I've merged this into master and 1.0. |
…g data Author: Cheng Hao <hao.cheng@intel.com> Closes #1436 from chenghao-intel/unwrapdata and squashes the following commits: 34cc21a [Cheng Hao] update the table scan accodringly since the unwrapData function changed afc39da [Cheng Hao] Polish the code 39d6475 [Cheng Hao] Add HiveDecimal & HiveVarchar support in unwrap data (cherry picked from commit 7f17208) Signed-off-by: Michael Armbrust <michael@databricks.com>
…g data Author: Cheng Hao <hao.cheng@intel.com> Closes apache#1436 from chenghao-intel/unwrapdata and squashes the following commits: 34cc21a [Cheng Hao] update the table scan accodringly since the unwrapData function changed afc39da [Cheng Hao] Polish the code 39d6475 [Cheng Hao] Add HiveDecimal & HiveVarchar support in unwrap data
…rts (apache#1436) Currently for `ALTER DATABASE SET LOCATION` command, Spark will throw exception when Hive version (e.g., specified via `spark.sql.hive.metastore.version`) is not 3.0/3.1. This PR removes the check so that the command works as long as the Hive version used by the Hive metastore (which could be different from the version used by Spark) supports the alter database location feature added via [HIVE-8472](https://issues.apache.org/jira/browse/HIVE-8472). If it does not support it, the same exception will still be thrown from Spark side. For the command `ALTER DATABASE SET LOCATION` command, Spark currently throws exception like the following: ``` AnalysisException: Hive 2.3.9 does not support altering database location ``` This is not accurate since it only considers the client version, while the feature support is on the Hive metastore server side. Therefore, the command should succeed if Spark is using Hive 2.3 while the remote Hive megastore is using Hive 3.1. On the other hand, the command will not succeed if Spark is using Hive 3.1 (thus no exception) but the remote Hive metastore is using 2.3. Yes, previously Spark users using Hive client with version other than 3.0/3.1 won't be able to run `ALTER DATABASE SET LOCATION` command against Hive metastore 3.x. After this PR it should work. Modified the existing test case. Closes apache#36750 from sunchao/SPARK-29260. Lead-authored-by: Chao Sun <sunchao@apple.com> Co-authored-by: Chao Sun <sunchao@apache.org> Signed-off-by: Yuming Wang <yumwang@ebay.com> Co-authored-by: Chao Sun <sunchao@apache.org>
### What changes were proposed in this pull request? Plan documents only: six compiler improvements, accepted by the owner on 11 November 2026, are added to the milestone 9 scope catalogue (`sql/varka/plans/m9/SCOPE.md`). Each is a proposal to be measured, not a committed design. Three of them were already partly in the catalogue, so they extend an existing item with a dated paragraph rather than adding a duplicate. The other three are new items 134 to 136. 1. **Check the host's capabilities when emitting, not only when benchmarking: new item 134.** `operator_census.json` records, per host and vector width, whether C2 vectorizes each operator, allocates, or falls back to the Vector API's Java code. No main source reads it. Row 597's decimal readers fuse at 128 bits on x86, where the EPYC 7763's census cells put their slot load in the fallback, and a benchmark found the loss. The item makes each lowering name the census cells it depends on. Planning then treats a fallback cell on the target host as a predicted cost, which item 131's admission step weighs. It is not a refusal by itself, because apache#1262 kept two-lane projections that win although their compare cell reads fallback. It is new rather than part of row 551 or items 127 and 131, because those read HotSpot's feature list or decide *where* a choice is made. None of them use the measured census as the source, and the item sets out how it relates to each. 2. **Compute node properties instead of keeping hand-written lists: extends item 47.** Item 47, "One place per node", was deferred until a second concrete case arrived. The new paragraph records that case. Facts that consumers need sit in `instanceof` lists that miss nodes: the apache#1462 review found `mayHoldOtherNaN` skipping its blend without checking the rounding mode, and row 597 records that apache#1478's `readsDecimal` misses `bigintPair`. The paragraph proposes finishing row 489's descriptor, so the analysis derives these facts bottom-up. The descriptor has to be rebuilt over master, because its branch predates apache#1329. Item 51's mapping type is noted as one more field of the same record. 3. **One admission decision instead of a decline rule per row: extends item 131.** Item 131 already proposes letting the cost model make the two-lane decline. The extension widens it to rows 503, 596, 597 and 603 as one admission step per shape, host and width. That step reads measured costs and item 134's table and refuses with a named reason. It also records that `VarkaEmitCost.kindOf` counts `rint` with and without apache#1462's blend as one feature, which a refit would average. 4. **A hash-consed IR: new item 135.** No item proposes interning. Item 11 has only a one-line design note, and item 85 is about jegg and storage layout. The item starts from row 559 (apache#1329), which cached node hashes and chose that over interning, and lists the walks that still run once per path: the lane lifts, and `equals` over disjoint copies. 5. **Tests that prove they reach the code they claim to test: new item 136.** No item covers this. Two cases from the 11 November reviews show the problem: a test on apache#1436 that passes on an earlier output-type decline, and apache#1425's body claiming checked answers for a test that only asserts no fusion. The item proposes two fixes. Each decline carries the name of the rule that produced it, as `VarkaKernelPlan.Refusal` already carries a kind. The harness records which nodes and lowerings a test reached. `VarkaCompileObserver` is the starting point, but it needs a second listener slot and a lowering report. Item 82's mutation runs are cited as the related check. 6. **Generated files that stop conflicting on every merge: extends item 121.** Item 121 was moved to milestone 8 as row 546, whose merge driver (apache#1293) merged. The new paragraph records what is still left. Every merge still regenerates `option_arms` and the docs blocks and needs another Build, and apache#1448 found four option-arm digests stale on master with no text conflict. The paragraph proposes splitting the files and digests per shape or family, or committing an input digest that CI checks. It notes that row 546 had ruled out CI committing files, because the fork has no token for it. A note further out, folded into **item 1** (decimal lanes) as the owner asked: a general multi-word integer lane for decimals above 18 digits, instead of the per-operation work of rows 359 to 364. Corrections to the evidence as first stated, made after checking it against the tree: - Item 135 does not claim the coverage harness's 45% planning share. `m8/results/VARKA-510-planning-local.txt` puts 45.1% in Spark's analyzer and optimizer, and only 11.7% in Varka's compile and emit, which is the share an interned IR can cut. - The "planning doubles per level" cost of row 561 was fixed for hashing by row 559 (apache#1329), so item 135 cites only the walks that remain. - Row 541's CSE mismatch is between two of Spark's own code generators on the fallback path, so the item says it is outside the IR. - The "60 to 75 minute regeneration" is left out. No committed file records it, and the laptop's regeneration logs of 10 and 11 November show 1 to 23 minutes of sbt time. - The note's rows are 359 to 364, which is the lane-pair range in the milestone 8 header. Row 365 is a read-path fix and row 366 a cast to floating point. One `/code-review high` pass: found 10 / fixed 9 / left 1 (the title says "compiler items" though item 121 is harness work, kept because the owner's brief names the six that way). Fixed: item 134 now treats a census cell that reads F as a predicted cost for item 131's admission step to weigh, not as a refusal on its own, because apache#1262 kept the two-lane projections whose cells read F; items 131 and 134 are one piece of work, with the table built first; item 135 says interning still needs one shared identity memo for the walks; item 47 limits "may raise" to fused subtrees, notes that the 489 branch predates apache#1329, and points its done-when at what apache#1462 and apache#1478 bring to master; item 121's note marks that it stays in milestone 9 and depends on item 129's merge-result build for the stale-digest case; item 136 states `VarkaCompileObserver`'s single listener and that it reports no lowerings; item 131 takes a node's key from row 489's cost kind, and keeps a test for a second lowering of an existing node. A follow-up self-check also tightened four citations: the census's `long.LT` cell, row 559's "every node with a child", row 541's two gates, and the regeneration command's name. ### Why are the changes needed? Several recent problems follow the same patterns. A form known to fall back on a host was still emitted, and only a later EPYC run found it. A hand-written list missed a node or a mode. A decline rule was added for one row at a time. A test passed for a reason other than the one it names. Generated files cost every merge a regeneration and a Build. Recording these as scope items, with their evidence, keeps the fixes general instead of patched one row at a time. Extending the existing items, rather than adding near-duplicates, keeps the catalogue with one entry per idea. ### Does this PR introduce _any_ user-facing change? No. Plan documents only. ### How was this patch tested? `python3 dev/varka_plan_lint.py --check --offline` reports 0 findings. `python3 dev/varka_push_check.py` passes: pre-commit, plan lint, quote check, census check, and 138 counted lines against the cap of 1000. Every row, pull request, file and identifier cited in the new text was checked against `sql/varka/plans/m8/PLAN.md`, the tree at `origin/master`, or the open branches it names. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 5.5)
…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)
No description provided.