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

[6/7] hive git commit: HIVE-9113 : Explain on query failed with NPE (Navis reviewed by Szehon Ho)

HIVE-9113 : Explain on query failed with NPE (Navis reviewed by Szehon Ho)

git-svn-id: https://svn.apache.org/repos/asf/hive/trunk@1646390 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/329a3368
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/329a3368
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/329a3368

Branch: refs/heads/branch-1.0
Commit: 329a33689a0bf1d96092cf0b600617d2a51099b5
Parents: 2b9414b
Author: Navis Ryu <na...@apache.org>
Authored: Thu Dec 18 06:41:09 2014 +0000
Committer: Pengcheng Xiong <px...@apache.org>
Committed: Tue Aug 11 13:58:16 2015 -0700

----------------------------------------------------------------------
 .../apache/hadoop/hive/ql/parse/QBSubQuery.java | 30 ++++++++++++++------
 .../hadoop/hive/ql/parse/SubQueryUtils.java     | 15 +++++-----
 .../clientnegative/subquery_missing_from.q      |  1 +
 .../clientnegative/subquery_missing_from.q.out  |  3 ++
 4 files changed, 32 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/329a3368/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java
index 3c7b707..1b6b33b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java
@@ -382,7 +382,7 @@ public class QBSubQuery implements ISubQueryJoinInfo {
     /*
      * row resolver of the SubQuery.
      * Set by the SemanticAnalyzer after the Plan for the SubQuery is genned.
-     * This is neede in case the SubQuery select list contains a TOK_ALLCOLREF
+     * This is needed in case the SubQuery select list contains a TOK_ALLCOLREF
      */
     RowResolver sqRR;
 
@@ -513,7 +513,10 @@ public class QBSubQuery implements ISubQueryJoinInfo {
       String outerQueryAlias,
       Set<String> outerQryAliases) throws SemanticException {
 
-    ASTNode selectClause = (ASTNode) subQueryAST.getChild(1).getChild(1);
+    ASTNode fromClause = getChildFromSubqueryAST("From", HiveParser.TOK_FROM);
+    ASTNode insertClause = getChildFromSubqueryAST("Insert", HiveParser.TOK_INSERT);
+
+    ASTNode selectClause = (ASTNode) insertClause.getChild(1);
 
     int selectExprStart = 0;
     if ( selectClause.getChild(0).getType() == HiveParser.TOK_HINTLIST ) {
@@ -537,7 +540,7 @@ public class QBSubQuery implements ISubQueryJoinInfo {
      * Restriction 17.s :: SubQuery cannot use the same table alias as one used in
      * the Outer Query.
      */
-    List<String> sqAliases = SubQueryUtils.getTableAliasesInSubQuery(this);
+    List<String> sqAliases = SubQueryUtils.getTableAliasesInSubQuery(fromClause);
     String sharedAlias = null;
     for(String s : sqAliases ) {
       if ( outerQryAliases.contains(s) ) {
@@ -545,7 +548,7 @@ public class QBSubQuery implements ISubQueryJoinInfo {
       }
     }
     if ( sharedAlias != null) {
-      ASTNode whereClause = SubQueryUtils.subQueryWhere(subQueryAST);
+      ASTNode whereClause = SubQueryUtils.subQueryWhere(insertClause);
 
       if ( whereClause != null ) {
         ASTNode u = SubQueryUtils.hasUnQualifiedColumnReferences(whereClause);
@@ -581,7 +584,7 @@ public class QBSubQuery implements ISubQueryJoinInfo {
       containsAggregationExprs = containsAggregationExprs | ( r == 1 );
     }
 
-    rewrite(outerQueryRR, forHavingClause, outerQueryAlias);
+    rewrite(outerQueryRR, forHavingClause, outerQueryAlias, insertClause, selectClause);
 
     SubQueryUtils.setOriginDeep(subQueryAST, originalSQASTOrigin);
 
@@ -631,6 +634,16 @@ public class QBSubQuery implements ISubQueryJoinInfo {
 
   }
 
+  private ASTNode getChildFromSubqueryAST(String errorMsg, int type) throws SemanticException {
+    ASTNode childAST = (ASTNode) subQueryAST.getFirstChildWithType(type);
+    if (childAST == null && errorMsg != null) {
+      subQueryAST.setOrigin(originalSQASTOrigin);
+      throw new SemanticException(ErrorMsg.INVALID_SUBQUERY_EXPRESSION.getMsg(
+          subQueryAST, errorMsg + " clause is missing in SubQuery."));
+    }
+    return childAST;
+  }
+
   private void setJoinType() {
     if ( operator.getType() == SubQueryType.NOT_IN ||
         operator.getType() == SubQueryType.NOT_EXISTS ) {
@@ -744,7 +757,7 @@ public class QBSubQuery implements ISubQueryJoinInfo {
    *         R2.x = min(R1.y)
    *      Where R1 is an outer table reference, and R2 is a SubQuery table reference.
    *   b. When hoisting the correlation predicate to a join predicate, we need to
-   *      rewrite it to be in the form the Join code allows: so the predicte needs
+   *      rewrite it to be in the form the Join code allows: so the predict needs
    *      to contain a qualified column references.
    *      We handle this by generating a new name for the aggregation expression,
    *      like R1._gby_sq_col_1 and adding this mapping to the Outer Query's
@@ -753,9 +766,8 @@ public class QBSubQuery implements ISubQueryJoinInfo {
    */
   private void rewrite(RowResolver parentQueryRR,
       boolean forHavingClause,
-      String outerQueryAlias) throws SemanticException {
-    ASTNode selectClause = (ASTNode) subQueryAST.getChild(1).getChild(1);
-    ASTNode whereClause = SubQueryUtils.subQueryWhere(subQueryAST);
+      String outerQueryAlias, ASTNode insertClause, ASTNode selectClause) throws SemanticException {
+    ASTNode whereClause = SubQueryUtils.subQueryWhere(insertClause);
 
     if ( whereClause == null ) {
       return;

http://git-wip-us.apache.org/repos/asf/hive/blob/329a3368/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java
index 089ad78..57868b7 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java
@@ -43,7 +43,7 @@ public class SubQueryUtils {
   }
 
   /*
-   * Remove the SubQuery from the Where CLause Tree.
+   * Remove the SubQuery from the Where Clause Tree.
    * return the remaining WhereClause.
    */
   static ASTNode rewriteParentQueryWhere(ASTNode whereCond, ASTNode subQuery)
@@ -271,10 +271,9 @@ public class SubQueryUtils {
     return r;
   }
 
-  static List<String> getTableAliasesInSubQuery(QBSubQuery sq) {
+  static List<String> getTableAliasesInSubQuery(ASTNode fromClause) {
     List<String> aliases = new ArrayList<String>();
-    ASTNode joinAST = (ASTNode) sq.getSubQueryAST().getChild(0);
-    getTableAliasesInSubQuery((ASTNode) joinAST.getChild(0), aliases);
+    getTableAliasesInSubQuery((ASTNode) fromClause.getChild(0), aliases);
     return aliases;
   }
 
@@ -318,10 +317,10 @@ public class SubQueryUtils {
     return null;
   }
   
-  static ASTNode subQueryWhere(ASTNode subQueryAST) {
-    if ( subQueryAST.getChild(1).getChildCount() > 2 &&
-        subQueryAST.getChild(1).getChild(2).getType() == HiveParser.TOK_WHERE ) {
-      return (ASTNode) subQueryAST.getChild(1).getChild(2);
+  static ASTNode subQueryWhere(ASTNode insertClause) {
+    if (insertClause.getChildCount() > 2 &&
+        insertClause.getChild(2).getType() == HiveParser.TOK_WHERE ) {
+      return (ASTNode) insertClause.getChild(2);
     }
     return null;
   }

http://git-wip-us.apache.org/repos/asf/hive/blob/329a3368/ql/src/test/queries/clientnegative/subquery_missing_from.q
----------------------------------------------------------------------
diff --git a/ql/src/test/queries/clientnegative/subquery_missing_from.q b/ql/src/test/queries/clientnegative/subquery_missing_from.q
new file mode 100644
index 0000000..3b49ac6
--- /dev/null
+++ b/ql/src/test/queries/clientnegative/subquery_missing_from.q
@@ -0,0 +1 @@
+select * from src where src.key in (select key);
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/hive/blob/329a3368/ql/src/test/results/clientnegative/subquery_missing_from.q.out
----------------------------------------------------------------------
diff --git a/ql/src/test/results/clientnegative/subquery_missing_from.q.out b/ql/src/test/results/clientnegative/subquery_missing_from.q.out
new file mode 100644
index 0000000..eaf7735
--- /dev/null
+++ b/ql/src/test/results/clientnegative/subquery_missing_from.q.out
@@ -0,0 +1,3 @@
+FAILED: SemanticException Line 0:-1 Invalid SubQuery expression 'key' in definition of SubQuery sq_1 [
+src.key in (select key)
+] used as sq_1 at Line 1:32: From clause is missing in SubQuery.