Search in sources :

Example 1 with MapSqlType

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

the class EnumerableUncollect method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    final BlockBuilder builder = new BlockBuilder();
    final EnumerableRel child = (EnumerableRel) getInput();
    final Result result = implementor.visitChild(this, 0, child, pref);
    final PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), getRowType(), JavaRowFormat.LIST);
    // final Enumerable<List<Employee>> child = <<child adapter>>;
    // return child.selectMany(FLAT_PRODUCT);
    final Expression child_ = builder.append("child", result.block);
    final List<Integer> fieldCounts = new ArrayList<>();
    final List<FlatProductInputType> inputTypes = new ArrayList<>();
    for (RelDataTypeField field : child.getRowType().getFieldList()) {
        final RelDataType type = field.getType();
        if (type instanceof MapSqlType) {
            fieldCounts.add(2);
            inputTypes.add(FlatProductInputType.MAP);
        } else {
            final RelDataType elementType = type.getComponentType();
            if (elementType.isStruct()) {
                fieldCounts.add(elementType.getFieldCount());
                inputTypes.add(FlatProductInputType.LIST);
            } else {
                fieldCounts.add(-1);
                inputTypes.add(FlatProductInputType.SCALAR);
            }
        }
    }
    final Expression lambda = Expressions.call(BuiltInMethod.FLAT_PRODUCT.method, Expressions.constant(Ints.toArray(fieldCounts)), Expressions.constant(withOrdinality), Expressions.constant(inputTypes.toArray(new FlatProductInputType[inputTypes.size()])));
    builder.add(Expressions.return_(null, Expressions.call(child_, BuiltInMethod.SELECT_MANY.method, lambda)));
    return implementor.result(physType, builder.toBlock());
}
Also used : ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) FlatProductInputType(org.apache.calcite.runtime.SqlFunctions.FlatProductInputType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) Expression(org.apache.calcite.linq4j.tree.Expression) MapSqlType(org.apache.calcite.sql.type.MapSqlType) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 2 with MapSqlType

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

the class SqlUnnestOperator method inferReturnType.

// ~ Methods ----------------------------------------------------------------
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    final RelDataTypeFactory.Builder builder = opBinding.getTypeFactory().builder();
    for (Integer operand : Util.range(opBinding.getOperandCount())) {
        RelDataType type = opBinding.getOperandType(operand);
        if (type.isStruct()) {
            type = type.getFieldList().get(0).getType();
        }
        assert type instanceof ArraySqlType || type instanceof MultisetSqlType || type instanceof MapSqlType;
        if (type instanceof MapSqlType) {
            builder.add(MAP_KEY_COLUMN_NAME, type.getKeyType());
            builder.add(MAP_VALUE_COLUMN_NAME, type.getValueType());
        } else {
            if (type.getComponentType().isStruct()) {
                builder.addAll(type.getComponentType().getFieldList());
            } else {
                builder.add(SqlUtil.deriveAliasFromOrdinal(operand), type.getComponentType());
            }
        }
    }
    if (withOrdinality) {
        builder.add(ORDINALITY_COLUMN_NAME, SqlTypeName.INTEGER);
    }
    return builder.build();
}
Also used : MapSqlType(org.apache.calcite.sql.type.MapSqlType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) MultisetSqlType(org.apache.calcite.sql.type.MultisetSqlType) RelDataType(org.apache.calcite.rel.type.RelDataType) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType)

Example 3 with MapSqlType

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

the class RelSchemaConverter method getRelDataType.

// TODO: SAMZA-2345 - Change RelSchemaConverter code to apply nullability based on Sql schema.
public RelDataType getRelDataType(SqlFieldSchema fieldSchema) {
    switch(fieldSchema.getFieldType()) {
        case ARRAY:
            RelDataType elementType = getRelDataType(fieldSchema.getElementSchema());
            return new ArraySqlType(elementType, true);
        case BOOLEAN:
            return createTypeWithNullability(createSqlType(SqlTypeName.BOOLEAN), true);
        case DOUBLE:
            return createTypeWithNullability(createSqlType(SqlTypeName.DOUBLE), true);
        case REAL:
            return createTypeWithNullability(createSqlType(SqlTypeName.REAL), true);
        case FLOAT:
            return createTypeWithNullability(createSqlType(SqlTypeName.FLOAT), true);
        case STRING:
            return createTypeWithNullability(createSqlType(SqlTypeName.VARCHAR), true);
        case BYTES:
            return createTypeWithNullability(createSqlType(SqlTypeName.VARBINARY), true);
        case INT16:
        case INT32:
            return createTypeWithNullability(createSqlType(SqlTypeName.INTEGER), true);
        case INT64:
            return createTypeWithNullability(createSqlType(SqlTypeName.BIGINT), true);
        case ROW:
        case ANY:
            // TODO Calcite execution engine doesn't support record type yet.
            return createTypeWithNullability(createSqlType(SqlTypeName.ANY), true);
        case MAP:
            RelDataType valueType = getRelDataType(fieldSchema.getValueSchema());
            return new MapSqlType(createSqlType(SqlTypeName.VARCHAR), valueType, true);
        default:
            String msg = String.format("Field Type %s is not supported", fieldSchema.getFieldType());
            LOG.error(msg);
            throw new SamzaException(msg);
    }
}
Also used : MapSqlType(org.apache.calcite.sql.type.MapSqlType) RelDataType(org.apache.calcite.rel.type.RelDataType) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) SamzaException(org.apache.samza.SamzaException)

Example 4 with MapSqlType

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

the class ZetaSqlUnnest 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 typeFactory = rel.getCluster().getTypeFactory();
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    if (fields.size() == 1 && fields.get(0).getType().getSqlTypeName() == SqlTypeName.ANY) {
        // and Any type.
        return builder.add(fields.get(0).getName(), SqlTypeName.ANY).nullable(true).build();
    }
    for (RelDataTypeField field : fields) {
        if (field.getType() instanceof MapSqlType) {
            builder.add(SqlUnnestOperator.MAP_KEY_COLUMN_NAME, Preconditions.checkArgumentNotNull(field.getType().getKeyType(), "Encountered MAP type with null key type in field %s", field));
            builder.add(SqlUnnestOperator.MAP_VALUE_COLUMN_NAME, Preconditions.checkArgumentNotNull(field.getType().getValueType(), "Encountered MAP type with null value type in field %s", field));
        } else {
            assert field.getType() instanceof ArraySqlType;
            RelDataType ret = Preconditions.checkArgumentNotNull(field.getType().getComponentType(), "Encountered ARRAY type with null component type in field %s", field);
            // Only difference than Uncollect.java: treats record type and scalar type equally
            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.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeField) MapSqlType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.MapSqlType) RelDataTypeFactory(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType) ArraySqlType(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.type.ArraySqlType)

Example 5 with MapSqlType

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

the class RexBuilder method makeLiteral.

/**
 * Creates a literal of a given type. The value is assumed to be
 * compatible with the type.
 *
 * @param value     Value
 * @param type      Type
 * @param allowCast Whether to allow a cast. If false, value is always a
 *                  {@link RexLiteral} but may not be the exact type
 * @return Simple literal, or cast simple literal
 */
public RexNode makeLiteral(Object value, RelDataType type, boolean allowCast) {
    if (value == null) {
        return makeCast(type, constantNull);
    }
    if (type.isNullable()) {
        final RelDataType typeNotNull = typeFactory.createTypeWithNullability(type, false);
        RexNode literalNotNull = makeLiteral(value, typeNotNull, allowCast);
        return makeAbstractCast(type, literalNotNull);
    }
    value = clean(value, type);
    RexLiteral literal;
    final List<RexNode> operands;
    switch(type.getSqlTypeName()) {
        case CHAR:
            return makeCharLiteral(padRight((NlsString) value, type.getPrecision()));
        case VARCHAR:
            literal = makeCharLiteral((NlsString) value);
            if (allowCast) {
                return makeCast(type, literal);
            } else {
                return literal;
            }
        case BINARY:
            return makeBinaryLiteral(padRight((ByteString) value, type.getPrecision()));
        case VARBINARY:
            literal = makeBinaryLiteral((ByteString) value);
            if (allowCast) {
                return makeCast(type, literal);
            } else {
                return literal;
            }
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case DECIMAL:
            return makeExactLiteral((BigDecimal) value, type);
        case FLOAT:
        case REAL:
        case DOUBLE:
            return makeApproxLiteral((BigDecimal) value, type);
        case BOOLEAN:
            return (Boolean) value ? booleanTrue : booleanFalse;
        case TIME:
            return makeTimeLiteral((TimeString) value, type.getPrecision());
        case TIME_WITH_LOCAL_TIME_ZONE:
            return makeTimeWithLocalTimeZoneLiteral((TimeString) value, type.getPrecision());
        case DATE:
            return makeDateLiteral((DateString) value);
        case TIMESTAMP:
            return makeTimestampLiteral((TimestampString) value, type.getPrecision());
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            return makeTimestampWithLocalTimeZoneLiteral((TimestampString) value, type.getPrecision());
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            return makeIntervalLiteral((BigDecimal) value, type.getIntervalQualifier());
        case MAP:
            final MapSqlType mapType = (MapSqlType) type;
            @SuppressWarnings("unchecked") final Map<Object, Object> map = (Map) value;
            operands = new ArrayList<>();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                operands.add(makeLiteral(entry.getKey(), mapType.getKeyType(), allowCast));
                operands.add(makeLiteral(entry.getValue(), mapType.getValueType(), allowCast));
            }
            return makeCall(SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR, operands);
        case ARRAY:
            final ArraySqlType arrayType = (ArraySqlType) type;
            @SuppressWarnings("unchecked") final List<Object> listValue = (List) value;
            operands = new ArrayList<>();
            for (Object entry : listValue) {
                operands.add(makeLiteral(entry, arrayType.getComponentType(), allowCast));
            }
            return makeCall(SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR, operands);
        case MULTISET:
            final MultisetSqlType multisetType = (MultisetSqlType) type;
            operands = new ArrayList<>();
            for (Object entry : (List) value) {
                final RexNode e = entry instanceof RexLiteral ? (RexNode) entry : makeLiteral(entry, multisetType.getComponentType(), allowCast);
                operands.add(e);
            }
            if (allowCast) {
                return makeCall(SqlStdOperatorTable.MULTISET_VALUE, operands);
            } else {
                return new RexLiteral((Comparable) FlatLists.of(operands), type, type.getSqlTypeName());
            }
        case ROW:
            operands = new ArrayList<>();
            // noinspection unchecked
            for (Pair<RelDataTypeField, Object> pair : Pair.zip(type.getFieldList(), (List<Object>) value)) {
                final RexNode e = pair.right instanceof RexLiteral ? (RexNode) pair.right : makeLiteral(pair.right, pair.left.getType(), allowCast);
                operands.add(e);
            }
            return new RexLiteral((Comparable) FlatLists.of(operands), type, type.getSqlTypeName());
        case ANY:
            return makeLiteral(value, guessType(value), allowCast);
        default:
            throw Util.unexpected(type.getSqlTypeName());
    }
}
Also used : ByteString(org.apache.calcite.avatica.util.ByteString) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) MapSqlType(org.apache.calcite.sql.type.MapSqlType) MultisetSqlType(org.apache.calcite.sql.type.MultisetSqlType) NlsString(org.apache.calcite.util.NlsString) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map)

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