Search in sources :

Example 6 with SortOrder

use of org.h2.result.SortOrder in project h2database by h2database.

the class TableFilter method getBestPlanItem.

/**
 * Get the best plan item (index, cost) to use use for the current join
 * order.
 *
 * @param s the session
 * @param filters all joined table filters
 * @param filter the current table filter index
 * @param allColumnsSet the set of all columns
 * @return the best plan item
 */
public PlanItem getBestPlanItem(Session s, TableFilter[] filters, int filter, HashSet<Column> allColumnsSet) {
    PlanItem item1 = null;
    SortOrder sortOrder = null;
    if (select != null) {
        sortOrder = select.getSortOrder();
    }
    if (indexConditions.isEmpty()) {
        item1 = new PlanItem();
        item1.setIndex(table.getScanIndex(s, null, filters, filter, sortOrder, allColumnsSet));
        item1.cost = item1.getIndex().getCost(s, null, filters, filter, sortOrder, allColumnsSet);
    }
    int len = table.getColumns().length;
    int[] masks = new int[len];
    for (IndexCondition condition : indexConditions) {
        if (condition.isEvaluatable()) {
            if (condition.isAlwaysFalse()) {
                masks = null;
                break;
            }
            int id = condition.getColumn().getColumnId();
            if (id >= 0) {
                masks[id] |= condition.getMask(indexConditions);
            }
        }
    }
    PlanItem item = table.getBestPlanItem(s, masks, filters, filter, sortOrder, allColumnsSet);
    item.setMasks(masks);
    // The more index conditions, the earlier the table.
    // This is to ensure joins without indexes run quickly:
    // x (x.a=10); y (x.b=y.b) - see issue 113
    item.cost -= item.cost * indexConditions.size() / 100 / (filter + 1);
    if (item1 != null && item1.cost < item.cost) {
        item = item1;
    }
    if (nestedJoin != null) {
        setEvaluatable(true);
        item.setNestedJoinPlan(nestedJoin.getBestPlanItem(s, filters, filter, allColumnsSet));
        // TODO optimizer: calculate cost of a join: should use separate
        // expected row number and lookup cost
        item.cost += item.cost * item.getNestedJoinPlan().cost;
    }
    if (join != null) {
        setEvaluatable(true);
        do {
            filter++;
        } while (filters[filter] != join);
        item.setJoinPlan(join.getBestPlanItem(s, filters, filter, allColumnsSet));
        // TODO optimizer: calculate cost of a join: should use separate
        // expected row number and lookup cost
        item.cost += item.cost * item.getJoinPlan().cost;
    }
    return item;
}
Also used : SortOrder(org.h2.result.SortOrder) IndexCondition(org.h2.index.IndexCondition)

Example 7 with SortOrder

use of org.h2.result.SortOrder in project h2database by h2database.

the class Query method prepareOrder.

/**
 * Create a {@link SortOrder} object given the list of {@link SelectOrderBy}
 * objects. The expression list is extended if necessary.
 *
 * @param orderList a list of {@link SelectOrderBy} elements
 * @param expressionCount the number of columns in the query
 * @return the {@link SortOrder} object
 */
public SortOrder prepareOrder(ArrayList<SelectOrderBy> orderList, int expressionCount) {
    int size = orderList.size();
    int[] index = new int[size];
    int[] sortType = new int[size];
    for (int i = 0; i < size; i++) {
        SelectOrderBy o = orderList.get(i);
        int idx;
        boolean reverse = false;
        Expression expr = o.columnIndexExpr;
        Value v = expr.getValue(null);
        if (v == ValueNull.INSTANCE) {
            // parameter not yet set - order by first column
            idx = 0;
        } else {
            idx = v.getInt();
            if (idx < 0) {
                reverse = true;
                idx = -idx;
            }
            idx -= 1;
            if (idx < 0 || idx >= expressionCount) {
                throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, "" + (idx + 1));
            }
        }
        index[i] = idx;
        boolean desc = o.descending;
        if (reverse) {
            desc = !desc;
        }
        int type = desc ? SortOrder.DESCENDING : SortOrder.ASCENDING;
        if (o.nullsFirst) {
            type += SortOrder.NULLS_FIRST;
        } else if (o.nullsLast) {
            type += SortOrder.NULLS_LAST;
        }
        sortType[i] = type;
    }
    return new SortOrder(session.getDatabase(), index, sortType, orderList);
}
Also used : ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) SortOrder(org.h2.result.SortOrder)

Example 8 with SortOrder

use of org.h2.result.SortOrder in project h2database by h2database.

the class Aggregate method sortWithOrderBy.

private void sortWithOrderBy(Value[] array) {
    final SortOrder sortOrder = orderBySort;
    if (sortOrder != null) {
        Arrays.sort(array, new Comparator<Value>() {

            @Override
            public int compare(Value v1, Value v2) {
                return sortOrder.compare(((ValueArray) v1).getList(), ((ValueArray) v2).getList());
            }
        });
    } else {
        final Database database = select.getSession().getDatabase();
        Arrays.sort(array, new Comparator<Value>() {

            @Override
            public int compare(Value v1, Value v2) {
                return database.compare(v1, v2);
            }
        });
    }
}
Also used : Value(org.h2.value.Value) Database(org.h2.engine.Database) SortOrder(org.h2.result.SortOrder) ValueArray(org.h2.value.ValueArray)

Example 9 with SortOrder

use of org.h2.result.SortOrder 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 10 with SortOrder

use of org.h2.result.SortOrder in project h2database by h2database.

the class Table method getBestPlanItem.

/**
 * Get the best plan for the given search mask.
 *
 * @param session the session
 * @param masks per-column comparison bit masks, null means 'always false',
 *              see constants in IndexCondition
 * @param filters all joined table filters
 * @param filter the current table filter index
 * @param sortOrder the sort order
 * @param allColumnsSet the set of all columns
 * @return the plan item
 */
public PlanItem getBestPlanItem(Session session, int[] masks, TableFilter[] filters, int filter, SortOrder sortOrder, HashSet<Column> allColumnsSet) {
    PlanItem item = new PlanItem();
    item.setIndex(getScanIndex(session));
    item.cost = item.getIndex().getCost(session, null, filters, filter, null, allColumnsSet);
    Trace t = session.getTrace();
    if (t.isDebugEnabled()) {
        t.debug("Table      :     potential plan item cost {0} index {1}", item.cost, item.getIndex().getPlanSQL());
    }
    ArrayList<Index> indexes = getIndexes();
    IndexHints indexHints = getIndexHints(filters, filter);
    if (indexes != null && masks != null) {
        for (int i = 1, size = indexes.size(); i < size; i++) {
            Index index = indexes.get(i);
            if (isIndexExcludedByHints(indexHints, index)) {
                continue;
            }
            double cost = index.getCost(session, masks, filters, filter, sortOrder, allColumnsSet);
            if (t.isDebugEnabled()) {
                t.debug("Table      :     potential plan item cost {0} index {1}", cost, index.getPlanSQL());
            }
            if (cost < item.cost) {
                item.cost = cost;
                item.setIndex(index);
            }
        }
    }
    return item;
}
Also used : Trace(org.h2.message.Trace) Index(org.h2.index.Index) Constraint(org.h2.constraint.Constraint)

Aggregations

IndexColumn (org.h2.table.IndexColumn)5 SortOrder (org.h2.result.SortOrder)4 Column (org.h2.table.Column)4 Query (org.h2.command.dml.Query)2 Value (org.h2.value.Value)2 Prepared (org.h2.command.Prepared)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 SelectOrderBy (org.h2.command.dml.SelectOrderBy)1 Constraint (org.h2.constraint.Constraint)1 Database (org.h2.engine.Database)1 DbObject (org.h2.engine.DbObject)1 Expression (org.h2.expression.Expression)1 Parameter (org.h2.expression.Parameter)1 ValueExpression (org.h2.expression.ValueExpression)1 Index (org.h2.index.Index)1 IndexCondition (org.h2.index.IndexCondition)1 ViewIndex (org.h2.index.ViewIndex)1 Trace (org.h2.message.Trace)1 TableFilter (org.h2.table.TableFilter)1 IntArray (org.h2.util.IntArray)1