Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/20141#discussion_r159439981
--- Diff: sql/core/src/main/scala/org/apache/spark/sql/functions.scala ---
@@ -3254,42 +3254,66 @@ object functions {
*/
def map_values(e: Column): Column = withExpr { MapValues(e.expr) }
-
//////////////////////////////////////////////////////////////////////////////////////////////
-
//////////////////////////////////////////////////////////////////////////////////////////////
-
// scalastyle:off line.size.limit
// scalastyle:off parameter.number
/* Use the following code to generate:
- (0 to 10).map { x =>
+
+ (0 to 10).foreach { x =>
val types = (1 to x).foldRight("RT")((i, s) => {s"A$i, $s"})
val typeTags = (1 to x).map(i => s"A$i: TypeTag").foldLeft("RT:
TypeTag")(_ + ", " + _)
val inputTypes = (1 to x).foldRight("Nil")((i, s) =>
{s"ScalaReflection.schemaFor(typeTag[A$i]).dataType :: $s"})
println(s"""
- /**
- * Defines a deterministic user-defined function of ${x} arguments as
user-defined
- * function (UDF). The data types are automatically inferred based on
the function's
- * signature. To change a UDF to nondeterministic, call the API
- * `UserDefinedFunction.asNondeterministic()`.
- *
- * @group udf_funcs
- * @since 1.3.0
- */
- def udf[$typeTags](f: Function$x[$types]): UserDefinedFunction = {
- val ScalaReflection.Schema(dataType, nullable) =
ScalaReflection.schemaFor[RT]
- val inputTypes = Try($inputTypes).toOption
- val udf = UserDefinedFunction(f, dataType, inputTypes)
- if (nullable) udf else udf.asNonNullable()
- }""")
+ |/**
+ | * Defines a Scala closure of $x arguments as user-defined function
(UDF).
+ | * The data types are automatically inferred based on the Scala
closure's
+ | * signature. By default the returned UDF is deterministic, to
change it to
+ | * nondeterministic, call the API
`UserDefinedFunction.asNondeterministic()`.
+ | *
+ | * @group udf_funcs
+ | * @since 1.3.0
+ | */
+ |def udf[$typeTags](f: Function$x[$types]): UserDefinedFunction = {
+ | val ScalaReflection.Schema(dataType, nullable) =
ScalaReflection.schemaFor[RT]
+ | val inputTypes = Try($inputTypes).toOption
+ | val udf = UserDefinedFunction(f, dataType, inputTypes)
+ | if (nullable) udf else udf.asNonNullable()
+ |}""".stripMargin)
+ }
+
+ (0 to 10).foreach { i =>
+ val extTypeArgs = (0 to i).map(_ => "_").mkString(", ")
+ val anyTypeArgs = (0 to i).map(_ => "Any").mkString(", ")
+ val anyCast = s".asInstanceOf[UDF$i[$anyTypeArgs]]"
+ val anyParams = (1 to i).map(_ => "_: Any").mkString(", ")
+ val funcCall = if (i == 0) "() => func" else "func"
+ println(s"""
+ |/**
+ | * Defines a Java UDF$i instance as user-defined function (UDF).
+ | * The caller must specify the output data type, and there is no
automatic input type coercion.
+ | * By default the returned UDF is deterministic, to change it to
nondeterministic, call the
+ | * API `UserDefinedFunction.asNondeterministic()`.
+ | *
+ | * @group udf_funcs
+ | * @since 2.3.0
+ | */
+ |def udf(f: UDF$i[$extTypeArgs], returnType: DataType):
UserDefinedFunction = {
+ | val func = f$anyCast.call($anyParams)
+ | UserDefinedFunction($funcCall, returnType, inputTypes = None)
+ |}""".stripMargin)
}
*/
+
//////////////////////////////////////////////////////////////////////////////////////////////
+ // Scala UDF functions
+
//////////////////////////////////////////////////////////////////////////////////////////////
+
/**
- * Defines a deterministic user-defined function of 0 arguments as
user-defined
- * function (UDF). The data types are automatically inferred based on
the function's
- * signature. To change a UDF to nondeterministic, call the API
- * `UserDefinedFunction.asNondeterministic()`.
+ * Defines a Scala closure of 0 arguments as user-defined function (UDF).
--- End diff --
to make the document consistent with `UDFRegistration.register`
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]