AlexisCubilla opened a new pull request, #5114:
URL: https://github.com/apache/calcite/pull/5114

   Fixes [CALCITE-7663](https://issues.apache.org/jira/browse/CALCITE-7663).
   
   ### Problem
   
   When `RelToSqlConverter` converts a `RelNode` for a dialect whose
   `SqlDialect.supportGenerateSelectStar()` returns `false` for a join with
   duplicate field names (e.g. `PostgresqlSqlDialect`), the `SELECT *` is 
expanded
   into explicit column references, but the expanded columns are **not** 
aliased to
   their (unique) row-type field names. When such a projection wraps a join that
   has two columns with the same name (e.g. two `DEPTNO` columns), the resulting
   sub-query exposes two identically named columns, and referencing that 
sub-query
   from an outer query is ambiguous. On PostgreSQL this fails at execution with
   `ERROR: column reference "..." is ambiguous`.
   
   This only manifests through the programmatic path (`RelBuilder` / a `RelNode`
   converted directly), because the parser + validator path uniquifies output
   names before conversion.
   
   ### Root cause
   
   The two `SELECT *` expansion blocks in `SqlImplementor`
   (`Result#builder(...)` and `Result#maybeExpandStar(...)`) build the select 
list
   from `Context.field(i)`, which returns the physical column reference. Its
   intrinsic name can collide with another column even though the `RelNode` row
   type already holds unique field names (`DEPTNO`, `DEPTNO0`). The expansion 
never
   carried those unique names into the SQL as aliases.
   
   ### Fix
   
   When expanding `SELECT *`, alias each column to its unique row-type field 
name
   whenever the column's intrinsic name differs from it. This mirrors what a
   `Project` node already does and matches the SQL produced through the 
validator
   path.
   
   ### Tests
   
   - Added `testNoSelectStarJoinWithDuplicateNamesAndFetchIsNotAmbiguous` in
     `RelToSqlConverterTest`, reproducing a join with duplicate field names 
wrapped
     by a FETCH and joined again.
   - Updated `testNoSelectStarWithJoin` to reflect the disambiguating alias.
   - `RelToSqlConverterTest`, `JdbcAdapterTest`, `autostyleCheck`, `checkstyle` 
all pass.
   


-- 
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