velesdb-core 2.0.0

High-performance vector database engine written in Rust
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
//! MATCH query parsing module (EPIC-061/US-002 refactoring).
//!
//! Extracted from select.rs to reduce file size and improve modularity.

use super::{extract_identifier, Rule};
use crate::velesql::ast::{OrderByExpr, Query};
use crate::velesql::error::ParseError;
use crate::velesql::graph_pattern::{
    Direction, GraphPattern, MatchClause, NodePattern, RelationshipPattern, ReturnClause,
    ReturnItem,
};
use crate::velesql::Parser;

impl Parser {
    /// Parse a MATCH query (EPIC-045 US-001).
    pub(crate) fn parse_match_query(
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<Query, ParseError> {
        let mut patterns = Vec::new();
        let mut where_clause = None;
        let mut return_clause = ReturnClause {
            items: Vec::new(),
            order_by: None,
            limit: None,
        };
        let mut limit = None;

        for inner_pair in pair.into_inner() {
            match inner_pair.as_rule() {
                Rule::graph_pattern => patterns.push(Self::parse_graph_pattern(inner_pair)?),
                Rule::where_clause => where_clause = Some(Self::parse_where_clause(inner_pair)?),
                Rule::return_clause => return_clause = Self::parse_return_clause(inner_pair)?,
                Rule::order_by_clause => {
                    return_clause.order_by = Some(Self::convert_order_by_to_match(inner_pair)?);
                }
                Rule::limit_clause => limit = Self::extract_limit_integer(inner_pair),
                _ => {}
            }
        }

        return_clause.limit = limit;

        Ok(Query::new_match(MatchClause {
            patterns,
            where_clause,
            return_clause,
        }))
    }

    /// Converts a parsed ORDER BY clause into MATCH-compatible `OrderByItem`s.
    fn convert_order_by_to_match(
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<Vec<crate::velesql::graph_pattern::OrderByItem>, ParseError> {
        let order_by = Self::parse_order_by_clause(pair)?;
        Ok(order_by
            .into_iter()
            .map(|ob| crate::velesql::graph_pattern::OrderByItem {
                expression: Self::order_by_expr_to_string(ob.expr),
                descending: ob.descending,
            })
            .collect())
    }

    /// Converts an `OrderByExpr` into its string representation.
    fn order_by_expr_to_string(expr: OrderByExpr) -> String {
        match expr {
            OrderByExpr::Field(f) => f,
            OrderByExpr::Similarity(s) => {
                let vec_str = match &s.vector {
                    crate::velesql::ast::VectorExpr::Parameter(name) => format!("${name}"),
                    crate::velesql::ast::VectorExpr::Literal(vals) => format!("{vals:?}"),
                };
                format!("similarity({}, {vec_str})", s.field)
            }
            OrderByExpr::SimilarityBare => "similarity()".to_string(),
            OrderByExpr::Aggregate(a) => format!("{:?}()", a.function_type),
            OrderByExpr::Arithmetic(expr) => format!("{expr}"),
        }
    }

    /// Extracts the integer value from a limit clause pair.
    fn extract_limit_integer(pair: pest::iterators::Pair<Rule>) -> Option<u64> {
        pair.into_inner()
            .find(|lp| lp.as_rule() == Rule::integer)
            .and_then(|lp| lp.as_str().parse().ok())
    }

    /// Parse a graph pattern (EPIC-045 US-001).
    pub(super) fn parse_graph_pattern(
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<GraphPattern, ParseError> {
        let mut nodes = Vec::new();
        let mut relationships = Vec::new();

        for inner_pair in pair.into_inner() {
            match inner_pair.as_rule() {
                Rule::node_pattern => {
                    nodes.push(Self::parse_node_pattern(inner_pair)?);
                }
                Rule::relationship_pattern => {
                    relationships.push(Self::parse_relationship_pattern(inner_pair)?);
                }
                _ => {}
            }
        }

        Ok(GraphPattern {
            name: None,
            nodes,
            relationships,
        })
    }

    /// Parse a node pattern (EPIC-045 US-001).
    fn parse_node_pattern(pair: pest::iterators::Pair<Rule>) -> Result<NodePattern, ParseError> {
        let mut node = NodePattern::new();

        for inner_pair in pair.into_inner() {
            if inner_pair.as_rule() == Rule::node_spec {
                Self::apply_node_spec(inner_pair, &mut node)?;
            }
        }

        Ok(node)
    }

    /// Applies all fields from a `node_spec` pest pair to a `NodePattern`.
    fn apply_node_spec(
        spec: pest::iterators::Pair<Rule>,
        node: &mut NodePattern,
    ) -> Result<(), ParseError> {
        for spec_pair in spec.into_inner() {
            match spec_pair.as_rule() {
                Rule::node_alias => {
                    node.alias = Some(spec_pair.as_str().to_string());
                }
                Rule::node_labels => Self::collect_node_labels(spec_pair, node),
                Rule::node_properties => {
                    node.properties = Self::parse_node_properties(spec_pair)?;
                }
                Rule::collection_annotation => Self::apply_collection_annotation(spec_pair, node),
                _ => {}
            }
        }
        Ok(())
    }

    /// Collects label names from a `node_labels` pest pair into the node.
    fn collect_node_labels(spec_pair: pest::iterators::Pair<Rule>, node: &mut NodePattern) {
        for label_pair in spec_pair.into_inner() {
            if label_pair.as_rule() == Rule::label_name {
                node.labels.push(label_pair.as_str().to_string());
            }
        }
    }

    /// Applies a `collection_annotation` pest pair to the node's collection field.
    fn apply_collection_annotation(spec_pair: pest::iterators::Pair<Rule>, node: &mut NodePattern) {
        for coll_pair in spec_pair.into_inner() {
            if coll_pair.as_rule() == Rule::collection_ref {
                node.collection = Some(coll_pair.as_str().to_string());
            }
        }
    }

    /// Parse node properties (EPIC-045 US-001).
    pub(super) fn parse_node_properties(
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<std::collections::HashMap<String, crate::velesql::Value>, ParseError> {
        let mut props = std::collections::HashMap::new();

        for inner_pair in pair.into_inner() {
            if inner_pair.as_rule() == Rule::property_list {
                Self::collect_property_list(inner_pair, &mut props)?;
            }
        }

        Ok(props)
    }

    /// Collects key-value pairs from a `property_list` pest node.
    fn collect_property_list(
        list_pair: pest::iterators::Pair<Rule>,
        props: &mut std::collections::HashMap<String, crate::velesql::Value>,
    ) -> Result<(), ParseError> {
        for prop_pair in list_pair.into_inner() {
            if prop_pair.as_rule() == Rule::property {
                let (key, value) = Self::parse_single_property(prop_pair)?;
                if !key.is_empty() {
                    props.insert(key, value);
                }
            }
        }
        Ok(())
    }

    /// Parses a single property pair into `(key, value)`.
    fn parse_single_property(
        prop_pair: pest::iterators::Pair<Rule>,
    ) -> Result<(String, crate::velesql::Value), ParseError> {
        let mut key = String::new();
        let mut value = crate::velesql::Value::Null;

        for p in prop_pair.into_inner() {
            match p.as_rule() {
                Rule::identifier => key = extract_identifier(&p),
                Rule::property_value => value = Self::parse_property_value(p)?,
                _ => {}
            }
        }
        Ok((key, value))
    }

    /// Parse a property value (EPIC-045 US-001).
    ///
    /// Delegates scalar literal parsing to the shared [`helpers::parse_scalar_from_rule`].
    fn parse_property_value(
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<crate::velesql::Value, ParseError> {
        for inner_pair in pair.into_inner() {
            match inner_pair.as_rule() {
                Rule::string
                | Rule::integer
                | Rule::float
                | Rule::boolean
                | Rule::null_value
                | Rule::parameter => {
                    return super::helpers::parse_scalar_from_rule(&inner_pair);
                }
                _ => {}
            }
        }
        Ok(crate::velesql::Value::Null)
    }

    /// Parse a relationship pattern (EPIC-045 US-001).
    fn parse_relationship_pattern(
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<RelationshipPattern, ParseError> {
        let mut direction = Direction::Outgoing;
        let mut rel = RelationshipPattern::new(direction);

        for inner_pair in pair.into_inner() {
            match inner_pair.as_rule() {
                Rule::rel_incoming => {
                    direction = Direction::Incoming;
                    rel = RelationshipPattern::new(direction);
                    Self::parse_rel_spec_inner(&mut rel, inner_pair)?;
                }
                Rule::rel_outgoing => {
                    direction = Direction::Outgoing;
                    rel = RelationshipPattern::new(direction);
                    Self::parse_rel_spec_inner(&mut rel, inner_pair)?;
                }
                Rule::rel_undirected => {
                    direction = Direction::Both;
                    rel = RelationshipPattern::new(direction);
                    Self::parse_rel_spec_inner(&mut rel, inner_pair)?;
                }
                _ => {}
            }
        }

        Ok(rel)
    }

    /// Parse relationship spec inner (EPIC-045 US-001).
    fn parse_rel_spec_inner(
        rel: &mut RelationshipPattern,
        pair: pest::iterators::Pair<Rule>,
    ) -> Result<(), ParseError> {
        for inner_pair in pair.into_inner() {
            if inner_pair.as_rule() == Rule::rel_spec {
                Self::parse_rel_spec(rel, inner_pair)?;
            }
        }
        Ok(())
    }

    /// Parses the contents of a `rel_spec` node, delegating to `rel_details`.
    fn parse_rel_spec(
        rel: &mut RelationshipPattern,
        spec_pair: pest::iterators::Pair<Rule>,
    ) -> Result<(), ParseError> {
        for detail_pair in spec_pair.into_inner() {
            if detail_pair.as_rule() == Rule::rel_details {
                Self::apply_rel_details(rel, detail_pair)?;
            }
        }
        Ok(())
    }

    /// Applies each detail field from a `rel_details` node to the relationship.
    fn apply_rel_details(
        rel: &mut RelationshipPattern,
        details_pair: pest::iterators::Pair<Rule>,
    ) -> Result<(), ParseError> {
        for detail_pair in details_pair.into_inner() {
            match detail_pair.as_rule() {
                Rule::rel_alias => rel.alias = Some(detail_pair.as_str().to_string()),
                Rule::rel_types => Self::collect_rel_types(detail_pair, &mut rel.types),
                Rule::rel_range => rel.range = Self::parse_rel_range(detail_pair),
                Rule::node_properties => {
                    rel.properties = Self::parse_node_properties(detail_pair)?;
                }
                _ => {}
            }
        }
        Ok(())
    }

    /// Collects relationship type names from a `rel_types` node.
    fn collect_rel_types(pair: pest::iterators::Pair<Rule>, types: &mut Vec<String>) {
        for type_pair in pair.into_inner() {
            if type_pair.as_rule() == Rule::rel_type_name {
                types.push(type_pair.as_str().to_string());
            }
        }
    }

    /// Parse relationship range (EPIC-045 US-001).
    #[allow(clippy::unnecessary_wraps)] // Option is for consistency with caller expectations
    fn parse_rel_range(pair: pest::iterators::Pair<Rule>) -> Option<(u32, u32)> {
        for inner_pair in pair.into_inner() {
            if inner_pair.as_rule() == Rule::range_spec {
                let text = inner_pair.as_str();
                if let Some(dot_pos) = text.find("..") {
                    let start: u32 = text[..dot_pos].parse().unwrap_or(1);
                    let end: u32 = text[dot_pos + 2..].parse().unwrap_or(u32::MAX);
                    return Some((start, end));
                } else if let Ok(exact) = text.parse::<u32>() {
                    return Some((exact, exact));
                }
            } else if inner_pair.as_rule() == Rule::integer {
                if let Ok(exact) = inner_pair.as_str().parse::<u32>() {
                    return Some((exact, exact));
                }
            }
        }
        // Default: unbounded
        Some((1, u32::MAX))
    }

    /// Parse RETURN clause (EPIC-045 US-001).
    #[allow(clippy::unnecessary_wraps)] // Consistent with other parse_* methods
    fn parse_return_clause(pair: pest::iterators::Pair<Rule>) -> Result<ReturnClause, ParseError> {
        let mut items = Vec::new();

        for inner_pair in pair.into_inner() {
            if inner_pair.as_rule() == Rule::return_item_list {
                Self::collect_return_items(inner_pair, &mut items);
            }
        }

        Ok(ReturnClause {
            items,
            order_by: None,
            limit: None,
        })
    }

    /// Collects `return_item` entries from a `return_item_list` pest pair.
    fn collect_return_items(list_pair: pest::iterators::Pair<Rule>, items: &mut Vec<ReturnItem>) {
        for item_pair in list_pair.into_inner() {
            if item_pair.as_rule() == Rule::return_item {
                items.push(Self::parse_return_item(item_pair));
            }
        }
    }

    /// Parses a single `return_item` pest pair into a `ReturnItem`.
    fn parse_return_item(item_pair: pest::iterators::Pair<Rule>) -> ReturnItem {
        let mut expression = String::new();
        let mut alias = None;

        for p in item_pair.into_inner() {
            match p.as_rule() {
                Rule::return_expr => expression = Self::parse_return_expr(p),
                Rule::identifier => alias = Some(extract_identifier(&p)),
                _ => {}
            }
        }

        ReturnItem { expression, alias }
    }

    /// Parse RETURN expression (EPIC-045 US-001).
    fn parse_return_expr(pair: pest::iterators::Pair<Rule>) -> String {
        let text = pair.as_str().to_string();
        for inner_pair in pair.into_inner() {
            match inner_pair.as_rule() {
                Rule::similarity_return => {
                    return "similarity()".to_string();
                }
                Rule::property_access | Rule::identifier => {
                    return inner_pair.as_str().to_string();
                }
                _ => {}
            }
        }
        text
    }
}