Skip to content

Commit

Permalink
Add truncate table and drop table to dialects (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Dec 9, 2024
1 parent 0a635d1 commit c399574
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 11 deletions.
8 changes: 8 additions & 0 deletions clients/bigquery/dialect/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func (BigQueryDialect) BuildCreateTableQuery(tableID sql.TableIdentifier, tempor
}
}

func (BigQueryDialect) BuildDropTableQuery(tableID sql.TableIdentifier) string {
return "DROP TABLE IF EXISTS " + tableID.FullyQualifiedName()
}

func (BigQueryDialect) BuildTruncateTableQuery(tableID sql.TableIdentifier) string {
return "TRUNCATE TABLE " + tableID.FullyQualifiedName()
}

func (BigQueryDialect) BuildAddColumnQuery(tableID sql.TableIdentifier, sqlPart string) string {
return fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s", tableID.FullyQualifiedName(), sqlPart)
}
Expand Down
14 changes: 14 additions & 0 deletions clients/bigquery/dialect/dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ func TestBigQueryDialect_BuildCreateTableQuery(t *testing.T) {
)
}

func TestBigQueryDialect_BuildDropTableQuery(t *testing.T) {
assert.Equal(t,
"DROP TABLE IF EXISTS `project1`.`dataset2`.`table3`",
BigQueryDialect{}.BuildDropTableQuery(NewTableIdentifier("project1", "dataset2", "table3")),
)
}

func TestBigQueryDialect_BuildTruncateTableQuery(t *testing.T) {
assert.Equal(t,
"TRUNCATE TABLE `project1`.`dataset2`.`table3`",
BigQueryDialect{}.BuildTruncateTableQuery(NewTableIdentifier("project1", "dataset2", "table3")),
)
}

func TestBigQueryDialect_BuildDropColumnQuery(t *testing.T) {
fakeTableID := &mocks.FakeTableIdentifier{}
fakeTableID.FullyQualifiedNameReturns("{TABLE}")
Expand Down
8 changes: 8 additions & 0 deletions clients/databricks/dialect/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ func (DatabricksDialect) BuildCreateTableQuery(tableID sql.TableIdentifier, _ bo
return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s)", tableID.FullyQualifiedName(), strings.Join(colSQLParts, ", "))
}

func (DatabricksDialect) BuildDropTableQuery(tableID sql.TableIdentifier) string {
return "DROP TABLE IF EXISTS " + tableID.FullyQualifiedName()
}

func (DatabricksDialect) BuildTruncateTableQuery(tableID sql.TableIdentifier) string {
return "TRUNCATE TABLE " + tableID.FullyQualifiedName()
}

func (DatabricksDialect) BuildAddColumnQuery(tableID sql.TableIdentifier, sqlPart string) string {
return fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s", tableID.FullyQualifiedName(), sqlPart)
}
Expand Down
14 changes: 14 additions & 0 deletions clients/databricks/dialect/dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func TestDatabricksDialect_BuildCreateTableQuery(t *testing.T) {
}
}

func TestDatabricksDialect_BuildDropTableQuery(t *testing.T) {
assert.Equal(t,
"DROP TABLE IF EXISTS `project1`.`dataset2`.`table3`",
DatabricksDialect{}.BuildDropTableQuery(NewTableIdentifier("project1", "dataset2", "table3")),
)
}

func TestDatabricksDialect_BuildTruncateTableQuery(t *testing.T) {
assert.Equal(t,
"TRUNCATE TABLE `project1`.`dataset2`.`table3`",
DatabricksDialect{}.BuildTruncateTableQuery(NewTableIdentifier("project1", "dataset2", "table3")),
)
}

func TestDatabricksDialect_BuildAddColumnQuery(t *testing.T) {
fakeTableID := &mocks.FakeTableIdentifier{}
fakeTableID.FullyQualifiedNameReturns("{TABLE}")
Expand Down
8 changes: 8 additions & 0 deletions clients/mssql/dialect/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ func (MSSQLDialect) BuildCreateTableQuery(tableID sql.TableIdentifier, _ bool, c
// Microsoft SQL Server doesn't support IF NOT EXISTS
return fmt.Sprintf("CREATE TABLE %s (%s);", tableID.FullyQualifiedName(), strings.Join(colSQLParts, ","))
}

func (MSSQLDialect) BuildDropTableQuery(tableID sql.TableIdentifier) string {
return "DROP TABLE IF EXISTS " + tableID.FullyQualifiedName()
}

func (MSSQLDialect) BuildTruncateTableQuery(tableID sql.TableIdentifier) string {
return "TRUNCATE TABLE " + tableID.FullyQualifiedName()
}
14 changes: 14 additions & 0 deletions clients/mssql/dialect/dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ func TestMSSQLDialect_BuildCreateTableQuery(t *testing.T) {
)
}

func TestMSSQLDialect_BuildDropTableQuery(t *testing.T) {
assert.Equal(t,
`DROP TABLE IF EXISTS "schema1"."table1"`,
MSSQLDialect{}.BuildDropTableQuery(NewTableIdentifier("schema1", "table1")),
)
}

func TestMSSQLDialect_BuildTruncateTableQuery(t *testing.T) {
assert.Equal(t,
`TRUNCATE TABLE "schema1"."table1"`,
MSSQLDialect{}.BuildTruncateTableQuery(NewTableIdentifier("schema1", "table1")),
)
}

func TestMSSQLDialect_BuildAddColumnQuery(t *testing.T) {
fakeTableID := &mocks.FakeTableIdentifier{}
fakeTableID.FullyQualifiedNameReturns("{TABLE}")
Expand Down
30 changes: 19 additions & 11 deletions clients/redshift/dialect/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ func (RedshiftDialect) BuildDescribeTableQuery(tableID sql.TableIdentifier) (str

// This query is a modified fork from: https://gist.github.com/alexanderlz/7302623
return fmt.Sprintf(`
SELECT
SELECT
c.column_name,
CASE
WHEN c.data_type = 'numeric' THEN
CASE
WHEN c.data_type = 'numeric' THEN
'numeric(' || COALESCE(CAST(c.numeric_precision AS VARCHAR), '') || ',' || COALESCE(CAST(c.numeric_scale AS VARCHAR), '') || ')'
ELSE
ELSE
c.data_type
END AS data_type,
c.%s,
d.description
FROM
INFORMATION_SCHEMA.COLUMNS c
LEFT JOIN
PG_CLASS c1 ON c.table_name = c1.relname
LEFT JOIN
PG_CATALOG.PG_NAMESPACE n ON c.table_schema = n.nspname AND c1.relnamespace = n.oid
LEFT JOIN
PG_CATALOG.PG_DESCRIPTION d ON d.objsubid = c.ordinal_position AND d.objoid = c1.oid
WHERE
LEFT JOIN
PG_CLASS c1 ON c.table_name = c1.relname
LEFT JOIN
PG_CATALOG.PG_NAMESPACE n ON c.table_schema = n.nspname AND c1.relnamespace = n.oid
LEFT JOIN
PG_CATALOG.PG_DESCRIPTION d ON d.objsubid = c.ordinal_position AND d.objoid = c1.oid
WHERE
LOWER(c.table_schema) = LOWER($1) AND LOWER(c.table_name) = LOWER($2);
`, constants.StrPrecisionCol), []any{redshiftTableID.Schema(), redshiftTableID.Table()}, nil
}
Expand All @@ -52,3 +52,11 @@ func (RedshiftDialect) BuildCreateTableQuery(tableID sql.TableIdentifier, _ bool
// Redshift uses the same syntax for temporary and permanent tables.
return fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s);", tableID.FullyQualifiedName(), strings.Join(colSQLParts, ","))
}

func (RedshiftDialect) BuildDropTableQuery(tableID sql.TableIdentifier) string {
return "DROP TABLE IF EXISTS " + tableID.FullyQualifiedName()
}

func (RedshiftDialect) BuildTruncateTableQuery(tableID sql.TableIdentifier) string {
return "TRUNCATE TABLE " + tableID.FullyQualifiedName()
}
14 changes: 14 additions & 0 deletions clients/redshift/dialect/dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ func TestRedshiftDialect_BuildCreateTableQuery(t *testing.T) {
)
}

func TestRedshiftDialect_BuildDropTableQuery(t *testing.T) {
assert.Equal(t,
`DROP TABLE IF EXISTS schema1."table1"`,
RedshiftDialect{}.BuildDropTableQuery(NewTableIdentifier("schema1", "table1")),
)
}

func TestRedshiftDialect_BuildTruncateTableQuery(t *testing.T) {
assert.Equal(t,
`TRUNCATE TABLE schema1."table1"`,
RedshiftDialect{}.BuildTruncateTableQuery(NewTableIdentifier("schema1", "table1")),
)
}

func TestRedshiftDialect_BuildAddColumnQuery(t *testing.T) {
fakeTableID := &mocks.FakeTableIdentifier{}
fakeTableID.FullyQualifiedNameReturns("{TABLE}")
Expand Down
8 changes: 8 additions & 0 deletions clients/snowflake/dialect/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ func (SnowflakeDialect) BuildCreateTableQuery(tableID sql.TableIdentifier, tempo
}
}

func (SnowflakeDialect) BuildDropTableQuery(tableID sql.TableIdentifier) string {
return "DROP TABLE IF EXISTS " + tableID.FullyQualifiedName()
}

func (SnowflakeDialect) BuildTruncateTableQuery(tableID sql.TableIdentifier) string {
return "TRUNCATE TABLE IF EXISTS " + tableID.FullyQualifiedName()
}

func (SnowflakeDialect) BuildAddColumnQuery(tableID sql.TableIdentifier, sqlPart string) string {
return fmt.Sprintf("ALTER TABLE %s ADD COLUMN IF NOT EXISTS %s", tableID.FullyQualifiedName(), sqlPart)
}
Expand Down
14 changes: 14 additions & 0 deletions clients/snowflake/dialect/dialect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func TestSnowflakeDialect_BuildCreateTableQuery(t *testing.T) {
)
}

func TestSnowflakeDialect_BuildDropTableQuery(t *testing.T) {
assert.Equal(t,
`DROP TABLE IF EXISTS database1.schema1."TABLE1"`,
SnowflakeDialect{}.BuildDropTableQuery(NewTableIdentifier("database1", "schema1", "table1")),
)
}

func TestSnowflakeDialect_BuildTruncateTableQuery(t *testing.T) {
assert.Equal(t,
`TRUNCATE TABLE IF EXISTS database1.schema1."TABLE1"`,
SnowflakeDialect{}.BuildTruncateTableQuery(NewTableIdentifier("database1", "schema1", "table1")),
)
}

func TestSnowflakeDialect_BuildAddColumnQuery(t *testing.T) {
fakeTableID := &mocks.FakeTableIdentifier{}
fakeTableID.FullyQualifiedNameReturns("{TABLE}")
Expand Down
2 changes: 2 additions & 0 deletions lib/sql/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Dialect interface {
IsColumnAlreadyExistsErr(err error) bool
IsTableDoesNotExistErr(err error) bool
BuildCreateTableQuery(tableID TableIdentifier, temporary bool, colSQLParts []string) string
BuildDropTableQuery(tableID TableIdentifier) string
BuildTruncateTableQuery(tableID TableIdentifier) string
BuildDedupeQueries(tableID, stagingTableID TableIdentifier, primaryKeys []string, includeArtieUpdatedAt bool) []string
BuildDedupeTableQuery(tableID TableIdentifier, primaryKeys []string) string
BuildDescribeTableQuery(tableID TableIdentifier) (string, []any, error)
Expand Down

0 comments on commit c399574

Please sign in to comment.