forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_expr_abs.cpp
More file actions
858 lines (789 loc) · 27 KB
/
Copy pathob_expr_abs.cpp
File metadata and controls
858 lines (789 loc) · 27 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
/*
* 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_abs.h"
#include "share/datum/ob_datum_util.h"
#include "sql/engine/expr/ob_expr_result_type_util.h"
#include "sql/session/ob_sql_session_info.h"
namespace oceanbase
{
using namespace common;
using namespace common::number;
namespace sql
{
#define DEF_EVAL_ABS_FUNC(type) \
template <> \
int eval_datum_abs<type>(const ObExpr &expr, ObEvalCtx &ctx, \
ObDatum &expr_datum)
static int check_expr_and_eval(const ObExpr &expr, ObEvalCtx &ctx,
ObDatum *¶m_datum, bool &found_null)
{
int ret = OB_SUCCESS;
found_null = false;
if (OB_UNLIKELY(expr.type_ != T_OP_ABS)
|| OB_UNLIKELY(expr.arg_cnt_ != 1) || OB_ISNULL(expr.args_)
|| OB_ISNULL(expr.args_[0])) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
} else if (OB_FAIL(expr.args_[0]->eval(ctx, param_datum))) {
LOG_WARN("failed to eval", K(ret));
} else if (param_datum->is_null()) {
found_null = true;
} else {
// do nothing
}
return ret;
}
template<ObObjType obj_type>
int eval_datum_abs(const ObExpr &expr,
ObEvalCtx &ctx,
ObDatum &expr_datum)
{
UNUSED(expr);
UNUSED(ctx);
UNUSED(expr_datum);
return OB_NOT_SUPPORTED;
}
DEF_EVAL_ABS_FUNC(ObNullType)
{
int ret = OB_SUCCESS;
UNUSED(ctx);
UNUSED(expr);
expr_datum.set_null();
return ret;
}
DEF_EVAL_ABS_FUNC(ObNumberType)
{
int ret = OB_SUCCESS;
ObDatum *param_datum = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param_datum, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
number::ObNumber param_nmb(param_datum->get_number());
number::ObNumber res_num = param_nmb;
if (param_nmb.is_negative()) {
res_num = param_nmb.negate();
}
expr_datum.set_number(res_num);
}
return ret;
}
DEF_EVAL_ABS_FUNC(ObUNumberType)
{
int ret = OB_SUCCESS;
ObDatum *param_datum = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param_datum, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
number::ObNumber param_nmb(param_datum->get_number());
number::ObNumber res_num = param_nmb;
if (param_nmb.is_negative()) {
res_num = param_nmb.negate();
}
expr_datum.set_number(res_num);
}
return ret;
}
DEF_EVAL_ABS_FUNC(ObFloatType)
{
int ret = OB_SUCCESS;
ObDatum *param = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param, found_null))) {
LOG_WARN("check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
expr_datum.set_float(param->get_float() >= 0.0
? param->get_float() : -param->get_float());
}
return ret;
}
DEF_EVAL_ABS_FUNC(ObDoubleType)
{
int ret = OB_SUCCESS;
ObDatum *param = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
expr_datum.set_double(param->get_double() >= 0
? param->get_double() : -param->get_double());
}
return ret;
}
DEF_EVAL_ABS_FUNC(ObUDoubleType)
{
int ret = OB_SUCCESS;
ObDatum *param = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
expr_datum.set_double(param->get_udouble());
}
return ret;
}
DEF_EVAL_ABS_FUNC(ObIntType)
{
int ret = OB_SUCCESS;
ObDatum *param = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
int64_t param_int = param->get_int();
// Only mysql mode will call this function, if INT64_MIN is found, out of range needs to be reported
if (INT64_MIN == param_int) {
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("value out of range", K(ret));
} else {
expr_datum.set_int(param_int >= 0 ? param_int : -param_int);
}
}
return ret;
}
DEF_EVAL_ABS_FUNC(ObUInt64Type)
{
int ret = OB_SUCCESS;
ObDatum *param = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
expr_datum.set_uint(param->get_uint64());
}
return ret;
}
#define MAKE_DECIMAL_INT_OPPOSITE(TYPE) \
case sizeof(TYPE##_t): { \
res_val.from(-(*(decint->TYPE##_v_))); \
break; \
}
DEF_EVAL_ABS_FUNC(ObDecimalIntType)
{
int ret = OB_SUCCESS;
ObDatum *param_datum = NULL;
bool found_null = false;
if (OB_FAIL(check_expr_and_eval(expr, ctx, param_datum, found_null))) {
LOG_WARN("failed to check expr and eval", K(ret));
} else if (found_null) {
expr_datum.set_null();
} else {
const ObDecimalInt *decint = param_datum->get_decimal_int();
const int32_t int_bytes = param_datum->get_int_bytes();
bool is_neg = wide::is_negative(decint, int_bytes);
if (is_neg) {
ObDecimalIntBuilder res_val;
switch (int_bytes) {
MAKE_DECIMAL_INT_OPPOSITE(int32)
MAKE_DECIMAL_INT_OPPOSITE(int64)
MAKE_DECIMAL_INT_OPPOSITE(int128)
MAKE_DECIMAL_INT_OPPOSITE(int256)
MAKE_DECIMAL_INT_OPPOSITE(int512)
default: {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("int_bytes is unexpected", K(ret), K(int_bytes));
break;
}
}
if (OB_SUCC(ret)) {
expr_datum.set_decimal_int(res_val.get_decimal_int(), int_bytes);
}
} else {
expr_datum.set_decimal_int(decint, int_bytes);
}
}
return ret;
}
ObExpr::EvalFunc abs_funcs[ObMaxType];
static int check_expr_and_eval_vector(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const EvalBound &bound)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(expr.type_ != T_OP_ABS)
|| OB_UNLIKELY(expr.arg_cnt_ != 1) || OB_ISNULL(expr.args_)
|| OB_ISNULL(expr.args_[0])) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
} else if (OB_FAIL(expr.args_[0]->eval_vector(ctx, skip, bound))) {
LOG_WARN("failed to eval vector", K(ret));
}
return ret;
}
// base
template<ObObjType obj_type>
int eval_vector_abs(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const EvalBound &bound)
{
UNUSED(expr);
UNUSED(ctx);
UNUSED(skip);
UNUSED(bound);
return OB_NOT_SUPPORTED;
}
#define DEF_EVAL_ABS_VEC_FUNC(type) \
template <> \
int eval_vector_abs<type>(const ObExpr &expr, ObEvalCtx &ctx, \
const ObBitVector &skip, const EvalBound &bound) \
template<VecValueTypeClass vec_tc, typename ArgVec, typename ResVec>
class EvalVectorRowAbsHelper {
public:
};
#define DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(type) \
template<typename ArgVec, typename ResVec> \
class EvalVectorRowAbsHelper<type, ArgVec, ResVec> { \
public: \
static int inner_eval_abs_row(const ArgVec *arg_vec, \
ResVec *res_vec, \
const int64_t &idx)
template<VecValueTypeClass vec_tc, typename ArgVec, typename ResVec>
class EvalVectorAbsHelper {
public:
static int inner_eval_abs_vector(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const EvalBound &bound)
{
int ret = OB_SUCCESS;
ResVec *res_vec = static_cast<ResVec *>(expr.get_vector(ctx));
ArgVec *arg_vec = static_cast<ArgVec *>(expr.args_[0]->get_vector(ctx));
for (int64_t j = bound.start(); OB_SUCC(ret) && j < bound.end(); ++j) {
if (skip.at(j)) {
continue;
}
if (arg_vec->is_null(j)) {
res_vec->set_null(j);
} else {
ret = EvalVectorRowAbsHelper<vec_tc, ArgVec, ResVec>::
inner_eval_abs_row(arg_vec, res_vec, j);
}
}
return ret;
}
};
#define DEF_INNER_EVAL_ABS_VEC_FUNC(type) \
template<typename ArgVec, typename ResVec> \
class EvalVectorAbsHelper<type, ArgVec, ResVec> { \
public: \
static int inner_eval_abs_vector(const ObExpr &expr, \
ObEvalCtx &ctx, \
const ObBitVector &skip, \
const EvalBound &bound)
#define END_DEF_INNER_EVAL_ABS_FUNC };
template<VecValueTypeClass vec_tc>
static int dispatch_eval_abs_fixed_len_vector(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const EvalBound &bound)
{
int ret = OB_SUCCESS;
VectorFormat arg_format = expr.args_[0]->get_format(ctx);
VectorFormat res_format = expr.get_format(ctx);
if (VEC_FIXED == arg_format && VEC_FIXED == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObFixedLengthVector<RTCType<vec_tc>, VectorBasicOp<vec_tc>>,
ObFixedLengthVector<RTCType<vec_tc>, VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else if (VEC_UNIFORM == arg_format && VEC_FIXED == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObUniformVector<false, VectorBasicOp<vec_tc>>,
ObFixedLengthVector<RTCType<vec_tc>, VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else if (VEC_UNIFORM_CONST == arg_format && VEC_FIXED == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObUniformVector<true, VectorBasicOp<vec_tc>>,
ObFixedLengthVector<RTCType<vec_tc>, VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else {
ret = EvalVectorAbsHelper<vec_tc, ObVectorBase, ObVectorBase>::
inner_eval_abs_vector(expr, ctx, skip, bound);
}
return ret;
}
template<VecValueTypeClass vec_tc>
static int dispatch_eval_abs_variable_len_vector(const ObExpr &expr,
ObEvalCtx &ctx,
const ObBitVector &skip,
const EvalBound &bound)
{
int ret = OB_SUCCESS;
VectorFormat arg_format = expr.args_[0]->get_format(ctx);
VectorFormat res_format = expr.get_format(ctx);
if (VEC_DISCRETE == arg_format && VEC_DISCRETE == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObDiscreteVector<VectorBasicOp<vec_tc>>,
ObDiscreteVector<VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else if (VEC_CONTINUOUS == arg_format && VEC_DISCRETE == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObContinuousVector<VectorBasicOp<vec_tc>>,
ObDiscreteVector<VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else if (VEC_UNIFORM == arg_format && VEC_DISCRETE == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObUniformVector<false, VectorBasicOp<vec_tc>>,
ObDiscreteVector<VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else if (VEC_UNIFORM_CONST == arg_format && VEC_DISCRETE == res_format) {
ret = EvalVectorAbsHelper<vec_tc, ObUniformVector<true, VectorBasicOp<vec_tc>>,
ObDiscreteVector<VectorBasicOp<vec_tc>>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
} else {
ret = EvalVectorAbsHelper<vec_tc, ObVectorBase, ObVectorBase>::
inner_eval_abs_vector(expr, ctx, skip, bound);
}
return ret;
}
// ObNullType
DEF_INNER_EVAL_ABS_VEC_FUNC(VEC_TC_NULL)
{
int ret = OB_SUCCESS;
ResVec *res_vec = static_cast<ResVec *>(expr.get_vector(ctx));
for (int64_t j = bound.start(); OB_SUCC(ret) && j < bound.end(); ++j) {
if (skip.at(j)) {
continue;
}
res_vec->set_null(j);
}
return ret;
}
END_DEF_INNER_EVAL_ABS_FUNC
DEF_EVAL_ABS_VEC_FUNC(ObNullType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else {
VectorFormat res_format = expr.get_format(ctx);
switch (res_format) {
case VEC_DISCRETE:
case VEC_CONTINUOUS:
case VEC_FIXED: {
ret = EvalVectorAbsHelper<VEC_TC_NULL, ObVectorBase, ObBitmapNullVectorBase>::
inner_eval_abs_vector(expr, ctx, skip, bound);
break;
}
case VEC_UNIFORM: {
ret = EvalVectorAbsHelper<VEC_TC_NULL, ObVectorBase, ObUniformFormat<false>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
break;
}
case VEC_UNIFORM_CONST: {
ret = EvalVectorAbsHelper<VEC_TC_NULL, ObVectorBase, ObUniformFormat<true>>::
inner_eval_abs_vector(expr, ctx, skip, bound);
break;
}
default: {
ret = EvalVectorAbsHelper<VEC_TC_NULL, ObVectorBase, ObVectorBase>::
inner_eval_abs_vector(expr, ctx, skip, bound);
}
}
}
return ret;
}
// ObNumberType
DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(VEC_TC_NUMBER)
{
number::ObNumber param_nmb(arg_vec->get_number(idx));
if (param_nmb.is_negative()) {
res_vec->set_number(idx, param_nmb.negate());
} else {
res_vec->set_number(idx, param_nmb);
}
return OB_SUCCESS;
}
END_DEF_INNER_EVAL_ABS_FUNC
DEF_EVAL_ABS_VEC_FUNC(ObNumberType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_variable_len_vector<
VEC_TC_NUMBER>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObUNumberType
// The "ObUNumberType" and "ObNumberType" are processed using the same logic.
DEF_EVAL_ABS_VEC_FUNC(ObUNumberType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_variable_len_vector<
VEC_TC_NUMBER>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObFloatType
DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(VEC_TC_FLOAT)
{
float param_float = arg_vec->get_float(idx);
res_vec->set_float(idx, param_float > 0.0 ? param_float : -param_float);
return OB_SUCCESS;
}
END_DEF_INNER_EVAL_ABS_FUNC
DEF_EVAL_ABS_VEC_FUNC(ObFloatType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_fixed_len_vector<
VEC_TC_FLOAT>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObDoubleType
DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(VEC_TC_DOUBLE)
{
double param_double = arg_vec->get_double(idx);
res_vec->set_double(idx, param_double >= 0 ? param_double : -param_double);
return OB_SUCCESS;
}
END_DEF_INNER_EVAL_ABS_FUNC
DEF_EVAL_ABS_VEC_FUNC(ObDoubleType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_fixed_len_vector<
VEC_TC_DOUBLE>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObUDoubleType
DEF_EVAL_ABS_VEC_FUNC(ObUDoubleType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_fixed_len_vector<
VEC_TC_DOUBLE>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObIntType
DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(VEC_TC_INTEGER)
{
int ret = OB_SUCCESS;
int64_t param_int = arg_vec->get_int(idx);
// This function is only called in the MySQL mode.
// If it is found to be INT64_MIN, it should report an "out of range" error.
if (INT64_MIN == param_int) {
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("int value out of range", K(ret));
} else {
res_vec->set_int(idx, param_int >= 0 ? param_int : -param_int);
}
return ret;
}
END_DEF_INNER_EVAL_ABS_FUNC
DEF_EVAL_ABS_VEC_FUNC(ObIntType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_fixed_len_vector<
VEC_TC_INTEGER>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObUInt64Type
DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(VEC_TC_UINTEGER)
{
res_vec->set_uint(idx, arg_vec->get_uint64(idx));
return OB_SUCCESS;
}
END_DEF_INNER_EVAL_ABS_FUNC
DEF_EVAL_ABS_VEC_FUNC(ObUInt64Type)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else if (OB_FAIL(dispatch_eval_abs_fixed_len_vector<
VEC_TC_UINTEGER>(expr, ctx, skip, bound))) {
LOG_WARN("dispatch_eval_abs_variable_len_vector", K(ret));
}
return ret;
}
// ObDecimalIntType
#define DEF_INNER_EVAL_ABS_DECIMAL_VEC_FUNC(INT_BIT) \
DEF_INNER_EVAL_ABS_VEC_ROW_FUNC(VEC_TC_DEC_INT##INT_BIT) \
{ \
const ObDecimalInt *decint = arg_vec->get_decimal_int(idx); \
bool is_neg = wide::is_negative(decint, INT_BIT / CHAR_BIT); \
if (is_neg) { \
ObDecimalIntBuilder res_val; \
res_val.from(-(*(decint->int##INT_BIT##_v_))); \
res_vec->set_decimal_int(idx, res_val.get_decimal_int(), INT_BIT / CHAR_BIT); \
} else { \
res_vec->set_decimal_int(idx, decint, INT_BIT / CHAR_BIT); \
} \
return OB_SUCCESS; \
} \
END_DEF_INNER_EVAL_ABS_FUNC
DEF_INNER_EVAL_ABS_DECIMAL_VEC_FUNC(32)
DEF_INNER_EVAL_ABS_DECIMAL_VEC_FUNC(64)
DEF_INNER_EVAL_ABS_DECIMAL_VEC_FUNC(128)
DEF_INNER_EVAL_ABS_DECIMAL_VEC_FUNC(256)
DEF_INNER_EVAL_ABS_DECIMAL_VEC_FUNC(512)
DEF_EVAL_ABS_VEC_FUNC(ObDecimalIntType)
{
int ret = OB_SUCCESS;
if (OB_FAIL(check_expr_and_eval_vector(expr, ctx, skip, bound))) {
LOG_WARN("check_expr_and_eval_vector failed", K(ret));
} else {
int16_t precision = expr.datum_meta_.precision_;
if (precision <= 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("dicimal precision <= 0", K(ret), K(precision));
} else if (precision <= MAX_PRECISION_DECIMAL_INT_32) {
ret = dispatch_eval_abs_fixed_len_vector<VEC_TC_DEC_INT32>(expr, ctx, skip, bound);
} else if (precision <= MAX_PRECISION_DECIMAL_INT_64) {
ret = dispatch_eval_abs_fixed_len_vector<VEC_TC_DEC_INT64>(expr, ctx, skip, bound);
} else if (precision <= MAX_PRECISION_DECIMAL_INT_128) {
ret = dispatch_eval_abs_fixed_len_vector<VEC_TC_DEC_INT128>(expr, ctx, skip, bound);
} else if (precision <= MAX_PRECISION_DECIMAL_INT_256) {
ret = dispatch_eval_abs_fixed_len_vector<VEC_TC_DEC_INT256>(expr, ctx, skip, bound);
} else {
ret = dispatch_eval_abs_fixed_len_vector<VEC_TC_DEC_INT512>(expr, ctx, skip, bound);
}
}
return ret;
}
ObExpr::EvalVectorFunc abs_vec_funcs[ObMaxType];
template<int IDX>
struct AbsFuncIniter
{
static bool init_array()
{
abs_funcs[IDX] = &eval_datum_abs<static_cast<ObObjType>(IDX)>;
abs_vec_funcs[IDX] = &eval_vector_abs<static_cast<ObObjType>(IDX)>;
return true;
}
};
static bool abs_eval_func_init_ret = ObArrayConstIniter<ObMaxType, AbsFuncIniter>::init();
static_assert(ObMaxType == sizeof(abs_funcs) / sizeof(void *), "unexpected size");
static_assert(ObMaxType == sizeof(abs_vec_funcs) / sizeof(void *), "unexpected size");
REG_SER_FUNC_ARRAY(OB_SFA_SQL_EXPR_ABS_EVAL, abs_funcs, ARRAYSIZEOF(abs_funcs));
REG_SER_FUNC_ARRAY(OB_SFA_SQL_EXPR_ABS_EVAL_VEC, abs_vec_funcs, ARRAYSIZEOF(abs_vec_funcs));
ObExprAbs::ObExprAbs(ObIAllocator &alloc)
: ObExprOperator(alloc, T_OP_ABS, N_ABS, 1, VALID_FOR_GENERATED_COL, NOT_ROW_DIMENSION),
func_(NULL) {}
int ObExprAbs::assign(const ObExprOperator &other)
{
int ret = OB_SUCCESS;
const ObExprAbs *tmp_other = dynamic_cast<const ObExprAbs *>(&other);
if (OB_UNLIKELY(NULL == tmp_other)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument. wrong type for other", K(ret), K(other));
} else if (OB_LIKELY(this != tmp_other)) {
if (OB_FAIL(ObExprOperator::assign(other))) {
LOG_WARN("copy in Base class ObExprOperator failed", K(ret));
} else {
this->func_ = tmp_other->func_;
}
}
return ret;
}
int ObExprAbs::calc_result_type1(ObExprResType &type, ObExprResType &type1,
ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
int ret = OB_SUCCESS;
const ObSQLSessionInfo *session = type_ctx.get_session();
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else if (NOT_ROW_DIMENSION == row_dimension_) {
// result type
ObObjType itype;
if (OB_SUCC(ObExprResultTypeUtil::get_abs_result_type(itype, type1.get_type()))) {
if (ObMaxType == itype) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
} else {
type.set_type(itype);
}
}
// collation
// The result cannot be of character type, no need to set collation
if (lib::is_mysql_mode() && type.is_double() && type1.get_scale() != SCALE_UNKNOWN_YET) {
type.set_scale(type1.get_scale());
type.set_precision(static_cast<ObPrecision>(ObMySQLUtil::float_length(type1.get_scale())));
} else {
type.set_accuracy(type1.get_accuracy());
}
// null flag
ObExprOperator::calc_result_flag1(type, type1);
if (OB_SUCC(ret)) {
// set calc type for param
ObObjType param_calc_type = calc_param_type(type1.get_type());
if (OB_UNLIKELY(ObMaxType == param_calc_type)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid param calc type", K(ret), K(type1.get_type()), K(param_calc_type));
} else {
type1.set_calc_type(param_calc_type);
if (type1.get_type() == ObJsonType) {
type1.set_calc_type(ObDoubleType);
type.set_type(ObDoubleType);
}
}
}
} else {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
}
return ret;
}
//tinyint, mediumint, smallint, int32
//int64
//utiniyint, umediumint, usmallint
//uint32 uint64
//float
//double
//ufloat
//udouble
//number
//unumber
//null
//others. (datetime time varchar, etc)
//others for oracle. (datetime time varchar, etc)
//bit
//enum_set
int ObExprAbs::cg_expr(ObExprCGCtx &ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(ctx);
UNUSED(raw_expr);
if (OB_UNLIKELY(T_OP_ABS != rt_expr.type_)
|| OB_ISNULL(rt_expr.args_)
|| OB_UNLIKELY(rt_expr.arg_cnt_ != 1)
|| OB_ISNULL(rt_expr.args_[0])) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
} else if (OB_UNLIKELY(rt_expr.args_[0]->datum_meta_.type_ >= ObMaxType)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid arg type for abs", K(ret));
} else {
rt_expr.eval_func_ = abs_funcs[rt_expr.args_[0]->datum_meta_.type_];
rt_expr.eval_vector_func_ = abs_vec_funcs[rt_expr.args_[0]->datum_meta_.type_];
}
return ret;
}
ObObjType ObExprAbs::calc_param_type(const ObObjType orig_param_type)
{
ObObjType calc_type = ObMaxType;
switch (orig_param_type)
{
case ObTinyIntType:
case ObSmallIntType:
case ObMediumIntType:
case ObInt32Type:
case ObIntType: {
calc_type = ObIntType;
break;
}
case ObUTinyIntType:
case ObUSmallIntType:
case ObUMediumIntType:
case ObUInt32Type:
case ObUInt64Type: {
calc_type = ObUInt64Type;
break;
}
case ObFloatType:
case ObDoubleType: {
calc_type = ObDoubleType;
break;
}
case ObUFloatType:
case ObUDoubleType: {
calc_type = ObUDoubleType;
break;
}
case ObNumberType: {
calc_type = ObNumberType;
break;
}
case ObUNumberType: {
calc_type = ObUNumberType;
break;
}
case ObNullType: {
calc_type = ObNullType;
break;
}
case ObYearType: {
calc_type = ObUInt64Type;
break;
}
case ObDateTimeType:
case ObTimestampType:
case ObDateType:
case ObTimeType:
case ObVarcharType:
case ObCharType:
case ObUnknownType:
case ObHexStringType:
case ObTextType:
case ObTinyTextType:
case ObMediumTextType:
case ObLongTextType:
case ObMySQLDateType:
case ObMySQLDateTimeType: {
calc_type = ObDoubleType;
break;
}
case ObBitType: {
calc_type = ObUInt64Type;
break;
}
case ObEnumType:
case ObSetType:
case ObJsonType: {
calc_type = ObDoubleType;
break;
}
case ObDecimalIntType: {
calc_type = ObDecimalIntType;
break;
}
default: {
break;
}
}
return calc_type;
}
} // namespace sql
} // namespace oceanbase