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


##########
sdks/java/io/iceberg/src/test/java/org/apache/beam/sdk/io/iceberg/TableMetadataDriverTest.java:
##########
@@ -0,0 +1,581 @@
+/*
+ * 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.transforms.Create;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.ParDo;
+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.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());
+
+  @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() {
+    TableIdentifier tableId = TableIdentifier.of("default", "single_table");
+    Table realTable = getCatalog().createTable(tableId, ICEBERG_SCHEMA);
+
+    DynamicDestinations dynamicDestinations = 
DynamicDestinations.singleTable(tableId, BEAM_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(dynamicDestinations)
+                .build());
+
+    String expectedTableIdString = 
IcebergUtils.tableIdentifierToString(tableId);
+
+    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);
+
+    DynamicDestinations dynamicDestinations =
+        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");
+          }
+        };
+
+    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(dynamicDestinations)
+                .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");

Review Comment:
   This is what I get for leaning on Gemini to produce some unit tests. The 
name is misleading, it's really testing that we deduplicate the target table 
IDs when we build the spec (since there are 50 elements referring to t1 and 50 
elements referring to t2, we deduplicate that to single references to the two 
tables.) I'll rename this, the window doesn't really matter



-- 
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