-
Notifications
You must be signed in to change notification settings - Fork 161
test: Add regression test for OR queries with mixed indexed/non-indexed fields #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This test demonstrates a bug where using `or()` with one indexed field and one non-indexed field causes items matching the non-indexed condition to be silently dropped. Example: `or(eq(element.id, itemId), eq(element.group_id, itemId))` where 'id' has an index but 'group_id' does not. Expected: Items matching EITHER condition should be returned Actual: Only items matching the indexed condition are returned Root cause is in `packages/db/src/utils/index-optimization.ts`: - `canOptimizeOrExpression` returns true if ANY operand can be optimized - `optimizeOrExpression` only collects results from optimizable branches - Non-optimizable branches are silently dropped The fix should change OR optimization to require ALL branches be optimizable, falling back to full scan otherwise. https://claude.ai/code/session_01R16FFtQbQr1VXjEVCYAeUk
|
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: 0 B Total Size: 90.9 kB ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.7 kB ℹ️ View Unchanged
|
Auto-indexing only recurses into AND expressions, not OR expressions. This means fields used inside OR don't get auto-indexed, which combined with the optimizer bug (dropping non-indexed OR branches), causes silent data loss in queries. This test demonstrates what SHOULD happen: auto-indexing should create indexes for all fields in OR expressions. https://claude.ai/code/session_01R16FFtQbQr1VXjEVCYAeUk
🎯 Changes
Bug reported in https://discord.com/channels/719702312431386674/1465855364502126662/1465855364502126662
Added a regression test that demonstrates a bug in OR query handling when mixing indexed and non-indexed fields. The test documents the issue where items matching non-indexed field conditions are silently dropped when combined with indexed field conditions in an OR query.
Bug Description:
When using
or(eq(element.id, itemId), eq(element.group_id, itemId))whereidhas an index butgroup_iddoes not, only items matching the indexed condition are returned. Items matching the non-indexed condition are silently dropped.Expected Behavior:
Items matching EITHER condition should be returned. When not all OR branches can be optimized with indexes, the query should fall back to a full scan to ensure all matching items are found.
Test Details:
age=25 OR name='Bob'agefield has an index,namefield does notThis test will fail until the underlying bug is fixed in the query optimization logic.
✅ Checklist
pnpm test:pr.🚀 Release Impact
Note: This is a test-only change that documents an existing bug. No changeset is needed.
https://claude.ai/code/session_01R16FFtQbQr1VXjEVCYAeUk