in xtable-api/src/main/java/org/apache/xtable/spi/sync/CatalogSync.java [87:126]
private <TABLE> CatalogSyncStatus syncCatalog(
CatalogSyncClient<TABLE> catalogSyncClient,
CatalogTableIdentifier tableIdentifier,
InternalTable table) {
log.info(
"Running catalog sync for table {} with base path {} and format {} using catalogSync {}",
tableIdentifier.getId(),
table.getBasePath(),
table.getTableFormat(),
catalogSyncClient.getClass().getName());
if (!catalogSyncClient.hasDatabase(tableIdentifier)) {
catalogSyncClient.createDatabase(tableIdentifier);
}
TABLE catalogTable = catalogSyncClient.getTable(tableIdentifier);
String storageDescriptorLocation = catalogSyncClient.getStorageLocation(catalogTable);
if (catalogTable == null) {
catalogSyncClient.createTable(table, tableIdentifier);
} else if (hasStorageDescriptorLocationChanged(
storageDescriptorLocation, table.getBasePath())) {
// Replace table if there is a mismatch between hmsTable location and Xtable basePath.
// Possible reasons could be:
// 1) hms table (manually) created with a different location before and need to be
// re-created with a new basePath
// 2) XTable basePath changes due to migration or other reasons
String oldLocation =
StringUtils.isEmpty(storageDescriptorLocation) ? "null" : storageDescriptorLocation;
log.warn(
"StorageDescriptor location changed from {} to {}, re-creating table",
oldLocation,
table.getBasePath());
catalogSyncClient.createOrReplaceTable(table, tableIdentifier);
} else {
log.debug("Table metadata changed, refreshing table");
catalogSyncClient.refreshTable(table, catalogTable, tableIdentifier);
}
return CatalogSyncStatus.builder()
.catalogId(catalogSyncClient.getCatalogId())
.statusCode(SyncStatusCode.SUCCESS)
.build();
}