narrowdb 0.3.0

A lightweight columnar database engine for log and time-series 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
use anyhow::{Result, bail};
use rayon::prelude::*;
use rustc_hash::FxHashMap;

use crate::sql::CompareOp;
use crate::storage::{ColumnData, ColumnStats, LoadedRowGroup, Table};
use crate::types::Value;

use super::aggregate::AggState;
use super::bitmap::SelectionBitmap;
use super::compile::{CompiledFilter, CompiledProjection, LocalKeyPart};

const PARALLEL_ROW_GROUPS: usize = 4;

// ---------------------------------------------------------------------------
// Parallel scan — replaces the old sequential visitor-based scan_table
// ---------------------------------------------------------------------------

pub(super) fn parallel_scan_table<F>(
    table: &Table,
    mapped: Option<&[u8]>,
    filters: &[CompiledFilter],
    required_columns: &[usize],
    extractor: F,
) -> Result<Vec<Vec<Value>>>
where
    F: Fn(&LoadedRowGroup<'_>, usize) -> Vec<Value> + Sync + Send,
{
    let process = |stored: &crate::storage::StoredRowGroup| -> Result<Vec<Vec<Value>>> {
        if !row_group_matches(stored.rows(), stored.stats(), filters) {
            return Ok(Vec::new());
        }
        let loaded = stored.load(&table.schema, mapped, required_columns)?;
        let selection = select_rows_bitmap(&loaded, filters);
        if selection.is_empty() {
            return Ok(Vec::new());
        }
        let mut rows = Vec::with_capacity(selection.count());
        for row_idx in selection.iter_set() {
            rows.push(extractor(&loaded, row_idx));
        }
        Ok(rows)
    };

    let chunks: Vec<Result<Vec<Vec<Value>>>> = if table.row_groups.len() >= PARALLEL_ROW_GROUPS {
        table.row_groups.par_iter().map(process).collect()
    } else {
        table.row_groups.iter().map(process).collect()
    };

    let mut all_rows = Vec::new();
    for chunk in chunks {
        all_rows.extend(chunk?);
    }
    Ok(all_rows)
}

// ---------------------------------------------------------------------------
// Aggregate scanning (already parallel via rayon)
// ---------------------------------------------------------------------------

pub(super) fn collect_row_group_aggregates<T, F>(
    table: &Table,
    mapped: Option<&[u8]>,
    filters: &[CompiledFilter],
    required_columns: &[usize],
    worker: F,
) -> Result<Vec<T>>
where
    T: Send,
    F: Fn(&LoadedRowGroup<'_>) -> Result<T> + Sync + Send,
{
    let partials = if table.row_groups.len() >= PARALLEL_ROW_GROUPS {
        table
            .row_groups
            .par_iter()
            .filter(|rg| row_group_matches(rg.rows(), rg.stats(), filters))
            .map(|rg| rg.load(&table.schema, mapped, required_columns).and_then(|l| worker(&l)))
            .collect::<Vec<_>>()
    } else {
        table
            .row_groups
            .iter()
            .filter(|rg| row_group_matches(rg.rows(), rg.stats(), filters))
            .map(|rg| rg.load(&table.schema, mapped, required_columns).and_then(|l| worker(&l)))
            .collect::<Vec<_>>()
    };
    partials.into_iter().collect()
}

pub(super) fn aggregate_row_group_all(
    row_group: &LoadedRowGroup<'_>,
    filters: &[CompiledFilter],
    projections: &[CompiledProjection],
) -> Result<Option<Vec<AggState>>> {
    let selection = select_rows_bitmap(row_group, filters);
    if selection.is_empty() {
        return Ok(None);
    }
    let mut states = projections.iter().map(AggState::new).collect::<Vec<_>>();
    for row in selection.iter_set() {
        for (state, projection) in states.iter_mut().zip(projections) {
            state.update(projection, row_group, row)?;
        }
    }
    Ok(Some(states))
}

pub(super) fn aggregate_row_group_grouped(
    row_group: &LoadedRowGroup<'_>,
    filters: &[CompiledFilter],
    projections: &[CompiledProjection],
    group_indexes: &[usize],
) -> Result<FxHashMap<Vec<Value>, Vec<AggState>>> {
    let selection = select_rows_bitmap(row_group, filters);
    if selection.is_empty() {
        return Ok(FxHashMap::default());
    }

    // Fast path: single StringDict GROUP BY column — use array-indexed aggregation.
    if group_indexes.len() == 1 {
        if let ColumnData::StringDict { dictionary, codes } =
            row_group.column(group_indexes[0])
        {
            return aggregate_single_stringdict(
                row_group, &selection, projections, dictionary, codes,
            );
        }
    }

    let mut local_groups: FxHashMap<Vec<LocalKeyPart>, Vec<AggState>> = FxHashMap::default();
    for row in selection.iter_set() {
        let key = group_indexes
            .iter()
            .map(|index| LocalKeyPart::from_column(row_group.column(*index), row))
            .collect::<Result<Vec<_>>>()?;
        let states = local_groups
            .entry(key)
            .or_insert_with(|| projections.iter().map(AggState::new).collect());
        for (state, projection) in states.iter_mut().zip(projections) {
            state.update(projection, row_group, row)?;
        }
    }

    let mut materialized = FxHashMap::default();
    for (local_key, local_states) in local_groups {
        let key = materialize_group_key(row_group, group_indexes, &local_key)?;
        materialized.insert(key, local_states);
    }
    Ok(materialized)
}

/// Array-indexed aggregation for a single StringDict GROUP BY column.
/// Replaces HashMap with a fixed-size array indexed by dictionary code.
fn aggregate_single_stringdict(
    row_group: &LoadedRowGroup<'_>,
    selection: &SelectionBitmap,
    projections: &[CompiledProjection],
    dictionary: &[String],
    codes: &[u32],
) -> Result<FxHashMap<Vec<Value>, Vec<AggState>>> {
    let dict_size = dictionary.len();
    let mut states_array: Vec<Option<Vec<AggState>>> = (0..dict_size).map(|_| None).collect();

    for row in selection.iter_set() {
        let code = codes[row] as usize;
        debug_assert!(code < dict_size);
        let states = states_array[code]
            .get_or_insert_with(|| projections.iter().map(AggState::new).collect());
        for (state, proj) in states.iter_mut().zip(projections) {
            state.update(proj, row_group, row)?;
        }
    }

    let mut result = FxHashMap::default();
    for (code, slot) in states_array.into_iter().enumerate() {
        if let Some(agg_states) = slot {
            let key = vec![Value::String(dictionary[code].clone())];
            result.insert(key, agg_states);
        }
    }
    Ok(result)
}

// ---------------------------------------------------------------------------
// Row group stats pruning
// ---------------------------------------------------------------------------

fn row_group_matches(rows: usize, stats: &[ColumnStats], filters: &[CompiledFilter]) -> bool {
    filters.iter().all(|filter| match filter.op {
        CompareOp::Eq => stats_may_match_eq(&stats[filter.column_index], &filter.value),
        CompareOp::Lt => stats_may_match_lt(&stats[filter.column_index], &filter.value),
        CompareOp::Lte => stats_may_match_lte(&stats[filter.column_index], &filter.value),
        CompareOp::Gt => stats_may_match_gt(&stats[filter.column_index], &filter.value),
        CompareOp::Gte => stats_may_match_gte(&stats[filter.column_index], &filter.value),
        CompareOp::NotEq => true,
        CompareOp::IsNull => stats[filter.column_index].null_count > 0,
        CompareOp::IsNotNull => stats[filter.column_index].null_count < rows,
    })
}

fn stats_may_match_eq(stats: &ColumnStats, value: &Value) -> bool {
    match (&stats.min, &stats.max) {
        (Some(min), Some(max)) => {
            min.compare(value).is_none_or(|o| o != std::cmp::Ordering::Greater)
                && max.compare(value).is_none_or(|o| o != std::cmp::Ordering::Less)
        }
        _ => true,
    }
}

fn stats_may_match_lt(stats: &ColumnStats, value: &Value) -> bool {
    match &stats.min {
        Some(min) => min.compare(value).is_none_or(|o| o == std::cmp::Ordering::Less),
        None => true,
    }
}

fn stats_may_match_lte(stats: &ColumnStats, value: &Value) -> bool {
    match &stats.min {
        Some(min) => min.compare(value).is_none_or(|o| o != std::cmp::Ordering::Greater),
        None => true,
    }
}

fn stats_may_match_gt(stats: &ColumnStats, value: &Value) -> bool {
    match &stats.max {
        Some(max) => max.compare(value).is_none_or(|o| o == std::cmp::Ordering::Greater),
        None => true,
    }
}

fn stats_may_match_gte(stats: &ColumnStats, value: &Value) -> bool {
    match &stats.max {
        Some(max) => max.compare(value).is_none_or(|o| o != std::cmp::Ordering::Less),
        None => true,
    }
}

// ---------------------------------------------------------------------------
// Vectorized filter evaluation — produces SelectionBitmap
// ---------------------------------------------------------------------------

fn select_rows_bitmap(row_group: &LoadedRowGroup<'_>, filters: &[CompiledFilter]) -> SelectionBitmap {
    if !row_group_matches(row_group.rows, row_group.stats, filters) {
        return SelectionBitmap::none(row_group.rows);
    }
    if filters.is_empty() {
        return SelectionBitmap::all(row_group.rows);
    }

    let mut bitmap = evaluate_filter_bitmap(row_group, &filters[0]);
    for filter in &filters[1..] {
        if bitmap.is_empty() {
            break;
        }
        bitmap.intersect(&evaluate_filter_bitmap(row_group, filter));
    }
    bitmap
}

fn evaluate_filter_bitmap(row_group: &LoadedRowGroup<'_>, filter: &CompiledFilter) -> SelectionBitmap {
    let nulls = row_group.nulls(filter.column_index);

    // Handle IS NULL / IS NOT NULL directly from null bitmap.
    match filter.op {
        CompareOp::IsNull => {
            return match nulls {
                Some(n) => {
                    let mut bm = SelectionBitmap::all(row_group.rows);
                    bm.mask_null(n);
                    bm
                }
                None => SelectionBitmap::none(row_group.rows),
            };
        }
        CompareOp::IsNotNull => {
            return match nulls {
                Some(n) => {
                    let mut bm = SelectionBitmap::all(row_group.rows);
                    bm.mask_non_null(n);
                    bm
                }
                None => SelectionBitmap::all(row_group.rows),
            };
        }
        _ => {}
    }

    let column = row_group.column(filter.column_index);
    let mut bitmap = evaluate_column_filter(column, filter.op, &filter.value, row_group.rows);

    // Null values must not pass non-null filters (SQL semantics).
    if let Some(n) = nulls {
        bitmap.mask_non_null(n);
    }
    bitmap
}

fn evaluate_column_filter(
    column: &ColumnData,
    op: CompareOp,
    value: &Value,
    rows: usize,
) -> SelectionBitmap {
    match (column, value) {
        (ColumnData::Int64(values), Value::Int64(target)) => {
            eval_int64(values, *target, op, rows)
        }
        (ColumnData::Int64(values), Value::Float64(target)) => {
            eval_int64_vs_float(values, target.into_inner(), op, rows)
        }
        (ColumnData::Float64(values), Value::Float64(target)) => {
            eval_float64(values, *target, op, rows)
        }
        (ColumnData::Float64(values), Value::Int64(target)) => {
            eval_float64(values, ordered_float::OrderedFloat(*target as f64), op, rows)
        }
        (ColumnData::Bool(values), Value::Bool(target)) => {
            eval_bool(values, *target, op, rows)
        }
        (ColumnData::StringDict { dictionary, codes }, Value::String(target)) => {
            eval_string_dict(dictionary, codes, target, op, rows)
        }
        (ColumnData::StringPlain(values), Value::String(target)) => {
            eval_string_plain(values, target, op, rows)
        }
        _ => {
            // Type mismatch: no rows match.
            SelectionBitmap::none(rows)
        }
    }
}

// ---------------------------------------------------------------------------
// Type-specialized vectorized evaluators
// ---------------------------------------------------------------------------

fn eval_int64(values: &[i64], target: i64, op: CompareOp, rows: usize) -> SelectionBitmap {
    let pred: fn(i64, i64) -> bool = match op {
        CompareOp::Eq => |a, b| a == b,
        CompareOp::NotEq => |a, b| a != b,
        CompareOp::Lt => |a, b| a < b,
        CompareOp::Lte => |a, b| a <= b,
        CompareOp::Gt => |a, b| a > b,
        CompareOp::Gte => |a, b| a >= b,
        _ => unreachable!(),
    };
    build_bitmap_from_predicate(rows, |i| pred(values[i], target))
}

fn eval_int64_vs_float(values: &[i64], target: f64, op: CompareOp, rows: usize) -> SelectionBitmap {
    let pred: fn(f64, f64) -> bool = match op {
        CompareOp::Eq => |a, b| a == b,
        CompareOp::NotEq => |a, b| a != b,
        CompareOp::Lt => |a, b| a < b,
        CompareOp::Lte => |a, b| a <= b,
        CompareOp::Gt => |a, b| a > b,
        CompareOp::Gte => |a, b| a >= b,
        _ => unreachable!(),
    };
    build_bitmap_from_predicate(rows, |i| pred(values[i] as f64, target))
}

fn eval_float64(
    values: &[ordered_float::OrderedFloat<f64>],
    target: ordered_float::OrderedFloat<f64>,
    op: CompareOp,
    rows: usize,
) -> SelectionBitmap {
    let pred: fn(ordered_float::OrderedFloat<f64>, ordered_float::OrderedFloat<f64>) -> bool = match op {
        CompareOp::Eq => |a, b| a == b,
        CompareOp::NotEq => |a, b| a != b,
        CompareOp::Lt => |a, b| a < b,
        CompareOp::Lte => |a, b| a <= b,
        CompareOp::Gt => |a, b| a > b,
        CompareOp::Gte => |a, b| a >= b,
        _ => unreachable!(),
    };
    build_bitmap_from_predicate(rows, |i| pred(values[i], target))
}

fn eval_bool(values: &[bool], target: bool, op: CompareOp, rows: usize) -> SelectionBitmap {
    match op {
        CompareOp::Eq => build_bitmap_from_predicate(rows, |i| values[i] == target),
        CompareOp::NotEq => build_bitmap_from_predicate(rows, |i| values[i] != target),
        _ => SelectionBitmap::none(rows),
    }
}

fn eval_string_dict(
    dictionary: &[String],
    codes: &[u32],
    target: &str,
    op: CompareOp,
    rows: usize,
) -> SelectionBitmap {
    match op {
        CompareOp::Eq => {
            // Find matching code in dictionary, then compare as integers.
            match dictionary.iter().position(|s| s == target) {
                Some(code) => {
                    let code = code as u32;
                    build_bitmap_from_predicate(rows, |i| codes[i] == code)
                }
                None => SelectionBitmap::none(rows),
            }
        }
        CompareOp::NotEq => {
            match dictionary.iter().position(|s| s == target) {
                Some(code) => {
                    let code = code as u32;
                    build_bitmap_from_predicate(rows, |i| codes[i] != code)
                }
                None => SelectionBitmap::all(rows),
            }
        }
        _ => {
            // Range comparisons: build a lookup table for matching codes.
            let pred: fn(&str, &str) -> bool = match op {
                CompareOp::Lt => |a, b| a < b,
                CompareOp::Lte => |a, b| a <= b,
                CompareOp::Gt => |a, b| a > b,
                CompareOp::Gte => |a, b| a >= b,
                _ => unreachable!(),
            };
            let matching_codes: Vec<bool> = dictionary.iter().map(|s| pred(s, target)).collect();
            build_bitmap_from_predicate(rows, |i| matching_codes[codes[i] as usize])
        }
    }
}

fn eval_string_plain(values: &[String], target: &str, op: CompareOp, rows: usize) -> SelectionBitmap {
    let pred: fn(&str, &str) -> bool = match op {
        CompareOp::Eq => |a, b| a == b,
        CompareOp::NotEq => |a, b| a != b,
        CompareOp::Lt => |a, b| a < b,
        CompareOp::Lte => |a, b| a <= b,
        CompareOp::Gt => |a, b| a > b,
        CompareOp::Gte => |a, b| a >= b,
        _ => unreachable!(),
    };
    build_bitmap_from_predicate(rows, |i| pred(&values[i], target))
}

// ---------------------------------------------------------------------------
// Bitmap building helper — processes in 64-element chunks for auto-vectorization
// ---------------------------------------------------------------------------

#[inline]
fn build_bitmap_from_predicate<F>(rows: usize, pred: F) -> SelectionBitmap
where
    F: Fn(usize) -> bool,
{
    let mut bitmap = SelectionBitmap::none(rows);
    let words = bitmap.words_mut();
    let full_words = rows / 64;
    for word_idx in 0..full_words {
        let base = word_idx * 64;
        let mut word: u64 = 0;
        for bit in 0..64 {
            word |= (pred(base + bit) as u64) << bit;
        }
        words[word_idx] = word;
    }
    // Handle the remainder.
    let remainder = rows % 64;
    if remainder > 0 {
        let base = full_words * 64;
        let mut word: u64 = 0;
        for bit in 0..remainder {
            word |= (pred(base + bit) as u64) << bit;
        }
        words[full_words] = word;
    }
    bitmap.recount();
    bitmap
}

// ---------------------------------------------------------------------------
// Group key materialization
// ---------------------------------------------------------------------------

fn materialize_group_key(
    row_group: &LoadedRowGroup<'_>,
    group_indexes: &[usize],
    key: &[LocalKeyPart],
) -> Result<Vec<Value>> {
    let mut values = Vec::with_capacity(key.len());
    for (group_index, part) in group_indexes.iter().zip(key) {
        let value = match (row_group.column(*group_index), part) {
            (ColumnData::Int64(_), LocalKeyPart::Int64(value)) => Value::Int64(*value),
            (ColumnData::Float64(_), LocalKeyPart::Float64(value)) => Value::Float64(*value),
            (ColumnData::Bool(_), LocalKeyPart::Bool(value)) => Value::Bool(*value),
            (ColumnData::StringPlain(_), LocalKeyPart::String(value)) => {
                Value::String(value.clone())
            }
            (ColumnData::StringDict { dictionary, .. }, LocalKeyPart::StringCode(code)) => {
                Value::String(dictionary[*code as usize].clone())
            }
            _ => bail!("group key type mismatch"),
        };
        values.push(value);
    }
    Ok(values)
}