You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by da...@apache.org on 2014/06/25 18:24:40 UTC

svn commit: r1605478 - in /db/derby/code/trunk/java: engine/org/apache/derby/catalog/types/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/

Author: dag
Date: Wed Jun 25 16:24:40 2014
New Revision: 1605478

URL: http://svn.apache.org/r1605478
Log:
DERBY-6623 DatabaseMetaDataTest fails intermittently

Patch *derby-6623-2a*, which adds an extra ordering [1] using
IndexDescriptor's *getBaseColumns* method as input to
*Arrays.toString()*. This imposes a total order on the rows returned
from the metadata query, so hopefully we get rid of the
Heisenbug. Note that this caused another unique constraint to be
chosen in the example that failed.

[1] *after* the number of columns in the index: small numbers
    before higher numbers since constraints with a small number of
    columns are considered better

The ordering makes sorting stable iff:

    - the row layout doesn't change
    - no indexes or constraint are added or removed.

Mostly, indexes with early base columns will be sorted first (and
selected as best), although not always: "[11,1]" sorts before "[2,1]",
but the main thing is the same index will be selected for the same
(unchanged) database always.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/catalog/types/IndexDescriptorImpl.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/metadata.properties
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/catalog/types/IndexDescriptorImpl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/catalog/types/IndexDescriptorImpl.java?rev=1605478&r1=1605477&r2=1605478&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/catalog/types/IndexDescriptorImpl.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/catalog/types/IndexDescriptorImpl.java Wed Jun 25 16:24:40 2014
@@ -259,7 +259,7 @@ public class IndexDescriptorImpl impleme
 
 	public String toString()
 	{
-		StringBuffer	sb = new StringBuffer(60);
+        StringBuilder   sb = new StringBuilder(60);
 
         if (isUnique || isUniqueDeferrable)
 			sb.append("UNIQUE ");

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java?rev=1605478&r1=1605477&r2=1605478&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedDatabaseMetaData.java Wed Jun 25 16:24:40 2014
@@ -2185,7 +2185,11 @@ public class EmbedDatabaseMetaData exten
 				return ps.executeQuery();
 			}
 	
-			// get the unique constraint with the fewest columns.
+            // Get the unique constraint with the fewest columns.
+            // If there is a tie, i.e. there is more than one unique constraint
+            // with the same number of columns involved, we chose the one
+            // that's ordered first, cf. the query text in metadata.properties
+            // (DERBY-6623)..
 			ps = getPreparedQuery("getBestRowIdentifierUniqueConstraint");
 			ps.setString(1,catalogPattern);
 			ps.setString(2,schemaPattern);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/metadata.properties
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/metadata.properties?rev=1605478&r1=1605477&r2=1605478&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/metadata.properties (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/metadata.properties Wed Jun 25 16:24:40 2014
@@ -975,7 +975,8 @@ getBestRowIdentifierPrimaryKeyColumns=\
 # parameter3 - table
 #
 getBestRowIdentifierUniqueConstraint=\
-	SELECT CONS.CONSTRAINTID, IDX.DESCRIPTOR.numberOfOrderedColumns() AS NUMCOLS \
+    SELECT CONS.CONSTRAINTID, IDX.DESCRIPTOR.numberOfOrderedColumns() AS NUMCOLS, \
+               java.util.Arrays::toString(IDX.DESCRIPTOR.baseColumnPositions()) AS LOOKS \
 	FROM SYS.SYSSCHEMAS SCHEMAS, SYS.SYSTABLES TABS, \
 		SYS.SYSCONSTRAINTS cons, SYS.SYSKEYS keys, SYS.SYSCONGLOMERATES IDX \
 	WHERE TABS.TABLEID = conS.TABLEID AND SCHEMAS.SCHEMAID = TABS.SCHEMAID \
@@ -985,7 +986,11 @@ getBestRowIdentifierUniqueConstraint=\
 		AND ((1=1) OR ? IS NOT NULL) \
 		AND (SCHEMAS.SCHEMANAME LIKE ?) \
 		AND (TABS.TABLENAME=?) \
-	ORDER BY NUMCOLS
+    ORDER BY NUMCOLS, LOOKS /* LOOKS: Sort based on string of base colums       */ \
+                                /* of the index, so the query is now fully ordered. */ \
+                                /* DERBY-6623. Stable as long as row is unchanged   */ \
+                                /* and no indexes are added or deleted. Not always  */ \
+                                /* intuitive though [1,11]: sorts before [1,2].     */
 
 # getBestRowIdentifierUniqueKeyColumns
 #

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java?rev=1605478&r1=1605477&r2=1605478&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DatabaseMetaDataTest.java Wed Jun 25 16:24:40 2014
@@ -3479,11 +3479,11 @@ public class DatabaseMetaDataTest extend
         		DatabaseMetaData.bestRowTemporary, true);
         verifyBRIResults(rs, expRSI);
 
-        // result: column j
+        // result: column i (j is equally good but its index descriptor
+        // sorts after i: DERBY-6623)
         rs = getBestRowIdentifier(null,schema,"BRIT3",
         		DatabaseMetaData.bestRowTemporary,true);
-        verifyBRIResults(rs, expRSJ);
-        
+        verifyBRIResults(rs, expRSI);
         // result: column i
         rs = getBestRowIdentifier(null,schema,"BRIT4",
         		DatabaseMetaData.bestRowTemporary,true);