table extraction

main
Ziyang Hu 2 years ago
parent 31ca5bf79e
commit 630beb1954

@ -10,9 +10,6 @@ use crate::error::CozoError::LogicError;
use crate::relation::tuple::{OwnTuple, Tuple};
use crate::relation::value;
pub fn extract_relevant_tables<'a>(value: Value<'a>) -> (Value<'a>, Vec<(TableId, ColId)>) {
todo!()
}
impl<'s> Session<'s> {
pub fn tuple_eval<'a>(&self, value: Value<'a>, tuples: Vec<Tuple<SlicePtr>>) -> Result<(OwnTuple, OwnTuple)> {
@ -68,7 +65,7 @@ impl<'s> Session<'s> {
Value::FieldAccess(field, arg) => {
// convert to tuple refs
if let Value::Variable(v) = &*arg {
if let Some(sub_dict) = table_bindings.get(v.as_ref()) {
if let Some(sub_dict) = table_bindings.get(v.as_ref()) {
return match sub_dict.get(field.as_ref()) {
None => {
Err(LogicError("Cannot resolve field in bound table".to_string()))
@ -76,7 +73,7 @@ impl<'s> Session<'s> {
Some(d) => {
Ok((false, Value::TupleRef(d.0, d.1)))
}
}
};
}
}

@ -10,6 +10,7 @@ use uuid::Uuid;
use crate::db::table::{ColId, TableId};
use crate::parser::{Parser, Rule};
use crate::error::{CozoError, Result};
use crate::error::CozoError::LogicError;
use crate::parser::number::parse_int;
use crate::parser::text_identifier::parse_string;
@ -174,6 +175,52 @@ impl<'a> Value<'a> {
let pair = pair.ok_or_else(|| CozoError::LogicError("Parsing value failed".to_string()))?;
Value::from_pair(pair)
}
pub fn extract_relevant_tables(self, coll: &mut Vec<TableId>) -> Result<Self> {
Ok(match self {
v @ (Value::Null |
Value::Bool(_) |
Value::Int(_) |
Value::Float(_) |
Value::Uuid(_) |
Value::Text(_) |
Value::Variable(_)) => v,
Value::List(l) => {
Value::List(l.into_iter()
.map(|v| v.extract_relevant_tables(coll))
.collect::<Result<Vec<_>>>()?)
}
Value::Dict(d) => {
Value::Dict(d.into_iter()
.map(|(k, v)|
v.extract_relevant_tables(coll).map(|v| (k, v)))
.collect::<Result<BTreeMap<_, _>>>()?)
}
Value::TupleRef(tid, cid) => {
let pos = coll.iter().position(|id| id == &tid).unwrap_or_else(|| {
let olen = coll.len();
coll.push(tid);
olen
});
Value::TupleRef((false, pos).into(), cid)
}
Value::Apply(op, args) => {
Value::Apply(op, args.into_iter()
.map(|v| v.extract_relevant_tables(coll))
.collect::<Result<Vec<_>>>()?)
}
Value::FieldAccess(field, arg) => {
Value::FieldAccess(field, arg.extract_relevant_tables(coll)?.into())
}
Value::IdxAccess(idx, arg) => {
Value::IdxAccess(idx, arg.extract_relevant_tables(coll)?.into())
}
Value::EndSentinel => {
return Err(LogicError("Encountered end sentinel".to_string()));
}
})
}
}
impl From<()> for StaticValue {

Loading…
Cancel
Save