Skip to content

Commit 0c0eb95

Browse files
fix(cypher): RETURN * must read the live scope, not the query pattern
RETURN * built its columns from the variables the query pattern named, never from the bindings it was about to project. One line caused two separate wrong answers, and neither one reported an error. After a WITH, the pattern's variables are out of scope — the WITH replaced them with the names it made. The old code still asked for the old names, found none of them, and answered a full result of empty strings. This query used to print twelve columns of nothing: MATCH (f:Function) OPTIONAL MATCH (f)-[:CALLS]->(g) WITH f.name AS caller, g.name AS callee RETURN * It now prints two columns, caller and callee, holding their values. A name the WITH made holds one value rather than a node, so it gets one column, not the four a node variable gets. Separately, collect_pattern_vars appended every pattern's variables with no repeat check. A variable named in two patterns got its four columns twice, which the OPTIONAL MATCH above does with f. Two tests cover both faults and fail against the old code: cypher_return_star_dedups_repeated_pattern_var col_count 12, want 8 cypher_return_star_after_with_names_aliases col_count 8, want 2 Cypher suite: 185 passed, 0 failed. clang-format clean on both files. Reported alongside a second fault this does NOT fix: a variable the WITH dropped is still accepted afterwards and renders empty, because nothing checks a projected name against the live scope. See .agents/research/2026-08-29-cypher-return-star-and-with-scope.md. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Joshua Richter <jrichter5781@gmail.com>
1 parent 997d087 commit 0c0eb95

2 files changed

Lines changed: 100 additions & 4 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4156,17 +4156,34 @@ static void execute_with_clause(cbm_query_t *q, binding_t **bindings_ptr, int *b
41564156

41574157
/* Project RETURN * — all bound variable properties */
41584158
/* Collect all variable names from query patterns */
4159+
/* Has this variable already been collected? A query may name the same variable
4160+
* in more than one pattern, and RETURN * must give it one set of columns. */
4161+
static bool star_var_seen(const char **vars, int vc, const char *name) {
4162+
for (int i = 0; i < vc; i++) {
4163+
if (strcmp(vars[i], name) == 0) {
4164+
return true;
4165+
}
4166+
}
4167+
return false;
4168+
}
4169+
4170+
/* Collect the variables a RETURN * projects, in the order the query names them
4171+
* and with no repeats. Without the repeat check, `MATCH (f) OPTIONAL MATCH
4172+
* (f)-[:CALLS]->(g)` names f in two patterns and f gets its four columns
4173+
* twice. */
41594174
static int collect_pattern_vars(cbm_query_t *q, const char **vars, int max_vars) {
41604175
int vc = 0;
41614176
for (int pi = 0; pi < q->pattern_count; pi++) {
41624177
for (int ni = 0; ni < q->patterns[pi].node_count && vc < max_vars; ni++) {
4163-
if (q->patterns[pi].nodes[ni].variable) {
4164-
vars[vc++] = q->patterns[pi].nodes[ni].variable;
4178+
const char *var = q->patterns[pi].nodes[ni].variable;
4179+
if (var && !star_var_seen(vars, vc, var)) {
4180+
vars[vc++] = var;
41654181
}
41664182
}
41674183
for (int ri = 0; ri < q->patterns[pi].rel_count && vc < max_vars; ri++) {
4168-
if (q->patterns[pi].rels[ri].variable) {
4169-
vars[vc++] = q->patterns[pi].rels[ri].variable;
4184+
const char *var = q->patterns[pi].rels[ri].variable;
4185+
if (var && !star_var_seen(vars, vc, var)) {
4186+
vars[vc++] = var;
41704187
}
41714188
}
41724189
}
@@ -4218,8 +4235,41 @@ static void project_star_row(binding_t *b, const char **vars, int vc, const char
42184235
}
42194236
}
42204237

4238+
/* RETURN * after a WITH.
4239+
*
4240+
* The pattern's variables are out of scope by this point — the WITH replaced
4241+
* them with the names it made. Each of those names holds one value, not a
4242+
* node, so each is ONE column rather than the four a node variable gets.
4243+
*
4244+
* Reading the pattern here instead is the fault this function exists to avoid:
4245+
* it named variables the bindings no longer hold, found nothing for every one
4246+
* of them, and answered a full result of empty strings with no error. */
4247+
static void execute_return_star_after_with(cbm_query_t *q, binding_t *bindings, int bind_count,
4248+
int max_rows, result_builder_t *rb) {
4249+
cbm_return_clause_t *wc = q->with_clause;
4250+
char name_bufs[CYP_MAX_VARS][CBM_SZ_128];
4251+
const char *cols[CYP_MAX_VARS];
4252+
int col_n = wc->count < CYP_MAX_VARS ? wc->count : CYP_MAX_VARS;
4253+
for (int i = 0; i < col_n; i++) {
4254+
cols[i] = resolve_item_alias(&wc->items[i], name_bufs[i], sizeof(name_bufs[i]));
4255+
}
4256+
rb_set_columns(rb, cols, col_n);
4257+
for (int bi = 0; bi < bind_count && rb->row_count < max_rows; bi++) {
4258+
const char *vals[CYP_MAX_VARS];
4259+
for (int i = 0; i < col_n; i++) {
4260+
cbm_node_t *vn = binding_get(&bindings[bi], cols[i]);
4261+
vals[i] = vn && vn->name ? vn->name : "";
4262+
}
4263+
rb_add_row(rb, vals);
4264+
}
4265+
}
4266+
42214267
static void execute_return_star(cbm_query_t *q, binding_t *bindings, int bind_count, int max_rows,
42224268
result_builder_t *rb) {
4269+
if (q->with_clause) {
4270+
execute_return_star_after_with(q, bindings, bind_count, max_rows, rb);
4271+
return;
4272+
}
42234273
const char *vars[CBM_SZ_32];
42244274
int vc = collect_pattern_vars(q, vars, CBM_SZ_32);
42254275
build_star_columns(rb, vars, vc);

‎tests/test_cypher.c‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,50 @@ TEST(cypher_exec_return_star) {
28592859
PASS();
28602860
}
28612861

2862+
TEST(cypher_return_star_dedups_repeated_pattern_var) {
2863+
/* RETURN * collected its column variables from every pattern in turn and
2864+
* never deduped, so a variable named in two patterns got its four columns
2865+
* twice. Here f is named in the MATCH and again in the OPTIONAL MATCH, so
2866+
* eight columns is right and twelve is the fault. */
2867+
cbm_store_t *s = setup_cypher_store();
2868+
cbm_cypher_result_t r = {0};
2869+
int rc = cbm_cypher_execute(s, "MATCH (f:Function) OPTIONAL MATCH (f)-[:CALLS]->(g) RETURN *",
2870+
"test", 0, &r);
2871+
ASSERT_EQ(rc, 0);
2872+
ASSERT_EQ(r.col_count, 8);
2873+
ASSERT_STR_EQ(r.columns[0], "f.name");
2874+
ASSERT_STR_EQ(r.columns[4], "g.name");
2875+
cbm_cypher_result_free(&r);
2876+
cbm_store_close(s);
2877+
PASS();
2878+
}
2879+
2880+
TEST(cypher_return_star_after_with_names_aliases) {
2881+
/* RETURN * built its columns from the query pattern, never from the
2882+
* bindings it was about to project. After a WITH the live scope is the
2883+
* aliases the WITH made, so the old code asked for f and g, found neither,
2884+
* and answered every value empty with no error. */
2885+
cbm_store_t *s = setup_cypher_store();
2886+
cbm_cypher_result_t r = {0};
2887+
int rc = cbm_cypher_execute(s,
2888+
"MATCH (f:Function)-[:CALLS]->(g) "
2889+
"WITH f.name AS caller, g.name AS callee RETURN *",
2890+
"test", 0, &r);
2891+
ASSERT_EQ(rc, 0);
2892+
ASSERT_EQ(r.col_count, 2);
2893+
ASSERT_STR_EQ(r.columns[0], "caller");
2894+
ASSERT_STR_EQ(r.columns[1], "callee");
2895+
/* Three CALLS edges in the fixture. */
2896+
ASSERT_EQ(r.row_count, 3);
2897+
for (int i = 0; i < r.row_count; i++) {
2898+
ASSERT_TRUE(r.rows[i][0][0] != '\0');
2899+
ASSERT_TRUE(r.rows[i][1][0] != '\0');
2900+
}
2901+
cbm_cypher_result_free(&r);
2902+
cbm_store_close(s);
2903+
PASS();
2904+
}
2905+
28622906
TEST(cypher_parse_neq) {
28632907
cbm_query_t *q = NULL;
28642908
char *err = NULL;
@@ -4161,6 +4205,8 @@ SUITE(cypher) {
41614205
RUN_TEST(cypher_exec_where_is_null);
41624206
RUN_TEST(cypher_exec_where_is_not_null);
41634207
RUN_TEST(cypher_exec_return_star);
4208+
RUN_TEST(cypher_return_star_dedups_repeated_pattern_var);
4209+
RUN_TEST(cypher_return_star_after_with_names_aliases);
41644210
RUN_TEST(cypher_parse_neq);
41654211
RUN_TEST(cypher_parse_in);
41664212
RUN_TEST(cypher_parse_is_null);

0 commit comments

Comments
 (0)