Repository navigation
[4.x] Backport #1644 (GH-1651) - #1653
Conversation
SQLAlchemy renders what follows a FROM hint or a statement hint on the hint's line: another FROM element, a JOIN, the alias that PyAthena places after a time-travel hint, the WHERE or SET of an UPDATE or DELETE target, a UNION, or a closing parenthesis. A hint that ended with a line comment turned that text into a comment, so Athena rejected the statement or ran a different one; a DELETE lost its WHERE clause. A hint whose last line contains "--" now ends with a line break, in format_from_hint_text() for every FROM element and placement, and in get_statement_hint_text() for each statement hint. Only "\n" ends a line, and a "--" that is not a comment adds only the line break, which does not change what the statement does. Hints that are not strings are passed through as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit d5e9dce)
Look the statement up by its shape, so a new or misspelled shape fails instead of compiling an UPDATE. Give the earlier-line case a hint that does not start with a comment, since a time-travel hint is recognized only after whitespace. Name the "\r" case for what it shows, and say that the line break does not change what the statement does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 6390d41)
Appending the line break first made a quoted_name hint a plain str, so the time-travel recognition upper-cased a quote=True hint that #1582 leaves in its own case and moved it before the alias. Recognition now reads the hint as given, and only the text passed on to SQLAlchemy ends with the line break. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit a639d72) Conflicts: tests/pyathena/sqlalchemy/test_compiler.py: import context only; 4.x does not import sqlalchemy.sql.ddl, so quoted_name is added to its sqlalchemy.sql import line.
…1598) The two-FROM case now uses a hint whose comment is on its second line, so a rule that skipped multiline hints fails. The proxy test also traps rpartition(), so the new type check cannot fall back to isinstance(). The docstring limits "does not change what the statement does" to a literal that the hint closes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 7301e92)
| [self._end_line_comment(text) for text in hint_texts] | ||
| ) | ||
|
|
||
| def _end_line_comment(self, hint): |
There was a problem hiding this comment.
Self-review round 1 (behavior, faithfulness to master): CLEAN, no repairs
Scope: git diff da0590dd6944b6f34360c8e7cd1debd9c5f3beb9..3179fd2a3affb3c30df33e16ea89380960dd19a8 in a detached snapshot, run with /code-review high. /simplify was not run: the added and removed lines are identical to master's 10d317f5..7301e929, which passed /simplify in #1644, and a cleanup here would make 4.x diverge from master.
Covered:
- The code from
get_from_hint_textthrough_split_aliasis byte-identical to master 7301e92. - No master-only dependency:
override,_split_aliasand both class patterns exist on4.x, andSQLCompiler.get_statement_hint_text/format_from_hint_textare the same in SQLAlchemy 2.0.44 and 2.1.x. - The import conflict note in b0aa083 is accurate (context only).
- ruff and mypy are clean.
- Offline SQLAlchemy tests: 2.0.44 green. The 2.1.x failures are the same on the untouched base (
connect_argsDDL, SQLAlchemy DDL options in connect_args are ignored on SQLAlchemy 2.0.48 and later #1542 on master). - Rendering probe: INSERT, INSERT…SELECT, CTE, LATERAL, LIMIT, FOR UPDATE.
- Differential check on
4.xwith 2.0.44 and 2.1.9: 2,745 expected changes, 0 problems.
Reported and not adopted. Each applies to master in the same way and was already decided in #1644, so a change here would break faithfulness:
- a
--in a closed literal also gets a line break: intended, and documented in the compatibility note; - a subclass
get_from_hint_text()that appends its own comment: hooks are best effort, as decided in End a hint's trailing line comment with a line break #1644's design consultation; - no INSERT test: it takes the same
iscrudpath as DELETE/UPDATE; - the
ids=[...]list: the file's existing style; prefix_with()/suffix_with(): the caller's SQL, out of scope;- the
format_from_hint_text()summary defers the exact rule to_end_line_comment().
| from sqlalchemy.ext.compiler import compiles | ||
| from sqlalchemy.orm import aliased, declarative_base | ||
| from sqlalchemy.sql import literal, literal_column, operators | ||
| from sqlalchemy.sql import literal, literal_column, operators, quoted_name |
There was a problem hiding this comment.
Self-review round 2 (claims, compatibility): FINDINGS → PR body corrected
Claims checked against this branch:
- The commit map and the import-conflict note: checked with
git range-diffand the line comparison. - 10d317f being on
4.xas 286b3b0: checked. - The
4.xtest counts and the matching 2.1.9 base failures: rerun on the base worktree. - The harness result: base run twice per version, stable.
- "the same result as on master": 2,745 expected changes on both.
- Compatibility for 4.4.1 callers: the SQL changes only for a hint whose last line contains
--, on SQLAlchemy 2.0.44 to 2.1.9 and Python ≥ 3.11 (the PyAthena code uses no 3.12+ syntax).
Correction: "Closes #1651" does not close an issue from a PR into 4.x, because GitHub honors closing keywords only on the default branch. The PR body now says that #1651 is closed with a link to this PR after the merge.
| """ | ||
| # The time-travel recognition reads the hint as given: a quoted_name | ||
| # keeps its own upper(). | ||
| ended = self._end_line_comment(hint) |
There was a problem hiding this comment.
Independent review (relayed): Codex CLI (codex exec -s read-only, fresh session 01a23234-8e2d-72a1-aa38-e1ebf32c640e), static review with offline tests run by the reviewer: CLEAN
The package was a detached snapshot of 3179fd2a3affb3c30df33e16ea89380960dd19a8 (base da0590dd6944b6f34360c8e7cd1debd9c5f3beb9), the master diff of the four source commits and the backport diff. It held no PR text or prior findings. The snapshot and the PR worktree were unchanged afterwards.
- Faithfulness: all four cherry-picks have exactly the same added and removed lines as their master sources, both per commit and over the whole range. Each
-xtrailer names the correct source, in order.10d317f5is in the base as286b3b01. - Conflict: the reviewer reproduced the conflict recorded in b0aa083. Adding
quoted_nameto 4.x's import line reproduces the committed file byte for byte. - Dependencies: no master-only code is needed.
- Accuracy: the docstrings, comments and tests match the observed behavior. This covers the last-line rule for FROM and statement hints,
quoted_namein the time-travel recognition, non-string proxies, explicit and generated aliases, joins, subqueries, UPDATE, DELETE and UNION ALL. - Tests: the 195 statement-compiler tests pass offline on Python 3.11.18 and 3.12.16, each with SQLAlchemy 2.0.44 and 2.1.9.
WHAT
Backports #1644 (#1598) to
4.x, withgit cherry-pick -xof its commits in order:d5e9dce3→628a858a: end a hint's trailing line comment with a line break.6390d419→e819f8f8: tighten the test and the helper docstring.a639d72f→b0aa083e: recognize a time-travel hint before ending its line comment. A conflict in the import context oftests/pyathena/sqlalchemy/test_compiler.pyis recorded in the commit message:4.xdoes not importsqlalchemy.sql.ddl, soquoted_namejoins itssqlalchemy.sqlimport line.7301e929→3179fd2a: cover multiline and proxy hints, and narrow the line-break claim.#1644's first commit,
10d317f5(a test comment), is already on4.xas 286b3b0 (#1640).The behavior is the same as on master. A FROM hint (
with_hint()) or statement hint (with_statement_hint()) that is astrand whose last line contains--ends with"\n". Then a trailing line comment no longer comments out what follows it on the same line: another FROM element, a JOIN, the alias after a time-travel hint, theSETorWHEREof an UPDATE or DELETE target, aUNION, or a closing parenthesis. The time-travel recognition reads the hint as given.WHY
Resolves #1651 (4.4.1). GitHub closes an issue through a closing keyword only for a merge into the default branch, so #1651 is closed with a link to this PR after the merge. 4.4.0 has the defect of #1598, and the fix is compatible: the SQL changes only for a hint whose last line contains
--. Such a statement was either rejected, or ran a different query, or, when the hint's line ended anyway, gains one line break.TEST
Tested commit: 3179fd2 on
4.xda0590d.git diff origin/4.x..HEADare identical to those of master'sgit diff 10d317f5..7301e929.just lint: passed.just test offline(SQLAlchemy 2.0.46, locked): 4278 passed.tests/pyathena/sqlalchemy/offline:4.xbase with 2.1.9 (594 passed, 7 failed):connect_argsDDL options, which SQLAlchemy DDL options in connect_args are ignored on SQLAlchemy 2.0.48 and later #1542 fixed on master. They are unrelated to hints.4.xbase was run twice per version, with identical output each time. Base and branch were compared on SQLAlchemy 2.0.44 and 2.1.9. Result: 2,745 expected changes, 2,745 differences and 0 problems on both, the same result as on master.