Repository navigation
Description
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 remained0. - 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
NEXTVALcontinued 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:
pkg/sql/plan/build_ddl.go:1039-1045resolves the sourceTableDef.pkg/sql/plan/build_ddl.go:1060-1068rejects onlySystemViewRelandSystemExternalRel;catalog.SystemSequenceRelfalls through.pkg/sql/plan/build_ddl.go:1081-1099rewrites the resolved definition through a bareCREATE TABLE, so the target is created as ordinaryrelkind = 'r'.pkg/sql/colexec/table_clone/table_clone.go:280-338opens the sequence's underlying relation and block-clones its internal row into that ordinary target.- Normal DML protection at
pkg/sql/plan/build_constraint_util.go:480-481applies only whileTableType == SystemSequenceRel; the converted target bypasses it. - Sequence functions independently validate the target relation kind at
pkg/sql/plan/function/seqfunc.go:97-103, producingTable 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:
- live and named-snapshot
CREATE TABLE ... CLONE <sequence>are rejected; - no target catalog row or physical relation remains after rejection;
- the source sequence definition/state and next values remain unchanged apart from explicit
NEXTVALcalls; - ordinary base-table CLONE still succeeds;
- 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
- [Bug]: CREATE DATABASE ... CLONE silently omits sequences and leaves dependent views unusable #27049 concerns database-level CLONE silently omitting sequences. This issue is a separate, opposite table-level validation failure with a different fix path.
- [Bug]: CREATE TEMPORARY TABLE ... CLONE leaves an unqueryable session ghost after failing #27042 is the original CLONE exploration reference.
- No matching open/closed issue or fix PR was found using searches for
CREATE TABLE CLONE sequence,last_seq_num clone,Table input is not a sequence, andSystemSequenceRel clone build_ddl.
Activity
fix: reject sequence sources in table clone (#27118)
fix: preserve expression defaults in SHOW CREATE TABLE and CLONE (#27136
gouhongshen commentedon Aug 17, 2026
fixed by #27136 #27118
Ariznawlll commentedon Aug 18, 2026
测试版本
main548c4122f0f8005a6cfefda23dc88e34645ba7f8原问题验证
创建 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 覆盖
CLONE成功,目标表精确保留(1, 'base-source')。Unhappy Path 覆盖
dst_live、dst_like均未创建。dst_snapshot未创建,源 sequence 状态保持可用。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 或行数,不需要大数据回归。测试证据
./motr --repeat 3 --branch main ... issue_27050_clone_sequence:3/3 success,81/81 SQL assertions success。测试结论
测试通过。
fix: reject sequence sources in table clone (matrixorigin#27118)
fix: preserve expression defaults in SHOW CREATE TABLE and CLONE (mat…
fix: reject sequence sources in table clone (matrixorigin#27118)
fix: preserve expression defaults in SHOW CREATE TABLE and CLONE (mat…
fix: reject sequence sources in table clone (matrixorigin#27118)
fix: preserve expression defaults in SHOW CREATE TABLE and CLONE (mat…