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

[dml] Reuse quoteColumns for primary keys #559

Merged
merged 1 commit into from
May 3, 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
15 changes: 5 additions & 10 deletions lib/destination/dml/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func removeDeleteColumnMarker(cols []columns.Column) ([]columns.Column, bool) {
return cols, len(cols) != origLength
}

func (m *MergeArgument) buildInsertQuery(columns []columns.Column, equalitySQLParts []string) string {
func (m *MergeArgument) buildRedshiftInsertQuery(columns []columns.Column, equalitySQLParts []string) string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this too.

return fmt.Sprintf(`INSERT INTO %s (%s) SELECT %s FROM %s as cc LEFT JOIN %s as c on %s WHERE c.%s IS NULL;`,
// insert into target (col1, col2, col3)
m.TableID.FullyQualifiedName(), strings.Join(quoteColumns(columns, m.Dialect), ","),
Expand Down Expand Up @@ -131,7 +131,7 @@ func (m *MergeArgument) GetParts() ([]string, error) {
if m.SoftDelete {
return []string{
// INSERT
m.buildInsertQuery(m.Columns, equalitySQLParts),
m.buildRedshiftInsertQuery(m.Columns, equalitySQLParts),
// UPDATE
fmt.Sprintf(`UPDATE %s as c SET %s FROM %s as cc WHERE %s%s;`,
// UPDATE table set col1 = cc. col1
Expand All @@ -148,14 +148,9 @@ func (m *MergeArgument) GetParts() ([]string, error) {
return nil, errors.New("artie delete flag doesn't exist")
}

var pks []string
for _, pk := range m.PrimaryKeys {
pks = append(pks, m.Dialect.QuoteIdentifier(pk.Name()))
}

parts := []string{
// INSERT
m.buildInsertQuery(columns, equalitySQLParts),
m.buildRedshiftInsertQuery(columns, equalitySQLParts),
// UPDATE
fmt.Sprintf(`UPDATE %s as c SET %s FROM %s as cc WHERE %s%s AND COALESCE(cc.%s, false) = false;`,
// UPDATE table set col1 = cc. col1
Expand All @@ -170,10 +165,10 @@ func (m *MergeArgument) GetParts() ([]string, error) {
// DELETE
fmt.Sprintf(`DELETE FROM %s WHERE (%s) IN (SELECT %s FROM %s as cc WHERE cc.%s = true);`,
// DELETE from table where (pk_1, pk_2)
m.TableID.FullyQualifiedName(), strings.Join(pks, ","),
m.TableID.FullyQualifiedName(), strings.Join(quoteColumns(m.PrimaryKeys, m.Dialect), ","),
// IN (cc.pk_1, cc.pk_2) FROM staging
array.StringsJoinAddPrefix(array.StringsJoinAddPrefixArgs{
Vals: pks,
Vals: quoteColumns(m.PrimaryKeys, m.Dialect),
Separator: ",",
Prefix: "cc.",
}), m.SubQuery, m.Dialect.QuoteIdentifier(constants.DeleteColumnMarker),
Expand Down
2 changes: 1 addition & 1 deletion lib/destination/dml/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,6 @@ func TestBuildInsertQuery(t *testing.T) {
}
assert.Equal(t,
`INSERT INTO {TABLE_ID} ("COL1","COL2") SELECT cc."COL1",cc."COL2" FROM {SUB_QUERY} as cc LEFT JOIN {TABLE_ID} as c on {EQUALITY_PART_1} and {EQUALITY_PART_2} WHERE c."COL1" IS NULL;`,
mergeArg.buildInsertQuery(cols, []string{"{EQUALITY_PART_1}", "{EQUALITY_PART_2}"}),
mergeArg.buildRedshiftInsertQuery(cols, []string{"{EQUALITY_PART_1}", "{EQUALITY_PART_2}"}),
)
}