Does Spark support subqqueries? [duplicate]

2019-02-26 00:20发布

问题:

This question already has an answer here:

  • Does SparkSQL support subquery? 2 answers

When I am running this query i got this type of error

  select * from raw_2 where ip NOT IN (select * from raw_1);

org.apache.spark.sql.AnalysisException:

Unsupported language features in query:

 select * from raw_2 where ip NOT IN (select * from raw_1)
  TOK_QUERY 1, 0,24, 14
    TOK_FROM 1, 4,6, 14
      TOK_TABREF 1, 6,6, 14
        TOK_TABNAME 1, 6,6, 14
          raw_2 1, 6,6, 14
    TOK_INSERT 0, -1,24, 0
      TOK_DESTINATION 0, -1,-1, 0
       TOK_DIR 0, -1,-1, 0
          TOK_TMP_FILE 0, -1,-1, 0
      TOK_SELECT 0, 0,2, 0
        TOK_SELEXPR 0, 2,2, 0
          TOK_ALLCOLREF 0, 2,2, 0
      TOK_WHERE 1, 8,24, 29
       NOT 1, 10,24, 29
          TOK_SUBQUERY_EXPR 1, 14,10, 33
            TOK_SUBQUERY_OP 1, 14,14, 33
              IN 1, 14,14, 33
            TOK_QUERY 1, 16,24, 51
              TOK_FROM 1, 21,23, 51
                TOK_TABREF 1, 23,23, 51
                  TOK_TABNAME 1, 23,23, 51
                    raw_1 1, 23,23, 51
              TOK_INSERT 0, -1,19, 0
          TOK_DESTINATION 0, -1,-1, 0
            TOK_DIR 0, -1,-1, 0
              TOK_TMP_FILE 0, -1,-1, 0
          TOK_SELECT 0, 17,19, 0
            TOK_SELEXPR 0, 19,19, 0
              TOK_ALLCOLREF 0, 19,19, 0
      TOK_TABLE_OR_COL 1, 10,10, 26
        ip 1, 10,10, 26

scala.NotImplementedError: No parse rules for ASTNode type: 817, text:

  TOK_SUBQUERY_EXPR :
  TOK_SUBQUERY_EXPR 1, 14,10, 33
    TOK_SUBQUERY_OP 1, 14,14, 33
      IN 1, 14,14, 33
    TOK_QUERY 1, 16,24, 51
      TOK_FROM 1, 21,23, 51
        TOK_

回答1:

Spark 2.0.0+:

since 2.0.0 Spark supports a full range of subqueries. See Does SparkSQL support subquery? for details.

Spark < 2.0.0

Does Spark support subqqueries?

Generally speaking it does. Constructs like SELECT * FROM (SELECT * FROM foo WHERE bar = 1) as tmp perfectly valid queries in the Spark SQL.

As far as I can tell from the Catalyst parser source it doesn't support inner queries in a NOT IN clause:

| termExpression ~ (NOT ~ IN ~ "(" ~> rep1sep(termExpression, ",")) <~ ")" ^^ {
    case e1 ~ e2 => Not(In(e1, e2))
  }

It is still possible to use outer join followed by filter to obtain the same effect.