You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "MaxGekk (via GitHub)" <gi...@apache.org> on 2026/06/08 09:17:25 UTC
[PR] [SPARK-57315][SQL] Support HOUR, MINUTE and SECOND functions over nanosecond-precision timestamps [spark]
MaxGekk opened a new pull request, #56366:
URL: https://github.com/apache/spark/pull/56366
### What changes were proposed in this pull request?
This PR makes the time-of-day extraction functions `hour()`, `minute()` and `second()` work over the nanosecond-precision timestamp types `TIMESTAMP_NTZ(p)` / `TIMESTAMP_LTZ(p)` (with `p` in `[7, 9]`), whose physical value is `TimestampNanosVal(epochMicros: Long, nanosWithinMicro: Short)`.
These functions are implemented by the `GetTimeField` expressions (`Hour`, `Minute`, `Second`), whose `inputTypes` is `AnyTimestampType`, which only accepts the microsecond `TimestampType` and `TimestampNTZType`. As a result, calling them on a nanosecond timestamp previously failed analysis.
Since the result is an integer field that depends only on `epochMicros` (the sub-microsecond digits never affect hour/minute/second), this PR adds a dedicated analyzer rule `ResolveTimestampNanosExpressions` (modeled on `ResolveBinaryArithmetic`) that casts a resolved `Hour`/`Minute`/`Second` child of a nanosecond type down to the matching microsecond type and reuses the existing expressions and `DateTimeUtils` logic:
- `TimestampNTZNanosType(p)` -> `TimestampNTZType` (UTC / wall-clock extraction)
- `TimestampLTZNanosType(p)` -> `TimestampType` (session-zone extraction)
The cast (already available, SPARK-57293) keeps `epochMicros` and drops `nanosWithinMicro`, which is lossless for these integer results. A rule is preferred over a `TypeCoercion` rule so the behavioral change stays scoped to these functions rather than every `AnyTimestampType` expression. The rule is named generically so future nanos-aware expressions can be added as additional `case` branches.
`SecondWithFraction` (the `extract(SECOND)` path returning `DECIMAL(8, 6)`) is intentionally excluded because its result depends on the sub-microsecond digits.
### Why are the changes needed?
To make the standard datetime extraction functions usable with the new nanosecond-precision timestamp types being added under SPARK-56822 (SPIP: Timestamps with nanosecond precision). Without this, `hour`/`minute`/`second` over `TIMESTAMP_NTZ(p)` / `TIMESTAMP_LTZ(p)` fail analysis.
### Does this PR introduce _any_ user-facing change?
Yes, but only for the in-progress, preview-gated nanosecond timestamp types (guarded by `spark.sql.timestampNanosTypes.enabled`). With the flag enabled, `hour()`, `minute()` and `second()` now accept `TIMESTAMP_NTZ(p)` / `TIMESTAMP_LTZ(p)` values, e.g.:
```
SELECT hour(CAST('2020-01-01 13:24:35.123456789' AS timestamp_ntz(9))); -- 13
```
There is no change for the already-released microsecond timestamp types.
### How was this patch tested?
- New unit suite `ResolveTimestampNanosExpressionsSuite` covering the rewrite for `Hour`/`Minute`/`Second` over NTZ/LTZ nanos inputs, plus negative cases (microsecond inputs and `SecondWithFraction` are left unchanged).
- Added end-to-end golden-file checks to `timestamp-ntz-nanos.sql` and `timestamp-ltz-nanos.sql` (including non-zero sub-microsecond digits and NULL), and regenerated the result/analyzer-result golden files.
- Ran scalafmt and `./dev/scalastyle`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor (Claude Opus 4.8)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [WIP][SPARK-57315][SQL] Support HOUR, MINUTE and SECOND functions over nanosecond-precision timestamps [spark]
Posted by "acruise (via GitHub)" <gi...@apache.org>.
acruise commented on code in PR #56366:
URL: https://github.com/apache/spark/pull/56366#discussion_r3382322853
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -643,6 +644,35 @@ class Analyzer(
}
}
+ /**
+ * Enables expressions that only accept the microsecond timestamp types to operate on the
+ * nanosecond-precision timestamp types (TIMESTAMP_NTZ(p) / TIMESTAMP_LTZ(p)) by casting the
+ * nanosecond child down to the matching microsecond type and reusing the existing expression.
+ *
+ * The cast keeps `epochMicros` and drops the sub-microsecond digits, so it is only applied to
+ * expressions whose result does not depend on those digits. Currently this covers the integer
+ * time-of-day extractors `Hour`, `Minute` and `Second`. `SecondWithFraction` (the
+ * `extract(SECOND)` path returning `DECIMAL(8, 6)`) is intentionally excluded because its result
+ * does depend on the sub-microsecond digits.
+ */
+ object ResolveTimestampNanosExpressions extends Rule[LogicalPlan] {
+ // Maps a nanosecond-precision timestamp type to the matching microsecond timestamp type.
+ private def microTimestampType(dt: DataType): Option[DataType] = dt match {
+ case _: TimestampNTZNanosType => Some(TimestampNTZType)
+ case _: TimestampLTZNanosType => Some(TimestampType)
+ case _ => None
+ }
+
+ override def apply(plan: LogicalPlan): LogicalPlan =
+ plan.resolveExpressionsUpWithPruning(_.containsPattern(GET_TIME_FIELD), ruleId) {
+ case e @ (_: Hour | _: Minute | _: Second) if e.childrenResolved =>
+ val child = e.children.head
+ microTimestampType(child.dataType)
Review Comment:
nit: this allocates `Option` instances needlessly considering it's the only (apparent) callsite but presumably this isn't in any fast path so it probably doesn't matter
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [WIP][SPARK-57315][SQL] Support HOUR, MINUTE and SECOND functions over nanosecond-precision timestamps [spark]
Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk closed pull request #56366: [WIP][SPARK-57315][SQL] Support HOUR, MINUTE and SECOND functions over nanosecond-precision timestamps
URL: https://github.com/apache/spark/pull/56366
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org