← Back to Pull requests

[SPARK-60542][SQL] Support nanosecond-precision timestamps in percentile, median, percentile_cont and percentile_disc #59781

Closed
unknown closed this pull request without merging on Oct 28, 2026 08:42 UTC
View on GitHub
unknown unknown commented on Oct 25, 2026 13:24 UTC
Author

What changes were proposed in this pull request?

This PR makes the exact percentile aggregates percentile, median, percentile_cont and percentile_disc accept nanosecond-precision timestamps (TIMESTAMP_NTZ(p) / TIMESTAMP_LTZ(p), p in [7, 9]) and return the input type, family and precision. It's part of the nanosecond timestamp SPIP (SPARK-56822).

  • AnyTimestampNanoType is appended last to the input TypeCollection. A nanos input matches it directly, so no cast is inserted. Because it's last, the implicit-cast fallback for every other input type picks the same member as before.

  • percentile_disc returns an input value, so the result is exact.

  • percentile, median and percentile_cont interpolate lower + fraction (higher - lower) exactly in epoch nanoseconds using java.math.BigDecimal, then floor the result to the input precision (100 ns for p=7, 10 ns for p=8). A double can't do this: epoch nanoseconds need about 61 bits, and the 0001..9999 span overflows a long. The position percentage (count - 1) is computed exactly too, because a double can land just below a whole number (90 * 0.7 is 62.99999999999999) and flooring would turn that into a whole step. The numeric, interval and TIME paths are unchanged.

  • percentile_cont over a nanos timestamp is not pushed down to DS V2 sources, which would interpolate with their own rounding. percentile_disc still is, since it returns an input value.
  • Design decision for reviewers: an interpolated value that falls between two grid points is floored toward -inf. For example, the median of .000000001 and .000000002 at p=9 is .000000001. Flooring matches how nanos values are already stored at a lower precision, and it keeps the result between the two neighboring values for both ascending and descending order. The TIME and interval paths truncate toward zero with .toLong. That's the same thing for TIME, which is never negative, and differs only for pre-epoch timestamps. Round-half-even would also work, but it would differ from the floor that casts to a lower nanos precision already apply.

    Why are the changes needed?

    Since SPARK-60335 stopped the implicit narrowing, these functions reject a nanos timestamp with DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE in both ANSI modes; before that, ANSI silently cast it to TIME(6). The SPIP asks for the supported functions to work at nanosecond precision, and the exact percentile family can do so without losing any digits.

    Does this PR introduce _any_ user-facing change?

    Yes, for the nanosecond timestamp preview types only (spark.sql.timestampNanosTypes.enabled). These functions now return a nanos timestamp of the input type instead of a type-mismatch error. The UNEXPECTED_INPUT_TYPE message for other unsupported inputs now also lists the nanos types. Microsecond timestamps behave as before.

    How was this patch tested?

  • PercentileSuite: result types, exact interpolation (sub-microsecond, pre-epoch, the 0001..9999 range, p=7/8 flooring, non-dyadic percentages, ascending vs descending), percentile_disc (current and legacy calculation), and serialized buffer merge.

  • TimestampNanosFunctionsAnsiOnSuite / AnsiOffSuite: SQL and Column API results and types for NTZ/LTZ at p=7/8/9 over multiple partitions. TypeCoercionSuite / AnsiTypeCoercionSuite: nanos input is not cast and micro timestamps are never widened to nanos. DataSourceV2StrategySuite: no pushdown of percentile_cont over nanos.

  • timestamp-ntz-nanos.sql / timestamp-ltz-nanos.sql golden files, including window, frequency and ANSI-off cases; the four functions move out of the SPARK-60335 rejection block.
  • Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Isaac

    Diff: 475 lines (+356 / -119) across 5 files Reviewer: stevomitric Review duration: 4037m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.46) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 475 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #59781"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.1% · Safe to merge
    18.1%
    Repository