Top Description Fields Constructors Methods
org.h2.expression.condition

public final Class Comparison

extends Condition
Class Inheritance
Imports
java.util.ArrayList, org.h2.engine.SessionLocal, org.h2.expression.Expression, .ExpressionColumn, .ExpressionList, .ExpressionVisitor, .Parameter, .TypedValueExpression, .ValueExpression, org.h2.expression.aggregate.Aggregate, .AggregateType, org.h2.index.IndexCondition, org.h2.message.DbException, org.h2.table.Column, .ColumnResolver, .TableFilter, org.h2.value.DataType, .TypeInfo, .Value, .ValueBoolean, .ValueNull, .ValueRow

Example comparison expressions are ID=1, NAME=NAME, NAME IS NULL.
Authors
Thomas Mueller, Noel Grandin, Nicolas Fortin, Atelier SIG, IRSTV FR CNRS 24888

Field Summary

Modifier and TypeField and Description
public static final int
BIGGER

The comparison type meaning > as in ID>1.

public static final int
BIGGER_EQUAL

The comparison type meaning >= as in ID>=1.

pack-priv static final String[]
private int
public static final int
EQUAL

The comparison type meaning = as in ID=1.

public static final int
EQUAL_NULL_SAFE

The comparison type meaning ID IS NOT DISTINCT FROM 1.

public static final int
FALSE

This is a pseudo comparison type that is only used for index conditions.

public static final int
IN_ARRAY

This is a pseudo comparison type that is only used for index conditions.

public static final int
IN_LIST

This is a pseudo comparison type that is only used for index conditions.

public static final int
IN_QUERY

This is a pseudo comparison type that is only used for index conditions.

private Expression
public static final int
NOT_EQUAL

The comparison type meaning <> as in ID<>1.

public static final int
NOT_EQUAL_NULL_SAFE

The comparison type meaning ID IS DISTINCT FROM 1.

private Expression
public static final int
SMALLER

The comparison type meaning < as in ID<1.

public static final int
SMALLER_EQUAL

The comparison type meaning <= as in ID<=1.

public static final int
SPATIAL_INTERSECTS

This is a comparison type that is only used for spatial index conditions (operator "&&").

private final boolean

Constructor Summary

AccessConstructor and Description
public
Comparison(int compareType, Expression left, Expression right, boolean whenOperand)

Method Summary

Modifier and TypeMethod and Description
pack-priv static Value

Returns:

result of comparison, either TRUE, FALSE, or NULL
compare
(SessionLocal
the session
session
,
Value
the first value
l
,
Value
the second value
r
,
int
the compare type
compareType
)

Compare two values.

public void
createIndexConditions(SessionLocal
the session
session
,
TableFilter
the table filter
filter
)

Overrides org.h2.expression.Expression.createIndexConditions.

Create index conditions if possible and attach them to the table filter.

pack-priv static void
createIndexConditions(TableFilter filter, Expression left, Expression right, int compareType)

private static void
createIndexConditions(TableFilter filter, ExpressionList left, ExpressionList right, int compareType)

private static void
createIndexConditions(TableFilter filter, ExpressionList left, ValueExpression right, int compareType)

pack-priv Expression

Returns:

null or the third condition for indexes
getAdditionalAnd
(SessionLocal
the session
session
,
Comparison
the second condition
other
)

Get an additional condition if possible.

private static ConditionIn
public int
getCost()

Implements abstract org.h2.expression.Expression.getCost.

Estimate the cost to process the expression.

pack-priv Expression

Returns:

null if no match, the other expression if there is a match
getIfEquals
(Expression
the expression that should match
match
)

Get the other expression if this is an equals comparison and the other expression matches.

pack-priv static int
public Expression
getNotIfPossible(SessionLocal
the session
session
)

Overrides org.h2.expression.Expression.getNotIfPossible.

If it is possible, return the negated expression.

private static int
public Expression
getSubexpression(int
0-based index
index
)

Overrides org.h2.expression.Expression.getSubexpression.

Returns subexpression with specified index.

public int
public StringBuilder
getUnenclosedSQL(StringBuilder
string builder
builder
,
int
formatting flags
sqlFlags
)

Implements abstract org.h2.expression.Expression.getUnenclosedSQL.

Get the SQL statement of this expression.

public Value
getValue(SessionLocal
the session
session
)

Implements abstract org.h2.expression.Expression.getValue.

Return the resulting value for the current row.

public StringBuilder
getWhenSQL(StringBuilder
string builder
builder
,
int
formatting flags
sqlFlags
)

Overrides org.h2.expression.Expression.getWhenSQL.

Appends the SQL statement of this when operand to the specified builder.

public boolean
getWhenValue(SessionLocal
the session
session
,
Value
value on the left side
left
)

Overrides org.h2.expression.Expression.getWhenValue.

Return the resulting value of when operand for the current row.

public boolean
isEverything(ExpressionVisitor
the visitor
visitor
)

Implements abstract org.h2.expression.Expression.isEverything.

Check if this expression and all sub-expressions can fulfill a criteria.

public boolean
isWhenConditionOperand()

Overrides org.h2.expression.Expression.isWhenConditionOperand.

Returns whether this expression is a right side of condition in a when operand.

public void
mapColumns(ColumnResolver
the column resolver
resolver
,
int
the subquery nesting level
level
,
int
current state for nesting checks, initial value is MAP_INITIAL
state
)

Implements abstract org.h2.expression.Expression.mapColumns.

Map the columns of the resolver to expression columns.

public boolean
needParentheses()

Overrides org.h2.expression.Expression.needParentheses.

Returns whether this expressions needs to be wrapped in parentheses when it is used as an argument of other expressions.

public Expression
optimize(SessionLocal
the session
session
)

Implements abstract org.h2.expression.Expression.optimize.

Try to optimize the expression.

pack-priv Expression

Returns:

null or the joined IN condition
optimizeOr
(SessionLocal
the session
session
,
Comparison
the second condition
other
)

Replace the OR condition with IN condition if possible.

public void
setEvaluatable(TableFilter
the table filter
tableFilter
,
boolean
true if the table filter can return value
b
)

Implements abstract org.h2.expression.Expression.setEvaluatable.

Tell the expression columns whether the table filter can return values now.

public void
updateAggregate(SessionLocal
the session
session
,
int
select stage
stage
)

Implements abstract org.h2.expression.Expression.updateAggregate.

Update an aggregate value.

Inherited from org.h2.expression.condition.Condition:
castToBooleangetType

Field Detail

BIGGERback to summary
public static final int BIGGER

The comparison type meaning > as in ID>1.

BIGGER_EQUALback to summary
public static final int BIGGER_EQUAL

The comparison type meaning >= as in ID>=1.

COMPARE_TYPESback to summary
pack-priv static final String[] COMPARE_TYPES
compareTypeback to summary
private int compareType
EQUALback to summary
public static final int EQUAL

The comparison type meaning = as in ID=1.

EQUAL_NULL_SAFEback to summary
public static final int EQUAL_NULL_SAFE

The comparison type meaning ID IS NOT DISTINCT FROM 1.

FALSEback to summary
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.

IN_ARRAYback to summary
public static final int IN_ARRAY

This is a pseudo comparison type that is only used for index conditions. It means equals any value of an ARRAY. Example: ARRAY[1, 2, 3].

IN_LISTback to summary
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).

IN_QUERYback to summary
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 ...).

leftback to summary
private Expression left
NOT_EQUALback to summary
public static final int NOT_EQUAL

The comparison type meaning <> as in ID<>1.

NOT_EQUAL_NULL_SAFEback to summary
public static final int NOT_EQUAL_NULL_SAFE

The comparison type meaning ID IS DISTINCT FROM 1.

SMALLERback to summary
public static final int SMALLER

The comparison type meaning < as in ID<1.

SMALLER_EQUALback to summary
public static final int SMALLER_EQUAL

The comparison type meaning <= as in ID<=1.

SPATIAL_INTERSECTSback to summary
public static final int SPATIAL_INTERSECTS

This is a comparison type that is only used for spatial index conditions (operator "&&").

whenOperandback to summary
private final boolean whenOperand

Constructor Detail

Comparisonback to summary
public Comparison(int compareType, Expression left, Expression right, boolean whenOperand)

Method Detail

compareback to summary
pack-priv static Value compare(SessionLocal session, Value l, Value r, int compareType)

Compare two values.

Parameters
session:SessionLocal

the session

l:Value

the first value

r:Value

the second value

compareType:int

the compare type

Returns:Value

result of comparison, either TRUE, FALSE, or NULL

createIndexConditionsback to summary
public void createIndexConditions(SessionLocal session, TableFilter filter)

Overrides org.h2.expression.Expression.createIndexConditions.

Doc from org.h2.expression.Expression.createIndexConditions.

Create index conditions if possible and attach them to the table filter.

Parameters
session:SessionLocal

the session

filter:TableFilter

the table filter

Annotations
@Override
createIndexConditionsback to summary
pack-priv static void createIndexConditions(TableFilter filter, Expression left, Expression right, int compareType)
createIndexConditionsback to summary
private static void createIndexConditions(TableFilter filter, ExpressionList left, ExpressionList right, int compareType)
createIndexConditionsback to summary
private static void createIndexConditions(TableFilter filter, ExpressionList left, ValueExpression right, int compareType)
getAdditionalAndback to summary
pack-priv 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:SessionLocal

the session

other:Comparison

the second condition

Returns:Expression

null or the third condition for indexes

getConditionInback to summary
private static ConditionIn getConditionIn(Expression left, Expression value1, Expression value2)
getCostback to summary
public int getCost()

Implements abstract org.h2.expression.Expression.getCost.

Doc from org.h2.expression.Expression.getCost.

Estimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.

Returns:int

the estimated cost

Annotations
@Override
getIfEqualsback to summary
pack-priv Expression getIfEquals(Expression match)

Get the other expression if this is an equals comparison and the other expression matches.

Parameters
match:Expression

the expression that should match

Returns:Expression

null if no match, the other expression if there is a match

getNotCompareTypeback to summary
pack-priv static int getNotCompareType(int type)
getNotIfPossibleback to summary
public Expression getNotIfPossible(SessionLocal session)

Overrides org.h2.expression.Expression.getNotIfPossible.

Doc from org.h2.expression.Expression.getNotIfPossible.

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.

Parameters
session:SessionLocal

the session

Returns:Expression

the negated expression, or null

Annotations
@Override
getReversedCompareTypeback to summary
private static int getReversedCompareType(int type)
getSubexpressionback to summary
public Expression getSubexpression(int index)

Overrides org.h2.expression.Expression.getSubexpression.

Doc from org.h2.expression.Expression.getSubexpression.

Returns subexpression with specified index.

Parameters
index:int

0-based index

Returns:Expression

subexpression with specified index, may be null

Annotations
@Override
getSubexpressionCountback to summary
public int getSubexpressionCount()

Overrides org.h2.expression.Expression.getSubexpressionCount.

Doc from org.h2.expression.Expression.getSubexpressionCount.

Returns count of subexpressions.

Returns:int

count of subexpressions

Annotations
@Override
getUnenclosedSQLback to summary
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags)

Implements abstract org.h2.expression.Expression.getUnenclosedSQL.

Doc from org.h2.expression.Expression.getUnenclosedSQL.

Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are never appended.

Parameters
builder:StringBuilder

string builder

sqlFlags:int

formatting flags

Returns:StringBuilder

the specified string builder

Annotations
@Override
getValueback to summary
public Value getValue(SessionLocal session)

Implements abstract org.h2.expression.Expression.getValue.

Doc from org.h2.expression.Expression.getValue.

Return the resulting value for the current row.

Parameters
session:SessionLocal

the session

Returns:Value

the result

Annotations
@Override
getWhenSQLback to summary
public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags)

Overrides org.h2.expression.Expression.getWhenSQL.

Doc from org.h2.expression.Expression.getWhenSQL.

Appends the SQL statement of this when operand to the specified builder.

Parameters
builder:StringBuilder

string builder

sqlFlags:int

formatting flags

Returns:StringBuilder

the specified string builder

Annotations
@Override
getWhenValueback to summary
public boolean getWhenValue(SessionLocal session, Value left)

Overrides org.h2.expression.Expression.getWhenValue.

Doc from org.h2.expression.Expression.getWhenValue.

Return the resulting value of when operand for the current row.

Parameters
session:SessionLocal

the session

left:Value

value on the left side

Returns:boolean

the result

Annotations
@Override
isEverythingback to summary
public boolean isEverything(ExpressionVisitor visitor)

Implements abstract org.h2.expression.Expression.isEverything.

Doc from org.h2.expression.Expression.isEverything.

Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.

Parameters
visitor:ExpressionVisitor

the visitor

Returns:boolean

if the criteria can be fulfilled

Annotations
@Override
isWhenConditionOperandback to summary
public boolean isWhenConditionOperand()

Overrides org.h2.expression.Expression.isWhenConditionOperand.

Doc from org.h2.expression.Expression.isWhenConditionOperand.

Returns whether this expression is a right side of condition in a when operand.

Returns:boolean

true if it is, false otherwise

Annotations
@Override
mapColumnsback to summary
public void mapColumns(ColumnResolver resolver, int level, int state)

Implements abstract org.h2.expression.Expression.mapColumns.

Doc from org.h2.expression.Expression.mapColumns.

Map the columns of the resolver to expression columns.

Parameters
resolver:ColumnResolver

the column resolver

level:int

the subquery nesting level

state:int

current state for nesting checks, initial value is MAP_INITIAL

Annotations
@Override
needParenthesesback to summary
public boolean needParentheses()

Overrides org.h2.expression.Expression.needParentheses.

Doc from org.h2.expression.Expression.needParentheses.

Returns whether this expressions needs to be wrapped in parentheses when it is used as an argument of other expressions.

Returns:boolean

true if it is

Annotations
@Override
optimizeback to summary
public Expression optimize(SessionLocal session)

Implements abstract org.h2.expression.Expression.optimize.

Doc from org.h2.expression.Expression.optimize.

Try to optimize the expression.

Parameters
session:SessionLocal

the session

Returns:Expression

the optimized expression

Annotations
@Override
optimizeOrback to summary
pack-priv 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:SessionLocal

the session

other:Comparison

the second condition

Returns:Expression

null or the joined IN condition

setEvaluatableback to summary
public void setEvaluatable(TableFilter tableFilter, boolean b)

Implements abstract org.h2.expression.Expression.setEvaluatable.

Doc from org.h2.expression.Expression.setEvaluatable.

Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.

Parameters
tableFilter:TableFilter

the table filter

b:boolean

true if the table filter can return value

Annotations
@Override
updateAggregateback to summary
public void updateAggregate(SessionLocal session, int stage)

Implements abstract org.h2.expression.Expression.updateAggregate.

Doc from org.h2.expression.Expression.updateAggregate.

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.

Parameters
session:SessionLocal

the session

stage:int

select stage

Annotations
@Override