[SPARK-60542][SQL] Support nanosecond-precision timestamps in percentile, median, percentile_cont and percentile_disc #59781
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
stevomitric
What changes were proposed in this pull request?
This PR makes the exact percentile aggregates
percentile,median,percentile_contandpercentile_discaccept 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).AnyTimestampNanoTypeis appended last to the inputTypeCollection. 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_discreturns an input value, so the result is exact.percentile,medianandpercentile_continterpolatelower + fraction (higher - lower)exactly in epoch nanoseconds usingjava.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 positionpercentage (count - 1)is computed exactly too, because a double can land just below a whole number (90 * 0.7is62.99999999999999) and flooring would turn that into a whole step. The numeric, interval and TIME paths are unchanged.percentile_contover a nanos timestamp is not pushed down to DS V2 sources, which would interpolate with their own rounding.percentile_discstill 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
.000000001and.000000002at 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_TYPEin both ANSI modes; before that, ANSI silently cast it toTIME(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. TheUNEXPECTED_INPUT_TYPEmessage 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 ofpercentile_contover nanos.timestamp-ntz-nanos.sql/timestamp-ltz-nanos.sqlgolden 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