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

Function expand_additional_patterns

src/cypher/cypher.c:4747–4791  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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