Search in sources :

Example 36 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN 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 37 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class GridH2Table method commitUserIndex.

/**
 * Promote temporary index to make it usable in queries.
 *
 * @param ses H2 session.
 * @param idxName Index name.
 * @return Temporary index with given name.
 */
private Index commitUserIndex(Session ses, String idxName) {
    lock(true);
    try {
        ensureNotDestroyed();
        Index idx = tmpIdxs.remove(idxName);
        assert idx != null;
        Index cloneIdx = createDuplicateIndexIfNeeded(idx);
        ArrayList<Index> newIdxs = new ArrayList<>(idxs.size() + ((cloneIdx == null) ? 1 : 2));
        newIdxs.addAll(idxs);
        newIdxs.add(idx);
        if (cloneIdx != null)
            newIdxs.add(cloneIdx);
        idxs = newIdxs;
        database.addSchemaObject(ses, idx);
        if (cloneIdx != null)
            database.addSchemaObject(ses, cloneIdx);
        incrementModificationCounter();
        return idx;
    } finally {
        unlock(true);
    }
}
Also used : ArrayList(java.util.ArrayList) Index(org.h2.index.Index) H2TreeIndex(org.apache.ignite.internal.processors.query.h2.database.H2TreeIndex) SpatialIndex(org.h2.index.SpatialIndex)

Example 38 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class IgniteSqlSkipReducerOnUpdateDmlSelfTest method checkNoLeaks.

/**
 * Ensure there are no leaks in data structures associated with distributed dml execution.
 */
private void checkNoLeaks() {
    GridQueryProcessor qryProc = grid(NODE_CLIENT).context().query();
    IgniteH2Indexing h2Idx = GridTestUtils.getFieldValue(qryProc, GridQueryProcessor.class, "idx");
    GridReduceQueryExecutor rdcQryExec = GridTestUtils.getFieldValue(h2Idx, IgniteH2Indexing.class, "rdcQryExec");
    Map updRuns = GridTestUtils.getFieldValue(rdcQryExec, GridReduceQueryExecutor.class, "updRuns");
    assertEquals(0, updRuns.size());
    for (int idx = 0; idx < NODE_COUNT; idx++) {
        qryProc = grid(idx).context().query();
        h2Idx = GridTestUtils.getFieldValue(qryProc, GridQueryProcessor.class, "idx");
        GridMapQueryExecutor mapQryExec = GridTestUtils.getFieldValue(h2Idx, IgniteH2Indexing.class, "mapQryExec");
        Map qryRess = GridTestUtils.getFieldValue(mapQryExec, GridMapQueryExecutor.class, "qryRess");
        for (Object obj : qryRess.values()) {
            Map updCancels = GridTestUtils.getFieldValue(obj, "updCancels");
            assertEquals(0, updCancels.size());
        }
    }
}
Also used : IgniteH2Indexing(org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing) Map(java.util.Map) GridReduceQueryExecutor(org.apache.ignite.internal.processors.query.h2.twostep.GridReduceQueryExecutor) GridMapQueryExecutor(org.apache.ignite.internal.processors.query.h2.twostep.GridMapQueryExecutor)

Example 39 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class IgniteH2Indexing method executeUpdateNonTransactional.

/**
 * Execute update in non-transactional mode.
 *
 * @param qryId Query id.
 * @param qryDesc Query descriptor.
 * @param qryParams Query parameters.
 * @param dml Plan.
 * @param loc Local flag.
 * @param filters Filters.
 * @param cancel Cancel hook.
 * @return Update result.
 * @throws IgniteCheckedException If failed.
 */
private UpdateResult executeUpdateNonTransactional(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultDml dml, boolean loc, IndexingQueryFilter filters, GridQueryCancel cancel) throws IgniteCheckedException {
    UpdatePlan plan = dml.plan();
    UpdateResult fastUpdateRes = plan.processFast(qryParams.arguments());
    if (fastUpdateRes != null)
        return fastUpdateRes;
    DmlDistributedPlanInfo distributedPlan = loc ? null : plan.distributedPlan();
    if (distributedPlan != null) {
        if (cancel == null)
            cancel = new GridQueryCancel();
        UpdateResult result = rdcQryExec.update(qryDesc.schemaName(), distributedPlan.getCacheIds(), qryDesc.sql(), qryParams.arguments(), qryDesc.enforceJoinOrder(), qryParams.pageSize(), qryParams.timeout(), qryParams.partitions(), distributedPlan.isReplicatedOnly(), cancel);
        // Null is returned in case not all nodes support distributed DML.
        if (result != null)
            return result;
    }
    final GridQueryCancel selectCancel = (cancel != null) ? new GridQueryCancel() : null;
    if (cancel != null)
        cancel.add(selectCancel::cancel);
    SqlFieldsQuery selectFieldsQry = new SqlFieldsQuery(plan.selectQuery(), qryDesc.collocated()).setArgs(qryParams.arguments()).setDistributedJoins(qryDesc.distributedJoins()).setEnforceJoinOrder(qryDesc.enforceJoinOrder()).setLocal(qryDesc.local()).setPageSize(qryParams.pageSize()).setTimeout(qryParams.timeout(), TimeUnit.MILLISECONDS).setLazy(qryParams.lazy() && plan.canSelectBeLazy());
    Iterable<List<?>> cur;
    // sub-query and not some dummy stuff like "select 1, 2, 3;"
    if (!loc && !plan.isLocalSubquery()) {
        assert !F.isEmpty(plan.selectQuery());
        cur = executeSelectForDml(qryId, qryDesc.schemaName(), selectFieldsQry, null, selectCancel, qryParams.timeout());
    } else if (plan.hasRows())
        cur = plan.createRows(qryParams.arguments());
    else {
        selectFieldsQry.setLocal(true);
        QueryParserResult selectParseRes = parser.parse(qryDesc.schemaName(), selectFieldsQry, false);
        final GridQueryFieldsResult res = executeSelectLocal(qryId, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), filters, null, selectCancel, false, qryParams.timeout());
        cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                try {
                    return new GridQueryCacheObjectsIterator(res.iterator(), objectContext(), true);
                } catch (IgniteCheckedException e) {
                    throw new IgniteException(e);
                }
            }
        }, cancel, true, qryParams.lazy());
    }
    int pageSize = qryParams.updateBatchSize();
    // TODO: IGNITE-11176 - Need to support cancellation
    try {
        return DmlUtils.processSelectResult(plan, cur, pageSize);
    } finally {
        if (cur instanceof AutoCloseable)
            U.closeQuiet((AutoCloseable) cur);
    }
}
Also used : DmlDistributedPlanInfo(org.apache.ignite.internal.processors.query.h2.dml.DmlDistributedPlanInfo) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) GridCloseableIterator(org.apache.ignite.internal.util.lang.GridCloseableIterator) GridEmptyCloseableIterator(org.apache.ignite.internal.util.GridEmptyCloseableIterator) DmlUpdateSingleEntryIterator(org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateSingleEntryIterator) DmlUpdateResultsIterator(org.apache.ignite.internal.processors.query.h2.dml.DmlUpdateResultsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 40 with IN

use of org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN in project ignite by apache.

the class GridSubqueryJoinOptimizer method pullOutSubQryFromInClause.

/**
 * Pull out sub-select from IN clause to the parent select level.
 * <p>
 * Example:
 * <pre>
 *   Before:
 *     SELECT name
 *       FROM emp e
 *      WHERE e.dep_id IN (SELECT d.id FROM dep d WHERE d.name = 'dep1')
 *        AND sal > 2000
 *
 *   After:
 *     SELECT name
 *       FROM emp e
 *       JOIN dep d
 *      WHERE sal > 2000 AND d.id = e.dep_id and d.name = 'dep1
 * </pre>
 *
 * @param parent Parent select.
 * @param targetEl Target sql element. Can be {@code null}.
 * @param childInd Column index.
 */
private static boolean pullOutSubQryFromInClause(GridSqlSelect parent, @Nullable GridSqlAst targetEl, int childInd) {
    // extract sub-query
    GridSqlSubquery subQry = targetEl != null ? targetEl.child(childInd).child(1) : parent.where().child(1);
    if (!isSimpleSelect(subQry.subquery()))
        return false;
    GridSqlSelect subS = subQry.subquery();
    GridSqlAst leftExpr = targetEl != null ? targetEl.child(childInd).child(0) : parent.where().child(0);
    // 4) c1 + c2/const IN (SELECT in_col FROM ...)
    if (subS.visibleColumns() != 1)
        return false;
    List<GridSqlElement> conditions = new ArrayList<>();
    if (leftExpr instanceof GridSqlArray) {
        GridSqlAst col = subS.columns(true).get(0);
        if (!(col instanceof GridSqlArray) || leftExpr.size() != col.size())
            return false;
        for (int i = 0; i < col.size(); i++) {
            GridSqlElement el = leftExpr.child(i);
            conditions.add(new GridSqlOperation(EQUAL, el, col.child(i)));
        }
    } else if (targetEl instanceof GridSqlFunction)
        return false;
    else
        conditions.add(new GridSqlOperation(EQUAL, leftExpr, subS.columns(true).get(0)));
    // save old condition and IN expression to restore them
    // in case of unsuccessfull pull out
    GridSqlElement oldCond = (GridSqlElement) subS.where();
    if (oldCond != null)
        conditions.add(oldCond);
    GridSqlElement oldInExpr = targetEl != null ? targetEl.child(childInd) : (GridSqlElement) parent.where();
    // update sub-query condition and convert IN clause to EXISTS
    subS.where(buildConditionBush(conditions));
    GridSqlOperation existsExpr = new GridSqlOperation(EXISTS, subQry);
    if (targetEl != null)
        targetEl.child(childInd, existsExpr);
    else
        parent.where(existsExpr);
    boolean res = pullOutSubQryFromExistsClause(parent, targetEl, childInd);
    if (!res) {
        // restore original query in case of failure
        if (targetEl != null)
            targetEl.child(childInd, oldInExpr);
        else
            parent.where(oldInExpr);
        subS.where(oldCond);
    }
    return res;
}
Also used : GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) ArrayList(java.util.ArrayList) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlFunction(org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation) GridSqlArray(org.apache.ignite.internal.processors.query.h2.sql.GridSqlArray) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)35 ArrayList (java.util.ArrayList)29 IgniteException (org.apache.ignite.IgniteException)26 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)26 SQLException (java.sql.SQLException)21 List (java.util.List)21 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)21 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)19 CacheException (javax.cache.CacheException)15 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)13 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)13 Column (org.h2.table.Column)13 IgniteH2Indexing (org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing)10 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)10 ResultSet (java.sql.ResultSet)9 HashMap (java.util.HashMap)9 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)9 GridSqlSelect (org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect)9 PreparedStatement (java.sql.PreparedStatement)8 HashSet (java.util.HashSet)8