1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
use super::*;
impl<'host, H: AggregationHost + ?Sized> AggregationExecutor<'host, H> {
/// Execute ROLLUP/CUBE aggregation
///
/// ROLLUP(a, b, c) generates grouping sets:
/// (a, b, c) - most detailed
/// (a, b) - subtotal for a, b
/// (a) - subtotal for a
/// () - grand total
///
/// CUBE(a, b) generates all combinations:
/// (a, b), (a), (b), ()
#[allow(clippy::too_many_arguments)]
pub(super) fn execute_rollup_aggregation(
&self,
aggregations: &[SqlAggregateFunction],
group_by_items: &[GroupByItem],
rows: &[(i64, Row)],
columns: &[String],
col_index_map: &StringMap<usize>,
stmt: &SelectStatement,
ctx: &ExecutionContext,
) -> Result<(Vec<String>, RowVec)> {
// Generate grouping sets based on modifier type
let grouping_sets = match &stmt.group_by.modifier {
GroupByModifier::Rollup => Self::generate_rollup_sets(group_by_items.len()),
GroupByModifier::Cube => Self::generate_cube_sets(group_by_items.len()),
GroupByModifier::GroupingSets(sets) => {
Self::generate_explicit_grouping_sets(sets, group_by_items)
}
GroupByModifier::None => {
// Shouldn't happen, but handle it
vec![GroupingSet {
active_columns: vec![true; group_by_items.len()],
}]
}
};
// Check if any aggregation has an expression (e.g., SUM(val * 2)) or ORDER BY
let has_agg_expression = aggregations
.iter()
.any(|a| a.expression.is_some() || !a.order_by.is_empty());
// Pre-compute aggregate column indices
let agg_col_indices: Vec<Option<usize>> = aggregations
.iter()
.map(|agg| {
if agg.column == "*" || agg.expression.is_some() {
None
} else {
Self::lookup_column_index(&agg.column_lower, col_index_map)
}
})
.collect();
// Pre-compute GROUP BY column indices
enum PrecomputedGroupBy<'a> {
ColumnIndex(usize),
Position(usize),
Expression(&'a Expression),
NotFound,
}
let precomputed_group_by: Vec<PrecomputedGroupBy> = group_by_items
.iter()
.map(|item| match item {
GroupByItem::Column(col_name) => {
if let Some(idx) =
Self::lookup_column_index(&col_name.to_lowercase(), col_index_map)
{
PrecomputedGroupBy::ColumnIndex(idx)
} else {
PrecomputedGroupBy::NotFound
}
}
GroupByItem::Position(pos) => PrecomputedGroupBy::Position(pos.saturating_sub(1)),
GroupByItem::Expression { expr, .. } => PrecomputedGroupBy::Expression(expr),
})
.collect();
// Pre-compile GROUP BY and aggregate expressions for VM-based evaluation
let has_expr_group_by = precomputed_group_by
.iter()
.any(|item| matches!(item, PrecomputedGroupBy::Expression(_)));
// Pre-compile GROUP BY expressions
// CRITICAL: Propagate errors instead of silently ignoring compilation failures
use crate::expression::{compile_expression, ExecuteContext, ExprVM, SharedProgram};
let compiled_group_by_exprs: Vec<Option<SharedProgram>> = precomputed_group_by
.iter()
.map(|item| match item {
PrecomputedGroupBy::Expression(expr) => compile_expression(expr, columns).map(Some),
_ => Ok(None),
})
.collect::<Result<Vec<_>>>()?;
// Pre-compile aggregate expressions
// CRITICAL: Propagate errors instead of silently ignoring compilation failures
let compiled_agg_expressions: Vec<Option<SharedProgram>> = if has_agg_expression {
aggregations
.iter()
.map(|agg| {
agg.expression
.as_ref()
.map(|e| compile_expression(e, columns))
.transpose()
})
.collect::<Result<Vec<_>>>()?
} else {
vec![None; aggregations.len()]
};
// Pre-compile aggregate-local ORDER BY expressions. Grouping modifiers
// share the same ordered aggregate contract as regular GROUP BY.
let compiled_agg_order_by: Vec<Vec<SharedProgram>> = if has_agg_expression {
aggregations
.iter()
.map(|agg| {
agg.order_by
.iter()
.map(|order| compile_expression(&order.expression, columns))
.collect::<Result<Vec<_>>>()
})
.collect::<Result<Vec<_>>>()?
} else {
vec![Vec::new(); aggregations.len()]
};
// Pre-compile FILTER expressions for aggregate functions
let has_filters = aggregations.iter().any(|a| a.filter.is_some());
let compiled_filters: Vec<Option<SharedProgram>> = if has_filters {
aggregations
.iter()
.map(|agg| {
agg.filter
.as_ref()
.map(|f| compile_expression(f, columns))
.transpose()
})
.collect::<Result<Vec<_>>>()?
} else {
vec![None; aggregations.len()]
};
let mut expr_vm = if has_expr_group_by || has_agg_expression || has_filters {
Some(ExprVM::new())
} else {
None
};
// Collect all results from all grouping sets
let mut all_result_rows: RowVec = RowVec::new();
let mut row_id = 0i64;
for grouping_set in &grouping_sets {
// Count active columns in this grouping set
let active_count = grouping_set.active_columns.iter().filter(|&&x| x).count();
if active_count == 0 {
// Grand total: aggregate all rows without grouping
let mut agg_funcs: Vec<Option<Box<dyn AggregateFunction>>> = aggregations
.iter()
.map(|agg| {
self.host
.aggregation_function_registry()
.get_aggregate(&agg.name)
})
.collect();
// Configure aggregate functions
for (i, agg) in aggregations.iter().enumerate() {
if !agg.extra_args.is_empty() {
if let Some(ref mut func) = agg_funcs[i] {
func.configure(&agg.extra_args);
}
}
if !agg.order_by.is_empty() {
if let Some(ref mut func) = agg_funcs[i] {
func.set_order_by_specs(
agg.order_by
.iter()
.map(|order| {
AggregateOrderBySpec::new(
order.ascending,
order.nulls_first,
)
})
.collect(),
);
}
}
}
let count_star_value = Value::Integer(1);
let mut expr_values: Vec<Value> = vec![Value::null_unknown(); aggregations.len()];
for (_, row) in rows {
// Create execution context for this row
// CRITICAL: Include params for parameterized queries
let exec_ctx = ExecuteContext::new(row)
.with_params(ctx.params())
.with_named_params(ctx.named_params())
.with_transaction_id(ctx.transaction_id())
.with_stored_function_invoker(ctx.stored_function_invoker());
for (i, agg) in aggregations.iter().enumerate() {
if let Some(ref mut func) = agg_funcs[i] {
// Check FILTER clause first - skip row if filter is false
if let Some(ref filter_program) = compiled_filters[i] {
if let Some(ref mut vm) = expr_vm {
match vm.execute_cow(filter_program, &exec_ctx) {
Ok(Value::Boolean(true)) => {}
_ => continue,
}
} else {
continue;
}
}
let value = if let Some(ref expr_program) = compiled_agg_expressions[i]
{
if let Some(ref mut vm) = expr_vm {
expr_values[i] = vm
.execute_cow(expr_program, &exec_ctx)
.map_err(|error| {
radixdb_core::Error::expression_evaluation(format!(
"{}({}): {}",
agg.name, agg.column, error
))
})?;
Some(&expr_values[i])
} else {
None
}
} else {
if let Some(col_idx) = agg_col_indices[i] {
row.get(col_idx)
} else {
Some(&count_star_value)
}
};
if let Some(v) = value {
if !compiled_agg_order_by[i].is_empty() && func.supports_order_by()
{
if let Some(ref mut vm) = expr_vm {
let mut sort_keys =
Vec::with_capacity(compiled_agg_order_by[i].len());
for order_program in &compiled_agg_order_by[i] {
sort_keys.push(
vm.execute_cow(order_program, &exec_ctx).map_err(
|error| {
radixdb_core::Error::expression_evaluation(
format!(
"{} ORDER BY: {}",
agg.name, error
),
)
},
)?,
);
}
func.accumulate_with_sort_key(v, sort_keys, agg.distinct);
} else {
func.accumulate(v, agg.distinct);
}
} else {
func.accumulate(v, agg.distinct);
}
}
}
}
}
// Build result row: all GROUP BY columns are NULL, then aggregates, then grouping flags
// Use CompactVec directly to avoid Vec→CompactVec conversion
let mut row_values: CompactVec<Value> = CompactVec::with_capacity(
group_by_items.len() + aggregations.len() + group_by_items.len(),
);
for _ in group_by_items {
row_values.push(Value::null_unknown());
}
for (i, agg) in aggregations.iter().enumerate() {
let value = if let Some(ref func) = agg_funcs[i] {
func.try_result()?
} else if agg.name == "COUNT" && agg.column == "*" {
Value::Integer(rows.len() as i64)
} else {
Value::null_unknown()
};
row_values.push(value);
}
// Add GROUPING flags: all columns are rolled up in grand total (GROUPING = 1)
for _ in group_by_items {
row_values.push(Value::Integer(1));
}
all_result_rows.push((row_id, Row::from_compact_vec(row_values)));
row_id += 1;
} else {
// Partial grouping: group by active columns only
// Use hash-based grouping with collision handling: u64 hash -> Vec<GroupEntry>
// Each hash bucket can contain multiple groups (handles hash collisions correctly)
// FxHashMap is optimized for trusted keys in embedded database context
let mut groups: FxHashMap<u64, Vec<GroupEntry>> = FxHashMap::default();
let mut key_buffer: Vec<Value> = Vec::with_capacity(active_count);
for (row_idx, (_, row)) in rows.iter().enumerate() {
key_buffer.clear();
// Create execution context for this row
// CRITICAL: Include params for parameterized queries
let exec_ctx = ExecuteContext::new(row)
.with_params(ctx.params())
.with_named_params(ctx.named_params())
.with_transaction_id(ctx.transaction_id())
.with_stored_function_invoker(ctx.stored_function_invoker());
// Only include active columns in the key
for (col_idx, &is_active) in grouping_set.active_columns.iter().enumerate() {
if is_active {
let value = match &precomputed_group_by[col_idx] {
PrecomputedGroupBy::ColumnIndex(idx) => {
row.get(*idx).cloned().unwrap_or_else(Value::null_unknown)
}
PrecomputedGroupBy::Position(idx) => {
row.get(*idx).cloned().unwrap_or_else(Value::null_unknown)
}
PrecomputedGroupBy::Expression(_) => {
// Use pre-compiled expression with VM
if let (Some(ref mut vm), Some(ref program)) =
(&mut expr_vm, &compiled_group_by_exprs[col_idx])
{
vm.execute_cow(program, &exec_ctx).map_err(|e| {
radixdb_core::Error::expression_evaluation(format!(
"GROUP BY: {}",
e
))
})?
} else {
Value::null_unknown()
}
}
PrecomputedGroupBy::NotFound => Value::null_unknown(),
};
key_buffer.push(value);
}
}
// Compute hash and handle bucket with proper collision detection
let hash = hash_group_key(&key_buffer);
match groups.entry(hash) {
std::collections::hash_map::Entry::Occupied(mut e) => {
let bucket = e.get_mut();
// Search bucket for matching group (handles hash collisions)
if let Some(entry) = bucket
.iter_mut()
.find(|entry| entry.key_values == key_buffer)
{
entry.row_indices.push(row_idx);
} else {
// Hash collision: different key with same hash
bucket.push(GroupEntry {
key_values: key_buffer.clone(),
row_indices: vec![row_idx],
});
}
}
std::collections::hash_map::Entry::Vacant(e) => {
// First entry for this hash
e.insert(vec![GroupEntry {
key_values: key_buffer.clone(),
row_indices: vec![row_idx],
}]);
}
}
}
// Process each group
let mut agg_funcs: Vec<Option<Box<dyn AggregateFunction>>> = aggregations
.iter()
.map(|agg| {
self.host
.aggregation_function_registry()
.get_aggregate(&agg.name)
})
.collect();
// Configure aggregate functions
for (i, agg) in aggregations.iter().enumerate() {
if !agg.extra_args.is_empty() {
if let Some(ref mut func) = agg_funcs[i] {
func.configure(&agg.extra_args);
}
}
if !agg.order_by.is_empty() {
if let Some(ref mut func) = agg_funcs[i] {
func.set_order_by_specs(
agg.order_by
.iter()
.map(|order| {
AggregateOrderBySpec::new(
order.ascending,
order.nulls_first,
)
})
.collect(),
);
}
}
}
let mut expr_values: Vec<Value> = vec![Value::null_unknown(); aggregations.len()];
// Note: We don't sort groups here for performance.
// SQL does not guarantee result order without ORDER BY.
// Users who need ordered results should add ORDER BY to their query.
// Flatten buckets: each bucket may contain multiple groups (hash collisions)
for (_hash, bucket) in groups {
for group in bucket {
// Reset aggregate functions
for f in agg_funcs.iter_mut().flatten() {
f.reset();
}
let count_star_value = Value::Integer(1);
for &row_idx in &group.row_indices {
let (_, row) = &rows[row_idx];
// Create execution context for this row
// CRITICAL: Include params for parameterized queries
let exec_ctx = ExecuteContext::new(row)
.with_params(ctx.params())
.with_named_params(ctx.named_params())
.with_transaction_id(ctx.transaction_id())
.with_stored_function_invoker(ctx.stored_function_invoker());
for (i, agg) in aggregations.iter().enumerate() {
if let Some(ref mut func) = agg_funcs[i] {
// Check FILTER clause first - skip row if filter is false
if let Some(ref filter_program) = compiled_filters[i] {
if let Some(ref mut vm) = expr_vm {
match vm.execute_cow(filter_program, &exec_ctx) {
Ok(Value::Boolean(true)) => {}
_ => continue,
}
} else {
continue;
}
}
let value = if let Some(ref expr_program) =
compiled_agg_expressions[i]
{
if let Some(ref mut vm) = expr_vm {
expr_values[i] = vm
.execute_cow(expr_program, &exec_ctx)
.map_err(|error| {
radixdb_core::Error::expression_evaluation(
format!(
"{}({}): {}",
agg.name, agg.column, error
),
)
})?;
Some(&expr_values[i])
} else {
None
}
} else {
if let Some(col_idx) = agg_col_indices[i] {
row.get(col_idx)
} else {
Some(&count_star_value)
}
};
if let Some(v) = value {
if !compiled_agg_order_by[i].is_empty()
&& func.supports_order_by()
{
if let Some(ref mut vm) = expr_vm {
let mut sort_keys = Vec::with_capacity(
compiled_agg_order_by[i].len(),
);
for order_program in &compiled_agg_order_by[i] {
sort_keys.push(
vm.execute_cow(order_program, &exec_ctx)
.map_err(|error| {
radixdb_core::Error::expression_evaluation(
format!(
"{} ORDER BY: {}",
agg.name, error
),
)
})?,
);
}
func.accumulate_with_sort_key(
v,
sort_keys,
agg.distinct,
);
} else {
func.accumulate(v, agg.distinct);
}
} else {
func.accumulate(v, agg.distinct);
}
}
}
}
}
// Build result row
// For GROUP BY columns: use key value if active, NULL if rolled up
// Use CompactVec directly to avoid Vec→CompactVec conversion
let mut row_values: CompactVec<Value> = CompactVec::with_capacity(
group_by_items.len() + aggregations.len() + group_by_items.len(),
);
let mut key_idx = 0;
for &is_active in &grouping_set.active_columns {
if is_active {
row_values.push(group.key_values[key_idx].clone());
key_idx += 1;
} else {
row_values.push(Value::null_unknown());
}
}
for (i, agg) in aggregations.iter().enumerate() {
let value = if let Some(ref func) = agg_funcs[i] {
func.try_result()?
} else if agg.name == "COUNT" && agg.column == "*" {
Value::Integer(group.row_indices.len() as i64)
} else {
Value::null_unknown()
};
row_values.push(value);
}
// Add GROUPING flags: 0 if column is active (grouped), 1 if rolled up
for &is_active in &grouping_set.active_columns {
row_values.push(Value::Integer(if is_active { 0 } else { 1 }));
}
all_result_rows.push((row_id, Row::from_compact_vec(row_values)));
row_id += 1;
} // end for group in bucket
} // end for bucket in groups
}
}
// Build result columns
let mut result_columns: Vec<String> =
self.resolve_group_by_column_names_new(group_by_items, columns, col_index_map);
result_columns.extend(aggregations.iter().map(|a| a.get_column_name()));
// Add hidden grouping flag columns for GROUPING() function support
// These will be used by the projection phase and stripped from final output
for i in 0..group_by_items.len() {
result_columns.push(format!("__grouping_{}__", i));
}
Ok((result_columns, all_result_rows))
}
/// Generate grouping sets for ROLLUP
/// ROLLUP(a, b, c) generates: (a,b,c), (a,b), (a), ()
pub(super) fn generate_rollup_sets(num_columns: usize) -> Vec<GroupingSet> {
let mut sets = Vec::with_capacity(num_columns + 1);
// From most specific to least specific (grand total)
for active_count in (0..=num_columns).rev() {
let mut active_columns = vec![false; num_columns];
for item in active_columns.iter_mut().take(active_count) {
*item = true;
}
sets.push(GroupingSet { active_columns });
}
sets
}
/// Generate grouping sets for CUBE
/// CUBE(a, b) generates: (a,b), (a), (b), ()
pub(super) fn generate_cube_sets(num_columns: usize) -> Vec<GroupingSet> {
let mut sets = Vec::with_capacity(1 << num_columns);
// Generate all 2^n combinations
for mask in (0..(1 << num_columns)).rev() {
let mut active_columns = vec![false; num_columns];
for (i, item) in active_columns.iter_mut().enumerate() {
if mask & (1 << (num_columns - 1 - i)) != 0 {
*item = true;
}
}
sets.push(GroupingSet { active_columns });
}
sets
}
/// Generate grouping sets from explicit GROUPING SETS clause
/// GROUPING SETS ((a, b), (a), ()) generates exactly those three sets
pub(super) fn generate_explicit_grouping_sets(
sets: &[Vec<Expression>],
group_by_items: &[GroupByItem],
) -> Vec<GroupingSet> {
let num_columns = group_by_items.len();
let mut result = Vec::with_capacity(sets.len());
// Build a lookup from canonical key to group_by_items index
// Uses the same canonical key function for consistent matching
let item_to_index: StringMap<usize> = group_by_items
.iter()
.enumerate()
.map(|(i, item)| (group_by_item_canonical_key(item), i))
.collect();
for set in sets {
let mut active_columns = vec![false; num_columns];
for expr in set {
// Get the canonical key for this expression
let key = expression_canonical_key(expr);
// Find the index in group_by_items
if let Some(&idx) = item_to_index.get(&key) {
active_columns[idx] = true;
}
}
result.push(GroupingSet { active_columns });
}
result
}
/// Resolve GROUP BY column names for result (new version supporting GroupByItem)
pub(super) fn resolve_group_by_column_names_new(
&self,
group_by_items: &[GroupByItem],
columns: &[String],
col_index_map: &StringMap<usize>,
) -> Vec<String> {
let mut names = Vec::new();
for item in group_by_items {
match item {
GroupByItem::Column(col_name) => {
// Use lookup_column_index to handle qualified names (e.g., "t.dept" -> "dept")
if let Some(idx) =
Self::lookup_column_index(&col_name.to_lowercase(), col_index_map)
{
if idx < columns.len() {
names.push(columns[idx].clone());
} else {
names.push(col_name.clone());
}
} else {
names.push(col_name.clone());
}
}
GroupByItem::Position(pos) => {
// Position is 1-indexed
let idx = pos.saturating_sub(1);
if idx < columns.len() {
names.push(columns[idx].clone());
} else {
names.push(format!("${}", pos));
}
}
GroupByItem::Expression { display_name, .. } => {
// Use the display name (alias or generated name)
names.push(display_name.clone());
}
}
}
names
}
/// Look up column index, handling both qualified (e.g., "o.amount") and unqualified names.
/// If a qualified name lookup fails, tries the unqualified part (after the dot).
pub(super) fn lookup_column_index(
column_lower: &str,
col_index_map: &StringMap<usize>,
) -> Option<usize> {
// First try exact match
if let Some(&idx) = col_index_map.get(column_lower) {
return Some(idx);
}
// If it's a qualified name (contains a dot), try the unqualified part
if let Some(dot_pos) = column_lower.rfind('.') {
let unqualified = &column_lower[dot_pos + 1..];
if let Some(&idx) = col_index_map.get(unqualified) {
return Some(idx);
}
}
None
}
/// Apply HAVING clause to aggregated results
pub(super) fn apply_having(
&self,
result: Box<dyn QueryResult>,
having: &Expression,
columns: &[String],
agg_aliases: &[(String, usize)],
expr_aliases: &[(String, usize)],
ctx: &ExecutionContext,
) -> Result<Box<dyn QueryResult>> {
// Materialize the result
let mut rows = RowVec::new();
let mut result = result;
let mut row_id = 0i64;
while result.next() {
rows.push((row_id, result.take_row()));
row_id += 1;
}
// Combine all aliases for HAVING clause evaluation
let mut all_aliases: Vec<(String, usize)> = agg_aliases.to_vec();
all_aliases.extend_from_slice(expr_aliases);
// Create RowFilter with all aliases and context
let having_filter =
RowFilter::with_aliases_and_context(having, columns, &all_aliases, ctx)?;
// Filter rows using the pre-compiled filter
let mut filtered_rows = RowVec::new();
let mut new_id = 0i64;
for (_, row) in rows {
if having_filter.matches_checked(&row)? {
filtered_rows.push((new_id, row));
new_id += 1;
}
}
Ok(Box::new(ExecutorResult::new(
columns.to_vec(),
filtered_rows,
)))
}
}