fix: preserve stored allowDecimalPrecisionLoss in DecimalPrecision rule#4179
Draft
andygrove wants to merge 1 commit intoapache:mainfrom
Draft
fix: preserve stored allowDecimalPrecisionLoss in DecimalPrecision rule#4179andygrove wants to merge 1 commit intoapache:mainfrom
andygrove wants to merge 1 commit intoapache:mainfrom
Conversation
Comet's `DecimalPrecision.promote` recomputed the result type for decimal Add/Subtract/Multiply/Divide/Remainder from the live `SQLConf` and wrapped the expression in `CheckOverflow` with that type. On Spark 4.1.1+ (SPARK-53968), arithmetic expressions store their `allowDecimalPrecisionLoss` per-instance so a view's analyzed plan keeps a stable result type across config changes. The recomputed type could disagree with `Add.dataType`, and the native `CheckOverflow` only relabels the decimal buffer (it does not rescale), shifting values by 10x. Use `expr.dataType` directly. On older Spark this is equivalent to the recomputed value; on 4.1+ it honours the stored evalContext.
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?
Closes #4124.
Rationale for this change
The new Spark 4.1 test
SPARK-53968 reading the view after allowPrecisionLoss is changedinSQLViewSuitereturns values 10x smaller than expected when run with Comet. The reproducer storesDECIMAL(38, 18)values in a view whose plan doesunit_price + COALESCE(shipping_price, 0), then re-reads the view after togglingspark.sql.decimalOperations.allowPrecisionLoss.Spark 4.1.1 (SPARK-53968) added a
NumericEvalContexttoAdd/Subtract/Multiply/Divide/IntegralDividethat capturesallowDecimalPrecisionLossat construction time, so a view's analyzed plan keeps a stable decimal result type across config changes. Comet'sDecimalPrecision.promoterule, however, recomputed the result type from the liveSQLConfand wrapped the expression inCheckOverflowwith that recomputed type. When the two disagreed (which is the SPARK-53968 scenario), the wrapper's target type no longer matched the child'sdataType.Comet's native
CheckOverflow(native/spark-expr/src/math_funcs/internal/checkoverflow.rs) only validates that values fit the target precision; it does not rescale. So aDecimal128Arrayproduced at scale 17 ended up relabelled as scale 18 (or vice versa), shifting every value by a factor of 10. With view created atallowPrecisionLoss=trueand read atfalse, expected100.00000000000000000came back as10.00000000000000000.What changes are included in this PR?
DecimalPrecision.promotenow sets theCheckOverflowtarget type toexpr.dataTypedirectly. On Spark 3.4 - 4.0 this is equivalent to the previous recomputation; on Spark 4.1+ it preserves the per-expressionallowDecimalPrecisionLosscaptured at view creation time.allowPrecisionLossparameter is dropped from the rule and its single call site inQueryPlanSerde.exprToProto.IgnoreCometannotation onSPARK-53968 reading the view after allowPrecisionLoss is changedis removed fromdev/diffs/4.1.1.diff.How are these changes tested?
CometDecimalArithmeticViewSuite(underspark/src/test/spark-4.1/) constructsAddwith aNumericEvalContextwhoseallowDecimalPrecisionLossdisagrees with the liveSQLConf, and asserts that the promotedCheckOverflowtarget tracksAdd.dataType. The test fails before the fix (target wasDecimalType(38,17)/DecimalType(38,18)instead of the stored type) and passes after.decimal division result type matches Sparkregression test inCometExpressionSuite, which already targeted a closely-related Spark 4 path, continues to pass.SQLViewSuite.SPARK-53968Spark SQL test will be exercised by the Spark SQL CI workflow once it is no longer ignored.