Class Comparison

  • All Implemented Interfaces:
    HasSQL, Typed

    public final class Comparison
    extends Condition
    Example comparison expressions are ID=1, NAME=NAME, NAME IS NULL.
    • Field Detail

      • EQUAL

        public static final int EQUAL
        The comparison type meaning = as in ID=1.
        See Also:
        Constant Field Values
      • NOT_EQUAL

        public static final int NOT_EQUAL
        The comparison type meaning <> as in ID<>1.
        See Also:
        Constant Field Values
      • SMALLER

        public static final int SMALLER
        The comparison type meaning < as in ID<1.
        See Also:
        Constant Field Values
      • BIGGER

        public static final int BIGGER
        The comparison type meaning > as in ID>1.
        See Also:
        Constant Field Values
      • SMALLER_EQUAL

        public static final int SMALLER_EQUAL
        The comparison type meaning <= as in ID<=1.
        See Also:
        Constant Field Values
      • BIGGER_EQUAL

        public static final int BIGGER_EQUAL
        The comparison type meaning >= as in ID>=1.
        See Also:
        Constant Field Values
      • EQUAL_NULL_SAFE

        public static final int EQUAL_NULL_SAFE
        The comparison type meaning ID IS NOT DISTINCT FROM 1.
        See Also:
        Constant Field Values
      • NOT_EQUAL_NULL_SAFE

        public static final int NOT_EQUAL_NULL_SAFE
        The comparison type meaning ID IS DISTINCT FROM 1.
        See Also:
        Constant Field Values
      • SPATIAL_INTERSECTS

        public static final int SPATIAL_INTERSECTS
        This is a comparison type that is only used for spatial index conditions (operator "&&").
        See Also:
        Constant Field Values
      • COMPARE_TYPES

        static final java.lang.String[] COMPARE_TYPES
      • FALSE

        public static final int FALSE
        This is a pseudo comparison type that is only used for index conditions. It means the comparison will always yield FALSE. Example: 1=0.
        See Also:
        Constant Field Values
      • IN_LIST

        public static final int IN_LIST
        This is a pseudo comparison type that is only used for index conditions. It means equals any value of a list. Example: IN(1, 2, 3).
        See Also:
        Constant Field Values
      • IN_QUERY

        public static final int IN_QUERY
        This is a pseudo comparison type that is only used for index conditions. It means equals any value of a list. Example: IN(SELECT ...).
        See Also:
        Constant Field Values
      • compareType

        private int compareType
      • whenOperand

        private final boolean whenOperand
    • Constructor Detail

      • Comparison

        public Comparison​(int compareType,
                          Expression left,
                          Expression right,
                          boolean whenOperand)
    • Method Detail

      • needParentheses

        public boolean needParentheses()
        Description copied from class: Expression
        Returns whether this expressions needs to be wrapped in parentheses when it is used as an argument of other expressions.
        Overrides:
        needParentheses in class Expression
        Returns:
        true if it is
      • getUnenclosedSQL

        public java.lang.StringBuilder getUnenclosedSQL​(java.lang.StringBuilder builder,
                                                        int sqlFlags)
        Description copied from class: Expression
        Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are never appended.
        Specified by:
        getUnenclosedSQL in class Expression
        Parameters:
        builder - string builder
        sqlFlags - formatting flags
        Returns:
        the specified string builder
      • getWhenSQL

        public java.lang.StringBuilder getWhenSQL​(java.lang.StringBuilder builder,
                                                  int sqlFlags)
        Description copied from class: Expression
        Appends the SQL statement of this when operand to the specified builder.
        Overrides:
        getWhenSQL in class Expression
        Parameters:
        builder - string builder
        sqlFlags - formatting flags
        Returns:
        the specified string builder
      • optimize

        public Expression optimize​(SessionLocal session)
        Description copied from class: Expression
        Try to optimize the expression.
        Specified by:
        optimize in class Expression
        Parameters:
        session - the session
        Returns:
        the optimized expression
      • getValue

        public Value getValue​(SessionLocal session)
        Description copied from class: Expression
        Return the resulting value for the current row.
        Specified by:
        getValue in class Expression
        Parameters:
        session - the session
        Returns:
        the result
      • getWhenValue

        public boolean getWhenValue​(SessionLocal session,
                                    Value left)
        Description copied from class: Expression
        Return the resulting value of when operand for the current row.
        Overrides:
        getWhenValue in class Expression
        Parameters:
        session - the session
        left - value on the left side
        Returns:
        the result
      • compare

        static Value compare​(SessionLocal session,
                             Value l,
                             Value r,
                             int compareType)
        Compare two values.
        Parameters:
        session - the session
        l - the first value
        r - the second value
        compareType - the compare type
        Returns:
        result of comparison, either TRUE, FALSE, or NULL
      • isWhenConditionOperand

        public boolean isWhenConditionOperand()
        Description copied from class: Expression
        Returns whether this expression is a right side of condition in a when operand.
        Overrides:
        isWhenConditionOperand in class Expression
        Returns:
        true if it is, false otherwise
      • getReversedCompareType

        private static int getReversedCompareType​(int type)
      • getNotIfPossible

        public Expression getNotIfPossible​(SessionLocal session)
        Description copied from class: Expression
        If it is possible, return the negated expression. This is used to optimize NOT expressions: NOT ID>10 can be converted to ID<=10. Returns null if negating is not possible.
        Overrides:
        getNotIfPossible in class Expression
        Parameters:
        session - the session
        Returns:
        the negated expression, or null
      • getNotCompareType

        private int getNotCompareType()
      • createIndexConditions

        public void createIndexConditions​(SessionLocal session,
                                          TableFilter filter)
        Description copied from class: Expression
        Create index conditions if possible and attach them to the table filter.
        Overrides:
        createIndexConditions in class Expression
        Parameters:
        session - the session
        filter - the table filter
      • setEvaluatable

        public void setEvaluatable​(TableFilter tableFilter,
                                   boolean b)
        Description copied from class: Expression
        Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.
        Specified by:
        setEvaluatable in class Expression
        Parameters:
        tableFilter - the table filter
        b - true if the table filter can return value
      • updateAggregate

        public void updateAggregate​(SessionLocal session,
                                    int stage)
        Description copied from class: Expression
        Update an aggregate value. This method is called at statement execution time. It is usually called once for each row, but if the expression is used multiple times (for example in the column list, and as part of the HAVING expression) it is called multiple times - the row counter needs to be used to make sure the internal state is only updated once.
        Specified by:
        updateAggregate in class Expression
        Parameters:
        session - the session
        stage - select stage
      • mapColumns

        public void mapColumns​(ColumnResolver resolver,
                               int level,
                               int state)
        Description copied from class: Expression
        Map the columns of the resolver to expression columns.
        Specified by:
        mapColumns in class Expression
        Parameters:
        resolver - the column resolver
        level - the subquery nesting level
        state - current state for nesting checks, initial value is Expression.MAP_INITIAL
      • isEverything

        public boolean isEverything​(ExpressionVisitor visitor)
        Description copied from class: Expression
        Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.
        Specified by:
        isEverything in class Expression
        Parameters:
        visitor - the visitor
        Returns:
        if the criteria can be fulfilled
      • getCost

        public int getCost()
        Description copied from class: Expression
        Estimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.
        Specified by:
        getCost in class Expression
        Returns:
        the estimated cost
      • getIfEquals

        Expression getIfEquals​(Expression match)
        Get the other expression if this is an equals comparison and the other expression matches.
        Parameters:
        match - the expression that should match
        Returns:
        null if no match, the other expression if there is a match
      • getAdditionalAnd

        Expression getAdditionalAnd​(SessionLocal session,
                                    Comparison other)
        Get an additional condition if possible. Example: given two conditions A=B AND B=C, the new condition A=C is returned.
        Parameters:
        session - the session
        other - the second condition
        Returns:
        null or the third condition for indexes
      • optimizeOr

        Expression optimizeOr​(SessionLocal session,
                              Comparison other)
        Replace the OR condition with IN condition if possible. Example: given the two conditions A=1 OR A=2, the new condition A IN(1, 2) is returned.
        Parameters:
        session - the session
        other - the second condition
        Returns:
        null or the joined IN condition
      • getSubexpressionCount

        public int getSubexpressionCount()
        Description copied from class: Expression
        Returns count of subexpressions.
        Overrides:
        getSubexpressionCount in class Expression
        Returns:
        count of subexpressions
      • getSubexpression

        public Expression getSubexpression​(int index)
        Description copied from class: Expression
        Returns subexpression with specified index.
        Overrides:
        getSubexpression in class Expression
        Parameters:
        index - 0-based index
        Returns:
        subexpression with specified index, may be null