Skip to content

Commit 8b03974

Browse files
fix(cypher): preserve node properties through WITH aggregation
A node group variable carried through a WITH aggregation (e.g. `WITH g, count(*) AS c RETURN g.file_path`) returned blank for every property except its name: the carried virtual binding held only the group key (the node's name) and lacked a store handle, so node_prop() could neither read other fields nor compute degrees. Fix: capture the node id of a bare node group-var in with_agg_find_or_create and tag the carried virtual binding with it; in node_prop(), when such a stub (id set, string fields unpopulated) is asked for a missing property, re-fetch the full node via cbm_store_find_node_by_id and project it. Also propagate the store onto virtual bindings so node_prop can re-fetch and compute degrees. The stub gate is heuristic but never yields a wrong value — worst case is one redundant indexed lookup. Adds regression test cypher_exec_with_node_groupvar_prop. Signed-off-by: Kris Kersey <kris@kerseyfabrications.com>
1 parent e599df1 commit 8b03974

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,15 +2061,18 @@ static const char *node_string_field(const cbm_node_t *n, const char *prop) {
20612061
/* Get node property by name.
20622062
* store may be NULL; only needed for virtual degree properties. */
20632063
static const char *json_extract_prop(const char *json, const char *key, char *buf, size_t buf_sz);
2064+
static void node_fields_free(cbm_node_t *n); /* defined below; used by the stub re-fetch */
20642065

20652066
static const char *node_prop(const cbm_node_t *n, const char *prop, cbm_store_t *store) {
20662067
if (!n || !prop) {
20672068
return "";
20682069
}
20692070
const char *str = node_string_field(n, prop);
2070-
if (str) {
2071+
if (str && str[0]) {
20712072
return str;
20722073
}
2074+
/* Note: a string field that exists but is empty ("") falls through here so a
2075+
* WITH-aggregation node stub (below) can re-fetch it. */
20732076
/* Computed and JSON-derived values live in rotating thread-local buffers:
20742077
* a single row (or an ORDER-BY comparison) reads several of these before any
20752078
* of them is copied out, so returning one shared static buffer would alias
@@ -2107,6 +2110,40 @@ static const char *node_prop(const cbm_node_t *n, const char *prop, cbm_store_t
21072110
return v;
21082111
}
21092112
}
2113+
/* WITH aggregation carries a node group var by id + name only (the group key
2114+
* is the node name), so every other property is absent on the stub. Detect
2115+
* the stub (id set, but the full string fields were never populated) and
2116+
* re-fetch the node so RETURN g.file_path / g.label / g.<metric> project
2117+
* correctly instead of returning blank. The gate is heuristic, not an exact
2118+
* stub discriminator: a real bound node with NULL label AND file_path would
2119+
* also match, but in that case the worst case is one redundant indexed fetch
2120+
* that returns the same value — never a wrong result. */
2121+
if (store && n->id > 0 && !n->file_path && !n->label) {
2122+
cbm_node_t full = {0};
2123+
if (cbm_store_find_node_by_id(store, n->id, &full) == CBM_STORE_OK) {
2124+
const char *res = NULL;
2125+
const char *rv = node_string_field(&full, prop);
2126+
if (rv && rv[0]) {
2127+
snprintf(out, CBM_SZ_512, "%s", rv);
2128+
res = out;
2129+
} else if (strcmp(prop, "start_line") == 0) {
2130+
snprintf(out, CBM_SZ_512, "%d", full.start_line);
2131+
res = out;
2132+
} else if (strcmp(prop, "end_line") == 0) {
2133+
snprintf(out, CBM_SZ_512, "%d", full.end_line);
2134+
res = out;
2135+
} else if (full.properties_json && full.properties_json[0] == '{') {
2136+
const char *jv = json_extract_prop(full.properties_json, prop, out, CBM_SZ_512);
2137+
if (jv && jv[0]) {
2138+
res = out;
2139+
}
2140+
}
2141+
node_fields_free(&full);
2142+
if (res) {
2143+
return res;
2144+
}
2145+
}
2146+
}
21102147
return "";
21112148
}
21122149

@@ -2550,6 +2587,9 @@ static void rb_add_row(result_builder_t *rb, const char **values) {
25502587
/* ── Binding virtual variables (for WITH clause) ──────────────── */
25512588

25522589
static const char *binding_get_virtual(binding_t *b, const char *var, const char *prop) {
2590+
if (!var) {
2591+
return "";
2592+
}
25532593
/* Check virtual vars first (from WITH projection) */
25542594
char full[CBM_SZ_256];
25552595
if (prop) {
@@ -3406,8 +3446,9 @@ typedef struct {
34063446
double *sums;
34073447
int *counts;
34083448
double *mins, *maxs;
3409-
char ***distinct_lists; /* per-item set of seen values for COUNT(DISTINCT) */
3410-
int *distinct_n; /* per-item distinct count (#239) */
3449+
char ***distinct_lists; /* per-item set of seen values for COUNT(DISTINCT) */
3450+
int *distinct_n; /* per-item distinct count (#239) */
3451+
int64_t *group_node_ids; /* per-item node id when the group var is a node (0 = not) */
34113452
} with_agg_t;
34123453

34133454
/* Build a group key from non-aggregate WITH items */
@@ -3447,6 +3488,7 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
34473488
(*aggs)[found].maxs = calloc(wc->count, sizeof(double));
34483489
(*aggs)[found].distinct_lists = calloc(wc->count, sizeof(char **));
34493490
(*aggs)[found].distinct_n = calloc(wc->count, sizeof(int));
3491+
(*aggs)[found].group_node_ids = calloc(wc->count, sizeof(int64_t));
34503492
for (int ci = 0; ci < wc->count; ci++) {
34513493
(*aggs)[found].mins[ci] = CYP_DBL_MAX;
34523494
(*aggs)[found].maxs[ci] = -CYP_DBL_MAX;
@@ -3458,6 +3500,15 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
34583500
}
34593501
const char *v = binding_get_virtual(b, wc->items[ci].variable, wc->items[ci].property);
34603502
(*aggs)[found].group_vals[ci] = heap_strdup(v);
3503+
/* If this group item is a bare node variable, remember its id so the
3504+
* carried virtual var can re-fetch any property (group_vals holds only
3505+
* the name). */
3506+
if (!wc->items[ci].property && wc->items[ci].variable) {
3507+
cbm_node_t *gn = binding_get(b, wc->items[ci].variable);
3508+
if (gn) {
3509+
(*aggs)[found].group_node_ids[ci] = gn->id;
3510+
}
3511+
}
34613512
}
34623513
return found;
34633514
}
@@ -3528,6 +3579,7 @@ static void with_agg_free(with_agg_t *aggs, int agg_cnt, int item_count) {
35283579
free(aggs[a].maxs);
35293580
free(aggs[a].distinct_lists);
35303581
free(aggs[a].distinct_n);
3582+
free(aggs[a].group_node_ids);
35313583
}
35323584
free(aggs);
35333585
}
@@ -3553,6 +3605,9 @@ static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings,
35533605
}
35543606
for (int a = 0; a < agg_cnt; a++) {
35553607
binding_t vb = {0};
3608+
/* Carry the store so node_prop can re-fetch a carried node's properties
3609+
* (and compute in_degree/out_degree) on the projected virtual binding. */
3610+
vb.store = (bind_count > 0) ? bindings[0].store : NULL;
35563611
for (int ci = 0; ci < wc->count; ci++) {
35573612
char name_buf[CBM_SZ_256];
35583613
const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));
@@ -3566,6 +3621,11 @@ static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings,
35663621
with_add_vbinding_var(&vb, alias, vbuf);
35673622
} else {
35683623
with_add_vbinding_var(&vb, alias, aggs[a].group_vals[ci]);
3624+
/* Tag the carried virtual var with the node id (when the group
3625+
* var is a node) so node_prop can re-fetch its full properties. */
3626+
if (aggs[a].group_node_ids[ci] > 0 && vb.var_count > 0) {
3627+
vb.var_nodes[vb.var_count - 1].id = aggs[a].group_node_ids[ci];
3628+
}
35693629
}
35703630
}
35713631
(*vbindings)[(*vcount)++] = vb;
@@ -3578,6 +3638,7 @@ static void execute_with_simple(cbm_return_clause_t *wc, binding_t *bindings, in
35783638
binding_t *vbindings, int *vcount) {
35793639
for (int bi = 0; bi < bind_count; bi++) {
35803640
binding_t vb = {0};
3641+
vb.store = bindings[bi].store; /* so node_prop can re-fetch / compute on the projection */
35813642
for (int ci = 0; ci < wc->count; ci++) {
35823643
char name_buf[CBM_SZ_256];
35833644
const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));

‎tests/test_cypher.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,27 @@ TEST(cypher_exec_with_count) {
21832183
PASS();
21842184
}
21852185

2186+
/* Regression: a bare node group-var carried through WITH aggregation must project
2187+
* its real properties (not blank). Pre-fix, the carried var held only the node
2188+
* name, so RETURN g.file_path returned "". */
2189+
TEST(cypher_exec_with_node_groupvar_prop) {
2190+
cbm_store_t *s = setup_cypher_store();
2191+
cbm_cypher_result_t r = {0};
2192+
int rc = cbm_cypher_execute(s,
2193+
"MATCH (f:Function)-[:CALLS]->(g:Function) "
2194+
"WHERE g.name = \"ValidateOrder\" "
2195+
"WITH g, COUNT(*) AS c "
2196+
"RETURN g.file_path, g.name, c",
2197+
"test", 0, &r);
2198+
ASSERT_EQ(rc, 0);
2199+
ASSERT_EQ(r.row_count, 1);
2200+
ASSERT_STR_EQ(r.rows[0][0], "validate.go"); /* was "" before the fix */
2201+
ASSERT_STR_EQ(r.rows[0][1], "ValidateOrder");
2202+
cbm_cypher_result_free(&r);
2203+
cbm_store_close(s);
2204+
PASS();
2205+
}
2206+
21862207
TEST(cypher_exec_with_where) {
21872208
cbm_store_t *s = setup_cypher_store();
21882209
cbm_cypher_result_t r = {0};
@@ -2642,6 +2663,7 @@ SUITE(cypher) {
26422663
/* Phase 6: WITH clause */
26432664
RUN_TEST(cypher_exec_with_rename);
26442665
RUN_TEST(cypher_exec_with_count);
2666+
RUN_TEST(cypher_exec_with_node_groupvar_prop);
26452667
RUN_TEST(cypher_exec_with_where);
26462668
RUN_TEST(cypher_exec_with_orderby_limit);
26472669
RUN_TEST(cypher_parse_with);

0 commit comments

Comments
 (0)