Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions core/src/main/java/org/apache/iceberg/SnapshotProducer.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,15 @@ private Map<String, String> summary(TableMetadata previous) {
}
} else {
// if there was no previous snapshot, default the summary to start totals at 0
previousSummary = ImmutableMap.of(
SnapshotSummary.TOTAL_RECORDS_PROP, "0",
SnapshotSummary.TOTAL_DATA_FILES_PROP, "0",
SnapshotSummary.TOTAL_DELETE_FILES_PROP, "0",
SnapshotSummary.TOTAL_POS_DELETES_PROP, "0",
SnapshotSummary.TOTAL_EQ_DELETES_PROP, "0");
ImmutableMap.Builder<String, String> summaryBuilder = ImmutableMap.builder();
summaryBuilder
.put(SnapshotSummary.TOTAL_RECORDS_PROP, "0")
.put(SnapshotSummary.TOTAL_FILE_SIZE_PROP, "0")
.put(SnapshotSummary.TOTAL_DATA_FILES_PROP, "0")
.put(SnapshotSummary.TOTAL_DELETE_FILES_PROP, "0")
.put(SnapshotSummary.TOTAL_POS_DELETES_PROP, "0")
.put(SnapshotSummary.TOTAL_EQ_DELETES_PROP, "0");
previousSummary = summaryBuilder.build();
}

ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
Expand All @@ -234,6 +237,9 @@ private Map<String, String> summary(TableMetadata previous) {
updateTotal(
builder, previousSummary, SnapshotSummary.TOTAL_RECORDS_PROP,
summary, SnapshotSummary.ADDED_RECORDS_PROP, SnapshotSummary.DELETED_RECORDS_PROP);
updateTotal(
builder, previousSummary, SnapshotSummary.TOTAL_FILE_SIZE_PROP,
summary, SnapshotSummary.ADDED_FILE_SIZE_PROP, SnapshotSummary.REMOVED_FILE_SIZE_PROP);
updateTotal(
builder, previousSummary, SnapshotSummary.TOTAL_DATA_FILES_PROP,
summary, SnapshotSummary.ADDED_FILES_PROP, SnapshotSummary.DELETED_FILES_PROP);
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/apache/iceberg/SnapshotSummary.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class SnapshotSummary {
public static final String TOTAL_RECORDS_PROP = "total-records";
public static final String ADDED_FILE_SIZE_PROP = "added-files-size";
public static final String REMOVED_FILE_SIZE_PROP = "removed-files-size";
public static final String TOTAL_FILE_SIZE_PROP = "total-files-size";
public static final String ADDED_POS_DELETES_PROP = "added-position-deletes";
public static final String REMOVED_POS_DELETES_PROP = "removed-position-deletes";
public static final String TOTAL_POS_DELETES_PROP = "total-position-deletes";
Expand Down
82 changes: 82 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestSnapshotSummary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iceberg;

import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TestSnapshotSummary extends TableTestBase {
public TestSnapshotSummary(int formatVersion) {
super(formatVersion);
}

@Parameterized.Parameters(name = "formatVersion = {0}")
public static Object[] parameters() {
return new Object[] { 1, 2 };
}

@Test
public void testFileSizeSummary() {
Assert.assertEquals("Table should start empty", 0, listManifestFiles().size());

// fast append
table.newFastAppend()
.appendFile(FILE_A)
.commit();
Map<String, String> summary = table.currentSnapshot().summary();
Assert.assertEquals("10", summary.get(SnapshotSummary.ADDED_FILE_SIZE_PROP));
Assert.assertNull(summary.get(SnapshotSummary.REMOVED_FILE_SIZE_PROP));
Assert.assertEquals("10", summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP));

// merge append
table.newAppend()
.appendFile(FILE_B)
.commit();
summary = table.currentSnapshot().summary();
Assert.assertEquals("10", summary.get(SnapshotSummary.ADDED_FILE_SIZE_PROP));
Assert.assertNull(summary.get(SnapshotSummary.REMOVED_FILE_SIZE_PROP));
Assert.assertEquals("20", summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP));

table.newOverwrite()
.deleteFile(FILE_A)
.deleteFile(FILE_B)
.addFile(FILE_C)
.addFile(FILE_D)
.addFile(FILE_D)
.commit();
summary = table.currentSnapshot().summary();
Assert.assertEquals("30", summary.get(SnapshotSummary.ADDED_FILE_SIZE_PROP));
Assert.assertEquals("20", summary.get(SnapshotSummary.REMOVED_FILE_SIZE_PROP));
Assert.assertEquals("30", summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP));

table.newDelete()
.deleteFile(FILE_C)
.deleteFile(FILE_D)
.commit();
summary = table.currentSnapshot().summary();
Assert.assertNull(summary.get(SnapshotSummary.ADDED_FILE_SIZE_PROP));
Assert.assertEquals("20", summary.get(SnapshotSummary.REMOVED_FILE_SIZE_PROP));
Assert.assertEquals("10", summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants;
import org.apache.iceberg.BaseMetastoreTableOperations;
import org.apache.iceberg.Snapshot;
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TableProperties;
import org.apache.iceberg.common.DynMethods;
Expand Down Expand Up @@ -196,7 +198,10 @@ protected void doCommit(TableMetadata base, TableMetadata metadata) {
.collect(Collectors.toSet());
}

setHmsTableParameters(newMetadataLocation, tbl, metadata.properties(), removedProps, hiveEngineEnabled);
Map<String, String> summary = Optional.ofNullable(metadata.currentSnapshot())
.map(Snapshot::summary)
.orElseGet(ImmutableMap::of);
setHmsTableParameters(newMetadataLocation, tbl, metadata.properties(), removedProps, hiveEngineEnabled, summary);

persistTable(tbl, updateHiveTable);
threw = false;
Expand Down Expand Up @@ -268,7 +273,8 @@ private Table newHmsTable() {
}

private void setHmsTableParameters(String newMetadataLocation, Table tbl, Map<String, String> icebergTableProps,
Set<String> obsoleteProps, boolean hiveEngineEnabled) {
Set<String> obsoleteProps, boolean hiveEngineEnabled,
Map<String, String> summary) {
Map<String, String> parameters = tbl.getParameters();

if (parameters == null) {
Expand Down Expand Up @@ -296,6 +302,13 @@ private void setHmsTableParameters(String newMetadataLocation, Table tbl, Map<St
parameters.remove(hive_metastoreConstants.META_TABLE_STORAGE);
}

// Set the basic statistics
parameters.put(StatsSetupConst.NUM_FILES, summary.getOrDefault(SnapshotSummary.TOTAL_DATA_FILES_PROP, "0"));
parameters.put(StatsSetupConst.ROW_COUNT, summary.getOrDefault(SnapshotSummary.TOTAL_RECORDS_PROP, "0"));
parameters.put(StatsSetupConst.TOTAL_SIZE, summary.getOrDefault(SnapshotSummary.TOTAL_FILE_SIZE_PROP, "0"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the summary property is missing, should we set the Hive property? I think this is correct only if "0" indicates to Hive that the value is not known.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question.
Based on this Hive considers 0 as a non valid statistics value anyway:

  public BasicStats(Partish p) {
    partish = p;

    rowCount = parseLong(StatsSetupConst.ROW_COUNT);
[..]
    currentNumRows = rowCount;
[..]
    if (currentNumRows > 0) {
      state = State.COMPLETE;
    } else {
      state = State.NONE;
    }
  }

But I agree that unsetting the value would be more intuitive. What is the content of the SnapshotSummary in case of a new table? Setting 0 there would still be good (even if Hive does not consider it as a valid one)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When creating a table, metadata.currentSnapshot() is null, therefore we won't have a summary and will create an empty map for it. I think @rdblue is right that we should only update the HMS values if they're actually present in the summary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove them if they are not present for whatever reasons?

What happens if a commit is a metadata only change? Do we still have the summary?

Thanks,
Peter

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked with a PropertiesUpdate, and it does not produce a new snapshot, so metadata.currentSnapshot() will be the same as before along with its summary object.

// we don't have the uncompressed file sizes, so we use the totalSize (size on disk) as an estimate for rawDataSize
parameters.put(StatsSetupConst.RAW_DATA_SIZE, summary.getOrDefault(SnapshotSummary.TOTAL_FILE_SIZE_PROP, "0"));
Comment thread
marton-bod marked this conversation as resolved.
Outdated

tbl.setParameters(parameters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import org.apache.iceberg.PartitionSpecParser;
import org.apache.iceberg.Schema;
import org.apache.iceberg.SchemaParser;
import org.apache.iceberg.SnapshotSummary;
import org.apache.iceberg.TableProperties;
import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.data.Record;
import org.apache.iceberg.exceptions.NoSuchTableException;
import org.apache.iceberg.hadoop.Util;
import org.apache.iceberg.hive.HiveSchemaUtil;
Expand Down Expand Up @@ -106,9 +108,7 @@ public class TestHiveIcebergStorageHandlerNoScan {
))
);

private static final Set<String> IGNORED_PARAMS =
ImmutableSet.of("bucketing_version", StatsSetupConst.ROW_COUNT,
StatsSetupConst.RAW_DATA_SIZE, StatsSetupConst.TOTAL_SIZE, StatsSetupConst.NUM_FILES, "numFilesErasureCoded");
private static final Set<String> IGNORED_PARAMS = ImmutableSet.of("bucketing_version", "numFilesErasureCoded");

@Parameters(name = "catalog={0}")
public static Collection<Object[]> parameters() {
Expand Down Expand Up @@ -485,7 +485,7 @@ public void testCreateTableWithoutColumnComments() {
}

@Test
public void testIcebergAndHmsTableProperties() throws TException, InterruptedException {
public void testIcebergAndHmsTableProperties() throws Exception {
TableIdentifier identifier = TableIdentifier.of("default", "customers");

shell.executeStatement(String.format("CREATE EXTERNAL TABLE default.customers " +
Expand Down Expand Up @@ -520,11 +520,15 @@ public void testIcebergAndHmsTableProperties() throws TException, InterruptedExc
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

if (Catalogs.hiveCatalog(shell.getHiveConf())) {
Assert.assertEquals(9, hmsParams.size());
Assert.assertEquals(13, hmsParams.size());
Assert.assertEquals("initial_val", hmsParams.get("custom_property"));
Assert.assertEquals("TRUE", hmsParams.get(InputFormatConfig.EXTERNAL_TABLE_PURGE));
Assert.assertEquals("TRUE", hmsParams.get("EXTERNAL"));
Assert.assertEquals("true", hmsParams.get(TableProperties.ENGINE_HIVE_ENABLED));
Assert.assertEquals("0", hmsParams.get(StatsSetupConst.NUM_FILES));
Assert.assertEquals("0", hmsParams.get(StatsSetupConst.ROW_COUNT));
Assert.assertEquals("0", hmsParams.get(StatsSetupConst.TOTAL_SIZE));
Assert.assertEquals("0", hmsParams.get(StatsSetupConst.RAW_DATA_SIZE));
Assert.assertEquals(HiveIcebergStorageHandler.class.getName(),
hmsParams.get(hive_metastoreConstants.META_TABLE_STORAGE));
Assert.assertEquals(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE.toUpperCase(),
Expand Down Expand Up @@ -557,7 +561,7 @@ public void testIcebergAndHmsTableProperties() throws TException, InterruptedExc
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

if (Catalogs.hiveCatalog(shell.getHiveConf())) {
Assert.assertEquals(12, hmsParams.size()); // 2 newly-added properties + previous_metadata_location prop
Assert.assertEquals(16, hmsParams.size()); // 2 newly-added properties + previous_metadata_location prop
Assert.assertEquals("true", hmsParams.get("new_prop_1"));
Assert.assertEquals("false", hmsParams.get("new_prop_2"));
Assert.assertEquals("new_val", hmsParams.get("custom_property"));
Expand All @@ -576,14 +580,24 @@ public void testIcebergAndHmsTableProperties() throws TException, InterruptedExc
.remove("custom_property")
.remove("new_prop_1")
.commit();
hmsParams = shell.metastore().getTable("default", "customers").getParameters()
.entrySet().stream()
.filter(e -> !IGNORED_PARAMS.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
hmsParams = shell.metastore().getTable("default", "customers").getParameters();
Assert.assertFalse(hmsParams.containsKey("custom_property"));
Assert.assertFalse(hmsParams.containsKey("new_prop_1"));
Assert.assertTrue(hmsParams.containsKey("new_prop_2"));
}

// append some data and check whether HMS stats are aligned with snapshot summary
if (Catalogs.hiveCatalog(shell.getHiveConf())) {
List<Record> records = HiveIcebergStorageHandlerTestUtils.CUSTOMER_RECORDS;
testTables.appendIcebergTable(shell.getHiveConf(), icebergTable, FileFormat.PARQUET, null, records);
hmsParams = shell.metastore().getTable("default", "customers").getParameters();
Map<String, String> summary = icebergTable.currentSnapshot().summary();
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_DATA_FILES_PROP), hmsParams.get(StatsSetupConst.NUM_FILES));
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_RECORDS_PROP), hmsParams.get(StatsSetupConst.ROW_COUNT));
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP), hmsParams.get(StatsSetupConst.TOTAL_SIZE));
Assert.assertEquals(summary.get(SnapshotSummary.TOTAL_FILE_SIZE_PROP),
hmsParams.get(StatsSetupConst.RAW_DATA_SIZE));
}
}

@Test
Expand Down