@@ -307,7 +307,8 @@ mod test {
307
307
use parquet_variant:: { Variant , VariantPath } ;
308
308
309
309
use crate :: json_to_variant;
310
- use crate :: { variant_array:: ShreddedVariantFieldArray , VariantArray } ;
310
+ use crate :: variant_array:: { ShreddedVariantFieldArray , StructArrayBuilder } ;
311
+ use crate :: VariantArray ;
311
312
312
313
use super :: { variant_get, GetOptions } ;
313
314
@@ -692,8 +693,8 @@ mod test {
692
693
] ) ;
693
694
694
695
let struct_array = StructArrayBuilder :: new( )
695
- . with_field( "metadata" , Arc :: new( metadata) )
696
- . with_field( "typed_value" , Arc :: new( typed_value) )
696
+ . with_field( "metadata" , Arc :: new( metadata) , false )
697
+ . with_field( "typed_value" , Arc :: new( typed_value) , true )
697
698
. build( ) ;
698
699
699
700
Arc :: new(
@@ -821,9 +822,9 @@ mod test {
821
822
] ) ;
822
823
823
824
let struct_array = StructArrayBuilder :: new( )
824
- . with_field( "metadata" , Arc :: new( metadata) )
825
- . with_field( "typed_value" , Arc :: new( typed_value) )
826
- . with_field( "value" , Arc :: new( values) )
825
+ . with_field( "metadata" , Arc :: new( metadata) , false )
826
+ . with_field( "typed_value" , Arc :: new( typed_value) , true )
827
+ . with_field( "value" , Arc :: new( values) , true )
827
828
. with_nulls( nulls)
828
829
. build( ) ;
829
830
@@ -926,9 +927,9 @@ mod test {
926
927
] ) ;
927
928
928
929
let struct_array = StructArrayBuilder :: new ( )
929
- . with_field ( "metadata" , Arc :: new ( metadata) )
930
- . with_field ( "typed_value" , Arc :: new ( typed_value) )
931
- . with_field ( "value" , Arc :: new ( values) )
930
+ . with_field ( "metadata" , Arc :: new ( metadata) , true )
931
+ . with_field ( "typed_value" , Arc :: new ( typed_value) , true )
932
+ . with_field ( "value" , Arc :: new ( values) , true )
932
933
. with_nulls ( nulls)
933
934
. build ( ) ;
934
935
@@ -937,45 +938,6 @@ mod test {
937
938
)
938
939
}
939
940
940
- /// Builds struct arrays from component fields
941
- ///
942
- /// TODO: move to arrow crate
943
- #[ derive( Debug , Default , Clone ) ]
944
- struct StructArrayBuilder {
945
- fields : Vec < FieldRef > ,
946
- arrays : Vec < ArrayRef > ,
947
- nulls : Option < NullBuffer > ,
948
- }
949
-
950
- impl StructArrayBuilder {
951
- fn new ( ) -> Self {
952
- Default :: default ( )
953
- }
954
-
955
- /// Add an array to this struct array as a field with the specified name.
956
- fn with_field ( mut self , field_name : & str , array : ArrayRef ) -> Self {
957
- let field = Field :: new ( field_name, array. data_type ( ) . clone ( ) , true ) ;
958
- self . fields . push ( Arc :: new ( field) ) ;
959
- self . arrays . push ( array) ;
960
- self
961
- }
962
-
963
- /// Set the null buffer for this struct array.
964
- fn with_nulls ( mut self , nulls : NullBuffer ) -> Self {
965
- self . nulls = Some ( nulls) ;
966
- self
967
- }
968
-
969
- pub fn build ( self ) -> StructArray {
970
- let Self {
971
- fields,
972
- arrays,
973
- nulls,
974
- } = self ;
975
- StructArray :: new ( Fields :: from ( fields) , arrays, nulls)
976
- }
977
- }
978
-
979
941
/// Return a VariantArray that represents an "all null" variant
980
942
/// for the following example (3 null values):
981
943
///
@@ -1005,7 +967,7 @@ mod test {
1005
967
let metadata = BinaryViewArray :: from_iter_values ( std:: iter:: repeat_n ( & metadata, 3 ) ) ;
1006
968
1007
969
let struct_array = StructArrayBuilder :: new ( )
1008
- . with_field ( "metadata" , Arc :: new ( metadata) )
970
+ . with_field ( "metadata" , Arc :: new ( metadata) , false )
1009
971
. with_nulls ( nulls)
1010
972
. build ( ) ;
1011
973
@@ -1096,8 +1058,8 @@ mod test {
1096
1058
let x_field_typed_value = Int32Array :: from ( vec ! [ Some ( 1 ) , Some ( 42 ) ] ) ;
1097
1059
1098
1060
// For perfect shredding of the x field, no "value" column, only typed_value
1099
- let x_field_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1100
- . with_field ( "typed_value" , Arc :: new ( x_field_typed_value) )
1061
+ let x_field_struct = StructArrayBuilder :: new ( )
1062
+ . with_field ( "typed_value" , Arc :: new ( x_field_typed_value) , true )
1101
1063
. build ( ) ;
1102
1064
1103
1065
// Wrap the x field struct in a ShreddedVariantFieldArray
@@ -1118,10 +1080,10 @@ mod test {
1118
1080
. unwrap ( ) ;
1119
1081
1120
1082
// Create the main VariantArray
1121
- let main_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1122
- . with_field ( "metadata" , Arc :: new ( metadata_array) )
1123
- . with_field ( "value" , Arc :: new ( value_array) )
1124
- . with_field ( "typed_value" , Arc :: new ( typed_value_struct) )
1083
+ let main_struct = StructArrayBuilder :: new ( )
1084
+ . with_field ( "metadata" , Arc :: new ( metadata_array) , false )
1085
+ . with_field ( "value" , Arc :: new ( value_array) , true )
1086
+ . with_field ( "typed_value" , Arc :: new ( typed_value_struct) , true )
1125
1087
. build ( ) ;
1126
1088
1127
1089
Arc :: new ( VariantArray :: try_new ( Arc :: new ( main_struct) ) . expect ( "should create variant array" ) )
@@ -1476,8 +1438,8 @@ mod test {
1476
1438
let x_field_typed_value = Int32Array :: from ( vec ! [ Some ( 42 ) , None ] ) ;
1477
1439
1478
1440
// For the x field, only typed_value (perfect shredding when possible)
1479
- let x_field_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1480
- . with_field ( "typed_value" , Arc :: new ( x_field_typed_value) )
1441
+ let x_field_struct = StructArrayBuilder :: new ( )
1442
+ . with_field ( "typed_value" , Arc :: new ( x_field_typed_value) , true )
1481
1443
. build ( ) ;
1482
1444
1483
1445
let x_field_shredded = ShreddedVariantFieldArray :: try_new ( Arc :: new ( x_field_struct) )
@@ -1494,10 +1456,10 @@ mod test {
1494
1456
. unwrap ( ) ;
1495
1457
1496
1458
// Build final VariantArray
1497
- let struct_array = crate :: variant_array :: StructArrayBuilder :: new ( )
1498
- . with_field ( "metadata" , Arc :: new ( metadata_array) )
1499
- . with_field ( "value" , Arc :: new ( value_array) )
1500
- . with_field ( "typed_value" , Arc :: new ( typed_value_struct) )
1459
+ let struct_array = StructArrayBuilder :: new ( )
1460
+ . with_field ( "metadata" , Arc :: new ( metadata_array) , false )
1461
+ . with_field ( "value" , Arc :: new ( value_array) , true )
1462
+ . with_field ( "typed_value" , Arc :: new ( typed_value_struct) , true )
1501
1463
. build ( ) ;
1502
1464
1503
1465
Arc :: new ( VariantArray :: try_new ( Arc :: new ( struct_array) ) . expect ( "should create VariantArray" ) )
@@ -1555,8 +1517,8 @@ mod test {
1555
1517
// Create the nested shredded structure
1556
1518
// Level 2: x field (the deepest level)
1557
1519
let x_typed_value = Int32Array :: from ( vec ! [ Some ( 55 ) , None ] ) ;
1558
- let x_field_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1559
- . with_field ( "typed_value" , Arc :: new ( x_typed_value) )
1520
+ let x_field_struct = StructArrayBuilder :: new ( )
1521
+ . with_field ( "typed_value" , Arc :: new ( x_typed_value) , true )
1560
1522
. build ( ) ;
1561
1523
let x_field_shredded = ShreddedVariantFieldArray :: try_new ( Arc :: new ( x_field_struct) )
1562
1524
. expect ( "should create ShreddedVariantFieldArray for x" ) ;
@@ -1582,15 +1544,16 @@ mod test {
1582
1544
x_field_shredded. data_type( ) . clone( ) ,
1583
1545
true ,
1584
1546
) ] ) ;
1585
- let a_inner_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1547
+ let a_inner_struct = StructArrayBuilder :: new ( )
1586
1548
. with_field (
1587
1549
"typed_value" ,
1588
1550
Arc :: new (
1589
1551
StructArray :: try_new ( a_inner_fields, vec ! [ Arc :: new( x_field_shredded) ] , None )
1590
1552
. unwrap ( ) ,
1591
1553
) ,
1554
+ true ,
1592
1555
)
1593
- . with_field ( "value" , Arc :: new ( a_value_array) )
1556
+ . with_field ( "value" , Arc :: new ( a_value_array) , true )
1594
1557
. build ( ) ;
1595
1558
let a_field_shredded = ShreddedVariantFieldArray :: try_new ( Arc :: new ( a_inner_struct) )
1596
1559
. expect ( "should create ShreddedVariantFieldArray for a" ) ;
@@ -1606,10 +1569,10 @@ mod test {
1606
1569
. unwrap ( ) ;
1607
1570
1608
1571
// Build final VariantArray
1609
- let struct_array = crate :: variant_array :: StructArrayBuilder :: new ( )
1610
- . with_field ( "metadata" , Arc :: new ( metadata_array) )
1611
- . with_field ( "value" , Arc :: new ( value_array) )
1612
- . with_field ( "typed_value" , Arc :: new ( typed_value_struct) )
1572
+ let struct_array = StructArrayBuilder :: new ( )
1573
+ . with_field ( "metadata" , Arc :: new ( metadata_array) , false )
1574
+ . with_field ( "value" , Arc :: new ( value_array) , true )
1575
+ . with_field ( "typed_value" , Arc :: new ( typed_value_struct) , true )
1613
1576
. build ( ) ;
1614
1577
1615
1578
Arc :: new ( VariantArray :: try_new ( Arc :: new ( struct_array) ) . expect ( "should create VariantArray" ) )
@@ -1660,8 +1623,8 @@ mod test {
1660
1623
1661
1624
// Level 3: x field (deepest level)
1662
1625
let x_typed_value = Int32Array :: from ( vec ! [ Some ( 100 ) , None , None ] ) ;
1663
- let x_field_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1664
- . with_field ( "typed_value" , Arc :: new ( x_typed_value) )
1626
+ let x_field_struct = StructArrayBuilder :: new ( )
1627
+ . with_field ( "typed_value" , Arc :: new ( x_typed_value) , true )
1665
1628
. build ( ) ;
1666
1629
let x_field_shredded = ShreddedVariantFieldArray :: try_new ( Arc :: new ( x_field_struct) )
1667
1630
. expect ( "should create ShreddedVariantFieldArray for x" ) ;
@@ -1685,15 +1648,16 @@ mod test {
1685
1648
x_field_shredded. data_type( ) . clone( ) ,
1686
1649
true ,
1687
1650
) ] ) ;
1688
- let b_inner_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1651
+ let b_inner_struct = StructArrayBuilder :: new ( )
1689
1652
. with_field (
1690
1653
"typed_value" ,
1691
1654
Arc :: new (
1692
1655
StructArray :: try_new ( b_inner_fields, vec ! [ Arc :: new( x_field_shredded) ] , None )
1693
1656
. unwrap ( ) ,
1694
1657
) ,
1658
+ true ,
1695
1659
)
1696
- . with_field ( "value" , Arc :: new ( b_value_array) )
1660
+ . with_field ( "value" , Arc :: new ( b_value_array) , true )
1697
1661
. build ( ) ;
1698
1662
let b_field_shredded = ShreddedVariantFieldArray :: try_new ( Arc :: new ( b_inner_struct) )
1699
1663
. expect ( "should create ShreddedVariantFieldArray for b" ) ;
@@ -1717,15 +1681,16 @@ mod test {
1717
1681
b_field_shredded. data_type( ) . clone( ) ,
1718
1682
true ,
1719
1683
) ] ) ;
1720
- let a_inner_struct = crate :: variant_array :: StructArrayBuilder :: new ( )
1684
+ let a_inner_struct = StructArrayBuilder :: new ( )
1721
1685
. with_field (
1722
1686
"typed_value" ,
1723
1687
Arc :: new (
1724
1688
StructArray :: try_new ( a_inner_fields, vec ! [ Arc :: new( b_field_shredded) ] , None )
1725
1689
. unwrap ( ) ,
1726
1690
) ,
1691
+ true ,
1727
1692
)
1728
- . with_field ( "value" , Arc :: new ( a_value_array) )
1693
+ . with_field ( "value" , Arc :: new ( a_value_array) , true )
1729
1694
. build ( ) ;
1730
1695
let a_field_shredded = ShreddedVariantFieldArray :: try_new ( Arc :: new ( a_inner_struct) )
1731
1696
. expect ( "should create ShreddedVariantFieldArray for a" ) ;
@@ -1741,10 +1706,10 @@ mod test {
1741
1706
. unwrap ( ) ;
1742
1707
1743
1708
// Build final VariantArray
1744
- let struct_array = crate :: variant_array :: StructArrayBuilder :: new ( )
1745
- . with_field ( "metadata" , Arc :: new ( metadata_array) )
1746
- . with_field ( "value" , Arc :: new ( value_array) )
1747
- . with_field ( "typed_value" , Arc :: new ( typed_value_struct) )
1709
+ let struct_array = StructArrayBuilder :: new ( )
1710
+ . with_field ( "metadata" , Arc :: new ( metadata_array) , false )
1711
+ . with_field ( "value" , Arc :: new ( value_array) , true )
1712
+ . with_field ( "typed_value" , Arc :: new ( typed_value_struct) , true )
1748
1713
. build ( ) ;
1749
1714
1750
1715
Arc :: new ( VariantArray :: try_new ( Arc :: new ( struct_array) ) . expect ( "should create VariantArray" ) )
0 commit comments