nodedb 0.4.0

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// SPDX-License-Identifier: BUSL-1.1

//! Integration tests for timeseries query engine (GitHub issues #6-#9).
//!
//! Tests the full path: ILP ingest → memtable → flush → disk partitions →
//! query across both sources.

use nodedb_physical::physical_plan::{PhysicalPlan, TimeseriesOp};

use crate::helpers::*;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Build an ILP payload with `count` lines for a given collection.
///
/// Generates wide rows (~10 columns, ~80 bytes each) to trigger memtable
/// flushes faster. Each 64MB memtable holds ~840K wide rows.
fn ilp_lines(collection: &str, count: usize, start_ts_ns: i64) -> String {
    let mut lines = String::new();
    let qtypes = ["A", "AAAA", "MX", "CNAME"];
    let rcodes = ["NOERROR", "NXDOMAIN", "SERVFAIL", "REFUSED"];
    for i in 0..count {
        let ts_ns = start_ts_ns + i as i64 * 1_000_000; // 1ms apart
        let qtype = qtypes[i % qtypes.len()];
        let rcode = rcodes[i % rcodes.len()];
        let qname = format!("host-{}.example.com", i % 50);
        let client_ip = format!("10.0.{}.{}", (i / 256) % 256, i % 256);
        lines.push_str(&format!(
            "{collection},qtype={qtype},rcode={rcode},qname={qname},client_ip={client_ip} elapsed_ms={}.0 {ts_ns}\n",
            (i % 1000) as f64
        ));
    }
    lines
}

fn ingest_ilp(
    ctx: &mut crate::helpers::TestCtx,
    collection: &str,
    payload: &str,
) -> serde_json::Value {
    let raw = send_ok(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
            collection: collection.to_string(),
            payload: payload.as_bytes().to_vec(),
            format: "ilp".to_string(),
            wal_lsn: None,
            surrogates: Vec::new(),
            provenance: None,
        }),
    );
    let json_str = nodedb::data::executor::response_codec::decode_payload_to_json(&raw);
    serde_json::from_str(&json_str).unwrap_or(serde_json::Value::Null)
}

fn ts_scan(
    ctx: &mut crate::helpers::TestCtx,
    collection: &str,
    group_by: Vec<String>,
    aggregates: Vec<(String, String)>,
    bucket_interval_ms: i64,
) -> Vec<serde_json::Value> {
    let raw = send_ok(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Scan {
            collection: collection.to_string(),
            time_range: (0, i64::MAX),
            projection: Vec::new(),
            limit: usize::MAX,
            filters: Vec::new(),
            bucket_interval_ms,
            group_by,
            aggregates,
            gap_fill: String::new(),
            rls_filters: Vec::new(),
            system_time: nodedb_types::SystemTimeScope::Current,
            valid_at_ms: None,
            computed_columns: Vec::new(),
        }),
    );
    // Payload may be MessagePack (grouped aggregates) or JSON — decode both.
    let json_str = nodedb::data::executor::response_codec::decode_payload_to_json(&raw);
    serde_json::from_str(&json_str).unwrap_or_default()
}

fn ts_scan_filtered(
    ctx: &mut crate::helpers::TestCtx,
    collection: &str,
    group_by: Vec<String>,
    aggregates: Vec<(String, String)>,
    bucket_interval_ms: i64,
    filters: Vec<nodedb::bridge::scan_filter::ScanFilter>,
) -> Vec<serde_json::Value> {
    let filter_bytes = if filters.is_empty() {
        Vec::new()
    } else {
        zerompk::to_msgpack_vec(&filters).unwrap_or_default()
    };
    let raw = send_ok(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Scan {
            collection: collection.to_string(),
            time_range: (0, i64::MAX),
            projection: Vec::new(),
            limit: usize::MAX,
            filters: filter_bytes,
            bucket_interval_ms,
            group_by,
            aggregates,
            gap_fill: String::new(),
            rls_filters: Vec::new(),
            system_time: nodedb_types::SystemTimeScope::Current,
            valid_at_ms: None,
            computed_columns: Vec::new(),
        }),
    );
    let json_str = nodedb::data::executor::response_codec::decode_payload_to_json(&raw);
    serde_json::from_str(&json_str).unwrap_or_default()
}

// ---------------------------------------------------------------------------
// #6 — Query reads all data (memtable + disk partitions)
// ---------------------------------------------------------------------------

#[test]
fn count_star_sees_flushed_partitions() {
    let mut ctx = make_ctx();

    // Ingest enough wide rows to force multiple memtable flushes.
    // Each row is ~7 columns × ~8 bytes = ~56 bytes of column data.
    // 64MB / 56 = ~1.2M rows per flush. We send 3M rows to guarantee
    // at least 2 flush cycles. Each batch is 10K rows.
    let batch_size = 10_000;
    let num_batches = 300;
    let mut total_accepted: u64 = 0;
    let mut total_rejected: u64 = 0;

    for b in 0..num_batches {
        let start_ns = (b * batch_size) as i64 * 1_000_000;
        let payload = ilp_lines("metrics", batch_size, start_ns);
        let resp = ingest_ilp(&mut ctx, "metrics", &payload);
        total_accepted += resp["accepted"].as_u64().unwrap_or(0);
        total_rejected += resp["rejected"].as_u64().unwrap_or(0);
    }

    let total_sent = (batch_size * num_batches) as u64;
    assert_eq!(
        total_rejected, 0,
        "no rows should be rejected — pre-flush must prevent hard-limit rejections"
    );
    assert_eq!(
        total_accepted, total_sent,
        "all sent rows should be accepted by the Data Plane"
    );

    // COUNT(*) must equal total accepted (memtable + ALL flushed partitions).
    // This catches: partition registry not populated, flush losing drain data,
    // or query path not reading from all sources.
    let results = ts_scan(
        &mut ctx,
        "metrics",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(results.len(), 1, "expected single aggregate row");
    let count = results[0]["count(*)"].as_u64().unwrap_or(0);
    assert_eq!(
        count, total_accepted,
        "COUNT(*) must see every accepted row — found {count}, expected {total_accepted}. \
         If count < accepted, rows were lost during flush or partitions are invisible."
    );
}

#[test]
fn group_by_sees_both_memtable_and_partitions() {
    let mut ctx = make_ctx();

    // Ingest two batches — enough for at least one flush.
    for b in 0..10 {
        let start_ns = b * 5_000 * 1_000_000_000i64;
        let payload = ilp_lines("dns", 5_000, start_ns);
        ingest_ilp(&mut ctx, "dns", &payload);
    }

    // GROUP BY qtype should see all data.
    let results = ts_scan(
        &mut ctx,
        "dns",
        vec!["qtype".into()],
        vec![("count".into(), "*".into())],
        0,
    );
    let total: u64 = results
        .iter()
        .map(|r| r["count(*)"].as_u64().unwrap_or(0))
        .sum();
    assert_eq!(total, 50_000, "GROUP BY total should match ingested rows");
    assert_eq!(results.len(), 4, "should have 4 qtypes: A, AAAA, MX, CNAME");
}

// ---------------------------------------------------------------------------
// #7 — time_bucket() works without panic
// ---------------------------------------------------------------------------

#[test]
fn time_bucket_aggregation_does_not_panic() {
    let mut ctx = make_ctx();

    // Ingest data spanning multiple hours.
    let payload = ilp_lines("ts_bucket", 1_000, 1_700_000_000_000_000_000);
    ingest_ilp(&mut ctx, "ts_bucket", &payload);

    // time_bucket('1h') should return valid buckets — not panic or timeout.
    let results = ts_scan(
        &mut ctx,
        "ts_bucket",
        Vec::new(),
        vec![("count".into(), "*".into())],
        3_600_000, // 1 hour
    );
    assert!(
        !results.is_empty(),
        "time_bucket should return at least 1 bucket"
    );
    let total: u64 = results
        .iter()
        .map(|r| r["count(*)"].as_u64().unwrap_or(0))
        .sum();
    assert_eq!(total, 1_000, "bucket counts should sum to ingested rows");
}

#[test]
fn time_bucket_with_named_value_column() {
    let mut ctx = make_ctx();

    let payload = ilp_lines("ts_named", 500, 1_700_000_000_000_000_000);
    ingest_ilp(&mut ctx, "ts_named", &payload);

    // AVG(elapsed_ms) grouped by time bucket — uses named column, not hardcoded index.
    let results = ts_scan(
        &mut ctx,
        "ts_named",
        Vec::new(),
        vec![
            ("count".into(), "*".into()),
            ("avg".into(), "elapsed_ms".into()),
        ],
        3_600_000,
    );
    assert!(!results.is_empty());
    for r in &results {
        let avg = r["avg(elapsed_ms)"].as_f64();
        assert!(avg.is_some(), "avg should be present: {r}");
    }
}

// ---------------------------------------------------------------------------
// #6 (cont.) — WHERE predicates filter aggregate results
// ---------------------------------------------------------------------------

#[test]
fn where_predicate_filters_count() {
    let mut ctx = make_ctx();

    let payload = ilp_lines("dns_filt", 1_000, 1_700_000_000_000_000_000);
    ingest_ilp(&mut ctx, "dns_filt", &payload);

    // Unfiltered COUNT(*) = 1000.
    let all = ts_scan(
        &mut ctx,
        "dns_filt",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    let total = all[0]["count(*)"].as_u64().unwrap();
    assert_eq!(total, 1_000);

    // Filtered: qtype = 'A' → 25% of rows (4 qtypes, round-robin).
    let filtered = ts_scan_filtered(
        &mut ctx,
        "dns_filt",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
        vec![nodedb::bridge::scan_filter::ScanFilter {
            field: "qtype".into(),
            op: "eq".into(),
            value: nodedb_types::Value::String("A".into()),
            clauses: vec![],
            expr: None,
        }],
    );
    let filtered_count = filtered[0]["count(*)"].as_u64().unwrap();
    assert_eq!(filtered_count, 250, "WHERE qtype='A' should return 25%");
}

// ---------------------------------------------------------------------------
// #8 — Schema evolution (new ILP fields)
// ---------------------------------------------------------------------------

#[test]
fn schema_evolution_adds_new_fields() {
    let mut ctx = make_ctx();

    // First batch: only elapsed_ms.
    let batch1 = "dns,qtype=A elapsed_ms=1.5 1700000000000000000\n\
                   dns,qtype=AAAA elapsed_ms=2.5 1700000001000000000\n";
    ingest_ilp(&mut ctx, "dns", batch1);

    // Second batch: adds response_bytes and ttl (new fields).
    let batch2 = "dns,qtype=A elapsed_ms=3.0,response_bytes=512i,ttl=3600i 1700000002000000000\n\
                   dns,qtype=MX elapsed_ms=4.0,response_bytes=256i,ttl=7200i 1700000003000000000\n";
    ingest_ilp(&mut ctx, "dns", batch2);

    // Raw scan should show new columns with values for batch2 rows.
    let results = ts_scan(
        &mut ctx,
        "dns",
        Vec::new(),
        Vec::new(), // raw scan
        0,
    );
    assert_eq!(results.len(), 4, "should see all 4 rows");

    // Batch2 rows (last 2) should have response_bytes.
    let has_response_bytes: Vec<_> = results
        .iter()
        .filter(|r| {
            r.get("response_bytes")
                .is_some_and(|v| !v.is_null() && v.as_i64().unwrap_or(0) > 0)
        })
        .collect();
    assert_eq!(
        has_response_bytes.len(),
        2,
        "2 rows should have response_bytes set"
    );

    // Batch1 rows should have response_bytes=0 (int64 default) or null.
    let batch1_rows: Vec<_> = results
        .iter()
        .filter(|r| {
            r.get("response_bytes")
                .is_none_or(|v| v.is_null() || v.as_i64() == Some(0))
        })
        .collect();
    assert_eq!(
        batch1_rows.len(),
        2,
        "batch1 rows should have null/0 for new fields"
    );
}

// ---------------------------------------------------------------------------
// #9 — GROUP BY not capped at 10K
// ---------------------------------------------------------------------------

#[test]
fn group_by_not_capped_at_10k() {
    let mut ctx = make_ctx();

    // Ingest data with high cardinality: 15K unique qnames.
    let mut lines = String::new();
    for i in 0..15_000 {
        let ts_ns = 1_700_000_000_000_000_000i64 + i as i64 * 1_000_000;
        lines.push_str(&format!(
            "dns,qname=host-{i}.example.com elapsed_ms=1.0 {ts_ns}\n"
        ));
    }
    ingest_ilp(&mut ctx, "dns_card", &lines);

    // GROUP BY qname should return all 15K groups — not capped at 10K.
    let results = ts_scan(
        &mut ctx,
        "dns_card",
        vec!["qname".into()],
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(
        results.len(),
        15_000,
        "GROUP BY should return all unique groups, not capped at 10K"
    );
}

// ---------------------------------------------------------------------------
// LSN-based deduplication for WAL catch-up
// ---------------------------------------------------------------------------

#[test]
fn dedup_only_skips_flushed_partitions() {
    let mut ctx = make_ctx();
    let payload = b"dns,qname=a.com elapsed_ms=1.0 1700000000000000000\n";

    // Ingest with wal_lsn — accepted (no flushed partitions yet).
    let resp1 = send_raw(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
            collection: "dns_dedup".to_string(),
            payload: payload.to_vec(),
            format: "ilp".to_string(),
            wal_lsn: Some(100),
            surrogates: Vec::new(),
            provenance: None,
        }),
    );
    let v1: serde_json::Value = serde_json::from_str(
        &nodedb::data::executor::response_codec::decode_payload_to_json(&resp1.payload),
    )
    .unwrap();
    assert_eq!(v1["accepted"], 1);

    // Same LSN again — accepted (no flushed partition to dedup against).
    // In-memory dedup is intentionally NOT done because SPSC gaps mean
    // max-LSN watermarks produce false negatives.
    let resp2 = send_raw(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
            collection: "dns_dedup".to_string(),
            payload: payload.to_vec(),
            format: "ilp".to_string(),
            wal_lsn: Some(100),
            surrogates: Vec::new(),
            provenance: None,
        }),
    );
    let v2: serde_json::Value = serde_json::from_str(
        &nodedb::data::executor::response_codec::decode_payload_to_json(&resp2.payload),
    )
    .unwrap();
    assert_eq!(
        v2["accepted"], 1,
        "same LSN re-ingest must be accepted (no flushed partition to dedup against)"
    );

    // Live ingest (wal_lsn=None) — always accepted.
    let resp3 = send_raw(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
            collection: "dns_dedup".to_string(),
            payload: payload.to_vec(),
            format: "ilp".to_string(),
            wal_lsn: None,
            surrogates: Vec::new(),
            provenance: None,
        }),
    );
    let v3: serde_json::Value = serde_json::from_str(
        &nodedb::data::executor::response_codec::decode_payload_to_json(&resp3.payload),
    )
    .unwrap();
    assert_eq!(v3["accepted"], 1);
}

// ---------------------------------------------------------------------------
// Catch-up must not skip gaps in LSN coverage
// ---------------------------------------------------------------------------

/// Simulates the real-world failure: ILP ingests batches 1,2,3,4,5 to WAL,
/// but SPSC drops batches 2 and 4 (never reach Data Plane). Batch 1,3,5
/// are ingested with their WAL LSNs. Later, catch-up replays batch 2 and 4
/// from WAL. The dedup must NOT skip them just because LSN 2 < max(5).
#[test]
fn catchup_replays_gaps_in_lsn_coverage() {
    let mut ctx = make_ctx();

    let mk_payload = |i: i64| -> Vec<u8> {
        format!(
            "dns,qname=host-{i}.test elapsed_ms=1.0 {}\n",
            1700000000000000000 + i * 1000000
        )
        .into_bytes()
    };

    // Simulate live ingest: batches at LSN 1, 3, 5 reach Data Plane.
    // (Batches at LSN 2, 4 were dropped by SPSC — they're in WAL only.)
    for lsn in [1, 3, 5] {
        send_raw(
            &mut ctx.core,
            &mut ctx.tx,
            &mut ctx.rx,
            PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
                collection: "dns_gap".to_string(),
                payload: mk_payload(lsn),
                format: "ilp".to_string(),
                wal_lsn: Some(lsn as u64),
                surrogates: Vec::new(),
                provenance: None,
            }),
        );
    }

    // Verify: 3 rows visible.
    let results = ts_scan(
        &mut ctx,
        "dns_gap",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(
        results[0]["count(*)"], 3,
        "should see 3 rows from live ingest"
    );

    // Now simulate catch-up replaying the DROPPED batches (LSN 2, 4).
    // These must NOT be skipped by dedup even though LSN 2 < max(5).
    for lsn in [2, 4] {
        let resp = send_raw(
            &mut ctx.core,
            &mut ctx.tx,
            &mut ctx.rx,
            PhysicalPlan::Timeseries(TimeseriesOp::Ingest {
                collection: "dns_gap".to_string(),
                payload: mk_payload(lsn),
                format: "ilp".to_string(),
                wal_lsn: Some(lsn as u64),
                surrogates: Vec::new(),
                provenance: None,
            }),
        );
        let v: serde_json::Value = serde_json::from_str(
            &nodedb::data::executor::response_codec::decode_payload_to_json(&resp.payload),
        )
        .unwrap();
        assert_eq!(
            v["accepted"], 1,
            "catch-up batch at LSN {lsn} must be accepted (not deduped)"
        );
    }

    // Verify: all 5 rows visible.
    let results = ts_scan(
        &mut ctx,
        "dns_gap",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(
        results[0]["count(*)"], 5,
        "all 5 rows must be visible after catch-up fills the gaps"
    );
}

/// Multi-batch ingest exceeding memtable capacity: all rows must survive
/// across memtable flushes. Simulates sustained ILP ingest where the
/// server receives many small batches, not one giant batch.
#[test]
fn multi_batch_ingest_survives_memtable_flush() {
    let mut ctx = make_ctx();

    // Ingest in 10 batches of 100K rows each = 1M total.
    // Each batch is ~10MB of wide ILP rows. After ~6 batches the memtable
    // hits 64MB and flushes. All 1M rows must be visible at the end.
    let batch_size = 100_000;
    let num_batches = 10;
    let total = batch_size * num_batches;

    for batch in 0..num_batches {
        let start = batch * batch_size;
        let lines = ilp_lines(
            "dns_multi",
            batch_size,
            1_700_000_000_000_000_000 + start as i64 * 1_000_000,
        );
        let result = ingest_ilp(&mut ctx, "dns_multi", &lines);
        let accepted = result["accepted"].as_u64().unwrap_or(0);
        assert!(
            accepted > 0,
            "batch {batch} must accept rows, got accepted={accepted}"
        );
    }

    // COUNT must see all rows (memtable + sealed partitions).
    let results = ts_scan(
        &mut ctx,
        "dns_multi",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    let count = results[0]["count(*)"].as_u64().unwrap();
    assert_eq!(
        count, total as u64,
        "all {total} rows must be visible across memtable + partitions"
    );
}

// ---------------------------------------------------------------------------
// Idle memtable flush via maybe_run_maintenance
// ---------------------------------------------------------------------------

#[test]
fn idle_flush_triggers_after_inactivity() {
    let mut ctx = make_ctx();

    // Ingest 5 small rows (well under 64MB threshold).
    let mut lines = String::new();
    for i in 0..5 {
        let ts_ns = 1_700_000_000_000_000_000i64 + i * 1_000_000;
        lines.push_str(&format!("dns,qname=idle-{i}.test elapsed_ms=1.0 {ts_ns}\n"));
    }
    ingest_ilp(&mut ctx, "dns_idle", &lines);

    // COUNT(*) should see all 5 rows (from memtable).
    let results = ts_scan(
        &mut ctx,
        "dns_idle",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(results[0]["count(*)"], 5);

    // Simulate idle time: set last_ts_ingest to 10 seconds ago.
    ctx.core.set_last_ts_ingest(Some(
        std::time::Instant::now() - std::time::Duration::from_secs(10),
    ));

    // Run maintenance — should trigger idle flush.
    ctx.core.maybe_run_maintenance();

    // COUNT(*) should still see all 5 rows (now from sealed partition).
    let results = ts_scan(
        &mut ctx,
        "dns_idle",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(results[0]["count(*)"], 5);

    // Verify memtable was flushed by ingesting more and checking partition count.
    // After idle flush, the partition registry should have at least one entry.
    // We test indirectly: ingest 3 more rows and count — should get 8 total.
    let mut lines2 = String::new();
    for i in 5..8 {
        let ts_ns = 1_700_000_000_000_000_000i64 + i * 1_000_000;
        lines2.push_str(&format!("dns,qname=idle-{i}.test elapsed_ms=1.0 {ts_ns}\n"));
    }
    ingest_ilp(&mut ctx, "dns_idle", &lines2);

    let results = ts_scan(
        &mut ctx,
        "dns_idle",
        Vec::new(),
        vec![("count".into(), "*".into())],
        0,
    );
    assert_eq!(
        results[0]["count(*)"], 8,
        "should see all 8 rows (5 from partition + 3 from memtable)"
    );
}

// ---------------------------------------------------------------------------
// Large GROUP BY produces valid single-payload response at Data Plane
// ---------------------------------------------------------------------------

#[test]
fn large_group_by_returns_single_valid_json() {
    let mut ctx = make_ctx();

    // Ingest 2000 unique groups (exceeds default stream_chunk_size of 1000).
    let mut lines = String::new();
    for i in 0..2_000 {
        let ts_ns = 1_700_000_000_000_000_000i64 + i as i64 * 1_000_000;
        lines.push_str(&format!(
            "dns,qname=host-{i}.example.com elapsed_ms=1.0 {ts_ns}\n"
        ));
    }
    ingest_ilp(&mut ctx, "dns_large", &lines);

    // GROUP BY should return a single valid JSON response with all 2000 groups.
    let raw = send_ok(
        &mut ctx.core,
        &mut ctx.tx,
        &mut ctx.rx,
        PhysicalPlan::Timeseries(TimeseriesOp::Scan {
            collection: "dns_large".to_string(),
            time_range: (0, i64::MAX),
            projection: Vec::new(),
            limit: usize::MAX,
            filters: Vec::new(),
            bucket_interval_ms: 0,
            group_by: vec!["qname".into()],
            aggregates: vec![("count".into(), "*".into())],
            gap_fill: String::new(),
            rls_filters: Vec::new(),
            system_time: nodedb_types::SystemTimeScope::Current,
            valid_at_ms: None,
            computed_columns: Vec::new(),
        }),
    );

    // The response should be a single valid JSON array.
    let json_str = nodedb::data::executor::response_codec::decode_payload_to_json(&raw);
    let results: Vec<serde_json::Value> = serde_json::from_str(&json_str)
        .expect("Data Plane response should be a single valid JSON array");
    assert_eq!(results.len(), 2_000);
}

// ---------------------------------------------------------------------------
// Tag-cardinality exhaustion must not duplicate or drop lines
// ---------------------------------------------------------------------------

/// Distinct tag values one memtable generation may hold in this test.
///
/// Small enough to exhaust with four lines. The production ceiling is 100k,
/// which is why this path went untested: filling it costs 100k distinct tag
/// values per run. `set_timeseries_tuning` is the same knob the server exposes
/// as `NODEDB_TS_MAX_TAG_CARDINALITY`.
const TEST_TAG_CARDINALITY: u32 = 4;

fn tag_line(collection: &str, host: &str, ts_ns: i64) -> String {
    format!("{collection},host={host} value=1.0 {ts_ns}\n")
}

/// Count the rows the scan returns per `host`, across memtable AND partitions.
fn counts_by_host(ctx: &mut crate::helpers::TestCtx, collection: &str) -> Vec<(String, u64)> {
    let mut rows: Vec<(String, u64)> = ts_scan(
        ctx,
        collection,
        vec!["host".into()],
        vec![("count".into(), "*".into())],
        0,
    )
    .iter()
    .map(|r| {
        (
            r["host"].as_str().unwrap_or_default().to_string(),
            r["count(*)"].as_u64().unwrap_or(0),
        )
    })
    .collect();
    rows.sort();
    rows
}

/// A batch that exhausts the tag dictionary partway through must be ingested
/// exactly once — every line present, no line twice.
///
/// This is the interleaving case, and it needs no crash to observe.
/// `SymbolDictionary::resolve` fails only for values that are NOT already in
/// the dictionary, so once the dictionary is full a batch alternating new and
/// existing tag values fails and succeeds ALTERNATELY — the failures are not a
/// suffix of the batch.
///
/// The ingest path used to react to any rejection by flushing and re-ingesting
/// `lines[accepted..]`, a slice that is only the untouched remainder if the
/// rejections WERE a suffix. Here `accepted` is 2 while 4 lines have been
/// consumed, so the retry re-ingested lines already in the memtable — and
/// because the flush it had just done resets the dictionaries, the retry
/// succeeded and duplicated them on the spot.
///
/// COUNT(*) alone cannot see this: the duplicated lines and the dropped ones
/// cancel out. The per-host counts are what make it visible.
#[test]
fn interleaved_tag_cardinality_batch_ingests_each_line_exactly_once() {
    let mut ctx = make_ctx();
    ctx.core
        .set_timeseries_tuning(nodedb_types::config::tuning::TimeseriesToning {
            max_tag_cardinality: TEST_TAG_CARDINALITY,
            ..Default::default()
        });

    // Fill the dictionary exactly: 4 lines, 4 distinct hosts.
    let mut first = String::new();
    for i in 0..TEST_TAG_CARDINALITY {
        first.push_str(&tag_line(
            "card",
            &format!("h{i}"),
            1_000_000 * (i as i64 + 1),
        ));
    }
    let resp = ingest_ilp(&mut ctx, "card", &first);
    assert_eq!(resp["accepted"].as_u64(), Some(4), "setup batch: {resp}");
    assert_eq!(resp["rejected"].as_u64(), Some(0), "setup batch: {resp}");

    // The interleaving batch: new host, existing host, new host, existing host.
    // Against a full dictionary, lines 0 and 2 fail and lines 1 and 3 succeed.
    // Its four lines carry only four distinct hosts, so once the gate flushes
    // (resetting the dictionaries) it fits whole.
    let second = [
        tag_line("card", "h4", 5_000_000),
        tag_line("card", "h0", 6_000_000),
        tag_line("card", "h5", 7_000_000),
        tag_line("card", "h1", 8_000_000),
    ]
    .concat();
    let resp = ingest_ilp(&mut ctx, "card", &second);
    assert_eq!(
        resp["rejected"].as_u64(),
        Some(0),
        "the gate must flush BEFORE this batch so the fresh dictionaries take \
         all four of its hosts: {resp}"
    );
    assert_eq!(resp["accepted"].as_u64(), Some(4), "{resp}");

    // Eight lines were sent: h0,h1,h2,h3 then h4,h0,h5,h1. Each must appear
    // exactly the number of times it was sent.
    assert_eq!(
        counts_by_host(&mut ctx, "card"),
        vec![
            ("h0".to_string(), 2),
            ("h1".to_string(), 2),
            ("h2".to_string(), 1),
            ("h3".to_string(), 1),
            ("h4".to_string(), 1),
            ("h5".to_string(), 1),
        ],
        "every sent line must appear exactly once. h1 appearing 3 times means \
         the mid-batch flush-and-retry re-ingested an already-accepted line; \
         h4 missing means its line was dropped outright."
    );
}