[feature-wip](array-type)Add element_at and subscript functions (#8597)

Describe the overview of changes.
1. add function element_at;
2. support element_subscript([]) to get element of array, col_array[N] <==> element_at(col_array, N);
3. return error message instead of BE crash while array function execute failed;

element_at(array, index) desc:
>   Returns element of array at given **(1-based)** index. 
  If **index < 0**, accesses elements from the last to the first. 
  Returns NULL if the index exceeds the length of the array or the array is NULL.

Usage example:
1. create table with ARRAY type column and insert some data:
```
+------+------+--------+
| k1   | k2   | k3     |
+------+------+--------+
|    1 |    2 | [1, 2] |
|    2 |    3 | NULL   |
|    4 | NULL | []     |
|    3 | NULL | NULL   |
+------+------+--------+
```
2. enable vectorized:
```
set enable_vectorized_engine=true;
```
3. element_subscript([]) usage example:
```
> select k1,k3,k3[1] from array_test;
+------+--------+----------------------------+
| k1   | k3     | %element_extract%(`k3`, 1) |
+------+--------+----------------------------+
|    3 | NULL   |                       NULL |
|    1 | [1, 2] |                          1 |
|    2 | NULL   |                       NULL |
|    4 | []     |                       NULL |
+------+--------+----------------------------+
```
4. element_at function usage example:
```
> select k1,k3 from array_test where element_at(k3, -1) = 2;
+------+--------+
| k1   | k3     |
+------+--------+
|    1 | [1, 2] |
+------+--------+
```
This commit is contained in:
camby
2022-04-02 12:03:56 +08:00
committed by GitHub
parent 8bb16bfeb3
commit 4d516bece8
14 changed files with 519 additions and 66 deletions

View File

@ -36,7 +36,10 @@ TEST(function_array_index_test, array_contains) {
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::Int32, TypeIndex::Int32};
Array vec = {Int32(1), Int32(2), Int32(3)};
DataSet data_set = {{{vec, 2}, UInt8(1)}, {{vec, 4}, UInt8(0)}, {{Null(), 1}, Null()}, {{empty_arr, 1}, UInt8(0)}};
DataSet data_set = {{{vec, 2}, UInt8(1)},
{{vec, 4}, UInt8(0)},
{{Null(), 1}, Null()},
{{empty_arr, 1}, UInt8(0)}};
check_function<DataTypeUInt8, true>(func_name, input_types, data_set);
}
@ -46,7 +49,10 @@ TEST(function_array_index_test, array_contains) {
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::Int32, TypeIndex::Int8};
Array vec = {Int32(1), Int32(2), Int32(3)};
DataSet data_set = {{{vec, Int8(2)}, UInt8(1)}, {{vec, Int8(4)}, UInt8(0)}, {{Null(), Int8(1)}, Null()}, {{empty_arr, Int8(1)}, UInt8(0)}};
DataSet data_set = {{{vec, Int8(2)}, UInt8(1)},
{{vec, Int8(4)}, UInt8(0)},
{{Null(), Int8(1)}, Null()},
{{empty_arr, Int8(1)}, UInt8(0)}};
check_function<DataTypeUInt8, true>(func_name, input_types, data_set);
}
@ -56,7 +62,10 @@ TEST(function_array_index_test, array_contains) {
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::Int8, TypeIndex::Int64};
Array vec = {Int8(1), Int8(2), Int8(3)};
DataSet data_set = {{{vec, Int64(2)}, UInt8(1)}, {{vec, Int64(4)}, UInt8(0)}, {{Null(), Int64(1)}, Null()}, {{empty_arr, Int64(1)}, UInt8(0)}};
DataSet data_set = {{{vec, Int64(2)}, UInt8(1)},
{{vec, Int64(4)}, UInt8(0)},
{{Null(), Int64(1)}, Null()},
{{empty_arr, Int64(1)}, UInt8(0)}};
check_function<DataTypeUInt8, true>(func_name, input_types, data_set);
}
@ -65,9 +74,12 @@ TEST(function_array_index_test, array_contains) {
{
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::String, TypeIndex::String};
Array vec = {Field("abc", 3), Field("", 0), Field("def",3)};
DataSet data_set = {{{vec, std::string("abc")}, UInt8(1)}, {{vec, std::string("aaa")}, UInt8(0)},
{{vec, std::string("")}, UInt8(1)}, {{Null(), std::string("abc")}, Null()}, {{empty_arr, std::string("")}, UInt8(0)}};
Array vec = {Field("abc", 3), Field("", 0), Field("def", 3)};
DataSet data_set = {{{vec, std::string("abc")}, UInt8(1)},
{{vec, std::string("aaa")}, UInt8(0)},
{{vec, std::string("")}, UInt8(1)},
{{Null(), std::string("abc")}, Null()},
{{empty_arr, std::string("")}, UInt8(0)}};
check_function<DataTypeUInt8, true>(func_name, input_types, data_set);
}
@ -82,7 +94,10 @@ TEST(function_array_index_test, array_position) {
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::Int32, TypeIndex::Int32};
Array vec = {Int32(1), Int32(2), Int32(3)};
DataSet data_set = {{{vec, 2}, Int64(2)}, {{vec, 4}, Int64(0)}, {{Null(), 1}, Null()}, {{empty_arr, 1}, Int64(0)}};
DataSet data_set = {{{vec, 2}, Int64(2)},
{{vec, 4}, Int64(0)},
{{Null(), 1}, Null()},
{{empty_arr, 1}, Int64(0)}};
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
}
@ -92,7 +107,10 @@ TEST(function_array_index_test, array_position) {
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::Int32, TypeIndex::Int8};
Array vec = {Int32(1), Int32(2), Int32(3)};
DataSet data_set = {{{vec, Int8(2)}, Int64(2)}, {{vec, Int8(4)}, Int64(0)}, {{Null(), Int8(1)}, Null()}, {{empty_arr, Int8(1)}, Int64(0)}};
DataSet data_set = {{{vec, Int8(2)}, Int64(2)},
{{vec, Int8(4)}, Int64(0)},
{{Null(), Int8(1)}, Null()},
{{empty_arr, Int8(1)}, Int64(0)}};
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
}
@ -102,7 +120,10 @@ TEST(function_array_index_test, array_position) {
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::Int8, TypeIndex::Int64};
Array vec = {Int8(1), Int8(2), Int8(3)};
DataSet data_set = {{{vec, Int64(2)}, Int64(2)}, {{vec, Int64(4)}, Int64(0)}, {{Null(), Int64(1)}, Null()}, {{empty_arr, Int64(1)}, Int64(0)}};
DataSet data_set = {{{vec, Int64(2)}, Int64(2)},
{{vec, Int64(4)}, Int64(0)},
{{Null(), Int64(1)}, Null()},
{{empty_arr, Int64(1)}, Int64(0)}};
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
}
@ -111,9 +132,12 @@ TEST(function_array_index_test, array_position) {
{
InputTypeSet input_types = {TypeIndex::Array, TypeIndex::String, TypeIndex::String};
Array vec = {Field("abc", 3), Field("", 0), Field("def",3)};
DataSet data_set = {{{vec, std::string("abc")}, Int64(1)}, {{vec, std::string("aaa")}, Int64(0)},
{{vec, std::string("")}, Int64(2)}, {{Null(), std::string("abc")}, Null()}, {{empty_arr, std::string("")}, Int64(0)}};
Array vec = {Field("abc", 3), Field("", 0), Field("def", 3)};
DataSet data_set = {{{vec, std::string("abc")}, Int64(1)},
{{vec, std::string("aaa")}, Int64(0)},
{{vec, std::string("")}, Int64(2)},
{{Null(), std::string("abc")}, Null()},
{{empty_arr, std::string("")}, Int64(0)}};
check_function<DataTypeInt64, true>(func_name, input_types, data_set);
}