use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.
the class DmlUtils method doUpdate.
/**
* Perform UPDATE operation on top of results of SELECT.
* @param cursor SELECT results.
* @param pageSize Batch size for streaming, anything <= 0 for single page operations.
* @return Pair [cursor corresponding to results of UPDATE (contains number of items affected); keys whose values
* had been modified concurrently (arguments for a re-run)].
*/
private static UpdateResult doUpdate(UpdatePlan plan, Iterable<List<?>> cursor, int pageSize) throws IgniteCheckedException {
GridCacheContext cctx = plan.cacheContext();
DmlBatchSender sender = new DmlBatchSender(cctx, pageSize, 1);
for (List<?> row : cursor) {
T3<Object, Object, Object> row0 = plan.processRowForUpdate(row);
Object key = row0.get1();
Object oldVal = row0.get2();
Object newVal = row0.get3();
sender.add(key, new DmlStatementsProcessor.ModifyingEntryProcessor(oldVal, new DmlStatementsProcessor.EntryValueUpdater(newVal)), 0);
}
sender.flush();
SQLException resEx = sender.error();
if (resEx != null) {
if (!F.isEmpty(sender.failedKeys())) {
// Don't go for a re-run if processing of some keys yielded exceptions and report keys that
// had been modified concurrently right away.
String msg = "Failed to UPDATE some keys because they had been modified concurrently " + "[keys=" + sender.failedKeys() + ']';
SQLException dupEx = createJdbcSqlException(msg, IgniteQueryErrorCode.CONCURRENT_UPDATE);
dupEx.setNextException(resEx);
resEx = dupEx;
}
throw new IgniteSQLException(resEx);
}
return new UpdateResult(sender.updateCount(), sender.failedKeys().toArray(), cursor instanceof QueryCursorImpl ? ((QueryCursorImpl) cursor).partitionResult() : null);
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND 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 FastUpdate 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 FastUpdate.create(filter.getKey(), filter.getValue(), set);
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.
the class DmlAstUtils method selectForUpdate.
/**
* Generate SQL SELECT based on UPDATE's WHERE, LIMIT, etc.
*
* @param update Update statement.
* @return SELECT statement.
*/
public static GridSqlSelect selectForUpdate(GridSqlUpdate update) {
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(QueryUtils.KEY_COL);
Column h2ValCol = gridTbl.getColumn(QueryUtils.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();
// On no MVCC mode we cannot use lazy mode when UPDATE query contains index with updated columns
// and that index may be chosen to scan by WHERE condition
// because in this case any rows update may be updated several times.
// e.g. in the cases below we cannot use lazy mode:
//
// 1. CREATE INDEX idx on test(val)
// UPDATE test SET val = val + 1 WHERE val >= ?
//
// 2. CREATE INDEX idx on test(val0, val1)
// UPDATE test SET val1 = val1 + 1 WHERE val0 >= ?
mapQry.canBeLazy(!isIndexWithUpdateColumnsMayBeUsed(gridTbl, update.cols().stream().map(GridSqlColumn::column).collect(Collectors.toSet()), extractColumns(gridTbl, where)));
mapQry.where(where);
mapQry.limit(update.limit());
return mapQry;
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.
the class GridH2Table method removeChildrenAndResources.
/**
* {@inheritDoc}
*/
@Override
public void removeChildrenAndResources(Session ses) {
lock(true);
try {
super.removeChildrenAndResources(ses);
// Clear all user indexes registered in schema.
while (idxs.size() > sysIdxsCnt) {
Index idx = idxs.get(sysIdxsCnt);
if (idx.getName() != null && idx.getSchema().findIndex(ses, idx.getName()) == idx) {
// This call implicitly removes both idx and its proxy, if any, from idxs.
database.removeSchemaObject(ses, idx);
// We have to call destroy here if we are who has removed this index from the table.
destroyIndex(idx);
}
}
if (SysProperties.CHECK) {
for (SchemaObject obj : database.getAllSchemaObjects(DbObject.INDEX)) {
Index idx = (Index) obj;
if (idx.getTable() == this)
DbException.throwInternalError("index not dropped: " + idx.getName());
}
}
database.removeMeta(ses, getId());
invalidate();
} finally {
unlock(true);
}
}
use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND in project ignite by apache.
the class OpenCensusSqlNativeTracingTest method testDistributedJoin.
/**
* Tests tracing of distributed join query which includes all communications between reducer and mapped nodes and
* index range requests.
*
* @throws Exception If failed.
*/
@Test
public void testDistributedJoin() throws Exception {
String prsnTable = createTableAndPopulate(Person.class, PARTITIONED, 1);
String orgTable = createTableAndPopulate(Organization.class, PARTITIONED, 1);
SpanId rootSpan = executeAndCheckRootSpan("SELECT * FROM " + prsnTable + " AS p JOIN " + orgTable + " AS o ON o.orgId = p.prsnId", TEST_SCHEMA, false, true, true);
String qryId = getAttribute(rootSpan, SQL_QRY_ID);
assertTrue(Long.parseLong(qryId.substring(qryId.indexOf('_') + 1)) > 0);
UUID.fromString(qryId.substring(0, qryId.indexOf('_')));
checkChildSpan(SQL_QRY_PARSE, rootSpan);
checkChildSpan(SQL_CURSOR_OPEN, rootSpan);
SpanId iterSpan = checkChildSpan(SQL_ITER_OPEN, rootSpan);
List<SpanId> execReqSpans = checkSpan(SQL_QRY_EXEC_REQ, iterSpan, GRID_CNT, null);
int idxRangeReqRows = 0;
int preparedRows = 0;
int fetchedRows = 0;
for (int i = 0; i < GRID_CNT; i++) {
SpanId execReqSpan = execReqSpans.get(i);
Ignite ignite = Ignition.ignite(UUID.fromString(getAttribute(execReqSpan, NODE_ID)));
SpanId partsReserveSpan = checkChildSpan(SQL_PARTITIONS_RESERVE, execReqSpan);
List<String> partsReserveLogs = handler().spanById(partsReserveSpan).getAnnotations().getEvents().stream().map(e -> e.getEvent().getDescription()).collect(Collectors.toList());
assertEquals(2, partsReserveLogs.size());
Pattern ptrn = compile("Cache partitions were reserved \\[cache=(.+), partitions=\\[(.+)], topology=(.+)]");
partsReserveLogs.forEach(l -> {
Matcher matcher = ptrn.matcher(l);
assertTrue(matcher.matches());
Set<Integer> expParts = Arrays.stream(ignite.affinity(matcher.group(1)).primaryPartitions(ignite.cluster().localNode())).boxed().collect(Collectors.toSet());
Set<Integer> parts = Arrays.stream(matcher.group(2).split(",")).map(s -> parseInt(s.trim())).collect(Collectors.toSet());
assertEquals(expParts, parts);
});
SpanId execSpan = checkChildSpan(SQL_QRY_EXECUTE, execReqSpan);
List<SpanId> distrLookupReqSpans = findChildSpans(SQL_IDX_RANGE_REQ, execSpan);
for (SpanId span : distrLookupReqSpans) {
idxRangeReqRows += parseInt(getAttribute(span, SQL_IDX_RANGE_ROWS));
checkChildSpan(SQL_IDX_RANGE_RESP, span);
}
preparedRows += parseInt(getAttribute(checkChildSpan(SQL_PAGE_PREPARE, execReqSpan), SQL_PAGE_ROWS));
checkChildSpan(SQL_PAGE_RESP, execReqSpan);
}
SpanId pageFetchSpan = checkChildSpan(SQL_PAGE_FETCH, iterSpan);
fetchedRows += parseInt(getAttribute(pageFetchSpan, SQL_PAGE_ROWS));
checkChildSpan(SQL_PAGE_WAIT, pageFetchSpan);
SpanId nexPageSpan = checkChildSpan(SQL_NEXT_PAGE_REQ, pageFetchSpan);
preparedRows += parseInt(getAttribute(checkChildSpan(SQL_PAGE_PREPARE, nexPageSpan), SQL_PAGE_ROWS));
checkChildSpan(SQL_PAGE_RESP, nexPageSpan);
List<SpanId> pageFetchSpans = findChildSpans(SQL_PAGE_FETCH, rootSpan);
for (SpanId span : pageFetchSpans) {
fetchedRows += parseInt(getAttribute(span, SQL_PAGE_ROWS));
checkChildSpan(SQL_PAGE_WAIT, span);
List<SpanId> nextPageSpans = findChildSpans(SQL_NEXT_PAGE_REQ, span);
if (!nextPageSpans.isEmpty()) {
assertEquals(1, nextPageSpans.size());
SpanId nextPageSpan = nextPageSpans.get(0);
preparedRows += parseInt(getAttribute(checkChildSpan(SQL_PAGE_PREPARE, nextPageSpan), SQL_PAGE_ROWS));
checkChildSpan(SQL_PAGE_RESP, nextPageSpan);
}
}
assertEquals(TEST_TABLE_POPULATION, fetchedRows);
assertEquals(TEST_TABLE_POPULATION, preparedRows);
assertEquals(TEST_TABLE_POPULATION, idxRangeReqRows);
checkSpan(SQL_QRY_CANCEL_REQ, rootSpan, mapNodesCount(), null);
assertFalse(findChildSpans(SQL_CURSOR_CLOSE, rootSpan).isEmpty());
}
Aggregations