-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-28844 Calcite. Improve type checking in LIMIT / OFFSET clauses #13311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,29 +17,31 @@ | |
|
|
||
| package org.apache.ignite.internal.processors.query.calcite.exec.rel; | ||
|
|
||
| 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.util.IgniteMath; | ||
| import org.apache.ignite.internal.util.typedef.F; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** Offset, fetch|limit support node. */ | ||
| public class LimitNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> { | ||
| /** Offset if its present, otherwise 0. */ | ||
| private final int offset; | ||
| /** Offset param. */ | ||
| private final long offset; | ||
|
|
||
| /** Fetch if its present, otherwise 0. */ | ||
| private final int fetch; | ||
| /** Fetch param. */ | ||
| private final long fetch; | ||
|
|
||
| /** Fetch can be unset. */ | ||
| private final boolean fetchUndefined; | ||
|
|
||
| /** Already processed (pushed to upstream) rows count. */ | ||
| private int rowsProcessed; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comparing int with long, can overflow |
||
|
|
||
| /** Fetch can be unset, in this case we need all rows. */ | ||
| private @Nullable Supplier<Integer> fetchNode; | ||
|
|
||
| /** Waiting results counter. */ | ||
| private int waiting; | ||
|
|
||
| /** Upper requested rows. */ | ||
| private int requested; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
|
|
@@ -49,34 +51,37 @@ public class LimitNode<Row> extends AbstractNode<Row> implements SingleNode<Row> | |
| public LimitNode( | ||
| ExecutionContext<Row> ctx, | ||
| RelDataType rowType, | ||
| Supplier<Integer> offsetNode, | ||
| Supplier<Integer> fetchNode | ||
| long offset, | ||
| long fetch | ||
| ) { | ||
| super(ctx, rowType); | ||
|
|
||
| offset = offsetNode == null ? 0 : offsetNode.get(); | ||
| fetch = fetchNode == null ? 0 : fetchNode.get(); | ||
| this.fetchNode = fetchNode; | ||
| this.offset = offset; | ||
| fetchUndefined = fetch == -1; | ||
| this.fetch = fetch == -1 ? 0 : fetch; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public void request(int rowsCnt) throws Exception { | ||
| assert !F.isEmpty(sources()) && sources().size() == 1; | ||
| assert rowsCnt > 0; | ||
|
|
||
| if (fetchNone()) { | ||
| if (!hasMoreData()) { | ||
| end(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (offset > 0 && rowsProcessed == 0) | ||
| rowsCnt = offset + rowsCnt; | ||
| assert requested == 0 : requested; | ||
| requested = rowsCnt; | ||
|
|
||
| waiting = rowsCnt; | ||
| if (fetch > 0) { | ||
| long remain = IgniteMath.addExact(fetch, offset) - rowsProcessed; | ||
|
|
||
| if (fetch > 0) | ||
| rowsCnt = Math.min(rowsCnt, (fetch + offset) - rowsProcessed); | ||
| rowsCnt = remain > rowsCnt ? rowsCnt : (int)remain; | ||
| } | ||
|
|
||
| waiting = rowsCnt; | ||
|
|
||
| checkState(); | ||
|
|
||
|
|
@@ -85,38 +90,46 @@ public LimitNode( | |
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public void push(Row row) throws Exception { | ||
| if (waiting == -1) | ||
| if (waiting == NOT_WAITING) | ||
| return; | ||
|
|
||
| ++rowsProcessed; | ||
|
|
||
| --waiting; | ||
|
|
||
| checkState(); | ||
|
|
||
| if (rowsProcessed > offset) { | ||
| if (fetchNode == null || (fetchNode != null && rowsProcessed <= fetch + offset)) | ||
| downstream().push(row); | ||
| if (rowsProcessed >= offset && hasMoreData()) { | ||
| // this two rows can`t be swapped, cause if all requested rows have been pushed it will trigger further request call. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment should be started with an uppercase letter. |
||
| --requested; | ||
| downstream().push(row); | ||
| } | ||
|
|
||
| if (fetch > 0 && rowsProcessed == fetch + offset && waiting > 0) | ||
| ++rowsProcessed; | ||
|
|
||
| // There several cases are possible: | ||
| // 1) requested = 512, limit = 1, offset = not defined: need to pass 1 row and call end() | ||
| // 2) requested = 512, limit = 512, offset = not defined: just need to pass all rows without end() call | ||
| // 3) requested = 512, limit = 512, offset = 1: need to request initially 512 and further 1 row | ||
| if (!hasMoreData() && requested > 0) | ||
| end(); | ||
|
|
||
| if (waiting == 0 && requested > 0) | ||
| source().request(waiting = requested); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public void end() throws Exception { | ||
| if (waiting == -1) | ||
| if (waiting == NOT_WAITING) | ||
| return; | ||
|
|
||
| assert downstream() != null; | ||
|
|
||
| waiting = -1; | ||
| waiting = NOT_WAITING; | ||
|
|
||
| downstream().end(); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override protected void rewindInternal() { | ||
| waiting = 0; | ||
| requested = 0; | ||
| rowsProcessed = 0; | ||
| } | ||
|
|
||
|
|
@@ -128,8 +141,8 @@ public LimitNode( | |
| return this; | ||
| } | ||
|
|
||
| /** {@code True} if requested 0 results, or all already processed. */ | ||
| private boolean fetchNone() { | ||
| return (fetchNode != null && fetch == 0) || (fetch > 0 && rowsProcessed == fetch + offset); | ||
| /** {@code True} if fetch is undefined, or current rows processed is less than required. */ | ||
| private boolean hasMoreData() { | ||
| return fetchUndefined || rowsProcessed < IgniteMath.addExact(fetch, offset); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,11 @@ | |
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.PriorityQueue; | ||
| 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.util.IgniteMath; | ||
| import org.apache.ignite.internal.util.GridBoundedPriorityQueue; | ||
| import org.apache.ignite.internal.util.typedef.F; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Sort node. | ||
|
|
@@ -44,7 +43,7 @@ public class SortNode<Row> extends MemoryTrackingNode<Row> implements SingleNode | |
| private final PriorityQueue<Row> rows; | ||
|
|
||
| /** SQL select limit. Negative if disabled. */ | ||
| private final int limit; | ||
| private final long limit; | ||
|
|
||
| /** Reverse-ordered rows in case of limited sort. */ | ||
| private List<Row> reversed; | ||
|
|
@@ -58,21 +57,21 @@ public class SortNode<Row> extends MemoryTrackingNode<Row> implements SingleNode | |
| public SortNode( | ||
| ExecutionContext<Row> ctx, RelDataType rowType, | ||
| Comparator<Row> comp, | ||
| @Nullable Supplier<Integer> offset, | ||
| @Nullable Supplier<Integer> fetch | ||
| long offset, | ||
| long fetch | ||
| ) { | ||
| super(ctx, rowType); | ||
|
|
||
| assert fetch == null || fetch.get() >= 0; | ||
| assert offset == null || offset.get() >= 0; | ||
| assert fetch == -1 || fetch >= 0; | ||
| assert offset >= 0; | ||
|
|
||
| limit = fetch == null ? -1 : fetch.get() + (offset == null ? 0 : offset.get()); | ||
| limit = fetch == -1 ? -1 : (fetch > Long.MAX_VALUE - offset ? -1 : fetch + offset); | ||
|
|
||
| if (limit < 0) | ||
| if (limit < 1 || limit > Integer.MAX_VALUE) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rich this line with limit == 0? If yes, it's more correct to use `limit <= 1' or add other logic to return empty result |
||
| rows = new PriorityQueue<>(comp); | ||
| else { | ||
| rows = new GridBoundedPriorityQueue<>(limit, comp == null ? (Comparator<Row>)Comparator.reverseOrder() | ||
| : comp.reversed()); | ||
| rows = new GridBoundedPriorityQueue<>(IgniteMath.convertToIntExact(limit), comp == null ? | ||
| (Comparator<Row>)Comparator.reverseOrder() : comp.reversed()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -81,7 +80,7 @@ public SortNode( | |
| * @param comp Rows comparator. | ||
| */ | ||
| public SortNode(ExecutionContext<Row> ctx, RelDataType rowType, Comparator<Row> comp) { | ||
| this(ctx, rowType, comp, null, null); | ||
| this(ctx, rowType, comp, 0, -1); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
|
|
@@ -150,7 +149,7 @@ else if (!inLoop) | |
|
|
||
| checkState(); | ||
|
|
||
| waiting = -1; | ||
| waiting = NOT_WAITING; | ||
|
|
||
| flush(); | ||
| } | ||
|
|
@@ -160,7 +159,7 @@ private void flush() throws Exception { | |
| if (isClosed()) | ||
| return; | ||
|
|
||
| assert waiting == -1; | ||
| assert waiting == NOT_WAITING; | ||
|
|
||
| int processed = 0; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add
long limit = fetch == -1 ? Long.MAX_VALUE : IgniteMath.addExact(fetch, offset)instead. To executeIgniteMath.addExact(fetch, offset)only once (and not for each request and each push), to simplifyhasMoreData(rowsProcessed < limit).