akar-storage 0.1.1

Storage engine for the Akar embedded graph database
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
//! Zone map predicate skipping for column scans.
//!
//! Uses column min/max statistics to determine whether a column chunk
//! can be skipped during a scan, avoiding unnecessary I/O.
//!
//! Ported from C++ `src/include/storage/predicate/` (column_predicate.h,
//! constant_predicate.h, null_predicate.h) and `src/storage/predicate/`.

use akar_common::types::Value;

/// Result of checking a zone map against a predicate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZoneMapCheckResult {
    /// Cannot determine — must scan the chunk.
    AlwaysScan = 0,
    /// Predicate definitely won't match — safe to skip.
    SkipScan = 1,
}

/// Statistics for a column chunk, used for zone map predicate checking.
#[derive(Debug, Clone)]
pub struct ColumnChunkStats {
    /// Minimum value in the chunk (None if unknown).
    pub min: Option<Value>,
    /// Maximum value in the chunk (None if unknown).
    pub max: Option<Value>,
    /// Whether the chunk is guaranteed to have no nulls.
    pub guaranteed_no_nulls: bool,
    /// Whether the chunk is guaranteed to have all nulls.
    pub guaranteed_all_nulls: bool,
}

impl ColumnChunkStats {
    pub fn new(min: Option<Value>, max: Option<Value>) -> Self {
        Self {
            min,
            max,
            guaranteed_no_nulls: true,
            guaranteed_all_nulls: false,
        }
    }

    /// Create stats for an all-null chunk.
    pub fn all_nulls() -> Self {
        Self {
            min: None,
            max: None,
            guaranteed_no_nulls: false,
            guaranteed_all_nulls: true,
        }
    }

    /// Update min/max with a new value.
    /// Only works for ordered Value types (Int64, Double, Float, Int32, String).
    pub fn update(&mut self, val: &Value) {
        match (&self.min, &self.max) {
            (None, None) => {
                self.min = Some(val.clone());
                self.max = Some(val.clone());
            }
            (Some(min), Some(max)) => {
                if Self::value_lt(val, min) {
                    self.min = Some(val.clone());
                }
                if Self::value_gt(val, max) {
                    self.max = Some(val.clone());
                }
            }
            _ => unreachable!(),
        }
    }

    /// Compare two Values for ordering. Returns true if a < b.
    fn value_lt(a: &Value, b: &Value) -> bool {
        match (a, b) {
            (Value::Int64(x), Value::Int64(y)) => x < y,
            (Value::Double(x), Value::Double(y)) => x < y,
            (Value::Float(x), Value::Float(y)) => x < y,
            (Value::Int32(x), Value::Int32(y)) => x < y,
            (Value::String(x), Value::String(y)) => x < y,
            _ => false,
        }
    }

    /// Compare two Values for ordering. Returns true if a > b.
    fn value_gt(a: &Value, b: &Value) -> bool {
        match (a, b) {
            (Value::Int64(x), Value::Int64(y)) => x > y,
            (Value::Double(x), Value::Double(y)) => x > y,
            (Value::Float(x), Value::Float(y)) => x > y,
            (Value::Int32(x), Value::Int32(y)) => x > y,
            (Value::String(x), Value::String(y)) => x > y,
            _ => false,
        }
    }
}

// ==================== Check helpers ====================

/// Check if a value falls within [min, max].
fn in_range<T: PartialOrd>(min: &T, max: &T, val: &T) -> bool {
    val >= min && val <= max
}

/// Zone map check for constant-value comparison predicates.
/// Returns `SkipScan` if the predicate cannot possibly match the zone.
fn check_constant_predicate<T: PartialOrd>(min: &T, max: &T, constant: &T, op: &str) -> ZoneMapCheckResult {
    match op {
        "=" | "==" => {
            if !in_range(min, max, constant) {
                return ZoneMapCheckResult::SkipScan;
            }
        }
        "!=" | "<>" => {
            if constant == min && constant == max {
                return ZoneMapCheckResult::SkipScan;
            }
        }
        ">" => {
            if constant >= max {
                return ZoneMapCheckResult::SkipScan;
            }
        }
        ">=" => {
            if constant > max {
                return ZoneMapCheckResult::SkipScan;
            }
        }
        "<" => {
            if constant <= min {
                return ZoneMapCheckResult::SkipScan;
            }
        }
        "<=" if constant < min => {
            return ZoneMapCheckResult::SkipScan;
        }
        _ => {}
    }
    ZoneMapCheckResult::AlwaysScan
}

/// Check whether a column chunk with the given stats can be skipped
/// based on a predicate `(column op constant)`.
///
/// Returns `SkipScan` if the chunk definitely doesn't match.
pub fn check_zone_map(stats: &ColumnChunkStats, op: &str, constant: &Value) -> ZoneMapCheckResult {
    let (Some(min), Some(max)) = (&stats.min, &stats.max) else {
        return ZoneMapCheckResult::AlwaysScan;
    };

    // Type-based dispatch for comparison
    match (min, max, constant) {
        // Int64
        (Value::Int64(min_v), Value::Int64(max_v), Value::Int64(c)) => check_constant_predicate(min_v, max_v, c, op),
        // Double
        (Value::Double(min_v), Value::Double(max_v), Value::Double(c)) => check_constant_predicate(min_v, max_v, c, op),
        // Float
        (Value::Float(min_v), Value::Float(max_v), Value::Float(c)) => check_constant_predicate(min_v, max_v, c, op),
        // Int32
        (Value::Int32(min_v), Value::Int32(max_v), Value::Int32(c)) => check_constant_predicate(min_v, max_v, c, op),
        // String
        (Value::String(min_v), Value::String(max_v), Value::String(c)) => check_constant_predicate(min_v, max_v, c, op),
        // InternalID (compare by offset)
        (Value::InternalID(min_id), Value::InternalID(max_id), Value::InternalID(c_id)) => {
            check_constant_predicate(&min_id.offset, &max_id.offset, &c_id.offset, op)
        }
        // Mixed/unknown types — fall back to AlwaysScan
        _ => ZoneMapCheckResult::AlwaysScan,
    }
}

/// Check a null predicate against chunk stats.
/// `is_null` is true for `IS NULL`, false for `IS NOT NULL`.
pub fn check_null_zone_map(stats: &ColumnChunkStats, is_null: bool) -> ZoneMapCheckResult {
    if is_null {
        if stats.guaranteed_no_nulls {
            return ZoneMapCheckResult::SkipScan;
        }
    } else if stats.guaranteed_all_nulls {
        return ZoneMapCheckResult::SkipScan;
    }
    ZoneMapCheckResult::AlwaysScan
}

#[cfg(test)]
mod tests {
    use super::*;
    use akar_common::types::InternalID;

    fn int_stats(min: i64, max: i64) -> ColumnChunkStats {
        ColumnChunkStats::new(Some(Value::Int64(min)), Some(Value::Int64(max)))
    }

    #[test]
    fn test_eq_in_range() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, "=", &Value::Int64(15)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_eq_out_of_range() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, "=", &Value::Int64(25)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_eq_below_range() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, "=", &Value::Int64(5)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_gt_all_below() {
        let stats = int_stats(10, 20);
        // constant > max → nothing in chunk can match
        assert_eq!(
            check_zone_map(&stats, ">", &Value::Int64(25)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_gt_some_above() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, ">", &Value::Int64(5)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_lt_all_above() {
        let stats = int_stats(10, 20);
        // constant < min → nothing in chunk can match
        assert_eq!(
            check_zone_map(&stats, "<", &Value::Int64(5)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_lt_some_below() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, "<", &Value::Int64(25)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_gte_eq_max_not_skip() {
        let stats = int_stats(10, 20);
        // constant == max → >= can match
        assert_eq!(
            check_zone_map(&stats, ">=", &Value::Int64(20)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_gte_gt_max() {
        let stats = int_stats(10, 20);
        // constant > max → >= can't match
        assert_eq!(
            check_zone_map(&stats, ">=", &Value::Int64(21)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_lte_eq_min_not_skip() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, "<=", &Value::Int64(10)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_lte_lt_min() {
        let stats = int_stats(10, 20);
        assert_eq!(
            check_zone_map(&stats, "<=", &Value::Int64(9)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_not_eq_single_value() {
        let stats = int_stats(15, 15);
        // constant == min == max → not_eq can't match
        assert_eq!(
            check_zone_map(&stats, "!=", &Value::Int64(15)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_not_eq_range() {
        let stats = int_stats(10, 20);
        // constant within range → might match
        assert_eq!(
            check_zone_map(&stats, "!=", &Value::Int64(15)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_null_predicate_is_null_no_nulls() {
        let stats = ColumnChunkStats {
            min: Some(Value::Int64(1)),
            max: Some(Value::Int64(10)),
            guaranteed_no_nulls: true,
            guaranteed_all_nulls: false,
        };
        assert_eq!(check_null_zone_map(&stats, true), ZoneMapCheckResult::SkipScan);
    }

    #[test]
    fn test_null_predicate_is_null_has_nulls() {
        let stats = ColumnChunkStats {
            min: Some(Value::Int64(1)),
            max: Some(Value::Int64(10)),
            guaranteed_no_nulls: false,
            guaranteed_all_nulls: false,
        };
        assert_eq!(check_null_zone_map(&stats, true), ZoneMapCheckResult::AlwaysScan);
    }

    #[test]
    fn test_null_predicate_is_not_null_all_nulls() {
        let stats = ColumnChunkStats {
            min: None,
            max: None,
            guaranteed_no_nulls: false,
            guaranteed_all_nulls: true,
        };
        assert_eq!(check_null_zone_map(&stats, false), ZoneMapCheckResult::SkipScan);
    }

    #[test]
    fn test_no_stats_always_scan() {
        let stats = ColumnChunkStats::new(None, None);
        assert_eq!(
            check_zone_map(&stats, "=", &Value::Int64(5)),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_string_zone_map() {
        let stats = ColumnChunkStats::new(
            Some(Value::String("apple".into())),
            Some(Value::String("banana".into())),
        );
        // "cherry" > "banana" → skip
        assert_eq!(
            check_zone_map(&stats, "=", &Value::String("cherry".into())),
            ZoneMapCheckResult::SkipScan
        );
        // "avocado" within [apple, banana] → scan
        assert_eq!(
            check_zone_map(&stats, "=", &Value::String("avocado".into())),
            ZoneMapCheckResult::AlwaysScan
        );
    }

    #[test]
    fn test_internal_id_zone_map() {
        let stats = ColumnChunkStats::new(
            Some(Value::InternalID(InternalID { offset: 0, table_id: 1 })),
            Some(Value::InternalID(InternalID {
                offset: 100,
                table_id: 1,
            })),
        );
        assert_eq!(
            check_zone_map(
                &stats,
                "=",
                &Value::InternalID(InternalID {
                    offset: 50,
                    table_id: 1
                })
            ),
            ZoneMapCheckResult::AlwaysScan
        );
        assert_eq!(
            check_zone_map(
                &stats,
                "=",
                &Value::InternalID(InternalID {
                    offset: 200,
                    table_id: 1
                })
            ),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_double_zone_map() {
        let stats = ColumnChunkStats::new(Some(Value::Double(1.5)), Some(Value::Double(9.5)));
        assert_eq!(
            check_zone_map(&stats, ">", &Value::Double(10.0)),
            ZoneMapCheckResult::SkipScan
        );
        assert_eq!(
            check_zone_map(&stats, "<", &Value::Double(1.0)),
            ZoneMapCheckResult::SkipScan
        );
    }

    #[test]
    fn test_column_chunk_stats_update() {
        let mut stats = ColumnChunkStats::new(None, None);
        stats.update(&Value::Int64(5));
        assert_eq!(stats.min, Some(Value::Int64(5)));
        assert_eq!(stats.max, Some(Value::Int64(5)));

        stats.update(&Value::Int64(3));
        assert_eq!(stats.min, Some(Value::Int64(3)));
        assert_eq!(stats.max, Some(Value::Int64(5)));

        stats.update(&Value::Int64(10));
        assert_eq!(stats.min, Some(Value::Int64(3)));
        assert_eq!(stats.max, Some(Value::Int64(10)));
    }
}