diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/file/ColumnStatistics.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/file/ColumnStatistics.java index 3ad64f81fc00..a5294aeb49ff 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/file/ColumnStatistics.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/file/ColumnStatistics.java @@ -82,8 +82,11 @@ public static ColumnStatistics fromHiveColumnStatistics(HiveColumnStatistics hiv public HiveColumnStatistics toHiveColumnStatistics(HiveBasicStatistics basicStatistics) { OptionalDouble averageColumnLength = this.averageColumnLength; - if (totalSizeInBytes.isPresent() && basicStatistics.getRowCount().orElse(0) > 0) { - averageColumnLength = OptionalDouble.of(totalSizeInBytes.getAsLong() / (double) basicStatistics.getRowCount().getAsLong()); + if (totalSizeInBytes.isPresent() && basicStatistics.getRowCount().orElse(0) > 0 && nullsCount().isPresent()) { + long nonNullCount = basicStatistics.getRowCount().getAsLong() - nullsCount().orElseThrow(); + if (nonNullCount > 0) { + averageColumnLength = OptionalDouble.of(totalSizeInBytes.getAsLong() / (double) nonNullCount); + } } return new HiveColumnStatistics( integerStatistics.map(stat -> new io.trino.plugin.hive.metastore.IntegerStatistics(stat.min(), stat.max())), diff --git a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/file/TestColumnStatistics.java b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/file/TestColumnStatistics.java index da5ef7731958..ffb05faa4a9e 100644 --- a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/file/TestColumnStatistics.java +++ b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/file/TestColumnStatistics.java @@ -96,7 +96,7 @@ private static Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map toHive(Map