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..b34704e7265c6b1b96bfd0f4d387a076d6470774 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)); + LOG.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. + LOG.warn("Using position alias: " + node.getText() + + " in order by when hive.groupby.orderby.position.alias is false. The position alias will be ignored."); } } } diff --git a/ql/src/test/queries/clientnegative/groupby_positionofferror.q b/ql/src/test/queries/clientnegative/groupby_positionofferror.q new file mode 100644 index 0000000000000000000000000000000000000000..03d83a5f513a301ad80ecc1c5ac07914bc1d76c8 --- /dev/null +++ b/ql/src/test/queries/clientnegative/groupby_positionofferror.q @@ -0,0 +1,3 @@ +set hive.groupby.orderby.position.alias=false; +select key, count(*) from src group by 1; + 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/clientnegative/groupby_positionofferror.q.out b/ql/src/test/results/clientnegative/groupby_positionofferror.q.out new file mode 100644 index 0000000000000000000000000000000000000000..7b593c3e67d12fb479aedafaa1a254346c7b1875 --- /dev/null +++ b/ql/src/test/results/clientnegative/groupby_positionofferror.q.out @@ -0,0 +1 @@ +FAILED: SemanticException [Error 10025]: Line 2:7 Expression not in GROUP BY key 'key' 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..84f94210b2f8d243b7c045bff087c1e7f152e133 --- /dev/null +++ b/ql/src/test/results/clientpositive/groupby_positionoff.q.out @@ -0,0 +1,103 @@ +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), ] +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