Skip to content

Commit 6166934

Browse files
committed
fix(cypher): ignore unbound optional variables in count
Signed-off-by: lorenzozanee <wyz0707@proton.me>
1 parent 59a05eb commit 6166934

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4450,11 +4450,33 @@ static void ret_agg_init_group(ret_agg_entry_t *entry, const char *key, int item
44504450
}
44514451

44524452
/* Accumulate a binding into RETURN aggregation */
4453+
static bool binding_has_value(binding_t *b, const char *var, const char *prop) {
4454+
if (!var || strcmp(var, "*") == 0) {
4455+
return true;
4456+
}
4457+
char full[CBM_SZ_256];
4458+
if (prop) {
4459+
snprintf(full, sizeof(full), "%s.%s", var, prop);
4460+
} else {
4461+
snprintf(full, sizeof(full), "%s", var);
4462+
}
4463+
for (int i = 0; i < b->var_count; i++) {
4464+
if (strcmp(b->var_names[i], full) == 0) {
4465+
return true;
4466+
}
4467+
}
4468+
return binding_get_edge(b, var) != NULL || binding_get(b, var) != NULL;
4469+
}
4470+
44534471
static void ret_agg_accumulate(ret_agg_entry_t *entry, cbm_return_clause_t *ret, binding_t *b) {
44544472
for (int ci = 0; ci < ret->count; ci++) {
44554473
if (!is_aggregate_func(ret->items[ci].func)) {
44564474
continue;
44574475
}
4476+
if (strcmp(ret->items[ci].func, "COUNT") == 0 &&
4477+
!binding_has_value(b, ret->items[ci].variable, ret->items[ci].property)) {
4478+
continue;
4479+
}
44584480
entry->counts[ci]++;
44594481
const char *raw = binding_get_virtual(b, ret->items[ci].variable, ret->items[ci].property);
44604482
double dv = strtod(raw, NULL);

‎tests/test_cypher.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,35 @@ TEST(cypher_exec_count) {
17441744
PASS();
17451745
}
17461746

1747+
TEST(cypher_exec_optional_bound_terminal_count_ignores_unbound) {
1748+
cbm_store_t *s = setup_cypher_store();
1749+
cbm_cypher_result_t r = {0};
1750+
1751+
int rc = cbm_cypher_execute(s,
1752+
"MATCH (f:Function) OPTIONAL MATCH (t)-[:CALLS]->(f) "
1753+
"RETURN f.name, COUNT(t) AS cnt",
1754+
"test", 0, &r);
1755+
ASSERT_EQ(rc, 0);
1756+
ASSERT_EQ(r.row_count, 4);
1757+
1758+
for (int i = 0; i < r.row_count; i++) {
1759+
const char *name = r.rows[i][0];
1760+
const char *count = r.rows[i][1];
1761+
if (strcmp(name, "HandleOrder") == 0) {
1762+
ASSERT_STR_EQ(count, "0");
1763+
} else if (strcmp(name, "ValidateOrder") == 0 || strcmp(name, "SubmitOrder") == 0 ||
1764+
strcmp(name, "LogError") == 0) {
1765+
ASSERT_STR_EQ(count, "1");
1766+
} else {
1767+
ASSERT_TRUE(false);
1768+
}
1769+
}
1770+
1771+
cbm_cypher_result_free(&r);
1772+
cbm_store_close(s);
1773+
PASS();
1774+
}
1775+
17471776
TEST(cypher_exec_limit) {
17481777
cbm_store_t *s = setup_cypher_store();
17491778
cbm_cypher_result_t r = {0};
@@ -4578,6 +4607,7 @@ SUITE(cypher) {
45784607
RUN_TEST(cypher_exec_calls_with_where);
45794608
RUN_TEST(cypher_exec_inbound);
45804609
RUN_TEST(cypher_exec_count);
4610+
RUN_TEST(cypher_exec_optional_bound_terminal_count_ignores_unbound);
45814611
RUN_TEST(cypher_exec_limit);
45824612
RUN_TEST(cypher_exec_order_by);
45834613
RUN_TEST(cypher_exec_variable_length);

0 commit comments

Comments
 (0)