extractSimpleComparisons returns value: null when the right side of a comparison isn't a literal

Open Beginner friendly
#2,381 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
data

Research direction

The fallback to null is in packages/db/src/query/expression-helpers.ts near lines 390 and 472, and the guard that throws 'Found complex expression' is just after it. Start by reproducing the three calls in the issue against extractSimpleComparisons. Done means those calls throw for non-literal right-hand sides while eq(row.a, null) still returns value: null, with a test covering both cases.

Written by the indexing model from the issue text.

Description

  • I've validated the bug against the latest version of DB packages

Describe the bug

extractSimpleComparisons is documented to throw on expressions it can't represent as a simple comparison, but a comparison whose right side is a column or a function doesn't throw. It returns value: null, so a queryFn built on it sends a filter against null to the backend with no error.

When the right side isn't a literal it falls back to null (expression-helpers.ts:390 and :472), and the guard after it only rejects undefined, so the "Found complex expression" error can't fire for this case.

To Reproduce

import { IR, extractSimpleComparisons } from "@tanstack/db"

const a = new IR.PropRef(["a"])
const b = new IR.PropRef(["b"])

console.log(extractSimpleComparisons(new IR.Func("eq", [a, b])))
console.log(extractSimpleComparisons(new IR.Func("eq", [a, new IR.Func("lower", [b])])))
console.log(extractSimpleComparisons(new IR.Func("not", [new IR.Func("gt", [a, b])])))

This prints:

[ { field: [ 'a' ], operator: 'eq', value: null } ]
[ { field: [ 'a' ], operator: 'eq', value: null } ]
[ { field: [ 'a' ], operator: 'not_gt', value: null } ]

The same happens through a live query. On an on-demand collection, .where(({ row }) => eq(row.a, lower(row.b))) passes loadSubset a where that extractSimpleComparisons reads as { field: ['a'], operator: 'eq', value: null }.

Expected behavior

These calls throw extractSimpleComparisons requires simple field-value comparisons. Found complex expression for 'eq' operator. (or 'not(gt)'), as they would for any other expression it can't represent. A literal null, such as eq(row.a, null), should keep returning value: null.

Additional context

@tanstack/db 1.0.0-rc.1.4, Node 24.12.0. The lines above are unchanged on main at 6ec538ecd.

Falling back to undefined instead of null at those two lines would let the existing guard throw. I can open a PR if that looks right to you.

Dominant language
TypeScript
Stars
4k
Forks
286
Avg merge
1d 1h
Merged PRs (30d)
104

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TanStack/db

All issues in TanStack/db

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.