cambyzju commented on code in PR #11406: URL: https://github.com/apache/doris/pull/11406#discussion_r937500931
########## be/src/vec/functions/array/function_array_join.h: ########## @@ -0,0 +1,261 @@ +// 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. +#pragma once + +#include "vec/columns/column_array.h" +#include "vec/columns/column_const.h" +#include "vec/data_types/data_type_array.h" +#include "vec/data_types/data_type_number.h" +#include "vec/data_types/data_type_string.h" +#include "vec/functions/function.h" +#include "vec/functions/function_helpers.h" + +namespace doris::vectorized { + +class FunctionArrayJoin : public IFunction { +public: + static constexpr auto name = "array_join"; + static FunctionPtr create() { return std::make_shared<FunctionArrayJoin>(); } + using NullMapType = PaddedPODArray<UInt8>; + + /// Get function name. + String get_name() const override { return name; } + + bool is_variadic() const override { return true; } + + size_t get_number_of_arguments() const override { return 3; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + DCHECK(is_array(arguments[0])) + << "first argument for function: " << name << " should be DataTypeArray" + << " and arguments[0] is " << arguments[0]->get_name(); + DCHECK(is_string_or_fixed_string(arguments[1])) + << "second argument for function: " << name << " should be DataTypeString" + << ", and arguments[1] is " << arguments[1]->get_name(); + if (arguments.size() > 2) { + DCHECK(is_string_or_fixed_string(arguments[2])) + << "third argument for function: " << name << " should be DataTypeString" + << ", and arguments[2] is " << arguments[2]->get_name(); + } + + return std::make_shared<DataTypeString>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + ColumnPtr src_column = + block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); + const auto& src_column_array = check_and_get_column<ColumnArray>(*src_column); + DCHECK(src_column_array != nullptr); + const auto& src_offsets = src_column_array->get_offsets(); + const auto* src_nested_column = &src_column_array->get_data(); + DCHECK(src_nested_column != nullptr); + + const NullMapType* src_null_map = nullptr; + if (src_nested_column->is_nullable()) { + const ColumnNullable* src_nested_nullable_col = + check_and_get_column<ColumnNullable>(*src_nested_column); + src_nested_column = src_nested_nullable_col->get_nested_column_ptr(); + src_null_map = &src_nested_nullable_col->get_null_map_column().get_data(); + } + + ColumnPtr sep_column = + block.get_by_position(arguments[1]).column->convert_to_full_column_if_const(); + ColumnPtr null_replace_column = + (arguments.size() > 2 ? block.get_by_position(arguments[2]) + .column->convert_to_full_column_if_const() + : nullptr); + + std::string sep_str = _get_string_from_column(sep_column); + std::string null_replace_str = _get_string_from_column(null_replace_column); + + DataTypePtr src_column_type = block.get_by_position(arguments[0]).type; + auto nested_type = assert_cast<const DataTypeArray&>(*src_column_type).get_nested_type(); + + auto dest_column_ptr = ColumnString::create(); + DCHECK(dest_column_ptr != nullptr); + dest_column_ptr->reserve(input_rows_count); + + auto res_val = _execute_by_type(*src_nested_column, src_offsets, src_null_map, sep_str, + null_replace_str, nested_type, dest_column_ptr); + if (!res_val) { + return Status::RuntimeError( + fmt::format("execute failed or unsupported types for function {}({},{},{})", + get_name(), block.get_by_position(arguments[0]).type->get_name(), + block.get_by_position(arguments[1]).type->get_name(), + block.get_by_position(arguments[2]).type->get_name())); + } + + block.replace_by_position(result, std::move(dest_column_ptr)); + return Status::OK(); + } + +private: + std::string _get_string_from_column(const ColumnPtr& column_ptr) { + if (!column_ptr) { + return std::string(""); + } + const ColumnString* column_string_ptr = check_and_get_column<ColumnString>(*column_ptr); + StringRef str_ref = column_string_ptr->get_data_at(0); + std::string str(str_ref.data, str_ref.size); + return str; + } + + void _get_result_string(std::vector<std::string>& str_vec, const std::string& sep_str, + std::string& result_str) { + for (size_t i = 0; i < str_vec.size(); ++i) { + if (i == str_vec.size() - 1) { + result_str += str_vec[i]; + } else { + result_str += str_vec[i] + sep_str; + } + } + return; + } + + template <typename ColumnType> + bool _execute_number(const IColumn& src_column, const ColumnArray::Offsets& src_offsets, + const NullMapType* src_null_map, const std::string& sep_str, + const std::string& null_replace_str, DataTypePtr& nested_type, + ColumnString* dest_column_ptr) { + using NestType = typename ColumnType::value_type; + bool is_decimal = IsDecimalNumber<NestType>; + + const ColumnType* src_data_concrete = reinterpret_cast<const ColumnType*>(&src_column); + if (!src_data_concrete) { + return false; + } + + ColumnArray::Offset prev_src_offset = 0; + for (auto curr_src_offset : src_offsets) { + std::string result_str; + std::vector<std::string> str_vec; Review Comment: append result directly, no need to use str_vec -- 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