rdblue commented on a change in pull request #1947:
URL: https://github.com/apache/iceberg/pull/1947#discussion_r559863470



##########
File path: 
spark3-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestMergeIntoTable.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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.iceberg.spark.extensions;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.spark.SparkCatalog;
+import org.apache.iceberg.spark.SparkSessionCatalog;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Assume;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runners.Parameterized;
+
+import static org.apache.iceberg.TableProperties.DEFAULT_FILE_FORMAT;
+import static org.apache.iceberg.TableProperties.PARQUET_VECTORIZATION_ENABLED;
+
+public class TestMergeIntoTable extends SparkRowLevelOperationsTestBase {
+  private final String sourceName;
+  private final String targetName;
+
+  @Parameterized.Parameters(
+      name = "catalogName = {0}, implementation = {1}, config = {2}, format = 
{3}, vectorized = {4}")
+  public static Object[][] parameters() {
+    return new Object[][] {
+        { "testhive", SparkCatalog.class.getName(),
+            ImmutableMap.of(
+                "type", "hive",
+                "default-namespace", "default"
+            ),
+            "parquet",
+            true
+        },
+        { "spark_catalog", SparkSessionCatalog.class.getName(),
+            ImmutableMap.of(
+                "type", "hive",
+                "default-namespace", "default",
+                "clients", "1",
+                "parquet-enabled", "false",
+                "cache-enabled", "false" // Spark will delete tables using v1, 
leaving the cache out of sync
+            ),
+            "parquet",
+            false
+        }
+    };
+  }
+
+  public TestMergeIntoTable(String catalogName, String implementation, 
Map<String, String> config,
+                            String fileFormat, Boolean vectorized) {
+    super(catalogName, implementation, config, fileFormat, vectorized);
+    this.sourceName = tableName("source");
+    this.targetName = tableName("target");
+  }
+
+  @BeforeClass
+  public static void setupSparkConf() {
+    spark.conf().set("spark.sql.shuffle.partitions", "4");
+  }
+
+  protected Map<String, String> extraTableProperties() {
+    return ImmutableMap.of(TableProperties.MERGE_MODE, 
TableProperties.MERGE_MODE_DEFAULT);
+  }
+
+  @Before
+  public void createTables() {
+    createAndInitUnPartitionedTargetTable(targetName);
+    createAndInitSourceTable(sourceName);
+  }
+
+  @After
+  public void removeTables() {
+    sql("DROP TABLE IF EXISTS %s", targetName);
+    sql("DROP TABLE IF EXISTS %s", sourceName);
+  }
+
+  @Test
+  public void testEmptyTargetInsertAllNonMatchingRows() throws 
NoSuchTableException {
+    append(sourceName, new Employee(1, "emp-id-1"), new Employee(2, 
"emp-id-2"), new Employee(3, "emp-id-3"));
+    String sqlText = "MERGE INTO %s AS target " +
+                     "USING %s AS source " +
+                     "ON target.id = source.id " +
+                     "WHEN NOT MATCHED THEN INSERT * ";
+
+    sql(sqlText, targetName, sourceName);
+    assertEquals("Should have expected rows",
+            ImmutableList.of(row(1, "emp-id-1"), row(2, "emp-id-2"), row(3, 
"emp-id-3")),
+            sql("SELECT * FROM %s ORDER BY id ASC NULLS LAST", targetName));
+  }
+
+  @Test
+  public void testEmptyTargetInsertOnlyMatchingRows() throws 
NoSuchTableException {
+    append(sourceName, new Employee(1, "emp-id-1"), new Employee(2, 
"emp-id-2"), new Employee(3, "emp-id-3"));
+    String sqlText = "MERGE INTO %s AS target " +
+                     "USING %s AS source " +
+                     "ON target.id = source.id " +
+                     "WHEN NOT MATCHED AND (source.id >= 2) THEN INSERT * ";
+
+    sql(sqlText, targetName, sourceName);
+    assertEquals("Should have expected rows",
+            ImmutableList.of(row(2, "emp-id-2"), row(3, "emp-id-3")),
+            sql("SELECT * FROM %s ORDER BY id ASC NULLS LAST", targetName));
+  }
+
+  @Test
+  public void testOnlyUpdate() throws NoSuchTableException {
+    append(targetName, new Employee(1, "emp-id-one"), new Employee(6, 
"emp-id-six"));
+    append(sourceName, new Employee(2, "emp-id-2"), new Employee(1, 
"emp-id-1"), new Employee(6, "emp-id-6"));
+    String sqlText = "MERGE INTO %s AS target " +
+            "USING %s AS source " +
+            "ON target.id = source.id " +
+            "WHEN MATCHED AND target.id = 1 THEN UPDATE SET * ";
+
+    sql(sqlText, targetName, sourceName);
+    assertEquals("Should have expected rows",
+            ImmutableList.of(row(1, "emp-id-1"), row(6, "emp-id-six")),
+            sql("SELECT * FROM %s ORDER BY id ASC NULLS LAST", targetName));
+  }
+
+  @Test
+  public void testOnlyDelete() throws NoSuchTableException {
+    append(targetName, new Employee(1, "emp-id-one"), new Employee(6, 
"emp-id-6"));
+    append(sourceName, new Employee(2, "emp-id-2"), new Employee(1, 
"emp-id-1"), new Employee(6, "emp-id-6"));
+    String sqlText = "MERGE INTO %s AS target " +
+            "USING %s AS source " +
+            "ON target.id = source.id " +
+            "WHEN MATCHED AND target.id = 6 THEN DELETE";
+
+    sql(sqlText, targetName, sourceName);
+    assertEquals("Should have expected rows",
+            ImmutableList.of(row(1, "emp-id-one")),
+            sql("SELECT * FROM %s ORDER BY id ASC NULLS LAST", targetName));
+  }
+
+  @Test
+  public void testAllCauses() throws NoSuchTableException {
+    append(targetName, new Employee(1, "emp-id-one"), new Employee(6, 
"emp-id-6"));
+    append(sourceName, new Employee(2, "emp-id-2"), new Employee(1, 
"emp-id-1"), new Employee(6, "emp-id-6"));
+    String sqlText = "MERGE INTO %s AS target " +
+                     "USING %s AS source " +
+                     "ON target.id = source.id " +
+                     "WHEN MATCHED AND target.id = 1 THEN UPDATE SET * " +
+                     "WHEN MATCHED AND target.id = 6 THEN DELETE " +
+                     "WHEN NOT MATCHED AND source.id = 2 THEN INSERT * ";
+
+    sql(sqlText, targetName, sourceName);
+    assertEquals("Should have expected rows",
+            ImmutableList.of(row(1, "emp-id-1"), row(2, "emp-id-2")),
+            sql("SELECT * FROM %s ORDER BY id ASC NULLS LAST", targetName));
+  }
+
+  @Test
+  public void testAllCausesWithExplicitColumnSpecification() throws 
NoSuchTableException {
+    append(targetName, new Employee(1, "emp-id-one"), new Employee(6, 
"emp-id-6"));
+    append(sourceName, new Employee(2, "emp-id-2"), new Employee(1, 
"emp-id-1"), new Employee(6, "emp-id-6"));
+    String sqlText = "MERGE INTO %s AS target " +
+            "USING %s AS source " +
+            "ON target.id = source.id " +
+            "WHEN MATCHED AND target.id = 1 THEN UPDATE SET target.id = 
source.id, target.dep = source.dep " +
+            "WHEN MATCHED AND target.id = 6 THEN DELETE " +
+            "WHEN NOT MATCHED AND source.id = 2 THEN INSERT (target.id, 
target.dep) VALUES (source.id, source.dep) ";
+
+    sql(sqlText, targetName, sourceName);
+    assertEquals("Should have expected rows",
+            ImmutableList.of(row(1, "emp-id-1"), row(2, "emp-id-2")),
+            sql("SELECT * FROM %s ORDER BY id ASC NULLS LAST", targetName));
+  }
+
+  @Test
+  public void testSourceCTE() throws NoSuchTableException {
+    Assume.assumeFalse(catalogName.equalsIgnoreCase("testhadoop"));
+    Assume.assumeFalse(catalogName.equalsIgnoreCase("testhive"));
+
+    append(targetName, new Employee(2, "emp-id-two"), new Employee(6, 
"emp-id-6"));
+    append(sourceName, new Employee(2, "emp-id-3"), new Employee(1, 
"emp-id-2"), new Employee(5, "emp-id-6"));
+    String sourceCTE = "WITH cte1 AS (SELECT id + 1 AS id, dep FROM source)";
+    String sqlText = sourceCTE + " " + "MERGE INTO %s AS target " +

Review comment:
       Nit: it looks like there are unnecessary string literals. `" " + "MERGE 
..."` can be updated to `" MERGE ..."`.




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to