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 1f6d53d2425efd9713278d2277e1761afe816580..d740ed04b9fd906708f670eaf50cbf27869caf8d 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 @@ -11011,9 +11011,10 @@ private void validateCreateView(CreateViewDesc createVwDesc) // 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) { @@ -11047,15 +11048,20 @@ private void processPositionAlias(ASTNode ast) throws SemanticException { 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 position alias: " + node.getText() + + " in group by when hive.groupby.orderby.position.alias is false. The position alias will be ignored."); } } } @@ -11074,19 +11080,24 @@ private void processPositionAlias(ASTNode ast) throws SemanticException { 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 position alias: " + node.getText() + + " in order by when hive.groupby.orderby.position.alias is false. The position alias will be ignored."); } } } @@ -12087,4 +12098,8 @@ private void copyInfoToQueryProperties(QueryProperties queryProperties) { queryProperties.setOuterQueryLimit(qb.getParseInfo().getOuterQueryLimit()); } } + private void warn(String msg) { + SessionState.getConsole().printWarn( + String.format("Warning: %s", msg)); + } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index 66cff87d118ee22f2e8308b0b5c70f87d99d5625..412cd0b0c923362768488afd66e106a10d6108f1 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -949,6 +949,15 @@ public void printInfo(String info, String detail) { LOG.info(info + StringUtils.defaultString(detail)); } + public void printWarn(String warn) { + printWarn(warn, null); + } + + public void printWarn(String warn, String detail) { + getInfoStream().println(warn); + LOG.warn(warn + StringUtils.defaultString(detail)); + } + public void printError(String error) { printError(error, null); } diff --git a/ql/src/test/queries/clientpositive/groupby_positionoff.q b/ql/src/test/queries/clientpositive/groupby_positionoff.q new file mode 100644 index 0000000000000000000000000000000000000000..520d2b0da656562ac01e38f0bee364101dd78fa1 --- /dev/null +++ b/ql/src/test/queries/clientpositive/groupby_positionoff.q @@ -0,0 +1,10 @@ +set hive.groupby.orderby.position.alias=false; +create table testpos (index1 int, value string); +insert overwrite table testpos select key, value from src where key < 20; +select index1, value from testpos order by 1, 2 desc; +set hive.groupby.orderby.position.alias=true; +select index1, value from testpos order by 1, 2 desc; +select index1, count(*) from testpos group by 1; +drop table testpos; + + diff --git a/ql/src/test/results/clientpositive/groupby_positionoff.q.out b/ql/src/test/results/clientpositive/groupby_positionoff.q.out new file mode 100644 index 0000000000000000000000000000000000000000..05c473fef48a4d071fa67912adbc4c502c960395 --- /dev/null +++ b/ql/src/test/results/clientpositive/groupby_positionoff.q.out @@ -0,0 +1,105 @@ +PREHOOK: query: create table testpos (index1 int, value string) +PREHOOK: type: CREATETABLE +PREHOOK: Output: database:default +PREHOOK: Output: default@testpos +POSTHOOK: query: create table testpos (index1 int, value string) +POSTHOOK: type: CREATETABLE +POSTHOOK: Output: database:default +POSTHOOK: Output: default@testpos +PREHOOK: query: insert overwrite table testpos select key, value from src where key < 20 +PREHOOK: type: QUERY +PREHOOK: Input: default@src +PREHOOK: Output: default@testpos +POSTHOOK: query: insert overwrite table testpos select key, value from src where key < 20 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@src +POSTHOOK: Output: default@testpos +POSTHOOK: Lineage: testpos.index1 EXPRESSION [(src)src.FieldSchema(name:key, type:string, comment:default), ] +POSTHOOK: Lineage: testpos.value SIMPLE [(src)src.FieldSchema(name:value, type:string, comment:default), ] +Warning: Using position alias: 1 in order by when hive.groupby.orderby.position.alias is false. The position alias will be ignored. +Warning: Using position alias: 2 in order by when hive.groupby.orderby.position.alias is false. The position alias will be ignored. +PREHOOK: query: select index1, value from testpos order by 1, 2 desc +PREHOOK: type: QUERY +PREHOOK: Input: default@testpos +#### A masked pattern was here #### +POSTHOOK: query: select index1, value from testpos order by 1, 2 desc +POSTHOOK: type: QUERY +POSTHOOK: Input: default@testpos +#### A masked pattern was here #### +18 val_18 +9 val_9 +18 val_18 +5 val_5 +12 val_12 +2 val_2 +5 val_5 +11 val_11 +5 val_5 +10 val_10 +19 val_19 +15 val_15 +0 val_0 +0 val_0 +8 val_8 +12 val_12 +4 val_4 +0 val_0 +17 val_17 +15 val_15 +PREHOOK: query: select index1, value from testpos order by 1, 2 desc +PREHOOK: type: QUERY +PREHOOK: Input: default@testpos +#### A masked pattern was here #### +POSTHOOK: query: select index1, value from testpos order by 1, 2 desc +POSTHOOK: type: QUERY +POSTHOOK: Input: default@testpos +#### A masked pattern was here #### +0 val_0 +0 val_0 +0 val_0 +2 val_2 +4 val_4 +5 val_5 +5 val_5 +5 val_5 +8 val_8 +9 val_9 +10 val_10 +11 val_11 +12 val_12 +12 val_12 +15 val_15 +15 val_15 +17 val_17 +18 val_18 +18 val_18 +19 val_19 +PREHOOK: query: select index1, count(*) from testpos group by 1 +PREHOOK: type: QUERY +PREHOOK: Input: default@testpos +#### A masked pattern was here #### +POSTHOOK: query: select index1, count(*) from testpos group by 1 +POSTHOOK: type: QUERY +POSTHOOK: Input: default@testpos +#### A masked pattern was here #### +0 3 +2 1 +4 1 +5 3 +8 1 +9 1 +10 1 +11 1 +12 2 +15 2 +17 1 +18 2 +19 1 +PREHOOK: query: drop table testpos +PREHOOK: type: DROPTABLE +PREHOOK: Input: default@testpos +PREHOOK: Output: default@testpos +POSTHOOK: query: drop table testpos +POSTHOOK: type: DROPTABLE +POSTHOOK: Input: default@testpos +POSTHOOK: Output: default@testpos