magnumdb 0.4.2

High-performance embeddable database engine in Rust with B+ Tree storage, WAL durability, Volcano query engine, and Postgres wire protocol support.
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
use anyhow::Result;
use std::collections::HashMap;

/// The Volcano-style Iterator Trait for query execution operators.
pub trait ExecutionPlan {
    fn open(&mut self) -> Result<()>;
    fn next(&mut self) -> Result<Option<Vec<String>>>;
    fn close(&mut self) -> Result<()>;
}

/// Sequential Scan Operator over table rows.
pub struct SeqScanExec {
    rows: Vec<Vec<String>>,
    cursor: usize,
}

impl SeqScanExec {
    pub fn new(rows: Vec<Vec<String>>) -> Self {
        Self { rows, cursor: 0 }
    }
}

impl ExecutionPlan for SeqScanExec {
    fn open(&mut self) -> Result<()> {
        self.cursor = 0;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        if self.cursor < self.rows.len() {
            let row = self.rows[self.cursor].clone();
            self.cursor += 1;
            Ok(Some(row))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.cursor = self.rows.len();
        Ok(())
    }
}

/// Filter Operator (WHERE clause evaluation).
pub struct FilterExec {
    child: Box<dyn ExecutionPlan>,
    col_idx: usize,
    target_val: String,
}

impl FilterExec {
    pub fn new(child: Box<dyn ExecutionPlan>, col_idx: usize, target_val: String) -> Self {
        Self {
            child,
            col_idx,
            target_val,
        }
    }
}

impl ExecutionPlan for FilterExec {
    fn open(&mut self) -> Result<()> {
        self.child.open()
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        while let Some(row) = self.child.next()? {
            if self.col_idx < row.len() && row[self.col_idx] == self.target_val {
                return Ok(Some(row));
            }
        }
        Ok(None)
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()
    }
}

/// Aggregate Function types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregateFunc {
    Count,
    Sum(usize),
    Avg(usize),
}

/// Aggregate Operator (`COUNT(*)`, `SUM(col)`, `AVG(col)`).
pub struct AggregateExec {
    child: Box<dyn ExecutionPlan>,
    func: AggregateFunc,
    executed: bool,
}

impl AggregateExec {
    pub fn new(child: Box<dyn ExecutionPlan>, func: AggregateFunc) -> Self {
        Self {
            child,
            func,
            executed: false,
        }
    }
}

impl ExecutionPlan for AggregateExec {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.executed = false;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        if self.executed {
            return Ok(None);
        }
        self.executed = true;

        let mut count = 0u64;
        let mut sum = 0.0f64;

        while let Some(row) = self.child.next()? {
            count += 1;
            match self.func {
                AggregateFunc::Count => {}
                AggregateFunc::Sum(col_idx) | AggregateFunc::Avg(col_idx) => {
                    if col_idx < row.len() {
                        if let Ok(val) = row[col_idx].parse::<f64>() {
                            sum += val;
                        }
                    }
                }
            }
        }

        let result_str = match self.func {
            AggregateFunc::Count => count.to_string(),
            AggregateFunc::Sum(_) => sum.to_string(),
            AggregateFunc::Avg(_) => {
                if count > 0 {
                    (sum / count as f64).to_string()
                } else {
                    "0".to_string()
                }
            }
        };

        Ok(Some(vec![result_str]))
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()
    }
}

/// Join type for relational join execution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JoinType {
    Inner,
    Left,
}

/// Hash Join Operator (`INNER JOIN`, `LEFT JOIN`).
pub struct HashJoinExec {
    left: Box<dyn ExecutionPlan>,
    right: Box<dyn ExecutionPlan>,
    left_key_idx: usize,
    right_key_idx: usize,
    join_type: JoinType,
    right_num_cols: usize,
    result_rows: Vec<Vec<String>>,
    cursor: usize,
}

impl HashJoinExec {
    pub fn new(
        left: Box<dyn ExecutionPlan>,
        right: Box<dyn ExecutionPlan>,
        left_key_idx: usize,
        right_key_idx: usize,
        join_type: JoinType,
        right_num_cols: usize,
    ) -> Self {
        Self {
            left,
            right,
            left_key_idx,
            right_key_idx,
            join_type,
            right_num_cols,
            result_rows: Vec::new(),
            cursor: 0,
        }
    }
}

impl ExecutionPlan for HashJoinExec {
    fn open(&mut self) -> Result<()> {
        self.left.open()?;
        self.right.open()?;
        self.result_rows.clear();
        self.cursor = 0;

        let mut hash_table: HashMap<String, Vec<Vec<String>>> = HashMap::new();
        while let Some(right_row) = self.right.next()? {
            if self.right_key_idx < right_row.len() {
                let key = right_row[self.right_key_idx].clone();
                hash_table.entry(key).or_default().push(right_row);
            }
        }

        while let Some(left_row) = self.left.next()? {
            if self.left_key_idx < left_row.len() {
                let key = &left_row[self.left_key_idx];
                if let Some(matching_right_rows) = hash_table.get(key) {
                    for r_row in matching_right_rows {
                        let mut joined = left_row.clone();
                        joined.extend_from_slice(r_row);
                        self.result_rows.push(joined);
                    }
                } else if self.join_type == JoinType::Left {
                    let mut joined = left_row.clone();
                    joined.extend(vec!["NULL".to_string(); self.right_num_cols]);
                    self.result_rows.push(joined);
                }
            }
        }

        Ok(())
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        if self.cursor < self.result_rows.len() {
            let row = self.result_rows[self.cursor].clone();
            self.cursor += 1;
            Ok(Some(row))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.left.close()?;
        self.right.close()?;
        self.cursor = self.result_rows.len();
        Ok(())
    }
}

/// Group By & Having Hash Aggregation Operator.
pub struct HashGroupAggregateExec {
    child: Box<dyn ExecutionPlan>,
    group_col_idx: usize,
    agg_func: AggregateFunc,
    having_op_val: Option<(String, String)>,
    result_rows: Vec<Vec<String>>,
    cursor: usize,
}

impl HashGroupAggregateExec {
    pub fn new(
        child: Box<dyn ExecutionPlan>,
        group_col_idx: usize,
        agg_func: AggregateFunc,
        having_op_val: Option<(String, String)>,
    ) -> Self {
        Self {
            child,
            group_col_idx,
            agg_func,
            having_op_val,
            result_rows: Vec::new(),
            cursor: 0,
        }
    }
}

impl ExecutionPlan for HashGroupAggregateExec {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.result_rows.clear();
        self.cursor = 0;

        let mut groups: HashMap<String, (u64, f64)> = HashMap::new();
        let mut group_keys = Vec::new();

        while let Some(row) = self.child.next()? {
            if self.group_col_idx < row.len() {
                let group_key = row[self.group_col_idx].clone();
                let entry = groups.entry(group_key.clone()).or_insert_with(|| {
                    group_keys.push(group_key);
                    (0, 0.0)
                });

                entry.0 += 1;
                match self.agg_func {
                    AggregateFunc::Count => {}
                    AggregateFunc::Sum(col_idx) | AggregateFunc::Avg(col_idx) => {
                        if col_idx < row.len() {
                            if let Ok(v) = row[col_idx].parse::<f64>() {
                                entry.1 += v;
                            }
                        }
                    }
                }
            }
        }

        for g_key in group_keys {
            if let Some(&(count, sum)) = groups.get(&g_key) {
                let agg_val = match self.agg_func {
                    AggregateFunc::Count => count.to_string(),
                    AggregateFunc::Sum(_) => sum.to_string(),
                    AggregateFunc::Avg(_) => {
                        if count > 0 {
                            (sum / count as f64).to_string()
                        } else {
                            "0".to_string()
                        }
                    }
                };

                let mut passes_having = true;
                if let Some((ref op, ref target_str)) = self.having_op_val {
                    if let Ok(target_num) = target_str.parse::<f64>() {
                        if let Ok(agg_num) = agg_val.parse::<f64>() {
                            passes_having = match op.as_str() {
                                ">=" => agg_num >= target_num,
                                "<=" => agg_num <= target_num,
                                ">" => agg_num > target_num,
                                "<" => agg_num < target_num,
                                "=" => (agg_num - target_num).abs() < 1e-9,
                                _ => true,
                            };
                        }
                    }
                }

                if passes_having {
                    self.result_rows.push(vec![g_key, agg_val]);
                }
            }
        }

        Ok(())
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        if self.cursor < self.result_rows.len() {
            let row = self.result_rows[self.cursor].clone();
            self.cursor += 1;
            Ok(Some(row))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()?;
        self.cursor = self.result_rows.len();
        Ok(())
    }
}

/// Sort Operator (`ORDER BY col [ASC|DESC]`).
pub struct SortExec {
    child: Box<dyn ExecutionPlan>,
    sort_col_idx: usize,
    is_desc: bool,
    result_rows: Vec<Vec<String>>,
    cursor: usize,
}

impl SortExec {
    pub fn new(child: Box<dyn ExecutionPlan>, sort_col_idx: usize, is_desc: bool) -> Self {
        Self {
            child,
            sort_col_idx,
            is_desc,
            result_rows: Vec::new(),
            cursor: 0,
        }
    }
}

impl ExecutionPlan for SortExec {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.result_rows.clear();
        self.cursor = 0;

        while let Some(row) = self.child.next()? {
            self.result_rows.push(row);
        }

        let idx = self.sort_col_idx;
        let is_desc = self.is_desc;

        self.result_rows.sort_by(|a, b| {
            let val_a = a.get(idx).map(|s| s.as_str()).unwrap_or("");
            let val_b = b.get(idx).map(|s| s.as_str()).unwrap_or("");

            let ord = match (val_a.parse::<f64>(), val_b.parse::<f64>()) {
                (Ok(num_a), Ok(num_b)) => num_a.partial_cmp(&num_b).unwrap_or(std::cmp::Ordering::Equal),
                _ => val_a.cmp(val_b),
            };

            if is_desc {
                ord.reverse()
            } else {
                ord
            }
        });

        Ok(())
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        if self.cursor < self.result_rows.len() {
            let row = self.result_rows[self.cursor].clone();
            self.cursor += 1;
            Ok(Some(row))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()?;
        self.cursor = self.result_rows.len();
        Ok(())
    }
}

/// Limit & Offset Operator (`LIMIT n [OFFSET m]`).
pub struct LimitOffsetExec {
    child: Box<dyn ExecutionPlan>,
    limit: Option<usize>,
    offset: usize,
    result_rows: Vec<Vec<String>>,
    cursor: usize,
}

impl LimitOffsetExec {
    pub fn new(child: Box<dyn ExecutionPlan>, limit: Option<usize>, offset: usize) -> Self {
        Self {
            child,
            limit,
            offset,
            result_rows: Vec::new(),
            cursor: 0,
        }
    }
}

impl ExecutionPlan for LimitOffsetExec {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.result_rows.clear();
        self.cursor = 0;

        let mut skipped = 0;
        let mut count = 0;

        while let Some(row) = self.child.next()? {
            if skipped < self.offset {
                skipped += 1;
                continue;
            }

            if let Some(l) = self.limit {
                if count >= l {
                    break;
                }
            }

            self.result_rows.push(row);
            count += 1;
        }

        Ok(())
    }

    fn next(&mut self) -> Result<Option<Vec<String>>> {
        if self.cursor < self.result_rows.len() {
            let row = self.result_rows[self.cursor].clone();
            self.cursor += 1;
            Ok(Some(row))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()?;
        self.cursor = self.result_rows.len();
        Ok(())
    }
}