Repository navigation
fix(engine): count only a found item against an ExecuteStatement Limit - #1142
LeeroyHannigan wants to merge 1 commit into
Conversation
An ExecuteStatement SELECT can name several sort keys of one partition. ExtendDB counted a key with no item against Limit. Under Limit 1 it ended the page with no rows and a NextToken. DynamoDB counts only the items it reads. A key with no item takes no unit of Limit, so the page reads the next key. DynamoDB charges the read of the missing key on that page. An item that the filter drops still takes a unit. Measured in us-east-1 on 2026-11-14 and 2026-11-18. With row-0 missing, (sk = 'row-0' OR sk = 'row-2') under Limit 1 gives [row-2], no NextToken and 1 capacity unit. ExtendDB gave an empty page with a NextToken, then [row-2]. A read of one named item now takes the number of items it evaluated as its units of Limit. The 1 MB page budget and the cap of 10 disjuncts a page are unchanged. Fixes #1026.
hitesh96db
left a comment
There was a problem hiding this comment.
Row 5 of the PR body does not match DynamoDB today, and one test gap lets a mutant through (inline). Required: correct row 5, and pin a read that carries a filter. I will approve once both are done.
Row 5: pk = 'p' AND (sk = 'row-0' OR sk > 'row-3'), Limit 1, partition holding row-1 to row-4. Measured in us-east-1 on 2026-11-24, twice on a table with a local index and three times on one without, identical each time.
- DynamoDB: one page,
[row-4], no NextToken, 1.0 units. - This PR:
[row-4]with a NextToken at 1.0, then[]at 0.5. The body says this is DynamoDB's answer. - main: three pages (
[]0.5, then[row-4]0.5, then[]0.5).
DynamoDB now sends no NextToken when a Query reaches Limit on its last matching item. pk = 'p' AND sk > 'row-2' with Limit 1 gives [row-3] with a token and then [row-4] at 0.5 with none, and the Query API with sk > :s returns Count 1 and no LastEvaluatedKey. This PR does not change that and it is not a regression. 22 cells of the key-OR-range family on the table with the local index and 23 on the one without still differ from live. Correct the row. No test covers a range disjunct beside a missing key.
Not a condition, but it changes answers: three cells at the 1 MB page boundary now differ from DynamoDB where main matched. Partition of rows of 200000 bytes, row-1 to row-6, Limit 5, sk = 'row-0' OR sk = 'row-1' OR ... OR sk = 'row-6'. DynamoDB: 4 rows, NextToken, 98.5 units, then 2 rows at 49.0. This PR: 5 rows at 123.0, then 1 row at 24.5. main: 4 rows at 98.5, then 2 rows. main matched only because the missing key took a unit of Limit. The same read without the missing key and without Limit gives DynamoDB 4 then 2 rows and ExtendDB 5 then 1, on main and here. The page size rule needs its own issue.
The branch is 26 commits behind main. The merge into current main answers the same as the head in every cell, and git merge-tree with #1176 is clean.
| page.add(result, read); | ||
| Ok(Read::Done(1)) | ||
| let (_, evaluated) = page.add(result, read); | ||
| Ok(Read::Done(evaluated)) |
There was a problem hiding this comment.
Nothing pins this count for a read that carries a filter. With read_item keeping the old count when the read has a filter, the engine test and all 494 PartiQL wire tests still pass. DynamoDB for pk = 'p' AND (sk = 'row-0' OR sk = 'row-1' OR sk = 'row-2') AND v = 1 with Limit 1 gives [] with a NextToken at 1.0, then [row-2] at 0.5. This PR matches. Add that walk to the engine test and to the Python cases.
A table with no sort key (pk IN [...]) is also unpinned. With the old count kept for such tables, all 494 tests pass. I could not measure that shape on DynamoDB, so the expected pages for it need a measurement before a test is written.
| # ---- a key with no item under Limit ------------------------------------------ | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( |
There was a problem hiding this comment.
The PR body lists ConsistentRead, sk > 'row-3' beside a missing key, pk IN on a table without a sort key and cross-partition item reads as measured. None has a case here. ConsistentRead and the cross-partition item reads match DynamoDB at the head. sk > 'row-3' beside a missing key does not (see the review body, row 5). pk IN on a table without a sort key was not measured on DynamoDB.
The cases also drop LastEvaluatedKey. DynamoDB sends one on a [row-2] page that carries no NextToken (issue #1141 tracks it), so the cases do not describe those pages completely.
| for (partition, condition, limit, pages) in WALKS { | ||
| let statement = | ||
| format!("SELECT * FROM \"pages\" WHERE pk = '{partition}' AND {condition}"); | ||
| assert_eq!(walk(&ctx, &statement, limit).await, pages, "{statement}"); |
There was a problem hiding this comment.
assert_eq! inside the loop stops at the first failing walk, so a failure in the first walk hides the second. Collect the walks and compare once.
What
An ExecuteStatement SELECT can name several sort keys of one partition, such as
pk = 'p' AND (sk = 'row-0' OR sk = 'row-2'). A key with no item now takes no unit ofLimit, so the page goes on to the next key. An item that exists takes one unit, also when the filter drops it. Each read stays charged on the page that makes it.Before this change, ExtendDB counted a key with no item against
Limit. UnderLimit1 it ended the page with no rows and a NextToken.Reader::read_itemincrates/engine/src/partiql/select/steps.rsnow returns the read'sScannedCount, whichPageParts::addalready returns, in place of 1. A Query disjunct already used that count. The change keeps the 1 MB page budget and the cap of 10 disjuncts a page.The engine test needs a table, so
test_context::catalog_context()builds a context over an in-memory SQLite catalog.Why
These are DynamoDB's pages, measured in us-east-1 on 14 and 18 November 2026. Each walk went out twice and gave the same pages both times. A walk is the request with
Limit1 andReturnConsumedCapacityTOTAL, then the same request with each NextToken. Partitionor-page-missingholdsrow-1torow-4. Partitionor-page-filterholdsrow-1, androw-2withv1.pk = 'or-page-missing' AND (sk = 'row-0' OR sk = 'row-2')[row-2], none, 1.0pk = 'or-page-missing' AND (sk = 'row-1' OR sk = 'row-15' OR sk = 'row-2')[row-1], a token, 0.5, then[row-2], none, 1.0pk = 'or-page-missing' AND (sk = 'row-0' OR sk = 'row-9')[], none, 1.0pk = 'or-page-filter' AND (sk = 'row-1' OR sk = 'row-2') AND v = 1[], a token, 0.5, then[row-2], none, 0.5pk = 'or-page-missing' AND (sk = 'row-0' OR sk > 'row-3')[row-4], a token, 1.0, then[], none, 0.5ConsistentRead[row-2], none, 2.0pk IN11 keys, none with an item, on a table keyed onpk[], a token, 5.0, then[], none, 0.5ExtendDB now gives each of these pages. The walks of the issue that read the missing key last keep the pages they had.
Closes #1026
Testing done
cargo test -p extenddb-core --lib: 1146 passed.cargo test -p extenddb-engine --lib: 871 passed. A backup timing test failed once in a full run under load and passed when it ran alone.cargo test -p extenddb-server --lib: 513 passed. The 5backup_storetests that check file permissions fail on the test host, where a parent directory belongs to another user. This PR does not changebackup_store.rs.cargo +1.97.0 clippy -p extenddb-core -p extenddb-engine -p extenddb-server --all-targets -- -D warningsandcargo +1.97.0 fmt --all -- --checkare clean.tests/test_partiql_page_reads.pywalks 10 rows. They are the 4 walks of the issue, its 2 walks that read the missing key last, and 4 walks measured on 18 November.only_a_key_that_has_an_item_takes_a_unit_of_the_limitwalks the first row and the filter row. With the fix reverted, it fails on the first row: ExtendDB gives[] 0.5 next | [row-2] 0.5where the test expects[row-2] 1.0.partiql::select::stepspassed and fmt is clean. The other runs above are on the commit before that change.Line counts: production +12 -6, of which 2 lines are code, tests +241 -16.
Not in this PR:
LastEvaluatedKey, and ExtendDB sends the key ofrow-2. origin/main gives the same key.Other open PRs:
crates/engine/src/table_name_length_tests.rs. Of the two PRs, the one that merges second makescatalog_contextpub(crate)and uses it there, which removes about 19 lines.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, 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
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.