db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r1428160 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ engine/org/apache/derby/loc/ shared/org/apache/derby/shared/common/reference/ testing/org/apache/derbyTesting/functionTests/master/ testing/org/apache/derbyT...
Date Thu, 03 Jan 2013 03:58:41 GMT
Author: dag
Date: Thu Jan  3 03:58:41 2013
New Revision: 1428160

URL: http://svn.apache.org/viewvc?rev=1428160&view=rev
Log:
DERBY-6009 Need stricter checking of ORDER BY clause in VALUES expressions

This patch, derby-6009c, improves on the restriction that ORDER BY for
VALUES be limited to column numbers (SQL-92 style) only. It should now
give an understandable error message if anything else is tried. It
adds a new error message, "4287B" and test cases.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByList.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj
    db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java?rev=1428160&r1=1428159&r2=1428160&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByColumn.java Thu
Jan  3 03:58:41 2013
@@ -233,6 +233,13 @@ public class OrderByColumn extends Order
 			}
 
 		}else{
+            if (list.isTableValueCtorOrdering()) {
+                // For VALUES, we only allow ordering by column number,
+                // SQL-92 style. This is a more general expression, so throw.
+                throw StandardException.newException(
+                        SQLState.LANG_TABLE_VALUE_CTOR_RESTRICTION);
+            }
+
 			/*checks for the conditions when using distinct*/
 			if (addedColumnOffset >= 0 &&
 					target instanceof SelectNode &&

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByList.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByList.java?rev=1428160&r1=1428159&r2=1428160&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByList.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/OrderByList.java Thu
Jan  3 03:58:41 2013
@@ -78,6 +78,25 @@ public class OrderByList extends Ordered
 	private boolean sortNeeded = true;
 	private int resultSetNumber = -1;
 
+    /**
+     * {@code true} if this instance orders a
+     * {@literal <table value constructor>}.
+     * See {@link #isTableValueCtorOrdering}.
+     */
+    private boolean isTableValueCtorOrdering;
+
+    /**
+     * Initialize with the type of the result set this {@code OrderByList} is
+     * attached to, e.g. {@code SELECT}, {@code VALUES} or a set operation.
+     * @param rs The result set this {@code OrderByList} is ordering.
+    */
+   public void init(Object rs) {
+        this.isTableValueCtorOrdering =
+                (rs instanceof UnionNode &&
+                ((UnionNode)rs).tableConstructor()) ||
+                rs instanceof RowResultSetNode;
+    }
+
 	/**
 		Add a column to the list
 	
@@ -846,4 +865,12 @@ public class OrderByList extends Ordered
 	public int getResultSetNumber() {
 		return resultSetNumber;
 	}
+
+    /**
+     * @return {@code true} if the {@code ORDER BY} is attached to a
+     * {@literal <table value constructor>}, i.e. a {@code VALUES} clause.
+     */
+    public boolean isTableValueCtorOrdering() {
+        return isTableValueCtorOrdering;
+    }
 }

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=1428160&r1=1428159&r2=1428160&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 Jan
 3 03:58:41 2013
@@ -3435,7 +3435,7 @@ preparableSelectStatement(boolean checkP
 }
 {
 	queryExpression = queryExpression(null, NO_SET_OP) 
-		[ orderCols = orderByClause() ]
+       [ orderCols = orderByClause(queryExpression) ]
         hasJDBClimitClause = offsetFetchFirstClause( offsetClauses )
 		[ <FOR> forUpdateState = forUpdateClause(updateColumns) ]
 		[ isolationLevel = atIsolationLevel() ]
@@ -5170,7 +5170,7 @@ nonJoinQueryPrimary() throws StandardExc
 |
     <LEFT_PAREN>
     primary = queryExpression(null, NO_SET_OP)
-    [ orderCols = orderByClause() ]
+    [ orderCols = orderByClause(primary) ]
     hasJDBClimitClause = offsetFetchFirstClause( offsetClauses )
 	<RIGHT_PAREN>
 	{
@@ -8291,12 +8291,12 @@ columnReference() throws StandardExcepti
 */
 
 OrderByList
-orderByClause() throws StandardException :
+orderByClause(ResultSetNode rs) throws StandardException :
 {
 	OrderByList orderCols;
 }
 {
-	<ORDER> <BY> orderCols = sortSpecificationList()
+   <ORDER> <BY> orderCols = sortSpecificationList(rs)
 	{
         forbidNextValueFor();
         
@@ -8318,10 +8318,11 @@ atIsolationLevel() throws StandardExcept
 }
 
 OrderByList
-sortSpecificationList() throws StandardException :
+sortSpecificationList(ResultSetNode rs) throws StandardException :
 { 
 	OrderByList orderCols = (OrderByList) nodeFactory.getNode(
 											C_NodeTypes.ORDER_BY_LIST,
+                                           rs,
 											getContextManager()); 
 }
 {
@@ -8683,7 +8684,7 @@ insertColumnsAndSource(QueryTreeNode tar
 	]
 	[ targetProperties = propertyList(false) <CHECK_PROPERTIES>]
 	queryExpression = queryExpression(null, NO_SET_OP)
-	[ orderCols = orderByClause() ]
+   [ orderCols = orderByClause(queryExpression) ]
     hasJDBClimitClause = offsetFetchFirstClause( offsetClauses )
 	{
 		if (orderCols != null && isTableValueConstructor(queryExpression)) {
@@ -8960,7 +8961,7 @@ subquery(int subqueryType, ValueNode lef
 }
 {
 	queryExpression = queryExpression(null, NO_SET_OP)
-	[ orderCols = orderByClause() ]
+   [ orderCols = orderByClause(queryExpression) ]
     hasJDBClimitClause = offsetFetchFirstClause( offsetClauses )
 	{
 		subqueryNode = (SubqueryNode) nodeFactory.getNode(
@@ -9924,7 +9925,7 @@ overClause() throws StandardException :
   LOOKAHEAD({ getToken(2).kind == LEFT_PAREN ||
 			  getToken(2).kind == IDENTIFIER })
 	<OVER>
-  ( <LEFT_PAREN> [ orderCols = orderByClause() ] <RIGHT_PAREN>
+  ( <LEFT_PAREN> [ orderCols = orderByClause(null) ] <RIGHT_PAREN>
 	{
 		return (QueryTreeNode) nodeFactory.getNode(
 			C_NodeTypes.WINDOW_DEFINITION_NODE,
@@ -10258,7 +10259,7 @@ windowDefinition(WindowList wl) throws S
 {
 	windowName = identifier(Limits.MAX_IDENTIFIER_LENGTH, true)
 	<AS>
-	<LEFT_PAREN> [ orderCols = orderByClause() ] <RIGHT_PAREN>
+   <LEFT_PAREN> [ orderCols = orderByClause(null) ] <RIGHT_PAREN>
 	{
 		wl.addWindow(
 			(WindowDefinitionNode)nodeFactory.getNode(
@@ -11280,7 +11281,7 @@ viewDefinition(Token beginToken) throws 
 	<VIEW> tableName = qualifiedName(Limits.MAX_IDENTIFIER_LENGTH) 
 		[ <LEFT_PAREN> resultColumns = viewColumnList() <RIGHT_PAREN> ]
 		<AS> queryExpression = queryExpression(null, NO_SET_OP)
-		[ orderCols = orderByClause() ]
+       [ orderCols = orderByClause(queryExpression) ]
         hasJDBClimitClause = offsetFetchFirstClause( offsetClauses )
 	{
 		checkOptionType = ViewDescriptor.NO_CHECK_OPTION;

Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=1428160&r1=1428159&r2=1428160&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Thu Jan  3 03:58:41
2013
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE messages SYSTEM "messages.dtd">
 
 <!-- 
@@ -1469,6 +1469,11 @@ Guide.
                 <text>The ORDER BY clause may not specify an expression, since the
query specifies DISTINCT.</text>
             </msg>
 
+            <msg>
+                <name>4287B</name>
+                <text>In this context, the ORDER BY clause may only specify a column
number.</text>
+            </msg>
+
 
             <msg>
                 <name>42884</name>

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java?rev=1428160&r1=1428159&r2=1428160&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
Thu Jan  3 03:58:41 2013
@@ -898,6 +898,7 @@ public interface SQLState {
         String LANG_UNION_ORDER_BY                                         = "42878";
 	String LANG_DISTINCT_ORDER_BY                                      = "42879";
 	String LANG_DISTINCT_ORDER_BY_EXPRESSION                           = "4287A";
+    String LANG_TABLE_VALUE_CTOR_RESTRICTION                           = "4287B";
 	String LANG_EMPTY_VALUES_CLAUSE                                    = "42X80";
 	String LANG_EMPTY_COLUMN_LIST                                      = "42X81";
 	String LANG_USING_CARDINALITY_VIOLATION                            = "42X82";

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out?rev=1428160&r1=1428159&r2=1428160&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
Thu Jan  3 03:58:41 2013
@@ -2320,5 +2320,30 @@ ij> drop table d3303;
 0 rows inserted/updated/deleted
 ij> -- See also Derby5005.java which should be merged with this test when
 -- it gets converted to JUnit.
-;
+
+-- DERBY-6009: forbid expression when using ORDER BY with VALUES
+values 1 order by 1+0;
+ERROR 4287B: In this context, the ORDER BY clause may only specify a column number.
+ij> values 1,2 order by 1+0;
+ERROR 4287B: In this context, the ORDER BY clause may only specify a column number.
+ij> (values 1 order by 1+0) union (values 1,2 order by 1);
+ERROR 4287B: In this context, the ORDER BY clause may only specify a column number.
+ij> -- OK:
+(values 1 order by 1) union (values 1,2 order by 1);
+1          
+-----------
+1          
+2          
+ij> values (1,-1),(3,-3),(2,-2) order by 1;
+1          |2          
+-----------------------
+1          |-1         
+2          |-2         
+3          |-3         
+ij> values (1,-1),(3,-3),(2,-2) order by 2;
+1          |2          
+-----------------------
+3          |-3         
+2          |-2         
+1          |-1         
 ij> 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql?rev=1428160&r1=1428159&r2=1428160&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
Thu Jan  3 03:58:41 2013
@@ -904,3 +904,13 @@ drop table d3303;
 
 -- See also Derby5005.java which should be merged with this test when
 -- it gets converted to JUnit.
+
+-- DERBY-6009: forbid expression when using ORDER BY with VALUES
+values 1 order by 1+0;
+values 1,2 order by 1+0;
+(values 1 order by 1+0) union (values 1,2 order by 1);
+-- OK:
+(values 1 order by 1) union (values 1,2 order by 1);
+values (1,-1),(3,-3),(2,-2) order by 1;
+values (1,-1),(3,-3),(2,-2) order by 2;
+



Mime
View raw message