fix(athena): word GetQueryResults' refusals as Athena does by the query's state (#9454)

GetQueryResults refused the results of every query that had not succeeded
with "Query has not succeeded yet". Athena words the refusal by the
query's state: "Query has not yet finished. Current state: <state>" for
one still queued or running, "Query did not finish successfully. Final
query state: FAILED" for a failed one, and "Could not find results" for a
cancelled one, each an InvalidRequestException as before.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-25 12:08:41 -04:00
1 parent 4018e8d0b5
commit 42dadc4ed9
4 files changed
+64 -2

No files matched your search

+6
View File
@@ -104,6 +104,12 @@ started in another region, or one Athena holds none of, return
`InvalidRequestException` with `QueryExecution <id> was not found`. An execution
stored before Floci recorded each execution's region is answered in every region.
`GetQueryResults` on a query that has not succeeded returns
`InvalidRequestException` worded by its state, as AWS words it: `Query has not
yet finished. Current state: <state>` for one still queued or running, `Query did
not finish successfully. Final query state: FAILED` for a failed one, and `Could
not find results` for a cancelled one.
A workgroup reports what its request gave and AWS's defaults for the rest. With
no `ResultConfiguration`, it reports none (`primary` reports an empty one, as on
AWS); a query then names its own output location, and Floci writes results to
@@ -748,8 +748,9 @@ public class AthenaService {
public ResultSet getQueryResults(String region, String id) {
QueryExecution execution = getQueryExecution(region, id);
if (execution.getStatus().getState() != QueryExecutionState.SUCCEEDED) {
throw new AwsException("InvalidRequestException", "Query has not succeeded yet", 400);
QueryExecutionState state = execution.getStatus().getState();
if (state != QueryExecutionState.SUCCEEDED) {
throw new AwsException("InvalidRequestException", unreadResults(state), 400);
}
// DDL rows (SHOW PARTITIONS) come from the Glue catalog, not the query engine, so mock mode keeps them.
@@ -763,6 +764,19 @@ public class AthenaService {
return readResultsFromS3(execution.getResultConfiguration().getOutputLocation(), id, columnTypes);
}
/**
* Why Athena refuses the results of a query in the state, which is not {@code SUCCEEDED}, as AWS words it: one still
* queued or running has not finished, a failed one did not finish successfully, and a cancelled one has none.
*/
private static String unreadResults(QueryExecutionState state) {
return switch (state) {
case QUEUED, RUNNING -> "Query has not yet finished. Current state: " + state.name();
case FAILED -> "Query did not finish successfully. Final query state: " + state.name();
case CANCELLED -> "Could not find results";
case SUCCEEDED -> throw new IllegalStateException("a succeeded query's results are read, not refused");
};
}
// ── private helpers ───────────────────────────────────────────────────────
private CreateDatabaseDdl parseCreateDatabase(String query) {
@@ -517,6 +517,17 @@ class AthenaIntegrationTest {
.body("QueryExecution.Status.StateChangeReason", equalTo("Invalid database properties"))
.body("QueryExecution.StatementType", equalTo("DDL"))
.body("QueryExecution.ResultConfiguration", nullValue());
given()
.header("X-Amz-Target", "AmazonAthena.GetQueryResults")
.contentType(CONTENT_TYPE)
.body("{ \"QueryExecutionId\": \"" + id + "\" }")
.when()
.post("/")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidRequestException"))
.body("message", equalTo("Query did not finish successfully. Final query state: FAILED"));
}
@Test
@@ -241,6 +241,37 @@ class AthenaServiceTest {
"a late stop must not cancel a failed query");
}
@Test
void getQueryResultsRefusesAnUnsucceededQueryAsAthenaWordsIt() throws Exception {
CountDownLatch started = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
doAnswer(invocation -> {
started.countDown();
release.await(10, TimeUnit.SECONDS);
return null;
}).when(duckClient).execute(anyString(), any(), any(), any());
String id = service.startQueryExecution("SELECT 1", "primary", null, null);
assertTrue(started.await(10, TimeUnit.SECONDS), "the worker never started");
assertEquals("Query has not yet finished. Current state: RUNNING", resultsRefusal(id));
service.stopQueryExecution(REGION, id);
release.countDown();
assertEquals("Could not find results", resultsRefusal(id));
doAnswer(invocation -> {
throw new IllegalStateException("duck failed");
}).when(duckClient).execute(anyString(), any(), any(), any());
String failed = service.startQueryExecution("SELECT 1", "primary", null, null);
awaitState(failed, QueryExecutionState.FAILED);
assertEquals("Query did not finish successfully. Final query state: FAILED", resultsRefusal(failed));
}
private String resultsRefusal(String id) {
AwsException refused = assertThrows(AwsException.class, () -> service.getQueryResults(REGION, id));
assertEquals("InvalidRequestException", refused.getErrorCode());
return refused.getMessage();
}
@Test
void getQueryResultsReportsTheSqlTypeOfEachColumn() throws Exception {
String query = "SELECT CAST(1 AS BIGINT) AS n, CAST(1.5 AS DOUBLE) AS d, true AS b, "