forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_array_map.h
More file actions
203 lines (181 loc) · 7.43 KB
/
Copy pathfunction_array_map.h
File metadata and controls
203 lines (181 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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.
#pragma once
#include <type_traits>
#include "core/column/column_array.h"
#include "core/column/column_decimal.h"
#include "core/column/column_string.h"
#include "core/data_type/data_type_array.h"
#include "core/data_type/primitive_type.h"
#include "exprs/function/array/function_array_utils.h"
#include "exprs/function/function_helpers.h"
namespace doris {
enum class MapOperation { INTERSECT, UNION };
template <typename Map, typename ColumnType>
struct IntersectAction;
template <typename Map, typename ColumnType>
struct UnionAction;
template <typename Map, typename ColumnType, MapOperation operation>
struct MapActionImpl;
template <typename Map, typename ColumnType>
struct MapActionImpl<Map, ColumnType, MapOperation::INTERSECT> {
using Action = IntersectAction<Map, ColumnType>;
};
template <typename Map, typename ColumnType>
struct MapActionImpl<Map, ColumnType, MapOperation::UNION> {
using Action = UnionAction<Map, ColumnType>;
};
template <MapOperation operation, typename ColumnType>
struct OpenMapImpl {
using Element = typename ColumnType::value_type;
using ElementNativeType = typename NativeType<Element>::Type;
using Map = phmap::flat_hash_map<ElementNativeType, size_t>;
using Action = typename MapActionImpl<Map, ColumnType, operation>::Action;
Action action;
Map map;
void reset() {
map.clear();
action.reset();
}
// this method calculate rows to get a rest dst data
void apply(ColumnArrayMutableData& dst, const ColumnArrayExecutionDatas params,
std::vector<bool>& col_const, size_t start_row, size_t end_row) {
size_t dst_off = 0;
for (size_t row = start_row; row < end_row; ++row) {
reset();
for (int i = 0; i < params.size(); ++i) {
action.apply(map, i, index_check_const(row, col_const[i]), params[i]);
}
// nullmap
if (action.apply_null()) {
++dst_off;
dst.nested_col->insert_default();
if (dst.nested_nullmap_data) {
dst.nested_nullmap_data->push_back(1);
}
}
// make map result to dst
for (const auto& entry : map) {
if ((operation == MapOperation::INTERSECT && entry.second == params.size()) ||
operation == MapOperation::UNION) {
++dst_off;
auto& dst_data = static_cast<ColumnType&>(*dst.nested_col).get_data();
dst_data.push_back(entry.first);
if (dst.nested_nullmap_data) {
dst.nested_nullmap_data->push_back(0);
}
}
}
dst.offsets_ptr->push_back(dst_off);
}
}
};
template <MapOperation operation>
struct OpenMapImpl<operation, ColumnString> {
using Map = phmap::flat_hash_map<StringRef, size_t, StringRefHash>;
using Action = typename MapActionImpl<Map, ColumnString, operation>::Action;
Action action;
Map map;
void reset() {
map.clear();
action.reset();
}
void apply(ColumnArrayMutableData& dst, const ColumnArrayExecutionDatas params,
std::vector<bool>& col_const, size_t start_row, size_t end_row) {
size_t dst_off = 0;
for (size_t row = start_row; row < end_row; ++row) {
reset();
for (size_t i = 0; i < params.size(); ++i) {
action.apply(map, i, index_check_const(row, col_const[i]), params[i]);
}
// nullmap
if (action.apply_null()) {
++dst_off;
dst.nested_col->insert_default();
if (dst.nested_nullmap_data) {
dst.nested_nullmap_data->push_back(1);
}
}
// make map result to dst
for (const auto& entry : map) {
if ((operation == MapOperation::INTERSECT && entry.second == params.size()) ||
operation == MapOperation::UNION) {
auto& dst_col = static_cast<ColumnString&>(*dst.nested_col);
StringRef key = entry.first;
++dst_off;
dst_col.insert_data(key.data, key.size);
if (dst.nested_nullmap_data) {
dst.nested_nullmap_data->push_back(0);
}
}
}
dst.offsets_ptr->push_back(dst_off);
}
}
};
template <MapOperation operation>
struct ArrayMapImpl {
public:
static DataTypePtr get_return_type(const DataTypes& arguments) {
DataTypePtr res;
// if any nested type of array arguments is nullable then return array with
// nullable nested type.
for (const auto& arg : arguments) {
const auto* array_type = check_and_get_data_type<DataTypeArray>(arg.get());
if (array_type->get_nested_type()->is_nullable()) {
res = arg;
break;
}
}
res = res ? res : arguments[0];
return res;
}
static Status execute(ColumnPtr& res_ptr, ColumnArrayExecutionDatas datas,
std::vector<bool>& col_const, size_t start_row, size_t end_row) {
ColumnArrayMutableData dst =
create_mutable_data(datas[0].nested_col.get(), datas[0].nested_nullmap_data);
if (_execute_internal<ALL_COLUMNS_SIMPLE>(dst, datas, col_const, start_row, end_row)) {
res_ptr = assemble_column_array(dst);
return Status::OK();
}
return Status::RuntimeError("Unexpected columns");
}
private:
template <typename ColumnType>
static bool _execute_internal(ColumnArrayMutableData& dst, ColumnArrayExecutionDatas datas,
std::vector<bool>& col_const, size_t start_row, size_t end_row) {
for (auto data : datas) {
if (!is_column<ColumnType>(*data.nested_col)) {
return false;
}
}
// do check staff
using Impl = OpenMapImpl<operation, ColumnType>;
Impl impl;
ColumnPtr res_column;
impl.apply(dst, datas, col_const, start_row, end_row);
return true;
}
template <typename T, typename... Ts>
requires(sizeof...(Ts) > 0)
static bool _execute_internal(ColumnArrayMutableData& dst, ColumnArrayExecutionDatas datas,
std::vector<bool>& col_const, size_t start_row, size_t end_row) {
return _execute_internal<T>(dst, datas, col_const, start_row, end_row) ||
_execute_internal<Ts...>(dst, datas, col_const, start_row, end_row);
}
};
} // namespace doris