Skip to content

Commit 04688eb

Browse files
committed
remove fallback to collecitonId
1 parent 9a794a3 commit 04688eb

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ export interface CompilationResult {
4545
}
4646

4747
/**
48-
* Compiles a query2 IR into a D2 pipeline
48+
* Compiles a query IR into a D2 pipeline
4949
* @param rawQuery The query IR to compile
50-
* @param inputs Mapping of collection names to input streams
50+
* @param inputs Mapping of source aliases to input streams (e.g., `{ employee: input1, manager: input2 }`)
51+
* @param collections Mapping of collection IDs to Collection instances
52+
* @param subscriptions Mapping of source aliases to CollectionSubscription instances
53+
* @param callbacks Mapping of source aliases to lazy loading callbacks
54+
* @param lazySources Set of source aliases that should load data lazily
55+
* @param optimizableOrderByCollections Map of collection IDs to order-by optimization info
5156
* @param cache Optional cache for compiled subqueries (used internally for recursion)
5257
* @param queryMapping Optional mapping from optimized queries to original queries
53-
* @returns A CompilationResult with the pipeline and collection WHERE clauses
58+
* @returns A CompilationResult with the pipeline, source WHERE clauses, and alias metadata
5459
*/
5560
export function compileQuery(
5661
rawQuery: QueryIR,
@@ -84,10 +89,9 @@ export function compileQuery(
8489
const aliasToCollectionId: Record<string, string> = {}
8590

8691
// 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.
92+
// Inputs MUST be keyed by alias (e.g., `{ employee: input1, manager: input2 }`),
93+
// not by collection ID. This enables per-alias subscriptions where different aliases
94+
// of the same collection (e.g., self-joins) maintain independent filtered streams.
9195
const sources: Record<string, KeyedStream> = {}
9296

9397
// Process the FROM clause to get the main source
@@ -351,7 +355,7 @@ function processFrom(
351355
): { alias: string; input: KeyedStream; collectionId: string } {
352356
switch (from.type) {
353357
case `collectionRef`: {
354-
const input = allInputs[from.alias] ?? allInputs[from.collection.id]
358+
const input = allInputs[from.alias]
355359
if (!input) {
356360
throw new CollectionInputNotFoundError(
357361
from.alias,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ function processJoinSource(
431431
): { alias: string; input: KeyedStream; collectionId: string } {
432432
switch (from.type) {
433433
case `collectionRef`: {
434-
const input = allInputs[from.alias] ?? allInputs[from.collection.id]
434+
const input = allInputs[from.alias]
435435
if (!input) {
436436
throw new CollectionInputNotFoundError(
437437
from.alias,

packages/db/tests/query/compiler/subqueries.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ describe(`Query2 Subqueries`, () => {
172172
const issuesInput = createIssueInput(graph)
173173
const { pipeline } = compileQuery(
174174
builtQuery,
175-
{ issues: issuesInput },
175+
{ issue: issuesInput },
176176
{ issues: issuesCollection },
177177
{},
178178
{},
@@ -286,12 +286,12 @@ describe(`Query2 Subqueries`, () => {
286286
const compilation = compileQuery(
287287
builtQuery,
288288
{
289-
issues: issuesInput,
290-
users: usersInput,
289+
issue: issuesInput,
290+
user: usersInput,
291291
},
292292
{ issues: issuesCollection, users: usersCollection },
293293
subscriptions,
294-
{ issues: dummyCallbacks, users: dummyCallbacks },
294+
{ issue: dummyCallbacks, user: dummyCallbacks },
295295
lazySources,
296296
{}
297297
)
@@ -395,12 +395,12 @@ describe(`Query2 Subqueries`, () => {
395395
const compilation = compileQuery(
396396
builtQuery,
397397
{
398-
issues: issuesInput,
399-
users: usersInput,
398+
issue: issuesInput,
399+
user: usersInput,
400400
},
401401
{ issues: issuesCollection, users: usersCollection },
402402
subscriptions,
403-
{ issues: dummyCallbacks, users: dummyCallbacks },
403+
{ issue: dummyCallbacks, user: dummyCallbacks },
404404
lazyCollections,
405405
{}
406406
)
@@ -441,7 +441,7 @@ describe(`Query2 Subqueries`, () => {
441441
const issuesInput = createIssueInput(graph)
442442
const { pipeline } = compileQuery(
443443
builtQuery,
444-
{ issues: issuesInput },
444+
{ issue: issuesInput },
445445
{ issues: issuesCollection },
446446
{},
447447
{},

packages/db/tests/query/compiler/subquery-caching.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ describe(`Subquery Caching`, () => {
5050
},
5151
}
5252

53-
// Set up D2 inputs
53+
// Set up D2 inputs - keyed by alias, not collection ID
5454
const graph = new D2()
5555
const userInput = graph.newInput<[number, any]>()
56-
const inputs = { users: userInput }
56+
const inputs = { u: userInput }
5757

5858
// Test: Compile the main query twice - first without shared cache, then with shared cache
5959

@@ -159,7 +159,7 @@ describe(`Subquery Caching`, () => {
159159

160160
const graph = new D2()
161161
const userInput = graph.newInput<[number, any]>()
162-
const inputs = { users: userInput }
162+
const inputs = { u: userInput }
163163

164164
// Create a shared cache
165165
const sharedCache = new WeakMap()
@@ -216,7 +216,7 @@ describe(`Subquery Caching`, () => {
216216

217217
const graph = new D2()
218218
const userInput = graph.newInput<[number, any]>()
219-
const inputs = { users: userInput }
219+
const inputs = { u: userInput }
220220

221221
const sharedCache = new WeakMap()
222222

@@ -287,7 +287,7 @@ describe(`Subquery Caching`, () => {
287287

288288
const graph = new D2()
289289
const userInput = graph.newInput<[number, any]>()
290-
const inputs = { users: userInput }
290+
const inputs = { u: userInput }
291291

292292
const sharedCache = new WeakMap()
293293

0 commit comments

Comments
 (0)