use of io.crate.planner.Plan in project crate by crate.
the class DeleteStatementPlanner method deleteByQuery.
private static Plan deleteByQuery(DocTableInfo tableInfo, List<WhereClause> whereClauses, Planner.Context context) {
List<Plan> planNodes = new ArrayList<>();
List<String> indicesToDelete = new ArrayList<>();
for (WhereClause whereClause : whereClauses) {
String[] indices = Planner.indices(tableInfo, whereClause);
if (indices.length > 0) {
if (!whereClause.hasQuery() && tableInfo.isPartitioned()) {
indicesToDelete.addAll(Arrays.asList(indices));
} else {
planNodes.add(collectWithDeleteProjection(tableInfo, whereClause, context));
}
}
}
if (!indicesToDelete.isEmpty()) {
assert planNodes.isEmpty() : "If a partition can be deleted that must be true for all bulk operations";
return new ESDeletePartition(context.jobId(), indicesToDelete.toArray(new String[0]));
}
if (planNodes.isEmpty()) {
return new NoopPlan(context.jobId());
}
return new Delete(planNodes, context.jobId());
}
use of io.crate.planner.Plan in project crate by crate.
the class BatchPortal method sync.
@Override
public CompletableFuture<Void> sync(Planner planner, JobsLogs jobsLogs) {
CountdownFutureCallback completionCallback = new CountdownFutureCallback(analysis.size());
for (int i = 0; i < analysis.size(); i++) {
UUID jobId = UUID.randomUUID();
Plan plan;
String stmt = queries.get(i);
try {
plan = planner.plan(analysis.get(i), jobId, 0, 0);
} catch (Throwable t) {
jobsLogs.logPreExecutionFailure(jobId, stmt, SQLExceptions.messageOf(t));
throw t;
}
ResultReceiver resultReceiver = resultReceivers.get(i);
jobsLogs.logExecutionStart(jobId, stmt);
JobsLogsUpdateListener jobsLogsUpdateListener = new JobsLogsUpdateListener(jobId, jobsLogs);
resultReceiver.completionFuture().whenComplete(jobsLogsUpdateListener).whenComplete(completionCallback);
BatchConsumer consumer = new BatchConsumerToResultReceiver(resultReceiver, 0);
portalContext.getExecutor().execute(plan, consumer, new RowN(batchParams.toArray()));
}
synced = true;
return completionCallback;
}
use of io.crate.planner.Plan in project crate by crate.
the class SimplePortal method sync.
@Override
public CompletableFuture<?> sync(Planner planner, JobsLogs jobsLogs) {
UUID jobId = UUID.randomUUID();
Plan plan;
try {
plan = planner.plan(analysis, jobId, defaultLimit, maxRows);
} catch (Throwable t) {
jobsLogs.logPreExecutionFailure(jobId, query, SQLExceptions.messageOf(t));
throw t;
}
if (!analysis.analyzedStatement().isWriteOperation()) {
resultReceiver = new ResultReceiverRetryWrapper(resultReceiver, this, portalContext.getAnalyzer(), planner, portalContext.getExecutor(), jobId, sessionContext);
}
jobsLogs.logExecutionStart(jobId, query);
JobsLogsUpdateListener jobsLogsUpdateListener = new JobsLogsUpdateListener(jobId, jobsLogs);
CompletableFuture completableFuture = resultReceiver.completionFuture().whenComplete(jobsLogsUpdateListener);
if (!resumeIfSuspended()) {
consumer = new BatchConsumerToResultReceiver(resultReceiver, maxRows);
portalContext.getExecutor().execute(plan, consumer, this.rowParams);
}
synced = true;
return completableFuture;
}
use of io.crate.planner.Plan 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();
}
use of io.crate.planner.Plan in project crate by crate.
the class Session method retryQuery.
private void retryQuery(UUID jobId, AnalyzedStatement stmt, RoutingProvider routingProvider, RowConsumer consumer, Row params, CoordinatorTxnCtx txnCtx, NodeContext nodeCtx) {
PlannerContext plannerContext = new PlannerContext(planner.currentClusterState(), routingProvider, jobId, txnCtx, nodeCtx, 0, params);
Plan plan = planner.plan(stmt, plannerContext);
plan.execute(executor, plannerContext, consumer, params, SubQueryResults.EMPTY);
}
Aggregations