linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
Documentation
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Variable attribute index lookup strategy
//!
//! Index lookup based on variable attributes, used in cases where the value of a runtime variable needs to be determined.
//!
//! Applicable scenarios:
//! - MATCH (v:Person) WHERE v.name = $varName
//! - MATCH (v:Person) WHERE v.age > $minAge
//! - Variable binding in parameterized queries

use super::seek_strategy::SeekStrategy;
use super::seek_strategy_base::{IndexInfo, SeekResult, SeekStrategyContext, SeekStrategyType};
use crate::storage::StorageReader;
use linkrs_core::{StorageError, Value};

/// Variable attribute predicate
#[derive(Debug, Clone, PartialEq)]
pub struct VariablePropertyPredicate {
    pub property: String,
    pub op: VariablePredicateOp,
    pub variable_name: String,
}

/// Variable predicate operation types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VariablePredicateOp {
    Eq, // =
    Ne, // !=
    Lt, // <
    Le, // <=
    Gt, // >
    Ge, // >=
    In, // IN
}

impl VariablePredicateOp {
    /// Convert to ordinary predicate operations
    pub fn to_predicate_op(&self) -> super::prop_index_seek::PredicateOp {
        match self {
            VariablePredicateOp::Eq => super::prop_index_seek::PredicateOp::Eq,
            VariablePredicateOp::Ne => super::prop_index_seek::PredicateOp::Ne,
            VariablePredicateOp::Lt => super::prop_index_seek::PredicateOp::Lt,
            VariablePredicateOp::Le => super::prop_index_seek::PredicateOp::Le,
            VariablePredicateOp::Gt => super::prop_index_seek::PredicateOp::Gt,
            VariablePredicateOp::Ge => super::prop_index_seek::PredicateOp::Ge,
            VariablePredicateOp::In => super::prop_index_seek::PredicateOp::In,
        }
    }
}

impl std::str::FromStr for VariablePredicateOp {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_uppercase().as_str() {
            "=" | "==" => Ok(VariablePredicateOp::Eq),
            "!=" | "<>" => Ok(VariablePredicateOp::Ne),
            "<" => Ok(VariablePredicateOp::Lt),
            "<=" => Ok(VariablePredicateOp::Le),
            ">" => Ok(VariablePredicateOp::Gt),
            ">=" => Ok(VariablePredicateOp::Ge),
            "IN" => Ok(VariablePredicateOp::In),
            _ => Err(format!("Invalid predicate operator: {}", s)),
        }
    }
}

/// Variable attribute index lookup strategy
#[derive(Debug, Clone)]
pub struct VariablePropIndexSeek {
    predicates: Vec<VariablePropertyPredicate>,
    variable_values: std::collections::HashMap<String, Value>,
}

impl VariablePropIndexSeek {
    pub fn new(predicates: Vec<VariablePropertyPredicate>) -> Self {
        Self {
            predicates,
            variable_values: std::collections::HashMap::new(),
        }
    }

    /// Binding variable values
    pub fn bind_variable(&mut self, name: &str, value: Value) {
        self.variable_values.insert(name.to_string(), value);
    }

    /// Batch binding of variable values
    pub fn bind_variables(&mut self, values: std::collections::HashMap<String, Value>) {
        self.variable_values.extend(values);
    }

    /// Check whether all variables have been bound.
    pub fn all_variables_bound(&self) -> bool {
        self.predicates
            .iter()
            .all(|pred| self.variable_values.contains_key(&pred.variable_name))
    }

    /// Extract variable attribute predicates from the list of expressions.
    pub fn extract_predicates(
        expressions: &[linkrs_core::Expression],
    ) -> Vec<VariablePropertyPredicate> {
        let mut predicates = Vec::new();

        for expr in expressions {
            if let Some(pred) = Self::extract_predicate(expr) {
                predicates.push(pred);
            }
        }

        predicates
    }

    /// Extracting variable attribute predicates from a single expression
    fn extract_predicate(expr: &linkrs_core::Expression) -> Option<VariablePropertyPredicate> {
        use linkrs_core::types::operators::BinaryOperator;

        match expr {
            linkrs_core::Expression::Binary { op, left, right } => {
                let op_str = match op {
                    BinaryOperator::Equal => "=",
                    BinaryOperator::NotEqual => "!=",
                    BinaryOperator::LessThan => "<",
                    BinaryOperator::LessThanOrEqual => "<=",
                    BinaryOperator::GreaterThan => ">",
                    BinaryOperator::GreaterThanOrEqual => ">=",
                    _ => return None,
                };

                // Try to extract the property name and variable name: v.name = $var
                if let (Some(prop), Some(var_name)) =
                    (Self::extract_property(left), Self::extract_variable(right))
                {
                    if let Ok(pred_op) = op_str.parse::<VariablePredicateOp>() {
                        return Some(VariablePropertyPredicate {
                            property: prop,
                            op: pred_op,
                            variable_name: var_name,
                        });
                    }
                }

                // Swap left and right attempts: $var = v.name
                if let (Some(prop), Some(var_name)) =
                    (Self::extract_property(right), Self::extract_variable(left))
                {
                    let swapped_op = match op_str {
                        "<" => VariablePredicateOp::Gt,
                        "<=" => VariablePredicateOp::Ge,
                        ">" => VariablePredicateOp::Lt,
                        ">=" => VariablePredicateOp::Le,
                        _ => op_str.parse::<VariablePredicateOp>().ok()?,
                    };
                    return Some(VariablePropertyPredicate {
                        property: prop,
                        op: swapped_op,
                        variable_name: var_name,
                    });
                }

                None
            }
            _ => None,
        }
    }

    /// Extract attribute names from the expression.
    fn extract_property(expr: &linkrs_core::Expression) -> Option<String> {
        match expr {
            linkrs_core::Expression::Property { object, property } => {
                // Check for node attribute access, e.g. v.name
                if matches!(object.as_ref(), linkrs_core::Expression::Variable(_)) {
                    Some(property.clone())
                } else {
                    None
                }
            }
            _ => None,
        }
    }

    /// Extract variable names from the expression.
    fn extract_variable(expr: &linkrs_core::Expression) -> Option<String> {
        match expr {
            // Variables that start with the symbol $ indicate parameters.
            linkrs_core::Expression::Variable(name) if name.starts_with('$') => {
                Some(name[1..].to_string())
            }
            _ => None,
        }
    }

    /// Find an index that is suitable for the predicate of the variable attribute.
    fn find_best_index<'a>(
        &'a self,
        context: &'a SeekStrategyContext,
    ) -> Option<(&'a IndexInfo, &'a VariablePropertyPredicate)> {
        for pred in &self.predicates {
            if let Some(index) = context.get_index_for_property(&pred.property) {
                return Some((index, pred));
            }
        }
        None
    }

    /// Does the evaluated value satisfy the predicate condition?
    fn value_matches(&self, value: &Value, pred: &VariablePropertyPredicate) -> bool {
        // Obtain the value of a variable
        let var_value = match self.variable_values.get(&pred.variable_name) {
            Some(v) => v,
            None => return false, // The variable is not bound, so a match cannot be made.
        };

        match pred.op {
            VariablePredicateOp::Eq => value == var_value,
            VariablePredicateOp::Ne => value != var_value,
            VariablePredicateOp::Lt => Self::compare_values(value, var_value)
                .map(|c| c < 0)
                .unwrap_or(false),
            VariablePredicateOp::Le => Self::compare_values(value, var_value)
                .map(|c| c <= 0)
                .unwrap_or(false),
            VariablePredicateOp::Gt => Self::compare_values(value, var_value)
                .map(|c| c > 0)
                .unwrap_or(false),
            VariablePredicateOp::Ge => Self::compare_values(value, var_value)
                .map(|c| c >= 0)
                .unwrap_or(false),
            VariablePredicateOp::In => {
                // The IN operation requires that the variable value be a list.
                matches!(var_value, Value::List(list) if list.contains(value))
            }
        }
    }

    /// Compare two values
    fn compare_values(left: &Value, right: &Value) -> Option<i32> {
        match (left, right) {
            (Value::SmallInt(i1), Value::SmallInt(i2)) => Some(i1.cmp(i2) as i32),
            (Value::Int(i1), Value::Int(i2)) => Some(i1.cmp(i2) as i32),
            (Value::BigInt(i1), Value::BigInt(i2)) => Some(i1.cmp(i2) as i32),
            (Value::Float(f1), Value::Float(f2)) => f1.partial_cmp(f2).map(|c| c as i32),
            (Value::Double(f1), Value::Double(f2)) => f1.partial_cmp(f2).map(|c| c as i32),
            (Value::String(s1), Value::String(s2)) => Some(s1.cmp(s2) as i32),
            _ => None,
        }
    }
}

fn has_distinct_multi_labels(labels: &[String]) -> bool {
    labels
        .iter()
        .collect::<std::collections::HashSet<_>>()
        .len()
        > 1
}

fn vertex_matches_single_label(
    vertex: &linkrs_core::Vertex,
    pattern: &super::seek_strategy_base::NodePattern,
) -> bool {
    if pattern.labels.is_empty() {
        return true;
    }
    pattern.labels.iter().all(|label| vertex.tag.name == *label)
}

impl SeekStrategy for VariablePropIndexSeek {
    fn execute<S: StorageReader>(
        &self,
        storage: &S,
        context: &SeekStrategyContext,
    ) -> Result<SeekResult, StorageError> {
        // Check whether the variable has been bound.
        if !self.all_variables_bound() {
            return Err(StorageError::invalid_input(
                "Variable property lookups require all variables to be bound".to_string(),
            ));
        }

        let mut vertex_ids = Vec::new();
        let mut rows_scanned = 0;

        if has_distinct_multi_labels(&context.node_pattern.labels) {
            return Ok(SeekResult {
                vertex_ids,
                strategy_used: SeekStrategyType::VariablePropIndexSeek,
                rows_scanned,
            });
        }

        // Find the best index.
        if let Some((index_info, primary_pred)) = self.find_best_index(context) {
            // Retrieve the vertices corresponding to the tags.
            let space_name = "default"; // In fact, the relevant information should be obtained from the context.
            let vertices = storage.scan_vertices_by_tag(space_name, &index_info.target_name)?;
            rows_scanned = vertices.len();

            // Filter the vertices that satisfy all predicates.
            for vertex in vertices {
                if !vertex_matches_single_label(&vertex, &context.node_pattern) {
                    continue;
                }
                let mut matches_all = true;

                // Check the subject and verb.
                if let Some(prop_value) = vertex.property_value(&primary_pred.property) {
                    if !self.value_matches(&prop_value, primary_pred) {
                        matches_all = false;
                    }
                } else {
                    matches_all = false;
                }

                // Check the other predicates.
                if matches_all {
                    for pred in &self.predicates {
                        if pred.property != primary_pred.property {
                            if let Some(prop_value) = vertex.property_value(&pred.property) {
                                if !self.value_matches(&prop_value, pred) {
                                    matches_all = false;
                                    break;
                                }
                            } else {
                                matches_all = false;
                                break;
                            }
                        }
                    }
                }

                if matches_all {
                    vertex_ids.push(Value::from(*vertex.vid()));
                }
            }
        }

        Ok(SeekResult {
            vertex_ids,
            strategy_used: SeekStrategyType::VariablePropIndexSeek,
            rows_scanned,
        })
    }

    fn supports(&self, _context: &SeekStrategyContext) -> bool {
        // Support is available as long as there is a predicate with variable attributes.
        !self.predicates.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use linkrs_core::Expression;

    #[test]
    fn test_variable_predicate_op_from_str() {
        assert_eq!(
            "=".parse::<VariablePredicateOp>().ok(),
            Some(VariablePredicateOp::Eq)
        );
        assert_eq!(
            "<".parse::<VariablePredicateOp>().ok(),
            Some(VariablePredicateOp::Lt)
        );
        assert_eq!(
            ">=".parse::<VariablePredicateOp>().ok(),
            Some(VariablePredicateOp::Ge)
        );
        assert_eq!(
            "IN".parse::<VariablePredicateOp>().ok(),
            Some(VariablePredicateOp::In)
        );
        assert!("unknown".parse::<VariablePredicateOp>().is_err());
    }

    #[test]
    fn test_extract_variable_predicate() {
        let expr = Expression::binary(
            Expression::property(Expression::variable("v"), "name"),
            linkrs_core::BinaryOperator::Equal,
            Expression::variable("$varName"),
        );

        let pred = VariablePropIndexSeek::extract_predicate(&expr);
        assert!(pred.is_some());

        let pred = pred.expect("Failed to extract predicate");
        assert_eq!(pred.property, "name");
        assert_eq!(pred.op, VariablePredicateOp::Eq);
        assert_eq!(pred.variable_name, "varName");
    }

    #[test]
    fn test_variable_binding() {
        let mut seek = VariablePropIndexSeek::new(vec![VariablePropertyPredicate {
            property: "age".to_string(),
            op: VariablePredicateOp::Gt,
            variable_name: "minAge".to_string(),
        }]);

        assert!(!seek.all_variables_bound());

        seek.bind_variable("minAge", Value::Int(18));

        assert!(seek.all_variables_bound());
    }
}