From c7706e119540f06e50a66408962b688b7219d4b9 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 24 Oct 2026 18:44:13 +0530 Subject: [PATCH 1/2] fix(query): expression = / != equate a keyword with a ref to its entity (#744) Patterns, joins, not bodies and or branches treat a keyword as equal to a ref to the entity it names; eval_binop compared the two Values as they are, so [(= ?g :bob)] kept no row where [?x :ref/friend :bob] matched one, and != kept it. Eq/Neq now use expr_eq: structural equality, except that a keyword equals a ref to the entity edn_to_entity_id derives from it. Ordering comparisons on keywords and refs stay a type mismatch. The frozen engine answer to [?e :person/self ?v] [(= ?e ?v)] (keyword :pN on entity :pN) changes from 0 rows to the rows [?e :person/self ?e] returns, at each temporal setting. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x --- CHANGELOG.md | 1 + docs/TEST_COVERAGE.md | 3 +- src/query/datalog/executor.rs | 17 +++++-- tests/answers/engine_tests.txt | 48 +++++++++---------- tests/join_rules_test.rs | 88 ++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09239aa9ea..a473180912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- **An expression's `=` and `!=` equate a keyword with a ref to the entity it names** (#744). Patterns, joins, `not` bodies and `or` branches treat the keyword `:bob` as equal to a ref to the entity `:bob` names, but `[(= ?g :bob)]` compared the two values as they are, so with `?g` bound to that ref it kept no row and `[(!= ?g :bob)]` kept the row: the same question answered differently by how the query was written, with no error. `=` and `!=` now compare as joins do, in queries, rule bodies, `not` clauses and binding expressions, against a keyword constant, a keyword value or a bound `Val(Ref)`. A keyword still equals only the same keyword, and `<`, `>`, `<=` and `>=` on a keyword or a ref are still a type mismatch (no row). - **`checkpoint`, `verify` and `rebuild_indexes` inside a `WriteTransaction` return `INT-001` instead of deadlocking** (#734). Called on a thread that holds a `WriteTransaction`, they waited on the write lock that the transaction already held, so the thread hung. They now return `INT-001`, as `execute`, `query`, `fact_log` and `begin_write` already did, and the transaction stays usable. `prepare` and `PreparedQuery::execute`/`query` make the same check; they could not deadlock, but they ran inside the transaction where `execute`/`query` refused. `checkpoint` deadlocked in every release since `WriteTransaction` was added; the fix for it and for the prepared calls ships in v2.x too. - **A float constant in a pattern matches equal floats only** (#706). A float written in a pattern's value position, or bound to a `$slot` of a prepared query, matched every stored float within an absolute `f64::EPSILON` (about `2.2e-16`), so `[?e :m/x 0.0]` returned an entity whose `:m/x` is `1e-17`, while `[?e :m/x ?v] [(= ?v 0.0)]` did not. Above 1.0 the epsilon is smaller than the gap between adjacent floats, so only floats smaller than about `2.2e-16` in magnitude were affected: every one of them matched `0.0`, and `1e-20` matched `5e-20`. Float constants now compare by IEEE `==`, as joins, predicates and the indexes do: `0.0` matches `-0.0` and nothing else changes. The fix covers patterns in queries, rule bodies and `not` / `or` bodies, on the executor and the streaming engine. Nothing stored was wrong. The same fix ships in v2.0.10. - **WAL entries of more than 1,000,000 facts replay** (#686). A WAL entry of more than 1,000,000 facts was taken as damage: replay stopped there, and the next open cut the WAL off at that entry, so after a crash, kill or close without a checkpoint that transaction and every later one in the WAL were lost, though each had committed. The writer had no such limit. Since #550 no v3 transaction writes such an entry, but a WAL left by v2.x can hold one. Replay now takes an entry's fact count as damage only when the bytes after it cannot hold that many facts. Affected every release since v0.5.0. Also fixed in v2.0.10. diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index b88aed8a3e..0545ce64d7 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -370,11 +370,12 @@ These tests compare the database against an independent answer rather than a sin - the planner builds a keyless join on the side with the smaller product estimate (#575's shape, a selective outer side, a tie, reordering off). - `meter.rs` (4 unit tests) checks charge, release, drop, peak and the allocator form. - `Aggregate` charges its sorted map of groups until every row is built, and lets its hash table and group rows go first, with `sum-distinct` members charged once (`aggregate_finish_charges_its_sorted_groups`, `aggregate_finish_keeps_sum_distinct_members_charged_once`). -- **Join rules** (`tests/join_rules_test.rs`, 11 tests; #545) +- **Join rules** (`tests/join_rules_test.rs`, 14 tests; #545, #744) - A join on a float value pairs `0.0` with `-0.0`, through the hash key and as a shared variable. A unit test checks that `-0.0` and `0.0` hash alike. - A float constant in a pattern, bound to a `$slot`, in a rule body or in an `=` predicate matches equal floats only (`1e-17` does not match `0.0`; `0.0` matches `-0.0`; #706), in memory, on file before and after a checkpoint and after reopen. - A variable bound to a keyword by one pattern and to the ref of the same entity by another returns the kind from the first pattern in `:where` text order: when the planner runs the patterns in another order, through the attribute position, before and after a rule call, and with a repeated pattern. - An expression binding unifies with a pattern binding the same variable; the first binder in the text, pattern or expression, gives the value (#596). + - An expression's `=` and `!=` equate a keyword with a ref to the entity it names, as the pattern constant and the join do: either operand order, a keyword constant or value, a bound `Val(Ref)`, a binding expression, a rule body and a `not` clause, in memory, after a checkpoint and after reopen; a keyword still equals the same keyword only (#744). - A pseudo-attribute pattern never gives a variable its value, even when it runs first (#606; the plan-independence matrix checks it with reordering off). - **Pseudo-attribute sources** (`tests/pseudo_source_test.rs`, 12 tests; #594) - A per-fact pseudo-attribute reads the nearest earlier pattern in the text with the same entity term (#594's example in both text orders). Two pseudo-attributes share a source. `[:alice ?a _]` gives one row per fact. Constant and bound value terms check. diff --git a/src/query/datalog/executor.rs b/src/query/datalog/executor.rs index 7b29ebcc77..948c2ec023 100644 --- a/src/query/datalog/executor.rs +++ b/src/query/datalog/executor.rs @@ -639,11 +639,22 @@ fn to_float_pair(l: &Value, r: &Value) -> Result<(f64, f64), ()> { Ok((lf, rf)) } +/// Whether `l` and `r` are equal as a join compares them: structurally, +/// except that a keyword equals a ref to the entity it names (#744). +fn expr_eq(l: &Value, r: &Value) -> bool { + match (l, r) { + (Value::Keyword(k), Value::Ref(u)) | (Value::Ref(u), Value::Keyword(k)) => { + edn_to_entity_id(&EdnValue::Keyword(k.clone())).is_ok_and(|e| e == *u) + } + _ => l == r, + } +} + fn eval_binop(op: &BinOp, l: Value, r: Value) -> Result { match op { - // Structural equality — works for all Value variants; no type mismatch error. - BinOp::Eq => return Ok(Value::Boolean(l == r)), - BinOp::Neq => return Ok(Value::Boolean(l != r)), + // Equality — works for all Value variants; no type mismatch error. + BinOp::Eq => return Ok(Value::Boolean(expr_eq(&l, &r))), + BinOp::Neq => return Ok(Value::Boolean(!expr_eq(&l, &r))), _ => {} } diff --git a/tests/answers/engine_tests.txt b/tests/answers/engine_tests.txt index fb2b03828f..0761511776 100644 --- a/tests/answers/engine_tests.txt +++ b/tests/answers/engine_tests.txt @@ -865,7 +865,7 @@ engine_matches_executor_across_checkpoints_and_reopen 117902ffc53f45c6 0 cbf29ce engine_matches_executor_across_checkpoints_and_reopen 118a9615904a6459 58 27960b3098d592b9 engine_matches_executor_across_checkpoints_and_reopen 11a93397dd96298c 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 11ab748ecbe0c39e 2 a1ae4f40f66aa14a -engine_matches_executor_across_checkpoints_and_reopen 11b2dd4c602717b6 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen 11b2dd4c602717b6 1 81811716bd6b8225 engine_matches_executor_across_checkpoints_and_reopen 11c04184157c1f87 12 43b7e885d6f10365 engine_matches_executor_across_checkpoints_and_reopen 11d242d2d8411c1e 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 11f52030cd2f59ae 0 cbf29ce484222325 @@ -971,7 +971,7 @@ engine_matches_executor_across_checkpoints_and_reopen 1e4c631d27d0ae10 98 7cac5d engine_matches_executor_across_checkpoints_and_reopen 1e5627056673fd8b 16 caa4a2629e02da05 engine_matches_executor_across_checkpoints_and_reopen 1e640fc6293dfe64 5 52a1017611c8c651 engine_matches_executor_across_checkpoints_and_reopen 1e84887c8dafc781 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen 1e877a9786a0f7e6 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen 1e877a9786a0f7e6 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen 1e8fc85fa9314b67 6 125405a256c292ce engine_matches_executor_across_checkpoints_and_reopen 1ee7333ba0d87bee 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 1f0602670a99401a 12 cc2b1bdab54294bb @@ -981,7 +981,7 @@ engine_matches_executor_across_checkpoints_and_reopen 1fa27bfbb3d21b6c 0 cbf29ce engine_matches_executor_across_checkpoints_and_reopen 1fa51be7ecd7e6ef 4 09bb376159ad3d90 engine_matches_executor_across_checkpoints_and_reopen 1fa51be7ecd7e6ef 4 5dfcd2146e828f37 engine_matches_executor_across_checkpoints_and_reopen 1fa51be7ecd7e6ef 4 acbc6d9f0c52a136 -engine_matches_executor_across_checkpoints_and_reopen 1fabd9b19d4db3ec 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen 1fabd9b19d4db3ec 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen 1fbca73566102685 5 ddcf853d801c97eb engine_matches_executor_across_checkpoints_and_reopen 1fc6ad9e1011c647 18 8f5ff12b787df1ae engine_matches_executor_across_checkpoints_and_reopen 1fc6ad9e1011c647 27 af597e5fa00c92d9 @@ -1524,7 +1524,7 @@ engine_matches_executor_across_checkpoints_and_reopen 5d2985b5ebe37f3e 95 cab7b4 engine_matches_executor_across_checkpoints_and_reopen 5d5b1f4282236d90 9 f1f9dde166aa3e79 engine_matches_executor_across_checkpoints_and_reopen 5d6d092decc3f66a 3 0709e1530c9e69e6 engine_matches_executor_across_checkpoints_and_reopen 5d7f3107af6ea22e 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen 5e39abd336588750 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen 5e39abd336588750 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen 5e65fcb2bebf6ae9 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 5e7efef3eb1d1419 150 5798decf5a8c4672 engine_matches_executor_across_checkpoints_and_reopen 5ee17d529f79f418 2 4ac6fbb661ab1b38 @@ -1700,7 +1700,7 @@ engine_matches_executor_across_checkpoints_and_reopen 757b8c809ed9c44e 4 e3ce276 engine_matches_executor_across_checkpoints_and_reopen 757b8c809ed9c44e 49 c391b78310286da6 engine_matches_executor_across_checkpoints_and_reopen 75d4db2227e0474d 1 2b0a3a192b55558b engine_matches_executor_across_checkpoints_and_reopen 75e2e6859e5dab3e 1 2b0a3a192b55558b -engine_matches_executor_across_checkpoints_and_reopen 7618ec94c2701a4c 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen 7618ec94c2701a4c 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen 763e2b38cb1e90eb 2 23742fb0ed610812 engine_matches_executor_across_checkpoints_and_reopen 7641e697cda4dc04 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 765a007b8c009bd7 1 72ab9d2934ddeebf @@ -1717,7 +1717,7 @@ engine_matches_executor_across_checkpoints_and_reopen 773d3064e99972f2 18 d5748d engine_matches_executor_across_checkpoints_and_reopen 774b4a1827498d8c 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 776a7617a28e8637 1 d251ad192a466b73 engine_matches_executor_across_checkpoints_and_reopen 778b91a63f4ac694 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen 77a123a2e07981f7 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen 77a123a2e07981f7 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen 77cc8a272f3ec5d8 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen 782888110769501c 3 6b32068b744d03cc engine_matches_executor_across_checkpoints_and_reopen 7831c9ed32f87e7c 3 d84b517a33c8e75d @@ -2013,11 +2013,11 @@ engine_matches_executor_across_checkpoints_and_reopen a0309bddc528c4f3 174 09200 engine_matches_executor_across_checkpoints_and_reopen a0309bddc528c4f3 65 a241e85840ee1c32 engine_matches_executor_across_checkpoints_and_reopen a0347b340349f15e 116 19f537044e73b093 engine_matches_executor_across_checkpoints_and_reopen a051035ec341e66e 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen a052b51e73a6e9f8 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen a052b51e73a6e9f8 5 1d529cd8654bc5fc engine_matches_executor_across_checkpoints_and_reopen a0a1c7bf7bdce3cf 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen a0c6d6de6e0327e7 3 6f595b518c33e6b0 engine_matches_executor_across_checkpoints_and_reopen a0eae9c93ece8629 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen a0eedaf41c35df7b 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen a0eedaf41c35df7b 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen a1345592f26f03c5 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen a13491be489359eb 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen a15d3deb7cf77be3 0 cbf29ce484222325 @@ -2260,7 +2260,7 @@ engine_matches_executor_across_checkpoints_and_reopen b9c1693cfe21278c 2 9de41c9 engine_matches_executor_across_checkpoints_and_reopen b9d1bbcfc5dff18b 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen b9ef43cca7dfe21b 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen ba5bc1a5aa29e540 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen ba7c52af4faeb177 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen ba7c52af4faeb177 3 b366d81396a9b58e engine_matches_executor_across_checkpoints_and_reopen ba83e0662ef8bb03 8 83fb1f87c9b5de5b engine_matches_executor_across_checkpoints_and_reopen ba8a33ce837e5b15 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen baba09a3259f55b1 0 cbf29ce484222325 @@ -2468,7 +2468,7 @@ engine_matches_executor_across_checkpoints_and_reopen d291657f90fcaeab 0 cbf29ce engine_matches_executor_across_checkpoints_and_reopen d2dfc209192dd0c3 2 0902cd9b55681795 engine_matches_executor_across_checkpoints_and_reopen d30b04425e597bcc 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen d31060725e4bc48c 9 187fb06fea7acee6 -engine_matches_executor_across_checkpoints_and_reopen d3203e472987f1b0 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen d3203e472987f1b0 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen d33cf3cd61fc526f 4 add2a9873aa7b437 engine_matches_executor_across_checkpoints_and_reopen d3806ed78d8be427 6 e1753466c4728415 engine_matches_executor_across_checkpoints_and_reopen d38aec406327e8c4 3 cb355cc9f650b085 @@ -2562,7 +2562,7 @@ engine_matches_executor_across_checkpoints_and_reopen dc638d2b6447110a 1 d9a8561 engine_matches_executor_across_checkpoints_and_reopen dc8374a5deaec97a 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen dca915079039da6d 1 2b0a3a192b55558b engine_matches_executor_across_checkpoints_and_reopen dcad82fc86b64d3a 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen dce049282e92b2d9 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen dce049282e92b2d9 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen dcffa5e9b17e6d30 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen dd08e05e7234da22 130 d938a2947bb03058 engine_matches_executor_across_checkpoints_and_reopen dd0b220eb2bc1712 0 cbf29ce484222325 @@ -2615,7 +2615,7 @@ engine_matches_executor_across_checkpoints_and_reopen e1d8ebef23cf5b08 6 d726407 engine_matches_executor_across_checkpoints_and_reopen e1d8ebef23cf5b08 9 688d36349666ae5e engine_matches_executor_across_checkpoints_and_reopen e1d8ebef23cf5b08 9 76b7e537eae2e54d engine_matches_executor_across_checkpoints_and_reopen e1d8ebef23cf5b08 9 aafd9c32b0609d82 -engine_matches_executor_across_checkpoints_and_reopen e2ad7d7fe987e4a8 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen e2ad7d7fe987e4a8 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen e2bf1023c2cf8f5a 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen e2c847426029320c 6 861eb491878c8fca engine_matches_executor_across_checkpoints_and_reopen e2fa7b62d6e83860 0 cbf29ce484222325 @@ -2840,7 +2840,7 @@ engine_matches_executor_across_checkpoints_and_reopen fbee3fc53c3e45ea 3 6526f06 engine_matches_executor_across_checkpoints_and_reopen fbf42e554bc579f3 8 b92f8e05d0d9724e engine_matches_executor_across_checkpoints_and_reopen fbf6cf1f9fa31b9b 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen fc14cbc1f4468b07 0 cbf29ce484222325 -engine_matches_executor_across_checkpoints_and_reopen fc34f499749808d4 0 cbf29ce484222325 +engine_matches_executor_across_checkpoints_and_reopen fc34f499749808d4 6 967c5c6d5a844ffb engine_matches_executor_across_checkpoints_and_reopen fc38f634cf89c0b2 0 cbf29ce484222325 engine_matches_executor_across_checkpoints_and_reopen fc3bf7d6a8cfa6e9 9 f4d27fdfca1c0fb8 engine_matches_executor_across_checkpoints_and_reopen fc5e1846ccaa9262 10 b5ae1711d5e51afb @@ -3010,7 +3010,7 @@ engine_matches_executor_in_memory 1155703c1e0812b4 16 7c8ada434af11c37 engine_matches_executor_in_memory 117902ffc53f45c6 0 cbf29ce484222325 engine_matches_executor_in_memory 11a93397dd96298c 0 cbf29ce484222325 engine_matches_executor_in_memory 11ab748ecbe0c39e 2 a1ae4f40f66aa14a -engine_matches_executor_in_memory 11b2dd4c602717b6 0 cbf29ce484222325 +engine_matches_executor_in_memory 11b2dd4c602717b6 1 81811716bd6b8225 engine_matches_executor_in_memory 11c04184157c1f87 12 43b7e885d6f10365 engine_matches_executor_in_memory 11d242d2d8411c1e 0 cbf29ce484222325 engine_matches_executor_in_memory 11f52030cd2f59ae 0 cbf29ce484222325 @@ -3097,7 +3097,7 @@ engine_matches_executor_in_memory 1e4c631d27d0ae10 82 f53835411076ab85 engine_matches_executor_in_memory 1e4c631d27d0ae10 98 7cac5d3d18ebd892 engine_matches_executor_in_memory 1e5627056673fd8b 16 caa4a2629e02da05 engine_matches_executor_in_memory 1e84887c8dafc781 0 cbf29ce484222325 -engine_matches_executor_in_memory 1e877a9786a0f7e6 0 cbf29ce484222325 +engine_matches_executor_in_memory 1e877a9786a0f7e6 6 967c5c6d5a844ffb engine_matches_executor_in_memory 1e8fc85fa9314b67 6 125405a256c292ce engine_matches_executor_in_memory 1ee7333ba0d87bee 0 cbf29ce484222325 engine_matches_executor_in_memory 1f0602670a99401a 12 cc2b1bdab54294bb @@ -3107,7 +3107,7 @@ engine_matches_executor_in_memory 1fa27bfbb3d21b6c 0 cbf29ce484222325 engine_matches_executor_in_memory 1fa51be7ecd7e6ef 4 09bb376159ad3d90 engine_matches_executor_in_memory 1fa51be7ecd7e6ef 4 5dfcd2146e828f37 engine_matches_executor_in_memory 1fa51be7ecd7e6ef 4 acbc6d9f0c52a136 -engine_matches_executor_in_memory 1fabd9b19d4db3ec 0 cbf29ce484222325 +engine_matches_executor_in_memory 1fabd9b19d4db3ec 6 967c5c6d5a844ffb engine_matches_executor_in_memory 1fbca73566102685 5 ddcf853d801c97eb engine_matches_executor_in_memory 1fc6ad9e1011c647 18 8f5ff12b787df1ae engine_matches_executor_in_memory 1fc6ad9e1011c647 27 af597e5fa00c92d9 @@ -3690,7 +3690,7 @@ engine_matches_executor_in_memory 757b8c809ed9c44e 36 af674fc80e764dd9 engine_matches_executor_in_memory 757b8c809ed9c44e 4 e3ce27647f40d44d engine_matches_executor_in_memory 75d4db2227e0474d 1 2b0a3a192b55558b engine_matches_executor_in_memory 75e2e6859e5dab3e 1 2b0a3a192b55558b -engine_matches_executor_in_memory 7618ec94c2701a4c 0 cbf29ce484222325 +engine_matches_executor_in_memory 7618ec94c2701a4c 6 967c5c6d5a844ffb engine_matches_executor_in_memory 7641e697cda4dc04 0 cbf29ce484222325 engine_matches_executor_in_memory 765a007b8c009bd7 1 72ab9d2934ddeebf engine_matches_executor_in_memory 76bb91fa7d1cfc28 1 ff2ec67c0fc3b7aa @@ -3703,7 +3703,7 @@ engine_matches_executor_in_memory 773d3064e99972f2 18 d5748d48706e0078 engine_matches_executor_in_memory 774b4a1827498d8c 0 cbf29ce484222325 engine_matches_executor_in_memory 776a7617a28e8637 1 d251ad192a466b73 engine_matches_executor_in_memory 778b91a63f4ac694 0 cbf29ce484222325 -engine_matches_executor_in_memory 77a123a2e07981f7 0 cbf29ce484222325 +engine_matches_executor_in_memory 77a123a2e07981f7 6 967c5c6d5a844ffb engine_matches_executor_in_memory 77cc8a272f3ec5d8 0 cbf29ce484222325 engine_matches_executor_in_memory 782888110769501c 3 6b32068b744d03cc engine_matches_executor_in_memory 7831c9ed32f87e7c 3 d84b517a33c8e75d @@ -3958,7 +3958,7 @@ engine_matches_executor_in_memory a0309bddc528c4f3 100 53b80245de45553d engine_matches_executor_in_memory a0309bddc528c4f3 129 9c35cf355b761c22 engine_matches_executor_in_memory a0309bddc528c4f3 153 14c7547e627b020a engine_matches_executor_in_memory a0309bddc528c4f3 65 a241e85840ee1c32 -engine_matches_executor_in_memory a052b51e73a6e9f8 0 cbf29ce484222325 +engine_matches_executor_in_memory a052b51e73a6e9f8 5 1d529cd8654bc5fc engine_matches_executor_in_memory a0a1c7bf7bdce3cf 0 cbf29ce484222325 engine_matches_executor_in_memory a0c6d6de6e0327e7 3 6f595b518c33e6b0 engine_matches_executor_in_memory a0eae9c93ece8629 0 cbf29ce484222325 @@ -4170,7 +4170,7 @@ engine_matches_executor_in_memory b9c1693cfe21278c 2 9de41c9d35adb9f3 engine_matches_executor_in_memory b9d1bbcfc5dff18b 0 cbf29ce484222325 engine_matches_executor_in_memory b9ef43cca7dfe21b 0 cbf29ce484222325 engine_matches_executor_in_memory ba5bc1a5aa29e540 0 cbf29ce484222325 -engine_matches_executor_in_memory ba7c52af4faeb177 0 cbf29ce484222325 +engine_matches_executor_in_memory ba7c52af4faeb177 3 b366d81396a9b58e engine_matches_executor_in_memory ba83e0662ef8bb03 8 83fb1f87c9b5de5b engine_matches_executor_in_memory ba8a33ce837e5b15 0 cbf29ce484222325 engine_matches_executor_in_memory bb35b8295268cffc 0 cbf29ce484222325 @@ -4347,7 +4347,7 @@ engine_matches_executor_in_memory d291657f90fcaeab 0 cbf29ce484222325 engine_matches_executor_in_memory d2dfc209192dd0c3 2 0902cd9b55681795 engine_matches_executor_in_memory d30b04425e597bcc 0 cbf29ce484222325 engine_matches_executor_in_memory d31060725e4bc48c 9 187fb06fea7acee6 -engine_matches_executor_in_memory d3203e472987f1b0 0 cbf29ce484222325 +engine_matches_executor_in_memory d3203e472987f1b0 6 967c5c6d5a844ffb engine_matches_executor_in_memory d33cf3cd61fc526f 4 add2a9873aa7b437 engine_matches_executor_in_memory d3806ed78d8be427 6 e1753466c4728415 engine_matches_executor_in_memory d3d61c78577d6c56 0 cbf29ce484222325 @@ -4426,7 +4426,7 @@ engine_matches_executor_in_memory dc39efb4829ce214 8 b0ec0fb2f348a8f8 engine_matches_executor_in_memory dc638d2b6447110a 1 d9a8561e9f938fde engine_matches_executor_in_memory dc8374a5deaec97a 0 cbf29ce484222325 engine_matches_executor_in_memory dca915079039da6d 1 2b0a3a192b55558b -engine_matches_executor_in_memory dce049282e92b2d9 0 cbf29ce484222325 +engine_matches_executor_in_memory dce049282e92b2d9 6 967c5c6d5a844ffb engine_matches_executor_in_memory dcffa5e9b17e6d30 0 cbf29ce484222325 engine_matches_executor_in_memory dd08e05e7234da22 130 d938a2947bb03058 engine_matches_executor_in_memory dd0b220eb2bc1712 0 cbf29ce484222325 @@ -4470,7 +4470,7 @@ engine_matches_executor_in_memory e1d8ebef23cf5b08 3 138f357547a401df engine_matches_executor_in_memory e1d8ebef23cf5b08 6 d7264077dca1890a engine_matches_executor_in_memory e1d8ebef23cf5b08 9 688d36349666ae5e engine_matches_executor_in_memory e1d8ebef23cf5b08 9 76b7e537eae2e54d -engine_matches_executor_in_memory e2ad7d7fe987e4a8 0 cbf29ce484222325 +engine_matches_executor_in_memory e2ad7d7fe987e4a8 6 967c5c6d5a844ffb engine_matches_executor_in_memory e2bf1023c2cf8f5a 0 cbf29ce484222325 engine_matches_executor_in_memory e2c847426029320c 6 861eb491878c8fca engine_matches_executor_in_memory e2fa7b62d6e83860 0 cbf29ce484222325 @@ -4667,7 +4667,7 @@ engine_matches_executor_in_memory fb99bc69bb5c317f 1 c3f77eeec1657e38 engine_matches_executor_in_memory fbdb74079af161c0 2 d69b4a517d2cc546 engine_matches_executor_in_memory fbee3fc53c3e45ea 3 6526f0696876a02f engine_matches_executor_in_memory fc14cbc1f4468b07 0 cbf29ce484222325 -engine_matches_executor_in_memory fc34f499749808d4 0 cbf29ce484222325 +engine_matches_executor_in_memory fc34f499749808d4 6 967c5c6d5a844ffb engine_matches_executor_in_memory fc3bf7d6a8cfa6e9 9 f4d27fdfca1c0fb8 engine_matches_executor_in_memory fc5e1846ccaa9262 10 b5ae1711d5e51afb engine_matches_executor_in_memory fc60da2457021f36 2 dd5187ca97af06b9 diff --git a/tests/join_rules_test.rs b/tests/join_rules_test.rs index 5e5a3fa788..54fb1bd7e2 100644 --- a/tests/join_rules_test.rs +++ b/tests/join_rules_test.rs @@ -255,3 +255,91 @@ fn pseudo_attribute_pattern_never_replaces_a_value() { "keyword from the first real pattern" ); } + +/// `:alice` holds a ref to `:bob`'s entity, `:carol` the keyword `:bob`. +fn ref_and_keyword_db() -> Minigraf { + let db = Minigraf::in_memory().unwrap(); + db.execute(&format!( + r#"(transact [[:alice :ref/friend #uuid "{}"] [:carol :kw/friend :bob] [:dan :kw/friend :eve]])"#, + named(":bob") + )) + .unwrap(); + db +} + +/// An expression's `=` and `!=` equate a keyword with a ref to the entity +/// it names, as patterns and joins do (#744). +fn assert_expression_equality_matches_joins(db: &Minigraf) { + for (q, want) in [ + // The pattern constant and the join the expressions must agree with. + ("[:find ?x :where [?x :ref/friend :bob]]", 1), + ("[:find ?x :where [?y :ref/friend ?g] [?x :kw/friend ?g]]", 1), + // A ref against a keyword constant, either side. + ("[:find ?x :where [?x :ref/friend ?g] [(= ?g :bob)]]", 1), + ("[:find ?x :where [?x :ref/friend ?g] [(= :bob ?g)]]", 1), + ("[:find ?x :where [?x :ref/friend ?g] [(!= ?g :bob)]]", 0), + ("[:find ?x :where [?x :ref/friend ?g] [(= ?g :eve)]]", 0), + ("[:find ?x :where [?x :ref/friend ?g] [(!= ?g :eve)]]", 1), + // A ref against a keyword value. + ( + "[:find ?x :where [?y :ref/friend ?g] [?x :kw/friend ?k] [(= ?g ?k)]]", + 1, + ), + ( + "[:find ?x :where [?y :ref/friend ?g] [?x :kw/friend ?k] [(!= ?k ?g)]]", + 1, + ), + // A keyword still equals the same keyword only. + ("[:find ?x :where [?x :kw/friend ?k] [(= ?k :bob)]]", 1), + ("[:find ?x :where [?x :kw/friend ?k] [(!= ?k :bob)]]", 1), + ] { + assert_eq!(query(db, q).len(), want, "{q}"); + } + // A binding expression's value is the comparison. + let r = query(db, "[:find ?b :where [?x :ref/friend ?g] [(= ?g :bob) ?b]]"); + assert!(matches!(r[..], [ref row] if row[0] == Value::Boolean(true))); + // A keyword value against a bound ref. + let p = db + .prepare("(query [:find ?x :where [?x :kw/friend ?k] [(= ?k $r)]])") + .unwrap(); + let r = rows( + p.execute(&[("r", BindValue::Val(Value::Ref(named(":bob"))))]) + .unwrap(), + ); + assert_eq!(r.len(), 1, "bound ref"); +} + +#[test] +fn expression_equality_equates_keyword_and_ref() { + assert_expression_equality_matches_joins(&ref_and_keyword_db()); +} + +#[test] +fn expression_equality_in_rule_bodies_and_negation() { + let db = ref_and_keyword_db(); + db.execute("(rule [(bobs-friend ?x) [?x :ref/friend ?g] [(= ?g :bob)]])") + .unwrap(); + assert_eq!(query(&db, "[:find ?x :where (bobs-friend ?x)]").len(), 1); + let r = query( + &db, + "[:find ?x :where [?x :ref/friend ?g] (not [(= ?g :bob)])]", + ); + assert!(r.is_empty(), "not of an equal pair"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn expression_equality_equates_keyword_and_ref_after_checkpoint() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("kw-ref.graph"); + let db = Minigraf::open(&path).unwrap(); + db.execute(&format!( + r#"(transact [[:alice :ref/friend #uuid "{}"] [:carol :kw/friend :bob] [:dan :kw/friend :eve]])"#, + named(":bob") + )) + .unwrap(); + db.checkpoint().unwrap(); + assert_expression_equality_matches_joins(&db); + drop(db); + assert_expression_equality_matches_joins(&Minigraf::open(&path).unwrap()); +} From f73ec91d6043fba890f96033212a90c44f6c2e03 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 24 Oct 2026 19:26:35 +0530 Subject: [PATCH 2/2] test(query): #744 in or branches, UUID keywords, set elements; test counts Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Urda7FD9CYYoxdeGGJa11x --- CLAUDE.md | 2 +- docs/TEST_COVERAGE.md | 8 ++++---- tests/join_rules_test.rs | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index be419b3807..a02d365051 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -215,7 +215,7 @@ Auto-migrates v7 → v8 on open (spec §9, with a backup meta page). v1–v6 are ## Test Coverage -**1792 tests** (1779 passing, 13 ignored; unit + integration + doc). Engine answers once checked by the executor are frozen in `tests/answers/` (#551). +**1796 tests** (1783 passing, 13 ignored; unit + integration + doc). Engine answers once checked by the executor are frozen in `tests/answers/` (#551). See `docs/TEST_COVERAGE.md` for the full per-file breakdown. **Testing conventions** — see the Testing Conventions section below before writing any tests. diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 0545ce64d7..eec18c111d 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -4,9 +4,9 @@ | | Default features | With `--features test-hooks` | |---|---|---| -| **Passing** | 1,779 | 1,797 | +| **Passing** | 1,783 | 1,801 | | **Ignored** | 13 | 14 | -| **Total** | 1,792 | 1,811 | +| **Total** | 1,796 | 1,815 | **Verified**: 2026-10-24 with `cargo test --quiet`. @@ -370,12 +370,12 @@ These tests compare the database against an independent answer rather than a sin - the planner builds a keyless join on the side with the smaller product estimate (#575's shape, a selective outer side, a tie, reordering off). - `meter.rs` (4 unit tests) checks charge, release, drop, peak and the allocator form. - `Aggregate` charges its sorted map of groups until every row is built, and lets its hash table and group rows go first, with `sum-distinct` members charged once (`aggregate_finish_charges_its_sorted_groups`, `aggregate_finish_keeps_sum_distinct_members_charged_once`). -- **Join rules** (`tests/join_rules_test.rs`, 14 tests; #545, #744) +- **Join rules** (`tests/join_rules_test.rs`, 15 tests; #545, #744) - A join on a float value pairs `0.0` with `-0.0`, through the hash key and as a shared variable. A unit test checks that `-0.0` and `0.0` hash alike. - A float constant in a pattern, bound to a `$slot`, in a rule body or in an `=` predicate matches equal floats only (`1e-17` does not match `0.0`; `0.0` matches `-0.0`; #706), in memory, on file before and after a checkpoint and after reopen. - A variable bound to a keyword by one pattern and to the ref of the same entity by another returns the kind from the first pattern in `:where` text order: when the planner runs the patterns in another order, through the attribute position, before and after a rule call, and with a repeated pattern. - An expression binding unifies with a pattern binding the same variable; the first binder in the text, pattern or expression, gives the value (#596). - - An expression's `=` and `!=` equate a keyword with a ref to the entity it names, as the pattern constant and the join do: either operand order, a keyword constant or value, a bound `Val(Ref)`, a binding expression, a rule body and a `not` clause, in memory, after a checkpoint and after reopen; a keyword still equals the same keyword only (#744). + - An expression's `=` and `!=` equate a keyword with a ref to the entity it names, as the pattern constant and the join do: either operand order, a keyword constant or value, a bound `Val(Ref)`, a binding expression, a rule body, a `not` clause, an `or` branch and a keyword whose name is a UUID, in memory, after a checkpoint and after reopen; a keyword still equals the same keyword only, and set elements still compare as they are (#744). - A pseudo-attribute pattern never gives a variable its value, even when it runs first (#606; the plan-independence matrix checks it with reordering off). - **Pseudo-attribute sources** (`tests/pseudo_source_test.rs`, 12 tests; #594) - A per-fact pseudo-attribute reads the nearest earlier pattern in the text with the same entity term (#594's example in both text orders). Two pseudo-attributes share a source. `[:alice ?a _]` gives one row per fact. Constant and bound value terms check. diff --git a/tests/join_rules_test.rs b/tests/join_rules_test.rs index 54fb1bd7e2..8b4662f658 100644 --- a/tests/join_rules_test.rs +++ b/tests/join_rules_test.rs @@ -273,7 +273,10 @@ fn assert_expression_equality_matches_joins(db: &Minigraf) { for (q, want) in [ // The pattern constant and the join the expressions must agree with. ("[:find ?x :where [?x :ref/friend :bob]]", 1), - ("[:find ?x :where [?y :ref/friend ?g] [?x :kw/friend ?g]]", 1), + ( + "[:find ?x :where [?y :ref/friend ?g] [?x :kw/friend ?g]]", + 1, + ), // A ref against a keyword constant, either side. ("[:find ?x :where [?x :ref/friend ?g] [(= ?g :bob)]]", 1), ("[:find ?x :where [?x :ref/friend ?g] [(= :bob ?g)]]", 1), @@ -343,3 +346,31 @@ fn expression_equality_equates_keyword_and_ref_after_checkpoint() { drop(db); assert_expression_equality_matches_joins(&Minigraf::open(&path).unwrap()); } + +#[test] +fn expression_equality_in_or_branches_and_on_uuid_keywords() { + let db = ref_and_keyword_db(); + let r = query( + &db, + "[:find ?x :where [?x :ref/friend ?g] (or [(= ?g :bob)] [(= ?g :eve)])]", + ); + assert_eq!(r.len(), 1, "or branch"); + // A keyword whose name is a UUID names the entity with that UUID. + let u = named(":bob"); + let r = query( + &db, + &format!("[:find ?x :where [?x :ref/friend ?g] [(= ?g :{u})]]"), + ); + assert_eq!(r.len(), 1, "UUID keyword"); + // Set elements still compare as they are: a keyword element never + // equals a ref element (#316). + db.execute(&format!( + r#"(transact [[:s1 :tags #{{:bob}}] [:s2 :tags #{{#uuid "{u}"}}]])"# + )) + .unwrap(); + let r = query( + &db, + "[:find ?a :where [:s1 :tags ?a] [:s2 :tags ?b] [(= ?a ?b)]]", + ); + assert!(r.is_empty(), "sets compare structurally"); +}