lix 0.17.1

Embeddable version control for apps and AI agents.
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
//! Registered-schema-bound columnar sidecars for immutable row generations.
//!
//! The transaction boundary calls this adapter while validated canonical
//! snapshots and typed row identities are still available. SQL's existing
//! projection decoder remains the single value-conversion contract; this
//! module only chooses physical row groups and delegates their encoding.

use crate::row_columnar::{EncodedRowGroups, RowGroupLocations};
#[cfg(test)]
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use datafusion::arrow::array::{ArrayRef, StringArray};
use datafusion::arrow::datatypes::{DataType, Field, Schema};
use datafusion::arrow::record_batch::RecordBatch;
#[cfg(test)]
use serde_json::Value as JsonValue;

use crate::LixError;
#[cfg(test)]
use crate::columnar_row_group::RowGroupRowLocation;
use crate::columnar_row_group::{ROW_GROUP_MAX_ROWS, encode_row_group_set_preserving_batches};
#[cfg(test)]
use crate::row_pk::RowPk;
#[cfg(test)]
use crate::sql2::RowProjectionDecoder;
use crate::sql2::{SchemaColumnType, SchemaSurfaceSpec, row_visible_fields};

pub(crate) const ROW_COLUMNAR_LAYOUT_FINGERPRINT_METADATA_KEY: &str =
    "lix.row_columnar.layout_fingerprint.v1";
pub(crate) const ROW_COLUMNAR_BASE_COORDINATES_METADATA_KEY: &str =
    "lix.row_columnar.base_coordinates.v1";
pub(crate) use crate::row_columnar::{
    ROW_COLUMNAR_IDENTITY_FIELD, ROW_COLUMNAR_LOSSLESS_SNAPSHOT_METADATA_KEY,
};
pub(crate) const LOW_CARDINALITY_CLUSTER_MAX_VALUES: usize = 64;
#[cfg(test)]
const LOW_CARDINALITY_CLUSTER_MAX_BUCKETS: usize = 8;
#[cfg(test)]
const ROW_COLUMNAR_MAX_CLUSTER_PARTITIONS: usize = 64;

#[cfg(test)]
enum ClusterField<'a> {
    Boolean(&'a str),
    String(&'a str, BTreeMap<String, u8>),
}

#[cfg(test)]
#[derive(Clone, Copy)]
pub(crate) struct RowColumnarRowRef<'a> {
    pub(crate) row_pk: &'a RowPk,
    pub(crate) snapshot_bytes: Option<&'a [u8]>,
    pub(crate) snapshot_value: Option<&'a JsonValue>,
    pub(crate) typed_row: Option<&'a lix_schema::Row>,
}

#[cfg(test)]
impl RowColumnarRowRef<'_> {
    fn boolean(&self, name: &str) -> Option<bool> {
        self.typed_row
            .and_then(|row| match row.get(name) {
                Some(lix_schema::Value::Boolean(value)) => Some(*value),
                _ => None,
            })
            .or_else(|| self.snapshot_value?.get(name)?.as_bool())
    }

    fn string(&self, name: &str) -> Option<&str> {
        self.typed_row
            .and_then(|row| match row.get(name) {
                Some(lix_schema::Value::Text(value)) => Some(value.as_str()),
                _ => None,
            })
            .or_else(|| self.snapshot_value?.get(name)?.as_str())
    }
}

// Independent fixture encoder for projection, corruption, and layout equivalence tests.
// Runtime publication uses the certified unclustered encoder or canonical rows.
#[cfg(test)]
pub(crate) fn encode_registered_row_groups<'a, I>(
    spec: &SchemaSurfaceSpec,
    rows: I,
) -> Result<Option<EncodedRowGroups>, LixError>
where
    I: ExactSizeIterator<Item = RowColumnarRowRef<'a>>,
{
    if rows.len() == 0 {
        return Ok(None);
    }
    // This is a derived acceleration structure. Projection or physical-limit
    // failures must retain the authoritative row layout rather than reject an
    // otherwise-valid transaction.
    Ok(optional_derived_row_group_set(
        encode_registered_row_groups_impl(spec, rows),
    ))
}

/// Encodes frontend-owned Arrow columns without reconstructing them from
/// canonical snapshot JSON. The fast contract is deliberately limited to
/// layouts whose established encoder would not reorder rows for clustering;
/// clustered layouts retain canonical row staging and identical physical
/// behavior.
pub(crate) fn encode_unclustered_registered_row_groups(
    spec: &SchemaSurfaceSpec,
    mut columns: Vec<ArrayRef>,
    row_pks: ArrayRef,
) -> Result<Option<EncodedRowGroups>, LixError> {
    if columns.len() != spec.columns.len() {
        return Err(row_columnar_error(
            "frontend column count does not match the registered schema",
        ));
    }
    let row_count = row_pks.len();
    if row_count == 0 || columns.iter().any(|column| column.len() != row_count) {
        return Err(row_columnar_error(
            "frontend columns are empty or have inconsistent row counts",
        ));
    }
    let primary_key_roots = spec
        .primary_key_paths
        .iter()
        .filter_map(|path| path.first().map(String::as_str))
        .collect::<std::collections::BTreeSet<_>>();
    for (spec_column, array) in spec.columns.iter().zip(&columns) {
        if spec_column.column_type == SchemaColumnType::Boolean {
            return Ok(None);
        }
        if spec_column.column_type != SchemaColumnType::String
            || primary_key_roots.contains(spec_column.name.as_str())
        {
            continue;
        }
        let Some(strings) = array.as_any().downcast_ref::<StringArray>() else {
            return Ok(None);
        };
        let mut values = std::collections::BTreeSet::new();
        for value in strings.iter().flatten() {
            values.insert(value);
            if values.len() > LOW_CARDINALITY_CLUSTER_MAX_VALUES {
                break;
            }
        }
        if (2..=LOW_CARDINALITY_CLUSTER_MAX_VALUES).contains(&values.len()) {
            return Ok(None);
        }
    }

    let mut fields = row_visible_fields(spec);
    fields.push(Field::new(
        ROW_COLUMNAR_IDENTITY_FIELD,
        DataType::Utf8,
        false,
    ));
    let metadata = row_columnar_metadata(spec);
    let schema = Arc::new(Schema::new_with_metadata(fields, metadata));
    columns.push(row_pks);
    let mut batches = Vec::with_capacity(row_count.div_ceil(ROW_GROUP_MAX_ROWS));
    for offset in (0..row_count).step_by(ROW_GROUP_MAX_ROWS) {
        let len = (row_count - offset).min(ROW_GROUP_MAX_ROWS);
        batches.push(
            RecordBatch::try_new(
                Arc::clone(&schema),
                columns
                    .iter()
                    .map(|column| column.slice(offset, len))
                    .collect(),
            )
            .map_err(|error| row_columnar_error(error.to_string()))?,
        );
    }
    let encoded = encode_row_group_set_preserving_batches(&spec.schema_key, schema, &batches)?;
    Ok(Some(EncodedRowGroups {
        encoded,
        input_locations: RowGroupLocations::Dense { row_count },
    }))
}

#[cfg(test)]
fn encode_registered_row_groups_impl<'a, I>(
    spec: &SchemaSurfaceSpec,
    rows: I,
) -> Result<EncodedRowGroups, LixError>
where
    I: ExactSizeIterator<Item = RowColumnarRowRef<'a>>,
{
    let mut fields = row_visible_fields(spec);
    fields.push(Field::new(
        ROW_COLUMNAR_IDENTITY_FIELD,
        DataType::Utf8,
        false,
    ));
    let metadata = row_columnar_metadata(spec);
    let schema = Arc::new(Schema::new_with_metadata(fields, metadata));
    let decoder =
        RowProjectionDecoder::new(spec, spec.columns.iter().map(|column| column.name.as_str()))?;

    let rows = rows.enumerate().collect::<Vec<_>>();
    let primary_key_roots = spec
        .primary_key_paths
        .iter()
        .filter_map(|path| path.first().map(String::as_str))
        .collect::<std::collections::BTreeSet<_>>();
    let mut cluster_fields = Vec::new();
    let mut partition_budget = 1_usize;
    for column in &spec.columns {
        if column.column_type == SchemaColumnType::Boolean
            && partition_budget.saturating_mul(3) <= ROW_COLUMNAR_MAX_CLUSTER_PARTITIONS
        {
            cluster_fields.push(ClusterField::Boolean(column.name.as_str()));
            partition_budget *= 3;
        }
    }
    for column in &spec.columns {
        if column.column_type != SchemaColumnType::String
            || primary_key_roots.contains(column.name.as_str())
        {
            continue;
        }
        let mut values = std::collections::BTreeSet::new();
        for (_, row) in &rows {
            if let Some(value) = row.string(&column.name) {
                values.insert(value.to_owned());
                if values.len() > LOW_CARDINALITY_CLUSTER_MAX_VALUES {
                    break;
                }
            }
        }
        if (2..=LOW_CARDINALITY_CLUSTER_MAX_VALUES).contains(&values.len()) {
            let value_count = values.len();
            let bucket_count = value_count.min(LOW_CARDINALITY_CLUSTER_MAX_BUCKETS);
            // Reserve one state for null, missing, or non-string values. Even
            // when none are present in this generation, charging the full
            // key domain keeps the global budget independent of row shape.
            let partition_count = bucket_count + 1;
            if partition_budget.saturating_mul(partition_count)
                > ROW_COLUMNAR_MAX_CLUSTER_PARTITIONS
            {
                continue;
            }
            partition_budget *= partition_count;
            cluster_fields.push(ClusterField::String(
                column.name.as_str(),
                values
                    .into_iter()
                    .enumerate()
                    .map(|(index, value)| {
                        let bucket = index.saturating_mul(bucket_count) / value_count;
                        (value, bucket as u8)
                    })
                    .collect(),
            ));
        }
    }
    let partitions = if cluster_fields.is_empty() {
        vec![rows]
    } else {
        let mut partitions = BTreeMap::<Vec<u8>, Vec<(usize, RowColumnarRowRef<'_>)>>::new();
        for (input_index, row) in rows {
            let key = cluster_fields
                .iter()
                .map(|field| match field {
                    ClusterField::Boolean(name) => match row.boolean(name) {
                        Some(false) => 0,
                        Some(true) => 1,
                        None => 2,
                    },
                    ClusterField::String(name, dictionary) => row
                        .string(name)
                        .and_then(|value| dictionary.get(value).copied())
                        .unwrap_or(u8::MAX),
                })
                .collect();
            partitions.entry(key).or_default().push((input_index, row));
        }
        partitions.into_values().collect()
    };

    let input_count = partitions.iter().map(Vec::len).sum();
    let mut input_locations = vec![None; input_count];
    let mut batches = Vec::new();
    for partition in partitions {
        for rows in partition.chunks(ROW_GROUP_MAX_ROWS) {
            let group_index = u32::try_from(batches.len())
                .map_err(|_| row_columnar_error("row-group index exceeds u32"))?;
            for (row_index, (input_index, _)) in rows.iter().enumerate() {
                input_locations[*input_index] = Some(RowGroupRowLocation {
                    group_index,
                    row_index: u32::try_from(row_index)
                        .map_err(|_| row_columnar_error("row index exceeds u32"))?,
                });
            }
            let mut columns = decoder.decode_mixed_arrow_columns(
                rows.iter()
                    .map(|(_, row)| (row.snapshot_bytes, row.typed_row)),
            )?;
            let row_pks = rows
                .iter()
                .map(|(_, row)| row.row_pk.as_json_array_text())
                .collect::<Result<Vec<_>, _>>()?;
            let row_pks: ArrayRef = Arc::new(StringArray::from(row_pks));
            columns.push(row_pks);
            batches.push(
                RecordBatch::try_new(Arc::clone(&schema), columns)
                    .map_err(|error| row_columnar_error(error.to_string()))?,
            );
        }
    }
    let encoded = encode_row_group_set_preserving_batches(&spec.schema_key, schema, &batches)?;
    Ok(EncodedRowGroups {
        encoded,
        input_locations: RowGroupLocations::Explicit(
            input_locations
                .into_iter()
                .collect::<Option<Vec<_>>>()
                .ok_or_else(|| row_columnar_error("row-group permutation omitted an input row"))?,
        ),
    })
}

#[cfg(test)]
fn optional_derived_row_group_set(
    encoded: Result<EncodedRowGroups, LixError>,
) -> Option<EncodedRowGroups> {
    encoded.ok()
}

fn row_columnar_metadata(spec: &SchemaSurfaceSpec) -> HashMap<String, String> {
    let mut metadata = HashMap::from([
        (
            "lix.schema_v1.fingerprint".to_owned(),
            blake3::Hash::from_bytes(spec.schema_fingerprint)
                .to_hex()
                .to_string(),
        ),
        (
            ROW_COLUMNAR_LAYOUT_FINGERPRINT_METADATA_KEY.to_string(),
            spec.columnar_layout_fingerprint(),
        ),
        (
            ROW_COLUMNAR_BASE_COORDINATES_METADATA_KEY.to_string(),
            "true".to_owned(),
        ),
    ]);
    if spec.columnar_snapshot_bijective {
        metadata.insert(
            ROW_COLUMNAR_LOSSLESS_SNAPSHOT_METADATA_KEY.to_string(),
            "true".to_owned(),
        );
    }
    metadata
}

fn row_columnar_error(message: impl Into<String>) -> LixError {
    LixError::new(
        LixError::CODE_INTERNAL_ERROR,
        format!("row columnar layout: {}", message.into()),
    )
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;
    use crate::columnar_row_group::RowGroupScalar;
    #[cfg(test)]
    use crate::row_pk::RowPk;
    use crate::sql2::derive_schema_surface_spec_from_schema;

    #[test]
    fn registered_types_and_hidden_identity_round_trip() {
        let spec = derive_schema_surface_spec_from_schema(&json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "typed_sidecar",
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
                { "name": "ordinal", "type": "int8", "nullable": false },
                { "name": "score", "type": "float8", "nullable": false },
                { "name": "active", "type": "boolean", "nullable": false },
                { "name": "payload", "type": "jsonb", "nullable": true },
            ],
            "primary_key": ["id", "ordinal"],
        }))
        .expect("spec");
        let snapshots = [
            json!({"id":"a","ordinal":1,"score":1.5,"active":true,"payload":{"z":2,"a":1}}),
            json!({"id":"b","ordinal":2,"score":2,"active":false,"payload":null}),
        ];
        let identities = [
            RowPk::from_json_array_value(&json!(["a", 1])).expect("first identity"),
            RowPk::from_json_array_value(&json!(["b", 2])).expect("second identity"),
        ];
        let canonical = snapshots
            .iter()
            .map(JsonValue::to_string)
            .collect::<Vec<_>>();
        let encoded = encode_registered_row_groups(
            &spec,
            identities.iter().zip(&snapshots).zip(&canonical).map(
                |((row_pk, snapshot), canonical)| RowColumnarRowRef {
                    row_pk,
                    snapshot_bytes: Some(canonical.as_bytes()),
                    snapshot_value: Some(snapshot),
                    typed_row: None,
                },
            ),
        )
        .expect("encode")
        .expect("registered sidecar");
        assert_eq!(
            encoded
                .manifest
                .metadata
                .get(ROW_COLUMNAR_LAYOUT_FINGERPRINT_METADATA_KEY),
            Some(&spec.columnar_layout_fingerprint())
        );
        assert_eq!(
            encoded
                .manifest
                .metadata
                .get(ROW_COLUMNAR_BASE_COORDINATES_METADATA_KEY)
                .map(String::as_str),
            Some("true")
        );
        let identity_index = encoded
            .manifest
            .fields
            .iter()
            .position(|field| field.name == ROW_COLUMNAR_IDENTITY_FIELD)
            .expect("hidden identity field");
        assert_eq!(
            encoded.manifest.fields[identity_index].data_type.to_arrow(),
            DataType::Utf8
        );
        let identities = encoded
            .manifest
            .groups
            .iter()
            .filter_map(|group| group.columns[identity_index].min.as_ref())
            .map(|value| match value {
                RowGroupScalar::String(value) => value.as_str(),
                _ => panic!("identity must have string statistics"),
            })
            .collect::<std::collections::BTreeSet<_>>();
        assert_eq!(
            identities,
            std::collections::BTreeSet::from([r#"["a",1]"#, r#"["b",2]"#])
        );
    }

    #[test]
    fn frontend_columns_match_canonical_encoding_when_clustering_is_absent() {
        let spec = derive_schema_surface_spec_from_schema(&json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "direct_columns",
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
                { "name": "value", "type": "text", "nullable": false },
            ],
            "primary_key": ["id"],
        }))
        .expect("schema should derive");
        let ids = (0..128)
            .map(|index| format!("id-{index:04}"))
            .collect::<Vec<_>>();
        let values = (0..128)
            .map(|index| format!("value-{index:04}"))
            .collect::<Vec<_>>();
        let snapshots = ids
            .iter()
            .zip(&values)
            .map(|(id, value)| json!({"id": id, "value": value}))
            .collect::<Vec<_>>();
        let canonical = snapshots
            .iter()
            .map(serde_json::to_string)
            .collect::<Result<Vec<_>, _>>()
            .expect("snapshots should encode");
        let identities = ids
            .iter()
            .map(|id| RowPk::from_validated_shared_string(id.as_str().into()))
            .collect::<Vec<_>>();
        let canonical_encoding = encode_registered_row_groups(
            &spec,
            identities.iter().zip(&snapshots).zip(&canonical).map(
                |((row_pk, snapshot), canonical)| RowColumnarRowRef {
                    row_pk,
                    snapshot_bytes: Some(canonical.as_bytes()),
                    snapshot_value: Some(snapshot),
                    typed_row: None,
                },
            ),
        )
        .expect("canonical encoding should succeed")
        .expect("canonical encoding should exist");
        let direct_encoding = encode_unclustered_registered_row_groups(
            &spec,
            vec![
                Arc::new(StringArray::from(ids.clone())),
                Arc::new(StringArray::from(values)),
            ],
            Arc::new(StringArray::from(
                identities
                    .iter()
                    .map(RowPk::as_json_array_text)
                    .collect::<Result<Vec<_>, _>>()
                    .expect("identities should encode"),
            )),
        )
        .expect("direct encoding should succeed")
        .expect("high-cardinality values should not cluster");
        assert!(matches!(
            &direct_encoding.input_locations,
            RowGroupLocations::Dense { row_count: 128 }
        ));
        assert_eq!(direct_encoding.manifest, canonical_encoding.manifest);
        assert_eq!(
            direct_encoding.input_locations,
            canonical_encoding.input_locations
        );
    }

    #[test]
    fn frontend_json_columns_match_canonical_path_value_encoding() {
        let spec = derive_schema_surface_spec_from_schema(&json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "path_value_columns",
            "columns": [
                { "name": "path", "type": "text", "nullable": false },
                { "name": "value", "type": "jsonb", "nullable": false },
            ],
            "primary_key": ["path"],
        }))
        .expect("schema should derive");
        let paths = (0..128)
            .map(|index| format!("/path/{index:04}"))
            .collect::<Vec<_>>();
        let json_values = (0..128)
            .map(|index| json!({"index": index, "nested": [index, index + 1]}))
            .collect::<Vec<_>>();
        let snapshots = paths
            .iter()
            .zip(&json_values)
            .map(|(path, value)| json!({"path": path, "value": value}))
            .collect::<Vec<_>>();
        let canonical = snapshots
            .iter()
            .map(serde_json::to_string)
            .collect::<Result<Vec<_>, _>>()
            .expect("snapshots should encode");
        let identities = paths
            .iter()
            .map(|path| RowPk::from_validated_shared_string(path.as_str().into()))
            .collect::<Vec<_>>();
        let canonical_encoding = encode_registered_row_groups(
            &spec,
            identities.iter().zip(&snapshots).zip(&canonical).map(
                |((row_pk, snapshot), canonical)| RowColumnarRowRef {
                    row_pk,
                    snapshot_bytes: Some(canonical.as_bytes()),
                    snapshot_value: Some(snapshot),
                    typed_row: None,
                },
            ),
        )
        .expect("canonical encoding should succeed")
        .expect("canonical encoding should exist");
        let direct_encoding = encode_unclustered_registered_row_groups(
            &spec,
            vec![
                Arc::new(StringArray::from(paths.clone())),
                Arc::new(StringArray::from(
                    json_values
                        .iter()
                        .map(serde_json::to_string)
                        .collect::<Result<Vec<_>, _>>()
                        .expect("JSON values should encode"),
                )),
            ],
            Arc::new(StringArray::from(
                identities
                    .iter()
                    .map(RowPk::as_json_array_text)
                    .collect::<Result<Vec<_>, _>>()
                    .expect("identities should encode"),
            )),
        )
        .expect("direct encoding should succeed")
        .expect("path/value columns should not cluster");
        assert_eq!(direct_encoding.manifest, canonical_encoding.manifest);
        assert_eq!(
            direct_encoding.input_locations,
            canonical_encoding.input_locations
        );
    }

    #[test]
    fn input_coordinates_follow_the_clustered_physical_permutation() {
        let spec = derive_schema_surface_spec_from_schema(&json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "coordinate_fixture",
            "columns": [
                { "name": "id", "type": "text", "nullable": false },
                { "name": "active", "type": "boolean", "nullable": false },
                { "name": "lane", "type": "text", "nullable": false },
            ],
            "primary_key": ["id"],
        }))
        .expect("spec");
        // Deliberately interleave clustering values so physical order differs
        // from authoritative input order.
        let snapshots = [
            json!({"id":"a","active":true,"lane":"z"}),
            json!({"id":"b","active":false,"lane":"a"}),
            json!({"id":"c","active":true,"lane":"y"}),
            json!({"id":"d","active":false,"lane":"b"}),
        ];
        let identities = ["a", "b", "c", "d"].map(RowPk::single);
        let canonical = snapshots
            .iter()
            .map(JsonValue::to_string)
            .collect::<Vec<_>>();
        let encoded = encode_registered_row_groups(
            &spec,
            identities.iter().zip(&snapshots).zip(&canonical).map(
                |((row_pk, snapshot), canonical)| RowColumnarRowRef {
                    row_pk,
                    snapshot_bytes: Some(canonical.as_bytes()),
                    snapshot_value: Some(snapshot),
                    typed_row: None,
                },
            ),
        )
        .expect("encode")
        .expect("registered sidecar");
        let identity_index = encoded
            .manifest
            .fields
            .iter()
            .position(|field| field.name == ROW_COLUMNAR_IDENTITY_FIELD)
            .expect("hidden identity field");

        assert_eq!(encoded.input_locations.len(), identities.len());
        assert_ne!(
            encoded
                .input_locations
                .location(0)
                .expect("first input coordinate")
                .group_index,
            encoded
                .input_locations
                .location(1)
                .expect("second input coordinate")
                .group_index
        );
        for (input_index, location) in encoded.input_locations.iter().enumerate() {
            let group = &encoded.manifest.groups[location.group_index as usize];
            let expected = identities[input_index]
                .as_json_array_text()
                .expect("identity text");
            assert_eq!(
                group.columns[identity_index].min,
                Some(RowGroupScalar::String(expected.clone()))
            );
            assert_eq!(
                group.columns[identity_index].max,
                Some(RowGroupScalar::String(expected))
            );
        }
    }

    #[test]
    fn derived_encoding_failure_falls_back_without_rejecting_authoritative_rows() {
        assert!(
            optional_derived_row_group_set(Err(row_columnar_error("physical limit"))).is_none()
        );
    }

    #[test]
    fn any_json_property_encodes_in_registered_layout() {
        let spec = derive_schema_surface_spec_from_schema(&json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "json_layout",
            "columns": [
                { "name": "path", "type": "text", "nullable": false },
                { "name": "value", "type": "jsonb", "nullable": false },
            ],
            "primary_key": ["path"],
        }))
        .expect("spec");
        let snapshot = json!({"path":"a","value":"value-a"});
        let canonical = snapshot.to_string();
        let identity = RowPk::single("a");
        assert!(
            encode_registered_row_groups(
                &spec,
                std::iter::once(RowColumnarRowRef {
                    row_pk: &identity,
                    snapshot_bytes: Some(canonical.as_bytes()),
                    snapshot_value: Some(&snapshot),
                    typed_row: None,
                }),
            )
            .expect("encode")
            .is_some()
        );
    }

    #[test]
    fn clustering_has_a_global_partition_budget_for_wide_low_cardinality_schemas() {
        let mut columns = vec![json!({ "name": "id", "type": "text", "nullable": false })];
        for index in 0..2 {
            columns.push(json!({
                "name": format!("flag_{index}"), "type": "boolean", "nullable": false
            }));
        }
        for index in 0..4 {
            columns.push(json!({
                "name": format!("lane_{index}"), "type": "text", "nullable": true
            }));
        }
        let spec = derive_schema_surface_spec_from_schema(&json!({
            "$schema": "https://lix.dev/schema-v1.json",
            "key": "wide_low_cardinality",
            "columns": columns,
            "primary_key": ["id"]
        }))
        .expect("spec");
        let snapshots = (0..1_024)
            .map(|row| {
                let mut snapshot = serde_json::Map::new();
                snapshot.insert("id".to_string(), json!(format!("row-{row}")));
                for index in 0..2 {
                    snapshot.insert(format!("flag_{index}"), json!(((row >> index) & 1) == 1));
                }
                for index in 0..4 {
                    let divisor = 4 * 3_usize.pow(index);
                    match (row / divisor) % 3 {
                        0 => {
                            snapshot.insert(format!("lane_{index}"), json!("lane-a"));
                        }
                        1 => {
                            snapshot.insert(format!("lane_{index}"), json!("lane-b"));
                        }
                        _ => {}
                    }
                }
                JsonValue::Object(snapshot)
            })
            .collect::<Vec<_>>();
        let identities = (0..snapshots.len())
            .map(|row| RowPk::from_json_array_value(&json!([format!("row-{row}")])).unwrap())
            .collect::<Vec<_>>();
        let canonical = snapshots
            .iter()
            .map(JsonValue::to_string)
            .collect::<Vec<_>>();

        let encoded = encode_registered_row_groups(
            &spec,
            identities.iter().zip(&snapshots).zip(&canonical).map(
                |((row_pk, snapshot), canonical)| RowColumnarRowRef {
                    row_pk,
                    snapshot_bytes: Some(canonical.as_bytes()),
                    snapshot_value: Some(snapshot),
                    typed_row: None,
                },
            ),
        )
        .expect("encode")
        .expect("registered sidecar");

        assert!(encoded.manifest.groups.len() > 1);
        assert!(
            encoded.manifest.groups.len() <= ROW_COLUMNAR_MAX_CLUSTER_PARTITIONS,
            "wide independent dimensions created {} groups despite a {}-partition budget",
            encoded.manifest.groups.len(),
            ROW_COLUMNAR_MAX_CLUSTER_PARTITIONS
        );
    }
}