Repository navigation
Conversation
|
If this pr get merged, will it be possible to port to 3.5 as well? |
|
Also should it return null or do lossy conversion |
|
@rluvaton I was a bit surprised the issue was discovered only recently. If this PR is approved, the community can decide if it needs be backported to previous versions. With regard to the behavior of returning null in the presence of invalid utf-8 bytes, I think it is more aligned with what other engines do. Redshift has a separate function that allows users to specify the replacement byte. |
d90014d to
075ef79
Compare
JIRA Issue Information=== Sub-task SPARK-54586 === This comment was automatically generated by GitHub Actions |
222912f to
8c7af75
Compare
8c7af75 to
aa1bf79
Compare
c5dd73c to
d5ce4af
Compare
This PR adds UTF-8 validation when casting from BinaryType to StringType to prevent data corruption. Spark's StringType is UTF-8 by design, and invalid UTF-8 violates the type system, causing irreversible data loss. Added spark.sql.castBinaryToString.validateUtf8 config (default=true): - When true (default): Validates UTF-8 during cast * ANSI mode: Throws exception on invalid UTF-8 * LEGACY mode: Returns NULL on invalid UTF-8 * TRY mode: Returns NULL on invalid UTF-8 - When false: Preserves old behavior (not recommended) Tests include unit tests and SQL tests covering both default behavior (validation ON) and legacy mode (validation OFF). Aligns with major databases' UTF-8 validation behavior: - Snowflake: TO_VARCHAR throws, TRY_TO_VARCHAR returns NULL - PostgreSQL: Rejects invalid UTF-8 by default - Redshift: Throws error when casting invalid bytes to VARCHAR BREAKING CHANGE: With default config, invalid UTF-8 casts now return NULL (LEGACY/TRY) or throw (ANSI) instead of silently corrupting data. Users needing old behavior can set validateUtf8=false.
Addresses PR review comments from @attilapiros: * Updated Cast.forceNullable() to check the actual session config values instead of unconditionally returning `false` for Binary to String casts. This ensures correct nullability at analysis time. * Add tests for Array/Map/Struct with invalid UTF-8 in LEGACY, ANSI, and TRY modes * Updated `_as_string_type()` in `base.py` to query actual Spark DataFrame schema after the cast * Added pandas tests for Binary to String cast with UTF-8 validation enabled and disabled
…/varchar Addresses PR review comments from @attilapiros: * Valdiate UTF-8 when casting Binary to char/varchar (in addition to string) * Reduce overhead in pandas: Query spark schema only for Binary to String * Tight up tests, explicitly test eval mode
d5ce4af to
7cd6370
Compare
|
We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable. |
What changes were proposed in this pull request?
This PR adds UTF-8 validation when casting from BinaryType to StringType/Varchar/Char to prevent data corruption. Spark's StringType is UTF-8 by design, invalid UTF-8 violates the type system, causing irreversible data loss.
Added
spark.sql.castBinaryToString.validateUtf8config (default=true):false: Preserves old behavior (not recommended)References:
TO_VARCHARthrows error on invalid UTF-8,TRY_TO_VARCHARreturns NULLThis PR brings Spark's behavior in line with those engines for UTF-8 validation.
Why are the changes needed?
This PR fixes SPARK-54586.
The fundamental problem:
Spark's
StringTypeis UTF-8 by definition. Invalid UTF-8 in StringType:Current behavior (bug):
With this fix:
Does this PR introduce any user-facing change?
Yes.
With default config (
validateUtf8=true), casting invalid UTF-8 to string now:SparkRuntimeExceptionwith error codeCAST_INVALID_INPUTPrevious behavior (before this PR):
New behavior (after this PR, default config):
Migration path:
spark.sql.castBinaryToString.validateUtf8=falseTRY_CAST(binary AS STRING)for safe conversion that returns NULL on failureHow was this patch tested?
Verified interactively with spark-shell
Added new tests covering both default behavior and legacy mode.
Test updates for existing tests:
Several existing tests use invalid UTF-8 and required updates:
hll.sql,thetasketch.sql: AddedSET spark.sql.castBinaryToString.validateUtf8=falseat the beginning of each file to preserve original collation test coverage with invalid UTF-8 casts.listagg.sql,string-functions.sql: Tests both default (validateUtf8=true) and legacy (validateUtf8=false) behaviors using inlineSETcommands to toggle validation for specific test sections, providing comprehensive coverage of both modes.StringFunctionsSuite.scala: Wrapped invalid UTF-8 test cases foris_valid_utf8,make_valid_utf8,validate_utf8, andtry_validate_utf8withwithSQLConf(SQLConf.VALIDATE_BINARY_TO_STRING_CAST.key -> "false"). This is necessary because these functions test invalid UTF-8 validation logic, but with the new default behavior, the BinaryType → StringType cast validates UTF-8 first and throws (ANSI) or returns NULL (LEGACY) before the functions can execute. Disabling cast validation allows the functions to receive and test their own validation logic.ThriftServerQueryTestSuite.scala: AddedneedsInvalidUtf8set containinghll.sqlandthetasketch.sql. These files automatically get UTF-8 validation disabled when run through ThriftServer.ExpressionInfoSuite.scala: Added UTF-8 validation functions (IsValidUTF8,MakeValidUTF8,ValidateUTF8,TryValidateUTF8) to theignoreSetfor the "check-outputs-of-expression-examples" test. Their documentation examples use invalid UTF-8 (e.g.,x'80'), which would fail with the new default before examples can execute. The functions are properly tested via unit tests and SQL tests with explicit config.gen-sql-functions-docs.py: Setsspark.sql.castBinaryToString.validateUtf8=falseto allow generation of documentation for UTF-8 validation functions with invalid UTF-8 examples.Was this patch authored or co-authored using generative AI tooling?
Co-authoredL Claude Sonnet 4.5