db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r882732 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/SelectNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
Date Fri, 20 Nov 2009 21:59:47 GMT
Author: dag
Date: Fri Nov 20 21:59:47 2009
New Revision: 882732

URL: http://svn.apache.org/viewvc?rev=882732&view=rev
Log:
DERBY-4450 GROUP BY in an IN-subquery inside HAVING clause whose select list is subset of
group by columns, gives NPE

Patch derby-4450b + Knut's simplification of the autocommit call in GrooupByTest#testDerby4450.

This solves the problem seen in this issue, which was a regression
from DERBY-681.  The crux of the problem is that a PRN is added in the
result set tree without adjusting a higher level reference so that
wrong code is generated. The solution here is to reuse the result
column list in the inserted PRN, so that reference from above will
point correctly even after the PRN insertion (more details in JIRA).


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

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java?rev=882732&r1=882731&r2=882732&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java Fri
Nov 20 21:59:47 2009
@@ -1604,14 +1604,24 @@
 			// select c1 from t group by c1, c2
 			// we would have added c2 to the projection list which will have to be 
 			// projected out.
-			
-			ResultColumnList newSelectList = prnRSN.getResultColumns().copyListAndObjects(); 
-			newSelectList.removeGeneratedGroupingColumns();
-			newSelectList.genVirtualColumnNodes(prnRSN, prnRSN.getResultColumns());
+			//
+
+			// Keep the same RCL on top, since there may be
+			// references to its result columns above us, e.g. in this query:
+			//
+			// select sum(j),i from t group by i having i
+			//             in (select i from t group by i,j )
+			//
+			ResultColumnList topList = prnRSN.getResultColumns();
+			ResultColumnList newSelectList = topList.copyListAndObjects();
+			prnRSN.setResultColumns(newSelectList);
+
+			topList.removeGeneratedGroupingColumns();
+			topList.genVirtualColumnNodes(prnRSN, newSelectList);
 			prnRSN = (ResultSetNode) getNodeFactory().getNode(
 						C_NodeTypes.PROJECT_RESTRICT_NODE,
 						prnRSN,
-						newSelectList,
+						topList,
 						null,
 						null,
 						null,

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java?rev=882732&r1=882731&r2=882732&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
Fri Nov 20 21:59:47 2009
@@ -2243,4 +2243,31 @@
          
          JDBC.assertFullResultSet(rs, new String[][] {{"4","2"},{"5","2"}});
     }
-}
\ No newline at end of file
+
+    /**
+     * GROUP BY in an IN-subquery inside HAVING clause whose select list is
+     * subset of group by columns.
+     *
+     * @throws SQLException
+     */
+    public void testDerby4450() throws SQLException {
+        setAutoCommit(false);
+        Statement s = createStatement();
+        ResultSet rs;
+
+        s.executeUpdate(
+            "create table tt(i int not null," +
+            "               j int, k int)");
+        s.executeUpdate(
+            "insert into tt values " +
+            "    (1,10,1), (1,40,1),(3,45,1),(4,46,1),(5,90,1)");
+
+        rs = s.executeQuery(
+            "select sum(j) from tt group by i having i " +
+            "                     in (select i from tt group by i,j )");
+
+        JDBC.assertFullResultSet(rs, new String[][] {
+                {"50"},{"45"},{"46"},{"90"}});
+        rollback();
+    }
+}



Mime
View raw message