Repository navigation
Require SQLAlchemy 2.0.52 (GH-1574) - #1600
Conversation
From SQLAlchemy 2.0.52, the literal processors of Numeric and Float check that a value is a number before rendering it unquoted, so a non-numeric string in a literal_execute parameter or with literal_binds raises CompileError instead of becoming SQL text. PyAthena's AthenaFloat and AthenaNumeric pass such values to those processors (#1573), but 2.0.44 to 2.0.51 still render str(value). Require 2.0.52 for the sqlalchemy and aiosqlalchemy extras and the dev group, and lock 2.0.58, the latest 2.0 release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…H-1574) SQLAlchemy 2.0.55 added TrueDivTest.test_truediv_divisor, which divides CAST(literal_column(text) AS NUMERIC(p, s)) values. Athena reads the text 5.02797279921331664759 as DOUBLE, so the quotient is not exactly 7, and rejects the 38-digit integer literal of numeric_38_digit_divisor ("Queries of this type are not supported"). Skip those two operand sets; the other three run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…erand pairs (GH-1574) With the 2.0.52 floor, SQLAlchemy's Numeric and Float literal processors reject a value that is not a number. Assert the CompileError directly, with literal_binds and literal_execute, instead of comparing PyAthena's processors with SQLAlchemy's, which also agreed on 2.0.51 where both wrote the string. State the CompileError in the docs. Match the skipped test_truediv_divisor cases on both operands, so an upstream case with the same dividend still runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| ("literal_execute", "compile_kwargs"), | ||
| [(False, {"literal_binds": True}), (True, {"render_postcompile": True})], | ||
| ) | ||
| def test_non_numeric_literal_raises(type_, literal_execute, compile_kwargs): |
There was a problem hiding this comment.
Self-review round one (implementation behavior): FINDINGS, repaired
Base 0efea77f0bd43cdcba169414427c639b2e6bd15e, head c4430adb/096febbd → eab1e07f83beb12cb511d686269dc717f7e4841c. Covered: the extras and dev group, README and docs versions, the uv.lock SQLAlchemy entry, the new compliance tests that the 2.0.58 lock collects, and the literal processors that rely on the floor. Passes: /code-review high and /simplify (reuse, simplification, efficiency, altitude).
Repaired:
- The comparison test also passed on 2.0.51, so it did not protect the guarantee the floor exists for. It is replaced by
test_non_numeric_literal_raises(literal_bindsandliteral_execute), which fails on 2.0.51 (ffbabb74). - The skip matched only the dividend; it now matches both operands, and the pairs are inlined (
ffbabb74,eab1e07f). - The docs now state that a non-number raises
CompileErrorfor both fixed-point and floating-point types (ffbabb74).
Rejected:
np.int64in aNumericliteral raisesCompileErrorfrom 2.0.52. This is SQLAlchemy's behavior, already recorded on CI tests SQLAlchemy only at the locked version, not the declared minimum or the latest release #1543 as an inherited upstream change kept as is; the PR body and docs state it.- The suggestion that the 38-digit rejection comes from the division: measured,
SELECT CAST(30000000000000000000000000000000000000 AS DECIMAL(38, 0))alone fails withQueries of this type are not supported, while dividing the values cast from strings returnsDecimal('3'). - A requirement instead of a test override:
precision_numerics_many_significant_digitsalso gates other upstream tests that pass on Athena, and the 38-digit pair has no requirement. - Regenerating the lock with uv's marker rewrites: both consultants preferred the minimal diff, and
uv lock --lockedpasses. - Pre-existing and recorded on Give DOUBLE division results a consistent type on SQLAlchemy 2.1 #1586 (division design): a
literal_column()decimal with more than about 17 significant digits loses precision in a division, because Athena reads the text as DOUBLE before the cast.
|
|
||
| [project.optional-dependencies] | ||
| sqlalchemy = ["sqlalchemy>=2.0.44"] | ||
| sqlalchemy = ["sqlalchemy>=2.0.52"] |
There was a problem hiding this comment.
Self-review round two (claims, compatibility, evidence): CLEAN
Base 0efea77f0bd43cdcba169414427c639b2e6bd15e, head eab1e07f83beb12cb511d686269dc717f7e4841c. Claims checked:
- "From 2.0.52, a non-number raises
CompileError":test_non_numeric_literal_raisespasses on 2.0.52, 2.0.58 and 2.1.9 and fails 4/4 on 2.0.51. - NumPy note:
np.int64(7)raisesCompileErrorinNumeric(10, 0)and renders7inFloaton 2.0.52 and 2.1.9. - Skip reasons: both measured against Athena (see round one). The other 6
test_truediv_divisorcases and the historic-date tests (years 1–1969) pass live on the default and the async driver. The check-constraint and RETURNING tests that 2.0.58 adds are skipped by requirements (14 skipped). - Lock:
uv lock --lockedpasses on Python 3.15 and 3.12. Only the SQLAlchemy entry and three specifiers change. - Compatibility: installations on 2.0.44–2.0.51 must upgrade SQLAlchemy within 2.0; the PR body states this for release notes.
4.xfollows through the backport session. - Offline evidence on
eab1e07f83beb12cb511d686269dc717f7e4841c: 724 passed on 2.0.58, 2.0.52 and 2.1.9; 4491 passed fortests/pyathena.
SQLAlchemy writes a numeric string such as "1.250" as is and rejects a value that decimal.Decimal() does not read as a number. A literal_execute parameter is rendered when the statement runs, so the rejection reaches the caller as a StatementError wrapping the CompileError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f4b8274 to
06fcc37
Compare
| A `Decimal` of a fixed-point type rendered inline, as with `literal_binds`, `literal_execute`, or in an ARRAY literal, is written with `decimal_literal()`: a DECIMAL literal with the value's own scale and no exponent, such as `DECIMAL '5.520'` for `Decimal("5.520")` and `DECIMAL '1000'` for `Decimal("1E+3")`. | ||
| Athena rejects a DECIMAL literal whose precision or scale is above 38, such as the one for `Decimal("1E-39")`, with scale 39, and a NaN or infinity, such as `DECIMAL 'NaN'` or `DECIMAL '-Infinity'`. | ||
| A `float` is written as for a floating-point type, and other values, such as an `int`, as SQLAlchemy's `Numeric` writes them. | ||
| A `float` is written as for a floating-point type, and other values, such as an `int` or a numeric string, as SQLAlchemy's `Numeric` writes them; it rejects a value for which `decimal.Decimal(value)` raises, such as `"1 OR 1=1"` or a NumPy integer. |
There was a problem hiding this comment.
Independent review (Codex codex exec -s read-only, static review plus offline Python; no AWS): base 0efea77f0bd43cdcba169414427c639b2e6bd15e
- Initial review of
..eab1e07f(session01a22c3e-ccae-7372-a007-2bf3a789f547). Codex confirmed the floor in every place it is stated, the lock (uv lock --checkpasses, and the 10 reverted marker rewrites are equivalent), 580 offline PyAthena tests passing on 2.0.52, 2.0.58 and 2.1.9, and theTrueDivTestoverride (both parameter axes kept, 4 of 10 combinations skipped, the guard keeps the inherited tests before 2.0.55). Findings:- The docs said
CompileError, butliteral_executeraisesStatementErrorwithorigCompileErrorat execution. Fixed in171daf16. The test stays at compile time; the wrapping at execution is SQLAlchemy's generic behavior. - The docs overstated rejection: a numeric string such as
"1.250"renders. Fixed in171daf16and06fcc379(the rule is now "decimal.Decimal(value)raises"). - SQLAlchemy up to 2.0.55 collides expanding bind names (
idswith[4, 5]vsids_1). Rejected: an upstream bug for every dialect; the floor follows what PyAthena's own contracts need (Require SQLAlchemy 2.0.52 and check the values of Float and binary literals #1574 decision). - 2.1.9 cannot import the compliance suite (
DeprecatedCompoundSelectTest). Pre-existing; fixed by Decode float ARRAY elements and import the compliance suite on SQLAlchemy 2.1 (GH-1569) #1577.
- The docs said
- Follow-up 1 (
eab1e07f..171daf16, session01a22c4d-2cf7-7a71-8049-61fc7aef9604): two qualifications,Noneand the decimal context. The rule was restated as "Decimal(value)raises". - Follow-up 2 (
171daf16..f4b8274c, session01a22c51-cc18-7582-af22-0ee650f99f83): the added "Noneis written asNULL" does not hold forevaluates_none()types. The sentence was removed in06fcc379180b04237e8edabec2ae745dbd1d51b3; such aNoneis rejected under the stated rule.
Snapshot and PR worktrees were clean after each run.
WHAT
sqlalchemy>=2.0.52for thesqlalchemyandaiosqlalchemyextras and the dev group (was>=2.0.44), and update the version inREADME.md,docs/introduction.mdanddocs/sqlalchemy.md.uv.lockdiff is limited to the SQLAlchemy entry and the three specifiers. The local uv also rewrote unrelated resolution markers (polars, coverage) into equivalent forms; those hunks are left out, anduv lock --lockedaccepts the result.test_other_literal_values_render_as_sqlalchemy, which compared PyAthena's literal processors with SQLAlchemy's and also passed on 2.0.51 where both wrote the string, withtest_non_numeric_literal_raises: a non-numeric string in aNumericorFloatvalue raisesCompileErrorwithliteral_bindsand withliteral_execute.docs/sqlalchemy.mdstates theCompileErrorfor both types.TrueDivTest.test_truediv_divisor, which SQLAlchemy 2.0.55 added and the new lock collects. The test dividesCAST(literal_column(text) AS NUMERIC(p, s))values. Athena reads the text5.02797279921331664759as DOUBLE, sonumeric_many_decimalsreturns7.00000000000000083533instead of 7. Athena also rejects the 38-digit integer literal ofnumeric_38_digit_divisorwithInvalidRequestException: Queries of this type are not supported. The skip matches both operands, and the other three operand sets run.Behavior change for installation: SQLAlchemy 2.0.44 to 2.0.51 no longer satisfy the extras. From 2.0.52, SQLAlchemy's
Numeric(2.1:NumericCommon) literal processor checks that a value is a number, so withliteral_executeorliteral_binds, a non-numeric string in aNumericorFloatparameter raisesCompileErrorinstead of being written into the SQL. A NumPy scalar such asnp.int64(7)in aNumericparameter also raisesCompileErrorfrom 2.0.52 instead of rendering;Floatparameters render NumPy scalars as before.WHY
Closes #1574 (sub-issue of #1494). The maintainer decided to raise the minimum within the 2.0 bug-fix releases on master and
4.x, with no<2.1cap. #1573 (#1483) already madeAthenaFloatandAthenaNumericpass such values to SQLAlchemy's processors, andAthenaBinaryread bytes throughmemoryview. On 2.0.44 to 2.0.51, SQLAlchemy's processor still writesstr(value), so only this floor makes the check apply. Codex and Claude Fable 5.2 reviewed the change before this PR. Fable pointed out the compliance tests that 2.0.55 added, which led to the targeted live run below.4.x(minimum 2.0.23) gets the same floor through the backport session;#1573's processor changes are a prerequisite there.TEST
Tested commit: 06fcc37 (on 0efea77); its changes after eab1e07 are docs wording only. The offline commands use the environment of
just test offline(_test-offlinein thejustfile).just lint,just docs lint: passed.uv lock --locked(Python 3.15 and 3.12): passed.-m "not aws" tests/pyathena/sqlalchemy tests/pyathena/aio/sqlalchemy): 724 passed on the locked 2.0.58, on 2.0.52, and on 2.1.9. Full offlinetests/pyathenaon the lock: 4491 passed.select(bindparam("x", "1 OR 1=1", type_=T, literal_execute=True))raisesCompileErrorforNumeric(10, 2),FloatandLargeBinaryon 2.0.52, 2.0.58 and 2.1.9.test_non_numeric_literal_raisespasses on these versions and fails on 2.0.51 (4 of 4), where SQLAlchemy writes the string.np.int64(7)raisesCompileErrorin aNumeric(10, 0)literal and renders7in aFloatliteral on 2.0.52 and 2.1.9.TrueDivTest.test_truediv_divisor(10),DateHistoricTest/DateTimeHistoricTest(6),ComponentReflectionTestExtra.test_check_constraint_parenthesized_expressions(12),SimpleUpdateDeleteTest.test_update_returning_unchanged_rows(2). Live against Athena (uv run --env-file .env pytest -n 1 tests/sqlalchemy/test_suite.py -k ...):test_truediv_divisorcases failed as described above; the other 6, and the 6 historic-date tests (years 1–1969), passed.--dburi async(rerun oftest_truediv_divisorafter the skip matched both operands: 6 passed, 4 skipped).SELECT CAST(30000000000000000000000000000000000000 AS DECIMAL(38, 0))alone fails with the same error, while dividing the same values cast from strings returnsDecimal('3'), so the literal, not the division, is rejected. The precision of aliteral_column()decimal in a division is recorded on Give DOUBLE division results a consistent type on SQLAlchemy 2.1 #1586.just test sqlaandjust test sqla-asyncon the locked version run in CI when the PR is marked Ready.🤖 Generated with Claude Code