@@ -118,7 +118,7 @@ impl SchemaVisitor for GlueSchemaBuilder {
118118 ( ICEBERG_FIELD_ID . to_string ( ) , format ! ( "{}" , field. id) ) ,
119119 (
120120 ICEBERG_FIELD_OPTIONAL . to_string ( ) ,
121- format ! ( "{}" , field. required) . to_lowercase ( ) ,
121+ format ! ( "{}" , ! field. required) . to_lowercase ( ) ,
122122 ) ,
123123 (
124124 ICEBERG_FIELD_CURRENT . to_string ( ) ,
@@ -209,10 +209,11 @@ mod tests {
209209 name : impl Into < String > ,
210210 r#type : impl Into < String > ,
211211 id : impl Into < String > ,
212+ optional : bool ,
212213 ) -> Result < Column > {
213214 let parameters = HashMap :: from ( [
214215 ( 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 ( ) ) ,
216217 ( ICEBERG_FIELD_CURRENT . to_string ( ) , "true" . to_string ( ) ) ,
217218 ] ) ;
218219
@@ -318,19 +319,19 @@ mod tests {
318319 let result = GlueSchemaBuilder :: from_iceberg ( & metadata) ?. build ( ) ;
319320
320321 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 ) ?,
334335 ] ;
335336
336337 assert_eq ! ( result, expected) ;
@@ -378,6 +379,7 @@ mod tests {
378379 "person" ,
379380 "struct<name:string, age:int>" ,
380381 "1" ,
382+ false ,
381383 ) ?] ;
382384
383385 assert_eq ! ( result, expected) ;
@@ -432,6 +434,7 @@ mod tests {
432434 "location" ,
433435 "array<struct<latitude:float, longitude:float>>" ,
434436 "1" ,
437+ false ,
435438 ) ?] ;
436439
437440 assert_eq ! ( result, expected) ;
@@ -475,10 +478,50 @@ mod tests {
475478
476479 let result = GlueSchemaBuilder :: from_iceberg ( & metadata) ?. build ( ) ;
477480
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+ ) ?] ;
479487
480488 assert_eq ! ( result, expected) ;
481489
482490 Ok ( ( ) )
483491 }
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+ }
484527}
0 commit comments