Skip to content

fix(core): check the key schema definitions of CreateTable and UpdateTable - #595

Open
LeeroyHannigan wants to merge 16 commits into
fix/517-519-520-stream-update-table-sentencesfrom
fix/index-key-definition-sentences
Open

LeeroyHannigan wants to merge 16 commits into
fix/517-519-520-stream-update-table-sentencesfrom
fix/index-key-definition-sentences

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

  • 19eb1187d merges fix(streams/update-table): CreationRequestDateTime, and measured UpdateTable sentences and validation order #548's head f3c65cd56, which has main at a417b2f82 merged in. crates/engine/src/gsi_updates.rs keeps this branch's calls into core, and the test of validate_gsi_updates takes fix(streams/update-table): CreationRequestDateTime, and measured UpdateTable sentences and validation order #548's data table with this branch's rows.
  • 2d0ddcd62 checks the key definitions in the service's order on CreateTable and on an UpdateTable GSI Create (threads on gsi_updates.rs:256 and validation/mod.rs:118). A HASH and a RANGE on one name get the service's sentence.
  • cb0b9c05e lists the keys and the definitions in the service's order (thread on key_definitions.rs:69).
  • 94721d8c1 adds rows for [a, b, b, a] and [a, b, a, b] (thread on test_key_schema_definitions.py:139).
  • d23aac188 restores the tables that main created with a name defined twice or with more index keys than definitions (the review on backup/restore.rs:287).
  • 0efed5521 makes the redefinition test wait for its index before the table is deleted, also when it fails (thread on test_key_schema_definitions.py:204).
  • 289fde9f8 keeps the HASH and RANGE check out of a multi-part table key schema, as on main, so a restore of such a table still passes.
  • e770ef3d8 states the bucket resize rule and the multi-part guard in the comments without naming a branch.
  • fb4ea9daa moves the Java HashMap hash of a name into spread_hash in core. listed and the PartiQL echo's bucket, which computed the same hash, both call it.
  • fc2186a0e folds the UpdateTable and CreateTable rows of tests/test_key_schema_definitions.py into two data tables, UPDATE_TABLE and CREATE_TABLE. The four billing rows keep their own test. Every row now goes to a table name that does not exist and asserts the status 400. ks() repeated keys(), so its 48 calls now use keys().
  • 1619dfe40 merges fix(streams/update-table): CreationRequestDateTime, and measured UpdateTable sentences and validation order #548's head 7fbb1bd2a, which changes only tests/python/test_update_table_sentences.py.

What

Stacked on #548, which owns the UpdateTable sentences this changes.

CreateTable and an UpdateTable GSI Create now check AttributeDefinitions against each key schema with the service's rules, order and sentences. They refuse a name defined twice.

  • crates/core/src/validation/key_definitions.rs (new, 312 lines): the four rules below, as validate_definition_count, validate_hash_and_range_differ, validate_unique_attribute_definitions and validate_keys_defined. listed renders a list of names in the service's order. It does not model a bucket that holds eight or more names and takes another while the table has 64 or more buckets. There HashMap builds a tree. That case needs at least 11 names. spread_hash computes the Java HashMap hash of a name for listed. Unit tests.
  • crates/core/src/validation/mod.rs: validate_create_table runs the rules in the service's order. validate_recorded_table runs the same checks for a restore, without the two rules main did not have. The check of a table HASH and RANGE on one name moves to rule 2, so the last copy of the Invalid KeySchema sentence outside key_definitions.rs goes. As on main, that check skips a multi-part table key schema (allow_multipart_table_keys). The unused format_attr_defs goes too.
  • crates/engine/src/gsi_updates.rs: the GSI Create branch of validate_gsi_updates keeps fix(streams/update-table): CreationRequestDateTime, and measured UpdateTable sentences and validation order #548's sentence for an absent member, then runs the four rules in order.
  • crates/engine/src/backup.rs and crates/engine/src/backup/restore.rs: both restore paths call validate_recorded_table.
  • crates/engine/src/partiql/request_checks/echo.rs: bucket calls spread_hash in place of its own copy of the hash.

validation/mod.rs goes from 4,866 to 4,964 lines, and gsi_updates.rs from 642 to 628. No file under docs/ describes these checks.

Why

Fixes #554. Fixes #555.

What the service answers

Measured in us-east-1 on 2026-10-25, 2026-10-26 and 2026-11-25, each request twice with the same answer. The requests named tables that do not exist, or had shapes that the service refuses. Only #555's first row named an existing table, and the service accepted it.

The service checks a key schema against the definitions with four rules, in this order:

  1. More keys than there are definitions: Invalid KeySchema: Some index key attribute have no definition.
  2. A HASH and a RANGE element on one name: Both the Hash Key and the Range Key element in the KeySchema have the same name.
  3. A name defined twice: Attribute Name is duplicated: <name>, naming the name whose second definition comes first. [a, b, b, a] names b, and [a, b, a, b] names a.
  4. A key with no definition: One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions. Keys: [...], AttributeDefinitions: [...], with every key of the schema and every definition.

An UpdateTable GSI Create runs the four rules in this order, after the sentence for an absent AttributeDefinitions.

On CreateTable the service checks the index names, then the table billing rules, then rules 1 to 3 on the table's key schema. The keySchema length constraint comes before rule 1. Then the service takes the table's key schema, each LSI's and each GSI's in turn, and checks an index's definitions before its shape. This branch checks rules 1 to 3 on the table's key schema in the service's place, before every shape check. It checks rule 4, and rule 1 on each index, after every shape (#1493).

The two lists come out in the iteration order of a Java HashMap that starts with 4 buckets and load factor 0.75. A name's bucket comes from its Java String.hashCode, spread as h ^ (h >>> 16). Within a bucket the names keep their request order. The table also doubles when a bucket that holds eight or more names takes another while the table has fewer than 64 buckets. Before the requests went out, the model predicted the 14 answers that this round measured and the 5 that the review measured. The service answered each one as predicted, twice.

On UpdateTable the service accepts a definition that gives the table's own key another type beside a valid Create. The table keeps its type (#555's first row).

Changes against main

These requests get another answer than on main, the service's:

  • a name defined twice in CreateTable or in an UpdateTable GSI Create: the duplicate sentence (main accepted it, or answered another check)
  • a key schema with more keys than there are definitions: Invalid KeySchema. Main listed the first missing key. It answered the shape sentence when a key schema also had a shape fault, and it accepted the schema when its keys repeat a name.
  • a missing key: the listing with every key of the schema, both lists in the service's order (main named only the first missing key)
  • a table billing fault beside a key schema shape fault, a key with no definition or an unused definition: the billing sentence. Main checked the billing rules after the shapes and the definitions, so it answered the other fault (for example First KeySchemaElement is not a HASH type, Second KeySchemaElement is not a RANGE type, Index KeySchema: The first KeySchemaElement is not a HASH type, or the listing).
  • fewer definitions than the table has keys, beside a shape fault in any key schema: Invalid KeySchema (main: the shape sentence)
  • a table HASH and RANGE on one name, with two definitions: the rule 2 sentence (main: Invalid KeySchema)
  • three table keys, RANGE first: the keySchema length constraint (main: the First KeySchemaElement sentence)
  • an UpdateTable GSI Create with a HASH and a RANGE on one name: the rule 2 sentence (main did not refuse it in validation)

An index name fault now comes before the billing rules, the four rules and the key schema shapes, as the measured order puts it. Main checked each index's name with its key schema shape.

RestoreTableFromBackup still restores every backup that main restores. Main could create a table with a name defined twice, or with more index keys than definitions. A backup of such a table keeps its recorded definitions and keys.

Related behaviour

These differences from the service exist on #548's head and on main as well:

Testing done

tests/test_key_schema_definitions.py has 52 dual-target wire tests from the measurements: the data tables UPDATE_TABLE (20 rows) and CREATE_TABLE (27 rows), four billing rows and the redefinition test. Each of the 51 rows compares the status, the error code and the whole message by equality. Each sends its request to a table name that does not exist.

Results at the head 1619dfe40, on a SQLite build of it:

  • The new file: 52 passed. On a SQLite build of fix(streams/update-table): CreationRequestDateTime, and measured UpdateTable sentences and validation order #548's head 7fbb1bd2a: 41 failed, 11 passed. On the merge 19eb1187d, before this round's fixes: 26 failed, 26 passed.
  • The new file with 15 related files (tests/test_multipart_gsi.py, tests/test_table_operations.py, tests/test_update_table_billing_validation.py, tests/test_backup_restore_fidelity.py, tests/test_gsi_attribute_definitions.py, tests/test_index_key_validation_on_update.py, and test_gsi.py, test_tables.py and the seven files that fix(streams/update-table): CreationRequestDateTime, and measured UpdateTable sentences and validation order #548 names under tests/python): 324 passed, 4 xfailed.
  • With the duplicate check changed to name the first defined name that repeats, the two [a, b, b, a] rows and the_first_repeated_name_is_named fail. The two [a, b, a, b] rows pass under both rules.
  • a_backup_that_records_a_name_defined_twice_restores and a_backup_that_records_more_index_keys_than_definitions_restores restore on both paths. At 1ad8ac04e they fail with the duplicate and the Invalid KeySchema sentences.
  • cargo test --lib: core 1172, engine 895 and storage 291 passed. a_multipart_table_hash_and_range_on_one_name_restores fails without the multi-part exception.

Gates on core, engine and storage at the head: cargo +1.97.0 fmt --all -- --check, both clippy commands of .github/workflows/clippy.yml with -D warnings, and cargo +1.91.1 check --all-targets are clean. cargo clippy -- -W clippy::pedantic reports no warning on a line this PR adds. The native run of the WebAssembly parity test (cargo test -p extenddb-wasm-adapter --test parity) passes. python3 -m pytest --collect-only -q collects tests/ and tests/python with no error.

The redefinition test creates a table, so this revision was not run against the service.

CI runs the new file in run-integration, run-integration-sqlite, mongodb-pytest and cassandra-pytest.

Merging

This is based on #548's branch. After #548 merges, retarget it to main.

git merge-tree --write-tree of the head merges cleanly with main. It conflicts with five open PRs:

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

CreateTable and an UpdateTable GSI Create now refuse a name defined twice in AttributeDefinitions, and a key schema with more keys than definitions, as the service does. Main accepted the first. It accepted the second only when the keys repeat a name, and otherwise refused it with another sentence. A table that main created with such definitions keeps working, and its backups restore.


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.

CreateTable and an UpdateTable GSI Create refused an undefined key attribute
with one sentence that named only the first missing key. Amazon DynamoDB
(measured 2026-10-25 and 2026-10-26 at 01:30 and 01:36 UTC, each request
twice) applies these rules:

- A name defined twice in AttributeDefinitions answers `Attribute Name is
  duplicated: <name>`, naming the first name whose second definition comes
  first, before any key check. ExtendDB accepted duplicates.
- The table's key schema is checked first, then each LSI's, then each GSI's.
  A schema with more keys than there are definitions answers `Invalid
  KeySchema: Some index key attribute have no definition`; otherwise a key
  with no definition answers `Some index key attributes are not defined in
  AttributeDefinitions`, listing every key of that schema and every
  definition.

The two checks live in the new validation/key_definitions.rs and both
operations call them. The service orders the two lists in a way that follows
neither the request nor the alphabet; ExtendDB lists them in request order.
The UpdateTable unit test row that expected the listing sentence for one
definition and two keys now expects the key schema sentence, as measured.
Dual-target wire tests from the 2026-10-25 and 2026-10-26 measurements:
UpdateTable GSI Create with a missing hash or range key, both keys, fewer
definitions than keys and names defined twice; CreateTable with missing keys
on the table, an LSI and GSIs in each order, fewer definitions than keys and
names defined twice; and an UpdateTable that redefines the table's own key
with another type, which keeps the stored type. The lists in the sentence are
compared as sets, since the service's order is not the request's.
…order documented

Amazon DynamoDB reports a CreateTable billing fault (throughput with
PAY_PER_REQUEST, or none with PROVISIONED) before a duplicated or missing
definition (measured 2026-10-26 at 02:27 and 02:34 UTC, each case twice).
The billing check now runs before the definition checks.

The duplicate check compared every definition with every earlier one, and
UpdateTable runs it once per GSI Create: 8,000 definitions with 200 Creates
took 22.9 s. It now makes one pass with a set of seen names and still names
the first name whose second definition comes first.

The module comment states where the service checks a duplicate (after the
billing checks, the one-name-twice rule and the length rule) and where
ExtendDB does (after the key schema shape checks).
docs/differences-from-dynamodb.md records the order of the two lists in the
listing sentence.
CreateTable with PAY_PER_REQUEST and throughput beside a duplicated name, a
missing key, or fewer definitions than keys answers the billing sentence
(measured 2026-10-26 at 02:27 and 02:34 UTC). The redefinition test waits
for its new index to be active before the table is deleted, since the
service refuses DeleteTable while an index is being created, and writes an
item with the stored key type. The measurement times in the docstring are
corrected to 01:27 and 01:29 UTC, plus the two later runs.
@LeeroyHannigan
LeeroyHannigan force-pushed the fix/index-key-definition-sentences branch from ea5045e to 2bb47d6 Compare October 31, 2026 22:28
…y-definition-sentences

crates/core/src/validation/mod.rs keeps the restorable item exports from the base branch and the key definition exports from this branch.

crates/engine/src/update_table.rs takes the base branch, which moved validate_gsi_updates into gsi_updates.rs and put stream_change_companion in its place.

crates/engine/src/update_table_tests.rs takes the base branch, which moved the undefined key attribute test into gsi_updates.rs and put the stream companion test in its place.

crates/engine/src/gsi_updates.rs carries this branch's GSI Create change to the moved function. It keeps the absent member sentence from the base branch, then calls validate_unique_attribute_definitions and validate_key_schema_defined, with this branch's test rows.

docs/differences-from-dynamodb.md keeps the U+0000 and stream rows from the base branch and the list order row from this branch.

@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 fewer-definitions rule now matches the service. When AttributeDefinitions has fewer entries than a created index has keys, this head answers Invalid KeySchema: Some index key attribute have no definition on CreateTable and on an UpdateTable GSI Create, as the service does in us-east-1, twice each, where main gives the listing sentence. A name defined twice and an index key that repeats a name are refused with the service's text, and a duplicate in UpdateTable without a Create is accepted, as on the service. Authorization still runs first: a user allowed only DescribeTable gets AccessDenied on every new refusal. The checks run before the table lookup, so a duplicate in CreateTable on an existing name answers ValidationException, as on the service. This head opens main's SQLite file and PostgreSQL catalog at migration 12 and serves tables with duplicate definitions normally.

The new file passes 31 tests twice on SQLite and on PostgreSQL and fails 27 on the 548 head, the related suites pass, fmt and both clippy commands are clean on 1.97.0, the code builds on the 1.88 MSRV, core (1141) and engine (870) pass, and CI is green on 59 checks.

I will approve once a backup that main restores restores here, and the check order and list order follow the service, with tests that hold them.

crates/engine/src/backup/restore.rs:287

RestoreTableFromBackup now refuses backups that main restores. This call, and the one at backup.rs:1862, run validate_create_table, which now includes the duplicate check (validation/mod.rs:973) and the length rule (key_definitions.rs:60). Main accepts a name defined twice in CreateTable and stores both definitions, and ImportTable still does at this head (import_export.rs:83).

  • SQLite: a table main created with definitions [pk S, g N, g S] and an index on g, backed up on main, answers Attribute Name is duplicated: g on every restore under this head on the same file. Main restores it twice.
  • PostgreSQL: three such tables restore 6 of 6 on main and 0 of 6 here. An index key [g HASH, g HASH, g RANGE] restores 2 of 2 on main and is refused 2 of 2 with Invalid KeySchema.
  • On this head alone, ImportTable with [pk, g, g] completes, the backup is AVAILABLE, and its restore is refused.

The comment at backup.rs:1858 says any table CreateTable or UpdateTable accepted restores. Keeping the two new checks out of the restore paths, or keeping the first of each repeated name in restore_input and restore_input_from_manifest (backup_definition.rs:393 and :615), with a restore test for this table, would keep these backups restorable.

)));
}
};
extenddb_core::validation::validate_unique_attribute_definitions(defs)?;

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.

On an UpdateTable GSI Create the duplicate check runs before two checks the service runs first, as the comment at key_definitions.rs:9 describes. Twice each:

  • Definitions [g, g] with 3 keys: the service answers Invalid KeySchema: Some index key attribute have no definition, and this head answers Attribute Name is duplicated: g. The same holds for keys a, b, c with [a, a].
  • Definitions [g, g] with keys g HASH, g RANGE: the service answers Both the Hash Key and the Range Key element in the KeySchema have the same name, and this head answers the duplicate sentence.

Running the length rule and the same-name rule ahead of validate_unique_attribute_definitions here would match. No test uses a key schema longer than the definitions list with a duplicate.

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 2d0ddcd. The GSI Create now checks the definition count, then the HASH and RANGE same-name rule with the service's sentence, then the duplicate, then the listing. [g, g] with three keys and [a, a] with keys a, b and c answer Invalid KeySchema, and [g, g] with g HASH, g RANGE answers the same-name sentence.

These shapes are rows of UPDATE_TABLE in tests/test_key_schema_definitions.py, sent to a table name that does not exist. The same-name check replaces CreateTable's check of a table HASH and RANGE on one name, so both paths use one function.

@@ -116,8 +118,10 @@ pub fn validate_create_table(
validate_key_schema(input, limits.allow_multipart_table_keys)?;

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.

On CreateTable the duplicate check runs after the key shape checks, and the service runs it first. With a duplicate in the definitions, twice each, the service answers Attribute Name is duplicated and this head answers the shape sentence for: RANGE first on the table, two HASH on the table, RANGE first on a GSI, and RANGE first on an LSI. The measured order is the billing rules, the table length rule, the table HASH and RANGE rule, the duplicate check, then each schema in turn with its definition checks before its shape. The comment at key_definitions.rs:12 discloses the difference.

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 2d0ddcd. CreateTable now checks the billing rules, then the definition count and the same-name rule on the table's key schema, then the duplicate check on AttributeDefinitions. The shapes come after them. The four shapes from this thread answer the duplicate sentence, as on the service, in the CREATE_TABLE rows of tests/test_key_schema_definitions.py.

Each index's definitions still come after every shape, as on main. A GSI keyed r RANGE, g HASH with [pk, g] answers the shape sentence here and the listing on the service, which is #1493.

.iter()
.all(|k| defs.iter().any(|d| d.attribute_name == k.attribute_name))
{
return Ok(());

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 of the two lists can be matched. It is the iteration order of a Java HashMap with 4 starting buckets and load factor 0.75, keyed by String.hashCode spread as h ^ (h >>> 16), with insertion order within a bucket. All 66 lists in 33 answers from the service fit it, as do the 5 examples in #554, and 8 new lists predicted before sending (13 names, mixed case, accented names and an emoji) matched the service twice each. This head's order differs in 22 of the 33, for example the service's Keys: [sk, pk], AttributeDefinitions: [x, pk] against [pk, sk] and [pk, x] here. Rendering both lists in that order would let docs/differences-from-dynamodb.md:60 go, and the tests that compare the lists as sets could compare the message.

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 cb0b9c0. listed in key_definitions.rs renders both lists in that order. It has one more rule: the table also doubles when a bucket that holds eight or more names takes another, below 64 buckets. Nine and eight names in one bucket of 16 settle that rule on the service. listed does not model a bucket that holds eight or more names and takes another while the table has 64 or more buckets. There HashMap builds a tree. That case needs at least 11 names.

Before the requests went out, the model predicted the 14 answers of this round, and the service answered each one as predicted, twice. The wire rows now compare the whole message, with a new row for [pk, x], and the docs/differences-from-dynamodb.md row is gone.

Comment thread tests/test_key_schema_definitions.py Outdated
assert message == f"Attribute Name is duplicated: {name}"


def _wait_for_index(client, table, index, timeout=120.0):

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.

test_update_table_keeps_the_type_of_a_redefined_table_key fails on the service in 2 of 2 runs: _wait_for_index raises TimeoutError ... not ACTIVE after 120.0 s, the cleanup DeleteTable answers ResourceInUseException, and the table is left behind. The indexes on these empty tables took about 16 and about 9 minutes to go ACTIVE. The redefinition assertion passed both times. A longer wait, or a cleanup that waits for the index before deleting, would let the file run on the service.

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 0efed55. _wait_for_index waits up to 30 minutes and treats an absent index as done. The test calls it in a finally block, so scoped_table deletes the table only after the index is ACTIVE, also when an assertion fails.

The test creates a table, so I did not run this revision against the service. On ExtendDB the index is ACTIVE within seconds, and the file passes on SQLite.

Comment thread tests/test_key_schema_definitions.py Outdated
@pytest.mark.parametrize(("spec", "h", "r", "name"), [
("h:S h:S", "h", None, "h"),
("h:S h:S", "h", "r", "h"),
("b:S a:S b:S a:S h:S", "h", "r", "b"),

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 only ordered duplicate case is [b, a, b, a], and "name the first defined name that repeats" gives the same answer for it: with the code changed to that rule, the 31 wire tests and the unit tests pass. On the service [a, b, b, a] answers b and [a, b, a, b] answers a, and this head matches both; a row for [a, b, b, a] would pin the rule.

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.

Added in 94721d8. [a, b, b, a] and [a, b, a, b] are now rows of UPDATE_TABLE and of CREATE_TABLE in tests/test_key_schema_definitions.py, and both lists are in the_first_repeated_name_is_named. The service answers b and a for them on CreateTable and on an UpdateTable GSI Create.

With the check changed to name the first defined name that repeats, the unit test and the two [a, b, b, a] rows fail.

…y-definition-sentences

The base branch now has main at a417b2f merged in, and its fix commits for the review of 24 November.

crates/engine/src/gsi_updates.rs keeps this branch's GSI Create check, which calls validate_unique_attribute_definitions and validate_key_schema_defined in core. That second call already holds the base branch's rule. Fewer definitions than the index has keys answer Invalid KeySchema, and otherwise the sentence lists every key of the index. The test a_created_gsi_on_an_undefined_key_attribute_is_refused_by_member_shape takes the base branch's data table and adds this branch's two rows, [pk, g] and [g, g].
The service checks a key schema against AttributeDefinitions in this order (measured 2026-11-25, twice each, on table names that do not exist):

1. More keys than there are definitions: "Invalid KeySchema: Some index key attribute have no definition".
2. A HASH and a RANGE element on one name: "Both the Hash Key and the Range Key element in the KeySchema have the same name".
3. A name defined twice: "Attribute Name is duplicated: <name>".
4. A key with no definition: the sentence that lists the keys and the definitions.

On an UpdateTable GSI Create this branch checked a name defined twice first, and it had no rule 2. The Create now runs the four rules in this order, after the sentence for an absent AttributeDefinitions. So [g, g] with keys g, x and y answers Invalid KeySchema. With keys g HASH and g RANGE it answers the rule 2 sentence.

On CreateTable the service checks the index names, then the table billing rules, then rules 1 to 3 on the table's key schema. All of them come before any key schema shape check. The keySchema length constraint comes before rule 1 and the shape checks. CreateTable now runs them in that order, after the length constraint, which stays first as on main. After the shape checks it runs rule 4 on the table's key schema, then rules 1 and 4 on each index's. A name defined twice beside a shape fault now answers the duplicate sentence. These requests now get the service's answer, where main gave another:

- a billing fault beside a table key schema that starts with RANGE: the billing sentence (main: "First KeySchemaElement is not a HASH type")
- one definition for two table keys, RANGE first: Invalid KeySchema (main: the same First KeySchemaElement sentence)
- a table HASH and RANGE on one name, with two definitions: the rule 2 sentence (main: Invalid KeySchema)
- three table keys, RANGE first: the keySchema length constraint (main: the First KeySchemaElement sentence)

Main checked each index's name with its key schema shape, after the table's key schema shape.

validate_key_schema_defined splits into validate_definition_count (rule 1) and validate_keys_defined (rule 4). The new validate_hash_and_range_differ (rule 2) replaces the check of a table HASH and RANGE on one name in validate_key_schema, so both paths use one check. That check was the last production copy of the Invalid KeySchema sentence outside key_definitions.rs, where the sentence is now one constant.

The measured requests are rows of test_update_table_checks_in_the_service_order and test_create_table_checks_in_the_service_order in tests/test_key_schema_definitions.py, sent to a table name that does not exist.
The service lists the names in "Keys: [..], AttributeDefinitions: [..]" in the iteration order of a java.util.HashMap that starts with 4 buckets and load factor 0.75. A name's bucket comes from its Java String.hashCode over UTF-16 code units, spread as h ^ (h >>> 16). Within a bucket the names keep their request order. The table also doubles when a bucket that holds eight names takes a ninth while the table has fewer than 64 buckets. This branch listed the keys in key schema order and the definitions in request order. Take keys g and r with the definitions [pk, x]. This branch answered "Keys: [g, r], AttributeDefinitions: [pk, x]", and the service answers "Keys: [r, g], AttributeDefinitions: [x, pk]".

validate_keys_defined now renders both lists through listed(), which models that order. Before the requests went out, the model predicted every list that the wire and unit rows assert. The service answered all 28 requests as predicted (2026-11-25, each list twice). Two of the lists put nine and eight names in one bucket of a 16-bucket table. The ninth name doubles the table, and the eighth does not. Its doc names the case it does not model: a ninth name in one bucket when the table has 64 buckets or more.

The rows of tests/test_key_schema_definitions.py now compare the whole message, in place of the two lists as sets. A new row sends [pk, x]. The row in docs/differences-from-dynamodb.md that described the order difference goes.
The only ordered duplicate row was [b, a, b, a], and two rules give b for it. The service names the name whose second definition comes first. The other rule names the first defined name that repeats. [a, b, b, a] tells them apart. The service answers b for it and a for [a, b, a, b] (measured 2026-11-25, twice each, on CreateTable and on an UpdateTable GSI Create).

Both lists are now rows of the two duplicate tables in tests/test_key_schema_definitions.py and of the_first_repeated_name_is_named. With the check changed to name the first defined name that repeats, the unit test and the two [a, b, b, a] wire rows fail. The two [a, b, a, b] rows pass under both rules.
Both restore paths ran validate_create_table on the input they build from the backup. This branch added two rules to it: a name defined twice in AttributeDefinitions, and a key schema with more keys than there are definitions. Main's CreateTable accepted both shapes and stored them. So a backup of such a table restored on main, and this branch refused it:

- definitions [pk S, gpk N, gpk S] with an index on gpk: "Attribute Name is duplicated: gpk"
- index keys [gpk HASH, gpk HASH, gpk RANGE] with the definitions [pk S, gpk S]: "Invalid KeySchema: Some index key attribute have no definition"

validate_recorded_table is a second entry point to the same checks. It runs every check of validate_create_table except those two rules. It keeps the rule main applied, that every key of each key schema has a definition. RestoreTableFromBackup calls it on the legacy path (backup.rs) and on the manifest path (backup/restore.rs). The comment on the legacy call now names the two rules.

Two engine tests restore each table on both paths and check that the restore keeps the recorded definitions and keys: a_backup_that_records_a_name_defined_twice_restores and a_backup_that_records_more_index_keys_than_definitions_restores. At 1ad8ac0 both fail with the two sentences above.
On the service, test_update_table_keeps_the_type_of_a_redefined_table_key failed in 2 of 2 runs and left its table behind. The index on the empty table took about 9 and about 16 minutes to go ACTIVE, and _wait_for_index stopped after 120 seconds. scoped_table then called DeleteTable, which the service refuses while an index is being created.

_wait_for_index now waits up to 30 minutes and treats an absent index as done. The test calls it in a finally block, so scoped_table deletes the table only after the index is ACTIVE, also when an assertion fails. The PutItem now runs before that wait. On ExtendDB the index is ACTIVE within seconds, so the local run time does not change. This revision was not run against the service, because the test creates a table.
Main checked a table HASH and RANGE on one name only outside the multi-part table key preview (allow_multipart_table_keys). With the preview on, main created a table keyed a HASH and a RANGE with the one definition [a S]. The same-name rule of this branch ran in both modes. So the restore of such a table failed with "Both the Hash Key and the Range Key element in the KeySchema have the same name".

The rule now runs on the table's key schema only outside the preview, where main ran its check. A new core test restores that table, and CreateTable still refuses it by the definition count. Without the guard, the test fails with the same-name sentence.
…a branch name

The doc of listed() said that the table doubles when a bucket that holds eight names takes a ninth. The code, like HashMap.putVal, acts on a bucket that holds eight or more names when it takes another. A bucket of nine that takes a tenth also doubles the table below 64 buckets. At 64 buckets or more it becomes a tree. The doc now says so in both places.

Two comments in crates/core/src/validation/mod.rs described what "main" checked and accepted. Once this branch merges, main is this code. They now say what earlier releases did.
listed() in crates/core/src/validation/key_definitions.rs and bucket() in crates/engine/src/partiql/request_checks/echo.rs each computed the same value. It is Java's String.hashCode over the UTF-16 code units of a name, spread as h ^ (h >>> 16). The first orders the names in the "Keys: [..], AttributeDefinitions: [..]" sentence, the second the members of a PartiQL statement echo.

spread_hash() in key_definitions.rs, exported from extenddb_core::validation, now computes it for both. The echo tests cover bucket(), and lists_follow_the_measured_order covers listed().
…t does not exist

tests/test_key_schema_definitions.py had eight parametrized tests over two request shapes, an UpdateTable GSI Create and a CreateTable. Three of them, the UpdateTable listing, definition count and duplicate rows, created a table through a module fixture, also on the service. Their measurements went to a table name that does not exist. The service answers every refusal in the file before it looks the table up.

The 47 rows of seven of these tests are now two tables, UPDATE_TABLE and CREATE_TABLE, each with one parametrized test. Every row has its full expected message and goes to NEVER_CREATED. The fixture goes. The four billing rows keep their test, which sends ProvisionedThroughput. Each row keeps its request and its expected message.

_message now also asserts the status 400.

ks() repeated keys() for a HASH key and an optional RANGE key. It goes, and its 48 calls now use keys().
…y-definition-sentences

The base branch's new commit folds the never-created rows of tests/python/test_update_table_sentences.py into one data table. This branch does not change that file, so the merge brings only that commit.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

d23aac1 fixes the restore. Both restore paths now run validate_recorded_table, which skips the two new rules, and two engine tests restore your two tables on both paths. The check order is in 2d0ddcd and the list order in cb0b9c0, with rows that compare the whole message.

ImportTable still accepts a name defined twice, which is #771. A backup of such a table now restores.

@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.

A backup that main restores restores here at 1619dfe, and the check order follows the service; two rules still differ from it.

  • On SQLite, this head restores the four backups main took of tables with repeated definitions, index keys [g HASH, g HASH, g RANGE] and an ImportTable table, 8 of 8, each ACTIVE with its definitions, keys, indexes and items; on PostgreSQL it restores main's six, 12 of 12, including [pk S, pk N] and a multi-part a HASH, a RANGE. Main restores this head's backups on both, and index overrides restore as on main. Reverting either restore path to validate_create_table, any recorded skip or the multi-part guard fails its test.
  • On UpdateTable, [a, a] and [g, g] with three keys answer Invalid KeySchema: Some index key attribute have no definition, [a, b, b, a] names b and [a, b, a, b] names a; on CreateTable the four round-1 shapes answer Attribute Name is duplicated, and 19 further combinations follow billing, the count, the same-name rule, the duplicate check and the shapes, each twice on the service in us-east-1 and on SQLite and PostgreSQL builds of this head. The 22 listing rows that differed now match, with 28 new lists. Moving each check fails its wire rows, and the redefinition file passes 52 of 52 on the service twice.
  • fmt, both clippy forms, the 1.91.1 check, core 1172, engine 895 and storage 291 tests are clean, and spread_hash buckets as the PartiQL echo copy did on 9.85 million pairs.

I will approve once the same-name rule holds in either order and the list order is sized as the service sizes it, or the comment and body say where the model stops.

crates/storage/src/backup_definition.rs:391

Optional. This comment, :608 and crates/engine/src/backup/overrides.rs:40 and :391 still name validate_create_table as the restore's check; both paths now call validate_recorded_table.

pub fn validate_hash_and_range_differ(
key_schema: &[KeySchemaElement],
) -> Result<(), DynamoDbError> {
if let [hash, range] = key_schema

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 gives this sentence for the two elements in either order. Twice each: UpdateTable GSI Create [g RANGE, g HASH] with definitions [g, g] answers it on the service and Attribute Name is duplicated: g here; with [g, x] the service refuses and this head creates the index; on a missing table the service gives the sentence and this head ResourceNotFoundException. CreateTable [pk RANGE, pk HASH] answers it on the service, and here Attribute Name is duplicated: pk with [pk, pk] and First KeySchemaElement is not a HASH type with [pk, x]. Base answers each of these as this head does. Matching the two names whatever the key types, with rows for the reversed order, would hold it.

fn listed<'a>(names: impl IntoIterator<Item = &'a str>) -> Vec<&'a str> {
let mut seen = HashSet::new();
let mut entries: Vec<(&str, u32)> = Vec::new();
let mut buckets: u32 = 4;

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's lists follow a java.util.HashMap sized for the names up front, the smallest power of two not below ceil(n / 0.75), not one that starts at 4 and doubles; checked against Java 17 it fits all 67 lists measured, the doubling model 62. The two agree until nine or more names share a bucket. Four lists of 20 and 21 names, with 9 in one bucket, predicted before sending, answer the presized order on the service, twice each, and a different one here; for example qaav qacx qaez qala qanc qape qarg qati qavk h00 h01 h02 h03 h04 h05 h06 h07 h09 h10 h11 lists [h10, h11, h01, h00, ...] on the service and [h10, h01, h00, h11, ...] here. The tree case is reached with 10 names in one bucket among 13, where the comment and body say at least 11. Starting buckets at the presized count matches every non-tree list measured; the tree bucket can be modelled or the stated limit corrected.

// (measured 2026-11-25).
validate_table_name(&input.table_name, limits)?;
validate_key_schema_size(input, limits.allow_multipart_table_keys)?;
for gsi in input.global_secondary_indexes.iter().flatten() {

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.

Optional. No test holds the index name check ahead of the billing rules: with the two loops moved after validate_provisioned_throughput, every unit test and all 52 wire rows pass, and nothing under tests/ asserts at 'indexName'.

@LeeroyHannigan LeeroyHannigan changed the title Key schema definition checks for CreateTable and UpdateTable fix(core): check the key schema definitions of CreateTable and UpdateTable Dec 2, 2026

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants