mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(athena): register every Glue database as a DuckDB schema so <db>.<table> resolves (#2993)
* feat(athena): extract GlueViewDdlBuilder with schema qualification and aliases * feat(athena): wire GlueViewDdlBuilder into AthenaService * docs(athena): document Glue database schema registration and aliases * fix(athena): propagate getDatabases failure and harden DDL table quoting * fix(athena): skip Glue tables with nonexistent S3 bucket and cleanup AthenaTest * fix(athena): avoid redundant table fetch and add qualified query compatibility test * fix(athena): do not skip views for tables with nonexistent S3 bucket
This commit is contained in:
1 parent
2e303e2a0b
commit
0ab9979edf
5 files changed
+545
-101
No files matched your search
@@ -1,12 +1,15 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.athena.AthenaClient;
|
||||
import software.amazon.awssdk.services.athena.model.*;
|
||||
import software.amazon.awssdk.services.glue.GlueClient;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -152,22 +155,30 @@ class AthenaTest {
|
||||
)
|
||||
);
|
||||
|
||||
GetTableMetadataResponse response = athena.getTableMetadata(
|
||||
GetTableMetadataRequest.builder()
|
||||
.catalogName("AwsDataCatalog")
|
||||
.databaseName(dbName)
|
||||
.tableName(tableName)
|
||||
.build()
|
||||
);
|
||||
try {
|
||||
GetTableMetadataResponse response = athena.getTableMetadata(
|
||||
GetTableMetadataRequest.builder()
|
||||
.catalogName("AwsDataCatalog")
|
||||
.databaseName(dbName)
|
||||
.tableName(tableName)
|
||||
.build()
|
||||
);
|
||||
|
||||
assertThat(response.tableMetadata().createTime())
|
||||
.as("createTime must be parseable by the AWS SDK")
|
||||
.isNotNull();
|
||||
assertThat(response.tableMetadata().lastAccessTime())
|
||||
.as("lastAccessTime must be parseable by the AWS SDK")
|
||||
.isNotNull();
|
||||
|
||||
glue.close();
|
||||
assertThat(response.tableMetadata().createTime())
|
||||
.as("createTime must be parseable by the AWS SDK")
|
||||
.isNotNull();
|
||||
assertThat(response.tableMetadata().lastAccessTime())
|
||||
.as("lastAccessTime must be parseable by the AWS SDK")
|
||||
.isNotNull();
|
||||
} finally {
|
||||
try {
|
||||
glue.deleteTable(r -> r.databaseName(dbName).name(tableName));
|
||||
glue.deleteDatabase(r -> r.name(dbName));
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort test cleanup
|
||||
}
|
||||
glue.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -178,31 +189,141 @@ class AthenaTest {
|
||||
String location = "s3://test-bucket/" + dbName + "/";
|
||||
|
||||
try (GlueClient glue = TestFixtures.glueClient()) {
|
||||
StartQueryExecutionResponse started = athena.startQueryExecution(
|
||||
StartQueryExecutionRequest.builder()
|
||||
.queryString("CREATE DATABASE IF NOT EXISTS " + dbName
|
||||
+ " LOCATION '" + location + "'")
|
||||
.workGroup("primary")
|
||||
.build());
|
||||
try {
|
||||
StartQueryExecutionResponse started = athena.startQueryExecution(
|
||||
StartQueryExecutionRequest.builder()
|
||||
.queryString("CREATE DATABASE IF NOT EXISTS " + dbName
|
||||
+ " LOCATION '" + location + "'")
|
||||
.workGroup("primary")
|
||||
.build());
|
||||
|
||||
QueryExecutionStatus status = TestFixtures.awaitAthenaQueryTerminal(
|
||||
athena, started.queryExecutionId(), Duration.ofSeconds(60));
|
||||
assertThat(status.state())
|
||||
.as("Athena DDL did not succeed: %s", status.stateChangeReason())
|
||||
.isEqualTo(QueryExecutionState.SUCCEEDED);
|
||||
QueryExecutionStatus status = TestFixtures.awaitAthenaQueryTerminal(
|
||||
athena, started.queryExecutionId(), Duration.ofSeconds(60));
|
||||
assertThat(status.state())
|
||||
.as("Athena DDL did not succeed: %s", status.stateChangeReason())
|
||||
.isEqualTo(QueryExecutionState.SUCCEEDED);
|
||||
|
||||
QueryExecution execution = athena.getQueryExecution(r -> r
|
||||
.queryExecutionId(started.queryExecutionId())).queryExecution();
|
||||
assertThat(execution.statementType()).isEqualTo(StatementType.DDL);
|
||||
assertThat(execution.resultConfiguration()).isNull();
|
||||
QueryExecution execution = athena.getQueryExecution(r -> r
|
||||
.queryExecutionId(started.queryExecutionId())).queryExecution();
|
||||
assertThat(execution.statementType()).isEqualTo(StatementType.DDL);
|
||||
assertThat(execution.resultConfiguration()).isNull();
|
||||
|
||||
GetQueryResultsResponse results = athena.getQueryResults(r -> r
|
||||
.queryExecutionId(started.queryExecutionId()));
|
||||
assertThat(results.resultSet().rows()).isEmpty();
|
||||
assertThat(results.resultSet().resultSetMetadata().columnInfo()).isEmpty();
|
||||
GetQueryResultsResponse results = athena.getQueryResults(r -> r
|
||||
.queryExecutionId(started.queryExecutionId()));
|
||||
assertThat(results.resultSet().rows()).isEmpty();
|
||||
assertThat(results.resultSet().resultSetMetadata().columnInfo()).isEmpty();
|
||||
|
||||
assertThat(glue.getDatabase(r -> r.name(dbName)).database().locationUri())
|
||||
.isEqualTo(location);
|
||||
assertThat(glue.getDatabase(r -> r.name(dbName)).database().locationUri())
|
||||
.isEqualTo(location);
|
||||
} finally {
|
||||
try {
|
||||
glue.deleteDatabase(r -> r.name(dbName));
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort test cleanup
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reproduces issue #2859: Athena queries referencing tables with qualified names (e.g.
|
||||
* {@code FROM shop.orders}) failed with "schema does not exist" when DuckDB schemas were not
|
||||
* registered for Glue databases.
|
||||
*/
|
||||
@Test
|
||||
@Order(9)
|
||||
@DisplayName("Query table with qualified database schema resolves and executes")
|
||||
void queryTableWithQualifiedDatabaseSchemaResolves() throws InterruptedException {
|
||||
String uniqueSuffix = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
|
||||
String dbName = "shop_" + uniqueSuffix;
|
||||
String tableName = "orders";
|
||||
String bucketName = "athena-compat-" + uniqueSuffix;
|
||||
String dataPrefix = "orders/";
|
||||
String location = "s3://" + bucketName + "/" + dataPrefix;
|
||||
|
||||
try (GlueClient glue = TestFixtures.glueClient();
|
||||
S3Client s3 = TestFixtures.s3Client()) {
|
||||
|
||||
s3.createBucket(r -> r.bucket(bucketName));
|
||||
s3.putObject(r -> r.bucket(bucketName).key(dataPrefix + "orders.json"),
|
||||
RequestBody.fromString(
|
||||
"{\"customer\":\"ana\",\"amount\":10}\n{\"customer\":\"leo\",\"amount\":7}\n"));
|
||||
|
||||
glue.createDatabase(r -> r.databaseInput(i -> i.name(dbName)));
|
||||
glue.createTable(r -> r
|
||||
.databaseName(dbName)
|
||||
.tableInput(t -> t
|
||||
.name(tableName)
|
||||
.tableType("EXTERNAL_TABLE")
|
||||
.storageDescriptor(sd -> sd
|
||||
.location(location)
|
||||
.inputFormat("org.apache.hadoop.mapred.TextInputFormat")
|
||||
.outputFormat("org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat")
|
||||
.serdeInfo(s -> s
|
||||
.serializationLibrary("org.openx.data.jsonserde.JsonSerDe")
|
||||
.parameters(Map.of("serialization.format", "1")))
|
||||
.columns(
|
||||
software.amazon.awssdk.services.glue.model.Column.builder().name("customer").type("string").build(),
|
||||
software.amazon.awssdk.services.glue.model.Column.builder().name("amount").type("int").build()
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
try {
|
||||
// Qualified query without context database (issue #2859 exact shape)
|
||||
StartQueryExecutionResponse started = athena.startQueryExecution(
|
||||
StartQueryExecutionRequest.builder()
|
||||
.queryString("SELECT customer, sum(amount) as total FROM " + dbName + "." + tableName
|
||||
+ " GROUP BY customer ORDER BY customer")
|
||||
.workGroup("primary")
|
||||
.resultConfiguration(ResultConfiguration.builder()
|
||||
.outputLocation("s3://" + bucketName + "/results/")
|
||||
.build())
|
||||
.build());
|
||||
|
||||
QueryExecutionStatus status = TestFixtures.awaitAthenaQueryTerminal(
|
||||
athena, started.queryExecutionId(), Duration.ofSeconds(60));
|
||||
assertThat(status.state())
|
||||
.as("Athena qualified query did not succeed: %s", status.stateChangeReason())
|
||||
.isEqualTo(QueryExecutionState.SUCCEEDED);
|
||||
|
||||
GetQueryResultsResponse results = athena.getQueryResults(r -> r
|
||||
.queryExecutionId(started.queryExecutionId()));
|
||||
assertThat(results.resultSet().rows()).hasSizeGreaterThanOrEqualTo(3);
|
||||
|
||||
// Unqualified query with context database (context alias verification)
|
||||
StartQueryExecutionResponse startedUnqualified = athena.startQueryExecution(
|
||||
StartQueryExecutionRequest.builder()
|
||||
.queryString("SELECT customer, sum(amount) as total FROM " + tableName
|
||||
+ " GROUP BY customer ORDER BY customer")
|
||||
.queryExecutionContext(QueryExecutionContext.builder().database(dbName).build())
|
||||
.workGroup("primary")
|
||||
.resultConfiguration(ResultConfiguration.builder()
|
||||
.outputLocation("s3://" + bucketName + "/results/")
|
||||
.build())
|
||||
.build());
|
||||
|
||||
QueryExecutionStatus statusUnqualified = TestFixtures.awaitAthenaQueryTerminal(
|
||||
athena, startedUnqualified.queryExecutionId(), Duration.ofSeconds(60));
|
||||
assertThat(statusUnqualified.state())
|
||||
.as("Athena unqualified query did not succeed: %s", statusUnqualified.stateChangeReason())
|
||||
.isEqualTo(QueryExecutionState.SUCCEEDED);
|
||||
} finally {
|
||||
try {
|
||||
glue.deleteTable(r -> r.databaseName(dbName).name(tableName));
|
||||
glue.deleteDatabase(r -> r.name(dbName));
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort test cleanup
|
||||
}
|
||||
try {
|
||||
s3.listObjectsV2(r -> r.bucket(bucketName)).contents().forEach(obj ->
|
||||
s3.deleteObject(r -> r.bucket(bucketName).key(obj.key())));
|
||||
s3.deleteBucket(r -> r.bucket(bucketName));
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort test cleanup
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user