Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(athena): save the query result file at the reported OutputLocation (#2430)

mishukdutta-czd6805ae350d0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b44d07a..e361a3d1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **Athena — the result file is saved at the reported `OutputLocation`** — a query's `<id>.csv` and `<id>.csv.metadata` are now saved directly under the result location, at the path that `GetQueryExecution` reports, as AWS does. Before, they were saved one folder too deep, at `<location>/<id>.csv/<id>.csv`, so a client that read the reported path got `NoSuchKey`.
- **Athena — an enforced workgroup overrides the client-side result location** — when a workgroup has `EnforceWorkGroupConfiguration: true`, `StartQueryExecution` now writes results to the workgroup's `OutputLocation`, even when the request gives its own `ResultConfiguration.OutputLocation`, as AWS does. Before, the request's location was used.
- **Lambda — a partial ARN as the `FunctionName`** — `123456789012:function:my-function[:qualifier]` resolved to the account ID as the function name, so `Invoke` and the other function operations failed with `Function not found: ...:function:123456789012`; it now resolves as the full ARN in the caller's region, as on AWS, and gives the same result as that full ARN.
## [1.5.33] — 2026-10-26
diff --git a/ministack/services/athena.py b/ministack/services/athena.py
index 52fd9fb1..ba61f96a 100644
--- a/ministack/services/athena.py
+++ b/ministack/services/athena.py
@@ -1298,11 +1298,8 @@ async def _save_query_results(query_id):
output_location = execution["ResultConfiguration"]["OutputLocation"]
p = urlparse(output_location)
bucket_name = p.netloc
- key_prefix = p.path.lstrip("/").rstrip("/")
- # Athena writes <id>.csv and <id>.csv.metadata under the OutputLocation
- # prefix. If the prefix is empty (output_location == "s3://bucket/"),
- # the files land at bucket root.
- csv_key = f"{key_prefix}/{query_id}.csv" if key_prefix else f"{query_id}.csv"
+ # OutputLocation already ends in <id>.csv
+ csv_key = p.path.lstrip("/")
meta_key = f"{csv_key}.metadata"
# Write directly to the S3 service's in-memory store via the public
diff --git a/tests/test_athena.py b/tests/test_athena.py
index 1d219e50..d141311a 100644
--- a/tests/test_athena.py
+++ b/tests/test_athena.py
@@ -906,6 +906,27 @@ def test_athena_workgroup_reports_engine_version(athena):
assert version["EffectiveEngineVersion"] == "Athena engine version 3"
+def test_athena_result_file_is_at_reported_output_location(athena, s3):
+ bucket = f"athena-out-{_uuid_mod.uuid4().hex[:8]}"
+ s3.create_bucket(Bucket=bucket)
+ qid = athena.start_query_execution(
+ QueryString="SELECT 1 AS n",
+ ResultConfiguration={"OutputLocation": f"s3://{bucket}/out/"},
+ )["QueryExecutionId"]
+ for _ in range(30):
+ execution = athena.get_query_execution(QueryExecutionId=qid)["QueryExecution"]
+ if execution["Status"]["State"] in ("SUCCEEDED", "FAILED", "CANCELLED"):
+ break
+ time.sleep(0.1)
+ assert execution["Status"]["State"] == "SUCCEEDED", execution["Status"]
+ assert execution["ResultConfiguration"]["OutputLocation"] == f"s3://{bucket}/out/{qid}.csv"
+
+ keys = sorted(o["Key"] for o in s3.list_objects_v2(Bucket=bucket)["Contents"])
+ assert keys == [f"out/{qid}.csv", f"out/{qid}.csv.metadata"]
+ body = s3.get_object(Bucket=bucket, Key=f"out/{qid}.csv")["Body"].read().decode()
+ assert body.splitlines() == ["n", "1"]
+
+
def _run_to_completion(athena, query, database):
query_id = athena.start_query_execution(
QueryString=query, QueryExecutionContext={"Database": database},