Spark connector: user-defined functions
The Spark connector loads user-defined functions registered in Gravitino. Once a function is
registered, Spark SQL can call it by name, with no CREATE FUNCTION statement in
Spark.
The connector runs only Java implementations registered with the SPARK runtime. SQL and Python
implementations registered in Gravitino are not visible in Spark.
Prerequisites
- The Spark connector is configured and the catalog is reachable.
- The function is registered in Gravitino with at least one definition that has a Java
implementation for the
SPARKruntime; see Register a function. - The jar holding the function class is on the Spark classpath, for example through
--jarsorspark.jars.
Java UDF requirements
The class named in the implementation's className must implement Spark's
org.apache.spark.sql.connector.catalog.functions.UnboundFunction interface, described in the
Spark API documentation.
It needs a public no-argument constructor and must be on both the driver and executor classpath.
Call functions in Spark SQL
Call a registered function by its three-part name, {catalog}.{schema}.{function}.
-- Call a scalar function
SELECT {catalog}.{schema}.add_one(42);
-- Use in a query
SELECT id, {catalog}.{schema}.add_one(value) AS incremented
FROM {catalog}.{schema}.{table};
After USE, the short name works.
USE {catalog};
USE {schema};
SELECT add_one(42);
Discover functions
SHOW FUNCTIONS lists every function with at least one implementation for the SPARK runtime,
whatever its language. Functions registered only for other runtimes, such as TRINO, are neither
listed nor loadable. Calling a function uses its first SPARK implementation, which must be Java;
register Spark functions with Java implementations only.
SHOW FUNCTIONS IN {catalog}.{schema};