GitHub user maropu opened a pull request:

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

    [SPARK-18884][SQL] Support Array[_] in ScalaUDF

    ## What changes were proposed in this pull request?
    This pr is to support `Array[_]` in `ScalaUDF`.
    Currently, a query below throws an exception if we use the type in 
`ScalaUDF`;
    
    ```
    scala> import org.apache.spark.sql.execution.debug._
    scala> Seq((0, 1)).toDF("a", "b").select(array($"a", 
$"b").as("ar")).write.mode("overwrite").parquet("/Users/maropu/Desktop/data/")
    scala> val df = spark.read.load("/Users/maropu/Desktop/data/")
    scala> val df = Seq((0, 1)).toDF("a", "b").select(array($"a", 
$"b").as("ar"))
    scala> val testArrayUdf = udf { (ar: Array[Int]) => ar.sum }
    scala> df.select(testArrayUdf($"ar")).show
    
    Caused by: java.lang.ClassCastException: 
scala.collection.mutable.WrappedArray$ofRef cannot be cast to [I
      at $anonfun$1.apply(<console>:23)
      at 
org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2.apply(ScalaUDF.scala:89)
      at 
org.apache.spark.sql.catalyst.expressions.ScalaUDF$$anonfun$2.apply(ScalaUDF.scala:88)
      at 
org.apache.spark.sql.catalyst.expressions.ScalaUDF.eval(ScalaUDF.scala:1069)
      ... 99 more
    ```
    On the other hand, another query below is passed;
    ```
    scala> val testSeqUdf = udf { (ar: Seq[Int]) => ar.sum }
    scala> df.select(testSeqUdf($"ar")).show
    +-------+
    |UDF(ar)|
    +-------+
    |      1|
    +-------+
    ```
    
    The existing implementation checks argument types (`DataType`) by 
reflection (`ScalaReflection.schemaFor`) in `sql.functions.udf`, and then 
creates type converters (`CatalystTypeConverters`) from the types. `Seq[_]` 
and `Array[_]` are represented as `ArrayType` in `DataType` and both types are 
handled by using `ArrayConverter`. So, since it cannot tell a difference 
between both types in `DataType`. This pr modified code to create type 
converters directly from `TypeTag` in `sql.functions.udf` (and also in 
`UDFRegistration.register`).
    
    ## How was this patch tested?
    Added tests in `DataFrameSuite`.

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

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

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

    https://github.com/apache/spark/pull/16605.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 #16605
    
----
commit f2cf910813adbcc139deea18c12b99b1a85affbe
Author: Takeshi YAMAMURO <[email protected]>
Date:   2016-12-15T11:24:13Z

    Support ArrayType in ScalaUDF

----


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