Skip to content

Commit

Permalink
DynamoDB: Fix serializing ARRAYs of OBJECTs using a cast to OBJECT[]
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Motl authored and amotl committed Aug 23, 2024
1 parent 5ab7848 commit f382455
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
16 changes: 2 additions & 14 deletions src/commons_codec/transform/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,8 @@ def values_to_update(self, keys: t.Dict[str, t.Dict[str, str]]) -> str:
# TODO: Does it also need escaping of inner TEXT values, like the above?
key_value = "'" + json.dumps(key_value) + "'::OBJECT"

# TODO: ARRAY types do not support JSON syntax yet.
# Let's relay them 1:1, which works for primitive inner types,
# but complex ones need special treatment, like representing
# OBJECTs in CrateDB-native syntax.
# FIXME: Find a way to use a custom JSONEncoder for that, in order to
# fully support also nested elements of those.
# https://github.com/crate/commons-codec/issues/33
elif isinstance(key_value, list):
if key_value:
if isinstance(key_value[0], dict):
items = []
for item in key_value:
items.append("{" + ", ".join(f"{key}='{value}'" for key, value in item.items()) + "}")
key_value = "[" + ",".join(items) + "]"
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
2 changes: 1 addition & 1 deletion tests/transform/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_decode_cdc_modify_nested():
"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}'::OBJECT,"
" data['list_of_objects'] = [{foo='bar'},{baz='qux'}]"
' data[\'list_of_objects\'] = \'[{"foo": "bar"}, {"baz": "qux"}]\'::OBJECT[]'
" WHERE data['device'] = 'foo' AND data['timestamp'] = '2024-07-12T01:17:42';"
)

Expand Down

0 comments on commit f382455

Please sign in to comment.