18
18
use crate :: builder:: ArrayBuilder ;
19
19
use crate :: { ArrayRef , FixedSizeListArray } ;
20
20
use arrow_buffer:: NullBufferBuilder ;
21
- use arrow_data:: ArrayData ;
22
- use arrow_schema:: { DataType , Field , FieldRef } ;
21
+ use arrow_schema:: { Field , FieldRef } ;
23
22
use std:: any:: Any ;
24
23
use std:: sync:: Arc ;
25
24
@@ -94,9 +93,9 @@ impl<T: ArrayBuilder> FixedSizeListBuilder<T> {
94
93
}
95
94
}
96
95
97
- /// Override the field passed to [`ArrayData::builder `]
96
+ /// Override the field passed to [`FixedSizeListArray::new `]
98
97
///
99
- /// By default a nullable field is created with the name `item`
98
+ /// By default, a nullable field is created with the name `item`
100
99
///
101
100
/// Note: [`Self::finish`] and [`Self::finish_cloned`] will panic if the
102
101
/// field's data type does not match that of `T`
@@ -169,116 +168,52 @@ where
169
168
/// Builds the [`FixedSizeListBuilder`] and reset this builder.
170
169
pub fn finish ( & mut self ) -> FixedSizeListArray {
171
170
let len = self . len ( ) ;
172
- let values_arr = self . values_builder . finish ( ) ;
173
- let values_data = values_arr . to_data ( ) ;
171
+ let values = self . values_builder . finish ( ) ;
172
+ let nulls = self . null_buffer_builder . finish ( ) ;
174
173
175
174
assert_eq ! (
176
- values_data . len( ) , len * self . list_len as usize ,
175
+ values . len( ) , len * self . list_len as usize ,
177
176
"Length of the child array ({}) must be the multiple of the value length ({}) and the array length ({})." ,
178
- values_data . len( ) ,
177
+ values . len( ) ,
179
178
self . list_len,
180
179
len,
181
180
) ;
182
181
183
- let nulls = self . null_buffer_builder . finish ( ) ;
182
+ let field = self
183
+ . field
184
+ . clone ( )
185
+ . unwrap_or_else ( || Arc :: new ( Field :: new ( "item" , values. data_type ( ) . clone ( ) , true ) ) ) ;
184
186
185
- let field = match & self . field {
186
- Some ( f) => {
187
- let size = self . value_length ( ) ;
188
- assert_eq ! (
189
- f. data_type( ) ,
190
- values_data. data_type( ) ,
191
- "DataType of field ({}) should be the same as the values_builder DataType ({})" ,
192
- f. data_type( ) ,
193
- values_data. data_type( )
194
- ) ;
195
-
196
- if let Some ( a) = values_arr. logical_nulls ( ) {
197
- let nulls_valid = f. is_nullable ( )
198
- || nulls
199
- . as_ref ( )
200
- . map ( |n| n. expand ( size as _ ) . contains ( & a) )
201
- . unwrap_or_default ( ) ;
202
-
203
- assert ! (
204
- nulls_valid,
205
- "Found unmasked nulls for non-nullable FixedSizeListBuilder field {:?}" ,
206
- f. name( )
207
- ) ;
208
- }
209
- f. clone ( )
210
- }
211
- None => Arc :: new ( Field :: new ( "item" , values_data. data_type ( ) . clone ( ) , true ) ) ,
212
- } ;
213
-
214
- let array_data = ArrayData :: builder ( DataType :: FixedSizeList ( field, self . list_len ) )
215
- . len ( len)
216
- . add_child_data ( values_data)
217
- . nulls ( nulls) ;
218
-
219
- let array_data = unsafe { array_data. build_unchecked ( ) } ;
220
-
221
- FixedSizeListArray :: from ( array_data)
187
+ FixedSizeListArray :: new ( field, self . list_len , values, nulls)
222
188
}
223
189
224
190
/// Builds the [`FixedSizeListBuilder`] without resetting the builder.
225
191
pub fn finish_cloned ( & self ) -> FixedSizeListArray {
226
192
let len = self . len ( ) ;
227
- let values_arr = self . values_builder . finish_cloned ( ) ;
228
- let values_data = values_arr . to_data ( ) ;
193
+ let values = self . values_builder . finish_cloned ( ) ;
194
+ let nulls = self . null_buffer_builder . finish_cloned ( ) ;
229
195
230
196
assert_eq ! (
231
- values_data . len( ) , len * self . list_len as usize ,
197
+ values . len( ) , len * self . list_len as usize ,
232
198
"Length of the child array ({}) must be the multiple of the value length ({}) and the array length ({})." ,
233
- values_data . len( ) ,
199
+ values . len( ) ,
234
200
self . list_len,
235
201
len,
236
202
) ;
237
203
238
- let nulls = self . null_buffer_builder . finish_cloned ( ) ;
204
+ let field = self
205
+ . field
206
+ . clone ( )
207
+ . unwrap_or_else ( || Arc :: new ( Field :: new ( "item" , values. data_type ( ) . clone ( ) , true ) ) ) ;
239
208
240
- let field = match & self . field {
241
- Some ( f) => {
242
- let size = self . value_length ( ) ;
243
- assert_eq ! (
244
- f. data_type( ) ,
245
- values_data. data_type( ) ,
246
- "DataType of field ({}) should be the same as the values_builder DataType ({})" ,
247
- f. data_type( ) ,
248
- values_data. data_type( )
249
- ) ;
250
- if let Some ( a) = values_arr. logical_nulls ( ) {
251
- let nulls_valid = f. is_nullable ( )
252
- || nulls
253
- . as_ref ( )
254
- . map ( |n| n. expand ( size as _ ) . contains ( & a) )
255
- . unwrap_or_default ( ) ;
256
-
257
- assert ! (
258
- nulls_valid,
259
- "Found unmasked nulls for non-nullable FixedSizeListBuilder field {:?}" ,
260
- f. name( )
261
- ) ;
262
- }
263
- f. clone ( )
264
- }
265
- None => Arc :: new ( Field :: new ( "item" , values_data. data_type ( ) . clone ( ) , true ) ) ,
266
- } ;
267
-
268
- let array_data = ArrayData :: builder ( DataType :: FixedSizeList ( field, self . list_len ) )
269
- . len ( len)
270
- . add_child_data ( values_data)
271
- . nulls ( nulls) ;
272
-
273
- let array_data = unsafe { array_data. build_unchecked ( ) } ;
274
-
275
- FixedSizeListArray :: from ( array_data)
209
+ FixedSizeListArray :: new ( field, self . list_len , values, nulls)
276
210
}
277
211
}
278
212
279
213
#[ cfg( test) ]
280
214
mod tests {
281
215
use super :: * ;
216
+ use arrow_schema:: DataType ;
282
217
283
218
use crate :: builder:: Int32Builder ;
284
219
use crate :: Array ;
@@ -368,7 +303,7 @@ mod tests {
368
303
}
369
304
370
305
#[ test]
371
- #[ should_panic( expected = "Found unmasked nulls for non-nullable FixedSizeListBuilder field " ) ]
306
+ #[ should_panic( expected = "Found unmasked nulls for non-nullable FixedSizeListArray " ) ]
372
307
fn test_fixed_size_list_array_builder_with_field_null_panic ( ) {
373
308
let builder = make_list_builder ( true , true ) ;
374
309
let mut builder = builder. with_field ( Field :: new ( "list_item" , DataType :: Int32 , false ) ) ;
@@ -377,9 +312,7 @@ mod tests {
377
312
}
378
313
379
314
#[ test]
380
- #[ should_panic(
381
- expected = "DataType of field (Int64) should be the same as the values_builder DataType (Int32)"
382
- ) ]
315
+ #[ should_panic( expected = "FixedSizeListArray expected data type Int64 got Int32" ) ]
383
316
fn test_fixed_size_list_array_builder_with_field_type_panic ( ) {
384
317
let values_builder = Int32Builder :: new ( ) ;
385
318
let builder = FixedSizeListBuilder :: new ( values_builder, 3 ) ;
@@ -417,7 +350,7 @@ mod tests {
417
350
}
418
351
419
352
#[ test]
420
- #[ should_panic( expected = "Found unmasked nulls for non-nullable FixedSizeListBuilder field " ) ]
353
+ #[ should_panic( expected = "Found unmasked nulls for non-nullable FixedSizeListArray " ) ]
421
354
fn test_fixed_size_list_array_builder_cloned_with_field_null_panic ( ) {
422
355
let builder = make_list_builder ( true , true ) ;
423
356
let builder = builder. with_field ( Field :: new ( "list_item" , DataType :: Int32 , false ) ) ;
@@ -439,9 +372,7 @@ mod tests {
439
372
}
440
373
441
374
#[ test]
442
- #[ should_panic(
443
- expected = "DataType of field (Int64) should be the same as the values_builder DataType (Int32)"
444
- ) ]
375
+ #[ should_panic( expected = "FixedSizeListArray expected data type Int64 got Int32" ) ]
445
376
fn test_fixed_size_list_array_builder_cloned_with_field_type_panic ( ) {
446
377
let builder = make_list_builder ( false , false ) ;
447
378
let builder = builder. with_field ( Field :: new ( "list_item" , DataType :: Int64 , true ) ) ;
0 commit comments