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

DynamoDB: Fix serializing OBJECT and ARRAY representations to CrateDB #34

Merged
merged 2 commits into from
Aug 23, 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- DynamoDB: Fix serializing OBJECT and ARRAY representations to CrateDB

## 2024/08/22 v0.0.10
- DynamoDB: Fix `Map` representation to CrateDB.
Expand Down
8 changes: 6 additions & 2 deletions src/commons_codec/transform/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@ def values_to_update(self, keys: t.Dict[str, t.Dict[str, str]]) -> str:
elif isinstance(key_value, str):
key_value = "'" + str(key_value).replace("'", "''") + "'"

if isinstance(key_value, dict):
key_value = repr(json.dumps(key_value))
elif isinstance(key_value, dict):
# TODO: Does it also need escaping of inner TEXT values, like the above?
Copy link
Member

Choose a reason for hiding this comment

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

Single quotes must always be escaped as they are used for any text literals at CrateDB.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. So that serialization will need further improvements to treat OBJECT values the same like above.

key_value = "'" + json.dumps(key_value) + "'::OBJECT"

elif isinstance(key_value, list) and key_value and isinstance(key_value[0], dict):
key_value = "'" + json.dumps(key_value) + "'::OBJECT[]"

constraint = f"{self.DATA_COLUMN}['{key_name}'] = {key_value}"
constraints.append(constraint)
Expand Down
6 changes: 4 additions & 2 deletions tests/transform/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"test2": {"N": 2},
}
},
"list_of_objects": {"L": [{"M": {"foo": {"S": "bar"}}}, {"M": {"baz": {"S": "qux"}}}]},
},
"OldImage": {
"humidity": {"N": "84.84"},
Expand Down Expand Up @@ -224,9 +225,10 @@ def test_decode_cdc_modify_basic():
def test_decode_cdc_modify_nested():
assert (
DynamoCDCTranslatorCrateDB(table_name="foo").to_sql(MSG_MODIFY_NESTED) == 'UPDATE "foo" '
"SET data['tags'] = ['foo', 'bar'], data['empty_map'] = '{}', data['empty_list'] = [],"
"SET data['tags'] = ['foo', 'bar'], data['empty_map'] = '{}'::OBJECT, data['empty_list'] = [],"
" data['string_set'] = ['location_1'], data['number_set'] = [0.34, 1.0, 2.0, 3.0],"
" data['binary_set'] = ['U3Vubnk='], data['somemap'] = '{\"test\": 1.0, \"test2\": 2.0}'"
" data['binary_set'] = ['U3Vubnk='], data['somemap'] = '{\"test\": 1.0, \"test2\": 2.0}'::OBJECT,"
' data[\'list_of_objects\'] = \'[{"foo": "bar"}, {"baz": "qux"}]\'::OBJECT[]'
" WHERE data['device'] = 'foo' AND data['timestamp'] = '2024-07-12T01:17:42';"
)

Expand Down