feat: support aggregates in subqueries#1298
Open
kevin-dp wants to merge 3 commits intokevin/includes-to-arrayfrom
Open
feat: support aggregates in subqueries#1298kevin-dp wants to merge 3 commits intokevin/includes-to-arrayfrom
kevin-dp wants to merge 3 commits intokevin/includes-to-arrayfrom
Conversation
🦋 Changeset detectedLatest commit: bf9489e 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/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: |
Contributor
|
Size Change: +130 B (+0.13%) Total Size: 96.4 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 3.7 kB ℹ️ View Unchanged
|
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
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
count) in child/includes subqueries, asserting per-parent behaviorprocessGroupByto preserve__correlationKeythrough GROUP BY, so aggregates in child queries compute per-parent rather than globallyProblem
When using aggregates like
count(i.id)inside an includes subquery, the correlation key that routes child results back to the correct parent was lost after the GROUP BY operator reshaped rows. This caused:selectwith aggregates, nogroupBy): all child rows across ALL parents aggregated into one global resultundefinedforcorrelationKey, so results couldn't be routed to parent CollectionsApproach
Unlike
orderBy+limit(which required a newgroupedOrderByWithFractionalIndexoperator because limiting is a post-grouping operation that needs explicit per-parent partitioning), aggregates don't need a new "grouped groupBy" operator. The existinggroupByoperator already handles per-parent aggregation naturally — we just need to include__correlationKeyin the grouping key so that rows from different parents end up in separate groups.Concretely:
mainSourcetoprocessGroupBywhen in includes mode__correlationKeyin the grouping key so rows from different parents stay in separate groups (e.g.{ __singleGroup: true, __correlationKey: 1 }instead of just{ __singleGroup: true }){ [mainSource]: { __correlationKey } }structure on output rows so the existing output extraction logic can route results back to the correct parentTest plan
🤖 Generated with Claude Code