Search in sources :

Example 1 with SqlCacheException

use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.

the class JdbcPreparedStatement method parameterMetaData.

/**
 * Fetches parameters metadata of the specified query.
 */
private ParameterMetaData parameterMetaData() throws SQLException {
    SqlFieldsQueryEx qry = new SqlFieldsQueryEx(sql, null);
    setupQuery(qry);
    try {
        List<JdbcParameterMeta> params = conn.ignite().context().query().getIndexing().parameterMetaData(conn.schemaName(), qry);
        return new JdbcThinParameterMetadata(params);
    } catch (IgniteSQLException ex) {
        throw ex.toJdbcException();
    } catch (SqlCacheException e) {
        throw e.toJdbcException();
    }
}
Also used : SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) JdbcParameterMeta(org.apache.ignite.internal.processors.odbc.jdbc.JdbcParameterMeta) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) JdbcThinParameterMetadata(org.apache.ignite.internal.jdbc.thin.JdbcThinParameterMetadata)

Example 2 with SqlCacheException

use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.

the class JdbcUtils method convertToSqlException.

/**
 * Convert exception to {@link SQLException}.
 *
 * @param e Converted Exception.
 * @param msgForUnknown Message for non-convertable exception.
 * @param sqlStateForUnknown SQLSTATE for non-convertable exception.
 * @return JDBC {@link SQLException}.
 * @see IgniteQueryErrorCode
 */
public static SQLException convertToSqlException(Exception e, String msgForUnknown, String sqlStateForUnknown) {
    SQLException sqlEx = null;
    Throwable t = e;
    while (sqlEx == null && t != null) {
        if (t instanceof SQLException) {
            if (t.getCause() instanceof IgniteSQLException)
                return ((IgniteSQLException) t.getCause()).toJdbcException();
            else if (t.getCause() instanceof SqlCacheException)
                return ((SqlCacheException) t.getCause()).toJdbcException();
            else
                return (SQLException) t;
        } else if (t instanceof IgniteSQLException)
            return ((IgniteSQLException) t).toJdbcException();
        else if (t instanceof SecurityException)
            return new SQLException(t.getMessage(), sqlStateForUnknown, t);
        else if (t instanceof SqlCacheException)
            return ((SqlCacheException) t).toJdbcException();
        t = t.getCause();
    }
    return new SQLException(msgForUnknown, sqlStateForUnknown, e);
}
Also used : SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SecurityException(org.apache.ignite.plugin.security.SecurityException)

Example 3 with SqlCacheException

use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.

the class DmlUtils method doInsertBatched.

/**
 * Execute INSERT statement plan.
 *
 * @param plan Plan to execute.
 * @param cursor Cursor to take inserted data from. I.e. list of batch arguments for each query.
 * @param pageSize Batch size for streaming, anything <= 0 for single page operations.
 * @return Number of items affected.
 * @throws IgniteCheckedException if failed, particularly in case of duplicate keys.
 */
private static List<UpdateResult> doInsertBatched(UpdatePlan plan, List<List<List<?>>> cursor, int pageSize) throws IgniteCheckedException {
    GridCacheContext cctx = plan.cacheContext();
    DmlBatchSender snd = new DmlBatchSender(cctx, pageSize, cursor.size());
    int rowNum = 0;
    SQLException resEx = null;
    for (List<List<?>> qryRow : cursor) {
        for (List<?> row : qryRow) {
            try {
                final IgniteBiTuple keyValPair = plan.processRow(row);
                snd.add(keyValPair.getKey(), new DmlStatementsProcessor.InsertEntryProcessor(keyValPair.getValue()), rowNum);
            } catch (Exception e) {
                String sqlState;
                int code;
                if (e instanceof IgniteSQLException) {
                    sqlState = ((IgniteSQLException) e).sqlState();
                    code = ((IgniteSQLException) e).statusCode();
                } else if (e instanceof SqlCacheException) {
                    sqlState = ((SqlCacheException) e).sqlState();
                    code = ((SqlCacheException) e).statusCode();
                } else {
                    sqlState = SqlStateCode.INTERNAL_ERROR;
                    code = IgniteQueryErrorCode.UNKNOWN;
                }
                resEx = chainException(resEx, new SQLException(e.getMessage(), sqlState, code, e));
                snd.setFailed(rowNum);
            }
        }
        rowNum++;
    }
    try {
        snd.flush();
    } catch (Exception e) {
        resEx = chainException(resEx, new SQLException(e.getMessage(), SqlStateCode.INTERNAL_ERROR, IgniteQueryErrorCode.UNKNOWN, e));
    }
    resEx = chainException(resEx, snd.error());
    if (!F.isEmpty(snd.failedKeys())) {
        SQLException e = new SQLException("Failed to INSERT some keys because they are already in cache [keys=" + snd.failedKeys() + ']', SqlStateCode.CONSTRAINT_VIOLATION, DUPLICATE_KEY);
        resEx = chainException(resEx, e);
    }
    if (resEx != null) {
        BatchUpdateException e = new BatchUpdateException(resEx.getMessage(), resEx.getSQLState(), resEx.getErrorCode(), snd.perRowCounterAsArray(), resEx);
        throw new IgniteCheckedException(e);
    }
    int[] cntPerRow = snd.perRowCounterAsArray();
    List<UpdateResult> res = new ArrayList<>(cntPerRow.length);
    for (int i = 0; i < cntPerRow.length; i++) {
        int cnt = cntPerRow[i];
        res.add(new UpdateResult(cnt, X.EMPTY_OBJECT_ARRAY));
    }
    return res;
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) BatchUpdateException(java.sql.BatchUpdateException) IgniteQueryErrorCode.createJdbcSqlException(org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.createJdbcSqlException) SQLException(java.sql.SQLException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) TransactionDuplicateKeyException(org.apache.ignite.transactions.TransactionDuplicateKeyException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) ArrayList(java.util.ArrayList) List(java.util.List) DmlStatementsProcessor(org.apache.ignite.internal.processors.query.h2.DmlStatementsProcessor) UpdateResult(org.apache.ignite.internal.processors.query.h2.UpdateResult) BatchUpdateException(java.sql.BatchUpdateException)

Example 4 with SqlCacheException

use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.

the class GridReduceQueryExecutor method onNextPage.

/**
 * @param node Node.
 * @param msg Message.
 */
public void onNextPage(final ClusterNode node, final GridQueryNextPageResponse msg) {
    try (TraceSurroundings ignored = MTC.support(ctx.tracing().create(SQL_PAGE_RESP, MTC.span()))) {
        final long qryReqId = msg.queryRequestId();
        final int qry = msg.query();
        final int seg = msg.segmentId();
        final ReduceQueryRun r = runs.get(qryReqId);
        if (// Already finished with error or canceled.
        r == null)
            return;
        final int pageSize = r.pageSize();
        Reducer idx = r.reducers().get(msg.query());
        ReduceResultPage page;
        try {
            page = new ReduceResultPage(ctx, node.id(), msg) {

                @Override
                public void fetchNextPage() {
                    if (r.hasErrorOrRetry()) {
                        if (r.exception() != null)
                            throw r.exception();
                        assert r.retryCause() != null;
                        throw new CacheException(r.retryCause());
                    }
                    try {
                        GridQueryNextPageRequest msg0 = new GridQueryNextPageRequest(qryReqId, qry, seg, pageSize, (byte) GridH2QueryRequest.setDataPageScanEnabled(0, r.isDataPageScanEnabled()));
                        if (node.isLocal())
                            h2.mapQueryExecutor().onNextPageRequest(node, msg0);
                        else
                            ctx.io().sendToGridTopic(node, GridTopic.TOPIC_QUERY, msg0, GridIoPolicy.QUERY_POOL);
                    } catch (IgniteCheckedException e) {
                        throw new CacheException("Failed to fetch data from node: " + node.id(), e);
                    }
                }
            };
        } catch (Exception e) {
            U.error(log, "Error in message.", e);
            MTC.span().addTag(ERROR, e::getMessage);
            int errCode = 0;
            IgniteSQLException sqlCause = X.cause(e, IgniteSQLException.class);
            if (sqlCause != null)
                errCode = sqlCause.statusCode();
            else {
                SqlCacheException sqlCacheException = X.cause(e, SqlCacheException.class);
                if (sqlCacheException != null)
                    errCode = sqlCacheException.statusCode();
            }
            fail(r, node.id(), "Error in message.", GridQueryFailResponse.GENERAL_ERROR, errCode);
            return;
        }
        idx.addPage(page);
        if (msg.retry() != null)
            r.setStateOnRetry(node.id(), msg.retry(), msg.retryCause());
        else if (// Count down only on each first page received.
        msg.page() == 0)
            r.onFirstPage();
    }
}
Also used : CacheException(javax.cache.CacheException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) GridQueryNextPageRequest(org.apache.ignite.internal.processors.query.h2.twostep.messages.GridQueryNextPageRequest) TraceSurroundings(org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings) QueryCancelledException(org.apache.ignite.cache.query.QueryCancelledException) SqlMemoryQuotaExceededException(org.apache.ignite.cache.query.exceptions.SqlMemoryQuotaExceededException) IgniteSQLMapStepException(org.apache.ignite.internal.processors.query.IgniteSQLMapStepException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) TransactionAlreadyCompletedException(org.apache.ignite.transactions.TransactionAlreadyCompletedException) IgniteTxAlreadyCompletedCheckedException(org.apache.ignite.internal.transactions.IgniteTxAlreadyCompletedCheckedException) QueryRetryException(org.apache.ignite.cache.query.QueryRetryException) SQLException(java.sql.SQLException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) TransactionException(org.apache.ignite.transactions.TransactionException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 5 with SqlCacheException

use of org.apache.ignite.cache.query.exceptions.SqlCacheException in project gridgain by gridgain.

the class QueryParser method prepareDmlStatement.

/**
 * Prepare DML statement.
 *
 * @param planKey Plan key.
 * @param prepared Prepared.
 * @return Statement.
 */
private QueryParserResultDml prepareDmlStatement(QueryDescriptor planKey, Prepared prepared) {
    if (F.eq(QueryUtils.sysSchemaName(), planKey.schemaName()))
        throw new IgniteSQLException("DML statements are not supported on " + planKey.schemaName() + " schema", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
    // Prepare AST.
    GridSqlQueryParser parser = new GridSqlQueryParser(false, log);
    GridSqlStatement stmt = parser.parse(prepared);
    List<GridH2Table> tbls = parser.tablesForDml();
    // available on local node.
    for (GridH2Table h2tbl : tbls) H2Utils.checkAndStartNotStartedCache(idx.kernalContext(), h2tbl);
    // Check MVCC mode.
    GridCacheContextInfo ctx = null;
    boolean mvccEnabled = false;
    for (GridH2Table h2tbl : tbls) {
        GridCacheContextInfo curCtx = h2tbl.cacheInfo();
        boolean curMvccEnabled = curCtx.config().getAtomicityMode() == CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT;
        if (ctx == null) {
            ctx = curCtx;
            mvccEnabled = curMvccEnabled;
        } else if (curMvccEnabled != mvccEnabled)
            MvccUtils.throwAtomicityModesMismatchException(ctx.config(), curCtx.config());
    }
    // Get streamer info.
    GridH2Table streamTbl = null;
    if (GridSqlQueryParser.isStreamableInsertStatement(prepared)) {
        GridSqlInsert insert = (GridSqlInsert) stmt;
        streamTbl = DmlAstUtils.gridTableForElement(insert.into()).dataTable();
    }
    // Create update plan.
    UpdatePlan plan;
    try {
        plan = UpdatePlanBuilder.planForStatement(planKey, stmt, mvccEnabled, idx, log, forceFillAbsentPKsWithDefaults);
    } catch (Exception e) {
        if (e instanceof IgniteSQLException)
            throw (IgniteSQLException) e;
        else if (e instanceof SqlCacheException)
            throw (SqlCacheException) e;
        else
            throw new IgniteSQLException("Failed to prepare update plan.", e);
    }
    return new QueryParserResultDml(stmt, mvccEnabled, streamTbl, plan);
}
Also used : SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) GridSqlQueryParser(org.apache.ignite.internal.processors.query.h2.sql.GridSqlQueryParser) GridSqlStatement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlStatement) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridCacheContextInfo(org.apache.ignite.internal.processors.cache.GridCacheContextInfo) GridSqlInsert(org.apache.ignite.internal.processors.query.h2.sql.GridSqlInsert) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan) SqlStrictParseException(org.apache.ignite.internal.sql.SqlStrictParseException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) SqlCacheException(org.apache.ignite.cache.query.exceptions.SqlCacheException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Aggregations

SqlCacheException (org.apache.ignite.cache.query.exceptions.SqlCacheException)11 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)10 SQLException (java.sql.SQLException)6 BatchUpdateException (java.sql.BatchUpdateException)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)2 IgniteQueryErrorCode.createJdbcSqlException (org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode.createJdbcSqlException)2 TraceSurroundings (org.apache.ignite.internal.processors.tracing.MTC.TraceSurroundings)2 CacheException (javax.cache.CacheException)1 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)1 IgniteException (org.apache.ignite.IgniteException)1 BulkLoadContextCursor (org.apache.ignite.cache.query.BulkLoadContextCursor)1 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)1 QueryRetryException (org.apache.ignite.cache.query.QueryRetryException)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 SqlMemoryQuotaExceededException (org.apache.ignite.cache.query.exceptions.SqlMemoryQuotaExceededException)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1