1
- use std:: sync:: Arc ;
1
+ use std:: sync:: { Arc , OnceLock } ;
2
2
3
3
use arrow:: array:: { Array , BooleanArray , Float64Array , Int64Array , StringArray , UnionArray } ;
4
4
use arrow:: buffer:: Buffer ;
@@ -36,10 +36,7 @@ impl JsonUnion {
36
36
}
37
37
38
38
pub fn data_type ( ) -> DataType {
39
- DataType :: Union (
40
- UnionFields :: new ( TYPE_IDS . to_vec ( ) , union_fields ( ) . to_vec ( ) ) ,
41
- UnionMode :: Sparse ,
42
- )
39
+ DataType :: Union ( union_fields ( ) , UnionMode :: Sparse )
43
40
}
44
41
45
42
fn push ( & mut self , field : JsonUnionField ) {
@@ -58,7 +55,7 @@ impl JsonUnion {
58
55
}
59
56
60
57
fn push_none ( & mut self ) {
61
- self . type_ids [ self . index ] = TYPE_IDS [ 0 ] ;
58
+ self . type_ids [ self . index ] = TYPE_ID_NULL ;
62
59
self . index += 1 ;
63
60
debug_assert ! ( self . index <= self . capacity) ;
64
61
}
@@ -86,17 +83,16 @@ impl TryFrom<JsonUnion> for UnionArray {
86
83
type Error = arrow:: error:: ArrowError ;
87
84
88
85
fn try_from ( value : JsonUnion ) -> Result < Self , Self :: Error > {
89
- let [ f0, f1, f2, f3, f4, f5, f6] = union_fields ( ) ;
90
- let children: Vec < ( Field , Arc < dyn Array > ) > = vec ! [
91
- ( f0, Arc :: new( BooleanArray :: from( value. nulls) ) ) ,
92
- ( f1, Arc :: new( BooleanArray :: from( value. bools) ) ) ,
93
- ( f2, Arc :: new( Int64Array :: from( value. ints) ) ) ,
94
- ( f3, Arc :: new( Float64Array :: from( value. floats) ) ) ,
95
- ( f4, Arc :: new( StringArray :: from( value. strings) ) ) ,
96
- ( f5, Arc :: new( StringArray :: from( value. arrays) ) ) ,
97
- ( f6, Arc :: new( StringArray :: from( value. objects) ) ) ,
86
+ let children: Vec < Arc < dyn Array > > = vec ! [
87
+ Arc :: new( BooleanArray :: from( value. nulls) ) ,
88
+ Arc :: new( BooleanArray :: from( value. bools) ) ,
89
+ Arc :: new( Int64Array :: from( value. ints) ) ,
90
+ Arc :: new( Float64Array :: from( value. floats) ) ,
91
+ Arc :: new( StringArray :: from( value. strings) ) ,
92
+ Arc :: new( StringArray :: from( value. arrays) ) ,
93
+ Arc :: new( StringArray :: from( value. objects) ) ,
98
94
] ;
99
- UnionArray :: try_new ( TYPE_IDS , Buffer :: from_slice_ref ( & value. type_ids ) , None , children)
95
+ UnionArray :: try_new ( union_fields ( ) , Buffer :: from_vec ( value. type_ids ) . into ( ) , None , children)
100
96
}
101
97
}
102
98
@@ -111,18 +107,29 @@ pub(crate) enum JsonUnionField {
111
107
Object ( String ) ,
112
108
}
113
109
114
- const TYPE_IDS : & [ i8 ] = & [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ;
115
-
116
- fn union_fields ( ) -> [ Field ; 7 ] {
117
- [
118
- Field :: new ( "null" , DataType :: Boolean , true ) ,
119
- Field :: new ( "bool" , DataType :: Boolean , false ) ,
120
- Field :: new ( "int" , DataType :: Int64 , false ) ,
121
- Field :: new ( "float" , DataType :: Float64 , false ) ,
122
- Field :: new ( "str" , DataType :: Utf8 , false ) ,
123
- Field :: new ( "array" , DataType :: Utf8 , false ) ,
124
- Field :: new ( "object" , DataType :: Utf8 , false ) ,
125
- ]
110
+ const TYPE_ID_NULL : i8 = 0 ;
111
+ const TYPE_ID_BOOL : i8 = 1 ;
112
+ const TYPE_ID_INT : i8 = 2 ;
113
+ const TYPE_ID_FLOAT : i8 = 3 ;
114
+ const TYPE_ID_STR : i8 = 4 ;
115
+ const TYPE_ID_ARRAY : i8 = 5 ;
116
+ const TYPE_ID_OBJECT : i8 = 6 ;
117
+
118
+ fn union_fields ( ) -> UnionFields {
119
+ static FIELDS : OnceLock < UnionFields > = OnceLock :: new ( ) ;
120
+ FIELDS
121
+ . get_or_init ( || {
122
+ UnionFields :: from_iter ( [
123
+ ( TYPE_ID_NULL , Arc :: new ( Field :: new ( "null" , DataType :: Boolean , true ) ) ) ,
124
+ ( TYPE_ID_BOOL , Arc :: new ( Field :: new ( "bool" , DataType :: Boolean , false ) ) ) ,
125
+ ( TYPE_ID_INT , Arc :: new ( Field :: new ( "int" , DataType :: Int64 , false ) ) ) ,
126
+ ( TYPE_ID_FLOAT , Arc :: new ( Field :: new ( "float" , DataType :: Float64 , false ) ) ) ,
127
+ ( TYPE_ID_STR , Arc :: new ( Field :: new ( "str" , DataType :: Utf8 , false ) ) ) ,
128
+ ( TYPE_ID_ARRAY , Arc :: new ( Field :: new ( "array" , DataType :: Utf8 , false ) ) ) ,
129
+ ( TYPE_ID_OBJECT , Arc :: new ( Field :: new ( "object" , DataType :: Utf8 , false ) ) ) ,
130
+ ] )
131
+ } )
132
+ . clone ( )
126
133
}
127
134
128
135
impl JsonUnionField {
@@ -141,7 +148,7 @@ impl JsonUnionField {
141
148
pub fn scalar_value ( f : Option < Self > ) -> ScalarValue {
142
149
ScalarValue :: Union (
143
150
f. map ( |f| ( f. type_id ( ) , Box :: new ( f. into ( ) ) ) ) ,
144
- UnionFields :: new ( TYPE_IDS . to_vec ( ) , union_fields ( ) . to_vec ( ) ) ,
151
+ union_fields ( ) ,
145
152
UnionMode :: Sparse ,
146
153
)
147
154
}
0 commit comments