Skip to content

Commit 9a794a3

Browse files
committed
rename stuff
1 parent d9b2014 commit 9a794a3

File tree

7 files changed

+236
-135
lines changed

7 files changed

+236
-135
lines changed

packages/db/src/query/compiler/index.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function compileQuery(
5858
collections: Record<string, Collection<any, any, any, any, any>>,
5959
subscriptions: Record<string, CollectionSubscription>,
6060
callbacks: Record<string, LazyCollectionCallbacks>,
61-
lazyCollections: Set<string>,
61+
lazySources: Set<string>,
6262
optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,
6363
cache: QueryCache = new WeakMap(),
6464
queryMapping: QueryMapping = new WeakMap()
@@ -83,13 +83,16 @@ export function compileQuery(
8383
// the live layer can subscribe to every alias the optimizer introduces.
8484
const aliasToCollectionId: Record<string, string> = {}
8585

86-
// Create a map of table aliases to inputs
87-
// Note: alias keys take precedence over collection keys for input resolution
88-
const tables: Record<string, KeyedStream> = {}
86+
// Create a map of source aliases to input streams.
87+
// Note: During input resolution, alias keys take precedence over collection ID keys.
88+
// This enables per-alias subscriptions: when looking up an input stream, we first check
89+
// for `inputs[alias]` before falling back to `inputs[collectionId]`. This allows different
90+
// aliases of the same collection (e.g., self-joins) to have independent filtered streams.
91+
const sources: Record<string, KeyedStream> = {}
8992

90-
// Process the FROM clause to get the main table
93+
// Process the FROM clause to get the main source
9194
const {
92-
alias: mainTableAlias,
95+
alias: mainSource,
9396
input: mainInput,
9497
collectionId: mainCollectionId,
9598
} = processFrom(
@@ -98,19 +101,19 @@ export function compileQuery(
98101
collections,
99102
subscriptions,
100103
callbacks,
101-
lazyCollections,
104+
lazySources,
102105
optimizableOrderByCollections,
103106
cache,
104107
queryMapping,
105108
aliasToCollectionId
106109
)
107-
tables[mainTableAlias] = mainInput
110+
sources[mainSource] = mainInput
108111

109-
// Prepare the initial pipeline with the main table wrapped in its alias
112+
// Prepare the initial pipeline with the main source wrapped in its alias
110113
let pipeline: NamespacedAndKeyedStream = mainInput.pipe(
111114
map(([key, row]) => {
112115
// Initialize the record with a nested structure
113-
const ret = [key, { [mainTableAlias]: row }] as [
116+
const ret = [key, { [mainSource]: row }] as [
114117
string,
115118
Record<string, typeof row>,
116119
]
@@ -123,16 +126,16 @@ export function compileQuery(
123126
pipeline = processJoins(
124127
pipeline,
125128
query.join,
126-
tables,
129+
sources,
127130
mainCollectionId,
128-
mainTableAlias,
131+
mainSource,
129132
allInputs,
130133
cache,
131134
queryMapping,
132135
collections,
133136
subscriptions,
134137
callbacks,
135-
lazyCollections,
138+
lazySources,
136139
optimizableOrderByCollections,
137140
rawQuery,
138141
compileQuery,
@@ -193,7 +196,7 @@ export function compileQuery(
193196
map(([key, namespacedRow]) => {
194197
const selectResults =
195198
!query.join && !query.groupBy
196-
? namespacedRow[mainTableAlias]
199+
? namespacedRow[mainSource]
197200
: namespacedRow
198201

199202
return [
@@ -340,7 +343,7 @@ function processFrom(
340343
collections: Record<string, Collection>,
341344
subscriptions: Record<string, CollectionSubscription>,
342345
callbacks: Record<string, LazyCollectionCallbacks>,
343-
lazyCollections: Set<string>,
346+
lazySources: Set<string>,
344347
optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,
345348
cache: QueryCache,
346349
queryMapping: QueryMapping,
@@ -370,7 +373,7 @@ function processFrom(
370373
collections,
371374
subscriptions,
372375
callbacks,
373-
lazyCollections,
376+
lazySources,
374377
optimizableOrderByCollections,
375378
cache,
376379
queryMapping

0 commit comments

Comments
 (0)