Search in sources :

Example 6 with GridSqlJoin

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlJoin 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();
    boolean isForUpdate = SELECT_IS_FOR_UPDATE.get(select);
    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);
    if (isForUpdate) {
        if (!(from instanceof GridSqlTable || (from instanceof GridSqlAlias && from.size() == 1 && from.child() instanceof GridSqlTable))) {
            throw new IgniteSQLException("SELECT FOR UPDATE with joins is not supported.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        }
        GridSqlTable gridTbl = from instanceof GridSqlTable ? (GridSqlTable) from : ((GridSqlAlias) from).child();
        GridH2Table tbl = gridTbl.dataTable();
        if (tbl == null) {
            throw new IgniteSQLException("SELECT FOR UPDATE query must involve Ignite table.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        }
        if (select.getLimit() != null || select.getOffset() != null) {
            throw new IgniteSQLException("LIMIT/OFFSET clauses are not supported for SELECT FOR UPDATE.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        }
        if (SELECT_IS_GROUP_QUERY.get(select)) {
            throw new IgniteSQLException("SELECT FOR UPDATE with aggregates and/or GROUP BY is not supported.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        }
        if (select.isDistinct())
            throw new IgniteSQLException("DISTINCT clause is not supported for SELECT FOR UPDATE.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        if (SplitterUtils.hasSubQueries(res))
            throw new IgniteSQLException("Sub queries are not supported for SELECT FOR UPDATE.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    }
    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);
    res.forUpdate(isForUpdate);
    processSortOrder(select.getSortOrder(), res);
    res.limit(parseExpression(select.getLimit(), false));
    res.offset(parseExpression(select.getOffset(), false));
    return res;
}
Also used : ArrayList(java.util.ArrayList) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) 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) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Aggregations

GridSqlJoin (org.apache.ignite.internal.processors.query.h2.sql.GridSqlJoin)4 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)3 GridSqlSubquery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery)3 ArrayList (java.util.ArrayList)2 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)2 GridSqlAst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst)2 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)2 GridSqlQuery (org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)1 GridSqlAlias (org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias)1 GridSqlConst (org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst)1 GridSqlOperation (org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)1 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)1 GridSqlType.fromExpression (org.apache.ignite.internal.processors.query.h2.sql.GridSqlType.fromExpression)1 PartitionJoinCondition (org.apache.ignite.internal.sql.optimizer.affinity.PartitionJoinCondition)1 PartitionTable (org.apache.ignite.internal.sql.optimizer.affinity.PartitionTable)1 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 Expression (org.h2.expression.Expression)1 ValueExpression (org.h2.expression.ValueExpression)1 TableFilter (org.h2.table.TableFilter)1