GitHub user maropu opened a pull request:

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

    [SPARK-19980][SQL] Add NULL checks in Bean serializer

    ## What changes were proposed in this pull request?
    A Bean serializer in `ExpressionEncoder`  could change values when Beans 
having NULL. A concrete example is as follows;
    ```
    scala> :paste
    class Outer extends Serializable {
      private var cls: Inner = _
      def setCls(c: Inner): Unit = cls = c
      def getCls(): Inner = cls
    }
    
    class Inner extends Serializable {
      private var str: String = _
      def setStr(s: String): Unit = str = str
      def getStr(): String = str
    }
    
    scala> Seq("""{"cls":null}""", """{"cls": 
{"str":null}}""").toDF().write.text("data")
    scala> val encoder = Encoders.bean(classOf[Outer])
    scala> val schema = encoder.schema
    scala> val df = spark.read.schema(schema).json("data").as[Outer](encoder)
    scala> df.show
    +------+
    |   cls|
    +------+
    |[null]|
    |  null|
    +------+
    
    scala> df.map(x => x)(encoder).show()
    +------+
    |   cls|
    +------+
    |[null]|
    |[null]|     // <-- Value changed
    +------+
    ```
    
    This is because the Bean serializer does not have the NULL-check 
expressions that the serializer of Scala's product types has. Actually, this 
value change does not happen in Scala's product types; 
    
    ```
    scala> :paste
    case class Outer(cls: Inner)
    case class Inner(str: String)
    
    scala> val encoder = Encoders.product[Outer]
    scala> val schema = encoder.schema
    scala> val df = spark.read.schema(schema).json("data").as[Outer](encoder)
    scala> df.show
    +------+
    |   cls|
    +------+
    |[null]|
    |  null|
    +------+
    
    scala> df.map(x => x)(encoder).show()
    +------+
    |   cls|
    +------+
    |[null]|
    |  null|
    +------+
    ```
    
    This pr added the NULL-check expressions in Bean serializer along with the 
serializer of Scala's product types.
    
    ## How was this patch tested?
    Added tests in `JavaDatasetSuite`.


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

    $ git pull https://github.com/maropu/spark SPARK-19980

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

    https://github.com/apache/spark/pull/17347.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 #17347
    
----
commit 093b4e3baecf8a92b9e1218ae1b39b76120a826b
Author: Takeshi Yamamuro <yamam...@apache.org>
Date:   2017-03-19T11:03:01Z

    Add null checks in Bean serializer

----


---
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