Search in sources :

Example 31 with AND

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);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) DmlStatementsProcessor(org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor) UpdateResult(org.apache.ignite.internal.processors.query.h2.UpdateResult)

Example 32 with AND

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);
}
Also used : GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) Table(org.h2.table.Table) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlConst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlParameter(org.apache.ignite.internal.processors.query.h2.sql.GridSqlParameter)

Example 33 with AND

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;
}
Also used : GridSqlAlias(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Column(org.h2.table.Column) 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) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) ValueString(org.h2.value.ValueString) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) HashSet(java.util.HashSet)

Example 34 with AND

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);
    }
}
Also used : SchemaObject(org.h2.schema.SchemaObject) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) SpatialIndex(org.h2.index.SpatialIndex)

Example 35 with AND

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());
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) SQL_DML_QRY_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_RESP) SQL_NEXT_PAGE_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_NEXT_PAGE_REQ) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) Arrays(java.util.Arrays) SQL_IDX_RANGE_ROWS(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_IDX_RANGE_ROWS) SpanType(org.apache.ignite.internal.processors.tracing.SpanType) SQL_PAGE_ROWS(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_PAGE_ROWS) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SQL_QRY_ID(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_QRY_ID) SQL_CMD_QRY_EXECUTE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CMD_QRY_EXECUTE) IgniteEx(org.apache.ignite.internal.IgniteEx) SQL_FAIL_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_FAIL_RESP) GridTestUtils.runAsync(org.apache.ignite.testframework.GridTestUtils.runAsync) NoopSpan(org.apache.ignite.internal.processors.tracing.NoopSpan) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQL_PAGE_WAIT(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_WAIT) SpanId(io.opencensus.trace.SpanId) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) NODE(org.apache.ignite.internal.processors.tracing.SpanTags.NODE) Tracing(io.opencensus.trace.Tracing) TracingConfigurationCoordinates(org.apache.ignite.spi.tracing.TracingConfigurationCoordinates) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) ImmutableMap(com.google.common.collect.ImmutableMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Set(java.util.Set) SQL_IDX_RANGE_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_IDX_RANGE_RESP) SQL_ITER_CLOSE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_ITER_CLOSE) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) DFLT_SCHEMA(org.apache.ignite.internal.processors.query.QueryUtils.DFLT_SCHEMA) SQL_CURSOR_CANCEL(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CURSOR_CANCEL) SAMPLING_RATE_ALWAYS(org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_ALWAYS) MTC(org.apache.ignite.internal.processors.tracing.MTC) TracingSpi(org.apache.ignite.spi.tracing.TracingSpi) SQL_IDX_RANGE_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_IDX_RANGE_REQ) SQL_CURSOR_OPEN(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CURSOR_OPEN) SQL(org.apache.ignite.spi.tracing.Scope.SQL) Pattern(java.util.regex.Pattern) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) GridQueryNextPageRequest(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageRequest) SQL_QRY_EXEC_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_EXEC_REQ) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) SQL_CACHE_UPDATE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CACHE_UPDATE) SQL_SCHEMA(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_SCHEMA) U(org.apache.ignite.internal.util.typedef.internal.U) SQL_PARTITIONS_RESERVE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PARTITIONS_RESERVE) SQL_QRY_CANCEL_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_CANCEL_REQ) SQL_DML_QRY_EXECUTE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_EXECUTE) OpenCensusTracingSpi(org.apache.ignite.spi.tracing.opencensus.OpenCensusTracingSpi) SQL_PAGE_RESP(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_RESP) SQL_QRY_TEXT(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_QRY_TEXT) SQL_CACHE_UPDATES(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_CACHE_UPDATES) SpanTags.tag(org.apache.ignite.internal.processors.tracing.SpanTags.tag) SQL_ITER_OPEN(org.apache.ignite.internal.processors.tracing.SpanType.SQL_ITER_OPEN) Iterator(java.util.Iterator) Pattern.compile(java.util.regex.Pattern.compile) SQL_CURSOR_CLOSE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_CURSOR_CLOSE) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) Test(org.junit.Test) SQL_PAGE_FETCH(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_FETCH) Ignite(org.apache.ignite.Ignite) SQL_PARSER_CACHE_HIT(org.apache.ignite.internal.processors.tracing.SpanTags.SQL_PARSER_CACHE_HIT) SQL_DML_QRY_EXEC_REQ(org.apache.ignite.internal.processors.tracing.SpanType.SQL_DML_QRY_EXEC_REQ) NAME(org.apache.ignite.internal.processors.tracing.SpanTags.NAME) Integer.parseInt(java.lang.Integer.parseInt) SQL_QRY(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY) SQL_PAGE_PREPARE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_PAGE_PREPARE) CONSISTENT_ID(org.apache.ignite.internal.processors.tracing.SpanTags.CONSISTENT_ID) NODE_ID(org.apache.ignite.internal.processors.tracing.SpanTags.NODE_ID) Ignition(org.apache.ignite.Ignition) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TracingConfigurationParameters(org.apache.ignite.spi.tracing.TracingConfigurationParameters) SQL_QRY_PARSE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_PARSE) SQL_QRY_EXECUTE(org.apache.ignite.internal.processors.tracing.SpanType.SQL_QRY_EXECUTE) CacheMode(org.apache.ignite.cache.CacheMode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Ignite(org.apache.ignite.Ignite) SpanId(io.opencensus.trace.SpanId) Test(org.junit.Test)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)37 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)33 ArrayList (java.util.ArrayList)26 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)25 List (java.util.List)22 IgniteException (org.apache.ignite.IgniteException)21 SQLException (java.sql.SQLException)15 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)15 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)13 HashMap (java.util.HashMap)12 Column (org.h2.table.Column)12 LinkedHashMap (java.util.LinkedHashMap)11 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)11 Index (org.h2.index.Index)11 PreparedStatement (java.sql.PreparedStatement)9 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)9 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)9 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)9 GridSqlColumn (org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn)9 GridSqlElement (org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement)9