xy720 commented on code in PR #14115: URL: https://github.com/apache/doris/pull/14115#discussion_r1020002617
########## be/src/vec/functions/array/function_array_with_constant.cpp: ########## @@ -0,0 +1,97 @@ +// 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/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/functions/function.h" +#include "vec/functions/function_helpers.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { + +/* array_with_constant(num, T) - return array of constants with length num. + * array_with_constant(2, 'xxx') = ['xxx', 'xxx'] + */ +class FunctionArrayWithConstant : public IFunction { +public: + static constexpr auto name = "array_with_constant"; + static FunctionPtr create() { return std::make_shared<FunctionArrayWithConstant>(); } + + /// 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 2; } + + // need handle null cases + bool use_default_implementation_for_nulls() const override { return false; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + if (arguments[1]->is_nullable()) { + return make_nullable(std::make_shared<DataTypeArray>(make_nullable(arguments[1]))); + } + return std::make_shared<DataTypeArray>(make_nullable(arguments[1])); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + auto num = block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); + auto value = block.get_by_position(arguments[1]).column->convert_to_full_column_if_const(); + auto offsets_col = ColumnVector<ColumnArray::Offset64>::create(); + ColumnArray::Offsets64& offsets = offsets_col->get_data(); + offsets.reserve(input_rows_count); + ColumnArray::Offset64 offset = 0; + std::vector<uint32_t> array_sizes; + array_sizes.reserve(input_rows_count); + for (size_t i = 0; i < input_rows_count; ++i) { + auto array_size = num->get_int(i); + if (UNLIKELY(array_size < 0)) { + return Status::RuntimeError("Array size can not be negative in function:" + + get_name()); + } + offset += array_size; + offsets.push_back(offset); + array_sizes.push_back(array_size); + } + auto clone = value->clone_empty(); + clone->reserve(input_rows_count); + value->replicate(array_sizes.data(), offset, *clone->assume_mutable().get()); + if (value->is_nullable()) { Review Comment: The same above. ########## regression-test/suites/query_p0/sql_functions/array_functions/test_array_functions.groovy: ########## @@ -70,4 +70,8 @@ suite("test_array_functions") { qt_select "SELECT k1, array_popback(k5) from ${tableName} ORDER BY k1" qt_select "SELECT k1, array_popback(k6) from ${tableName} ORDER BY k1" qt_select "SELECT k1, array_popback(k7) from ${tableName} ORDER BY k1" + qt_select "SELECT k1, array_with_constant(3, k1) from ${tableName} ORDER BY k1" + qt_select "SELECT k1, array_with_constant(10, null) from ${tableName} ORDER BY k1" Review Comment: Why the result is null here? ########## be/src/vec/functions/array/function_array_with_constant.cpp: ########## @@ -0,0 +1,97 @@ +// 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/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/functions/function.h" +#include "vec/functions/function_helpers.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { + +/* array_with_constant(num, T) - return array of constants with length num. + * array_with_constant(2, 'xxx') = ['xxx', 'xxx'] + */ +class FunctionArrayWithConstant : public IFunction { +public: + static constexpr auto name = "array_with_constant"; + static FunctionPtr create() { return std::make_shared<FunctionArrayWithConstant>(); } + + /// 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 2; } + + // need handle null cases + bool use_default_implementation_for_nulls() const override { return false; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + if (arguments[1]->is_nullable()) { Review Comment: Why should we make a branch by `value->is_nullable()`? If k1 is nullable, when k1 is null, It seems that the result will be [null, null, null]. So the return value will never be null. ########## be/src/vec/functions/array/function_array_with_constant.cpp: ########## @@ -0,0 +1,97 @@ +// 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/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/functions/function.h" +#include "vec/functions/function_helpers.h" +#include "vec/functions/simple_function_factory.h" + +namespace doris::vectorized { + +/* array_with_constant(num, T) - return array of constants with length num. + * array_with_constant(2, 'xxx') = ['xxx', 'xxx'] + */ +class FunctionArrayWithConstant : public IFunction { +public: + static constexpr auto name = "array_with_constant"; + static FunctionPtr create() { return std::make_shared<FunctionArrayWithConstant>(); } + + /// 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 2; } + + // need handle null cases + bool use_default_implementation_for_nulls() const override { return false; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + if (arguments[1]->is_nullable()) { + return make_nullable(std::make_shared<DataTypeArray>(make_nullable(arguments[1]))); + } + return std::make_shared<DataTypeArray>(make_nullable(arguments[1])); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + auto num = block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); + auto value = block.get_by_position(arguments[1]).column->convert_to_full_column_if_const(); + auto offsets_col = ColumnVector<ColumnArray::Offset64>::create(); + ColumnArray::Offsets64& offsets = offsets_col->get_data(); + offsets.reserve(input_rows_count); + ColumnArray::Offset64 offset = 0; + std::vector<uint32_t> array_sizes; + array_sizes.reserve(input_rows_count); + for (size_t i = 0; i < input_rows_count; ++i) { + auto array_size = num->get_int(i); + if (UNLIKELY(array_size < 0)) { + return Status::RuntimeError("Array size can not be negative in function:" + + get_name()); + } + offset += array_size; + offsets.push_back(offset); + array_sizes.push_back(array_size); + } + auto clone = value->clone_empty(); + clone->reserve(input_rows_count); + value->replicate(array_sizes.data(), offset, *clone->assume_mutable().get()); + if (value->is_nullable()) { + auto nested_column = ColumnNullable::create(clone->assume_mutable(), Review Comment: If `value` is ColumnNullable, then `clone` is ColumnNullable,too. May be it is not right to put a ColumnNullable inside another ColumnNullable. No need to wrap a ColumnNullable outside. -- 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