Search in sources :

Example 16 with SqlFieldsQueryEx

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

the class OpenCensusSqlNativeTracingTest method executeQuery.

/**
 * Executes query with specified parameters.
 *
 * @param sql SQL query to execute.
 * @param schema SQL query schema.
 * @param skipReducerOnUpdate Wether reduce phase should be skipped during update query execution.
 * @param distributedJoins Whether distributed joins enabled.
 * @param isQry {@code True} if query is SELECT, {@code False} - DML queries, {@code null} - all remainings.
 */
protected void executeQuery(String sql, String schema, boolean skipReducerOnUpdate, boolean distributedJoins, Boolean isQry) {
    SqlFieldsQuery qry = new SqlFieldsQueryEx(sql, isQry).setSkipReducerOnUpdate(skipReducerOnUpdate).setDistributedJoins(distributedJoins).setPageSize(PAGE_SIZE).setSchema(schema);
    reducer().context().query().querySqlFields(qry, false).getAll();
}
Also used : SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 17 with SqlFieldsQueryEx

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

the class RetryCauseMessageSelfTest method testUpdateQueryMappingFailureMessage.

/**
 * Test update query remap failure reason.
 */
@Test
public void testUpdateQueryMappingFailureMessage() {
    final GridReduceQueryExecutor rdcQryExec = GridTestUtils.getFieldValue(h2Idx, IgniteH2Indexing.class, "rdcQryExec");
    final ReducePartitionMapper mapper = GridTestUtils.getFieldValue(rdcQryExec, GridReduceQueryExecutor.class, "mapper");
    final IgniteLogger logger = GridTestUtils.getFieldValue(rdcQryExec, GridReduceQueryExecutor.class, "log");
    final GridKernalContext ctx = GridTestUtils.getFieldValue(rdcQryExec, GridReduceQueryExecutor.class, "ctx");
    GridTestUtils.setFieldValue(rdcQryExec, "mapper", new ReducePartitionMapper(ctx, logger) {

        @Override
        public ReducePartitionMapResult nodesForPartitions(List<Integer> cacheIds, AffinityTopologyVersion topVer, int[] parts, boolean isReplicatedOnly) {
            final ReducePartitionMapResult res = super.nodesForPartitions(cacheIds, topVer, parts, isReplicatedOnly);
            return new ReducePartitionMapResult(Collections.emptyList(), res.partitionsMap(), res.queryPartitionsMap());
        }
    });
    try {
        final SqlFieldsQueryEx qry = new SqlFieldsQueryEx(UPDATE_SQL, false).setArgs("New Name");
        GridTestUtils.assertThrows(log, () -> {
            return personCache.query(qry).getAll();
        }, CacheException.class, "Failed to map SQL query to topology during timeout");
        qry.setArgs("Another Name");
        qry.setSkipReducerOnUpdate(true);
        GridTestUtils.assertThrows(log, () -> {
            return personCache.query(qry).getAll();
        }, CacheException.class, "Failed to determine nodes participating in the update. ");
    } finally {
        GridTestUtils.setFieldValue(rdcQryExec, "mapper", mapper);
    }
}
Also used : AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) GridKernalContext(org.apache.ignite.internal.GridKernalContext) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) IgniteLogger(org.apache.ignite.IgniteLogger) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 18 with SqlFieldsQueryEx

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

the class OdbcRequestHandler method makeQuery.

/**
 * Make query considering handler configuration.
 * @param schema Schema.
 * @param sql SQL request.
 * @param args Arguments.
 * @return Query instance.
 */
private SqlFieldsQueryEx makeQuery(String schema, String sql, Object[] args, int timeout) {
    SqlFieldsQueryEx qry = new SqlFieldsQueryEx(sql, null);
    qry.setArgs(args);
    qry.setDistributedJoins(distributedJoins);
    qry.setEnforceJoinOrder(enforceJoinOrder);
    qry.setReplicatedOnly(replicatedOnly);
    qry.setCollocated(collocated);
    qry.setLazy(lazy);
    qry.setSchema(schema);
    qry.setSkipReducerOnUpdate(skipReducerOnUpdate);
    qry.setTimeout(timeout, TimeUnit.SECONDS);
    return qry;
}
Also used : SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)

Example 19 with SqlFieldsQueryEx

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

the class JdbcRequestHandler method executeQuery.

/**
 * {@link JdbcQueryExecuteRequest} command handler.
 *
 * @param req Execute query request.
 * @return Response.
 */
@SuppressWarnings("unchecked")
private JdbcResponse executeQuery(JdbcQueryExecuteRequest req) {
    GridQueryCancel cancel = null;
    boolean unregisterReq = false;
    if (isCancellationSupported()) {
        synchronized (reqMux) {
            JdbcQueryDescriptor desc = reqRegister.get(req.requestId());
            // Query was already cancelled and unregistered.
            if (desc == null)
                return null;
            cancel = desc.cancelHook();
            desc.incrementUsageCount();
        }
    }
    try {
        int cursorCnt = jdbcCursors.size();
        if (maxCursors > 0 && cursorCnt >= maxCursors)
            return new JdbcResponse(IgniteQueryErrorCode.UNKNOWN, "Too many open cursors (either close other " + "open cursors or increase the limit through " + "ClientConnectorConfiguration.maxOpenCursorsPerConnection) [maximum=" + maxCursors + ", current=" + cursorCnt + ']');
        assert !cliCtx.isStream();
        String sql = req.sqlQuery();
        SqlFieldsQueryEx qry;
        switch(req.expectedStatementType()) {
            case ANY_STATEMENT_TYPE:
                qry = new SqlFieldsQueryEx(sql, null);
                break;
            case SELECT_STATEMENT_TYPE:
                qry = new SqlFieldsQueryEx(sql, true);
                break;
            default:
                assert req.expectedStatementType() == JdbcStatementType.UPDATE_STMT_TYPE;
                qry = new SqlFieldsQueryEx(sql, false);
                if (cliCtx.isSkipReducerOnUpdate())
                    ((SqlFieldsQueryEx) qry).setSkipReducerOnUpdate(true);
        }
        setupQuery(qry, prepareSchemaName(req.schemaName()));
        qry.setArgs(req.arguments());
        qry.setAutoCommit(req.autoCommit());
        if (req.explicitTimeout()) {
            // Timeout is handled on a client side, do not handle it on a server side.
            qry.setTimeout(0, TimeUnit.MILLISECONDS);
        }
        if (req.pageSize() <= 0)
            return new JdbcResponse(IgniteQueryErrorCode.UNKNOWN, "Invalid fetch size: " + req.pageSize());
        qry.setPageSize(req.pageSize());
        String schemaName = req.schemaName();
        if (F.isEmpty(schemaName))
            schemaName = QueryUtils.DFLT_SCHEMA;
        qry.setSchema(schemaName);
        List<FieldsQueryCursor<List<?>>> results = connCtx.kernalContext().query().querySqlFields(null, qry, cliCtx, true, protocolVer.compareTo(VER_2_3_0) < 0, cancel);
        FieldsQueryCursor<List<?>> fieldsCur = results.get(0);
        if (fieldsCur instanceof BulkLoadContextCursor) {
            BulkLoadContextCursor blCur = (BulkLoadContextCursor) fieldsCur;
            BulkLoadProcessor blProcessor = blCur.bulkLoadProcessor();
            BulkLoadAckClientParameters clientParams = blCur.clientParams();
            JdbcBulkLoadProcessor processor = new JdbcBulkLoadProcessor(blProcessor, req.requestId());
            jdbcCursors.put(processor.cursorId(), processor);
            // responses for the same query on the client side
            return resultToResonse(new JdbcBulkLoadAckResult(processor.cursorId(), clientParams));
        }
        if (results.size() == 1) {
            JdbcQueryCursor cur = new JdbcQueryCursor(req.pageSize(), req.maxRows(), (QueryCursorImpl) fieldsCur, req.requestId());
            jdbcCursors.put(cur.cursorId(), cur);
            cur.openIterator();
            JdbcQueryExecuteResult res;
            PartitionResult partRes = ((QueryCursorImpl<List<?>>) fieldsCur).partitionResult();
            if (cur.isQuery())
                res = new JdbcQueryExecuteResult(cur.cursorId(), cur.fetchRows(), !cur.hasNext(), isClientPartitionAwarenessApplicable(req.partitionResponseRequest(), partRes) ? partRes : null);
            else {
                List<List<Object>> items = cur.fetchRows();
                assert items != null && items.size() == 1 && items.get(0).size() == 1 && items.get(0).get(0) instanceof Long : "Invalid result set for not-SELECT query. [qry=" + sql + ", res=" + S.toString(List.class, items) + ']';
                res = new JdbcQueryExecuteResult(cur.cursorId(), (Long) items.get(0).get(0), isClientPartitionAwarenessApplicable(req.partitionResponseRequest(), partRes) ? partRes : null);
            }
            if (res.last() && (!res.isQuery() || autoCloseCursors)) {
                jdbcCursors.remove(cur.cursorId());
                unregisterReq = true;
                cur.close();
            }
            return resultToResonse(res);
        } else {
            List<JdbcResultInfo> jdbcResults = new ArrayList<>(results.size());
            List<List<Object>> items = null;
            boolean last = true;
            for (FieldsQueryCursor<List<?>> c : results) {
                QueryCursorImpl qryCur = (QueryCursorImpl) c;
                JdbcResultInfo jdbcRes;
                if (qryCur.isQuery()) {
                    JdbcQueryCursor cur = new JdbcQueryCursor(req.pageSize(), req.maxRows(), qryCur, req.requestId());
                    jdbcCursors.put(cur.cursorId(), cur);
                    jdbcRes = new JdbcResultInfo(true, -1, cur.cursorId());
                    cur.openIterator();
                    if (items == null) {
                        items = cur.fetchRows();
                        last = cur.hasNext();
                    }
                } else
                    jdbcRes = new JdbcResultInfo(false, (Long) ((List<?>) qryCur.getAll().get(0)).get(0), -1);
                jdbcResults.add(jdbcRes);
            }
            return resultToResonse(new JdbcQueryExecuteMultipleStatementsResult(jdbcResults, items, last));
        }
    } catch (Exception e) {
        // Trying to close all cursors of current request.
        clearCursors(req.requestId());
        unregisterReq = true;
        U.error(log, "Failed to execute SQL query [reqId=" + req.requestId() + ", req=" + req + ']', e);
        if (X.cause(e, QueryCancelledException.class) != null)
            return exceptionToResult(new QueryCancelledException());
        else
            return exceptionToResult(e);
    } finally {
        cleanupQueryCancellationMeta(unregisterReq, req.requestId());
    }
}
Also used : FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) ArrayList(java.util.ArrayList) BulkLoadProcessor(org.apache.ignite.internal.processors.bulkload.BulkLoadProcessor) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) List(java.util.List) ArrayList(java.util.ArrayList) PartitionResult(org.apache.ignite.internal.sql.optimizer.affinity.PartitionResult) BulkLoadContextCursor(org.apache.ignite.cache.query.BulkLoadContextCursor) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BatchUpdateException(java.sql.BatchUpdateException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BulkLoadAckClientParameters(org.apache.ignite.internal.processors.bulkload.BulkLoadAckClientParameters) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Example 20 with SqlFieldsQueryEx

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

the class JdbcRequestHandler method executeBatch.

/**
 * @param req Request.
 * @return Response.
 */
private JdbcResponse executeBatch(JdbcBatchExecuteRequest req) {
    GridQueryCancel cancel = null;
    // separate thread.
    if (isCancellationSupported() && req.type() == BATCH_EXEC) {
        synchronized (reqMux) {
            JdbcQueryDescriptor desc = reqRegister.get(req.requestId());
            // Query was already cancelled and unregisterd.
            if (desc == null)
                return null;
            cancel = desc.cancelHook();
            desc.incrementUsageCount();
        }
    }
    try {
        String schemaName = prepareSchemaName(req.schemaName());
        int qryCnt = req.queries().size();
        List<Integer> updCntsAcc = new ArrayList<>(qryCnt);
        // Send back only the first error. Others will be written to the log.
        IgniteBiTuple<Integer, String> firstErr = new IgniteBiTuple<>();
        SqlFieldsQueryEx qry = null;
        for (JdbcQuery q : req.queries()) {
            if (q.sql() != null) {
                // If we have a new query string in the batch,
                if (// then execute the previous sub-batch and create a new SqlFieldsQueryEx.
                qry != null)
                    executeBatchedQuery(qry, updCntsAcc, firstErr, cancel);
                qry = new SqlFieldsQueryEx(q.sql(), false);
                setupQuery(qry, schemaName);
                qry.setAutoCommit(req.autoCommit());
            }
            assert qry != null;
            qry.addBatchedArgs(q.args());
        }
        if (qry != null)
            executeBatchedQuery(qry, updCntsAcc, firstErr, cancel);
        if (req.isLastStreamBatch())
            cliCtx.disableStreaming();
        int[] updCnts = U.toIntArray(updCntsAcc);
        return firstErr.isEmpty() ? resultToResonse(new JdbcBatchExecuteResult(updCnts, ClientListenerResponse.STATUS_SUCCESS, null)) : resultToResonse(new JdbcBatchExecuteResult(updCnts, firstErr.getKey(), firstErr.getValue()));
    } catch (QueryCancelledException e) {
        return exceptionToResult(e);
    } finally {
        cleanupQueryCancellationMeta(true, req.requestId());
    }
}
Also used : IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) GridQueryCancel(org.apache.ignite.internal.processors.query.GridQueryCancel) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException)

Aggregations

SqlFieldsQueryEx (org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)42 Test (org.junit.Test)23 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)17 List (java.util.List)11 ArrayList (java.util.ArrayList)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)8 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 BatchUpdateException (java.sql.BatchUpdateException)6 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)6 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)5 SQLException (java.sql.SQLException)4 IgniteException (org.apache.ignite.IgniteException)4 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 BulkLoadContextCursor (org.apache.ignite.cache.query.BulkLoadContextCursor)3 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)3 IgniteLogger (org.apache.ignite.IgniteLogger)2 QueryCursor (org.apache.ignite.cache.query.QueryCursor)2 GridKernalContext (org.apache.ignite.internal.GridKernalContext)2