नोट
इस पेज तक पहुँच के लिए प्रमाणन की आवश्यकता होती है. आप साइन इन करने या निर्देशिकाओं को बदलने का प्रयास कर सकते हैं.
इस पेज तक पहुँच के लिए प्रमाणन की आवश्यकता होती है. आप निर्देशिकाओं को बदलने का प्रयास कर सकते हैं.
Call a SQL function. Supports Spark Connect.
Syntax
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
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()
+---------+
|intX2(id)|
+---------+
| 2|
| 4|
| 6|
+---------+
Example 2: Calling a function with a string column
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()
+-----------+
|strX2(name)|
+-----------+
| aa|
| bb|
| cc|
+-----------+
Example 3: Calling a built-in function
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()
+-------+
|avg(id)|
+-------+
| 2.0|
+-------+
Example 4: Calling a custom SQL function
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()
+------------------------------------+
|spark_catalog.default.custom_avg(id)|
+------------------------------------+
| 102.0|
+------------------------------------+
Example 5: Calling a custom SQL function with fully qualified name
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()
+------------------------------------+
|spark_catalog.default.custom_avg(id)|
+------------------------------------+
| 102.0|
+------------------------------------+