Class Expression

  • All Implemented Interfaces:
    java.lang.Cloneable
    Direct Known Subclasses:
    ArithmeticOperator, BooleanOperator, ColumnValue, CompareOperator, FunctionCall, InValuesOperator, IsNullOperator, LiteralValue, NegateOperator, StringMatchOperator, SubqueryOperator

    public abstract class Expression
    extends java.lang.Object
    implements java.lang.Cloneable
    This is the base type of all arithmetic and logical expressions that can be represented in SQL commands. Expressions can contain literal values, symbolic references (e.g. to columns on tables), arithmetic and logical operators, SQL operators such as LIKE and IN, and so forth.

    This class also provides a mechanism for evaluating these expressions, via the evaluate() and evaluate(Environment) methods. Evaluation may or may not require an "environment" which specifies the current values for symbols that appear within the expression. The hasSymbols() method reports whether the expression contains any symbols that would require an environment for evaluation. If the expression does not contain symbols, it can be evaluated without an environment.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Expression.SymbolFinder
      This ExpressionProcessor implementation traverses an expression and finds all symbols that appear within the expression.
    • Constructor Summary

      Constructors 
      Constructor Description
      Expression()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Object clone()
      Creates a copy of expression.
      Expression duplicate()
      Returns a deep copy of this expression.
      abstract boolean equals​(java.lang.Object obj)
      Checks if the argument is an expression with the same structure, but not necesarily the same references.
      java.lang.Object evaluate()
      Evaluates this expression object without any environment.
      abstract java.lang.Object evaluate​(Environment env)
      Evaluates this expression object in the context of the specified environment.
      boolean evaluatePredicate()
      Evaluates an expression as a Boolean predicate without any environment.
      boolean evaluatePredicate​(Environment env)
      Evaluates an expression as a Boolean predicate.
      void getAllSymbols​(java.util.Collection<ColumnName> symbols)
      This method stores all of the symbols in an expression into a collection, so that the expression's symbols can be validated against the schema that the expression will be evaluated against.
      abstract ColumnInfo getColumnInfo​(Schema schema)
      Returns a ColumnInfo object describing the type (and possibly the name) of the expression's result.
      abstract int hashCode()
      Computes the hashcode of an Expression.
      boolean hasSymbols()
      Returns true if this expression contains any symbols that will require an environment to evaluate.
      Expression simplify()
      Returns an Expression reference to a (possibly) simplified version of this expression.
      abstract Expression traverse​(ExpressionProcessor p)
      This method allows the entire expression tree to be traversed node by node, either for analysis or for transformation.
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Expression

        public Expression()
    • Method Detail

      • getColumnInfo

        public abstract ColumnInfo getColumnInfo​(Schema schema)
                                          throws SchemaNameException
        Returns a ColumnInfo object describing the type (and possibly the name) of the expression's result.
        Parameters:
        schema - a schema object that can be used to look up name and type details for symbols referenced by the expression.
        Returns:
        a column-information object describing the type (and possibly the name and table-name) of this expression's result
        Throws:
        SchemaNameException - if a symbol cannot be resolved, either because it doesn't appear in the schema, or because the name is ambiguous.
      • evaluate

        public abstract java.lang.Object evaluate​(Environment env)
                                           throws ExpressionException
        Evaluates this expression object in the context of the specified environment. The environment provides any external information necessary to evaluate the expression, such as the current tuples loaded from tables referenced within the expression.
        Parameters:
        env - the environment to look up symbol-values from, when evaluating the expression
        Returns:
        the result of the expression evaluation
        Throws:
        ExpressionException - if the expression cannot be evaluated for some reason.
      • evaluate

        public java.lang.Object evaluate()
                                  throws ExpressionException
        Evaluates this expression object without any environment. This method may only be used if the expression doesn't contain any symbolic values. If the expression does contain symbols then an exception will be thrown at evaluation time.
        Returns:
        the result of the expression evaluation
        Throws:
        ExpressionException - if the expression cannot be evaluated for some reason.
      • evaluatePredicate

        public boolean evaluatePredicate​(Environment env)
                                  throws ExpressionException
        Evaluates an expression as a Boolean predicate.
        Parameters:
        env - the environment that the predicate is evaluated within
        Returns:
        the result of the evaluation, converted to a Boolean value
        Throws:
        ExpressionException - if an error occurred during evaluation
      • evaluatePredicate

        public boolean evaluatePredicate()
                                  throws ExpressionException
        Evaluates an expression as a Boolean predicate without any environment. This method may only be used if the expression doesn't contain any symbolic values. If the expression does contain symbols then an exception will be thrown at evaluation time.
        Returns:
        the result of the evaluation, converted to a Boolean value
        Throws:
        ExpressionException - if an error occurred during evaluation
      • traverse

        public abstract Expression traverse​(ExpressionProcessor p)

        This method allows the entire expression tree to be traversed node by node, either for analysis or for transformation. The ExpressionProcessor instance receives notifications as each node in the expression is entered and left.

        The expression tree can also be manipulated by this traversal process, depending on what the ExpressionProcessor wants to do. If the expression node that traverse() is invoked on, needs to be replaced with a new expression node, the replacement is returned by the traverse() method. (The ExpressionProcessor specifies the replacement as the return-value from the ExpressionProcessor.leave(Expression) method.)

        Parameters:
        p - the object that performs analysis or transformation of the expression tree
        Returns:
        an Expression node to replace this node, or null if no changes are to be made.
      • hasSymbols

        public final boolean hasSymbols()
        Returns true if this expression contains any symbols that will require an environment to evaluate. If this method returns false then the expression contains only literals, and therefore can be evaluated without an environment.
        Returns:
        true if the expression contains any symbols, false otherwise
      • getAllSymbols

        public final void getAllSymbols​(java.util.Collection<ColumnName> symbols)
        This method stores all of the symbols in an expression into a collection, so that the expression's symbols can be validated against the schema that the expression will be evaluated against.

        Note that the specific kind of collection can be varied, to achieve different results. Callers that need every single symbol-reference can pass in a List implementation, while callers that only need to know what symbols are referenced could pass a Set implementation as the argument.

        Parameters:
        symbols - A collection that will receive all of the symbols in the expression.
      • simplify

        public Expression simplify()
        Returns an Expression reference to a (possibly) simplified version of this expression. If it's not possible to simplify the expression, or if the expression class doesn't support simplification, then the return-value will simply be this expression object, unsimplified.
        Returns:
        a reference to an expression, either a simplified version of this expression, or the original unmodified expression
      • equals

        public abstract boolean equals​(java.lang.Object obj)
        Checks if the argument is an expression with the same structure, but not necesarily the same references.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to which we are comparing
      • hashCode

        public abstract int hashCode()
        Computes the hashcode of an Expression. This method is used to see if two expressions CAN be equal.
        Overrides:
        hashCode in class java.lang.Object
      • clone

        protected java.lang.Object clone()
                                  throws java.lang.CloneNotSupportedException
        Creates a copy of expression.
        Overrides:
        clone in class java.lang.Object
        Throws:
        java.lang.CloneNotSupportedException
      • duplicate

        public Expression duplicate()
        Returns a deep copy of this expression.
        Returns:
        a deep copy of this expression