From fd11161cfcaf5fb853629314837d7d6ab8fdda5b Mon Sep 17 00:00:00 2001 From: Cao Kai Date: Thu, 9 Jan 2025 11:12:21 +0800 Subject: [PATCH] use full db.table path in insert stmt --- pkg/sql/plan/build_ddl.go | 2 +- pkg/sql/plan/build_ddl_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/sql/plan/build_ddl.go b/pkg/sql/plan/build_ddl.go index a13f3f63d8478..acdfe45b36806 100644 --- a/pkg/sql/plan/build_ddl.go +++ b/pkg/sql/plan/build_ddl.go @@ -1293,7 +1293,7 @@ func buildTableDefs(stmt *tree.CreateTable, ctx CompilerContext, createTable *pl // insert into new_table select default_val1, default_val2, ..., * from (select clause); var insertSqlBuilder strings.Builder - insertSqlBuilder.WriteString(fmt.Sprintf("insert into `%s` select ", createTable.TableDef.Name)) + insertSqlBuilder.WriteString(fmt.Sprintf("insert into `%s`.`%s` select ", createTable.Database, createTable.TableDef.Name)) cols := createTable.TableDef.Cols firstCol := true diff --git a/pkg/sql/plan/build_ddl_test.go b/pkg/sql/plan/build_ddl_test.go index b106b0c54962a..40dc190b733a5 100644 --- a/pkg/sql/plan/build_ddl_test.go +++ b/pkg/sql/plan/build_ddl_test.go @@ -488,3 +488,36 @@ func TestParseDuration(t *testing.T) { assert.Equal(t, err, c.expectedErr) } } + +func Test_buildTableDefs(t *testing.T) { + stmt := &tree.CreateTable{ + Temporary: false, + IsClusterTable: false, + IfNotExists: false, + Table: tree.TableName{}, + Defs: nil, + Options: nil, + PartitionOption: nil, + ClusterByOption: nil, + Param: nil, + AsSource: &tree.Select{Select: &tree.SelectClause{From: &tree.From{}}}, + IsDynamicTable: false, + DTOptions: nil, + IsAsSelect: true, + IsAsLike: false, + LikeTableName: tree.TableName{}, + SubscriptionOption: nil, + } + + ctx := &MockCompilerContext{} + + createTable := &plan.CreateTable{ + Database: "db", + TableDef: &plan.TableDef{ + Name: "table", + }, + } + + err := buildTableDefs(stmt, ctx, createTable, nil) + assert.Error(t, err) +}