fix: reject incompatible decimal precision/scale in native_datafusion scan#4090
Open
andygrove wants to merge 1 commit intoapache:mainfrom
Open
fix: reject incompatible decimal precision/scale in native_datafusion scan#4090andygrove wants to merge 1 commit intoapache:mainfrom
andygrove wants to merge 1 commit intoapache:mainfrom
Conversation
… scan The native_datafusion Spark physical expression adapter previously fell through to a Spark Cast for decimal-to-decimal type changes, which silently rescales or truncates values that should have raised an error. Mirror Spark's TypeUtil.isDecimalTypeMatched (Spark 3.x rule) by rejecting reads where the target precision is smaller than the source precision or the scales differ. Closes apache#4089.
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 #4089.
Rationale for this change
When the
native_datafusionscan reads a Parquet column with a higher-precision decimal physical type under a requested read schema with smaller precision or different scale, the existing schema adapter falls through to Spark'sCastexpression.Casthappily rescales or truncates, producing wrong values silently. Spark's vectorized reader rejects this withSchemaColumnConvertNotSupportedException, andnative_iceberg_compatalready does the same viaTypeUtil.checkParquetType. The native scan should match.What changes are included in this PR?
native/core/src/parquet/schema_adapter.rs: inreplace_with_spark_cast, add a guard before the existing branches that returns aDataFusionError::Planwhen bothphysical_typeandtarget_typeareDecimal128and either the target precision is smaller than the source precision or the scales differ. The rule mirrors Spark'sTypeUtil.isDecimalTypeMatched(Spark 3.x path).The check is intentionally narrow: it only fires for decimal-to-decimal mismatches, leaving every other type path unchanged. The closed prior attempt at broad schema validation in #3311 broke unrelated tests; this one does not.
Spark 4.0's decimal type widening (allowing strictly-larger target precision/scale per SPARK-37147) is not implemented here. For #4089's specific case (
Decimal(10,2)read asDecimal(5,0)) Spark 4.0 also rejects, so behavior is correct on all supported versions. Implementing 4.0's wider widening is a follow-up.How are these changes tested?
Added a focused test to
ParquetReadSuite:native_datafusion rejects incompatible decimal precision/scale. It writesDecimal(10, 2)data, reads it underDecimal(5, 0), forcesspark.comet.scan.impl=native_datafusionandspark.sql.sources.useV1SourceList=parquet, and asserts thatcollect()raisesSparkException. Verified againstParquetReadV1Suite(44 tests, all pass; 1 pre-existing test ignored). The behavior is also covered by the per-impl matrix added in #4087, which will need itsdecimal(10,2) read as decimal(5,0): native_datafusionassertion flipped from "succeeds" to "throws" in a follow-up once that PR merges.