nodedb-lite 0.1.0

Embedded edge client for NodeDB — offline vector search, graph traversal, and CRDT sync
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
593
594
595
596
// SPDX-License-Identifier: Apache-2.0
//! Aggregate and PartialAggregate QueryOp implementations for Lite.
//!
//! Supports GROUP BY, aggregate functions (COUNT, SUM, AVG, MIN, MAX,
//! TS_STDDEV, and the DISTINCT forms of COUNT, SUM and AVG), HAVING,
//! ORDER BY, and GROUPING SETS (ROLLUP/CUBE) expansion. Any other function
//! is refused.

use std::collections::HashMap;

use nodedb_physical::physical_plan::query::AggregateSpec;
use nodedb_physical::physical_plan::{PARAMETERIZED_AGGREGATE_FUNCTIONS, SortKeySpec};
use nodedb_query::EvalError;
use nodedb_query::expr::GroupKeySpec;
use nodedb_query::scan_filter::ScanFilter;
use nodedb_types::result::QueryResult;
use nodedb_types::value::Value;

use crate::error::LiteError;

use super::aggregate_numeric::Numeric;

// ─── Type aliases ─────────────────────────────────────────────────────────────

type GroupMap = HashMap<String, (Vec<Value>, Vec<HashMap<String, Value>>)>;

// ─── Public entry points ─────────────────────────────────────────────────────

/// Execute a full Aggregate: apply filters, group, aggregate, HAVING, sort.
pub fn execute_aggregate(
    rows: Vec<HashMap<String, Value>>,
    group_by: &[GroupKeySpec],
    aggregates: &[AggregateSpec],
    filters: &[u8],
    having: &[u8],
    sort_keys: &[SortKeySpec],
    grouping_sets: &[Vec<u32>],
) -> Result<QueryResult, LiteError> {
    let scan_filters = decode_filters(filters)?;
    let having_filters = decode_filters(having)?;

    let filtered = retain_matching(rows, &scan_filters)?;

    if grouping_sets.is_empty() {
        // Plain GROUP BY — single grouping set containing all group_by columns.
        let grouped = group_rows(&filtered, group_by)?;
        let mut result_rows = compute_aggregate_groups(grouped, group_by, aggregates)?;
        apply_having(&mut result_rows, &having_filters, group_by, aggregates)?;
        apply_sort(&mut result_rows, sort_keys, group_by, aggregates);
        let columns = make_columns(group_by, aggregates);
        Ok(QueryResult {
            columns,
            rows: result_rows,
            rows_affected: 0,
            command: None,
        })
    } else {
        // GROUPING SETS: union results for each subset.
        let mut all_rows: Vec<Vec<Value>> = Vec::new();
        for set_indices in grouping_sets {
            let subset: Vec<GroupKeySpec> = set_indices
                .iter()
                .filter_map(|&i| group_by.get(i as usize).cloned())
                .collect();
            let grouped = group_rows(&filtered, &subset)?;
            let mut set_rows = compute_aggregate_groups(grouped, &subset, aggregates)?;
            // Null-fill absent group columns.
            let full_col_count = group_by.len();
            for row in &mut set_rows {
                while row.len() < full_col_count + aggregates.len() {
                    row.insert(set_indices.len(), Value::Null);
                }
            }
            apply_having(&mut set_rows, &having_filters, &subset, aggregates)?;
            all_rows.extend(set_rows);
        }
        apply_sort(&mut all_rows, sort_keys, group_by, aggregates);
        let columns = make_columns(group_by, aggregates);
        Ok(QueryResult {
            columns,
            rows: all_rows,
            rows_affected: 0,
            command: None,
        })
    }
}

/// Execute a PartialAggregate.
///
/// On single-node Lite, data is already local so this produces the same
/// final result as Aggregate (no HAVING/sort/grouping_sets) and encodes it
/// in the partial-state format Origin expects: each output row is
/// `[group_key_col_0, ..., group_key_col_N, count_i64, sum_f64, min, max, ...]`
/// in the same column order as `make_columns`, with a leading `__partial`
/// boolean column set to `true` so the Control-Plane merger can distinguish
/// partial from final results.
pub fn execute_partial_aggregate(
    rows: Vec<HashMap<String, Value>>,
    group_by: &[String],
    aggregates: &[AggregateSpec],
    filters: &[u8],
) -> Result<QueryResult, LiteError> {
    // `QueryOp::PartialAggregate` groups by bare column names only, so the
    // column → spec lift is lossless here.
    let group_by: Vec<GroupKeySpec> = group_by.iter().map(GroupKeySpec::column).collect();
    let group_by = group_by.as_slice();
    let scan_filters = decode_filters(filters)?;
    let filtered = retain_matching(rows, &scan_filters)?;

    let grouped = group_rows(&filtered, group_by)?;
    let result_rows = compute_aggregate_groups(grouped, group_by, aggregates)?;

    let mut columns = vec!["__partial".to_string()];
    columns.extend(make_columns(group_by, aggregates));

    let output = result_rows
        .into_iter()
        .map(|mut row| {
            row.insert(0, Value::Bool(true));
            row
        })
        .collect();

    Ok(QueryResult {
        columns,
        rows: output,
        rows_affected: 0,
        command: None,
    })
}

// ─── Internal helpers ─────────────────────────────────────────────────────────

fn decode_filters(bytes: &[u8]) -> Result<Vec<ScanFilter>, LiteError> {
    if bytes.is_empty() {
        return Ok(Vec::new());
    }
    zerompk::from_msgpack(bytes).map_err(|e| LiteError::Serialization {
        detail: format!("decode filters: {e}"),
    })
}

/// Group rows by the specified key columns.
///
/// `Value` does not implement `Hash`/`Eq`, so we use a string serialisation of
/// the key for the `HashMap` discriminant and carry the original `Vec<Value>`
/// alongside for output.
pub(crate) fn group_rows(
    rows: &[HashMap<String, Value>],
    group_by: &[GroupKeySpec],
) -> Result<GroupMap, LiteError> {
    let mut groups: GroupMap = HashMap::new();
    for row in rows {
        let key: Vec<Value> = group_by
            .iter()
            .map(|spec| group_key_value(row, spec))
            .collect::<Result<Vec<_>, LiteError>>()?;
        let key_str = value_key_str(&key);
        groups
            .entry(key_str)
            .or_insert_with(|| (key, Vec::new()))
            .1
            .push(row.clone());
    }
    Ok(groups)
}

/// Keep only the rows matching every filter. Evaluation is fallible (a
/// divide-by-zero predicate fails the statement rather than dropping the row),
/// so this replaces the `filter(..).collect()` form it grew out of.
fn retain_matching(
    rows: Vec<HashMap<String, Value>>,
    filters: &[ScanFilter],
) -> Result<Vec<HashMap<String, Value>>, LiteError> {
    if filters.is_empty() {
        return Ok(rows);
    }
    let mut kept = Vec::with_capacity(rows.len());
    for row in rows {
        let doc = Value::Object(row.clone());
        if ScanFilter::all_match_value(filters, &doc)? {
            kept.push(row);
        }
    }
    Ok(kept)
}

/// Resolve one group key for a row.
///
/// A spec carrying an expression (e.g. `GROUP BY date_trunc('day', ts)`) is
/// evaluated against the row; otherwise the named field is extracted directly.
/// A spec with neither groups everything under `Null`.
fn group_key_value(row: &HashMap<String, Value>, spec: &GroupKeySpec) -> Result<Value, LiteError> {
    match (&spec.expr, &spec.field) {
        (Some(expr), _) => Ok(expr.eval(&Value::Object(row.clone()))?),
        (None, Some(field)) => Ok(row.get(field).cloned().unwrap_or(Value::Null)),
        (None, None) => Ok(Value::Null),
    }
}

/// Stable string discriminant for a `Vec<Value>` group key.
fn value_key_str(key: &[Value]) -> String {
    key.iter()
        .map(value_discriminant)
        .collect::<Vec<_>>()
        .join("|")
}

pub(crate) fn value_discriminant(v: &Value) -> String {
    match v {
        Value::Null => "null".to_string(),
        Value::Bool(b) => format!("b:{b}"),
        Value::Integer(n) => format!("i:{n}"),
        Value::Float(f) => format!("f:{f}"),
        Value::String(s) => format!("s:{s}"),
        Value::Uuid(s) | Value::Ulid(s) | Value::Regex(s) => format!("str:{s}"),
        Value::Bytes(b) => format!("by:{}", b.len()),
        Value::Array(a) | Value::Set(a) => format!("a:{}", a.len()),
        Value::Object(m) => format!("o:{}", m.len()),
        _ => "other".to_string(),
    }
}

fn compute_aggregate_groups(
    groups: GroupMap,
    _group_by: &[GroupKeySpec],
    aggregates: &[AggregateSpec],
) -> Result<Vec<Vec<Value>>, LiteError> {
    let mut result = Vec::with_capacity(groups.len());
    for (_key_str, (key_vals, group_rows)) in groups {
        let mut row: Vec<Value> = key_vals;
        for spec in aggregates {
            let agg_val = compute_one_aggregate(&group_rows, spec)?;
            row.push(agg_val);
        }
        result.push(row);
    }
    Ok(result)
}

fn compute_one_aggregate(
    group: &[HashMap<String, Value>],
    spec: &AggregateSpec,
) -> Result<Value, LiteError> {
    let func = spec.function.to_uppercase();
    // Lite has no accumulator for a parameterized aggregate (`approx_topk`,
    // `approx_percentile`, `percentile_cont`), so its parameter has nothing
    // to feed.
    if spec.param.is_some()
        || PARAMETERIZED_AGGREGATE_FUNCTIONS.contains(&spec.function.to_lowercase().as_str())
    {
        return Err(unsupported_function(&spec.function));
    }
    if func == "COUNT" && spec.field == "*" && spec.expr.is_none() {
        return Ok(Value::Integer(group.len() as i64));
    }
    // One input per row: the evaluated argument expression, or the field.
    // A NULL input is skipped by every function below.
    let inputs: Vec<Value> = group
        .iter()
        .map(|row| match &spec.expr {
            Some(expr) => Ok(expr.eval(&Value::Object(row.clone()))?),
            None => Ok(row.get(&spec.field).cloned().unwrap_or(Value::Null)),
        })
        .collect::<Result<_, LiteError>>()?;
    let non_null = || inputs.iter().filter(|v| !matches!(v, Value::Null));
    let distinct = || {
        let mut seen: Vec<&Value> = Vec::new();
        for v in non_null() {
            if !seen.contains(&v) {
                seen.push(v);
            }
        }
        seen
    };
    match func.as_str() {
        "COUNT" => Ok(Value::Integer(non_null().count() as i64)),
        "COUNT_DISTINCT" => Ok(Value::Integer(distinct().len() as i64)),
        "SUM" => Numeric::collect(non_null())
            .sum()
            .map_err(|e| total_error(spec, e)),
        "SUM_DISTINCT" => Numeric::collect(distinct().into_iter())
            .sum()
            .map_err(|e| total_error(spec, e)),
        "AVG" => Numeric::collect(non_null())
            .avg()
            .map_err(|e| total_error(spec, e)),
        "AVG_DISTINCT" => Numeric::collect(distinct().into_iter())
            .avg()
            .map_err(|e| total_error(spec, e)),
        "TS_STDDEV" => {
            // Population standard deviation; NULL below two inputs.
            let mut accum = nodedb_query::ts_functions::TsStddevAccum::new();
            for v in Numeric::collect(non_null()).as_f64() {
                accum.update(v);
            }
            Ok(accum
                .evaluate_population()
                .map_or(Value::Null, Value::Float))
        }
        "MIN" => {
            let numeric = Numeric::collect(non_null());
            if !numeric.is_empty() {
                Ok(numeric.min())
            } else {
                // String MIN
                let s = non_null()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .min();
                Ok(s.map(Value::String).unwrap_or(Value::Null))
            }
        }
        "MAX" => {
            let numeric = Numeric::collect(non_null());
            if !numeric.is_empty() {
                Ok(numeric.max())
            } else {
                let s = non_null()
                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                    .max();
                Ok(s.map(Value::String).unwrap_or(Value::Null))
            }
        }
        _ => Err(unsupported_function(&spec.function)),
    }
}

/// The error of a SUM or AVG total. An exact total past the `Decimal` range
/// is `NumericValueOutOfRange`, SQLSTATE `22003` as on Origin, and names the
/// aggregate and its column.
fn total_error(spec: &AggregateSpec, e: EvalError) -> LiteError {
    match e {
        EvalError::NumericOverflow { .. } => LiteError::NumericValueOutOfRange {
            detail: format!(
                "{}({}): integer total out of range",
                spec.function.to_lowercase(),
                spec.field
            ),
        },
        other @ (EvalError::DivisionByZero
        | EvalError::UnknownFunction { .. }
        | EvalError::VectorDimensionMismatch { .. }
        | EvalError::ArgumentType { .. }
        | EvalError::InvalidJsonPath { .. }) => LiteError::from(other),
    }
}

fn unsupported_function(function: &str) -> LiteError {
    LiteError::Unsupported {
        detail: format!("aggregate function {function}() is not supported in Lite"),
    }
}

fn apply_having(
    rows: &mut Vec<Vec<Value>>,
    having: &[ScanFilter],
    group_by: &[GroupKeySpec],
    aggregates: &[AggregateSpec],
) -> Result<(), LiteError> {
    if having.is_empty() {
        return Ok(());
    }
    let columns = make_columns(group_by, aggregates);
    // HAVING evaluation is fallible, so the keep-decision is computed before
    // the retain rather than inside a closure that cannot propagate.
    let mut keep = Vec::with_capacity(rows.len());
    for row in rows.iter() {
        let mut map = HashMap::new();
        for (col, val) in columns.iter().zip(row.iter()) {
            map.insert(col.clone(), val.clone());
        }
        let doc = Value::Object(map);
        keep.push(ScanFilter::all_match_value(having, &doc)?);
    }
    let mut iter = keep.into_iter();
    rows.retain(|_| iter.next().unwrap_or(true));
    Ok(())
}

fn apply_sort(
    rows: &mut [Vec<Value>],
    sort_keys: &[SortKeySpec],
    group_by: &[GroupKeySpec],
    aggregates: &[AggregateSpec],
) {
    if sort_keys.is_empty() {
        return;
    }
    let columns = make_columns(group_by, aggregates);
    rows.sort_by(|a, b| {
        for key in sort_keys {
            // Aggregate output rows are addressed by output column name, so a
            // computed key has nothing to match here — skip it, matching the
            // Origin executor's `sort_aggregated_rows`.
            let Some(col) = key.as_column() else {
                continue;
            };
            let idx = columns.iter().position(|c| c == col);
            if let Some(i) = idx {
                let av = a.get(i).unwrap_or(&Value::Null);
                let bv = b.get(i).unwrap_or(&Value::Null);
                // NULL placement is absolute; the direction never flips it.
                let ord = match key.order_nulls(av == &Value::Null, bv == &Value::Null) {
                    Some(ord) => ord,
                    None => key.direct(value_cmp(av, bv)),
                };
                if ord != std::cmp::Ordering::Equal {
                    return ord;
                }
            }
        }
        std::cmp::Ordering::Equal
    });
}

pub(crate) fn value_cmp(a: &Value, b: &Value) -> std::cmp::Ordering {
    match (a, b) {
        (Value::Integer(x), Value::Integer(y)) => x.cmp(y),
        (Value::Float(x), Value::Float(y)) => x.partial_cmp(y).unwrap_or(std::cmp::Ordering::Equal),
        (Value::Integer(x), Value::Float(y)) => (*x as f64)
            .partial_cmp(y)
            .unwrap_or(std::cmp::Ordering::Equal),
        (Value::Float(x), Value::Integer(y)) => x
            .partial_cmp(&(*y as f64))
            .unwrap_or(std::cmp::Ordering::Equal),
        (Value::String(x), Value::String(y)) => x.cmp(y),
        (Value::Bool(x), Value::Bool(y)) => x.cmp(y),
        (Value::Null, Value::Null) => std::cmp::Ordering::Equal,
        (Value::Null, _) => std::cmp::Ordering::Less,
        (_, Value::Null) => std::cmp::Ordering::Greater,
        _ => std::cmp::Ordering::Equal,
    }
}

pub(crate) fn make_columns(group_by: &[GroupKeySpec], aggregates: &[AggregateSpec]) -> Vec<String> {
    let mut cols: Vec<String> = group_by.iter().map(|s| s.output_name.clone()).collect();
    for spec in aggregates {
        if let Some(alias) = &spec.user_alias {
            cols.push(alias.clone());
        } else {
            cols.push(spec.alias.clone());
        }
    }
    cols
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use nodedb_physical::physical_plan::query::AggregateSpec;
    use nodedb_query::expr::{BinaryOp, SqlExpr};

    fn make_spec(function: &str, field: &str, alias: &str) -> AggregateSpec {
        AggregateSpec {
            function: function.to_string(),
            alias: alias.to_string(),
            user_alias: None,
            field: field.to_string(),
            expr: None,
            param: None,
        }
    }

    fn make_rows(data: &[(&str, i64, f64)]) -> Vec<HashMap<String, Value>> {
        data.iter()
            .map(|(cat, n, price)| {
                let mut m = HashMap::new();
                m.insert("category".into(), Value::String(cat.to_string()));
                m.insert("count".into(), Value::Integer(*n));
                m.insert("price".into(), Value::Float(*price));
                m
            })
            .collect()
    }

    #[test]
    fn group_by_with_having() {
        let rows = make_rows(&[("a", 1, 10.0), ("a", 2, 20.0), ("b", 3, 5.0)]);
        let aggregates = vec![
            make_spec("COUNT", "*", "cnt"),
            make_spec("SUM", "price", "total"),
        ];
        // HAVING total > 10
        let having_filter = ScanFilter {
            field: "total".into(),
            op: nodedb_query::scan_filter::FilterOp::Gt,
            value: Value::Float(10.0),
            clauses: Vec::new(),
            expr: None,
        };
        let having_bytes = zerompk::to_msgpack_vec(&vec![having_filter]).unwrap();

        let result = execute_aggregate(
            rows,
            &[GroupKeySpec::column("category")],
            &aggregates,
            &[],
            &having_bytes,
            &[],
            &[],
        )
        .unwrap();
        // Only group "a" has total=30 > 10; group "b" has total=5.0
        assert_eq!(result.rows.len(), 1);
        let cat = &result.rows[0][0];
        assert_eq!(cat, &Value::String("a".into()));
    }

    #[test]
    fn integer_sum_past_i64_does_not_clamp() {
        let rows = make_rows(&[("a", i64::MAX, 1.0), ("a", 1, 1.0)]);
        let aggregates = vec![make_spec("SUM", "count", "total")];
        let result = execute_aggregate(rows, &[], &aggregates, &[], &[], &[], &[]).unwrap();
        assert_eq!(
            result.rows[0][0],
            Value::Decimal(rust_decimal::Decimal::from_i128_with_scale(
                i128::from(i64::MAX) + 1,
                0
            ))
        );
    }

    #[test]
    fn integer_sum_overflow_names_aggregate_and_column() {
        let row = |amount| {
            let mut m = HashMap::new();
            m.insert("amount".to_string(), Value::Decimal(amount));
            m
        };
        let max = rust_decimal::Decimal::MAX;
        let aggregates = vec![make_spec("SUM", "amount", "total")];
        let err = execute_aggregate(
            vec![row(max), row(max)],
            &[],
            &aggregates,
            &[],
            &[],
            &[],
            &[],
        )
        .unwrap_err();
        match err {
            LiteError::NumericValueOutOfRange { detail } => {
                assert!(detail.contains("sum(amount)"), "{detail}");
            }
            other => panic!("expected NumericValueOutOfRange, got {other:?}"),
        }
    }

    #[test]
    fn partial_aggregate_prefix_column() {
        let rows = make_rows(&[("x", 1, 1.0)]);
        let aggregates = vec![make_spec("COUNT", "*", "cnt")];
        let result =
            execute_partial_aggregate(rows, &["category".to_string()], &aggregates, &[]).unwrap();
        assert_eq!(result.columns[0], "__partial");
        assert_eq!(result.rows[0][0], Value::Bool(true));
    }

    #[test]
    fn group_by_computed_expression_key() {
        // GROUP BY on a computed key (count + count) rather than a bare column:
        // rows with counts 1 and 3 collapse into distinct groups 2 and 6, while
        // the two rows with count 1 share a group.
        let rows = make_rows(&[("a", 1, 10.0), ("b", 1, 20.0), ("c", 3, 5.0)]);
        let aggregates = vec![make_spec("COUNT", "*", "cnt")];

        let doubled = GroupKeySpec {
            output_name: "doubled".to_string(),
            field: None,
            expr: Some(SqlExpr::BinaryOp {
                left: Box::new(SqlExpr::Column("count".to_string())),
                op: BinaryOp::Add,
                right: Box::new(SqlExpr::Column("count".to_string())),
            }),
        };

        let result = execute_aggregate(rows, &[doubled], &aggregates, &[], &[], &[], &[]).unwrap();

        assert_eq!(result.columns[0], "doubled");
        assert_eq!(result.rows.len(), 2);

        let mut got: Vec<(i64, i64)> = result
            .rows
            .iter()
            .filter_map(|r| match (&r[0], &r[1]) {
                (Value::Integer(k), Value::Integer(n)) => Some((*k, *n)),
                _ => None,
            })
            .collect();
        got.sort_unstable();
        assert_eq!(got, vec![(2, 2), (6, 1)]);
    }
}