Skip to content

fix rename column: delete table with old metadata #22182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions pkg/vm/engine/disttae/txn_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,11 @@ func (tbl *txnTable) TableDefs(ctx context.Context) ([]engine.TableDef, error) {
return defs, nil
}

func (tbl *txnTable) RefeshTableDef(ctx context.Context) {
tbl.tableDef = nil
tbl.GetTableDef(ctx)
}

func (tbl *txnTable) GetTableDef(ctx context.Context) *plan.TableDef {
if tbl.tableDef == nil {
var clusterByDef *plan.ClusterByDef
Expand Down Expand Up @@ -1393,13 +1398,15 @@ func (tbl *txnTable) AlterTable(ctx context.Context, c *engine.ConstraintDef, re
panic("not equal cstr")
}

var baseDefs []engine.TableDef
// update TableDef
if hasReplaceDef {
// When ReplaceDef exists, replace the entire table definition
replaceDef := replaceDefReq.GetReplaceDef()
defs, _, _ := engine.PlanDefsToExeDefs(replaceDef.Def)
defs = append(defs, engine.PlanColsToExeCols(replaceDef.Def.Cols)...)
tbl.defs = defs
baseDefs, _, _ = engine.PlanDefsToExeDefs(replaceDef.Def)
baseDefs = append(baseDefs, engine.PlanColsToExeCols(replaceDef.Def.Cols)...)
} else {
baseDefs = append([]engine.TableDef{}, tbl.defs...)
}

// 0. check if the table is created in txn.
Expand Down Expand Up @@ -1436,16 +1443,16 @@ func (tbl *txnTable) AlterTable(ctx context.Context, c *engine.ConstraintDef, re
}
}

tbl.defs = append(tbl.defs, appendDef...)
tbl.tableDef = nil
tbl.GetTableDef(ctx)

//------------------------------------------------------------------------------------------------------------------
// 1. delete old table metadata
if _, err := tbl.db.deleteTable(ctx, oldTableName, true, !createdInTxn); err != nil {
return err
}

// update table defs after deleting old table metadata
tbl.defs = append(baseDefs, appendDef...)
tbl.RefeshTableDef(ctx)

//------------------------------------------------------------------------------------------------------------------
// 2. insert new table metadata
if err := tbl.db.createWithID(ctx, tbl.tableName, tbl.tableId, tbl.defs, !createdInTxn, tbl.extraInfo); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ drop table if exists orders;
drop table if exists part;
drop table if exists partsupp;
drop table if exists supplier;
select sleep(10);
sleep(10)
0
select count(*) from customer {snapshot = 'tpch_snapshot'};
count(*)
150
Expand Down Expand Up @@ -181,9 +178,6 @@ select count(*) from supplier;
count(*)
10
drop database tpch;
select sleep(10);
sleep(10)
0
select count(*) from tpch.customer {snapshot = 'tpch_snapshot'};
count(*)
150
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ drop table if exists part;
drop table if exists partsupp;
drop table if exists supplier;

select sleep(10);
-- customer
select count(*) from customer {snapshot = 'tpch_snapshot'};
select count(*) from customer {snapshot = 'tpch_snapshot'};
Expand Down Expand Up @@ -90,7 +89,6 @@ select count(*) from supplier;
select count(*) from supplier;

drop database tpch;
select sleep(10);

select count(*) from tpch.customer {snapshot = 'tpch_snapshot'};
select count(*) from tpch.customer {snapshot = 'tpch_snapshot'};
Expand Down
39 changes: 39 additions & 0 deletions test/distributed/cases/ddl/alter.result
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ drop table tm1;
drop table ti2;
drop table tm2;
drop table if exists foreign01;

create temporary table foreign01(col1 int primary key,
col2 varchar(20),
col3 int,
col4 bigint);

insert into foreign01 values(1,'shujuku',100,3247984);

insert into foreign01 values(2,'数据库',328932,32324423432);

drop table if exists foreign02;

create temporary table foreign02(col1 int,
col2 int,
col3 int primary key,
Expand All @@ -81,27 +86,41 @@ no such table test.foreign02
insert into foreign02 values(2,2,2);
no such table test.foreign02
delete from foreign01 where col3 = 100;

show create table foreign02;
no such table test.foreign02
alter table foreign02 drop foreign key fk;
no such table test.foreign02
show create table foreign02;
no such table test.foreign02
drop table foreign01;

drop table foreign02;
no such table test.foreign02
drop table if exists ti1;

drop table if exists tm1;

drop table if exists ti2;

drop table if exists tm2;

create temporary table ti1(a INT not null, b INT, c INT);

create temporary table tm1(a INT not null, b INT, c INT);

create temporary table ti2(a INT primary key AUTO_INCREMENT, b INT, c INT);

create temporary table tm2(a INT primary key AUTO_INCREMENT, b INT, c INT);

insert into ti1 values (1,1,1), (2,2,2);

insert into ti2 values (1,1,1), (2,2,2);

insert into tm1 values (1,1,1), (2,2,2);

insert into tm2 values (1,1,1), (2,2,2);

alter table ti1 add constraint fi1 foreign key (b) references ti2(a);
alter table for temporary table is not yet implemented
alter table tm1 add constraint fm1 foreign key (b) references tm2(a);
Expand All @@ -113,7 +132,9 @@ show create table tm1;
Table Create Table
tm1 CREATE TABLE `tm1` (\n `a` int NOT NULL,\n `b` int DEFAULT NULL,\n `c` int DEFAULT NULL\n)
delete from ti2 where c = 1;

delete from tm2 where c = 1;

alter table ti1 drop foreign key fi1;
alter table for temporary table is not yet implemented
alter table tm1 drop foreign key fm1;
Expand All @@ -125,11 +146,17 @@ show create table tm1;
Table Create Table
tm1 CREATE TABLE `tm1` (\n `a` int NOT NULL,\n `b` int DEFAULT NULL,\n `c` int DEFAULT NULL\n)
delete from ti2 where c = 1;

delete from tm2 where c = 1;

drop table ti1;

drop table tm1;

drop table ti2;

drop table tm2;

drop table if exists index01;
create table index01(col1 int,key key1(col1));
show index from index01;
Expand Down Expand Up @@ -556,6 +583,18 @@ id content
show create table t5;
Table Create Table
t5 CREATE TABLE `t5` (\n `id` int NOT NULL,\n `content` varchar(1000) DEFAULT NULL,\n PRIMARY KEY (`id`)\n)
begin;
alter table t5 rename column content to new_content;
select mo_ctl('dn', 'flush', 'mo_catalog.mo_columns');
mo_ctl(dn, flush, mo_catalog.mo_columns)
{\n "method": "Flush",\n "result": [\n {\n "returnStr": "OK"\n }\n ]\n}\n
select sleep(0.51);
sleep(0.51)
0
commit;
show create table t5;
Table Create Table
t5 CREATE TABLE `t5` (\n `id` int NOT NULL,\n `new_content` varchar(1000) DEFAULT NULL,\n PRIMARY KEY (`id`)\n)
drop table t1;
drop table t2;
drop table t3;
Expand Down
14 changes: 14 additions & 0 deletions test/distributed/cases/ddl/alter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,20 @@ alter table t5 modify column content varchar(1000);
select * from t5;
show create table t5;

begin;

alter table t5 rename column content to new_content;

-- @separator:table
select mo_ctl('dn', 'flush', 'mo_catalog.mo_columns');

select sleep(0.51);

commit;

show create table t5;


-- Cleanup
drop table t1;
drop table t2;
Expand Down
4 changes: 2 additions & 2 deletions test/distributed/cases/view/system_view.result
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
begin;
select sleep(10);
sleep(10)
select sleep(5);
sleep(5)
0
select count(*) > 0 from mo_sessions() t;
count(*) > 0
Expand Down
4 changes: 2 additions & 2 deletions test/distributed/cases/view/system_view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-- @session:id=1{
begin;
select sleep(10);
select sleep(5);
-- @session}

select count(*) > 0 from mo_sessions() t;
Expand All @@ -24,4 +24,4 @@ select count(*) >0 from mo_configurations() t;
select count(*) >0 from mo_configurations() t where node_type = 'cn';

select distinct node_type,default_value from mo_configurations() t where name like '%frontend.port';
select count(*) > 0 from mo_configurations() t where internal = 'advanced';
select count(*) > 0 from mo_configurations() t where internal = 'advanced';
Loading