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

   ## Jira Link
   
   [CALCITE-7614](https://issues.apache.org/jira/browse/CALCITE-7614)
   
   ## Changes Proposed
   
   When the operand of `UNNEST` is an **unqualified** identifier whose leading 
component is a `PEEK_FIELDS` struct column, validation fails:
   
   ```sql
   -- table T has column S: STRUCT<arr ARRAY<VARCHAR>> with 
StructKind.PEEK_FIELDS
   
   SELECT * FROM t AS r CROSS JOIN UNNEST(r.s.arr) AS x;  -- OK 
(table-qualified)
   SELECT * FROM t        CROSS JOIN UNNEST(s.arr)   AS x;  -- FAILS: "Column 
'S.S' not found in table 'T'"
   ```
   
   ### Root cause
   
   `DelegatingScope.fullyQualify` qualifies the unqualified operand by first 
resolving the table that owns the `PEEK_FIELDS` column. That resolution 
re-enters validation of the `UNNEST` namespace, which expands the **same** 
operand `SqlIdentifier` and rewrites its name list **in place** to the 
fully-qualified form (`SqlValidatorImpl.validateIdentifier` → 
`SqlIdentifier.assignNamesFrom`). Control then returns to `fullyQualify`, which 
re-qualifies the now-already-qualified identifier, prepending the table/struct 
segment a second time and producing the doubled `S.S`.
   
   The table-qualified form is unaffected because re-qualifying `r.s.arr` is 
idempotent.
   
   ### Fix
   
   `DelegatingScope.fullyQualify` now snapshots the identifier before the 
re-entrant resolution and builds the qualified `PEEK_FIELDS` identifier from 
that snapshot, so it always works from the original names. The change is a 
no-op when no re-entrant rewrite occurs. The production change is confined to 
`DelegatingScope.java`.
   
   ### Tests
   
   - New `SqlValidatorTest.testUnnestPeekFieldsArrayColumn` covering 
qualified/unqualified × `CROSS JOIN`/comma-join.
   - Added a `PEEK_FIELDS` struct type containing an array field 
(`Fixture.peekArrayType`) and a `DEPT_NESTED_PEEK(DEPTNO, S)` table in the mock 
catalog (`MockCatalogReaderSimple`), since no such column existed.
   - Updated `SqlAdvisorTest`'s expected SALES completion list to include the 
new table.
   
   `./gradlew :core:test` passes (15649 tests, 0 failed).
   


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