@@ -316,25 +316,23 @@ impl Field {
316
316
let logical_type = self . ty . logical_type ( ) ;
317
317
let repetition = self . ty . repetition ( ) ;
318
318
let converted_type = self . ty . converted_type ( ) ;
319
+ let length = self . ty . length ( ) ;
320
+
321
+ let mut builder = quote ! {
322
+ ParquetType :: primitive_type_builder( #field_name, #physical_type)
323
+ . with_logical_type( #logical_type)
324
+ . with_repetition( #repetition)
325
+ } ;
319
326
320
327
if let Some ( converted_type) = converted_type {
321
- quote ! {
322
- fields. push( ParquetType :: primitive_type_builder( #field_name, #physical_type)
323
- . with_logical_type( #logical_type)
324
- . with_repetition( #repetition)
325
- . with_converted_type( #converted_type)
326
- . build( ) . unwrap( ) . into( )
327
- )
328
- }
329
- } else {
330
- quote ! {
331
- fields. push( ParquetType :: primitive_type_builder( #field_name, #physical_type)
332
- . with_logical_type( #logical_type)
333
- . with_repetition( #repetition)
334
- . build( ) . unwrap( ) . into( )
335
- )
336
- }
328
+ builder = quote ! { #builder. with_converted_type( #converted_type) } ;
329
+ }
330
+
331
+ if let Some ( length) = length {
332
+ builder = quote ! { #builder. with_length( #length) } ;
337
333
}
334
+
335
+ quote ! { fields. push( #builder. build( ) . unwrap( ) . into( ) ) }
338
336
}
339
337
340
338
fn option_into_vals ( & self ) -> proc_macro2:: TokenStream {
@@ -394,7 +392,7 @@ impl Field {
394
392
quote ! { rec. #field_name. signed_duration_since( :: chrono:: NaiveDate :: from_ymd( 1970 , 1 , 1 ) ) . num_days( ) as i32 }
395
393
}
396
394
Some ( ThirdPartyType :: Uuid ) => {
397
- quote ! { ( & rec. #field_name. to_string ( ) [ .. ] ) . into( ) }
395
+ quote ! { rec. #field_name. as_bytes ( ) . to_vec ( ) . into( ) }
398
396
}
399
397
_ => {
400
398
if self . is_a_byte_buf {
@@ -430,7 +428,7 @@ impl Field {
430
428
}
431
429
}
432
430
Some ( ThirdPartyType :: Uuid ) => {
433
- quote ! { :: uuid:: Uuid :: parse_str ( vals[ i] . data( ) . convert ( ) ) . unwrap( ) }
431
+ quote ! { :: uuid:: Uuid :: from_bytes ( vals[ i] . data( ) . try_into ( ) . unwrap( ) ) }
434
432
}
435
433
_ => match & self . ty {
436
434
Type :: TypePath ( _) => match self . ty . last_part ( ) . as_str ( ) {
@@ -638,11 +636,40 @@ impl Type {
638
636
}
639
637
"f32" => BasicType :: FLOAT ,
640
638
"f64" => BasicType :: DOUBLE ,
641
- "String" | "str" | "Uuid" => BasicType :: BYTE_ARRAY ,
639
+ "String" | "str" => BasicType :: BYTE_ARRAY ,
640
+ "Uuid" => BasicType :: FIXED_LEN_BYTE_ARRAY ,
642
641
f => unimplemented ! ( "{} currently is not supported" , f) ,
643
642
}
644
643
}
645
644
645
+ fn length ( & self ) -> Option < i32 > {
646
+ let last_part = self . last_part ( ) ;
647
+ let leaf_type = self . leaf_type_recursive ( ) ;
648
+
649
+ match leaf_type {
650
+ Type :: Array ( ref first_type) => {
651
+ if let Type :: TypePath ( _) = * * first_type {
652
+ if last_part == "u8" {
653
+ return Some ( 1 ) ;
654
+ }
655
+ }
656
+ }
657
+ Type :: Vec ( ref first_type) | Type :: Slice ( ref first_type) => {
658
+ if let Type :: TypePath ( _) = * * first_type {
659
+ if last_part == "u8" {
660
+ return None ;
661
+ }
662
+ }
663
+ }
664
+ _ => ( ) ,
665
+ }
666
+
667
+ match last_part. trim ( ) {
668
+ "Uuid" => Some ( 16 ) ,
669
+ _ => None ,
670
+ }
671
+ }
672
+
646
673
fn logical_type ( & self ) -> proc_macro2:: TokenStream {
647
674
let last_part = self . last_part ( ) ;
648
675
let leaf_type = self . leaf_type_recursive ( ) ;
@@ -1328,8 +1355,8 @@ mod test {
1328
1355
let when = Field :: from ( & fields[ 0 ] ) ;
1329
1356
assert_eq ! ( when. writer_snippet( ) . to_string( ) , ( quote!{
1330
1357
{
1331
- let vals : Vec <_> = records. iter( ) . map( |rec| ( & rec. unique_id. to_string ( ) [ .. ] ) . into( ) ) . collect( ) ;
1332
- if let ColumnWriter :: ByteArrayColumnWriter ( ref mut typed) = column_writer. untyped( ) {
1358
+ let vals : Vec <_> = records. iter( ) . map( |rec| rec. unique_id. as_bytes ( ) . to_vec ( ) . into( ) ) . collect( ) ;
1359
+ if let ColumnWriter :: FixedLenByteArrayColumnWriter ( ref mut typed) = column_writer. untyped( ) {
1333
1360
typed. write_batch( & vals[ ..] , None , None ) ?;
1334
1361
} else {
1335
1362
panic!( "Schema and struct disagree on type for {}" , stringify!{ unique_id } )
@@ -1349,7 +1376,7 @@ mod test {
1349
1376
}
1350
1377
} ) . collect( ) ;
1351
1378
1352
- if let ColumnWriter :: ByteArrayColumnWriter ( ref mut typed) = column_writer. untyped( ) {
1379
+ if let ColumnWriter :: FixedLenByteArrayColumnWriter ( ref mut typed) = column_writer. untyped( ) {
1353
1380
typed. write_batch( & vals[ ..] , Some ( & definition_levels[ ..] ) , None ) ?;
1354
1381
} else {
1355
1382
panic!( "Schema and struct disagree on type for {}" , stringify!{ maybe_unique_id } )
@@ -1371,13 +1398,13 @@ mod test {
1371
1398
assert_eq ! ( when. reader_snippet( ) . to_string( ) , ( quote!{
1372
1399
{
1373
1400
let mut vals = Vec :: new( ) ;
1374
- if let ColumnReader :: ByteArrayColumnReader ( mut typed) = column_reader {
1401
+ if let ColumnReader :: FixedLenByteArrayColumnReader ( mut typed) = column_reader {
1375
1402
typed. read_records( num_records, None , None , & mut vals) ?;
1376
1403
} else {
1377
1404
panic!( "Schema and struct disagree on type for {}" , stringify!{ unique_id } ) ;
1378
1405
}
1379
1406
for ( i, r) in & mut records[ ..num_records] . iter_mut( ) . enumerate( ) {
1380
- r. unique_id = :: uuid:: Uuid :: parse_str ( vals[ i] . data( ) . convert ( ) ) . unwrap( ) ;
1407
+ r. unique_id = :: uuid:: Uuid :: from_bytes ( vals[ i] . data( ) . try_into ( ) . unwrap( ) ) ;
1381
1408
}
1382
1409
}
1383
1410
} ) . to_string( ) ) ;
0 commit comments