relate-executor 1.4.0

In-memory graph and Cypher physical executor
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
//! Compact query row: one shared name schema, one value per slot.
//!
//! Rows produced by the same clause share the schema `Arc`. Cloning a row
//! clones the value vector and the schema pointer, not a fresh copy of
//! every variable name.
//!
//! The crate's lowest tier: every consumer — both walk engines
//! (`pattern_walk`'s bind family among them), the exec layer's operators,
//! the workspace's accounting — imports these types downward from
//! `crate::row`, the one public path per item. Hoisted from
//! `pattern_walk/row.rs` (itself hoisted from `eval/row.rs`, LABS-668):
//! the row schema is not walk vocabulary — every clause product is a row,
//! pattern-walked or not — so its home is the crate root, not the
//! pattern-walk module that once carried it.

use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock, Weak};

use relate_core::Value;
use relate_names::SmallName;

/// The most appended-name links one schema remembers: a hot bind site
/// extends a handful of shapes by one name, so a short list serves it
/// and the scan stays a few string compares.
const APPENDED_LINKS: usize = 8;

struct RowSchema {
    names: Vec<String>,
    index: HashMap<String, usize>,
    /// REL8-2242: the schemas this one became by appending one name —
    /// weak, so a link never keeps a shape alive past the thread-local
    /// cache's own bound. `Row::insert` of a new name reads it before the
    /// clone-every-name + hash-the-whole-list intern path.
    appended: Mutex<Vec<(String, Weak<RowSchema>)>>,
}

/// Schemas at most this wide resolve a name by scanning `names`: a few
/// length-checked string compares beat hashing the key (the std SipHash
/// dominated the per-row `get` on wide pattern queries), and the keys are
/// query variable names, so no hash seed is involved. Wider schemas keep
/// the indexed lookup.
const LINEAR_SLOTS: usize = 8;

impl Clone for RowSchema {
    /// A copy starts with no links: it may be mutated in place
    /// (`schema_mut`), and a link is only valid for the names it was
    /// recorded against.
    fn clone(&self) -> Self {
        Self {
            names: self.names.clone(),
            index: self.index.clone(),
            appended: Mutex::new(Vec::new()),
        }
    }
}

impl std::fmt::Debug for RowSchema {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RowSchema")
            .field("names", &self.names)
            .finish()
    }
}

impl PartialEq for RowSchema {
    fn eq(&self, other: &Self) -> bool {
        self.names == other.names
    }
}

impl Eq for RowSchema {}

impl RowSchema {
    fn slot(&self, key: &str) -> Option<usize> {
        if self.names.len() <= LINEAR_SLOTS {
            self.names.iter().position(|n| n == key)
        } else {
            self.index.get(key).copied()
        }
    }

    /// Drop the name at `slot`, moving the last name into its place, and keep `index` in step — the one mutation that owns the
    /// names/index invariant.
    fn swap_remove(&mut self, slot: usize) {
        let name = self.names.swap_remove(slot);
        self.index.remove(&name);
        // `slot` was the last name unless another name moved into it.
        if slot != self.names.len() {
            let swapped = self.names[slot].clone();
            self.index.insert(swapped, slot);
        }
    }

    fn empty() -> Arc<Self> {
        static EMPTY: OnceLock<Arc<RowSchema>> = OnceLock::new();
        Arc::clone(EMPTY.get_or_init(|| {
            Arc::new(RowSchema {
                names: Vec::new(),
                index: HashMap::new(),
                appended: Mutex::new(Vec::new()),
            })
        }))
    }

    fn from_names(names: &[String]) -> Arc<Self> {
        let mut index = HashMap::with_capacity(names.len());
        for (slot, name) in names.iter().enumerate() {
            index.insert(name.clone(), slot);
        }
        Arc::new(Self {
            names: names.to_vec(),
            index,
            appended: Mutex::new(Vec::new()),
        })
    }

    /// Whether this schema may carry append links: the empty schema is one
    /// process-wide `Arc` (`empty()`), so its one-name children come from
    /// the thread-local intern cache, never a link on the shared singleton.
    fn links_enabled(&self) -> bool {
        !self.names.is_empty()
    }

    /// The schema this one becomes with `key` appended, when a prior
    /// append recorded it and it is still alive.
    fn appended_with(&self, key: &str) -> Option<Arc<RowSchema>> {
        if !self.links_enabled() {
            return None;
        }
        let links = self.appended.lock().expect("row schema links poisoned");
        links
            .iter()
            .find(|(name, _)| name == key)
            .and_then(|(_, child)| child.upgrade())
    }

    /// Record that appending `key` yields `child` (dead links drop first;
    /// the oldest goes at the bound).
    fn record_appended(&self, key: &str, child: &Arc<RowSchema>) {
        if !self.links_enabled() {
            return;
        }
        let mut links = self.appended.lock().expect("row schema links poisoned");
        links.retain(|(_, weak)| weak.strong_count() > 0);
        if links.iter().any(|(name, _)| name == key) {
            return;
        }
        if links.len() >= APPENDED_LINKS {
            links.remove(0);
        }
        links.push((key.to_string(), Arc::downgrade(child)));
    }
}

/// The shared column names of one projected batch.
#[derive(Clone)]
pub struct RowNames(Arc<RowSchema>);

impl RowNames {
    pub fn new(names: &[String]) -> Self {
        Self(RowSchema::from_names(names))
    }

    /// The schema's column names, in slot order — the chunk layer's
    /// column projection reads them; every other consumer holds the names
    /// it built the schema from.
    pub(crate) fn names(&self) -> &[String] {
        &self.0.names
    }
}

/// The schema a row holds after a walk appends one pre-known set of
/// names — one rung of a walk's binding-schema ladder, [`Row::stamp`]'s
/// input. Built once per walk (the rung's names are the row's names plus
/// the appended set, in binding order); stamping any number of candidate
/// rows with it costs one `Arc` clone and the value pushes — the
/// new-name `insert` path (clone every name, intern the list in the
/// thread-local cache) never runs per candidate.
#[derive(Clone)]
pub(crate) struct RowRung(Arc<RowSchema>);

impl RowRung {
    /// The rung for a name list — walk-setup work, paid once per walk:
    /// the interned-schema cache serves repeats (a batch of walks over
    /// one entry schema asks for the same rungs).
    pub(crate) fn from_names(names: &[String]) -> Self {
        Self(interned_schema(names))
    }

    /// The rung for appending `bind_names` past `row`'s schema — the
    /// per-walk rung builds that read one row (the agg fold's hop rung,
    /// the existence probe's, the path-name bind's, the OPTIONAL-miss
    /// null binder's) instead of a pattern's whole element ladder. A
    /// name the row already binds appends nothing.
    pub(crate) fn past<'a>(
        row: &Row,
        bind_names: impl IntoIterator<Item = Option<&'a str>>,
    ) -> Self {
        let mut names: Vec<String> = row.keys().cloned().collect();
        for name in bind_names {
            push_bind_name(&mut names, name);
        }
        Self::from_names(&names)
    }
}

/// Push one bind name onto a walk's accumulated schema names unless the
/// walk already binds it — a row-bound variable appends nothing. The
/// one atom every binding-schema ladder and rung build reads, so the
/// precomputed schemas and the runtime binds agree by construction.
pub(crate) fn push_bind_name(names: &mut Vec<String>, name: Option<&str>) {
    if let Some(name) = name {
        if !names.iter().any(|bound| bound.as_str() == name) {
            names.push(name.to_string());
        }
    }
}
#[derive(Clone, Debug, PartialEq)]
pub struct Row {
    schema: Arc<RowSchema>,
    values: Vec<Value>,
}

impl Default for Row {
    fn default() -> Self {
        Self::new()
    }
}

impl Row {
    pub fn new() -> Self {
        Self {
            schema: RowSchema::empty(),
            values: Vec::new(),
        }
    }

    /// Every value sits in `names`'s order. Rows filled from the same
    /// `RowNames` share the schema allocation.
    pub fn from_names(names: &RowNames, values: Vec<Value>) -> Self {
        debug_assert_eq!(names.0.names.len(), values.len());
        Self {
            schema: Arc::clone(&names.0),
            values,
        }
    }

    /// Stamp `rung` onto this row: adopt the rung's precomputed schema —
    /// this row's names plus the rung's appended names — and push
    /// `values`, one per appended name, in the rung's order. The append
    /// path clones the schema `Arc` and pushes the values: no
    /// thread-local cache, no map, no per-name hashing. The debug check
    /// pins the prefix invariant — the rung's names must be this row's
    /// names plus the appended set — so a ladder/caller disagreement
    /// fails here instead of corrupting the row.
    pub(crate) fn stamp(&mut self, rung: &RowRung, values: impl IntoIterator<Item = Value>) {
        debug_assert!(
            self.values.len() <= rung.0.names.len()
                && rung.0.names[..self.values.len()] == self.schema.names[..],
            "the rung's schema must extend this row's schema"
        );
        self.schema = Arc::clone(&rung.0);
        self.values.extend(values);
        debug_assert_eq!(self.values.len(), self.schema.names.len());
    }

    /// Stamp `rung` onto this row with one null per appended name — the
    /// null-binding twin of [`Row::stamp`]: the rung's appended-name
    /// count (its schema's length past this row's own) is the null
    /// count, so a null-binding site (the OPTIONAL-miss binder) never
    /// re-derives the count from the row's key list beside the rung it
    /// just built. A rung that appends nothing stamps nothing.
    pub(crate) fn stamp_nulls(&mut self, rung: &RowRung) {
        let appended = rung.0.names.len() - self.values.len();
        if appended == 0 {
            return;
        }
        self.stamp(rung, std::iter::repeat_n(Value::Null, appended));
    }

    /// Remove `key` without preserving the order of the names after it.
    /// Matches `IndexMap::swap_remove`.
    pub fn swap_remove(&mut self, key: &str) -> Option<Value> {
        let slot = self.schema.slot(key)?;
        let removed = self.values.swap_remove(slot);
        self.schema_mut().swap_remove(slot);
        Some(removed)
    }

    pub fn len(&self) -> usize {
        self.values.len()
    }

    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    pub fn values(&self) -> &[Value] {
        &self.values
    }

    /// Capacity of the value buffer. The sink reserves exactly the column
    /// count; a test pins that so a size-hint-less collect cannot grow it
    /// back to the four-slot minimum.
    #[cfg(test)]
    pub(crate) fn values_capacity(&self) -> usize {
        self.values.capacity()
    }

    /// Identity of the shared schema allocation. Rows from one clause
    /// compare equal here, so a workspace walk charges that schema once.
    pub(crate) fn schema_ptr(&self) -> *const () {
        Arc::as_ptr(&self.schema) as *const ()
    }

    /// Bytes of this row's schema: the name vector, the index map, and
    /// both copies of each column name.
    pub(crate) fn schema_bytes(&self) -> u64 {
        use crate::memory::{reserved_len, OVERHEAD_MAP_ENTRY, VEC_HEADER};

        let names = self.schema.names.len();
        let mut bytes = if names == 0 {
            0
        } else {
            VEC_HEADER + reserved_len(names) as u64 * std::mem::size_of::<String>() as u64
        };
        bytes += self.schema.index.len() as u64 * OVERHEAD_MAP_ENTRY;
        for name in &self.schema.names {
            bytes = bytes.saturating_add(relate_core::str_logical_bytes(name).saturating_mul(2));
        }
        bytes
    }

    pub fn get(&self, key: &str) -> Option<&Value> {
        self.schema.slot(key).map(|slot| &self.values[slot])
    }

    /// Read a column through a [`SmallName`]: the one text bridge from
    /// the name representation to this row's String-keyed schema.
    pub fn get_name(&self, name: &SmallName) -> Option<&Value> {
        name.with_str(|text| self.get(text))
    }

    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        let slot = self.schema.slot(key)?;
        Some(&mut self.values[slot])
    }

    pub fn contains_key(&self, key: &str) -> bool {
        self.schema.slot(key).is_some()
    }

    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.schema.names.iter()
    }

    /// Point every row with the same column order at one schema.
    ///
    /// A match binds each output by cloning a seed and inserting a column,
    /// and that insert copies the schema. The copies are identical, so one
    /// map serves the whole batch. A later insert still copies on write.
    pub(crate) fn share_schemas(rows: &mut [Row]) {
        let Some(first) = rows.first() else {
            return;
        };
        let names = first.schema.names.clone();
        if rows.iter().skip(1).any(|row| row.schema.names != names) {
            return;
        }
        let shared = Arc::clone(&first.schema);
        for row in rows.iter_mut().skip(1) {
            row.schema = Arc::clone(&shared);
        }
    }

    pub fn insert(&mut self, key: String, value: Value) -> Option<Value> {
        if let Some(slot) = self.schema.slot(&key) {
            return Some(std::mem::replace(&mut self.values[slot], value));
        }
        if let Some(child) = self.schema.appended_with(&key) {
            self.schema = child;
            self.values.push(value);
            return None;
        }
        let mut names = self.schema.names.clone();
        names.push(key);
        let child = interned_schema(&names);
        let key = names.last().expect("the appended name was just pushed");
        self.schema.record_appended(key, &child);
        self.schema = child;
        self.values.push(value);
        None
    }

    /// Remove `key`, keeping the order of the names that remain.
    pub fn shift_remove(&mut self, key: &str) -> Option<Value> {
        let slot = self.schema.slot(key)?;
        let removed = std::mem::replace(&mut self.values[slot], Value::Null);
        let keep: Vec<usize> = (0..self.values.len()).filter(|i| *i != slot).collect();
        self.rebuild(keep);
        Some(removed)
    }

    /// Move `key`'s value out, leaving null in the slot. The schema stays
    /// shared with every other row from the same clause.
    pub fn take_value(&mut self, key: &str) -> Option<Value> {
        let slot = self.schema.slot(key)?;
        Some(std::mem::replace(&mut self.values[slot], Value::Null))
    }

    pub fn retain(&mut self, mut keep: impl FnMut(&String, &mut Value) -> bool) {
        let mut kept = Vec::with_capacity(self.values.len());
        for (slot, value) in self.values.iter_mut().enumerate() {
            if keep(&self.schema.names[slot], value) {
                kept.push(slot);
            }
        }
        if kept.len() != self.values.len() {
            self.rebuild(kept);
        }
    }

    pub fn entry(&mut self, key: String) -> RowEntry<'_> {
        let slot = self.schema.slot(&key);
        RowEntry {
            row: self,
            key,
            slot,
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
        self.schema.names.iter().zip(self.values.iter())
    }

    /// Each column's name with its value, mutable in place: the schema is
    /// shared and untouched, so a pass over many rows never rebuilds or
    /// re-interns one.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&String, &mut Value)> {
        self.schema.names.iter().zip(self.values.iter_mut())
    }

    fn schema_mut(&mut self) -> &mut RowSchema {
        if Arc::get_mut(&mut self.schema).is_none() {
            self.schema = Arc::new((*self.schema).clone());
        }
        let schema = Arc::get_mut(&mut self.schema).expect("schema was just made unique");
        // an in-place edit invalidates every recorded append link
        schema
            .appended
            .get_mut()
            .expect("row schema links poisoned")
            .clear();
        schema
    }

    fn rebuild(&mut self, keep: Vec<usize>) {
        let mut names = Vec::with_capacity(keep.len());
        let mut values = Vec::with_capacity(keep.len());
        for slot in keep {
            names.push(self.schema.names[slot].clone());
            values.push(std::mem::replace(&mut self.values[slot], Value::Null));
        }
        self.schema = RowSchema::from_names(&names);
        self.values = values;
    }
}

/// The row's bound-variable membership predicate — the row tier's one
/// spelling of "this name is bound in this row": a closure over the row,
/// no key cloning, read by every site that asks the question of the row
/// it walks from ([`crate::exec::pivot`]'s shared start estimates and
/// order-safety gate, [`crate::exec::expand`]'s scan-predicate compile).
/// It is row vocabulary, not start-selection vocabulary, so it lives
/// beside [`Row`] rather than in the first caller that needed it.
pub(crate) fn row_bound(row: &Row) -> impl Fn(&str) -> bool + '_ {
    |name: &str| row.contains_key(name)
}

// Counts `interned_schema` calls: the per-row schema cost a hot path must
// not pay. Test-only, so a test can assert a path interns at setup and
// never per row.
#[cfg(test)]
thread_local! {
    static INTERNED_SCHEMA_CALLS: std::cell::Cell<usize> =
        const { std::cell::Cell::new(0) };
}

#[cfg(test)]
pub(crate) fn interned_schema_calls() -> usize {
    INTERNED_SCHEMA_CALLS.with(std::cell::Cell::get)
}

/// One schema per column list on this thread. The remaining per-row
/// `insert` sites (comprehension bindings, the GPM walk's own binds,
/// the walks a binding-schema ladder cannot serve) build the same
/// shapes per row, and a private map per row was most of the create
/// family's resident bytes. The binding-schema ladders'
/// [`RowRung::from_names`] reads the same cache — once per walk, never
/// per candidate. The cache is cleared past 128 shapes so a long-lived
/// worker cannot keep every list it has seen.
fn interned_schema(names: &[String]) -> Arc<RowSchema> {
    #[cfg(test)]
    INTERNED_SCHEMA_CALLS.with(|calls| calls.set(calls.get() + 1));
    thread_local! {
        static CACHE: RefCell<HashMap<Vec<String>, Arc<RowSchema>>> =
            RefCell::new(HashMap::new());
    }
    CACHE.with(|cell| {
        let mut cache = cell.borrow_mut();
        if let Some(hit) = cache.get(names) {
            return Arc::clone(hit);
        }
        let schema = RowSchema::from_names(names);
        if cache.len() >= 128 {
            cache.clear();
        }
        cache.insert(names.to_vec(), Arc::clone(&schema));
        schema
    })
}

pub struct RowEntry<'a> {
    row: &'a mut Row,
    key: String,
    slot: Option<usize>,
}

impl<'a> RowEntry<'a> {
    pub fn or_insert(self, value: Value) -> &'a mut Value {
        self.or_insert_with(|| value)
    }

    pub fn or_insert_with<F>(self, make: F) -> &'a mut Value
    where
        F: FnOnce() -> Value,
    {
        if let Some(slot) = self.slot {
            return &mut self.row.values[slot];
        }
        self.row.insert(self.key, make());
        let slot = self.row.values.len() - 1;
        &mut self.row.values[slot]
    }
}

impl FromIterator<(String, Value)> for Row {
    fn from_iter<I: IntoIterator<Item = (String, Value)>>(iter: I) -> Self {
        let mut row = Row::new();
        for (key, value) in iter {
            row.insert(key, value);
        }
        row
    }
}

impl<const N: usize> From<[(String, Value); N]> for Row {
    fn from(entries: [(String, Value); N]) -> Self {
        entries.into_iter().collect()
    }
}

impl std::ops::Index<&str> for Row {
    type Output = Value;

    fn index(&self, key: &str) -> &Value {
        self.get(key)
            .unwrap_or_else(|| panic!("no entry found for key {key:?}"))
    }
}

impl<'a> IntoIterator for &'a Row {
    type Item = (&'a String, &'a Value);
    type IntoIter = std::iter::Zip<std::slice::Iter<'a, String>, std::slice::Iter<'a, Value>>;

    fn into_iter(self) -> Self::IntoIter {
        self.schema.names.iter().zip(self.values.iter())
    }
}

impl IntoIterator for Row {
    type Item = (String, Value);
    type IntoIter = std::vec::IntoIter<(String, Value)>;

    fn into_iter(self) -> Self::IntoIter {
        self.schema
            .names
            .iter()
            .cloned()
            .zip(self.values)
            .collect::<Vec<_>>()
            .into_iter()
    }
}

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

    #[test]
    fn rows_inserted_with_the_same_columns_share_a_schema() {
        let mut rows = Vec::new();
        for i in 0..32 {
            let mut row = Row::new();
            row.insert("s".into(), Value::Int(i));
            rows.push(row);
        }
        let ptr = rows[0].schema_ptr();
        assert!(rows.iter().all(|row| row.schema_ptr() == ptr));
        assert_eq!(rows[7].get("s"), Some(&Value::Int(7)));
    }

    proptest! {
        #![proptest_config(crate::test_support::config_with_floor(100))]

        /// The stamping primitive is the insert path's equal, generated:
        /// over generated seed names and binding sequences, rows built by
        /// `Row::insert` and rows stamped up one binding-schema ladder
        /// hold the same names in the same order, answer the same `get()`
        /// lookups, and carry equal values — and every row stamped up one
        /// ladder shares that ladder's final schema `Arc` (`schema_ptr`).
        /// The binding names are fresh by construction (index-suffixed):
        /// the walk binds a row-bound name never — it appends nothing —
        /// so both paths apply the same append-only sequence.
        #[test]
        fn stamped_rows_match_insert_built_rows(
            seed in proptest::collection::vec(("[a-z]{1,5}", any::<i64>()), 0..8),
            binds in proptest::collection::vec(("[a-z]{1,5}", any::<i64>()), 0..12),
        ) {
            let seed: Vec<(String, Value)> = seed
                .into_iter()
                .enumerate()
                .map(|(i, (name, v))| (format!("s{name}{i}"), Value::Int(v)))
                .collect();
            let binds: Vec<(String, Value)> = binds
                .into_iter()
                .enumerate()
                .map(|(i, (name, v))| (format!("b{name}{i}"), Value::Int(v)))
                .collect();

            // the insert-built baseline
            let mut inserted = Row::new();
            for (name, value) in seed.iter().chain(binds.iter()) {
                inserted.insert(name.clone(), value.clone());
            }

            // the ladder: rung i is the schema after seed + binds[..=i]
            let mut names: Vec<String> = seed.iter().map(|(name, _)| name.clone()).collect();
            let rungs: Vec<RowRung> = binds
                .iter()
                .map(|(name, _)| {
                    push_bind_name(&mut names, Some(name));
                    RowRung::from_names(&names)
                })
                .collect();

            // several rows stamped up the one ladder
            let stamped: Vec<Row> = (0..3)
                .map(|_| {
                    let mut row = Row::new();
                    for (name, value) in &seed {
                        row.insert(name.clone(), value.clone());
                    }
                    for ((_, value), rung) in binds.iter().zip(&rungs) {
                        row.stamp(rung, [value.clone()]);
                    }
                    row
                })
                .collect();

            for row in &stamped {
                prop_assert_eq!(
                    row.keys().cloned().collect::<Vec<_>>(),
                    inserted.keys().cloned().collect::<Vec<_>>()
                );
                prop_assert_eq!(row.values(), inserted.values());
                for (name, value) in inserted.iter() {
                    prop_assert_eq!(row.get(name), Some(value));
                }
            }
            let shared = stamped[0].schema_ptr();
            prop_assert!(stamped.iter().all(|row| row.schema_ptr() == shared));
        }
    }
}