Search in sources :

Example 31 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 Aggregate method deriveRowType.

/**
 * Computes the row type of an {@code Aggregate} before it exists.
 *
 * @param typeFactory Type factory
 * @param inputRowType Input row type
 * @param indicator Whether row type should include indicator fields to
 *                 indicate which grouping set is active; must be true if
 *                 aggregate is not simple
 * @param groupSet Bit set of grouping fields
 * @param groupSets List of all grouping sets; null for just {@code groupSet}
 * @param aggCalls Collection of calls to aggregate functions
 * @return Row type of the aggregate
 */
public static RelDataType deriveRowType(RelDataTypeFactory typeFactory, final RelDataType inputRowType, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, final List<AggregateCall> aggCalls) {
    final List<Integer> groupList = groupSet.asList();
    assert groupList.size() == groupSet.cardinality();
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    final List<RelDataTypeField> fieldList = inputRowType.getFieldList();
    final Set<String> containedNames = Sets.newHashSet();
    for (int groupKey : groupList) {
        final RelDataTypeField field = fieldList.get(groupKey);
        containedNames.add(field.getName());
        builder.add(field);
        if (groupSets != null && !allContain(groupSets, groupKey)) {
            builder.nullable(true);
        }
    }
    if (indicator) {
        for (int groupKey : groupList) {
            final RelDataType booleanType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BOOLEAN), false);
            final String base = "i$" + fieldList.get(groupKey).getName();
            String name = base;
            int i = 0;
            while (containedNames.contains(name)) {
                name = base + "_" + i++;
            }
            containedNames.add(name);
            builder.add(name, booleanType);
        }
    }
    for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
        final String base;
        if (aggCall.e.name != null) {
            base = aggCall.e.name;
        } else {
            base = "$f" + (groupList.size() + aggCall.i);
        }
        String name = base;
        int i = 0;
        while (containedNames.contains(name)) {
            name = base + "_" + i++;
        }
        containedNames.add(name);
        builder.add(name, aggCall.e.type);
    }
    return builder.build();
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 32 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 Aggregate method typeMatchesInferred.

/**
 * Returns whether the inferred type of an {@link AggregateCall} matches the
 * type it was given when it was created.
 *
 * @param aggCall Aggregate call
 * @param litmus What to do if an error is detected (types do not match)
 * @return Whether the inferred and declared types match
 */
private boolean typeMatchesInferred(final AggregateCall aggCall, final Litmus litmus) {
    SqlAggFunction aggFunction = aggCall.getAggregation();
    AggCallBinding callBinding = aggCall.createBinding(this);
    RelDataType type = aggFunction.inferReturnType(callBinding);
    RelDataType expectedType = aggCall.type;
    return RelOptUtil.eq("aggCall type", expectedType, "inferred type", type, litmus);
}
Also used : RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction)

Example 33 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 AggregateCall method create.

/**
 * Creates an AggregateCall, inferring its type if {@code type} is null.
 */
public static AggregateCall create(SqlAggFunction aggFunction, boolean distinct, boolean approximate, List<Integer> argList, int filterArg, int groupCount, RelNode input, RelDataType type, String name) {
    if (type == null) {
        final RelDataTypeFactory typeFactory = input.getCluster().getTypeFactory();
        final List<RelDataType> types = SqlTypeUtil.projectTypes(input.getRowType(), argList);
        final Aggregate.AggCallBinding callBinding = new Aggregate.AggCallBinding(typeFactory, aggFunction, types, groupCount, filterArg >= 0);
        type = aggFunction.inferReturnType(callBinding);
    }
    return create(aggFunction, distinct, approximate, argList, filterArg, type, name);
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 34 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 Collect method deriveCollectRowType.

/**
 * Derives the output type of a collect relational expression.
 *
 * @param rel       relational expression
 * @param fieldName name of sole output field
 * @return output type of a collect relational expression
 */
public static RelDataType deriveCollectRowType(SingleRel rel, String fieldName) {
    RelDataType childType = rel.getInput().getRowType();
    assert childType.isStruct();
    final RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
    RelDataType ret = SqlTypeUtil.createMultisetType(typeFactory, childType, false);
    ret = typeFactory.builder().add(fieldName, ret).build();
    return typeFactory.createTypeWithNullability(ret, false);
}
Also used : RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 35 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 LogicalProject method create.

// ~ Methods ----------------------------------------------------------------
/**
 * Creates a LogicalProject.
 */
public static LogicalProject create(final RelNode input, final List<? extends RexNode> projects, List<String> fieldNames) {
    final RelOptCluster cluster = input.getCluster();
    final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), projects, fieldNames, SqlValidatorUtil.F_SUGGESTER);
    return create(input, projects, rowType);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) RelDataType(org.apache.calcite.rel.type.RelDataType)

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