alex-plekhanov commented on a change in pull request #9095: URL: https://github.com/apache/ignite/pull/9095#discussion_r640653281
########## File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/AbstractSetOpNode.java ########## @@ -0,0 +1,366 @@ +/* + * 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.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import org.apache.calcite.rel.type.RelDataType; +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.AggregateType; +import org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.GroupKey; +import org.apache.ignite.internal.util.typedef.F; + +/** + * Abstract execution node for set operators (EXCEPT, INTERSECT). + */ +public abstract class AbstractSetOpNode<Row> extends AbstractNode<Row> { + /** */ + private final AggregateType type; + + /** */ + private final Grouping<Row> grouping; + + /** */ + private int requested; + + /** */ + private int waiting; + + /** Current source index. */ + private int curSrcIdx; + + /** */ + private boolean inLoop; + + /** */ + protected AbstractSetOpNode(ExecutionContext<Row> ctx, RelDataType rowType, AggregateType type, boolean all, + RowFactory<Row> rowFactory, Grouping<Row> grouping) { + super(ctx, rowType); + + this.type = type; + this.grouping = grouping; + } + + /** {@inheritDoc} */ + @Override public void request(int rowsCnt) throws Exception { + assert !F.isEmpty(sources()); + assert rowsCnt > 0 && requested == 0; + assert waiting <= 0; + + checkState(); + + requested = rowsCnt; + + if (waiting == 0) + sources().get(curSrcIdx).request(waiting = IN_BUFFER_SIZE); + else if (!inLoop) + context().execute(this::flush, this::onError); + } + + /** */ + public void push(Row row, int idx) throws Exception { + assert downstream() != null; + assert waiting > 0; + + checkState(); + + waiting--; + + grouping.add(row, idx); + + if (waiting == 0) + sources().get(curSrcIdx).request(waiting = IN_BUFFER_SIZE); + } + + /** */ + public void end(int idx) throws Exception { + assert downstream() != null; + assert waiting > 0; + assert curSrcIdx == idx; + + checkState(); + + grouping.endOfSet(idx); + + if (type == AggregateType.SINGLE && grouping.isEmpty()) + curSrcIdx = sources().size(); // Skip subsequent sources. + else + curSrcIdx++; + + if (curSrcIdx >= sources().size()) { + waiting = -1; + + flush(); + } + else + sources().get(curSrcIdx).request(waiting); + } + + /** {@inheritDoc} */ + @Override protected void rewindInternal() { + requested = 0; + waiting = 0; + grouping.groups.clear(); + } + + /** {@inheritDoc} */ + @Override protected Downstream<Row> requestDownstream(int idx) { + return new Downstream<Row>() { + @Override public void push(Row row) throws Exception { + AbstractSetOpNode.this.push(row, idx); + } + + @Override public void end() throws Exception { + AbstractSetOpNode.this.end(idx); + } + + @Override public void onError(Throwable e) { + AbstractSetOpNode.this.onError(e); + } + }; + } + + /** */ + private void flush() throws Exception { + if (isClosed()) + return; + + checkState(); + + assert waiting == -1; + + int processed = 0; + + inLoop = true; + + try { + while (requested > 0 && !grouping.isEmpty()) { Review comment: Looks like only one iteration is possible. Changed to `if` -- 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: [email protected]
