smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
parser.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_PARSER_H_
2#define SMOLLERGRAPH_PARSER_H_
3
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "absl/status/statusor.h"
10
11namespace smollergraph {
12
13// A node in a pattern, such as (a:Animal {name: 'Red fox'}). Its variable,
14// its label and its properties are all optional: () matches any node, and
15// gives it no variable.
17 std::string variable;
18 std::string label;
19 std::vector<Property> properties;
20};
21
22// The hops of a relationship written with a star and no number, such as
23// -[:KNOWS*]-, which stands for any number of them.
24inline constexpr int kAnyHops = 0;
25
26// A relationship from a node in a pattern to the next one, such as -[:EATS]->,
27// or -[:EATS*2]->, which is two of them in a row. One written <-[:EATS]- goes
28// the other way, from the next node to this one, and one written -[:EATS]-,
29// with no arrow, goes either way.
31 // Its types: a relationship of any of them matches.
32 std::vector<std::string> types;
33 int hops = 1;
34 // Which way to follow it from the node before it: out, in when it's written
35 // with its arrow on the left, or both with no arrow.
37 // The property a cheapest path adds up, as in -[:KNOWS* WSHORTEST(weight)]-,
38 // which is how Kùzu writes it.
39 std::string weight;
40};
41
42// A variable's property, such as a.name or b.weight_g.
44 std::string variable;
45 std::string key;
46};
47
48// A comparison in WHERE, such as b.weight_g < 1000: a property, an operator,
49// which is one of = <> < <= > >=, and a value.
55
56// What a query returns in a column: a variable's property, such as b.name,
57// or with no key, a path's variable, such as p, which returns the path, or a
58// function of the path: length(p), how many relationships it has, or cost(p),
59// what its weights add up to.
60struct ReturnItem {
61 std::string variable;
62 std::string key;
63 std::string function;
64};
65
66// A query in our subset of Cypher, such as
67// MATCH (a {name: 'Red fox'})-[:EATS]->(b) WHERE b.weight_g < 1000
68// RETURN DISTINCT b.name
69// with EXPLAIN in front of it to ask for its plan. Relationship i joins node i
70// to node i + 1.
71struct Query {
72 bool explain = false;
73 // The path's variable, for a pattern written as p = shortestPath(...),
74 // which asks for a shortest path between its two nodes, or as p = (...),
75 // for a cheapest path.
76 std::string path;
77 bool shortest = false;
78 std::vector<NodePattern> nodes;
79 std::vector<RelationshipPattern> relationships;
80 // The comparisons in WHERE, joined by AND, so a row has to pass them all.
81 std::vector<Comparison> where;
82 bool distinct = false;
83 // What it returns, in order.
84 std::vector<ReturnItem> returns;
85};
86
87// Reads a query. Returns an error that says what's wrong and where, when it
88// isn't one our subset has.
89absl::StatusOr<Query> Parse(std::string_view text);
90
91// Reads properties as Cypher writes them, in braces, such as
92// {legs: 4, diet: 'omnivore'}, with a string or a whole number for each key.
93absl::StatusOr<std::vector<Property>> ParseProperties(std::string_view text);
94
95} // namespace smollergraph
96
97#endif // SMOLLERGRAPH_PARSER_H_
std::string_view text
Definition facts.cc:17
std::variant< int64_t, std::string > Value
A property's value, as the graph hands it out: a whole number or a string.
Definition graph.h:100
absl::StatusOr< Query > Parse(std::string_view text)
Reads a query.
Definition parser.cc:284
absl::StatusOr< std::vector< Property > > ParseProperties(std::string_view text)
Reads properties as Cypher writes them, in braces, such as {legs: 4, diet: 'omnivore'},...
Definition parser.cc:293
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
Definition graph.h:72
constexpr int kAnyHops
The hops of a relationship written with a star and no number, such as -[:KNOWS*]-,...
Definition parser.h:24
A comparison in WHERE, such as b.weight_g < 1000: a property, an operator, which is one of = <> < <= ...
Definition parser.h:50
PropertyLookup property
Definition parser.h:51
A node in a pattern, such as (a:Animal {name: 'Red fox'}).
Definition parser.h:16
std::vector< Property > properties
Definition parser.h:19
std::string variable
Definition parser.h:17
A variable's property, such as a.name or b.weight_g.
Definition parser.h:43
A query in our subset of Cypher, such as MATCH (a {name: 'Red fox'})-[:EATS]->(b) WHERE b....
Definition parser.h:71
std::vector< RelationshipPattern > relationships
Definition parser.h:79
std::string path
The path's variable, for a pattern written as p = shortestPath(...), which asks for a shortest path b...
Definition parser.h:76
std::vector< Comparison > where
The comparisons in WHERE, joined by AND, so a row has to pass them all.
Definition parser.h:81
std::vector< NodePattern > nodes
Definition parser.h:78
std::vector< ReturnItem > returns
What it returns, in order.
Definition parser.h:84
A relationship from a node in a pattern to the next one, such as -[:EATS]->, or -[:EATS*2]->,...
Definition parser.h:30
std::vector< std::string > types
Its types: a relationship of any of them matches.
Definition parser.h:32
std::string weight
The property a cheapest path adds up, as in -[:KNOWS* WSHORTEST(weight)]-, which is how Kùzu writes i...
Definition parser.h:39
Direction direction
Which way to follow it from the node before it: out, in when it's written with its arrow on the left,...
Definition parser.h:36
What a query returns in a column: a variable's property, such as b.name, or with no key,...
Definition parser.h:60
std::string function
Definition parser.h:63
std::string variable
Definition parser.h:61