You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ur...@apache.org on 2026/09/14 13:49:35 UTC
(spark) branch branch-4.x updated: [SPARK-59491][SQL] Zero the reserved payload when writing a null CalendarInterval to UnsafeRow
This is an automated email from the ASF dual-hosted git repository.
uros-b pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new 66ae652a61ad [SPARK-59491][SQL] Zero the reserved payload when writing a null CalendarInterval to UnsafeRow
66ae652a61ad is described below
commit 66ae652a61ad29a378b8a47c807aefa131b35044
Author: Stevo Mitric <st...@gmail.com>
AuthorDate: Mon Sep 14 13:49:04 2026 +0000
[SPARK-59491][SQL] Zero the reserved payload when writing a null CalendarInterval to UnsafeRow
### What changes were proposed in this pull request?
`UnsafeWriter.write(int, CalendarInterval)` reserves a 16-byte variable-length payload for every value, but its null branch only sets the null bit and leaves those bytes untouched. This PR zeroes the reserved payload in the null branch, mirroring the in-place update path `UnsafeRow#setInterval`, which already zeroes it the same way.
### Why are the changes needed?
Because the projection output buffer is reused across rows, a null interval inherits the stale bytes of a previous non-null row, so two logically-equal null values can encode to different `UnsafeRow` bytes. A nullable `CalendarInterval` used as a hash GROUP BY or join key can therefore split its NULLs across several groups. This is a latent correctness defect found by code inspection (the sibling of the same issue in the nanosecond-timestamp writer); there is no demonstrated query-lev [...]
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
New `UnsafeRowConverterSuite` test "null interval keys are byte-identical regardless of prior rows" projects a null after two different non-null intervals and asserts the two null rows are byte-identical. It fails without the fix (both codegen and interpreted modes) and passes with it.
### Was this patch authored or co-authored using generative AI tooling?
Co-Authored-By: Claude Opus 4.8
Closes #58774 from stevomitric/stevomitric/calendar-interval-null-key.
Authored-by: Stevo Mitric <st...@gmail.com>
Signed-off-by: Uros Bojanic <22...@users.noreply.github.com>
(cherry picked from commit dc7ac4b62fce6513a5d61521e7c8fe058639da5f)
Signed-off-by: Uros Bojanic <22...@users.noreply.github.com>
---
.../catalyst/expressions/codegen/UnsafeWriter.java | 2 ++
.../expressions/UnsafeRowConverterSuite.scala | 27 ++++++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeWriter.java b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeWriter.java
index 20ec4c2bb0c6..d6ca7afa9e9c 100644
--- a/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeWriter.java
+++ b/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/codegen/UnsafeWriter.java
@@ -145,6 +145,8 @@ public abstract class UnsafeWriter {
if (input == null) {
BitSetMethods.set(getBuffer(), startingOffset, ordinal);
+ Platform.putLong(getBuffer(), cursor(), 0L);
+ Platform.putLong(getBuffer(), cursor() + 8, 0L);
} else {
// Write the months, days and microseconds fields of interval to the variable length portion.
long longVal =
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/UnsafeRowConverterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/UnsafeRowConverterSuite.scala
index 8ffece81a474..c3611019d42a 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/UnsafeRowConverterSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/UnsafeRowConverterSuite.scala
@@ -73,6 +73,33 @@ class UnsafeRowConverterSuite extends SparkFunSuite with Matchers with Expressio
assert(unsafeRow2.getInt(2) === 2)
}
+ testBothCodegenAndInterpreted(
+ "null interval keys are byte-identical regardless of prior rows") {
+ // CalendarInterval occupies a 16-byte variable-length payload. The projection reuses its output
+ // buffer across rows, so a null value must zero that payload; otherwise it inherits the prior
+ // non-null value's bytes and two null rows compare unequal -- which splits a nullable interval
+ // GROUP BY / join key into several null groups.
+ val fieldTypes: Array[DataType] = Array(CalendarIntervalType)
+ val converter = UnsafeProjection.create(fieldTypes)
+ val row = new SpecificInternalRow(fieldTypes.toImmutableArraySeq)
+
+ // Dirty the reused buffer with one non-null value, then project a null.
+ row.update(0, new CalendarInterval(3, 1, 1000L))
+ converter.apply(row)
+ row.setNullAt(0)
+ val nullAfterA = converter.apply(row).copy()
+
+ // Dirty the buffer with a *different* non-null value, then project a null again.
+ row.update(0, new CalendarInterval(7, 9, 987654321L))
+ converter.apply(row)
+ row.setNullAt(0)
+ val nullAfterB = converter.apply(row).copy()
+
+ assert(nullAfterA.isNullAt(0) && nullAfterB.isNullAt(0))
+ assert(nullAfterA == nullAfterB,
+ "two null interval projections must be byte-identical but differed (stale payload)")
+ }
+
testBothCodegenAndInterpreted(
"null nanosecond timestamp keys are byte-identical regardless of prior rows") {
// The nanosecond timestamp types occupy a 16-byte variable-length payload. The projection
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org