Skip to content

[Bug]: index comments with single quotes generate malformed mo_indexes SQL #27079

Closed

Description

@Ariznawlll

Description

An index comment containing a single quote is accepted by the SQL parser but is inserted into the internally generated mo_catalog.mo_indexes SQL without escaping. Both inline index creation and ALTER TABLE ... ADD INDEX fail with an internal SQL syntax error.

Environment

  • Branch: main
  • Commit: 1d7b6311ce91df0968da435bb405f3d68137c595
  • Deployment: local single process, 1 Log Service + 1 TN + 1 CN
  • Test date: 2026-08-13

Steps to reproduce

DROP DATABASE IF EXISTS index_comment_quote;
CREATE DATABASE index_comment_quote;
USE index_comment_quote;

-- Control: succeeds.
CREATE TABLE safe_control(
  id INT PRIMARY KEY,
  note VARCHAR(32),
  KEY idx_note(note) COMMENT 'safe index comment'
);

-- Reproducer 1: inline index creation.
CREATE TABLE quoted_inline(
  id INT PRIMARY KEY,
  note VARCHAR(32),
  KEY idx_note(note) COMMENT 'index''s comment'
);

-- Reproducer 2: ALTER ADD INDEX.
CREATE TABLE quoted_alter(id INT PRIMARY KEY, note VARCHAR(32));
ALTER TABLE quoted_alter ADD KEY idx_note(note) COMMENT 'index''s comment';
SHOW INDEX FROM quoted_alter;

Actual behavior

Both reproducer statements fail with a syntax error from the internally generated catalog statement. The error exposes the malformed fragment:

... 1, 0, 'index's comment', 'note', 1, null, ...
                 ^ unescaped quote

The inline quoted_inline table is not created. The quoted_alter table remains present with only its primary key; the failed secondary index leaves no catalog residue.

Expected behavior

Both statements must succeed, and SHOW CREATE TABLE / SHOW INDEX must return the index comment as index's comment with correct SQL escaping on replay.

Stability and controls

  • Inline CREATE reproducer: 3/3 failures with the same malformed internal SQL.
  • ALTER ADD INDEX reproducer: 3/3 failures with the same malformed internal SQL.
  • Normal control: COMMENT 'safe index comment' succeeds and is visible in SHOW CREATE TABLE.
  • Failure atomicity: the failed inline table is absent; the failed ALTER leaves no secondary-index row.
  • Fixture check: table and column comments using the same SQL literal escaping ('') succeed, so the input quoting is valid.

Evidence

Final-run excerpt:

create table quoted_inline(... key idx_note(note) comment 'index''s comment');
syntax error ... near "..., 1, 0, 'index's comment', 'note', 1, null, ..."

alter table quoted_alter add key idx_note(note) comment 'index''s comment';
syntax error ... near "..., 1, 0, 'index's comment', 'note', 1, null, ..."

show index from quoted_alter;
quoted_alter  0  PRIMARY  1  id  ...

Minimal runnable SQL and generated output:

/private/tmp/mo-clone-validation/cases/main_index_comment_quote_repro.sql
/private/tmp/mo-clone-validation/cases/main_index_comment_quote_repro.result

Code analysis

pkg/sql/compile/util.go:301-303 correctly serializes IndexAlgoParams with sqlquote.String, but pkg/sql/compile/util.go:311-312 writes indexDef.Comment using raw string interpolation:

fmt.Fprintf(buffer, "'%s', ", indexDef.Comment)

Therefore a catalog value containing ' terminates the SQL literal. The comment should use the same SQL-literal quoting utility used by other string fields. Other raw string fields in this generator should be audited for the same class of bug.

Regression coverage

  1. Add a focused unit test for genInsertMOIndexesSql with an index comment containing ', backslash, and Unicode; assert the generated SQL is safely quoted.
  2. Add distributed DDL cases for both inline index creation and ALTER TABLE ... ADD INDEX.
  3. Verify SHOW CREATE TABLE round-trips the comment and dropping/recreating the table succeeds.
  4. Keep the safe-comment control and post-failure atomicity assertions.

Related

  • [Bug]: table and database CLONE turn invisible indexes visible #27078 was discovered in the same clone/index exploration but concerns index visibility metadata and is not a duplicate.
  • No matching open/closed issue or PR was found for index comment apostrophes, single-quote escaping, mo_indexes comment escaping, or genInsertMOIndexesSql comments.

Activity

  1. added this to the MO-202608 milestone on Aug 13, 2026
  2. added
    kind/bug
    on Aug 13, 2026
  3. added theissue type on Aug 13, 2026
  4. added
    severity/s0
    and removed on Aug 13, 2026
  5. jiangxinmeng1 commented on Aug 19, 2026

    @jiangxinmeng1
    Contributor

    fixed by #27085

  6. Ariznawlll commented on Aug 19, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:b5c948130897783c544e12ab83bfd94702282f89
    • 测试日期:2026-08-19
    • 环境:macOS arm64;MatrixOne 本地单 CN;MOTR 使用 dump 测试账号

    原问题验证

    原问题是 index comment 含单引号时,catalog 生成的 mo_indexes SQL 未正确转义,导致 CREATE INDEX 或 ALTER TABLE ADD INDEX 后 metadata SQL 损坏。

    Happy Path 覆盖

    1. comment 为普通文本时索引创建和 metadata 查询正常。
    2. comment 含单引号、反斜杠和 Unicode 时,CREATE INDEX 后 information_schema.statistics.index_comment 可正确读取。
    3. ALTER TABLE ADD KEY 使用 O''Reilly 后 comment 可正确读取。

    Unhappy Path 覆盖

    覆盖单引号和反斜杠组合,确认不会生成 malformed catalog SQL,且 DDL 失败时不会留下半成品索引。

    白盒验证

    执行 compile/plan 定向测试 3 次全部通过;MOTR 用例 14_issue_regression.issue_27079_index_comment_escape 连续 3 次通过(每次 8 statements,0 failures)。对应修复为 PR #27085,commit 61d03894a。

    回归测试

    已加入 MOTR 分支 codex/stabilize-motr-analyze-results,commit f7e6b68d。

    大数据评估

    不需要新增 big-data。问题发生在 DDL comment 序列化和 catalog SQL 转义阶段,与表行数无关。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions