Compare commits
3 commits
master
...
fluffyemil
Author | SHA1 | Date | |
---|---|---|---|
![]() |
13be228ab7 | ||
![]() |
f785110536 | ||
![]() |
4ad7fed33f |
2 changed files with 106 additions and 20 deletions
|
@ -48,6 +48,7 @@ use errors::{
|
||||||
|
|
||||||
use types::{
|
use types::{
|
||||||
ColumnConstraint,
|
ColumnConstraint,
|
||||||
|
ColumnConstraintOrAlternation,
|
||||||
ColumnIntersection,
|
ColumnIntersection,
|
||||||
ComputedTable,
|
ComputedTable,
|
||||||
Column,
|
Column,
|
||||||
|
@ -806,26 +807,24 @@ impl ConjoiningClauses {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// When a CC has accumulated all patterns, generate value_type_tag entries in `wheres`
|
/// For each bound var. discover which ones have
|
||||||
/// to refine value types for which two things are true:
|
|
||||||
///
|
|
||||||
/// - There are two or more different types with the same SQLite representation. E.g.,
|
|
||||||
/// ValueType::Boolean shares a representation with Integer and Ref.
|
|
||||||
/// - There is no attribute constraint present in the CC.
|
|
||||||
///
|
|
||||||
/// It's possible at this point for the space of acceptable type tags to not intersect: e.g.,
|
|
||||||
/// for the query
|
|
||||||
///
|
|
||||||
/// ```edn
|
|
||||||
/// [:find ?x :where
|
|
||||||
/// [?x ?y true]
|
|
||||||
/// [?z ?y ?x]]
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// where `?y` must simultaneously be a ref-typed attribute and a boolean-typed attribute. This
|
|
||||||
/// function deduces that and calls `self.mark_known_empty`. #293.
|
|
||||||
pub fn expand_type_tags(&mut self) {
|
pub fn expand_type_tags(&mut self) {
|
||||||
// TODO.
|
let mut vars_to_remove = Vec::new();
|
||||||
|
for (v, tag) in self.extracted_types.iter() {
|
||||||
|
if let Some(cols) = self.column_bindings.get(&v) {
|
||||||
|
for col in cols {
|
||||||
|
if col.0 != tag.0 {
|
||||||
|
let value = QueryValue::Column(QualifiedAlias(col.0.clone(), Column::Fixed(DatomsColumn::ValueTypeTag)));
|
||||||
|
self.wheres.0.push(ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(tag.clone(), value)));
|
||||||
|
vars_to_remove.push(v.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for v in vars_to_remove {
|
||||||
|
self.extracted_types.remove(&v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -888,8 +887,58 @@ pub fn ident(ns: &str, name: &str) -> PatternNonValuePlace {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
extern crate mentat_query_parser;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
use self::mentat_query_parser::parse_find_string;
|
||||||
|
use algebrize;
|
||||||
|
|
||||||
|
fn prepopulated_schema() -> Schema {
|
||||||
|
let mut schema = Schema::default();
|
||||||
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "name"), 65);
|
||||||
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "knows"), 66);
|
||||||
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "parent"), 67);
|
||||||
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "age"), 68);
|
||||||
|
associate_ident(&mut schema, NamespacedKeyword::new("foo", "height"), 69);
|
||||||
|
add_attribute(&mut schema,
|
||||||
|
65,
|
||||||
|
Attribute {
|
||||||
|
value_type: ValueType::String,
|
||||||
|
multival: false,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
add_attribute(&mut schema,
|
||||||
|
66,
|
||||||
|
Attribute {
|
||||||
|
value_type: ValueType::String,
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// Our alias counter is shared between CCs.
|
// Our alias counter is shared between CCs.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_aliasing_through_template() {
|
fn test_aliasing_through_template() {
|
||||||
|
@ -902,4 +951,40 @@ mod tests {
|
||||||
assert!(alias_zero != alias_one);
|
assert!(alias_zero != alias_one);
|
||||||
assert!(alias_one != alias_two);
|
assert!(alias_one != alias_two);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_expand_type_tags_constrain_value_types() {
|
||||||
|
let schema = prepopulated_schema();
|
||||||
|
let query = r#"
|
||||||
|
[:find ?x
|
||||||
|
:where [?x :foo/name]
|
||||||
|
[?x _ ?y]
|
||||||
|
[?a _ ?y]]"#;
|
||||||
|
let parsed = parse_find_string(query).expect("parse failed");
|
||||||
|
let cc = algebrize(&schema, parsed).expect("Expected a valid query").cc;
|
||||||
|
|
||||||
|
let d0 = "datoms00".to_string();
|
||||||
|
let d0e = QualifiedAlias::new(d0.clone(), DatomsColumn::Entity);
|
||||||
|
let d0a = QualifiedAlias::new(d0.clone(), DatomsColumn::Attribute);
|
||||||
|
|
||||||
|
let d1 = "all_datoms01".to_string();
|
||||||
|
let d1e = QualifiedAlias::new(d1.clone(), DatomsColumn::Entity);
|
||||||
|
let d1v = QualifiedAlias::new(d1.clone(), DatomsColumn::Value);
|
||||||
|
let d1t = QualifiedAlias::new(d1.clone(), DatomsColumn::ValueTypeTag);
|
||||||
|
|
||||||
|
let d2 = "all_datoms02".to_string();
|
||||||
|
let d2v = QualifiedAlias::new(d2.clone(), DatomsColumn::Value);
|
||||||
|
let d2t = QualifiedAlias::new(d2.clone(), DatomsColumn::ValueTypeTag);
|
||||||
|
|
||||||
|
let name = QueryValue::Entid(65);
|
||||||
|
|
||||||
|
assert!(!cc.is_known_empty());
|
||||||
|
assert_eq!(cc.wheres, ColumnIntersection(vec![
|
||||||
|
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0a.clone(), name)),
|
||||||
|
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d0e.clone(), QueryValue::Column(d1e.clone()))),
|
||||||
|
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1v.clone(), QueryValue::Column(d2v.clone()))),
|
||||||
|
ColumnConstraintOrAlternation::Constraint(ColumnConstraint::Equals(d1t.clone(), QueryValue::Column(d2t.clone()))),
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -183,6 +183,7 @@ pub fn algebrize_with_inputs(schema: &Schema,
|
||||||
}
|
}
|
||||||
cc.expand_column_bindings();
|
cc.expand_column_bindings();
|
||||||
cc.prune_extracted_types();
|
cc.prune_extracted_types();
|
||||||
|
cc.expand_type_tags();
|
||||||
|
|
||||||
let (order, extra_vars) = validate_and_simplify_order(&cc, parsed.order)?;
|
let (order, extra_vars) = validate_and_simplify_order(&cc, parsed.order)?;
|
||||||
let with: BTreeSet<Variable> = parsed.with.into_iter().chain(extra_vars.into_iter()).collect();
|
let with: BTreeSet<Variable> = parsed.with.into_iter().chain(extra_vars.into_iter()).collect();
|
||||||
|
|
Loading…
Reference in a new issue