Repository: spark Updated Branches: refs/heads/master ddc7ba31c -> 770d8153a
[SPARK-4375] [SQL] Add 0 argument support for udf Author: Cheng Hao <hao.ch...@intel.com> Closes #3595 from chenghao-intel/udf0 and squashes the following commits: a858973 [Cheng Hao] Add 0 arguments support for udf Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/770d8153 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/770d8153 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/770d8153 Branch: refs/heads/master Commit: 770d8153a5fe400147cc597c8b4b703f0aa00c22 Parents: ddc7ba3 Author: Cheng Hao <hao.ch...@intel.com> Authored: Tue Dec 16 21:21:11 2014 -0800 Committer: Michael Armbrust <mich...@databricks.com> Committed: Tue Dec 16 21:21:11 2014 -0800 ---------------------------------------------------------------------- .../org/apache/spark/sql/UdfRegistration.scala | 16 ++++++++++------ .../test/scala/org/apache/spark/sql/UDFSuite.scala | 5 +++++ 2 files changed, 15 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/770d8153/sql/core/src/main/scala/org/apache/spark/sql/UdfRegistration.scala ---------------------------------------------------------------------- diff --git a/sql/core/src/main/scala/org/apache/spark/sql/UdfRegistration.scala b/sql/core/src/main/scala/org/apache/spark/sql/UdfRegistration.scala index 00d6b43..5fb4726 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/UdfRegistration.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/UdfRegistration.scala @@ -72,14 +72,13 @@ private[sql] trait UDFRegistration { functionRegistry.registerFunction(name, builder) } - /** registerFunction 1-22 were generated by this script + /** registerFunction 0-22 were generated by this script - (1 to 22).map { x => - val types = (1 to x).map(x => "_").reduce(_ + ", " + _) + (0 to 22).map { x => + val types = (1 to x).foldRight("T")((_, s) => {s"_, $s"}) s""" - def registerFunction[T: TypeTag](name: String, func: Function$x[$types, T]): Unit = { - def builder(e: Seq[Expression]) = - ScalaUdf(func, ScalaReflection.schemaFor[T].dataType, e) + def registerFunction[T: TypeTag](name: String, func: Function$x[$types]): Unit = { + def builder(e: Seq[Expression]) = ScalaUdf(func, ScalaReflection.schemaFor[T].dataType, e) functionRegistry.registerFunction(name, builder) } """ @@ -87,6 +86,11 @@ private[sql] trait UDFRegistration { */ // scalastyle:off + def registerFunction[T: TypeTag](name: String, func: Function0[T]): Unit = { + def builder(e: Seq[Expression]) = ScalaUdf(func, ScalaReflection.schemaFor[T].dataType, e) + functionRegistry.registerFunction(name, builder) + } + def registerFunction[T: TypeTag](name: String, func: Function1[_, T]): Unit = { def builder(e: Seq[Expression]) = ScalaUdf(func, ScalaReflection.schemaFor[T].dataType, e) functionRegistry.registerFunction(name, builder) http://git-wip-us.apache.org/repos/asf/spark/blob/770d8153/sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala ---------------------------------------------------------------------- diff --git a/sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala index ef9b76b..720953a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/UDFSuite.scala @@ -31,6 +31,11 @@ class UDFSuite extends QueryTest { assert(sql("SELECT strLenScala('test')").first().getInt(0) === 4) } + test("ZeroArgument UDF") { + registerFunction("random0", () => { Math.random()}) + assert(sql("SELECT random0()").first().getDouble(0) >= 0.0) + } + test("TwoArgument UDF") { registerFunction("strLenScala", (_: String).length + (_:Int)) assert(sql("SELECT strLenScala('test', 1)").first().getInt(0) === 5) --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org