]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
apply Grouping on left side of JSONB subscript in compiler
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Jan 2026 01:03:10 +0000 (20:03 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Jan 2026 03:05:50 +0000 (22:05 -0500)
commitee285f78f482fd3fe0c6f47da124d73fdce50828
treeed782978888822a7d4336e213a8f400e159120fb
parent893699178555cc7a8842ac1e51c053ce9dd28f3a
apply Grouping on left side of JSONB subscript in compiler

Fixed regression in PostgreSQL dialect where JSONB subscription syntax
would generate incorrect SQL for :func:`.cast` expressions returning JSONB,
causing syntax errors. The dialect now properly wraps cast expressions in
parentheses when using the ``[]`` subscription syntax, generating
``(CAST(...))[index]`` instead of ``CAST(...)[index]`` to comply with
PostgreSQL syntax requirements. This extends the fix from :ticket:`12778`
which addressed the same issue for function calls.

This reverts how we did the fix for #12778 in Function.self_group()
and instead moves to a direct Grouping() applied in the PG compiler
based on isinstance of the left side.

in retrospect, when we first did #10927, we **definitely** made
the completely wrong choice in how to do this, the original idea
to detect when we were in an UPDATE and use [] only then was
by **far** what we should have done, given the fact that PG indexes
are based on exact syntax matches.  but since we've made everyone
switch to [] format for their indexes now we can't keep going
back and forth.   even though PG would like [] to be the defacto
syntax it simply is not.    We should potentially pursue a dialect/
create_engine option to switch the use of [] back to -> for
all cases except UPDATE.

Fixes: #13067
Change-Id: I2e0d0f45ebb820d2a8f214550f1d1a500bae223b
(cherry picked from commit 217b3fd053857d396a65349a170da1342ae030d1)
doc/build/changelog/unreleased_20/13067.rst [new file with mode: 0644]
doc/build/tutorial/data_select.rst
lib/sqlalchemy/dialects/postgresql/base.py
lib/sqlalchemy/sql/functions.py
test/dialect/postgresql/test_compiler.py
test/dialect/postgresql/test_query.py