DynamoDB:query() KeyConditionExpression now allows enclosing the sort key condition in brackets (#6178)

This commit is contained in:
Cole Peters 2023-04-05 09:30:19 -05:00 committed by GitHub
parent db6874aec6
commit f424c6ac05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -160,8 +160,10 @@ def parse_expression(
if crnt_char == "(":
# hashkey = :id and begins_with( sortkey, :sk)
# ^ --> ^
# (hash_key = :id) and (sortkey = :sk)
# ^
if current_stage in [EXPRESSION_STAGES.INITIAL_STAGE]:
if current_phrase != "begins_with":
if current_phrase not in ["begins_with", ""]:
raise MockValidationException(
f"Invalid KeyConditionExpression: Invalid function name; function: {current_phrase}"
)

View File

@ -179,6 +179,24 @@ class TestHashAndRangeKey:
)
desired_hash_key.should.equal("pk")
@pytest.mark.parametrize(
"expr",
[
"(job_id = :id) and start_date = :sk",
"job_id = :id and (start_date = :sk)",
"(job_id = :id) and (start_date = :sk)",
],
)
def test_brackets(self, expr):
eav = {":id": "pk", ":sk1": "19", ":sk2": "21"}
desired_hash_key, comparison, range_values = parse_expression(
expression_attribute_values=eav,
key_condition_expression=expr,
schema=self.schema,
expression_attribute_names=dict(),
)
desired_hash_key.should.equal("pk")
class TestNamesAndValues:
schema = [{"AttributeName": "job_id", "KeyType": "HASH"}]