Search in sources :

Example 36 with RelDataTypeFactory

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

the class HiveAggregateReduceFunctionsRule method reduceStddev.

private RexNode reduceStddev(Aggregate oldAggRel, AggregateCall oldCall, boolean biased, boolean sqrt, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
    // stddev_pop(x) ==>
    // power(
    // (sum(x * x) - sum(x) * sum(x) / count(x))
    // / count(x),
    // .5)
    // 
    // stddev_samp(x) ==>
    // power(
    // (sum(x * x) - sum(x) * sum(x) / count(x))
    // / nullif(count(x) - 1, 0),
    // .5)
    final int nGroups = oldAggRel.getGroupCount();
    final RelOptCluster cluster = oldAggRel.getCluster();
    final RexBuilder rexBuilder = cluster.getRexBuilder();
    final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
    assert oldCall.getArgList().size() == 1 : oldCall.getArgList();
    final int argOrdinal = oldCall.getArgList().get(0);
    final RelDataType argOrdinalType = getFieldType(oldAggRel.getInput(), argOrdinal);
    final RelDataType oldCallType = typeFactory.createTypeWithNullability(oldCall.getType(), true);
    final RexNode argRef = rexBuilder.ensureType(oldCallType, inputExprs.get(argOrdinal), false);
    final int argRefOrdinal = lookupOrAdd(inputExprs, argRef);
    final RelDataType sumReturnType = getSumReturnType(rexBuilder.getTypeFactory(), argRef.getType());
    final RexNode argSquared = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, argRef, argRef);
    final int argSquaredOrdinal = lookupOrAdd(inputExprs, argSquared);
    final RelDataType sumSquaredReturnType = getSumReturnType(rexBuilder.getTypeFactory(), argSquared.getType());
    final AggregateCall sumArgSquaredAggCall = createAggregateCallWithBinding(typeFactory, new HiveSqlSumAggFunction(oldCall.isDistinct(), ReturnTypes.explicit(sumSquaredReturnType), InferTypes.explicit(Collections.singletonList(argSquared.getType())), // SqlStdOperatorTable.SUM,
    oldCall.getAggregation().getOperandTypeChecker()), argSquared.getType(), oldAggRel, oldCall, argSquaredOrdinal);
    final RexNode sumArgSquared = rexBuilder.addAggCall(sumArgSquaredAggCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(sumArgSquaredAggCall.getType()));
    final AggregateCall sumArgAggCall = AggregateCall.create(new HiveSqlSumAggFunction(oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), InferTypes.explicit(Collections.singletonList(argOrdinalType)), // SqlStdOperatorTable.SUM,
    oldCall.getAggregation().getOperandTypeChecker()), oldCall.isDistinct(), oldCall.isApproximate(), ImmutableIntList.of(argRefOrdinal), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), null, null);
    final RexNode sumArg = rexBuilder.addAggCall(sumArgAggCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(sumArgAggCall.getType()));
    final RexNode sumArgCast = rexBuilder.ensureType(oldCallType, sumArg, true);
    final RexNode sumSquaredArg = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, sumArgCast, sumArgCast);
    RelDataType countRetType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT), true);
    final AggregateCall countArgAggCall = AggregateCall.create(new HiveSqlCountAggFunction(oldCall.isDistinct(), ReturnTypes.explicit(countRetType), oldCall.getAggregation().getOperandTypeInference(), // SqlStdOperatorTable.COUNT,
    oldCall.getAggregation().getOperandTypeChecker()), oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), oldCall.filterArg, oldAggRel.getGroupCount(), oldAggRel.getInput(), countRetType, null);
    final RexNode countArg = rexBuilder.addAggCall(countArgAggCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(argOrdinalType));
    final RexNode avgSumSquaredArg = rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, sumSquaredArg, countArg);
    final RexNode diff = rexBuilder.makeCall(SqlStdOperatorTable.MINUS, sumArgSquared, avgSumSquaredArg);
    final RexNode denominator;
    if (biased) {
        denominator = countArg;
    } else {
        final RexLiteral one = rexBuilder.makeExactLiteral(BigDecimal.ONE);
        final RexNode nul = rexBuilder.makeCast(countArg.getType(), rexBuilder.constantNull());
        final RexNode countMinusOne = rexBuilder.makeCall(SqlStdOperatorTable.MINUS, countArg, one);
        final RexNode countEqOne = rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, countArg, one);
        denominator = rexBuilder.makeCall(SqlStdOperatorTable.CASE, countEqOne, nul, countMinusOne);
    }
    final RexNode div = rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, diff, denominator);
    RexNode result = div;
    if (sqrt) {
        final RexNode half = rexBuilder.makeExactLiteral(new BigDecimal("0.5"));
        result = rexBuilder.makeCall(SqlStdOperatorTable.POWER, div, half);
    }
    return rexBuilder.makeCast(oldCall.getType(), result);
}
Also used : RelOptCluster(org.apache.calcite.plan.RelOptCluster) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RexLiteral(org.apache.calcite.rex.RexLiteral) HiveSqlCountAggFunction(org.apache.hadoop.hive.ql.optimizer.calcite.functions.HiveSqlCountAggFunction) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) HiveSqlSumAggFunction(org.apache.hadoop.hive.ql.optimizer.calcite.functions.HiveSqlSumAggFunction) BigDecimal(java.math.BigDecimal) RexNode(org.apache.calcite.rex.RexNode)

Example 37 with RelDataTypeFactory

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

the class CassandraTable method query.

/**
 * Executes a CQL query on the underlying table.
 *
 * @param session Cassandra session
 * @param fields List of fields to project
 * @param predicates A list of predicates which should be used in the query
 * @return Enumerator of results
 */
public Enumerable<Object> query(final Session session, List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, List<String> predicates, List<String> order, final Integer offset, final Integer fetch) {
    // Build the type of the resulting row based on the provided fields
    final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
    final RelDataType rowType = getRowType(typeFactory);
    Function1<String, Void> addField = new Function1<String, Void>() {

        public Void apply(String fieldName) {
            SqlTypeName typeName = rowType.getField(fieldName, true, false).getType().getSqlTypeName();
            fieldInfo.add(fieldName, typeFactory.createSqlType(typeName)).nullable(true);
            return null;
        }
    };
    if (selectFields.isEmpty()) {
        for (Map.Entry<String, Class> field : fields) {
            addField.apply(field.getKey());
        }
    } else {
        for (Map.Entry<String, String> field : selectFields) {
            addField.apply(field.getKey());
        }
    }
    final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
    // Construct the list of fields to project
    final String selectString;
    if (selectFields.isEmpty()) {
        selectString = "*";
    } else {
        selectString = Util.toString(new Iterable<String>() {

            public Iterator<String> iterator() {
                final Iterator<Map.Entry<String, String>> selectIterator = selectFields.iterator();
                return new Iterator<String>() {

                    @Override
                    public boolean hasNext() {
                        return selectIterator.hasNext();
                    }

                    @Override
                    public String next() {
                        Map.Entry<String, String> entry = selectIterator.next();
                        return entry.getKey() + " AS " + entry.getValue();
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        }, "", ", ", "");
    }
    // Combine all predicates conjunctively
    String whereClause = "";
    if (!predicates.isEmpty()) {
        whereClause = " WHERE ";
        whereClause += Util.toString(predicates, "", " AND ", "");
    }
    // Build and issue the query and return an Enumerator over the results
    StringBuilder queryBuilder = new StringBuilder("SELECT ");
    queryBuilder.append(selectString);
    queryBuilder.append(" FROM \"" + columnFamily + "\"");
    queryBuilder.append(whereClause);
    if (!order.isEmpty()) {
        queryBuilder.append(Util.toString(order, " ORDER BY ", ", ", ""));
    }
    int limit = offset;
    if (fetch >= 0) {
        limit += fetch;
    }
    if (limit > 0) {
        queryBuilder.append(" LIMIT " + limit);
    }
    queryBuilder.append(" ALLOW FILTERING");
    final String query = queryBuilder.toString();
    return new AbstractEnumerable<Object>() {

        public Enumerator<Object> enumerator() {
            final ResultSet results = session.execute(query);
            // Skip results until we get to the right offset
            int skip = 0;
            Enumerator<Object> enumerator = new CassandraEnumerator(results, resultRowType);
            while (skip < offset && enumerator.moveNext()) {
                skip++;
            }
            return enumerator;
        }
    };
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataType(org.apache.calcite.rel.type.RelDataType) RelProtoDataType(org.apache.calcite.rel.type.RelProtoDataType) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) Iterator(java.util.Iterator) AbstractEnumerable(org.apache.calcite.linq4j.AbstractEnumerable) ResultSet(com.datastax.driver.core.ResultSet) Function1(org.apache.calcite.linq4j.function.Function1) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) Map(java.util.Map)

Example 38 with RelDataTypeFactory

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

the class RelMdColumnUniqueness method areColumnsUnique.

public Boolean areColumnsUnique(Project rel, RelMetadataQuery mq, ImmutableBitSet columns, boolean ignoreNulls) {
    // LogicalProject maps a set of rows to a different set;
    // Without knowledge of the mapping function(whether it
    // preserves uniqueness), it is only safe to derive uniqueness
    // info from the child of a project when the mapping is f(a) => a.
    // 
    // Also need to map the input column set to the corresponding child
    // references
    List<RexNode> projExprs = rel.getProjects();
    ImmutableBitSet.Builder childColumns = ImmutableBitSet.builder();
    for (int bit : columns) {
        RexNode projExpr = projExprs.get(bit);
        if (projExpr instanceof RexInputRef) {
            childColumns.set(((RexInputRef) projExpr).getIndex());
        } else if (projExpr instanceof RexCall && ignoreNulls) {
            // If the expression is a cast such that the types are the same
            // except for the nullability, then if we're ignoring nulls,
            // it doesn't matter whether the underlying column reference
            // is nullable.  Check that the types are the same by making a
            // nullable copy of both types and then comparing them.
            RexCall call = (RexCall) projExpr;
            if (call.getOperator() != SqlStdOperatorTable.CAST) {
                continue;
            }
            RexNode castOperand = call.getOperands().get(0);
            if (!(castOperand instanceof RexInputRef)) {
                continue;
            }
            RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
            RelDataType castType = typeFactory.createTypeWithNullability(projExpr.getType(), true);
            RelDataType origType = typeFactory.createTypeWithNullability(castOperand.getType(), true);
            if (castType.equals(origType)) {
                childColumns.set(((RexInputRef) castOperand).getIndex());
            }
        } else {
            // projection, then skip it.
            continue;
        }
    }
    // If no columns can affect uniqueness, then return unknown
    if (childColumns.cardinality() == 0) {
        return null;
    }
    return mq.areColumnsUnique(rel.getInput(), childColumns.build(), ignoreNulls);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 39 with RelDataTypeFactory

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

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.type.RelDataTypeFactory 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)

Aggregations

RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)263 RelDataType (org.apache.calcite.rel.type.RelDataType)211 RexNode (org.apache.calcite.rex.RexNode)78 RexBuilder (org.apache.calcite.rex.RexBuilder)67 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)55 ArrayList (java.util.ArrayList)51 SqlTypeFactoryImpl (org.apache.calcite.sql.type.SqlTypeFactoryImpl)43 RelNode (org.apache.calcite.rel.RelNode)40 Test (org.junit.jupiter.api.Test)36 List (java.util.List)27 SqlNode (org.apache.calcite.sql.SqlNode)26 BigDecimal (java.math.BigDecimal)21 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)21 RelOptCluster (org.apache.calcite.plan.RelOptCluster)19 AggregateCall (org.apache.calcite.rel.core.AggregateCall)17 ImmutableList (com.google.common.collect.ImmutableList)15 Map (java.util.Map)15 JoinRelType (org.apache.calcite.rel.core.JoinRelType)15 SqlOperator (org.apache.calcite.sql.SqlOperator)15 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)14