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));
}
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;
}
}
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());
}
}
}
Aggregations