Top Description Fields Constructors Methods
org.apache.derby.impl.sql.compile

pack-priv Class SubqueryNode

extends ValueNode
Class Inheritance
Imports
java.lang.reflect.Modifier, java.util.List, org.apache.derby.shared.common.error.StandardException, org.apache.derby.shared.common.reference.ClassName, .SQLState, org.apache.derby.iapi.services.classfile.VMOpcode, org.apache.derby.iapi.services.compiler.LocalField, .MethodBuilder, org.apache.derby.iapi.services.context.ContextManager, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.iapi.sql.compile.CompilerContext, .CostEstimate, .Visitor, org.apache.derby.iapi.sql.conn.Authorizer, org.apache.derby.iapi.sql.dictionary.DataDictionary, org.apache.derby.iapi.store.access.Qualifier, org.apache.derby.iapi.types.DataTypeDescriptor, org.apache.derby.iapi.util.JBitSet, org.apache.derby.impl.sql.execute.OnceResultSet

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: has to be treated specially if expression has an aggregate. We change it to:

Field Summary

Modifier and TypeField and Description
pack-priv boolean
private boolean
private boolean
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
private ValueNode
private boolean
private boolean
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
private boolean
pack-priv boolean
havingSubquery

is this subquery part of a having clause.

pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv ValueNode
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
pack-priv static final int
private ValueNode
private OrderByList
pack-priv BinaryComparisonOperatorNode
private int
pack-priv boolean
pack-priv boolean
pack-priv ResultSetNode
private int
pack-priv int
private BooleanConstantNode
pack-priv boolean
pack-priv boolean
Inherited from org.apache.derby.impl.sql.compile.ValueNode:
transformed

Constructor Summary

AccessConstructor and Description
pack-priv
SubqueryNode(ResultSetNode
The ResultSetNode for the subquery
resultSet
,
int
The type of the subquery
subqueryType
,
ValueNode
The left operand, if any, of the subquery
leftOperand
,
OrderByList
ORDER BY list
orderCols
,
ValueNode
OFFSET n ROWS
offset
,
ValueNode
FETCH FIRST n ROWS ONLY
fetchFirst
,
boolean
True if the offset/fetchFirst clauses come from JDBC limit/offset escape syntax
hasJDBClimitClause
,
ContextManager
Context Manager
cm
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
acceptChildren(Visitor
the visitor
v
)

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.acceptChildren.

Accept the visitor for all visitable children of this node.

pack-priv ValueNode

Returns:

The new top of the expression tree.
bindExpression
(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.

fromList
,
SubqueryList
The subquery list being built as we find SubqueryNodes
subqueryList
,
List<AggregateNode>
The aggregate list being built as we find AggregateNodes
aggregates
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.bindExpression.

Bind this expression.

private boolean

Returns:

Whether or not the NOT IN or ALL subquery can be flattened.
canAllBeFlattened
()

Can NOT IN, ALL be falttened to NOT EXISTS join?

pack-priv boolean

Returns:

boolean Whether or not source.expression is a ColumnReference or a VirtualColumnNode.
categorize
(JBitSet
JBitSet with bit map of referenced FromTables
referencedTabs
,
boolean
Whether or not to consider method calls, field references and conditional nodes when building bit map
simplePredsOnly
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.categorize.

Categorize this predicate.

pack-priv ValueNode

Returns:

The modified expression
changeToCNF
(boolean
Whether or not we are under a top level AndNode.
underTopAndNode
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.changeToCNF.

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.

pack-priv ValueNode

Returns:

The modified expression
eliminateNots
(boolean
Whether or not we are under a NotNode.
underNotNode
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.eliminateNots.

Eliminate NotNodes in the current query block.

private ValueNode

Returns:

The modified expression
flattenToExistsJoin
(int
Number of tables in the DML Statement
numTables
,
FromList
FromList from outer query block
outerFromList
,
SubqueryList
SubqueryList from outer query block
outerSubqueryList
,
PredicateList
PredicateList from outer query block
outerPredicateList
,
boolean
Is it a flattening into a NOT EXISTS join
flattenableNotExists
)

Flatten this subquery into the outer query block as an exists join.

private ValueNode

Returns:

The modified expression
flattenToNormalJoin
(int
Number of tables in the DML Statement
numTables
,
FromList
FromList from outer query block
outerFromList
,
SubqueryList
SubqueryList from outer query block
outerSubqueryList
,
PredicateList
PredicateList from outer query block
outerPredicateList
)

Flatten this subquery into the outer query block.

pack-priv void
generateExpression(ExpressionClassBuilder
The ExpressionClassBuilder for the class being built
expressionBuilder
,
MethodBuilder
The method the expression will go into
mbex
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.generateExpression.

Do code generation for this subquery.

private LocalField
public ValueNode

Returns:

fetch first if specified, else null.
getFetchFirst
()

Get FETCH FIRST (used to construct FROM_SUBQUERY only), cf. FromSubquery, for which this node is transient.

private BinaryComparisonOperatorNode
getNewJoinCondition(ValueNode
The left operand for the new condition.
leftOperand
,
ValueNode
The right operand for the new condition.
rightOperand
)

Build a new join condition between the leftOperand and the rightOperand.

public ValueNode

Returns:

offset if specified, else null.
getOffset
()

Get OFFSET (used to construct FROM_SUBQUERY only), cf. FromSubquery, for which this node is transient.

protected int

Returns:

The variant type for the underlying expression.
getOrderableVariantType
()

Overrides org.apache.derby.impl.sql.compile.ValueNode.getOrderableVariantType.

Return the variant type for the underlying expression.

public OrderByList

Returns:

order by list if specified, else null.
getOrderByList
()

Get ORDER BY list (used to construct FROM_SUBQUERY only), cf. FromSubquery, for which this node is transient.

pack-priv int

Returns:

int The ResultSet # for the point of attachment
getPointOfAttachment
()

Get the ResultSet # for the point of attachment for this SubqueryNode.

pack-priv boolean

Returns:

Whether or not this SubqueryNode has already been preprocessed.
getPreprocessed
()

Get whether or not this SubqueryNode has already been preprocessed.

pack-priv ResultSetNode

Returns:

ResultSetNode underlying this SubqueryNode.
getResultSet
()

Return the resultSet for this SubqueryNode.

private ValueNode

Returns:

the right operand
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.

pack-priv int

Returns:

int Type of this subquery.
getSubqueryType
()

Return the type of this subquery.

private BooleanConstantNode
pack-priv boolean

Returns:

boolean Whether or not this subquery is immediately under a top level AndNode.
getUnderTopAndNode
()

Return whether or not this subquery is immediately under a top level AndNode.

pack-priv boolean

Returns:

whether the subquery has correlated column references.
hasCorrelatedCRs
()

Check to see if this subquery has correlated column references.

public boolean

Returns:

true if the JDBC limit/offset semantics (rather than the SQL Standard OFFSET/FETCH NEXT) semantics apply
hasJDBClimitClause
()

Return true if the offset/fetchFirst clauses were added by JDBC LIMIT escape syntax.

private boolean
private boolean
pack-priv boolean
isEquivalent(ValueNode
the node to compare this ValueNode against.
o
)

Implements abstract org.apache.derby.impl.sql.compile.ValueNode.isEquivalent.

Tests if this node is equivalent to the specified ValueNode.

private boolean
public boolean

Returns:

true if it is part of a having clause, otherwise false
isHavingSubquery
()

Is this subquery part of a having clause?

private boolean
isIN()

private boolean

Returns:

boolean whether we have
isInvariant
()

Check to see if we have a Variant value below us.

public boolean
private boolean
private boolean
pack-priv boolean

Returns:

true if this subquery is a WHERE EXISTS | ANY | IN subquery with a subquery in its own WHERE clause
isWhereExistsAnyInWithWhereSubquery
()

Check whether this is a WHERE EXISTS | ANY | IN subquery with a subquery in its own WHERE clause.

pack-priv boolean

Returns:

true if it is part of a where clause, otherwise false
isWhereSubquery
()

Is this subquery part of a whereclause?

pack-priv void
modifyAccessPaths()

Make any changes to the access paths, as decided by the optimizer.

pack-priv void
optimize(DataDictionary
The DataDictionary to use for optimization
dataDictionary
,
double
The optimizer's estimate of the number of times this subquery will be executed.
outerRows
)

Optimize this SubqueryNode.

pack-priv ValueNode

Returns:

The modified expression
preprocess
(int
Number of tables in the DML Statement
numTables
,
FromList
FromList from outer query block
outerFromList
,
SubqueryList
SubqueryList from outer query block
outerSubqueryList
,
PredicateList
PredicateList from outer query block
outerPredicateList
)

Overrides org.apache.derby.impl.sql.compile.ValueNode.preprocess.

Preprocess an expression tree.

pack-priv void
printSubNodes(int
The depth of this node in the tree
depth
)

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.printSubNodes.

Prints the sub-nodes of this object.

private UnaryComparisonOperatorNode

Returns:

UnaryComparisonOperatorNode An IS [NOT] NULL above the transformed subquery.
pushNewPredicate
(int
Number of tables in DML Statement
numTables
)

Transform: expression QuantifiedOperator (select x from ...) into (select true from ..

public boolean
referencesSessionSchema()

Overrides org.apache.derby.impl.sql.compile.QueryTreeNode.referencesSessionSchema.

Return true if the node references SESSION schema tables (temporary or permanent)

pack-priv ValueNode

Returns:

ValueNode The remapped expression tree.
remapColumnReferencesToExpressions
()

Overrides org.apache.derby.impl.sql.compile.ValueNode.remapColumnReferencesToExpressions.

Remap all ColumnReferences in this tree to be clones of the underlying expression.

private boolean

Returns:

true if it is OK to flatten this query to a NOT EXISTS join, false otherwise
rightOperandFlattenableToNotExists
(int
the number of tables in this statement
numTables
,
FromBaseTable
the only FromBaseTable in this subquery
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
public void
setHavingSubquery(boolean havingSubquery)

Mark this subquery as being part of a having clause.

pack-priv void
setParentComparisonOperator(BinaryComparisonOperatorNode
The parent BCON.
parent
)

Set the parent BCON.

pack-priv void
setPointOfAttachment(int
The point of attachment of this subquery.
pointOfAttachment
)

Set the point of attachment of this subquery.

pack-priv void
setSubqueryType(int
of this subquery.
subqueryType
)

Set the type of this subquery.

pack-priv void
setWhereSubquery(boolean whereSubquery)

Mark this subquery as being part of a where clause.

private FromBaseTable

Returns:

the FromBaseTable 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 FBT
singleFromBaseTable
(FromList
The from list from the subquery
fromList
)

Does the from list from the subquery contain a single entry which is a FBT or a PRN/FBT.

public String

Returns:

This object as a String
toString
()

Overrides org.apache.derby.impl.sql.compile.ValueNode.toString.

Convert this object to a String.

Inherited from org.apache.derby.impl.sql.compile.ValueNode:
bindExpressioncheckIsBooleancheckTopPredicatesForEqualsConditionsconstantExpressioncopyFieldsevaluateConstantExpressionsgenEqualsFalseTreegenerategenIsNullTreegenSQLJavaSQLTreegetClonegetColumnNamegetConstantValueAsObjectgetDataValueFactorygetSchemaNamegetSourceResultColumngetTableNamegetTablesReferencedgetTransformedgetTypeCompilergetTypeIdgetTypeServicesisBinaryEqualsOperatorNodeisBooleanFalseisBooleanTrueisCloneableisConstantExpressionisInListProbeNodeisParameterNodeisRelationalOperatorisSameNodeKindoptimizableEqualityNodeputAndsOnToprequiresTypeFromContextselectivitysetCollationInfosetCollationInfosetCollationUsingCompilationSchemasetCollationUsingCompilationSchemasetNullabilitysetTransformedsetTypesetTypesetTypeupdatableByCursorverifyChangeToCNFverifyEliminateNotsverifyPutAndsOnTop