aboutsummaryrefslogtreecommitdiff
path: root/sql/hive
diff options
context:
space:
mode:
authorgatorsmile <gatorsmile@gmail.com>2016-06-16 22:54:02 -0700
committerYin Huai <yhuai@databricks.com>2016-06-16 22:54:02 -0700
commite5d703bca85c65ce329b1e202283cfa35d109146 (patch)
treee16efee0c26449d5f03fb4dd34098aa2959d4e47 /sql/hive
parent5ada606144c7bf38a797764619d7d1ff677802b3 (diff)
downloadspark-e5d703bca85c65ce329b1e202283cfa35d109146.tar.gz
spark-e5d703bca85c65ce329b1e202283cfa35d109146.tar.bz2
spark-e5d703bca85c65ce329b1e202283cfa35d109146.zip
[SPARK-15706][SQL] Fix Wrong Answer when using IF NOT EXISTS in INSERT OVERWRITE for DYNAMIC PARTITION
#### What changes were proposed in this pull request? `IF NOT EXISTS` in `INSERT OVERWRITE` should not support dynamic partitions. If we specify `IF NOT EXISTS`, the inserted statement is not shown in the table. This PR is to issue an exception in this case, just like what Hive does. Also issue an exception if users specify `IF NOT EXISTS` if users do not specify any `PARTITION` specification. #### How was this patch tested? Added test cases into `PlanParserSuite` and `InsertIntoHiveTableSuite` Author: gatorsmile <gatorsmile@gmail.com> Closes #13447 from gatorsmile/insertIfNotExist.
Diffstat (limited to 'sql/hive')
-rw-r--r--sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertIntoHiveTableSuite.scala68
1 files changed, 68 insertions, 0 deletions
diff --git a/sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertIntoHiveTableSuite.scala b/sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertIntoHiveTableSuite.scala
index fae59001b9..3bf45ced75 100644
--- a/sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertIntoHiveTableSuite.scala
+++ b/sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertIntoHiveTableSuite.scala
@@ -166,6 +166,74 @@ class InsertIntoHiveTableSuite extends QueryTest with TestHiveSingleton with Bef
sql("DROP TABLE tmp_table")
}
+ test("INSERT OVERWRITE - partition IF NOT EXISTS") {
+ withTempDir { tmpDir =>
+ val table = "table_with_partition"
+ withTable(table) {
+ val selQuery = s"select c1, p1, p2 from $table"
+ sql(
+ s"""
+ |CREATE TABLE $table(c1 string)
+ |PARTITIONED by (p1 string,p2 string)
+ |location '${tmpDir.toURI.toString}'
+ """.stripMargin)
+ sql(
+ s"""
+ |INSERT OVERWRITE TABLE $table
+ |partition (p1='a',p2='b')
+ |SELECT 'blarr'
+ """.stripMargin)
+ checkAnswer(
+ sql(selQuery),
+ Row("blarr", "a", "b"))
+
+ sql(
+ s"""
+ |INSERT OVERWRITE TABLE $table
+ |partition (p1='a',p2='b')
+ |SELECT 'blarr2'
+ """.stripMargin)
+ checkAnswer(
+ sql(selQuery),
+ Row("blarr2", "a", "b"))
+
+ var e = intercept[AnalysisException] {
+ sql(
+ s"""
+ |INSERT OVERWRITE TABLE $table
+ |partition (p1='a',p2) IF NOT EXISTS
+ |SELECT 'blarr3', 'newPartition'
+ """.stripMargin)
+ }
+ assert(e.getMessage.contains(
+ "Dynamic partitions do not support IF NOT EXISTS. Specified partitions with value: [p2]"))
+
+ e = intercept[AnalysisException] {
+ sql(
+ s"""
+ |INSERT OVERWRITE TABLE $table
+ |partition (p1='a',p2) IF NOT EXISTS
+ |SELECT 'blarr3', 'b'
+ """.stripMargin)
+ }
+ assert(e.getMessage.contains(
+ "Dynamic partitions do not support IF NOT EXISTS. Specified partitions with value: [p2]"))
+
+ // If the partition already exists, the insert will overwrite the data
+ // unless users specify IF NOT EXISTS
+ sql(
+ s"""
+ |INSERT OVERWRITE TABLE $table
+ |partition (p1='a',p2='b') IF NOT EXISTS
+ |SELECT 'blarr3'
+ """.stripMargin)
+ checkAnswer(
+ sql(selQuery),
+ Row("blarr2", "a", "b"))
+ }
+ }
+ }
+
test("Insert ArrayType.containsNull == false") {
val schema = StructType(Seq(
StructField("a", ArrayType(StringType, containsNull = false))))