-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgremlin_query.cpp
More file actions
227 lines (200 loc) · 7.05 KB
/
Copy pathgremlin_query.cpp
File metadata and controls
227 lines (200 loc) · 7.05 KB
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
#include "function/gremlin_query.h"
#include <cctype>
#include <sstream>
#include "common/exception/runtime.h"
#include "function/table/bind_data.h"
#include "function/table/bind_input.h"
#include "function/table/simple_table_function.h"
#include "function/table/table_function.h"
namespace lbug {
namespace gremlin_extension {
using namespace lbug::common;
using namespace lbug::function;
using namespace lbug::main;
namespace {
struct GremlinQueryBindData final : TableFuncBindData {
std::string query;
explicit GremlinQueryBindData(std::string query)
: TableFuncBindData{binder::expression_vector{}, 0 /* maxOffset */},
query{std::move(query)} {}
std::unique_ptr<TableFuncBindData> copy() const override {
return std::make_unique<GremlinQueryBindData>(*this);
}
};
struct Traversal {
std::string hasKey;
std::string hasValue;
std::vector<std::string> outLabels;
std::string valuesKey;
};
class GremlinParser {
public:
explicit GremlinParser(std::string query) : query{std::move(query)} {}
Traversal parse() {
consumeWhitespace();
consumeToken("g");
consumeToken(".");
consumeCall("V");
consumeWhitespace();
Traversal traversal;
while (!isAtEnd()) {
consumeToken(".");
const auto step = parseIdentifier();
consumeWhitespace();
consumeToken("(");
if (step == "has") {
if (!traversal.hasKey.empty()) {
throw RuntimeException{"GREMLIN supports a single has(key, value) step."};
}
traversal.hasKey = parseString();
consumeWhitespace();
consumeToken(",");
traversal.hasValue = parseString();
} else if (step == "out") {
traversal.outLabels.push_back(parseString());
} else if (step == "values") {
traversal.valuesKey = parseString();
consumeWhitespace();
consumeToken(")");
consumeWhitespace();
if (!isAtEnd()) {
throw RuntimeException{"GREMLIN values(key) must be the final step."};
}
validate(traversal);
return traversal;
} else {
throw RuntimeException{"GREMLIN supports only has(key, value), out(label), and "
"values(key) after g.V()."};
}
consumeWhitespace();
consumeToken(")");
consumeWhitespace();
}
validate(traversal);
return traversal;
}
private:
bool isAtEnd() const { return pos >= query.size(); }
void consumeWhitespace() {
while (!isAtEnd() && std::isspace(static_cast<unsigned char>(query[pos]))) {
pos++;
}
}
void consumeToken(const std::string& token) {
consumeWhitespace();
if (query.substr(pos, token.size()) != token) {
throw RuntimeException{"Invalid GREMLIN traversal near '" + query.substr(pos) + "'."};
}
pos += token.size();
}
void consumeCall(const std::string& name) {
consumeToken(name);
consumeToken("(");
consumeToken(")");
}
std::string parseIdentifier() {
consumeWhitespace();
const auto start = pos;
while (!isAtEnd() &&
(std::isalnum(static_cast<unsigned char>(query[pos])) || query[pos] == '_')) {
pos++;
}
if (start == pos) {
throw RuntimeException{"Expected GREMLIN step name."};
}
return query.substr(start, pos - start);
}
std::string parseString() {
consumeWhitespace();
if (isAtEnd() || (query[pos] != '"' && query[pos] != '\'')) {
throw RuntimeException{"Expected GREMLIN string literal."};
}
const auto quote = query[pos++];
std::string result;
while (!isAtEnd()) {
const auto ch = query[pos++];
if (ch == quote) {
return result;
}
if (ch == '\\') {
if (isAtEnd()) {
throw RuntimeException{"Unterminated GREMLIN string escape."};
}
result.push_back(query[pos++]);
} else {
result.push_back(ch);
}
}
throw RuntimeException{"Unterminated GREMLIN string literal."};
}
static void validate(const Traversal& traversal) {
if (traversal.hasKey.empty() || traversal.valuesKey.empty()) {
throw RuntimeException{
"GREMLIN traversal must contain has(key, value) and final values(key) steps."};
}
}
private:
std::string query;
size_t pos = 0;
};
static std::string quoteIdentifier(const std::string& identifier) {
std::string result = "`";
for (const auto ch : identifier) {
if (ch == '`') {
result += "``";
} else {
result.push_back(ch);
}
}
result += "`";
return result;
}
static std::string quoteStringLiteral(const std::string& value) {
std::string result = "'";
for (const auto ch : value) {
if (ch == '\'') {
result += "\\'";
} else if (ch == '\\') {
result += "\\\\";
} else {
result.push_back(ch);
}
}
result += "'";
return result;
}
static std::string translateToCypher(const std::string& gremlinQuery) {
const auto traversal = GremlinParser{gremlinQuery}.parse();
std::ostringstream cypher;
cypher << "MATCH (v0";
for (auto i = 0u; i < traversal.outLabels.size(); i++) {
cypher << ")-[:" << quoteIdentifier(traversal.outLabels[i]) << "]->(v" << (i + 1);
}
cypher << ") WHERE v0." << quoteIdentifier(traversal.hasKey) << " = "
<< quoteStringLiteral(traversal.hasValue) << " RETURN v" << traversal.outLabels.size()
<< "." << quoteIdentifier(traversal.valuesKey) << " AS "
<< quoteIdentifier(traversal.valuesKey) << ";";
return cypher.str();
}
static std::unique_ptr<TableFuncBindData> bindFunc(ClientContext* /*context*/,
const TableFuncBindInput* input) {
return std::make_unique<GremlinQueryBindData>(input->getLiteralVal<std::string>(0));
}
static std::string rewriteQuery(ClientContext& /*context*/, const TableFuncBindData& bindData) {
return translateToCypher(bindData.constPtrCast<GremlinQueryBindData>()->query);
}
} // namespace
function_set GremlinQueryFunction::getFunctionSet() {
function_set functionSet;
auto func = std::make_unique<TableFunction>(name, std::vector{LogicalTypeID::STRING});
func->tableFunc = TableFunction::emptyTableFunc;
func->bindFunc = bindFunc;
func->initSharedStateFunc = SimpleTableFunc::initSharedState;
func->initLocalStateFunc = TableFunction::initEmptyLocalState;
func->rewriteFunc = rewriteQuery;
func->canParallelFunc = [] { return false; };
functionSet.push_back(std::move(func));
return functionSet;
}
} // namespace gremlin_extension
} // namespace lbug