Search in sources :

Example 1 with RunQuery

use of org.apache.drill.exec.proto.UserProtos.RunQuery in project drill by apache.

the class DrillClient method runQuery.

/**
   * Submits a string based query plan for execution and returns the result batches. Supported query types are:
   * <p><ul>
   *  <li>{@link QueryType#LOGICAL}
   *  <li>{@link QueryType#PHYSICAL}
   *  <li>{@link QueryType#SQL}
   * </ul>
   *
   * @param type Query type
   * @param plan Query to execute
   * @return a handle for the query result
   * @throws RpcException
   */
public List<QueryDataBatch> runQuery(QueryType type, String plan) throws RpcException {
    checkArgument(type == QueryType.LOGICAL || type == QueryType.PHYSICAL || type == QueryType.SQL, String.format("Only query types %s, %s and %s are supported in this API", QueryType.LOGICAL, QueryType.PHYSICAL, QueryType.SQL));
    final UserProtos.RunQuery query = newBuilder().setResultsMode(STREAM_FULL).setType(type).setPlan(plan).build();
    final ListHoldingResultsListener listener = new ListHoldingResultsListener(query);
    client.submitQuery(listener, query);
    return listener.getResults();
}
Also used : RunQuery(org.apache.drill.exec.proto.UserProtos.RunQuery) UserProtos(org.apache.drill.exec.proto.UserProtos)

Example 2 with RunQuery

use of org.apache.drill.exec.proto.UserProtos.RunQuery in project drill by apache.

the class DrillClient method executePreparedStatement.

/**
   * Execute the given prepared statement and return the results.
   *
   * @param preparedStatementHandle Prepared statement handle returned in response to
   *                                {@link #createPreparedStatement(String)}.
   * @return List of {@link QueryDataBatch}s. It is responsibility of the caller to release query data batches.
   * @throws RpcException
   */
@VisibleForTesting
public List<QueryDataBatch> executePreparedStatement(final PreparedStatementHandle preparedStatementHandle) throws RpcException {
    final RunQuery runQuery = newBuilder().setResultsMode(STREAM_FULL).setType(QueryType.PREPARED_STATEMENT).setPreparedStatementHandle(preparedStatementHandle).build();
    final ListHoldingResultsListener resultsListener = new ListHoldingResultsListener(runQuery);
    client.submitQuery(resultsListener, runQuery);
    return resultsListener.getResults();
}
Also used : RunQuery(org.apache.drill.exec.proto.UserProtos.RunQuery) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with RunQuery

use of org.apache.drill.exec.proto.UserProtos.RunQuery in project drill by apache.

the class QueryWrapper method run.

public QueryResult run(final WorkManager workManager, final WebUserConnection webUserConnection) throws Exception {
    final RunQuery runQuery = RunQuery.newBuilder().setType(getType()).setPlan(getQuery()).setResultsMode(QueryResultsMode.STREAM_FULL).build();
    // Submit user query to Drillbit work queue.
    final QueryId queryId = workManager.getUserWorker().submitWork(webUserConnection, runQuery);
    // Wait until the query execution is complete or there is error submitting the query
    webUserConnection.await();
    if (logger.isTraceEnabled()) {
        logger.trace("Query {} is completed ", queryId);
    }
    if (webUserConnection.results.isEmpty()) {
        webUserConnection.results.add(Maps.<String, String>newHashMap());
    }
    // Return the QueryResult.
    return new QueryResult(webUserConnection.columns, webUserConnection.results);
}
Also used : RunQuery(org.apache.drill.exec.proto.UserProtos.RunQuery) QueryId(org.apache.drill.exec.proto.UserBitShared.QueryId)

Example 4 with RunQuery

use of org.apache.drill.exec.proto.UserProtos.RunQuery in project drill by apache.

the class UserServerRequestHandler method handle.

@Override
public void handle(BitToUserConnection connection, int rpcType, ByteBuf pBody, ByteBuf dBody, ResponseSender responseSender) throws RpcException {
    switch(rpcType) {
        case RpcType.RUN_QUERY_VALUE:
            logger.debug("Received query to run.  Returning query handle.");
            try {
                final RunQuery query = RunQuery.PARSER.parseFrom(new ByteBufInputStream(pBody));
                final QueryId queryId = worker.submitWork(connection, query);
                responseSender.send(new Response(RpcType.QUERY_HANDLE, queryId));
                break;
            } catch (InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding RunQuery body.", e);
            }
        case RpcType.CANCEL_QUERY_VALUE:
            try {
                final QueryId queryId = QueryId.PARSER.parseFrom(new ByteBufInputStream(pBody));
                final Ack ack = worker.cancelQuery(queryId);
                responseSender.send(new Response(RpcType.ACK, ack));
                break;
            } catch (InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding QueryId body.", e);
            }
        case RpcType.RESUME_PAUSED_QUERY_VALUE:
            try {
                final QueryId queryId = QueryId.PARSER.parseFrom(new ByteBufInputStream(pBody));
                final Ack ack = worker.resumeQuery(queryId);
                responseSender.send(new Response(RpcType.ACK, ack));
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding QueryId body.", e);
            }
        case RpcType.GET_QUERY_PLAN_FRAGMENTS_VALUE:
            try {
                final GetQueryPlanFragments req = GetQueryPlanFragments.PARSER.parseFrom(new ByteBufInputStream(pBody));
                responseSender.send(new Response(RpcType.QUERY_PLAN_FRAGMENTS, worker.getQueryPlan(connection, req)));
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetQueryPlanFragments body.", e);
            }
        case RpcType.GET_CATALOGS_VALUE:
            try {
                final GetCatalogsReq req = GetCatalogsReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitCatalogMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetCatalogsReq body.", e);
            }
        case RpcType.GET_SCHEMAS_VALUE:
            try {
                final GetSchemasReq req = GetSchemasReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitSchemasMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetSchemasReq body.", e);
            }
        case RpcType.GET_TABLES_VALUE:
            try {
                final GetTablesReq req = GetTablesReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitTablesMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetTablesReq body.", e);
            }
        case RpcType.GET_COLUMNS_VALUE:
            try {
                final GetColumnsReq req = GetColumnsReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitColumnsMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding GetColumnsReq body.", e);
            }
        case RpcType.CREATE_PREPARED_STATEMENT_VALUE:
            try {
                final CreatePreparedStatementReq req = CreatePreparedStatementReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitPreparedStatementWork(connection, req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding CreatePreparedStatementReq body.", e);
            }
        case RpcType.GET_SERVER_META_VALUE:
            try {
                final GetServerMetaReq req = GetServerMetaReq.PARSER.parseFrom(new ByteBufInputStream(pBody));
                worker.submitServerMetadataWork(connection.getSession(), req, responseSender);
                break;
            } catch (final InvalidProtocolBufferException e) {
                throw new RpcException("Failure while decoding CreatePreparedStatementReq body.", e);
            }
        default:
            throw new UnsupportedOperationException(String.format("UserServerRequestHandler received rpc of unknown type. Type was %d.", rpcType));
    }
}
Also used : RunQuery(org.apache.drill.exec.proto.UserProtos.RunQuery) QueryId(org.apache.drill.exec.proto.UserBitShared.QueryId) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Ack(org.apache.drill.exec.proto.GeneralRPCProtos.Ack) GetQueryPlanFragments(org.apache.drill.exec.proto.UserProtos.GetQueryPlanFragments) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) CreatePreparedStatementReq(org.apache.drill.exec.proto.UserProtos.CreatePreparedStatementReq) GetCatalogsReq(org.apache.drill.exec.proto.UserProtos.GetCatalogsReq) Response(org.apache.drill.exec.rpc.Response) GetSchemasReq(org.apache.drill.exec.proto.UserProtos.GetSchemasReq) GetServerMetaReq(org.apache.drill.exec.proto.UserProtos.GetServerMetaReq) RpcException(org.apache.drill.exec.rpc.RpcException) GetColumnsReq(org.apache.drill.exec.proto.UserProtos.GetColumnsReq) GetTablesReq(org.apache.drill.exec.proto.UserProtos.GetTablesReq)

Example 5 with RunQuery

use of org.apache.drill.exec.proto.UserProtos.RunQuery in project drill by apache.

the class DrillClient method runQuery.

/**
   * Run query based on list of fragments that were supposedly produced during query planning phase. Supported
   * query type is {@link QueryType#EXECUTION}
   * @param type
   * @param planFragments
   * @param resultsListener
   * @throws RpcException
   */
public void runQuery(QueryType type, List<PlanFragment> planFragments, UserResultsListener resultsListener) throws RpcException {
    // QueryType can be only executional
    checkArgument((QueryType.EXECUTION == type), "Only EXECUTION type query is supported with PlanFragments");
    // setting Plan on RunQuery will be used for logging purposes and therefore can not be null
    // since there is no Plan string provided we will create a JsonArray out of individual fragment Plans
    ArrayNode jsonArray = objectMapper.createArrayNode();
    for (PlanFragment fragment : planFragments) {
        try {
            jsonArray.add(objectMapper.readTree(fragment.getFragmentJson()));
        } catch (IOException e) {
            logger.error("Exception while trying to read PlanFragment JSON for %s", fragment.getHandle().getQueryId(), e);
            throw new RpcException(e);
        }
    }
    final String fragmentsToJsonString;
    try {
        fragmentsToJsonString = objectMapper.writeValueAsString(jsonArray);
    } catch (JsonProcessingException e) {
        logger.error("Exception while trying to get JSONString from Array of individual Fragments Json for %s", e);
        throw new RpcException(e);
    }
    final UserProtos.RunQuery query = newBuilder().setType(type).addAllFragments(planFragments).setPlan(fragmentsToJsonString).setResultsMode(STREAM_FULL).build();
    client.submitQuery(resultsListener, query);
}
Also used : RunQuery(org.apache.drill.exec.proto.UserProtos.RunQuery) RpcException(org.apache.drill.exec.rpc.RpcException) NonTransientRpcException(org.apache.drill.exec.rpc.NonTransientRpcException) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UserProtos(org.apache.drill.exec.proto.UserProtos) PlanFragment(org.apache.drill.exec.proto.BitControl.PlanFragment)

Aggregations

RunQuery (org.apache.drill.exec.proto.UserProtos.RunQuery)6 QueryId (org.apache.drill.exec.proto.UserBitShared.QueryId)2 UserProtos (org.apache.drill.exec.proto.UserProtos)2 RpcException (org.apache.drill.exec.rpc.RpcException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 IOException (java.io.IOException)1 PlanFragment (org.apache.drill.exec.proto.BitControl.PlanFragment)1 Ack (org.apache.drill.exec.proto.GeneralRPCProtos.Ack)1 CreatePreparedStatementReq (org.apache.drill.exec.proto.UserProtos.CreatePreparedStatementReq)1 GetCatalogsReq (org.apache.drill.exec.proto.UserProtos.GetCatalogsReq)1 GetColumnsReq (org.apache.drill.exec.proto.UserProtos.GetColumnsReq)1 GetQueryPlanFragments (org.apache.drill.exec.proto.UserProtos.GetQueryPlanFragments)1 GetSchemasReq (org.apache.drill.exec.proto.UserProtos.GetSchemasReq)1 GetServerMetaReq (org.apache.drill.exec.proto.UserProtos.GetServerMetaReq)1 GetTablesReq (org.apache.drill.exec.proto.UserProtos.GetTablesReq)1 NonTransientRpcException (org.apache.drill.exec.rpc.NonTransientRpcException)1