diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java index 6ee6923c2aab98..56c8a1f6c40493 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmetic.java @@ -33,6 +33,7 @@ import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral; import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; +import org.apache.doris.nereids.types.DecimalV2Type; import org.apache.doris.nereids.types.DecimalV3Type; import org.apache.doris.nereids.types.DoubleType; import org.apache.doris.nereids.types.FloatType; @@ -89,12 +90,12 @@ public static Expression abs(DoubleLiteral literal) { @ExecFunction(name = "abs") public static Expression abs(DecimalLiteral literal) { - return new DecimalLiteral(literal.getValue().abs()); + return new DecimalLiteral((DecimalV2Type) literal.getDataType(), literal.getValue().abs()); } @ExecFunction(name = "abs") public static Expression abs(DecimalV3Literal literal) { - return new DecimalV3Literal(literal.getValue().abs()); + return new DecimalV3Literal((DecimalV3Type) literal.getDataType(), literal.getValue().abs()); } /** diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmeticTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmeticTest.java new file mode 100644 index 00000000000000..5e2f70d91dd89a --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/NumericArithmeticTest.java @@ -0,0 +1,47 @@ +// 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.nereids.trees.expressions.functions.executable; + +import org.apache.doris.nereids.trees.expressions.literal.DecimalLiteral; +import org.apache.doris.nereids.trees.expressions.literal.DecimalV3Literal; +import org.apache.doris.nereids.types.DecimalV2Type; +import org.apache.doris.nereids.types.DecimalV3Type; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; + +public class NumericArithmeticTest { + + @Test + public void testDecimalV3Abs() { + DecimalV3Literal decimalV3Literal = new DecimalV3Literal( + DecimalV3Type.createDecimalV3Type(10, 0), new BigDecimal(1)); + DecimalV3Literal result = (DecimalV3Literal) NumericArithmetic.abs(decimalV3Literal); + Assertions.assertEquals(DecimalV3Type.createDecimalV3Type(10, 0), result.getDataType()); + } + + @Test + public void testDecimalV2Abs() { + DecimalLiteral decimalV3Literal = new DecimalLiteral( + DecimalV2Type.createDecimalV2Type(10, 0), new BigDecimal(1)); + DecimalLiteral result = (DecimalLiteral) NumericArithmetic.abs(decimalV3Literal); + Assertions.assertEquals(DecimalV2Type.createDecimalV2Type(10, 0), result.getDataType()); + } +}