← Back to Pull requests

[fix](constant folding) time_format() on a TIME literal folds to NULL, not the string "-null" #68732

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 04, 2026 23:00 UTC
Author

What problem does this PR solve?

Issue Number: close #68725

Related PR: None

Problem Summary:

DateTimeFormatterUtils.toFormatStringConservative(TimeV2Literal, StringLikeLiteral) prefixed the
sign before it checked whether the formatter produced a result:

String res = formatTemporalLiteral(0, 0, 0, time.getHour(), ...);
if (time.isNegative()) {
    res = "-" + res;      // res is null here -> the string "-null"
}
return res;

formatTemporalLiteral returns null when the pattern needs date fields, which a TIME value does
not carry. So every date specifier (%a %b %D %j %M %U %u %V %v %W %w %X %x) on a negative TIME
folded to the string -null in the planner, while BE returns NULL:

SELECT time_format('-10:00:00', '%a') AS fe, time_format(CAST(s1 AS TIME), '%a') AS be FROM t;
+-------+------+
| -null | NULL |
+-------+------+

The fix returns null before applying the sign, and the folding function now maps a null result to a
typed NullLiteral(StringType) instead of handing null to new VarcharLiteral(...), whose
constructor dereferences the string (value.length()). After this change both the negative and the
positive case fold to NULL, which is what BE already returns.

Time-only specifiers are untouched: time_format('-10:00:00', '%H:%i:%s') still folds to
-10:00:00 with the sign kept, as before.

Release note

None

Check List (For Author)

  • Test <!-- At least one of them must be included. -->

  • - [ ] Regression test
    - [x] Unit Test
    - [ ] Manual test (add detailed scripts or steps below)
    - [ ] No need to test or manual test. Explain why:

    New DateTimeExtractAndTransformTest.testTimeFormatKeepsNullForDateSpecifiers covers
    time_format('-10:00:00', '%a') -> NULL, time_format('10:00:00', '%a') -> NULL and
    time_format('-10:00:00', '%H:%i:%s') -> -10:00:00.

    Red, on master with only the test added:

      [ERROR]   DateTimeExtractAndTransformTest.testTimeFormatKeepsNullForDateSpecifiers:341
                Unexpected type, expected: <NullLiteral> but was: <VarcharLiteral>
      

    Green, focused run with both edits:

      [INFO] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0 - in DateTimeExtractAndTransformTest
      [INFO] BUILD SUCCESS
      

    Wider net with the fix (DateTimeExtractAndTransformTest, ConstantProjectionFoldingTest) also
    clean: 14 and 1 tests, no failures. FoldConstantTest was run too and shows two failures
    (testDateConstructFunction:1552, testFoldString:581) that are pre-existing on this machine
    and unrelated
    : they fail identically on pristine master with my edits stashed, and they are
    session-time-zone dependent timestamp assertions.

    Command (project flags from run-fe-ut.sh):

      mvn test -pl fe-core -am -Dcheckstyle.skip=true -DfailIfNoTests=false \
          -Dmaven.build.cache.enabled=false -Dtest=DateTimeExtractAndTransformTest
      

    Environment: JDK 17.0.20 (Temurin), Maven 3.9.16, thrift 0.24.0 from the prebuilt thirdparty
    package, Ubuntu 22.04 under WSL2. Style gate mvn -pl fe-core checkstyle:check ->
    You have 0 Checkstyle violations.

    Review follow-up (second commit): time_format is declared FunctionSignature.ret(
    VarcharType.SYSTEM_DEFAULT)
    , so folding the null result to NullLiteral(StringType.INSTANCE)
    made the folded expression decide the result field type as STRING, which the MySQL protocol
    reports as BLOB instead of VARCHAR. It is now NullLiteral(VarcharType.SYSTEM_DEFAULT), and
    testTimeFormatKeepsNullForDateSpecifiers asserts getDataType() for both the negative and the
    positive TIME input (it is NULL for both, since a TIME value carries no date fields). Re-run on
    that commit: DateTimeExtractAndTransformTest, FoldConstantTest, DateTimeLiteralTest,
    TimeV2LiteralTest
    -> Tests run: 77, Failures: 3, Errors: 0, the three being the same
    session-timezone timestamp assertions quoted above, and 0 Checkstyle violations.

    The FE UT failure on the previous head was not this change: the failed test occurrences in
    TeamCity build 1064064 are BatchRollupJobTest.testBatchRollup,
    ForwardToMasterTest.testAddBeDropBe and two EliminateGroupByKeyByUniformTest cases, and all
    four are reported with currentlyMuted=true. None of them reach time_format.

  • Behavior changed:

  • - [ ] No.
    - [x] Yes. <!-- Explain the behavior change -->
    time_format(<TIME literal>, <pattern with a date specifier>) folds to NULL instead of the
    string -null (or null), matching BE. The folded NULL carries the function's declared
    VARCHAR type, so the result field of such a query is VARCHAR rather than STRING/BLOB. No other
    input changes its folded value.

  • Does this need documentation?

  • - [x] No.
    - [ ] Yes. <!-- Add document PR link here. -->

    Check List (For Reviewer who merge this PR)

  • Confirm the release note

  • Confirm test cases

  • Confirm document

  • Add branch pick label
  • AI assistance disclosure

    An AI coding agent was used to draft this change and the new test. I read and reviewed every line
    myself, ran the FE unit tests above on the exact commit in this branch (including the pristine-master
    control run for FoldConstantTest), and I take responsibility for the result.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: abhinav-phi Review duration: 81634m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.59) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    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.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #68732"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% 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: 59% (MEDIUM) · Higher = larger blast radius
    59%
    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: 40.9% · Lower = rushed or shallow review
    40.9%
    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: 34.8% · Safe to merge
    34.8%
    Repository