Class SubqueryNode
- java.lang.Object
-
- org.apache.derby.impl.sql.compile.QueryTreeNode
-
- org.apache.derby.impl.sql.compile.ValueNode
-
- org.apache.derby.impl.sql.compile.SubqueryNode
-
- All Implemented Interfaces:
Visitable
class SubqueryNode extends ValueNode
A SubqueryNode represents a subquery. Subqueries return values to their outer queries. An quantified subquery is one that appears under a quantified operator (like IN or EXISTS) - quantified subqueries can return more than one value per invocation. An expression subquery is one that is not directly under a quantified operator - expression subqueries are allowed to return at most one value per invocation (returning no value is considered to be equivalent to returning NULL). There are a large number of subquery types. Because of the large number of types, and the large amount of shared code, we have decided to have 1 SubqueryNode without any subclasses. The subquery type (and operator) is encoded in the subqueryType field. The query optimizer is responsible for optimizing subqueries, and also for transforming them so that code can be generated for them. The optimizer may eliminate some subqueries by transforming them into joins, or it may change the internal form of a subquery (for example, transforming 'where x in (select y from z where ...)' into 'where (select true from z where x = y and ...)'). Note that aggregates present some additional issues. A transformation such as:- where x in (SELECT expression FROM z)
- where x = (SELECT true FROM (SELECT MAX(x) FROM z) WHERE SQLCOL1 = y)
-
-
Field Summary
Fields Modifier and Type Field Description (package private) booleandistinctExpressionprivate booleandoneCorrelationCheckprivate booleandoneInvariantCheck(package private) static intEQ_ALL_SUBQUERY(package private) static intEQ_ANY_SUBQUERY(package private) static intEXISTS_SUBQUERY(package private) static intEXPRESSION_SUBQUERYprivate ValueNodefetchFirstprivate booleanfoundCorrelationprivate booleanfoundVariant(package private) static intFROM_SUBQUERY(package private) static intGE_ALL_SUBQUERY(package private) static intGE_ANY_SUBQUERY(package private) static intGT_ALL_SUBQUERY(package private) static intGT_ANY_SUBQUERYprivate booleanhasJDBClimitClause(package private) booleanhavingSubqueryis this subquery part of a having clause.(package private) static intIN_SUBQUERY(package private) static intLE_ALL_SUBQUERY(package private) static intLE_ANY_SUBQUERY(package private) ValueNodeleftOperand(package private) static intLT_ALL_SUBQUERY(package private) static intLT_ANY_SUBQUERY(package private) static intNE_ALL_SUBQUERY(package private) static intNE_ANY_SUBQUERY(package private) static intNOT_EXISTS_SUBQUERY(package private) static intNOT_IN_SUBQUERY(package private) static intNOTIMPLEMENTED_SUBQUERYprivate ValueNodeoffsetprivate OrderByListorderByList(package private) BinaryComparisonOperatorNodeparentComparisonOperatorprivate intpointOfAttachment(package private) booleanpreprocessed(package private) booleanpushedNewPredicate(package private) ResultSetNoderesultSetprivate intsubqueryNumber(package private) intsubqueryTypeprivate BooleanConstantNodetrueNode(package private) booleanunderTopAndNode(package private) booleanwhereSubquery-
Fields inherited from class org.apache.derby.impl.sql.compile.ValueNode
transformed
-
Fields inherited from class org.apache.derby.impl.sql.compile.QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_CYCLE, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX
-
-
Constructor Summary
Constructors Constructor Description SubqueryNode(ResultSetNode resultSet, int subqueryType, ValueNode leftOperand, OrderByList orderCols, ValueNode offset, ValueNode fetchFirst, boolean hasJDBClimitClause, ContextManager cm)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidacceptChildren(Visitor v)Accept the visitor for all visitable children of this node.(package private) ValueNodebindExpression(FromList fromList, SubqueryList subqueryList, java.util.List<AggregateNode> aggregates)Bind this expression.private booleancanAllBeFlattened()Can NOT IN, ALL be falttened to NOT EXISTS join?(package private) booleancategorize(JBitSet referencedTabs, boolean simplePredsOnly)Categorize this predicate.(package private) ValueNodechangeToCNF(boolean underTopAndNode)Finish putting an expression into conjunctive normal form.private voidchangeToCorrespondingExpressionType()Convert this IN/ANY subquery, which is known to return at most 1 row, to an equivalent expression subquery.(package private) ValueNodeeliminateNots(boolean underNotNode)Eliminate NotNodes in the current query block.private ValueNodeflattenToExistsJoin(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList, boolean flattenableNotExists)Flatten this subquery into the outer query block as an exists join.private ValueNodeflattenToNormalJoin(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList)Flatten this subquery into the outer query block.(package private) voidgenerateExpression(ExpressionClassBuilder expressionBuilder, MethodBuilder mbex)Do code generation for this subquery.private LocalFieldgenerateMaterialization(ActivationClassBuilder acb, MethodBuilder mbsq, java.lang.String type)ValueNodegetFetchFirst()Get FETCH FIRST (used to construct FROM_SUBQUERY only), cf.private BinaryComparisonOperatorNodegetNewJoinCondition(ValueNode leftOperand, ValueNode rightOperand)Build a new join condition between the leftOperand and the rightOperand.ValueNodegetOffset()Get OFFSET (used to construct FROM_SUBQUERY only), cf.protected intgetOrderableVariantType()Return the variant type for the underlying expression.OrderByListgetOrderByList()Get ORDER BY list (used to construct FROM_SUBQUERY only), cf.(package private) intgetPointOfAttachment()Get the ResultSet # for the point of attachment for this SubqueryNode.(package private) booleangetPreprocessed()Get whether or not this SubqueryNode has already been preprocessed.(package private) ResultSetNodegetResultSet()Return the resultSet for this SubqueryNode.private ValueNodegetRightOperand()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.(package private) intgetSubqueryType()Return the type of this subquery.private BooleanConstantNodegetTrueNode()(package private) booleangetUnderTopAndNode()Return whether or not this subquery is immediately under a top level AndNode.(package private) booleanhasCorrelatedCRs()Check to see if this subquery has correlated column references.booleanhasJDBClimitClause()Return true if the offset/fetchFirst clauses were added by JDBC LIMIT escape syntax.private booleanisALL()private booleanisANY()(package private) booleanisEquivalent(ValueNode o)Tests if this node is equivalent to the specified ValueNode.private booleanisEXISTS()booleanisHavingSubquery()Is this subquery part of a having clause?private booleanisIN()private booleanisInvariant()Check to see if we have a Variant value below us.booleanisMaterializable()private booleanisNOT_EXISTS()private booleanisNOT_IN()(package private) booleanisWhereExistsAnyInWithWhereSubquery()Check whether this is a WHERE EXISTS | ANY | IN subquery with a subquery in its own WHERE clause.(package private) booleanisWhereSubquery()Is this subquery part of a whereclause?(package private) voidmodifyAccessPaths()Make any changes to the access paths, as decided by the optimizer.(package private) voidoptimize(DataDictionary dataDictionary, double outerRows)Optimize this SubqueryNode.(package private) ValueNodepreprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList)Preprocess an expression tree.(package private) voidprintSubNodes(int depth)Prints the sub-nodes of this object.private UnaryComparisonOperatorNodepushNewPredicate(int numTables)Transform: expression QuantifiedOperator (select x from ...)booleanreferencesSessionSchema()Return true if the node references SESSION schema tables (temporary or permanent)(package private) ValueNoderemapColumnReferencesToExpressions()Remap all ColumnReferences in this tree to be clones of the underlying expression.private booleanrightOperandFlattenableToNotExists(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 voidsetDataTypeServices(ResultColumnList resultColumns)voidsetHavingSubquery(boolean havingSubquery)Mark this subquery as being part of a having clause.(package private) voidsetParentComparisonOperator(BinaryComparisonOperatorNode parent)Set the parent BCON.(package private) voidsetPointOfAttachment(int pointOfAttachment)Set the point of attachment of this subquery.(package private) voidsetSubqueryType(int subqueryType)Set the type of this subquery.(package private) voidsetWhereSubquery(boolean whereSubquery)Mark this subquery as being part of a where clause.private FromBaseTablesingleFromBaseTable(FromList fromList)Does the from list from the subquery contain a single entry which is a FBT or a PRN/FBT.java.lang.StringtoString()Convert this object to a String.-
Methods inherited from class org.apache.derby.impl.sql.compile.ValueNode
bindExpression, checkIsBoolean, checkTopPredicatesForEqualsConditions, constantExpression, copyFields, evaluateConstantExpressions, genEqualsFalseTree, generate, genIsNullTree, genSQLJavaSQLTree, getClone, getColumnName, getConstantValueAsObject, getDataValueFactory, getSchemaName, getSourceResultColumn, getTableName, getTablesReferenced, getTransformed, getTypeCompiler, getTypeId, getTypeServices, isBinaryEqualsOperatorNode, isBooleanFalse, isBooleanTrue, isCloneable, isConstantExpression, isInListProbeNode, isParameterNode, isRelationalOperator, isSameNodeKind, optimizableEqualityNode, putAndsOnTop, requiresTypeFromContext, selectivity, setCollationInfo, setCollationInfo, setCollationUsingCompilationSchema, setCollationUsingCompilationSchema, setNullability, setTransformed, setType, setType, setType, updatableByCursor, verifyChangeToCNF, verifyEliminateNots, verifyPutAndsOnTop
-
Methods inherited from class org.apache.derby.impl.sql.compile.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, isAtomic, isPrivilegeCollectionRequired, isSessionSchema, isSessionSchema, makeConstantAction, makeTableName, makeTableName, nodeHeader, optimizerTracingIsOn, orReliability, parseSearchCondition, parseStatement, printLabel, resolveTableToSynonym, setBeginOffset, setEndOffset, setRefActionInfo, stackPrint, taggedWith, treePrint, treePrint, verifyClassExist
-
-
-
-
Field Detail
-
resultSet
ResultSetNode resultSet
-
subqueryType
int subqueryType
-
underTopAndNode
boolean underTopAndNode
-
preprocessed
boolean preprocessed
-
distinctExpression
boolean distinctExpression
-
whereSubquery
boolean whereSubquery
-
leftOperand
ValueNode leftOperand
-
pushedNewPredicate
boolean pushedNewPredicate
-
havingSubquery
boolean havingSubquery
is this subquery part of a having clause. We need to know this so we can avoid flattening.
-
parentComparisonOperator
BinaryComparisonOperatorNode parentComparisonOperator
-
trueNode
private BooleanConstantNode trueNode
-
subqueryNumber
private int subqueryNumber
-
pointOfAttachment
private int pointOfAttachment
-
foundCorrelation
private boolean foundCorrelation
-
doneCorrelationCheck
private boolean doneCorrelationCheck
-
foundVariant
private boolean foundVariant
-
doneInvariantCheck
private boolean doneInvariantCheck
-
orderByList
private OrderByList orderByList
-
offset
private ValueNode offset
-
fetchFirst
private ValueNode fetchFirst
-
hasJDBClimitClause
private boolean hasJDBClimitClause
-
NOTIMPLEMENTED_SUBQUERY
static final int NOTIMPLEMENTED_SUBQUERY
- See Also:
- Constant Field Values
-
FROM_SUBQUERY
static final int FROM_SUBQUERY
- See Also:
- Constant Field Values
-
IN_SUBQUERY
static final int IN_SUBQUERY
- See Also:
- Constant Field Values
-
NOT_IN_SUBQUERY
static final int NOT_IN_SUBQUERY
- See Also:
- Constant Field Values
-
EQ_ANY_SUBQUERY
static final int EQ_ANY_SUBQUERY
- See Also:
- Constant Field Values
-
EQ_ALL_SUBQUERY
static final int EQ_ALL_SUBQUERY
- See Also:
- Constant Field Values
-
NE_ANY_SUBQUERY
static final int NE_ANY_SUBQUERY
- See Also:
- Constant Field Values
-
NE_ALL_SUBQUERY
static final int NE_ALL_SUBQUERY
- See Also:
- Constant Field Values
-
GT_ANY_SUBQUERY
static final int GT_ANY_SUBQUERY
- See Also:
- Constant Field Values
-
GT_ALL_SUBQUERY
static final int GT_ALL_SUBQUERY
- See Also:
- Constant Field Values
-
GE_ANY_SUBQUERY
static final int GE_ANY_SUBQUERY
- See Also:
- Constant Field Values
-
GE_ALL_SUBQUERY
static final int GE_ALL_SUBQUERY
- See Also:
- Constant Field Values
-
LT_ANY_SUBQUERY
static final int LT_ANY_SUBQUERY
- See Also:
- Constant Field Values
-
LT_ALL_SUBQUERY
static final int LT_ALL_SUBQUERY
- See Also:
- Constant Field Values
-
LE_ANY_SUBQUERY
static final int LE_ANY_SUBQUERY
- See Also:
- Constant Field Values
-
LE_ALL_SUBQUERY
static final int LE_ALL_SUBQUERY
- See Also:
- Constant Field Values
-
EXISTS_SUBQUERY
static final int EXISTS_SUBQUERY
- See Also:
- Constant Field Values
-
NOT_EXISTS_SUBQUERY
static final int NOT_EXISTS_SUBQUERY
- See Also:
- Constant Field Values
-
EXPRESSION_SUBQUERY
static final int EXPRESSION_SUBQUERY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SubqueryNode
SubqueryNode(ResultSetNode resultSet, int subqueryType, ValueNode leftOperand, OrderByList orderCols, ValueNode offset, ValueNode fetchFirst, boolean hasJDBClimitClause, ContextManager cm)
Constructor.- Parameters:
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 syntaxcm- Context Manager
-
-
Method Detail
-
toString
public java.lang.String toString()
Convert this object to a String. See comments in QueryTreeNode.java for how this should be done for tree printing.
-
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 classQueryTreeNode- Parameters:
depth- The depth of this node in the tree
-
getResultSet
ResultSetNode getResultSet()
Return the resultSet for this SubqueryNode.- Returns:
- ResultSetNode underlying this SubqueryNode.
-
getSubqueryType
int getSubqueryType()
Return the type of this subquery.- Returns:
- int Type of this subquery.
-
setSubqueryType
void setSubqueryType(int subqueryType)
Set the type of this subquery.- Parameters:
subqueryType- of this subquery.
-
setPointOfAttachment
void setPointOfAttachment(int pointOfAttachment) throws StandardExceptionSet the point of attachment of this subquery.- Parameters:
pointOfAttachment- The point of attachment of this subquery.- Throws:
StandardException- Thrown on error
-
getUnderTopAndNode
boolean getUnderTopAndNode()
Return whether or not this subquery is immediately under a top level AndNode.- Returns:
- boolean Whether or not this subquery is immediately under a top level AndNode.
-
getPointOfAttachment
int getPointOfAttachment()
Get the ResultSet # for the point of attachment for this SubqueryNode.- Returns:
- int The ResultSet # for the point of attachment
-
getPreprocessed
boolean getPreprocessed()
Get whether or not this SubqueryNode has already been preprocessed.- Returns:
- Whether or not this SubqueryNode has already been preprocessed.
-
setParentComparisonOperator
void setParentComparisonOperator(BinaryComparisonOperatorNode parent)
Set the parent BCON. Useful when considering flattening expression subqueries.- Parameters:
parent- The parent BCON.
-
referencesSessionSchema
public boolean referencesSessionSchema() throws StandardExceptionDescription 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
-
remapColumnReferencesToExpressions
ValueNode remapColumnReferencesToExpressions() throws StandardException
Remap all ColumnReferences in this tree to be clones of the underlying expression.- Overrides:
remapColumnReferencesToExpressionsin classValueNode- Returns:
- ValueNode The remapped expression tree.
- Throws:
StandardException- Thrown on error
-
bindExpression
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, java.util.List<AggregateNode> aggregates) throws StandardException
Bind this expression. This means binding the sub-expressions, as well as figuring out what the return type is for this expression.- Overrides:
bindExpressionin classValueNode- Parameters:
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 SubqueryNodesaggregates- The aggregate list being built as we find AggregateNodes- Returns:
- The new top of the expression tree.
- Throws:
StandardException- Thrown on error
-
preprocess
ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException
Preprocess an expression tree. We do a number of transformations here (including subqueries, IN lists, LIKE and BETWEEN) plus subquery flattening. NOTE: This is done before the outer ResultSetNode is preprocessed.- Overrides:
preprocessin classValueNode- Parameters:
numTables- Number of tables in the DML StatementouterFromList- FromList from outer query blockouterSubqueryList- SubqueryList from outer query blockouterPredicateList- PredicateList from outer query block- Returns:
- The modified expression
- Throws:
StandardException- Thrown on error
-
singleFromBaseTable
private FromBaseTable singleFromBaseTable(FromList fromList)
Does the from list from the subquery contain a single entry which is a FBT or a PRN/FBT.- Parameters:
fromList- The from list from the subquery- Returns:
- the
FromBaseTableif the from list from the subquery contains a single entry which is a FBT or a PRN/FBT, ornullif the subquery does not contain a single FBT
-
rightOperandFlattenableToNotExists
private boolean rightOperandFlattenableToNotExists(int numTables, FromBaseTable fbt) throws StandardExceptionCheck 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 withX<>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.
- Parameters:
numTables- the number of tables in this statementfbt- the onlyFromBaseTablein this subquery- Returns:
trueif it is OK to flatten this query to a NOT EXISTS join,falseotherwise- Throws:
StandardException
-
canAllBeFlattened
private boolean canAllBeFlattened() throws StandardExceptionCan NOT IN, ALL be falttened to NOT EXISTS join? We can't or the flattening doesn't easily make sense if either side of the comparison is nullable. (beetle 5173)- Returns:
- Whether or not the NOT IN or ALL subquery can be flattened.
- Throws:
StandardException
-
flattenToNormalJoin
private ValueNode flattenToNormalJoin(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException
Flatten this subquery into the outer query block. At this point we are only flattening based on a uniqueness condition and only flattening non-aggregate subqueries. So, we promote the subquery's from list, as is, into the outer from list. For EXISTS subquerys, we return a TRUE. Otherwise we return a new comparison between the leftOperand and the expression in the subquery's SELECT list. RESOLVE - we will need to modify this logic to account for exists joins and aggregates as we support flattening for them. Anyway, here's what we do: o We remove ourself from the outer subquery list. o We decrement the nesting level for all tables in the subquery tree. o We append the subquery's from list to the outer from list. o We add the subquery's predicate list to the outer predicate list. (The subquery has already been preprocessed.) o We add the subquery's subquery list to the outer subquery list. o For EXISTS, we return a true. o Otherwise, we return a new comparison between the leftOperand and the expression in the inner select's RCL.- Parameters:
numTables- Number of tables in the DML StatementouterFromList- FromList from outer query blockouterSubqueryList- SubqueryList from outer query blockouterPredicateList- PredicateList from outer query block- Returns:
- The modified expression
- Throws:
StandardException- Thrown on error
-
flattenToExistsJoin
private ValueNode flattenToExistsJoin(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList, boolean flattenableNotExists) throws StandardException
Flatten this subquery into the outer query block as an exists join. At this point we are only flattening non-aggregate subqueries with a single FBT in the from list. So, we transform all FBTs in the from list into ExistBaseTables, update the dependency lists for each of the tables and then flatten the subquery. RESOLVE - we will need to modify this logic to account for aggregates as we support flattening for them.- Parameters:
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 join- Returns:
- The modified expression
- Throws:
StandardException- Thrown on error
-
getRightOperand
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.- Returns:
- the right operand
-
isInvariant
private boolean isInvariant() throws StandardExceptionCheck to see if we have a Variant value below us. If so, return true. Caches the result so multiple calls are ok.- Returns:
- boolean whether we have
- Throws:
StandardException- Thrown on error
-
hasCorrelatedCRs
boolean hasCorrelatedCRs() throws StandardExceptionCheck to see if this subquery has correlated column references. Only useful results if called AFTER binding (after CRs have been bound).- Returns:
- whether the subquery has correlated column references.
- Throws:
StandardException- Thrown on error
-
pushNewPredicate
private UnaryComparisonOperatorNode pushNewPredicate(int numTables) throws StandardException
Transform: expression QuantifiedOperator (select x from ...) into (select true from .. where expressionx ...) IS [NOT] NULL or, if we have an aggregate: (select true from (select AGG(x) from ...) where expression x ...) IS [NOT] NULL For ANY and IN subqueries: o We generate an IS NULL above the SubqueryNode and return the top of the new tree to the caller. o The operator in the new predicate that is added to the subquery will correspond to the operator that modifies the ANY. (eg, = for = ANY, with = for IN.) For ALL and NOT IN subqueries: o We generate an IS NOT NULL above the SubqueryNode and return the top of the new tree to the caller. o The operator in the new predicate that is added to the subquery will be a BinaryAllOperatorNode whose bcoNodeType corresponds to the negation of the operator that modifies the ALL. (eg, <> for = ALL, with <> for NOT IN.) NOTE: This method is called after the underlying subquery has been preprocessed, so we build a new Predicate, not just a new expression. - Parameters:
numTables- Number of tables in DML Statement- Returns:
- UnaryComparisonOperatorNode An IS [NOT] NULL above the transformed subquery.
- Throws:
StandardException- Thrown on error
-
getNewJoinCondition
private BinaryComparisonOperatorNode getNewJoinCondition(ValueNode leftOperand, ValueNode rightOperand) throws StandardException
Build a new join condition between the leftOperand and the rightOperand. The comparison operator is dependent on the subquery type.- Parameters:
leftOperand- The left operand for the new condition.rightOperand- The right operand for the new condition.- Throws:
StandardException- Thrown on error
-
eliminateNots
ValueNode eliminateNots(boolean underNotNode) throws StandardException
Eliminate NotNodes in the current query block. We traverse the tree, inverting ANDs and ORs and eliminating NOTs as we go. We stop at ComparisonOperators and boolean expressions. We invert ComparisonOperators and replace boolean expressions with boolean expression = false. NOTE: Since we do not recurse under ComparisonOperators, there still could be NotNodes left in the tree.- Overrides:
eliminateNotsin classValueNode- Parameters:
underNotNode- Whether or not we are under a NotNode.- Returns:
- The modified expression
- Throws:
StandardException- Thrown on error
-
changeToCNF
ValueNode changeToCNF(boolean underTopAndNode) throws StandardException
Finish putting an expression into conjunctive normal form. An expression tree in conjunctive normal form meets the following criteria: o If the expression tree is not null, the top level will be a chain of AndNodes terminating in a true BooleanConstantNode. o The left child of an AndNode will never be an AndNode. o Any right-linked chain that includes an AndNode will be entirely composed of AndNodes terminated by a true BooleanConstantNode. o The left child of an OrNode will never be an OrNode. o Any right-linked chain that includes an OrNode will be entirely composed of OrNodes terminated by a false BooleanConstantNode. o ValueNodes other than AndNodes and OrNodes are considered leaf nodes for purposes of expression normalization. In other words, we won't do any normalization under those nodes. In addition, we track whether or not we are under a top level AndNode. SubqueryNodes need to know this for subquery flattening.- Overrides:
changeToCNFin classValueNode- Parameters:
underTopAndNode- Whether or not we are under a top level AndNode.- Returns:
- The modified expression
- Throws:
StandardException- Thrown on error
-
categorize
boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) throws StandardException
Categorize this predicate. Initially, this means building a bit map of the referenced tables for each predicate. If the source of this ColumnReference (at the next underlying level) is not a ColumnReference or a VirtualColumnNode then this predicate will not be pushed down. For example, in: select * from (select 1 from s) a (x) where x = 1 we will not push down x = 1. NOTE: It would be easy to handle the case of a constant, but if the inner SELECT returns an arbitrary expression, then we would have to copy that tree into the pushed predicate, and that tree could contain subqueries and method calls. RESOLVE - revisit this issue once we have views.- Overrides:
categorizein classValueNode- Parameters:
referencedTabs- JBitSet with bit map of referenced FromTablessimplePredsOnly- Whether or not to consider method calls, field references and conditional nodes when building bit map- Returns:
- boolean Whether or not source.expression is a ColumnReference or a VirtualColumnNode.
- Throws:
StandardException- Thrown on error
-
isMaterializable
public boolean isMaterializable() throws StandardException- Throws:
StandardException
-
optimize
void optimize(DataDictionary dataDictionary, double outerRows) throws StandardException
Optimize this SubqueryNode.- Parameters:
dataDictionary- The DataDictionary to use for optimizationouterRows- The optimizer's estimate of the number of times this subquery will be executed.- Throws:
StandardException- Thrown on error
-
modifyAccessPaths
void modifyAccessPaths() throws StandardExceptionMake any changes to the access paths, as decided by the optimizer.- Throws:
StandardException- Thrown on error
-
getOrderableVariantType
protected int getOrderableVariantType() throws StandardExceptionReturn the variant type for the underlying expression. The variant type can be: VARIANT - variant within a scan (method calls and non-static field access) SCAN_INVARIANT - invariant within a scan (column references from outer tables) QUERY_INVARIANT - invariant within the life of a query (constant expressions)- Overrides:
getOrderableVariantTypein classValueNode- Returns:
- The variant type for the underlying expression.
- Throws:
StandardException- Thrown on error
-
generateExpression
void generateExpression(ExpressionClassBuilder expressionBuilder, MethodBuilder mbex) throws StandardException
Do code generation for this subquery.- Overrides:
generateExpressionin classValueNode- Parameters:
expressionBuilder- The ExpressionClassBuilder for the class being builtmbex- The method the expression will go into- Throws:
StandardException- Thrown on error
-
generateMaterialization
private LocalField generateMaterialization(ActivationClassBuilder acb, MethodBuilder mbsq, java.lang.String type)
-
getTrueNode
private BooleanConstantNode getTrueNode() throws StandardException
- Throws:
StandardException
-
acceptChildren
void acceptChildren(Visitor v) throws StandardException
Accept the visitor for all visitable children of this node.- Overrides:
acceptChildrenin classQueryTreeNode- Parameters:
v- the visitor- Throws:
StandardException- on error
-
isIN
private boolean isIN()
-
isNOT_IN
private boolean isNOT_IN()
-
isANY
private boolean isANY()
-
isALL
private boolean isALL()
-
isEXISTS
private boolean isEXISTS()
-
isNOT_EXISTS
private boolean isNOT_EXISTS()
-
changeToCorrespondingExpressionType
private void changeToCorrespondingExpressionType() throws StandardExceptionConvert this IN/ANY subquery, which is known to return at most 1 row, to an equivalent expression subquery.- Throws:
StandardException- Thrown on error
-
setDataTypeServices
private void setDataTypeServices(ResultColumnList resultColumns) throws StandardException
- Throws:
StandardException
-
isEquivalent
boolean isEquivalent(ValueNode o)
Tests if this node is equivalent to the specified ValueNode. Two ValueNodes are considered equivalent if they will evaluate to the same value during query execution.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:
- The two ValueNodes must be of the same node type to be considered equivalent. e.g.: CastNode vs. CastNode - equivalent (if their args also match), ColumnReference vs CastNode - not equivalent.
- If node P contains other ValueNode(s) and so on, those node(s) must also be of the same node type to be considered equivalent.
- If node P takes a parameter list, then the number of arguments and its arguments for the two nodes must also match to be considered equivalent. e.g.: CAST(c1 as INTEGER) vs CAST(c1 as SMALLINT), they are not equivalent.
- When comparing SQL NULLs in this context, they are considered to be equivalent.
- If this does not apply or it is determined that the two nodes are not equivalent then the derived class of this method should return false; otherwise, return true.
- Specified by:
isEquivalentin classValueNode- Parameters:
o- the node to compare this ValueNode against.- Returns:
trueif the two nodes are equivalent,falseotherwise.
-
isHavingSubquery
public boolean isHavingSubquery()
Is this subquery part of a having clause?- Returns:
- true if it is part of a having clause, otherwise false
-
setHavingSubquery
public void setHavingSubquery(boolean havingSubquery)
Mark this subquery as being part of a having clause.- Parameters:
havingSubquery-
-
isWhereSubquery
boolean isWhereSubquery()
Is this subquery part of a whereclause?- Returns:
- true if it is part of a where clause, otherwise false
-
setWhereSubquery
void setWhereSubquery(boolean whereSubquery)
Mark this subquery as being part of a where clause.- Parameters:
whereSubquery-
-
isWhereExistsAnyInWithWhereSubquery
boolean isWhereExistsAnyInWithWhereSubquery() throws StandardExceptionCheck whether this is a WHERE EXISTS | ANY | IN subquery with a subquery in its own WHERE clause. Used in flattening decision making. DERBY-3301 reported wrong results from a nested WHERE EXISTS, but according to the derby optimizer docs this applies to a broader range of WHERE clauses in a WHERE EXISTS subquery. No WHERE EXISTS subquery with anohter subquery in it own WHERE clause can be flattened.- Returns:
- true if this subquery is a WHERE EXISTS | ANY | IN subquery with a subquery in its own WHERE clause
- Throws:
StandardException
-
getOrderByList
public OrderByList getOrderByList()
Get ORDER BY list (used to construct FROM_SUBQUERY only), cf. FromSubquery, for which this node is transient.- Returns:
- order by list if specified, else null.
-
getOffset
public ValueNode getOffset()
Get OFFSET (used to construct FROM_SUBQUERY only), cf. FromSubquery, for which this node is transient.- Returns:
- offset if specified, else null.
-
getFetchFirst
public ValueNode getFetchFirst()
Get FETCH FIRST (used to construct FROM_SUBQUERY only), cf. FromSubquery, for which this node is transient.- Returns:
- fetch first if specified, else null.
-
hasJDBClimitClause
public boolean hasJDBClimitClause()
Return true if the offset/fetchFirst clauses were added by JDBC LIMIT escape syntax. This method is used to construct a FROM_SUBQUERY only, cf. FromSubquery, for which this node is transient.- Returns:
- true if the JDBC limit/offset semantics (rather than the SQL Standard OFFSET/FETCH NEXT) semantics apply
-
-