On-demand collections drop rows for not(isUndefined(column)): the Electric and PowerSync compilers translate isUndefined to IS NULL
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- postgresql, sqlite, typescript
- Lĩnh vực
- database
Hướng nghiên cứu
Begin at packages/db/src/query/compiler/evaluators.ts (isUndefined is === undefined) and compare the negation mapping in packages/electric-db-collection/src/sql-compiler.ts (L196-L214, L351) and packages/powersync-db-collection/src/sqlite-compiler.ts (L222-L243); the mapping is pinned in packages/electric-db-collection/tests/sql-compiler.test.ts L477-L484. Run the issue's repro first, then decide which side changes — the payload leaves that open. Done: not(isUndefined(note)) returns row 2 in both sync modes on both collections, with compiler tests updated.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
- I've validated the bug against the latest version of DB packages
Describe the bug
The same live query returns different rows on an eager and on an on-demand collection when it contains not(isUndefined(column)). The on-demand collection never receives the rows whose column is NULL. This happens with both @tanstack/electric-db-collection and @tanstack/powersync-db-collection.
Cause: the live query engine and the SQL compilers give isUndefined different meanings.
- In memory,
isUndefined(v)isv === undefined, andisNull(v)isv === null. ANULLcolumn reaches the query asnull, soisUndefined(row.note)isfalsefor it andnot(isUndefined(row.note))istrue. - The compilers turn
isUndefinedintoIS NULL: Electric (not(isUndefined(x))becomesx IS NOT NULL, L196-L214), and PowerSync. The Electric mapping is pinned by a unit test, so it looks intentional.
The optimizer pushes the predicate into loadSubset, so the source is asked for "note" IS NOT NULL and the NULL rows are never loaded, although the query itself says they match.
Only negated forms lose rows. A positive isUndefined(col) makes the source send extra rows, which the query then filters out. isNull is consistent in both modes.
To Reproduce
Electric (Postgres 16 + electricsql/electric:1.6.0):
docker-compose.yml
services:
postgres:
image: postgres:16
command: ["-c", "wal_level=logical"]
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: repro
ports: ["54399:5432"]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d repro"]
interval: 1s
retries: 30
electric:
image: electricsql/electric:1.6.0
environment:
DATABASE_URL: postgresql://postgres:password@postgres:5432/repro?sslmode=disable
ELECTRIC_INSECURE: "true"
ports: ["3199:3000"]
depends_on:
postgres:
condition: service_healthy
repro.ts (docker compose up -d --wait && bun add @tanstack/[email protected] @tanstack/[email protected] pg && bun repro.ts)
import { createCollection, createLiveQueryCollection, isNull, isUndefined, not } from "@tanstack/db";
import { electricCollectionOptions } from "@tanstack/electric-db-collection";
import pg from "pg";
const pool = new pg.Pool({ connectionString: "postgresql://postgres:password@localhost:54399/repro" });
await pool.query(`DROP TABLE IF EXISTS notes`);
await pool.query(`CREATE TABLE notes (id integer PRIMARY KEY, note text)`);
await pool.query(`INSERT INTO notes VALUES (1, 'has a note'), (2, NULL)`);
type Note = { id: number; note: string | null };
const notes = (id: string, syncMode: "eager" | "on-demand") =>
createCollection(
electricCollectionOptions({
id,
getKey: (row: Note) => row.id,
shapeOptions: { url: "http://localhost:3199/v1/shape", params: { table: "notes" } },
syncMode,
}),
);
const predicates = {
"isUndefined(note)": (note: any) => isUndefined(note),
"not(isUndefined(note))": (note: any) => not(isUndefined(note)),
"isNull(note)": (note: any) => isNull(note),
"not(isNull(note))": (note: any) => not(isNull(note)),
};
for (const [name, predicate] of Object.entries(predicates)) {
const ids: Record<string, Array<number>> = {};
for (const syncMode of ["eager", "on-demand"] as const) {
const query = createLiveQueryCollection({
startSync: true,
query: (q) =>
q
.from({ n: notes(`notes-${syncMode}-${name}`, syncMode) })
.where(({ n }) => predicate(n.note))
.select(({ n }) => ({ id: n.id })),
});
await query.preload();
await new Promise((resolve) => setTimeout(resolve, 1500));
ids[syncMode] = query.toArray.map((row) => row.id).sort();
}
const same = JSON.stringify(ids.eager) === JSON.stringify(ids["on-demand"]);
console.log(`${name.padEnd(24)} eager: ${JSON.stringify(ids.eager).padEnd(6)} on-demand: ${JSON.stringify(ids["on-demand"]).padEnd(6)} ${same ? "" : "<-- DIFFERENT"}`);
}
await pool.end();
process.exit(0);
Output:
isUndefined(note) eager: [] on-demand: []
not(isUndefined(note)) eager: [1,2] on-demand: [1] <-- DIFFERENT
isNull(note) eager: [2] on-demand: [2]
not(isNull(note)) eager: [1] on-demand: [1]
PowerSync (no service needed, a local SQLite file)
bun add @tanstack/[email protected] @tanstack/[email protected] @powersync/[email protected] @powersync/[email protected] && node repro-powersync.ts
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { PowerSyncDatabase, Schema, Table, column } from "@powersync/node";
import { createCollection, createLiveQueryCollection, isNull, isUndefined, not } from "@tanstack/db";
import { powerSyncCollectionOptions } from "@tanstack/powersync-db-collection";
const APP_SCHEMA = new Schema({ notes: new Table({ note: column.text }) });
const db = new PowerSyncDatabase({
database: { dbFilename: `repro-${randomUUID()}.sqlite`, dbLocation: tmpdir(), implementation: { type: "node:sqlite" } },
schema: APP_SCHEMA,
});
await db.disconnectAndClear();
await db.execute(`INSERT INTO notes (id, note) VALUES ('1', 'has a note'), ('2', NULL)`);
const predicates = {
"isUndefined(note)": (note: any) => isUndefined(note),
"not(isUndefined(note))": (note: any) => not(isUndefined(note)),
"isNull(note)": (note: any) => isNull(note),
"not(isNull(note))": (note: any) => not(isNull(note)),
};
for (const [name, predicate] of Object.entries(predicates)) {
const ids: Record<string, Array<string>> = {};
for (const syncMode of ["eager", "on-demand"] as const) {
const collection = createCollection(
powerSyncCollectionOptions({ database: db, table: APP_SCHEMA.props.notes, syncMode }),
);
const query = createLiveQueryCollection({
startSync: true,
query: (q) =>
q
.from({ n: collection })
.where(({ n }) => predicate(n.note))
.select(({ n }) => ({ id: n.id })),
});
await query.preload();
await new Promise((resolve) => setTimeout(resolve, 500));
ids[syncMode] = query.toArray.map((row) => row.id).sort();
}
const same = JSON.stringify(ids.eager) === JSON.stringify(ids["on-demand"]);
console.log(`${name.padEnd(24)} eager: ${JSON.stringify(ids.eager).padEnd(10)} on-demand: ${JSON.stringify(ids["on-demand"]).padEnd(10)} ${same ? "" : "<-- DIFFERENT"}`);
}
await db.close();
process.exit(0);
isUndefined(note) eager: [] on-demand: []
not(isUndefined(note)) eager: ["1","2"] on-demand: ["1"] <-- DIFFERENT
isNull(note) eager: ["2"] on-demand: ["2"]
not(isNull(note)) eager: ["1"] on-demand: ["1"]
Expected behavior
A live query returns the same rows whatever the collection's syncMode. Here not(isUndefined(note)) should include row 2 on the on-demand collection, as it does on the eager one.
Additional context
- Versions:
@tanstack/db,@tanstack/electric-db-collectionand@tanstack/powersync-db-collection1.0.0-rc.1.5 (the latest published; the code above is the same onmainat 21c52a0),@electric-sql/client1.5.28,@powersync/node0.21.0, Node 24.14, Bun 1.4. - I did not find an existing issue for this. #574 and #685 are about
isUndefinedin left-join filters, which is a different case. - Which side should change is a design question, so I haven't picked one. Compiling
isUndefined(column)to a constantFALSEwould be wrong for a collection whose schema turnsNULLintoundefined(thenIS NULLis correct). Making the evaluator treatnullasundefinedwould change left-join behaviour. What does not depend on the schema is keeping predicates that containisUndefinedout ofloadSubset'swhere, so the source loads a superset, but I haven't tried that in the optimizer. - Happy to send a PR once there is a preferred direction.
- Ngôn ngữ chính
- TypeScript
- Star
- 4k
- Fork
- 286
- Merge trung bình
- 1 ngày 1 giờ
- Pull request đã merge (30 ngày)
- 104
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/db
-
extractSimpleComparisons returns value: null when the right side of a comparison isn't a literalĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`avg`, `min`, `max` and `sum` treat `null` values as `0`Có thể đã có người làm @oyobbeb đã nhận 8 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
`Collection.currentStateAsChanges` JSDoc examples don’t match `CurrentStateAsChangesOptions`Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Two @tanstack/db tests time out under CPU load because a cold module import counts against the 5 s default timeoutCó thể đã có người làm @jp-knj đã nhận 20 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
vercel-labs/ui#809 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bot:ai-assisted component:platform-js status:untriaged
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
midnightntwrk/platform-js#38 ·
-
[bug] Second false positive on high-cost plugin alert: detection only compares patch entry id, community plugin dsh-auto | [bug] 高成本插件告警第二次误报:检测只比对 patch 条目 id,社区插件 dsh-auto-review 被认成 @deepseek-ai/dsh-experimental-auto-reviewCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ranxianglei/billion-context#7068 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
vendor: bump whisper.cpp past 6a8addd — the current pin aborts the app on a failed Metal initĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
mybigday/whisper.rn#348 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
six-group/six-webcomponents#600 ·