MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / execute_with_aggregate

Function execute_with_aggregate

src/cypher/cypher.c:3974–4020  ·  view source on GitHub ↗

Execute WITH aggregation path */

Source from the content-addressed store, hash-verified

3972
3973/* Execute WITH aggregation path */
3974static void execute_with_aggregate(cbm_return_clause_t *wc, binding_t *bindings, int bind_count,
3975 binding_t **vbindings, int *vcount) {
3976 int agg_cap = CBM_SZ_256;
3977 with_agg_t *aggs = calloc(agg_cap, sizeof(with_agg_t));
3978 int agg_cnt = 0;
3979
3980 for (int bi = 0; bi < bind_count; bi++) {
3981 char key[CBM_SZ_1K] = "";
3982 with_agg_build_key(wc, &bindings[bi], key, sizeof(key));
3983 int found = with_agg_find_or_create(&aggs, &agg_cnt, &agg_cap, wc, &bindings[bi], key);
3984 with_agg_accumulate(&aggs[found], wc, &bindings[bi]);
3985 }
3986
3987 *vbindings = safe_realloc(*vbindings, (agg_cnt + SKIP_ONE) * sizeof(binding_t));
3988 if (!*vbindings) {
3989 with_agg_free(aggs, agg_cnt, wc->count);
3990 return;
3991 }
3992 for (int a = 0; a < agg_cnt; a++) {
3993 binding_t vb = {0};
3994 /* Carry the store so node_prop can re-fetch a carried node's properties
3995 * (and compute in_degree/out_degree) on the projected virtual binding. */
3996 vb.store = (bind_count > 0) ? bindings[0].store : NULL;
3997 for (int ci = 0; ci < wc->count; ci++) {
3998 char name_buf[CBM_SZ_256];
3999 const char *alias = resolve_item_alias(&wc->items[ci], name_buf, sizeof(name_buf));
4000 if (is_aggregate_func(wc->items[ci].func)) {
4001 char vbuf[CBM_SZ_64];
4002 if (wc->items[ci].distinct && strcmp(wc->items[ci].func, "COUNT") == 0) {
4003 snprintf(vbuf, sizeof(vbuf), "%d", aggs[a].distinct_n[ci]); /* #239 */
4004 } else {
4005 with_agg_format(wc->items[ci].func, &aggs[a], ci, vbuf, sizeof(vbuf));
4006 }
4007 with_add_vbinding_var(&vb, alias, vbuf);
4008 } else {
4009 with_add_vbinding_var(&vb, alias, aggs[a].group_vals[ci]);
4010 /* Tag the carried virtual var with the node id (when the group
4011 * var is a node) so node_prop can re-fetch its full properties. */
4012 if (aggs[a].group_node_ids[ci] > 0 && vb.var_count > 0) {
4013 vb.var_nodes[vb.var_count - 1].id = aggs[a].group_node_ids[ci];
4014 }
4015 }
4016 }
4017 (*vbindings)[(*vcount)++] = vb;
4018 }
4019 with_agg_free(aggs, agg_cnt, wc->count);
4020}
4021
4022/* Execute WITH simple (non-aggregate) projection */
4023static void execute_with_simple(cbm_return_clause_t *wc, binding_t *bindings, int bind_count,

Callers 1

execute_with_clauseFunction · 0.85

Calls 9

with_agg_build_keyFunction · 0.85
with_agg_find_or_createFunction · 0.85
with_agg_accumulateFunction · 0.85
safe_reallocFunction · 0.85
with_agg_freeFunction · 0.85
resolve_item_aliasFunction · 0.85
is_aggregate_funcFunction · 0.85
with_agg_formatFunction · 0.85
with_add_vbinding_varFunction · 0.85

Tested by

no test coverage detected