-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtransform_expr_predicate.c
More file actions
446 lines (382 loc) · 20 KB
/
transform_expr_predicate.c
File metadata and controls
446 lines (382 loc) · 20 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* transform_expr_predicate.c
* Predicate expression transformations for Cypher queries
*
* This file contains transformations for predicate expressions:
* - EXISTS { pattern } - pattern existence check
* - EXISTS(n.property) - property existence check
* - all/any/none/single(x IN list WHERE predicate) - list predicates
* - reduce(acc = initial, x IN list | expr) - list reduction
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "transform/cypher_transform.h"
#include "transform/transform_functions.h"
#include "transform/transform_helpers.h"
#include "parser/cypher_ast.h"
#include "parser/cypher_debug.h"
/* Transform EXISTS expression */
int transform_exists_expression(cypher_transform_context *ctx, cypher_exists_expr *exists_expr)
{
CYPHER_DEBUG("Transforming EXISTS expression");
if (!exists_expr) {
ctx->has_error = true;
ctx->error_message = strdup("Invalid EXISTS expression");
return -1;
}
switch (exists_expr->expr_type) {
case EXISTS_TYPE_PATTERN:
{
CYPHER_DEBUG("Transforming EXISTS pattern expression");
if (!exists_expr->expr.pattern || exists_expr->expr.pattern->count == 0) {
ctx->has_error = true;
ctx->error_message = strdup("EXISTS pattern expression is empty");
return -1;
}
/* Generate SQL EXISTS subquery with pattern matching */
append_sql(ctx, "EXISTS (");
/* For each pattern in the EXISTS clause, we need to generate a subquery */
/* that checks if the pattern exists in the database */
ast_node *pattern = exists_expr->expr.pattern->items[0];
if (pattern->type == AST_NODE_PATH) {
cypher_path *path = (cypher_path*)pattern;
/* Simple pattern like (n)-[r]->(m) */
if (path->elements && path->elements->count >= 1) {
/* Start with SELECT 1 to make it an existence check */
append_sql(ctx, "SELECT 1 FROM ");
bool first_table = true;
char node_aliases[10][32]; /* Support up to 10 nodes in pattern */
bool node_is_external[10]; /* Track which nodes are from outer context */
int node_count = 0;
/* Process each element in the path */
for (int i = 0; i < path->elements->count; i++) {
ast_node *element = path->elements->items[i];
if (element->type == AST_NODE_NODE_PATTERN) {
cypher_node_pattern *node = (cypher_node_pattern*)element;
/* Check if this node variable exists in outer context */
const char *outer_alias = NULL;
if (node->variable) {
outer_alias = transform_var_get_alias(ctx->var_ctx, node->variable);
}
if (outer_alias) {
/* Use alias from outer query - don't add to FROM */
strncpy(node_aliases[node_count], outer_alias,
sizeof(node_aliases[node_count]) - 1);
node_aliases[node_count][sizeof(node_aliases[node_count]) - 1] = '\0';
node_is_external[node_count] = true;
} else {
/* Generate new alias and add to FROM */
if (!first_table) {
append_sql(ctx, ", ");
}
snprintf(node_aliases[node_count], sizeof(node_aliases[node_count]),
"n%d", node_count);
append_sql(ctx, "nodes AS %s", node_aliases[node_count]);
node_is_external[node_count] = false;
first_table = false;
}
node_count++;
} else if (element->type == AST_NODE_REL_PATTERN && i > 0) {
/* Relationship pattern: -[variable:TYPE]-> */
if (!first_table) {
append_sql(ctx, ", ");
}
append_sql(ctx, "edges AS e%d", i/2); /* Relationships are at odd indices */
first_table = false;
}
}
/* Add WHERE clause for joins and constraints */
append_sql(ctx, " WHERE ");
bool first_condition = true;
int rel_index = 0;
/* Generate join conditions between nodes and relationships */
for (int i = 0; i < path->elements->count; i++) {
ast_node *element = path->elements->items[i];
if (element->type == AST_NODE_REL_PATTERN && i > 0 && i < path->elements->count - 1) {
cypher_rel_pattern *rel = (cypher_rel_pattern*)element;
if (!first_condition) {
append_sql(ctx, " AND ");
}
/* Join source node with relationship, respecting direction */
int left_node = i / 2;
int right_node = left_node + 1;
if (rel->left_arrow && !rel->right_arrow) {
/* Incoming: <-[]- means edge goes right_node -> left_node */
append_sql(ctx, "e%d.source_id = %s.id AND e%d.target_id = %s.id",
rel_index, node_aliases[right_node],
rel_index, node_aliases[left_node]);
} else if (!rel->left_arrow && !rel->right_arrow) {
/* Undirected: -[]- match either direction */
append_sql(ctx, "((e%d.source_id = %s.id AND e%d.target_id = %s.id) OR (e%d.source_id = %s.id AND e%d.target_id = %s.id))",
rel_index, node_aliases[left_node],
rel_index, node_aliases[right_node],
rel_index, node_aliases[right_node],
rel_index, node_aliases[left_node]);
} else {
/* Outgoing: -[]-> (default) */
append_sql(ctx, "e%d.source_id = %s.id AND e%d.target_id = %s.id",
rel_index, node_aliases[left_node],
rel_index, node_aliases[right_node]);
}
/* Add relationship type constraint if specified */
if (rel->type) {
append_sql(ctx, " AND e%d.type = ", rel_index);
append_string_literal(ctx, rel->type);
}
rel_index++;
first_condition = false;
} else if (element->type == AST_NODE_NODE_PATTERN) {
cypher_node_pattern *node = (cypher_node_pattern*)element;
/* Add label constraints if specified - one condition per label */
if (has_labels(node)) {
for (int j = 0; j < node->labels->count; j++) {
const char *label = get_label_string(node->labels->items[j]);
if (label) {
if (!first_condition) {
append_sql(ctx, " AND ");
}
int current_node = (i == 0) ? 0 : i / 2;
append_sql(ctx, "EXISTS (SELECT 1 FROM node_labels WHERE node_id = %s.id AND label = ",
node_aliases[current_node]);
append_string_literal(ctx, label);
append_sql(ctx, ")");
first_condition = false;
}
}
}
}
}
} else {
/* Empty pattern - should not happen */
append_sql(ctx, "SELECT 0");
}
} else {
/* Unsupported pattern type */
ctx->has_error = true;
ctx->error_message = strdup("Unsupported pattern type in EXISTS expression");
return -1;
}
append_sql(ctx, ")");
return 0;
}
case EXISTS_TYPE_PROPERTY:
{
CYPHER_DEBUG("Transforming EXISTS property expression");
if (!exists_expr->expr.property) {
ctx->has_error = true;
ctx->error_message = strdup("EXISTS property expression is empty");
return -1;
}
/* Generate property existence check */
/* This should be a property access like n.property */
if (exists_expr->expr.property->type == AST_NODE_PROPERTY) {
cypher_property *prop = (cypher_property*)exists_expr->expr.property;
if (prop->expr->type == AST_NODE_IDENTIFIER) {
cypher_identifier *id = (cypher_identifier*)prop->expr;
const char *alias = transform_var_get_alias(ctx->var_ctx, id->name);
if (!alias) {
ctx->has_error = true;
char error[256];
snprintf(error, sizeof(error), "Unknown variable in EXISTS property: %s", id->name);
ctx->error_message = strdup(error);
return -1;
}
/* Multi-graph support: get graph prefix for table references */
const char *graph = transform_var_get_graph(ctx->var_ctx, id->name);
const char *gprefix = "";
char gprefix_buf[64] = "";
if (graph && graph[0] != '\0') {
snprintf(gprefix_buf, sizeof(gprefix_buf), "%s.", graph);
gprefix = gprefix_buf;
}
/* Generate SQL to check if property exists */
append_sql(ctx, "(");
append_sql(ctx, "EXISTS (SELECT 1 FROM %snode_props_text npt JOIN %sproperty_keys pk ON npt.key_id = pk.id WHERE npt.node_id = %s.id AND pk.key = ", gprefix, gprefix, alias);
append_string_literal(ctx, prop->property_name);
append_sql(ctx, ") OR ");
append_sql(ctx, "EXISTS (SELECT 1 FROM %snode_props_int npi JOIN %sproperty_keys pk ON npi.key_id = pk.id WHERE npi.node_id = %s.id AND pk.key = ", gprefix, gprefix, alias);
append_string_literal(ctx, prop->property_name);
append_sql(ctx, ") OR ");
append_sql(ctx, "EXISTS (SELECT 1 FROM %snode_props_real npr JOIN %sproperty_keys pk ON npr.key_id = pk.id WHERE npr.node_id = %s.id AND pk.key = ", gprefix, gprefix, alias);
append_string_literal(ctx, prop->property_name);
append_sql(ctx, ") OR ");
append_sql(ctx, "EXISTS (SELECT 1 FROM %snode_props_bool npb JOIN %sproperty_keys pk ON npb.key_id = pk.id WHERE npb.node_id = %s.id AND pk.key = ", gprefix, gprefix, alias);
append_string_literal(ctx, prop->property_name);
append_sql(ctx, ")");
append_sql(ctx, ")");
return 0;
} else {
ctx->has_error = true;
ctx->error_message = strdup("EXISTS property must reference a variable");
return -1;
}
} else {
ctx->has_error = true;
ctx->error_message = strdup("EXISTS property expression must be a property access");
return -1;
}
}
default:
ctx->has_error = true;
ctx->error_message = strdup("Unknown EXISTS expression type");
return -1;
}
}
/* Transform list predicate: all/any/none/single(x IN list WHERE predicate)
*
* SQL generation:
* - all(x IN list WHERE pred) -> (SELECT COUNT(*) = json_array_length(list) FROM json_each(list) WHERE pred)
* - any(x IN list WHERE pred) -> (SELECT COUNT(*) > 0 FROM json_each(list) WHERE pred)
* - none(x IN list WHERE pred) -> (SELECT COUNT(*) = 0 FROM json_each(list) WHERE pred)
* - single(x IN list WHERE pred) -> (SELECT COUNT(*) = 1 FROM json_each(list) WHERE pred)
*/
int transform_list_predicate(cypher_transform_context *ctx, cypher_list_predicate *pred)
{
CYPHER_DEBUG("Transforming list predicate type %d", pred->pred_type);
if (!pred || !pred->variable || !pred->list_expr || !pred->predicate) {
ctx->has_error = true;
ctx->error_message = strdup("Invalid list predicate");
return -1;
}
/* Save the old alias if this variable name already exists */
const char *old_alias = transform_var_get_alias(ctx->var_ctx, pred->variable);
char *saved_alias = old_alias ? strdup(old_alias) : NULL;
/* Register the predicate variable to map to json_each.value */
/* Register in unified system as projected */
transform_var_register_projected(ctx->var_ctx, pred->variable, "json_each.value");
/* For 'all' predicate, we need to capture the list expression to compare count */
if (pred->pred_type == LIST_PRED_ALL) {
/* Build: (SELECT COUNT(*) = json_array_length(list) FROM json_each(list) WHERE pred) */
append_sql(ctx, "(SELECT COUNT(*) = json_array_length(");
if (transform_expression(ctx, pred->list_expr) < 0) {
if (saved_alias) free(saved_alias);
return -1;
}
append_sql(ctx, ") FROM json_each(");
if (transform_expression(ctx, pred->list_expr) < 0) {
if (saved_alias) free(saved_alias);
return -1;
}
append_sql(ctx, ") WHERE ");
if (transform_expression(ctx, pred->predicate) < 0) {
if (saved_alias) free(saved_alias);
return -1;
}
append_sql(ctx, ")");
} else {
/* Build: (SELECT COUNT(*) <op> <n> FROM json_each(list) WHERE pred) */
append_sql(ctx, "(SELECT COUNT(*) ");
switch (pred->pred_type) {
case LIST_PRED_ANY:
append_sql(ctx, "> 0");
break;
case LIST_PRED_NONE:
append_sql(ctx, "= 0");
break;
case LIST_PRED_SINGLE:
append_sql(ctx, "= 1");
break;
default:
break;
}
append_sql(ctx, " FROM json_each(");
if (transform_expression(ctx, pred->list_expr) < 0) {
if (saved_alias) free(saved_alias);
return -1;
}
append_sql(ctx, ") WHERE ");
if (transform_expression(ctx, pred->predicate) < 0) {
if (saved_alias) free(saved_alias);
return -1;
}
append_sql(ctx, ")");
}
/* Restore the old alias if we saved one */
if (saved_alias) {
/* Restore in unified system */
transform_var_register_projected(ctx->var_ctx, pred->variable, saved_alias);
free(saved_alias);
}
return 0;
}
/* Transform reduce expression: reduce(acc = initial, x IN list | expr)
*
* SQL generation using recursive CTE:
* (WITH RECURSIVE _reduce_N AS (
* SELECT initial AS acc, 0 AS idx
* UNION ALL
* SELECT (expression), idx + 1
* FROM _reduce_N, json_each(list)
* WHERE idx = json_each.key
* )
* SELECT acc FROM _reduce_N WHERE idx = json_array_length(list))
*/
int transform_reduce_expr(cypher_transform_context *ctx, cypher_reduce_expr *reduce)
{
static int reduce_counter = 0;
CYPHER_DEBUG("Transforming reduce expression");
if (!reduce || !reduce->accumulator || !reduce->initial_value ||
!reduce->variable || !reduce->list_expr || !reduce->expression) {
ctx->has_error = true;
ctx->error_message = strdup("Invalid reduce expression");
return -1;
}
/* Generate unique CTE name */
char cte_name[32];
snprintf(cte_name, sizeof(cte_name), "_reduce_%d", reduce_counter++);
/* Save existing aliases for accumulator and variable names */
const char *old_acc_alias = transform_var_get_alias(ctx->var_ctx, reduce->accumulator);
const char *old_var_alias = transform_var_get_alias(ctx->var_ctx, reduce->variable);
char *saved_acc_alias = old_acc_alias ? strdup(old_acc_alias) : NULL;
char *saved_var_alias = old_var_alias ? strdup(old_var_alias) : NULL;
append_sql(ctx, "(WITH RECURSIVE %s AS (SELECT ", cte_name);
/* Transform initial value */
if (transform_expression(ctx, reduce->initial_value) < 0) {
if (saved_acc_alias) free(saved_acc_alias);
if (saved_var_alias) free(saved_var_alias);
return -1;
}
append_sql(ctx, " AS acc, 0 AS idx UNION ALL SELECT (");
/* Register variables for the expression:
* - accumulator maps to cte_name.acc
* - variable maps to json_each.value
*/
char acc_ref[64];
snprintf(acc_ref, sizeof(acc_ref), "%s.acc", cte_name);
/* Register in unified system */
transform_var_register_projected(ctx->var_ctx, reduce->accumulator, acc_ref);
transform_var_register_projected(ctx->var_ctx, reduce->variable, "json_each.value");
/* Transform the expression that computes new accumulator value */
if (transform_expression(ctx, reduce->expression) < 0) {
if (saved_acc_alias) free(saved_acc_alias);
if (saved_var_alias) free(saved_var_alias);
return -1;
}
append_sql(ctx, "), idx + 1 FROM %s, json_each(", cte_name);
/* Transform list expression */
if (transform_expression(ctx, reduce->list_expr) < 0) {
if (saved_acc_alias) free(saved_acc_alias);
if (saved_var_alias) free(saved_var_alias);
return -1;
}
append_sql(ctx, ") WHERE %s.idx = json_each.key) SELECT acc FROM %s WHERE idx = json_array_length(",
cte_name, cte_name);
/* Transform list expression again for length check */
if (transform_expression(ctx, reduce->list_expr) < 0) {
if (saved_acc_alias) free(saved_acc_alias);
if (saved_var_alias) free(saved_var_alias);
return -1;
}
append_sql(ctx, "))");
/* Restore old aliases */
if (saved_acc_alias) {
transform_var_register_projected(ctx->var_ctx, reduce->accumulator, saved_acc_alias);
free(saved_acc_alias);
}
if (saved_var_alias) {
transform_var_register_projected(ctx->var_ctx, reduce->variable, saved_var_alias);
free(saved_var_alias);
}
return 0;
}