fix(db): require all or() branches to be index-optimizable#1550
fix(db): require all or() branches to be index-optimizable#1550v-anton wants to merge 3 commits into
Conversation
📝 WalkthroughWalkthroughTightens OR-expression index optimization: optimizer now requires every OR branch be index-optimizable before using unioned index results; otherwise it falls back to a full scan. Adds a test exercising a partially indexed OR and a changeset for a patch release. ChangesIndex optimization fix for OR expressions
Sequence DiagramsequenceDiagram
participant Client
participant Optimizer
participant IndexStore
Client->>Optimizer: optimizeOrExpression(or(branch1, branch2, ...))
Optimizer->>IndexStore: optimizeQueryRecursive(branch1)
Optimizer->>IndexStore: optimizeQueryRecursive(branch2)
IndexStore-->>Optimizer: branch optimization results (canOptimize, matchingKeys)
Optimizer->>Client: return canOptimize = true AND union(matchingKeys) only if ALL branches canOptimize
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/db/src/utils/index-optimization.ts (1)
480-503:⚠️ Potential issue | 🟠 Major | ⚡ Quick winAlign OR capability check with the new all-branches requirement.
After Line [480], OR optimization requires every branch to be optimizable, but Line [502] still returns true when only some branches are optimizable. This makes
canOptimizeExpressioninconsistent withoptimizeExpressionWithIndexes.Suggested fix
function canOptimizeOrExpression< T extends object, TKey extends string | number, >(expression: BasicExpression, collection: CollectionLike<T, TKey>): boolean { if (expression.type !== `func` || expression.args.length < 2) { return false } - // If any argument can be optimized, we can gain some speedup - return expression.args.some((arg) => canOptimizeExpression(arg, collection)) + // OR is index-optimizable only when every branch is index-optimizable + return expression.args.every((arg) => canOptimizeExpression(arg, collection)) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/db/src/utils/index-optimization.ts` around lines 480 - 503, The OR capability check is too permissive: update canOptimizeOrExpression so it only returns true when every branch is optimizable (use a check like every on expression.args calling canOptimizeExpression) instead of returning true when some branches are optimizable; adjust only the boolean logic inside canOptimizeOrExpression (function name) so it matches the requirement used by optimizeExpressionWithIndexes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/db/tests/query/or-partial-optimization.test.ts`:
- Around line 39-45: The test uses explicit any in the query callbacks which
weakens type checking; remove the explicit any annotations from the callbacks
passed to query/.where and .select (e.g., the ({ item }: any) => ... parameters
used in the .from({ item: collection }), .where and .select calls) so TypeScript
can infer the correct types from the collection, or replace them with the
correct domain type if needed (e.g., ItemRow) — update the parameter signatures
for the .where and .select callbacks to omit ": any" and rely on inferred types
or the proper interface name.
---
Outside diff comments:
In `@packages/db/src/utils/index-optimization.ts`:
- Around line 480-503: The OR capability check is too permissive: update
canOptimizeOrExpression so it only returns true when every branch is optimizable
(use a check like every on expression.args calling canOptimizeExpression)
instead of returning true when some branches are optimizable; adjust only the
boolean logic inside canOptimizeOrExpression (function name) so it matches the
requirement used by optimizeExpressionWithIndexes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: cc1d3b47-bfa5-42c4-b0a3-e7efd2469ff8
📒 Files selected for processing (3)
.changeset/fix-or-partial-union.mdpackages/db/src/utils/index-optimization.tspackages/db/tests/query/or-partial-optimization.test.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/db/tests/query/or-partial-optimization.test.ts (1)
21-58: 💤 Low valueConsider adding test cases for edge scenarios.
The current test validates the core fix (partial indexing fallback) well. To strengthen coverage per testing guidelines, consider adding cases for:
- Both OR branches indexed (should use optimized union)
- Neither branch indexed (full scan fallback)
- Empty collection or single-item edge cases
These can be deferred to a follow-up if needed.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/db/tests/query/or-partial-optimization.test.ts` around lines 21 - 58, Add additional test cases around the or() partial-index behavior: create tests that use the same helpers (createCollection, createLiveQueryCollection, createIndex, BasicIndex, stateWhenReady) to cover (1) both OR branches indexed (expect optimized union behavior), (2) neither branch indexed (expect full-scan fallback), and (3) edge collections (empty collection and single-item collection) verifying liveQuery.toArray results; reuse the existing pattern (query: q => q.from({ item: collection }).where(({ item }) => or(eq(item.category, 'A'), eq(item.tag, 'x'))).select(...), startSync: true) and assert expected ids for each scenario.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/db/tests/query/or-partial-optimization.test.ts`:
- Around line 21-58: Add additional test cases around the or() partial-index
behavior: create tests that use the same helpers (createCollection,
createLiveQueryCollection, createIndex, BasicIndex, stateWhenReady) to cover (1)
both OR branches indexed (expect optimized union behavior), (2) neither branch
indexed (expect full-scan fallback), and (3) edge collections (empty collection
and single-item collection) verifying liveQuery.toArray results; reuse the
existing pattern (query: q => q.from({ item: collection }).where(({ item }) =>
or(eq(item.category, 'A'), eq(item.tag, 'x'))).select(...), startSync: true) and
assert expected ids for each scenario.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 40e74d91-0e66-4dcc-b367-4bfb4f9bc40c
📒 Files selected for processing (1)
packages/db/tests/query/or-partial-optimization.test.ts
🎯 Changes
Fixes #1502.
optimizeOrExpressionreturnedcanOptimize: truewhen only someor()branches had matching indexes, producing a partial union that silently dropped rows from unindexed branches.The fix requires all branches to be optimizable (
results.length === expression.args.length) before returning the index result. If any branch can't be optimized, fall back to full scan.✅ Checklist
pnpm test:pr.🚀 Release Impact
Summary by CodeRabbit