Python UDF for Apache Arrow
Notice: Python 3.12 is required to run this library.
If python3
is not 3.12, please set the environment variable PYO3_PYTHON=python3.12
.
Add the following lines to your Cargo.toml
:
[]
= "0.4"
Create a Runtime
and define your Python functions in string form.
Note that the function name must match the one you pass to add_function
.
use ;
let mut runtime = new.unwrap;
let python_code = r#"
def gcd(a: int, b: int) -> int:
while b:
a, b = b, a % b
return a
"#;
let return_type = Int32;
let mode = ReturnNullOnNullInput;
runtime.add_function.unwrap;
You can then call the python function on a RecordBatch
:
let input: RecordBatch = ...;
let output: RecordBatch = runtime.call.unwrap;
The python code will be run in an embedded CPython 3.12 interpreter, powered by PyO3.
See the example for more details.
Struct Type
If the function returns a struct type, you can return a class instance or a dictionary.
use ;
use ;
let mut runtime = new.unwrap;
let python_code = r#"
class KeyValue:
def __init__(self, key, value):
self.key = key
self.value = value
def key_value(s: str):
key, value = s.split('=')
## return a class instance
return KeyValue(key, value)
## or return a dict
return {"key": key, "value": value}
"#;
let return_type = Struct;
let mode = ReturnNullOnNullInput;
runtime.add_function.unwrap;
Extension Type
This crate also supports the following Arrow extension types:
Extension Type | Physical Type | ARROW:extension:name |
Python Type |
---|---|---|---|
JSON | String | arrowudf.json |
any (parsed by json.loads ) |
Decimal | String | arrowudf.decimal |
decimal.Decimal |
Pickle | Binary | arrowudf.pickle |
any (parsed by pickle.loads ) |
Pickle Type
When a field is pickle type, the data is stored in a binary array in serialized form.
# use ;
# use BinaryArray;
let pickle_field = new
.with_metadata;
let pickle_array = from;
Pickle type is useful for the state of aggregation functions when the state is complex.