alex-plekhanov commented on code in PR #12096:
URL: https://github.com/apache/ignite/pull/12096#discussion_r3202600754


##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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.ArrayDeque;
+import java.util.Comparator;
+import java.util.Deque;
+import java.util.function.Supplier;
+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.exp.window.WindowPartition;
+import org.apache.ignite.internal.util.typedef.F;
+
+/** Window support node */
+public class WindowNode<Row> extends MemoryTrackingNode<Row> implements 
SingleNode<Row>, Downstream<Row> {
+    /**  */
+    private final Comparator<Row> partCmp;
+
+    /**  */
+    private final Supplier<WindowPartition<Row>> partitionFactory;
+
+    /**  */
+    private final RowHandler.RowFactory<Row> rowFactory;
+
+    /**  */
+    private WindowPartition<Row> partition;
+
+    /**  */
+    private int requested;
+
+    /**  */
+    private int waiting;
+
+    /**  */
+    private Row prevRow;
+
+    /**  */
+    private final Deque<Row> outBuf = new ArrayDeque<>(IN_BUFFER_SIZE);
+
+    /**  */
+    public WindowNode(
+        ExecutionContext<Row> ctx,
+        RelDataType rowType,
+        Comparator<Row> partCmp,
+        Supplier<WindowPartition<Row>> partitionFactory,
+        RowHandler.RowFactory<Row> rowFactory
+    ) {
+        super(ctx, rowType, DFLT_ROW_OVERHEAD);
+        this.partCmp = partCmp;
+        this.partitionFactory = partitionFactory;
+        this.rowFactory = rowFactory;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) throws Exception {
+        assert !F.isEmpty(sources()) && sources().size() == 1;
+        assert rowsCnt > 0;
+
+        checkState();
+
+        requested = rowsCnt;
+
+        doPush();
+
+        if (waiting == 0) {
+            waiting = IN_BUFFER_SIZE;
+            source().request(IN_BUFFER_SIZE);
+        }
+        else if (waiting < 0)
+            downstream().end();
+    }
+
+    @Override public void push(Row row) throws Exception {
+        assert downstream() != null;
+        assert waiting > 0;
+
+        checkState();
+
+        waiting--;
+
+        if (partition == null) {
+            partition = partitionFactory.get();
+        }
+        else if (prevRow != null && partCmp != null && 
partCmp.compare(prevRow, row) != 0) {
+            partition.drainTo(rowFactory, outBuf);
+            partition.reset();
+            doPush();
+        }
+
+        if (partition.add(row)) {
+            partition.drainTo(rowFactory, outBuf);
+            doPush();
+        }
+        else
+            nodeMemoryTracker.onRowAdded(row);
+
+        prevRow = row;
+
+        if (waiting == 0 && requested > 0) {
+            waiting = IN_BUFFER_SIZE;
+
+            context().execute(() -> source().request(IN_BUFFER_SIZE), 
this::onError);
+        }
+    }
+
+    @Override public void end() throws Exception {
+        assert downstream() != null;
+        if (waiting < 0) {
+            return;
+        }
+
+        waiting = -1;
+
+        checkState();
+
+        if (partition != null) {
+            partition.drainTo(rowFactory, outBuf);
+            partition.reset();
+        }
+
+        doPush();
+
+        downstream().end();

Review Comment:
   Yes, looks like fix is almost correct (added one new comment about 
requesting from input).



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to