1717 */
1818package org .apache .beam .sdk .io .iceberg .catalog ;
1919
20+ import static org .apache .beam .sdk .io .iceberg .IcebergUtils .beamSchemaToIcebergSchema ;
21+ import static org .apache .beam .sdk .io .iceberg .IcebergUtils .icebergSchemaToBeamSchema ;
2022import static org .apache .beam .sdk .managed .Managed .ICEBERG ;
2123import static org .apache .beam .sdk .managed .Managed .ICEBERG_CDC ;
2224import static org .apache .beam .sdk .util .Preconditions .checkStateNotNull ;
@@ -313,7 +315,7 @@ public Row apply(Long num) {
313315 };
314316
315317 protected static final org .apache .iceberg .Schema ICEBERG_SCHEMA =
316- IcebergUtils . beamSchemaToIcebergSchema (BEAM_SCHEMA );
318+ beamSchemaToIcebergSchema (BEAM_SCHEMA );
317319 protected static final SimpleFunction <Row , Record > RECORD_FUNC =
318320 new SimpleFunction <Row , Record >() {
319321 @ Override
@@ -346,7 +348,7 @@ private List<Row> populateTable(Table table, @Nullable String charOverride) thro
346348 }
347349 DataWriter <Record > writer =
348350 Parquet .writeData (file )
349- .schema (ICEBERG_SCHEMA )
351+ .schema (table . schema () )
350352 .createWriterFunc (GenericParquetWriter ::create )
351353 .overwrite ()
352354 .withSpec (table .spec ())
@@ -652,7 +654,7 @@ public void testWrite() throws IOException {
652654 pipeline .run ().waitUntilFinish ();
653655
654656 Table table = catalog .loadTable (TableIdentifier .parse (tableId ()));
655- assertTrue ( table .schema (). sameSchema ( ICEBERG_SCHEMA ));
657+ assertEquals ( BEAM_SCHEMA , icebergSchemaToBeamSchema ( table .schema ()));
656658
657659 // Read back and check records are correct
658660 List <Record > returnedRecords = readRecords (table );
@@ -664,16 +666,23 @@ public void testWrite() throws IOException {
664666 public void testWriteToPartitionedTable () throws IOException {
665667 Map <String , Object > config = new HashMap <>(managedIcebergConfig (tableId ()));
666668 int truncLength = "value_x" .length ();
667- config . put (
668- "partition_fields" ,
669- Arrays . asList ( "bool_field " , "hour(datetime)" , "truncate(str, " + truncLength + ")" ) );
669+ List < String > partitionFields =
670+ Arrays . asList ( "bool_field" , "hour(datetime)" , "truncate(str, " + truncLength + ")" );
671+ config . put ( "partition_fields " , partitionFields );
670672 PCollection <Row > input = pipeline .apply (Create .of (inputRows )).setRowSchema (BEAM_SCHEMA );
671673 input .apply (Managed .write (ICEBERG ).withConfig (config ));
672674 pipeline .run ().waitUntilFinish ();
673675
674676 // Read back and check records are correct
675677 Table table = catalog .loadTable (TableIdentifier .parse (tableId ()));
676678 List <Record > returnedRecords = readRecords (table );
679+ PartitionSpec expectedSpec =
680+ PartitionSpec .builderFor (table .schema ())
681+ .identity ("bool_field" )
682+ .hour ("datetime" )
683+ .truncate ("str" , truncLength )
684+ .build ();
685+ assertEquals (expectedSpec , table .spec ());
677686 assertThat (
678687 returnedRecords , containsInAnyOrder (inputRows .stream ().map (RECORD_FUNC ::apply ).toArray ()));
679688 }
@@ -815,10 +824,8 @@ private void writeToDynamicDestinations(
815824 Table table3 = catalog .loadTable (TableIdentifier .parse (tableId () + "_3_d" ));
816825 Table table4 = catalog .loadTable (TableIdentifier .parse (tableId () + "_4_e" ));
817826
818- org .apache .iceberg .Schema tableSchema =
819- IcebergUtils .beamSchemaToIcebergSchema (rowFilter .outputSchema ());
820827 for (Table t : Arrays .asList (table0 , table1 , table2 , table3 , table4 )) {
821- assertTrue ( t . schema (). sameSchema ( tableSchema ));
828+ assertEquals ( rowFilter . outputSchema (), icebergSchemaToBeamSchema ( t . schema () ));
822829 }
823830
824831 // Read back and check records are correct
@@ -830,6 +837,7 @@ private void writeToDynamicDestinations(
830837 readRecords (table3 ),
831838 readRecords (table4 ));
832839
840+ org .apache .iceberg .Schema tableSchema = beamSchemaToIcebergSchema (rowFilter .outputSchema ());
833841 SerializableFunction <Row , Record > recordFunc =
834842 row -> IcebergUtils .beamRowToIcebergRecord (tableSchema , row );
835843
@@ -936,7 +944,7 @@ public void testWriteToDynamicNamespaces() throws IOException {
936944 table3false ,
937945 table4true ,
938946 table4false )) {
939- assertTrue ( t .schema (). sameSchema ( ICEBERG_SCHEMA ));
947+ assertEquals ( BEAM_SCHEMA , icebergSchemaToBeamSchema ( t .schema ()));
940948 }
941949
942950 // Read back and check records are correct
0 commit comments