Index: java/engine/org/apache/derby/impl/sql/compile/VerifyAggregateExpressionsVisitor.java =================================================================== --- java/engine/org/apache/derby/impl/sql/compile/VerifyAggregateExpressionsVisitor.java (revision 652184) +++ java/engine/org/apache/derby/impl/sql/compile/VerifyAggregateExpressionsVisitor.java (working copy) @@ -119,15 +119,7 @@ SQLState.LANG_INVALID_NON_GROUPED_SELECT_LIST : SQLState.LANG_INVALID_GROUPED_SELECT_LIST); } - } else if (node instanceof JavaToSQLValueNode) - { - // disallow any expression which involves native java computation. - // Not possible to consider java expressions for equivalence. - throw StandardException.newException( (groupByList == null) ? - SQLState.LANG_INVALID_NON_GROUPED_SELECT_LIST : - SQLState.LANG_INVALID_GROUPED_SELECT_LIST); } - return node; } Index: java/engine/org/apache/derby/impl/sql/compile/GroupByList.java =================================================================== --- java/engine/org/apache/derby/impl/sql/compile/GroupByList.java (revision 652184) +++ java/engine/org/apache/derby/impl/sql/compile/GroupByList.java (working copy) @@ -220,6 +220,13 @@ */ numColsAddedHere++; } + if (groupingCol.getColumnExpression() instanceof JavaToSQLValueNode) + { + // disallow any expression which involves native java computation. + // Not possible to consider java expressions for equivalence. + throw StandardException.newException( + SQLState.LANG_INVALID_GROUPED_SELECT_LIST); + } } /* Verify that no subqueries got added to the dummy list */ Index: java/testing/org/apache/derbyTesting/functionTests/tests/lang/RoutineTest.java =================================================================== --- java/testing/org/apache/derbyTesting/functionTests/tests/lang/RoutineTest.java (revision 652184) +++ java/testing/org/apache/derbyTesting/functionTests/tests/lang/RoutineTest.java (working copy) @@ -23,6 +23,7 @@ import java.io.UnsupportedEncodingException; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Time; @@ -461,6 +462,23 @@ s.close(); } + /** + * Test function with an aggregate argument. DERBY-3649 + * @throws SQLException + */ + public void testAggregateArgument() throws SQLException + { + Statement s = createStatement(); + s.executeUpdate("CREATE TABLE TEST (I INT)"); + s.executeUpdate("INSERT INTO TEST VALUES(1)"); + s.executeUpdate("INSERT INTO TEST VALUES(2)"); + s.executeUpdate("CREATE FUNCTION CheckCount(count integer) RETURNS INTEGER PARAMETER STYLE JAVA NO SQL LANGUAGE JAVA EXTERNAL NAME 'org.apache.derbyTesting.functionTests.tests.lang.RoutineTest.checkCount'"); + ResultSet rs = s.executeQuery("select checkCount(count(*)) from test"); + JDBC.assertSingleValueResultSet(rs, "2"); + + + } + /* ** Routine implementations called from the tests but do * not use DriverManager so that this test can be used on @@ -492,5 +510,19 @@ return t; } + + + public static int checkCount(int count) + throws SQLException { + // throws ZeroException { + + if (count == 0) { + //throw new ZeroException(); + throw new SQLException("No results found", "38777"); + } + + return count; + } + } Index: java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByExpressionTest.java =================================================================== --- java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByExpressionTest.java (revision 652184) +++ java/testing/org/apache/derbyTesting/functionTests/tests/lang/GroupByExpressionTest.java (working copy) @@ -478,7 +478,9 @@ // disallow java function assertCompileError( "42Y30", "select r(), count(*) from test group by r()"); - + + assertCompileError( + "42Y30", "select count(*) from test group by r()"); // invalid grouping expression. assertCompileError( "42Y30", "select c1+1, count(*) from test group by c1+2");