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 2020/06/22 11:58:14 UTC

[GitHub] [ignite] gvvinblade commented on a change in pull request #7903: IGNITE-13025: add IgniteLimit logical node

gvvinblade commented on a change in pull request #7903:
URL: https://github.com/apache/ignite/pull/7903#discussion_r443500274



##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/LimitNode.java
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.function.Supplier;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ *
+ */
+public class LimitNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> {
+    /** */
+    private static final int NOT_READY = -1;
+
+    /** */
+    private static final int LIMIT_NOT_SET = -2;
+
+    /** */
+    private final Supplier<Integer> offsetSup;
+
+    /** */
+    private final Supplier<Integer> limitSup;
+
+    /** */
+    private int offset = NOT_READY;
+
+    /** */
+    private int limit = NOT_READY;
+
+    /** */
+    private int requested;
+
+    /** */
+    private int rowNum;
+
+    /** */
+    private int waiting;
+
+    /** */
+    boolean ended;
+
+    /**
+     * @param ctx Execution context.
+     * @param offsetSup Offset parameter supplier.
+     * @param limitSup Limit parameter supplier.
+     */
+    public LimitNode(
+        ExecutionContext<Row> ctx,
+        Supplier<Integer> offsetSup,
+        Supplier<Integer> limitSup) {
+        super(ctx);
+
+        this.offsetSup = offsetSup;
+        this.limitSup = limitSup;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) {
+        checkThread();
+
+        assert !F.isEmpty(sources) && sources.size() == 1;
+        assert rowsCnt > 0 && requested == 0;
+
+        requested = rowsCnt;
+
+        // Initialize offset / limit.
+        if (offset == NOT_READY && limit == NOT_READY) {
+            if (offsetSup != null) {
+                offset = offsetSup.get();
+
+                if (offset < 0)
+                    onError(new IgniteSQLException("Invalid query offset: " + offset));
+            }
+            else
+                offset = 0;
+
+            if (limitSup != null) {
+                limit = limitSup.get();
+
+                if (limit < 0)
+                    onError(new IgniteSQLException("Invalid query limit: " + limit));
+            }
+            else
+                limit = LIMIT_NOT_SET;
+        }
+
+        request0(requested);
+    }
+
+    /**
+     * Process request (some parameters may not yet be calculated).
+     */
+    private void request0(int rowsCnt) {
+        if (limit == 0 || limit > 0 && rowNum >= limit + offset) {
+            end();
+
+            return;
+        }
+
+        int req = rowsCnt;
+
+        if (rowNum == 0 && offset > 0)
+            req += offset;
+
+        if (limit > 0 && rowNum + req > limit + offset)
+            req = limit + offset - rowNum;
+
+        waiting = req;
+
+        F.first(sources).request(req);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void push(Row row) {
+        checkThread();
+
+        assert downstream != null;
+
+        waiting--;
+
+        if (!ended && rowNum >= offset) {
+            requested--;
+
+            downstream.push(row);
+        }
+
+        rowNum++;
+
+        if (requested > 0 && limit > 0 && rowNum >= limit + offset)
+            end();

Review comment:
       let's cancel upstream only here and do not do it inside end() method

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/Inbox.java
##########
@@ -143,9 +152,22 @@ public void init(ExecutionContext<Row> ctx, Collection<UUID> nodes, Comparator<R
 
     /** {@inheritDoc} */
     @Override public void cancel() {
-        checkThread();
-        context().markCancelled();
+        if (isCancelled())
+            return;
+
+        nodes.forEach(nodeId -> {
+            try {

Review comment:
       let's move it into a specific method of the Buffer class

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/message/ErrorMessage.java
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.message;
+
+import java.nio.ByteBuffer;
+import java.util.UUID;
+import org.apache.ignite.plugin.extensions.communication.MessageReader;
+import org.apache.ignite.plugin.extensions.communication.MessageWriter;
+
+/**
+ *
+ */
+public class ErrorMessage implements ExecutionContextAware {
+    /** */
+    private UUID queryId;
+
+    /** */
+    private long fragmentId;
+
+    /** */
+    private long exchangeId;
+
+    /** */
+    private byte[] errBytes;
+
+    /** */
+    public ErrorMessage() {
+        // No-op.
+    }
+
+    /** */
+    public ErrorMessage(UUID queryId, long fragmentId, long exchangeId, byte[] errBytes) {
+        this.queryId = queryId;
+        this.fragmentId = fragmentId;
+        this.exchangeId = exchangeId;
+        this.errBytes = errBytes;

Review comment:
       Let's implement `MarshalableMessage` and put marshaling logic into appropriate methods

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/LimitNode.java
##########
@@ -0,0 +1,185 @@
+/*
+ * 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.function.Supplier;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.calcite.exec.ExecutionContext;
+import org.apache.ignite.internal.util.typedef.F;
+
+/**
+ *
+ */
+public class LimitNode<Row> extends AbstractNode<Row> implements SingleNode<Row>, Downstream<Row> {
+    /** */
+    private static final int NOT_READY = -1;
+
+    /** */
+    private static final int LIMIT_NOT_SET = -2;
+
+    /** */
+    private final Supplier<Integer> offsetSup;
+
+    /** */
+    private final Supplier<Integer> limitSup;
+
+    /** */
+    private int offset = NOT_READY;
+
+    /** */
+    private int limit = NOT_READY;
+
+    /** */
+    private int requested;
+
+    /** */
+    private int rowNum;
+
+    /** */
+    private int waiting;
+
+    /** */
+    boolean ended;
+
+    /**
+     * @param ctx Execution context.
+     * @param offsetSup Offset parameter supplier.
+     * @param limitSup Limit parameter supplier.
+     */
+    public LimitNode(
+        ExecutionContext<Row> ctx,
+        Supplier<Integer> offsetSup,
+        Supplier<Integer> limitSup) {
+        super(ctx);
+
+        this.offsetSup = offsetSup;
+        this.limitSup = limitSup;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void request(int rowsCnt) {
+        checkThread();
+
+        assert !F.isEmpty(sources) && sources.size() == 1;
+        assert rowsCnt > 0 && requested == 0;
+
+        requested = rowsCnt;
+
+        // Initialize offset / limit.
+        if (offset == NOT_READY && limit == NOT_READY) {
+            if (offsetSup != null) {
+                offset = offsetSup.get();
+
+                if (offset < 0)
+                    onError(new IgniteSQLException("Invalid query offset: " + offset));
+            }
+            else
+                offset = 0;
+
+            if (limitSup != null) {
+                limit = limitSup.get();
+
+                if (limit < 0)
+                    onError(new IgniteSQLException("Invalid query limit: " + limit));
+            }
+            else
+                limit = LIMIT_NOT_SET;
+        }
+
+        request0(requested);
+    }
+
+    /**
+     * Process request (some parameters may not yet be calculated).
+     */
+    private void request0(int rowsCnt) {
+        if (limit == 0 || limit > 0 && rowNum >= limit + offset) {
+            end();
+
+            return;
+        }
+
+        int req = rowsCnt;
+
+        if (rowNum == 0 && offset > 0)
+            req += offset;
+
+        if (limit > 0 && rowNum + req > limit + offset)
+            req = limit + offset - rowNum;
+
+        waiting = req;
+
+        F.first(sources).request(req);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void push(Row row) {
+        checkThread();
+
+        assert downstream != null;
+
+        waiting--;
+
+        if (!ended && rowNum >= offset) {
+            requested--;
+
+            downstream.push(row);
+        }
+
+        rowNum++;
+
+        if (requested > 0 && limit > 0 && rowNum >= limit + offset)
+            end();
+
+        if (!ended && requested > 0 && waiting == 0)
+            request0(IN_BUFFER_SIZE);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void end() {
+        checkThread();
+
+        if (ended)
+            return;
+
+        ended = true;
+
+        sources.get(0).cancel();

Review comment:
       We shouldn't cancel upstream here

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/Outbox.java
##########
@@ -157,24 +154,26 @@ public void init() {
         U.error(context().planningContext().logger(),
             "Error occurred during execution: " + X.getFullStackTrace(e));
 
-        cancel(); // TODO send cause to originator.
+        dest.targets().forEach(nodeId -> {

Review comment:
       let's move it into specific Buffer method

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rule/SortConverterRule.java
##########
@@ -40,12 +42,22 @@ public SortConverterRule() {
     }
 
     /** {@inheritDoc} */
-    @Override protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalSort rel) {
-        RelOptCluster cluster = rel.getCluster();
-        RelTraitSet outTraits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(rel.getCollation());
+    @Override protected PhysicalNode convert(RelOptPlanner planner, RelMetadataQuery mq, LogicalSort sort) {
+        RelOptCluster cluster = sort.getCluster();
+        RelTraitSet outTraits = cluster.traitSetOf(IgniteConvention.INSTANCE).replace(sort.getCollation());
         RelTraitSet inTraits = cluster.traitSetOf(IgniteConvention.INSTANCE);
-        RelNode input = convert(rel.getInput(), inTraits);
+        RelNode input = convert(sort.getInput(), inTraits);
 
-        return new IgniteSort(cluster, outTraits, input, rel.getCollation(), rel.offset, rel.fetch);
+        IgniteRel res = null;
+
+        if (!sort.getCollation().getFieldCollations().isEmpty())

Review comment:
       let's split into two rules: for sort and for limit (see `RelOptRule.matched()` method to separate rules better)

##########
File path: modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/type/IgniteTypeSystem.java
##########
@@ -20,10 +20,15 @@
 import java.io.Serializable;
 import org.apache.calcite.rel.type.RelDataTypeSystem;
 import org.apache.calcite.rel.type.RelDataTypeSystemImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
 
 /**
  * Ignite type system.
  */
 public class IgniteTypeSystem extends RelDataTypeSystemImpl implements Serializable {
     public static final RelDataTypeSystem INSTANCE = new IgniteTypeSystem();
+
+    @Override public int getDefaultPrecision(SqlTypeName typeName) {

Review comment:
       redundant overriding




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