|
|
@ -37,7 +37,10 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
.build_for_graph_pattern( |
|
|
|
.build_for_graph_pattern( |
|
|
|
pattern, |
|
|
|
pattern, |
|
|
|
&mut variables, |
|
|
|
&mut variables, |
|
|
|
&PatternValue::Constant(EncodedTerm::DefaultGraph), |
|
|
|
&PatternValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: EncodedTerm::DefaultGraph, |
|
|
|
|
|
|
|
plain: PatternValueConstant::DefaultGraph, |
|
|
|
|
|
|
|
}), |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
let plan = if !without_optimizations && !is_cardinality_meaningful { |
|
|
|
let plan = if !without_optimizations && !is_cardinality_meaningful { |
|
|
|
// let's reduce downstream task.
|
|
|
|
// let's reduce downstream task.
|
|
|
@ -125,13 +128,13 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
PlanNode::HashLeftJoin { |
|
|
|
PlanNode::HashLeftJoin { |
|
|
|
left: Box::new(left), |
|
|
|
left: Box::new(left), |
|
|
|
right: Box::new(right), |
|
|
|
right: Box::new(right), |
|
|
|
expression: Box::new( |
|
|
|
expression: Box::new(expression.as_ref().map_or( |
|
|
|
expression |
|
|
|
Ok(PlanExpression::Literal(PlanTerm { |
|
|
|
.as_ref() |
|
|
|
encoded: true.into(), |
|
|
|
.map_or(Ok(PlanExpression::Constant(true.into())), |e| { |
|
|
|
plain: true.into(), |
|
|
|
self.build_for_expression(e, variables, graph_name) |
|
|
|
})), |
|
|
|
})?, |
|
|
|
|e| self.build_for_expression(e, variables, graph_name), |
|
|
|
), |
|
|
|
)?), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -171,7 +174,7 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
expression, |
|
|
|
expression, |
|
|
|
} => PlanNode::Extend { |
|
|
|
} => PlanNode::Extend { |
|
|
|
child: Box::new(self.build_for_graph_pattern(inner, variables, graph_name)?), |
|
|
|
child: Box::new(self.build_for_graph_pattern(inner, variables, graph_name)?), |
|
|
|
position: variable_key(variables, variable), |
|
|
|
variable: build_plan_variable(variables, variable), |
|
|
|
expression: Box::new(self.build_for_expression(expression, variables, graph_name)?), |
|
|
|
expression: Box::new(self.build_for_expression(expression, variables, graph_name)?), |
|
|
|
}, |
|
|
|
}, |
|
|
|
GraphPattern::Minus { left, right } => PlanNode::AntiJoin { |
|
|
|
GraphPattern::Minus { left, right } => PlanNode::AntiJoin { |
|
|
@ -200,14 +203,18 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
aggregates, |
|
|
|
aggregates, |
|
|
|
} => PlanNode::Aggregate { |
|
|
|
} => PlanNode::Aggregate { |
|
|
|
child: Box::new(self.build_for_graph_pattern(inner, variables, graph_name)?), |
|
|
|
child: Box::new(self.build_for_graph_pattern(inner, variables, graph_name)?), |
|
|
|
key_variables: Rc::new(by.iter().map(|k| variable_key(variables, k)).collect()), |
|
|
|
key_variables: Rc::new( |
|
|
|
|
|
|
|
by.iter() |
|
|
|
|
|
|
|
.map(|k| build_plan_variable(variables, k)) |
|
|
|
|
|
|
|
.collect(), |
|
|
|
|
|
|
|
), |
|
|
|
aggregates: Rc::new( |
|
|
|
aggregates: Rc::new( |
|
|
|
aggregates |
|
|
|
aggregates |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.map(|(v, a)| { |
|
|
|
.map(|(v, a)| { |
|
|
|
Ok(( |
|
|
|
Ok(( |
|
|
|
self.build_for_aggregate(a, variables, graph_name)?, |
|
|
|
self.build_for_aggregate(a, variables, graph_name)?, |
|
|
|
variable_key(variables, v), |
|
|
|
build_plan_variable(variables, v), |
|
|
|
)) |
|
|
|
)) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.collect::<Result<Vec<_>, EvaluationError>>()?, |
|
|
|
.collect::<Result<Vec<_>, EvaluationError>>()?, |
|
|
@ -216,9 +223,36 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
GraphPattern::Values { |
|
|
|
GraphPattern::Values { |
|
|
|
variables: table_variables, |
|
|
|
variables: table_variables, |
|
|
|
bindings, |
|
|
|
bindings, |
|
|
|
} => PlanNode::StaticBindings { |
|
|
|
} => { |
|
|
|
tuples: self.encode_bindings(table_variables, bindings, variables), |
|
|
|
let bindings_variables = table_variables |
|
|
|
}, |
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|v| build_plan_variable(variables, v)) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
let encoded_tuples = bindings |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|row| { |
|
|
|
|
|
|
|
let mut result = EncodedTuple::with_capacity(variables.len()); |
|
|
|
|
|
|
|
for (key, value) in row.iter().enumerate() { |
|
|
|
|
|
|
|
if let Some(term) = value { |
|
|
|
|
|
|
|
result.set( |
|
|
|
|
|
|
|
bindings_variables[key].encoded, |
|
|
|
|
|
|
|
match term { |
|
|
|
|
|
|
|
GroundTerm::NamedNode(node) => self.build_term(node), |
|
|
|
|
|
|
|
GroundTerm::Literal(literal) => self.build_term(literal), |
|
|
|
|
|
|
|
GroundTerm::Triple(triple) => self.build_triple(triple), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
result |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
PlanNode::StaticBindings { |
|
|
|
|
|
|
|
encoded_tuples, |
|
|
|
|
|
|
|
variables: bindings_variables, |
|
|
|
|
|
|
|
plain_bindings: bindings.clone(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
GraphPattern::OrderBy { inner, expression } => { |
|
|
|
GraphPattern::OrderBy { inner, expression } => { |
|
|
|
let condition: Result<Vec<_>, EvaluationError> = expression |
|
|
|
let condition: Result<Vec<_>, EvaluationError> = expression |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
@ -242,7 +276,7 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
let mut inner_variables = projection.clone(); |
|
|
|
let mut inner_variables = projection.clone(); |
|
|
|
let inner_graph_name = |
|
|
|
let inner_graph_name = |
|
|
|
Self::convert_pattern_value_id(graph_name, variables, &mut inner_variables); |
|
|
|
Self::convert_pattern_value_id(graph_name, &mut inner_variables); |
|
|
|
PlanNode::Project { |
|
|
|
PlanNode::Project { |
|
|
|
child: Box::new(self.build_for_graph_pattern( |
|
|
|
child: Box::new(self.build_for_graph_pattern( |
|
|
|
inner, |
|
|
|
inner, |
|
|
@ -254,7 +288,13 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.enumerate() |
|
|
|
.enumerate() |
|
|
|
.map(|(new_variable, variable)| { |
|
|
|
.map(|(new_variable, variable)| { |
|
|
|
(new_variable, variable_key(variables, variable)) |
|
|
|
( |
|
|
|
|
|
|
|
PlanVariable { |
|
|
|
|
|
|
|
encoded: new_variable, |
|
|
|
|
|
|
|
plain: variable.clone(), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
build_plan_variable(variables, variable), |
|
|
|
|
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.collect(), |
|
|
|
.collect(), |
|
|
|
), |
|
|
|
), |
|
|
@ -306,13 +346,18 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
}) |
|
|
|
}) |
|
|
|
.reduce(|a, b| self.new_join(a, b)) |
|
|
|
.reduce(|a, b| self.new_join(a, b)) |
|
|
|
.unwrap_or_else(|| PlanNode::StaticBindings { |
|
|
|
.unwrap_or_else(|| PlanNode::StaticBindings { |
|
|
|
tuples: vec![EncodedTuple::with_capacity(variables.len())], |
|
|
|
encoded_tuples: vec![EncodedTuple::with_capacity(variables.len())], |
|
|
|
|
|
|
|
variables: Vec::new(), |
|
|
|
|
|
|
|
plain_bindings: vec![Vec::new()], |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn build_for_path(&self, path: &PropertyPathExpression) -> PlanPropertyPath { |
|
|
|
fn build_for_path(&self, path: &PropertyPathExpression) -> PlanPropertyPath { |
|
|
|
match path { |
|
|
|
match path { |
|
|
|
PropertyPathExpression::NamedNode(p) => PlanPropertyPath::Path(self.build_term(p)), |
|
|
|
PropertyPathExpression::NamedNode(p) => PlanPropertyPath::Path(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(p), |
|
|
|
|
|
|
|
plain: p.clone(), |
|
|
|
|
|
|
|
}), |
|
|
|
PropertyPathExpression::Reverse(p) => { |
|
|
|
PropertyPathExpression::Reverse(p) => { |
|
|
|
PlanPropertyPath::Reverse(Rc::new(self.build_for_path(p))) |
|
|
|
PlanPropertyPath::Reverse(Rc::new(self.build_for_path(p))) |
|
|
|
} |
|
|
|
} |
|
|
@ -333,9 +378,16 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
PropertyPathExpression::ZeroOrOne(p) => { |
|
|
|
PropertyPathExpression::ZeroOrOne(p) => { |
|
|
|
PlanPropertyPath::ZeroOrOne(Rc::new(self.build_for_path(p))) |
|
|
|
PlanPropertyPath::ZeroOrOne(Rc::new(self.build_for_path(p))) |
|
|
|
} |
|
|
|
} |
|
|
|
PropertyPathExpression::NegatedPropertySet(p) => PlanPropertyPath::NegatedPropertySet( |
|
|
|
PropertyPathExpression::NegatedPropertySet(p) => { |
|
|
|
Rc::new(p.iter().map(|p| self.build_term(p)).collect()), |
|
|
|
PlanPropertyPath::NegatedPropertySet(Rc::new( |
|
|
|
), |
|
|
|
p.iter() |
|
|
|
|
|
|
|
.map(|p| PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(p), |
|
|
|
|
|
|
|
plain: p.clone(), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.collect(), |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -346,9 +398,15 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
graph_name: &PatternValue, |
|
|
|
graph_name: &PatternValue, |
|
|
|
) -> Result<PlanExpression, EvaluationError> { |
|
|
|
) -> Result<PlanExpression, EvaluationError> { |
|
|
|
Ok(match expression { |
|
|
|
Ok(match expression { |
|
|
|
Expression::NamedNode(node) => PlanExpression::Constant(self.build_term(node)), |
|
|
|
Expression::NamedNode(node) => PlanExpression::NamedNode(PlanTerm { |
|
|
|
Expression::Literal(l) => PlanExpression::Constant(self.build_term(l)), |
|
|
|
encoded: self.build_term(node), |
|
|
|
Expression::Variable(v) => PlanExpression::Variable(variable_key(variables, v)), |
|
|
|
plain: node.clone(), |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
Expression::Literal(l) => PlanExpression::Literal(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(l), |
|
|
|
|
|
|
|
plain: l.clone(), |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
Expression::Variable(v) => PlanExpression::Variable(build_plan_variable(variables, v)), |
|
|
|
Expression::Or(a, b) => PlanExpression::Or( |
|
|
|
Expression::Or(a, b) => PlanExpression::Or( |
|
|
|
Box::new(self.build_for_expression(a, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(a, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(b, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(b, variables, graph_name)?), |
|
|
@ -393,7 +451,12 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
.reduce(|a: Result<_, EvaluationError>, b| { |
|
|
|
.reduce(|a: Result<_, EvaluationError>, b| { |
|
|
|
Ok(PlanExpression::Or(Box::new(a?), Box::new(b?))) |
|
|
|
Ok(PlanExpression::Or(Box::new(a?), Box::new(b?))) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.unwrap_or_else(|| Ok(PlanExpression::Constant(false.into())))? |
|
|
|
.unwrap_or_else(|| { |
|
|
|
|
|
|
|
Ok(PlanExpression::Literal(PlanTerm { |
|
|
|
|
|
|
|
encoded: false.into(), |
|
|
|
|
|
|
|
plain: false.into(), |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
})? |
|
|
|
} |
|
|
|
} |
|
|
|
Expression::Add(a, b) => PlanExpression::Add( |
|
|
|
Expression::Add(a, b) => PlanExpression::Add( |
|
|
|
Box::new(self.build_for_expression(a, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(a, variables, graph_name)?), |
|
|
@ -824,7 +887,7 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
Expression::Bound(v) => PlanExpression::Bound(variable_key(variables, v)), |
|
|
|
Expression::Bound(v) => PlanExpression::Bound(build_plan_variable(variables, v)), |
|
|
|
Expression::If(a, b, c) => PlanExpression::If( |
|
|
|
Expression::If(a, b, c) => PlanExpression::If( |
|
|
|
Box::new(self.build_for_expression(a, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(a, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(b, variables, graph_name)?), |
|
|
|
Box::new(self.build_for_expression(b, variables, graph_name)?), |
|
|
@ -883,17 +946,23 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
) -> PatternValue { |
|
|
|
) -> PatternValue { |
|
|
|
match term_or_variable { |
|
|
|
match term_or_variable { |
|
|
|
TermPattern::Variable(variable) => { |
|
|
|
TermPattern::Variable(variable) => { |
|
|
|
PatternValue::Variable(variable_key(variables, variable)) |
|
|
|
PatternValue::Variable(build_plan_variable(variables, variable)) |
|
|
|
} |
|
|
|
} |
|
|
|
TermPattern::NamedNode(node) => PatternValue::Constant(self.build_term(node)), |
|
|
|
TermPattern::NamedNode(node) => PatternValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(node), |
|
|
|
|
|
|
|
plain: PatternValueConstant::NamedNode(node.clone()), |
|
|
|
|
|
|
|
}), |
|
|
|
TermPattern::BlankNode(bnode) => { |
|
|
|
TermPattern::BlankNode(bnode) => { |
|
|
|
PatternValue::Variable(variable_key( |
|
|
|
PatternValue::Variable(build_plan_variable( |
|
|
|
variables, |
|
|
|
variables, |
|
|
|
&Variable::new_unchecked(bnode.as_str()), |
|
|
|
&Variable::new_unchecked(bnode.as_str()), |
|
|
|
)) |
|
|
|
)) |
|
|
|
//TODO: very bad hack to convert bnode to variable
|
|
|
|
//TODO: very bad hack to convert bnode to variable
|
|
|
|
} |
|
|
|
} |
|
|
|
TermPattern::Literal(literal) => PatternValue::Constant(self.build_term(literal)), |
|
|
|
TermPattern::Literal(literal) => PatternValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(literal), |
|
|
|
|
|
|
|
plain: PatternValueConstant::Literal(literal.clone()), |
|
|
|
|
|
|
|
}), |
|
|
|
TermPattern::Triple(triple) => { |
|
|
|
TermPattern::Triple(triple) => { |
|
|
|
match ( |
|
|
|
match ( |
|
|
|
self.pattern_value_from_term_or_variable(&triple.subject, variables), |
|
|
|
self.pattern_value_from_term_or_variable(&triple.subject, variables), |
|
|
@ -901,19 +970,48 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
self.pattern_value_from_term_or_variable(&triple.object, variables), |
|
|
|
self.pattern_value_from_term_or_variable(&triple.object, variables), |
|
|
|
) { |
|
|
|
) { |
|
|
|
( |
|
|
|
( |
|
|
|
PatternValue::Constant(subject), |
|
|
|
PatternValue::Constant(PlanTerm { |
|
|
|
PatternValue::Constant(predicate), |
|
|
|
encoded: encoded_subject, |
|
|
|
PatternValue::Constant(object), |
|
|
|
plain: plain_subject, |
|
|
|
) => PatternValue::Constant( |
|
|
|
}), |
|
|
|
EncodedTriple { |
|
|
|
PatternValue::Constant(PlanTerm { |
|
|
|
subject, |
|
|
|
encoded: encoded_predicate, |
|
|
|
predicate, |
|
|
|
plain: plain_predicate, |
|
|
|
object, |
|
|
|
}), |
|
|
|
|
|
|
|
PatternValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: encoded_object, |
|
|
|
|
|
|
|
plain: plain_object, |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
) => PatternValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: EncodedTriple { |
|
|
|
|
|
|
|
subject: encoded_subject, |
|
|
|
|
|
|
|
predicate: encoded_predicate, |
|
|
|
|
|
|
|
object: encoded_object, |
|
|
|
} |
|
|
|
} |
|
|
|
.into(), |
|
|
|
.into(), |
|
|
|
), |
|
|
|
plain: PatternValueConstant::Triple(Box::new(Triple { |
|
|
|
|
|
|
|
subject: match plain_subject { |
|
|
|
|
|
|
|
PatternValueConstant::NamedNode(s) => s.into(), |
|
|
|
|
|
|
|
PatternValueConstant::Triple(s) => s.into(), |
|
|
|
|
|
|
|
PatternValueConstant::Literal(_) |
|
|
|
|
|
|
|
| PatternValueConstant::DefaultGraph => unreachable!(), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
predicate: match plain_predicate { |
|
|
|
|
|
|
|
PatternValueConstant::NamedNode(s) => s, |
|
|
|
|
|
|
|
PatternValueConstant::Literal(_) |
|
|
|
|
|
|
|
| PatternValueConstant::Triple(_) |
|
|
|
|
|
|
|
| PatternValueConstant::DefaultGraph => unreachable!(), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
object: match plain_object { |
|
|
|
|
|
|
|
PatternValueConstant::NamedNode(s) => s.into(), |
|
|
|
|
|
|
|
PatternValueConstant::Literal(s) => s.into(), |
|
|
|
|
|
|
|
PatternValueConstant::Triple(s) => s.into(), |
|
|
|
|
|
|
|
PatternValueConstant::DefaultGraph => unreachable!(), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
})), |
|
|
|
|
|
|
|
}), |
|
|
|
(subject, predicate, object) => { |
|
|
|
(subject, predicate, object) => { |
|
|
|
PatternValue::Triple(Box::new(TriplePatternValue { |
|
|
|
PatternValue::TriplePattern(Box::new(TriplePatternValue { |
|
|
|
subject, |
|
|
|
subject, |
|
|
|
predicate, |
|
|
|
predicate, |
|
|
|
object, |
|
|
|
object, |
|
|
@ -930,45 +1028,16 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
variables: &mut Vec<Variable>, |
|
|
|
variables: &mut Vec<Variable>, |
|
|
|
) -> PatternValue { |
|
|
|
) -> PatternValue { |
|
|
|
match named_node_or_variable { |
|
|
|
match named_node_or_variable { |
|
|
|
NamedNodePattern::NamedNode(named_node) => { |
|
|
|
NamedNodePattern::NamedNode(named_node) => PatternValue::Constant(PlanTerm { |
|
|
|
PatternValue::Constant(self.build_term(named_node)) |
|
|
|
encoded: self.build_term(named_node), |
|
|
|
} |
|
|
|
plain: PatternValueConstant::NamedNode(named_node.clone()), |
|
|
|
|
|
|
|
}), |
|
|
|
NamedNodePattern::Variable(variable) => { |
|
|
|
NamedNodePattern::Variable(variable) => { |
|
|
|
PatternValue::Variable(variable_key(variables, variable)) |
|
|
|
PatternValue::Variable(build_plan_variable(variables, variable)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn encode_bindings( |
|
|
|
|
|
|
|
&self, |
|
|
|
|
|
|
|
table_variables: &[Variable], |
|
|
|
|
|
|
|
rows: &[Vec<Option<GroundTerm>>], |
|
|
|
|
|
|
|
variables: &mut Vec<Variable>, |
|
|
|
|
|
|
|
) -> Vec<EncodedTuple> { |
|
|
|
|
|
|
|
let bindings_variables_keys = table_variables |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|v| variable_key(variables, v)) |
|
|
|
|
|
|
|
.collect::<Vec<_>>(); |
|
|
|
|
|
|
|
rows.iter() |
|
|
|
|
|
|
|
.map(move |row| { |
|
|
|
|
|
|
|
let mut result = EncodedTuple::with_capacity(variables.len()); |
|
|
|
|
|
|
|
for (key, value) in row.iter().enumerate() { |
|
|
|
|
|
|
|
if let Some(term) = value { |
|
|
|
|
|
|
|
result.set( |
|
|
|
|
|
|
|
bindings_variables_keys[key], |
|
|
|
|
|
|
|
match term { |
|
|
|
|
|
|
|
GroundTerm::NamedNode(node) => self.build_term(node), |
|
|
|
|
|
|
|
GroundTerm::Literal(literal) => self.build_term(literal), |
|
|
|
|
|
|
|
GroundTerm::Triple(triple) => self.build_triple(triple), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
result |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.collect() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn build_for_aggregate( |
|
|
|
fn build_for_aggregate( |
|
|
|
&self, |
|
|
|
&self, |
|
|
|
aggregate: &AggregateExpression, |
|
|
|
aggregate: &AggregateExpression, |
|
|
@ -1059,15 +1128,20 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
) -> TripleTemplateValue { |
|
|
|
) -> TripleTemplateValue { |
|
|
|
match term_or_variable { |
|
|
|
match term_or_variable { |
|
|
|
TermPattern::Variable(variable) => { |
|
|
|
TermPattern::Variable(variable) => { |
|
|
|
TripleTemplateValue::Variable(variable_key(variables, variable)) |
|
|
|
TripleTemplateValue::Variable(build_plan_variable(variables, variable)) |
|
|
|
} |
|
|
|
|
|
|
|
TermPattern::NamedNode(node) => TripleTemplateValue::Constant(self.build_term(node)), |
|
|
|
|
|
|
|
TermPattern::BlankNode(bnode) => { |
|
|
|
|
|
|
|
TripleTemplateValue::BlankNode(bnode_key(bnodes, bnode)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
TermPattern::Literal(literal) => { |
|
|
|
|
|
|
|
TripleTemplateValue::Constant(self.build_term(literal)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
TermPattern::NamedNode(node) => TripleTemplateValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(node), |
|
|
|
|
|
|
|
plain: node.clone().into(), |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
TermPattern::BlankNode(bnode) => TripleTemplateValue::BlankNode(PlanVariable { |
|
|
|
|
|
|
|
encoded: bnode_key(bnodes, bnode), |
|
|
|
|
|
|
|
plain: bnode.clone(), |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
TermPattern::Literal(literal) => TripleTemplateValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(literal), |
|
|
|
|
|
|
|
plain: literal.clone().into(), |
|
|
|
|
|
|
|
}), |
|
|
|
TermPattern::Triple(triple) => match ( |
|
|
|
TermPattern::Triple(triple) => match ( |
|
|
|
self.template_value_from_term_or_variable(&triple.subject, variables, bnodes), |
|
|
|
self.template_value_from_term_or_variable(&triple.subject, variables, bnodes), |
|
|
|
self.template_value_from_named_node_or_variable(&triple.predicate, variables), |
|
|
|
self.template_value_from_named_node_or_variable(&triple.predicate, variables), |
|
|
@ -1077,14 +1151,30 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
TripleTemplateValue::Constant(subject), |
|
|
|
TripleTemplateValue::Constant(subject), |
|
|
|
TripleTemplateValue::Constant(predicate), |
|
|
|
TripleTemplateValue::Constant(predicate), |
|
|
|
TripleTemplateValue::Constant(object), |
|
|
|
TripleTemplateValue::Constant(object), |
|
|
|
) => TripleTemplateValue::Constant( |
|
|
|
) => TripleTemplateValue::Constant(PlanTerm { |
|
|
|
EncodedTriple { |
|
|
|
encoded: EncodedTriple { |
|
|
|
subject, |
|
|
|
subject: subject.encoded, |
|
|
|
predicate, |
|
|
|
predicate: predicate.encoded, |
|
|
|
object, |
|
|
|
object: object.encoded, |
|
|
|
} |
|
|
|
} |
|
|
|
.into(), |
|
|
|
.into(), |
|
|
|
), |
|
|
|
plain: Triple { |
|
|
|
|
|
|
|
subject: match subject.plain { |
|
|
|
|
|
|
|
Term::NamedNode(node) => node.into(), |
|
|
|
|
|
|
|
Term::BlankNode(node) => node.into(), |
|
|
|
|
|
|
|
Term::Literal(_) => unreachable!(), |
|
|
|
|
|
|
|
Term::Triple(node) => node.into(), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
predicate: match predicate.plain { |
|
|
|
|
|
|
|
Term::NamedNode(node) => node, |
|
|
|
|
|
|
|
Term::BlankNode(_) | Term::Literal(_) | Term::Triple(_) => { |
|
|
|
|
|
|
|
unreachable!() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
object: object.plain, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
.into(), |
|
|
|
|
|
|
|
}), |
|
|
|
(subject, predicate, object) => { |
|
|
|
(subject, predicate, object) => { |
|
|
|
TripleTemplateValue::Triple(Box::new(TripleTemplate { |
|
|
|
TripleTemplateValue::Triple(Box::new(TripleTemplate { |
|
|
|
subject, |
|
|
|
subject, |
|
|
@ -1103,35 +1193,34 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
) -> TripleTemplateValue { |
|
|
|
) -> TripleTemplateValue { |
|
|
|
match named_node_or_variable { |
|
|
|
match named_node_or_variable { |
|
|
|
NamedNodePattern::Variable(variable) => { |
|
|
|
NamedNodePattern::Variable(variable) => { |
|
|
|
TripleTemplateValue::Variable(variable_key(variables, variable)) |
|
|
|
TripleTemplateValue::Variable(build_plan_variable(variables, variable)) |
|
|
|
} |
|
|
|
|
|
|
|
NamedNodePattern::NamedNode(term) => { |
|
|
|
|
|
|
|
TripleTemplateValue::Constant(self.build_term(term)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
NamedNodePattern::NamedNode(term) => TripleTemplateValue::Constant(PlanTerm { |
|
|
|
|
|
|
|
encoded: self.build_term(term), |
|
|
|
|
|
|
|
plain: term.clone().into(), |
|
|
|
|
|
|
|
}), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn convert_pattern_value_id( |
|
|
|
fn convert_pattern_value_id(from_value: &PatternValue, to: &mut Vec<Variable>) -> PatternValue { |
|
|
|
from_value: &PatternValue, |
|
|
|
|
|
|
|
from: &[Variable], |
|
|
|
|
|
|
|
to: &mut Vec<Variable>, |
|
|
|
|
|
|
|
) -> PatternValue { |
|
|
|
|
|
|
|
match from_value { |
|
|
|
match from_value { |
|
|
|
PatternValue::Constant(v) => PatternValue::Constant(v.clone()), |
|
|
|
PatternValue::Constant(c) => PatternValue::Constant(c.clone()), |
|
|
|
PatternValue::Variable(from_id) => { |
|
|
|
PatternValue::Variable(from_id) => { |
|
|
|
PatternValue::Variable(Self::convert_variable_id(*from_id, from, to)) |
|
|
|
PatternValue::Variable(Self::convert_plan_variable(from_id, to)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
PatternValue::TriplePattern(triple) => { |
|
|
|
|
|
|
|
PatternValue::TriplePattern(Box::new(TriplePatternValue { |
|
|
|
|
|
|
|
subject: Self::convert_pattern_value_id(&triple.subject, to), |
|
|
|
|
|
|
|
predicate: Self::convert_pattern_value_id(&triple.predicate, to), |
|
|
|
|
|
|
|
object: Self::convert_pattern_value_id(&triple.object, to), |
|
|
|
|
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
PatternValue::Triple(triple) => PatternValue::Triple(Box::new(TriplePatternValue { |
|
|
|
|
|
|
|
subject: Self::convert_pattern_value_id(&triple.subject, from, to), |
|
|
|
|
|
|
|
predicate: Self::convert_pattern_value_id(&triple.predicate, from, to), |
|
|
|
|
|
|
|
object: Self::convert_pattern_value_id(&triple.object, from, to), |
|
|
|
|
|
|
|
})), |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn convert_variable_id(from_id: usize, from: &[Variable], to: &mut Vec<Variable>) -> usize { |
|
|
|
fn convert_plan_variable(from_variable: &PlanVariable, to: &mut Vec<Variable>) -> PlanVariable { |
|
|
|
if let Some(to_id) = to.iter().enumerate().find_map(|(to_id, var)| { |
|
|
|
let encoded = if let Some(to_id) = to.iter().enumerate().find_map(|(to_id, var)| { |
|
|
|
if *var == from[from_id] { |
|
|
|
if *var == from_variable.plain { |
|
|
|
Some(to_id) |
|
|
|
Some(to_id) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
None |
|
|
|
None |
|
|
@ -1141,6 +1230,10 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
to.push(Variable::new_unchecked(format!("{:x}", random::<u128>()))); |
|
|
|
to.push(Variable::new_unchecked(format!("{:x}", random::<u128>()))); |
|
|
|
to.len() - 1 |
|
|
|
to.len() - 1 |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
PlanVariable { |
|
|
|
|
|
|
|
encoded, |
|
|
|
|
|
|
|
plain: from_variable.plain.clone(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1228,8 +1321,8 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
let mut child_bound = BTreeSet::new(); |
|
|
|
let mut child_bound = BTreeSet::new(); |
|
|
|
Self::add_left_join_problematic_variables(child, &mut child_bound); |
|
|
|
Self::add_left_join_problematic_variables(child, &mut child_bound); |
|
|
|
for (child_i, output_i) in mapping.iter() { |
|
|
|
for (child_i, output_i) in mapping.iter() { |
|
|
|
if child_bound.contains(child_i) { |
|
|
|
if child_bound.contains(&child_i.encoded) { |
|
|
|
set.insert(*output_i); |
|
|
|
set.insert(output_i.encoded); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1238,10 +1331,10 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
aggregates, |
|
|
|
aggregates, |
|
|
|
.. |
|
|
|
.. |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
set.extend(key_variables.iter()); |
|
|
|
set.extend(key_variables.iter().map(|v| v.encoded)); |
|
|
|
//TODO: This is too harsh
|
|
|
|
//TODO: This is too harsh
|
|
|
|
for (_, var) in aggregates.iter() { |
|
|
|
for (_, var) in aggregates.iter() { |
|
|
|
set.insert(*var); |
|
|
|
set.insert(var.encoded); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -1371,21 +1464,21 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
PlanNode::Extend { |
|
|
|
PlanNode::Extend { |
|
|
|
child, |
|
|
|
child, |
|
|
|
expression, |
|
|
|
expression, |
|
|
|
position, |
|
|
|
variable: position, |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
//TODO: handle the case where the filter generates an expression variable
|
|
|
|
//TODO: handle the case where the filter generates an expression variable
|
|
|
|
if filter_variables.iter().all(|v| child.is_variable_bound(*v)) { |
|
|
|
if filter_variables.iter().all(|v| child.is_variable_bound(*v)) { |
|
|
|
PlanNode::Extend { |
|
|
|
PlanNode::Extend { |
|
|
|
child: Box::new(self.push_filter(child, filter)), |
|
|
|
child: Box::new(self.push_filter(child, filter)), |
|
|
|
expression, |
|
|
|
expression, |
|
|
|
position, |
|
|
|
variable: position, |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
PlanNode::Filter { |
|
|
|
PlanNode::Filter { |
|
|
|
child: Box::new(PlanNode::Extend { |
|
|
|
child: Box::new(PlanNode::Extend { |
|
|
|
child, |
|
|
|
child, |
|
|
|
expression, |
|
|
|
expression, |
|
|
|
position, |
|
|
|
variable: position, |
|
|
|
}), |
|
|
|
}), |
|
|
|
expression: filter, |
|
|
|
expression: filter, |
|
|
|
} |
|
|
|
} |
|
|
@ -1439,13 +1532,17 @@ impl<'a> PlanBuilder<'a> { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn variable_key(variables: &mut Vec<Variable>, variable: &Variable) -> usize { |
|
|
|
fn build_plan_variable(variables: &mut Vec<Variable>, variable: &Variable) -> PlanVariable { |
|
|
|
match slice_key(variables, variable) { |
|
|
|
let encoded = match slice_key(variables, variable) { |
|
|
|
Some(key) => key, |
|
|
|
Some(key) => key, |
|
|
|
None => { |
|
|
|
None => { |
|
|
|
variables.push(variable.clone()); |
|
|
|
variables.push(variable.clone()); |
|
|
|
variables.len() - 1 |
|
|
|
variables.len() - 1 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
PlanVariable { |
|
|
|
|
|
|
|
plain: variable.clone(), |
|
|
|
|
|
|
|
encoded, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|