Search in sources :

Example 41 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project calcite by apache.

the class RelBuilder method aggregate.

/**
 * Creates an {@link org.apache.calcite.rel.core.Aggregate} with a list of
 * calls.
 */
public RelBuilder aggregate(GroupKey groupKey, Iterable<AggCall> aggCalls) {
    final Registrar registrar = new Registrar();
    registrar.extraNodes.addAll(fields());
    registrar.names.addAll(peek().getRowType().getFieldNames());
    final GroupKeyImpl groupKey_ = (GroupKeyImpl) groupKey;
    final ImmutableBitSet groupSet = ImmutableBitSet.of(registrar.registerExpressions(groupKey_.nodes));
    label: if (Iterables.isEmpty(aggCalls) && !groupKey_.indicator) {
        final RelMetadataQuery mq = peek().getCluster().getMetadataQuery();
        if (groupSet.isEmpty()) {
            final Double minRowCount = mq.getMinRowCount(peek());
            if (minRowCount == null || minRowCount < 1D) {
                // empty.
                break label;
            }
        }
        if (registrar.extraNodes.size() == fields().size()) {
            final Boolean unique = mq.areColumnsUnique(peek(), groupSet);
            if (unique != null && unique) {
                // Rel is already unique.
                return project(fields(groupSet.asList()));
            }
        }
        final Double maxRowCount = mq.getMaxRowCount(peek());
        if (maxRowCount != null && maxRowCount <= 1D) {
            // If there is at most one row, rel is already unique.
            return this;
        }
    }
    final ImmutableList<ImmutableBitSet> groupSets;
    if (groupKey_.nodeLists != null) {
        final int sizeBefore = registrar.extraNodes.size();
        final SortedSet<ImmutableBitSet> groupSetSet = new TreeSet<>(ImmutableBitSet.ORDERING);
        for (ImmutableList<RexNode> nodeList : groupKey_.nodeLists) {
            final ImmutableBitSet groupSet2 = ImmutableBitSet.of(registrar.registerExpressions(nodeList));
            if (!groupSet.contains(groupSet2)) {
                throw new IllegalArgumentException("group set element " + nodeList + " must be a subset of group key");
            }
            groupSetSet.add(groupSet2);
        }
        groupSets = ImmutableList.copyOf(groupSetSet);
        if (registrar.extraNodes.size() > sizeBefore) {
            throw new IllegalArgumentException("group sets contained expressions not in group key: " + registrar.extraNodes.subList(sizeBefore, registrar.extraNodes.size()));
        }
    } else {
        groupSets = ImmutableList.of(groupSet);
    }
    for (AggCall aggCall : aggCalls) {
        if (aggCall instanceof AggCallImpl) {
            final AggCallImpl aggCall1 = (AggCallImpl) aggCall;
            registrar.registerExpressions(aggCall1.operands);
            if (aggCall1.filter != null) {
                registrar.registerExpression(aggCall1.filter);
            }
        }
    }
    project(registrar.extraNodes);
    rename(registrar.names);
    final Frame frame = stack.pop();
    final RelNode r = frame.rel;
    final List<AggregateCall> aggregateCalls = new ArrayList<>();
    for (AggCall aggCall : aggCalls) {
        final AggregateCall aggregateCall;
        if (aggCall instanceof AggCallImpl) {
            final AggCallImpl aggCall1 = (AggCallImpl) aggCall;
            final List<Integer> args = registrar.registerExpressions(aggCall1.operands);
            final int filterArg = aggCall1.filter == null ? -1 : registrar.registerExpression(aggCall1.filter);
            if (aggCall1.distinct && !aggCall1.aggFunction.isQuantifierAllowed()) {
                throw new IllegalArgumentException("DISTINCT not allowed");
            }
            if (aggCall1.filter != null && !aggCall1.aggFunction.allowsFilter()) {
                throw new IllegalArgumentException("FILTER not allowed");
            }
            aggregateCall = AggregateCall.create(aggCall1.aggFunction, aggCall1.distinct, aggCall1.approximate, args, filterArg, groupSet.cardinality(), r, null, aggCall1.alias);
        } else {
            aggregateCall = ((AggCallImpl2) aggCall).aggregateCall;
        }
        aggregateCalls.add(aggregateCall);
    }
    assert ImmutableBitSet.ORDERING.isStrictlyOrdered(groupSets) : groupSets;
    for (ImmutableBitSet set : groupSets) {
        assert groupSet.contains(set);
    }
    RelNode aggregate = aggregateFactory.createAggregate(r, groupKey_.indicator, groupSet, groupSets, aggregateCalls);
    // build field list
    final ImmutableList.Builder<Field> fields = ImmutableList.builder();
    final List<RelDataTypeField> aggregateFields = aggregate.getRowType().getFieldList();
    int i = 0;
    // first, group fields
    for (Integer groupField : groupSet.asList()) {
        RexNode node = registrar.extraNodes.get(groupField);
        final SqlKind kind = node.getKind();
        switch(kind) {
            case INPUT_REF:
                fields.add(frame.fields.get(((RexInputRef) node).getIndex()));
                break;
            default:
                String name = aggregateFields.get(i).getName();
                RelDataTypeField fieldType = new RelDataTypeFieldImpl(name, i, node.getType());
                fields.add(new Field(ImmutableSet.<String>of(), fieldType));
                break;
        }
        i++;
    }
    // second, indicator fields (copy from aggregate rel type)
    if (groupKey_.indicator) {
        for (int j = 0; j < groupSet.cardinality(); ++j) {
            final RelDataTypeField field = aggregateFields.get(i);
            final RelDataTypeField fieldType = new RelDataTypeFieldImpl(field.getName(), i, field.getType());
            fields.add(new Field(ImmutableSet.<String>of(), fieldType));
            i++;
        }
    }
    // third, aggregate fields. retain `i' as field index
    for (int j = 0; j < aggregateCalls.size(); ++j) {
        final AggregateCall call = aggregateCalls.get(j);
        final RelDataTypeField fieldType = new RelDataTypeFieldImpl(aggregateFields.get(i + j).getName(), i + j, call.getType());
        fields.add(new Field(ImmutableSet.<String>of(), fieldType));
    }
    stack.push(new Frame(aggregate, fields.build()));
    return this;
}
Also used : RelMetadataQuery(org.apache.calcite.rel.metadata.RelMetadataQuery) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) NlsString(org.apache.calcite.util.NlsString) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) TreeSet(java.util.TreeSet) RelDataTypeFieldImpl(org.apache.calcite.rel.type.RelDataTypeFieldImpl) SqlKind(org.apache.calcite.sql.SqlKind) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Example 42 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class FindFiltersForCollation method visitCall.

@Override
public Boolean visitCall(RexCall call) {
    final SqlOperator op = call.getOperator();
    final SqlKind kind = op.getKind();
    if (kind == SqlKind.AND) {
        for (RexNode n : call.getOperands()) {
            n.accept(this);
        }
    } else if (kind == SqlKind.CAST) {
        // visited by visitInputRef()).
        return true;
    } else if (op == SqlStdOperatorTable.ITEM) {
        List<RexNode> ops = call.getOperands();
        boolean left = ops.get(0).accept(this);
        boolean right = ops.get(1).accept(this);
        return left && right;
    } else if (allowedComparisons.contains(kind)) {
        List<RexNode> ops = call.getOperands();
        boolean left = ops.get(0).accept(this);
        boolean right = ops.get(1).accept(this);
        if (left && right) {
            if (collationFilterMap.containsKey(currentFieldIndex)) {
                List<RexNode> n = collationFilterMap.get(currentFieldIndex);
                n.add(call);
            } else {
                List<RexNode> clist = Lists.newArrayList();
                clist.add(call);
                collationFilterMap.put(currentFieldIndex, clist);
            }
            return true;
        }
    }
    return false;
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) List(java.util.List) SqlKind(org.apache.calcite.sql.SqlKind) RexNode(org.apache.calcite.rex.RexNode)

Example 43 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class JoinPrel method buildJoinConditions.

/**
 * Build the list of join conditions for this join.
 * A join condition is built only for equality and IS NOT DISTINCT FROM comparisons. The difference is:
 * null == null is FALSE whereas null IS NOT DISTINCT FROM null is TRUE
 * For a use case of the IS NOT DISTINCT FROM comparison, see
 * {@link org.apache.calcite.rel.rules.AggregateRemoveRule}
 * @param conditions populated list of join conditions
 * @param leftFields join fields from the left input
 * @param rightFields join fields from the right input
 */
protected void buildJoinConditions(List<JoinCondition> conditions, List<String> leftFields, List<String> rightFields, List<Integer> leftKeys, List<Integer> rightKeys) {
    List<RexNode> conjuncts = RelOptUtil.conjunctions(this.getCondition());
    short i = 0;
    for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
        final RexNode conditionExpr = conjuncts.get(i++);
        SqlKind kind = conditionExpr.getKind();
        if (kind != SqlKind.EQUALS && kind != SqlKind.IS_NOT_DISTINCT_FROM) {
            throw UserException.unsupportedError().message("Unsupported comparator in join condition %s", conditionExpr).build(logger);
        }
        conditions.add(new JoinCondition(kind.toString(), FieldReference.getWithQuotedRef(leftFields.get(pair.left)), FieldReference.getWithQuotedRef(rightFields.get(pair.right))));
    }
}
Also used : SqlKind(org.apache.calcite.sql.SqlKind) RexNode(org.apache.calcite.rex.RexNode) JoinCondition(org.apache.drill.common.logical.data.JoinCondition)

Example 44 with SqlKind

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by apache.

the class RewriteCombineBinaryOperators method visitCall.

@Override
public RexNode visitCall(RexCall call) {
    SqlOperator op = call.getOperator();
    SqlKind kind = op.getKind();
    RelDataType type = call.getType();
    if (kind == SqlKind.AND) {
        List<RexNode> conjuncts = Lists.newArrayList();
        for (RexNode child : call.getOperands()) {
            conjuncts.addAll(RelOptUtil.conjunctions(child.accept(this)));
        }
        return RexUtil.composeConjunction(builder, conjuncts, true);
    }
    if (kind == SqlKind.OR) {
        List<RexNode> disjuncts = Lists.newArrayList();
        for (RexNode child : call.getOperands()) {
            disjuncts.addAll(RelOptUtil.disjunctions(child.accept(this)));
        }
        return RexUtil.composeDisjunction(builder, disjuncts, true);
    }
    return builder.makeCall(type, op, visitChildren(call));
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlKind(org.apache.calcite.sql.SqlKind) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

SqlKind (org.apache.calcite.sql.SqlKind)44 RexNode (org.apache.calcite.rex.RexNode)21 ArrayList (java.util.ArrayList)16 SqlOperator (org.apache.calcite.sql.SqlOperator)14 RelDataType (org.apache.calcite.rel.type.RelDataType)11 RexCall (org.apache.calcite.rex.RexCall)10 RexInputRef (org.apache.calcite.rex.RexInputRef)8 List (java.util.List)7 AggregateCall (org.apache.calcite.rel.core.AggregateCall)7 RexBuilder (org.apache.calcite.rex.RexBuilder)7 SqlNode (org.apache.calcite.sql.SqlNode)6 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)6 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)5 SqlCall (org.apache.calcite.sql.SqlCall)5 ImmutableList (com.google.common.collect.ImmutableList)4 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)4 Aggregate (org.apache.calcite.rel.core.Aggregate)3 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)3 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)3