Class MergeNode
- All Implemented Interfaces:
Visitable
A MergeNode represents a MERGE statement. The statement looks like this...
MERGE INTO targetTable USING sourceTable ON searchCondition matchingClause1 ... matchingClauseN
...where each matching clause looks like this...
WHEN MATCHED [ AND matchingRefinement ] THEN DELETE
...or
WHEN MATCHED [ AND matchingRefinement ] THEN UPDATE SET col1 = expr1, ... colM = exprM
...or
WHEN NOT MATCHED [ AND matchingRefinement ] THEN INSERT columnList VALUES valueList
The Derby compiler essentially rewrites this statement into a driving left join followed by a series of DELETE/UPDATE/INSERT actions. The left join looks like this:
SELECT selectList FROM sourceTable LEFT OUTER JOIN targetTable ON searchCondition
The selectList of the driving left join consists of the following:
- All of the columns mentioned in the searchCondition.
- All of the columns mentioned in the matchingRefinement clauses.
- All of the columns mentioned in the SET clauses and the INSERT columnLists and valueLists.
- All additional columns needed for the triggers and foreign keys fired by the DeleteResultSets and UpdateResultSets constructed for the WHEN MATCHED clauses.
- All additional columns needed to build index rows and evaluate generated columns needed by the UpdateResultSets constructed for the WHEN MATCHED...THEN UPDATE clauses.
- A trailing targetTable.RowLocation column.
The driving left join's selectList then looks like this...
sc1, ..., scN, tc1, ..., tcM, targetTable.RowLocation
Where sc1...scN are the columns we need from the source table (in alphabetical order) and tc1...tcM are the columns we need from the target table (in alphabetical order).
The matchingRefinement expressions are bound and generated against the FromList of the driving left join. Dummy DeleteNode, UpdateNode, and InsertNode statements are independently constructed in order to bind and generate the DELETE/UPDATE/INSERT actions.
At execution time, the targetTable.RowLocation column is used to determine whether a given driving row matches. The row matches iff targetTable.RowLocation is not null. The driving row is then assigned to the first DELETE/UPDATE/INSERT action to which it applies. The relevant columns from the driving row are extracted and buffered in a temporary table (the "then" rows) specific to that DELETE/UPDATE/INSERT action. After the driving left join has been processed, the DELETE/UPDATE/INSERT actions are run in order, each taking its corresponding temporary table as its source ResultSet.
Name resolution was a particularly thorny problem. This is because name resolution behaves differently for SELECTs and UPDATEs. In particular, while processing UPDATEs, the compiler throws away name resolution information; this happens as a consequence of work done on DERBY-1043. In the end, I had to invent more name resolution machinery in order to compensate for the differences in the handling of SELECTs and UPDATEs. If we are to allow subqueries in matching refinement clauses and in the values expressions of INSERT and UPDATE actions, then we probably need to remove this special name resolution machinery. And that, in turn, probably means revisiting DERBY-1043.
The special name resolution machinery involves marking source and target column references in order to make it clear which table they belong to. This is done in associateColumn(). The markers are consulted at code-generation time in order to resolve column references when we generate the expressions needed to populate the rows which go into the temporary tables. That resolution happens in MatchingClauseNode.getSelectListOffset().
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ConstantActionprivate HalfOuterJoinNodeprivate CursorNodeprivate FromListprivate QueryTreeNodeVector<MatchingClauseNode> private ValueNodeprivate ResultColumnListprivate FromTableprivate FromBaseTable////////////////////////////////////////////////////////////////////////////////static final int////////////////////////////////////////////////////////////////////////////////private static final Stringstatic final intFields inherited from class DMLModStatementNode
dependentTables, fkColArrays, fkColDescriptors, fkIndexConglomNumbers, fkInfo, fkRefActions, fkSchemaNames, fkTableNames, indexConglomerateNumbers, indexNames, indicesToMaintain, isDependentTable, lockMode, matchingClause, relevantCdl, relevantTriggers, resultColumnList, synonymTableName, targetTableDescriptor, targetTableName, targetVTI, triggerInfoFields inherited from class DMLStatementNode
resultSetFields inherited from class StatementNode
EMPTY_TD_LIST, NEED_CURSOR_ACTIVATION, NEED_DDL_ACTIVATION, NEED_NOTHING_ACTIVATION, NEED_PARAM_ACTIVATION, NEED_ROW_ACTIVATIONFields inherited from class QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_CYCLE, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX -
Constructor Summary
ConstructorsConstructorDescriptionMergeNode(FromTable targetTable, FromTable sourceTable, ValueNode searchCondition, QueryTreeNodeVector<MatchingClauseNode> matchingClauses, ContextManager cm) Constructor for a MergeNode. -
Method Summary
Modifier and TypeMethodDescription(package private) voidAccept the visitor for all visitable children of this node.(package private) voidaddColumn(HashMap<String, ColumnReference> map, ColumnReference cr, int mergeTableID) Add a column to the evolving map of referenced columnsprivate voidAdd SELECT privilege on the indicated column.private voidaddColumns(FromTable fromTable, HashMap<String, ColumnReference> drivingColumnMap, ResultColumnList selectList, int mergeTableID) Add to an evolving select list the columns from the indicated table.private voidAdd the privileges required by the ON clause.private voidaddRoutinePrivilege(StaticMethodCallNode routine) Add EXECUTE privilege on the indicated routine.private voidaddTargetRowLocation(ResultColumnList selectList) Add the target table's row location to the left join's select list(package private) voidassociateColumn(FromList fromList, ColumnReference cr, int mergeTableID) Associate a column with the SOURCE or TARGET table.(package private) voidbindExpression(ValueNode value, FromList fromList) Boilerplate for binding an expression against a FromListprivate voidBind the driving left join select.void////////////////////////////////////////////////////////////////////////////////private ResultColumnListBuild the select list for the left join(package private) static voidcheckNoAggregates(QueryTreeNode clause) private FromListcloneFromList(DataDictionary dd, FromBaseTable targetTable) Create a FromList for binding a WHEN [ NOT ] MATCHED clauseprivate FromTablecloneFromTable(FromTable fromTable) Clone a FromTable to avoid binding the originalprivate voidBecause of name resolution complexities, we do not allow derived column lists on source or target tables.private voidNeither the source nor the target table may be a synonymprivate voidforbidSynonyms(TableName tableName) (package private) voidgenerate(ActivationClassBuilder acb, MethodBuilder mb) ////////////////////////////////////////////////////////////////////////////////getCastNodes(QueryTreeNode expression) Get a list of CastNodes in an expressionprivate List<ColumnReference> getColumnReferences(QueryTreeNode expression) Get a list of column references in an expressionprivate String[]getColumns(int mergeTableID, HashMap<String, ColumnReference> map) Get the column names from the table with the given table number, in sorted orderprivate voidgetColumnsFromList(HashMap<String, ColumnReference> map, List<ColumnReference> colRefs, int mergeTableID) Add a list of columns to the the evolving map(package private) voidgetColumnsFromList(HashMap<String, ColumnReference> map, ResultColumnList rcl, int mergeTableID) Add a list of columns to the the evolving map.(package private) voidgetColumnsInExpression(HashMap<String, ColumnReference> map, ValueNode expression, int mergeTableID) Add the columns in the matchingRefinement clause to the evolving map.private StringGet the exposed name of a FromTableprivate List<StaticMethodCallNode> getRoutineReferences(QueryTreeNode expression) Get a list of routines in an expression(package private) FromBaseTableGet the target table for the MERGE statementThis creates a class that will do the work that's constant across all Executions of a PreparedStatement.private StringmakeDCMKey(String tableName, String columnName) Make a HashMap key for a column in the driving column map of the LEFT JOINprivate voidThrow a "not base table" exceptionvoid////////////////////////////////////////////////////////////////////////////////(package private) voidprintSubNodes(int depth) Prints the sub-nodes of this object.booleanReturn true if the node references SESSION schema tables (temporary or permanent)private booleanReturn true if the source table is a base table, view, or table function(package private) Stringprivate booleantargetIsBaseTable(FromBaseTable targetTable) Return true if the target table is a base tableMethods inherited from class DMLModStatementNode
adjustDeferredFlag, bindConstraints, bindRowScopedExpression, generateCheckConstraints, generateCheckConstraints, generateCodeForTemporaryTable, generateGenerationClauses, getAffectedIndexes, getAllRelevantConstraints, getAllRelevantTriggers, getCheckConstraints, getFKInfo, getReadColMap, getResultColumnList, getResultColumnList, getSchemaDescriptor, getTriggerInfo, getXAffectedIndexes, hasCheckConstraints, hasGenerationClauses, inMatchingClause, isAtomic, markAffectedIndexes, normalizeSynonymColumns, parseAndBindGenerationClauses, parseCheckConstraint, parseGenerationClause, requiresDeferredProcessing, setRefActionInfo, setTarget, verifyTargetTableMethods inherited from class DMLStatementNode
activationKind, bind, bindExpressions, bindExpressionsWithTables, bindResultSetsWithTables, bindTables, generateParameterValueSet, getPrivType, getResultSetNode, makeResultDescriptionMethods inherited from class StatementNode
executeSchemaName, executeStatementName, generate, getCursorInfo, getSPSName, lockTableForCompilation, needsSavepoint, toString, updateIndexStatisticsForMethods inherited from class QueryTreeNode
accept, addTag, addUDTUsagePriv, addUDTUsagePriv, bindOffsetFetch, bindRowMultiSet, bindUserCatalogType, bindUserType, checkReliability, checkReliability, convertDefaultNode, copyTagsFrom, createTypeDependency, debugFlush, debugPrint, disablePrivilegeCollection, formatNodeString, generateAuthorizeCheck, getBeginOffset, getClassFactory, getCompilerContext, getContext, getContextManager, getDataDictionary, getDependencyManager, getEndOffset, getExecutionFactory, getGenericConstantActionFactory, getIntProperty, getLanguageConnectionContext, getLongProperty, getNullNode, getOffsetOrderedNodes, getOptimizerFactory, getOptimizerTracer, getParameterTypes, getSchemaDescriptor, getSchemaDescriptor, getStatementType, getTableDescriptor, getTypeCompiler, getUDTDesc, isPrivilegeCollectionRequired, isSessionSchema, isSessionSchema, makeTableName, makeTableName, nodeHeader, optimizerTracingIsOn, orReliability, parseSearchCondition, parseStatement, printLabel, resolveTableToSynonym, setBeginOffset, setEndOffset, stackPrint, taggedWith, treePrint, treePrint, verifyClassExist
-
Field Details
-
SOURCE_TABLE_INDEX
public static final int SOURCE_TABLE_INDEX////////////////////////////////////////////////////////////////////////////////- See Also:
-
TARGET_TABLE_INDEX
public static final int TARGET_TABLE_INDEX- See Also:
-
TARGET_ROW_LOCATION_NAME
- See Also:
-
_targetTable
//////////////////////////////////////////////////////////////////////////////// -
_sourceTable
-
_searchCondition
-
_matchingClauses
-
_selectList
-
_leftJoinFromList
-
_hojn
-
_constantAction
-
_leftJoinCursor
-
-
Constructor Details
-
MergeNode
public MergeNode(FromTable targetTable, FromTable sourceTable, ValueNode searchCondition, QueryTreeNodeVector<MatchingClauseNode> matchingClauses, ContextManager cm) throws StandardException Constructor for a MergeNode.
- Throws:
StandardException
-
-
Method Details
-
getTargetTable
FromBaseTable getTargetTable()Get the target table for the MERGE statement -
associateColumn
void associateColumn(FromList fromList, ColumnReference cr, int mergeTableID) throws StandardException Associate a column with the SOURCE or TARGET table. This is part of the special name resolution machinery which smooths over the differences between name resolution for SELECTs and UPDATEs.
- Throws:
StandardException
-
bindExpression
Boilerplate for binding an expression against a FromList- Throws:
StandardException
-
getColumnsInExpression
void getColumnsInExpression(HashMap<String, ColumnReference> map, ValueNode expression, int mergeTableID) throws StandardException Add the columns in the matchingRefinement clause to the evolving map. This is called when we're building the SELECT list for the driving left join.
- Throws:
StandardException
-
getColumnsFromList
void getColumnsFromList(HashMap<String, ColumnReference> map, ResultColumnList rcl, int mergeTableID) throws StandardException Add a list of columns to the the evolving map. This is called when we're building the SELECT list for the driving left join.
- Throws:
StandardException
-
bindStatement
////////////////////////////////////////////////////////////////////////////////- Overrides:
bindStatementin classStatementNode- Throws:
StandardException- Thrown on error
-
checkNoAggregates
- Throws:
StandardException
-
getExposedName
Get the exposed name of a FromTable- Throws:
StandardException
-
referencesSessionSchema
Description copied from class:QueryTreeNodeReturn true if the node references SESSION schema tables (temporary or permanent)- Overrides:
referencesSessionSchemain classQueryTreeNode- Returns:
- true if references SESSION schema tables, else false
- Throws:
StandardException- Thrown on error
-
forbidDerivedColumnLists
Because of name resolution complexities, we do not allow derived column lists on source or target tables. These lists arise in queries like the following:
merge into t1 r( x ) using t2 on r.x = t2.a when matched then delete; merge into t1 using t2 r( x ) on t1.a = r.x when matched then delete;
- Throws:
StandardException
-
forbidSynonyms
Neither the source nor the target table may be a synonym- Throws:
StandardException
-
forbidSynonyms
- Throws:
StandardException
-
notBaseTable
Throw a "not base table" exception- Throws:
StandardException
-
targetIsBaseTable
Return true if the target table is a base table- Throws:
StandardException
-
sourceIsBase_or_VTI
Return true if the source table is a base table, view, or table function- Throws:
StandardException
-
bindLeftJoin
Bind the driving left join select. Stuffs the left join SelectNode into the resultSet variable.- Throws:
StandardException
-
cloneFromList
private FromList cloneFromList(DataDictionary dd, FromBaseTable targetTable) throws StandardException Create a FromList for binding a WHEN [ NOT ] MATCHED clause- Throws:
StandardException
-
cloneFromTable
Clone a FromTable to avoid binding the original- Throws:
StandardException
-
addOnClausePrivileges
Add the privileges required by the ON clause.
- Throws:
StandardException
-
addColumnPrivilege
Add SELECT privilege on the indicated column.
- Throws:
StandardException
-
addRoutinePrivilege
Add EXECUTE privilege on the indicated routine.
- Throws:
StandardException
-
getCastNodes
Get a list of CastNodes in an expression- Throws:
StandardException
-
getRoutineReferences
private List<StaticMethodCallNode> getRoutineReferences(QueryTreeNode expression) throws StandardException Get a list of routines in an expression- Throws:
StandardException
-
buildSelectList
Build the select list for the left join- Throws:
StandardException
-
addTargetRowLocation
Add the target table's row location to the left join's select list- Throws:
StandardException
-
addColumns
private void addColumns(FromTable fromTable, HashMap<String, ColumnReference> drivingColumnMap, ResultColumnList selectList, int mergeTableID) throws StandardException Add to an evolving select list the columns from the indicated table.
- Throws:
StandardException
-
getColumns
Get the column names from the table with the given table number, in sorted order -
getColumnReferences
private List<ColumnReference> getColumnReferences(QueryTreeNode expression) throws StandardException Get a list of column references in an expression- Throws:
StandardException
-
getColumnsFromList
private void getColumnsFromList(HashMap<String, ColumnReference> map, List<ColumnReference> colRefs, int mergeTableID) throws StandardException Add a list of columns to the the evolving map- Throws:
StandardException
-
addColumn
void addColumn(HashMap<String, ColumnReference> map, ColumnReference cr, int mergeTableID) throws StandardException Add a column to the evolving map of referenced columns- Throws:
StandardException
-
makeDCMKey
-
optimizeStatement
////////////////////////////////////////////////////////////////////////////////- Overrides:
optimizeStatementin classDMLModStatementNode- Throws:
StandardException- Thrown on failure
-
generate
////////////////////////////////////////////////////////////////////////////////- Overrides:
generatein classQueryTreeNode- Parameters:
acb- The ActivationClassBuilder for the class being builtmb- The method for the generated code to go into- Throws:
StandardException- Thrown on error
-
makeConstantAction
Description copied from class:QueryTreeNodeThis creates a class that will do the work that's constant across all Executions of a PreparedStatement. It's up to our subclasses to override this method if they need to compile constant actions into PreparedStatements.- Overrides:
makeConstantActionin classQueryTreeNode- Throws:
StandardException- Thrown on failure
-
acceptChildren
Accept the visitor for all visitable children of this node.- Overrides:
acceptChildrenin classDMLModStatementNode- Parameters:
v- the visitor- Throws:
StandardException- on error
-
printSubNodes
void printSubNodes(int depth) Prints the sub-nodes of this object. See QueryTreeNode.java for how tree printing is supposed to work.- Overrides:
printSubNodesin classDMLModStatementNode- Parameters:
depth- The depth of this node in the tree
-
statementToString
String statementToString()- Overrides:
statementToStringin classDMLModStatementNode
-