Skip to content

Commit 75fed22

Browse files
authored
Merge pull request DeusData#1378 from SEPURI-SAI-KRISHNA/fix/cypher-bound-terminal-optional-drop
fix(cypher): make expand_from_bound_terminal OPTIONAL fallback lossless
2 parents 5e5dcd8 + 068690c commit 75fed22

2 files changed

Lines changed: 193 additions & 16 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4646,21 +4646,40 @@ static void expand_from_bound_terminal(cbm_store_t *store, cbm_pattern_t *patn,
46464646
bool scan_targets =
46474647
!rel_inbound; /* (start)->(term): start = edge source = scan term's inbound */
46484648

4649-
size_t alloc_n = (size_t)*bind_count * (size_t)CYP_GROWTH_10 + SKIP_ONE;
4649+
/* Size this hop's output for BOTH writers without dropping any row: the
4650+
* materialised expansion is capped at max_new = *bind_count * 10 rows (only
4651+
* the WRITE is gated on max_new; match DETECTION below is not, so a full
4652+
* buffer never hides a real neighbour), and the OPTIONAL fallback
4653+
* emits at most one row per source (<= *bind_count). A source either matches
4654+
* (feeds the expansion) or takes the fallback, never both, so the two counts
4655+
* are additive and bounded by max_new + *bind_count. Computed in size_t so
4656+
* the product cannot overflow.
4657+
* The previous "+ SKIP_ONE" sizing tied max_new to the whole buffer, so once
4658+
* the expansion saturated it the fallback guard silently dropped every later
4659+
* OPTIONAL no-match row (a data-loss bug, not an OOB — the fallback stayed
4660+
* in-bounds behind that guard) — exactly the rows
4661+
* `OPTIONAL MATCH ... WHERE <start> IS NULL` is meant to surface. */
4662+
size_t alloc_n = (size_t)*bind_count * (size_t)CYP_GROWTH_10 + (size_t)*bind_count;
46504663
binding_t *new_bindings = malloc(alloc_n * sizeof(binding_t));
46514664
if (!new_bindings) {
46524665
return;
46534666
}
46544667
int new_count = 0;
4655-
int max_new = (int)alloc_n;
4668+
int max_new = *bind_count * CYP_GROWTH_10;
46564669

4657-
for (int bi = 0; bi < *bind_count && new_count < max_new; bi++) {
4670+
for (int bi = 0; bi < *bind_count; bi++) {
46584671
binding_t *b = &(*bindings)[bi];
46594672
cbm_node_t *term = binding_get(b, patn->nodes[1].variable ? patn->nodes[1].variable : "");
46604673
int match_count = 0;
46614674
if (term) {
4662-
for (int ti = 0;
4663-
ti < (rel->type_count > 0 ? rel->type_count : 1) && new_count < max_new; ti++) {
4675+
/* Detection is decoupled from the write budget: scan every edge and
4676+
* type unconditionally so match_count reflects the true neighbour
4677+
* count, and gate only the WRITE on the ceiling (below). If detection
4678+
* stopped at max_new too, a saturated buffer would leave match_count at
4679+
* 0 for a terminal that actually has callers, and the fallback below
4680+
* would fabricate an UNBOUND "dead code" row for live code — worse than
4681+
* dropping a row. */
4682+
for (int ti = 0; ti < (rel->type_count > 0 ? rel->type_count : 1); ti++) {
46644683
cbm_edge_t *edges = NULL;
46654684
int edge_count = 0;
46664685
if (rel->type_count > 0) {
@@ -4676,7 +4695,7 @@ static void expand_from_bound_terminal(cbm_store_t *store, cbm_pattern_t *patn,
46764695
} else {
46774696
cbm_store_find_edges_by_source(store, term->id, &edges, &edge_count);
46784697
}
4679-
for (int ei = 0; ei < edge_count && new_count < max_new; ei++) {
4698+
for (int ei = 0; ei < edge_count; ei++) {
46804699
int64_t sid = scan_targets ? edges[ei].source_id : edges[ei].target_id;
46814700
cbm_node_t found = {0};
46824701
if (cbm_store_find_node_by_id(store, sid, &found) != CBM_STORE_OK) {
@@ -4686,22 +4705,30 @@ static void expand_from_bound_terminal(cbm_store_t *store, cbm_pattern_t *patn,
46864705
node_fields_free(&found);
46874706
continue;
46884707
}
4689-
binding_t nb = {0};
4690-
binding_copy(&nb, b);
4691-
binding_set(&nb, start_var, &found);
4692-
if (rel->variable) {
4693-
binding_set_edge(&nb, rel->variable, &edges[ei]);
4708+
match_count++;
4709+
if (new_count < max_new) {
4710+
binding_t nb = {0};
4711+
binding_copy(&nb, b);
4712+
binding_set(&nb, start_var, &found);
4713+
if (rel->variable) {
4714+
binding_set_edge(&nb, rel->variable, &edges[ei]);
4715+
}
4716+
new_bindings[new_count++] = nb;
46944717
}
46954718
node_fields_free(&found);
4696-
new_bindings[new_count++] = nb;
4697-
match_count++;
46984719
}
46994720
cbm_store_free_edges(edges, edge_count);
47004721
}
47014722
}
4702-
if (opt && match_count == 0 && new_count < max_new) {
4703-
/* No matching neighbour: keep the row with start_var left UNBOUND so
4704-
* `WHERE <start> IS NULL` correctly identifies the no-edge case. */
4723+
if (opt && match_count == 0) {
4724+
/* GENUINELY no matching neighbour: the scan above runs unconditionally,
4725+
* so match_count is the true neighbour count and match_count == 0 here
4726+
* means the terminal really has none — not merely that the buffer filled
4727+
* first. Keep the row with start_var left UNBOUND
4728+
* so `WHERE <start> IS NULL` correctly identifies the no-edge case. The
4729+
* buffer is sized max_new + *bind_count precisely so every fallback row
4730+
* has a slot — no guard needed, and no OPTIONAL no-match row is dropped
4731+
* even after the expansion has saturated max_new. */
47054732
binding_t nb = {0};
47064733
binding_copy(&nb, b);
47074734
new_bindings[new_count++] = nb;

‎tests/test_cypher.c‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,154 @@ TEST(cypher_exec_optional_rel_leaf_fallback_survives) {
843843
PASS();
844844
}
845845

846+
/* Sibling of the leaf-fallback test, but for the BOUND-TERMINAL expansion path
847+
* (expand_from_bound_terminal): the OPTIONAL start var is unbound and the
848+
* terminal is bound, e.g. `MATCH (f) OPTIONAL MATCH (c)-[:CALLS]->(f)`. That
849+
* function sized its hop buffer bind_count*10 + 1 and gated the OPTIONAL
850+
* fallback on `new_count < max_new`, so once one terminal's expansion saturated
851+
* the buffer, every LATER terminal's no-match row was silently dropped — the
852+
* rows `WHERE c IS NULL` is meant to surface. Not an overflow (the guard kept
853+
* the write in bounds) but real data loss. Lossless sizing (bind_count*10 +
854+
* bind_count, fallback ungated) preserves them. */
855+
TEST(cypher_exec_bound_terminal_optional_fallback_survives) {
856+
cbm_store_t *s = cbm_store_open_memory();
857+
cbm_store_upsert_project(s, "test", "/tmp/test");
858+
859+
/* 2 Function nodes (hub + leaf) → bind_count = 2, max_new = 20. The hub has
860+
* 21 incoming CALLS edges (> max_new), so its expansion saturates the write
861+
* buffer; the leaf has none, so it must still yield its OPTIONAL no-match
862+
* row. Under the original "+ SKIP_ONE" sizing that row was dropped once the
863+
* buffer filled; the lossless sizing preserves it regardless of the order in
864+
* which the (unordered) label scan visits the two terminals. */
865+
cbm_node_t hub = {
866+
.project = "test", .label = "Function", .name = "hub", .qualified_name = "test.hub"};
867+
int64_t hub_id = cbm_store_upsert_node(s, &hub);
868+
cbm_node_t leaf = {
869+
.project = "test", .label = "Function", .name = "leaf", .qualified_name = "test.leaf"};
870+
cbm_store_upsert_node(s, &leaf);
871+
872+
/* Callers are non-Function so they do not inflate bind_count. Each CALLS the
873+
* hub (source = caller, target = hub), so from the bound terminal `hub` the
874+
* expansion binds the unbound start `c` to each caller. */
875+
for (int i = 0; i < 21; i++) {
876+
char nm[32];
877+
char qn[48];
878+
snprintf(nm, sizeof(nm), "caller%02d", i);
879+
snprintf(qn, sizeof(qn), "test.caller%02d", i);
880+
cbm_node_t caller = {.project = "test", .label = "Var", .name = nm, .qualified_name = qn};
881+
int64_t cid = cbm_store_upsert_node(s, &caller);
882+
cbm_edge_t e = {.project = "test", .source_id = cid, .target_id = hub_id, .type = "CALLS"};
883+
cbm_store_insert_edge(s, &e);
884+
}
885+
886+
/* max_rows 0 → the 100000 result ceiling, so the output LIMIT does not hide
887+
* the leaf row; we are testing the hop buffer, not the output cap. */
888+
cbm_cypher_result_t r = {0};
889+
int rc = cbm_cypher_execute(
890+
s, "MATCH (f:Function) OPTIONAL MATCH (c)-[:CALLS]->(f) RETURN f.name, c.name", "test", 0,
891+
&r);
892+
ASSERT_EQ(rc, 0);
893+
ASSERT_EQ(r.col_count, 2);
894+
895+
/* The leaf terminal has no incoming CALLS edge, so it must yield one row with
896+
* the start var `c` unbound (""). Before the fix this row was dropped once the
897+
* hub saturated the buffer. Also confirm the hub still expanded to bound rows. */
898+
bool leaf_fallback = false;
899+
bool hub_expanded = false;
900+
for (int i = 0; i < r.row_count; i++) {
901+
const char *f = r.rows[i][0];
902+
const char *c = r.rows[i][1];
903+
if (strcmp(f, "leaf") == 0 && c[0] == '\0') {
904+
leaf_fallback = true;
905+
}
906+
if (strcmp(f, "hub") == 0 && c[0] != '\0') {
907+
hub_expanded = true;
908+
}
909+
}
910+
ASSERT_TRUE(leaf_fallback); /* the bound-terminal OPTIONAL no-match row survived */
911+
ASSERT_TRUE(hub_expanded); /* the expansion still produced bound rows */
912+
913+
cbm_cypher_result_free(&r);
914+
cbm_store_close(s);
915+
PASS();
916+
}
917+
918+
/* Discriminating companion to the test above: preserving OPTIONAL no-match rows
919+
* under saturation must NOT be bought by fabricating them. If match detection is
920+
* gated on the same ceiling as the write, then once one terminal fills the
921+
* buffer, another terminal that genuinely HAS callers is never scanned, its
922+
* match_count stays 0, and the fallback invents an unbound "dead code" row for it
923+
* — reporting live code as dead, which is worse than dropping a row.
924+
*
925+
* The construction is deliberately order-independent: BOTH hubs have enough
926+
* callers to saturate the buffer on their own, so whichever the scan visits
927+
* second is guaranteed to be processed after saturation. Under the gated variant
928+
* that second hub is fabricated as dead; the assertion "no hub with callers is
929+
* dead" then fails no matter which order `find_nodes_by_label` returns (its query
930+
* has no ORDER BY, so the test must not depend on one). The fix makes it pass. */
931+
TEST(cypher_exec_bound_terminal_saturation_no_false_deadcode) {
932+
cbm_store_t *s = cbm_store_open_memory();
933+
cbm_store_upsert_project(s, "test", "/tmp/test");
934+
935+
/* 3 Function terminals → bind_count = 3, max_new = 30. */
936+
cbm_node_t hubA = {
937+
.project = "test", .label = "Function", .name = "hubA", .qualified_name = "test.hubA"};
938+
int64_t hubA_id = cbm_store_upsert_node(s, &hubA);
939+
cbm_node_t hubB = {
940+
.project = "test", .label = "Function", .name = "hubB", .qualified_name = "test.hubB"};
941+
int64_t hubB_id = cbm_store_upsert_node(s, &hubB);
942+
cbm_node_t leaf = {
943+
.project = "test", .label = "Function", .name = "leaf", .qualified_name = "test.leaf"};
944+
cbm_store_upsert_node(s, &leaf);
945+
946+
/* BOTH hubs get 35 callers (> max_new = 30), so either one saturates the write
947+
* buffer by itself; leaf gets none. Callers are non-Function so they don't
948+
* inflate bind_count. */
949+
for (int i = 0; i < 70; i++) {
950+
char nm[32];
951+
char qn[48];
952+
snprintf(nm, sizeof(nm), "caller%02d", i);
953+
snprintf(qn, sizeof(qn), "test.caller%02d", i);
954+
cbm_node_t caller = {.project = "test", .label = "Var", .name = nm, .qualified_name = qn};
955+
int64_t cid = cbm_store_upsert_node(s, &caller);
956+
int64_t tgt = i < 35 ? hubA_id : hubB_id; /* 35 -> hubA, 35 -> hubB */
957+
cbm_edge_t e = {.project = "test", .source_id = cid, .target_id = tgt, .type = "CALLS"};
958+
cbm_store_insert_edge(s, &e);
959+
}
960+
961+
cbm_cypher_result_t r = {0};
962+
int rc = cbm_cypher_execute(
963+
s, "MATCH (f:Function) OPTIONAL MATCH (c)-[:CALLS]->(f) RETURN f.name, c.name", "test", 0,
964+
&r);
965+
ASSERT_EQ(rc, 0);
966+
ASSERT_EQ(r.col_count, 2);
967+
968+
bool hub_expanded = false; /* sanity: the buffer really did fill from a hub */
969+
bool hub_false_deadcode = false; /* the bug: a hub with callers invented as dead */
970+
bool leaf_deadcode = false; /* the lossless property: genuine dead code kept */
971+
for (int i = 0; i < r.row_count; i++) {
972+
const char *f = r.rows[i][0];
973+
const char *c = r.rows[i][1];
974+
bool is_hub = strcmp(f, "hubA") == 0 || strcmp(f, "hubB") == 0;
975+
if (is_hub && c[0] != '\0') {
976+
hub_expanded = true;
977+
}
978+
if (is_hub && c[0] == '\0') {
979+
hub_false_deadcode = true;
980+
}
981+
if (strcmp(f, "leaf") == 0 && c[0] == '\0') {
982+
leaf_deadcode = true;
983+
}
984+
}
985+
ASSERT_TRUE(hub_expanded);
986+
ASSERT_FALSE(hub_false_deadcode); /* live code with callers must never appear as dead */
987+
ASSERT_TRUE(leaf_deadcode); /* genuine no-match row still survives saturation */
988+
989+
cbm_cypher_result_free(&r);
990+
cbm_store_close(s);
991+
PASS();
992+
}
993+
846994
TEST(cypher_exec_where_eq) {
847995
cbm_store_t *s = setup_cypher_store();
848996
cbm_cypher_result_t r = {0};
@@ -3723,6 +3871,8 @@ SUITE(cypher) {
37233871
RUN_TEST(cypher_exec_optional_rel_saturated_no_overflow);
37243872
RUN_TEST(cypher_exec_optional_saturated_does_not_fabricate_no_match);
37253873
RUN_TEST(cypher_exec_optional_rel_leaf_fallback_survives);
3874+
RUN_TEST(cypher_exec_bound_terminal_optional_fallback_survives);
3875+
RUN_TEST(cypher_exec_bound_terminal_saturation_no_false_deadcode);
37263876
RUN_TEST(cypher_issue240_labels_function);
37273877
RUN_TEST(cypher_issue237_distinct_order_limit);
37283878
RUN_TEST(cypher_issue873_distinct_order_limit_dedupes_before_limit);

0 commit comments

Comments
 (0)