Skip to content

[4.x] Backport #1582 and #1633 - #1640

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

laughingman7743 merged 22 commits into
4.xfrom
backport/4.x-1582-1633

Conversation

@laughingman7743

@laughingman7743 laughingman7743 commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

WHAT

Backports #1582 (#1575) and #1633 (#1576) to 4.x, in master merge order. Each commit is a git cherry-pick -x of the master commit.

Master PR Issue Commits Change on 4.x
#1582 (fdd2f3c) #1575 all 16 A time-travel hint is placed before the alias that SQLAlchemy generates for a schemaless table beside a same-named table with a schema (FROM events FOR VERSION AS OF 1 AS events_1, db.events). Hints with irregular whitespace (leading whitespace, line breaks, tabs, repeated spaces between the keywords) are recognized.
#1633 (9b6631b) #1576 all 5 docs/sqlalchemy.md (time travel): for a statement with a time-travel hint on an alias, pass the parameters to execute(), not to Select.params() or Select.unique_params().

4.x already has #1547 (#1503) through #1578, which #1582 builds on.

Conflict (recorded in its commit message): 21da68c (#1582), tests/pyathena/sqlalchemy/test_compiler.py. On master, the _StrProxy test helper follows the requires_create_view block that 852b1a5 (#1612, master only) added. On 4.x, _StrProxy is added before _VersionHint without that block. Its added and removed lines equal master's.

Each PR's change equals master's line for line. Compared as sorted added and removed lines of git diff: #1582 for pyathena/sqlalchemy/compiler.py, tests/pyathena/sqlalchemy/test_compiler.py and docs/sqlalchemy.md, and #1633 for the whole diff. Three commits have patch IDs that differ from master's only through context: 21da68c (the conflict), c60f169 and 3f7ba6c.

One commit beyond master (maintainer decision, after the independent review): 286b3b0 corrects a test comment inherited from #1582. An empty table name cannot be rendered only on SQLAlchemy 2.0 (IndexError); 2.1 renders it as "". Comment only. The same correction goes into the next master PR that touches tests/pyathena/sqlalchemy/test_compiler.py.

Release notes (from the master PRs):

WHY

#1575 and #1576 were planned for 4.x. There is no tracking issue for this pair, so this session backports them directly. #1576 was closed as not planned after #1633: SQLAlchemy defects are not reported upstream.

TEST

Tested commit: b3ff610, on 4.x 0b909e1. The head 286b3b0 adds only the comment correction above; just lint passes and the hook rename tests pass on 2.0.44 and 2.1.9.

🤖 Generated with Claude Code

laughingman7743 and others added 21 commits December 1, 2026 10:00
…r whitespace (GH-1575)

format_from_hint_text() placed a time-travel hint before the alias of an
aliased table only. In a SELECT, SQLAlchemy also gives a table without a
schema an alias when another table in the statement has the same name,
and passes the plain table with that alias; the hint followed the alias.
For a table, the text is split after the table as
SQLCompiler.visit_table() renders it without the generated alias. A DML
target has no generated alias and keeps the default placement.

The hint is recognized with an anchored, case-insensitive pattern, so
leading whitespace and any whitespace between the keywords (a line
break, a tab, repeated spaces) are accepted. The hint text is rendered
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit be570b1)
…iew (GH-1575)

State the generated-alias condition precisely (a table of the same name
with a schema), move the DML comment next to the iscrud check, explain
the split check, and tell readers to pass the table itself to
with_hint() for a generated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit dbef7a8)
…nly (GH-1575)

A compiler hook that renders the table under another name and calls the
hint hook itself (events_archive) had its name split after "events".
The text is now split only when the table is followed by the alias
marker of get_render_as_alias_suffix(). This also leaves the text that
a hook appends to a DML target in place, so the iscrud check is no
longer needed.

The hint pattern matches ASCII keywords and whitespace only, as Athena
reads them; Unicode case folding no longer turns a lookalike such as
"VERSİON" into a time-travel hint.

The docs and docstring state that the generated alias needs an
unaliased table of the same name with a schema.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 5ab7d44)
…H-1575)

Decided with the maintainer after consulting Codex and Claude Fable 5.2:

- The hint is matched against hint.upper(), as the merge base did, with
  the whitespace that Athena reads ([ \t\r\n]) before and between the
  keywords and no word boundary. Only irregular whitespace changes the
  recognition; FOR VERſION and FOR VERSION AS OFTEN are recognized
  as before, and FOR VERSİON is not.
- A table's text is split only when it is not a DML target and the
  rendered table is followed by whitespace, AS in any case, and
  whitespace. get_render_as_alias_suffix() is no longer called with an
  empty name, which broke subclasses that read the name. SQLAlchemy can
  give a DML target a generated alias inside a CTE; it keeps the default
  placement.
- The docs say that SQLAlchemy can generate the alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 28d6188)
…ses (GH-1575)

The plain-table branch rendered the table with SQLCompiler.visit_table()
before it checked anything. A hook that renders the table under another
name bypasses that rendering, and for a table that SQLCompiler cannot
render (a schema name with square brackets under schema_translate_map)
the hint then raised CompileError where the merge base compiled. The
branch now leaves a DML target and text without an AS keyword at the
default placement before it renders the table again.

The docstring names unaliased DML target tables, which keep the default
placement, and the comment describes the text that is split. The hook
tests cover a table renamed with an alias and a suffix hook that reads
the alias name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 724589e)
…AS check linear (GH-1575)

Decided with the maintainer after consulting Codex and Claude Fable 5.2:

- The table is rendered again inside try/except CompileError. A hook
  that renders a table SQLAlchemy cannot render (a schema name with
  square brackets under schema_translate_map) and puts AS in its text
  (a quoted "events AS archive") now gets the default placement, as on
  the merge base, instead of an error.
- The AS pre-check stays, so a hooked table without AS is never rendered
  again (a table without a name raised IndexError there). Its pattern
  starts only at the first whitespace of a run, so a search over a long
  run of whitespace is linear instead of quadratic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2f3c285)
Decided with the maintainer after consulting Codex and Claude Fable 5.2:
the two calls that render the table or the alias again only to compare
it with the text of a subclass hook now fall back to SQLAlchemy's
default placement on any exception, not only CompileError. SQLAlchemy
2.0 raises IndexError for an empty name, and a name that is not a
string raises AttributeError; listing exception types would leave the
next one. Without hooks, these calls render what SQLAlchemy has just
rendered, so they do not fail and the output does not change.

The rename test drops the case without AS that duplicated its neighbor
and adds an empty name and a name that is not a string; a new test
covers an alias that only the hook can name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 8c747ad)
…the hint (GH-1575)

- The whole comparison with the text of a subclass hook, including the
  string operations after the rendering, now runs in
  AthenaStatementCompiler._split_alias() inside the fallback. SQLAlchemy
  renders a UserString name as a UserString, which made removeprefix()
  and removesuffix() raise TypeError after the guarded call.
- An unaliased DML target is checked before the hint is read, so
  update(t).with_hint(None) renders no hint again, as on the merge base,
  instead of raising AttributeError at hint.upper().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit b3a7050)
…1575)

Decided with the maintainer after consulting Codex and Claude Fable 5.2:

- A hint is recognized only when it is a str (str subclasses such as a
  str Enum included), as SQLAlchemy annotates it. Any other hint, such
  as a UserString or bytes, or None passed by a subclass hook, is not
  read and keeps SQLAlchemy's default placement; an empty one no longer
  raises where the merge base compiled.
- The alias suffix is removed with removesuffix() and compared with !=,
  as on the merge base. UserString.endswith() rejects a UserString
  suffix, which moved the hint of a tablesample named by a UserString
  after its alias.
- The DML comment no longer claims that a DML target never has a
  generated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 79ad19d)
The comparison with the text of a subclass hook rendered the table or
alias again even when its name or schema was not a str. SQLAlchemy's
identifier preparer caches what it quotes, so a UserString name stayed
in the cache and was returned for a later table named "events", which
made the FROM clause raise TypeError where the merge base compiled.
Only str names and schemas, as SQLAlchemy annotates them, are compared
now; others keep the default placement without rendering again.

The rename test drops its UserString case, which the new cache test
covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c178a6f)
isinstance() accepts a proxy that reports str as its __class__, such as
Werkzeug's LocalProxy. Rendering a table named by such a proxy to
compare it cached the proxy in the identifier preparer, and a later
plain table of the same name raised TypeError in the FROM clause, where
the merge base compiled. The name and schema are now checked with
issubclass(type(...), str), which still accepts str subclasses.

Conflict on 4.x, tests/pyathena/sqlalchemy/test_compiler.py: on master, the
_StrProxy test helper follows the requires_create_view block that
852b1a5 (#1612, master only) added; it is added before _VersionHint
without that block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 21da68c)
isinstance() accepts an object that only reports str as its class, such
as MagicMock(spec=str). Such a hint on an aliased UPDATE target was read
with hint.upper(), and the pattern raised TypeError on the result, where
the merge base compiled the statement. The hint is now read only when
issubclass(type(hint), str), as for names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit c60f169)
…H-1575)

SQLAlchemy cannot render an int name; it renders other objects, such as
a proxy of a string, so the comments name the int case instead of every
name that is not a string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 98d474e)
For a generated alias, the text before the alias was the rendering of
SQLCompiler.visit_table(), which can be a quoted_name, and that object
went to the next format_from_hint_text() in the MRO. A hook that
requires an exact str, such as one calling sys.intern(), raised where
the merge base compiled. The table text is now the same span of the
text the caller passed.

The mock hint test now fails if the hint is read, as its name says.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit b5aa24e)
…uard (GH-1575)

- A hooked text that ends with AS and no name, such as "events AS ",
  no longer splits; the alias keyword pattern now needs a name
  character after AS, as the docstring says.
- An UPDATE inside a CTE gets the generated alias from SQLAlchemy;
  a hook-free test now pins that its hint keeps the default placement,
  replacing the synthetic hook test, which a check of self.isupdate
  instead of iscrud also passed.
- The alias test with a hook-named alias now also checks that the
  comparison did not quote a UserString name into the identifier cache.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 3f7ba6c)
- The time-travel pattern is matched against hint.upper(), which keeps
  the case of a quoted_name with quote=True.
- The alias pattern checks the first character of a name; the rendered
  table before the match marks where the alias starts.
- The docs say to pass the table itself when the SELECT uses it, and
  the alias when it uses an alias of the table.
- The hook test checks the text it receives, not only its type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 6dcc562)
…travel hint (GH-1576)

SQLAlchemy's Select.params() and Select.unique_params() can drop the
FROM hint of an alias that the statement selects directly, as in
select(snapshot), without an error: on SQLAlchemy 2.0 both do, and on
2.1 unique_params() does. The query then reads the current table. The
time-travel section now says to pass such parameters to execute().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 16004f7)
…GH-1576)

The first self-review found that the hint is also dropped when the alias
is passed to select_from() or join_from(), that on SQLAlchemy 2.0 a
with_hint() after params() does not help, and that SQLAlchemy 2.1 keeps
the hint for params(). The note now names these cases, says that the
query reads the current snapshot, and the example imports bindparam.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit dc4afd0)
Round two of the self-review checked each claim offline on SQLAlchemy
2.0.52, 2.0.58 and 2.1.9: an alias passed as the target of join() loses
its hint in the same way as with select_from() and join_from().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit dc1e143)
…t the example (GH-1576)

The independent review found more calls that give a statement the alias
itself (add_columns(), with_only_columns(), outerjoin(),
outerjoin_from()), cases where passing the alias keeps the hint, and
that a later with_hint() works with the cloned alias but not with the
original one. The note now states the condition as the alias itself
being given to the statement, with examples, says that the methods
can drop the hint, and names with_hint(snapshot, ...). The example has
no common indentation, so it runs when pasted at module scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 07c4486)
…H-1576)

The follow-up review found another way for the alias to reach the FROM
clause (with_only_columns(..., maintain_column_froms=True)). The note
now gives the rule the issue asks for: pass the parameters to execute()
for a statement with a time-travel hint on an alias. Passing the alias
itself is an example of when the methods drop the hint, not the
complete condition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 77f97b3)
"FOR SYSTEM_VERSION AS OF",
# The start of a time-travel hint, matched against hint.upper(), with any
# of the whitespace that Athena reads before and between the keywords.
_TIME_TRAVEL_HINT_PATTERN: ClassVar[re.Pattern[str]] = re.compile(

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 (/code-review medium, focused on faithfulness), scope 0b909e10..b3ff6109: CLEAN.

Self-review round two (claims), same scope: CLEAN.

  • The per-PR line equality and the three patch-ID differences that come only from context (21da68c, c60f169, 3f7ba6c) were measured with git diff and git patch-id --stable.
  • The 4.x floor and the test counts were measured in this session: just test offline passed 4253, and the time-travel/hint tests passed 36 on 2.0.44 and 2.1.9.
  • The differential check: the 4.x base is stable over two runs on 2.0.44 and 2.1.9, the branch has exactly the 2536 expected differences, and its output is identical to master's head.
  • Track upstream: SQLAlchemy 2.0 params()/unique_params() drop the FROM hint of a selected alias #1576 is CLOSED (NOT_PLANNED), as the PR says.
  • The release notes match the master PRs' scope.

GH-1575)

SQLAlchemy 2.0 raises IndexError for an empty table name, but 2.1
renders it as "", so on 2.1 the empty-name case keeps the default
placement because the hooked text does not start with the rendering.
Comment only; the same correction is due on master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
("events", None, "events_archive AS events"),
# SQLCompiler cannot render a schema name with square brackets under
# schema_translate_map (CompileError) or an int name (AttributeError).
# It cannot render an empty name on SQLAlchemy 2.0 (IndexError), and

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 0.172.0, model gpt-6.2-astra, codex exec -s read-only, session 01a22f25-3f94-78d3-8adf-a43be93e7b07.

FINDINGS (1): a test comment inherited from master listed an empty name among names SQLCompiler cannot render. On SQLAlchemy 2.1 it renders as "".

Maintainer decision: fix it here and carry the same correction to master later. Fixed in 286b3b0, comment only. The PR body records it as the one commit beyond master. The hook rename tests pass on 2.0.44 and 2.1.9.

[
("events", None, "events_archive"),
("events", None, "events_archive AS events"),
# SQLCompiler cannot render a schema name with square brackets under

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.

Repair check (relayed): Codex CLI 0.172.0, model gpt-6.2-astra, codex exec -s read-only, session 01a23199-5fef-76e2-839b-38dd4412dbba. Scope: b3ff6109..286b3b01, comment only. Result: CLEAN.

Offline compilation confirmed the comment:

  • On SQLAlchemy 2.0.44–2.0.58, an empty table name raises IndexError.
  • On 2.1.0–2.1.9, it renders as "", so the hooked events AS archive fails the prefix check and keeps the default placement.
  • Both series raise CompileError for tenant[1] under schema_translate_map and AttributeError for an int name.

The independent review is complete.

@laughingman7743
laughingman7743 marked this pull request as ready for review December 1, 2026 12:39
@laughingman7743
laughingman7743 merged commit ce91025 into 4.x Dec 1, 2026
16 checks passed
@laughingman7743
laughingman7743 deleted the backport/4.x-1582-1633 branch December 1, 2026 13:00
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