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: Wrap all values in UPDATE statements in quotes #19

Merged
merged 1 commit into from
Aug 16, 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: Fixed a syntax issue with `text` data type in `UPDATE` statements

## 2024/08/16 v0.0.6
- Changed `UPDATE` statements from DMS not to write the entire `data`
Expand Down
6 changes: 4 additions & 2 deletions src/commons_codec/transform/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ def values_to_update(self, keys: t.Dict[str, t.Dict[str, str]]) -> str:
{'humidity': {'N': '84.84'}, 'temperature': {'N': '55.66'}}

OUT:
data['humidity] = 84.84, temperature = 55.66
data['humidity] = '84.84', temperature = '55.66'
"""
values_clause = self.deserialize_item(keys)

constraints: t.List[str] = []
for key_name, key_value in values_clause.items():
constraint = f"{self.DATA_COLUMN}['{key_name}'] = {key_value}"
key_value = str(key_value).replace("'", "''")
constraint = f"{self.DATA_COLUMN}['{key_name}'] = '{key_value}'"

Choose a reason for hiding this comment

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

We may need to replace ' with ''

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. Or we use some other notation, like $$. Even though that will not eliminate the need for escaping.

Choose a reason for hiding this comment

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

Suggested change
constraint = f"{self.DATA_COLUMN}['{key_name}'] = '{key_value}'"
key_value = str(key_value).replace("'", "''")
constraint = f"{self.DATA_COLUMN}['{key_name}'] = '{key_value}'"

Copy link
Member Author

Choose a reason for hiding this comment

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

We can also check if there is a reusable method in sqlalchemy or crate-python, which would already have sufficient test coverage. And such a method would be easier to re-use, since we will need it several times.

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Hi. Thanks for tagging.

We are talking about quoting values on this spot, right? We provided a generic quoting function for schema- and table-names per DatabaseAdapter.quote_relation_name, based on SQLAlchemy's identifier preparer.

So, Nomen est omen, the identifier preparer is about quoting identifiers. I will have a look if there is something similar for values.

Copy link
Member

Choose a reason for hiding this comment

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

On this spot, is it about proper quoting, or rather about escaping special characters within the value string at hand?

Copy link
Member

Choose a reason for hiding this comment

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

I think that this SO conversation includes good and relevant content and pointers? Please correct me if I'm looking at the wrong thing.

-- https://stackoverflow.com/questions/25104259/how-to-properly-escape-strings-when-manually-building-sql-queries-in-sqlalchemy

Copy link
Member

Choose a reason for hiding this comment

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

I will be merging as-is, including @hlcianfagna's suggestion. Generalizing and improving the ad hoc implementation, I am deferring to a subsequent iteration.

constraints.append(constraint)

return ", ".join(constraints)

def keys_to_where(self, keys: t.Dict[str, t.Dict[str, str]]) -> str:
Expand Down
4 changes: 3 additions & 1 deletion tests/transform/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@
"humidity": {"N": "84.84"},
"temperature": {"N": "55.66"},
"device": {"S": "bar"},
"location": {"S": "Sydney"},
"timestamp": {"S": "2024-07-12T01:17:42"},
},
"OldImage": {
"humidity": {"N": "84.84"},
"temperature": {"N": "42.42"},
"device": {"S": "foo"},
"location": {"S": "Sydney"},
"timestamp": {"S": "2024-07-12T01:17:42"},
},
"SizeBytes": 161,
Expand Down Expand Up @@ -145,7 +147,7 @@ def test_decode_cdc_insert_nested():
def test_decode_cdc_modify():
assert (
DynamoCDCTranslatorCrateDB(table_name="foo").to_sql(MSG_MODIFY) == 'UPDATE "foo" '
"SET data['humidity'] = 84.84, data['temperature'] = 55.66 "
"SET data['humidity'] = '84.84', data['temperature'] = '55.66', data['location'] = 'Sydney' "
"WHERE data['device'] = 'foo' AND data['timestamp'] = '2024-07-12T01:17:42';"
)

Expand Down
Loading