Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](DECIMAL) error DECIMAL cat to BOOLEAN #33868

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions be/src/vec/data_types/data_type_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,21 @@ void convert_from_decimals(RealTo* dst, const RealFrom* src, UInt32 scale_from,
// from decimal to integer
MaxFieldType multiplier = DataTypeDecimal<MaxFieldType>::get_scale_multiplier(scale_from);
for (size_t i = 0; i < size; i++) {
auto tmp = static_cast<MaxFieldType>(src[i]).value / multiplier.value;
if constexpr (narrow_integral) {
if (tmp < min_result.value || tmp > max_result.value) {
throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
"Arithmetic overflow, convert failed from {}, "
"expected data is [{}, {}]",
tmp, min_result.value, max_result.value);
// uint8_t now use as boolean in doris
if constexpr (std::is_same_v<RealTo, UInt8>) {
dst[i] = static_cast<MaxFieldType>(src[i]).value != 0;
} else {
auto tmp = static_cast<MaxFieldType>(src[i]).value / multiplier.value;
if constexpr (narrow_integral) {
if (tmp < min_result.value || tmp > max_result.value) {
throw Exception(ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
"Arithmetic overflow, convert failed from {}, "
"expected data is [{}, {}]",
tmp, min_result.value, max_result.value);
}
}
dst[i] = tmp;
}
dst[i] = tmp;
}
}

Expand Down
14 changes: 14 additions & 0 deletions regression-test/data/correctness/test_cast_decimalv3_as_bool.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select1 --
0.000 13131.213132100 0E-16
0.000 2131231.231000000 2.3323000E-9
3.141 0E-9 123123.2131231231322130

-- !select2 --
false true false
false true true
true false true

-- !select3 --
true 1 true false

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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_cast_decimalv3_as_bool") {
sql """ DROP TABLE IF EXISTS cast_decimalv3_as_bool """
sql """
CREATE TABLE IF NOT EXISTS cast_decimalv3_as_bool (
`id` int(11) ,
`k1` decimalv3(9,3) ,
`k2` decimalv3(18,9) ,
`k3` decimalv3(38,16) ,
)
UNIQUE KEY(`id`)
DISTRIBUTED BY HASH(`id`) BUCKETS 10
PROPERTIES (
"enable_unique_key_merge_on_write" = "true",
"replication_num" = "1"
);
"""
sql """
set enable_nereids_planner=true,enable_fold_constant_by_be = false
"""
sql """
INSERT INTO cast_decimalv3_as_bool VALUES
(1,0.00001,13131.2131321,0.000000000000000000),
(2,0.00000,2131231.231,0.0000000023323),
(3,3.141414,0.0000000000,123123.213123123132213);
"""
qt_select1 """
select k1,k2,k3 from cast_decimalv3_as_bool order by id
"""
qt_select2 """
select cast(k1 as boolean), cast(k2 as boolean) , cast(k3 as boolean) from cast_decimalv3_as_bool order by id
"""
qt_select3"""
select cast(3.00001 as boolean), cast(cast(3.00001 as boolean) as int),cast(0.001 as boolean),cast(0.000 as boolean);
"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ suite("test_case_function_null", "query,p0,arrow_flight_sql") {

qt_sql_case1 """
SELECT SUM(
CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.4716 AS BOOLEAN)))
CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.000 AS BOOLEAN)))
WHEN ((CAST('-1530390546' AS VARCHAR)) LIKE ('-1678299490'))
THEN (- (+ case_null2.c0))
WHEN CASE (NULL IN (NULL))
Expand All @@ -199,7 +199,7 @@ suite("test_case_function_null", "query,p0,arrow_flight_sql") {
"""

qt_sql_case2 """
SELECT SUM(CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.4716 AS BOOLEAN)))
SELECT SUM(CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.000 AS BOOLEAN)))
WHEN ((CAST('-1530390546' AS VARCHAR)) LIKE ('-1678299490'))
THEN (- (+ case_null2.c0))
END)
Expand All @@ -211,7 +211,7 @@ suite("test_case_function_null", "query,p0,arrow_flight_sql") {

qt_sql_case1 """
SELECT SUM(
CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.4716 AS BOOLEAN)))
CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.000 AS BOOLEAN)))
WHEN ((CAST('-1530390546' AS VARCHAR)) LIKE ('-1678299490'))
THEN (- (+ case_null2.c0))
WHEN CASE (NULL IN (NULL))
Expand All @@ -222,7 +222,7 @@ suite("test_case_function_null", "query,p0,arrow_flight_sql") {
"""

qt_sql_case2 """
SELECT SUM(CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.4716 AS BOOLEAN)))
SELECT SUM(CASE (((NULL BETWEEN NULL AND NULL)) and (CAST(0.000 AS BOOLEAN)))
WHEN ((CAST('-1530390546' AS VARCHAR)) LIKE ('-1678299490'))
THEN (- (+ case_null2.c0))
END)
Expand Down
Loading