dupehell 0.5.5

Synthetic multi-domain dataset generator for record linkage benchmarking
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
// DupeHell -- MIT License
//
// Synthetic multi-domain dataset generator for record linkage benchmarking.
// No liability for misuse.

use std::sync::Arc;

use arrow::array::{ArrayRef, StringBuilder};

#[cfg(test)]
use arrow::array::{Array, AsArray};

use crate::context::Context;
use crate::rng::Rng;

// ── Character tables ──────────────────────────────────────────────────────

const ALPHA_UPPER_NO_IOQ: &[u8] = b"ABCDEFGHJKLMNPRSTUVWXYZ";
#[cfg(test)]
const ALPHA_UPPER: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// ── Helpers ───────────────────────────────────────────────────────────────

/// Build a StringArray by appending rows using a closure that writes into
/// &mut Vec<u8>.
///
/// # Safety invariant on `build_row`
/// Every byte `build_row` pushes must come either from an ASCII literal
/// (`b'-'`, `b"St"`, ...) or from copying a **whole** `&str`/byte slice
/// (`s.as_bytes()`, never a sub-range that could split a multi-byte code
/// point) — `u8::to_ascii_lowercase`/`to_ascii_uppercase` are also safe to
/// apply per-byte, since they're the identity on every byte `>= 0x80`, so a
/// multi-byte sequence passes through intact either way. Every one of the
/// ~60 call sites across `fast_template.rs`/`buf_gen.rs` follows this
/// pattern (mixing crate ASCII literals with untouched slices of
/// `Context::pool_store` values), so the buffer handed to `from_utf8` below
/// is valid UTF-8 by construction — checked with `debug_assert!` (so
/// `cargo test`'s `fast_template::tests::test_all_templates_run`, which
/// exercises every registered template, catches a future violation) and
/// then skipped in `--release` via `from_utf8_unchecked` (perf-hunt
/// hunt0109/H1, measured isolated ~22-29% faster than the checked call —
/// `examples/profile_utf8_validation.rs` — on the highest-execution-count
/// path in the crate: once per generated string cell, of every row, of
/// every batch, of every run).
pub(crate) fn build_string_array<F>(n: usize, avg_width: usize, mut build_row: F) -> ArrayRef
where
    F: FnMut(&mut Vec<u8>),
{
    let mut builder = StringBuilder::with_capacity(n, n * avg_width);
    let mut buf = Vec::with_capacity(avg_width);
    for _ in 0..n {
        buf.clear();
        build_row(&mut buf);
        debug_assert!(
            std::str::from_utf8(&buf).is_ok(),
            "build_string_array: build_row produced invalid UTF-8 — see the safety invariant \
             on this function's doc comment"
        );
        // SAFETY: see the doc comment above.
        let s = unsafe { std::str::from_utf8_unchecked(&buf) };
        builder.append_value(s);
    }
    Arc::new(builder.finish())
}

/// Return a random byte from `chars`.
fn rand_char(chars: &[u8], rng: &mut Rng) -> u8 {
    chars[rng.next_usize(chars.len())]
}

// ── buf_digits: generic digit string from integer ─────────────────────────

/// Produce an Arrow StringArray where each entry is `num` zero-padded to `width` digits.
/// If `watermark_mask` is Some, the last 3 digits are masked with the given value.
pub fn buf_digits(nums: &[u64], width: usize, watermark_mask: Option<u64>) -> ArrayRef {
    let n = nums.len();
    let mut builder = StringBuilder::with_capacity(n, n * width);
    // SAFETY: every index 0..width is written by the digit-fill loop below
    // before `s` is ever read (`from_utf8(&s)`), so the zero-fill that
    // `vec![b'0'; width]` would otherwise do is redundant work.
    #[allow(clippy::uninit_vec)]
    let mut s: Vec<u8> = {
        let mut v = Vec::with_capacity(width);
        unsafe { v.set_len(width) };
        v
    };
    for num in nums {
        let mut val = *num;
        for j in (0..width).rev() {
            s[j] = b'0' + (val % 10) as u8;
            val /= 10;
        }
        if let Some(wm) = watermark_mask
            && width >= 3
        {
            let start = width - 3;
            s[start] = b'0' + ((wm / 100) % 10) as u8;
            s[start + 1] = b'0' + ((wm / 10) % 10) as u8;
            s[start + 2] = b'0' + (wm % 10) as u8;
        }
        // SAFETY (perf-hunt hunt0109/H1): every byte of `s` is one of
        // `b'0'..=b'9'` (see the digit-fill loop above), so `s` is valid
        // ASCII/UTF-8 by construction — `debug_assert!` keeps `cargo test`
        // catching a future violation, `from_utf8_unchecked` skips the
        // redundant re-validation in `--release` (same measured gain as
        // `buf_gen::build_string_array`, see its doc comment).
        debug_assert!(
            std::str::from_utf8(&s).is_ok(),
            "buf_digits: non-digit byte in s"
        );
        builder.append_value(unsafe { std::str::from_utf8_unchecked(&s) });
    }
    Arc::new(builder.finish())
}

// ── bytes_strings: random fixed-length strings from a charset ─────────────

/// Generate random fixed-length strings from a byte character table.
///
/// # Safety invariant on `chars`
/// Every caller passes an ASCII byte table (`ALPHA_UPPER_NO_IOQ`, hex/
/// base36 digit sets, ...) — see the SAFETY note below.
pub fn bytes_strings(chars: &[u8], n: usize, length: usize, rng: &mut Rng) -> ArrayRef {
    let mut builder = StringBuilder::with_capacity(n, n * length);
    // SAFETY: the `for b in s.iter_mut()` loop below writes every index
    // 0..length before `s` is ever read (`from_utf8(&s)`), so the zero-fill
    // that `vec![0u8; length]` would otherwise do is redundant work.
    #[allow(clippy::uninit_vec)]
    let mut s: Vec<u8> = {
        let mut v = Vec::with_capacity(length);
        unsafe { v.set_len(length) };
        v
    };
    for _ in 0..n {
        for b in s.iter_mut() {
            *b = rand_char(chars, rng);
        }
        // SAFETY (perf-hunt hunt0109/H1): every byte of `s` is drawn from
        // `chars`, which every call site passes as an ASCII table — valid
        // UTF-8 by construction, checked via `debug_assert!` (skipped in
        // `--release`, same measured gain as `build_string_array`).
        debug_assert!(
            std::str::from_utf8(&s).is_ok(),
            "bytes_strings: non-ASCII byte in chars"
        );
        builder.append_value(unsafe { std::str::from_utf8_unchecked(&s) });
    }
    Arc::new(builder.finish())
}

/// Replace the last `n_digits` bytes of `buf` with the given value (0-padded).
fn watermark_buf(buf: &mut [u8], n_digits: usize, value: u64) {
    let start = buf.len() - n_digits;
    let mut v = value;
    for j in (0..n_digits).rev() {
        buf[start + j] = b'0' + (v % 10) as u8;
        v /= 10;
    }
}

// ── Buffer generators ─────────────────────────────────────────────────────

macro_rules! push_digit {
    ($buf:ident, $val:expr) => {
        $buf.push(b'0' + ($val as u8));
    };
}

/// `+1-XXX-555-01XX xXXXX` (21 bytes) -- exchange 555, line 0100-0199, the
/// NANP fictional-use block reserved by the numbering plan administrator so
/// no generated number can ever be a real, dialable subscriber number,
/// regardless of area code. The `xXXXX` suffix is not part of the reserved
/// block -- it exists purely to give the watermark a place to live that
/// isn't the line number, so the 100-value line range stays fully free
/// instead of being collapsed to a single dataset-wide constant (audit
/// finding: `distinct(phone)` was capped at ~800 regardless of dataset
/// size, since the watermark previously overwrote both free digits of the
/// line number). Mirrors `buf_office_phone`'s layout, which never had this
/// problem because its watermark already lands on the extension.
pub fn buf_phone(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_3digits(0x50484f4e);
    build_string_array(n, 21, |buf| {
        let a = rng.next_usize(800) + 200; // 200-999
        let line = rng.next_usize(100); // 00-99
        let x = rng.next_usize(9900) + 100; // 100-9999
        buf.push(b'+');
        buf.push(b'1');
        buf.push(b'-');
        push_digit!(buf, a / 100);
        push_digit!(buf, (a / 10) % 10);
        push_digit!(buf, a % 10);
        buf.push(b'-');
        buf.extend_from_slice(b"555-01");
        push_digit!(buf, line / 10);
        push_digit!(buf, line % 10);
        buf.push(b' ');
        buf.push(b'x');
        push_digit!(buf, x / 1000);
        push_digit!(buf, (x / 100) % 10);
        push_digit!(buf, (x / 10) % 10);
        push_digit!(buf, x % 10);
        watermark_buf(buf, 2, wm / 10);
    })
}

/// `XXX-XX-XXXX` (11 bytes) -- area number confined to 900-999, a block the
/// SSA has documented as never assigned (along with 000 and 666), so no
/// generated value can ever collide with a real person's SSN. Unlike
/// `buf_phone`, the format is a fixed 9-digit standard shape -- no room to
/// add an extension-style suffix -- so the fix here narrows the
/// watermark's footprint on `c` from 3 digits to 1 (audit finding:
/// `distinct(ssn)` was capped at ~81 000 regardless of dataset size, since
/// only `c`'s leading digit survived the watermark). Still watermarked and
/// still detectable, just over a smaller digit span.
pub fn buf_ssn(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_3digits(0x53534e);
    build_string_array(n, 11, |buf| {
        let a = rng.next_usize(100) + 900; // 900-999 (never assigned by SSA)
        let b = rng.next_usize(90) + 10; // 10-99
        let c = rng.next_usize(9000) + 1000; // 1000-9999
        push_digit!(buf, a / 100);
        push_digit!(buf, (a / 10) % 10);
        push_digit!(buf, a % 10);
        buf.push(b'-');
        push_digit!(buf, b / 10);
        push_digit!(buf, b % 10);
        buf.push(b'-');
        push_digit!(buf, c / 1000);
        push_digit!(buf, (c / 100) % 10);
        push_digit!(buf, (c / 10) % 10);
        push_digit!(buf, c % 10);
        watermark_buf(buf, 1, wm % 10);
    })
}

/// Keep only ASCII alphanumeric characters, lowercased -- used to turn a
/// pool name (possibly accented, possibly multi-word) into a safe email/
/// username local-part fragment without risking invalid UTF-8 slicing.
/// Appends into a caller-owned, reusable buffer (not cleared here -- callers
/// `clear()` it themselves, same convention as `build_string_array`'s row
/// buffer) instead of allocating a fresh `String` per call.
fn ascii_local_into(s: &str, buf: &mut Vec<u8>) {
    for c in s.chars() {
        let lc = c.to_ascii_lowercase();
        if lc.is_ascii_alphanumeric() {
            // SAFETY: `is_ascii_alphanumeric` guarantees `lc` is in 0..128,
            // so truncating to `u8` reproduces the same byte `lc.to_string()`
            // would have produced (ASCII chars are 1 byte in UTF-8).
            buf.push(lc as u8);
        }
    }
}

/// `firstname.lastname######@example.com` (variable length, `example.com`
/// reserved by RFC 2606 -- see `ETHICS.md`) -- derived from the
/// `first_name`/`last_name` pools already loaded for other fields, combined
/// across 3 layout strategies plus a 6-digit numeric suffix. Replaces the
/// previous fixed `user#####@mail.com` pattern, whose 90 000-value space
/// caused accidental (unlabeled) collisions between unrelated entities once
/// a dataset's population exceeded that range.
///
/// Audit finding (backlog item #7): only the 3rd layout strategy carried a
/// numeric suffix, and a narrow one (100-999, 900 values). The other two
/// strategies' cardinality was bounded solely by the name pools (~520
/// first names x ~515 last names per locale, or ~26 x ~515 for the
/// initial+lastname layout) -- collisions grew from 8.5% at 1M rows to
/// 28.9% at 25M, a name-pool-driven birthday-paradox effect rather than a
/// hard cap. Every strategy now appends the same wide 6-digit suffix
/// (100000-999999, 900 000 values), independent of which name pair was
/// drawn, so collisions stay governed by that much larger, size-independent
/// space instead of the name pools.
pub fn buf_email(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let first = ctx
        .pool_store
        .get("first_name")
        .expect("pool 'first_name' not found");
    let last = ctx
        .pool_store
        .get("last_name")
        .expect("pool 'last_name' not found");
    let mut f_buf: Vec<u8> = Vec::with_capacity(16);
    let mut l_buf: Vec<u8> = Vec::with_capacity(16);
    build_string_array(n, 38, |buf| {
        f_buf.clear();
        ascii_local_into(&first[rng.next_usize(first.len())], &mut f_buf);
        if f_buf.is_empty() {
            f_buf.extend_from_slice(b"user");
        }
        l_buf.clear();
        ascii_local_into(&last[rng.next_usize(last.len())], &mut l_buf);
        if l_buf.is_empty() {
            l_buf.push(b'x');
        }
        match rng.next_usize(3) {
            0 => {
                buf.extend_from_slice(&f_buf);
                buf.push(b'.');
                buf.extend_from_slice(&l_buf);
            }
            1 => {
                buf.push(f_buf[0]);
                buf.extend_from_slice(&l_buf);
            }
            _ => {
                buf.extend_from_slice(&f_buf);
            }
        }
        let num = rng.next_usize(900_000) + 100_000; // 100000-999999
        push_digit!(buf, num / 100_000);
        push_digit!(buf, (num / 10_000) % 10);
        push_digit!(buf, (num / 1_000) % 10);
        push_digit!(buf, (num / 100) % 10);
        push_digit!(buf, (num / 10) % 10);
        push_digit!(buf, num % 10);
        buf.extend_from_slice(b"@example.com");
    })
}

/// `XXX-XX-XXXXXX` (13 bytes, PAN)
pub fn buf_pan(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_3digits(0x50414e);
    build_string_array(n, 13, |buf| {
        let a = rng.next_usize(900) + 100; // 100-999
        let b = rng.next_usize(90) + 10; // 10-99
        let c = rng.next_usize(900000) + 100000; // 100000-999999
        push_digit!(buf, a / 100);
        push_digit!(buf, (a / 10) % 10);
        push_digit!(buf, a % 10);
        buf.push(b'-');
        push_digit!(buf, b / 10);
        push_digit!(buf, b % 10);
        buf.push(b'-');
        push_digit!(buf, c / 100000);
        push_digit!(buf, (c / 10000) % 10);
        push_digit!(buf, (c / 1000) % 10);
        push_digit!(buf, (c / 100) % 10);
        push_digit!(buf, (c / 10) % 10);
        push_digit!(buf, c % 10);
        watermark_buf(buf, 3, wm);
    })
}

/// `XXXX-XXX-XXX` (12 bytes, Medicare)
pub fn buf_medicare(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_3digits(0x4d4544);
    build_string_array(n, 12, |buf| {
        let a = rng.next_usize(9000) + 1000; // 1000-9999
        let b = rng.next_usize(900) + 100; // 100-999
        let c = rng.next_usize(900) + 100; // 100-999
        push_digit!(buf, a / 1000);
        push_digit!(buf, (a / 100) % 10);
        push_digit!(buf, (a / 10) % 10);
        push_digit!(buf, a % 10);
        buf.push(b'-');
        push_digit!(buf, b / 100);
        push_digit!(buf, (b / 10) % 10);
        push_digit!(buf, b % 10);
        buf.push(b'-');
        push_digit!(buf, c / 100);
        push_digit!(buf, (c / 10) % 10);
        push_digit!(buf, c % 10);
        watermark_buf(buf, 2, wm / 10);
    })
}

/// `+1-XXX-555-01XX xXXXX` (21 bytes, office phone with extension) --
/// exchange 555, line 0100-0199, same NANP fictional-use block as
/// `buf_phone`.
pub fn buf_office_phone(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_3digits(0x4f4643);
    build_string_array(n, 21, |buf| {
        let a = rng.next_usize(800) + 200; // 200-999
        let line = rng.next_usize(100); // 00-99
        let x = rng.next_usize(9900) + 100; // 100-9999
        buf.push(b'+');
        buf.push(b'1');
        buf.push(b'-');
        push_digit!(buf, a / 100);
        push_digit!(buf, (a / 10) % 10);
        push_digit!(buf, a % 10);
        buf.push(b'-');
        buf.extend_from_slice(b"555-01");
        push_digit!(buf, line / 10);
        push_digit!(buf, line % 10);
        buf.push(b' ');
        buf.push(b'x');
        push_digit!(buf, x / 1000);
        push_digit!(buf, (x / 100) % 10);
        push_digit!(buf, (x / 10) % 10);
        push_digit!(buf, x % 10);
        watermark_buf(buf, 3, wm);
    })
}

/// `LXXXXXXX` (8 bytes, passport: 1 letter + 7 digits)
pub fn buf_passport(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_3digits(0x504153);
    build_string_array(n, 8, |buf| {
        let letter = rand_char(ALPHA_UPPER_NO_IOQ, rng);
        let nums = rng.next_usize(9000000) + 1000000;
        buf.push(letter);
        push_digit!(buf, nums / 1000000);
        push_digit!(buf, (nums / 100000) % 10);
        push_digit!(buf, (nums / 10000) % 10);
        push_digit!(buf, (nums / 1000) % 10);
        push_digit!(buf, (nums / 100) % 10);
        push_digit!(buf, (nums / 10) % 10);
        push_digit!(buf, nums % 10);
        watermark_buf(buf, 3, wm);
    })
}

/// `XXX-XX-XXXX` with X replacements (SSN last 4)
pub fn buf_ssn_last4(n: usize, rng: &mut Rng) -> ArrayRef {
    build_string_array(n, 11, |buf| {
        let nums = rng.next_usize(9000) + 1000;
        buf.extend_from_slice(b"XXX-XX-");
        push_digit!(buf, nums / 1000);
        push_digit!(buf, (nums / 100) % 10);
        push_digit!(buf, (nums / 10) % 10);
        push_digit!(buf, nums % 10);
    })
}

/// `****XXXXXXX` (11 bytes, masked account number) -- 7 digits, only the
/// last 2 watermarked, leaving 5 free digits (~900 000 combinations)
/// instead of the previous 3-digit/2-watermarked layout, which left only 1
/// free digit (9 possible values total across the whole dataset).
pub fn buf_acct_num(n: usize, rng: &mut Rng, ctx: &Context) -> ArrayRef {
    let wm = ctx.watermark_2digits(0x414354);
    build_string_array(n, 11, |buf| {
        let nums = rng.next_usize(9_000_000) + 1_000_000;
        buf.extend_from_slice(b"****");
        push_digit!(buf, nums / 1_000_000);
        push_digit!(buf, (nums / 100_000) % 10);
        push_digit!(buf, (nums / 10_000) % 10);
        push_digit!(buf, (nums / 1_000) % 10);
        push_digit!(buf, (nums / 100) % 10);
        push_digit!(buf, (nums / 10) % 10);
        push_digit!(buf, nums % 10);
        watermark_buf(buf, 2, wm);
    })
}

/// `LXXX` (4 bytes, branch code: letter + 3 digits)
pub fn buf_branch(n: usize, rng: &mut Rng) -> ArrayRef {
    build_string_array(n, 4, |buf| {
        let letter = rand_char(ALPHA_UPPER_NO_IOQ, rng);
        let nums = rng.next_usize(900) + 100;
        buf.push(letter);
        push_digit!(buf, nums / 100);
        push_digit!(buf, (nums / 10) % 10);
        push_digit!(buf, nums % 10);
    })
}

/// `NNL` (2-3 bytes, aircraft seat: row 1-60 + seat letter A-F, no I).
pub fn buf_seat_number(n: usize, rng: &mut Rng) -> ArrayRef {
    const SEAT_LETTERS: &[u8] = b"ABCDEF";
    build_string_array(n, 3, |buf| {
        let row = rng.next_usize(60) + 1;
        if row >= 10 {
            push_digit!(buf, row / 10);
        }
        push_digit!(buf, row % 10);
        buf.push(rand_char(SEAT_LETTERS, rng));
    })
}

/// `XXXXXX` (6 bytes, PNR-style booking reference: 6 alnum chars, no I/O/0/1
/// to match real-world airline PNR conventions and avoid visual ambiguity).
pub fn buf_booking_reference(n: usize, rng: &mut Rng) -> ArrayRef {
    const PNR_CHARS: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    build_string_array(n, 6, |buf| {
        for _ in 0..6 {
            buf.push(rand_char(PNR_CHARS, rng));
        }
    })
}

/// `LLXXXXXXXXX` (11 bytes, frequent-flyer number: 2 letters + 9 digits).
pub fn buf_frequent_flyer(n: usize, rng: &mut Rng) -> ArrayRef {
    build_string_array(n, 11, |buf| {
        buf.push(rand_char(ALPHA_UPPER_NO_IOQ, rng));
        buf.push(rand_char(ALPHA_UPPER_NO_IOQ, rng));
        let nums = rng.next_usize(900_000_000) + 100_000_000;
        push_digit!(buf, nums / 100_000_000);
        push_digit!(buf, (nums / 10_000_000) % 10);
        push_digit!(buf, (nums / 1_000_000) % 10);
        push_digit!(buf, (nums / 100_000) % 10);
        push_digit!(buf, (nums / 10_000) % 10);
        push_digit!(buf, (nums / 1_000) % 10);
        push_digit!(buf, (nums / 100) % 10);
        push_digit!(buf, (nums / 10) % 10);
        push_digit!(buf, nums % 10);
    })
}

// ── Tests ─────────────────────────────────────────────────────────────────

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

    fn test_rng() -> Rng {
        Rng::new(42)
    }

    /// Unlike `Context::test()` (empty pool_store), this loads the real
    /// `assets/pools` directory -- needed by `buf_email`, which now derives
    /// the local-part from the `first_name`/`last_name` pools.
    fn test_ctx_with_pools() -> Context {
        let pools_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("assets/pools");
        Context::new("kyc", "en", pools_dir.to_str().unwrap()).unwrap()
    }

    #[test]
    fn test_buf_phone() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_phone(5, &mut rng, &ctx);
        assert_eq!(arr.len(), 5);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 21, "phone[{i}] = {v:?}");
            assert!(v.starts_with("+1-"), "phone[{i}] = {v:?}");
            assert!(
                v.contains("-555-01"),
                "phone[{i}] = {v:?} not in NANP fictional block"
            );
        }
    }

    #[test]
    fn test_buf_ssn() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_ssn(5, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 11, "ssn[{i}] = {v:?}");
            let area: u32 = v[..3].parse().unwrap();
            assert!(
                (900..=999).contains(&area),
                "ssn[{i}] = {v:?} area not in never-assigned SSA block"
            );
        }
    }

    #[test]
    fn test_buf_email() {
        let mut rng = test_rng();
        let ctx = test_ctx_with_pools();
        let arr = buf_email(200, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        let mut seen = std::collections::HashSet::new();
        for i in 0..200 {
            let v = s.value(i);
            assert!(v.ends_with("@example.com"), "email[{i}] = {v:?}");
            let local = &v[..v.len() - "@example.com".len()];
            assert!(!local.is_empty(), "email[{i}] = {v:?} has empty local-part");
            assert!(
                local
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b == b'.'),
                "email[{i}] = {v:?} has non ascii-alnum local-part"
            );
            seen.insert(v.to_string());
        }
        assert!(
            seen.len() > 150,
            "expected high cardinality over 200 rows, got {} distinct",
            seen.len()
        );
    }

    #[test]
    fn test_buf_pan() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_pan(5, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            assert_eq!(s.value(i).len(), 13, "pan[{i}] = {:?}", s.value(i));
        }
    }

    #[test]
    fn test_buf_medicare() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_medicare(5, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            assert_eq!(s.value(i).len(), 12, "medicare[{i}] = {:?}", s.value(i));
        }
    }

    #[test]
    fn test_buf_office_phone() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_office_phone(5, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 21, "office_phone[{i}] = {v:?}");
            assert!(v.contains(" x"), "office_phone[{i}] = {v:?}");
            assert!(
                v.contains("-555-01"),
                "office_phone[{i}] = {v:?} not in NANP fictional block"
            );
        }
    }

    #[test]
    fn test_buf_passport() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_passport(5, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 8, "passport[{i}] = {v:?}");
            assert!(v.as_bytes()[0].is_ascii_uppercase());
            for j in 1..8 {
                assert!(v.as_bytes()[j].is_ascii_digit(), "passport[{i}] = {v:?}");
            }
        }
    }

    #[test]
    fn test_buf_ssn_last4() {
        let mut rng = test_rng();
        let arr = buf_ssn_last4(5, &mut rng);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 11, "ssn_last4[{i}] = {v:?}");
            assert!(v.starts_with("XXX-XX-"));
            assert!(v.as_bytes()[3] == b'-' && v.as_bytes()[6] == b'-');
            for j in 7..11 {
                assert!(v.as_bytes()[j].is_ascii_digit(), "ssn_last4[{i}] = {v:?}");
            }
        }
    }

    #[test]
    fn test_buf_acct_num() {
        let mut rng = test_rng();
        let ctx = Context::test();
        let arr = buf_acct_num(5, &mut rng, &ctx);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 11, "acct_num[{i}] = {v:?}");
            assert!(v.starts_with("****"));
            assert!(v.as_bytes()[4].is_ascii_digit());
        }
    }

    #[test]
    fn test_buf_branch() {
        let mut rng = test_rng();
        let arr = buf_branch(5, &mut rng);
        let s = arr.as_string::<i32>();
        for i in 0..5 {
            let v = s.value(i);
            assert_eq!(v.len(), 4, "branch[{i}] = {v:?}");
            assert!(v.as_bytes()[0].is_ascii_uppercase());
            for j in 1..4 {
                assert!(v.as_bytes()[j].is_ascii_digit(), "branch[{i}] = {v:?}");
            }
        }
    }

    #[test]
    fn test_bytes_strings() {
        let mut rng = test_rng();
        let arr = bytes_strings(ALPHA_UPPER, 10, 5, &mut rng);
        let s = arr.as_string::<i32>();
        assert_eq!(s.len(), 10);
        for i in 0..10 {
            let v = s.value(i);
            assert_eq!(v.len(), 5, "bytes_strings[{i}] = {v:?}");
            for c in v.bytes() {
                assert!(c.is_ascii_uppercase(), "bytes_strings[{i}] bad byte = {c}");
            }
        }
    }

    #[test]
    fn test_buf_digits() {
        let nums = vec![123, 45, 7890, 0, 9999];
        let arr = buf_digits(&nums, 4, None);
        let s = arr.as_string::<i32>();
        assert_eq!(s.value(0), "0123");
        assert_eq!(s.value(1), "0045");
        assert_eq!(s.value(2), "7890");
        assert_eq!(s.value(3), "0000");
        assert_eq!(s.value(4), "9999");
    }

    #[test]
    fn test_deterministic() {
        let mut rng_a = Rng::new(42);
        let mut rng_b = Rng::new(42);
        let ctx = Context::test();
        let a = buf_phone(100, &mut rng_a, &ctx);
        let b = buf_phone(100, &mut rng_b, &ctx);
        let a_arr = a.as_string::<i32>();
        let b_arr = b.as_string::<i32>();
        for i in 0..100 {
            assert_eq!(a_arr.value(i), b_arr.value(i), "mismatch at {i}");
        }
    }
}