diff --git a/docs/services/athena.md b/docs/services/athena.md index 5c06cf1e52..01f75e0a35 100644 --- a/docs/services/athena.md +++ b/docs/services/athena.md @@ -114,7 +114,7 @@ results storage. 1. **Lazy sidecar start**: On the first `StartQueryExecution` call, Floci checks for a local `floci/floci-duck:latest` image and starts the container. Subsequent queries reuse the running container. 2. **Glue DDL injection**: Floci registers every Glue database as a DuckDB schema (`CREATE SCHEMA IF NOT EXISTS ""`) and creates qualified views (`CREATE OR REPLACE VIEW "".""`) mapping each table to its S3 location via DuckDB's `read_parquet`, `read_json_auto`, or `read_csv_auto` functions, chosen based on the table's `InputFormat` or SerDe serialization library. In addition, tables in the query context database receive unqualified view aliases (`CREATE OR REPLACE VIEW "
"` in DuckDB's default `main` schema), so queries can reference tables as both `FROM db.table` and `FROM table`. 3. **Query execution**: The user's SQL is wrapped in `COPY (...) TO 's3://...' (FORMAT CSV, HEADER)` and executed. Results are written directly to the output S3 path. -4. **Results retrieval**: `GetQueryResults` reads the CSV back from S3 and returns it in the standard Athena `ResultSet` shape. Each column's `Type` comes from DuckDB describing the query, mapped to the Athena name (`bigint`, `integer`, `double`, `boolean`, `date`, `time`, `timestamp`, `decimal` and so on), with `Precision` and `Scale` for a decimal; a column whose type has no Athena name, such as an interval, and every column of a query DuckDB cannot describe are reported as `varchar`. +4. **Results retrieval**: `GetQueryResults` reads the CSV back from S3 and returns it in the standard Athena `ResultSet` shape. Each column's `Type` comes from DuckDB describing the query, mapped to the Athena name (`bigint`, `integer`, `double`, `boolean`, `date`, `time`, `timestamp`, `decimal` and so on), with `Precision` and `Scale` for a decimal; a column whose type has no Athena name, such as an interval, and every column of a query DuckDB cannot describe are reported as `varchar`. DuckDB widens `sum` and `count_if` over integers to `HUGEINT`, which is reported as `bigint`, Trino's type for them; a sum past `bigint`'s range, which Athena fails with `NUMERIC_VALUE_OUT_OF_RANGE`, is returned instead. ## Format inference diff --git a/src/main/java/io/floci/aws/services/athena/AthenaService.java b/src/main/java/io/floci/aws/services/athena/AthenaService.java index 1cee2b04f6..edee054c00 100644 --- a/src/main/java/io/floci/aws/services/athena/AthenaService.java +++ b/src/main/java/io/floci/aws/services/athena/AthenaService.java @@ -1150,7 +1150,11 @@ public class AthenaService { } int parameters = type.indexOf('('); return switch (parameters < 0 ? type : type.substring(0, parameters)) { - case "BIGINT" -> "bigint"; + // DuckDB widens COUNT_IF and SUM over a BIGINT to HUGEINT, where Trino keeps BIGINT. + // TODO: Athena (engine v3) fails a sum past BIGINT's range with NUMERIC_VALUE_OUT_OF_RANGE; + // DuckDB's HUGEINT holds it, so floci returns the out-of-range value typed bigint instead. + // Casting HUGEINT result columns back to BIGINT would make DuckDB fail the query too. + case "BIGINT", "HUGEINT" -> "bigint"; case "INTEGER" -> "integer"; case "SMALLINT" -> "smallint"; case "TINYINT" -> "tinyint"; diff --git a/src/test/java/io/floci/aws/services/athena/AthenaColumnTypesSidecarIntegrationTest.java b/src/test/java/io/floci/aws/services/athena/AthenaColumnTypesSidecarIntegrationTest.java index a68a7a3bc1..f76285a901 100644 --- a/src/test/java/io/floci/aws/services/athena/AthenaColumnTypesSidecarIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/athena/AthenaColumnTypesSidecarIntegrationTest.java @@ -41,4 +41,19 @@ class AthenaColumnTypesSidecarIntegrationTest { assertEquals(List.of("bigint", "double", "boolean", "date", "decimal", "varchar", "time", "timestamp", "timestamp with time zone"), types); } + + /** + * Trino returns BIGINT for {@code count_if} and for {@code sum} over a BIGINT; DuckDB widens both + * to HUGEINT, which Athena clients must still see as bigint. + */ + @Test + void aggregatesDuckDbWidensToHugeintAreReportedAsBigint() { + List duckTypes = duck.query("DESCRIBE SELECT count_if(true) AS c, sum(CAST(1 AS BIGINT)) AS s", null) + .stream() + .map(column -> String.valueOf(column.get("column_type"))) + .toList(); + + assertEquals(List.of("HUGEINT", "HUGEINT"), duckTypes); + assertEquals(List.of("bigint", "bigint"), duckTypes.stream().map(AthenaService::athenaType).toList()); + } }