Skip to content

Commit 22d93dd

Browse files
authored
Merge pull request #1323 from Enferlain/fix/1196-unlabeled-candidate-limit
fix(cypher): scan all unlabeled query candidates
2 parents 2397d58 + 64c5dda commit 22d93dd

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
@@ -3071,31 +3071,14 @@ static void scan_alternation_labels(cbm_store_t *store, const char *project, con
30713071
free(copy);
30723072
}
30733073

3074-
static void scan_pattern_nodes(cbm_store_t *store, const char *project, int max_rows,
3075-
cbm_node_pattern_t *first, cbm_node_t **out_nodes, int *out_count) {
3074+
static void scan_pattern_nodes(cbm_store_t *store, const char *project, cbm_node_pattern_t *first,
3075+
cbm_node_t **out_nodes, int *out_count) {
30763076
if (first->label && strchr(first->label, '|')) {
30773077
scan_alternation_labels(store, project, first->label, out_nodes, out_count);
30783078
} else if (first->label) {
30793079
cbm_store_find_nodes_by_label(store, project, first->label, out_nodes, out_count);
30803080
} else {
3081-
cbm_search_params_t params = {.project = project,
3082-
.min_degree = CYP_FOUND_NONE,
3083-
.max_degree = CYP_FOUND_NONE,
3084-
.limit = max_rows * CYP_GROWTH_10};
3085-
cbm_search_output_t sout = {0};
3086-
cbm_store_search(store, &params, &sout);
3087-
*out_count = sout.count;
3088-
*out_nodes = malloc(sout.count * sizeof(cbm_node_t));
3089-
for (int i = 0; i < sout.count; i++) {
3090-
(*out_nodes)[i] = sout.results[i].node;
3091-
sout.results[i].node.name = NULL;
3092-
sout.results[i].node.project = NULL;
3093-
sout.results[i].node.label = NULL;
3094-
sout.results[i].node.qualified_name = NULL;
3095-
sout.results[i].node.file_path = NULL;
3096-
sout.results[i].node.properties_json = NULL;
3097-
}
3098-
cbm_store_search_free(&sout);
3081+
cbm_store_find_nodes(store, project, out_nodes, out_count);
30993082
}
31003083
/* Apply inline property filters — free rejected nodes' strings */
31013084
if (first->prop_count > 0) {
@@ -4779,7 +4762,7 @@ static int expand_additional_patterns(cbm_store_t *store, cbm_query_t *q, const
47794762

47804763
cbm_node_t *extra_nodes = NULL;
47814764
int extra_count = 0;
4782-
scan_pattern_nodes(store, project, max_rows, &patn->nodes[0], &extra_nodes, &extra_count);
4765+
scan_pattern_nodes(store, project, &patn->nodes[0], &extra_nodes, &extra_count);
47834766
int rc = 0;
47844767
if (patn->rel_count == 0) {
47854768
rc = cross_join_nodes(bindings, bind_count, extra_nodes, extra_count, nvar, opt);
@@ -4831,7 +4814,7 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
48314814
/* Step 1: Scan initial nodes */
48324815
cbm_node_t *scanned = NULL;
48334816
int scan_count = 0;
4834-
scan_pattern_nodes(store, project, max_rows, &pat0->nodes[0], &scanned, &scan_count);
4817+
scan_pattern_nodes(store, project, &pat0->nodes[0], &scanned, &scan_count);
48354818

48364819
/* Build initial bindings with early WHERE */
48374820
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
@@ -117,6 +117,7 @@ struct cbm_store {
117117
sqlite3_stmt *stmt_find_node_by_id;
118118
sqlite3_stmt *stmt_find_node_by_qn;
119119
sqlite3_stmt *stmt_find_node_by_qn_any; /* QN lookup without project filter */
120+
sqlite3_stmt *stmt_find_nodes;
120121
sqlite3_stmt *stmt_find_nodes_by_name;
121122
sqlite3_stmt *stmt_find_nodes_by_name_any; /* name lookup without project filter */
122123
sqlite3_stmt *stmt_find_nodes_by_label;
@@ -1140,6 +1141,7 @@ void cbm_store_close(cbm_store_t *s) {
11401141
finalize_stmt(&s->stmt_find_node_by_id);
11411142
finalize_stmt(&s->stmt_find_node_by_qn);
11421143
finalize_stmt(&s->stmt_find_node_by_qn_any);
1144+
finalize_stmt(&s->stmt_find_nodes);
11431145
finalize_stmt(&s->stmt_find_nodes_by_name);
11441146
finalize_stmt(&s->stmt_find_nodes_by_name_any);
11451147
finalize_stmt(&s->stmt_find_nodes_by_label);
@@ -1978,7 +1980,9 @@ static int find_nodes_generic(cbm_store_t *s, sqlite3_stmt **slot, const char *s
19781980
}
19791981

19801982
bind_text(stmt, SKIP_ONE, project);
1981-
bind_text(stmt, ST_COL_2, val);
1983+
if (val) {
1984+
bind_text(stmt, ST_COL_2, val);
1985+
}
19821986

19831987
int cap = ST_INIT_CAP_16;
19841988
int n = 0;
@@ -2006,6 +2010,19 @@ static int find_nodes_generic(cbm_store_t *s, sqlite3_stmt **slot, const char *s
20062010
return CBM_STORE_OK;
20072011
}
20082012

2013+
int cbm_store_find_nodes(cbm_store_t *s, const char *project, cbm_node_t **out, int *count) {
2014+
if (!s) {
2015+
*out = NULL;
2016+
*count = 0;
2017+
return CBM_STORE_ERR;
2018+
}
2019+
return find_nodes_generic(s, &s->stmt_find_nodes,
2020+
"SELECT id, project, label, name, qualified_name, file_path, "
2021+
"start_line, end_line, properties FROM nodes "
2022+
"WHERE project = ?1;",
2023+
project, NULL, out, count);
2024+
}
2025+
20092026
int cbm_store_find_nodes_by_name(cbm_store_t *s, const char *project, const char *name,
20102027
cbm_node_t **out, int *count) {
20112028
if (!s) {

‎src/store/store.h‎

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

406+
/* Find all nodes in a project. Returns allocated array, caller frees. */
407+
int cbm_store_find_nodes(cbm_store_t *s, const char *project, cbm_node_t **out, int *count);
408+
406409
/* Find nodes by name (exact match). Returns allocated array, caller frees. */
407410
int cbm_store_find_nodes_by_name(cbm_store_t *s, const char *project, const char *name,
408411
cbm_node_t **out, int *count);

‎tests/test_cypher.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,46 @@ TEST(cypher_exec_where_eq) {
10051005
PASS();
10061006
}
10071007

1008+
/* #1196: max_rows limits projected results, not the source-node candidates
1009+
* considered before WHERE. The old unlabeled scan searched only
1010+
* 10 * max_rows nodes, so a valid match later in search order disappeared. */
1011+
TEST(cypher_exec_unlabeled_where_beyond_result_limit_issue1196) {
1012+
cbm_store_t *s = cbm_store_open_memory();
1013+
ASSERT_NOT_NULL(s);
1014+
ASSERT_EQ(cbm_store_upsert_project(s, "test", "/tmp/test"), CBM_STORE_OK);
1015+
1016+
for (int i = 0; i < 11; i++) {
1017+
char name[32];
1018+
char qn[64];
1019+
snprintf(name, sizeof(name), "early_%02d", i);
1020+
snprintf(qn, sizeof(qn), "test.%s", name);
1021+
cbm_node_t distractor = {.project = "test",
1022+
.label = "Function",
1023+
.name = name,
1024+
.qualified_name = qn,
1025+
.file_path = "early.py"};
1026+
ASSERT_GT(cbm_store_upsert_node(s, &distractor), 0);
1027+
}
1028+
1029+
cbm_node_t late = {.project = "test",
1030+
.label = "Function",
1031+
.name = "zz_late_match",
1032+
.qualified_name = "test.zz_late_match",
1033+
.file_path = "late.py"};
1034+
ASSERT_GT(cbm_store_upsert_node(s, &late), 0);
1035+
1036+
cbm_cypher_result_t r = {0};
1037+
int rc = cbm_cypher_execute(
1038+
s, "MATCH (n) WHERE n.name = \"zz_late_match\" RETURN n.name", "test", 1, &r);
1039+
ASSERT_EQ(rc, 0);
1040+
ASSERT_EQ(r.row_count, 1);
1041+
ASSERT_STR_EQ(r.rows[0][0], "zz_late_match");
1042+
1043+
cbm_cypher_result_free(&r);
1044+
cbm_store_close(s);
1045+
PASS();
1046+
}
1047+
10081048
/* #874: coalesce(var.prop, literal) in WHERE — null-safe numeric filters
10091049
* for audit queries over OPTIONAL graph properties. The parser rejected the
10101050
* call outright ("unexpected operator"); RETURN-side coalesce already
@@ -3897,6 +3937,7 @@ SUITE(cypher) {
38973937
RUN_TEST(cypher_issue252_tointeger);
38983938
RUN_TEST(cypher_issue305_count_star_alias);
38993939
RUN_TEST(cypher_exec_where_eq);
3940+
RUN_TEST(cypher_exec_unlabeled_where_beyond_result_limit_issue1196);
39003941
RUN_TEST(cypher_exec_varlength_path_semantics_issue797);
39013942
RUN_TEST(cypher_exec_where_coalesce_issue874);
39023943
RUN_TEST(cypher_exec_where_regex);

0 commit comments

Comments
 (0)