db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r916075 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj testing/org/apache/derbyTesting/functionTests/tests/lang/OffsetFetchNextTest.java
Date Thu, 25 Feb 2010 01:13:57 GMT
Author: dag
Date: Thu Feb 25 01:13:57 2010
New Revision: 916075

URL: http://svn.apache.org/viewvc?rev=916075&view=rev
Log:
DERBY-4562 Compilation of prepared statement results in Syntax Error

Patch derby-4562c. This fixes a problem with the parser logic to
determine if OFFSET is used as an identifer or is the start of an
offset clause. (Derby does not make OFFSET a reserved word for
compatibility reasons.)

The patch also adds new test cases.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OffsetFetchNextTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj?rev=916075&r1=916074&r2=916075&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj Thu Feb
25 01:13:57 2010
@@ -1254,6 +1254,49 @@
 	}
 
 
+
+    /**
+     * Determine if we are seeing an offsetClause or the identifier OFFSET
+     * (Derby does not make it a reserved word).  "n" must be an integer
+     * literal or a dynamic parameter specification.
+     *
+     * @return true if it is an offsetClause.
+     */
+    private boolean seeingOffsetClause()
+    {
+        int nesting = 0;
+
+        // Token number, i == 1: OFFSET
+        int i = 2;
+
+        int tokKind = getToken(i).kind;
+
+        // check for integer literal or ? followed by ROW(S)
+        if (tokKind == PLUS_SIGN ||
+            tokKind == MINUS_SIGN) {
+
+            tokKind = getToken(++i).kind;
+
+            if (tokKind == EXACT_NUMERIC) {
+
+                tokKind = getToken(++i).kind;
+
+                return (tokKind == ROW ||
+                        tokKind == ROWS);
+            }
+        } else if (tokKind == EXACT_NUMERIC ||
+                   tokKind == QUESTION_MARK) {
+
+            tokKind = getToken(++i).kind;
+
+            return (tokKind == ROW ||
+                    tokKind == ROWS);
+        }
+
+        return false;
+    }
+
+
 	/**
 	 * Determine whether the next sequence of tokens can be the beginning
 	 * of a rowValueConstructorList. A rowValueConstructorList is a comma-
@@ -14495,10 +14538,7 @@
 	|	tok = <OFF>
 	|	LOOKAHEAD({
 			getToken(1).kind == OFFSET &&
-			!(getToken(2).kind == PLUS_SIGN ||
-			  getToken(2).kind == MINUS_SIGN ||
-			  getToken(2).kind == EXACT_NUMERIC ||
-			  getToken(2).kind == QUESTION_MARK)
+			!seeingOffsetClause()
 		})
 		tok = <OFFSET>
 	|	tok = <OLD>

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OffsetFetchNextTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OffsetFetchNextTest.java?rev=916075&r1=916074&r2=916075&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OffsetFetchNextTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/OffsetFetchNextTest.java
Thu Feb 25 01:13:57 2010
@@ -115,6 +115,9 @@
         assertStatementError(LANG_INVALID_ROW_COUNT_OFFSET, st,
                              "select * from t1 offset -1 rows");
 
+        assertStatementError(LANG_SYNTAX_ERROR, st,
+                             "select * from t1 offset -? rows");
+
         assertStatementError(LANG_INVALID_ROW_COUNT_FIRST, st,
                              "select * from t1 fetch first 0 rows only");
 
@@ -139,12 +142,35 @@
     public void testNewKeywordNonReserved()
             throws Exception
     {
-        prepareStatement("select a,b as OFFSET from t1 OFFSET 0 rows");
+        setAutoCommit(false);
+        prepareStatement("select a,b as offset from t1 offset 0 rows");
 
         // Column and table correlation name usage
-        prepareStatement("select a,b from t1 AS OFFSET");
+        prepareStatement("select a,b from t1 as offset");
+
+        prepareStatement("select a,b offset from t1 offset");
+        prepareStatement("select a,b offset from t1 offset +2 rows");
+        prepareStatement("select a offset,b from t1 offset ? rows");
+        prepareStatement("select offset.a, offset.b offset from t1 as offset offset ? rows");
 
-        prepareStatement("select a,b OFFSET from t1 OFFSET");
+        // DERBY-4562
+        Statement s = createStatement();
+        s.executeUpdate("create table t4562(i int, offset int)");
+        ResultSet rs = s.executeQuery(
+            "select * from t4562 where i > 0 and offset + i < 0 offset 2 rows");
+        rs.next();
+
+        rs = s.executeQuery(
+            "select * from t4562 where i > 0 and offset - i < 0 offset 2 rows");
+        rs.next();
+
+        rs = s.executeQuery(
+            "select * from t4562 where i > 0 and offset * i < 0 offset 2 rows");
+        rs.next();
+
+        rs.close();
+
+        rollback();
     }
 
 



Mime
View raw message