You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by su...@apache.org on 2015/04/09 23:58:18 UTC

svn commit: r1672503 - /hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java

Author: sunchao
Date: Thu Apr  9 21:58:18 2015
New Revision: 1672503

URL: http://svn.apache.org/r1672503
Log:
HIVE-10160 - Give a warning when grouping or ordering by a constant column (Yongzhi via Chao)

Modified:
    hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java?rev=1672503&r1=1672502&r2=1672503&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java Thu Apr  9 21:58:18 2015
@@ -11013,9 +11013,10 @@ public class SemanticAnalyzer extends Ba
 
   // Process the position alias in GROUPBY and ORDERBY
   private void processPositionAlias(ASTNode ast) throws SemanticException {
+    boolean isByPos = false;
     if (HiveConf.getBoolVar(conf,
-          HiveConf.ConfVars.HIVE_GROUPBY_ORDERBY_POSITION_ALIAS) == false) {
-      return;
+          HiveConf.ConfVars.HIVE_GROUPBY_ORDERBY_POSITION_ALIAS) == true) {
+      isByPos = true;
     }
 
     if (ast.getChildCount()  == 0) {
@@ -11049,15 +11050,20 @@ public class SemanticAnalyzer extends Ba
         for (int child_pos = 0; child_pos < groupbyNode.getChildCount(); ++child_pos) {
           ASTNode node = (ASTNode) groupbyNode.getChild(child_pos);
           if (node.getToken().getType() == HiveParser.Number) {
-            int pos = Integer.parseInt(node.getText());
-            if (pos > 0 && pos <= selectExpCnt) {
-              groupbyNode.setChild(child_pos,
-                selectNode.getChild(pos - 1).getChild(0));
+            if (isByPos) {
+              int pos = Integer.parseInt(node.getText());
+              if (pos > 0 && pos <= selectExpCnt) {
+                groupbyNode.setChild(child_pos,
+                  selectNode.getChild(pos - 1).getChild(0));
+              } else {
+                throw new SemanticException(
+                  ErrorMsg.INVALID_POSITION_ALIAS_IN_GROUPBY.getMsg(
+                  "Position alias: " + pos + " does not exist\n" +
+                  "The Select List is indexed from 1 to " + selectExpCnt));
+              }
             } else {
-              throw new SemanticException(
-                ErrorMsg.INVALID_POSITION_ALIAS_IN_GROUPBY.getMsg(
-                "Position alias: " + pos + " does not exist\n" +
-                "The Select List is indexed from 1 to " + selectExpCnt));
+              warn("Using constant number  " + node.getText() +
+                " in group by. If you try to use position alias when hive.groupby.orderby.position.alias is false, the position alias will be ignored.");
             }
           }
         }
@@ -11076,19 +11082,24 @@ public class SemanticAnalyzer extends Ba
           ASTNode colNode = (ASTNode) orderbyNode.getChild(child_pos);
           ASTNode node = (ASTNode) colNode.getChild(0);
           if (node.getToken().getType() == HiveParser.Number) {
-            if (!isAllCol) {
-              int pos = Integer.parseInt(node.getText());
-              if (pos > 0 && pos <= selectExpCnt) {
-                colNode.setChild(0, selectNode.getChild(pos - 1).getChild(0));
+            if( isByPos ) {
+              if (!isAllCol) {
+                int pos = Integer.parseInt(node.getText());
+                if (pos > 0 && pos <= selectExpCnt) {
+                  colNode.setChild(0, selectNode.getChild(pos - 1).getChild(0));
+                } else {
+                  throw new SemanticException(
+                    ErrorMsg.INVALID_POSITION_ALIAS_IN_ORDERBY.getMsg(
+                    "Position alias: " + pos + " does not exist\n" +
+                    "The Select List is indexed from 1 to " + selectExpCnt));
+                }
               } else {
                 throw new SemanticException(
-                  ErrorMsg.INVALID_POSITION_ALIAS_IN_ORDERBY.getMsg(
-                  "Position alias: " + pos + " does not exist\n" +
-                  "The Select List is indexed from 1 to " + selectExpCnt));
+                  ErrorMsg.NO_SUPPORTED_ORDERBY_ALLCOLREF_POS.getMsg());
               }
-            } else {
-              throw new SemanticException(
-                ErrorMsg.NO_SUPPORTED_ORDERBY_ALLCOLREF_POS.getMsg());
+            } else { //if not using position alias and it is a number.
+              warn("Using constant number " + node.getText() +
+                " in order by. If you try to use position alias when hive.groupby.orderby.position.alias is false, the position alias will be ignored.");
             }
           }
         }
@@ -12089,4 +12100,8 @@ public class SemanticAnalyzer extends Ba
       queryProperties.setOuterQueryLimit(qb.getParseInfo().getOuterQueryLimit());
     }
   }
+  private void warn(String msg) {
+    SessionState.getConsole().printInfo(
+        String.format("Warning: %s", msg));
+  }
 }