Skip to content

Spark: Fix Z-order NPE on null booleans and case-insensitive column resolution - #17669

Merged
huaxingao merged 4 commits into
apache:mainfrom
GGraziadei:fix-zorder-null-and-case-handling
Sep 11, 2026
Merged

huaxingao merged 4 commits into
apache:mainfrom
GGraziadei:fix-zorder-null-and-case-handling

Conversation

@GGraziadei

@GGraziadei GGraziadei commented Aug 15, 2026 •

Copy link
Copy Markdown
Member

Closes #17658.

Two independent defects in the Spark Z-order clustering path, both spotted by @manuzhang while
reviewing #16827. They are fixed here rather than in that PR because they change existing Z-order
behaviour and deserve their own review.

Both defects are also present in spark/v3.5 and spark/v4.0. Per @manuzhang's request, this PR
targets Spark 4.1 only; back-ports to 4.0 and 3.5 will follow once this is merged.

1. NPE when a boolean clustering column contains nulls

SparkZOrderUDF.booleanToOrderedBytesUDF() unboxed a possibly-null Boolean with no guard, so a
single null aborted the whole rewrite with:

[FAILED_EXECUTE_UDF] ... NullPointerException:
Cannot invoke "java.lang.Boolean.booleanValue()" because "value" is null

Boolean is the only converter with this problem — the seven numeric and temporal converters
already return PRIMITIVE_EMPTY for null, and stringToOrderedBytes / byteTruncateOrFill handle
it inside ZOrderByteUtils. The surrounding code therefore already establishes the semantics — a
null sorts lowest, as all-zero bytes — and this change simply makes boolean follow it. No type that
works today changes behaviour.

2. Case-insensitive validation kept the caller's spelling

validZOrderColNames resolved the column case-insensitively when spark.sql.caseSensitive is
false (the Spark default) but then stored the caller's spelling instead of the resolved field
name. Validation passed, and the mis-cased name later reached the case-sensitive
df.schema().apply(...) in zValue, failing with:

[FIELD_NOT_FOUND] No such struct field `C2` in `c1`, `c2`, `c3`. SQLSTATE: 42704

Z-ordering on C2 in a table whose column is c2 now works, which is what case-insensitive
resolution promised.

Tests

Three regression tests were added to TestRewriteDataFilesAction. Each was written first and
confirmed to fail for the expected reason before the fix:

  • zOrderUDFEncodesNullValuesAsZeroBytes — asserts that a null of every one of the 12 supported
    types produces all-zero ordered bytes, pinning the null-sorts-lowest convention across types
    rather than only checking that boolean no longer throws;
  • zOrderSortWithNullBooleanValues — end-to-end rewrite over a table with null booleans;
  • zOrderSortWithMismatchedColumnCase — end-to-end rewrite using C2/C3 against columns
    c2/c3.

The full TestRewriteDataFilesAction suite passes (186 tests, 0 failures), along with
checkstyleMain, checkstyleTest and spotlessCheck.

@github-actions github-actions Bot added the spark label Aug 15, 2026
@manuzhang

Copy link
Copy Markdown
Member

@GGraziadei Please submit your patch against Spark 4.1 first and back-port to previous versions when it's merged.

@GGraziadei
GGraziadei force-pushed the fix-zorder-null-and-case-handling branch from 9066c99 to 85f293d Compare August 15, 2026 15:47
@GGraziadei

Copy link
Copy Markdown
Member Author

Thanks for the feedback, @manuzhang ! I've reduced the scope of this PR to target Spark 4.1. Once this is merged, I'll follow up with PRs for 4.0 and 3.5.

public void zOrderSortWithMismatchedColumnCase() {
assertThat(spark.conf().get("spark.sql.caseSensitive"))
.as("This test covers the case-insensitive column resolution path")
.isEqualTo("false");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this asserts on whatever spark.sql.caseSensitive happens to be rather than setting it, so the test quietly depends on no earlier test in the shared session having changed it — and if one does, it fails on this assertion instead of testing the resolution path. withSQLConf pins it and lets the assertion go:

withSQLConf(
    ImmutableMap.of(SQLConf.CASE_SENSITIVE().key(), "false"),
    () -> {
      // existing body
    });

@GGraziadei GGraziadei Sep 9, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @huaxingao,
Fixed, thanks. Out of curiosity for future changes: since the session is shared across tests, would it make sense to reset the SQL conf in an @AfterEach in TestBase or directly close and restart the session, so that a test forgetting to use withSQLConf cannot leak config into the others? This point is related to test idempotency

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not reset config in @AfterEach because it runs after every test method, so a
reset hook would wipe per-class setup. This class is a good example -- setupSpark()
disables AQE in @BeforeAll because "tests assume that writes generate a particular
number of files". Reset after each test and AQE comes back on for every method after the
first.

GGraziadei and others added 2 commits September 9, 2026 21:16
…s/SparkZOrderFileRewriteRunner.java

Co-authored-by: Huaxin Gao <[email protected]>
Use withSQLConf to set the config to false for the test instead of
asserting on whatever value the shared session happens to have.
@GGraziadei
GGraziadei force-pushed the fix-zorder-null-and-case-handling branch from 8a99abf to 19c6f39 Compare September 9, 2026 19:43
@GGraziadei

Copy link
Copy Markdown
Member Author

To put this change in context: the NPE fix here is about correctness of the code path, not about making boolean Z-ordering more useful. A boolean contributes a single meaningful bit to the interleaved key, and since that bit lands at the top of the Z-value it effectively splits the data into two halves and then Z-orders the remaining columns within each half. That is a valid layout, but it is closer to sort by flag than to real multi-dimensional clustering, and the same is true for any very low-cardinality column.

Would it make sense to also log a warning when a Z-order or Hilbert column is boolean, or more generally has a type whose cardinality is known to be tiny? It wouldn't change behavior, but it would steer users away from a configuration that does less than they probably expect.

Happy to add it in this PR or as a follow-up, whichever you prefer.

@GGraziadei
GGraziadei requested a review from huaxingao September 9, 2026 21:56
@huaxingao

Copy link
Copy Markdown
Contributor

To put this change in context: the NPE fix here is about correctness of the code path, not about making boolean Z-ordering more useful. A boolean contributes a single meaningful bit to the interleaved key, and since that bit lands at the top of the Z-value it effectively splits the data into two halves and then Z-orders the remaining columns within each half. That is a valid layout, but it is closer to sort by flag than to real multi-dimensional clustering, and the same is true for any very low-cardinality column.

Would it make sense to also log a warning when a Z-order or Hilbert column is boolean, or more generally has a type whose cardinality is known to be tiny? It wouldn't change behavior, but it would steer users away from a configuration that does less than they probably expect.

Happy to add it in this PR or as a follow-up, whichever you prefer.

No strong opinion, but I'd leave it out of this PR. The case that actually hurts is a low
distinct-value count (e.g. a string with three values), which isn't visible from the type
-- so a type-based warning would miss the common cases while firing on boolean columns
that are a reasonable choice.

@huaxingao huaxingao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nssalian nssalian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @GGraziadei. lgtm.

@huaxingao
huaxingao merged commit 99c291d into apache:main Sep 11, 2026
36 checks passed
@huaxingao

Copy link
Copy Markdown
Contributor

Thanks @GGraziadei for the PR! Thanks @nssalian @uros-b for the review!

@huaxingao

Copy link
Copy Markdown
Contributor

@GGraziadei Could port the changes to 3.5, 4.0 and 4.2. Thanks!

GGraziadei added a commit to GGraziadei/iceberg that referenced this pull request Sep 12, 2026
huaxingao pushed a commit that referenced this pull request Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spark: Z-order rewrite fails with NPE on null values and keeps the caller's column spelling when case-insensitive

5 participants