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();
}
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();
}
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);
}
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));
}
}
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);
}
Aggregations