Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Correlated toArray over a distinct derived query drops children from earlier parents

Abierto
#2,273 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
58/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
typescript
Área
database

Línea de trabajo

Start by running the provided reproduction with Bun and the listed package versions, then trace how .distinct() results are consumed by a correlated toArray() query. Compare the two loop iterations and check which parent’s children disappear. Done when both iterations return the expected rows for both parents, with a regression test covering distinct derived queries used as nested sources.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

A correlated toArray() loses rows from an earlier parent when its source is a derived query with .distinct(). The same query without .distinct() returns all expected rows, even though the input contains no duplicates.

Reproduced with @tanstack/[email protected] / @tanstack/[email protected], Bun 1.4.4, macOS. No React rendering or external sync service is needed.

Run this as bun repro.mjs in a project with @tanstack/[email protected] installed:

import { createCollection, Query, eq, toArray, queryOnce } from '@tanstack/react-db';

function coll(id, rows) {
  return createCollection({
    id, getKey: r => r.id, startSync: true,
    sync: { sync: ({ begin, write, commit, markReady }) => {
      begin();
      for (const value of rows) write({ type: 'insert', value });
      commit();
      markReady();
    } },
  });
}

const groups = coll('groups', [{ id: 'a' }, { id: 'b' }]);
const items = coll('items', [
  { id: 'a1', group: 'a', value: '1' },
  { id: 'a2', group: 'a', value: '2' },
  { id: 'b1', group: 'b', value: '1' },
  { id: 'b2', group: 'b', value: '2' },
]);

for (const dedup of [false, true]) {
  let derived = new Query().from({ row: items })
    .select(({ row }) => ({ group: row.group, value: row.value }));
  if (dedup) derived = derived.distinct();
  const query = new Query().from({ g: groups }).select(({ g }) => ({
    id: g.id,
    items: toArray(new Query().from({ item: derived })
      .where(({ item }) => eq(item.group, g.id))
      .select(({ item }) => ({ value: item.value }))),
  }));
  console.log(dedup, JSON.stringify((await queryOnce({ query }))
    .map(({ id, items }) => ({ id, items }))));
}

Expected for both iterations:

[{"id":"a","items":[{"value":"1"},{"value":"2"}]},{"id":"b","items":[{"value":"1"},{"value":"2"}]}]

Actual with dedup=true:

[{"id":"a","items":[]},{"id":"b","items":[{"value":"1"},{"value":"2"}]}]

The derived rows differ by group, so they should remain distinct. This silently omits children when multiple parents have equal projected child values. Materializing the distinct derived query into a live-query collection before using it as the nested source avoids the omission in testing, but requires a separate collection lifecycle.

I checked #1709 (join scan cost) and #1975 (reused aliases). This reproduction has no join and uses different aliases, so it appears to be a separate correctness issue.

Lenguaje dominante
TypeScript
Estrellas
4k
Forks
277
Merge medio
1 d 2 h
PR fusionados (30 d)
213

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/db

Todos los issues de TanStack/db

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.