2017-04-26 22:50:17 +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.
|
|
|
|
|
|
|
|
extern crate mentat_core;
|
|
|
|
extern crate mentat_query;
|
|
|
|
extern crate mentat_query_algebrizer;
|
|
|
|
extern crate mentat_query_parser;
|
|
|
|
|
2018-01-29 22:29:16 +00:00
|
|
|
mod utils;
|
|
|
|
|
2017-06-12 18:38:46 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2017-04-26 22:50:17 +00:00
|
|
|
use mentat_core::{
|
|
|
|
Attribute,
|
|
|
|
Schema,
|
|
|
|
ValueType,
|
|
|
|
TypedValue,
|
|
|
|
};
|
|
|
|
|
|
|
|
use mentat_query::{
|
2018-05-11 16:52:17 +00:00
|
|
|
Keyword,
|
2017-04-26 22:50:17 +00:00
|
|
|
PlainSymbol,
|
|
|
|
Variable,
|
|
|
|
};
|
|
|
|
|
|
|
|
use mentat_query_algebrizer::{
|
|
|
|
BindingError,
|
|
|
|
ComputedTable,
|
|
|
|
Error,
|
|
|
|
ErrorKind,
|
2018-02-14 00:51:21 +00:00
|
|
|
Known,
|
2017-06-12 18:38:46 +00:00
|
|
|
QueryInputs,
|
2017-04-26 22:50:17 +00:00
|
|
|
};
|
|
|
|
|
2018-01-29 22:29:16 +00:00
|
|
|
use utils::{
|
|
|
|
add_attribute,
|
|
|
|
alg,
|
|
|
|
associate_ident,
|
|
|
|
bails,
|
|
|
|
bails_with_inputs,
|
|
|
|
};
|
2017-04-26 22:50:17 +00:00
|
|
|
|
|
|
|
fn prepopulated_schema() -> Schema {
|
|
|
|
let mut schema = Schema::default();
|
2018-05-11 16:52:17 +00:00
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "name"), 65);
|
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "knows"), 66);
|
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "parent"), 67);
|
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "age"), 68);
|
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "height"), 69);
|
2017-04-26 22:50:17 +00:00
|
|
|
add_attribute(&mut schema, 65, Attribute {
|
|
|
|
value_type: ValueType::String,
|
|
|
|
multival: false,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
add_attribute(&mut schema, 66, Attribute {
|
|
|
|
value_type: ValueType::Ref,
|
|
|
|
multival: true,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
add_attribute(&mut schema, 67, Attribute {
|
|
|
|
value_type: ValueType::String,
|
|
|
|
multival: true,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
add_attribute(&mut schema, 68, Attribute {
|
|
|
|
value_type: ValueType::Long,
|
|
|
|
multival: false,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
add_attribute(&mut schema, 69, Attribute {
|
|
|
|
value_type: ValueType::Long,
|
|
|
|
multival: false,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
schema
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_doesnt_bail_for_type_conflicts() {
|
|
|
|
// We know `?x` to be a ref, but we're attempting to ground it to a Double.
|
|
|
|
// The query can return no results.
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground 9.95) ?x]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_tuple_fails_impossible() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [5 9.95]) [?x ?p]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_scalar_fails_impossible() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground true) ?p]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_coll_skips_impossible() {
|
|
|
|
// We know `?x` to be a ref, but we're attempting to ground it to a Double.
|
|
|
|
// The query can return no results.
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [5 9.95 11]) [?x ...]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_none());
|
|
|
|
assert_eq!(cc.computed_tables[0], ComputedTable::NamedValues {
|
|
|
|
names: vec![Variable::from_valid_name("?x")],
|
|
|
|
values: vec![TypedValue::Ref(5), TypedValue::Ref(11)],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_coll_fails_if_all_impossible() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [5.1 5.2]) [?p ...]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_skips_impossible() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [[8 "foo"] [5 7] [9.95 9] [11 12]]) [[?x ?p]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_none());
|
|
|
|
assert_eq!(cc.computed_tables[0], ComputedTable::NamedValues {
|
|
|
|
names: vec![Variable::from_valid_name("?x"), Variable::from_valid_name("?p")],
|
|
|
|
values: vec![TypedValue::Ref(5), TypedValue::Ref(7), TypedValue::Ref(11), TypedValue::Ref(12)],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_fails_if_all_impossible() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [[11 5.1] [12 5.2]]) [[?x ?p]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_tuple_rejects_all_placeholders() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [8 "foo" 3]) [_ _ _]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_rejects_all_placeholders() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [[8 "foo"]]) [[_ _]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_tuple_placeholders() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [8 "foo" 3]) [?x _ ?p]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_none());
|
|
|
|
assert_eq!(cc.bound_value(&Variable::from_valid_name("?x")), Some(TypedValue::Ref(8)));
|
|
|
|
assert_eq!(cc.bound_value(&Variable::from_valid_name("?p")), Some(TypedValue::Ref(3)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_placeholders() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/knows ?p] [(ground [[8 "foo" 3] [5 false 7] [5 9.95 9]]) [[?x _ ?p]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_none());
|
|
|
|
assert_eq!(cc.computed_tables[0], ComputedTable::NamedValues {
|
|
|
|
names: vec![Variable::from_valid_name("?x"), Variable::from_valid_name("?p")],
|
|
|
|
values: vec![
|
|
|
|
TypedValue::Ref(8),
|
|
|
|
TypedValue::Ref(3),
|
|
|
|
TypedValue::Ref(5),
|
|
|
|
TypedValue::Ref(7),
|
|
|
|
TypedValue::Ref(5),
|
|
|
|
TypedValue::Ref(9),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do with ground, but while we're here…
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_reference_type_failure() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/age ?y] [?x :foo/knows ?y]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_tuple_infers_types() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/age ?v] [(ground [8 10]) [?x ?v]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_none());
|
|
|
|
assert_eq!(cc.bound_value(&Variable::from_valid_name("?x")), Some(TypedValue::Ref(8)));
|
|
|
|
assert_eq!(cc.bound_value(&Variable::from_valid_name("?v")), Some(TypedValue::Long(10)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_infers_types() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/age ?v] [(ground [[8 10]]) [[?x ?v]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let cc = alg(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
assert!(cc.empty_because.is_none());
|
|
|
|
assert_eq!(cc.computed_tables[0], ComputedTable::NamedValues {
|
|
|
|
names: vec![Variable::from_valid_name("?x"), Variable::from_valid_name("?v")],
|
|
|
|
values: vec![TypedValue::Ref(8), TypedValue::Long(10)],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_coll_heterogeneous_types() {
|
|
|
|
let q = r#"[:find ?x :where [?x _ ?v] [(ground [false 8.5]) [?v ...]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let e = bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
match e {
|
|
|
|
Error(ErrorKind::InvalidGroundConstant, _) => {
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_heterogeneous_types() {
|
|
|
|
let q = r#"[:find ?x :where [?x _ ?v] [(ground [[false] [5]]) [[?v]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let e = bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
match e {
|
|
|
|
Error(ErrorKind::InvalidGroundConstant, _) => {
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_tuple_duplicate_vars() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/age ?v] [(ground [8 10]) [?x ?x]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let e = bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
match e {
|
|
|
|
Error(ErrorKind::InvalidBinding(v, e), _) => {
|
2018-05-11 16:52:17 +00:00
|
|
|
assert_eq!(v, PlainSymbol::plain("ground"));
|
2017-04-26 22:50:17 +00:00
|
|
|
assert_eq!(e, BindingError::RepeatedBoundVariable);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_rel_duplicate_vars() {
|
|
|
|
let q = r#"[:find ?x :where [?x :foo/age ?v] [(ground [[8 10]]) [[?x ?x]]]]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let e = bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
match e {
|
|
|
|
Error(ErrorKind::InvalidBinding(v, e), _) => {
|
2018-05-11 16:52:17 +00:00
|
|
|
assert_eq!(v, PlainSymbol::plain("ground"));
|
2017-04-26 22:50:17 +00:00
|
|
|
assert_eq!(e, BindingError::RepeatedBoundVariable);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ground_nonexistent_variable_invalid() {
|
|
|
|
let q = r#"[:find ?x ?e :where [?e _ ?x] (not [(ground 17) ?v])]"#;
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
let e = bails(known, &q);
|
2017-04-26 22:50:17 +00:00
|
|
|
match e {
|
|
|
|
Error(ErrorKind::UnboundVariable(PlainSymbol(v)), _) => {
|
|
|
|
assert_eq!(v, "?v".to_string());
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 18:38:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unbound_input_variable_invalid() {
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
2017-06-12 18:38:46 +00:00
|
|
|
let q = r#"[:find ?y ?age :in ?x :where [(ground [?x]) [?y ...]] [?y :foo/age ?age]]"#;
|
|
|
|
|
|
|
|
// This fails even if we know the type: we don't support grounding bindings
|
|
|
|
// that aren't known at algebrizing time.
|
|
|
|
let mut types = BTreeMap::default();
|
|
|
|
types.insert(Variable::from_valid_name("?x"), ValueType::Ref);
|
|
|
|
|
|
|
|
let i = QueryInputs::new(types, BTreeMap::default()).expect("valid QueryInputs");
|
|
|
|
|
2018-02-14 00:51:21 +00:00
|
|
|
let e = bails_with_inputs(known, &q, i);
|
2017-06-12 18:38:46 +00:00
|
|
|
match e {
|
|
|
|
Error(ErrorKind::UnboundVariable(v), _) => {
|
|
|
|
assert_eq!(v.0, "?x");
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|