Skip to content

Commit bee35fe

Browse files
authored
Merge branch 'master' into dependabot/go_modules/golang.org/x/net-0.36.0
2 parents a5010f0 + 02d64d3 commit bee35fe

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

clients/redshift/dialect/default.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package dialect
22

3-
import "github.com/artie-labs/transfer/lib/sql"
3+
import (
4+
"fmt"
5+
6+
"github.com/artie-labs/transfer/lib/sql"
7+
)
48

59
func (RedshiftDialect) GetDefaultValueStrategy() sql.DefaultValueStrategy {
610
return sql.Backfill
711
}
12+
13+
func (RedshiftDialect) BuildBackfillQuery(tableID sql.TableIdentifier, escapedColumn string, defaultValue any) string {
14+
return fmt.Sprintf(`UPDATE %s SET %s = %v WHERE %s IS NULL;`, tableID.FullyQualifiedName(), escapedColumn, defaultValue, escapedColumn)
15+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dialect
2+
3+
import (
4+
"testing"
5+
6+
"github.com/artie-labs/transfer/lib/typing"
7+
"github.com/artie-labs/transfer/lib/typing/columns"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestRedshiftDialect_BuildBackfillQuery(t *testing.T) {
12+
_dialect := RedshiftDialect{}
13+
14+
tableID := NewTableIdentifier("{SCHEMA}", "{TABLE}")
15+
col := columns.NewColumn("{COLUMN}", typing.String)
16+
17+
assert.Equal(t, `UPDATE {SCHEMA}."{table}" SET "{column}" = {DEFAULT_VALUE} WHERE "{column}" IS NULL;`, _dialect.BuildBackfillQuery(tableID, _dialect.QuoteIdentifier(col.Name()), "{DEFAULT_VALUE}"))
18+
}

clients/shared/default_value.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
bigQueryDialect "github.com/artie-labs/transfer/clients/bigquery/dialect"
9+
redshiftDialect "github.com/artie-labs/transfer/clients/redshift/dialect"
910
"github.com/artie-labs/transfer/lib/destination"
1011
"github.com/artie-labs/transfer/lib/sql"
1112
"github.com/artie-labs/transfer/lib/typing"
@@ -72,23 +73,30 @@ func DefaultValue(column columns.Column, dialect sql.Dialect) (any, error) {
7273
}
7374

7475
func BackfillColumn(dest destination.Destination, column columns.Column, tableID sql.TableIdentifier) error {
75-
switch dest.Dialect().GetDefaultValueStrategy() {
76+
dialect := dest.Dialect()
77+
switch dialect.GetDefaultValueStrategy() {
7678
case sql.Backfill:
7779
if !column.ShouldBackfill() {
7880
// If we don't need to backfill, don't backfill.
7981
return nil
8082
}
8183

82-
defaultVal, err := DefaultValue(column, dest.Dialect())
84+
defaultVal, err := DefaultValue(column, dialect)
8385
if err != nil {
8486
return fmt.Errorf("failed to escape default value: %w", err)
8587
}
8688

87-
escapedCol := dest.Dialect().QuoteIdentifier(column.Name())
89+
escapedCol := dialect.QuoteIdentifier(column.Name())
8890
query := fmt.Sprintf(`UPDATE %s as t SET t.%s = %v WHERE t.%s IS NULL;`,
8991
// UPDATE table as t SET t.col = default_val WHERE t.col IS NULL
9092
tableID.FullyQualifiedName(), escapedCol, defaultVal, escapedCol,
9193
)
94+
95+
if rd, ok := dialect.(redshiftDialect.RedshiftDialect); ok {
96+
// Redshift UPDATE does not support table aliasing nor do we need it. Redshift will not throw an ambiguous error if the table and column name are the same.
97+
query = rd.BuildBackfillQuery(tableID, escapedCol, defaultVal)
98+
}
99+
92100
slog.Info("Backfilling column",
93101
slog.String("colName", column.Name()),
94102
slog.String("query", query),
@@ -100,7 +108,7 @@ func BackfillColumn(dest destination.Destination, column columns.Column, tableID
100108
}
101109

102110
query = fmt.Sprintf(`COMMENT ON COLUMN %s.%s IS '%v';`, tableID.FullyQualifiedName(), escapedCol, `{"backfilled": true}`)
103-
if _, ok := dest.Dialect().(bigQueryDialect.BigQueryDialect); ok {
111+
if _, ok := dialect.(bigQueryDialect.BigQueryDialect); ok {
104112
query = fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s SET OPTIONS (description=`%s`);",
105113
// ALTER TABLE table ALTER COLUMN col set OPTIONS (description=...)
106114
tableID.FullyQualifiedName(), escapedCol, `{"backfilled": true}`,

0 commit comments

Comments
 (0)