Skip to content

Commit 64c5dda

Browse files
committed
fix(cypher): scan all unlabeled query candidates
Signed-off-by: imi <hoshinoimi@gmail.com>
1 parent 2789301 commit 64c5dda

4 files changed

Lines changed: 67 additions & 23 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,31 +3024,14 @@ static void scan_alternation_labels(cbm_store_t *store, const char *project, con
30243024
free(copy);
30253025
}
30263026

3027-
static void scan_pattern_nodes(cbm_store_t *store, const char *project, int max_rows,
3028-
cbm_node_pattern_t *first, cbm_node_t **out_nodes, int *out_count) {
3027+
static void scan_pattern_nodes(cbm_store_t *store, const char *project, cbm_node_pattern_t *first,
3028+
cbm_node_t **out_nodes, int *out_count) {
30293029
if (first->label && strchr(first->label, '|')) {
30303030
scan_alternation_labels(store, project, first->label, out_nodes, out_count);
30313031
} else if (first->label) {
30323032
cbm_store_find_nodes_by_label(store, project, first->label, out_nodes, out_count);
30333033
} else {
3034-
cbm_search_params_t params = {.project = project,
3035-
.min_degree = CYP_FOUND_NONE,
3036-
.max_degree = CYP_FOUND_NONE,
3037-
.limit = max_rows * CYP_GROWTH_10};
3038-
cbm_search_output_t sout = {0};
3039-
cbm_store_search(store, &params, &sout);
3040-
*out_count = sout.count;
3041-
*out_nodes = malloc(sout.count * sizeof(cbm_node_t));
3042-
for (int i = 0; i < sout.count; i++) {
3043-
(*out_nodes)[i] = sout.results[i].node;
3044-
sout.results[i].node.name = NULL;
3045-
sout.results[i].node.project = NULL;
3046-
sout.results[i].node.label = NULL;
3047-
sout.results[i].node.qualified_name = NULL;
3048-
sout.results[i].node.file_path = NULL;
3049-
sout.results[i].node.properties_json = NULL;
3050-
}
3051-
cbm_store_search_free(&sout);
3034+
cbm_store_find_nodes(store, project, out_nodes, out_count);
30523035
}
30533036
/* Apply inline property filters — free rejected nodes' strings */
30543037
if (first->prop_count > 0) {
@@ -4656,7 +4639,7 @@ static int expand_additional_patterns(cbm_store_t *store, cbm_query_t *q, const
46564639

46574640
cbm_node_t *extra_nodes = NULL;
46584641
int extra_count = 0;
4659-
scan_pattern_nodes(store, project, max_rows, &patn->nodes[0], &extra_nodes, &extra_count);
4642+
scan_pattern_nodes(store, project, &patn->nodes[0], &extra_nodes, &extra_count);
46604643
int rc = 0;
46614644
if (patn->rel_count == 0) {
46624645
rc = cross_join_nodes(bindings, bind_count, extra_nodes, extra_count, nvar, opt);
@@ -4708,7 +4691,7 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
47084691
/* Step 1: Scan initial nodes */
47094692
cbm_node_t *scanned = NULL;
47104693
int scan_count = 0;
4711-
scan_pattern_nodes(store, project, max_rows, &pat0->nodes[0], &scanned, &scan_count);
4694+
scan_pattern_nodes(store, project, &pat0->nodes[0], &scanned, &scan_count);
47124695

47134696
/* Build initial bindings with early WHERE */
47144697
int bind_cap = scan_count > max_rows ? scan_count : (max_rows > 0 ? max_rows : SKIP_ONE);

‎src/store/store.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct cbm_store {
115115
sqlite3_stmt *stmt_find_node_by_id;
116116
sqlite3_stmt *stmt_find_node_by_qn;
117117
sqlite3_stmt *stmt_find_node_by_qn_any; /* QN lookup without project filter */
118+
sqlite3_stmt *stmt_find_nodes;
118119
sqlite3_stmt *stmt_find_nodes_by_name;
119120
sqlite3_stmt *stmt_find_nodes_by_name_any; /* name lookup without project filter */
120121
sqlite3_stmt *stmt_find_nodes_by_label;
@@ -1019,6 +1020,7 @@ void cbm_store_close(cbm_store_t *s) {
10191020
finalize_stmt(&s->stmt_find_node_by_id);
10201021
finalize_stmt(&s->stmt_find_node_by_qn);
10211022
finalize_stmt(&s->stmt_find_node_by_qn_any);
1023+
finalize_stmt(&s->stmt_find_nodes);
10221024
finalize_stmt(&s->stmt_find_nodes_by_name);
10231025
finalize_stmt(&s->stmt_find_nodes_by_name_any);
10241026
finalize_stmt(&s->stmt_find_nodes_by_label);
@@ -1708,7 +1710,9 @@ static int find_nodes_generic(cbm_store_t *s, sqlite3_stmt **slot, const char *s
17081710
}
17091711

17101712
bind_text(stmt, SKIP_ONE, project);
1711-
bind_text(stmt, ST_COL_2, val);
1713+
if (val) {
1714+
bind_text(stmt, ST_COL_2, val);
1715+
}
17121716

17131717
int cap = ST_INIT_CAP_16;
17141718
int n = 0;
@@ -1736,6 +1740,19 @@ static int find_nodes_generic(cbm_store_t *s, sqlite3_stmt **slot, const char *s
17361740
return CBM_STORE_OK;
17371741
}
17381742

1743+
int cbm_store_find_nodes(cbm_store_t *s, const char *project, cbm_node_t **out, int *count) {
1744+
if (!s) {
1745+
*out = NULL;
1746+
*count = 0;
1747+
return CBM_STORE_ERR;
1748+
}
1749+
return find_nodes_generic(s, &s->stmt_find_nodes,
1750+
"SELECT id, project, label, name, qualified_name, file_path, "
1751+
"start_line, end_line, properties FROM nodes "
1752+
"WHERE project = ?1;",
1753+
project, NULL, out, count);
1754+
}
1755+
17391756
int cbm_store_find_nodes_by_name(cbm_store_t *s, const char *project, const char *name,
17401757
cbm_node_t **out, int *count) {
17411758
if (!s) {

‎src/store/store.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ int cbm_store_find_node_by_qn(cbm_store_t *s, const char *project, const char *q
327327
/* Find node by qualified_name only (no project filter — QNs are globally unique). */
328328
int cbm_store_find_node_by_qn_any(cbm_store_t *s, const char *qn, cbm_node_t *out);
329329

330+
/* Find all nodes in a project. Returns allocated array, caller frees. */
331+
int cbm_store_find_nodes(cbm_store_t *s, const char *project, cbm_node_t **out, int *count);
332+
330333
/* Find nodes by name (exact match). Returns allocated array, caller frees. */
331334
int cbm_store_find_nodes_by_name(cbm_store_t *s, const char *project, const char *name,
332335
cbm_node_t **out, int *count);

‎tests/test_cypher.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,46 @@ TEST(cypher_exec_where_eq) {
689689
PASS();
690690
}
691691

692+
/* #1196: max_rows limits projected results, not the source-node candidates
693+
* considered before WHERE. The old unlabeled scan searched only
694+
* 10 * max_rows nodes, so a valid match later in search order disappeared. */
695+
TEST(cypher_exec_unlabeled_where_beyond_result_limit_issue1196) {
696+
cbm_store_t *s = cbm_store_open_memory();
697+
ASSERT_NOT_NULL(s);
698+
ASSERT_EQ(cbm_store_upsert_project(s, "test", "/tmp/test"), CBM_STORE_OK);
699+
700+
for (int i = 0; i < 11; i++) {
701+
char name[32];
702+
char qn[64];
703+
snprintf(name, sizeof(name), "early_%02d", i);
704+
snprintf(qn, sizeof(qn), "test.%s", name);
705+
cbm_node_t distractor = {.project = "test",
706+
.label = "Function",
707+
.name = name,
708+
.qualified_name = qn,
709+
.file_path = "early.py"};
710+
ASSERT_GT(cbm_store_upsert_node(s, &distractor), 0);
711+
}
712+
713+
cbm_node_t late = {.project = "test",
714+
.label = "Function",
715+
.name = "zz_late_match",
716+
.qualified_name = "test.zz_late_match",
717+
.file_path = "late.py"};
718+
ASSERT_GT(cbm_store_upsert_node(s, &late), 0);
719+
720+
cbm_cypher_result_t r = {0};
721+
int rc = cbm_cypher_execute(
722+
s, "MATCH (n) WHERE n.name = \"zz_late_match\" RETURN n.name", "test", 1, &r);
723+
ASSERT_EQ(rc, 0);
724+
ASSERT_EQ(r.row_count, 1);
725+
ASSERT_STR_EQ(r.rows[0][0], "zz_late_match");
726+
727+
cbm_cypher_result_free(&r);
728+
cbm_store_close(s);
729+
PASS();
730+
}
731+
692732
/* #874: coalesce(var.prop, literal) in WHERE — null-safe numeric filters
693733
* for audit queries over OPTIONAL graph properties. The parser rejected the
694734
* call outright ("unexpected operator"); RETURN-side coalesce already
@@ -3500,6 +3540,7 @@ SUITE(cypher) {
35003540
RUN_TEST(cypher_issue252_tointeger);
35013541
RUN_TEST(cypher_issue305_count_star_alias);
35023542
RUN_TEST(cypher_exec_where_eq);
3543+
RUN_TEST(cypher_exec_unlabeled_where_beyond_result_limit_issue1196);
35033544
RUN_TEST(cypher_exec_varlength_path_semantics_issue797);
35043545
RUN_TEST(cypher_exec_where_coalesce_issue874);
35053546
RUN_TEST(cypher_exec_where_regex);

0 commit comments

Comments
 (0)