Package org.h2.table
Class TableFilter
- java.lang.Object
-
- org.h2.table.TableFilter
-
- All Implemented Interfaces:
ColumnResolver
public class TableFilter extends java.lang.Object implements ColumnResolver
A table filter represents a table that is used in a query. There is one such object whenever a table (or view) is used in a query. For example the following query has 2 table filters: SELECT * FROM TEST T1, TEST T2.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TableFilter.MapColumnsVisitor
A visitor that maps columns.static interface
TableFilter.TableFilterVisitor
A visitor for table filters.
-
Field Summary
Fields Modifier and Type Field Description private static int
AFTER_LAST
private java.lang.String
alias
private static int
BEFORE_FIRST
private java.util.LinkedHashMap<Column,Column>
commonJoinColumns
Map of common join columns, used for NATURAL joins and USING clause of other joins.private TableFilter
commonJoinColumnsFilter
private java.util.ArrayList<Column>
commonJoinColumnsToExclude
private Row
current
private SearchRow
currentSearchRow
private IndexCursor
cursor
The filter used to walk through the index.private java.util.LinkedHashMap<Column,java.lang.String>
derivedColumnMap
Map of derived column names.private boolean
evaluatable
private Expression
filterCondition
Additional conditions that can't be used for index lookup, but for row filter for this table (ID=ID, NAME LIKE '%X%')private static int
FOUND
private boolean
foundOne
private Expression
fullCondition
private int
hashCode
private Index
index
private java.util.ArrayList<IndexCondition>
indexConditions
The index conditions used for direct index lookup (start or end).private IndexHints
indexHints
private static TableFilter.TableFilterVisitor
JOI_VISITOR
A visitor that sets joinOuterIndirect to true.private TableFilter
join
The joined table (if there is one).private Expression
joinCondition
The complete join condition.private boolean
joinOuter
Whether this is an outer join.protected boolean
joinOuterIndirect
Whether this is a direct or indirect (nested) outer joinprivate int[]
masks
private TableFilter
nestedJoin
The nested joined table (if there is one).private static int
NULL_ROW
static java.util.Comparator<TableFilter>
ORDER_IN_FROM_COMPARATOR
Comparator that uses order in FROM clause as a sort key.private int
orderInFrom
private int
scanCount
private Select
select
private SessionLocal
session
private int
state
private Table
table
private boolean
used
Indicates that this filter is used in the plan.
-
Constructor Summary
Constructors Constructor Description TableFilter(SessionLocal session, Table table, java.lang.String alias, boolean rightsChecked, Select select, int orderInFrom, IndexHints indexHints)
Create a new table filter object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCommonJoinColumns(Column leftColumn, Column replacementColumn, TableFilter replacementFilter)
Add a column to the common join column list for a left table filter.void
addCommonJoinColumnToExclude(Column columnToExclude)
Add an excluded column to the common join column list.void
addFilterCondition(Expression condition, boolean isJoin)
Add a filter condition.void
addIndexCondition(IndexCondition condition)
Add an index condition.void
addJoin(TableFilter filter, boolean outer, Expression on)
Add a joined table.private void
checkTimeout()
void
createIndexConditions()
Create the index conditions for this filter if needed.Column
findColumn(java.lang.String name)
Get the column with the specified name.Row
get()
Get the current row.PlanItem
getBestPlanItem(SessionLocal s, TableFilter[] filters, int filter, AllColumnsForPlan allColumnsSet)
Get the best plan item (index, cost) to use for the current join order.Column
getColumn(java.lang.String columnName, boolean ifExists)
Get the column with the given name.java.lang.String
getColumnName(Column column)
Get the name of the specified column.Column[]
getColumns()
Get the column list.java.util.LinkedHashMap<Column,Column>
getCommonJoinColumns()
Returns common join columns map.TableFilter
getCommonJoinColumnsFilter()
Returns common join columns table filter.private Value
getDelegatedValue(Column column)
Expression
getFilterCondition()
Index
getIndex()
java.util.ArrayList<IndexCondition>
getIndexConditions()
IndexCursor
getIndexCursor()
IndexHints
getIndexHints()
TableFilter
getJoin()
Expression
getJoinCondition()
int[]
getMasks()
TableFilter
getNestedJoin()
int
getOrderInFrom()
Get the order number (index) of this table filter in the "from" clause of the query.java.lang.StringBuilder
getPlanSQL(java.lang.StringBuilder builder, boolean isJoin, int sqlFlags)
Get the query execution plan text to use for this table filter and append it to the specified builder.Column
getRowIdColumn()
Get the row id pseudo column, if there is one.java.lang.String
getSchemaName()
Get the schema name or null.Select
getSelect()
Get the select statement.SessionLocal
getSession()
Column[]
getSystemColumns()
Get the system columns that this table understands.Table
getTable()
java.lang.String
getTableAlias()
Get the table alias name.TableFilter
getTableFilter()
Get the table filter.Value
getValue(Column column)
Get the value for the given column.boolean
hasDerivedColumnList()
Returns whether this column resolver has a derived column list.int
hashCode()
boolean
hasInComparisons()
Are there any index conditions that involve IN(...).boolean
isCommonJoinColumnToExclude(Column c)
Check if the given column is an excluded common join column.boolean
isEvaluatable()
boolean
isJoinOuter()
Whether this is an outer joined table.boolean
isJoinOuterIndirect()
Whether this is indirectly an outer joined table (nested within an inner join).boolean
isNoFromClauseFilter()
Returns whether this is a table filter with implicit DUAL table for a SELECT without a FROM clause.boolean
isNullRow()
(package private) boolean
isOk(Expression condition)
Whether the current value of the condition is true, or there is no condition.boolean
isUsed()
void
lock(SessionLocal s)
Lock the table.void
mapAndAddFilter(Expression on)
Map the columns and add the join condition.boolean
next()
Check if there are more rows to read.(package private) void
optimizeFullCondition()
Optimize the full condition.void
prepare()
Prepare reading rows.void
removeFilterCondition()
Remove the filter condition.void
removeJoin()
Remove the joined tablevoid
removeJoinCondition()
Remove the join condition.(package private) void
removeUnusableIndexConditions()
Remove all index conditions that are not used by the current index.void
reset()
Reset to the current position.void
set(Row current)
Set the current row.void
setAlias(java.lang.String alias)
void
setDerivedColumns(java.util.ArrayList<java.lang.String> derivedColumnNames)
Set derived column list.void
setEvaluatable(boolean evaluatable)
void
setEvaluatable(TableFilter filter, boolean b)
Update the filter and join conditions of this and all joined tables with the information that the given table filter and all nested filter can now return rows or not.void
setFullCondition(Expression condition)
void
setIndex(Index index)
void
setNestedJoin(TableFilter filter)
Set a nested joined table.protected void
setNullRow()
Set the state of this and all nested tables to the NULL row.void
setPlanItem(PlanItem item)
Set what plan item (index, cost, masks) to use.private void
setScanIndexes()
Set all missing indexes to scan indexes recursively.void
setUsed(boolean used)
void
startQuery(SessionLocal s)
Start the query.java.lang.String
toString()
void
visit(TableFilter.TableFilterVisitor visitor)
Visit this and all joined or nested table filters.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.h2.table.ColumnResolver
optimize
-
-
-
-
Field Detail
-
BEFORE_FIRST
private static final int BEFORE_FIRST
- See Also:
- Constant Field Values
-
FOUND
private static final int FOUND
- See Also:
- Constant Field Values
-
AFTER_LAST
private static final int AFTER_LAST
- See Also:
- Constant Field Values
-
NULL_ROW
private static final int NULL_ROW
- See Also:
- Constant Field Values
-
ORDER_IN_FROM_COMPARATOR
public static final java.util.Comparator<TableFilter> ORDER_IN_FROM_COMPARATOR
Comparator that uses order in FROM clause as a sort key.
-
JOI_VISITOR
private static final TableFilter.TableFilterVisitor JOI_VISITOR
A visitor that sets joinOuterIndirect to true.
-
joinOuterIndirect
protected boolean joinOuterIndirect
Whether this is a direct or indirect (nested) outer join
-
session
private SessionLocal session
-
table
private final Table table
-
select
private final Select select
-
alias
private java.lang.String alias
-
index
private Index index
-
indexHints
private final IndexHints indexHints
-
masks
private int[] masks
-
scanCount
private int scanCount
-
evaluatable
private boolean evaluatable
-
used
private boolean used
Indicates that this filter is used in the plan.
-
cursor
private final IndexCursor cursor
The filter used to walk through the index.
-
indexConditions
private final java.util.ArrayList<IndexCondition> indexConditions
The index conditions used for direct index lookup (start or end).
-
filterCondition
private Expression filterCondition
Additional conditions that can't be used for index lookup, but for row filter for this table (ID=ID, NAME LIKE '%X%')
-
joinCondition
private Expression joinCondition
The complete join condition.
-
currentSearchRow
private SearchRow currentSearchRow
-
current
private Row current
-
state
private int state
-
join
private TableFilter join
The joined table (if there is one).
-
joinOuter
private boolean joinOuter
Whether this is an outer join.
-
nestedJoin
private TableFilter nestedJoin
The nested joined table (if there is one).
-
commonJoinColumns
private java.util.LinkedHashMap<Column,Column> commonJoinColumns
Map of common join columns, used for NATURAL joins and USING clause of other joins. This map preserves original order of the columns.
-
commonJoinColumnsFilter
private TableFilter commonJoinColumnsFilter
-
commonJoinColumnsToExclude
private java.util.ArrayList<Column> commonJoinColumnsToExclude
-
foundOne
private boolean foundOne
-
fullCondition
private Expression fullCondition
-
hashCode
private final int hashCode
-
orderInFrom
private final int orderInFrom
-
derivedColumnMap
private java.util.LinkedHashMap<Column,java.lang.String> derivedColumnMap
Map of derived column names. This map preserves original order of the columns.
-
-
Constructor Detail
-
TableFilter
public TableFilter(SessionLocal session, Table table, java.lang.String alias, boolean rightsChecked, Select select, int orderInFrom, IndexHints indexHints)
Create a new table filter object.- Parameters:
session
- the sessiontable
- the table from where to read dataalias
- the alias namerightsChecked
- true if rights are already checkedselect
- the select statementorderInFrom
- original order number (index) of this table filter inindexHints
- the index hints to be used by the query planner
-
-
Method Detail
-
getOrderInFrom
public int getOrderInFrom()
Get the order number (index) of this table filter in the "from" clause of the query.- Returns:
- the index (0, 1, 2,...)
-
getIndexCursor
public IndexCursor getIndexCursor()
-
getSelect
public Select getSelect()
Description copied from interface:ColumnResolver
Get the select statement.- Specified by:
getSelect
in interfaceColumnResolver
- Returns:
- the select statement
-
getTable
public Table getTable()
-
lock
public void lock(SessionLocal s)
Lock the table. This will also lock joined tables.- Parameters:
s
- the session
-
getBestPlanItem
public PlanItem getBestPlanItem(SessionLocal s, TableFilter[] filters, int filter, AllColumnsForPlan allColumnsSet)
Get the best plan item (index, cost) to use for the current join order.- Parameters:
s
- the sessionfilters
- all joined table filtersfilter
- the current table filter indexallColumnsSet
- the set of all columns- Returns:
- the best plan item
-
setPlanItem
public void setPlanItem(PlanItem item)
Set what plan item (index, cost, masks) to use.- Parameters:
item
- the plan item
-
setScanIndexes
private void setScanIndexes()
Set all missing indexes to scan indexes recursively.
-
prepare
public void prepare()
Prepare reading rows. This method will remove all index conditions that can not be used, and optimize the conditions.
-
startQuery
public void startQuery(SessionLocal s)
Start the query. This will reset the scan counts.- Parameters:
s
- the session
-
reset
public void reset()
Reset to the current position.
-
next
public boolean next()
Check if there are more rows to read.- Returns:
- true if there are
-
isNullRow
public boolean isNullRow()
-
setNullRow
protected void setNullRow()
Set the state of this and all nested tables to the NULL row.
-
checkTimeout
private void checkTimeout()
-
isOk
boolean isOk(Expression condition)
Whether the current value of the condition is true, or there is no condition.- Parameters:
condition
- the condition (null for no condition)- Returns:
- true if yes
-
get
public Row get()
Get the current row.- Returns:
- the current row, or null
-
set
public void set(Row current)
Set the current row.- Parameters:
current
- the current row
-
getTableAlias
public java.lang.String getTableAlias()
Get the table alias name. If no alias is specified, the table name is returned.- Specified by:
getTableAlias
in interfaceColumnResolver
- Returns:
- the alias name
-
addIndexCondition
public void addIndexCondition(IndexCondition condition)
Add an index condition.- Parameters:
condition
- the index condition
-
addFilterCondition
public void addFilterCondition(Expression condition, boolean isJoin)
Add a filter condition.- Parameters:
condition
- the conditionisJoin
- if this is in fact a join condition
-
addJoin
public void addJoin(TableFilter filter, boolean outer, Expression on)
Add a joined table.- Parameters:
filter
- the joined table filterouter
- if this is an outer joinon
- the join condition
-
setNestedJoin
public void setNestedJoin(TableFilter filter)
Set a nested joined table.- Parameters:
filter
- the joined table filter
-
mapAndAddFilter
public void mapAndAddFilter(Expression on)
Map the columns and add the join condition.- Parameters:
on
- the condition
-
createIndexConditions
public void createIndexConditions()
Create the index conditions for this filter if needed.
-
getJoin
public TableFilter getJoin()
-
isJoinOuter
public boolean isJoinOuter()
Whether this is an outer joined table.- Returns:
- true if it is
-
isJoinOuterIndirect
public boolean isJoinOuterIndirect()
Whether this is indirectly an outer joined table (nested within an inner join).- Returns:
- true if it is
-
getPlanSQL
public java.lang.StringBuilder getPlanSQL(java.lang.StringBuilder builder, boolean isJoin, int sqlFlags)
Get the query execution plan text to use for this table filter and append it to the specified builder.- Parameters:
builder
- string builder to append toisJoin
- if this is a joined tablesqlFlags
- formatting flags- Returns:
- the specified builder
-
removeUnusableIndexConditions
void removeUnusableIndexConditions()
Remove all index conditions that are not used by the current index.
-
getMasks
public int[] getMasks()
-
getIndexConditions
public java.util.ArrayList<IndexCondition> getIndexConditions()
-
getIndex
public Index getIndex()
-
setIndex
public void setIndex(Index index)
-
setUsed
public void setUsed(boolean used)
-
isUsed
public boolean isUsed()
-
removeJoin
public void removeJoin()
Remove the joined table
-
getJoinCondition
public Expression getJoinCondition()
-
removeJoinCondition
public void removeJoinCondition()
Remove the join condition.
-
getFilterCondition
public Expression getFilterCondition()
-
removeFilterCondition
public void removeFilterCondition()
Remove the filter condition.
-
setFullCondition
public void setFullCondition(Expression condition)
-
optimizeFullCondition
void optimizeFullCondition()
Optimize the full condition. This will add the full condition to the filter condition.
-
setEvaluatable
public void setEvaluatable(TableFilter filter, boolean b)
Update the filter and join conditions of this and all joined tables with the information that the given table filter and all nested filter can now return rows or not.- Parameters:
filter
- the table filterb
- the new flag
-
setEvaluatable
public void setEvaluatable(boolean evaluatable)
-
getSchemaName
public java.lang.String getSchemaName()
Description copied from interface:ColumnResolver
Get the schema name or null.- Specified by:
getSchemaName
in interfaceColumnResolver
- Returns:
- the schema name or null
-
getColumns
public Column[] getColumns()
Description copied from interface:ColumnResolver
Get the column list.- Specified by:
getColumns
in interfaceColumnResolver
- Returns:
- the column list
-
findColumn
public Column findColumn(java.lang.String name)
Description copied from interface:ColumnResolver
Get the column with the specified name.- Specified by:
findColumn
in interfaceColumnResolver
- Parameters:
name
- the column name, must be a derived name if this column resolver has a derived column list- Returns:
- the column with the specified name, or
null
-
getColumnName
public java.lang.String getColumnName(Column column)
Description copied from interface:ColumnResolver
Get the name of the specified column.- Specified by:
getColumnName
in interfaceColumnResolver
- Parameters:
column
- column- Returns:
- column name
-
hasDerivedColumnList
public boolean hasDerivedColumnList()
Description copied from interface:ColumnResolver
Returns whether this column resolver has a derived column list.- Specified by:
hasDerivedColumnList
in interfaceColumnResolver
- Returns:
true
if this column resolver has a derived column list,false
otherwise
-
getColumn
public Column getColumn(java.lang.String columnName, boolean ifExists)
Get the column with the given name.- Parameters:
columnName
- the column nameifExists
- if (@code true) returnnull
if column does not exist- Returns:
- the column
- Throws:
DbException
- if the column was not found andifExists
isfalse
-
getSystemColumns
public Column[] getSystemColumns()
Get the system columns that this table understands. This is used for compatibility with other databases. The columns are only returned if the current mode supports system columns.- Specified by:
getSystemColumns
in interfaceColumnResolver
- Returns:
- the system columns
-
getRowIdColumn
public Column getRowIdColumn()
Description copied from interface:ColumnResolver
Get the row id pseudo column, if there is one.- Specified by:
getRowIdColumn
in interfaceColumnResolver
- Returns:
- the row id column or null
-
getValue
public Value getValue(Column column)
Description copied from interface:ColumnResolver
Get the value for the given column.- Specified by:
getValue
in interfaceColumnResolver
- Parameters:
column
- the column- Returns:
- the value
-
getTableFilter
public TableFilter getTableFilter()
Description copied from interface:ColumnResolver
Get the table filter.- Specified by:
getTableFilter
in interfaceColumnResolver
- Returns:
- the table filter
-
setAlias
public void setAlias(java.lang.String alias)
-
setDerivedColumns
public void setDerivedColumns(java.util.ArrayList<java.lang.String> derivedColumnNames)
Set derived column list.- Parameters:
derivedColumnNames
- names of derived columns
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
addCommonJoinColumns
public void addCommonJoinColumns(Column leftColumn, Column replacementColumn, TableFilter replacementFilter)
Add a column to the common join column list for a left table filter.- Parameters:
leftColumn
- the column on the left sidereplacementColumn
- the column to use instead, may be the same as column on the left sidereplacementFilter
- the table filter for replacement columns
-
addCommonJoinColumnToExclude
public void addCommonJoinColumnToExclude(Column columnToExclude)
Add an excluded column to the common join column list.- Parameters:
columnToExclude
- the column to exclude
-
getCommonJoinColumns
public java.util.LinkedHashMap<Column,Column> getCommonJoinColumns()
Returns common join columns map.- Returns:
- common join columns map, or
null
-
getCommonJoinColumnsFilter
public TableFilter getCommonJoinColumnsFilter()
Returns common join columns table filter.- Returns:
- common join columns table filter, or
null
-
isCommonJoinColumnToExclude
public boolean isCommonJoinColumnToExclude(Column c)
Check if the given column is an excluded common join column.- Parameters:
c
- the column to check- Returns:
- true if this is an excluded common join column
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
hasInComparisons
public boolean hasInComparisons()
Are there any index conditions that involve IN(...).- Returns:
- whether there are IN(...) comparisons
-
getNestedJoin
public TableFilter getNestedJoin()
-
visit
public void visit(TableFilter.TableFilterVisitor visitor)
Visit this and all joined or nested table filters.- Parameters:
visitor
- the visitor
-
isEvaluatable
public boolean isEvaluatable()
-
getSession
public SessionLocal getSession()
-
getIndexHints
public IndexHints getIndexHints()
-
isNoFromClauseFilter
public boolean isNoFromClauseFilter()
Returns whether this is a table filter with implicit DUAL table for a SELECT without a FROM clause.- Returns:
- whether this is a table filter with implicit DUAL table
-
-