Skip to content

Conversation

@gengliangwang
Copy link
Member

What changes were proposed in this pull request?

If table is renamed to a existing new location, data won't show up.

scala>  Seq("hello").toDF("a").write.format("parquet").saveAsTable("t")
                                                                                
scala> sql("select * from t").show()
+-----+
|    a|
+-----+
|hello|
+-----+


scala> sql("alter table t rename to test")
res2: org.apache.spark.sql.DataFrame = []

scala> sql("select * from test").show()
+---+
|  a|
+---+
+---+

The file layout is like

$ tree test
test
├── gabage
└── t
    ├── _SUCCESS
    └── part-00000-856b0f10-08f1-42d6-9eb3-7719261f3d5e-c000.snappy.parquet

In Hive, if the new location exists, the renaming will fail even the location is empty.

We should have the same validation in Catalog, in case of unexpected bugs.

How was this patch tested?

New unit test.

tempViews.foreach(kv => target.tempViews.put(kv._1, kv._2))
}

private def validateLocationOfRename(oldName: TableIdentifier, newName: TableIdentifier): Unit = {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add code comments along with validateName?

}

test ("rename a managed table with existing empty directory") {
val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab2")))
Copy link
Member

Choose a reason for hiding this comment

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

nit: remove the space between test and (.

val newTableLocation = new Path(new Path(databaseLocation), formatTableName(newName.table))
val fs = newTableLocation.getFileSystem(hadoopConf)
if (fs.exists(newTableLocation)) {
throw new AnalysisException(s"Can not rename the managed table('${oldName}')" +
Copy link
Member

Choose a reason for hiding this comment

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

nit: remove braces: ${oldName} -> $oldName

val fs = newTableLocation.getFileSystem(hadoopConf)
if (fs.exists(newTableLocation)) {
throw new AnalysisException(s"Can not rename the managed table('${oldName}')" +
s". The associated location('${newTableLocation.toString}') 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.

nit: ${newTableLocation.toString} -> $newTableLocation?

val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab2")))
try {
withTable("tab1") {
sql(s"CREATE TABLE tab1 USING ${dataSource} AS SELECT 1, 'a'")
Copy link
Member

Choose a reason for hiding this comment

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

nit: ${dataSource} -> $dataSource

@SparkQA
Copy link

SparkQA commented Jun 28, 2018

Test build #92417 has finished for PR 21655 at commit 1622d9c.

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

@SparkQA
Copy link

SparkQA commented Jun 28, 2018

Test build #92429 has finished for PR 21655 at commit 7265f66.

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

requireTableExists(TableIdentifier(oldTableName, Some(db)))
requireTableNotExists(TableIdentifier(newTableName, Some(db)))
validateName(newTableName)
validateNewLocationOfRename(oldName, newName)
Copy link
Member

Choose a reason for hiding this comment

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

Add this new behavior to migration guide?

@SparkQA
Copy link

SparkQA commented Jul 4, 2018

Test build #92591 has finished for PR 21655 at commit 18418c9.

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

@gatorsmile
Copy link
Member

LGTM

Thanks! Merged to master.

@asfgit asfgit closed this in 33952cf Jul 5, 2018
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