Skip to content

expression: fix WEEK NULL mode handling#68440

Merged
ti-chi-bot[bot] merged 2 commits into
pingcap:masterfrom
hawkingrei:issue-59444-week-null-mode
May 18, 2026
Merged

expression: fix WEEK NULL mode handling#68440
ti-chi-bot[bot] merged 2 commits into
pingcap:masterfrom
hawkingrei:issue-59444-week-null-mode

Conversation

@hawkingrei
Copy link
Copy Markdown
Member

@hawkingrei hawkingrei commented May 17, 2026

What problem does this PR solve?

Issue Number: close #59444

Problem Summary:

WEEK('2023-01-01', NULL) returns NULL in TiDB, while MySQL returns the same result as mode 0.

What changed and how does it work?

This PR treats a NULL second argument to WEEK(date, mode) as mode 0, matching the existing YEARWEEK(date, mode) behavior and MySQL compatibility.

The change is local to WEEK expression evaluation:

  • update scalar builtinWeekWithModeSig
  • update vectorized builtinWeekWithModeSig
  • add scalar, vectorized, and SQL-level regression coverage

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

Fix the issue that WEEK(date, NULL) returns NULL instead of using week mode 0.

Summary by CodeRabbit

Bug Fixes

  • Fixed the WEEK() function to properly handle NULL mode arguments by defaulting to mode 0 instead of returning NULL for the entire expression, achieving MySQL compatibility.

Review Change Stack

@ti-chi-bot ti-chi-bot Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels May 17, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 5aadb7c2-c645-4997-bb39-57ab6405c7c9

📥 Commits

Reviewing files that changed from the base of the PR and between 6a6eefe and 39d7354.

📒 Files selected for processing (5)
  • pkg/expression/builtin_time.go
  • pkg/expression/builtin_time_test.go
  • pkg/expression/builtin_time_vec.go
  • pkg/expression/builtin_time_vec_test.go
  • pkg/expression/integration_test/integration_test.go

📝 Walkthrough

Walkthrough

The PR fixes the WEEK() builtin function to align with MySQL behavior: when the optional mode argument is NULL, it now defaults to mode 0 and returns a computed week number instead of NULL. The fix is applied to both scalar and vectorized evaluation code paths, with corresponding unit and integration test coverage.

Changes

WEEK() NULL mode default

Layer / File(s) Summary
Scalar WEEK evaluation with NULL mode handling
pkg/expression/builtin_time.go, pkg/expression/builtin_time_test.go
builtinWeekWithModeSig.evalInt now distinguishes NULL from errors: when mode is NULL, it assigns mode to 0 instead of returning NULL for the entire expression. Unit test confirms WEEK("2023-01-01", nil) returns week number 1.
Vectorized WEEK evaluation with NULL mode handling
pkg/expression/builtin_time_vec.go, pkg/expression/builtin_time_vec_test.go
builtinWeekWithModeSig.vecEvalInt is updated to resolve NULL week-mode input to 0 instead of marking the output row as NULL. Vectorized test adds explicit NULL mode argument case for the (ETDatetime, ETInt) signature.
Integration test for WEEK NULL mode
pkg/expression/integration_test/integration_test.go
End-to-end verification added: week("2023-01-01", null) returns 1 with no warnings.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 A week that's null becomes a week that's one,
Default modes make the function complete and run,
MySQL whispers "zero is the key,"
Scalar and vector now in harmony! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'expression: fix WEEK NULL mode handling' clearly and concisely identifies the main change: fixing how the WEEK function handles NULL mode arguments.
Description check ✅ Passed The PR description follows the repository template with all required sections: issue number, problem summary, what changed, comprehensive test coverage, side effects, and release notes.
Linked Issues check ✅ Passed The PR fully addresses the requirements in issue #59444 by implementing WEEK(date, NULL) to return the same result as WEEK(date, 0), matching MySQL behavior, with comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing WEEK NULL mode handling in both scalar and vectorized implementations, with corresponding test coverage and no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@hawkingrei hawkingrei added the AI-Correction Bugfix by AI label May 17, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 17, 2026

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.7280%. Comparing base (6a6eefe) to head (39d7354).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #68440        +/-   ##
================================================
- Coverage   77.2762%   75.7280%   -1.5483%     
================================================
  Files          2010       2008         -2     
  Lines        555477     563839      +8362     
================================================
- Hits         429252     426984      -2268     
- Misses       125305     136720     +11415     
+ Partials        920        135       -785     
Flag Coverage Δ
integration 41.1239% <0.0000%> (+1.3299%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 60.4888% <ø> (ø)
parser ∅ <ø> (∅)
br 49.9725% <ø> (-13.0354%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hawkingrei
Copy link
Copy Markdown
Member Author

/retest-required

@hawkingrei hawkingrei requested a review from windtalker May 18, 2026 03:12
Copy link
Copy Markdown
Contributor

@windtalker windtalker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ti-chi-bot ti-chi-bot Bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels May 18, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 18, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: qw4990, windtalker

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels May 18, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 18, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-05-18 08:12:21.989902566 +0000 UTC m=+165471.494033232: ☑️ agreed by windtalker.
  • 2026-05-18 12:51:35.907022942 +0000 UTC m=+182225.411153608: ☑️ agreed by qw4990.

@ti-chi-bot ti-chi-bot Bot merged commit 889d1a2 into pingcap:master May 18, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-Correction Bugfix by AI approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WEEK should return week even when the mode is NULL

3 participants