Skip to content

Require SQLAlchemy 2.0.52 and check the values of Float and binary literals #1574

Done

Description

@laughingman7743

Found in the SQLAlchemy 2.1 audit for #1563 (PR #1566); related to #1494 and #1543.

Problem

SQLAlchemy renders the value of a Numeric or Float parameter into the SQL text, unquoted, when the parameter uses literal_execute or the statement is compiled with literal_binds. From SQLAlchemy 2.0.52 (and in 2.1), NumericCommon.literal_processor() first checks that the value parses as a number (decimal.Decimal(value)), so a non-numeric string raises CompileError instead of being rendered. Up to 2.0.51 it renders str(value).

  1. The declared minimums admit versions without that check: sqlalchemy>=2.0.44 on master and >=2.0.23 on 4.x, for the sqlalchemy and aiosqlalchemy extras.
  2. AthenaFloat skips the check on every version. AthenaFloat.literal_processor() (pyathena/sqlalchemy/types.py) renders Athena DOUBLE literals: the repr() of a float, and nan(), infinity() and -infinity() for non-finite values. It renders every other value with str(value) instead of SQLAlchemy's processor.

Measured on master 07f34d4, offline, by compiling select(bindparam("x", "1 OR 1=1", type_=T, literal_execute=True)) with render_postcompile:

SQLAlchemy T = Numeric(10, 2) T = Float (PyAthena's AthenaFloat)
2.0.51 SELECT 1 OR 1=1 AS anon_1 SELECT 1 OR 1=1 AS anon_1
2.0.52 CompileError SELECT 1 OR 1=1 AS anon_1
2.1.9 CompileError SELECT 1 OR 1=1 AS anon_1

Ordinary parameters are not affected; PyAthena's DB API formatter quotes them.

  1. AthenaBinary renders whatever value.hex() returns. AthenaBinary.literal_processor() (pyathena/sqlalchemy/types.py) renders X'{value.hex()}' for any value with a hex() method. A float renders X'0x1.8000000000000p+0', and an object whose hex() returns ' OR 1=1 -- renders X'' OR 1=1 --'. str and int values raise CompileError, since they have no hex() method.

The other literal processors that PyAthena defines were checked too. AthenaTimestamp and AthenaDate quote a non-temporal value as a string literal. The ARRAY, MAP and ROW literals render each element with its type's processor, and render binary elements with bytes(value).hex(). The lambda text of func.filter() is SQL by design, which docs/sqlalchemy.md documents as trusted input only.

Proposed change

  1. Require sqlalchemy>=2.0.52 (and sqlalchemy[asyncio]>=2.0.52) on master and 4.x. This stays within the 2.0 bug-fix releases. Update uv.lock to a 2.0 release at or above it; CI coverage of 2.1 is CI tests SQLAlchemy only at the locked version, not the declared minimum or the latest release #1543's.
  2. In AthenaFloat.literal_processor(), keep rendering floats and non-finite values as Athena literals, and pass every other value to SQLAlchemy's Float literal processor, so that its check applies.
  3. In AthenaBinary.literal_processor(), render memoryview(value).hex(), which accepts only bytes-like values (bytes, bytearray, memoryview) and raises TypeError for others.

Prototyped offline on 2.0.52 and 2.1.9: "1 OR 1=1" raises CompileError, while 5, Decimal("1.50"), 0.1, float("nan") and Decimal("Infinity") render as before (5, 1.50, 0.1, nan(), infinity()).

Validation plan

  • Offline tests check that a non-numeric string in a Float parameter, and a value that is not bytes-like in a binary parameter, both with literal_execute, raise CompileError. They fail without changes 2 and 3. The existing Float and binary literal tests keep passing.
  • The offline SQLAlchemy tests pass on the new locked version, on 2.0.52 and on 2.1.9.
  • just test sqla and just test sqla-async pass in CI after the PR is marked Ready.

Dependencies and compatibility

Activity

  1. changed the title [-]Require SQLAlchemy 2.0.52 and let it check Float literal values[/-] [+]Require SQLAlchemy 2.0.52 and check the values of Float and binary literals[/+] on Nov 30, 2026
  2. laughingman7743 commented on Nov 30, 2026

    @laughingman7743
    MemberAuthor

    Status after #1573 (merged as 445512d, for #1483), which touched the same processors:

    • Proposed change 2 is done, with one difference. AthenaFloat.literal_processor() writes every real number itself: a float through double_literal(), and nan(), infinity() or -infinity() for NaN and infinity. It passes only values that are not real numbers, such as a string, to SQLAlchemy's Float processor. Passing integers too would make numpy.int64(7) raise CompileError on 2.0.52+, since Decimal(numpy.int64(7)) fails. The merge base rendered 7. The new AthenaNumeric (Numeric, NUMERIC, DECIMAL) likewise passes values that are neither a Decimal nor a float to SQLAlchemy's Numeric processor. Measured on 2.0.52 and 2.1.9: "1 OR 1=1" raises CompileError for both types.
    • Proposed change 3 is done. AthenaBinary.literal_processor() is varbinary_literal() (pyathena/formatter.py), which renders memoryview(value).hex(). The ARRAY element path still calls bytes(value) before it.
    • Remaining: proposed change 1 (the sqlalchemy>=2.0.52 floor and lock). On 2.0.44 to 2.0.51, SQLAlchemy's own processor still writes str(value). test_other_literal_values_render_as_sqlalchemy (tests/pyathena/sqlalchemy/test_types.py) compares PyAthena's processors with SQLAlchemy's own, so it starts failing for a regression once the lock is at 2.0.52 or later.
  3. laughingman7743 commented on Nov 30, 2026

    @laughingman7743
    MemberAuthor

    4.x backport handoff (fixed on master by #1600, merge commit b2620f4):

    • Raise sqlalchemy>=2.0.23 to >=2.0.52 for the sqlalchemy and aiosqlalchemy extras and the dev group, update the README and docs, and lock 2.0.58. Lock only the SQLAlchemy entry, as on master: the local uv rewrites unrelated markers, and uv lock --locked accepts the trimmed lock.
    • Prerequisite: Render inline Decimal values as Athena DECIMAL literals (GH-1483) #1573 (SQLAlchemy: inline Numeric values render as bare numbers, which Athena reads as DOUBLE #1483) on 4.x, so that AthenaFloat and AthenaNumeric pass non-numeric values to SQLAlchemy's processor and AthenaBinary reads bytes through memoryview.
    • Port test_non_numeric_literal_raises and the TrueDivTest.test_truediv_divisor override, which skips the two operand pairs Athena cannot read as literal text (needed with the 2.0.58 lock). Live, 6 passed and 4 skipped on both the default and the async driver.
    • Release note: installations on SQLAlchemy 2.0.23–2.0.51 must upgrade within 2.0. Inline (literal_binds / literal_execute) non-numeric strings in Numeric/Float values, and NumPy integers in Numeric values, raise CompileError.
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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions