Search in sources :

Example 66 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class RexImpTable method harmonize.

/**
 * Ensures that operands have identical type.
 */
private static List<RexNode> harmonize(final RexToLixTranslator translator, final List<RexNode> operands) {
    int nullCount = 0;
    final List<RelDataType> types = new ArrayList<>();
    final RelDataTypeFactory typeFactory = translator.builder.getTypeFactory();
    for (RexNode operand : operands) {
        RelDataType type = operand.getType();
        type = toSql(typeFactory, type);
        if (translator.isNullable(operand)) {
            ++nullCount;
        } else {
            type = typeFactory.createTypeWithNullability(type, false);
        }
        types.add(type);
    }
    if (allSame(types)) {
        // unchanged.
        return operands;
    }
    final RelDataType type = typeFactory.leastRestrictive(types);
    if (type == null) {
        // to be harmonized.
        return operands;
    }
    assert (nullCount > 0) == type.isNullable();
    final List<RexNode> list = new ArrayList<>();
    for (RexNode operand : operands) {
        list.add(translator.builder.ensureType(type, operand, false));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 67 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class RexToLixTranslator method translateProjects.

/**
 * Translates a {@link RexProgram} to a sequence of expressions and
 * declarations.
 *
 * @param program Program to be translated
 * @param typeFactory Type factory
 * @param list List of statements, populated with declarations
 * @param outputPhysType Output type, or null
 * @param root Root expression
 * @param inputGetter Generates expressions for inputs
 * @param correlates Provider of references to the values of correlated
 *                   variables
 * @return Sequence of expressions, optional condition
 */
public static List<Expression> translateProjects(RexProgram program, JavaTypeFactory typeFactory, BlockBuilder list, PhysType outputPhysType, Expression root, InputGetter inputGetter, Function1<String, InputGetter> correlates) {
    List<Type> storageTypes = null;
    if (outputPhysType != null) {
        final RelDataType rowType = outputPhysType.getRowType();
        storageTypes = new ArrayList<>(rowType.getFieldCount());
        for (int i = 0; i < rowType.getFieldCount(); i++) {
            storageTypes.add(outputPhysType.getJavaFieldType(i));
        }
    }
    return new RexToLixTranslator(program, typeFactory, root, inputGetter, list).setCorrelates(correlates).translateList(program.getProjectList(), storageTypes);
}
Also used : ExpressionType(org.apache.calcite.linq4j.tree.ExpressionType) Type(java.lang.reflect.Type) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 68 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType 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 69 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class EnumerableMergeJoin method implement.

public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
    BlockBuilder builder = new BlockBuilder();
    final Result leftResult = implementor.visitChild(this, 0, (EnumerableRel) left, pref);
    final Expression leftExpression = builder.append("left", leftResult.block);
    final ParameterExpression left_ = Expressions.parameter(leftResult.physType.getJavaRowType(), "left");
    final Result rightResult = implementor.visitChild(this, 1, (EnumerableRel) right, pref);
    final Expression rightExpression = builder.append("right", rightResult.block);
    final ParameterExpression right_ = Expressions.parameter(rightResult.physType.getJavaRowType(), "right");
    final JavaTypeFactory typeFactory = implementor.getTypeFactory();
    final PhysType physType = PhysTypeImpl.of(typeFactory, getRowType(), pref.preferArray());
    final List<Expression> leftExpressions = new ArrayList<>();
    final List<Expression> rightExpressions = new ArrayList<>();
    for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
        final RelDataType keyType = typeFactory.leastRestrictive(ImmutableList.of(left.getRowType().getFieldList().get(pair.left).getType(), right.getRowType().getFieldList().get(pair.right).getType()));
        final Type keyClass = typeFactory.getJavaClass(keyType);
        leftExpressions.add(Types.castIfNecessary(keyClass, leftResult.physType.fieldReference(left_, pair.left)));
        rightExpressions.add(Types.castIfNecessary(keyClass, rightResult.physType.fieldReference(right_, pair.right)));
    }
    final PhysType leftKeyPhysType = leftResult.physType.project(leftKeys, JavaRowFormat.LIST);
    final PhysType rightKeyPhysType = rightResult.physType.project(rightKeys, JavaRowFormat.LIST);
    return implementor.result(physType, builder.append(Expressions.call(BuiltInMethod.MERGE_JOIN.method, Expressions.list(leftExpression, rightExpression, Expressions.lambda(leftKeyPhysType.record(leftExpressions), left_), Expressions.lambda(rightKeyPhysType.record(rightExpressions), right_), EnumUtils.joinSelector(joinType, physType, ImmutableList.of(leftResult.physType, rightResult.physType)), Expressions.constant(joinType.generatesNullsOnLeft()), Expressions.constant(joinType.generatesNullsOnRight())))).toBlock());
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) JoinRelType(org.apache.calcite.rel.core.JoinRelType) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) BlockBuilder(org.apache.calcite.linq4j.tree.BlockBuilder)

Example 70 with RelDataType

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataType in project calcite by apache.

the class EnumerableWindow method declareAndResetState.

private void declareAndResetState(final JavaTypeFactory typeFactory, BlockBuilder builder, final Result result, int windowIdx, List<AggImpState> aggs, PhysType outputPhysType, List<Expression> outputRow) {
    for (final AggImpState agg : aggs) {
        agg.context = new WinAggContext() {

            public SqlAggFunction aggregation() {
                return agg.call.getAggregation();
            }

            public RelDataType returnRelType() {
                return agg.call.type;
            }

            public Type returnType() {
                return EnumUtils.javaClass(typeFactory, returnRelType());
            }

            public List<? extends Type> parameterTypes() {
                return EnumUtils.fieldTypes(typeFactory, parameterRelTypes());
            }

            public List<? extends RelDataType> parameterRelTypes() {
                return EnumUtils.fieldRowTypes(result.physType.getRowType(), constants, agg.call.getArgList());
            }

            public List<ImmutableBitSet> groupSets() {
                throw new UnsupportedOperationException();
            }

            public List<Integer> keyOrdinals() {
                throw new UnsupportedOperationException();
            }

            public List<? extends RelDataType> keyRelTypes() {
                throw new UnsupportedOperationException();
            }

            public List<? extends Type> keyTypes() {
                throw new UnsupportedOperationException();
            }
        };
        String aggName = "a" + agg.aggIdx;
        if (CalcitePrepareImpl.DEBUG) {
            aggName = Util.toJavaId(agg.call.getAggregation().getName(), 0).substring("ID$0$".length()) + aggName;
        }
        List<Type> state = agg.implementor.getStateType(agg.context);
        final List<Expression> decls = new ArrayList<Expression>(state.size());
        for (int i = 0; i < state.size(); i++) {
            Type type = state.get(i);
            ParameterExpression pe = Expressions.parameter(type, builder.newName(aggName + "s" + i + "w" + windowIdx));
            builder.add(Expressions.declare(0, pe, null));
            decls.add(pe);
        }
        agg.state = decls;
        Type aggHolderType = agg.context.returnType();
        Type aggStorageType = outputPhysType.getJavaFieldType(outputRow.size());
        if (Primitive.is(aggHolderType) && !Primitive.is(aggStorageType)) {
            aggHolderType = Primitive.box(aggHolderType);
        }
        ParameterExpression aggRes = Expressions.parameter(0, aggHolderType, builder.newName(aggName + "w" + windowIdx));
        builder.add(Expressions.declare(0, aggRes, Expressions.constant(Primitive.is(aggRes.getType()) ? Primitive.of(aggRes.getType()).defaultValue : null, aggRes.getType())));
        agg.result = aggRes;
        outputRow.add(aggRes);
        agg.implementor.implementReset(agg.context, new WinAggResetContextImpl(builder, agg.state, null, null, null, null, null, null));
    }
}
Also used : WinAggResetContextImpl(org.apache.calcite.adapter.enumerable.impl.WinAggResetContextImpl) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) RelDataType(org.apache.calcite.rel.type.RelDataType) Type(java.lang.reflect.Type) BinaryExpression(org.apache.calcite.linq4j.tree.BinaryExpression) Expression(org.apache.calcite.linq4j.tree.Expression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)834 RexNode (org.apache.calcite.rex.RexNode)268 ArrayList (java.util.ArrayList)214 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)209 RelNode (org.apache.calcite.rel.RelNode)153 SqlNode (org.apache.calcite.sql.SqlNode)143 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)123 RexBuilder (org.apache.calcite.rex.RexBuilder)118 Test (org.junit.Test)62 ImmutableList (com.google.common.collect.ImmutableList)58 RexInputRef (org.apache.calcite.rex.RexInputRef)57 List (java.util.List)51 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)45 RexLiteral (org.apache.calcite.rex.RexLiteral)44 SqlNodeList (org.apache.calcite.sql.SqlNodeList)42 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)40 AggregateCall (org.apache.calcite.rel.core.AggregateCall)39 BitString (org.apache.calcite.util.BitString)38 BigDecimal (java.math.BigDecimal)35 RelBuilder (org.apache.calcite.tools.RelBuilder)34