diff --git a/hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java b/hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java index 494bd8089489..aee2fb04f164 100644 --- a/hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java +++ b/hive-metastore/src/main/java/org/apache/iceberg/hive/HMSTablePropertyHelper.java @@ -110,6 +110,7 @@ public static void updateHmsTableForIcebergTable( maxHiveTablePropertySize); setStorageHandler(parameters, hiveEngineEnabled); setMetadataHash(metadata, parameters); + parameters.put(TableProperties.FORMAT_VERSION, String.valueOf(metadata.formatVersion())); // Set the basic statistics if (summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP) != null) { diff --git a/hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java b/hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java index 58a45e68db89..36f6a35098e7 100644 --- a/hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java +++ b/hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCatalog.java @@ -94,6 +94,7 @@ import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.FieldSource; import org.junit.jupiter.params.provider.ValueSource; /** @@ -994,6 +995,54 @@ public void testUUIDinTableProperties() throws Exception { } } + @ParameterizedTest + @FieldSource("org.apache.iceberg.TestHelpers#ALL_VERSIONS") + void formatVersionInTableParametersOnCreate(int formatVersion) throws Exception { + Schema schema = getTestSchema(); + TableIdentifier tableIdentifier = TableIdentifier.of(DB_NAME, "tbl"); + String location = temp.resolve("tbl").toUri().toString(); + + try { + catalog + .buildTable(tableIdentifier, schema) + .withLocation(location) + .withProperty(TableProperties.FORMAT_VERSION, String.valueOf(formatVersion)) + .create(); + + assertThat(hmsTableParameters()) + .containsEntry(TableProperties.FORMAT_VERSION, String.valueOf(formatVersion)); + } finally { + catalog.dropTable(tableIdentifier); + } + } + + @Test + void formatVersionInTableParametersOnUpgrade() throws Exception { + Schema schema = getTestSchema(); + TableIdentifier tableIdentifier = TableIdentifier.of(DB_NAME, "tbl"); + String location = temp.resolve("tbl").toString(); + int upgradedFormatVersion = 3; + + try { + catalog + .buildTable(tableIdentifier, schema) + .withLocation(location) + .withProperty(TableProperties.FORMAT_VERSION, "2") + .create(); + + catalog + .loadTable(tableIdentifier) + .updateProperties() + .set(TableProperties.FORMAT_VERSION, String.valueOf(upgradedFormatVersion)) + .commit(); + + assertThat(hmsTableParameters()) + .containsEntry(TableProperties.FORMAT_VERSION, String.valueOf(upgradedFormatVersion)); + } finally { + catalog.dropTable(tableIdentifier); + } + } + @Test public void testSnapshotStatsTableProperties() throws Exception { Schema schema = getTestSchema();