-
Notifications
You must be signed in to change notification settings - Fork 30
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
Move typing.UpdateQuery
to dml
#550
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package dml | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/config/constants" | ||
"github.com/artie-labs/transfer/lib/sql" | ||
"github.com/artie-labs/transfer/lib/typing" | ||
"github.com/artie-labs/transfer/lib/typing/columns" | ||
) | ||
|
||
// buildColumnsUpdateFragment will parse the columns and then returns a list of strings like: cc.first_name=c.first_name,cc.last_name=c.last_name,cc.email=c.email | ||
func buildColumnsUpdateFragment(c *columns.Columns, dialect sql.Dialect, skipDeleteCol bool) string { | ||
var cols []string | ||
for _, column := range c.GetColumns() { | ||
if column.ShouldSkip() { | ||
continue | ||
} | ||
|
||
// skipDeleteCol is useful because we don't want to copy the deleted column over to the source table if we're doing a hard row delete. | ||
if skipDeleteCol && column.Name() == constants.DeleteColumnMarker { | ||
continue | ||
} | ||
|
||
colName := dialect.QuoteIdentifier(column.Name()) | ||
if column.ToastColumn { | ||
if column.KindDetails == typing.Struct { | ||
cols = append(cols, processToastStructCol(colName, dialect)) | ||
} else { | ||
cols = append(cols, processToastCol(colName, dialect)) | ||
} | ||
|
||
} else { | ||
// This is to make it look like: objCol = cc.objCol | ||
cols = append(cols, fmt.Sprintf("%s=cc.%s", colName, colName)) | ||
} | ||
} | ||
|
||
return strings.Join(cols, ",") | ||
} | ||
|
||
func processToastStructCol(colName string, dialect sql.Dialect) string { | ||
switch dialect.(type) { | ||
case sql.BigQueryDialect: | ||
return fmt.Sprintf(`%s= CASE WHEN COALESCE(TO_JSON_STRING(cc.%s) != '{"key":"%s"}', true) THEN cc.%s ELSE c.%s END`, | ||
colName, colName, constants.ToastUnavailableValuePlaceholder, | ||
colName, colName) | ||
case sql.RedshiftDialect: | ||
return fmt.Sprintf(`%s= CASE WHEN COALESCE(cc.%s != JSON_PARSE('{"key":"%s"}'), true) THEN cc.%s ELSE c.%s END`, | ||
colName, colName, constants.ToastUnavailableValuePlaceholder, colName, colName) | ||
case sql.MSSQLDialect: | ||
// Microsoft SQL Server doesn't allow boolean expressions to be in the COALESCE statement. | ||
return fmt.Sprintf("%s= CASE WHEN COALESCE(cc.%s, {}) != {'key': '%s'} THEN cc.%s ELSE c.%s END", | ||
colName, colName, constants.ToastUnavailableValuePlaceholder, colName, colName) | ||
default: | ||
// TODO: Change this to Snowflake and error out if the destKind isn't supported so we're explicit. | ||
return fmt.Sprintf("%s= CASE WHEN COALESCE(cc.%s != {'key': '%s'}, true) THEN cc.%s ELSE c.%s END", | ||
colName, colName, constants.ToastUnavailableValuePlaceholder, colName, colName) | ||
} | ||
} | ||
|
||
func processToastCol(colName string, dialect sql.Dialect) string { | ||
if _, ok := dialect.(sql.MSSQLDialect); ok { | ||
// Microsoft SQL Server doesn't allow boolean expressions to be in the COALESCE statement. | ||
return fmt.Sprintf("%s= CASE WHEN COALESCE(cc.%s, '') != '%s' THEN cc.%s ELSE c.%s END", colName, colName, | ||
constants.ToastUnavailableValuePlaceholder, colName, colName) | ||
} else { | ||
return fmt.Sprintf("%s= CASE WHEN COALESCE(cc.%s != '%s', true) THEN cc.%s ELSE c.%s END", | ||
colName, colName, constants.ToastUnavailableValuePlaceholder, colName, colName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package dml | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/artie-labs/transfer/lib/config/constants" | ||
"github.com/artie-labs/transfer/lib/sql" | ||
"github.com/artie-labs/transfer/lib/typing" | ||
"github.com/artie-labs/transfer/lib/typing/columns" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildColumnsUpdateFragment(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
columns columns.Columns | ||
expectedString string | ||
dialect sql.Dialect | ||
skipDeleteCol bool | ||
} | ||
|
||
fooBarCols := []string{"foo", "bar"} | ||
|
||
var ( | ||
happyPathCols columns.Columns | ||
stringAndToastCols columns.Columns | ||
lastCaseColTypes columns.Columns | ||
lastCaseEscapeTypes columns.Columns | ||
) | ||
for _, col := range fooBarCols { | ||
column := columns.NewColumn(col, typing.String) | ||
column.ToastColumn = false | ||
happyPathCols.AddColumn(column) | ||
} | ||
for _, col := range fooBarCols { | ||
var toastCol bool | ||
if col == "foo" { | ||
toastCol = true | ||
} | ||
|
||
column := columns.NewColumn(col, typing.String) | ||
column.ToastColumn = toastCol | ||
stringAndToastCols.AddColumn(column) | ||
} | ||
|
||
lastCaseCols := []string{"a1", "b2", "c3"} | ||
for _, lastCaseCol := range lastCaseCols { | ||
kd := typing.String | ||
var toast bool | ||
// a1 - struct + toast, b2 - string + toast, c3 = regular string. | ||
if lastCaseCol == "a1" { | ||
kd = typing.Struct | ||
toast = true | ||
} else if lastCaseCol == "b2" { | ||
toast = true | ||
} | ||
|
||
column := columns.NewColumn(lastCaseCol, kd) | ||
column.ToastColumn = toast | ||
lastCaseColTypes.AddColumn(column) | ||
} | ||
|
||
lastCaseColsEsc := []string{"a1", "b2", "c3", "start", "select"} | ||
for _, lastCaseColEsc := range lastCaseColsEsc { | ||
kd := typing.String | ||
var toast bool | ||
// a1 - struct + toast, b2 - string + toast, c3 = regular string. | ||
if lastCaseColEsc == "a1" { | ||
kd = typing.Struct | ||
toast = true | ||
} else if lastCaseColEsc == "b2" { | ||
toast = true | ||
} else if lastCaseColEsc == "start" { | ||
kd = typing.Struct | ||
toast = true | ||
} | ||
|
||
column := columns.NewColumn(lastCaseColEsc, kd) | ||
column.ToastColumn = toast | ||
lastCaseEscapeTypes.AddColumn(column) | ||
} | ||
|
||
lastCaseEscapeTypes.AddColumn(columns.NewColumn(constants.DeleteColumnMarker, typing.Boolean)) | ||
|
||
key := `{"key":"__debezium_unavailable_value"}` | ||
testCases := []testCase{ | ||
{ | ||
name: "happy path", | ||
columns: happyPathCols, | ||
dialect: sql.RedshiftDialect{}, | ||
expectedString: `"foo"=cc."foo","bar"=cc."bar"`, | ||
}, | ||
{ | ||
name: "string and toast", | ||
columns: stringAndToastCols, | ||
dialect: sql.SnowflakeDialect{}, | ||
expectedString: `"FOO"= CASE WHEN COALESCE(cc."FOO" != '__debezium_unavailable_value', true) THEN cc."FOO" ELSE c."FOO" END,"BAR"=cc."BAR"`, | ||
}, | ||
{ | ||
name: "struct, string and toast string", | ||
columns: lastCaseColTypes, | ||
dialect: sql.RedshiftDialect{}, | ||
expectedString: `"a1"= CASE WHEN COALESCE(cc."a1" != JSON_PARSE('{"key":"__debezium_unavailable_value"}'), true) THEN cc."a1" ELSE c."a1" END,"b2"= CASE WHEN COALESCE(cc."b2" != '__debezium_unavailable_value', true) THEN cc."b2" ELSE c."b2" END,"c3"=cc."c3"`, | ||
}, | ||
{ | ||
name: "struct, string and toast string (bigquery)", | ||
columns: lastCaseColTypes, | ||
dialect: sql.BigQueryDialect{}, | ||
expectedString: "`a1`= CASE WHEN COALESCE(TO_JSON_STRING(cc.`a1`) != '{\"key\":\"__debezium_unavailable_value\"}', true) THEN cc.`a1` ELSE c.`a1` END,`b2`= CASE WHEN COALESCE(cc.`b2` != '__debezium_unavailable_value', true) THEN cc.`b2` ELSE c.`b2` END,`c3`=cc.`c3`", | ||
}, | ||
{ | ||
name: "struct, string and toast string (bigquery) w/ reserved keywords", | ||
columns: lastCaseEscapeTypes, | ||
dialect: sql.BigQueryDialect{}, | ||
expectedString: fmt.Sprintf("`a1`= CASE WHEN COALESCE(TO_JSON_STRING(cc.`a1`) != '%s', true) THEN cc.`a1` ELSE c.`a1` END,`b2`= CASE WHEN COALESCE(cc.`b2` != '__debezium_unavailable_value', true) THEN cc.`b2` ELSE c.`b2` END,`c3`=cc.`c3`,%s,%s", | ||
key, fmt.Sprintf("`start`= CASE WHEN COALESCE(TO_JSON_STRING(cc.`start`) != '%s', true) THEN cc.`start` ELSE c.`start` END", key), "`select`=cc.`select`"), | ||
skipDeleteCol: true, | ||
}, | ||
{ | ||
name: "struct, string and toast string (bigquery) w/ reserved keywords", | ||
columns: lastCaseEscapeTypes, | ||
dialect: sql.BigQueryDialect{}, | ||
expectedString: fmt.Sprintf("`a1`= CASE WHEN COALESCE(TO_JSON_STRING(cc.`a1`) != '%s', true) THEN cc.`a1` ELSE c.`a1` END,`b2`= CASE WHEN COALESCE(cc.`b2` != '__debezium_unavailable_value', true) THEN cc.`b2` ELSE c.`b2` END,`c3`=cc.`c3`,%s,%s", | ||
key, fmt.Sprintf("`start`= CASE WHEN COALESCE(TO_JSON_STRING(cc.`start`) != '%s', true) THEN cc.`start` ELSE c.`start` END", key), "`select`=cc.`select`,`__artie_delete`=cc.`__artie_delete`"), | ||
skipDeleteCol: false, | ||
}, | ||
} | ||
|
||
for _, _testCase := range testCases { | ||
actualQuery := buildColumnsUpdateFragment(&_testCase.columns, _testCase.dialect, _testCase.skipDeleteCol) | ||
assert.Equal(t, _testCase.expectedString, actualQuery, _testCase.name) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
lib/typing/columns/columns_toast_test.go → lib/destination/dml/columns_toast_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package columns | ||
package dml | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything below this line was copied directly. The only change was to the function definition.