Search in sources :

Example 1 with SqlCursor

use of org.apache.ignite.internal.sql.engine.SqlCursor in project ignite-3 by apache.

the class ExecutionServiceImpl method mapAndExecutePlan.

private SqlCursor<List<?>> mapAndExecutePlan(RootQuery<RowT> qry, MultiStepPlan plan) {
    qry.mapping();
    plan.init(mappingSrvc, new MappingQueryContext(locNodeId, topologyVersion()));
    List<Fragment> fragments = plan.fragments();
    // Local execution
    Fragment fragment = first(fragments);
    if (IgniteUtils.assertionsEnabled()) {
        assert fragment != null;
        FragmentMapping mapping = plan.mapping(fragment);
        assert mapping != null;
        List<String> nodes = mapping.nodeIds();
        assert nodes != null && nodes.size() == 1 && first(nodes).equals(locNodeId);
    }
    FragmentDescription fragmentDesc = new FragmentDescription(fragment.fragmentId(), plan.mapping(fragment), plan.target(fragment), plan.remotes(fragment));
    ExecutionContext<RowT> ectx = new ExecutionContext<>(qry.context(), taskExecutor, qry.id(), locNodeId, locNodeId, topologyVersion(), fragmentDesc, handler, Commons.parametersMap(qry.parameters()));
    Node<RowT> node = new LogicalRelImplementor<>(ectx, affSrvc, mailboxRegistry, exchangeSrvc).go(fragment.root());
    qry.run(ectx, plan, node);
    // start remote execution
    for (int i = 1; i < fragments.size(); i++) {
        fragment = fragments.get(i);
        fragmentDesc = new FragmentDescription(fragment.fragmentId(), plan.mapping(fragment), plan.target(fragment), plan.remotes(fragment));
        Throwable ex = null;
        for (String nodeId : fragmentDesc.nodeIds()) {
            if (ex != null) {
                qry.onResponse(nodeId, fragment.fragmentId(), ex);
            } else {
                try {
                    QueryStartRequest req = FACTORY.queryStartRequest().queryId(qry.id()).fragmentId(fragment.fragmentId()).schema(qry.context().schemaName()).root(fragment.serialized()).topologyVersion(ectx.topologyVersion()).fragmentDescription(fragmentDesc).parameters(qry.parameters()).build();
                    msgSrvc.send(nodeId, req);
                } catch (Throwable e) {
                    qry.onResponse(nodeId, fragment.fragmentId(), ex = e);
                }
            }
        }
    }
    return Commons.createCursor(new TransformingIterator<>(iteratorsHolder.iterator(qry.iterator()), row -> {
        int rowSize = ectx.rowHandler().columnCount(row);
        List<Object> res = new ArrayList<>(rowSize);
        for (int i = 0; i < rowSize; i++) {
            res.add(ectx.rowHandler().get(i, row));
        }
        return res;
    }), plan);
}
Also used : MappingServiceImpl(org.apache.ignite.internal.sql.engine.metadata.MappingServiceImpl) RunningFragment(org.apache.ignite.internal.sql.engine.RunningFragment) Frameworks(org.apache.calcite.tools.Frameworks) PlanningContext(org.apache.ignite.internal.sql.engine.prepare.PlanningContext) ErrorMessage(org.apache.ignite.internal.sql.engine.message.ErrorMessage) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) IgniteLogger(org.apache.ignite.lang.IgniteLogger) Collections.singletonList(java.util.Collections.singletonList) Contexts(org.apache.calcite.plan.Contexts) MappingService(org.apache.ignite.internal.sql.engine.metadata.MappingService) QueryRegistry(org.apache.ignite.internal.sql.engine.QueryRegistry) QueryState(org.apache.ignite.internal.sql.engine.QueryState) DdlPlan(org.apache.ignite.internal.sql.engine.prepare.DdlPlan) AffinityService(org.apache.ignite.internal.sql.engine.metadata.AffinityService) SqlSchemaManager(org.apache.ignite.internal.sql.engine.schema.SqlSchemaManager) Outbox(org.apache.ignite.internal.sql.engine.exec.rel.Outbox) FragmentMapping(org.apache.ignite.internal.sql.engine.metadata.FragmentMapping) MultiStepPlan(org.apache.ignite.internal.sql.engine.prepare.MultiStepPlan) Context(org.apache.calcite.plan.Context) Fragment(org.apache.ignite.internal.sql.engine.prepare.Fragment) ResultSetMetadataImpl(org.apache.ignite.internal.sql.engine.prepare.ResultSetMetadataImpl) ClusterNode(org.apache.ignite.network.ClusterNode) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ResultSetMetadataInternal(org.apache.ignite.internal.sql.engine.prepare.ResultSetMetadataInternal) IgniteInternalCheckedException(org.apache.ignite.lang.IgniteInternalCheckedException) RelJsonReader.fromJson(org.apache.ignite.internal.sql.engine.externalize.RelJsonReader.fromJson) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) FragmentPlan(org.apache.ignite.internal.sql.engine.prepare.FragmentPlan) QueryStartRequest(org.apache.ignite.internal.sql.engine.message.QueryStartRequest) Node(org.apache.ignite.internal.sql.engine.exec.rel.Node) SqlQueryMessagesFactory(org.apache.ignite.internal.sql.engine.message.SqlQueryMessagesFactory) RootQuery(org.apache.ignite.internal.sql.engine.RootQuery) RunningQuery(org.apache.ignite.internal.sql.engine.RunningQuery) QueryPlanCache(org.apache.ignite.internal.sql.engine.prepare.QueryPlanCache) ArrayList(java.util.ArrayList) Query(org.apache.ignite.internal.sql.engine.Query) Inbox(org.apache.ignite.internal.sql.engine.exec.rel.Inbox) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) SqlCursor(org.apache.ignite.internal.sql.engine.SqlCursor) RemoteException(org.apache.ignite.internal.sql.engine.metadata.RemoteException) CacheKey(org.apache.ignite.internal.sql.engine.prepare.CacheKey) TableManager(org.apache.ignite.internal.table.distributed.TableManager) DdlCommandHandler(org.apache.ignite.internal.sql.engine.exec.ddl.DdlCommandHandler) RelDataType(org.apache.calcite.rel.type.RelDataType) NodeLeaveHandler(org.apache.ignite.internal.sql.engine.util.NodeLeaveHandler) TopologyService(org.apache.ignite.network.TopologyService) ExplainPlan(org.apache.ignite.internal.sql.engine.prepare.ExplainPlan) BaseQueryContext(org.apache.ignite.internal.sql.engine.util.BaseQueryContext) TypeUtils(org.apache.ignite.internal.sql.engine.util.TypeUtils) Commons(org.apache.ignite.internal.sql.engine.util.Commons) FragmentDescription(org.apache.ignite.internal.sql.engine.metadata.FragmentDescription) QueryPlan(org.apache.ignite.internal.sql.engine.prepare.QueryPlan) TransformingIterator(org.apache.ignite.internal.sql.engine.util.TransformingIterator) FRAMEWORK_CONFIG(org.apache.ignite.internal.sql.engine.util.Commons.FRAMEWORK_CONFIG) QueryStartResponse(org.apache.ignite.internal.sql.engine.message.QueryStartResponse) SqlQueryMessageGroup(org.apache.ignite.internal.sql.engine.message.SqlQueryMessageGroup) CollectionUtils.first(org.apache.ignite.internal.util.CollectionUtils.first) Collections(java.util.Collections) MessageService(org.apache.ignite.internal.sql.engine.message.MessageService) MappingQueryContext(org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext) RunningFragment(org.apache.ignite.internal.sql.engine.RunningFragment) Fragment(org.apache.ignite.internal.sql.engine.prepare.Fragment) FragmentMapping(org.apache.ignite.internal.sql.engine.metadata.FragmentMapping) QueryStartRequest(org.apache.ignite.internal.sql.engine.message.QueryStartRequest) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) FragmentDescription(org.apache.ignite.internal.sql.engine.metadata.FragmentDescription)

Example 2 with SqlCursor

use of org.apache.ignite.internal.sql.engine.SqlCursor in project ignite-3 by apache.

the class QueryChecker method check.

/**
 * Run checks.
 */
public void check() {
    // Check plan.
    QueryProcessor qryProc = getEngine();
    List<SqlCursor<List<?>>> explainCursors = qryProc.query("PUBLIC", "EXPLAIN PLAN FOR " + qry);
    Cursor<List<?>> explainCursor = explainCursors.get(0);
    List<List<?>> explainRes = getAllFromCursor(explainCursor);
    String actualPlan = (String) explainRes.get(0).get(0);
    if (!CollectionUtils.nullOrEmpty(planMatchers)) {
        for (Matcher<String> matcher : planMatchers) {
            assertThat("Invalid plan:\n" + actualPlan, actualPlan, matcher);
        }
    }
    if (exactPlan != null) {
        assertEquals(exactPlan, actualPlan);
    }
    // Check result.
    List<SqlCursor<List<?>>> cursors = qryProc.query("PUBLIC", qry, params);
    SqlCursor<List<?>> cur = cursors.get(0);
    if (expectedColumnNames != null) {
        List<String> colNames = cur.metadata().fields().stream().map(ResultFieldMetadata::name).collect(Collectors.toList());
        assertThat("Column names don't match", colNames, equalTo(expectedColumnNames));
    }
    if (expectedColumnTypes != null) {
        List<Type> colNames = cur.metadata().fields().stream().map(ResultFieldMetadata::type).map(Commons::nativeTypeToClass).collect(Collectors.toList());
        assertThat("Column types don't match", colNames, equalTo(expectedColumnTypes));
    }
    List<List<?>> res = CursorUtils.getAllFromCursor(cur);
    if (expectedResult != null) {
        if (!ordered) {
            // Avoid arbitrary order.
            res.sort(new ListComparator());
            expectedResult.sort(new ListComparator());
        }
        assertEqualsCollections(expectedResult, res);
    }
}
Also used : QueryProcessor(org.apache.ignite.internal.sql.engine.QueryProcessor) Type(java.lang.reflect.Type) SqlCursor(org.apache.ignite.internal.sql.engine.SqlCursor) ResultFieldMetadata(org.apache.ignite.internal.sql.engine.ResultFieldMetadata) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with SqlCursor

use of org.apache.ignite.internal.sql.engine.SqlCursor in project ignite-3 by apache.

the class JdbcQueryEventHandlerImpl method queryAsync.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<QueryExecuteResult> queryAsync(QueryExecuteRequest req) {
    if (req.pageSize() <= 0) {
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "Invalid fetch size : [fetchSize=" + req.pageSize() + ']'));
    }
    List<SqlCursor<List<?>>> cursors;
    try {
        QueryContext context = createQueryContext(req.getStmtType());
        List<SqlCursor<List<?>>> queryCursors = processor.query(context, req.schemaName(), req.sqlQuery(), req.arguments() == null ? OBJECT_EMPTY_ARRAY : req.arguments());
        cursors = queryCursors.stream().map(cursor -> new JdbcQueryCursor<>(req.maxRows(), cursor)).collect(Collectors.toList());
    } catch (Exception e) {
        StringWriter sw = getWriterWithStackTrace(e);
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "Exception while executing query " + req.sqlQuery() + ". Error message: " + sw));
    }
    if (cursors.isEmpty()) {
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "At least one cursor is expected for query " + req.sqlQuery()));
    }
    List<QuerySingleResult> results = new ArrayList<>();
    try {
        for (SqlCursor<List<?>> cur : cursors) {
            QuerySingleResult res = createJdbcResult(cur, req);
            results.add(res);
        }
    } catch (Exception ex) {
        StringWriter sw = getWriterWithStackTrace(ex);
        return CompletableFuture.completedFuture(new QueryExecuteResult(Response.STATUS_FAILED, "Failed to fetch results for query " + req.sqlQuery() + ". Error message: " + sw));
    }
    return CompletableFuture.completedFuture(new QueryExecuteResult(results));
}
Also used : SqlCursor(org.apache.ignite.internal.sql.engine.SqlCursor) StringWriter(java.io.StringWriter) QueryExecuteResult(org.apache.ignite.client.proto.query.event.QueryExecuteResult) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ArrayList(java.util.ArrayList) IntList(it.unimi.dsi.fastutil.ints.IntList) QueryContext(org.apache.ignite.internal.sql.engine.QueryContext) QuerySingleResult(org.apache.ignite.client.proto.query.event.QuerySingleResult) QueryValidationException(org.apache.ignite.internal.sql.engine.exec.QueryValidationException)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 SqlCursor (org.apache.ignite.internal.sql.engine.SqlCursor)3 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 IntList (it.unimi.dsi.fastutil.ints.IntList)1 StringWriter (java.io.StringWriter)1 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1 Collections.singletonList (java.util.Collections.singletonList)1 Objects (java.util.Objects)1 Context (org.apache.calcite.plan.Context)1 Contexts (org.apache.calcite.plan.Contexts)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 Frameworks (org.apache.calcite.tools.Frameworks)1 QueryExecuteResult (org.apache.ignite.client.proto.query.event.QueryExecuteResult)1 QuerySingleResult (org.apache.ignite.client.proto.query.event.QuerySingleResult)1 Query (org.apache.ignite.internal.sql.engine.Query)1 QueryContext (org.apache.ignite.internal.sql.engine.QueryContext)1 QueryProcessor (org.apache.ignite.internal.sql.engine.QueryProcessor)1 QueryRegistry (org.apache.ignite.internal.sql.engine.QueryRegistry)1