From 8ac7d3d0507abe3cab03404ca0e69386deaa7857 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 21 Oct 2026 18:47:55 +0530 Subject: [PATCH] fix(prepared): prepared queries use the database's rule limits (#679) Backport of the v3 fix in #676. PreparedQuery::execute built its DatalogExecutor without set_limits, so a prepared rule query ran with the default max_derived_facts and max_results whatever OpenOptions set: a lower limit did not stop it, and a higher one did not help it. prepare now hands the database's (max_derived_facts, max_results) to the PreparedQuery, which sets them on every execute, as every other read path in db.rs does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019kY61Y11gcRD91Y6b7jbGT --- CHANGELOG.md | 4 +++ CLAUDE.md | 2 +- README.md | 2 +- docs/TEST_COVERAGE.md | 2 +- src/db.rs | 4 +++ src/query/datalog/prepared.rs | 7 ++++- tests/prepared_statements_test.rs | 51 +++++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aee2a5963..a1534bacb7 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 + +- **Prepared queries use the database's rule limits** (#679), backported from v3.0.0. `PreparedQuery::execute` ran a rule query with the default `max_derived_facts` and `max_results` of 1,000,000 whatever `OpenOptions` set, so a lower limit set to stop a runaway recursive rule did not apply to prepared queries, and a higher one set for a large derivation failed with `INT-020` where `execute` of the same query succeeded. It now uses the database's limits, as `execute` does. `:max-derived-facts` and `:max-results` written in the query text were always applied. Present since v0.19.0. + ## v2.0.9 — 2026-10-21 Patch release on the v2.x line under the [support policy](PHILOSOPHY.md#support-policy): two data-integrity fixes, for a pseudo-attribute in a rule body that read the wrong valid time and tx metadata (#661) and for a fact of more than four elements whose extra elements were silently dropped (#649), plus error codes in `BrowserDb` rejections (#673). File format is unchanged (v7). No API changes; a fact of more than four elements, which used to be stored truncated, now fails with `PRS-108`. Upgrading is recommended for every v2.x user who reads `:db/valid-from`, `:db/valid-to`, `:db/tx-count` or `:db/tx-id` in rules. diff --git a/CLAUDE.md b/CLAUDE.md index 6e9c094fb8..235b7821b9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -175,7 +175,7 @@ Auto-migrates v1/v2/v3/v4/v5/v6 → v7 on open/checkpoint. ## Test Coverage -**1318 tests** (1309 passing, 9 ignored; unit + integration + doc). +**1321 tests** (1312 passing, 9 ignored; unit + integration + doc). 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/README.md b/README.md index 04dbde559d..91d9f11eeb 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ let r2 = pq.execute(&[("tx", BindValue::TxCount(2)), ("entity", BindValue::Entit ```bash cargo run # interactive Datalog REPL -cargo test # run 1318 tests +cargo test # run 1321 tests cargo run < demos/demo_recursive.txt # recursive rules demo ``` diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index 28aedbc472..1e86cd8cdf 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -14,7 +14,7 @@ For v2.0.5, torn-WAL-tail tests (#513) check that opening a WAL writer cuts off `tests/crash_kill_test.rs` kills a child process running a seeded workload under five checkpoint modes at a random point and checks that the reopened file holds exactly the transactions the child logged as committed, or those plus the one in flight, through full-scan, AEVT, AVET, EAVT and `:as-of` queries, again after a second reopen and after a checkpoint plus reopen (#384). The workload leaves out the v2.x known issues #371 and #435. -`tests/prepared_statements_test.rs` checks that a prepared query with a literal `:as-of`, `:valid-at` or `:any-valid-time` returns the same rows as the unprepared query (#603). +`tests/prepared_statements_test.rs` checks that a prepared query with a literal `:as-of`, `:valid-at` or `:any-valid-time` returns the same rows as the unprepared query (#603), and that a prepared rule query fails with `INT-020` one below the database's `max_derived_facts` or `max_results` and succeeds at it, as `execute` does (#679). `tests/magic_sets_test.rs` checks rule queries with a literal argument where some call site cannot carry the magic-sets demand (#608): a rule-body call with an argument nothing binds yet, the same rule called with free arguments, a call from a rule called with free arguments, calls inside `or` and `not`, a fully literal call, and a call bound only by a variable; `magic_sets.rs` unit tests check the adornment each shape solves to. diff --git a/src/db.rs b/src/db.rs index 66059e09ed..e9e84a4237 100644 --- a/src/db.rs +++ b/src/db.rs @@ -852,6 +852,10 @@ impl Minigraf { self.inner.fact_storage.clone(), self.inner.rules.clone(), self.inner.functions.clone(), + ( + self.inner.options.max_derived_facts, + self.inner.options.max_results, + ), ) } diff --git a/src/query/datalog/prepared.rs b/src/query/datalog/prepared.rs index 7e8ef31c3c..178b3e3617 100644 --- a/src/query/datalog/prepared.rs +++ b/src/query/datalog/prepared.rs @@ -81,6 +81,8 @@ pub struct PreparedQuery { fact_storage: FactStorage, rules: Arc>, functions: Arc>, + /// The database's `max_derived_facts` and `max_results`. + limits: (usize, usize), } impl std::fmt::Debug for PreparedQuery { @@ -115,11 +117,12 @@ impl PreparedQuery { let filled_query = substitute(&self.template, &binding_map)?; - let executor = DatalogExecutor::new_with_rules_and_functions( + let mut executor = DatalogExecutor::new_with_rules_and_functions( self.fact_storage.clone(), self.rules.clone(), self.functions.clone(), ); + executor.set_limits(self.limits.0, self.limits.1); executor.execute(DatalogCommand::Query(filled_query)) } } @@ -131,6 +134,7 @@ pub(crate) fn prepare_query( fact_storage: FactStorage, rules: Arc>, functions: Arc>, + limits: (usize, usize), ) -> Result { validate_no_attribute_slots(&query)?; let slot_names = collect_slot_names(&query); @@ -140,6 +144,7 @@ pub(crate) fn prepare_query( fact_storage, rules, functions, + limits, }) } diff --git a/tests/prepared_statements_test.rs b/tests/prepared_statements_test.rs index 0ce8e94dbb..88cafee696 100644 --- a/tests/prepared_statements_test.rs +++ b/tests/prepared_statements_test.rs @@ -499,3 +499,54 @@ fn prepared_literal_as_of_and_valid_at_are_kept() { 2 ); } + +// ─── Database limits (#679) ─────────────────────────────────────────────────── + +/// A friend chain a -> b -> c -> d -> e with a recursive `reach` rule: +/// 10 derived facts in all, and the last pass derives all 10 again. +fn reach_db(opts: minigraf::OpenOptions) -> Minigraf { + let db = opts.open_memory().unwrap(); + db.execute("(transact [[:a :friend :b] [:b :friend :c] [:c :friend :d] [:d :friend :e]])") + .unwrap(); + db.execute("(rule [(reach ?x ?y) [?x :friend ?y]])") + .unwrap(); + db.execute("(rule [(reach ?x ?z) (reach ?x ?y) [?y :friend ?z]])") + .unwrap(); + db +} + +fn execute_reach(db: &Minigraf) -> Result { + db.execute("(query [:find ?x ?y :where (reach ?x ?y) [?y :friend :e]])") +} + +fn prepared_reach(db: &Minigraf) -> Result { + db.prepare("(query [:find ?x ?y :where (reach ?x ?y) [?y :friend $z]])") + .unwrap() + .execute(&[("z", BindValue::Val(Value::Keyword(":e".to_string())))]) +} + +#[test] +fn prepared_rule_queries_use_the_database_max_derived_facts() { + let db = reach_db(minigraf::OpenOptions::new().max_derived_facts(9)); + assert_eq!(execute_reach(&db).expect_err("10 > 9").code(), "INT-020"); + assert_eq!(prepared_reach(&db).expect_err("10 > 9").code(), "INT-020"); +} + +#[test] +fn prepared_rule_queries_use_the_database_max_results() { + let db = reach_db(minigraf::OpenOptions::new().max_results(9)); + assert_eq!(execute_reach(&db).expect_err("10 > 9").code(), "INT-020"); + assert_eq!(prepared_reach(&db).expect_err("10 > 9").code(), "INT-020"); +} + +#[test] +fn prepared_rule_queries_succeed_at_the_database_limits() { + // Exactly at both limits. + let db = reach_db( + minigraf::OpenOptions::new() + .max_derived_facts(10) + .max_results(10), + ); + execute_reach(&db).unwrap(); + prepared_reach(&db).unwrap(); +}