org.apache.derby.impl.sql.execute
Class AlterTableConstantAction

java.lang.Object
  extended byorg.apache.derby.impl.sql.execute.GenericConstantAction
      extended byorg.apache.derby.impl.sql.execute.DDLConstantAction
          extended byorg.apache.derby.impl.sql.execute.DDLSingleTableConstantAction
              extended byorg.apache.derby.impl.sql.execute.AlterTableConstantAction
All Implemented Interfaces:
ConstantAction, RowLocationRetRowSource, RowSource

class AlterTableConstantAction
extends DDLSingleTableConstantAction
implements RowLocationRetRowSource

This class describes actions that are ALWAYS performed for an ALTER TABLE Statement at Execution time.

Author:
Jerry Brenner.

Field Summary
private  Activation activation
           
private  ExecRow[] baseRow
           
private  DataValueDescriptor[][] baseRowArray
           
private  int behavior
           
private  int bulkFetchSize
           
protected  ColumnInfo[] columnInfo
           
private  int columnPosition
           
private  ConglomerateController compressHeapCC
           
private  GroupFetchScanController compressHeapGSC
           
private  IndexRowGenerator[] compressIRGs
           
private  RowLocation[] compressRL
           
private  boolean compressTable
           
protected  ConstraintConstantAction[] constraintActions
           
private  int currentCompressRow
           
private  ExecRow currentRow
           
private  DataDictionary dd
           
private  DependencyManager dm
           
private  boolean doneScan
           
private  long estimatedRowCount
           
private  long[] indexConglomerateNumbers
           
private  FormatableBitSet indexedCols
           
private  ExecIndexRow[] indexRows
           
private  LanguageConnectionContext lcc
           
protected  char lockGranularity
           
private  boolean[] needToDropSort
           
private  int numIndexes
           
private  ColumnOrdering[][] ordering
           
private  int rowCount
           
protected  UUID schemaId
           
protected  SchemaDescriptor sd
           
private  boolean sequential
           
private  SortController[] sorters
           
private  long[] sortIds
           
protected  long tableConglomerateId
           
protected  UUID tableId
           
protected  java.lang.String tableName
           
protected  int tableType
           
private  TransactionController tc
           
private  TableDescriptor td
           
private  boolean truncateTable
           
private  boolean[] validRow
           
 
Fields inherited from interface org.apache.derby.iapi.sql.execute.ConstantAction
RUNTIMESTATISTICS, STATISTICSTIMING
 
Constructor Summary
(package private) AlterTableConstantAction(SchemaDescriptor sd, java.lang.String tableName, UUID tableId, long tableConglomerateId, int tableType, ColumnInfo[] columnInfo, ConstraintConstantAction[] constraintActions, char lockGranularity, boolean compressTable, int behavior, boolean sequential, boolean truncateTable)
          Make the AlterAction for an ALTER TABLE statement.
 
Method Summary
private  void addNewColumnToTable(Activation activation, int ix)
          Workhorse for adding a new column to a table.
 void cleanUp()
           
private  void closeBulkFetchScan()
           
 void closeRowSource()
          closeRowSource tells the RowSource that it will no longer need to return any rows and it can release any resource it may have.
private  java.lang.Object[] compressIndexArrays(long[] indexCIDS, IndexRowGenerator[] irgs)
          Get rid of duplicates from a set of index conglomerate numbers and index descriptors.
private  void compressTable(Activation activation)
           
private  void dropAllColumnDefaults(UUID tableId, DataDictionary dd)
           
private  void dropColumnFromTable(Activation activation, int ix)
          Workhorse for dropping a column from a table.
 void execGuts(Activation activation)
          Wrapper for this DDL action.
 void executeConstantAction(Activation activation)
          This is the guts of the Execution-time logic for ALTER TABLE.
private static void executeUpdate(LanguageConnectionContext lcc, java.lang.String updateStmt)
           
private  void getAffectedIndexes(Activation activation)
          Get info on the indexes on the table being compress.
private  long getColumnMax(Activation activation, TableDescriptor td, java.lang.String columnName, long increment, long initial)
          computes the minimum/maximum value in a column of a table.
 DataValueDescriptor[] getNextRowFromRowSource()
          Get the next row as an array of column objects.
private  int getSemiRowCount(TransactionController tc)
          Return the "semi" row count of a table.
 FormatableBitSet getValidColumns()
          getValidColumns describes the DataValueDescriptor[] returned by all calls to the getNextRowFromRowSource() call.
private  void insertIntoSorter(int index, RowLocation rl)
           
 boolean modifiesTableId(UUID tableId)
          Does this constant action modify the passed in table uuid?
private  void modifyColumnConstraint(Activation activation, java.lang.String colName, boolean nullability)
          Workhorse for modifying column level constraints.
private  void modifyColumnDefault(Activation activation, int ix)
          Workhorse for modifying the default value of a column.
private  void modifyColumnType(Activation activation, int ix)
           
 boolean needsRowLocation()
          needsRowLocation returns true iff this the row source expects the drainer of the row source to call rowLocation after getting a row from getNextRowFromRowSource.
 boolean needsToClone()
          Does the caller of getNextRowFromRowSource() need to clone the row in order to keep a reference to the row past the getNextRowFromRowSource() call which returned the row.
private  void objectifyStreamingColumns()
           
private  void openBulkFetchScan(long heapConglomNumber)
           
 void rowLocation(RowLocation rl)
          rowLocation is a callback for the drainer of the row source to return the rowLocation of the current row, i.e, the row that is being returned by getNextRowFromRowSource.
private  void setUpAllSorts(ExecRow sourceRow, RowLocation rl)
          Set up to update all of the indexes on a table when doing a bulk insert on an empty table.
 java.lang.String toString()
           
private  void truncateTable(Activation activation)
           
private  void updateAllIndexes(long newHeapConglom, DataDictionary dd)
          Update all of the indexes on a table when doing a bulk insert on an empty table.
private  void updateIndex(long newHeapConglom, DataDictionary dd, int index, long[] newIndexCongloms)
           
private  void updateNewAutoincrementColumn(Activation activation, java.lang.String columnName, long initial, long increment)
          Update values in a new autoincrement column being added to a table.
private  void updateNewColumnToDefault(Activation activation, java.lang.String columnName, java.lang.String defaultText, LanguageConnectionContext lcc)
          Update a new column with its default.
private  boolean validateNotNullConstraint(java.lang.String[] columnNames, boolean[] nullCols, int numRows, LanguageConnectionContext lcc, java.lang.String errorMsg)
          Make sure that the columns are non null If any column is nullable, check that the data is null.
 
Methods inherited from class org.apache.derby.impl.sql.execute.DDLConstantAction
constructToString, getAndCheckSchemaDescriptor, getSchemaDescriptorForCreate, lockTableForDDL, readExternal, upToDate, writeExternal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

sd

protected SchemaDescriptor sd

tableName

protected java.lang.String tableName

schemaId

protected UUID schemaId

tableType

protected int tableType

tableConglomerateId

protected long tableConglomerateId

columnInfo

protected ColumnInfo[] columnInfo

constraintActions

protected ConstraintConstantAction[] constraintActions

lockGranularity

protected char lockGranularity

compressTable

private boolean compressTable

sequential

private boolean sequential

behavior

private int behavior

doneScan

private boolean doneScan

needToDropSort

private boolean[] needToDropSort

validRow

private boolean[] validRow

bulkFetchSize

private int bulkFetchSize

currentCompressRow

private int currentCompressRow

numIndexes

private int numIndexes

rowCount

private int rowCount

estimatedRowCount

private long estimatedRowCount

indexConglomerateNumbers

private long[] indexConglomerateNumbers

sortIds

private long[] sortIds

indexedCols

private FormatableBitSet indexedCols

compressHeapCC

private ConglomerateController compressHeapCC

indexRows

private ExecIndexRow[] indexRows

baseRow

private ExecRow[] baseRow

currentRow

private ExecRow currentRow

compressHeapGSC

private GroupFetchScanController compressHeapGSC

compressIRGs

private IndexRowGenerator[] compressIRGs

baseRowArray

private DataValueDescriptor[][] baseRowArray

compressRL

private RowLocation[] compressRL

sorters

private SortController[] sorters

columnPosition

private int columnPosition

ordering

private ColumnOrdering[][] ordering

td

private TableDescriptor td

truncateTable

private boolean truncateTable

lcc

private LanguageConnectionContext lcc

dd

private DataDictionary dd

dm

private DependencyManager dm

tc

private TransactionController tc

activation

private Activation activation

tableId

protected UUID tableId
Constructor Detail

AlterTableConstantAction

AlterTableConstantAction(SchemaDescriptor sd,
                         java.lang.String tableName,
                         UUID tableId,
                         long tableConglomerateId,
                         int tableType,
                         ColumnInfo[] columnInfo,
                         ConstraintConstantAction[] constraintActions,
                         char lockGranularity,
                         boolean compressTable,
                         int behavior,
                         boolean sequential,
                         boolean truncateTable)
Make the AlterAction for an ALTER TABLE statement.

Parameters:
sd - descriptor for the schema that table lives in.
tableName - Name of table.
tableId - UUID of table
tableConglomerateId - heap conglomerate number of table
tableType - Type of table (e.g., BASE).
columnInfo - Information on all the columns in the table.
constraintActions - ConstraintConstantAction[] for constraints
lockGranularity - The lock granularity.
compressTable - Whether or not this is a compress table
behavior - drop behavior for dropping column
sequential - If compress table/drop column, whether or not sequential
truncateTable - Whether or not this is a truncate table
Method Detail

toString

public java.lang.String toString()

executeConstantAction

public void executeConstantAction(Activation activation)
                           throws StandardException
This is the guts of the Execution-time logic for ALTER TABLE.

Specified by:
executeConstantAction in interface ConstantAction
Parameters:
activation - The execution environment for this constant action.
Throws:
StandardException - Thrown on failure
See Also:
ConstantAction.executeConstantAction(org.apache.derby.iapi.sql.Activation)

execGuts

public void execGuts(Activation activation)
              throws StandardException
Wrapper for this DDL action. Factored out so that our child, RepAlterTableConstantAction could enjoy the benefits of the startWriting() method above.

Throws:
StandardException - Thrown on failure

addNewColumnToTable

private void addNewColumnToTable(Activation activation,
                                 int ix)
                          throws StandardException
Workhorse for adding a new column to a table.

Parameters:
ix - the index of the column specfication in the ALTER statement-- currently we allow only one.
Throws:
StandardException - thrown on failure.

dropColumnFromTable

private void dropColumnFromTable(Activation activation,
                                 int ix)
                          throws StandardException
Workhorse for dropping a column from a table.

Parameters:
ix - the index of the column specfication in the ALTER statement-- currently we allow only one.
Throws:
StandardException - thrown on failure.

modifyColumnType

private void modifyColumnType(Activation activation,
                              int ix)
                       throws StandardException
Throws:
StandardException

modifyColumnConstraint

private void modifyColumnConstraint(Activation activation,
                                    java.lang.String colName,
                                    boolean nullability)
                             throws StandardException
Workhorse for modifying column level constraints. Right now it is restricted to modifying a null constraint to a not null constraint.

Throws:
StandardException

modifyColumnDefault

private void modifyColumnDefault(Activation activation,
                                 int ix)
                          throws StandardException
Workhorse for modifying the default value of a column.

Parameters:
activation - activation
ix - the index of the column specfication in the ALTER statement-- currently we allow only one.
Throws:
StandardException, - thrown on error.
StandardException

compressTable

private void compressTable(Activation activation)
                    throws StandardException
Throws:
StandardException

truncateTable

private void truncateTable(Activation activation)
                    throws StandardException
Throws:
StandardException

updateAllIndexes

private void updateAllIndexes(long newHeapConglom,
                              DataDictionary dd)
                       throws StandardException
Update all of the indexes on a table when doing a bulk insert on an empty table.

Throws:
StandardException - thrown on error

updateIndex

private void updateIndex(long newHeapConglom,
                         DataDictionary dd,
                         int index,
                         long[] newIndexCongloms)
                  throws StandardException
Throws:
StandardException

getAffectedIndexes

private void getAffectedIndexes(Activation activation)
                         throws StandardException
Get info on the indexes on the table being compress.

Returns:
Nothing
Throws:
StandardException - Thrown on error

setUpAllSorts

private void setUpAllSorts(ExecRow sourceRow,
                           RowLocation rl)
                    throws StandardException
Set up to update all of the indexes on a table when doing a bulk insert on an empty table.

Throws:
StandardException - thrown on error

getValidColumns

public FormatableBitSet getValidColumns()
Description copied from interface: RowSource
getValidColumns describes the DataValueDescriptor[] returned by all calls to the getNextRowFromRowSource() call. If getValidColumns returns null, the number of columns is given by the DataValueDescriptor.length where DataValueDescriptor[] is returned by the preceeding getNextRowFromRowSource() call. Column N maps to DataValueDescriptor[N], where column numbers start at zero. If getValidColumns return a non null validColumns FormatableBitSet the number of columns is given by the number of bits set in validColumns. Column N is not in the partial row if validColumns.get(N) returns false. Column N is in the partial row if validColumns.get(N) returns true. If column N is in the partial row then it maps to DataValueDescriptor[M] where M is the count of calls to validColumns.get(i) that return true where i < N. If DataValueDescriptor.length is greater than the number of columns indicated by validColumns the extra entries are ignored.

Specified by:
getValidColumns in interface RowSource
See Also:
RowSource.getValidColumns()

getNextRowFromRowSource

public DataValueDescriptor[] getNextRowFromRowSource()
                                              throws StandardException
Description copied from interface: RowSource
Get the next row as an array of column objects. The column objects can be a JBMS Storable or any Serializable/Externalizable/Formattable/Streaming type.
A return of null indicates that the complete set of rows has been read.

A null column can be specified by leaving the object null, or indicated by returning a non-null getValidColumns. On streaming columns, it can be indicated by returning a non-null get FieldStates.

If RowSource.needToClone() is true then the returned row (the DataValueDescriptor[]) is guaranteed not to be modified by drainer of the RowSource (except that the input stream will be read, of course) and drainer will keep no reference to it before making the subsequent nextRow call. So it is safe to return the same DataValueDescriptor[] in subsequent nextRow calls if that is desirable for performance reasons.

If RowSource.needToClone() is false then the returned row (the DataValueDescriptor[]) may be be modified by drainer of the RowSource, and the drainer may keep a reference to it after making the subsequent nextRow call. In this case the client should severe all references to the row after returning it from getNextRowFromRowSource().

Specified by:
getNextRowFromRowSource in interface RowSource
Throws:
StandardException - on error
See Also:
RowSource.getNextRowFromRowSource()

needsToClone

public boolean needsToClone()
Description copied from interface: RowSource
Does the caller of getNextRowFromRowSource() need to clone the row in order to keep a reference to the row past the getNextRowFromRowSource() call which returned the row. This call must always return the same for all rows in a RowSource (ie. the caller will call this once per scan from a RowSource and assume the behavior is true for all rows in the RowSource).

Specified by:
needsToClone in interface RowSource
See Also:
RowSource.needsToClone()

closeRowSource

public void closeRowSource()
Description copied from interface: RowSource
closeRowSource tells the RowSource that it will no longer need to return any rows and it can release any resource it may have. Subsequent call to any method on the RowSource will result in undefined behavior. A closed rowSource can be closed again.

Specified by:
closeRowSource in interface RowSource
See Also:
RowSource.closeRowSource()

needsRowLocation

public boolean needsRowLocation()
Description copied from interface: RowLocationRetRowSource
needsRowLocation returns true iff this the row source expects the drainer of the row source to call rowLocation after getting a row from getNextRowFromRowSource.

Specified by:
needsRowLocation in interface RowLocationRetRowSource
Returns:
true iff this row source expects some row location to be returned
See Also:
RowLocationRetRowSource.needsRowLocation()

rowLocation

public void rowLocation(RowLocation rl)
                 throws StandardException
Description copied from interface: RowLocationRetRowSource
rowLocation is a callback for the drainer of the row source to return the rowLocation of the current row, i.e, the row that is being returned by getNextRowFromRowSource. This interface is for the purpose of loading a base table with index. In that case, the indices can be built at the same time the base table is laid down once the row location of the base row is known. This is an example pseudo code on how this call is expected to be used:
                boolean needsRL = rowSource.needsRowLocation();
                DataValueDescriptor[] row;
                while((row = rowSource.getNextRowFromRowSource()) != null)
                {
                        RowLocation rl = heapConglomerate.insertRow(row);
                        if (needsRL)
                                rowSource.rowLocation(rl);
                }
                

NeedsRowLocation and rowLocation will ONLY be called by a drainer of the row source which CAN return a row location. Drainer of row source which cannot return rowLocation will guarentee to not call either callbacks. Conversely, if NeedsRowLocation is called and it returns true, then for every row return by getNextRowFromRowSource, a rowLocation callback must also be issued with the row location of the row. Implementor of both the source and the drain of the row source must be aware of this protocol.
The RowLocation object is own by the caller of rowLocation, in other words, the drainer of the RowSource. This is so that we don't need to new a row location for every row. If the Row Source wants to keep the row location, it needs to clone it (RowLocation is a ClonableObject).

Specified by:
rowLocation in interface RowLocationRetRowSource
Throws:
StandardException - on error
See Also:
RowLocationRetRowSource.rowLocation(org.apache.derby.iapi.types.RowLocation)

objectifyStreamingColumns

private void objectifyStreamingColumns()
                                throws StandardException
Throws:
StandardException

insertIntoSorter

private void insertIntoSorter(int index,
                              RowLocation rl)
                       throws StandardException
Throws:
StandardException

cleanUp

public void cleanUp()
             throws StandardException
Throws:
StandardException - Thrown on error
See Also:
ResultSet.cleanUp()

getSemiRowCount

private int getSemiRowCount(TransactionController tc)
                     throws StandardException
Return the "semi" row count of a table. We are only interested in whether the table has 0, 1 or > 1 rows.

Returns:
Number of rows (0, 1 or > 1) in table.
Throws:
StandardException - Thrown on failure

updateNewColumnToDefault

private void updateNewColumnToDefault(Activation activation,
                                      java.lang.String columnName,
                                      java.lang.String defaultText,
                                      LanguageConnectionContext lcc)
                               throws StandardException
Update a new column with its default. We could do the scan ourself here, but instead we get a nested connection and issue the appropriate update statement.

Parameters:
columnName - column name
defaultText - default text
lcc - the language connection context
Throws:
StandardException - if update to default fails

executeUpdate

private static void executeUpdate(LanguageConnectionContext lcc,
                                  java.lang.String updateStmt)
                           throws StandardException
Throws:
StandardException

getColumnMax

private long getColumnMax(Activation activation,
                          TableDescriptor td,
                          java.lang.String columnName,
                          long increment,
                          long initial)
                   throws StandardException
computes the minimum/maximum value in a column of a table.

Throws:
StandardException

dropAllColumnDefaults

private void dropAllColumnDefaults(UUID tableId,
                                   DataDictionary dd)
                            throws StandardException
Throws:
StandardException

openBulkFetchScan

private void openBulkFetchScan(long heapConglomNumber)
                        throws StandardException
Throws:
StandardException

closeBulkFetchScan

private void closeBulkFetchScan()
                         throws StandardException
Throws:
StandardException

updateNewAutoincrementColumn

private void updateNewAutoincrementColumn(Activation activation,
                                          java.lang.String columnName,
                                          long initial,
                                          long increment)
                                   throws StandardException
Update values in a new autoincrement column being added to a table. This is similar to updateNewColumnToDefault whereby we issue an update statement using a nested connection. The UPDATE statement uses a static method in ConnectionInfo (which is not documented) which returns the next value to be inserted into the autoincrement column.

Parameters:
columnName - autoincrement column name that is being added.
initial - initial value of the autoincrement column.
increment - increment value of the autoincrement column.
Throws:
StandardException
See Also:
updateNewColumnToDefault(org.apache.derby.iapi.sql.Activation, java.lang.String, java.lang.String, org.apache.derby.iapi.sql.conn.LanguageConnectionContext)

validateNotNullConstraint

private boolean validateNotNullConstraint(java.lang.String[] columnNames,
                                          boolean[] nullCols,
                                          int numRows,
                                          LanguageConnectionContext lcc,
                                          java.lang.String errorMsg)
                                   throws StandardException
Make sure that the columns are non null If any column is nullable, check that the data is null.

Parameters:
columnNames - names of columns to be checked
nullCols - true if corresponding column is nullable
numRows - number of rows in the table
lcc - language context
errorMsg - error message to use for exception
Returns:
true if any nullable columns found (nullable columns must have all non null data or exception is thrown
Throws:
StandardException - on error

compressIndexArrays

private java.lang.Object[] compressIndexArrays(long[] indexCIDS,
                                               IndexRowGenerator[] irgs)
Get rid of duplicates from a set of index conglomerate numbers and index descriptors.

Parameters:
indexCIDS - array of index conglomerate numbers
irgs - array of index row generaters
Returns:
value: If no duplicates, returns NULL; otherwise, a size-3 array of objects, first element is an array of duplicates' indexes in the input arrays; second element is the compact indexCIDs; third element is the compact irgs.

modifiesTableId

public boolean modifiesTableId(UUID tableId)
Does this constant action modify the passed in table uuid? By modify we mean add or drop things tied to this table (e.g. index, trigger, constraint). Things like views or spses that reference this table don't count.

Specified by:
modifiesTableId in interface ConstantAction
Overrides:
modifiesTableId in class DDLConstantAction
Parameters:
tableId - the table id

Built on Mon 2007-06-04 09:58:47+0400, from revision ???

Apache Derby V10.1 Engine Documentation - Copyright © 1997,2005 The Apache Software Foundation or its licensors, as applicable.