Skip to content

perf: skip CheckOverflow for narrow decimal arithmetic - #7757

Draft
ErikBPF wants to merge 1 commit into
apache:mainfrom
ErikBPF:7659-skip-checkoverflow-decimal-arithmetic
Draft

ErikBPF wants to merge 1 commit into
apache:mainfrom
ErikBPF:7659-skip-checkoverflow-decimal-arithmetic

Conversation

@ErikBPF

@ErikBPF ErikBPF commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.promote wrapped every decimal Add, Subtract, and Multiply in
CheckOverflow. When the native planner takes its 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.

The native planner decides narrow vs. wide from the operand types:

  • + / - narrow when max(s1, s2) + max(p1 - s1, p2 - s2) < 38
  • * narrow when p1 + p2 < 38

Spark's result precision on that path is max(p1 - s1, p2 - s2) + max(s1, s2) + 1 for +/- and
p1 + p2 + 1 for *. Both are at most 38, so DecimalType.adjustPrecisionScale and
DecimalType.bounded leave them unchanged. DataFusion's decimal kernel computes the same result
type from the operand types, so the native child type equals expr.dataType and the wrapper adds
no type information. Each operand's rescaled magnitude is below 10^width, so a sum or difference
is 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, and
l_extendedprice * (1 - l_discount). The chained * (1 + l_tax) remains wide; the native planner
already drops that wrapper because WideDecimalBinaryExpr checks its own output.

Divide and Remainder keep their wrappers: native decimal division uses i128::MAX as an
overflow sentinel, which the wrapper converts to NULL or an ANSI error.

Performance

The gain is measured in #7658: CheckOverflow
was about 5% of native CPU samples in TPC-H Q1 at SF10, and every CheckOverflow in Q1 is on this
path. 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 checkDecimalOverflow and nulls or raises. After this change Comet's
narrow BinaryExpr passes the unvalidated value through. This is a difference from Spark only for
data that is already inconsistent with its declared type. The wide path does not have this gap:
WideDecimalBinaryExpr performs the declared-precision check itself after the i256 arithmetic.

What changes are included in this PR?

  • DecimalPrecision.promote asks addSubFitsDecimal128 / multiplyFitsDecimal128 before adding
    a wrapper. The checks mirror native/core/src/execution/planner.rs and use
    DecimalType.MAX_PRECISION.
  • The width checks use i16-safe arithmetic and match
    decimal128_add_sub_width, including negative scales.
  • Tests: promotion boundary cases at width 37 (narrow) and 38 (wide) for all three operators,
    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.
  • QueryPlanSerde classifies narrow decimal + - * as unable to raise now that no
    CheckOverflow sits above them, so the subexpression pass may dedupe and NULL-skip them;
    QueryPlanSerdeSuite pins that.
  • Docs: one line in the math expression audit and one bullet in the compatibility guide for the
    malformed-data difference. The bullet says the precision check is skipped; an i128 overflow
    still 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 native line dropping by 1. The operator trees,
transitions, and percentages are unchanged. Each query loses exactly the one narrow-path
CheckOverflow wrapper this change removes. The diff touches 35 golden files because the base
goldens 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 -- native empty):

  • CometDecimalPromotionSuite — promoted proto shape for narrow vs. wide operands, the Q1
    plan-shape test, and execution/schema checks at widths 37/38 in ANSI and legacy mode.
  • DecimalPrecisionSuite — promotion unit tests and boundary conditions.
  • CometDecimalArithmeticViewSuite — per-expression allowDecimalPrecisionLoss and eval-mode
    coverage on the still-wrapped wide/divide paths.
  • CometCodegenSuite — coverage-tag lifting through the promotion rewrite.
  • CometExpressionSuite — includes safe ops on negative-scale decimal run natively, which
    covers the removed negative-scale guard.
  • CometTPCDSV1_4_PlanStabilitySuite and CometTPCDSV2_7_PlanStabilitySuite — all 129 tests pass
    after 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 path
updated; nothing else in the diff references the old name.

@ErikBPF
ErikBPF force-pushed the 7659-skip-checkoverflow-decimal-arithmetic branch 3 times, most recently from 31fbddf to 7cf2754 Compare November 24, 2026 09:52
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.
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.

1 participant