[common] Fix inferred variant shredding failing on decimals with trailing zeros - #9811
Open
zhuxiangyi wants to merge 1 commit into
Open
[common] Fix inferred variant shredding failing on decimals with trailing zeros#9811zhuxiangyi wants to merge 1 commit into
zhuxiangyi wants to merge 1 commit into
Conversation
…ling zeros InferVariantShreddingSchema builds the shredded type of a decimal from getDecimal(), which strips trailing zeros: 10.0 becomes 1E+1 and 100.00 becomes 1E+2, both with a negative scale. DataTypes.DECIMAL rejects that, so any file whose sampled rows contain such a value failed to write with "Decimal scale must be between 0 and the precision 1" as soon as variant.inferShreddingSchema was enabled. Integers beyond the long range with trailing zeros hit the same path. Fold a negative scale back into the digits before building the type, the way VariantGet already does since apache#9672, and keep the precision at least the scale for values below 0.1.
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
This is a bug fix. With
variant.inferShreddingSchema=true, writing a Variant that contains a decimal with trailing zeros fails the whole file:InferVariantShreddingSchema.schemaOfderives the shredded decimal type fromgetDecimal(), which strips trailing zeros:10.0arrives as1E+1and100.00as1E+2, both with a negative scale thatDecimalTyperejects. The existing guard only coveredprecision < scale(values below0.1). Integers beyond thelongrange that end in zeros (100000000000000000000→1E+20) hit the same path. Any engine that writes through the inference path (Spark, Flink) is affected, and10.0-style values are common in real data.The fix folds a negative scale back into the digits (
setScale(0)) before building the type, the same normalisationVariantGetgained in #9672, and keeps the precision at least the scale.10.0now infers asDECIMAL(2, 0), merges with20.5intoDECIMAL(18, 1), and a lone100.00widens toBIGINTas an integer-like decimal, which the shredding writer accepts because it compares against the original scale and allows exact rescaling.Tests
The new tests reproduce the bug: on
masterthey fail with theIllegalArgumentExceptionabove (both the unit test and the two inferred-shredding Spark suites), and pass with this change.InferVariantShreddingSchemaTest#testInferSchemaWithDecimalTrailingZeros:10.0/100.00/0.05/100000000000000000000infer toDECIMAL(18, 1)/BIGINT/DECIMAL(18, 3)/DECIMAL(38, 0).InferVariantShreddingWriteTest#testInferSchemaWithDecimalTrailingZeros: writes such rows with inference on, checks the physical Parquet schema, the reconstructed Variant and typed extraction of$.price/$.whole.VariantTestBase: inserts10.0/100.00and reads them back; runs under all four Spark 4.x configurations. Verified on Spark 4.1.2: 116 tests pass.API and Format
No.
Documentation
No.