mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
54a5cf3e9c
commit
30b491a546
5 files changed
+590
No files matched your search
@@ -402,6 +402,89 @@ class GlueJsonHandlerTest {
|
||||
assertEquals("glueetl", job.get("Command").get("Name").asText());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, Object> body(Response response) {
|
||||
assertEquals(200, response.getStatus());
|
||||
return (Map<String, Object>) 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<String, Object> version = (Map<String, Object>) 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<GlueService.TableVersionError> errors = (List<GlueService.TableVersionError>) 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<GlueService.BatchCreatePartitionError> errors = (List<GlueService.BatchCreatePartitionError>) 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<String, Object> 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<String, Object> 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);
|
||||
|
||||
@@ -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<String, Object> archived = glueService.getTableVersion("db1", "plain", "0");
|
||||
assertEquals("0", archived.get("VersionId"));
|
||||
assertEquals("v0", ((Table) archived.get("Table")).getDescription());
|
||||
|
||||
Map<String, Object> 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<String> 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<GlueService.TableVersionError> 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<String> 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<GlueService.BatchCreatePartitionError> 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<String> 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<Table> first = glueService.searchTables(null, null, null, 2, null);
|
||||
assertEquals(2, first.items().size());
|
||||
assertNotNull(first.nextToken());
|
||||
GlueService.Page<Table> second = glueService.searchTables(null, null, null, 2, first.nextToken());
|
||||
assertEquals(List.of("xxlinkyy"), names(second));
|
||||
assertNull(second.nextToken());
|
||||
}
|
||||
|
||||
private static List<String> names(GlueService.Page<Table> page) {
|
||||
return page.items().stream().map(Table::getName).toList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user