-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,8 +173,24 @@ 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? | ||
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) + "]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This does not apply any special treatments to the OBJECT keys yet, and I did not perform any further orientation flights yet. For example, what happens if the key is a reserved keyword, or contains special characters like dashes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Depending on your spirits, we may want to defer further special handling to a subsequent iteration, and first check if the use case at hand already gets satisfied well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is about the other cases where the array element isn't a dict? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They absolutely need to be taken care of, right. Currently, as far as I can see through knowing about data payloads at hand, it might not be of current concerns yet. |
||
|
||
constraint = f"{self.DATA_COLUMN}['{key_name}'] = {key_value}" | ||
constraints.append(constraint) | ||
|
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.
Single quotes must always be escaped as they are used for any text literals at CrateDB.
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.
Thanks. So that serialization will need further improvements to treat OBJECT values the same like above.