Skip to content

[Bug]: QUOTE returns SQL NULL and uses non-MySQL escaping #28203

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

The old QUOTE feature-request issues #22943 and #22999 were closed after the function was implemented. #25054 concerns an internal SQL-quoting helper, not the SQL QUOTE() function.

Branch Name

main

Commit ID

e7bb0572235ec4bac81eb098eb0ad8900f0065ab

Other Environment Information

  • Deployment: local standalone MatrixOne, 1 CN / 1 TN / 1 Log service
  • Client: MySQL text protocol
  • Reference: MySQL 8.3.0

Actual Behavior

QUOTE() does not implement the MySQL result contract for NULL, apostrophes, or newlines:

SELECT QUOTE(NULL);
-- MatrixOne: SQL NULL
-- MySQL:    string "NULL"

SELECT HEX(QUOTE(CONCAT('a', CHAR(39), 'b')));
-- MatrixOne: 276127276227       ('a''b')
-- MySQL:    27615C276227       ('a\'b')

SELECT HEX(QUOTE(CONCAT('a', CHAR(10), 'b')));
-- MatrixOne: 27615C6E6227       ('a\nb')
-- MySQL:    27610A6227         (the newline is retained)

Backslash, NUL, and Ctrl+Z controls do match MySQL. The discrepancies above occur for both constant expressions and table-column values and were reproduced three times.

Expected Behavior

QUOTE(NULL) must return the non-NULL string NULL. For non-NULL values, output bytes must match MySQL's documented SQL-literal escaping: apostrophes are preceded by a backslash, while ordinary newlines are retained.

MySQL reference: https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_quote

Steps to Reproduce

SELECT QUOTE(NULL), HEX(QUOTE(NULL));
SELECT QUOTE(CONCAT('a', CHAR(39), 'b')),
       HEX(QUOTE(CONCAT('a', CHAR(39), 'b')));
SELECT QUOTE(CONCAT('a', CHAR(10), 'b')),
       HEX(QUOTE(CONCAT('a', CHAR(10), 'b')));

Additional information

The implementation currently marks SQL NULL as a null result before calling the quoting writer. For apostrophes, writeQuotedBytes writes two quote bytes; for newline, it writes backslash plus n. Those choices explain the observed output exactly.

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. added
    severity/s0
    and removed on Sep 5, 2026
  6. XuPeng-SH commented on Sep 8, 2026

    @XuPeng-SH
    Contributor

    修复已提交到 PR #28443: #28443

    根因已确认:后续 writer 重构偏离了早期 MySQL 兼容实现,导致 QUOTE(NULL) 被当作 SQL NULL、单引号使用双引号形式、LF/CR/TAB 被错误转义;同时长度计算与 writer 的规则存在漂移风险。

    本次统一了字节级长度计算和 writer:NULL 输入返回非 NULL 字符串 NULL;仅转义单引号、反斜杠、NUL、Ctrl-Z;保留其他字节,因此兼容二进制和非法 UTF-8。外层 select-list 的掩码行仍保持 SQL NULL。

    验证:函数包完整 UT 通过;QUOTE/string-domain race 定向测试通过;独立服务黑盒覆盖 NULL、单引号、反斜杠、LF、CR、TAB、NUL、Ctrl-Z;func_string_quote BVT 24/24 连续两次通过;molint、golangci-lint、license-eye(5205 文件,invalid 0)通过。

  7. XuPeng-SH commented on Sep 8, 2026

    @XuPeng-SH
    Contributor

    PR #28443 follow-up commit 10d63e1 已补齐 review 发现的返回类型边界问题。

    Root cause:QUOTE(NULL) 的运行时结果是四字节字符串 NULL,但原 quoteReturnType 对已知零宽 VARCHAR/VARBINARY 只给出 2 字节上界;CTAS 等消费者会因此得到低估的列元数据。

    修复:

    • 对已知且有限的 QUOTE 返回宽度设置至少 4;
    • 未知/溢出宽度继续保持未知,不改变原有保守行为;
    • 增加 VARCHAR(0)、VARBINARY(0) 类型推导测试;
    • 增加真实 VARCHAR(0) -> QUOTE(NULL) -> CTAS 边界测试。

    证据:目标包 UT、go vet/molint、golangci-lint(0 issues)均通过;固定二进制执行 func_string_quote BVT 30/30 通过。运行时 QUOTE 实现未改,性能路径不变。PR 合入后再由 CI 做完整门禁。

  8. added a commit that references this issue on Sep 9, 2026
    af965c3
  9. removed their assignment
    on Sep 9, 2026
  10. Ariznawlll commented on Sep 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在包含 #28443 的最新 main(f0c31cd4b830be32442cf329e0a3fb08aa9c16c3)确认 QUOTE() 的结果契约已与 MySQL 8.3 对齐:

    • QUOTE(NULL) 返回非空字符串 NULL,继续参与 CONCAT 时得到 [NULL];
    • 单引号使用反斜杠转义;反斜杠、NUL、Ctrl-Z 的字节输出正确;
    • LF、CR、TAB 保留原字节,不再转换为 \\n、\\r、\\t;
    • 上述行为在常量、VARCHAR/VARBINARY 列、SQL PREPARE/EXECUTE、CTAS 中一致;
    • VARCHAR(32) 经 QUOTE() 的 CTAS 宽度为 VARCHAR(66),与 MySQL 一致;
    • 每类场景连续执行 3 次,连接和源表保持可用。

    额外验证了二进制输入:MatrixOne 保留 VARBINARY 结果域并能按字节处理无效 UTF-8;这是 #28443 明确实现的扩展行为,不影响本 issue 所列 MySQL 兼容性修复。

  11. added 3 commits that reference this issue on Sep 17, 2026
    bd308bb
    37619a2
    2b92913
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugseverity/s0

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions