Repository navigation
Conversation
ErikBPF
force-pushed
the
7659-skip-checkoverflow-decimal-arithmetic
branch
3 times, most recently
from
November 24, 2026 09:52
31fbddf to
7cf2754
Compare
DecimalPrecision.promote wrapped every decimal `+`, `-` and `*` in CheckOverflow so the native side had an explicit target type. On the planner's narrow Decimal128 path no value that respects its declared input type can overflow the declared result precision, so the wrapper is a per-batch scan that can never fire; TPC-H Q1 spent about 5% of native CPU in it. Skip the wrapper when the planner stays narrow: `max(s1,s2) + max(p1-s1,p2-s2) < 38` for `+` and `-`, `p1 + p2 < 38` for `*`. The checks mirror `decimal128_add_sub_width`, including its i16 arithmetic for negative scales. Divide and Remainder keep their wrappers, and wide-path arithmetic is unchanged. One difference from Spark remains: over-precision input no longer trips the precision check, so a value that violates its own declared precision passes through where Spark returns NULL or raises NUMERIC_VALUE_OUT_OF_RANGE. Valid input cannot exceed the narrow result precision. The compatibility guide states the difference and that an i128 overflow still fails. Verified on the Spark 4.1 profile: 146 tests across the decimal suites, 129/129 TPC-DS plan stability after the golden update (each of the 29 affected queries loses one `Accelerated expressions: N native` count and nothing else), QueryPlanSerdeSuite for the subexpression classification, and the wider view/codegen/expression run at 324 passed with 4 ignored.
ErikBPF
force-pushed
the
7659-skip-checkoverflow-decimal-arithmetic
branch
from
November 24, 2026 10:36
7cf2754 to
96e6038
Compare
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.
Which issue does this PR close?
Part of #7659. This is the Scala-side fix, so the
wrapper is never serialized for the affected expressions.
Rationale for this change
DecimalPrecision.promotewrapped every decimalAdd,Subtract, andMultiplyinCheckOverflow. When the native planner takes its narrowDecimal128path, no value that respectsits declared input type can overflow the declared result precision, so the wrapper is a per-batch
scan that can never fire.
The native planner decides narrow vs. wide from the operand types:
+/-narrow whenmax(s1, s2) + max(p1 - s1, p2 - s2) < 38*narrow whenp1 + p2 < 38Spark's result precision on that path is
max(p1 - s1, p2 - s2) + max(s1, s2) + 1for+/-andp1 + p2 + 1for*. Both are at most 38, soDecimalType.adjustPrecisionScaleandDecimalType.boundedleave them unchanged. DataFusion's decimal kernel computes the same resulttype from the operand types, so the native child type equals
expr.dataTypeand the wrapper addsno type information. Each operand's rescaled magnitude is below
10^width, so a sum or differenceis below the result precision bound; the product is below
10^(p1 + p2).This removes the wrapper from TPC-H Q1's
1 - l_discount,1 + l_tax, andl_extendedprice * (1 - l_discount). The chained* (1 + l_tax)remains wide; the native planneralready drops that wrapper because
WideDecimalBinaryExprchecks its own output.DivideandRemainderkeep their wrappers: native decimal division usesi128::MAXas anoverflow sentinel, which the wrapper converts to NULL or an ANSI error.
Performance
The gain is measured in #7658:
CheckOverflowwas about 5% of native CPU samples in TPC-H Q1 at SF10, and every
CheckOverflowin Q1 is on thispath. There is no slower shape: the array of expressions that still take the wide path is
unchanged, and the narrow path only loses a per-batch scan. This PR does not add its own
micro-benchmark; it removes a wrapper whose cost was already profiled in that issue.
Compatibility note for malformed data
If an input value violates its own declared precision (for example a Parquet file whose unscaled
value exceeds the column's declared precision and whose reader does not reject it), Spark still
detects the overflow in
checkDecimalOverflowand nulls or raises. After this change Comet'snarrow
BinaryExprpasses the unvalidated value through. This is a difference from Spark only fordata that is already inconsistent with its declared type. The wide path does not have this gap:
WideDecimalBinaryExprperforms the declared-precision check itself after the i256 arithmetic.What changes are included in this PR?
DecimalPrecision.promoteasksaddSubFitsDecimal128/multiplyFitsDecimal128before addinga wrapper. The checks mirror
native/core/src/execution/planner.rsand useDecimalType.MAX_PRECISION.i16-safe arithmetic and matchdecimal128_add_sub_width, including negative scales.execution and schema checks at those boundaries in both eval modes, a TPC-H Q1-shaped plan
assertion on the serialized projection, and existing tests that deliberately pinned the old
wrapper for narrow arithmetic now use wide-path operands.
QueryPlanSerdeclassifies narrow decimal+ - *as unable to raise now that noCheckOverflowsits above them, so the subexpression pass may dedupe and NULL-skip them;QueryPlanSerdeSuitepins that.malformed-data difference. The bullet says the precision check is skipped; an
i128overflowstill fails, as before.
No native code changes. The TPC-DS plan-stability goldens change for 29 queries, and every change
is the final
Accelerated expressions: N nativeline dropping by 1. The operator trees,transitions, and percentages are unchanged. Each query loses exactly the one narrow-path
CheckOverflowwrapper this change removes. The diff touches 35 golden files because the basegoldens are also used by Spark 3.5 and as fallbacks for 4.0+, and every existing version-specific
copy of an affected query is decremented by the same one. The six version-specific copies for
Spark 4.0 and 4.2 were edited without regenerating them; CI on those profiles is what confirms
them.
How are these changes tested?
On the Spark 4.1 profile, with the native library built from the same native sources
(
git diff origin/main -- nativeempty):CometDecimalPromotionSuite— promoted proto shape for narrow vs. wide operands, the Q1plan-shape test, and execution/schema checks at widths 37/38 in ANSI and legacy mode.
DecimalPrecisionSuite— promotion unit tests and boundary conditions.CometDecimalArithmeticViewSuite— per-expressionallowDecimalPrecisionLossand eval-modecoverage on the still-wrapped wide/divide paths.
CometCodegenSuite— coverage-tag lifting through the promotion rewrite.CometExpressionSuite— includessafe ops on negative-scale decimal run natively, whichcovers the removed negative-scale guard.
CometTPCDSV1_4_PlanStabilitySuiteandCometTPCDSV2_7_PlanStabilitySuite— all 129 tests passafter the count-only golden update.
Counts, all re-run on the final commit: 146/146 across the four decimal suites, 129/129
plan-stability, 324 passed / 4 ignored / 0 failed on the wider view/codegen/expression run, and
13/13 for
QueryPlanSerdeSuite+DecimalPrecisionSuite.Note for merge order: this branch edits
docs/source/user-guide/latest/compatibility/index.md.PR #7758 renames that source directory to
dev/. If #7758 lands first, this PR needs the pathupdated; nothing else in the diff references the old name.