| 3757 | } |
| 3758 | |
| 3759 | static void execute_with_clause(cbm_query_t *q, binding_t **bindings_ptr, int *bind_count_ptr) { |
| 3760 | cbm_return_clause_t *wc = q->with_clause; |
| 3761 | if (!wc) { |
| 3762 | return; |
| 3763 | } |
| 3764 | binding_t *bindings = *bindings_ptr; |
| 3765 | int bind_count = *bind_count_ptr; |
| 3766 | |
| 3767 | binding_t *vbindings = malloc((bind_count + SKIP_ONE) * sizeof(binding_t)); |
| 3768 | int vcount = 0; |
| 3769 | |
| 3770 | bool has_agg = false; |
| 3771 | for (int i = 0; i < wc->count; i++) { |
| 3772 | if (is_aggregate_func(wc->items[i].func)) { |
| 3773 | has_agg = true; |
| 3774 | break; |
| 3775 | } |
| 3776 | } |
| 3777 | |
| 3778 | if (has_agg) { |
| 3779 | execute_with_aggregate(wc, bindings, bind_count, &vbindings, &vcount); |
| 3780 | } else { |
| 3781 | execute_with_simple(wc, bindings, bind_count, vbindings, &vcount); |
| 3782 | } |
| 3783 | |
| 3784 | /* WITH DISTINCT: dedup projected rows (no-op for aggregation, which already |
| 3785 | * collapses to one row per group). */ |
| 3786 | if (wc->distinct) { |
| 3787 | with_apply_distinct(wc, vbindings, &vcount); |
| 3788 | } |
| 3789 | |
| 3790 | with_sort_skip_limit(wc, vbindings, &vcount); |
| 3791 | |
| 3792 | for (int bi = 0; bi < bind_count; bi++) { |
| 3793 | binding_free(&bindings[bi]); |
| 3794 | } |
| 3795 | free(bindings); |
| 3796 | |
| 3797 | if (q->post_with_where) { |
| 3798 | filter_bindings_where(q->post_with_where, vbindings, &vcount); |
| 3799 | } |
| 3800 | |
| 3801 | *bindings_ptr = vbindings; |
| 3802 | *bind_count_ptr = vcount; |
| 3803 | } |
| 3804 | |
| 3805 | /* ── Execute a single query (no UNION recursion) ──────────────── */ |
| 3806 |
no test coverage detected