reddb-io-server 1.9.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
//! Hypertable chunk pruning for the SELECT planner.
//!
//! Phase 0 of PRD #850 — *activate* the dormant partition-pruning
//! primitive for hypertables. The machinery already exists
//! ([`super::partition_pruning`] + the per-chunk time bounds tracked by
//! [`HypertableRegistry`](crate::storage::timeseries::HypertableRegistry))
//! but nothing in the SELECT path consulted it, so every query against a
//! hypertable scanned every chunk.
//!
//! This module is the bridge: given a hypertable's spec, its chunk set,
//! and the SELECT `WHERE` clause, it returns only the chunks whose
//! declared `[start_ns, end_ns)` interval can contain a row the temporal
//! predicate admits. Chunks proven disjoint from the predicate window are
//! dropped; everything else is kept.
//!
//! **Soundness contract** — the pruner never drops a chunk that could
//! hold a matching row. A row in chunk `C` carries a timestamp inside
//! `[C.start_ns, C.end_ns_exclusive)`; if any timestamp in that interval
//! satisfies the predicate, `C` is kept. When the `WHERE` clause does not
//! constrain the time column (or uses a shape the lowering can't reason
//! about), the pruner is conservative and keeps every chunk — exactly the
//! Timescale / Postgres contract.

use crate::storage::query::ast::{CompareOp, FieldRef, Filter};
use crate::storage::schema::Value;
use crate::storage::timeseries::{ChunkMeta, HypertableSpec};

use super::partition_pruning::{
    prune_range, PruneKind, PruneOp, PrunePartitioning, PrunePredicate, PruneValue, RangeChild,
};

/// Stable per-chunk name used to thread results through the generic
/// range pruner. `start_ns` is unique within a hypertable, so this is a
/// 1:1 key back to the originating [`ChunkMeta`].
fn chunk_name(chunk: &ChunkMeta) -> String {
    format!("{}:{}", chunk.id.hypertable, chunk.id.start_ns)
}

/// Column a `FieldRef` targets, when it is a plain column / property
/// reference. A hypertable time predicate lowers to a bare `TableColumn`.
fn field_column(field: &FieldRef) -> Option<&str> {
    match field {
        FieldRef::TableColumn { column, .. } => Some(column.as_str()),
        FieldRef::NodeProperty { property, .. } | FieldRef::EdgeProperty { property, .. } => {
            Some(property.as_str())
        }
        FieldRef::NodeId { .. } => None,
    }
}

/// Lower an integer-shaped time value. The time axis is unix-ns
/// `BIGINT`, so only integer variants are actionable; anything else
/// yields `None`, which makes the enclosing predicate `Opaque` (keep
/// every chunk) — conservative and correct.
fn int_value(value: &Value) -> Option<i64> {
    match value {
        Value::Integer(n) | Value::Timestamp(n) | Value::Duration(n) => Some(*n),
        Value::UnsignedInteger(n) => i64::try_from(*n).ok(),
        _ => None,
    }
}

fn map_op(op: CompareOp) -> PruneOp {
    match op {
        CompareOp::Eq => PruneOp::Eq,
        CompareOp::Ne => PruneOp::NotEq,
        CompareOp::Lt => PruneOp::Lt,
        CompareOp::Le => PruneOp::LtEq,
        CompareOp::Gt => PruneOp::Gt,
        CompareOp::Ge => PruneOp::GtEq,
    }
}

/// Lower the runtime `Filter` AST to the pruner's [`PrunePredicate`],
/// keeping only the fragments that reference `time_column`. Any shape we
/// can't act on (a predicate on another column, a `LIKE`, a `NOT`, a
/// non-integer literal) collapses to `Opaque`, which the pruner reads as
/// "every chunk possibly matches".
fn lower_filter(filter: &Filter, time_column: &str) -> PrunePredicate {
    match filter {
        Filter::Compare { field, op, value } => match (field_column(field), int_value(value)) {
            (Some(col), Some(v)) if col == time_column => PrunePredicate::Compare {
                column: time_column.to_string(),
                op: map_op(*op),
                value: PruneValue::Int(v),
            },
            _ => PrunePredicate::Opaque,
        },
        Filter::Between { field, low, high } => {
            match (field_column(field), int_value(low), int_value(high)) {
                (Some(col), Some(lo), Some(hi)) if col == time_column => PrunePredicate::And(vec![
                    PrunePredicate::Compare {
                        column: time_column.to_string(),
                        op: PruneOp::GtEq,
                        value: PruneValue::Int(lo),
                    },
                    PrunePredicate::Compare {
                        column: time_column.to_string(),
                        op: PruneOp::LtEq,
                        value: PruneValue::Int(hi),
                    },
                ]),
                _ => PrunePredicate::Opaque,
            }
        }
        Filter::In { field, values } => match field_column(field) {
            Some(col) if col == time_column => {
                let lowered: Option<Vec<PruneValue>> = values
                    .iter()
                    .map(|v| int_value(v).map(PruneValue::Int))
                    .collect();
                match lowered {
                    Some(vs) if !vs.is_empty() => PrunePredicate::In {
                        column: time_column.to_string(),
                        values: vs,
                    },
                    // A non-integer member taints the set — keep all.
                    _ => PrunePredicate::Opaque,
                }
            }
            _ => PrunePredicate::Opaque,
        },
        Filter::And(a, b) => PrunePredicate::And(vec![
            lower_filter(a, time_column),
            lower_filter(b, time_column),
        ]),
        Filter::Or(a, b) => PrunePredicate::Or(vec![
            lower_filter(a, time_column),
            lower_filter(b, time_column),
        ]),
        // NOT / LIKE / IS NULL / field-to-field / opaque expressions all
        // stay conservative.
        _ => PrunePredicate::Opaque,
    }
}

/// Return the subset of `chunks` that may contain a row matching
/// `filter`'s temporal predicate.
///
/// * `filter == None` (no `WHERE`) → every chunk is kept.
/// * a `WHERE` that doesn't constrain the time column → every chunk is
///   kept (conservative).
/// * a temporal predicate → only chunks whose `[start_ns, end_ns)`
///   interval overlaps the predicate window survive.
///
/// Ordering mirrors the input `chunks` slice.
pub fn prune_hypertable_chunks(
    spec: &HypertableSpec,
    chunks: &[ChunkMeta],
    filter: Option<&Filter>,
) -> Vec<ChunkMeta> {
    let Some(filter) = filter else {
        return chunks.to_vec();
    };
    let predicate = lower_filter(filter, &spec.time_column);
    // No actionable temporal constraint → nothing to prune.
    if matches!(predicate, PrunePredicate::Opaque) {
        return chunks.to_vec();
    }

    let partitioning = PrunePartitioning {
        kind: PruneKind::Range,
        column: spec.time_column.clone(),
    };
    let children: Vec<RangeChild> = chunks
        .iter()
        .map(|c| RangeChild {
            name: chunk_name(c),
            low: Some(PruneValue::Int(c.id.start_ns as i64)),
            high_exclusive: Some(PruneValue::Int(c.end_ns_exclusive as i64)),
        })
        .collect();

    let kept: std::collections::HashSet<String> = prune_range(&partitioning, &children, &predicate)
        .into_iter()
        .collect();

    chunks
        .iter()
        .filter(|c| kept.contains(&chunk_name(c)))
        .cloned()
        .collect()
}

/// Smallest `[lo, hi)` nanosecond window that contains every kept
/// chunk's declared interval, or `None` when `kept` is empty.
///
/// A `None` return is the planner's signal that *no* chunk overlaps the
/// predicate, so the scan can be skipped entirely — there is provably no
/// matching row. A `Some((lo, hi))` window is a superset of every kept
/// chunk and therefore of every row the predicate admits, so a caller
/// may use it to bound the physical scan without dropping a match.
pub fn kept_scan_bounds(kept: &[ChunkMeta]) -> Option<(u64, u64)> {
    if kept.is_empty() {
        return None;
    }
    let mut lo = u64::MAX;
    let mut hi = 0u64;
    for c in kept {
        lo = lo.min(c.id.start_ns);
        hi = hi.max(c.end_ns_exclusive);
    }
    Some((lo, hi))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::timeseries::ChunkId;
    use proptest::prelude::*;

    /// Build a `ChunkMeta` covering `[start, end)`. Row stats are set to
    /// the interval bounds so the fixture is internally consistent.
    fn chunk(hypertable: &str, start: u64, end: u64) -> ChunkMeta {
        let mut meta = ChunkMeta::new(
            ChunkId {
                hypertable: hypertable.to_string(),
                start_ns: start,
            },
            end,
        );
        // Pretend a row lands at each boundary so min/max are sane.
        meta.observe(start);
        if end > start {
            meta.observe(end - 1);
        }
        meta
    }

    fn spec() -> HypertableSpec {
        HypertableSpec::new("metrics", "ts", 100)
    }

    fn ts_compare(op: CompareOp, v: i64) -> Filter {
        Filter::Compare {
            field: FieldRef::column("metrics", "ts"),
            op,
            value: Value::Integer(v),
        }
    }

    fn kept_starts(kept: &[ChunkMeta]) -> Vec<u64> {
        kept.iter().map(|c| c.id.start_ns).collect()
    }

    #[test]
    fn no_filter_keeps_every_chunk() {
        let chunks = vec![chunk("metrics", 0, 100), chunk("metrics", 100, 200)];
        let kept = prune_hypertable_chunks(&spec(), &chunks, None);
        assert_eq!(kept_starts(&kept), vec![0, 100]);
    }

    #[test]
    fn predicate_on_other_column_keeps_every_chunk() {
        let chunks = vec![chunk("metrics", 0, 100), chunk("metrics", 100, 200)];
        let filter = Filter::Compare {
            field: FieldRef::column("metrics", "host"),
            op: CompareOp::Eq,
            value: Value::Text("a".into()),
        };
        let kept = prune_hypertable_chunks(&spec(), &chunks, Some(&filter));
        assert_eq!(kept_starts(&kept), vec![0, 100]);
    }

    #[test]
    fn equality_keeps_only_the_owning_chunk() {
        let chunks = vec![
            chunk("metrics", 0, 100),
            chunk("metrics", 100, 200),
            chunk("metrics", 200, 300),
        ];
        let filter = ts_compare(CompareOp::Eq, 150);
        let kept = prune_hypertable_chunks(&spec(), &chunks, Some(&filter));
        assert_eq!(kept_starts(&kept), vec![100]);
    }

    #[test]
    fn between_keeps_overlapping_chunks_only() {
        let chunks = vec![
            chunk("metrics", 0, 100),
            chunk("metrics", 100, 200),
            chunk("metrics", 200, 300),
            chunk("metrics", 300, 400),
        ];
        let filter = Filter::Between {
            field: FieldRef::column("metrics", "ts"),
            low: Value::Integer(150),
            high: Value::Integer(250),
        };
        let kept = prune_hypertable_chunks(&spec(), &chunks, Some(&filter));
        assert_eq!(kept_starts(&kept), vec![100, 200]);
    }

    #[test]
    fn and_of_bounds_tightens_window() {
        let chunks = vec![
            chunk("metrics", 0, 100),
            chunk("metrics", 100, 200),
            chunk("metrics", 200, 300),
        ];
        let filter = Filter::And(
            Box::new(ts_compare(CompareOp::Ge, 120)),
            Box::new(ts_compare(CompareOp::Lt, 190)),
        );
        let kept = prune_hypertable_chunks(&spec(), &chunks, Some(&filter));
        assert_eq!(kept_starts(&kept), vec![100]);
    }

    #[test]
    fn disjoint_window_prunes_everything() {
        let chunks = vec![chunk("metrics", 0, 100), chunk("metrics", 100, 200)];
        let filter = ts_compare(CompareOp::Ge, 1_000);
        let kept = prune_hypertable_chunks(&spec(), &chunks, Some(&filter));
        assert!(kept.is_empty());
        assert_eq!(kept_scan_bounds(&kept), None);
    }

    #[test]
    fn scan_bounds_span_kept_chunks() {
        let kept = vec![chunk("metrics", 100, 200), chunk("metrics", 200, 300)];
        assert_eq!(kept_scan_bounds(&kept), Some((100, 300)));
    }

    // ---------------------------------------------------------------
    // Property: pruning is sound — it never drops a chunk that contains
    // a timestamp satisfying the predicate. Regardless of chunk layout
    // or predicate shape, every chunk holding a matching point survives.
    // ---------------------------------------------------------------

    /// Predicate shapes the property test exercises, with both an
    /// executable `Filter` and a reference SQL evaluator.
    #[derive(Debug, Clone)]
    enum Pred {
        Cmp(CompareOp, i64),
        Between(i64, i64),
        In(Vec<i64>),
        And(Box<Pred>, Box<Pred>),
        Or(Box<Pred>, Box<Pred>),
    }

    fn pred_to_filter(p: &Pred) -> Filter {
        match p {
            Pred::Cmp(op, v) => ts_compare(*op, *v),
            Pred::Between(lo, hi) => Filter::Between {
                field: FieldRef::column("metrics", "ts"),
                low: Value::Integer(*lo),
                high: Value::Integer(*hi),
            },
            Pred::In(vs) => Filter::In {
                field: FieldRef::column("metrics", "ts"),
                values: vs.iter().map(|v| Value::Integer(*v)).collect(),
            },
            Pred::And(a, b) => {
                Filter::And(Box::new(pred_to_filter(a)), Box::new(pred_to_filter(b)))
            }
            Pred::Or(a, b) => Filter::Or(Box::new(pred_to_filter(a)), Box::new(pred_to_filter(b))),
        }
    }

    /// Ground-truth SQL semantics for a single timestamp.
    fn eval(p: &Pred, ts: i64) -> bool {
        match p {
            Pred::Cmp(op, v) => match op {
                CompareOp::Eq => ts == *v,
                CompareOp::Ne => ts != *v,
                CompareOp::Lt => ts < *v,
                CompareOp::Le => ts <= *v,
                CompareOp::Gt => ts > *v,
                CompareOp::Ge => ts >= *v,
            },
            Pred::Between(lo, hi) => ts >= *lo && ts <= *hi,
            Pred::In(vs) => vs.contains(&ts),
            Pred::And(a, b) => eval(a, ts) && eval(b, ts),
            Pred::Or(a, b) => eval(a, ts) || eval(b, ts),
        }
    }

    fn leaf_pred() -> impl Strategy<Value = Pred> {
        prop_oneof![
            (
                prop_oneof![
                    Just(CompareOp::Eq),
                    Just(CompareOp::Ne),
                    Just(CompareOp::Lt),
                    Just(CompareOp::Le),
                    Just(CompareOp::Gt),
                    Just(CompareOp::Ge),
                ],
                0i64..60,
            )
                .prop_map(|(op, v)| Pred::Cmp(op, v)),
            (0i64..60, 0i64..60).prop_map(|(a, b)| Pred::Between(a.min(b), a.max(b))),
            prop::collection::vec(0i64..60, 1..4).prop_map(Pred::In),
        ]
    }

    fn pred_strategy() -> impl Strategy<Value = Pred> {
        leaf_pred().prop_recursive(3, 12, 2, |inner| {
            prop_oneof![
                (inner.clone(), inner.clone())
                    .prop_map(|(a, b)| Pred::And(Box::new(a), Box::new(b))),
                (inner.clone(), inner).prop_map(|(a, b)| Pred::Or(Box::new(a), Box::new(b))),
            ]
        })
    }

    /// A layout of small, contiguous chunks so every contained timestamp
    /// can be enumerated exhaustively in the soundness check.
    fn chunks_strategy() -> impl Strategy<Value = Vec<ChunkMeta>> {
        prop::collection::vec(1u64..8, 1..8).prop_map(|widths| {
            let mut out = Vec::with_capacity(widths.len());
            let mut start = 0u64;
            for w in widths {
                out.push(chunk("metrics", start, start + w));
                start += w;
            }
            out
        })
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(512))]

        #[test]
        fn pruning_never_drops_a_chunk_with_a_matching_point(
            chunks in chunks_strategy(),
            pred in pred_strategy(),
        ) {
            let filter = pred_to_filter(&pred);
            let kept = prune_hypertable_chunks(&spec(), &chunks, Some(&filter));
            let kept_keys: std::collections::HashSet<u64> =
                kept.iter().map(|c| c.id.start_ns).collect();

            for c in &chunks {
                // Enumerate every timestamp the chunk could hold.
                let contains_match = (c.id.start_ns..c.end_ns_exclusive)
                    .any(|ts| eval(&pred, ts as i64));
                if contains_match {
                    prop_assert!(
                        kept_keys.contains(&c.id.start_ns),
                        "dropped chunk [{}, {}) that contains a matching row for {:?}",
                        c.id.start_ns,
                        c.end_ns_exclusive,
                        pred,
                    );
                }
            }
        }
    }
}