-
Notifications
You must be signed in to change notification settings - Fork 107
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
Open
KyleAMathews
wants to merge
5
commits into
main
Choose a base branch
from
claude/investigate-discord-bug-011CUSTFzsoVoJZ3rpWh3QSZ
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix: validate against duplicate collection aliases in subqueries #719
KyleAMathews
wants to merge
5
commits into
main
from
claude/investigate-discord-bug-011CUSTFzsoVoJZ3rpWh3QSZ
+216
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>
🦋 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 <[email protected]>
- 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 <[email protected]>
Removed investigation and PR update docs that were used during development. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
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 <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.