2017-06-14 23:17:25 +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.
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
extern crate core_traits;
|
2018-08-08 18:23:07 +00:00
|
|
|
extern crate edn;
|
2017-06-14 23:17:25 +00:00
|
|
|
extern crate mentat_core;
|
|
|
|
extern crate mentat_query_algebrizer;
|
2018-08-08 18:56:27 +00:00
|
|
|
extern crate query_algebrizer_traits;
|
2017-06-14 23:17:25 +00:00
|
|
|
|
2018-01-29 22:29:16 +00:00
|
|
|
mod utils;
|
|
|
|
|
2020-01-14 15:46:21 +00:00
|
|
|
use core_traits::{Attribute, TypedValue, ValueType, ValueTypeSet};
|
|
|
|
|
|
|
|
use mentat_core::{DateTime, Schema, Utc};
|
|
|
|
|
|
|
|
use edn::query::{Keyword, PlainSymbol, Variable};
|
|
|
|
|
|
|
|
use query_algebrizer_traits::errors::AlgebrizerError;
|
|
|
|
|
|
|
|
use mentat_query_algebrizer::{EmptyBecause, Known, QueryInputs};
|
|
|
|
|
2020-08-06 03:03:58 +00:00
|
|
|
use crate::utils::{add_attribute, alg, alg_with_inputs, associate_ident, bails};
|
2017-06-14 23:17:25 +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", "date"), 65);
|
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "double"), 66);
|
|
|
|
associate_ident(&mut schema, Keyword::namespaced("foo", "long"), 67);
|
2020-01-14 15:46:21 +00:00
|
|
|
add_attribute(
|
|
|
|
&mut schema,
|
|
|
|
65,
|
|
|
|
Attribute {
|
|
|
|
value_type: ValueType::Instant,
|
|
|
|
multival: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
add_attribute(
|
|
|
|
&mut schema,
|
|
|
|
66,
|
|
|
|
Attribute {
|
|
|
|
value_type: ValueType::Double,
|
|
|
|
multival: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
add_attribute(
|
|
|
|
&mut schema,
|
|
|
|
67,
|
|
|
|
Attribute {
|
|
|
|
value_type: ValueType::Long,
|
|
|
|
multival: false,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2017-06-14 23:17:25 +00:00
|
|
|
schema
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_instant_predicates_require_instants() {
|
|
|
|
let schema = prepopulated_schema();
|
2018-02-14 00:51:21 +00:00
|
|
|
let known = Known::for_schema(&schema);
|
2017-06-14 23:17:25 +00:00
|
|
|
|
|
|
|
// You can't use a string for an inequality: this is a straight-up error.
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:where
|
|
|
|
[?e :foo/date ?t]
|
|
|
|
[(> ?t "2017-06-16T00:56:41.257Z")]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
bails(known, query),
|
2018-06-27 18:53:35 +00:00
|
|
|
AlgebrizerError::InvalidArgumentType(
|
|
|
|
PlainSymbol::plain(">"),
|
|
|
|
ValueTypeSet::of_numeric_and_instant_types(),
|
2020-01-14 15:46:21 +00:00
|
|
|
1
|
|
|
|
)
|
|
|
|
);
|
2017-06-14 23:17:25 +00:00
|
|
|
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:where
|
|
|
|
[?e :foo/date ?t]
|
|
|
|
[(> "2017-06-16T00:56:41.257Z", ?t)]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
bails(known, query),
|
2018-06-27 18:53:35 +00:00
|
|
|
AlgebrizerError::InvalidArgumentType(
|
|
|
|
PlainSymbol::plain(">"),
|
|
|
|
ValueTypeSet::of_numeric_and_instant_types(),
|
2020-01-14 15:46:21 +00:00
|
|
|
0
|
|
|
|
)
|
|
|
|
); // We get this right.
|
2017-06-14 23:17:25 +00:00
|
|
|
|
|
|
|
// You can try using a number, which is valid input to a numeric predicate.
|
|
|
|
// In this store and query, though, that means we expect `?t` to be both
|
|
|
|
// an instant and a number, so the query is known-empty.
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:where
|
|
|
|
[?e :foo/date ?t]
|
|
|
|
[(> ?t 1234512345)]]"#;
|
2018-02-14 00:51:21 +00:00
|
|
|
let cc = alg(known, query);
|
2017-06-14 23:17:25 +00:00
|
|
|
assert!(cc.is_known_empty());
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
cc.empty_because.unwrap(),
|
|
|
|
EmptyBecause::TypeMismatch {
|
|
|
|
var: Variable::from_valid_name("?t"),
|
|
|
|
existing: ValueTypeSet::of_one(ValueType::Instant),
|
|
|
|
desired: ValueTypeSet::of_numeric_types(),
|
|
|
|
}
|
|
|
|
);
|
2017-06-14 23:17:25 +00:00
|
|
|
|
|
|
|
// You can compare doubles to longs.
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:where
|
|
|
|
[?e :foo/double ?t]
|
|
|
|
[(< ?t 1234512345)]]"#;
|
2018-02-14 00:51:21 +00:00
|
|
|
let cc = alg(known, query);
|
2017-06-14 23:17:25 +00:00
|
|
|
assert!(!cc.is_known_empty());
|
2020-01-14 15:46:21 +00:00
|
|
|
assert_eq!(
|
|
|
|
cc.known_type(&Variable::from_valid_name("?t"))
|
|
|
|
.expect("?t is known"),
|
|
|
|
ValueType::Double
|
|
|
|
);
|
2017-06-14 23:17:25 +00:00
|
|
|
}
|
2018-05-09 15:24:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_instant_predicates_accepts_var() {
|
|
|
|
let schema = prepopulated_schema();
|
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
|
|
|
|
let instant_var = Variable::from_valid_name("?time");
|
2020-01-14 15:46:21 +00:00
|
|
|
let instant_value = TypedValue::Instant(
|
|
|
|
DateTime::parse_from_rfc3339("2018-04-11T19:17:00.000Z")
|
|
|
|
.map(|t| t.with_timezone(&Utc))
|
|
|
|
.expect("expected valid date"),
|
|
|
|
);
|
2018-05-09 15:24:12 +00:00
|
|
|
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:in ?time
|
|
|
|
:where
|
|
|
|
[?e :foo/date ?t]
|
|
|
|
[(< ?t ?time)]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
let cc = alg_with_inputs(
|
|
|
|
known,
|
|
|
|
query,
|
|
|
|
QueryInputs::with_value_sequence(vec![(instant_var.clone(), instant_value.clone())]),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
cc.known_type(&instant_var).expect("?time is known"),
|
|
|
|
ValueType::Instant
|
|
|
|
);
|
2018-05-09 15:24:12 +00:00
|
|
|
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:in ?time
|
|
|
|
:where
|
|
|
|
[?e :foo/date ?t]
|
|
|
|
[(> ?time, ?t)]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
let cc = alg_with_inputs(
|
|
|
|
known,
|
|
|
|
query,
|
2020-08-06 03:03:58 +00:00
|
|
|
QueryInputs::with_value_sequence(vec![(instant_var.clone(), instant_value)]),
|
2020-01-14 15:46:21 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
cc.known_type(&instant_var).expect("?time is known"),
|
|
|
|
ValueType::Instant
|
|
|
|
);
|
2018-05-09 15:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_numeric_predicates_accepts_var() {
|
|
|
|
let schema = prepopulated_schema();
|
|
|
|
let known = Known::for_schema(&schema);
|
|
|
|
|
|
|
|
let numeric_var = Variable::from_valid_name("?long");
|
|
|
|
let numeric_value = TypedValue::Long(1234567);
|
|
|
|
|
|
|
|
// You can't use a string for an inequality: this is a straight-up error.
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:in ?long
|
|
|
|
:where
|
|
|
|
[?e :foo/long ?t]
|
|
|
|
[(> ?t ?long)]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
let cc = alg_with_inputs(
|
|
|
|
known,
|
|
|
|
query,
|
|
|
|
QueryInputs::with_value_sequence(vec![(numeric_var.clone(), numeric_value.clone())]),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
cc.known_type(&numeric_var).expect("?long is known"),
|
|
|
|
ValueType::Long
|
|
|
|
);
|
2018-05-09 15:24:12 +00:00
|
|
|
|
|
|
|
let query = r#"[:find ?e
|
|
|
|
:in ?long
|
|
|
|
:where
|
|
|
|
[?e :foo/long ?t]
|
|
|
|
[(> ?long, ?t)]]"#;
|
2020-01-14 15:46:21 +00:00
|
|
|
let cc = alg_with_inputs(
|
|
|
|
known,
|
|
|
|
query,
|
2020-08-06 03:03:58 +00:00
|
|
|
QueryInputs::with_value_sequence(vec![(numeric_var.clone(), numeric_value)]),
|
2020-01-14 15:46:21 +00:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
cc.known_type(&numeric_var).expect("?long is known"),
|
|
|
|
ValueType::Long
|
|
|
|
);
|
2018-05-09 15:24:12 +00:00
|
|
|
}
|