Class CachedValueNode
- All Implemented Interfaces:
Visitable
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 the
ValueNode for expr1 in a CachedValueNode, we
make sure expr1 is only evaluated once, and the second reference
to it will use the cached return value from the first evaluation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate LocalFieldThe field in theActivationclass where the value is cached.private ValueNodeThe node representing the expression whose value should be cached.Fields inherited from class ValueNode
transformedFields inherited from class QueryTreeNode
AUTOINCREMENT_CREATE_MODIFY, AUTOINCREMENT_CYCLE, AUTOINCREMENT_INC_INDEX, AUTOINCREMENT_IS_AUTOINCREMENT_INDEX, AUTOINCREMENT_START_INDEX -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidAccept a visitor on all child nodes.(package private) ValueNodebindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) (package private) booleancategorize(JBitSet referencedTabs, boolean simplePredsOnly) Categorize this predicate.(package private) voidGenerate code that clears the field that holds the cached value, so that it can be garbage collected.(package private) voidGenerate code that returns the value that this expression evaluates to.(package private) DataTypeDescriptorGet the DataTypeServices from this ValueNode.(package private) booleanisEquivalent(ValueNode other) Tests if this node is equivalent to the specified ValueNode.(package private) ValueNodepreprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) Preprocess an expression tree.(package private) ValueNodeRemap all ColumnReferences in this tree to be clones of the underlying expression.(package private) booleanReturns TRUE if the type of this node will be determined from the context in which it is getting used.(package private) voidSet the DataTypeServices for this ValueNode.Methods inherited from class 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, verifyPutAndsOnTopMethods 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, 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 Details
-
value
The node representing the expression whose value should be cached. -
field
The field in theActivationclass where the value is cached.
-
-
Constructor Details
-
CachedValueNode
CachedValueNode(ValueNode value) Wrap the value in aCachedValueNode.- Parameters:
value- the value to wrap
-
-
Method Details
-
generateExpression
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 theActivationclass. 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:
generateExpressionin classValueNode- Parameters:
acb- the class buildermb- the method builder- Throws:
StandardException- if an error occurs
-
generateClearField
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, List<AggregateNode> aggregates) throws StandardException - Overrides:
bindExpressionin classValueNode- Throws:
StandardException
-
preprocess
ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException Description copied from class:ValueNodePreprocess 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
-
isEquivalent
Description copied from class:ValueNodeTests 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:
other- the node to compare this ValueNode against.- Returns:
trueif the two nodes are equivalent,falseotherwise.- Throws:
StandardException
-
acceptChildren
Description copied from class:QueryTreeNodeAccept 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:
acceptChildrenin classQueryTreeNode- Parameters:
v- the visitor- Throws:
StandardException- on errors raised by the visitor
-
getTypeServices
DataTypeDescriptor getTypeServices()Description copied from class:ValueNodeGet the DataTypeServices from this ValueNode.- Overrides:
getTypeServicesin classValueNode- Returns:
- The DataTypeServices from this ValueNode. This may be null if the node isn't bound yet.
-
setType
Description copied from class:ValueNodeSet the DataTypeServices for this ValueNode. This method is overridden in ParameterNode.- Overrides:
setTypein classValueNode- Parameters:
dtd- The DataTypeServices to set in this ValueNode- Throws:
StandardException
-
requiresTypeFromContext
boolean requiresTypeFromContext()Description copied from class:ValueNodeReturns 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:
requiresTypeFromContextin classValueNode- Returns:
- Whether this node's type will be determined from the context
-
remapColumnReferencesToExpressions
Description copied from class:ValueNodeRemap 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
-
categorize
Description copied from class:ValueNodeCategorize 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
-