Skip to content

Commit 937ad47

Browse files
committed
fix(cypher): defer WHERE until referenced bindings exist
Signed-off-by: lorenzozanee <wyz0707@proton.me>
1 parent 96c3f41 commit 937ad47

2 files changed

Lines changed: 139 additions & 2 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4145,7 +4145,54 @@ static void execute_with_simple(cbm_return_clause_t *wc, binding_t *bindings, in
41454145
}
41464146
}
41474147

4148-
/* Apply post-WITH WHERE filter */
4148+
static bool condition_bindings_available(const cbm_condition_t *condition, binding_t *binding) {
4149+
if (condition->variable && !binding_get(binding, condition->variable) &&
4150+
!binding_get_edge(binding, condition->variable)) {
4151+
return false;
4152+
}
4153+
for (int i = 0; i < condition->arg_count; i++) {
4154+
const char *var = condition->args[i].variable;
4155+
if (var && !binding_get(binding, var) && !binding_get_edge(binding, var)) {
4156+
return false;
4157+
}
4158+
}
4159+
return true;
4160+
}
4161+
4162+
static bool expr_bindings_available(const cbm_expr_t *expr, binding_t *binding) {
4163+
if (!expr) {
4164+
return true;
4165+
}
4166+
switch (expr->type) {
4167+
case EXPR_CONDITION:
4168+
return condition_bindings_available(&expr->cond, binding);
4169+
case EXPR_AND:
4170+
case EXPR_OR:
4171+
case EXPR_XOR:
4172+
return expr_bindings_available(expr->left, binding) &&
4173+
expr_bindings_available(expr->right, binding);
4174+
case EXPR_NOT:
4175+
return expr_bindings_available(expr->left, binding);
4176+
}
4177+
return false;
4178+
}
4179+
4180+
static bool where_bindings_available(const cbm_where_clause_t *where, binding_t *binding) {
4181+
if (!where) {
4182+
return true;
4183+
}
4184+
if (where->root) {
4185+
return expr_bindings_available(where->root, binding);
4186+
}
4187+
for (int i = 0; i < where->count; i++) {
4188+
if (!condition_bindings_available(&where->conditions[i], binding)) {
4189+
return false;
4190+
}
4191+
}
4192+
return true;
4193+
}
4194+
4195+
/* Apply a WHERE filter after its referenced variables have been bound. */
41494196
static void filter_bindings_where(const cbm_where_clause_t *where, binding_t *vbindings,
41504197
int *vcount) {
41514198
int kept = 0;
@@ -5149,7 +5196,8 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
51495196
binding_t b = {0};
51505197
b.store = store;
51515198
binding_set(&b, var_name, &scanned[i]);
5152-
bool pass = !q->where || eval_where(q->where, &b);
5199+
bool pass = !q->where || !where_bindings_available(q->where, &b) ||
5200+
eval_where(q->where, &b);
51535201
if (pass) {
51545202
bindings[bind_count++] = b;
51555203
} else {

‎tests/test_cypher.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,32 @@ TEST(cypher_exists_has_outgoing_calls) {
17571757
ASSERT_EQ(rc, 0);
17581758
ASSERT_EQ(r.row_count, 2);
17591759
cbm_cypher_result_free(&r);
1760+
1761+
rc = cbm_cypher_execute(s,
1762+
"MATCH (a)-[:CALLS]->(b) WHERE b.name <> \"SubmitOrder\" "
1763+
"RETURN a.name, b.name",
1764+
"test", 0, &r);
1765+
ASSERT_EQ(rc, 0);
1766+
ASSERT_EQ(r.row_count, 2);
1767+
cbm_cypher_result_free(&r);
1768+
1769+
rc = cbm_cypher_execute(s,
1770+
"MATCH (a)-[:CALLS]->(b) WHERE b.name = \"SubmitOrder\" OR "
1771+
"NOT b.name = \"SubmitOrder\" RETURN count(*)",
1772+
"test", 0, &r);
1773+
ASSERT_EQ(rc, 0);
1774+
ASSERT_EQ(r.row_count, 1);
1775+
ASSERT_STR_EQ(r.rows[0][0], "3");
1776+
cbm_cypher_result_free(&r);
1777+
1778+
rc = cbm_cypher_execute(s,
1779+
"MATCH (a)-[:CALLS]->(b) WITH a, b WHERE NOT b.name = "
1780+
"\"SubmitOrder\" RETURN a.name, b.name",
1781+
"test", 0, &r);
1782+
ASSERT_EQ(rc, 0);
1783+
ASSERT_EQ(r.row_count, 2);
1784+
cbm_cypher_result_free(&r);
1785+
17601786
cbm_store_close(s);
17611787
PASS();
17621788
}
@@ -3150,6 +3176,65 @@ TEST(cypher_exec_where_not) {
31503176
PASS();
31513177
}
31523178

3179+
TEST(cypher_exec_where_not_target_node) {
3180+
cbm_store_t *s = setup_cypher_store();
3181+
cbm_cypher_result_t r = {0};
3182+
int rc = cbm_cypher_execute(s,
3183+
"MATCH (a)-[:CALLS]->(b) WHERE NOT b.name = \"SubmitOrder\" "
3184+
"RETURN a.name, b.name",
3185+
"test", 0, &r);
3186+
ASSERT_EQ(rc, 0);
3187+
ASSERT_EQ(r.row_count, 2);
3188+
cbm_cypher_result_free(&r);
3189+
cbm_store_close(s);
3190+
PASS();
3191+
}
3192+
3193+
TEST(cypher_exec_where_not_target_relationship) {
3194+
cbm_store_t *s = setup_cypher_multi_edge_store();
3195+
cbm_cypher_result_t r = {0};
3196+
int rc = cbm_cypher_execute(s,
3197+
"MATCH (a)-[r:HTTP_CALLS]->(b) WHERE NOT r.url_path = "
3198+
"\"/health\" RETURN r.url_path",
3199+
"testproj", 0, &r);
3200+
ASSERT_EQ(rc, 0);
3201+
ASSERT_EQ(r.row_count, 1);
3202+
ASSERT_STR_EQ(r.rows[0][0], "/api/orders");
3203+
cbm_cypher_result_free(&r);
3204+
cbm_store_close(s);
3205+
PASS();
3206+
}
3207+
3208+
TEST(cypher_exec_where_not_source_node) {
3209+
cbm_store_t *s = setup_cypher_store();
3210+
cbm_cypher_result_t r = {0};
3211+
int rc = cbm_cypher_execute(s,
3212+
"MATCH (a)-[:CALLS]->(b) WHERE NOT a.name = "
3213+
"\"HandleOrder\" RETURN a.name, b.name",
3214+
"test", 0, &r);
3215+
ASSERT_EQ(rc, 0);
3216+
ASSERT_EQ(r.row_count, 1);
3217+
ASSERT_STR_EQ(r.rows[0][0], "ValidateOrder");
3218+
cbm_cypher_result_free(&r);
3219+
cbm_store_close(s);
3220+
PASS();
3221+
}
3222+
3223+
TEST(cypher_exec_where_not_target_node_count) {
3224+
cbm_store_t *s = setup_cypher_store();
3225+
cbm_cypher_result_t r = {0};
3226+
int rc = cbm_cypher_execute(s,
3227+
"MATCH (a)-[:CALLS]->(b) WHERE NOT b.name = "
3228+
"\"SubmitOrder\" RETURN count(*)",
3229+
"test", 0, &r);
3230+
ASSERT_EQ(rc, 0);
3231+
ASSERT_EQ(r.row_count, 1);
3232+
ASSERT_STR_EQ(r.rows[0][0], "2");
3233+
cbm_cypher_result_free(&r);
3234+
cbm_store_close(s);
3235+
PASS();
3236+
}
3237+
31533238
TEST(cypher_exec_where_in) {
31543239
cbm_store_t *s = setup_cypher_store();
31553240
cbm_cypher_result_t r = {0};
@@ -5005,6 +5090,10 @@ SUITE(cypher) {
50055090
RUN_TEST(cypher_exec_where_neq_bang);
50065091
RUN_TEST(cypher_exec_where_ends_with);
50075092
RUN_TEST(cypher_exec_where_not);
5093+
RUN_TEST(cypher_exec_where_not_target_node);
5094+
RUN_TEST(cypher_exec_where_not_target_relationship);
5095+
RUN_TEST(cypher_exec_where_not_source_node);
5096+
RUN_TEST(cypher_exec_where_not_target_node_count);
50085097
RUN_TEST(cypher_exec_where_in);
50095098
RUN_TEST(cypher_exec_where_not_in);
50105099
RUN_TEST(cypher_exec_where_is_null);

0 commit comments

Comments
 (0)