[CALCITE-2637] In SQL parser, allow prefix '-' between BETWEEN and AND (Qi Yu)

For example, "WHERE deptno BETWEEN - deptno AND 5".

Fix Mimer URL; add a test (Julian Hyde)

Close apache/calcite#941


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/453f171d
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/453f171d
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/453f171d

Branch: refs/heads/master
Commit: 453f171d900ac8ac04bed95bbdd59c986ba4e726
Parents: 847e76c
Author: park.yq <[email protected]>
Authored: Mon Nov 26 21:12:45 2018 +0800
Committer: Julian Hyde <[email protected]>
Committed: Fri Nov 30 20:29:36 2018 -0800

----------------------------------------------------------------------
 core/src/main/codegen/templates/Parser.jj       |  6 +++--
 .../calcite/sql/parser/SqlParserTest.java       | 24 +++++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/453f171d/core/src/main/codegen/templates/Parser.jj
----------------------------------------------------------------------
diff --git a/core/src/main/codegen/templates/Parser.jj 
b/core/src/main/codegen/templates/Parser.jj
index 6361391..247c8b0 100644
--- a/core/src/main/codegen/templates/Parser.jj
+++ b/core/src/main/codegen/templates/Parser.jj
@@ -3031,6 +3031,7 @@ List<Object> Expression2(ExprContext exprContext) :
 {
     final List<Object> list = new ArrayList();
     List<Object> list2;
+    final List<Object> list3 = new ArrayList();
     SqlNodeList nodeList;
     SqlNode e;
     SqlOperator op;
@@ -3106,9 +3107,10 @@ List<Object> Expression2(ExprContext exprContext) :
                         <ASYMMETRIC>
                     ]
                 )
-                e = Expression3(ExprContext.ACCEPT_SUB_QUERY) {
+                Expression2b(ExprContext.ACCEPT_SUB_QUERY, list3) {
                     list.add(new SqlParserUtil.ToTreeListItem(op, s.pos()));
-                    list.add(e);
+                    list.addAll(list3);
+                    list3.clear();
                 }
             |
                 {

http://git-wip-us.apache.org/repos/asf/calcite/blob/453f171d/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java 
b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
index 6d5d1dd..535aa15 100644
--- a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -83,7 +83,7 @@ public class SqlParserTest {
    * the SQL:92, SQL:99, SQL:2003, SQL:2011, SQL:2014 standards and in Calcite.
    *
    * <p>The standard keywords are derived from
-   * <a 
href="http://developer.mimer.com/validator/sql-reserved-words.tml";>Mimer</a>
+   * <a 
href="https://developer.mimer.com/wp-content/uploads/2018/05/Standard-SQL-Reserved-Words-Summary.pdf";>Mimer</a>
    * and from the specification.
    *
    * <p>If a new <b>reserved</b> keyword is added to the parser, include it in
@@ -731,6 +731,28 @@ public class SqlParserTest {
             + "INNER JOIN `DEPT` AS `D` (`DEPTNO`, `DNAME`) ON (`EMP`.`DEPTNO` 
= `DEPT`.`DEPTNO`)");
   }
 
+  /** Test case that does not reproduce but is related to
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-2637";>[CALCITE-2637]
+   * Prefix '-' operator failed between BETWEEN and AND</a>. */
+  @Test public void testBetweenAnd() {
+    final String sql = "select * from emp\n"
+        + "where deptno between - DEPTNO + 1 and 5";
+    final String expected = "SELECT *\n"
+        + "FROM `EMP`\n"
+        + "WHERE (`DEPTNO` BETWEEN ASYMMETRIC ((- `DEPTNO`) + 1) AND 5)";
+    sql(sql).ok(expected);
+  }
+
+  @Test public void testBetweenAnd2() {
+    final String sql = "select * from emp\n"
+        + "where deptno between - DEPTNO + 1 and - empno - 3";
+    final String expected = "SELECT *\n"
+        + "FROM `EMP`\n"
+        + "WHERE (`DEPTNO` BETWEEN ASYMMETRIC ((- `DEPTNO`) + 1)"
+        + " AND ((- `EMPNO`) - 3))";
+    sql(sql).ok(expected);
+  }
+
   @Ignore
   @Test public void testDerivedColumnListNoAs() {
     check("select * from emp e (empno, gender) where true", "foo");

Reply via email to