-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-20831] [SQL] Fix INSERT OVERWRITE data source tables with IF NOT EXISTS #18050
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,72 +166,54 @@ 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")) | ||
testPartitionedTable("INSERT OVERWRITE - partition IF NOT EXISTS") { tableName => | ||
val selQuery = s"select a, b, c, d from $tableName" | ||
sql( | ||
s""" | ||
|INSERT OVERWRITE TABLE $tableName | ||
|partition (b=2, c=3) | ||
|SELECT 1, 4 | ||
""".stripMargin) | ||
checkAnswer(sql(selQuery), Row(1, 2, 3, 4)) | ||
|
||
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]")) | ||
sql( | ||
s""" | ||
|INSERT OVERWRITE TABLE $tableName | ||
|partition (b=2, c=3) | ||
|SELECT 5, 6 | ||
""".stripMargin) | ||
checkAnswer(sql(selQuery), Row(5, 2, 3, 6)) | ||
|
||
val e = intercept[AnalysisException] { | ||
sql( | ||
s""" | ||
|INSERT OVERWRITE TABLE $tableName | ||
|partition (b=2, c) IF NOT EXISTS | ||
|SELECT 7, 8, 3 | ||
""".stripMargin) | ||
} | ||
assert(e.getMessage.contains( | ||
"Dynamic partitions do not support IF NOT EXISTS. Specified partitions with value: [c]")) | ||
|
||
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 $tableName | ||
|partition (b=2, c=3) IF NOT EXISTS | ||
|SELECT 9, 10 | ||
""".stripMargin) | ||
checkAnswer(sql(selQuery), Row(5, 2, 3, 6)) | ||
|
||
// If the partition already exists, the insert will overwrite the data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh. This is for Hive table. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So seems we should also add a test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is why I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Looks good. |
||
// 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")) | ||
} | ||
} | ||
// ADD PARTITION has the same effect, even if no actual data is inserted. | ||
sql(s"ALTER TABLE $tableName ADD PARTITION (b=21, c=31)") | ||
sql( | ||
s""" | ||
|INSERT OVERWRITE TABLE $tableName | ||
|partition (b=21, c=31) IF NOT EXISTS | ||
|SELECT 20, 24 | ||
""".stripMargin) | ||
checkAnswer(sql(selQuery), Row(5, 2, 3, 6)) | ||
} | ||
|
||
test("Insert ArrayType.containsNull == false") { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a little weird to test data source table insertion in InsertIntoHiveTableSuite...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anyway, we can fix it later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create and move all these tests to a new suite
InsertIntoTableSuite
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are still under hive/?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is not too verbose, I'd like to have them separated, instead of mixing together in one test suite under hive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can move the test cases to a new one in /core and let
InsertIntoHiveTableSuite
extends that one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can do it as a test-only PR.