Skip to content

Correlated subquery in HAVING cannot reference outer query column, ColumnNotFoundException in PG #37431

@duerwuyi

Description

@duerwuyi

Bug Report

Which version of ShardingSphere did you use?

shardingsphere-proxy 5.5.2
postgres 17.2 as backend

Expected behavior

The query should execute successfully (same as PostgreSQL behavior), because the subquery is correlated and should be able to reference outer query columns in WHERE.

Actual behavior

The following query fails with:
ERROR: Unknown column 'ref_0.c13' in 'where clause'

testdb=> SET DEFAULT SINGLE TABLE STORAGE UNIT = ds_0;
SUCCESS
testdb=> DROP TABLE IF EXISTS t3;
DROP TABLE
testdb=> DROP TABLE IF EXISTS t2;
DROP TABLE
testdb=> CREATE TABLE t3 (c13 INT);
CREATE TABLE
testdb=> CREATE TABLE t2 (vkey INT, c8 TEXT);
CREATE TABLE
---sql
testdb=> select 
  ref_0.c13 as c_0
from 
  t3 as ref_0
group by ref_0.c13
having 'a' = (
    select ref_2.c8 as c_0
    from t2 as ref_2
    where (ref_0.c13) between (ref_2.vkey) and (ref_2.vkey)
    limit 1
);
ERROR:  Unknown column 'ref_0.c13' in 'where clause'.

If I comment out the WHERE clause in the subquery (remove the correlated reference), the query runs successfully.
We consider that the tables are all single tables stored in the same storage unit, so it may not be a problem of federation engine (We did not enable it).

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

ss_postgres.zip

run docker compose up -d in this file.

SQL to execute are shown above

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions