← Back to Pull requests

[SPARK-60314][SQL] Make `Literal.sql` preserve the precision of `TIME(7)`-`TIME(9)` literals #59571

Closed
unknown closed this pull request without merging on Oct 19, 2026 20:35 UTC
View on GitHub
unknown unknown commented on Oct 17, 2026 03:29 UTC
Author

What changes were proposed in this pull request?

This PR makes Literal.sql render the fractional seconds of a TIME(p) literal with p in [7, 9] with exactly p digits. Like the nanosecond-precision timestamp literals (SPARK-57339, #56494), it truncates the value to p digits before formatting and pads back the trailing zeros with the existing private padToNanosPrecision helper. The truncation matters only for a literal whose value has more fractional digits than p, e.g. Literal.create(LocalTime.of(12, 0, 0, 123456789), TimeType(7)).

TIME(0)-TIME(6) literals and Literal.toString (display-only, trailing zeros stripped) are unchanged. TIME(0)-TIME(5) literals and TIME(6) literals carrying sub-microsecond digits (e.g. lit(LocalTime.of(23, 59, 59, 999999999))) do not round-trip through Literal.sql either, but they are out of the scope of this PR and will be handled in a new JIRA issue.

Why are the changes needed?

Literal.sql renders a TIME literal through FractionTimeFormatter, which strips trailing fractional zeros. Since SPARK-57551 (#56622), the parser derives TIME(7)-TIME(9) from the number of fractional digits of a TIME literal and gives TIME(6) to a literal with 6 or fewer digits. So a TIME(9) literal whose value ends with zeros, e.g. 12:00:00.100000000, is rendered as TIME '12:00:00.1' and parses back as TIME(6).

  • https://github.com/apache/spark/pull/56622
  • This causes wrong query results. By default (spark.sql.legacy.parameterSubstitution.constantsOnly=false), spark.sql(sqlText, args) and EXECUTE IMMEDIATE ... USING substitute each argument into the SQL text as Literal.sql and parse the result. So a TIME(9) argument becomes TIME(6), and a string compared or combined with it is cast to TIME(6), i.e. truncated to microseconds. For example, with val t = expr("TIME '12:00:00.100000000'"):

    | Query | Before | After |
    | --- | --- | --- |
    | spark.sql("SELECT :t = '12:00:00.100000001'", Map("t" -> t)) | true | false |
    | spark.sql("SELECT ? < '12:00:00.100000001'", Array(t)) | false | true |
    | spark.sql("SELECT s FROM VALUES ('12:00:00.100000001'), ('12:00:00.1') AS t(s) WHERE s = :t", Map("t" -> t)) | 12:00:00.100000001, 12:00:00.1 | 12:00:00.1 |
    | EXECUTE IMMEDIATE 'SELECT ? = \'12:00:00.100000001\'' USING TIME '12:00:00.100000000' | true | false |
    | spark.sql("SELECT CASE WHEN id = 0 THEN :t ELSE '12:00:00.123456789' END FROM range(1, 2)", Map("t" -> t)) | 12:00:00.123456 (TIME(6)) | 12:00:00.123456789 (TIME(9)) |

    In addition, Literal.sql is used for the EXISTS_DEFAULT column metadata, the auto-generated column names, and the values in error messages. EXISTS_DEFAULT keeps the value because it is cast back to the column type, but the column names and the error messages drop the digits written in the literal.

    Does this PR introduce _any_ user-facing change?

    Yes. This fixes the wrong results above. In addition, the auto-generated column names and the values in error messages keep all p fractional digits of a TIME(7)-TIME(9) literal.

    spark.sql("SELECT TIME '12:00:00.100000000'").schema.simpleString
    spark.sql("SELECT CAST(TIME '23:59:59.000000000' AS TINYINT)").collect()
    

    BEFORE

    struct<TIME '12:00:00.1':time(9)>
    [CAST_OVERFLOW] The value TIME '23:59:59' of the type "TIME(9)" cannot be cast to "TINYINT" due to an overflow. ...
    

    AFTER

    struct<TIME '12:00:00.100000000':time(9)>
    [CAST_OVERFLOW] The value TIME '23:59:59.000000000' of the type "TIME(9)" cannot be cast to "TINYINT" due to an overflow. ...
    

    The behavior changes only for TIME(7)-TIME(9) literals whose values end with fractional zeros or have more fractional digits than the precision. This is not a user-facing change compared to the released Apache Spark versions because TIME(7)-TIME(9) was added by SPARK-57551, which exists only in the unreleased master, branch-4.x, and branch-4.3 (Apache Spark 4.3.0 release candidates, v4.3.0-rc1 (2026-08-31) and v4.3.0-rc2 (2026-10-12)).

    How was this patch tested?

    Pass the CIs with the newly added test cases.

  • LiteralExpressionSuite checks the Literal.sql round trip of TIME(7), TIME(8), and TIME(9) literals with trailing fractional zeros, a whole second, and no trailing zeros, and the truncation of a value with more fractional digits than the precision.

  • ParametersSuite checks the comparison and filter results above with named and positional arguments and EXECUTE IMMEDIATE, and the result types of ARRAY, STRUCT, and MAP arguments of TIME(9).
  • Without this fix, they fail like the following.

    [info] - SPARK-60314: TimeType(7-9) sql keeps the precision on a round trip *** FAILED ***
    [info]   TimeType(6) did not equal TimeType(7) (LiteralExpressionSuite.scala:729)
    

    [info] - SPARK-60314: TIME(7)-TIME(9) literal argument keeps its precision *** FAILED ***
    [info]   TimeType(6) did not equal TimeType(9) (ParametersSuite.scala:533)
    

    The existing golden files are unchanged because all TIME(7)-TIME(9) literals there have no trailing fractional zeros.

    Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Opus 5.5

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: cloud-fan Review duration: 3905m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    56%
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #59571"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 22.5% · Safe to merge
    22.5%
    Repository