Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function$x[$fTypes], returnType: DataType${if (args.length > 0) ", " + args else ""}): Column = {
ScalaUdf(f, returnType, Seq($argsInUdf))
}""")
Expand Down Expand Up @@ -1521,7 +1523,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function0[_], returnType: DataType): Column = {
ScalaUdf(f, returnType, Seq())
}
Expand All @@ -1532,7 +1536,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function1[_, _], returnType: DataType, arg1: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr))
}
Expand All @@ -1543,7 +1549,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function2[_, _, _], returnType: DataType, arg1: Column, arg2: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr))
}
Expand All @@ -1554,7 +1562,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function3[_, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr))
}
Expand All @@ -1565,7 +1575,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function4[_, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr))
}
Expand All @@ -1576,7 +1588,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function5[_, _, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column, arg5: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr, arg5.expr))
}
Expand All @@ -1587,7 +1601,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function6[_, _, _, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column, arg5: Column, arg6: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr, arg5.expr, arg6.expr))
}
Expand All @@ -1598,7 +1614,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function7[_, _, _, _, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column, arg5: Column, arg6: Column, arg7: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr, arg5.expr, arg6.expr, arg7.expr))
}
Expand All @@ -1609,7 +1627,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function8[_, _, _, _, _, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column, arg5: Column, arg6: Column, arg7: Column, arg8: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr, arg5.expr, arg6.expr, arg7.expr, arg8.expr))
}
Expand All @@ -1620,7 +1640,9 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function9[_, _, _, _, _, _, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column, arg5: Column, arg6: Column, arg7: Column, arg8: Column, arg9: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr, arg5.expr, arg6.expr, arg7.expr, arg8.expr, arg9.expr))
}
Expand All @@ -1631,13 +1653,34 @@ object functions {
*
* @group udf_funcs
* @since 1.3.0
* @deprecated As of 1.5.0, since it's redundant with udf()
*/
@deprecated("Use udf", "1.5.0")
def callUDF(f: Function10[_, _, _, _, _, _, _, _, _, _, _], returnType: DataType, arg1: Column, arg2: Column, arg3: Column, arg4: Column, arg5: Column, arg6: Column, arg7: Column, arg8: Column, arg9: Column, arg10: Column): Column = {
ScalaUdf(f, returnType, Seq(arg1.expr, arg2.expr, arg3.expr, arg4.expr, arg5.expr, arg6.expr, arg7.expr, arg8.expr, arg9.expr, arg10.expr))
}

// scalastyle:on

/**
* Call an user-defined function.
* Example:
* {{{
* import org.apache.spark.sql._
*
* val df = Seq(("id1", 1), ("id2", 4), ("id3", 5)).toDF("id", "value")
* val sqlContext = df.sqlContext
* sqlContext.udf.register("simpleUdf", (v: Int) => v * v)
* df.select($"id", callUDF("simpleUdf", $"value"))
* }}}
*
* @group udf_funcs
* @since 1.5.0
*/
def callUDF(udfName: String, cols: Column*): Column = {
UnresolvedFunction(udfName, cols.map(_.expr))
}

/**
* Call an user-defined function.
* Example:
Expand All @@ -1652,7 +1695,9 @@ object functions {
*
* @group udf_funcs
* @since 1.4.0
* @deprecated As of 1.5.0, since it was not coherent to have two functions callUdf and callUDF
*/
@deprecated("Use callUDF", "1.5.0")
def callUdf(udfName: String, cols: Column*): Column = {
UnresolvedFunction(udfName, cols.map(_.expr))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class DataFrameSuite extends QueryTest {
)
}

test("call udf in SQLContext") {
test("deprecated callUdf in SQLContext") {
val df = Seq(("id1", 1), ("id2", 4), ("id3", 5)).toDF("id", "value")
val sqlctx = df.sqlContext
sqlctx.udf.register("simpleUdf", (v: Int) => v * v)
Expand All @@ -310,6 +310,15 @@ class DataFrameSuite extends QueryTest {
Row("id1", 1) :: Row("id2", 16) :: Row("id3", 25) :: Nil)
}

test("callUDF in SQLContext") {
val df = Seq(("id1", 1), ("id2", 4), ("id3", 5)).toDF("id", "value")
val sqlctx = df.sqlContext
sqlctx.udf.register("simpleUDF", (v: Int) => v * v)
checkAnswer(
df.select($"id", callUDF("simpleUDF", $"value")),
Row("id1", 1) :: Row("id2", 16) :: Row("id3", 25) :: Nil)
}

test("withColumn") {
val df = testData.toDF().withColumn("newCol", col("key") + 1)
checkAnswer(
Expand Down