Class 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 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 Detail

      • value

        private ValueNode value
        The node representing the expression whose value should be cached.
      • field

        private LocalField field
        The field in the Activation class where the value is cached.
    • Constructor Detail

      • CachedValueNode

        CachedValueNode​(ValueNode value)
        Wrap the value in a CachedValueNode.
        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 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.
        Overrides:
        generateExpression in class ValueNode
        Parameters:
        acb - the class builder
        mb - 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
      • 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 class ValueNode
        Parameters:
        numTables - Number of tables in the DML Statement
        outerFromList - FromList from outer query block
        outerSubqueryList - SubqueryList from outer query block
        outerPredicateList - 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 class ValueNode
        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 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.
        Overrides:
        acceptChildren in class QueryTreeNode
        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 class ValueNode
        Returns:
        The DataTypeServices from this ValueNode. This may be null if the node isn't bound yet.
      • 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 class ValueNode
        Returns:
        Whether this node's type will be determined from the context
      • 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 class ValueNode
        Parameters:
        referencedTabs - JBitSet with bit map of referenced FromTables
        simplePredsOnly - 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