Skip to main content

call_function

Call a SQL function. Supports Spark Connect.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.call_function(funcName=<funcName>, *cols)

Parameters

Parameter

Type

Description

funcName

str

Function name that follows the SQL identifier syntax (can be quoted, can be qualified).

cols

pyspark.sql.Column or str

Column names or Columns to be used in the function.

Returns

pyspark.sql.Column: result of executed function.

Examples

Example 1: Calling a function with an integer column

Python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql.types import IntegerType, StringType
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
_ = spark.udf.register("intX2", lambda i: i * 2, IntegerType())
df.select(dbf.call_function("intX2", "id")).show()
Output
+---------+
|intX2(id)|
+---------+
| 2|
| 4|
| 6|
+---------+

Example 2: Calling a function with a string column

Python
from pyspark.databricks.sql import functions as dbf
from pyspark.sql.types import StringType
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
_ = spark.udf.register("strX2", lambda s: s * 2, StringType())
df.select(dbf.call_function("strX2", dbf.col("name"))).show()
Output
+-----------+
|strX2(name)|
+-----------+
| aa|
| bb|
| cc|
+-----------+

Example 3: Calling a built-in function

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
df.select(dbf.call_function("avg", dbf.col("id"))).show()
Output
+-------+
|avg(id)|
+-------+
| 2.0|
+-------+

Example 4: Calling a custom SQL function

Python
from pyspark.databricks.sql import functions as dbf
_ = spark.sql("CREATE FUNCTION custom_avg AS 'test.org.apache.spark.sql.MyDoubleAvg'")

df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
df.select(dbf.call_function("custom_avg", dbf.col("id"))).show()

Output
+------------------------------------+
|spark_catalog.default.custom_avg(id)|
+------------------------------------+
| 102.0|
+------------------------------------+

Example 5: Calling a custom SQL function with fully qualified name

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
df.select(dbf.call_function("spark_catalog.default.custom_avg", dbf.col("id"))).show()
Output
+------------------------------------+
|spark_catalog.default.custom_avg(id)|
+------------------------------------+
| 102.0|
+------------------------------------+