|
23 | 23 | import java.util.Map;
|
24 | 24 | import org.apache.iceberg.HasTableOperations;
|
25 | 25 | import org.apache.iceberg.Table;
|
| 26 | +import org.apache.iceberg.exceptions.AlreadyExistsException; |
26 | 27 | import org.apache.iceberg.hive.HiveTableOperations;
|
27 | 28 | import org.apache.iceberg.spark.Spark3Util;
|
28 | 29 | import org.apache.spark.sql.catalyst.analysis.NoSuchTableException;
|
@@ -86,4 +87,42 @@ public void testRegisterTable() throws NoSuchTableException, ParseException {
|
86 | 87 | Assert.assertEquals("Should have the right datafile count in the procedure result",
|
87 | 88 | originalFileCount, result.get(0)[2]);
|
88 | 89 | }
|
| 90 | + |
| 91 | + @Test |
| 92 | + public void testForceRegisterTable() throws NoSuchTableException, ParseException { |
| 93 | + Assume.assumeTrue("Register only implemented on Hive Catalogs", |
| 94 | + spark.conf().get("spark.sql.catalog." + catalogName + ".type").equals("hive")); |
| 95 | + |
| 96 | + long numRows1 = 100; |
| 97 | + long numRows2 = 200; |
| 98 | + sql("CREATE TABLE %s (id int, data string) using ICEBERG", tableName); |
| 99 | + |
| 100 | + spark.range(0, numRows1) |
| 101 | + .withColumn("data", functions.col("id").cast(DataTypes.StringType)) |
| 102 | + .writeTo(tableName) |
| 103 | + .append(); |
| 104 | + Table table = Spark3Util.loadIcebergTable(spark, tableName); |
| 105 | + String metaLocation = ((HiveTableOperations) (((HasTableOperations) table).operations())).currentMetadataLocation(); |
| 106 | + spark.range(0, numRows2) |
| 107 | + .withColumn("data", functions.col("id").cast(DataTypes.StringType)) |
| 108 | + .writeTo(tableName) |
| 109 | + .append(); |
| 110 | + table = Spark3Util.loadIcebergTable(spark, tableName); |
| 111 | + String newMetalocation = ((HiveTableOperations) (((HasTableOperations) table).operations())) |
| 112 | + .currentMetadataLocation(); |
| 113 | + |
| 114 | + |
| 115 | + sql("CALL %s.system.register_table('%s', '%s')", catalogName, targetName, metaLocation); |
| 116 | + List<Object[]> oldResults = sql("SELECT * FROM %s", targetName); |
| 117 | + sql("CALL %s.system.register_table('%s', '%s', 'force')", catalogName, targetName, newMetalocation); |
| 118 | + List<Object[]> newResults = sql("SELECT * FROM %s", targetName); |
| 119 | + |
| 120 | + Assert.assertEquals("Should have the right row count in the procedure result", |
| 121 | + numRows1, oldResults.size()); |
| 122 | + Assert.assertEquals("Should have the right row count in the procedure result", |
| 123 | + numRows1 + numRows2, newResults.size()); |
| 124 | + Assert.assertThrows("Can't repeat the register table without the force option.", |
| 125 | + AlreadyExistsException.class, |
| 126 | + () -> sql("CALL %s.system.register_table('%s', '%s')", catalogName, targetName, newMetalocation)); |
| 127 | + } |
89 | 128 | }
|
0 commit comments