Search in sources :

Example 11 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite by apache.

the class CalciteStatement method prepare.

protected <T> CalcitePrepare.CalciteSignature<T> prepare(Queryable<T> queryable) {
    final CalciteConnectionImpl calciteConnection = getConnection();
    final CalcitePrepare prepare = calciteConnection.prepareFactory.apply();
    final CalciteServerStatement serverStatement;
    try {
        serverStatement = calciteConnection.server.getStatement(handle);
    } catch (NoSuchStatementException e) {
        throw new AssertionError("invalid statement", e);
    }
    final CalcitePrepare.Context prepareContext = serverStatement.createPrepareContext();
    return prepare.prepareQueryable(prepareContext, queryable);
}
Also used : CalciteServerStatement(org.apache.calcite.server.CalciteServerStatement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 12 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project druid by druid-io.

the class DruidMeta method prepareAndExecute.

@Override
public ExecuteResult prepareAndExecute(final StatementHandle statement, final String sql, final long maxRowCount, final int maxRowsInFirstFrame, final PrepareCallback callback) throws NoSuchStatementException {
    try {
        // Ignore "callback", this class is designed for use with LocalService which doesn't use it.
        final DruidStatement druidStatement = getDruidStatement(statement);
        final DruidConnection druidConnection = getDruidConnection(statement.connectionId);
        AuthenticationResult authenticationResult = authenticateConnection(druidConnection);
        if (authenticationResult == null) {
            throw logFailure(new ForbiddenException("Authentication failed."), "Authentication failed for statement[%s]", druidStatement.getStatementId());
        }
        druidStatement.prepare(sql, maxRowCount, authenticationResult);
        final Frame firstFrame = druidStatement.execute(Collections.emptyList()).nextFrame(DruidStatement.START_OFFSET, getEffectiveMaxRowsPerFrame(maxRowsInFirstFrame));
        final Signature signature = druidStatement.getSignature();
        LOG.debug("Successfully prepared statement[%s] and started execution", druidStatement.getStatementId());
        return new ExecuteResult(ImmutableList.of(MetaResultSet.create(statement.connectionId, statement.id, false, signature, firstFrame)));
    }// cannot affect these exceptions as avatica handles them
     catch (NoSuchConnectionException | NoSuchStatementException e) {
        throw e;
    } catch (Throwable t) {
        throw errorHandler.sanitize(t);
    }
}
Also used : ForbiddenException(org.apache.druid.server.security.ForbiddenException) NoSuchConnectionException(org.apache.calcite.avatica.NoSuchConnectionException) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult)

Example 13 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project druid by druid-io.

the class DruidMeta method syncResults.

@Override
public boolean syncResults(final StatementHandle sh, final QueryState state, final long offset) throws NoSuchStatementException {
    try {
        final DruidStatement druidStatement = getDruidStatement(sh);
        final boolean isDone = druidStatement.isDone();
        final long currentOffset = druidStatement.getCurrentOffset();
        if (currentOffset != offset) {
            throw logFailure(new ISE("Requested offset[%,d] does not match currentOffset[%,d]", offset, currentOffset));
        }
        return !isDone;
    } catch (NoSuchStatementException | NoSuchConnectionException e) {
        throw e;
    } catch (Throwable t) {
        throw errorHandler.sanitize(t);
    }
}
Also used : NoSuchConnectionException(org.apache.calcite.avatica.NoSuchConnectionException) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) ISE(org.apache.druid.java.util.common.ISE)

Example 14 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite-avatica by apache.

the class JdbcMeta method prepareAndExecute.

public ExecuteResult prepareAndExecute(StatementHandle h, String sql, long maxRowCount, int maxRowsInFirstFrame, PrepareCallback callback) throws NoSuchStatementException {
    try {
        final StatementInfo info = getStatementCache().getIfPresent(h.id);
        if (info == null) {
            throw new NoSuchStatementException(h);
        }
        final Statement statement = info.statement;
        // Make sure that we limit the number of rows for the query
        setMaxRows(statement, maxRowCount);
        boolean ret = statement.execute(sql);
        info.setResultSet(statement.getResultSet());
        // Either execute(sql) returned true or the resultSet was null
        assert ret || null == info.getResultSet();
        final List<MetaResultSet> resultSets = new ArrayList<>();
        if (null == info.getResultSet()) {
            // Create a special result set that just carries update count
            resultSets.add(JdbcResultSet.count(h.connectionId, h.id, AvaticaUtils.getLargeUpdateCount(statement)));
        } else {
            resultSets.add(JdbcResultSet.create(h.connectionId, h.id, info.getResultSet(), maxRowsInFirstFrame));
        }
        LOG.trace("prepAndExec statement {}", h);
        // TODO: review client to ensure statementId is updated when appropriate
        return new ExecuteResult(resultSets);
    } catch (SQLException e) {
        throw propagate(e);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) AvaticaPreparedStatement(org.apache.calcite.avatica.AvaticaPreparedStatement) Statement(java.sql.Statement) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) ArrayList(java.util.ArrayList)

Example 15 with NoSuchStatementException

use of org.apache.calcite.avatica.NoSuchStatementException in project calcite-avatica by apache.

the class JdbcMeta method executeBatch.

@Override
public ExecuteBatchResult executeBatch(StatementHandle h, List<List<TypedValue>> updateBatches) throws NoSuchStatementException {
    try {
        final StatementInfo info = statementCache.getIfPresent(h.id);
        if (null == info) {
            throw new NoSuchStatementException(h);
        }
        final PreparedStatement preparedStmt = (PreparedStatement) info.statement;
        int rowUpdate = 1;
        for (List<TypedValue> batch : updateBatches) {
            int i = 1;
            for (TypedValue value : batch) {
                // Set the TypedValue in the PreparedStatement
                try {
                    preparedStmt.setObject(i, value.toJdbc(calendar));
                    i++;
                } catch (SQLException e) {
                    throw new RuntimeException("Failed to set value on row #" + rowUpdate + " and column #" + i, e);
                }
                // Track the update number for better error messages
                rowUpdate++;
            }
            preparedStmt.addBatch();
        }
        return new ExecuteBatchResult(AvaticaUtils.executeLargeBatch(preparedStmt));
    } catch (SQLException e) {
        throw propagate(e);
    }
}
Also used : SQLException(java.sql.SQLException) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException) PreparedStatement(java.sql.PreparedStatement) AvaticaPreparedStatement(org.apache.calcite.avatica.AvaticaPreparedStatement) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Aggregations

NoSuchStatementException (org.apache.calcite.avatica.NoSuchStatementException)16 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)6 AvaticaPreparedStatement (org.apache.calcite.avatica.AvaticaPreparedStatement)6 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 NoSuchConnectionException (org.apache.calcite.avatica.NoSuchConnectionException)3 TypedValue (org.apache.calcite.avatica.remote.TypedValue)3 CalciteServerStatement (org.apache.calcite.server.CalciteServerStatement)3 DataContext (org.apache.calcite.DataContext)2 Meta (org.apache.calcite.avatica.Meta)2 Context (org.apache.calcite.avatica.metrics.Timer.Context)2 ISE (org.apache.druid.java.util.common.ISE)2 AuthenticationResult (org.apache.druid.server.security.AuthenticationResult)2 ForbiddenException (org.apache.druid.server.security.ForbiddenException)2 Connection (java.sql.Connection)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Nonnull (javax.annotation.Nonnull)1 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)1 Common (org.apache.calcite.avatica.proto.Common)1