Index: java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql	(revision 545903)
+++ java/testing/org/apache/derbyTesting/functionTests/tests/lang/orderby.sql	(working copy)
@@ -636,3 +636,40 @@
 select id, id i from D2459_A1 union select id j, id from D2459_A2 order by 2;
 select id, ref from D2459_A1 union select ref, id from D2459_A2;
 select id i, ref j from D2459_A1 union select ref i, id j from D2459_A2;
+
+-- Some test cases for DERBY-2351. The issue in DERBY-2351 involves whether
+-- pulled-up ORDER BY columns appear in the result set or not, and how
+-- DISCTINCT interacts with that decision. The point is that DISTINCT should
+-- apply only to the columns specified by the user in the result column list,
+-- not to the extra columns pulled up into the result by the ORDER BY. This
+-- means that some queries should throw an error, but due to DERBY-2351
+-- the queries instead display erroneous results.
+
+create table t1 (c1 int, c2 varchar(10));
+insert into t1 values (3, 'a'), (4, 'c'), (2, 'b'), (1, 'c');
+-- This query should return 4 distinct rows, ordered by column c1:
+select distinct c1, c2 from t1 order by c1;
+-- DERBY-2351 causes this statement to return 4 rows, which it should
+-- instead show an error:
+select distinct c1, c2 from t1 order by c1+1;
+-- DERBY-2351 causes this statement to return 4 rows, which it should
+-- instead show an error. Note that the rows returned are not distinct!
+select distinct c2 from t1 order by c1;
+-- This query should return 3 distinct rows, ordered by column c2
+select distinct c2 from t1 order by c2;
+drop table t1;
+
+create table person (name varchar(10), age int);
+insert into person values ('John', 10);
+insert into person values ('John', 30);
+insert into person values ('Mary', 20);
+-- DERBY-2351 causes this statement to display 3 rows, when it should
+-- instead show an error. Again, note that the rows returned are not distinct.
+SELECT DISTINCT name FROM person ORDER BY age;
+-- This query should return two rows, ordered by name.
+SELECT DISTINCT name FROM person ORDER BY name;
+-- This query should return two rows, ordered by name descending:
+SELECT DISTINCT name FROM person ORDER BY name desc;
+drop table person;
+
+
Index: java/testing/org/apache/derbyTesting/functionTests/master/orderby.out
===================================================================
--- java/testing/org/apache/derbyTesting/functionTests/master/orderby.out	(revision 545903)
+++ java/testing/org/apache/derbyTesting/functionTests/master/orderby.out	(working copy)
@@ -1674,4 +1674,81 @@
 b   |NULL
 g   |c   
 NULL|d   
+ij> -- Some test cases for DERBY-2351. The issue in DERBY-2351 involves whether
+-- pulled-up ORDER BY columns appear in the result set or not, and how
+-- DISCTINCT interacts with that decision. The point is that DISTINCT should
+-- apply only to the columns specified by the user in the result column list,
+-- not to the extra columns pulled up into the result by the ORDER BY. This
+-- means that some queries should throw an error, but due to DERBY-2351
+-- the queries instead display erroneous results.
+
+create table t1 (c1 int, c2 varchar(10));
+0 rows inserted/updated/deleted
+ij> insert into t1 values (3, 'a'), (4, 'c'), (2, 'b'), (1, 'c');
+4 rows inserted/updated/deleted
+ij> -- This query should return 4 distinct rows, ordered by column c1:
+select distinct c1, c2 from t1 order by c1;
+C1         |C2        
+----------------------
+1          |c         
+2          |b         
+3          |a         
+4          |c         
+ij> -- DERBY-2351 causes this statement to return 4 rows, which it should
+-- instead show an error:
+select distinct c1, c2 from t1 order by c1+1;
+C1         |C2        
+----------------------
+1          |c         
+2          |b         
+3          |a         
+4          |c         
+ij> -- DERBY-2351 causes this statement to return 4 rows, which it should
+-- instead show an error. Note that the rows returned are not distinct!
+select distinct c2 from t1 order by c1;
+C2        
+----------
+c         
+b         
+a         
+c         
+ij> -- This query should return 3 distinct rows, ordered by column c2
+select distinct c2 from t1 order by c2;
+C2        
+----------
+a         
+b         
+c         
+ij> drop table t1;
+0 rows inserted/updated/deleted
+ij> create table person (name varchar(10), age int);
+0 rows inserted/updated/deleted
+ij> insert into person values ('John', 10);
+1 row inserted/updated/deleted
+ij> insert into person values ('John', 30);
+1 row inserted/updated/deleted
+ij> insert into person values ('Mary', 20);
+1 row inserted/updated/deleted
+ij> -- DERBY-2351 causes this statement to display 3 rows, when it should
+-- instead show an error. Again, note that the rows returned are not distinct.
+SELECT DISTINCT name FROM person ORDER BY age;
+NAME      
+----------
+John      
+Mary      
+John      
+ij> -- This query should return two rows, ordered by name.
+SELECT DISTINCT name FROM person ORDER BY name;
+NAME      
+----------
+John      
+Mary      
+ij> -- This query should return two rows, ordered by name descending:
+SELECT DISTINCT name FROM person ORDER BY name desc;
+NAME      
+----------
+Mary      
+John      
+ij> drop table person;
+0 rows inserted/updated/deleted
 ij> 
\ No newline at end of file
