Skip to content

[SPARK-57501][SQL] Add +/- ANSI day-time interval support for TIMESTAMP_NTZ/LTZ(p)#56562

Open
MaxGekk wants to merge 8 commits into
apache:masterfrom
MaxGekk:nanos-ansi-intervals
Open

[SPARK-57501][SQL] Add +/- ANSI day-time interval support for TIMESTAMP_NTZ/LTZ(p)#56562
MaxGekk wants to merge 8 commits into
apache:masterfrom
MaxGekk:nanos-ansi-intervals

Conversation

@MaxGekk

@MaxGekk MaxGekk commented Jun 17, 2026

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

This PR adds support for TIMESTAMP_NTZ(p) / TIMESTAMP_LTZ(p) (p in [7, 9]) in +/- ANSI day-time interval arithmetic.

Concretely:

  • Extends TimestampAddInterval input typing to accept nanos timestamp types alongside existing microsecond timestamp types.
  • Adds nanos-aware execution/codegen paths for ANSI day-time interval arithmetic that preserve nanosWithinMicro while applying interval arithmetic on epoch micros.
  • Keeps nanos timestamp arithmetic scoped to ANSI day-time intervals: CalendarIntervalType with nanos timestamps now reports a type mismatch.
  • Adds utility helpers in DateTimeUtils to keep nanos remainder unchanged during timestamp +/- day-time interval operations.
  • Adds catalyst and SQL test coverage for NTZ/LTZ nanos interval arithmetic parity.
  • Regenerates impacted SQL golden files, including analyzer outputs where requiredType now correctly reports both micro and nanos timestamp families.

A small follow-up commit in this branch removes temporary helper abstractions (AnyTimestampFamilyType / AnyTimestampFamilyTypeExpression) and keeps the change scoped without widening SubtractTimestamps handling.

Why are the changes needed?

Spark already supports timestamp +/- ANSI day-time interval for microsecond timestamp families, but nanos timestamp families (TIMESTAMP_NTZ(p) / TIMESTAMP_LTZ(p), p in [7, 9]) lacked parity. This left valid datetime arithmetic unsupported for nanos types.

These changes close that parity gap while preserving nanos precision semantics and existing LTZ/NTZ timezone behavior.

Does this PR introduce any user-facing change?

Yes.

TIMESTAMP_NTZ(p) / TIMESTAMP_LTZ(p) (p in [7, 9]) now support +/- ANSI day-time interval arithmetic.

For nanos timestamps, CalendarIntervalType is rejected with a type mismatch and users should use ANSI day-time intervals.

Examples:

  • TIMESTAMP_NTZ '2020-01-02 03:04:05.123456789' + INTERVAL '2 00:03:00.000456' DAY TO SECOND
  • TIMESTAMP_LTZ '2020-01-02 03:04:05.123456789 UTC' - INTERVAL '1 00:04:00.000321' DAY TO SECOND

How was this patch tested?

  • build/sbt 'catalyst/testOnly org.apache.spark.sql.catalyst.expressions.DateExpressionsSuite'
  • SPARK_GENERATE_GOLDEN_FILES=1 build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z "timestamp-ntz-nanos"'
  • SPARK_GENERATE_GOLDEN_FILES=1 build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z "timestamp-ltz-nanos"'
  • SPARK_GENERATE_GOLDEN_FILES=1 build/sbt 'sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z "interval"'
  • ./dev/scalastyle
  • ./build/mvn scalafmt:format -Dscalafmt.skip=false -Dscalafmt.validateOnly=false -Dscalafmt.changedOnly=false -pl sql/api -pl sql/connect/common -pl sql/connect/server -pl sql/connect/shims -pl sql/connect/client/jvm

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Cursor Codex 5.3

MaxGekk added 8 commits June 17, 2026 10:04
…stamp nanos types

Enable TIMESTAMP_NTZ(p)/TIMESTAMP_LTZ(p) (p in [7, 9]) to participate in +/- ANSI day-time interval arithmetic with analyzer and expression parity to microsecond timestamps. Preserve nanosWithinMicro during interval arithmetic and add catalyst/SQL coverage with regenerated golden files.
Remove AnyTimestampFamilyType and AnyTimestampFamilyTypeExpression and reuse existing timestamp type collections/matchers in the nanos ANSI day-time interval path. Keep subtract-timestamps resolver/type checks unchanged to keep this PR scoped to interval arithmetic parity.
…stamp arithmetic

Restrict TIMESTAMP_NTZ/LTZ(p) interval arithmetic in TimestampAddInterval to ANSI day-time intervals and return an analysis-time type mismatch for CalendarIntervalType inputs. Add catalyst checks that nanos timestamp operands reject calendar intervals.
…ches

Drop the remaining CalendarIntervalType nanos eval/codegen branches in TimestampAddInterval now that nanos + calendar interval is rejected during analysis. Keep runtime/codegen paths aligned with the enforced type-check behavior.
…ranch

Collapse the CalendarIntervalType timestampAddInterval call to a single line to keep this hunk closer to the original code layout while preserving behavior.
…helper

The nanos calendar-interval eval/codegen branches were dropped once nanos +
calendar interval became a type-check error, leaving DateTimeUtils.timestampNanosAddInterval
without any callers. Remove the dead helper.

Co-authored-by: Isaac
…mestamp adds

Keep TimestampAddInterval's new nanos support while restoring the historical requiredType wording for non-timestamp left operands. This avoids unrelated analyzer golden churn (for example interval.sql_analyzer_test) while still rejecting nanos + calendar intervals.
…rval add errors

Make TimestampAddInterval type-check errors report both micro and nanos timestamp families for invalid left operands. Regenerate interval analyzer goldens to match the corrected requiredType text in ansi and nonansi variants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant