Skip to content

Commit

Permalink
feat(coqgnitarium): implement select at query engine level
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jun 5, 2023
1 parent 4b5c0ac commit 5a85ef3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions contracts/okp4-cognitarium/src/querier/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ impl<'a> QueryEngine<'a> {
Self { storage }
}

pub fn select(
&'a self,
plan: QueryPlan,
selection: Vec<SelectItem>,
) -> StdResult<SelectResponse> {
let bindings = selection
.iter()
.map(|item| match item {
SelectItem::Variable(v) => v,
})
.map(|name| -> StdResult<(String, usize)> {
match plan.get_var_index(name.as_str()) {
Some(index) => Ok((name.clone(), index)),
None => Err(StdError::generic_err(
"Selected variable not found in query",
)),
}
})
.collect::<StdResult<BTreeMap<String, usize>>>()?;

Ok(SelectResponse {
head: Head {
vars: bindings.keys().cloned().collect(),
},
results: Results {
bindings: SolutionsIterator::new(self.storage, self.eval_plan(plan), bindings)
.collect::<StdResult<Vec<BTreeMap<String, Value>>>>()?,
},
})
}

pub fn eval_plan(&'a self, plan: QueryPlan) -> ResolvedVariablesIterator {
return self.eval_node(plan.entrypoint)(ResolvedVariables::with_capacity(
plan.variables.len(),
Expand Down
11 changes: 11 additions & 0 deletions contracts/okp4-cognitarium/src/querier/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ pub struct QueryPlan {
pub variables: Vec<String>,
}

impl QueryPlan {
pub fn get_var_index(&self, var_name: &str) -> Option<usize> {
self.variables.iter().enumerate().find_map(|(index, it)| {
if it.as_str() == var_name {
return Some(index);
}
None
})
}
}

/// Represents a single part of the query plan processing. Each node is intended to provide a
/// specific behavior given an evaluation context.
pub enum QueryNode {
Expand Down

0 comments on commit 5a85ef3

Please sign in to comment.