Search in sources :

Example 1 with QueryPlan

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

the class ExecutionServiceImpl method onMessage.

@SuppressWarnings("unchecked")
private void onMessage(String nodeId, QueryStartRequest msg) {
    assert nodeId != null && msg != null;
    try {
        Query<RowT> qry = (Query<RowT>) queryRegistry.register(new Query<>(msg.queryId(), nodeId, null, exchangeSrvc, (q) -> queryRegistry.unregister(q.id()), LOG));
        QueryPlan qryPlan = qryPlanCache.queryPlan(new CacheKey(msg.schema(), msg.root()), () -> prepareFragment(msg.root()));
        FragmentPlan plan = (FragmentPlan) qryPlan;
        final BaseQueryContext qctx = createQueryContext(Contexts.empty(), msg.schema());
        ExecutionContext<RowT> ectx = new ExecutionContext<>(qctx, taskExecutor, msg.queryId(), locNodeId, nodeId, msg.topologyVersion(), msg.fragmentDescription(), handler, Commons.parametersMap(msg.parameters()));
        executeFragment(qry, plan, ectx);
    } catch (Throwable ex) {
        LOG.error("Failed to start query fragment", ex);
        mailboxRegistry.outboxes(msg.queryId(), msg.fragmentId(), -1).forEach(Outbox::close);
        mailboxRegistry.inboxes(msg.queryId(), msg.fragmentId(), -1).forEach(Inbox::close);
        try {
            msgSrvc.send(nodeId, FACTORY.queryStartResponse().queryId(msg.queryId()).fragmentId(msg.fragmentId()).error(ex).build());
        } catch (Exception e) {
            LOG.error("Error occurred during send error message", e);
            IgniteInternalException wrpEx = new IgniteInternalException("Error occurred during send error message", e);
            e.addSuppressed(ex);
            RunningQuery qry = queryRegistry.query(msg.queryId());
            qry.cancel();
            throw wrpEx;
        }
        throw ex;
    }
}
Also used : FragmentPlan(org.apache.ignite.internal.sql.engine.prepare.FragmentPlan) RootQuery(org.apache.ignite.internal.sql.engine.RootQuery) RunningQuery(org.apache.ignite.internal.sql.engine.RunningQuery) Query(org.apache.ignite.internal.sql.engine.Query) BaseQueryContext(org.apache.ignite.internal.sql.engine.util.BaseQueryContext) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) QueryPlan(org.apache.ignite.internal.sql.engine.prepare.QueryPlan) IgniteInternalCheckedException(org.apache.ignite.lang.IgniteInternalCheckedException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) RemoteException(org.apache.ignite.internal.sql.engine.metadata.RemoteException) RunningQuery(org.apache.ignite.internal.sql.engine.RunningQuery) CacheKey(org.apache.ignite.internal.sql.engine.prepare.CacheKey)

Example 2 with QueryPlan

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

the class SqlQueryProcessor method query0.

private List<SqlCursor<List<?>>> query0(QueryContext context, String schemaName, String sql, Object... params) {
    SchemaPlus schema = schemaManager.schema(schemaName);
    assert schema != null : "Schema not found: " + schemaName;
    QueryPlan plan = planCache.queryPlan(new CacheKey(schema.getName(), sql));
    if (plan != null) {
        final QueryPlan finalPlan = plan;
        context.maybeUnwrap(QueryValidator.class).ifPresent(queryValidator -> queryValidator.validatePlan(finalPlan));
        RootQuery<Object[]> qry = new RootQuery<>(sql, schema, params, exchangeService, (q) -> queryRegistry.unregister(q.id()), LOG);
        queryRegistry.register(qry);
        try {
            return Collections.singletonList(executionSrvc.executePlan(qry, plan));
        } catch (Exception e) {
            boolean isCanceled = qry.isCancelled();
            qry.cancel();
            queryRegistry.unregister(qry.id());
            if (isCanceled) {
                throw new IgniteInternalException("The query was cancelled while planning", e);
            } else {
                throw e;
            }
        }
    }
    SqlNodeList qryList = Commons.parse(sql, FRAMEWORK_CONFIG.getParserConfig());
    List<SqlCursor<List<?>>> cursors = new ArrayList<>(qryList.size());
    List<RootQuery<Object[]>> qrys = new ArrayList<>(qryList.size());
    for (final SqlNode sqlNode : qryList) {
        RootQuery<Object[]> qry = new RootQuery<>(sqlNode.toString(), // Update schema for each query in multiple statements.
        schemaManager.schema(schemaName), params, exchangeService, (q) -> queryRegistry.unregister(q.id()), LOG);
        qrys.add(qry);
        queryRegistry.register(qry);
        try {
            if (qryList.size() == 1) {
                plan = planCache.queryPlan(new CacheKey(schemaName, qry.sql()), () -> prepareSvc.prepareSingle(sqlNode, qry.planningContext()));
            } else {
                plan = prepareSvc.prepareSingle(sqlNode, qry.planningContext());
            }
            final QueryPlan finalPlan = plan;
            context.maybeUnwrap(QueryValidator.class).ifPresent(queryValidator -> queryValidator.validatePlan(finalPlan));
            cursors.add(executionSrvc.executePlan(qry, plan));
        } catch (Exception e) {
            boolean isCanceled = qry.isCancelled();
            qrys.forEach(RootQuery::cancel);
            queryRegistry.unregister(qry.id());
            if (isCanceled) {
                throw new IgniteInternalException("The query was cancelled while planning", e);
            } else {
                throw e;
            }
        }
    }
    return cursors;
}
Also used : IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) QueryPlan(org.apache.ignite.internal.sql.engine.prepare.QueryPlan) IgniteException(org.apache.ignite.lang.IgniteException) NodeStoppingException(org.apache.ignite.lang.NodeStoppingException) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) SqlNodeList(org.apache.calcite.sql.SqlNodeList) CacheKey(org.apache.ignite.internal.sql.engine.prepare.CacheKey) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

CacheKey (org.apache.ignite.internal.sql.engine.prepare.CacheKey)2 QueryPlan (org.apache.ignite.internal.sql.engine.prepare.QueryPlan)2 IgniteInternalException (org.apache.ignite.lang.IgniteInternalException)2 ArrayList (java.util.ArrayList)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1 Query (org.apache.ignite.internal.sql.engine.Query)1 RootQuery (org.apache.ignite.internal.sql.engine.RootQuery)1 RunningQuery (org.apache.ignite.internal.sql.engine.RunningQuery)1 RemoteException (org.apache.ignite.internal.sql.engine.metadata.RemoteException)1 FragmentPlan (org.apache.ignite.internal.sql.engine.prepare.FragmentPlan)1 BaseQueryContext (org.apache.ignite.internal.sql.engine.util.BaseQueryContext)1 IgniteException (org.apache.ignite.lang.IgniteException)1 IgniteInternalCheckedException (org.apache.ignite.lang.IgniteInternalCheckedException)1 NodeStoppingException (org.apache.ignite.lang.NodeStoppingException)1