1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//! Conformance Operations
//!
//! Implementations for SPARQL algebra operators needed for SPARQL 1.1 conformance:
//! - MINUS (set difference)
//! - EXTEND (BIND)
//! - VALUES (inline data)
//! - Property paths (evaluation via path module)
use crate::algebra::{Algebra, Binding, Solution, Term, Variable};
use anyhow::Result;
use std::collections::{HashSet, VecDeque};
use super::dataset::{convert_property_path, Dataset, DatasetPathAdapter};
use super::queryexecutor_type::QueryExecutor;
impl QueryExecutor {
/// Execute a MINUS pattern
///
/// SPARQL MINUS removes solutions from P1 that are compatible with any
/// solution in P2 (based on shared variables).
pub fn execute_minus(
&self,
left: &Algebra,
right: &Algebra,
dataset: &dyn Dataset,
) -> Result<Solution> {
let left_solutions = self.execute_serial(left, dataset)?;
let right_solutions = self.execute_serial(right, dataset)?;
if right_solutions.is_empty() {
return Ok(left_solutions);
}
let mut result = Solution::new();
for left_binding in &left_solutions {
let left_vars: HashSet<&Variable> = left_binding.keys().collect();
let mut should_remove = false;
for right_binding in &right_solutions {
let right_vars: HashSet<&Variable> = right_binding.keys().collect();
let shared_vars: Vec<&&Variable> = left_vars.intersection(&right_vars).collect();
if shared_vars.is_empty() {
// Disjoint variables: SPARQL spec says MINUS removes nothing
continue;
}
// Check compatibility on shared variables
let mut compatible = true;
for shared_var in &shared_vars {
let left_val = left_binding.get(**shared_var);
let right_val = right_binding.get(**shared_var);
if left_val != right_val {
compatible = false;
break;
}
}
if compatible {
should_remove = true;
break;
}
}
if !should_remove {
result.push(left_binding.clone());
}
}
Ok(result)
}
/// Execute an EXTEND (BIND) pattern
///
/// Extends each solution with a new variable binding computed by evaluating expr.
pub fn execute_extend(
&self,
pattern: &Algebra,
variable: &Variable,
expr: &crate::algebra::Expression,
dataset: &dyn Dataset,
) -> Result<Solution> {
let pattern_solutions = self.execute_serial(pattern, dataset)?;
let mut result = Solution::new();
for mut binding in pattern_solutions {
match self.evaluate_expression(expr, &binding) {
Ok(value) => {
binding.insert(variable.clone(), value);
}
Err(_) => {
// In SPARQL, BIND with an error: the binding is still included
// but the variable is unbound (we just don't add it)
}
}
result.push(binding);
}
Ok(result)
}
/// Execute a VALUES clause
///
/// Returns the inline data as solutions.
pub fn execute_values(
&self,
_variables: &[Variable],
bindings: &[Binding],
) -> Result<Solution> {
Ok(bindings.to_vec())
}
/// Execute a property path pattern
///
/// Evaluates property paths against the dataset using BFS/DFS traversal.
pub fn execute_property_path(
&self,
subject: &Term,
path: &crate::algebra::PropertyPath,
object: &Term,
dataset: &dyn Dataset,
) -> Result<Solution> {
let adapter = DatasetPathAdapter::new(dataset);
let path_pp = convert_property_path(path)?;
let mut result = Solution::new();
match (subject, object) {
(Term::Variable(s_var), Term::Variable(o_var)) => {
// Both variable: enumerate all subjects AND objects as potential starting nodes.
// This is needed for paths like inverse (^pred) where the "subjects" in path
// evaluation are the objects in the graph, and vice versa.
let mut candidates: HashSet<Term> = HashSet::new();
for s in dataset.subjects()? {
candidates.insert(s);
}
for o in dataset.objects()? {
candidates.insert(o);
}
let mut seen_pairs: HashSet<(Term, Term)> = HashSet::new();
for s in candidates {
let reachable = evaluate_path_from(&path_pp, &s, &adapter)?;
for o in reachable {
let pair = (s.clone(), o.clone());
if seen_pairs.insert(pair) {
let mut binding = Binding::new();
binding.insert(s_var.clone(), s.clone());
binding.insert(o_var.clone(), o);
result.push(binding);
}
}
}
}
(Term::Variable(s_var), concrete_obj) => {
// Subject variable, concrete object: find all subjects that can reach object
// We enumerate all subjects AND all objects to handle terminal nodes
// (nodes that appear only as objects, not as subjects)
let mut candidates: HashSet<Term> = HashSet::new();
for s in dataset.subjects()? {
candidates.insert(s);
}
// Also include all objects so terminal nodes (like leaves in hierarchy)
// are considered as potential starting points
for o in dataset.objects()? {
candidates.insert(o);
}
for s in candidates {
let reachable = evaluate_path_from(&path_pp, &s, &adapter)?;
if reachable.contains(concrete_obj) {
let mut binding = Binding::new();
binding.insert(s_var.clone(), s);
result.push(binding);
}
}
}
(concrete_subj, Term::Variable(o_var)) => {
// Concrete subject, object variable: find all reachable objects
let reachable = evaluate_path_from(&path_pp, concrete_subj, &adapter)?;
for o in reachable {
let mut binding = Binding::new();
binding.insert(o_var.clone(), o);
result.push(binding);
}
}
(concrete_subj, concrete_obj) => {
// Both concrete: check if path exists
let reachable = evaluate_path_from(&path_pp, concrete_subj, &adapter)?;
if reachable.contains(concrete_obj) {
result.push(Binding::new());
}
}
}
Ok(result)
}
}
/// Evaluate a property path from a given subject, returning all reachable objects
fn evaluate_path_from(
path: &crate::path::PropertyPath,
subject: &Term,
adapter: &DatasetPathAdapter<'_>,
) -> Result<HashSet<Term>> {
use crate::path::PathDataset;
use crate::path::PropertyPath as PP;
let mut result = HashSet::new();
match path {
PP::Direct(pred) => {
let objects = adapter.find_outgoing(subject, pred)?;
result.extend(objects);
}
PP::Inverse(inner) => {
// For inverse: find nodes x such that inner(x, subject)
// We need to find all nodes from which subject is reachable via inner
// For a direct inner path, this is efficient:
if let PP::Direct(pred) = inner.as_ref() {
let incoming = adapter.find_incoming(pred, subject)?;
result.extend(incoming);
} else {
// For complex inner paths, we enumerate all predicates
let all_preds = adapter.get_predicates()?;
for pred in &all_preds {
let candidates = adapter.find_incoming(pred, subject)?;
for candidate in candidates {
let forward = evaluate_path_from(inner, &candidate, adapter)?;
if forward.contains(subject) {
result.insert(candidate);
}
}
}
}
}
PP::Sequence(left, right) => {
let intermediates = evaluate_path_from(left, subject, adapter)?;
for mid in &intermediates {
let right_results = evaluate_path_from(right, mid, adapter)?;
result.extend(right_results);
}
}
PP::Alternative(left, right) => {
result.extend(evaluate_path_from(left, subject, adapter)?);
result.extend(evaluate_path_from(right, subject, adapter)?);
}
PP::ZeroOrMore(inner) => {
// Include subject itself (zero hops)
result.insert(subject.clone());
// BFS from subject
let mut queue = VecDeque::new();
queue.push_back(subject.clone());
let mut seen = HashSet::new();
seen.insert(subject.clone());
while let Some(current) = queue.pop_front() {
let next_nodes = evaluate_path_from(inner, ¤t, adapter)?;
for next in next_nodes {
if !seen.contains(&next) {
seen.insert(next.clone());
result.insert(next.clone());
queue.push_back(next);
}
}
}
}
PP::OneOrMore(inner) => {
// BFS without including subject itself
let mut queue = VecDeque::new();
let mut seen = HashSet::new();
seen.insert(subject.clone());
let immediate = evaluate_path_from(inner, subject, adapter)?;
for node in immediate {
if !seen.contains(&node) {
seen.insert(node.clone());
result.insert(node.clone());
queue.push_back(node);
}
}
while let Some(current) = queue.pop_front() {
let next_nodes = evaluate_path_from(inner, ¤t, adapter)?;
for next in next_nodes {
if !seen.contains(&next) {
seen.insert(next.clone());
result.insert(next.clone());
queue.push_back(next);
}
}
}
}
PP::ZeroOrOne(inner) => {
// Include subject itself (zero hops)
result.insert(subject.clone());
// Include direct successors (one hop)
result.extend(evaluate_path_from(inner, subject, adapter)?);
}
PP::NegatedPropertySet(excluded_preds) => {
let all_preds = adapter.get_predicates()?;
let excluded_set: HashSet<&Term> = excluded_preds.iter().collect();
for pred in &all_preds {
if excluded_set.is_empty() || !excluded_set.contains(pred) {
let objects = adapter.find_outgoing(subject, pred)?;
result.extend(objects);
}
}
}
}
Ok(result)
}