Skip to content

Support OR in WHERE - #11

Merged
jessestimpson merged 1 commit into
mainfrom
claude/or-conditions
Sep 27, 2026
Merged

jessestimpson merged 1 commit into
mainfrom
claude/or-conditions

Conversation

@jessestimpson

Copy link
Copy Markdown
Contributor

Summary

OR is now supported in WHERE, and so is NOT over AND (not (a and b) is not a or not b).

  • Predicate shape: {:or, branches}. Each branch is an AND-ed list of predicates, so AND binds tighter than OR and nesting is kept. Efsql.Parser flattens chains of ORs into a single predicate.
  • Evaluation: an OR is checked on the rows read (pushdown is :filter). The conditions AND-ed with it still narrow the read.
  • NULLs: a NULL fails only its own branch, as in SQL. A row whose status is NULL still matches status = 'paid' or total > 100 when its total is over 100.
  • OR to IN: a new Efsql.Rewrite pass turns an OR of equalities and INs on one field into an IN. An index, or the primary key _, then serves it with one lookup per value.
  • Duplicate IN values: pass_in_singleton now deduplicates IN and NOT IN values using Types.equality_key. Before this, each value was read on its own, so a in (1, 1) returned its rows twice. OR made this easier to hit (a = 1 or a in (1, 2)).
  • Predicate.fields/1: returns every field a predicate reads. It is used for the planner's residual columns, the check on the primary key _, and the _tenant checks.
  • Across tenants: an OR on _tenant alone picks the tenants. An OR that mixes _tenant with other fields is refused with a clear message, because each tenant's rows are filtered before they get _tenant.
  • Primary key _: only an OR of equalities or INs is supported. The error says to use the key's field name for anything else.
  • Docs and UI: the README has a new OR section and no longer lists OR as unsupported. The TUI help and completion now include or.

Tests

  • test/or_test.exs (FoundationDB):
    • an OR across fields;
    • NULL per branch;
    • precedence and parentheses;
    • NOT over AND;
    • union reads for an OR of equalities on an indexed field or on keys;
    • a repeated IN value is read once;
    • ORDER BY and LIMIT after an OR;
    • the error for _ inside an OR.
  • test/cross_tenant_test.exs: an OR on _tenant picks the tenants; mixing _tenant with other fields is refused.
  • Parser, rewrite, predicate, completion and TUI help unit tests.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko


Generated by Claude Code

An OR becomes one predicate, {:or, branches}, each branch an AND-ed list
of predicates, so AND binds tighter and nesting is kept. It is checked on
the rows read, with SQL NULL semantics per branch, while the conditions
AND-ed with it still narrow the read. NOT over AND now works too, as the
OR it means.

An OR of equalities and INs on one field is rewritten to an IN, so an
index or the primary key serves it as one lookup per value. IN lists are
now deduplicated, since each value is read on its own and a repeated one
returned its rows twice.

Predicate.fields/1 gives every field a predicate reads, for the planner's
residual columns, the primary-key check and _tenant. In a query across
tenants an OR on _tenant alone picks the tenants; mixing _tenant with
other fields is refused, as each tenant's rows are filtered before they
get _tenant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PFBZUrerge6bbWVCHpS5Ko
@jessestimpson
jessestimpson merged commit 1d8bb79 into main Sep 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants