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

pack-priv Class CachedValueNode

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, org.apache.derby.iapi.services.compiler.LocalField, .MethodBuilder, org.apache.derby.iapi.sql.compile.Visitor, org.apache.derby.iapi.types.DataTypeDescriptor, org.apache.derby.iapi.util.JBitSet

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

Modifier and TypeField and Description
private LocalField
field

The field in the Activation class where the value is cached.

private ValueNode
value

The node representing the expression whose value should be cached.

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

Constructor Summary

AccessConstructor and Description
pack-priv
CachedValueNode(ValueNode
the value to wrap
value
)

Wrap the value in a CachedValueNode.

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 a visitor on all child nodes.

pack-priv ValueNode
pack-priv boolean
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 void
generateClearField(MethodBuilder
the method builder that should have the code
mb
)

Generate code that clears the field that holds the cached value, so that it can be garbage collected.

pack-priv void
generateExpression(ExpressionClassBuilder
the class builder
acb
,
MethodBuilder
the method builder
mb
)

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

Generate code that returns the value that this expression evaluates to.

pack-priv DataTypeDescriptor
getTypeServices()

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

Get the DataTypeServices from this ValueNode.

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

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

Tests if this node is equivalent to the specified ValueNode.

pack-priv ValueNode
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 ValueNode
remapColumnReferencesToExpressions()

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

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

pack-priv boolean
requiresTypeFromContext()

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

Returns TRUE if the type of this node will be determined from the context in which it is getting used.

pack-priv void
setType(DataTypeDescriptor
The DataTypeServices to set in this ValueNode
dtd
)

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

Set the DataTypeServices for this ValueNode.

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

Field Detail

fieldback to summary
private LocalField field

The field in the Activation class where the value is cached.

valueback to summary
private ValueNode value

The node representing the expression whose value should be cached.

Constructor Detail

CachedValueNodeback to summary
pack-priv CachedValueNode(ValueNode value)

Wrap the value in a CachedValueNode.

Parameters
value:ValueNode

the value to wrap

Method Detail

acceptChildrenback to summary
pack-priv void acceptChildren(Visitor v) throws StandardException

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

Doc from org.apache.derby.impl.sql.compile.QueryTreeNode.acceptChildren.

Accept a visitor on all child nodes. All sub-classes that add fields that should be visited, should override this method and call accept(v) on all visitable fields, as well as super.acceptChildren(v) to make sure all visitable fields defined by the super-class are accepted too.

Parameters
v:Visitor

the visitor

Annotations
@Override
Exceptions
StandardException:
on errors raised by the visitor
bindExpressionback to summary
pack-priv ValueNode bindExpression(FromList fromList, SubqueryList subqueryList, List<AggregateNode> aggregates) throws StandardException

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

Annotations
@Override
categorizeback to summary
pack-priv boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) throws StandardException

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.categorize.

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.

Parameters
referencedTabs:JBitSet

JBitSet with bit map of referenced FromTables

simplePredsOnly:boolean

Whether or not to consider method calls, field references and conditional nodes when building bit map

Returns:boolean

boolean Whether or not source.expression is a ColumnReference or a VirtualColumnNode.

Annotations
@Override
Exceptions
StandardException:
Thrown on error
generateClearFieldback to summary
pack-priv void generateClearField(MethodBuilder mb)

Generate code that clears the field that holds the cached value, so that it can be garbage collected.

Parameters
mb:MethodBuilder

the method builder that should have the code

generateExpressionback to summary
pack-priv void generateExpression(ExpressionClassBuilder acb, MethodBuilder mb) throws StandardException

Overrides org.apache.derby.impl.sql.compile.ValueNode.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 the Activation 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.

Parameters
acb:ExpressionClassBuilder

the class builder

mb:MethodBuilder

the method builder

Annotations
@Override
Exceptions
StandardException:
if an error occurs
getTypeServicesback to summary
pack-priv DataTypeDescriptor getTypeServices()

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.getTypeServices.

Get the DataTypeServices from this ValueNode.

Returns:DataTypeDescriptor

The DataTypeServices from this ValueNode. This may be null if the node isn't bound yet.

Annotations
@Override
isEquivalentback to summary
pack-priv boolean isEquivalent(ValueNode other) throws StandardException

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.isEquivalent.

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.
Parameters
other:ValueNode

the node to compare this ValueNode against.

Returns:boolean

true if the two nodes are equivalent, false otherwise.

Annotations
@Override
preprocessback to summary
pack-priv ValueNode preprocess(int numTables, FromList outerFromList, SubqueryList outerSubqueryList, PredicateList outerPredicateList) throws StandardException

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.preprocess.

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.

Parameters
numTables:int

Number of tables in the DML Statement

outerFromList:FromList

FromList from outer query block

outerSubqueryList:SubqueryList

SubqueryList from outer query block

outerPredicateList:PredicateList

PredicateList from outer query block

Returns:ValueNode

The modified expression

Annotations
@Override
Exceptions
StandardException:
Thrown on error
remapColumnReferencesToExpressionsback to summary
pack-priv ValueNode remapColumnReferencesToExpressions() throws StandardException

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.remapColumnReferencesToExpressions.

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

Returns:ValueNode

ValueNode The remapped expression tree.

Annotations
@Override
Exceptions
StandardException:
Thrown on error
requiresTypeFromContextback to summary
pack-priv boolean requiresTypeFromContext()

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.requiresTypeFromContext.

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().

Returns:boolean

Whether this node's type will be determined from the context

Annotations
@Override
setTypeback to summary
pack-priv void setType(DataTypeDescriptor dtd) throws StandardException

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

Doc from org.apache.derby.impl.sql.compile.ValueNode.setType.

Set the DataTypeServices for this ValueNode. This method is overridden in ParameterNode.

Parameters
dtd:DataTypeDescriptor

The DataTypeServices to set in this ValueNode

Annotations
@Override