| 4102 | } |
| 4103 | |
| 4104 | static void execute_with_clause(cbm_query_t *q, binding_t **bindings_ptr, int *bind_count_ptr) { |
| 4105 | cbm_return_clause_t *wc = q->with_clause; |
| 4106 | if (!wc) { |
| 4107 | return; |
| 4108 | } |
| 4109 | binding_t *bindings = *bindings_ptr; |
| 4110 | int bind_count = *bind_count_ptr; |
| 4111 | |
| 4112 | binding_t *vbindings = malloc((bind_count + SKIP_ONE) * sizeof(binding_t)); |
| 4113 | int vcount = 0; |
| 4114 | |
| 4115 | bool has_agg = false; |
| 4116 | for (int i = 0; i < wc->count; i++) { |
| 4117 | if (is_aggregate_func(wc->items[i].func)) { |
| 4118 | has_agg = true; |
| 4119 | break; |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | if (has_agg) { |
| 4124 | execute_with_aggregate(wc, bindings, bind_count, &vbindings, &vcount); |
| 4125 | } else { |
| 4126 | execute_with_simple(wc, bindings, bind_count, vbindings, &vcount); |
| 4127 | } |
| 4128 | |
| 4129 | /* WITH DISTINCT: dedup projected rows (no-op for aggregation, which already |
| 4130 | * collapses to one row per group). */ |
| 4131 | if (wc->distinct) { |
| 4132 | with_apply_distinct(wc, vbindings, &vcount); |
| 4133 | } |
| 4134 | |
| 4135 | with_sort_skip_limit(wc, vbindings, &vcount); |
| 4136 | |
| 4137 | for (int bi = 0; bi < bind_count; bi++) { |
| 4138 | binding_free(&bindings[bi]); |
| 4139 | } |
| 4140 | free(bindings); |
| 4141 | |
| 4142 | if (q->post_with_where) { |
| 4143 | filter_bindings_where(q->post_with_where, vbindings, &vcount); |
| 4144 | } |
| 4145 | |
| 4146 | *bindings_ptr = vbindings; |
| 4147 | *bind_count_ptr = vcount; |
| 4148 | } |
| 4149 | |
| 4150 | /* ── Execute a single query (no UNION recursion) ──────────────── */ |
| 4151 |
no test coverage detected