@@ -118,7 +118,7 @@ impl SchemaVisitor for GlueSchemaBuilder {
118
118
( ICEBERG_FIELD_ID . to_string ( ) , format ! ( "{}" , field. id) ) ,
119
119
(
120
120
ICEBERG_FIELD_OPTIONAL . to_string ( ) ,
121
- format ! ( "{}" , field. required) . to_lowercase ( ) ,
121
+ format ! ( "{}" , ! field. required) . to_lowercase ( ) ,
122
122
) ,
123
123
(
124
124
ICEBERG_FIELD_CURRENT . to_string ( ) ,
@@ -209,10 +209,11 @@ mod tests {
209
209
name : impl Into < String > ,
210
210
r#type : impl Into < String > ,
211
211
id : impl Into < String > ,
212
+ optional : bool ,
212
213
) -> Result < Column > {
213
214
let parameters = HashMap :: from ( [
214
215
( ICEBERG_FIELD_ID . to_string ( ) , id. into ( ) ) ,
215
- ( ICEBERG_FIELD_OPTIONAL . to_string ( ) , "true" . to_string ( ) ) ,
216
+ ( ICEBERG_FIELD_OPTIONAL . to_string ( ) , optional . to_string ( ) ) ,
216
217
( ICEBERG_FIELD_CURRENT . to_string ( ) , "true" . to_string ( ) ) ,
217
218
] ) ;
218
219
@@ -318,19 +319,19 @@ mod tests {
318
319
let result = GlueSchemaBuilder :: from_iceberg ( & metadata) ?. build ( ) ;
319
320
320
321
let expected = vec ! [
321
- create_column( "c1" , "boolean" , "1" ) ?,
322
- create_column( "c2" , "int" , "2" ) ?,
323
- create_column( "c3" , "bigint" , "3" ) ?,
324
- create_column( "c4" , "float" , "4" ) ?,
325
- create_column( "c5" , "double" , "5" ) ?,
326
- create_column( "c6" , "decimal(2,2)" , "6" ) ?,
327
- create_column( "c7" , "date" , "7" ) ?,
328
- create_column( "c8" , "string" , "8" ) ?,
329
- create_column( "c9" , "timestamp" , "9" ) ?,
330
- create_column( "c10" , "string" , "10" ) ?,
331
- create_column( "c11" , "string" , "11" ) ?,
332
- create_column( "c12" , "binary" , "12" ) ?,
333
- create_column( "c13" , "binary" , "13" ) ?,
322
+ create_column( "c1" , "boolean" , "1" , false ) ?,
323
+ create_column( "c2" , "int" , "2" , false ) ?,
324
+ create_column( "c3" , "bigint" , "3" , false ) ?,
325
+ create_column( "c4" , "float" , "4" , false ) ?,
326
+ create_column( "c5" , "double" , "5" , false ) ?,
327
+ create_column( "c6" , "decimal(2,2)" , "6" , false ) ?,
328
+ create_column( "c7" , "date" , "7" , false ) ?,
329
+ create_column( "c8" , "string" , "8" , false ) ?,
330
+ create_column( "c9" , "timestamp" , "9" , false ) ?,
331
+ create_column( "c10" , "string" , "10" , false ) ?,
332
+ create_column( "c11" , "string" , "11" , false ) ?,
333
+ create_column( "c12" , "binary" , "12" , false ) ?,
334
+ create_column( "c13" , "binary" , "13" , false ) ?,
334
335
] ;
335
336
336
337
assert_eq ! ( result, expected) ;
@@ -378,6 +379,7 @@ mod tests {
378
379
"person" ,
379
380
"struct<name:string, age:int>" ,
380
381
"1" ,
382
+ false ,
381
383
) ?] ;
382
384
383
385
assert_eq ! ( result, expected) ;
@@ -432,6 +434,7 @@ mod tests {
432
434
"location" ,
433
435
"array<struct<latitude:float, longitude:float>>" ,
434
436
"1" ,
437
+ false ,
435
438
) ?] ;
436
439
437
440
assert_eq ! ( result, expected) ;
@@ -475,10 +478,50 @@ mod tests {
475
478
476
479
let result = GlueSchemaBuilder :: from_iceberg ( & metadata) ?. build ( ) ;
477
480
478
- let expected = vec ! [ create_column( "quux" , "map<string,map<string,int>>" , "1" ) ?] ;
481
+ let expected = vec ! [ create_column(
482
+ "quux" ,
483
+ "map<string,map<string,int>>" ,
484
+ "1" ,
485
+ false ,
486
+ ) ?] ;
479
487
480
488
assert_eq ! ( result, expected) ;
481
489
482
490
Ok ( ( ) )
483
491
}
492
+
493
+ #[ test]
494
+ fn test_schema_with_optional_fields ( ) -> Result < ( ) > {
495
+ let record = r#"{
496
+ "type": "struct",
497
+ "schema-id": 1,
498
+ "fields": [
499
+ {
500
+ "id": 1,
501
+ "name": "required_field",
502
+ "required": true,
503
+ "type": "string"
504
+ },
505
+ {
506
+ "id": 2,
507
+ "name": "optional_field",
508
+ "required": false,
509
+ "type": "int"
510
+ }
511
+ ]
512
+ }"# ;
513
+
514
+ let schema = serde_json:: from_str :: < Schema > ( record) ?;
515
+ let metadata = create_metadata ( schema) ?;
516
+
517
+ let result = GlueSchemaBuilder :: from_iceberg ( & metadata) ?. build ( ) ;
518
+
519
+ let expected = vec ! [
520
+ create_column( "required_field" , "string" , "1" , false ) ?,
521
+ create_column( "optional_field" , "int" , "2" , true ) ?,
522
+ ] ;
523
+
524
+ assert_eq ! ( result, expected) ;
525
+ Ok ( ( ) )
526
+ }
484
527
}
0 commit comments