Search in sources :

Example 1 with BaseResultReceiver

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);
}
Also used : Path(java.nio.file.Path) NodeContext(io.crate.metadata.NodeContext) Node(org.elasticsearch.node.Node) ClusterService(org.elasticsearch.cluster.service.ClusterService) Injector(org.elasticsearch.common.inject.Injector) Environment(org.elasticsearch.env.Environment) BaseResultReceiver(io.crate.action.sql.BaseResultReceiver) Planner(io.crate.planner.Planner) SQLOperations(io.crate.action.sql.SQLOperations) Settings(org.elasticsearch.common.settings.Settings) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with BaseResultReceiver

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));
    }
}
Also used : Symbol(io.crate.expression.symbol.Symbol) BaseResultReceiver(io.crate.action.sql.BaseResultReceiver) PGpoint(org.postgresql.geometric.PGpoint) Session(io.crate.action.sql.Session)

Example 3 with BaseResultReceiver

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));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) BaseResultReceiver(io.crate.action.sql.BaseResultReceiver) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 4 with BaseResultReceiver

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))));
}
Also used : DependencyCarrier(io.crate.planner.DependencyCarrier) SubQueryResults(io.crate.planner.operators.SubQueryResults) ArrayList(java.util.ArrayList) Plan(io.crate.planner.Plan) TableStats(io.crate.statistics.TableStats) StubUserManager(io.crate.user.StubUserManager) SessionSettingRegistry(io.crate.metadata.settings.session.SessionSettingRegistry) PlannerContext(io.crate.planner.PlannerContext) SQLExecutor(io.crate.testing.SQLExecutor) BaseResultReceiver(io.crate.action.sql.BaseResultReceiver) Planner(io.crate.planner.Planner) AnalyzedStatement(io.crate.analyze.AnalyzedStatement) RowConsumer(io.crate.data.RowConsumer) Row(io.crate.data.Row) JobsLogs(io.crate.execution.engine.collect.stats.JobsLogs) Session(io.crate.action.sql.Session) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 5 with BaseResultReceiver

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);
        }
    }
}
Also used : CopyFromPlan(io.crate.planner.statement.CopyFromPlan) Row1(io.crate.data.Row1) RowConsumerToResultReceiver(io.crate.action.sql.RowConsumerToResultReceiver) NodeOperationTree(io.crate.execution.dsl.phases.NodeOperationTree) ExecutionPlan(io.crate.planner.ExecutionPlan) Timer(io.crate.profile.Timer) PrintContext(io.crate.planner.operators.PrintContext) BaseResultReceiver(io.crate.action.sql.BaseResultReceiver) LogicalPlan(io.crate.planner.operators.LogicalPlan) RowConsumer(io.crate.data.RowConsumer) UUID(java.util.UUID)

Aggregations

BaseResultReceiver (io.crate.action.sql.BaseResultReceiver)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)3 Test (org.junit.Test)3 Session (io.crate.action.sql.Session)2 RowConsumer (io.crate.data.RowConsumer)2 Planner (io.crate.planner.Planner)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 RowConsumerToResultReceiver (io.crate.action.sql.RowConsumerToResultReceiver)1 SQLOperations (io.crate.action.sql.SQLOperations)1 AnalyzedStatement (io.crate.analyze.AnalyzedStatement)1 Row (io.crate.data.Row)1 Row1 (io.crate.data.Row1)1 NodeOperationTree (io.crate.execution.dsl.phases.NodeOperationTree)1 JobsLogs (io.crate.execution.engine.collect.stats.JobsLogs)1 Symbol (io.crate.expression.symbol.Symbol)1 NodeContext (io.crate.metadata.NodeContext)1 SessionSettingRegistry (io.crate.metadata.settings.session.SessionSettingRegistry)1 DependencyCarrier (io.crate.planner.DependencyCarrier)1 ExecutionPlan (io.crate.planner.ExecutionPlan)1