[common] Fix invalid DecimalType when extracting a variant decimal - #9672
Open
jackylee-ch wants to merge 1 commit into
Open
[common] Fix invalid DecimalType when extracting a variant decimal#9672jackylee-ch wants to merge 1 commit into
jackylee-ch wants to merge 1 commit into
Conversation
VariantGet took the precision and scale straight off the BigDecimal that getDecimal() returns. precision() counts the digits of the unscaled value, so it is below the scale for anything under 0.1, and the trailing zero stripping in getDecimal() turns 100.00 into 1E+2, a negative scale. DecimalType rejects both, so extracting such a value threw "Decimal scale must be between 0 and the precision 1". Rescale a negative scale to zero and widen the precision to the scale, which is bit for bit what Spark's Decimal.set(BigDecimal) does. The reader caps scale and precision at 38 in GenericVariantUtil.checkDecimal, so the widened precision stays inside DecimalType.
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.
Purpose
VariantGettook the precision and scale straight off theBigDecimalthatgetDecimal()returns.precision()counts the unscaled digits, so it is below the scale for anything under 0.1, and the trailing-zero stripping ingetDecimal()turns100.00into1E+2, a negative scale.DecimalTyperejects both, so extraction threwDecimal scale must be between 0 and the precision 1. Reachable on Spark 4.1+ pushdown, which accepts astringtarget and anytry_variant_get.A negative scale is rescaled to zero and the precision widened to the scale, matching Spark's
Decimal.set(BigDecimal).checkDecimalcaps both at 38 on read, so this stays in range.Two things it does not close, both owned by open PRs:
InferVariantShreddingSchemawidens precision but never clamps a negative scale, so{"round": 100.00}still throws undervariant.inferShreddingSchema([common] Keep the selected variant schema when its evidence is gone #9532).-DECIMALtoSTRINGnow yields100unshredded and100.00shredded, since the shredded leaf does not strip. Spark covers that withcastDecimalToString; Paimon's belongs inBaseVariantReader([common] Default missing variant shredding fields to the unshredded index #9616).Tests
GenericVariantTestfor both shapes and the scale-38 boundary,PaimonShreddingUtilsTestfor the pushdown path.Written with Claude Code; verification is mine.