Max Gekk created SPARK-57526:
--------------------------------
Summary: Add the `timestamp_nanos` function to create
nanosecond-precision timestamps from numeric nanoseconds
Key: SPARK-57526
URL: https://issues.apache.org/jira/browse/SPARK-57526
Project: Spark
Issue Type: Sub-task
Components: SQL
Affects Versions: 4.3.0
Reporter: Max Gekk
h3. Background
Part of the SPARK-56822 umbrella (nanosecond-precision timestamps). Spark
already has
{{timestamp_seconds}}, {{timestamp_millis}} and {{timestamp_micros}}, which
build a
{{TIMESTAMP}} (LTZ, microsecond precision) from a numeric count since the Unix
epoch.
There is no counterpart that produces a nanosecond-precision value.
h3. Proposal
Add a new built-in function {{timestamp_nanos(expr)}} that interprets {{expr}}
as the
number of nanoseconds since {{1970-01-01 00:00:00 UTC}} and returns a
{{TIMESTAMP_LTZ(9)}}
({{TimestampLTZNanosType(9)}}).
* Input: integral numeric ({{BIGINT}}).
* Output type: {{TIMESTAMP_LTZ(9)}}.
* Mapping to the internal pair: {{epochMicros = Math.floorDiv(n, 1000)}},
{{nanosWithinMicro = Math.floorMod(n, 1000)}}, built via
{{TimestampNanosVal.fromParts(epochMicros, nanosWithinMicro)}}.
{code:sql}
SELECT timestamp_nanos(1230219000123456789);
-- 2008-12-25 15:30:00.123456789
{code}
h3. Implementation notes
* Mirror the existing {{SecondsToTimestamp}} / {{MicrosToTimestamp}}
expressions in
{{datetimeExpressions.scala}}.
* Reuse {{TimestampNanosVal.fromParts}} and {{NANOS_PER_MICROS}} from
{{DateTimeUtils}}.
* Register in {{FunctionRegistry}} and add to the SQL function docs/examples.
* Provide both interpreted and codegen paths.
h3. Acceptance criteria
* {{timestamp_nanos}} available in SQL, Scala, Python and R function APIs.
* Returns {{TIMESTAMP_LTZ(9)}}; sub-microsecond digits preserved.
* Null-safe; handles negative (pre-epoch) inputs via floor semantics.
* Unit tests + golden files (result, type, codegen) added.
h3. Out of scope
* The inverse {{unix_nanos}} function (separate sub-task).
Parent: SPARK-56822
--
This message was sent by Atlassian Jira
(v8.20.10#820010)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]