Index: java/engine/org/apache/derby/impl/sql/compile/TernaryOperatorNode.java =================================================================== --- java/engine/org/apache/derby/impl/sql/compile/TernaryOperatorNode.java (revision 468290) +++ java/engine/org/apache/derby/impl/sql/compile/TernaryOperatorNode.java (working copy) @@ -921,10 +921,18 @@ if (isSameNodeType(o)) { TernaryOperatorNode other = (TernaryOperatorNode)o; + + /* + * SUBSTR function can either have 2 or 3 arguments. In the + * 2-args case, rightOperand will be null and thus needs + * additional handling in the equivalence check. + */ return (other.methodName.equals(methodName) && other.receiver.isEquivalent(receiver) && other.leftOperand.isEquivalent(leftOperand) - && other.rightOperand.isEquivalent(rightOperand)); + && ( (rightOperand == null && other.rightOperand == null) || + (other.rightOperand != null && + other.rightOperand.isEquivalent(rightOperand)) ) ); } return false; } Index: java/testing/org/apache/derbyTesting/functionTests/tests/lang/groupBy.sql =================================================================== --- java/testing/org/apache/derbyTesting/functionTests/tests/lang/groupBy.sql (revision 468290) +++ java/testing/org/apache/derbyTesting/functionTests/tests/lang/groupBy.sql (working copy) @@ -360,3 +360,15 @@ -- ok, gives one row select 10,avg(c) from t having 1 < 2; drop table t; + +-- DERBY-2008 +-- test SUBSTR with 2 args with GROUP BY expression +create table dt (vc varchar(30)); +insert into dt values ('1928-09-21'), ('1903-12-08'); +-- ok +select substr(vc, 3) from dt group by substr(vc, 3); +select substr(vc, 3, 4) from dt group by substr(vc, 3, 4); +-- expect errors +select substr(vc, 3, 4) from dt group by substr(vc, 3); +select substr(vc, 3) from dt group by substr(vc, 3, 4); +drop table dt; Index: java/testing/org/apache/derbyTesting/functionTests/master/groupBy.out =================================================================== --- java/testing/org/apache/derbyTesting/functionTests/master/groupBy.out (revision 468290) +++ java/testing/org/apache/derbyTesting/functionTests/master/groupBy.out (working copy) @@ -636,4 +636,28 @@ 10 |2 ij> drop table t; 0 rows inserted/updated/deleted +ij> -- DERBY-2008 +-- test SUBSTR with 2 args with GROUP BY expression +create table dt (vc varchar(30)); +0 rows inserted/updated/deleted +ij> insert into dt values ('1928-09-21'), ('1903-12-08'); +2 rows inserted/updated/deleted +ij> -- ok +select substr(vc, 3) from dt group by substr(vc, 3); +1 +------------------------------ +03-12-08 +28-09-21 +ij> select substr(vc, 3, 4) from dt group by substr(vc, 3, 4); +1 +---- +03-1 +28-0 +ij> -- expect errors +select substr(vc, 3, 4) from dt group by substr(vc, 3); +ERROR 42Y30: The SELECT list of a grouped query contains at least one invalid expression. If a SELECT list has a GROUP BY, the list may only contain valid grouping expressions and valid aggregate expressions. +ij> select substr(vc, 3) from dt group by substr(vc, 3, 4); +ERROR 42Y30: The SELECT list of a grouped query contains at least one invalid expression. If a SELECT list has a GROUP BY, the list may only contain valid grouping expressions and valid aggregate expressions. +ij> drop table dt; +0 rows inserted/updated/deleted ij>