Skip to content

Commit dd6953f

Browse files
committed
feat(cognitarium): resolve query node bound variables
1 parent 3a074be commit dd6953f

File tree

1 file changed

+42
-0
lines changed
  • contracts/okp4-cognitarium/src/querier

1 file changed

+42
-0
lines changed

contracts/okp4-cognitarium/src/querier/plan.rs

+42
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,49 @@ pub enum QueryNode {
4545
Limit { child: Self, first: usize },
4646
}
4747

48+
impl QueryNode {
49+
pub fn bound_variables(&self) -> BTreeSet<usize> {
50+
let mut vars = BTreeSet::new();
51+
self.lookup_bound_variables(&mut |v| {
52+
vars.insert(v);
53+
});
54+
vars
55+
}
56+
57+
pub fn lookup_bound_variables(&self, callback: &mut impl FnMut(usize)) {
58+
match self {
59+
QueryNode::TriplePattern {
60+
subject,
61+
predicate,
62+
object,
63+
} => {
64+
subject.lookup_bound_variable(callback);
65+
predicate.lookup_bound_variable(callback);
66+
object.lookup_bound_variable(callback);
67+
}
68+
QueryNode::CartesianProductJoin { left, right } => {
69+
left.lookup_bound_variables(callback);
70+
right.lookup_bound_variables(callback);
71+
}
72+
QueryNode::ForLoopJoin { left, right } => {
73+
left.lookup_bound_variables(callback);
74+
right.lookup_bound_variables(callback);
75+
}
76+
QueryNode::Skip { child, .. } => child.lookup_bound_variables(callback),
77+
QueryNode::Limit { child, .. } => child.lookup_bound_variables(callback),
78+
}
79+
}
80+
}
81+
4882
pub enum PatternValue<V> {
4983
Constant(V),
5084
Variable(usize),
5185
}
86+
87+
impl<V> PatternValue<V> {
88+
pub fn lookup_bound_variable(&self, callback: &mut impl FnMut(usize)) {
89+
if let PatternValue::Variable(v) = self {
90+
callback(*v);
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)