-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Feature](array-function) Support array_concat function #17436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // 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 <cstddef> | ||
|
|
||
| #include "vec/columns/column_array.h" | ||
| #include "vec/data_types/data_type_array.h" | ||
| #include "vec/functions/function.h" | ||
| #include "vec/functions/simple_function_factory.h" | ||
|
|
||
| namespace doris::vectorized { | ||
|
|
||
| // array_concat([1, 2], [7, 8], [5, 6]) -> [1, 2, 7, 8, 5, 6] | ||
| class FunctionArrayConcat : public IFunction { | ||
| public: | ||
| static constexpr auto name = "array_concat"; | ||
|
|
||
| static FunctionPtr create() { return std::make_shared<FunctionArrayConcat>(); } | ||
|
|
||
| String get_name() const override { return name; } | ||
|
|
||
| bool is_variadic() const override { return true; } | ||
|
|
||
| size_t get_number_of_arguments() const override { return 1; } | ||
|
|
||
| DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | ||
| DCHECK(arguments.size() > 0) | ||
| << "function: " << get_name() << ", arguments should not be empty"; | ||
| for (const auto& arg : arguments) { | ||
| DCHECK(is_array(arg)) << "argument for function array_concat should be DataTypeArray" | ||
| << " and argument is " << arg->get_name(); | ||
| } | ||
| return arguments[0]; | ||
| } | ||
|
|
||
| Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
| const size_t result, size_t input_rows_count) override { | ||
| DataTypePtr column_type = block.get_by_position(arguments[0]).type; | ||
| auto nested_type = assert_cast<const DataTypeArray&>(*column_type).get_nested_type(); | ||
| auto result_column = ColumnArray::create(nested_type->create_column(), | ||
| ColumnArray::ColumnOffsets::create()); | ||
| IColumn& result_nested_col = result_column->get_data(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could do |
||
| ColumnArray::Offsets64& column_offsets = result_column->get_offsets(); | ||
| column_offsets.resize(input_rows_count); | ||
|
|
||
| size_t total_size = 0; | ||
| for (size_t col : arguments) { | ||
| ColumnPtr src_column = | ||
| block.get_by_position(col).column->convert_to_full_column_if_const(); | ||
| const auto& src_column_array = check_and_get_column<ColumnArray>(*src_column); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should get the nested column of array, and caculate it's size, other wise, it's the row size of array not the inner nested row size of the array.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| total_size += src_column_array->get_data().size(); | ||
| } | ||
| result_nested_col.reserve(total_size); | ||
|
|
||
| size_t off = 0; | ||
| for (size_t row = 0; row < input_rows_count; ++row) { | ||
| for (size_t col : arguments) { | ||
| ColumnPtr src_column = | ||
| block.get_by_position(col).column->convert_to_full_column_if_const(); | ||
| const auto& src_column_array = check_and_get_column<ColumnArray>(*src_column); | ||
| const auto& src_column_offsets = src_column_array->get_offsets(); | ||
| const size_t length = src_column_offsets[row] - src_column_offsets[row - 1]; | ||
| result_nested_col.insert_range_from(src_column_array->get_data(), | ||
| src_column_offsets[row - 1], length); | ||
| off += length; | ||
| } | ||
| column_offsets[row] = off; | ||
| } | ||
|
|
||
| block.replace_by_position(result, std::move(result_column)); | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| void register_function_array_concat(SimpleFunctionFactory& factory) { | ||
| factory.register_function<FunctionArrayConcat>(); | ||
| } | ||
|
|
||
| } // namespace doris::vectorized | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| { | ||
| "title": "array_concat", | ||
| "language": "en" | ||
| } | ||
| --- | ||
|
|
||
| <!-- | ||
| 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. | ||
| --> | ||
|
|
||
| ## array_concat | ||
|
|
||
| <version since="1.2.3"> | ||
|
|
||
| array_concat | ||
|
|
||
| </version> | ||
|
|
||
| ### description | ||
|
|
||
| Concat all arrays passed in the arguments | ||
|
|
||
| #### Syntax | ||
|
|
||
| ```sql | ||
| Array<T> array_concat(Array<T>, ...) | ||
| ``` | ||
|
|
||
| #### Returned value | ||
|
|
||
| The concated array. | ||
|
|
||
| Type: Array. | ||
|
|
||
| ### notice | ||
|
|
||
| `Only supported in vectorized engine` | ||
|
|
||
| ### example | ||
|
|
||
| ``` | ||
| mysql> select array_concat([1, 2], [7, 8], [5, 6]); | ||
| +-----------------------------------------------------+ | ||
| | array_concat(ARRAY(1, 2), ARRAY(7, 8), ARRAY(5, 6)) | | ||
| +-----------------------------------------------------+ | ||
| | [1, 2, 7, 8, 5, 6] | | ||
| +-----------------------------------------------------+ | ||
| 1 row in set (0.02 sec) | ||
|
|
||
| mysql> select col2, col3, array_concat(col2, col3) from array_test; | ||
| +--------------+-----------+------------------------------+ | ||
| | col2 | col3 | array_concat(`col2`, `col3`) | | ||
| +--------------+-----------+------------------------------+ | ||
| | [1, 2, 3] | [3, 4, 5] | [1, 2, 3, 3, 4, 5] | | ||
| | [1, NULL, 2] | [NULL] | [1, NULL, 2, NULL] | | ||
| | [1, 2, 3] | NULL | NULL | | ||
| | [] | [] | [] | | ||
| +--------------+-----------+------------------------------+ | ||
| ``` | ||
|
|
||
| ### keywords | ||
|
|
||
| ARRAY,CONCAT,ARRAY_CONCAT |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| --- | ||
| { | ||
| "title": "array_concat", | ||
| "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. | ||
| --> | ||
|
|
||
| ## array_concat | ||
|
|
||
| <version since="1.2.3"> | ||
|
|
||
| array_concat | ||
|
|
||
| </version> | ||
|
|
||
| ### description | ||
|
|
||
| 将输入的所有数组拼接为一个数组 | ||
|
|
||
| #### Syntax | ||
|
|
||
| ```sql | ||
| Array<T> array_concat(Array<T>, ...) | ||
| ``` | ||
|
|
||
| #### Returned value | ||
|
|
||
| 拼接好的数组 | ||
|
|
||
| 类型: Array. | ||
|
|
||
| ### notice | ||
|
|
||
| `只支持在向量化引擎中使用` | ||
|
|
||
| ### example | ||
|
|
||
| ``` | ||
| mysql> select array_concat([1, 2], [7, 8], [5, 6]); | ||
| +-----------------------------------------------------+ | ||
| | array_concat(ARRAY(1, 2), ARRAY(7, 8), ARRAY(5, 6)) | | ||
| +-----------------------------------------------------+ | ||
| | [1, 2, 7, 8, 5, 6] | | ||
| +-----------------------------------------------------+ | ||
| 1 row in set (0.02 sec) | ||
|
|
||
| mysql> select col2, col3, array_concat(col2, col3) from array_test; | ||
| +--------------+-----------+------------------------------+ | ||
| | col2 | col3 | array_concat(`col2`, `col3`) | | ||
| +--------------+-----------+------------------------------+ | ||
| | [1, 2, 3] | [3, 4, 5] | [1, 2, 3, 3, 4, 5] | | ||
| | [1, NULL, 2] | [NULL] | [1, NULL, 2, NULL] | | ||
| | [1, 2, 3] | NULL | NULL | | ||
| | [] | [] | [] | | ||
| +--------------+-----------+------------------------------+ | ||
| ``` | ||
|
|
||
|
|
||
| ### keywords | ||
|
|
||
| ARRAY,CONCAT,ARRAY_CONCAT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also need add this doc to sidebar
docs/sidebars.json