Add WEEK() function mode parameter support and configurable CTE max recursion depth
User description
What type of PR is this?
- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring
Which issue(s) this PR fixes:
issue #23265
What this PR does / why we need it:
This PR adds two enhancements:
- WEEK() function mode parameter support: Adds optional second parameter mode (0-7) for WEEK(date, mode) and WEEK(datetime, mode) to control week numbering behavior, making it compatible with MySQL's WEEK() function specification.
- Configurable CTE max recursion depth: Allows users to configure CTE recursion limit via SET cte_max_recursion_depth = N instead of using a hardcoded value. This enables deeper recursion for complex hierarchical queries when needed.
PR Type
Enhancement, Bug fix
Description
-
Add optional mode parameter (0-7) to WEEK() function for date/datetime types
- Supports MySQL-compatible week numbering modes
- Handles mode validation and clamping to valid range
-
Make CTE max recursion depth configurable via SET cte_max_recursion_depth variable
- Replaces hardcoded recursion limit with dynamic resolution
- Enables deeper recursion for complex hierarchical queries
-
Add comprehensive test coverage for WEEK() mode parameter and CTE recursion depth
Should we remain consistent with MySQL?
- Prerequisites (Create Database/Table)
MySQL:
CREATE DATABASE IF NOT EXISTS test;
USE test;
MO:
CREATE DATABASE IF NOT EXISTS test;
USE test;
- WEEK Function Tests
Normal Cases (MySQL and MO results are identical ✅)
| SQL | MySQL | MO |
|---|---|---|
| SELECT week('2023-01-01', 0); | 1 | 1 |
| SELECT week('2023-01-01', 1); | 0 | 0 |
| SELECT week('2023-01-02', 0); | 1 | 1 |
| SELECT week('2023-01-02', 1); | 1 | 1 |
| SELECT week('2023-12-31', 0); | 53 | 53 |
| SELECT week('2023-12-31', 1); | 52 | 52 |
| SELECT week('2023-01-01', -1); | 52 | 52 |
| SELECT week('2023-01-01', 8); | 1 | 1 |
| SELECT week('2023-01-01', 100); | 1 | 1 |
| SELECT week(null, 0); | NULL | NULL |
| SELECT week('2023-01-01', null); | 1 | 1 |
Invalid Cases (MySQL and MO behavior differs ⚠️)
| SQL | MySQL | MO | Note |
|---|---|---|---|
| SELECT week('0000-00-00', 0); | NULL | Error: invalid argument parsedate | MO is stricter, rejects invalid date |
| SELECT week('2023-02-30', 0); | NULL | Error: invalid argument parsedate | MO is stricter, rejects non-existent date |
| SELECT week('abc', 0); | NULL | Error: invalid argument parsedate | MO is stricter, rejects invalid format |
| SELECT week('', 0); | NULL | NULL | Identical |
- CTE Recursion Depth Tests
Prerequisites
CREATE DATABASE IF NOT EXISTS test;
USE test;
DROP TABLE IF EXISTS t_cte;
CREATE TABLE t_cte(a int);
INSERT INTO t_cte VALUES (1);
Normal Cases (MySQL and MO results are identical ✅)
| SQL | MySQL | MO |
|---|---|---|
| SET cte_max_recursion_depth = 200;WITH RECURSIVE c AS (SELECT a FROM t_cte UNION ALL SELECT a+1 FROM c WHERE a < 150) SELECT count(*) FROM c; | 150 | 150 |
| SET cte_max_recursion_depth = 50;WITH RECURSIVE c AS (SELECT a FROM t_cte UNION ALL SELECT a+1 FROM c WHERE a < 100) SELECT count(*) FROM c; | Error: Recursive query aborted after 51 iterations | Error: recursive level out of range |
Invalid Cases (MySQL and MO behavior differs ⚠️)
| SQL | MySQL | MO | Note |
|---|---|---|---|
| SET cte_max_recursion_depth = -1; | Success, silently converts to 0 | Error: convert to the system variable int type failed | MO is stricter, rejects negative values |
| SET cte_max_recursion_depth = -100; | Success, silently converts to 0 | Error: convert to the system variable int type failed | MO is stricter, rejects negative values |
| SET cte_max_recursion_depth = 0; | Success, value is 0 | Success, value is 0 | Identical ✅ |
| SET cte_max_recursion_depth = 4294967295; | Success, value is 4294967295 | Success | Identical ✅ |
| SET cte_max_recursion_depth = 4294967296; | Success, silently truncates to 4294967295 | Error: convert to the system variable int type failed | MO is stricter, rejects overflow |
| SET cte_max_recursion_depth = 'abc'; | Error: Incorrect argument type | Error: convert to the system variable int type failed | Both error ✅ |
Diagram Walkthrough
flowchart LR
A["WEEK Function"] -->|"Add mode parameter"| B["DateToWeek/DatetimeToWeek"]
B -->|"Support modes 0-7"| C["Week calculation"]
D["CTE Recursion"] -->|"Read variable"| E["cte_max_recursion_depth"]
E -->|"Dynamic limit"| F["MergeCTE execution"]
G["Test Cases"] -->|"Validate"| B
G -->|"Validate"| F
File Walkthrough
| Relevant files | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Enhancement |
| ||||||||
| Tests |
|
You are nearing your monthly Qodo Merge usage quota. For more information, please visit here.
PR Compliance Guide 🔍
Below is a summary of compliance checks for this PR:
| Security Compliance | |
| 🟢 | No security concerns identifiedNo security vulnerabilities detected by AI analysis. Human verification advised for critical code. |
| Ticket Compliance | |
| ⚪ | 🎫 No ticket provided
|
| Codebase Duplication Compliance | |
| ⚪ | Codebase context is not definedFollow the guide to enable codebase context checks. |
| Custom Compliance | |
| 🟢 |
Generic: Meaningful Naming and Self-Documenting CodeObjective: Ensure all identifiers clearly express their purpose and intent, making code Status: Passed
|
Generic: Secure Error HandlingObjective: To prevent the leakage of sensitive system information through error messages while Status: Passed
| |
Generic: Secure Logging PracticesObjective: To ensure logs are useful for debugging and auditing without exposing sensitive Status: Passed
| |
Generic: Security-First Input Validation and Data HandlingObjective: Ensure all data inputs are validated, sanitized, and handled securely to prevent Status: Passed
| |
| ⚪ | Generic: Comprehensive Audit TrailsObjective: To create a detailed and reliable record of critical system actions for security analysis Status: Referred Code
|
Generic: Robust Error Handling and Edge Case ManagementObjective: Ensure comprehensive error handling that provides meaningful context and graceful Status: Referred Code
| |
| |
Compliance status legend
🟢 - Fully Compliant🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label
You are nearing your monthly Qodo Merge usage quota. For more information, please visit here.
PR Code Suggestions ✨
Explore these optional code suggestions:
| Category | Suggestion | Impact |
| Possible issue |
Use a more flexible overload checkReplace the pkg/sql/plan/function/list_builtIn.go [7254-7283]
Suggestion importance[1-10]: 8__ Why: The suggestion correctly identifies that | Medium |
Validate that mode argument is constantAdd a validation check to ensure the pkg/sql/plan/function/func_unary.go [3037-3046]
Suggestion importance[1-10]: 7__ Why: The suggestion correctly identifies that the | Medium | |
| High-level |
Resolve CTE recursion depth onceTo improve performance, fetch the Examples:pkg/sql/colexec/mergecte/mergecte.go [124-132]
Solution Walkthrough:Before:
After:
Suggestion importance[1-10]: 7__ Why: The suggestion correctly identifies a performance issue where | Medium |
| ||
Merge Queue Status
✅ The pull request has been merged at 57050c55807edfe8c813c2b1eaa011b1fcaace58
This pull request spent 7 seconds in the queue, with no time running CI. The checks were run in-place.
Required conditions to merge
-
#approved-reviews-by >= 1[🛡 GitHub branch protection]- [X] #23268
-
#changes-requested-reviews-by = 0[🛡 GitHub branch protection]- [X] #23268
-
#review-threads-unresolved = 0[🛡 GitHub branch protection]- [X] #23268
-
branch-protection-review-decision = APPROVED[🛡 GitHub branch protection]- [X] #23268
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC) - [ ]
check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC) - [ ]
check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY) - [ ]
check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY) - [ ]
check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC) - [ ]
check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC) - [ ]
check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone CI / SCA Test on Ubuntu/x86 - [ ]
check-neutral = Matrixone CI / SCA Test on Ubuntu/x86 - [ ]
check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone CI / UT Test on Ubuntu/x86 - [ ]
check-neutral = Matrixone CI / UT Test on Ubuntu/x86 - [ ]
check-skipped = Matrixone CI / UT Test on Ubuntu/x86
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH) - [ ]
check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH) - [ ]
check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic) - [ ]
check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic) - [ ]
check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH) - [ ]
check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH) - [ ]
check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
- [X]
- [X] any of [🛡 GitHub branch protection]:
- [X]
check-skipped = Matrixone Utils CI / Coverage - [ ]
check-neutral = Matrixone Utils CI / Coverage - [ ]
check-success = Matrixone Utils CI / Coverage
- [X]