Repository navigation
Spark: Fix Z-order NPE on null booleans and case-insensitive column resolution - #17669
Conversation
|
@GGraziadei Please submit your patch against Spark 4.1 first and back-port to previous versions when it's merged. |
9066c99 to
85f293d
Compare
|
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"); |
There was a problem hiding this comment.
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
});There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
…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.
8a99abf to
19c6f39
Compare
|
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 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 |
nssalian
left a comment
There was a problem hiding this comment.
Thanks for the PR @GGraziadei. lgtm.
|
Thanks @GGraziadei for the PR! Thanks @nssalian @uros-b for the review! |
|
@GGraziadei Could port the changes to 3.5, 4.0 and 4.2. Thanks! |
…s to 3.5, 4.0 and 4.2 Backport of apache#17669.
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.5andspark/v4.0. Per @manuzhang's request, this PRtargets 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-nullBooleanwith no guard, so asingle null aborted the whole rewrite with:
Boolean is the only converter with this problem — the seven numeric and temporal converters
already return
PRIMITIVE_EMPTYfor null, andstringToOrderedBytes/byteTruncateOrFillhandleit inside
ZOrderByteUtils. The surrounding code therefore already establishes the semantics — anull 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
validZOrderColNamesresolved the column case-insensitively whenspark.sql.caseSensitiveisfalse(the Spark default) but then stored the caller's spelling instead of the resolved fieldname. Validation passed, and the mis-cased name later reached the case-sensitive
df.schema().apply(...)inzValue, failing with:Z-ordering on
C2in a table whose column isc2now works, which is what case-insensitiveresolution promised.
Tests
Three regression tests were added to
TestRewriteDataFilesAction. Each was written first andconfirmed to fail for the expected reason before the fix:
zOrderUDFEncodesNullValuesAsZeroBytes— asserts that a null of every one of the 12 supportedtypes 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 usingC2/C3against columnsc2/c3.The full
TestRewriteDataFilesActionsuite passes (186 tests, 0 failures), along withcheckstyleMain,checkstyleTestandspotlessCheck.