Repository navigation
fix(engine): end a PartiQL disjunct's Query when its next row does not match - #1176
LeeroyHannigan wants to merge 4 commits into
Conversation
…t match An ExecuteStatement SELECT of a local secondary index can name two values of its sort key. One such condition is pk = 'lk' AND (lsiSk = 'a' OR lsiSk = 'z'). Key 'a' has one row and key 'z' has none. Under Limit 1, both sides return that row with a NextToken. The next page is empty and has no NextToken. DynamoDB charges that page 0.5 read units. ExtendDB charged 1. The Query of 'a' stopped at the limit on its one row and kept a LastEvaluatedKey. So the next page read 'a' again and found nothing. ExtendDB charged that page for this Query and for the Query of 'z'. DynamoDB ends the read of a key when the next row of its index partition does not match that key. The next page then reads only the keys that remain. After a Query stops at the limit, ExtendDB now reads the next row, first under its key condition and then in its partition. When only the partition has a next row, ExtendDB treats the Query as read to its end. ExtendDB does not charge the page for these reads. A Query of a whole partition matches every row, so it skips both reads. A Query that stops on the last row of its partition keeps its LastEvaluatedKey as before. There DynamoDB reads the key again on some walks and not on others. Measured in us-east-1 on 2026-11-14 and 2026-11-16 on a local secondary index. Measured on 2026-11-18 on a local and a global secondary index, with and without ConsistentRead. On 2026-11-19 a table read gave the same pages: pk = 'lk' AND (sk < 'r2' OR sk = 'r9') under Limit 1. Fixes #1032.
hitesh96db
left a comment
There was a problem hiding this comment.
Required before approval: (a) do not pin the DESC walk that DynamoDB does not answer the same way every time, (b) add the table path and the cases where the next row matches to the dual-target Python test (inline), (c) correct the partition end claim (inline). I will approve once those are done. No row is lost or repeated by this change.
In 3704 walks per build on DynamoDB, this PR and main (local index on two partitions, the table, a global index with ALL and KEYS_ONLY projection, filters, ? parameters, 12 to 24 disjuncts, ASC and DESC, Limit 1, 2, 3 and 100), the rows equal DynamoDB's row for row, and no walk loops. The PR never adds a page or charges more than main, and removes 829 pages. 941 of 1088 pair walks match DynamoDB, against 598 for main. Every pair walk that does not match touches a partition edge. An item inserted after the stop key but inside the disjunct's range between two pages is not returned by DynamoDB and not by this PR, and main returned it. The merge into current main and with #1142 answers the same as the head.
Optional: the comment at crates/engine/src/partiql/select/visits.rs lines 20 to 22 still says a Query the page stops inside always goes on later. This PR now ends it when the next item does not match. catalog_context() in test_context.rs opens a second in-memory SQLite engine through ..context(...) and drops it.
| /// with r1 at a and r2 at b, `lsiSk = 'a' OR lsiSk = 'z'` with `Limit` 1 | ||
| /// gives [r1] and a NextToken, then an empty page of 0.5, as the table | ||
| /// read `pk = 'lk' AND (sk < 'r2' OR sk = 'r9')` does on 2026-11-19). At | ||
| /// a partition's end it reads the disjunct again on some walks only, so |
There was a problem hiding this comment.
The comment and the PR body say that at a partition's end the service reads the disjunct again on some walks only, so the Query goes on. Measured in us-east-1 on 2026-11-24 on 8 partitions of the issue's data (r1 at a, r2 and r3 at b), 10 walks each, ReturnConsumedCapacity TOTAL: the variance exists and depends on the partition.
lsiSk = 'a' OR lsiSk = 'b', Limit 1, ASC: pages [a] 0.5 T, [b] 0.5 T, [b] 0.5 with no extra page in 66 of 80 walks, and a trailing [] 0.5 page in 14. Six of the 8 partitions never sent the extra page. lsiSk = 'b' OR lsiSk = 'z', Limit 2: [b b] 0.5 T, [] 0.5 in 66 of 80 walks, [] 1.0 in 14. This PR charges [] 1.0 in every walk, as main does. On one partition every ASC walk that ends on the partition's last item was identical in 4 of 4 sends: lsiSk = 'l6' OR lsiSk = 'l9', Limit 1 gives DynamoDB [l6] 0.5 T | [] 0.5, this PR [l6] 0.5 T | [] 1.0.
So the PR fixes the issue's overcharge only when another item follows the found key in the partition. Say that in the body and in the comment instead of saying the answer cannot be asserted. 96 of the 104 cells that improve but still differ from DynamoDB differ only in the last page.
There was a problem hiding this comment.
Agreed. The comment now says what the code does, in ed5399c.
At a partition's end the Query keeps its key as before, so a later page reads the disjunct again and charges that read. This change ends the issue's overcharge only when another item follows the found key in its partition. The PR body says the same now.
| for (condition, pages) in WALKS { | ||
| let statement = | ||
| format!("SELECT * FROM \"pages\".\"lsi-all\" WHERE pk = 'lk' AND {condition}"); | ||
| assert_eq!(walk(&ctx, &statement, 1).await, pages, "{statement}"); |
There was a problem hiding this comment.
The DESC walk in this test expects [r3] | [r2] | [r1] | [] at 0.5 units each. DynamoDB gave that form on 2 of 5 sends and ended at [r1] with no NextToken on the other 3. Do not pin the last page of that walk.
Only this test guards the branches where the next row matches and where the partition ends. With the "next row matches" branch or the partition end branch changed (two mutations that lose rows in a walk, r3 on lsiSk = 'a' OR lsiSk = 'b' and r2 on sk <= 'r2' OR sk = 'r9'), this test fails and the Python cells do not. PostgreSQL, MongoDB and Cassandra therefore have no cell where the next row matches, none at a partition's end, none with DESC and none with Limit above 1.
There was a problem hiding this comment.
Done in 09c40aa. The descending walk now checks the three pages that hold r3, r2 and r1, at 0.5 each. It accepts both ends: [r1] with no NextToken, or [r1] with a NextToken and then an empty page at 0.5. The issue row is still checked in full.
Both mutations that lose rows still fail the test, on the descending walk, which then gives [r3] 0.5 next | [r1] 0.5 next | [] 0.5. The first makes the "next row matches" branch return true. The second deletes the read under the key condition, so the partition read decides alone. Each one also loses r3 on lsiSk = 'a' OR lsiSk = 'b' and r2 on sk <= 'r2' OR sk = 'r9'.
With only the partition read deleted, so that a Query also ends at its partition's end, the test passes now. That read decides only the page after the last item of a partition, and no walk loses a row without it.
| # ---- a Query that stops before an item it does not match -------------------- | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( |
There was a problem hiding this comment.
No test walks a table disjunct. With the rule limited to indexes, all 194 engine tests under partiql:: still pass and so do the Python tests, because that tree behaves like main for tables. Add a table case, lsiSk cases where the next row matches, one with DESC, one with Limit above 1, and one with a no-token last page. The cells in this file match DynamoDB on every walk except the partition end page described in the comment on steps.rs.
There was a problem hiding this comment.
The table case and the lsiSk cases you list need tables with items on DynamoDB to measure first. I didn't create those tables for this push, so these cells wait on that measurement.
The comment of next_item_unmatched said that at a partition's end the service reads a disjunct again on some walks only. It now says what the code does. At a partition's end the Query keeps its key as before, so a later page reads the disjunct again and charges that read. A later page skips that read only when another item follows the key in its partition. The visits module said that a Query the page stops inside always goes on later. It now says that a Query that stops at the limit before an item it does not match is read to its end.
On some sends DynamoDB ends the walk of lsiSk = 'a' OR lsiSk = 'b' with ORDER BY lsiSk DESC at [r1], with no NextToken. On other sends it gives [r1] with a NextToken and then an empty page of 0.5. The engine test now checks the pages that hold r3, r2 and r1, and accepts both ends.
|
Thanks for the review. I fixed the
|
hitesh96db
left a comment
There was a problem hiding this comment.
Required before approval: (b) from my first review is still open. The new commits changed tests/test_partiql_page_reads.py not at all. I will approve once the cases below are in. Items (a) and (c) are fixed. Checked at 09c40aa9 against DynamoDB in us-east-1 and SQLite builds of this head, the old head and current main.
(b), the table path and the next-row-matches cases: the reply says they need tables with items on DynamoDB to measure first. I measured them on a table with a local index, each walk 9 times (3 sends on each of 3 partitions), on partitions that hold r1 at a, r2 and r3 at b, and r4 at c with sort key r4. The fourth item moves the end of the walk off the partition's end, where DynamoDB answers two ways. All nine sends gave one answer in every cell below. Pages are rows, units, T for a NextToken. Main gives a page DynamoDB did not give on 18 of 29 cells. This head matches 28 of 29, and the new Python cells do not include a table disjunct or a case where the next row matches.
- Table
sk < 'r2' OR sk = 'r9',Limit1:[r1] 0.5 T, then[] 0.5(main gives[] 1.0). WithConsistentRead:[r1] 1.0 T, then[] 1.0(main[] 2.0). - Table
sk <= 'r2' OR sk = 'r9',Limit1, next row matches:[r1] 0.5 T,[r2] 0.5 T,[] 0.5.Limit2:[r1 r2] 0.5 T,[] 0.5.Limit100:[r1 r2] 1.0, no token. - Table
sk > 'r3' OR sk = 'r0'DESC,Limit1:[r4] 0.5 T,[] 0.5.sk >= 'r3' OR sk = 'r0'DESC,Limit1:[r4] 0.5 T,[r3] 0.5 T,[] 0.5. lsiSk = 'b' OR lsiSk = 'z',Limit1, next row matches:[r2] 0.5 T,[r3] 0.5 T,[] 0.5.Limit2:[r2 r3] 0.5 T,[] 0.5.Limit100:[r2 r3] 1.0.lsiSk <= 'b' OR lsiSk = 'z',Limit2:[r1 r2] 0.5 T,[r3] 1.0, no token.Limit3:[r1 r2 r3] 0.5 T,[] 0.5.lsiSk = 'a' OR lsiSk = 'b',Limit1 ASC:[r1] 0.5 T,[r2] 0.5 T,[r3] 0.5, no token.Limit2 DESC:[r3 r2] 0.5 T,[r1] 0.5.lsiSk = 'a' OR lsiSk = 'z',Limit100:[r1] 1.0, no token.lsiSk = 'a' OR lsiSk = 'y' OR lsiSk = 'z',Limit1:[r1] 0.5 T,[] 1.0.
Do not pin the same walks on the shipped fixture without r4: DynamoDB gives two answers at the last page there (lsiSk = 'b' OR lsiSk = 'z', Limit 1 ends [] 1.0 in 5 sends and [] 0.5 in 4). Adding r4 at lsiSk c (sort key r4) to the fixture removes that variance. Leave out begins_with(sk, 'r') OR sk = 'q9' at Limit 2: main differs from DynamoDB there too, because the absent item q9 takes a unit of Limit, which is #1142's subject.
The PR body says the new table walks cover 5 of the rows above. None of the five Python cells is a table disjunct.
Also open: no test pins the partition read. With next_item_unmatched's partition read replaced by Ok(true), the engine test still passes.
Fixed: (a), the DESC walk now accepts both ends. DynamoDB gave both again, with 8 of 9 sends continuing to an empty page on partitions with a fourth item and 6 of 9 ending at [r1] on partitions without one. The engine test still fails on both lost-row mutations. (c), the steps.rs comment now matches the code, which is unchanged apart from comments. The visits.rs comment is fixed. catalog_context is unchanged, and the author's reason holds (its 28 lines are identical to the copy in #1142), so it is optional.
CI: the 18 failing checks are all cancelled (13 passed). They never started a step, waited about 54 minutes for the Linux runner group, and were cancelled together at 22:24 UTC on 2026-11-25. The PR needs a re-run. A local run on this head passes: the SQLite build, fmt, clippy for extenddb-engine, 195 engine partiql:: tests, and 65 of 65 in the Python file.
Not measured: PostgreSQL, MongoDB, Cassandra, the full workspace suite, and the 1.91.1 toolchain check.
What
A PartiQL SELECT can read a partition by several disjuncts, such as
pk = 'lk' AND (lsiSk = 'a' OR lsiSk = 'z')on a local secondary index. Each disjunct that names no single item is a Query. UnderLimit, a Query that stopped at the limit kept its key, so the next page read that disjunct again. DynamoDB does not read it again when the next item of the partition does not match the disjunct. So ExtendDB charged the next page for an extra empty Query.After a Query stops at the limit,
next_item_unmatchedincrates/engine/src/partiql/select/steps.rsnow reads one item past the stop key under the disjunct's key condition. When that read finds nothing, it reads one item past the stop key in the whole partition. When only the second read finds an item, the Query counts as read to its end, and no later page reads it. These two reads are not charged and add no row to the page.At a partition's end the Query keeps its key, as before, and a later page reads the disjunct again. So this change ends the issue's overcharge only when another item follows the found key in its partition. A disjunct with no sort condition matches every item of its partition, so it skips both reads.
The change reaches index reads and table reads. A table disjunct is a Query when it names a range of the sort key, a
begins_withor the partition key alone.Why
These are DynamoDB's answers, measured in us-east-1 on 14, 16, 18 and 19 November 2026. Each walk went out twice and got the same pages both times. A walk is ExecuteStatement with
Limit1 and"ReturnConsumedCapacity": "TOTAL", then the same request with each NextToken. Partitionlkholdsr1withlsiSka, andr2andr3withlsiSkb. Each page shows its rows, whether it has a NextToken, and its read units.lsi-all,lsiSk = 'a' OR lsiSk = 'z'(the issue)[r1]token 0.5,[]0.5lsi-all,lsiSk = 'z' OR lsiSk = 'a'(the issue)[r1]token 0.5,[]0.5lsi-all,lsiSk = 'a' OR lsiSk = 'y' OR lsiSk = 'z'[r1]token 0.5,[]1.0lsi-all,lsiSk = 'a' OR lsiSk = 'z',ConsistentRead[r1]token 1.0,[]1.0gpkandgsk,gsk = 'a' OR gsk = 'z'[r1]token 0.5,[]0.5lsi-all,lsiSk = 'a' OR lsiSk = 'b',ORDER BY lsiSk DESC[r3]token 0.5,[r2]token 0.5,[r1]token 0.5,[]0.5sk < 'r2' OR sk = 'r9'[r1]token 0.5,[]0.5sk < 'r2' OR sk = 'r9',ConsistentRead[r1]token 1.0,[]1.0lsi-all,lsiSk = '0' OR lsiSk = 'a'[r1]1.0, no tokenlsi-all,lsiSk IN ['a', 'c00', ..., 'c09'][r1]token 0.5,[]5.0This PR gives DynamoDB's pages for each walk above. Some walks got the same pages from DynamoDB as from ExtendDB before, and they keep them. Two of them are
sk <= 'r2' OR sk = 'r9'on the table and a filter that drops the next item of the partition.Closes #1032
Testing done
cargo test -p extenddb-core --lib: 1177 passed, 1 ignored.cargo test -p extenddb-engine --lib: 934 passed.cargo +1.97.0 clippy -p extenddb-core -p extenddb-engine --all-targets -- -D warnings,cargo +1.97.0 fmt --all -- --checkandcargo +1.91.1 check -p extenddb-engine --all-targetsare clean.tests/test_partiql_page_reads.pypassed 65 tests. Its new table walks 5 of the rows above.steps.rsfrom main, it fails on the issue row. When the "next row matches" branch ends the Query, it fails on the descending walk, which losesr2. With the partition read deleted, it passes. That read decides only the page after[r1], and the test accepts that page in both forms DynamoDB gave.cargo test -p extenddb-server --libpassed 513 tests. 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.Line counts: production +44 -3. Tests +225 -10.
Not in this PR:
LastEvaluatedKeyof such a page follows its last item, where DynamoDB's follows the Query that stopped. Main gives the same keys.[r1]with no NextToken, so the engine test accepts both ends of that walk. Two other walks differ from ExtendDB there:lsiSk = 'a' OR lsiSk = 'b'ends after[r3]on DynamoDB on most sends. ExtendDB sends a NextToken with[r3]and then an empty page at 0.5.lsiSk = 'b' OR lsiSk = 'z'withLimit2 gives[r2, r3]and then[]at 0.5 on DynamoDB. ExtendDB charges that empty page 1.0.Other open PRs:
catalog_contextlines tocrates/engine/src/partiql/test_context.rs, and git merges them as one copy.walkinsteps/tests.rs. This PR adds the samewalkinsteps/index_tests.rs. The PR that merges second deletes its copy and calls the other one. That change is +3 -34 lines here, or +2 -32 lines in fix(engine): count only a found item against an ExecuteStatement Limit #1142.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.