]> 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 02:08:26 +0000 (21:08 -0500)
commit12ccef924a99b53f1b0a905830df4c2ba03cd7d4
tree274a13bc4dc09c9ee3536f5cc45ba93e0a7e2b96
parentd18c4cf4991758bf31de1e26956ff69898a0bcdb
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
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