QueryCommand extends MultiClusterCommandBase { public void run() throws OdpsException, ODPSConsoleException { if (isSelectCommand && getContext().isAsyncMode()) { getWriter().writeError("[async mode]: can't support select command."); return; } ExecutionContext context = getContext(); if (context.isInteractiveMode()) { if (!confirmSQLInput()) { return; } } boolean isDryRun = getContext().isDryRun(); DefaultOutputWriter writer = context.getOutputWriter(); int retryTime = isSelectCommand ? 1 : context.getRetryTimes(); retryTime = retryTime > 0 ? retryTime : 1; while (retryTime > 0) { Task task = null; try { if (isDryRun) { taskName = "console_sqlplan_task_" + Calendar.getInstance().getTimeInMillis(); task = new SqlPlanTask(taskName, getCommandText()); } else { taskName = "console_query_task_" + Calendar.getInstance().getTimeInMillis(); task = new SQLTask(); task.setName(taskName); ((SQLTask) task).setQuery(getCommandText()); } HashMap<String, String> taskConfig = QueryUtil.getTaskConfig(); if (!getContext().isMachineReadable()) { Map<String, String> settings = new HashMap<String, String>(); settings.put("odps.sql.select.output.format", "HumanReadable"); addSetting(taskConfig, settings); } for (Entry<String, String> property : taskConfig.entrySet()) { task.setProperty(property.getKey(), property.getValue()); } runJob(task); break; } catch (UserInterruptException e) { throw e; } catch (Exception e) { if (task instanceof SQLTask && QueryUtil.isOperatorDisabled(((SQLTask) task).getQuery())) { String errorMessage = e.getMessage(); if (errorMessage.indexOf("ODPS-0110999") != -1) { throw new ODPSConsoleException(e.getMessage()); } else if (errorMessage.indexOf("ODPS-") == -1) { throw new ODPSConsoleException("ODPS-0110999:" + e.getMessage()); } } retryTime--; if (retryTime == 0) { throw new ODPSConsoleException(e.getMessage(), e); } writer.writeError("retry " + retryTime); writer.writeDebug(StringUtils.stringifyException(e)); } } if (!isSelectCommand) { writer.writeError("OK"); } } QueryCommand(boolean isSelect, String commandText, ExecutionContext context); void run(); String getTaskName(); String getInstanceId(); boolean isSelectCommand(); static QueryCommand parse(String commandString, ExecutionContext sessionContext);  }
@Test public void testRun() throws ODPSConsoleException, OdpsException { ExecutionContext context = ExecutionContext.init(); MockQueryCommand command = new MockQueryCommand(true, selectCommandStrings[0], context); command.run(); command = new MockQueryCommand(false, queryCommandStrings[0], context); command.run(); }