Skip to content

fix(core): refuse a key schema that names one attribute in two elements - #1576

Open
navareamol wants to merge 5 commits into
mainfrom
fix/1509-duplicate-key-attribute
Open

navareamol wants to merge 5 commits into
mainfrom
fix/1509-duplicate-key-attribute

Conversation

@navareamol

@navareamol navareamol commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

What

CreateTable and an UpdateTable GSI Create refuse a key schema that names one attribute in two elements. The answer is ValidationException The KeySchema contains multiple key attributes with the same name, with no One or more parameter values were invalid: prefix.

  • crates/core/src/validation/key_attribute_names.rs (new): validate_distinct_key_attributes and the sentence. A key schema of exactly two elements, one HASH then one RANGE, on one name is not refused here; the issue says that case has its own rule and sentence.
  • crates/core/src/validation/mod.rs: the new check runs inside CreateTable's existing sequence. No existing check moves.
  • crates/engine/src/gsi_updates.rs: the GSI Create branch runs the check after the empty key schema check and before the Keys: sentence.
  • crates/engine/src/backup.rs, crates/engine/src/backup/restore.rs: both restore paths call validate_recorded_table, which is validate_create_table without the new check. Main accepts a GSI such as [a HASH, b HASH, a RANGE] through UpdateTable, so a backup of such a table has to keep restoring. The comments at both call sites say which rule is skipped.

Where the check runs in CreateTable

In order, as the code runs on this branch (the new rule is marked):

  1. table name
  2. table key schema: empty, first element not HASH, and in a standard (not multi-part) table the length rule (length less than or equal to 2)
  3. table key schema: repeated name (new), then the second element not RANGE and the HASH and RANGE on one name sentence. On a multi-part table the new rule runs after the multi-part shape checks.
  4. each GSI's key schema, with its index name check
  5. each LSI's key schema, with its index name check
  6. each GSI's key schema: repeated name (new)
  7. definitions: the Keys: sentence, unused definitions
  8. the billing checks (table throughput, GSI throughput)
  9. index counts and the checks after them, as before

The billing checks, the index name checks and the shape checks are where main runs them. The new rule is placed before the first existing check that answers the issue's measured rows: the second-element check for a table key schema (row [pk HASH, pk HASH]), and the Keys: definition check for a GSI key schema.

Paths that take a key schema

Path Covered
CreateTable, table key schema yes
CreateTable, GSI key schema yes
UpdateTable GSI Create yes
CreateTable, LSI key schema no. validate_lsi_key_schemas already requires [HASH, RANGE] with the table's HASH name, so the only shape that repeats a name is the HASH and RANGE pair left alone here
RestoreTableFromBackup, both paths skipped on purpose (see above)
RestoreTableToPointInTime no key schema is validated: handle_restore_table_to_point_in_time in crates/engine/src/backup_requests/pitr.rs checks the target name, the source table and the time members, then returns pitr_restore_unavailable_error()
ImportTable no. create_table_input_from_params in crates/engine/src/import_export/files.rs builds the table without calling validate_create_table, so import accepts a repeated name. Open issue #771 already tracks ImportTable skipping validate_create_table.

How shapes the issue does not list are answered:

  • [a HASH, b HASH, a RANGE] and [a HASH, b RANGE, a RANGE] (three elements) are refused by the new rule on a GSI. On a standard table key schema the length rule answers first.
  • [a RANGE, a HASH] on a table or GSI in CreateTable gets the first-element sentence (First KeySchemaElement is not a HASH type, or the index form), because that check runs first. In an UpdateTable GSI Create, which has no shape check, it is refused with the new sentence.
  • [a HASH, a RANGE] is not touched by the new rule and gets the same answer as on main.

Why

Refs #1509.

The issue measured seven requests twice each in us-east-1 on 2026-11-25. On main they get the Keys: sentence, Second KeySchemaElement is not a RANGE type, or pass validation. The tests use those rows and the sentence from the issue.

The issue also says that on the service the rule comes after the billing checks and the definition count, and before the Attribute Name is duplicated check and the key schema shape checks. This change follows neither part of that order, so that no existing check moves: moving checks would change answers for requests the issue does not measure. The table rule runs inside validate_key_schema after the empty, first-element and length checks, and the GSI rule runs after the GSI and LSI shape and index-name checks. So [a RANGE, a HASH] on CreateTable gets the first-element sentence, not the new one. On CreateTable, a request with a billing fault and a repeated key name gets the new sentence, where the issue's order would give the billing sentence. On an UpdateTable GSI Create the throughput-minimum check already runs first, so billing wins there, as the issue says. Every measured row in the issue still gets the issue's sentence, with the attribute definitions the wire test sends for it. One difference a reviewer measured on 2026-12-04: CreateTable with [pk HASH, pk HASH] and only pk defined answers Invalid KeySchema: Some index key attribute have no definition on the service, and the new sentence only with a second definition (pk, x). Here it answers the new sentence in both cases, because the table rule runs before the definition checks. The wire test's table row now sends pk, x. The same holds for an UpdateTable GSI Create of [a HASH, b HASH, a RANGE] on a table keyed pk: with definitions a, b the service answers the missing-definition sentence, and with pk, a, b the new sentence (measured by a reviewer on 2026-12-04). Here both get the new sentence, because gsi_updates.rs runs the rule before the definition check. The wire test's hash_range_on_one_of_three row now sends pk, a, b.

Testing done

Branch fix/1509-duplicate-key-attribute, three commits on main at c05766036. Run on the dev desktop. The desktop tree and the Mac tree are both 2ba543d81. Merging origin/main (5826e1bea at the time of writing) with git merge-tree has no conflicts.

Check Result
cargo fmt --check clean
cargo +1.97.0 clippy -p extenddb-core -p extenddb-engine --all-targets -- -D warnings clean
core lib tests 1270 passed, 1 ignored
engine lib tests 983 passed
new unit tests (10 core, 1 engine gsi_updates, 1 engine restore), run 20 times; the whole gsi_updates module (11 tests) ran with them 20 of 20 runs passed
tests/test_key_schema_repeated_attribute.py (8 cases), SQLite, 3 runs 8 passed in each
same file, PostgreSQL (own container, removed afterwards), 1 run 8 passed
same file, Cassandra and MongoDB not run in this round; Cassandra passed 8 of 8 on an earlier head (ef5aff64a) before the check moved

New tests: the core tests for the rows and for main's order (length and first-element rules, the index name and GSI shape faults, GSI and table billing faults); the engine test for the UpdateTable rows and the reversed pair; and a_backup_whose_gsi_names_an_attribute_twice_restores_on_both_paths, which runs the legacy path (backup.rs) and the manifest path (backup/restore.rs) on a recorded GSI [a HASH, b HASH, a RANGE], requires both to accept it and to agree, and checks CreateTable refuses the same input.

Controls, each on the committed tree, reverted with git checkout -- crates, tree clean after each:

Control Change Tests that fail
A, check does nothing if true { return Ok(()); } at the top of validate_distinct_key_attributes 7 core tests; engine a_key_schema_naming_one_attribute_twice_is_refused_before_the_definitions_are_read and the restore test
B, UpdateTable call removed delete the call in gsi_updates.rs engine a_key_schema_naming_one_attribute_twice_is_refused_before_the_definitions_are_read
C, recorded table refuses too validate_recorded_table passes true core a_recorded_table_with_a_repeated_gsi_key_attribute_still_passes; engine restore test
D, GSI loop removed from CreateTable delete the GSI loop in validate_create_table_with core a_gsi_key_schema_naming_one_attribute_twice_gets_the_sentence, a_recorded_table_with_a_repeated_gsi_key_attribute_still_passes, the_billing_checks_keep_their_place_after_the_key_schema_checks; engine restore test
F, table key calls removed from CreateTable delete both table-key calls in validate_key_schema core a_table_key_schema_naming_pk_twice_gets_the_sentence, the_sentence_comes_before_the_key_definition_and_shape_checks
G, backup.rs call reverted validate_create_table in place of validate_recorded_table engine restore test
H, backup/restore.rs call reverted the same engine restore test

Not run: main's lib test counts, Cassandra and MongoDB on this head, any request against the live service.

To rerun the wire file: build cargo build -p extenddb --no-default-features --features sqlite, run extenddb init and serve, provision credentials with devtools/provision-test-credentials, set EXTENDDB_TEST_ENDPOINT and run pytest tests/test_key_schema_repeated_attribute.py.

Open question

None of these orders is measured on the service. Has anyone measured them?

  • Billing fault against the new rule (the issue states billing first; this change keeps main's billing placement, after the new rule).
  • Billing fault against the shape checks (main's order is kept).
  • A bad GSI index name beside a repeated key name (the index name check answers first here, as on main).
  • [pk HASH, pk HASH, pk RANGE] on a standard table (the length rule answers first here, as on main).
  • [a RANGE, a HASH] and [a HASH, b RANGE, a RANGE], and a repeated name in an LSI.
  • The definition count check that the issue puts before this rule is not on main for a general key schema, so it is not added here.

#595 changes the same four files (crates/core/src/validation/mod.rs, crates/engine/src/gsi_updates.rs, crates/engine/src/backup.rs, crates/engine/src/backup/restore.rs). It defines its own validate_recorded_table and its own order of these checks (index names, billing, then the key definition rules), and does not contain this sentence. The two will conflict. Whichever lands second should rebase; this rule goes before #595's duplicate definition rule. Question for #595's author: which order did you measure for index names, billing and the key schema shapes?

Amol Navare added 3 commits December 3, 2026 01:08
DynamoDB answers a key schema that names one attribute in two elements
with ValidationException 'The KeySchema contains multiple key attributes
with the same name', with no 'One or more parameter values were invalid:'
prefix. ExtendDB answered the definition listing, the second-element
sentence, or accepted the request.

CreateTable checks the table's and each GSI's key schema, and an
UpdateTable GSI Create checks its key schema, after the billing checks
and before the key definition and key schema shape checks. CreateTable
now answers its billing faults before the key schema shapes so that
order holds. A schema of one HASH and one RANGE element on one name is
left to its own rule. Restore from a backup skips the new check so a
global secondary index that UpdateTable accepted before still restores.

Refs #1509
…ave room

The rule for a table key schema now runs inside validate_key_schema, after
the empty, first-element and length checks and before the second-element
check. The rule for each GSI runs after every key schema shape check and
before the definition checks. The billing checks and the index name checks
stay where main runs them.

Restore tests cover both restore paths with a recorded GSI of
[a HASH, b HASH, a RANGE], and the tests that pinned the moved billing order
are replaced by tests that pin main's order.

Refs #1509

@LJMart11 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed at 81096d0. 1 finding: 1 Docs.

The change refuses a key schema that names one attribute in two elements on CreateTable (table and GSI key schemas) and on an UpdateTable GSI Create, and keeps both restore paths on a variant of the validator that skips the new rule. Confirmed: core lib tests (1270 passed, 1 ignored) and engine lib tests (983 passed) pass under 1.94.0, both on the head and on the head merged with the current main tip; the new wire file passes 8 of 8 on SQLite; removing the UpdateTable call makes the engine test a_key_schema_naming_one_attribute_twice_is_refused_before_the_definitions_are_read fail at gsi_updates.rs:561. I sent the GSI rows [g H, g H], [a H, b H, a R], [a H, b R, a R] and the table row [pk H, pk H] (with an extra definition) to DynamoDB in us-east-1: each answered the new sentence, as the head does.

  • Docs tests/test_key_schema_repeated_attribute.py:50 — The table row defines only pk, and DynamoDB answers Invalid KeySchema: Some index key attribute have no definition for it, not the new sentence the file's docstring says was measured.

… of messages

The table row of the wire test now sends an unused second definition,
which is what the service answers the new sentence for. The billing
order test's failure messages no longer print the answer.

Refs #1509
@navareamol
navareamol requested a review from LJMart11 December 4, 2026 08:53

@LJMart11 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checked at eee5fd8 against the list from 81096d0. 1 finding: 1 Docs. Chronology: 1 new-head delta.

The follow-up makes the wire test's table row send an unused second definition, documents why in the module docstring, and drops the answer from two assertion messages in the billing-order core test. Confirmed: the table row now matches DynamoDB (us-east-1 answered The KeySchema contains multiple key attributes with the same name for [pk HASH, pk HASH] with definitions pk, x, and the missing-definition sentence with pk alone); the two reworded assertions still fail when their billing sentence changes (each panics with its new message at key_attribute_names.rs:245 and :275); reverting either restore call to validate_create_table, or removing the CreateTable GSI loop or table-key call, fails the named tests. Core and engine lib tests pass (1270 passed, 1 ignored; 983 passed) on the head, on the head merged with main at 5826e1b, and on the head merged with #1442. The wire file passes 8 of 8 on SQLite and PostgreSQL; against DynamoDB 7 of 8 rows pass.

Previous items: 1 fixed.

  • Docs tests/test_key_schema_repeated_attribute.py:10 — The new docstring says each row was measured with the definitions it sends, but DynamoDB answers the missing-definition sentence for the hash_range_on_one_of_three row at line 92, and this file fails that row against the service.

@navareamol
navareamol requested a review from LJMart11 December 4, 2026 18:36

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.

3 participants