public class SubqueryNode extends ValueNode
Modifier and Type | Field and Description |
---|---|
(package private) boolean |
distinctExpression |
private boolean |
doneCorrelationCheck |
private boolean |
doneInvariantCheck |
static int |
EQ_ALL_SUBQUERY |
static int |
EQ_ANY_SUBQUERY |
static int |
EXISTS_SUBQUERY |
static int |
EXPRESSION_SUBQUERY |
private ValueNode |
fetchFirst |
private boolean |
foundCorrelation |
private boolean |
foundVariant |
static int |
FROM_SUBQUERY |
static int |
GE_ALL_SUBQUERY |
static int |
GE_ANY_SUBQUERY |
static int |
GT_ALL_SUBQUERY |
static int |
GT_ANY_SUBQUERY |
private boolean |
hasJDBClimitClause |
(package private) boolean |
havingSubquery
is this subquery part of a having clause.
|
static int |
IN_SUBQUERY |
static int |
LE_ALL_SUBQUERY |
static int |
LE_ANY_SUBQUERY |
(package private) ValueNode |
leftOperand |
static int |
LT_ALL_SUBQUERY |
static int |
LT_ANY_SUBQUERY |
static int |
NE_ALL_SUBQUERY |
static int |
NE_ANY_SUBQUERY |
static int |
NOT_EXISTS_SUBQUERY |
static int |
NOT_IN_SUBQUERY |
static int |
NOTIMPLEMENTED_SUBQUERY |
private ValueNode |
offset |
private OrderByList |
orderByList |
(package private) BinaryComparisonOperatorNode |
parentComparisonOperator |
private int |
pointOfAttachment |
(package private) boolean |
preprocessed |
(package private) boolean |
pushedNewPredicate |
(package private) ResultSetNode |
resultSet |
private int |
subqueryNumber |
(package private) int |
subqueryType |
private BooleanConstantNode |
trueNode |
(package private) boolean |
underTopAndNode |
(package private) boolean |
whereSubquery |
transformed
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX, isPrivilegeCollectionRequired
Constructor and Description |
---|
SubqueryNode() |
Modifier and Type | Method and Description |
---|---|
(package private) void |
acceptChildren(Visitor v)
Accept the visitor for all visitable children of this node.
|
ValueNode |
bindExpression(FromList fromList,
SubqueryList subqueryList,
java.util.List aggregateVector)
Bind this expression.
|
private boolean |
canAllBeFlattened()
Can NOT IN, ALL be falttened to NOT EXISTS join?
|
boolean |
categorize(JBitSet referencedTabs,
boolean simplePredsOnly)
Categorize this predicate.
|
ValueNode |
changeToCNF(boolean underTopAndNode)
Finish putting an expression into conjunctive normal
form.
|
private void |
changeToCorrespondingExpressionType()
Convert this IN/ANY subquery, which is known to return at most 1 row,
to an equivalent expression subquery.
|
(package private) ValueNode |
eliminateNots(boolean underNotNode)
Eliminate NotNodes in the current query block.
|
private ValueNode |
flattenToExistsJoin(int numTables,
FromList outerFromList,
SubqueryList outerSubqueryList,
PredicateList outerPredicateList,
boolean flattenableNotExists)
Flatten this subquery into the outer query block
as an exists join.
|
private ValueNode |
flattenToNormalJoin(int numTables,
FromList outerFromList,
SubqueryList outerSubqueryList,
PredicateList outerPredicateList)
Flatten this subquery into the outer query block.
|
void |
generateExpression(ExpressionClassBuilder expressionBuilder,
MethodBuilder mbex)
Do code generation for this subquery.
|
private LocalField |
generateMaterialization(ActivationClassBuilder acb,
MethodBuilder mbsq,
java.lang.String type) |
ValueNode |
getFetchFirst()
Get FETCH FIRST (used to construct FROM_SUBQUERY only), cf.
|
private BinaryComparisonOperatorNode |
getNewJoinCondition(ValueNode leftOperand,
ValueNode rightOperand)
Build a new join condition between the leftOperand
and the rightOperand.
|
ValueNode |
getOffset()
Get OFFSET (used to construct FROM_SUBQUERY only), cf.
|
protected int |
getOrderableVariantType()
Return the variant type for the underlying expression.
|
OrderByList |
getOrderByList()
Get ORDER BY list (used to construct FROM_SUBQUERY only), cf.
|
int |
getPointOfAttachment()
Get the ResultSet # for the point of attachment for this SubqueryNode.
|
(package private) boolean |
getPreprocessed()
Get whether or not this SubqueryNode has already been
preprocessed.
|
ResultSetNode |
getResultSet()
Return the resultSet for this SubqueryNode.
|
private ValueNode |
getRightOperand()
Get the node that will be the right operand in the join condition if
this ALL/ANY/SOME/(NOT) IN subquery is flattened to a join.
|
int |
getSubqueryType()
Return the type of this subquery.
|
private BooleanConstantNode |
getTrueNode() |
boolean |
getUnderTopAndNode()
Return whether or not this subquery is immediately under a top level
AndNode.
|
boolean |
hasCorrelatedCRs()
Check to see if this subquery has correlated
column references.
|
boolean |
hasJDBClimitClause()
Return true if the offset/fetchFirst clauses were added by JDBC LIMIT escape syntax.
|
void |
init(java.lang.Object resultSet,
java.lang.Object subqueryType,
java.lang.Object leftOperand,
java.lang.Object orderCols,
java.lang.Object offset,
java.lang.Object fetchFirst,
java.lang.Object hasJDBClimitClause)
Initializer.
|
private boolean |
isALL() |
private boolean |
isANY() |
protected boolean |
isEquivalent(ValueNode o)
Tests if this node is equivalent to the specified ValueNode.
|
private boolean |
isEXISTS() |
boolean |
isHavingSubquery()
Is this subquery part of a having clause?
|
private boolean |
isIN() |
private boolean |
isInvariant()
Check to see if we have a Variant value below us.
|
(package private) boolean |
isMaterializable() |
private boolean |
isNOT_EXISTS() |
private boolean |
isNOT_IN() |
boolean |
isWhereExistsAnyInWithWhereSubquery()
Check whether this is a WHERE EXISTS | ANY | IN subquery with a subquery
in its own WHERE clause.
|
boolean |
isWhereSubquery()
Is this subquery part of a whereclause?
|
void |
modifyAccessPaths()
Make any changes to the access paths, as decided by the optimizer.
|
void |
optimize(DataDictionary dataDictionary,
double outerRows)
Optimize this SubqueryNode.
|
ValueNode |
preprocess(int numTables,
FromList outerFromList,
SubqueryList outerSubqueryList,
PredicateList outerPredicateList)
Preprocess an expression tree.
|
void |
printSubNodes(int depth)
Prints the sub-nodes of this object.
|
private UnaryComparisonOperatorNode |
pushNewPredicate(int numTables)
Transform:
expresion QuantifiedOperator (select x from ...)
|
ValueNode |
remapColumnReferencesToExpressions()
Remap all ColumnReferences in this tree to be clones of the
underlying expression.
|
private boolean |
rightOperandFlattenableToNotExists(int numTables,
FromBaseTable fbt)
Check if the right operand is on a form that makes it possible to
flatten this query to a NOT EXISTS join.
|
private void |
setDataTypeServices(ResultColumnList resultColumns) |
void |
setHavingSubquery(boolean havingSubquery)
Mark this subquery as being part of a having clause.
|
(package private) void |
setParentComparisonOperator(BinaryComparisonOperatorNode parent)
Set the parent BCON.
|
void |
setPointOfAttachment(int pointOfAttachment)
Set the point of attachment of this subquery.
|
void |
setSubqueryType(int subqueryType)
Set the type of this subquery.
|
void |
setWhereSubquery(boolean whereSubquery)
Mark this subquery as being part of a where clause.
|
private FromBaseTable |
singleFromBaseTable(FromList fromList)
Does the from list from the subquery contain a
single entry which is a FBT or a PRN/FBT.
|
java.lang.String |
toString()
Convert this object to a String.
|
bindExpression, checkIsBoolean, checkTopPredicatesForEqualsConditions, constantExpression, copyFields, evaluateConstantExpressions, genEqualsFalseTree, generate, generateFilter, genIsNullTree, genSQLJavaSQLTree, getClone, getColumnName, getConstantValueAsObject, getDataValueFactory, getSchemaName, getSourceResultColumn, getTableName, getTablesReferenced, getTransformed, getTypeCompiler, getTypeId, getTypeServices, init, isBinaryEqualsOperatorNode, isBooleanFalse, isBooleanTrue, isCloneable, isConstantExpression, isInListProbeNode, isParameterNode, isRelationalOperator, isSameNodeType, optimizableEqualityNode, putAndsOnTop, requiresTypeFromContext, selectivity, setCollationInfo, setCollationInfo, setCollationUsingCompilationSchema, setCollationUsingCompilationSchema, setNullability, setTransformed, setType, setType, setType, updatableByCursor, verifyChangeToCNF, verifyEliminateNots, verifyPutAndsOnTop
accept, bindOffsetFetch, bindRowMultiSet, bindUserCatalogType, bindUserType, checkReliability, checkReliability, convertDefaultNode, createTypeDependency, debugFlush, debugPrint, disablePrivilegeCollection, formatNodeString, foundString, generateAuthorizeCheck, getBeginOffset, getClassFactory, getCompilerContext, getContextManager, getCursorInfo, getDataDictionary, getDependencyManager, getEndOffset, getExecutionFactory, getGenericConstantActionFactory, getIntProperty, getLanguageConnectionContext, getNodeFactory, getNodeType, getNullNode, getParameterTypes, getSchemaDescriptor, getSchemaDescriptor, getStatementType, getTableDescriptor, getTypeCompiler, init, init, init, init, init, init, init, init, init, init, init, init, isAtomic, isInstanceOf, isPrivilegeCollectionRequired, isSessionSchema, isSessionSchema, makeConstantAction, makeTableName, makeTableName, nodeHeader, orReliability, parseStatement, printLabel, referencesSessionSchema, resolveTableToSynonym, setBeginOffset, setContextManager, setEndOffset, setNodeType, setRefActionInfo, stackPrint, treePrint, treePrint, verifyClassExist
ResultSetNode resultSet
int subqueryType
boolean underTopAndNode
boolean preprocessed
boolean distinctExpression
boolean whereSubquery
ValueNode leftOperand
boolean pushedNewPredicate
boolean havingSubquery
BinaryComparisonOperatorNode parentComparisonOperator
private BooleanConstantNode trueNode
private int subqueryNumber
private int pointOfAttachment
private boolean foundCorrelation
private boolean doneCorrelationCheck
private boolean foundVariant
private boolean doneInvariantCheck
private OrderByList orderByList
private ValueNode offset
private ValueNode fetchFirst
private boolean hasJDBClimitClause
public static final int NOTIMPLEMENTED_SUBQUERY
public static final int FROM_SUBQUERY
public static final int IN_SUBQUERY
public static final int NOT_IN_SUBQUERY
public static final int EQ_ANY_SUBQUERY
public static final int EQ_ALL_SUBQUERY
public static final int NE_ANY_SUBQUERY
public static final int NE_ALL_SUBQUERY
public static final int GT_ANY_SUBQUERY
public static final int GT_ALL_SUBQUERY
public static final int GE_ANY_SUBQUERY
public static final int GE_ALL_SUBQUERY
public static final int LT_ANY_SUBQUERY
public static final int LT_ALL_SUBQUERY
public static final int LE_ANY_SUBQUERY
public static final int LE_ALL_SUBQUERY
public static final int EXISTS_SUBQUERY
public static final int NOT_EXISTS_SUBQUERY
public static final int EXPRESSION_SUBQUERY
public void init(java.lang.Object resultSet, java.lang.Object subqueryType, java.lang.Object leftOperand, java.lang.Object orderCols, java.lang.Object offset, java.lang.Object fetchFirst, java.lang.Object hasJDBClimitClause)
init
in interface Node
init
in class QueryTreeNode
resultSet
- The ResultSetNode for the subquerysubqueryType
- The type of the subqueryleftOperand
- The left operand, if any, of the subqueryorderCols
- ORDER BY listoffset
- OFFSET n ROWSfetchFirst
- FETCH FIRST n ROWS ONLYhasJDBClimitClause
- True if the offset/fetchFirst clauses come from JDBC limit/offset escape syntaxpublic java.lang.String toString()
public void printSubNodes(int depth)
printSubNodes
in class QueryTreeNode
depth
- The depth of this node in the treepublic ResultSetNode getResultSet()
public int getSubqueryType()
public void setSubqueryType(int subqueryType)
subqueryType
- of this subquery.public void setPointOfAttachment(int pointOfAttachment) throws StandardException
pointOfAttachment
- The point of attachment of this subquery.StandardException
- Thrown on errorpublic boolean getUnderTopAndNode()
public int getPointOfAttachment()
boolean getPreprocessed()
void setParentComparisonOperator(BinaryComparisonOperatorNode parent)
parent
- The parent BCON.public ValueNode remapColumnReferencesToExpressions() throws StandardException
remapColumnReferencesToExpressions
in class ValueNode
StandardException
- Thrown on errorpublic ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, java.util.List aggregateVector) throws StandardException
bindExpression
in class ValueNode
fromList
- The FROM list for the query this
expression is in, for binding columns.
NOTE: fromList will be null if the subquery appears
in a VALUES clause.subqueryList
- The subquery list being built as we find SubqueryNodesaggregateVector
- The aggregate vector being built as we find AggregateNodesStandardException
- Thrown on errorpublic ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException
preprocess
in class ValueNode
numTables
- Number of tables in the DML StatementouterFromList
- FromList from outer query blockouterSubqueryList
- SubqueryList from outer query blockouterPredicateList
- PredicateList from outer query blockStandardException
- Thrown on errorprivate FromBaseTable singleFromBaseTable(FromList fromList)
fromList
- The from list from the subqueryFromBaseTable
if the from list from the subquery
contains a single entry which is a FBT or a PRN/FBT, or null
if the subquery does not contain a single FBTprivate boolean rightOperandFlattenableToNotExists(int numTables, FromBaseTable fbt) throws StandardException
Check if the right operand is on a form that makes it possible to flatten this query to a NOT EXISTS join. We don't allow flattening if the right operand doesn't reference the base table of the subquery. (Requirement added as part of DERBY-4001.)
The problem with the right operand not referencing the base table of the subquery, is that the join condition may then be used to filter rows from the right side (outer) table in the NOT EXISTS join. In a NOT EXISTS join, the join condition can only safely be applied to the left side (inner) table of the join. Otherwise, it will filter out all the interesting rows too early.
Take the query below as an example:
SELECT * FROM T1 WHERE X NOT IN (SELECT 1 FROM T2)
Here, the right operand is 1, and the join condition is T1.X=1
.
If flattened, the join condition will be used directly on the outer
table, and hide all rows with X<>1
, although those are the only
rows we're interested in. If the join condition had only been used on
the inner table, the NOT EXISTS join logic would do the correct thing.
If the join condition references the inner table, the condition cannot be used directly on the outer table, so it is safe to flatten the query.
numTables
- the number of tables in this statementfbt
- the only FromBaseTable
in this subquerytrue
if it is OK to flatten this query to a NOT EXISTS
join, false
otherwiseStandardException
private boolean canAllBeFlattened() throws StandardException
StandardException
private ValueNode flattenToNormalJoin(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException
numTables
- Number of tables in the DML StatementouterFromList
- FromList from outer query blockouterSubqueryList
- SubqueryList from outer query blockouterPredicateList
- PredicateList from outer query blockStandardException
- Thrown on errorprivate ValueNode flattenToExistsJoin(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList, boolean flattenableNotExists) throws StandardException
numTables
- Number of tables in the DML StatementouterFromList
- FromList from outer query blockouterSubqueryList
- SubqueryList from outer query blockouterPredicateList
- PredicateList from outer query blockflattenableNotExists
- Is it a flattening into a NOT EXISTS joinStandardException
- Thrown on errorprivate ValueNode getRightOperand()
private boolean isInvariant() throws StandardException
StandardException
- Thrown on errorpublic boolean hasCorrelatedCRs() throws StandardException
StandardException
- Thrown on errorprivate UnaryComparisonOperatorNode pushNewPredicate(int numTables) throws StandardException
numTables
- Number of tables in DML StatementStandardException
- Thrown on errorprivate BinaryComparisonOperatorNode getNewJoinCondition(ValueNode leftOperand, ValueNode rightOperand) throws StandardException
leftOperand
- The left operand for the new condition.rightOperand
- The right operand for the new condition.StandardException
- Thrown on errorValueNode eliminateNots(boolean underNotNode) throws StandardException
eliminateNots
in class ValueNode
underNotNode
- Whether or not we are under a NotNode.StandardException
- Thrown on errorpublic ValueNode changeToCNF(boolean underTopAndNode) throws StandardException
changeToCNF
in class ValueNode
underTopAndNode
- Whether or not we are under a top level AndNode.StandardException
- Thrown on errorpublic boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) throws StandardException
categorize
in class ValueNode
referencedTabs
- JBitSet with bit map of referenced FromTablessimplePredsOnly
- Whether or not to consider method
calls, field references and conditional nodes
when building bit mapStandardException
- Thrown on errorboolean isMaterializable() throws StandardException
StandardException
public void optimize(DataDictionary dataDictionary, double outerRows) throws StandardException
dataDictionary
- The DataDictionary to use for optimizationouterRows
- The optimizer's estimate of the number of
times this subquery will be executed.StandardException
- Thrown on errorpublic void modifyAccessPaths() throws StandardException
StandardException
- Thrown on errorprotected int getOrderableVariantType() throws StandardException
getOrderableVariantType
in class ValueNode
StandardException
- Thrown on errorpublic void generateExpression(ExpressionClassBuilder expressionBuilder, MethodBuilder mbex) throws StandardException
generateExpression
in class ValueNode
expressionBuilder
- The ExpressionClassBuilder for the class being builtmbex
- The method the expression will go intoStandardException
- Thrown on errorprivate LocalField generateMaterialization(ActivationClassBuilder acb, MethodBuilder mbsq, java.lang.String type)
private BooleanConstantNode getTrueNode() throws StandardException
StandardException
void acceptChildren(Visitor v) throws StandardException
acceptChildren
in class QueryTreeNode
v
- the visitorStandardException
- on errorprivate boolean isIN()
private boolean isNOT_IN()
private boolean isANY()
private boolean isALL()
private boolean isEXISTS()
private boolean isNOT_EXISTS()
private void changeToCorrespondingExpressionType() throws StandardException
StandardException
- Thrown on errorprivate void setDataTypeServices(ResultColumnList resultColumns) throws StandardException
StandardException
protected boolean isEquivalent(ValueNode o)
This method provides basic expression matching facility for the derived class of ValueNode and it is used by the language layer to compare the node structural form of the two expressions for equivalence at bind phase.
Note that it is not comparing the actual row values at runtime to produce a result; hence, when comparing SQL NULLs, they are considered to be equivalent and not unknown.
One usage case of this method in this context is to compare the select column expression against the group by expression to check if they are equivalent. e.g.:
SELECT c1+c2 FROM t1 GROUP BY c1+c2
In general, node equivalence is determined by the derived class of ValueNode. But they generally abide to the rules below:
isEquivalent
in class ValueNode
o
- the node to compare this ValueNode against.true
if the two nodes are equivalent,
false
otherwise.public boolean isHavingSubquery()
public void setHavingSubquery(boolean havingSubquery)
havingSubquery
- public boolean isWhereSubquery()
public void setWhereSubquery(boolean whereSubquery)
whereSubquery
- public boolean isWhereExistsAnyInWithWhereSubquery() throws StandardException
StandardException
public OrderByList getOrderByList()
public ValueNode getOffset()
public ValueNode getFetchFirst()
public boolean hasJDBClimitClause()
Apache Derby V10.10 Internals - Copyright © 2004,2013 The Apache Software Foundation. All Rights Reserved.