From afb52ed81fa00090a1163ce860af1903e8c2f7a5 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 16 Oct 2026 15:12:26 +0530 Subject: [PATCH 1/2] fix(not-join): bind the query's valid time for :db/valid-at on the per-row path (#615) evaluate_not_join matched the body with PatternMatcher::from_slice, so a :db/valid-at pattern bound nil when the body held an expression, sat in an or branch, or ran on the rules query path. It now takes the valid-at value and uses from_slice_with_valid_at, as not_body_matches does. The StratifiedEvaluator passes Value::Null, matching how it matches rule bodies. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PtCdzbJMSS1V8qEPaJ4jXz (cherry picked from commit 90f862c088b8d9f4bb6cd8e8c01b4e1fcabcd250) --- CHANGELOG.md | 4 ++ src/query/datalog/evaluator.rs | 8 +++- src/query/datalog/executor.rs | 34 ++++++++++++++-- tests/not_join_test.rs | 73 ++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e5fcb333e..1a014966d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- **A `:db/valid-at` pattern in a `not-join` body binds the query's valid time on every path** (#615). When the body also held an expression, sat in an `or` branch, or ran in a query that invokes a rule, the body bound `nil`, so `(not-join [?e] [?e :db/valid-at ?t] [(> ?t 0)])` never matched and excluded nothing; without the expression the same body excluded every entity. It now binds the query's `:valid-at` value (or the current time) as the other paths do. Present since v0.13.1. + ## v2.0.7 — 2026-10-16 Patch release on the v2.x line under the [support policy](PHILOSOPHY.md#support-policy): one data-integrity fix for prepared queries (#603) and two query fixes for rules with literal arguments (#608) and `or` clauses that share no variable with the earlier clauses (#610). File format is unchanged (v7). No API changes. Upgrading is recommended for every v2.x user who uses prepared queries with a literal `:as-of` or `:valid-at`, rule calls with a literal argument, or `or`. diff --git a/src/query/datalog/evaluator.rs b/src/query/datalog/evaluator.rs index 3dfd10ade4..10db589a14 100644 --- a/src/query/datalog/evaluator.rs +++ b/src/query/datalog/evaluator.rs @@ -504,13 +504,15 @@ pub(crate) fn rule_invocation_to_pattern(predicate: &str, args: &[EdnValue]) -> /// - Pattern → substitute join_vars via substitute_pattern. /// - RuleInvocation → convert to Pattern via rule_invocation_to_pattern, then substitute. /// Rule-derived facts are already present in `storage` (accumulated) from lower strata. -/// 3. Run PatternMatcher::match_patterns on all resulting patterns against `storage`. +/// 3. Run PatternMatcher::match_patterns on all resulting patterns against `storage`, +/// with `valid_at` as the value a `:db/valid-at` pattern binds (#615). /// 4. Any complete match → body is satisfiable → return true (reject outer binding). pub fn evaluate_not_join( join_vars: &[String], clauses: &[WhereClause], binding: &Bindings, storage: Arc<[Fact]>, + valid_at: Value, functions: &FunctionRegistry, ) -> bool { // Build a partial binding containing only the join variables @@ -539,7 +541,7 @@ pub fn evaluate_not_join( .filter(|c| matches!(c, WhereClause::Expr { .. })) .collect(); - let matcher = PatternMatcher::from_slice(storage.clone()); + let matcher = PatternMatcher::from_slice_with_valid_at(storage.clone(), valid_at); let mut not_bindings: Vec = if substituted.is_empty() { // Expr-only not-join body: seed with the partial binding so variables resolve. vec![partial.clone()] @@ -1006,6 +1008,7 @@ impl StratifiedEvaluator { nj_clauses, &binding, accumulated_facts.clone(), + Value::Null, &fn_guard, ) { continue 'binding; @@ -1420,6 +1423,7 @@ mod tests { &[expr_clause], &binding, facts, + Value::Null, &FunctionRegistry::with_builtins(), ); assert!( diff --git a/src/query/datalog/executor.rs b/src/query/datalog/executor.rs index 5fd16c82b2..be42063980 100644 --- a/src/query/datalog/executor.rs +++ b/src/query/datalog/executor.rs @@ -821,6 +821,7 @@ impl DatalogExecutor { &join_vars, &clauses, derived_facts.clone(), + valid_at_value.clone(), ®istry, ); } @@ -866,6 +867,7 @@ impl DatalogExecutor { &join_vars, &clauses, derived_facts.clone(), + valid_at_value.clone(), ®istry, ), _ => bindings, @@ -1115,7 +1117,14 @@ fn apply_not_join_clause( return bindings .into_iter() .filter(|b| { - !evaluate_not_join(join_vars, nj_clauses, b, filtered_facts.clone(), registry) + !evaluate_not_join( + join_vars, + nj_clauses, + b, + filtered_facts.clone(), + valid_at_value.clone(), + registry, + ) }) .collect(); } @@ -1170,6 +1179,7 @@ fn apply_not_join_clause( nj_clauses, binding, filtered_facts.clone(), + valid_at_value.clone(), registry, ) }) @@ -1297,11 +1307,21 @@ fn apply_not_join_clause_derived( join_vars: &[String], nj_clauses: &[WhereClause], derived_facts: Arc<[Fact]>, + valid_at_value: Value, registry: &FunctionRegistry, ) -> Vec { bindings .into_iter() - .filter(|b| !evaluate_not_join(join_vars, nj_clauses, b, derived_facts.clone(), registry)) + .filter(|b| { + !evaluate_not_join( + join_vars, + nj_clauses, + b, + derived_facts.clone(), + valid_at_value.clone(), + registry, + ) + }) .collect() } @@ -1786,8 +1806,14 @@ pub(crate) fn evaluate_branch( } } for (join_vars, nj_clauses) in ¬_join_clauses { - if evaluate_not_join(join_vars, nj_clauses, binding, storage.clone(), registry) - { + if evaluate_not_join( + join_vars, + nj_clauses, + binding, + storage.clone(), + branch_valid_at_value.clone(), + registry, + ) { return false; } } diff --git a/tests/not_join_test.rs b/tests/not_join_test.rs index 07f3394302..643013799d 100644 --- a/tests/not_join_test.rs +++ b/tests/not_join_test.rs @@ -446,3 +446,76 @@ fn test_not_join_coexists_with_not_in_query() { "only charlie must pass: alice excluded by (not), bob excluded by (not-join)" ); } + +/// #615: a `:db/valid-at` pattern in a `not-join` body binds the query's valid +/// time on every path, not `nil` on the per-row one (body with an expression). +#[test] +fn test_not_join_valid_at_with_expr_binds_query_valid_time() { + let db = in_memory_db(); + db.execute("(transact [[:a :n 1] [:b :n 2]])").unwrap(); + + let plain = db + .execute( + r#"(query [:find ?e :valid-at "2030-01-01" + :where [?e :n _] (not-join [?e] [?e :db/valid-at ?t])])"#, + ) + .unwrap(); + assert_eq!(result_count(&plain), 0, "exclusion-set path excludes both"); + + let with_expr = db + .execute( + r#"(query [:find ?e :valid-at "2030-01-01" + :where [?e :n _] (not-join [?e] [?e :db/valid-at ?t] [(> ?t 0)])])"#, + ) + .unwrap(); + assert_eq!(result_count(&with_expr), 0, "per-row path excludes both"); +} + +/// #615: the same inside an `or` branch, where `not-join` always runs per row. +#[test] +fn test_not_join_valid_at_in_or_branch_binds_query_valid_time() { + let db = in_memory_db(); + db.execute("(transact [[:a :n 1] [:b :n 2]])").unwrap(); + + let result = db + .execute( + r#"(query [:find ?e :valid-at "2030-01-01" + :where [?e :n _] + (or (and [?e :n 1] (not-join [?e] [?e :db/valid-at ?t] [(> ?t 0)])) + (and [?e :n 2] (not-join [?e] [?e :db/valid-at ?t] [(> ?t 0)])))])"#, + ) + .unwrap(); + assert_eq!(result_count(&result), 0, "or-branch not-join excludes both"); +} + +/// #615: the same in a query that invokes a rule (rules query path). +#[test] +fn test_not_join_valid_at_in_rule_query_binds_query_valid_time() { + let db = in_memory_db(); + db.execute("(transact [[:a :n 1] [:b :n 2]])").unwrap(); + db.execute("(rule [(numbered ?e) [?e :n _]])").unwrap(); + + let result = db + .execute( + r#"(query [:find ?e :valid-at "2030-01-01" + :where (numbered ?e) (not-join [?e] [?e :db/valid-at ?t])])"#, + ) + .unwrap(); + assert_eq!( + result_count(&result), + 0, + "rule-query not-join excludes both" + ); + + let with_expr = db + .execute( + r#"(query [:find ?e :valid-at "2030-01-01" + :where (numbered ?e) (not-join [?e] [?e :db/valid-at ?t] [(> ?t 0)])])"#, + ) + .unwrap(); + assert_eq!( + result_count(&with_expr), + 0, + "rule-query not-join with expr excludes both" + ); +} From 5a97b504411970ae7a7a6c21ae567af4681f14f9 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 16 Oct 2026 15:30:57 +0530 Subject: [PATCH 2/2] test(not-join): deferred rule-query and expression-only bodies (#615) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PtCdzbJMSS1V8qEPaJ4jXz --- tests/not_join_test.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/not_join_test.rs b/tests/not_join_test.rs index 643013799d..62c0810d97 100644 --- a/tests/not_join_test.rs +++ b/tests/not_join_test.rs @@ -519,3 +519,38 @@ fn test_not_join_valid_at_in_rule_query_binds_query_valid_time() { "rule-query not-join with expr excludes both" ); } + +/// #615: a `not-join` whose `?e` only an `or` binds runs after the `or`, on +/// the rules query path's deferred pass, and binds the query's valid time too. +#[test] +fn test_not_join_valid_at_deferred_in_rule_query_binds_query_valid_time() { + let db = in_memory_db(); + db.execute("(transact [[:a :n 1] [:b :n 2]])").unwrap(); + db.execute("(rule [(numbered ?e) [?e :n _]])").unwrap(); + + let result = db + .execute( + r#"(query [:find ?x ?e :valid-at "2030-01-01" + :where (numbered ?x) + (or [?e :n 1] [?e :n 2]) + (not-join [?e] [?e :db/valid-at ?t] [(> ?t 0)])])"#, + ) + .unwrap(); + assert_eq!( + result_count(&result), + 0, + "deferred not-join excludes every ?e" + ); +} + +/// A `not-join` body holding only an expression filters on the join variables. +#[test] +fn test_not_join_expression_only_body() { + let db = in_memory_db(); + db.execute("(transact [[:a :n 1] [:b :n 2]])").unwrap(); + + let result = db + .execute("(query [:find ?e :where [?e :n ?v] (not-join [?v] [(> ?v 1)])])") + .unwrap(); + assert_eq!(result_count(&result), 1, "only :a has ?v <= 1"); +}