RussellSpitzer commented on code in PR #7607: URL: https://github.com/apache/iceberg/pull/7607#discussion_r1282420262
########## 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 = + new IcebergMergeIntoBuilder( + targetTable, + Some(source), + whenCondition, + whenMatchedActions, + whenNotMatchedActions + ) + + /** + * Set the `when` condition of the merge action from a [[Column]] + * + * @param condition : [[Column]] expression + * @return [[IcebergMergeIntoBuilder]] + */ + def when(condition: Column): IcebergMergeIntoBuilder = Review Comment: I would keep this as "on" because fundamentally this is setting shuffle join conditions (join on) while the other functions are only exercised at execution time. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
