Skip to content

fix(engine): answer an invalid table Scan start key as DynamoDB does - #1218

Open
LeeroyHannigan wants to merge 6 commits into
fix/1033-index-scan-start-key-typefrom
fix/1174-table-scan-start-key-text
Open

LeeroyHannigan wants to merge 6 commits into
fix/1033-index-scan-start-key-typefrom
fix/1174-table-scan-start-key-text

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

  • 1b77c8b adds TABLE_START_KEY_ROWS to tests/python/test_scan_edge_cases.py. It holds the six refused start keys of the table in Why that had no test, and one parametrized test, test_table_start_keys, sends them. Each row asserts the answer that DynamoDB gave. The test stores one item before the Scan, as the measured tables held one item.
  • 12e4194 adds table_scan_start_key_faults_answer_as_dynamodb_does to crates/engine/src/start_key_tests.rs. It sends the nine refused start keys of the table without Segment, and the served one, to validate_scan_exclusive_start_key with no index.
  • This body names the test of each row of the table and gives the results at 12e4194.

What

A Scan of a table refused an ExclusiveStartKey that does not fit the key schema with The provided starting key is invalid: The provided key element does not match the schema. DynamoDB answers The provided starting key is invalid. A Query gives that text for the same start keys. A Scan of an index gives it for a key left out, an extra attribute or an empty map. A table Scan now answers each of these start keys with that text.

The change is in crates/engine/src/start_key.rs. It removes the long text, a second copy of the short text, the function that chose between them, and the parameter that carried the choice. The Query constant becomes INVALID_START_KEY_MSG and serves both reads. Five tests in tests/python/test_scan_edge_cases.py now expect the short text. They pass the module's INVALID_START_KEY to assert_validation, which also checks HTTP status 400. TABLE_START_KEY_ROWS in the same file holds the other six refused start keys of the table in Why. table_scan_start_key_faults_answer_as_dynamodb_does in crates/engine/src/start_key_tests.rs checks the table Scan start keys in the engine.

Two storage copies of the long text stay, in scan_impl of crates/storage-sqlite/src/data/query_scan.rs and in scan_impl of crates/storage-postgres/src/data/query_scan.rs. The engine refuses these start keys first, so no request reaches them. The code after each copy relies on the condition that guards it (SQLite reads the partition key with unwrap()), so both stay.

The base of this PR is #1179, which changes the same file.

Why

DynamoDB's answers, measured in us-east-1 on 19 and 20 November 2026 for a Scan with an ExclusiveStartKey. Each request was sent twice and got the same answer both times. The key of table A is pk (S). The key of table B is pk and sk (S).

Table ExclusiveStartKey DynamoDB ExtendDB before this change
A {"bad": {"S": "p"}} The provided starting key is invalid the long text
A {} the same the long text
A {"pk": {"S": "P"}, "sk": {"S": "S"}} the same the long text
A {"pk": {"N": "42"}} the same the long text
B {"pk": {"S": "P"}, "sk": {"S": "S"}, "extra": {"S": "junk"}} the same the long text
B {"pk": {"N": "42"}, "sk": {"S": "S"}} the same the long text
B {"pk": {"S": "a"}, "sk": {"SS": ["a"]}} the same the long text
B {"pk": {"S": "P"}} the same the long text
B {} the same the long text
B, Segment 0 of 2 {"pk": {"N": "42"}, "sk": {"S": "S"}} the same the long text
B, Segment 0 of 2 {"pk": {"S": "P"}} the same the long text
B {"pk": {"S": "P"}, "sk": {"S": "S"}} served served

Each refusal is HTTP 400 ValidationException. With this change, ExtendDB answers each row as DynamoDB does. The answers of a Query and of an index Scan with these start keys do not change.

The wire tests of these rows are in tests/python/test_scan_edge_cases.py:

  • The first three rows of table A: test_scan_malformed_exclusive_start_key, test_scan_empty_exclusive_start_key and test_scan_simple_table_extra_attribute_in_exclusive_start_key.
  • The first two rows of table B: test_scan_extra_attribute_in_exclusive_start_key and test_scan_wrong_scalar_type_in_exclusive_start_key.
  • The other six refusals, the two Segment rows included: TABLE_START_KEY_ROWS.
  • The served row: test_scan_full_composite_exclusive_start_key_is_accepted.

The two Segment rows give DynamoDB's answer. The record does not show the order of DynamoDB's checks, because it does not show which segment DynamoDB gives these keys. handle_scan in crates/engine/src/scan.rs runs the start key shape check before the segment check. On SQLite and on PostgreSQL, {"pk": {"S": "P"}} is outside segment 0 of 2 in the test's table, so its row fails when the segment check runs first.

Fixes #1174

Testing done

At 12e4194, with --locked on each cargo build, check, clippy and test:

  • cargo +1.97.0 test -p extenddb-engine --lib: 935 passed.
  • The two clippy commands of .github/workflows/clippy.yml, with cargo +1.97.0, are clean: cargo clippy --all-targets --features extenddb-storage-cassandra/test-hooks,extenddb-storage-mongodb/test-hooks -- -D warnings and cargo clippy --all-targets --features extenddb-engine/backup-test-hooks -- -D warnings. cargo +1.97.0 clippy -p extenddb-engine --all-targets -- -W clippy::pedantic gives no warning on a line this PR adds. cargo +1.94.0 check -p extenddb-engine --all-targets, cargo +1.91.1 check -p extenddb-engine --all-targets and cargo +1.97.0 fmt --all -- --check are clean too.
  • Against ExtendDB built from 12e4194, on SQLite (a build with --no-default-features --features sqlite) and on PostgreSQL 16 (the default build):
    • Six files of tests/python passed 255 tests on each backend: test_scan_edge_cases.py, test_query_start_key_index.py, test_query_start_key_partition.py, test_query_start_key_range.py, test_query_scan.py and test_composite_keys.py.
    • Six files of tests/ passed 590 tests on each backend: test_attribute_value_validation.py, test_query_scan.py, test_read_paging.py, test_gsi_multi_range_query.py, test_query_key_value_types.py and test_query_begins_with_max_character.py. On each backend, one test skipped, because it runs only on a backend that filters the first RANGE attribute alone.
    • pytest --collect-only of tests and of tests/python collects 7102 and 703 tests with no error.
  • Controls, each with the tests of 12e4194:
    • With crates/engine/src/start_key.rs of fix(engine): name the start key of the wrong type in an index Scan #1179's head 74fe005, each of the six rows of TABLE_START_KEY_ROWS fails on SQLite and on PostgreSQL with the long text. table_scan_start_key_faults_answer_as_dynamodb_does fails, and the other 934 engine lib tests pass.
    • With the segment check before the shape check in handle_scan, the Segment row with {"pk": {"S": "P"}} fails on SQLite and on PostgreSQL with the segment text. The Segment row with {"pk": {"N": "42"}, "sk": {"S": "S"}} passes.
  • A local merge of 12e4194 with main at c057660 has no conflict. On that tree, the 39 start_key tests of the engine pass, and the 56 tests of test_scan_edge_cases.py pass on SQLite and on PostgreSQL. This merge is not in the branch.
  • The branch does not have fix(core): read the latency segment clock through web_time #1482 or fix(auth): pre-fault the memfd_secret store with reads from /dev/zero #1522. These runs do not include the clippy step of the WebAssembly workflow or the tests of extenddb-wasm-adapter.

Line counts against #1179: production +8 -33, tests +70 -27.

This PR adds no conflict with an open PR.

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

A Scan of a table with an ExclusiveStartKey that does not fit the key schema gets The provided starting key is invalid in place of the longer text. A client that matches the longer text sees the short one. A start key that fits, a Query and a Scan of an index get the same answers as before.


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.

A table Scan refused an ExclusiveStartKey that does not fit the key
schema with a longer text. That text was "The provided starting key is
invalid: The provided key element does not match the schema". Amazon
DynamoDB answers "The provided starting key is invalid" for each of
these start keys. They hold another attribute name, an extra attribute
or a key of another type, or they leave a key out. A Query already
answers this text for the same start keys.

Measured in us-east-1 on 19 and 20 Nov 2026, each request twice. The
tables were keyed on a partition key, and on a partition key and a sort
key.

A table Scan now answers each of these start keys with this text. The
longer text and the function that chose between the two texts are
removed.

Fixes #1174.
The five Scan tests that expect "The provided starting key is invalid"
compared only the error code and the message. They now call
assert_validation with the INVALID_START_KEY constant of the module.
assert_validation also checks that the HTTP status is 400.
The doc comment of validate_scan_exclusive_start_key keeps the dates of
the measurements and names no issue.

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

CI has not run the wire tests on this head. The Test, Integration Tests, Cassandra Integration Tests and MongoDB Integration Tests runs for 4b7486e (45695123506, 45695122959, 45695123427 and 45695123014) were cancelled before a runner picked them up, and WebAssembly failed with E0308. WebAssembly passes on main since #1482 landed, and this branch is 14 commits behind main. I merged current main (222d80a) into the head locally: no conflict, it builds, and on SQLite the six tests/python files from the description pass (249), as do tests/test_read_paging.py, tests/test_query_scan.py and tests/test_attribute_value_validation.py (294). So PostgreSQL, Cassandra and MongoDB have no result for this change yet. Could you update the branch with main once #1179 lands and let these workflows finish?

The five changed tests cover 5 of the 11 refused start keys in the description table. These rows have no test: table A with {"pk": {"N": "42"}}, and table B with {"pk": {"S": "a"}, "sk": {"SS": ["a"]}}, with {"pk": {"S": "P"}}, with {}, and the two Segment 0 of 2 rows. The Segment rows also pass through the segment membership check in crates/engine/src/scan.rs, which runs after the shape check, and nothing pins that order for a table Scan. There is no engine unit test for a table Scan start key either: with the start_key.rs change reverted, the engine library unit tests still pass, 979 of 979. I ran all 11 rows as wire tests on SQLite. They pass on this head merged with main and fail on main and on the revert, so I think the code is right and only the tests are short. Could you add the six missing rows to test_scan_edge_cases.py, for example as one parametrized table like INDEX_START_KEY_ROWS, and a table case (no index) to start_key_tests.rs?

I will approve once CI has run to the end on a branch updated with main and the six missing rows have tests.

DynamoDB refuses 11 start keys of a table Scan with `The provided
starting key is invalid` in the table of #1174, and five tests covered
5 of them. TABLE_START_KEY_ROWS adds the other six to one parametrized
test. Table "a" is keyed on pk, and table "b" on pk and sk:

- "a" with {"pk": {"N": "42"}}
- "b" with {"pk": {"S": "a"}, "sk": {"SS": ["a"]}}, with {"pk": {"S": "P"}}
  and with {}
- "b" with Segment 0 of 2, with {"pk": {"N": "42"}, "sk": {"S": "S"}} and
  with {"pk": {"S": "P"}}

Each row is the answer that DynamoDB gave in us-east-1 on 19 or 20
November 2026, on a table that held one item. The answer was HTTP 400,
ValidationException and the short text. The test stores the same item
before the Scan.
The engine tests checked the start key text of an index Scan and an
index Query, but not of a table Scan. table_scan_start_key_faults_answer_as_dynamodb_does
sends the measured start keys of a table Scan, without Segment, to
validate_scan_exclusive_start_key with no index. It expects `The
provided starting key is invalid` for the nine start keys that DynamoDB
refuses and no error for the one that it serves.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

@navareamol

  1. CI and main. This branch takes main after fix(engine): name the start key of the wrong type in an index Scan #1179 does, because fix(engine): name the start key of the wrong type in an index Scan #1179 is its base. The new commits do not merge main. The facts at 4b7486e:

  2. The six rows and the engine test.

    • 1b77c8b adds the six rows to tests/python/test_scan_edge_cases.py as TABLE_START_KEY_ROWS, one parametrized test in the form of INDEX_START_KEY_ROWS. Each row asserts the answer that DynamoDB gave in us-east-1 on 19 or 20 November 2026, on a table that held one item. That answer is HTTP 400, ValidationException and The provided starting key is invalid. Each request went out twice with the same answer. The four rows without Segment have records from three probe runs, on 19 and 20 November. The two Segment rows have one probe run, on 20 November at 04:30 UTC.
    • The Segment rows and the order of the checks: the record gives DynamoDB's answer, but not the order of its checks. It does not show which segment DynamoDB gives these keys. In ExtendDB, {"pk": {"S": "P"}} is outside segment 0 of 2 in the test's table on SQLite and on PostgreSQL. On SQLite the stored item has an odd rowid, and on PostgreSQL hashtext('P') is odd. With the segment check moved before the shape check in handle_scan, that row fails on both backends, so it pins the order there. The row with {"pk": {"N": "42"}, "sk": {"S": "S"}} passes with either order: on SQLite no item has that key, and on PostgreSQL hashtext('42') is even.
    • 12e4194 adds table_scan_start_key_faults_answer_as_dynamodb_does to crates/engine/src/start_key_tests.rs. It sends the nine refused start keys without Segment, and the served one, to validate_scan_exclusive_start_key with no index. With start_key.rs of fix(engine): name the start key of the wrong type in an index Scan #1179's head, this test fails and the other 934 engine lib tests pass.
    • Each of the six new rows fails with start_key.rs of fix(engine): name the start key of the wrong type in an index Scan #1179's head and passes at 12e4194, on SQLite and on PostgreSQL. A local merge of 12e4194 with main at c057660 (not pushed) has no conflict. On that tree, the 56 tests of test_scan_edge_cases.py pass on SQLite and on PostgreSQL, the six new rows included. These local runs do not include Cassandra or MongoDB.

@navareamol

Copy link
Copy Markdown
Collaborator

The wire suites now run on this head and pass on SQLite, PostgreSQL, Cassandra and MongoDB (run-integration, run-integration-sqlite, cassandra-pytest and mongodb-pytest all pass at 12e4194). Six checks are red: wasm, run-tests (stable and 1.91.1), test, mongodb-rust and mongodb-integration. I read the job logs. wasm fails with E0308 at crates/core/src/metrics/collector.rs:60, expected std::time::Instant, found web_time::Instant, and the branch does not have #1482. run-tests and mongodb-rust fail on secmem::tests::init_refuses_under_a_low_memlock_limit_then_starts_unlocked (secmem.rs:482), and the branch does not have #1522. mongodb-integration fails in its gate step. On main at c057660 the Test and WebAssembly runs pass, and on #1167, which has both changes, run-tests, test and wasm pass. The head merges with main without a conflict. I read the red checks as the stack sitting behind main, so they should clear once #1179 takes main and this branch merges it.

  1. The six rows. TABLE_START_KEY_ROWS runs on SQLite and the engine test runs in cargo test -p extenddb-engine --lib (935 passed). With start_key.rs of fix(engine): name the start key of the wrong type in an index Scan #1179's head put back, the new engine test fails and all six rows fail with the long text, together with the five old tests. With the segment check moved ahead of the shape check in handle_scan, only the Segment row with {"pk": {"S": "P"}} fails on SQLite. The {"pk": {"N": "42"}, "sk": {"S": "S"}} row passes either way, as you wrote. I did not run PostgreSQL for the order check. The answers for those two rows are your measurements, and I did not repeat them.

  2. The six files of tests/python from the body give 255 passed on SQLite here, the same number as yours.

I will approve once the checks are green on a branch that has #1179 updated with main.

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