@@ -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+
846994TEST (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