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): apply WITH DISTINCT deduplication (#238)

Martin Vogel3014867cb3c5
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.sh
index 4235c9b97..e107e4e24 100755
--- a/scripts/smoke-test.sh
+++ b/scripts/smoke-test.sh
@@ -165,6 +165,17 @@ if [ "$FOLDER_COUNT" -lt 2 ]; then
fi
echo "OK: $FOLDER_COUNT Folder nodes (init.py didn't clobber them)"
+# 3d-cypher: query_graph Cypher capabilities
+# #238 WITH DISTINCT — all functions share label "Function" → collapses to 1 row.
+CYPHER_WD=$(cli query_graph "{\"project\":\"$PROJECT\",\"query\":\"MATCH (f:Function) WITH DISTINCT f.label AS lbl RETURN lbl\"}")
+WD_ROWS=$(echo "$CYPHER_WD" | python3 -c "import json,sys; d=json.loads(sys.stdin.read()); print(len(d.get('rows',[])))" 2>/dev/null || echo "0")
+if [ "$WD_ROWS" -lt 1 ]; then
+ echo "FAIL: query_graph WITH DISTINCT returned 0 rows"
+ echo "$CYPHER_WD"
+ exit 1
+fi
+echo "OK: query_graph WITH DISTINCT returned $WD_ROWS row(s)"
+
# 3e: delete_project cleanup
cli delete_project "{\"project\":\"$PROJECT\"}" > /dev/null
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index e9e0cf88a..854959386 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -2930,6 +2930,54 @@ static void filter_bindings_where(const cbm_where_clause_t *where, binding_t *vb
*vcount = kept;
}
+/* Build a key from a projected vbinding's value tuple (all WITH output items),
+ * used to detect duplicate rows for WITH DISTINCT (#238). */
+static void with_proj_key(cbm_return_clause_t *wc, binding_t *b, char *key, size_t key_sz) {
+ int kl = 0;
+ key[0] = '\0';
+ char name_buf[CBM_SZ_256];
+ for (int ci = 0; ci < wc->count; ci++) {
+ const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));
+ const char *v = binding_get_virtual(b, alias, NULL);
+ int w = snprintf(key + kl, (kl < (int)key_sz) ? key_sz - (size_t)kl : 0, "%s|", v ? v : "");
+ if (w > 0) {
+ kl += w;
+ }
+ if (kl >= (int)key_sz) {
+ kl = (int)key_sz - SKIP_ONE;
+ break;
+ }
+ }
+}
+
+/* Apply WITH DISTINCT: drop projected rows whose value tuple duplicates an
+ * earlier one, keeping first occurrence (#238 — previously silently ignored). */
+static void with_apply_distinct(cbm_return_clause_t *wc, binding_t *vbindings, int *vcount) {
+ int kept = 0;
+ for (int i = 0; i < *vcount; i++) {
+ char key[CBM_SZ_1K];
+ with_proj_key(wc, &vbindings[i], key, sizeof(key));
+ bool dup = false;
+ for (int j = 0; j < kept; j++) {
+ char pkey[CBM_SZ_1K];
+ with_proj_key(wc, &vbindings[j], pkey, sizeof(pkey));
+ if (strcmp(key, pkey) == 0) {
+ dup = true;
+ break;
+ }
+ }
+ if (dup) {
+ binding_free(&vbindings[i]);
+ } else {
+ if (kept != i) {
+ vbindings[kept] = vbindings[i];
+ }
+ kept++;
+ }
+ }
+ *vcount = kept;
+}
+
static void execute_with_clause(cbm_query_t *q, binding_t **bindings_ptr, int *bind_count_ptr) {
cbm_return_clause_t *wc = q->with_clause;
if (!wc) {
@@ -2955,6 +3003,12 @@ static void execute_with_clause(cbm_query_t *q, binding_t **bindings_ptr, int *b
execute_with_simple(wc, bindings, bind_count, vbindings, &vcount);
}
+ /* WITH DISTINCT: dedup projected rows (no-op for aggregation, which already
+ * collapses to one row per group). */
+ if (wc->distinct) {
+ with_apply_distinct(wc, vbindings, &vcount);
+ }
+
with_sort_skip_limit(wc, vbindings, &vcount);
for (int bi = 0; bi < bind_count; bi++) {
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index d79511373..e800e65e8 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -745,6 +745,31 @@ TEST(cypher_exec_distinct) {
PASS();
}
+/* issue #238: WITH DISTINCT must deduplicate projected rows (previously the
+ * DISTINCT keyword on WITH was parsed but silently ignored). */
+TEST(cypher_exec_with_distinct_issue238) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+
+ /* 4 Function nodes all share label "Function" → WITH DISTINCT collapses to
+ * one row; without dedup this returned 4. */
+ int rc = cbm_cypher_execute(s, "MATCH (f:Function) WITH DISTINCT f.label AS lbl RETURN lbl",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ cbm_cypher_result_free(&r);
+
+ /* Control: without DISTINCT, all 4 rows flow through. */
+ cbm_cypher_result_t r2 = {0};
+ rc = cbm_cypher_execute(s, "MATCH (f:Function) WITH f.label AS lbl RETURN lbl", "test", 0, &r2);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r2.row_count, 4);
+ cbm_cypher_result_free(&r2);
+
+ cbm_store_close(s);
+ PASS();
+}
+
/* --- Ported from cypher_test.go: TestExecuteInlinePropertyFilter --- */
TEST(cypher_exec_inline_props) {
cbm_store_t *s = setup_cypher_store();
@@ -2216,6 +2241,7 @@ SUITE(cypher) {
RUN_TEST(cypher_exec_where_numeric);
/* Go test ports */
RUN_TEST(cypher_exec_distinct);
+ RUN_TEST(cypher_exec_with_distinct_issue238);
RUN_TEST(cypher_exec_inline_props);
RUN_TEST(cypher_parse_where_starts_with);
RUN_TEST(cypher_parse_where_contains);