Github user gatorsmile commented on a diff in the pull request:
https://github.com/apache/spark/pull/19747#discussion_r153396188
--- Diff:
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala
---
@@ -174,6 +174,88 @@ class HiveCatalogedDDLSuite extends DDLSuite with
TestHiveSingleton with BeforeA
test("alter datasource table add columns - partitioned - orc") {
testAddColumnPartitioned("orc")
}
+
+ test("SPARK-22431: illegal nested type") {
+ val queries = Seq(
+ "CREATE TABLE t AS SELECT STRUCT('a' AS `$a`, 1 AS b) q",
+ "CREATE TABLE t(q STRUCT<`$a`:INT, col2:STRING>, i1 INT)",
+ "CREATE VIEW t AS SELECT STRUCT('a' AS `$a`, 1 AS b) q")
+
+ queries.foreach(query => {
+ val err = intercept[SparkException] {
+ spark.sql(query)
+ }.getMessage
+ assert(err.contains("Cannot recognize hive type string"))
+ })
+
+ withView("v") {
+ spark.sql("CREATE VIEW v AS SELECT STRUCT('a' AS `a`, 1 AS b) q")
+ checkAnswer(sql("SELECT q.`a`, q.b FROM v"), Row("a", 1) :: Nil)
+
+ val err = intercept[SparkException] {
+ spark.sql("ALTER VIEW v AS SELECT STRUCT('a' AS `$a`, 1 AS b) q")
+ }.getMessage
+ assert(err.contains("Cannot recognize hive type string"))
+ }
+ }
+
+ test("SPARK-22431: table with nested type") {
+ withTable("t", "x") {
+ spark.sql("CREATE TABLE t(q STRUCT<`$a`:INT, col2:STRING>, i1 INT)
USING PARQUET")
+ checkAnswer(sql("SELECT * FROM t"), Nil)
+ spark.sql("CREATE TABLE x (q STRUCT<col1:INT, col2:STRING>, i1 INT)")
+ checkAnswer(sql("SELECT * FROM x"), Nil)
+ }
+ }
+
+ test("SPARK-22431: view with nested type") {
+ withView("v") {
+ spark.sql("CREATE VIEW v AS SELECT STRUCT('a' AS `a`, 1 AS b) q")
+ checkAnswer(spark.sql("SELECT * FROM v"), Row(Row("a", 1)) :: Nil)
+
+ spark.sql("ALTER VIEW v AS SELECT STRUCT('a' AS `b`, 1 AS b) q1")
+ val df = spark.sql("SELECT * FROM v")
+ assert("q1".equals(df.schema.fields(0).name))
+ checkAnswer(df, Row(Row("a", 1)) :: Nil)
+ }
+ }
+
+ test("SPARK-22431: alter table tests with nested types") {
+ withTable("t1", "t2", "t3") {
+ spark.sql("CREATE TABLE t1 (q STRUCT<col1:INT, col2:STRING>, i1
INT)")
+ spark.sql("ALTER TABLE t1 ADD COLUMNS (newcol1 STRUCT<`col1`:STRING,
col2:Int>)")
+ val newcol = spark.sql("SELECT * FROM t1").schema.fields(2).name
+ assert("newcol1".equals(newcol))
+
+ spark.sql("CREATE TABLE t2(q STRUCT<`a`:INT, col2:STRING>, i1 INT)
USING PARQUET")
+ spark.sql("ALTER TABLE t2 ADD COLUMNS (newcol1
STRUCT<`$col1`:STRING, col2:Int>)")
+ spark.sql("ALTER TABLE t2 ADD COLUMNS (newcol2 STRUCT<`col1`:STRING,
col2:Int>)")
+
+ val df2 = spark.sql("SELECT * FROM t2")
+ checkAnswer(df2, Nil)
--- End diff --
The same here
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]