diff --git a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java index 06332ac..f1db3a9 100644 --- a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java +++ b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java @@ -1602,6 +1602,8 @@ private static void populateLlapDaemonVarsSet(Set llapDaemonVarsSetLocal HIVESAMPLINGNUMBERFORORDERBY("hive.optimize.sampling.orderby.number", 1000, "Total number of samples to be obtained."), HIVESAMPLINGPERCENTFORORDERBY("hive.optimize.sampling.orderby.percent", 0.1f, new RatioValidator(), "Probability with which a row will be chosen."), + HIVE_CHECK_ORDERBY_IN_SUBQUERY("hive.check.orderby.in.subquery", true, + "If set to true, disallows sub-queries containing order by or sort by without limit"), HIVEOPTIMIZEDISTINCTREWRITE("hive.optimize.distinct.rewrite", true, "When applicable this " + "optimization rewrites distinct aggregates from a single stage to multi-stage " + "aggregation. This may not be optimal in all cases. Ideally, whether to trigger it or " diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java index d68f832..a2a683a 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.Serializable; import java.security.AccessControlException; +import java.util.AbstractMap; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -502,6 +503,31 @@ public void doPhase1QBExpr(ASTNode ast, QBExpr qbexpr, String id, String alias, qbexpr.setOpcode(QBExpr.Opcode.NULLOP); qbexpr.setQB(qb); + + if (HiveConf.getBoolVar(conf, ConfVars.HIVE_CHECK_ORDERBY_IN_SUBQUERY)) { + QBParseInfo parseInfo = qb.getParseInfo(); + HashMap> destToLimit = + parseInfo.getDestToLimit(); + Map destToOrderBy = parseInfo.getDestToOrderBy(); + Map destToSortBy = parseInfo.getDestToSortBy(); + final String errMsg = "Sub-query(" + alias + ") contains order/sort by " + + "without limit. Such order/sort by is pointless and bad for performance."; + if (destToOrderBy != null) { + for (String dest : destToOrderBy.keySet()) { + if (destToLimit == null || !destToLimit.containsKey(dest)) { + throw new SemanticException(errMsg); + } + } + } + if (destToSortBy != null) { + for (String dest : destToSortBy.keySet()) { + if (destToLimit == null || !destToLimit.containsKey(dest)) { + throw new SemanticException(errMsg); + } + } + } + } + } // setop else {