Class FunctionTable
- java.lang.Object
-
- com.mckoi.database.Table
-
- com.mckoi.database.AbstractDataTable
-
- com.mckoi.database.DefaultDataTable
-
- com.mckoi.database.FunctionTable
-
- All Implemented Interfaces:
RootTable,TableDataSource
public class FunctionTable extends DefaultDataTable
A table that has a number of columns and as many rows as the refering table. Tables of this type are used to construct aggregate and function columns based on an expression. They are joined with the result table in the last part of the query processing.For example, a query like 'select id, id * 2, 8 * 9 from Part' the columns 'id * 2' and '8 * 9' would be formed from this table.
SYNCHRONIZATION ISSUE: Instances of this object are NOT thread safe. The reason it's not is because if 'getCellContents' is used concurrently it's possible for the same value to be added into the cache causing an error. It is not expected that this object will be shared between threads.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classFunctionTable.TableGroupResolverGroup resolver.-
Nested classes/interfaces inherited from class com.mckoi.database.Table
Table.TableVariableResolver
-
-
Field Summary
Fields Modifier and Type Field Description private QueryContextcontextThe context of this function table.private Table.TableVariableResolvercr_resolverThe TableVariableResolver for the table we are cross referencing.private Tablecross_ref_tableThe table that this function table cross references.private byte[]exp_infoSome information about the expression list.private Expression[]exp_listThe list of expressions that are evaluated to form each column.private DataTableDeffun_table_defThe DataTableDef object that describes the columns in this function table.private static TableNameFUNCTION_TABLE_NAMEThe table name given to all function tables.private IntegerVectorgroup_linksThe group row links.private IntegerVectorgroup_lookupThe lookup mapping for row->group_index used for grouping.private FunctionTable.TableGroupResolvergroup_resolverThe TableGroupResolver for the table.private intunique_idA unique id given to this FunctionTable when it is created.private static intUNIQUE_KEY_SEQThe key used to make distinct unique ids for FunctionTables.private booleanwhole_table_as_groupWhether the whole table is a group.private IntegerVectorwhole_table_groupIf the whole table is a group, this is the grouping rows.private intwhole_table_group_sizeThe total size of the whole table group size.private booleanwhole_table_is_simple_enumIf the whole table is a simple enumeration (row index is 0 to getRowCount) then this is true.-
Fields inherited from class com.mckoi.database.DefaultDataTable
row_count
-
Fields inherited from class com.mckoi.database.Table
DEBUG_QUERY
-
-
Constructor Summary
Constructors Constructor Description FunctionTable(Expression[] exp_list, java.lang.String[] col_names, DatabaseQueryContext context)FunctionTable(Table cross_ref_table, Expression[] in_exp_list, java.lang.String[] col_names, DatabaseQueryContext context)Constructs the FunctionTable.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddDataTableListener(DataTableListener listener)Adds a DataTableListener to the DataTable objects at the root of this table tree hierarchy.private TObjectcalcValue(int column, int row, DataCellCache cache)Return a TObject that represents the value of the 'column', 'row' of this table.voidcreateGroupMatrix(Variable[] col_list)Creates a grouping matrix for the given tables.TObjectgetCellContents(int column, int row)Returns an object that represents the information in the given cell in the table.DataTableDefgetDataTableDef()Returns the DataTableDef object that represents the columns in this function table.TablegetReferenceTable()Returns the Table this function is based on.IntegerVectorgroupRows(int group_number)Returns an IntegerVector that represents the list of all rows in the group the index is at.intgroupSize(int group_number)The size of the group with the given number.booleanhasRootsLocked()Returns true if the table has its row roots locked (via the lockRoot(int) method.voidlockRoot(int lock_key)Locks the root table(s) of this table so that it is impossible to overwrite the underlying rows that may appear in this table.(package private) IntegerVectormaxFromEachGroup(int col_num)Returns a list of rows that represent the maximum row of the given column from each distinct group in this table.TablemergeWithReference(Variable max_column)Returns a Table that is this function table merged with the cross reference table.(package private) voidremoveDataTableListener(DataTableListener listener)Removes a DataTableListener from the DataTable objects at the root of this table tree hierarchy.static TableresultTable(DatabaseQueryContext context, int result_val)Returns a FunctionTable that has an int value made into a BigDecimal.static TableresultTable(DatabaseQueryContext context, Expression expression)Returns a FunctionTable that has a single Expression evaluated in it.static TableresultTable(DatabaseQueryContext context, TObject ob)Returns a FunctionTable that has a single TObject in it.static TableresultTable(DatabaseQueryContext context, java.lang.Object ob)Returns a FunctionTable that has a single Object in it.RowEnumerationrowEnumeration()Returns an Enumeration of the rows in this table.introwGroup(int row_index)Returns the group of the row at the given index.voidsetWholeTableAsGroup()Sets the whole reference table as a single group.(package private) IntegerVectortopFromEachGroup()Returns a list of rows that represent one row from each distinct group in this table.voidunlockRoot(int lock_key)Unlocks the root tables so that the underlying rows may once again be used if they are not locked and have been removed.-
Methods inherited from class com.mckoi.database.DefaultDataTable
addCellToColumnSchemes, addRowToColumnSchemes, blankSelectableSchemes, blankSelectableSchemes, clearColumnScheme, findFieldName, getColumnCount, getDatabase, getResolvedVariable, getRootColumnScheme, getRowCount, getSelectableSchemeFor, removeRowToColumnSchemes, resolveToRawTable, setToRowTableDomain
-
Methods inherited from class com.mckoi.database.AbstractDataTable
getTableName, toString, typeEquals
-
Methods inherited from class com.mckoi.database.Table
all, allColumnMatchesValue, allRowsIn, allRowsNotIn, any, columnContainsCell, columnContainsValue, columnMatchesValue, columnMerge, compareCells, Debug, distinct, distinct, dumpTo, emptySelect, exhaustiveSelect, fastFindFieldName, getColumnDefAt, getColumnScheme, getFirstCellContent, getFirstCellContent, getLastCellContent, getLastCellContent, getSingleCellContent, getSingleCellContent, getSystem, getTableAccessState, getTTypeForColumn, getTTypeForColumn, getVariableResolver, join, orderByColumn, orderByColumn, orderByColumn, orderByColumns, orderedRowList, outside, printGraph, rangeSelect, selectAll, selectAll, selectFirst, selectFromPattern, selectFromRegex, selectLast, selectRange, selectRest, selectRows, selectRows, selectRows, simpleJoin, simpleSelect, singleRowSelect, toMap, union
-
-
-
-
Field Detail
-
UNIQUE_KEY_SEQ
private static int UNIQUE_KEY_SEQ
The key used to make distinct unique ids for FunctionTables.NOTE: This is a thread-safe static mutable variable.
-
FUNCTION_TABLE_NAME
private static final TableName FUNCTION_TABLE_NAME
The table name given to all function tables.
-
unique_id
private int unique_id
A unique id given to this FunctionTable when it is created. No two FunctionTable objects may have the same number. This number is between 0 and 260 million.
-
fun_table_def
private DataTableDef fun_table_def
The DataTableDef object that describes the columns in this function table.
-
cross_ref_table
private Table cross_ref_table
The table that this function table cross references. This is not a parent table, but more like the table we will eventually be joined with.
-
cr_resolver
private Table.TableVariableResolver cr_resolver
The TableVariableResolver for the table we are cross referencing.
-
group_resolver
private FunctionTable.TableGroupResolver group_resolver
The TableGroupResolver for the table.
-
exp_list
private Expression[] exp_list
The list of expressions that are evaluated to form each column.
-
exp_info
private byte[] exp_info
Some information about the expression list. If the value is 0 then the column is simple to solve and shouldn't be cached.
-
group_lookup
private IntegerVector group_lookup
The lookup mapping for row->group_index used for grouping.
-
group_links
private IntegerVector group_links
The group row links. Iterate through this to find all the rows in a group until bit 31 set.
-
whole_table_as_group
private boolean whole_table_as_group
Whether the whole table is a group.
-
whole_table_group
private IntegerVector whole_table_group
If the whole table is a group, this is the grouping rows. This is obtained via 'selectAll' of the reference table.
-
whole_table_group_size
private int whole_table_group_size
The total size of the whole table group size.
-
whole_table_is_simple_enum
private boolean whole_table_is_simple_enum
If the whole table is a simple enumeration (row index is 0 to getRowCount) then this is true.
-
context
private QueryContext context
The context of this function table.
-
-
Constructor Detail
-
FunctionTable
public FunctionTable(Table cross_ref_table, Expression[] in_exp_list, java.lang.String[] col_names, DatabaseQueryContext context)
Constructs the FunctionTable.
-
FunctionTable
public FunctionTable(Expression[] exp_list, java.lang.String[] col_names, DatabaseQueryContext context)
-
-
Method Detail
-
calcValue
private TObject calcValue(int column, int row, DataCellCache cache)
Return a TObject that represents the value of the 'column', 'row' of this table. If 'cache' is not null then the resultant value is added to the cache. If 'cache' is null, no caching happens.
-
setWholeTableAsGroup
public void setWholeTableAsGroup()
Sets the whole reference table as a single group.
-
createGroupMatrix
public void createGroupMatrix(Variable[] col_list)
Creates a grouping matrix for the given tables. The grouping matrix is arranged so that each row of the referring table that is in the group is given a number that refers to the top group entry in the group list. The group list is a linked integer list that chains through each row item in the list.
-
getReferenceTable
public Table getReferenceTable()
Returns the Table this function is based on. We need to provide this method for aggregate functions.
-
rowGroup
public int rowGroup(int row_index)
Returns the group of the row at the given index.
-
groupSize
public int groupSize(int group_number)
The size of the group with the given number.
-
groupRows
public IntegerVector groupRows(int group_number)
Returns an IntegerVector that represents the list of all rows in the group the index is at.
-
mergeWithReference
public Table mergeWithReference(Variable max_column)
Returns a Table that is this function table merged with the cross reference table. The result table includes only one row from each group.The 'max_column' argument is optional (can be null). If it's set to a column in the reference table, then the row with the max value from the group is used as the group row. For example, 'Part.id' will return the row with the maximum part.id from each group.
-
topFromEachGroup
IntegerVector topFromEachGroup()
Returns a list of rows that represent one row from each distinct group in this table. This should be used to construct a virtual table of rows from each distinct group.
-
maxFromEachGroup
IntegerVector maxFromEachGroup(int col_num)
Returns a list of rows that represent the maximum row of the given column from each distinct group in this table. This should be used to construct a virtual table of rows from each distinct group.
-
getDataTableDef
public DataTableDef getDataTableDef()
Returns the DataTableDef object that represents the columns in this function table.- Specified by:
getDataTableDefin interfaceTableDataSource- Specified by:
getDataTableDefin classTable
-
getCellContents
public TObject getCellContents(int column, int row)
Returns an object that represents the information in the given cell in the table. This can be used to obtain information about the given table cells.- Specified by:
getCellContentsin interfaceTableDataSource- Specified by:
getCellContentsin classTable
-
rowEnumeration
public RowEnumeration rowEnumeration()
Returns an Enumeration of the rows in this table. Each call to 'nextRowIndex' returns the next valid row index in the table.- Specified by:
rowEnumerationin interfaceTableDataSource- Specified by:
rowEnumerationin classTable
-
addDataTableListener
void addDataTableListener(DataTableListener listener)
Adds a DataTableListener to the DataTable objects at the root of this table tree hierarchy. If this table represents the join of a number of tables then the DataTableListener is added to all the DataTable objects at the root.A DataTableListener is notified of all modifications to the raw entries of the table. This listener can be used for detecting changes in VIEWs, for triggers or for caching of common queries.
- Specified by:
addDataTableListenerin classTable
-
removeDataTableListener
void removeDataTableListener(DataTableListener listener)
Removes a DataTableListener from the DataTable objects at the root of this table tree hierarchy. If this table represents the join of a number of tables, then the DataTableListener is removed from all the DataTable objects at the root.- Specified by:
removeDataTableListenerin classTable
-
lockRoot
public void lockRoot(int lock_key)
Locks the root table(s) of this table so that it is impossible to overwrite the underlying rows that may appear in this table. This is used when cells in the table need to be accessed 'outside' the lock. So we may have late access to cells in the table. 'lock_key' is a given key that will also unlock the root table(s). NOTE: This is nothing to do with the 'LockingMechanism' object.
-
unlockRoot
public void unlockRoot(int lock_key)
Unlocks the root tables so that the underlying rows may once again be used if they are not locked and have been removed. This should be called some time after the rows have been locked.- Specified by:
unlockRootin classTable
-
hasRootsLocked
public boolean hasRootsLocked()
Returns true if the table has its row roots locked (via the lockRoot(int) method.- Specified by:
hasRootsLockedin classTable
-
resultTable
public static Table resultTable(DatabaseQueryContext context, Expression expression)
Returns a FunctionTable that has a single Expression evaluated in it. The column name is 'result'.
-
resultTable
public static Table resultTable(DatabaseQueryContext context, TObject ob)
Returns a FunctionTable that has a single TObject in it. The column title is 'result'.
-
resultTable
public static Table resultTable(DatabaseQueryContext context, java.lang.Object ob)
Returns a FunctionTable that has a single Object in it. The column title is 'result'.
-
resultTable
public static Table resultTable(DatabaseQueryContext context, int result_val)
Returns a FunctionTable that has an int value made into a BigDecimal. The column title is 'result'.
-
-