fix: reject string/binary read as numeric in native_datafusion scan#4091
Open
andygrove wants to merge 1 commit intoapache:mainfrom
Open
fix: reject string/binary read as numeric in native_datafusion scan#4091andygrove wants to merge 1 commit intoapache:mainfrom
andygrove wants to merge 1 commit intoapache:mainfrom
Conversation
The native_datafusion Spark physical expression adapter fell through to DataFusion's cast for Utf8/Binary -> numeric type changes (because SparkCastOptions.is_adapting_schema delegates to DataFusion's cast), which silently parses the bytes (returning nulls or, on some paths, reinterpreting raw bytes) where Spark's vectorized reader and the native_iceberg_compat scan throw SchemaColumnConvertNotSupportedException. Add a guard in replace_with_spark_cast that rejects when the source type is Utf8/LargeUtf8/Binary/LargeBinary and the target type is any integer or floating-point type, mirroring TypeUtil.checkParquetType on the JVM side. Closes apache#4088.
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 #4088.
Rationale for this change
When the
native_datafusionscan reads a ParquetBINARY(UTF8) column under a numeric read schema, the existing schema adapter creates a SparkCastwithis_adapting_schema=true. In that modeCastdelegates to DataFusion's cast, which parses the bytes (returning null on non-numeric strings, or in some paths reinterpreting the raw bytes). Spark's vectorized reader rejects this kind of mismatch withSchemaColumnConvertNotSupportedExceptionon every supported version, 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 returnsDataFusionError::Planwhen the source type isUtf8,LargeUtf8,Binary, orLargeBinaryand the target type is any integer (Int8/Int16/Int32/Int64/UInt*) or floating-point type (Float32/Float64). The rule mirrorsTypeUtil.checkParquetType'sBINARYcase (lines 208-221), which only allows reading BINARY asStringType,BinaryType, or a binary-encoded decimal.The check is intentionally narrow: it only fires for string/binary -> numeric mismatches and leaves every other type path unchanged.
How are these changes tested?
Added a focused test to
ParquetReadSuite:native_datafusion rejects string read as numeric. It writes string data, reads it underc int, forcesspark.comet.scan.impl=native_datafusionandspark.sql.sources.useV1SourceList=parquet, and asserts thatcollect()raisesSparkException. Verified againstParquetReadV1Suite(44 succeeded, no regressions; 1 pre-existing test ignored).The behavior is also covered by the per-impl matrix added in #4087 (
string read as int: native_datafusion), whose assertion will need flipping from "succeeds with garbage" to "throws" once that PR merges.