oxigdal-query 0.1.7

SQL-like query language and cost-based optimizer for geospatial data
Documentation
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
//! Aggregation executor.

use crate::error::{QueryError, Result};
use crate::executor::filter::{Value, evaluate_expr_for_row};
use crate::executor::scan::{ColumnData, DataType, Field, RecordBatch, Schema};
use crate::parser::ast::Expr;
use std::collections::HashMap;
use std::sync::Arc;

/// Aggregate operator.
pub struct Aggregate {
    /// GROUP BY expressions.
    pub group_by: Vec<Expr>,
    /// Aggregate functions.
    pub aggregates: Vec<AggregateFunction>,
}

impl Aggregate {
    /// Create a new aggregate operator.
    pub fn new(group_by: Vec<Expr>, aggregates: Vec<AggregateFunction>) -> Self {
        Self {
            group_by,
            aggregates,
        }
    }

    /// Execute aggregation.
    pub fn execute(&self, batch: &RecordBatch) -> Result<RecordBatch> {
        if self.group_by.is_empty() {
            // Global aggregation
            self.execute_global_aggregate(batch)
        } else {
            // Grouped aggregation
            self.execute_grouped_aggregate(batch)
        }
    }

    /// Execute global aggregation (no GROUP BY).
    fn execute_global_aggregate(&self, batch: &RecordBatch) -> Result<RecordBatch> {
        let mut result_fields = Vec::new();
        let mut result_columns = Vec::new();

        for agg in &self.aggregates {
            let value = if agg.column == "*" {
                // COUNT(*) - count all rows regardless of NULL values
                if matches!(agg.func, AggregateFunc::Count) {
                    Some(batch.num_rows as f64)
                } else {
                    return Err(QueryError::semantic(
                        "Wildcard (*) can only be used with COUNT function",
                    ));
                }
            } else {
                let column = batch
                    .column_by_name(&agg.column)
                    .ok_or_else(|| QueryError::ColumnNotFound(agg.column.clone()))?;
                self.compute_aggregate(agg.func, column)?
            };

            result_fields.push(Field::new(
                agg.alias.clone().unwrap_or_else(|| {
                    if agg.column == "*" {
                        "count".to_string()
                    } else {
                        agg.column.clone()
                    }
                }),
                crate::executor::scan::DataType::Float64,
                true,
            ));
            result_columns.push(ColumnData::Float64(vec![value]));
        }

        let schema = Arc::new(Schema::new(result_fields));
        RecordBatch::new(schema, result_columns, 1)
    }

    /// Execute grouped aggregation (`GROUP BY`).
    ///
    /// Rows are partitioned into groups by evaluating every `GROUP BY`
    /// expression per row and hashing the resulting scalar values (NULLs group
    /// together, matching SQL semantics; composite keys are supported). For
    /// each group the aggregate functions are computed over the member rows,
    /// producing one output row per group. Output columns are the `GROUP BY`
    /// keys (preserving their original types) followed by the aggregate results
    /// (as `Float64`, matching the global-aggregation path).
    fn execute_grouped_aggregate(&self, batch: &RecordBatch) -> Result<RecordBatch> {
        let num_rows = batch.num_rows;

        // Evaluate each GROUP BY expression for every row.
        let mut group_values: Vec<Vec<Value>> = Vec::with_capacity(self.group_by.len());
        for expr in &self.group_by {
            let mut vals = Vec::with_capacity(num_rows);
            for row in 0..num_rows {
                let value = evaluate_expr_for_row(expr, batch, row)?;
                if matches!(value, Value::Geometry(_)) {
                    return Err(QueryError::unsupported(
                        "GROUP BY on a geometry value is not supported",
                    ));
                }
                vals.push(value);
            }
            group_values.push(vals);
        }

        // Partition rows into groups, preserving first-appearance order.
        let mut key_to_group: HashMap<Vec<String>, usize> = HashMap::new();
        let mut group_rows: Vec<Vec<usize>> = Vec::new();
        let mut group_repr: Vec<Vec<Value>> = Vec::new();

        for row in 0..num_rows {
            let key: Vec<String> = group_values
                .iter()
                .map(|gv| group_key_component(&gv[row]))
                .collect();
            let gid = match key_to_group.get(&key) {
                Some(&g) => g,
                None => {
                    let g = group_rows.len();
                    key_to_group.insert(key, g);
                    group_rows.push(Vec::new());
                    group_repr.push(group_values.iter().map(|gv| gv[row].clone()).collect());
                    g
                }
            };
            group_rows[gid].push(row);
        }

        let num_groups = group_rows.len();

        let mut result_fields = Vec::new();
        let mut result_columns = Vec::new();

        // GROUP BY key columns.
        for (gi, expr) in self.group_by.iter().enumerate() {
            let repr_vals: Vec<Value> =
                (0..num_groups).map(|g| group_repr[g][gi].clone()).collect();
            let column = values_to_column(&repr_vals);
            let data_type = column_data_type(&column);
            let name = match expr {
                Expr::Column { name, .. } => name.clone(),
                _ => format!("group_{}", gi),
            };
            result_fields.push(Field::new(name, data_type, true));
            result_columns.push(column);
        }

        // Aggregate columns.
        for agg in &self.aggregates {
            let mut agg_vals: Vec<Option<f64>> = Vec::with_capacity(num_groups);
            for rows in group_rows.iter() {
                let value = if agg.column == "*" {
                    if matches!(agg.func, AggregateFunc::Count) {
                        Some(rows.len() as f64)
                    } else {
                        return Err(QueryError::semantic(
                            "Wildcard (*) can only be used with COUNT function",
                        ));
                    }
                } else {
                    let column = batch
                        .column_by_name(&agg.column)
                        .ok_or_else(|| QueryError::ColumnNotFound(agg.column.clone()))?;
                    let sub = gather_column(column, rows);
                    self.compute_aggregate(agg.func, &sub)?
                };
                agg_vals.push(value);
            }
            result_fields.push(Field::new(
                agg.alias.clone().unwrap_or_else(|| {
                    if agg.column == "*" {
                        "count".to_string()
                    } else {
                        agg.column.clone()
                    }
                }),
                DataType::Float64,
                true,
            ));
            result_columns.push(ColumnData::Float64(agg_vals));
        }

        let schema = Arc::new(Schema::new(result_fields));
        RecordBatch::new(schema, result_columns, num_groups)
    }

    /// Compute aggregate function.
    fn compute_aggregate(&self, func: AggregateFunc, column: &ColumnData) -> Result<Option<f64>> {
        match func {
            AggregateFunc::Count => Ok(Some(self.count(column))),
            AggregateFunc::Sum => self.sum(column),
            AggregateFunc::Avg => self.avg(column),
            AggregateFunc::Min => self.min(column),
            AggregateFunc::Max => self.max(column),
        }
    }

    /// Count aggregate.
    fn count(&self, column: &ColumnData) -> f64 {
        let non_null_count = match column {
            ColumnData::Boolean(data) => data.iter().filter(|v| v.is_some()).count(),
            ColumnData::Int32(data) => data.iter().filter(|v| v.is_some()).count(),
            ColumnData::Int64(data) => data.iter().filter(|v| v.is_some()).count(),
            ColumnData::Float32(data) => data.iter().filter(|v| v.is_some()).count(),
            ColumnData::Float64(data) => data.iter().filter(|v| v.is_some()).count(),
            ColumnData::String(data) => data.iter().filter(|v| v.is_some()).count(),
            ColumnData::Binary(data) => data.iter().filter(|v| v.is_some()).count(),
        };
        non_null_count as f64
    }

    /// Sum aggregate.
    fn sum(&self, column: &ColumnData) -> Result<Option<f64>> {
        match column {
            ColumnData::Int32(data) => {
                let sum: i64 = data.iter().filter_map(|v| v.map(|i| i as i64)).sum();
                Ok(Some(sum as f64))
            }
            ColumnData::Int64(data) => {
                let sum: i64 = data.iter().filter_map(|v| *v).sum();
                Ok(Some(sum as f64))
            }
            ColumnData::Float32(data) => {
                let sum: f32 = data.iter().filter_map(|v| *v).sum();
                Ok(Some(sum as f64))
            }
            ColumnData::Float64(data) => {
                let sum: f64 = data.iter().filter_map(|v| *v).sum();
                Ok(Some(sum))
            }
            _ => Err(QueryError::type_mismatch("numeric", "non-numeric")),
        }
    }

    /// Average aggregate.
    fn avg(&self, column: &ColumnData) -> Result<Option<f64>> {
        let sum = self.sum(column)?;
        let count = self.count(column);
        if count > 0.0 {
            Ok(sum.map(|s| s / count))
        } else {
            Ok(None)
        }
    }

    /// Minimum aggregate.
    fn min(&self, column: &ColumnData) -> Result<Option<f64>> {
        match column {
            ColumnData::Int32(data) => {
                let min = data.iter().filter_map(|v| *v).min();
                Ok(min.map(|m| m as f64))
            }
            ColumnData::Int64(data) => {
                let min = data.iter().filter_map(|v| *v).min();
                Ok(min.map(|m| m as f64))
            }
            ColumnData::Float32(data) => {
                let min = data
                    .iter()
                    .filter_map(|v| *v)
                    .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                Ok(min.map(|m| m as f64))
            }
            ColumnData::Float64(data) => {
                let min = data
                    .iter()
                    .filter_map(|v| *v)
                    .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                Ok(min)
            }
            _ => Err(QueryError::type_mismatch("numeric", "non-numeric")),
        }
    }

    /// Maximum aggregate.
    fn max(&self, column: &ColumnData) -> Result<Option<f64>> {
        match column {
            ColumnData::Int32(data) => {
                let max = data.iter().filter_map(|v| *v).max();
                Ok(max.map(|m| m as f64))
            }
            ColumnData::Int64(data) => {
                let max = data.iter().filter_map(|v| *v).max();
                Ok(max.map(|m| m as f64))
            }
            ColumnData::Float32(data) => {
                let max = data
                    .iter()
                    .filter_map(|v| *v)
                    .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                Ok(max.map(|m| m as f64))
            }
            ColumnData::Float64(data) => {
                let max = data
                    .iter()
                    .filter_map(|v| *v)
                    .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
                Ok(max)
            }
            _ => Err(QueryError::type_mismatch("numeric", "non-numeric")),
        }
    }
}

/// Build a hashable/equatable key component for a group-by value.
///
/// NULLs map to a single sentinel so they group together. Floats use their bit
/// representation so that `NaN` values group deterministically.
fn group_key_component(value: &Value) -> String {
    match value {
        Value::Null => "N".to_string(),
        Value::Boolean(b) => format!("b{}", b),
        Value::Int32(i) => format!("i{}", i),
        Value::Int64(i) => format!("l{}", i),
        Value::Float32(f) => format!("f{}", f.to_bits()),
        Value::Float64(f) => format!("d{}", f.to_bits()),
        Value::String(s) => format!("s{}", s),
        // Geometry is rejected before this point.
        Value::Geometry(_) => "g".to_string(),
    }
}

/// Convert per-group representative [`Value`]s into a typed [`ColumnData`].
///
/// The column type is inferred from the first non-NULL value; an all-NULL group
/// key defaults to an `Int64` column of NULLs.
fn values_to_column(values: &[Value]) -> ColumnData {
    let first_non_null = values.iter().find(|v| !matches!(v, Value::Null));
    match first_non_null {
        Some(Value::Boolean(_)) => ColumnData::Boolean(
            values
                .iter()
                .map(|v| match v {
                    Value::Boolean(b) => Some(*b),
                    _ => None,
                })
                .collect(),
        ),
        Some(Value::Int32(_)) => ColumnData::Int32(
            values
                .iter()
                .map(|v| match v {
                    Value::Int32(i) => Some(*i),
                    _ => None,
                })
                .collect(),
        ),
        Some(Value::Int64(_)) => ColumnData::Int64(
            values
                .iter()
                .map(|v| match v {
                    Value::Int64(i) => Some(*i),
                    _ => None,
                })
                .collect(),
        ),
        Some(Value::Float32(_)) => ColumnData::Float32(
            values
                .iter()
                .map(|v| match v {
                    Value::Float32(f) => Some(*f),
                    _ => None,
                })
                .collect(),
        ),
        Some(Value::Float64(_)) => ColumnData::Float64(
            values
                .iter()
                .map(|v| match v {
                    Value::Float64(f) => Some(*f),
                    _ => None,
                })
                .collect(),
        ),
        Some(Value::String(_)) => ColumnData::String(
            values
                .iter()
                .map(|v| match v {
                    Value::String(s) => Some(s.clone()),
                    _ => None,
                })
                .collect(),
        ),
        // All NULL (or geometry, which is rejected earlier): default to Int64 NULLs.
        _ => ColumnData::Int64(values.iter().map(|_| None).collect()),
    }
}

/// Map a [`ColumnData`] variant to its [`DataType`].
fn column_data_type(column: &ColumnData) -> DataType {
    match column {
        ColumnData::Boolean(_) => DataType::Boolean,
        ColumnData::Int32(_) => DataType::Int32,
        ColumnData::Int64(_) => DataType::Int64,
        ColumnData::Float32(_) => DataType::Float32,
        ColumnData::Float64(_) => DataType::Float64,
        ColumnData::String(_) => DataType::String,
        ColumnData::Binary(_) => DataType::Binary,
    }
}

/// Gather a subset of a column's rows (by index) into a new column.
fn gather_column(column: &ColumnData, indices: &[usize]) -> ColumnData {
    match column {
        ColumnData::Boolean(d) => {
            ColumnData::Boolean(indices.iter().filter_map(|&i| d.get(i).copied()).collect())
        }
        ColumnData::Int32(d) => {
            ColumnData::Int32(indices.iter().filter_map(|&i| d.get(i).copied()).collect())
        }
        ColumnData::Int64(d) => {
            ColumnData::Int64(indices.iter().filter_map(|&i| d.get(i).copied()).collect())
        }
        ColumnData::Float32(d) => {
            ColumnData::Float32(indices.iter().filter_map(|&i| d.get(i).copied()).collect())
        }
        ColumnData::Float64(d) => {
            ColumnData::Float64(indices.iter().filter_map(|&i| d.get(i).copied()).collect())
        }
        ColumnData::String(d) => {
            ColumnData::String(indices.iter().filter_map(|&i| d.get(i).cloned()).collect())
        }
        ColumnData::Binary(d) => {
            ColumnData::Binary(indices.iter().filter_map(|&i| d.get(i).cloned()).collect())
        }
    }
}

/// Aggregate function.
#[derive(Debug, Clone)]
pub struct AggregateFunction {
    /// Function type.
    pub func: AggregateFunc,
    /// Column to aggregate.
    pub column: String,
    /// Output alias.
    pub alias: Option<String>,
}

/// Aggregate function type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateFunc {
    /// COUNT function.
    Count,
    /// SUM function.
    Sum,
    /// AVG function.
    Avg,
    /// MIN function.
    Min,
    /// MAX function.
    Max,
}

#[cfg(test)]
#[allow(clippy::panic)]
mod tests {
    use super::*;
    use crate::executor::scan::DataType;

    #[test]
    fn test_global_aggregate() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![Field::new(
            "value".to_string(),
            DataType::Int64,
            false,
        )]));

        let columns = vec![ColumnData::Int64(vec![
            Some(10),
            Some(20),
            Some(30),
            Some(40),
            Some(50),
        ])];

        let batch = RecordBatch::new(schema, columns, 5)?;

        let agg = Aggregate::new(
            vec![],
            vec![
                AggregateFunction {
                    func: AggregateFunc::Sum,
                    column: "value".to_string(),
                    alias: Some("sum".to_string()),
                },
                AggregateFunction {
                    func: AggregateFunc::Avg,
                    column: "value".to_string(),
                    alias: Some("avg".to_string()),
                },
            ],
        );

        let result = agg.execute(&batch)?;
        assert_eq!(result.num_rows, 1);
        assert_eq!(result.columns.len(), 2);

        Ok(())
    }

    #[test]
    fn test_grouped_aggregate() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![
            Field::new("k".to_string(), DataType::Int64, false),
            Field::new("v".to_string(), DataType::Int64, false),
        ]));
        // groups: k=1 -> [10, 30] (count 2, sum 40); k=2 -> [20] (count 1, sum 20)
        let columns = vec![
            ColumnData::Int64(vec![Some(1), Some(2), Some(1)]),
            ColumnData::Int64(vec![Some(10), Some(20), Some(30)]),
        ];
        let batch = RecordBatch::new(schema, columns, 3)?;

        let agg = Aggregate::new(
            vec![Expr::Column {
                table: None,
                name: "k".to_string(),
            }],
            vec![
                AggregateFunction {
                    func: AggregateFunc::Sum,
                    column: "v".to_string(),
                    alias: Some("sum_v".to_string()),
                },
                AggregateFunction {
                    func: AggregateFunc::Count,
                    column: "*".to_string(),
                    alias: Some("cnt".to_string()),
                },
            ],
        );

        let result = agg.execute(&batch)?;
        assert_eq!(result.num_rows, 2); // two groups
        assert_eq!(result.columns.len(), 3); // k, sum_v, cnt

        // Group key column preserves the Int64 type and first-appearance order.
        let ColumnData::Int64(k) = &result.columns[0] else {
            panic!("expected int64 group key");
        };
        assert_eq!(k[0], Some(1));
        assert_eq!(k[1], Some(2));

        let ColumnData::Float64(sum_v) = &result.columns[1] else {
            panic!("expected float64 sum");
        };
        assert_eq!(sum_v[0], Some(40.0));
        assert_eq!(sum_v[1], Some(20.0));

        let ColumnData::Float64(cnt) = &result.columns[2] else {
            panic!("expected float64 count");
        };
        assert_eq!(cnt[0], Some(2.0));
        assert_eq!(cnt[1], Some(1.0));

        Ok(())
    }

    #[test]
    fn test_grouped_aggregate_null_key_groups_together() -> Result<()> {
        let schema = Arc::new(Schema::new(vec![
            Field::new("k".to_string(), DataType::Int64, true),
            Field::new("v".to_string(), DataType::Int64, false),
        ]));
        let columns = vec![
            ColumnData::Int64(vec![None, Some(1), None]),
            ColumnData::Int64(vec![Some(5), Some(7), Some(9)]),
        ];
        let batch = RecordBatch::new(schema, columns, 3)?;
        let agg = Aggregate::new(
            vec![Expr::Column {
                table: None,
                name: "k".to_string(),
            }],
            vec![AggregateFunction {
                func: AggregateFunc::Count,
                column: "*".to_string(),
                alias: Some("cnt".to_string()),
            }],
        );
        let result = agg.execute(&batch)?;
        // NULL group (2 rows) + k=1 group (1 row) => 2 groups.
        assert_eq!(result.num_rows, 2);
        let ColumnData::Float64(cnt) = &result.columns[1] else {
            panic!("expected float64 count");
        };
        assert_eq!(cnt[0], Some(2.0)); // NULL group first (rows 0 and 2)
        assert_eq!(cnt[1], Some(1.0));
        Ok(())
    }
}