Search in sources :

Example 41 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class GridSqlQuerySplitter method keyColumn.

/**
 * Retrieves _KEY column from SELECT. This column is used for SELECT FOR UPDATE statements.
 *
 * @param sel Select statement.
 * @return Key column alias.
 */
public static GridSqlAlias keyColumn(GridSqlSelect sel) {
    GridSqlAst from = sel.from();
    GridSqlTable tbl = from instanceof GridSqlTable ? (GridSqlTable) from : ((GridSqlElement) from).child();
    GridH2Table gridTbl = tbl.dataTable();
    Column h2KeyCol = gridTbl.getColumn(QueryUtils.KEY_COL);
    GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
    keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
    GridSqlAlias al = SplitterUtils.alias(QueryUtils.KEY_FIELD_NAME, keyCol);
    return al;
}
Also used : Column(org.h2.table.Column) GridSqlSelect.childIndexForColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect.childIndexForColumn) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Example 42 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class SqlAstTraverser method lookForPartitionedJoin.

/**
 * Traverse AST while join operation isn't found. Check it if found.
 *
 * @param ast AST item to check recursively.
 * @param upWhere Where condition that applies to this ast.
 */
private void lookForPartitionedJoin(GridSqlAst ast, GridSqlAst upWhere) {
    if (ast == null)
        return;
    GridSqlJoin join = null;
    GridSqlAst where = null;
    if (ast instanceof GridSqlJoin) {
        join = (GridSqlJoin) ast;
        where = upWhere;
    } else if (ast instanceof GridSqlSelect) {
        GridSqlSelect select = (GridSqlSelect) ast;
        if (select.from() instanceof GridSqlJoin) {
            join = (GridSqlJoin) select.from();
            where = select.where();
        }
    } else if (ast instanceof GridSqlSubquery)
        hasSubQueries = true;
    else if (ast instanceof GridSqlTable)
        hasPartitionedTables |= ((GridSqlTable) ast).dataTable().isPartitioned();
    // No joins on this level. Traverse AST deeper.
    if (join == null) {
        for (int i = 0; i < ast.size(); i++) lookForPartitionedJoin(ast.child(i), null);
        return;
    }
    // Check WHERE clause first.
    lookForPartitionedJoin(where, null);
    // Check left side of join.
    GridSqlTable leftTable = getTable(join.leftTable());
    GridH2Table left = null;
    // Left side of join is a subquery.
    if (leftTable == null) {
        hasSubQueries = true;
        // Check subquery on left side.
        lookForPartitionedJoin(join.leftTable(), where);
    } else {
        left = leftTable.dataTable();
        // Data table is NULL for views.
        if (left != null && left.isPartitioned())
            hasPartitionedTables = true;
    }
    // Check right side of join.
    GridSqlTable rightTable = getTable(join.rightTable());
    // Right side of join is a subquery.
    if (rightTable == null) {
        hasSubQueries = true;
        // Check subquery and return (can't exctract more info there).
        lookForPartitionedJoin(join.rightTable(), where);
        return;
    }
    GridH2Table right = rightTable.dataTable();
    if (right != null && right.isPartitioned())
        hasPartitionedTables = true;
    // Skip check of views.
    if (left == null || right == null)
        return;
    if (join.isLeftOuter() && !left.isPartitioned() && right.isPartitioned())
        hasOuterJoinReplicatedPartitioned = true;
    // Skip check if at least one of tables isn't partitioned.
    if (!(left.isPartitioned() && right.isPartitioned()))
        return;
    if (!distributedJoins)
        checkPartitionedJoin(join, where, left, right, log);
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Example 43 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class IgniteH2Indexing method createSortedIndex.

/**
     * Create sorted index.
     *
     * @param schema Schema.
     * @param name Index name,
     * @param tbl Table.
     * @param pk Primary key flag.
     * @param cols Columns.
     * @return Index.
     */
public GridH2IndexBase createSortedIndex(H2Schema schema, String name, GridH2Table tbl, boolean pk, List<IndexColumn> cols, int inlineSize) {
    try {
        GridCacheContext cctx = tbl.cache();
        if (log.isDebugEnabled())
            log.debug("Creating cache index [cacheId=" + cctx.cacheId() + ", idxName=" + name + ']');
        final int segments = tbl.rowDescriptor().context().config().getQueryParallelism();
        return new H2TreeIndex(cctx, tbl, name, pk, cols, inlineSize, segments);
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
}
Also used : H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException)

Example 44 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class DmlAstUtils method isEqualityCondition.

/**
     * @param op Operation.
     * @param key true - check for key equality condition,
     *            otherwise check for value equality condition
     * @return Whether this condition is of form {@code colName} = ?
     */
private static boolean isEqualityCondition(GridSqlOperation op, boolean key) {
    if (op.operationType() != GridSqlOperationType.EQUAL)
        return false;
    GridSqlElement left = op.child(0);
    GridSqlElement right = op.child(1);
    if (!(left instanceof GridSqlColumn))
        return false;
    GridSqlColumn column = (GridSqlColumn) left;
    if (!(column.column().getTable() instanceof GridH2Table))
        return false;
    GridH2RowDescriptor desc = ((GridH2Table) column.column().getTable()).rowDescriptor();
    return (key ? desc.isKeyColumn(column.column().getColumnId()) : desc.isValueColumn(column.column().getColumnId())) && (right instanceof GridSqlConst || right instanceof GridSqlParameter);
}
Also used : GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)

Example 45 with GridH2Table

use of org.apache.ignite.internal.processors.query.h2.opt.GridH2Table in project ignite by apache.

the class UpdatePlanBuilder method planForUpdate.

/**
     * Prepare update plan for UPDATE or DELETE.
     *
     * @param stmt UPDATE or DELETE statement.
     * @param errKeysPos index to inject param for re-run keys at. Null if it's not a re-run plan.
     * @return Update plan.
     * @throws IgniteCheckedException if failed.
     */
private static UpdatePlan planForUpdate(GridSqlStatement stmt, @Nullable Integer errKeysPos) throws IgniteCheckedException {
    GridSqlElement target;
    FastUpdateArguments fastUpdate;
    UpdateMode mode;
    if (stmt instanceof GridSqlUpdate) {
        // Let's verify that user is not trying to mess with key's columns directly
        verifyUpdateColumns(stmt);
        GridSqlUpdate update = (GridSqlUpdate) stmt;
        target = update.target();
        fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
        mode = UpdateMode.UPDATE;
    } else if (stmt instanceof GridSqlDelete) {
        GridSqlDelete del = (GridSqlDelete) stmt;
        target = del.from();
        fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
        mode = UpdateMode.DELETE;
    } else
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    GridSqlTable tbl = gridTableForElement(target);
    GridH2Table gridTbl = tbl.dataTable();
    GridH2RowDescriptor desc = gridTbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + gridTbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    if (fastUpdate != null)
        return UpdatePlan.forFastUpdate(mode, gridTbl, fastUpdate);
    else {
        GridSqlSelect sel;
        if (stmt instanceof GridSqlUpdate) {
            List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
            int valColIdx = -1;
            String[] colNames = new String[updatedCols.size()];
            int[] colTypes = new int[updatedCols.size()];
            for (int i = 0; i < updatedCols.size(); i++) {
                colNames[i] = updatedCols.get(i).columnName();
                colTypes[i] = updatedCols.get(i).resultType().type();
                Column column = updatedCols.get(i).column();
                if (desc.isValueColumn(column.getColumnId()))
                    valColIdx = i;
            }
            boolean hasNewVal = (valColIdx != -1);
            // Statement updates distinct properties if it does not have _val in updated columns list
            // or if its list of updated columns includes only _val, i.e. is single element.
            boolean hasProps = !hasNewVal || updatedCols.size() > 1;
            // Index of new _val in results of SELECT
            if (hasNewVal)
                valColIdx += 2;
            int newValColIdx = (hasNewVal ? valColIdx : 1);
            KeyValueSupplier newValSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
            sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt, errKeysPos);
            return UpdatePlan.forUpdate(gridTbl, colNames, colTypes, newValSupplier, valColIdx, sel.getSQL());
        } else {
            sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt, errKeysPos);
            return UpdatePlan.forDelete(gridTbl, sel.getSQL());
        }
    }
}
Also used : GridSqlDelete(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) Column(org.h2.table.Column) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlUpdate(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)

Aggregations

GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)66 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)26 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)18 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)18 Column (org.h2.table.Column)18 ArrayList (java.util.ArrayList)16 IgniteException (org.apache.ignite.IgniteException)15 HashSet (java.util.HashSet)13 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)12 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)11 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)11 SQLException (java.sql.SQLException)10 List (java.util.List)10 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)10 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)8 Map (java.util.Map)7 IgniteEx (org.apache.ignite.internal.IgniteEx)7 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)7 HashMap (java.util.HashMap)5