You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by al...@apache.org on 2021/06/17 09:36:55 UTC

[ignite] branch sql-calcite updated: IGNITE-14849 Support '%' operator - Fixes #9166.

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

alexpl 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 2b87beb  IGNITE-14849 Support '%' operator - Fixes #9166.
2b87beb is described below

commit 2b87bebe1878be1033dee06040d25611f65b540e
Author: Aleksey Plekhanov <pl...@gmail.com>
AuthorDate: Thu Jun 17 12:34:24 2021 +0300

    IGNITE-14849 Support '%' operator - Fixes #9166.
    
    Signed-off-by: Aleksey Plekhanov <pl...@gmail.com>
---
 .../query/calcite/CalciteQueryProcessor.java       |  7 ++--
 .../query/calcite/sql/IgniteSqlConformance.java    | 33 ++++++++++++++++
 .../processors/query/calcite/FunctionsTest.java    |  9 +++++
 .../query/calcite/logical/SqlScriptRunner.java     | 16 ++++----
 .../test/sql/aggregate/group/test_group_by.test    | 31 +--------------
 ...est_group_by.test => test_group_by.test_ignore} |  6 ++-
 .../calcite/src/test/sql/order/test_order_by.test  | 45 ----------------------
 ...est_order_by.test => test_order_by.test_ignore} |  1 +
 8 files changed, 60 insertions(+), 88 deletions(-)

diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
index b14491b..378d1b4 100644
--- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
@@ -18,7 +18,6 @@
 package org.apache.ignite.internal.processors.query.calcite;
 
 import java.util.List;
-
 import org.apache.calcite.config.Lex;
 import org.apache.calcite.plan.Contexts;
 import org.apache.calcite.rel.core.Aggregate;
@@ -27,7 +26,6 @@ import org.apache.calcite.sql.fun.SqlLibrary;
 import org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory;
 import org.apache.calcite.sql.parser.SqlParser;
 import org.apache.calcite.sql.util.SqlOperatorTables;
-import org.apache.calcite.sql.validate.SqlConformanceEnum;
 import org.apache.calcite.sql.validate.SqlValidator;
 import org.apache.calcite.sql2rel.SqlToRelConverter;
 import org.apache.calcite.tools.FrameworkConfig;
@@ -59,6 +57,7 @@ import org.apache.ignite.internal.processors.query.calcite.prepare.QueryPlanCach
 import org.apache.ignite.internal.processors.query.calcite.prepare.QueryPlanCacheImpl;
 import org.apache.ignite.internal.processors.query.calcite.schema.SchemaHolder;
 import org.apache.ignite.internal.processors.query.calcite.schema.SchemaHolderImpl;
+import org.apache.ignite.internal.processors.query.calcite.sql.IgniteSqlConformance;
 import org.apache.ignite.internal.processors.query.calcite.sql.IgniteSqlParserImpl;
 import org.apache.ignite.internal.processors.query.calcite.sql.fun.IgniteSqlOperatorTable;
 import org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
@@ -91,10 +90,10 @@ public class CalciteQueryProcessor extends GridProcessorAdapter implements Query
             SqlParser.config()
                 .withParserFactory(IgniteSqlParserImpl.FACTORY)
                 .withLex(Lex.ORACLE)
-                .withConformance(SqlConformanceEnum.DEFAULT))
+                .withConformance(IgniteSqlConformance.INSTANCE))
         .sqlValidatorConfig(SqlValidator.Config.DEFAULT
             .withIdentifierExpansion(true)
-            .withSqlConformance(SqlConformanceEnum.DEFAULT))
+            .withSqlConformance(IgniteSqlConformance.INSTANCE))
         // Dialects support.
         .operatorTable(SqlOperatorTables.chain(
             SqlLibraryOperatorTableFactory.INSTANCE
diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/sql/IgniteSqlConformance.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/sql/IgniteSqlConformance.java
new file mode 100644
index 0000000..52750fe
--- /dev/null
+++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/sql/IgniteSqlConformance.java
@@ -0,0 +1,33 @@
+/*
+ * 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.sql;
+
+import org.apache.calcite.sql.validate.SqlAbstractConformance;
+import org.apache.calcite.sql.validate.SqlConformance;
+
+/**
+ * Ignite specific SQL compatibility mode.
+ */
+public class IgniteSqlConformance extends SqlAbstractConformance {
+    /** Singleton. */
+    public static final SqlConformance INSTANCE = new IgniteSqlConformance();
+
+    /** {@inheritDoc} */
+    @Override public boolean isPercentRemainderAllowed() {
+        return true;
+    }
+}
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/FunctionsTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/FunctionsTest.java
index f200904..0d7bdcf 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/FunctionsTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/FunctionsTest.java
@@ -140,6 +140,15 @@ public class FunctionsTest extends GridCommonAbstractTest {
     }
 
     /** */
+    @Test
+    public void testPercentRemainder() {
+        checkQuery("SELECT 3 % 2").returns(1).check();
+        checkQuery("SELECT 4 % 2").returns(0).check();
+        checkQuery("SELECT NULL % 2").returns(new Object[] { null }).check();
+        checkQuery("SELECT 3 % NULL::int").returns(new Object[] { null }).check();
+    }
+
+    /** */
     private QueryChecker checkQuery(String qry) {
         return new QueryChecker(qry) {
             @Override protected QueryEngine getEngine() {
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/SqlScriptRunner.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/SqlScriptRunner.java
index 66493d1..291c1ac 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/SqlScriptRunner.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/logical/SqlScriptRunner.java
@@ -155,7 +155,7 @@ public class SqlScriptRunner {
 
         /** */
         String positionDescription() {
-            return fileName + ':' + lineNum;
+            return '(' + fileName + ':' + lineNum + ')';
         }
 
         /** {@inheritDoc} */
@@ -427,13 +427,13 @@ public class SqlScriptRunner {
                         break;
 
                     default:
-                        throw new IgniteException("Unknown type character '" + resTypesChars.charAt(i) + "'. "
+                        throw new IgniteException("Unknown type character '" + resTypesChars.charAt(i) + "' at: "
                             + script.positionDescription() + "[cmd=" + Arrays.toString(cmd) + ']');
                 }
             }
 
             if (F.isEmpty(resTypes)) {
-                throw new IgniteException("Missing type string. "
+                throw new IgniteException("Missing type string at: "
                     + script.positionDescription() + "[cmd=" + Arrays.toString(cmd) + ']');
             }
 
@@ -474,7 +474,7 @@ public class SqlScriptRunner {
                         singleValOnLine = true;
 
                     if (vals.length != resTypes.size() && !singleValOnLine) {
-                        throw new IgniteException("Invalid columns count at the result. "
+                        throw new IgniteException("Invalid columns count at the result at: "
                             + script.positionDescription() + " [row=\"" + s + "\", types=" + resTypes + ']');
                     }
 
@@ -497,7 +497,7 @@ public class SqlScriptRunner {
                         }
                     }
                     catch (Exception e) {
-                        throw new IgniteException("Cannot parse expected results. "
+                        throw new IgniteException("Cannot parse expected results at: "
                             + script.positionDescription() + "[row=\"" + s + "\", types=" + resTypes + ']', e);
                     }
 
@@ -529,7 +529,7 @@ public class SqlScriptRunner {
         /** */
         private void checkResultTuples(List<List<?>> res) {
             if (expectedRes.size() != res.size()) {
-                throw new AssertionError("Invalid results rows count at " + posDesc +
+                throw new AssertionError("Invalid results rows count at: " + posDesc +
                     ". [expected=" + expectedRes + ", actual=" + res + ']');
             }
 
@@ -538,12 +538,12 @@ public class SqlScriptRunner {
                 List<?> row = res.get(i);
 
                 if (row.size() != expectedRow.size()) {
-                    throw new AssertionError("Invalid columns count at " + posDesc +
+                    throw new AssertionError("Invalid columns count at: " + posDesc +
                         ". [expected=" + expectedRes + ", actual=" + res + ']');
                 }
 
                 for (int j = 0; j < expectedRow.size(); ++j) {
-                    checkEquals("Not expected result at " + posDesc +
+                    checkEquals("Not expected result at: " + posDesc +
                         ". [row=" + i + ", col=" + j +
                         ", expected=" + expectedRow.get(j) + ", actual=" + row.get(j) + ']', expectedRow.get(j), row.get(j));
                 }
diff --git a/modules/calcite/src/test/sql/aggregate/group/test_group_by.test b/modules/calcite/src/test/sql/aggregate/group/test_group_by.test
index 816402a..539e525 100644
--- a/modules/calcite/src/test/sql/aggregate/group/test_group_by.test
+++ b/modules/calcite/src/test/sql/aggregate/group/test_group_by.test
@@ -67,13 +67,6 @@ SELECT b, SUM(a), COUNT(*), SUM(a+2) FROM test GROUP BY b ORDER BY b;
 21	12.000000	1	14.000000
 22	24.000000	2	28.000000
 
-# group by alias
-query IR
-SELECT b % 2 AS f, SUM(a) FROM test GROUP BY f;
-----
-0	24.000000
-1	12.000000
-
 # group by with filter
 query IRIR
 SELECT b, SUM(a), COUNT(*), SUM(a+2) FROM test WHERE a <= 12 GROUP BY b ORDER BY b;
@@ -113,19 +106,6 @@ SELECT i, i + 10 FROM integers GROUP BY i ORDER BY i
 statement error
 SELECT i, SUM(j), j FROM integers GROUP BY i ORDER BY i
 
-# but it works if we wrap it in FIRST()
-query IRI
-SELECT i, SUM(j), FIRST(j) FROM integers GROUP BY i ORDER BY i
-----
-2	4.000000	4
-3	8.000000	4
-
-# group by constant alias
-query IR
-SELECT 1 AS k, SUM(i) FROM integers GROUP BY k ORDER BY 2;
-----
-1	8.000000
-
 # use an alias that is identical to a column name (should prioritize column name)
 query IR
 SELECT 1 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 2;
@@ -133,13 +113,6 @@ SELECT 1 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 2;
 1	2.000000
 1	6.000000
 
-# refer to the same alias twice
-query IR
-SELECT i % 2 AS k, SUM(i) FROM integers GROUP BY k, k ORDER BY 1;
-----
-0	2.000000
-1	6.000000
-
 statement ok
 DROP TABLE integers;
 
@@ -151,7 +124,7 @@ INSERT INTO integers VALUES (1), (2), (3), (NULL);
 
 # group by NULL
 query IR
-SELECT i, SUM(i) FROM integers GROUP BY i ORDER BY 1;
+SELECT i, SUM(i) FROM integers GROUP BY i ORDER BY 1 NULLS FIRST;
 ----
 NULL	NULL
 1	1.000000
@@ -160,7 +133,7 @@ NULL	NULL
 
 # column reference should have preference over alias reference in grouping
 query IIR
-SELECT i, i % 2 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 1;
+SELECT i, i % 2 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 1 NULLS FIRST;
 ----
 NULL	NULL	NULL
 1	1	1.000000
diff --git a/modules/calcite/src/test/sql/aggregate/group/test_group_by.test b/modules/calcite/src/test/sql/aggregate/group/test_group_by.test_ignore
similarity index 95%
copy from modules/calcite/src/test/sql/aggregate/group/test_group_by.test
copy to modules/calcite/src/test/sql/aggregate/group/test_group_by.test_ignore
index 816402a..cf53871 100644
--- a/modules/calcite/src/test/sql/aggregate/group/test_group_by.test
+++ b/modules/calcite/src/test/sql/aggregate/group/test_group_by.test_ignore
@@ -1,6 +1,8 @@
 # name: test/sql/aggregate/group/test_group_by.test
 # description: Test aggregation/group by statements
 # group: [group]
+# Ignored: https://issues.apache.org/jira/browse/IGNITE-14885
+# Ignored: https://issues.apache.org/jira/browse/IGNITE-14597
 
 statement ok
 PRAGMA enable_verification
@@ -151,7 +153,7 @@ INSERT INTO integers VALUES (1), (2), (3), (NULL);
 
 # group by NULL
 query IR
-SELECT i, SUM(i) FROM integers GROUP BY i ORDER BY 1;
+SELECT i, SUM(i) FROM integers GROUP BY i ORDER BY 1 NULLS FIRST;
 ----
 NULL	NULL
 1	1.000000
@@ -160,7 +162,7 @@ NULL	NULL
 
 # column reference should have preference over alias reference in grouping
 query IIR
-SELECT i, i % 2 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 1;
+SELECT i, i % 2 AS i, SUM(i) FROM integers GROUP BY i ORDER BY 1 NULLS FIRST;
 ----
 NULL	NULL	NULL
 1	1	1.000000
diff --git a/modules/calcite/src/test/sql/order/test_order_by.test b/modules/calcite/src/test/sql/order/test_order_by.test
index 40dd9e7..7e98746 100644
--- a/modules/calcite/src/test/sql/order/test_order_by.test
+++ b/modules/calcite/src/test/sql/order/test_order_by.test
@@ -106,13 +106,6 @@ SELECT b, a FROM test WHERE a < 13 ORDER BY b DESC;
 22	11
 21	12
 
-# order by expression
-query IR
-SELECT b % 2 AS f, SUM(a) FROM test GROUP BY f ORDER BY b % 2;
-----
-0	24.000000
-1	12.000000
-
 # order by expression that is not in SELECT
 query II
 SELECT b % 2 AS f, a FROM test ORDER BY b % 4;
@@ -121,19 +114,6 @@ SELECT b % 2 AS f, a FROM test ORDER BY b % 4;
 0	11
 0	13
 
-# ORDER BY alias
-query IR
-SELECT b % 2 AS f, SUM(a) FROM test GROUP BY f ORDER BY f;
-----
-0	24.000000
-1	12.000000
-
-query IR
-SELECT b % 2 AS f, SUM(a) FROM test GROUP BY f ORDER BY 1;
-----
-0	24.000000
-1	12.000000
-
 # ORDER BY after union
 query I
 SELECT a-10 AS k FROM test UNION SELECT a-10 AS l FROM test ORDER BY k;
@@ -142,32 +122,7 @@ SELECT a-10 AS k FROM test UNION SELECT a-10 AS l FROM test ORDER BY k;
 2
 3
 
-# ORDER BY on alias in right-most query
-# CONTROVERSIAL: SQLite allows both "k" and "l" to be referenced here, Postgres and MonetDB give an error.
-query I
-SELECT a-10 AS k FROM test UNION SELECT a-10 AS l FROM test ORDER BY l;
-----
-1
-2
-3
-
 # computations with aliases are not allowed though
 statement error
 SELECT a-10 AS k FROM test UNION SELECT a-10 AS l FROM test ORDER BY 1-k;
 
-# but ordering on computation elements should work
-query I
-SELECT a-10 AS k FROM test UNION SELECT a-10 AS l FROM test ORDER BY a-10;
-----
-1
-2
-3
-
-query I
-SELECT a-10 AS k FROM test UNION SELECT a-11 AS l FROM test ORDER BY a-11;
-----
-0
-1
-2
-3
-
diff --git a/modules/calcite/src/test/sql/order/test_order_by.test b/modules/calcite/src/test/sql/order/test_order_by.test_ignore
similarity index 97%
copy from modules/calcite/src/test/sql/order/test_order_by.test
copy to modules/calcite/src/test/sql/order/test_order_by.test_ignore
index 40dd9e7..2536c60 100644
--- a/modules/calcite/src/test/sql/order/test_order_by.test
+++ b/modules/calcite/src/test/sql/order/test_order_by.test_ignore
@@ -1,6 +1,7 @@
 # name: test/sql/order/test_order_by.test
 # description: Test ORDER BY keyword
 # group: [order]
+# Ignored: https://issues.apache.org/jira/browse/IGNITE-14885
 
 statement ok
 PRAGMA enable_verification