smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
plan.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_PLAN_H_
2#define SMOLLERGRAPH_PLAN_H_
3
4#include <optional>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include "absl/status/statusor.h"
10#include "smollergraph/graph.h"
11#include "smollergraph/parser.h"
12#include "smollergraph/query.h"
13
14namespace smollergraph {
15
16// One step of a plan. A plan works on rows, which have a slot for each node
17// of the pattern, and each step turns the rows it's given into the rows for
18// the next step. The steps take their names from Neo4j's plans.
19struct Step {
20 enum class Kind {
21 // A row for the node named `text`, in slot `to`, found in the index.
23 // A row for each node in the graph, in slot `to`.
25 // A row for each node with the label `text`, in slot `to`. With no index
26 // on labels, it reads each node's record to find them.
28 // For each row, a row for each node that a relationship of any of the
29 // `types` goes to from the node in slot `from`, in slot `to`, or comes
30 // from, when its direction is in.
31 kExpand,
32 // The rows whose node in slot `from` has a property `text` that compares
33 // with `value` as `op` says.
34 kFilter,
35 // The rows whose node in slot `from` has the label `text`.
37 // For each row, a shortest path from the node in slot `from` to the node
38 // in slot `to`, along relationships of any of the `types`, and no row when
39 // there's no path.
41 // The same for a cheapest path, whose relationships' `weight` adds up to
42 // the least.
44 };
46 int from = -1;
47 int to = -1;
48 std::string text{};
49 // A filter's operator, and the value it compares with.
50 std::string op = "=";
52 // Which of its node's relationships an expand or a path follows.
54 // The property a cheapest path adds up.
55 std::string weight{};
56 // The relationship types an expand or a path follows.
57 std::vector<std::string> types{};
58};
59
60// A column of what a plan returns: its heading, such as b.name, p or
61// length(p), and the property of the node in a slot that it returns, or with
62// no slot, the row's path, or a function of it, its length or its cost.
63struct Column {
64 std::string heading;
65 int slot = -1;
66 std::string key;
67 std::string function;
68};
69
70// How to answer a query: where to start, and where to go from there.
71struct Plan {
72 std::vector<Step> steps;
73 // The pattern's variable for each slot, or nothing for a node it didn't
74 // give one.
75 std::vector<std::string> slots;
76 // What it returns, a column at a time, in order.
77 std::vector<Column> returns;
78 bool distinct = false;
79};
80
81// Plans a query, starting from the node of its pattern that's cheapest to
82// find, or for a shortest path, finding both its nodes, and then the path.
83// Returns an error when the query compares or returns a variable its pattern
84// doesn't have, gives two nodes one variable, or has a pattern it can't plan.
85absl::StatusOr<Plan> PlanQuery(const Query& query);
86
87// The plan's steps, a line each, in the order they run, as EXPLAIN shows them.
88std::string Explain(const Plan& plan);
89
90// A value a query returns, and the node it's a property of. A node without
91// the property returns Cypher's null, which is no value. A path, or a function
92// of it, comes with the path, and its first node.
93struct Cell {
95 std::optional<Value> value;
97};
98
99// What a query returns: a row for each match of its pattern, with a cell for
100// each column.
101struct Result {
102 std::vector<std::string> columns;
103 std::vector<std::vector<Cell>> rows;
104};
105
106// A value as sg query prints it: a number in digits, and a string as it is.
107std::string ValueText(const Value& value);
108
109// Runs a plan on a graph, a step at a time, each on all of the rows at once.
110Result Run(const Plan& plan, const Graph& graph);
111
112// Parses, plans and runs a query in our subset of Cypher on a graph, in one
113// call, as Kùzu's connection does with execute. Returns the parser's or the
114// planner's error for a query it can't run, and an error for a query with
115// EXPLAIN in front, as that asks for the plan, which PlanQuery and Explain
116// give.
117absl::StatusOr<Result> Execute(const Graph& graph, std::string_view text);
118
119} // namespace smollergraph
120
121#endif // SMOLLERGRAPH_PLAN_H_
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
Definition graph.h:113
std::string_view text
Definition facts.cc:17
absl::StatusOr< Plan > PlanQuery(const Query &query)
Plans a query, starting from the node of its pattern that's cheapest to find, or for a shortest path,...
Definition plan.cc:209
uint32_t NodeId
Definition graph.h:20
Result Run(const Plan &plan, const Graph &graph)
Runs a plan on a graph, a step at a time, each on all of the rows at once.
Definition plan.cc:291
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< Result > Execute(const Graph &graph, std::string_view text)
Parses, plans and runs a query in our subset of Cypher on a graph, in one call, as Kùzu's connection ...
Definition plan.cc:387
std::string ValueText(const Value &value)
A value as sg query prints it: a number in digits, and a string as it is.
Definition plan.cc:202
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
Definition graph.h:72
std::string Explain(const Plan &plan)
The plan's steps, a line each, in the order they run, as EXPLAIN shows them.
Definition plan.cc:399
A value a query returns, and the node it's a property of.
Definition plan.h:93
NodeId node
Definition plan.h:94
std::optional< Value > value
Definition plan.h:95
A column of what a plan returns: its heading, such as b.name, p or length(p), and the property of the...
Definition plan.h:63
std::string function
Definition plan.h:67
std::string key
Definition plan.h:66
std::string heading
Definition plan.h:64
A path through the graph: its nodes, from its start to its end, and the relationships between them,...
Definition query.h:28
How to answer a query: where to start, and where to go from there.
Definition plan.h:71
std::vector< Step > steps
Definition plan.h:72
std::vector< Column > returns
What it returns, a column at a time, in order.
Definition plan.h:77
std::vector< std::string > slots
The pattern's variable for each slot, or nothing for a node it didn't give one.
Definition plan.h:75
A query in our subset of Cypher, such as MATCH (a {name: 'Red fox'})-[:EATS]->(b) WHERE b....
Definition parser.h:71
What a query returns: a row for each match of its pattern, with a cell for each column.
Definition plan.h:101
std::vector< std::vector< Cell > > rows
Definition plan.h:103
std::vector< std::string > columns
Definition plan.h:102
One step of a plan.
Definition plan.h:19
std::string weight
The property a cheapest path adds up.
Definition plan.h:55
Direction direction
Which of its node's relationships an expand or a path follows.
Definition plan.h:53
@ kShortestPath
For each row, a shortest path from the node in slot from to the node in slot to, along relationships ...
@ kNodeIndexSeek
A row for the node named text, in slot to, found in the index.
@ kCheapestPath
The same for a cheapest path, whose relationships' weight adds up to the least.
@ kHasLabel
The rows whose node in slot from has the label text.
@ kFilter
The rows whose node in slot from has a property text that compares with value as op says.
@ kExpand
For each row, a row for each node that a relationship of any of the types goes to from the node in sl...
@ kNodeByLabelScan
A row for each node with the label text, in slot to.
@ kAllNodesScan
A row for each node in the graph, in slot to.
std::vector< std::string > types
The relationship types an expand or a path follows.
Definition plan.h:57
std::string op
A filter's operator, and the value it compares with.
Definition plan.h:50
std::string text
Definition plan.h:48