Skip to content
Source
Sign inSign up
TanStack/db

Live query returns no rows when `materialize()` reuses an alias joined inside a `from()` sub-query (regression in 0.11.0) #1975

ClosedJaime02(GitHub profile, opens in a new tab) opened this issue 0 comments

Disclaimer: bug found & reported by Claude 😅

Describe the bug

Since @tanstack/db 0.11.0, a live query returns no rows under all of these conditions:

  1. A from() sub-query has a leftJoin({ ref: ... }) and two or more filter predicates (separate .where() calls or one and(...)).
  2. The outer query selects a materialize() sub-query that uses the same alias (ref) as that join.

On 0.9.2 the same query returns the expected rows. 0.9.2 is the previous published version, so the regression is somewhere in 0.9.2 → 0.11.0.

Reproduction

Needs only @tanstack/db. Run with bun repro.ts or npx tsx repro.ts.

import { createCollection, createLiveQueryCollection, localOnlyCollectionOptions, eq, materialize } from "@tanstack/db";

const parts = createCollection(
  localOnlyCollectionOptions<{ id: number; active: boolean }>({
    id: "parts",
    getKey: (p) => p.id,
    initialData: [{ id: 1, active: true }, { id: 2, active: true }],
  }),
);
const refs = createCollection(
  localOnlyCollectionOptions<{ id: number; partId: number; clientId: number }>({
    id: "refs",
    getKey: (r) => r.id,
    initialData: [{ id: 10, partId: 1, clientId: 1 }, { id: 20, partId: 2, clientId: 2 }],
  }),
);

const live = createLiveQueryCollection({
  startSync: true,
  query: (q) => {
    const filteredParts = q
      .from({ part: parts })
      .leftJoin({ ref: refs }, ({ part, ref }) => eq(ref.partId, part.id))
      .where(({ ref }) => eq(ref.clientId, 1))
      .where(({ part }) => eq(part.active, true))
      .select(({ part }) => part);

    return q.from({ part: filteredParts }).select(({ part }) => ({
      ...part,
      // Same alias ("ref") as the join inside the sub-query above
      refs: materialize(q.from({ ref: refs }).where(({ ref }) => eq(ref.partId, part.id))),
    }));
  },
});

await live.preload();
console.log(live.toArray.map((r) => r.id)); // expected [1]

Expected behavior

[ 1 ]: part 1, with its refs materialized.

Actual behavior

@tanstack/dbOutput
0.9.2[ 1 ]
0.11.0[]

No error or warning is logged. The rows are silently dropped.

What triggers it / what doesn't

All tested on 0.11.0:

VariationResult
As above (shared alias + 2 predicates)[] ❌
Same, but the 2 predicates in a single .where(and(...))[] ❌
Shared alias + only one predicateOK
Rename the materialize() alias (e.g. partRef) — 2 predicatesOK
materialize() over a different collectionOK
Outer select without materialize()OK
Adding/removing distinct(), orderBy(), limit()/offset()no effect

Because of this, I think the predicate pushdown or alias handling added in 0.11.0 is resolving the materialize() sub-query's ref against the join alias of the same name in the parent's from() sub-query.

Workaround

Use different aliases for the join and the materialize() sub-query.

Environment

  • @tanstack/db 0.11.0 (via @tanstack/react-db 0.5.0). Works on 0.9.2 (via @tanstack/react-db 0.4.1).
  • Reproduced with localOnlyCollectionOptions. Originally found with @tanstack/electric-db-collection 0.5.1.
  • Bun 1.x, Windows 11.
  1. KyleAMathews closed this

Sign in to join this conversation.