-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-56981][SQL] Add physical representation and UnsafeRow support for nanosecond timestamps #56059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxGekk
wants to merge
9
commits into
apache:master
Choose a base branch
from
MaxGekk:nanos-in-rows
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[SPARK-56981][SQL] Add physical representation and UnsafeRow support for nanosecond timestamps #56059
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f95e5c6
[SPARK-56981][SQL] Add physical representation for nanosecond timestamps
MaxGekk d8092dd
[SPARK-56981][SQL][FOLLOWUP] Fix checkstyle line length in TimestampN…
MaxGekk 49effdf
[SPARK-56981][SQL][FOLLOWUP] Fix null unsafe projection for nanos tim…
MaxGekk f3897bc
[SPARK-56981][SQL][FOLLOWUP] Read nanosWithinMicro with getLong for e…
MaxGekk f2c048f
[SPARK-56981][SQL][FOLLOWUP] Add explicit canSupport cases for nanos …
MaxGekk b7bd93d
[SPARK-56981][SQL][FOLLOWUP] Add Literal.default for nanos timestamp …
MaxGekk 86ef651
[SPARK-56981][SQL][FOLLOWUP] Document nanos timestamp physical layout…
MaxGekk 3f18668
[SPARK-56981][SQL][FOLLOWUP] Fix line length in nanos timestamp docum…
MaxGekk ba83fe6
[SPARK-56981][SQL][TEST] Clarify null unsafe projection test for nano…
MaxGekk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
common/unsafe/src/main/java/org/apache/spark/unsafe/types/TimestampLTZNanos.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.unsafe.types; | ||
|
|
||
| import org.apache.spark.annotation.Unstable; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Physical representation of {@code TIMESTAMP_LTZ(p)} with nanosecond-capable fractional precision | ||
| * ({@code p} in [7, 9]). Analogous to microsecond {@code TimestampType}, which stores a single | ||
| * {@code long} epoch micros value at UTC+00:00, but with sub-microsecond resolution. | ||
| * | ||
| * <p>Values are stored as two components: | ||
| * <ul> | ||
| * <li>{@link #epochMicros} - microseconds since the Unix epoch at UTC+00:00 (same unit as | ||
| * {@code TimestampType}),</li> | ||
| * <li>{@link #nanosWithinMicro} - additional nanoseconds within that microsecond, in [0, 999]. | ||
| * The SQL fractional-second precision {@code p} applies when converting to and from external | ||
| * representations; the physical value always retains up to three sub-micro digits.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Logical row-size estimation uses 10 bytes (8 + 2). In {@code UnsafeRow}, values are stored in | ||
| * the variable-length region using a 16-byte payload (see | ||
| * {@link org.apache.spark.sql.catalyst.expressions.TimestampNanosRowValues}), the same pattern as | ||
| * {@link CalendarInterval}. | ||
| * | ||
| * <p>NTZ and LTZ nanosecond timestamps share this composite layout at the row layer; time-zone | ||
| * semantics are enforced in the SQL and conversion layers, not in this class. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| @Unstable | ||
| public final class TimestampLTZNanos implements Serializable { | ||
| /** Size of the {@code UnsafeRow} variable-length payload for this type (two 8-byte words). */ | ||
| public static final int SIZE_IN_BYTES = 16; | ||
|
|
||
| /** Microseconds since the Unix epoch at UTC+00:00. */ | ||
| public final long epochMicros; | ||
| /** Nanoseconds within {@link #epochMicros}, in [0, 999]. */ | ||
| public final short nanosWithinMicro; | ||
|
|
||
| /** | ||
| * @param epochMicros microseconds since the Unix epoch at UTC+00:00 | ||
| * @param nanosWithinMicro nanoseconds within {@code epochMicros}, must be in [0, 999] | ||
| */ | ||
| public TimestampLTZNanos(long epochMicros, short nanosWithinMicro) { | ||
| TimestampNanosUtils.validateNanosWithinMicro(nanosWithinMicro); | ||
| this.epochMicros = epochMicros; | ||
| this.nanosWithinMicro = nanosWithinMicro; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| TimestampLTZNanos that = (TimestampLTZNanos) o; | ||
| return epochMicros == that.epochMicros && nanosWithinMicro == that.nanosWithinMicro; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(epochMicros, nanosWithinMicro); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "TimestampLTZNanos(" + epochMicros + ", " + nanosWithinMicro + ")"; | ||
| } | ||
| } | ||
86 changes: 86 additions & 0 deletions
86
common/unsafe/src/main/java/org/apache/spark/unsafe/types/TimestampNTZNanos.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.unsafe.types; | ||
|
|
||
| import org.apache.spark.annotation.Unstable; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
| * Physical representation of {@code TIMESTAMP_NTZ(p)} with nanosecond-capable fractional precision | ||
| * ({@code p} in [7, 9]). Analogous to microsecond {@code TimestampNTZType}, which stores a single | ||
| * {@code long} epoch micros value, but with sub-microsecond resolution. | ||
| * | ||
| * <p>Values are stored as two components: | ||
| * <ul> | ||
| * <li>{@link #epochMicros} - microseconds since the Unix epoch in the proleptic Gregorian | ||
| * calendar (same unit as {@code TimestampNTZType}),</li> | ||
| * <li>{@link #nanosWithinMicro} - additional nanoseconds within that microsecond, in [0, 999]. | ||
| * The SQL fractional-second precision {@code p} applies when converting to and from external | ||
| * representations; the physical value always retains up to three sub-micro digits.</li> | ||
| * </ul> | ||
| * | ||
| * <p>Logical row-size estimation uses 10 bytes (8 + 2). In {@code UnsafeRow}, values are stored in | ||
| * the variable-length region using a 16-byte payload (see | ||
| * {@link org.apache.spark.sql.catalyst.expressions.TimestampNanosRowValues}), the same pattern as | ||
| * {@link CalendarInterval}. | ||
| * | ||
| * <p>NTZ and LTZ nanosecond timestamps share this composite layout at the row layer; time-zone | ||
| * semantics are enforced in the SQL and conversion layers, not in this class. | ||
| * | ||
| * @since 4.2.0 | ||
| */ | ||
| @Unstable | ||
| public final class TimestampNTZNanos implements Serializable { | ||
| /** Size of the {@code UnsafeRow} variable-length payload for this type (two 8-byte words). */ | ||
| public static final int SIZE_IN_BYTES = 16; | ||
|
|
||
| /** Microseconds since the Unix epoch. */ | ||
| public final long epochMicros; | ||
| /** Nanoseconds within {@link #epochMicros}, in [0, 999]. */ | ||
| public final short nanosWithinMicro; | ||
|
|
||
| /** | ||
| * @param epochMicros microseconds since the Unix epoch | ||
| * @param nanosWithinMicro nanoseconds within {@code epochMicros}, must be in [0, 999] | ||
| */ | ||
| public TimestampNTZNanos(long epochMicros, short nanosWithinMicro) { | ||
| TimestampNanosUtils.validateNanosWithinMicro(nanosWithinMicro); | ||
| this.epochMicros = epochMicros; | ||
| this.nanosWithinMicro = nanosWithinMicro; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| TimestampNTZNanos that = (TimestampNTZNanos) o; | ||
| return epochMicros == that.epochMicros && nanosWithinMicro == that.nanosWithinMicro; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(epochMicros, nanosWithinMicro); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "TimestampNTZNanos(" + epochMicros + ", " + nanosWithinMicro + ")"; | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
common/unsafe/src/main/java/org/apache/spark/unsafe/types/TimestampNanosUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.unsafe.types; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import org.apache.spark.SparkIllegalArgumentException; | ||
|
|
||
| /** | ||
| * Internal helpers shared by {@link TimestampNTZNanos} and {@link TimestampLTZNanos}. | ||
| */ | ||
| final class TimestampNanosUtils { | ||
| /** Maximum valid value for {@code nanosWithinMicro} (three sub-micro decimal digits). */ | ||
| static final int MAX_NANOS_WITHIN_MICRO = 999; | ||
|
|
||
| private TimestampNanosUtils() {} | ||
|
|
||
| static void validateNanosWithinMicro(short nanosWithinMicro) { | ||
| if (nanosWithinMicro < 0 || nanosWithinMicro > MAX_NANOS_WITHIN_MICRO) { | ||
| throw new SparkIllegalArgumentException( | ||
| "INTERNAL_ERROR", | ||
| Map.of( | ||
| "message", | ||
| "nanosWithinMicro must be in [0, " + MAX_NANOS_WITHIN_MICRO + "], got: " | ||
| + nanosWithinMicro)); | ||
| } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
common/unsafe/src/test/java/org/apache/spark/unsafe/types/TimestampNanosSuite.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.unsafe.types; | ||
|
|
||
| import org.apache.spark.SparkIllegalArgumentException; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class TimestampNanosSuite { | ||
|
|
||
| @Test | ||
| public void timestampNTZNanosEqualsAndHashCode() { | ||
| TimestampNTZNanos t1 = new TimestampNTZNanos(1000L, (short) 100); | ||
| TimestampNTZNanos t2 = new TimestampNTZNanos(1001L, (short) 100); | ||
| TimestampNTZNanos t3 = new TimestampNTZNanos(1000L, (short) 101); | ||
| TimestampNTZNanos t4 = new TimestampNTZNanos(1000L, (short) 100); | ||
|
|
||
| assertNotEquals(t1, t2); | ||
| assertNotEquals(t1, t3); | ||
| assertEquals(t1, t4); | ||
| assertEquals(t1.hashCode(), t4.hashCode()); | ||
| } | ||
|
|
||
| @Test | ||
| public void timestampLTZNanosEqualsAndHashCode() { | ||
| TimestampLTZNanos t1 = new TimestampLTZNanos(2000L, (short) 0); | ||
| TimestampLTZNanos t2 = new TimestampLTZNanos(2000L, (short) 1); | ||
| TimestampLTZNanos t3 = new TimestampLTZNanos(2000L, (short) 0); | ||
|
|
||
| assertNotEquals(t1, t2); | ||
| assertEquals(t1, t3); | ||
| } | ||
|
|
||
| @Test | ||
| public void invalidNanosWithinMicroNTZ() { | ||
| assertThrows(SparkIllegalArgumentException.class, | ||
| () -> new TimestampNTZNanos(0L, (short) -1)); | ||
| assertThrows(SparkIllegalArgumentException.class, | ||
| () -> new TimestampNTZNanos(0L, (short) 1000)); | ||
| } | ||
|
|
||
| @Test | ||
| public void invalidNanosWithinMicroLTZ() { | ||
| assertThrows(SparkIllegalArgumentException.class, | ||
| () -> new TimestampLTZNanos(0L, (short) -1)); | ||
| assertThrows(SparkIllegalArgumentException.class, | ||
| () -> new TimestampLTZNanos(0L, (short) 1000)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is physical data class, do we really need to have both
TimestampLTZNanosandTimestampNTZNanos? We just need a container for 10 bytes.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those classes incapsulate not only physical data but might have specific methods in the future. For instance:
LTZ-specific (instant semantics — globally unambiguous, no session zone needed):
NTZ-specific (wall-clock semantics — no zone):
At the moment one class is enough. @cloud-fan Would you prefer one class in any case?