Interface FunctionContext


public interface FunctionContext
Information about a function call that is passed to the constructor of a function instance.

This may enable a function to perform work up front, during construction, rather than each time it is invoked. Here is an example of such a function:

 class RegexMatchesFunction {
   final java.util.regex.Pattern pattern;

   public RegexMatchesFunction(FunctionContext cx) {
     String pattern = cx.argumentValueAs(String.class);
     this.compiledPattern = java.util.regex.Pattern.compile(pattern);
   }

   public boolean eval(String pattern, String s) {
     return this.compiledPattern.matches(s);
   }
 }
 

Register it in the model as follows:

   functions: [
     {
       name: 'REGEX_MATCHES',
       className: 'com.example.RegexMatchesFun'
     }
   ]
 

and use it in a query:

 SELECT empno, ename
 FROM Emp
 WHERE regex_matches('J.*ES', ename);

 +-------+--------+
 | EMPNO | ENAME  |
 +-------+--------+
 | 7900  | JAMES  |
 | 7566  | JONES  |
 +-------+--------+
 

When executing the query, Calcite will create an instance of RegexMatchesFunction and call the eval method on that instance once per row.

If the eval method was static, or if the function's constructor had zero parameters, the eval method would have to call java.util.regex.Pattern.compile(pattern) to compile the pattern each time.

This interface is marked Experimental, which means that we may change or remove methods, or the entire interface, without notice. But probably we will add methods over time, which will just your UDFs more information to work with.

  • Method Summary

    Modifier and Type
    Method
    Description
    <V> @Nullable V
    getArgumentValueAs(int ordinal, Class<V> valueClass)
    Returns the value of an argument to this function, null if the argument is the NULL literal.
    int
    Returns the number of parameters.
    Returns the type factory.
    boolean
    isArgumentConstant(int ordinal)
    Returns whether the value of an argument is constant.
  • Method Details

    • getTypeFactory

      RelDataTypeFactory getTypeFactory()
      Returns the type factory.
    • getParameterCount

      int getParameterCount()
      Returns the number of parameters.
    • isArgumentConstant

      boolean isArgumentConstant(int ordinal)
      Returns whether the value of an argument is constant.
      Parameters:
      ordinal - Argument ordinal, starting from 0
    • getArgumentValueAs

      <V> @Nullable V getArgumentValueAs(int ordinal, Class<V> valueClass)
      Returns the value of an argument to this function, null if the argument is the NULL literal.
      Parameters:
      ordinal - Argument ordinal, starting from 0
      valueClass - Type of value
      Throws:
      ClassCastException - if argument cannot be converted to valueClass
      IllegalArgumentException - if argument is not constant