terrymanu commented on issue #37431:
URL: 
https://github.com/apache/shardingsphere/issues/37431#issuecomment-3673472779

   ##  Problem Understanding
   
     - Scenario: single database, single table (SET DEFAULT SINGLE TABLE 
STORAGE UNIT = ds_0), no SQL Federation or sharding enabled. SQL is a standard 
GROUP BY + HAVING with a correlated subquery whose WHERE references the outer 
grouping column.
     - Expected: behave like native PostgreSQL and push down successfully.
     - Actual: in Proxy 5.5.2 (and ShardingSphere-JDBC, which shares the same 
binder) binding fails with Unknown column 'ref_0.c13' in 'where clause', so it 
never reaches the backend.
   
   ##  Root Cause
   
     - In infra/binder/core, HAVING binding passes only the projection-based 
currentTableBinderContexts into the subquery’s outer context and omits the 
FROM-generated tableBinderContexts (which contain outer aliases such as ref_0).
     - When binding the subquery, the outer alias is invisible; 
ColumnSegmentBinder then treats ref_0.c13 in the subquery WHERE as missing and 
throws ColumnNotFoundException.
   
   ##  Analysis
   
     - The SQL is within ShardingSphere’s supported scope: single-table direct 
pushdown should work, and the database itself executes it fine.
     - Correlated subqueries in WHERE work because that path includes the FROM 
context; the HAVING path misses it.
     - Impact scope: all dialects (MySQL/PG/openGauss, etc.) in both Proxy and 
JDBC modes can hit this binding defect.
   
   ##  Conclusion (Fix Proposal & Invitation)
   
     - This is a general binder gap for correlated subqueries in HAVING. 
Proposal: when binding HAVING, merge the FROM tableBinderContexts together with 
currentTableBinderContexts into the subquery’s outer context (align with the 
WHERE path) so the subquery can resolve outer
       aliases.
     - Add regression tests in infra/binder for HAVING with correlated 
subqueries referencing outer columns to ensure no ColumnNotFound and correct 
pushdown.
     - We warmly welcome community contributors to submit a PR to implement the 
fix and the tests—your help to make this scenario solid is greatly appreciated!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to