2017-03-06 22:40:10 +00:00
|
|
|
// Copyright 2016 Mozilla
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
|
|
// this file except in compliance with the License. You may obtain a copy of the
|
|
|
|
// License at http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software distributed
|
|
|
|
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
|
|
// specific language governing permissions and limitations under the License.
|
|
|
|
|
2018-02-01 17:17:07 +00:00
|
|
|
#[macro_use]
|
2017-03-06 22:40:10 +00:00
|
|
|
extern crate mentat;
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// TODO: when we switch to `failure`, make this more humane.
|
2020-08-06 03:03:58 +00:00
|
|
|
//use query_algebrizer_traits; // For errors;
|
2017-03-06 22:40:10 +00:00
|
|
|
|
2017-04-29 03:11:55 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use chrono::FixedOffset;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use core_traits::{Entid, KnownEntid, ValueType, ValueTypeSet};
|
2018-08-08 17:35:06 +00:00
|
|
|
|
2021-07-02 22:09:07 +00:00
|
|
|
use edn::OrderedFloat;
|
2020-01-14 15:46:21 +00:00
|
|
|
use mentat_core::{DateTime, HasSchema, Utc, Uuid};
|
2018-03-15 14:14:06 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use query_projector_traits::aggregates::SimpleAggregationOp;
|
2017-03-06 22:40:10 +00:00
|
|
|
|
|
|
|
use mentat::{
|
2020-01-14 15:46:21 +00:00
|
|
|
new_connection, Binding, IntoResult, Keyword, PlainSymbol, QueryInputs, QueryResults,
|
|
|
|
Queryable, RelResult, Store, TxReport, TypedValue, Variable,
|
2017-03-06 22:40:10 +00:00
|
|
|
};
|
|
|
|
|
2018-02-14 00:51:21 +00:00
|
|
|
use mentat::query::q_uncached;
|
|
|
|
|
2017-04-29 03:11:55 +00:00
|
|
|
use mentat::conn::Conn;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use public_traits::errors::MentatError;
|
2017-04-17 20:14:30 +00:00
|
|
|
|
2020-08-07 13:15:36 +00:00
|
|
|
use assert_approx_eq::assert_approx_eq;
|
|
|
|
|
2017-03-06 22:40:10 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rel() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// Rel.
|
2020-01-14 15:46:21 +00:00
|
|
|
let start = time::Instant::now();
|
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find ?x ?ident :where [?x :db/ident ?ident]]",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("Query failed")
|
|
|
|
.results;
|
|
|
|
let end = time::Instant::now();
|
2017-03-06 22:40:10 +00:00
|
|
|
|
|
|
|
// This will need to change each time we add a default ident.
|
2017-12-20 23:26:45 +00:00
|
|
|
assert_eq!(40, results.len());
|
2017-03-06 22:40:10 +00:00
|
|
|
|
|
|
|
// Every row is a pair of a Ref and a Keyword.
|
2018-04-24 22:04:00 +00:00
|
|
|
if let QueryResults::Rel(rel) = results {
|
|
|
|
for r in rel.into_iter() {
|
2017-03-06 22:40:10 +00:00
|
|
|
assert_eq!(r.len(), 2);
|
|
|
|
assert!(r[0].matches_type(ValueType::Ref));
|
|
|
|
assert!(r[1].matches_type(ValueType::Keyword));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("Expected rel.");
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
println!("Rel took {}µs", (end - start).whole_microseconds());
|
2017-03-06 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_failing_scalar() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// Scalar that fails.
|
2020-01-14 15:46:21 +00:00
|
|
|
let start = time::Instant::now();
|
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find ?x . :where [?x :db/fulltext true]]",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("Query failed")
|
|
|
|
.results;
|
|
|
|
let end = time::Instant::now();
|
2017-03-06 22:40:10 +00:00
|
|
|
|
|
|
|
assert_eq!(0, results.len());
|
|
|
|
|
|
|
|
if let QueryResults::Scalar(None) = results {
|
|
|
|
} else {
|
|
|
|
panic!("Expected failed scalar.");
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
println!(
|
|
|
|
"Failing scalar took {}µs",
|
|
|
|
(end - start).whole_microseconds()
|
|
|
|
);
|
2017-03-06 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_scalar() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// Scalar that succeeds.
|
2020-01-14 15:46:21 +00:00
|
|
|
let start = time::Instant::now();
|
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find ?ident . :where [24 :db/ident ?ident]]",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("Query failed")
|
|
|
|
.results;
|
|
|
|
let end = time::Instant::now();
|
2017-03-06 22:40:10 +00:00
|
|
|
|
|
|
|
assert_eq!(1, results.len());
|
|
|
|
|
2018-04-24 22:08:38 +00:00
|
|
|
if let QueryResults::Scalar(Some(Binding::Scalar(TypedValue::Keyword(ref rc)))) = results {
|
2017-03-06 22:40:10 +00:00
|
|
|
// Should be '24'.
|
2018-05-11 16:52:17 +00:00
|
|
|
assert_eq!(&Keyword::namespaced("db.type", "keyword"), rc.as_ref());
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(KnownEntid(24), db.schema.get_entid(rc).unwrap());
|
2017-03-06 22:40:10 +00:00
|
|
|
} else {
|
|
|
|
panic!("Expected scalar.");
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("{:?}", results);
|
2020-01-14 15:46:21 +00:00
|
|
|
println!("Scalar took {}µs", (end - start).whole_microseconds());
|
2017-03-06 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// Tuple.
|
2020-01-14 15:46:21 +00:00
|
|
|
let start = time::Instant::now();
|
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find [?index ?cardinality]
|
2018-02-14 00:51:21 +00:00
|
|
|
:where [:db/txInstant :db/index ?index]
|
|
|
|
[:db/txInstant :db/cardinality ?cardinality]]",
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("Query failed")
|
|
|
|
.results;
|
|
|
|
let end = time::Instant::now();
|
2017-03-06 22:40:10 +00:00
|
|
|
|
|
|
|
assert_eq!(1, results.len());
|
|
|
|
|
|
|
|
if let QueryResults::Tuple(Some(ref tuple)) = results {
|
2018-05-11 16:52:17 +00:00
|
|
|
let cardinality_one = Keyword::namespaced("db.cardinality", "one");
|
2017-03-06 22:40:10 +00:00
|
|
|
assert_eq!(tuple.len(), 2);
|
2018-04-24 22:08:38 +00:00
|
|
|
assert_eq!(tuple[0], TypedValue::Boolean(true).into());
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
tuple[1],
|
|
|
|
db.schema.get_entid(&cardinality_one).expect("c1").into()
|
|
|
|
);
|
2017-03-06 22:40:10 +00:00
|
|
|
} else {
|
|
|
|
panic!("Expected tuple.");
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("{:?}", results);
|
2020-01-14 15:46:21 +00:00
|
|
|
println!("Tuple took {}µs", (end - start).whole_microseconds());
|
2017-03-06 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_coll() {
|
2017-03-06 22:55:14 +00:00
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// Coll.
|
2020-01-14 15:46:21 +00:00
|
|
|
let start = time::Instant::now();
|
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find [?e ...] :where [?e :db/ident _]]",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("Query failed")
|
|
|
|
.results;
|
|
|
|
let end = time::Instant::now();
|
2017-03-06 22:55:14 +00:00
|
|
|
|
2017-12-20 23:26:45 +00:00
|
|
|
assert_eq!(40, results.len());
|
2017-03-06 22:55:14 +00:00
|
|
|
|
|
|
|
if let QueryResults::Coll(ref coll) = results {
|
|
|
|
assert!(coll.iter().all(|item| item.matches_type(ValueType::Ref)));
|
|
|
|
} else {
|
|
|
|
panic!("Expected coll.");
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("{:?}", results);
|
2020-01-14 15:46:21 +00:00
|
|
|
println!("Coll took {}µs", (end - start).whole_microseconds());
|
2017-03-06 22:40:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:14:30 +00:00
|
|
|
#[test]
|
|
|
|
fn test_inputs() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// entids::DB_INSTALL_VALUE_TYPE = 5.
|
|
|
|
let ee = (Variable::from_valid_name("?e"), TypedValue::Ref(5));
|
|
|
|
let inputs = QueryInputs::with_value_sequence(vec![ee]);
|
2020-01-14 15:46:21 +00:00
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find ?i . :in ?e :where [?e :db/ident ?i]]",
|
|
|
|
inputs,
|
|
|
|
)
|
|
|
|
.expect("query to succeed")
|
|
|
|
.results;
|
2017-04-17 20:14:30 +00:00
|
|
|
|
2018-04-24 22:08:38 +00:00
|
|
|
if let QueryResults::Scalar(Some(Binding::Scalar(TypedValue::Keyword(value)))) = results {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
value.as_ref(),
|
|
|
|
&Keyword::namespaced("db.install", "valueType")
|
|
|
|
);
|
2017-04-17 20:14:30 +00:00
|
|
|
} else {
|
|
|
|
panic!("Expected scalar.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensure that a query won't be run without all of its `:in` variables being bound.
|
|
|
|
#[test]
|
|
|
|
fn test_unbound_inputs() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let db = mentat_db::db::ensure_current_version(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
|
|
|
// Bind the wrong var by 'mistake'.
|
|
|
|
let xx = (Variable::from_valid_name("?x"), TypedValue::Ref(5));
|
|
|
|
let inputs = QueryInputs::with_value_sequence(vec![xx]);
|
2020-01-14 15:46:21 +00:00
|
|
|
let results = q_uncached(
|
|
|
|
&c,
|
|
|
|
&db.schema,
|
|
|
|
"[:find ?i . :in ?e :where [?e :db/ident ?i]]",
|
|
|
|
inputs,
|
|
|
|
);
|
2017-04-17 20:14:30 +00:00
|
|
|
|
2018-06-27 20:19:40 +00:00
|
|
|
match results.expect_err("expected unbound variables") {
|
2018-06-04 22:07:09 +00:00
|
|
|
MentatError::UnboundVariables(vars) => {
|
2017-04-17 20:14:30 +00:00
|
|
|
assert_eq!(vars, vec!["?e".to_string()].into_iter().collect());
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-06-04 22:07:09 +00:00
|
|
|
_ => panic!("Expected UnboundVariables variant."),
|
2017-04-17 20:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-29 03:11:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_instants_and_uuids() {
|
|
|
|
// We assume, perhaps foolishly, that the clocks on test machines won't lose more than an
|
|
|
|
// hour while this test is running.
|
2023-12-01 16:21:21 +00:00
|
|
|
let start = Utc::now() + FixedOffset::west_opt(60 * 60).unwrap();
|
2017-04-29 03:11:55 +00:00
|
|
|
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-04-29 03:11:55 +00:00
|
|
|
[:db/add "s" :db/ident :foo/uuid]
|
|
|
|
[:db/add "s" :db/valueType :db.type/uuid]
|
|
|
|
[:db/add "s" :db/cardinality :db.cardinality/one]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-04-29 03:11:55 +00:00
|
|
|
[:db/add "u" :foo/uuid #uuid "cf62d552-6569-4d1b-b667-04703041dfc4"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let r = conn
|
|
|
|
.q_once(
|
2020-08-06 03:03:58 +00:00
|
|
|
&c,
|
2020-01-14 15:46:21 +00:00
|
|
|
r#"[:find [?x ?u ?when]
|
2017-06-28 17:20:16 +00:00
|
|
|
:where [?x :foo/uuid ?u ?tx]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?tx :db/txInstant ?when]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2017-04-29 03:11:55 +00:00
|
|
|
match r {
|
2018-01-30 22:11:41 +00:00
|
|
|
QueryResults::Tuple(Some(vals)) => {
|
2017-04-29 03:11:55 +00:00
|
|
|
let mut vals = vals.into_iter();
|
|
|
|
match (vals.next(), vals.next(), vals.next(), vals.next()) {
|
2020-01-14 15:46:21 +00:00
|
|
|
(
|
|
|
|
Some(Binding::Scalar(TypedValue::Ref(e))),
|
|
|
|
Some(Binding::Scalar(TypedValue::Uuid(u))),
|
|
|
|
Some(Binding::Scalar(TypedValue::Instant(t))),
|
|
|
|
None,
|
|
|
|
) => {
|
|
|
|
assert!(e > 40); // There are at least this many entities in the store.
|
|
|
|
assert_eq!(
|
|
|
|
Ok(u),
|
|
|
|
Uuid::from_str("cf62d552-6569-4d1b-b667-04703041dfc4")
|
|
|
|
);
|
|
|
|
assert!(t > start);
|
|
|
|
}
|
|
|
|
_ => panic!("Unexpected results."),
|
2017-04-29 03:11:55 +00:00
|
|
|
}
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2017-04-29 03:11:55 +00:00
|
|
|
_ => panic!("Expected query to work."),
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 21:19:35 +00:00
|
|
|
|
2017-06-28 17:20:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_tx() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "s" :db/ident :foo/uuid]
|
|
|
|
[:db/add "s" :db/valueType :db.type/uuid]
|
|
|
|
[:db/add "s" :db/cardinality :db.cardinality/one]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
|
|
|
|
|
|
|
let t = conn
|
|
|
|
.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "u" :foo/uuid #uuid "cf62d552-6569-4d1b-b667-04703041dfc4"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
2017-06-28 17:20:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "u" :foo/uuid #uuid "550e8400-e29b-41d4-a716-446655440000"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
2017-06-28 17:20:16 +00:00
|
|
|
|
2020-08-06 03:03:58 +00:00
|
|
|
let r = conn.q_once(&c,
|
2018-01-20 03:21:04 +00:00
|
|
|
r#"[:find ?tx
|
2018-01-30 22:11:41 +00:00
|
|
|
:where [?x :foo/uuid #uuid "cf62d552-6569-4d1b-b667-04703041dfc4" ?tx]]"#, None)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2017-06-28 17:20:16 +00:00
|
|
|
match r {
|
2018-01-30 22:11:41 +00:00
|
|
|
QueryResults::Rel(ref v) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(*v, vec![vec![TypedValue::Ref(t.tx_id),]].into());
|
|
|
|
}
|
2017-06-28 17:20:16 +00:00
|
|
|
_ => panic!("Expected query to work."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tx_as_input() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "s" :db/ident :foo/uuid]
|
|
|
|
[:db/add "s" :db/valueType :db.type/uuid]
|
|
|
|
[:db/add "s" :db/cardinality :db.cardinality/one]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "u" :foo/uuid #uuid "550e8400-e29b-41d4-a716-446655440000"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
|
|
|
let t = conn
|
|
|
|
.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "u" :foo/uuid #uuid "cf62d552-6569-4d1b-b667-04703041dfc4"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-28 17:20:16 +00:00
|
|
|
[:db/add "u" :foo/uuid #uuid "267bab92-ee39-4ca2-b7f0-1163a85af1fb"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("successful transaction");
|
2017-06-28 17:20:16 +00:00
|
|
|
|
|
|
|
let tx = (Variable::from_valid_name("?tx"), TypedValue::Ref(t.tx_id));
|
|
|
|
let inputs = QueryInputs::with_value_sequence(vec![tx]);
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = conn
|
|
|
|
.q_once(
|
2020-08-06 03:03:58 +00:00
|
|
|
&c,
|
2020-01-14 15:46:21 +00:00
|
|
|
r#"[:find ?uuid
|
2017-06-28 17:20:16 +00:00
|
|
|
:in ?tx
|
2020-01-14 15:46:21 +00:00
|
|
|
:where [?x :foo/uuid ?uuid ?tx]]"#,
|
|
|
|
inputs,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2017-06-28 17:20:16 +00:00
|
|
|
match r {
|
2018-01-30 22:11:41 +00:00
|
|
|
QueryResults::Rel(ref v) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
*v,
|
|
|
|
vec![vec![TypedValue::Uuid(
|
|
|
|
Uuid::from_str("cf62d552-6569-4d1b-b667-04703041dfc4").expect("Valid UUID")
|
|
|
|
),]]
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
}
|
2017-06-28 17:20:16 +00:00
|
|
|
_ => panic!("Expected query to work."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 21:19:35 +00:00
|
|
|
#[test]
|
|
|
|
fn test_fulltext() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
2017-06-14 21:44:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-14 21:44:16 +00:00
|
|
|
[:db/add "a" :db/ident :foo/term]
|
|
|
|
[:db/add "a" :db/valueType :db.type/string]
|
|
|
|
[:db/add "a" :db/fulltext false]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/many]
|
|
|
|
|
2017-06-12 21:19:35 +00:00
|
|
|
[:db/add "s" :db/ident :foo/fts]
|
|
|
|
[:db/add "s" :db/valueType :db.type/string]
|
|
|
|
[:db/add "s" :db/fulltext true]
|
2018-02-01 17:06:01 +00:00
|
|
|
[:db/add "s" :db/index true]
|
2017-06-12 21:19:35 +00:00
|
|
|
[:db/add "s" :db/cardinality :db.cardinality/many]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let v = conn
|
|
|
|
.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-12 21:19:35 +00:00
|
|
|
[:db/add "v" :foo/fts "hello darkness my old friend"]
|
|
|
|
[:db/add "v" :foo/fts "I've come to talk with you again"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.tempids
|
|
|
|
.get("v")
|
|
|
|
.cloned()
|
|
|
|
.expect("v was mapped");
|
|
|
|
|
|
|
|
let r = conn
|
|
|
|
.q_once(
|
2020-08-06 03:03:58 +00:00
|
|
|
&c,
|
2020-01-14 15:46:21 +00:00
|
|
|
r#"[:find [?x ?val ?score]
|
|
|
|
:where [(fulltext $ :foo/fts "darkness") [[?x ?val _ ?score]]]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2017-06-12 21:19:35 +00:00
|
|
|
match r {
|
2018-01-30 22:11:41 +00:00
|
|
|
QueryResults::Tuple(Some(vals)) => {
|
2017-06-12 21:19:35 +00:00
|
|
|
let mut vals = vals.into_iter();
|
|
|
|
match (vals.next(), vals.next(), vals.next(), vals.next()) {
|
2020-01-14 15:46:21 +00:00
|
|
|
(
|
|
|
|
Some(Binding::Scalar(TypedValue::Ref(x))),
|
|
|
|
Some(Binding::Scalar(TypedValue::String(text))),
|
|
|
|
Some(Binding::Scalar(TypedValue::Double(score))),
|
|
|
|
None,
|
|
|
|
) => {
|
|
|
|
assert_eq!(x, v);
|
|
|
|
assert_eq!(text.as_str(), "hello darkness my old friend");
|
2021-07-02 22:09:07 +00:00
|
|
|
assert_approx_eq!(score, OrderedFloat(0.0f64));
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
|
|
|
_ => panic!("Unexpected results."),
|
2017-06-12 21:19:35 +00:00
|
|
|
}
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
r => panic!("Unexpected results {:?}.", r),
|
2017-06-12 21:19:35 +00:00
|
|
|
}
|
2017-06-14 21:44:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let a = conn
|
|
|
|
.transact(&mut c, r#"[[:db/add "a" :foo/term "talk"]]"#)
|
|
|
|
.unwrap()
|
|
|
|
.tempids
|
|
|
|
.get("a")
|
|
|
|
.cloned()
|
|
|
|
.expect("a was mapped");
|
2017-06-14 21:44:16 +00:00
|
|
|
|
|
|
|
// If you use a non-constant search term, it must be bound earlier in the query.
|
|
|
|
let query = r#"[:find ?x ?val
|
|
|
|
:where
|
|
|
|
[(fulltext $ :foo/fts ?term) [[?x ?val]]]
|
|
|
|
[?a :foo/term ?term]
|
|
|
|
]"#;
|
2020-08-06 03:03:58 +00:00
|
|
|
let r = conn.q_once(&c, query, None);
|
2018-06-27 20:19:40 +00:00
|
|
|
match r.expect_err("expected query to fail") {
|
2020-01-14 15:46:21 +00:00
|
|
|
MentatError::AlgebrizerError(
|
|
|
|
query_algebrizer_traits::errors::AlgebrizerError::InvalidArgument(
|
|
|
|
PlainSymbol(s),
|
|
|
|
ty,
|
|
|
|
i,
|
|
|
|
),
|
|
|
|
) => {
|
2017-06-14 21:44:16 +00:00
|
|
|
assert_eq!(s, "fulltext");
|
|
|
|
assert_eq!(ty, "string");
|
|
|
|
assert_eq!(i, 2);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2017-06-14 21:44:16 +00:00
|
|
|
_ => panic!("Expected query to fail."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bound to the wrong type? Error.
|
|
|
|
let query = r#"[:find ?x ?val
|
|
|
|
:where
|
|
|
|
[?a :foo/term ?term]
|
|
|
|
[(fulltext $ :foo/fts ?a) [[?x ?val]]]]"#;
|
2020-08-06 03:03:58 +00:00
|
|
|
let r = conn.q_once(&c, query, None);
|
2018-06-27 20:19:40 +00:00
|
|
|
match r.expect_err("expected query to fail") {
|
2020-01-14 15:46:21 +00:00
|
|
|
MentatError::AlgebrizerError(
|
|
|
|
query_algebrizer_traits::errors::AlgebrizerError::InvalidArgument(
|
|
|
|
PlainSymbol(s),
|
|
|
|
ty,
|
|
|
|
i,
|
|
|
|
),
|
|
|
|
) => {
|
2017-06-14 21:44:16 +00:00
|
|
|
assert_eq!(s, "fulltext");
|
|
|
|
assert_eq!(ty, "string");
|
|
|
|
assert_eq!(i, 2);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2017-06-14 21:44:16 +00:00
|
|
|
_ => panic!("Expected query to fail."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's bound, and the right type, it'll work!
|
|
|
|
let query = r#"[:find ?x ?val
|
|
|
|
:in ?a
|
|
|
|
:where
|
|
|
|
[?a :foo/term ?term]
|
|
|
|
[(fulltext $ :foo/fts ?term) [[?x ?val]]]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
let inputs = QueryInputs::with_value_sequence(vec![(
|
|
|
|
Variable::from_valid_name("?a"),
|
|
|
|
TypedValue::Ref(a),
|
|
|
|
)]);
|
2020-08-06 03:03:58 +00:00
|
|
|
let r = conn.q_once(&c, query, inputs).expect("results").into();
|
2017-06-14 21:44:16 +00:00
|
|
|
match r {
|
2018-01-30 22:11:41 +00:00
|
|
|
QueryResults::Rel(rels) => {
|
2018-04-24 22:08:38 +00:00
|
|
|
let values: Vec<Vec<Binding>> = rels.into_iter().collect();
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
values,
|
|
|
|
vec![vec![
|
|
|
|
Binding::Scalar(TypedValue::Ref(v)),
|
|
|
|
"I've come to talk with you again".into(),
|
|
|
|
]]
|
|
|
|
);
|
|
|
|
}
|
2017-06-14 21:44:16 +00:00
|
|
|
_ => panic!("Expected query to work."),
|
|
|
|
}
|
2017-06-12 21:19:35 +00:00
|
|
|
}
|
2017-06-14 23:17:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_instant_range_query() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-14 23:17:25 +00:00
|
|
|
[:db/add "a" :db/ident :foo/date]
|
|
|
|
[:db/add "a" :db/valueType :db.type/instant]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/one]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let ids = conn
|
|
|
|
.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-06-14 23:17:25 +00:00
|
|
|
[:db/add "b" :foo/date #inst "2016-01-01T11:00:00.000Z"]
|
|
|
|
[:db/add "c" :foo/date #inst "2016-06-01T11:00:01.000Z"]
|
|
|
|
[:db/add "d" :foo/date #inst "2017-01-01T11:00:02.000Z"]
|
|
|
|
[:db/add "e" :foo/date #inst "2017-06-01T11:00:03.000Z"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.tempids;
|
|
|
|
|
|
|
|
let r = conn
|
|
|
|
.q_once(
|
2020-08-06 03:03:58 +00:00
|
|
|
&c,
|
2020-01-14 15:46:21 +00:00
|
|
|
r#"[:find [?x ...]
|
2017-06-14 23:17:25 +00:00
|
|
|
:order (asc ?date)
|
|
|
|
:where
|
|
|
|
[?x :foo/date ?date]
|
2020-01-14 15:46:21 +00:00
|
|
|
[(< ?date #inst "2017-01-01T11:00:02.000Z")]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2017-06-14 23:17:25 +00:00
|
|
|
match r {
|
2018-01-30 22:11:41 +00:00
|
|
|
QueryResults::Coll(vals) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec![
|
|
|
|
Binding::Scalar(TypedValue::Ref(*ids.get("b").unwrap())),
|
|
|
|
Binding::Scalar(TypedValue::Ref(*ids.get("c").unwrap()))
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2017-06-14 23:17:25 +00:00
|
|
|
_ => panic!("Expected query to work."),
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lookup() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-12-11 19:08:10 +00:00
|
|
|
[:db/add "a" :db/ident :foo/date]
|
|
|
|
[:db/add "a" :db/valueType :db.type/instant]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/one]
|
|
|
|
[:db/add "b" :db/ident :foo/many]
|
|
|
|
[:db/add "b" :db/valueType :db.type/long]
|
|
|
|
[:db/add "b" :db/cardinality :db.cardinality/many]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let ids = conn
|
|
|
|
.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2017-12-11 19:08:10 +00:00
|
|
|
[:db/add "b" :foo/many 123]
|
|
|
|
[:db/add "b" :foo/many 456]
|
|
|
|
[:db/add "b" :foo/date #inst "2016-01-01T11:00:00.000Z"]
|
|
|
|
[:db/add "c" :foo/date #inst "2016-06-01T11:00:01.000Z"]
|
|
|
|
[:db/add "d" :foo/date #inst "2017-01-01T11:00:02.000Z"]
|
|
|
|
[:db/add "e" :foo/date #inst "2017-06-01T11:00:03.000Z"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.tempids;
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
let entid = ids.get("b").unwrap();
|
2018-02-01 17:17:07 +00:00
|
|
|
let foo_date = kw!(:foo/date);
|
|
|
|
let foo_many = kw!(:foo/many);
|
|
|
|
let db_ident = kw!(:db/ident);
|
2020-01-14 15:46:21 +00:00
|
|
|
let expected =
|
|
|
|
TypedValue::Instant(DateTime::<Utc>::from_str("2016-01-01T11:00:00.000Z").unwrap());
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
// Fetch a value.
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
expected,
|
|
|
|
conn.lookup_value_for_attribute(&c, *entid, &foo_date)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
);
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
// Try to fetch a missing attribute.
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(conn
|
|
|
|
.lookup_value_for_attribute(&c, *entid, &db_ident)
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
// Try to fetch from a non-existent entity.
|
2020-01-14 15:46:21 +00:00
|
|
|
assert!(conn
|
|
|
|
.lookup_value_for_attribute(&c, 12344567, &foo_date)
|
|
|
|
.unwrap()
|
|
|
|
.is_none());
|
2017-12-11 19:08:10 +00:00
|
|
|
|
|
|
|
// Fetch a multi-valued property.
|
|
|
|
let two_longs = vec![TypedValue::Long(123), TypedValue::Long(456)];
|
2020-01-14 15:46:21 +00:00
|
|
|
let fetched_many = conn
|
|
|
|
.lookup_value_for_attribute(&c, *entid, &foo_many)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2017-12-11 19:08:10 +00:00
|
|
|
assert!(two_longs.contains(&fetched_many));
|
|
|
|
}
|
2018-03-15 14:14:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_aggregates_type_handling() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-15 14:14:06 +00:00
|
|
|
{:db/ident :test/boolean :db/valueType :db.type/boolean :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/long :db/valueType :db.type/long :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/double :db/valueType :db.type/double :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/string :db/valueType :db.type/string :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/keyword :db/valueType :db.type/keyword :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/uuid :db/valueType :db.type/uuid :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/instant :db/valueType :db.type/instant :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/ref :db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-15 14:14:06 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-15 14:14:06 +00:00
|
|
|
{:test/boolean false
|
|
|
|
:test/long 10
|
|
|
|
:test/double 2.4
|
|
|
|
:test/string "one"
|
|
|
|
:test/keyword :foo/bar
|
|
|
|
:test/uuid #uuid "55555234-1234-1234-1234-123412341234"
|
|
|
|
:test/instant #inst "2017-01-01T11:00:00.000Z"
|
|
|
|
:test/ref 1}
|
|
|
|
{:test/boolean true
|
|
|
|
:test/long 20
|
|
|
|
:test/double 4.4
|
|
|
|
:test/string "two"
|
|
|
|
:test/keyword :foo/baz
|
|
|
|
:test/uuid #uuid "66666234-1234-1234-1234-123412341234"
|
|
|
|
:test/instant #inst "2018-01-01T11:00:00.000Z"
|
|
|
|
:test/ref 2}
|
|
|
|
{:test/boolean true
|
|
|
|
:test/long 30
|
|
|
|
:test/double 6.4
|
|
|
|
:test/string "three"
|
|
|
|
:test/keyword :foo/noo
|
|
|
|
:test/uuid #uuid "77777234-1234-1234-1234-123412341234"
|
|
|
|
:test/instant #inst "2019-01-01T11:00:00.000Z"
|
|
|
|
:test/ref 3}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-15 14:14:06 +00:00
|
|
|
|
|
|
|
// No type limits => can't do it.
|
|
|
|
let r = store.q_once(r#"[:find (sum ?v) . :where [_ _ ?v]]"#, None);
|
|
|
|
let all_types = ValueTypeSet::any();
|
2018-06-27 20:19:40 +00:00
|
|
|
match r.expect_err("expected query to fail") {
|
2020-01-14 15:46:21 +00:00
|
|
|
MentatError::ProjectorError(
|
|
|
|
::query_projector_traits::errors::ProjectorError::CannotApplyAggregateOperationToTypes(
|
|
|
|
SimpleAggregationOp::Sum,
|
|
|
|
types,
|
|
|
|
),
|
|
|
|
) => {
|
|
|
|
assert_eq!(types, all_types);
|
|
|
|
}
|
2018-06-04 22:07:09 +00:00
|
|
|
e => panic!("Unexpected error type {:?}", e),
|
2018-03-15 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// You can't sum instants.
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store.q_once(
|
|
|
|
r#"[:find (sum ?v) .
|
2018-05-28 21:34:42 +00:00
|
|
|
:where [_ _ ?v] [(type ?v :db.type/instant)]]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
);
|
2018-06-27 20:19:40 +00:00
|
|
|
match r.expect_err("expected query to fail") {
|
2020-01-14 15:46:21 +00:00
|
|
|
MentatError::ProjectorError(
|
|
|
|
::query_projector_traits::errors::ProjectorError::CannotApplyAggregateOperationToTypes(
|
|
|
|
SimpleAggregationOp::Sum,
|
|
|
|
types,
|
|
|
|
),
|
|
|
|
) => {
|
|
|
|
assert_eq!(types, ValueTypeSet::of_one(ValueType::Instant));
|
|
|
|
}
|
2018-06-04 22:07:09 +00:00
|
|
|
e => panic!("Unexpected error type {:?}", e),
|
2018-03-15 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// But you can count them.
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?v) .
|
2018-05-28 21:34:42 +00:00
|
|
|
:where [_ _ ?v] [(type ?v :db.type/instant)]]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("results")
|
|
|
|
.unwrap();
|
2018-03-15 14:14:06 +00:00
|
|
|
|
|
|
|
// Our two transactions, the bootstrap transaction, plus the three values.
|
2018-04-24 22:08:38 +00:00
|
|
|
assert_eq!(Binding::Scalar(TypedValue::Long(6)), r);
|
2018-03-15 14:14:06 +00:00
|
|
|
|
|
|
|
// And you can min them, which returns an instant.
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (min ?v) .
|
2018-05-28 21:34:42 +00:00
|
|
|
:where [_ _ ?v] [(type ?v :db.type/instant)]]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("results")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let earliest = DateTime::parse_from_rfc3339("2017-01-01T11:00:00.000Z")
|
|
|
|
.unwrap()
|
|
|
|
.with_timezone(&Utc);
|
2018-04-24 22:08:38 +00:00
|
|
|
assert_eq!(Binding::Scalar(TypedValue::Instant(earliest)), r);
|
2018-03-15 14:14:06 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (sum ?v) .
|
2018-05-28 21:34:42 +00:00
|
|
|
:where [_ _ ?v] [(type ?v :db.type/long)]]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("results")
|
|
|
|
.unwrap();
|
2018-03-15 14:14:06 +00:00
|
|
|
|
|
|
|
// Yes, the current version is in the store as a Long!
|
|
|
|
let total = 30i64 + 20i64 + 10i64 + ::mentat_db::db::CURRENT_VERSION as i64;
|
2018-04-24 22:08:38 +00:00
|
|
|
assert_eq!(Binding::Scalar(TypedValue::Long(total)), r);
|
2018-03-15 14:14:06 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (avg ?v) .
|
2018-05-28 21:34:42 +00:00
|
|
|
:where [_ _ ?v] [(type ?v :db.type/double)]]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("results")
|
|
|
|
.unwrap();
|
2018-03-15 14:14:06 +00:00
|
|
|
|
|
|
|
let avg = (6.4f64 / 3f64) + (4.4f64 / 3f64) + (2.4f64 / 3f64);
|
2018-04-24 22:08:38 +00:00
|
|
|
assert_eq!(Binding::Scalar(TypedValue::Double(avg.into())), r);
|
2018-03-15 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 22:29:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_type_reqs() {
|
|
|
|
let mut c = new_connection("").expect("Couldn't open conn.");
|
|
|
|
let mut conn = Conn::connect(&mut c).expect("Couldn't open DB.");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2018-01-29 22:29:16 +00:00
|
|
|
{:db/ident :test/boolean :db/valueType :db.type/boolean :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/long :db/valueType :db.type/long :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/double :db/valueType :db.type/double :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/string :db/valueType :db.type/string :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/keyword :db/valueType :db.type/keyword :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/uuid :db/valueType :db.type/uuid :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/instant :db/valueType :db.type/instant :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :test/ref :db/valueType :db.type/ref :db/cardinality :db.cardinality/one}
|
2021-08-23 21:25:10 +00:00
|
|
|
{:db/ident :test/bytes :db/valueType :db.type/bytes :db/cardinality :db.cardinality/one}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-01-29 22:29:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2018-01-29 22:29:16 +00:00
|
|
|
{:test/boolean true
|
|
|
|
:test/long 33
|
|
|
|
:test/double 1.4
|
|
|
|
:test/string "foo"
|
|
|
|
:test/keyword :foo/bar
|
|
|
|
:test/uuid #uuid "12341234-1234-1234-1234-123412341234"
|
|
|
|
:test/instant #inst "2018-01-01T11:00:00.000Z"
|
2021-08-23 21:25:10 +00:00
|
|
|
:test/ref 1
|
|
|
|
:test/bytes #bytes 010203050403022a }
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-01-29 22:29:16 +00:00
|
|
|
|
|
|
|
let eid_query = r#"[:find ?eid :where [?eid :test/string "foo"]]"#;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let res = conn
|
2020-08-07 13:15:36 +00:00
|
|
|
.q_once(&c, eid_query, None)
|
2020-01-14 15:46:21 +00:00
|
|
|
.into_rel_result()
|
|
|
|
.expect("results");
|
2018-04-24 22:04:00 +00:00
|
|
|
|
|
|
|
assert_eq!(res.row_count(), 1);
|
|
|
|
assert_eq!(res.width, 1);
|
2020-01-14 15:46:21 +00:00
|
|
|
let entid = match res.into_iter().next().unwrap().into_iter().next().unwrap() {
|
|
|
|
Binding::Scalar(TypedValue::Ref(eid)) => eid,
|
|
|
|
unexpected => {
|
|
|
|
panic!(
|
|
|
|
"Query to get the entity id returned unexpected result {:?}",
|
|
|
|
unexpected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2018-01-29 22:29:16 +00:00
|
|
|
|
2018-05-28 21:34:42 +00:00
|
|
|
for value_type in ValueType::all_enums().iter() {
|
2020-01-14 15:46:21 +00:00
|
|
|
let q = format!(
|
|
|
|
"[:find [?v ...] :in ?e :where [?e _ ?v] [(type ?v {})]]",
|
|
|
|
value_type.into_keyword()
|
|
|
|
);
|
|
|
|
let results = conn
|
|
|
|
.q_once(
|
2020-08-06 03:03:58 +00:00
|
|
|
&c,
|
2020-01-14 15:46:21 +00:00
|
|
|
&q,
|
|
|
|
QueryInputs::with_value_sequence(vec![(
|
|
|
|
Variable::from_valid_name("?e"),
|
|
|
|
TypedValue::Ref(entid),
|
|
|
|
)]),
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-01-29 22:29:16 +00:00
|
|
|
match results {
|
|
|
|
QueryResults::Coll(vals) => {
|
|
|
|
assert_eq!(vals.len(), 1, "Query should find exactly 1 item");
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-01-29 22:29:16 +00:00
|
|
|
v => {
|
|
|
|
panic!("Query returned unexpected type: {:?}", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(
|
|
|
|
&mut c,
|
|
|
|
r#"[
|
2018-01-29 22:29:16 +00:00
|
|
|
{:db/ident :test/long2 :db/valueType :db.type/long :db/cardinality :db.cardinality/one}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-01-29 22:29:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
conn.transact(&mut c, format!("[[:db/add {} :test/long2 5]]", entid))
|
|
|
|
.unwrap();
|
2018-01-29 22:29:16 +00:00
|
|
|
let longs_query = r#"[:find [?v ...]
|
|
|
|
:order (asc ?v)
|
|
|
|
:in ?e
|
2018-05-28 21:34:42 +00:00
|
|
|
:where [?e _ ?v] [(type ?v :db.type/long)]]"#;
|
2018-01-29 22:29:16 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let res = conn
|
|
|
|
.q_once(
|
2020-08-06 03:03:58 +00:00
|
|
|
&c,
|
2020-01-14 15:46:21 +00:00
|
|
|
longs_query,
|
|
|
|
QueryInputs::with_value_sequence(vec![(
|
|
|
|
Variable::from_valid_name("?e"),
|
|
|
|
TypedValue::Ref(entid),
|
|
|
|
)]),
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-01-29 22:29:16 +00:00
|
|
|
match res {
|
2020-01-14 15:46:21 +00:00
|
|
|
QueryResults::Coll(vals) => assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec![
|
|
|
|
Binding::Scalar(TypedValue::Long(5)),
|
|
|
|
Binding::Scalar(TypedValue::Long(33))
|
|
|
|
]
|
|
|
|
),
|
2018-01-29 22:29:16 +00:00
|
|
|
v => {
|
|
|
|
panic!("Query returned unexpected type: {:?}", v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_monster_head_aggregates() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
let mut in_progress = store.begin_transaction().expect("began");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
in_progress
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
{:db/ident :monster/heads
|
|
|
|
:db/valueType :db.type/long
|
|
|
|
:db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :monster/name
|
|
|
|
:db/valueType :db.type/string
|
|
|
|
:db/cardinality :db.cardinality/one
|
|
|
|
:db/index true
|
|
|
|
:db/unique :db.unique/identity}
|
|
|
|
{:db/ident :monster/weapon
|
|
|
|
:db/valueType :db.type/string
|
|
|
|
:db/cardinality :db.cardinality/many}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("transacted");
|
2018-03-12 22:18:50 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
in_progress
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
{:monster/heads 1
|
|
|
|
:monster/name "Medusa"
|
|
|
|
:monster/weapon "Stony gaze"}
|
|
|
|
{:monster/heads 1
|
|
|
|
:monster/name "Cyclops"
|
|
|
|
:monster/weapon ["Large club" "Mighty arms" "Stompy feet"]}
|
|
|
|
{:monster/heads 1
|
|
|
|
:monster/name "Chimera"
|
|
|
|
:monster/weapon "Goat-like agility"}
|
|
|
|
{:monster/heads 3
|
|
|
|
:monster/name "Cerberus"
|
|
|
|
:monster/weapon ["8-foot Kong®" "Deadly drool"]}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("transacted");
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// Without :with, uniqueness applies prior to aggregation, so we get 1 + 3 = 4.
|
2020-01-14 15:46:21 +00:00
|
|
|
let res = in_progress
|
|
|
|
.q_once(
|
|
|
|
"[:find (sum ?heads) . :where [?monster :monster/heads ?heads]]",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match res {
|
2018-04-24 22:08:38 +00:00
|
|
|
QueryResults::Scalar(Some(Binding::Scalar(TypedValue::Long(count)))) => {
|
2018-03-12 22:18:50 +00:00
|
|
|
assert_eq!(count, 4);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
r => panic!("Unexpected result {:?}", r),
|
|
|
|
};
|
|
|
|
|
|
|
|
// With :with, uniqueness includes the monster, so we get 1 + 1 + 1 + 3 = 6.
|
2020-01-14 15:46:21 +00:00
|
|
|
let res = in_progress
|
|
|
|
.q_once(
|
|
|
|
"[:find (sum ?heads) . :with ?monster :where [?monster :monster/heads ?heads]]",
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match res {
|
2018-04-24 22:08:38 +00:00
|
|
|
QueryResults::Scalar(Some(Binding::Scalar(TypedValue::Long(count)))) => {
|
2018-03-12 22:18:50 +00:00
|
|
|
assert_eq!(count, 6);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
r => panic!("Unexpected result {:?}", r),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Aggregates group.
|
2020-01-14 15:46:21 +00:00
|
|
|
let res = in_progress
|
|
|
|
.q_once(
|
|
|
|
r#"[:find ?name (count ?weapon)
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?monster
|
|
|
|
:order (asc ?name)
|
|
|
|
:where [?monster :monster/name ?name]
|
|
|
|
[?monster :monster/weapon ?weapon]]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match res {
|
|
|
|
QueryResults::Rel(vals) => {
|
|
|
|
let expected = vec![
|
|
|
|
vec!["Cerberus".into(), TypedValue::Long(2)],
|
2020-01-14 15:46:21 +00:00
|
|
|
vec!["Chimera".into(), TypedValue::Long(1)],
|
|
|
|
vec!["Cyclops".into(), TypedValue::Long(3)],
|
|
|
|
vec!["Medusa".into(), TypedValue::Long(1)],
|
2018-03-12 22:18:50 +00:00
|
|
|
];
|
2018-04-24 22:04:00 +00:00
|
|
|
assert_eq!(vals, expected.into());
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
r => panic!("Unexpected result {:?}", r),
|
|
|
|
};
|
|
|
|
|
|
|
|
in_progress.rollback().expect("rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic_aggregates() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
|
|
|
|
store.transact(r#"[
|
|
|
|
{:db/ident :foo/is-vegetarian :db/valueType :db.type/boolean :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :foo/age :db/valueType :db.type/long :db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :foo/name :db/valueType :db.type/string :db/cardinality :db.cardinality/one}
|
|
|
|
]"#).unwrap();
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let _ids = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
[:db/add "a" :foo/name "Alice"]
|
|
|
|
[:db/add "b" :foo/name "Beli"]
|
|
|
|
[:db/add "c" :foo/name "Carlos"]
|
|
|
|
[:db/add "d" :foo/name "Diana"]
|
|
|
|
[:db/add "a" :foo/is-vegetarian true]
|
|
|
|
[:db/add "b" :foo/is-vegetarian true]
|
|
|
|
[:db/add "c" :foo/is-vegetarian false]
|
|
|
|
[:db/add "d" :foo/is-vegetarian false]
|
|
|
|
[:db/add "a" :foo/age 14]
|
|
|
|
[:db/add "b" :foo/age 22]
|
|
|
|
[:db/add "c" :foo/age 42]
|
|
|
|
[:db/add "d" :foo/age 28]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.tempids;
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// Count the number of distinct bindings of `?veg` that are `true` -- namely, one.
|
|
|
|
// This is not the same as `count-distinct`: note the distinction between
|
|
|
|
// including `:with` and not.
|
|
|
|
// In this case, the `DISTINCT` must occur inside the aggregation, not outside it.
|
|
|
|
/*
|
|
|
|
Rather than:
|
|
|
|
|
|
|
|
SELECT DISTINCT count(1) AS `(count ?veg)`
|
|
|
|
FROM `datoms` AS `datoms00`
|
|
|
|
WHERE `datoms00`.a = 65536
|
|
|
|
AND `datoms00`.v = 1;
|
|
|
|
|
|
|
|
our query should be
|
|
|
|
|
|
|
|
SELECT DISTINCT count(`?veg`) AS `(count ?veg)`
|
|
|
|
FROM (
|
|
|
|
SELECT DISTINCT 1 AS `?veg`
|
|
|
|
FROM `datoms` AS `datoms00`
|
|
|
|
WHERE `datoms00`.a = 65536
|
|
|
|
AND `datoms00`.v = 1
|
|
|
|
);
|
|
|
|
*/
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?veg)
|
2018-03-12 22:18:50 +00:00
|
|
|
:where
|
|
|
|
[_ :foo/is-vegetarian ?veg]
|
2020-01-14 15:46:21 +00:00
|
|
|
[(ground true) ?veg]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match r {
|
|
|
|
QueryResults::Rel(vals) => {
|
2018-04-24 22:04:00 +00:00
|
|
|
assert_eq!(vals, vec![vec![TypedValue::Long(1)]].into());
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
_ => panic!("Expected rel."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// And this should be
|
|
|
|
/*
|
|
|
|
SELECT DISTINCT count(`?veg`) AS `(count ?veg)`
|
|
|
|
FROM (
|
|
|
|
SELECT DISTINCT 1 AS `?veg`, `datoms00`.e AS `?person`
|
|
|
|
FROM `datoms` AS `datoms00`
|
|
|
|
WHERE `datoms00`.a = 65536
|
|
|
|
AND `datoms00`.v = 1
|
|
|
|
);
|
|
|
|
*/
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?veg) .
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?person
|
|
|
|
:where
|
|
|
|
[?person :foo/is-vegetarian ?veg]
|
2020-01-14 15:46:21 +00:00
|
|
|
[(ground true) ?veg]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match r {
|
|
|
|
QueryResults::Scalar(Some(val)) => {
|
2018-04-24 22:08:38 +00:00
|
|
|
assert_eq!(val, Binding::Scalar(TypedValue::Long(2)));
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
_ => panic!("Expected scalar."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// What are the oldest and youngest ages?
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find [(min ?age) (max ?age)]
|
2018-03-12 22:18:50 +00:00
|
|
|
:where
|
2020-01-14 15:46:21 +00:00
|
|
|
[_ :foo/age ?age]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match r {
|
|
|
|
QueryResults::Tuple(Some(vals)) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec![
|
|
|
|
Binding::Scalar(TypedValue::Long(14)),
|
|
|
|
Binding::Scalar(TypedValue::Long(42))
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
_ => panic!("Expected tuple."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Who's youngest, via order?
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find [?name ?age]
|
2018-03-12 22:18:50 +00:00
|
|
|
:order (asc ?age)
|
|
|
|
:where
|
|
|
|
[?x :foo/age ?age]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?x :foo/name ?name]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match r {
|
|
|
|
QueryResults::Tuple(Some(vals)) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec!["Alice".into(), Binding::Scalar(TypedValue::Long(14))]
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
r => panic!("Unexpected results {:?}", r),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Who's oldest, via order?
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find [?name ?age]
|
2018-03-12 22:18:50 +00:00
|
|
|
:order (desc ?age)
|
|
|
|
:where
|
|
|
|
[?x :foo/age ?age]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?x :foo/name ?name]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match r {
|
|
|
|
QueryResults::Tuple(Some(vals)) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec!["Carlos".into(), Binding::Scalar(TypedValue::Long(42))]
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
_ => panic!("Expected tuple."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// How many of each age do we have?
|
|
|
|
// Add an extra person to make this interesting.
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(r#"[{:foo/name "Medusa", :foo/age 28}]"#)
|
|
|
|
.expect("transacted");
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// If we omit the 'with', we'll get the wrong answer:
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find ?age (count ?age)
|
2018-03-12 22:18:50 +00:00
|
|
|
:order (asc ?age)
|
2020-01-14 15:46:21 +00:00
|
|
|
:where [_ :foo/age ?age]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
match r {
|
|
|
|
QueryResults::Rel(vals) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec![
|
|
|
|
vec![TypedValue::Long(14), TypedValue::Long(1)],
|
|
|
|
vec![TypedValue::Long(22), TypedValue::Long(1)],
|
|
|
|
vec![TypedValue::Long(28), TypedValue::Long(1)],
|
|
|
|
vec![TypedValue::Long(42), TypedValue::Long(1)],
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
_ => panic!("Expected rel."),
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we include it, we'll get the right one:
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find ?age (count ?age)
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?person
|
|
|
|
:order (asc ?age)
|
2020-01-14 15:46:21 +00:00
|
|
|
:where [?person :foo/age ?age]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
match r {
|
|
|
|
QueryResults::Rel(vals) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec![
|
|
|
|
vec![TypedValue::Long(14), TypedValue::Long(1)],
|
|
|
|
vec![TypedValue::Long(22), TypedValue::Long(1)],
|
|
|
|
vec![TypedValue::Long(28), TypedValue::Long(2)],
|
|
|
|
vec![TypedValue::Long(42), TypedValue::Long(1)],
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
_ => panic!("Expected rel."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_combinatorial() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
[:db/add "a" :db/ident :foo/name]
|
|
|
|
[:db/add "a" :db/valueType :db.type/string]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/one]
|
|
|
|
[:db/add "b" :db/ident :foo/dance]
|
|
|
|
[:db/add "b" :db/valueType :db.type/ref]
|
|
|
|
[:db/add "b" :db/cardinality :db.cardinality/many]
|
|
|
|
[:db/add "b" :db/index true]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-12 22:18:50 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
[:db/add "a" :foo/name "Alice"]
|
|
|
|
[:db/add "b" :foo/name "Beli"]
|
|
|
|
[:db/add "c" :foo/name "Carlos"]
|
|
|
|
[:db/add "d" :foo/name "Diana"]
|
|
|
|
|
|
|
|
;; Alice danced with Beli twice.
|
|
|
|
[:db/add "a" :foo/dance "ab"]
|
|
|
|
[:db/add "b" :foo/dance "ab"]
|
|
|
|
[:db/add "a" :foo/dance "ba"]
|
|
|
|
[:db/add "b" :foo/dance "ba"]
|
|
|
|
|
|
|
|
;; Carlos danced with Diana.
|
|
|
|
[:db/add "c" :foo/dance "cd"]
|
|
|
|
[:db/add "d" :foo/dance "cd"]
|
|
|
|
|
|
|
|
;; Alice danced with Diana.
|
|
|
|
[:db/add "a" :foo/dance "ad"]
|
|
|
|
[:db/add "d" :foo/dance "ad"]
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// How many different pairings of dancers were there?
|
|
|
|
// If we just use `!=` (or `differ`), the number is doubled because of symmetry!
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
Binding::Scalar(TypedValue::Long(6)),
|
|
|
|
store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?right) .
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?left
|
|
|
|
:where
|
|
|
|
[?left :foo/dance ?dance]
|
|
|
|
[?right :foo/dance ?dance]
|
2020-01-14 15:46:21 +00:00
|
|
|
[(differ ?left ?right)]]"#,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("scalar results")
|
|
|
|
.unwrap()
|
|
|
|
);
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// SQL addresses this by using `<` instead of `!=` -- by imposing
|
|
|
|
// an order on values, we can ensure that each pair only appears once, not
|
|
|
|
// once per permutation.
|
|
|
|
// It's far from ideal to expose an ordering on entids, because developers
|
|
|
|
// will come to rely on it. Instead we expose a specific operator: `unpermute`.
|
|
|
|
// When used in a query that generates permuted pairs of references, this
|
|
|
|
// ensures that only one permutation is returned for a given pair.
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
Binding::Scalar(TypedValue::Long(3)),
|
|
|
|
store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?right) .
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?left
|
|
|
|
:where
|
|
|
|
[?left :foo/dance ?dance]
|
|
|
|
[?right :foo/dance ?dance]
|
2020-01-14 15:46:21 +00:00
|
|
|
[(unpermute ?left ?right)]]"#,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("scalar results")
|
|
|
|
.unwrap()
|
|
|
|
);
|
2018-03-12 22:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:58:58 +00:00
|
|
|
#[test]
|
|
|
|
fn test_aggregate_the() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-10 18:58:58 +00:00
|
|
|
{:db/ident :visit/visitedOnDevice
|
|
|
|
:db/valueType :db.type/ref
|
|
|
|
:db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :visit/visitAt
|
|
|
|
:db/valueType :db.type/instant
|
|
|
|
:db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :site/visit
|
|
|
|
:db/valueType :db.type/ref
|
|
|
|
:db/isComponent true
|
|
|
|
:db/cardinality :db.cardinality/many}
|
|
|
|
{:db/ident :site/url
|
|
|
|
:db/valueType :db.type/string
|
|
|
|
:db/unique :db.unique/identity
|
|
|
|
:db/cardinality :db.cardinality/one
|
|
|
|
:db/index true}
|
|
|
|
{:db/ident :visit/page
|
|
|
|
:db/valueType :db.type/ref
|
|
|
|
:db/isComponent true ; Debatable.
|
|
|
|
:db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :page/title
|
|
|
|
:db/valueType :db.type/string
|
|
|
|
:db/fulltext true
|
|
|
|
:db/index true
|
|
|
|
:db/cardinality :db.cardinality/one}
|
|
|
|
{:db/ident :visit/container
|
|
|
|
:db/valueType :db.type/ref
|
|
|
|
:db/cardinality :db.cardinality/one}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("transacted schema");
|
2018-04-10 18:58:58 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-10 18:58:58 +00:00
|
|
|
{:db/ident :container/facebook}
|
|
|
|
{:db/ident :container/personal}
|
|
|
|
|
|
|
|
{:db/ident :device/my-desktop}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("transacted idents");
|
2018-04-10 18:58:58 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-10 18:58:58 +00:00
|
|
|
{:visit/visitedOnDevice :device/my-desktop
|
|
|
|
:visit/visitAt #inst "2018-04-06T20:46:00Z"
|
|
|
|
:visit/container :container/facebook
|
|
|
|
:db/id "another"
|
|
|
|
:visit/page "fbpage2"}
|
|
|
|
{:db/id "fbpage2"
|
|
|
|
:page/title "(1) Facebook"}
|
|
|
|
{:visit/visitedOnDevice :device/my-desktop
|
|
|
|
:visit/visitAt #inst "2018-04-06T18:46:00Z"
|
|
|
|
:visit/container :container/facebook
|
|
|
|
:db/id "fbvisit"
|
|
|
|
:visit/page "fbpage"}
|
|
|
|
{:db/id "fbpage"
|
|
|
|
:page/title "(2) Facebook"}
|
|
|
|
{:site/url "https://www.facebook.com"
|
|
|
|
:db/id "aa"
|
|
|
|
:site/visit ["personalvisit" "another" "fbvisit"]}
|
|
|
|
{:visit/visitedOnDevice :device/my-desktop
|
|
|
|
:visit/visitAt #inst "2018-04-06T18:46:00Z"
|
|
|
|
:visit/container :container/personal
|
|
|
|
:db/id "personalvisit"
|
|
|
|
:visit/page "personalpage"}
|
|
|
|
{:db/id "personalpage"
|
|
|
|
:page/title "Facebook - Log In or Sign Up"}
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("transacted data");
|
2018-04-10 18:58:58 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let per_title = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-04-10 18:58:58 +00:00
|
|
|
[:find (max ?visitDate) ?title
|
|
|
|
:where [?site :site/url "https://www.facebook.com"]
|
|
|
|
[?site :site/visit ?visit]
|
|
|
|
[?visit :visit/container :container/facebook]
|
|
|
|
[?visit :visit/visitAt ?visitDate]
|
|
|
|
[?visit :visit/page ?page]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?page :page/title ?title]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_rel_result()
|
|
|
|
.expect("two results");
|
|
|
|
|
|
|
|
let corresponding_title = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-04-10 18:58:58 +00:00
|
|
|
[:find (max ?visitDate) (the ?title)
|
|
|
|
:where [?site :site/url "https://www.facebook.com"]
|
|
|
|
[?site :site/visit ?visit]
|
|
|
|
[?visit :visit/container :container/facebook]
|
|
|
|
[?visit :visit/visitAt ?visitDate]
|
|
|
|
[?visit :visit/page ?page]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?page :page/title ?title]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_rel_result()
|
|
|
|
.expect("one result");
|
2018-04-10 18:58:58 +00:00
|
|
|
|
|
|
|
// This test shows the distinction between `?title` and `(the ?title`) — the former returns two
|
|
|
|
// results, while the latter returns one. Without `the` we group by `?title`, getting the
|
|
|
|
// maximum visit date for each title; with it we don't group by value, instead getting the title
|
|
|
|
// that corresponds to the maximum visit date.
|
|
|
|
//
|
|
|
|
// 'Group' in this context translates to GROUP BY in the generated SQL.
|
2018-04-24 22:04:00 +00:00
|
|
|
assert_eq!(2, per_title.row_count());
|
|
|
|
assert_eq!(1, corresponding_title.row_count());
|
2018-04-10 18:58:58 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
corresponding_title,
|
|
|
|
vec![vec![
|
|
|
|
TypedValue::Instant(DateTime::<Utc>::from_str("2018-04-06T20:46:00.000Z").unwrap()),
|
|
|
|
TypedValue::typed_string("(1) Facebook")
|
|
|
|
]]
|
|
|
|
.into()
|
|
|
|
);
|
2018-04-10 18:58:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 21:17:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_null_aggregates() {
|
|
|
|
let store = Store::open("").expect("opened");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let rel = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-06-01 21:17:31 +00:00
|
|
|
[:find (count ?tx) (max ?txInstant)
|
|
|
|
:where [_ _ _ ?tx]
|
|
|
|
[?tx :db/txInstant ?txInstant]
|
|
|
|
[(< ?txInstant #inst "2016-01-01T11:00:00.000Z")]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_rel_result()
|
|
|
|
.expect("no results");
|
2018-06-01 21:17:31 +00:00
|
|
|
|
|
|
|
// (count ?tx) is 0, but (max ?txInstant) is over 0 SQL rows, yielding a NULL in the SQL rows.
|
|
|
|
// We reject the entire row containing NULL aggregates.
|
|
|
|
assert_eq!(0, rel.row_count());
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let rel_pull = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-06-01 21:17:31 +00:00
|
|
|
[:find (count ?tx) (max ?txInstant) (pull ?tx [*])
|
|
|
|
:where [_ _ _ ?tx]
|
|
|
|
[?tx :db/txInstant ?txInstant]
|
|
|
|
[(< ?txInstant #inst "2016-01-01T11:00:00.000Z")]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_rel_result()
|
|
|
|
.expect("no results");
|
2018-06-01 21:17:31 +00:00
|
|
|
|
|
|
|
// Same logic as above -- just verifying that `RelTwoStagePullProjector` handles NULL.
|
|
|
|
assert_eq!(0, rel_pull.row_count());
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let coll = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-06-01 21:17:31 +00:00
|
|
|
[:find [(max ?txInstant) ...]
|
|
|
|
:where [_ _ _ ?tx]
|
|
|
|
[?tx :db/txInstant ?txInstant]
|
|
|
|
[(< ?txInstant #inst "2016-01-01T11:00:00.000Z")]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_coll_result()
|
|
|
|
.expect("no results");
|
2018-06-01 21:17:31 +00:00
|
|
|
|
|
|
|
// (max ?txInstant) is over 0 SQL rows, yielding a NULL in the SQL rows. We reject the entire
|
|
|
|
// row containing NULL aggregates, yielding an empty vector of results.
|
|
|
|
assert_eq!(coll, vec![]);
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let tuple = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-06-01 21:17:31 +00:00
|
|
|
[:find [(count ?tx) (max ?txInstant)]
|
|
|
|
:where [_ _ _ ?tx]
|
|
|
|
[?tx :db/txInstant ?txInstant]
|
|
|
|
[(< ?txInstant #inst "2016-01-01T11:00:00.000Z")]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_tuple_result()
|
|
|
|
.expect("no results");
|
2018-06-01 21:17:31 +00:00
|
|
|
|
|
|
|
// (count ?tx) is 0, but (max ?txInstant) is over 0 SQL rows, yielding a NULL in the SQL rows.
|
|
|
|
// We reject the entire row containing NULL aggregates, yielding no tuple result at all.
|
|
|
|
assert_eq!(tuple, None);
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let tuple_pull = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-06-01 21:17:31 +00:00
|
|
|
[:find [(count ?tx) (max ?txInstant) (pull ?tx [*])]
|
|
|
|
:where [_ _ _ ?tx]
|
|
|
|
[?tx :db/txInstant ?txInstant]
|
|
|
|
[(< ?txInstant #inst "2016-01-01T11:00:00.000Z")]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_tuple_result()
|
|
|
|
.expect("no results");
|
2018-06-01 21:17:31 +00:00
|
|
|
|
|
|
|
// Same logic as above -- just verifying that `CollTwoStagePullProjector` handles NULL.
|
|
|
|
assert_eq!(tuple_pull, None);
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let scalar = store
|
|
|
|
.q_once(
|
|
|
|
r#"
|
2018-06-01 21:17:31 +00:00
|
|
|
[:find (max ?txInstant) .
|
|
|
|
:where [_ _ _ ?tx]
|
|
|
|
[?tx :db/txInstant ?txInstant]
|
|
|
|
[(< ?txInstant #inst "2016-01-01T11:00:00.000Z")]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("no results");
|
2018-06-01 21:17:31 +00:00
|
|
|
|
|
|
|
// (max ?txInstant) is over 0 SQL rows, yielding a NULL in the SQL rows. We reject the entire
|
|
|
|
// row containing NULL aggregates, yielding no scalar result at all.
|
|
|
|
assert_eq!(scalar, None);
|
|
|
|
}
|
|
|
|
|
2018-03-12 22:18:50 +00:00
|
|
|
#[test]
|
|
|
|
fn test_aggregation_implicit_grouping() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
[:db/add "a" :db/ident :foo/score]
|
|
|
|
[:db/add "a" :db/valueType :db.type/long]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/one]
|
|
|
|
[:db/add "b" :db/ident :foo/name]
|
|
|
|
[:db/add "b" :db/valueType :db.type/string]
|
|
|
|
[:db/add "b" :db/cardinality :db.cardinality/one]
|
|
|
|
[:db/add "c" :db/ident :foo/is-vegetarian]
|
|
|
|
[:db/add "c" :db/valueType :db.type/boolean]
|
|
|
|
[:db/add "c" :db/cardinality :db.cardinality/one]
|
|
|
|
[:db/add "d" :db/ident :foo/play]
|
|
|
|
[:db/add "d" :db/valueType :db.type/ref]
|
|
|
|
[:db/add "d" :db/cardinality :db.cardinality/many]
|
|
|
|
[:db/add "d" :db/index true]
|
|
|
|
[:db/add "d" :db/unique :db.unique/value]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-12 22:18:50 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let ids = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-03-12 22:18:50 +00:00
|
|
|
[:db/add "a" :foo/name "Alice"]
|
|
|
|
[:db/add "b" :foo/name "Beli"]
|
|
|
|
[:db/add "c" :foo/name "Carlos"]
|
|
|
|
[:db/add "d" :foo/name "Diana"]
|
|
|
|
[:db/add "a" :foo/is-vegetarian true]
|
|
|
|
[:db/add "b" :foo/is-vegetarian true]
|
|
|
|
[:db/add "c" :foo/is-vegetarian false]
|
|
|
|
[:db/add "d" :foo/is-vegetarian false]
|
|
|
|
[:db/add "aa" :foo/score 14]
|
|
|
|
[:db/add "ab" :foo/score 99]
|
|
|
|
[:db/add "ac" :foo/score 14]
|
|
|
|
[:db/add "ba" :foo/score 22]
|
|
|
|
[:db/add "bb" :foo/score 11]
|
|
|
|
[:db/add "ca" :foo/score 42]
|
|
|
|
[:db/add "da" :foo/score 5]
|
|
|
|
[:db/add "db" :foo/score 28]
|
|
|
|
[:db/add "d" :foo/play "da"]
|
|
|
|
[:db/add "d" :foo/play "db"]
|
|
|
|
[:db/add "a" :foo/play "aa"]
|
|
|
|
[:db/add "a" :foo/play "ab"]
|
|
|
|
[:db/add "a" :foo/play "ac"]
|
|
|
|
[:db/add "b" :foo/play "ba"]
|
|
|
|
[:db/add "b" :foo/play "bb"]
|
|
|
|
[:db/add "c" :foo/play "ca"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.tempids;
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// How many different scores were there?
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
Binding::Scalar(TypedValue::Long(7)),
|
|
|
|
store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?score) .
|
2018-03-12 22:18:50 +00:00
|
|
|
:where
|
2020-01-14 15:46:21 +00:00
|
|
|
[?game :foo/score ?score]]"#,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("scalar results")
|
|
|
|
.unwrap()
|
|
|
|
);
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// How many different games resulted in scores?
|
|
|
|
// '14' appears twice.
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
Binding::Scalar(TypedValue::Long(8)),
|
|
|
|
store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find (count ?score) .
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?game
|
|
|
|
:where
|
2020-01-14 15:46:21 +00:00
|
|
|
[?game :foo/score ?score]]"#,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.into_scalar_result()
|
|
|
|
.expect("scalar results")
|
|
|
|
.unwrap()
|
|
|
|
);
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// Who's the highest-scoring vegetarian?
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vec!["Alice".into(), Binding::Scalar(TypedValue::Long(99))],
|
|
|
|
store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find [(the ?name) (max ?score)]
|
2018-03-12 22:18:50 +00:00
|
|
|
:where
|
|
|
|
[?game :foo/score ?score]
|
|
|
|
[?person :foo/play ?game]
|
|
|
|
[?person :foo/is-vegetarian true]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?person :foo/name ?name]]"#,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.into_tuple_result()
|
|
|
|
.expect("tuple results")
|
|
|
|
.unwrap()
|
|
|
|
);
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// We can't run an ambiguous correspondence.
|
2020-01-14 15:46:21 +00:00
|
|
|
let res = store.q_once(
|
|
|
|
r#"[:find [(the ?name) (min ?score) (max ?score)]
|
2018-04-24 22:08:38 +00:00
|
|
|
:where
|
|
|
|
[?game :foo/score ?score]
|
|
|
|
[?person :foo/play ?game]
|
|
|
|
[?person :foo/is-vegetarian true]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?person :foo/name ?name]]"#,
|
|
|
|
None,
|
|
|
|
);
|
2018-06-27 20:19:40 +00:00
|
|
|
match res.expect_err("expected query to fail") {
|
2020-01-14 15:46:21 +00:00
|
|
|
MentatError::ProjectorError(
|
|
|
|
::query_projector_traits::errors::ProjectorError::AmbiguousAggregates(mmc, cc),
|
|
|
|
) => {
|
2018-03-12 22:18:50 +00:00
|
|
|
assert_eq!(mmc, 2);
|
|
|
|
assert_eq!(cc, 1);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-06-04 22:07:09 +00:00
|
|
|
e => {
|
|
|
|
panic!("Unexpected error type {:?}.", e);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Max scores for vegetarians.
|
2020-01-14 15:46:21 +00:00
|
|
|
let expected: RelResult<Binding> = vec![
|
|
|
|
vec!["Alice".into(), TypedValue::Long(99)],
|
|
|
|
vec!["Beli".into(), TypedValue::Long(22)],
|
|
|
|
]
|
|
|
|
.into();
|
|
|
|
assert_eq!(
|
|
|
|
expected,
|
|
|
|
store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find ?name (max ?score)
|
2018-03-12 22:18:50 +00:00
|
|
|
:where
|
|
|
|
[?game :foo/score ?score]
|
|
|
|
[?person :foo/play ?game]
|
|
|
|
[?person :foo/is-vegetarian true]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?person :foo/name ?name]]"#,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
.into_rel_result()
|
|
|
|
.expect("rel results")
|
|
|
|
);
|
2018-03-12 22:18:50 +00:00
|
|
|
|
|
|
|
// We can combine these aggregates.
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find ?x ?name (max ?score) (count ?score) (avg ?score)
|
2018-03-12 22:18:50 +00:00
|
|
|
:with ?game ; So we don't discard duplicate scores!
|
|
|
|
:where
|
|
|
|
[?x :foo/name ?name]
|
|
|
|
[?x :foo/play ?game]
|
2020-01-14 15:46:21 +00:00
|
|
|
[?game :foo/score ?score]]"#,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.expect("results")
|
|
|
|
.into();
|
2018-03-12 22:18:50 +00:00
|
|
|
match r {
|
|
|
|
QueryResults::Rel(vals) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
2018-03-12 22:18:50 +00:00
|
|
|
vec![
|
2020-01-14 15:46:21 +00:00
|
|
|
vec![
|
|
|
|
TypedValue::Ref(ids.get("a").cloned().unwrap()),
|
|
|
|
"Alice".into(),
|
|
|
|
TypedValue::Long(99),
|
|
|
|
TypedValue::Long(3),
|
|
|
|
TypedValue::Double((127f64 / 3f64).into())
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
TypedValue::Ref(ids.get("b").cloned().unwrap()),
|
|
|
|
"Beli".into(),
|
|
|
|
TypedValue::Long(22),
|
|
|
|
TypedValue::Long(2),
|
|
|
|
TypedValue::Double((33f64 / 2f64).into())
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
TypedValue::Ref(ids.get("c").cloned().unwrap()),
|
|
|
|
"Carlos".into(),
|
|
|
|
TypedValue::Long(42),
|
|
|
|
TypedValue::Long(1),
|
|
|
|
TypedValue::Double(42f64.into())
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
TypedValue::Ref(ids.get("d").cloned().unwrap()),
|
|
|
|
"Diana".into(),
|
|
|
|
TypedValue::Long(28),
|
|
|
|
TypedValue::Long(2),
|
|
|
|
TypedValue::Double((33f64 / 2f64).into())
|
|
|
|
]
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
}
|
2018-03-12 22:18:50 +00:00
|
|
|
x => panic!("Got unexpected results {:?}", x),
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 21:08:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tx_ids() {
|
|
|
|
let mut store = Store::open("").expect("opened");
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "a" :db/ident :foo/term]
|
|
|
|
[:db/add "a" :db/valueType :db.type/string]
|
|
|
|
[:db/add "a" :db/fulltext false]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/many]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-04-16 21:08:00 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let tx1 = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "v" :foo/term "1"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("tx1 to apply")
|
|
|
|
.tx_id;
|
|
|
|
|
|
|
|
let tx2 = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "v" :foo/term "2"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("tx2 to apply")
|
|
|
|
.tx_id;
|
|
|
|
|
|
|
|
let tx3 = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "v" :foo/term "3"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("tx3 to apply")
|
|
|
|
.tx_id;
|
2018-04-16 21:08:00 +00:00
|
|
|
|
|
|
|
fn assert_tx_id_range(store: &Store, after: Entid, before: Entid, expected: Vec<TypedValue>) {
|
|
|
|
// TODO: after https://github.com/mozilla/mentat/issues/641, use q_prepare with inputs bound
|
|
|
|
// at execution time.
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find [?tx ...]
|
2018-04-16 21:08:00 +00:00
|
|
|
:in ?after ?before
|
|
|
|
:where
|
|
|
|
[(tx-ids $ ?after ?before) [?tx ...]]
|
|
|
|
]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
QueryInputs::with_value_sequence(vec![
|
|
|
|
(Variable::from_valid_name("?after"), TypedValue::Ref(after)),
|
|
|
|
(
|
|
|
|
Variable::from_valid_name("?before"),
|
|
|
|
TypedValue::Ref(before),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
)
|
2018-04-16 21:08:00 +00:00
|
|
|
.expect("results")
|
|
|
|
.into();
|
|
|
|
match r {
|
|
|
|
QueryResults::Coll(txs) => {
|
2018-04-24 22:08:38 +00:00
|
|
|
let expected: Vec<Binding> = expected.into_iter().map(|tv| tv.into()).collect();
|
2018-04-16 21:08:00 +00:00
|
|
|
assert_eq!(txs, expected);
|
2020-01-14 15:46:21 +00:00
|
|
|
}
|
2018-04-16 21:08:00 +00:00
|
|
|
x => panic!("Got unexpected results {:?}", x),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tx_id_range(&store, tx1, tx2, vec![TypedValue::Ref(tx1)]);
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_tx_id_range(
|
|
|
|
&store,
|
|
|
|
tx1,
|
|
|
|
tx3,
|
|
|
|
vec![TypedValue::Ref(tx1), TypedValue::Ref(tx2)],
|
|
|
|
);
|
2018-04-16 21:08:00 +00:00
|
|
|
assert_tx_id_range(&store, tx2, tx3, vec![TypedValue::Ref(tx2)]);
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_tx_id_range(
|
|
|
|
&store,
|
|
|
|
tx2,
|
|
|
|
tx3 + 1,
|
|
|
|
vec![TypedValue::Ref(tx2), TypedValue::Ref(tx3)],
|
|
|
|
);
|
2018-04-16 21:08:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 15:49:40 +00:00
|
|
|
fn run_tx_data_test(mut store: Store) {
|
2020-01-14 15:46:21 +00:00
|
|
|
store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "a" :db/ident :foo/term]
|
|
|
|
[:db/add "a" :db/valueType :db.type/string]
|
|
|
|
[:db/add "a" :db/fulltext false]
|
|
|
|
[:db/add "a" :db/cardinality :db.cardinality/many]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-04-16 21:08:00 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let tx1 = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "e" :foo/term "1"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("tx1 to apply");
|
2018-04-16 21:08:00 +00:00
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
let tx2 = store
|
|
|
|
.transact(
|
|
|
|
r#"[
|
2018-04-16 21:08:00 +00:00
|
|
|
[:db/add "e" :foo/term "2"]
|
2020-01-14 15:46:21 +00:00
|
|
|
]"#,
|
|
|
|
)
|
|
|
|
.expect("tx2 to apply");
|
2018-04-16 21:08:00 +00:00
|
|
|
|
|
|
|
fn assert_tx_data(store: &Store, tx: &TxReport, value: TypedValue) {
|
|
|
|
// TODO: after https://github.com/mozilla/mentat/issues/641, use q_prepare with inputs bound
|
|
|
|
// at execution time.
|
2020-01-14 15:46:21 +00:00
|
|
|
let r = store
|
|
|
|
.q_once(
|
|
|
|
r#"[:find ?e ?a-name ?v ?tx ?added
|
2018-04-16 21:08:00 +00:00
|
|
|
:in ?tx-in
|
|
|
|
:where
|
|
|
|
[(tx-data $ ?tx-in) [[?e ?a ?v ?tx ?added]]]
|
|
|
|
[?a :db/ident ?a-name]
|
|
|
|
:order ?e
|
|
|
|
]"#,
|
2020-01-14 15:46:21 +00:00
|
|
|
QueryInputs::with_value_sequence(vec![(
|
|
|
|
Variable::from_valid_name("?tx-in"),
|
|
|
|
TypedValue::Ref(tx.tx_id),
|
|
|
|
)]),
|
|
|
|
)
|
2018-04-16 21:08:00 +00:00
|
|
|
.expect("results")
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let e = tx.tempids.get("e").cloned().expect("tempid");
|
|
|
|
|
|
|
|
match r {
|
|
|
|
QueryResults::Rel(vals) => {
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
vals,
|
|
|
|
vec![
|
|
|
|
vec![
|
|
|
|
TypedValue::Ref(e),
|
|
|
|
TypedValue::typed_ns_keyword("foo", "term"),
|
|
|
|
value,
|
|
|
|
TypedValue::Ref(tx.tx_id),
|
|
|
|
TypedValue::Boolean(true)
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
TypedValue::Ref(tx.tx_id),
|
|
|
|
TypedValue::typed_ns_keyword("db", "txInstant"),
|
|
|
|
TypedValue::Instant(tx.tx_instant),
|
|
|
|
TypedValue::Ref(tx.tx_id),
|
|
|
|
TypedValue::Boolean(true)
|
|
|
|
],
|
|
|
|
]
|
|
|
|
.into()
|
|
|
|
);
|
|
|
|
}
|
2018-04-16 21:08:00 +00:00
|
|
|
x => panic!("Got unexpected results {:?}", x),
|
|
|
|
}
|
2021-07-02 22:09:07 +00:00
|
|
|
}
|
2018-04-16 21:08:00 +00:00
|
|
|
|
2018-04-25 21:23:27 +00:00
|
|
|
assert_tx_data(&store, &tx1, "1".into());
|
|
|
|
assert_tx_data(&store, &tx2, "2".into());
|
2018-04-16 21:08:00 +00:00
|
|
|
}
|
2018-06-13 15:49:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tx_data() {
|
|
|
|
run_tx_data_test(Store::open("").expect("opened"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
#[test]
|
|
|
|
fn test_encrypted() {
|
|
|
|
// We expect this to blow up completely if something is wrong with the encryption,
|
|
|
|
// so the specific test we use doesn't matter that much.
|
|
|
|
run_tx_data_test(Store::open_with_key("", "secret").expect("opened"));
|
|
|
|
}
|