Search in sources :

Example 36 with TableFilter

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

the class GridH2CollocationModel method joinedWithCollocated.

/**
 * @param f Filter.
 * @return Affinity join type.
 */
@SuppressWarnings("ForLoopReplaceableByForEach")
private Affinity joinedWithCollocated(int f) {
    TableFilter tf = childFilters[f];
    GridH2Table tbl = (GridH2Table) tf.getTable();
    if (validate) {
        if (tbl.rowDescriptor().context().customAffinityMapper())
            throw customAffinityError(tbl.cacheName());
        if (F.isEmpty(tf.getIndexConditions())) {
            throw new CacheException("Failed to prepare distributed join query: " + "join condition does not use index [joinedCache=" + tbl.cacheName() + ", plan=" + tf.getSelect().getPlanSQL() + ']');
        }
    }
    IndexColumn affCol = tbl.getAffinityKeyColumn();
    boolean affKeyCondFound = false;
    if (affCol != null) {
        ArrayList<IndexCondition> idxConditions = tf.getIndexConditions();
        int affColId = affCol.column.getColumnId();
        for (int i = 0; i < idxConditions.size(); i++) {
            IndexCondition c = idxConditions.get(i);
            int colId = c.getColumn().getColumnId();
            int cmpType = c.getCompareType();
            if ((cmpType == Comparison.EQUAL || cmpType == Comparison.EQUAL_NULL_SAFE) && (colId == affColId || tbl.rowDescriptor().isKeyColumn(colId)) && c.isEvaluatable()) {
                affKeyCondFound = true;
                Expression exp = c.getExpression();
                exp = exp.getNonAliasExpression();
                if (exp instanceof ExpressionColumn) {
                    ExpressionColumn expCol = (ExpressionColumn) exp;
                    // This is one of our previous joins.
                    TableFilter prevJoin = expCol.getTableFilter();
                    if (prevJoin != null) {
                        GridH2CollocationModel cm = child(indexOf(prevJoin), true);
                        // different affinity columns from different tables.
                        if (cm != null && !cm.view) {
                            Type t = cm.type(true);
                            if (t.isPartitioned() && t.isCollocated() && isAffinityColumn(prevJoin, expCol, validate))
                                return Affinity.COLLOCATED_JOIN;
                        }
                    }
                }
            }
        }
    }
    return affKeyCondFound ? Affinity.HAS_AFFINITY_CONDITION : Affinity.NONE;
}
Also used : TableFilter(org.h2.table.TableFilter) CacheException(javax.cache.CacheException) Expression(org.h2.expression.Expression) IndexCondition(org.h2.index.IndexCondition) IndexColumn(org.h2.table.IndexColumn) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 37 with TableFilter

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

the class Optimizer method calculateBruteForceSome.

private void calculateBruteForceSome() {
    int bruteForce = getMaxBruteForceFilters(filters.length);
    TableFilter[] list = new TableFilter[filters.length];
    Permutations<TableFilter> p = Permutations.create(filters, list, bruteForce);
    for (int x = 0; !canStop(x) && p.next(); x++) {
        // find out what filters are not used yet
        for (TableFilter f : filters) {
            f.setUsed(false);
        }
        for (int i = 0; i < bruteForce; i++) {
            list[i].setUsed(true);
        }
        // fill the remaining elements with the unused elements (greedy)
        for (int i = bruteForce; i < filters.length; i++) {
            double costPart = -1.0;
            int bestPart = -1;
            for (int j = 0; j < filters.length; j++) {
                if (!filters[j].isUsed()) {
                    if (i == filters.length - 1) {
                        bestPart = j;
                        break;
                    }
                    list[i] = filters[j];
                    Plan part = new Plan(list, i + 1, condition);
                    double costNow = part.calculateCost(session);
                    if (costPart < 0 || costNow < costPart) {
                        costPart = costNow;
                        bestPart = j;
                    }
                }
            }
            filters[bestPart].setUsed(true);
            list[i] = filters[bestPart];
        }
        testPlan(list);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) Plan(org.h2.table.Plan)

Example 38 with TableFilter

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

the class Optimizer method optimize.

/**
 * Calculate the best query plan to use.
 *
 * @param parse If we do not need to really get the best plan because it is
 *            a view parsing stage.
 */
void optimize(boolean parse) {
    if (parse) {
        calculateFakePlan();
    } else {
        calculateBestPlan();
        bestPlan.removeUnusableIndexConditions();
    }
    TableFilter[] f2 = bestPlan.getFilters();
    topFilter = f2[0];
    for (int i = 0; i < f2.length - 1; i++) {
        f2[i].addJoin(f2[i + 1], false, null);
    }
    if (parse) {
        return;
    }
    for (TableFilter f : f2) {
        PlanItem item = bestPlan.getItem(f);
        f.setPlanItem(item);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) PlanItem(org.h2.table.PlanItem)

Example 39 with TableFilter

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

the class Optimizer method shuffleAll.

private void shuffleAll(TableFilter[] f) {
    for (int i = 0; i < f.length - 1; i++) {
        int j = i + random.nextInt(f.length - i);
        if (j != i) {
            TableFilter temp = f[i];
            f[i] = f[j];
            f[j] = temp;
        }
    }
}
Also used : TableFilter(org.h2.table.TableFilter)

Example 40 with TableFilter

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

the class Optimizer method testPlan.

private boolean testPlan(TableFilter[] list) {
    Plan p = new Plan(list, list.length, condition);
    double costNow = p.calculateCost(session);
    if (cost < 0 || costNow < cost) {
        cost = costNow;
        bestPlan = p;
        return true;
    }
    return false;
}
Also used : Plan(org.h2.table.Plan)

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