Skip to content

[Bug]: LAST_DAY returns VARCHAR and CTAS persists VARCHAR(65535) instead of DATE #29552

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched open and closed LAST_DAY reports. #25306 tracks zero/invalid-date behavior, not valid-result types. #28493 is the separate MAKEDATE type issue. #24436 is a broad CTAS tracker whose listed cases do not include LAST_DAY. No dedicated report for this function's return-type failure was found.

Branch Name

main

Commit ID

79fa30e10c0b298567eb073cdfd88bdc91ab09b9

Other Environment Information

  • Debian Linux x86_64; Go 1.26.4; CPU build from the exact commit.
  • Test-owned two-CN standalone launch after graceful restart.
  • Real SQL frontend with PyMySQL; independent MySQL 8.0.44 reference.
  • Discovered during latest-main black-box QA; no claim that a recent commit introduced this longstanding signature.

Actual Behavior

LAST_DAY computes the correct calendar value but exposes VARCHAR rather than DATE, including when its input is already DATE:

SELECT LAST_DAY('2024-02-29'),
       LAST_DAY(CAST('2024-02-29' AS DATE));

MatrixOne advertises both result columns as MySQL protocol type 253 (VARCHAR), display length 262140, and Python receives strings. MySQL 8.0.44 advertises type 10 (DATE), display length 10, and returns date values.

This is observable in SQL independently of client decoding: CTAS stores the output as VARCHAR(65535) on MatrixOne, versus DATE on MySQL. Client metadata and materialized schema therefore lose the temporal contract. The same result-type discrepancy was reproduced for leap-day, ordinary February, year-end, and new-year inputs.

Expected Behavior

LAST_DAY returns DATE for a valid character, DATE, or DATETIME input. CTAS, View metadata, and text/binary prepared output should preserve that DATE contract rather than materializing a large character column. Invalid-date/NULL handling should retain its separately defined behavior.

Steps to Reproduce

DROP DATABASE IF EXISTS qa_lastday_type;
CREATE DATABASE qa_lastday_type;
USE qa_lastday_type;

SELECT LAST_DAY('2024-02-29'),
       LAST_DAY(CAST('2024-02-29' AS DATE));
-- Values agree, but MatrixOne sends VARCHAR metadata instead of DATE.

CREATE TABLE materialized AS
SELECT LAST_DAY('2024-02-29') AS d;

SELECT column_name,data_type,column_type
FROM information_schema.columns
WHERE table_schema='qa_lastday_type' AND table_name='materialized';
-- MatrixOne: d, varchar, VARCHAR(65535)
-- MySQL:     d, date, date

SELECT d FROM materialized;
-- Both represent 2024-02-29, so the mismatch is the actual SQL type.
DROP DATABASE qa_lastday_type;

Additional information

White-box root cause: the LAST_DAY registration in pkg/sql/plan/function/list_builtIn.go has only VARCHAR/CHAR argument overloads and both retType callbacks return types.T_varchar. The existing LastDay execution function produces a string vector. fixedTypeMatch routes native DATE input through the character implementation as well. CTAS faithfully persists this declared type, so its schema builder/client parser is not the owner of the defect.

Suggested priority: P2 compatibility and persistent-schema issue. Restore the temporal result at the function signature/execution owner; do not fix individual clients or add CTAS-only casts. Acceptance should assert exact values and DATE metadata/schema for valid character/DATE/DATETIME inputs, leap/year boundaries, NULL, direct SQL, SQL/binary PREPARE, CTAS, and View consumers.

Activity

  1. self-assigned this
    on Oct 1, 2026
  2. added
    kind/bug
    on Oct 1, 2026
  3. changed the title [-][Compatibility]: LAST_DAY returns VARCHAR and CTAS persists VARCHAR(65535) instead of DATE[/-] [+][Bug]: LAST_DAY returns VARCHAR and CTAS persists VARCHAR(65535) instead of DATE[/+] on Oct 1, 2026
  4. added a commit that references this issue on Oct 2, 2026
    7a169dd
  5. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。CTAS 从目标表达式推导出的列类型为 DATE,结果值与源查询一致。

    Happy Path 覆盖

    覆盖 DATE literal、列、prepared 参数和后续 DESCRIBE。

    Unhappy Path 覆盖

    覆盖 NULL、invalid date 与空输入 CTAS;不退化为 VARCHAR/DATETIME。

    白盒验证

    关联修复 #29556 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:CTAS 从目标表达式推导出的列类型为 DATE,结果值与源查询一致。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

  6. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    Collaborator

    在当前 main bab4b3286a0dd5683a9b291763817722233e586c 上补充了 LAST_DAY 的值、元数据及持久化消费者覆盖。

    结果:

    • VARCHAR、DATE、DATETIME 输入分别得到准确的月末 DATE;leap day、年末和跨年值正确,NULL 保持 NULL;
    • CTAS 两个 LAST_DAY 列均持久化为 DATE(0),不再是 VARCHAR(65535);
    • View 的 information_schema 元数据为 data_type=date, column_type=DATE(0);
    • STORED generated column ld DATE AS (LAST_DAY(dt)) 能正确写入和随 UPDATE 重算,NULL 输入保持 NULL;
    • generated DATE 列建立二级索引后,WHERE ld='2024-02-29' 使用 Index Table Scan 和 prefix_eq;
    • SQL prepared statement 按文本 → DATETIME → NULL → 非法日期 → 有效日期复用,输出分别为准确 DATE / NULL / 恢复后的 DATE。

    真实 MySQL binary prepared statement 另外连续执行 3 轮:

    • 每次 ColumnTypes 都报告 DatabaseTypeName=DATE、ScanType=sql.NullTime;
    • 文本 2024-02-10 扫描为 2024-02-29;Go time.Time(2024-12-31 23:59:59) 扫描为 2024-12-31;
    • NULL 和非法日期均为无值的 sql.NullTime;
    • 随后的 2025-01-01 每轮都恢复为 2025-01-31。

    直接结果、binary protocol metadata、CTAS、View、generated column 及索引消费路径均保持 DATE 合同。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions