Skip to content

[Bug]: CREATE TABLE ... CLONE converts a sequence into a writable base table #27050

Closed

Description

@Ariznawlll

Description

CREATE TABLE dst CLONE seq accepts a sequence as its source and converts the sequence's internal seven-column storage relation into a writable ordinary table. The target has relkind = 'r', is absent from SHOW SEQUENCES, accepts arbitrary INSERT/UPDATE, and is rejected by NEXTVAL as "not a sequence".

The equivalent non-base-table control using a view is correctly rejected with ... is not BASE TABLE and leaves no target. A sequence should receive the same early type validation rather than exposing and copying its internal state as a user table.

Environment

  • Branch: main
  • Commit: 6b81a6d364dad13b1b2b1e52a9c3ac1f212d92bb
  • Test date: 2026-08-12
  • Deployment: local single process, 1 Log Service + 1 TN + 1 CN
  • Client: MySQL protocol, port 6001

Steps to reproduce

drop database if exists clone_sequence_as_table;
create database clone_sequence_as_table;
use clone_sequence_as_table;

create sequence seq1 increment 2 start with 11 no cycle;
select nextval('seq1');

create table dst clone seq1;

select relname, relkind
from mo_catalog.mo_tables
where reldatabase = 'clone_sequence_as_table'
order by relname;

show create table dst;
select * from dst;
show sequences;

update dst set last_seq_num = 777 where increment_value = 2;
insert into dst values
  (999, 1, 9223372036854775807, 999, 9, false, false);
select * from dst order by increment_value;

select nextval('dst');
select nextval('seq1');

Actual behavior

The CLONE statement succeeds. Catalog metadata shows different object kinds:

relname  relkind
dst      r
seq1     S

SHOW CREATE TABLE dst exposes an ordinary table with these internal sequence columns:

last_seq_num, min_value, max_value, start_value,
increment_value, cycle, is_called

The copied row contains the sequence state. The target accepts arbitrary DML, including changing last_seq_num and inserting a second state row. However:

SELECT NEXTVAL('dst');
ERROR 20101 (HY000): internal error: Table input is not a sequence

SHOW SEQUENCES lists only seq1; the original sequence continues independently.

Snapshot table CLONE behaves the same and copies the sequence state as of the named snapshot.

Expected behavior

CREATE TABLE ... CLONE ... should reject a sequence source before creating any target, just as it rejects a view or external table with ... is not BASE TABLE. A failed statement must leave no target metadata or data.

If sequence cloning is intended to be supported, it should use a sequence-specific operation that creates relkind = 'S', preserves valid sequence invariants, and remains usable by sequence functions; silently converting it to a writable base table is not a valid sequence clone.

Stability and controls

  • Live reproducer: 3/3 with independent sources/targets and different start/increment values.
  • Snapshot variant: reproduced and copied the earlier snapshot state.
  • Normal control: ordinary base-table CLONE returned the exact schema and row.
  • Non-base-table control: View CLONE returned ... is not BASE TABLE; target catalog count remained 0.
  • Invalid snapshot and missing target-database controls were rejected with target/catalog count 0.
  • Source integrity: target DML did not modify the source sequence; source NEXTVAL continued correctly.
  • Service remained queryable; no panic, restart, or hang was observed.

Evidence

Complete reproducer:

/private/tmp/mo-clone-evidence-6b81a6d/clone_sequence_as_table_repro.sql

Three-round signature:

dst_r1 relkind=r, copied last_seq_num=13, increment=2
dst_r2 relkind=r, copied last_seq_num=24, increment=3
dst_r3 relkind=r, copied last_seq_num=35, increment=4
snapshot relkind=r, copied pre-advance last_seq_num=11

Code analysis

The planner resolves the source relation but does not reject the sequence type:

  1. pkg/sql/plan/build_ddl.go:1039-1045 resolves the source TableDef.
  2. pkg/sql/plan/build_ddl.go:1060-1068 rejects only SystemViewRel and SystemExternalRel; catalog.SystemSequenceRel falls through.
  3. pkg/sql/plan/build_ddl.go:1081-1099 rewrites the resolved definition through a bare CREATE TABLE, so the target is created as ordinary relkind = 'r'.
  4. pkg/sql/colexec/table_clone/table_clone.go:280-338 opens the sequence's underlying relation and block-clones its internal row into that ordinary target.
  5. Normal DML protection at pkg/sql/plan/build_constraint_util.go:480-481 applies only while TableType == SystemSequenceRel; the converted target bypasses it.
  6. Sequence functions independently validate the target relation kind at pkg/sql/plan/function/seqfunc.go:97-103, producing Table input is not a sequence.

The minimal confirmed root cause is the missing SystemSequenceRel source-type guard in the shared CREATE TABLE LIKE/CLONE planner path.

Regression coverage

Add a distributed CLONE BVT that asserts:

  1. live and named-snapshot CREATE TABLE ... CLONE <sequence> are rejected;
  2. no target catalog row or physical relation remains after rejection;
  3. the source sequence definition/state and next values remain unchanged apart from explicit NEXTVAL calls;
  4. ordinary base-table CLONE still succeeds;
  5. View, external-table, and sequence source checks share consistent non-BASE-TABLE behavior.

Add a focused planner UT verifying SystemSequenceRel cannot pass through the CREATE TABLE LIKE/CLONE rewrite.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 12, 2026
  2. added
    kind/bug
    on Aug 12, 2026
  3. added theissue type on Aug 12, 2026
  4. added
    severity/s0
    and removed on Aug 12, 2026
  5. added
    ai-easy
    on Aug 13, 2026
  6. added a commit that references this issue on Aug 13, 2026
    a6a6d09
  7. gouhongshen commented on Aug 17, 2026

    @gouhongshen
    Contributor

    fixed by #27136 #27118

  8. added and removed
    severity/s0
    on Aug 17, 2026
  9. Ariznawlll commented on Aug 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:548c4122f0f8005a6cfefda23dc88e34645ba7f8
    • 部署方式:本地隔离 MatrixOne,1 CN / 1 TN / 1 Log,MySQL text protocol
    • 测试日期:2026-08-18

    原问题验证

    创建 sequence 后分别执行原始的 CREATE TABLE ... CLONE sequence、CREATE TABLE ... LIKE sequence,并在 named snapshot 中再次执行 CLONE。四种非 BASE TABLE 源均稳定返回 20101,原因文本为 is not BASE TABLE;目标 relation 计数均为 0,sequence 仍可继续 NEXTVAL。连续 3 轮结果一致。

    Happy Path 覆盖

    1. 普通 base table CLONE 成功,目标表精确保留 (1, 'base-source')。
    2. sequence 在拒绝 CLONE/LIKE 前后均可继续产生预期递增值。
    3. 测试结束后会话仍可执行普通查询。

    Unhappy Path 覆盖

    1. live sequence 的 CLONE 和 LIKE 均拒绝,dst_live、dst_like 均未创建。
    2. named snapshot sequence 的 CLONE 拒绝,dst_snapshot 未创建,源 sequence 状态保持可用。
    3. view CLONE 同样拒绝,dst_view 未创建;每条拒绝后均检查 catalog,无部分对象残留。

    白盒验证

    go test ./pkg/sql/plan -run 'TestBuildCreateTableLikeAndCloneRejectsSequenceSource' -count=1 -v 通过,覆盖 live CLONE、snapshot CLONE 与 LIKE 的 plan 层 relation-kind guard。该问题为 SQL DDL 语义,不适用 -race。

    回归测试

    已加入 MOTR PR #113:issue_27050_clone_sequence.test。用例因创建全局 named snapshot 而顺序执行;不依赖内存阈值、spill 或行数,不需要大数据回归。

    测试证据

    测试结论

    测试通过。

  10. added 6 commits that reference this issue on Sep 17, 2026
    af68a46
    8349f72
    bd587a7
    c6afe4f
    77c7232
    521cd5e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions