Skip to content
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-19539][SQL] Block duplicate temp table during creation #16878

Closed
wants to merge 6 commits into from

Conversation

xwu0226
Copy link
Contributor

@xwu0226 xwu0226 commented Feb 10, 2017

What changes were proposed in this pull request?

Current CREATE TEMPORARY TABLE ... is deprecated and recommend users to use CREATE TEMPORARY VIEW ... And it does not support IF NOT EXISTS clause. However, if there is an existing temporary view defined, it is possible to unintentionally replace this existing view by issuing CREATE TEMPORARY TABLE ... with the same table/view name.

This PR is to disallow CREATE TEMPORARY TABLE ... with an existing view name.
Under the cover, CREATE TEMPORARY TABLE ... will be changed to create temporary view, however, passing in a flag replace=false, instead of currently true. So when creating temporary view under the cover, if there is existing view with the same name, the operation will be blocked.

How was this patch tested?

New unit test case is added and updated some existing test cases to adapt the new behavior

@xwu0226
Copy link
Contributor Author

xwu0226 commented Feb 10, 2017

@gatorsmile @cloud-fan Thanks!

@SparkQA
Copy link

SparkQA commented Feb 10, 2017

Test build #72670 has finished for PR 16878 at commit 133bd99.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -425,7 +425,9 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {

logWarning(s"CREATE TEMPORARY TABLE ... USING ... is deprecated, please use " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check when did we forbid this? maybe it's time to fully remove the support

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloud-fan Thanks for reviewing! I found that this deprecation was added by your PR #14482 back on 8/5/2016. Do you think it is the right time to remove CREATE TEMPORARY TABLE... completely?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we referring to the same PR? I found the change here https://github.com/apache/spark/pull/14482/files#diff-1bb4f7bd5a2656f48bcd3c857167a11bR362, where this WARN message is added in SparkSQLParser.scala

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean it was moved from other places, so this warning should have been added earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. sorry. I misunderstood. Let me check further.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#13414 deprecated this. Merged on Jun 7th, 2016.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was merged to 2.0, let's keep it

|CREATE TEMPORARY TABLE carsTable USING csv
|OPTIONS (path "${testFile(carsFile8859)}", header "true",
|charset "iso-8859-1", delimiter "þ")
""".stripMargin.replaceAll("\n", " "))
""".stripMargin.
replaceAll("\n", " "))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be put in the previous line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. thanks!


verifyCars(spark.table("carsTable"), numFields = 6, withHeader = true, checkHeader = false)
))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be put in the previous line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. Thanks!

s"""
withView("carsTable") {
spark.sql(
s"""
|CREATE TEMPORARY TABLE carsTable USING csv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you are fixing this test suite, can you replace all CREATE TEMPORARY TABLE to VIEW?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloud-fan there are test cases that are related to ALTER TABLE for the temp table, should we keep such test cases for a while?

@@ -425,7 +425,9 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {

logWarning(s"CREATE TEMPORARY TABLE ... USING ... is deprecated, please use " +
"CREATE TEMPORARY VIEW ... USING ... instead")
CreateTempViewUsing(table, schema, replace = true, global = false, provider, options)
// Since we don't support IF NOT EXISTS for temp table, we should not allow
// replacing existing temp table, that may accidentally remove a temp view in use.
Copy link
Member

@gatorsmile gatorsmile Feb 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just got your points... How about update the description to

Unlike CREATE TEMPORARY VIEW USING, CREATE TEMPORARY TABLE USING does not support IF NOT EXISTS. Users are not allowed to replace the existing temp table.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Good point. Thanks!

val e = intercept[TempTableAlreadyExistsException] {
sql("CREATE TEMPORARY TABLE t_temp (c3 int, c4 string) USING JSON")
}.getMessage
assert(e.contains("already exists"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please capture the whole error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

@SparkQA
Copy link

SparkQA commented Feb 13, 2017

Test build #72800 has finished for PR 16878 at commit f7253c5.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -971,7 +971,7 @@ class DDLSuite extends QueryTest with SharedSQLContext with BeforeAndAfterEach {
|)
""".stripMargin)
assert(catalog.listTables("default") == Seq(TableIdentifier("tab1")))
sql("DROP TABLE tab1")
sql("DROP view tab1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: upper case "view"

withView("carsTable") {
spark.sql(
s"""
|CREATE TEMPORARY VIEW carsTable USING csv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indention is wrong

withView("carsTable") {
spark.sql(
s"""
|CREATE TEMPORARY VIEW carsTable USING csv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indention is wrong

withView("carsTable") {
spark.sql(
s"""
|CREATE TEMPORARY VIEW carsTable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indention is wrong

withView("carsTable") {
spark.sql(
s"""
|CREATE TEMPORARY VIEW carsTable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indention is wrong

withView("carsTable") {
spark.sql(
s"""
|CREATE TEMPORARY VIEW carsTable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the indention is wrong

@cloud-fan
Copy link
Contributor

LGTM, pending jenkins, thanks for working on it!

@SparkQA
Copy link

SparkQA commented Feb 14, 2017

Test build #72834 has finished for PR 16878 at commit 6683d8c.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Feb 14, 2017

Test build #72837 has finished for PR 16878 at commit 3a989cf.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@gatorsmile
Copy link
Member

Thanks! Merging to master.

@asfgit asfgit closed this in 1ab9731 Feb 14, 2017
cmonkey pushed a commit to cmonkey/spark that referenced this pull request Feb 15, 2017
## What changes were proposed in this pull request?
Current `CREATE TEMPORARY TABLE ... ` is deprecated and recommend users to use `CREATE TEMPORARY VIEW ...` And it does not support `IF NOT EXISTS `clause. However, if there is an existing temporary view defined, it is possible to unintentionally replace this existing view by issuing `CREATE TEMPORARY TABLE ...`  with the same table/view name.

This PR is to disallow `CREATE TEMPORARY TABLE ...` with an existing view name.
Under the cover, `CREATE TEMPORARY TABLE ...` will be changed to create temporary view, however, passing in a flag `replace=false`, instead of currently `true`. So when creating temporary view under the cover, if there is existing view with the same name, the operation will be blocked.

## How was this patch tested?
New unit test case is added and updated some existing test cases to adapt the new behavior

Author: Xin Wu <xinwu@us.ibm.com>

Closes apache#16878 from xwu0226/block_duplicate_temp_table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants