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 2013/12/11 15:34:32 UTC

svn commit: r1550152 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/execute/ engine/org/apache/derby/impl/store/access/btree/ testing/org/apache/derbyTesting/functionTests/tests/lang/

Author: dag
Date: Wed Dec 11 14:34:31 2013
New Revision: 1550152

URL: http://svn.apache.org/r1550152
Log:
DERBY-532 Support deferrable constraints

Patch derby-532-nullableUniqueFix. When we changed the implementation from
special treatment of deferrable constraints in the BTree, a couple of extra
predicates needed to be added were omitted - added those here: we should not
mark the physical index with "uniqueWithDuplicateNulls" if it is deferrable.
This error was found when running the regressions with default deferrable for
all pk and unique constraints.

We also removed an unused flag "hasDeferrableChecking" in the same places (it is
not longer used by the physical index).

Added a new test case, testCompressTable, which tests the
"uniqueWithDuplicateNulls" case.

We also change the behavior in the following way for deferrable, but not
deferred constraints: if we hit a time-out or dead-lock when checking uniqueness
(in the BTree scan), we throw that time-out or dead-lock. Up till now we
converted it to a duplicate exception. We will only assume it can be a duplicate
- for later checking - iff the constraint mode is deferrable.


Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/IndexChanger.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/InsertResultSet.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/btree/BTree.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java?rev=1550152&r1=1550151&r2=1550152&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java Wed Dec 11 14:34:31 2013
@@ -2576,18 +2576,13 @@ class AlterTableConstantAction extends D
 			properties.put(
                 "nUniqueColumns", Integer.toString(indexRowLength));
 		}
-		if(cd.getIndexDescriptor().isUniqueWithDuplicateNulls())
+        if (  cd.getIndexDescriptor().isUniqueWithDuplicateNulls() &&
+             !cd.getIndexDescriptor().hasDeferrableChecking() )
 		{
 			properties.put(
                 "uniqueWithDuplicateNulls", Boolean.toString(true));
 		}
 
-        if (cd.getIndexDescriptor().hasDeferrableChecking())
-        {
-            properties.put(
-                "hasDeferrableChecking", Boolean.toString(true));
-        }
-
 		properties.put(
             "rowLocationColumn", Integer.toString(indexRowLength - 1));
 		properties.put(

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/IndexChanger.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/IndexChanger.java?rev=1550152&r1=1550151&r2=1550152&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/IndexChanger.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/IndexChanger.java Wed Dec 11 14:34:31 2013
@@ -490,8 +490,9 @@ class IndexChanger
                 duplicate = foundOne && idxScan.next();
 
             } catch (StandardException e) {
-                if (e.getSQLState().equals(SQLState.LOCK_TIMEOUT) ||
-                    e.getSQLState().equals(SQLState.DEADLOCK)) {
+                if ((e.getSQLState().equals(SQLState.LOCK_TIMEOUT) ||
+                     e.getSQLState().equals(SQLState.DEADLOCK)) &&
+                     lcc.isEffectivelyDeferred(activation, indexCID))  {
                     // Assume there is a duplicate, so we'll check again at
                     // commit time.
                     duplicate = true;

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/InsertResultSet.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/InsertResultSet.java?rev=1550152&r1=1550151&r2=1550152&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/InsertResultSet.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/InsertResultSet.java Wed Dec 11 14:34:31 2013
@@ -2003,17 +2003,14 @@ class InsertResultSet extends DMLWriteRe
 				properties.put("nUniqueColumns", 
 							   Integer.toString(indexRowLength));
 			}
-			if(cd.getIndexDescriptor().isUniqueWithDuplicateNulls())
+
+            if ( cd.getIndexDescriptor().isUniqueWithDuplicateNulls() &&
+                !cd.getIndexDescriptor().hasDeferrableChecking() )
 			{
 				properties.put(
                     "uniqueWithDuplicateNulls", Boolean.toString(true));
 			}
 
-            if (cd.getIndexDescriptor().hasDeferrableChecking()) {
-                properties.put(
-                    "hasDeferrableChecking", Boolean.toString(true));
-            }
-
 			properties.put("rowLocationColumn", 
 							Integer.toString(indexRowLength - 1));
 			properties.put("nKeyFields", Integer.toString(indexRowLength));
@@ -2508,17 +2505,13 @@ class InsertResultSet extends DMLWriteRe
 				properties.put("nUniqueColumns", 
 							   Integer.toString(indexRowLength));
 			}
-			if(cd.getIndexDescriptor().isUniqueWithDuplicateNulls())
+            if( cd.getIndexDescriptor().isUniqueWithDuplicateNulls() &&
+               !cd.getIndexDescriptor().hasDeferrableChecking() )
 			{
 				properties.put(
                     "uniqueWithDuplicateNulls", Boolean.toString(true));
 			}
 
-            if (cd.getIndexDescriptor().hasDeferrableChecking()) {
-                properties.put(
-                    "hasDeferrableChecking", Boolean.toString(true));
-            }
-
             properties.put("rowLocationColumn",
 							Integer.toString(indexRowLength - 1));
 			properties.put("nKeyFields", Integer.toString(indexRowLength));

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/btree/BTree.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/btree/BTree.java?rev=1550152&r1=1550151&r2=1550152&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/btree/BTree.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/btree/BTree.java Wed Dec 11 14:34:31 2013
@@ -109,8 +109,10 @@ public abstract class BTree extends Gene
     public static final String PROPERTY_NUNIQUECOLUMNS  = "nUniqueColumns";
     public static final String PROPERTY_PARENTLINKS     = "maintainParentLinks";
 
-    //property key to indicate if the index will allow duplicate nulls, but
-    //otherwise insure unique keys
+    // Property key to indicate if the index will allow duplicate nulls, but
+    // otherwise ensure unique keys. Note that this is not used in the BTree for
+    // deferrable constraints: in this case it handled at the language level,
+    // cf. IndexChanger.
     public static final String PROPERTY_UNIQUE_WITH_DUPLICATE_NULLS 
                                                     = "uniqueWithDuplicateNulls";
 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java?rev=1550152&r1=1550151&r2=1550152&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java Wed Dec 11 14:34:31 2013
@@ -94,32 +94,34 @@ public class ConstraintCharacteristicsTe
         systemProperties.setProperty("derby.locks.waitTimeout", "2");
         TestSuite s = new TestSuite("WithLenientChecking");
 
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testLocking"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testDatabaseMetaData"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testCreateConstraintDictionaryEncodings"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testAlterConstraintDictionaryEncodings"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testAlterConstraintInvalidation"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testBasicDeferral"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testRoutines"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testDeferredRowsInvalidation"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testImport"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testDerby6374"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testXA"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testManySimilarDuplicates"));
-       s.addTest(new ConstraintCharacteristicsTest(
-           "testAlmostRemovedAllDups"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testCompressTable"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testLocking"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testDatabaseMetaData"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testCreateConstraintDictionaryEncodings"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testAlterConstraintDictionaryEncodings"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testAlterConstraintInvalidation"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testBasicDeferral"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testRoutines"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testDeferredRowsInvalidation"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testImport"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testDerby6374"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testXA"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testManySimilarDuplicates"));
+        s.addTest(new ConstraintCharacteristicsTest(
+                      "testAlmostRemovedAllDups"));
 
         suite.addTest(new SystemPropertyTestSetup(s, systemProperties));
 
@@ -1005,6 +1007,46 @@ public class ConstraintCharacteristicsTe
         }
     }
 
+    // Exposed a bug when running regression suites with default
+    // deferrable: compress recreates the index.
+    public void testCompressTable() throws SQLException {
+        Connection con = getConnection();
+        Statement stmt = con.createStatement();
+        stmt.executeUpdate(
+                "create table table1(" +
+                "name1 int unique deferrable initially immediate, " +
+                "name2 int unique not null, " +
+                "name3 int primary key)");
+        try {
+            stmt.execute(
+                "call syscs_util.syscs_compress_table('APP','TABLE1',1)");
+            stmt.executeUpdate(
+                "insert into table1 values(1,11,111)");
+
+            // The following should run into problem because of constraint
+            // on name1
+            assertStatementError(
+                "23505", stmt,
+                "insert into table1 values(1,22,222)");
+
+            // The following should run into problem because of constraint
+            // on name2
+            assertStatementError(
+                "23505", stmt,
+                "insert into table1 values(3,11,333)");
+
+            // The following should run into problem because of constraint
+            // on name3
+            assertStatementError(
+                "23505", stmt,
+                "insert into table1 values(4,44,111)");
+
+        } finally {
+            stmt.executeUpdate("drop table table1");
+        }
+    }
+
+
     final static long SIZE = (1024L * 1024L * 10) / 256;
     public void testManySimilarDuplicates() throws SQLException {
         Statement s = createStatement();