FAQ
Author: navis
Date: Thu Dec 18 06:41:09 2014
New Revision: 1646390

URL: http://svn.apache.org/r1646390
Log:
HIVE-9113 : Explain on query failed with NPE (Navis reviewed by Szehon Ho)

Added:
     hive/trunk/ql/src/test/queries/clientnegative/subquery_missing_from.q
     hive/trunk/ql/src/test/results/clientnegative/subquery_missing_from.q.out
Modified:
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java?rev=1646390&r1=1646389&r2=1646390&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/QBSubQuery.java Thu Dec 18 06:41:09 2014
@@ -382,7 +382,7 @@ public class QBSubQuery implements ISubQ
      /*
       * 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 ISubQ
        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 ISubQ
       * 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 ISubQ
        }
      }
      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 ISubQ
        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 ISubQ

    }

+ 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 ISubQ
     * 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 ISubQ
     */
    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;

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java?rev=1646390&r1=1646389&r2=1646390&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/parse/SubQueryUtils.java Thu Dec 18 06:41:09 2014
@@ -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;
    }

Added: hive/trunk/ql/src/test/queries/clientnegative/subquery_missing_from.q
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/queries/clientnegative/subquery_missing_from.q?rev=1646390&view=auto
==============================================================================
--- hive/trunk/ql/src/test/queries/clientnegative/subquery_missing_from.q (added)
+++ hive/trunk/ql/src/test/queries/clientnegative/subquery_missing_from.q Thu Dec 18 06:41:09 2014
@@ -0,0 +1 @@
+select * from src where src.key in (select key);
\ No newline at end of file

Added: hive/trunk/ql/src/test/results/clientnegative/subquery_missing_from.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientnegative/subquery_missing_from.q.out?rev=1646390&view=auto
==============================================================================
--- hive/trunk/ql/src/test/results/clientnegative/subquery_missing_from.q.out (added)
+++ hive/trunk/ql/src/test/results/clientnegative/subquery_missing_from.q.out Thu Dec 18 06:41:09 2014
@@ -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.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcommits @
categorieshive, hadoop
postedDec 18, '14 at 6:41a
activeDec 18, '14 at 6:41a
posts1
users1
websitehive.apache.org

1 user in discussion

Navis: 1 post

People

Translate

site design / logo © 2021 Grokbase