use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class DeleteNode method makeConstantAction.
/**
* Compile constants that Execution will use
*
* @exception StandardException Thrown on failure
*/
@Override
public ConstantAction makeConstantAction() throws StandardException {
/* Different constant actions for base tables and updatable VTIs */
if (targetTableDescriptor != null) {
// Base table
int lckMode = resultSet.updateTargetLockMode();
long heapConglomId = targetTableDescriptor.getHeapConglomerateId();
TransactionController tc = getLanguageConnectionContext().getTransactionCompile();
StaticCompiledOpenConglomInfo[] indexSCOCIs = new StaticCompiledOpenConglomInfo[indexConglomerateNumbers.length];
for (int index = 0; index < indexSCOCIs.length; index++) {
indexSCOCIs[index] = tc.getStaticCompiledConglomInfo(indexConglomerateNumbers[index]);
}
/*
** Do table locking if the table's lock granularity is
** set to table.
*/
if (targetTableDescriptor.getLockGranularity() == TableDescriptor.TABLE_LOCK_GRANULARITY) {
lckMode = TransactionController.MODE_TABLE;
}
ResultDescription resultDescription = null;
if (isDependentTable) {
// triggers need the result description ,
// dependent tables don't have a source from generation time
// to get the result description
resultDescription = makeResultDescription();
}
return getGenericConstantActionFactory().getDeleteConstantAction(heapConglomId, targetTableDescriptor.getTableType(), tc.getStaticCompiledConglomInfo(heapConglomId), indicesToMaintain, indexConglomerateNumbers, indexSCOCIs, deferred, false, targetTableDescriptor.getUUID(), lckMode, null, null, null, 0, null, null, resultDescription, getFKInfo(), getTriggerInfo(), (readColsBitSet == null) ? (FormatableBitSet) null : new FormatableBitSet(readColsBitSet), getReadColMap(targetTableDescriptor.getNumberOfColumns(), readColsBitSet), resultColumnList.getStreamStorableColIds(targetTableDescriptor.getNumberOfColumns()), (readColsBitSet == null) ? targetTableDescriptor.getNumberOfColumns() : readColsBitSet.getNumBitsSet(), (UUID) null, resultSet.isOneRowResultSet(), dependentConstantActions, inMatchingClause());
} else {
/* Return constant action for VTI
* NOTE: ConstantAction responsible for preserving instantiated
* VTIs for in-memory queries and for only preserving VTIs
* that implement Serializable for SPSs.
*/
return getGenericConstantActionFactory().getUpdatableVTIConstantAction(DeferModification.DELETE_STATEMENT, deferred);
}
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class DeleteNode method getDeleteReadMap.
/**
* Builds a bitmap of all columns which should be read from the
* Store in order to satisfy an DELETE statement.
*
* 1) finds all indices on this table
* 2) adds the index columns to a bitmap of affected columns
* 3) adds the index descriptors to a list of conglomerate
* descriptors.
* 4) finds all DELETE triggers on the table
* 5) if there are any DELETE triggers, then do one of the following
* a)If all of the triggers have MISSING referencing clause, then that
* means that the trigger actions do not have access to before and
* after values. In that case, there is no need to blanketly decide
* to include all the columns in the read map just because there are
* triggers defined on the table.
* b)Since one/more triggers have REFERENCING clause on them, get all
* the columns because we don't know what the user will ultimately
* reference.
* 6) adds the triggers to an evolving list of triggers
*
* @param conglomerates OUT: list of affected indices
* @param relevantTriggers IN/OUT. Passed in as an empty list. Filled in as we go.
* @param needsDeferredProcessing IN/OUT. true if the statement already needs
* deferred processing. set while evaluating this
* routine if a trigger requires
* deferred processing
*
* @return a FormatableBitSet of columns to be read out of the base table
*
* @exception StandardException Thrown on error
*/
private static FormatableBitSet getDeleteReadMap(TableDescriptor baseTable, List<ConglomerateDescriptor> conglomerates, TriggerDescriptorList relevantTriggers, boolean[] needsDeferredProcessing) throws StandardException {
int columnCount = baseTable.getMaxColumnID();
FormatableBitSet columnMap = new FormatableBitSet(columnCount + 1);
/*
** Get a list of the indexes that need to be
** updated. ColumnMap contains all indexed
** columns where 1 or more columns in the index
** are going to be modified.
**
** Notice that we don't need to add constraint
** columns. This is because we add all key constraints
** (e.g. foreign keys) as a side effect of adding their
** indexes above. And we don't need to deal with
** check constraints on a delete.
**
** Adding indexes also takes care of the replication
** requirement of having the primary key.
*/
DMLModStatementNode.getXAffectedIndexes(baseTable, null, columnMap, conglomerates);
/*
** If we have any DELETE triggers, then do one of the following
** 1)If all of the triggers have MISSING referencing clause, then that
** means that the trigger actions do not have access to before and
** after values. In that case, there is no need to blanketly decide to
** include all the columns in the read map just because there are
** triggers defined on the table.
** 2)Since one/more triggers have REFERENCING clause on them, get all
** the columns because we don't know what the user will ultimately reference.
*/
baseTable.getAllRelevantTriggers(StatementType.DELETE, (int[]) null, relevantTriggers);
if (relevantTriggers.size() > 0) {
needsDeferredProcessing[0] = true;
boolean needToIncludeAllColumns = false;
for (TriggerDescriptor trd : relevantTriggers) {
// If yes, then read all the columns from the trigger table.
if (!trd.getReferencingNew() && !trd.getReferencingOld())
continue;
else {
needToIncludeAllColumns = true;
break;
}
}
if (needToIncludeAllColumns) {
for (int i = 1; i <= columnCount; i++) {
columnMap.set(i);
}
}
}
return columnMap;
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class UpdateNode method makeConstantAction.
/**
* Compile constants that Execution will use
*
* @exception StandardException Thrown on failure
*/
@Override
public ConstantAction makeConstantAction() throws StandardException {
/*
** Updates are also deferred if they update a column in the index
** used to scan the table being updated.
*/
if (!deferred && !inMatchingClause()) {
ConglomerateDescriptor updateCD = targetTable.getTrulyTheBestAccessPath().getConglomerateDescriptor();
if (updateCD != null && updateCD.isIndex()) {
int[] baseColumns = updateCD.getIndexDescriptor().baseColumnPositions();
if (resultSet.getResultColumns().updateOverlaps(baseColumns)) {
deferred = true;
}
}
}
if (null == targetTableDescriptor) {
/* Return constant action for VTI
* NOTE: ConstantAction responsible for preserving instantiated
* VTIs for in-memory queries and for only preserving VTIs
* that implement Serializable for SPSs.
*/
return getGenericConstantActionFactory().getUpdatableVTIConstantAction(DeferModification.UPDATE_STATEMENT, deferred, changedColumnIds);
}
int lckMode = inMatchingClause() ? TransactionController.MODE_RECORD : resultSet.updateTargetLockMode();
long heapConglomId = targetTableDescriptor.getHeapConglomerateId();
TransactionController tc = getLanguageConnectionContext().getTransactionCompile();
StaticCompiledOpenConglomInfo[] indexSCOCIs = new StaticCompiledOpenConglomInfo[indexConglomerateNumbers.length];
for (int index = 0; index < indexSCOCIs.length; index++) {
indexSCOCIs[index] = tc.getStaticCompiledConglomInfo(indexConglomerateNumbers[index]);
}
/*
** Do table locking if the table's lock granularity is
** set to table.
*/
if (targetTableDescriptor.getLockGranularity() == TableDescriptor.TABLE_LOCK_GRANULARITY) {
lckMode = TransactionController.MODE_TABLE;
}
return getGenericConstantActionFactory().getUpdateConstantAction(targetTableDescriptor, tc.getStaticCompiledConglomInfo(heapConglomId), indicesToMaintain, indexConglomerateNumbers, indexSCOCIs, indexNames, deferred, targetTableDescriptor.getUUID(), lckMode, false, changedColumnIds, null, null, getFKInfo(), getTriggerInfo(), (readColsBitSet == null) ? (FormatableBitSet) null : new FormatableBitSet(readColsBitSet), getReadColMap(targetTableDescriptor.getNumberOfColumns(), readColsBitSet), resultColumnList.getStreamStorableColIds(targetTableDescriptor.getNumberOfColumns()), (readColsBitSet == null) ? targetTableDescriptor.getNumberOfColumns() : readColsBitSet.getNumBitsSet(), positionedUpdate, resultSet.isOneRowResultSet(), autoincRowLocation, inMatchingClause(), identitySequenceUUIDString);
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class UpdateNode method bindStatement.
/**
* Bind this UpdateNode. This means looking up tables and columns and
* getting their types, and figuring out the result types of all
* expressions, as well as doing view resolution, permissions checking,
* etc.
* <p>
* Binding an update will also massage the tree so that
* the ResultSetNode has a set of columns to contain the old row
* value, followed by a set of columns to contain the new row
* value, followed by a column to contain the RowLocation of the
* row to be updated.
*
* @exception StandardException Thrown on error
*/
@Override
public void bindStatement() throws StandardException {
// We just need select privilege on the expressions
getCompilerContext().pushCurrentPrivType(Authorizer.SELECT_PRIV);
FromList fromList = new FromList(getOptimizerFactory().doJoinOrderOptimization(), getContextManager());
TableName cursorTargetTableName = null;
CurrentOfNode currentOfNode = null;
ResultColumnList afterColumns = null;
DataDictionary dataDictionary = getDataDictionary();
// check if targetTable is a synonym
if (targetTableName != null) {
TableName synonymTab = resolveTableToSynonym(this.targetTableName);
if (synonymTab != null) {
this.synonymTableName = targetTableName;
this.targetTableName = synonymTab;
}
}
//
if (inMatchingClause()) {
tagOriginalResultSetColumns();
}
// collect lists of objects which will require privilege checks
ArrayList<String> explicitlySetColumns = getExplicitlySetColumns();
List<CastNode> allCastNodes = collectAllCastNodes();
tagPrivilegedNodes();
// tell the compiler to only add privilege checks for nodes which have been tagged
TagFilter tagFilter = new TagFilter(TagFilter.NEED_PRIVS_FOR_UPDATE_STMT);
getCompilerContext().addPrivilegeFilter(tagFilter);
bindTables(dataDictionary);
// for positioned update, get the cursor's target table.
if (SanityManager.DEBUG) {
SanityManager.ASSERT((resultSet != null && resultSet instanceof SelectNode), "Update must have a select result set");
}
SelectNode sel;
sel = (SelectNode) resultSet;
targetTable = (FromTable) sel.fromList.elementAt(0);
if (targetTable instanceof CurrentOfNode) {
positionedUpdate = true;
currentOfNode = (CurrentOfNode) targetTable;
cursorTargetTableName = currentOfNode.getBaseCursorTargetTableName();
// instead of an assert, we might say the cursor is not updatable.
if (SanityManager.DEBUG) {
SanityManager.ASSERT(cursorTargetTableName != null);
}
}
if (targetTable instanceof FromVTI) {
targetVTI = (FromVTI) targetTable;
targetVTI.setTarget();
} else {
// we get it from the cursor supplying the position.
if (targetTableName == null) {
// verify we have current of
if (SanityManager.DEBUG)
SanityManager.ASSERT(cursorTargetTableName != null);
targetTableName = cursorTargetTableName;
} else // the named table is the same as the cursor's target.
if (cursorTargetTableName != null) {
// be the same as a correlation name in the cursor.
if (!targetTableName.equals(cursorTargetTableName)) {
throw StandardException.newException(SQLState.LANG_CURSOR_UPDATE_MISMATCH, targetTableName, currentOfNode.getCursorName());
}
}
}
// because we verified that the tables match
// and we already bound the cursor or the select,
// the table descriptor should always be found.
verifyTargetTable();
// add UPDATE_PRIV on all columns on the left side of SET operators
addUpdatePriv(explicitlySetColumns);
/* Verify that all underlying ResultSets reclaimed their FromList */
if (SanityManager.DEBUG) {
SanityManager.ASSERT(fromList.size() == 0, "fromList.size() is expected to be 0, not " + fromList.size() + " on return from RS.bindExpressions()");
}
//
// Add generated columns whose generation clauses mention columns
// in the user's original update list.
//
ColumnDescriptorList addedGeneratedColumns = new ColumnDescriptorList();
ColumnDescriptorList affectedGeneratedColumns = new ColumnDescriptorList();
addGeneratedColumns(targetTableDescriptor, resultSet, affectedGeneratedColumns, addedGeneratedColumns);
/*
** The current result column list is the one supplied by the user.
** Mark these columns as "updated", so we can tell later which
** columns are really being updated, and which have been added
** but are not really being updated.
*/
resultSet.getResultColumns().markUpdated();
/* Prepend CurrentRowLocation() to the select's result column list. */
if (SanityManager.DEBUG)
SanityManager.ASSERT((resultSet.getResultColumns() != null), "resultColumns is expected not to be null at bind time");
/* Normalize the SET clause's result column list for synonym */
if (synonymTableName != null)
normalizeSynonymColumns(resultSet.getResultColumns(), targetTable);
/* Bind the original result columns by column name */
normalizeCorrelatedColumns(resultSet.getResultColumns(), targetTable);
resultSet.bindResultColumns(targetTableDescriptor, targetVTI, resultSet.getResultColumns(), this, fromList);
// don't allow overriding of generation clauses
forbidGenerationOverrides(resultSet.getResultColumns(), addedGeneratedColumns);
// the code for old way of generating unique ids.
if (dataDictionary.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
// Replace any DEFAULTs with the associated tree for the default if
// allowed, otherwise throw an exception
resultSet.getResultColumns().replaceOrForbidDefaults(targetTableDescriptor, resultSet.getResultColumns(), true);
resultSet.getResultColumns().checkForInvalidDefaults();
resultSet.getResultColumns().forbidOverrides(resultSet.getResultColumns());
} else {
LanguageConnectionContext lcc = getLanguageConnectionContext();
if (lcc.getAutoincrementUpdate() == false)
resultSet.getResultColumns().forbidOverrides(null);
}
/*
** Mark the columns in this UpdateNode's result column list as
** updateable in the ResultColumnList of the table being updated.
** only do this for FromBaseTables - if the result table is a
** CurrentOfNode, it already knows what columns in its cursor
** are updateable.
*/
boolean allColumns = false;
if (targetTable instanceof FromBaseTable) {
((FromBaseTable) targetTable).markUpdated(resultSet.getResultColumns());
} else if ((targetTable instanceof FromVTI) || (targetTable instanceof FromSubquery)) {
resultColumnList = resultSet.getResultColumns();
} else {
/*
** Positioned update: WHERE CURRENT OF
*/
if (SanityManager.DEBUG) {
SanityManager.ASSERT(currentOfNode != null, "currentOfNode is null");
}
ExecPreparedStatement cursorStmt = currentOfNode.getCursorStatement();
/*
** If there is no update column list, we need to build
** out the result column list to have all columns.
*/
if (!cursorStmt.hasUpdateColumns()) {
/*
** Get the resultColumnList representing ALL of the columns in the
** base table. This is the "before" portion of the result row.
*/
getResultColumnList();
/*
** Add the "after" portion of the result row. This is the update
** list augmented to include every column in the target table.
** Those columns that are not being updated are set to themselves.
** The expanded list will be in the order of the columns in the base
** table.
*/
afterColumns = resultSet.getResultColumns().expandToAll(targetTableDescriptor, targetTable.getTableName());
/*
** Need to get all indexes here since we aren't calling
** getReadMap().
*/
getAffectedIndexes(targetTableDescriptor, (ResultColumnList) null, (FormatableBitSet) null);
allColumns = true;
} else {
/* Check the updatability */
resultSet.getResultColumns().checkColumnUpdateability(cursorStmt, currentOfNode.getCursorName());
}
}
changedColumnIds = getChangedColumnIds(resultSet.getResultColumns());
//
// Trigger transition tables are implemented as VTIs. This short-circuits some
// necessary steps if the source table of a MERGE statement is a trigger
// transition table. The following boolean is meant to prevent that short-circuiting.
//
boolean needBaseColumns = (targetVTI == null) || inMatchingClause();
/*
** We need to add in all the columns that are needed
** by the constraints on this table.
*/
if (!allColumns && needBaseColumns) {
getCompilerContext().pushCurrentPrivType(Authorizer.NULL_PRIV);
try {
readColsBitSet = new FormatableBitSet();
FromBaseTable fbt = getResultColumnList(resultSet.getResultColumns());
afterColumns = resultSet.getResultColumns().copyListAndObjects();
readColsBitSet = getReadMap(dataDictionary, targetTableDescriptor, afterColumns, affectedGeneratedColumns);
afterColumns = fbt.addColsToList(afterColumns, readColsBitSet);
resultColumnList = fbt.addColsToList(resultColumnList, readColsBitSet);
/*
** If all bits are set, then behave as if we chose all
** in the first place
*/
int i = 1;
int size = targetTableDescriptor.getMaxColumnID();
for (; i <= size; i++) {
if (!readColsBitSet.get(i)) {
break;
}
}
if (i > size) {
readColsBitSet = null;
}
} finally {
getCompilerContext().popCurrentPrivType();
}
}
ValueNode rowLocationNode;
if (needBaseColumns) {
/* Append the list of "after" columns to the list of "before" columns,
* preserving the afterColumns list. (Necessary for binding
* check constraints.)
*/
resultColumnList.appendResultColumns(afterColumns, false);
/* Generate the RowLocation column */
rowLocationNode = new CurrentRowLocationNode(getContextManager());
} else {
rowLocationNode = new NumericConstantNode(TypeId.getBuiltInTypeId(Types.INTEGER), 0, getContextManager());
}
ResultColumn rowLocationColumn = new ResultColumn(COLUMNNAME, rowLocationNode, getContextManager());
rowLocationColumn.markGenerated();
/* Append to the ResultColumnList */
resultColumnList.addResultColumn(rowLocationColumn);
/*
* The last thing that we do to the generated RCL is to clear
* the table name out from each RC. See comment on
* checkTableNameAndScrubResultColumns().
*/
checkTableNameAndScrubResultColumns(resultColumnList);
/* Set the new result column list in the result set */
resultSet.setResultColumns(resultColumnList);
//
if (inMatchingClause()) {
associateAddedColumns();
}
// SQL 2011, section 6.10, SR 4b.
SelectNode.checkNoWindowFunctions(resultSet, "<update source>");
/* Bind the expressions */
super.bindExpressions();
/* Bind untyped nulls directly under the result columns */
resultSet.getResultColumns().bindUntypedNullsToResultColumns(resultColumnList);
/* Bind the new ResultColumn */
rowLocationColumn.bindResultColumnToExpression();
resultColumnList.checkStorableExpressions();
/* Insert a NormalizeResultSetNode above the source if the source
* and target column types and lengths do not match.
*/
if (!resultColumnList.columnTypesAndLengthsMatch()) {
resultSet = new NormalizeResultSetNode(resultSet, resultColumnList, null, true, getContextManager());
if (hasCheckConstraints(dataDictionary, targetTableDescriptor) || hasGenerationClauses(targetTableDescriptor)) {
/* Get and bind all check constraints and generated columns on the columns
* being updated. We want to bind the check constraints and
* generated columns against
* the after columns. We need to bind against the portion of the
* resultColumns in the new NormalizeResultSet that point to
* afterColumns. Create an RCL composed of just those RCs in
* order to bind the check constraints.
*/
int afterColumnsSize = afterColumns.size();
afterColumns = new ResultColumnList(getContextManager());
ResultColumnList normalizedRCs = resultSet.getResultColumns();
for (int index = 0; index < afterColumnsSize; index++) {
afterColumns.addElement(normalizedRCs.elementAt(index + afterColumnsSize));
}
}
}
if (null != targetVTI && !inMatchingClause()) {
deferred = VTIDeferModPolicy.deferIt(DeferModification.UPDATE_STATEMENT, targetVTI, resultColumnList.getColumnNames(), sel.getWhereClause());
} else // not VTI
{
/* we always include triggers in core language */
boolean hasTriggers = (getAllRelevantTriggers(dataDictionary, targetTableDescriptor, changedColumnIds, true).size() > 0);
ResultColumnList sourceRCL = hasTriggers ? resultColumnList : afterColumns;
/* bind all generation clauses for generated columns */
parseAndBindGenerationClauses(dataDictionary, targetTableDescriptor, afterColumns, resultColumnList, true, resultSet);
/* Get and bind all constraints on the columns being updated */
checkConstraints = bindConstraints(dataDictionary, getOptimizerFactory(), targetTableDescriptor, null, sourceRCL, changedColumnIds, readColsBitSet, true, /* we always include triggers in core language */
new boolean[1]);
/* If the target table is also a source table, then
* the update will have to be in deferred mode
* For updates, this means that the target table appears in a
* subquery. Also, self referencing foreign keys are
* deferred. And triggers cause an update to be deferred.
*/
if (resultSet.subqueryReferencesTarget(targetTableDescriptor.getName(), true) || requiresDeferredProcessing()) {
deferred = true;
}
TransactionController tc = getLanguageConnectionContext().getTransactionCompile();
autoincRowLocation = dataDictionary.computeAutoincRowLocations(tc, targetTableDescriptor);
}
identitySequenceUUIDString = getUUIDofSequenceGenerator();
getCompilerContext().popCurrentPrivType();
getCompilerContext().removePrivilegeFilter(tagFilter);
//
for (CastNode value : allCastNodes) {
addUDTUsagePriv(value);
}
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class WindowResultSetNode method generate.
@Override
void generate(ActivationClassBuilder acb, MethodBuilder mb) throws StandardException {
// Get the next ResultSet#, so we can number this ResultSetNode, its
// ResultColumnList and ResultSet.
assignResultSetNumber();
// Get the final cost estimate from the child.
setCostEstimate(childResult.getFinalCostEstimate());
acb.pushGetResultSetFactoryExpression(mb);
int rclSize = getResultColumns().size();
FormatableBitSet referencedCols = new FormatableBitSet(rclSize);
for (int index = rclSize - 1; index >= 0; index--) {
ResultColumn rc = getResultColumns().elementAt(index);
ValueNode expr = rc.getExpression();
if (rc.isGenerated() && (expr instanceof ColumnReference) && ((ColumnReference) expr).getGeneratedToReplaceWindowFunctionCall()) {
// meaningless to copy these, they arise in this rs.
} else {
referencedCols.set(index);
}
}
int erdNumber = acb.addItem(referencedCols);
// arg 1
acb.pushThisAsActivation(mb);
// arg 2
childResult.generate(acb, mb);
mb.upCast(ClassName.NoPutResultSet);
/* row allocator */
// arg 3
mb.push(acb.addItem(getResultColumns().buildRowTemplate()));
// arg 4
mb.push(getResultSetNumber());
/* Pass in the erdNumber for the referenced column FormatableBitSet */
// arg 5
mb.push(erdNumber);
/* There is no restriction at this level, we just want to pass null. */
// arg 6
mb.pushNull(ClassName.GeneratedMethod);
// arg 7
mb.push(getCostEstimate().rowCount());
// arg 8
mb.push(getCostEstimate().getEstimatedCost());
mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null, "getWindowResultSet", ClassName.NoPutResultSet, 8);
}
Aggregations