[WIP][SPARK-57832][SQL] Support timestamp - timestamp subtraction for nanosecond-precision timestamps - #57680
Open
stevomitric wants to merge 1 commit into
Open
Conversation
…second-precision timestamps ### What changes were proposed in this pull request? `SubtractTimestamps` (the `timestamp - timestamp` operator) previously only accepted the microsecond timestamp types (`AnyTimestampType`). This PR extends it to also accept the nanosecond-precision timestamp types (`TIMESTAMP_NTZ(p)` / `TIMESTAMP_LTZ(p)`, `p` in [7, 9]). The difference is always reported on the microsecond grid, since the result is a `DayTimeIntervalType` (which has microsecond resolution). Each operand therefore contributes only its `epochMicros`; the sub-microsecond remainder is truncated. This keeps `ts_nanos - ts_nanos` numerically identical to `ts_nanos::timestamp - ts_nanos::timestamp`. Concretely: - `SubtractTimestamps.inputTypes` widens to `TypeCollection(AnyTimestampType, AnyTimestampNanoType)` for both operands, and `nullSafeEval` / `doGenCode` reduce a `TimestampNanosVal` operand to its `epochMicros` before subtracting. - `BinaryArithmeticWithDatetimeResolver` recognizes a nanos operand on either side of a `Subtract` and routes it to `SubtractTimestamps`. - The `DateTimeOperations` / `AnsiDateTimeOperations` type-coercion rules unify mixed operands (DATE vs timestamp, or two timestamps differing in precision or time-zone family). A cross-family pair unifies in the NTZ family (mirroring the microsecond `TIMESTAMP - TIMESTAMP_NTZ` precedent); a same-family pair keeps its family so an LTZ pair still subtracts in the session time zone. For pure-microsecond inputs the behavior is unchanged. ### Why are the changes needed? Sub-task of SPARK-56822 (nanosecond-precision timestamp support). Without this, subtracting two nanosecond timestamps fails analysis. ### Does this PR introduce _any_ user-facing change? Yes. `timestamp_nanos - timestamp_nanos` (and mixed micro/nanos, and nanos/DATE) now produces a microsecond-grid `DayTimeIntervalType` instead of failing analysis. ### How was this patch tested? - New unit test in `DateExpressionsSuite` covering NTZ/LTZ nanos operands, sub-microsecond truncation, pre-epoch values, NULLs, and interpreted/codegen consistency. - New coercion cases in `TypeCoercionSuite` / `AnsiTypeCoercionSuite`. - New golden SQL cases in `timestamp-ntz-nanos.sql` / `timestamp-ltz-nanos.sql`. Co-authored-by: Isaac
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changes were proposed in this pull request?
SubtractTimestamps(thetimestamp - timestampoperator) previously only accepted the microsecond timestamp types (AnyTimestampType). This PR extends it to also accept the nanosecond-precision timestamp types (TIMESTAMP_NTZ(p)/TIMESTAMP_LTZ(p),pin [7, 9]).The difference is always reported on the microsecond grid, since the result is a
DayTimeIntervalType(which has microsecond resolution). Each operand therefore contributes only itsepochMicros; the sub-microsecond remainder is truncated. This keepsts_nanos - ts_nanosnumerically identical tots_nanos::timestamp - ts_nanos::timestamp.Concretely:
SubtractTimestamps.inputTypeswidens toTypeCollection(AnyTimestampType, AnyTimestampNanoType)for both operands, andnullSafeEval/doGenCodereduce aTimestampNanosValoperand to itsepochMicrosbefore subtracting.BinaryArithmeticWithDatetimeResolverrecognizes a nanos operand on either side of aSubtractand routes it toSubtractTimestamps.DateTimeOperations/AnsiDateTimeOperationstype-coercion rules unify mixed operands (DATE vs timestamp, or two timestamps differing in precision or time-zone family). A cross-family pair unifies in the NTZ family (mirroring the microsecondTIMESTAMP - TIMESTAMP_NTZprecedent); a same-family pair keeps its family so an LTZ pair still subtracts in the session time zone.For pure-microsecond inputs the behavior is unchanged.
Why are the changes needed?
Sub-task of SPARK-56822 (nanosecond-precision timestamp support). Without this, subtracting two nanosecond timestamps fails analysis.
Does this PR introduce any user-facing change?
Yes.
timestamp_nanos - timestamp_nanos(and mixed micro/nanos, and nanos/DATE) now produces a microsecond-gridDayTimeIntervalTypeinstead of failing analysis.How was this patch tested?
DateExpressionsSuitecovering NTZ/LTZ nanos operands, sub-microsecond truncation, pre-epoch values, NULLs, and interpreted/codegen consistency.TypeCoercionSuite/AnsiTypeCoercionSuite.timestamp-ntz-nanos.sql/timestamp-ltz-nanos.sql.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 4.8)