This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 6801c7d3c62 IGNITE-28882 SQL Calcite: Optimize windows function
execution - Fixes #13359.
6801c7d3c62 is described below
commit 6801c7d3c62a4071ad674f6d5e8351d17be37dc3
Author: ozinovev <[email protected]>
AuthorDate: Thu Oct 15 17:34:19 2026 +0300
IGNITE-28882 SQL Calcite: Optimize windows function execution - Fixes
#13359.
Signed-off-by: Aleksey Plekhanov <[email protected]>
---
.../query/calcite/exec/LogicalRelImplementor.java | 28 +++-
.../exec/exp/window/BufferingWindowPartition.java | 182 ++++++++++++++-------
.../exec/exp/window/RangeWindowPartitionFrame.java | 19 +--
.../exec/exp/window/RowWindowPartitionFrame.java | 7 -
...artition.java => StreamingWindowPartition.java} | 52 ++----
.../exec/exp/window/WindowFunctionFactory.java | 11 ++
.../exec/exp/window/WindowFunctionWrapper.java | 3 +
.../calcite/exec/exp/window/WindowPartition.java | 16 --
.../exec/exp/window/WindowPartitionBase.java | 2 +-
.../exec/exp/window/WindowPartitionFactory.java | 69 ++++----
.../exec/exp/window/WindowPartitionFrame.java | 8 +-
.../{WindowNode.java => BufferingWindowNode.java} | 125 ++++++++------
.../{WindowNode.java => StreamingWindowNode.java} | 110 +++----------
.../query/calcite/rule/WindowConverterRule.java | 1 +
.../calcite/exec/rel/WindowExecutionTest.java | 64 ++++++--
15 files changed, 368 insertions(+), 329 deletions(-)
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/LogicalRelImplementor.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/LogicalRelImplementor.java
index ac39e7c6a4b..5bf56be0b8b 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/LogicalRelImplementor.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/LogicalRelImplementor.java
@@ -55,9 +55,12 @@ import
org.apache.ignite.internal.processors.query.calcite.exec.exp.ExpressionFa
import
org.apache.ignite.internal.processors.query.calcite.exec.exp.RangeIterable;
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.exec.exp.window.WindowPartition;
+import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.BufferingWindowPartition;
+import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.StreamingWindowPartition;
+import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.WindowFunctions;
import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.WindowPartitionFactory;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.AbstractSetOpNode;
+import
org.apache.ignite.internal.processors.query.calcite.exec.rel.BufferingWindowNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.CollectNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.CorrelatedNestedLoopJoinNode;
import org.apache.ignite.internal.processors.query.calcite.exec.rel.FilterNode;
@@ -79,12 +82,13 @@ import
org.apache.ignite.internal.processors.query.calcite.exec.rel.RepeatUnionN
import org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanStorageNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.ScanTableRowNode;
+import org.apache.ignite.internal.processors.query.calcite.exec.rel.SingleNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.SortAggregateNode;
import org.apache.ignite.internal.processors.query.calcite.exec.rel.SortNode;
+import
org.apache.ignite.internal.processors.query.calcite.exec.rel.StreamingWindowNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.TableSpoolNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.UncollectNode;
import
org.apache.ignite.internal.processors.query.calcite.exec.rel.UnionAllNode;
-import org.apache.ignite.internal.processors.query.calcite.exec.rel.WindowNode;
import
org.apache.ignite.internal.processors.query.calcite.metadata.AffinityService;
import
org.apache.ignite.internal.processors.query.calcite.metadata.ColocationGroup;
import
org.apache.ignite.internal.processors.query.calcite.prepare.IgniteSqlSemantics;
@@ -998,18 +1002,28 @@ public class LogicalRelImplementor<Row> implements
IgniteRelVisitor<Node<Row>> {
assert collation.getFieldCollations().size() >= grpKeys.size();
Comparator<Row> partCmp =
expressionFactory.comparator(TraitUtils.createCollation(grpKeys));
+ RowFactory<Row> rowFactory =
ctx.rowHandler().factory(ctx.getTypeFactory(), outType);
+
List<AggregateCall> calls = grp.getAggregateCalls(rel);
- Supplier<WindowPartition<Row>> partFactory = new
WindowPartitionFactory<>(ctx, grp, calls, inputType);
+ assert !calls.isEmpty() : "Window aggregate calls should not be empty";
- RowFactory<Row> rowFactory =
ctx.rowHandler().factory(ctx.getTypeFactory(), outType);
+ WindowPartitionFactory<Row> factory = new
WindowPartitionFactory<>(ctx);
- WindowNode<Row> node = new WindowNode<>(ctx, outType, partCmp,
partFactory, rowFactory);
+ SingleNode<Row> windowNode;
+ if (WindowFunctions.streamable(grp)) {
+ StreamingWindowPartition<Row> partition =
factory.newStreamingPartition(grp, calls, inputType);
+ windowNode = new StreamingWindowNode<>(ctx, outType, partCmp,
partition, rowFactory);
+ }
+ else {
+ BufferingWindowPartition<Row> partition =
factory.newBufferingPartition(grp, calls, inputType);
+ windowNode = new BufferingWindowNode<>(ctx, outType, partCmp,
partition, rowFactory);
+ }
Node<Row> input = visit(rel.getInput());
- node.register(input);
+ windowNode.register(input);
- return node;
+ return windowNode;
}
/** {@inheritDoc} */
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/BufferingWindowPartition.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/BufferingWindowPartition.java
index 98aafe087ea..19cf2cb9b31 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/BufferingWindowPartition.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/BufferingWindowPartition.java
@@ -17,26 +17,35 @@
package org.apache.ignite.internal.processors.query.calcite.exec.exp.window;
-import java.util.ArrayList;
+import java.util.ArrayDeque;
import java.util.Comparator;
+import java.util.Deque;
import java.util.List;
-import java.util.function.Consumer;
import org.apache.calcite.rel.core.Window;
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.tracker.RowTracker;
+import org.jetbrains.annotations.Nullable;
/** Buffering implementation of the ROWS / RANGE window partition. */
-final class BufferingWindowPartition<Row> extends WindowPartitionBase<Row> {
- /** Rows in partition. */
- private final List<Row> buf;
+public final class BufferingWindowPartition<Row> extends
WindowPartitionBase<Row> {
+ /** */
+ private final ExecutionContext<Row> ctx;
- /** Frame within partition. */
- private final WindowPartitionFrame<Row> frame;
+ /** */
+ private final Window.Group grp;
/** */
- private RowTracker<Row> memoryTracker;
+ private final RelDataType inputRowType;
+
+ /** Slices in partition. */
+ private final Deque<FrameHolder> frames;
+
+ /** Number of rows, can be evaluted right now. */
+ private int ready;
+
+ /** Current frame for row evaluation. */
+ private FrameHolder currFrame;
/** */
BufferingWindowPartition(
@@ -48,60 +57,64 @@ final class BufferingWindowPartition<Row> extends
WindowPartitionBase<Row> {
RelDataType inputRowType
) {
super(peerCmp, funcFactory, rowFactory);
- buf = new ArrayList<>();
- frame = createFrame(ctx, grp, peerCmp, inputRowType, buf);
- }
-
- /** {@inheritDoc} */
- @Override public void add(Row row) {
- buf.add(row);
- onRowAdded(row);
+ this.ctx = ctx;
+ this.grp = grp;
+ this.inputRowType = inputRowType;
+ frames = new ArrayDeque<>();
}
- /** {@inheritDoc} */
- @Override public void evalTo(RowHandler.RowFactory<Row> factory,
Consumer<Row> output) {
+ /**
+ * Appends rows to the partition.
+ * Important: the buffer must not be modified after being passed to this
method.
+ **/
+ public void appendPartition(List<Row> buf, Runnable onBufRemoved) {
if (buf.isEmpty())
return;
- List<WindowFunctionWrapper<Row>> accumulators = createWrappers();
- Object[] accResults = new Object[accumulators.size()];
+ WindowPartitionFrame<Row> frame = createFrame(ctx, grp, peerCmp,
inputRowType, buf);
+ ready += frame.size();
+ frames.add(new FrameHolder(frame, onBufRemoved));
+ }
- int size = buf.size();
- Row prevRow = null;
- int peerIdx = -1;
- for (int rowIdx = 0; rowIdx < size; rowIdx++) {
- Row currRow = buf.get(rowIdx);
- if (isNewPeer(currRow, prevRow))
- peerIdx++;
+ /**
+ * Evaluates next row in the window partition.
+ * @return Result row or {@code null} if there are no more rows.
+ */
+ public @Nullable Row nextRow(RowHandler.RowFactory<Row> factory) {
+ if (currFrame == null || currFrame.consumed()) {
+ if (currFrame != null)
+ currFrame.release();
+ currFrame = frames.pollFirst();
+ }
- int accIdx = 0;
- for (WindowFunctionWrapper<Row> acc : accumulators) {
- Object accResult = acc.callBuffering(currRow, rowIdx, peerIdx,
frame);
- accResults[accIdx++] = accResult;
- }
+ if (currFrame == null) {
+ assert ready == 0;
+ return null;
+ }
- Row resultRow = createResultRow(factory, currRow, accResults);
- output.accept(resultRow);
+ assert ready > 0;
+ Row resultRow = currFrame.nextRow(factory);
+ ready--;
+ return resultRow;
+ }
- prevRow = currRow;
- }
+ /** Returns {@code true} if the partition contains at least one pending
row. */
+ public boolean isReady() {
+ return ready != 0;
}
/** {@inheritDoc} */
@Override public void reset() {
- buf.forEach(this::onRowRemoved);
- buf.clear();
- frame.reset();
- }
+ FrameHolder holder;
+ while ((holder = frames.poll()) != null)
+ holder.release();
- /** {@inheritDoc} */
- @Override public boolean isStreaming() {
- return false;
- }
+ if (currFrame != null) {
+ currFrame.release();
+ currFrame = null;
+ }
- /** {@inheritDoc} */
- @Override public void attachMemoryTracker(RowTracker<Row> memoryTracker) {
- this.memoryTracker = memoryTracker;
+ ready = 0;
}
/** Creates frame for partition. */
@@ -118,15 +131,70 @@ final class BufferingWindowPartition<Row> extends
WindowPartitionBase<Row> {
return new RangeWindowPartitionFrame<>(buf, ctx, peerCmp, grp,
inputRowType);
}
- /** Adds row to memory tracker. */
- private void onRowAdded(Row row) {
- if (memoryTracker != null)
- memoryTracker.onRowAdded(row);
- }
+ /** */
+ private final class FrameHolder {
+ /** */
+ private final WindowPartitionFrame<Row> frame;
- /** Removes row from memory tracker. */
- private void onRowRemoved(Row row) {
- if (memoryTracker != null)
- memoryTracker.onRowRemoved(row);
+ /** */
+ private final Runnable onFrameRemoved;
+
+ /** Index of the current row in the frame. */
+ private int rowIdx;
+
+ /** Index of the current peer in the frame. */
+ private int peerIdx = -1;
+
+ /** */
+ private Row prevRow;
+
+ /** */
+ private List<WindowFunctionWrapper<Row>> accumulators;
+
+ /** */
+ private Object[] accResults;
+
+ /** */
+ private FrameHolder(WindowPartitionFrame<Row> frame, Runnable removed)
{
+ this.frame = frame;
+ onFrameRemoved = removed;
+ }
+
+ /** */
+ Row nextRow(RowHandler.RowFactory<Row> factory) {
+ assert !consumed();
+
+ if (accumulators == null) {
+ accumulators = createWrappers();
+ accResults = new Object[accumulators.size()];
+ }
+
+ Row currRow = frame.get(rowIdx);
+ if (isNewPeer(currRow, prevRow))
+ peerIdx++;
+
+ int accIdx = 0;
+ for (WindowFunctionWrapper<Row> acc : accumulators) {
+ Object accResult = acc.callBuffering(currRow, rowIdx, peerIdx,
currFrame.frame);
+ accResults[accIdx++] = accResult;
+ }
+
+ Row resultRow = createResultRow(factory, currRow, accResults);
+
+ rowIdx++;
+ prevRow = currRow;
+
+ return resultRow;
+ }
+
+ /** */
+ boolean consumed() {
+ return rowIdx == frame.size();
+ }
+
+ /** */
+ void release() {
+ onFrameRemoved.run();
+ }
}
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RangeWindowPartitionFrame.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RangeWindowPartitionFrame.java
index 48bd52ef503..8a88ec160ee 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RangeWindowPartitionFrame.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RangeWindowPartitionFrame.java
@@ -103,7 +103,7 @@ final class RangeWindowPartitionFrame<Row> extends
WindowPartitionFrame<Row> {
if (lowerBoundRow == null)
cachedStartIdx = 0;
else
- cachedStartIdx = bsearchBound(lowerBoundRow, buf, true);
+ cachedStartIdx = bsearchBound(lowerBoundRow, true);
return cachedStartIdx;
}
@@ -121,29 +121,20 @@ final class RangeWindowPartitionFrame<Row> extends
WindowPartitionFrame<Row> {
if (upperBoundRow == null)
cachedEndIdx = size() - 1;
else
- cachedEndIdx = bsearchBound(upperBoundRow, buf, false);
+ cachedEndIdx = bsearchBound(upperBoundRow, false);
return cachedEndIdx;
}
- /** {@inheritDoc} */
- @Override public void reset() {
- // Reseting index cache.
- cachedStartPeerIdx = -1;
- cachedEndPeerIdx = -1;
- cachedStartRowIdx = -1;
- cachedEndRowIdx = -1;
- }
-
/** Binary search bound. */
- private int bsearchBound(Row row, List<Row> buf, boolean lower) {
+ private int bsearchBound(Row row, boolean lower) {
int start = 0;
- int end = buf.size() - 1;
+ int end = size() - 1;
while (start <= end) {
int mid = (start + end) / 2;
- Row midRow = buf.get(mid);
+ Row midRow = get(mid);
int cmp = compareRowPeer(midRow, row);
if (cmp > 0 || (lower && cmp == 0))
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RowWindowPartitionFrame.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RowWindowPartitionFrame.java
index 84712e3fc09..f8d44cae010 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RowWindowPartitionFrame.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/RowWindowPartitionFrame.java
@@ -95,13 +95,6 @@ final class RowWindowPartitionFrame<Row> extends
WindowPartitionFrame<Row> {
return applyOffset(rowIdx, cachedEndOffset, size() - 1);
}
- /** {@inheritDoc} */
- @Override protected void reset() {
- // Reseting index cache.
- cachedStartRowIdx = -1;
- cachedEndRowIdx = -1;
- }
-
/** */
private static int applyOffset(int rowIdx, int offset, int cap) {
int idx = Math.addExact(rowIdx, offset);
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/StreamWindowPartition.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/StreamingWindowPartition.java
similarity index 63%
rename from
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/StreamWindowPartition.java
rename to
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/StreamingWindowPartition.java
index 6459c8ecff9..cb0cd12ec0b 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/StreamWindowPartition.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/StreamingWindowPartition.java
@@ -19,18 +19,13 @@ package
org.apache.ignite.internal.processors.query.calcite.exec.exp.window;
import java.util.Comparator;
import java.util.List;
-import java.util.function.Consumer;
import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
-import
org.apache.ignite.internal.processors.query.calcite.exec.tracker.RowTracker;
/** Non-buffering implementation of the ROWS / RANGE window partition. */
-final class StreamWindowPartition<Row> extends WindowPartitionBase<Row> {
+public final class StreamingWindowPartition<Row> extends
WindowPartitionBase<Row> {
/** */
private Row prevRow;
- /** */
- private Row currRow;
-
/** */
private int rowIdx = -1;
@@ -44,7 +39,7 @@ final class StreamWindowPartition<Row> extends
WindowPartitionBase<Row> {
private Object[] accResults;
/** */
- StreamWindowPartition(
+ StreamingWindowPartition(
Comparator<Row> peerCmp,
WindowFunctionFactory<Row> funcFactory,
RowHandler.RowFactory<Row> rowFactory
@@ -52,55 +47,42 @@ final class StreamWindowPartition<Row> extends
WindowPartitionBase<Row> {
super(peerCmp, funcFactory, rowFactory);
}
- /** {@inheritDoc} */
- @Override public void add(Row row) {
- assert currRow == null : "StreamingWindowPartition can only hold one
row";
- currRow = row;
- }
-
- /** {@inheritDoc} */
- @Override public void evalTo(RowHandler.RowFactory<Row> factory,
Consumer<Row> output) {
- if (currRow == null)
- return;
-
+ /** Evaluates window functions for the given row. */
+ public Row eval(Row row, RowHandler.RowFactory<Row> factory) {
if (accumulators == null) {
accumulators = createWrappers();
accResults = new Object[accumulators.size()];
}
rowIdx++;
- if (isNewPeer(currRow, prevRow))
+ if (isNewPeer(row, prevRow))
peerIdx++;
int accIdx = 0;
for (WindowFunctionWrapper<Row> acc : accumulators) {
- Object accResult = acc.callStreaming(currRow, rowIdx, peerIdx);
+ Object accResult = acc.callStreaming(row, rowIdx, peerIdx);
accResults[accIdx++] = accResult;
}
- Row resultRow = createResultRow(factory, currRow, accResults);
- output.accept(resultRow);
+ Row resultRow = createResultRow(factory, row, accResults);
+ prevRow = row;
+ return resultRow;
+ }
+
+ /** Returns true if any of the window functions is an accumulator stores
incomig row. */
+ public boolean hasAggAccumulators() {
+ List<WindowFunctionWrapper<Row>> accumulators = this.accumulators;
+ if (accumulators == null)
+ accumulators = createWrappers();
- prevRow = currRow;
- currRow = null;
+ return
accumulators.stream().anyMatch(WindowFunctionWrapper::isAggAccumulator);
}
/** {@inheritDoc} */
@Override public void reset() {
- currRow = null;
prevRow = null;
rowIdx = -1;
peerIdx = -1;
accumulators = null;
}
-
- /** {@inheritDoc} */
- @Override public boolean isStreaming() {
- return true;
- }
-
- /** {@inheritDoc} */
- @Override public void attachMemoryTracker(RowTracker<Row> memoryTracker) {
- // Streaming partition does not use memory tracker.
- }
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionFactory.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionFactory.java
index 493e8d75c88..96a144aff7b 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionFactory.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionFactory.java
@@ -28,6 +28,7 @@ import
org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext
import
org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorWrapper;
import
org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AccumulatorsFactoryBase;
import
org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.AggregateType;
+import
org.apache.ignite.internal.processors.query.calcite.exec.exp.agg.IterableAccumulator;
import org.apache.ignite.internal.processors.query.calcite.util.Commons;
import org.jetbrains.annotations.NotNull;
@@ -163,6 +164,11 @@ final class WindowFunctionFactory<Row> extends
AccumulatorsFactoryBase<Row> {
Object result = windowFunction.call(accRow, rowIdx, peerIdx,
frame);
return outAdapter.apply(result);
}
+
+ /** {@inheritDoc} */
+ @Override public boolean isAggAccumulator() {
+ return false;
+ }
}
/** */
@@ -237,6 +243,11 @@ final class WindowFunctionFactory<Row> extends
AccumulatorsFactoryBase<Row> {
return acc.end();
}
+ /** {@inheritDoc} */
+ @Override public boolean isAggAccumulator() {
+ return accumulator().accumulator() instanceof
IterableAccumulator<?>;
+ }
+
/** */
private AccumulatorWrapper<Row> accumulator() {
if (accHolder != null)
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionWrapper.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionWrapper.java
index 138ef14ae6c..e80fd491795 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionWrapper.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowFunctionWrapper.java
@@ -26,4 +26,7 @@ interface WindowFunctionWrapper<Row> {
/** Performs window function computation for the specified row inside the
buffered frame. */
@Nullable Object callBuffering(Row row, int rowIdx, int peerIdx,
WindowPartitionFrame<Row> frame);
+
+ /** Returns true if the window function has an accumulator stores incomig
row. */
+ boolean isAggAccumulator();
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartition.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartition.java
index 0f875faa11f..fb276ea8ff3 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartition.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartition.java
@@ -17,24 +17,8 @@
package org.apache.ignite.internal.processors.query.calcite.exec.exp.window;
-import java.util.function.Consumer;
-import org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
-import
org.apache.ignite.internal.processors.query.calcite.exec.tracker.RowTracker;
-
/** Partition of rows in window function calculation. */
public interface WindowPartition<Row> {
- /** Adding row to the window partition. */
- void add(Row row);
-
- /** Evaluates partition to an output. */
- void evalTo(RowHandler.RowFactory<Row> factory, Consumer<Row> output);
-
/** Reset current window partition (i.e., on partition restart). */
void reset();
-
- /** Returns {@code true} if partition is streaming. */
- boolean isStreaming();
-
- /** Sets memory tracker for current partition. */
- void attachMemoryTracker(RowTracker<Row> memoryTracker);
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionBase.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionBase.java
index 41aa3eb58c9..258088db91d 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionBase.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionBase.java
@@ -25,7 +25,7 @@ import org.jetbrains.annotations.Nullable;
/** Base implementation of window partition. */
abstract class WindowPartitionBase<Row> implements WindowPartition<Row> {
/** Comparator for computing the peer index. */
- private final Comparator<Row> peerCmp;
+ protected final Comparator<Row> peerCmp;
/** */
private final WindowFunctionFactory<Row> funcFactory;
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFactory.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFactory.java
index 88d0db30e9c..f111c30d56d 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFactory.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFactory.java
@@ -19,7 +19,6 @@ package
org.apache.ignite.internal.processors.query.calcite.exec.exp.window;
import java.util.Comparator;
import java.util.List;
-import java.util.function.Supplier;
import org.apache.calcite.rel.core.AggregateCall;
import org.apache.calcite.rel.core.Window;
import org.apache.calcite.rel.type.RelDataType;
@@ -28,56 +27,64 @@ import
org.apache.ignite.internal.processors.query.calcite.exec.RowHandler;
import org.apache.ignite.internal.processors.query.calcite.util.Commons;
/** Factory to create {@link WindowPartitionBase} factory from {@link
Window.Group}. */
-public final class WindowPartitionFactory<Row> implements
Supplier<WindowPartition<Row>> {
- /** */
- private final WindowFunctionFactory<Row> funcFactory;
-
+public final class WindowPartitionFactory<Row> {
/** */
private final ExecutionContext<Row> ctx;
/** */
- private final Window.Group grp;
-
- /** */
- private final RelDataType inputRowType;
+ public WindowPartitionFactory(ExecutionContext<Row> ctx) {
+ this.ctx = ctx;
+ }
- /** */
- private final RowHandler.RowFactory<Row> rowFactory;
+ /** Creates streaming window partition for provieded group, aggragates and
input row type, */
+ public StreamingWindowPartition<Row> newStreamingPartition(
+ Window.Group grp,
+ List<AggregateCall> calls,
+ RelDataType inputRowType
+ ) {
- /** */
- private final Comparator<Row> peerCmp;
+ assert WindowFunctions.streamable(grp);
- /** */
- private final boolean streamable;
+ return createPartition(grp, calls, inputRowType,
StreamingWindowPartition::new);
+ }
- /** */
- public WindowPartitionFactory(
- ExecutionContext<Row> ctx,
+ /** Creates buffering window partition for provieded group, aggragates and
input row type, */
+ public BufferingWindowPartition<Row> newBufferingPartition(
Window.Group grp,
List<AggregateCall> calls,
RelDataType inputRowType
) {
- this.ctx = ctx;
- this.grp = grp;
- this.inputRowType = inputRowType;
- List<RelDataType> aggTypes = Commons.transform(calls,
AggregateCall::getType);
- rowFactory = ctx.rowHandler().factory(Commons.typeFactory(), aggTypes);
+ return createPartition(grp, calls, inputRowType, (peerCmp,
funcFactory, rowFactory)
+ -> new BufferingWindowPartition<>(peerCmp, funcFactory,
rowFactory, ctx, grp, inputRowType));
+ }
+
+ /** */
+ private <T extends WindowPartition<Row>> T createPartition(
+ Window.Group grp,
+ List<AggregateCall> calls,
+ RelDataType inputRowType,
+ PartitionCreator<Row, T> creator
+ ) {
+ Comparator<Row> peerCmp;
if (grp.isRows)
// peer comparator in meaningless in rows frame.
peerCmp = null;
else
peerCmp = ctx.expressionFactory().comparator(grp.collation());
- streamable = WindowFunctions.streamable(grp);
- funcFactory = new WindowFunctionFactory<>(ctx, grp, calls,
inputRowType);
+ WindowFunctionFactory<Row> funcFactory = new
WindowFunctionFactory<>(ctx, grp, calls, inputRowType);
+
+ List<RelDataType> aggTypes = Commons.transform(calls,
AggregateCall::getType);
+ RowHandler.RowFactory<Row> rowFactory =
ctx.rowHandler().factory(Commons.typeFactory(), aggTypes);
+
+ return creator.create(peerCmp, funcFactory, rowFactory);
}
- /** {@inheritDoc} */
- @Override public WindowPartition<Row> get() {
- if (streamable)
- return new StreamWindowPartition<>(peerCmp, funcFactory,
rowFactory);
- else
- return new BufferingWindowPartition<>(peerCmp, funcFactory,
rowFactory, ctx, grp, inputRowType);
+ /** */
+ @FunctionalInterface
+ private interface PartitionCreator<Row, T extends WindowPartition<?>> {
+ /** */
+ T create(Comparator<Row> peerCmp, WindowFunctionFactory<Row>
funcFactory, RowHandler.RowFactory<Row> rowFactory);
}
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFrame.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFrame.java
index 25225f215ae..c69ef5f4d99 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFrame.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/window/WindowPartitionFrame.java
@@ -17,17 +17,16 @@
package org.apache.ignite.internal.processors.query.calcite.exec.exp.window;
-import java.util.Collections;
import java.util.List;
/** Rows frame within window partition. */
abstract class WindowPartitionFrame<Row> {
/** Holds immutable refrence to buffered window partition rows. */
- protected final List<Row> buf;
+ private final List<Row> buf;
/** */
WindowPartitionFrame(List<Row> buf) {
- this.buf = Collections.unmodifiableList(buf);
+ this.buf = buf;
}
/** Returns row from partition by index. */
@@ -56,7 +55,4 @@ abstract class WindowPartitionFrame<Row> {
final int size() {
return buf.size();
}
-
- /** Resets current frame. */
- protected abstract void reset();
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/BufferingWindowNode.java
similarity index 55%
copy from
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java
copy to
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/BufferingWindowNode.java
index ae4e0527eb3..ccd3247ff01 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/BufferingWindowNode.java
@@ -17,29 +17,34 @@
package org.apache.ignite.internal.processors.query.calcite.exec.rel;
-import java.util.ArrayDeque;
+import java.util.ArrayList;
import java.util.Comparator;
-import java.util.Deque;
-import java.util.function.Supplier;
+import java.util.List;
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.processors.query.calcite.exec.exp.window.BufferingWindowPartition;
+import
org.apache.ignite.internal.processors.query.calcite.exec.tracker.RowTracker;
import org.apache.ignite.internal.util.typedef.F;
+import static
org.apache.ignite.internal.processors.query.calcite.exec.rel.MemoryTrackingNode.DFLT_ROW_OVERHEAD;
+
/** Window node. */
-public class WindowNode<Row> extends MemoryTrackingNode<Row> implements
SingleNode<Row>, Downstream<Row> {
+public class BufferingWindowNode<Row> extends AbstractNode<Row> implements
SingleNode<Row>, Downstream<Row> {
/** */
private final Comparator<Row> partCmp;
/** */
- private final Supplier<WindowPartition<Row>> partFactory;
+ private final BufferingWindowPartition<Row> part;
/** */
private final RowHandler.RowFactory<Row> rowFactory;
/** */
- private WindowPartition<Row> part;
+ private List<Row> inBuf;
+
+ /** */
+ private RowTracker<Row> inBufTracker;
/** */
private int requested;
@@ -50,39 +55,39 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
/** */
private Row prevRow;
- /** */
- private final Deque<Row> outBuf = new ArrayDeque<>(IN_BUFFER_SIZE);
-
/** */
private boolean inLoop;
/** */
- public WindowNode(
+ public BufferingWindowNode(
ExecutionContext<Row> ctx,
RelDataType rowType,
Comparator<Row> partCmp,
- Supplier<WindowPartition<Row>> partFactory,
+ BufferingWindowPartition<Row> part,
RowHandler.RowFactory<Row> rowFactory
) {
- super(ctx, rowType, DFLT_ROW_OVERHEAD);
+ super(ctx, rowType);
this.partCmp = partCmp;
- this.partFactory = partFactory;
+ this.part = part;
this.rowFactory = rowFactory;
}
/** {@inheritDoc} */
@Override public void request(int rowsCnt) throws Exception {
assert !F.isEmpty(sources()) && sources().size() == 1;
- assert rowsCnt > 0;
+ assert rowsCnt > 0 && requested == 0;
checkState();
requested = rowsCnt;
- if (waiting == 0 && outBuf.isEmpty())
+ if (inLoop)
+ return;
+
+ if (part.isReady() || waiting < 0)
+ context().execute(this::flush, this::onError);
+ else if (waiting == 0)
source().request(waiting = IN_BUFFER_SIZE);
- else if (!inLoop)
- flush();
}
/** {@inheritDoc} */
@@ -94,24 +99,29 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
waiting--;
- if (part == null) {
- part = partFactory.get();
- part.attachMemoryTracker(nodeMemoryTracker);
- }
- else if (prevRow != null && partCmp != null &&
partCmp.compare(prevRow, row) != 0) {
- part.evalTo(rowFactory, this::appendToOutBuf);
- part.reset();
+ if (prevRow != null && partCmp != null && partCmp.compare(prevRow,
row) != 0) {
+ part.appendPartition(inBuf, inBufTracker::reset);
+ inBuf = null;
+ inBufTracker = null;
}
- part.add(row);
+ if (inBuf == null) {
+ inBuf = new ArrayList<>(IN_BUFFER_SIZE);
+ inBufTracker =
context().createNodeMemoryTracker(DFLT_ROW_OVERHEAD);
+ }
- if (part.isStreaming())
- part.evalTo(rowFactory, this::appendToOutBuf);
+ inBuf.add(row);
+ inBufTracker.onRowAdded(row);
prevRow = row;
- if (!inLoop)
+ if (inLoop)
+ return;
+
+ if (part.isReady())
flush();
+ else if (waiting == 0 && requested > 0)
+ context().execute(() -> source().request(waiting =
IN_BUFFER_SIZE), this::onError);
}
/** {@inheritDoc} */
@@ -124,9 +134,11 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
checkState();
- if (part != null) {
- part.evalTo(rowFactory, this::appendToOutBuf);
- part.reset();
+ // Append partition for remaining rows if any.
+ if (!F.isEmpty(inBuf)) {
+ part.appendPartition(inBuf, inBufTracker::reset);
+ inBuf = null;
+ inBufTracker = null;
}
flush();
@@ -136,12 +148,22 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
@Override protected void rewindInternal() {
requested = 0;
waiting = 0;
- if (part != null) {
- part.reset();
- part = null;
+ prevRow = null;
+ part.reset();
+ if (inBuf != null) {
+ inBufTracker.reset();
+ inBuf = null;
+ inBufTracker = null;
}
- outBuf.clear();
- nodeMemoryTracker.reset();
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void closeInternal() {
+ part.reset();
+ if (inBuf != null)
+ inBufTracker.reset();
+
+ super.closeInternal();
}
/** {@inheritDoc} */
@@ -152,32 +174,37 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
return this;
}
- /** */
- private void appendToOutBuf(Row row) {
- outBuf.add(row);
- nodeMemoryTracker.onRowAdded(row);
- }
-
/** */
private void flush() throws Exception {
inLoop = true;
try {
- while (requested > 0 && !outBuf.isEmpty()) {
+ int processed = 0;
+ while (requested > 0) {
+ Row result = part.nextRow(rowFactory);
+ if (result == null)
+ break;
+
requested--;
- Row row = outBuf.poll();
- nodeMemoryTracker.onRowRemoved(row);
- downstream().push(row);
+ downstream().push(result);
+
+ processed++;
+
+ if (processed == IN_BUFFER_SIZE && requested > 0) {
+ // Allow others to do their job.
+ context().execute(this::flush, this::onError);
+ return;
+ }
}
}
finally {
inLoop = false;
}
- if (waiting == 0 && requested > 0 && outBuf.isEmpty())
+ if (waiting == 0 && requested > 0)
context().execute(() -> source().request(waiting =
IN_BUFFER_SIZE), this::onError);
- if (waiting < 0 && requested > 0 && outBuf.isEmpty()) {
+ if (waiting < 0 && requested > 0 && !part.isReady()) {
requested = 0;
downstream().end();
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/StreamingWindowNode.java
similarity index 50%
rename from
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java
rename to
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/StreamingWindowNode.java
index ae4e0527eb3..8dc9f105bf8 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowNode.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/StreamingWindowNode.java
@@ -17,57 +17,43 @@
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.processors.query.calcite.exec.exp.window.StreamingWindowPartition;
import org.apache.ignite.internal.util.typedef.F;
-/** Window node. */
-public class WindowNode<Row> extends MemoryTrackingNode<Row> implements
SingleNode<Row>, Downstream<Row> {
+/** Non-buffering implementation of Window node. */
+public class StreamingWindowNode<Row> extends MemoryTrackingNode<Row>
implements SingleNode<Row>, Downstream<Row> {
/** */
private final Comparator<Row> partCmp;
- /** */
- private final Supplier<WindowPartition<Row>> partFactory;
-
/** */
private final RowHandler.RowFactory<Row> rowFactory;
/** */
- private WindowPartition<Row> part;
-
- /** */
- private int requested;
+ private final StreamingWindowPartition<Row> part;
/** */
- private int waiting;
+ private final boolean hasAggAccum;
/** */
private Row prevRow;
/** */
- private final Deque<Row> outBuf = new ArrayDeque<>(IN_BUFFER_SIZE);
-
- /** */
- private boolean inLoop;
-
- /** */
- public WindowNode(
+ public StreamingWindowNode(
ExecutionContext<Row> ctx,
RelDataType rowType,
Comparator<Row> partCmp,
- Supplier<WindowPartition<Row>> partFactory,
+ StreamingWindowPartition<Row> part,
RowHandler.RowFactory<Row> rowFactory
) {
super(ctx, rowType, DFLT_ROW_OVERHEAD);
this.partCmp = partCmp;
- this.partFactory = partFactory;
+ this.part = part;
this.rowFactory = rowFactory;
+ hasAggAccum = part.hasAggAccumulators();
}
/** {@inheritDoc} */
@@ -77,70 +63,43 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
checkState();
- requested = rowsCnt;
-
- if (waiting == 0 && outBuf.isEmpty())
- source().request(waiting = IN_BUFFER_SIZE);
- else if (!inLoop)
- flush();
+ source().request(rowsCnt);
}
/** {@inheritDoc} */
@Override public void push(Row row) throws Exception {
assert downstream() != null;
- assert waiting > 0;
checkState();
- waiting--;
-
- if (part == null) {
- part = partFactory.get();
- part.attachMemoryTracker(nodeMemoryTracker);
- }
- else if (prevRow != null && partCmp != null &&
partCmp.compare(prevRow, row) != 0) {
- part.evalTo(rowFactory, this::appendToOutBuf);
+ if (prevRow != null && partCmp != null && partCmp.compare(prevRow,
row) != 0) {
part.reset();
+ nodeMemoryTracker.reset();
}
- part.add(row);
+ Row result = part.eval(row, rowFactory);
- if (part.isStreaming())
- part.evalTo(rowFactory, this::appendToOutBuf);
+ if (hasAggAccum)
+ nodeMemoryTracker.onRowAdded(row);
prevRow = row;
- if (!inLoop)
- flush();
+ downstream().push(result);
}
/** {@inheritDoc} */
@Override public void end() throws Exception {
assert downstream() != null;
- if (waiting < 0)
- return;
-
- waiting = -1;
checkState();
- if (part != null) {
- part.evalTo(rowFactory, this::appendToOutBuf);
- part.reset();
- }
-
- flush();
+ downstream().end();
}
/** {@inheritDoc} */
@Override protected void rewindInternal() {
- requested = 0;
- waiting = 0;
- if (part != null) {
- part.reset();
- part = null;
- }
- outBuf.clear();
+ prevRow = null;
+ part.reset();
nodeMemoryTracker.reset();
}
@@ -151,35 +110,4 @@ public class WindowNode<Row> extends
MemoryTrackingNode<Row> implements SingleNo
return this;
}
-
- /** */
- private void appendToOutBuf(Row row) {
- outBuf.add(row);
- nodeMemoryTracker.onRowAdded(row);
- }
-
- /** */
- private void flush() throws Exception {
- inLoop = true;
- try {
- while (requested > 0 && !outBuf.isEmpty()) {
- requested--;
-
- Row row = outBuf.poll();
- nodeMemoryTracker.onRowRemoved(row);
- downstream().push(row);
- }
- }
- finally {
- inLoop = false;
- }
-
- if (waiting == 0 && requested > 0 && outBuf.isEmpty())
- context().execute(() -> source().request(waiting =
IN_BUFFER_SIZE), this::onError);
-
- if (waiting < 0 && requested > 0 && outBuf.isEmpty()) {
- requested = 0;
- downstream().end();
- }
- }
}
diff --git
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/WindowConverterRule.java
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/WindowConverterRule.java
index d550d981825..4cea7c737f3 100644
---
a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/WindowConverterRule.java
+++
b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/WindowConverterRule.java
@@ -106,6 +106,7 @@ public class WindowConverterRule extends
AbstractIgniteConverterRule<LogicalWind
for (int i = 0; i < calls.size(); i++) {
Window.RexWinAggCall aggCall = calls.get(i);
Window.RexWinAggCall newCall = new Window.RexWinAggCall(
+ aggCall.pos,
(SqlAggFunction)aggCall.op,
aggCall.type,
aggCall.operands,
diff --git
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowExecutionTest.java
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowExecutionTest.java
index 6abff318bd8..852e9f2e0bb 100644
---
a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowExecutionTest.java
+++
b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/WindowExecutionTest.java
@@ -23,7 +23,6 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;
-import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.calcite.rel.RelCollations;
@@ -36,14 +35,15 @@ import org.apache.calcite.rex.RexWindowBounds;
import org.apache.calcite.rex.RexWindowExclusion;
import org.apache.calcite.sql.SqlAggFunction;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableBitSet;
import org.apache.calcite.util.ImmutableIntList;
import
org.apache.ignite.internal.processors.query.calcite.exec.ArrayRowHandler;
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.IgniteRexBuilder;
-import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.WindowFunctions;
-import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.WindowPartition;
+import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.BufferingWindowPartition;
+import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.StreamingWindowPartition;
import
org.apache.ignite.internal.processors.query.calcite.exec.exp.window.WindowPartitionFactory;
import
org.apache.ignite.internal.processors.query.calcite.sql.fun.IgniteOwnSqlOperatorTable;
import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
@@ -64,12 +64,15 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
public void testRowNumber() {
checkWindow(rowNumber(), true,
new Object[][] {{1}, {2}, {1}, {2}, {3}, {1}});
+ checkWindow(rowNumber(), false,
+ new Object[][] {{1}, {2}, {1}, {2}, {3}, {1}});
}
/** dense_rank() over (partition by {0} order by {1}). */
@Test
public void testDenseRank() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.DENSE_RANK,
relIntType,
F.asList(),
@@ -89,12 +92,15 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
checkWindow(grp, true,
new Object[][] {{1}, {1}, {1}, {2}, {3}, {1}});
+ checkWindow(grp, false,
+ new Object[][] {{1}, {1}, {1}, {2}, {3}, {1}});
}
/** rank() over (partition by {0} order by {1}). */
@Test
public void testRank() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.RANK,
relIntType,
F.asList(),
@@ -114,12 +120,15 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
checkWindow(grp, true,
new Object[][] {{1}, {1}, {1}, {2}, {3}, {1}});
+ checkWindow(grp, false,
+ new Object[][] {{1}, {1}, {1}, {2}, {3}, {1}});
}
/** percent_rank() over (partition by {0} order by {1}). */
@Test
public void testRercentRank() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.PERCENT_RANK,
relDoubleType,
F.asList(),
@@ -145,6 +154,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testCumeDist() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.CUME_DIST,
relDoubleType,
F.asList(),
@@ -170,6 +180,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testFirstValue() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.FIRST_VALUE,
relIntType,
F.asList(rexBuilder.makeInputRef(relIntType, 1)),
@@ -195,6 +206,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLastValue() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.LAST_VALUE,
relIntType,
F.asList(rexBuilder.makeInputRef(relIntType, 2)),
@@ -220,6 +232,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testNTile() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.NTILE,
relIntType,
F.asList(rexBuilder.makeInputRef(relIntType, 3)),
@@ -245,6 +258,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testNth() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.NTH_VALUE,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(
@@ -273,6 +287,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLag1() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
IgniteOwnSqlOperatorTable.LAG,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(rexBuilder.makeInputRef(relIntType, 2)),
@@ -298,6 +313,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLag2() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
IgniteOwnSqlOperatorTable.LAG,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(
@@ -326,6 +342,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLag3() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
IgniteOwnSqlOperatorTable.LAG,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(
@@ -355,6 +372,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLead1() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
IgniteOwnSqlOperatorTable.LEAD,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(rexBuilder.makeInputRef(relIntType, 2)),
@@ -380,6 +398,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLead2() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
IgniteOwnSqlOperatorTable.LEAD,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(
@@ -408,6 +427,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
@Test
public void testLead3() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
IgniteOwnSqlOperatorTable.LEAD,
typeFactory.createTypeWithNullability(relIntType, true),
F.asList(
@@ -438,6 +458,8 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
public void testCountRowsBetweenUnboundedPrescendingAndCurrentRow() {
checkWindow(count(true, UNBOUNDED_PRECEDING, CURRENT_ROW), true,
new Object[][] {{1}, {2}, {1}, {2}, {3}, {1}});
+ checkWindow(count(true, UNBOUNDED_PRECEDING, CURRENT_ROW), false,
+ new Object[][] {{1}, {2}, {1}, {2}, {3}, {1}});
}
/** count(*) over (partition by {0} rows between unbounded prescending and
unbounded following). */
@@ -479,6 +501,8 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
public void testSumRowsAndRowNumberToCurrentRow() {
checkWindow(sumRowsAndRowNumber(CURRENT_ROW), true,
new Object[][] {{1, 1}, {2, 2}, {2, 1}, {4, 2}, {6, 3}, {3, 1}});
+ checkWindow(sumRowsAndRowNumber(CURRENT_ROW), false,
+ new Object[][] {{1, 1}, {2, 2}, {2, 1}, {4, 2}, {6, 3}, {3, 1}});
}
/**
@@ -529,9 +553,8 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
Node<Object[]> input,
Object[][] expRes
) {
- assertEquals(streaming, WindowFunctions.streamable(grp));
- WindowNode<Object[]> window = createWindowNode(ctx, grp, input);
+ Node<Object[]> window = createWindowNode(ctx, grp, input, streaming);
int resFldShift = input.rowType().getFieldCount();
@@ -549,7 +572,12 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
}
/** */
- private WindowNode<Object[]> createWindowNode(ExecutionContext<Object[]>
ctx, Window.Group grp, Node<Object[]> input) {
+ private Node<Object[]> createWindowNode(
+ ExecutionContext<Object[]> ctx,
+ Window.Group grp,
+ Node<Object[]> input,
+ boolean streaming
+ ) {
Class<?>[] outFields = new Class<?>[input.rowType().getFieldCount() +
grp.aggCalls.size()];
Arrays.fill(outFields, int.class);
RelDataType outRowType = TypeUtils.createRowType(typeFactory,
outFields);
@@ -559,16 +587,18 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
List<AggregateCall> aggCalls = toAggCall(grp);
- Supplier<WindowPartition<Object[]>> partitionFactory =
- new WindowPartitionFactory<>(ctx, grp, aggCalls, input.rowType());
+ WindowPartitionFactory<Object[]> partitionFactory = new
WindowPartitionFactory<>(ctx);
+
+ SingleNode<Object[]> window;
+ if (streaming) {
+ StreamingWindowPartition<Object[]> partition =
partitionFactory.newStreamingPartition(grp, aggCalls, input.rowType());
+ window = new StreamingWindowNode<>(ctx, outRowType, partCmp,
partition, rowFactory());
+ }
+ else {
+ BufferingWindowPartition<Object[]> partition =
partitionFactory.newBufferingPartition(grp, aggCalls, input.rowType());
+ window = new BufferingWindowNode<>(ctx, outRowType, partCmp,
partition, rowFactory());
+ }
- WindowNode<Object[]> window = new WindowNode<>(
- ctx,
- outRowType,
- partCmp,
- partitionFactory,
- rowFactory()
- );
window.register(input);
return window;
}
@@ -631,6 +661,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
/** row_number() over (partition by {0}). */
private static Window.Group rowNumber() {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.ROW_NUMBER,
relIntType,
F.asList(),
@@ -654,6 +685,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
*/
private static Window.Group count(boolean rows, RexWindowBound lower,
RexWindowBound upper) {
Window.RexWinAggCall aggCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.COUNT,
relIntType,
F.asList(rexBuilder.makeInputRef(relIntType, 0)),
@@ -678,6 +710,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
*/
private static Window.Group sumRowsAndRowNumber(RexWindowBound upper) {
Window.RexWinAggCall sumCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.SUM,
relIntType,
F.asList(rexBuilder.makeInputRef(relIntType, 0)),
@@ -686,6 +719,7 @@ public class WindowExecutionTest extends
AbstractExecutionParametrizedTest {
false
);
Window.RexWinAggCall rowNumberCall = new Window.RexWinAggCall(
+ SqlParserPos.ZERO,
SqlStdOperatorTable.ROW_NUMBER,
relIntType,
F.asList(),