Index: TaskCompiler.java =================================================================== --- TaskCompiler.java (revision 15974) +++ TaskCompiler.java (working copy) @@ -126,19 +126,29 @@ fetch.setSource(pCtx.getFetchSource()); fetch.setSink(pCtx.getFetchSink()); - pCtx.setFetchTask((FetchTask) TaskFactory.get(fetch, conf)); + //pCtx.setFetchTask((FetchTask) TaskFactory.get(fetch, conf)); // For the FetchTask, the limit optimization requires we fetch all the rows // in memory and count how many rows we get. It's not practical if the // limit factor is too big int fetchLimit = HiveConf.getIntVar(conf, HiveConf.ConfVars.HIVELIMITOPTMAXFETCH); - if (globalLimitCtx.isEnable() && globalLimitCtx.getGlobalLimit() > fetchLimit) { - LOG.info("For FetchTask, LIMIT " + globalLimitCtx.getGlobalLimit() + " > " + fetchLimit - + ". Doesn't qualify limit optimiztion."); - globalLimitCtx.disableOpt(); + if (globalLimitCtx.isEnable()) { + // For the FetchTask, the limit optimization requires we fetch all the rows + // in memory and count how many rows we get. It's not practical if the + // limit factor is too big + if (globalLimitCtx.getGlobalLimit() > fetchLimit) { + LOG.info("For FetchTask, LIMIT " + globalLimitCtx.getGlobalLimit() + " > " + fetchLimit + + ". Doesn't qualify limit optimiztion."); + globalLimitCtx.disableOpt(); + } else if (globalLimitCtx.getGlobalLimit() >= 0 + && (fetch.getLimit() == -1 || globalLimitCtx.getGlobalLimit() < fetch.getLimit())) { + LOG.info("For FetchTask, set LIMIT " + globalLimitCtx.getGlobalLimit()); + fetch.setLimit(globalLimitCtx.getGlobalLimit()); + } + } + pCtx.setFetchTask((FetchTask) TaskFactory.get(fetch, conf)); - } - if (outerQueryLimit == 0) { + if (fetch.getLimit() == 0) { // Believe it or not, some tools do generate queries with limit 0 and than expect // query to run quickly. Lets meet their requirement. LOG.info("Limit 0. No query execution needed."); @@ -274,6 +284,8 @@ if (globalLimitCtx.isEnable() && pCtx.getFetchTask() != null) { LOG.info("set least row check for FetchTask: " + globalLimitCtx.getGlobalLimit()); pCtx.getFetchTask().getWork().setLeastNumRows(globalLimitCtx.getGlobalLimit()); + LOG.info("set limit row check for LimitDesc: " + globalLimitCtx.getGlobalLimit()); + globalLimitCtx.getLastReduceLimitDesc().setLimit(globalLimitCtx.getGlobalLimit()); } if (globalLimitCtx.isEnable() && globalLimitCtx.getLastReduceLimitDesc() != null) {