Class CachedValueNode
- java.lang.Object
-
- org.apache.derby.impl.sql.compile.QueryTreeNode
-
- org.apache.derby.impl.sql.compile.ValueNode
-
- org.apache.derby.impl.sql.compile.CachedValueNode
-
- All Implemented Interfaces:
Visitable
class CachedValueNode extends ValueNode
A wrapper class for a
ValueNode
that is referenced multiple places in the abstract syntax tree, but should only be evaluated once. This node will cache the return value the first time the expression is evaluated, and simply return the cached value the next time.For example, an expression such as
CASE expr1 WHEN expr2 THEN expr3 WHEN expr4 THEN expr5 END
is rewritten by the parser to
CASE WHEN expr1 = expr2 THEN expr3 WHEN expr1 = expr4 THEN expr5 END
In this case, we want
expr1
to be evaluated only once, even though it's referenced twice in the rewritten tree. By wrapping theValueNode
forexpr1
in aCachedValueNode
, we make sureexpr1
is only evaluated once, and the second reference to it will use the cached return value from the first evaluation.
-
-
Field Summary
Fields Modifier and Type Field Description private LocalField
field
The field in theActivation
class where the value is cached.private ValueNode
value
The node representing the expression whose value should be cached.-
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 CachedValueNode(ValueNode value)
Wrap the value in aCachedValueNode
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
acceptChildren(Visitor v)
Accept a visitor on all child nodes.(package private) ValueNode
bindExpression(FromList fromList, SubqueryList subqueryList, java.util.List<AggregateNode> aggregates)
(package private) boolean
categorize(JBitSet referencedTabs, boolean simplePredsOnly)
Categorize this predicate.(package private) void
generateClearField(MethodBuilder mb)
Generate code that clears the field that holds the cached value, so that it can be garbage collected.(package private) void
generateExpression(ExpressionClassBuilder acb, MethodBuilder mb)
Generate code that returns the value that this expression evaluates to.(package private) DataTypeDescriptor
getTypeServices()
Get the DataTypeServices from this ValueNode.(package private) boolean
isEquivalent(ValueNode other)
Tests if this node is equivalent to the specified ValueNode.(package private) ValueNode
preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList)
Preprocess an expression tree.(package private) ValueNode
remapColumnReferencesToExpressions()
Remap all ColumnReferences in this tree to be clones of the underlying expression.(package private) boolean
requiresTypeFromContext()
Returns TRUE if the type of this node will be determined from the context in which it is getting used.(package private) void
setType(DataTypeDescriptor dtd)
Set the DataTypeServices for this ValueNode.-
Methods inherited from class org.apache.derby.impl.sql.compile.ValueNode
bindExpression, changeToCNF, checkIsBoolean, checkTopPredicatesForEqualsConditions, constantExpression, copyFields, eliminateNots, evaluateConstantExpressions, genEqualsFalseTree, generate, genIsNullTree, genSQLJavaSQLTree, getClone, getColumnName, getConstantValueAsObject, getDataValueFactory, getOrderableVariantType, getSchemaName, getSourceResultColumn, getTableName, getTablesReferenced, getTransformed, getTypeCompiler, getTypeId, isBinaryEqualsOperatorNode, isBooleanFalse, isBooleanTrue, isCloneable, isConstantExpression, isInListProbeNode, isParameterNode, isRelationalOperator, isSameNodeKind, optimizableEqualityNode, putAndsOnTop, selectivity, setCollationInfo, setCollationInfo, setCollationUsingCompilationSchema, setCollationUsingCompilationSchema, setNullability, setTransformed, setType, setType, toString, 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, printSubNodes, referencesSessionSchema, resolveTableToSynonym, setBeginOffset, setEndOffset, setRefActionInfo, stackPrint, taggedWith, treePrint, treePrint, verifyClassExist
-
-
-
-
Field Detail
-
value
private ValueNode value
The node representing the expression whose value should be cached.
-
field
private LocalField field
The field in theActivation
class where the value is cached.
-
-
Constructor Detail
-
CachedValueNode
CachedValueNode(ValueNode value)
Wrap the value in aCachedValueNode
.- Parameters:
value
- the value to wrap
-
-
Method Detail
-
generateExpression
void generateExpression(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException
Generate code that returns the value that this expression evaluates to. For the first occurrence of this node in the abstract syntax tree, this method generates the code needed to evaluate the expression. Additionally, it stores the returned value in a field in theActivation
class. For subsequent occurrences of this node, it will simply generate code that reads the value of that field, so that reevaluation is not performed.- Overrides:
generateExpression
in classValueNode
- Parameters:
acb
- the class buildermb
- the method builder- Throws:
StandardException
- if an error occurs
-
generateClearField
void generateClearField(MethodBuilder mb)
Generate code that clears the field that holds the cached value, so that it can be garbage collected.- Parameters:
mb
- the method builder that should have the code
-
bindExpression
ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, java.util.List<AggregateNode> aggregates) throws StandardException
- Overrides:
bindExpression
in classValueNode
- Throws:
StandardException
-
preprocess
ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException
Description copied from class:ValueNode
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:
preprocess
in 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
-
isEquivalent
boolean isEquivalent(ValueNode other) throws StandardException
Description copied from class:ValueNode
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:
isEquivalent
in classValueNode
- Parameters:
other
- the node to compare this ValueNode against.- Returns:
true
if the two nodes are equivalent,false
otherwise.- Throws:
StandardException
-
acceptChildren
void acceptChildren(Visitor v) throws StandardException
Description copied from class:QueryTreeNode
Accept a visitor on all child nodes. All sub-classes that add fields that should be visited, should override this method and callaccept(v)
on all visitable fields, as well assuper.acceptChildren(v)
to make sure all visitable fields defined by the super-class are accepted too.- Overrides:
acceptChildren
in classQueryTreeNode
- Parameters:
v
- the visitor- Throws:
StandardException
- on errors raised by the visitor
-
getTypeServices
DataTypeDescriptor getTypeServices()
Description copied from class:ValueNode
Get the DataTypeServices from this ValueNode.- Overrides:
getTypeServices
in classValueNode
- Returns:
- The DataTypeServices from this ValueNode. This may be null if the node isn't bound yet.
-
setType
void setType(DataTypeDescriptor dtd) throws StandardException
Description copied from class:ValueNode
Set the DataTypeServices for this ValueNode. This method is overridden in ParameterNode.- Overrides:
setType
in classValueNode
- Parameters:
dtd
- The DataTypeServices to set in this ValueNode- Throws:
StandardException
-
requiresTypeFromContext
boolean requiresTypeFromContext()
Description copied from class:ValueNode
Returns TRUE if the type of this node will be determined from the context in which it is getting used. If true is returned then after bindExpression() is called on the node, its type must be set (from the relevant context) using setType().- Overrides:
requiresTypeFromContext
in classValueNode
- Returns:
- Whether this node's type will be determined from the context
-
remapColumnReferencesToExpressions
ValueNode remapColumnReferencesToExpressions() throws StandardException
Description copied from class:ValueNode
Remap all ColumnReferences in this tree to be clones of the underlying expression.- Overrides:
remapColumnReferencesToExpressions
in classValueNode
- Returns:
- ValueNode The remapped expression tree.
- Throws:
StandardException
- Thrown on error
-
categorize
boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) throws StandardException
Description copied from class:ValueNode
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:
categorize
in 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
-
-