Skip to content

add date extract - #26248

Merged
XuPeng-SH merged 28 commits into
matrixorigin:mainfrom
daviszhen:0727-add-date-extract
Jul 30, 2026
Merged

XuPeng-SH merged 28 commits into
matrixorigin:mainfrom
daviszhen:0727-add-date-extract

Conversation

@daviszhen

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24504

What this PR does / why we need it:

• 此分支主要完成:

  • 为 DAYOFMONTH、DAYNAME、MONTHNAME、QUARTER、WEEKOFYEAR 增加 VARCHAR 参数支持。
  • 新增统一的字符串日期解析执行器,非法日期返回 NULL,并正确处理 NULL、零日期及选择掩码。
  • 修复 FunctionSelectList.AllNull 路径,避免被跳过的行仍被解析并产生非 NULL 结果。
  • WEEKOFYEAR 统一使用 WEEK(date, 3) 的 ISO 周语义,修复 2005-01-01 应返回 53 等跨年边界问题。
  • 补充函数解析、执行器、AllNull 和跨年边界 UT。
  • 为五个函数补充 BVT,结果已与本地 MatrixOne 和 MySQL 行为核对。

@daviszhen
daviszhen requested a review from a user July 27, 2026 10:46
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/L Denotes a PR that changes [500,999] lines labels Jul 28, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed exact head e8b7d62 against latest main 522a157. CI is green and the prospective merge is clean.

Requesting changes for two independent production issues.

[P1] Make the new VARCHAR temporal parser complete and range-safe instead of returning plausible values for malformed input.

The shared component parser currently drifts in both directions from the canonical datetime grammar:

  • A normal ISO datetime such as 2024-01-01T12:34:56 is rejected, so all five new VARCHAR overloads return NULL. MySQL 8.4.10 accepts it, and MatrixOne already accepts this form in types.ParseDatetime.
  • Out-of-range time suffixes such as 2024-01-01 24:00:00 and 2024-01-01 23:60:00 are accepted because the state machine counts time-field digits but never records or validates their values. All five functions then return normal results, whereas MySQL returns NULL.
  • 4294967297-01-01 is accumulated as int64 and narrowed to int32 before validation, wrapping to year 1. The probe on this head produced a valid 0001-01-01, so DAYNAME/WEEKOFYEAR and the component extractors return plausible but false values; MySQL returns NULL.
  • The month state has no rejection branch for unexpected characters. For example, 2024-0x-01 is accepted as an incomplete date and produces DAYOFMONTH=1 and QUARTER=0, while MySQL returns NULL for every extractor.

These are one parser-contract problem, not isolated literals: accept the supported complete datetime grammar, validate separators and time ranges, and bounds-check raw components before narrowing. Please add category tests for ISO separators, malformed separators, time boundaries, and oversized components.

[P2] Remove the measurable regression added to the existing ParseDateCast hot path.

ParseDateCast now trims and calls isZeroDatetimeString, then ParseDateCastComponents repeats both operations. isZeroDatetimeString includes a full IndexByte scan, so every existing date cast and external-ingestion caller pays duplicated preprocessing even though the PR only needs raw components for the new overloads.

A same-host, zero-allocation benchmark against latest main, 500 ms x 5, gave these median times:

  • dashed date: 23.70 -> 29.06 ns/op (+22.6%)
  • compact date: 14.79 -> 17.92 ns/op (+21.2%)
  • datetime with fraction: 53.53 -> 62.29 ns/op (+16.4%)

Please structure the shared parser around an internal already-trimmed path or return enough metadata for ParseDateCast to avoid redoing the scan. Keep a regression benchmark or otherwise verify parity for existing ParseDateCast callers.

Other verification on this exact head passed: full pkg/container/types and pkg/sql/plan/function suites under race; the new focused tests for 100 repetitions under race; vet and diff checks. I also exhaustively compared Date.Week(3) with Go ISOWeek over every valid date from year 1 through 9999, and that WEEKOFYEAR replacement is correct.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Re-reviewed exact head e8b7d62 against latest main 522a157. CI is green and the prospective merge is clean.

Requesting changes for two independent production issues.

[P1] Make the new VARCHAR temporal parser complete and range-safe instead of returning plausible values for malformed input.

The shared component parser currently drifts in both directions from the canonical datetime grammar:

  • A normal ISO datetime such as 2024-01-01T12:34:56 is rejected, so all five new VARCHAR overloads return NULL. MySQL 8.4.10 accepts it, and MatrixOne already accepts this form in types.ParseDatetime.
  • Out-of-range time suffixes such as 2024-01-01 24:00:00 and 2024-01-01 23:60:00 are accepted because the state machine counts time-field digits but never records or validates their values. All five functions then return normal results, whereas MySQL returns NULL.
  • 4294967297-01-01 is accumulated as int64 and narrowed to int32 before validation, wrapping to year 1. The probe on this head produced a valid 0001-01-01, so DAYNAME/WEEKOFYEAR and the component extractors return plausible but false values; MySQL returns NULL.
  • The month state has no rejection branch for unexpected characters. For example, 2024-0x-01 is accepted as an incomplete date and produces DAYOFMONTH=1 and QUARTER=0, while MySQL returns NULL for every extractor.

These are one parser-contract problem, not isolated literals: accept the supported complete datetime grammar, validate separators and time ranges, and bounds-check raw components before narrowing. Please add category tests for ISO separators, malformed separators, time boundaries, and oversized components.

[P2] Remove the measurable regression added to the existing ParseDateCast hot path.

ParseDateCast now trims and calls isZeroDatetimeString, then ParseDateCastComponents repeats both operations. isZeroDatetimeString includes a full IndexByte scan, so every existing date cast and external-ingestion caller pays duplicated preprocessing even though the PR only needs raw components for the new overloads.

A same-host, zero-allocation benchmark against latest main, 500 ms x 5, gave these median times:

  • dashed date: 23.70 -> 29.06 ns/op (+22.6%)
  • compact date: 14.79 -> 17.92 ns/op (+21.2%)
  • datetime with fraction: 53.53 -> 62.29 ns/op (+16.4%)

Please structure the shared parser around an internal already-trimmed path or return enough metadata for ParseDateCast to avoid redoing the scan. Keep a regression benchmark or otherwise verify parity for existing ParseDateCast callers.

Other verification on this exact head passed: full pkg/container/types and pkg/sql/plan/function suites under race; the new focused tests for 100 repetitions under race; vet and diff checks. I also exhaustively compared Date.Week(3) with Go ISOWeek over every valid date from year 1 through 9999, and that WEEKOFYEAR replacement is correct.

  • 重构 VARCHAR 日期组件解析,支持 T 分隔的 ISO datetime,同时统一完整日期与零日期/不完整日期的校验路径。
  • 修复非法输入被误解析的问题:拒绝非法分隔符、无效时分秒、悬空 T、超大年份及越界月份/日期,相关五个日期提取函数均返回 NULL。
  • 保留并优化 ParseDateCast 快路径,避免重复 trim/扫描和重复解析,消除审查指出的热路径性能回退。
  • 补充类型层与函数层 UT,包括 ISO 格式、非法时间、非法分隔符、超大年份等场景,并增加 ParseDateCast benchmark。
  • 新增 BVT func_datetime_varchar_date_validation,覆盖 DAYOFMONTH、DAYNAME、MONTHNAME、QUARTER、WEEKOFYEAR 对合法 ISO 输入和各类非法输入的行为。

验证已通过:git diff --check、相关 UT 的 race 重复执行,以及 pkg/container/types、pkg/sql/plan/function 的 race 测试。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed exact head 858ea17e1c12b780adc1677d952c0b31b2d5a24e. The previous parser-safety and ParseDateCast performance blockers are fixed: ISO-T, range checks, oversized years, and malformed separators now follow the intended paths, and same-host 0-allocation benchmarks show the common dashed/compact/datetime-with-fraction paths are about 46%/24%/55% faster than current main.

Two independent MySQL-compatibility issues remain:

  1. [P1] Implement MySQL year-0 calendar semantics instead of treating every year-0 value as an incomplete date and validating it as year 2000. On MySQL 8.4.10, 0000-01-01 is a valid date: DAYOFMONTH/DAYNAME/MONTHNAME/QUARTER/WEEKOFYEAR return 1 / Sunday / January / 1 / 52. This head returns NULL for DAYNAME and WEEKOFYEAR because types.ValidDate rejects year 0. Conversely, MySQL rejects 0000-02-29 for all five functions because year 0 is not a leap year, while validationYear = 2000 makes this head return 29 / February / 1 from the component extractors. MatrixOne already has the MySQL-specific year-0 rule in calcDaysInYear; please validate and calculate this category consistently and add both valid and invalid year-0 regressions.

  2. [P2] Accept the nonstandard date delimiters that MySQL still accepts. MySQL 8.4.10 returns 15 / Monday / January / 1 / 3 for both 2024/01/15 and 2024:01:15 (with deprecation warnings). The shared state machine accepts only -, so all five new overloads return NULL. This is a real migration compatibility break for existing MySQL data even though those delimiters are deprecated. Please handle the delimiter grammar as a category rather than adding literal-specific exceptions, and cover date plus datetime forms.

Both categories were reproduced against MySQL 8.4.10 and with direct current-head parser probes. The PR-owned focused tests pass, confirming these are coverage gaps rather than environment failures.

@daviszhen

Copy link
Copy Markdown
Contributor Author

Re-reviewed exact head 858ea17e1c12b780adc1677d952c0b31b2d5a24e. The previous parser-safety and ParseDateCast performance blockers are fixed: ISO-T, range checks, oversized years, and malformed separators now follow the intended paths, and same-host 0-allocation benchmarks show the common dashed/compact/datetime-with-fraction paths are about 46%/24%/55% faster than current main.

Two independent MySQL-compatibility issues remain:

  1. [P1] Implement MySQL year-0 calendar semantics instead of treating every year-0 value as an incomplete date and validating it as year 2000. On MySQL 8.4.10, 0000-01-01 is a valid date: DAYOFMONTH/DAYNAME/MONTHNAME/QUARTER/WEEKOFYEAR return 1 / Sunday / January / 1 / 52. This head returns NULL for DAYNAME and WEEKOFYEAR because types.ValidDate rejects year 0. Conversely, MySQL rejects 0000-02-29 for all five functions because year 0 is not a leap year, while validationYear = 2000 makes this head return 29 / February / 1 from the component extractors. MatrixOne already has the MySQL-specific year-0 rule in calcDaysInYear; please validate and calculate this category consistently and add both valid and invalid year-0 regressions.
  2. [P2] Accept the nonstandard date delimiters that MySQL still accepts. MySQL 8.4.10 returns 15 / Monday / January / 1 / 3 for both 2024/01/15 and 2024:01:15 (with deprecation warnings). The shared state machine accepts only -, so all five new overloads return NULL. This is a real migration compatibility break for existing MySQL data even though those delimiters are deprecated. Please handle the delimiter grammar as a category rather than adding literal-specific exceptions, and cover date plus datetime forms.

Both categories were reproduced against MySQL 8.4.10 and with direct current-head parser probes. The PR-owned focused tests pass, confirming these are coverage gaps rather than environment failures.

  • 新增 year-0 日历校验:允许有效的 0000-01-01,并按 MySQL 规则将 year 0 视为非闰年,拒绝 0000-02-29。
  • 为 DAYNAME、WEEKOFYEAR 增加基于原始年月日的 year-0 计算,避免错误依赖不支持 year 0 的内部 Date 表示。
  • 日期字符串解析改为支持 MySQL 兼容的标点分隔符,包括 /、:、. 等,以及相应 datetime 输入。
  • 保留普通 DATE 类型的年份下界,year 0 仅用于 VARCHAR 日期提取函数兼容路径。
  • 补充类型层和函数层 UT,覆盖有效/无效 year 0、日期与 datetime 分隔符形式。
  • 扩展 func_datetime_varchar_date_validation BVT 及结果文件,覆盖五个新增日期提取函数。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: relaxed temporal parsing is still only partially compatible and now codifies a valid MySQL date-context value as invalid. In parseDateCastComponents, the (s[i] != : || yearLen == 4) gate makes 10:11:12 fail, and the new unit test explicitly expects that error. MySQL date-context rules interpret it as 2010-11-12; delimited YY-MM-DD/YY:MM:DD values use the documented 00-69 => 2000-2069 and 70-99 => 1970-1999 conversion. The same root issue exists in the datetime suffix: 2024/01/15 12*34*56 is valid relaxed MySQL syntax, but this parser accepts only : between time fields. These inputs therefore return NULL from DAYNAME/WEEKOFYEAR/etc. even though MySQL returns a value. Please implement the delimiter category coherently rather than special-casing four-digit-year colon: normalize two-digit years at the shared parser boundary, accept punctuation delimiters between time fields while retaining range/full-consumption validation, and add boundary/oracle tests (for example 10:11:12, years 69/70, and a datetime with * time delimiters). The year-0 calendar fix and standard hot paths otherwise look sound; focused tests passed and ParseDateCast remains 0-allocation.

@daviszhen

Copy link
Copy Markdown
Contributor Author

P1: relaxed temporal parsing is still only partially compatible and now codifies a valid MySQL date-context value as invalid. In parseDateCastComponents, the (s[i] != : || yearLen == 4) gate makes 10:11:12 fail, and the new unit test explicitly expects that error. MySQL date-context rules interpret it as 2010-11-12; delimited YY-MM-DD/YY:MM:DD values use the documented 00-69 => 2000-2069 and 70-99 => 1970-1999 conversion. The same root issue exists in the datetime suffix: 2024/01/15 12*34*56 is valid relaxed MySQL syntax, but this parser accepts only : between time fields. These inputs therefore return NULL from DAYNAME/WEEKOFYEAR/etc. even though MySQL returns a value. Please implement the delimiter category coherently rather than special-casing four-digit-year colon: normalize two-digit years at the shared parser boundary, accept punctuation delimiters between time fields while retaining range/full-consumption validation, and add boundary/oracle tests (for example 10:11:12, years 69/70, and a datetime with * time delimiters). The year-0 calendar fix and standard hot paths otherwise look sound; focused tests passed and ParseDateCast remains 0-allocation.

  • 移除对 : 两位年份的错误限制,支持 10:11:12。

  • 在共享解析器中加入 MySQL 两位年份映射:

    • 00-69 -> 2000-2069
    • 70-99 -> 1970-1999
  • 时分秒分隔符从仅支持 : 扩展为统一支持标点符号,例如 123456。

  • 保留原有时间范围、日期范围和完整输入校验。

  • 增加 YY-MM-DD、YY:MM:DD、69/70 边界以及带 * 时间分隔符的单测。

  • 在 func_datetime_varchar_date_validation 中补充对应 BVT 和结果文件。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The two-digit-year fix is still incomplete for compact MySQL date forms. parseDateCastComponents rejects every all-digit input shorter than 7 bytes before applying normalizeDateCastYear, so the new VARCHAR overloads return NULL for 101112, 691231, 700101, and incomplete 101100.

MySQL 8.4 interprets these as 2010-11-12, 2069-12-31, 1970-01-01, and 2010-11-00, respectively. The five functions added by this PR consequently return the corresponding day/month/quarter values (and NULL only for the calendar-dependent functions on the incomplete date). My focused test against this head confirmed ParseDateCastComponents returns invalid argument parsedate for all four inputs.

Please extend the two-digit-year normalization to compact YYMMDD forms as well, including the 69/70 boundary and incomplete-date cases, and add unit/BVT coverage. MySQL also accepts compact YYMMDDHHMMSS (for example 101112123456) as the same date, so that form should be covered if the intent is to preserve the accepted VARCHAR temporal grammar.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed the current head in the context of the full discussion and the author's latest response. The response implements the previously requested categories: two-digit delimited years (including the 69/70 window), relaxed punctuation in time fields, year-0 calendar handling, parser range/full-consumption checks, and focused UT/BVT coverage. Full pkg/container/types and pkg/sql/plan/function tests pass locally, as do the relevant focused tests under -race -count=10.

I am treating compact YYMMDD / YYMMDDHHMMSS parsing as a non-blocking follow-up rather than expanding this PR beyond issue #24504 after the author addressed the requested cases. MySQL accepts those forms, while the shared parser currently rejects them, so retaining that compatibility gap as a follow-up would still be useful.

@mergify

mergify Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-30 05:35 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-07-30 06:27 UTC · at d12b39225ff36cca2b987d818d596df38e57b98f

This pull request spent 51 minutes 55 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • 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)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
All conditions
  • any of [🛡 GitHub branch protection]:
    • 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)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

Pull request #26248 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
ci — d12b3922 Deployed Jul 30, 2026 by mergify[bot] via Matrixone Utils CI / Coverage #43526
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants