fix(glue): read back omitted numeric string and binary column statistics as 0 (#5747)

This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-10-16 07:55:12 -04:00
1 parent 23e1b3a97c
commit b2d98d6d08
2 files changed
+91 -1

No files matched your search

@@ -84,6 +84,11 @@ public class GlueService {
static final String COLUMN_TYPE = "ColumnType";
static final String ANALYZED_TIME = "AnalyzedTime";
static final String STATISTICS_DATA = "StatisticsData";
private static final Map<String, List<String>> NUMERIC_STATISTICS_FIELDS = Map.of(
"StringColumnStatisticsData",
List.of("MaximumLength", "AverageLength", "NumberOfNulls", "NumberOfDistinctValues"),
"BinaryColumnStatisticsData",
List.of("MaximumLength", "AverageLength", "NumberOfNulls"));
private static final Pattern COMPARISON_EXPRESSION = Pattern.compile(
"\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*(=|<>|<=|>=|<|>)\\s*('?[^']*'?|[^\\s]+)\\s*",
Pattern.CASE_INSENSITIVE);
@@ -794,7 +799,7 @@ public class GlueService {
Optional<Map<String, Object>> statistics =
columnStatisticsStore.get(columnStatisticsKey(table.getDatabaseName(), table.getName(), columnName));
if (statistics.isPresent()) {
columnStatistics.add(new LinkedHashMap<>(statistics.get()));
columnStatistics.add(withNumericDefaults(statistics.get()));
}
else {
errors.add(new ColumnError(
@@ -1122,6 +1127,7 @@ public class GlueService {
List<ColumnError> errors = new ArrayList<>();
for (String columnName : columnNames) {
partitionColumnStatisticsStore.get(partitionColumnStatisticsKey(databaseName, tableName, partitionValues, columnName))
.map(GlueService::withNumericDefaults)
.ifPresentOrElse(columnStatistics::add, () -> errors.add(columnStatisticsNotFoundError(columnName)));
}
return new ColumnStatisticsResult(columnStatistics, errors);
@@ -1303,6 +1309,34 @@ public class GlueService {
return stringValue;
}
/**
* Glue reads back the numeric string and binary statistics a writer left out as 0, and clients such as Trino
* unbox them without a null check, so every statistic read back carries all of them, whatever the stored
* record holds.
*/
private static Map<String, Object> withNumericDefaults(Map<String, Object> statistics) {
Map<String, Object> stored = new LinkedHashMap<>(statistics);
if (!(stored.get(STATISTICS_DATA) instanceof Map<?, ?> data)) {
return stored;
}
Map<String, Object> storedData = copyOf(data);
NUMERIC_STATISTICS_FIELDS.forEach((typeKey, fields) -> {
if (storedData.get(typeKey) instanceof Map<?, ?> typed) {
Map<String, Object> filled = copyOf(typed);
fields.forEach(field -> filled.putIfAbsent(field, 0));
storedData.put(typeKey, filled);
}
});
stored.put(STATISTICS_DATA, storedData);
return stored;
}
private static Map<String, Object> copyOf(Map<?, ?> source) {
Map<String, Object> copy = new LinkedHashMap<>();
source.forEach((key, value) -> copy.put(String.valueOf(key), value));
return copy;
}
private static Object requireColumnStatisticsField(Map<String, Object> statistics, String field) {
Object value = statistics.get(field);
if (value == null) {