Yukang-Lian commented on code in PR #14396:
URL: https://github.com/apache/doris/pull/14396#discussion_r1027505309


##########
docs/zh-CN/docs/sql-manual/sql-functions/width-bucket.md:
##########
@@ -0,0 +1,153 @@
+---
+{
+    "title": "width_bucket",
+    "language": "zh-CN"
+}
+---
+
+<!-- 
+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.
+-->
+
+## width_bucket
+
+### Description
+
+构造等宽直方图,其中直方图范围被划分为相同大小的区间,并在计算后返回表达式的值所在的桶号。该函数返回一个整数值或空值(如果任何输入为空值则返回空值)。
+
+#### Syntax
+
+```sql
+width_bucket(expr, min_value, max_value, num_buckets)
+```
+
+#### Arguments
+`expr` -
+创建直方图的表达式。此表达式必须计算为数值或可隐式转换为数值的值。

Review Comment:
   `date` can be implicitly converted to a numeric value. Actually, `date` is 
`Int64`. 
https://github.com/apache/doris/blob/ff197b0fa537e076ff1a0b5c43437552fb6d8ba1/be/src/vec/core/types.h#L262-L265



##########
be/src/vec/functions/function_width_bucket.cpp:
##########
@@ -0,0 +1,156 @@
+// 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.
+
+#include "vec/columns/columns_number.h"
+#include "vec/data_types/data_type_nullable.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/function.h"
+#include "vec/functions/function_helpers.h"
+#include "vec/functions/simple_function_factory.h"
+
+namespace doris::vectorized {
+class FunctionWidthBucket : public IFunction {
+public:
+    static constexpr auto name = "width_bucket";
+    static FunctionPtr create() { return 
std::make_shared<FunctionWidthBucket>(); }
+
+    /// Get function name.
+    String get_name() const override { return name; }
+
+    bool is_variadic() const override { return false; }
+
+    size_t get_number_of_arguments() const override { return 4; }
+
+    DataTypePtr get_return_type_impl(const DataTypes& arguments) const 
override {
+        if (arguments[0]->is_nullable()) {
+            return make_nullable(std::make_shared<DataTypeInt64>());
+        } else {
+            return std::make_shared<DataTypeInt64>();
+        }
+    }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        size_t result, size_t input_rows_count) override {
+        ColumnPtr expr_ptr =
+                
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
+        ColumnPtr min_value_ptr =
+                
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
+        ColumnPtr max_value_ptr =
+                
block.get_by_position(arguments[2]).column->convert_to_full_column_if_const();
+        ColumnPtr num_buckets_ptr =
+                
block.get_by_position(arguments[3]).column->convert_to_full_column_if_const();
+        int64_t num_buckets = num_buckets_ptr->get_int(0);
+
+        auto nested_column_ptr = ColumnInt64::create(input_rows_count, 0);
+        DataTypePtr expr_type = block.get_by_position(arguments[0]).type;
+
+        _execute_by_type(*expr_ptr, *min_value_ptr, *max_value_ptr, 
num_buckets, *nested_column_ptr,
+                         expr_type);
+
+        WhichDataType which(remove_nullable(expr_type));
+        if (which.is_nullable()) {
+            auto dest_column_ptr =
+                    ColumnNullable::create(std::move(nested_column_ptr),
+                                           
ColumnUInt8::create(nested_column_ptr->size(), 0));
+            block.replace_by_position(result, std::move(dest_column_ptr));
+        } else {
+            block.replace_by_position(result, std::move(nested_column_ptr));
+        }
+        return Status::OK();
+    }
+
+private:
+    template <typename ColumnType>
+    void _execute(const IColumn& expr_column, const IColumn& min_value_column,
+                  const IColumn& max_value_column, const int64_t num_buckets,
+                  IColumn& nested_column) {
+        const ColumnType& expr_column_concrete = reinterpret_cast<const 
ColumnType&>(expr_column);
+        const ColumnType& min_value_column_concrete =
+                reinterpret_cast<const ColumnType&>(min_value_column);
+        const ColumnType& max_value_column_concrete =
+                reinterpret_cast<const ColumnType&>(max_value_column);
+        ColumnInt64& nested_column_concrete = 
reinterpret_cast<ColumnInt64&>(nested_column);
+
+        auto min_value = min_value_column_concrete.get_data()[0];
+        auto max_value = max_value_column_concrete.get_data()[0];
+
+        size_t input_rows_count = expr_column.size();
+
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (expr_column_concrete.get_data()[i] < min_value) {
+                continue;
+            } else if (expr_column_concrete.get_data()[i] >= max_value) {
+                nested_column_concrete.get_data()[i] = num_buckets + 1;
+            } else {
+                if ((max_value - min_value) / num_buckets == 0) {
+                    continue;
+                }
+                nested_column_concrete.get_data()[i] =
+                        (int64_t)(1 + (expr_column_concrete.get_data()[i] - 
min_value) /
+                                              ((max_value - min_value) / 
num_buckets));
+            }
+            LOG(WARNING) << std::to_string(i) + "   " +

Review Comment:
   This is used for debugging. I forgot to remove it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to