public void execQueryOnVirtualTable( QueryContext queryContext, Session session, String query, LogicalPlan plan, SubmitQueryResponse.Builder response) throws Exception { int maxRow = Integer.MAX_VALUE; if (plan.getRootBlock().hasNode(NodeType.LIMIT)) { LimitNode limitNode = plan.getRootBlock().getNode(NodeType.LIMIT); maxRow = (int) limitNode.getFetchFirstNum(); } QueryInfo queryInfo = context .getQueryJobManager() .createNewSimpleQuery( queryContext, session, query, (LogicalRootNode) plan.getRootBlock().getRoot()); NonForwardQueryResultScanner queryResultScanner = new NonForwardQueryResultSystemScanner( context, plan, queryInfo.getQueryId(), session.getSessionId(), maxRow); queryResultScanner.init(); session.addNonForwardQueryResultScanner(queryResultScanner); response.setState(OK); response.setQueryId(queryInfo.getQueryId().getProto()); response.setResultType(ResultType.ENCLOSED); response.setMaxRowNum(maxRow); response.setTableDesc(queryResultScanner.getTableDesc().getProto()); }
public void execSetSession( Session session, LogicalPlan plan, SubmitQueryResponse.Builder response) { SetSessionNode setSessionNode = ((LogicalRootNode) plan.getRootBlock().getRoot()).getChild(); final String varName = setSessionNode.getName(); // SET CATALOG 'XXX' if (varName.equals(SessionVars.CURRENT_DATABASE.name())) { String databaseName = setSessionNode.getValue(); if (catalog.existDatabase(databaseName)) { session.selectDatabase(setSessionNode.getValue()); } else { response.setQueryId(QueryIdFactory.NULL_QUERY_ID.getProto()); response.setState(errUndefinedDatabase(databaseName)); } // others } else { if (setSessionNode.hasValue()) { session.setVariable(varName, setSessionNode.getValue()); } else { session.removeVariable(varName); } } response.setQueryId(QueryIdFactory.NULL_QUERY_ID.getProto()); response.setState(OK); }
public void execSimpleQuery( QueryContext queryContext, Session session, String query, LogicalPlan plan, SubmitQueryResponse.Builder response) throws Exception { ScanNode scanNode = plan.getRootBlock().getNode(NodeType.SCAN); TableDesc desc = scanNode.getTableDesc(); if (desc.hasPartition()) { scanNode = plan.getRootBlock().getNode(NodeType.PARTITIONS_SCAN); } int maxRow = Integer.MAX_VALUE; if (plan.getRootBlock().hasNode(NodeType.LIMIT)) { LimitNode limitNode = plan.getRootBlock().getNode(NodeType.LIMIT); maxRow = (int) limitNode.getFetchFirstNum(); } if (desc.getStats().getNumRows() == 0) { desc.getStats().setNumRows(TajoConstants.UNKNOWN_ROW_NUMBER); } QueryInfo queryInfo = context .getQueryJobManager() .createNewSimpleQuery( queryContext, session, query, (LogicalRootNode) plan.getRootBlock().getRoot()); NonForwardQueryResultScanner queryResultScanner = new NonForwardQueryResultFileScanner( context.getConf(), session.getSessionId(), queryInfo.getQueryId(), scanNode, desc, maxRow); queryResultScanner.init(); session.addNonForwardQueryResultScanner(queryResultScanner); response.setState(OK); response.setQueryId(queryInfo.getQueryId().getProto()); response.setResultType(ResultType.ENCLOSED); response.setMaxRowNum(maxRow); response.setTableDesc(desc.getProto()); }
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(); }