diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a404931ce..5aacd26897 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Join answers no longer depend on the query plan** (#545). Two cases changed. A join on a float value now pairs `0.0` with `-0.0`, as a shared variable compared after the join key already did; before, `Value`'s hash told them apart, so the join key never paired them. NaN still equals nothing. Float constants in a pattern still match within `f64::EPSILON`. And a variable bound to a keyword by one pattern and to the ref of the entity the keyword names by another, which already joined, now returns the kind from the first of those patterns in the `:where` text. Before, it returned the kind from the pattern the planner ran first, so the answer could change with the selectivity of the other patterns, and differed under the `wasm` feature, which does not reorder. For example, with `[:alice :friend :bob]` stored as a keyword, `[:find ?b :where [?a :friend ?b] [?b :name "Bob"]]` returned the ref of `:bob`, because the planner runs the pattern with two constants first, and now returns the keyword `:bob`. Expression bindings follow the next entry (#596). The rule covers the top-level `:where` of queries with and without rules; inside `or`, `not` and rule bodies the kinds are unchanged. - **An expression binding a variable that another clause binds keeps only the rows where the two agree** (#596). `[(f …) ?x]` used to overwrite `?x` wherever the planner ran it, so when a pattern also bound `?x` the answer depended on whether that pattern ran first, and differed under the `wasm` feature, which does not reorder. Now the expression unifies: the row is kept when its result equals `?x`'s value, with the join equality of #545 (floats by IEEE `==`, a keyword equal to a ref to the entity it names, an integer never equal to a float, NaN equal to nothing). For example, with `[:a :age 30] [:b :age 40] [:p1 :score 41]`, `[:find ?a ?x :where [?e :age ?a] [:p1 :score ?x] [(+ ?a 1) ?x]]` returned `[30 31] [40 41]` natively and now returns `[40 41]`, as it did under `wasm`. `[(+ ?v 1) ?v]` never matches, and two expressions binding one variable must agree. When the two values differ only as keyword and ref, the variable returns the value of the first clause in the `:where` text that binds it, expressions included. This applies to `or` branches, `not` bodies and rule bodies too, on the executor and on the streaming engine. - **A per-fact pseudo-attribute reads the nearest pattern before it with the same entity** (#594). `:db/valid-from`, `:db/valid-to`, `:db/tx-count` and `:db/tx-id` read the metadata of one fact: the one matched by their *source*, chosen when the query, rule or prepared query is parsed, as the nearest pattern before them in the `:where` text with the same entity term and a stored attribute (a keyword, or a variable as in `[?e ?a _]`). The search starts in the pseudo-attribute's own clause list and moves outwards, before each enclosing `not` / `or`; it crosses a `not-join` / `or-join` only for an entity variable in its join variables, and stops at a rule body, which never reads its caller's patterns. Terms are the same when they are one variable, one `$slot`, or constants naming one entity (`:alice` and its UUID). Before, they read the last pattern *the planner ran* that matched the entity, so the answer depended on the plan and differed under the `wasm` feature: in `[?e :person/name ?n] [?e :person/age ?a] [?e :db/tx-count ?t]`, `?t` could be the tx-count of either fact, and is now the `:person/age` fact's. Three further changes follow: (1) with no source, the query fails at parse time with the new `PRS-081` instead of matching every visible fact of the entity (`[?e :db/tx-count ?t]` before `[?e :person/name ?n]`, a `_` entity, a pseudo-attribute in a rule body whose only candidate is in the caller); to read every fact of an entity, write `[?e ?a _]` before it; (2) inside `not`, `not-join`, `or` and `or-join`, a pseudo-attribute reads the outer fact, so `[?e :person/name ?n] (not [?e :db/tx-count 1])` excludes the entities whose *name* fact is from tx 1, where before the body matched any fact of the entity; (3) rows that differ only in a source fact's metadata are no longer merged by `or`. `:db/valid-at` is unchanged. The pseudo-attribute pattern now binds or checks a value; it never scans facts, so the planner places it after its source rather than by selectivity. +- **A pseudo-attribute pattern never gives a variable its value** (#606). A variable bound by several patterns returns the value of the first real pattern in the `:where` text that binds it (#545); a `:db/valid-at` pattern that the planner ran first used to bind its entity variable itself, as a ref, so `[:find ?b ?t :where [?b :db/valid-at ?t] [?a :friend ?b] [?b :name "Bob"]]` returned the ref of `:bob` with source order and the keyword `:bob` stored by `:friend` natively. It returns the keyword now, whatever runs first. Only the keyword-or-ref kind changes; rows and counts are the same. +- **Every build reorders query clauses; the `wasm` feature does nothing** (#594, #596). The `wasm` Cargo feature kept patterns, `not` clauses and `or` branches in source order and turned off the `or` short-circuits (#250), for small browser datasets and a predictable order. With every answer independent of the plan (#594, #596, #606) it only made queries slower, and no first-party build enabled it (the browser package uses `browser`). The feature is kept, so builds that name it still resolve, but is deprecated and has no effect. ### Added diff --git a/CLAUDE.md b/CLAUDE.md index 9973847993..d524fd8f7b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -135,7 +135,7 @@ cargo run < demos/demo_negation.txt - `stratification.rs`: `DependencyGraph`, `stratify()` — negative edges + cycle detection - `rules.rs`: `RuleRegistry` — thread-safe rule management - `types.rs`: `EdnValue`, `Pattern`, `DatalogQuery`, `AsOf`, `ValidAt`, `WhereClause` (incl. `Not`, `NotJoin`); `PseudoAttr` enum, `AttributeSpec` wrapper - - `optimizer.rs`: Selectivity-based join reordering; disabled under `wasm` feature. `reorder()` gates every reordering site; tests turn it off per thread (`set_reorder`, `minigraf::__reorder` under `test-oracle`, #596) + - `optimizer.rs`: Selectivity-based join reordering on every build (the `wasm` feature is a deprecated no-op). `reorder()` gates every reordering site; tests turn it off per thread (`set_reorder`, `minigraf::__reorder` under `test-oracle`, #596) - `prepared.rs`: `BindValue`, `PreparedQuery` — parse-once/execute-many with named `$slot` bind slots; `prepare_query`, `substitute` - `magic_sets.rs`: Magic-sets rewriting for demand-driven recursive rule evaluation (not applied to rules with `not`/`not-join`) diff --git a/Cargo.toml b/Cargo.toml index 02a3d57870..c2e58f541d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,6 +82,8 @@ harness = false [features] default = [] +# Deprecated, does nothing since v3.0.0: every build reorders query clauses +# (#594, #596). Kept so that builds naming it still resolve. wasm = [] # Run the existing executor beside the streaming engine and compare every # answer (#473 spec §9). Tests only. diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 715f0d9251..dc32d5b717 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -16,7 +16,7 @@ This covers the unreleased file-format-v8 work (#287, #371): the index-key value - **Reliability and concurrency**: concurrent reads and writes, rollback behavior, same-process handle exclusion, cross-process locking, PID namespaces, NFS and container lock behavior, SIGKILL recovery, and queries seeing each concurrent transaction whole and reading one state for all their patterns, including while a checkpoint runs (`tests/snapshot_atomicity_test.rs`, #554, #555, #558). Generation pins (#543): a pinned generation's pages are never written by later saves or index rebuilds and are reused once the pin is dropped (by page id, `storage::persistent_facts`, `storage::pins`, `storage::page`); fact logs stay correct while checkpoints, rebuilds and concurrent writers run, and see whole transactions only (`tests/fact_log_test.rs`). Query and prepared-query cursors keep their answer and their pages across writes, checkpoints and index rebuilds, and the file stops growing once they close (`tests/cursor_test.rs`); a snapshot leaves out facts appended after it on every scan (`graph::storage`); a crash at every write point of a save with a pinned generation loses no fact and no page, and the reopened database reuses the quarantined ids (`storage::persistent_facts`); the browser withholds free pages a pinned generation may read and releases them once out of force (`storage::pins::split_released`, and a `#[wasm_bindgen_test]` with an open `BrowserDb` cursor). - **Streaming query engine** (#473, #544): every query the engine runs is also run by the existing executor under `cfg(test)` and the `test-oracle` feature, and the answers must agree (`query::engine::oracle`); CI runs the whole suite with the feature, so the model-based, property, golden-corpus and integration suites check the engine. `query::engine::tests` reaches every index choice, net-assert of pending records over committed ones, `:as-of`, valid time, long strings and reads of several batches, in memory, across checkpoints and after reopen, expression filters and bindings (including rebinding and failing expressions), pseudo-attribute columns, keyword/UUID/float constants against keywords, refs and UUID-named keywords, and checks which queries stay on the executor and that INT-026 and QRY-007 come first. Resumed `live_keys` reads equal one live read over random histories (`storage::persistent_facts`). Lazy cursors (`cursor::tests`, #544 PR 3): one pull reads one internal batch of the scan (operator counters), rows left in a batch need no pull, a closed stream reads nothing more, a filter's counters chain, and a cursor's answer is unchanged by writes and by checkpoints that free the leaves it has yet to read (file-backed) or by later writes (in memory); a read error after some rows are gathered comes on the next call, then the cursor ends (`query::engine::tests`). DICT lookups are cached for one pull only, and provisional ids keep their values across pulls (`query::engine::ids`). Joins (#545 PR 2): the oracle matrix adds two- and three-pattern joins on the entity (`IndexNestedLoopJoin`), value and attribute positions (`HashJoin`), cross products, keywords and refs of one entity in both text orders, UUID-named keywords, attributes that name entities, floats `0.0`/`-0.0`, long strings committed and pending, and expressions between joins, at every temporal; joins whose answers span many batches (one probe with more than a batch of matches, cross products, tables built on either side); the planner's join choice; a keyword's entity id (`query::engine::ids`). Pseudo-attributes across joins (#545 PR 3): the metadata of the last pattern before them that matched their entity, chosen per row (a pattern on another entity variable that equals theirs on some rows only, constant entities), pseudo-attribute patterns planned before any pattern with a stored attribute (joined with every fact of their entity, including cross products), and `:db/valid-at` at every temporal with variables, `nil` and integer constants; which pseudo-attribute shapes stay on the executor. - **Prepared time travel** (#603): `tests/prepared_statements_test.rs` runs literal `:as-of` (counter and timestamp), `:valid-at`, `:any-valid-time` and a per-fact pseudo-attribute query through `execute`, a prepared `execute` and a prepared cursor, which must agree; a `prepared.rs` unit test checks that `substitute` keeps non-slot `:as-of` and `:valid-at`. -- **Join rules** (`tests/join_rules_test.rs`, 10 tests; #545): a join on a float value pairs `0.0` with `-0.0`, through the hash key and as a shared variable, and a float constant still matches within epsilon; 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, and the first binder in the text, pattern or expression, gives the value (#596); a pseudo-attribute pattern never replaces a value (native plan order only: under `wasm` the pattern runs first and binds the variable). Under the `test-oracle` feature the joins among them also run on the streaming engine. A unit test checks that `-0.0` and `0.0` hash alike. +- **Join rules** (`tests/join_rules_test.rs`, 10 tests; #545): a join on a float value pairs `0.0` with `-0.0`, through the hash key and as a shared variable, and a float constant still matches within epsilon; 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, and the first binder in the text, pattern or expression, gives the value (#596); 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). Under the `test-oracle` feature the joins among them also run on the streaming engine. A unit test checks that `-0.0` and `0.0` hash alike. - **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, `:alice` and its UUID are one term while `?e` and `:alice` are not, PRS-081 for a pattern after it, alone, `_`/`?_x` entities, another entity's pattern and a rule call; a `not` body reads the outer fact (and a source inside the body), `not-join` crosses for a listed variable only, `or` branches read the outer fact and never a sibling, `or-join`, closed rule bodies, `$slot`s by name at prepare time, and `:db/valid-at` once per fact. Parser unit tests check the hidden variables (`?#.`), the join variable a crossed `not-join` gains, and the PRS-081 text; an optimizer test places a pseudo-attribute pattern after its source and the pattern binding its value; matcher tests read the hidden variable, a substituted integer, and nothing without a source; the engine tests run the sourced shapes and check PRS-081 for the old fallback shapes. The plan-independence matrix gains eleven pseudo-attribute shapes. - **Plan independence** (`tests/plan_independence_test.rs`, 7 tests, `test-oracle` feature only; #596): a matrix of queries (joins in both text orders, cross products, keyword and ref, floats, long strings, filter and binding expressions, recursive rules and rules with `not`, `or`/`or-join` including the #250 short-circuit shapes, placed and deferred `not`/`not-join`, aggregates, `:as-of`, `:valid-at`, `:any-valid-time`), in memory and on file after a checkpoint and a reopen, through `execute` and a cursor, gives the same bag of rows with clause reordering on and off (`minigraf::__reorder`); #596's example and the unification cases (an expression whose output is its input, two expressions on one variable, integer vs float, `-0.0` vs `0.0`). Under `test-oracle` the model-based and property tests also rerun every generated query with reordering off. Unit tests cover `apply_expr` (equal, different, integer vs float, NaN, `-0.0`/`0.0` with and without replacing, keyword vs ref both ways, a failing expression), `Binders::expression`, the switch (source order kept, per thread, restored on drop), and the engine planner's `Bind` modes and build side with the switch off; the engine's join matrix gains six unification shapes. - **Public diagnostics**: structured parser, query, storage, WAL, API, and internal error codes; the error-code registry is checked against [`ERROR_REFERENCE.md`](ERROR_REFERENCE.md). diff --git a/src/query/datalog/evaluator.rs b/src/query/datalog/evaluator.rs index d50b0ee230..6f0aae6fc9 100644 --- a/src/query/datalog/evaluator.rs +++ b/src/query/datalog/evaluator.rs @@ -810,7 +810,6 @@ impl StratifiedEvaluator { }) .collect(); - // WASM omission: small datasets + determinism — see optimizer::selectivity_score(). if crate::query::datalog::optimizer::reorder() { not_clauses.sort_by_key(|body| { crate::query::datalog::optimizer::clause_cost(&WhereClause::Not( diff --git a/src/query/datalog/executor.rs b/src/query/datalog/executor.rs index 6da0e0ec85..165353a434 100644 --- a/src/query/datalog/executor.rs +++ b/src/query/datalog/executor.rs @@ -1489,7 +1489,8 @@ type Binding = std::collections::HashMap; /// returned as they were bound. An expression binding is a binder like a /// pattern: when the variable is already bound it keeps the rows where its /// result is equal (#596), and gives the value when it is first in the text. -/// A pseudo-attribute pattern never replaces a value. +/// A pseudo-attribute pattern never gives a variable its value: a real +/// pattern that binds it replaces its value, wherever it is (#606). pub(crate) struct Binders { /// Each top-level pattern (rule calls as patterns) and its position in /// `:where`; taken as the planner hands them out. @@ -1532,11 +1533,17 @@ impl Binders { /// Record the pattern `p`, run next, and return the variables whose /// value it replaces. pub(crate) fn pattern(&mut self, p: &Pattern) -> Vec { + let real = matches!(p.attribute, AttributeSpec::Real(_)); let at = match self.text.iter().position(|(q, _)| q == p) { - Some(i) => self.text.swap_remove(i).1, + // A pseudo-attribute pattern never gives a variable its value: it + // ranks after every real pattern, whatever runs first (#606). + Some(i) if real => self.text.swap_remove(i).1, + Some(i) => { + self.text.swap_remove(i); + usize::MAX + } None => usize::MAX, }; - let real = matches!(p.attribute, AttributeSpec::Real(_)); let attribute = match &p.attribute { AttributeSpec::Real(a) => Some(a), AttributeSpec::Pseudo(_) => None, @@ -2086,7 +2093,6 @@ pub(crate) fn apply_or_clauses( let mut b: Vec<&Vec> = branches.iter().collect(); // Sort branches by cost ascending so cheaper branches evaluate first, // maximizing the chance the short-circuit below fires early (#250). - // WASM omission: small datasets + determinism — see optimizer::selectivity_score(). if optimizer::reorder() { b.sort_by_key(|br| optimizer::branch_cost(br)); } @@ -2313,7 +2319,6 @@ pub(crate) fn apply_or_clauses( let mut b: Vec<&Vec> = branches.iter().collect(); // Sort branches by cost ascending so cheaper branches evaluate first, // maximizing the chance the short-circuit below fires early (#250). - // WASM omission: small datasets + determinism — see optimizer::selectivity_score(). if optimizer::reorder() { b.sort_by_key(|br| optimizer::branch_cost(br)); } diff --git a/src/query/datalog/optimizer.rs b/src/query/datalog/optimizer.rs index c405545353..9f5be75d6e 100644 --- a/src/query/datalog/optimizer.rs +++ b/src/query/datalog/optimizer.rs @@ -1,7 +1,7 @@ //! Query optimizer: index selection and join ordering for Datalog patterns. //! //! `plan()` is the single entry point. It assigns an `IndexHint` to each -//! pattern and (outside the `wasm` feature) sorts patterns by selectivity. +//! pattern and sorts patterns by selectivity, on every build. //! //! [`reorder`] says whether this build reorders: patterns, `not`/`not-join` //! clauses and `or` branches by cost, the `or` short-circuits (#250), and the @@ -16,15 +16,16 @@ thread_local! { static REORDER: std::cell::Cell = const { std::cell::Cell::new(true) }; } -/// Whether queries reorder clauses by cost: never under the `wasm` feature, -/// and, in tests, only while this thread's switch is on ([`set_reorder`]). +/// Whether queries reorder clauses by cost: always, except in tests while +/// this thread's switch is off ([`set_reorder`]). The `wasm` feature no +/// longer turns it off: no answer depends on the plan (#594, #596). #[inline] pub(crate) fn reorder() -> bool { #[cfg(any(test, feature = "test-oracle"))] if !REORDER.with(std::cell::Cell::get) { return false; } - !cfg!(feature = "wasm") + true } /// Turns reordering on or off for queries on this thread until the guard @@ -81,10 +82,6 @@ fn attr_is_index_bound(a: &AttributeSpec) -> bool { /// Count the number of non-variable components in a pattern. /// Higher score = more selective. /// -/// Not used under the `wasm` feature ([`reorder`]): WASM/browser targets typically -/// work with small datasets where the overhead of computing scores and sorting can -/// equal or exceed the benefit. Source order is preserved there for deterministic, -/// debuggable query behaviour. fn selectivity_score(p: &Pattern) -> u8 { let e = !is_variable(&p.entity); let a = attr_is_index_bound(&p.attribute); @@ -238,7 +235,6 @@ pub fn plan( // Stable sort patterns by selectivity descending (when reordering). // Preserves original order for ties, ensuring deterministic output. - // WASM omission: see selectivity_score() — small datasets, determinism. if reorder() { patterns.sort_by_key(|(clause, _)| { if let WhereClause::Pattern(p) = clause { @@ -508,8 +504,6 @@ mod tests { let p = make_pattern(var("e"), var("a"), var("v")); assert_eq!(select_index(&p), IndexHint::Eavt); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_join_ordering_moves_selective_pattern_first() { let p1 = make_pattern(var("e"), kw(":age"), var("a")); // selectivity 1 (attr only) @@ -601,15 +595,15 @@ mod tests { #[cfg(not(target_arch = "wasm32"))] { let other = std::thread::spawn(reorder).join().unwrap(); - assert_eq!(other, !cfg!(feature = "wasm"), "other thread unaffected"); + assert!(other, "other thread unaffected"); } { let _on = set_reorder(true); - assert_eq!(reorder(), !cfg!(feature = "wasm")); + assert!(reorder()); } assert!(!reorder(), "inner guard restores off"); } - assert_eq!(reorder(), !cfg!(feature = "wasm"), "restored on drop"); + assert!(reorder(), "restored on drop"); } // ── expr_vars() ────────────────────────────────────────────────────────── @@ -666,7 +660,6 @@ mod tests { #[test] fn test_plan_pattern_carries_some_hint() { - #[cfg(not(feature = "wasm"))] { let p = WhereClause::Pattern(make_pattern(var("e"), kw(":val"), var("v"))); let (planned, _deferred) = plan(vec![p]); @@ -679,7 +672,6 @@ mod tests { #[test] fn test_plan_expr_carries_none_hint() { - #[cfg(not(feature = "wasm"))] { let p = WhereClause::Pattern(make_pattern(var("e"), kw(":val"), var("v"))); let expr = WhereClause::Expr { @@ -697,8 +689,6 @@ mod tests { ); } } - - #[cfg(not(feature = "wasm"))] #[test] fn test_expr_pushed_after_binding_pattern() { // Three patterns with equal selectivity (1 attr bound each) — stable sort preserves @@ -728,8 +718,6 @@ mod tests { "p3 must be at index 3" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_expr_no_vars_goes_to_end() { let p1 = WhereClause::Pattern(make_pattern(var("e"), kw(":val"), var("v"))); @@ -744,8 +732,6 @@ mod tests { "no-var Expr must be last" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_expr_unbound_var_goes_to_end() { // ?x is never bound by any pattern @@ -767,8 +753,6 @@ mod tests { } // ── Not/NotJoin push-down (#248) ───────────────────────────────────── - - #[cfg(not(feature = "wasm"))] #[test] fn test_not_pushed_after_binding_pattern() { // p1 binds ?e/?n, p2 binds ?e/?v. Not body needs ?v, bound at p2 (pos 1). @@ -793,8 +777,6 @@ mod tests { "p3 must be pushed after the not clause" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_not_with_unbound_var_is_deferred() { // Not body references ?z, which no pattern in this list ever binds @@ -814,8 +796,6 @@ mod tests { assert_eq!(deferred.len(), 1, "unplaceable not clause must be deferred"); assert_eq!(deferred[0], not_clause); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_not_join_pushed_to_correct_position() { // not-join requires only ?e bound (join_vars), which p1 binds at pos 0. @@ -838,8 +818,6 @@ mod tests { "NotJoin must be placed right after the pattern binding ?e (index 0)" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_not_join_unbound_join_var_is_deferred() { let p1 = WhereClause::Pattern(make_pattern(var("e"), kw(":name"), var("n"))); @@ -859,8 +837,6 @@ mod tests { "not-join with unbound join var must be deferred" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_not_with_no_required_vars_goes_first() { // Not body is fully self-contained (no outer vars referenced) — safe to run @@ -879,8 +855,6 @@ mod tests { "Not clause with no required vars must be placed first" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_not_wildcard_var_does_not_block_placement() { // `?_x` is the wildcard idiom — it must not count as an outer dependency, @@ -904,8 +878,6 @@ mod tests { "Not must be pushed right after the pattern binding ?e, not deferred to the end" ); } - - #[cfg(not(feature = "wasm"))] #[test] fn test_multiple_not_clauses_at_same_position_stay_cheapest_first() { // Both Not bodies need only ?e (bound by p1) and land at the same insertion diff --git a/src/query/engine/plan.rs b/src/query/engine/plan.rs index fb5d1e20c8..55575128b3 100644 --- a/src/query/engine/plan.rs +++ b/src/query/engine/plan.rs @@ -315,9 +315,8 @@ pub(crate) fn plan(query: &DatalogQuery) -> Option { } if let JoinKind::Hash { build_inner } = &mut join.kind { // With the test switch off, always the inner pattern - // (decision 3 of the #596 plan); `wasm` keeps the cost choice. - let cost = optimizer::reorder() || cfg!(feature = "wasm"); - *build_inner = !cost + // (decision 3 of the #596 plan). + *build_inner = !optimizer::reorder() || optimizer::branch_cost(&[WhereClause::Pattern(q.clone())]) <= optimizer::branch_cost(&outer_patterns); } diff --git a/src/query/engine/tests.rs b/src/query/engine/tests.rs index b2aac37954..dcef76f7d1 100644 --- a/src/query/engine/tests.rs +++ b/src/query/engine/tests.rs @@ -739,14 +739,12 @@ fn planner_chooses_the_join() { ["hash inner=false"] ); // With the test switch off the table is always built on the inner - // pattern (`wasm` keeps the cost choice). + // pattern. let _off = crate::query::datalog::optimizer::set_reorder(false); - if !cfg!(feature = "wasm") { - assert_eq!( - kinds("(query [:find ?e :where [?e :a 1] [?f :b ?y] [?f :c ?e]])"), - ["hash inner=true", "probe"] - ); - } + assert_eq!( + kinds("(query [:find ?e :where [?e :a 1] [?f :b ?y] [?f :c ?e]])"), + ["hash inner=true", "probe"] + ); } #[test] diff --git a/tests/disjunction_test.rs b/tests/disjunction_test.rs index 79d8682f34..a5f25c6e1b 100644 --- a/tests/disjunction_test.rs +++ b/tests/disjunction_test.rs @@ -388,8 +388,6 @@ fn test_or_join_does_not_return_duplicate_bindings() { /// before merging, so a branch can't smuggle in a new distinguishing variable): /// once the cheap branch covers every incoming join-var key, the expensive branch /// (guarded by a call-counting predicate) must never run. -// The #250 short-circuits are native-only (`optimizer::reorder`). -#[cfg(not(feature = "wasm"))] #[test] fn test_or_join_short_circuits_expensive_branch_when_fully_covered() { let db = db(); @@ -478,8 +476,6 @@ fn test_or_join_falls_back_to_expensive_branch_when_not_fully_covered() { /// is 1). If `branch_cost`-based sorting were broken (e.g. reverted to declaration /// order), the expensive branch — written first — would run before coverage is /// established and invoke the counter. -// The #250 short-circuits are native-only (`optimizer::reorder`). -#[cfg(not(feature = "wasm"))] #[test] fn test_or_join_short_circuit_relies_on_cost_sort_not_declaration_order() { let db = db(); @@ -530,8 +526,6 @@ fn test_or_join_short_circuit_relies_on_cost_sort_not_declaration_order() { /// (21) Plain `or` short-circuits too, when it's a pure filter (no branch introduces /// a variable beyond what's already bound in the incoming scope). -// The #250 short-circuits are native-only (`optimizer::reorder`). -#[cfg(not(feature = "wasm"))] #[test] fn test_or_short_circuits_pure_filter_when_fully_covered() { let db = db(); diff --git a/tests/join_rules_test.rs b/tests/join_rules_test.rs index d68e713c1c..636d567b7e 100644 --- a/tests/join_rules_test.rs +++ b/tests/join_rules_test.rs @@ -172,9 +172,7 @@ fn keyword_or_ref_with_a_repeated_pattern() { ); } -// Under `wasm` the planner keeps source order, so the pseudo-attribute -// pattern runs first and binds `?b` itself, as the entity's ref. -#[cfg(not(feature = "wasm"))] +// Whatever runs first, the first real pattern gives `?b` its value (#606). #[test] fn pseudo_attribute_pattern_never_replaces_a_value() { let db = friends_db(); diff --git a/tests/plan_independence_test.rs b/tests/plan_independence_test.rs index 4d3fab6cc7..290ebaea66 100644 --- a/tests/plan_independence_test.rs +++ b/tests/plan_independence_test.rs @@ -172,6 +172,10 @@ const MATRIX: &[&str] = &[ r#"[:find ?n :any-valid-time :where [?e :name ?n] (or [?e :db/tx-count 1] [?e :age 40])]"#, r#"[:find ?n :any-valid-time :where [?e :name ?n] (or-join [?e] [?e :db/tx-count 1] [?e :friend _])]"#, r#"[:find ?e ?t :any-valid-time :where (stamped ?e ?t) [?e :age ?a]]"#, + // `:db/valid-at` first in the text: it never gives a variable's value. + // (A fixed `:valid-at`: by default `?t` is each query's "now".) + r#"[:find ?b ?t :valid-at "2030-01-01T00:00:00Z" :where [?b :db/valid-at ?t] [?a :friend ?b] [?b :name ?n]]"#, + r#"[:find ?b :where [?b :db/valid-at ?t] [?a :friend ?b]]"#, // Time travel. r#"[:find ?n :as-of 1 :where [?e :name ?n] [?e :age ?a]]"#, r#"[:find ?n ?a :valid-at "2020-06-01T00:00:00Z" :where [?e :name ?n] [?e :age ?a]]"#,