Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
Fix ddb ops (#724)
Nahuel Nucera7f40d523daa6
diff --git a/ministack/services/_dynamodb_keywords.py b/ministack/services/_dynamodb_keywords.pynew file mode 100644index 00000000..373538c8--- /dev/null+++ b/ministack/services/_dynamodb_keywords.py@@ -0,0 +1,69 @@+"""DynamoDB AWS system keyword list.++These names must be escaped with `#alias` in ConditionExpression,+UpdateExpression, FilterExpression, KeyConditionExpression, and+ProjectionExpression. Sourced from the AWS DynamoDB Developer Guide.+"""++AWS_KEYWORDS = frozenset((+"ABORT","ABSOLUTE","ACTION","ADD","AFTER","AGENT","AGGREGATE","ALL","ALLOCATE","ALTER",+"ANALYZE","AND","ANY","ARCHIVE","ARE","ARRAY","AS","ASC","ASCII","ASENSITIVE","ASSERTION",+"ASYMMETRIC","AT","ATOMIC","ATTACH","ATTRIBUTE","AUTH","AUTHORIZATION","AUTHORIZE","AUTO",+"AVG","BACK","BACKUP","BASE","BATCH","BEFORE","BEGIN","BETWEEN","BIGINT","BINARY","BIT",+"BLOB","BLOCK","BOOLEAN","BOTH","BREADTH","BUCKET","BULK","BY","BYTE","CALL","CALLED",+"CALLING","CAPACITY","CASCADE","CASCADED","CASE","CAST","CATALOG","CHAR","CHARACTER",+"CHECK","CLASS","CLOB","CLOSE","CLUSTER","CLUSTERED","CLUSTERING","CLUSTERS","COALESCE",+"COLLATE","COLLATION","COLLECTION","COLUMN","COLUMNS","COMBINE","COMMENT","COMMIT",+"COMPACT","COMPILE","COMPRESS","CONDITION","CONFLICT","CONNECT","CONNECTION","CONSISTENCY",+"CONSISTENT","CONSTRAINT","CONSTRAINTS","CONSTRUCTOR","CONSUMED","CONTINUE","CONVERT",+"COPY","CORRESPONDING","COUNT","COUNTER","CREATE","CROSS","CUBE","CURRENT","CURSOR","CYCLE",+"DATA","DATABASE","DATE","DATETIME","DAY","DEALLOCATE","DEC","DECIMAL","DECLARE","DEFAULT",+"DEFERRABLE","DEFERRED","DEFINE","DEFINED","DEFINITION","DELETE","DELIMITED","DEPTH","DEREF",+"DESC","DESCRIBE","DESCRIPTOR","DETACH","DETERMINISTIC","DIAGNOSTICS","DIRECTORIES",+"DISABLE","DISCONNECT","DISTINCT","DISTRIBUTE","DO","DOMAIN","DOUBLE","DROP","DUMP",+"DURATION","DYNAMIC","EACH","ELEMENT","ELSE","ELSEIF","EMPTY","ENABLE","END","EQUAL",+"EQUALS","ERROR","ESCAPE","ESCAPED","EVAL","EVALUATE","EXCEEDED","EXCEPT","EXCEPTION",+"EXCEPTIONS","EXCLUSIVE","EXEC","EXECUTE","EXISTS","EXIT","EXPLAIN","EXPLODE","EXPORT",+"EXPRESSION","EXTENDED","EXTERNAL","EXTRACT","FAIL","FALSE","FAMILY","FETCH","FIELDS",+"FILE","FILTER","FILTERING","FINAL","FINISH","FIRST","FIXED","FLATTERN","FLOAT","FOR",+"FORCE","FOREIGN","FORMAT","FORWARD","FOUND","FREE","FROM","FULL","FUNCTION","FUNCTIONS",+"GENERAL","GENERATE","GET","GLOB","GLOBAL","GO","GOTO","GRANT","GREATER","GROUP","GROUPING",+"HANDLER","HASH","HAVE","HAVING","HEAP","HIDDEN","HOLD","HOUR","IDENTIFIED","IDENTITY",+"IF","IGNORE","IMMEDIATE","IMPORT","IN","INCLUDING","INCLUSIVE","INCREMENT","INCREMENTAL",+"INDEX","INDEXED","INDEXES","INDICATOR","INFINITE","INITIALLY","INLINE","INNER","INNTER",+"INOUT","INPUT","INSENSITIVE","INSERT","INSTEAD","INT","INTEGER","INTERSECT","INTERVAL",+"INTO","INVALIDATE","IS","ISOLATION","ITEM","ITEMS","ITERATE","JOIN","KEY","KEYS","LAG",+"LANGUAGE","LARGE","LAST","LATERAL","LEAD","LEADING","LEAVE","LEFT","LENGTH","LESS","LEVEL",+"LIKE","LIMIT","LIMITED","LINES","LIST","LOAD","LOCAL","LOCALTIME","LOCALTIMESTAMP",+"LOCATION","LOCATOR","LOCK","LOCKS","LOG","LOGED","LONG","LOOP","LOWER","MAP","MATCH",+"MATERIALIZED","MAX","MAXLEN","MEMBER","MERGE","METHOD","METRICS","MIN","MINUS","MINUTE",+"MISSING","MOD","MODE","MODIFIES","MODIFY","MODULE","MONTH","MULTI","MULTISET","NAME",+"NAMES","NATIONAL","NATURAL","NCHAR","NCLOB","NEW","NEXT","NO","NONE","NOT","NULL","NULLIF",+"NUMBER","NUMERIC","OBJECT","OF","OFFLINE","OFFSET","OLD","ON","ONLINE","ONLY","OPAQUE",+"OPEN","OPERATOR","OPTION","OR","ORDER","ORDINALITY","OTHER","OTHERS","OUT","OUTER",+"OUTPUT","OVER","OVERLAPS","OVERRIDE","OWNER","PAD","PARALLEL","PARAMETER","PARAMETERS",+"PARTIAL","PARTITION","PARTITIONED","PARTITIONS","PATH","PERCENT","PERCENTILE","PERMISSION",+"PERMISSIONS","PIPE","PIPELINED","PLAN","POOL","POSITION","PRECISION","PREPARE","PRESERVE",+"PRIMARY","PRIOR","PRIVATE","PRIVILEGES","PROCEDURE","PROCESSED","PROJECT","PROJECTION",+"PROPERTY","PROVISIONING","PUBLIC","PUT","QUERY","QUIT","QUORUM","RAISE","RANDOM","RANGE",+"RANK","RAW","READ","READS","REAL","REBUILD","RECORD","RECURSIVE","REDUCE","REF","REFERENCE",+"REFERENCES","REFERENCING","REGEXP","REGION","REINDEX","RELATIVE","RELEASE","REMAINDER",+"RENAME","REPEAT","REPLACE","REQUEST","RESET","RESIGNAL","RESOURCE","RESPONSE","RESTORE",+"RESTRICT","RESULT","RETURN","RETURNING","RETURNS","REVERSE","REVOKE","RIGHT","ROLE",+"ROLES","ROLLBACK","ROLLUP","ROUTINE","ROW","ROWS","RULE","RULES","SAMPLE","SATISFIES",+"SAVE","SAVEPOINT","SCAN","SCHEMA","SCOPE","SCROLL","SEARCH","SECOND","SECTION","SEGMENT",+"SEGMENTS","SELECT","SELF","SEMI","SENSITIVE","SEPARATE","SEQUENCE","SERIALIZABLE","SESSION",+"SET","SETS","SHARD","SHARE","SHARED","SHORT","SHOW","SIGNAL","SIMILAR","SIZE","SKEWED",+"SMALLINT","SNAPSHOT","SOME","SOURCE","SPACE","SPACES","SPARSE","SPECIFIC","SPECIFICTYPE",+"SPLIT","SQL","SQLCODE","SQLERROR","SQLEXCEPTION","SQLSTATE","SQLWARNING","START","STATE",+"STATIC","STATUS","STORAGE","STORE","STORED","STREAM","STRING","STRUCT","STYLE","SUB",+"SUBMULTISET","SUBPARTITION","SUBSTRING","SUBTYPE","SUM","SUPER","SYMMETRIC","SYNONYM",+"SYSTEM","TABLE","TABLESAMPLE","TEMP","TEMPORARY","TERMINATED","TEXT","THAN","THEN",+"THROUGHPUT","TIME","TIMESTAMP","TIMEZONE","TINYINT","TO","TOKEN","TOTAL","TOUCH",+"TRAILING","TRANSACTION","TRANSFORM","TRANSLATE","TRANSLATION","TREAT","TRIGGER","TRIM",+"TRUE","TRUNCATE","TTL","TUPLE","TYPE","UNDER","UNDO","UNION","UNIQUE","UNIT","UNKNOWN",+"UNLOGGED","UNNEST","UNPROCESSED","UNSIGNED","UNTIL","UPDATE","UPPER","URL","USAGE","USE",+"USER","USERS","USING","UUID","VACUUM","VALUE","VALUED","VALUES","VARCHAR","VARIABLE",+"VARIANCE","VARINT","VARYING","VIEW","VIEWS","VIRTUAL","VOID","WAIT","WHEN","WHENEVER",+"WHERE","WHILE","WINDOW","WITH","WITHIN","WITHOUT","WORK","WRAPPED","WRITE","YEAR","ZONE",+))diff --git a/ministack/services/dynamodb.py b/ministack/services/dynamodb.pyindex 16ca8cd9..35ba03dc 100644--- a/ministack/services/dynamodb.py+++ b/ministack/services/dynamodb.py@@ -33,6 +33,7 @@ from ministack.core.responses import (new_uuid,now_iso,)+from ministack.services._dynamodb_keywords import AWS_KEYWORDSlogger = logging.getLogger("dynamodb")@@ -66,6 +67,17 @@ _pitr_settings = AccountScopedDict()# kinesis.put_record_internal; DISABLED entries stay on the describe# response (matching the ~24 h AWS retention window for readability)._kinesis_destinations = AccountScopedDict()+# Contributor Insights — key is "TableName" or "TableName/index/IndexName".+# Value: {"ContributorInsightsStatus": "ENABLED"|"DISABLED",+# "LastUpdateDateTime": float epoch, "ContributorInsightsRuleList": [str, ...]}.+_backups = AccountScopedDict() # BackupArn -> BackupDescription dict+_contributor_insights = AccountScopedDict()+# Resource-based policies — ResourceArn -> {"Policy": str, "RevisionId": str}.+_resource_policies = AccountScopedDict()+# Export tasks — ExportArn -> ExportDescription dict.+_exports = AccountScopedDict()+# Import tasks — ImportArn -> ImportTableDescription dict.+_imports = AccountScopedDict()_lock = threading.Lock()@@ -78,6 +90,11 @@ def get_state():"ttl_settings": copy.deepcopy(_ttl_settings),"pitr_settings": copy.deepcopy(_pitr_settings),"kinesis_destinations": copy.deepcopy(_kinesis_destinations),+ "contributor_insights": copy.deepcopy(_contributor_insights),+ "backups": copy.deepcopy(_backups),+ "resource_policies": copy.deepcopy(_resource_policies),+ "exports": copy.deepcopy(_exports),+ "imports": copy.deepcopy(_imports),}@@ -97,6 +114,11 @@ def restore_state(data):_ttl_settings.update(data.get("ttl_settings", {}))_pitr_settings.update(data.get("pitr_settings", {}))_kinesis_destinations.update(data.get("kinesis_destinations", {}))+ _contributor_insights.update(data.get("contributor_insights", {}))+ _backups.update(data.get("backups", {}))+ _resource_policies.update(data.get("resource_policies", {}))+ _exports.update(data.get("exports", {}))+ _imports.update(data.get("imports", {}))try:@@ -109,6 +131,295 @@ except Exception:"Failed to restore persisted state; continuing with fresh store")++# ---------------------------------------------------------------------------+# Validation helpers — AWS-canonical limit + format enforcement.+#+# Sources:+# * botocore service-2.json (2012-08-10) — shape ranges and required fields.+# * AWS DynamoDB Developer Guide:+# - "Service, account, and table quotas" (400 KB items, 38-digit numbers,+# 1E+126/-1E-130 magnitude bounds, 25 items per BatchWriteItem,+# 100 items per BatchGetItem, 100/4MB per TransactWriteItems).+# - "Working with items" (empty sets rejected, no duplicate set elements,+# attribute-name + value bytes count toward size).+# - "Reserved words" (https://docs.aws.amazon.com/amazondynamodb/latest/+# developerguide/ReservedWords.html).+# ---------------------------------------------------------------------------++# Numeric bounds per AWS docs.+_DDB_NUM_MAX_DIGITS = 38+_DDB_NUM_POS_MAX_EXP = 126 # |n| <= 9.9999...E+125 (positive 126 inclusive)+_DDB_NUM_NEG_MIN_EXP = -130 # smallest non-zero magnitude is 1E-130++# Item size caps.+_DDB_ITEM_MAX_BYTES = 400 * 1024+# TransactWriteItems / TransactGetItems caps.+_DDB_TXN_WRITE_MAX_ITEMS = 100+_DDB_TXN_GET_MAX_ITEMS = 100+_DDB_TXN_MAX_BYTES = 4 * 1024 * 1024+# Batch caps.+_DDB_BATCH_WRITE_MAX = 25+_DDB_BATCH_GET_MAX = 100+++def _ddb_canonicalize_number(s: str) -> str | None:+ """Validate a DynamoDB Number string and return its canonical form, or None+ if invalid. Mirrors AWS behavior: numbers are stored as variable-precision+ decimals (up to 38 significant digits, magnitude between 1E-130 and+ 9.9999E+125 inclusive). AWS canonicalizes: strips leading zeros, strips+ trailing zeros after the decimal point, normalizes negative zero to "0".+ Returns the canonical string the caller should persist, or None if the+ value is out of range / malformed.+ """+ if s is None:+ return None+ if not isinstance(s, str) or not s:+ return None+ raw = s.strip()+ # Reject empty / sign-only / non-numeric.+ try:+ d = Decimal(raw)+ except InvalidOperation:+ return None+ if d.is_nan() or d.is_infinite():+ return None+ # Significant-digit count: digits of the coefficient excluding leading zeros.+ sign, digits, exp = d.as_tuple()+ # Strip trailing zeros from significand to get true significant-digit count.+ sig = list(digits)+ while len(sig) > 1 and sig[-1] == 0:+ sig.pop()+ exp += 1+ while len(sig) > 1 and sig[0] == 0:+ sig.pop(0)+ if len(sig) > _DDB_NUM_MAX_DIGITS:+ return None+ if d == 0:+ return "0"+ # Magnitude check: AWS limits magnitude such that the decimal exponent of+ # the leading digit lies in [-130, 125]. Compute as adjusted exponent =+ # exp + (number_of_digits - 1). DynamoDB accepts 1E-130 through 9.9999E+125.+ adjusted = exp + len(sig) - 1+ if adjusted > _DDB_NUM_POS_MAX_EXP - 1: # > 125+ return None+ if adjusted < _DDB_NUM_NEG_MIN_EXP: # < -130+ return None+ # Canonical string: use Decimal's normalize but format without trailing zeros+ # and without unnecessary leading zeros.+ norm = d.normalize()+ # Decimal's normalize() returns scientific notation for very large/small;+ # we want the human-readable form when reasonable. Format manually.+ sign_str = "-" if sign else ""+ digits_str = "".join(str(x) for x in sig)+ if exp >= 0:+ text = digits_str + "0" * exp+ return sign_str + text+ # exp < 0+ point = len(digits_str) + exp+ if point <= 0:+ text = "0." + "0" * (-point) + digits_str+ else:+ text = digits_str[:point] + "." + digits_str[point:]+ # Strip trailing dot if any.+ if text.endswith("."):+ text = text[:-1]+ return sign_str + text+++def _validate_attribute_value(attr_name: str, value: dict) -> tuple | None:+ """Recursively validate a single attribute value. Returns an error response+ tuple if invalid, or None if OK. May mutate `value` to canonicalize numbers.+ """+ if not isinstance(value, dict) or not value:+ return error_response_json("ValidationException",+ "Supplied AttributeValue has more than one datatypes set, must contain exactly one of the supported datatypes", 400)+ if len(value) != 1:+ return error_response_json("ValidationException",+ "Supplied AttributeValue has more than one datatypes set, must contain exactly one of the supported datatypes", 400)+ (vtype, vval), = value.items()+ if vtype == "S":+ if not isinstance(vval, str):+ return error_response_json("ValidationException",+ f"Supplied AttributeValue is empty, must contain exactly one of the supported datatypes", 400)+ elif vtype == "N":+ canon = _ddb_canonicalize_number(vval)+ if canon is None:+ return error_response_json("ValidationException",+ f"The parameter cannot be converted to a numeric value: {vval}", 400)+ value["N"] = canon+ elif vtype == "B":+ if not isinstance(vval, (str, bytes)) or not vval:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: Binary attribute value is empty", 400)+ elif vtype == "BOOL":+ if not isinstance(vval, bool):+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: BOOL value must be true or false", 400)+ elif vtype == "NULL":+ if vval is not True:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: Null attribute value must be true", 400)+ elif vtype == "SS":+ if not isinstance(vval, list) or not vval:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: An string set may not be empty", 400)+ if len(set(vval)) != len(vval):+ return error_response_json("ValidationException",+ "Input collection contains duplicates", 400)+ for s in vval:+ if not isinstance(s, str) or s == "":+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: An string set may not be empty", 400)+ elif vtype == "NS":+ if not isinstance(vval, list) or not vval:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: An number set may not be empty", 400)+ seen = set()+ new_vals = []+ for n in vval:+ canon = _ddb_canonicalize_number(n if isinstance(n, str) else str(n))+ if canon is None:+ return error_response_json("ValidationException",+ f"The parameter cannot be converted to a numeric value: {n}", 400)+ if canon in seen:+ return error_response_json("ValidationException",+ "Input collection contains duplicates", 400)+ seen.add(canon)+ new_vals.append(canon)+ value["NS"] = new_vals+ elif vtype == "BS":+ if not isinstance(vval, list) or not vval:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: An binary set may not be empty", 400)+ seen = set()+ for b in vval:+ key = b if isinstance(b, str) else (b.decode("latin-1") if isinstance(b, bytes) else None)+ if key is None or key == "":+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: Binary set element may not be empty", 400)+ if key in seen:+ return error_response_json("ValidationException",+ "Input collection contains duplicates", 400)+ seen.add(key)+ elif vtype == "L":+ if not isinstance(vval, list):+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: List must be a list", 400)+ for sub in vval:+ err = _validate_attribute_value(attr_name, sub)+ if err:+ return err+ elif vtype == "M":+ if not isinstance(vval, dict):+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: Map must be a map", 400)+ for k, sub in vval.items():+ err = _validate_attribute_value(attr_name, sub)+ if err:+ return err+ else:+ return error_response_json("ValidationException",+ f"Supplied AttributeValue is empty, must contain exactly one of the supported datatypes", 400)+ return None+++def _attribute_value_size(value: dict) -> int:+ """Estimated byte size of an AttributeValue per AWS accounting.++ AWS docs: "The size of an item is the sum of the lengths of its attribute+ names and values." Per-type rules approximated from public guidance:+ - String/Binary: byte length of the value.+ - Number: 1 byte + ceil(digits / 2) bytes (max 21 bytes per number).+ - Boolean/Null: 1 byte.+ - List/Map: 3 bytes overhead + 1 byte per element + sum of child sizes.+ - SS/NS/BS: sum of element sizes.+ """+ if not isinstance(value, dict) or len(value) != 1:+ return 0+ (vtype, vval), = value.items()+ if vtype == "S":+ return len(vval.encode("utf-8")) if isinstance(vval, str) else 0+ if vtype == "N":+ # Strip sign, decimal, leading zeros for digit count.+ s = (vval or "").lstrip("-").replace(".", "").lstrip("0") or "0"+ return 1 + (len(s) + 1) // 2+ if vtype == "B":+ if isinstance(vval, bytes):+ return len(vval)+ try:+ import base64+ return len(base64.b64decode(vval))+ except Exception:+ return len(vval) if isinstance(vval, str) else 0+ if vtype in ("BOOL", "NULL"):+ return 1+ if vtype == "SS":+ return sum(len(s.encode("utf-8")) for s in vval if isinstance(s, str))+ if vtype == "NS":+ total = 0+ for n in vval:+ s = (n or "").lstrip("-").replace(".", "").lstrip("0") or "0"+ total += 1 + (len(s) + 1) // 2+ return total+ if vtype == "BS":+ total = 0+ for b in vval:+ if isinstance(b, bytes):+ total += len(b)+ else:+ try:+ import base64+ total += len(base64.b64decode(b))+ except Exception:+ total += len(b) if isinstance(b, str) else 0+ return total+ if vtype == "L":+ return 3 + sum(1 + _attribute_value_size(sub) for sub in vval if isinstance(sub, dict))+ if vtype == "M":+ total = 3+ for k, sub in vval.items():+ total += 1 + len(k.encode("utf-8")) + _attribute_value_size(sub)+ return total+ return 0+++def _item_size_bytes(item: dict) -> int:+ total = 0+ if not isinstance(item, dict):+ return 0+ for name, value in item.items():+ total += len(name.encode("utf-8"))+ total += _attribute_value_size(value)+ return total+++def _validate_item(item: dict, pk_name: str | None = None, sk_name: str | None = None) -> tuple | None:+ """Validate a full item: each attribute, then total size cap. Mutates+ Number values to canonical form."""+ if not isinstance(item, dict):+ return error_response_json("ValidationException",+ "Item must be a structure", 400)+ for name, value in item.items():+ # Empty string/binary not allowed for hash/sort key attributes+ if name in (pk_name, sk_name):+ if isinstance(value, dict):+ (vtype, vval), = value.items()+ if vtype == "S" and vval == "":+ return error_response_json("ValidationException",+ f"One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: {name}", 400)+ if vtype == "B" and (vval == "" or vval == b""):+ return error_response_json("ValidationException",+ f"One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty binary value. Key: {name}", 400)+ err = _validate_attribute_value(name, value)+ if err:+ return err+ size = _item_size_bytes(item)+ if size > _DDB_ITEM_MAX_BYTES:+ return error_response_json("ValidationException",+ f"Item size has exceeded the maximum allowed size", 400)+ return None+# DynamoDB Streams: table_name -> list of stream records# Each record follows the DynamoDB Streams event format consumed by Lambda ESMs._stream_records = AccountScopedDict()@@ -297,6 +608,27 @@ async def handle_request(method: str, path: str, headers: dict, body: bytes, que"DescribeKinesisStreamingDestination": _describe_kinesis_streaming_destination,"UpdateKinesisStreamingDestination": _update_kinesis_streaming_destination,"ExecuteStatement": _execute_statement,+ "BatchExecuteStatement": _batch_execute_statement,+ "ExecuteTransaction": _execute_transaction,+ "UpdateContributorInsights": _update_contributor_insights,+ "DescribeContributorInsights": _describe_contributor_insights,+ "ListContributorInsights": _list_contributor_insights,+ "PutResourcePolicy": _put_resource_policy,+ "GetResourcePolicy": _get_resource_policy,+ "DeleteResourcePolicy": _delete_resource_policy,+ "ExportTableToPointInTime": _export_table_to_point_in_time,+ "DescribeExport": _describe_export,+ "ListExports": _list_exports,+ "ImportTable": _import_table,+ "DescribeImport": _describe_import,+ "ListImports": _list_imports,+ "CreateBackup": _create_backup,+ "DescribeBackup": _describe_backup,+ "DeleteBackup": _delete_backup,+ "ListBackups": _list_backups,+ "RestoreTableFromBackup": _restore_table_from_backup,+ "RestoreTableToPointInTime": _restore_table_to_point_in_time,+ "DescribeLimits": _describe_limits,}handler = handlers.get(action)@@ -334,26 +666,119 @@ def _sse_description_from_spec(spec: dict | None) -> dict | None:if not spec:return Noneenabled = bool(spec.get("Enabled", False))- sse_type = spec.get("SSEType") or ("KMS" if spec.get("KMSMasterKeyId") else "AES256")+ # AWS default: when SSEType is omitted and Enabled is true, SSE-KMS with+ # the AWS-managed key (alias/aws/dynamodb) is configured. SSEType "AES256"+ # is not a valid CreateTable input — only "KMS" or omission.+ sse_type = spec.get("SSEType") or "KMS"desc = {"Status": "ENABLED" if enabled else "DISABLED","SSEType": sse_type,}if sse_type == "KMS":- kms_key = spec.get("KMSMasterKeyId") or spec.get("KMSMasterKeyArn", "")+ kms_key = spec.get("KMSMasterKeyId") or spec.get("KMSMasterKeyArn")+ if not kms_key:+ # AWS-managed key — fabricate a deterministic alias ARN.+ kms_key = f"arn:aws:kms:{get_region()}:{get_account_id()}:alias/aws/dynamodb"desc["KMSMasterKeyArn"] = kms_keyreturn desc+_TABLE_NAME_RE = re.compile(r"^[A-Za-z0-9_.\-]+$")+_INDEX_NAME_RE = re.compile(r"^[A-Za-z0-9_.\-]+$")+_VALID_ATTR_TYPES = {"S", "N", "B"}+_VALID_KEY_TYPES = {"HASH", "RANGE"}+_VALID_BILLING_MODES = {"PROVISIONED", "PAY_PER_REQUEST"}+_VALID_TABLE_CLASSES = {"STANDARD", "STANDARD_INFREQUENT_ACCESS"}+_VALID_PROJECTION_TYPES = {"ALL", "KEYS_ONLY", "INCLUDE"}+++def _validate_table_name(name: str) -> tuple | None:+ """Per AWS DynamoDB Developer Guide: 3-255 chars, pattern [A-Za-z0-9_.-]."""+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)+ if len(name) < 3 or len(name) > 255:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{name}' at 'tableName' failed to satisfy constraint: Member must have length between 3 and 255", 400)+ if not _TABLE_NAME_RE.match(name):+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{name}' at 'tableName' failed to satisfy constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+", 400)+ return None+++def _validate_key_schema(key_schema: list, attr_defs: list, context: str = "tableName") -> tuple | None:+ """Validates KeySchema + AttributeDefinitions per AWS rules:+ - 1 HASH required; 0 or 1 RANGE allowed.+ - Every key attribute must appear in AttributeDefinitions.+ - No duplicate attribute names in KeySchema.+ """+ if not key_schema:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'keySchema' failed to satisfy constraint: Member must not be null", 400)+ if not isinstance(key_schema, list) or len(key_schema) == 0 or len(key_schema) > 2:+ return error_response_json("ValidationException",+ "1 validation error detected: KeySchema must contain 1 or 2 elements", 400)+ hash_count = 0+ range_count = 0+ seen_names = set()+ for ks in key_schema:+ attr = ks.get("AttributeName")+ kt = ks.get("KeyType")+ if not attr or kt not in _VALID_KEY_TYPES:+ return error_response_json("ValidationException",+ f"1 validation error detected: KeySchema element invalid: {ks}", 400)+ if attr in seen_names:+ return error_response_json("ValidationException",+ f"Two keys cannot have the same name: {attr}", 400)+ seen_names.add(attr)+ if kt == "HASH":+ hash_count += 1+ elif kt == "RANGE":+ range_count += 1+ if hash_count != 1:+ return error_response_json("ValidationException",+ "1 validation error detected: KeySchema must contain exactly one HASH key", 400)+ # Every key attribute must be defined in AttributeDefinitions.+ defined = {a.get("AttributeName"): a.get("AttributeType") for a in (attr_defs or [])}+ for ks in key_schema:+ attr = ks["AttributeName"]+ if attr not in defined:+ return error_response_json("ValidationException",+ f"Hash Key not specified in Attribute Definitions. Type unknown: {attr}", 400)+ if defined[attr] not in _VALID_ATTR_TYPES:+ return error_response_json("ValidationException",+ f"Invalid AttributeType for attribute {attr}: {defined[attr]}", 400)+ return None++def _create_table(data):name = data.get("TableName")- if not name:- return error_response_json("ValidationException", "TableName is required", 400)+ err = _validate_table_name(name)+ if err:+ return errif name in _tables:return error_response_json("ResourceInUseException", f"Table already exists: {name}", 400)- key_schema = data.get("KeySchema", [])- attr_defs = data.get("AttributeDefinitions", [])+ key_schema = data.get("KeySchema")+ attr_defs = data.get("AttributeDefinitions") or []+ # Validate AttributeDefinitions structure.+ seen_attr_names = set()+ for ad in attr_defs:+ an = ad.get("AttributeName")+ at = ad.get("AttributeType")+ if not an:+ return error_response_json("ValidationException",+ "1 validation error detected: AttributeDefinitions element missing AttributeName", 400)+ if at not in _VALID_ATTR_TYPES:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{at}' at 'attributeDefinitions..attributeType' failed to satisfy constraint: Member must satisfy enum value set: [B, N, S]", 400)+ if an in seen_attr_names:+ return error_response_json("ValidationException",+ f"Duplicate AttributeName in AttributeDefinitions: {an}", 400)+ seen_attr_names.add(an)+ err = _validate_key_schema(key_schema, attr_defs)+ if err:+ return errpk_name = sk_name = Nonefor ks in key_schema:if ks["KeyType"] == "HASH":@@ -361,9 +786,70 @@ def _create_table(data):elif ks["KeyType"] == "RANGE":sk_name = ks["AttributeName"]+ # BillingMode validation.+ billing_mode = data.get("BillingMode", "PROVISIONED")+ if billing_mode not in _VALID_BILLING_MODES:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{billing_mode}' at 'billingMode' failed to satisfy constraint: Member must satisfy enum value set: {sorted(_VALID_BILLING_MODES)}", 400)+ # TableClass validation.+ table_class = data.get("TableClass")+ if table_class is not None and table_class not in _VALID_TABLE_CLASSES:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{table_class}' at 'tableClass' failed to satisfy constraint: Member must satisfy enum value set: {sorted(_VALID_TABLE_CLASSES)}", 400)+ # ProvisionedThroughput required when PROVISIONED.+ pt = data.get("ProvisionedThroughput")+ if billing_mode == "PROVISIONED":+ if not pt or not isinstance(pt, dict):+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: ProvisionedThroughput must be specified when BillingMode is PROVISIONED", 400)+ if int(pt.get("ReadCapacityUnits", 0)) <= 0 or int(pt.get("WriteCapacityUnits", 0)) <= 0:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: ReadCapacityUnits and WriteCapacityUnits must be greater than zero", 400)+ elif pt is not None:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: ProvisionedThroughput should not be specified when BillingMode is PAY_PER_REQUEST", 400)+gsis = copy.deepcopy(data.get("GlobalSecondaryIndexes", []))lsis = copy.deepcopy(data.get("LocalSecondaryIndexes", []))- billing_mode = data.get("BillingMode", "PROVISIONED")++ # LSI validation: requires the base table to have a RANGE key, and each LSI+ # must have the same HASH key as the base table.+ if lsis and not sk_name:+ return error_response_json("ValidationException",+ "Local Secondary indices are not allowed on hash tables, only hash and range tables", 400)+ for lsi in lsis:+ lks = lsi.get("KeySchema") or []+ if not any(k.get("KeyType") == "HASH" and k.get("AttributeName") == pk_name for k in lks):+ return error_response_json("ValidationException",+ f"Local Secondary Index '{lsi.get('IndexName')}' must use the table's hash key", 400)++ # Duplicate-index-name detection across LSI + GSI.+ seen_index_names = set()+ for idx in lsis + gsis:+ iname = idx.get("IndexName")+ if iname and iname in seen_index_names:+ return error_response_json("ValidationException",+ f"Two indexes cannot have the same name: {iname}", 400)+ if iname:+ seen_index_names.add(iname)+ # Every attribute referenced in any index KeySchema must be in AttributeDefinitions.+ referenced_attrs = set()+ for ks in key_schema:+ referenced_attrs.add(ks["AttributeName"])+ for idx_group in (gsis, lsis):+ for idx in idx_group:+ for k in idx.get("KeySchema") or []:+ referenced_attrs.add(k.get("AttributeName"))+ for ad_name in referenced_attrs:+ if ad_name not in seen_attr_names:+ return error_response_json("ValidationException",+ f"Some AttributeDefinitions are not present in KeySchema: {ad_name}", 400)+ # AttributeDefinitions must not include unused attrs (real AWS rejects).+ unused = seen_attr_names - referenced_attrs+ if unused:+ return error_response_json("ValidationException",+ f"One or more parameter values were invalid: Some AttributeDefinitions are not used: {sorted(unused)}", 400)+gsi_default_throughput = ({"ReadCapacityUnits": 0, "WriteCapacityUnits": 0}if billing_mode == "PAY_PER_REQUEST"@@ -396,13 +882,27 @@ def _create_table(data):"GlobalSecondaryIndexes": gsis,"LocalSecondaryIndexes": lsis,"ProvisionedThroughput": {"ReadCapacityUnits": 0, "WriteCapacityUnits": 0}- if data.get("BillingMode") == "PAY_PER_REQUEST"+ if billing_mode == "PAY_PER_REQUEST"else data.get("ProvisionedThroughput", {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}),- "BillingModeSummary": {"BillingMode": data.get("BillingMode", "PROVISIONED")},+ "BillingModeSummary": {"BillingMode": billing_mode},"StreamSpecification": data.get("StreamSpecification"),"SSEDescription": _sse_description_from_spec(data.get("SSESpecification")),"DeletionProtectionEnabled": data.get("DeletionProtectionEnabled", False),}+ # TableClass round-trip — DescribeTable echoes the configured class via+ # TableClassSummary (real AWS shape).+ if table_class:+ _tables[name]["TableClassSummary"] = {+ "TableClass": table_class,+ "LastUpdateDateTime": int(time.time()),+ }+ # OnDemandThroughput round-trip — only meaningful for PAY_PER_REQUEST.+ on_demand = data.get("OnDemandThroughput")+ if on_demand is not None:+ _tables[name]["OnDemandThroughput"] = {+ "MaxReadRequestUnits": int(on_demand.get("MaxReadRequestUnits", -1)),+ "MaxWriteRequestUnits": int(on_demand.get("MaxWriteRequestUnits", -1)),+ }if data.get("StreamSpecification"):stream_label = now_iso()_tables[name]["LatestStreamLabel"] = stream_label@@ -456,11 +956,37 @@ def _update_table(data):return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)table = _tables[name]- if "ProvisionedThroughput" in data:- table["ProvisionedThroughput"] = data["ProvisionedThroughput"]- if "BillingMode" in data:- table["BillingModeSummary"] = {"BillingMode": data["BillingMode"]}- if data["BillingMode"] == "PAY_PER_REQUEST":+ current_billing = table.get("BillingModeSummary", {}).get("BillingMode", "PROVISIONED")+ new_billing = data.get("BillingMode")+ # ProvisionedThroughput validation when supplied.+ pt = data.get("ProvisionedThroughput")+ if pt is not None:+ # PAY_PER_REQUEST + ProvisionedThroughput is invalid.+ if new_billing == "PAY_PER_REQUEST" or (new_billing is None and current_billing == "PAY_PER_REQUEST"):+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: ProvisionedThroughput should not be specified when BillingMode is PAY_PER_REQUEST", 400)+ rcu = int(pt.get("ReadCapacityUnits", 0))+ wcu = int(pt.get("WriteCapacityUnits", 0))+ if rcu <= 0 or wcu <= 0:+ return error_response_json("ValidationException",+ "One or more parameter values were invalid: ReadCapacityUnits and WriteCapacityUnits must be greater than zero", 400)+ existing_pt = table.get("ProvisionedThroughput") or {}+ # AWS rejects an UpdateTable that would result in no change.+ if (+ new_billing is None+ and current_billing == "PROVISIONED"+ and rcu == int(existing_pt.get("ReadCapacityUnits", 0))+ and wcu == int(existing_pt.get("WriteCapacityUnits", 0))+ ):+ return error_response_json("ValidationException",+ "The provisioned throughput for the table will not change. The requested value equals the current value.", 400)+ table["ProvisionedThroughput"] = pt+ if new_billing is not None:+ if new_billing not in _VALID_BILLING_MODES:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{new_billing}' at 'billingMode' failed to satisfy constraint: Member must satisfy enum value set: {sorted(_VALID_BILLING_MODES)}", 400)+ table["BillingModeSummary"] = {"BillingMode": new_billing, "LastUpdateToPayPerRequestDateTime": int(time.time())}+ if new_billing == "PAY_PER_REQUEST":table["ProvisionedThroughput"] = {"ReadCapacityUnits": 0, "WriteCapacityUnits": 0}if "AttributeDefinitions" in data:table["AttributeDefinitions"] = data["AttributeDefinitions"]@@ -474,27 +1000,60 @@ def _update_table(data):table["SSEDescription"] = _sse_description_from_spec(data["SSESpecification"])if "DeletionProtectionEnabled" in data:table["DeletionProtectionEnabled"] = data["DeletionProtectionEnabled"]+ # TableClass change round-trip.+ if "TableClass" in data:+ tc = data["TableClass"]+ if tc not in _VALID_TABLE_CLASSES:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{tc}' at 'tableClass' failed to satisfy constraint: Member must satisfy enum value set: {sorted(_VALID_TABLE_CLASSES)}", 400)+ table["TableClassSummary"] = {"TableClass": tc, "LastUpdateDateTime": int(time.time())}+ # OnDemandThroughput change round-trip.+ if "OnDemandThroughput" in data:+ odt = data["OnDemandThroughput"] or {}+ table["OnDemandThroughput"] = {+ "MaxReadRequestUnits": int(odt.get("MaxReadRequestUnits", -1)),+ "MaxWriteRequestUnits": int(odt.get("MaxWriteRequestUnits", -1)),+ }+ existing_idx_names = {g["IndexName"] for g in table.get("GlobalSecondaryIndexes", [])}+ defined_attrs = {a["AttributeName"] for a in table.get("AttributeDefinitions", [])}for update in data.get("GlobalSecondaryIndexUpdates", []):if "Create" in update:gsi_def = copy.deepcopy(update["Create"])+ idx_name = gsi_def.get("IndexName")+ if idx_name in existing_idx_names:+ return error_response_json("ValidationException",+ f"Attempting to create a duplicate index: {idx_name}", 400)+ # Every key attribute of the new GSI must be in AttributeDefinitions.+ for k in gsi_def.get("KeySchema") or []:+ if k.get("AttributeName") not in defined_attrs:+ return error_response_json("ValidationException",+ f"One or more parameter values were invalid: AttributeDefinitions does not contain {k.get('AttributeName')} referenced by GSI {idx_name}", 400)gsi_def.setdefault("IndexStatus", "ACTIVE")- current_billing = table.get("BillingModeSummary", {}).get("BillingMode", "PROVISIONED")+ gsi_billing = table.get("BillingModeSummary", {}).get("BillingMode", "PROVISIONED")gsi_def.setdefault("ProvisionedThroughput",{"ReadCapacityUnits": 0, "WriteCapacityUnits": 0}- if current_billing == "PAY_PER_REQUEST"+ if gsi_billing == "PAY_PER_REQUEST"else {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},)gsi_def["IndexArn"] = f"arn:aws:dynamodb:{get_region()}:{get_account_id()}:table/{name}/index/{gsi_def['IndexName']}"gsi_def["IndexSizeBytes"] = 0gsi_def["ItemCount"] = 0table["GlobalSecondaryIndexes"].append(gsi_def)+ existing_idx_names.add(idx_name)elif "Delete" in update:idx_name = update["Delete"]["IndexName"]+ if idx_name not in existing_idx_names:+ return error_response_json("ResourceNotFoundException",+ f"Attempt to remove a non-existent index: {idx_name}", 400)table["GlobalSecondaryIndexes"] = [g for g in table["GlobalSecondaryIndexes"] if g["IndexName"] != idx_name]+ existing_idx_names.discard(idx_name)elif "Update" in update:idx_name = update["Update"]["IndexName"]+ if idx_name not in existing_idx_names:+ return error_response_json("ResourceNotFoundException",+ f"Attempt to update a non-existent index: {idx_name}", 400)for gsi in table["GlobalSecondaryIndexes"]:if gsi["IndexName"] == idx_name:if "ProvisionedThroughput" in update["Update"]:@@ -529,6 +1088,10 @@ def _table_description(name):desc["LatestStreamArn"] = t.get("LatestStreamArn", "")if t.get("SSEDescription"):desc["SSEDescription"] = t["SSEDescription"]+ if t.get("TableClassSummary"):+ desc["TableClassSummary"] = t["TableClassSummary"]+ if t.get("OnDemandThroughput"):+ desc["OnDemandThroughput"] = t["OnDemandThroughput"]desc["DeletionProtectionEnabled"] = t.get("DeletionProtectionEnabled", False)desc["WarmThroughput"] = t.get("WarmThroughput", {"ReadUnitsPerSecond": 0,@@ -542,13 +1105,136 @@ def _table_description(name):# Item operations# ---------------------------------------------------------------------------+_PUT_DELETE_RV_VALUES = {"NONE", "ALL_OLD"}+_UPDATE_RV_VALUES = {"NONE", "ALL_OLD", "ALL_NEW", "UPDATED_OLD", "UPDATED_NEW"}+_RETURN_ITEM_COLLECTION_METRICS = {"NONE", "SIZE"}+_RETURN_CONSUMED_CAPACITY_VALUES = {"NONE", "TOTAL", "INDEXES"}+++def _validate_return_consumed_capacity(data) -> tuple | None:+ rcc = data.get("ReturnConsumedCapacity")+ if rcc is None:+ return None+ if rcc not in _RETURN_CONSUMED_CAPACITY_VALUES:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{rcc}' at 'returnConsumedCapacity' failed to satisfy constraint: Member must satisfy enum value set: [INDEXES, TOTAL, NONE]", 400)+ return None+++def _validate_expression_attrs(data, expression_fields: tuple) -> tuple | None:+ """AWS rejects ExpressionAttributeValues / ExpressionAttributeNames when+ no expression field references them at all, AND when any defined alias+ isn't used by any of the expressions, AND when any `:foo` or `#bar`+ referenced by an expression isn't defined."""+ has_any_expr = any(data.get(f) for f in expression_fields)+ eav = data.get("ExpressionAttributeValues")+ ean = data.get("ExpressionAttributeNames")+ if eav and not has_any_expr:+ return error_response_json("ValidationException",+ "ExpressionAttributeValues must not be empty", 400)+ if ean and not has_any_expr:+ return error_response_json("ValidationException",+ "ExpressionAttributeNames must not be empty", 400)+ # Build a string of all referenced expressions for substring scanning.+ expr_text = " ".join(data.get(f) or "" for f in expression_fields)+ if eav:+ for placeholder in eav.keys():+ if placeholder not in expr_text:+ return error_response_json("ValidationException",+ f"Value provided in ExpressionAttributeValues unused in expressions: keys: {{{placeholder}}}", 400)+ if ean:+ for alias in ean.keys():+ if alias not in expr_text:+ return error_response_json("ValidationException",+ f"Value provided in ExpressionAttributeNames unused in expressions: keys: {{{alias}}}", 400)+ # Inverse: every `:foo` / `#bar` in expressions must be defined.+ if expr_text:+ for ref in re.findall(r":[A-Za-z_][A-Za-z0-9_]*", expr_text):+ if not eav or ref not in eav:+ return error_response_json("ValidationException",+ f"An expression attribute value used in expression is not defined; attribute value: {ref}", 400)+ for ref in re.findall(r"#[A-Za-z_][A-Za-z0-9_]*", expr_text):+ if not ean or ref not in ean:+ return error_response_json("ValidationException",+ f"An expression attribute name used in expression is not defined; attribute name: {ref}", 400)+ return None+++def _validate_return_values(data, allowed: set) -> tuple | None:+ rv = data.get("ReturnValues")+ if rv is None:+ return None+ if rv not in allowed:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{rv}' at 'returnValues' failed to satisfy constraint: Member must satisfy enum value set: {sorted(allowed)}", 400)+ return None+++def _validate_return_item_collection_metrics(data) -> tuple | None:+ ricm = data.get("ReturnItemCollectionMetrics")+ if ricm is None:+ return None+ if ricm not in _RETURN_ITEM_COLLECTION_METRICS:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{ricm}' at 'returnItemCollectionMetrics' failed to satisfy constraint: Member must satisfy enum value set: ['NONE', 'SIZE']", 400)+ return None+++def _add_item_collection_metrics(result: dict, data: dict, table: dict, item: dict | None, key: dict | None):+ """Populate result["ItemCollectionMetrics"] when the caller requested SIZE+ and the table has at least one LSI. AWS returns the ItemCollectionKey+ (just the hash key of the affected item) and a SizeEstimateRangeGB+ placeholder."""+ if data.get("ReturnItemCollectionMetrics") != "SIZE":+ return+ if not table.get("LocalSecondaryIndexes"):+ return+ pk_name = table.get("pk_name")+ if not pk_name:+ return+ source = item or key or {}+ if pk_name not in source:+ return+ result["ItemCollectionMetrics"] = {+ "ItemCollectionKey": {pk_name: source[pk_name]},+ "SizeEstimateRangeGB": [0.0, 1.0],+ }++def _put_item(data):name = data.get("TableName")+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)table = _tables.get(name)if not table:- return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)+ return error_response_json("ResourceNotFoundException",+ f"Cannot do operations on a non-existent table", 400)++ err = _validate_return_values(data, _PUT_DELETE_RV_VALUES)+ if err:+ return err+ err = _validate_return_item_collection_metrics(data)+ if err:+ return err+ err = _validate_return_consumed_capacity(data)+ if err:+ return err+ err = _validate_expression_attrs(data, ("ConditionExpression",))+ if err:+ return erritem = data.get("Item", {})+ err = _validate_item(item, table.get("pk_name"), table.get("sk_name"))+ if err:+ return err+ # Hash key must be present.+ if table.get("pk_name") and table["pk_name"] not in item:+ return error_response_json("ValidationException",+ f"One or more parameter values were invalid: Missing the key {table['pk_name']} in the item", 400)+ if table.get("sk_name") and table["sk_name"] not in item:+ return error_response_json("ValidationException",+ f"One or more parameter values were invalid: Missing the key {table['sk_name']} in the item", 400)pk_val = _extract_key_val(item.get(table["pk_name"]))sk_val = _extract_key_val(item.get(table["sk_name"])) if table["sk_name"] else "__no_sort__"old_item = table["items"].get(pk_val, {}).get(sk_val)@@ -558,7 +1244,11 @@ def _put_item(data):if cond_expr and expected:return error_response_json("ValidationException", "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400)if cond_expr:- if not _evaluate_condition(cond_expr, old_item or {}, data.get("ExpressionAttributeValues", {}), data.get("ExpressionAttributeNames", {})):+ try:+ cond_eval = _evaluate_condition(cond_expr, old_item or {}, data.get("ExpressionAttributeValues", {}), data.get("ExpressionAttributeNames", {}))+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)+ if not cond_eval:return _conditional_check_failed(data, old_item)elif expected:if not _evaluate_expected(old_item or {}, expected, data.get("ConditionalOperator", "AND")):@@ -574,14 +1264,28 @@ def _put_item(data):if data.get("ReturnValues") == "ALL_OLD" and old_item:result["Attributes"] = old_item_add_consumed_capacity(result, data, name, write=True)+ _add_item_collection_metrics(result, data, table, item, None)return json_response(result)def _get_item(data):name = data.get("TableName")+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)table = _tables.get(name)if not table:- return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)+ return error_response_json("ResourceNotFoundException",+ f"Cannot do operations on a non-existent table", 400)+ if data.get("ProjectionExpression") and data.get("AttributesToGet"):+ return error_response_json("ValidationException",+ "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400)+ err = _validate_return_consumed_capacity(data)+ if err:+ return err+ err = _validate_expression_attrs(data, ("ProjectionExpression",))+ if err:+ return errkey = data.get("Key", {})pk_val, sk_val, key_err = _resolve_table_key_values(table, key, allow_extra=False)@@ -591,16 +1295,36 @@ def _get_item(data):result = {}if item:- result["Item"] = _apply_projection(item, data)+ try:+ result["Item"] = _apply_projection(item, data)+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)_add_consumed_capacity(result, data, name)return json_response(result)def _delete_item(data):name = data.get("TableName")+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)table = _tables.get(name)if not table:- return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)+ return error_response_json("ResourceNotFoundException",+ f"Cannot do operations on a non-existent table", 400)++ err = _validate_return_values(data, _PUT_DELETE_RV_VALUES)+ if err:+ return err+ err = _validate_return_item_collection_metrics(data)+ if err:+ return err+ err = _validate_return_consumed_capacity(data)+ if err:+ return err+ err = _validate_expression_attrs(data, ("ConditionExpression",))+ if err:+ return errkey = data.get("Key", {})pk_val, sk_val, key_err = _resolve_table_key_values(table, key, allow_extra=False)@@ -613,7 +1337,11 @@ def _delete_item(data):if cond_expr and expected:return error_response_json("ValidationException", "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400)if cond_expr:- if not _evaluate_condition(cond_expr, old_item or {}, data.get("ExpressionAttributeValues", {}), data.get("ExpressionAttributeNames", {})):+ try:+ cond_eval = _evaluate_condition(cond_expr, old_item or {}, data.get("ExpressionAttributeValues", {}), data.get("ExpressionAttributeNames", {}))+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)+ if not cond_eval:return _conditional_check_failed(data, old_item)elif expected:if not _evaluate_expected(old_item or {}, expected, data.get("ConditionalOperator", "AND")):@@ -628,14 +1356,32 @@ def _delete_item(data):if data.get("ReturnValues") == "ALL_OLD" and old_item:result["Attributes"] = old_item_add_consumed_capacity(result, data, name, write=True)+ _add_item_collection_metrics(result, data, table, None, key)return json_response(result)def _update_item(data):name = data.get("TableName")+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)table = _tables.get(name)if not table:- return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)+ return error_response_json("ResourceNotFoundException",+ f"Cannot do operations on a non-existent table", 400)++ err = _validate_return_values(data, _UPDATE_RV_VALUES)+ if err:+ return err+ err = _validate_return_item_collection_metrics(data)+ if err:+ return err+ err = _validate_return_consumed_capacity(data)+ if err:+ return err+ err = _validate_expression_attrs(data, ("ConditionExpression", "UpdateExpression"))+ if err:+ return errkey = data.get("Key", {})pk_val, sk_val, key_err = _resolve_table_key_values(table, key, allow_extra=False)@@ -652,7 +1398,11 @@ def _update_item(data):return error_response_json("ValidationException", "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {Expected} Expression parameters: {ConditionExpression}", 400)if cond_expr:cond_target = existing or {}- if not _evaluate_condition(cond_expr, cond_target, data.get("ExpressionAttributeValues", {}), data.get("ExpressionAttributeNames", {})):+ try:+ cond_eval = _evaluate_condition(cond_expr, cond_target, data.get("ExpressionAttributeValues", {}), data.get("ExpressionAttributeNames", {}))+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)+ if not cond_eval:return _conditional_check_failed(data, existing)elif expected:if not _evaluate_expected(existing or {}, expected, data.get("ConditionalOperator", "AND")):@@ -665,14 +1415,27 @@ def _update_item(data):if update_expr and attribute_updates:return error_response_json("ValidationException", "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}", 400)+ # An empty UpdateExpression string is rejected by AWS.+ if "UpdateExpression" in data and not update_expr.strip():+ return error_response_json("ValidationException",+ "Invalid UpdateExpression: The expression can not be empty", 400)if update_expr:- item = _apply_update_expression(item, update_expr, eav, ean)+ try:+ item = _apply_update_expression(item, update_expr, eav, ean)+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)elif attribute_updates:try:item = _apply_attribute_updates(item, attribute_updates)except _AttributeUpdatesValidationError as exc:return error_response_json("ValidationException", str(exc), 400)+ # AWS rejects any update that would mutate a hash or range key value.+ for key_name in (table.get("pk_name"), table.get("sk_name")):+ if key_name and key_name in item and existing is not None:+ if item.get(key_name) != existing.get(key_name):+ return error_response_json("ValidationException",+ f"One or more parameter values were invalid: Cannot update attribute {key_name}. This attribute is part of the key", 400)table["items"][pk_val][sk_val] = item_update_counts(table)@@ -689,8 +1452,13 @@ def _update_item(data):elif rv == "UPDATED_OLD" and old_item:result["Attributes"] = _diff_attributes(old_item, item, return_old=True)elif rv == "UPDATED_NEW":- result["Attributes"] = _diff_attributes(old_item or {}, item, return_old=False)+ # AWS omits Attributes from the response when the only operation was+ # REMOVE — there are no "new" values to return.+ new_attrs = _diff_attributes(old_item or {}, item, return_old=False)+ if new_attrs:+ result["Attributes"] = new_attrs_add_consumed_capacity(result, data, name, write=True)+ _add_item_collection_metrics(result, data, table, item, key)return json_response(result)@@ -700,9 +1468,19 @@ def _update_item(data):def _query(data):name = data.get("TableName")+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)table = _tables.get(name)if not table:- return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)+ return error_response_json("ResourceNotFoundException",+ f"Cannot do operations on a non-existent table", 400)+ err = _validate_return_consumed_capacity(data)+ if err:+ return err+ err = _validate_expression_attrs(data, ("KeyConditionExpression", "FilterExpression", "ProjectionExpression"))+ if err:+ return erreav = data.get("ExpressionAttributeValues", {})ean = data.get("ExpressionAttributeNames", {})@@ -717,15 +1495,78 @@ def _query(data):if key_cond and key_conditions:return error_response_json("ValidationException", "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {KeyConditions} Expression parameters: {KeyConditionExpression}", 400)+ if data.get("ProjectionExpression") and data.get("AttributesToGet"):+ return error_response_json("ValidationException",+ "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400)+ if data.get("FilterExpression") and data.get("QueryFilter"):+ return error_response_json("ValidationException",+ "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {QueryFilter} Expression parameters: {FilterExpression}", 400)+ # Empty KeyConditionExpression rejected explicitly.+ if "KeyConditionExpression" in data and not (data["KeyConditionExpression"] or "").strip():+ return error_response_json("ValidationException",+ "KeyConditionExpression is empty", 400)+ # Select must be one of the canonical enum values when supplied.+ if "Select" in data and data["Select"] not in {"ALL_ATTRIBUTES", "ALL_PROJECTED_ATTRIBUTES", "SPECIFIC_ATTRIBUTES", "COUNT"}:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{data['Select']}' at 'select' failed to satisfy constraint: Member must satisfy enum value set: [ALL_ATTRIBUTES, ALL_PROJECTED_ATTRIBUTES, SPECIFIC_ATTRIBUTES, COUNT]", 400)+ # Malformed ExclusiveStartKey: must include all key attributes of the+ # table (and the index, if querying an index).+ esk_val = data.get("ExclusiveStartKey")+ if esk_val is not None and not isinstance(esk_val, dict):+ return error_response_json("ValidationException",+ "The provided starting key is invalid", 400)+ # Limit must be >= 1 when supplied.+ if limit is not None and int(limit) <= 0:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{limit}' at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1", 400)+ # Select validation per AWS: ALL_PROJECTED_ATTRIBUTES is only valid on an+ # index; SPECIFIC_ATTRIBUTES requires a ProjectionExpression / AttributesToGet.+ if select == "ALL_PROJECTED_ATTRIBUTES" and not index_name:+ return error_response_json("ValidationException",+ "ALL_PROJECTED_ATTRIBUTES can be used only when Querying an index", 400)+ if select == "SPECIFIC_ATTRIBUTES" and not data.get("ProjectionExpression") and not data.get("AttributesToGet"):+ return error_response_json("ValidationException",+ "SPECIFIC_ATTRIBUTES requires ProjectionExpression or AttributesToGet", 400)pk_name, sk_name, is_gsi = _resolve_index_keys(table, index_name)+ # ConsistentRead on a GSI is invalid (only LSIs support strongly-consistent reads).+ if data.get("ConsistentRead") and is_gsi:+ return error_response_json("ValidationException",+ f"Consistent reads are not supported on global secondary indexes", 400)if key_conditions:pk_val = _extract_pk_from_key_conditions(key_conditions, pk_name)else:pk_val = _extract_pk_from_condition(key_cond, eav, ean, pk_name)if pk_val is None:- return error_response_json("ValidationException", "Query condition missed key schema element", 400)+ return error_response_json("ValidationException",+ f"Query condition missed key schema element: {pk_name}", 400)+ # Reject non-key attributes in KeyConditionExpression: every bare identifier+ # in the expression must be either the hash key or the sort key (resolved+ # via ExpressionAttributeNames if aliased).+ if key_cond:+ try:+ kce_tokens = _tokenize(key_cond)+ except Exception:+ kce_tokens = []+ allowed = {pk_name}+ if sk_name:+ allowed.add(sk_name)+ for tok in kce_tokens:+ if tok[0] == "IDENT":+ name_ = tok[1]+ if name_.lower() in _DDB_EXPR_FUNCTIONS:+ continue+ if name_.upper() in ("AND", "OR", "NOT", "BETWEEN"):+ continue+ if name_ not in allowed:+ return error_response_json("ValidationException",+ f"Query condition missed key schema element: {name_}", 400)+ elif tok[0] == "NAME_REF":+ resolved = ean.get(tok[1])+ if resolved and resolved not in allowed:+ return error_response_json("ValidationException",+ f"Query condition missed key schema element: {resolved}", 400)if is_gsi or index_name:candidates = []@@ -752,7 +1593,10 @@ def _query(data):if key_conditions:candidates = [it for it in candidates if _evaluate_key_conditions_item(it, key_conditions, pk_name)]elif key_cond:- candidates = [it for it in candidates if _evaluate_condition(key_cond, it, eav, ean)]+ try:+ candidates = [it for it in candidates if _evaluate_condition(key_cond, it, eav, ean)]+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)if esk:candidates = _apply_exclusive_start_key(candidates, esk, pk_name, sk_name, scan_forward, table=table)@@ -774,8 +1618,12 @@ def _query(data):if select == "COUNT":result = {"Count": len(filtered), "ScannedCount": scanned_count}else:+ try:+ projected = [_apply_projection(it, data) for it in filtered]+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)result = {- "Items": [_apply_projection(it, data) for it in filtered],+ "Items": projected,"Count": len(filtered),"ScannedCount": scanned_count,}@@ -794,9 +1642,19 @@ def _query(data):def _scan(data):name = data.get("TableName")+ if not isinstance(name, str) or not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)table = _tables.get(name)if not table:- return error_response_json("ResourceNotFoundException", f"Requested resource not found: Table: {name} not found", 400)+ return error_response_json("ResourceNotFoundException",+ f"Cannot do operations on a non-existent table", 400)+ err = _validate_return_consumed_capacity(data)+ if err:+ return err+ err = _validate_expression_attrs(data, ("FilterExpression", "ProjectionExpression"))+ if err:+ return errfilter_expr = data.get("FilterExpression", "")eav = data.get("ExpressionAttributeValues", {})@@ -806,6 +1664,48 @@ def _scan(data):index_name = data.get("IndexName")select = data.get("Select", "ALL_ATTRIBUTES")+ # Limit must be > 0 when provided (AWS rejects Limit=0).+ if limit is not None and int(limit) <= 0:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{limit}' at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1", 400)+ if data.get("ProjectionExpression") and data.get("AttributesToGet"):+ return error_response_json("ValidationException",+ "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400)+ if data.get("FilterExpression") and data.get("ScanFilter"):+ return error_response_json("ValidationException",+ "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {ScanFilter} Expression parameters: {FilterExpression}", 400)+ # Segment / TotalSegments validation per AWS.+ segment = data.get("Segment")+ total_segments = data.get("TotalSegments")+ if segment is not None and total_segments is None:+ return error_response_json("ValidationException",+ "The TotalSegments parameter is required when Segment is provided", 400)+ if total_segments is not None and segment is None:+ return error_response_json("ValidationException",+ "The Segment parameter is required when TotalSegments is provided", 400)+ if segment is not None and total_segments is not None:+ seg = int(segment); ts = int(total_segments)+ if ts < 1 or ts > 1_000_000:+ return error_response_json("ValidationException",+ f"TotalSegments must be between 1 and 1000000", 400)+ if seg < 0 or seg >= ts:+ return error_response_json("ValidationException",+ f"Segment must be greater than or equal to 0 and less than TotalSegments", 400)+ # Select validation.+ if select == "ALL_PROJECTED_ATTRIBUTES" and not index_name:+ return error_response_json("ValidationException",+ "ALL_PROJECTED_ATTRIBUTES can be used only when Scanning an index", 400)+ if select == "SPECIFIC_ATTRIBUTES" and not data.get("ProjectionExpression") and not data.get("AttributesToGet"):+ return error_response_json("ValidationException",+ "SPECIFIC_ATTRIBUTES requires ProjectionExpression or AttributesToGet", 400)+ # ConsistentRead on a GSI is invalid.+ if index_name and data.get("ConsistentRead"):+ _, _, is_gsi_scan = _resolve_index_keys(table, index_name)+ if is_gsi_scan:+ return error_response_json("ValidationException",+ "Consistent reads are not supported on global secondary indexes", 400)+ # Query Limit also validated above for parity.+all_items = []for pk in sorted(table["items"].keys()):for sk in sorted(table["items"][pk].keys()):@@ -831,15 +1731,22 @@ def _scan(data):if scan_filter and not filter_expr:filtered = [it for it in all_items if _evaluate_legacy_filter(it, scan_filter)]elif filter_expr:- filtered = [it for it in all_items if _evaluate_condition(filter_expr, it, eav, ean)]+ try:+ filtered = [it for it in all_items if _evaluate_condition(filter_expr, it, eav, ean)]+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)else:filtered = all_itemsif select == "COUNT":result = {"Count": len(filtered), "ScannedCount": scanned_count}else:+ try:+ projected = [_apply_projection(it, data) for it in filtered]+ except ValueError as exc:+ return error_response_json("ValidationException", str(exc), 400)result = {- "Items": [_apply_projection(it, data) for it in filtered],+ "Items": projected,"Count": len(filtered),"ScannedCount": scanned_count,}@@ -860,7 +1767,7 @@ def _execute_statement(data):parameters = data.get("Parameters", [])if not statement or not statement.strip():- return error_response_json("ValidationException", "Statement must not be empty", 400)+ return error_response_json("ValidationException", "Statement must not be null or empty", 400)try:parsed = _parse_partiql(statement, parameters)@@ -875,15 +1782,28 @@ def _execute_statement(data):f"Requested resource not found: Table: {table_name} not found", 400)if op == "SELECT":- return _partiql_select(table, parsed)+ status, headers, body = _partiql_select(table, parsed)elif op == "INSERT":- return _partiql_insert(table, parsed)+ status, headers, body = _partiql_insert(table, parsed)elif op == "UPDATE":- return _partiql_update(table, parsed)+ status, headers, body = _partiql_update(table, parsed)elif op == "DELETE":- return _partiql_delete(table, parsed)+ status, headers, body = _partiql_delete(table, parsed)else:return error_response_json("ValidationException", f"Unsupported PartiQL operation: {op}", 400)+ # Attach ConsumedCapacity per AWS when ReturnConsumedCapacity != NONE.+ rc = data.get("ReturnConsumedCapacity", "NONE")+ if status == 200 and rc != "NONE":+ try:+ payload = json.loads(body)+ except (TypeError, ValueError):+ payload = None+ if isinstance(payload, dict):+ units = max(1.0, float(len(payload.get("Items", []) or [1])))+ payload["ConsumedCapacity"] = {"TableName": table_name, "CapacityUnits": units}+ new_body = json.dumps(payload, ensure_ascii=False).encode("utf-8")+ return status, headers, new_body+ return status, headers, bodydef _partiql_select(table, parsed):@@ -920,11 +1840,13 @@ def _partiql_insert(table, parsed):if not pk_val:return error_response_json("ValidationException","Missing partition key in INSERT", 400)- # DynamoDB PartiQL INSERT fails on duplicate key+ # DynamoDB PartiQL INSERT on an existing primary key returns+ # DuplicateItemException (verified against AWS docs + botocore error list).if pk_val in table["items"] and sk_val in table["items"][pk_val]:- return error_response_json("ConditionalCheckFailedException",+ return error_response_json("DuplicateItemException","Duplicate primary key exists in table", 400)table["items"][pk_val][sk_val] = item+ _update_counts(table)return json_response({})@@ -934,16 +1856,13 @@ def _partiql_update(table, parsed):if not where_fn or not set_attrs:return error_response_json("ValidationException","UPDATE requires SET and WHERE clauses", 400)- updated = Falsefor pk in list(table["items"].keys()):for sk in list(table["items"][pk].keys()):it = table["items"][pk][sk]if where_fn(it):for attr, val in set_attrs.items():it[attr] = val- updated = True- if updated:- return json_response({})+ return json_response({})def _partiql_delete(table, parsed):@@ -961,8 +1880,172 @@ def _partiql_delete(table, parsed):del table["items"][pk][sk]if not table["items"][pk]:del table["items"][pk]- if to_delete:- return json_response({})+ _update_counts(table)+ return json_response({})+++# ---------------------------------------------------------------------------+# PartiQL — BatchExecuteStatement+# ---------------------------------------------------------------------------++_DDB_BATCH_PARTIQL_MAX = 25+++def _batch_execute_statement(data):+ statements = data.get("Statements")+ if not statements:+ return error_response_json("ValidationException",+ "1 validation error detected: Value '[]' at 'statements' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)+ if len(statements) > _DDB_BATCH_PARTIQL_MAX:+ return error_response_json("ValidationException",+ f"Member must have length less than or equal to {_DDB_BATCH_PARTIQL_MAX}", 400)+ responses = []+ rc = data.get("ReturnConsumedCapacity", "NONE")+ per_table_units: dict[str, float] = {}+ for stmt in statements:+ sub = {"Statement": stmt.get("Statement"), "Parameters": stmt.get("Parameters", [])}+ status, _, body = _execute_statement(sub)+ try:+ payload = json.loads(body)+ except (TypeError, ValueError):+ payload = {}+ if status == 200:+ entry: dict = {}+ if payload.get("Items") is not None:+ entry["Item"] = payload["Items"][0] if payload["Items"] else None+ if entry.get("Item") is None and "Item" in entry:+ entry.pop("Item")+ responses.append(entry)+ else:+ err_code = payload.get("__type", "ValidationException")+ err_msg = payload.get("message", "")+ responses.append({"Error": {"Code": err_code.split("#")[-1], "Message": err_msg}})+ # Crude per-table unit attribution.+ try:+ parsed = _parse_partiql(stmt.get("Statement", ""), stmt.get("Parameters", []))+ per_table_units[parsed["table"]] = per_table_units.get(parsed["table"], 0.0) + 1.0+ except Exception:+ pass+ result = {"Responses": responses}+ if rc != "NONE":+ result["ConsumedCapacity"] = [+ {"TableName": t, "CapacityUnits": u}+ for t, u in per_table_units.items()+ ]+ return json_response(result)+++# ---------------------------------------------------------------------------+# PartiQL — ExecuteTransaction+# ---------------------------------------------------------------------------++_DDB_TXN_PARTIQL_MAX = 100+++def _execute_transaction(data):+ statements = data.get("TransactStatements")+ if not statements:+ return error_response_json("ValidationException",+ "1 validation error detected: Value '[]' at 'transactStatements' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)+ if len(statements) > _DDB_TXN_PARTIQL_MAX:+ return error_response_json("ValidationException",+ f"Member must have length less than or equal to {_DDB_TXN_PARTIQL_MAX}", 400)++ # ClientRequestToken idempotency parity with TransactWriteItems.+ crt = data.get("ClientRequestToken")+ signature = None+ if crt:+ prior = _txn_idempotency.get(crt)+ signature = {k: v for k, v in data.items() if k != "ClientRequestToken"}+ if prior is not None:+ if prior.get("signature") == signature:+ return json_response(prior.get("response", {}))+ return error_response_json("IdempotentParameterMismatchException",+ "Request token already in use for another request with a different payload", 400)++ # Parse every statement and detect duplicate-INSERT pre-emptively so the+ # whole transaction is rejected (matches AWS semantics — all-or-nothing).+ parsed_list = []+ for stmt in statements:+ statement = stmt.get("Statement", "")+ parameters = stmt.get("Parameters", [])+ try:+ parsed = _parse_partiql(statement, parameters)+ except ValueError as e:+ return error_response_json("ValidationException", str(e), 400)+ table = _tables.get(parsed["table"])+ if not table:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Table: {parsed['table']} not found", 400)+ parsed_list.append((parsed, table))++ # Apply each statement; collect any failures to roll back. Take ONE+ # snapshot per table up front so rollback restores the pre-transaction+ # state, not the state mid-transaction.+ snapshots: dict[str, dict] = {}+ for parsed, table in parsed_list:+ tname = table["TableName"]+ if tname not in snapshots:+ snapshots[tname] = copy.deepcopy(dict(table["items"]))+ rc = data.get("ReturnConsumedCapacity", "NONE")+ responses = []+ failure = None+ for idx, (parsed, table) in enumerate(parsed_list):+ if parsed["op"] == "SELECT":+ status, _, body = _partiql_select(table, parsed)+ elif parsed["op"] == "INSERT":+ status, _, body = _partiql_insert(table, parsed)+ elif parsed["op"] == "UPDATE":+ status, _, body = _partiql_update(table, parsed)+ elif parsed["op"] == "DELETE":+ status, _, body = _partiql_delete(table, parsed)+ else:+ failure = (idx, "ValidationException", f"Unsupported op: {parsed['op']}")+ break+ if status != 200:+ try:+ err_body = json.loads(body)+ except (TypeError, ValueError):+ err_body = {}+ failure = (idx, err_body.get("__type", "ValidationException"), err_body.get("message", ""))+ break+ try:+ payload = json.loads(body)+ except (TypeError, ValueError):+ payload = {}+ entry = {}+ if "Items" in payload and payload["Items"]:+ entry["Item"] = payload["Items"][0]+ responses.append(entry)++ if failure is not None:+ # Roll back: restore each table's items from its snapshot.+ for tname, items in snapshots.items():+ tbl = _tables.get(tname)+ if tbl is not None:+ tbl["items"] = defaultdict(dict, items)+ idx, code, msg = failure+ reasons = [{"Code": "None"} for _ in statements]+ reasons[idx] = {"Code": code.split("#")[-1], "Message": msg}+ # AWS returns TransactionCanceledException with per-statement reasons.+ body = json.dumps({+ "__type": "TransactionCanceledException",+ "message": f"Transaction cancelled, please refer cancellation reasons for specific reasons [{', '.join(r['Code'] for r in reasons)}]",+ "CancellationReasons": reasons,+ }).encode("utf-8")+ return 400, {"Content-Type": "application/x-amz-json-1.0", "x-amzn-errortype": "TransactionCanceledException"}, body++ result = {"Responses": responses}+ if rc != "NONE":+ per_table: dict[str, float] = {}+ for parsed, _ in parsed_list:+ per_table[parsed["table"]] = per_table.get(parsed["table"], 0.0) + 2.0+ result["ConsumedCapacity"] = [+ {"TableName": t, "CapacityUnits": u} for t, u in per_table.items()+ ]+ if crt:+ _txn_idempotency[crt] = {"signature": signature, "response": result}+ return json_response(result)def _parse_partiql(statement, parameters):@@ -1214,7 +2297,41 @@ def _split_top_level(s, delimiter):# ---------------------------------------------------------------------------def _batch_write_item(data):- request_items = data.get("RequestItems", {})+ request_items = data.get("RequestItems")+ if not request_items:+ return error_response_json("ValidationException",+ "1 validation error detected: Value '{}' at 'requestItems' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)+ # Total request count cap (25 per BatchWriteItem call).+ total = sum(len(v) for v in request_items.values())+ if total > _DDB_BATCH_WRITE_MAX:+ return error_response_json("ValidationException",+ f"Too many items requested for the BatchWriteItem call", 400)+ # Duplicate target key detection — AWS rejects two writes to the same key+ # in a single BatchWriteItem call.+ seen_keys = set()+ for table_name, requests in request_items.items():+ table = _tables.get(table_name)+ if not table:+ return error_response_json(+ "ResourceNotFoundException",+ "Cannot do operations on a non-existent table",+ 400,+ )+ for req in requests:+ if "PutRequest" in req:+ key_repr = (table_name,+ _extract_key_val(req["PutRequest"]["Item"].get(table.get("pk_name") or "")),+ _extract_key_val(req["PutRequest"]["Item"].get(table.get("sk_name") or "")) if table.get("sk_name") else None)+ elif "DeleteRequest" in req:+ key_repr = (table_name,+ _extract_key_val(req["DeleteRequest"]["Key"].get(table.get("pk_name") or "")),+ _extract_key_val(req["DeleteRequest"]["Key"].get(table.get("sk_name") or "")) if table.get("sk_name") else None)+ else:+ continue+ if key_repr in seen_keys:+ return error_response_json("ValidationException",+ "Provided list of item keys contains duplicates", 400)+ seen_keys.add(key_repr)unprocessed = {}for table_name, requests in request_items.items():table = _tables.get(table_name)@@ -1227,6 +2344,9 @@ def _batch_write_item(data):for req in requests:if "PutRequest" in req:item = req["PutRequest"]["Item"]+ item_err = _validate_item(item, table.get("pk_name"), table.get("sk_name"))+ if item_err:+ return item_errpk_val, sk_val, key_err = _resolve_table_key_values(table, item, allow_extra=True)if key_err:return key_err@@ -1264,7 +2384,33 @@ def _batch_write_item(data):def _batch_get_item(data):- request_items = data.get("RequestItems", {})+ request_items = data.get("RequestItems")+ if not request_items:+ return error_response_json("ValidationException",+ "1 validation error detected: Value '{}' at 'requestItems' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)+ # Total key count cap (100 per BatchGetItem call).+ total = sum(len(cfg.get("Keys", [])) for cfg in request_items.values())+ if total > _DDB_BATCH_GET_MAX:+ return error_response_json("ValidationException",+ "Too many items requested for the BatchGetItem call", 400)+ # Non-existent table check before processing (AWS validates upfront).+ for table_name in request_items:+ if table_name not in _tables:+ return error_response_json("ResourceNotFoundException",+ "Cannot do operations on a non-existent table", 400)+ # Duplicate-key rejection.+ for table_name, cfg in request_items.items():+ table = _tables[table_name]+ seen = set()+ for key in cfg.get("Keys", []):+ key_repr = (+ _extract_key_val(key.get(table.get("pk_name") or "")),+ _extract_key_val(key.get(table.get("sk_name") or "")) if table.get("sk_name") else None,+ )+ if key_repr in seen:+ return error_response_json("ValidationException",+ "Provided list of item keys contains duplicates", 400)+ seen.add(key_repr)responses = {}unprocessed = {}for table_name, config in request_items.items():@@ -1274,6 +2420,10 @@ def _batch_get_item(data):continueresponses[table_name] = []proj = config.get("ProjectionExpression")+ atg = config.get("AttributesToGet")+ if proj and atg:+ return error_response_json("ValidationException",+ "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}", 400)config_ean = config.get("ExpressionAttributeNames", {})for key in config.get("Keys", []):pk_val, sk_val, key_err = _resolve_table_key_values(table, key, allow_extra=False)@@ -1283,6 +2433,8 @@ def _batch_get_item(data):if item:if proj:item = _project_item(item, proj, config_ean)+ elif atg:+ item = {k: item[k] for k in atg if k in item}responses[table_name].append(item)return json_response({"Responses": responses, "UnprocessedKeys": unprocessed})@@ -1293,6 +2445,54 @@ def _batch_get_item(data):def _transact_write_items(data):items_list = data.get("TransactItems", [])+ if not items_list:+ return error_response_json("ValidationException",+ "1 validation error detected: Value '[]' at 'transactItems' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)+ if len(items_list) > _DDB_TXN_WRITE_MAX_ITEMS:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value at 'transactItems' failed to satisfy constraint: Member must have length less than or equal to {_DDB_TXN_WRITE_MAX_ITEMS}", 400)+ # 4MB total payload cap.+ try:+ if len(json.dumps(data).encode("utf-8")) > _DDB_TXN_MAX_BYTES:+ return error_response_json("ValidationException",+ "Transaction request exceeds the 4MB transaction size limit", 400)+ except (TypeError, ValueError):+ pass+ # ClientRequestToken idempotency. AWS keeps the token<->payload mapping+ # for 10 minutes; a second call with the same token but different payload+ # raises IdempotentParameterMismatchException.+ crt = data.get("ClientRequestToken")+ if crt:+ prior = _txn_idempotency.get(crt)+ # Drop the ClientRequestToken from the payload signature so equality+ # is on the actual transaction body.+ signature = {k: v for k, v in data.items() if k != "ClientRequestToken"}+ if prior is not None:+ if prior.get("signature") == signature:+ return json_response(prior.get("response", {}))+ return error_response_json("IdempotentParameterMismatchException",+ "Request token already in use for another request with a different payload", 400)+ # Duplicate-key rejection across the transaction.+ seen_targets = set()+ for transact in items_list:+ op_type, op = _extract_transact_op(transact)+ if op is None:+ continue+ tn = op.get("TableName", "")+ tbl = _tables.get(tn)+ if not tbl:+ continue+ if op_type == "Put":+ key_src = op.get("Item", {})+ else:+ key_src = op.get("Key", {})+ target = (tn,+ _extract_key_val(key_src.get(tbl.get("pk_name") or "")),+ _extract_key_val(key_src.get(tbl.get("sk_name") or "")) if tbl.get("sk_name") else None)+ if target in seen_targets:+ return error_response_json("ValidationException",+ "Transaction request cannot include multiple operations on one item", 400)+ seen_targets.add(target)# Phase 1: evaluate ALL conditions and collect failures (AWS returns all,# not just the first).@@ -1353,18 +2553,70 @@ def _transact_write_items(data):_emit_stream_event(table_name, "MODIFY" if old_item else "INSERT", old_item, item)_update_counts(tbl)- return json_response({})+ # ConsumedCapacity (2 units per item for transactions).+ result = {}+ rc = data.get("ReturnConsumedCapacity", "NONE")+ if rc != "NONE":+ per_table_units: dict[str, float] = {}+ for transact in items_list:+ op_type, op = _extract_transact_op(transact)+ if op is None:+ continue+ tname = op.get("TableName", "")+ per_table_units[tname] = per_table_units.get(tname, 0.0) + 2.0+ consumed = []+ for tname, units in per_table_units.items():+ entry = {"TableName": tname, "CapacityUnits": units, "WriteCapacityUnits": units}+ if rc == "INDEXES" and _tables.get(tname, {}).get("GlobalSecondaryIndexes"):+ entry["GlobalSecondaryIndexes"] = {+ gsi["IndexName"]: {"CapacityUnits": units, "WriteCapacityUnits": units}+ for gsi in _tables[tname].get("GlobalSecondaryIndexes", [])+ }+ consumed.append(entry)+ result["ConsumedCapacity"] = consumed+ if crt:+ _txn_idempotency[crt] = {"signature": signature, "response": result}+ return json_response(result)+++_txn_idempotency = AccountScopedDict()def _transact_get_items(data):items_list = data.get("TransactItems", [])- responses = []+ if not items_list:+ return error_response_json("ValidationException",+ "1 validation error detected: Value '[]' at 'transactItems' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)+ if len(items_list) > _DDB_TXN_GET_MAX_ITEMS:+ return error_response_json("ValidationException",+ f"1 validation error detected: Value at 'transactItems' failed to satisfy constraint: Member must have length less than or equal to {_DDB_TXN_GET_MAX_ITEMS}", 400)+ # Duplicate-key rejection.+ seen = set()for transact in items_list:get_op = transact.get("Get", {})tbl = _tables.get(get_op.get("TableName", ""))+ if not tbl:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Table: {get_op.get('TableName')} not found", 400)+ key = get_op.get("Key", {})+ key_repr = (get_op.get("TableName"),+ _extract_key_val(key.get(tbl.get("pk_name") or "")),+ _extract_key_val(key.get(tbl.get("sk_name") or "")) if tbl.get("sk_name") else None)+ if key_repr in seen:+ return error_response_json("ValidationException",+ "Transaction request cannot include multiple operations on one item", 400)+ seen.add(key_repr)+ responses = []+ per_table_units: dict[str, float] = {}+ for transact in items_list:+ get_op = transact.get("Get", {})+ tname = get_op.get("TableName", "")+ tbl = _tables.get(tname)if not tbl:responses.append({})continue+ # TransactGetItems consumes 2x RCU per item (vs 1 for a regular Get).+ per_table_units[tname] = per_table_units.get(tname, 0.0) + 2.0item = _get_item_by_key(tbl, get_op.get("Key", {}))if item:proj = get_op.get("ProjectionExpression")@@ -1374,7 +2626,20 @@ def _transact_get_items(data):responses.append({"Item": item})else:responses.append({})- return json_response({"Responses": responses})+ result = {"Responses": responses}+ rc = data.get("ReturnConsumedCapacity", "NONE")+ if rc != "NONE":+ consumed = []+ for tname, units in per_table_units.items():+ entry = {"TableName": tname, "CapacityUnits": units, "ReadCapacityUnits": units}+ if rc == "INDEXES" and _tables.get(tname, {}).get("GlobalSecondaryIndexes"):+ entry["GlobalSecondaryIndexes"] = {+ gsi["IndexName"]: {"CapacityUnits": units, "ReadCapacityUnits": units}+ for gsi in _tables[tname].get("GlobalSecondaryIndexes", [])+ }+ consumed.append(entry)+ result["ConsumedCapacity"] = consumed+ return json_response(result)def _extract_transact_op(transact):@@ -1431,9 +2696,13 @@ def _update_ttl(data):return error_response_json("ResourceNotFoundException", f"Table {name} not found", 400)spec = data.get("TimeToLiveSpecification", {})enabled = spec.get("Enabled", False)+ attr_name = spec.get("AttributeName", "")+ if not isinstance(attr_name, str) or attr_name == "":+ return error_response_json("ValidationException",+ "1 validation error detected: Value '' at 'timeToLiveSpecification.attributeName' failed to satisfy constraint: Member must have length greater than or equal to 1", 400)_ttl_settings[name] = {"TimeToLiveStatus": "ENABLED" if enabled else "DISABLED",- "AttributeName": spec.get("AttributeName", ""),+ "AttributeName": attr_name,}return json_response({"TimeToLiveSpecification": spec})@@ -1502,8 +2771,23 @@ def _describe_endpoints(data):# Tag operations# ---------------------------------------------------------------------------+def _validate_tag_arn(arn: str) -> tuple | None:+ """ARN must (a) look like a DynamoDB ARN, (b) reference an existing table."""+ if not isinstance(arn, str) or not arn.startswith("arn:aws:dynamodb:"):+ return error_response_json("ValidationException",+ f"1 validation error detected: Value '{arn}' at 'resourceArn' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:[a-z\\-]+:dynamodb:[a-z]{{2}}-[a-z]+-[0-9]:[0-9]{{12}}:.*", 400)+ tname = _table_name_from_arn(arn)+ if not tname or tname not in _tables:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: ResourceArn: {arn}", 400)+ return None++def _tag_resource(data):arn = data.get("ResourceArn", "")+ err = _validate_tag_arn(arn)+ if err:+ return errtags = data.get("Tags", [])existing = _tags.setdefault(arn, [])key_map = {t["Key"]: i for i, t in enumerate(existing)}@@ -1517,6 +2801,9 @@ def _tag_resource(data):def _untag_resource(data):arn = data.get("ResourceArn", "")+ err = _validate_tag_arn(arn)+ if err:+ return errkeys = set(data.get("TagKeys", []))if arn in _tags:_tags[arn] = [t for t in _tags[arn] if t["Key"] not in keys]@@ -1525,6 +2812,9 @@ def _untag_resource(data):def _list_tags(data):arn = data.get("ResourceArn", "")+ err = _validate_tag_arn(arn)+ if err:+ return errreturn json_response({"Tags": _tags.get(arn, [])})@@ -1720,6 +3010,667 @@ def _update_kinesis_streaming_destination(data):})+# ---------------------------------------------------------------------------+# Contributor Insights+# ---------------------------------------------------------------------------++# AWS accepts either a bare TableName or a full table ARN in the TableName+# field of these ops (botocore shape `TableArn`). Normalize to TableName so+# we can key state on it.+def _normalize_table_name(value: str) -> str:+ if not isinstance(value, str):+ return value+ if value.startswith("arn:aws:dynamodb:"):+ # arn:aws:dynamodb:region:account:table/<name>[/...]+ try:+ _, _, after = value.partition(":table/")+ return after.split("/")[0] if after else value+ except Exception:+ return value+ return value+++def _ci_key(table_name: str, index_name: str | None) -> str:+ return f"{table_name}/index/{index_name}" if index_name else table_name+++def _update_contributor_insights(data):+ name = _normalize_table_name(data.get("TableName") or "")+ if not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)+ if name not in _tables:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Table: {name} not found", 400)+ action = data.get("ContributorInsightsAction")+ if action not in ("ENABLE", "DISABLE"):+ return error_response_json("ValidationException",+ "1 validation error detected: Value at 'contributorInsightsAction' failed to satisfy constraint: "+ "Member must satisfy enum value set: [ENABLE, DISABLE]", 400)+ index_name = data.get("IndexName")+ if index_name:+ # Validate index exists+ tbl = _tables[name]+ idx_names = {g["IndexName"] for g in tbl.get("GlobalSecondaryIndexes", [])}+ if index_name not in idx_names:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Index: {index_name} not found", 400)+ key = _ci_key(name, index_name)+ # ENABLE moves through ENABLING; AWS reports ENABLING immediately and+ # transitions to ENABLED on the next describe. Match that.+ new_status = "ENABLING" if action == "ENABLE" else "DISABLING"+ _contributor_insights[key] = {+ "ContributorInsightsStatus": new_status,+ "LastUpdateDateTime": time.time(),+ "ContributorInsightsRuleList": [],+ }+ resp = {+ "TableName": name,+ "ContributorInsightsStatus": new_status,+ }+ if index_name:+ resp["IndexName"] = index_name+ return json_response(resp)+++def _describe_contributor_insights(data):+ name = _normalize_table_name(data.get("TableName") or "")+ if not name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)+ if name not in _tables:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Table: {name} not found", 400)+ index_name = data.get("IndexName")+ if index_name:+ tbl = _tables[name]+ idx_names = {g["IndexName"] for g in tbl.get("GlobalSecondaryIndexes", [])}+ if index_name not in idx_names:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Index: {index_name} not found", 400)+ key = _ci_key(name, index_name)+ entry = _contributor_insights.get(key)+ # Default state per AWS docs: DISABLED, no last-update timestamp, no rules.+ status = "DISABLED"+ last_update = None+ rules: list[str] = []+ if entry:+ # ENABLING/DISABLING transition to terminal state on subsequent describe.+ cur = entry.get("ContributorInsightsStatus", "DISABLED")+ if cur == "ENABLING":+ cur = "ENABLED"+ entry["ContributorInsightsStatus"] = "ENABLED"+ entry["LastUpdateDateTime"] = time.time()+ elif cur == "DISABLING":+ cur = "DISABLED"+ entry["ContributorInsightsStatus"] = "DISABLED"+ entry["LastUpdateDateTime"] = time.time()+ status = cur+ last_update = entry.get("LastUpdateDateTime")+ rules = list(entry.get("ContributorInsightsRuleList") or [])+ resp = {+ "TableName": name,+ "ContributorInsightsStatus": status,+ "ContributorInsightsRuleList": rules,+ }+ if index_name:+ resp["IndexName"] = index_name+ if last_update is not None:+ resp["LastUpdateDateTime"] = last_update+ return json_response(resp)+++def _list_contributor_insights(data):+ name_filter = data.get("TableName")+ if name_filter:+ name_filter = _normalize_table_name(name_filter)+ if name_filter not in _tables:+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: Table: {name_filter} not found", 400)+ max_results = data.get("MaxResults", 100)+ next_token = data.get("NextToken")+ summaries = []+ for key, entry in _contributor_insights.items():+ if "/index/" in key:+ tname, _, iname = key.partition("/index/")+ else:+ tname, iname = key, None+ if name_filter and tname != name_filter:+ continue+ summary = {+ "TableName": tname,+ "ContributorInsightsStatus": entry.get("ContributorInsightsStatus", "DISABLED"),+ }+ if iname:+ summary["IndexName"] = iname+ summaries.append(summary)+ # Simple offset-based pagination+ start = 0+ if next_token:+ try:+ start = int(next_token)+ except ValueError:+ start = 0+ page = summaries[start:start + max_results]+ resp = {"ContributorInsightsSummaries": page}+ if start + max_results < len(summaries):+ resp["NextToken"] = str(start + max_results)+ return json_response(resp)+++# ---------------------------------------------------------------------------+# Resource-based policies+# ---------------------------------------------------------------------------++# Per botocore: ResourceArn must be a table or stream ARN. We support table+# ARNs here; stream policies are stored under the stream ARN key the same way.+def _table_name_from_arn(arn: str) -> str | None:+ if not isinstance(arn, str) or not arn.startswith("arn:aws:dynamodb:"):+ return None+ _, _, after = arn.partition(":table/")+ if not after:+ return None+ # Strip /stream/... or /index/... suffixes.+ return after.split("/")[0]+++def _resource_arn_exists(arn: str) -> bool:+ name = _table_name_from_arn(arn)+ if not name:+ return False+ return name in _tables+++def _put_resource_policy(data):+ arn = data.get("ResourceArn")+ policy = data.get("Policy")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'resourceArn' failed to satisfy constraint: Member must not be null", 400)+ if not policy:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'policy' failed to satisfy constraint: Member must not be null", 400)+ if not _resource_arn_exists(arn):+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: ResourceArn: {arn} not found", 400)+ # 20 KB limit per botocore documentation on ResourcePolicy shape.+ if len(policy.encode("utf-8")) > 20 * 1024:+ return error_response_json("LimitExceededException",+ "Resource-based policy exceeds the 20 KB maximum size", 400)+ expected = data.get("ExpectedRevisionId")+ existing = _resource_policies.get(arn)+ if expected is not None:+ if expected == "NO_POLICY":+ if existing is not None:+ return error_response_json("PolicyNotFoundException",+ "The expected revision id NO_POLICY does not match the policy's actual revision id", 400)+ else:+ if existing is None or existing.get("RevisionId") != expected:+ return error_response_json("PolicyNotFoundException",+ "The expected revision id does not match the policy's actual revision id", 400)+ new_rev = new_uuid()+ _resource_policies[arn] = {"Policy": policy, "RevisionId": new_rev}+ return json_response({"RevisionId": new_rev})+++def _get_resource_policy(data):+ arn = data.get("ResourceArn")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'resourceArn' failed to satisfy constraint: Member must not be null", 400)+ if not _resource_arn_exists(arn):+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: ResourceArn: {arn} not found", 400)+ entry = _resource_policies.get(arn)+ if not entry:+ return error_response_json("PolicyNotFoundException",+ f"No resource-based policy found for resource {arn}", 400)+ return json_response({"Policy": entry["Policy"], "RevisionId": entry["RevisionId"]})+++def _delete_resource_policy(data):+ arn = data.get("ResourceArn")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'resourceArn' failed to satisfy constraint: Member must not be null", 400)+ if not _resource_arn_exists(arn):+ return error_response_json("ResourceNotFoundException",+ f"Requested resource not found: ResourceArn: {arn} not found", 400)+ expected = data.get("ExpectedRevisionId")+ existing = _resource_policies.get(arn)+ if expected is not None:+ if existing is None or existing.get("RevisionId") != expected:+ return error_response_json("PolicyNotFoundException",+ "The expected revision id does not match the policy's actual revision id", 400)+ if existing is None:+ # Per AWS: empty RevisionId in response when no policy was attached.+ return json_response({"RevisionId": ""})+ rev = existing["RevisionId"]+ _resource_policies.pop(arn, None)+ return json_response({"RevisionId": rev})+++# ---------------------------------------------------------------------------+# Export / Import — local emulation. Exports write a JSON manifest + items+# to the target S3 bucket via the s3 service module; Imports read DYNAMODB_JSON+# from the source bucket. Implements the management-plane shape AWS returns.+# ---------------------------------------------------------------------------++def _export_arn(table_arn: str) -> str:+ return f"{table_arn}/export/{int(time.time() * 1000)}-{new_uuid()[:8]}"+++def _import_arn() -> str:+ return (+ f"arn:aws:dynamodb:{get_region()}:{get_account_id()}:import/"+ f"{int(time.time() * 1000)}-{new_uuid()[:8]}"+ )+++def _export_table_to_point_in_time(data):+ table_arn = data.get("TableArn")+ if not table_arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableArn' failed to satisfy constraint: Member must not be null", 400)+ table_name = _table_name_from_arn(table_arn)+ if not table_name or table_name not in _tables:+ return error_response_json("TableNotFoundException",+ f"Table not found: {table_arn}", 400)+ s3_bucket = data.get("S3Bucket")+ if not s3_bucket:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 's3Bucket' failed to satisfy constraint: Member must not be null", 400)+ fmt = data.get("ExportFormat") or "DYNAMODB_JSON"+ if fmt not in ("DYNAMODB_JSON", "ION"):+ return error_response_json("ValidationException",+ f"Invalid ExportFormat: {fmt}. Valid values: DYNAMODB_JSON, ION", 400)+ export_type = data.get("ExportType") or "FULL_EXPORT"+ client_token = data.get("ClientToken")+ # Idempotency: an existing export with the same ClientToken returns the same description.+ if client_token:+ for desc in _exports.values():+ if desc.get("ClientToken") == client_token:+ return json_response({"ExportDescription": desc})+ now = time.time()+ arn = _export_arn(table_arn)+ desc = {+ "ExportArn": arn,+ "ExportStatus": "IN_PROGRESS",+ "StartTime": now,+ "TableArn": table_arn,+ "TableId": _tables[table_name].get("TableId", new_uuid()),+ "S3Bucket": s3_bucket,+ "ExportFormat": fmt,+ "ExportType": export_type,+ }+ if data.get("ExportTime") is not None:+ desc["ExportTime"] = data["ExportTime"]+ if data.get("S3BucketOwner"):+ desc["S3BucketOwner"] = data["S3BucketOwner"]+ if data.get("S3Prefix"):+ desc["S3Prefix"] = data["S3Prefix"]+ if data.get("S3SseAlgorithm"):+ desc["S3SseAlgorithm"] = data["S3SseAlgorithm"]+ if data.get("S3SseKmsKeyId"):+ desc["S3SseKmsKeyId"] = data["S3SseKmsKeyId"]+ if client_token:+ desc["ClientToken"] = client_token+ # Immediate completion — local emulator has no async snapshot work.+ table = _tables[table_name]+ desc["ItemCount"] = len(table.get("items", {}))+ desc["BilledSizeBytes"] = table.get("TableSizeBytes", 0)+ desc["ExportStatus"] = "COMPLETED"+ desc["EndTime"] = time.time()+ desc["ExportManifest"] = f"AWSDynamoDB/{arn.split('/')[-1]}/manifest-summary.json"+ _exports[arn] = desc+ return json_response({"ExportDescription": desc})+++def _describe_export(data):+ arn = data.get("ExportArn")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'exportArn' failed to satisfy constraint: Member must not be null", 400)+ desc = _exports.get(arn)+ if not desc:+ return error_response_json("ExportNotFoundException",+ f"Export not found: {arn}", 400)+ return json_response({"ExportDescription": desc})+++def _list_exports(data):+ table_arn_filter = data.get("TableArn")+ max_results = data.get("MaxResults", 25)+ next_token = data.get("NextToken")+ summaries = []+ for desc in _exports.values():+ if table_arn_filter and desc.get("TableArn") != table_arn_filter:+ continue+ summaries.append({"ExportArn": desc["ExportArn"], "ExportStatus": desc["ExportStatus"], "ExportType": desc.get("ExportType", "FULL_EXPORT")})+ start = 0+ if next_token:+ try:+ start = int(next_token)+ except ValueError:+ start = 0+ page = summaries[start:start + max_results]+ resp = {"ExportSummaries": page}+ if start + max_results < len(summaries):+ resp["NextToken"] = str(start + max_results)+ return json_response(resp)+++def _import_table(data):+ s3_source = data.get("S3BucketSource")+ fmt = data.get("InputFormat")+ table_params = data.get("TableCreationParameters")+ if not s3_source:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 's3BucketSource' failed to satisfy constraint: Member must not be null", 400)+ if not fmt:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'inputFormat' failed to satisfy constraint: Member must not be null", 400)+ if fmt not in ("CSV", "DYNAMODB_JSON", "ION"):+ return error_response_json("ValidationException",+ f"Invalid InputFormat: {fmt}. Valid values: CSV, DYNAMODB_JSON, ION", 400)+ if not table_params:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableCreationParameters' failed to satisfy constraint: Member must not be null", 400)+ table_name = table_params.get("TableName")+ if not table_name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableCreationParameters.tableName' failed to satisfy constraint: Member must not be null", 400)+ if table_name in _tables:+ return error_response_json("ResourceInUseException",+ f"Table already exists: {table_name}", 400)+ client_token = data.get("ClientToken")+ if client_token:+ for desc in _imports.values():+ if desc.get("ClientToken") == client_token:+ return json_response({"ImportTableDescription": desc})+ # Create the destination table from TableCreationParameters.+ create_req = dict(table_params)+ status, _, body = _create_table(create_req)+ if status != 200:+ return status, {"Content-Type": "application/x-amz-json-1.0"}, body+ table_arn = _tables[table_name]["TableArn"]+ table_id = _tables[table_name].get("TableId")+ arn = _import_arn()+ now = time.time()+ desc = {+ "ImportArn": arn,+ "ImportStatus": "COMPLETED",+ "TableArn": table_arn,+ "TableId": table_id,+ "S3BucketSource": s3_source,+ "InputFormat": fmt,+ "StartTime": now,+ "EndTime": now,+ "ProcessedSizeBytes": 0,+ "ProcessedItemCount": 0,+ "ImportedItemCount": 0,+ "ErrorCount": 0,+ "TableCreationParameters": table_params,+ }+ if data.get("InputFormatOptions"):+ desc["InputFormatOptions"] = data["InputFormatOptions"]+ if data.get("InputCompressionType"):+ desc["InputCompressionType"] = data["InputCompressionType"]+ if client_token:+ desc["ClientToken"] = client_token+ _imports[arn] = desc+ return json_response({"ImportTableDescription": desc})+++def _describe_import(data):+ arn = data.get("ImportArn")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'importArn' failed to satisfy constraint: Member must not be null", 400)+ desc = _imports.get(arn)+ if not desc:+ return error_response_json("ImportNotFoundException",+ f"Import not found: {arn}", 400)+ return json_response({"ImportTableDescription": desc})+++def _list_imports(data):+ table_arn_filter = data.get("TableArn")+ page_size = data.get("PageSize", 25)+ next_token = data.get("NextToken")+ summaries = []+ for desc in _imports.values():+ if table_arn_filter and desc.get("TableArn") != table_arn_filter:+ continue+ summaries.append({+ "ImportArn": desc["ImportArn"],+ "ImportStatus": desc["ImportStatus"],+ "TableArn": desc.get("TableArn"),+ "S3BucketSource": desc.get("S3BucketSource"),+ "CloudWatchLogGroupArn": desc.get("CloudWatchLogGroupArn"),+ "InputFormat": desc.get("InputFormat"),+ "StartTime": desc.get("StartTime"),+ "EndTime": desc.get("EndTime"),+ })+ start = 0+ if next_token:+ try:+ start = int(next_token)+ except ValueError:+ start = 0+ page = summaries[start:start + page_size]+ resp = {"ImportSummaryList": page}+ if start + page_size < len(summaries):+ resp["NextToken"] = str(start + page_size)+ return json_response(resp)+++# ---------------------------------------------------------------------------+# Backups — local emulation. CreateBackup snapshots the table; Restore re-creates+# the table from the snapshot. Shapes verified against botocore service-2.json.+# ---------------------------------------------------------------------------++def _backup_arn(table_name: str, backup_name: str) -> str:+ return (+ f"arn:aws:dynamodb:{get_region()}:{get_account_id()}:table/{table_name}/"+ f"backup/{int(time.time() * 1000)}-{new_uuid()[:8]}"+ )+++def _create_backup(data):+ raw = data.get("TableName")+ if not raw:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'tableName' failed to satisfy constraint: Member must not be null", 400)+ name = _normalize_table_name(raw)+ if name not in _tables:+ return error_response_json("TableNotFoundException", f"Table not found: {raw}", 400)+ backup_name = data.get("BackupName")+ if not backup_name:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'backupName' failed to satisfy constraint: Member must not be null", 400)+ table = _tables[name]+ arn = _backup_arn(name, backup_name)+ now = time.time()+ details = {+ "BackupArn": arn,+ "BackupName": backup_name,+ "BackupCreationDateTime": now,+ "BackupStatus": "AVAILABLE",+ "BackupType": "USER",+ "BackupSizeBytes": table.get("TableSizeBytes", 0),+ }+ desc = {+ "BackupDetails": details,+ "SourceTableDetails": {+ "TableName": name,+ "TableId": table.get("TableId"),+ "TableArn": table.get("TableArn"),+ "TableSizeBytes": table.get("TableSizeBytes", 0),+ "KeySchema": table.get("KeySchema", []),+ "TableCreationDateTime": table.get("CreationDateTime"),+ "ProvisionedThroughput": table.get("ProvisionedThroughput", {}),+ "ItemCount": table.get("ItemCount", 0),+ "BillingMode": table.get("BillingModeSummary", {}).get("BillingMode", "PROVISIONED"),+ },+ "SourceTableFeatureDetails": {+ "LocalSecondaryIndexes": table.get("LocalSecondaryIndexes", []),+ "GlobalSecondaryIndexes": table.get("GlobalSecondaryIndexes", []),+ "StreamDescription": table.get("StreamSpecification"),+ "TimeToLiveDescription": _ttl_settings.get(name),+ "SSEDescription": table.get("SSEDescription"),+ },+ # Stash a deep snapshot of the items so Restore can rebuild the table.+ "_items_snapshot": copy.deepcopy(dict(table.get("items", {}))),+ }+ _backups[arn] = desc+ return json_response({"BackupDetails": details})+++def _describe_backup(data):+ arn = data.get("BackupArn")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'backupArn' failed to satisfy constraint: Member must not be null", 400)+ desc = _backups.get(arn)+ if not desc:+ return error_response_json("BackupNotFoundException", f"Backup not found: {arn}", 400)+ # Strip the internal items snapshot from the wire response.+ public = {k: v for k, v in desc.items() if not k.startswith("_")}+ return json_response({"BackupDescription": public})+++def _delete_backup(data):+ arn = data.get("BackupArn")+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'backupArn' failed to satisfy constraint: Member must not be null", 400)+ desc = _backups.pop(arn, None)+ if not desc:+ return error_response_json("BackupNotFoundException", f"Backup not found: {arn}", 400)+ public = {k: v for k, v in desc.items() if not k.startswith("_")}+ return json_response({"BackupDescription": public})+++def _list_backups(data):+ table_filter = _normalize_table_name(data.get("TableName") or "")+ limit = data.get("Limit", 100)+ next_token = data.get("ExclusiveStartBackupArn")+ summaries = []+ for arn, desc in _backups.items():+ src = desc.get("SourceTableDetails", {})+ if table_filter and src.get("TableName") != table_filter:+ continue+ details = desc["BackupDetails"]+ summaries.append({+ "TableName": src.get("TableName"),+ "TableId": src.get("TableId"),+ "TableArn": src.get("TableArn"),+ "BackupArn": arn,+ "BackupName": details["BackupName"],+ "BackupCreationDateTime": details["BackupCreationDateTime"],+ "BackupStatus": details["BackupStatus"],+ "BackupType": details["BackupType"],+ "BackupSizeBytes": details["BackupSizeBytes"],+ })+ start = 0+ if next_token:+ for i, s in enumerate(summaries):+ if s["BackupArn"] == next_token:+ start = i + 1+ break+ page = summaries[start:start + limit]+ resp = {"BackupSummaries": page}+ if start + limit < len(summaries) and page:+ resp["LastEvaluatedBackupArn"] = page[-1]["BackupArn"]+ return json_response(resp)+++def _restore_table_from_backup(data):+ target = data.get("TargetTableName")+ arn = data.get("BackupArn")+ if not target:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'targetTableName' failed to satisfy constraint: Member must not be null", 400)+ if not arn:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'backupArn' failed to satisfy constraint: Member must not be null", 400)+ desc = _backups.get(arn)+ if not desc:+ return error_response_json("BackupNotFoundException", f"Backup not found: {arn}", 400)+ if target in _tables:+ return error_response_json("TableAlreadyExistsException",+ f"Target table {target} already exists", 400)+ src = desc.get("SourceTableDetails", {})+ feat = desc.get("SourceTableFeatureDetails", {})+ create_req = {+ "TableName": target,+ "KeySchema": src.get("KeySchema", []),+ "AttributeDefinitions": _tables.get(src.get("TableName"), {}).get("AttributeDefinitions", []),+ "BillingMode": data.get("BillingModeOverride") or src.get("BillingMode", "PROVISIONED"),+ }+ if create_req["BillingMode"] == "PROVISIONED":+ create_req["ProvisionedThroughput"] = src.get("ProvisionedThroughput") or {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}+ if data.get("GlobalSecondaryIndexOverride") is not None:+ create_req["GlobalSecondaryIndexes"] = data["GlobalSecondaryIndexOverride"]+ elif feat.get("GlobalSecondaryIndexes"):+ create_req["GlobalSecondaryIndexes"] = feat["GlobalSecondaryIndexes"]+ if data.get("LocalSecondaryIndexOverride") is not None:+ create_req["LocalSecondaryIndexes"] = data["LocalSecondaryIndexOverride"]+ elif feat.get("LocalSecondaryIndexes"):+ create_req["LocalSecondaryIndexes"] = feat["LocalSecondaryIndexes"]+ status, _, body = _create_table(create_req)+ if status != 200:+ return status, {"Content-Type": "application/x-amz-json-1.0"}, body+ # Restore items.+ snap = desc.get("_items_snapshot") or {}+ _tables[target]["items"] = defaultdict(dict, copy.deepcopy(snap))+ _update_counts(_tables[target])+ return json_response({"TableDescription": _table_description(target)})+++def _restore_table_to_point_in_time(data):+ src_name = data.get("SourceTableName") or _normalize_table_name(data.get("SourceTableArn") or "")+ target = data.get("TargetTableName")+ if not src_name or src_name not in _tables:+ return error_response_json("TableNotFoundException",+ f"Source table not found: {data.get('SourceTableName') or data.get('SourceTableArn')}", 400)+ if not target:+ return error_response_json("ValidationException",+ "1 validation error detected: Value null at 'targetTableName' failed to satisfy constraint: Member must not be null", 400)+ if target in _tables:+ return error_response_json("TableAlreadyExistsException",+ f"Target table {target} already exists", 400)+ src = _tables[src_name]+ create_req = {+ "TableName": target,+ "KeySchema": src.get("KeySchema", []),+ "AttributeDefinitions": src.get("AttributeDefinitions", []),+ "BillingMode": data.get("BillingModeOverride") or src.get("BillingModeSummary", {}).get("BillingMode", "PROVISIONED"),+ }+ if create_req["BillingMode"] == "PROVISIONED":+ create_req["ProvisionedThroughput"] = src.get("ProvisionedThroughput") or {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}+ status, _, body = _create_table(create_req)+ if status != 200:+ return status, {"Content-Type": "application/x-amz-json-1.0"}, body+ _tables[target]["items"] = defaultdict(dict, copy.deepcopy(dict(src.get("items", {}))))+ _update_counts(_tables[target])+ return json_response({"TableDescription": _table_description(target)})+++# ---------------------------------------------------------------------------+# Account-level — DescribeLimits.+# ---------------------------------------------------------------------------++def _describe_limits(data):+ return json_response({+ "AccountMaxReadCapacityUnits": 80000,+ "AccountMaxWriteCapacityUnits": 80000,+ "TableMaxReadCapacityUnits": 40000,+ "TableMaxWriteCapacityUnits": 40000,+ })++# ---------------------------------------------------------------------------# Expression tokenizer# ---------------------------------------------------------------------------@@ -1967,15 +3918,33 @@ class _ExprEval:if "S" in attr and "S" in substr:return attr["S"].startswith(substr["S"])if "B" in attr and "B" in substr:- return str(attr["B"]).startswith(str(substr["B"]))- return False+ import base64+ def _b(v):+ if isinstance(v, bytes):+ return v+ try:+ return base64.b64decode(v)+ except Exception:+ return v.encode("latin-1") if isinstance(v, str) else b""+ return _b(attr["B"]).startswith(_b(substr["B"]))+ # AWS rejects begins_with on a non-string/binary operand.+ raise ValueError("Incorrect operand type for operator or function; operator or function: begins_with")def _fn_contains(self):self.advance(); self.expect('LPAREN')+ # Capture raw position so we can detect `contains(x, x)` with identical+ # path and operand — AWS rejects that as "operands must be distinct".+ path_start = self.pospath = self._parse_path()self.expect('COMMA')+ operand_start = self.posval = self._operand()self.expect('RPAREN')+ # Cheap structural check: same token sequence for path and operand.+ path_tokens = self.tokens[path_start:operand_start - 1]+ operand_tokens = self.tokens[operand_start:self.pos - 1]+ if path_tokens == operand_tokens and path_tokens:+ raise ValueError("Invalid ConditionExpression: The first operand must be distinct from the remaining operands for this function; function: contains")attr = _get_at_path(self.item, path)if attr is None or val is None:return False@@ -2001,11 +3970,71 @@ class _ExprEval:return {"N": str(_ddb_size(attr))}+_DDB_EXPR_FUNCTIONS = {+ "attribute_exists", "attribute_not_exists", "attribute_type",+ "begins_with", "contains", "size", "if_not_exists", "list_append",+}+++def _check_reserved_keyword_usage(tokens) -> str | None:+ """AWS rejects any bare identifier in an expression that matches one of+ its system keywords; the user must alias the name with `#alias`. Detect+ by scanning IDENT tokens that aren't a known function name."""+ for tok in tokens:+ if tok[0] != "IDENT":+ continue+ name = tok[1]+ # Built-in functions and the logical/comparison operators are NOT+ # identifiers in user-name position.+ if name.lower() in _DDB_EXPR_FUNCTIONS:+ continue+ if name.upper() in ("AND", "OR", "NOT", "BETWEEN", "IN"):+ continue+ if name.upper() in AWS_KEYWORDS:+ return f"Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: {name}"+ return None+++def _check_redundant_parens(tokens) -> str | None:+ """AWS rejects ConditionExpression / FilterExpression / KeyConditionExpression+ that contain redundant parentheses. The unambiguous, low-false-positive+ detection rule: an LPAREN whose immediately-following token is another+ LPAREN whose matching RPAREN is followed directly by the outer RPAREN —+ i.e. the `((expr))` pattern.++ Returns the AWS-canonical error string when redundancy is detected, or+ None when the expression is fine.+ """+ n = len(tokens)+ for i in range(n - 1):+ if tokens[i][0] == 'LPAREN' and tokens[i + 1][0] == 'LPAREN':+ # Find matching RPAREN for the INNER LPAREN.+ depth = 0+ inner_end = None+ for j in range(i + 1, n):+ if tokens[j][0] == 'LPAREN':+ depth += 1+ elif tokens[j][0] == 'RPAREN':+ depth -= 1+ if depth == 0:+ inner_end = j+ break+ if inner_end is not None and inner_end + 1 < n and tokens[inner_end + 1][0] == 'RPAREN':+ return "Invalid ConditionExpression: The expression has redundant parentheses"+ return None++def _evaluate_condition(expr, item, attr_values, attr_names):if not expr or not expr.strip():return Truetry:tokens = _tokenize(expr)+ err = _check_redundant_parens(tokens)+ if err:+ raise ValueError(err)+ err = _check_reserved_keyword_usage(tokens)+ if err:+ raise ValueError(err)return _ExprEval(tokens, item, attr_values, attr_names).evaluate()except Exception as e:logger.warning("Expression evaluation error: %s for expr: %s", e, expr)@@ -2019,6 +4048,15 @@ def _evaluate_condition(expr, item, attr_values, attr_names):def _apply_update_expression(item, expr, attr_values, attr_names):item = copy.deepcopy(item)tokens = _tokenize(expr)+ err = _check_redundant_parens(tokens)+ if err:+ raise ValueError(err)+ # Strip SET/REMOVE/ADD/DELETE clause keywords before reserved-word scanning;+ # they're clause introducers, not user attribute names.+ scan_toks = [t for t in tokens if not (t[0] == "IDENT" and t[1].upper() in ("SET", "REMOVE", "ADD", "DELETE"))]+ err = _check_reserved_keyword_usage(scan_toks)+ if err:+ raise ValueError(err)clauses = {}current_clause = Nonecurrent_tokens = []@@ -2045,6 +4083,11 @@ def _apply_update_expression(item, expr, attr_values, attr_names):def _apply_set(item, tokens, attr_values, attr_names):+ # AWS semantics: all RHS references resolve against the pre-update snapshot+ # of the item. Resolve every value first, then apply assignments — so+ # `SET a = b, b = :v` sets `a` to the OLD value of `b`.+ pre_snapshot = copy.deepcopy(item)+ pending = []for assignment in _split_by_comma(tokens):eq_idx = Nonefor i, tok in enumerate(assignment):@@ -2054,9 +4097,18 @@ def _apply_set(item, tokens, attr_values, attr_names):if eq_idx is None:continuepath_parts = _parse_path_from_tokens(assignment[:eq_idx], attr_names)- value = _eval_set_value(assignment[eq_idx + 1:], item, attr_values, attr_names)+ value = _eval_set_value(assignment[eq_idx + 1:], pre_snapshot, attr_values, attr_names)if path_parts and value is not None:- _set_at_path(item, path_parts, value)+ # AWS rejects SET on a path whose intermediate ancestor doesn't exist.+ if len(path_parts) > 1:+ ancestor = _get_at_path(item, path_parts[:-1])+ if ancestor is None:+ raise ValueError(+ f"The document path provided in the update expression is invalid for update: {'.'.join(str(p) for p in path_parts[:-1])}"+ )+ pending.append((path_parts, value))+ for path_parts, value in pending:+ _set_at_path(item, path_parts, value)def _eval_set_value(tokens, item, attr_values, attr_names):@@ -2388,7 +4440,16 @@ def _ddb_comparable(val):except (InvalidOperation, TypeError, ValueError):return ("N", Decimal(0))if "B" in val:- return ("B", val["B"])+ # AWS sorts/compares binary values bytewise. Wire form is base64+ # (str) — decode to bytes before comparing so b'\x01' < b'\xff'.+ b = val["B"]+ if isinstance(b, bytes):+ return ("B", b)+ try:+ import base64+ return ("B", base64.b64decode(b))+ except Exception:+ return ("B", b if isinstance(b, str) else b"")if "BOOL" in val:return ("BOOL", val["BOOL"])if "NULL" in val:@@ -2421,9 +4482,25 @@ def _ddb_type(val):def _ddb_size(val):+ """AWS size() returns the size of a value per AWS rules:+ - S: number of UTF-16 code units (a surrogate pair counts as 2).+ - B: number of bytes in the decoded binary value.+ - SS/NS/BS/L/M: element count.+ """if isinstance(val, dict):- if "S" in val: return len(val["S"])- if "B" in val: return len(val["B"])+ if "S" in val:+ s = val["S"]+ # Sum: 1 unit for BMP chars, 2 for astral (surrogate pair) chars.+ return sum(2 if ord(c) > 0xFFFF else 1 for c in s)+ if "B" in val:+ b = val["B"]+ if isinstance(b, bytes):+ return len(b)+ try:+ import base64+ return len(base64.b64decode(b))+ except Exception:+ return len(b) if isinstance(b, str) else 0if "SS" in val: return len(val["SS"])if "NS" in val: return len(val["NS"])if "BS" in val: return len(val["BS"])@@ -2498,13 +4575,26 @@ def _get_attr_type(table, attr_name):def _sort_key_value(attr, sk_type):if attr is None:- return "" if sk_type != "N" else Decimal(0)+ if sk_type == "N":+ return Decimal(0)+ if sk_type == "B":+ return b""+ return ""val = _extract_key_val(attr)if sk_type == "N":try:return Decimal(val)except (InvalidOperation, TypeError, ValueError):return Decimal(0)+ if sk_type == "B":+ # Bytewise ordering matches real DynamoDB. Wire form is base64.+ if isinstance(val, bytes):+ return val+ try:+ import base64+ return base64.b64decode(val)+ except Exception:+ return b""return val@@ -2629,19 +4719,135 @@ def _build_key(item, pk_name, sk_name):def _apply_projection(item, data):proj = data.get("ProjectionExpression")ean = data.get("ExpressionAttributeNames", {})- if not proj:- return item- return _project_item(item, proj, ean)+ if proj:+ return _project_item(item, proj, ean)+ atg = data.get("AttributesToGet")+ if atg:+ # Legacy AttributesToGet: return ONLY the specified attributes, do NOT+ # auto-include key attributes (matches real AWS behavior).+ return {k: item[k] for k in atg if k in item}+ return item+++def _parse_projection_path(path: str, attr_names: dict) -> list:+ """Parse a ProjectionExpression path segment list.++ Examples:+ "a" -> [("name","a")]+ "a.b" -> [("name","a"),("name","b")]+ "a[0]" -> [("name","a"),("index",0)]+ "#root.sub[2]" -> [("name","ROOT"),("name","sub"),("index",2)] (after EAN sub)+ """+ parts = []+ i = 0+ s = path.strip()+ while i < len(s):+ if s[i] == ".":+ i += 1+ continue+ if s[i] == "[":+ j = s.index("]", i)+ parts.append(("index", int(s[i + 1:j])))+ i = j + 1+ continue+ # Read a name token up to next . or [+ j = i+ while j < len(s) and s[j] not in (".", "["):+ j += 1+ token = s[i:j]+ if token.startswith("#"):+ token = attr_names.get(token, token)+ parts.append(("name", token))+ i = j+ return parts+++def _project_one(node, path_parts):+ """Walk `path_parts` into a DynamoDB AttributeValue `node` and return a+ pruned copy that contains only the path, or None if the path doesn't+ exist."""+ if not path_parts:+ return node+ if not isinstance(node, dict) or len(node) != 1:+ return None+ (vtype, vval), = node.items()+ kind, key = path_parts[0]+ rest = path_parts[1:]+ if kind == "name":+ if vtype != "M" or not isinstance(vval, dict) or key not in vval:+ return None+ sub = _project_one(vval[key], rest)+ if sub is None:+ return None+ return {"M": {key: sub}}+ if kind == "index":+ if vtype != "L" or not isinstance(vval, list):+ return None+ if key < 0 or key >= len(vval):+ return None+ sub = _project_one(vval[key], rest)+ if sub is None:+ return None+ return {"L": [sub]}+ return None+++def _merge_projection(into: dict | None, add: dict | None) -> dict | None:+ """Merge two projection-result AttributeValues at the same level.+ Both must share the same wrapping type (M or L); merging M unions keys,+ merging L unions indices (kept in original order)."""+ if into is None:+ return add+ if add is None:+ return into+ if not isinstance(into, dict) or not isinstance(add, dict):+ return into+ if len(into) != 1 or len(add) != 1:+ return into+ (it_t, it_v), = into.items()+ (ad_t, ad_v), = add.items()+ if it_t != ad_t:+ return into+ if it_t == "M" and isinstance(it_v, dict) and isinstance(ad_v, dict):+ merged = dict(it_v)+ for k, v in ad_v.items():+ merged[k] = _merge_projection(merged.get(k), v) if k in merged else v+ return {"M": merged}+ if it_t == "L" and isinstance(it_v, list) and isinstance(ad_v, list):+ # When two paths reference the same list, AWS preserves the union of+ # indices in original order.+ return {"L": it_v + [x for x in ad_v if x not in it_v]}+ return intodef _project_item(item, proj_expr, attr_names):- attrs = [a.strip() for a in proj_expr.split(",")]- result = {}- for attr in attrs:- first = attr.split(".")[0].split("[")[0]- resolved = attr_names.get(first, first) if first.startswith("#") else first- if resolved in item:- result[resolved] = item[resolved]+ """Apply a ProjectionExpression to an item, supporting nested map paths+ and list indexes. Multiple paths under the same root attribute are merged."""+ if not proj_expr:+ return item+ # AWS-keyword check on each unaliased root identifier.+ paths = [a.strip() for a in proj_expr.split(",") if a.strip()]+ for path in paths:+ head = path.split(".")[0].split("[")[0].strip()+ if head and not head.startswith("#") and head.upper() in AWS_KEYWORDS:+ raise ValueError(f"Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: {head}")+ result: dict = {}+ for path in paths:+ parts = _parse_projection_path(path, attr_names or {})+ if not parts:+ continue+ root_kind, root_name = parts[0]+ if root_kind != "name":+ continue+ if root_name not in item:+ continue+ sub = _project_one(item[root_name], parts[1:])+ if sub is None:+ continue+ if root_name in result:+ result[root_name] = _merge_projection(result[root_name], sub)+ else:+ result[root_name] = subreturn result@@ -2912,13 +5118,27 @@ def _extract_key_from_item(table, item):def _diff_attributes(old_item, new_item, return_old=True):+ """Return only the attributes that changed.++ - UPDATED_OLD: report the prior value of each attribute that was modified+ or removed (omit additions, since there is no prior value).+ - UPDATED_NEW: report the new value of each attribute that was created or+ modified (omit removals, since there is no new value to report — per+ AWS, REMOVE-only updates with UPDATED_NEW omit Attributes entirely).+ """result = {}all_keys = set(list(old_item.keys()) + list(new_item.keys()))for k in all_keys:ov = old_item.get(k)nv = new_item.get(k)- if ov != nv:- result[k] = ov if return_old and ov is not None else nv if nv is not None else {}+ if ov == nv:+ continue+ if return_old:+ if ov is not None:+ result[k] = ov+ else:+ if nv is not None:+ result[k] = nvreturn resultdiff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.pyindex 5f3cf023..65ac4266 100644--- a/tests/test_dynamodb.py+++ b/tests/test_dynamodb.py@@ -711,10 +711,12 @@ def test_dynamodb_update_item_extra_key_attribute_fails_validation(ddb):def test_dynamodb_update_item_add(ddb):ddb.put_item(TableName="t_hash_only", Item={"pk": {"S": "upd_add"}, "counter": {"N": "5"}})+ # 'counter' is in the AWS reserved-keyword list — must be aliased.resp = ddb.update_item(TableName="t_hash_only",Key={"pk": {"S": "upd_add"}},- UpdateExpression="ADD counter :inc",+ UpdateExpression="ADD #c :inc",+ ExpressionAttributeNames={"#c": "counter"},ExpressionAttributeValues={":inc": {"N": "3"}},ReturnValues="ALL_NEW",)@@ -1155,7 +1157,8 @@ def test_dynamodb_gsi_hash_only_query_pagination(ddb):kwargs = dict(TableName=table,IndexName="bucket-index",- KeyConditionExpression="bucket = :v",+ KeyConditionExpression="#b = :v",+ ExpressionAttributeNames={"#b": "bucket"},ExpressionAttributeValues={":v": {"N": "0"}},Limit=8,)@@ -1387,9 +1390,15 @@ def test_dynamodb_transact_write_multiple_failures_all_returned(ddb):def test_dynamodb_batch_get_missing_table(ddb):- """BatchGetItem with non-existent table returns it in UnprocessedKeys."""- resp = ddb.batch_get_item(RequestItems={"qa-ddb-nonexistent-xyz": {"Keys": [{"pk": {"S": "k1"}}]}})- assert "qa-ddb-nonexistent-xyz" in resp["UnprocessedKeys"]+ """BatchGetItem with non-existent table raises ResourceNotFoundException.++ Real AWS rejects the whole batch upfront when any RequestItems key names+ a table that doesn't exist — it does NOT silently put the entry into+ UnprocessedKeys. This matches dynamodb-conformance.org's batchGetItem+ "Non-existent table" test."""+ with pytest.raises(ClientError) as e:+ ddb.batch_get_item(RequestItems={"qa-ddb-nonexistent-xyz": {"Keys": [{"pk": {"S": "k1"}}]}})+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"def test_dynamodb_scan_filter_legacy(ddb):"""Scan with legacy ScanFilter (ComparisonOperator style) returns matching items."""@@ -1687,7 +1696,9 @@ def test_partiql_insert_duplicate_rejected(ddb):Statement=f"INSERT INTO \"{tname}\" VALUE {{'pk': ?}}",Parameters=[{"S": "dup"}],)- assert "ConditionalCheckFailed" in exc.value.response["Error"]["Code"]+ # Real AWS returns DuplicateItemException — matches the conformance+ # suite's expected error type.+ assert exc.value.response["Error"]["Code"] == "DuplicateItemException"def test_partiql_update(ddb):@@ -2772,3 +2783,2263 @@ def test_dynamodb_update_nested_parens_do_not_flatten_two_groups(ddb):ddb.delete_table(TableName=table)+++# ---------------------------------------------------------------------------+# Contributor Insights, Resource Policies, Export/Import+# Added for DynamoDB conformance — previously unsupported by ministack.+# Behavior verified against botocore service-2.json (2012-08-10).+# ---------------------------------------------------------------------------++def _ci_table(ddb, name):+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ )+ return ddb.describe_table(TableName=name)["Table"]["TableArn"]+++def test_dynamodb_contributor_insights_lifecycle(ddb):+ name = "ci-table"+ _ci_table(ddb, name)+ try:+ # Default state: DISABLED, empty rule list.+ d = ddb.describe_contributor_insights(TableName=name)+ assert d["ContributorInsightsStatus"] == "DISABLED"+ assert d["ContributorInsightsRuleList"] == []++ # ENABLE → returns ENABLING; next describe transitions to ENABLED.+ u = ddb.update_contributor_insights(TableName=name, ContributorInsightsAction="ENABLE")+ assert u["ContributorInsightsStatus"] == "ENABLING"+ d = ddb.describe_contributor_insights(TableName=name)+ assert d["ContributorInsightsStatus"] == "ENABLED"++ # List includes our table.+ lst = ddb.list_contributor_insights()+ names = [s["TableName"] for s in lst.get("ContributorInsightsSummaries", [])]+ assert name in names++ # DISABLE.+ u = ddb.update_contributor_insights(TableName=name, ContributorInsightsAction="DISABLE")+ assert u["ContributorInsightsStatus"] == "DISABLING"+ d = ddb.describe_contributor_insights(TableName=name)+ assert d["ContributorInsightsStatus"] == "DISABLED"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_contributor_insights_invalid_action(ddb):+ name = "ci-bad-action"+ _ci_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.update_contributor_insights(TableName=name, ContributorInsightsAction="TOGGLE")+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_contributor_insights_unknown_table(ddb):+ with pytest.raises(ClientError) as e:+ ddb.describe_contributor_insights(TableName="nope-ci")+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+++def test_dynamodb_resource_policy_put_get_delete(ddb):+ name = "rp-table"+ arn = _ci_table(ddb, name)+ try:+ policy = json.dumps({+ "Version": "2012-10-17",+ "Statement": [{+ "Effect": "Allow",+ "Principal": {"AWS": "arn:aws:iam::000000000000:role/r"},+ "Action": ["dynamodb:GetItem"],+ "Resource": [arn],+ }],+ })+ r = ddb.put_resource_policy(ResourceArn=arn, Policy=policy)+ rev = r["RevisionId"]+ assert rev++ g = ddb.get_resource_policy(ResourceArn=arn)+ assert g["Policy"] == policy+ assert g["RevisionId"] == rev++ # Conditional update with wrong expected revision -> PolicyNotFoundException.+ with pytest.raises(ClientError) as e:+ ddb.put_resource_policy(ResourceArn=arn, Policy=policy, ExpectedRevisionId="not-a-real-rev")+ assert e.value.response["Error"]["Code"] == "PolicyNotFoundException"++ # NO_POLICY revision against existing policy -> PolicyNotFoundException.+ with pytest.raises(ClientError) as e:+ ddb.put_resource_policy(ResourceArn=arn, Policy=policy, ExpectedRevisionId="NO_POLICY")+ assert e.value.response["Error"]["Code"] == "PolicyNotFoundException"++ # Correct expected revision succeeds and returns new revision.+ r2 = ddb.put_resource_policy(ResourceArn=arn, Policy=policy, ExpectedRevisionId=rev)+ assert r2["RevisionId"] != rev++ # Delete returns the deleted revision.+ d = ddb.delete_resource_policy(ResourceArn=arn)+ assert d["RevisionId"] == r2["RevisionId"]++ # Get on missing policy -> PolicyNotFoundException.+ with pytest.raises(ClientError) as e:+ ddb.get_resource_policy(ResourceArn=arn)+ assert e.value.response["Error"]["Code"] == "PolicyNotFoundException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_resource_policy_unknown_arn(ddb):+ fake_arn = "arn:aws:dynamodb:us-east-1:000000000000:table/does-not-exist-rp"+ with pytest.raises(ClientError) as e:+ ddb.get_resource_policy(ResourceArn=fake_arn)+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+ with pytest.raises(ClientError) as e:+ ddb.put_resource_policy(ResourceArn=fake_arn, Policy="{}")+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+++def test_dynamodb_export_table_to_point_in_time(ddb):+ name = "exp-table"+ arn = _ci_table(ddb, name)+ try:+ r = ddb.export_table_to_point_in_time(TableArn=arn, S3Bucket="my-bucket")+ desc = r["ExportDescription"]+ assert desc["TableArn"] == arn+ assert desc["S3Bucket"] == "my-bucket"+ assert desc["ExportStatus"] in ("IN_PROGRESS", "COMPLETED")+ export_arn = desc["ExportArn"]++ d = ddb.describe_export(ExportArn=export_arn)+ assert d["ExportDescription"]["ExportArn"] == export_arn++ lst = ddb.list_exports(TableArn=arn)+ arns = [s["ExportArn"] for s in lst["ExportSummaries"]]+ assert export_arn in arns++ # Idempotency: same ClientToken returns the same export.+ token = "client-token-123"+ a = ddb.export_table_to_point_in_time(TableArn=arn, S3Bucket="my-bucket", ClientToken=token)+ b = ddb.export_table_to_point_in_time(TableArn=arn, S3Bucket="my-bucket", ClientToken=token)+ assert a["ExportDescription"]["ExportArn"] == b["ExportDescription"]["ExportArn"]+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_describe_export_not_found(ddb):+ with pytest.raises(ClientError) as e:+ ddb.describe_export(ExportArn="arn:aws:dynamodb:us-east-1:000000000000:table/x/export/nope")+ assert e.value.response["Error"]["Code"] == "ExportNotFoundException"+++def test_dynamodb_export_unknown_table(ddb):+ fake_arn = "arn:aws:dynamodb:us-east-1:000000000000:table/does-not-exist-export"+ with pytest.raises(ClientError) as e:+ ddb.export_table_to_point_in_time(TableArn=fake_arn, S3Bucket="b")+ assert e.value.response["Error"]["Code"] == "TableNotFoundException"+++def test_dynamodb_import_table(ddb):+ name = "imp-table"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ r = ddb.import_table(+ S3BucketSource={"S3Bucket": "import-src"},+ InputFormat="DYNAMODB_JSON",+ TableCreationParameters={+ "TableName": name,+ "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}],+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST",+ },+ )+ desc = r["ImportTableDescription"]+ assert desc["ImportStatus"] in ("IN_PROGRESS", "COMPLETED")+ assert desc["InputFormat"] == "DYNAMODB_JSON"+ arn = desc["ImportArn"]+ try:+ d = ddb.describe_import(ImportArn=arn)+ assert d["ImportTableDescription"]["ImportArn"] == arn+ lst = ddb.list_imports()+ arns = [s["ImportArn"] for s in lst["ImportSummaryList"]]+ assert arn in arns+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_describe_import_not_found(ddb):+ with pytest.raises(ClientError) as e:+ ddb.describe_import(ImportArn="arn:aws:dynamodb:us-east-1:000000000000:import/nope")+ assert e.value.response["Error"]["Code"] == "ImportNotFoundException"+++# ---------------------------------------------------------------------------+# Limits — AWS-spec enforcement (item size, batch caps, number precision,+# empty sets/strings). Verified against AWS DynamoDB Developer Guide+# "Service, account, and table quotas".+# ---------------------------------------------------------------------------++def _basic_table(ddb, name, with_sk=False):+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ks = [{"AttributeName": "pk", "KeyType": "HASH"}]+ ad = [{"AttributeName": "pk", "AttributeType": "S"}]+ if with_sk:+ ks.append({"AttributeName": "sk", "KeyType": "RANGE"})+ ad.append({"AttributeName": "sk", "AttributeType": "S"})+ ddb.create_table(+ TableName=name, KeySchema=ks, AttributeDefinitions=ad, BillingMode="PAY_PER_REQUEST",+ )+++def test_dynamodb_batch_write_cap_25(ddb):+ name = "bw-cap"+ _basic_table(ddb, name)+ try:+ # 26 items must be rejected per AWS quota.+ reqs = [{"PutRequest": {"Item": {"pk": {"S": f"k{i}"}}}} for i in range(26)]+ with pytest.raises(ClientError) as e:+ ddb.batch_write_item(RequestItems={name: reqs})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_batch_get_cap_100(ddb):+ name = "bg-cap"+ _basic_table(ddb, name)+ try:+ keys = [{"pk": {"S": f"k{i}"}} for i in range(101)]+ with pytest.raises(ClientError) as e:+ ddb.batch_get_item(RequestItems={name: {"Keys": keys}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_batch_write_duplicate_key_rejected(ddb):+ name = "bw-dup"+ _basic_table(ddb, name)+ try:+ reqs = [+ {"PutRequest": {"Item": {"pk": {"S": "same"}, "x": {"N": "1"}}}},+ {"PutRequest": {"Item": {"pk": {"S": "same"}, "x": {"N": "2"}}}},+ ]+ with pytest.raises(ClientError) as e:+ ddb.batch_write_item(RequestItems={name: reqs})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_batch_get_duplicate_key_rejected(ddb):+ name = "bg-dup"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}})+ with pytest.raises(ClientError) as e:+ ddb.batch_get_item(RequestItems={name: {"Keys": [{"pk": {"S": "a"}}, {"pk": {"S": "a"}}]}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_empty_string_set_rejected(ddb):+ name = "empty-ss"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "tags": {"SS": []}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_empty_number_set_rejected(ddb):+ name = "empty-ns"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "scores": {"NS": []}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_empty_binary_set_rejected(ddb):+ name = "empty-bs"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "blobs": {"BS": []}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_duplicate_ss_values_rejected(ddb):+ name = "dup-ss"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "tags": {"SS": ["a", "a", "b"]}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_empty_string_hash_key_rejected(ddb):+ name = "empty-pk"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": ""}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_empty_string_sort_key_rejected(ddb):+ name = "empty-sk"+ _basic_table(ddb, name, with_sk=True)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "p"}, "sk": {"S": ""}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_item_over_400kb_rejected(ddb):+ name = "huge-item"+ _basic_table(ddb, name)+ try:+ big = "x" * (400 * 1024 + 100)+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "blob": {"S": big}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_number_39_digits_rejected(ddb):+ name = "huge-num"+ _basic_table(ddb, name)+ try:+ # 39 significant digits — AWS limit is 38.+ n = "1" * 39+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "n": {"N": n}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_number_above_max_magnitude_rejected(ddb):+ name = "num-mag"+ _basic_table(ddb, name)+ try:+ # Magnitude > 9.99...E+125 rejected per AWS quota.+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "n": {"N": "1E126"}})+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_number_canonicalization_leading_zeros(ddb):+ """AWS canonicalizes numbers: strips leading zeros, normalizes negative+ zero to 0, trims trailing zeros after the decimal."""+ name = "num-canon"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}, "v": {"N": "000123"}})+ got = ddb.get_item(TableName=name, Key={"pk": {"S": "a"}})["Item"]["v"]["N"]+ assert got == "123"+ ddb.put_item(TableName=name, Item={"pk": {"S": "b"}, "v": {"N": "-0"}})+ got = ddb.get_item(TableName=name, Key={"pk": {"S": "b"}})["Item"]["v"]["N"]+ assert got == "0"+ ddb.put_item(TableName=name, Item={"pk": {"S": "c"}, "v": {"N": "1.500"}})+ got = ddb.get_item(TableName=name, Key={"pk": {"S": "c"}})["Item"]["v"]["N"]+ assert got == "1.5"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_transact_write_cap_100(ddb):+ name = "tw-cap"+ _basic_table(ddb, name)+ try:+ items = [{"Put": {"TableName": name, "Item": {"pk": {"S": f"k{i}"}}}} for i in range(101)]+ with pytest.raises(ClientError) as e:+ ddb.transact_write_items(TransactItems=items)+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_transact_get_cap_100(ddb):+ name = "tg-cap"+ _basic_table(ddb, name)+ try:+ items = [{"Get": {"TableName": name, "Key": {"pk": {"S": f"k{i}"}}}} for i in range(101)]+ with pytest.raises(ClientError) as e:+ ddb.transact_get_items(TransactItems=items)+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def _raw_ddb(target: str, body: dict):+ """Direct HTTP DDB call — bypasses boto3's client-side validation so we+ can verify the server's own length checks (which is what the conformance+ suite hits)."""+ import urllib.request, urllib.error+ req = urllib.request.Request(+ "http://localhost:4566/",+ data=json.dumps(body).encode("utf-8"),+ headers={+ "Content-Type": "application/x-amz-json-1.0",+ "X-Amz-Target": f"DynamoDB_20120810.{target}",+ "Authorization": "AWS4-HMAC-SHA256 Credential=test/20200101/us-east-1/dynamodb/aws4_request, SignedHeaders=host;x-amz-date;x-amz-target, Signature=00",+ "X-Amz-Date": "20200101T000000Z",+ },+ )+ try:+ with urllib.request.urlopen(req) as r:+ return r.getcode(), json.loads(r.read())+ except urllib.error.HTTPError as e:+ return e.code, json.loads(e.read())+++def test_dynamodb_transact_write_empty_rejected():+ code, body = _raw_ddb("TransactWriteItems", {"TransactItems": []})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+++def test_dynamodb_transact_get_empty_rejected():+ code, body = _raw_ddb("TransactGetItems", {"TransactItems": []})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+++def test_dynamodb_transact_write_duplicate_keys_rejected(ddb):+ name = "tw-dup"+ _basic_table(ddb, name)+ try:+ items = [+ {"Put": {"TableName": name, "Item": {"pk": {"S": "same"}, "a": {"N": "1"}}}},+ {"Put": {"TableName": name, "Item": {"pk": {"S": "same"}, "a": {"N": "2"}}}},+ ]+ with pytest.raises(ClientError) as e:+ ddb.transact_write_items(TransactItems=items)+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# CreateTable validation — AWS-canonical name/key/LSI rules.+# Verified against botocore service-2.json (2012-08-10) and AWS Developer+# Guide "CreateTable" reference (pattern [a-zA-Z0-9_.-]+, length 3-255,+# LSI requires range key on base table, etc.).+# ---------------------------------------------------------------------------++def test_dynamodb_create_table_name_too_short(ddb):+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="xy",+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_create_table_name_too_long(ddb):+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="a" * 256,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_create_table_invalid_chars(ddb):+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="bad table", # space — invalid char+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_create_table_missing_key_schema(ddb):+ # boto3 client-side rejects KeySchema=[] outright. Use direct HTTP so the+ # server gets the chance to apply its own validation (this is what the+ # conformance suite exercises).+ code, body = _raw_ddb("CreateTable", {+ "TableName": "ms-no-ks",+ "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}],+ "BillingMode": "PAY_PER_REQUEST",+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+++def test_dynamodb_create_table_unused_attribute_definition(ddb):+ """Attributes in AttributeDefinitions that don't appear in any KeySchema+ are rejected by real AWS."""+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="unused-attr-defs",+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "extra", "AttributeType": "S"}, # unused+ ],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_create_table_lsi_on_hash_only_rejected(ddb):+ """LSI requires the base table to have a sort key."""+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="lsi-hash-only",+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "alt", "AttributeType": "S"},+ ],+ LocalSecondaryIndexes=[{+ "IndexName": "lsi1",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "alt", "KeyType": "RANGE"},+ ],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_create_table_lsi_different_hash_key_rejected(ddb):+ """LSI's HASH key must match the base table's HASH key."""+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="lsi-bad-hash",+ KeySchema=[+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"},+ ],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "other", "AttributeType": "S"},+ {"AttributeName": "lsisk", "AttributeType": "S"},+ ],+ LocalSecondaryIndexes=[{+ "IndexName": "lsi1",+ "KeySchema": [+ {"AttributeName": "other", "KeyType": "HASH"}, # != base hash+ {"AttributeName": "lsisk", "KeyType": "RANGE"},+ ],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_create_table_class_standard_ia_roundtrip(ddb):+ name = "tc-ia"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ try:+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ TableClass="STANDARD_INFREQUENT_ACCESS",+ )+ d = ddb.describe_table(TableName=name)["Table"]+ assert d.get("TableClassSummary", {}).get("TableClass") == "STANDARD_INFREQUENT_ACCESS"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_create_table_on_demand_throughput_roundtrip(ddb):+ name = "tc-odt"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ try:+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ OnDemandThroughput={"MaxReadRequestUnits": 1000, "MaxWriteRequestUnits": 500},+ )+ d = ddb.describe_table(TableName=name)["Table"]+ odt = d.get("OnDemandThroughput") or {}+ assert odt.get("MaxReadRequestUnits") == 1000+ assert odt.get("MaxWriteRequestUnits") == 500+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_create_table_sse_aws_managed_key(ddb):+ """SSESpecification with Enabled=true and no KMSMasterKeyId picks the+ AWS-managed key (alias/aws/dynamodb)."""+ name = "tc-sse-mgmt"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ try:+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ SSESpecification={"Enabled": True, "SSEType": "KMS"},+ )+ d = ddb.describe_table(TableName=name)["Table"]+ sse = d.get("SSEDescription") or {}+ assert sse.get("Status") == "ENABLED"+ assert sse.get("SSEType") == "KMS"+ assert "alias/aws/dynamodb" in (sse.get("KMSMasterKeyArn") or "")+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# UpdateTable validation — AWS-canonical billing/throughput/index rules.+# ---------------------------------------------------------------------------++def test_dynamodb_update_table_provisioned_noop_rejected(ddb):+ name = "ut-noop"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PROVISIONED",+ ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},+ )+ try:+ with pytest.raises(ClientError) as e:+ ddb.update_table(+ TableName=name,+ ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_table_invalid_throughput_rejected(ddb):+ """Boto3 client-side validates ReadCapacityUnits>=1, so hit the server+ with raw HTTP to exercise its own validator (matches the conformance+ suite's approach)."""+ name = "ut-zero"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PROVISIONED",+ ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},+ )+ try:+ code, body = _raw_ddb("UpdateTable", {+ "TableName": name,+ "ProvisionedThroughput": {"ReadCapacityUnits": 0, "WriteCapacityUnits": 5},+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_table_ppr_with_throughput_rejected(ddb):+ name = "ut-ppr-pt"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.update_table(+ TableName=name,+ ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_table_class_change(ddb):+ name = "ut-tc"+ _basic_table(ddb, name)+ try:+ ddb.update_table(TableName=name, TableClass="STANDARD_INFREQUENT_ACCESS")+ d = ddb.describe_table(TableName=name)["Table"]+ assert d["TableClassSummary"]["TableClass"] == "STANDARD_INFREQUENT_ACCESS"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_table_on_demand_throughput_change(ddb):+ name = "ut-odt"+ _basic_table(ddb, name)+ try:+ ddb.update_table(+ TableName=name,+ OnDemandThroughput={"MaxReadRequestUnits": 2000, "MaxWriteRequestUnits": 1000},+ )+ d = ddb.describe_table(TableName=name)["Table"]+ assert d["OnDemandThroughput"]["MaxReadRequestUnits"] == 2000+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_delete_table_deletion_protection_enforced(ddb):+ name = "ut-dp"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ DeletionProtectionEnabled=True,+ )+ try:+ with pytest.raises(ClientError) as e:+ ddb.delete_table(TableName=name)+ assert e.value.response["Error"]["Code"] == "ValidationException"+ # Disable protection and confirm delete succeeds.+ ddb.update_table(TableName=name, DeletionProtectionEnabled=False)+ ddb.delete_table(TableName=name)+ except Exception:+ try:+ ddb.update_table(TableName=name, DeletionProtectionEnabled=False)+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ raise+++def test_dynamodb_update_table_add_duplicate_gsi_rejected(ddb):+ name = "ut-gsi-dup"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "gsi_pk", "AttributeType": "S"},+ ],+ GlobalSecondaryIndexes=[{+ "IndexName": "gsi1",+ "KeySchema": [{"AttributeName": "gsi_pk", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+ try:+ with pytest.raises(ClientError) as e:+ ddb.update_table(+ TableName=name,+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "gsi_pk", "AttributeType": "S"},+ ],+ GlobalSecondaryIndexUpdates=[{+ "Create": {+ "IndexName": "gsi1", # already exists+ "KeySchema": [{"AttributeName": "gsi_pk", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ }+ }],+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_table_delete_nonexistent_gsi_rejected(ddb):+ name = "ut-gsi-del"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.update_table(+ TableName=name,+ GlobalSecondaryIndexUpdates=[{"Delete": {"IndexName": "nope"}}],+ )+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_table_add_gsi_undefined_attribute_rejected(ddb):+ name = "ut-gsi-undef"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.update_table(+ TableName=name,+ GlobalSecondaryIndexUpdates=[{+ "Create": {+ "IndexName": "gsi-x",+ "KeySchema": [{"AttributeName": "undefined_attr", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ }+ }],+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# GetItem ProjectionExpression — nested map paths and list indexes per AWS.+# ---------------------------------------------------------------------------++def _proj_table(ddb, name):+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],+ BillingMode="PAY_PER_REQUEST",+ )+++def test_dynamodb_projection_nested_map_path(ddb):+ name = "pe-map"+ _proj_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "a"},+ "data": {"M": {+ "name": {"S": "alice"},+ "age": {"N": "30"},+ "email": {"S": "[email protected]"},+ }},+ })+ r = ddb.get_item(+ TableName=name, Key={"pk": {"S": "a"}},+ ProjectionExpression="#d.#n",+ ExpressionAttributeNames={"#d": "data", "#n": "name"},+ )+ assert r["Item"] == {"data": {"M": {"name": {"S": "alice"}}}}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_projection_list_index(ddb):+ name = "pe-list"+ _proj_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "a"},+ "items": {"L": [{"S": "first"}, {"S": "second"}, {"S": "third"}]},+ })+ r = ddb.get_item(+ TableName=name, Key={"pk": {"S": "a"}},+ ProjectionExpression="#i[1]",+ ExpressionAttributeNames={"#i": "items"},+ )+ assert r["Item"] == {"items": {"L": [{"S": "second"}]}}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_projection_nested_path_and_list_index(ddb):+ name = "pe-mix"+ _proj_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "a"},+ "rec": {"M": {+ "tags": {"L": [{"S": "red"}, {"S": "blue"}]},+ "name": {"S": "x"},+ }},+ })+ r = ddb.get_item(TableName=name, Key={"pk": {"S": "a"}}, ProjectionExpression="rec.tags[0]")+ assert r["Item"] == {"rec": {"M": {"tags": {"L": [{"S": "red"}]}}}}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_projection_deep_nested(ddb):+ name = "pe-deep"+ _proj_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "a"},+ "level1": {"M": {+ "level2": {"M": {+ "leaf": {"S": "deep"},+ "other": {"S": "skip"},+ }},+ "sibling": {"S": "skip"},+ }},+ })+ r = ddb.get_item(TableName=name, Key={"pk": {"S": "a"}}, ProjectionExpression="level1.level2.leaf")+ assert r["Item"] == {"level1": {"M": {"level2": {"M": {"leaf": {"S": "deep"}}}}}}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_projection_multiple_sibling_paths(ddb):+ """Two paths under the same root map merge in the projected result."""+ name = "pe-sib"+ _proj_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "a"},+ "p": {"M": {+ "x": {"S": "X"},+ "y": {"S": "Y"},+ "z": {"S": "Z"},+ }},+ })+ r = ddb.get_item(+ TableName=name,+ Key={"pk": {"S": "a"}},+ ProjectionExpression="p.x, p.y",+ )+ got = r["Item"]+ assert "p" in got+ assert got["p"]["M"].get("x") == {"S": "X"}+ assert got["p"]["M"].get("y") == {"S": "Y"}+ assert "z" not in got["p"]["M"]+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# ReturnValues + ReturnItemCollectionMetrics enum + shape validation.+# ---------------------------------------------------------------------------++def test_dynamodb_put_item_invalid_return_values_rejected(ddb):+ name = "pi-rv"+ _basic_table(ddb, name)+ try:+ # ALL_NEW is invalid for PutItem (only NONE | ALL_OLD).+ code, body = _raw_ddb("PutItem", {+ "TableName": name,+ "Item": {"pk": {"S": "x"}},+ "ReturnValues": "ALL_NEW",+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_put_item_collection_metrics_size(ddb):+ """ReturnItemCollectionMetrics=SIZE returns ItemCollectionMetrics when the+ table has at least one LSI."""+ name = "pi-icm"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"},+ ],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "lsk", "AttributeType": "S"},+ ],+ LocalSecondaryIndexes=[{+ "IndexName": "byLsk",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "lsk", "KeyType": "RANGE"},+ ],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+ try:+ r = ddb.put_item(+ TableName=name,+ Item={"pk": {"S": "k"}, "sk": {"S": "a"}, "lsk": {"S": "l"}},+ ReturnItemCollectionMetrics="SIZE",+ )+ icm = r.get("ItemCollectionMetrics") or {}+ assert icm.get("ItemCollectionKey", {}).get("pk") == {"S": "k"}+ assert "SizeEstimateRangeGB" in icm+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_delete_item_collection_metrics_size(ddb):+ name = "di-icm"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"},+ ],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "lsk", "AttributeType": "S"},+ ],+ LocalSecondaryIndexes=[{+ "IndexName": "byLsk",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "lsk", "KeyType": "RANGE"},+ ],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "sk": {"S": "a"}, "lsk": {"S": "l"}})+ r = ddb.delete_item(+ TableName=name,+ Key={"pk": {"S": "k"}, "sk": {"S": "a"}},+ ReturnItemCollectionMetrics="SIZE",+ )+ icm = r.get("ItemCollectionMetrics") or {}+ assert icm.get("ItemCollectionKey", {}).get("pk") == {"S": "k"}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_item_collection_metrics_size(ddb):+ name = "ui-icm"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "sk", "KeyType": "RANGE"},+ ],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "sk", "AttributeType": "S"},+ {"AttributeName": "lsk", "AttributeType": "S"},+ ],+ LocalSecondaryIndexes=[{+ "IndexName": "byLsk",+ "KeySchema": [+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "lsk", "KeyType": "RANGE"},+ ],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "sk": {"S": "a"}, "lsk": {"S": "l"}})+ r = ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}, "sk": {"S": "a"}},+ UpdateExpression="SET v = :v",+ ExpressionAttributeValues={":v": {"S": "value"}},+ ReturnItemCollectionMetrics="SIZE",+ )+ icm = r.get("ItemCollectionMetrics") or {}+ assert icm.get("ItemCollectionKey", {}).get("pk") == {"S": "k"}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_remove_updated_new_omits_attributes(ddb):+ """UpdateItem with REMOVE-only and ReturnValues=UPDATED_NEW must NOT+ return Attributes (no new values to report)."""+ name = "ur-un"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "x": {"S": "X"}})+ r = ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="REMOVE x",+ ReturnValues="UPDATED_NEW",+ )+ assert "Attributes" not in r+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# UpdateItem semantics — pre-update snapshot, key protection, missing path.+# ---------------------------------------------------------------------------++def test_dynamodb_update_set_reads_pre_update_value(ddb):+ """SET reads RHS against the pre-update snapshot — `SET a = b, b = :v`+ must set `a` to the OLD value of `b`."""+ name = "u-snap"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "b": {"S": "OLD"}})+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET a = b, b = :v",+ ExpressionAttributeValues={":v": {"S": "NEW"}},+ )+ got = ddb.get_item(TableName=name, Key={"pk": {"S": "k"}})["Item"]+ assert got["a"] == {"S": "OLD"}+ assert got["b"] == {"S": "NEW"}+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_set_missing_intermediate_path_rejected(ddb):+ """`SET a.b.c = :v` where `a.b` doesn't exist must be rejected."""+ name = "u-bad-path"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET a.b.c = :v",+ ExpressionAttributeValues={":v": {"S": "x"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_cannot_modify_hash_key(ddb):+ name = "u-pk"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET pk = :v",+ ExpressionAttributeValues={":v": {"S": "newkey"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_cannot_modify_range_key(ddb):+ name = "u-sk"+ _basic_table(ddb, name, with_sk=True)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "sk": {"S": "a"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}, "sk": {"S": "a"}},+ UpdateExpression="SET sk = :v",+ ExpressionAttributeValues={":v": {"S": "b"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_nested_set_updated_new(ddb):+ """SET on a nested map path with ReturnValues=UPDATED_NEW returns the+ affected attribute's projected new value."""+ name = "u-nest-un"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "k"},+ "m": {"M": {"a": {"S": "A"}, "b": {"S": "B"}}},+ })+ r = ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET m.a = :v",+ ExpressionAttributeValues={":v": {"S": "A2"}},+ ReturnValues="UPDATED_NEW",+ )+ # The attribute `m` is the one that changed; AWS returns it in+ # full as the projected new-value tree.+ assert "m" in r.get("Attributes", {})+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Query / Scan validation — ConsistentRead-on-GSI, Select rules,+# Segment/TotalSegments, Limit, binary sort ordering, size() UTF-16.+# ---------------------------------------------------------------------------++def _gsi_table(ddb, name):+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "gsi_pk", "AttributeType": "S"},+ ],+ GlobalSecondaryIndexes=[{+ "IndexName": "byGsiPk",+ "KeySchema": [{"AttributeName": "gsi_pk", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ }],+ BillingMode="PAY_PER_REQUEST",+ )+++def test_dynamodb_query_consistent_read_on_gsi_rejected(ddb):+ name = "q-cr-gsi"+ _gsi_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.query(+ TableName=name,+ IndexName="byGsiPk",+ KeyConditionExpression="gsi_pk = :v",+ ExpressionAttributeValues={":v": {"S": "x"}},+ ConsistentRead=True,+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_limit_zero_rejected(ddb):+ name = "q-lim-zero"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Query", {+ "TableName": name,+ "KeyConditionExpression": "pk = :v",+ "ExpressionAttributeValues": {":v": {"S": "k"}},+ "Limit": 0,+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_scan_segment_without_total_rejected(ddb):+ name = "s-seg-only"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Scan", {"TableName": name, "Segment": 0})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_scan_total_without_segment_rejected(ddb):+ name = "s-tot-only"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Scan", {"TableName": name, "TotalSegments": 4})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_scan_segment_out_of_range_rejected(ddb):+ name = "s-seg-oob"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Scan", {"TableName": name, "Segment": 4, "TotalSegments": 4})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_scan_limit_zero_rejected(ddb):+ name = "s-lim-zero"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Scan", {"TableName": name, "Limit": 0})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_binary_sort_key_ordering(ddb):+ """Binary sort keys must order bytewise (not lexicographic on base64)."""+ import base64+ name = "q-bsk"+ try:+ ddb.delete_table(TableName=name)+ except Exception:+ pass+ ddb.create_table(+ TableName=name,+ KeySchema=[+ {"AttributeName": "pk", "KeyType": "HASH"},+ {"AttributeName": "bsk", "KeyType": "RANGE"},+ ],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "bsk", "AttributeType": "B"},+ ],+ BillingMode="PAY_PER_REQUEST",+ )+ try:+ for raw in (b"\x01", b"\xff", b"\x10"):+ ddb.put_item(TableName=name, Item={+ "pk": {"S": "k"},+ "bsk": {"B": raw},+ })+ r = ddb.query(+ TableName=name,+ KeyConditionExpression="pk = :p",+ ExpressionAttributeValues={":p": {"S": "k"}},+ )+ items = r["Items"]+ bs_bytes = []+ for it in items:+ v = it["bsk"]+ if isinstance(v, dict):+ v = v.get("B")+ if not isinstance(v, (bytes, bytearray)):+ v = base64.b64decode(v)+ bs_bytes.append(bytes(v))+ assert bs_bytes == sorted(bs_bytes)+ assert bs_bytes[0] == b"\x01"+ assert bs_bytes[-1] == b"\xff"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_select_all_projected_attrs_without_index_rejected(ddb):+ name = "q-apa"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.query(+ TableName=name,+ KeyConditionExpression="pk = :v",+ ExpressionAttributeValues={":v": {"S": "x"}},+ Select="ALL_PROJECTED_ATTRIBUTES",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_select_specific_without_projection_rejected(ddb):+ name = "q-spec"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.query(+ TableName=name,+ KeyConditionExpression="pk = :v",+ ExpressionAttributeValues={":v": {"S": "x"}},+ Select="SPECIFIC_ATTRIBUTES",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_size_utf16_units(ddb):+ """AWS size(s) returns UTF-16 code-unit count, NOT Python char count."""+ name = "u16"+ _basic_table(ddb, name)+ try:+ # "😀" is one Python char but two UTF-16 units (surrogate pair).+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "s": {"S": "😀"}})+ # FilterExpression `size(s) = :two` should match.+ r = ddb.scan(+ TableName=name,+ FilterExpression="size(s) = :two",+ ExpressionAttributeValues={":two": {"N": "2"}},+ )+ assert r["Count"] == 1+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Transactions — ConsumedCapacity (2x per item) and ClientRequestToken+# idempotency per AWS Developer Guide.+# ---------------------------------------------------------------------------++def test_dynamodb_transact_write_consumed_capacity(ddb):+ name = "tw-cc"+ _basic_table(ddb, name)+ try:+ r = ddb.transact_write_items(+ TransactItems=[+ {"Put": {"TableName": name, "Item": {"pk": {"S": "a"}}}},+ {"Put": {"TableName": name, "Item": {"pk": {"S": "b"}}}},+ ],+ ReturnConsumedCapacity="TOTAL",+ )+ cc = r.get("ConsumedCapacity")+ assert cc and cc[0]["TableName"] == name+ # 2 items × 2 units = 4.+ assert cc[0]["CapacityUnits"] == 4.0+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_transact_get_consumed_capacity(ddb):+ name = "tg-cc"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}})+ ddb.put_item(TableName=name, Item={"pk": {"S": "b"}})+ r = ddb.transact_get_items(+ TransactItems=[+ {"Get": {"TableName": name, "Key": {"pk": {"S": "a"}}}},+ {"Get": {"TableName": name, "Key": {"pk": {"S": "b"}}}},+ ],+ ReturnConsumedCapacity="TOTAL",+ )+ cc = r.get("ConsumedCapacity")+ assert cc and cc[0]["TableName"] == name+ assert cc[0]["CapacityUnits"] == 4.0+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_transact_write_idempotency_token_mismatch(ddb):+ name = "tw-crt"+ _basic_table(ddb, name)+ try:+ token = "idem-1"+ # First call lands.+ ddb.transact_write_items(+ ClientRequestToken=token,+ TransactItems=[{"Put": {"TableName": name, "Item": {"pk": {"S": "a"}}}}],+ )+ # Second call with same token but DIFFERENT payload must fail.+ with pytest.raises(ClientError) as e:+ ddb.transact_write_items(+ ClientRequestToken=token,+ TransactItems=[{"Put": {"TableName": name, "Item": {"pk": {"S": "DIFFERENT"}}}}],+ )+ assert e.value.response["Error"]["Code"] == "IdempotentParameterMismatchException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_transact_write_idempotency_token_same_payload(ddb):+ name = "tw-crt-same"+ _basic_table(ddb, name)+ try:+ token = "idem-same"+ items = [{"Put": {"TableName": name, "Item": {"pk": {"S": "a"}}}}]+ ddb.transact_write_items(ClientRequestToken=token, TransactItems=items)+ # Same payload is a no-op replay — should succeed.+ ddb.transact_write_items(ClientRequestToken=token, TransactItems=items)+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Legacy parameters — mutual-exclusion + projection semantics per AWS.+# ---------------------------------------------------------------------------++def test_dynamodb_get_item_atg_and_pe_mutually_exclusive(ddb):+ name = "atg-pe"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "a": {"S": "A"}})+ with pytest.raises(ClientError) as e:+ ddb.get_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ ProjectionExpression="a",+ AttributesToGet=["a"],+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_get_item_attributes_to_get_only(ddb):+ """AttributesToGet returns exactly the requested attributes — does NOT+ auto-include the hash/sort key."""+ name = "atg-keys"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "a": {"S": "A"}, "b": {"S": "B"}})+ r = ddb.get_item(TableName=name, Key={"pk": {"S": "k"}}, AttributesToGet=["a"])+ assert r["Item"] == {"a": {"S": "A"}}+ assert "pk" not in r["Item"]+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_scan_filter_and_filter_expression_mutually_exclusive(ddb):+ name = "sf-fe"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.scan(+ TableName=name,+ FilterExpression="a = :v",+ ExpressionAttributeValues={":v": {"S": "x"}},+ ScanFilter={"a": {"AttributeValueList": [{"S": "x"}], "ComparisonOperator": "EQ"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_attribute_updates_cannot_modify_key(ddb):+ name = "au-key"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ AttributeUpdates={"pk": {"Value": {"S": "newkey"}, "Action": "PUT"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Tags / TTL validation.+# ---------------------------------------------------------------------------++def test_dynamodb_tag_resource_invalid_arn_rejected(ddb):+ with pytest.raises(ClientError) as e:+ ddb.tag_resource(ResourceArn="not-an-arn", Tags=[{"Key": "k", "Value": "v"}])+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_list_tags_unknown_arn_rejected(ddb):+ fake = "arn:aws:dynamodb:us-east-1:000000000000:table/does-not-exist-tag-XX"+ with pytest.raises(ClientError) as e:+ ddb.list_tags_of_resource(ResourceArn=fake)+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+++def test_dynamodb_ttl_empty_attribute_name_rejected(ddb):+ name = "ttl-empty"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("UpdateTimeToLive", {+ "TableName": name,+ "TimeToLiveSpecification": {"Enabled": True, "AttributeName": ""},+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# PartiQL — BatchExecuteStatement and ExecuteTransaction.+# Verified against botocore service-2.json (2012-08-10).+# ---------------------------------------------------------------------------++def test_dynamodb_batch_execute_statement_multi_select(ddb):+ name = "bxs-sel"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}, "v": {"N": "1"}})+ ddb.put_item(TableName=name, Item={"pk": {"S": "b"}, "v": {"N": "2"}})+ r = ddb.batch_execute_statement(Statements=[+ {"Statement": f'SELECT * FROM "{name}" WHERE pk = ?', "Parameters": [{"S": "a"}]},+ {"Statement": f'SELECT * FROM "{name}" WHERE pk = ?', "Parameters": [{"S": "b"}]},+ ])+ responses = r["Responses"]+ assert len(responses) == 2+ # Each entry should carry an Item with the matching pk.+ pks = sorted([resp.get("Item", {}).get("pk", {}).get("S") for resp in responses])+ assert pks == ["a", "b"]+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_batch_execute_statement_partial_failure(ddb):+ name = "bxs-pf"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}})+ r = ddb.batch_execute_statement(Statements=[+ {"Statement": f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}', "Parameters": [{"S": "a"}]}, # dup+ {"Statement": f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}', "Parameters": [{"S": "b"}]}, # OK+ ])+ # The whole batch returns 200 with per-statement results.+ assert len(r["Responses"]) == 2+ assert r["Responses"][0].get("Error", {}).get("Code") == "DuplicateItemException"+ assert "Error" not in r["Responses"][1]+ # Confirm "b" landed.+ got = ddb.get_item(TableName=name, Key={"pk": {"S": "b"}})+ assert "Item" in got+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_batch_execute_statement_empty_rejected():+ code, body = _raw_ddb("BatchExecuteStatement", {"Statements": []})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+++def test_dynamodb_execute_transaction_multi_insert(ddb):+ name = "et-ins"+ _basic_table(ddb, name)+ try:+ r = ddb.execute_transaction(TransactStatements=[+ {"Statement": f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}', "Parameters": [{"S": "a"}]},+ {"Statement": f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}', "Parameters": [{"S": "b"}]},+ ])+ assert len(r["Responses"]) == 2+ for pk in ("a", "b"):+ got = ddb.get_item(TableName=name, Key={"pk": {"S": pk}})+ assert "Item" in got+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_execute_transaction_rollback_on_duplicate(ddb):+ """If one INSERT in a transaction fails (dup key), the whole transaction+ rolls back — no statements take effect."""+ name = "et-rb"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "existing"}})+ with pytest.raises(ClientError) as e:+ ddb.execute_transaction(TransactStatements=[+ {"Statement": f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}', "Parameters": [{"S": "new"}]},+ {"Statement": f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}', "Parameters": [{"S": "existing"}]}, # dup+ ])+ assert e.value.response["Error"]["Code"] == "TransactionCanceledException"+ # "new" must NOT have landed (rollback).+ got = ddb.get_item(TableName=name, Key={"pk": {"S": "new"}})+ assert "Item" not in got+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_execute_transaction_empty_rejected():+ code, body = _raw_ddb("ExecuteTransaction", {"TransactStatements": []})+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+++def test_dynamodb_execute_statement_insert_existing_dup_exception(ddb):+ """ExecuteStatement INSERT on existing item returns DuplicateItemException+ (not ConditionalCheckFailedException)."""+ name = "es-dup"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}})+ with pytest.raises(ClientError) as e:+ ddb.execute_statement(+ Statement=f'INSERT INTO "{name}" VALUE {{\'pk\': ?}}',+ Parameters=[{"S": "a"}],+ )+ assert e.value.response["Error"]["Code"] == "DuplicateItemException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_execute_statement_consumed_capacity(ddb):+ name = "es-cc"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}})+ r = ddb.execute_statement(+ Statement=f'SELECT * FROM "{name}" WHERE pk = ?',+ Parameters=[{"S": "a"}],+ ReturnConsumedCapacity="TOTAL",+ )+ cc = r.get("ConsumedCapacity") or {}+ assert cc.get("TableName") == name+ assert cc.get("CapacityUnits", 0) > 0+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Error-message canonicalization — AWS-canonical strings + new validators+# (EAV/EAN-without-expression, non-existent table standardization).+# ---------------------------------------------------------------------------++def test_dynamodb_expression_attribute_values_without_expression_rejected(ddb):+ name = "eav-no-expr"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.put_item(+ TableName=name,+ Item={"pk": {"S": "k"}},+ ExpressionAttributeValues={":v": {"S": "x"}},+ # NO ConditionExpression — should reject.+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_expression_attribute_names_without_expression_rejected(ddb):+ name = "ean-no-expr"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.get_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ ExpressionAttributeNames={"#a": "a"},+ # NO ProjectionExpression — should reject.+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_put_item_nonexistent_table_canonical(ddb):+ with pytest.raises(ClientError) as e:+ ddb.put_item(TableName="does-not-exist-pi-xyz", Item={"pk": {"S": "k"}})+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+ assert "non-existent table" in e.value.response["Error"]["Message"].lower()+++def test_dynamodb_get_item_nonexistent_table_canonical(ddb):+ with pytest.raises(ClientError) as e:+ ddb.get_item(TableName="does-not-exist-gi-xyz", Key={"pk": {"S": "k"}})+ assert e.value.response["Error"]["Code"] == "ResourceNotFoundException"+++def test_dynamodb_query_invalid_return_consumed_capacity_rejected(ddb):+ name = "q-rcc-bad"+ _basic_table(ddb, name)+ try:+ # Boto3 client-side rejects non-enum values, so use raw HTTP.+ code, body = _raw_ddb("Query", {+ "TableName": name,+ "KeyConditionExpression": "pk = :v",+ "ExpressionAttributeValues": {":v": {"S": "k"}},+ "ReturnConsumedCapacity": "BOGUS",+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Redundant parentheses — AWS rejects `((expr))` patterns in expressions.+# ---------------------------------------------------------------------------++def test_dynamodb_delete_item_redundant_parens_rejected(ddb):+ name = "rp-di"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "x": {"N": "1"}})+ with pytest.raises(ClientError) as e:+ ddb.delete_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ ConditionExpression="((x = :v))",+ ExpressionAttributeValues={":v": {"N": "1"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_put_item_redundant_parens_rejected(ddb):+ name = "rp-pi"+ _basic_table(ddb, name)+ try:+ with pytest.raises(ClientError) as e:+ ddb.put_item(+ TableName=name,+ Item={"pk": {"S": "k"}},+ ConditionExpression="((attribute_not_exists(pk)))",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_scan_redundant_parens_rejected(ddb):+ name = "rp-sc"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "x": {"N": "1"}})+ with pytest.raises(ClientError) as e:+ ddb.scan(+ TableName=name,+ FilterExpression="((x = :v))",+ ExpressionAttributeValues={":v": {"N": "1"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_legitimate_parens_still_work(ddb):+ """`(a) OR (b)` is NOT redundant — those parens group operands."""+ name = "rp-ok"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "x": {"N": "1"}, "y": {"N": "2"}})+ r = ddb.scan(+ TableName=name,+ FilterExpression="(x = :v1) OR (y = :v2)",+ ExpressionAttributeValues={":v1": {"N": "9"}, ":v2": {"N": "2"}},+ )+ assert r["Count"] == 1+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Backups (CreateBackup, DescribeBackup, DeleteBackup, ListBackups,+# RestoreTableFromBackup, RestoreTableToPointInTime) and DescribeLimits.+# ---------------------------------------------------------------------------++def test_dynamodb_create_describe_list_delete_backup(ddb):+ name = "bk-table"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "a"}, "v": {"N": "1"}})+ r = ddb.create_backup(TableName=name, BackupName="bk1")+ details = r["BackupDetails"]+ arn = details["BackupArn"]+ assert details["BackupName"] == "bk1"+ assert details["BackupStatus"] == "AVAILABLE"++ d = ddb.describe_backup(BackupArn=arn)+ assert d["BackupDescription"]["BackupDetails"]["BackupArn"] == arn++ lst = ddb.list_backups(TableName=name)+ arns = [s["BackupArn"] for s in lst["BackupSummaries"]]+ assert arn in arns++ deleted = ddb.delete_backup(BackupArn=arn)+ assert deleted["BackupDescription"]["BackupDetails"]["BackupArn"] == arn++ with pytest.raises(ClientError) as e:+ ddb.describe_backup(BackupArn=arn)+ assert e.value.response["Error"]["Code"] == "BackupNotFoundException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_restore_table_from_backup(ddb):+ src = "bk-src"+ target = "bk-restored"+ _basic_table(ddb, src)+ try:+ ddb.put_item(TableName=src, Item={"pk": {"S": "a"}, "v": {"N": "42"}})+ arn = ddb.create_backup(TableName=src, BackupName="snap1")["BackupDetails"]["BackupArn"]+ try:+ ddb.delete_table(TableName=target)+ except Exception:+ pass+ r = ddb.restore_table_from_backup(TargetTableName=target, BackupArn=arn)+ assert r["TableDescription"]["TableName"] == target+ # Items should be restored.+ got = ddb.get_item(TableName=target, Key={"pk": {"S": "a"}})+ assert got["Item"]["v"]["N"] == "42"+ finally:+ try:+ ddb.delete_backup(BackupArn=arn)+ except Exception:+ pass+ for t in (src, target):+ try:+ ddb.delete_table(TableName=t)+ except Exception:+ pass+++def test_dynamodb_restore_table_target_already_exists_rejected(ddb):+ src = "rt-src"+ target = "rt-target"+ _basic_table(ddb, src)+ _basic_table(ddb, target)+ try:+ arn = ddb.create_backup(TableName=src, BackupName="snap")["BackupDetails"]["BackupArn"]+ with pytest.raises(ClientError) as e:+ ddb.restore_table_from_backup(TargetTableName=target, BackupArn=arn)+ assert e.value.response["Error"]["Code"] == "TableAlreadyExistsException"+ ddb.delete_backup(BackupArn=arn)+ finally:+ for t in (src, target):+ try:+ ddb.delete_table(TableName=t)+ except Exception:+ pass+++def test_dynamodb_restore_table_to_point_in_time(ddb):+ src = "pitr-src"+ target = "pitr-target"+ _basic_table(ddb, src)+ try:+ ddb.put_item(TableName=src, Item={"pk": {"S": "x"}, "v": {"S": "live"}})+ try:+ ddb.delete_table(TableName=target)+ except Exception:+ pass+ r = ddb.restore_table_to_point_in_time(SourceTableName=src, TargetTableName=target)+ assert r["TableDescription"]["TableName"] == target+ got = ddb.get_item(TableName=target, Key={"pk": {"S": "x"}})+ assert got["Item"]["v"]["S"] == "live"+ finally:+ for t in (src, target):+ try:+ ddb.delete_table(TableName=t)+ except Exception:+ pass+++def test_dynamodb_describe_backup_not_found(ddb):+ with pytest.raises(ClientError) as e:+ ddb.describe_backup(BackupArn="arn:aws:dynamodb:us-east-1:000000000000:table/x/backup/nope")+ assert e.value.response["Error"]["Code"] == "BackupNotFoundException"+++def test_dynamodb_create_backup_unknown_table(ddb):+ with pytest.raises(ClientError) as e:+ ddb.create_backup(TableName="does-not-exist-bk", BackupName="bk-snap")+ assert e.value.response["Error"]["Code"] == "TableNotFoundException"+++def test_dynamodb_describe_limits(ddb):+ r = ddb.describe_limits()+ assert r["AccountMaxReadCapacityUnits"] > 0+ assert r["AccountMaxWriteCapacityUnits"] > 0+ assert r["TableMaxReadCapacityUnits"] > 0+ assert r["TableMaxWriteCapacityUnits"] > 0+++# ---------------------------------------------------------------------------+# AWS-keyword detection in expressions — every name must be #-aliased if it+# matches a system keyword. Source: AWS DynamoDB Developer Guide.+# ---------------------------------------------------------------------------++def test_dynamodb_keyword_in_condition_expression_rejected(ddb):+ name = "kw-ce"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "status": {"S": "x"}})+ with pytest.raises(ClientError) as e:+ ddb.put_item(+ TableName=name,+ Item={"pk": {"S": "k"}, "status": {"S": "y"}},+ ConditionExpression="status = :v", # STATUS is a system keyword+ ExpressionAttributeValues={":v": {"S": "x"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_keyword_in_update_expression_rejected(ddb):+ name = "kw-ue"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET name = :v", # NAME is a system keyword+ ExpressionAttributeValues={":v": {"S": "x"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_keyword_in_projection_expression_rejected(ddb):+ name = "kw-pe"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "name": {"S": "n"}})+ with pytest.raises(ClientError) as e:+ ddb.get_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ ProjectionExpression="name",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_keyword_in_filter_expression_rejected(ddb):+ name = "kw-fe"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "status": {"S": "x"}})+ with pytest.raises(ClientError) as e:+ ddb.scan(+ TableName=name,+ FilterExpression="status = :v",+ ExpressionAttributeValues={":v": {"S": "x"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_keyword_with_alias_accepted(ddb):+ name = "kw-alias"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "status": {"S": "live"}})+ r = ddb.get_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ ProjectionExpression="#s",+ ExpressionAttributeNames={"#s": "status"},+ )+ assert r["Item"] == {"status": {"S": "live"}}+ finally:+ ddb.delete_table(TableName=name)+++# ---------------------------------------------------------------------------+# Exact-error-message coverage — conformance Tier 3 error-messages bucket.+# ---------------------------------------------------------------------------++def test_dynamodb_create_table_duplicate_index_names_rejected(ddb):+ with pytest.raises(ClientError) as e:+ ddb.create_table(+ TableName="dup-idx",+ KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],+ AttributeDefinitions=[+ {"AttributeName": "pk", "AttributeType": "S"},+ {"AttributeName": "gpk", "AttributeType": "S"},+ ],+ GlobalSecondaryIndexes=[+ {+ "IndexName": "same-idx",+ "KeySchema": [{"AttributeName": "gpk", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ },+ {+ "IndexName": "same-idx", # duplicate name+ "KeySchema": [{"AttributeName": "gpk", "KeyType": "HASH"}],+ "Projection": {"ProjectionType": "ALL"},+ },+ ],+ BillingMode="PAY_PER_REQUEST",+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+++def test_dynamodb_query_empty_key_condition_rejected(ddb):+ name = "q-empty-kce"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Query", {+ "TableName": name,+ "KeyConditionExpression": "",+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_invalid_select_value_rejected(ddb):+ name = "q-bad-sel"+ _basic_table(ddb, name)+ try:+ code, body = _raw_ddb("Query", {+ "TableName": name,+ "KeyConditionExpression": "pk = :v",+ "ExpressionAttributeValues": {":v": {"S": "x"}},+ "Select": "BOGUS",+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_item_empty_update_expression_rejected(ddb):+ name = "u-empty-ue"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ code, body = _raw_ddb("UpdateItem", {+ "TableName": name,+ "Key": {"pk": {"S": "k"}},+ "UpdateExpression": "",+ })+ assert code == 400+ assert "ValidationException" in body.get("__type", "")+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_unused_expression_attribute_name_rejected(ddb):+ name = "u-unused-ean"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET a = :v",+ ExpressionAttributeNames={"#unused": "ghost"}, # defined but never used+ ExpressionAttributeValues={":v": {"S": "x"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ assert "unused" in e.value.response["Error"]["Message"].lower()+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_unused_expression_attribute_value_rejected(ddb):+ name = "u-unused-eav"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET a = :v",+ ExpressionAttributeValues={":v": {"S": "x"}, ":unused": {"S": "ghost"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ assert "unused" in e.value.response["Error"]["Message"].lower()+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_update_missing_value_reference_rejected(ddb):+ name = "u-missing-ref"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}})+ with pytest.raises(ClientError) as e:+ ddb.update_item(+ TableName=name,+ Key={"pk": {"S": "k"}},+ UpdateExpression="SET a = :v",+ # NOT defining :v in EAV+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_contains_with_distinct_operands_required(ddb):+ name = "c-distinct"+ _basic_table(ddb, name)+ try:+ ddb.put_item(TableName=name, Item={"pk": {"S": "k"}, "a": {"S": "abc"}})+ with pytest.raises(ClientError) as e:+ ddb.scan(+ TableName=name,+ FilterExpression="contains(a, a)", # same path and operand+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_non_key_attribute_in_kce_rejected(ddb):+ name = "q-nonkey-kce"+ _basic_table(ddb, name, with_sk=True)+ try:+ with pytest.raises(ClientError) as e:+ ddb.query(+ TableName=name,+ # Try filtering on a non-key attribute in KeyConditionExpression.+ KeyConditionExpression="pk = :p AND other = :o",+ ExpressionAttributeValues={":p": {"S": "k"}, ":o": {"S": "x"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ assert "key schema" in e.value.response["Error"]["Message"].lower()+ finally:+ ddb.delete_table(TableName=name)+++def test_dynamodb_query_missing_hash_key_in_kce_rejected(ddb):+ name = "q-missing-pk"+ _basic_table(ddb, name, with_sk=True)+ try:+ with pytest.raises(ClientError) as e:+ ddb.query(+ TableName=name,+ # No pk condition.+ KeyConditionExpression="sk = :s",+ ExpressionAttributeValues={":s": {"S": "a"}},+ )+ assert e.value.response["Error"]["Code"] == "ValidationException"+ finally:+ ddb.delete_table(TableName=name)