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

[snowflake] Use table alias constants for merge queries #660

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 15 additions & 17 deletions clients/snowflake/dialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,31 +216,29 @@ func (sd SnowflakeDialect) BuildMergeQueries(
// This is because Snowflake does not respect NS granularity.
var idempotentClause string
if idempotentKey != "" {
idempotentClause = fmt.Sprintf("AND cc.%s >= c.%s ", idempotentKey, idempotentKey)
idempotentClause = fmt.Sprintf("AND %s.%s >= %s.%s ", stagingAlias, idempotentKey, targetAlias, idempotentKey)
}

equalitySQLParts := sql.BuildColumnComparisons(primaryKeys, targetAlias, stagingAlias, sql.Equal, sd)

subQuery = fmt.Sprintf("( %s )", subQuery)

if len(additionalEqualityStrings) > 0 {
equalitySQLParts = append(equalitySQLParts, additionalEqualityStrings...)
}

if softDelete {
return []string{fmt.Sprintf(`
MERGE INTO %s c USING %s AS cc ON %s
MERGE INTO %s %s USING ( %s ) AS %s ON %s
WHEN MATCHED %sTHEN UPDATE SET %s
WHEN NOT MATCHED AND IFNULL(cc.%s, false) = false THEN INSERT (%s) VALUES (%s);`,
tableID.FullyQualifiedName(), subQuery, strings.Join(equalitySQLParts, " AND "),
WHEN NOT MATCHED AND IFNULL(%s.%s, false) = false THEN INSERT (%s) VALUES (%s);`,
tableID.FullyQualifiedName(), targetAlias, subQuery, stagingAlias, strings.Join(equalitySQLParts, " AND "),
// Update + Soft Deletion
idempotentClause, sql.BuildColumnsUpdateFragment(cols, stagingAlias, targetAlias, sd),
// Insert
sd.QuoteIdentifier(constants.DeleteColumnMarker), strings.Join(sql.QuoteColumns(cols, sd), ","),
stagingAlias, sd.QuoteIdentifier(constants.DeleteColumnMarker), strings.Join(sql.QuoteColumns(cols, sd), ","),
array.StringsJoinAddPrefix(array.StringsJoinAddPrefixArgs{
Vals: sql.QuoteColumns(cols, sd),
Separator: ",",
Prefix: "cc.",
Prefix: stagingAlias + ".",
}))}, nil
}

Expand All @@ -251,20 +249,20 @@ WHEN NOT MATCHED AND IFNULL(cc.%s, false) = false THEN INSERT (%s) VALUES (%s);`
}

return []string{fmt.Sprintf(`
MERGE INTO %s c USING %s AS cc ON %s
WHEN MATCHED AND cc.%s THEN DELETE
WHEN MATCHED AND IFNULL(cc.%s, false) = false %sTHEN UPDATE SET %s
WHEN NOT MATCHED AND IFNULL(cc.%s, false) = false THEN INSERT (%s) VALUES (%s);`,
tableID.FullyQualifiedName(), subQuery, strings.Join(equalitySQLParts, " AND "),
MERGE INTO %s %s USING ( %s ) AS %s ON %s
WHEN MATCHED AND %s.%s THEN DELETE
WHEN MATCHED AND IFNULL(%s.%s, false) = false %sTHEN UPDATE SET %s
WHEN NOT MATCHED AND IFNULL(%s.%s, false) = false THEN INSERT (%s) VALUES (%s);`,
tableID.FullyQualifiedName(), targetAlias, subQuery, stagingAlias, strings.Join(equalitySQLParts, " AND "),
// Delete
sd.QuoteIdentifier(constants.DeleteColumnMarker),
stagingAlias, sd.QuoteIdentifier(constants.DeleteColumnMarker),
// Update
sd.QuoteIdentifier(constants.DeleteColumnMarker), idempotentClause, sql.BuildColumnsUpdateFragment(cols, stagingAlias, targetAlias, sd),
stagingAlias, sd.QuoteIdentifier(constants.DeleteColumnMarker), idempotentClause, sql.BuildColumnsUpdateFragment(cols, stagingAlias, targetAlias, sd),
// Insert
sd.QuoteIdentifier(constants.DeleteColumnMarker), strings.Join(sql.QuoteColumns(cols, sd), ","),
stagingAlias, sd.QuoteIdentifier(constants.DeleteColumnMarker), strings.Join(sql.QuoteColumns(cols, sd), ","),
array.StringsJoinAddPrefix(array.StringsJoinAddPrefixArgs{
Vals: sql.QuoteColumns(cols, sd),
Separator: ",",
Prefix: "cc.",
Prefix: stagingAlias + ".",
}))}, nil
}