Search in sources :

Example 36 with AND

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

the class IgniteH2Indexing method lockSelectedRows.

/**
 * Locks rows from query cursor and returns the select result.
 *
 * @param cur Query cursor.
 * @param cctx Cache context.
 * @param pageSize Page size.
 * @param timeout Timeout.
 * @return Query results cursor.
 */
private Iterable<List<?>> lockSelectedRows(Iterable<List<?>> cur, GridCacheContext cctx, int pageSize, long timeout) {
    assert cctx != null && cctx.mvccEnabled();
    GridNearTxLocal tx = tx(ctx);
    if (tx == null)
        throw new IgniteSQLException("Failed to perform SELECT FOR UPDATE operation: transaction has already finished.");
    Collection<List<?>> rowsCache = new ArrayList<>();
    UpdateSourceIterator srcIt = new UpdateSourceIterator<KeyCacheObject>() {

        private Iterator<List<?>> it = cur.iterator();

        @Override
        public EnlistOperation operation() {
            return EnlistOperation.LOCK;
        }

        @Override
        public boolean hasNextX() throws IgniteCheckedException {
            return it.hasNext();
        }

        @Override
        public KeyCacheObject nextX() throws IgniteCheckedException {
            List<?> res = it.next();
            // nextX() can be called from the different threads.
            synchronized (rowsCache) {
                rowsCache.add(res.subList(0, res.size() - 1));
                if (rowsCache.size() > TX_SIZE_THRESHOLD) {
                    throw new IgniteCheckedException("Too many rows are locked by SELECT FOR UPDATE statement. " + "Consider locking fewer keys or increase the limit by setting a " + IGNITE_MVCC_TX_SIZE_CACHING_THRESHOLD + " system property. Current value is " + TX_SIZE_THRESHOLD + " rows.");
                }
            }
            // The last column is expected to be a _key.
            return cctx.toCacheKeyObject(res.get(res.size() - 1));
        }
    };
    IgniteInternalFuture<Long> fut = tx.updateAsync(cctx, srcIt, pageSize, timeout, true);
    try {
        fut.get();
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
    return rowsCache;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UpdateSourceIterator(org.apache.ignite.internal.processors.query.UpdateSourceIterator) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) 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) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 37 with AND

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

the class IgniteH2Indexing method executeUpdateOnDataNodeTransactional.

/**
 * {@inheritDoc}
 */
@Override
public UpdateSourceIterator<?> executeUpdateOnDataNodeTransactional(GridCacheContext<?, ?> cctx, int[] ids, int[] parts, String schema, String qry, Object[] params, int flags, int pageSize, int timeout, AffinityTopologyVersion topVer, MvccSnapshot mvccSnapshot, GridQueryCancel cancel) throws IgniteCheckedException {
    SqlFieldsQuery fldsQry = QueryUtils.withQueryTimeout(new SqlFieldsQuery(qry), timeout, TimeUnit.MILLISECONDS);
    if (params != null)
        fldsQry.setArgs(params);
    fldsQry.setEnforceJoinOrder(U.isFlagSet(flags, GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER));
    fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS);
    fldsQry.setPageSize(pageSize);
    fldsQry.setLocal(true);
    fldsQry.setLazy(U.isFlagSet(flags, GridH2QueryRequest.FLAG_LAZY));
    boolean loc = true;
    final boolean replicated = U.isFlagSet(flags, GridH2QueryRequest.FLAG_REPLICATED);
    GridCacheContext<?, ?> cctx0;
    if (!replicated && !F.isEmpty(ids) && (cctx0 = CU.firstPartitioned(cctx.shared(), ids)) != null && cctx0.config().getQueryParallelism() > 1) {
        fldsQry.setDistributedJoins(true);
        loc = false;
    }
    QueryParserResult parseRes = parser.parse(schema, fldsQry, false);
    assert parseRes.remainingQuery() == null;
    QueryParserResultDml dml = parseRes.dml();
    assert dml != null;
    IndexingQueryFilter filter = backupFilter(topVer, parts);
    UpdatePlan plan = dml.plan();
    GridCacheContext planCctx = plan.cacheContext();
    // Force keepBinary for operation context to avoid binary deserialization inside entry processor
    DmlUtils.setKeepBinaryContext(planCctx);
    SqlFieldsQuery selectFieldsQry = QueryUtils.withQueryTimeout(new SqlFieldsQuery(plan.selectQuery(), fldsQry.isCollocated()), fldsQry.getTimeout(), TimeUnit.MILLISECONDS).setArgs(fldsQry.getArgs()).setDistributedJoins(fldsQry.isDistributedJoins()).setEnforceJoinOrder(fldsQry.isEnforceJoinOrder()).setLocal(fldsQry.isLocal()).setPageSize(fldsQry.getPageSize()).setTimeout(fldsQry.getTimeout(), TimeUnit.MILLISECONDS).setLazy(fldsQry.isLazy());
    QueryCursorImpl<List<?>> cur;
    // sub-query and not some dummy stuff like "select 1, 2, 3;"
    if (!loc && !plan.isLocalSubquery()) {
        cur = executeSelectForDml(RunningQueryManager.UNDEFINED_QUERY_ID, schema, selectFieldsQry, new StaticMvccQueryTracker(planCctx, mvccSnapshot), cancel, timeout);
    } else {
        selectFieldsQry.setLocal(true);
        QueryParserResult selectParseRes = parser.parse(schema, selectFieldsQry, false);
        GridQueryFieldsResult res = executeSelectLocal(RunningQueryManager.UNDEFINED_QUERY_ID, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), filter, new StaticMvccQueryTracker(planCctx, mvccSnapshot), cancel, true, timeout);
        cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                try {
                    return res.iterator();
                } catch (IgniteCheckedException e) {
                    throw new IgniteException(e);
                }
            }
        }, cancel, true, selectParseRes.queryParameters().lazy());
    }
    return plan.iteratorForTransaction(connMgr, cur);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) StaticMvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.StaticMvccQueryTracker) 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) 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 38 with AND

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

the class CommandProcessor method runCommandNativeDdl.

/**
 * Run DDL statement.
 *
 * @param sql Original SQL.
 * @param cmd Command.
 */
private void runCommandNativeDdl(String sql, SqlCommand cmd) {
    IgniteInternalFuture fut = null;
    try {
        isDdlOnSchemaSupported(cmd.schemaName());
        finishActiveTxIfNecessary();
        if (cmd instanceof SqlCreateIndexCommand) {
            SqlCreateIndexCommand cmd0 = (SqlCreateIndexCommand) cmd;
            GridH2Table tbl = schemaMgr.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null)
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            assert tbl.rowDescriptor() != null;
            ensureDdlSupported(tbl);
            QueryIndex newIdx = new QueryIndex();
            newIdx.setName(cmd0.indexName());
            newIdx.setIndexType(cmd0.spatial() ? QueryIndexType.GEOSPATIAL : QueryIndexType.SORTED);
            LinkedHashMap<String, Boolean> flds = new LinkedHashMap<>();
            // Let's replace H2's table and property names by those operated by GridQueryProcessor.
            GridQueryTypeDescriptor typeDesc = tbl.rowDescriptor().type();
            for (SqlIndexColumn col : cmd0.columns()) {
                GridQueryProperty prop = typeDesc.property(col.name());
                if (prop == null)
                    throw new SchemaOperationException(SchemaOperationException.CODE_COLUMN_NOT_FOUND, col.name());
                flds.put(prop.name(), !col.descending());
            }
            newIdx.setFields(flds);
            newIdx.setInlineSize(cmd0.inlineSize());
            fut = ctx.query().dynamicIndexCreate(tbl.cacheName(), cmd.schemaName(), typeDesc.tableName(), newIdx, cmd0.ifNotExists(), cmd0.parallel());
        } else if (cmd instanceof SqlDropIndexCommand) {
            SqlDropIndexCommand cmd0 = (SqlDropIndexCommand) cmd;
            GridH2Table tbl = schemaMgr.dataTableForIndex(cmd0.schemaName(), cmd0.indexName());
            if (tbl != null) {
                ensureDdlSupported(tbl);
                fut = ctx.query().dynamicIndexDrop(tbl.cacheName(), cmd0.schemaName(), cmd0.indexName(), cmd0.ifExists());
            } else {
                if (cmd0.ifExists())
                    fut = new GridFinishedFuture();
                else
                    throw new SchemaOperationException(SchemaOperationException.CODE_INDEX_NOT_FOUND, cmd0.indexName());
            }
        } else if (cmd instanceof SqlAlterTableCommand) {
            SqlAlterTableCommand cmd0 = (SqlAlterTableCommand) cmd;
            GridH2Table tbl = schemaMgr.dataTable(cmd0.schemaName(), cmd0.tableName());
            if (tbl == null) {
                throw new SchemaOperationException(SchemaOperationException.CODE_TABLE_NOT_FOUND, cmd0.tableName());
            }
            Boolean logging = cmd0.logging();
            assert logging != null : "Only LOGGING/NOLOGGING are supported at the moment.";
            IgniteCluster cluster = ctx.grid().cluster();
            if (logging) {
                boolean res = cluster.enableWal(tbl.cacheName());
                if (!res)
                    throw new IgniteSQLException("Logging already enabled for table: " + cmd0.tableName());
            } else {
                boolean res = cluster.disableWal(tbl.cacheName());
                if (!res)
                    throw new IgniteSQLException("Logging already disabled for table: " + cmd0.tableName());
            }
            fut = new GridFinishedFuture();
        } else if (cmd instanceof SqlCreateUserCommand) {
            SqlCreateUserCommand addCmd = (SqlCreateUserCommand) cmd;
            ctx.security().createUser(addCmd.userName(), addCmd.password().toCharArray());
        } else if (cmd instanceof SqlAlterUserCommand) {
            SqlAlterUserCommand altCmd = (SqlAlterUserCommand) cmd;
            ctx.security().alterUser(altCmd.userName(), altCmd.password().toCharArray());
        } else if (cmd instanceof SqlDropUserCommand) {
            SqlDropUserCommand dropCmd = (SqlDropUserCommand) cmd;
            ctx.security().dropUser(dropCmd.userName());
        } else if (cmd instanceof SqlAnalyzeCommand)
            processAnalyzeCommand((SqlAnalyzeCommand) cmd);
        else if (cmd instanceof SqlRefreshStatitsicsCommand)
            processRefreshStatisticsCommand((SqlRefreshStatitsicsCommand) cmd);
        else if (cmd instanceof SqlDropStatisticsCommand)
            processDropStatisticsCommand((SqlDropStatisticsCommand) cmd);
        else
            throw new IgniteSQLException("Unsupported DDL operation: " + sql, IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
        if (fut != null)
            fut.get();
    } catch (SchemaOperationException e) {
        throw convert(e);
    } catch (IgniteSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new IgniteSQLException(e.getMessage(), e);
    }
}
Also used : SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) LinkedHashMap(java.util.LinkedHashMap) GridQueryTypeDescriptor(org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor) GridFinishedFuture(org.apache.ignite.internal.util.future.GridFinishedFuture) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) QueryIndex(org.apache.ignite.cache.QueryIndex) SqlAnalyzeCommand(org.apache.ignite.internal.sql.command.SqlAnalyzeCommand) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlDropStatisticsCommand(org.apache.ignite.internal.sql.command.SqlDropStatisticsCommand) SqlIndexColumn(org.apache.ignite.internal.sql.command.SqlIndexColumn) SqlRefreshStatitsicsCommand(org.apache.ignite.internal.sql.command.SqlRefreshStatitsicsCommand) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SchemaOperationException(org.apache.ignite.internal.processors.query.schema.SchemaOperationException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridQueryProperty(org.apache.ignite.internal.processors.query.GridQueryProperty) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) IgniteCluster(org.apache.ignite.IgniteCluster) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 39 with AND

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

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

the class GridSubqueryJoinOptimizer method pullOutSubQryFromSelectExpr.

/**
 * Pulls out subquery from select expression.
 *
 * @param select Parent query where to find and pull out subqueries.
 */
private static void pullOutSubQryFromSelectExpr(GridSqlSelect select) {
    for (int i = 0; i < select.allColumns(); i++) {
        boolean wasPulledOut = false;
        GridSqlAst col = select.columns(false).get(i);
        if (col instanceof GridSqlSubquery)
            wasPulledOut = pullOutSubQryFromSelectExpr(select, null, i);
        else if (col instanceof GridSqlOperation && ((GridSqlOperation) col).operationType() == EXISTS)
            // We actially can't rewrite select query under exists operator. So we skip it.
            continue;
        else {
            ASTNodeFinder finder = new ASTNodeFinder(col, ELEMENT_WITH_SUBQUERY);
            ASTNodeFinder.Result res;
            while ((res = finder.findNext()) != null) wasPulledOut |= pullOutSubQryFromSelectExpr(select, res.getEl(), res.getIdx());
        }
        if (// we have to analyze just pulled out element as well
        wasPulledOut)
            i--;
    }
}
Also used : GridSqlSubquery(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery) GridSqlAst(org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst) GridSqlOperation(org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation)

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