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 empty strings #30

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

## Unreleased
- DynamoDB: Fix `String Set` and `Number Set` representation for CrateDB
- DynamoDB: Fix serializing empty strings

## 2024/08/20 v0.0.8
- DynamoDB: Apply rough type evaluation and dispatching when computing
Expand Down
5 changes: 3 additions & 2 deletions src/commons_codec/transform/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import simplejson as json
import toolz

from commons_codec.util.data import is_container, is_number
from commons_codec.vendor.boto3.dynamodb.types import DYNAMODB_CONTEXT, TypeDeserializer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -169,7 +168,9 @@ def values_to_update(self, keys: t.Dict[str, t.Dict[str, str]]) -> str:

constraints: t.List[str] = []
for key_name, key_value in values_clause.items():
if not is_container(key_value) and not is_number(key_value):
if key_value is None:
key_value = "NULL"
elif isinstance(key_value, str):
key_value = "'" + str(key_value).replace("'", "''") + "'"
constraint = f"{self.DATA_COLUMN}['{key_name}'] = {key_value}"
constraints.append(constraint)
Expand Down
4 changes: 0 additions & 4 deletions src/commons_codec/util/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ def is_number(s):
pass

return False


def is_container(value):
return isinstance(value, (dict, list, set))
4 changes: 3 additions & 1 deletion tests/transform/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
"string_set": {"SS": ["location_1"]},
"number_set": {"NS": [1, 2, 3, 0.34]},
"binary_set": {"BS": ["U3Vubnk="]},
"empty_string": {"S": ""},
"null_string": {"S": None},
},
"OldImage": {
"humidity": {"N": "84.84"},
Expand Down Expand Up @@ -197,7 +199,7 @@ def test_decode_cdc_modify_basic():
DynamoCDCTranslatorCrateDB(table_name="foo").to_sql(MSG_MODIFY_BASIC) == 'UPDATE "foo" '
"SET data['humidity'] = 84.84, data['temperature'] = 55.66, data['location'] = 'Sydney', "
"data['string_set'] = ['location_1'], data['number_set'] = [0.34, 1.0, 2.0, 3.0],"
" data['binary_set'] = ['U3Vubnk=']"
" data['binary_set'] = ['U3Vubnk='], data['empty_string'] = '', data['null_string'] = NULL"
" WHERE data['device'] = 'foo' AND data['timestamp'] = '2024-07-12T01:17:42';"
)

Expand Down