Search in sources :

Example 6 with Statement

use of io.crate.sql.tree.Statement in project crate by crate.

the class Session method singleExec.

@VisibleForTesting
CompletableFuture<?> singleExec(Portal portal, ResultReceiver<?> resultReceiver, int maxRows) {
    var activeConsumer = portal.activeConsumer();
    if (activeConsumer != null && activeConsumer.suspended()) {
        activeConsumer.replaceResultReceiver(resultReceiver, maxRows);
        activeConsumer.resume();
        return resultReceiver.completionFuture();
    }
    var jobId = UUIDs.dirtyUUID();
    var routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
    var clusterState = executor.clusterService().state();
    var txnCtx = new CoordinatorTxnCtx(sessionContext);
    var nodeCtx = executor.nodeContext();
    var params = new RowN(portal.params().toArray());
    var plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, maxRows, params);
    var analyzedStmt = portal.analyzedStatement();
    String rawStatement = portal.preparedStmt().rawStatement();
    if (analyzedStmt == null) {
        String errorMsg = "Statement must have been analyzed: " + rawStatement;
        jobsLogs.logPreExecutionFailure(jobId, rawStatement, errorMsg, sessionContext.sessionUser());
        throw new IllegalStateException(errorMsg);
    }
    Plan plan;
    try {
        plan = planner.plan(analyzedStmt, plannerContext);
    } catch (Throwable t) {
        jobsLogs.logPreExecutionFailure(jobId, rawStatement, SQLExceptions.messageOf(t), sessionContext.sessionUser());
        throw t;
    }
    if (!analyzedStmt.isWriteOperation()) {
        resultReceiver = new RetryOnFailureResultReceiver(executor.clusterService(), clusterState, indexName -> executor.clusterService().state().metadata().hasIndex(indexName), resultReceiver, jobId, (newJobId, resultRec) -> retryQuery(newJobId, analyzedStmt, routingProvider, new RowConsumerToResultReceiver(resultRec, maxRows, new JobsLogsUpdateListener(newJobId, jobsLogs)), params, txnCtx, nodeCtx));
    }
    jobsLogs.logExecutionStart(jobId, rawStatement, sessionContext.sessionUser(), StatementClassifier.classify(plan));
    RowConsumerToResultReceiver consumer = new RowConsumerToResultReceiver(resultReceiver, maxRows, new JobsLogsUpdateListener(jobId, jobsLogs));
    portal.setActiveConsumer(consumer);
    plan.execute(executor, plannerContext, consumer, params, SubQueryResults.EMPTY);
    return resultReceiver.completionFuture();
}
Also used : ParamTypeHints(io.crate.analyze.ParamTypeHints) RetryOnFailureResultReceiver(io.crate.protocols.postgres.RetryOnFailureResultReceiver) Analyzer(io.crate.analyze.Analyzer) DependencyCarrier(io.crate.planner.DependencyCarrier) ClusterState(org.elasticsearch.cluster.ClusterState) Relations(io.crate.analyze.Relations) RowN(io.crate.data.RowN) TransactionState(io.crate.protocols.postgres.TransactionState) Map(java.util.Map) JobsLogsUpdateListener(io.crate.protocols.postgres.JobsLogsUpdateListener) TableInfo(io.crate.metadata.table.TableInfo) NodeContext(io.crate.metadata.NodeContext) AnalyzedStatement(io.crate.analyze.AnalyzedStatement) UUIDs(org.elasticsearch.common.UUIDs) UUID(java.util.UUID) Lists2(io.crate.common.collections.Lists2) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AnalyzedDiscard(io.crate.analyze.AnalyzedDiscard) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) AnalyzedBegin(io.crate.analyze.AnalyzedBegin) SubQueryResults(io.crate.planner.operators.SubQueryResults) Statement(io.crate.sql.tree.Statement) VisibleForTesting(io.crate.common.annotations.VisibleForTesting) Row1(io.crate.data.Row1) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) AnalyzedCommit(io.crate.analyze.AnalyzedCommit) AccessControl(io.crate.auth.AccessControl) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Portal(io.crate.protocols.postgres.Portal) Symbols(io.crate.expression.symbol.Symbols) FormatCodes(io.crate.protocols.postgres.FormatCodes) SqlParser(io.crate.sql.parser.SqlParser) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) RelationInfo(io.crate.metadata.RelationInfo) Target(io.crate.sql.tree.DiscardStatement.Target) DataType(io.crate.types.DataType) Planner(io.crate.planner.Planner) RoutingProvider(io.crate.metadata.RoutingProvider) RowConsumer(io.crate.data.RowConsumer) StatementClassifier(io.crate.planner.operators.StatementClassifier) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) PlannerContext(io.crate.planner.PlannerContext) Plan(io.crate.planner.Plan) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) ReadOnlyException(io.crate.exceptions.ReadOnlyException) SQLExceptions(io.crate.exceptions.SQLExceptions) LogManager(org.apache.logging.log4j.LogManager) Randomness(org.elasticsearch.common.Randomness) AnalyzedDeallocate(io.crate.analyze.AnalyzedDeallocate) RoutingProvider(io.crate.metadata.RoutingProvider) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) Plan(io.crate.planner.Plan) RowN(io.crate.data.RowN) PlannerContext(io.crate.planner.PlannerContext) JobsLogsUpdateListener(io.crate.protocols.postgres.JobsLogsUpdateListener) RetryOnFailureResultReceiver(io.crate.protocols.postgres.RetryOnFailureResultReceiver) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 7 with Statement

use of io.crate.sql.tree.Statement 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 8 with Statement

use of io.crate.sql.tree.Statement in project crate by crate.

the class TestStatementBuilder method testShowCreateTable.

@Test
public void testShowCreateTable() {
    Statement stmt = SqlParser.createStatement("SHOW CREATE TABLE foo");
    assertTrue(stmt instanceof ShowCreateTable);
    assertEquals(((ShowCreateTable) stmt).table().getName().toString(), "foo");
    stmt = SqlParser.createStatement("SHOW CREATE TABLE my_schema.foo");
    assertEquals(((ShowCreateTable) stmt).table().getName().toString(), "my_schema.foo");
}
Also used : SetStatement(io.crate.sql.tree.SetStatement) CommitStatement(io.crate.sql.tree.CommitStatement) DeallocateStatement(io.crate.sql.tree.DeallocateStatement) BeginStatement(io.crate.sql.tree.BeginStatement) Statement(io.crate.sql.tree.Statement) KillStatement(io.crate.sql.tree.KillStatement) SetSessionAuthorizationStatement(io.crate.sql.tree.SetSessionAuthorizationStatement) ShowCreateTable(io.crate.sql.tree.ShowCreateTable) Test(org.junit.Test)

Example 9 with Statement

use of io.crate.sql.tree.Statement in project crate by crate.

the class TestStatementBuilder method testDeallocateAll.

@Test
public void testDeallocateAll() {
    Statement stmt = SqlParser.createStatement("DEALLOCATE ALL");
    assertTrue(stmt.equals(new DeallocateStatement()));
}
Also used : DeallocateStatement(io.crate.sql.tree.DeallocateStatement) SetStatement(io.crate.sql.tree.SetStatement) CommitStatement(io.crate.sql.tree.CommitStatement) DeallocateStatement(io.crate.sql.tree.DeallocateStatement) BeginStatement(io.crate.sql.tree.BeginStatement) Statement(io.crate.sql.tree.Statement) KillStatement(io.crate.sql.tree.KillStatement) SetSessionAuthorizationStatement(io.crate.sql.tree.SetSessionAuthorizationStatement) Test(org.junit.Test)

Example 10 with Statement

use of io.crate.sql.tree.Statement in project crate by crate.

the class TestStatementBuilder method printStatement.

private static void printStatement(String sql) {
    println(sql.trim());
    println("");
    Statement statement = SqlParser.createStatement(sql);
    println(statement.toString());
    println("");
    // TODO: support formatting all statement types
    if (statement instanceof Query || statement instanceof CreateTable || statement instanceof CopyFrom || statement instanceof SwapTable || statement instanceof GCDanglingArtifacts || statement instanceof CreateFunction || statement instanceof CreateUser || statement instanceof GrantPrivilege || statement instanceof DenyPrivilege || statement instanceof RevokePrivilege || statement instanceof DropUser || statement instanceof DropAnalyzer || statement instanceof DropFunction || statement instanceof DropTable || statement instanceof DropBlobTable || statement instanceof DropView || statement instanceof DropRepository || statement instanceof DropSnapshot || statement instanceof Update || statement instanceof Insert || statement instanceof SetSessionAuthorizationStatement || statement instanceof Window) {
        println(SqlFormatter.formatSql(statement));
        println("");
        assertFormattedSql(statement);
    }
    println("=".repeat(60));
    println("");
}
Also used : SetSessionAuthorizationStatement(io.crate.sql.tree.SetSessionAuthorizationStatement) Window(io.crate.sql.tree.Window) Query(io.crate.sql.tree.Query) GCDanglingArtifacts(io.crate.sql.tree.GCDanglingArtifacts) SetStatement(io.crate.sql.tree.SetStatement) CommitStatement(io.crate.sql.tree.CommitStatement) DeallocateStatement(io.crate.sql.tree.DeallocateStatement) BeginStatement(io.crate.sql.tree.BeginStatement) Statement(io.crate.sql.tree.Statement) KillStatement(io.crate.sql.tree.KillStatement) SetSessionAuthorizationStatement(io.crate.sql.tree.SetSessionAuthorizationStatement) CreateFunction(io.crate.sql.tree.CreateFunction) DropView(io.crate.sql.tree.DropView) ShowCreateTable(io.crate.sql.tree.ShowCreateTable) CreateTable(io.crate.sql.tree.CreateTable) DropUser(io.crate.sql.tree.DropUser) CreateUser(io.crate.sql.tree.CreateUser) DenyPrivilege(io.crate.sql.tree.DenyPrivilege) DropRepository(io.crate.sql.tree.DropRepository) DropTable(io.crate.sql.tree.DropTable) Update(io.crate.sql.tree.Update) Insert(io.crate.sql.tree.Insert) DropAnalyzer(io.crate.sql.tree.DropAnalyzer) GrantPrivilege(io.crate.sql.tree.GrantPrivilege) DropBlobTable(io.crate.sql.tree.DropBlobTable) CopyFrom(io.crate.sql.tree.CopyFrom) DropSnapshot(io.crate.sql.tree.DropSnapshot) RevokePrivilege(io.crate.sql.tree.RevokePrivilege) DropFunction(io.crate.sql.tree.DropFunction) SwapTable(io.crate.sql.tree.SwapTable)

Aggregations

Statement (io.crate.sql.tree.Statement)18 BeginStatement (io.crate.sql.tree.BeginStatement)8 CommitStatement (io.crate.sql.tree.CommitStatement)8 DeallocateStatement (io.crate.sql.tree.DeallocateStatement)8 KillStatement (io.crate.sql.tree.KillStatement)8 SetSessionAuthorizationStatement (io.crate.sql.tree.SetSessionAuthorizationStatement)8 SetStatement (io.crate.sql.tree.SetStatement)8 Test (org.junit.Test)8 Lists2 (io.crate.common.collections.Lists2)6 AnalyzedStatement (io.crate.analyze.AnalyzedStatement)5 ParamTypeHints (io.crate.analyze.ParamTypeHints)5 AccessControl (io.crate.auth.AccessControl)5 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)5 Row (io.crate.data.Row)5 Symbol (io.crate.expression.symbol.Symbol)5 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)5 NodeContext (io.crate.metadata.NodeContext)5 SqlParser (io.crate.sql.parser.SqlParser)5 DataType (io.crate.types.DataType)5 ArrayList (java.util.ArrayList)5