Class ValueNode

    • Field Detail

      • dataTypeServices

        private DataTypeDescriptor dataTypeServices
        The data type for this node.
      • transformed

        boolean transformed
    • Method Detail

      • setType

        final void setType​(TypeId typeId,
                           int precision,
                           int scale,
                           boolean isNullable,
                           int maximumWidth)
                    throws StandardException
        Set this node's type from type components.
        Throws:
        StandardException
      • toString

        public java.lang.String toString()
        Convert this object to a String. See comments in QueryTreeNode.java for how this should be done for tree printing.
        Overrides:
        toString in class QueryTreeNode
        Returns:
        This object as a String
      • getTypeServices

        DataTypeDescriptor getTypeServices()
        Get the DataTypeServices from this ValueNode.
        Returns:
        The DataTypeServices from this ValueNode. This may be null if the node isn't bound yet.
      • setCollationInfo

        public void setCollationInfo​(DataTypeDescriptor collationInfoType)
                              throws StandardException
        Set the collation type and derivation of this node based upon the collation information in the passed in type. Note that the base type of this node is not changed (e.g. INTEGER), only its collation settings. This may result in a different object being returned from getTypeServices().
        Parameters:
        collationInfoType - Type to take collation type and derivation from.
        Throws:
        StandardException - Error setting type.
      • setCollationInfo

        public void setCollationInfo​(int collationType,
                                     int collationDerivation)
                              throws StandardException
        Set the collation type and derivation of this node based upon the collation information passed in. This may result in a different object being returned from getTypeServices().
        Parameters:
        collationType - Collation type
        collationDerivation - Collation derivation
        Throws:
        StandardException - Error setting type
      • getTypeId

        TypeId getTypeId()
                  throws StandardException
        Get the TypeId from this ValueNode.
        Returns:
        The TypeId from this ValueNode. This may be null if the node isn't bound yet.
        Throws:
        StandardException
      • getDataValueFactory

        protected final DataValueFactory getDataValueFactory()
        Return the DataValueFactory
      • setType

        void setType​(DataTypeDescriptor dataTypeServices)
              throws StandardException
        Set the DataTypeServices for this ValueNode. This method is overridden in ParameterNode.
        Parameters:
        dataTypeServices - The DataTypeServices to set in this ValueNode
        Throws:
        StandardException
      • setCollationUsingCompilationSchema

        protected final void setCollationUsingCompilationSchema()
                                                         throws StandardException
        Set the collation based upon the current schema with derivation type implicit.
        Throws:
        StandardException
      • setCollationUsingCompilationSchema

        protected final void setCollationUsingCompilationSchema​(int collationDerivation)
                                                         throws StandardException
        There are many subclasses of ValueNode where we want the DataTypeDescriptor of the node to have the same collation type as the compilation schema's collation type. For that purpose, this method in the baseclass here can be utilized by the subclasses. In addition, the subclasses can pass the collationDerivation that they expect the DataTypeDescriptor to have.
        Parameters:
        collationDerivation - This can be StringDataValue#COLLATION_DERIVATION_IMPLICIT StringDataValue#COLLATION_DERIVATION_NONE StringDataValue#COLLATION_DERIVATION_EXPLICIT
        Throws:
        StandardException
      • getSourceResultColumn

        ResultColumn getSourceResultColumn()
        Get the source for this ValueNode.
        Returns:
        The source of this ValueNode, null if this node is not sourced by a column.
      • setTransformed

        void setTransformed()
        Mark this predicate has having been transformed (other predicates were generated from it). This will help us with ensure that the predicate does not get calculated into the selectivity multiple times.
      • getTransformed

        boolean getTransformed()
        Return whether or not this predicate has been transformed.
        Returns:
        Whether or not this predicate has been transformed.
      • bindExpression

        ValueNode bindExpression​(FromList fromList,
                                 SubqueryList subqueryList,
                                 java.util.List<AggregateNode> aggregates,
                                 boolean forQueryRewrite)
                          throws StandardException
        Bind this expression. This is a place-holder method - it should never be called.
        Parameters:
        fromList - The FROM list to use for binding
        subqueryList - The SubqueryList we are building as we hit SubqueryNodes.
        aggregates - The aggregate list being built as we find AggregateNodes
        Returns:
        The new top of the expression tree.
        Throws:
        StandardException - Thrown on error
      • genSQLJavaSQLTree

        ValueNode genSQLJavaSQLTree()
                             throws StandardException
        Generate a SQL->Java->SQL conversion tree above the current node and bind the new nodes individually. This is useful when doing comparisons, built-in functions, etc. on java types which have a direct mapping to system built-in types.
        Returns:
        ValueNode The new tree.
        Throws:
        StandardException - Thrown on error
      • preprocess

        ValueNode preprocess​(int numTables,
                             FromList outerFromList,
                             SubqueryList outerSubqueryList,
                             PredicateList outerPredicateList)
                      throws StandardException
        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 - 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
      • evaluateConstantExpressions

        ValueNode evaluateConstantExpressions()
                                       throws StandardException
        If this node is known to always evaluate to the same value, return a node that represents that known value as a constant. Typically used to transform operators with constant operands into constants.
        Returns:
        a constant representing the value to which this node is guaranteed to evaluate, or this if the value is not known
        Throws:
        StandardException - if an error occurs during evaluation
        See Also:
        ConstantExpressionVisitor
      • eliminateNots

        ValueNode eliminateNots​(boolean underNotNode)
                         throws StandardException
        Eliminate NotNodes in the current query block. We traverse the tree, inverting ANDs and ORs and eliminating NOTs as we go. We stop at ComparisonOperators and boolean expressions. We invert ComparisonOperators and replace boolean expressions with boolean expression = false. NOTE: Since we do not recurse under ComparisonOperators, there still could be NotNodes left in the tree.
        Parameters:
        underNotNode - Whether or not we are under a NotNode.
        Returns:
        The modified expression
        Throws:
        StandardException - Thrown on error
      • genEqualsFalseTree

        ValueNode genEqualsFalseTree()
                              throws StandardException
        Transform this into this = false. Useful for NOT elimination.
        Returns:
        The modified expression
        Throws:
        StandardException - Thrown on error
      • genIsNullTree

        ValueNode genIsNullTree​(boolean notNull)
                         throws StandardException
        Transform this into this IS NULL or IS NOT NULL.
        Parameters:
        notNull - if true, transform this into IS NOT NULL; otherwise, transform this into IS NULL
        Returns:
        The modified expression
        Throws:
        StandardException - Thrown on error
      • verifyEliminateNots

        boolean verifyEliminateNots()
        Verify that eliminateNots() did its job correctly. Verify that there are no NotNodes above the top level comparison operators and boolean expressions.
        Returns:
        Boolean which reflects validity of the tree.
      • putAndsOnTop

        ValueNode putAndsOnTop()
                        throws StandardException
        Do the 1st step in putting an expression into conjunctive normal form. This step ensures that the top level of the expression is a chain of AndNodes.
        Returns:
        The modified expression
        Throws:
        StandardException - Thrown on error
      • verifyPutAndsOnTop

        boolean verifyPutAndsOnTop()
        Verify that putAndsOnTop() did its job correctly. Verify that the top level of the expression is a chain of AndNodes.
        Returns:
        Boolean which reflects validity of the tree.
      • changeToCNF

        ValueNode changeToCNF​(boolean underTopAndNode)
                       throws StandardException
        Finish putting an expression into conjunctive normal form. An expression tree in conjunctive normal form meets the following criteria: o If the expression tree is not null, the top level will be a chain of AndNodes terminating in a true BooleanConstantNode. o The left child of an AndNode will never be an AndNode. o Any right-linked chain that includes an AndNode will be entirely composed of AndNodes terminated by a true BooleanConstantNode. o The left child of an OrNode will never be an OrNode. o Any right-linked chain that includes an OrNode will be entirely composed of OrNodes terminated by a false BooleanConstantNode. o ValueNodes other than AndNodes and OrNodes are considered leaf nodes for purposes of expression normalization. In other words, we won't do any normalization under those nodes. In addition, we track whether or not we are under a top level AndNode. SubqueryNodes need to know this for subquery flattening.
        Parameters:
        underTopAndNode - Whether or not we are under a top level AndNode.
        Returns:
        The modified expression
        Throws:
        StandardException - Thrown on error
      • verifyChangeToCNF

        boolean verifyChangeToCNF()
        Verify that changeToCNF() did its job correctly. Verify that: o AndNode - rightOperand is not instanceof OrNode leftOperand is not instanceof AndNode o OrNode - rightOperand is not instanceof AndNode leftOperand is not instanceof OrNode
        Returns:
        Boolean which reflects validity of the tree.
      • categorize

        boolean categorize​(JBitSet referencedTabs,
                           boolean simplePredsOnly)
                    throws StandardException
        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 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
      • getSchemaName

        java.lang.String getSchemaName()
                                throws StandardException
        This returns the user-supplied schema name of the column. At this class level, it simply returns null. But, the subclasses of ValueNode will overwrite this method to return the user-supplied schema name. When the value node is in a result column of a select list, the user can request metadata information. The result column won't have a column descriptor, so we return some default information through the expression. This lets expressions that are simply columns return all of the info, and others use this supertype's default values.
        Returns:
        the default schema name for an expression -- null
        Throws:
        StandardException
      • getTableName

        java.lang.String getTableName()
        This returns the user-supplied table name of the column. At this class level, it simply returns null. But, the subclasses of ValueNode will overwrite this method to return the user-supplied table name. When the value node is in a result column of a select list, the user can request metadata information. The result column won't have a column descriptor, so we return some default information through the expression. This lets expressions that are simply columns return all of the info, and others use this supertype's default values.
        Returns:
        the default table name for an expression -- null
      • updatableByCursor

        public boolean updatableByCursor()
        Returns:
        the default updatability for an expression - false
      • getColumnName

        java.lang.String getColumnName()
        This is null so that the caller will substitute in the resultset generated name as needed.
        Returns:
        the default column name for an expression -- null.
      • getTablesReferenced

        JBitSet getTablesReferenced()
                             throws StandardException
        Get a bit map of table references in this expression
        Returns:
        A bit map of table numbers referred to in this expression
        Throws:
        StandardException - Thrown on error
      • isCloneable

        boolean isCloneable()
        Return whether or not this expression tree is cloneable.
        Returns:
        boolean Whether or not this expression tree is cloneable.
      • remapColumnReferencesToExpressions

        ValueNode remapColumnReferencesToExpressions()
                                              throws StandardException
        Remap all ColumnReferences in this tree to be clones of the underlying expression.
        Returns:
        ValueNode The remapped expression tree.
        Throws:
        StandardException - Thrown on error
      • isConstantExpression

        boolean isConstantExpression()
        Return whether or not this expression tree represents a constant expression.
        Returns:
        Whether or not this expression tree represents a constant expression.
      • constantExpression

        boolean constantExpression​(PredicateList whereClause)
        Return whether or not this expression tree represents a constant value. In this case, "constant" means that it will always evaluate to the same thing, even if it includes columns. A column is constant if it is compared to a constant expression.
        Returns:
        True means this expression tree represents a constant value.
      • getOrderableVariantType

        protected int getOrderableVariantType()
                                       throws StandardException
        Return the variant type for the underlying expression. The variant type can be: VARIANT - variant within a scan (method calls and non-static field access) SCAN_INVARIANT - invariant within a scan (column references from outer tables) QUERY_INVARIANT - invariant within the life of a query (constant expressions)
        Returns:
        The variant type for the underlying expression.
        Throws:
        StandardException - Thrown on error
      • checkIsBoolean

        public ValueNode checkIsBoolean()
                                 throws StandardException
        Bind time logic. Raises an error if this ValueNode does not resolve to a boolean value. This method is called by WHERE clauses.
        Returns:
        bound coercion of this node to a builtin type as necessary
        Throws:
        StandardException - Thrown on error
      • getConstantValueAsObject

        java.lang.Object getConstantValueAsObject()
                                           throws StandardException
        Return an Object representing the bind time value of this expression tree. If the expression tree does not evaluate to a constant at bind time then we return null. This is useful for bind time resolution of VTIs. RESOLVE: What do we do for primitives?
        Returns:
        An Object representing the bind time value of this expression tree. (null if not a bind time constant.)
        Throws:
        StandardException - Thrown on error
      • selectivity

        public double selectivity​(Optimizable optTable)
                           throws StandardException
        The default selectivity for value nodes is 50%. This is overridden in specific cases, such as the RelationalOperators.
        Throws:
        StandardException
      • checkTopPredicatesForEqualsConditions

        void checkTopPredicatesForEqualsConditions​(int tableNumber,
                                                   boolean[] eqOuterCols,
                                                   int[] tableNumbers,
                                                   JBitSet[] tableColMap,
                                                   boolean resultColTable)
                                            throws StandardException
        Update the array of columns in = conditions with expressions without column references from the same table. This is useful when doing subquery flattening on the basis of an equality condition. eqOuterCols or tableColMap may be null if the calling routine doesn't need the information provided
        Parameters:
        tableNumber - The tableNumber of the table from which the columns of interest come from.
        eqOuterCols - Array of booleans for noting which columns are in = predicates without columns from the subquery block. May be null.
        tableNumbers - Array of table numbers in this query block.
        tableColMap - Array of bits for noting which columns are in = predicates for each table in the query block. May be null.
        resultColTable - True if tableNumber is the table containing result columns
        Throws:
        StandardException - Thrown on error
      • isBooleanTrue

        boolean isBooleanTrue()
        Does this represent a true constant.
        Returns:
        Whether or not this node represents a true constant.
      • isBooleanFalse

        boolean isBooleanFalse()
        Does this represent a false constant.
        Returns:
        Whether or not this node represents a false constant.
      • generateExpression

        void generateExpression​(ExpressionClassBuilder acb,
                                MethodBuilder mb)
                         throws StandardException
        Generate code for this calculation. This is a place-holder method - it should not be called.
        Parameters:
        acb - The ExpressionClassBuilder for the class being built
        mb - The method the expression will go into
        Throws:
        StandardException - Thrown on error
      • updateMaps

        private void updateMaps​(JBitSet[] tableColMap,
                                boolean[] eqOuterCols,
                                int[] tableNumbers,
                                int tableNumber,
                                int resultTable,
                                ValueNode arg1,
                                ValueNode arg2)
                         throws StandardException
        Set the correct bits in tableColMap and set the boolean value in eqOuterCols given two arguments to an = predicate tableColMap[t] - bit is set if the column is in an = predicate with a column in table t, or a bit is set if the column is in an = predicate with a constant,parameter or correlation variable (for all table t, if this tableColMap is not for the table with the result columns) eqOuterCols[c] - is true if the column is in an = predicate with a constant, parameter or correlation variable
        Parameters:
        tableColMap - Array of bitmaps for noting which columns are in = predicates with columns from each table
        eqOuterCols - Array of booleans for noting which columns are in = predicates without columns from the subquery block.
        tableNumber - table number for which we are setting up the Maps
        resultTable - -1 if this table is not the result table; otherwise the index into tableNumbers for the result table
        arg1 - one side of the = predicate
        arg2 - other side of the = predicate
        Throws:
        StandardException - Thrown on error
      • setValueCols

        private void setValueCols​(JBitSet[] tableColMap,
                                  boolean[] eqOuterCols,
                                  int colReference,
                                  int resultTable)
        Set eqOuterCols and the column in all the tables for constants, parmeters and correlation columns The column in the tableColMap is set only for the current table if the table is the result column table. For other tables in the query we set the column for all the tables since the constant will reduced the number of columns required in a unique multicolumn index for distinctness. For example, given an unique index on t1(a,b), setting b=1 means that t1(a) is unique since there can be no duplicates for a where b=1 without destroying the uniqueness of t1(a,b). However, for the result columns setting b=1, does not mean that a select list of t1.a is distinct if t1.a is the only column used in joining with another table e.g. select t1.a from t1, t2 where t1.a = t2.a and t1.b = 1; t1 t2 result a b a a 1 1 1 1 1 2 2 1 2 1
        Parameters:
        tableColMap - Array of bitmaps for noting which columns are in = predicates with columns from each table
        eqOuterCols - Array of booleans for noting which columns are in = predicates without columns from the subquery block.
        colReference - The column to set
        resultTable - If -1 set all the bit for all the tables for that column; otherwise set the bit for the specified table
      • isRelationalOperator

        boolean isRelationalOperator()
        Returns true if this ValueNode is a relational operator. Relational Operators are <, <=, =, >, >=, <> as well as IS NULL and IS NOT NULL. This is the preferred way of figuring out if a ValueNode is relational or not.
        See Also:
        RelationalOperator, BinaryRelationalOperatorNode, IsNullNode
      • isBinaryEqualsOperatorNode

        boolean isBinaryEqualsOperatorNode()
        Returns true if this value node is a equals operator.
        See Also:
        isRelationalOperator()
      • isInListProbeNode

        boolean isInListProbeNode()
        Returns true if this value node is an operator created for optimized performance of an IN list. Or more specifically, returns true if this value node is an equals operator of the form "col = ?" that we generated during preprocessing to allow index multi-probing.
      • optimizableEqualityNode

        boolean optimizableEqualityNode​(Optimizable optTable,
                                        int columnNumber,
                                        boolean isNullOkay)
                                 throws StandardException
        Return true if the predicate represents an optimizable equality node. an expression is considered to be an optimizable equality node if all the following conditions are met:
        1. the operator is an = or IS NULL operator
        2. one of the operands is a column specified by optTable/columnNumber
        3. Both operands are not the same column; i.e tab.col = tab.col
        4. There are no implicit varchar comparisons of the operands; i.e either both operands are string like (varchar, char, longvarchar) or neither operand is string like
        Parameters:
        optTable - the table being optimized. Column reference must be from this table.
        columnNumber - the column number. One of the operands of this predicate must be the column number specified by optTable/columnNumber
        isNullOkay - if set to true we also consider IS NULL predicates; otherwise consider only = predicates.
        Throws:
        StandardException
      • requiresTypeFromContext

        boolean 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:
        Whether this node's type will be determined from the context
      • isParameterNode

        boolean isParameterNode()
        Returns TRUE if this is a parameter node. We do lots of special things with Parameter Nodes.
      • isEquivalent

        abstract boolean isEquivalent​(ValueNode other)
                               throws StandardException
        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 - the node to compare this ValueNode against.
        Returns:
        true if the two nodes are equivalent, false otherwise.
        Throws:
        StandardException
      • isSameNodeKind

        boolean isSameNodeKind​(ValueNode other)
        Some node classes represent several logical node types (to reduce footprint), which we call kinds. This means that implementations of isEquivalent(org.apache.derby.impl.sql.compile.ValueNode) cannot always just use instanceof to check if the other node represents the same kind. Hence this method needs to be overridden by all node classes that represent several kinds. This default implementation does not look at kinds. It is only called from implementations of isEquivalent.
        Parameters:
        other - The other value node whose kind we want to compare with.
        Returns:
        true if this and o represent the same logical node type, i.e. kind.