Crate udf

source ·
Expand description

A wrapper crate to make writing SQL user-defined functions (UDFs) easy

This crate provides bindings for easy creation of SQL user-defined functions in Rust. See the readme for more background information.

Usage

Using this crate is fairly simple: create a struct that will be used to share data among UDF functions (which can be zero-sized), then implement needed traits for it. BasicUdf provides function signatures for standard UDFs, and AggregateUdf provides signatures for aggregate (and window) UDFs. See the documentation there for a step-by-step guide.

use udf::prelude::*;

// Our struct that will produce a UDF of name `my_udf`
struct MyUdf {}

#[register]
impl BasicUdf for MyUdf {
    // Specify return type of this UDF to be a nullable integer
    type Returns<'a> = Option<i64>;

    // Perform initialization steps here
    fn init<'a>(
        cfg: &UdfCfg<Init>,
        args: &'a ArgList<'a, Init>
    ) -> Result<Self, String> {
        todo!();
    }

    // Create a result here
    fn process<'a>(
        &'a mut self,
        cfg: &UdfCfg<Process>,
        args: &ArgList<Process>,
        error: Option<NonZeroU8>,
    ) -> Result<Self::Returns<'a>, ProcessError> {
        todo!();
    }
}

Version Note

Because of reliance on a feature called GATs, this library requires Rust version >= 1.65 which is currently in beta. If rustup show does not show 1.65 or greater under active toolchain, you will need to update:

rustup default beta
rustup update beta

1.65 is scheduled to become stable on 2022-11-03, so this message may become irrelevant not long after time of writing.

Re-exports

pub extern crate udf_sys;

Modules

Module that can be imported with use udf::prelude::*; to quickly get the most often used imports.
Module containing traits to be implemented by a user
Types and traits that represent SQL interfaces

Macros

Print a formatted log message to stderr to display in server logs

Structs

A collection of SQL arguments
Typestate marker for the initialization phase
Iterator over arguments in a ArgList
Typestate marker for the processing phase
A zero-sized struct indicating that something went wrong
A single SQL argument, including its attributes
A collection of SQL arguments

Enums

Helpful constants related to the max_length parameter
A possible SQL result consisting of a type and nullable value
Enum representing possible SQL result types

Constants

Max error message size, 0x200 = 512 bytes
Minimum size of a buffer for string results

Traits

This trait must be implemented if this function performs aggregation.
This trait specifies the functions needed for a standard (non-aggregate) UDF
A state of the UDF, representing either Init or Process

Attribute Macros

Register exposed function names required for a UDF