cloud-fan commented on code in PR #52016:
URL: https://github.com/apache/spark/pull/52016#discussion_r2416911786
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/UpdateTableSuiteBase.scala:
##########
@@ -735,4 +738,137 @@ abstract class UpdateTableSuiteBase extends
RowLevelOperationSuiteBase {
Row(5)))
}
}
+
+ test("update with UDT column") {
+ withTempView("v2") {
+ val data2 =
+ Seq(
+ (1, Point(21, 31)),
+ (2, Point(22, 32)),
+ (3, Point(23, 33))
+ ).toDF("pk", "pt")
+ data2.createTempView("v2")
+
+ withTable(tableNameAsString) {
+ // Creates a table with a Point column.
+ val data1 = Seq(
+ (1, Point(11, 21)),
+ (2, Point(12, 22)),
+ (3, Point(13, 23))
+ ).toDF("pk", "pt")
+ data1.write.saveAsTable(tableNameAsString)
+
+ checkAnswer(spark.table(tableNameAsString), data1)
+
+ sql(
+ s""" merge into $tableNameAsString as t
+ | using v2
+ | on t.pk = v2.pk
+ | when matched and t.pk <> 2 then update set pt = v2.pt
+ |""".stripMargin)
+ checkAnswer(spark.table(tableNameAsString),
+ Seq(
+ (1, Point(21, 31)),
+ (2, Point(12, 22)),
+ (3, Point(23, 33))
+ ).toDF("pk", "pt"))
+
+ sql(
+ s""" UPDATE $tableNameAsString
+ | SET pt = (select pt from v2 where pk = 2)
+ | WHERE pk = 2
+ |""".stripMargin)
+ checkAnswer(spark.table(tableNameAsString), data2)
+ }
+ }
+ }
+
+ test("unwrap UDT to struct before update") {
+ withTempView("v1", "v2") {
+ val data1 = Seq(
+ (1, Point(11, 21)),
+ (2, Point(12, 22)),
+ (3, Point(13, 23))
+ ).toDF("pk", "pt")
+ data1.createTempView("v1")
+
+ val data2 =
+ Seq(
+ (1, Point(21, 31)),
+ (2, Point(22, 32)),
+ (3, Point(23, 33))
+ ).toDF("pk", "pt")
+ data2.createTempView("v2")
+
+ withTable(tableNameAsString) {
+ sql(
+ s"""
+ | create table $tableNameAsString (
+ | pk long not null,
+ | pt struct<x: double not null, y: double not null>)
+ |""".stripMargin)
+
+ sql(s"INSERT INTO $tableNameAsString SELECT * FROM v1")
+ checkAnswer(spark.table(tableNameAsString),
+ Seq(
+ (1, (11.0, 21.0)),
+ (2, (12.0, 22.0)),
+ (3, (13.0, 23.0))
+ ).toDF("pk", "pt"))
+
+ sql(
+ s""" merge into $tableNameAsString as t
+ | using v2
+ | on t.pk = v2.pk
+ | when matched and t.pk <> 2 then update set pt = v2.pt
+ |""".stripMargin)
+ checkAnswer(spark.table(tableNameAsString),
+ Seq(
+ (1, (21, 31)),
+ (2, (12, 22)),
+ (3, (23, 33))
+ ).toDF("pk", "pt"))
+
+ sql(
+ s""" UPDATE $tableNameAsString
+ | SET pt = (select pt from v2 where pk = 2)
+ | WHERE pk = 2
+ |""".stripMargin)
+ checkAnswer(spark.table(tableNameAsString),
+ Seq(
+ (1, (21, 31)),
+ (2, (22, 32)),
+ (3, (23, 33))
+ ).toDF("pk", "pt"))
+ }
+ }
+ }
+}
+
+
+object UpdateTableSuiteBase {
+ @SQLUserDefinedType(udt = classOf[PointUDT])
+ case class Point(x: Double, y: Double)
+
+ class PointUDT extends UserDefinedType[Point] {
Review Comment:
shall we use the shared testing UDT `ExamplePointUDT`?
--
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]