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