Package org.h2.expression

# Class Expression

java.lang.Object
org.h2.expression.Expression

All Implemented Interfaces:
  `HasSQL`, `Typed`

Direct Known Subclasses:
  `Alias`, `ArrayConstructorByQuery`, `BetweenPredicate`, `CompareLike`, `Comparison`, `ConditionAndOr`, `ConditionAndOrN`, `ConditionIn`, `ConditionInArray`, `ConditionInConstantSet`, `ConditionInQuery`, `ConditionLocalAndGlobal`, `ConditionNot`, `DataAnalysisOperation`, `ExistsPredicate`, `ExpressionColumn`, `ExpressionList`, `Function0_1`, `IsJsonPredicate`, `JavaFunction`, `Operation0`, `Operation1`, `Operation1_2`, `Operation2`, `OperationN`, `SimpleCase`, `SimplePredicate`, `Subquery`, `UniquePredicate`, `Wildcard`

public abstract class Expression extends Object implements HasSQL, Typed
An expression is a operation, a value, or a function in a query.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`AUTO_PARENTHESES`
Wrap expression in parentheses only if it can't be safely included into other expressions without them.
`static final int`
`MAP_IN_AGGREGATE`
State for expressions inside an aggregate for `mapColumns(ColumnResolver, int, int)`.
`static final int`
`MAP_IN_WINDOW`
State for expressions inside a window function for `mapColumns(ColumnResolver, int, int)`.
`static final int`
`MAP_INITIAL`
Initial state for `mapColumns(ColumnResolver, int, int)`.
`static final int`
`WITH_PARENTHESES`
Wrap expression in parentheses unconditionally.
`static final int`
`WITHOUT_PARENTHESES`
Do not wrap expression in parentheses.

### Fields inherited from interface org.h2.util.HasSQL

`ADD_PLAN_INFORMATION, DEFAULT_SQL_FLAGS, NO_CASTS, QUOTE_ONLY_WHEN_REQUIRED, REPLACE_LOBS_FOR_TRACE, TRACE_SQL_FLAGS`

## Constructor Summary

Constructors
Constructor
Description
`Expression()`

## Method Summary

Modifier and Type
Method
Description
`void`
`addFilterConditions(TableFilter filter)`
Add conditions to a table filter if they can be evaluated.
`void`
`createIndexConditions(SessionLocal session, TableFilter filter)`
Create index conditions if possible and attach them to the table filter.
`String`
`getAlias(SessionLocal session, int columnIndex)`
Get the alias name of a column or SQL expression if it is not an aliased expression.
`boolean`
`getBooleanValue(SessionLocal session)`
Get the value in form of a boolean expression.
`String`
`getColumnName(SessionLocal session, int columnIndex)`
Get the column name or alias name of this expression.
`String`
`getColumnNameForView(SessionLocal session, int columnIndex)`
Get the column name of this expression for a view.
`abstract int`
`getCost()`
Estimate the cost to process the expression.
`final StringBuilder`
`getEnclosedSQL(StringBuilder builder, int sqlFlags)`
Get the SQL statement of this expression.
`Expression`
`getNonAliasExpression()`
Returns the main expression, skipping aliases.
`Expression`
`getNotIfPossible(SessionLocal session)`
If it is possible, return the negated expression.
`int`
`getNullable()`
Check whether this expression is a column and can store NULL.
`String`
`getSchemaName()`
Get the schema name, or null
`final String`
`getSQL(int sqlFlags)`
Get the SQL statement of this expression.
`final String`
`getSQL(int sqlFlags, int parentheses)`
Get the SQL statement of this expression.
`final StringBuilder`
`getSQL(StringBuilder builder, int sqlFlags)`
Appends the SQL statement of this object to the specified builder.
`final StringBuilder`
`getSQL(StringBuilder builder, int sqlFlags, int parentheses)`
Get the SQL statement of this expression.
`Expression`
`getSubexpression(int index)`
Returns subexpression with specified index.
`int`
`getSubexpressionCount()`
Returns count of subexpressions.
`String`
`getTableAlias()`
Get the table alias name or null if this expression does not represent a column.
`String`
`getTableName()`
Get the table name, or null
`abstract TypeInfo`
`getType()`
Returns the data type.
`TypeInfo`
`getTypeIfStaticallyKnown(SessionLocal session)`
Returns data type of this expression if it is statically known.
`abstract StringBuilder`
`getUnenclosedSQL(StringBuilder builder, int sqlFlags)`
Get the SQL statement of this expression.
`abstract Value`
`getValue(SessionLocal session)`
Return the resulting value for the current row.
`StringBuilder`
`getWhenSQL(StringBuilder builder, int sqlFlags)`
Appends the SQL statement of this when operand to the specified builder.
`boolean`
`getWhenValue(SessionLocal session, Value left)`
Return the resulting value of when operand for the current row.
`boolean`
`isConstant()`
Check if this expression will always return the same value.
`abstract boolean`
`isEverything(ExpressionVisitor visitor)`
Check if this expression and all sub-expressions can fulfill a criteria.
`boolean`
`isIdentity()`
Check if this is an identity column.
`boolean`
`isNullConstant()`
Check if this expression will always return the NULL value.
`boolean`
`isValueSet()`
Is the value of a parameter set.
`boolean`
`isWhenConditionOperand()`
Returns whether this expression is a right side of condition in a when operand.
`abstract void`
`mapColumns(ColumnResolver resolver, int level, int state)`
Map the columns of the resolver to expression columns.
`boolean`
`needParentheses()`
Returns whether this expressions needs to be wrapped in parentheses when it is used as an argument of other expressions.
`abstract Expression`
`optimize(SessionLocal session)`
Try to optimize the expression.
`final Expression`
`optimizeCondition(SessionLocal session)`
Try to optimize or remove the condition.
`abstract void`
`setEvaluatable(TableFilter tableFilter, boolean value)`
Tell the expression columns whether the table filter can return values now.
`String`
`toString()`
Convert this expression to a String.
`abstract void`
`updateAggregate(SessionLocal session, int stage)`
Update an aggregate value.
`static StringBuilder`
`writeExpressions(StringBuilder builder, List<? extends Expression> expressions, int sqlFlags)`
Get the SQL snippet for a list of expressions.
`static StringBuilder`
`writeExpressions(StringBuilder builder, Expression[] expressions, int sqlFlags)`
Get the SQL snippet for an array of expressions.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.h2.util.HasSQL

`getTraceSQL`

## Field Details

### MAP_INITIAL

public static final int MAP_INITIAL
Initial state for `mapColumns(ColumnResolver, int, int)`.

See Also:
  - Constant Field Values

### MAP_IN_WINDOW

public static final int MAP_IN_WINDOW
State for expressions inside a window function for `mapColumns(ColumnResolver, int, int)`.

See Also:
  - Constant Field Values

### MAP_IN_AGGREGATE

public static final int MAP_IN_AGGREGATE
State for expressions inside an aggregate for `mapColumns(ColumnResolver, int, int)`.

See Also:
  - Constant Field Values

### AUTO_PARENTHESES

public static final int AUTO_PARENTHESES
Wrap expression in parentheses only if it can't be safely included into other expressions without them.

See Also:
  - Constant Field Values

### WITH_PARENTHESES

public static final int WITH_PARENTHESES
Wrap expression in parentheses unconditionally.

See Also:
  - Constant Field Values

### WITHOUT_PARENTHESES

public static final int WITHOUT_PARENTHESES
Do not wrap expression in parentheses.

See Also:
  - Constant Field Values

## Constructor Details

### Expression

public Expression()

## Method Details

### writeExpressions

public static StringBuilder writeExpressions(StringBuilder builder, List<? extends Expression> expressions, int sqlFlags)
Get the SQL snippet for a list of expressions.

Parameters:
  `builder` - the builder to append the SQL to
  `expressions` - the list of expressions
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

### writeExpressions

public static StringBuilder writeExpressions(StringBuilder builder, Expression[] expressions, int sqlFlags)
Get the SQL snippet for an array of expressions.

Parameters:
  `builder` - the builder to append the SQL to
  `expressions` - the list of expressions
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

### getValue

public abstract Value getValue(SessionLocal session)
Return the resulting value for the current row.

Parameters:
  `session` - the session
Returns:
  the result

### getType

public abstract TypeInfo getType()
Returns the data type. The data type may be unknown before the optimization phase.

Specified by:
  `getType` in interface `Typed`
Returns:
  the data type

### mapColumns

public abstract void mapColumns(ColumnResolver resolver, int level, int state)
Map the columns of the resolver to expression columns.

Parameters:
  `resolver` - the column resolver
  `level` - the subquery nesting level
  `state` - current state for nesting checks, initial value is `MAP_INITIAL`

### optimize

public abstract Expression optimize(SessionLocal session)
Try to optimize the expression.

Parameters:
  `session` - the session
Returns:
  the optimized expression

### optimizeCondition

public final Expression optimizeCondition(SessionLocal session)
Try to optimize or remove the condition.

Parameters:
  `session` - the session
Returns:
  the optimized condition, or `null`

### setEvaluatable

public abstract void setEvaluatable(TableFilter tableFilter, boolean value)
Tell the expression columns whether the table filter can return values now. This is used when optimizing the query.

Parameters:
  `tableFilter` - the table filter
  `value` - true if the table filter can return value

### getSQL

public final String getSQL(int sqlFlags)
Description copied from interface: `HasSQL`
Get the SQL statement of this expression. This may not always be the original SQL statement, specially after optimization.

Specified by:
  `getSQL` in interface `HasSQL`
Parameters:
  `sqlFlags` - formatting flags
Returns:
  the SQL statement

### getSQL

public final StringBuilder getSQL(StringBuilder builder, int sqlFlags)
Description copied from interface: `HasSQL`
Appends the SQL statement of this object to the specified builder.

Specified by:
  `getSQL` in interface `HasSQL`
Parameters:
  `builder` - string builder
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

### getSQL

public final String getSQL(int sqlFlags, int parentheses)
Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization.

Parameters:
  `sqlFlags` - formatting flags
  `parentheses` - parentheses mode
Returns:
  the SQL statement

### getSQL

public final StringBuilder getSQL(StringBuilder builder, int sqlFlags, int parentheses)
Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization.

Parameters:
  `builder` - string builder
  `sqlFlags` - formatting flags
  `parentheses` - parentheses mode
Returns:
  the specified string builder

### needParentheses

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

Returns:
  `true` if it is

### getEnclosedSQL

public final StringBuilder getEnclosedSQL(StringBuilder builder, int sqlFlags)
Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are always appended.

Parameters:
  `builder` - string builder
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

### getUnenclosedSQL

public abstract StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags)
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` - string builder
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

### updateAggregate

public abstract void updateAggregate(SessionLocal session, int stage)
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` - the session
  `stage` - select stage

### isEverything

public abstract boolean isEverything(ExpressionVisitor visitor)
Check if this expression and all sub-expressions can fulfill a criteria. If any part returns false, the result is false.

Parameters:
  `visitor` - the visitor
Returns:
  if the criteria can be fulfilled

### getCost

public abstract int getCost()
Estimate the cost to process the expression. Used when optimizing the query, to calculate the query plan with the lowest estimated cost.

Returns:
  the estimated cost

### getNotIfPossible

public Expression getNotIfPossible(SessionLocal session)
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` - the session
Returns:
  the negated expression, or null

### getTypeIfStaticallyKnown

public TypeInfo getTypeIfStaticallyKnown(SessionLocal session)
Returns data type of this expression if it is statically known.

Parameters:
  `session` - the session
Returns:
  data type or `null`

### isConstant

public boolean isConstant()
Check if this expression will always return the same value.

Returns:
  if the expression is constant

### isNullConstant

public boolean isNullConstant()
Check if this expression will always return the NULL value.

Returns:
  if the expression is constant NULL value

### isValueSet

public boolean isValueSet()
Is the value of a parameter set.

Returns:
  true if set

### isIdentity

public boolean isIdentity()
Check if this is an identity column.

Returns:
  true if it is an identity column

### getBooleanValue

public boolean getBooleanValue(SessionLocal session)
Get the value in form of a boolean expression. Returns true or false. In this database, everything can be a condition.

Parameters:
  `session` - the session
Returns:
  the result

### createIndexConditions

public void createIndexConditions(SessionLocal session, TableFilter filter)
Create index conditions if possible and attach them to the table filter.

Parameters:
  `session` - the session
  `filter` - the table filter

### getColumnName

public String getColumnName(SessionLocal session, int columnIndex)
Get the column name or alias name of this expression.

Parameters:
  `session` - the session
  `columnIndex` - 0-based column index
Returns:
  the column name

### getSchemaName

public String getSchemaName()
Get the schema name, or null

Returns:
  the schema name

### getTableName

public String getTableName()
Get the table name, or null

Returns:
  the table name

### getNullable

public int getNullable()
Check whether this expression is a column and can store NULL.

Returns:
  whether NULL is allowed

### getTableAlias

public String getTableAlias()
Get the table alias name or null if this expression does not represent a column.

Returns:
  the table alias name

### getAlias

public String getAlias(SessionLocal session, int columnIndex)
Get the alias name of a column or SQL expression if it is not an aliased expression.

Parameters:
  `session` - the session
  `columnIndex` - 0-based column index
Returns:
  the alias name

### getColumnNameForView

public String getColumnNameForView(SessionLocal session, int columnIndex)
Get the column name of this expression for a view.

Parameters:
  `session` - the session
  `columnIndex` - 0-based column index
Returns:
  the column name for a view

### getNonAliasExpression

public Expression getNonAliasExpression()
Returns the main expression, skipping aliases.

Returns:
  the expression

### addFilterConditions

public void addFilterConditions(TableFilter filter)
Add conditions to a table filter if they can be evaluated.

Parameters:
  `filter` - the table filter

### toString

public String toString()
Convert this expression to a String.

Overrides:
  `toString` in class `Object`
Returns:
  the string representation

### getSubexpressionCount

public int getSubexpressionCount()
Returns count of subexpressions.

Returns:
  count of subexpressions

### getSubexpression

public Expression getSubexpression(int index)
Returns subexpression with specified index.

Parameters:
  `index` - 0-based index
Returns:
  subexpression with specified index, may be null
Throws:
  `IndexOutOfBoundsException` - if specified index is not valid

### getWhenValue

public boolean getWhenValue(SessionLocal session, Value left)
Return the resulting value of when operand for the current row.

Parameters:
  `session` - the session
  `left` - value on the left side
Returns:
  the result

### getWhenSQL

public StringBuilder getWhenSQL(StringBuilder builder, int sqlFlags)
Appends the SQL statement of this when operand to the specified builder.

Parameters:
  `builder` - string builder
  `sqlFlags` - formatting flags
Returns:
  the specified string builder

### isWhenConditionOperand

public boolean isWhenConditionOperand()
Returns whether this expression is a right side of condition in a when operand.

Returns:
  `true` if it is, `false` otherwise