Java UDF

Java UDF provides users with a Java interface written in UDF to facilitate the execution of user-defined functions in Java language. Compared with native UDF implementation, Java UDF has the following advantages and limitations:

  1. The advantages
  • Compatibility: Using Java UDF can be compatible with different Doris versions, so when upgrading Doris version, Java UDF does not need additional migration. At the same time, Java UDF also follows the same programming specifications as hive / spark and other engines, so that users can directly move Hive / Spark UDF jar to Doris.
  • Security: The failure or crash of Java UDF execution will only cause the JVM to report an error, not the Doris process to crash.
  • Flexibility: In Java UDF, users can package the third-party dependencies together in the user jar.
  1. Restrictions on use
  • Performance: Compared with native UDF, Java UDF will bring additional JNI overhead, but through batch execution, we have minimized the JNI overhead as much as possible.
  • Vectorized engine: Java UDF is only supported on vectorized engine now.

Type correspondence

TypeUDF Argument Type
BoolBoolean
TinyIntByte
SmallIntShort
IntInteger
BigIntLong
LargeIntBigInteger
FloatFloat
DoubleDouble
DateLocalDate
DatetimeLocalDateTime
CharString
VarcharString
DecimalBigDecimal

Write UDF functions

This section mainly introduces how to develop a Java UDF. Samples for the Java version are provided under samples/doris-demo/java-udf-demo/ for your reference, Check it out here

To use Java UDF, the main entry of UDF must be the evaluate function. This is consistent with other engines such as Hive. In the example of AddOne, we have completed the operation of adding an integer as the UDF.

It is worth mentioning that this example is not only the Java UDF supported by Doris, but also the UDF supported by Hive, that’s to say, for users, Hive UDF can be directly migrated to Doris.

Create UDF

  1. CREATE FUNCTION
  2. name ([,...])
  3. [RETURNS] rettype
  4. PROPERTIES (["key"="value"][,...])

Instructions:

  1. symbol in properties represents the class name containing UDF classes. This parameter must be set.
  2. The jar package containing UDF represented by file in properties must be set.
  3. The UDF call type represented by type in properties is native by default. When using java UDF, it is transferred to Java_UDF.
  4. name: A function belongs to a DB and name is of the formdbName.funcName. When dbName is not explicitly specified, the db of the current session is useddbName.

Sample:

  1. CREATE FUNCTION java_udf_add_one(int) RETURNS int PROPERTIES (
  2. "file"="file:///path/to/java-udf-demo-jar-with-dependencies.jar",
  3. "symbol"="org.apache.doris.udf.AddOne",
  4. "type"="JAVA_UDF"
  5. );

Create UDAF

When using Java code to write UDAF, there are some functions that must be implemented (mark required) and an inner class State, which will be explained with a specific example below. The following SimpleDemo will implement a simple function similar to sum, the input parameter is INT, and the output parameter is INT

  1. package org.apache.doris.udf;
  2. public class SimpleDemo {
  3. //Need an inner class to store data
  4. /*required*/
  5. public static class State {
  6. /*some variables if you need */
  7. public int sum = 0;
  8. }
  9. /*required*/
  10. public State create() {
  11. /* here could do some init work if needed */
  12. return new State();
  13. }
  14. /*required*/
  15. public void destroy(State state) {
  16. /* here could do some destroy work if needed */
  17. }
  18. /*required*/
  19. //first argument is State, then other types your input
  20. public void add(State state, Integer val) {
  21. /* here doing update work when input data*/
  22. if (val != null) {
  23. state.sum += val;
  24. }
  25. }
  26. /*required*/
  27. public void serialize(State state, DataOutputStream out) {
  28. /* serialize some data into buffer */
  29. out.writeInt(state.sum);
  30. }
  31. /*required*/
  32. public void deserialize(State state, DataInputStream in) {
  33. /* deserialize get data from buffer before you put */
  34. int val = in.readInt();
  35. state.sum = val;
  36. }
  37. /*required*/
  38. public void merge(State state, State rhs) {
  39. /* merge data from state */
  40. state.sum += rhs.sum;
  41. }
  42. /*required*/
  43. //return Type you defined
  44. public Integer getValue(State state) {
  45. /* return finally result */
  46. return state.sum;
  47. }
  48. }
  1. CREATE AGGREGATE FUNCTION simple_sum(INT) RETURNS INT PROPERTIES (
  2. "file"="file:///pathTo/java-udaf.jar",
  3. "symbol"="org.apache.doris.udf.SimpleDemo",
  4. "type"="JAVA_UDF"
  5. );

Currently, UDTF are not supported.

Use UDF

Users must have the SELECT permission of the corresponding database to use UDF/UDAF.

The use of UDF is consistent with ordinary function methods. The only difference is that the scope of built-in functions is global, and the scope of UDF is internal to DB. When the link session is inside the data, directly using the UDF name will find the corresponding UDF inside the current DB. Otherwise, the user needs to display the specified UDF database name, such as dbName.funcName.

Delete UDF

When you no longer need UDF functions, you can delete a UDF function by the following command, you can refer to DROP FUNCTION.

Example

Examples of Java UDF are provided in the samples/doris-demo/java-udf-demo/ directory. See the README.md in each directory for details on how to use it, Check it out here

Unsupported Use Case

At present, Java UDF is still in the process of continuous development, so some features are not completed.

  1. Complex data types (HLL, bitmap) are not supported.
  2. Memory management and statistics of JVM and Doris have not been unified.