Skip to content

Commit 2d675c0

Browse files
fix: iter_is_valid_for_type wrapper arms preserve cost (CodeRabbit)
CodeRabbit flagged that the three aggregate-wrapper arms in `QueryItem::iter_is_valid_for_type` (AggregateCountOnRange, AggregateSumOnRange, AggregateCountAndSumOnRange) early-return from the inner match BEFORE the outer `cost` accumulator is folded into the result, dropping the already-collected `iter.key()` cost AND making the recursive call re-read the iterator. Fix: short-circuit wrapper variants at the TOP of the function, before any `iter.key()` read. The inner item does its own read + cost accumulation, so the outer-level read becomes redundant. The in-match arms for wrapper variants become `unreachable!()` since the early return covers them all. Skipped CodeRabbit findings (decline with reasons): - `helpers.rs:99` `.unwrap()` on `CostResult` — false alarm. `.unwrap()` on `CostContext<T>` (which is what `CostResult<T>` is — `CostContext` is a wrapper struct, not `Result`) returns the inner `T` while discarding the cost. No panic risk. The `Result` is then handled by `.map_err(...)?`. Same idiom used by sibling `aggregate_count/helpers.rs` and `aggregate_sum/helpers.rs`. - V0 PCPS rejection at `generate.rs:815-823` — declining as inconsistent with the established V0 pattern. The match arm groups PCPS with `MmrTree`, `BulkAppendTree`, `DenseAppendOnlyFixedSizeTree`, and the other `Provable*` variants as "tree-without-subquery" emissions. CodeRabbit's suggestion would single-out PCPS for hard rejection while leaving sibling new-Element-variants behaving as tree-passthrough — that's the inconsistency. V0 already rejects PCPS at descend-time via the leaf-merk-open guard at `prove_subqueries:425`; the surface that remains (returning a PCPS Element as a query result without descending) follows the same passthrough pattern as MmrTree/BulkAppendTree and is consistent with how V0 has historically dealt with new Element variants it doesn't fully understand. 627 merk + 1791 grovedb tests pass; cargo fmt clean. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 517f327 commit 2d675c0

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

  • grovedb-query/src/query_item

‎grovedb-query/src/query_item/mod.rs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,20 @@ impl QueryItem {
12921292
}
12931293

12941294
// Key should also be something, otherwise terminate early.
1295+
// Aggregate-wrapper variants delegate directly to their inner item.
1296+
// Returning before the `iter.key()` read below avoids
1297+
// (a) double-charging the `iter.key()` cost when the inner item
1298+
// reads it again, and
1299+
// (b) duplicating the raw-iter read itself.
1300+
// The depth-bounded decoder + nested-aggregate validators already
1301+
// prevent wrapper-of-wrapper shapes, so recursion here is bounded.
1302+
if let QueryItem::AggregateCountOnRange(inner)
1303+
| QueryItem::AggregateSumOnRange(inner)
1304+
| QueryItem::AggregateCountAndSumOnRange(inner) = self
1305+
{
1306+
return inner.iter_is_valid_for_type(iter, limit, aggregate_limit, left_to_right);
1307+
}
1308+
12951309
let key = if let Some(key) = iter.key().unwrap_add_cost(&mut cost) {
12961310
key
12971311
} else {
@@ -1345,14 +1359,10 @@ impl QueryItem {
13451359
}
13461360
}
13471361
}
1348-
QueryItem::AggregateCountOnRange(inner) => {
1349-
return inner.iter_is_valid_for_type(iter, limit, aggregate_limit, left_to_right);
1350-
}
1351-
QueryItem::AggregateSumOnRange(inner) => {
1352-
return inner.iter_is_valid_for_type(iter, limit, aggregate_limit, left_to_right);
1353-
}
1354-
QueryItem::AggregateCountAndSumOnRange(inner) => {
1355-
return inner.iter_is_valid_for_type(iter, limit, aggregate_limit, left_to_right);
1362+
QueryItem::AggregateCountOnRange(_)
1363+
| QueryItem::AggregateSumOnRange(_)
1364+
| QueryItem::AggregateCountAndSumOnRange(_) => {
1365+
unreachable!("aggregate-wrapper variants short-circuit at the top of the function")
13561366
}
13571367
};
13581368

0 commit comments

Comments
 (0)