How do I write a custom Window Function for Presto?

542 views Asked by At

I'd like to create a custom Window function that processes a sorted stream of doubles and produces a single output double per partition. It is mandatory for values to be sorted. The Window function can process a single row at the time (no need for look behind/ahead) as long as it can maintain an internal state per partition.

The signature will look something like this:

SELECT my_windows_func() OVER (PARTITION BY my_key ORDER BY my_val ASC) AS my_stuff

Now, I figured out how to create AggreagtionFunctions, ScalarFunctions, but with WindowFunctions I don't know where to start and could not find any documentation online.

Which interface should I implement for my usecase? Can I enforce users to add the ORDER BY clause when they call it? Any sample code that I could refer to?

1

There are 1 answers

0
Piotr Findeisen On

The function implementation needs to be returned from io.prestosql.spi.Plugin#getFunctions and be annotated with @WindowFunctionSignature. The io.prestosql.spi.function.ValueWindowFunction is a useful base for implementing a window function. See https://github.com/trinodb/trino/blob/4fba34f3d94291d5411a5c24bd47fa0213898e51/presto-main/src/main/java/io/prestosql/operator/window/LagFunction.java for an example.