jrmccluskey commented on code in PR #39883:
URL: https://github.com/apache/beam/pull/39883#discussion_r3926012784


##########
sdks/java/io/iceberg/src/test/java/org/apache/beam/sdk/io/iceberg/TableMetadataDriverTest.java:
##########
@@ -0,0 +1,723 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.iceberg;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.coders.RowCoder;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.testing.PAssert;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.testing.TestStream;
+import org.apache.beam.sdk.transforms.Create;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.transforms.windowing.FixedWindows;
+import org.apache.beam.sdk.transforms.windowing.Window;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.PCollectionView;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TimestampedValue;
+import org.apache.beam.sdk.values.ValueInSingleWindow;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.CatalogProperties;
+import org.apache.iceberg.CatalogUtil;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.PartitionKey;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.types.Types;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class TableMetadataDriverTest implements Serializable {
+
+  @Rule public transient TestPipeline pipeline = TestPipeline.create();
+  @Rule public transient TemporaryFolder tempFolder = new TemporaryFolder();
+
+  private String warehouseLocation;
+  private IcebergCatalogConfig catalogConfig;
+
+  private static final Schema BEAM_SCHEMA =
+      Schema.builder()
+          .addInt64Field("id")
+          .addStringField("data")
+          .addNullableStringField("dest")
+          .build();
+
+  private static final org.apache.iceberg.Schema ICEBERG_SCHEMA =
+      IcebergUtils.beamSchemaToIcebergSchema(
+          Schema.builder().addInt64Field("id").addStringField("data").build());
+
+  private static final TableIdentifier TABLE_ID = 
TableIdentifier.of("default", "table");
+
+  private static final DynamicDestinations SINGLE_TABLE_DYNAMIC_DESTINATIONS =
+      DynamicDestinations.singleTable(TABLE_ID, BEAM_SCHEMA);
+
+  private static final DynamicDestinations DYNAMIC_DESTINATIONS =
+      new DynamicDestinations() {
+        @Override
+        public Schema getDataSchema() {
+          return BEAM_SCHEMA;
+        }
+
+        @Override
+        public Row getData(Row element) {
+          return element;
+        }
+
+        @Override
+        public IcebergDestination instantiateDestination(String destination) {
+          return IcebergDestination.builder()
+              
.setTableIdentifier(IcebergUtils.parseTableIdentifier(destination))
+              .build();
+        }
+
+        @Override
+        public String getTableStringIdentifier(ValueInSingleWindow<Row> 
element) {
+          return element.getValue().getString("dest");
+        }
+      };
+
+  @Before
+  public void setUp() throws Exception {
+    warehouseLocation = "file:" + tempFolder.newFolder().getAbsolutePath();
+    catalogConfig =
+        IcebergCatalogConfig.builder()
+            .setCatalogName("hadoop")
+            .setCatalogProperties(ImmutableMap.of("type", "hadoop", 
"warehouse", warehouseLocation))
+            .build();
+  }
+
+  private Catalog getCatalog() {
+    return CatalogUtil.loadCatalog(
+        CatalogUtil.ICEBERG_CATALOG_HADOOP,
+        "hadoop",
+        ImmutableMap.of(CatalogProperties.WAREHOUSE_LOCATION, 
warehouseLocation),
+        new Configuration());
+  }
+
+  @Test
+  public void testSingleTableExtractionAndSpecOutput() {
+    Table realTable = getCatalog().createTable(TABLE_ID, ICEBERG_SCHEMA);
+
+    List<Row> rows = new ArrayList<>();
+    for (int i = 0; i < 5; i++) {
+      rows.add(
+          Row.withSchema(BEAM_SCHEMA)
+              .withFieldValue("id", (long) i)
+              .withFieldValue("data", "val_" + i)
+              .withFieldValue("dest", null)
+              .build());
+    }
+
+    PCollection<Row> input = 
pipeline.apply(Create.of(rows)).setCoder(RowCoder.of(BEAM_SCHEMA));
+
+    PCollection<KV<String, SerializableTableSpec>> specs =
+        input.apply(
+            TableMetadataDriver.builder()
+                .setCatalogConfig(catalogConfig)
+                .setDynamicDestinations(SINGLE_TABLE_DYNAMIC_DESTINATIONS)
+                .build());
+
+    String expectedTableIdString = 
IcebergUtils.tableIdentifierToString(TABLE_ID);
+
+    PAssert.that(specs)
+        .satisfies(
+            elements -> {
+              List<KV<String, SerializableTableSpec>> list = 
ImmutableList.copyOf(elements);
+              assertEquals(1, list.size());
+              KV<String, SerializableTableSpec> kv = list.get(0);
+              assertEquals(expectedTableIdString, kv.getKey());
+              SerializableTableSpec spec = kv.getValue();
+              assertNotNull(spec);
+              assertEquals(realTable.name(), spec.getName());
+              assertEquals(realTable.location(), spec.getLocation());
+              assertEquals(realTable.schema().asStruct(), 
spec.getSchema().asStruct());
+              assertEquals(realTable.spec(), spec.getPartitionSpec());
+              assertNotNull(spec.getFileIO());
+              return null;
+            });
+
+    pipeline.run();
+  }
+
+  @Test
+  public void testMultipleDynamicDestinationsExtraction() {
+    Catalog catalog = getCatalog();
+    TableIdentifier tableA = TableIdentifier.of("default", "table_a");
+    TableIdentifier tableB = TableIdentifier.of("default", "table_b");
+    TableIdentifier tableC = TableIdentifier.of("default", "table_c");
+
+    catalog.createTable(tableA, ICEBERG_SCHEMA);
+    catalog.createTable(tableB, ICEBERG_SCHEMA);
+    catalog.createTable(tableC, ICEBERG_SCHEMA);
+
+    List<Row> rows =
+        ImmutableList.of(
+            Row.withSchema(BEAM_SCHEMA).addValues(1L, "v1", 
"default.table_a").build(),
+            Row.withSchema(BEAM_SCHEMA).addValues(2L, "v2", 
"default.table_b").build(),
+            Row.withSchema(BEAM_SCHEMA).addValues(3L, "v3", 
"default.table_c").build(),
+            Row.withSchema(BEAM_SCHEMA).addValues(4L, "v4", 
"default.table_a").build(),
+            Row.withSchema(BEAM_SCHEMA).addValues(5L, "v5", 
"default.table_b").build());
+
+    PCollection<Row> input = 
pipeline.apply(Create.of(rows)).setCoder(RowCoder.of(BEAM_SCHEMA));
+
+    PCollection<KV<String, SerializableTableSpec>> specs =
+        input.apply(
+            TableMetadataDriver.builder()
+                .setCatalogConfig(catalogConfig)
+                .setDynamicDestinations(DYNAMIC_DESTINATIONS)
+                .build());
+
+    PAssert.that(specs)
+        .satisfies(
+            elements -> {
+              List<KV<String, SerializableTableSpec>> list = 
ImmutableList.copyOf(elements);
+              assertEquals(3, list.size());
+              Map<String, SerializableTableSpec> map =
+                  
list.stream().collect(ImmutableMap.toImmutableMap(KV::getKey, KV::getValue));
+              assertTrue(map.containsKey("default.table_a"));
+              assertTrue(map.containsKey("default.table_b"));
+              assertTrue(map.containsKey("default.table_c"));
+              return null;
+            });
+
+    pipeline.run();
+  }
+
+  @Test
+  public void testWindowedDeduplication() {
+    Catalog catalog = getCatalog();
+    TableIdentifier table1 = TableIdentifier.of("default", "t1");
+    TableIdentifier table2 = TableIdentifier.of("default", "t2");
+
+    catalog.createTable(table1, ICEBERG_SCHEMA);
+    catalog.createTable(table2, ICEBERG_SCHEMA);
+
+    List<Row> rows = new ArrayList<>();
+    for (int i = 0; i < 100; i++) {
+      String dest = (i % 2 == 0) ? "default.t1" : "default.t2";
+      rows.add(Row.withSchema(BEAM_SCHEMA).addValues((long) i, "val_" + i, 
dest).build());
+    }
+
+    PCollection<Row> input = 
pipeline.apply(Create.of(rows)).setCoder(RowCoder.of(BEAM_SCHEMA));
+
+    PCollection<KV<String, SerializableTableSpec>> specs =
+        input.apply(
+            TableMetadataDriver.builder()
+                .setCatalogConfig(catalogConfig)
+                .setDynamicDestinations(DYNAMIC_DESTINATIONS)
+                .build());
+
+    PAssert.that(specs)
+        .satisfies(
+            elements -> {
+              List<KV<String, SerializableTableSpec>> list = 
ImmutableList.copyOf(elements);
+              assertEquals(2, list.size());
+              Map<String, SerializableTableSpec> map =
+                  
list.stream().collect(ImmutableMap.toImmutableMap(KV::getKey, KV::getValue));
+              assertTrue(map.containsKey("default.t1"));
+              assertTrue(map.containsKey("default.t2"));
+              return null;
+            });
+
+    pipeline.run();
+  }
+
+  @Test
+  public void testUnboundedGlobalWindowStreamingDeduplication() {
+    Catalog catalog = getCatalog();
+    TableIdentifier table1 = TableIdentifier.of("default", "stream_t1");
+    TableIdentifier table2 = TableIdentifier.of("default", "stream_t2");
+
+    catalog.createTable(table1, ICEBERG_SCHEMA);
+    catalog.createTable(table2, ICEBERG_SCHEMA);
+
+    Row row1 = Row.withSchema(BEAM_SCHEMA).addValues(1L, "v1", 
"default.stream_t1").build();
+    Row row2 = Row.withSchema(BEAM_SCHEMA).addValues(2L, "v2", 
"default.stream_t2").build();
+    Row row3 = Row.withSchema(BEAM_SCHEMA).addValues(3L, "v3", 
"default.stream_t1").build();
+
+    TestStream<Row> stream =
+        TestStream.create(RowCoder.of(BEAM_SCHEMA))
+            .advanceWatermarkTo(new Instant(0))
+            .addElements(row1)
+            .addElements(row2)
+            .addElements(row3)
+            .advanceProcessingTime(Duration.standardSeconds(5))
+            .advanceWatermarkToInfinity();
+
+    PCollection<Row> input = pipeline.apply("StreamInput", stream);
+
+    PCollection<KV<String, SerializableTableSpec>> specs =
+        input.apply(
+            TableMetadataDriver.builder()
+                .setCatalogConfig(catalogConfig)
+                .setDynamicDestinations(DYNAMIC_DESTINATIONS)
+                .setRefreshInterval(Duration.standardSeconds(2))
+                .build());
+
+    PAssert.that(specs)
+        .satisfies(
+            elements -> {
+              List<KV<String, SerializableTableSpec>> list = 
ImmutableList.copyOf(elements);
+              assertEquals(2, list.size());
+              Map<String, SerializableTableSpec> map =
+                  
list.stream().collect(ImmutableMap.toImmutableMap(KV::getKey, KV::getValue));
+              assertTrue(map.containsKey("default.stream_t1"));
+              assertTrue(map.containsKey("default.stream_t2"));
+              return null;
+            });
+
+    pipeline.run();
+  }
+
+  @Test
+  public void testMetadataRefreshedAcrossIntervals() {

Review Comment:
   This was a good call, adding that test variation found a number of problems 
with streaming cases and the triggering setup. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to