[SPARK-57850][SQL] Return the correct fractional-second scale from EXTRACT(SECOND) on TIME#56931
Open
yadavay-amzn wants to merge 2 commits into
Open
[SPARK-57850][SQL] Return the correct fractional-second scale from EXTRACT(SECOND) on TIME#56931yadavay-amzn wants to merge 2 commits into
yadavay-amzn wants to merge 2 commits into
Conversation
MaxGekk
approved these changes
Jul 2, 2026
MaxGekk
left a comment
Member
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.