Search in sources :

Example 1 with AnalyzedDiscard

use of io.crate.analyze.AnalyzedDiscard in project crate by crate.

the class Session method execute.

@Nullable
public CompletableFuture<?> execute(String portalName, int maxRows, ResultReceiver<?> resultReceiver) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("method=execute portalName={} maxRows={}", portalName, maxRows);
    }
    Portal portal = getSafePortal(portalName);
    var analyzedStmt = portal.analyzedStatement();
    if (isReadOnly && analyzedStmt.isWriteOperation()) {
        throw new ReadOnlyException(portal.preparedStmt().rawStatement());
    }
    if (analyzedStmt instanceof AnalyzedBegin) {
        currentTransactionState = TransactionState.IN_TRANSACTION;
        resultReceiver.allFinished(false);
    } else if (analyzedStmt instanceof AnalyzedCommit) {
        currentTransactionState = TransactionState.IDLE;
        resultReceiver.allFinished(false);
        return resultReceiver.completionFuture();
    } else if (analyzedStmt instanceof AnalyzedDeallocate) {
        String stmtToDeallocate = ((AnalyzedDeallocate) analyzedStmt).preparedStmtName();
        if (stmtToDeallocate != null) {
            close((byte) 'S', stmtToDeallocate);
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("deallocating all prepared statements");
            }
            preparedStatements.clear();
        }
        resultReceiver.allFinished(false);
    } else if (analyzedStmt instanceof AnalyzedDiscard) {
        AnalyzedDiscard discard = (AnalyzedDiscard) analyzedStmt;
        // See https://www.postgresql.org/docs/current/sql-discard.html
        if (discard.target() == Target.ALL) {
            close();
        }
        resultReceiver.allFinished(false);
    } else if (analyzedStmt.isWriteOperation()) {
        /* We defer the execution for any other statements to `sync` messages so that we can efficiently process
             * bulk operations. E.g. If we receive `INSERT INTO (x) VALUES (?)` bindings/execute multiple times
             * We want to create bulk requests internally:                                                          /
             * - To reduce network overhead
             * - To have 1 disk flush per shard instead of 1 disk flush per item
             *
             * Many clients support this by doing something like this:
             *
             *      var preparedStatement = conn.prepareStatement("...")
             *      for (var args in manyArgs):
             *          preparedStatement.execute(args)
             *      conn.commit()
             */
        deferredExecutionsByStmt.compute(portal.preparedStmt().parsedStatement(), (key, oldValue) -> {
            DeferredExecution deferredExecution = new DeferredExecution(portal, maxRows, resultReceiver);
            if (oldValue == null) {
                ArrayList<DeferredExecution> deferredExecutions = new ArrayList<>();
                deferredExecutions.add(deferredExecution);
                return deferredExecutions;
            } else {
                oldValue.add(deferredExecution);
                return oldValue;
            }
        });
        return resultReceiver.completionFuture();
    } else {
        if (!deferredExecutionsByStmt.isEmpty()) {
            throw new UnsupportedOperationException("Only write operations are allowed in Batch statements");
        }
        if (activeExecution == null) {
            activeExecution = singleExec(portal, resultReceiver, maxRows);
        } else {
            activeExecution = activeExecution.thenCompose(ignored -> singleExec(portal, resultReceiver, maxRows));
        }
        return activeExecution;
    }
    return null;
}
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) AnalyzedBegin(io.crate.analyze.AnalyzedBegin) ArrayList(java.util.ArrayList) AnalyzedCommit(io.crate.analyze.AnalyzedCommit) AnalyzedDeallocate(io.crate.analyze.AnalyzedDeallocate) Portal(io.crate.protocols.postgres.Portal) AnalyzedDiscard(io.crate.analyze.AnalyzedDiscard) ReadOnlyException(io.crate.exceptions.ReadOnlyException) Nullable(javax.annotation.Nullable)

Aggregations

AnalyzedBegin (io.crate.analyze.AnalyzedBegin)1 AnalyzedCommit (io.crate.analyze.AnalyzedCommit)1 AnalyzedDeallocate (io.crate.analyze.AnalyzedDeallocate)1 AnalyzedDiscard (io.crate.analyze.AnalyzedDiscard)1 AnalyzedStatement (io.crate.analyze.AnalyzedStatement)1 Analyzer (io.crate.analyze.Analyzer)1 ParamTypeHints (io.crate.analyze.ParamTypeHints)1 QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)1 Relations (io.crate.analyze.Relations)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 AccessControl (io.crate.auth.AccessControl)1 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 Lists2 (io.crate.common.collections.Lists2)1 Row (io.crate.data.Row)1 Row1 (io.crate.data.Row1)1 RowConsumer (io.crate.data.RowConsumer)1 RowN (io.crate.data.RowN)1 ReadOnlyException (io.crate.exceptions.ReadOnlyException)1 SQLExceptions (io.crate.exceptions.SQLExceptions)1