You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by tl...@apache.org on 2021/04/21 13:38:42 UTC

[ignite] branch sql-calcite updated: IGNITE-14549 Calcite. ORDER BY column not from SELECT LIST

This is an automated email from the ASF dual-hosted git repository.

tledkov pushed a commit to branch sql-calcite
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/sql-calcite by this push:
     new 9c32f78  IGNITE-14549 Calcite. ORDER BY column not from SELECT LIST
9c32f78 is described below

commit 9c32f7874331044e31bb6eb3baa06ef959a06bff
Author: korlov42 <ko...@gridgain.com>
AuthorDate: Wed Apr 21 16:38:20 2021 +0300

    IGNITE-14549 Calcite. ORDER BY column not from SELECT LIST
---
 .../query/calcite/exec/ExecutionServiceImpl.java   | 20 +++++++++++++++--
 .../query/calcite/prepare/IgnitePlanner.java       |  3 +--
 .../query/calcite/CalciteQueryProcessorTest.java   | 25 ++++++++++++++++++++++
 .../query/calcite/planner/PlannerTest.java         |  2 +-
 4 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionServiceImpl.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionServiceImpl.java
index b9ca57d..449ddb5 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionServiceImpl.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/ExecutionServiceImpl.java
@@ -41,6 +41,8 @@ import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.RelRoot;
 import org.apache.calcite.rel.hint.Hintable;
 import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.sql.SqlDdl;
 import org.apache.calcite.sql.SqlExplain;
 import org.apache.calcite.sql.SqlExplainLevel;
@@ -50,6 +52,7 @@ import org.apache.calcite.sql.parser.SqlParseException;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.tools.Frameworks;
 import org.apache.calcite.tools.ValidationException;
+import org.apache.calcite.util.Pair;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.cache.query.FieldsQueryCursor;
@@ -104,6 +107,7 @@ import org.apache.ignite.internal.processors.query.calcite.prepare.Splitter;
 import org.apache.ignite.internal.processors.query.calcite.prepare.ValidationResult;
 import org.apache.ignite.internal.processors.query.calcite.prepare.ddl.DdlSqlToCommandConverter;
 import org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteProject;
 import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
 import org.apache.ignite.internal.processors.query.calcite.schema.SchemaHolder;
 import org.apache.ignite.internal.processors.query.calcite.trait.CorrelationTraitDef;
@@ -631,7 +635,7 @@ public class ExecutionServiceImpl<Row> extends AbstractService implements Execut
             // Convert to Relational operators graph
             RelRoot root = planner.rel(sqlNode);
 
-            RelNode rel = root.project();
+            RelNode rel = root.rel;
 
             if (rel instanceof Hintable)
                 planner.setDisabledRules(HintUtils.disabledRules((Hintable)rel));
@@ -645,7 +649,19 @@ public class ExecutionServiceImpl<Row> extends AbstractService implements Execut
                 .replace(root.collation == null ? RelCollations.EMPTY : root.collation)
                 .simplify();
 
-            return planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
+            IgniteRel igniteRel = planner.transform(PlannerPhase.OPTIMIZATION, desired, rel);
+
+            if (!root.isRefTrivial()) {
+                final List<RexNode> projects = new ArrayList<>();
+                final RexBuilder rexBuilder = igniteRel.getCluster().getRexBuilder();
+
+                for (int field : Pair.left(root.fields))
+                    projects.add(rexBuilder.makeInputRef(igniteRel, field));
+
+                igniteRel = new IgniteProject(igniteRel.getCluster(), desired, igniteRel, projects, root.validatedRowType);
+            }
+
+            return igniteRel;
         }
         catch (Throwable ex) {
             log.error("Unexpected error at query optimizer.", ex);
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java
index ec7daaf..fdf9b73 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/prepare/IgnitePlanner.java
@@ -208,7 +208,7 @@ public class IgnitePlanner implements Planner, RelOptTable.ViewExpander {
 
     /** {@inheritDoc} */
     @Override public RelNode convert(SqlNode sql) {
-        return rel(sql).project();
+        throw new UnsupportedOperationException();
     }
 
     /** {@inheritDoc} */
@@ -216,7 +216,6 @@ public class IgnitePlanner implements Planner, RelOptTable.ViewExpander {
         SqlToRelConverter sqlToRelConverter = sqlToRelConverter(validator(), catalogReader, sqlToRelConverterCfg);
         RelRoot root = sqlToRelConverter.convertQuery(sql, false, true);
         root = root.withRel(sqlToRelConverter.decorrelate(sql, root.rel));
-        root = root.withRel(root.project());
         root = trimUnusedFields(root);
 
         return root;
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java
index e2a8617..dcaa953 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java
@@ -135,6 +135,11 @@ public class CalciteQueryProcessorTest extends GridCommonAbstractTest {
         cache.put(1, "1");
         cache.put(2, "2");
 
+        assertQuery(client, "select val from tbl order by id")
+            .returns("1")
+            .returns("2")
+            .check();
+
         assertQuery(client, "select id, val from tbl")
             .returns(1, "1")
             .returns(2, "2")
@@ -463,6 +468,26 @@ public class CalciteQueryProcessorTest extends GridCommonAbstractTest {
 
     /** */
     @Test
+    public void testOrderingByColumnOutsideSelectList() throws InterruptedException {
+        populateTables();
+
+        assertQuery(client, "select salary from account order by _key desc")
+            .returns(13d)
+            .returns(13d)
+            .returns(13d)
+            .returns(12d)
+            .returns(11d)
+            .returns(10d)
+            .check();
+
+        assertQuery(client, "select name, sum(salary) from account group by name order by count(salary)")
+            .returns("Roman", 46d)
+            .returns("Igor1", 26d)
+            .check();
+    }
+
+    /** */
+    @Test
     public void testEqConditionWithDistinctSubquery() throws Exception {
         populateTables();
 
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTest.java
index 42787033..1150d5e 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTest.java
@@ -1955,7 +1955,7 @@ public class PlannerTest extends AbstractPlannerTest {
             sqlNode = planner.validate(sqlNode);
 
             // Convert to Relational operators graph
-            rel = planner.convert(sqlNode);
+            rel = planner.rel(sqlNode).rel;
 
             rel = planner.transform(PlannerPhase.HEURISTIC_OPTIMIZATION, rel.getTraitSet(), rel);