Skip to content

Preserve exact wide negative DECIMAL values - #658

Merged
vuanhphung merged 4 commits into
mainfrom
vuanhphung/exact-negative-decimals
Dec 3, 2026
Merged

vuanhphung merged 4 commits into
mainfrom
vuanhphung/exact-negative-decimals

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

With preserveBigNumericPrecision: true, negative DECIMAL values whose unscaled value exceeds 2^53 are corrupted on both backends, both as top-level columns and nested. -123456789012345678.91 comes back as "-123456789012345670.00", and wider values come back as broken exponent strings. apache-arrow's bigNumToBigInt returns a decimal string for Decimal128 even though it is typed bigint (still true in 21.2.0), and bigNumDecimalToString negated that string as a lossy JS number. This PR converts the input to a bigint once, in bigNumDecimalToString. The default number mode is unchanged.

The existing real-Arrow unit fixtures (scalars, structs, lists, map values, NULL, both row modes) and the shared row-materialization e2e fixture now include wide negatives, and they fail on main and pass here on both backends. Precision-mode exactness is asserted for scales 0/2/18/38. Negative scales aren't covered in that mode: they predate this PR, and Databricks SQL rejects negative-scale DECIMALs. driver-test's skipped DecimalTests DECIMAL(38, 37) — max scale already tracks this bug. It fails on main and passes on this branch on both backends, and can be re-enabled after merge. A 100k-row mixed-sign DECIMAL(38,2) fetch shows no slowdown: median Thrift 425 → 405 ms, kernel 359 → 354 ms, on a shared warehouse.

Cross-driver state

Driver State Notes
JDBC Supported Top-level BigDecimal is exact (DatabricksResultSet.java:748 @8898712); nested values are exact with EnableComplexDatatypeSupport=1 (default off)
Python Supported Arrow decimals become decimal.Decimal (utils.py:1043 @543777a)
Go Supported Exact negation in internal/decimalfmt/decimalfmt.go:52 @eefbf7b
Node.js Partial → Supported (this PR) preserveBigNumericPrecision (default off) is now exact for wide negatives
Kernel Not applicable Preserves Decimal128 in IPC (napi/src/result.rs:88 @3b691d2); formatting happens in the JS driver

#657 wraps its DECIMAL map keys in BigInt(...) for the same reason. Once both merge, that wrapper is redundant but harmless. Security Scan fails on the same cross-spawn/ws advisories as main.

Adversarial review: converged in round 1 with no high findings.

This pull request and its description were written by Isaac.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
@vuanhphung
vuanhphung marked this pull request as ready for review December 2, 2026 21:58

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@aakash-saravanan-db aakash-saravanan-db left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shared BigInt normalization is the right fix for Arrow's runtime string/bigint mismatch: it covers scalar and recursive conversion on both backends without changing default number mode.

Approving the production change. Both inline test comments are blocking and must be addressed before merge.


This review was generated with GitHub MCP.

return null;
}
const integer = BigInt(value);
if (scale <= 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking before merge: this new oracle treats every scale <= 0 as an unscaled integer. With unscaled 1 and scale -2, the value is 100, but the new precision-mode expectation is "1"; the same test correctly expects 100 in default mode.

The production limitation predates this PR, but we should avoid adding an assertion that certifies the wrong value. Please keep negative-scale coverage for default mode and restrict the new precision-exactness assertions to nonnegative scales. Negative-scale support can be tracked separately; this PR does not need a production change for it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in aed9cb3: precision-mode exactness now covers only non-negative scales, and every default-mode negative-scale case stays. Negative-scale support in precision mode is left for a separate change. Databricks SQL rejects CAST(... AS DECIMAL(10,-2)), and ROUND(..., -2) returns scale 0, so it's only reachable from hand-built Arrow data.

});
}

for (const rowMode of ['object', 'array'] as const) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking before merge: please extend the existing fixtures to cover this regression and consolidate the duplicated setup.

The existing matches legacy precise DECIMAL in scalars and containers test already covers scalars, structs, lists, map values, NULL and both row modes. Replacing its small negative (-12500) with a wide negative and updating the expected literals catches this bug; that extension fails against the old formatter and passes here. The expanded scale/sign matrix already covers the top-level boundaries. This lets us remove this separate nested block while retaining the regression coverage and adding wide-negative map-value coverage.

Likewise, please add the wide-negative scalar and nested case to tests/e2e/utils/rowMaterialization.ts and reuse its backend/mode cases instead of the separate negative-decimals.test.ts connection/session scaffolding. Preserve the mode-specific default-number rounding expectations. Assert nested exactness for kernel and Thrift native-Arrow mode; Thrift's internal non-native mode parses complex values as JSON, so it should not acquire an assertion blessing lossy output as an exact result.

Please retain the direct real-Arrow formatter test, which usefully exercises the runtime type mismatch.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in aed9cb3: the wide negative now extends matches legacy precise DECIMAL in scalars and containers (and the dictionary case). The separate nested block and negative-decimals.test.ts are gone. rowMaterialization.ts now covers a wide-negative scalar plus nested struct, list and map values, with exactness asserted only for kernel and native-Arrow Thrift. Default-number rounding is unchanged in each mode, and the direct real-Arrow formatter test stays.

Exclude negative scales from precision-exact assertions and extend shared
unit and live fixtures instead of duplicating conversion and session setup.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Thanks for your contribution! To satisfy the DCO policy in our contributing guide every commit message must include a sign-off message. One or more of your commits is missing this message. You can reword previous commit messages with an interactive rebase (git rebase -i main).

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@vuanhphung
vuanhphung enabled auto-merge December 3, 2026 16:26
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@vuanhphung
vuanhphung added this pull request to the merge queue Dec 3, 2026
Merged via the queue into main with commit 2c4d50a Dec 3, 2026
22 of 23 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — da94610f Deployed Dec 3, 2026 by vuanhphung via e2e-test (22.23.4, 22_23_4) #2296
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants