diff --git a/fe/java-udf/src/main/java/org/apache/doris/udf/BaseExecutor.java b/fe/java-udf/src/main/java/org/apache/doris/udf/BaseExecutor.java index 55ff08f700..9e481b521a 100644 --- a/fe/java-udf/src/main/java/org/apache/doris/udf/BaseExecutor.java +++ b/fe/java-udf/src/main/java/org/apache/doris/udf/BaseExecutor.java @@ -21,7 +21,6 @@ import org.apache.doris.catalog.Type; import org.apache.doris.thrift.TJavaUdfExecutorCtorParams; import org.apache.doris.udf.UdfUtils.JavaUdfDataType; -import com.google.common.base.Preconditions; import org.apache.log4j.Logger; import org.apache.thrift.TDeserializer; import org.apache.thrift.TException; @@ -327,8 +326,8 @@ public abstract class BaseExecutor { return true; } case DECIMALV2: { - Preconditions.checkArgument(((BigDecimal) obj).scale() == 9, "Scale of DECIMALV2 must be 9"); - BigInteger data = ((BigDecimal) obj).unscaledValue(); + BigDecimal retValue = ((BigDecimal) obj).setScale(9, RoundingMode.HALF_EVEN); + BigInteger data = retValue.unscaledValue(); byte[] bytes = UdfUtils.convertByteOrder(data.toByteArray()); //TODO: here is maybe overflow also, and may find a better way to handle byte[] value = new byte[16]; @@ -386,5 +385,6 @@ public abstract class BaseExecutor { } } - protected void updateOutputOffset(long offset) {} + protected void updateOutputOffset(long offset) { + } } diff --git a/regression-test/data/javaudf_p0/test_javaudf_decimal.out b/regression-test/data/javaudf_p0/test_javaudf_decimal.out index 50b61279c7..b1ad6275aa 100644 --- a/regression-test/data/javaudf_p0/test_javaudf_decimal.out +++ b/regression-test/data/javaudf_p0/test_javaudf_decimal.out @@ -21,3 +21,6 @@ 112 1456778.444443300 113 \N +-- !select_decimal_string -- +0E-9 + diff --git a/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalStringTest.java b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalStringTest.java new file mode 100644 index 0000000000..9d4faf6415 --- /dev/null +++ b/regression-test/java-udf-src/src/main/java/org/apache/doris/udf/DecimalStringTest.java @@ -0,0 +1,26 @@ +// 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. + +package org.apache.doris.udf; +import java.math.BigDecimal; +import org.apache.hadoop.hive.ql.exec.UDF; + +public class DecimalStringTest extends UDF { + public BigDecimal evaluate(BigDecimal value, String partition, String orderBy) { + return new BigDecimal(0.0); + } +} diff --git a/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy b/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy index 2ca1121eb8..406fb857f7 100644 --- a/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy +++ b/regression-test/suites/javaudf_p0/test_javaudf_decimal.groovy @@ -64,6 +64,16 @@ suite("test_javaudf_decimal") { sql """ DROP FUNCTION java_udf_decimal_test(decimal(27,9),decimal(27,9)); """ + + + sql """ DROP FUNCTION if exists java_udf_decimal_string_test(decimal(27,9),String,String); """ + sql """ CREATE FUNCTION java_udf_decimal_string_test(decimal(27,9),String,String) RETURNS decimal(27,9) PROPERTIES ( + "file"="file://${jarPath}", + "symbol"="org.apache.doris.udf.DecimalStringTest", + "type"="JAVA_UDF" + ); """ + qt_select_decimal_string """ SELECT java_udf_decimal_string_test(2.83645,'asd','a') as result; """ + sql """ DROP FUNCTION java_udf_decimal_string_test(decimal(27,9),String,String); """ } finally { try_sql("DROP TABLE IF EXISTS ${tableName}") }