HappenLee commented on code in PR #47102: URL: https://github.com/apache/doris/pull/47102#discussion_r1919514860
########## be/src/vec/functions/function_format.cpp: ########## @@ -0,0 +1,241 @@ +// 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 <glog/logging.h> + +#include <cstdio> +#include <regex> +#include <vector> + +#include "common/status.h" +#include "vec/columns/column.h" +#include "vec/columns/column_vector.h" +#include "vec/common/assert_cast.h" +#include "vec/core/types.h" +#include "vec/data_types/data_type_number.h" +#include "vec/functions/cast_type_to_either.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { + +class FunctionFormatNumber : public IFunction { +public: + static constexpr auto name = "format_number"; + + static constexpr const char* UNITS[6] = {"", "K", "M", "B", "T", "Q"}; + + static FunctionPtr create() { return std::make_shared<FunctionFormatNumber>(); } + + String get_name() const override { return name; } + + size_t get_number_of_arguments() const override { return 1; } + + bool is_variadic() const override { return false; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return std::make_shared<DataTypeString>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + auto column = block.get_by_position(arguments[0]).column; + const auto& column_data = + assert_cast<const ColumnVector<Float64>*>(column.get())->get_data(); + auto col_res = ColumnString::create(); + fmt::memory_buffer buffer; + + for (auto i = 0; i < input_rows_count; ++i) { + auto res_data = format_number(buffer, column_data[i]); + col_res->insert_data(res_data.data(), res_data.length()); + } + block.replace_by_position(result, std::move(col_res)); + return Status::OK(); + } + + std::string format_number(fmt::memory_buffer& buffer, double number) const { + buffer.clear(); + double abs_number = std::abs(number); + int unit_index = 0; + while (abs_number >= 1000 && unit_index < 5) { + abs_number /= 1000; + ++unit_index; + } + if (number < 0) { + fmt::format_to(buffer, "-"); + } + if (abs_number == 1) { + fmt::format_to(buffer, "{}", abs_number); + } else if (abs_number < 10) { + fmt::format_to(buffer, "{:.2f}", abs_number); Review Comment: why the format .2f ? each format need comment to desc -- 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