Search in sources :

Example 11 with TableFilter

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

the class Optimizer method shuffleTwo.

private boolean shuffleTwo(TableFilter[] f) {
    int a = 0, b = 0, i = 0;
    for (; i < 20; i++) {
        a = random.nextInt(f.length);
        b = random.nextInt(f.length);
        if (a == b) {
            continue;
        }
        if (a < b) {
            int temp = a;
            a = b;
            b = temp;
        }
        int s = a * f.length + b;
        if (switched.get(s)) {
            continue;
        }
        switched.set(s);
        break;
    }
    if (i == 20) {
        return false;
    }
    TableFilter temp = f[a];
    f[a] = f[b];
    f[b] = temp;
    return true;
}
Also used : TableFilter(org.h2.table.TableFilter)

Example 12 with TableFilter

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

the class Select method expandColumnList.

private void expandColumnList() {
    Database db = session.getDatabase();
    // the expressions may change within the loop
    for (int i = 0; i < expressions.size(); i++) {
        Expression expr = expressions.get(i);
        if (!expr.isWildcard()) {
            continue;
        }
        String schemaName = expr.getSchemaName();
        String tableAlias = expr.getTableAlias();
        if (tableAlias == null) {
            expressions.remove(i);
            for (TableFilter filter : filters) {
                i = expandColumnList(filter, i);
            }
            i--;
        } else {
            TableFilter filter = null;
            for (TableFilter f : filters) {
                if (db.equalsIdentifiers(tableAlias, f.getTableAlias())) {
                    if (schemaName == null || db.equalsIdentifiers(schemaName, f.getSchemaName())) {
                        filter = f;
                        break;
                    }
                }
            }
            if (filter == null) {
                throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableAlias);
            }
            expressions.remove(i);
            i = expandColumnList(filter, i);
            i--;
        }
    }
}
Also used : Database(org.h2.engine.Database)

Example 13 with TableFilter

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

the class Select method prepare.

@Override
public void prepare() {
    if (isPrepared) {
        // sometimes a subquery is prepared twice (CREATE TABLE AS SELECT)
        return;
    }
    if (SysProperties.CHECK && !checkInit) {
        DbException.throwInternalError("not initialized");
    }
    if (orderList != null) {
        sort = prepareOrder(orderList, expressions.size());
        orderList = null;
    }
    ColumnNamer columnNamer = new ColumnNamer(session);
    for (int i = 0; i < expressions.size(); i++) {
        Expression e = expressions.get(i);
        String proposedColumnName = e.getAlias();
        String columnName = columnNamer.getColumnName(e, i, proposedColumnName);
        // if the name changed, create an alias
        if (!columnName.equals(proposedColumnName)) {
            e = new Alias(e, columnName, true);
        }
        expressions.set(i, e.optimize(session));
    }
    if (condition != null) {
        condition = condition.optimize(session);
        for (TableFilter f : filters) {
            // left outer join child on p = pc where c is null;
            if (!f.isJoinOuter() && !f.isJoinOuterIndirect()) {
                condition.createIndexConditions(session, f);
            }
        }
    }
    if (isGroupQuery && groupIndex == null && havingIndex < 0 && filters.size() == 1) {
        if (condition == null) {
            Table t = filters.get(0).getTable();
            ExpressionVisitor optimizable = ExpressionVisitor.getOptimizableVisitor(t);
            isQuickAggregateQuery = isEverything(optimizable);
        }
    }
    cost = preparePlan(session.isParsingCreateView());
    if (distinct && session.getDatabase().getSettings().optimizeDistinct && !isGroupQuery && filters.size() == 1 && expressions.size() == 1 && condition == null) {
        Expression expr = expressions.get(0);
        expr = expr.getNonAliasExpression();
        if (expr instanceof ExpressionColumn) {
            Column column = ((ExpressionColumn) expr).getColumn();
            int selectivity = column.getSelectivity();
            Index columnIndex = topTableFilter.getTable().getIndexForColumn(column, false, true);
            if (columnIndex != null && selectivity != Constants.SELECTIVITY_DEFAULT && selectivity < 20) {
                // the first column must be ascending
                boolean ascending = columnIndex.getIndexColumns()[0].sortType == SortOrder.ASCENDING;
                Index current = topTableFilter.getIndex();
                // if another index is faster
                if (columnIndex.canFindNext() && ascending && (current == null || current.getIndexType().isScan() || columnIndex == current)) {
                    IndexType type = columnIndex.getIndexType();
                    // indexes don't work
                    if (!type.isHash() && (!type.isUnique() || columnIndex.getColumns().length > 1)) {
                        topTableFilter.setIndex(columnIndex);
                        isDistinctQuery = true;
                    }
                }
            }
        }
    }
    if (sort != null && !isQuickAggregateQuery && !isGroupQuery) {
        Index index = getSortIndex();
        Index current = topTableFilter.getIndex();
        if (index != null && current != null) {
            if (current.getIndexType().isScan() || current == index) {
                topTableFilter.setIndex(index);
                if (!topTableFilter.hasInComparisons()) {
                    // in(select ...) and in(1,2,3) may return the key in
                    // another order
                    sortUsingIndex = true;
                }
            } else if (index.getIndexColumns() != null && index.getIndexColumns().length >= current.getIndexColumns().length) {
                IndexColumn[] sortColumns = index.getIndexColumns();
                IndexColumn[] currentColumns = current.getIndexColumns();
                boolean swapIndex = false;
                for (int i = 0; i < currentColumns.length; i++) {
                    if (sortColumns[i].column != currentColumns[i].column) {
                        swapIndex = false;
                        break;
                    }
                    if (sortColumns[i].sortType != currentColumns[i].sortType) {
                        swapIndex = true;
                    }
                }
                if (swapIndex) {
                    topTableFilter.setIndex(index);
                    sortUsingIndex = true;
                }
            }
        }
    }
    if (!isQuickAggregateQuery && isGroupQuery && getGroupByExpressionCount() > 0) {
        Index index = getGroupSortedIndex();
        Index current = topTableFilter.getIndex();
        if (index != null && current != null && (current.getIndexType().isScan() || current == index)) {
            topTableFilter.setIndex(index);
            isGroupSortedQuery = true;
        }
    }
    expressionArray = expressions.toArray(new Expression[0]);
    isPrepared = true;
}
Also used : Index(org.h2.index.Index) IndexType(org.h2.index.IndexType)

Example 14 with TableFilter

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

the class Delete method prepare.

@Override
public void prepare() {
    if (condition != null) {
        condition.mapColumns(targetTableFilter, 0);
        if (sourceTableFilter != null) {
            condition.mapColumns(sourceTableFilter, 0);
        }
        condition = condition.optimize(session);
        condition.createIndexConditions(session, targetTableFilter);
    }
    TableFilter[] filters;
    if (sourceTableFilter == null) {
        filters = new TableFilter[] { targetTableFilter };
    } else {
        filters = new TableFilter[] { targetTableFilter, sourceTableFilter };
    }
    PlanItem item = targetTableFilter.getBestPlanItem(session, filters, 0, ExpressionVisitor.allColumnsForTableFilters(filters));
    targetTableFilter.setPlanItem(item);
    targetTableFilter.prepare();
}
Also used : TableFilter(org.h2.table.TableFilter) PlanItem(org.h2.table.PlanItem)

Example 15 with TableFilter

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

the class ExpressionColumn method createIndexConditions.

@Override
public void createIndexConditions(Session session, TableFilter filter) {
    TableFilter tf = getTableFilter();
    if (filter == tf && column.getType() == Value.BOOLEAN) {
        IndexCondition cond = IndexCondition.get(Comparison.EQUAL, this, ValueExpression.get(ValueBoolean.TRUE));
        filter.addIndexCondition(cond);
    }
}
Also used : TableFilter(org.h2.table.TableFilter) IndexCondition(org.h2.index.IndexCondition)

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