User Defined Functions

User-Defined Functions (aka UDF) is a feature of Lenses SQL to define new functions that extend the vocabulary of Lenses SQL’s DSL for manipulating the data. Many functions are already built in already and over time that set is expected to grow. But a user might need a function not covered in the default package. Lenses provides a simple API that can be implemented to provide custom functions that go beyond the built-in ones. Creating a custom function applying a pre-trained machine learning model to a stream is one of the scenarios.

Implementing your UDF

A simple API is provided to allow someone to provide their own function. Writing a user-defined function for Lenses requires a person to provide an implementation to one of these Java interfaces:

/* The function accepts n-number of arguments */
public interface UserDefinedFunction {
    Object evaluate(List<Object> args);
}

/* The UDF function accepts 1 argument */
public interface UserDefinedFunction1 {
    Object evaluate(Object arg1);
}

/* The UDF function accepts 2 arguments */
public interface UserDefinedFunction2 {
    Object evaluate(Object arg1, Object arg2);
}

It is required for the implementing class to be placed under io.lenses.sql.udf package. Furthermore, the actual class name is going to represent the function name which will be called from Lenses SQL’s DSL.

package io.lenses.sql.udf


class celsius_to_fahrenheit extends UserDefinedFunction1 {

    @Override
    public Object evaluate(arg1: Object) {
        double celsius = Double.parseDouble(arg1.toString());
        double fahrenheit = celsius * 1.8 + 32;
        return Math.round(fahrenheit);
    }
}

You can see on Github a sample project implementing a UDF.

Once the compiled jar file is available, it needs to be dropped under the udf folder found in Lenses installed location. This library will be automatically loaded and the UDF will be available to the SQL engine. Therefore a user can run a query similar to this one:

SELECT celsius_to_fahrenheit(celsius) as fahrenheit
FROM iot_data