public interface FunctionContext
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.
| Modifier and Type | Method and Description |
|---|---|
<V> 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 |
getParameterCount()
Returns the number of parameters.
|
RelDataTypeFactory |
getTypeFactory()
Returns the type factory.
|
boolean |
isArgumentConstant(int ordinal)
Returns whether the value of an argument is constant.
|
RelDataTypeFactory getTypeFactory()
int getParameterCount()
boolean isArgumentConstant(int ordinal)
ordinal - Argument ordinal, starting from 0<V> V getArgumentValueAs(int ordinal,
Class<V> valueClass)
ordinal - Argument ordinal, starting from 0valueClass - Type of valueClassCastException - if argument cannot be converted to
valueClassIllegalArgumentException - if argument is not constantCopyright © 2012-2022 Apache Software Foundation. All Rights Reserved.