recursive typing resolve

main
Ziyang Hu 2 years ago
parent 517756e087
commit 017025884a

@ -1,3 +1,4 @@
pub mod engine;
pub mod eval;
pub mod plan;
pub mod plan;
pub mod table_def;

@ -94,7 +94,6 @@ impl Engine {
let ret = Arc::new(RwLock::new(SessionHandle {
cf_ident,
status: SessionStatus::Prepared,
table_count: 0,
}));
guard.push(ret.clone());
ret
@ -188,7 +187,6 @@ impl<'a> Drop for Session<'a> {
pub struct SessionHandle {
cf_ident: String,
status: SessionStatus,
table_count: usize,
}
#[derive(Eq, PartialEq, Debug, Clone)]
@ -202,7 +200,6 @@ pub enum SessionStatus {
mod tests {
use std::{fs, thread};
use crate::db::eval::Environment;
use crate::relation::table::DataKind::Value;
use crate::relation::tuple::Tuple;
use super::*;

@ -1,10 +1,11 @@
use cozorocks::SlicePtr;
use crate::db::engine::{Session};
use crate::relation::table::{DataKind, Table};
use crate::relation::table::{Table};
use crate::relation::tuple::{Tuple};
use crate::relation::typing::Typing;
use crate::relation::value::Value;
use crate::error::Result;
use crate::relation::data::DataKind;
pub trait Environment<T: AsRef<[u8]>> {
fn push_env(&mut self);
@ -12,7 +13,7 @@ pub trait Environment<T: AsRef<[u8]>> {
fn define_variable(&mut self, name: &str, val: &Value, in_root: bool) -> Result<()>;
fn define_type_alias(&mut self, name: &str, typ: &Typing, in_root: bool) -> Result<()>;
fn define_table(&mut self, table: &Table, in_root: bool) -> Result<()>;
fn resolve(&mut self, name: &str) -> Result<Option<Tuple<T>>>;
fn resolve(&self, name: &str) -> Result<Option<Tuple<T>>>;
fn delete_defined(&mut self, name: &str, in_root: bool) -> Result<()>;
}
@ -91,7 +92,7 @@ impl<'a> Environment<SlicePtr> for Session<'a> {
todo!()
}
fn resolve(&mut self, name: &str) -> Result<Option<Tuple<SlicePtr>>> {
fn resolve(&self, name: &str) -> Result<Option<Tuple<SlicePtr>>> {
let mut tuple = Tuple::with_null_prefix();
tuple.push_str(name);
let it = self.txn.iterator(false, &self.temp_cf);

@ -0,0 +1,10 @@
use pest::iterators::Pair;
use crate::db::engine::Session;
use crate::parser::Rule;
use crate::relation::tuple::Tuple;
impl<'a> Session<'a> {
pub fn parse_table_def(&self, pair: Pair<Rule>) -> Tuple<Vec<u8>> {
todo!()
}
}

@ -23,6 +23,12 @@ pub enum CozoError {
//
#[error("Undefined type '{0}'")]
UndefinedType(String),
#[error("Undefined data kind {0}")]
UndefinedDataKind(u32),
#[error("Bad data format {0:?}")]
BadDataFormat(Vec<u8>),
//
// #[error("Undefined table")]
// UndefinedTable,
@ -57,7 +63,7 @@ pub enum CozoError {
#[error(transparent)]
Parse(#[from] pest::error::Error<Rule>),
// #[error(transparent)]
// #[error(transparent)]
// Storage(#[from] cozo_rocks::BridgeStatus),
//
// #[error(transparent)]

@ -136,7 +136,7 @@ key_marker = { "*" }
typing = _{ simple_type | nullable_type | homogeneous_list_type | unnamed_tuple_type | named_tuple_type }
simple_type = { ident }
nullable_type = { "?" ~ typing }
nullable_type = { "?" ~ (simple_type | homogeneous_list_type | unnamed_tuple_type | named_tuple_type) }
homogeneous_list_type = { "[" ~ typing ~ "]"}
unnamed_tuple_type = { "(" ~ typing ~ ("," ~ typing)* ~ ")" }
named_tuple_type = { "{" ~ named_type_pair ~ ("," ~ named_type_pair)* ~ "}" }

@ -2,4 +2,5 @@ pub mod tuple;
pub mod value;
pub mod key_order;
pub mod typing;
pub mod table;
pub mod table;
pub mod data;

@ -0,0 +1,43 @@
use std::borrow::Borrow;
use crate::relation::tuple::Tuple;
use crate::error::{CozoError, Result};
use crate::relation::typing::Typing;
use crate::relation::value::Value;
#[repr(u32)]
#[derive(Ord, PartialOrd, Eq, PartialEq)]
pub enum DataKind {
DataTuple = 0,
Node = 1,
Edge = 2,
Associate = 3,
Index = 4,
Value = 5,
TypeAlias = 6,
}
// In storage, key layout is `[0, name, stack_depth]` where stack_depth is a non-positive number as zigzag
// Also has inverted index `[0, stack_depth, name]` for easy popping of stacks
impl<T: AsRef<[u8]>> Tuple<T> {
pub fn data_kind(&self) -> Result<DataKind> {
use DataKind::*;
Ok(match self.get_prefix() {
0 => DataTuple,
1 => Node,
2 => Edge,
3 => Associate,
4 => Index,
5 => Value,
6 => TypeAlias,
v => return Err(CozoError::UndefinedDataKind(v))
})
}
pub fn interpret_as_type(&self) -> Result<Typing> {
if let Value::Text(s) = self.get(0).ok_or_else(|| CozoError::BadDataFormat(self.as_ref().to_vec()))? {
Ok(Typing::try_from(s.borrow())?)
} else {
return Err(CozoError::BadDataFormat(self.as_ref().to_vec()));
}
}
}

@ -1,34 +1,19 @@
use crate::relation::typing::Typing;
#[repr(u32)]
#[derive(Ord, PartialOrd, Eq, PartialEq)]
pub enum DataKind {
DataTuple = 0,
Node = 1,
Edge = 2,
Associate = 3,
Index = 4,
Value = 5,
TypeAlias = 6
}
// In storage, key layout is `[0, name, stack_depth]` where stack_depth is a non-positive number as zigzag
// Also has inverted index `[0, stack_depth, name]` for easy popping of stacks
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone)]
pub struct StorageId {
cf: String,
tid: u32,
pub cf: String,
pub tid: u32,
}
pub struct Column {
name: String,
typ: Typing,
pub name: String,
pub typ: Typing,
}
pub struct StoredRelation {
keys: Vec<Column>,
vals: Vec<Column>,
pub keys: Vec<Column>,
pub vals: Vec<Column>,
}
pub enum Table {

@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use uuid::Uuid;
use crate::relation::table::DataKind;
use crate::relation::data::DataKind;
use crate::relation::value::{Tag, Value};
#[derive(Clone)]

@ -4,9 +4,13 @@ use pest::iterators::Pair;
use crate::error::{Result, CozoError};
use crate::relation::value::Value;
use pest::Parser as PestParser;
use cozorocks::SlicePtr;
use crate::db::engine::Session;
use crate::db::eval::Environment;
use crate::parser::Parser;
use crate::parser::Rule;
use crate::parser::text_identifier::build_name_in_def;
use crate::relation::data::DataKind;
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Clone)]
@ -63,7 +67,7 @@ impl Typing {
}
impl Typing {
fn from_pair(pair: Pair<Rule>) -> Result<Self> {
fn from_pair<T: AsRef<[u8]>, E: Environment<T>>(pair: Pair<Rule>, env: Option<&E>) -> Result<Self> {
Ok(match pair.as_rule() {
Rule::simple_type => match pair.as_str() {
"Any" => Typing::Any,
@ -73,12 +77,24 @@ impl Typing {
"Text" => Typing::Text,
"Uuid" => Typing::Uuid,
"UInt" => Typing::UInt,
t => return Err(CozoError::UndefinedType(t.to_string()))
t => {
match env {
None => return Err(CozoError::UndefinedType(t.to_string())),
Some(env) => {
let resolved = env.resolve(t)?;
let resolved = resolved.ok_or_else(|| CozoError::UndefinedType(t.to_string()))?;
match resolved.data_kind()? {
DataKind::TypeAlias => resolved.interpret_as_type()?,
_ => return Err(CozoError::UndefinedType(t.to_string()))
}
}
}
}
},
Rule::nullable_type => Typing::Nullable(Box::new(Typing::from_pair(pair.into_inner().next().unwrap())?)),
Rule::homogeneous_list_type => Typing::Homogeneous(Box::new(Typing::from_pair(pair.into_inner().next().unwrap())?)),
Rule::nullable_type => Typing::Nullable(Box::new(Typing::from_pair(pair.into_inner().next().unwrap(), env)?)),
Rule::homogeneous_list_type => Typing::Homogeneous(Box::new(Typing::from_pair(pair.into_inner().next().unwrap(), env)?)),
Rule::unnamed_tuple_type => {
let types = pair.into_inner().map(|p| Typing::from_pair(p)).collect::<Result<Vec<Typing>>>()?;
let types = pair.into_inner().map(|p| Typing::from_pair(p, env)).collect::<Result<Vec<Typing>>>()?;
Typing::UnnamedTuple(types)
}
Rule::named_tuple_type => {
@ -87,7 +103,7 @@ impl Typing {
let name_pair = ps.next().unwrap();
let name = build_name_in_def(name_pair, true)?;
let typ_pair = ps.next().unwrap();
let typ = Typing::from_pair(typ_pair)?;
let typ = Typing::from_pair(typ_pair, env)?;
Ok((name, typ))
}).collect::<Result<Vec<(String, Typing)>>>()?;
Typing::NamedTuple(types)
@ -102,7 +118,7 @@ impl TryFrom<&str> for Typing {
fn try_from(value: &str) -> Result<Self> {
let pair = Parser::parse(Rule::typing, value)?.next().unwrap();
Typing::from_pair(pair)
Typing::from_pair::<SlicePtr, Session>(pair, None)
}
}
@ -133,5 +149,8 @@ mod tests {
let res: Result<Typing> = "?({x : Text, ppqp: ?UInt}, [Int], ?Uuid)".try_into();
println!("{:#?}", res);
assert!(res.is_ok());
let res: Result<Typing> = "??Int".try_into();
println!("{:#?}", res);
assert!(res.is_err());
}
}
Loading…
Cancel
Save