fix: support Glue column statistics (#1309)

This commit is contained in:
David Phillips authored and GitHub committed 2026-06-13 01:59:45 -05:00
1 parent 2c057db396
commit e8e44e70bc
4 files changed
+228

No files matched your search

@@ -29,6 +29,7 @@ import java.util.Objects;
public class GlueJsonHandler {
private static final TypeReference<List<String>> STRING_LIST = new TypeReference<>() {};
private static final TypeReference<List<Map<String, Object>>> MAP_LIST = new TypeReference<>() {};
private final GlueService glueService;
private final GlueSchemaRegistryService schemaRegistryService;
@@ -112,6 +113,8 @@ public class GlueJsonHandler {
List<String> tableNames = mapper.convertValue(request.get("TablesToDelete"), STRING_LIST);
yield Response.ok(Map.of("Errors", glueService.batchDeleteTables(dbName, tableNames))).build();
}
case "UpdateColumnStatisticsForTable" -> handleUpdateColumnStatisticsForTable(request);
case "GetColumnStatisticsForTable" -> handleGetColumnStatisticsForTable(request);
case "CreatePartition" -> {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
@@ -156,6 +159,27 @@ public class GlueJsonHandler {
};
}
private Response handleUpdateColumnStatisticsForTable(JsonNode request) {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
List<Map<String, Object>> columnStatistics =
mapper.convertValue(request.get("ColumnStatisticsList"), MAP_LIST);
glueService.updateColumnStatisticsForTable(dbName, tableName, columnStatistics);
return Response.ok(Map.of("Errors", List.of())).build();
}
private Response handleGetColumnStatisticsForTable(JsonNode request) {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
List<String> columnNames = mapper.convertValue(request.get("ColumnNames"), STRING_LIST);
GlueService.ColumnStatisticsResult result =
glueService.getColumnStatisticsForTable(dbName, tableName, columnNames);
return Response.ok(Map.of(
"ColumnStatisticsList", result.columnStatisticsList(),
"Errors", result.errors()))
.build();
}
private Response handleCreateUserDefinedFunction(JsonNode request) throws Exception {
String dbName = request.get("DatabaseName").asText();
UserDefinedFunction function = mapper.treeToValue(request.get("FunctionInput"), UserDefinedFunction.class);
@@ -39,10 +39,15 @@ public class GlueService {
private static final Logger LOG = Logger.getLogger(GlueService.class);
private static final int MAX_FUNCTION_PATTERN_LENGTH = 255;
private static final int MAX_FUNCTION_RESULTS = 100;
static final String COLUMN_NAME = "ColumnName";
static final String COLUMN_TYPE = "ColumnType";
static final String ANALYZED_TIME = "AnalyzedTime";
static final String STATISTICS_DATA = "StatisticsData";
private final StorageBackend<String, Database> databaseStore;
private final StorageBackend<String, Table> tableStore;
private final StorageBackend<String, Table> tableVersionStore;
private final StorageBackend<String, Map<String, Object>> columnStatisticsStore;
private final StorageBackend<String, Partition> partitionStore;
private final StorageBackend<String, UserDefinedFunction> functionStore;
private final GlueSchemaRegistryService schemaRegistryService;
@@ -57,6 +62,7 @@ public class GlueService {
this.databaseStore = storageFactory.create("glue", "databases.json", new TypeReference<>() {});
this.tableStore = storageFactory.create("glue", "tables.json", new TypeReference<>() {});
this.tableVersionStore = storageFactory.create("glue", "table_versions.json", new TypeReference<>() {});
this.columnStatisticsStore = storageFactory.create("glue", "column_statistics.json", new TypeReference<>() {});
this.partitionStore = storageFactory.create("glue", "partitions.json", new TypeReference<>() {});
this.functionStore = storageFactory.create("glue", "functions.json", new TypeReference<>() {});
this.schemaRegistryService = schemaRegistryService;
@@ -67,6 +73,7 @@ public class GlueService {
GlueService(StorageBackend<String, Database> databaseStore,
StorageBackend<String, Table> tableStore,
StorageBackend<String, Table> tableVersionStore,
StorageBackend<String, Map<String, Object>> columnStatisticsStore,
StorageBackend<String, Partition> partitionStore,
StorageBackend<String, UserDefinedFunction> functionStore,
GlueSchemaRegistryService schemaRegistryService,
@@ -75,6 +82,7 @@ public class GlueService {
this.databaseStore = databaseStore;
this.tableStore = tableStore;
this.tableVersionStore = tableVersionStore;
this.columnStatisticsStore = columnStatisticsStore;
this.partitionStore = partitionStore;
this.functionStore = functionStore;
this.schemaRegistryService = schemaRegistryService;
@@ -218,6 +226,9 @@ public class GlueService {
tableVersionStore.keys().stream()
.filter(versionKey -> versionKey.startsWith(key + ":"))
.forEach(tableVersionStore::delete);
columnStatisticsStore.keys().stream()
.filter(statisticsKey -> statisticsKey.startsWith(key + ":"))
.forEach(columnStatisticsStore::delete);
partitionStore.scan(k -> k.startsWith(key + ":")).forEach(p -> {
partitionStore.delete(key + ":" + String.join(",", p.getValues()));
});
@@ -241,6 +252,44 @@ public class GlueService {
return errors;
}
public void updateColumnStatisticsForTable(
String databaseName,
String tableName,
List<Map<String, Object>> columnStatistics) {
Table table = getTable(databaseName, tableName);
for (Map<String, Object> statistics : columnStatistics) {
String columnNameString = requireColumnStatisticsString(statistics, COLUMN_NAME);
requireColumnStatisticsString(statistics, COLUMN_TYPE);
requireColumnStatisticsField(statistics, ANALYZED_TIME);
requireColumnStatisticsField(statistics, STATISTICS_DATA);
columnStatisticsStore.put(
columnStatisticsKey(table.getDatabaseName(), table.getName(), columnNameString),
new LinkedHashMap<>(statistics));
}
}
public ColumnStatisticsResult getColumnStatisticsForTable(
String databaseName,
String tableName,
List<String> columnNames) {
Table table = getTable(databaseName, tableName);
List<Map<String, Object>> columnStatistics = new ArrayList<>();
List<ColumnError> errors = new ArrayList<>();
for (String columnName : columnNames) {
Optional<Map<String, Object>> statistics =
columnStatisticsStore.get(columnStatisticsKey(table.getDatabaseName(), table.getName(), columnName));
if (statistics.isPresent()) {
columnStatistics.add(new LinkedHashMap<>(statistics.get()));
}
else {
errors.add(new ColumnError(
columnName,
new ErrorDetail("EntityNotFoundException", "Statistics do not exist for this column")));
}
}
return new ColumnStatisticsResult(columnStatistics, errors);
}
public void createPartition(String databaseName, String tableName, Partition partition) {
Table table = getTable(databaseName, tableName);
String key = tableKey(databaseName, tableName) + ":" + String.join(",", partition.getValues());
@@ -376,6 +425,26 @@ public class GlueService {
return tableKey(databaseName, tableName) + ":" + versionId;
}
private static String columnStatisticsKey(String databaseName, String tableName, String columnName) {
return tableKey(databaseName, tableName) + ":" + normalizeName(columnName);
}
private static String requireColumnStatisticsString(Map<String, Object> statistics, String field) {
Object value = requireColumnStatisticsField(statistics, field);
if (!(value instanceof String stringValue) || stringValue.isBlank()) {
throw new AwsException("InvalidInputException", field + " is required", 400);
}
return stringValue;
}
private static Object requireColumnStatisticsField(Map<String, Object> statistics, String field) {
Object value = statistics.get(field);
if (value == null) {
throw new AwsException("InvalidInputException", field + " is required", 400);
}
return value;
}
private static String normalizeName(String name) {
return name.toLowerCase(Locale.ROOT);
}
@@ -425,6 +494,14 @@ public class GlueService {
@JsonProperty("ErrorCode") String errorCode,
@JsonProperty("ErrorMessage") String errorMessage) {}
public record ColumnStatisticsResult(
@JsonProperty("ColumnStatisticsList") List<Map<String, Object>> columnStatisticsList,
@JsonProperty("Errors") List<ColumnError> errors) {}
public record ColumnError(
@JsonProperty("ColumnName") String columnName,
@JsonProperty("Error") ErrorDetail error) {}
private static Table copyTable(Table source) {
Table copy = new Table();
copy.setName(source.getName());