Prerequisites
Make sure the following requirements are met before proceeding:- Install Python 3.8 or later.
- Enable UDF in the database by setting
enable_udftotruein the FE configuration file fe/conf/fe.conf, and then restart the FE nodes to allow the configuration to take effect. For more information, see FE configuration - enable_udf. - Python UDF requires the following packages: pyarrow
- Set the location of the Python interpreter environment in the BE instance using environment variable. Add the variable item
python_envsand set it to the location of the Python interpreter installation, for example,/opt/Python-3.8/.
python_worker_max_per_driver (default: 3) to change how many local Python worker processes one pipeline driver uses. Each worker runs Python separately, so a larger value lets more UDF calls of a driver run in parallel, for example when the UDF is evaluated in a scan, at the cost of more worker processes. You can change it without restarting the BE.
Develop and use Python UDFs
Syntax:
Properties include:
Create an inline scalar input UDF using Python
The following example creates an inlineecho function with scalar input using Python.
Create an inline vectorized input UDF using Python
Vectorized input is supported to enhance the UDF processing performance. The following example creates an inlineadd function with vectorized input using Python.
Create a packaged UDF using Python
When creating the Python package, you must package the module into.py.zip files, which need to meet the zipimport format.
Use an external worker service
By default, the BE spawns and manages a local Python worker process to run a Python UDF. Alternatively, you can run the worker as an external Arrow Flight service yourself and have the BE connect to it by setting theservice_url property. This lets you run and isolate the worker independently of the BE (for example, in a separate, sandboxed container).
When service_url is set:
- The BE connects to the worker at that URL instead of spawning a local worker.
- For inline UDFs, the function code is sent to the worker over the connection.
- For packaged (
file) UDFs, the worker downloads the package from thefileURL itself and verifies it against the function’s checksum, so the worker service must be able to reach that URL. - You are responsible for running, scaling, securing, and isolating the worker service. The connection is plaintext by default; restrict network access to the worker and enable TLS/authentication as needed.
- To keep a query from hanging when a worker is unreachable or stuck, set the BE configuration item
python_udf_rpc_timeout_ms(milliseconds) to bound the worker call. It applies to both local and external workers and defaults to0(no timeout). Because it bounds the whole call stream, set it above the longest expected UDF query.
Appendix
Mapping between SQL data types and Python data types
Nested types (
ARRAY, MAP, STRUCT) may be composed arbitrarily — for example,
array<map<string,int>>, map<string,array<int>>, struct<a array<int>, b map<string,int>>,
or array<struct<...>>. In scalar mode each row is delivered as plain Python list /
dict values recursively. To return a nested type, return matching Python list / dict
values (with dict keys equal to struct field names); pyarrow performs the
recursive conversion automatically.
Compile Python
Follow these steps to compile Python:-
Get the OpenSSL package.
-
Decompress the package.
-
Navigate to the decompressed folder.
-
Set the environment variable
OPENSSL_DIR. -
Prepare source code for compilation.
-
Compile OpenSSL.
-
Set the environment variable
LD_LIBRARY_PATH. -
Navigate back to the working directory and get the Python package.
-
Decompress the package.
- Navigate to the decompressed folder.
- Make and navigate to the directory
build.
- Prepare source code for compilation.
- Compile Python.
- Install PyArrow and grpcio.
- Compress the files into the package.
- Distribute the package to the target BE server, and decompress the package.
- Modify the BE configuration file be.conf to add the following configuration item.