feat: support aggregates in subqueries#1298
Merged
kevin-dp merged 9 commits intokevin/includesfrom Mar 12, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: ac2022e 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: +127 B (+0.13%) Total Size: 98.6 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 3.85 kB ℹ️ View Unchanged
|
1 task
samwillis
approved these changes
Mar 9, 2026
f491fd4 to
4c41062
Compare
bf9489e to
939f886
Compare
toArray() wraps an includes subquery so the parent row contains Array<T> instead of Collection<T>. When children change, the parent row is re-emitted with a fresh array snapshot. - Add ToArrayWrapper class and toArray() function - Add materializeAsArray flag to IncludesSubquery IR node - Detect ToArrayWrapper in builder, pass flag through compiler - Re-emit parent rows on child changes for toArray entries - Add SelectValue type support for ToArrayWrapper - Add tests for basic toArray, reactivity, ordering, and limits Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Test the reactive model difference between Collection and toArray includes: - Collection includes: child change does NOT re-emit the parent row (the child Collection updates in place) - toArray includes: child change DOES re-emit the parent row (the parent row is re-emitted with the updated array snapshot) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
939f886 to
a6bad2a
Compare
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