GitHub user ueshin opened a pull request:

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

    [SPARK-15313][SQL] EmbedSerializerInFilter rule should keep exprIds of 
output of surrounded SerializeFromObject.

    ## What changes were proposed in this pull request?
    
    The following code:
    
    ```
    val ds = Seq(("a", 1), ("b", 2), ("c", 3)).toDS()
    ds.filter(_._1 == "b").select(expr("_1").as[String]).foreach(println(_))
    ```
    
    throws an Exception:
    
    ```
    org.apache.spark.sql.catalyst.errors.package$TreeNodeException: Binding 
attribute, tree: _1#420
     at 
org.apache.spark.sql.catalyst.errors.package$.attachTree(package.scala:50)
     at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1.applyOrElse(BoundAttribute.scala:88)
     at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1.applyOrElse(BoundAttribute.scala:87)
    
    ...
     Cause: java.lang.RuntimeException: Couldn't find _1#420 in [_1#416,_2#417]
     at scala.sys.package$.error(package.scala:27)
     at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1$$anonfun$applyOrElse$1.apply(BoundAttribute.scala:94)
     at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1$$anonfun$applyOrElse$1.apply(BoundAttribute.scala:88)
     at 
org.apache.spark.sql.catalyst.errors.package$.attachTree(package.scala:49)
     at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1.applyOrElse(BoundAttribute.scala:88)
     at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1.applyOrElse(BoundAttribute.scala:87)
    ...
    ```
    
    This is because `EmbedSerializerInFilter` rule drops the `exprId`s of 
output of surrounded `SerializeFromObject`.
    
    The analyzed and optimized plans of the above example are as follows:
    
    ```
    == Analyzed Logical Plan ==
    _1: string
    Project [_1#420]
    +- SerializeFromObject [staticinvoke(class 
org.apache.spark.unsafe.types.UTF8String, StringType, fromString, input[0, 
scala.Tuple2]._1, true) AS _1#420,input[0, scala.Tuple2]._2 AS _2#421]
       +- Filter <function1>.apply
          +- DeserializeToObject newInstance(class scala.Tuple2), obj#419: 
scala.Tuple2
             +- LocalRelation [_1#416,_2#417], 
[[0,1800000001,1,61],[0,1800000001,2,62],[0,1800000001,3,63]]
    
    == Optimized Logical Plan ==
    !Project [_1#420]
    +- Filter <function1>.apply
       +- LocalRelation [_1#416,_2#417], 
[[0,1800000001,1,61],[0,1800000001,2,62],[0,1800000001,3,63]]
    ```
    
    This PR fixes `EmbedSerializerInFilter` rule to keep `exprId`s of output of 
surrounded `SerializeFromObject`.
    
    The plans after this patch are as follows:
    
    ```
    == Analyzed Logical Plan ==
    _1: string
    Project [_1#420]
    +- SerializeFromObject [staticinvoke(class 
org.apache.spark.unsafe.types.UTF8String, StringType, fromString, input[0, 
scala.Tuple2]._1, true) AS _1#420,input[0, scala.Tuple2]._2 AS _2#421]
       +- Filter <function1>.apply
          +- DeserializeToObject newInstance(class scala.Tuple2), obj#419: 
scala.Tuple2
             +- LocalRelation [_1#416,_2#417], 
[[0,1800000001,1,61],[0,1800000001,2,62],[0,1800000001,3,63]]
    
    == Optimized Logical Plan ==
    Project [_1#416]
    +- Filter <function1>.apply
       +- LocalRelation [_1#416,_2#417], 
[[0,1800000001,1,61],[0,1800000001,2,62],[0,1800000001,3,63]]
    ```
    
    ## How was this patch tested?
    
    Existing tests and I added a test to check if `filter and then select` 
works.


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

    $ git pull https://github.com/ueshin/apache-spark issues/SPARK-15313

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

    https://github.com/apache/spark/pull/13096.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 #13096
    
----
commit 59085d3862522265246d0e80992f4fb031a98c79
Author: Takuya UESHIN <[email protected]>
Date:   2016-05-13T08:25:35Z

    Add a test to check if filter and then select works.

commit 2620409239e808578dbbd9e0099d32f041879f8e
Author: Takuya UESHIN <[email protected]>
Date:   2016-05-13T08:39:26Z

    Fix EmbedSerializerInFilter rule to keep exprIds of output of surrounded 
SerializeFromObject.

----


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to