Skip to content

fix(engine): count only a found item against an ExecuteStatement Limit - #1142

Open
LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/1026-partiql-missing-key-limit
Open

LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/1026-partiql-missing-key-limit

Conversation

@LeeroyHannigan

Copy link
Copy Markdown
Collaborator

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 of Limit, 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. Under Limit 1 it ended the page with no rows and a NextToken.

Reader::read_item in crates/engine/src/partiql/select/steps.rs now returns the read's ScannedCount, which PageParts::add already 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 Limit 1 and ReturnConsumedCapacity TOTAL, then the same request with each NextToken. Partition or-page-missing holds row-1 to row-4. Partition or-page-filter holds row-1, and row-2 with v 1.

Condition DynamoDB pages: rows, NextToken, capacity units
pk = 'or-page-missing' AND (sk = 'row-0' OR sk = 'row-2') [row-2], none, 1.0
pk = '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.0
pk = 'or-page-missing' AND (sk = 'row-0' OR sk = 'row-9') [], none, 1.0
pk = 'or-page-filter' AND (sk = 'row-1' OR sk = 'row-2') AND v = 1 [], a token, 0.5, then [row-2], none, 0.5
pk = 'or-page-missing' AND (sk = 'row-0' OR sk > 'row-3') [row-4], a token, 1.0, then [], none, 0.5
the first row with ConsistentRead [row-2], none, 2.0
pk IN 11 keys, none with an item, on a table keyed on pk [], a token, 5.0, then [], none, 0.5

ExtendDB 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 5 backup_store tests that check file permissions fail on the test host, where a parent directory belongs to another user. This PR does not change backup_store.rs.
  • cargo +1.97.0 clippy -p extenddb-core -p extenddb-engine -p extenddb-server --all-targets -- -D warnings and cargo +1.97.0 fmt --all -- --check are clean.
  • Against ExtendDB on SQLite, 7 PartiQL wire test files passed 494 tests. The new data table in tests/test_partiql_page_reads.py walks 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.
  • The engine test only_a_key_that_has_an_item_takes_a_unit_of_the_limit walks 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.5 where the test expects [row-2] 1.0.
  • The last change cut the engine table from 10 walks to 2 and cut two doc lines. After it, the 5 tests of partiql::select::steps passed 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:

Other 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


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.

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 hitesh96db left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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.

PartiQL SELECT of sort keys under Limit 1 counts a missing key against Limit, where DynamoDB counts only the items it returns

2 participants