
VerticaPy
User-Defined Functions¶
Vertica is an analytical database with many advanced functions. While Vertica obviously doesn't have every function, it comes pretty close by implementing a Python SDK that allows users to create and import their own "lambda" functions into Vertica.
While the SDK is somewhat difficult to use, VerticaPy makes it easy by generating UDFs from python code and importing them into Vertica for you.
To demonstrate this, let's look at the following example. We want to bring into Vertica two functions from the 'math' module. We also want to import our own custom 'normalize_titanic' function defined in the 'pmath.py' file.
from verticapy.udf import create_lib_udf
import verticapy as vp
def normalize_titanic(age, fare):
return (age - 30.15) / 14.44, (fare - 33.96) / 52.65
# The generated files must be placed in the folder: /home/dbadmin/
file_path = "/home/dbadmin/python_math_lib.py"
pmath_path = os.path.dirname(vp.__file__) + "/tests/udf/pmath.py"
udx_str, udx_sql = create_lib_udf([(math.exp, [float], float, {}, "python_exp"),
(math.isclose, [float, float], bool, {"abs_tol": float}, "python_isclose"),
(normalize_titanic, [float, float], {"norm_age": float, "norm_fare": float}, {}, "python_norm_titanic"),],
library_name = "python_math",
include_dependencies = pmath_path,
file_path = file_path,
create_file = False)
We simply need to provide some information about our functions (input/output types, parameters, library name, etc.) and VerticaPy will generate two files.
One for the UDx:
print(udx_str)
And one for the SQL statements.
print("\n".join(udx_sql))
We can then run these queries in our server.
Our functions are now available in Vertica.
%load_ext verticapy.sql
%sql SELECT python_exp(1);
%sql SELECT python_isclose(2, 3 USING PARAMETERS abs_tol=0.01);
%sql SELECT python_isclose(2, 3 USING PARAMETERS abs_tol=1);
%sql SELECT python_norm_titanic(20.0, 30.0) OVER();
It is now easy to bring customized Python functions.