cloud-fan commented on code in PR #47233:
URL: https://github.com/apache/spark/pull/47233#discussion_r1705634549


##########
sql/core/src/main/scala/org/apache/spark/sql/UpdateWriter.scala:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.spark.sql
+
+import org.apache.spark.annotation.Experimental
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, UpdateTable}
+import org.apache.spark.sql.functions.expr
+
+/**
+ * `UpdateWriter` provides methods to define and execute an update action on a 
target table.
+ *
+ * @param tableDF DataFrame representing table to update.
+ *
+ * @since 4.0.0
+ */
+@Experimental
+case class UpdateWriter (tableDF: DataFrame) {
+
+  /**
+   * @param assignments A Map of column names to Column expressions 
representing the updates
+   *     to be applied.
+   */
+  def set(assignments: Map[String, Column]): UpdateWithAssignment = {
+    UpdateWithAssignment(tableDF, assignments)
+  }
+}
+
+/**
+ * A class for defining a condition on an update operation or directly 
executing it.
+ *
+ * @param tableDF DataFrame representing table to update.
+ * @param assignment A Map of column names to Column expressions representing 
the updates
+ *     to be applied.
+ *
+ * @since 4.0.0
+ */
+@Experimental
+case class UpdateWithAssignment(tableDF: DataFrame, assignment: Map[String, 
Column]) {
+
+  private val sparkSession = tableDF.sparkSession
+  private val logicalPlan = tableDF.queryExecution.logical
+
+  /**
+   * Limits the update to rows matching the specified condition.
+   *
+   * @param condition the update condition
+   * @return
+   */
+  def where(condition: Column): UpdateWithCondition = {
+    UpdateWithCondition(tableDF, assignment, condition)
+  }
+
+  /**
+   * Executes the update operation.
+   */
+  def execute(): Unit = {
+    val update = UpdateTable(
+      logicalPlan,
+      assignment.map(x => Assignment(expr(x._1).expr, x._2.expr)).toSeq,
+      None)
+    val qe = sparkSession.sessionState.executePlan(update)
+    qe.assertCommandExecuted()
+  }
+}
+
+/**
+ * A class for executing an update operation.
+ *
+ * @param tableDF DataFrame representing table to update.
+ * @param assignments A Map of column names to Column expressions representing 
the updates
+ *     to be applied.
+ * @param condition the update condition
+ * @since 4.0.0
+ */
+@Experimental
+case class UpdateWithCondition(
+    tableDF: DataFrame,
+    assignments: Map[String, Column],
+    condition: Column) {

Review Comment:
   should this be optional? then the implementation of 
`UpdateWithAssignment#execute` can be `new UpdateWithCondition(..., 
None).execute()`



-- 
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: reviews-unsubscr...@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to