Skip to content

fix(query): preserve subquery limit after offset skips - #730

Merged
QuantumExplorer merged 7 commits into
developfrom
codex/fix-690-subquery-offset-limit
Aug 21, 2026
Merged

QuantumExplorer merged 7 commits into
developfrom
codex/fix-690-subquery-offset-limit

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented May 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #690: when an offset consumed all of a subquery's inner results, the outer limit was decremented anyway, so e.g. limit=2, offset=1 over a range of trees could return a single element when the first inner match was skipped rather than absent.

The fix only charges the outer limit for an empty subquery result when nothing was skipped (skipped == 0): true no-match emptiness still consumes a limit slot (preserving the decrease_limit_on_range_with_no_sub_elements DoS protection — offset is finite, so skips can't make empty-tree walks free), while offset-consumed emptiness leaves the limit untouched.

Versioning

Gated to GROVE_V4 (element.path_query_push: 1); GROVE_V1..GROVE_V3 are live in production and keep the legacy accounting bug-for-bug. Structured as the standard versioned dispatch (same shape as add_element_on_transaction and terminal_keys):

  • grovedb/src/element/path_query_push/mod.rs — module doc + version table
  • grovedb/src/element/path_query_push/v0.rs — frozen legacy implementation (GROVE_V1..V3)
  • grovedb/src/element/path_query_push/v1.rs — fixed accounting (GROVE_V4+)
  • ElementQueryExtensions::path_query_push in element/query.rs is the match dispatcher (unknown versions → UnknownVersionMismatch)

The two implementations differ only in the skipped == 0 guard on the empty-subquery limit decrement; the delta is marked inline at the change site in both files. The gate is also documented in the GROVE_V4 module doc.

path_query_push is trusted-read only: proof generation rejects non-zero offsets and never calls it, so no proof surface changes.

Tests

  • Forward and reverse regressions where offset consumes the first inner result before limit accounting begins (GROVE_V4 → fixed results)
  • Legacy pin: the same query under the real GROVE_V3 still returns the old (short) result set
  • Version-matrix equivalence tests running every other path_query_push branch (subquery_path with all three result types, subquery_path offset skip, raw tree push, non-tree push, empty-subquery_path error) under both GROVE_V3 and GROVE_V4, asserting identical results — proving v0 and v1 agree everywhere outside the gated accounting
  • Dispatcher unknown-version rejection test

Verification

  • cargo test -p grovedb — full suite green (2800+ tests)
  • cargo test -p grovedb subquery_offset_consumed / across_path_query_push_versions
  • cargo clippy -- -D warnings, cargo fmt --check

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Protocol version v3 now enables path_query_push v1, which implements versioned behavior for subquery offset/limit handling. The query logic conditionally gates limit decrement on version and skipped count to fix offset-consumed-empty subquery behavior; three new tests validate both v1 and legacy v0 modes.

Changes

Subquery Offset-Consumed-Empty Limit Fix

Layer / File(s) Summary
Version contract and query logic versioning
grovedb-version/src/version/v3.rs, grovedb/src/element/query.rs
GROVE_V3 bumps path_query_push to version 1. Query logic reads this version, validates with check_grovedb_v0_or_v1_with_cost!, and gates the limit-decrement behavior in empty-subquery branches on path_query_push_version == 0 || skipped == 0.
Forward, reverse, and legacy version test coverage
grovedb/src/tests/query_tests.rs
Three tests validate offset-consumed-empty behavior: forward and reverse direction with v1 expect two returned elements and skipped = 1 without limit decrease; legacy v0 test expects one element with the original decrement behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A subquery offset once played a trick,
Consuming the limit, oh how quick!
Now versioned in v3, the fix rings true—
Skip-aware decrement, a behavior made new! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: preserving subquery limit after offset skips, which directly addresses the core issue in the PR.
Linked Issues check ✅ Passed The PR implements the exact fix suggested in issue #690: only decrement the outer limit when no rows were skipped by offset, with new forward/reverse tests.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #690: version bump for path_query_push, conditional limit decrement logic, and regression tests.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-690-subquery-offset-limit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented May 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.40%. Comparing base (062a701) to head (9a92958).
⚠️ Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
grovedb/src/element/path_query_push/v0.rs 95.03% 8 Missing ⚠️
grovedb/src/element/path_query_push/v1.rs 96.34% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #730      +/-   ##
===========================================
+ Coverage    92.37%   92.40%   +0.03%     
===========================================
  Files          280      282       +2     
  Lines        86307    86482     +175     
===========================================
+ Hits         79724    79913     +189     
+ Misses        6583     6569      -14     
Components Coverage Δ
grovedb-core 90.60% <95.83%> (+0.07%) ⬆️
merk 93.28% <ø> (ø)
storage 87.05% <ø> (ø)
commitment-tree 96.07% <ø> (ø)
mmr 96.42% <ø> (ø)
bulk-append-tree 91.20% <ø> (ø)
element 97.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@QuantumExplorer QuantumExplorer changed the title Preserve subquery limit after offset skips fix(query): preserve subquery limit after offset skips May 21, 2026
@QuantumExplorer
QuantumExplorer force-pushed the codex/fix-690-subquery-offset-limit branch from 789d5ff to 5687dce Compare May 21, 2026 03:52
QuantumExplorer added a commit that referenced this pull request Jul 30, 2026
* feat(version): add GROVE_V4, behaviourally identical to V3

GROVE_V3 is live, so a fix that changes an accepted/rejected outcome, a
committed root hash, or a tracked cost cannot be applied unconditionally —
nodes carrying it would diverge from nodes that do not. There is currently
nowhere for such a fix to land, which has left several of them stuck:

  - #776: overwriting an indexed tree with a bare Reference skips the
    per-axis secondary cleanup. Closing it costs an extra stored-element read
    on EVERY reference overwrite (+1 seek, +79 storage_loaded_bytes, measured
    by the refresh-reference cost tests), and references over plain trees are
    shipped functionality.
  - Batch DeleteTree treats the caller-declared tree type as authoritative
    when selecting cleanup namespaces. Reading the stored element instead
    fixes both an indexed type-confusion and a live CommitmentTree
    wrong-emptiness-path bug, but adds a read to a released path.
  - Per project notes, five audit-fix PRs (#726, #730, #732, #734, #739)
    are gated on v3 and need re-gating before they can merge.

This adds the version and nothing else. Every method-version slot is copied
from V3 unchanged, so activating protocol version 4 today is a no-op; each
gate is a deliberate, separately-reviewable slot bump.

Verified rather than assumed: registering V4 changes what
`GroveVersion::latest()` resolves to, and the whole test suite defaults to
latest. The full workspace suite passes with V4 as latest (2459 grovedb + 705
merk + the rest), and the only two failures were the version registry's own
self-describing tests — `grove_version_latest_returns_v3` and
`grove_versions_count` — which are updated here. That is the evidence the
change is inert.

Adds `grove_v4_is_behaviourally_identical_to_v3_until_a_gate_is_added`, which
compares every slot and fails the moment one is bumped. That failure is the
intended prompt to document the gate rather than let V4 accrete behaviour
silently.

Co-Authored-By: Claude Fable 5 <[email protected]>

* test(version): drop the V3/V4 slot-identity assertion

It pinned V4's initial state as inert, which was worth verifying once but
becomes churn the moment a gate is added — and the DeleteTree read and #776
are both queued to gate on V4 next, so it would fail immediately and be
deleted anyway.

The evidence it provided is preserved where it belongs: the PR description
records that the full workspace suite passed with V4 as latest and that only
the registry's own self-describing tests changed.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
QuantumExplorer and others added 6 commits August 22, 2026 01:43
GROVE_V3 is live in production, so the fixed accounting moves to
GROVE_V4: v4 sets element.path_query_push = 1 (offset-consumed empty
subqueries no longer eat a limit slot) while v3 keeps the legacy
behavior. The legacy regression test now pins the real GROVE_V3
instead of a hand-mutated version struct.

Co-Authored-By: Claude Fable 5 <[email protected]>
…ispatch

Move the implementation into element/path_query_push/{mod.rs,v0.rs,v1.rs}:
v0 freezes the legacy limit/offset accounting for GROVE_V1..GROVE_V3, v1
(GROVE_V4+) carries the issue #690 fix as its own implementation, and the
ElementQueryExtensions::path_query_push trait method becomes the match
dispatcher, replacing the inline version check. Also document the gate in
the GROVE_V4 module doc.

Co-Authored-By: Claude Fable 5 <[email protected]>
Version-matrix tests run the subquery_path (all result types + offset
skip), raw-tree, non-tree, and empty-subquery_path branches under both
GROVE_V3 (v0) and GROVE_V4 (v1) and assert identical results, proving
the implementations agree everywhere outside the gated issue #690
accounting; plus a dispatcher unknown-version rejection test.

Co-Authored-By: Claude Fable 5 <[email protected]>
@QuantumExplorer
QuantumExplorer merged commit 86fe328 into develop Aug 21, 2026
7 checks passed
@QuantumExplorer
QuantumExplorer deleted the codex/fix-690-subquery-offset-limit branch August 21, 2026 21:03
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.

[audit][medium] Subquery offset can consume limit without returning a result

1 participant