From 97409a51fc2bf880c43f23526497df3eae99532e Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Dec 2024 13:08:15 -0800 Subject: [PATCH 1/6] More. --- lib/antlr/create_table.go | 8 ++++++-- lib/antlr/create_table_test.go | 33 +++++++++++++++++++++++---------- lib/antlr/drop_table_test.go | 10 ++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/antlr/create_table.go b/lib/antlr/create_table.go index 9628fe186..c51e1b213 100644 --- a/lib/antlr/create_table.go +++ b/lib/antlr/create_table.go @@ -109,12 +109,16 @@ func processCopyTable(ctx *generated.CopyCreateTableContext) (Event, error) { return nil, fmt.Errorf("expected exactly 2 table names, got %d", len(tableNames)) } - tableName, err := getTextFromSingleNodeBranch(tableNames[0]) + for _, cc := range tableNames[0].GetChildren() { + fmt.Println("cc", cc, "type", fmt.Sprintf("%T", cc), "cc text", cc.(antlr.ParseTree).GetText()) + } + + tableName, err := getTableNameFromNode(tableNames[0]) if err != nil { return nil, err } - copiedFromTableName, err := getTextFromSingleNodeBranch(tableNames[1]) + copiedFromTableName, err := getTableNameFromNode(tableNames[1]) if err != nil { return nil, err } diff --git a/lib/antlr/create_table_test.go b/lib/antlr/create_table_test.go index 32e4d6e40..e2aa770d2 100644 --- a/lib/antlr/create_table_test.go +++ b/lib/antlr/create_table_test.go @@ -9,16 +9,10 @@ import ( func TestCreateTable(t *testing.T) { { - // Create table LIKE - sameQueries := []string{ - "CREATE TABLE table_name LIKE other_table;", - "create table table_name (like other_table);", - } - - for _, query := range sameQueries { - events, err := Parse(query) + { + // Create table LIKE by specifying schema + events, err := Parse("CREATE TABLE db_name.table_name LIKE db_name.other_table;") assert.NoError(t, err) - assert.Len(t, events, 1) createTableEvent, isOk := events[0].(CopyTableEvent) assert.True(t, isOk) @@ -27,7 +21,26 @@ func TestCreateTable(t *testing.T) { assert.Len(t, createTableEvent.GetColumns(), 0) assert.Equal(t, "other_table", createTableEvent.GetCopyFromTableName()) } - + { + // Create table LIKE + sameQueries := []string{ + "CREATE TABLE table_name LIKE other_table;", + "create table table_name (like other_table);", + } + + for _, query := range sameQueries { + events, err := Parse(query) + assert.NoError(t, err) + assert.Len(t, events, 1) + + createTableEvent, isOk := events[0].(CopyTableEvent) + assert.True(t, isOk) + + assert.Equal(t, "table_name", createTableEvent.GetTable()) + assert.Len(t, createTableEvent.GetColumns(), 0) + assert.Equal(t, "other_table", createTableEvent.GetCopyFromTableName()) + } + } } { // Create table with column as CHARACTER SET and collation specified at the column level diff --git a/lib/antlr/drop_table_test.go b/lib/antlr/drop_table_test.go index cf409a784..9d4c29ee1 100644 --- a/lib/antlr/drop_table_test.go +++ b/lib/antlr/drop_table_test.go @@ -7,6 +7,16 @@ import ( ) func TestDropTable(t *testing.T) { + { + // Drop table specify schema + events, err := Parse("DROP TABLE db_name.table_name;") + assert.NoError(t, err) + assert.Len(t, events, 1) + + dropTableEvent, isOk := events[0].(DropTableEvent) + assert.True(t, isOk) + assert.Equal(t, "table_name", dropTableEvent.GetTable()) + } { // Single drop table for _, tblName := range []string{"table_name", "`table_name`", "db_name.table_name", "`db_name`.`table_name`"} { From d584a44074310f1c59af725238548398952a104e Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Dec 2024 13:09:53 -0800 Subject: [PATCH 2/6] Update. --- lib/antlr/rename_table.go | 7 ++++++- lib/antlr/rename_table_test.go | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/antlr/rename_table.go b/lib/antlr/rename_table.go index b6f3450f1..d82d30057 100644 --- a/lib/antlr/rename_table.go +++ b/lib/antlr/rename_table.go @@ -13,7 +13,12 @@ func processRenameTable(ctx *generated.RenameTableContext) ([]Event, error) { case *generated.RenameTableClauseContext: var allTableNames []string for _, tableName := range castedChild.AllTableName() { - allTableNames = append(allTableNames, tableName.GetText()) + parsedTableName, err := getTableNameFromNode(tableName) + if err != nil { + return nil, fmt.Errorf("failed to get table name: %w", err) + } + + allTableNames = append(allTableNames, parsedTableName) } // Must be at least two table names diff --git a/lib/antlr/rename_table_test.go b/lib/antlr/rename_table_test.go index afae455ca..fc36e8454 100644 --- a/lib/antlr/rename_table_test.go +++ b/lib/antlr/rename_table_test.go @@ -20,15 +20,15 @@ func TestRenameTable(t *testing.T) { } { // Another one table variant - events, err := Parse(`RENAME TABLE current_db.tbl_name TO other_db.tbl_name;`) + events, err := Parse(`RENAME TABLE current_db.tbl_name TO current_db.tbl_name;`) assert.NoError(t, err) assert.Len(t, events, 1) renameTableEvent, isOk := events[0].(RenameTableEvent) assert.True(t, isOk) - assert.Equal(t, "current_db.tbl_name", renameTableEvent.GetTable()) - assert.Equal(t, "other_db.tbl_name", renameTableEvent.GetNewTableName()) + assert.Equal(t, "tbl_name", renameTableEvent.GetTable()) + assert.Equal(t, "tbl_name", renameTableEvent.GetNewTableName()) } { // Multiple tables From 68e6976b41b62afc5b62847e03c39c13f176768f Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Dec 2024 13:11:08 -0800 Subject: [PATCH 3/6] Clean up. --- lib/antlr/create_table.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/antlr/create_table.go b/lib/antlr/create_table.go index c51e1b213..1c70b65b2 100644 --- a/lib/antlr/create_table.go +++ b/lib/antlr/create_table.go @@ -108,11 +108,7 @@ func processCopyTable(ctx *generated.CopyCreateTableContext) (Event, error) { if len(tableNames) != 2 { return nil, fmt.Errorf("expected exactly 2 table names, got %d", len(tableNames)) } - - for _, cc := range tableNames[0].GetChildren() { - fmt.Println("cc", cc, "type", fmt.Sprintf("%T", cc), "cc text", cc.(antlr.ParseTree).GetText()) - } - + tableName, err := getTableNameFromNode(tableNames[0]) if err != nil { return nil, err From a8b6a9ca100241c15e2e647f5f33881ce8b41e9d Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Dec 2024 13:11:18 -0800 Subject: [PATCH 4/6] rm tab. --- lib/antlr/create_table.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/antlr/create_table.go b/lib/antlr/create_table.go index 1c70b65b2..3de1f93ba 100644 --- a/lib/antlr/create_table.go +++ b/lib/antlr/create_table.go @@ -108,7 +108,7 @@ func processCopyTable(ctx *generated.CopyCreateTableContext) (Event, error) { if len(tableNames) != 2 { return nil, fmt.Errorf("expected exactly 2 table names, got %d", len(tableNames)) } - + tableName, err := getTableNameFromNode(tableNames[0]) if err != nil { return nil, err From aabbe48164cb2a23c33e8d5369b7d633fd02198e Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Dec 2024 13:11:51 -0800 Subject: [PATCH 5/6] Remove. --- lib/antlr/drop_table_test.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/antlr/drop_table_test.go b/lib/antlr/drop_table_test.go index 9d4c29ee1..cf409a784 100644 --- a/lib/antlr/drop_table_test.go +++ b/lib/antlr/drop_table_test.go @@ -7,16 +7,6 @@ import ( ) func TestDropTable(t *testing.T) { - { - // Drop table specify schema - events, err := Parse("DROP TABLE db_name.table_name;") - assert.NoError(t, err) - assert.Len(t, events, 1) - - dropTableEvent, isOk := events[0].(DropTableEvent) - assert.True(t, isOk) - assert.Equal(t, "table_name", dropTableEvent.GetTable()) - } { // Single drop table for _, tblName := range []string{"table_name", "`table_name`", "db_name.table_name", "`db_name`.`table_name`"} { From 7d49deeed1758a5e6cb70feec7d57b761d7f7f4a Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Dec 2024 13:14:03 -0800 Subject: [PATCH 6/6] Clean up. --- lib/antlr/util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/antlr/util.go b/lib/antlr/util.go index 8b427f54e..e4aec2203 100644 --- a/lib/antlr/util.go +++ b/lib/antlr/util.go @@ -41,6 +41,7 @@ func getTextFromSingleNodeBranch(tree antlr.Tree) (string, error) { return getTextFromSingleNodeBranch(tree.GetChild(0)) } +// TODO: Extend this function to return the schema (if present) func getTableNameFromNode(ctx generated.ITableNameContext) (string, error) { children := ctx.GetChildren() if len(children) != 1 {