ErikBPF opened a new pull request, #7757:
URL: https://github.com/apache/datafusion-comet/pull/7757

   ## Which issue does this PR close?
   
   Part of https://github.com/apache/datafusion-comet/issues/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 
https://github.com/apache/datafusion-comet/issues/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.
   - Docs: one line in the math expression audit and one bullet in the 
compatibility guide for the
     malformed-data difference.
   
   No native code changes. No TPC-DS golden changes.
   
   ## How are these changes tested?
   
   - `CometDecimalPromotionSuite` — promoted proto shape for narrow vs. wide 
operands, plus the Q1
     plan-shape test.
   - `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` decimal tests.
   - `CometTPCDSV2_7_PlanStabilitySuite` — no golden diff.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to