Search in sources :

Example 1 with ViewIndex

use of org.h2.index.ViewIndex in project h2database by h2database.

the class TableFilter method prepareJoinBatch.

/**
 * Attempt to initialize batched join.
 *
 * @param jb join batch if it is already created
 * @param filters the table filters
 * @param filter the filter index (0, 1,...)
 * @return join batch if query runs over index which supports batched
 *         lookups, {@code null} otherwise
 */
public JoinBatch prepareJoinBatch(JoinBatch jb, TableFilter[] filters, int filter) {
    assert filters[filter] == this;
    joinBatch = null;
    joinFilterId = -1;
    if (getTable().isView()) {
        session.pushSubQueryInfo(masks, filters, filter, select.getSortOrder());
        try {
            ((ViewIndex) index).getQuery().prepareJoinBatch();
        } finally {
            session.popSubQueryInfo();
        }
    }
    // For globally top table filter we don't need to create lookup batch,
    // because currently it will not be used (this will be shown in
    // ViewIndex.getPlanSQL()). Probably later on it will make sense to
    // create it to better support X IN (...) conditions, but this needs to
    // be implemented separately. If isAlwaysTopTableFilter is false then we
    // either not a top table filter or top table filter in a sub-query,
    // which in turn is not top in outer query, thus we need to enable
    // batching here to allow outer query run batched join against this
    // sub-query.
    IndexLookupBatch lookupBatch = null;
    if (jb == null && select != null && !isAlwaysTopTableFilter(filter)) {
        lookupBatch = index.createLookupBatch(filters, filter);
        if (lookupBatch != null) {
            jb = new JoinBatch(filter + 1, join);
        }
    }
    if (jb != null) {
        if (nestedJoin != null) {
            throw DbException.throwInternalError();
        }
        joinBatch = jb;
        joinFilterId = filter;
        if (lookupBatch == null && !isAlwaysTopTableFilter(filter)) {
            // createLookupBatch will be called at most once because jb can
            // be created only if lookupBatch is already not null from the
            // call above.
            lookupBatch = index.createLookupBatch(filters, filter);
            if (lookupBatch == null) {
                // the index does not support lookup batching, need to fake
                // it because we are not top
                lookupBatch = JoinBatch.createFakeIndexLookupBatch(this);
            }
        }
        jb.register(this, lookupBatch);
    }
    return jb;
}
Also used : IndexLookupBatch(org.h2.index.IndexLookupBatch)

Example 2 with ViewIndex

use of org.h2.index.ViewIndex in project h2database by h2database.

the class TableView method init.

private synchronized void init(String querySQL, ArrayList<Parameter> params, Column[] columnTemplates, Session session, boolean allowRecursive, boolean literalsChecked, boolean isTableExpression, boolean isPersistent) {
    this.querySQL = querySQL;
    this.columnTemplates = columnTemplates;
    this.allowRecursive = allowRecursive;
    this.isRecursiveQueryDetected = false;
    this.isTableExpression = isTableExpression;
    this.isPersistent = isPersistent;
    index = new ViewIndex(this, querySQL, params, allowRecursive);
    initColumnsAndTables(session, literalsChecked);
}
Also used : ViewIndex(org.h2.index.ViewIndex)

Example 3 with ViewIndex

use of org.h2.index.ViewIndex in project h2database by h2database.

the class JoinBatch method createViewIndexLookupBatch.

/**
 * Create index lookup batch for a view index.
 *
 * @param viewIndex view index
 * @return index lookup batch or {@code null} if batching is not supported
 *         for this query
 */
public static IndexLookupBatch createViewIndexLookupBatch(ViewIndex viewIndex) {
    Query query = viewIndex.getQuery();
    if (query.isUnion()) {
        ViewIndexLookupBatchUnion unionBatch = new ViewIndexLookupBatchUnion(viewIndex);
        return unionBatch.initialize() ? unionBatch : null;
    }
    JoinBatch jb = ((Select) query).getJoinBatch();
    if (jb == null || jb.getLookupBatch(0) == null) {
        // our sub-query is not batched or is top batched sub-query
        return null;
    }
    assert !jb.batchedSubQuery;
    jb.batchedSubQuery = true;
    return jb.viewIndexLookupBatch(viewIndex);
}
Also used : Query(org.h2.command.dml.Query) Select(org.h2.command.dml.Select)

Example 4 with ViewIndex

use of org.h2.index.ViewIndex in project h2database by h2database.

the class TableView method getBestPlanItem.

@Override
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
    final CacheKey cacheKey = new CacheKey(masks, this);
    Map<Object, ViewIndex> indexCache = session.getViewIndexCache(topQuery != null);
    ViewIndex i = indexCache.get(cacheKey);
    if (i == null || i.isExpired()) {
        i = new ViewIndex(this, index, session, masks, filters, filter, sortOrder);
        indexCache.put(cacheKey, i);
    }
    PlanItem item = new PlanItem();
    item.cost = i.getCost(session, masks, filters, filter, sortOrder, allColumnsSet);
    item.setIndex(i);
    return item;
}
Also used : DbObject(org.h2.engine.DbObject) ViewIndex(org.h2.index.ViewIndex)

Example 5 with ViewIndex

use of org.h2.index.ViewIndex in project ignite by apache.

the class GridSqlQueryParser method collectOptimizedTableFiltersOrder.

/**
 * @param qry Query.
 */
private void collectOptimizedTableFiltersOrder(Query qry) {
    if (qry instanceof SelectUnion) {
        collectOptimizedTableFiltersOrder(((SelectUnion) qry).getLeft());
        collectOptimizedTableFiltersOrder(((SelectUnion) qry).getRight());
    } else {
        Select select = (Select) qry;
        TableFilter filter = select.getTopTableFilter();
        int i = 0;
        do {
            assert0(filter != null, select);
            assert0(filter.getNestedJoin() == null, select);
            // Here all the table filters must have generated unique aliases,
            // thus we can store them in the same map for all the subqueries.
            optimizedTableFilterOrder.put(filter.getTableAlias(), i++);
            Table tbl = filter.getTable();
            // Go down and collect inside of optimized subqueries.
            if (tbl instanceof TableView) {
                ViewIndex viewIdx = (ViewIndex) filter.getIndex();
                collectOptimizedTableFiltersOrder(viewIdx.getQuery());
            }
            filter = filter.getJoin();
        } while (filter != null);
    }
}
Also used : SelectUnion(org.h2.command.dml.SelectUnion) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) RangeTable(org.h2.table.RangeTable) MetaTable(org.h2.table.MetaTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) TableFilter(org.h2.table.TableFilter) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) ViewIndex(org.h2.index.ViewIndex) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) TableView(org.h2.table.TableView)

Aggregations

ViewIndex (org.h2.index.ViewIndex)3 Select (org.h2.command.dml.Select)2 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 CreateTable (org.h2.command.ddl.CreateTable)1 DropTable (org.h2.command.ddl.DropTable)1 Query (org.h2.command.dml.Query)1 SelectUnion (org.h2.command.dml.SelectUnion)1 DbObject (org.h2.engine.DbObject)1 ConditionInSelect (org.h2.expression.ConditionInSelect)1 IndexLookupBatch (org.h2.index.IndexLookupBatch)1 FunctionTable (org.h2.table.FunctionTable)1 MetaTable (org.h2.table.MetaTable)1 RangeTable (org.h2.table.RangeTable)1 Table (org.h2.table.Table)1 TableFilter (org.h2.table.TableFilter)1 TableView (org.h2.table.TableView)1