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: KeyConditionExpression can have additional parentheses #8444

Merged
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
4 changes: 4 additions & 0 deletions moto/dynamodb/parsing/key_condition_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ def parse_expression(
# (hash_key = :id) and (sortkey = :sk)
# ^
if current_stage in [EXPRESSION_STAGES.INITIAL_STAGE]:
if not current_phrase:
# hashkey = :id and (begins_with(sortkey, :sk))
# ^
continue
if current_phrase not in ["begins_with", ""]:
raise MockValidationException(
f"Invalid KeyConditionExpression: Invalid function name; function: {current_phrase}"
Expand Down
25 changes: 25 additions & 0 deletions tests/test_dynamodb/test_dynamodb_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,3 +867,28 @@ def test_query_gsi_pagination_with_string_gsi_range_no_sk(table_name=None):
results = page1["Items"] + page2["Items"]
subjects = set([int(r["pk"]) for r in results])
assert subjects == set(range(10))


@pytest.mark.aws_verified
@dynamodb_aws_verified(add_range=True)
def test_key_condition_expression_with_brackets(table_name=None):
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
table = dynamodb.Table(table_name)

table.put_item(Item={"pk": "TEST", "sk": "aa"})
table.put_item(Item={"pk": "TEST", "sk": "ab"})
table.put_item(Item={"pk": "TEST", "sk": "ac"})

results = table.query(
KeyConditionExpression="(#0 = :0) AND (begins_with(#1, :1))",
ExpressionAttributeNames={"#0": "pk", "#1": "sk"},
ExpressionAttributeValues={":0": "TEST", ":1": "a"},
)
assert results["Count"] == 3

results = table.query(
KeyConditionExpression="(#0 = :0) AND (#1 BETWEEN :1 AND :2)",
ExpressionAttributeNames={"#0": "pk", "#1": "sk"},
ExpressionAttributeValues={":0": "TEST", ":1": "ab", ":2": "ab"},
)
assert results["Items"] == [{"pk": "TEST", "sk": "ab"}]
Loading