Index: C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java =================================================================== --- C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java (revision 264896) +++ C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/ResultSetNode.java (working copy) @@ -72,6 +72,7 @@ import java.util.Properties; import java.util.Vector; +import java.util.Set; /** * A ResultSetNode represents a result set, that is, a set of rows. It is @@ -1857,9 +1858,10 @@ * Is it possible to do a distinct scan on this ResultSet tree. * (See SelectNode for the criteria.) * + * @param distinctColumns the set of distinct columns * @return Whether or not it is possible to do a distinct scan on this ResultSet tree. */ - boolean isPossibleDistinctScan() + boolean isPossibleDistinctScan(Set distinctColumns) { return false; } Index: C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java =================================================================== --- C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java (revision 264896) +++ C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/SelectNode.java (working copy) @@ -43,6 +43,7 @@ import org.apache.derby.iapi.util.JBitSet; import java.util.Vector; +import java.util.HashSet; /** * A SelectNode represents the result set for any of the basic DML @@ -1335,14 +1336,44 @@ * FromBaseTable. This is because all of a table's columns must come * from the same conglomerate in order to get consistent data. */ + boolean distinctScanPossible = false; if (origFromListSize == 1 && (! orderByAndDistinctMerged) && - resultColumns.countNumberOfSimpleColumnReferences() == resultColumns.size() && - prnRSN.isPossibleDistinctScan()) + resultColumns.countNumberOfSimpleColumnReferences() == resultColumns.size()) { - prnRSN.markForDistinctScan(); + boolean simpleColumns = true; + HashSet distinctColumns = new HashSet(); + int size = resultColumns.size(); + for (int i = 1; i <= size; i++) { + ValueNode vn = resultColumns.getResultColumn(i).getExpression(); + BaseColumnNode source = null; + while (source == null) { + if (vn instanceof ResultColumn) { + vn = ((ResultColumn) vn).getExpression(); + } else if (vn instanceof ColumnReference) { + vn = ((ColumnReference) vn).getSource(); + } else if (vn instanceof VirtualColumnNode) { + vn = ((VirtualColumnNode) vn).getSourceColumn(); + } else if (vn instanceof BaseColumnNode) { + source = (BaseColumnNode) vn; + } else { + break; + } + } + if (source != null) { + distinctColumns.add(source); + } else { + simpleColumns = false; + break; + } + } + if (simpleColumns && prnRSN.isPossibleDistinctScan(distinctColumns)) { + prnRSN.markForDistinctScan(); + distinctScanPossible = true; + } } - else + + if (!distinctScanPossible) { /* We can't do a distinct scan. Determine if we can filter out * duplicates without a sorter. Index: C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/ProjectRestrictNode.java =================================================================== --- C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/ProjectRestrictNode.java (revision 264896) +++ C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/ProjectRestrictNode.java (working copy) @@ -64,6 +64,8 @@ import org.apache.derby.iapi.services.classfile.VMOpcode; import java.util.Properties; +import java.util.HashSet; +import java.util.Set; /** * A ProjectRestrictNode represents a result set for any of the basic DML @@ -1608,16 +1610,39 @@ * Is it possible to do a distinct scan on this ResultSet tree. * (See SelectNode for the criteria.) * + * @param distinctColumns the set of distinct columns * @return Whether or not it is possible to do a distinct scan on this ResultSet tree. */ - boolean isPossibleDistinctScan() + boolean isPossibleDistinctScan(Set distinctColumns) { if (restriction != null || (restrictionList != null && restrictionList.size() != 0)) { return false; } - return childResult.isPossibleDistinctScan(); + + HashSet columns = new HashSet(); + for (int i = 0; i < resultColumns.size(); i++) { + ResultColumn rc = (ResultColumn) resultColumns.elementAt(i); + ValueNode vn = rc.getExpression(); + BaseColumnNode source = null; + while (source == null) { + if (vn instanceof ResultColumn) { + vn = ((ResultColumn) vn).getExpression(); + } else if (vn instanceof ColumnReference) { + vn = ((ColumnReference) vn).getSource(); + } else if (vn instanceof VirtualColumnNode) { + vn = ((VirtualColumnNode) vn).getSourceColumn(); + } else if (vn instanceof BaseColumnNode) { + source = (BaseColumnNode) vn; + } else { + return false; + } + } + columns.add(source); + } + + return columns.equals(distinctColumns) && childResult.isPossibleDistinctScan(distinctColumns); } /** Index: C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/FromBaseTable.java =================================================================== --- C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/FromBaseTable.java (revision 264896) +++ C:/derbyt/svn/trunk/java/engine/org/apache/derby/impl/sql/compile/FromBaseTable.java (working copy) @@ -96,6 +96,8 @@ import java.util.Enumeration; import java.util.Properties; import java.util.Vector; +import java.util.HashSet; +import java.util.Set; /** * A FromBaseTable represents a table in the FROM list of a DML statement, @@ -4005,12 +4007,22 @@ * Is it possible to do a distinct scan on this ResultSet tree. * (See SelectNode for the criteria.) * + * @param distinctColumns the set of distinct columns * @return Whether or not it is possible to do a distinct scan on this ResultSet tree. */ - boolean isPossibleDistinctScan() + boolean isPossibleDistinctScan(Set distinctColumns) { - return (restrictionList == null || - restrictionList.size() == 0); + if ((restrictionList != null && restrictionList.size() != 0)) { + return false; + } + + HashSet columns = new HashSet(); + for (int i = 0; i < resultColumns.size(); i++) { + ResultColumn rc = (ResultColumn) resultColumns.elementAt(i); + columns.add(rc.getExpression()); + } + + return columns.equals(distinctColumns); } Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/distinct.sql =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/distinct.sql (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/distinct.sql (working copy) @@ -340,5 +340,42 @@ -- get the storage system to do the sort. select * from td, (select distinct x from td) as sub(x); --- reset autocomiit +-- Tests for DERBY-504 (select distinct from a subquery) + +create table names (id int, name varchar(10), age int); + +insert into names (id, name, age) values + (1, 'Anna', 23), (2, 'Ben', 24), (3, 'Carl', 25), + (4, 'Anna', 23), (5, 'Ben', 24), (6, 'Carl', 25); + +call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1); +maximumdisplaywidth 20000; + +-- distinct names should be returned +select distinct name from (select name, id from names) as n; + +-- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); + +-- distinct names should be returned +select distinct name from (select name from names) as n; + +-- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); + +select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m; + +-- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); + +select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m; + +-- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); + +call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(0); + +drop table names; + +-- reset autocommit autocommit on; Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/groupBy.out =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/groupBy.out (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/groupBy.out (working copy) @@ -293,7 +293,6 @@ ------------------------------------------------------------------------ 0 |goodbye everyone is here 0 |hello everyone is here -0 |hello everyone is here 100 |hello everyone is here 0 |hello noone is here NULL |NULL Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/distinct.out =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/distinct.out (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/distinct.out (working copy) @@ -2502,6 +2502,288 @@ 1 |1 2 |2 2 |1 -ij> -- reset autocomiit +ij> -- Tests for DERBY-504 (select distinct from a subquery) +create table names (id int, name varchar(10), age int); +0 rows inserted/updated/deleted +ij> insert into names (id, name, age) values + (1, 'Anna', 23), (2, 'Ben', 24), (3, 'Carl', 25), + (4, 'Anna', 23), (5, 'Ben', 24), (6, 'Carl', 25); +6 rows inserted/updated/deleted +ij> call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1); +0 rows inserted/updated/deleted +ij> maximumdisplaywidth 20000; +ij> -- distinct names should be returned +select distinct name from (select name, id from names) as n; +NAME +---------- +Anna +Ben +Carl +ij> -- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + -- distinct names should be returned +select distinct name from (select name, id from names) as n +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Sort ResultSet: +Number of opens = 1 +Rows input = 6 +Rows returned = 3 +Eliminate duplicates = true +In sorted order = false +Sort information: + Number of rows input=6 + Number of rows output=3 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 +Source result set: + Project-Restrict ResultSet (3): + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + restriction = false + projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 + Source result set: + Table Scan ResultSet for NAMES at read committed isolation level using share row locking chosen by the optimizer + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + Fetch Size = 1 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: +null stop position: +null qualifiers: +None +ij> -- distinct names should be returned +select distinct name from (select name from names) as n; +NAME +---------- +Anna +Ben +Carl +ij> -- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + -- distinct names should be returned +select distinct name from (select name from names) as n +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Distinct Scan ResultSet for NAMES using index xxxxFILTERED-UUIDxxxx at read committed isolation level using instantaneous share row locking: +Number of opens = 1 +Hash table size = 3 +Distinct column is column number 1 +Rows seen = 3 +Rows filtered = 0 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 +scan information: + Bit set of columns fetched={1} + Number of columns fetched=1 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: + None + stop position: + None + scan qualifiers: +None + next qualifiers: +None +ij> select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m; +A |B |B |A +--------------------------------------------- +Anna |1 |1 |Anna +Carl |6 |6 |Carl +Ben |5 |5 |Ben +Carl |3 |3 |Carl +Ben |2 |2 |Ben +Anna |4 |4 |Anna +ij> -- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Project-Restrict ResultSet (2): +Number of opens = 1 +Rows seen = 6 +Rows filtered = 0 +restriction = false +projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 +Source result set: + Distinct Scan ResultSet for NAMES using index xxxxFILTERED-UUIDxxxx at read committed isolation level using instantaneous share row locking: + Number of opens = 1 + Hash table size = 6 + Distinct columns are column numbers (0,1) + Rows seen = 6 + Rows filtered = 0 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: + None + stop position: + None + scan qualifiers: +None + next qualifiers: +None +ij> select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m; +A |A +--------------------- +Anna |Anna +Ben |Ben +Carl |Carl +ij> -- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Sort ResultSet: +Number of opens = 1 +Rows input = 6 +Rows returned = 3 +Eliminate duplicates = true +In sorted order = false +Sort information: + Number of rows input=6 + Number of rows output=3 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 +Source result set: + Project-Restrict ResultSet (3): + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + restriction = false + projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 + Source result set: + Table Scan ResultSet for NAMES at read committed isolation level using share row locking chosen by the optimizer + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + Fetch Size = 1 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: +null stop position: +null qualifiers: +None +ij> call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(0); +0 rows inserted/updated/deleted +ij> drop table names; +0 rows inserted/updated/deleted +ij> -- reset autocommit autocommit on; ij> Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_13/distinct.out =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_13/distinct.out (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_13/distinct.out (working copy) @@ -2477,6 +2477,313 @@ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- name1 |sum2 ij> rollback; -ij> -- reset autocomiit +ij> create table td (x int); +0 rows inserted/updated/deleted +ij> insert into td values (1); +1 row inserted/updated/deleted +ij> insert into td values (1); +1 row inserted/updated/deleted +ij> insert into td values (2); +1 row inserted/updated/deleted +ij> -- distinct in subquery where the store does not perform the sort. +select * from td, (select distinct 1 from td) as sub(x); +X |X +----------------------- +1 |1 +1 |1 +2 |1 +ij> -- get the storage system to do the sort. +select * from td, (select distinct x from td) as sub(x); +X |X +----------------------- +1 |1 +1 |2 +1 |1 +1 |2 +2 |1 +2 |2 +ij> -- Tests for DERBY-504 (select distinct from a subquery) +create table names (id int, name varchar(10), age int); +0 rows inserted/updated/deleted +ij> insert into names (id, name, age) values + (1, 'Anna', 23), (2, 'Ben', 24), (3, 'Carl', 25), + (4, 'Anna', 23), (5, 'Ben', 24), (6, 'Carl', 25); +6 rows inserted/updated/deleted +ij> call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1); +0 rows inserted/updated/deleted +ij> maximumdisplaywidth 20000; +ij> -- distinct names should be returned +select distinct name from (select name, id from names) as n; +NAME +---------- +Anna +Ben +Carl +ij> -- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + -- distinct names should be returned +select distinct name from (select name, id from names) as n +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Sort ResultSet: +Number of opens = 1 +Rows input = 6 +Rows returned = 3 +Eliminate duplicates = true +In sorted order = false +Sort information: + Number of rows input=6 + Number of rows output=3 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 +Source result set: + Project-Restrict ResultSet (3): + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + restriction = false + projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 + Source result set: + Table Scan ResultSet for NAMES at read committed isolation level using share row locking chosen by the optimizer + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + Fetch Size = 1 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: +null stop position: +null qualifiers: +None +ij> -- distinct names should be returned +select distinct name from (select name from names) as n; +NAME +---------- +Ben +Anna +Carl +ij> -- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + -- distinct names should be returned +select distinct name from (select name from names) as n +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Distinct Scan ResultSet for NAMES using index xxxxFILTERED-UUIDxxxx at read committed isolation level using instantaneous share row locking: +Number of opens = 1 +Hash table size = 3 +Distinct column is column number 1 +Rows seen = 3 +Rows filtered = 0 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 +scan information: + Bit set of columns fetched={1} + Number of columns fetched=1 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: + None + stop position: + None + scan qualifiers: +None + next qualifiers: +None +ij> select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m; +A |B |B |A +--------------------------------------------- +Ben |2 |2 |Ben +Ben |5 |5 |Ben +Anna |1 |1 |Anna +Anna |4 |4 |Anna +Carl |3 |3 |Carl +Carl |6 |6 |Carl +ij> -- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Project-Restrict ResultSet (2): +Number of opens = 1 +Rows seen = 6 +Rows filtered = 0 +restriction = false +projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 +Source result set: + Distinct Scan ResultSet for NAMES using index xxxxFILTERED-UUIDxxxx at read committed isolation level using instantaneous share row locking: + Number of opens = 1 + Hash table size = 6 + Distinct columns are column numbers (0,1) + Rows seen = 6 + Rows filtered = 0 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: + None + stop position: + None + scan qualifiers: +None + next qualifiers: +None +ij> select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m; +A |A +--------------------- +Anna |Anna +Ben |Ben +Carl |Carl +ij> -- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Sort ResultSet: +Number of opens = 1 +Rows input = 6 +Rows returned = 3 +Eliminate duplicates = true +In sorted order = false +Sort information: + Number of rows input=6 + Number of rows output=3 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 +Source result set: + Project-Restrict ResultSet (3): + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + restriction = false + projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 + Source result set: + Table Scan ResultSet for NAMES at read committed isolation level using share row locking chosen by the optimizer + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + Fetch Size = 1 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: +null stop position: +null qualifiers: +None +ij> call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(0); +0 rows inserted/updated/deleted +ij> drop table names; +0 rows inserted/updated/deleted +ij> -- reset autocommit autocommit on; ij> Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_13/groupBy.out =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_13/groupBy.out (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_13/groupBy.out (working copy) @@ -293,7 +293,6 @@ ------------------------------------------------------------------------ 0 |goodbye everyone is here 0 |hello everyone is here -0 |hello everyone is here 100 |hello everyone is here 0 |hello noone is here NULL |NULL Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_22/distinct.out =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_22/distinct.out (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_22/distinct.out (working copy) @@ -2477,6 +2477,313 @@ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- name1 |sum2 ij> rollback; -ij> -- reset autocomiit +ij> create table td (x int); +0 rows inserted/updated/deleted +ij> insert into td values (1); +1 row inserted/updated/deleted +ij> insert into td values (1); +1 row inserted/updated/deleted +ij> insert into td values (2); +1 row inserted/updated/deleted +ij> -- distinct in subquery where the store does not perform the sort. +select * from td, (select distinct 1 from td) as sub(x); +X |X +----------------------- +1 |1 +1 |1 +2 |1 +ij> -- get the storage system to do the sort. +select * from td, (select distinct x from td) as sub(x); +X |X +----------------------- +1 |2 +1 |1 +1 |2 +1 |1 +2 |2 +2 |1 +ij> -- Tests for DERBY-504 (select distinct from a subquery) +create table names (id int, name varchar(10), age int); +0 rows inserted/updated/deleted +ij> insert into names (id, name, age) values + (1, 'Anna', 23), (2, 'Ben', 24), (3, 'Carl', 25), + (4, 'Anna', 23), (5, 'Ben', 24), (6, 'Carl', 25); +6 rows inserted/updated/deleted +ij> call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(1); +0 rows inserted/updated/deleted +ij> maximumdisplaywidth 20000; +ij> -- distinct names should be returned +select distinct name from (select name, id from names) as n; +NAME +---------- +Anna +Ben +Carl +ij> -- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + -- distinct names should be returned +select distinct name from (select name, id from names) as n +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Sort ResultSet: +Number of opens = 1 +Rows input = 6 +Rows returned = 3 +Eliminate duplicates = true +In sorted order = false +Sort information: + Number of rows input=6 + Number of rows output=3 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 +Source result set: + Project-Restrict ResultSet (3): + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + restriction = false + projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 + Source result set: + Table Scan ResultSet for NAMES at read committed isolation level using share row locking chosen by the optimizer + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + Fetch Size = 1 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: +null stop position: +null qualifiers: +None +ij> -- distinct names should be returned +select distinct name from (select name from names) as n; +NAME +---------- +Anna +Ben +Carl +ij> -- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + -- distinct names should be returned +select distinct name from (select name from names) as n +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Distinct Scan ResultSet for NAMES using index xxxxFILTERED-UUIDxxxx at read committed isolation level using instantaneous share row locking: +Number of opens = 1 +Hash table size = 3 +Distinct column is column number 1 +Rows seen = 3 +Rows filtered = 0 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 +scan information: + Bit set of columns fetched={1} + Number of columns fetched=1 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: + None + stop position: + None + scan qualifiers: +None + next qualifiers: +None +ij> select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m; +A |B |B |A +--------------------------------------------- +Anna |1 |1 |Anna +Carl |6 |6 |Carl +Ben |5 |5 |Ben +Carl |3 |3 |Carl +Ben |2 |2 |Ben +Anna |4 |4 |Anna +ij> -- runtime statistics should have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + select distinct a, b, b, a from (select y as a, x as b from (select id as x, name as y from names) as n) as m +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Project-Restrict ResultSet (2): +Number of opens = 1 +Rows seen = 6 +Rows filtered = 0 +restriction = false +projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 +Source result set: + Distinct Scan ResultSet for NAMES using index xxxxFILTERED-UUIDxxxx at read committed isolation level using instantaneous share row locking: + Number of opens = 1 + Hash table size = 6 + Distinct columns are column numbers (0,1) + Rows seen = 6 + Rows filtered = 0 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: + None + stop position: + None + scan qualifiers: +None + next qualifiers: +None +ij> select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m; +A |A +--------------------- +Anna |Anna +Ben |Ben +Carl |Carl +ij> -- runtime statistics should not have Distinct Scan in it +values SYSCS_UTIL.SYSCS_GET_RUNTIMESTATISTICS(); +1 +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Statement Name: + null +Statement Text: + select distinct a, a from (select y as a from (select id as x, name as y from names) as n) as m +Parse Time: 0 +Bind Time: 0 +Optimize Time: 0 +Generate Time: 0 +Compile Time: 0 +Execute Time: 0 +Begin Compilation Timestamp : null +End Compilation Timestamp : null +Begin Execution Timestamp : null +End Execution Timestamp : null +Statement Execution Plan Text: +Sort ResultSet: +Number of opens = 1 +Rows input = 6 +Rows returned = 3 +Eliminate duplicates = true +In sorted order = false +Sort information: + Number of rows input=6 + Number of rows output=3 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 +Source result set: + Project-Restrict ResultSet (3): + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + restriction = false + projection = true + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + restriction time (milliseconds) = 0 + projection time (milliseconds) = 0 + Source result set: + Table Scan ResultSet for NAMES at read committed isolation level using share row locking chosen by the optimizer + Number of opens = 1 + Rows seen = 6 + Rows filtered = 0 + Fetch Size = 1 + constructor time (milliseconds) = 0 + open time (milliseconds) = 0 + next time (milliseconds) = 0 + close time (milliseconds) = 0 + next time in milliseconds/row = 0 + scan information: + Bit set of columns fetched={0, 1} + Number of columns fetched=2 + Number of pages visited=1 + Number of rows qualified=6 + Number of rows visited=6 + Scan type=heap + start position: +null stop position: +null qualifiers: +None +ij> call SYSCS_UTIL.SYSCS_SET_RUNTIMESTATISTICS(0); +0 rows inserted/updated/deleted +ij> drop table names; +0 rows inserted/updated/deleted +ij> -- reset autocommit autocommit on; ij> Index: C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_22/groupBy.out =================================================================== --- C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_22/groupBy.out (revision 264896) +++ C:/derbyt/svn/trunk/java/testing/org/apache/derbyTesting/functionTests/master/j9_22/groupBy.out (working copy) @@ -293,7 +293,6 @@ ------------------------------------------------------------------------ 0 |goodbye everyone is here 0 |hello everyone is here -0 |hello everyone is here 100 |hello everyone is here 0 |hello noone is here NULL |NULL