Fix variables in predicates
This commit is contained in:
parent
6adb97c587
commit
5fbf17f4f9
3 changed files with 106 additions and 14 deletions
|
@ -45,11 +45,18 @@ impl ConjoiningClauses {
|
||||||
use self::FnArg::*;
|
use self::FnArg::*;
|
||||||
match arg {
|
match arg {
|
||||||
FnArg::Variable(var) => {
|
FnArg::Variable(var) => {
|
||||||
self.constrain_var_to_numeric(var.clone());
|
match self.bound_value(&var) {
|
||||||
self.column_bindings
|
// The type is already known if it's a bound variable….
|
||||||
.get(&var)
|
Some(TypedValue::Long(v)) => Ok(QueryValue::TypedValue(TypedValue::Long(v))),
|
||||||
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
|
Some(TypedValue::Double(v)) => Ok(QueryValue::TypedValue(TypedValue::Double(v))),
|
||||||
.ok_or_else(|| Error::from_kind(ErrorKind::UnboundVariable(var.name())))
|
_ => {
|
||||||
|
self.constrain_var_to_numeric(var.clone());
|
||||||
|
self.column_bindings
|
||||||
|
.get(&var)
|
||||||
|
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
|
||||||
|
.ok_or_else(|| Error::from_kind(ErrorKind::UnboundVariable(var.name())))
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// Can't be an entid.
|
// Can't be an entid.
|
||||||
EntidOrInteger(i) => Ok(QueryValue::TypedValue(TypedValue::Long(i))),
|
EntidOrInteger(i) => Ok(QueryValue::TypedValue(TypedValue::Long(i))),
|
||||||
|
@ -73,11 +80,17 @@ impl ConjoiningClauses {
|
||||||
use self::FnArg::*;
|
use self::FnArg::*;
|
||||||
match arg {
|
match arg {
|
||||||
FnArg::Variable(var) => {
|
FnArg::Variable(var) => {
|
||||||
self.constrain_var_to_type(var.clone(), ValueType::Instant);
|
match self.bound_value(&var) {
|
||||||
self.column_bindings
|
// The type is already known if it's a bound variable….
|
||||||
.get(&var)
|
Some(TypedValue::Instant(v)) => Ok(QueryValue::TypedValue(TypedValue::Instant(v))),
|
||||||
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
|
_ => {
|
||||||
.ok_or_else(|| Error::from_kind(ErrorKind::UnboundVariable(var.name())))
|
self.constrain_var_to_type(var.clone(), ValueType::Instant);
|
||||||
|
self.column_bindings
|
||||||
|
.get(&var)
|
||||||
|
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
|
||||||
|
.ok_or_else(|| Error::from_kind(ErrorKind::UnboundVariable(var.name())))
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Constant(NonIntegerConstant::Instant(v)) => {
|
Constant(NonIntegerConstant::Instant(v)) => {
|
||||||
Ok(QueryValue::TypedValue(TypedValue::Instant(v)))
|
Ok(QueryValue::TypedValue(TypedValue::Instant(v)))
|
||||||
|
@ -144,10 +157,16 @@ impl ConjoiningClauses {
|
||||||
use self::FnArg::*;
|
use self::FnArg::*;
|
||||||
match arg {
|
match arg {
|
||||||
FnArg::Variable(var) => {
|
FnArg::Variable(var) => {
|
||||||
self.column_bindings
|
match self.bound_value(&var) {
|
||||||
.get(&var)
|
// The type is already known if it's a bound variable….
|
||||||
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
|
Some(v) => Ok(QueryValue::TypedValue(v)),
|
||||||
.ok_or_else(|| Error::from_kind(ErrorKind::UnboundVariable(var.name())))
|
None => {
|
||||||
|
self.column_bindings
|
||||||
|
.get(&var)
|
||||||
|
.and_then(|cols| cols.first().map(|col| QueryValue::Column(col.clone())))
|
||||||
|
.ok_or_else(|| Error::from_kind(ErrorKind::UnboundVariable(var.name())))
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
EntidOrInteger(i) => Ok(QueryValue::PrimitiveLong(i)),
|
EntidOrInteger(i) => Ok(QueryValue::PrimitiveLong(i)),
|
||||||
IdentOrKeyword(_) => unimplemented!(), // TODO
|
IdentOrKeyword(_) => unimplemented!(), // TODO
|
||||||
|
|
|
@ -17,7 +17,10 @@ mod utils;
|
||||||
|
|
||||||
use mentat_core::{
|
use mentat_core::{
|
||||||
Attribute,
|
Attribute,
|
||||||
|
DateTime,
|
||||||
Schema,
|
Schema,
|
||||||
|
TypedValue,
|
||||||
|
Utc,
|
||||||
ValueType,
|
ValueType,
|
||||||
ValueTypeSet,
|
ValueTypeSet,
|
||||||
};
|
};
|
||||||
|
@ -32,11 +35,13 @@ use mentat_query_algebrizer::{
|
||||||
EmptyBecause,
|
EmptyBecause,
|
||||||
ErrorKind,
|
ErrorKind,
|
||||||
Known,
|
Known,
|
||||||
|
QueryInputs,
|
||||||
};
|
};
|
||||||
|
|
||||||
use utils::{
|
use utils::{
|
||||||
add_attribute,
|
add_attribute,
|
||||||
alg,
|
alg,
|
||||||
|
alg_with_inputs,
|
||||||
associate_ident,
|
associate_ident,
|
||||||
bails,
|
bails,
|
||||||
};
|
};
|
||||||
|
@ -45,6 +50,7 @@ fn prepopulated_schema() -> Schema {
|
||||||
let mut schema = Schema::default();
|
let mut schema = Schema::default();
|
||||||
associate_ident(&mut schema, NamespacedKeyword::new("foo", "date"), 65);
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "date"), 65);
|
||||||
associate_ident(&mut schema, NamespacedKeyword::new("foo", "double"), 66);
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "double"), 66);
|
||||||
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "long"), 67);
|
||||||
add_attribute(&mut schema, 65, Attribute {
|
add_attribute(&mut schema, 65, Attribute {
|
||||||
value_type: ValueType::Instant,
|
value_type: ValueType::Instant,
|
||||||
multival: false,
|
multival: false,
|
||||||
|
@ -55,6 +61,11 @@ fn prepopulated_schema() -> Schema {
|
||||||
multival: false,
|
multival: false,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
add_attribute(&mut schema, 67, Attribute {
|
||||||
|
value_type: ValueType::Long,
|
||||||
|
multival: false,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
schema
|
schema
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,3 +127,60 @@ fn test_instant_predicates_require_instants() {
|
||||||
assert_eq!(cc.known_type(&Variable::from_valid_name("?t")).expect("?t is known"),
|
assert_eq!(cc.known_type(&Variable::from_valid_name("?t")).expect("?t is known"),
|
||||||
ValueType::Double);
|
ValueType::Double);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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");
|
||||||
|
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"));
|
||||||
|
|
||||||
|
let query = r#"[:find ?e
|
||||||
|
:in ?time
|
||||||
|
:where
|
||||||
|
[?e :foo/date ?t]
|
||||||
|
[(< ?t ?time)]]"#;
|
||||||
|
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);
|
||||||
|
|
||||||
|
let query = r#"[:find ?e
|
||||||
|
:in ?time
|
||||||
|
:where
|
||||||
|
[?e :foo/date ?t]
|
||||||
|
[(> ?time, ?t)]]"#;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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)]]"#;
|
||||||
|
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);
|
||||||
|
|
||||||
|
let query = r#"[:find ?e
|
||||||
|
:in ?long
|
||||||
|
:where
|
||||||
|
[?e :foo/long ?t]
|
||||||
|
[(> ?long, ?t)]]"#;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -98,3 +98,8 @@ pub fn alg(known: Known, input: &str) -> ConjoiningClauses {
|
||||||
let parsed = parse_find_string(input).expect("query input to have parsed");
|
let parsed = parse_find_string(input).expect("query input to have parsed");
|
||||||
algebrize(known, parsed).expect("algebrizing to have succeeded").cc
|
algebrize(known, parsed).expect("algebrizing to have succeeded").cc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn alg_with_inputs(known: Known, input: &str, inputs: QueryInputs) -> ConjoiningClauses {
|
||||||
|
let parsed = parse_find_string(input).expect("query input to have parsed");
|
||||||
|
algebrize_with_inputs(known, parsed, 0, inputs).expect("algebrizing to have succeeded").cc
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue