forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_expr_array_max.cpp
More file actions
328 lines (299 loc) · 12.4 KB
/
Copy pathob_expr_array_max.cpp
File metadata and controls
328 lines (299 loc) · 12.4 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
/*
* 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_max.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
{
ObExprArrayExtreme::ObExprArrayExtreme(common::ObIAllocator &alloc, ObExprOperatorType type, const char *name)
: ObFuncExprOperator(alloc, type, name, 1, VALID_FOR_GENERATED_COL, NOT_ROW_DIMENSION)
{
}
ObExprArrayExtreme::~ObExprArrayExtreme()
{
}
int ObExprArrayExtreme::calc_result_type1(ObExprResType &type,
ObExprResType &type1,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = NULL;
ObExecContext *exec_ctx = NULL;
ObSubSchemaValue arr_meta;
const ObSqlCollectionInfo *coll_info = NULL;
ObCollectionArrayType *arr_type = NULL;
ObDataType src_elem_type;
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())) {
type.set_utinyint(); // default type
} 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(exec_ctx->get_sqludt_meta_by_subschema_id(type1.get_subschema_id(), arr_meta))) {
LOG_WARN("failed to get elem meta.", K(ret), K(type1.get_subschema_id()));
} else if (OB_ISNULL(coll_info = reinterpret_cast<const ObSqlCollectionInfo *>(arr_meta.value_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ObSqlCollectionInfo is null", K(ret));
} else if (coll_info->collection_meta_->type_id_ != ObNestedType::OB_ARRAY_TYPE
&& coll_info->collection_meta_->type_id_ != ObNestedType::OB_VECTOR_TYPE) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_WARN("invalid collection type", K(ret), K(coll_info->collection_meta_->type_id_ ));
} else if (OB_ISNULL(arr_type = static_cast<ObCollectionArrayType *>(coll_info->collection_meta_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ObCollectionArrayType is null", K(ret));
} else if (arr_type->element_type_->type_id_ == ObNestedType::OB_BASIC_TYPE) {
ObCollectionBasicType *elem_type = static_cast<ObCollectionBasicType *>(arr_type->element_type_);
type.set_meta(elem_type->basic_meta_.get_meta_type());
type.set_accuracy(elem_type->basic_meta_.get_accuracy());
} else if (arr_type->element_type_->type_id_ == ObNestedType::OB_ARRAY_TYPE
|| arr_type->element_type_->type_id_ == ObNestedType::OB_VECTOR_TYPE) {
// TODO: support array of array
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "nested array");
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected ObNestedType type", K(ret), K(arr_type->element_type_->type_id_));
}
return ret;
}
int ObExprArrayExtreme::calc_extreme(ObIArrayType* src_arr, ObObj &res_obj, bool is_max)
{
int ret = OB_SUCCESS;
ObCollectionBasicType *elem_type = NULL;
res_obj.set_null();
if (OB_ISNULL(elem_type = dynamic_cast<ObCollectionBasicType *>(dynamic_cast<const ObCollectionArrayType*>(src_arr->get_array_type())->element_type_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("source array collection element type is null", K(ret));
} else if (src_arr->get_format() == Nested_Array) {
// TODO: support array of array
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "array_max with nested array");
} else {
for (uint32_t i = 0; i < src_arr->size() && OB_SUCC(ret); ++i) {
ObObj elem_obj;
int cmp = 0;
if (src_arr->is_null(i)) {
// do nothing
} else if (OB_FAIL(src_arr->elem_at(i, elem_obj))) {
LOG_WARN("failed to get element", K(ret), K(i));
} else if (res_obj.is_null()) {
res_obj = elem_obj;
} else if (elem_obj.is_varchar()) {
if (is_max && elem_obj.get_string() > res_obj.get_string()) {
res_obj = elem_obj;
} else if (!is_max && elem_obj.get_string() < res_obj.get_string()) {
res_obj = elem_obj;
}
} else {
if (is_max && elem_obj > res_obj) {
res_obj = elem_obj;
} else if (!is_max && elem_obj < res_obj) {
res_obj = elem_obj;
}
}
} // end for
}
return ret;
}
int ObExprArrayExtreme::eval_array_extreme(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res, bool is_max)
{
int ret = OB_SUCCESS;
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &tmp_allocator = tmp_alloc_g.get_allocator();
ObExprStrResAlloc res_alloc(expr, ctx);
const uint16_t subschema_id = expr.args_[0]->obj_meta_.get_subschema_id();
ObDatum *arr_datum = NULL;
ObIArrayType *src_arr = NULL;
ObObj res_obj;
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_obj(tmp_allocator, ctx, subschema_id, arr_datum->get_string(), src_arr))) {
LOG_WARN("construct array obj failed", K(ret));
} else if (OB_FAIL(calc_extreme(src_arr, res_obj, is_max))) {
LOG_WARN("calc array extreme value failed", K(ret));
} else {
res.from_obj(res_obj);
if (res_obj.is_string_type() && OB_FAIL(res.deep_copy(res, res_alloc))) {
LOG_WARN("fail to deep copy for res datum", K(ret), K(res_obj), K(res));
}
}
return ret;
}
int ObExprArrayExtreme::eval_array_extreme_batch(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const int64_t batch_size,
bool is_max)
{
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();
ObExprStrResAlloc res_alloc(expr, ctx);
const uint16_t subschema_id = expr.args_[0]->obj_meta_.get_subschema_id();
ObIArrayType *src_arr = NULL;
ObObj res_obj;
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) {
int64_t idx = 0;
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_obj(tmp_allocator, ctx, subschema_id, arr_array.at(j)->get_string(), src_arr))) {
LOG_WARN("construct array obj failed", K(ret));
} else if (OB_FAIL(calc_extreme(src_arr, res_obj, is_max))) {
LOG_WARN("calc array extreme value failed", K(ret));
} else {
res_datum.at(j)->from_obj(res_obj);
if (res_obj.is_string_type() && OB_FAIL(res_datum.at(j)->deep_copy(*res_datum.at(j), res_alloc))) {
LOG_WARN("fail to deep copy for res datum", K(ret), K(res_obj), KPC(res_datum.at(j)));
}
}
} // end for
}
return ret;
}
int ObExprArrayExtreme::eval_array_extreme_vector(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const EvalBound &bound,
bool is_max)
{
int ret = OB_SUCCESS;
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
common::ObArenaAllocator &tmp_allocator = tmp_alloc_g.get_allocator();
ObExprStrResAlloc res_alloc(expr, ctx);
const uint16_t subschema_id = expr.args_[0]->obj_meta_.get_subschema_id();
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);
VectorFormat arr_format = arr_vec->get_format();
ObIVector *res_vec = expr.get_vector(ctx);
ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
ObObj res_obj;
for (int64_t idx = bound.start(); OB_SUCC(ret) && idx < bound.end(); ++idx) {
bool is_null_res = false;
int64_t arr_idx = 0;
if (skip.at(idx) || eval_flags.at(idx)) {
continue;
}
eval_flags.set(idx);
if (arr_vec->is_null(idx)) {
is_null_res = true;
} else {
ObString arr_str = arr_vec->get_string(idx);
if (OB_FAIL(ObNestedVectorFunc::construct_param(tmp_allocator, ctx, subschema_id, arr_str, src_arr))) {
LOG_WARN("construct array obj failed", K(ret));
}
}
if (OB_FAIL(ret)) {
} else if (is_null_res) {
res_vec->set_null(idx);
} else if (OB_FAIL(calc_extreme(src_arr, res_obj, is_max))) {
LOG_WARN("calc array extreme value failed", K(ret));
} else if (OB_FAIL(ObArrayExprUtils::set_obj_to_vector(res_vec, idx, res_obj, res_alloc))) {
LOG_WARN("failed to set object value to result vector", K(ret), K(idx), K(res_obj));
}
} // end for
}
return ret;
}
ObExprArrayMax::ObExprArrayMax(common::ObIAllocator &alloc)
: ObExprArrayExtreme(alloc, T_FUNC_SYS_ARRAY_MAX, N_ARRAY_MAX)
{
}
ObExprArrayMax::~ObExprArrayMax()
{
}
int ObExprArrayMax::eval_array_max(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
{
return ObExprArrayExtreme::eval_array_extreme(expr, ctx, res, true);
}
int ObExprArrayMax::eval_array_max_batch(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const int64_t batch_size)
{
return ObExprArrayExtreme::eval_array_extreme_batch(expr, ctx, skip, batch_size, true);
}
int ObExprArrayMax::eval_array_max_vector(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const EvalBound &bound)
{
return ObExprArrayExtreme::eval_array_extreme_vector(expr, ctx, skip, bound, true);
}
int ObExprArrayMax::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_max;
rt_expr.eval_batch_func_ = eval_array_max_batch;
rt_expr.eval_vector_func_ = eval_array_max_vector;
return OB_SUCCESS;
}
ObExprArrayMin::ObExprArrayMin(common::ObIAllocator &alloc)
: ObExprArrayExtreme(alloc, T_FUNC_SYS_ARRAY_MIN, N_ARRAY_MIN)
{
}
ObExprArrayMin::~ObExprArrayMin()
{
}
int ObExprArrayMin::eval_array_min(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
{
return ObExprArrayExtreme::eval_array_extreme(expr, ctx, res, false);
}
int ObExprArrayMin::eval_array_min_batch(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const int64_t batch_size)
{
return ObExprArrayExtreme::eval_array_extreme_batch(expr, ctx, skip, batch_size, false);
}
int ObExprArrayMin::eval_array_min_vector(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const EvalBound &bound)
{
return ObExprArrayExtreme::eval_array_extreme_vector(expr, ctx, skip, bound, false);
}
int ObExprArrayMin::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_min;
rt_expr.eval_batch_func_ = eval_array_min_batch;
rt_expr.eval_vector_func_ = eval_array_min_vector;
return OB_SUCCESS;
}
} // namespace sql
} // namespace oceanbase