Repository navigation
Description
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/3failures with the same malformed internal SQL. - ALTER ADD INDEX reproducer:
3/3failures with the same malformed internal SQL. - Normal control:
COMMENT 'safe index comment'succeeds and is visible inSHOW 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
- Add a focused unit test for
genInsertMOIndexesSqlwith an index comment containing', backslash, and Unicode; assert the generated SQL is safely quoted. - Add distributed DDL cases for both inline index creation and
ALTER TABLE ... ADD INDEX. - Verify
SHOW CREATE TABLEround-trips the comment and dropping/recreating the table succeeds. - 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_indexescomment escaping, orgenInsertMOIndexesSqlcomments.
Activity
fix: escape index comments in catalog SQL (#27085)
jiangxinmeng1 commentedon Aug 19, 2026
fixed by #27085
Ariznawlll commentedon Aug 19, 2026
测试版本
mainb5c948130897783c544e12ab83bfd94702282f89dump测试账号原问题验证
原问题是 index comment 含单引号时,catalog 生成的
mo_indexesSQL 未正确转义,导致 CREATE INDEX 或 ALTER TABLE ADD INDEX 后 metadata SQL 损坏。Happy Path 覆盖
information_schema.statistics.index_comment可正确读取。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,commit61d03894a。回归测试
已加入 MOTR 分支
codex/stabilize-motr-analyze-results,commit f7e6b68d。大数据评估
不需要新增 big-data。问题发生在 DDL comment 序列化和 catalog SQL 转义阶段,与表行数无关。
测试结论
测试通过。
fix: escape index comments in catalog SQL (matrixorigin#27085)
fix: escape index comments in catalog SQL (matrixorigin#27085)
fix: escape index comments in catalog SQL (matrixorigin#27085)