Function Extension

    To develop a function for Kuiper is to implement api.Function (opens new window) interface and export it as a golang plugin.

    Before starting the development, you must .

    To develop a function, the Validate method is firstly to be implemented. This method will be called during SQL validation. In this method, a slice of xsql.ExprFunction Extension - 图2 (opens new window) is passed as the parameter that contains the arguments for this function in the runtime. The developer can do a validation against it to check the argument count and type etc. If validation is successful, return nil. Otherwise, return an error object.

    There are 2 types of functions: aggregate function and common function. For aggregate function, if the argument is a column, the received value will always be a slice of the column values in a group. The extended function must distinguish the function type by implement IsAggregate method.

    As the function itself is a plugin, it must be in the main package. Given the function struct name is myFunction. At last of the file, the source must be exported as a symbol as below. There are . For function extension, if there is no internal state, it is recommended to export a singleton instance.

    The Echo Function (opens new window) is a good example.

    In one plugin, developers can export multiple functions. Each function must implement as described at Develop a customized function section. Make sure all functions are exported like:

    It is a best practice to combine all related functions in a plugin to simplify the build and deployment of functions.

      Kuiper will load plugins in the plugin folders automatically. The auto loaded function plugin assumes there is a function named the same as the plugin name. If multiple functions are exported, users need to explicitly register them to make them available. There are two ways to register the functions.

      1. In development environment, we recommend to build plugin .so file directly into the plugin folder so that kuiper can auto load it. Then call or REST register functions API.
      2. In production environment, , then call CLI function plugin create command or with functions list specified.

      Usage

      The customized function can be directly used in the SQL of a rule if it follows the below convention.

      If you have developed a function implementation MyFunction, you should have:

      1. In the plugin file, symbol MyFunction is exported.
      2. The compiled MyFunction.so file is located inside plugins/functions

      To use it, just call it in the SQL inside a rule definition: