mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(athena): query and write S3 Tables catalogs (s3tablescatalog/<bucket>) (#8549)
This commit is contained in:
1 parent
0b6077d374
commit
35b03c6bf6
10 files changed
+444
-43
No files matched your search
@@ -14,7 +14,9 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AthenaHiveDdlExecutorTest {
|
||||
@@ -35,6 +37,27 @@ class AthenaHiveDdlExecutorTest {
|
||||
assertEquals("EntityNotFoundException", e.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* In an S3 Tables context catalog, DROP TABLE deletes the bucket's table and OPTIMIZE is refused as VACUUM is; in
|
||||
* the default catalog, OPTIMIZE is not supported yet.
|
||||
*/
|
||||
@Test
|
||||
void optimizeIsRefusedInAnS3TablesCatalogAndNotSupportedYetInTheDefaultOne() {
|
||||
AthenaIcebergStatements iceberg = Mockito.mock(AthenaIcebergStatements.class);
|
||||
AthenaHiveDdlExecutor executor = new AthenaHiveDdlExecutor(Mockito.mock(GlueService.class),
|
||||
Mockito.mock(S3Service.class), iceberg);
|
||||
|
||||
executor.execute(AthenaHiveDdl.parse("DROP TABLE orders"), "s3tablescatalog/lake", "sales");
|
||||
verify(iceberg).dropS3Table("lake", "sales", "orders", false);
|
||||
AwsException optimize = assertThrows(AwsException.class, () -> executor.execute(
|
||||
AthenaHiveDdl.parse("OPTIMIZE orders REWRITE DATA USING BIN_PACK"), "s3tablescatalog/lake", "sales"));
|
||||
assertTrue(optimize.getMessage().contains("not supported on S3 Tables"), optimize.getMessage());
|
||||
|
||||
AwsException unsupported = assertThrows(AwsException.class, () -> executor.execute(
|
||||
AthenaHiveDdl.parse("OPTIMIZE lake.events REWRITE DATA USING BIN_PACK"), "default"));
|
||||
assertEquals("OPTIMIZE is not supported yet", unsupported.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFolderWithAnInvalidEscapeIsNotAPartitionFolder() {
|
||||
Column region = new Column();
|
||||
|
||||
@@ -164,6 +164,14 @@ class AthenaHiveDdlTest {
|
||||
AthenaHiveDdl.parse("vacuum `_events`")).ref());
|
||||
}
|
||||
|
||||
@Test
|
||||
void optimizeNamesItsTableWhateverItsClauses() {
|
||||
assertEquals(new AthenaHiveDdl.TableRef("lake", "events"), assertInstanceOf(AthenaHiveDdl.Optimize.class,
|
||||
AthenaHiveDdl.parse("OPTIMIZE lake.events REWRITE DATA USING BIN_PACK WHERE id > 5")).ref());
|
||||
assertEquals(new AthenaHiveDdl.TableRef(null, "events"), assertInstanceOf(AthenaHiveDdl.Optimize.class,
|
||||
AthenaHiveDdl.parse("optimize events rewrite data using bin_pack;")).ref());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropRepairAndShowNameTheirTable() {
|
||||
AthenaHiveDdl.DropTable drop = assertInstanceOf(AthenaHiveDdl.DropTable.class,
|
||||
|
||||
+109
-15
@@ -1,6 +1,7 @@
|
||||
package io.floci.aws.services.athena;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -9,11 +10,16 @@ import io.floci.aws.services.athena.model.QueryExecutionContext;
|
||||
import io.floci.aws.services.athena.model.QueryExecutionState;
|
||||
import io.floci.aws.services.athena.model.ResultConfiguration;
|
||||
import io.floci.aws.services.floci.duck.FlociDuckClient;
|
||||
import io.floci.aws.services.glue.GlueCatalogService;
|
||||
import io.floci.aws.services.glue.GlueService;
|
||||
import io.floci.aws.services.glue.iceberg.GlueIcebergCatalog;
|
||||
import io.floci.aws.services.glue.model.Database;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3tables.S3TablesService;
|
||||
import io.floci.aws.services.s3tables.model.Namespace;
|
||||
import io.floci.aws.services.s3tables.model.S3Table;
|
||||
import io.floci.aws.services.s3tables.model.TableBucket;
|
||||
import io.floci.aws.testing.SidecarContainersProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
@@ -24,6 +30,7 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
@@ -76,6 +83,12 @@ class AthenaIcebergSidecarIntegrationTest {
|
||||
@Inject
|
||||
GlueIcebergCatalog catalog;
|
||||
|
||||
@Inject
|
||||
S3TablesService s3Tables;
|
||||
|
||||
@Inject
|
||||
GlueCatalogService catalogs;
|
||||
|
||||
private AthenaService athena;
|
||||
private String bucket;
|
||||
private String database;
|
||||
@@ -174,28 +187,109 @@ class AthenaIcebergSidecarIntegrationTest {
|
||||
rows("SELECT id FROM events ORDER BY id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anS3TablesCatalogIsCreatedWrittenAndReadInItsOwnNameAndAsTheContext() throws Exception {
|
||||
String tableBucket = "athena-s3t-" + System.nanoTime();
|
||||
String catalogName = "s3tablescatalog/" + tableBucket;
|
||||
TableBucket created = RequestScopes.callAs(ACCOUNT, REGION, () -> {
|
||||
TableBucket made = s3Tables.createTableBucket(tableBucket, null, null, Map.of(), REGION);
|
||||
try {
|
||||
catalogs.createCatalog("s3tablescatalog", new ObjectMapper().readTree(
|
||||
"{\"FederatedCatalog\":{\"Identifier\":\"" + made.getArn()
|
||||
+ "\",\"ConnectionName\":\"aws:s3tables\"}}"), Map.of(), REGION);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return made;
|
||||
});
|
||||
try {
|
||||
run(catalogName, null, "CREATE DATABASE sales");
|
||||
run(catalogName, "sales", "CREATE TABLE orders (id bigint, name string) "
|
||||
+ "TBLPROPERTIES ('table_type'='iceberg')");
|
||||
run(catalogName, "sales", "INSERT INTO orders VALUES (1, 'a'), (2, 'b')");
|
||||
run(null, database, "INSERT INTO \"" + catalogName + "\".sales.orders VALUES (3, 'c')");
|
||||
run(catalogName, "sales", "DELETE FROM orders WHERE id = 1");
|
||||
|
||||
assertEquals(List.of(row("id", "2", "name", "b"), row("id", "3", "name", "c")),
|
||||
s3TablesRows(catalogName, "sales", "SELECT id, name FROM orders ORDER BY id"));
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, REGION, () -> s3Tables.getTable(created.getArn(), "sales",
|
||||
"orders", REGION)).getMetadataLocation().contains("/metadata/"));
|
||||
// With no Database, Athena's context database is "default", which the bucket does not have.
|
||||
run(catalogName, null, "SELECT count(*) FROM \"" + catalogName + "\".sales.orders");
|
||||
for (String maintenance : List.of("VACUUM orders", "OPTIMIZE orders REWRITE DATA USING BIN_PACK")) {
|
||||
QueryExecution refused = execute(catalogName, "sales", maintenance);
|
||||
assertEquals(QueryExecutionState.FAILED, refused.getStatus().getState(), maintenance);
|
||||
assertTrue(refused.getStatus().getStateChangeReason().contains("not supported on S3 Tables"),
|
||||
refused.getStatus().getStateChangeReason());
|
||||
}
|
||||
|
||||
run(catalogName, "sales", "DROP TABLE orders");
|
||||
assertTrue(RequestScopes.callAs(ACCOUNT, REGION,
|
||||
() -> s3Tables.listTables(created.getArn(), "sales", null, REGION)).isEmpty());
|
||||
} finally {
|
||||
RequestScopes.runAs(ACCOUNT, REGION, () -> {
|
||||
catalogs.deleteCatalog("s3tablescatalog", REGION);
|
||||
for (Namespace namespace : s3Tables.listNamespaces(created.getArn(), null, REGION)) {
|
||||
for (S3Table table : s3Tables.listTables(created.getArn(), namespace.getName(), null, REGION)) {
|
||||
s3Tables.deleteTable(created.getArn(), namespace.getName(), table.getName(), REGION);
|
||||
}
|
||||
s3Tables.deleteNamespace(created.getArn(), namespace.getName(), REGION);
|
||||
}
|
||||
s3Tables.deleteTableBucket(created.getArn(), REGION);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void run(String sql) {
|
||||
QueryExecutionContext context = new QueryExecutionContext();
|
||||
context.setDatabase(database);
|
||||
String id = RequestScopes.callAs(ACCOUNT, REGION, () -> athena.startQueryExecution(sql, "primary", context,
|
||||
new ResultConfiguration("s3://" + bucket + "/results/")));
|
||||
QueryExecution execution = await().atMost(Duration.ofSeconds(60)).pollInterval(Duration.ofMillis(100))
|
||||
.until(() -> RequestScopes.callAs(ACCOUNT, REGION, () -> athena.getQueryExecution(id)),
|
||||
found -> found.getStatus().getState() != QueryExecutionState.RUNNING
|
||||
&& found.getStatus().getState() != QueryExecutionState.QUEUED);
|
||||
run(null, database, sql);
|
||||
}
|
||||
|
||||
/** Runs {@code sql} in the context catalog and database, failing the test unless it succeeds. */
|
||||
private void run(String catalogName, String contextDatabase, String sql) {
|
||||
QueryExecution execution = execute(catalogName, contextDatabase, sql);
|
||||
if (execution.getStatus().getState() != QueryExecutionState.SUCCEEDED) {
|
||||
fail(sql + " -> " + execution.getStatus().getState() + ": " + execution.getStatus().getStateChangeReason());
|
||||
}
|
||||
}
|
||||
|
||||
private QueryExecution execute(String catalogName, String contextDatabase, String sql) {
|
||||
QueryExecutionContext context = new QueryExecutionContext();
|
||||
context.setCatalog(catalogName);
|
||||
context.setDatabase(contextDatabase);
|
||||
String id = RequestScopes.callAs(ACCOUNT, REGION, () -> athena.startQueryExecution(sql, "primary", context,
|
||||
new ResultConfiguration("s3://" + bucket + "/results/")));
|
||||
return await().atMost(Duration.ofSeconds(60)).pollInterval(Duration.ofMillis(100))
|
||||
.until(() -> RequestScopes.callAs(ACCOUNT, REGION, () -> athena.getQueryExecution(id)),
|
||||
found -> found.getStatus().getState() != QueryExecutionState.RUNNING
|
||||
&& found.getStatus().getState() != QueryExecutionState.QUEUED);
|
||||
}
|
||||
|
||||
/** The rows of {@code sql} read in an S3 Tables context catalog, through the attachment Athena gives a query. */
|
||||
private List<Map<String, String>> s3TablesRows(String catalogName, String contextDatabase, String sql) {
|
||||
return rowsOf(RequestScopes.callAs(ACCOUNT, REGION, () -> {
|
||||
AthenaIcebergStatements.S3TablesAttachment attachment = icebergStatements.s3TablesAttachment(sql,
|
||||
catalogName, contextDatabase);
|
||||
try {
|
||||
return duck.query(TrinoSqlCompatibility.translate(sql), attachment.attachSql()
|
||||
+ TrinoSqlCompatibility.FUNCTION_MACROS + attachment.searchPathSql(), ACCOUNT);
|
||||
} finally {
|
||||
icebergStatements.finished(attachment);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private List<Map<String, String>> rows(String sql) {
|
||||
return RequestScopes.callAs(ACCOUNT, REGION, () -> duck.query(TrinoSqlCompatibility.translate(sql),
|
||||
TrinoSqlCompatibility.FUNCTION_MACROS + ddlBuilder.build(database, sql), ACCOUNT)).stream()
|
||||
.map(found -> {
|
||||
Map<String, String> row = new TreeMap<>();
|
||||
found.forEach((column, value) -> row.put(column, String.valueOf(value)));
|
||||
return row;
|
||||
}).toList();
|
||||
return rowsOf(RequestScopes.callAs(ACCOUNT, REGION, () -> duck.query(TrinoSqlCompatibility.translate(sql),
|
||||
TrinoSqlCompatibility.FUNCTION_MACROS + ddlBuilder.build(database, sql), ACCOUNT)));
|
||||
}
|
||||
|
||||
/** The engine's rows, each column's value as its text, columns in name order. */
|
||||
private static List<Map<String, String>> rowsOf(List<Map<String, Object>> found) {
|
||||
return found.stream().map(columns -> {
|
||||
Map<String, String> row = new TreeMap<>();
|
||||
columns.forEach((column, value) -> row.put(column, String.valueOf(value)));
|
||||
return row;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
private static Map<String, String> row(String... columnsAndValues) {
|
||||
|
||||
@@ -10,11 +10,13 @@ import io.floci.aws.services.floci.duck.FlociDuckClient;
|
||||
import io.floci.aws.services.glue.GlueService;
|
||||
import io.floci.aws.services.glue.iceberg.GlueIcebergCatalog;
|
||||
import io.floci.aws.services.glue.iceberg.GlueIcebergTokens;
|
||||
import io.floci.aws.services.glue.iceberg.IcebergRestException;
|
||||
import io.floci.aws.services.glue.model.Column;
|
||||
import io.floci.aws.services.glue.model.StorageDescriptor;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import io.floci.aws.services.s3.model.S3Object;
|
||||
import io.floci.aws.testing.InlineMocks;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -95,12 +97,13 @@ class AthenaIcebergStatementsTest {
|
||||
assertEquals("ev\"ents", target.table());
|
||||
assertEquals("Lake . \"Ev\"\"ents\"", insert.substring(target.start(), target.end()));
|
||||
|
||||
assertEquals(new AthenaIcebergStatements.Target(null, "events", 12, 18),
|
||||
assertEquals(new AthenaIcebergStatements.Target(null, "events", false, 12, 18),
|
||||
AthenaIcebergStatements.target("DELETE FROM events WHERE id = 1"));
|
||||
AthenaIcebergStatements.Target merge = AthenaIcebergStatements.target(
|
||||
"MERGE INTO awsdatacatalog.lake.events AS t USING src s ON t.id = s.id WHEN MATCHED THEN DELETE");
|
||||
assertEquals("lake", merge.database());
|
||||
assertEquals("events", merge.table());
|
||||
assertTrue(merge.catalogNamed());
|
||||
assertEquals("events", AthenaIcebergStatements.target("UPDATE events SET a = 1").table());
|
||||
|
||||
assertNull(AthenaIcebergStatements.target("UPDATE other_catalog.lake.events SET a = 1"));
|
||||
@@ -126,6 +129,24 @@ class AthenaIcebergStatementsTest {
|
||||
verify(tokens).revoke("tok'en");
|
||||
}
|
||||
|
||||
/**
|
||||
* In an S3 Tables context catalog, a write naming the default catalog goes through Glue's attachment, and one
|
||||
* naming no catalog is the context's, which the S3 Tables attachment serves.
|
||||
*/
|
||||
@Test
|
||||
void inAnS3TablesContextOnlyAWriteNamingTheDefaultCatalogGoesThroughGlue() {
|
||||
glueTable("lake", "events", true);
|
||||
when(tokens.mint(eq("111122223333"), eq("eu-west-1"), any())).thenReturn("tok");
|
||||
|
||||
assertNull(statements.write("INSERT INTO events VALUES (1)", "s3tablescatalog/bucket", "lake"));
|
||||
assertNull(statements.write("INSERT INTO lake.events VALUES (1)", "s3tablescatalog/bucket", "sales"));
|
||||
verify(tokens, never()).mint(anyString(), anyString(), any());
|
||||
AthenaIcebergStatements.IcebergWrite write = statements.write(
|
||||
"INSERT INTO awsdatacatalog.lake.events SELECT * FROM orders", "s3tablescatalog/bucket", "sales");
|
||||
assertEquals("INSERT INTO glue_iceberg.\"lake\".\"events\" SELECT * FROM orders", write.query());
|
||||
statements.finished(write);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWriteToAHiveTableOrAMissingOneIsLeftAlone() {
|
||||
glueTable("lake", "hive_events", false);
|
||||
@@ -137,6 +158,47 @@ class AthenaIcebergStatementsTest {
|
||||
verify(tokens, never()).mint(anyString(), anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anS3TablesCatalogIsAttachedUnderItsOwnNameAndIsTheSearchPathAsTheContext() {
|
||||
when(tokens.mint(eq("111122223333"), eq("eu-west-1"), any())).thenReturn("tok");
|
||||
assertEquals("lake", AthenaIcebergStatements.s3TablesBucket("S3TablesCatalog/lake"));
|
||||
assertNull(AthenaIcebergStatements.s3TablesBucket("AwsDataCatalog"));
|
||||
assertNull(statements.s3TablesAttachment("SELECT 1", "AwsDataCatalog", "default"));
|
||||
|
||||
AthenaIcebergStatements.S3TablesAttachment attachment = statements.s3TablesAttachment(
|
||||
"INSERT INTO \"s3tablescatalog/other\".ns.t SELECT * FROM orders", "s3tablescatalog/lake", "sales");
|
||||
|
||||
assertEquals("INSTALL iceberg; LOAD iceberg;\n"
|
||||
+ "ATTACH '111122223333:s3tablescatalog/lake' AS \"s3tablescatalog/lake\" (TYPE iceberg, ENDPOINT "
|
||||
+ "'http://floci:4566/iceberg', AUTHORIZATION_TYPE 'oauth2', TOKEN 'tok', ACCESS_DELEGATION_MODE 'none');\n"
|
||||
+ "ATTACH '111122223333:s3tablescatalog/other' AS \"s3tablescatalog/other\" (TYPE iceberg, ENDPOINT "
|
||||
+ "'http://floci:4566/iceberg', AUTHORIZATION_TYPE 'oauth2', TOKEN 'tok', ACCESS_DELEGATION_MODE 'none');\n",
|
||||
attachment.attachSql());
|
||||
assertEquals("SET search_path = '\"s3tablescatalog/lake\".\"sales\",memory.main';\n", attachment.searchPathSql());
|
||||
statements.finished(attachment);
|
||||
verify(tokens).revoke("tok");
|
||||
}
|
||||
|
||||
/**
|
||||
* A catalog only a comment or a string literal names is not attached, and a context namespace the bucket does
|
||||
* not have (Athena's {@code default} for a query naming none) is not the search path, which the engine would
|
||||
* refuse.
|
||||
*/
|
||||
@Test
|
||||
void anS3TablesCatalogNamedOnlyInACommentOrLiteralIsNotAttachedNorAMissingNamespaceSearched() {
|
||||
when(tokens.mint(eq("111122223333"), eq("eu-west-1"), any())).thenReturn("tok");
|
||||
assertNull(statements.s3TablesAttachment("SELECT '\"s3tablescatalog/a\".ns.t' -- \"s3tablescatalog/b\".ns.t\n"
|
||||
+ "/* \"s3tablescatalog/c\".ns.t */ FROM dual", "AwsDataCatalog", "default"));
|
||||
|
||||
IcebergRestException missing = InlineMocks.inlineMock(IcebergRestException.class);
|
||||
when(missing.type()).thenReturn("NoSuchNamespaceException");
|
||||
when(catalog.loadNamespace("s3tablescatalog:lake", "default")).thenThrow(missing);
|
||||
AthenaIcebergStatements.S3TablesAttachment attachment = statements.s3TablesAttachment(
|
||||
"SELECT * FROM \"s3tablescatalog/lake\".sales.orders", "s3tablescatalog/lake", "default");
|
||||
assertTrue(attachment.attachSql().contains("AS \"s3tablescatalog/lake\""), attachment.attachSql());
|
||||
assertEquals("", attachment.searchPathSql());
|
||||
}
|
||||
|
||||
// ─── CREATE TABLE ─────────────────────────────────────────────────────────────
|
||||
|
||||
private static AthenaHiveDdl.CreateIcebergTable create(boolean ifNotExists, Map<String, String> properties) {
|
||||
|
||||
Reference in new issue
Block a user