Repository navigation
Description
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).
- The declared minimums admit versions without that check:
sqlalchemy>=2.0.44on master and>=2.0.23on4.x, for thesqlalchemyandaiosqlalchemyextras. AthenaFloatskips the check on every version.AthenaFloat.literal_processor()(pyathena/sqlalchemy/types.py) renders Athena DOUBLE literals: therepr()of a float, andnan(),infinity()and-infinity()for non-finite values. It renders every other value withstr(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.
AthenaBinaryrenders whatevervalue.hex()returns.AthenaBinary.literal_processor()(pyathena/sqlalchemy/types.py) rendersX'{value.hex()}'for any value with ahex()method. A float rendersX'0x1.8000000000000p+0', and an object whosehex()returns' OR 1=1 --rendersX'' OR 1=1 --'.strandintvalues raiseCompileError, since they have nohex()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
- Require
sqlalchemy>=2.0.52(andsqlalchemy[asyncio]>=2.0.52) on master and4.x. This stays within the 2.0 bug-fix releases. Updateuv.lockto 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. - In
AthenaFloat.literal_processor(), keep rendering floats and non-finite values as Athena literals, and pass every other value to SQLAlchemy'sFloatliteral processor, so that its check applies. - In
AthenaBinary.literal_processor(), rendermemoryview(value).hex(), which accepts only bytes-like values (bytes,bytearray,memoryview) and raisesTypeErrorfor 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
Floatparameter, and a value that is not bytes-like in a binary parameter, both withliteral_execute, raiseCompileError. 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 sqlaandjust test sqla-asyncpass in CI after the PR is marked Ready.
Dependencies and compatibility
- Blocked by SQLAlchemy DDL options in connect_args are ignored on SQLAlchemy 2.0.48 and later #1542 (PR Take SQLAlchemy DDL options in connect_args from the first connection (GH-1542) #1559). From SQLAlchemy 2.0.48,
connect_argsno longer reaches the DDL compiler, so 7 offline DDL tests fail on any version this issue requires, until that fix merges. - Installations on SQLAlchemy 2.0.44 to 2.0.51 (master) or 2.0.23 to 2.0.51 (
4.x) must upgrade SQLAlchemy within 2.0. Backport to4.x.
Activity
[-]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[/+]laughingman7743 commentedon Nov 30, 2026
Status after #1573 (merged as 445512d, for #1483), which touched the same processors:
AthenaFloat.literal_processor()writes every real number itself: a float throughdouble_literal(), andnan(),infinity()or-infinity()for NaN and infinity. It passes only values that are not real numbers, such as a string, to SQLAlchemy'sFloatprocessor. Passing integers too would makenumpy.int64(7)raiseCompileErroron 2.0.52+, sinceDecimal(numpy.int64(7))fails. The merge base rendered7. The newAthenaNumeric(Numeric,NUMERIC,DECIMAL) likewise passes values that are neither aDecimalnor afloatto SQLAlchemy'sNumericprocessor. Measured on 2.0.52 and 2.1.9:"1 OR 1=1"raisesCompileErrorfor both types.AthenaBinary.literal_processor()isvarbinary_literal()(pyathena/formatter.py), which rendersmemoryview(value).hex(). The ARRAY element path still callsbytes(value)before it.sqlalchemy>=2.0.52floor and lock). On 2.0.44 to 2.0.51, SQLAlchemy's own processor still writesstr(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.Assert the CompileError for non-numeric literals and match skipped op…
Inline the skipped operand pairs of test_truediv_divisor (GH-1574)
State which literal values SQLAlchemy rejects and how (GH-1574)
Name the decimal.Decimal() rule and NULL in the literal docs (GH-1574)
Name the decimal.Decimal() rule in the literal docs (GH-1574)
Merge pull request #1600 from pyathena-dev/fix/1574-sqlalchemy-2052-f…
laughingman7743 commentedon Nov 30, 2026
4.x backport handoff (fixed on master by #1600, merge commit b2620f4):
sqlalchemy>=2.0.23to>=2.0.52for thesqlalchemyandaiosqlalchemyextras 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, anduv lock --lockedaccepts the trimmed lock.4.x, so thatAthenaFloatandAthenaNumericpass non-numeric values to SQLAlchemy's processor andAthenaBinaryreads bytes throughmemoryview.test_non_numeric_literal_raisesand theTrueDivTest.test_truediv_divisoroverride, 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.literal_binds/literal_execute) non-numeric strings inNumeric/Floatvalues, and NumPy integers inNumericvalues, raiseCompileError.