@@ -2300,7 +2300,6 @@ static const char *node_string_field(const cbm_node_t *n, const char *prop) {
23002300/* Get node property by name.
23012301 * store may be NULL; only needed for virtual degree properties. */
23022302static const char * json_extract_prop (const char * json , const char * key , char * buf , size_t buf_sz );
2303- static void node_fields_free (cbm_node_t * n ); /* defined below; used by the stub re-fetch */
23042303
23052304static const char * node_prop (const cbm_node_t * n , const char * prop , cbm_store_t * store ) {
23062305 if (!n || !prop ) {
@@ -2310,8 +2309,6 @@ static const char *node_prop(const cbm_node_t *n, const char *prop, cbm_store_t
23102309 if (str && str [0 ]) {
23112310 return str ;
23122311 }
2313- /* Note: a string field that exists but is empty ("") falls through here so a
2314- * WITH-aggregation node stub (below) can re-fetch it. */
23152312 /* Computed and JSON-derived values live in rotating thread-local buffers:
23162313 * a single row (or an ORDER-BY comparison) reads several of these before any
23172314 * of them is copied out, so returning one shared static buffer would alias
@@ -2349,40 +2346,6 @@ static const char *node_prop(const cbm_node_t *n, const char *prop, cbm_store_t
23492346 return v ;
23502347 }
23512348 }
2352- /* WITH aggregation carries a node group var by id + name only (the group key
2353- * is the node name), so every other property is absent on the stub. Detect
2354- * the stub (id set, but the full string fields were never populated) and
2355- * re-fetch the node so RETURN g.file_path / g.label / g.<metric> project
2356- * correctly instead of returning blank. The gate is heuristic, not an exact
2357- * stub discriminator: a real bound node with NULL label AND file_path would
2358- * also match, but in that case the worst case is one redundant indexed fetch
2359- * that returns the same value — never a wrong result. */
2360- if (store && n -> id > 0 && !n -> file_path && !n -> label ) {
2361- cbm_node_t full = {0 };
2362- if (cbm_store_find_node_by_id (store , n -> id , & full ) == CBM_STORE_OK ) {
2363- const char * res = NULL ;
2364- const char * rv = node_string_field (& full , prop );
2365- if (rv && rv [0 ]) {
2366- snprintf (out , CBM_SZ_512 , "%s" , rv );
2367- res = out ;
2368- } else if (strcmp (prop , "start_line" ) == 0 ) {
2369- snprintf (out , CBM_SZ_512 , "%d" , full .start_line );
2370- res = out ;
2371- } else if (strcmp (prop , "end_line" ) == 0 ) {
2372- snprintf (out , CBM_SZ_512 , "%d" , full .end_line );
2373- res = out ;
2374- } else if (full .properties_json && full .properties_json [0 ] == '{' ) {
2375- const char * jv = json_extract_prop (full .properties_json , prop , out , CBM_SZ_512 );
2376- if (jv && jv [0 ]) {
2377- res = out ;
2378- }
2379- }
2380- node_fields_free (& full );
2381- if (res ) {
2382- return res ;
2383- }
2384- }
2385- }
23862349 return "" ;
23872350}
23882351
@@ -3842,16 +3805,39 @@ static void distinct_list_add(char ***list, int *count, const char *val) {
38423805 (* list )[idx ] = heap_strdup (val );
38433806}
38443807
3808+ /* Resolve one WITH ORDER BY key on a projected binding. The key is either a
3809+ * projected name (`c`, or an unaliased `f.name`) or a property of a carried
3810+ * node (`g.start_line` after `WITH f AS g`, #2208): an exact projected name
3811+ * wins, otherwise `var.prop` is split and resolved on the carried node. */
3812+ static const char * order_key_value (binding_t * b , const char * key ) {
3813+ const char * dot = strchr (key , '.' );
3814+ if (!dot ) {
3815+ return binding_get_virtual (b , key , NULL );
3816+ }
3817+ for (int i = 0 ; i < b -> var_count ; i ++ ) {
3818+ if (strcmp (b -> var_names [i ], key ) == 0 ) {
3819+ return binding_get_virtual (b , key , NULL );
3820+ }
3821+ }
3822+ char var [CBM_SZ_256 ];
3823+ size_t vlen = (size_t )(dot - key );
3824+ if (vlen >= sizeof (var )) {
3825+ return "" ;
3826+ }
3827+ memcpy (var , key , vlen );
3828+ var [vlen ] = '\0' ;
3829+ return binding_get_virtual (b , var , dot + SKIP_ONE );
3830+ }
3831+
38453832/* Sort bindings by the ORDER BY key list (virtual variables) using bubble
38463833 * sort; later keys break ties, direction is per key (#1334). */
38473834static void sort_bindings (binding_t * vbindings , int count , const cbm_return_clause_t * wc ) {
38483835 for (int i = 0 ; i < count - SKIP_ONE ; i ++ ) {
38493836 for (int j = 0 ; j < count - i - SKIP_ONE ; j ++ ) {
38503837 int cmp = 0 ;
38513838 for (int k = 0 ; k < wc -> order_key_count && cmp == 0 ; k ++ ) {
3852- const char * va = binding_get_virtual (& vbindings [j ], wc -> order_keys [k ], NULL );
3853- const char * vb2 =
3854- binding_get_virtual (& vbindings [j + SKIP_ONE ], wc -> order_keys [k ], NULL );
3839+ const char * va = order_key_value (& vbindings [j ], wc -> order_keys [k ]);
3840+ const char * vb2 = order_key_value (& vbindings [j + SKIP_ONE ], wc -> order_keys [k ]);
38553841 char * ea = NULL ;
38563842 char * eb = NULL ;
38573843 double da = strtod (va , & ea );
@@ -3925,9 +3911,28 @@ typedef struct {
39253911 double * mins , * maxs ;
39263912 char * * * distinct_lists ; /* per-item set of seen values for COUNT(DISTINCT) */
39273913 int * distinct_n ; /* per-item distinct count (#239) */
3928- int64_t * group_node_ids ; /* per-item node id when the group var is a node (0 = not ) */
3914+ cbm_node_t * group_nodes ; /* per-item carried node (deep copy; id 0 = not a node) */
39293915} with_agg_t ;
39303916
3917+ /* A WITH item that is a bare, bound node variable (`WITH f`, `WITH f AS g`)
3918+ * carries the node itself, not a scalar: return that node, else NULL. Property
3919+ * items, functions (labels/id/keys/...) and unbound OPTIONAL vars (id 0) stay
3920+ * scalar. The carried node keeps every field, so a later g.<prop> resolves
3921+ * exactly as it would without the WITH (#2208). */
3922+ static const cbm_node_t * with_item_carried_node (const cbm_return_item_t * item , binding_t * b ) {
3923+ if (item -> func || item -> property || !item -> variable ) {
3924+ return NULL ;
3925+ }
3926+ const cbm_node_t * n = binding_get (b , item -> variable );
3927+ return (n && n -> id > 0 ) ? n : NULL ;
3928+ }
3929+
3930+ /* Name a carried node is bound under: the AST-owned alias or variable, which
3931+ * outlives every binding of the query (binding_set does not copy the name). */
3932+ static const char * with_item_node_alias (const cbm_return_item_t * item ) {
3933+ return item -> alias ? item -> alias : item -> variable ;
3934+ }
3935+
39313936/* Build a group key from non-aggregate WITH items */
39323937static int with_agg_build_key (cbm_return_clause_t * wc , binding_t * b , char * key , size_t key_sz ) {
39333938 int kl = 0 ;
@@ -3936,7 +3941,12 @@ static int with_agg_build_key(cbm_return_clause_t *wc, binding_t *b, char *key,
39363941 continue ;
39373942 }
39383943 char vbuf [CBM_SZ_512 ];
3939- const char * v = project_item (b , & wc -> items [ci ], vbuf , sizeof (vbuf ));
3944+ const cbm_node_t * gn = with_item_carried_node (& wc -> items [ci ], b );
3945+ if (gn ) {
3946+ /* Group a node by identity, never by its (non-unique) name. */
3947+ snprintf (vbuf , sizeof (vbuf ), "#%lld" , (long long )gn -> id );
3948+ }
3949+ const char * v = gn ? vbuf : project_item (b , & wc -> items [ci ], vbuf , sizeof (vbuf ));
39403950 kl += snprintf (key + kl , key_sz - (size_t )kl , "%s|" , v );
39413951 if (kl >= (int )key_sz ) {
39423952 kl = (int )key_sz - SKIP_ONE ;
@@ -3966,7 +3976,7 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
39663976 (* aggs )[found ].maxs = calloc (wc -> count , sizeof (double ));
39673977 (* aggs )[found ].distinct_lists = calloc (wc -> count , sizeof (char * * ));
39683978 (* aggs )[found ].distinct_n = calloc (wc -> count , sizeof (int ));
3969- (* aggs )[found ].group_node_ids = calloc (wc -> count , sizeof (int64_t ));
3979+ (* aggs )[found ].group_nodes = calloc (wc -> count , sizeof (cbm_node_t ));
39703980 for (int ci = 0 ; ci < wc -> count ; ci ++ ) {
39713981 (* aggs )[found ].mins [ci ] = CYP_DBL_MAX ;
39723982 (* aggs )[found ].maxs [ci ] = - CYP_DBL_MAX ;
@@ -3979,18 +3989,9 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
39793989 char vbuf [CBM_SZ_512 ];
39803990 const char * v = project_item (b , & wc -> items [ci ], vbuf , sizeof (vbuf ));
39813991 (* aggs )[found ].group_vals [ci ] = heap_strdup (v );
3982- /* If this group item is a bare node variable, remember its id so the
3983- * carried virtual var can re-fetch any property (group_vals holds only
3984- * the name). Excludes entity-introspection funcs (labels/id/keys/
3985- * properties): those project a scalar off the node (via project_item
3986- * above), not the node itself, so the carried id must not be set or a
3987- * later alias.property re-fetches the source node's real properties
3988- * instead of returning empty for the non-node alias. */
3989- if (!wc -> items [ci ].func && !wc -> items [ci ].property && wc -> items [ci ].variable ) {
3990- cbm_node_t * gn = binding_get (b , wc -> items [ci ].variable );
3991- if (gn ) {
3992- (* aggs )[found ].group_node_ids [ci ] = gn -> id ;
3993- }
3992+ const cbm_node_t * gn = with_item_carried_node (& wc -> items [ci ], b );
3993+ if (gn ) {
3994+ node_deep_copy (& (* aggs )[found ].group_nodes [ci ], gn );
39943995 }
39953996 }
39963997 return found ;
@@ -4033,11 +4034,14 @@ static void with_agg_format(const char *func, with_agg_t *agg, int ci, char *buf
40334034 }
40344035}
40354036
4036- /* Add a virtual variable binding for one WITH item */
4037+ /* Add a scalar virtual variable binding for one WITH item. The value lives in
4038+ * .name; the owned alias string (var_names points at it) lives in .project,
4039+ * which no property accessor exposes. It used to live in .qualified_name, so
4040+ * alias.qualified_name / keys(alias) returned the variable name as data (#2208). */
40374041static void with_add_vbinding_var (binding_t * vb , const char * alias , const char * val ) {
4038- cbm_node_t vn = {.name = heap_strdup (val ), .qualified_name = heap_strdup (alias )};
4042+ cbm_node_t vn = {.name = heap_strdup (val ), .project = heap_strdup (alias )};
40394043 if (vb -> var_count < CYP_BUF_16 ) {
4040- vb -> var_names [vb -> var_count ] = vn .qualified_name ;
4044+ vb -> var_names [vb -> var_count ] = vn .project ;
40414045 vb -> var_nodes [vb -> var_count ] = vn ;
40424046 vb -> var_count ++ ;
40434047 }
@@ -4062,7 +4066,12 @@ static void with_agg_free(with_agg_t *aggs, int agg_cnt, int item_count) {
40624066 free (aggs [a ].maxs );
40634067 free (aggs [a ].distinct_lists );
40644068 free (aggs [a ].distinct_n );
4065- free (aggs [a ].group_node_ids );
4069+ if (aggs [a ].group_nodes ) {
4070+ for (int ci = 0 ; ci < item_count ; ci ++ ) {
4071+ node_fields_free (& aggs [a ].group_nodes [ci ]);
4072+ }
4073+ }
4074+ free (aggs [a ].group_nodes );
40664075 }
40674076 free (aggs );
40684077}
@@ -4102,13 +4111,10 @@ static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings,
41024111 with_agg_format (wc -> items [ci ].func , & aggs [a ], ci , vbuf , sizeof (vbuf ));
41034112 }
41044113 with_add_vbinding_var (& vb , alias , vbuf );
4114+ } else if (aggs [a ].group_nodes [ci ].id > 0 ) {
4115+ binding_set (& vb , with_item_node_alias (& wc -> items [ci ]), & aggs [a ].group_nodes [ci ]);
41054116 } else {
41064117 with_add_vbinding_var (& vb , alias , aggs [a ].group_vals [ci ]);
4107- /* Tag the carried virtual var with the node id (when the group
4108- * var is a node) so node_prop can re-fetch its full properties. */
4109- if (aggs [a ].group_node_ids [ci ] > 0 && vb .var_count > 0 ) {
4110- vb .var_nodes [vb .var_count - 1 ].id = aggs [a ].group_node_ids [ci ];
4111- }
41124118 }
41134119 }
41144120 (* vbindings )[(* vcount )++ ] = vb ;
@@ -4125,6 +4131,11 @@ static void execute_with_simple(cbm_return_clause_t *wc, binding_t *bindings, in
41254131 for (int ci = 0 ; ci < wc -> count ; ci ++ ) {
41264132 char name_buf [CBM_SZ_256 ];
41274133 const char * alias = resolve_item_alias (& wc -> items [ci ], name_buf , sizeof (name_buf ));
4134+ const cbm_node_t * carried = with_item_carried_node (& wc -> items [ci ], & bindings [bi ]);
4135+ if (carried ) {
4136+ binding_set (& vb , with_item_node_alias (& wc -> items [ci ]), carried );
4137+ continue ;
4138+ }
41284139 char func_buf [CBM_SZ_512 ];
41294140 const char * val =
41304141 project_item (& bindings [bi ], & wc -> items [ci ], func_buf , sizeof (func_buf ));
0 commit comments