Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DDL] Filter for valid columns on CreateTable #1068

Merged
merged 5 commits into from
Dec 2, 2024
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
2 changes: 1 addition & 1 deletion clients/shared/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Merge(ctx context.Context, dwh destination.DataWarehouse, tableData *optimi
// TODO: Examine whether [AuditColumnsToDelete] still needs to be called.
tableConfig.AuditColumnsToDelete(srcKeysMissing)
if err = tableData.MergeColumnsFromDestination(tableConfig.GetColumns()...); err != nil {
return fmt.Errorf("failed to merge columns from destination: %w", err)
return fmt.Errorf("failed to merge columns from destination: %w for table %q", err, tableData.Name())
}

temporaryTableID := TempTableIDWithSuffix(dwh.IdentifierFor(tableData.TopicConfig(), tableData.Name()), tableData.TempTableSuffix())
Expand Down
32 changes: 21 additions & 11 deletions clients/shared/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,29 @@ import (
"github.com/artie-labs/transfer/lib/typing/columns"
)

func getValidColumns(cols []columns.Column) []columns.Column {
var validCols []columns.Column
for _, col := range cols {
if col.ShouldSkip() {
continue
}

validCols = append(validCols, col)
}

return validCols
}

func CreateTempTable(ctx context.Context, dwh destination.DataWarehouse, tableData *optimization.TableData, tc *types.DwhTableConfig, settings config.SharedDestinationColumnSettings, tableID sql.TableIdentifier) error {
return CreateTable(ctx, dwh, tableData, tc, settings, tableID, true, tableData.ReadOnlyInMemoryCols().GetColumns())
}

func CreateTable(ctx context.Context, dwh destination.DataWarehouse, tableData *optimization.TableData, tc *types.DwhTableConfig, settings config.SharedDestinationColumnSettings, tableID sql.TableIdentifier, tempTable bool, cols []columns.Column) error {
cols = getValidColumns(cols)
if len(cols) == 0 {
return nil
}

query, err := ddl.BuildCreateTableSQL(settings, dwh.Dialect(), tableID, tempTable, tableData.Mode(), cols)
if err != nil {
return fmt.Errorf("failed to build create table sql: %w", err)
Expand All @@ -37,20 +55,12 @@ func CreateTable(ctx context.Context, dwh destination.DataWarehouse, tableData *
}

func AlterTableAddColumns(ctx context.Context, dwh destination.DataWarehouse, tc *types.DwhTableConfig, settings config.SharedDestinationColumnSettings, tableID sql.TableIdentifier, cols []columns.Column) error {
cols = getValidColumns(cols)
if len(cols) == 0 {
return nil
}

var colsToAdd []columns.Column
for _, col := range cols {
if col.ShouldSkip() {
continue
}

colsToAdd = append(colsToAdd, col)
}

sqlParts, err := ddl.BuildAlterTableAddColumns(settings, dwh.Dialect(), tableID, colsToAdd)
sqlParts, err := ddl.BuildAlterTableAddColumns(settings, dwh.Dialect(), tableID, cols)
if err != nil {
return fmt.Errorf("failed to build alter table add columns: %w", err)
}
Expand All @@ -64,7 +74,7 @@ func AlterTableAddColumns(ctx context.Context, dwh destination.DataWarehouse, tc
}
}

tc.MutateInMemoryColumns(constants.Add, colsToAdd...)
tc.MutateInMemoryColumns(constants.Add, cols...)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion lib/destination/ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func BuildCreateTableSQL(settings config.SharedDestinationColumnSettings, dialec
var primaryKeys []string
for _, col := range columns {
if col.ShouldSkip() {
continue
// It should be filtered upstream
return "", fmt.Errorf("received an invalid column %q", col.Name())
}

colName := dialect.QuoteIdentifier(col.Name())
Expand Down