smallish improvements

main
Ziyang Hu 2 years ago
parent dff481130b
commit 61fbcdde52

@ -1,15 +1,15 @@
use std::collections::HashSet;
use std::collections::{BTreeSet};
use crate::db::table::TableId;
use crate::relation::value;
use crate::relation::value::Value;
pub fn extract_tables(val: &Value) -> HashSet<TableId> {
let mut coll = HashSet::new();
pub fn extract_tables(val: &Value) -> BTreeSet<TableId> {
let mut coll = BTreeSet::new();
do_extract_tables(val, &mut coll);
coll
}
fn do_extract_tables(val: &Value, coll: &mut HashSet<TableId>) {
fn do_extract_tables(val: &Value, coll: &mut BTreeSet<TableId>) {
match val {
Value::Null |
Value::Bool(_) |

@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::cmp::{max, min};
use std::collections::{BTreeMap, HashSet};
use std::collections::{BTreeMap, BTreeSet};
use cozorocks::SlicePtr;
use crate::db::cnf_transform::{cnf_transform, extract_tables};
use crate::db::engine::{Session};
@ -117,7 +117,8 @@ impl<'s> Session<'s> {
}
pub fn cnf_with_table_refs<'a>(&self, value: Value<'a>, params: &BTreeMap<String, Value<'a>>,
table_bindings: &AccessorMap) -> Result<Vec<(Value<'a>, HashSet<TableId>)>> {
table_bindings: &AccessorMap)
-> Result<BTreeMap<BTreeSet<TableId>, Value<'a>>> {
let (_, value) = self.partial_cnf_eval(value, params, table_bindings)?;
let conjunctives;
if let Value::Apply(op, args) = value {
@ -129,10 +130,20 @@ impl<'s> Session<'s> {
} else {
conjunctives = vec![value]
}
Ok(conjunctives.into_iter().map(|v| {
let grouped = conjunctives.into_iter().fold(BTreeMap::new(), |mut coll, v| {
let tids = extract_tables(&v);
(v, tids)
}).collect())
let ent = coll.entry(tids).or_insert(vec![]);
ent.push(v);
coll
}).into_iter().map(|(k, mut v)| {
let v = match v.len() {
0 => Value::Bool(true),
1 => v.pop().unwrap(),
_ => Value::Apply(value::OP_AND.into(), v)
};
(k, v)
}).collect::<BTreeMap<_, _>>();
Ok(grouped)
}
pub fn partial_eval<'a>(&self, value: Value<'a>, params: &BTreeMap<String, Value<'a>>,
@ -145,6 +156,7 @@ impl<'s> Session<'s> {
Value::Uuid(_) |
Value::Text(_) |
Value::EndSentinel) => Ok((true, v)),
v@Value::TupleRef(_, _) => Ok((false, v)),
Value::List(l) => {
let init_vec = Vec::with_capacity(l.len());
let res: Result<(bool, Vec<Value>)> = l.into_iter()
@ -257,9 +269,6 @@ impl<'s> Session<'s> {
_ => { todo!() }
})
}
Value::TupleRef(_, _) => {
todo!()
}
}
}
}

@ -199,6 +199,7 @@ mod tests {
let amap = sess.base_relation_to_accessor_map(&from_pat.table, &from_pat.binding, &from_pat.info);
let (_, vals) = sess.partial_eval(sel_pat.vals, &Default::default(), &amap).unwrap();
let (_, where_vals) = sess.partial_eval(where_pat, &Default::default(), &amap).unwrap();
println!("{:#?}", sess.cnf_with_table_refs(where_vals.clone(), &Default::default(), &amap));
let (vcoll, mut rel_tbls) = Value::extract_relevant_tables([vals, where_vals].into_iter()).unwrap();
let mut vcoll = vcoll.into_iter();
let vals = vcoll.next().unwrap();

@ -1,16 +1,23 @@
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use crate::db::engine::Session;
use crate::error::{CozoError, Result};
use crate::error::CozoError::LogicError;
use crate::relation::data::DataKind;
use crate::relation::typing::Typing;
#[derive(Eq, PartialEq, Debug, Clone, Copy, Ord, PartialOrd, Hash)]
#[derive(Eq, PartialEq, Clone, Copy, Ord, PartialOrd, Hash)]
pub struct TableId {
pub in_root: bool,
pub id: i64,
}
impl Debug for TableId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "#{}{}", if self.in_root { 'G' } else { 'L' }, self.id)
}
}
impl TableId {
pub fn new(in_root: bool, id: i64) -> Self {
TableId { in_root, id }
@ -32,12 +39,18 @@ impl From<(bool, i64)> for TableId {
}
}
#[derive(Eq, PartialEq, Debug, Clone, Copy, Ord, PartialOrd)]
#[derive(Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
pub struct ColId {
pub is_key: bool,
pub id: i64,
}
impl Debug for ColId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, ".{}{}", if self.is_key { 'K' } else { 'D' }, self.id)
}
}
impl From<(bool, i64)> for ColId {
fn from((is_key, id): (bool, i64)) -> Self {
Self { is_key, id }

@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt::{Display, Formatter, Write};
use std::fmt::{Debug, Display, Formatter, Write};
use lazy_static::lazy_static;
use pest::prec_climber::{Assoc, PrecClimber, Operator};
use ordered_float::OrderedFloat;
@ -92,7 +92,7 @@ impl TryFrom<u8> for Tag {
// C128Arr = 74,
#[derive(Debug, Clone, PartialEq, Ord, PartialOrd, Eq)]
#[derive(Clone, PartialEq, Ord, PartialOrd, Eq)]
pub enum Value<'a> {
// evaluated
Null,
@ -116,6 +116,12 @@ pub enum Value<'a> {
pub type StaticValue = Value<'static>;
impl <'a> Debug for Value<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Value {{ {} }}", self)
}
}
impl<'a> Value<'a> {
#[inline]
pub fn to_static(self) -> StaticValue {
@ -378,7 +384,7 @@ impl<'a> Display for Value<'a> {
write!(f, "(.{} {})", idx, value)?;
}
Value::TupleRef(tid, cid) => {
write!(f, "#{}{}{}{}", if tid.in_root { 'G' } else { 'L' }, tid.id, if cid.is_key { 'K' } else { 'D' }, cid.id)?;
write!(f, "#{}{}.{}{}", if tid.in_root { 'G' } else { 'L' }, tid.id, if cid.is_key { 'K' } else { 'D' }, cid.id)?;
}
}
Ok(())

Loading…
Cancel
Save