[Bug]: COPY ALTER doubles apostrophes in CHECK and INCLUDE identifiers

Open
#33,357 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@matrix-meow is already working on this.

Since Nov 27, 2026.

Assessment

This issue has not been assessed yet.

Description

kind/bug needs-triage

Description

Tested on MatrixOne main c0cef5c9cce2164fedd201c188bfffe4d2d09a96 (version() = 8.0.30-MatrixOne-v1.3.0, git_version() = c0cef5c), single CN.

COPY schema replay incorrectly doubles an apostrophe inside a backtick-quoted identifier. This produces two user-visible failures from the same formatting root cause:

  1. An unrelated successful COPY silently changes a CHECK constraint name from c'k to c''k.
  2. FULLTEXT2 and IVFFLAT indexes whose INCLUDE column contains an apostrophe make an unrelated COPY fail with 20301 because replay looks for the doubled, nonexistent column name.

The symptoms are reported together because both come from applying string-literal escaping to quoted identifiers in the temporary CREATE replay.

Problem 1: COPY persists a different CHECK constraint name

CREATE TABLE ck_copy(
  id INT PRIMARY KEY,
  CONSTRAINT `c'k` CHECK(id>0)
);
INSERT INTO ck_copy VALUES(1);

ALTER TABLE ck_copy ALGORITHM=COPY,ADD COLUMN marker INT DEFAULT 7 FIRST;
-- Succeeds; the constraint identity should remain c'k

SELECT HEX(constraint_name),constraint_name
FROM information_schema.table_constraints
WHERE table_schema=DATABASE()
  AND table_name='ck_copy'
  AND constraint_type='CHECK';

Actual result:

6327276B | c''k

Expected result:

63276B | c'k

The doubled name becomes the durable identity:

ALTER TABLE ck_copy DROP CONSTRAINT `c'k`;
-- Actual: Error 1091

ALTER TABLE ck_copy DROP CONSTRAINT `c''k`;
-- Actual: succeeds

The base row remains (marker=7,id=1), and the CHECK still rejects id=-1, although the error uses the renamed c''k. This report therefore concerns silent constraint-identity mutation, not lost CHECK enforcement or lost base rows.

Problem 2: apostrophe INCLUDE columns make COPY fail

CREATE TABLE ivf_bad(
  id BIGINT PRIMARY KEY,
  v VECF32(3),
  `pay'load` INT
);
INSERT INTO ivf_bad VALUES(1,'[1,2,3]',100),(2,'[1,2,4]',200);
CREATE INDEX iv USING IVFFLAT ON ivf_bad(v)
  LISTS=1 OP_TYPE 'vector_l2_ops' INCLUDE(`pay'load`);

ALTER TABLE ivf_bad ALGORITHM=COPY,ADD COLUMN marker INT DEFAULT 7 FIRST;

Actual result:

Error 20301: INCLUDE column 'pay''load' is not exist

Expected result: COPY succeeds, both rows are preserved with marker=7, and the INCLUDE column remains pay'load.

A FULLTEXT2 index with INCLUDE(`sta'tus`) fails at the same replay stage with 20301 while looking for sta''tus. The IVFFLAT and FULLTEXT2 cases are acceptance variants of the same identifier-formatting defect, not independent root causes.

Positive controls and failure atomicity

  • Without COPY, the apostrophe CHECK can be dropped by its original name.
  • An ordinary CHECK name such as ck remains ck through the same COPY.
  • Ordinary IVFFLAT/FULLTEXT2 INCLUDE names such as payload/status survive the same COPY, including covered ANN/MATCH queries and subsequent fresh-connection reads.
  • After either failing INCLUDE COPY, SHOW CREATE has no new marker, both original rows and the original index remain, and covered MATCH/ANN results are unchanged in fresh connections.

MySQL 8.0.46 was also checked for the CHECK case: its SHOW CREATE consistently displays CONSTRAINT `c'k` , the same ALGORITHM=COPY preserves catalog HEX 63276B, and the original name remains droppable.

One pre-existing display symptom should be noted explicitly: MatrixOne's SHOW CREATE text already doubles these apostrophes before COPY. This report owns the additional COPY outcomes—persisted CHECK-name mutation and COPY failure for INCLUDE columns—while a complete fix should correct both the display and internal replay uses of the shared helper.

Root cause

In pkg/sql/plan/build_show_util.go:

  • indexIncludeColumnsToString emits `<name>` using formatStr(resolvedName);
  • constructCheckDefs emits CONSTRAINT `<name>` using formatStr(check.Name).

formatStr doubles backticks and also unconditionally changes ' to ''. Doubling apostrophes is string-literal escaping and is incorrect inside a backtick-quoted identifier. The MySQL scanner only collapses doubled backticks in that context, so replay really parses c'k/pay'load as the different names c''k/pay''load.

Ordinary base-column formatting uses sqlquote.Ident, so the base column itself retains its name. INCLUDE validation then cannot find the doubled name. CHECK replay regenerates the constraint under the doubled name.

Finally, pkg/sql/plan/build_alter_table.go places this generated text in CreateTmpTableSql, and pkg/sql/compile/alter.go executes it as the internal temporary CREATE. The display-formatting error therefore becomes a durable COPY result.

Validation

The CHECK mutation, original/doubled-name DROP behavior, no-COPY and ordinary-name controls, both INCLUDE failures, ordinary INCLUDE controls, failure atomicity, fresh-connection reads, and base-row/enforcement checks passed 35/35 assertions in each of two independent fresh-database runs. The MySQL 8.0.46 CHECK control also completed successfully and preserved the original constraint name.

Duplicate check

Open and closed issue/PR searches covered apostrophes in quoted identifiers, CHECK constraint names, INCLUDE replay, formatStr, and the exact INCLUDE error. No matching issue or open fix was found.

Closed #30550 concerns embedded backticks in table names during internal DROP. Closed #30545/#30549 concern metadata-query escaping for other SHOW statements. Closed #29015/#29016 concern asynchronous serialization of INCLUDE values, and open #33280 concerns pending ASYNC IVFFLAT initialization. None owns CHECK/INCLUDE identifier replay through formatStr.

Dominant language
Go
Stars
1.9k
Forks
312
Avg merge
22h 59m
Merged PRs (30d)
736

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matrixorigin/matrixone

All issues in matrixorigin/matrixone

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.