Skip to content

Commit 96f5f8e

Browse files
committed
review comments round 2
1 parent 2a26a04 commit 96f5f8e

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,24 @@ private String newTableMetadataFilePath(TableMetadata meta, int newVersion) {
331331
return metadataFileLocation(meta, String.format("%05d-%s%s", newVersion, UUID.randomUUID(), fileExtension));
332332
}
333333

334+
/**
335+
* Parse the version from table metadata file name.
336+
*
337+
* @param metadataLocation table metadata file location
338+
* @return version of the table metadata file in success case and
339+
* -1 if the version is not parsable (as a sign that the metadata is not part of this catalog)
340+
*/
334341
private static int parseVersion(String metadataLocation) {
335342
int versionStart = metadataLocation.lastIndexOf('/') + 1; // if '/' isn't found, this will be 0
336343
int versionEnd = metadataLocation.indexOf('-', versionStart);
344+
if (versionEnd < 0) {
345+
LOG.warn("Found filesystem table's metadata : {}", metadataLocation);
346+
return -1;
347+
}
348+
337349
try {
338350
return Integer.valueOf(metadataLocation.substring(versionStart, versionEnd));
339-
} catch (NumberFormatException | StringIndexOutOfBoundsException e) {
340-
// As per the spec, metastore tables and filesystem tables
341-
// will have different representation for metadata file names.
342-
// So, StringIndexOutOfBoundsException can happen when parsing a filesystem based metadata file name.
351+
} catch (NumberFormatException e) {
343352
LOG.warn("Unable to parse version from metadata location: {}", metadataLocation, e);
344353
return -1;
345354
}

hive-metastore/src/test/java/org/apache/iceberg/hive/HiveTableTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Set;
2829
import java.util.stream.Collectors;
2930
import org.apache.avro.generic.GenericData;
3031
import org.apache.avro.generic.GenericRecordBuilder;
@@ -37,6 +38,7 @@
3738
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
3839
import org.apache.hadoop.hive.serde.serdeConstants;
3940
import org.apache.iceberg.AssertHelpers;
41+
import org.apache.iceberg.BaseTable;
4042
import org.apache.iceberg.DataFile;
4143
import org.apache.iceberg.DataFiles;
4244
import org.apache.iceberg.FileScanTask;
@@ -398,9 +400,10 @@ public void testRegisterHadoopTableToHiveCatalog() throws IOException, TExceptio
398400
TableIdentifier identifier = TableIdentifier.of(DB_NAME, "table1");
399401
Table table = hadoopCatalog.createTable(identifier, schema, PartitionSpec.unpartitioned(), Maps.newHashMap());
400402
// insert some data
401-
appendData(table, "file1");
403+
String file1Location = appendData(table, "file1");
402404
List<FileScanTask> tasks = Lists.newArrayList(table.newScan().planFiles());
403405
Assert.assertEquals("Should scan 1 file", 1, tasks.size());
406+
Assert.assertEquals(tasks.get(0).file().path(), file1Location);
404407

405408
// collect metadata file
406409
List<String> metadataFiles =
@@ -420,18 +423,20 @@ public void testRegisterHadoopTableToHiveCatalog() throws IOException, TExceptio
420423
() -> catalog.loadTable(identifier));
421424

422425
// register the table to hive catalog using the latest metadata file
423-
metadataFiles.sort(Collections.reverseOrder());
424-
catalog.registerTable(identifier, "file:" + metadataFiles.get(0));
426+
String latestMetadataFile = ((BaseTable) table).operations().current().metadataFileLocation();
427+
catalog.registerTable(identifier, "file:" + latestMetadataFile);
425428
Assert.assertNotNull(metastoreClient.getTable(DB_NAME, "table1"));
426429

427430
// load the table in hive catalog
428431
table = catalog.loadTable(identifier);
429432
Assert.assertNotNull(table);
430433

431434
// insert some data
432-
appendData(table, "file2");
435+
String file2Location = appendData(table, "file2");
433436
tasks = Lists.newArrayList(table.newScan().planFiles());
434437
Assert.assertEquals("Should scan 2 files", 2, tasks.size());
438+
Set<String> files = tasks.stream().map(task -> task.file().path().toString()).collect(Collectors.toSet());
439+
Assert.assertTrue(files.contains(file1Location) && files.contains(file2Location));
435440
}
436441

437442
private String appendData(Table table, String fileName) throws IOException {

0 commit comments

Comments
 (0)