View a markdown version of this page

SQL differences from Athena v3 - Amazon Athena

SQL differences from Athena v3

Amazon Athena runs queries on DuckDB, which uses its own SQL dialect, while Athena engine version 3 is based on Trino's SQL dialect. Because Athena does not translate between the engines, not all engine version 3 SQL runs on DuckDB as is: queries that use standard SQL often run unchanged, but others need changes because some statements are spelled differently, some functions have other names, and some expressions return a different result without an error. This page highlights the common query changes to make when you move queries from Athena engine version 3 to DuckDB. To translate your existing queries automatically, use the migration agent (see Migrate from Athena engine version 3 to DuckDB).

A typical rewrite

In engine version 3 you might write the following query.

SELECT date_format(order_date, '%Y-%m') AS order_month, approx_distinct(customer) AS customers, sum(amount) AS revenue FROM sales GROUP BY 1 ORDER BY order_month

On DuckDB, date_format and approx_distinct are not DuckDB functions, so the query fails. The DuckDB version uses strftime, approx_count_distinct, and GROUP BY ALL.

SELECT strftime(order_date, '%Y-%m') AS order_month, approx_count_distinct(customer) AS customers, sum(amount) AS revenue FROM sales GROUP BY ALL ORDER BY order_month

Keep these points in mind when you move a workload:

  • Compare results, not only success. Run each migrated query in both engines on the same data and compare the rows that it returns.

  • You cannot switch on engine version 3 behavior. Each query runs on a fresh engine whose settings are locked, so SET statements are not accepted, including the ones that some DuckDB error messages suggest.

  • Keep an engine version 3 workgroup for statements this Preview does not support, such as OPTIMIZE, VACUUM, materialized views, and views over Iceberg tables. Both engines use the same AWS Glue Data Catalog, so the same tables are visible from both.

Statement mapping

Athena engine version 3 DuckDB
CREATE DATABASE db CREATE SCHEMA db
DROP DATABASE db CASCADE DROP SCHEMA IF EXISTS db CASCADE
SHOW DATABASES SHOW SCHEMAS
ALTER DATABASE db SET DBPROPERTIES ('key' = 'value') ALTER SCHEMA db SET (key = 'value')
CREATE EXTERNAL TABLE t (...) STORED AS PARQUET LOCATION '...' CREATE TABLE t (...) WITH (type = 'HIVE', format = 'parquet', location = 's3://...')
CREATE TABLE t (...) LOCATION '...' TBLPROPERTIES ('table_type' = 'ICEBERG') CREATE TABLE t (...) WITH (type = 'ICEBERG', location = 's3://...')
CREATE TABLE t (...) PARTITIONED BY (...) Include partition columns in the main column list, then use PARTITIONED BY (partition_column_name [, ...]) without data types.
CREATE TABLE t WITH (format='PARQUET', external_location='...') AS SELECT ... CREATE TABLE t WITH (type='HIVE', format='parquet', location='s3://...') AS SELECT ...
DESCRIBE EXTENDED t, DESCRIBE FORMATTED t Not supported. Hive: SELECT * EXCLUDE (response) FROM glue_get_table_response('[catalog.database.]table'). Iceberg: DESCRIBE and iceberg_table_properties('[catalog.database.]table'). These metadata interfaces are not substitutes for Hive's formatted output.
SHOW TABLES IN db SHOW TABLES FROM db (result column is name, not tab_name)
SHOW TBLPROPERTIES t Hive: glue_get_table_response. Iceberg: iceberg_table_properties
SHOW CREATE TABLE t Not supported. Use DESCRIBE and the property functions.
SHOW PARTITIONS t Hive: SELECT * FROM glue_partitions('catalog.database.table'). Iceberg: SELECT * FROM iceberg_partitions('db.t')
MSCK REPAIR TABLE t Not supported. Register partitions with ALTER TABLE ... ADD PARTITION.
ALTER TABLE t RENAME TO t2 Not supported.
ALTER TABLE t ADD COLUMNS (...) ALTER TABLE t ADD COLUMN c type (one table-wide column addition per statement; partition-specific additions are not supported)
ALTER TABLE t CHANGE COLUMN c c BIGINT ALTER TABLE t ALTER COLUMN c TYPE BIGINT (widening only)
ALTER TABLE t SET TBLPROPERTIES (...) ALTER TABLE t SET (...)
ALTER TABLE t UNSET TBLPROPERTIES (...) Not supported. Run it in an engine version 3 workgroup.
ALTER TABLE t SET LOCATION '...' CALL glue_set_table_location('db.t', 's3://...') (Hive only)
UNLOAD (SELECT ...) TO '...' WITH (format='PARQUET') COPY (SELECT ...) TO 's3://.../file.parquet' (FORMAT parquet)
CREATE VIEW v AS ... CREATE VIEW v AS ... over Hive tables only. See Work with views.
ALTER VIEW v RENAME TO w Not supported. Run CREATE VIEW w AS ... with the same query, then DROP VIEW v.
SHOW VIEWS IN db Not supported. To list views, see List views.
SHOW CREATE VIEW v Not supported. Use DESCRIBE v to see the columns.
OPTIMIZE t , VACUUM t Not supported. Use an engine version 3 workgroup.
SELECT * FROM t FOR VERSION AS OF <snapshot_id> SELECT * FROM t AT (VERSION => <snapshot_id>)
SELECT * FROM t FOR TIMESTAMP AS OF TIMESTAMP '...' SELECT * FROM t AT (TIMESTAMP => TIMESTAMP '...')
SELECT * FROM "t$snapshots" SELECT ... FROM iceberg_snapshots('db.t')
PREPARE , EXECUTE , USE db Not supported. Set the database in the query execution context.

When you use a table alias with time travel, put the alias before AT, for example SELECT * FROM orders o AT (TIMESTAMP => TIMESTAMP '2026-11-05 00:00:00'). An alias after AT (...) is a syntax error.

List tables and schemas

To list tables, use SHOW TABLES, SHOW TABLES FROM database_name, or SHOW TABLES FROM catalog_name.database_name. Double-quoted database names are accepted. Replace engine version 3 SHOW TABLES IN with FROM. The engine version 3 LIKE and bare regular-expression filtering forms are not supported. To filter names, query duckdb_tables() with a predicate:

SELECT * FROM duckdb_tables() WHERE schema_name = 'database_name' AND table_name LIKE 'pattern';

duckdb_tables() does not list views. To list views, see the List views section.

Engine version 3 returns SHOW TABLES as a Hive utility result with the column tab_name and no header row. DuckDB returns a normal result with the column name and a header row. Clients that depend on engine version 3 result metadata must account for this difference.

SHOW SCHEMAS replaces SHOW DATABASES. It returns one schema per row instead of the aggregated Hive utility result. SHOW SCHEMAS IN catalog_name and SHOW SCHEMAS FROM catalog_name are not supported. To filter schema names, use:

SELECT * FROM duckdb_schemas() WHERE schema_name LIKE '%pattern%';

Inspect table metadata

Use DESCRIBE TABLE table_name for a table's columns and data types in place of SHOW COLUMNS. DESCRIBE EXTENDED and DESCRIBE FORMATTED are not supported. For Hive table details such as the location, SerDe, table parameters, and partition keys, query AWS Glue metadata with SELECT * EXCLUDE (response) FROM glue_get_table_response('[catalog.database.]table'). glue_partitions('catalog.database.table') returns partition metadata, including partition names and locations. For Iceberg table properties, use iceberg_table_properties('[catalog.database.]table'). These metadata interfaces are not substitutes for Hive's formatted output.

SHOW CREATE TABLE is not supported. You can inspect the engine-generated SQL with:

SELECT sql FROM duckdb_tables() WHERE table_name = 'table_name';

The function call requires parentheses. The sql field omits the table location, file format, table type, and table properties, and it can differ from the original customer DDL.

Add columns

Each ALTER TABLE ... ADD COLUMN adds one column to the whole table. Use a separate statement for each column. You cannot add a column only to one partition; use an engine version 3 workgroup when you need partition-specific schemas.

Differences that change results without an error

These differences matter most, because a query runs without an error and returns a different answer. You cannot change this behavior with engine settings.

Expression Engine version 3 DuckDB To get the engine version 3 result
5 / 2 (integers) 2 2.5 5 // 2
1 / 0 (integers) Query fails Infinity x / NULLIF(y, 0) or TRY(x // y)
CAST(1.23456 AS DECIMAL) 1, type decimal(38,0) 1.235, type DECIMAL(18,3) CAST(x AS DECIMAL(38,0))
CAST(1 AS DECIMAL(38,10)) / 3 * 3 (decimals) 0.9999999999, type decimal(38,10) 1.0, type DOUBLE decimal_division(x, y, s). See Exact decimal division.
CAST(2.5E0 AS BIGINT) (DOUBLE or REAL) 3 2 (halves round to even) CAST(round(x) AS BIGINT). The same applies to INTEGER, SMALLINT, and TINYINT.
try_cast('12 ' AS INTEGER), try_cast('1e2' AS INTEGER) NULL 12, 100 CASE WHEN regexp_full_match(s, '[+-]?[0-9]+') THEN try_cast(s AS INTEGER) END. This accepts ASCII digits 0-9 only. Engine version 3 also reads other digits, such as '12'.
CAST('[1,2]' AS JSON) (text) "[1,2]" (a JSON string) [1,2] (the text is parsed as JSON) to_json(s)
CAST(CAST('1.5000' AS DECIMAL(10,4)) AS JSON) 1.5000 1.5 to_json(CAST(x AS DECIMAL(16,4))). See Decimals in JSON.
regexp_replace('a1b2', '[0-9]', 'X') aXbX aXb2 (first match only) regexp_replace(s, p, r, 'g')
regexp_extract('abc', 'x') (no match) NULL '' NULLIF(regexp_extract(s, p), '')
concat('a', NULL, 'b') NULL ab (NULL skipped) Use the string concatenation operator, which returns NULL for a NULL argument
greatest(1, NULL, 3) NULL 3 (NULL skipped) Guard with a CASE on NULL
date_diff('day', a, b) Whole days elapsed Day boundaries crossed date_sub('day', a, b)
date_trunc('month', DATE '...') DATE TIMESTAMP CAST(date_trunc(...) AS DATE)
ts AT TIME ZONE 'America/Los_Angeles' Value read as UTC Value read as local time CAST(ts AS TIMESTAMPTZ) AT TIME ZONE 'America/Los_Angeles'
arr[0] Query fails NULL (index starts at 1) Check the index first
TABLESAMPLE SYSTEM (50) About 50 percent Exactly 50 rows Add PERCENT

These rewrites change only the expressions you write. A cast that the engine adds for you, for example when it inserts a DOUBLE value into a BIGINT column, keeps the DuckDB behavior.

Exact decimal division

The / operator converts DECIMAL values to DOUBLE. To divide decimals exactly and get a DECIMAL result, use decimal_division(x, y, s). Set s to the larger of the two input scales, which is the scale that engine version 3 returns. An INTEGER or BIGINT input has scale 0.

-- Engine version 3: 16.1290204579, type decimal(38,10) SELECT CAST('100.00' AS DECIMAL(38,10)) * 1234567 * 100 / (7654321 * 100); -- DuckDB: same value and type SELECT decimal_division(CAST('100.00' AS DECIMAL(38,10)) * 1234567 * 100, 7654321 * 100, 10);

Always pass s. Without it, the result scale is larger, and the query can fail because the result needs more than 38 digits.

Cast an INTEGER input to DECIMAL(10,0) and a BIGINT input to DECIMAL(19,0) first. Without the cast, the result can be wrong when the other input is a DECIMAL with fewer than 10 digits: decimal_division(10, CAST('4.00' AS DECIMAL(9,2)), 2) returns 0.01 instead of 2.50. Do not use decimal_division when both inputs are integers; use //.

When the exact result is halfway between two values at scale s, decimal_division rounds to the even value: 1.00 / 8.00 returns 0.12. Engine version 3 rounds away from zero and returns 0.13. To get the engine version 3 result, use this form, where u is one unit at scale s, for example CAST('0.01' AS DECIMAL(2,2)) when s is 2:

decimal_division(x, y, s) + CASE WHEN abs(x - decimal_division(x, y, s) * y) * 2 = abs(y) * u AND abs(decimal_division(x, y, s) * y) < abs(x) THEN sign(x) * sign(y) * u ELSE 0 END

The result type of this form has more digits than in engine version 3. If the type matters, wrap it in CAST(... AS DECIMAL(p, s)) with the engine version 3 type.

This form multiplies the result by y, which adds the scale of y to the scale s. When x has many digits before the decimal point and these scales are large, the multiplication needs more than 38 digits, and the query fails with an overflow error. Engine version 3 returns a result in this case. If this happens, use plain decimal_division(x, y, s), which rounds ties to the even value.

If both inputs are wide, for example DECIMAL(38,10), the query fails because the result needs more than 38 digits. Engine version 3 limits the result to 38 digits instead. Cast the first input to width 38 + s1 - s2 - s with the same scale, where s1 and s2 are the input scales: decimal_division(CAST(x AS DECIMAL(28,10)), y, 10). This does not change the value. If the value does not fit in the narrower type, the query fails with an out-of-range error. If the computed width is smaller than s1, for example when both inputs are DECIMAL(38,20), this method does not work, and the division cannot be rewritten with decimal_division.

Decimals in JSON

When DuckDB converts a DECIMAL with a width of 15 digits or less to JSON, it writes the value as a DOUBLE, so trailing zeros are lost: 1.5000 becomes 1.5. A wider DECIMAL keeps its digits. To keep the zeros, cast to a width of 16 or more with the same scale before the conversion. This also works for values inside arrays, structs, and maps:

to_json(CAST(x AS DECIMAL(16,4))) -- x is DECIMAL(10,4) to_json(CAST(col AS DECIMAL(16,4)[])) -- an array column to_json(CAST(col AS STRUCT(k DECIMAL(16,4), t VARCHAR))) -- a struct column to_json(CAST(col AS MAP(VARCHAR, DECIMAL(16,4)))) -- a map column

Some JSON text still differs, although the JSON value is the same. Engine version 3 writes very small decimals in E notation (-1E-10), escapes emoji ("\uD83D\uDE00"), and sorts map keys. DuckDB writes -0.0000000001, writes emoji as-is, and keeps map keys in insertion order.

Differences that make a query fail

You find these the first time you run the query.

Engine version 3 Write this instead
transform(arr, x -> x + 1) (arrow lambda) list_transform(arr, lambda x: x + 1)
date_add('day', 1, ts) ts + INTERVAL 1 DAY
date_format(ts, '%Y-%m-%d %H:%i:%s') strftime(ts, '%Y-%m-%d %H:%M:%S')
lag(v) IGNORE NULLS OVER (...) lag(v IGNORE NULLS) OVER (...)
listagg(x, ',') WITHIN GROUP (ORDER BY y) string_agg(x, ',' ORDER BY y)
backtick identifier double-quoted identifier
TABLESAMPLE BERNOULLI (50) TABLESAMPLE BERNOULLI (50 PERCENT)
element_at(arr, 2) arr[2] or list_extract(arr, 2)
to_timestamp('2024-01-02', 'yyyy-mm-dd') strptime('2024-01-02', '%Y-%m-%d')
DECIMAL(18,4) * DECIMAL(9,6) (product needs more than 18 digits) CAST(a AS DECIMAL(38,4)) * b, with the same scale. Needed when both inputs have 18 or fewer digits.
CAST('abc' AS JSON) (text that is not JSON) to_json('abc')
approx_distinct(x) and other engine version 3 functions See the following function replacements.

Common function replacements

Athena does not translate function names. To check whether a function exists, query duckdb_functions().

Engine version 3 DuckDB Notes
approx_distinct(x) approx_count_distinct(x)
approx_percentile(x, 0.5) approx_quantile(x, 0.5)
cardinality(arr) len(arr)
array_join(arr, ',') array_to_string(arr, ',')
transform(arr, f) list_transform(arr, lambda x: ...)
date_format(ts, fmt) strftime(ts, fmt) Rewrite %i to %M, %s to %S.
date_parse(s, fmt) strptime(s, fmt)
date_diff(unit, a, b) date_sub(unit, a, b) date_diff counts boundaries.
day_of_week(d) isodow(d) dayofweek numbers Sunday as 0.
from_unixtime(n) CAST(to_timestamp(n) AS TIMESTAMP)
to_unixtime(ts) epoch(ts)
json_extract_scalar(j, p) json_extract_string(j, p) Also available as the JSON arrow operator.
json_parse(s) json(s) CAST(s AS JSON) also parses text.
regexp_like(s, p) regexp_matches(s, p)
is_nan(x) , is_finite(x) isnan(x), isfinite(x)
rand() random()

Geospatial functions, machine learning functions, approximate-set and quantile-digest functions, and several higher-order and hash functions have no DuckDB equivalent. For the complete list, query duckdb_functions() and consult the DuckDB function reference.

Sharing tables with engine version 3 and other engines

Both engines use the AWS Glue Data Catalog, so a table created with one engine is visible to the other. Follow these rules for tables that more than one engine reads:

  • Use Parquet for Hive tables that other engines read.

  • Keep shared Iceberg tables at format version 2. Athena engine version 3 cannot read format version 3 tables, and you cannot downgrade a table.

  • Do not use TIMESTAMP WITH TIME ZONE, TIME, UUID, or VARIANT columns in Hive tables that other engines read. Engine version 3 cannot query a Hive table that has any of these columns.

  • Do not set write.format.default to orc or avro on a shared Iceberg table. This engine then refuses to write to the table, and other engines write files this engine cannot read.

  • DROP TABLE on an Iceberg table in the AWS Glue Data Catalog leaves its data and metadata files in Amazon S3 on this engine, while engine version 3 deletes them.

  • Query a view only from the engine that created it. This engine cannot query views created by engine version 3 or other engines. Queries from other engines on views created on this engine might fail, or return results that differ from this engine because the engines evaluate some SQL differently.

Non-finite values in Hive CSV tables

When INSERT or CTAS on DuckDB writes a Hive CSV table, it serializes DOUBLE and REAL values for NaN, positive infinity, and negative infinity as nan, inf, and -inf. Engine version 3 writes NaN, Infinity, and -Infinity for the equivalent Hive CSV or TEXTFILE table using LazySimpleSerDe.

DuckDB can read the files it writes. Engine version 3 and other readers using the Hive SerDe read the lowercase values as NULL without failing the query. Counts and IS NULL filters on these tables can silently differ between engines.

This issue affects only Hive CSV tables, DOUBLE or REAL columns, and NaN or infinity values. NULL values are still written as \N. When other engines must read the non-finite values, use Parquet or Iceberg, or write the CSV table with engine version 3.