Skip to content

Commit 9595822

Browse files
authored
Merge pull request DeusData#1221 from AmirF194/fix/1111-aggregate-scalar-func-misclassification
fix(cypher): classify aggregation columns by is_aggregate_func, not a bare func check
2 parents 7814e1b + 2f522b4 commit 9595822

2 files changed

Lines changed: 87 additions & 11 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,10 +3742,11 @@ typedef struct {
37423742
static int with_agg_build_key(cbm_return_clause_t *wc, binding_t *b, char *key, size_t key_sz) {
37433743
int kl = 0;
37443744
for (int ci = 0; ci < wc->count; ci++) {
3745-
if (wc->items[ci].func) {
3745+
if (is_aggregate_func(wc->items[ci].func)) {
37463746
continue;
37473747
}
3748-
const char *v = binding_get_virtual(b, wc->items[ci].variable, wc->items[ci].property);
3748+
char vbuf[CBM_SZ_512];
3749+
const char *v = project_item(b, &wc->items[ci], vbuf, sizeof(vbuf));
37493750
kl += snprintf(key + kl, key_sz - (size_t)kl, "%s|", v);
37503751
if (kl >= (int)key_sz) {
37513752
kl = (int)key_sz - SKIP_ONE;
@@ -3781,16 +3782,21 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
37813782
(*aggs)[found].maxs[ci] = -CYP_DBL_MAX;
37823783
}
37833784
for (int ci = 0; ci < wc->count; ci++) {
3784-
if (wc->items[ci].func) {
3785+
if (is_aggregate_func(wc->items[ci].func)) {
37853786
(*aggs)[found].group_vals[ci] = heap_strdup("0");
37863787
continue;
37873788
}
3788-
const char *v = binding_get_virtual(b, wc->items[ci].variable, wc->items[ci].property);
3789+
char vbuf[CBM_SZ_512];
3790+
const char *v = project_item(b, &wc->items[ci], vbuf, sizeof(vbuf));
37893791
(*aggs)[found].group_vals[ci] = heap_strdup(v);
37903792
/* If this group item is a bare node variable, remember its id so the
37913793
* carried virtual var can re-fetch any property (group_vals holds only
3792-
* the name). */
3793-
if (!wc->items[ci].property && wc->items[ci].variable) {
3794+
* the name). Excludes entity-introspection funcs (labels/id/keys/
3795+
* properties): those project a scalar off the node (via project_item
3796+
* above), not the node itself, so the carried id must not be set or a
3797+
* later alias.property re-fetches the source node's real properties
3798+
* instead of returning empty for the non-node alias. */
3799+
if (!wc->items[ci].func && !wc->items[ci].property && wc->items[ci].variable) {
37943800
cbm_node_t *gn = binding_get(b, wc->items[ci].variable);
37953801
if (gn) {
37963802
(*aggs)[found].group_node_ids[ci] = gn->id;
@@ -3803,7 +3809,7 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
38033809
/* Accumulate aggregation values for a binding */
38043810
static void with_agg_accumulate(with_agg_t *agg, cbm_return_clause_t *wc, binding_t *b) {
38053811
for (int ci = 0; ci < wc->count; ci++) {
3806-
if (!wc->items[ci].func) {
3812+
if (!is_aggregate_func(wc->items[ci].func)) {
38073813
continue;
38083814
}
38093815
agg->counts[ci]++;
@@ -3898,7 +3904,7 @@ static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings,
38983904
for (int ci = 0; ci < wc->count; ci++) {
38993905
char name_buf[CBM_SZ_256];
39003906
const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));
3901-
if (wc->items[ci].func) {
3907+
if (is_aggregate_func(wc->items[ci].func)) {
39023908
char vbuf[CBM_SZ_64];
39033909
if (wc->items[ci].distinct && strcmp(wc->items[ci].func, "COUNT") == 0) {
39043910
snprintf(vbuf, sizeof(vbuf), "%d", aggs[a].distinct_n[ci]); /* #239 */
@@ -4197,7 +4203,7 @@ static void ret_agg_init_group(ret_agg_entry_t *entry, const char *key, int item
41974203
/* Accumulate a binding into RETURN aggregation */
41984204
static void ret_agg_accumulate(ret_agg_entry_t *entry, cbm_return_clause_t *ret, binding_t *b) {
41994205
for (int ci = 0; ci < ret->count; ci++) {
4200-
if (!ret->items[ci].func) {
4206+
if (!is_aggregate_func(ret->items[ci].func)) {
42014207
continue;
42024208
}
42034209
entry->counts[ci]++;
@@ -4249,7 +4255,7 @@ static void ret_agg_build_key(cbm_return_clause_t *ret, binding_t *b, char *key,
42494255
const char **vals, char valbufs[][CBM_SZ_512]) {
42504256
int klen = 0;
42514257
for (int ci = 0; ci < ret->count; ci++) {
4252-
if (ret->items[ci].func) {
4258+
if (is_aggregate_func(ret->items[ci].func)) {
42534259
vals[ci] = "0";
42544260
continue;
42554261
}
@@ -4273,7 +4279,7 @@ static void ret_agg_emit_row(cbm_return_clause_t *ret, ret_agg_entry_t *agg, res
42734279
const char *row[CBM_SZ_32];
42744280
char bufs[CBM_SZ_32][CBM_SZ_64];
42754281
for (int ci = 0; ci < ret->count; ci++) {
4276-
if (!ret->items[ci].func) {
4282+
if (!is_aggregate_func(ret->items[ci].func)) {
42774283
row[ci] = agg->group_vals[ci];
42784284
continue;
42794285
}

‎tests/test_cypher.c‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,28 @@ TEST(cypher_exec_count_star) {
26522652
PASS();
26532653
}
26542654

2655+
/* #1111: type(r) grouped with count(*) must return the actual relationship type,
2656+
* not the row count. ret_agg_build_key/ret_agg_emit_row classified aggregate vs.
2657+
* scalar columns with a bare `item->func` truthy check, so type(r) (a non-aggregate
2658+
* function, func != NULL) was misrouted into the aggregate-value branch and
2659+
* formatted via format_agg_value's default case, silently substituting the row
2660+
* count for the relationship type. */
2661+
TEST(cypher_issue1111_return_type_count_group) {
2662+
cbm_store_t *s = setup_cypher_store();
2663+
cbm_cypher_result_t r = {0};
2664+
int rc = cbm_cypher_execute(
2665+
s, "MATCH (a)-[r]->(b) RETURN type(r) AS t, count(*) AS n ORDER BY n DESC", "test", 0, &r);
2666+
ASSERT_EQ(rc, 0);
2667+
ASSERT_EQ(r.row_count, 2);
2668+
ASSERT_STR_EQ(r.rows[0][0], "CALLS");
2669+
ASSERT_STR_EQ(r.rows[0][1], "3");
2670+
ASSERT_STR_EQ(r.rows[1][0], "DEFINES");
2671+
ASSERT_STR_EQ(r.rows[1][1], "1");
2672+
cbm_cypher_result_free(&r);
2673+
cbm_store_close(s);
2674+
PASS();
2675+
}
2676+
26552677
TEST(cypher_parse_skip) {
26562678
cbm_query_t *q = NULL;
26572679
char *err = NULL;
@@ -2836,6 +2858,51 @@ TEST(cypher_exec_with_node_groupvar_prop) {
28362858
PASS();
28372859
}
28382860

2861+
/* #1111, WITH variant: the same misrouting in with_agg_build_key/with_agg_accumulate/
2862+
* execute_with_aggregate's per-column func check. */
2863+
TEST(cypher_issue1111_with_type_count_group) {
2864+
cbm_store_t *s = setup_cypher_store();
2865+
cbm_cypher_result_t r = {0};
2866+
int rc = cbm_cypher_execute(
2867+
s,
2868+
"MATCH (a)-[r]->(b) WITH type(r) AS t, count(*) AS n RETURN t, n ORDER BY n DESC",
2869+
"test", 0, &r);
2870+
ASSERT_EQ(rc, 0);
2871+
ASSERT_EQ(r.row_count, 2);
2872+
ASSERT_STR_EQ(r.rows[0][0], "CALLS");
2873+
ASSERT_STR_EQ(r.rows[0][1], "3");
2874+
ASSERT_STR_EQ(r.rows[1][0], "DEFINES");
2875+
ASSERT_STR_EQ(r.rows[1][1], "1");
2876+
cbm_cypher_result_free(&r);
2877+
cbm_store_close(s);
2878+
PASS();
2879+
}
2880+
2881+
/* #1111 follow-up (review from DeusData on #1221): with_agg_find_or_create's
2882+
* bare-node-carry check only tested `!property && variable`, so an entity-
2883+
* introspection alias like `labels(f) AS l` (variable set, property NULL, func
2884+
* set) was ALSO tagged with the source node's id. A later `l.file_path` then
2885+
* hit node_prop's stub re-fetch heuristic (id set, file_path/label both NULL on
2886+
* the virtual stub) and silently returned HandleOrder's real file_path instead
2887+
* of "" for the non-node alias `l`. */
2888+
TEST(cypher_issue1111_with_scalar_func_alias_no_node_leak) {
2889+
cbm_store_t *s = setup_cypher_store();
2890+
cbm_cypher_result_t r = {0};
2891+
int rc = cbm_cypher_execute(s,
2892+
"MATCH (f:Function) WHERE f.name = \"HandleOrder\" "
2893+
"WITH labels(f) AS l, COUNT(*) AS c "
2894+
"RETURN l, l.file_path, c",
2895+
"test", 0, &r);
2896+
ASSERT_EQ(rc, 0);
2897+
ASSERT_EQ(r.row_count, 1);
2898+
ASSERT_STR_EQ(r.rows[0][0], "[\"Function\"]");
2899+
ASSERT_STR_EQ(r.rows[0][1], ""); /* was "handler.go" before the fix */
2900+
ASSERT_STR_EQ(r.rows[0][2], "1");
2901+
cbm_cypher_result_free(&r);
2902+
cbm_store_close(s);
2903+
PASS();
2904+
}
2905+
28392906
TEST(cypher_exec_with_where) {
28402907
cbm_store_t *s = setup_cypher_store();
28412908
cbm_cypher_result_t r = {0};
@@ -3533,6 +3600,7 @@ SUITE(cypher) {
35333600
RUN_TEST(cypher_exec_max);
35343601
RUN_TEST(cypher_exec_collect);
35353602
RUN_TEST(cypher_exec_count_star);
3603+
RUN_TEST(cypher_issue1111_return_type_count_group);
35363604
RUN_TEST(cypher_parse_skip);
35373605
RUN_TEST(cypher_parse_sum_avg);
35383606
RUN_TEST(cypher_parse_collect);
@@ -3546,6 +3614,8 @@ SUITE(cypher) {
35463614
/* Phase 6: WITH clause */
35473615
RUN_TEST(cypher_exec_with_rename);
35483616
RUN_TEST(cypher_exec_with_count);
3617+
RUN_TEST(cypher_issue1111_with_type_count_group);
3618+
RUN_TEST(cypher_issue1111_with_scalar_func_alias_no_node_leak);
35493619
RUN_TEST(cypher_exec_with_node_groupvar_prop);
35503620
RUN_TEST(cypher_exec_with_where);
35513621
RUN_TEST(cypher_exec_with_orderby_limit);

0 commit comments

Comments
 (0)