Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-17154

Wrong result can be returned or AnalysisException can be thrown after self-join or similar operations

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Incomplete
    • 1.6.2, 2.0.0
    • None
    • SQL

    Description

      When we join two DataFrames which are originated from a same DataFrame, operations to the joined DataFrame can fail.

      One reproducible example is as follows.

      val df = Seq(
        (1, "a", "A"),
        (2, "b", "B"),
        (3, "c", "C"),
        (4, "d", "D"),
        (5, "e", "E")).toDF("col1", "col2", "col3")
        val filtered = df.filter("col1 != 3").select("col1", "col2")
        val joined = filtered.join(df, filtered("col1") === df("col1"), "inner")
        val selected1 = joined.select(df("col3"))
      

      In this case, AnalysisException is thrown.

      Another example is as follows.

      val df = Seq(
        (1, "a", "A"),
        (2, "b", "B"),
        (3, "c", "C"),
        (4, "d", "D"),
        (5, "e", "E")).toDF("col1", "col2", "col3")
        val filtered = df.filter("col1 != 3").select("col1", "col2")
        val rightOuterJoined = filtered.join(df, filtered("col1") === df("col1"), "right")
        val selected2 = rightOuterJoined.select(df("col1"))
        selected2.show
      

      In this case, we will expect to get the answer like as follows.

      1
      2
      3
      4
      5
      

      But the actual result is as follows.

      1
      2
      null
      4
      5
      

      The cause of the problems in the examples is that the logical plan related to the right side DataFrame and the expressions of its output are re-created in the analyzer (at ResolveReference rule) when a DataFrame has expressions which have a same exprId each other.
      Re-created expressions are equally to the original ones except exprId.
      This will happen when we do self-join or similar pattern operations.

      In the first example, df("col3") returns a Column which includes an expression and the expression have an exprId (say id1 here).
      After join, the expresion which the right side DataFrame (df) has is re-created and the old and new expressions are equally but exprId is renewed (say id2 for the new exprId here).
      Because of the mismatch of those exprIds, AnalysisException is thrown.

      In the second example, df("col1") returns a column and the expression contained in the column is assigned an exprId (say id3).
      On the other hand, a column returned by filtered("col1") has an expression which has the same exprId (id3).
      After join, the expressions in the right side DataFrame are re-created and the expression assigned id3 is no longer present in the right side but present in the left side.
      So, referring df("col1") to the joined DataFrame, we get col1 of right side which includes null.

      Attachments

        1. Solution_Proposal_SPARK-17154.pdf
          367 kB
          Kousuke Saruta
        2. Name-conflicts-2.pdf
          538 kB
          Nattavut Sutyanyong

        Issue Links

          Activity

            People

              Unassigned Unassigned
              sarutak Kousuke Saruta
              Votes:
              4 Vote for this issue
              Watchers:
              11 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: