[SPARK-57850][SQL] Return the correct fractional-second scale from EXTRACT(SECOND) on TIME#56931
[SPARK-57850][SQL] Return the correct fractional-second scale from EXTRACT(SECOND) on TIME#56931yadavay-amzn wants to merge 3 commits into
Conversation
MaxGekk
left a comment
There was a problem hiding this comment.
0 blocking, 0 non-blocking, 1 nits.
Correct, minimal, well-tested fix. The declared StaticInvoke return type and the runtime Decimal construction are changed in lockstep to DecimalType(2 + p, p), so the analyzer-declared type matches execution output; the TIMESTAMP paths are correctly left unchanged (single-precision, so a fixed scale is already exact). Tests assert both the declared DecimalType and the value across TIME(0-9), including the sub-microsecond 7-9 cases, and the time.sql golden is updated consistently.
Nits: 1 minor item (general comment, in an unchanged file):
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala:480-482: theSecondWithFractionNanosScaladoc citesSecondsOfTimeWithFractionas a fixed-scale exemplar ("The scale is fixed ... likeSecondsOfTimeWithFractiondoes for TIME values"). This PR changes that expression to a per-precision scale (DecimalType(2 + p, p)), so the cross-reference is now inaccurate for the very symbol it names. Since this PR changes the referenced behavior, consider fixing the comment here: drop thelike [[SecondsOfTimeWithFraction]] does for TIME valuesclause, or restate it as a contrast (TIME now uses a per-precision scale; the nanos timestamp type keeps a fixed scale because it is single-precision).
|
@yadavay-amzn Could you fix an outdated comment, please. |
…ce to TIME EXTRACT(SECOND) scale
|
@MaxGekk Good point - updated the SecondWithFractionNanos scaladoc: it no longer describes SecondsOfTimeWithFraction as fixed-scale. It now states the nanos TIMESTAMP path stays fixed DECIMAL(11,9) and contrasts that with EXTRACT(SECOND) on TIME, which this PR changed to the per-precision DECIMAL(2 + p, p). |
What changes were proposed in this pull request?
EXTRACT(SECOND FROM t)/date_part('SECOND', t)on aTIME(p)value returned a hardcodedDecimalType(8, 6)regardless of the TIME precision. This changes the result type toDecimalType(2 + p, p), so the fractional-second scale matches the value's precision (e.g.TIME(0)->decimal(2,0),TIME(6)->decimal(8,6)).Why are the changes needed?
The scale was always 6, so
EXTRACT(SECOND)on e.g.TIME(0)reported an incorrectdecimal(8,6)type with spurious fractional digits. The scale should reflect the value's declared precision.Does this PR introduce any user-facing change?
Yes.
EXTRACT(SECOND FROM <TIME(p)>)/date_part('SECOND', <TIME(p)>)now returnsDecimalType(2 + p, p)instead of alwaysDecimalType(8, 6). TIMESTAMP behavior is unchanged.How was this patch tested?
Unit tests in
TimeExpressionsSuiteasserting both the resultDecimalTypeand the value across TIME precisions 0-6; thetime.sqlSQLQueryTestSuitegolden was updated accordingly.Was this patch authored or co-authored using generative AI tooling?
Authored with assistance by Claude Opus 4.8.