Search in sources :

Example 1 with FastUpdateArguments

use of org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments in project ignite by apache.

the class DmlAstUtils method getFastUpdateArgs.

/**
     * @param update UPDATE statement.
     * @return {@code null} if given statement directly updates {@code _val} column with a literal or param value
     * and filters by single non expression key (and, optionally,  by single non expression value).
     */
public static FastUpdateArguments getFastUpdateArgs(GridSqlUpdate update) {
    IgnitePair<GridSqlElement> filter = findKeyValueEqualityCondition(update.where());
    if (filter == null)
        return null;
    if (update.cols().size() != 1)
        return null;
    Table tbl = update.cols().get(0).column().getTable();
    if (!(tbl instanceof GridH2Table))
        return null;
    GridH2RowDescriptor desc = ((GridH2Table) tbl).rowDescriptor();
    if (!desc.isValueColumn(update.cols().get(0).column().getColumnId()))
        return null;
    GridSqlElement set = update.set().get(update.cols().get(0).columnName());
    if (!(set instanceof GridSqlConst || set instanceof GridSqlParameter))
        return null;
    return new FastUpdateArguments(operandForElement(filter.getKey()), operandForElement(filter.getValue()), operandForElement(set));
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) Table(org.h2.table.Table) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 2 with FastUpdateArguments

use of org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments in project ignite by apache.

the class DmlStatementsProcessor method doFastUpdate.

/**
     * Perform single cache operation based on given args.
     * @param args Query parameters.
     * @return 1 if an item was affected, 0 otherwise.
     * @throws IgniteCheckedException if failed.
     */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static UpdateResult doFastUpdate(UpdatePlan plan, Object[] args) throws IgniteCheckedException {
    GridCacheContext cctx = plan.tbl.rowDescriptor().context();
    FastUpdateArguments singleUpdate = plan.fastUpdateArgs;
    assert singleUpdate != null;
    boolean valBounded = (singleUpdate.val != FastUpdateArguments.NULL_ARGUMENT);
    if (singleUpdate.newVal != FastUpdateArguments.NULL_ARGUMENT) {
        // Single item UPDATE
        Object key = singleUpdate.key.apply(args);
        Object newVal = singleUpdate.newVal.apply(args);
        if (valBounded) {
            Object val = singleUpdate.val.apply(args);
            return (cctx.cache().replace(key, val, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
        } else
            return (cctx.cache().replace(key, newVal) ? UpdateResult.ONE : UpdateResult.ZERO);
    } else {
        // Single item DELETE
        Object key = singleUpdate.key.apply(args);
        Object val = singleUpdate.val.apply(args);
        if (// No _val bound in source query
        singleUpdate.val == FastUpdateArguments.NULL_ARGUMENT)
            return cctx.cache().remove(key) ? UpdateResult.ONE : UpdateResult.ZERO;
        else
            return cctx.cache().remove(key, val) ? UpdateResult.ONE : UpdateResult.ZERO;
    }
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) BinaryObject(org.apache.ignite.binary.BinaryObject) FastUpdateArguments(org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)

Example 3 with FastUpdateArguments

use of org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments 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

FastUpdateArguments (org.apache.ignite.internal.processors.query.h2.dml.FastUpdateArguments)2 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)2 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)2 BinaryObject (org.apache.ignite.binary.BinaryObject)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)1 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)1 GridSqlDelete (org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete)1 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)1 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)1 GridSqlTable (org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable)1 GridSqlUpdate (org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)1 Column (org.h2.table.Column)1 Table (org.h2.table.Table)1