Add TupleRef to value

main
Ziyang Hu 2 years ago
parent fb97bf632b
commit 1d9f1e55e6

@ -113,6 +113,9 @@ impl<'s> Session<'s> {
_ => { todo!() }
})
}
Value::TupleRef(_, _) => {
todo!()
}
}
}
@ -469,6 +472,9 @@ impl<'s> Session<'s> {
collected.push(cur_val);
Ok((false, has_null, collected))
}
Value::TupleRef(_, _) => {
todo!()
}
}
}
}
@ -612,6 +618,9 @@ impl<'s> Session<'s> {
collected.push(cur_val);
Ok((false, has_null, collected))
}
Value::TupleRef(_, _) => {
todo!()
}
}
}
}

@ -48,8 +48,9 @@ pub enum QueryPlan {
filter: StaticValue,
},
BaseRelation {
table: String,
binding: String,
// table: String,
// binding: String,
accessors: AccessorMap,
info: TableInfo,
},
}
@ -61,6 +62,9 @@ pub enum OuterJoinType {
FullOuterJoin,
}
pub type AccessorMap = BTreeMap<String, BTreeMap<String, (usize, bool, usize)>>;
impl<'a> Session<'a> {
pub fn query_to_plan(&self, pair: Pair<Rule>) -> Result<()> {
let mut pairs = pair.into_inner();
@ -85,8 +89,7 @@ impl<'a> Session<'a> {
let res = match from_data.pop().unwrap() {
FromEl::Simple(el) => {
QueryPlan::BaseRelation {
table: el.table,
binding: el.binding,
accessors: self.single_table_accessor_map(el.binding, &el.info),
info: el.info,
}
}
@ -94,6 +97,14 @@ impl<'a> Session<'a> {
};
Ok(res)
}
fn single_table_accessor_map(&self, binding: String, info: &TableInfo) -> AccessorMap {
// let mut ret = BTreeMap::new();
// for (k, v) in &info.key_typing {}
// for (k, v) in &info.val_typing {
//
// }
todo!()
}
fn convert_where_data_to_plan(&self, plan: QueryPlan, where_data: StaticValue) -> Result<QueryPlan> {
let where_data = self.partial_eval(where_data, &Default::default(), &Default::default());
let plan = match where_data?.1 {

@ -270,7 +270,7 @@ impl<'a> Session<'a> {
vals,
ordering,
limit,
offset
offset,
})
}
}
@ -282,7 +282,7 @@ pub struct Selection {
pub vals: StaticValue,
pub ordering: Vec<(bool, String)>,
pub limit: Option<i64>,
pub offset: Option<i64>
pub offset: Option<i64>,
}
#[cfg(test)]

@ -5,7 +5,7 @@ use crate::error::CozoError::LogicError;
use crate::relation::data::DataKind;
use crate::relation::typing::Typing;
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
#[derive(Eq, PartialEq, Debug, Clone, Copy, Ord, PartialOrd)]
pub struct TableId {
pub in_root: bool,
pub id: i64,
@ -20,6 +20,12 @@ impl TableId {
}
}
#[derive(Eq, PartialEq, Debug, Clone, Copy, Ord, PartialOrd)]
pub struct ColId {
pub is_key: bool,
pub id: i64,
}
impl Default for TableId {
fn default() -> Self {
TableId { in_root: false, id: -1 }

@ -4,6 +4,7 @@ use std::collections::BTreeMap;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use uuid::Uuid;
use crate::db::table::{ColId, TableId};
use crate::relation::data::DataKind;
use crate::relation::value::{Tag, Value};
@ -90,6 +91,10 @@ impl<T: AsRef<[u8]>> Tuple<T> {
Tag::Dict |
Tag::IdxAccess |
Tag::FieldAccess => start + u32::from_be_bytes(data[start..start + 4].try_into().unwrap()) as usize,
Tag::TupleRef => {
let temp = start + 1 + self.parse_varint(start + 1).1 + 1;
temp + self.parse_varint(temp).1
}
Tag::MaxTag => panic!(),
};
self.idx_cache.borrow_mut().push(nxt);
@ -225,11 +230,7 @@ impl<T: AsRef<[u8]>> Tuple<T> {
Tag::BoolFalse => (start, false.into()),
Tag::Int => {
let (u, offset) = self.parse_varint(start);
let val = if u & 1 == 0 {
(u >> 1) as i64
} else {
-((u >> 1) as i64) - 1
};
let val = Self::varint_to_zigzag(u);
(start + offset, val.into())
}
Tag::Float => (start + 8, f64::from_be_bytes(data[start..start + 8].try_into().unwrap()).into()),
@ -321,9 +322,26 @@ impl<T: AsRef<[u8]>> Tuple<T> {
let (val, _) = self.parse_value_at(start_pos);
(end_pos, Value::FieldAccess(field.into(), val.into()))
}
Tag::TupleRef => {
let in_root = self.parse_value_at(start).0 == Value::Bool(true);
let (tidu, parse_len) = self.parse_varint(start + 1);
let is_key = self.parse_value_at(parse_len + start + 1).0 == Value::Bool(true);
let (cidu, parse_len2) = self.parse_varint(start + 1 + parse_len + 1);
(start + 1 + parse_len + 1 + parse_len2,
Value::TupleRef(TableId { in_root, id: Self::varint_to_zigzag(tidu) },
ColId { is_key, id: Self::varint_to_zigzag(cidu) }))
}
};
(val, nxt)
}
fn varint_to_zigzag(u: u64) -> i64 {
if u & 1 == 0 {
(u >> 1) as i64
} else {
-((u >> 1) as i64) - 1
}
}
pub fn iter(&self) -> TupleIter<T> {
TupleIter {
tuple: self,
@ -536,6 +554,17 @@ impl OwnTuple {
cache.truncate(start_len);
cache.push(self.data.len());
}
Value::TupleRef(tid, cid) => {
self.push_tag(Tag::Dict);
let start_len = self.idx_cache.borrow().len();
self.push_bool(tid.in_root);
self.push_int(tid.id);
self.push_bool(cid.is_key);
self.push_int(cid.id);
let mut cache = self.idx_cache.borrow_mut();
cache.truncate(start_len);
cache.push(self.data.len());
}
Value::EndSentinel => panic!("Cannot push sentinel value"),
}
}

@ -7,6 +7,7 @@ use ordered_float::OrderedFloat;
use pest::Parser as PestParser;
use pest::iterators::Pair;
use uuid::Uuid;
use crate::db::table::{ColId, TableId};
use crate::parser::{Parser, Rule};
use crate::error::{CozoError, Result};
use crate::parser::number::parse_int;
@ -27,6 +28,7 @@ pub enum Tag {
List = 128,
Dict = 129,
TupleRef = 250,
IdxAccess = 251,
FieldAccess = 252,
Variable = 253,
@ -47,8 +49,11 @@ impl TryFrom<u8> for Tag {
5 => Float,
6 => Text,
7 => Uuid,
128 => List,
129 => Dict,
250 => TupleRef,
251 => IdxAccess,
252 => FieldAccess,
253 => Variable,
@ -100,6 +105,7 @@ pub enum Value<'a> {
Dict(BTreeMap<Cow<'a, str>, Value<'a>>),
// not evaluated
Variable(Cow<'a, str>),
TupleRef(TableId, ColId),
Apply(Cow<'a, str>, Vec<Value<'a>>),
FieldAccess(Cow<'a, str>, Box<Value<'a>>),
IdxAccess(usize, Box<Value<'a>>),
@ -135,6 +141,7 @@ impl<'a> Value<'a> {
Value::IdxAccess(idx, value) => {
Value::IdxAccess(idx, value.to_static().into())
}
Value::TupleRef(tid, cid) => Value::TupleRef(tid, cid)
}
}
#[inline]
@ -152,7 +159,8 @@ impl<'a> Value<'a> {
Value::Variable(_) => false,
Value::Apply(_, _) => false,
Value::FieldAccess(_, _) => false,
Value::IdxAccess(_, _) => false
Value::IdxAccess(_, _) => false,
Value::TupleRef(_, _) => false
}
}
#[inline]
@ -314,6 +322,9 @@ impl<'a> Display for Value<'a> {
Value::IdxAccess(idx, value) => {
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)?;
}
}
Ok(())
}

Loading…
Cancel
Save