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();
}
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;
}
}
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");
}
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()));
}
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("");
}
Aggregations