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();
}
Aggregations