Repository navigation
Description
Found in the independent review of PR #1577 (#1569); related to #1543 and #1494. Codex and Claude Fable 5.2 both judged it outside #1569: the type of an expression is fixed when SQLAlchemy builds it, before the dialect compiles it, so fixing it needs a decision about the type of a division result.
Problem
Measured offline on master 07f34d4 with SQLAlchemy 2.0.46 and 2.1.9. t has the columns n Numeric(10, 2), f Float, i Integer, j Integer, and c F, where F is a TypeDecorator with impl = Float.
-
SQLAlchemy 2.1 types a mixed
Numeric/Floatquotient asNumeric, while PyAthena renders it as DOUBLE.visit_truediv_binary()(pyathena/sqlalchemy/compiler.py) casts both sides to DOUBLE when either is aFloat, on every version. SQLAlchemy 2.0 types such an expression asFloat(); 2.1 gives it the type of the left operand (NumericCommon._expression_adaptations), so the ARRAY built from it gets decimal elements:Expression 2.0.46 2.1.9 type of literal(Decimal("1")) / literal(2.0)Float()Numeric()select(func.array_agg(q).concat([0.25]))for thatq... || CAST(:p AS ARRAY(REAL))CompileError: ARRAY decimal values require explicit Numeric precisiontype of t.c.n / t.c.fFloat()Numeric(precision=10, scale=2)select(func.array_agg(t.c.n / t.c.f).concat([0.255]))CAST(:p AS ARRAY(REAL))CAST(:p AS ARRAY(DECIMAL(10, 2)))The DOUBLE quotients are then concatenated with an ARRAY that the bind casts to
DECIMAL(10, 2); whether Athena rounds0.255there was not run.Integer / Integeralready has this mismatch on 2.0: PyAthena rendersi / CAST(j AS DOUBLE), SQLAlchemy types itNumeric(), andarray_agg(t.c.i / t.c.j).concat([0.25])raises the sameCompileErroron 2.0.46 and 2.1.9. CI tests SQLAlchemy only at the locked version, not the declared minimum or the latest release #1543's notes record the 2.1 inference forarray_agg(Numeric + Float)as upstream behavior; for division, PyAthena's DOUBLE rendering and the expression type disagree. -
visit_truediv_binary()does not resolve aTypeDecoratorto its implementation.t.c.c / t.c.irenderst.c / CAST(t.i AS DECIMAL)on both versions.(t.c.c * t.c.i) / t.c.jrendersCAST((t.c * t.i) AS DOUBLE) / CAST(t.j AS DOUBLE)on 2.0.46 but(t.c * t.i) / CAST(t.j AS DECIMAL)on 2.1.9, because 2.1 keeps the decorator as the type ofc * i. Commit d0bc81d (SQLAlchemy: true division of DECIMAL(p, s) columns casts both operands to DECIMAL(38, 0) #1450) reverted resolving variants in this method, so resolving decorators touches that decision (cache keys and variants).
type_coerce(q, Double()) (or cast()) before aggregating works around problem 1 on every version: the concatenated bind is cast to ARRAY(DOUBLE).
To decide
- Whether PyAthena should give a division that it renders as DOUBLE a floating-point type where the ARRAY and bind types are built from it, keep SQLAlchemy's type and document the workaround, or something else. Whichever is chosen applies to
Integer / Integeras well, whose array elements decode toDecimaltoday on every version. - Whether
visit_truediv_binary()should check the dialect implementation of each operand type (e.g. withdialect_type()), which would changec / ito DOUBLE on 2.0 and 2.1.
Validation plan
Offline compile and result-processor tests on the locked version, the declared minimum, and 2.1.9 for the cases above, plus a live check of the chosen rendering of the Numeric(10, 2) / Float concatenation.
Activity
laughingman7743 commentedon Nov 30, 2026
Another division case, found while raising the SQLAlchemy floor in #1574 (PR #1600). SQLAlchemy 2.0.55 added
TrueDivTest.test_truediv_divisorto the compliance suite, which dividescast(literal_column(text), Numeric(p, s))values. Measured against Athena:numeric_many_decimals:SELECT CAST(5.02797279921331664759 AS DECIMAL(22, 20)) / CAST(0.71828182845904523537 AS DECIMAL(22, 20))returns7.00000000000000083533, not 7. Athena reads the literal text as DOUBLE before the CAST, so digits beyond double precision are lost._cast_decimal_literal_column()(pyathena/sqlalchemy/compiler.py) produces the same shape for aliteral_column()typedNumeric(p, s)in a division, anddocs/sqlalchemy.md(Division) presents that cast as keeping the declared DECIMAL type; it keeps the type, not digits past about 17 significant digits.numeric_38_digit_divisor:SELECT CAST(30000000000000000000000000000000000000 AS DECIMAL(38, 0))alone fails withInvalidRequestException: Queries of this type are not supported, whileCAST('3...0' AS DECIMAL(38, 0)) / CAST('1...0' AS DECIMAL(38, 0))returnsDecimal('3'). So the 38-digit integer literal is rejected, not the division.PR #1600 skips these two operand sets in the compliance suite, since the literal text is raw SQL. Whether the dialect should render a
literal_column()typedNumeric(p, s)whose text is a plain number asDECIMAL '<text>'instead ofCAST(<text> AS DECIMAL(38, s))belongs to the division design here.Pass the classified type to the literal column cast (GH-1586)
Describe quotient types by operand class (GH-1586)
Correct the quotient type for a Double divisor (GH-1586)
Describe quotient types by example (GH-1586)