Skip to content

Commit 1cc2377

Browse files
fix(cypher): support property-to-property comparisons in WHERE (DeusData#2591)
Signed-off-by: harshitaajoshi <j.harshitaa06@gmail.com>
1 parent 31ba586 commit 1cc2377

3 files changed

Lines changed: 281 additions & 46 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 115 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ static void expr_free(cbm_expr_t *e) {
791791
safe_str_free(&cur->cond.op);
792792
safe_str_free(&cur->cond.value);
793793
safe_str_free(&cur->cond.coalesce_default);
794+
safe_str_free(&cur->cond.rhs_variable);
795+
safe_str_free(&cur->cond.rhs_property);
794796
for (int i = 0; i < cur->cond.in_value_count; i++) {
795797
safe_str_free(&cur->cond.in_values[i]);
796798
}
@@ -1081,9 +1083,49 @@ static cbm_expr_t *parse_exists_predicate(parser_t *p, bool negated) {
10811083
return expr_leaf(c);
10821084
}
10831085

1086+
/* Free what a condition parser owns when it gives up mid-condition. */
1087+
static void cond_parse_fail_free(cbm_condition_t *c) {
1088+
cond_func_fields_free(c);
1089+
safe_str_free(&c->variable);
1090+
safe_str_free(&c->property);
1091+
safe_str_free(&c->op);
1092+
safe_str_free(&c->coalesce_default);
1093+
safe_str_free(&c->rhs_variable);
1094+
safe_str_free(&c->rhs_property);
1095+
}
1096+
1097+
enum { COND_RHS_REF = 1 };
1098+
1099+
/* RHS operand that names a row value instead of a literal: var.prop, or a bare
1100+
* alias such as a WITH projection (#2591). Returns COND_RHS_REF when one was
1101+
* parsed, 0 when the next token is not one, CBM_NOT_FOUND with p->error set
1102+
* when it is malformed or a function call. */
1103+
static int parse_condition_rhs_ref(parser_t *p, cbm_condition_t *c) {
1104+
if (p->pos + SKIP_ONE < p->count && p->tokens[p->pos + SKIP_ONE].type == TOK_LPAREN) {
1105+
snprintf(p->error, sizeof(p->error),
1106+
"unsupported function '%s' on the right side of a WHERE comparison "
1107+
"(only literals and var.prop are supported)",
1108+
peek(p)->text);
1109+
return CBM_NOT_FOUND;
1110+
}
1111+
if (!check(p, TOK_IDENT)) {
1112+
return 0;
1113+
}
1114+
c->rhs_variable = heap_strdup(advance(p)->text);
1115+
if (match(p, TOK_DOT)) {
1116+
const cbm_token_t *prop = expect(p, TOK_IDENT);
1117+
if (!prop) {
1118+
return CBM_NOT_FOUND;
1119+
}
1120+
c->rhs_property = heap_strdup(prop->text);
1121+
}
1122+
return COND_RHS_REF;
1123+
}
1124+
10841125
/* Parse the operator + value tail shared by every condition subject
10851126
* (var[.prop] and multi-arg functions like coalesce(...)): IS [NOT] NULL,
1086-
* IN [...], or a comparison operator with a literal value. */
1127+
* IN [...], or a comparison operator with a literal value or a property
1128+
* reference (a.x = b.y). */
10871129
static cbm_expr_t *parse_condition_op(parser_t *p, cbm_condition_t *c) {
10881130
/* IS NULL / IS NOT NULL */
10891131
if (check(p, TOK_IS)) {
@@ -1111,10 +1153,7 @@ static cbm_expr_t *parse_condition_op(parser_t *p, cbm_condition_t *c) {
11111153
c->op = parse_comparison_op(p);
11121154
if (!c->op) {
11131155
snprintf(p->error, sizeof(p->error), "unexpected operator at pos %d", peek(p)->pos);
1114-
cond_func_fields_free(c);
1115-
safe_str_free(&c->variable);
1116-
safe_str_free(&c->property);
1117-
safe_str_free(&c->coalesce_default);
1156+
cond_parse_fail_free(c);
11181157
return NULL;
11191158
}
11201159

@@ -1127,13 +1166,11 @@ static cbm_expr_t *parse_condition_op(parser_t *p, cbm_condition_t *c) {
11271166
} else if (check(p, TOK_FALSE)) {
11281167
advance(p);
11291168
c->value = heap_strdup("false");
1130-
} else {
1131-
snprintf(p->error, sizeof(p->error), "expected value at pos %d", peek(p)->pos);
1132-
cond_func_fields_free(c);
1133-
safe_str_free(&c->variable);
1134-
safe_str_free(&c->property);
1135-
safe_str_free(&c->op);
1136-
safe_str_free(&c->coalesce_default);
1169+
} else if (parse_condition_rhs_ref(p, c) <= 0) {
1170+
if (!p->error[0]) {
1171+
snprintf(p->error, sizeof(p->error), "expected value at pos %d", peek(p)->pos);
1172+
}
1173+
cond_parse_fail_free(c);
11371174
return NULL;
11381175
}
11391176

@@ -2227,6 +2264,8 @@ static void free_where(cbm_where_clause_t *w) {
22272264
free(w->conditions[i].in_values);
22282265
safe_str_free(&w->conditions[i].func);
22292266
func_args_free(w->conditions[i].args, w->conditions[i].arg_count);
2267+
safe_str_free(&w->conditions[i].rhs_variable);
2268+
safe_str_free(&w->conditions[i].rhs_property);
22302269
}
22312270
free(w->conditions);
22322271
safe_str_free(&w->op);
@@ -2732,6 +2771,59 @@ static bool eval_comparison_op(const char *op, const char *actual, const char *e
27322771
return false;
27332772
}
27342773

2774+
enum { EXISTS_DIR_OUT = 0, EXISTS_DIR_IN = 1 };
2775+
2776+
/* EXISTS { (var)-[:TYPE]->() }: does the bound node have any edge of the
2777+
* given type in the requested direction? (dir 0=out, 1=in, 2=any) */
2778+
static bool eval_exists_condition(const cbm_condition_t *c, binding_t *b) {
2779+
cbm_node_t *n = binding_get(b, c->variable);
2780+
bool result = false;
2781+
if (n && b->store) {
2782+
cbm_edge_t *edges = NULL;
2783+
int cnt = 0;
2784+
if (c->exists_dir != EXISTS_DIR_IN) { /* outbound or any */
2785+
if (c->value) {
2786+
cbm_store_find_edges_by_source_type(b->store, n->id, c->value, &edges, &cnt);
2787+
} else {
2788+
cbm_store_find_edges_by_source(b->store, n->id, &edges, &cnt);
2789+
}
2790+
result = cnt > 0;
2791+
cbm_store_free_edges(edges, cnt);
2792+
}
2793+
if (!result && c->exists_dir != EXISTS_DIR_OUT) { /* inbound or any */
2794+
edges = NULL;
2795+
cnt = 0;
2796+
if (c->value) {
2797+
cbm_store_find_edges_by_target_type(b->store, n->id, c->value, &edges, &cnt);
2798+
} else {
2799+
cbm_store_find_edges_by_target(b->store, n->id, &edges, &cnt);
2800+
}
2801+
result = cnt > 0;
2802+
cbm_store_free_edges(edges, cnt);
2803+
}
2804+
}
2805+
return c->negated ? !result : result;
2806+
}
2807+
2808+
/* a.x <op> b.y (#2591): the RHS operand is read from the same row. An unbound
2809+
* RHS alias does not filter, like an unbound LHS. A missing (empty) property
2810+
* on either side is NULL and the comparison is false, so two nodes that both
2811+
* lack a property do not compare equal. */
2812+
static bool eval_property_comparison(const cbm_condition_t *c, const char *actual, binding_t *b) {
2813+
cbm_condition_t rhs = {0};
2814+
rhs.variable = c->rhs_variable;
2815+
rhs.property = c->rhs_property;
2816+
const char *expected = resolve_condition_value(&rhs, b);
2817+
if (!expected) {
2818+
return true;
2819+
}
2820+
if (actual[0] == '\0' || expected[0] == '\0') {
2821+
return false;
2822+
}
2823+
bool result = eval_comparison_op(c->op, actual, expected);
2824+
return c->negated ? !result : result;
2825+
}
2826+
27352827
/* Evaluate a WHERE condition against a binding */
27362828
static bool eval_condition(const cbm_condition_t *c, binding_t *b) {
27372829
/* Label test: WHERE n:Label (#241) — compare the bound node's label
@@ -2742,36 +2834,8 @@ static bool eval_condition(const cbm_condition_t *c, binding_t *b) {
27422834
return c->negated ? !result : result;
27432835
}
27442836

2745-
/* EXISTS { (var)-[:TYPE]->() }: does the bound node have any edge of the
2746-
* given type in the requested direction? (dir 0=out, 1=in, 2=any) */
27472837
if (strcmp(c->op, "EXISTS") == 0) {
2748-
cbm_node_t *n = binding_get(b, c->variable);
2749-
bool result = false;
2750-
if (n && b->store) {
2751-
cbm_edge_t *edges = NULL;
2752-
int cnt = 0;
2753-
if (c->exists_dir != 1) { /* outbound or any */
2754-
if (c->value) {
2755-
cbm_store_find_edges_by_source_type(b->store, n->id, c->value, &edges, &cnt);
2756-
} else {
2757-
cbm_store_find_edges_by_source(b->store, n->id, &edges, &cnt);
2758-
}
2759-
result = cnt > 0;
2760-
cbm_store_free_edges(edges, cnt);
2761-
}
2762-
if (!result && c->exists_dir != 0) { /* inbound or any */
2763-
edges = NULL;
2764-
cnt = 0;
2765-
if (c->value) {
2766-
cbm_store_find_edges_by_target_type(b->store, n->id, c->value, &edges, &cnt);
2767-
} else {
2768-
cbm_store_find_edges_by_target(b->store, n->id, &edges, &cnt);
2769-
}
2770-
result = cnt > 0;
2771-
cbm_store_free_edges(edges, cnt);
2772-
}
2773-
}
2774-
return c->negated ? !result : result;
2838+
return eval_exists_condition(c, b);
27752839
}
27762840

27772841
const char *actual = resolve_condition_value(c, b);
@@ -2808,6 +2872,9 @@ static bool eval_condition(const cbm_condition_t *c, binding_t *b) {
28082872
return c->negated ? !result : result;
28092873
}
28102874

2875+
if (c->rhs_variable) {
2876+
return eval_property_comparison(c, actual, b);
2877+
}
28112878
result = eval_comparison_op(c->op, actual, c->value);
28122879
return c->negated ? !result : result;
28132880
}
@@ -2904,11 +2971,12 @@ static bool partial_alias_unbound(binding_t *b, const char *alias) {
29042971
}
29052972

29062973
/* A condition is UNKNOWN while any alias it reads is not bound yet: its own
2907-
* variable, and every function argument's -- a func condition is filed under
2908-
* its first argument's alias, so the b in coalesce(a.x, b.name) is only seen
2909-
* here. A literal-only LHS has nothing to wait for. */
2974+
* variable, the RHS of a property comparison (the b in a.x = b.y), and every
2975+
* function argument's -- a func condition is filed under its first argument's
2976+
* alias, so the b in coalesce(a.x, b.name) is only seen here. A literal-only
2977+
* LHS has nothing to wait for. */
29102978
static cyp_partial_t eval_condition_partial(const cbm_condition_t *c, binding_t *b) {
2911-
if (partial_alias_unbound(b, c->variable)) {
2979+
if (partial_alias_unbound(b, c->variable) || partial_alias_unbound(b, c->rhs_variable)) {
29122980
return CYP_PARTIAL_UNKNOWN;
29132981
}
29142982
for (int i = 0; i < c->arg_count; i++) {
@@ -5885,7 +5953,8 @@ static int cypher_cond_selectivity(const cbm_condition_t *c, const char *var) {
58855953
if (!c || !c->variable || !var || strcmp(c->variable, var) != 0 || c->negated) {
58865954
return 0;
58875955
}
5888-
if (!c->op || strcmp(c->op, "=") != 0 || !c->property) {
5956+
/* a.x = b.y pins no value on either alias, so it seeds nothing. */
5957+
if (!c->op || strcmp(c->op, "=") != 0 || !c->property || c->rhs_variable) {
58895958
return 0;
58905959
}
58915960
return (strcmp(c->property, "name") == 0 || strcmp(c->property, "qualified_name") == 0) ? 3 : 2;

‎src/cypher/cypher.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ typedef struct {
210210
const char *func;
211211
cbm_func_arg_t *args;
212212
int arg_count;
213+
/* Property reference on the RHS, e.g. a.file_path = b.file_path (#2591).
214+
* When rhs_variable is set the comparison reads its operand from the row
215+
* instead of `value`; rhs_property NULL = bare alias. */
216+
const char *rhs_variable;
217+
const char *rhs_property;
213218
} cbm_condition_t;
214219

215220
/* Expression tree for WHERE clause */

0 commit comments

Comments
 (0)