Skip to content

Commit 45af5a6

Browse files
committed
[HUDI-3861] update tblp 'path' when rename table
1 parent 52e63b3 commit 45af5a6

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/spark/sql/hudi/command/AlterHoodieTableRenameCommand.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
package org.apache.spark.sql.hudi.command
1919

2020
import org.apache.hudi.common.table.HoodieTableMetaClient
21-
2221
import org.apache.spark.sql.{Row, SparkSession}
2322
import org.apache.spark.sql.catalyst.TableIdentifier
2423
import org.apache.spark.sql.catalyst.catalog.HoodieCatalogTable
25-
import org.apache.spark.sql.execution.command.AlterTableRenameCommand
24+
import org.apache.spark.sql.execution.command.{AlterTableRenameCommand, AlterTableSetPropertiesCommand}
2625

2726
/**
2827
* Command for alter hudi table's table name.
@@ -46,6 +45,14 @@ case class AlterHoodieTableRenameCommand(
4645

4746
// Call AlterTableRenameCommand#run to rename table in meta.
4847
AlterTableRenameCommand(oldName, newName, isView).run(sparkSession)
48+
49+
// update table properties path in every op
50+
if (hoodieCatalogTable.table.properties.contains("path")) {
51+
val catalogTable = sparkSession.sessionState.catalog.getTableMetadata(newName)
52+
val path = catalogTable.storage.locationUri.get.getPath
53+
AlterTableSetPropertiesCommand(newName, Map("path" -> path), isView).run(sparkSession)
54+
}
55+
4956
}
5057
Seq.empty[Row]
5158
}

hudi-spark-datasource/hudi-spark/src/test/scala/org/apache/spark/sql/hudi/TestAlterTable.scala

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
package org.apache.spark.sql.hudi
1919

20+
import org.apache.hudi.HoodieSparkUtils
2021
import org.apache.hudi.common.table.HoodieTableMetaClient
21-
2222
import org.apache.spark.sql.catalyst.TableIdentifier
23-
import org.apache.spark.sql.types.{LongType, StructField, StructType}
2423

2524
class TestAlterTable extends HoodieSparkSqlTestBase {
2625

@@ -169,4 +168,77 @@ class TestAlterTable extends HoodieSparkSqlTestBase {
169168
}
170169
}
171170
}
171+
172+
test("Test Alter Rename Table") {
173+
withTempDir { tmp =>
174+
Seq("cow", "mor").foreach { tableType =>
175+
val tableName = generateTableName
176+
// Create table
177+
spark.sql(
178+
s"""
179+
|create table $tableName (
180+
| id int,
181+
| name string,
182+
| price double,
183+
| ts long
184+
|) using hudi
185+
| tblproperties (
186+
| type = '$tableType',
187+
| primaryKey = 'id',
188+
| preCombineField = 'ts'
189+
| )
190+
""".stripMargin)
191+
192+
// alter table name.
193+
val newTableName = s"${tableName}_1"
194+
val oldLocation = spark.sessionState.catalog.getTableMetadata(new TableIdentifier(tableName)).properties.get("path")
195+
spark.sql(s"alter table $tableName rename to $newTableName")
196+
val newLocation = spark.sessionState.catalog.getTableMetadata(new TableIdentifier(newTableName)).properties.get("path")
197+
if (HoodieSparkUtils.isSpark3_2) {
198+
assertResult(false)(
199+
newLocation.equals(oldLocation)
200+
)
201+
} else {
202+
assertResult(None) (oldLocation)
203+
assertResult(None) (newLocation)
204+
}
205+
206+
207+
// Create table with location
208+
val locTableName = s"${tableName}_loc"
209+
val tablePath = s"${tmp.getCanonicalPath}/$locTableName"
210+
spark.sql(
211+
s"""
212+
|create table $locTableName (
213+
| id int,
214+
| name string,
215+
| price double,
216+
| ts long
217+
|) using hudi
218+
| location '$tablePath'
219+
| tblproperties (
220+
| type = '$tableType',
221+
| primaryKey = 'id',
222+
| preCombineField = 'ts'
223+
| )
224+
""".stripMargin)
225+
226+
// alter table name.
227+
val newLocTableName = s"${locTableName}_1"
228+
val oldLocation2 = spark.sessionState.catalog.getTableMetadata(new TableIdentifier(locTableName))
229+
.properties.get("path")
230+
spark.sql(s"alter table $locTableName rename to $newLocTableName")
231+
val newLocation2 = spark.sessionState.catalog.getTableMetadata(new TableIdentifier(newLocTableName))
232+
.properties.get("path")
233+
if (HoodieSparkUtils.isSpark3_2) {
234+
assertResult(true)(
235+
newLocation2.equals(oldLocation2)
236+
)
237+
} else {
238+
assertResult(None) (oldLocation2)
239+
assertResult(None) (newLocation2)
240+
}
241+
}
242+
}
243+
}
172244
}

0 commit comments

Comments
 (0)