fix(athena): report a HUGEINT result column as bigint (#8254)

This commit is contained in:
Ghassan Gedeon Achi authored and GitHub committed 2026-11-13 23:05:50 -04:00
1 parent ea4427ca58
commit 8185a340c4
3 files changed
+21 -2

No files matched your search

+1 -1
View File
@@ -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 "<schema>"`) and creates qualified views (`CREATE OR REPLACE VIEW "<db>"."<table>"`) 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 "<table>"` 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
@@ -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";
@@ -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<String> 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());
}
}