@@ -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