You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/01/25 10:15:19 UTC

[GitHub] [ignite] korlov42 commented on a change in pull request #8683: IGNITE-13543 Calcite integration. Sort-based aggregates

korlov42 commented on a change in pull request #8683:
URL: https://github.com/apache/ignite/pull/8683#discussion_r562591884



##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/LogicalRelImplementor.java
##########
@@ -427,8 +433,8 @@ public LogicalRelImplementor(
     }
 
     /** {@inheritDoc} */
-    @Override public Node<Row> visit(IgniteAggregate rel) {
-        AggregateNode.AggregateType type = AggregateNode.AggregateType.SINGLE;
+    @Override public Node<Row> visit(IgniteAggregateHash rel) {

Review comment:
       `IgniteHashAggregate` and `IgniteSortAggregate`  sounds more natural to me

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/AggregateSortNode.java
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.exec.rel;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Supplier;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler.RowFactory;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.Accumulator;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorWrapper;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AggregateType;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ *
+ */
+public class AggregateSortNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> {
+    /** */
+    private final AggregateType type;
+
+    /** */
+    private final Supplier<List<AccumulatorWrapper<Row>>> accFactory;
+
+    /** */
+    private final RowFactory<Row> rowFactory;
+
+    /** */
+    private final ImmutableBitSet grpSet;
+
+    /** */
+    private final Comparator<Row> comp;
+
+    /** */
+    private Row prevRow;
+
+    /** */
+    private Group grp;
+
+    /** */
+    private int requested;
+
+    /** */
+    private int waiting;
+
+    /** */
+    private int cmpRes;
+
+    /**
+     * @param ctx Execution context.
+     */
+    public AggregateSortNode(
+        ExecutionContext<Row> ctx,
+        RelDataType rowType,
+        AggregateType type,
+        ImmutableBitSet grpSet,
+        Supplier<List<AccumulatorWrapper<Row>>> accFactory,
+        RowFactory<Row> rowFactory,
+        Comparator<Row> comp
+    ) {
+        super(ctx, rowType);
+
+        this.type = type;
+        this.accFactory = accFactory;
+        this.rowFactory = rowFactory;
+        this.grpSet = grpSet;
+        this.comp = comp;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) {
+        assert !F.isEmpty(sources()) && sources().size() == 1;
+        assert rowsCnt > 0 && requested == 0;
+        assert waiting <= 0;
+
+        try {
+            checkState();
+
+            requested = rowsCnt;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void push(Row row) {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        try {
+            checkState();
+
+            waiting--;
+
+            if (grp != null) {
+                int cmp = comp.compare(row, prevRow);
+
+                if (cmp == 0)
+                    grp.add(row);
+                else {
+                    if (cmpRes == 0)
+                        cmpRes = cmp;
+                    else
+                        assert cmp == cmpRes : "Input not sorted";
+
+                    doPush();
+
+                    grp = newGroup(row);
+                }
+            }
+            else
+                grp = newGroup(row);
+
+            prevRow = row;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void end() {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        try {
+            checkState();
+
+            waiting = -1;
+
+            if (grp != null)
+                doPush();
+
+            grp = null;
+            prevRow = null;
+
+            downstream().end();
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void rewindInternal() {
+        requested = 0;
+        waiting = 0;
+        grp = null;
+        prevRow = null;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected Downstream<Row> requestDownstream(int idx) {
+        if (idx != 0)
+            throw new IndexOutOfBoundsException();
+
+        return this;
+    }
+
+    /** */
+    private Group newGroup(Row r) {
+        if (type == AggregateType.REDUCE)
+            System.out.println();

Review comment:
       out

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/AggregateSortNode.java
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.exec.rel;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Supplier;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler.RowFactory;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.Accumulator;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorWrapper;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AggregateType;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ *
+ */
+public class AggregateSortNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> {
+    /** */
+    private final AggregateType type;
+
+    /** */
+    private final Supplier<List<AccumulatorWrapper<Row>>> accFactory;
+
+    /** */
+    private final RowFactory<Row> rowFactory;
+
+    /** */
+    private final ImmutableBitSet grpSet;
+
+    /** */
+    private final Comparator<Row> comp;
+
+    /** */
+    private Row prevRow;
+
+    /** */
+    private Group grp;
+
+    /** */
+    private int requested;
+
+    /** */
+    private int waiting;
+
+    /** */
+    private int cmpRes;
+
+    /**
+     * @param ctx Execution context.
+     */
+    public AggregateSortNode(
+        ExecutionContext<Row> ctx,
+        RelDataType rowType,
+        AggregateType type,
+        ImmutableBitSet grpSet,
+        Supplier<List<AccumulatorWrapper<Row>>> accFactory,
+        RowFactory<Row> rowFactory,
+        Comparator<Row> comp
+    ) {
+        super(ctx, rowType);
+
+        this.type = type;
+        this.accFactory = accFactory;
+        this.rowFactory = rowFactory;
+        this.grpSet = grpSet;
+        this.comp = comp;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) {
+        assert !F.isEmpty(sources()) && sources().size() == 1;
+        assert rowsCnt > 0 && requested == 0;
+        assert waiting <= 0;
+
+        try {
+            checkState();
+
+            requested = rowsCnt;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void push(Row row) {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        try {
+            checkState();
+
+            waiting--;
+
+            if (grp != null) {
+                int cmp = comp.compare(row, prevRow);
+
+                if (cmp == 0)
+                    grp.add(row);
+                else {
+                    if (cmpRes == 0)
+                        cmpRes = cmp;
+                    else
+                        assert cmp == cmpRes : "Input not sorted";
+
+                    doPush();
+
+                    grp = newGroup(row);
+                }
+            }
+            else
+                grp = newGroup(row);
+
+            prevRow = row;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void end() {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        try {
+            checkState();
+
+            waiting = -1;
+
+            if (grp != null)
+                doPush();
+
+            grp = null;
+            prevRow = null;
+
+            downstream().end();

Review comment:
       downstream's `end` should not be called subsequently with current `end()` 

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteAggregateSort.java
##########
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.rel;
+
+import java.util.List;
+import java.util.Objects;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelWriter;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.ImmutableIntList;
+import org.apache.calcite.util.Pair;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+
+/**
+ *
+ */
+public class IgniteAggregateSort extends IgniteAggregateBase {
+    /** Collation. */
+    private final RelCollation collation;
+
+    /** {@inheritDoc} */
+    public IgniteAggregateSort(
+        RelOptCluster cluster,
+        RelTraitSet traitSet,
+        RelNode input,
+        ImmutableBitSet groupSet,
+        List<ImmutableBitSet> groupSets,
+        List<AggregateCall> aggCalls
+    ) {
+        super(cluster, traitSet, input, groupSet, groupSets, aggCalls);
+
+        assert !TraitUtils.collation(traitSet).isDefault();
+
+        collation = TraitUtils.collation(traitSet);
+    }
+
+    /** {@inheritDoc} */
+    public IgniteAggregateSort(RelInput input) {
+        super(input);
+
+        collation = input.getCollation();
+
+        assert Objects.nonNull(collation);
+        assert !collation.isDefault();
+    }
+
+    /** {@inheritDoc} */
+    @Override public Aggregate copy(RelTraitSet traitSet, RelNode input, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
+        return new IgniteAggregateSort(getCluster(), traitSet.replace(collation), input, groupSet, groupSets, aggCalls);
+    }
+
+    /** {@inheritDoc} */
+    @Override public IgniteRel clone(RelOptCluster cluster, List<IgniteRel> inputs) {
+        return new IgniteAggregateSort(cluster, getTraitSet(), sole(inputs),
+            getGroupSet(), getGroupSets(), getAggCallList());
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T> T accept(IgniteRelVisitor<T> visitor) {
+        return visitor.visit(this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public RelWriter explainTerms(RelWriter pw) {
+        return super.explainTerms(pw).item("collation", collation);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(RelTraitSet nodeTraits, List<RelTraitSet> inputTraits) {
+        RelCollation collation = RelCollations.of(ImmutableIntList.copyOf(groupSet.asList()));
+
+        return Pair.of(nodeTraits.replace(RelCollations.EMPTY),

Review comment:
       why do you think a node collation should be empty? Seems for query "SELECT name, COUNT(*) FROM t GROUP BY name" we could preserve collation by `name`

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/AggregateSortNode.java
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.exec.rel;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Supplier;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler.RowFactory;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.Accumulator;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorWrapper;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AggregateType;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ *
+ */
+public class AggregateSortNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> {
+    /** */
+    private final AggregateType type;
+
+    /** */
+    private final Supplier<List<AccumulatorWrapper<Row>>> accFactory;
+
+    /** */
+    private final RowFactory<Row> rowFactory;
+
+    /** */
+    private final ImmutableBitSet grpSet;
+
+    /** */
+    private final Comparator<Row> comp;
+
+    /** */
+    private Row prevRow;
+
+    /** */
+    private Group grp;
+
+    /** */
+    private int requested;
+
+    /** */
+    private int waiting;
+
+    /** */
+    private int cmpRes;
+
+    /**
+     * @param ctx Execution context.
+     */
+    public AggregateSortNode(
+        ExecutionContext<Row> ctx,
+        RelDataType rowType,
+        AggregateType type,
+        ImmutableBitSet grpSet,
+        Supplier<List<AccumulatorWrapper<Row>>> accFactory,
+        RowFactory<Row> rowFactory,
+        Comparator<Row> comp
+    ) {
+        super(ctx, rowType);
+
+        this.type = type;
+        this.accFactory = accFactory;
+        this.rowFactory = rowFactory;
+        this.grpSet = grpSet;
+        this.comp = comp;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) {
+        assert !F.isEmpty(sources()) && sources().size() == 1;
+        assert rowsCnt > 0 && requested == 0;
+        assert waiting <= 0;
+
+        try {
+            checkState();
+
+            requested = rowsCnt;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void push(Row row) {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        try {
+            checkState();
+
+            waiting--;
+
+            if (grp != null) {
+                int cmp = comp.compare(row, prevRow);
+
+                if (cmp == 0)
+                    grp.add(row);
+                else {
+                    if (cmpRes == 0)
+                        cmpRes = cmp;
+                    else
+                        assert cmp == cmpRes : "Input not sorted";
+
+                    doPush();

Review comment:
       right now it's possible to push more row than actually requested. Please see my PR with test that highlight this issue: https://github.com/gridgain/apache-ignite/pull/262 (this includes some minor fixes)

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteMapAggregateHash.java
##########
@@ -113,8 +93,11 @@ public static RelDataType rowType(RelDataTypeFactory typeFactory) {
     }
 
     /** {@inheritDoc} */
-    @Override public IgniteRel clone(RelOptCluster cluster, List<IgniteRel> inputs) {
-        return new IgniteMapAggregate(cluster, getTraitSet(), sole(inputs),
-            getGroupSet(), getGroupSets(), getAggCallList());
+    @Override public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
+        double rows = mq.getRowCount(getInput());
+
+        // TODO: fix it when https://issues.apache.org/jira/browse/IGNITE-13543 will be resolved
+        // currently it's OK to have such a dummy cost because there is no other options
+        return planner.getCostFactory().makeCost(rows, rows * IgniteCost.ROW_PASS_THROUGH_COST, 0);

Review comment:
       the same as for the simple hash aggregate: cost should be reimplemented

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/AggregateSortNode.java
##########
@@ -0,0 +1,299 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.exec.rel;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.function.Supplier;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
+import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler.RowFactory;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.Accumulator;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorWrapper;
+import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AggregateType;
+import org.apache.ignite.internal.processors.query.calcite.util.Commons;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ *
+ */
+public class AggregateSortNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> {
+    /** */
+    private final AggregateType type;
+
+    /** */
+    private final Supplier<List<AccumulatorWrapper<Row>>> accFactory;
+
+    /** */
+    private final RowFactory<Row> rowFactory;
+
+    /** */
+    private final ImmutableBitSet grpSet;
+
+    /** */
+    private final Comparator<Row> comp;
+
+    /** */
+    private Row prevRow;
+
+    /** */
+    private Group grp;
+
+    /** */
+    private int requested;
+
+    /** */
+    private int waiting;
+
+    /** */
+    private int cmpRes;
+
+    /**
+     * @param ctx Execution context.
+     */
+    public AggregateSortNode(
+        ExecutionContext<Row> ctx,
+        RelDataType rowType,
+        AggregateType type,
+        ImmutableBitSet grpSet,
+        Supplier<List<AccumulatorWrapper<Row>>> accFactory,
+        RowFactory<Row> rowFactory,
+        Comparator<Row> comp
+    ) {
+        super(ctx, rowType);
+
+        this.type = type;
+        this.accFactory = accFactory;
+        this.rowFactory = rowFactory;
+        this.grpSet = grpSet;
+        this.comp = comp;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) {
+        assert !F.isEmpty(sources()) && sources().size() == 1;
+        assert rowsCnt > 0 && requested == 0;
+        assert waiting <= 0;
+
+        try {
+            checkState();
+
+            requested = rowsCnt;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);
+        }
+        catch (Exception e) {
+            onError(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void push(Row row) {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        try {
+            checkState();
+
+            waiting--;
+
+            if (grp != null) {
+                int cmp = comp.compare(row, prevRow);
+
+                if (cmp == 0)
+                    grp.add(row);
+                else {
+                    if (cmpRes == 0)
+                        cmpRes = cmp;
+                    else
+                        assert cmp == cmpRes : "Input not sorted";
+
+                    doPush();
+
+                    grp = newGroup(row);
+                }
+            }
+            else
+                grp = newGroup(row);
+
+            prevRow = row;
+
+            if (waiting == 0)
+                source().request(waiting = IN_BUFFER_SIZE);

Review comment:
       it's better to request new batch in another task to allow other queries do progress

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteAggregateHash.java
##########
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.processors.query.calcite.rel;
+
+import java.util.List;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelInput;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Pair;
+import org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCost;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+
+/**
+ *
+ */
+public class IgniteAggregateHash extends IgniteAggregateBase {
+    /** {@inheritDoc} */
+    public IgniteAggregateHash(RelOptCluster cluster, RelTraitSet traitSet, RelNode input, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
+        super(cluster, traitSet, input, groupSet, groupSets, aggCalls);
+    }
+
+    /** {@inheritDoc} */
+    public IgniteAggregateHash(RelInput input) {
+        super(input);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Aggregate copy(RelTraitSet traitSet, RelNode input, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
+        return new IgniteAggregateHash(getCluster(), traitSet, input, groupSet, groupSets, aggCalls);
+    }
+
+    /** {@inheritDoc} */
+    @Override public IgniteRel clone(RelOptCluster cluster, List<IgniteRel> inputs) {
+        return new IgniteAggregateHash(cluster, getTraitSet(), sole(inputs),
+            getGroupSet(), getGroupSets(), getAggCallList());
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T> T accept(IgniteRelVisitor<T> visitor) {
+        return visitor.visit(this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughCollation(RelTraitSet nodeTraits, List<RelTraitSet> inputTraits) {
+        // Since it's a hash aggregate it erases collation.
+        return Pair.of(nodeTraits.replace(RelCollations.EMPTY),
+            ImmutableList.of(inputTraits.get(0).replace(RelCollations.EMPTY)));
+    }
+
+    /** {@inheritDoc} */
+    @Override public List<Pair<RelTraitSet, List<RelTraitSet>>> deriveCollation(RelTraitSet nodeTraits, List<RelTraitSet> inputTraits) {
+        // Since it's a hash aggregate it erases collation.
+
+        return ImmutableList.of(Pair.of(nodeTraits.replace(RelCollations.EMPTY),
+            ImmutableList.of(inputTraits.get(0).replace(RelCollations.EMPTY))));
+    }
+
+    /** {@inheritDoc} */
+    @Override public List<Pair<RelTraitSet, List<RelTraitSet>>> deriveCorrelation(RelTraitSet nodeTraits,
+        List<RelTraitSet> inTraits) {
+        return ImmutableList.of(Pair.of(nodeTraits.replace(TraitUtils.correlation(inTraits.get(0))),
+            inTraits));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected RelNode createMapAggregate(RelOptCluster cluster, RelTraitSet traits, RelNode input,
+        ImmutableBitSet groupSet, ImmutableList<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) {
+        return new IgniteMapAggregateHash(getCluster(), traits, input, groupSet, groupSets, aggCalls);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected RelNode createReduceAggregate(RelOptCluster cluster, RelTraitSet traits, RelNode input,
+        ImmutableBitSet groupSet, ImmutableList<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls,
+        RelDataType rowType) {
+        return new IgniteReduceAggregateHash(getCluster(), traits, input, groupSet, groupSets, aggCalls, getRowType());
+    }
+
+    /** {@inheritDoc} */
+    @Override public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) {
+        double rows = mq.getRowCount(getInput());
+
+        // TODO: fix it when https://issues.apache.org/jira/browse/IGNITE-13543 will be resolved
+        // currently it's OK to have such a dummy cost because there is no other options
+        return planner.getCostFactory().makeCost(rows, rows * IgniteCost.ROW_PASS_THROUGH_COST, 0);

Review comment:
       Now we have a two types of aggregates, so the cost estimation should be reimplemented




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org