} break;
case NodeTypes.DROP_COLUMN_NODE: {
String columnName = ((ModifyColumnNode)node).getColumnName();
if(Column.isInternalName(columnName) || (origTable.getColumn(columnName) == null)) {
skipOrThrow(context, ((ModifyColumnNode)node).getExistenceCheck(), null, new NoSuchColumnException(columnName));
} else {
columnChanges.add(TableChange.createDrop(columnName));
}
} break;
case NodeTypes.MODIFY_COLUMN_DEFAULT_NODE:
case NodeTypes.MODIFY_COLUMN_CONSTRAINT_NODE:
case NodeTypes.MODIFY_COLUMN_CONSTRAINT_NOT_NULL_NODE:
case NodeTypes.MODIFY_COLUMN_TYPE_NODE: {
ModifyColumnNode modNode = (ModifyColumnNode)node;
String columnName = modNode.getColumnName();
Column column = origTable.getColumn(columnName);
if(Column.isInternalName(columnName) || (column == null)) {
skipOrThrow(context, modNode.getExistenceCheck(), null, new NoSuchColumnException(columnName));
} else {
// Special case: The only requested change is RESTART WITH.
// Cannot go through the current ALTER flow as the new value may appear to be the same,
// triggering NONE change, but should still take affect as values may have been allocated.
if((elements.size() == 1) && isRestartWithNode(modNode)) {
Sequence curSeq = column.getIdentityGenerator();
if(curSeq == null) {
throw new ColumnNotGeneratedException(column);
}
AkibanInformationSchema aisCopy = new AkibanInformationSchema();
Sequence newSeq = Sequence.create(aisCopy,
curSeq.getSchemaName(),
curSeq.getSequenceName().getTableName(),
modNode.getAutoincrementStart(),
curSeq.getIncrement(),
curSeq.getMinValue(),
curSeq.getMaxValue(),
curSeq.isCycle());
ddl.alterSequence(session, curSeq.getSequenceName(), newSeq);
return ChangeLevel.METADATA;
} else {
columnChanges.add(TableChange.createModify(columnName, columnName));
columnDefNodes.add((ColumnDefinitionNode)node);
}
}
} break;
case NodeTypes.FK_CONSTRAINT_DEFINITION_NODE: {
FKConstraintDefinitionNode fkNode = (FKConstraintDefinitionNode) node;
if(fkNode.getConstraintType() == ConstraintType.DROP) {
if(fkNode.isGrouping()) {
if(origTable.getParentJoin() == null) {
skipOrThrow(context, fkNode.getExistenceCheck(), null, new NoSuchGroupingFKException(origTable.getName()));
fkNode = null;
}
} else {
if(fkNode.getConstraintName() == null) {
Collection<ForeignKey> fkeys = origTable.getReferencingForeignKeys();
if(fkeys.size() == 0) {
skipOrThrow(context, fkNode.getExistenceCheck(), null, new UnsupportedFKIndexException());
fkNode = null;
} else if(fkeys.size() != 1) {
throw new UnsupportedFKIndexException();
} else {
try {
fkNode.setConstraintName(fkeys.iterator().next().getConstraintName().getTableName());
} catch(StandardException ex) {
throw new SQLParserInternalException(ex);
}
}
} else if(origTable.getReferencingForeignKey(fkNode.getConstraintName().getTableName()) == null) {
skipOrThrow(context,
fkNode.getExistenceCheck(),
null,
new NoSuchForeignKeyException(fkNode.getConstraintName().getTableName(), origTable.getName()));
fkNode = null;
}
if(fkNode != null) {
// Also drop the referencing index.
indexChanges.add(TableChange.createDrop(fkNode.getConstraintName().getTableName()));
}
}
}
if(fkNode != null) {
fkDefNodes.add(fkNode);
}
} break;
case NodeTypes.CONSTRAINT_DEFINITION_NODE: {
ConstraintDefinitionNode cdn = (ConstraintDefinitionNode) node;
if(cdn.getConstraintType() == ConstraintType.DROP) {
String name = cdn.getName();
switch(cdn.getVerifyType()) {
case PRIMARY_KEY:
if(origTable.getPrimaryKey() == null) {
skipOrThrow(context, cdn.getExistenceCheck(),
null,
new NoSuchConstraintException(origTable.getName(), Index.PRIMARY));
name = null;
} else {
name = origTable.getPrimaryKey().getIndex().getIndexName().getName();
}
break;
case DROP:
boolean found = false;
String indexName = indexNameForConstrainName(origTable, name);
if (indexName != null) {
found = true;
name = indexName;
} else if (origTable.getReferencingForeignKey(name) != null) {
fkDefNodes.add(newFKDropNode(node, name, Boolean.FALSE));
found = true;
} else if (origTable.getParentJoin() != null && origTable.getParentJoin().getName().equals(name)) {
fkDefNodes.add(newFKDropNode(node, name, Boolean.TRUE));
found = true;
name = null;
}
if(!found) {
skipOrThrow(context,
cdn.getExistenceCheck(),
null,
new NoSuchConstraintException(origTable.getName(), name));
name = null;
}
break;
case UNIQUE:
Index index = origTable.getIndex(name);
if(index == null || !index.isUnique()) {
skipOrThrow(context,
cdn.getExistenceCheck(),
null,
new NoSuchUniqueException(origTable.getName(), cdn.getName()));
name = null;
}
break;
case CHECK:
throw new UnsupportedCheckConstraintException();
}
if (name != null) {
indexChanges.add(TableChange.createDrop(name));
}
} else if (cdn.getConstraintType() == ConstraintType.PRIMARY_KEY) {
if (origTable.getPrimaryKeyIncludingInternal().isAkibanPK())
{
columnChanges.add(TableChange.createDrop(Column.ROW_ID_NAME));
String indexName = origTable.getPrimaryKeyIncludingInternal().getIndex().getIndexName().getName();
indexChanges.add(TableChange.createDrop(indexName));
}
conDefNodes.add(cdn);
} else {
conDefNodes.add(cdn);
}
} break;
case NodeTypes.INDEX_DEFINITION_NODE:
IndexDefinitionNode idn = (IndexDefinitionNode)node;
if(idn.getJoinType() != null) {
throw new UnsupportedSQLException("ALTER ADD INDEX containing group index");
}
indexDefNodes.add(idn);
break;
case NodeTypes.AT_DROP_INDEX_NODE: {
AlterDropIndexNode dropIndexNode = (AlterDropIndexNode)node;
String name = dropIndexNode.getIndexName();
if(origTable.getIndex(name) == null) {
skipOrThrow(context, dropIndexNode.getExistenceCheck(), null, new NoSuchIndexException(name));
} else {
indexChanges.add(TableChange.createDrop(name));
}
}
break;
case NodeTypes.AT_RENAME_NODE:
TableName newName = DDLHelper.convertName(defaultSchema,
((AlterTableRenameNode)node).newName());
TableName oldName = origTable.getName();
ddl.renameTable(session, oldName, newName);
return ChangeLevel.METADATA;
case NodeTypes.AT_RENAME_COLUMN_NODE:
AlterTableRenameColumnNode alterRenameCol = (AlterTableRenameColumnNode) node;
String oldColName = alterRenameCol.getName();
String newColName = alterRenameCol.newName();
final Column oldCol = origTable.getColumn(oldColName);
if (oldCol == null) {
throw new NoSuchColumnException(oldColName);
}
columnChanges.add(TableChange.createModify(oldColName, newColName));
break;
default: