Search in sources :

Example 1 with DescribeResult

use of io.crate.action.sql.DescribeResult in project crate by crate.

the class PostgresWireProtocol method handleDescribeMessage.

/**
 * Describe Message
 * Header:
 * | 'D' | int32 len
 * <p>
 * Body:
 * | 'S' = prepared statement or 'P' = portal
 * | string nameOfPortalOrStatement
 */
private void handleDescribeMessage(ByteBuf buffer, Channel channel) {
    byte type = buffer.readByte();
    String portalOrStatement = readCString(buffer);
    DescribeResult describeResult = session.describe((char) type, portalOrStatement);
    Collection<Symbol> fields = describeResult.getFields();
    if (type == 'S') {
        Messages.sendParameterDescription(channel, describeResult.getParameters());
    }
    if (fields == null) {
        Messages.sendNoData(channel);
    } else {
        var resultFormatCodes = type == 'P' ? session.getResultFormatCodes(portalOrStatement) : null;
        Messages.sendRowDescription(channel, fields, resultFormatCodes, describeResult.relation());
    }
}
Also used : DescribeResult(io.crate.action.sql.DescribeResult) Symbol(io.crate.expression.symbol.Symbol)

Example 2 with DescribeResult

use of io.crate.action.sql.DescribeResult in project crate by crate.

the class PostgresWireProtocolTest method submitQueriesThroughSimpleQueryMode.

private void submitQueriesThroughSimpleQueryMode(String statements, @Nullable Throwable failure) {
    SQLOperations sqlOperations = Mockito.mock(SQLOperations.class);
    Session session = mock(Session.class);
    SessionContext sessionContext = new SessionContext(User.CRATE_USER);
    when(session.sessionContext()).thenReturn(sessionContext);
    when(sqlOperations.createSession(any(String.class), any(User.class))).thenReturn(session);
    DescribeResult describeResult = mock(DescribeResult.class);
    when(describeResult.getFields()).thenReturn(null);
    when(session.describe(anyChar(), anyString())).thenReturn(describeResult);
    when(session.transactionState()).thenReturn(TransactionState.IDLE);
    PostgresWireProtocol ctx = new PostgresWireProtocol(sqlOperations, sessionCtx -> AccessControl.DISABLED, new AlwaysOKAuthentication(userName -> User.CRATE_USER), null);
    channel = new EmbeddedChannel(ctx.decoder, ctx.handler);
    if (failure != null) {
        when(session.sync()).thenAnswer(invocationOnMock -> {
            Messages.sendErrorResponse(channel, AccessControl.DISABLED, failure);
            return CompletableFuture.failedFuture(failure);
        });
    } else {
        when(session.sync()).thenReturn(CompletableFuture.completedFuture(null));
    }
    sendStartupMessage(channel);
    readAuthenticationOK(channel);
    skipParameterMessages(channel);
    readReadyForQueryMessage(channel);
    ByteBuf query = Unpooled.buffer();
    try {
        // the actual statements don't have to be valid as they are not executed
        Messages.writeCString(query, statements.getBytes(StandardCharsets.UTF_8));
        ctx.handleSimpleQuery(query, new DelayableWriteChannel(channel));
    } finally {
        query.release();
    }
}
Also used : Arrays(java.util.Arrays) SessionContext(io.crate.action.sql.SessionContext) PGTypes(io.crate.protocols.postgres.types.PGTypes) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Provider(org.elasticsearch.common.inject.Provider) Unpooled(io.netty.buffer.Unpooled) DependencyCarrier(io.crate.planner.DependencyCarrier) Settings(org.elasticsearch.common.settings.Settings) After(org.junit.After) Map(java.util.Map) SecureString(org.elasticsearch.common.settings.SecureString) SQLOperations(io.crate.action.sql.SQLOperations) Matchers.isOneOf(org.hamcrest.Matchers.isOneOf) User(io.crate.user.User) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Session(io.crate.action.sql.Session) Version(org.elasticsearch.Version) DataTypes(io.crate.types.DataTypes) UserManager(io.crate.user.UserManager) Matchers.is(org.hamcrest.Matchers.is) DescribeResult(io.crate.action.sql.DescribeResult) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) AccessControl(io.crate.auth.AccessControl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Authentication(io.crate.auth.Authentication) SQLExecutor(io.crate.testing.SQLExecutor) StubUserManager(io.crate.user.StubUserManager) Nullable(javax.annotation.Nullable) Before(org.junit.Before) ArgumentMatchers.anyChar(org.mockito.ArgumentMatchers.anyChar) JobKilledException(io.crate.exceptions.JobKilledException) AuthenticationMethod(io.crate.auth.AuthenticationMethod) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) ChannelHandler(io.netty.channel.ChannelHandler) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AlwaysOKAuthentication(io.crate.auth.AlwaysOKAuthentication) DescribeResult(io.crate.action.sql.DescribeResult) User(io.crate.user.User) SessionContext(io.crate.action.sql.SessionContext) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SecureString(org.elasticsearch.common.settings.SecureString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf) SQLOperations(io.crate.action.sql.SQLOperations) Session(io.crate.action.sql.Session)

Example 3 with DescribeResult

use of io.crate.action.sql.DescribeResult in project crate by crate.

the class PostgresWireProtocol method handleSingleQuery.

private CompletableFuture<?> handleSingleQuery(Statement statement, DelayableWriteChannel channel) {
    CompletableFuture<?> result = new CompletableFuture<>();
    String query;
    try {
        query = SqlFormatter.formatSql(statement);
    } catch (Exception e) {
        query = statement.toString();
    }
    AccessControl accessControl = getAccessControl.apply(session.sessionContext());
    try {
        session.analyze("", statement, Collections.emptyList(), query);
        session.bind("", "", Collections.emptyList(), null);
        DescribeResult describeResult = session.describe('P', "");
        List<Symbol> fields = describeResult.getFields();
        CompletableFuture<?> execute;
        if (fields == null) {
            RowCountReceiver rowCountReceiver = new RowCountReceiver(query, channel.bypassDelay(), accessControl);
            execute = session.execute("", 0, rowCountReceiver);
        } else {
            Messages.sendRowDescription(channel, fields, null, describeResult.relation());
            ResultSetReceiver resultSetReceiver = new ResultSetReceiver(query, channel.bypassDelay(), TransactionState.IDLE, accessControl, Lists2.map(fields, x -> PGTypes.get(x.valueType())), null);
            execute = session.execute("", 0, resultSetReceiver);
        }
        if (execute != null) {
            channel.delayWritesUntil(execute);
        }
        return session.sync();
    } catch (Throwable t) {
        Messages.sendErrorResponse(channel, accessControl, t);
        result.completeExceptionally(t);
        return result;
    }
}
Also used : SessionContext(io.crate.action.sql.SessionContext) PGTypes(io.crate.protocols.postgres.types.PGTypes) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) STARTUP_HEADER(io.crate.protocols.postgres.PostgresWireProtocol.State.STARTUP_HEADER) Locale(java.util.Locale) SQLOperations(io.crate.action.sql.SQLOperations) User(io.crate.user.User) Collection(java.util.Collection) SqlFormatter(io.crate.sql.SqlFormatter) ChannelPipeline(io.netty.channel.ChannelPipeline) Lists2(io.crate.common.collections.Lists2) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) FormatCodes.getFormatCode(io.crate.protocols.postgres.FormatCodes.getFormatCode) Session(io.crate.action.sql.Session) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) PRE_STARTUP(io.crate.protocols.postgres.PostgresWireProtocol.State.PRE_STARTUP) Symbol(io.crate.expression.symbol.Symbol) Statement(io.crate.sql.tree.Statement) DescribeResult(io.crate.action.sql.DescribeResult) SslContextProvider(io.crate.protocols.ssl.SslContextProvider) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) AccessControl(io.crate.auth.AccessControl) PGType(io.crate.protocols.postgres.types.PGType) Netty4HttpServerTransport(org.elasticsearch.http.netty4.Netty4HttpServerTransport) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) SocketException(java.net.SocketException) SSLSession(javax.net.ssl.SSLSession) ByteBuf(io.netty.buffer.ByteBuf) BiConsumer(java.util.function.BiConsumer) SqlParser(io.crate.sql.parser.SqlParser) Authentication(io.crate.auth.Authentication) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) Nullable(javax.annotation.Nullable) Properties(java.util.Properties) AuthenticationMethod(io.crate.auth.AuthenticationMethod) DataType(io.crate.types.DataType) SSL.getSession(io.crate.protocols.SSL.getSession) Channel(io.netty.channel.Channel) Protocol(io.crate.auth.Protocol) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ResultReceiver(io.crate.action.sql.ResultReceiver) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeResult(io.crate.action.sql.DescribeResult) Symbol(io.crate.expression.symbol.Symbol) SocketException(java.net.SocketException) AccessControl(io.crate.auth.AccessControl)

Example 4 with DescribeResult

use of io.crate.action.sql.DescribeResult in project crate by crate.

the class SqlHttpHandler method executeBulkRequest.

private CompletableFuture<XContentBuilder> executeBulkRequest(Session session, String stmt, List<List<Object>> bulkArgs) {
    final long startTimeInNs = System.nanoTime();
    session.parse(UNNAMED, stmt, emptyList());
    final RestBulkRowCountReceiver.Result[] results = new RestBulkRowCountReceiver.Result[bulkArgs.size()];
    for (int i = 0; i < bulkArgs.size(); i++) {
        session.bind(UNNAMED, UNNAMED, bulkArgs.get(i), null);
        ResultReceiver resultReceiver = new RestBulkRowCountReceiver(results, i);
        session.execute(UNNAMED, 0, resultReceiver);
    }
    if (results.length > 0) {
        DescribeResult describeResult = session.describe('P', UNNAMED);
        if (describeResult.getFields() != null) {
            return CompletableFuture.failedFuture(new UnsupportedOperationException("Bulk operations for statements that return result sets is not supported"));
        }
    }
    return session.sync().thenApply(ignored -> {
        try {
            return ResultToXContentBuilder.builder(JsonXContent.contentBuilder()).cols(emptyList()).duration(startTimeInNs).bulkRows(results).build();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : DescribeResult(io.crate.action.sql.DescribeResult) IOException(java.io.IOException) DescribeResult(io.crate.action.sql.DescribeResult) ResultReceiver(io.crate.action.sql.ResultReceiver)

Example 5 with DescribeResult

use of io.crate.action.sql.DescribeResult in project crate by crate.

the class SqlHttpHandler method executeSimpleRequest.

private CompletableFuture<XContentBuilder> executeSimpleRequest(Session session, String stmt, List<Object> args, boolean includeTypes) throws IOException {
    long startTimeInNs = System.nanoTime();
    session.parse(UNNAMED, stmt, emptyList());
    session.bind(UNNAMED, UNNAMED, args == null ? emptyList() : args, null);
    DescribeResult description = session.describe('P', UNNAMED);
    List<Symbol> resultFields = description.getFields();
    ResultReceiver<XContentBuilder> resultReceiver;
    if (resultFields == null) {
        resultReceiver = new RestRowCountReceiver(JsonXContent.contentBuilder(), startTimeInNs, includeTypes);
    } else {
        CircuitBreaker breaker = circuitBreakerProvider.apply(HierarchyCircuitBreakerService.QUERY);
        RamAccounting ramAccounting = new BlockBasedRamAccounting(b -> breaker.addEstimateBytesAndMaybeBreak(b, "http-result"), MAX_BLOCK_SIZE_IN_BYTES);
        resultReceiver = new RestResultSetReceiver(JsonXContent.contentBuilder(), resultFields, startTimeInNs, new RowAccountingWithEstimators(Symbols.typeView(resultFields), ramAccounting), includeTypes);
        resultReceiver.completionFuture().whenComplete((result, error) -> ramAccounting.close());
    }
    session.execute(UNNAMED, 0, resultReceiver);
    return session.sync().thenCompose(ignored -> resultReceiver.completionFuture());
}
Also used : DescribeResult(io.crate.action.sql.DescribeResult) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) BlockBasedRamAccounting(io.crate.breaker.BlockBasedRamAccounting) RamAccounting(io.crate.breaker.RamAccounting) Symbol(io.crate.expression.symbol.Symbol) RowAccountingWithEstimators(io.crate.breaker.RowAccountingWithEstimators) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

DescribeResult (io.crate.action.sql.DescribeResult)5 Symbol (io.crate.expression.symbol.Symbol)3 ResultReceiver (io.crate.action.sql.ResultReceiver)2 SQLOperations (io.crate.action.sql.SQLOperations)2 Session (io.crate.action.sql.Session)2 SessionContext (io.crate.action.sql.SessionContext)2 AccessControl (io.crate.auth.AccessControl)2 Authentication (io.crate.auth.Authentication)2 AuthenticationMethod (io.crate.auth.AuthenticationMethod)2 PGTypes (io.crate.protocols.postgres.types.PGTypes)2 User (io.crate.user.User)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 AlwaysOKAuthentication (io.crate.auth.AlwaysOKAuthentication)1 Protocol (io.crate.auth.Protocol)1 BlockBasedRamAccounting (io.crate.breaker.BlockBasedRamAccounting)1 RamAccounting (io.crate.breaker.RamAccounting)1 RowAccountingWithEstimators (io.crate.breaker.RowAccountingWithEstimators)1