|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.hudi |
19 | 19 |
|
| 20 | +import org.apache.hudi.HoodieSparkUtils |
20 | 21 | import org.apache.hudi.common.table.HoodieTableMetaClient |
21 | | - |
22 | 22 | import org.apache.spark.sql.catalyst.TableIdentifier |
23 | | -import org.apache.spark.sql.types.{LongType, StructField, StructType} |
24 | 23 |
|
25 | 24 | class TestAlterTable extends HoodieSparkSqlTestBase { |
26 | 25 |
|
@@ -169,4 +168,77 @@ class TestAlterTable extends HoodieSparkSqlTestBase { |
169 | 168 | } |
170 | 169 | } |
171 | 170 | } |
| 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 | + } |
172 | 244 | } |
0 commit comments