forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_expr_array_avg.cpp
More file actions
344 lines (324 loc) · 14.1 KB
/
Copy pathob_expr_array_avg.cpp
File metadata and controls
344 lines (324 loc) · 14.1 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (c) 2025 OceanBase.
*
* Licensed 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.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "sql/engine/expr/ob_expr_array_avg.h"
#include "lib/udt/ob_array_type.h"
#include "sql/engine/expr/ob_array_expr_utils.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/engine/expr/ob_expr_result_type_util.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
using namespace oceanbase::omt;
namespace oceanbase
{
namespace sql
{
ObExprArrayAvg::ObExprArrayAvg(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUNC_SYS_ARRAY_AVG, N_ARRAY_AVG, 1, VALID_FOR_GENERATED_COL, NOT_ROW_DIMENSION)
{
}
ObExprArrayAvg::~ObExprArrayAvg()
{
}
int ObExprArrayAvg::calc_result_type1(ObExprResType &type,
ObExprResType &type1,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = NULL;
ObExecContext *exec_ctx = NULL;
ObDataType src_elem_type;
ObCollectionTypeBase *coll_type = NULL;
uint32_t depth = 0;
bool is_vec = false;
if (OB_ISNULL(session = const_cast<ObSQLSessionInfo *>(type_ctx.get_session()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ObSQLSessionInfo is null", K(ret));
} else if (OB_ISNULL(exec_ctx = session->get_cur_exec_ctx())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ObExecContext is null", K(ret));
} else if (ob_is_null(type1.get_type())) {
// do nothing
} else if (!ob_is_collection_sql_type(type1.get_type())) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_OP, "ARRAY", ob_obj_type_str(type1.get_type()));
} else if (OB_FAIL(ObArrayExprUtils::get_coll_type_by_subschema_id(exec_ctx, type1.get_subschema_id(), coll_type))) {
LOG_WARN("failed to get array type by subschema id", K(ret), K(type1.get_subschema_id()));
} else if (coll_type->type_id_ != ObNestedType::OB_ARRAY_TYPE && coll_type->type_id_ != ObNestedType::OB_VECTOR_TYPE) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_WARN("invalid collection type", K(ret), K(coll_type->type_id_));
} else if (OB_FAIL(ObArrayExprUtils::get_array_element_type(exec_ctx, type1.get_subschema_id(), src_elem_type, depth, is_vec))) {
LOG_WARN("failed to get array element type", K(ret));
} else if (depth != 1) {
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "array_avg with multi-dimension array");
LOG_WARN("not supported array dimension", K(ret), K(depth));
} else if (!ob_is_numeric_type(src_elem_type.get_obj_type())) {
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "array_avg with non-numeric type");
LOG_WARN("not supported array data type", K(ret), K(src_elem_type.get_obj_type()));
}
if (OB_SUCC(ret)) {
type.set_double();
type.set_accuracy(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObDoubleType]);
}
return ret;
}
int ObExprArrayAvg::eval_array_avg(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
{
int ret = OB_SUCCESS;
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &tmp_allocator = tmp_alloc_g.get_allocator();
const uint16_t subschema_id = expr.args_[0]->obj_meta_.get_subschema_id();
ObDatum *arr_datum = NULL;
ObCollectionArrayType *arr_type = NULL;
ObIArrayType *src_arr = NULL;
double res_average = 0;
uint32_t depth = 0;
if (OB_FAIL(expr.args_[0]->eval(ctx, arr_datum))) {
LOG_WARN("failed to eval source array arg", K(ret));
} else if (arr_datum->is_null()) {
res.set_null();
} else if (OB_FAIL(ObArrayExprUtils::get_array_type_by_subschema_id(ctx, subschema_id, arr_type))) {
LOG_WARN("failed to get array type by subschema id", K(ret), K(subschema_id));
} else {
ObString data_str = arr_datum->get_string();
uint32_t len = 0, data_len = 0;
uint8_t *null_bitmaps = nullptr;
const char *data = nullptr;
if (OB_FAIL(ObTextStringHelper::read_real_string_data(&tmp_allocator,
ObLongTextType,
CS_TYPE_BINARY,
true,
data_str))) {
LOG_WARN("fail to get real data", K(ret), K(data_str));
} else if (OB_FAIL(ObArrayExprUtils::get_array_data(data_str,
arr_type,
len,
null_bitmaps,
data,
data_len))) {
LOG_WARN("failed to get array data", K(ret));
} else if (ob_is_integer_type(arr_type->get_basic_meta(depth).get_obj_type())) {
depth = 0;
if (ob_is_unsigned_type(arr_type->get_basic_meta(depth).get_obj_type())) {
uint64_t res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len, null_bitmaps, data, data_len, arr_type, res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res.set_double(static_cast<double>(res_sum) / len);
}
} else {
int64_t res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len, null_bitmaps, data, data_len, arr_type, res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res.set_double(static_cast<double>(res_sum) / len);
}
}
} else {
double res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len, null_bitmaps, data, data_len, arr_type, res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res.set_double(res_sum / len);
}
}
}
return ret;
}
int ObExprArrayAvg::eval_array_avg_batch(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const int64_t batch_size)
{
int ret = OB_SUCCESS;
ObDatumVector res_datum = expr.locate_expr_datumvector(ctx);
ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &tmp_allocator = tmp_alloc_g.get_allocator();
const uint16_t subschema_id = expr.args_[0]->obj_meta_.get_subschema_id();
ObCollectionArrayType *arr_type = NULL;
ObIArrayType *src_arr = NULL;
uint32_t depth = 0;
if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
LOG_WARN("eval source array failed", K(ret));
} else {
ObDatumVector arr_array = expr.args_[0]->locate_expr_datumvector(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
bool is_null_res = false;
if (skip.at(j) || eval_flags.at(j)) {
continue;
}
eval_flags.set(j);
if (arr_array.at(j)->is_null()) {
res_datum.at(j)->set_null();
} else if (OB_FAIL(ObArrayExprUtils::get_array_type_by_subschema_id(ctx, subschema_id, arr_type))) {
LOG_WARN("failed to get array type by subschema id", K(ret), K(subschema_id));
} else {
ObString data_str = arr_array.at(j)->get_string();
uint32_t len = 0, data_len = 0;
uint8_t *null_bitmaps = nullptr;
const char *data = nullptr;
if (OB_FAIL(ObTextStringHelper::read_real_string_data(&tmp_allocator,
ObLongTextType,
CS_TYPE_BINARY,
true,
data_str))) {
LOG_WARN("fail to get real data.", K(ret), K(data_str));
} else if (OB_FAIL(ObArrayExprUtils::get_array_data(data_str,
arr_type,
len,
null_bitmaps,
data,
data_len))) {
LOG_WARN("failed to get array data", K(ret));
} else if (ob_is_integer_type(arr_type->get_basic_meta(depth).get_obj_type())) {
depth = 0;
if (ob_is_unsigned_type(arr_type->get_basic_meta(depth).get_obj_type())) {
uint64_t res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len, null_bitmaps, data, data_len, arr_type, res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res_datum.at(j)->set_double(static_cast<double>(res_sum) / len);
}
} else {
int64_t res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len, null_bitmaps, data, data_len, arr_type, res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res_datum.at(j)->set_double(static_cast<double>(res_sum) / len);
}
}
} else {
double res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len,
null_bitmaps,
data,
data_len,
arr_type,
res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res_datum.at(j)->set_double(res_sum / len);
}
}
}
} // end for
}
return ret;
}
int ObExprArrayAvg::eval_array_avg_vector(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const EvalBound &bound)
{
int ret = OB_SUCCESS;
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &tmp_allocator = tmp_alloc_g.get_allocator();
const uint16_t subschema_id = expr.args_[0]->obj_meta_.get_subschema_id();
ObCollectionArrayType *arr_type = NULL;
ObIArrayType *src_arr = NULL;
if (OB_FAIL(expr.args_[0]->eval_vector(ctx, skip, bound))) {
LOG_WARN("eval source array failed", K(ret));
} else {
ObIVector *arr_vec = expr.args_[0]->get_vector(ctx);
ObIVector *res_vec = expr.get_vector(ctx);
VectorFormat arr_format = arr_vec->get_format();
ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
uint32_t depth = 0;
for (int64_t j = bound.start(); OB_SUCC(ret) && j < bound.end(); ++j) {
uint32_t len = 0, data_len = 0;
uint8_t *null_bitmaps = nullptr;
const char *data = nullptr;
if (skip.at(j) || eval_flags.at(j)) {
continue;
}
eval_flags.set(j);
ObString data_str = arr_vec->get_string(j);
if (arr_vec->is_null(j)) {
res_vec->set_null(j);
} else if (OB_FAIL(ObArrayExprUtils::get_array_type_by_subschema_id(ctx, subschema_id, arr_type))) {
LOG_WARN("failed to get array type by subschema id", K(ret), K(subschema_id));
} else if (!ObCollectionExprUtil::is_compact_fmt_cell(data_str.ptr())) {
ret = OB_ERR_UNEXPECTED;
SQL_LOG(WARN, "unexpected data format", K(ret));
} else if (OB_FAIL(ObTextStringHelper::read_real_string_data(&tmp_allocator,
ObLongTextType,
CS_TYPE_BINARY,
true,
data_str))) {
LOG_WARN("fail to get real data.", K(ret), K(data_str));
} else if (OB_FAIL(ObArrayExprUtils::get_array_data(data_str,
arr_type,
len,
null_bitmaps,
data,
data_len))) {
LOG_WARN("failed to get array data", K(ret));
} else if (ob_is_integer_type(arr_type->get_basic_meta(depth).get_obj_type())) {
depth = 0;
if (ob_is_unsigned_type(arr_type->get_basic_meta(depth).get_obj_type())) {
uint64_t res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len,
null_bitmaps,
data,
data_len,
arr_type,
res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res_vec->set_double(j, static_cast<double>(res_sum) / len);
}
} else {
int64_t res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len,
null_bitmaps,
data,
data_len,
arr_type,
res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res_vec->set_double(j, static_cast<double>(res_sum) / len);
}
}
} else {
double res_sum = 0;
if (OB_FAIL(ObArrayExprUtils::calc_array_sum(len,
null_bitmaps,
data,
data_len,
arr_type,
res_sum))) {
LOG_WARN("failed to calc sum", K(ret));
} else {
res_vec->set_double(j, res_sum / len);
}
}
} // end for
}
return ret;
}
int ObExprArrayAvg::cg_expr(ObExprCGCtx &expr_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
UNUSED(expr_cg_ctx);
UNUSED(raw_expr);
rt_expr.eval_func_ = eval_array_avg;
rt_expr.eval_batch_func_ = eval_array_avg_batch;
rt_expr.eval_vector_func_ = eval_array_avg_vector;
return OB_SUCCESS;
}
} // namespace sql
} // namespace oceanbase