diff --git a/fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UdfExecutor.java b/fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UdfExecutor.java index 2fc0435a32b9b7..76801ba4bbafa2 100644 --- a/fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UdfExecutor.java +++ b/fe/be-java-extensions/java-udf/src/main/java/org/apache/doris/udf/UdfExecutor.java @@ -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; @@ -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 @@ -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)) {