Skip to content

Commit bef4271

Browse files
authored
fix(dynamodb): validate Select and honor Scan COUNT (#10175)
1 parent 4db88f3 commit bef4271

3 files changed

Lines changed: 392 additions & 6 deletions

File tree

‎moto/dynamodb/responses.py‎

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
)
3737

3838
TRANSACTION_MAX_ITEMS = 25
39+
SELECT_VALUES = [
40+
"SPECIFIC_ATTRIBUTES",
41+
"COUNT",
42+
"ALL_ATTRIBUTES",
43+
"ALL_PROJECTED_ATTRIBUTES",
44+
]
3945

4046

4147
def include_consumed_capacity(
@@ -167,6 +173,58 @@ def validate_attributes_used(
167173
)
168174

169175

176+
def validate_select(
177+
*,
178+
operation: str,
179+
select: str | None,
180+
projection_expression: str | None,
181+
attributes_to_get: list[str] | None,
182+
table: Table,
183+
index_name: str | None,
184+
) -> None:
185+
if select is None:
186+
return
187+
188+
if select not in SELECT_VALUES:
189+
raise MockValidationException(
190+
f"1 validation error detected: Value '{select}' at 'select' failed to satisfy constraint: Member must satisfy enum value set: [{', '.join(SELECT_VALUES)}]"
191+
)
192+
193+
validation_prefix = "1 validation error detected: " if operation == "Query" else ""
194+
195+
if select == "SPECIFIC_ATTRIBUTES" and not (
196+
projection_expression or attributes_to_get
197+
):
198+
raise MockValidationException(
199+
f"{validation_prefix}Must specify the AttributesToGet or ProjectionExpression when choosing to get SPECIFIC_ATTRIBUTES"
200+
)
201+
202+
if select != "SPECIFIC_ATTRIBUTES":
203+
selection_description = "only the Count" if select == "COUNT" else select
204+
if projection_expression:
205+
raise MockValidationException(
206+
f"{validation_prefix}Cannot specify the ProjectionExpression when choosing to get {selection_description}"
207+
)
208+
if attributes_to_get:
209+
raise MockValidationException(
210+
f"{validation_prefix}Cannot specify the AttributesToGet when choosing to get {selection_description}"
211+
)
212+
213+
if select == "ALL_PROJECTED_ATTRIBUTES" and index_name is None:
214+
raise MockValidationException(
215+
f"{validation_prefix}ALL_PROJECTED_ATTRIBUTES can be used only when Querying using an IndexName"
216+
)
217+
218+
if select == "ALL_ATTRIBUTES" and index_name:
219+
global_index = next(
220+
(index for index in table.global_indexes if index.name == index_name), None
221+
)
222+
if global_index and global_index.projection.get("ProjectionType") != "ALL":
223+
raise MockValidationException(
224+
f"One or more parameter values were invalid: Select type ALL_ATTRIBUTES is not supported for global secondary index {index_name} because its projection type is not ALL"
225+
)
226+
227+
170228
def check_projection_expression(expression: str) -> None:
171229
if expression.upper() in ReservedKeywords.get_reserved_keywords():
172230
raise MockValidationException(
@@ -962,6 +1020,15 @@ def query(self) -> ActionResult:
9621020
scan_index_forward = self.body.get("ScanIndexForward", True)
9631021
consistent_read = self.body.get("ConsistentRead", False)
9641022

1023+
validate_select(
1024+
operation="Query",
1025+
select=self.body.get("Select"),
1026+
projection_expression=projection_expression,
1027+
attributes_to_get=self.body.get("AttributesToGet"),
1028+
table=self.dynamodb_backend.get_table(name),
1029+
index_name=index_name,
1030+
)
1031+
9651032
items, scanned_count, last_evaluated_key = self.dynamodb_backend.query(
9661033
name,
9671034
hash_key,
@@ -986,7 +1053,7 @@ def query(self) -> ActionResult:
9861053
"ScannedCount": scanned_count,
9871054
}
9881055

989-
if self.body.get("Select", "").upper() != "COUNT":
1056+
if self.body.get("Select") != "COUNT":
9901057
result["Items"] = [item.attrs for item in items]
9911058

9921059
if last_evaluated_key is not None:
@@ -1056,6 +1123,15 @@ def scan(self) -> ActionResult:
10561123
expression_attribute_names, expression_attribute_names_used
10571124
)
10581125

1126+
validate_select(
1127+
operation="Scan",
1128+
select=self.body.get("Select"),
1129+
projection_expression=projection_expression,
1130+
attributes_to_get=self.body.get("AttributesToGet"),
1131+
table=self.dynamodb_backend.get_table(name),
1132+
index_name=index_name,
1133+
)
1134+
10591135
try:
10601136
items, scanned_count, last_evaluated_key = self.dynamodb_backend.scan(
10611137
name,
@@ -1073,11 +1149,12 @@ def scan(self) -> ActionResult:
10731149
except ValueError as err:
10741150
raise MockValidationException(f"Bad Filter Expression: {err}")
10751151

1076-
result = {
1152+
result: dict[str, Any] = {
10771153
"Count": len(items),
1078-
"Items": [item.attrs for item in items],
10791154
"ScannedCount": scanned_count,
10801155
}
1156+
if self.body.get("Select") != "COUNT":
1157+
result["Items"] = [item.attrs for item in items]
10811158
if last_evaluated_key is not None:
10821159
result["LastEvaluatedKey"] = last_evaluated_key
10831160
return DynamoResult(result)

‎tests/test_dynamodb/__init__.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def dynamodb_aws_verified(
1414
add_gsi: bool = False,
1515
add_gsi_range: bool = False,
1616
gsi_projection_type: str = "ALL",
17+
gsi_non_key_attributes: list[str] | None = None,
1718
add_lsi: bool = False,
1819
lsi_projection_type: str = "ALL",
1920
numeric_gsi_range: bool = False,
@@ -46,6 +47,14 @@ def create_table_and_test():
4647
range_type = "N" if numeric_range else "S"
4748
gsi_range_type = "N" if numeric_gsi_range else "S"
4849
lsi_range_type = "N" if numeric_lsi_range else "S"
50+
gsi_projection = {
51+
"ProjectionType": gsi_projection_type,
52+
**(
53+
{"NonKeyAttributes": gsi_non_key_attributes}
54+
if gsi_non_key_attributes
55+
else {}
56+
),
57+
}
4958

5059
db_kwargs = {
5160
"KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],
@@ -67,7 +76,7 @@ def create_table_and_test():
6776
"KeySchema": [
6877
{"AttributeName": "gsi_pk", "KeyType": "HASH"},
6978
],
70-
"Projection": {"ProjectionType": gsi_projection_type},
79+
"Projection": gsi_projection,
7180
"ProvisionedThroughput": {
7281
"ReadCapacityUnits": 1,
7382
"WriteCapacityUnits": 1,
@@ -85,7 +94,7 @@ def create_table_and_test():
8594
{"AttributeName": "gsi_pk", "KeyType": "HASH"},
8695
{"AttributeName": "gsi_sk", "KeyType": "RANGE"},
8796
],
88-
"Projection": {"ProjectionType": gsi_projection_type},
97+
"Projection": gsi_projection,
8998
"ProvisionedThroughput": {
9099
"ReadCapacityUnits": 1,
91100
"WriteCapacityUnits": 1,
@@ -107,7 +116,7 @@ def create_table_and_test():
107116
{"AttributeName": "gsi_sk", "KeyType": "RANGE"},
108117
{"AttributeName": "gsi_sk2", "KeyType": "RANGE"},
109118
],
110-
"Projection": {"ProjectionType": gsi_projection_type},
119+
"Projection": gsi_projection,
111120
"ProvisionedThroughput": {
112121
"ReadCapacityUnits": 1,
113122
"WriteCapacityUnits": 1,

0 commit comments

Comments
 (0)