From 7fe7c01125a57d9d9103cac89dba390888dfba90 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 25 Oct 2022 12:08:49 +0800 Subject: [PATCH] [Bug](decimal) Fix incorrect result for decimal multiply (#13591) Fix incorrect result for decimal multiply --- be/src/vec/functions/multiply.cpp | 6 +++- .../math_functions/test_multiply.out | 9 +++++ .../math_functions/test_multiply.groovy | 33 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/query_p0/sql_functions/math_functions/test_multiply.out create mode 100644 regression-test/suites/query_p0/sql_functions/math_functions/test_multiply.groovy diff --git a/be/src/vec/functions/multiply.cpp b/be/src/vec/functions/multiply.cpp index c6f3d88d598ebc..e55a8549e56490 100644 --- a/be/src/vec/functions/multiply.cpp +++ b/be/src/vec/functions/multiply.cpp @@ -48,8 +48,12 @@ struct MultiplyImpl { int8 sgn[size]; for (int i = 0; i < size; i++) { - sgn[i] = ((DecimalV2Value(a[i]).value() >= 0) == (DecimalV2Value(b[i]).value() >= 0)) + sgn[i] = ((DecimalV2Value(a[i]).value() > 0) && (DecimalV2Value(b[i]).value() > 0)) || + ((DecimalV2Value(a[i]).value() < 0) && + (DecimalV2Value(b[i]).value() < 0)) ? 1 + : ((DecimalV2Value(a[i]).value() == 0) || (DecimalV2Value(b[i]).value() == 0)) + ? 0 : -1; } diff --git a/regression-test/data/query_p0/sql_functions/math_functions/test_multiply.out b/regression-test/data/query_p0/sql_functions/math_functions/test_multiply.out new file mode 100644 index 00000000000000..4611fe72880f32 --- /dev/null +++ b/regression-test/data/query_p0/sql_functions/math_functions/test_multiply.out @@ -0,0 +1,9 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +0E-9 +0E-9 + +-- !select -- +\N +0E-9 + diff --git a/regression-test/suites/query_p0/sql_functions/math_functions/test_multiply.groovy b/regression-test/suites/query_p0/sql_functions/math_functions/test_multiply.groovy new file mode 100644 index 00000000000000..cdea8f00266476 --- /dev/null +++ b/regression-test/suites/query_p0/sql_functions/math_functions/test_multiply.groovy @@ -0,0 +1,33 @@ +// 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. + +suite("test_multiply") { + sql """ set enable_vectorized_engine = true """ + + def tableName = "test_multiply" + sql """ CREATE TABLE `${tableName}` ( + `user_id` LARGEINT NOT NULL COMMENT "用户id", + `col2` DECIMAL(27,9) COMMENT "数据灌入日期时间", + `col3` DECIMAL(16,8) COMMENT "数据灌入日期时间") + DUPLICATE KEY(`user_id`) DISTRIBUTED BY HASH(`user_id`) + PROPERTIES ( "replication_num" = "1" ); """ + + sql """ insert into `${tableName}` values(1,null,2.2); """ + sql """ insert into `${tableName}` values(1,0,0); """ + qt_select """ select COALESCE(col2, 0) * COALESCE(col3, 0) from `${tableName}`; """ + qt_select """ select col2 * col3 from `${tableName}`; """ +}