Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB-12363: fix drop column and constraints(3.1) #5702

Open
wants to merge 1 commit into
base: branch-3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ private void dropColumnFromTable(Activation activation,
int maxStoragePosition = tableDescriptor.getColumnDescriptorList().maxStoragePosition();
int size = tableDescriptor.getColumnDescriptorList().size();
int droppedColumnPosition = columnDescriptor.getPosition();

int droppedStoragePosition = columnDescriptor.getStoragePosition();
FormatableBitSet toDrop = new FormatableBitSet(maxStoragePosition + 1);
toDrop.set(droppedColumnPosition);
tableDescriptor.setReferencedColumnMap(toDrop);
Expand All @@ -781,7 +781,7 @@ private void dropColumnFromTable(Activation activation,

// Now handle constraints
List<ConstantAction> newCongloms = handleConstraints(activation, tableDescriptor, columnName, lcc, dd, dm, cascade, tc,
droppedColumnPosition);
droppedStoragePosition);

/* If there are new backing conglomerates which must be
* created to replace a dropped shared conglomerate
Expand Down Expand Up @@ -940,7 +940,7 @@ private List<ConstantAction> handleConstraints(Activation activation,
DependencyManager dm,
boolean cascade,
TransactionController tc,
int droppedColumnPosition) throws StandardException {
int droppedStoragePosition) throws StandardException {
ConstraintDescriptorList csdl = dd.getConstraintDescriptors(td);
int csdl_size = csdl.size();

Expand All @@ -959,16 +959,16 @@ private List<ConstantAction> handleConstraints(Activation activation,
int numRefCols = referencedColumns.length, j;
boolean changed = false;
for (j = 0; j < numRefCols; j++) {
if (referencedColumns[j] > droppedColumnPosition)
if (referencedColumns[j] > droppedStoragePosition)
changed = true;
if (referencedColumns[j] == droppedColumnPosition)
if (referencedColumns[j] == droppedStoragePosition)
break;
}
if (j == numRefCols) {// column not referenced
if ((cd instanceof CheckConstraintDescriptor) && changed) {
dd.dropConstraintDescriptor(cd, tc);
for (j = 0; j < numRefCols; j++) {
if (referencedColumns[j] > droppedColumnPosition)
if (referencedColumns[j] > droppedStoragePosition)
referencedColumns[j]--;
}
((CheckConstraintDescriptor) cd).setReferencedColumnsDescriptor(new ReferencedColumnsDescriptorImpl(referencedColumns));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,30 @@ public void testCascadeDropTrigger() throws Exception {
assertEquals(0, count);
}
}

@Test
public void testCascadeDropConstraint() throws Exception {
methodWatcher.execute("create table t6(c1 int)");
methodWatcher.execute("alter table t6 add c2 float");
methodWatcher.execute("alter table t6 drop c2");
methodWatcher.execute("alter table t6 add c3 float not null default 1 constraint con3 primary key");

try(Connection conn = methodWatcher.getOrCreateConnection();
Statement s = conn.createStatement()) {
s.execute("alter table t6 drop c3");
SQLWarning warning = s.getWarnings();
Assert.assertTrue(warning!=null);
Assert.assertEquals("01500", warning.getSQLState());
}

String sql = String.format("select count(*) from sys.sysprimarykeys where conglomerateid=(" +
"select conglomerateid from sys.sysconglomerates c, sys.sysschemas s, sys.systables t " +
"where s.schemaid=t.schemaid and t.tableid=c.tableid and s.schemaname='%s' and t.tablename='T6')",
SCHEMA_NAME);
try(ResultSet rs = methodWatcher.executeQuery(sql)) {
rs.next();
int count = rs.getInt(1);
Assert.assertEquals(0, count);
}
}
}