Repository navigation
Description
Apache Iceberg version
main (branch)
Query engine
Spark
Please describe the bug 🐞
Two independent defects in the Spark Z-order clustering path, both present in spark/v3.5,
spark/v4.0 and spark/v4.1. They were spotted by @manuzhang while reviewing #16827, which adds a
Hilbert strategy that reuses this same code
(#16827 (comment) and
#16827 (comment)).
They are filed separately from that PR because fixing them changes existing Z-order behaviour, which
deserves its own review rather than riding along with a new feature. Once fixed here, both
strategies benefit, since the Hilbert runner reuses SparkZOrderUDF.sortedLexicographically for the
per-column byte conversion.
Correction (edited): an earlier revision of this issue claimed the numeric converters were
affected too. That is wrong — they already guard against null.booleanToOrderedBytesUDFis the
only converter missing the guard. The description below is the verified version.
1. NullPointerException when a boolean clustering column contains nulls
SparkZOrderUDF.booleanToOrderedBytesUDF() unboxes a possibly-null Boolean with no guard:
(Boolean value) -> {
ByteBuffer buffer = inputBuffer(position, ZOrderByteUtils.PRIMITIVE_BUFFER_SIZE);
buffer.put(0, (byte) (value ? -127 : 0)); // NPE when value is null
return buffer.array();
}Spark passes nulls straight through to Java UDFs, so any null in a clustered boolean column aborts
the whole rewrite with [FAILED_EXECUTE_UDF] ... NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because "value" is null.
This is the only converter with the problem. Every other type is already handled:
- the seven numeric and temporal converters (
tinyToOrderedBytesUDF,shortToOrderedBytesUDF,
intToOrderedBytesUDF,longToOrderedBytesUDF,floatToOrderedBytesUDF,
doubleToOrderedBytesUDF,timestampNtzToOrderedBytesUDF) already returnPRIMITIVE_EMPTY— an
all-zero, 8-byte array — when the value is null; stringToOrderedBytesandbyteTruncateOrFillhandle null insideZOrderByteUtils, also
producing all-zero bytes.
So the intended semantics are already established by the surrounding code — a null sorts lowest,
encoded as all-zero bytes — and boolean simply fails to follow it. The fix is to make boolean
consistent rather than to invent a new convention, which keeps this a genuine bug fix with no
behaviour change for any type that works today.
To reproduce
CREATE TABLE db.tbl (id INT, flag BOOLEAN) USING iceberg;
INSERT INTO db.tbl VALUES (1, true), (2, null), (3, false);
CALL system.rewrite_data_files(
table => 'db.tbl',
strategy => 'sort',
sort_order => 'zorder(id, flag)'
);There is currently no test covering null values on the Z-order path, so the fix should come with
one.
2. Case-insensitive validation keeps the caller's spelling
SparkZOrderFileRewriteRunner.validZOrderColNames resolves the column case-insensitively when
spark.sql.caseSensitive is false (the Spark default), but then stores the string the caller
supplied rather than the resolved field name:
Types.NestedField field =
caseSensitive ? schema.findField(colName) : schema.caseInsensitiveFindField(colName);
Preconditions.checkArgument(field != null, "Cannot find column '%s' ...");
...
validZOrderColNames.add(colName); // caller's spelling, not field.name()Validation therefore passes, but the mis-cased name is what later reaches df.schema().apply(...)
in zValue, which is case-sensitive. Z-ordering on C2 in a table whose column is c2 fails with:
org.apache.spark.SparkIllegalArgumentException: [FIELD_NOT_FOUND]
No such struct field `C2` in `c1`, `c2`, `c3`. SQLSTATE: 42704
instead of being caught by the check that was supposed to catch it — or, more to the point, instead
of just working, which is what case-insensitive resolution promised.
The fix is to add field.name() instead of colName, plus a test with a mis-cased column name.
Willingness to contribute
- I can contribute a fix for this bug independently
Activity