diff --git a/docs/services/glue.md b/docs/services/glue.md index 064068751f..12618b6929 100644 --- a/docs/services/glue.md +++ b/docs/services/glue.md @@ -25,7 +25,14 @@ Floci emulates the AWS Glue Data Catalog and Glue Schema Registry, allowing you | CreateTable | Creates a table definition in the local Glue Data Catalog. | | GetTable | Returns a stored table definition and resolves schema references when possible. | | GetTables | Lists table definitions for a database. | +| UpdateTable | Replaces a table definition; the previous version is archived unless `SkipArchive` is set. | | DeleteTable | Deletes a table definition from a database. | +| BatchDeleteTable | Deletes several tables of a database, reporting the missing ones in `Errors`. | +| GetTableVersions | Lists the current and archived versions of a table, newest first. | +| GetTableVersion | Returns one version by `VersionId`, or the current version when none is given. | +| DeleteTableVersion | Deletes an archived version; the current version cannot be deleted, as on AWS. | +| BatchDeleteTableVersion | Deletes several archived versions, reporting the ones not deleted in `Errors`. | +| SearchTables | Searches every table of the catalog by `SearchText` (substring, or exact when quoted), property `Filters` (string keys use the tokenised match of the API reference, time keys honour `Comparator`, other keys read table parameters) and `SortCriteria`, paged by `MaxResults` and `NextToken`. | #### Partitions @@ -33,6 +40,7 @@ Floci emulates the AWS Glue Data Catalog and Glue Schema Registry, allowing you |--------|-------------| | CreatePartition | Creates a partition for a Data Catalog table. | | GetPartitions | Lists partitions stored for a Data Catalog table. | +| BatchDeletePartition | Deletes up to 25 partitions, reporting the ones not found in `Errors`. | | CreatePartitionIndex | Registers a partition index on a table. Keys must name partition columns, and a table holds at most 3 indexes. The index reports `CREATING` before `ACTIVE`. | | GetPartitionIndexes | Lists a table's partition indexes, each with its keys resolved to name and type. | | DeletePartitionIndex | Removes a partition index from a table. The index reports `DELETING` before it disappears. | diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java index 273c97ddb8..4694a53a49 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java @@ -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 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> partitionsToDelete = mapper.convertValue(request.get("PartitionsToDelete"), MAP_LIST); + List> partitionValues = (partitionsToDelete == null ? List.>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 filters = request.hasNonNull("Filters") + ? mapper.convertValue(request.get("Filters"), new TypeReference>() {}) + : null; + List sortCriteria = request.hasNonNull("SortCriteria") + ? mapper.convertValue(request.get("SortCriteria"), new TypeReference>() {}) + : null; + GlueService.Page 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(); diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java index 45d706e334..36c2781a8f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java @@ -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
searchTables(String searchText, List filters, + List sortCriteria, Integer maxResults, String nextToken) { + List
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 searchableValues(Table table) { + List 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 values, String value) { + if (value != null && !value.isBlank()) { + values.add(value); + } + } + + private static boolean matchesFilters(Table table, List 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
searchComparator(List sortCriteria) { + Comparator
byName = Comparator.comparing(Table::getDatabaseName, Comparator.nullsLast(String::compareTo)) + .thenComparing(Table::getName, Comparator.nullsLast(String::compareTo)); + if (sortCriteria == null || sortCriteria.isEmpty()) { + return byName; + } + Comparator
result = null; + for (SearchSort criterion : sortCriteria) { + Comparator
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
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 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 batchDeleteTableVersions( + String databaseName, String tableName, List versionIds) { + Table current = getTable(databaseName, tableName); + if (versionIds.size() > 100) { + throw new AwsException("InvalidInputException", + "VersionIds must contain at most 100 versions.", 400); + } + List 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 batchDeletePartitions( + String databaseName, String tableName, List> partitionsToDelete) { + getTable(databaseName, tableName); + if (partitionsToDelete.size() > 25) { + throw new AwsException("InvalidInputException", + "PartitionsToDelete must contain at most 25 partitions.", 400); + } + List errors = new ArrayList<>(); + for (List 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 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 partitionValues, diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java index 8e33bd28cc..b7f23ddc12 100644 --- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java @@ -402,6 +402,89 @@ class GlueJsonHandlerTest { assertEquals("glueetl", job.get("Command").get("Name").asText()); } + @SuppressWarnings("unchecked") + private static Map body(Response response) { + assertEquals(200, response.getStatus()); + return (Map) response.getEntity(); + } + + /** The version calls answer with the reference's shapes: a TableVersion wrapper, per-version Errors. */ + @Test + void tableVersionCallsUseTheReferenceShapes() throws Exception { + createDatabaseAndTable("versions_db", "plain"); + ObjectNode update = mapper.createObjectNode(); + update.put("DatabaseName", "versions_db"); + update.putObject("TableInput").put("Name", "plain").put("Description", "second"); + assertEquals(200, handler.handle("UpdateTable", update, REGION).getStatus()); + + ObjectNode get = mapper.createObjectNode(); + get.put("DatabaseName", "versions_db").put("TableName", "plain").put("VersionId", "0"); + Map version = (Map) body(handler.handle("GetTableVersion", get, REGION)) + .get("TableVersion"); + assertEquals("0", version.get("VersionId")); + assertNotNull(version.get("Table")); + + ObjectNode batch = mapper.createObjectNode(); + batch.put("DatabaseName", "versions_db").put("TableName", "plain"); + batch.putArray("VersionIds").add("0").add("5"); + List errors = (List) body( + handler.handle("BatchDeleteTableVersion", batch, REGION)).get("Errors"); + assertEquals(1, errors.size()); + assertEquals("5", errors.get(0).versionId()); + assertEquals("plain", errors.get(0).tableName()); + assertEquals("{\"TableName\":\"plain\",\"VersionId\":\"5\",\"ErrorDetail\":{\"ErrorCode\":\"EntityNotFoundException\",\"ErrorMessage\":\"Version not found.\"}}", + mapper.writeValueAsString(errors.get(0))); + + ObjectNode delete = mapper.createObjectNode(); + delete.put("DatabaseName", "versions_db").put("TableName", "plain").put("VersionId", "0"); + AwsException gone = assertThrows(AwsException.class, () -> handler.handle("DeleteTableVersion", delete, REGION)); + assertEquals("EntityNotFoundException", gone.getErrorCode()); + } + + @Test + void batchDeletePartitionReportsMissingPartitionsInErrors() throws Exception { + createPartitionedTable("bdp_db", "events", "dt"); + ObjectNode create = mapper.createObjectNode(); + create.put("DatabaseName", "bdp_db").put("TableName", "events"); + create.putObject("PartitionInput").putArray("Values").add("2026-01-01"); + assertEquals(200, handler.handle("CreatePartition", create, REGION).getStatus()); + + ObjectNode batch = mapper.createObjectNode(); + batch.put("DatabaseName", "bdp_db").put("TableName", "events"); + ArrayNode toDelete = batch.putArray("PartitionsToDelete"); + toDelete.addObject().putArray("Values").add("2026-01-01"); + toDelete.addObject().putArray("Values").add("2026-01-02"); + List errors = (List) body( + handler.handle("BatchDeletePartition", batch, REGION)).get("Errors"); + assertEquals(1, errors.size()); + assertEquals(List.of("2026-01-02"), errors.get(0).partitionValues()); + assertTrue(mapper.writeValueAsString(errors.get(0)).startsWith("{\"PartitionValues\":[\"2026-01-02\"],\"ErrorDetail\":")); + + ObjectNode list = mapper.createObjectNode(); + list.put("DatabaseName", "bdp_db").put("TableName", "events"); + assertTrue(((List) body(handler.handle("GetPartitions", list, REGION)).get("Partitions")).isEmpty()); + } + + @Test + void searchTablesReturnsTableListWithPaging() throws Exception { + createDatabaseAndTable("search_db", "alpha"); + createDatabaseAndTable("search_db2", "beta"); + + ObjectNode search = mapper.createObjectNode(); + search.put("MaxResults", 1); + Map first = body(handler.handle("SearchTables", search, REGION)); + assertEquals(1, ((List) first.get("TableList")).size()); + assertNotNull(first.get("NextToken")); + + ObjectNode filtered = mapper.createObjectNode(); + filtered.putArray("Filters").addObject().put("Key", "DatabaseName").put("Value", "search_db2"); + filtered.putArray("SortCriteria").addObject().put("FieldName", "Name").put("Sort", "ASC"); + Map page = body(handler.handle("SearchTables", filtered, REGION)); + List tables = (List) page.get("TableList"); + assertEquals(1, tables.size()); + assertEquals("beta", ((io.github.hectorvent.floci.services.glue.model.Table) tables.get(0)).getName()); + } + private static final class InMemoryStorageFactory extends StorageFactory { private InMemoryStorageFactory() { super(null, null); diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java index 9bf4a9722e..80bda3da9f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueServiceTest.java @@ -37,6 +37,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.CountDownLatch; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -1311,4 +1312,173 @@ class GlueServiceTest { glueService.getPartitionIndexes("db1", "indexed"); } + // ── Table versions, batch partition delete and SearchTables ────────────── + + private Table namedTable(String name) { + Table table = new Table(); + table.setName(name); + return table; + } + + private void createVersions(String name, int updates) { + Table table = namedTable(name); + table.setDescription("v0"); + glueService.createTable("db1", table); + for (int i = 1; i <= updates; i++) { + Table replacement = namedTable(name); + replacement.setDescription("v" + i); + glueService.updateTable("db1", replacement, String.valueOf(i - 1), false); + } + } + + @Test + void getTableVersionReturnsTheRequestedOrTheCurrentVersion() { + createVersions("plain", 2); + + Map archived = glueService.getTableVersion("db1", "plain", "0"); + assertEquals("0", archived.get("VersionId")); + assertEquals("v0", ((Table) archived.get("Table")).getDescription()); + + Map current = glueService.getTableVersion("db1", "plain", null); + assertEquals("2", current.get("VersionId")); + assertEquals("v2", ((Table) current.get("Table")).getDescription()); + assertEquals("2", glueService.getTableVersion("db1", "plain", "2").get("VersionId")); + + AwsException missing = assertThrows(AwsException.class, + () -> glueService.getTableVersion("db1", "plain", "7")); + assertEquals("EntityNotFoundException", missing.getErrorCode()); + assertEquals("Version not found.", missing.getMessage()); + AwsException notAnInteger = assertThrows(AwsException.class, + () -> glueService.getTableVersion("db1", "plain", "latest")); + assertEquals("InvalidInputException", notAnInteger.getErrorCode()); + AwsException noTable = assertThrows(AwsException.class, + () -> glueService.getTableVersion("db1", "nope", "0")); + assertEquals("EntityNotFoundException", noTable.getErrorCode()); + } + + @Test + void deleteTableVersionDropsArchivedVersionsButNeverTheCurrentOne() { + createVersions("plain", 3); + + glueService.deleteTableVersion("db1", "plain", "1"); + List remaining = glueService.getTableVersions("db1", "plain").stream() + .map(version -> (String) version.get("VersionId")).toList(); + assertEquals(List.of("3", "2", "0"), remaining); + + AwsException current = assertThrows(AwsException.class, + () -> glueService.deleteTableVersion("db1", "plain", "3")); + assertEquals("InvalidInputException", current.getErrorCode()); + AwsException gone = assertThrows(AwsException.class, + () -> glueService.deleteTableVersion("db1", "plain", "1")); + assertEquals("EntityNotFoundException", gone.getErrorCode()); + AwsException blank = assertThrows(AwsException.class, + () -> glueService.deleteTableVersion("db1", "plain", null)); + assertEquals("InvalidInputException", blank.getErrorCode()); + + List errors = + glueService.batchDeleteTableVersions("db1", "plain", List.of("0", "9", "3", "2")); + assertEquals(List.of("9", "3"), errors.stream().map(GlueService.TableVersionError::versionId).toList()); + assertEquals("EntityNotFoundException", errors.get(0).errorDetail().errorCode()); + assertEquals("InvalidInputException", errors.get(1).errorDetail().errorCode()); + assertEquals("plain", errors.get(0).tableName()); + assertEquals(1, glueService.getTableVersions("db1", "plain").size(), "only the current version is left"); + + List tooMany = java.util.stream.IntStream.rangeClosed(1, 101) + .mapToObj(String::valueOf).toList(); + AwsException overCap = assertThrows(AwsException.class, + () -> glueService.batchDeleteTableVersions("db1", "plain", tooMany)); + assertEquals("InvalidInputException", overCap.getErrorCode()); + } + + @Test + void batchDeletePartitionDeletesWhatExistsAndReportsTheRest() { + Table table = namedTable("events"); + table.setPartitionKeys(List.of(new Column("dt", "string"))); + glueService.createTable("db1", table); + for (String dt : List.of("2026-01-01", "2026-01-02", "2026-01-03")) { + Partition partition = new Partition(); + partition.setValues(List.of(dt)); + glueService.createPartition("db1", "events", partition); + } + + List errors = glueService.batchDeletePartitions( + "db1", "events", List.of(List.of("2026-01-01"), List.of("2026-01-09"), List.of("2026-01-03"))); + + assertEquals(1, errors.size()); + assertEquals(List.of("2026-01-09"), errors.get(0).partitionValues()); + assertEquals("EntityNotFoundException", errors.get(0).errorDetail().errorCode()); + assertEquals(List.of(List.of("2026-01-02")), + glueService.getPartitions("db1", "events").stream().map(Partition::getValues).toList()); + AwsException noTable = assertThrows(AwsException.class, + () -> glueService.batchDeletePartitions("db1", "nope", List.of(List.of("x")))); + assertEquals("EntityNotFoundException", noTable.getErrorCode()); + } + + @Test + void searchTablesMatchesTextTokensAndTimesAcrossDatabases() { + glueService.createDatabase(new Database("db2")); + Table orders = namedTable("customer-orders"); + orders.setDescription("Orders placed by customers"); + orders.setOwner("sales"); + orders.setTableType("EXTERNAL_TABLE"); + StorageDescriptor sd = new StorageDescriptor(); + sd.setColumns(List.of(new Column("order_id", "string"), new Column("total", "double"))); + orders.setStorageDescriptor(sd); + orders.setParameters(Map.of("classification", "parquet")); + glueService.createTable("db1", orders); + Table link = namedTable("xx-link-yy"); + link.setOwner("data"); + glueService.createTable("db2", link); + Table nolink = namedTable("xxlinkyy"); + nolink.setOwner("data"); + glueService.createTable("db2", nolink); + + List all = names(glueService.searchTables(null, null, null, null, null)); + assertEquals(List.of("customer-orders", "xx-link-yy", "xxlinkyy"), all, "database then name"); + + assertEquals(List.of("customer-orders"), names(glueService.searchTables("ORDER_ID", null, null, null, null)), + "column names take part in the text search"); + assertEquals(List.of("xx-link-yy", "xxlinkyy"), names(glueService.searchTables("link", null, null, null, null))); + assertEquals(List.of("xx-link-yy"), + names(glueService.searchTables("\"xx-link-yy\"", null, null, null, null)), "quotes mean exact"); + assertEquals(List.of(), names(glueService.searchTables("\"link\"", null, null, null, null))); + + // The reference's example: Key=Name, Value=link finds xx-link-yy but not xxlinkyy. + assertEquals(List.of("xx-link-yy"), names(glueService.searchTables(null, + List.of(new GlueService.SearchFilter("Name", "link", null)), null, null, null))); + assertEquals(List.of("customer-orders"), names(glueService.searchTables(null, + List.of(new GlueService.SearchFilter("DatabaseName", "db1", null), + new GlueService.SearchFilter("classification", "parquet", null)), null, null, null))); + assertEquals(List.of(), names(glueService.searchTables(null, + List.of(new GlueService.SearchFilter("Owner", "sales", null), + new GlueService.SearchFilter("TableType", "VIRTUAL_VIEW", null)), null, null, null))); + + long future = Instant.now().getEpochSecond() + 3600; + assertEquals(3, glueService.searchTables(null, + List.of(new GlueService.SearchFilter("CreateTime", String.valueOf(future), "LESS_THAN")), + null, null, null).items().size()); + assertEquals(List.of(), names(glueService.searchTables(null, + List.of(new GlueService.SearchFilter("CreateTime", String.valueOf(future), "GREATER_THAN")), + null, null, null))); + AwsException badComparator = assertThrows(AwsException.class, () -> glueService.searchTables(null, + List.of(new GlueService.SearchFilter("CreateTime", "0", "BETWEEN")), null, null, null)); + assertEquals("InvalidInputException", badComparator.getErrorCode()); + + assertEquals(List.of("xxlinkyy", "xx-link-yy", "customer-orders"), names(glueService.searchTables(null, null, + List.of(new GlueService.SearchSort("Name", "DESC")), null, null))); + AwsException badField = assertThrows(AwsException.class, () -> glueService.searchTables(null, null, + List.of(new GlueService.SearchSort("Columns", "ASC")), null, null)); + assertEquals("InvalidInputException", badField.getErrorCode()); + + GlueService.Page
first = glueService.searchTables(null, null, null, 2, null); + assertEquals(2, first.items().size()); + assertNotNull(first.nextToken()); + GlueService.Page
second = glueService.searchTables(null, null, null, 2, first.nextToken()); + assertEquals(List.of("xxlinkyy"), names(second)); + assertNull(second.nextToken()); + } + + private static List names(GlueService.Page
page) { + return page.items().stream().map(Table::getName).toList(); + } }