Skip to content
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
31 changes: 31 additions & 0 deletions pkg/sql/plan/build_ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ type rootSQLCompilerContext struct {
calls int
}

func TestBuildRenameTableUsesPriorDestinationAsNextSource(t *testing.T) {
stmt, err := parsers.ParseOne(
t.Context(),
dialect.MYSQL,
"rename table t1 to t2, t2 to t3",
1,
)
require.NoError(t, err)
defer stmt.Free()

ctx := NewMockCompilerContext(false)
delete(ctx.tables, "t2")
delete(ctx.tables, "t3")
delete(ctx.objects, "t2")
delete(ctx.objects, "t3")
ctx.tables["t1"] = DeepCopyTableDef(ctx.tables["nation"], true)
ctx.tables["t1"].Name = "t1"
ctx.objects["t1"] = &ObjectRef{SchemaName: "tpch", ObjName: "t1"}

p, err := BuildPlan(ctx, stmt, false)
require.NoError(t, err)

renames := p.GetDdl().GetRenameTable().GetAlterTables()
require.Len(t, renames, 2)
require.Equal(t, "t1", renames[0].GetActions()[0].GetAlterName().GetOldName())
require.Equal(t, "t2", renames[0].GetActions()[0].GetAlterName().GetNewName())
require.Equal(t, "t2", renames[1].GetTableDef().GetName())
require.Equal(t, "t2", renames[1].GetActions()[0].GetAlterName().GetOldName())
require.Equal(t, "t3", renames[1].GetActions()[0].GetAlterName().GetNewName())
}

func TestBuildDropTemporaryTableOnlyTargetsTemporaryTable(t *testing.T) {
stmt, err := parsers.ParseOne(context.Background(), dialect.MYSQL, "drop temporary table nation", 1)
require.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions test/distributed/cases/ddl/rename_table_chained.result
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ id
select * from t_shadow;
id
100
create table t1 (a int, b int);
insert into t1 values (24499, 3);
rename table t1 to t2, t2 to t3;
select * from t3;
a b
24499 3
drop table t_live, t_shadow;
create table a (id int);
create table b (id int);
Expand Down
6 changes: 6 additions & 0 deletions test/distributed/cases/ddl/rename_table_chained.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ rename table t_live to t_tmp, t_shadow to t_live, t_tmp to t_shadow;
select * from t_live;
select * from t_shadow;

-- #24499: a destination can be the source of the next rename pair
create table t1 (a int, b int);
insert into t1 values (24499, 3);
rename table t1 to t2, t2 to t3;
select * from t3;

-- 2-pair rename (non-conflicting)
drop table t_live, t_shadow;
create table a (id int);
Expand Down
Loading