You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "giucris (via GitHub)" <gi...@apache.org> on 2023/06/09 06:54:52 UTC

[GitHub] [iceberg] giucris commented on a diff in pull request #7607: Spark: Added Scala/Java IcebergMergeInto Api on spark3.4

giucris commented on code in PR #7607:
URL: https://github.com/apache/iceberg/pull/7607#discussion_r1223916115


##########
spark/v3.4/spark-extensions/src/main/scala/org/apache/iceberg/spark/extensions/IcebergMergeInto.scala:
##########
@@ -0,0 +1,399 @@
+/*
+ * 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 org.apache.spark.sql.Column
+import org.apache.spark.sql.Dataset
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.plans.logical.Assignment
+import org.apache.spark.sql.catalyst.plans.logical.DeleteAction
+import org.apache.spark.sql.catalyst.plans.logical.InsertAction
+import org.apache.spark.sql.catalyst.plans.logical.InsertStarAction
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.plans.logical.MergeAction
+import org.apache.spark.sql.catalyst.plans.logical.MergeIntoContext
+import org.apache.spark.sql.catalyst.plans.logical.UnresolvedMergeIntoIcebergTable
+import org.apache.spark.sql.catalyst.plans.logical.UpdateAction
+import org.apache.spark.sql.catalyst.plans.logical.UpdateStarAction
+import org.apache.spark.sql.functions.expr
+import scala.collection.JavaConverters
+
+
+object IcebergMergeInto {
+  /**
+   * Initialize an [[IcebergMergeIntoBuilder]].
+   *
+   * @param table : Target table name of the merge action
+   * @return [[IcebergMergeIntoBuilder]]
+   */
+  def apply(table: String): IcebergMergeIntoBuilder =
+    new IcebergMergeIntoBuilder(table, None, None, Seq.empty[MergeAction], Seq.empty[MergeAction])
+
+  /**
+   * Builder to specify an IcebergMergeInto action.
+   *
+   * It could be possible to provide any number of `whenMatched`
+   * and `whenNotMatched` actions.
+   *
+   *
+   * Scala Examples:
+   * {{{
+   *   val ds = ...
+   *   IcebergMergeInto("icebergTable")
+   *      .using(ds.as("source")
+   *      .when("source.id = icebergTable.id")
+   *      .whenMatched("source.op = U")
+   *      .updateAll()
+   *      .whenMatched("source.op = D)
+   *      .delete()
+   *      .whenNotMatched()
+   *      .insertAll()
+   * }}}
+   *
+   * JavaExamples:
+   * {{{
+   *   Dataset<Row> ds = ...
+   *   IcebergMergeInto.apply("icebergTable")
+   *      .using(ds.as("source")
+   *      .when("source.id = icebergTable.id")
+   *      .whenMatched("source.op = U")
+   *      .updateAll()
+   *      .whenMatched("source.op = D)
+   *      .delete()
+   *      .whenNotMatched()
+   *      .insertAll()
+   * }}}
+   *
+   */
+  class IcebergMergeIntoBuilder(
+                                 private val targetTable: String,
+                                 private val source: Option[Dataset[Row]],
+                                 private val whenCondition: Option[Expression],
+                                 private val whenMatchedActions: Seq[MergeAction],
+                                 private val whenNotMatchedActions: Seq[MergeAction]) {
+
+    /**
+     * Set the source dataset used during merge actions.
+     *
+     * @param source  : Dataset[Row]
+     * @return [[IcebergMergeIntoBuilder]]
+     */
+    def using(source: Dataset[Row]): IcebergMergeIntoBuilder =

Review Comment:
   @ConeyLiu In my opinion the main use of a Merge api is to handle the merge between a source dataset and a target table. To avoid having to register the dataset as a temporary table and perform the operation in sql. 
   
   In case both the "delta" and the target table are already registered I don't see why use this api in favour of a simple SQL query. 
   



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org