Search in sources :

Example 6 with MapSqlType

use of org.apache.calcite.sql.type.MapSqlType in project calcite by apache.

the class Uncollect method deriveUncollectRowType.

/**
 * Returns the row type returned by applying the 'UNNEST' operation to a
 * relational expression.
 *
 * <p>Each column in the relational expression must be a multiset of structs
 * or an array. The return type is the type of that column, plus an ORDINALITY
 * column if {@code withOrdinality}.
 */
public static RelDataType deriveUncollectRowType(RelNode rel, boolean withOrdinality) {
    RelDataType inputType = rel.getRowType();
    assert inputType.isStruct() : inputType + " is not a struct";
    final List<RelDataTypeField> fields = inputType.getFieldList();
    final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
    for (RelDataTypeField field : fields) {
        if (field.getType() instanceof MapSqlType) {
            builder.add(SqlUnnestOperator.MAP_KEY_COLUMN_NAME, field.getType().getKeyType());
            builder.add(SqlUnnestOperator.MAP_VALUE_COLUMN_NAME, field.getType().getValueType());
        } else {
            RelDataType ret = field.getType().getComponentType();
            assert null != ret;
            if (ret.isStruct()) {
                builder.addAll(ret.getFieldList());
            } else {
                // Element type is not a record. It may be a scalar type, say
                // "INTEGER". Wrap it in a struct type.
                builder.add(SqlUtil.deriveAliasFromOrdinal(field.getIndex()), ret);
            }
        }
    }
    if (withOrdinality) {
        builder.add(SqlUnnestOperator.ORDINALITY_COLUMN_NAME, SqlTypeName.INTEGER);
    }
    return builder.build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) MapSqlType(org.apache.calcite.sql.type.MapSqlType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)5 MapSqlType (org.apache.calcite.sql.type.MapSqlType)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)3 ArraySqlType (org.apache.calcite.sql.type.ArraySqlType)3 ArrayList (java.util.ArrayList)2 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)2 MultisetSqlType (org.apache.calcite.sql.type.MultisetSqlType)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Map (java.util.Map)1 RelDataType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType)1 RelDataTypeFactory (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory)1 RelDataTypeField (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField)1 ArraySqlType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.ArraySqlType)1 MapSqlType (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.MapSqlType)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 BlockBuilder (org.apache.calcite.linq4j.tree.BlockBuilder)1 Expression (org.apache.calcite.linq4j.tree.Expression)1 FlatProductInputType (org.apache.calcite.runtime.SqlFunctions.FlatProductInputType)1 NlsString (org.apache.calcite.util.NlsString)1