use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement 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());
}
}
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement in project ignite by apache.
the class UpdatePlanBuilder method gridTableForElement.
/**
* @param target Expression to extract the table from.
* @return Back end table for this element.
*/
private static GridSqlTable gridTableForElement(GridSqlElement target) {
Set<GridSqlTable> tbls = new HashSet<>();
DmlAstUtils.collectAllGridTablesInTarget(target, tbls);
if (tbls.size() != 1)
throw new IgniteSQLException("Failed to determine target table", IgniteQueryErrorCode.TABLE_NOT_FOUND);
return tbls.iterator().next();
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement in project ignite by apache.
the class DmlAstUtils method selectForUpdate.
/**
* Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
*
* @param update Update statement.
* @param keysParamIdx Index of new param for the array of keys.
* @return SELECT statement.
*/
public static GridSqlSelect selectForUpdate(GridSqlUpdate update, @Nullable Integer keysParamIdx) {
GridSqlSelect mapQry = new GridSqlSelect();
mapQry.from(update.target());
Set<GridSqlTable> tbls = new HashSet<>();
collectAllGridTablesInTarget(update.target(), tbls);
assert tbls.size() == 1 : "Failed to determine target table for UPDATE";
GridSqlTable tbl = tbls.iterator().next();
GridH2Table gridTbl = tbl.dataTable();
assert gridTbl != null : "Failed to determine target grid table for UPDATE";
Column h2KeyCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.KEY_COL);
Column h2ValCol = gridTbl.getColumn(GridH2KeyValueRowOnheap.VAL_COL);
GridSqlColumn keyCol = new GridSqlColumn(h2KeyCol, tbl, h2KeyCol.getName());
keyCol.resultType(GridSqlType.fromColumn(h2KeyCol));
GridSqlColumn valCol = new GridSqlColumn(h2ValCol, tbl, h2ValCol.getName());
valCol.resultType(GridSqlType.fromColumn(h2ValCol));
mapQry.addColumn(keyCol, true);
mapQry.addColumn(valCol, true);
for (GridSqlColumn c : update.cols()) {
String newColName = Parser.quoteIdentifier("_upd_" + c.columnName());
// We have to use aliases to cover cases when the user
// wants to update _val field directly (if it's a literal)
GridSqlAlias alias = new GridSqlAlias(newColName, elementOrDefault(update.set().get(c.columnName()), c), true);
alias.resultType(c.resultType());
mapQry.addColumn(alias, true);
}
GridSqlElement where = update.where();
if (keysParamIdx != null)
where = injectKeysFilterParam(where, keyCol, keysParamIdx);
mapQry.where(where);
mapQry.limit(update.limit());
return mapQry;
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement in project ignite by apache.
the class DmlAstUtils method gridTableForElement.
/**
* @param target Expression to extract the table from.
* @return Back end table for this element.
*/
public static GridSqlTable gridTableForElement(GridSqlElement target) {
Set<GridSqlTable> tbls = new HashSet<>();
collectAllGridTablesInTarget(target, tbls);
if (tbls.size() != 1)
throw new IgniteSQLException("Failed to determine target table", IgniteQueryErrorCode.TABLE_NOT_FOUND);
return tbls.iterator().next();
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement in project ignite by apache.
the class DmlAstUtils method findKeyValueEqualityCondition.
/**
* @param where Element to test.
* @return Whether given element corresponds to {@code WHERE _key = ?}, and key is a literal expressed
* in query or a query param.
*/
@SuppressWarnings("RedundantCast")
private static IgnitePair<GridSqlElement> findKeyValueEqualityCondition(GridSqlElement where) {
if (where == null || !(where instanceof GridSqlOperation))
return null;
GridSqlOperation whereOp = (GridSqlOperation) where;
// Does this WHERE limit only by _key?
if (isKeyEqualityCondition(whereOp))
return new IgnitePair<>((GridSqlElement) whereOp.child(1), null);
// Or maybe it limits both by _key and _val?
if (whereOp.operationType() != GridSqlOperationType.AND)
return null;
GridSqlElement left = whereOp.child(0);
GridSqlElement right = whereOp.child(1);
if (!(left instanceof GridSqlOperation && right instanceof GridSqlOperation))
return null;
GridSqlOperation leftOp = (GridSqlOperation) left;
GridSqlOperation rightOp = (GridSqlOperation) right;
if (isKeyEqualityCondition(leftOp)) {
// _key = ? and _val = ?
if (!isValueEqualityCondition(rightOp))
return null;
return new IgnitePair<>((GridSqlElement) leftOp.child(1), (GridSqlElement) rightOp.child(1));
} else if (isKeyEqualityCondition(rightOp)) {
// _val = ? and _key = ?
if (!isValueEqualityCondition(leftOp))
return null;
return new IgnitePair<>((GridSqlElement) rightOp.child(1), (GridSqlElement) leftOp.child(1));
} else
// Neither
return null;
}
Aggregations