User Defined Functions

User-Defined Functions (UDF) is a feature of Lenses SQL that allows you to define new functions that extend the existing vocabulary of Lenses SQL’s DSL for manipulating data. Many functions are already built-in into Lenses SQL and over time that set is expected to grow. However, a user might still need a function that is not covered by 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 and 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 functions. Writing a user-defined function for Lenses requires a person to provide an implementation of 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 that the implementing class is placed under the 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 implements UserDefinedFunction1 {

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

You can see on Github for 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