MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / parse

Function parse

nodedb/src/engine/graph/pattern/compiler/mod.rs:32–119  ·  view source on GitHub ↗

Parse a MATCH query string into a `MatchQuery` AST. The input should start with `MATCH` or `OPTIONAL MATCH`.

(sql: &str)

Source from the content-addressed store, hash-verified

30///
31/// The input should start with `MATCH` or `OPTIONAL MATCH`.
32pub fn parse(sql: &str) -> crate::Result<MatchQuery> {
33 let mut where_predicates = Vec::new();
34 let mut return_columns = Vec::new();
35 let mut distinct = false;
36 let mut limit = None;
37 let mut order_by = Vec::new();
38 let mut collection = None;
39
40 let trimmed = sql.trim();
41 let upper = trimmed.to_uppercase();
42
43 let where_pos = find_top_level_keyword(&upper, "WHERE");
44 let return_pos = find_top_level_keyword(&upper, "RETURN");
45 let limit_pos = find_top_level_keyword(&upper, "LIMIT");
46 let order_pos = find_top_level_keyword(&upper, "ORDER BY");
47 // `IN 'collection'` clause: appears after the last closing `)` and before RETURN/WHERE.
48 // Use a boundary-aware scan that accepts `)` before `IN`.
49 let in_pos = find_in_clause(&upper);
50
51 // Extract collection name from `IN 'collection'` if present.
52 if let Some(ip) = in_pos {
53 let after_in = trimmed[ip + 2..].trim();
54 // Strip surrounding quotes.
55 let coll = if (after_in.starts_with('\'') && after_in.contains('\''))
56 || (after_in.starts_with('"') && after_in.contains('"'))
57 {
58 let q = after_in.chars().next().unwrap();
59 let rest = &after_in[1..];
60 rest.find(q).map(|end| rest[..end].to_string())
61 } else {
62 after_in.split_whitespace().next().map(|s| s.to_string())
63 };
64 collection = coll;
65 }
66
67 let pattern_end = [where_pos, return_pos, limit_pos, order_pos, in_pos]
68 .iter()
69 .filter_map(|&p| p)
70 .min()
71 .unwrap_or(trimmed.len());
72
73 let pattern_section = &trimmed[..pattern_end];
74 let clauses = parse_match_clauses(pattern_section)?;
75
76 if let Some(wp) = where_pos {
77 let where_end = [return_pos, limit_pos, order_pos]
78 .iter()
79 .filter_map(|&p| p)
80 .min()
81 .unwrap_or(trimmed.len());
82 let where_section = &trimmed[wp + 5..where_end].trim();
83 where_predicates = parse_where(where_section)?;
84 }
85
86 if let Some(rp) = return_pos {
87 let return_end = [limit_pos, order_pos]
88 .iter()
89 .filter_map(|&p| p)

Callers 15

parse_simple_matchFunction · 0.70
parse_multi_hopFunction · 0.70
parse_optional_matchFunction · 0.70
parse_where_equalsFunction · 0.70
parse_where_comparisonFunction · 0.70
parse_where_not_existsFunction · 0.70
parse_self_joinFunction · 0.70
parse_left_arrowFunction · 0.70
parse_undirectedFunction · 0.70
parse_limitFunction · 0.70

Calls 14

find_top_level_keywordFunction · 0.85
find_in_clauseFunction · 0.85
parse_match_clausesFunction · 0.85
parse_whereFunction · 0.85
to_stringMethod · 0.80
parse_returnFunction · 0.70
parse_order_byFunction · 0.70
containsMethod · 0.45
nextMethod · 0.45
findMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45

Tested by 15

parse_simple_matchFunction · 0.56
parse_multi_hopFunction · 0.56
parse_optional_matchFunction · 0.56
parse_where_equalsFunction · 0.56
parse_where_comparisonFunction · 0.56
parse_where_not_existsFunction · 0.56
parse_self_joinFunction · 0.56
parse_left_arrowFunction · 0.56
parse_undirectedFunction · 0.56
parse_limitFunction · 0.56