be/src/exprs/function/array/function_array_enumerate.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include <fmt/format.h> |
19 | | #include <glog/logging.h> |
20 | | #include <stddef.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <boost/iterator/iterator_facade.hpp> |
24 | | #include <memory> |
25 | | #include <string> |
26 | | #include <utility> |
27 | | |
28 | | #include "common/status.h" |
29 | | #include "core/assert_cast.h" |
30 | | #include "core/block/block.h" |
31 | | #include "core/block/column_numbers.h" |
32 | | #include "core/block/column_with_type_and_name.h" |
33 | | #include "core/column/column.h" |
34 | | #include "core/column/column_array.h" |
35 | | #include "core/column/column_nullable.h" |
36 | | #include "core/column/column_vector.h" |
37 | | #include "core/data_type/data_type.h" |
38 | | #include "core/data_type/data_type_array.h" |
39 | | #include "core/data_type/data_type_nullable.h" |
40 | | #include "core/data_type/data_type_number.h" |
41 | | #include "core/types.h" |
42 | | #include "exprs/aggregate/aggregate_function.h" |
43 | | #include "exprs/function/function.h" |
44 | | #include "exprs/function/function_helpers.h" |
45 | | #include "exprs/function/simple_function_factory.h" |
46 | | |
47 | | namespace doris { |
48 | | class FunctionContext; |
49 | | } // namespace doris |
50 | | |
51 | | namespace doris { |
52 | | |
53 | | class FunctionArrayEnumerate : public IFunction { |
54 | | public: |
55 | | static constexpr auto name = "array_enumerate"; |
56 | 2 | static FunctionPtr create() { return std::make_shared<FunctionArrayEnumerate>(); } |
57 | 1 | String get_name() const override { return name; } |
58 | 0 | size_t get_number_of_arguments() const override { return 1; } |
59 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
60 | 0 | const DataTypeArray* array_type = |
61 | 0 | check_and_get_data_type<DataTypeArray>(remove_nullable(arguments[0]).get()); |
62 | 0 | if (!array_type) { |
63 | 0 | throw doris::Exception( |
64 | 0 | ErrorCode::INVALID_ARGUMENT, |
65 | 0 | "First argument for function {} .must be an array but it type is {}", |
66 | 0 | get_name(), arguments[0]->get_name()); |
67 | 0 | } |
68 | | |
69 | 0 | auto nested_type = assert_cast<const DataTypeArray&>(*array_type).get_nested_type(); |
70 | 0 | bool is_nested_nullable = nested_type->is_nullable(); |
71 | 0 | bool is_nullable = arguments[0]->is_nullable(); |
72 | 0 | auto return_nested_type = std::make_shared<DataTypeInt64>(); |
73 | 0 | DataTypePtr return_type = std::make_shared<DataTypeArray>( |
74 | 0 | is_nested_nullable ? make_nullable(return_nested_type) : return_nested_type); |
75 | 0 | if (is_nullable) { |
76 | 0 | return_type = make_nullable(return_type); |
77 | 0 | } |
78 | 0 | return return_type; |
79 | 0 | } |
80 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
81 | 0 | uint32_t result, size_t input_rows_count) const override { |
82 | 0 | auto left_column = |
83 | 0 | block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); |
84 | 0 | const ColumnArray* array = |
85 | 0 | check_and_get_column<ColumnArray>(remove_nullable(left_column->get_ptr()).get()); |
86 | 0 | if (!array) { |
87 | 0 | return Status::RuntimeError( |
88 | 0 | fmt::format("Illegal column {}, of first argument of function {}", |
89 | 0 | left_column->get_name(), get_name())); |
90 | 0 | } |
91 | 0 | const ColumnArray::Offsets64& offsets = array->get_offsets(); |
92 | 0 | auto res_nested = ColumnInt64::create(); |
93 | 0 | ColumnInt64::Container& res_values = res_nested->get_data(); |
94 | 0 | res_values.resize(array->get_data().size()); |
95 | 0 | ColumnArray::Offset64 prev_off = 0; |
96 | 0 | for (auto off : offsets) { |
97 | 0 | for (ColumnArray::Offset64 j = prev_off; j < off; ++j) res_values[j] = j - prev_off + 1; |
98 | 0 | prev_off = off; |
99 | 0 | } |
100 | |
|
101 | 0 | ColumnPtr nested_column = res_nested->get_ptr(); |
102 | 0 | if (array->get_data().is_nullable()) { |
103 | 0 | nested_column = ColumnNullable::create(nested_column, |
104 | 0 | ColumnUInt8::create(nested_column->size(), 0)); |
105 | 0 | } |
106 | 0 | ColumnPtr res_column = |
107 | 0 | ColumnArray::create(std::move(nested_column), array->get_offsets_ptr()); |
108 | 0 | if (block.get_by_position(arguments[0]).column->is_nullable()) { |
109 | 0 | const ColumnNullable* nullable = |
110 | 0 | check_and_get_column<ColumnNullable>(left_column.get()); |
111 | 0 | res_column = ColumnNullable::create( |
112 | 0 | res_column, nullable->get_null_map_column().clone_resized(nullable->size())); |
113 | 0 | } |
114 | 0 | block.replace_by_position(result, std::move(res_column)); |
115 | 0 | return Status::OK(); |
116 | 0 | } |
117 | | }; |
118 | | |
119 | 1 | void register_function_array_enumerate(SimpleFunctionFactory& factory) { |
120 | 1 | factory.register_function<FunctionArrayEnumerate>(); |
121 | 1 | } |
122 | | |
123 | | } // namespace doris |