Search in sources :

Example 6 with TableFilter

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

the class GridH2CollocationModel method buildCollocationModel.

/**
 * @param upper Upper.
 * @param filter Filter.
 * @param qry Query.
 * @param unions Unions.
 * @param validate Query validation flag.
 * @return Built model.
 */
private static GridH2CollocationModel buildCollocationModel(GridH2CollocationModel upper, int filter, Query qry, List<GridH2CollocationModel> unions, boolean validate) {
    if (qry.isUnion()) {
        if (unions == null)
            unions = new ArrayList<>();
        SelectUnion union = (SelectUnion) qry;
        GridH2CollocationModel left = buildCollocationModel(upper, filter, union.getLeft(), unions, validate);
        GridH2CollocationModel right = buildCollocationModel(upper, filter, union.getRight(), unions, validate);
        assert left != null;
        assert right != null;
        return upper != null ? upper : left;
    }
    Select select = (Select) qry;
    List<TableFilter> list = new ArrayList<>();
    for (TableFilter f = select.getTopTableFilter(); f != null; f = f.getJoin()) list.add(f);
    TableFilter[] filters = list.toArray(new TableFilter[list.size()]);
    GridH2CollocationModel cm = createChildModel(upper, filter, unions, true, validate);
    cm.childFilters(filters);
    for (int i = 0; i < filters.length; i++) {
        TableFilter f = filters[i];
        if (f.getTable().isView())
            buildCollocationModel(cm, i, getSubQuery(f), null, validate);
        else if (f.getTable() instanceof GridH2Table)
            createChildModel(cm, i, null, false, validate);
    }
    return upper != null ? upper : cm;
}
Also used : SelectUnion(org.h2.command.dml.SelectUnion) TableFilter(org.h2.table.TableFilter) ArrayList(java.util.ArrayList) Select(org.h2.command.dml.Select)

Example 7 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 8 with TableFilter

use of org.h2.table.TableFilter 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)

Example 9 with TableFilter

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

the class GridSqlQueryParser method parseSelect.

/**
 * @param select Select.
 */
private GridSqlSelect parseSelect(Select select) {
    GridSqlSelect res = (GridSqlSelect) h2ObjToGridObj.get(select);
    if (res != null)
        return res;
    res = new GridSqlSelect();
    h2ObjToGridObj.put(select, res);
    res.distinct(select.isDistinct());
    Expression where = CONDITION.get(select);
    res.where(parseExpression(where, true));
    ArrayList<TableFilter> tableFilters = new ArrayList<>();
    TableFilter filter = select.getTopTableFilter();
    do {
        assert0(filter != null, select);
        assert0(filter.getNestedJoin() == null, select);
        // Can use optimized join order only if we are not inside of an expression.
        if (parsingSubQryExpression == 0 && optimizedTableFilterOrder != null) {
            String tblAlias = filter.getTableAlias();
            int idx = optimizedTableFilterOrder.get(tblAlias);
            setElementAt(tableFilters, idx, filter);
        } else
            tableFilters.add(filter);
        filter = filter.getJoin();
    } while (filter != null);
    // Build FROM clause from correctly ordered table filters.
    GridSqlElement from = null;
    for (int i = 0; i < tableFilters.size(); i++) {
        TableFilter f = tableFilters.get(i);
        GridSqlElement gridFilter = parseTableFilter(f);
        from = from == null ? gridFilter : new GridSqlJoin(from, gridFilter, f.isJoinOuter(), parseExpression(f.getJoinCondition(), true));
    }
    res.from(from);
    ArrayList<Expression> expressions = select.getExpressions();
    for (int i = 0; i < expressions.size(); i++) res.addColumn(parseExpression(expressions.get(i), true), i < select.getColumnCount());
    int[] grpIdx = GROUP_INDEXES.get(select);
    if (grpIdx != null)
        res.groupColumns(grpIdx);
    int havingIdx = HAVING_INDEX.get(select);
    if (havingIdx >= 0)
        res.havingColumn(havingIdx);
    processSortOrder(select.getSortOrder(), res);
    res.limit(parseExpression(select.getLimit(), false));
    res.offset(parseExpression(select.getOffset(), false));
    return res;
}
Also used : Expression(org.h2.expression.Expression) GridSqlType.fromExpression(org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression) ValueExpression(org.h2.expression.ValueExpression) TableFilter(org.h2.table.TableFilter) ArrayList(java.util.ArrayList) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint)

Aggregations

TableFilter (org.h2.table.TableFilter)5 Select (org.h2.command.dml.Select)4 IndexColumn (org.h2.table.IndexColumn)3 ArrayList (java.util.ArrayList)2 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)2 SelectUnion (org.h2.command.dml.SelectUnion)2 Expression (org.h2.expression.Expression)2 ExpressionColumn (org.h2.expression.ExpressionColumn)2 Table (org.h2.table.Table)2 CacheException (javax.cache.CacheException)1 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)1 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)1 CreateTable (org.h2.command.ddl.CreateTable)1 DropTable (org.h2.command.ddl.DropTable)1 Query (org.h2.command.dml.Query)1 ConditionInSelect (org.h2.expression.ConditionInSelect)1 ValueExpression (org.h2.expression.ValueExpression)1 IndexCondition (org.h2.index.IndexCondition)1 ViewIndex (org.h2.index.ViewIndex)1 Column (org.h2.table.Column)1