GitHub user gatorsmile opened a pull request:

    https://github.com/apache/spark/pull/15781

    [SPARK-17981] [SPARK-17957] [SQL] [BACKPORT-2.0] Fix Incorrect Nullability 
Setting to False in FilterExec

    ### What changes were proposed in this pull request?
    
    **This PR is to backport the fix https://github.com/apache/spark/pull/15523 
to 2.0.**
    
    When `FilterExec` contains `isNotNull`, which could be inferred and pushed 
down or users specified, we convert the nullability of the involved columns if 
the top-layer expression is null-intolerant. However, this is not correct, if 
the top-layer expression is not a leaf expression, it could still tolerate the 
null when it has null-tolerant child expressions. 
    
    For example, `cast(coalesce(a#5, a#15) as double)`. Although `cast` is a 
null-intolerant expression, but obviously`coalesce` is null-tolerant. Thus, it 
could eat null.
    
    When the nullability is wrong, we could generate incorrect results in 
different cases. For example, 
    
    ``` Scala
        val df1 = Seq((1, 2), (2, 3)).toDF("a", "b")
        val df2 = Seq((2, 5), (3, 4)).toDF("a", "c")
        val joinedDf = df1.join(df2, Seq("a"), "outer").na.fill(0)
        val df3 = Seq((3, 1)).toDF("a", "d")
        joinedDf.join(df3, "a").show
    ```
    
    The optimized plan is like 
    
    ```
    Project [a#29, b#30, c#31, d#42]
    +- Join Inner, (a#29 = a#41)
       :- Project [cast(coalesce(cast(coalesce(a#5, a#15) as double), 0.0) as 
int) AS a#29, cast(coalesce(cast(b#6 as double), 0.0) as int) AS b#30, 
cast(coalesce(cast(c#16 as double), 0.0) as int) AS c#31]
       :  +- Filter isnotnull(cast(coalesce(cast(coalesce(a#5, a#15) as 
double), 0.0) as int))
       :     +- Join FullOuter, (a#5 = a#15)
       :        :- LocalRelation [a#5, b#6]
       :        +- LocalRelation [a#15, c#16]
       +- LocalRelation [a#41, d#42]
    ```
    
    Without the fix, it returns an empty result. With the fix, it can return a 
correct answer:
    
    ```
    +---+---+---+---+
    |  a|  b|  c|  d|
    +---+---+---+---+
    |  3|  0|  4|  1|
    +---+---+---+---+
    ```
    ### How was this patch tested?
    
    Added test cases to verify the nullability changes in FilterExec. Also 
added a test case for verifying the reported incorrect result.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/gatorsmile/spark nullabilityFix

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/15781.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #15781
    
----
commit f2254619c96b95156c0cb79ef8f70330cab7ba1d
Author: gatorsmile <gatorsm...@gmail.com>
Date:   2016-11-05T05:50:27Z

    fix.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to