Skip to content

fix(core): refuse a second condition on a key in KeyConditionExpression - #484

Open
LeeroyHannigan wants to merge 34 commits into
mainfrom
fix/key-condition-one-per-key
Open

LeeroyHannigan wants to merge 34 commits into
mainfrom
fix/key-condition-one-per-key

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

  • 1c6a6c6 merges main, 24 commits, with no conflict.
  • 8b43195 merges main again, 13 commits, with no conflict. fix(query): refuse a key condition with no HASH equality the way the service does #479 builds on this branch and merged the same main commit.
  • bf57850 merges main a third time, 10 commits. crates/engine/src/query.rs and docs/manuals/07-upgrade-manual.md conflicted.
  • 468b256 refuses an invalid Select before the IAM decision, where fix(query): check Query values against the key schema and the number rules #544 put that check on main.
    • Before the IAM decision, an invalid Select makes the server run check_request, as a duplicate does. Every principal gets the Select text, or a fault that check_request reports ahead of it (H3, B4). Item 14 under What gives the answer that changes.
    • an_invalid_select_is_refused_before_authorization in crates/engine/src/query/request_checks.rs sends pk = :own and pk > :own with Select BOGUS, and the shape of H3.
    • It also removes three comment lines in check_request that repeated the doc of validate_query_request_body.
  • 534fb77 removes the one-condition-per-key check of the classifier (resolve_multipart_with). No request reached it. The engine refuses a duplicate in parse_key_condition_with_maps before it classifies any clause, and authorization refuses one before the IAM decision. Its 13 multi-attribute rows now run through parse_key_condition_with_maps.
  • 5d4ae9b keeps <> and contains clauses out of RawClause. So raw_to_sk is main's infallible function again, and two match arms that no input reached are gone.
  • 19bb1dd answers a fault in the key condition's shape after the table, index, ConsistentRead and Select checks, as main does. The service answers it after the table (R10, R11), the index (S7), ConsistentRead (S6) and an invalid Select (Q1), and before the Select index check (S1). What stays different from DynamoDB lists that order. The reviewed head answered the shape fault ahead of all of these checks.
  • 76e33b2 makes the key condition and filter begins_with operand checks call one helper, check_begins_with_operand.
  • 06d7d20 runs the second filter operand check only for a desugared QueryFilter. Beside KeyConditions it repeated the first check.
  • a3d77ee fixes the doc comment of the map-less key condition read in request_attributes.rs.
  • b83c3b8 drops the :u = {"N": "bob"} requests of test_unreadable_partition_equality_does_not_bind_another_clause, which checked only the error code, and renames the test test_partition_equality_does_not_bind_another_clause. It also drops two issue numbers from test comments.
  • 98d09b3 adds a v1.1.0 entry to the upgrade manual. It lists the requests this change refuses. It also says that the 0.1.15 LeadingKeys entry does not hold for a key condition with two conditions on one key attribute.
  • This description now gives the merge order with fix(query): refuse a key condition with no HASH equality the way the service does #479, under Overlap with fix(query): refuse a key condition with no HASH equality the way the service does #479. It moves R8 and R9 to the measured rows and drops F1d from item 6. It listed R3 and R4 under What stays different from DynamoDB until bf57850.
  • At 8b43195, against main at a417b2f, the PR's production lines came to 371 net. The reviewed head had 377 net against its merge base. At 468b256 they come to 404 net against main at 5252eda. query_key_values.rs adds 29 of them, for validate_query_select, and 468b256 adds 2.
  • On 24 Nov the description gained the Select index rows of that day (Q1, S1 to S8, X1, X2, W1 to W5). They give the order of the Select index check against the key condition's shape and four other faults. The code is unchanged.

What

Query now refuses a KeyConditionExpression with two conditions on one key attribute. The refusal has the service's text, Invalid KeyConditionExpression: KeyConditionExpressions must only contain one condition per key. parse_key_condition_with_maps in crates/core/src/expression/key_condition.rs makes the check. Two clauses name the same attribute when their paths resolve to the same name, written as a plain name or as a #name reference. The engine parses the key condition for every storage backend, so the refusal applies on SQLite, PostgreSQL, MongoDB and Cassandra. No storage backend changed.

handle_query in crates/engine/src/query.rs now checks every Query in one order:

  1. The raw-body checks of validate_query_request_body, which fix(query): check Query values against the key schema and the number rules #544 added to main: Limit, the ReturnConsumedCapacity value, parameter mixing, the ExpressionAttributeNames map and every number in ExpressionAttributeValues. Then deserialization. The handler removes an invalid Select from the body before deserialization and keeps its error. With fix(query): check Query values against the key schema and the number rules #544's shape fault rule, a member of the wrong JSON type answers ahead of that error. Otherwise the Select error answers at step 5.
  2. The parameter checks of check_request_parameters, which are main's code in main's order: an empty KeyConditionExpression, names or values with no expression, and ConditionalOperator usage. This branch's copies of the Limit, ReturnConsumedCapacity and parameter mixing checks are gone. Step 1 makes those checks with fix(query): check Query values against the key schema and the number rules #544's code.
  3. Every FilterExpression fault, in main's relative order: grammar and reserved words, then undefined #name references, then the begins_with and ordering operand types, then undefined :value references. The read uses the filter that this step parses.
  4. Every KeyConditionExpression fault except its shape: reserved words and grammar (syntax, redundant parentheses, nested paths), then undefined #name and :value references in written order. Then come the begins_with operand type, the duplicate, and last <> and contains.
  5. Select, the table, the index, ConsistentRead and the Select index check.
  6. The key condition's shape, where main parsed the key condition: an equality on the partition key, and at most one non-equality condition. parse_key_condition_with_maps returns a shape fault inside Ok, and handle_query returns it here. The legacy KeyConditions desugar, the vector index refusal, the key schema checks, projection and the unused name and value checks follow, as on main. Among them, handle_query calls fix(query): check Query values against the key schema and the number rules #544's refuse_begins_with_on_number for a legacy KeyConditions request only, because step 4 refuses the same value in a KeyConditionExpression.

On the expression path, an undefined key condition :value no longer reaches the storage query builders. PartiQL SELECT reaches handle_query through its translation. Its key conditions hold one partition key equality and at most one sort key condition, on distinct #name references that are all defined. So the duplicate and reference refusals cannot fire there.

For every Query, authorize_request calls refuse_duplicate_key_condition before the IAM decision. When the key condition has two conditions on one attribute, or the Select value is invalid, that function runs check_request and returns its error. check_request makes the checks above that come before the table read. So every principal gets the answer the table owner gets. For a duplicate, that answer is the one-condition-per-key text, or a fault that the service reports ahead of it. Main answered AccessDeniedException to a principal whose policy denied such a request. Item 14 under Answers that change against main gives the answers for an invalid Select.

DynamoDB refuses the duplicate before it evaluates the policy. On 19 Nov, 9 duplicate shapes went twice to the table owner and to 6 roles. Four roles had a dynamodb:LeadingKeys condition, one had no Allow for dynamodb:Query, and one had a Deny on it. All 126 requests got Invalid KeyConditionExpression: KeyConditionExpressions must only contain one condition per key. Beside Limit 0, an undefined #q in the filter or an undefined #u in the key condition, every principal got that fault.

Authorization passes leading_keys::names_map to that function as the request's names, so names_map is now pub(crate). Apart from that, crates/server/src/leading_keys.rs is main's code. A key condition without a duplicate goes to the IAM decision, and dynamodb:LeadingKeys binds through the map-less key_condition_clause_operands and parse_key_condition, as on main.

key_condition.rs was over 600 lines on main, and this change does not split it. Against main at 5252eda the PR adds 591 production lines and removes 187. It adds 1017 test lines and removes 53.

Answers that change against main

The row labels refer to the measurements under Why. "10 Nov" is the set of 10 November.

With one fault in the request:

  1. The parse refuses an undefined key condition :value, with the Invalid KeyConditionExpression: prefix. Main refused it in the storage query builder, without the prefix. Rows C1, C2, C3, C5, F1e and 10 Nov.
  2. An undefined key condition #name that main treated as a missing key, such as #k = :v, answers the undefined name. Main answered "Query condition missed key schema element: pk". 10 Nov.
  3. The filter check refuses an undefined filter :value before the read, also on a partition with no items, where main served Count 0. F3.
  4. begins_with(pk, :n) with a number answers the operand type message. Main answered "partition key condition must use equality (=)". P1.

With two faults in the request, where the order changed:

  1. The parameter checks of check_request_parameters answer ahead of the table checks, which main runs first.
    • Values with no expression (M1), an empty KeyConditionExpression (M2) and ConditionalOperator with no QueryFilter (M3) answer ahead of a missing table.
    • Not measured: names with no expression, and M1 to M3 beside an index, Select or ConsistentRead fault. They follow the same order.
  2. A filter fault answers ahead of these faults:
  3. An undefined filter :value answers ahead of projection faults, unused names or values, Select and projection conflicts, and ExclusiveStartKey faults. Main refused it at evaluation. This follows F3 and F1d and is not measured against each.
  4. These key condition faults answer ahead of Select BOGUS, a missing table, IndexName nope and ConsistentRead on a GSI, which main answered first. They are the size and token limits, reserved words and grammar, undefined references, the begins_with operand type, the duplicate, and <> and contains.
    • B1 to B5 measure this for the duplicate.
    • On a missing table, R2 measures it for a syntax error and R5 for an undefined value. R6 measures it for a begins_with number, R7 for <> and R8 for a nested path.
    • No row measures the size and token limits or reserved words.
    • A fault in the key condition's shape still answers after Select BOGUS (Q1), a missing table (R10, R11), IndexName nope (S7) and ConsistentRead on a GSI (S6), as on main.
  5. Select BOGUS answers after a filter fault (H3) and a key condition fault (B4, the duplicate), which main checks after it. It answers after Limit (L7) and parameter mixing (F2c), as on main since fix(query): check Query values against the key schema and the number rules #544.
  6. An undefined key condition reference answers ahead of an unused name or value, where main answered the unused check. C3 and 10 Nov.
  7. Well-formed <> and contains clauses answer after an undefined name (E2, E3), a later syntax error (E10) and a nested path in another clause. R8 measures the nested path in the first clause. Main answered the operator refusal.
  8. With IndexName naming a vector index, a key condition reference fault or begins_with operand fault answers ahead of the vector index refusal. Not measured.
  9. The filter check refuses a FilterExpression that names an internal legacy placeholder such as :_kc_pk, beside KeyConditions, as undefined, as the service does (R9). Main resolved it from the desugared KeyConditions. When the request defines :_kc_pk, the filter's operand check reads the request's value, where main read the KeyConditions value.
  10. For a principal whose policy denies the request, an invalid Select beside a filter fault or a key condition fault answers that fault. Main answers the Select text, because fix(query): check Query values against the key schema and the number rules #544 checks Select before the IAM decision. This branch refuses an invalid Select before the IAM decision too, after the filter and key condition checks (H3, B4). The answer for such a principal is not measured. With no other fault, every principal gets the Select text, as on main.

Some orders stay as on main. With one fault, a number in begins_with on an S sort key (E1) answers the begins_with operand type message, as on main since #544. Limit comes ahead of the parameter, filter and key condition checks (L2 to L5), a missing table (L6) and Select BOGUS (L7). #544 gave main that order. Beside IndexName nope, ConsistentRead on a GSI or Select ALL_ATTRIBUTES on a KEYS_ONLY index, Limit 0 is not measured. Parameter mixing comes ahead of a filter fault (H5). It also comes ahead of a missing table, IndexName nope, Select BOGUS and ConsistentRead on a GSI (F2a to F2d). The order inside the filter is main's, and F1, F1b, F1c and F1d answer as on main. A fault in the key condition's shape answers after the table, index, ConsistentRead and Select checks, as on main. The service answers it after the table (R10, R11), the index (S7), ConsistentRead (S6) and an invalid Select (Q1), and before the Select index check (S1).

What stays different from DynamoDB

Why

Fixes #430.

On main, Query served a key condition with two conditions on one key attribute. For pk = :a AND pk = :b the parser read the first equality as the partition key condition and the second as the sort key condition. On a table with pk HASH and sk RANGE, the request returned the items in partition :a whose sort key equals :b. DynamoDB refuses these shapes with one message.

Every measurement is against DynamoDB in us-east-1. The client sent each row of 31 Oct, 10 Nov, 11 Nov, 19 Nov, 23 Nov and 24 Nov twice. Both sends got the same answer. Each set measured these shapes and orders:

  • 24 Oct: which shapes the service refuses as a duplicate and which it serves. Also the duplicate against nested paths, redundant parentheses, an undefined name and a reserved word.
  • 31 Oct: the duplicate against the key value type check, undefined references, an unused value and an IndexName the table does not have.
  • 10 Nov, on a hash-only table: without a duplicate, an undefined reference answers ahead of an unused name or value.
  • 11 Nov, 09:43 UTC: filter faults ahead of the duplicate (A), and the duplicate ahead of the table, index, ConsistentRead and Select checks (B). Also references in written order (C), and <>, contains and begins_with beside a duplicate (D).
  • 11 Nov, 11:22 and 11:23 UTC: <>, contains and begins_with with and without a duplicate (E1 to E6, E10 to E12). Also parameter mixing ahead of the duplicate (E8, E9).
  • 11 Nov, 12:42 to 12:44 UTC: filter faults ahead of key condition faults (F1 to F1g) and ahead of the table checks (H1 to H4). Also parameter mixing ahead of the table checks (F2a to F2d) and of a filter fault (H5). F3 is an undefined filter value on an empty partition.
  • 11 Nov, 13:26 UTC: Limit ahead of every other check (L1 to L7), and parameter checks ahead of a missing table (M1 to M3). Also begins_with on the partition key (P1, P2). The SDK's parameter checks were off, so Limit 0 reached the service.
  • 11 Nov, 14:39 UTC: the 24 Oct duplicates on a non-key attribute (J1) and on a hash-only table (J2). Also the two Query on a single-attribute key schema applies a condition on a non-key attribute to the sort key #674 rows without a duplicate (J3, J4).
  • 19 Nov, 15:37 and 19:27 UTC: the duplicate for the table owner and for the 6 roles described under What.
  • 23 Nov, 16:44 to 17:00 UTC, on a missing table: faults ahead of the table (R1, R2, R5 to R8, R12, R13). Shape faults that answer after it (R10, R11). An N value that is not a number beside a duplicate (R3, R4). A filter that names :_kc_pk beside KeyConditions (R9).
  • 24 Nov, 21:36 to 22:04 UTC: the Select index check against the key condition's shape and four other faults. The rows (Q1, S1 to S8, X1, X2, W1 to W5) are in the comment on Query looks up the table, the index and ConsistentRead before it checks the expressions, and reads the key condition before the filter #1208.
24 and 31 October rows

24 October 2026, 00:27 to 00:37 UTC. The table has pk S HASH and sk S RANGE and holds alice/bob and alice/carol. boto3 ran with retries off.

KeyConditionExpression DynamoDB answer
pk = :a OK, 2 items
pk = :a AND sk = :b OK, 1 item
sk = :b AND pk = :a OK, 1 item
pk = :a AND sk BETWEEN :b AND :c OK, 2 items
pk = :a AND begins_with(sk, :b) OK, 1 item
pk = :a AND pk = :b ValidationException, one condition per key
pk = :a AND pk = :a ValidationException, one condition per key
pk = :a AND pk = :b AND sk = :c ValidationException, one condition per key
pk = :a AND pk > :b ValidationException, one condition per key
pk > :b AND pk = :a ValidationException, one condition per key
:a = pk AND pk = :b ValidationException, one condition per key
(pk = :a) AND (pk = :b) ValidationException, one condition per key
pk = :a AND sk = :b AND sk = :c ValidationException, one condition per key
pk = :a AND sk = :b AND sk = :b ValidationException, one condition per key
pk = :a AND sk BETWEEN :b AND :c AND sk = :b ValidationException, one condition per key
pk = :a AND sk BETWEEN :b AND :c AND begins_with(sk, :b) ValidationException, one condition per key
pk = :a AND begins_with(sk, :b) AND sk = :c ValidationException, one condition per key
pk = :a AND sk > :b AND sk < :c ValidationException, one condition per key
pk = :a AND begins_with(sk, :b) AND begins_with(sk, :c) ValidationException, one condition per key
pk = :a AND pk = :b AND sk = :b AND sk = :c ValidationException, one condition per key
sk = :b AND sk = :c ValidationException, one condition per key
#p = :a AND pk = :b (#p -> pk) ValidationException, one condition per key
#p = :a AND #q = :b (both -> pk) ValidationException, one condition per key
pk = :a AND #s = :b AND sk = :c (#s -> sk) ValidationException, one condition per key
pk = :a AND zz = :b AND zz = :c ValidationException, one condition per key
pk = :a AND pk = :b on a hash-only table ValidationException, one condition per key
pk = :a AND pk.foo = :b ValidationException, conditions on nested attributes
((pk = :a)) AND (pk = :b) ValidationException, redundant parentheses
#u = :a AND #u = :b (#u undefined) ValidationException, attribute name is not defined
pk = :a AND pk = :b AND other = :c ValidationException, reserved keyword: other
begins_with(pk, :a) AND sk = :b ValidationException, Query key condition not supported
pk BETWEEN :a AND :b ValidationException, Query key condition not supported

31 October 2026, 17:37 UTC, AWS CLI. The table has pk S HASH and sk S RANGE. :a, :b, :c are strings and :n is the number 1. Every answer is a ValidationException.

KeyConditionExpression and parameters DynamoDB answer
pk = :a AND pk = :b one condition per key
pk = :a AND pk = :n one condition per key
#p = :a AND pk = :n (#p -> pk) one condition per key
pk = :n One or more parameter values were invalid: Condition parameter type does not match schema type
pk = :a AND #s = :b AND sk = :n (#s -> sk) one condition per key
#u = :a AND sk = :b AND sk = :c, no ExpressionAttributeNames Invalid KeyConditionExpression: An expression attribute name used in the document path is not defined; attribute name: #u
pk = :a AND pk = :b AND #u = :c, no ExpressionAttributeNames the same undefined-name text for #u
#u = :a AND #u > :b, no ExpressionAttributeNames the same undefined-name text for #u
pk = :a AND pk = :b, no ExpressionAttributeValues Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :a
pk = :a AND pk = :b with an unused :z in ExpressionAttributeValues one condition per key
pk = :a AND pk = :b with IndexName nope, an index the table does not have one condition per key
10 and 11 November rows

10 November 2026, 23:33 UTC, on a table keyed on pk (S) alone. In the answers, "undefined name #x" and "undefined value :x" stand for the service's texts with the Invalid KeyConditionExpression: prefix, or with Invalid FilterExpression: where the row says "filter".

KeyConditionExpression and maps DynamoDB answer
pk = :v, only an unused :w defined undefined value :v
#k = :v, no names, :v and an unused :w defined undefined name #k
#k = :v, an unused #j defined, :v defined undefined name #k
pk = :v, an unused #j and an unused :w defined undefined value :v
#k = :v, no names, only an unused :w defined undefined name #k

11 November 2026, 09:43 to 14:39 UTC. The table has pk (S) HASH, sk (S) RANGE and a KEYS_ONLY global secondary index g-keys on g (S). J2 and J4 ran on the hash-only table of 10 Nov. :a, :b, :c are strings and :n is a number. A missing table is a table name that does not exist. For F1f, F1g and H1 to H5 the request column comes from the labels recorded with each answer.

Row Request DynamoDB answer
A1 pk = :a AND pk = :b, FilterExpression #q = :a, no names filter: undefined name #q
A2 the same with FilterExpression foo = :zz filter: undefined value :zz
A3 the same with FilterExpression begins_with(foo, :n) Invalid FilterExpression: Incorrect operand type for operator or function; operator or function: begins_with, operand type: N
A4 the same with FilterExpression name = :a Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: name
A5 the same with FilterExpression zz = = :a Invalid FilterExpression: Syntax error; token: "=", near: "= = :a"
B1 to B5 pk = :a AND pk = :b on a missing table (B1), with IndexName nope (B2) and with Select BOGUS (B4). g = :a AND g = :b on g-keys with ConsistentRead (B3) and with Select ALL_ATTRIBUTES (B5) the duplicate
C1 pk = :q AND #u = :c, no names or values undefined value :q
C2 sk = :b AND pk = :a, no values undefined value :b
C3 pk = :a AND sk = :missing, :a and an unused :extra defined undefined value :missing
C4 pk = :a AND pk = :b AND #u = :c, no names or values undefined value :a
C5 pk = :q AND pk = :b AND #u = :c, :b and :c defined undefined value :q
C6 #u = :a AND #u = :b, no names or values undefined name #u
C7 pk = :b AND pk = :a, no values undefined value :b
D1, D2 pk = :a AND pk <> :b, pk = :a AND contains(pk, :b) the duplicate
D3 pk = :a AND begins_with(sk, :n) AND sk = :b Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: begins_with, operand type: N
E1 pk = :a AND begins_with(sk, :n) the operand type message of D3
E2, E3 pk = :a AND sk <> :b AND #u = :c, pk = :a AND contains(sk, :b) AND #u = :c, no names undefined name #u
E4, E5 pk = :a AND sk <> :b, pk = :a AND contains(sk, :b) Invalid KeyConditionExpression: Invalid operator used in KeyConditionExpression
E6 pk = :a AND contains(sk) Invalid KeyConditionExpression: Incorrect number of operands for operator or function; operator or function: contains, number of operands: 1
E8 pk = :a AND pk = :b with ConditionalOperator AND and no QueryFilter Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {ConditionalOperator} Expression parameters: {KeyConditionExpression}
E9 pk = :a AND pk = :b with legacy KeyConditions the same text with {KeyConditions}
E10 pk = :a AND sk <> :b AND Invalid KeyConditionExpression: Syntax error; token: "<EOF>", near: "AND"
E11, E12 pk = :a AND sk <> :b AND sk = :c, pk = :a AND contains(sk, :b) AND sk = :c the duplicate
F1 pk = :a AND sk = :missing, FilterExpression #q = :a, no names filter: undefined name #q
F1b the same key condition, FilterExpression zz = = :a Invalid FilterExpression: Syntax error; token: "=", near: "= = :a"
F1c pk = :a AND #s = :b, no names, FilterExpression #q = :a filter: undefined name #q
F1d pk = :a AND #s = :b, no names, FilterExpression foo = :zz filter: undefined value :zz
F1e pk = :a AND sk = :missing, FilterExpression foo = :a undefined value :missing
F1f pk = :a AND begins_with(sk, :n), FilterExpression #q = :a filter: undefined name #q
F1g pk = :a AND, FilterExpression #q = :a filter: undefined name #q
F2a to F2d pk = :a with ConditionalOperator AND on a missing table (F2a), with IndexName nope (F2b) and with Select BOGUS (F2c). g = :a on g-keys with ConditionalOperator AND and ConsistentRead (F2d) the mixing text of E8
F3 pk = :a on a partition with no items, FilterExpression foo = :zz filter: undefined value :zz
H1 to H4 pk = :a, FilterExpression #q = :a, on a missing table (H1), with IndexName nope (H2) and with Select BOGUS (H3). g = :a on g-keys with ConsistentRead (H4) filter: undefined name #q
H5 pk = :a, FilterExpression #q = :a, ConditionalOperator AND the mixing text with Expression parameters: {FilterExpression, KeyConditionExpression}
L1 to L7 Limit 0 alone (L1), beside pk = :a AND (L2), pk = :a AND pk = :b (L3), FilterExpression #q = :a with no names (L4), ConditionalOperator AND (L5), a missing table (L6) and Select BOGUS (L7) 1 validation error detected: Value at 'Limit' failed to satisfy constraint: Member must have value greater than or equal to 1
P1 begins_with(pk, :n) the operand type message of D3
P2 begins_with(pk, :s), :s a string Query key condition not supported
M1 legacy KeyConditions with ExpressionAttributeValues, on a missing table ExpressionAttributeValues can only be specified when using expressions
M2 an empty KeyConditionExpression, on a missing table Invalid KeyConditionExpression: The expression can not be empty;
M3 legacy KeyConditions with ConditionalOperator AND and no QueryFilter, on a missing table ConditionalOperator cannot be used without Filter or Expected
J1 pk = :a AND zz = :b AND zz = :c the duplicate
J2 pk = :a AND pk = :b, on the hash-only table the duplicate
J3 pk = :a AND zz = :c Query condition missed key schema element: sk
J4 pk = :a AND zz = :c, on the hash-only table The number of query conditions (2) exceeds the number of key attributes defined in the schema (1)
23 November rows

23 November 2026, 16:44 to 17:00 UTC. boto3 ran with retries and parameter validation off. Every request went to a table name that does not exist. :a is alice and :b is bob, both strings. :n is the number 1, and :t is the boolean true.

Row Request DynamoDB answer
R1 pk = :a AND pk = :b, FilterExpression zz = = :a ValidationException Invalid FilterExpression: Syntax error; token: "=", near: "= = :a"
R2 pk = :a AND sk <> :b AND ValidationException Invalid KeyConditionExpression: Syntax error; token: "<EOF>", near: "AND"
R3 pk = :u AND pk > :v, :u = {"N": "bob"}, :v = alice ValidationException The parameter cannot be converted to a numeric value: bob
R4 pk > :v AND pk = :u, the values of R3 the text of R3
R5 pk = :q, no values ValidationException Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :q
R6 pk = :a AND begins_with(sk, :n) ValidationException Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: begins_with, operand type: N
R7 pk = :a AND sk <> :b ValidationException Invalid KeyConditionExpression: Invalid operator used in KeyConditionExpression
R8 pk.x = :a AND sk <> :b ValidationException Invalid KeyConditionExpression: KeyConditionExpressions cannot have conditions on nested attributes
R9 KeyConditions pk EQ alice, FilterExpression foo = :_kc_pk, no values ValidationException Invalid FilterExpression: An expression attribute value used in expression is not defined; attribute value: :_kc_pk
R10 pk > :a ResourceNotFoundException Requested resource not found
R11 pk > :a AND sk > :b ResourceNotFoundException Requested resource not found
R12 pk = :a, FilterExpression begins_with(foo, :n) ValidationException Invalid FilterExpression: Incorrect operand type for operator or function; operator or function: begins_with, operand type: N
R13 pk = :a, FilterExpression foo < :t ValidationException Invalid FilterExpression: Incorrect operand type for operator or function; operator or function: <, operand type: BOOL

Overlap with #479

#479 added three checks that this PR also makes:

  • KeyCondition::validate_references in references.rs. It read names before values, each in sorted order. The service names the first undefined reference in written order (C4, C5), as check_references here does.
  • ONE_CONDITION_PER_KEY in no_hash_equality.rs, another copy of the text that ONE_CONDITION_PER_KEY_ERR holds here.
  • refuse_two_conditions_on_one_attribute, which refused that text for two non-equality clauses. With this PR, parse_key_condition_with_maps refuses every duplicate before it.

The two PRs merge in sequence, this PR first. #479 sits on this branch and dropped those three copies in aeec7b0. Its own commits conflict with this branch in crates/engine/src/query.rs, and its merge commits resolve that.

Merging

At 468b256 this branch merges with main at 5252eda without a conflict. At 16:40 UTC on 25 Nov, 105 other open PRs changed a file of this PR or of #479. 42 of them conflict with main themselves and were not checked. 48 merge with this branch without a conflict, and 15 conflict with it:

Testing done

New tests in tests/test_query_key_condition_one_per_key.py compare the service's whole text, except where a row says "by the prefix":

  • TestDuplicateAheadOfRequestChecks: A1 to A5 (A5 by the prefix), B1 to B5, E8 and E9.
  • TestReferenceAndOperatorOrder: C1 to C7, the 10 Nov rows, D1 to D3, E1 to E3, and E10 (by the prefix) to E12.
  • TestQueryCheckOrder: F1 to F1g (F1b by the prefix), F2a to F2d, F3 and H1 to H5. Also R10 and R11, which expect ResourceNotFoundException on a missing table.
  • TestLimitAndParameterChecksFirst: L1 to L7, P1 and M1 to M3. The Limit 0 rows use the suite's dynamodb_client_no_validation fixture.

The classes of the first revision stay: TestOneConditionPerKey (shapes from 24 Oct) and TestDuplicateCheckOrder (7 of the 31 Oct requests). TestOneConditionPerKey now also has the J1 and J2 rows. Each class is one table of rows with one parametrized test_refused. Core unit tests pin the parse order: first_undefined_reference_in_text_order_is_reported, operators_and_operands_around_the_duplicate_check, other_ne_and_contains_clauses_keep_their_refusal and the_shape_is_refused_after_the_checks. The last one checks that parse_key_condition_with_maps returns a shape fault inside Ok. duplicate_condition_on_one_key_attribute_rejected also holds the 13 multi-attribute shapes of the classifier test that 534fb77 removes. test_duplicate_key_condition_is_refused_before_leading_keys in tests/test_fgac_leading_keys.py sends the 9 shapes of 19 Nov as 6 principals and expects the one-condition-per-key text. Four principals have a dynamodb:LeadingKeys condition, one has no Allow for dynamodb:Query, and one has a Deny on it. Authorization binds no value for a duplicate now, and crates/server/src/leading_keys/tests.rs equals main's file. test_partition_equality_does_not_bind_another_clause is main's test_unreadable_partition_equality_does_not_bind_another_clause. It sends pk = :u AND pk > :v and pk > :v AND pk = :u with :u outside the fence, and expects the one-condition-per-key text for the table owner and both its policies. a_duplicate_gets_the_answer_of_the_table_owner in crates/engine/src/query/request_checks.rs pins the faults that answer ahead of the duplicate in authorization. an_invalid_select_is_refused_before_authorization in the same file pins the answers there for an invalid Select: the Select text for pk = :own and pk > :own, and the filter fault for the shape of H3.

Checks on the tree of 468b256, with main at 5252eda merged in:

  • cargo +1.97.0 fmt --all -- --check: exit 0.
  • cargo +1.97.0 clippy --locked -p extenddb-core -p extenddb-engine --all-targets -- -D warnings: exit 0. The same for the engine crate with --features extenddb-engine/backup-test-hooks: exit 0.
  • cargo +1.91.1 check --locked -p extenddb-core -p extenddb-engine --all-targets: exit 0.
  • cargo +1.97.0 test --locked --no-fail-fast -p extenddb-core -p extenddb-engine --lib: core 1183 passed and 1 ignored, engine 937 passed.
  • With the condition of bf57850 back in refuse_duplicate_key_condition (if duplicate), an_invalid_select_is_refused_before_authorization fails: pk = :own with Select BOGUS answers Ok(()).
  • cargo +1.97.0 test --locked -p extenddb-wasm-adapter --test parity: every_case_gives_its_expected_answer passed.
  • A SQLite server built from 468b256 on 127.0.0.1 ran tests/test_query_key_condition_one_per_key.py (98 tests) and tests/test_fgac_leading_keys.py (32 tests): 130 passed. It ran fix(query): check Query values against the key schema and the number rules #544's tests/test_query_key_value_types.py, tests/test_query_leading_keys_values.py and tests/test_query_value_shapes.py: 329 passed.
  • python3 -m pytest --collect-only -q of tests/ and of tests/python: 7170 and 672 tests, no error.

Checks on the tree of b83c3b8:

  • cargo +1.97.0 fmt --all -- --check: exit 0.
  • cargo +1.97.0 clippy --locked -p extenddb-core -p extenddb-engine -p extenddb-server --all-targets -- -D warnings: exit 0. The same for the engine and server crates with --features extenddb-engine/backup-test-hooks: exit 0.
  • cargo +1.97.0 clippy -- -W clippy::pedantic on the same three crates warns on 6 lines that the PR adds. These are 2 doc comments without backticks, 2 HashMap parameters, an item after statements in a test, and a binding name in the moved parameter checks. So the Clippy box stays unticked.
  • cargo +1.91.1 check --locked -p extenddb-core -p extenddb-engine -p extenddb-server --all-targets: exit 0.
  • cargo +1.97.0 test --locked --no-fail-fast -p extenddb-core -p extenddb-engine -p extenddb-server: core 1152 passed, engine 874 passed, server 513 passed and 5 failed.
    • The 5 are backup_store::tests::a_defaulted_root_with_rows_is_handled_as_a_configured_one, a_permissive_lock_file_is_narrowed, an_existing_spool_must_be_owner_only, an_unreadable_catalog_keeps_a_failing_default_root_configured and the_spool_lock_refuses_a_second_instance.
    • They fail on the test host because uid 65534 owns an ancestor directory of the spool. backup_store.rs is main's code, and this PR does not change it.
  • A SQLite server built from b83c3b8 ran tests/test_query_key_condition_one_per_key.py (98 tests) and tests/test_fgac_leading_keys.py (32 tests): 130 passed.
  • A SQLite server built from 1c6a6c6, the merge commit before the fixes, fails R10 and R11 with the shape ValidationException.

Checks on the tree of 3746280 that this round did not repeat:

  • With the call in authorize_request removed, the two changed tests in tests/test_fgac_leading_keys.py fail with AccessDeniedException.
  • tests/test_query_key_condition_one_per_key.py against DynamoDB: 96 passed. R10 and R11 come from the 23 Nov measurements, and the file did not run against DynamoDB with them.

Checks on the tree of 9733e94, before the authorization change:

  • cargo +1.97.0 fmt --all -- --check: exit 0.
  • cargo +1.97.0 clippy --locked --all-targets --features extenddb-storage-cassandra/test-hooks,extenddb-storage-mongodb/test-hooks -- -D warnings: exit 0.
  • cargo +1.97.0 clippy --locked --all-targets --features extenddb-engine/backup-test-hooks -- -D warnings: exit 0.
  • cargo +1.97.0 test --locked -p extenddb-core -p extenddb-engine -p extenddb-server: core 1102 passed, engine 769 passed, server 448 passed and 5 failed, the same 5. Core had 1106 before five unit tests became one table.
  • A SQLite server built from the branch (cargo +1.97.0 build --locked -p extenddb --no-default-features --features sqlite) ran the wire tests:
    • devtools/run-tests --extenddb --config <config> --rust-integration --filter query: 47 passed, 0 failed.
    • devtools/run-tests --extenddb --config <config> --pytest --filter "one_per_key or leading_keys or test_query or query_scan or key_condition or KeyCondition or partiql_key or partiql_check_order or conditional_operator": 761 passed, 0 failed.
    • That run includes tests/test_query_key_condition_one_per_key.py (96), tests/test_fgac_leading_keys.py (32), tests/test_query_scan.py (116), tests/test_conditional_operator.py (42) and tests/test_partiql_check_order.py (66).
    • run-tests exits 1 on the Python run because its backup store step selects none of its 19 tests under this filter. pytest exits 5 when no test runs.
  • The same server got 85 rows, two sends each. They are every row measured on 10 Nov and on 11 Nov up to 13:26 UTC. They also include 12 unmeasured rows, the 7 rows of Query: the missing key refusal comes before the filter syntax and unused value checks when the key condition has an equality #712 and 1 control row.

52a90f6 and 9733e94 change no answer. 52a90f6 runs the filter's operand checks through one helper and cuts the comments that repeated this description. 9733e94 writes the wire tests as tables (925 lines down to 422), and the new file sends the same requests as the old one. At c560e41, with the duplicate check removed from parse_key_condition_with_maps, the J1 and J2 tests got HTTP 200 and failed.

At the first revision, a server built from main failed the duplicate wire tests: pk = :a AND pk = :b answered 200 with Count 1.

Not run here: cargo test --workspace, the whole Python suite, and the MongoDB, PostgreSQL and Cassandra suites. CI runs the MongoDB, PostgreSQL and Cassandra suites.

CI at 98d09b3: 64 of 64 checks pass, and no code scanning alert is open.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a

Breaking changes

Some Query requests that main served now get an error:

  • a key condition with two conditions on one key attribute
  • an undefined FilterExpression :value on a partition with no items (F3)
  • a FilterExpression that names an internal legacy placeholder such as :_kc_pk, beside KeyConditions, when the request does not define it (the service refuses it too, R9)

For a key condition with two conditions on one key attribute, main answered AccessDeniedException to a principal whose policy does not allow the request. That principal now gets the ValidationException. Beside an invalid Select, a filter fault or a key condition fault now answers ahead of the Select text for every principal. A request with more than one fault can get a different error than on main, as the list under What gives.


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

…e key

A key condition that named the same key attribute twice was served. For
`pk = :a AND pk = :b` the parser took the first equality as the partition key
condition and the second as the sort key condition, so on a table with
pk HASH and sk RANGE the request returned the items in partition `:a` whose
sort key equalled `:b`. The same hole accepted two conditions on the sort key,
including BETWEEN or begins_with paired with another sort key condition.

DynamoDB rejects all of these with
"Invalid KeyConditionExpression: KeyConditionExpressions must only contain one
condition per key" (measured 2026-10-24, us-east-1). It resolves every `#name`
and `:value` reference before it looks for the duplicate, so an undefined name
or value is reported first, and it reports the duplicate ahead of the key value
type check and the unused-value check (measured 2026-10-31).

parse_key_condition_with_maps takes the request's expression attribute names
and values. After the parser's own checks, and before any clause is
classified, it refuses two conditions on one key attribute, written with plain
names or through `#name` references. When there is a duplicate it reports an
undefined `#name` and then an undefined `:value` first. A key condition
without a duplicate takes the same path as before. The engine's Query handler
parses through it, and the server's LeadingKeys extraction runs the same check
through key_condition_clause_operands, so a duplicate leaves
dynamodb:LeadingKeys absent whichever way it is written.

Fixes #430
@LeeroyHannigan
LeeroyHannigan force-pushed the fix/key-condition-one-per-key branch from 1632b19 to 4a2d382 Compare October 31, 2026 19:55
@navareamol navareamol added the v1 Needed for the 1.0 release label Nov 3, 2026
@navareamol navareamol added this to the 1.0 milestone Nov 3, 2026
@demohan demohan self-assigned this Nov 10, 2026
@pdf-amzn pdf-amzn self-assigned this Nov 10, 2026

@pdf-amzn pdf-amzn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR seems to have become more convoluted due to how it interacts with 479. It may just be simpler to combine this and 479 into one change, I think it's going to be easier to reduce duplication overall in the result.

AI generated:

I reviewed this together with #479, since the two overlap. I tested main (cbc3680) merged with both PRs on PostgreSQL: the full pytest suite shows no regressions against main, and the new wire file passes (30/30). The new wire files also pass against DynamoDB (30/30 and 83/83).

Size. +962/-61 is mostly tests. Net against the merge base: 75 lines of production code, 58 of comments, 764 of tests (420 Rust, 344 Python). The fix itself is reject_duplicate_key_attrs plus the wiring in the two callers. Most of the remaining weight is the same duplicate-shape and check-order rows asserted at three layers: core unit tests, key_condition_duplicate_check_order_matches_the_service in expression_helpers.rs, and the Python TestOneConditionPerKey / TestDuplicateCheckOrder.

Should-fix: undefined references are checked only when there is a duplicate. reject_duplicate_key_attrs (crates/core/src/expression/key_condition.rs:485) reports an undefined #name, then an undefined :value, but only after it has found a duplicate (:507). Without one, an undefined #name is caught later and only for the clauses the classifier resolves, by resolve_path_attr (:537, called at :220 and :234 through resolve_key_schema_with). An undefined :value isn't checked in core or the engine at all: check_key_value_type skips it (crates/engine/src/query.rs:632), and the first refusal is maps.resolve_value in each backend's query builder (e.g. storage-sqlite/src/data/query.rs:46), without the Invalid KeyConditionExpression: prefix. That is the third difference listed in this PR's description. The two message strings are also written out again here; they already exist at :547 and in resolve_value_for (resolver.rs:83). Check every key condition's references once, first, in parse_key_condition_with_maps: all names, then all values, with the prefix. #479's KeyCondition::validate_references (references.rs:25) is that check. Then reject_duplicate_key_attrs only has to detect the duplicate. That would also fix the missing prefix, keep undefined values from reaching storage, and report an undefined #name ahead of "missed key schema element" (#712). One service row is needed before doing it: a no-duplicate key condition with an undefined reference plus an unused value, to confirm the order between those two checks.

Should-fix: #429's classifier still has its own duplicate check, and it can no longer fire in production. resolve_multipart_with (crates/core/src/expression/key_condition.rs:244-259) refuses one condition per key with an inline copy of the message. Every production caller now reaches it through resolve_key_schema_with after parse_key_condition_with_maps has already run reject_duplicate_key_attrs; the only remaining callers of bare parse_key_condition are tests. Either remove that branch, or keep it as a guard for the map-less parse_key_condition path with a comment saying so and point it at ONE_CONDITION_PER_KEY_ERR (:469).

Nit-or-follow-up: parse_key_condition is still pub and re-exported from expression/mod.rs, but only tests call it. Drop the re-export, or document it as the deliberately map-less entry that skips the duplicate check.

Nit-or-follow-up: on the LeadingKeys equality path the duplicate check runs twice: key_condition_clause_operands runs reject_duplicate_key_attrs, then equality_pk_value parses again with parse_key_condition_with_maps. Harmless; one shared parse would do it once.

Nit-or-follow-up: key_condition_duplicate_check_order_matches_the_service repeats 8 order rows the core unit tests and the Python file already cover. Its unique value is showing that parse_key_condition_expr passes the maps through and applies the prefix; two rows (one duplicate, one undefined name) show that. That saves about 70 lines.

Nit-or-follow-up: raw_clause_values (key_condition.rs:767) builds a Vec<&Expr> per clause; it's only on the error path, but an iterator would avoid the allocation.

Overlap with #479. #479 adds refuse_two_conditions_on_one_attribute (no_hash_equality.rs:219), which refuses the same message but only for two non-equality clauses, later, in the engine. This PR's parse-time check covers every schema and every operator, so it should own the refusal. Whichever of the two merges second should drop #479's copy; I've said the same on #479.

The FGAC change (a duplicate now leaves dynamodb:LeadingKeys absent, so some principals get ValidationException instead of AccessDeniedException) looks fine to me: the engine refuses every duplicate, so no item is read either way. Keep the "not measured" note in leading_keys.rs.

@demohan demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Measured on us-east-1, two samples per request. The duplicate refusal is correct on every base table, GSI, LSI, multi-attribute GSI and hash-only shape tried, and the wire files pass on SQLite, PostgreSQL, MongoDB and Cassandra. Four items remain: the refusal now comes ahead of FilterExpression errors that main reported correctly; it comes after the table, index, ConsistentRead and Select checks, which the service runs later; undefined references follow text order on the service; and the LeadingKeys binding for a duplicate needs a measurement before it changes. Small items: the substring assertion, and tests for the measured non-key and hash-only rows.

Comment thread crates/engine/src/query.rs Outdated
// before the clauses are classified, which is where the service
// reports it (measured 2026-10-31).
let parsed =
crate::expression_helpers::parse_key_condition_expr(kce_str, &maps, &ctx.limits)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate is now refused before the FilterExpression is parsed and checked. The service reports the filter first. With KeyConditionExpression pk = :a AND pk = :b (both defined) and FilterExpression #q = :a (#q undefined), DynamoDB answers Invalid FilterExpression: An expression attribute name used in the document path is not defined; attribute name: #q (us-east-1, two samples). This branch answers KeyConditionExpressions must only contain one condition per key; main answered the filter error. The same holds for foo = :zz with :zz undefined, begins_with(foo, :n) with :n a number, name = :c (reserved word) and the filter syntax errors zz = = :c and foo = :a OR. The projection, Select, start key, unused value and key type checks come after the duplicate on the service, as here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5ea180d, which checks every FilterExpression fault ahead of every key condition fault, and your 12 Nov review measured that order. With e3e1da1, authorization runs the same checks in the same order for a key condition with a duplicate. So a principal fenced by dynamodb:LeadingKeys also gets the #q filter error, as all 7 principals did on the service on 19 Nov.

Comment thread crates/engine/src/query.rs Outdated
// before the clauses are classified, which is where the service
// reports it (measured 2026-10-31).
let parsed =
crate::expression_helpers::parse_key_condition_expr(kce_str, &maps, &ctx.limits)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DynamoDB reports the duplicate ahead of the table and index lookups. pk = :a AND pk = :b against a table that does not exist answers Invalid KeyConditionExpression: KeyConditionExpressions must only contain one condition per key, not ResourceNotFoundException. It answers the duplicate too with IndexName nope, with ConsistentRead true on a GSI, with Select BOGUS, and with Select ALL_ATTRIBUTES on a KEYS_ONLY GSI. Two samples each, us-east-1. This branch answers the other error on all five. A refusal after the expression grammar, reserved word and reference checks of both expressions, and before anything that reads the table, matches every case measured here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 822c472 and 5ea180d: the duplicate comes ahead of the table, index, ConsistentRead and Select checks, and b13ec53 puts Limit first. e3e1da1 keeps that order for every principal. Authorization now refuses a duplicate with the same checks before the IAM decision, and those checks read nothing from the table.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate still answers ahead of the table, index, ConsistentRead and Select checks (B1 to B5). 5ea180d had also moved the key condition's shape checks ahead of the table, which the service does not do. On 23 Nov, on a table that does not exist, pk > :a and pk > :a AND sk > :b answered ResourceNotFoundException, as on main. In 19bb1dd the shape checks run after the table checks again, where main runs them, and the wire tests send both requests (R10, R11).

if !duplicated {
return Ok(());
}
for clause in clauses {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service names the first undefined reference in the order the expression is written, whether it is a #name or a :value. A single reference check run first would need to walk the clauses in that order, not all names and then all values. Without a duplicate the service does the same: pk = :q AND #u = :c with no maps answers :q, and sk = :b AND pk = :a with no values answers :b. An undefined reference is reported before an unused value or name: pk = :a AND sk = :missing with :a and an unused :extra answers the :missing text (two samples each). pk = :a AND pk = :b AND #u = :c with no ExpressionAttributeNames and no ExpressionAttributeValues answers Invalid KeyConditionExpression: An expression attribute value used in expression is not defined; attribute value: :a (two samples). This loop reports #u because it checks every name before any value. pk = :q AND pk = :b AND #u = :c with :b and :c defined answers :q on the service and #u here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8b49672. One reference check walks the tokens in written order, names and values alike, before the duplicate check. Your 12 Nov review measured the reference order as the service's.

&undefined("missing"),
"pk = :a AND pk = :missing",
);
let tokens = tokenize("pk = :a AND pk = :b AND #u = :c").unwrap();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case expects #u; the service answers :a for this request (see the comment on line 510). The order is measurable: #u = :a AND #u = :b with no maps answers #u, and pk = :b AND pk = :a with no values answers :b.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8b49672. first_undefined_reference_in_text_order_is_reported expects :a for this request with no maps. It also expects #u for #u = :a AND #u = :b and :b for pk = :b AND pk = :a.

) -> Result<(), DynamoDbError> {
let mut seen: Vec<&str> = Vec::with_capacity(clauses.len());
let mut duplicated = false;
for clause in clauses {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two more shapes where the service reports something else before the duplicate. pk = :a AND pk <> :b and pk = :a AND contains(pk, :b) answer the duplicate on the service and an operator error here. pk = :a AND begins_with(sk, :n) AND sk = :b with :n a number answers Invalid KeyConditionExpression: Incorrect operand type for operator or function; operator or function: begins_with, operand type: N on the service and the duplicate here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8b49672. The parse refuses <> and contains after the duplicate check, and it checks the begins_with operand type before it. Your 12 Nov review measured the three shapes as the service answers them.

Comment thread crates/server/src/leading_keys.rs Outdated
// a second condition on one key attribute, written with plain names
// or through a `#name` reference, is refused here with the check the
// engine runs, so it leaves the key absent whatever its operators.
let pk_clauses: Vec<_> = key_condition_clause_operands(&tokens, &names, &values)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leaves dynamodb:LeadingKeys absent for every duplicate, where main bound the equality's value. For pk = :own AND sk = :b AND sk = :c, a principal fenced to own by ForAnyValue:StringEquals now gets AccessDeniedException instead of ValidationException, and one fenced away by ForAllValues:StringEquals without a Null guard gets ValidationException instead of AccessDeniedException. The service binds the operand and authorizes first for pk > :v (the note below). Which value it binds for a duplicate needs a measurement with a restricted principal before the binding changes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc8d192 put main's binding back. With e3e1da1, authorization refuses a key condition with a duplicate before the IAM decision, so it binds no dynamodb:LeadingKeys value for one. On 19 Nov your 9 shapes answered the one-condition-per-key message to the table owner and to 6 roles. Two of those roles had no Allow for Query or a Deny on it.

Comment thread tests/test_fgac_leading_keys.py Outdated
# -- An equality on the partition key decides the key ------------------
# -- A second clause on the partition key binds nothing ----------------

def test_unreadable_partition_equality_does_not_bind_another_clause(self, table):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both policies here expect AccessDeniedException whether dynamodb:LeadingKeys is absent or bound to :u, so the test passes on either binding. A case where the readable value is the fenced value (for example pk = :own AND sk = :b AND sk = :c) gives different answers for the two bindings.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In e3e1da1 both policies here expect the one-condition-per-key ValidationException, which the service returned for these requests on 19 Nov. Without the fix this assertion fails with AccessDeniedException. test_duplicate_key_condition_is_refused_before_leading_keys sends pk = :own AND sk = :b AND sk = :c and the other eight shapes from your 12 Nov comment to six principals.

)
)
assert code == "ValidationException"
assert UNDEFINED_NAME in msg

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UNDEFINED_NAME in msg passes on a message without the Invalid KeyConditionExpression: prefix. The service answers the exact text in UNDEFINED_NAME_EXACT.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ef09b47. Since 9733e94, every row in the file goes through _refused, which compares the whole message. Only the syntax error rows compare the prefix, because the service words those errors differently.

Main is 241 commits ahead of the branch point, and the merge is clean.
Three files changed on both sides.

crates/core/src/expression/mod.rs: main exports removes_only, and this
branch exports parse_key_condition_with_maps.

crates/engine/src/query.rs: when the request has an ExclusiveStartKey,
main runs the Select check for an index after the start key checks. Main
also adds the start key bounds check. This branch changes the key
condition parse further down the same function.

crates/server/src/leading_keys.rs: main makes extract_typed_value visible
to the crate, and this branch changes query_leading_keys.
The branch made authorization run the one-condition-per-key check, so a
key condition with two conditions on one key attribute left
dynamodb:LeadingKeys absent. Main bound the partition equality's value
for such a condition. Which value the service binds for it is not
measured. This restores main's binding: crates/server/src/leading_keys.rs
is main's code again, and key_condition_clause_operands takes the tokens
alone, as on main.

The answers differ for `pk = :own AND sk = :b AND sk = :c`. With main's
binding, a principal fenced to `own` by ForAnyValue:StringEquals reaches
the engine and gets the one-condition-per-key ValidationException. With
the key absent, it got AccessDeniedException. A unit test in
leading_keys/tests.rs and a wire test in tests/test_fgac_leading_keys.py
pin main's answer. The engine refuses every such key condition, so it
reads no item either way.
parse_key_condition_with_maps reported an undefined reference only when
the key condition also had a second condition on one key attribute. It
checked every #name before any :value. Without a duplicate, the
classifier reported an undefined #name later. An undefined :value
reached the storage query builders, which refused it without the
`Invalid KeyConditionExpression: ` prefix.

The parse now takes the request's ExpressionMaps and checks, after the
grammar and before any clause is classified:

1. Every #name and :value reference, once, in the order the expression
   is written. The first undefined one is refused with the prefix, with
   or without a duplicate. The engine's unused name and value check runs
   after the parse, so an undefined reference comes first.
2. A begins_with operand that is not a string or a binary value.
3. A second condition on one key attribute.

`<>` and `contains` are parsed as clauses and refused after these
checks, with the messages they had.

Measured in us-east-1 on 10 and 11 Nov 2026, two sends each, the same
answer both times:

- `pk = :q AND #u = :c` with no maps answers `:q`.
- `sk = :b AND pk = :a` with no values answers `:b`.
- `pk = :a AND pk = :b AND #u = :c` with no maps answers `:a`. The core
  test that expected `#u` for this request now expects `:a`.
- `pk = :q AND pk = :b AND #u = :c` with `:b` and `:c` defined answers
  `:q`.
- `#u = :a AND #u = :b` with no maps answers `#u`.
- `pk = :a AND sk = :missing` with an unused `:extra` answers `:missing`,
  and `pk = :v` or `#k = :v` with an unused name or value answers the
  undefined reference.
- `pk = :a AND pk <> :b` and `pk = :a AND contains(pk, :b)` answer the
  one-condition-per-key message.
- `pk = :a AND begins_with(sk, :n) AND sk = :b` with `:n` a number
  answers "Incorrect operand type for operator or function; operator or
  function: begins_with, operand type: N".

Without a duplicate, the begins_with operand check now also runs ahead of
the key value type check. The service's order for that request is not
measured.

The undefined name message has one copy in key_condition.rs, and the
undefined value message comes from ExpressionMaps::resolve_value_for.
The branch refused a key condition with two conditions on one key
attribute after the table, index, ConsistentRead and Select checks. It
refused it ahead of the FilterExpression checks. The service runs both
the other way round.

Measured in us-east-1 on 11 Nov 2026, two sends each, the same answer
both times. The table has pk (S) HASH, sk (S) RANGE and a KEYS_ONLY
global secondary index on g (S). The key condition is
`pk = :a AND pk = :b`:

- A missing table, IndexName `nope`, ConsistentRead on the index, Select
  `BOGUS` and Select `ALL_ATTRIBUTES` on the index each answer the
  one-condition-per-key message.
- A FilterExpression `#q = :a` with `#q` undefined, `foo = :zz` with
  `:zz` undefined, `begins_with(foo, :n)` with `:n` a number,
  `name = :a` (a reserved word) and `zz = = :a` each answer the filter's
  own message.

handle_query now parses the key condition right after deserialization.
When the parse answers the one-condition-per-key message, the filter's
grammar, reserved word, reference and operand type checks run. The
first fault they find is returned, and otherwise the duplicate. Any
other fault of the parse is reported further down, where it was. An
invalid Select is left out of the typed input and its error is returned
after this check. The parse result is used further down, so the key
condition is parsed once.

tests/test_query_key_condition_one_per_key.py pins the measured rows.
Its table gets the KEYS_ONLY index. For `zz = = :a` the test compares
the expression the error names. ExtendDB words its filter syntax errors
differently from the service.
test_undefined_name_reported_ahead_of_duplicate checked that the
message contained the undefined name text. It passed on a message
without the `Invalid KeyConditionExpression: ` prefix. It now compares
the whole message with UNDEFINED_NAME_EXACT. That is the service's text
for `#u = :a AND #u = :b` with no ExpressionAttributeNames, measured in
us-east-1 on 24 Oct 2026.
tests/test_query_key_condition_one_per_key.py gets the requests behind
the reference order and operator change, each with the service's
answer:

- The first undefined reference in the order the expression is written,
  a #name or a :value, with or without a duplicate. Measured in
  us-east-1 on 11 Nov 2026.
- An undefined reference ahead of an unused name or value (measured on
  10 and 11 Nov 2026). The 10 Nov rows were sent to a table keyed on
  pk (S) alone. The test table also has sk (S) as its sort key, and none
  of these requests names it.
- `pk = :a AND pk <> :b` and `pk = :a AND contains(pk, :b)` answer the
  one-condition-per-key message (measured on 11 Nov 2026).
- `pk = :a AND begins_with(sk, :n) AND sk = :b` with `:n` a number
  answers the begins_with operand type message (measured on 11 Nov
  2026).

Every send in both runs got the same answer twice.
… guard

resolve_multipart_with refused a second condition on one key attribute
with its own copy of the message. The engine never reaches that branch
with a duplicate, since parse_key_condition_with_maps refuses one before
the clauses are classified. Authorization still parses with the map-less
parse_key_condition when it finds dynamodb:LeadingKeys, and keeps the
binding it had for such a condition. On a multi-attribute key schema the
branch is what refuses the duplicate there.

The branch stays, with a comment that says so, and returns
ONE_CONDITION_PER_KEY_ERR. No answer changes.
key_condition_duplicate_check_order_matches_the_service repeated eight
order rows that the core tests and the wire file already cover. What it
adds is that parse_key_condition_expr passes the request's maps to the
parse, with the `#` and `:` keys a request carries. Two rows show that:
`#p = :a AND pk = :b` with `#p` defined as pk answers the
one-condition-per-key message, and `pk = :a AND pk = :b AND #u = :c`
answers the undefined name message for `#u`. The test is renamed
key_condition_parse_reads_the_request_maps, and the file is 74 lines
shorter.
Earlier commits on this branch left five spots unformatted.
cargo +1.97.0 fmt --all rewraps a statement, a constant, three test
table rows and an assertion in key_condition.rs. It also joins one loop
header in leading_keys/tests.rs. No code changes.
resolve_multipart_with refused a second condition on one key attribute
with its own copy of the check. No request reaches it with one. The
engine refuses a duplicate in parse_key_condition_with_maps before any
clause is classified. Authorization refuses one before the IAM decision
(refuse_duplicate_key_condition), ahead of the map-less
parse_key_condition it reads for dynamodb:LeadingKeys. The sorts that
put the HASH and RANGE equalities in key-schema order stay, with a
comment that says what they are for.

The thirteen multi-attribute shapes of
a_second_condition_on_one_key_attribute_is_refused move into the table
of duplicate_condition_on_one_key_attribute_rejected, so they run
through the parse that refuses them. The doc comments of
resolve_multipart and resolve_multipart_with no longer list the check.
parse_clauses returned RawClause::Unsupported for a `<>` or `contains`
clause, so that the duplicate check could read its attribute.
classify_clauses refused such a clause before any clause reached
raw_to_sk or key_condition_clause_operands. Both still had to match the
variant, in arms no input reaches, and raw_to_sk returned a Result for
that arm alone.

The parser now returns a Clause: a RawClause that a key condition
supports, or the path and message of a clause it does not. The
reference, begins_with and duplicate checks read Clause.
refuse_unsupported_clauses refuses the first unsupported clause and
returns the RawClauses. raw_to_sk is main's infallible function again,
and the two arms are gone. No answer changes.
check_request parsed the whole KeyConditionExpression before
handle_query read the table. So the shape refusals of classify_clauses
answered ahead of the table, index, ConsistentRead and Select checks.
Main and DynamoDB answer those checks first for these key conditions.
On us-east-1, on 23 Nov, each of these went twice to a table that does
not exist:

    pk > :a              ResourceNotFoundException: Requested resource not found
    pk > :a AND sk > :b  ResourceNotFoundException: Requested resource not found

Before this commit the branch answered "partition key condition must
use equality (=)" and "one condition must be an equality on the
partition key".

parse_key_condition_with_maps still refuses the faults that come after
the grammar and before the table on the service. These are the
references in written order, the begins_with operand type, the
duplicate, and the <> and contains refusal. It now returns a fault of
the shape inside Ok. handle_query returns that fault where main parsed
the key condition: after the ConsistentRead and Select index checks,
and before the vector index refusal.

The wire tests add both requests as R10 and R11. A core test checks
that the parse passes three shape faults and returns each refusal
inside Ok.
check_begins_with_operands wrote out the begins_with operand type check
and message a second time, beside validate_begins_with_operands in
resolver.rs. check_begins_with_operand in resolver.rs now refuses a
:value that is not a string or a binary value, with a prefix the caller
gives. Both checks call it: the key condition check with its Invalid
KeyConditionExpression prefix, as before, and the filter and condition
check with none, as before. No answer changes.
… only

parse_filter_expression checks a FilterExpression's begins_with and
ordering operand types against the request's values. Beside legacy
KeyConditions, handle_query checked them a second time, against the
request's values with the desugared KeyConditions values added. The
desugar adds only _kc_ placeholders. So that pass repeated the first
one. The second pass now runs only for a desugared QueryFilter, whose
values the first pass does not see.

One answer changes. A FilterExpression can name a _kc_ placeholder that
the request also defines. The first pass checks the request's value,
and the second pass checked the KeyConditions value that replaces it in
the merged map.
The doc comment of Names::key_condition in request_attributes.rs said
that it reads a key condition as parse_key_condition_expr reads it. The
engine parse now reads the request's ExpressionAttributeNames and
ExpressionAttributeValues and refuses more. The comment now says that
this function reads the clause paths with the map-less
parse_key_condition. That parse accepts key conditions that the engine
refuses for an undefined reference, a begins_with operand type or a
second condition on one key attribute.
test_unreadable_partition_equality_does_not_bind_another_clause also
sent its two key conditions with :u = {"N": "bob"}, and it checked only
the error code. Authorization refuses the duplicate before it reads any
value, so those requests took the path of the readable ones. DynamoDB
answers them with "The parameter cannot be converted to a numeric
value: bob" (us-east-1, 23 Nov, two sends each). This server answers
the one-condition-per-key text, and the check of those numbers is the
subject of #544. The test keeps the readable rows, under the name
test_partition_equality_does_not_bind_another_clause.

The module docstring of test_query_key_condition_one_per_key.py drops
its issue number. The TestOneConditionPerKey docstring no longer says
that the class holds every measured duplicate shape. The reserved word
row of 24 Oct is not in its table.
The upgrade manual had no entry for this change. A key condition with
two conditions on one key attribute now gets the ValidationException
for every principal, ahead of the IAM decision. Two FilterExpression
shapes that 1.0.0 ran are refused too. The new v1.1.0 entry lists them.
It also says that the 0.1.15 LeadingKeys entry does not hold for a key
condition with two conditions on one key attribute.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

@pdf-amzn On the two open items of your 10 Nov review, combining this PR with #479 and which one drops its copy:

On the classifier's duplicate check: on 11 Nov I kept it as the guard of the map-less parse_key_condition. Since e3e1da1, authorization refuses a duplicate before it reads dynamodb:LeadingKeys, so no request reached that check. 534fb77 removes it, and its multi-attribute rows now run through parse_key_condition_with_maps.

LeeroyHannigan added a commit that referenced this pull request Nov 24, 2026
The size check added every clause into the HASH or RANGE total,
including a second clause on one key attribute. `pk = :a AND pk = :b`
with 1025 bytes in each value got the aggregated hash sentence.
`pk = :p AND sk = :a AND sk = :b` with 600 bytes in each got the range
sentence. Main serves both. DynamoDB refuses both with `Invalid
KeyConditionExpression: KeyConditionExpressions must only contain one
condition per key`. The `pk` shape is from a measurement on 2026-11-24
in us-east-1 (sent twice), and #489 records the `sk` shape.

Each key attribute now contributes its largest value once, and the
single hash check reads that value. The one-condition-per-key refusal is
#484's. Two rows join the equality table.

@demohan demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items from the earlier rounds are resolved at 98d09b3, with one exception, which 19bb1dd introduced (inline).

  • Filter faults, then the duplicate, then the table, index, ConsistentRead and Select checks, then written-order references, operators and begins_with operand types: on a SQLite build of the head, these answer as the service answers. Each row was sent twice to both. On the merge-base most of these rows answer differently.
  • The duplicate is refused before LeadingKeys. A principal with an explicit Deny on the table and a principal with no policy get the same one-condition-per-key text on an existing table, a missing table, a missing index and a GSI. Malformed requests without a duplicate still answer AccessDeniedException. check_request reads no storage and takes no lock on this path. 1,000 Queries with a 3.9 KB key condition ran within noise of 9733e94.
  • Each new test fails with its fix reverted. CI is green on 64 of 64 checks, and the new wire file passes 98 of 98 on all four backends.

I will approve once the order in the inline comment is restored. The pk = :a AND name = :b reserved-word order stays with #1007.

// Parsed and checked above. A fault in its shape answers here, after
// the table checks (measured 2026-11-23: `pk > :a` on a table that does
// not exist answers ResourceNotFoundException).
(parsed?, None)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 19bb1dd the shape fault is unwrapped after validate_query_select_index_support (line 121), and the service answers it first. This request was sent four times to each target:
{"TableName": "t", "IndexName": "gix", "Select": "ALL_ATTRIBUTES", "KeyConditionExpression": "gpk > :a", "ExpressionAttributeValues": {":a": {"S": "x"}}} on a KEYS_ONLY GSI.

  • Service: ValidationException: Query key condition not supported
  • 9733e94: Invalid KeyConditionExpression: partition key condition must use equality (=)
  • This head: One or more parameter values were invalid: Select type ALL_ATTRIBUTES is not supported for global secondary index gix because its projection type is not ALL

pk > :a AND sk > :b and begins_with(pk, :a) behave the same way. The measured order puts the shape check after the table, index and ConsistentRead checks and before the Select index check. The commit message says the service answers the Select check first, which holds for Select BOGUS and not for this case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the service. On 24 Nov, on a table with a KEYS_ONLY index and Select ALL_ATTRIBUTES, gpk > :a, gpk > :a AND gsk > :b and begins_with(gpk, :a) answered Query key condition not supported, two sends each. A missed key schema element, a key value of the wrong type, an unused value and a ProjectionExpression also answer ahead of the Select index check.

Main answers the Select index error first for all of these, and this PR keeps main's order, so it changes none of these answers. Moving the check would change answers that this fix does not touch, so I left the order to #1208, which covers Query's check order. The rows are in the comment on #1208. The description now says what the service does and lists the rows under What stays different from DynamoDB.

Main is 13 commits ahead of the branch, and the merge is clean. Three
files changed on both sides and merged without a conflict:
crates/core/src/expression/resolver.rs, crates/engine/src/lib.rs and
docs/manuals/07-upgrade-manual.md.
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
The branch of #484 merged main at a417b2f, the main commit that
0c4ddaf merged into this branch. The tree does not change. With this
merge, this branch has one merge base with the branch of #484.
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
…ct index check

Row A3 sends sk > :a to the KEYS_ONLY index g-keys with Select
ALL_ATTRIBUTES. On 25 November the service answered Query condition
missed key schema element: g to both sends. The engine skips the Select
index check for every key condition with no HASH equality. W5 covers
only the Query key condition not supported message. On #484 merged with
main, ExtendDB answers A3 with the Select index message.
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
…ty check

The new test sends pk > :v and zz > :v to the vector index. The
measured table has HASH pk, RANGE sk and a vector index. On 25 November
the service answered Query operation not supported on this index type
to each of two sends. This branch parses such a key condition and
refuses it after the vector index check. On #484 merged with main,
ExtendDB answers both with the parser's message: Invalid
KeyConditionExpression: partition key condition must use equality (=).
Main is 10 commits ahead of the branch. Two files conflicted.

docs/manuals/07-upgrade-manual.md: both sides add a v1.1.0 entry at the
same place. Both stay, main's #398 entry first.

crates/engine/src/query.rs: #544 (1ddc087) moved the Limit, enum,
parameter mix, names and number checks of a Query into
query_key_values::validate_query_request_body. That function reads the
raw body before deserialization and before the IAM decision. This branch
runs its checks in query::request_checks::check_request. The three
conflict regions take this branch's side. handle_query reads the request
through check_request, and the empty key condition, parameter usage and
ConditionalOperator checks stay in check_request_parameters.
check_request now starts with validate_query_request_body and drops its
own copies of the ReturnConsumedCapacity enum check and the Limit check.
check_request_parameters drops its copy of the parameter mix check.
The copies of #544 merged first.

The Select enum check moves out of validate_query_request_body into
validate_query_select, which check_request runs after the
FilterExpression and KeyConditionExpression checks, with the same shape
fault rule. The measured rows settle the order. An undefined filter name
(H3) and a second condition on one key attribute (B4) answer ahead of an
invalid Select. An N value that is not a number (f1 of #544) and a Limit
of 0 (L7, f3) answer ahead of it. The server's check before the IAM
decision no longer includes Select. A principal the policy denies, with
an invalid Select, now gets AccessDeniedException where main gave the
Select text. That answer is not measured.

Both PRs refuse a begins_with value that is not a string or a binary
value. This branch's parser check stays, because the measured rows put
that refusal ahead of the duplicate check (D3) and in the parse (E1,
P1). handle_query now calls refuse_begins_with_on_number (#544) for a
legacy KeyConditions request only, so a KeyConditionExpression is not
checked twice.

Two unit tests of #544 that read the Select answer from
validate_query_request_body now read it from validate_query_select.
The merge of main moved the Select check behind the FilterExpression and
KeyConditionExpression checks. The check that the server makes before
the IAM decision then no longer covered Select. A principal the policy
denies got AccessDeniedException for an invalid Select. Main gives that
principal the Select text, because #544 checks Select before the IAM
decision.

refuse_duplicate_key_condition now runs check_request for an invalid
Select too. Every principal gets the Select text, or a fault that
check_request reports ahead of it. The measured rows still hold. An
undefined filter name (H3) and a duplicate (B4) answer ahead of an
invalid Select. No row measures the answer for a principal the policy
denies.

The new test an_invalid_select_is_refused_before_authorization sends
pk = :own and pk > :own with Select BOGUS, and the shape of H3.

It also deletes the three comment lines above the
validate_query_request_body call in check_request, which repeated the
list of checks in that function's doc.
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
The branch of #484 merged main at 5252eda, which adds #544 (1ddc087).
Its next commit, 468b256, refuses an invalid Select before the IAM
decision. One file conflicted, crates/engine/src/query.rs, in three
places.

The key schema checks of handle_query: this branch runs them only when
the key condition has an equality on the HASH attribute. #544 runs the
classifier's type check only on a multi-attribute key schema or for
legacy KeyConditions, and #484's branch calls refuse_begins_with_on_number
for legacy KeyConditions only. The resolution keeps this branch's guard
around their code, and this branch's missed_key_schema_element helper.

refuse_without_hash_equality, beside the new doc of
check_request_parameters: both stay. The engine refusal drops its call
of refuse_begins_with_on_number. With #544 that function refuses a
begins_with value that is not a string or a binary value. The parse
refuses that value before the table lookup, and lacks_hash_equality
holds only for a KeyConditionExpression, so the call could not fail.
Rows 6 and the two begins_with rows of 1 November in
test_query_lone_non_equality.py stay, and the parse answers them.

The test modules: all three stay.

The code that only that call needed goes too.
KeyCondition::without_equality put a second clause that is a
begins_with into sk_condition, where the engine refusal read it. It now
keeps the first clause there. Two engine tests called the refusal with a
begins_with on a number key: this merge deletes one, and the other keeps
its row 6b.
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
The module docstring of test_query_lone_non_equality.py placed a
begins_with on a number HASH attribute after an unused value and an
invalid FilterExpression. No row sends that shape beside either fault,
and #484's parse refuses that begins_with value ahead of the unused
value check of handle_query. Its sentence that no other value is checked
also left out the parse's begins_with refusal, which covers any
attribute.

The docstring now gives each order with the rows that measure it:

- two conditions on one attribute ahead of a wrong-typed value
- that value ahead of the key schema wording, and after an unused value
  and an invalid FilterExpression
- the operand type message for a begins_with number on a number HASH
  attribute, in either clause

@demohan demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order our inline named is in #1208, and the body lists it under what stays different; with Select: ALL_ATTRIBUTES on a KEYS_ONLY index beside a key condition fault, this head answers the Select text first as main does, where the service answers the key condition fault.

468b256 refuses an invalid Select before the authorization decision. For the table owner this head answers as 468b256's parent on all 25 rows, and the 12 invalid-Select rows match the service in us-east-1 on 11, four sends each. A user with an explicit Deny and a user with no policy, which got AccessDeniedException on 10 of those 12 rows before, now get the Select text or the fault the owner gets, and the answer is the same for an existing and a missing table. Removing the new trigger or checking Select before the filter each fails a test. fmt, clippy, the 1.91.1 check and 1457 engine and server tests are clean, and the one failing CI job, wasm, is main's, which #1482 fixes.

parsnips pushed a commit to parsnips/extenddb that referenced this pull request Dec 4, 2026
…endDB#481)

* fix(query): refuse key condition values over the key size limits

Closes ExtendDB#435.

The service refuses a Query whose key condition values exceed the key
size limits before it reads anything; ExtendDB ran the Query. Measured on
2026-10-23, every row repeated with identical answers:

- A HASH value over 2048 bytes: "One or more parameter values were
  invalid: Size of hashkey has exceeded the maximum size limit of2048
  bytes" (no space after "of", as the service writes it). 2048 bytes is
  accepted.
- HASH values that each fit but add up to more than 2048 bytes (an index
  with two HASH attributes): "One or more parameter values were invalid:
  Aggregated size of all hash keys has exceeded the size limit of 2048
  bytes". A sum of exactly 2048 is accepted. A single oversized value
  gets the single sentence even when the sum is also over.
- RANGE values adding up to more than 1024 bytes, one clause or several:
  "One or more parameter values were invalid: Aggregated size of all
  range keys has exceeded the size limit of 1024 bytes". 1024 is
  accepted. Every operator is sized (=, <, <=, >, >=, begins_with);
  BETWEEN contributes its larger bound, not the sum of both.
- The HASH checks are reported before the RANGE check.
- Strings are sized by UTF-8 bytes (683 U+20AC characters, 2049 bytes,
  refused; 682 plus "xx", 2048 bytes, accepted) and binary values by
  byte length. Base tables, LSIs, GSIs and legacy KeyConditions behave
  the same.
- An undefined #name or :value in the key condition is reported before
  the size check.

Ordering, measured with a 2049-byte value: filter syntax, unused names
and values, index name, ConsistentRead on a GSI, missing table, Limit,
projection syntax and overlap, the Select conflicts and the filter
operand type are all reported first; the ExclusiveStartKey checks after.

The check is crates/core/src/validation/key_size.rs, called from
handle_query after the expression validations and before the start key
checks. The limits come from the configuration and the sentences name
them.

Tests: unit tests in validation/key_size/tests.rs and the engine;
tests/python/test_query_key_size.py, test_query_key_size_values.py and
test_query_key_size_order.py (64 rows) pass against the service. On
ExtendDB alone, five rows with two or more RANGE clauses on one index
skip until the multi-attribute range change in ExtendDB#429 merges (the handler
refuses the shape earlier today); they pass on the merged tree. The
undefined-name row stays unit level because the undefined-name refusal
for a sort key clause arrives with ExtendDB#479.

Pre-existing, measured, not changed here: a BETWEEN with the lower bound
above the upper bound is refused by the service with a bound-order
sentence; two conditions on one key are refused with "KeyConditionExpressions
must only contain one condition per key"; a Number with 39 or more digits
is refused with "Attempting to store more than 38 significant digits in a
Number"; a FilterExpression on a key attribute is refused with "Filter
Expression can only contain non-primary key attributes". ExtendDB accepts
all four.

* fix(cassandra): pass the bare index name in IndexNotFound so the Query sentence matches the other backends

* test(query): skip the idx accept rows on a backend that cannot read a multi-RANGE index

The Cassandra backend answers InternalServerError to any Query on an index
with several RANGE attributes: its ORDER BY lists sk_* then base_pk and skips
the sk2_* to skN_* clustering columns, which Cassandra refuses with
"Order by currently only supports the ordering of columns following their
declared order in the PRIMARY KEY". require_multi_range now also skips on
InternalServerError, so test_one_600_byte_range_value_is_accepted carries the
probe like the other idx rows.

* test(query): skip later-RANGE accept rows on first-RANGE-only backends

Since ExtendDB#429, the MongoDB and Cassandra backends refuse a key condition on
a later RANGE attribute of a multi-attribute index with a
ValidationException. Two rows of TestGsiQueryKeySize expect such a Query
to be served, and they failed on MongoDB. They now skip when a probe
gets that refusal, as the multi-range tests on main do. The refusal rows
still run on those backends, because the size check answers first.

* fix(core): read the key byte size through the key_size module in restorable_item

Main added validation/restorable_item.rs, which sizes a backup item's key with key_value_byte_size from the validation module. This branch moved that function into validation/key_size.rs. The call now names the new module, as check_key_size does.

* fix(core): check the number rules before sizing a Query key value

Query sized an N value in a key condition by its digit string, so an invalid
number got the key size sentence. The service checks the number first.
Measured 2026-11-20 in us-east-1 on a GSI with HASH n (N), each shape twice:

- 2049 or 39 digits answer "Attempting to store more than 38 significant
  digits in a Number", in KeyConditionExpression and in KeyConditions.
- 1 followed by 2100 zeros answers "Number overflow. Attempting to store a
  number with magnitude larger than supported range".
- The service serves 2100 zeros followed by 1.

validate_query_key_sizes now passes each N value it reads to
validate_and_normalize_number and sizes the normalized digits. Query served
a 39-digit value before and refuses it now, as the service does.

The doc comment of validate_query_key_sizes no longer lists two shapes as
not measured. The wire tests assert both, a HASH sum of exactly 2048 bytes
and the aggregated hash sentence over an oversized RANGE value. Both pass
against the service.

The comment also names a request that ExtendDB serves and the service
refuses. The request has an undefined #name in a second clause, on a key
schema with one HASH attribute and at most one RANGE attribute.

* fix(query): check Select against the GSI projection after the key size

With no ExclusiveStartKey, Query checked Select=ALL_ATTRIBUTES against the
index projection before the other request checks. The service makes that
check after the key size check. With a start key, it also comes after the
start key checks. Measured 2026-11-20 in us-east-1 on a KEYS_ONLY GSI with
Select=ALL_ATTRIBUTES, each shape twice:

- A 2049-byte key value answers the size sentence, with and without an
  ExclusiveStartKey.
- A 1-byte value answers the Select sentence.
- Without a start key, Limit 0 and a parameter mix each answer their own
  refusal ahead of the Select sentence. So do an empty or invalid
  KeyConditionExpression, a missed key schema element, a wrong-typed key
  value and a filter or projection fault. ProjectionExpression with
  ALL_ATTRIBUTES and an unused name or value also come first. ExtendDB
  answered the Select sentence for all of them.

The check now runs once, after the size and start key checks. The comment
above the size check no longer says that the type check is still to come.
The type check runs above it, in resolve_key_schema_with. The engine test
comments cite the measurement dates and region, not local files.

* test(query): add the number and Select rows and keep refusal rows visible

- test_query_key_size_values.py: rows on a GSI with HASH n (N) for 2049
  digits, 39 digits, 1 followed by 2100 zeros and 2100 zeros followed by 1.
  Each row runs in KeyConditionExpression and in KeyConditions.
- test_query_key_size_order.py: rows on a KEYS_ONLY GSI for
  Select=ALL_ATTRIBUTES with a 2049-byte and a 1-byte value, with and
  without an ExclusiveStartKey. The Limit 0 row uses the raw_client fixture
  from conftest.py in place of its own copy.
- This commit removes require_multi_range. Neither of its skips fires on any
  backend at this head. On a refusal row, a 500 or a classifier change
  turned the row into a skip.
- The BETWEEN row is two tests. The refusal half reports its result on
  MongoDB and Cassandra, where the accept half skips.
- The docstrings cite the measurement dates and region, not local files.

* docs(core): cite ExtendDB#1214 for the undefined name that Query serves

The doc comment of validate_query_key_sizes names one request that ExtendDB
serves and the service refuses: an undefined #name in a second key
condition clause. ExtendDB#1214 tracks that request.

* docs(query): split the long sentences in four new comments

Four new comments now use shorter sentences. They are the number rules in
sized_clause and its unit test, the undefined #name in
validate_query_key_sizes and the Select check in handle_query. They state
the same measurements.

* fix(core): size a Query key number the validator does not read by its text

001fe29 sized each N value of a key condition after
validate_and_normalize_number, so a number written in other Unicode
decimal digits got a 400. DynamoDB reads U+FF11 and U+0967 U+0968 as
digits. It serves `n = :v` and the KeyConditions form with those values
(200, Count 0, measured 2026-11-20 in us-east-1, twice each), and so did
the previous head.

The size check now calls normalize_number and refuses only the digit
count and magnitude faults. A value the validator does not read as a
number is sized by its text again, as before 001fe29. The unit table
and TestNumberKeyValues gain the two values, in both syntaxes.

The Select test uses the shared refused helper. Its 1-byte row without a
start key goes, because test_query_start_key_partition.py sends the same
request. Two comments are shorter.

* fix(core): do not size a Query key N value that is not a number

31159a2 sized an N value that normalize_number does not read as a
number by its text. So 2049 `x` or 2049 U+FF11 in a key condition got
the hash size sentence. DynamoDB refuses such a value with a number
sentence before it looks up the table (measured 2026-11-24 in us-east-1,
twice each). 2049 `x` gets `The parameter cannot be converted to a
numeric value: ` and the value, in KeyConditionExpression and in
KeyConditions. 39 U+FF11 get `Attempting to store more than 38
significant digits in a Number`.

Such a value now adds nothing to the size, and ExtendDB serves the Query
as main does. The number check of key condition values is ExtendDB#544's. The
unit table gains a 2049 `x` row and a 2049 U+FF11 row.

* test(query): fold the key size rows into two tables

query_key_size_tests.rs had 19 tests that sent rows of the core tests
through the expression parser. The wire files send the same rows end to
end. The file keeps the two tests that add a path: the clause order in
the expression, and the KeyConditions desugaring. The comments of two
deleted index tests named a gate that b116a46 took out.

In the core tests, one table holds the equality rows on the base table,
on `idx` and on `two-hash`. A second table holds the six
undefined-reference rows. Each row keeps its measurement comment. Two
rows have measurements from 2026-11-20 on `two-hash`, and their comments
no longer say "Not measured". They are a HASH sum of exactly 2048 bytes, and
the aggregated hash sentence with a 1025-byte RANGE value. Two tests
checked the end of a sentence that a table row asserts in full, and they
are gone.

* fix(core): count each key attribute once in the Query key size

The size check added every clause into the HASH or RANGE total,
including a second clause on one key attribute. `pk = :a AND pk = :b`
with 1025 bytes in each value got the aggregated hash sentence.
`pk = :p AND sk = :a AND sk = :b` with 600 bytes in each got the range
sentence. Main serves both. DynamoDB refuses both with `Invalid
KeyConditionExpression: KeyConditionExpressions must only contain one
condition per key`. The `pk` shape is from a measurement on 2026-11-24
in us-east-1 (sent twice), and ExtendDB#489 records the `sk` shape.

Each key attribute now contributes its largest value once, and the
single hash check reads that value. The one-condition-per-key refusal is
ExtendDB#484's. Two rows join the equality table.

* style(core): clear the clippy warnings in the Query key size code

The pedantic clippy run flagged `ExtendDB` without backticks in the doc
comment of validate_query_key_sizes (doc_markdown). The two tables of
762d4a8 tripped type_complexity, a default lint that CI's
`-D warnings` refuses. Each table now names its row type. After this
commit, clippy with `-W clippy::pedantic` and `--all-targets` on
extenddb-core and extenddb-engine reports no warning on a line this PR
adds.

* test(query): rename the isrefused tests and reuse assert_validation

Nine test names in test_query_key_size.py read `isrefused`, and they now
read `is_refused`. `refused` in query_key_size_support.py repeated the
three asserts of helpers.assert_validation. It now calls that helper and
takes its `code` argument. In test_query_key_size_order.py, `_first` and
five tests made the same three asserts after `_error`, and they now call
`refused`. `_error` stays for the two syntax error tests, which check
the start of the message.

* docs(query): count one sort key value per attribute in the limits table

482bf8a counts the largest value of each key attribute once. The sort
key row of docs/dynamodb-limits.md said that the Query check adds up
every sort key clause. It now says that the check adds up the sort key
values of the key condition, one per attribute.

* fix(core): refuse a Query key N value that is not a number

a3f733d sized an N value that normalize_number does not read as a
number as 0 bytes, so the Query went on to storage. On SQLite at
1e5f4bc, `abc`, 2049 `x`, `" 1"` and the empty value answered HTTP
200 with Count 0. That was the answer on a GSI keyed on an N attribute,
in KeyConditions and on the base table's N partition key. On the base
table's N sort key they answered InternalServerError, and the server
log shows `invalid numeric sort key: invalid digit found in string`.

DynamoDB refuses each of these values with `The parameter cannot be
converted to a numeric value: ` followed by the value as sent. The
empty value gets the colon too (measured 2026-11-24 in us-east-1, twice
each, in KeyConditionExpression and in KeyConditions). sized_clause now
refuses such a value with that text when the value holds ASCII
characters only. NumberFault::Empty has no colon, so the refusal uses
the text of NumberFault::Malformed.

A value with a character outside ASCII still adds nothing to the size.
The service reads Unicode decimal digits as digits, and the number rules
do not (ExtendDB#565). A refusal here would refuse one U+FF11, which the service
reads as the number 1.

The unit table now expects the sentence for 2049 `x`. It gains rows for
`abc`, `" 1"`, the empty value, and `1e-131` as a partition key value
and as a sort key value.

* test(query): pin the number sentences in each Query key position

test_query_key_size_values.py gains a table for N values that the
service refuses as numbers: `abc`, 2049 `x`, `" 1"`, the empty value and
`1e-131`. Each row runs in four forms. Two are on a GSI keyed on an N
attribute, in KeyConditionExpression and in KeyConditions. The other two
use the N partition key and the N sort key of a new `number_table`
fixture. Each row asserts status, code and message by equality. The
service gives these answers before the table lookup (measured
2026-11-24 in us-east-1, twice each, on a table that does not exist).

Two more rows, 39 and 683 U+FF11, assert the service's 38-digit
sentence. They are expected failures on ExtendDB, because the number
rules do not read U+FF11 as a digit (ExtendDB#565). The mark is not strict, so
a check that reads these digits makes the rows of its form pass.

The existing number test now builds its Query with the same helper.

On a server built from 1e5f4bc, the 16 rows for `abc`, 2049 `x`,
`" 1"` and the empty value fail. 12 of them get HTTP 200, and the 4 on
the sort key get InternalServerError. The 4 underflow rows pass there
too. With 1fe3f0c, the three wire files pass on SQLite: 99 passed and
8 expected failures.

* test(query): leave the U+FF11 rows to ExtendDB#565

The files in tests/python hold no expected failures (tests/python/README.md,
Design Principles). The rows for 39 and 683 U+FF11 assert the service's
38-digit sentence. ExtendDB gives that sentence only once its number rules
read Unicode decimal digits, which is ExtendDB#565, so the rows and their mark go.
The measurements of 2026-11-24 are on ExtendDB#565.

* test(query): pin the Select order on a KEYS_ONLY GSI without a start key

On a KEYS_ONLY GSI with Select ALL_ATTRIBUTES and no ExclusiveStartKey,
DynamoDB answers each of these refusals before the Select sentence:

- Limit 0
- a key condition on the base table's partition key
- an N value on the S key
- an unused expression value
- an empty KeyConditionExpression
- FilterExpression `#`

A reviewer measured each shape twice on 2026-11-24.

These shapes are now rows of
test_select_all_attributes_on_a_keys_only_gsi_is_reported_after. Each
row asserts status, code and message by equality, except the `#` row.
ExtendDB words that syntax error in its own way (ExtendDB#1419), so the row
checks the `Invalid FilterExpression: ` label. The table now sends its
requests through raw_client, because the SDK refuses Limit 0.
FILTER_LABEL, LIMIT_MESSAGE and MISSED_G_MESSAGE hold the three texts
that a new row shares with an existing test.

With the Select check moved above the Limit check, the six new rows
fail. The two 2049-byte rows of the table and two start key rows in
test_query_start_key_partition.py fail too. On SQLite, the three wire
files and test_query_start_key_partition.py give 136 passed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Needed for the 1.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query accepts a KeyConditionExpression with two conditions on the partition key

4 participants