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

   ## Jira Link
   
   [CALCITE-7776](https://issues.apache.org/jira/browse/CALCITE-7776)
   
   ## Changes Proposed
   
   `UNNEST ... WITH ORDINALITY` over a column declared `ANY` (as used by
   schema-less/dynamic-schema systems) silently dropped the `ORDINALITY`
   column. `SqlUnnestOperator#inferReturnType` and
   `Uncollect#deriveRowType` each have a branch for `ANY`-typed operands
   that returns a single-column row type (`$unnest`/the column name)
   without ever reaching the `if (withOrdinality)` block that appends the
   `ORDINALITY` column, unlike the `ARRAY`/`MULTISET`/`MAP` branches
   right below them. The net effect: naming two aliases for the unnested
   column and its ordinal (e.g. `UNNEST(t.tags) WITH ORDINALITY AS
   T(x, o)`) fails validation with `List of column aliases must have same
   degree as table; table has 1 columns ('$unnest'), whereas alias list
   has 2 columns`, and `SELECT *` only returns the unnested value, never
   the ordinal.
   
   Reproduction (dynamic-schema table with an `ANY`-typed column):
   
   ```sql
   select "D"."ID", "T"."X", "T"."O"
   from "s"."docs" as "D", UNNEST("D"."TAGS") WITH ORDINALITY as "T"("X", "O")
   ```
   
   fails with the alias-degree error above, while the same query over a
   declared array works:
   
   ```sql
   select * from UNNEST(ARRAY['red', 'green']) WITH ORDINALITY as "T"("X", "O")
   ```
   
   The fix appends the `ORDINALITY` column in the `ANY` branch of both
   methods before returning, the same way the `ARRAY`/`MULTISET`/`MAP`
   branches already do.
   
   Added `SqlToRelConverterTest#testDynamicSchemaUnnestWithOrdinality`,
   mirroring the existing `testDynamicSchemaUnnest`/`testUnnestWithOrdinality`
   coverage, over the dynamic-table fixture (`fake_col`, an `ANY`-typed
   column) with `WITH ORDINALITY` and two aliases, plus its golden plan in
   `SqlToRelConverterTest.xml`.
   
   ### Testing
   
   - `./gradlew :core:test --tests 
"org.apache.calcite.test.SqlToRelConverterTest"` — passes, including the new 
test.
   - Manually reproduced the reported failure against the dynamic-schema
     fixture before the fix (validator threw the alias-degree exception
     from the Jira report), and confirmed it is resolved after the fix
     (row type now carries the `ORDINALITY` field and the query validates).
   
   <sub>Assisted by Claude (Anthropic); see `Generated-by` trailer on the 
commit.</sub>
   


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