Expand additional MATCH patterns (pi >= 1) */
| 5811 | |
| 5812 | /* Expand additional MATCH patterns (pi >= 1) */ |
| 5813 | static int expand_additional_patterns(cbm_store_t *store, cbm_query_t *q, const char *project, |
| 5814 | int max_rows, binding_t **bindings, int *bind_count, |
| 5815 | int *bind_cap) { |
| 5816 | for (int pi = SKIP_ONE; pi < q->pattern_count; pi++) { |
| 5817 | cbm_pattern_t *patn = &q->patterns[pi]; |
| 5818 | bool opt = q->pattern_optional[pi]; |
| 5819 | const char *nvar = patn->nodes[0].variable ? patn->nodes[0].variable : "_n_extra"; |
| 5820 | bool start_bound = *bind_count > 0 && binding_get(&(*bindings)[0], nvar) != NULL; |
| 5821 | |
| 5822 | if (start_bound && patn->rel_count > 0) { |
| 5823 | const char *tv = nvar; |
| 5824 | expand_pattern_rels(store, patn, bindings, bind_count, bind_cap, &tv, opt); |
| 5825 | continue; |
| 5826 | } |
| 5827 | |
| 5828 | /* Single-rel pattern whose START is unbound but whose TERMINAL is already |
| 5829 | * bound: drive from the bound terminal instead of scanning all nodes for |
| 5830 | * the start var (avoids the int-overflow OOB write and the c-IS-NULL |
| 5831 | * corruption of #627). */ |
| 5832 | if (!start_bound && patn->rel_count == 1 && *bind_count > 0) { |
| 5833 | const char *term_var = patn->nodes[1].variable; |
| 5834 | bool term_bound = term_var && binding_get(&(*bindings)[0], term_var) != NULL; |
| 5835 | if (term_bound) { |
| 5836 | expand_from_bound_terminal(store, patn, bindings, bind_count, nvar, opt); |
| 5837 | continue; |
| 5838 | } |
| 5839 | } |
| 5840 | |
| 5841 | cbm_node_t *extra_nodes = NULL; |
| 5842 | int extra_count = 0; |
| 5843 | scan_pattern_nodes(store, project, &patn->nodes[0], &extra_nodes, &extra_count); |
| 5844 | int rc = 0; |
| 5845 | if (patn->rel_count == 0) { |
| 5846 | rc = cross_join_nodes(bindings, bind_count, extra_nodes, extra_count, nvar, opt); |
| 5847 | } else { |
| 5848 | cross_join_with_rels(store, patn, bindings, bind_count, extra_nodes, extra_count, nvar, |
| 5849 | opt); |
| 5850 | } |
| 5851 | cbm_store_free_nodes(extra_nodes, extra_count); |
| 5852 | if (rc != 0) { |
| 5853 | return rc; /* allocation refused/failed: propagate as a query error */ |
| 5854 | } |
| 5855 | } |
| 5856 | return 0; |
| 5857 | } |
| 5858 | |
| 5859 | /* ── Planner: seed a single-hop pattern from its selective end ────────────── |
| 5860 | * |
no test coverage detected