Skip to content

[4.x] Backport #1644 (GH-1651) - #1653

Merged
laughingman7743 merged 4 commits into
4.xfrom
backport/4.x-1598
Dec 1, 2026
Merged

laughingman7743 merged 4 commits into
4.xfrom
backport/4.x-1598

Conversation

@laughingman7743

@laughingman7743 laughingman7743 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

WHAT

Backports #1644 (#1598) to 4.x, with git cherry-pick -x of its commits in order:

  1. d5e9dce3 → 628a858a: end a hint's trailing line comment with a line break.
  2. 6390d419 → e819f8f8: tighten the test and the helper docstring.
  3. a639d72f → b0aa083e: recognize a time-travel hint before ending its line comment. A conflict in the import context of tests/pyathena/sqlalchemy/test_compiler.py is recorded in the commit message: 4.x does not import sqlalchemy.sql.ddl, so quoted_name joins its sqlalchemy.sql import line.
  4. 7301e929 → 3179fd2a: cover multiline and proxy hints, and narrow the line-break claim.

#1644's first commit, 10d317f5 (a test comment), is already on 4.x as 286b3b0 (#1640).

The behavior is the same as on master. A FROM hint (with_hint()) or statement hint (with_statement_hint()) that is a str and 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, the SET or WHERE of an UPDATE or DELETE target, a UNION, 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.x da0590d.

  • The added and removed lines of git diff origin/4.x..HEAD are identical to those of master's git diff 10d317f5..7301e929.
  • just lint: passed.
  • just test offline (SQLAlchemy 2.0.46, locked): 4278 passed.
  • tests/pyathena/sqlalchemy/ offline:
  • Differential check (End a hint's trailing line comment with a line break #1644's harness: 13,503 cases × 10 dialects). The 4.x base 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.
  • Not run: live AWS tests (no live test renders such a hint). The AWS CI runs when the PR is marked Ready.

laughingman7743 and others added 4 commits December 2, 2026 00:11
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)
@laughingman7743 laughingman7743 added this to the 4.4.1 milestone Dec 1, 2026
[self._end_line_comment(text) for text in hint_texts]
)

def _end_line_comment(self, hint):

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 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_text through _split_alias is byte-identical to master 7301e92.
  • No master-only dependency: override, _split_alias and both class patterns exist on 4.x, and SQLCompiler.get_statement_hint_text/format_from_hint_text are 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_args DDL, 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.x with 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 iscrud path 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

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 2 (claims, compatibility): FINDINGS → PR body corrected

Claims checked against this branch:

  • The commit map and the import-conflict note: checked with git range-diff and the line comparison.
  • 10d317f being on 4.x as 286b3b0: checked.
  • The 4.x test 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)

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 (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 -x trailer names the correct source, in order. 10d317f5 is in the base as 286b3b01.
  • Conflict: the reviewer reproduced the conflict recorded in b0aa083. Adding quoted_name to 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_name in 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.

@laughingman7743
laughingman7743 marked this pull request as ready for review December 1, 2026 15:31
@laughingman7743
laughingman7743 merged commit 6f4a038 into 4.x Dec 1, 2026
14 checks passed
@laughingman7743
laughingman7743 deleted the backport/4.x-1598 branch December 1, 2026 15:55
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.

1 participant