Skip to content

Fix crash when inserting invalid integer into DATETIME column with DuckDB engine - #134

Merged
p1p1bear merged 1 commit into
alibaba:masterfrom
p1p1bear:fix_issue_131
Mar 31, 2026
Merged

Fix crash when inserting invalid integer into DATETIME column with DuckDB engine#134
p1p1bear merged 1 commit into
alibaba:masterfrom
p1p1bear:fix_issue_131

Conversation

@p1p1bear

Copy link
Copy Markdown
Contributor

Fixes #131

Description

  • Server crashes with assertion failure when executing: INSERT IGNORE INTO t (col1) VALUES (57399) where col1 is a DATETIME column on a DuckDB engine table.
  • The assertion mon > 0 && mon < 13 && year <= 9999 in sec_since_epoch() (sql/tztime.cc:356) fails because the MYSQL_TIME struct contains month=0.
  • InnoDB handles this case gracefully by truncating to zero date with a warning, but DuckDB engine crashes.

Cause

  • MySQL's SQL layer converts the invalid integer 57399 to a zero date (0000-00-00 00:00:00) via number_to_datetime() -> reset(), setting month=0.
  • In the DuckDB write path, DeltaAppender::append_mysql_field() for MYSQL_TYPE_DATETIME2 calls TIME_to_gmt_sec() without validating the MYSQL_TIME struct, which triggers the assertion in sec_since_epoch().

Fix

  • Add a zero/invalid date check (tm.month == 0) in the MYSQL_TYPE_DATETIME2 branch of DeltaAppender::append_mysql_field() before calling TIME_to_gmt_sec().
  • When a zero date is detected, compute the timestamp directly using calc_daynr(), consistent with the existing MYSQL_TYPE_NEWDATE handling.
  • Valid dates (month in [1, 12]) continue to use the original TIME_to_gmt_sec() path.

…ckDB engine

Fixes alibaba#131

Description
===========
- Server crashes with assertion failure when executing:
  INSERT IGNORE INTO t (col1) VALUES (57399)
  where col1 is a DATETIME column on a DuckDB engine table.
- The assertion `mon > 0 && mon < 13 && year <= 9999` in sec_since_epoch()
  (sql/tztime.cc:356) fails because the MYSQL_TIME struct contains month=0.
- InnoDB handles this case gracefully by truncating to zero date with a warning,
  but DuckDB engine crashes.

Cause
=====
- MySQL's SQL layer converts the invalid integer 57399 to a zero date
  (0000-00-00 00:00:00) via number_to_datetime() -> reset(), setting month=0.
- In the DuckDB write path, DeltaAppender::append_mysql_field() for
  MYSQL_TYPE_DATETIME2 calls TIME_to_gmt_sec() without validating the
  MYSQL_TIME struct, which triggers the assertion in sec_since_epoch().

Fix
===
- Add a zero/invalid date check (tm.month == 0) in the MYSQL_TYPE_DATETIME2
  branch of DeltaAppender::append_mysql_field() before calling TIME_to_gmt_sec().
- When a zero date is detected, compute the timestamp directly using calc_daynr(),
  consistent with the existing MYSQL_TYPE_NEWDATE handling.
- Valid dates (month in [1, 12]) continue to use the original TIME_to_gmt_sec() path.
@p1p1bear p1p1bear self-assigned this Mar 31, 2026
@CLAassistant

CLAassistant commented Mar 31, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@p1p1bear
p1p1bear merged commit 4fb1793 into alibaba:master Mar 31, 2026
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assertion `mon > 0 && mon < 13 && year <= 9999' failed. Crash in debug builds

2 participants