Skip to content

fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits - #133

Merged
kalaspuff merged 1 commit into
issues-stack/7-test-helpersfrom
issues-stack/8-memory-query-validation
Oct 31, 2026
Merged

kalaspuff merged 1 commit into
issues-stack/7-test-helpersfrom
issues-stack/8-memory-query-validation

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Requests DynamoDB refuses that the in-memory backend accepted, so tests passed where production fails (#93):

  • Empty key values in KeyConditionExpression: pk = :p with :p = "" returned nothing. It now raises DynamoDB's ValidationException, "One or more parameter values are not valid. The AttributeValue for a key attribute cannot contain an empty string value. Key: pk" (reported against AWS in Dynamodb query with a KeyConditionExpression should raise ClientError when the partition key is empty getmoto/moto#7062), and the binary form for B keys. This covers partition and sort key conditions, on tables and indexes.
  • ExclusiveStartKey of a query: only the table key was checked. An index start key with a number for a string key, or with an extra attribute, restarted at the first item, and start keys outside the key condition were accepted. The backend now follows what dynoxide-rs captured against DynamoDB (tests/query_start_key_and_key_conditions.rs):
    • A start key holds the table key and the index key, each of its type, and nothing else, or it is "The provided starting key is invalid". An index scan with an index key of the wrong type names the index, and empty table key values have their own message.
    • A start key outside the sort key condition "does not match the range key predicate".
    • A start key in another partition "is outside query boundaries based on provided conditions". With a sort key condition it is "at most one row and cannot be restarted", and on a table without a sort key "The provided exclusive start key is invalid".
  • Index key sizes: secondary index key values had no size limit (a 3,000-byte global index partition key was accepted). They now have the 2,048- and 1,024-byte limits of table keys, with the same messages (moto's test of a global index sort key gives that message).
  • TransactWriteItems size: only Put items counted towards the 4 MB limit, so eleven updates that store 390 KB items each were accepted. Updates now count with the item they store.

Verified with Rust tests of the memory backend for each rule, which fail without the fix. make check passes, including the Python suites on the memory backend and MiniStack, whose cursors and pagination still fit.

Refs #93: two size limits of the issue are left open, because no source gives DynamoDB's answer: the 400 KB limit on an item together with its local index entries, and a 4 MB limit for TransactGetItems. I described them on the issue.

Checklist

  • Tests cover the change (in-memory backend: these are its rules)
  • Docs updated (changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No: what the backend now refuses, DynamoDB refuses

Worth confirming

  • Assumption: index key values over the size limits get the same messages as table keys ("Size of hashkey has exceeded ...", "Aggregated size of all range keys has exceeded ..."). Only the sort key form has a source (a moto test); the partition key form is inferred from it.
  • Assumption: an empty value anywhere in a sort key condition, begins_with included, is refused like an empty partition key value. moto does so, and dynamoxy never sends an empty prefix itself.

fctl session


Note

Low risk. It only changes the in-memory test backend, making it refuse requests that DynamoDB refuses, so tests that relied on the old leniency may start failing; production code against AWS is untouched.

What it does. Makes the in-memory DynamoDB reject queries with empty string or binary key values, reject ExclusiveStartKey values that are malformed or outside the key condition, and enforce the 2,048- and 1,024-byte size limits on secondary index keys. TransactWriteItems now also counts the stored size of updated items towards its 4 MB limit. The error messages follow what DynamoDB returns, as captured by dynoxide-rs.

Stricter test backend. Application tests on the memory backend that used empty key values, hand-built start keys or oversized index keys will now fail with a ValidationError, as they would against AWS.

fctl review's first look at 5044d61 · 5 files, +369 −22 · Review agent settings

…he key condition, and index keys have size limits

The in-memory backend returned nothing for key conditions with empty
string or binary values, checked only the table key of a query's start
key (index keys of another type or extra attributes restarted at the
first item) and accepted start keys outside the key condition, all of
which DynamoDB refuses. Secondary index key values had no size limit,
and TransactWriteItems counted only Put items towards its 4 MB limit.

Refs #93

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-sh

fctl-sh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Stack of 10 pull requests, merged in this order with "Squash and merge":

  1. fix(models): numbers of union fields render as stored, and literals of plain enum members write #126 fix(models): numbers of union fields render as stored, and literals of plain enum members write
  2. fix(client): calls keep their middleware chain and deliver all their events #127 fix(client): calls keep their middleware chain and deliver all their events
  3. fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work #128 fix(pydantic): use_enum_values, forward references, RootModel fields and data-taking factories work
  4. fix(unique)!: upserts that race with creates, unique floats written back, and collections refused #129 fix(unique)!: upserts that race with creates, unique floats written back, and collections refused
  5. fix(transactions): commit() no longer raises after the transaction was applied #130 fix(transactions): commit() no longer raises after the transaction was applied
  6. fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows #131 fix(telemetry): records and metrics count PartiQL writes, throttles of failed calls, batch retries and rows
  7. fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB #132 fix(testing): purge_expired, inject_fault(table=) and replayed transactions behave like DynamoDB
  8. fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits #133 fix(memory): queries refuse empty key values and start keys outside the key condition, and index keys have size limits ← this pull request
  9. fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active #134 fix(binding): conversions take any mapping and refuse invalid wire values, and unknown table statuses are not active
  10. fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses #135 fix(memory): PartiQL and expressions follow DynamoDB's rules, and hand-written conditions get no redundant parentheses

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant