df-derive 0.3.0

Derive fast conversions from Rust structs into Polars DataFrames.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
use crate::core::dataframe::{Decimal128Encode, ToDataFrame, ToDataFrameVec};
use chrono::{NaiveDate, NaiveTime};
use df_derive::ToDataFrame;
use polars::prelude::*;
use std::sync::Arc;

// 1. Bare tuple — every primitive shape
#[derive(ToDataFrame, Clone)]
struct BareTuple {
    pair: (String, i32),
    triple: (f64, f64, bool),
    single: (i32,),
}

// 2. Option<tuple>
#[derive(ToDataFrame, Clone)]
struct OptTuple {
    pair: Option<(String, i32)>,
}

// 3. Vec<tuple>
#[derive(ToDataFrame, Clone)]
struct VecTuple {
    pairs: Vec<(String, i32)>,
}

// 4. Vec<Option<tuple>>
#[derive(ToDataFrame, Clone)]
struct VecOptTuple {
    items: Vec<Option<(String, i32)>>,
}

// 5. Option<Vec<tuple>>
#[derive(ToDataFrame, Clone)]
struct OptVecTuple {
    pairs: Option<Vec<(String, i32)>>,
}

// 6. Tuple in tuple struct
#[derive(ToDataFrame, Clone)]
struct WithTuple(i32, (String, bool));

// 7. Smart pointer in element
#[derive(ToDataFrame, Clone)]
struct SmartPtrTuple {
    pair: (Box<i32>, String),
}

// 8. Vec<(Arc<u64>, String)> — smart pointer + Vec
#[derive(ToDataFrame, Clone)]
struct VecSmartPtrTuple {
    items: Vec<(Arc<u64>, String)>,
}

// 9. Temporal composition
#[derive(ToDataFrame, Clone)]
struct TemporalTuple {
    times: (NaiveDate, NaiveTime),
    durs: Vec<(chrono::Duration, String)>,
}

// 10. Nested tuples (no parent wrappers)
#[derive(ToDataFrame, Clone)]
struct NestedTuple {
    nested: ((i32, String), bool),
}

// 11. Regression: HashMap rejection's hint now actionable. The hint says
// "Convert to Vec<(K, V)>"; verify the converted form compiles.
#[derive(ToDataFrame, Clone)]
struct ConvertedHashMap {
    metadata: Vec<(String, String)>,
}

// 12. Tuple element of nested-struct type (no parent wrappers).
#[derive(ToDataFrame, Clone)]
struct Inner {
    a: i32,
    b: f64,
}

#[derive(ToDataFrame, Clone)]
struct WithInnerTuple {
    pair: (Inner, i32),
}

// 13. Tuple element of nested-struct type under a Vec parent.
#[derive(ToDataFrame, Clone)]
struct VecInnerTuple {
    items: Vec<(Inner, i32)>,
}

// 14. Box<tuple> — smart pointer wrapping the tuple itself.
#[derive(ToDataFrame, Clone)]
struct BoxTuple {
    pair: Box<(String, i32)>,
}

// 15. Tuple element with its own Vec wrapper under a Vec parent.
#[derive(ToDataFrame, Clone)]
struct VecTupleWithVecElement {
    items: Vec<(Vec<i32>, String)>,
}

// 16. Parent Vec element is optional, and the tuple element has its own Vec.
#[derive(ToDataFrame, Clone)]
struct VecOptTupleWithVecElement {
    items: Vec<Option<(Vec<i32>, String)>>,
}

// 17. Tuple element carries Option<Vec<_>> under a Vec parent.
#[derive(ToDataFrame, Clone)]
struct VecTupleWithOptVecElement {
    items: Vec<(Option<Vec<i32>>, String)>,
}

// 18. Optional tuple whose elements have non-Copy wrapper stacks.
#[derive(ToDataFrame, Clone)]
#[allow(clippy::type_complexity)]
struct OptTupleWithWrappedElements {
    pair: Option<(Vec<i32>, Option<Box<i32>>, Vec<Box<Option<i32>>>)>,
}

// 19. Parent tuple has interleaved Option / smart pointer / Option wrappers.
#[derive(ToDataFrame, Clone)]
struct OptionBoxOptionTuple {
    x: Option<Box<Option<(i32, String)>>>,
}

// 20. Parent tuple item has an Option and smart pointer above the tuple, and
// the projected element has its own Vec layer.
#[derive(ToDataFrame, Clone)]
#[allow(clippy::type_complexity)]
struct VecOptBoxTupleWithVecElement {
    items: Vec<Option<Box<(Vec<i32>, String)>>>,
}

// 21. Parent tuple is required, while a projected primitive element is optional.
#[derive(ToDataFrame, Clone)]
struct VecTupleWithOptionalElement {
    xs: Vec<(Option<i32>, String)>,
}

// 22. Borrowed string tuple elements lower to LeafSpec::AsStr without attrs.
#[derive(ToDataFrame, Clone)]
struct BorrowedStrTupleVec<'a> {
    xs: Vec<(&'a str, i32)>,
}

// 23. Parent Option tuple with borrowed Copy element.
#[derive(ToDataFrame, Clone)]
struct OptTupleWithBorrowedCopy<'a> {
    pair: Option<(&'a i32, bool)>,
}

// 24. Parent Option tuple with boxed Copy element.
#[derive(ToDataFrame, Clone)]
struct OptTupleWithBoxedCopy {
    pair: Option<(Box<i32>,)>,
}

// 25. Parent Option tuple with boxed non-Copy element.
#[derive(ToDataFrame, Clone)]
#[allow(clippy::box_collection)]
struct OptTupleWithBoxedString {
    pair: Option<(Box<String>,)>,
}

// 26. Parent Option tuple whose projected elements are independently optional.
#[derive(ToDataFrame, Clone)]
#[allow(clippy::type_complexity)]
struct OptTupleWithOptionalElements {
    pair: Option<(Option<String>, Option<Box<i32>>)>,
}

#[derive(ToDataFrame, Clone)]
struct Nested {
    id: i32,
    name: String,
}

// 27. Parent Vec<Option<tuple>> plus nested element Vec<Option<Box<Nested>>>
// and sibling Option<Vec<String>>.
#[derive(ToDataFrame, Clone)]
#[allow(clippy::type_complexity)]
struct VecOptTupleWithNestedVecAndOptVec {
    items: Vec<Option<(Vec<Option<Box<Nested>>>, Option<Vec<String>>)>>,
}

// 28. Parent Vec<Box<Option<tuple>>> plus nested Vec<Nested> and boxed
// optional scalar element.
#[derive(ToDataFrame, Clone)]
#[allow(clippy::type_complexity)]
struct VecBoxOptTupleWithNestedVecAndBoxedOpt {
    items: Vec<Box<Option<(Vec<Nested>, Box<Option<i64>>)>>>,
}

// 29. Box<((i32, String), Arc<bool>)>: boxed parent tuple, unwrapped nested
// tuple, and smart pointer projected from the second element.
#[derive(ToDataFrame, Clone)]
struct BoxNestedTupleWithArc {
    nested: Box<((i32, String), Arc<bool>)>,
}

// 30. Parent Option tuple with an implicit rust_decimal::Decimal leaf.
#[derive(ToDataFrame, Clone)]
struct OptTupleWithRustDecimal {
    pair: Option<(rust_decimal::Decimal,)>,
}

#[derive(Clone)]
struct MyDecimal(i128);

impl Decimal128Encode for MyDecimal {
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128> {
        Some(self.0 + i128::from(target_scale))
    }
}

mod custom_decimal_tuple_backend {
    use super::*;

    type Decimal = MyDecimal;

    // 31. Parent Option tuple with a custom backend routed through the
    // default Decimal128Encode trait. Tuple elements cannot carry field-level
    // decimal attrs, so the syntactic `Decimal` alias exercises the implicit
    // decimal backend path while the impl lives on the custom type.
    #[derive(ToDataFrame, Clone)]
    pub(super) struct OptTupleWithCustomDecimal {
        pair: Option<(Decimal,)>,
    }

    pub(super) fn row(mantissa: Option<i128>) -> OptTupleWithCustomDecimal {
        OptTupleWithCustomDecimal {
            pair: mantissa.map(|value| (MyDecimal(value),)),
        }
    }
}

fn list_strings(value: AnyValue<'_>) -> Vec<Option<String>> {
    match value {
        AnyValue::List(series) => series
            .str()
            .unwrap()
            .into_iter()
            .map(|value| value.map(str::to_owned))
            .collect(),
        other => panic!("expected string List, got {other:?}"),
    }
}

fn list_i32s(value: AnyValue<'_>) -> Vec<Option<i32>> {
    match value {
        AnyValue::List(series) => series.i32().unwrap().into_iter().collect(),
        other => panic!("expected i32 List, got {other:?}"),
    }
}

fn list_i64s(value: AnyValue<'_>) -> Vec<Option<i64>> {
    match value {
        AnyValue::List(series) => series.i64().unwrap().into_iter().collect(),
        other => panic!("expected i64 List, got {other:?}"),
    }
}

fn nested_i32_lists(value: AnyValue<'_>) -> Vec<Option<Vec<Option<i32>>>> {
    let AnyValue::List(outer) = value else {
        panic!("expected outer List, got {value:?}");
    };
    (0..outer.len())
        .map(|idx| match outer.get(idx).unwrap() {
            AnyValue::List(inner) => Some(inner.i32().unwrap().into_iter().collect()),
            AnyValue::Null => None,
            other => panic!("expected inner i32 List or Null, got {other:?}"),
        })
        .collect()
}

fn nested_string_lists(value: AnyValue<'_>) -> Vec<Option<Vec<Option<String>>>> {
    let AnyValue::List(outer) = value else {
        panic!("expected outer List, got {value:?}");
    };
    (0..outer.len())
        .map(|idx| match outer.get(idx).unwrap() {
            AnyValue::List(inner) => Some(
                inner
                    .str()
                    .unwrap()
                    .into_iter()
                    .map(|value| value.map(str::to_owned))
                    .collect(),
            ),
            AnyValue::Null => None,
            other => panic!("expected inner string List or Null, got {other:?}"),
        })
        .collect()
}

fn decimal_mantissa(value: AnyValue<'_>) -> Option<i128> {
    match value {
        AnyValue::Decimal(value, _precision, _scale) => Some(value),
        AnyValue::Null => None,
        other => panic!("expected decimal or null, got {other:?}"),
    }
}

#[test]
fn runtime_semantics() {
    // 1. Bare
    let v = BareTuple {
        pair: ("hello".to_string(), 42),
        triple: (1.5, 2.5, true),
        single: (7,),
    };
    let df = v.to_dataframe().unwrap();
    let cols = df.get_column_names();
    let expected = [
        "pair.field_0",
        "pair.field_1",
        "triple.field_0",
        "triple.field_1",
        "triple.field_2",
        "single.field_0",
    ];
    assert_eq!(cols, expected);
    assert_eq!(
        df.column("pair.field_0").unwrap().get(0).unwrap(),
        AnyValue::String("hello")
    );
    assert_eq!(
        df.column("pair.field_1").unwrap().get(0).unwrap(),
        AnyValue::Int32(42)
    );
    assert_eq!(
        df.column("triple.field_2").unwrap().get(0).unwrap(),
        AnyValue::Boolean(true)
    );
    assert_eq!(
        df.column("single.field_0").unwrap().get(0).unwrap(),
        AnyValue::Int32(7)
    );

    // Schema
    let schema = BareTuple::schema().unwrap();
    assert_eq!(schema.len(), 6);
    let empty = BareTuple::empty_dataframe().unwrap();
    assert_eq!(empty.shape(), (0, 6));

    // 2. Option
    let opt_v = vec![
        OptTuple {
            pair: Some(("a".to_string(), 1)),
        },
        OptTuple { pair: None },
    ];
    let opt_df = opt_v.as_slice().to_dataframe().unwrap();
    assert_eq!(opt_df.shape(), (2, 2));
    assert_eq!(
        opt_df.column("pair.field_0").unwrap().get(0).unwrap(),
        AnyValue::String("a")
    );
    assert_eq!(
        opt_df.column("pair.field_0").unwrap().get(1).unwrap(),
        AnyValue::Null
    );
    assert_eq!(
        opt_df.column("pair.field_1").unwrap().get(0).unwrap(),
        AnyValue::Int32(1)
    );
    assert_eq!(
        opt_df.column("pair.field_1").unwrap().get(1).unwrap(),
        AnyValue::Null
    );

    // 3. Vec
    let vec_v = VecTuple {
        pairs: vec![("x".to_string(), 10), ("y".to_string(), 20)],
    };
    let vec_df = vec_v.to_dataframe().unwrap();
    assert!(matches!(
        vec_df.column("pairs.field_0").unwrap().dtype(),
        DataType::List(_)
    ));
    assert!(matches!(
        vec_df.column("pairs.field_1").unwrap().dtype(),
        DataType::List(_)
    ));

    // 4. Vec<Option<tuple>>
    let vot_v = VecOptTuple {
        items: vec![Some(("a".to_string(), 1)), None, Some(("b".to_string(), 2))],
    };
    let vot_df = vot_v.to_dataframe().unwrap();
    assert_eq!(
        list_strings(vot_df.column("items.field_0").unwrap().get(0).unwrap()),
        vec![Some("a".to_string()), None, Some("b".to_string())],
    );
    assert_eq!(
        list_i32s(vot_df.column("items.field_1").unwrap().get(0).unwrap()),
        vec![Some(1), None, Some(2)],
    );

    // 5. Option<Vec<tuple>>
    let ovt_v = vec![
        OptVecTuple {
            pairs: Some(vec![("a".to_string(), 1), ("b".to_string(), 2)]),
        },
        OptVecTuple { pairs: None },
    ];
    let ovt_df = ovt_v.as_slice().to_dataframe().unwrap();
    assert_eq!(
        list_strings(ovt_df.column("pairs.field_0").unwrap().get(0).unwrap()),
        vec![Some("a".to_string()), Some("b".to_string())],
    );
    assert_eq!(
        list_i32s(ovt_df.column("pairs.field_1").unwrap().get(0).unwrap()),
        vec![Some(1), Some(2)],
    );
    assert_eq!(
        ovt_df.column("pairs.field_0").unwrap().get(1).unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        ovt_df.column("pairs.field_1").unwrap().get(1).unwrap(),
        AnyValue::Null,
    );

    // 6. Tuple in tuple struct
    let wt = WithTuple(99, ("hi".to_string(), false));
    let wt_df = wt.to_dataframe().unwrap();
    assert_eq!(
        wt_df.get_column_names(),
        ["field_0", "field_1.field_0", "field_1.field_1"]
    );
    assert_eq!(
        wt_df.column("field_0").unwrap().get(0).unwrap(),
        AnyValue::Int32(99)
    );

    // 7. Smart pointer in element
    let sp = SmartPtrTuple {
        pair: (Box::new(42), "wrapped".to_string()),
    };
    let sp_df = sp.to_dataframe().unwrap();
    assert_eq!(
        sp_df.column("pair.field_0").unwrap().get(0).unwrap(),
        AnyValue::Int32(42)
    );
    assert_eq!(
        sp_df.column("pair.field_1").unwrap().get(0).unwrap(),
        AnyValue::String("wrapped")
    );

    // 8. Vec<(Arc<u64>, String)>
    let vsp = VecSmartPtrTuple {
        items: vec![
            (Arc::new(100), "a".to_string()),
            (Arc::new(200), "b".to_string()),
        ],
    };
    let _vsp_df = vsp.to_dataframe().unwrap();

    // 9. Temporal
    let tt = TemporalTuple {
        times: (
            NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
        ),
        durs: vec![(chrono::Duration::seconds(60), "minute".to_string())],
    };
    let tt_df = tt.to_dataframe().unwrap();
    assert!(matches!(
        tt_df.column("times.field_0").unwrap().dtype(),
        DataType::Date
    ));
    assert!(matches!(
        tt_df.column("times.field_1").unwrap().dtype(),
        DataType::Time
    ));

    // 10. Nested tuples
    let nt = NestedTuple {
        nested: ((42, "inner".to_string()), true),
    };
    let nt_df = nt.to_dataframe().unwrap();
    let nt_cols = nt_df.get_column_names();
    assert_eq!(
        nt_cols,
        [
            "nested.field_0.field_0",
            "nested.field_0.field_1",
            "nested.field_1"
        ]
    );
    assert_eq!(
        nt_df
            .column("nested.field_0.field_0")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Int32(42)
    );
    assert_eq!(
        nt_df
            .column("nested.field_0.field_1")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::String("inner")
    );
    assert_eq!(
        nt_df.column("nested.field_1").unwrap().get(0).unwrap(),
        AnyValue::Boolean(true)
    );

    // 11. HashMap-rejection hint regression: Vec<(K, V)> compiles.
    let cm = ConvertedHashMap {
        metadata: vec![("key".to_string(), "value".to_string())],
    };
    let cm_df = cm.to_dataframe().unwrap();
    assert_eq!(
        cm_df.get_column_names(),
        ["metadata.field_0", "metadata.field_1"]
    );

    // 12. Tuple containing a nested struct
    let wi = WithInnerTuple {
        pair: (Inner { a: 7, b: 3.125 }, 99),
    };
    let wi_df = wi.to_dataframe().unwrap();
    let wi_cols: Vec<&str> = wi_df
        .get_column_names()
        .iter()
        .map(|n| n.as_str())
        .collect();
    assert!(wi_cols.contains(&"pair.field_0.a"));
    assert!(wi_cols.contains(&"pair.field_0.b"));
    assert!(wi_cols.contains(&"pair.field_1"));
    assert_eq!(
        wi_df.column("pair.field_0.a").unwrap().get(0).unwrap(),
        AnyValue::Int32(7)
    );
    assert_eq!(
        wi_df.column("pair.field_1").unwrap().get(0).unwrap(),
        AnyValue::Int32(99)
    );

    // 13. Vec of tuple containing a nested struct
    let vi = VecInnerTuple {
        items: vec![(Inner { a: 1, b: 1.0 }, 10), (Inner { a: 2, b: 2.0 }, 20)],
    };
    let vi_df = vi.to_dataframe().unwrap();
    let vi_cols: Vec<&str> = vi_df
        .get_column_names()
        .iter()
        .map(|n| n.as_str())
        .collect();
    assert!(vi_cols.contains(&"items.field_0.a"));
    assert!(vi_cols.contains(&"items.field_0.b"));
    assert!(vi_cols.contains(&"items.field_1"));
    assert!(matches!(
        vi_df.column("items.field_1").unwrap().dtype(),
        DataType::List(_)
    ));

    // 14. Box<tuple>
    let bt = BoxTuple {
        pair: Box::new(("boxed".to_string(), 7)),
    };
    let bt_df = bt.to_dataframe().unwrap();
    assert_eq!(
        bt_df.column("pair.field_0").unwrap().get(0).unwrap(),
        AnyValue::String("boxed")
    );
    assert_eq!(
        bt_df.column("pair.field_1").unwrap().get(0).unwrap(),
        AnyValue::Int32(7)
    );

    // 15. Vec<(Vec<i32>, String)>: projection happens between list layers.
    let vve = VecTupleWithVecElement {
        items: vec![
            (vec![1, 2], "a".to_string()),
            (vec![], "b".to_string()),
            (vec![3], "c".to_string()),
        ],
    };
    let vve_df = vve.to_dataframe().unwrap();
    assert!(matches!(
        vve_df.column("items.field_0").unwrap().dtype(),
        DataType::List(inner) if matches!(inner.as_ref(), DataType::List(_))
    ));
    assert_eq!(
        nested_i32_lists(vve_df.column("items.field_0").unwrap().get(0).unwrap()),
        vec![
            Some(vec![Some(1), Some(2)]),
            Some(Vec::<Option<i32>>::new()),
            Some(vec![Some(3)]),
        ],
    );
    assert_eq!(
        list_strings(vve_df.column("items.field_1").unwrap().get(0).unwrap()),
        vec![
            Some("a".to_string()),
            Some("b".to_string()),
            Some("c".to_string()),
        ],
    );

    // 16. Vec<Option<(Vec<i32>, String)>>: parent Option becomes inner-list validity.
    let vov = VecOptTupleWithVecElement {
        items: vec![
            Some((vec![10], "x".to_string())),
            None,
            Some((vec![20, 30], "z".to_string())),
        ],
    };
    let vov_df = vov.to_dataframe().unwrap();
    assert_eq!(
        nested_i32_lists(vov_df.column("items.field_0").unwrap().get(0).unwrap()),
        vec![Some(vec![Some(10)]), None, Some(vec![Some(20), Some(30)])],
    );

    // 17. Vec<(Option<Vec<i32>>, String)>: element Option becomes inner-list validity.
    let vov_elem = VecTupleWithOptVecElement {
        items: vec![
            (Some(vec![100]), "left".to_string()),
            (None, "middle".to_string()),
            (Some(vec![200, 300]), "right".to_string()),
        ],
    };
    let vov_elem_df = vov_elem.to_dataframe().unwrap();
    assert_eq!(
        nested_i32_lists(vov_elem_df.column("items.field_0").unwrap().get(0).unwrap()),
        vec![
            Some(vec![Some(100)]),
            None,
            Some(vec![Some(200), Some(300)])
        ],
    );

    // 18. Option<(Vec<i32>, Option<Box<i32>>, Vec<Box<Option<i32>>>)>:
    // parent Option must project tuple elements by reference when the
    // element wrapper is not Copy-projectable.
    let wrapped = vec![
        OptTupleWithWrappedElements {
            pair: Some((
                vec![1, 2],
                Some(Box::new(5)),
                vec![Box::new(Some(7)), Box::new(None)],
            )),
        },
        OptTupleWithWrappedElements { pair: None },
        OptTupleWithWrappedElements {
            pair: Some((vec![], None, vec![Box::new(None)])),
        },
    ];
    let wrapped_df = wrapped.as_slice().to_dataframe().unwrap();
    assert_eq!(
        list_i32s(wrapped_df.column("pair.field_0").unwrap().get(0).unwrap()),
        vec![Some(1), Some(2)],
    );
    assert_eq!(
        wrapped_df.column("pair.field_0").unwrap().get(1).unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        list_i32s(wrapped_df.column("pair.field_0").unwrap().get(2).unwrap()),
        Vec::<Option<i32>>::new(),
    );
    assert_eq!(
        wrapped_df.column("pair.field_1").unwrap().get(0).unwrap(),
        AnyValue::Int32(5),
    );
    assert_eq!(
        wrapped_df.column("pair.field_1").unwrap().get(1).unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        wrapped_df.column("pair.field_1").unwrap().get(2).unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        list_i32s(wrapped_df.column("pair.field_2").unwrap().get(0).unwrap()),
        vec![Some(7), None],
    );
    assert_eq!(
        wrapped_df.column("pair.field_2").unwrap().get(1).unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        list_i32s(wrapped_df.column("pair.field_2").unwrap().get(2).unwrap()),
        vec![None],
    );

    // 19. Option<Box<Option<(i32, String)>>>: collapse the real access chain,
    // not merely the number of Option layers.
    let interleaved = vec![
        OptionBoxOptionTuple {
            x: Some(Box::new(Some((11, "eleven".to_string())))),
        },
        OptionBoxOptionTuple {
            x: Some(Box::new(None)),
        },
        OptionBoxOptionTuple { x: None },
    ];
    let interleaved_df = interleaved.as_slice().to_dataframe().unwrap();
    assert_eq!(
        interleaved_df.column("x.field_0").unwrap().get(0).unwrap(),
        AnyValue::Int32(11),
    );
    assert_eq!(
        interleaved_df.column("x.field_1").unwrap().get(0).unwrap(),
        AnyValue::String("eleven"),
    );
    assert_eq!(
        interleaved_df.column("x.field_0").unwrap().get(1).unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        interleaved_df.column("x.field_1").unwrap().get(2).unwrap(),
        AnyValue::Null,
    );

    // 20. Vec<Option<Box<(Vec<i32>, String)>>>: projection consumes the
    // parent access chain once before the element's Vec layer is walked.
    let vec_interleaved = VecOptBoxTupleWithVecElement {
        items: vec![
            Some(Box::new((vec![1, 2], "left".to_string()))),
            None,
            Some(Box::new((vec![3], "right".to_string()))),
        ],
    };
    let vec_interleaved_df = vec_interleaved.to_dataframe().unwrap();
    assert_eq!(
        nested_i32_lists(
            vec_interleaved_df
                .column("items.field_0")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![Some(vec![Some(1), Some(2)]), None, Some(vec![Some(3)])],
    );
    assert_eq!(
        list_strings(
            vec_interleaved_df
                .column("items.field_1")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![Some("left".to_string()), None, Some("right".to_string())],
    );

    // 21. Vec<(Option<i32>, String)>: the Option belongs to field_0 only;
    // field_1 remains present for every tuple item.
    let optional_elem = VecTupleWithOptionalElement {
        xs: vec![
            (Some(1), "one".to_string()),
            (None, "missing".to_string()),
            (Some(3), "three".to_string()),
        ],
    };
    let optional_elem_df = optional_elem.to_dataframe().unwrap();
    assert_eq!(
        list_i32s(
            optional_elem_df
                .column("xs.field_0")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![Some(1), None, Some(3)],
    );
    assert_eq!(
        list_strings(
            optional_elem_df
                .column("xs.field_1")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![
            Some("one".to_string()),
            Some("missing".to_string()),
            Some("three".to_string()),
        ],
    );

    // 22. Vec<(&str, i32)>: borrowed string tuple elements use the default
    // AsStr lowering and should share the same list string emitter.
    let borrowed = BorrowedStrTupleVec {
        xs: vec![("alpha", 10), ("beta", 20)],
    };
    let borrowed_df = borrowed.to_dataframe().unwrap();
    assert_eq!(
        list_strings(borrowed_df.column("xs.field_0").unwrap().get(0).unwrap()),
        vec![Some("alpha".to_string()), Some("beta".to_string())],
    );
    assert_eq!(
        list_i32s(borrowed_df.column("xs.field_1").unwrap().get(0).unwrap()),
        vec![Some(10), Some(20)],
    );

    // 23. Option<(&i32, bool)>: parent Option projection must dereference the
    // borrowed numeric element before feeding the Copy Option leaf.
    let borrowed_scalar = 123;
    let borrowed_copy = vec![
        OptTupleWithBorrowedCopy {
            pair: Some((&borrowed_scalar, true)),
        },
        OptTupleWithBorrowedCopy { pair: None },
    ];
    let borrowed_copy_df = borrowed_copy.as_slice().to_dataframe().unwrap();
    assert_eq!(
        borrowed_copy_df
            .column("pair.field_0")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Int32(123),
    );
    assert_eq!(
        borrowed_copy_df
            .column("pair.field_1")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Boolean(true),
    );
    assert_eq!(
        borrowed_copy_df
            .column("pair.field_0")
            .unwrap()
            .get(1)
            .unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        borrowed_copy_df
            .column("pair.field_1")
            .unwrap()
            .get(1)
            .unwrap(),
        AnyValue::Null,
    );

    // 24. Option<(Box<i32>,)>: projection must not move the Box out of the
    // borrowed tuple; it dereferences first and copies the i32.
    let boxed_copy = vec![
        OptTupleWithBoxedCopy {
            pair: Some((Box::new(456),)),
        },
        OptTupleWithBoxedCopy { pair: None },
    ];
    let boxed_copy_df = boxed_copy.as_slice().to_dataframe().unwrap();
    assert_eq!(
        boxed_copy_df
            .column("pair.field_0")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Int32(456),
    );
    assert_eq!(
        boxed_copy_df
            .column("pair.field_0")
            .unwrap()
            .get(1)
            .unwrap(),
        AnyValue::Null,
    );

    // 25. Option<(Box<String>,)>: non-Copy smart-pointer elements project as
    // references after applying the element smart-pointer depth.
    let boxed_string = vec![
        OptTupleWithBoxedString {
            pair: Some((Box::new("boxed".to_string()),)),
        },
        OptTupleWithBoxedString { pair: None },
    ];
    let boxed_string_df = boxed_string.as_slice().to_dataframe().unwrap();
    assert_eq!(
        boxed_string_df
            .column("pair.field_0")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::String("boxed"),
    );
    assert_eq!(
        boxed_string_df
            .column("pair.field_0")
            .unwrap()
            .get(1)
            .unwrap(),
        AnyValue::Null,
    );

    // 26. Option<(Option<String>, Option<Box<i32>>)>
    let optional_elements = vec![
        OptTupleWithOptionalElements {
            pair: Some((Some("present".to_string()), Some(Box::new(5)))),
        },
        OptTupleWithOptionalElements {
            pair: Some((None, None)),
        },
        OptTupleWithOptionalElements { pair: None },
    ];
    let optional_elements_df = optional_elements.as_slice().to_dataframe().unwrap();
    assert_eq!(
        optional_elements_df
            .column("pair.field_0")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::String("present"),
    );
    assert_eq!(
        optional_elements_df
            .column("pair.field_1")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Int32(5),
    );
    assert_eq!(
        optional_elements_df
            .column("pair.field_0")
            .unwrap()
            .get(1)
            .unwrap(),
        AnyValue::Null,
    );
    assert_eq!(
        optional_elements_df
            .column("pair.field_1")
            .unwrap()
            .get(2)
            .unwrap(),
        AnyValue::Null,
    );

    // 27. Vec<Option<(Vec<Option<Box<Nested>>>, Option<Vec<String>>)>>
    let nested_opt_vec = VecOptTupleWithNestedVecAndOptVec {
        items: vec![
            Some((
                vec![
                    Some(Box::new(Nested {
                        id: 1,
                        name: "one".to_string(),
                    })),
                    None,
                    Some(Box::new(Nested {
                        id: 2,
                        name: "two".to_string(),
                    })),
                ],
                Some(vec!["left".to_string(), "right".to_string()]),
            )),
            None,
            Some((Vec::new(), None)),
        ],
    };
    let nested_opt_vec_df = nested_opt_vec.to_dataframe().unwrap();
    assert_eq!(
        nested_i32_lists(
            nested_opt_vec_df
                .column("items.field_0.id")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![Some(vec![Some(1), None, Some(2)]), None, Some(Vec::new())],
    );
    assert_eq!(
        nested_string_lists(
            nested_opt_vec_df
                .column("items.field_0.name")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![
            Some(vec![Some("one".to_string()), None, Some("two".to_string())]),
            None,
            Some(Vec::new()),
        ],
    );
    assert_eq!(
        nested_string_lists(
            nested_opt_vec_df
                .column("items.field_1")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![
            Some(vec![Some("left".to_string()), Some("right".to_string())]),
            None,
            None,
        ],
    );

    // 28. Vec<Box<Option<(Vec<Nested>, Box<Option<i64>>)>>>
    let boxed_opt_nested = VecBoxOptTupleWithNestedVecAndBoxedOpt {
        items: vec![
            Box::new(Some((
                vec![
                    Nested {
                        id: 10,
                        name: "ten".to_string(),
                    },
                    Nested {
                        id: 11,
                        name: "eleven".to_string(),
                    },
                ],
                Box::new(Some(100)),
            ))),
            Box::new(None),
            Box::new(Some((Vec::new(), Box::new(None)))),
            Box::new(Some((
                vec![Nested {
                    id: 12,
                    name: "twelve".to_string(),
                }],
                Box::new(Some(120)),
            ))),
        ],
    };
    let boxed_opt_nested_df = boxed_opt_nested.to_dataframe().unwrap();
    assert_eq!(
        nested_i32_lists(
            boxed_opt_nested_df
                .column("items.field_0.id")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![
            Some(vec![Some(10), Some(11)]),
            None,
            Some(Vec::new()),
            Some(vec![Some(12)]),
        ],
    );
    assert_eq!(
        nested_string_lists(
            boxed_opt_nested_df
                .column("items.field_0.name")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![
            Some(vec![Some("ten".to_string()), Some("eleven".to_string())]),
            None,
            Some(Vec::new()),
            Some(vec![Some("twelve".to_string())]),
        ],
    );
    assert_eq!(
        list_i64s(
            boxed_opt_nested_df
                .column("items.field_1")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        vec![Some(100), None, None, Some(120)],
    );

    // 29. Box<((i32, String), Arc<bool>)>
    let boxed_nested_tuple = BoxNestedTupleWithArc {
        nested: Box::new(((8, "boxed nested".to_string()), Arc::new(true))),
    };
    let boxed_nested_tuple_df = boxed_nested_tuple.to_dataframe().unwrap();
    assert_eq!(
        boxed_nested_tuple_df.get_column_names(),
        [
            "nested.field_0.field_0",
            "nested.field_0.field_1",
            "nested.field_1"
        ],
    );
    assert_eq!(
        boxed_nested_tuple_df
            .column("nested.field_0.field_0")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Int32(8),
    );
    assert_eq!(
        boxed_nested_tuple_df
            .column("nested.field_0.field_1")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::String("boxed nested"),
    );
    assert_eq!(
        boxed_nested_tuple_df
            .column("nested.field_1")
            .unwrap()
            .get(0)
            .unwrap(),
        AnyValue::Boolean(true),
    );

    // 30. Option<(rust_decimal::Decimal,)>: parent Option projection yields
    // Option<&Decimal>; decimal UFCS dispatch must still reach the backend.
    let rust_decimal_rows = vec![
        OptTupleWithRustDecimal {
            pair: Some((rust_decimal::Decimal::new(123, 2),)),
        },
        OptTupleWithRustDecimal { pair: None },
    ];
    let rust_decimal_df = rust_decimal_rows.as_slice().to_dataframe().unwrap();
    assert_eq!(
        rust_decimal_df.column("pair.field_0").unwrap().dtype(),
        &DataType::Decimal(38, 10),
    );
    assert_eq!(
        decimal_mantissa(
            rust_decimal_df
                .column("pair.field_0")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        Some(12_300_000_000),
    );
    assert_eq!(
        decimal_mantissa(
            rust_decimal_df
                .column("pair.field_0")
                .unwrap()
                .get(1)
                .unwrap()
        ),
        None,
    );

    // 31. Option<(Decimal alias to MyDecimal,)>: custom backend dispatch
    // uses the same projected Option<&T> shape as rust_decimal.
    let custom_decimal_rows = vec![
        custom_decimal_tuple_backend::row(Some(700)),
        custom_decimal_tuple_backend::row(None),
    ];
    let custom_decimal_df = custom_decimal_rows.as_slice().to_dataframe().unwrap();
    assert_eq!(
        custom_decimal_df.column("pair.field_0").unwrap().dtype(),
        &DataType::Decimal(38, 10),
    );
    assert_eq!(
        decimal_mantissa(
            custom_decimal_df
                .column("pair.field_0")
                .unwrap()
                .get(0)
                .unwrap()
        ),
        Some(710),
    );
    assert_eq!(
        decimal_mantissa(
            custom_decimal_df
                .column("pair.field_0")
                .unwrap()
                .get(1)
                .unwrap()
        ),
        None,
    );

    // Batch round-trip
    let batch = vec![
        BareTuple {
            pair: ("a".to_string(), 1),
            triple: (1.0, 2.0, true),
            single: (10,),
        },
        BareTuple {
            pair: ("b".to_string(), 2),
            triple: (3.0, 4.0, false),
            single: (20,),
        },
    ];
    let batch_df = batch.as_slice().to_dataframe().unwrap();
    assert_eq!(batch_df.shape(), (2, 6));
    assert_eq!(
        batch_df.column("pair.field_1").unwrap().get(1).unwrap(),
        AnyValue::Int32(2)
    );
    assert_eq!(
        batch_df.column("single.field_0").unwrap().get(0).unwrap(),
        AnyValue::Int32(10)
    );

    println!("Tuple field tests passed");
}