Search in sources :

Example 61 with TableFilter

use of org.h2.table.TableFilter in project h2database by h2database.

the class ViewIndex method getQuery.

private Query getQuery(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder) {
    Query q = prepareSubQuery(querySQL, session, masks, filters, filter, sortOrder);
    if (masks == null) {
        return q;
    }
    if (!q.allowGlobalConditions()) {
        return q;
    }
    int firstIndexParam = view.getParameterOffset(originalParameters);
    // the column index of each parameter
    // (for example: paramColumnIndex {0, 0} mean
    // param[0] is column 0, and param[1] is also column 0)
    IntArray paramColumnIndex = new IntArray();
    int indexColumnCount = 0;
    for (int i = 0; i < masks.length; i++) {
        int mask = masks[i];
        if (mask == 0) {
            continue;
        }
        indexColumnCount++;
        // the number of parameters depends on the mask;
        // for range queries it is 2: >= x AND <= y
        // but bitMask could also be 7 (=, and <=, and >=)
        int bitCount = Integer.bitCount(mask);
        for (int j = 0; j < bitCount; j++) {
            paramColumnIndex.add(i);
        }
    }
    int len = paramColumnIndex.size();
    ArrayList<Column> columnList = New.arrayList();
    for (int i = 0; i < len; ) {
        int idx = paramColumnIndex.get(i);
        columnList.add(table.getColumn(idx));
        int mask = masks[idx];
        if ((mask & IndexCondition.EQUALITY) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.EQUAL_NULL_SAFE);
            i++;
        }
        if ((mask & IndexCondition.START) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.BIGGER_EQUAL);
            i++;
        }
        if ((mask & IndexCondition.END) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.SMALLER_EQUAL);
            i++;
        }
        if ((mask & IndexCondition.SPATIAL_INTERSECTS) != 0) {
            Parameter param = new Parameter(firstIndexParam + i);
            q.addGlobalCondition(param, idx, Comparison.SPATIAL_INTERSECTS);
            i++;
        }
    }
    columns = columnList.toArray(new Column[0]);
    // reconstruct the index columns from the masks
    this.indexColumns = new IndexColumn[indexColumnCount];
    this.columnIds = new int[indexColumnCount];
    for (int type = 0, indexColumnId = 0; type < 2; type++) {
        for (int i = 0; i < masks.length; i++) {
            int mask = masks[i];
            if (mask == 0) {
                continue;
            }
            if (type == 0) {
                if ((mask & IndexCondition.EQUALITY) == 0) {
                    // the first columns need to be equality conditions
                    continue;
                }
            } else {
                if ((mask & IndexCondition.EQUALITY) != 0) {
                    // after that only range conditions
                    continue;
                }
            }
            IndexColumn c = new IndexColumn();
            c.column = table.getColumn(i);
            indexColumns[indexColumnId] = c;
            columnIds[indexColumnId] = c.column.getColumnId();
            indexColumnId++;
        }
    }
    String sql = q.getPlanSQL();
    q = prepareSubQuery(sql, session, masks, filters, filter, sortOrder);
    return q;
}
Also used : Query(org.h2.command.dml.Query) IntArray(org.h2.util.IntArray) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Parameter(org.h2.expression.Parameter) IndexColumn(org.h2.table.IndexColumn)

Example 62 with TableFilter

use of org.h2.table.TableFilter in project h2database by h2database.

the class ViewIndex method prepareSubQuery.

private static Query prepareSubQuery(String sql, Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder) {
    Prepared p;
    session.pushSubQueryInfo(masks, filters, filter, sortOrder);
    try {
        p = session.prepare(sql, true, true);
    } finally {
        session.popSubQueryInfo();
    }
    return (Query) p;
}
Also used : Query(org.h2.command.dml.Query) Prepared(org.h2.command.Prepared)

Example 63 with TableFilter

use of org.h2.table.TableFilter in project h2database by h2database.

the class NonUniqueHashIndex method getCost.

@Override
public double getCost(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
    for (Column column : columns) {
        int index = column.getColumnId();
        int mask = masks[index];
        if ((mask & IndexCondition.EQUALITY) != IndexCondition.EQUALITY) {
            return Long.MAX_VALUE;
        }
    }
    return 2;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn)

Example 64 with TableFilter

use of org.h2.table.TableFilter in project ignite by apache.

the class H2IndexCostedBase method sortingCost.

/**
 * Estimate sorting cost.
 *
 * @param rowCount Total rows count.
 * @param filters Filters array.
 * @param filter Column filter index.
 * @param sortOrder Sort order.
 * @param isScanIndex Flag if current index is a scan index.
 * @return Sorting cost.
 */
private long sortingCost(long rowCount, TableFilter[] filters, int filter, SortOrder sortOrder, boolean isScanIndex) {
    if (sortOrder == null)
        return 0;
    long sortingCost = 100 + rowCount / 10;
    if (!isScanIndex) {
        boolean sortOrderMatches = true;
        int coveringCount = 0;
        int[] sortTypes = sortOrder.getSortTypes();
        TableFilter tableFilter = filters == null ? null : filters[filter];
        for (int i = 0, len = sortTypes.length; i < len; i++) {
            if (i >= indexColumns.length) {
                // more of the order by columns.
                break;
            }
            Column col = sortOrder.getColumn(i, tableFilter);
            if (col == null) {
                sortOrderMatches = false;
                break;
            }
            IndexColumn indexCol = indexColumns[i];
            if (!col.equals(indexCol.column)) {
                sortOrderMatches = false;
                break;
            }
            int sortType = sortTypes[i];
            if (sortType != indexCol.sortType) {
                sortOrderMatches = false;
                break;
            }
            coveringCount++;
        }
        if (sortOrderMatches)
            // "coveringCount" makes sure that when we have two
            // or more covering indexes, we choose the one
            // that covers more.
            sortingCost = 100 - coveringCount;
    }
    return sortingCost;
}
Also used : TableFilter(org.h2.table.TableFilter) Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) IndexColumn(org.h2.table.IndexColumn)

Example 65 with TableFilter

use of org.h2.table.TableFilter in project ignite by apache.

the class CollocationModel method toString.

/**
 * @param b String builder.
 * @param lvl Depth level.
 */
private boolean toString(SB b, int lvl) {
    boolean res = false;
    if (lvl == 0) {
        TableFilter f = filter();
        String tblAlias = f == null ? "^" : f.getTableAlias();
        b.a("[tbl=").a(tblAlias).a(", type=").a(type).a(", mul=").a(multiplier).a("]");
        res = true;
    } else if (childFilters != null) {
        assert lvl > 0;
        lvl--;
        for (int i = 0; i < childFilters.length; i++) {
            if (lvl == 0)
                b.a(" | ");
            res |= child(i, true).toString(b, lvl);
        }
        if (lvl == 0)
            b.a(" | ");
    }
    return res;
}
Also used : TableFilter(org.h2.table.TableFilter)

Aggregations

TableFilter (org.h2.table.TableFilter)39 IndexColumn (org.h2.table.IndexColumn)21 Column (org.h2.table.Column)20 Expression (org.h2.expression.Expression)18 ValueExpression (org.h2.expression.ValueExpression)14 ExpressionColumn (org.h2.expression.ExpressionColumn)12 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)11 Table (org.h2.table.Table)11 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)9 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)9 Select (org.h2.command.dml.Select)9 ValueString (org.h2.value.ValueString)9 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)6 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)6 CreateTable (org.h2.command.ddl.CreateTable)6 DropTable (org.h2.command.ddl.DropTable)6 Query (org.h2.command.dml.Query)6 IndexCondition (org.h2.index.IndexCondition)6 ArrayList (java.util.ArrayList)5 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)5