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

Function expand_additional_patterns

src/cypher/cypher.c:4746–4790  ·  view source on GitHub ↗

Expand additional MATCH patterns (pi >= 1) */

Source from the content-addressed store, hash-verified

4744
4745/* Expand additional MATCH patterns (pi >= 1) */
4746static int expand_additional_patterns(cbm_store_t *store, cbm_query_t *q, const char *project,
4747 int max_rows, binding_t **bindings, int *bind_count,
4748 int *bind_cap) {
4749 for (int pi = SKIP_ONE; pi < q->pattern_count; pi++) {
4750 cbm_pattern_t *patn = &q->patterns[pi];
4751 bool opt = q->pattern_optional[pi];
4752 const char *nvar = patn->nodes[0].variable ? patn->nodes[0].variable : "_n_extra";
4753 bool start_bound = *bind_count > 0 && binding_get(&(*bindings)[0], nvar) != NULL;
4754
4755 if (start_bound && patn->rel_count > 0) {
4756 const char *tv = nvar;
4757 expand_pattern_rels(store, patn, bindings, bind_count, bind_cap, &tv, opt);
4758 continue;
4759 }
4760
4761 /* Single-rel pattern whose START is unbound but whose TERMINAL is already
4762 * bound: drive from the bound terminal instead of scanning all nodes for
4763 * the start var (avoids the int-overflow OOB write and the c-IS-NULL
4764 * corruption of #627). */
4765 if (!start_bound && patn->rel_count == 1 && *bind_count > 0) {
4766 const char *term_var = patn->nodes[1].variable;
4767 bool term_bound = term_var && binding_get(&(*bindings)[0], term_var) != NULL;
4768 if (term_bound) {
4769 expand_from_bound_terminal(store, patn, bindings, bind_count, nvar, opt);
4770 continue;
4771 }
4772 }
4773
4774 cbm_node_t *extra_nodes = NULL;
4775 int extra_count = 0;
4776 scan_pattern_nodes(store, project, &patn->nodes[0], &extra_nodes, &extra_count);
4777 int rc = 0;
4778 if (patn->rel_count == 0) {
4779 rc = cross_join_nodes(bindings, bind_count, extra_nodes, extra_count, nvar, opt);
4780 } else {
4781 cross_join_with_rels(store, patn, bindings, bind_count, extra_nodes, extra_count, nvar,
4782 opt);
4783 }
4784 cbm_store_free_nodes(extra_nodes, extra_count);
4785 if (rc != 0) {
4786 return rc; /* allocation refused/failed: propagate as a query error */
4787 }
4788 }
4789 return 0;
4790}
4791
4792/* Project RETURN clause results */
4793static void execute_return_clause(cbm_query_t *q, cbm_return_clause_t *ret, binding_t *bindings,

Callers 1

execute_singleFunction · 0.85

Calls 7

binding_getFunction · 0.85
expand_pattern_relsFunction · 0.85
scan_pattern_nodesFunction · 0.85
cross_join_nodesFunction · 0.85
cross_join_with_relsFunction · 0.85
cbm_store_free_nodesFunction · 0.85

Tested by

no test coverage detected