Skip to content

Commit

Permalink
[enhancement](udf) add prepare function for java-udf
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangstar333 committed Dec 20, 2023
1 parent cbf1f86 commit c9110fe
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

public class UdfExecutor extends BaseExecutor {
public static final Logger LOG = Logger.getLogger(UdfExecutor.class);
public static final String UDF_PREPARE_FUNCTION_NAME = "prepare";

// setup by init() and cleared by close()
private Method method;

Expand Down Expand Up @@ -122,6 +124,16 @@ public Method getMethod() {
return method;
}

private Method findPrepareMethod(Method[] methods) {
for (Method method : methods) {
if (method.getName().equals(UDF_PREPARE_FUNCTION_NAME) && method.getReturnType().equals(void.class)
&& method.getParameterCount() == 0) {
return method;
}
}
return null; // Method not found
}

// Preallocate the input objects that will be passed to the underlying UDF.
// These objects are allocated once and reused across calls to evaluate()
@Override
Expand All @@ -146,6 +158,10 @@ protected void init(TJavaUdfExecutorCtorParams request, String jarPath, Type fun
Constructor<?> ctor = c.getConstructor();
udf = ctor.newInstance();
Method[] methods = c.getMethods();
Method prepareMethod = findPrepareMethod(methods);
if (prepareMethod != null) {
prepareMethod.invoke(udf);
}
for (Method m : methods) {
// By convention, the udf must contain the function "evaluate"
if (!m.getName().equals(UDF_FUNCTION_NAME)) {
Expand Down

0 comments on commit c9110fe

Please sign in to comment.