Hello,

I am testing programs with user-defined types (RecordType) where some fields 
are ARRAYs of other RecordType objects.
E.g.:

            CREATE TYPE simple AS (s INT, t BOOLEAN);
            CREATE TYPE vec AS (fields SIMPLE ARRAY);
            CREATE TABLE T(col vec);

It looks like applying UNNEST to an ARRAY of RecordType objects does not 
produce a collection of RecordType objects, but rather unpacks these RecordType 
objects into their fields.    

CREATE VIEW V AS SELECT A.* FROM (T CROSS JOIN UNNEST(T.col.fields) A)

I would expect the resulting view to have 1 column of type "simple", because 
this is the type of elements in the array "fields".
However, Calcite pretends that the resulting view has 2 columns, the two fields 
S, T, of the "simple" RecordType.

This is the logical plan:

    [Logical plan]
    LogicalProject(S=[$1], T=[$2]), id = 10
      LogicalCorrelate(correlation=[$cor0], joinType=[inner], 
requiredColumns=[{0}]), id = 9
        LogicalTableScan(table=[[schema, T]]), id = 1
        Uncollect, id = 8
          LogicalProject(FIELDS=[$cor0.COL.FIELDS]), id = 7
            LogicalValues(tuples=[[{ 0 }]]), id = 2

Here is, for example, how type inference works for Uncollect:

        if (requireAlias) {
          builder.add(itemAliases.get(i), ret);
        } else if (ret.isStruct()) {
          builder.addAll(ret.getFieldList());  // <<< Here a struct is unpacked.
        } else {
          // Element type is not a record, use the field name of the element 
directly
          builder.add(field.getName(), ret);
        }

I assume that this behavior is by design, but I find it strange.
Does anyone know the rationale?

Thank you,
Mihai

Reply via email to