Repository navigation
Count query matches with COUNT(*) instead of materializing rows (closes #2743) - #2749
Conversation
#2743) The query editor's Preview answered "how many nodes match these filters?" by running the query and calling `.length` on the result: up to 500 full node payloads fetched, transferred over gRPC and deserialized, with every byte past the count discarded. It also saturated — a filter matching 12,000 tasks rendered as "500+", true but useless for judging whether the filter did what the user meant. Add a `CountQuery` RPC over `ExecuteQueryRequest` returning a scalar, mirroring the existing `QueryNodesSimple`/`CountNodes` pairing. `QueryService::build_where_clause` is extracted so the select and the count are built from one clause and cannot select one set of rows while counting another; `build_count_query` wraps it in COUNT(*) with no ORDER BY and no LIMIT, neither of which can affect a total. The preview now reports an exact total at any size. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Code Review: PR #2749 — Count query matches with COUNT(*) instead of materializing rowsReview Type: Initial Review SummaryClean, well-scoped, net-positive change that does exactly what the issue asked and nothing more. The central design decision — extracting Correctness claims were verified empirically rather than by reading alone — including mutation-testing the two tests flagged as possibly tautological. Both catch real regressions. Verification performed:
Requirements Check (issue #2743)
All four acceptance criteria met. Deep-dive on high-scrutiny areas1. SQL injection — no new surface. Confirmed safe.
Net: the count path's injection surface is a strict subset of the select path's (no 2. Select/count divergence — no input found where they disagree.
One theoretical difference: 3. int64 across the wire — correct. 4. 5. Test quality — mutation-tested; neither flagged test is tautological. Mutation A — Mutation B — count built as
6. Dead code / leftovers — clean. Findings🔴 CriticalNone. 🟡 ImportantNone. 🟢 Suggestions🟢 Nit: an unbounded Every other query RPC clamps to Principle: removing a limit is a deliberate cost/correctness trade — worth naming explicitly so a future reader doesn't "fix" it by reinstating a cap that would silently reintroduce the saturation bug this PR removes. 🟢 Nit: Takes fully-assembled SQL and relies on the caller to have escaped everything. The doc comment states this and notes Principle: invariants enforced by the type system survive refactors; invariants enforced by doc comments survive only as long as people read them. Worth escalating when the surface grows, not at N=2. Recommendation: APPROVEClear net improvement to code health, no critical or important findings. Three things stand out:
Both suggestions are optional and neither should block the merge. |
malibio
left a comment
There was a problem hiding this comment.
Automated pragmatic-code-review: APPROVE (posted as a comment — GitHub blocks self-approval). Full findings in the review comment above: 0 critical, 0 important, 2 optional nits. All four acceptance criteria met.
Both review findings were 🟢 nits on an APPROVE; neither changes behavior. - Document why `count_query` is the one query RPC that does not clamp to MAX_ROW_LIMIT, and that reinstating a cap would reintroduce the saturation bug the RPC exists to remove — so the trade is recorded at the site rather than rediscovered by someone "fixing" it. - Narrow `count_nodes_raw` from `pub` to `pub(crate)`. Its only caller, QueryService, is in-crate, so the compiler now enforces the caller-escapes-everything contract the doc comment had been carrying alone. Skipped the reviewer's optional `TrustedSql` newtype: it would require converting the pre-existing `query_node_ids_raw` to match, extending this PR into code it does not otherwise touch. `pub(crate)` gets the same compiler-enforced-invariant benefit at a fraction of the surface. Worth revisiting if the raw-SQL surface grows past these two methods. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Closes #2743.
Problem
The query editor's Preview answered "how many nodes match these filters?" by running the query and calling
.lengthon the result — fetching up to 500 full node payloads, transferring them over gRPC, deserializing them, and discarding every byte past the count.It also saturated: a filter narrowing 50,000 tasks to 12,000 rendered as
500+. True, but useless for judging whether the filter did what the user meant.Approach
Added a
CountQueryRPC overExecuteQueryRequestreturning a scalar, mirroring the existingQueryNodesSimple/CountNodespairing.The core of the change is extracting
QueryService::build_where_clauseas the single authority on which rows a query matches. Bothbuild_query(select) andbuild_count_query(count) are now built from it, so a filter cannot select one set of rows while counting another. Ordering and limiting are not part of matching and stay with the caller — a count has neither.Threaded through every layer: proto →
QueryService/SqliteStore/query_ops→ daemon → Tauri command → dev-proxy → adapter interface and all three implementations → the Svelte preview handler. The editor's500+suffix is gone, since aCOUNT(*)has no row cap to saturate against.Acceptance criteria
ExecuteQueryfilter shape, reusingQueryService's WHERE-clause generation rather than duplicating ithandleQueryPreviewuses it and no longer materializes rows500+saturationTesting
QueryServicecount testscount_query_reports_exact_total_ignoring_limit_and_sortingpasses — pins the no-saturation behavior end-to-end and assertsexecute_query/count_queryagree on the same filtersquality:fix: clean (0 errors, 0 warnings, clippy clean, no new design-token violations)I verified
test_count_ignores_limitgenuinely fails against the real pre-fix behavior (reports 2 instead of 7) rather than passing vacuously. Worth noting for reviewers: appendingLIMITto the COUNT SQL does not fail that test, because SQLite'sLIMITcaps result rows rather than counted rows — the mutation that reproduces the bug is at the call site.🤖 Generated with Claude Code