Add specialized tx-before and tx-after predicates. (#599) r=emily

This commit is contained in:
Richard Newman 2018-04-05 10:49:06 -07:00 committed by GitHub
parent 8607ecb745
commit a57f7aff99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 6 deletions

View file

@ -33,6 +33,7 @@ use types::{
ColumnConstraint, ColumnConstraint,
EmptyBecause, EmptyBecause,
Inequality, Inequality,
QueryValue,
}; };
use Known; use Known;
@ -150,16 +151,34 @@ impl ConjoiningClauses {
// These arguments must be variables or instant/numeric constants. // These arguments must be variables or instant/numeric constants.
// TODO: static evaluation. #383. // TODO: static evaluation. #383.
let constraint = ColumnConstraint::Inequality { let constraint = comparison.to_constraint(left_v, right_v);
operator: comparison,
left: left_v,
right: right_v,
};
self.wheres.add_intersection(constraint); self.wheres.add_intersection(constraint);
Ok(()) Ok(())
} }
} }
impl Inequality {
fn to_constraint(&self, left: QueryValue, right: QueryValue) -> ColumnConstraint {
match *self {
Inequality::TxAfter |
Inequality::TxBefore => {
// TODO: both ends of the range must be inside the tx partition!
// If we know the partition map -- and at this point we do, it's just
// not passed to this function -- then we can generate two constraints,
// or clamp a fixed value.
},
_ => {
},
}
ColumnConstraint::Inequality {
operator: *self,
left: left,
right: right,
}
}
}
#[cfg(test)] #[cfg(test)]
mod testing { mod testing {
use super::*; use super::*;

View file

@ -287,6 +287,10 @@ pub enum Inequality {
// Ref operators. // Ref operators.
Unpermute, Unpermute,
Differ, Differ,
// Tx operators.
TxAfter,
TxBefore,
} }
impl Inequality { impl Inequality {
@ -301,6 +305,9 @@ impl Inequality {
Unpermute => "<", Unpermute => "<",
Differ => "<>", Differ => "<>",
TxAfter => ">",
TxBefore => "<",
} }
} }
@ -314,6 +321,9 @@ impl Inequality {
"unpermute" => Some(Inequality::Unpermute), "unpermute" => Some(Inequality::Unpermute),
"differ" => Some(Inequality::Differ), "differ" => Some(Inequality::Differ),
"tx-after" => Some(Inequality::TxAfter),
"tx-before" => Some(Inequality::TxBefore),
_ => None, _ => None,
} }
} }
@ -332,7 +342,9 @@ impl Inequality {
ts ts
}, },
&Unpermute | &Unpermute |
&Differ => { &Differ |
&TxAfter |
&TxBefore => {
ValueTypeSet::of_one(ValueType::Ref) ValueTypeSet::of_one(ValueType::Ref)
}, },
} }
@ -351,6 +363,9 @@ impl Debug for Inequality {
&Unpermute => "<", &Unpermute => "<",
&Differ => "<>", &Differ => "<>",
&TxAfter => ">",
&TxBefore => "<",
}) })
} }
} }

View file

@ -1115,3 +1115,22 @@ fn test_project_aggregates() {
WHERE `datoms00`.a = 99)"); WHERE `datoms00`.a = 99)");
assert_eq!(args, vec![]); assert_eq!(args, vec![]);
} }
#[test]
fn test_tx_before_and_after() {
let schema = prepopulated_typed_schema(ValueType::Long);
let query = r#"[:find ?x :where [?x _ _ ?tx] [(tx-after ?tx 12345)]]"#;
let SQLQuery { sql, args } = translate(&schema, query);
assert_eq!(sql, "SELECT DISTINCT \
`datoms00`.e AS `?x` \
FROM `datoms` AS `datoms00` \
WHERE `datoms00`.tx > 12345");
assert_eq!(args, vec![]);
let query = r#"[:find ?x :where [?x _ _ ?tx] [(tx-before ?tx 12345)]]"#;
let SQLQuery { sql, args } = translate(&schema, query);
assert_eq!(sql, "SELECT DISTINCT \
`datoms00`.e AS `?x` \
FROM `datoms` AS `datoms00` \
WHERE `datoms00`.tx < 12345");
assert_eq!(args, vec![]);
}