Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*/
package io.trino.plugin.hive;

import com.google.common.collect.ImmutableList;
import com.google.common.net.HostAndPort;
import io.trino.spi.connector.SchemaTableName;
import org.apache.hadoop.net.NetUtils;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -75,4 +77,77 @@ public void testHiveViewTranslationError()
// TODO: combine this with tests for successful translation (currently in TestHiveViews product test)
}
}

@Override
public void testUpdateBasicPartitionStatistics()
throws Exception
{
SchemaTableName tableName = temporaryTable("update_basic_partition_statistics");
try {
createDummyPartitionedTable(tableName, STATISTICS_PARTITIONED_TABLE_COLUMNS);
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
testUpdatePartitionStatistics(
tableName,
PartitionStatistics.empty(),
ImmutableList.of(BASIC_STATISTICS_1, BASIC_STATISTICS_2),
ImmutableList.of(BASIC_STATISTICS_2, BASIC_STATISTICS_1));
}
finally {
dropTable(tableName);
}
}

@Override
public void testUpdatePartitionColumnStatistics()
throws Exception
{
SchemaTableName tableName = temporaryTable("update_partition_column_statistics");
try {
createDummyPartitionedTable(tableName, STATISTICS_PARTITIONED_TABLE_COLUMNS);
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
testUpdatePartitionStatistics(
tableName,
PartitionStatistics.empty(),
ImmutableList.of(STATISTICS_1_1, STATISTICS_1_2, STATISTICS_2),
ImmutableList.of(STATISTICS_1_2, STATISTICS_1_1, STATISTICS_2));
}
finally {
dropTable(tableName);
}
}

@Override
public void testUpdatePartitionColumnStatisticsEmptyOptionalFields()
throws Exception
{
SchemaTableName tableName = temporaryTable("update_partition_column_statistics");
try {
createDummyPartitionedTable(tableName, STATISTICS_PARTITIONED_TABLE_COLUMNS);
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
testUpdatePartitionStatistics(
tableName,
PartitionStatistics.empty(),
ImmutableList.of(STATISTICS_EMPTY_OPTIONAL_FIELDS),
ImmutableList.of(STATISTICS_EMPTY_OPTIONAL_FIELDS));
}
finally {
dropTable(tableName);
}
}

@Override
public void testStorePartitionWithStatistics()
throws Exception
{
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
testStorePartitionWithStatistics(STATISTICS_PARTITIONED_TABLE_COLUMNS, STATISTICS_1, STATISTICS_2, STATISTICS_1_1, PartitionStatistics.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import static io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.getHiveBasicStatistics;
import static io.trino.plugin.hive.util.HiveUtil.makePartName;
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static java.util.AbstractMap.SimpleEntry;
import static java.util.Objects.requireNonNull;

Comment thread
Dith3r marked this conversation as resolved.
Outdated
/**
Expand Down Expand Up @@ -161,6 +162,18 @@ public Map<String, PartitionStatistics> getPartitionStatistics(Table table, List
Map<String, OptionalLong> partitionRowCounts = partitionBasicStatistics.entrySet().stream()
.collect(toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().getRowCount()));

long tableRowCount = partitionRowCounts.values().stream()
.mapToLong(count -> count.orElse(0))
.sum();
if (!partitionRowCounts.isEmpty() && tableRowCount == 0) {
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
partitionBasicStatistics = partitionBasicStatistics.keySet().stream()
.map(key -> new SimpleEntry<>(key, HiveBasicStatistics.createEmptyStatistics()))
.collect(toImmutableMap(SimpleEntry::getKey, SimpleEntry::getValue));
}

Map<String, List<ColumnStatisticsInfo>> colStatsMap = client.getPartitionColumnStatistics(table.getDatabaseName(), table.getTableName(),
ImmutableList.copyOf(partitionBasicStatistics.keySet()), dataColumns);
Map<String, Map<String, HiveColumnStatistics>> partitionColumnStatistics = colStatsMap.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import javax.inject.Inject;

import java.time.Duration;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -318,10 +319,24 @@ public PartitionStatistics getTableStatistics(Table table)
@Override
public Map<String, PartitionStatistics> getPartitionStatistics(Table table, List<Partition> partitions)
{
return columnStatisticsProvider.getPartitionColumnStatistics(partitions).entrySet().stream()
Map<String, PartitionStatistics> partitionBasicStatistics = columnStatisticsProvider.getPartitionColumnStatistics(partitions).entrySet().stream()
.collect(toImmutableMap(
entry -> makePartitionName(table, entry.getKey()),
entry -> new PartitionStatistics(getHiveBasicStatistics(entry.getKey().getParameters()), entry.getValue())));

long tableRowCount = partitionBasicStatistics.values().stream()
.mapToLong(partitionStatistics -> partitionStatistics.getBasicStatistics().getRowCount().orElse(0))
.sum();
if (!partitionBasicStatistics.isEmpty() && tableRowCount == 0) {
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
partitionBasicStatistics = partitionBasicStatistics.keySet().stream()
.map(key -> new SimpleEntry<>(key, PartitionStatistics.empty()))
.collect(toImmutableMap(SimpleEntry::getKey, SimpleEntry::getValue));
}

return partitionBasicStatistics;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -387,6 +388,19 @@ public Map<String, PartitionStatistics> getPartitionStatistics(Table table, List
}));
Map<String, OptionalLong> partitionRowCounts = partitionBasicStatistics.entrySet().stream()
.collect(toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().getRowCount()));

long tableRowCount = partitionRowCounts.values().stream()
.mapToLong(count -> count.orElse(0))
.sum();
if (!partitionRowCounts.isEmpty() && tableRowCount == 0) {
Comment thread
Dith3r marked this conversation as resolved.
Outdated
// When the table has partitions, but row count statistics are set to zero, we treat this case as empty
// statistics to avoid underestimation in the CBO. This scenario may be caused when other engines are
// used to ingest data into partitioned hive tables.
partitionBasicStatistics = partitionBasicStatistics.keySet().stream()
.map(partitionName -> new SimpleEntry<>(partitionName, HiveBasicStatistics.createEmptyStatistics()))
.collect(toImmutableMap(SimpleEntry::getKey, SimpleEntry::getValue));
}

Map<String, Map<String, HiveColumnStatistics>> partitionColumnStatistics = getPartitionColumnStatistics(
table.getDbName(),
table.getTableName(),
Expand Down
Loading