Skip to content

[SPARK-54586][SQL] Validate UTF-8 when casting Binary to String - #53458

Closed
qlong wants to merge 3 commits into
apache:masterfrom
qlong:SPARK-54586-fix-invalid-utf8-cast
Closed

qlong wants to merge 3 commits into
apache:masterfrom
qlong:SPARK-54586-fix-invalid-utf8-cast

Conversation

@qlong

@qlong qlong commented Dec 12, 2025 •

Copy link
Copy Markdown
Contributor

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.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)

References:

  • Snowflake: TO_VARCHAR throws error on invalid UTF-8, TRY_TO_VARCHAR returns NULL
  • PostgreSQL: Rejects invalid UTF-8 by default, validates on input
  • Redshift: Throws error when casting invalid bytes to VARCHAR

This 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:

// Before this PR: Invalid UTF-8 bytes are silently allowed in StringType
val bytes = Array[Byte](0x80.toByte, 0xFF.toByte)  // Invalid UTF-8
// Cast creates UTF8String containing raw bytes [0x80, 0xFF]
// These raw invalid bytes violate UTF-8 spec but are stored anyway

Spark's StringType is UTF-8 by definition. Invalid UTF-8 in StringType:

  1. Violates type system: StringType should only contain valid UTF-8
  2. Causes data loss: When other tools read the data, they may replace with U+FFFD (changing 2 bytes to 6 bytes) or fail
  3. Breaks interoperability: Other tools may fail reading the data entirely as Parquet, ORC, JSON all require valid UTF-8 for strings

Current behavior (bug):

val df = Seq(Array[Byte](0x80.toByte, 0xFF.toByte)).toDF("binary")
df.selectExpr("cast(binary as string) as str")
  .write.parquet("/tmp/output")
// Result: Parquet file contains invalid UTF-8 bytes [0x80, 0xFF] in STRING column
// Other tools reading this file may:
//   - Replace with U+FFFD replacement characters (data corruption)
//   - Fail to read the file
//   - Produce undefined behavior

With this fix:

// Default behavior (LEGACY mode)
spark.sql("SELECT CAST(X'80' AS STRING)").show()  // NULL

// ANSI mode
spark.conf.set("spark.sql.ansi.enabled", true)
spark.sql("SELECT CAST(X'80' AS STRING)")  // Throws SparkRuntimeException
spark.sql("SELECT CAST(X'80' AS CHAR(3))") // Throws SparkRuntimeException
spark.sql("SELECT CAST(X'80' AS VARCHAR(3))") // Throws SparkRuntimeException
spark.sql("SELECT CAST(ARRAY(X'80') AS ARRAY<char(10)>)") // Throws SparkRuntimeException
spark.sql("SELECT CAST(ARRAY(X'80') AS STRING)") // No change of behavior

// Legacy behavior still available
spark.conf.set("spark.sql.castBinaryToString.validateUtf8", false)
spark.sql("SELECT CAST(X'80' AS STRING)").show()  // Old behavior
spark.sql("SELECT CAST(X'80' AS CHAR(3))").show() // Old behavior
spark.sql("SELECT CAST(X'80' AS VARCHAR(3))").show() // Old behavior
spark.sql("SELECT CAST(ARRAY(X'80') AS ARRAY<char(10)>)") // Old behavior

Does this PR introduce any user-facing change?

Yes.

With default config (validateUtf8=true), casting invalid UTF-8 to string now:

  • LEGACY/TRY mode: Returns NULL instead of corrupted string
  • ANSI mode: Throws SparkRuntimeException with error code CAST_INVALID_INPUT

Previous behavior (before this PR):

scala> spark.sql("SELECT CAST(X'80' AS STRING)").show()
+---------------------+
|CAST(X'80' AS STRING)|
+---------------------+
|                    �|  // Invalid UTF-8 byte (raw 0x80 stored, displays as �)
+---------------------+

New behavior (after this PR, default config):

// LEGACY mode (default)
scala> spark.sql("SELECT CAST(X'80' AS STRING)").show()
+------------------------+
|CAST(X'80' AS STRING)   |
+------------------------+
|NULL                    |  // NULL instead of corrupted data
+------------------------+

// ANSI mode
scala> spark.conf.set("spark.sql.ansi.enabled", true)
scala> spark.sql("SELECT CAST(X'80' AS STRING)")
org.apache.spark.SparkRuntimeException: [CAST_INVALID_INPUT] The value \x80 of the type "BINARY" cannot be cast to "STRING" because it is malformed.

Migration path:

  • Should use the new default behavior (prevents data corruption)
  • Users needing old behavior: set spark.sql.castBinaryToString.validateUtf8=false
  • Use TRY_CAST(binary AS STRING) for safe conversion that returns NULL on failure

How 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: Added SET spark.sql.castBinaryToString.validateUtf8=false at 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 inline SET commands to toggle validation for specific test sections, providing comprehensive coverage of both modes.

  • StringFunctionsSuite.scala: Wrapped invalid UTF-8 test cases for is_valid_utf8, make_valid_utf8, validate_utf8, and try_validate_utf8 with withSQLConf(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: Added needsInvalidUtf8 set containing hll.sql and thetasketch.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 the ignoreSet for 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: Sets spark.sql.castBinaryToString.validateUtf8=false to 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

@github-actions github-actions Bot added the SQL label Dec 12, 2025
@rluvaton

Copy link
Copy Markdown
Member

If this pr get merged, will it be possible to port to 3.5 as well?

@rluvaton

Copy link
Copy Markdown
Member

Also should it return null or do lossy conversion

@qlong

qlong commented Dec 15, 2025

Copy link
Copy Markdown
Contributor Author

@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.

@qlong
qlong force-pushed the SPARK-54586-fix-invalid-utf8-cast branch 9 times, most recently from d90014d to 075ef79 Compare January 27, 2026 18:28
@github-actions

Copy link
Copy Markdown

JIRA Issue Information

=== Sub-task SPARK-54586 ===
Summary: Spark is creating invalid parquet files - put non utf-8 data in string column
Assignee: None
Status: Open
Affected: ["3.5.1","3.5.3","4.1.0","4.0.0","4.0.1","3.5.7"]


This comment was automatically generated by GitHub Actions

@qlong
qlong force-pushed the SPARK-54586-fix-invalid-utf8-cast branch 2 times, most recently from 222912f to 8c7af75 Compare January 29, 2026 04:01
@qlong
qlong requested a review from attilapiros January 29, 2026 21:31
@qlong
qlong force-pushed the SPARK-54586-fix-invalid-utf8-cast branch from 8c7af75 to aa1bf79 Compare February 2, 2026 22:46
@qlong
qlong force-pushed the SPARK-54586-fix-invalid-utf8-cast branch 3 times, most recently from c5dd73c to d5ce4af Compare February 11, 2026 15:48
@qlong
qlong requested a review from attilapiros February 11, 2026 21:15
qlong added 3 commits May 18, 2026 12:46
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
@qlong
qlong force-pushed the SPARK-54586-fix-invalid-utf8-cast branch from d5ce4af to 7cd6370 Compare May 18, 2026 16:47
@github-actions

Copy link
Copy Markdown

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.
If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!

@github-actions github-actions Bot added the Stale label Aug 27, 2026
@github-actions github-actions Bot closed this Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants