3636)
3737
3838TRANSACTION_MAX_ITEMS = 25
39+ SELECT_VALUES = [
40+ "SPECIFIC_ATTRIBUTES" ,
41+ "COUNT" ,
42+ "ALL_ATTRIBUTES" ,
43+ "ALL_PROJECTED_ATTRIBUTES" ,
44+ ]
3945
4046
4147def 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+
170228def 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 )
0 commit comments