@Override public Response run(JerseyResourceDelegateContext context) { JerseyResourceDelegateContextKey<String> sessionIdKey = JerseyResourceDelegateContextKey.valueOf(sessionIdKeyName, String.class); String sessionId = context.get(sessionIdKey); JerseyResourceDelegateContextKey<SubmitQueryRequest> submitQueryRequestKey = JerseyResourceDelegateContextKey.valueOf( submitQueryRequestKeyName, SubmitQueryRequest.class); SubmitQueryRequest request = context.get(submitQueryRequestKey); JerseyResourceDelegateContextKey<MasterContext> masterContextKey = JerseyResourceDelegateContextKey.valueOf( JerseyResourceDelegateUtil.MasterContextKey, MasterContext.class); MasterContext masterContext = context.get(masterContextKey); if (sessionId == null || sessionId.isEmpty()) { return ResourcesUtil.createBadRequestResponse(LOG, "Session Id is null or empty string."); } if (request == null || request.getQuery() == null || request.getQuery().isEmpty()) { return ResourcesUtil.createBadRequestResponse(LOG, "query is null or emptry string."); } Session session; try { session = masterContext.getSessionManager().getSession(sessionId); } catch (InvalidSessionException e) { return ResourcesUtil.createBadRequestResponse( LOG, "Provided session id (" + sessionId + ") is invalid."); } SubmitQueryResponse response = masterContext.getGlobalEngine().executeQuery(session, request.getQuery(), false); if (ReturnStateUtil.isError(response.getState())) { return ResourcesUtil.createExceptionResponse(LOG, response.getState().getMessage()); } else { JerseyResourceDelegateContextKey<UriInfo> uriInfoKey = JerseyResourceDelegateContextKey.valueOf( JerseyResourceDelegateUtil.UriInfoKey, UriInfo.class); UriInfo uriInfo = context.get(uriInfoKey); QueryId queryId = new QueryId(response.getQueryId()); URI queryURI = uriInfo .getBaseUriBuilder() .path(QueryResource.class) .path(QueryResource.class, "getQuery") .build(queryId.toString()); GetSubmitQueryResponse queryResponse = new GetSubmitQueryResponse(); if (queryId.isNull() == false) { queryResponse.setUri(queryURI); } queryResponse.setResultCode(response.getState().getReturnCode()); queryResponse.setQuery(request.getQuery()); return Response.status(Status.OK).entity(queryResponse).build(); } }
public SubmitQueryResponse execute( QueryContext queryContext, Session session, String sql, String jsonExpr, LogicalPlan plan) throws Exception { SubmitQueryResponse.Builder response = SubmitQueryResponse.newBuilder(); response.setUserName(queryContext.get(SessionVars.USERNAME)); LogicalRootNode rootNode = plan.getRootBlock().getRoot(); if (PlannerUtil.checkIfSetSession(rootNode)) { execSetSession(session, plan, response); } else if (PlannerUtil.checkIfDDLPlan(rootNode)) { if (PlannerUtil.isDistExecDDL(rootNode)) { if (rootNode.getChild().getType() == NodeType.CREATE_INDEX) { checkIndexExistence(queryContext, (CreateIndexNode) rootNode.getChild()); } executeDistributedQuery(queryContext, session, plan, sql, jsonExpr, response); } else { ddlExecutor.execute(queryContext, plan); response.setState(OK); response.setResultType(ResultType.NO_RESULT); } } else if (plan.isExplain()) { // explain query execExplain(session, sql, plan, queryContext, plan.isExplainGlobal(), response); } else if (PlannerUtil.checkIfQueryTargetIsVirtualTable(plan)) { execQueryOnVirtualTable(queryContext, session, sql, plan, response); // Simple query indicates a form of 'select * from tb_name [LIMIT X];'. } else if (PlannerUtil.checkIfSimpleQuery(plan)) { execSimpleQuery(queryContext, session, sql, plan, response); // NonFromQuery indicates a form of 'select a, x+y;' } else if (PlannerUtil.checkIfNonFromQuery(plan)) { execNonFromQuery(queryContext, session, sql, plan, response); } else { // it requires distributed execution. So, the query is forwarded to a query master. executeDistributedQuery(queryContext, session, plan, sql, jsonExpr, response); } response.setSessionVars(ProtoUtil.convertFromMap(session.getAllVariables())); return response.build(); }