Skip to content

Commit 9a7d53b

Browse files
committed
fix(cypher): return one row for an ungrouped aggregate over empty input
A RETURN made only of aggregates is one group over the whole input, so it answers exactly one row. The RETURN aggregation sink emitted one row per group it had seen, and when MATCH/WHERE produced no bindings it had seen none: `MATCH (f:Function) WHERE f.name = "nope" RETURN count(*) AS total` answered no rows instead of total = 0. ret_sink_finish now emits that row when no group was created and every RETURN item is an aggregate: count and sum are 0, collect is [], and avg/min/max are null (""). A grouped aggregation over empty input still returns no rows, and the row goes through the usual DISTINCT / ORDER BY / SKIP / LIMIT post-pass. Fixes DeusData#2742 Signed-off-by: Gaurav Dubey <gauravdubey0107@gmail.com>
1 parent 10ab613 commit 9a7d53b

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5370,6 +5370,34 @@ static void ret_agg_emit_row(cbm_return_clause_t *ret, ret_agg_entry_t *agg, res
53705370
rb_add_row(rb, row);
53715371
}
53725372

5373+
/* True when every RETURN item is an aggregate, so the whole input is one group. */
5374+
static bool ret_agg_is_ungrouped(const cbm_return_clause_t *ret) {
5375+
for (int ci = 0; ci < ret->count; ci++) {
5376+
if (!is_aggregate_func(ret->items[ci].func)) {
5377+
return false;
5378+
}
5379+
}
5380+
return true;
5381+
}
5382+
5383+
/* The one row an ungrouped aggregation answers when no binding reached it
5384+
* (#2742): count and sum are 0, collect is an empty list, and avg/min/max have
5385+
* no value, rendered as "" like any other null. */
5386+
static void ret_agg_emit_empty_row(cbm_return_clause_t *ret, result_builder_t *rb) {
5387+
const char *row[CBM_SZ_32];
5388+
for (int ci = 0; ci < ret->count && ci < CBM_SZ_32; ci++) {
5389+
const char *func = ret->items[ci].func;
5390+
if (strcmp(func, "COUNT") == 0 || strcmp(func, "SUM") == 0) {
5391+
row[ci] = "0";
5392+
} else if (strcmp(func, "COLLECT") == 0) {
5393+
row[ci] = "[]";
5394+
} else {
5395+
row[ci] = "";
5396+
}
5397+
}
5398+
rb_add_row(rb, row);
5399+
}
5400+
53735401
static void ret_sink_feed_agg(ret_sink_t *s, binding_t *bindings, int bind_count) {
53745402
cbm_return_clause_t *ret = s->ret;
53755403
for (int bi = 0; bi < bind_count && !s->done; bi++) {
@@ -5586,6 +5614,11 @@ static void ret_sink_finish(ret_sink_t *s) {
55865614
for (int a = 0; a < s->agg_count; a++) {
55875615
ret_agg_emit_row(ret, &s->aggs[a], rb);
55885616
}
5617+
/* Without grouping keys the input is a single group even when it is
5618+
* empty; a grouped aggregation over no rows has no group to report. */
5619+
if (s->agg_count == 0 && ret_agg_is_ungrouped(ret)) {
5620+
ret_agg_emit_empty_row(ret, rb);
5621+
}
55895622
}
55905623
ret_sink_free(s);
55915624

‎tests/test_cypher.c‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,6 +4958,107 @@ TEST(cypher_issue305_count_star_alias) {
49584958
PASS();
49594959
}
49604960

4961+
/* Runs one query and asserts it answers exactly one row with `want` in the
4962+
* first column. */
4963+
static int expect_one_row(cbm_store_t *s, const char *project, const char *query,
4964+
const char *want) {
4965+
cbm_cypher_result_t r = {0};
4966+
int rc = cbm_cypher_execute(s, query, project, 0, &r);
4967+
ASSERT_EQ(rc, 0);
4968+
ASSERT_NULL(r.error);
4969+
ASSERT_EQ(r.row_count, 1);
4970+
ASSERT_STR_EQ(r.rows[0][0], want);
4971+
cbm_cypher_result_free(&r);
4972+
return 0;
4973+
}
4974+
4975+
/* #2742: an ungrouped aggregation is one row even when MATCH/WHERE produced no
4976+
* input rows. count(*) over nothing is 0, not an empty result: a caller asking
4977+
* "how many?" must not have to read "no rows" as "zero". */
4978+
TEST(cypher_count_star_empty_input_returns_zero) {
4979+
/* Empty graph: the project exists but holds no nodes. */
4980+
cbm_store_t *empty = cbm_store_open_memory();
4981+
cbm_store_upsert_project(empty, "empty", "/tmp/empty");
4982+
cbm_cypher_result_t r = {0};
4983+
int rc = cbm_cypher_execute(empty, "MATCH (n) RETURN count(*) AS total", "empty", 0, &r);
4984+
ASSERT_EQ(rc, 0);
4985+
ASSERT_NULL(r.error);
4986+
ASSERT_EQ(r.col_count, 1);
4987+
ASSERT_STR_EQ(r.columns[0], "total");
4988+
ASSERT_EQ(r.row_count, 1);
4989+
ASSERT_STR_EQ(r.rows[0][0], "0");
4990+
cbm_cypher_result_free(&r);
4991+
cbm_store_close(empty);
4992+
4993+
/* Nonempty graph, every row filtered out. */
4994+
cbm_store_t *s = setup_cypher_store();
4995+
ASSERT_EQ(expect_one_row(s, "test",
4996+
"MATCH (f:Function) WHERE f.name = \"NoSuchFunction\" "
4997+
"RETURN count(*) AS total",
4998+
"0"),
4999+
0);
5000+
ASSERT_EQ(expect_one_row(s, "test",
5001+
"MATCH (a)-[:CALLS]->(b) WHERE b.name = \"NoSuchFunction\" "
5002+
"RETURN count(*)",
5003+
"0"),
5004+
0);
5005+
ASSERT_EQ(expect_one_row(s, "test", "MATCH (n:NoSuchLabel) RETURN count(*)", "0"), 0);
5006+
ASSERT_EQ(expect_one_row(s, "test",
5007+
"MATCH (f:Function) WHERE f.name = \"NoSuchFunction\" "
5008+
"RETURN count(f) AS n",
5009+
"0"),
5010+
0);
5011+
ASSERT_EQ(expect_one_row(s, "test",
5012+
"MATCH (f:Function) WHERE f.name = \"NoSuchFunction\" "
5013+
"RETURN count(DISTINCT f.name) AS n",
5014+
"0"),
5015+
0);
5016+
5017+
/* The other aggregates in the same row take their empty-input values:
5018+
* sum 0, collect [], and avg/min/max null (rendered as ""). */
5019+
rc = cbm_cypher_execute(s,
5020+
"MATCH (f:Function) WHERE f.name = \"NoSuchFunction\" "
5021+
"RETURN count(*) AS c, sum(f.start_line) AS s, "
5022+
"avg(f.start_line) AS a, min(f.start_line) AS lo, "
5023+
"max(f.start_line) AS hi, collect(f.name) AS names",
5024+
"test", 0, &r);
5025+
ASSERT_EQ(rc, 0);
5026+
ASSERT_NULL(r.error);
5027+
ASSERT_EQ(r.row_count, 1);
5028+
ASSERT_STR_EQ(r.rows[0][0], "0");
5029+
ASSERT_STR_EQ(r.rows[0][1], "0");
5030+
ASSERT_STR_EQ(r.rows[0][2], "");
5031+
ASSERT_STR_EQ(r.rows[0][3], "");
5032+
ASSERT_STR_EQ(r.rows[0][4], "");
5033+
ASSERT_STR_EQ(r.rows[0][5], "[]");
5034+
cbm_cypher_result_free(&r);
5035+
5036+
/* A grouped aggregation has no group to report: still no rows. */
5037+
rc = cbm_cypher_execute(s,
5038+
"MATCH (f:Function) WHERE f.name = \"NoSuchFunction\" "
5039+
"RETURN f.label, count(*)",
5040+
"test", 0, &r);
5041+
ASSERT_EQ(rc, 0);
5042+
ASSERT_NULL(r.error);
5043+
ASSERT_EQ(r.row_count, 0);
5044+
cbm_cypher_result_free(&r);
5045+
5046+
/* SKIP still applies to the single aggregate row. */
5047+
rc = cbm_cypher_execute(s,
5048+
"MATCH (f:Function) WHERE f.name = \"NoSuchFunction\" "
5049+
"RETURN count(*) SKIP 1",
5050+
"test", 0, &r);
5051+
ASSERT_EQ(rc, 0);
5052+
ASSERT_NULL(r.error);
5053+
ASSERT_EQ(r.row_count, 0);
5054+
cbm_cypher_result_free(&r);
5055+
5056+
/* Nonempty input is unchanged. */
5057+
ASSERT_EQ(expect_one_row(s, "test", "MATCH (f:Function) RETURN count(*) AS total", "4"), 0);
5058+
cbm_store_close(s);
5059+
PASS();
5060+
}
5061+
49615062
/* Regression: projecting several computed/JSON properties in one row must yield
49625063
* DISTINCT values. node_prop previously returned a single shared static buffer,
49635064
* so every such column aliased the last property read — and because the search
@@ -6260,6 +6361,7 @@ SUITE(cypher) {
62606361
RUN_TEST(cypher_issue873_distinct_order_skip_limit_dedupes_before_skip);
62616362
RUN_TEST(cypher_issue252_tointeger);
62626363
RUN_TEST(cypher_issue305_count_star_alias);
6364+
RUN_TEST(cypher_count_star_empty_input_returns_zero);
62636365
RUN_TEST(cypher_exec_where_eq);
62646366
RUN_TEST(cypher_exec_unlabeled_where_beyond_result_limit_issue1196);
62656367
RUN_TEST(cypher_exec_aggregate_sees_all_edges_beyond_expansion_cap_issue1196);

0 commit comments

Comments
 (0)