-
Couldn't load subscription status.
- Fork 108
fix: validate against duplicate collection aliases in subqueries #719
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?
fix: validate against duplicate collection aliases in subqueries #719
Conversation
Investigated Discord bug report where using the same collection with the same alias in both a subquery and main query causes: 1. Empty results when subquery has joins 2. Aggregation cross-leaking (wrong aggregated values) ROOT CAUSE: - Parent and subquery share the same input streams from allInputs when they use the same alias - IVM stream sharing causes interference between query contexts - Caching compounds the issue by reusing compilations with wrong bindings ATTEMPTED FIXES (all unsuccessful): - Input filtering: Doesn't solve the core issue of shared streams - Fresh caching: Breaks existing caching tests and doesn't fix the bug - Both approaches together: Still fails CONCLUSION: This requires a fundamental architectural change to support independent input streams per query context. Current workaround: wrap query in an extra from() layer to avoid alias conflicts. See INVESTIGATION_SAME_COLLECTION_BUG.md for full details. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 632ef93 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-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: +372 B (+0.44%) Total Size: 84.7 kB
ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 2.89 kB ℹ️ View Unchanged
|
Implements validation to prevent the Discord bug where using the same
alias for a collection in both parent query and subquery causes empty
results or incorrect aggregation values.
The issue occurs when both parent and subquery use the same alias to
reference a collection directly (e.g., both use `vote: votesCollection`).
This causes them to share the same input stream, leading to interference.
Solution: Add validation that throws a clear DuplicateAliasInSubqueryError
when this pattern is detected.
Implementation:
- New error: DuplicateAliasInSubqueryError with clear message
- collectDirectCollectionAliases(): Collects only direct CollectionRef aliases
- validateSubqueryAliases(): Checks for conflicts before compiling subqueries
- Only validates DIRECT collection references, not QueryRef (subquery outputs)
This allows legitimate patterns like:
const sub = q.from({ issue: collection })
q.from({ issue: sub }) // OK - different scopes
But prevents problematic patterns like:
const sub = q.from({ x: coll }).join({ vote: voteColl }, ...)
q.from({ vote: voteColl }).join({ x: sub }, ...) // Error!
Users should rename the alias in either parent or subquery to fix.
Fixes Discord bug reported by JustTheSyme.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- Cleaned up INVESTIGATION_SAME_COLLECTION_BUG.md to focus on solution - Added PR_UPDATE.md with suggested title and body for PR #719 - Added changeset for the alias validation fix 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Removed investigation and PR update docs that were used during development. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Moved the validation for duplicate collection aliases in subqueries to run before query optimization rather than during compilation. This prevents false positives from optimizer-generated subqueries while still catching user-written duplicate aliases. The optimizer can legitimately create internal subqueries that reuse aliases (e.g., for predicate pushdown), but users should not be allowed to write queries with conflicting aliases between parent and subquery. Changes: - Added validateQueryStructure() in compiler/index.ts that recursively validates the entire query tree before optimization - Removed validation logic from processFrom() and processJoinSource() in joins.ts - Fixed TypeScript type errors in discord-alias-bug.test.ts All tests pass (1403 tests, 0 type errors). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Summary
Fixes a Discord bug where using the same collection alias in both a parent query and subquery causes empty results or incorrect aggregation values.
Problem
When both parent and subquery use the same alias for a direct collection reference:
Result:
Root Cause: Both queries share the same input stream, causing interference.
Solution
Added validation that throws a clear
DuplicateAliasInSubqueryErrorwhen this pattern is detected.Implementation:
collectDirectCollectionAliases(): Identifies conflicting aliasesvalidateSubqueryAliases(): Validates before compiling subqueriesUser Fix:
Simply rename the alias in either the parent or subquery:
Testing
discord-alias-bug.test.tswith comprehensive test coverageRelated
Fixes Discord bug reported by JustTheSyme.