Search in sources :

Example 6 with AggregateCall

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall in project flink by apache.

the class FlinkAggregateExpandDistinctAggregatesRule method onMatch.

//~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
    final Aggregate aggregate = call.rel(0);
    if (!aggregate.containsDistinctCall()) {
        return;
    }
    // Find all of the agg expressions. We use a LinkedHashSet to ensure
    // determinism.
    int nonDistinctCount = 0;
    int distinctCount = 0;
    int filterCount = 0;
    int unsupportedAggCount = 0;
    final Set<Pair<List<Integer>, Integer>> argLists = new LinkedHashSet<>();
    for (AggregateCall aggCall : aggregate.getAggCallList()) {
        if (aggCall.filterArg >= 0) {
            ++filterCount;
        }
        if (!aggCall.isDistinct()) {
            ++nonDistinctCount;
            if (!(aggCall.getAggregation() instanceof SqlCountAggFunction || aggCall.getAggregation() instanceof SqlSumAggFunction || aggCall.getAggregation() instanceof SqlMinMaxAggFunction)) {
                ++unsupportedAggCount;
            }
            continue;
        }
        ++distinctCount;
        argLists.add(Pair.of(aggCall.getArgList(), aggCall.filterArg));
    }
    Preconditions.checkState(argLists.size() > 0, "containsDistinctCall lied");
    // arguments then we can use a more efficient form.
    if (nonDistinctCount == 0 && argLists.size() == 1) {
        final Pair<List<Integer>, Integer> pair = Iterables.getOnlyElement(argLists);
        final RelBuilder relBuilder = call.builder();
        convertMonopole(relBuilder, aggregate, pair.left, pair.right);
        call.transformTo(relBuilder.build());
        return;
    }
    if (useGroupingSets) {
        rewriteUsingGroupingSets(call, aggregate, argLists);
        return;
    }
    // we can generate multi-phase aggregates
    if (// one distinct aggregate
    distinctCount == 1 && // no filter
    filterCount == 0 && // sum/min/max/count in non-distinct aggregate
    unsupportedAggCount == 0 && nonDistinctCount > 0) {
        // one or more non-distinct aggregates
        final RelBuilder relBuilder = call.builder();
        convertSingletonDistinct(relBuilder, aggregate, argLists);
        call.transformTo(relBuilder.build());
        return;
    }
    // Create a list of the expressions which will yield the final result.
    // Initially, the expressions point to the input field.
    final List<RelDataTypeField> aggFields = aggregate.getRowType().getFieldList();
    final List<RexInputRef> refs = new ArrayList<>();
    final List<String> fieldNames = aggregate.getRowType().getFieldNames();
    final ImmutableBitSet groupSet = aggregate.getGroupSet();
    final int groupAndIndicatorCount = aggregate.getGroupCount() + aggregate.getIndicatorCount();
    for (int i : Util.range(groupAndIndicatorCount)) {
        refs.add(RexInputRef.of(i, aggFields));
    }
    // Aggregate the original relation, including any non-distinct aggregates.
    final List<AggregateCall> newAggCallList = new ArrayList<>();
    int i = -1;
    for (AggregateCall aggCall : aggregate.getAggCallList()) {
        ++i;
        if (aggCall.isDistinct()) {
            refs.add(null);
            continue;
        }
        refs.add(new RexInputRef(groupAndIndicatorCount + newAggCallList.size(), aggFields.get(groupAndIndicatorCount + i).getType()));
        newAggCallList.add(aggCall);
    }
    // In the case where there are no non-distinct aggregates (regardless of
    // whether there are group bys), there's no need to generate the
    // extra aggregate and join.
    final RelBuilder relBuilder = call.builder();
    relBuilder.push(aggregate.getInput());
    int n = 0;
    if (!newAggCallList.isEmpty()) {
        final RelBuilder.GroupKey groupKey = relBuilder.groupKey(groupSet, aggregate.indicator, aggregate.getGroupSets());
        relBuilder.aggregate(groupKey, newAggCallList);
        ++n;
    }
    // set of operands.
    for (Pair<List<Integer>, Integer> argList : argLists) {
        doRewrite(relBuilder, aggregate, n++, argList.left, argList.right, refs);
    }
    relBuilder.project(refs, fieldNames);
    call.transformTo(relBuilder.build());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RelBuilder(org.apache.calcite.tools.RelBuilder) SqlMinMaxAggFunction(org.apache.calcite.sql.fun.SqlMinMaxAggFunction) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ArrayList(java.util.ArrayList) SqlCountAggFunction(org.apache.calcite.sql.fun.SqlCountAggFunction) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) SqlSumAggFunction(org.apache.calcite.sql.fun.SqlSumAggFunction) RexInputRef(org.apache.calcite.rex.RexInputRef) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableIntList(org.apache.calcite.util.ImmutableIntList) List(java.util.List) Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) Pair(org.apache.calcite.util.Pair)

Example 7 with AggregateCall

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall in project lucene-solr by apache.

the class SolrAggregate method implement.

public void implement(Implementor implementor) {
    implementor.visitChild(0, getInput());
    final List<String> inNames = SolrRules.solrFieldNames(getInput().getRowType());
    for (Pair<AggregateCall, String> namedAggCall : getNamedAggCalls()) {
        AggregateCall aggCall = namedAggCall.getKey();
        Pair<String, String> metric = toSolrMetric(implementor, aggCall, inNames);
        implementor.addReverseAggMapping(namedAggCall.getValue(), metric.getKey().toLowerCase(Locale.ROOT) + "(" + metric.getValue() + ")");
        implementor.addMetricPair(namedAggCall.getValue(), metric.getKey(), metric.getValue());
    /*
      if(aggCall.getName() == null) {
        System.out.println("AGG:"+namedAggCall.getValue()+":"+ aggCall.getAggregation().getName() + "(" + inNames.get(aggCall.getArgList().get(0)) + ")");
        implementor.addFieldMapping(namedAggCall.getValue(),
          aggCall.getAggregation().getName() + "(" + inNames.get(aggCall.getArgList().get(0)) + ")");
      }
      */
    }
    for (int group : getGroupSet()) {
        String inName = inNames.get(group);
        implementor.addBucket(inName);
    }
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall)

Example 8 with AggregateCall

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall in project herddb by diennea.

the class CalcitePlanner method planAggregate.

private PlannerOp planAggregate(EnumerableAggregate op, RelDataType rowType, boolean returnValues) {
    List<RelDataTypeField> fieldList = op.getRowType().getFieldList();
    List<AggregateCall> calls = op.getAggCallList();
    String[] fieldnames = new String[fieldList.size()];
    String[] aggtypes = new String[calls.size()];
    Column[] columns = new Column[fieldList.size()];
    List<Integer> groupedFiledsIndexes = op.getGroupSet().toList();
    List<List<Integer>> argLists = new ArrayList<>(calls.size());
    int i = 0;
    int idaggcall = 0;
    for (RelDataTypeField c : fieldList) {
        int type = convertToHerdType(c.getType());
        Column co = Column.column(c.getName(), type);
        columns[i] = co;
        fieldnames[i] = c.getName().toLowerCase();
        i++;
    }
    for (AggregateCall call : calls) {
        aggtypes[idaggcall++] = call.getAggregation().getName();
        argLists.add(call.getArgList());
    }
    PlannerOp input = convertRelNode(op.getInput(), null, returnValues, false);
    return new AggregateOp(input, fieldnames, columns, aggtypes, argLists, groupedFiledsIndexes);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) AggregateOp(herddb.model.planner.AggregateOp) Column(herddb.model.Column) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 9 with AggregateCall

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall in project calcite by apache.

the class RelMdColumnOrigins method getColumnOrigins.

public Set<RelColumnOrigin> getColumnOrigins(Aggregate rel, RelMetadataQuery mq, int iOutputColumn) {
    if (iOutputColumn < rel.getGroupCount()) {
        // Group columns pass through directly.
        return mq.getColumnOrigins(rel.getInput(), iOutputColumn);
    }
    if (rel.indicator) {
        if (iOutputColumn < rel.getGroupCount() + rel.getIndicatorCount()) {
            // The indicator column is originated here.
            return ImmutableSet.of();
        }
    }
    // Aggregate columns are derived from input columns
    AggregateCall call = rel.getAggCallList().get(iOutputColumn - rel.getGroupCount() - rel.getIndicatorCount());
    final Set<RelColumnOrigin> set = new HashSet<>();
    for (Integer iInput : call.getArgList()) {
        Set<RelColumnOrigin> inputSet = mq.getColumnOrigins(rel.getInput(), iInput);
        inputSet = createDerivedColumnOrigins(inputSet);
        if (inputSet != null) {
            set.addAll(inputSet);
        }
    }
    return set;
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) HashSet(java.util.HashSet)

Example 10 with AggregateCall

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.core.AggregateCall in project calcite by apache.

the class RelJsonReader method readRel.

private void readRel(final Map<String, Object> jsonRel) {
    String id = (String) jsonRel.get("id");
    String type = (String) jsonRel.get("relOp");
    Constructor constructor = relJson.getConstructor(type);
    RelInput input = new RelInput() {

        public RelOptCluster getCluster() {
            return cluster;
        }

        public RelTraitSet getTraitSet() {
            return cluster.traitSetOf(Convention.NONE);
        }

        public RelOptTable getTable(String table) {
            final List<String> list = getStringList(table);
            return relOptSchema.getTableForMember(list);
        }

        public RelNode getInput() {
            final List<RelNode> inputs = getInputs();
            assert inputs.size() == 1;
            return inputs.get(0);
        }

        public List<RelNode> getInputs() {
            final List<String> jsonInputs = getStringList("inputs");
            if (jsonInputs == null) {
                return ImmutableList.of(lastRel);
            }
            final List<RelNode> inputs = new ArrayList<>();
            for (String jsonInput : jsonInputs) {
                inputs.add(lookupInput(jsonInput));
            }
            return inputs;
        }

        public RexNode getExpression(String tag) {
            return relJson.toRex(this, jsonRel.get(tag));
        }

        public ImmutableBitSet getBitSet(String tag) {
            return ImmutableBitSet.of(getIntegerList(tag));
        }

        public List<ImmutableBitSet> getBitSetList(String tag) {
            List<List<Integer>> list = getIntegerListList(tag);
            if (list == null) {
                return null;
            }
            final ImmutableList.Builder<ImmutableBitSet> builder = ImmutableList.builder();
            for (List<Integer> integers : list) {
                builder.add(ImmutableBitSet.of(integers));
            }
            return builder.build();
        }

        public List<String> getStringList(String tag) {
            // noinspection unchecked
            return (List<String>) jsonRel.get(tag);
        }

        public List<Integer> getIntegerList(String tag) {
            // noinspection unchecked
            return (List<Integer>) jsonRel.get(tag);
        }

        public List<List<Integer>> getIntegerListList(String tag) {
            // noinspection unchecked
            return (List<List<Integer>>) jsonRel.get(tag);
        }

        public List<AggregateCall> getAggregateCalls(String tag) {
            @SuppressWarnings("unchecked") final List<Map<String, Object>> jsonAggs = (List) jsonRel.get(tag);
            final List<AggregateCall> inputs = new ArrayList<>();
            for (Map<String, Object> jsonAggCall : jsonAggs) {
                inputs.add(toAggCall(jsonAggCall));
            }
            return inputs;
        }

        public Object get(String tag) {
            return jsonRel.get(tag);
        }

        public String getString(String tag) {
            return (String) jsonRel.get(tag);
        }

        public float getFloat(String tag) {
            return ((Number) jsonRel.get(tag)).floatValue();
        }

        public boolean getBoolean(String tag, boolean default_) {
            final Boolean b = (Boolean) jsonRel.get(tag);
            return b != null ? b : default_;
        }

        public <E extends Enum<E>> E getEnum(String tag, Class<E> enumClass) {
            return Util.enumVal(enumClass, getString(tag).toUpperCase(Locale.ROOT));
        }

        public List<RexNode> getExpressionList(String tag) {
            @SuppressWarnings("unchecked") final List<Object> jsonNodes = (List) jsonRel.get(tag);
            final List<RexNode> nodes = new ArrayList<>();
            for (Object jsonNode : jsonNodes) {
                nodes.add(relJson.toRex(this, jsonNode));
            }
            return nodes;
        }

        public RelDataType getRowType(String tag) {
            final Object o = jsonRel.get(tag);
            return relJson.toType(cluster.getTypeFactory(), o);
        }

        public RelDataType getRowType(String expressionsTag, String fieldsTag) {
            final List<RexNode> expressionList = getExpressionList(expressionsTag);
            @SuppressWarnings("unchecked") final List<String> names = (List<String>) get(fieldsTag);
            return cluster.getTypeFactory().createStructType(new AbstractList<Map.Entry<String, RelDataType>>() {

                @Override
                public Map.Entry<String, RelDataType> get(int index) {
                    return Pair.of(names.get(index), expressionList.get(index).getType());
                }

                @Override
                public int size() {
                    return names.size();
                }
            });
        }

        public RelCollation getCollation() {
            // noinspection unchecked
            return relJson.toCollation((List) get("collation"));
        }

        public RelDistribution getDistribution() {
            return relJson.toDistribution(get("distribution"));
        }

        public ImmutableList<ImmutableList<RexLiteral>> getTuples(String tag) {
            // noinspection unchecked
            final List<List> jsonTuples = (List) get(tag);
            final ImmutableList.Builder<ImmutableList<RexLiteral>> builder = ImmutableList.builder();
            for (List jsonTuple : jsonTuples) {
                builder.add(getTuple(jsonTuple));
            }
            return builder.build();
        }

        public ImmutableList<RexLiteral> getTuple(List jsonTuple) {
            final ImmutableList.Builder<RexLiteral> builder = ImmutableList.builder();
            for (Object jsonValue : jsonTuple) {
                builder.add((RexLiteral) relJson.toRex(this, jsonValue));
            }
            return builder.build();
        }
    };
    try {
        final RelNode rel = (RelNode) constructor.newInstance(input);
        relMap.put(id, rel);
        lastRel = rel;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        final Throwable e2 = e.getCause();
        if (e2 instanceof RuntimeException) {
            throw (RuntimeException) e2;
        }
        throw new RuntimeException(e2);
    }
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Constructor(java.lang.reflect.Constructor) RelInput(org.apache.calcite.rel.RelInput) InvocationTargetException(java.lang.reflect.InvocationTargetException) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelNode(org.apache.calcite.rel.RelNode) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

AggregateCall (org.apache.calcite.rel.core.AggregateCall)158 ArrayList (java.util.ArrayList)82 RexNode (org.apache.calcite.rex.RexNode)78 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)57 RelNode (org.apache.calcite.rel.RelNode)54 RexBuilder (org.apache.calcite.rex.RexBuilder)52 RelDataType (org.apache.calcite.rel.type.RelDataType)42 Aggregate (org.apache.calcite.rel.core.Aggregate)37 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)36 RexInputRef (org.apache.calcite.rex.RexInputRef)33 RelBuilder (org.apache.calcite.tools.RelBuilder)29 HashMap (java.util.HashMap)28 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)28 List (java.util.List)27 RexLiteral (org.apache.calcite.rex.RexLiteral)23 Pair (org.apache.calcite.util.Pair)20 ImmutableList (com.google.common.collect.ImmutableList)19 Project (org.apache.calcite.rel.core.Project)17 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)17 LogicalAggregate (org.apache.calcite.rel.logical.LogicalAggregate)16