Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Dec 19, 2024
1 parent 14f58c2 commit 75bc70a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/antlr/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func visit(tree antlr.Tree) ([]Event, error) {
return processAlterTable(ctx)
case *generated.DropTableContext:
return processDropTable(ctx)
case *generated.RenameTableContext:
return processRenameTable(ctx)
case
*generated.EmptyStatement_Context,
*generated.TruncateTableContext,
Expand Down
39 changes: 39 additions & 0 deletions lib/antlr/rename_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package antlr

import (
"fmt"
"github.com/artie-labs/reader/lib/antlr/generated"
"slices"
)

func processRenameTable(ctx *generated.RenameTableContext) ([]Event, error) {
var renameEvents []Event
for _, child := range ctx.GetChildren() {
switch castedChild := child.(type) {
case *generated.RenameTableClauseContext:
var allTableNames []string
for _, tableName := range castedChild.AllTableName() {
allTableNames = append(allTableNames, tableName.GetText())
}

// Must be at least two table names
if len(allTableNames) < 2 {
return nil, fmt.Errorf("expected at least 2 table names, got %d", len(allTableNames))
}

// Make sure it's divisible by 2
if len(allTableNames)%2 != 0 {
return nil, fmt.Errorf("unexpected number of table names: %d", len(allTableNames))
}

for group := range slices.Chunk(allTableNames, 2) {
renameEvents = append(renameEvents, RenameTableEvent{
tableName: group[0],
newTableName: group[1],
})
}
}
}

return renameEvents, nil
}
42 changes: 42 additions & 0 deletions lib/antlr/rename_table_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package antlr

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRenameTable(t *testing.T) {
{
// One table
events, err := Parse(`RENAME TABLE table_b TO table_a;`)
assert.NoError(t, err)
assert.Len(t, events, 1)

renameTableEvent, isOk := events[0].(RenameTableEvent)
assert.True(t, isOk)

assert.Equal(t, "table_b", renameTableEvent.GetTable())
assert.Equal(t, "table_a", renameTableEvent.GetNewTableName())
}
{
// Multiple tables
events, err := Parse(`RENAME TABLE old_table TO tmp_table, new_table TO old_table, tmp_table TO new_table;`)
assert.NoError(t, err)
assert.Len(t, events, 3)

renameTableEvent1, isOk := events[0].(RenameTableEvent)
assert.True(t, isOk)
assert.Equal(t, "old_table", renameTableEvent1.GetTable())
assert.Equal(t, "tmp_table", renameTableEvent1.GetNewTableName())

renameTableEvent2, isOk := events[1].(RenameTableEvent)
assert.True(t, isOk)
assert.Equal(t, "new_table", renameTableEvent2.GetTable())
assert.Equal(t, "old_table", renameTableEvent2.GetNewTableName())

renameTableEvent3, isOk := events[2].(RenameTableEvent)
assert.True(t, isOk)
assert.Equal(t, "tmp_table", renameTableEvent3.GetTable())
assert.Equal(t, "new_table", renameTableEvent3.GetNewTableName())
}
}
17 changes: 17 additions & 0 deletions lib/antlr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ func (a AfterPosition) Kind() string {
return "after"
}

type RenameTableEvent struct {
tableName string
newTableName string
}

func (r RenameTableEvent) GetTable() string {
return unescape(r.tableName)
}

func (r RenameTableEvent) GetNewTableName() string {
return unescape(r.newTableName)
}

func (r RenameTableEvent) GetColumns() []Column {
return nil
}

type CopyTableEvent struct {
tableName string
copyFromTableName string
Expand Down

0 comments on commit 75bc70a

Please sign in to comment.