forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_datum_cast.cpp
More file actions
8129 lines (7781 loc) · 276 KB
/
ob_datum_cast.cpp
File metadata and controls
8129 lines (7781 loc) · 276 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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL
#include <string.h>
#include "lib/charset/ob_dtoa.h"
#include "share/object/ob_obj_cast_util.h"
#include "share/object/ob_obj_cast.h"
#include "sql/engine/expr/ob_datum_cast.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "sql/engine/ob_serializable_function.h"
namespace oceanbase {
namespace sql {
using namespace oceanbase::common;
//// common function and macro
#define CAST_FUNC_NAME(intype, outtype) \
int intype##_##outtype(const sql::ObExpr& expr, sql::ObEvalCtx& ctx, sql::ObDatum& res_datum)
#define CAST_ENUMSET_FUNC_NAME(intype, outtype) \
int intype##_##outtype(const sql::ObExpr& expr, \
const common::ObIArray<common::ObString>& str_values, \
const uint64_t cast_mode, \
sql::ObEvalCtx& ctx, \
sql::ObDatum& res_datum)
// OB_ISNULL(expr.args_[0]), arg_cnt_ == 1, OB_ISNULL(child_res->ptr_) won't check
#define EVAL_ARG() \
int ret = OB_SUCCESS; \
ObDatum* child_res = NULL; \
if (OB_FAIL(expr.args_[0]->eval(ctx, child_res))) { \
LOG_WARN("eval arg failed", K(ret)); \
} else if (child_res->is_null()) { \
res_datum.set_null(); \
} else
// in oracle mode, the empty string of longtext type is not equal to null, and the empty
// string of other string types is equal to null
#define EVAL_STRING_ARG() \
int ret = OB_SUCCESS; \
ObDatum* child_res = NULL; \
if (OB_FAIL(expr.args_[0]->eval(ctx, child_res))) { \
LOG_WARN("eval arg failed", K(ret)); \
} else if (child_res->is_null() || \
(lib::is_oracle_mode() && 0 == child_res->len_ && ObLongTextType != expr.args_[0]->datum_meta_.type_)) { \
res_datum.set_null(); \
} else
#define DEF_IN_OUT_TYPE() \
int warning = OB_SUCCESS; \
ObObjType in_type = expr.args_[0]->datum_meta_.type_; \
ObObjType out_type = expr.datum_meta_.type_;
// if you use ObDatum::get_xxx(), you need to pass one more parameter to specify the function name
// so the following macro directly casts
#define DEF_IN_OUT_VAL(in_type, out_type, init_val) \
int warning = OB_SUCCESS; \
in_type in_val = *(reinterpret_cast<const in_type*>(child_res->ptr_)); \
out_type out_val = (init_val);
#define CAST_FAIL(stmt) (OB_UNLIKELY((OB_SUCCESS != (ret = get_cast_ret((expr.extra_), (stmt), warning)))))
// similar to CAST_FAIL, but the above macro will use expr.extra_ as cast_mode, so one less
// parameter is written when using it
#define CAST_FAIL_CM(stmt, cast_mode) (OB_UNLIKELY((OB_SUCCESS != (ret = get_cast_ret((cast_mode), (stmt), warning)))))
#define GET_SESSION() \
ObBasicSessionInfo* session = ctx.exec_ctx_.get_my_session(); \
if (OB_ISNULL(session)) { \
ret = OB_ERR_UNEXPECTED; \
LOG_WARN("session is NULL", K(ret)); \
} else
static OB_INLINE int get_cast_ret(const ObCastMode& cast_mode, int ret, int& warning)
{
// compatibility for old ob
if (OB_UNLIKELY(OB_ERR_UNEXPECTED_TZ_TRANSITION == ret) || OB_UNLIKELY(OB_ERR_UNKNOWN_TIME_ZONE == ret)) {
ret = OB_INVALID_DATE_VALUE;
} else if (OB_SUCCESS != ret && CM_IS_WARN_ON_FAIL(cast_mode)) {
warning = ret;
ret = OB_SUCCESS;
}
return ret;
}
// When an error occurs, the processing is as follows:
// if only WARN_ON_FAIL is set, the error code will be overwritten
// if WARN_ON_FAIL and ZERO_ON_WARN are set, the error code will be overwritten, and the result will be set to 0
// if WARN_ON_FAIL and NULL_ON_WARN are set, the error code will be overwritten, and the result will be set to null
#define SET_RES_OBJ(cast_mode, func_val, zero_value, value) \
do { \
if (OB_SUCC(ret)) { \
if (OB_SUCCESS == warning || OB_ERR_TRUNCATED_WRONG_VALUE == warning || OB_DATA_OUT_OF_RANGE == warning || \
OB_ERR_DATA_TRUNCATED == warning || OB_ERR_DOUBLE_TRUNCATED == warning || \
OB_ERR_TRUNCATED_WRONG_VALUE_FOR_FIELD == warning) { \
res_datum.set_##func_val(value); \
} else if (CM_IS_ZERO_ON_WARN(cast_mode)) { \
res_datum.set_##func_val(zero_value); \
} else { \
res_datum.set_null(); \
} \
} else { \
res_datum.set_##func_val(value); \
} \
} while (0)
// Macros for setting timestamp nano and timestamp ltz
#define SET_RES_OTIMESTAMP_10BYTE(value) \
do { \
if (OB_SUCC(ret)) { \
if (OB_SUCCESS == warning || OB_ERR_TRUNCATED_WRONG_VALUE == warning || OB_DATA_OUT_OF_RANGE == warning || \
OB_ERR_DATA_TRUNCATED == warning || OB_ERR_DOUBLE_TRUNCATED == warning || \
OB_ERR_TRUNCATED_WRONG_VALUE_FOR_FIELD == warning) { \
res_datum.set_otimestamp_tiny(value); \
} else if (CM_IS_ZERO_ON_WARN(expr.extra_)) { \
res_datum.set_otimestamp_tiny(ObOTimestampData()); \
} else { \
res_datum.set_null(); \
} \
} else { \
res_datum.set_otimestamp_tiny(value); \
} \
} while (0)
#define SET_RES_INT(value) SET_RES_OBJ(expr.extra_, int, 0, value)
#define SET_RES_BIT(value) SET_RES_OBJ(expr.extra_, uint, 0, value)
#define SET_RES_UINT(value) SET_RES_OBJ(expr.extra_, uint, 0, value)
#define SET_RES_DATE(value) SET_RES_OBJ(expr.extra_, date, ObTimeConverter::ZERO_DATE, value)
#define SET_RES_TIME(value) SET_RES_OBJ(expr.extra_, time, ObTimeConverter::ZERO_TIME, value)
#define SET_RES_YEAR(value) SET_RES_OBJ(expr.extra_, year, ObTimeConverter::ZERO_YEAR, value)
#define SET_RES_FLOAT(value) SET_RES_OBJ(expr.extra_, float, 0.0, value)
#define SET_RES_DOUBLE(value) SET_RES_OBJ(expr.extra_, double, 0.0, value)
#define SET_RES_ENUM(value) SET_RES_OBJ(expr.extra_, enum, 0, value)
#define SET_RES_SET(value) SET_RES_OBJ(expr.extra_, set, 0, value)
#define SET_RES_OTIMESTAMP(value) SET_RES_OBJ(expr.extra_, otimestamp_tz, ObOTimestampData(), value)
#define SET_RES_INTERVAL_YM(value) SET_RES_OBJ(expr.extra_, interval_nmonth, 0, value)
#define SET_RES_INTERVAL_DS(value) SET_RES_OBJ(expr.extra_, interval_ds, ObIntervalDSValue(), value)
#define SET_RES_DATETIME(value) SET_RES_OBJ(expr.extra_, datetime, ObTimeConverter::ZERO_DATETIME, value)
static const int64_t MAX_DOUBLE_STRICT_PRINT_SIZE = 512;
static OB_INLINE int serialize_obnumber(number::ObNumber& nmb, ObIAllocator& allocator, ObString& out_str)
{
int ret = OB_SUCCESS;
const number::ObNumber::Desc& nmb_desc = nmb.get_desc();
uint32_t* digits = nmb.get_digits();
char* buf = NULL;
int64_t buf_len = sizeof(nmb_desc) + nmb_desc.len_ * sizeof(uint32_t);
if (OB_ISNULL(buf = reinterpret_cast<char*>(allocator.alloc(buf_len)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(ret), K(buf_len));
} else {
MEMCPY(buf, &nmb_desc, sizeof(nmb_desc));
MEMCPY(buf + sizeof(nmb_desc), digits, nmb_desc.len_ * sizeof(uint32_t));
out_str.assign_ptr(buf, static_cast<int32_t>(buf_len));
}
return ret;
}
int ObDatumHexUtils::hex(
const ObExpr& expr, const ObString& in_str, ObEvalCtx& ctx, ObIAllocator& calc_alloc, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
if (in_str.empty()) {
if (lib::is_oracle_mode()) {
res_datum.set_null();
} else {
res_datum.set_string(NULL, 0);
}
} else {
char* buf = NULL;
bool need_convert = false;
ObCollationType def_cs = ObCharset::get_system_collation();
ObCollationType dst_cs = expr.datum_meta_.cs_type_;
ObIAllocator* alloc = NULL;
ObExprStrResAlloc res_alloc(expr, ctx);
// check if need convert first, and setup alloc. we can avoid copy converted res str.
if (ObExprUtil::need_convert_string_collation(def_cs, dst_cs, need_convert)) {
LOG_WARN("check need convert cs type failed", K(ret), K(def_cs), K(dst_cs));
} else if (need_convert) {
alloc = &calc_alloc;
} else {
alloc = &res_alloc;
}
if (OB_SUCC(ret)) {
const int32_t alloc_length = in_str.length() * 2;
if (OB_ISNULL(buf = reinterpret_cast<char*>(alloc->alloc(alloc_length)))) {
res_datum.set_null();
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(ret), K(alloc_length));
} else {
static const char* HEXCHARS = "0123456789ABCDEF";
int32_t pos = 0;
for (int32_t i = 0; i < in_str.length(); ++i) {
buf[pos++] = HEXCHARS[in_str[i] >> 4 & 0xF];
buf[pos++] = HEXCHARS[in_str[i] & 0xF];
}
ObString res_str;
if (need_convert) {
if (OB_FAIL(ObExprUtil::convert_string_collation(ObString(pos, buf), def_cs, res_str, dst_cs, res_alloc))) {
LOG_WARN("convert string collation failed", K(ret));
}
} else {
res_str.assign_ptr(buf, pos);
}
if (OB_SUCC(ret)) {
res_datum.set_string(res_str);
}
}
}
}
return ret;
}
static OB_INLINE int common_construct_otimestamp(
const ObObjType type, const ObDatum& in_datum, ObOTimestampData& out_val);
int ObDatumHexUtils::rawtohex(const ObExpr& expr, const ObString& in_str, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
ObObjType in_type = expr.args_[0]->datum_meta_.type_;
if (0 >= in_str.length()) {
res_datum.set_null();
} else if (!(ObNullType < in_type && in_type < ObMaxType)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid in type", K(ret), K(in_type));
} else {
ObIAllocator& tmp_alloc = ctx.get_reset_tmp_alloc();
ObString out_str;
switch (in_type) {
// TODO::this should same as oracle, and support dump func
case ObTinyIntType:
case ObSmallIntType:
case ObInt32Type:
case ObIntType: {
// although tiny/small/int32/int actually occupies less than 8 bytes,
// but the CG stage will still allocate 8 bytes of space for it
int64_t in_val = *(reinterpret_cast<const int64_t*>(in_str.ptr()));
number::ObNumber nmb;
if (OB_FAIL(nmb.from(in_val, tmp_alloc))) {
LOG_WARN("fail to int_number", K(ret), K(in_val), "type", in_type);
} else if (OB_FAIL(serialize_obnumber(nmb, tmp_alloc, out_str))) {
LOG_WARN("serialize_obnumber failed", K(ret), K(nmb));
}
break;
}
case ObNumberFloatType:
case ObNumberType: {
out_str = in_str;
break;
}
case ObDateTimeType: {
// shallow copy is ok. unhex accept const argument
out_str = in_str;
break;
}
case ObNVarchar2Type:
case ObNCharType:
case ObVarcharType:
case ObCharType:
case ObLongTextType:
case ObRawType: {
// https://www.techonthenet.com/oracle/functions/rawtohex.php
// NOTE:: when convert string to raw, Oracle use utl_raw.cast_to_raw(),
// while PL/SQL use hextoraw(), here we use utl_raw.cast_to_raw(),
// as we can not distinguish in which SQL
out_str = in_str;
break;
}
case ObTimestampTZType:
case ObTimestampLTZType:
case ObTimestampNanoType: {
ObOTimestampData in_val;
// ObTimestampTZType is 12 bytes, ObTimestampLTZType and ObTimestampNanoType is 10 bytes
// so it needs to be distinguished
ObDatum tmp_datum;
tmp_datum.ptr_ = in_str.ptr();
tmp_datum.pack_ = static_cast<uint32_t>(in_str.length());
if (OB_FAIL(common_construct_otimestamp(in_type, tmp_datum, in_val))) {
LOG_WARN("common_construct_otimestamp failed", K(ret));
} else {
out_str.assign_ptr(reinterpret_cast<char*>(&in_val), static_cast<int32_t>(in_str.length()));
}
break;
}
default: {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(in_str), "type", in_type);
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(hex(expr, out_str, ctx, tmp_alloc, res_datum))) {
LOG_WARN("fail to convert to hex", K(ret), K(out_str));
}
}
}
return ret;
}
int ObDatumHexUtils::hextoraw_string(const ObExpr& expr, const ObString& in_str, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
if (0 == in_str.length()) {
res_datum.set_null();
} else if (OB_FAIL(unhex(expr, in_str, ctx, res_datum))) {
LOG_WARN("unhex failed", K(ret), K(in_str));
}
return ret;
}
static int common_copy_string(
const ObExpr& expr, const ObString& src, ObEvalCtx& ctx, ObDatum& res_datum, const int64_t align_offset = 0);
int ObDatumHexUtils::hextoraw(const ObExpr& expr, const ObDatum& in, const ObObjType& in_type,
const ObCollationType& in_cs_type, ObEvalCtx& ctx, ObDatum& res)
{
int ret = OB_SUCCESS;
if (in.is_null()) {
res.set_null();
} else if (ob_is_numeric_type(in_type)) {
number::ObNumber res_nmb;
ObNumStackOnceAlloc tmp_alloc;
if (OB_FAIL(get_uint(in_type, in, tmp_alloc, res_nmb))) {
LOG_WARN("fail to get uint64", K(ret));
} else if (OB_FAIL(uint_to_raw(res_nmb, expr, ctx, res))) {
LOG_WARN("fail to convert to hex", K(ret), K(res_nmb));
}
} else if (ob_is_character_type(in_type, in_cs_type) || ob_is_varbinary_or_binary(in_type, in_cs_type)) {
const ObString& in_str = in.get_string();
if (OB_FAIL(hextoraw_string(expr, in_str, ctx, res))) {
LOG_WARN("hextoraw_string failed", K(ret), K(in_str));
}
} else if (ob_is_raw(in_type)) {
ObString in_str(in.get_string());
if (OB_FAIL(common_copy_string(expr, in_str, ctx, res))) {
LOG_WARN("common_copy_string failed", K(ret), K(in_str));
}
} else {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(in_type));
}
return ret;
}
int ObDatumHexUtils::uint_to_raw(
const number::ObNumber& uint_num, const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
const int64_t oracle_max_avail_len = 40;
char uint_buf[number::ObNumber::MAX_TOTAL_SCALE] = {0};
int64_t uint_pos = 0;
ObString uint_str;
if (OB_FAIL(uint_num.format(uint_buf, number::ObNumber::MAX_TOTAL_SCALE, uint_pos, 0))) {
LOG_WARN("fail to format ", K(ret), K(uint_num));
} else if (uint_pos > oracle_max_avail_len) {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(uint_pos), K(oracle_max_avail_len), K(uint_num));
} else {
uint_str.assign_ptr(uint_buf, static_cast<int32_t>(uint_pos));
if (OB_FAIL(unhex(expr, uint_str, ctx, res_datum))) {
LOG_WARN("fail to str_to_raw", K(ret), K(uint_str));
}
}
return ret;
}
int ObDatumHexUtils::get_uint(const ObObjType& in_type, const ObDatum& in, ObIAllocator& alloc, number::ObNumber& out)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!ob_is_accurate_numeric_type(in_type))) {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(in_type));
} else if (ObNumberType == in_type || ObUNumberType == in_type) {
const number::ObNumber value(in.get_number());
if (OB_FAIL(out.from(value, alloc))) {
LOG_WARN("deep copy failed", K(ret), K(value));
} else if (OB_UNLIKELY(!out.is_integer()) || OB_UNLIKELY(out.is_negative())) {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(out));
} else if (OB_FAIL(out.round(0))) {
LOG_WARN("round failed", K(ret), K(out));
}
} else {
if (OB_UNLIKELY(in.get_int() < 0)) {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(in.get_int()));
} else if (OB_FAIL(out.from(in.get_int(), alloc))) {
LOG_WARN("deep copy failed", K(ret), K(in.get_int()));
}
}
return ret;
}
// %align_offset is used in mysql mode to align blob to other charset.
static int common_copy_string(
const ObExpr& expr, const ObString& src, ObEvalCtx& ctx, ObDatum& res_datum, const int64_t align_offset /* = 0 */)
{
int ret = OB_SUCCESS;
char* out_ptr = NULL;
int64_t len = align_offset + src.length();
if (expr.res_buf_len_ < src.length()) {
if (OB_ISNULL(out_ptr = expr.get_str_res_mem(ctx, len))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
}
} else {
out_ptr = const_cast<char*>(res_datum.ptr_);
}
if (OB_SUCC(ret)) {
MEMMOVE(out_ptr + align_offset, src.ptr(), len - align_offset);
MEMSET(out_ptr, 0, align_offset);
res_datum.set_string(out_ptr, len);
}
return ret;
}
static int common_copy_string_zf(
const ObExpr& expr, const ObString& src, ObEvalCtx& ctx, ObDatum& res_datum, const int64_t align_offset = 0)
{
int ret = OB_SUCCESS;
int64_t out_len = static_cast<uint8_t>(expr.datum_meta_.scale_);
if (out_len <= 0) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected zf length", K(ret), K(out_len), K(expr.datum_meta_.scale_));
} else if (CM_IS_ZERO_FILL(expr.extra_) && out_len > src.length()) {
char* out_ptr = NULL;
// out_ptr may overlap with src, so memmove is used.
if (expr.res_buf_len_ < out_len) {
if (OB_ISNULL(out_ptr = expr.get_str_res_mem(ctx, out_len))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
}
} else {
out_ptr = const_cast<char*>(res_datum.ptr_);
}
if (OB_SUCC(ret)) {
int64_t zf_len = out_len - src.length();
if (0 < zf_len) {
MEMMOVE(out_ptr + zf_len, src.ptr(), src.length());
MEMSET(out_ptr, '0', zf_len);
} else {
MEMMOVE(out_ptr, src.ptr(), out_len);
}
res_datum.set_string(ObString(out_len, out_ptr));
}
} else {
if (OB_FAIL(common_copy_string(expr, src, ctx, res_datum, align_offset))) {
LOG_WARN("common_copy_string failed", K(ret), K(src), K(expr));
}
}
return ret;
}
int ObDatumHexUtils::unhex(const ObExpr& expr, const ObString& in_str, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
UNUSED(expr);
char* buf = NULL;
const bool need_fill_zero = (1 == in_str.length() % 2);
const int32_t tmp_length = in_str.length() / 2 + need_fill_zero;
int32_t alloc_length = (0 == tmp_length ? 1 : tmp_length);
if (OB_ISNULL(buf = expr.get_str_res_mem(ctx, alloc_length))) {
res_datum.set_null();
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(alloc_length), K(ret));
} else {
bool all_valid_char = true;
int32_t i = 0;
char c1 = 0;
char c2 = 0;
if (in_str.length() > 0) {
if (need_fill_zero) {
c1 = '0';
c2 = in_str[0];
i = 0;
} else {
c1 = in_str[0];
c2 = in_str[1];
i = 1;
}
}
while (OB_SUCC(ret) && all_valid_char && i < in_str.length()) {
if (isxdigit(c1) && isxdigit(c2)) {
buf[i / 2] = (char)((get_xdigit(c1) << 4) | get_xdigit(c2));
c1 = in_str[++i];
c2 = in_str[++i];
} else if (lib::is_oracle_mode()) {
ret = OB_ERR_INVALID_HEX_NUMBER;
LOG_WARN("invalid hex number", K(ret), K(c1), K(c2), K(in_str));
} else {
all_valid_char = false;
res_datum.set_null();
}
}
if (OB_SUCC(ret) && all_valid_char) {
ObString str_res(tmp_length, buf);
// There will be no zero fill in the unhex() function, so it is directly assigned here
res_datum.pack_ = tmp_length;
res_datum.ptr_ = buf;
}
}
return ret;
}
static int common_get_nls_format(const ObBasicSessionInfo* session, const ObObjType in_type,
const bool force_use_standard_format, ObString& format_str)
{
int ret = OB_SUCCESS;
const ObString* session_nls_formats = NULL;
if (OB_ISNULL(session)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("session is NULL", K(ret));
} else {
session_nls_formats = session->get_local_nls_formats();
if (OB_ISNULL(session_nls_formats)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("nls_formats is NULL", K(ret));
}
}
if (OB_LIKELY(OB_SUCC(ret))) {
switch (in_type) {
case ObDateTimeType:
format_str = (force_use_standard_format ? ObTimeConverter::COMPAT_OLD_NLS_DATE_FORMAT
: (session_nls_formats[ObNLSFormatEnum::NLS_DATE].empty()
? ObTimeConverter::DEFAULT_NLS_DATE_FORMAT
: session_nls_formats[ObNLSFormatEnum::NLS_DATE]));
break;
case ObTimestampNanoType:
case ObTimestampLTZType:
format_str = (force_use_standard_format ? ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_FORMAT
: (session_nls_formats[ObNLSFormatEnum::NLS_TIMESTAMP].empty()
? ObTimeConverter::DEFAULT_NLS_TIMESTAMP_FORMAT
: session_nls_formats[ObNLSFormatEnum::NLS_TIMESTAMP]));
break;
case ObTimestampTZType:
format_str = (force_use_standard_format ? ObTimeConverter::COMPAT_OLD_NLS_TIMESTAMP_TZ_FORMAT
: (session_nls_formats[ObNLSFormatEnum::NLS_TIMESTAMP_TZ].empty()
? ObTimeConverter::DEFAULT_NLS_TIMESTAMP_TZ_FORMAT
: session_nls_formats[ObNLSFormatEnum::NLS_TIMESTAMP_TZ]));
break;
default:
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected in_type", K(in_type), K(ret));
break;
}
}
return ret;
}
static int common_int_datetime(const ObExpr& expr, const int64_t in_val, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
GET_SESSION()
{
int64_t out_val = 0;
int warning = OB_SUCCESS;
if (0 > in_val) {
ret = OB_INVALID_DATE_FORMAT;
} else {
ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), ObTimestampType == expr.datum_meta_.type_);
ret = ObTimeConverter::int_to_datetime(in_val, 0, cvrt_ctx, out_val);
}
if (CAST_FAIL(ret)) {
LOG_WARN("int_datetime failed", K(ret));
} else {
SET_RES_DATETIME(out_val);
}
}
return ret;
}
static OB_INLINE int common_int_number(const ObExpr& expr, int64_t in_val, ObIAllocator& alloc, number::ObNumber& nmb)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
ObObjType out_type = expr.datum_meta_.type_;
if ((ObUNumberType == out_type) && CAST_FAIL(numeric_negative_check(in_val))) {
LOG_WARN("numeric_negative_check faield", K(ret), K(in_val));
} else if (nmb.from(in_val, alloc)) {
LOG_WARN("nmb.from failed", K(ret), K(in_val));
}
return ret;
}
static OB_INLINE int common_int_date(const ObExpr& expr, const int64_t in_val, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
int32_t out_val = 0;
if (CAST_FAIL(ObTimeConverter::int_to_date(in_val, out_val))) {
LOG_WARN("int_to_date failed", K(ret), K(in_val), K(out_val));
} else {
SET_RES_DATE(out_val);
}
return ret;
}
static OB_INLINE int common_int_time(const ObExpr& expr, const int64_t in_val, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
int64_t out_val = 0;
if (CAST_FAIL(ObTimeConverter::int_to_time(in_val, out_val))) {
LOG_WARN("int_to_date failed", K(ret), K(in_val), K(out_val));
} else {
SET_RES_TIME(out_val);
}
return ret;
}
static OB_INLINE int common_int_year(const ObExpr& expr, const int64_t in_val, ObDatum& res_datum, int& warning)
{
int ret = OB_SUCCESS;
uint8_t out_val = 0;
if (CAST_FAIL(ObTimeConverter::int_to_year(in_val, out_val))) {
LOG_WARN("int_to_date failed", K(ret), K(in_val), K(out_val));
} else {
SET_RES_YEAR(out_val);
}
return ret;
}
static OB_INLINE int common_int_year(const ObExpr& expr, const int64_t in_val, ObDatum& res_datum)
{
int warning = OB_SUCCESS;
return common_int_year(expr, in_val, res_datum, warning);
}
static OB_INLINE int common_uint_int(
const ObExpr& expr, const ObObjType& out_type, uint64_t in_val, ObEvalCtx& ctx, int64_t& out_val)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
out_val = static_cast<int64_t>(in_val);
UNUSED(ctx);
if (CM_NEED_RANGE_CHECK(expr.extra_) && CAST_FAIL(int_upper_check(out_type, in_val, out_val))) {
LOG_WARN("int_upper_check failed", K(ret), K(in_val));
}
return ret;
}
static int common_string_int(const ObExpr& expr, const uint64_t& extra, const ObString& in_str,
const bool is_str_integer_cast, ObDatum& res_datum, int& warning)
{
int ret = OB_SUCCESS;
ObObjType in_type = expr.args_[0]->datum_meta_.type_;
ObObjType out_type = expr.datum_meta_.type_;
int64_t out_val = 0;
ret = common_string_integer(extra, in_type, in_str, is_str_integer_cast, out_val);
if (CAST_FAIL_CM(ret, extra)) {
LOG_WARN("string_int failed", K(ret));
} else if (out_type < ObIntType && CAST_FAIL_CM(int_range_check(out_type, out_val, out_val), extra)) {
LOG_WARN("int_range_check failed", K(ret));
} else {
SET_RES_INT(out_val);
}
return ret;
}
static int common_string_int(const ObExpr& expr, const uint64_t& extra, const ObString& in_str,
const bool is_str_integer_cast, ObDatum& res_datum)
{
int warning = OB_SUCCESS;
return common_string_int(expr, extra, in_str, is_str_integer_cast, res_datum, warning);
}
static int common_string_uint(
const ObExpr& expr, const ObString& in_str, const bool is_str_integer_cast, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
uint64_t out_val = 0;
DEF_IN_OUT_TYPE();
ret = common_string_unsigned_integer(expr.extra_, in_type, in_str, is_str_integer_cast, out_val);
if (CAST_FAIL(ret)) {
LOG_WARN("string_int failed", K(ret));
} else if (out_type < ObUInt64Type && CM_NEED_RANGE_CHECK(expr.extra_) &&
CAST_FAIL(uint_upper_check(out_type, out_val))) {
LOG_WARN("uint_upper_check failed", K(ret));
} else {
SET_RES_UINT(out_val);
}
return ret;
}
int common_string_double(
const ObExpr& expr, const ObObjType& in_type, const ObObjType& out_type, const ObString& in_str, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
double out_val = 0.0;
if (ObHexStringType == in_type) {
out_val = static_cast<double>(hex_to_uint64(in_str));
} else {
int err = 0;
char* endptr = NULL;
out_val = ObCharset::strntod(in_str.ptr(), in_str.length(), &endptr, &err);
if (EOVERFLOW == err && (-DBL_MAX == out_val || DBL_MAX == out_val)) {
ret = OB_DATA_OUT_OF_RANGE;
} else {
ObString tmp_str = in_str;
ObString trimed_str = tmp_str.trim();
if (lib::is_mysql_mode() && 0 == trimed_str.length()) {
if (!CM_IS_COLUMN_CONVERT(expr.extra_)) {
/* do nothing */
} else {
ret = OB_ERR_DOUBLE_TRUNCATED;
LOG_WARN("convert string to double failed", K(ret), K(in_str));
}
} else if (OB_FAIL(check_convert_str_err(in_str.ptr(), endptr, in_str.length(), err))) {
LOG_WARN("failed to check_convert_str_err", K(ret), K(in_str), K(out_val), K(err));
ret = OB_ERR_DOUBLE_TRUNCATED;
if (CM_IS_WARN_ON_FAIL(expr.extra_)) {
LOG_USER_WARN(OB_ERR_DOUBLE_TRUNCATED, in_str.length(), in_str.ptr());
}
}
}
}
if (CAST_FAIL(ret)) {
LOG_WARN("string_double failed", K(ret));
} else if (ObUDoubleType == out_type && CAST_FAIL(numeric_negative_check(out_val))) {
LOG_WARN("numeric_negative_check failed", K(ret), K(out_val));
} else {
SET_RES_DOUBLE(out_val);
}
LOG_DEBUG("common_string_double", K(ret), K(warning), K(out_val), K(in_str));
return ret;
}
static OB_INLINE int common_double_float(const ObExpr& expr, const double in_val, float& out_val)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
out_val = static_cast<float>(in_val);
ObObjType out_type = expr.datum_meta_.type_;
if (CAST_FAIL(real_range_check(out_type, in_val, out_val))) {
LOG_WARN("real_range_check failed", K(ret), K(in_val));
}
return ret;
}
static OB_INLINE int common_string_float(const ObExpr& expr, const ObString& in_str, float& out_val)
{
int ret = OB_SUCCESS;
double tmp_double = 0.0;
ObDatum tmp_datum;
tmp_datum.ptr_ = reinterpret_cast<const char*>(&tmp_double);
tmp_datum.pack_ = sizeof(double);
DEF_IN_OUT_TYPE();
if (OB_FAIL(common_string_double(expr, in_type, out_type, in_str, tmp_datum))) {
LOG_WARN("common_string_double failed", K(ret), K(in_str));
} else if (OB_FAIL(common_double_float(expr, tmp_double, out_val))) {
LOG_WARN("common_double_float failed", K(ret), K(tmp_double));
}
return ret;
}
static OB_INLINE int common_string_date(const ObExpr& expr, const ObString& in_str, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
int32_t out_val = 0;
if (CAST_FAIL(ObTimeConverter::str_to_date(in_str, out_val))) {
LOG_WARN("str_to_date failed", K(ret), K(in_str));
} else {
SET_RES_DATE(out_val);
}
return ret;
}
static OB_INLINE int common_string_time(const ObExpr& expr, const ObString& in_str, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
int64_t out_val = 0;
ObScale res_scale; // useless
if (CAST_FAIL(ObTimeConverter::str_to_time(in_str, out_val, &res_scale))) {
LOG_WARN("str_to_time failed", K(ret), K(in_str));
} else {
SET_RES_TIME(out_val);
}
return ret;
}
static OB_INLINE int common_string_year(const ObExpr& expr, const ObString& in_str, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
const bool is_str_int_cast = false;
// eg: insert into t1(col_year) values('201a');
// will give out of range error by common_int_year(because in_val is 201, invalid year)
// so need to unset warn on fail when do string_int cast.
const uint64_t extra = CM_UNSET_STRING_INTEGER_TRUNC(CM_SET_WARN_ON_FAIL(expr.extra_));
// datum size of year type is one byte!
int64_t tmp_int = 0;
ObDatum tmp_res;
tmp_res.int_ = &tmp_int;
tmp_res.pack_ = sizeof(tmp_int);
if (OB_FAIL(common_string_int(expr, extra, in_str, is_str_int_cast, tmp_res, warning))) {
LOG_WARN("common_string_int failed", K(ret), K(in_str));
} else {
if (CAST_FAIL(common_int_year(expr, tmp_int, res_datum, warning))) {
LOG_WARN("common_int_year failed", K(ret), K(tmp_int));
} else {
int tmp_warning = warning;
CAST_FAIL(tmp_warning);
}
}
return ret;
}
static OB_INLINE int common_string_number(
const ObExpr& expr, const ObString& in_str, ObIAllocator& alloc, number::ObNumber& nmb)
{
int ret = OB_SUCCESS;
DEF_IN_OUT_TYPE();
if (ObHexStringType == in_type) {
ret = nmb.from(hex_to_uint64(in_str), alloc);
} else if (0 == in_str.length()) {
// in mysql mode, this err will be ignored(because default cast_mode is WARN_ON_FAIL)
nmb.set_zero();
ret = OB_ERR_TRUNCATED_WRONG_VALUE_FOR_FIELD;
} else {
ObPrecision res_precision; // useless
ObScale res_scale;
ret = nmb.from_sci_opt(in_str.ptr(), in_str.length(), alloc, &res_precision, &res_scale);
// select cast('1e500' as decimal); -> max_val
// select cast('-1e500' as decimal); -> min_val
if (OB_NUMERIC_OVERFLOW == ret && CM_IS_SET_MIN_IF_OVERFLOW(expr.extra_)) {
int64_t i = 0;
while (i < in_str.length() && isspace(in_str[i])) {
++i;
}
bool is_neg = (in_str[i] == '-');
int tmp_ret = OB_SUCCESS;
const ObAccuracy& def_acc = ObAccuracy::DDL_DEFAULT_ACCURACY2[0][out_type];
const ObPrecision prec = def_acc.get_precision();
const ObScale scale = def_acc.get_scale();
const number::ObNumber* bound_num = NULL;
if (is_neg) {
bound_num = &(ObNumberConstValue::MYSQL_MIN[prec][scale]);
} else {
bound_num = &(ObNumberConstValue::MYSQL_MAX[prec][scale]);
}
if (OB_ISNULL(bound_num)) {
tmp_ret = OB_ERR_UNEXPECTED;
LOG_WARN("bound_num is NULL", K(tmp_ret), K(ret), K(is_neg));
} else if (OB_SUCCESS != (tmp_ret = nmb.from(*bound_num, alloc))) {
LOG_WARN("copy min number failed", K(ret), K(tmp_ret), KPC(bound_num));
}
}
}
if (CAST_FAIL(ret)) {
LOG_WARN("string_number failed", K(ret));
} else if (ObUNumberType == out_type && CAST_FAIL(numeric_negative_check(nmb))) {
LOG_WARN("numeric_negative_check failed", K(ret));
}
return ret;
}
static OB_INLINE int common_string_datetime(
const ObExpr& expr, const ObString& in_str, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
int warning = OB_SUCCESS;
int64_t out_val = 0;
GET_SESSION()
{
ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), ObTimestampType == expr.datum_meta_.type_);
if (lib::is_oracle_mode()) {
if (OB_FAIL(common_get_nls_format(session,
expr.datum_meta_.type_,
CM_IS_FORCE_USE_STANDARD_NLS_FORMAT(expr.extra_),
cvrt_ctx.oracle_nls_format_))) {
LOG_WARN("common_get_nls_format failed", K(ret));
} else if (CAST_FAIL(ObTimeConverter::str_to_date_oracle(in_str, cvrt_ctx, out_val))) {
LOG_WARN("str_to_date_oracle failed", K(ret));
}
} else {
ObScale res_scale; // useless
if (CAST_FAIL(ObTimeConverter::str_to_datetime(in_str, cvrt_ctx, out_val, &res_scale))) {
LOG_WARN("str_to_datetime failed", K(ret), K(in_str));
}
}
if (OB_SUCC(ret)) {
SET_RES_DATETIME(out_val);
}
}
return ret;
}
static OB_INLINE int common_get_bit_len(const ObString& str, int32_t& bit_len)
{
int ret = OB_SUCCESS;
if (str.empty()) {
bit_len = 1;
} else {
const char* ptr = str.ptr();
uint32_t uneven_value = reinterpret_cast<const unsigned char&>(ptr[0]);
if (0 == uneven_value) {
bit_len = 1;
} else {
// Built-in Function: int __builtin_clz (unsigned int x).
// Returns the number of leading 0-bits in x, starting at the most significant bit position.
// If x is 0, the result is undefined.
int32_t len = str.length();
int32_t uneven_len = static_cast<int32_t>(sizeof(unsigned int) * 8 - __builtin_clz(uneven_value));
bit_len = uneven_len + 8 * (len - 1);
}
}
return ret;
}
static int common_string_bit(const ObExpr& expr, const ObString& in_str, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
UNUSED(ctx);
int warning = OB_SUCCESS;
int32_t bit_len = 0;
if (OB_FAIL(common_get_bit_len(in_str, bit_len))) {
LOG_WARN("common_get_bit_len failed", K(ret));
} else {
ObObjType in_type = expr.args_[0]->datum_meta_.type_;
uint64_t out_val = 0;
if (ObHexStringType == in_type && in_str.empty()) {
ret = OB_ERR_TRUNCATED_WRONG_VALUE_FOR_FIELD;
LOG_WARN("hex string is empty, can't cast to bit", K(ret), K(in_str));
} else if (bit_len > OB_MAX_BIT_LENGTH) {
out_val = UINT64_MAX;
ret = OB_ERR_DATA_TOO_LONG;
LOG_WARN("bit type length is too long", K(ret), K(in_str), K(OB_MAX_BIT_LENGTH), K(bit_len));
} else {
out_val = hex_to_uint64(in_str);
}
if (OB_FAIL(ret) && CM_IS_WARN_ON_FAIL(expr.extra_)) {
warning = OB_DATA_OUT_OF_RANGE;
ret = OB_SUCCESS;
}
if (OB_SUCC(ret)) {
SET_RES_BIT(out_val);
}
}
return ret;
}
// Copy the result from get_str_res_mem to get_reset_tmp_alloc space
int copy_datum_str_with_tmp_alloc(ObEvalCtx& ctx, ObDatum& res_datum, ObString& res_str)
{
int ret = OB_SUCCESS;
ObIAllocator& calc_alloc = ctx.get_reset_tmp_alloc();
char* tmp_res = NULL;
const ObString str = res_datum.get_string();
if (0 == str.length()) {
res_str.assign_ptr(NULL, 0);
} else if (OB_ISNULL(tmp_res = static_cast<char*>(calc_alloc.alloc(str.length())))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else {
MEMMOVE(tmp_res, str.ptr(), str.length());
res_str.assign_ptr(tmp_res, str.length());
}
return ret;
}
int common_check_convert_string(const ObExpr& expr, ObEvalCtx& ctx, const ObString& in_str, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
ObObjType out_type = expr.datum_meta_.type_;
ObCollationType out_cs_type = expr.datum_meta_.cs_type_;
ObObjType in_type = expr.args_[0]->datum_meta_.type_;
ObCollationType in_cs_type = expr.args_[0]->datum_meta_.cs_type_;
if (lib::is_oracle_mode() && (ob_is_blob(out_type, out_cs_type) || ob_is_blob_locator(out_type, out_cs_type)) &&
!(ob_is_blob(in_type, in_cs_type) || ob_is_blob_locator(in_type, in_cs_type))) {
// !blob -> blob
if (ObCharType == in_type || ObVarcharType == in_type) {
if (OB_FAIL(ObDatumHexUtils::hextoraw_string(expr, in_str, ctx, res_datum))) {
LOG_WARN("fail to hextoraw_string for blob", K(ret), K(in_str));
}
} else {
LOG_ERROR("invalid use of blob type", K(ret), K(in_str), K(out_type));
LOG_USER_ERROR(OB_NOT_SUPPORTED, "cast to blob type");
}
} else {
// When convert blob/binary/varbinary to other charset, need to align to mbminlen of destination charset