Skip to content

fix(query): run an expression reading an or-bound variable after the ors (#625, backport) - #630

Merged
adityamukho merged 1 commit into
mainfrom
fix/625-or-expr-v2
Oct 19, 2026
Merged

adityamukho merged 1 commit into
mainfrom
fix/625-or-expr-v2

Conversation

@adityamukho

Copy link
Copy Markdown
Collaborator

Backport of #629 (v3) to main for the next v2.0.x patch. #625 is a data-integrity known issue and stays open until that patch is published.

Problem

optimizer::plan appended an expression whose variable only an or / or-join binds to the end of the planned list, and the ors run after that list, so the expression dropped every row:

(transact [[:a :n 1] [:b :n 2] [:c :ok true]])
(query [:find ?e ?x :where [?e :n _] (or [?e :n ?x] [?e :ok ?x]) [(> ?x 1)]])
;; was: No results found.   now: :b 2

Fix (hand-port; main has no streaming engine, seeded planning or Binders)

  • optimizer::plan defers an expression that reads a variable no pattern in the list and no expression placed before it binds. Deferred expressions come first in the deferred list, in text order, ahead of the deferred nots.
  • Executor, both paths: the deferred pass after apply_or_clauses runs them via apply_expr_clauses. Expressions cost 0, so the stable cheapest-first sort keeps them ahead of the nots. Under the wasm feature there is no sort, and the list order already puts them first.
  • Rule-body evaluator: applies them after its or pass via apply_expr_clauses_in_evaluator.

Tests

  • tests/disjunction_test.rs (+2, same as on v3): the issue's query, a not after a deferred binding expression, a query calling a rule, and a rule body. Both fail without the fix.
  • optimizer unit tests (+3, replacing test_expr_unbound_var_goes_to_end).
  • cargo test: 1293 tests (1284 passing, 9 ignored). clippy (default and --all-features) and fmt are clean. The per-module coverage gates pass locally (evaluator.rs branches 85.48%, executor.rs 78.00%).
  • With --features wasm, 3 perf: short-circuit or/or-join branch evaluation using cost model #250 short-circuit tests in disjunction_test.rs fail on main with and without this change. They were already failing before this PR.

Review

Codex (read-only): no correctness bug found.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PtCdzbJMSS1V8qEPaJ4jXz

…ors (#625, backport)

Backport of #629 from v3. optimizer::plan appended an expression whose
variable only an or / or-join binds to the end of the planned list, and
the ors run after that list, so the expression saw the variable unbound
and dropped every row, in queries and in rule bodies.

plan now defers an expression that reads a variable no pattern in the
list and no expression placed before it binds, ahead of the deferred
nots and in text order. Both executor deferred passes run it after
apply_or_clauses (via apply_expr_clauses; expressions cost 0, so the
stable cheapest-first sort keeps them first), and the rule-body
evaluator applies it after its or pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PtCdzbJMSS1V8qEPaJ4jXz
@adityamukho
adityamukho merged commit e72febd into main Oct 19, 2026
21 checks passed
@adityamukho
adityamukho deleted the fix/625-or-expr-v2 branch October 19, 2026 14:30
@codecov

codecov Bot commented Oct 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/query/datalog/optimizer.rs 90.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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.

1 participant