morningman commented on code in PR #14396: URL: https://github.com/apache/doris/pull/14396#discussion_r1027306020
########## 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: Do not print WARNING log here -- 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