Search in sources :

Example 11 with GridQueryFieldsResult

use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult in project ignite by apache.

the class DmlStatementsProcessor method streamUpdateQuery.

/**
 * Perform given statement against given data streamer. Only rows based INSERT is supported.
 *
 * @param schemaName Schema name.
 * @param streamer Streamer to feed data to.
 * @param stmt Statement.
 * @param args Statement arguments.
 * @return Number of rows in given INSERT statement.
 * @throws IgniteCheckedException if failed.
 */
@SuppressWarnings({ "unchecked", "ConstantConditions" })
long streamUpdateQuery(String schemaName, IgniteDataStreamer streamer, PreparedStatement stmt, final Object[] args) throws IgniteCheckedException {
    idx.checkStatementStreamable(stmt);
    Prepared p = GridSqlQueryParser.prepared(stmt);
    assert p != null;
    final UpdatePlan plan = getPlanForStatement(schemaName, null, p, null, true, null);
    assert plan.isLocalSubquery();
    final GridCacheContext cctx = plan.cacheContext();
    QueryCursorImpl<List<?>> cur;
    final ArrayList<List<?>> data = new ArrayList<>(plan.rowCount());
    QueryCursorImpl<List<?>> stepCur = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            try {
                Iterator<List<?>> it;
                if (!F.isEmpty(plan.selectQuery())) {
                    GridQueryFieldsResult res = idx.queryLocalSqlFields(idx.schema(cctx.name()), plan.selectQuery(), F.asList(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)), null, false, 0, null);
                    it = res.iterator();
                } else
                    it = plan.createRows(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)).iterator();
                return new GridQueryCacheObjectsIterator(it, idx.objectContext(), cctx.keepBinary());
            } catch (IgniteCheckedException e) {
                throw new IgniteException(e);
            }
        }
    }, null);
    data.addAll(stepCur.getAll());
    cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

        @Override
        public Iterator<List<?>> iterator() {
            return data.iterator();
        }
    }, null);
    if (plan.rowCount() == 1) {
        IgniteBiTuple t = plan.processRow(cur.iterator().next());
        streamer.addData(t.getKey(), t.getValue());
        return 1;
    }
    Map<Object, Object> rows = new LinkedHashMap<>(plan.rowCount());
    for (List<?> row : cur) {
        final IgniteBiTuple t = plan.processRow(row);
        rows.put(t.getKey(), t.getValue());
    }
    streamer.addData(rows);
    return rows.size();
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Prepared(org.h2.command.Prepared) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) IgniteSingletonIterator(org.apache.ignite.internal.util.lang.IgniteSingletonIterator) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 12 with GridQueryFieldsResult

use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult in project ignite by apache.

the class IgniteH2Indexing method executeSelect0.

/**
 * Execute an all-ready {@link SqlFieldsQuery}.
 *
 * @param qryId Query id.
 * @param qryDesc Plan key.
 * @param qryParams Parameters.
 * @param select Select.
 * @param keepBinary Whether binary objects must not be deserialized automatically.
 * @param mvccTracker MVCC tracker.
 * @param cancel Query cancel state holder.
 * @param inTx Flag whether query is executed within transaction.
 * @param timeout Timeout.
 * @return Query result.
 * @throws IgniteCheckedException On error.
 */
private Iterable<List<?>> executeSelect0(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultSelect select, boolean keepBinary, MvccQueryTracker mvccTracker, GridQueryCancel cancel, boolean inTx, int timeout) throws IgniteCheckedException {
    assert !select.mvccEnabled() || mvccTracker != null;
    // Check security.
    if (ctx.security().enabled())
        checkSecurity(select.cacheIds());
    Iterable<List<?>> iter;
    if (select.splitNeeded()) {
        // Distributed query.
        GridCacheTwoStepQuery twoStepQry = select.forUpdate() && inTx ? select.forUpdateTwoStepQuery() : select.twoStepQuery();
        assert twoStepQry != null;
        iter = executeSelectDistributed(qryId, qryDesc, qryParams, twoStepQry, keepBinary, mvccTracker, cancel, timeout);
    } else {
        // Local query.
        IndexingQueryFilter filter = (qryDesc.local() ? backupFilter(null, qryParams.partitions()) : null);
        GridQueryFieldsResult res = executeSelectLocal(qryId, qryDesc, qryParams, select, filter, mvccTracker, cancel, inTx, timeout);
        iter = () -> {
            try {
                return new GridQueryCacheObjectsIterator(res.iterator(), objectContext(), keepBinary);
            } catch (IgniteCheckedException | IgniteSQLException e) {
                throw new CacheException(e);
            }
        };
    }
    return iter;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridCacheTwoStepQuery(org.apache.ignite.internal.processors.cache.query.GridCacheTwoStepQuery) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) GridQueryCacheObjectsIterator(org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult)

Aggregations

GridQueryFieldsResult (org.apache.ignite.internal.processors.query.GridQueryFieldsResult)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)10 IgniteException (org.apache.ignite.IgniteException)8 GridQueryCacheObjectsIterator (org.apache.ignite.internal.processors.query.GridQueryCacheObjectsIterator)8 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)6 UpdatePlan (org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)6 Iterator (java.util.Iterator)5 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)5 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 IgniteSingletonIterator (org.apache.ignite.internal.util.lang.IgniteSingletonIterator)4 Collections.singletonList (java.util.Collections.singletonList)3 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)3 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)3 IndexingQueryFilter (org.apache.ignite.spi.indexing.IndexingQueryFilter)3 LinkedHashMap (java.util.LinkedHashMap)2 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)2 BinaryObject (org.apache.ignite.binary.BinaryObject)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2