Skip to content
Navigation Menu
{{ message }}
forked from RedisGraph/RedisGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathast_rewrite_same_clauses.c
More file actions
373 lines (315 loc) · 11.9 KB
/
Copy pathast_rewrite_same_clauses.c
File metadata and controls
373 lines (315 loc) · 11.9 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
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
/*
* Copyright (c) 2006-Present, Redis Ltd.
* All rights reserved.
*
* Licensed under your choice of the Redis Source Available License 2.0
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
* GNU Affero General Public License v3 (AGPLv3).
*/
#include "ast.h"
#include "../util/arr.h"
#include "../query_ctx.h"
#include "../util/qsort.h"
#include "../util/sds/sds.h"
#include "../errors/errors.h"
#include "../procedures/procedure.h"
// forward declarations
static inline bool is_compressible(const cypher_astnode_t *clause,
cypher_astnode_type_t type);
static bool _compress_clauses(const cypher_astnode_t *node);
typedef void (*replace_func)(cypher_astnode_t *, cypher_astnode_t *, unsigned int, unsigned int);
typedef const cypher_astnode_t * (*get_clause_func)(const cypher_astnode_t *, unsigned int);
// compresses multiple consecutive CREATE clauses into a single CREATE clause
static void _replace_create_clause
(
cypher_astnode_t *root, // ast root
cypher_astnode_t **clauses, // clause being replaced
int scope_start, // beginning of scope
int scope_end, // ending of scope
replace_func replace // replace function pointer
) {
uint count = array_len(clauses);
struct cypher_input_range range = cypher_astnode_range(clauses[0]);
cypher_astnode_t **paths = array_new(cypher_astnode_t *, count);
// collect paths
for (uint i = 0; i < count; i++) {
const cypher_astnode_t *pattern =
cypher_ast_create_get_pattern(clauses[i]);
uint npaths = cypher_ast_pattern_npaths(pattern);
for (uint j = 0; j < npaths; j++) {
const cypher_astnode_t *path =
cypher_ast_pattern_get_path(pattern, j);
array_append(paths, cypher_ast_clone(path));
}
}
// build the replacement pattern
cypher_astnode_t *pattern = cypher_ast_pattern(paths, array_len(paths),
paths, array_len(paths), range);
// build the replacement clause
cypher_astnode_t *new_clause = cypher_ast_create(false, pattern, &pattern,
1, range);
// replace original clause with the new one
replace(root, new_clause, scope_start, scope_end);
array_free(paths);
}
// compresses multiple consecutive MATCH clauses into a single MATCH clause
static void _replace_match_clause
(
cypher_astnode_t *root, // ast root
cypher_astnode_t **clauses, // clause being replaced
int scope_start, // beginning of scope
int scope_end, // ending of scope
replace_func replace // replace function pointer
) {
uint count = array_len(clauses);
cypher_astnode_t *predicate = NULL;
struct cypher_input_range range = cypher_astnode_range(clauses[0]);
cypher_astnode_t **paths = array_new(cypher_astnode_t *, count);
// collect MATCH patterns and predicates
for (uint i = 0; i < count; i++) {
const cypher_astnode_t *pattern =
cypher_ast_match_get_pattern(clauses[i]);
uint npaths = cypher_ast_pattern_npaths(pattern);
for (uint j = 0; j < npaths; j++) {
const cypher_astnode_t *path =
cypher_ast_pattern_get_path(pattern, j);
array_append(paths, cypher_ast_clone(path));
}
// combine MATCH predicates into a single predicate using AND connectors
cypher_astnode_t *new_predicate =
(cypher_astnode_t *)cypher_ast_match_get_predicate(clauses[i]);
if(new_predicate != NULL) {
new_predicate = cypher_ast_clone(new_predicate);
if(predicate == NULL) {
predicate = new_predicate;
} else {
// concat using AND
cypher_astnode_t *children[] = {predicate, new_predicate};
predicate = cypher_ast_binary_operator(CYPHER_OP_AND, predicate,
new_predicate, children, 2, range);
}
}
}
// build the replacement pattern
cypher_astnode_t *pattern =
cypher_ast_pattern(paths, array_len(paths), paths, array_len(paths),
range);
// build the replacement clause
cypher_astnode_t *children[] = {pattern, predicate};
cypher_astnode_t *new_clause = cypher_ast_match(false, pattern, NULL, 0,
predicate, children, predicate == NULL ? 1 : 2, range);
// replace original clause with the new one
replace(root, new_clause, scope_start, scope_end);
array_free(paths);
}
// compresses multiple consecutive DELETE clauses into a single DELETE clause
static void _replace_delete_clause
(
cypher_astnode_t *root, // ast root
cypher_astnode_t **clauses, // clause being replaced
int scope_start, // beginning of scope
int scope_end, // ending of scope
replace_func replace // replace function pointer
) {
uint count = array_len(clauses);
struct cypher_input_range range = cypher_astnode_range(clauses[0]);
cypher_astnode_t **exps = array_new(cypher_astnode_t *, count);
rax *identifiers = raxNew();
// collect expressions
for (uint i = 0; i < count; i++) {
uint nexps = cypher_ast_delete_nexpressions(clauses[i]);
for(uint j = 0; j < nexps; j++) {
const cypher_astnode_t *exp =
cypher_ast_delete_get_expression(clauses[i], j);
// do not aggregate multiple appearances of an identifier
if(cypher_astnode_type(exp) == CYPHER_AST_IDENTIFIER) {
const char *identifier = cypher_ast_identifier_get_name(exp);
if(raxTryInsert(identifiers, (unsigned char *)identifier, strlen(identifier), NULL, NULL)) {
array_append(exps, cypher_ast_clone(exp));
}
} else {
// "MATCH p1=(n:N), p2=(m:M) DELETE nodes(p1)[0] DELETE nodes(p2)[0]"
array_append(exps, cypher_ast_clone(exp));
}
}
}
raxFree(identifiers);
// build the replacement clause
cypher_astnode_t *new_clause = cypher_ast_delete(false, exps,
array_len(exps), exps, array_len(exps), range);
// replace original clause with the new one
replace(root, new_clause, scope_start, scope_end);
array_free(exps);
}
// compresses multiple consecutive SET clauses into a single SET clause
static void _replace_set_clause
(
cypher_astnode_t *root, // ast root
cypher_astnode_t **clauses, // clause being replaced
int scope_start, // beginning of scope
int scope_end, // ending of scope
replace_func replace // replace function pointer
) {
uint count = array_len(clauses);
struct cypher_input_range range = cypher_astnode_range(clauses[0]);
cypher_astnode_t **items = array_new(cypher_astnode_t *, count);
for (uint i = 0; i < count; i++) {
uint nitems = cypher_ast_set_nitems(clauses[i]);
for(uint j = 0; j < nitems; j++) {
const cypher_astnode_t *item =
cypher_ast_set_get_item(clauses[i], j);
array_append(items, cypher_ast_clone(item));
}
}
// build the replacement clause
cypher_astnode_t *new_clause = cypher_ast_set(items, array_len(items),
items, array_len(items), range);
// replace original clause with the new one
replace(root, new_clause, scope_start, scope_end);
array_free(items);
}
// compresses multiple consecutive REMOVE clauses into a single REMOVE clause
static void _replace_remove_clause
(
cypher_astnode_t *root, // ast root
cypher_astnode_t **clauses, // clause being replaced
int scope_start, // beginning of scope
int scope_end, // ending of scope
replace_func replace // replace function pointer
) {
uint count = array_len(clauses);
struct cypher_input_range range = cypher_astnode_range(clauses[0]);
cypher_astnode_t **items = array_new(cypher_astnode_t *, count);
for (uint i = 0; i < count; i++) {
uint nitems = cypher_ast_remove_nitems(clauses[i]);
for(uint j = 0; j < nitems; j++) {
const cypher_astnode_t *item =
cypher_ast_remove_get_item(clauses[i], j);
array_append(items, cypher_ast_clone(item));
}
}
// build the replacement clause
cypher_astnode_t *new_clause =
cypher_ast_remove(items, array_len(items), items, array_len(items),
range);
// replace original clause with fully populated one
replace(root, new_clause, scope_start, scope_end);
array_free(items);
}
// tries to compress clauses of a query or a foreach clause
// returns true if a rewrite occurred
static bool _compress_clauses
(
const cypher_astnode_t *node // node containing clauses to compress
) {
bool rewritten = false;
cypher_astnode_type_t type = cypher_astnode_type(node);
ASSERT(type == CYPHER_AST_QUERY || type == CYPHER_AST_FOREACH);
cypher_astnode_t **clauses = array_new(cypher_astnode_t *, 0);
// is the node representing a FOREACH clause
uint clause_count = 0;
replace_func replace_func = NULL;
get_clause_func get_clause = NULL;
// use appropriate function to get a positioned clause
if(type == CYPHER_AST_FOREACH) {
get_clause = cypher_ast_foreach_get_clause;
clause_count = cypher_ast_foreach_nclauses(node);
replace_func = cypher_ast_foreach_replace_clauses;
} else {
get_clause = cypher_ast_query_get_clause;
replace_func = cypher_ast_query_replace_clauses;
clause_count = cypher_ast_query_nclauses(node);
}
for(uint i = 0; i < clause_count; i++) {
const cypher_astnode_t *clause = get_clause(node, i);
cypher_astnode_type_t t = cypher_astnode_type(clause);
// try compressing the inner-clauses of foreach clause and call subquery
if(t == CYPHER_AST_FOREACH) {
rewritten |= _compress_clauses(clause);
continue;
} else if(t == CYPHER_AST_CALL_SUBQUERY) {
rewritten |=
_compress_clauses(cypher_ast_call_subquery_get_query(clause));
continue;
}
// check compressibility, move on if not compressible
if(!is_compressible(clause, t)) {
continue;
}
//----------------------------------------------------------------------
// collect clauses of the same type as current clause
//----------------------------------------------------------------------
for (uint j = i; j < clause_count; j++) {
clause = get_clause(node, j);
cypher_astnode_type_t t2 = cypher_astnode_type(clause);
if(t2 != t || !is_compressible(clause, t2)) {
break;
}
array_append(clauses, (cypher_astnode_t *)clause);
}
//----------------------------------------------------------------------
// compress clauses
//----------------------------------------------------------------------
uint s = i;
uint e = s + array_len(clauses) - 1;
if(array_len(clauses) > 1) {
// multiple consecutive clauses of the same type
// compress them
if(t == CYPHER_AST_CREATE) {
_replace_create_clause((cypher_astnode_t *)node, clauses, s, e, replace_func);
} else if(t == CYPHER_AST_MATCH) {
_replace_match_clause((cypher_astnode_t *)node, clauses, s, e, replace_func);
} else if(t == CYPHER_AST_DELETE) {
_replace_delete_clause((cypher_astnode_t *)node, clauses, s, e, replace_func);
} else if(t == CYPHER_AST_SET) {
_replace_set_clause((cypher_astnode_t *)node, clauses, s, e, replace_func);
} else if(t == CYPHER_AST_REMOVE) {
_replace_remove_clause((cypher_astnode_t *)node, clauses, s, e, replace_func);
}
rewritten = true;
// update clause count, skip compressed clauses
clause_count -= array_len(clauses) - 1;
}
array_clear(clauses);
}
array_free(clauses);
return rewritten;
}
// returns true if AST clause type is compressible
static inline bool is_compressible
(
const cypher_astnode_t *clause, // clause to check
cypher_astnode_type_t type // type of clause
) {
// compressible clauses:
// 1. Non-OPTIONAL MATCH
// 2. CREATE
// 3. SET
// 4. DELETE
// 5. REMOVE
return ((type == CYPHER_AST_MATCH &&
!cypher_ast_match_is_optional(clause)) ||
type == CYPHER_AST_CREATE ||
type == CYPHER_AST_SET ||
type == CYPHER_AST_DELETE ||
type == CYPHER_AST_REMOVE);
}
// rewrite result by compressing consecutive clauses of the same type
// to a single clause, returning true if the rewrite has been performed
bool AST_RewriteSameClauses
(
const cypher_astnode_t *root // root of AST
) {
bool rewritten = false;
if(cypher_astnode_type(root) != CYPHER_AST_STATEMENT) {
return rewritten;
}
// retrieve the root's body
const cypher_astnode_t *body = cypher_ast_statement_get_body(root);
if(cypher_astnode_type(body) != CYPHER_AST_QUERY) {
return rewritten;
}
// traverse clauses
// compress consecutive clauses
return _compress_clauses(body);
}
You can’t perform that action at this time.
