From 899c2457140330b5ab3a63dc221d4581ba8610fd Mon Sep 17 00:00:00 2001 From: zhangstar333 <2561612514@qq.com> Date: Wed, 20 Sep 2023 14:53:21 +0800 Subject: [PATCH] [bug](function) fix width bucket function return wrong result --- be/src/vec/functions/function_width_bucket.cpp | 5 +++-- .../test_width_bucket_function.out | 15 +++++++++++++++ .../test_width_bucket_function.groovy | 6 ++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/be/src/vec/functions/function_width_bucket.cpp b/be/src/vec/functions/function_width_bucket.cpp index 42ad8156ed7f0c..cf93e2c8e56020 100644 --- a/be/src/vec/functions/function_width_bucket.cpp +++ b/be/src/vec/functions/function_width_bucket.cpp @@ -98,6 +98,7 @@ class FunctionWidthBucket : public IFunction { for (size_t i = 0; i < input_rows_count; ++i) { auto min_value = min_value_column_concrete.get_data()[i]; auto max_value = max_value_column_concrete.get_data()[i]; + auto average_value = (max_value - min_value) / (1.0 * num_buckets); if (expr_column_concrete.get_data()[i] < min_value) { continue; } else if (expr_column_concrete.get_data()[i] >= max_value) { @@ -107,8 +108,8 @@ class FunctionWidthBucket : public IFunction { continue; } nested_column_concrete.get_data()[i] = - (int64_t)(1 + (expr_column_concrete.get_data()[i] - min_value) / - ((max_value - min_value) / num_buckets)); + (int64_t)(1 + + (expr_column_concrete.get_data()[i] - min_value) / average_value); } } } diff --git a/regression-test/data/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.out b/regression-test/data/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.out index 6f2348ac3e4479..bf7294504b21f5 100644 --- a/regression-test/data/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.out +++ b/regression-test/data/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.out @@ -93,3 +93,18 @@ 7 5 8 \N +-- !select_width_bucket_1 -- +10 + +-- !select_width_bucket_2 -- +10 + +-- !select_width_bucket_3 -- +10 + +-- !select_width_bucket_4 -- +10 + +-- !select_width_bucket_5 -- +10 + diff --git a/regression-test/suites/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.groovy b/regression-test/suites/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.groovy index dca36cb1da4c9f..7ed23d697e3276 100644 --- a/regression-test/suites/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/width_bucket_fuctions/test_width_bucket_function.groovy @@ -83,4 +83,10 @@ suite("test_width_bucket_function") { qt_select "SELECT k1, width_bucket(v1, date('2023-11-18'), date('2027-11-18'), 4) FROM ${tableName2} ORDER BY k1" qt_select "SELECT k1, width_bucket(v2, 200000, 600000, 4) FROM ${tableName2} ORDER BY k1" qt_select "SELECT k1, width_bucket(v3, 200000, 600000, 4) FROM ${tableName2} ORDER BY k1" + + qt_select_width_bucket_1 "select width_bucket(10,0,11,10);" + qt_select_width_bucket_2 "select width_bucket(cast(10 as int),0,11,10);" + qt_select_width_bucket_3 "select width_bucket(10.0,0,11,10);" + qt_select_width_bucket_4 "select width_bucket(10,0,10.1,10);" + qt_select_width_bucket_5 "select width_bucket(10,0,10.10,10);" } \ No newline at end of file