[ 
https://issues.apache.org/jira/browse/CALCITE-7791?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Sean Broeder updated CALCITE-7791:
----------------------------------
    Description: 
AliasNamespace.validateImpl() already handles this ambiguity for table 
functions.  For example, SELECT rmp, rmp.i FROM TABLE(ramp(3)) AS rmp works 
because a 1-column result aliased with no column list gets wrapped in 
SingleColumnAliasRelDataType.  This was added in CALCITE-5779. However, that 
wrapping only triggers for SqlKind.COLLECTION_TABLE.

UNNEST hits the same ambiguity and isn't covered. 
SqlUnnestOperator#inferReturnType flattens a single-field ROW array element to 
the top level, producing the same 1-field row shape, but AliasNamespace falls 
through to the plain branch instead and renames that field to the alias and 
discards its real name.

Example
{code:sql}
select * from unnest(cast(array[row(1)] as row(a integer) array)) as d;
-- before: RecordType(INTEGER NOT NULL D)
-- after:  RecordType(INTEGER NOT NULL A)
{code}

Behavior after the fix
- The column keeps the declared field name for star expansion and NATURAL JOIN, 
for plain UNNEST, WITH ORDINALITY, a sub-query operand and MULTISET.
- The alias still resolves ({{d}}, {{d.a}}, {{d.ordinality}}), and USING 
accepts either name.
- Conformances where {{allowAliasUnnestItems}} is true (PRESTO, BIG_QUERY) are 
unaffected.

Outer joins
The nullable copy of a SingleColumnAliasRelDataType left the wrapped types NOT 
NULL, so a column of an aliased UNNEST or {{TABLE(...) AS alias}} on the 
null-generating side of an outer join failed conversion with "failed to 
preserve datatypes". {{RelDataTypeFactoryImpl.createTypeWithNullability}} now 
makes both wrapped types nullable.

Release note
Under the default conformance, {{select *}} over an aliased UNNEST of 
single-field ROW values returns the declared field name instead of the alias 
(for example {{A}} instead of {{D}}), and NATURAL JOIN matches the field name. 
A generated field name does not apply; the alias still names the column. To 
keep the alias as the output column name, write {{AS d (d)}}.


  was:
AliasNamespace.validateImpl() already handles this ambiguity for table 
functions.  For example, SELECT rmp, rmp.i FROM TABLE(ramp(3)) AS rmp works 
because a 1-column result aliased with no column list gets wrapped in 
SingleColumnAliasRelDataType.  This was added in CALCITE-5779. However, that 
wrapping only triggers for SqlKind.COLLECTION_TABLE.

UNNEST hits the same ambiguity and isn't covered. 
SqlUnnestOperator#inferReturnType flattens a single-field ROW array element to 
the top level, producing the same 1-field row shape, but AliasNamespace falls 
through to the plain branch instead and renames that field to the alias and 
discards its real name.


> UNNEST of a single-field ROW array loses the field name when aliased
> --------------------------------------------------------------------
>
>                 Key: CALCITE-7791
>                 URL: https://issues.apache.org/jira/browse/CALCITE-7791
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.42.0
>            Reporter: Sean Broeder
>            Assignee: Sean Broeder
>            Priority: Major
>              Labels: pull-request-available
>
> AliasNamespace.validateImpl() already handles this ambiguity for table 
> functions.  For example, SELECT rmp, rmp.i FROM TABLE(ramp(3)) AS rmp works 
> because a 1-column result aliased with no column list gets wrapped in 
> SingleColumnAliasRelDataType.  This was added in CALCITE-5779. However, that 
> wrapping only triggers for SqlKind.COLLECTION_TABLE.
> UNNEST hits the same ambiguity and isn't covered. 
> SqlUnnestOperator#inferReturnType flattens a single-field ROW array element 
> to the top level, producing the same 1-field row shape, but AliasNamespace 
> falls through to the plain branch instead and renames that field to the alias 
> and discards its real name.
> Example
> {code:sql}
> select * from unnest(cast(array[row(1)] as row(a integer) array)) as d;
> -- before: RecordType(INTEGER NOT NULL D)
> -- after:  RecordType(INTEGER NOT NULL A)
> {code}
> Behavior after the fix
> - The column keeps the declared field name for star expansion and NATURAL 
> JOIN, for plain UNNEST, WITH ORDINALITY, a sub-query operand and MULTISET.
> - The alias still resolves ({{d}}, {{d.a}}, {{d.ordinality}}), and USING 
> accepts either name.
> - Conformances where {{allowAliasUnnestItems}} is true (PRESTO, BIG_QUERY) 
> are unaffected.
> Outer joins
> The nullable copy of a SingleColumnAliasRelDataType left the wrapped types 
> NOT NULL, so a column of an aliased UNNEST or {{TABLE(...) AS alias}} on the 
> null-generating side of an outer join failed conversion with "failed to 
> preserve datatypes". {{RelDataTypeFactoryImpl.createTypeWithNullability}} now 
> makes both wrapped types nullable.
> Release note
> Under the default conformance, {{select *}} over an aliased UNNEST of 
> single-field ROW values returns the declared field name instead of the alias 
> (for example {{A}} instead of {{D}}), and NATURAL JOIN matches the field 
> name. A generated field name does not apply; the alias still names the 
> column. To keep the alias as the output column name, write {{AS d (d)}}.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to