1212#include "foundation/platform.h"
1313#include "foundation/limits.h"
1414#include "foundation/log.h"
15+ #include "foundation/hash_table.h"
16+ #include "foundation/mem_core.h"
1517
1618enum {
1719 CYP_BUF_16 = 16 ,
@@ -2837,6 +2839,104 @@ static void rb_add_row(result_builder_t *rb, const char **values) {
28372839 rb -> rows [rb -> row_count ++ ] = row ;
28382840}
28392841
2842+ /* ── DISTINCT while projecting (#1364) ─────────────────────────────
2843+ *
2844+ * RETURN DISTINCT used to project up to the engine row cap and only then drop
2845+ * duplicates, so a value first seen past the cap never reached the result: on
2846+ * a 150k-edge index `RETURN DISTINCT type(r)` lost its one HAS_BRANCH edge.
2847+ * The projection loops now de-duplicate as they go, so the cap counts DISTINCT
2848+ * rows. Memory stays bounded by the cap: only admitted rows are remembered;
2849+ * rows past the cap are probed (no insert) solely to decide `truncated`. */
2850+ typedef struct {
2851+ CBMHashTable * seen ; /* row key -> the same heap key; NULL = not DISTINCT */
2852+ } rb_distinct_t ;
2853+
2854+ static void rb_distinct_init (rb_distinct_t * d , bool distinct ) {
2855+ d -> seen = distinct ? cbm_ht_create (CBM_SZ_64 ) : NULL ;
2856+ }
2857+
2858+ static void rb_distinct_free_key (const char * key , void * value , void * userdata ) {
2859+ (void )key ;
2860+ (void )userdata ;
2861+ cbm_free (CBM_MEM_CLASS_OTHER , value );
2862+ }
2863+
2864+ static void rb_distinct_free (rb_distinct_t * d ) {
2865+ if (d -> seen ) {
2866+ cbm_ht_foreach (d -> seen , rb_distinct_free_key , NULL );
2867+ cbm_ht_free (d -> seen );
2868+ d -> seen = NULL ;
2869+ }
2870+ }
2871+
2872+ /* Length-prefixed row key ("<len>:<value>" per column), so no value content
2873+ * can make two different rows collide. NULL projects as "" (as rb_add_row). */
2874+ static char * rb_distinct_row_key (const char * * vals , int n ) {
2875+ size_t total = SKIP_ONE ;
2876+ for (int i = 0 ; i < n ; i ++ ) {
2877+ total += (vals [i ] ? strlen (vals [i ]) : 0 ) + CBM_SZ_32 ;
2878+ }
2879+ char * key = cbm_alloc (CBM_MEM_CLASS_OTHER , total );
2880+ if (!key ) {
2881+ return NULL ;
2882+ }
2883+ size_t pos = 0 ;
2884+ key [0 ] = '\0' ;
2885+ for (int i = 0 ; i < n ; i ++ ) {
2886+ const char * v = vals [i ] ? vals [i ] : "" ;
2887+ int w = snprintf (key + pos , total - pos , "%zu:%s" , strlen (v ), v );
2888+ if (w > 0 ) {
2889+ pos += (size_t )w ;
2890+ }
2891+ }
2892+ return key ;
2893+ }
2894+
2895+ /* Admission for one projected row. Returns false for a row DISTINCT already
2896+ * holds. On true, *key_out is the key to hand to rb_distinct_keep (NULL when
2897+ * not DISTINCT, or when the key allocation failed — the row is then admitted
2898+ * without de-duplication rather than dropped). */
2899+ static bool rb_distinct_is_new (const rb_distinct_t * d , const char * * vals , int n , char * * key_out ) {
2900+ * key_out = NULL ;
2901+ if (!d -> seen ) {
2902+ return true;
2903+ }
2904+ char * key = rb_distinct_row_key (vals , n );
2905+ if (!key ) {
2906+ return true;
2907+ }
2908+ if (cbm_ht_has (d -> seen , key )) {
2909+ cbm_free (CBM_MEM_CLASS_OTHER , key );
2910+ return false;
2911+ }
2912+ * key_out = key ;
2913+ return true;
2914+ }
2915+
2916+ static void rb_distinct_keep (rb_distinct_t * d , char * key ) {
2917+ if (key ) {
2918+ cbm_ht_set (d -> seen , key , key );
2919+ }
2920+ }
2921+
2922+ /* Add one projected row under a row cap, de-duplicating when DISTINCT.
2923+ * Returns false once a NEW row arrives with the cap already full: the caller
2924+ * stops there, and flags truncation when the cap is the engine budget. A
2925+ * duplicate past the cap is neither kept nor evidence of truncation. */
2926+ static bool rb_add_row_capped (result_builder_t * rb , rb_distinct_t * d , const char * * vals , int cap ) {
2927+ char * key = NULL ;
2928+ if (!rb_distinct_is_new (d , vals , rb -> col_count , & key )) {
2929+ return true;
2930+ }
2931+ if (rb -> row_count >= cap ) {
2932+ cbm_free (CBM_MEM_CLASS_OTHER , key );
2933+ return false;
2934+ }
2935+ rb_distinct_keep (d , key );
2936+ rb_add_row (rb , vals );
2937+ return true;
2938+ }
2939+
28402940/* ── Main execution ─────────────────────────────────────────────── */
28412941
28422942/* Hard ceiling: queries returning more than this trigger an error instead of data.
@@ -4359,14 +4459,20 @@ static void execute_return_star_after_with(cbm_query_t *q, binding_t *bindings,
43594459 cols [i ] = resolve_item_alias (& wc -> items [i ], name_bufs [i ], sizeof (name_bufs [i ]));
43604460 }
43614461 rb_set_columns (rb , cols , col_n );
4362- for (int bi = 0 ; bi < bind_count && rb -> row_count < max_rows ; bi ++ ) {
4462+ rb_distinct_t seen ;
4463+ rb_distinct_init (& seen , q -> ret && q -> ret -> distinct );
4464+ for (int bi = 0 ; bi < bind_count ; bi ++ ) {
43634465 const char * vals [CYP_MAX_VARS ];
43644466 for (int i = 0 ; i < col_n ; i ++ ) {
43654467 cbm_node_t * vn = binding_get (& bindings [bi ], cols [i ]);
43664468 vals [i ] = vn && vn -> name ? vn -> name : "" ;
43674469 }
4368- rb_add_row (rb , vals );
4470+ if (!rb_add_row_capped (rb , & seen , vals , max_rows )) {
4471+ g_cypher_truncated = true;
4472+ break ;
4473+ }
43694474 }
4475+ rb_distinct_free (& seen );
43704476}
43714477
43724478static void execute_return_star (cbm_query_t * q , binding_t * bindings , int bind_count , int max_rows ,
@@ -4381,19 +4487,22 @@ static void execute_return_star(cbm_query_t *q, binding_t *bindings, int bind_co
43814487 int projection_cap = max_rows ;
43824488 bool cap_is_engine_budget = true;
43834489 cbm_return_clause_t * ret = q -> ret ;
4384- if (ret && ret -> limit >= 0 && ret -> limit <= max_rows && ! ret -> distinct &&
4385- ret -> order_key_count == 0 && ret -> skip <= 0 ) {
4386- projection_cap = ret -> limit ;
4490+ if (ret && ret -> limit >= 0 && ret -> limit <= max_rows && ret -> order_key_count == 0 &&
4491+ ret -> skip <= 0 ) {
4492+ projection_cap = ret -> limit ; /* DISTINCT: the cap counts distinct rows (#1364) */
43874493 cap_is_engine_budget = false;
43884494 }
4389- for (int bi = 0 ; bi < bind_count && rb -> row_count < projection_cap ; bi ++ ) {
4495+ rb_distinct_t seen ;
4496+ rb_distinct_init (& seen , ret && ret -> distinct );
4497+ for (int bi = 0 ; bi < bind_count ; bi ++ ) {
43904498 const char * vals [CBM_SZ_128 ];
43914499 project_star_row (& bindings [bi ], vars , vc , vals );
4392- rb_add_row ( rb , vals );
4393- }
4394- if ( cap_is_engine_budget && bind_count > projection_cap ) {
4395- g_cypher_truncated = true;
4500+ if (! rb_add_row_capped ( rb , & seen , vals , projection_cap )) {
4501+ g_cypher_truncated = g_cypher_truncated || cap_is_engine_budget ;
4502+ break ;
4503+ }
43964504 }
4505+ rb_distinct_free (& seen );
43974506}
43984507
43994508/* Format an aggregate value into buf based on function name */
@@ -4662,22 +4771,25 @@ static void execute_return_simple(cbm_return_clause_t *ret, binding_t *bindings,
46624771 int max_rows , result_builder_t * rb ) {
46634772 int proj_cap = max_rows ;
46644773 bool cap_is_engine_budget = true;
4665- if (ret -> limit >= 0 && ! ret -> distinct && ret -> order_key_count == 0 && ret -> skip <= 0 ) {
4666- proj_cap = ret -> limit ;
4774+ if (ret -> limit >= 0 && ret -> order_key_count == 0 && ret -> skip <= 0 ) {
4775+ proj_cap = ret -> limit ; /* DISTINCT: the cap counts distinct rows (#1364) */
46674776 cap_is_engine_budget = false;
46684777 }
4669- for (int bi = 0 ; bi < bind_count && rb -> row_count < proj_cap ; bi ++ ) {
4778+ rb_distinct_t seen ;
4779+ rb_distinct_init (& seen , ret -> distinct );
4780+ for (int bi = 0 ; bi < bind_count ; bi ++ ) {
46704781 const char * vals [CBM_SZ_32 ];
46714782 char func_bufs [CBM_SZ_32 ][CBM_SZ_512 ];
46724783 for (int ci = 0 ; ci < ret -> count ; ci ++ ) {
46734784 vals [ci ] =
46744785 project_item (& bindings [bi ], & ret -> items [ci ], func_bufs [ci ], sizeof (func_bufs [ci ]));
46754786 }
4676- rb_add_row ( rb , vals );
4677- }
4678- if ( cap_is_engine_budget && bind_count > proj_cap ) {
4679- g_cypher_truncated = true;
4787+ if (! rb_add_row_capped ( rb , & seen , vals , proj_cap )) {
4788+ g_cypher_truncated = g_cypher_truncated || cap_is_engine_budget ;
4789+ break ;
4790+ }
46804791 }
4792+ rb_distinct_free (& seen );
46814793}
46824794
46834795/* Build default 3-column headers (name, qualified_name, label) per variable */
@@ -5019,7 +5131,9 @@ static void execute_return_clause(cbm_query_t *q, cbm_return_clause_t *ret, bind
50195131 }
50205132 }
50215133
5022- if (ret -> distinct ) {
5134+ /* The simple and star projections de-duplicate while projecting (#1364);
5135+ * only aggregate rows still need the post-pass. */
5136+ if (ret -> distinct && has_agg && !ret -> star ) {
50235137 rb_apply_distinct (rb );
50245138 }
50255139 rb_apply_order_by (rb , ret );
0 commit comments