Skip to content

Give DOUBLE division results a consistent type on SQLAlchemy 2.1 #1586

Done

Description

@laughingman7743

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.

  1. SQLAlchemy 2.1 types a mixed Numeric/Float quotient as Numeric, while PyAthena renders it as DOUBLE. visit_truediv_binary() (pyathena/sqlalchemy/compiler.py) casts both sides to DOUBLE when either is a Float, on every version. SQLAlchemy 2.0 types such an expression as Float(); 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 that q ... || CAST(:p AS ARRAY(REAL)) CompileError: ARRAY decimal values require explicit Numeric precision
    type of t.c.n / t.c.f Float() 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 rounds 0.255 there was not run. Integer / Integer already has this mismatch on 2.0: PyAthena renders i / CAST(j AS DOUBLE), SQLAlchemy types it Numeric(), and array_agg(t.c.i / t.c.j).concat([0.25]) raises the same CompileError on 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 for array_agg(Numeric + Float) as upstream behavior; for division, PyAthena's DOUBLE rendering and the expression type disagree.

  2. visit_truediv_binary() does not resolve a TypeDecorator to its implementation. t.c.c / t.c.i renders t.c / CAST(t.i AS DECIMAL) on both versions. (t.c.c * t.c.i) / t.c.j renders CAST((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 of c * 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 / Integer as well, whose array elements decode to Decimal today on every version.
  • Whether visit_truediv_binary() should check the dialect implementation of each operand type (e.g. with dialect_type()), which would change c / i to 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

  1. laughingman7743 commented on Nov 30, 2026

    @laughingman7743
    MemberAuthor

    Another division case, found while raising the SQLAlchemy floor in #1574 (PR #1600). SQLAlchemy 2.0.55 added TrueDivTest.test_truediv_divisor to the compliance suite, which divides cast(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)) returns 7.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 a literal_column() typed Numeric(p, s) in a division, and docs/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 with InvalidRequestException: Queries of this type are not supported, while CAST('3...0' AS DECIMAL(38, 0)) / CAST('1...0' AS DECIMAL(38, 0)) returns Decimal('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() typed Numeric(p, s) whose text is a plain number as DECIMAL '<text>' instead of CAST(<text> AS DECIMAL(38, s)) belongs to the division design here.

  2. added this to the 5.0.0 milestone on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions