[SPARK-60314][SQL] Make `Literal.sql` preserve the precision of `TIME(7)`-`TIME(9)` literals #59571
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
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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #59571"
general
weight: 0.35
cloud-fan
What changes were proposed in this pull request?
This PR makes
Literal.sqlrender the fractional seconds of aTIME(p)literal withpin[7, 9]with exactlypdigits. Like the nanosecond-precision timestamp literals (SPARK-57339, #56494), it truncates the value topdigits before formatting and pads back the trailing zeros with the existing privatepadToNanosPrecisionhelper. The truncation matters only for a literal whose value has more fractional digits thanp, e.g.Literal.create(LocalTime.of(12, 0, 0, 123456789), TimeType(7)).TIME(0)-TIME(6)literals andLiteral.toString(display-only, trailing zeros stripped) are unchanged.TIME(0)-TIME(5)literals andTIME(6)literals carrying sub-microsecond digits (e.g.lit(LocalTime.of(23, 59, 59, 999999999))) do not round-trip throughLiteral.sqleither, 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.sqlrenders aTIMEliteral throughFractionTimeFormatter, which strips trailing fractional zeros. Since SPARK-57551 (#56622), the parser derivesTIME(7)-TIME(9)from the number of fractional digits of aTIMEliteral and givesTIME(6)to a literal with 6 or fewer digits. So aTIME(9)literal whose value ends with zeros, e.g.12:00:00.100000000, is rendered asTIME '12:00:00.1'and parses back asTIME(6).This causes wrong query results. By default (
spark.sql.legacy.parameterSubstitution.constantsOnly=false),spark.sql(sqlText, args)andEXECUTE IMMEDIATE ... USINGsubstitute each argument into the SQL text asLiteral.sqland parse the result. So aTIME(9)argument becomesTIME(6), and a string compared or combined with it is cast toTIME(6), i.e. truncated to microseconds. For example, withval 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.sqlis used for theEXISTS_DEFAULTcolumn metadata, the auto-generated column names, and the values in error messages.EXISTS_DEFAULTkeeps 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
pfractional digits of aTIME(7)-TIME(9)literal.BEFORE
AFTER
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 becauseTIME(7)-TIME(9)was added by SPARK-57551, which exists only in the unreleasedmaster,branch-4.x, andbranch-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.
LiteralExpressionSuitechecks theLiteral.sqlround trip ofTIME(7),TIME(8), andTIME(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.ParametersSuitechecks the comparison and filter results above with named and positional arguments andEXECUTE IMMEDIATE, and the result types ofARRAY,STRUCT, andMAParguments ofTIME(9).Without this fix, they fail like the following.
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