feat(glue): table version reads and deletes, batch partition delete, SearchTables (#3828)

GetTableVersion answers one archived version by VersionId, or the
current table when none is given; an unknown version is
EntityNotFoundException and a non-integer one InvalidInputException.
DeleteTableVersion and BatchDeleteTableVersion drop archived versions,
the batch form reporting per-version Errors; the current version is
refused, as AWS refuses it on the console and the API, since DeleteTable
is the way to drop it.

BatchDeletePartition deletes up to 25 partitions and reports the ones
not found in Errors, the shape the other batch partition calls use.

SearchTables searches every table of the catalog: SearchText as a
substring of name, database, description, owner, columns and parameter
values (exact when quoted), Filters with the API reference's tokenised
match for string keys and a Comparator for the time fields, SortCriteria
on the same fields, and MaxResults with NextToken paging.

Closes #3826
This commit is contained in:
David authored and GitHub committed 2026-09-17 14:43:10 -04:00
1 parent 54a5cf3e9c
commit 30b491a546
5 files changed
+590

No files matched your search

@@ -124,6 +124,26 @@ public class GlueJsonHandler {
String tableName = request.get("TableName").asText();
yield Response.ok(Map.of("TableVersions", glueService.getTableVersions(dbName, tableName))).build();
}
case "GetTableVersion" -> {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
yield Response.ok(Map.of("TableVersion", glueService.getTableVersion(
dbName, tableName, request.path("VersionId").asText(null)))).build();
}
case "DeleteTableVersion" -> {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
glueService.deleteTableVersion(dbName, tableName, request.path("VersionId").asText(null));
yield Response.ok().build();
}
case "BatchDeleteTableVersion" -> {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
List<String> versionIds = mapper.convertValue(request.get("VersionIds"), STRING_LIST);
yield Response.ok(Map.of("Errors", glueService.batchDeleteTableVersions(
dbName, tableName, versionIds == null ? List.of() : versionIds))).build();
}
case "SearchTables" -> handleSearchTables(request);
case "DeleteTable" -> {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("Name").asText();
@@ -156,6 +176,7 @@ public class GlueJsonHandler {
yield Response.ok(Map.of("Partitions", glueService.getPartitions(dbName, tableName, expression))).build();
}
case "DeletePartition" -> handleDeletePartition(request);
case "BatchDeletePartition" -> handleBatchDeletePartition(request);
case "UpdatePartition" -> handleUpdatePartition(request);
case "UpdateColumnStatisticsForPartition" -> handleUpdateColumnStatisticsForPartition(request);
case "GetColumnStatisticsForPartition" -> handleGetColumnStatisticsForPartition(request);
@@ -319,6 +340,31 @@ public class GlueJsonHandler {
.build();
}
private Response handleBatchDeletePartition(JsonNode request) {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
List<Map<String, Object>> partitionsToDelete = mapper.convertValue(request.get("PartitionsToDelete"), MAP_LIST);
List<List<String>> partitionValues = (partitionsToDelete == null ? List.<Map<String, Object>>of() : partitionsToDelete)
.stream()
.map(partition -> mapper.convertValue(partition.get("Values"), STRING_LIST))
.toList();
return Response.ok(Map.of(
"Errors", glueService.batchDeletePartitions(dbName, tableName, partitionValues))).build();
}
private Response handleSearchTables(JsonNode request) {
List<GlueService.SearchFilter> filters = request.hasNonNull("Filters")
? mapper.convertValue(request.get("Filters"), new TypeReference<List<GlueService.SearchFilter>>() {})
: null;
List<GlueService.SearchSort> sortCriteria = request.hasNonNull("SortCriteria")
? mapper.convertValue(request.get("SortCriteria"), new TypeReference<List<GlueService.SearchSort>>() {})
: null;
GlueService.Page<Table> page = glueService.searchTables(
request.path("SearchText").asText(null), filters, sortCriteria,
readMaxResults(request), readNextToken(request));
return Response.ok(pageResponse("TableList", page.items(), page.nextToken())).build();
}
private Response handleDeletePartition(JsonNode request) {
String dbName = request.get("DatabaseName").asText();
String tableName = request.get("TableName").asText();
@@ -243,6 +243,198 @@ public class GlueService {
return resolved;
}
/**
* SearchTables over every table of the catalog. SearchText matches a substring of the name,
* database, description, owner, column names and comments and parameter values, or the whole
* name when quoted, as the reference describes. String filters use the reference's tokenised
* match (the field split on punctuation, each token compared whole), time filters honour the
* Comparator, and any other key is looked up in the table's parameters.
*/
public Page<Table> searchTables(String searchText, List<SearchFilter> filters,
List<SearchSort> sortCriteria, Integer maxResults, String nextToken) {
List<Table> matches = new ArrayList<>();
for (Table stored : tableStore.scan(k -> true)) {
Table table = withResolvedSchemaReference(stored);
if (matchesSearchText(table, searchText) && matchesFilters(table, filters)) {
matches.add(table);
}
}
matches.sort(searchComparator(sortCriteria));
return paginate(matches, maxResults, nextToken);
}
@RegisterForReflection
public record SearchFilter(
@JsonProperty("Key") String key,
@JsonProperty("Value") String value,
@JsonProperty("Comparator") String comparator) {}
@RegisterForReflection
public record SearchSort(
@JsonProperty("FieldName") String fieldName,
@JsonProperty("Sort") String sort) {}
private static boolean matchesSearchText(Table table, String searchText) {
if (searchText == null || searchText.isBlank()) {
return true;
}
String text = searchText.trim();
boolean exact = text.length() >= 2 && text.startsWith("\"") && text.endsWith("\"");
if (exact) {
text = text.substring(1, text.length() - 1);
}
String needle = text.toLowerCase(Locale.ROOT);
if (exact) {
return searchableValues(table).stream().anyMatch(value -> value.equalsIgnoreCase(needle));
}
return searchableValues(table).stream()
.anyMatch(value -> value.toLowerCase(Locale.ROOT).contains(needle));
}
private static List<String> searchableValues(Table table) {
List<String> values = new ArrayList<>();
addIfPresent(values, table.getName());
addIfPresent(values, table.getDatabaseName());
addIfPresent(values, table.getDescription());
addIfPresent(values, table.getOwner());
if (table.getStorageDescriptor() != null && table.getStorageDescriptor().getColumns() != null) {
for (Column column : table.getStorageDescriptor().getColumns()) {
addIfPresent(values, column.getName());
addIfPresent(values, column.getComment());
}
}
if (table.getPartitionKeys() != null) {
for (Column column : table.getPartitionKeys()) {
addIfPresent(values, column.getName());
addIfPresent(values, column.getComment());
}
}
if (table.getParameters() != null) {
table.getParameters().values().forEach(value -> addIfPresent(values, value));
}
return values;
}
private static void addIfPresent(List<String> values, String value) {
if (value != null && !value.isBlank()) {
values.add(value);
}
}
private static boolean matchesFilters(Table table, List<SearchFilter> filters) {
if (filters == null) {
return true;
}
for (SearchFilter filter : filters) {
if (filter == null || filter.key() == null || filter.key().isBlank()) {
throw new AwsException("InvalidInputException", "Filter Key is required.", 400);
}
if (!matchesFilter(table, filter)) {
return false;
}
}
return true;
}
private static boolean matchesFilter(Table table, SearchFilter filter) {
String key = filter.key();
String value = filter.value() == null ? "" : filter.value();
switch (key) {
case "Name": return tokenMatch(table.getName(), value);
case "DatabaseName": return tokenMatch(table.getDatabaseName(), value);
case "Owner": return tokenMatch(table.getOwner(), value);
case "TableType": return tokenMatch(table.getTableType(), value);
case "Description": return tokenMatch(table.getDescription(), value);
case "CreateTime": return timeMatch(table.getCreateTime(), value, filter.comparator());
case "UpdateTime": return timeMatch(table.getUpdateTime(), value, filter.comparator());
case "LastAccessTime": return timeMatch(table.getLastAccessTime(), value, filter.comparator());
default:
return table.getParameters() != null && tokenMatch(table.getParameters().get(key), value);
}
}
/** The reference's fuzzy match: the field split on punctuation, each token compared whole. */
private static boolean tokenMatch(String field, String value) {
if (field == null) {
return false;
}
if (field.equalsIgnoreCase(value)) {
return true;
}
for (String token : field.split("[\\p{Punct}\\s]+")) {
if (!token.isEmpty() && token.equalsIgnoreCase(value)) {
return true;
}
}
return false;
}
private static boolean timeMatch(Instant field, String value, String comparator) {
if (field == null) {
return false;
}
Instant bound = parseSearchTime(value);
int cmp = field.compareTo(bound);
String op = comparator == null || comparator.isBlank() ? "EQUALS" : comparator;
return switch (op) {
case "EQUALS" -> cmp == 0;
case "GREATER_THAN" -> cmp > 0;
case "LESS_THAN" -> cmp < 0;
case "GREATER_THAN_EQUALS" -> cmp >= 0;
case "LESS_THAN_EQUALS" -> cmp <= 0;
default -> throw new AwsException("InvalidInputException",
"Invalid Comparator: " + comparator, 400);
};
}
/** A time filter value: epoch seconds as AWS timestamps travel, or an ISO-8601 instant. */
private static Instant parseSearchTime(String value) {
try {
return Instant.ofEpochSecond(Long.parseLong(value.trim()));
} catch (NumberFormatException ignored) {
// not epoch seconds
}
try {
return Instant.parse(value.trim());
} catch (java.time.format.DateTimeParseException e) {
throw new AwsException("InvalidInputException", "Invalid time value: " + value, 400);
}
}
private static Comparator<Table> searchComparator(List<SearchSort> sortCriteria) {
Comparator<Table> byName = Comparator.comparing(Table::getDatabaseName, Comparator.nullsLast(String::compareTo))
.thenComparing(Table::getName, Comparator.nullsLast(String::compareTo));
if (sortCriteria == null || sortCriteria.isEmpty()) {
return byName;
}
Comparator<Table> result = null;
for (SearchSort criterion : sortCriteria) {
Comparator<Table> next = sortField(criterion.fieldName());
if ("DESC".equalsIgnoreCase(criterion.sort())) {
next = next.reversed();
} else if (criterion.sort() != null && !"ASC".equalsIgnoreCase(criterion.sort())) {
throw new AwsException("InvalidInputException", "Invalid Sort: " + criterion.sort(), 400);
}
result = result == null ? next : result.thenComparing(next);
}
return result.thenComparing(byName);
}
private static Comparator<Table> sortField(String fieldName) {
String field = fieldName == null ? "" : fieldName;
return switch (field) {
case "Name" -> Comparator.comparing(Table::getName, Comparator.nullsLast(String::compareTo));
case "DatabaseName" -> Comparator.comparing(Table::getDatabaseName, Comparator.nullsLast(String::compareTo));
case "Owner" -> Comparator.comparing(Table::getOwner, Comparator.nullsLast(String::compareTo));
case "TableType" -> Comparator.comparing(Table::getTableType, Comparator.nullsLast(String::compareTo));
case "CreateTime" -> Comparator.comparing(Table::getCreateTime, Comparator.nullsLast(Instant::compareTo));
case "UpdateTime" -> Comparator.comparing(Table::getUpdateTime, Comparator.nullsLast(Instant::compareTo));
case "LastAccessTime" -> Comparator.comparing(Table::getLastAccessTime, Comparator.nullsLast(Instant::compareTo));
default -> throw new AwsException("InvalidInputException",
"Invalid sort FieldName: " + fieldName, 400);
};
}
public synchronized void updateTable(String databaseName, Table table, String versionId, boolean skipArchive) {
Database database = getDatabase(databaseName);
String key = tableKey(databaseName, table.getName());
@@ -282,6 +474,71 @@ public class GlueService {
.toList();
}
/** One archived version, or the current table when no VersionId is given, as AWS answers. */
public Map<String, Object> getTableVersion(String databaseName, String tableName, String versionId) {
Table current = getTable(databaseName, tableName);
Table version;
if (versionId == null || versionId.isBlank() || versionId.equals(current.getVersionId())) {
version = current;
} else {
requireIntegerVersionId(versionId);
version = tableVersionStore.get(tableVersionKey(databaseName, tableName, versionId))
.map(this::withResolvedSchemaReference)
.orElseThrow(() -> new AwsException("EntityNotFoundException", "Version not found.", 400));
}
return Map.of("Table", version, "VersionId", version.getVersionId());
}
/**
* Drops an archived version. The current version is never deletable on AWS (DeleteTable is
* the way to drop it), so it is refused rather than silently kept.
*/
public synchronized void deleteTableVersion(String databaseName, String tableName, String versionId) {
Table current = getTable(databaseName, tableName);
requireIntegerVersionId(versionId);
if (versionId.equals(current.getVersionId())) {
throw new AwsException("InvalidInputException",
"Cannot delete the latest version " + versionId + " of table " + tableName
+ "; delete the table instead.", 400);
}
String key = tableVersionKey(databaseName, tableName, versionId);
if (tableVersionStore.get(key).isEmpty()) {
throw new AwsException("EntityNotFoundException", "Version not found.", 400);
}
tableVersionStore.delete(key);
LOG.infov("Deleted Glue Table version: {0}.{1} v{2}", databaseName, tableName, versionId);
}
public synchronized List<TableVersionError> batchDeleteTableVersions(
String databaseName, String tableName, List<String> versionIds) {
Table current = getTable(databaseName, tableName);
if (versionIds.size() > 100) {
throw new AwsException("InvalidInputException",
"VersionIds must contain at most 100 versions.", 400);
}
List<TableVersionError> errors = new ArrayList<>();
for (String versionId : versionIds) {
try {
deleteTableVersion(databaseName, current.getName(), versionId);
} catch (AwsException e) {
errors.add(new TableVersionError(current.getName(), versionId,
new ErrorDetail(e.getErrorCode(), e.getMessage())));
}
}
return errors;
}
private static void requireIntegerVersionId(String versionId) {
if (versionId == null || versionId.isBlank()) {
throw new AwsException("InvalidInputException", "VersionId is required.", 400);
}
try {
Long.parseLong(versionId);
} catch (NumberFormatException e) {
throw new AwsException("InvalidInputException", "Invalid table VersionId: " + versionId, 400);
}
}
public void deleteTable(String databaseName, String tableName) {
getTable(databaseName, tableName);
String key = tableKey(databaseName, tableName);
@@ -621,6 +878,26 @@ public class GlueService {
.forEach(partitionColumnStatisticsStore::delete);
}
/** Deletes what exists and reports the rest, the batch shape BatchCreatePartition uses. */
public List<BatchCreatePartitionError> batchDeletePartitions(
String databaseName, String tableName, List<List<String>> partitionsToDelete) {
getTable(databaseName, tableName);
if (partitionsToDelete.size() > 25) {
throw new AwsException("InvalidInputException",
"PartitionsToDelete must contain at most 25 partitions.", 400);
}
List<BatchCreatePartitionError> errors = new ArrayList<>();
for (List<String> values : partitionsToDelete) {
if (partitionStore.get(partitionKey(databaseName, tableName, values)).isEmpty()) {
errors.add(new BatchCreatePartitionError(values,
new ErrorDetail("EntityNotFoundException", "Cannot find partition.")));
continue;
}
deletePartition(databaseName, tableName, values);
}
return errors;
}
public void updatePartition(String databaseName, String tableName, List<String> partitionValues, Partition partition) {
Table table = getTable(databaseName, tableName);
String key = partitionKey(databaseName, tableName, partitionValues);
@@ -1090,6 +1367,12 @@ public class GlueService {
@JsonProperty("TableName") String tableName,
@JsonProperty("ErrorDetail") ErrorDetail errorDetail) {}
@RegisterForReflection
public record TableVersionError(
@JsonProperty("TableName") String tableName,
@JsonProperty("VersionId") String versionId,
@JsonProperty("ErrorDetail") ErrorDetail errorDetail) {}
@RegisterForReflection
public record BatchCreatePartitionError(
@JsonProperty("PartitionValues") List<String> partitionValues,