use of io.crate.sql.tree.Statement in project crate by crate.
the class TestStatementBuilder method testDropSnapshotStmtBuilder.
@Test
public void testDropSnapshotStmtBuilder() {
Statement statement = SqlParser.createStatement("DROP SNAPSHOT my_repo.my_snapshot");
assertThat(statement.toString(), is("DropSnapshot{name=my_repo.my_snapshot}"));
}
use of io.crate.sql.tree.Statement in project crate by crate.
the class TreeAssertions method assertFormattedSql.
static void assertFormattedSql(Node expected) {
String formatted = formatSql(expected);
// verify round-trip of formatting already-formatted SQL
Statement actual = parseFormatted(formatted, expected);
assertEquals(formatSql(actual), formatted);
// compare parsed tree with parsed tree of formatted SQL
if (!actual.equals(expected)) {
// simplify finding the non-equal part of the tree
assertListEquals(linearizeTree(actual), linearizeTree(expected));
}
assertEquals(actual, expected);
}
use of io.crate.sql.tree.Statement in project crate by crate.
the class Session method quickExec.
/**
* Execute a query in one step, avoiding the parse/bind/execute/sync procedure.
* Opposed to using parse/bind/execute/sync this method is thread-safe.
*
* @param parse A function to parse the statement; This can be used to cache the parsed statement.
* Use {@link #quickExec(String, ResultReceiver, Row)} to use the regular parser
*/
public void quickExec(String statement, Function<String, Statement> parse, ResultReceiver<?> resultReceiver, Row params) {
CoordinatorTxnCtx txnCtx = new CoordinatorTxnCtx(sessionContext);
Statement parsedStmt = parse.apply(statement);
AnalyzedStatement analyzedStatement = analyzer.analyze(parsedStmt, sessionContext, ParamTypeHints.EMPTY);
RoutingProvider routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
UUID jobId = UUIDs.dirtyUUID();
ClusterState clusterState = planner.currentClusterState();
PlannerContext plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, 0, params);
Plan plan;
try {
plan = planner.plan(analyzedStatement, plannerContext);
} catch (Throwable t) {
jobsLogs.logPreExecutionFailure(jobId, statement, SQLExceptions.messageOf(t), sessionContext.sessionUser());
throw t;
}
StatementClassifier.Classification classification = StatementClassifier.classify(plan);
jobsLogs.logExecutionStart(jobId, statement, sessionContext.sessionUser(), classification);
JobsLogsUpdateListener jobsLogsUpdateListener = new JobsLogsUpdateListener(jobId, jobsLogs);
if (!analyzedStatement.isWriteOperation()) {
resultReceiver = new RetryOnFailureResultReceiver(executor.clusterService(), clusterState, // clusterState at the time of the index check is used
indexName -> clusterState.metadata().hasIndex(indexName), resultReceiver, jobId, (newJobId, retryResultReceiver) -> retryQuery(newJobId, analyzedStatement, routingProvider, new RowConsumerToResultReceiver(retryResultReceiver, 0, jobsLogsUpdateListener), params, txnCtx, nodeCtx));
}
RowConsumerToResultReceiver consumer = new RowConsumerToResultReceiver(resultReceiver, 0, jobsLogsUpdateListener);
plan.execute(executor, plannerContext, consumer, params, SubQueryResults.EMPTY);
}
use of io.crate.sql.tree.Statement in project crate by crate.
the class Session method parse.
public void parse(String statementName, String query, List<DataType> paramTypes) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("method=parse stmtName={} query={} paramTypes={}", statementName, query, paramTypes);
}
Statement statement;
try {
statement = SqlParser.createStatement(query);
} catch (Throwable t) {
if ("".equals(query)) {
statement = EMPTY_STMT;
} else {
jobsLogs.logPreExecutionFailure(UUIDs.dirtyUUID(), query, SQLExceptions.messageOf(t), sessionContext.sessionUser());
throw t;
}
}
analyze(statementName, statement, paramTypes, query);
}
use of io.crate.sql.tree.Statement in project crate by crate.
the class Session method bulkExec.
private CompletableFuture<?> bulkExec(Statement statement, List<DeferredExecution> toExec) {
assert toExec.size() >= 1 : "Must have at least 1 deferred execution for bulk exec";
var jobId = UUIDs.dirtyUUID();
var routingProvider = new RoutingProvider(Randomness.get().nextInt(), planner.getAwarenessAttributes());
var clusterState = executor.clusterService().state();
var txnCtx = new CoordinatorTxnCtx(sessionContext);
var plannerContext = new PlannerContext(clusterState, routingProvider, jobId, txnCtx, nodeCtx, 0, null);
PreparedStmt firstPreparedStatement = toExec.get(0).portal().preparedStmt();
AnalyzedStatement analyzedStatement = firstPreparedStatement.analyzedStatement();
Plan plan;
try {
plan = planner.plan(analyzedStatement, plannerContext);
} catch (Throwable t) {
jobsLogs.logPreExecutionFailure(jobId, firstPreparedStatement.rawStatement(), SQLExceptions.messageOf(t), sessionContext.sessionUser());
throw t;
}
jobsLogs.logExecutionStart(jobId, firstPreparedStatement.rawStatement(), sessionContext.sessionUser(), StatementClassifier.classify(plan));
var bulkArgs = Lists2.map(toExec, x -> (Row) new RowN(x.portal().params().toArray()));
List<CompletableFuture<Long>> rowCounts = plan.executeBulk(executor, plannerContext, bulkArgs, SubQueryResults.EMPTY);
CompletableFuture<Void> allRowCounts = CompletableFuture.allOf(rowCounts.toArray(new CompletableFuture[0]));
List<CompletableFuture<?>> resultReceiverFutures = Lists2.map(toExec, x -> x.resultReceiver().completionFuture());
CompletableFuture<Void> allResultReceivers = CompletableFuture.allOf(resultReceiverFutures.toArray(new CompletableFuture[0]));
return allRowCounts.exceptionally(// swallow exception - failures are set per item in emitResults
t -> null).thenAccept(ignored -> emitRowCountsToResultReceivers(jobId, jobsLogs, toExec, rowCounts)).runAfterBoth(allResultReceivers, () -> {
});
}
Aggregations