Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions be/src/vec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ set(VEC_FILES
functions/array/function_array_constructor.cpp
functions/array/function_array_with_constant.cpp
functions/array/function_array_apply.cpp
functions/array/function_array_concat.cpp
exprs/table_function/vexplode_json_array.cpp
functions/math.cpp
functions/function_bitmap.cpp
Expand Down
93 changes: 93 additions & 0 deletions be/src/vec/functions/array/function_array_concat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the Apache Software Foundation (ASF) under one
Copy link
Member

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

// 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could do reserve for result_nested_col for more efficiency

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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
2 changes: 2 additions & 0 deletions be/src/vec/functions/array/function_array_register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void register_function_array_popback(SimpleFunctionFactory&);
void register_function_array_with_constant(SimpleFunctionFactory&);
void register_function_array_constructor(SimpleFunctionFactory&);
void register_function_array_apply(SimpleFunctionFactory&);
void register_function_array_concat(SimpleFunctionFactory&);

void register_function_array(SimpleFunctionFactory& factory) {
register_function_array_element(factory);
Expand All @@ -64,6 +65,7 @@ void register_function_array(SimpleFunctionFactory& factory) {
register_function_array_with_constant(factory);
register_function_array_constructor(factory);
register_function_array_apply(factory);
register_function_array_concat(factory);
}

} // 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
1 change: 1 addition & 0 deletions docs/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@
"sql-manual/sql-functions/array-functions/array_enumerate",
"sql-manual/sql-functions/array-functions/array_popback",
"sql-manual/sql-functions/array-functions/array_compact",
"sql-manual/sql-functions/array-functions/array_concat",
"sql-manual/sql-functions/array-functions/arrays_overlap",
"sql-manual/sql-functions/array-functions/countequal",
"sql-manual/sql-functions/array-functions/element_at"
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,8 @@ private void analyzeArrayFunction(Analyzer analyzer) throws AnalysisException {
|| fnName.getFunction().equalsIgnoreCase("array_union")
|| fnName.getFunction().equalsIgnoreCase("array_except")
|| fnName.getFunction().equalsIgnoreCase("array_intersect")
|| fnName.getFunction().equalsIgnoreCase("arrays_overlap")) {
|| fnName.getFunction().equalsIgnoreCase("arrays_overlap")
|| fnName.getFunction().equalsIgnoreCase("array_concat")) {
Type[] childTypes = collectChildReturnTypes();
Type compatibleType = childTypes[0];
for (int i = 1; i < childTypes.length; ++i) {
Expand Down Expand Up @@ -1494,7 +1495,8 @@ private void analyzeNestedFunction() {
|| fnName.getFunction().equalsIgnoreCase("array_popback")
|| fnName.getFunction().equalsIgnoreCase("reverse")
|| fnName.getFunction().equalsIgnoreCase("%element_slice%")
|| fnName.getFunction().equalsIgnoreCase("array_except")) {
|| fnName.getFunction().equalsIgnoreCase("array_except")
|| fnName.getFunction().equalsIgnoreCase("array_concat")) {
if (children.size() > 0) {
this.type = children.get(0).getType();
}
Expand Down
19 changes: 19 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,25 @@
[['array_apply'], 'ARRAY_DATETIMEV2', ['ARRAY_DATETIMEV2', 'VARCHAR', 'DATETIMEV2'], ''],
[['array_apply'], 'ARRAY_DATEV2', ['ARRAY_DATEV2', 'VARCHAR', 'DATEV2'], ''],

[['array_concat'], 'ARRAY_BOOLEAN', ['ARRAY_BOOLEAN', '...'], ''],
[['array_concat'], 'ARRAY_TINYINT', ['ARRAY_TINYINT', '...'], ''],
[['array_concat'], 'ARRAY_SMALLINT', ['ARRAY_SMALLINT', '...'], ''],
[['array_concat'], 'ARRAY_INT', ['ARRAY_INT', '...'], ''],
[['array_concat'], 'ARRAY_BIGINT', ['ARRAY_BIGINT', '...'], ''],
[['array_concat'], 'ARRAY_LARGEINT', ['ARRAY_LARGEINT', '...'], ''],
[['array_concat'], 'ARRAY_FLOAT', ['ARRAY_FLOAT', '...'], ''],
[['array_concat'], 'ARRAY_DOUBLE', ['ARRAY_DOUBLE', '...'], ''],
[['array_concat'], 'ARRAY_DECIMALV2', ['ARRAY_DECIMALV2', '...'], ''],
[['array_concat'], 'ARRAY_DECIMAL32', ['ARRAY_DECIMAL32', '...'], ''],
[['array_concat'], 'ARRAY_DECIMAL64', ['ARRAY_DECIMAL64', '...'], ''],
[['array_concat'], 'ARRAY_DECIMAL128', ['ARRAY_DECIMAL128', '...'], ''],
[['array_concat'], 'ARRAY_DATETIME', ['ARRAY_DATETIME', '...'], ''],
[['array_concat'], 'ARRAY_DATE', ['ARRAY_DATE', '...'], ''],
[['array_concat'], 'ARRAY_DATETIMEV2', ['ARRAY_DATETIMEV2', '...'], ''],
[['array_concat'], 'ARRAY_DATEV2', ['ARRAY_DATEV2', '...'], ''],
[['array_concat'], 'ARRAY_VARCHAR', ['ARRAY_VARCHAR', '...'], ''],
[['array_concat'], 'ARRAY_STRING', ['ARRAY_STRING', '...'], ''],

[['array_except'], 'ARRAY_BOOLEAN', ['ARRAY_BOOLEAN', 'ARRAY_BOOLEAN'], ''],
[['array_except'], 'ARRAY_TINYINT', ['ARRAY_TINYINT', 'ARRAY_TINYINT'], ''],
[['array_except'], 'ARRAY_SMALLINT', ['ARRAY_SMALLINT', 'ARRAY_SMALLINT'], ''],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,50 @@
8 [8] true
9 [9] true

-- !select --
1 [1, 2, 3, 1, 2]
2 [4, 5]
3 \N
4 [1, 2, 3, 4, 5, 4, 3, 2, 1]
5 \N
6 \N
7 \N
8 [1, 2, 3, 3, 4, 4, NULL, 1, 2, 2, 3]
9 [1, 2, 3, 1, 2]

-- !select --
1 [1, 2, 3, 1, NULL, 2, 1, 2, NULL]
2 [4, 1, NULL, 2, 5, NULL]
3 \N
4 [1, 2, 3, 4, 5, 4, 3, 2, 1, 1, NULL, 2, NULL]
5 \N
6 \N
7 \N
8 [1, 2, 3, 3, 4, 4, NULL, 1, NULL, 2, 1, 2, 2, 3, NULL]
9 [1, 2, 3, 1, NULL, 2, 1, 2, NULL]

-- !select --
1 [2023-02-05, 2023-02-06, 2023-02-07, 2023-02-06]
2 [2023-01-05, 2023-01-06, 2023-01-07, 2023-01-06]
3 \N
4 \N
5 \N
6 \N
7 \N
8 \N
9 \N

-- !select --
1 [2022-10-15 10:30:00.999, 2022-08-31 12:00:00.999, 2022-10-16 10:30:00.999, 2022-08-31 12:00:00.999, 2023-03-05 10:30:00.999]
2 [2022-11-15 10:30:00.999, 2022-01-31 12:00:00.999, 2022-11-16 10:30:00.999, 2022-01-31 12:00:00.999, 2023-03-05 10:30:00.999]
3 \N
4 \N
5 \N
6 \N
7 \N
8 \N
9 \N

-- !select --
\N \N
-1 \N
Expand Down
Loading