diff --git a/be/src/vec/functions/function_java_udf.h b/be/src/vec/functions/function_java_udf.h index db400a2482..8d878ff2b8 100644 --- a/be/src/vec/functions/function_java_udf.h +++ b/be/src/vec/functions/function_java_udf.h @@ -78,6 +78,8 @@ private: struct IntermediateState { size_t buffer_size; size_t row_idx; + + IntermediateState() : buffer_size(0), row_idx(0) {} }; struct JniContext { @@ -95,17 +97,16 @@ private: // intermediate_state includes two parts: reserved / used buffer size and rows std::unique_ptr output_intermediate_state_ptr; - JniContext(int64_t num_args, JavaFunctionCall* parent) : parent(parent) { - input_values_buffer_ptr.reset(new int64_t[num_args]); - input_nulls_buffer_ptr.reset(new int64_t[num_args]); - input_offsets_ptrs.reset(new int64_t[num_args]); - output_value_buffer.reset((int64_t*)malloc(sizeof(int64_t))); - output_null_value.reset((int64_t*)malloc(sizeof(int64_t))); - batch_size_ptr.reset((int32_t*)malloc(sizeof(int32_t))); - output_offsets_ptr.reset((int64_t*)malloc(sizeof(int64_t))); - output_intermediate_state_ptr.reset( - (IntermediateState*)malloc(sizeof(IntermediateState))); - } + JniContext(int64_t num_args, JavaFunctionCall* parent) + : parent(parent), + input_values_buffer_ptr(new int64_t[num_args]), + input_nulls_buffer_ptr(new int64_t[num_args]), + input_offsets_ptrs(new int64_t[num_args]), + output_value_buffer(new int64_t()), + output_null_value(new int64_t()), + output_offsets_ptr(new int64_t()), + batch_size_ptr(new int32_t()), + output_intermediate_state_ptr(new IntermediateState()) {} ~JniContext() { VLOG_DEBUG << "Free resources for JniContext"; diff --git a/regression-test/data/javaudf_p0/test_javaudf_addone.out b/regression-test/data/javaudf_p0/test_javaudf_addone.out new file mode 100644 index 0000000000..8f579a5f8b --- /dev/null +++ b/regression-test/data/javaudf_p0/test_javaudf_addone.out @@ -0,0 +1,25 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select_default -- +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 + +-- !select -- +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 + diff --git a/regression-test/suites/javaudf_p0/test_javaudf_addone.groovy b/regression-test/suites/javaudf_p0/test_javaudf_addone.groovy new file mode 100644 index 0000000000..d53741410b --- /dev/null +++ b/regression-test/suites/javaudf_p0/test_javaudf_addone.groovy @@ -0,0 +1,69 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import org.codehaus.groovy.runtime.IOGroovyMethods + +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Paths + +suite("test_javaudf_addone") { + def tableName = "test_javaudf_addone" + def jarPath = """${context.file.parent}/jars/java-udf-demo-jar-with-dependencies.jar""" + + log.info("Jar path: ${jarPath}".toString()) + try { + sql """ DROP TABLE IF EXISTS ${tableName} """ + sql """ + CREATE TABLE IF NOT EXISTS ${tableName} ( + `user_id` INT NOT NULL COMMENT "用户id" + ) + DISTRIBUTED BY HASH(user_id) PROPERTIES("replication_num" = "1"); + """ + StringBuilder sb = new StringBuilder() + int i = 1 + for (; i < 10; i ++) { + sb.append(""" + (${i}), + """) + } + sb.append(""" + (${i}) + """) + sql """ INSERT INTO ${tableName} VALUES + ${sb.toString()} + """ + qt_select_default """ SELECT * FROM ${tableName} t ORDER BY user_id; """ + + File path = new File(jarPath) + if (!path.exists()) { + throw new IllegalStateException("""${jarPath} doesn't exist! """) + } + + sql """ CREATE FUNCTION java_udf_add_one(int) RETURNS int PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.AddOne", + "type"="JAVA_UDF" + ); """ + + qt_select """ SELECT java_udf_add_one(user_id) result FROM ${tableName} ORDER BY result; """ + + sql """ DROP FUNCTION java_udf_add_one(int); """ + } finally { + try_sql("DROP TABLE IF EXISTS ${tableName}") + } +} diff --git a/run-regression-test.sh b/run-regression-test.sh index 0a57fc0ea6..f1b43fc91d 100755 --- a/run-regression-test.sh +++ b/run-regression-test.sh @@ -151,6 +151,16 @@ if ! test -f ${RUN_JAR:+${RUN_JAR}}; then cp -r "${REGRESSION_TEST_BUILD_DIR}"/regression-test-*.jar "${OUTPUT_DIR}/lib" fi +# build jar needed by java-udf case +JAVAUDF_JAR="${DORIS_HOME}/samples/doris-demo/java-udf-demo/target/java-udf-demo-jar-with-dependencies.jar" +if ! test -f ${JAVAUDF_JAR:+${JAVAUDF_JAR}}; then + mkdir -p "${DORIS_HOME}"/regression-test/suites/javaudf_p0/jars + cd "${DORIS_HOME}"/samples/doris-demo/java-udf-demo + "${MVN_CMD}" package + cp target/java-udf-demo-jar-with-dependencies.jar "${DORIS_HOME}"/regression-test/suites/javaudf_p0/jars/ + cd "${DORIS_HOME}" +fi + # check java home if [[ -z "${JAVA_HOME}" ]]; then echo "Error: JAVA_HOME is not set"