Skip to content

Require SQLAlchemy 2.0.52 (GH-1574) - #1600

Merged
laughingman7743 merged 6 commits into
masterfrom
fix/1574-sqlalchemy-2052-floor
Nov 30, 2026
Merged

laughingman7743 merged 6 commits into
masterfrom
fix/1574-sqlalchemy-2052-floor

Conversation

@laughingman7743

@laughingman7743 laughingman7743 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

WHAT

  • Require sqlalchemy>=2.0.52 for the sqlalchemy and aiosqlalchemy extras and the dev group (was >=2.0.44), and update the version in README.md, docs/introduction.md and docs/sqlalchemy.md.
  • Lock SQLAlchemy 2.0.58, the latest 2.0 release (was 2.0.46). The uv.lock diff 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, and uv lock --locked accepts the result.
  • Replace 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, with test_non_numeric_literal_raises: a non-numeric string in a Numeric or Float value raises CompileError with literal_binds and with literal_execute. docs/sqlalchemy.md states the CompileError for both types.
  • In the compliance suite, skip two of the five operand sets of TrueDivTest.test_truediv_divisor, which SQLAlchemy 2.0.55 added and the new lock collects. The test divides CAST(literal_column(text) AS NUMERIC(p, s)) values. Athena reads the text 5.02797279921331664759 as DOUBLE, so numeric_many_decimals returns 7.00000000000000083533 instead of 7. Athena also rejects the 38-digit integer literal of numeric_38_digit_divisor with InvalidRequestException: 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 with literal_execute or literal_binds, a non-numeric string in a Numeric or Float parameter raises CompileError instead of being written into the SQL. A NumPy scalar such as np.int64(7) in a Numeric parameter also raises CompileError from 2.0.52 instead of rendering; Float parameters 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.1 cap. #1573 (#1483) already made AthenaFloat and AthenaNumeric pass such values to SQLAlchemy's processors, and AthenaBinary read bytes through memoryview. On 2.0.44 to 2.0.51, SQLAlchemy's processor still writes str(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-offline in the justfile).

  • just lint, just docs lint: passed. uv lock --locked (Python 3.15 and 3.12): passed.
  • Offline SQLAlchemy tests (-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 offline tests/pyathena on the lock: 4491 passed.
  • select(bindparam("x", "1 OR 1=1", type_=T, literal_execute=True)) raises CompileError for Numeric(10, 2), Float and LargeBinary on 2.0.52, 2.0.58 and 2.1.9. test_non_numeric_literal_raises passes on these versions and fails on 2.0.51 (4 of 4), where SQLAlchemy writes the string. np.int64(7) raises CompileError in a Numeric(10, 0) literal and renders 7 in a Float literal on 2.0.52 and 2.1.9.
  • Compliance tests collected on 2.0.58 but not on 2.0.46: 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 ...):
    • Without the skip: 4 test_truediv_divisor cases failed as described above; the other 6, and the 6 historic-date tests (years 1–1969), passed.
    • With the skip: 24 passed, 4 skipped, on both the default and --dburi async (rerun of test_truediv_divisor after 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 returns Decimal('3'), so the literal, not the division, is rejected. The precision of a literal_column() decimal in a division is recorded on Give DOUBLE division results a consistent type on SQLAlchemy 2.1 #1586.
    • The check-constraint and RETURNING tests: 14 skipped by SQLAlchemy's requirements.
  • just test sqla and just test sqla-async on the locked version run in CI when the PR is marked Ready.

🤖 Generated with Claude Code

laughingman7743 and others added 3 commits November 30, 2026 20:01
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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_binds and literal_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 CompileError for both fixed-point and floating-point types (ffbabb74).

Rejected:

  • np.int64 in a Numeric literal raises CompileError from 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 with Queries of this type are not supported, while dividing the values cast from strings returns Decimal('3').
  • A requirement instead of a test override: precision_numerics_many_significant_digits also 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 --locked passes.
  • 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.

Comment thread pyproject.toml

[project.optional-dependencies]
sqlalchemy = ["sqlalchemy>=2.0.44"]
sqlalchemy = ["sqlalchemy>=2.0.52"]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_raises passes on 2.0.52, 2.0.58 and 2.1.9 and fails 4/4 on 2.0.51.
  • NumPy note: np.int64(7) raises CompileError in Numeric(10, 0) and renders 7 in Float on 2.0.52 and 2.1.9.
  • Skip reasons: both measured against Athena (see round one). The other 6 test_truediv_divisor cases 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 --locked passes 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.x follows through the backport session.
  • Offline evidence on eab1e07f83beb12cb511d686269dc717f7e4841c: 724 passed on 2.0.58, 2.0.52 and 2.1.9; 4491 passed for tests/pyathena.

laughingman7743 and others added 2 commits November 30, 2026 20:53
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>
@laughingman7743
laughingman7743 force-pushed the fix/1574-sqlalchemy-2052-floor branch from f4b8274 to 06fcc37 Compare November 30, 2026 12:03
Comment thread docs/sqlalchemy.md
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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review (Codex codex exec -s read-only, static review plus offline Python; no AWS): base 0efea77f0bd43cdcba169414427c639b2e6bd15e

  • Initial review of ..eab1e07f (session 01a22c3e-ccae-7372-a007-2bf3a789f547). Codex confirmed the floor in every place it is stated, the lock (uv lock --check passes, 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 the TrueDivTest override (both parameter axes kept, 4 of 10 combinations skipped, the guard keeps the inherited tests before 2.0.55). Findings:
    1. The docs said CompileError, but literal_execute raises StatementError with orig CompileError at execution. Fixed in 171daf16. The test stays at compile time; the wrapping at execution is SQLAlchemy's generic behavior.
    2. The docs overstated rejection: a numeric string such as "1.250" renders. Fixed in 171daf16 and 06fcc379 (the rule is now "decimal.Decimal(value) raises").
    3. SQLAlchemy up to 2.0.55 collides expanding bind names (ids with [4, 5] vs ids_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).
    4. 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.
  • Follow-up 1 (eab1e07f..171daf16, session 01a22c4d-2cf7-7a71-8049-61fc7aef9604): two qualifications, None and the decimal context. The rule was restated as "Decimal(value) raises".
  • Follow-up 2 (171daf16..f4b8274c, session 01a22c51-cc18-7582-af22-0ee650f99f83): the added "None is written as NULL" does not hold for evaluates_none() types. The sentence was removed in 06fcc379180b04237e8edabec2ae745dbd1d51b3; such a None is rejected under the stated rule.

Snapshot and PR worktrees were clean after each run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant