AngersZhuuuu commented on a change in pull request #30421:
URL: https://github.com/apache/spark/pull/30421#discussion_r583528725
##########
File path: sql/core/src/test/scala/org/apache/spark/sql/SQLInsertTestSuite.scala
##########
@@ -208,6 +212,53 @@ trait SQLInsertTestSuite extends QueryTest with
SQLTestUtils {
checkAnswer(spark.table("t"), Row("1", null))
}
}
+
+ test("SPARK-33474: Support typed literals as partition spec values") {
+ withTable("t1", "t2", "t4", "t5") {
+ val binaryStr = "Spark SQL"
+ val binaryHexStr =
Hex.hex(UTF8String.fromString(binaryStr).getBytes).toString
+ sql("CREATE TABLE t1(name STRING, part DATE) USING PARQUET PARTITIONED
BY (part)")
+ sql("INSERT INTO t1 PARTITION(part = date'2019-01-02') VALUES('a')")
+ checkAnswer(sql("SELECT name, CAST(part AS STRING) FROM t1"), Row("a",
"2019-01-02"))
+
+ sql("CREATE TABLE t2(name STRING, part TIMESTAMP) USING PARQUET
PARTITIONED BY (part)")
+ sql("INSERT INTO t2 PARTITION(part = timestamp'2019-01-02 11:11:11')
VALUES('a')")
+ checkAnswer(sql("SELECT name, CAST(part AS STRING) FROM t2"), Row("a",
"2019-01-02 11:11:11"))
+
+ val e = intercept[AnalysisException] {
+ sql("CREATE TABLE t3(name STRING, part INTERVAL) USING PARQUET
PARTITIONED BY (part)")
+ }.getMessage
+ assert(e.contains("Cannot use interval for partition column"))
+
+ sql("CREATE TABLE t4(name STRING, part BINARY) USING CSV PARTITIONED BY
(part)")
+ sql(s"INSERT INTO t4 PARTITION(part = X'$binaryHexStr') VALUES('a')")
+ checkAnswer(sql("SELECT name, cast(part as string) FROM t4"), Row("a",
binaryStr))
+
+ // test type conversion
+ sql("CREATE TABLE t5(name STRING, part STRING) USING CSV PARTITIONED BY
(part)")
+ sql(s"INSERT INTO t5 PARTITION(part = X'$binaryHexStr') VALUES('a')")
+ sql("INSERT INTO t5 PARTITION(part = date'2019-01-02') VALUES('a')")
+ sql("INSERT INTO t5 PARTITION(part = timestamp'2019-01-02 11:11:11')
VALUES('a')")
+ checkAnswer(sql("SELECT name, cast(part as string) FROM t5"),
+ Row("a", binaryStr) :: Row("a", "2019-01-02") ::
+ Row("a", "2019-01-02 11:11:11") :: Nil)
+
+ // test insert timestamp literal partition value to date partition
+ sql("INSERT INTO t1 PARTITION(part = timestamp'2019-01-02 11:11:11')
VALUES('b')")
+ checkAnswer(sql("SELECT name, cast(part as string) FROM t1"),
+ Row("a", "2019-01-02") :: Row("b", "2019-01-02") :: Nil)
+ // test insert invalid binary string partition value to date partition
+ val e2 = intercept[DateTimeException] {
+ sql(s"INSERT INTO t2 PARTITION(part = X'$binaryHexStr') VALUES('a')")
+ }.getMessage
+ assert(e2 == s"Cannot cast $binaryStr to TimestampType.")
+
+ // test change work for ALTER TABLE ... ADD PARTITION(part =
date'2020-01-01')
Review comment:
> We have dedicated test suits for ADD/DROP/RENAME PARTITION, can we add
tests there?
yea, updated
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]