Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(cypher): support COUNT(DISTINCT x) (#239)

Martin Vogel869341c70940
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.sh
index 5b015ddbc..1228a75b2 100755
--- a/scripts/smoke-test.sh
+++ b/scripts/smoke-test.sh
@@ -196,6 +196,16 @@ if [ "$ALT_ROWS" -lt 1 ]; then
fi
echo "OK: query_graph (n:Function|Module) returned $ALT_ROWS row(s)"
+# #239 count(DISTINCT) — must parse and return a single aggregate row.
+CYPHER_CD=$(cli query_graph "{\"project\":\"$PROJECT\",\"query\":\"MATCH (f:Function) RETURN count(DISTINCT f.label)\"}")
+CD_ROWS=$(echo "$CYPHER_CD" | python3 -c "import json,sys; d=json.loads(sys.stdin.read()); print(len(d.get('rows',[])))" 2>/dev/null || echo "0")
+if [ "$CD_ROWS" -ne 1 ]; then
+ echo "FAIL: query_graph count(DISTINCT) expected 1 row, got $CD_ROWS"
+ echo "$CYPHER_CD"
+ exit 1
+fi
+echo "OK: query_graph count(DISTINCT f.label) returned 1 aggregate row"
+
# 3e: delete_project cleanup
cli delete_project "{\"project\":\"$PROJECT\"}" > /dev/null
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 210cf18a5..48b6feb1f 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -1267,6 +1267,8 @@ static int parse_aggregate_item(parser_t *p, cbm_return_item_t *item) {
cbm_token_type_t ft = peek(p)->type;
advance(p);
expect(p, TOK_LPAREN);
+ /* Optional DISTINCT inside the call: COUNT(DISTINCT x) (#239). */
+ item->distinct = match(p, TOK_DISTINCT);
if (match(p, TOK_STAR)) {
item->variable = heap_strdup("*");
} else {
@@ -2774,6 +2776,19 @@ static bool is_aggregate_func(const char *func) {
strcmp(func, "MIN") == 0 || strcmp(func, "MAX") == 0 || strcmp(func, "COLLECT") == 0);
}
+/* Append `val` to a string list only if not already present — i.e. maintain a
+ * set of distinct values. Used by COUNT(DISTINCT x) (#239). */
+static void distinct_list_add(char ***list, int *count, const char *val) {
+ for (int i = 0; i < *count; i++) {
+ if (strcmp((*list)[i], val) == 0) {
+ return;
+ }
+ }
+ int idx = (*count)++;
+ *list = safe_realloc(*list, (size_t)(idx + SKIP_ONE) * sizeof(char *));
+ (*list)[idx] = heap_strdup(val);
+}
+
/* Sort bindings by a virtual variable using bubble sort */
static void sort_bindings(binding_t *vbindings, int count, const char *key, bool desc) {
for (int i = 0; i < count - SKIP_ONE; i++) {
@@ -2848,6 +2863,8 @@ typedef struct {
double *sums;
int *counts;
double *mins, *maxs;
+ char ***distinct_lists; /* per-item set of seen values for COUNT(DISTINCT) */
+ int *distinct_n; /* per-item distinct count (#239) */
} with_agg_t;
/* Build a group key from non-aggregate WITH items */
@@ -2885,6 +2902,8 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
(*aggs)[found].counts = calloc(wc->count, sizeof(int));
(*aggs)[found].mins = calloc(wc->count, sizeof(double));
(*aggs)[found].maxs = calloc(wc->count, sizeof(double));
+ (*aggs)[found].distinct_lists = calloc(wc->count, sizeof(char **));
+ (*aggs)[found].distinct_n = calloc(wc->count, sizeof(int));
for (int ci = 0; ci < wc->count; ci++) {
(*aggs)[found].mins[ci] = CYP_DBL_MAX;
(*aggs)[found].maxs[ci] = -CYP_DBL_MAX;
@@ -2908,6 +2927,9 @@ static void with_agg_accumulate(with_agg_t *agg, cbm_return_clause_t *wc, bindin
}
agg->counts[ci]++;
const char *raw = binding_get_virtual(b, wc->items[ci].variable, wc->items[ci].property);
+ if (wc->items[ci].distinct && strcmp(wc->items[ci].func, "COUNT") == 0) {
+ distinct_list_add(&agg->distinct_lists[ci], &agg->distinct_n[ci], raw);
+ }
double dv = strtod(raw, NULL);
agg->sums[ci] += dv;
if (dv < agg->mins[ci]) {
@@ -2949,12 +2971,20 @@ static void with_agg_free(with_agg_t *aggs, int agg_cnt, int item_count) {
for (int a = 0; a < agg_cnt; a++) {
for (int ci = 0; ci < item_count; ci++) {
safe_str_free(&aggs[a].group_vals[ci]);
+ if (aggs[a].distinct_lists && aggs[a].distinct_lists[ci]) {
+ for (int j = 0; j < aggs[a].distinct_n[ci]; j++) {
+ free(aggs[a].distinct_lists[ci][j]);
+ }
+ free(aggs[a].distinct_lists[ci]);
+ }
}
free(aggs[a].group_vals);
free(aggs[a].sums);
free(aggs[a].counts);
free(aggs[a].mins);
free(aggs[a].maxs);
+ free(aggs[a].distinct_lists);
+ free(aggs[a].distinct_n);
}
free(aggs);
}
@@ -2985,7 +3015,11 @@ static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings,
const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));
if (wc->items[ci].func) {
char vbuf[CBM_SZ_64];
- with_agg_format(wc->items[ci].func, &aggs[a], ci, vbuf, sizeof(vbuf));
+ if (wc->items[ci].distinct && strcmp(wc->items[ci].func, "COUNT") == 0) {
+ snprintf(vbuf, sizeof(vbuf), "%d", aggs[a].distinct_n[ci]); /* #239 */
+ } else {
+ with_agg_format(wc->items[ci].func, &aggs[a], ci, vbuf, sizeof(vbuf));
+ }
with_add_vbinding_var(&vb, alias, vbuf);
} else {
with_add_vbinding_var(&vb, alias, aggs[a].group_vals[ci]);
@@ -3291,6 +3325,9 @@ static void ret_agg_accumulate(ret_agg_entry_t *entry, cbm_return_clause_t *ret,
entry->collect_lists[ci] =
safe_realloc(entry->collect_lists[ci], (idx + SKIP_ONE) * sizeof(char *));
entry->collect_lists[ci][idx] = heap_strdup(raw);
+ } else if (ret->items[ci].distinct && strcmp(ret->items[ci].func, "COUNT") == 0) {
+ /* COUNT(DISTINCT x): track unique values; emit the set size (#239). */
+ distinct_list_add(&entry->collect_lists[ci], &entry->collect_counts[ci], raw);
}
}
}
@@ -3344,6 +3381,12 @@ static void ret_agg_emit_row(cbm_return_clause_t *ret, ret_agg_entry_t *agg, res
row[ci] = agg->group_vals[ci];
continue;
}
+ if (ret->items[ci].distinct && strcmp(ret->items[ci].func, "COUNT") == 0) {
+ /* COUNT(DISTINCT x) — number of unique values accumulated (#239). */
+ snprintf(bufs[ci], sizeof(bufs[ci]), "%d", agg->collect_counts[ci]);
+ row[ci] = bufs[ci];
+ continue;
+ }
format_agg_value(ret->items[ci].func, agg->counts[ci], agg->sums[ci], agg->mins[ci],
agg->maxs[ci], agg->collect_lists, agg->collect_counts, ci, bufs[ci],
sizeof(bufs[ci]));
diff --git a/src/cypher/cypher.h b/src/cypher/cypher.h
index dedf4c824..b43466219 100644
--- a/src/cypher/cypher.h
+++ b/src/cypher/cypher.h
@@ -237,6 +237,7 @@ typedef struct {
const char *alias; /* NULL if no alias */
const char *func; /* "COUNT", "SUM", "AVG", "MIN", "MAX", "COLLECT",
"toLower", "toUpper", "toString" or NULL */
+ bool distinct; /* COUNT(DISTINCT x) — count unique values (#239) */
cbm_case_expr_t *kase; /* CASE expression (NULL if not CASE) */
} cbm_return_item_t;
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index a88a6ef3d..5a027bfd9 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -802,6 +802,37 @@ TEST(cypher_exec_where_label_test_issue241) {
PASS();
}
+/* issue #239: COUNT(DISTINCT x) — previously a parse error. */
+TEST(cypher_exec_count_distinct_issue239) {
+ cbm_store_t *s = setup_cypher_store();
+
+ /* 4 functions all share label "Function" → COUNT(DISTINCT f.label) = 1. */
+ cbm_cypher_result_t r = {0};
+ int rc =
+ cbm_cypher_execute(s, "MATCH (f:Function) RETURN count(DISTINCT f.label)", "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_STR_EQ(r.rows[0][0], "1");
+ cbm_cypher_result_free(&r);
+
+ /* Non-distinct COUNT counts all 4 occurrences. */
+ cbm_cypher_result_t r2 = {0};
+ rc = cbm_cypher_execute(s, "MATCH (f:Function) RETURN count(f.label)", "test", 0, &r2);
+ ASSERT_EQ(rc, 0);
+ ASSERT_STR_EQ(r2.rows[0][0], "4");
+ cbm_cypher_result_free(&r2);
+
+ /* DISTINCT over the 4 unique function names = 4. */
+ cbm_cypher_result_t r3 = {0};
+ rc = cbm_cypher_execute(s, "MATCH (f:Function) RETURN count(DISTINCT f.name)", "test", 0, &r3);
+ ASSERT_EQ(rc, 0);
+ ASSERT_STR_EQ(r3.rows[0][0], "4");
+ cbm_cypher_result_free(&r3);
+
+ cbm_store_close(s);
+ PASS();
+}
+
/* issue #242: openCypher label alternation in MATCH — (n:A|B). */
TEST(cypher_exec_label_alternation_issue242) {
cbm_store_t *s = setup_cypher_store();
@@ -2298,6 +2329,7 @@ SUITE(cypher) {
RUN_TEST(cypher_exec_with_distinct_issue238);
RUN_TEST(cypher_exec_where_label_test_issue241);
RUN_TEST(cypher_exec_label_alternation_issue242);
+ RUN_TEST(cypher_exec_count_distinct_issue239);
RUN_TEST(cypher_exec_inline_props);
RUN_TEST(cypher_parse_where_starts_with);
RUN_TEST(cypher_parse_where_contains);