db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r756052 - in /db/derby/code/branches/10.4/java: engine/org/apache/derby/impl/sql/compile/GroupByNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
Date Thu, 19 Mar 2009 15:43:17 GMT
Author: dag
Date: Thu Mar 19 15:43:17 2009
New Revision: 756052

URL: http://svn.apache.org/viewvc?rev=756052&view=rev
Log:
DERBY-4071 AssertFailure when selecting rows from a table with CHARACTER and VARCHAR columns

Patch derby-4071-10_4 backport the trunk patch to 10.4 branch.

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

Modified: db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java?rev=756052&r1=756051&r2=756052&view=diff
==============================================================================
--- db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java
(original)
+++ db/derby/code/branches/10.4/java/engine/org/apache/derby/impl/sql/compile/GroupByNode.java
Thu Mar 19 15:43:17 2009
@@ -336,12 +336,17 @@
 	}
 
 	/**
-	 * In the query rewrite for group by, add the columns on which
-	 * we are doing the group by.
-
+	 * In the query rewrite for group by, add the columns on which we are doing
+	 * the group by.
+	 *
+	 * @return havingRefsToSubstitute visitors array. Return any
+	 *         havingRefsToSubstitute visitors since it is too early to apply
+	 *         them yet; we need the AggregateNodes unmodified until after
+	 *         we add the new columns for aggregation (DERBY-4071).
+	 *
 	 * @see #addNewColumnsForAggregation
 	 */
-	private void addUnAggColumns() throws StandardException
+	private ArrayList addUnAggColumns() throws StandardException
 	{
 		ResultColumnList bottomRCL  = childResult.getResultColumns();
 		ResultColumnList groupByRCL = resultColumns;
@@ -452,10 +457,13 @@
 		if (havingRefsToSubstitute != null)
 		{
 			Collections.sort(havingRefsToSubstitute,sorter);
-			for (int r = 0; r < havingRefsToSubstitute.size(); r++)
-				havingClause.accept(
-					(SubstituteExpressionVisitor)havingRefsToSubstitute.get(r));
-}
+			// DERBY-4071 Don't substitute quite yet; we need the AggrateNodes
+			// undisturbed until after we have had the chance to build the
+			// other columns.  (The AggrateNodes are shared via an alias from
+			// aggregateVector and from the expression tree under
+			// havingClause).
+		}
+		return havingRefsToSubstitute;
 	}
 
 	/**
@@ -535,11 +543,26 @@
 		throws StandardException
 	{
 		aggInfo = new AggregatorInfoList();
+		ArrayList havingRefsToSubstitute = null;
+
 		if (groupingList != null)
 		{
-			addUnAggColumns();
+			havingRefsToSubstitute = addUnAggColumns();
 		}
+
+		addAggregateColumns();
+
 		if (havingClause != null) {
+
+			// Now do the substitution of the group by expressions in the
+			// having clause.
+			if (havingRefsToSubstitute != null) {
+				for (int r = 0; r < havingRefsToSubstitute.size(); r++) {
+					havingClause.accept(
+						(SubstituteExpressionVisitor)havingRefsToSubstitute.get(r));
+				}
+			}
+
 			// we have replaced group by expressions in the having clause.
 			// there should be no column references in the having clause 
 			// referencing this table. Skip over aggregate nodes.
@@ -565,7 +588,7 @@
 				}
 			}
 		}
-		addAggregateColumns();
+
 	}
 	
 	/**
@@ -650,7 +673,7 @@
 			** Set the GB aggregrate result column to
 			** point to this.  The GB aggregate result
 			** was created when we called
-			** ReplaceAggregatesWithColumnReferencesVisitor()
+			** ReplaceAggregatesWithCRVisitor()
 			*/
 			newColumnRef = (ColumnReference) getNodeFactory().getNode(
 					C_NodeTypes.COLUMN_REFERENCE,

Modified: db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java?rev=756052&r1=756051&r2=756052&view=diff
==============================================================================
--- db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
(original)
+++ db/derby/code/branches/10.4/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByTest.java
Thu Mar 19 15:43:17 2009
@@ -110,6 +110,15 @@
 		st.execute("INSERT INTO d3904_T2 VALUES" +
 				"( DATE( '2008-10-01' ), 'something' )" ); 
 
+        st.executeUpdate("create table d4071(i int, v char(10))");
+        st.executeUpdate("insert into d4071 " +
+                         "       values (1, '0123456789')," +
+                         "              (1, '1234567890')," +
+                         "              (3, '2345678901')," +
+                         "              (4, '0123456789')," +
+                         "              (5, '1234567890')");
+
+
         // create an all types tables
         
         st.executeUpdate(
@@ -2010,5 +2019,20 @@
 					"FROM d3904_T1, D3904_T2 WHERE d3904_T1.D1='2008-10-02'"),
             new String[][] {  {"2008-10-02"} } );
 	}
-}
 
+
+    /**
+     * Test aggregate function on a GROUP BY column also present in a HAVING
+     * clause.  Iff the GROUP BY column is not the first column in the table,
+     * this would fail before DERBY-4071 was fixed.
+     *
+     * @throws SQLException
+     */
+    public void testDerby4071AggregateOnGroupByColumnInHaving() throws SQLException {
+         Statement s = createStatement();
+         ResultSet rs = s.executeQuery("SELECT MAX(i), COUNT(T.V) FROM d4071 T " +
+                                       "    GROUP BY T.V HAVING COUNT(T.V) > 1");
+         
+         JDBC.assertFullResultSet(rs, new String[][] {{"4","2"},{"5","2"}});
+    }
+}



Mime
View raw message