MCPcopy Index your code
hub / github.com/TanStack/db / groupWhereClauses

Function groupWhereClauses

packages/db/src/query/optimizer.ts:666–702  ·  view source on GitHub ↗

* Step 3: Group WHERE clauses by the sources they touch. * * Single-source clauses can be pushed down to subqueries for optimization. * Multi-source clauses must remain in the main query to preserve join semantics. * * @param analyzedClauses - Array of analyzed WHERE clauses * @returns Grouped

(
  analyzedClauses: Array<AnalyzedWhereClause>,
)

Source from the content-addressed store, hash-verified

664 * @returns Grouped clauses ready for optimization
665 */
666function groupWhereClauses(
667 analyzedClauses: Array<AnalyzedWhereClause>,
668): GroupedWhereClauses {
669 const singleSource = new Map<string, Array<BasicExpression<boolean>>>()
670 const multiSource: Array<BasicExpression<boolean>> = []
671
672 // Categorize each clause based on how many sources it touches
673 for (const clause of analyzedClauses) {
674 if (clause.touchedSources.size === 1 && !clause.hasNamespaceOnlyRef) {
675 // Single source clause without namespace-only references - can be optimized
676 const source = Array.from(clause.touchedSources)[0]!
677 if (!singleSource.has(source)) {
678 singleSource.set(source, [])
679 }
680 singleSource.get(source)!.push(clause.expression)
681 } else if (clause.touchedSources.size > 1 || clause.hasNamespaceOnlyRef) {
682 // Multi-source clause or namespace-only reference - must stay in main query
683 multiSource.push(clause.expression)
684 }
685 // Skip clauses that touch no sources (constants) - they don't need optimization
686 }
687
688 // Combine multiple clauses for each source with AND
689 const combinedSingleSource = new Map<string, BasicExpression<boolean>>()
690 for (const [source, clauses] of singleSource) {
691 combinedSingleSource.set(source, combineWithAnd(clauses))
692 }
693
694 // Combine multi-source clauses with AND
695 const combinedMultiSource =
696 multiSource.length > 0 ? combineWithAnd(multiSource) : undefined
697
698 return {
699 singleSource: combinedSingleSource,
700 multiSource: combinedMultiSource,
701 }
702}
703
704/**
705 * Step 4: Apply optimizations by lifting single-source clauses into subqueries.

Callers 2

Calls 5

combineWithAndFunction · 0.85
fromMethod · 0.80
hasMethod · 0.45
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected