forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48821][SQL] Support Update in DataFrameWriterV2
- Loading branch information
Showing
4 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
sql/core/src/main/scala/org/apache/spark/sql/UpdateWriter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 | ||
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 = { | ||
new 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 | ||
class UpdateWithAssignment(tableDF: DataFrame, assignment: Map[String, Column]) { | ||
|
||
/** | ||
* Limits the update to rows matching the specified condition. | ||
* | ||
* @param condition the update condition | ||
* @return | ||
*/ | ||
def where(condition: Column): UpdateWithCondition = { | ||
new UpdateWithCondition(tableDF, assignment, Some(condition)) | ||
} | ||
|
||
/** | ||
* Executes the update operation. | ||
*/ | ||
def execute(): Unit = { | ||
new UpdateWithCondition(tableDF, assignment, None) | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
class UpdateWithCondition( | ||
tableDF: DataFrame, | ||
assignments: Map[String, Column], | ||
condition: Option[Column]) { | ||
|
||
private val sparkSession = tableDF.sparkSession | ||
private val logicalPlan = tableDF.queryExecution.logical | ||
|
||
/** | ||
* Executes the update operation. | ||
*/ | ||
def execute(): Unit = { | ||
val update = UpdateTable( | ||
logicalPlan, | ||
assignments.map(x => Assignment(expr(x._1).expr, x._2.expr)).toSeq, | ||
condition.map(_.expr)) | ||
val qe = sparkSession.sessionState.executePlan(update) | ||
qe.assertCommandExecuted() | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
sql/core/src/test/scala/org/apache/spark/sql/connector/UpdateDataFrameSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.connector | ||
|
||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.functions._ | ||
|
||
class UpdateDataFrameSuite extends RowLevelOperationSuiteBase { | ||
|
||
import testImplicits._ | ||
|
||
test("Basic Update") { | ||
createAndInitTable("pk INT, salary INT, dep STRING", | ||
"""{ "pk": 1, "salary": 300, "dep": 'hr' } | ||
|{ "pk": 2, "salary": 150, "dep": 'software' } | ||
|{ "pk": 3, "salary": 120, "dep": 'hr' } | ||
|""".stripMargin) | ||
|
||
spark.update(tableNameAsString) | ||
.set(Map("salary" -> lit(-1))) | ||
.where($"pk" >= 2) | ||
.execute() | ||
|
||
checkAnswer( | ||
sql(s"SELECT * FROM $tableNameAsString"), | ||
Seq( | ||
Row(1, 300, "hr"), | ||
Row(2, -1, "software"), | ||
Row(3, -1, "hr"))) | ||
} | ||
|
||
test("Update without where clause") { | ||
createAndInitTable("pk INT, salary INT, dep STRING", | ||
"""{ "pk": 1, "salary": 300, "dep": 'hr' } | ||
|{ "pk": 2, "salary": 150, "dep": 'software' } | ||
|{ "pk": 3, "salary": 120, "dep": 'hr' } | ||
|""".stripMargin) | ||
|
||
spark.update(tableNameAsString) | ||
.set(Map("dep" -> lit("software"))) | ||
.execute() | ||
|
||
checkAnswer( | ||
sql(s"SELECT * FROM $tableNameAsString"), | ||
Seq( | ||
Row(1, 300, "software"), | ||
Row(2, 150, "software"), | ||
Row(3, 120, "software"))) | ||
} | ||
} |