use of io.crate.action.sql.BaseResultReceiver in project crate by crate.
the class PreExecutionBenchmark method setup.
@Setup
public void setup() throws Exception {
Path tempDir = Files.createTempDirectory("");
Settings settings = Settings.builder().put("path.home", tempDir.toAbsolutePath().toString()).build();
Environment environment = new Environment(settings, tempDir);
node = new Node(environment, List.of(Netty4Plugin.class), true);
node.start();
Injector injector = node.injector();
sqlOperations = injector.getInstance(SQLOperations.class);
analyzer = injector.getInstance(Analyzer.class);
planner = injector.getInstance(Planner.class);
clusterService = injector.getInstance(ClusterService.class);
nodeCtx = injector.getInstance(NodeContext.class);
String statement = "create table users (id int primary key, name string, date timestamp, text string index using fulltext)";
var resultReceiver = new BaseResultReceiver();
sqlOperations.newSystemSession().quickExec(statement, resultReceiver, Row.EMPTY);
resultReceiver.completionFuture().get(5, TimeUnit.SECONDS);
}
use of io.crate.action.sql.BaseResultReceiver in project crate by crate.
the class SQLTransportExecutor method execute.
private void execute(String stmt, @Nullable Object[][] bulkArgs, final ActionListener<long[]> listener) {
Session session = newSession();
try {
session.parse(UNNAMED, stmt, Collections.emptyList());
if (bulkArgs == null) {
bulkArgs = new Object[0][];
}
final long[] rowCounts = new long[bulkArgs.length];
if (rowCounts.length == 0) {
session.bind(UNNAMED, UNNAMED, Collections.emptyList(), null);
session.execute(UNNAMED, 0, new BaseResultReceiver());
} else {
for (int i = 0; i < bulkArgs.length; i++) {
session.bind(UNNAMED, UNNAMED, Arrays.asList(bulkArgs[i]), null);
ResultReceiver<?> resultReceiver = new BulkRowCountReceiver(rowCounts, i);
session.execute(UNNAMED, 0, resultReceiver);
}
}
List<Symbol> outputColumns = session.describe('P', UNNAMED).getFields();
if (outputColumns != null) {
throw new UnsupportedOperationException("Bulk operations for statements that return result sets is not supported");
}
session.sync().whenComplete((Object result, Throwable t) -> {
if (t == null) {
listener.onResponse(rowCounts);
} else {
listener.onFailure(Exceptions.toRuntimeException(t));
}
session.close();
});
} catch (Throwable t) {
session.close();
listener.onFailure(Exceptions.toRuntimeException(t));
}
}
use of io.crate.action.sql.BaseResultReceiver in project crate by crate.
the class RetryOnFailureResultReceiverTest method testRetryOnNodeConnectionError.
@Test
public void testRetryOnNodeConnectionError() throws Exception {
AtomicInteger numRetries = new AtomicInteger(0);
BaseResultReceiver baseResultReceiver = new BaseResultReceiver();
ClusterState initialState = clusterService.state();
RetryOnFailureResultReceiver retryOnFailureResultReceiver = new RetryOnFailureResultReceiver(clusterService, initialState, indexName -> true, baseResultReceiver, UUID.randomUUID(), (newJobId, receiver) -> numRetries.incrementAndGet());
// Must have a different cluster state then the initial state to trigger a retry
clusterService.submitStateUpdateTask("dummy", new DummyUpdate());
assertBusy(() -> assertThat(initialState, Matchers.not(sameInstance(clusterService.state()))));
retryOnFailureResultReceiver.fail(new ConnectTransportException(null, "node not connected"));
assertThat(numRetries.get(), is(1));
}
use of io.crate.action.sql.BaseResultReceiver in project crate by crate.
the class BatchPortalTest method testEachStatementReceivesCorrectParams.
@Test
public void testEachStatementReceivesCorrectParams() throws Throwable {
SQLExecutor sqlExecutor = SQLExecutor.builder(clusterService).addTable("create table t1 (x int)").build();
Plan insertPlan = new Plan() {
@Override
public StatementType type() {
return StatementType.INSERT;
}
@Override
public void executeOrFail(DependencyCarrier executor, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) {
consumer.accept(InMemoryBatchIterator.of(params, null), null);
}
};
Planner planner = new Planner(Settings.EMPTY, clusterService, sqlExecutor.nodeCtx, new TableStats(), null, null, sqlExecutor.schemas(), new StubUserManager(), mock(SessionSettingRegistry.class)) {
@Override
public Plan plan(AnalyzedStatement analyzedStatement, PlannerContext plannerContext) {
return insertPlan;
}
};
DependencyCarrier executor = mock(DependencyCarrier.class, Answers.RETURNS_MOCKS);
Session session = new Session(sqlExecutor.nodeCtx, sqlExecutor.analyzer, planner, new JobsLogs(() -> false), false, executor, AccessControl.DISABLED, SessionContext.systemSessionContext());
session.parse("S_1", "insert into t1(x) values(1)", Collections.emptyList());
session.bind("Portal", "S_1", Collections.emptyList(), null);
final ArrayList<Object[]> s1Rows = new ArrayList<>();
session.execute("Portal", 0, new BaseResultReceiver() {
@Override
public void setNextRow(Row row) {
s1Rows.add(row.materialize());
}
});
session.parse("S_2", "insert into t1(x) values(?)", Collections.emptyList());
session.bind("Portal", "S_2", Collections.singletonList(2), null);
final ArrayList<Object[]> s2Rows = new ArrayList<>();
session.execute("Portal", 0, new BaseResultReceiver() {
@Override
public void setNextRow(Row row) {
s2Rows.add(row.materialize());
}
});
session.sync().get(5, TimeUnit.SECONDS);
assertThat(s1Rows, contains(emptyArray()));
assertThat(s2Rows, contains(arrayContaining(is(2))));
}
use of io.crate.action.sql.BaseResultReceiver in project crate by crate.
the class ExplainPlan method executeOrFail.
@Override
public void executeOrFail(DependencyCarrier dependencies, PlannerContext plannerContext, RowConsumer consumer, Row params, SubQueryResults subQueryResults) {
if (context != null) {
assert subPlan instanceof LogicalPlan : "subPlan must be a LogicalPlan";
LogicalPlan plan = (LogicalPlan) subPlan;
/**
* EXPLAIN ANALYZE does not support analyzing {@link io.crate.planner.MultiPhasePlan}s
*/
if (plan.dependencies().isEmpty()) {
UUID jobId = plannerContext.jobId();
BaseResultReceiver resultReceiver = new BaseResultReceiver();
RowConsumer noopRowConsumer = new RowConsumerToResultReceiver(resultReceiver, 0, t -> {
});
Timer timer = context.createTimer(Phase.Execute.name());
timer.start();
NodeOperationTree operationTree = LogicalPlanner.getNodeOperationTree(plan, dependencies, plannerContext, params, subQueryResults);
resultReceiver.completionFuture().whenComplete(createResultConsumer(dependencies, consumer, jobId, timer, operationTree));
LogicalPlanner.executeNodeOpTree(dependencies, plannerContext.transactionContext(), jobId, noopRowConsumer, true, operationTree);
} else {
consumer.accept(null, new UnsupportedOperationException("EXPLAIN ANALYZE does not support profiling multi-phase plans, " + "such as queries with scalar subselects."));
}
} else {
if (subPlan instanceof LogicalPlan) {
PrintContext printContext = new PrintContext();
((LogicalPlan) subPlan).print(printContext);
consumer.accept(InMemoryBatchIterator.of(new Row1(printContext.toString()), SENTINEL), null);
} else if (subPlan instanceof CopyFromPlan) {
ExecutionPlan executionPlan = CopyFromPlan.planCopyFromExecution(((CopyFromPlan) subPlan).copyFrom(), dependencies.clusterService().state().nodes(), plannerContext, params, subQueryResults);
String planAsJson = DataTypes.STRING.implicitCast(PlanPrinter.objectMap(executionPlan));
consumer.accept(InMemoryBatchIterator.of(new Row1(planAsJson), SENTINEL), null);
} else {
consumer.accept(InMemoryBatchIterator.of(new Row1("EXPLAIN not supported for " + subPlan.getClass().getSimpleName()), SENTINEL), null);
}
}
}
Aggregations