infino 0.1.0

A fast retrieval engine that stores data on object storage and runs SQL, full-text search, and vector search over it from a single system — search-on-Parquet.
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! Partition assignment.
//!
//! Given a `SuperfileEntry`'s per-column min/max summaries +
//! the supertable's configured `PartitionStrategy`, decide
//! which partition the superfile belongs to. Drives the
//! writer's "rewrite latest part" policy: superfiles in the
//! same partition share a `ManifestPart`; superfiles in
//! different partitions go into separate parts so a
//! single-partition commit rewrites exactly one part.

use std::time::{SystemTime, UNIX_EPOCH};

use crate::supertable::{
    error::{CommitError, ManifestError},
    manifest::{SuperfileEntry, list::PartitionStrategy},
};

/// Opaque partition identifier. Encoded into
/// `SuperfileEntry.partition_key` + `ManifestPartEntry.partition_key`
/// for the manifest layer; the writer uses this typed shape
/// in-memory to group superfiles before encoding.
///
/// The on-disk encoding (LE u64 / u32 / u16) is the
/// responsibility of [`encode_partition_key`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PartitionKey {
    /// `time_range` bucket = `value / granularity_secs`.
    TimeRange(u64),
    /// `hash` bucket = `hash(column_value) % n_buckets`.
    /// For `n_buckets == 1` the writer short-circuits to
    /// `Hash(0)` without requiring the `partition_hint`
    /// field — see [`assign_partition`].
    Hash(u32),
    /// `column_range` bucket = boundary index.
    ColumnRange(u16),
}

/// Encode a `PartitionKey` to its on-disk bytes — the shape
/// `SuperfileEntry.partition_key` and `ManifestPartEntry.partition_key`
/// carry: 8-byte LE u64 for TimeRange, 4-byte LE u32 for
/// Hash, 2-byte LE u16 for ColumnRange.
pub fn encode_partition_key(key: &PartitionKey) -> Vec<u8> {
    match key {
        PartitionKey::TimeRange(b) => b.to_le_bytes().to_vec(),
        PartitionKey::Hash(b) => b.to_le_bytes().to_vec(),
        PartitionKey::ColumnRange(b) => b.to_le_bytes().to_vec(),
    }
}

/// Decode a `partition_key: Vec<u8>` back to its typed
/// shape, given the strategy. Used by the writer when
/// reading existing list entries to group surviving parts
/// by partition.
pub fn decode_partition_key(
    bytes: &[u8],
    strategy: &PartitionStrategy,
) -> Result<PartitionKey, CommitError> {
    match strategy {
        PartitionStrategy::TimeRange { .. } => {
            let arr: [u8; 8] = bytes.try_into().map_err(|_| {
                CommitError::PointerParse(format!(
                    "TimeRange partition_key must be 8 bytes; got {}",
                    bytes.len()
                ))
            })?;
            Ok(PartitionKey::TimeRange(u64::from_le_bytes(arr)))
        }
        PartitionStrategy::Hash { .. } => {
            let arr: [u8; 4] = bytes.try_into().map_err(|_| {
                CommitError::PointerParse(format!(
                    "Hash partition_key must be 4 bytes; got {}",
                    bytes.len()
                ))
            })?;
            Ok(PartitionKey::Hash(u32::from_le_bytes(arr)))
        }
        PartitionStrategy::ColumnRange { .. } => {
            let arr: [u8; 2] = bytes.try_into().map_err(|_| {
                CommitError::PointerParse(format!(
                    "ColumnRange partition_key must be 2 bytes; got {}",
                    bytes.len()
                ))
            })?;
            Ok(PartitionKey::ColumnRange(u16::from_le_bytes(arr)))
        }
        PartitionStrategy::IngestionTime { .. } => {
            let arr: [u8; 8] = bytes.try_into().map_err(|_| {
                CommitError::PointerParse(format!(
                    "IngestionTime partition_key must be 8 bytes; got {}",
                    bytes.len()
                ))
            })?;
            Ok(PartitionKey::TimeRange(u64::from_le_bytes(arr)))
        }
    }
}

/// Decide which partition `seg` belongs to under `strategy`.
///
/// - **TimeRange**: superfile's `(min, max)` on the partition
///   column must fall within a single bucket
///   (`min / granularity_secs == max / granularity_secs`).
///   Spans → `SuperfileSpansPartition`.
/// - **Hash**: requires `seg.partition_hint = Some(bucket)`
///   from the writer's pre-shard step — except the
///   `n_buckets == 1` special case (every row hashes to
///   bucket 0; pre-shard is trivial). Without the hint,
///   surfaces `SuperfileSpansPartition` with a "hash strategy
///   requires pre-sharded superfiles" message.
/// - **ColumnRange**: superfile's `(min, max)` must fall within
///   one boundary interval. Spans → `SuperfileSpansPartition`.
///
/// The `n_buckets == 1` Hash short-circuit is critical for
/// backward compatibility: the default partition
/// strategy (when nothing's configured) is
/// `Hash { id_column, n_buckets: 1 }`, and the existing
/// writer path doesn't yet pre-shard — so the hint is None.
/// Special-casing 1-bucket lets every existing test keep
/// running on the new partition-aware writer without
/// any test changes.
pub fn assign_partition(
    seg: &SuperfileEntry,
    strategy: &PartitionStrategy,
) -> Result<PartitionKey, ManifestError> {
    match strategy {
        PartitionStrategy::TimeRange {
            column,
            granularity_secs,
        } => {
            if *granularity_secs <= 0 {
                return Err(ManifestError::SuperfileSpansPartition {
                    detail: format!(
                        "TimeRange granularity_secs must be > 0; got {granularity_secs}"
                    ),
                });
            }
            let (min, max) = scalar_i64_minmax(seg, column)?;
            let g = *granularity_secs;
            let min_bucket = min.div_euclid(g);
            let max_bucket = max.div_euclid(g);
            if min_bucket != max_bucket {
                return Err(ManifestError::SuperfileSpansPartition {
                    detail: format!(
                        "superfile {} column {column:?} [{min}, {max}] spans buckets \
                         {min_bucket}..={max_bucket}; reduce commit_threshold_size_mb \
                         or flush at granularity boundaries",
                        seg.uri.0
                    ),
                });
            }
            Ok(PartitionKey::TimeRange(min_bucket as u64))
        }

        PartitionStrategy::Hash {
            column: _,
            n_buckets,
        } => {
            // Single-bucket short-circuit: every row hashes
            // to bucket 0 trivially. No pre-shard required.
            if *n_buckets <= 1 {
                return Ok(PartitionKey::Hash(0));
            }
            // Multi-bucket: writer must have stamped
            // partition_hint at pre-shard time.
            let bucket =
                seg.partition_hint
                    .ok_or_else(|| ManifestError::SuperfileSpansPartition {
                        detail: format!(
                            "Hash{{n_buckets:{n_buckets}}} strategy requires pre-sharded \
                         superfiles; SuperfileEntry.partition_hint must be Some(bucket) \
                         (superfile {})",
                            seg.uri.0
                        ),
                    })?;
            if bucket >= *n_buckets {
                return Err(ManifestError::SuperfileSpansPartition {
                    detail: format!(
                        "Hash{{n_buckets:{n_buckets}}} got partition_hint={bucket} \
                         (out of range)"
                    ),
                });
            }
            Ok(PartitionKey::Hash(bucket))
        }

        PartitionStrategy::ColumnRange {
            column: _,
            boundaries: _,
        } => Err(ManifestError::SuperfileSpansPartition {
            detail: "ColumnRange partition assignment lands in a follow-up; \
                     no writer currently emits ColumnRange-partitioned commits"
                .into(),
        }),

        PartitionStrategy::IngestionTime { granularity_secs } => {
            if *granularity_secs <= 0 {
                return Err(ManifestError::SuperfileSpansPartition {
                    detail: format!(
                        "IngestionTime granularity_secs must be > 0; got {granularity_secs}"
                    ),
                });
            }
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map_err(|_| ManifestError::SuperfileSpansPartition {
                    detail: "failed to get current system time".into(),
                })?
                .as_secs() as i64;
            let g = *granularity_secs;
            let bucket = now.div_euclid(g);
            Ok(PartitionKey::TimeRange(bucket as u64))
        }
    }
}

/// Extract the superfile's `(min, max)` for `column` as `i64`.
/// `scalar_stats[column]` carries Arrow length-1
/// `ArrayRef`s; this helper downcasts against the column's
/// actual Arrow type and returns the value at index 0.
///
/// Supported types: `Int64` (epoch seconds-style integer
/// columns) and the three timestamp widths
/// (`TimestampSecond` / `TimestampMillisecond` /
/// `TimestampMicrosecond` / `TimestampNanosecond`). All
/// timestamp values downcast to i64 directly; the
/// granularity-bucket math in `assign_partition` treats
/// them as opaque i64 — callers configuring
/// `granularity_secs` are responsible for matching it to
/// the column's actual unit (seconds for `Int64`,
/// microseconds for `TimestampMicrosecond`, etc.).
fn scalar_i64_minmax(seg: &SuperfileEntry, column: &str) -> Result<(i64, i64), ManifestError> {
    let agg =
        seg.scalar_stats
            .get(column)
            .ok_or_else(|| ManifestError::SuperfileSpansPartition {
                detail: format!(
                    "TimeRange strategy: superfile {} has no scalar_stats \
                     for column {column:?}",
                    seg.uri.0
                ),
            })?;
    let min = downcast_i64(agg.min.as_ref(), column, seg)?;
    let max = downcast_i64(agg.max.as_ref(), column, seg)?;
    Ok((min, max))
}

fn downcast_i64(
    arr: &dyn arrow_array::Array,
    column: &str,
    seg: &SuperfileEntry,
) -> Result<i64, ManifestError> {
    use arrow_array::*;
    use arrow_schema::DataType;
    if arr.is_empty() || arr.is_null(0) {
        return Err(ManifestError::SuperfileSpansPartition {
            detail: format!(
                "TimeRange strategy: superfile {} column {column:?} stats array \
                 is empty or null at index 0",
                seg.uri.0
            ),
        });
    }
    let v = match arr.data_type() {
        DataType::Int64 => arr
            .as_any()
            .downcast_ref::<Int64Array>()
            .map(|a| a.value(0)),
        DataType::Timestamp(arrow_schema::TimeUnit::Second, _) => arr
            .as_any()
            .downcast_ref::<TimestampSecondArray>()
            .map(|a| a.value(0)),
        DataType::Timestamp(arrow_schema::TimeUnit::Millisecond, _) => arr
            .as_any()
            .downcast_ref::<TimestampMillisecondArray>()
            .map(|a| a.value(0)),
        DataType::Timestamp(arrow_schema::TimeUnit::Microsecond, _) => arr
            .as_any()
            .downcast_ref::<TimestampMicrosecondArray>()
            .map(|a| a.value(0)),
        DataType::Timestamp(arrow_schema::TimeUnit::Nanosecond, _) => arr
            .as_any()
            .downcast_ref::<TimestampNanosecondArray>()
            .map(|a| a.value(0)),
        other => {
            return Err(ManifestError::SuperfileSpansPartition {
                detail: format!(
                    "TimeRange strategy: superfile {} column {column:?} has \
                     unsupported type {other:?}; expected Int64 or Timestamp*",
                    seg.uri.0
                ),
            });
        }
    };
    v.ok_or_else(|| ManifestError::SuperfileSpansPartition {
        detail: format!(
            "TimeRange strategy: superfile {} column {column:?} downcast failed",
            seg.uri.0
        ),
    })
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, sync::Arc};

    use arrow_array::{
        ArrayRef, Int32Array, Int64Array, TimestampMicrosecondArray, TimestampMillisecondArray,
        TimestampNanosecondArray, TimestampSecondArray,
    };

    use super::*;
    use crate::supertable::manifest::{ScalarStatsAgg, SuperfileEntry, SuperfileUri};

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

    fn empty_seg() -> SuperfileEntry {
        SuperfileEntry {
            superfile_id: uuid::Uuid::nil(),
            uri: SuperfileUri(uuid::Uuid::nil()),
            n_docs: 0,
            id_min: 0,
            id_max: 0,
            scalar_stats: HashMap::new(),
            fts_summary: HashMap::new(),
            vector_summary: HashMap::new(),
            partition_key: Vec::new(),
            partition_hint: None,
            subsection_offsets: None,
        }
    }

    fn seg_with_i64(column: &str, min: i64, max: i64) -> SuperfileEntry {
        let mut s = empty_seg();
        let mn: ArrayRef = Arc::new(Int64Array::from(vec![min]));
        let mx: ArrayRef = Arc::new(Int64Array::from(vec![max]));
        s.scalar_stats
            .insert(column.to_string(), ScalarStatsAgg::from_min_max(mn, mx));
        s
    }

    fn assert_spans_partition(err: ManifestError, needle: &str) {
        match err {
            ManifestError::SuperfileSpansPartition { detail } => assert!(
                detail.contains(needle),
                "expected `{needle}` in detail; got: {detail}"
            ),
            other => panic!("expected SuperfileSpansPartition; got {other:?}"),
        }
    }

    // ---- encode_partition_key ------------------------------------------

    #[test]
    fn encode_partition_key_time_range_emits_le_u64() {
        let bytes = encode_partition_key(&PartitionKey::TimeRange(0x01_02_03_04_05_06_07_08));
        assert_eq!(bytes.len(), 8);
        assert_eq!(bytes, 0x01_02_03_04_05_06_07_08u64.to_le_bytes().to_vec());
    }

    #[test]
    fn encode_partition_key_hash_emits_le_u32() {
        let bytes = encode_partition_key(&PartitionKey::Hash(0xCAFEBABE));
        assert_eq!(bytes.len(), 4);
        assert_eq!(bytes, 0xCAFEBABEu32.to_le_bytes().to_vec());
    }

    #[test]
    fn encode_partition_key_column_range_emits_le_u16() {
        let bytes = encode_partition_key(&PartitionKey::ColumnRange(0xDEAD));
        assert_eq!(bytes.len(), 2);
        assert_eq!(bytes, 0xDEADu16.to_le_bytes().to_vec());
    }

    // ---- decode_partition_key — success roundtrip ----------------------

    #[test]
    fn decode_partition_key_round_trips_time_range() {
        let original = PartitionKey::TimeRange(42);
        let bytes = encode_partition_key(&original);
        let strategy = PartitionStrategy::TimeRange {
            column: "_id".into(),
            granularity_secs: 86_400,
        };
        let decoded = decode_partition_key(&bytes, &strategy).expect("decode");
        assert_eq!(decoded, original);
    }

    #[test]
    fn decode_partition_key_round_trips_hash() {
        let original = PartitionKey::Hash(5);
        let bytes = encode_partition_key(&original);
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 16,
        };
        let decoded = decode_partition_key(&bytes, &strategy).expect("decode");
        assert_eq!(decoded, original);
    }

    #[test]
    fn decode_partition_key_round_trips_column_range() {
        let original = PartitionKey::ColumnRange(3);
        let bytes = encode_partition_key(&original);
        let strategy = PartitionStrategy::ColumnRange {
            column: "_id".into(),
            boundaries: vec![vec![]],
        };
        let decoded = decode_partition_key(&bytes, &strategy).expect("decode");
        assert_eq!(decoded, original);
    }

    // ---- decode_partition_key — size mismatch errors -------------------

    #[test]
    fn decode_partition_key_rejects_wrong_size_time_range() {
        let strategy = PartitionStrategy::TimeRange {
            column: "_id".into(),
            granularity_secs: 1,
        };
        let err = decode_partition_key(&[1, 2, 3], &strategy).expect_err("must error");
        match err {
            CommitError::PointerParse(msg) => {
                assert!(msg.contains("TimeRange"), "{msg}");
                assert!(msg.contains("8 bytes"), "{msg}");
            }
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn decode_partition_key_rejects_wrong_size_hash() {
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 4,
        };
        let err = decode_partition_key(&[1, 2, 3], &strategy).expect_err("must error");
        match err {
            CommitError::PointerParse(msg) => {
                assert!(msg.contains("Hash"), "{msg}");
                assert!(msg.contains("4 bytes"), "{msg}");
            }
            other => panic!("got {other:?}"),
        }
    }

    #[test]
    fn decode_partition_key_rejects_wrong_size_column_range() {
        let strategy = PartitionStrategy::ColumnRange {
            column: "_id".into(),
            boundaries: vec![vec![]],
        };
        let err = decode_partition_key(&[1, 2, 3], &strategy).expect_err("must error");
        match err {
            CommitError::PointerParse(msg) => {
                assert!(msg.contains("ColumnRange"), "{msg}");
                assert!(msg.contains("2 bytes"), "{msg}");
            }
            other => panic!("got {other:?}"),
        }
    }

    // ---- assign_partition: TimeRange -----------------------------------

    #[test]
    fn assign_partition_time_range_single_bucket() {
        // min and max sit in the same daily bucket → success.
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 86_400,
        };
        // 100 .. 90_000 → both buckets are 0..1 → same bucket 0.
        let seg = seg_with_i64("ts", 100, 80_000);
        let key = assign_partition(&seg, &strategy).expect("assign");
        assert_eq!(key, PartitionKey::TimeRange(0));
    }

    #[test]
    fn assign_partition_time_range_rejects_superfile_spanning_buckets() {
        // min in bucket 0, max in bucket 1 → SuperfileSpansPartition.
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 86_400,
        };
        let seg = seg_with_i64("ts", 100, 100_000);
        let err = assign_partition(&seg, &strategy).expect_err("must span");
        assert_spans_partition(err, "spans buckets");
    }

    #[test]
    fn assign_partition_time_range_rejects_zero_granularity() {
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 0,
        };
        let seg = seg_with_i64("ts", 0, 0);
        let err = assign_partition(&seg, &strategy).expect_err("must reject");
        assert_spans_partition(err, "granularity_secs must be > 0");
    }

    #[test]
    fn assign_partition_time_range_rejects_negative_granularity() {
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: -1,
        };
        let seg = seg_with_i64("ts", 0, 0);
        let err = assign_partition(&seg, &strategy).expect_err("must reject");
        assert_spans_partition(err, "granularity_secs must be > 0");
    }

    #[test]
    fn assign_partition_time_range_rejects_missing_column_stats() {
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 86_400,
        };
        let seg = empty_seg(); // no scalar_stats at all
        let err = assign_partition(&seg, &strategy).expect_err("missing stats");
        assert_spans_partition(err, "no scalar_stats");
    }

    #[test]
    fn assign_partition_time_range_supports_timestamp_columns() {
        // Each timestamp width must downcast cleanly so users can
        // configure granularity_secs against the column's actual
        // unit. Covers Second/Milli/Micro/Nano arms in downcast_i64.
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 86_400,
        };
        let cases: Vec<(ArrayRef, ArrayRef)> = vec![
            (
                Arc::new(TimestampSecondArray::from(vec![100])),
                Arc::new(TimestampSecondArray::from(vec![200])),
            ),
            (
                Arc::new(TimestampMillisecondArray::from(vec![100])),
                Arc::new(TimestampMillisecondArray::from(vec![200])),
            ),
            (
                Arc::new(TimestampMicrosecondArray::from(vec![100])),
                Arc::new(TimestampMicrosecondArray::from(vec![200])),
            ),
            (
                Arc::new(TimestampNanosecondArray::from(vec![100])),
                Arc::new(TimestampNanosecondArray::from(vec![200])),
            ),
        ];
        for (mn, mx) in cases {
            let mut seg = empty_seg();
            seg.scalar_stats
                .insert("ts".into(), ScalarStatsAgg::from_min_max(mn, mx));
            let key = assign_partition(&seg, &strategy).expect("assign");
            assert_eq!(key, PartitionKey::TimeRange(0));
        }
    }

    #[test]
    fn assign_partition_time_range_rejects_unsupported_column_type() {
        // Int32 isn't supported (only Int64 + Timestamp widths).
        // Surfaces as SuperfileSpansPartition with a type-name hint.
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 86_400,
        };
        let mut seg = empty_seg();
        let mn: ArrayRef = Arc::new(Int32Array::from(vec![100]));
        let mx: ArrayRef = Arc::new(Int32Array::from(vec![200]));
        seg.scalar_stats
            .insert("ts".into(), ScalarStatsAgg::from_min_max(mn, mx));
        let err = assign_partition(&seg, &strategy).expect_err("unsupported");
        assert_spans_partition(err, "unsupported type");
    }

    #[test]
    fn assign_partition_time_range_rejects_null_stats_array() {
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 86_400,
        };
        let mut seg = empty_seg();
        // Length-1 array with a single null value.
        let nulls: Vec<Option<i64>> = vec![None];
        let mn: ArrayRef = Arc::new(Int64Array::from(nulls.clone()));
        let mx: ArrayRef = Arc::new(Int64Array::from(nulls));
        seg.scalar_stats
            .insert("ts".into(), ScalarStatsAgg::from_min_max(mn, mx));
        let err = assign_partition(&seg, &strategy).expect_err("null stats");
        assert_spans_partition(err, "empty or null at index 0");
    }

    #[test]
    fn assign_partition_time_range_handles_negative_values_with_div_euclid() {
        // `div_euclid` (not bare `/`) ensures negative timestamps
        // bucket consistently — same-bucket pairs across the
        // negative range must succeed. Catches an accidental
        // `min / g != max / g` regression that breaks at the
        // sign flip.
        let strategy = PartitionStrategy::TimeRange {
            column: "ts".into(),
            granularity_secs: 10,
        };
        // -25 div_euclid 10 = -3; -21 div_euclid 10 = -3.
        let seg = seg_with_i64("ts", -25, -21);
        let key = assign_partition(&seg, &strategy).expect("assign");
        assert_eq!(key, PartitionKey::TimeRange(-3i64 as u64));
    }

    // ---- assign_partition: Hash ----------------------------------------

    #[test]
    fn assign_partition_hash_single_bucket_short_circuits() {
        // n_buckets=1 → always bucket 0, no partition_hint needed.
        // This is the default; tests rely on it staying
        // hint-less.
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 1,
        };
        let seg = empty_seg();
        let key = assign_partition(&seg, &strategy).expect("assign");
        assert_eq!(key, PartitionKey::Hash(0));
    }

    #[test]
    fn assign_partition_hash_zero_buckets_treats_as_one() {
        // Defensive: n_buckets=0 falls into the <=1 short-circuit
        // rather than panicking on a later modulo.
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 0,
        };
        let seg = empty_seg();
        let key = assign_partition(&seg, &strategy).expect("assign");
        assert_eq!(key, PartitionKey::Hash(0));
    }

    #[test]
    fn assign_partition_hash_uses_partition_hint() {
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 4,
        };
        let mut seg = empty_seg();
        seg.partition_hint = Some(2);
        let key = assign_partition(&seg, &strategy).expect("assign");
        assert_eq!(key, PartitionKey::Hash(2));
    }

    #[test]
    fn assign_partition_hash_requires_hint_when_multi_bucket() {
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 4,
        };
        let seg = empty_seg(); // hint = None
        let err = assign_partition(&seg, &strategy).expect_err("must reject");
        assert_spans_partition(err, "requires pre-sharded");
    }

    #[test]
    fn assign_partition_hash_rejects_out_of_range_hint() {
        let strategy = PartitionStrategy::Hash {
            column: "_id".into(),
            n_buckets: 4,
        };
        let mut seg = empty_seg();
        seg.partition_hint = Some(4); // == n_buckets, off-by-one
        let err = assign_partition(&seg, &strategy).expect_err("out of range");
        assert_spans_partition(err, "out of range");
    }

    // ---- assign_partition: ColumnRange (currently unimplemented) -------

    #[test]
    fn assign_partition_column_range_is_not_yet_supported() {
        // The implementation explicitly bails on ColumnRange until
        // Follow-up. Locking the message keeps it visible
        // when the follow-up lands.
        let strategy = PartitionStrategy::ColumnRange {
            column: "_id".into(),
            boundaries: vec![vec![]],
        };
        let seg = empty_seg();
        let err = assign_partition(&seg, &strategy).expect_err("not impl");
        assert_spans_partition(err, "ColumnRange partition assignment lands");
    }
}