Skip to content

Commit b43bddd

Browse files
AmirF194sleep-agent
authored andcommitted
fix(cypher): classify aggregation columns by is_aggregate_func, not a bare func check
RETURN/WITH aggregation classified each projected column with a bare `if (item->func)` check to decide group-key vs. aggregate value. That check is true for any function call, so type()/labels()/id()/keys()/ properties() were routed into the aggregate branch alongside real aggregates and formatted via format_agg_value's default case, which emits the row count instead of the function's actual value. Swap the five call sites (ret_agg_build_key, ret_agg_emit_row, with_agg_build_key, with_agg_find_or_create, with_agg_accumulate) to is_aggregate_func(), the predicate already used correctly one level up to decide whether a query needs aggregation at all. Group-key columns carrying a non-aggregate function now project through the existing project_item() helper instead of binding_get_virtual(), since they need to evaluate the function rather than read a raw property. Fixes DeusData#1111 (the type(r)/count(*) half; the inline-property-map half of that issue does not reproduce on current HEAD) Signed-off-by: Amir Fathi <amirfathi.me@gmail.com>
1 parent 8f06867 commit b43bddd

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,10 +3731,11 @@ typedef struct {
37313731
static int with_agg_build_key(cbm_return_clause_t *wc, binding_t *b, char *key, size_t key_sz) {
37323732
int kl = 0;
37333733
for (int ci = 0; ci < wc->count; ci++) {
3734-
if (wc->items[ci].func) {
3734+
if (is_aggregate_func(wc->items[ci].func)) {
37353735
continue;
37363736
}
3737-
const char *v = binding_get_virtual(b, wc->items[ci].variable, wc->items[ci].property);
3737+
char vbuf[CBM_SZ_512];
3738+
const char *v = project_item(b, &wc->items[ci], vbuf, sizeof(vbuf));
37383739
kl += snprintf(key + kl, key_sz - (size_t)kl, "%s|", v);
37393740
if (kl >= (int)key_sz) {
37403741
kl = (int)key_sz - SKIP_ONE;
@@ -3770,11 +3771,12 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
37703771
(*aggs)[found].maxs[ci] = -CYP_DBL_MAX;
37713772
}
37723773
for (int ci = 0; ci < wc->count; ci++) {
3773-
if (wc->items[ci].func) {
3774+
if (is_aggregate_func(wc->items[ci].func)) {
37743775
(*aggs)[found].group_vals[ci] = heap_strdup("0");
37753776
continue;
37763777
}
3777-
const char *v = binding_get_virtual(b, wc->items[ci].variable, wc->items[ci].property);
3778+
char vbuf[CBM_SZ_512];
3779+
const char *v = project_item(b, &wc->items[ci], vbuf, sizeof(vbuf));
37783780
(*aggs)[found].group_vals[ci] = heap_strdup(v);
37793781
/* If this group item is a bare node variable, remember its id so the
37803782
* carried virtual var can re-fetch any property (group_vals holds only
@@ -3792,7 +3794,7 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
37923794
/* Accumulate aggregation values for a binding */
37933795
static void with_agg_accumulate(with_agg_t *agg, cbm_return_clause_t *wc, binding_t *b) {
37943796
for (int ci = 0; ci < wc->count; ci++) {
3795-
if (!wc->items[ci].func) {
3797+
if (!is_aggregate_func(wc->items[ci].func)) {
37963798
continue;
37973799
}
37983800
agg->counts[ci]++;
@@ -3887,7 +3889,7 @@ static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings,
38873889
for (int ci = 0; ci < wc->count; ci++) {
38883890
char name_buf[CBM_SZ_256];
38893891
const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));
3890-
if (wc->items[ci].func) {
3892+
if (is_aggregate_func(wc->items[ci].func)) {
38913893
char vbuf[CBM_SZ_64];
38923894
if (wc->items[ci].distinct && strcmp(wc->items[ci].func, "COUNT") == 0) {
38933895
snprintf(vbuf, sizeof(vbuf), "%d", aggs[a].distinct_n[ci]); /* #239 */
@@ -4186,7 +4188,7 @@ static void ret_agg_init_group(ret_agg_entry_t *entry, const char *key, int item
41864188
/* Accumulate a binding into RETURN aggregation */
41874189
static void ret_agg_accumulate(ret_agg_entry_t *entry, cbm_return_clause_t *ret, binding_t *b) {
41884190
for (int ci = 0; ci < ret->count; ci++) {
4189-
if (!ret->items[ci].func) {
4191+
if (!is_aggregate_func(ret->items[ci].func)) {
41904192
continue;
41914193
}
41924194
entry->counts[ci]++;
@@ -4238,7 +4240,7 @@ static void ret_agg_build_key(cbm_return_clause_t *ret, binding_t *b, char *key,
42384240
const char **vals, char valbufs[][CBM_SZ_512]) {
42394241
int klen = 0;
42404242
for (int ci = 0; ci < ret->count; ci++) {
4241-
if (ret->items[ci].func) {
4243+
if (is_aggregate_func(ret->items[ci].func)) {
42424244
vals[ci] = "0";
42434245
continue;
42444246
}
@@ -4262,7 +4264,7 @@ static void ret_agg_emit_row(cbm_return_clause_t *ret, ret_agg_entry_t *agg, res
42624264
const char *row[CBM_SZ_32];
42634265
char bufs[CBM_SZ_32][CBM_SZ_64];
42644266
for (int ci = 0; ci < ret->count; ci++) {
4265-
if (!ret->items[ci].func) {
4267+
if (!is_aggregate_func(ret->items[ci].func)) {
42664268
row[ci] = agg->group_vals[ci];
42674269
continue;
42684270
}

‎tests/test_cypher.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,28 @@ TEST(cypher_exec_count_star) {
25232523
PASS();
25242524
}
25252525

2526+
/* #1111: type(r) grouped with count(*) must return the actual relationship type,
2527+
* not the row count. ret_agg_build_key/ret_agg_emit_row classified aggregate vs.
2528+
* scalar columns with a bare `item->func` truthy check, so type(r) (a non-aggregate
2529+
* function, func != NULL) was misrouted into the aggregate-value branch and
2530+
* formatted via format_agg_value's default case, silently substituting the row
2531+
* count for the relationship type. */
2532+
TEST(cypher_issue1111_return_type_count_group) {
2533+
cbm_store_t *s = setup_cypher_store();
2534+
cbm_cypher_result_t r = {0};
2535+
int rc = cbm_cypher_execute(
2536+
s, "MATCH (a)-[r]->(b) RETURN type(r) AS t, count(*) AS n ORDER BY n DESC", "test", 0, &r);
2537+
ASSERT_EQ(rc, 0);
2538+
ASSERT_EQ(r.row_count, 2);
2539+
ASSERT_STR_EQ(r.rows[0][0], "CALLS");
2540+
ASSERT_STR_EQ(r.rows[0][1], "3");
2541+
ASSERT_STR_EQ(r.rows[1][0], "DEFINES");
2542+
ASSERT_STR_EQ(r.rows[1][1], "1");
2543+
cbm_cypher_result_free(&r);
2544+
cbm_store_close(s);
2545+
PASS();
2546+
}
2547+
25262548
TEST(cypher_parse_skip) {
25272549
cbm_query_t *q = NULL;
25282550
char *err = NULL;
@@ -2707,6 +2729,26 @@ TEST(cypher_exec_with_node_groupvar_prop) {
27072729
PASS();
27082730
}
27092731

2732+
/* #1111, WITH variant: the same misrouting in with_agg_build_key/with_agg_accumulate/
2733+
* execute_with_aggregate's per-column func check. */
2734+
TEST(cypher_issue1111_with_type_count_group) {
2735+
cbm_store_t *s = setup_cypher_store();
2736+
cbm_cypher_result_t r = {0};
2737+
int rc = cbm_cypher_execute(
2738+
s,
2739+
"MATCH (a)-[r]->(b) WITH type(r) AS t, count(*) AS n RETURN t, n ORDER BY n DESC",
2740+
"test", 0, &r);
2741+
ASSERT_EQ(rc, 0);
2742+
ASSERT_EQ(r.row_count, 2);
2743+
ASSERT_STR_EQ(r.rows[0][0], "CALLS");
2744+
ASSERT_STR_EQ(r.rows[0][1], "3");
2745+
ASSERT_STR_EQ(r.rows[1][0], "DEFINES");
2746+
ASSERT_STR_EQ(r.rows[1][1], "1");
2747+
cbm_cypher_result_free(&r);
2748+
cbm_store_close(s);
2749+
PASS();
2750+
}
2751+
27102752
TEST(cypher_exec_with_where) {
27112753
cbm_store_t *s = setup_cypher_store();
27122754
cbm_cypher_result_t r = {0};
@@ -3402,6 +3444,7 @@ SUITE(cypher) {
34023444
RUN_TEST(cypher_exec_max);
34033445
RUN_TEST(cypher_exec_collect);
34043446
RUN_TEST(cypher_exec_count_star);
3447+
RUN_TEST(cypher_issue1111_return_type_count_group);
34053448
RUN_TEST(cypher_parse_skip);
34063449
RUN_TEST(cypher_parse_sum_avg);
34073450
RUN_TEST(cypher_parse_collect);
@@ -3415,6 +3458,7 @@ SUITE(cypher) {
34153458
/* Phase 6: WITH clause */
34163459
RUN_TEST(cypher_exec_with_rename);
34173460
RUN_TEST(cypher_exec_with_count);
3461+
RUN_TEST(cypher_issue1111_with_type_count_group);
34183462
RUN_TEST(cypher_exec_with_node_groupvar_prop);
34193463
RUN_TEST(cypher_exec_with_where);
34203464
RUN_TEST(cypher_exec_with_orderby_limit);

0 commit comments

Comments
 (0)