xianyinxin commented on a change in pull request #25626: [SPARK-28892][SQL] Add
UPDATE support for DataSource V2
URL: https://github.com/apache/spark/pull/25626#discussion_r320645608
##########
File path:
sql/core/src/test/scala/org/apache/spark/sql/sources/v2/DataSourceV2SQLSuite.scala
##########
@@ -1767,6 +1769,240 @@ class DataSourceV2SQLSuite
}
}
+ test("Update: basic") {
+ val t = "testcat.ns1.ns2.tbl"
+ withTable(t) {
+ sql(
+ s"""
+ |CREATE TABLE $t (id bigint, name string, age int, p int)
+ |USING foo
+ |PARTITIONED BY (id, p)
+ """.stripMargin)
+ sql(
+ s"""
+ |INSERT INTO $t
+ |VALUES (1L, 'Herry', 26, 1),
+ |(2L, 'Jack', 31, 2),
+ |(3L, 'Lisa', 28, 3),
+ |(4L, 'Frank', 33, 3)
+ """.stripMargin)
+ sql(s"UPDATE $t SET name='Robert', age=32")
+
+ checkAnswer(spark.table(t),
+ Seq(Row(1, "Robert", 32, 1),
+ Row(2, "Robert", 32, 2),
+ Row(3, "Robert", 32, 3),
+ Row(4, "Robert", 32, 3)))
+ }
+ }
+
+ test("Update: update with where clause") {
+ val t = "testcat.ns1.ns2.tbl"
+ withTable(t) {
+ sql(
+ s"""
+ |CREATE TABLE $t (id bigint, name string, age int, p int)
+ |USING foo
+ |PARTITIONED BY (id, p)
+ """.stripMargin)
+ sql(
+ s"""
+ |INSERT INTO $t
+ |VALUES (1L, 'Herry', 26, 1),
+ |(2L, 'Jack', 31, 2),
+ |(3L, 'Lisa', 28, 3),
+ |(4L, 'Frank', 33, 3)
+ """.stripMargin)
+ sql(s"UPDATE $t SET name='Robert', age=32 where p=2")
+
+ checkAnswer(spark.table(t),
+ Seq(Row(1, "Herry", 26, 1),
+ Row(2, "Robert", 32, 2),
+ Row(3, "Lisa", 28, 3),
+ Row(4, "Frank", 33, 3)))
+ }
+ }
+
+ test("Update: update the partition key") {
+ val t = "testcat.ns1.ns2.tbl"
+ withTable(t) {
+ sql(
+ s"""
+ |CREATE TABLE $t (id bigint, name string, age int, p int)
+ |USING foo
+ |PARTITIONED BY (id, p)
+ """.stripMargin)
+ sql(
+ s"""
+ |INSERT INTO $t
+ |VALUES (1L, 'Herry', 26, 1),
+ |(2L, 'Jack', 31, 2),
+ |(3L, 'Lisa', 28, 3),
+ |(4L, 'Frank', 33, 3)
+ """.stripMargin)
+ sql(s"UPDATE $t SET p=4 where id=4")
+
+ checkAnswer(spark.table(t),
+ Seq(Row(1, "Herry", 26, 1),
+ Row(2, "Jack", 31, 2),
+ Row(3, "Lisa", 28, 3),
+ Row(4, "Frank", 33, 4)))
+ }
+ }
+
+ test("Update: update with aliased target table") {
+ val t = "testcat.ns1.ns2.tbl"
+ withTable(t) {
+ sql(
+ s"""
+ |CREATE TABLE $t (id bigint, name string, age int, p int)
+ |USING foo
+ |PARTITIONED BY (id, p)
+ """.stripMargin)
+ sql(
+ s"""
+ |INSERT INTO $t
+ |VALUES (1L, 'Herry', 26, 1),
+ |(2L, 'Jack', 31, 2),
+ |(3L, 'Lisa', 28, 3),
+ |(4L, 'Frank', 33, 3)
+ """.stripMargin)
+ sql(s"UPDATE $t AS tbl SET tbl.name='Robert', tbl.age=32 where p=2")
+
+ checkAnswer(spark.table(t),
+ Seq(Row(1, "Herry", 26, 1),
+ Row(2, "Robert", 32, 2),
+ Row(3, "Lisa", 28, 3),
+ Row(4, "Frank", 33, 3)))
+ }
+ }
+
+ test("Update: normalize attribute names") {
+ val t = "testcat.ns1.ns2.tbl"
+ withTable(t) {
+ sql(
+ s"""
+ |CREATE TABLE $t (id bigint, name string, age int, p int)
+ |USING foo
+ |PARTITIONED BY (id, p)
+ """.stripMargin)
+ sql(
+ s"""
+ |INSERT INTO $t
+ |VALUES (1L, 'Herry', 26, 1),
+ |(2L, 'Jack', 31, 2),
+ |(3L, 'Lisa', 28, 3),
+ |(4L, 'Frank', 33, 3)
+ """.stripMargin)
+ sql(s"UPDATE $t AS tbl SET tbl.NAME='Robert', tbl.AGE=32 where P=2")
+
+ checkAnswer(spark.table(t),
+ Seq(Row(1, "Herry", 26, 1),
+ Row(2, "Robert", 32, 2),
+ Row(3, "Lisa", 28, 3),
+ Row(4, "Frank", 33, 3)))
+ }
+ }
+
+ test("Update: columns aliases is not allowed") {
Review comment:
Moved. But I left the name `DDLParserSuite` unchanged since it does not
matter much with this pr.
----------------------------------------------------------------
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]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]