forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_json_table_op.cpp
More file actions
3371 lines (3129 loc) · 121 KB
/
Copy pathob_json_table_op.cpp
File metadata and controls
3371 lines (3129 loc) · 121 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.
* This file contains implementation support for the json table abstraction.
*/
#define USING_LOG_PREFIX SQL_ENG
#include "ob_json_table_op.h"
#include "share/object/ob_obj_cast_util.h"
#include "share/object/ob_obj_cast.h"
#include "common/sql_mode/ob_sql_mode_utils.h"
#include "sql/ob_sql_utils.h"
#include "sql/engine/expr/ob_datum_cast.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/ob_physical_plan.h"
#include "sql/engine/expr/ob_expr_json_func_helper.h"
namespace oceanbase
{
using namespace common;
namespace sql
{
/* json table empty or error */
const static int32_t JSN_TABLE_ERROR = 0;
const static int32_t JSN_TABLE_NULL = 1;
const static int32_t JSN_TABLE_DEFAULT = 2;
const static int32_t JSN_TABLE_IMPLICIT = 3;
/* json query empty or error */
const static int32_t JSN_QUERY_ERROR = 0;
const static int32_t JSN_QUERY_NULL = 1;
const static int32_t JSN_QUERY_EMPTY = 2;
const static int32_t JSN_QUERY_EMPTY_ARRAY = 3;
const static int32_t JSN_QUERY_EMPTY_OBJECT = 4;
const static int32_t JSN_QUERY_IMPLICIT = 5;
/* json query on mismatch { error : 0, null : 1, implicit : 2 }*/
const static int32_t JSN_QUERY_MISMATCH_ERROR = 0;
const static int32_t JSN_QUERY_MISMATCH_NULL = 1;
const static int32_t JSN_QUERY_MISMATCH_IMPLICIT = 2;
/* json query wrapper type */
const static int32_t JSN_QUERY_WITHOUT_WRAPPER = 0;
const static int32_t JSN_QUERY_WITHOUT_ARRAY_WRAPPER = 1;
const static int32_t JSN_QUERY_WITH_WRAPPER = 2;
const static int32_t JSN_QUERY_WITH_ARRAY_WRAPPER = 3;
const static int32_t JSN_QUERY_WITH_UNCONDITIONAL_WRAPPER = 4;
const static int32_t JSN_QUERY_WITH_CONDITIONAL_WRAPPER = 5;
const static int32_t JSN_QUERY_WITH_UNCONDITIONAL_ARRAY_WRAPPER = 6;
const static int32_t JSN_QUERY_WITH_CONDITIONAL_ARRAY_WRAPPER = 7;
const static int32_t JSN_QUERY_WRAPPER_IMPLICIT = 8;
/* json query on scalars { allow : 0, disallow : 1, implicit : 2 }*/
const static int32_t JSN_QUERY_SCALARS_ALLOW = 0;
const static int32_t JSN_QUERY_SCALARS_DISALLOW = 1;
const static int32_t JSN_QUERY_SCALARS_IMPLICIT = 2;
/* json value empty or error */
const static int32_t JSN_VALUE_ERROR = 0;
const static int32_t JSN_VALUE_NULL = 1;
const static int32_t JSN_VALUE_DEFAULT = 2;
const static int32_t JSN_VALUE_IMPLICIT = 3;
/* json value on mismatch { error : 0, null : 1, ignore : 2 }*/
const static int32_t JSN_VALUE_MISMATCH_ERROR = 0;
const static int32_t JSN_VALUE_MISMATCH_NULL = 1;
const static int32_t JSN_VALUE_MISMATCH_IGNORE = 2;
const static int32_t JSN_VALUE_MISMATCH_IMPLICIT = 3;
/* json value mismatch type { MISSING : 0, EXTRA : 1, TYPE : 2, EMPTY : 3} */
const static int32_t JSN_VALUE_TYPE_MISSING_DATA = 0;
const static int32_t JSN_VALUE_TYPE_EXTRA_DATA = 1;
const static int32_t JSN_VALUE_TYPE_TYPE_ERROR = 2;
const static int32_t JSN_VALUE_TYPE_IMPLICIT = 3;
/* json exists */
const static int32_t JSN_EXIST_FALSE = 0;
const static int32_t JSN_EXIST_TRUE = 1;
const static int32_t JSN_EXIST_ERROR = 2;
const static int32_t JSN_EXIST_DEFAULT = 3;
#define SET_COVER_ERROR(jt_ctx_ptr, error_code) \
{\
if (!(jt_ctx_ptr)->is_cover_error_) { \
(jt_ctx_ptr)->is_cover_error_ = true; \
(jt_ctx_ptr)->error_code_ = error_code; \
} \
}
#define EVAL_COVER_CODE(jt_ctx_ptr, error_code) \
{\
if ((jt_ctx_ptr)->is_cover_error_) { \
error_code = (jt_ctx_ptr)->error_code_; \
} \
}
#define RESET_COVER_CODE(jt_ctx_ptr) \
{\
if ((jt_ctx_ptr)->is_cover_error_) { \
(jt_ctx_ptr)->is_cover_error_ = false; \
(jt_ctx_ptr)->error_code_ = 0; \
} \
}
int JtFuncHelpler::cast_to_int(ObIJsonBase *j_base, ObObjType dst_type, int64_t &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_int(val, true))) {
ret = OB_OPERATE_OVERFLOW;
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, "SIGNED", "json_table");
LOG_WARN("cast to int failed", K(ret), K(*j_base));
} else if (dst_type < ObIntType &&
OB_FAIL(int_range_check(dst_type, val, val))) {
ret = OB_OPERATE_OVERFLOW;
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, "SIGNED", "json_table");
}
return ret;
}
int JtFuncHelpler::cast_to_uint(ObIJsonBase *j_base, ObObjType dst_type, uint64_t &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_uint(val, true, true))) {
LOG_WARN("cast to uint failed", K(ret), K(*j_base));
if (ret == OB_OPERATE_OVERFLOW) {
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, "UNSIGNED", "json_table");
}
} else if (dst_type < ObUInt64Type &&
OB_FAIL(uint_upper_check(dst_type, val))) {
LOG_WARN("uint_upper_check failed", K(ret));
}
return ret;
}
int JtFuncHelpler::number_range_check(const ObAccuracy &accuracy,
ObIAllocator *allocator,
number::ObNumber &val,
bool strict)
{
INIT_SUCC(ret);
ObPrecision precision = accuracy.get_precision();
ObScale scale = accuracy.get_scale();
const number::ObNumber *min_check_num = NULL;
const number::ObNumber *max_check_num = NULL;
const number::ObNumber *min_num_mysql = NULL;
const number::ObNumber *max_num_mysql = NULL;
bool is_finish = false;
if (lib::is_oracle_mode()) {
if (OB_MAX_NUMBER_PRECISION >= precision
&& precision >= OB_MIN_NUMBER_PRECISION
&& number::ObNumber::MAX_SCALE >= scale
&& scale >= number::ObNumber::MIN_SCALE) {
min_check_num = &(ObNumberConstValue::ORACLE_CHECK_MIN[precision][scale + ObNumberConstValue::MAX_ORACLE_SCALE_DELTA]);
max_check_num = &(ObNumberConstValue::ORACLE_CHECK_MAX[precision][scale + ObNumberConstValue::MAX_ORACLE_SCALE_DELTA]);
} else if (ORA_NUMBER_SCALE_UNKNOWN_YET == scale
&& PRECISION_UNKNOWN_YET == precision) {
is_finish = true;
} else if (PRECISION_UNKNOWN_YET == precision
&& number::ObNumber::MAX_SCALE >= scale
&& scale >= number::ObNumber::MIN_SCALE) {
number::ObNumber num;
if (OB_FAIL(num.from(val, *allocator))) {
} else if (OB_FAIL(num.round(scale))) {
} else if (val.compare(num) != 0) {
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("input value is out of range.", K(scale), K(val));
} else {
is_finish = true;
}
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid arguments", K(ret), K(precision), K(scale));
}
} else {
if (OB_UNLIKELY(precision < scale)) {
ret = OB_ERR_M_BIGGER_THAN_D;
LOG_WARN("Invalid accuracy.", K(ret), K(scale), K(precision));
} else if (number::ObNumber::MAX_PRECISION >= precision
&& precision >= OB_MIN_DECIMAL_PRECISION
&& number::ObNumber::MAX_SCALE >= scale
&& scale >= 0) {
min_check_num = &(ObNumberConstValue::MYSQL_CHECK_MIN[precision][scale]);
max_check_num = &(ObNumberConstValue::MYSQL_CHECK_MAX[precision][scale]);
min_num_mysql = &(ObNumberConstValue::MYSQL_MIN[precision][scale]);
max_num_mysql = &(ObNumberConstValue::MYSQL_MAX[precision][scale]);
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid arguments", K(ret), K(precision), K(scale));
}
}
if (OB_SUCC(ret) && !is_finish) {
if (OB_ISNULL(min_check_num) || OB_ISNULL(max_check_num)
|| (!lib::is_oracle_mode()
&& (OB_ISNULL(min_num_mysql) || OB_ISNULL(max_num_mysql)))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("min_num or max_num is null", K(ret), KPC(min_check_num), KPC(max_check_num));
} else if (val <= *min_check_num) {
if (lib::is_oracle_mode()) {
ret = OB_ERR_VALUE_LARGER_THAN_ALLOWED;
} else {
ret = OB_DATA_OUT_OF_RANGE;
}
LOG_WARN("val is out of min range check.", K(val), K(*min_check_num));
is_finish = true;
} else if (val >= *max_check_num) {
if (lib::is_oracle_mode()) {
ret = OB_ERR_VALUE_LARGER_THAN_ALLOWED;
} else {
ret = OB_DATA_OUT_OF_RANGE;
}
LOG_WARN("val is out of max range check.", K(val), K(*max_check_num));
is_finish = true;
} else {
ObNumStackOnceAlloc tmp_alloc;
number::ObNumber num;
if (OB_FAIL(num.from(val, tmp_alloc))) {
} else if (OB_FAIL(num.round(scale))) {
LOG_WARN("num.round failed", K(ret), K(scale));
} else {
if (strict) {
if (num.compare(val) != 0) {
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("input value is out of range.", K(scale), K(val));
} else {
is_finish = true;
}
} else {
if (OB_ISNULL(allocator)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("allocator is null", K(ret));
} else if (OB_FAIL(val.deep_copy_v3(num, *allocator))) {
LOG_WARN("val.deep_copy_v3 failed", K(ret), K(num));
} else {
is_finish = true;
}
}
}
}
}
if (OB_SUCC(ret) && !is_finish) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected situation, res is not set", K(ret));
}
LOG_DEBUG("number_range_check_v2 done", K(ret), K(is_finish), K(accuracy), K(val),
KPC(min_check_num), KPC(max_check_num));
return ret;
}
int JtFuncHelpler::datetime_scale_check(const ObAccuracy &accuracy, int64_t &value, bool strict)
{
INIT_SUCC(ret);
ObScale scale = accuracy.get_scale();
if (OB_UNLIKELY(scale > MAX_SCALE_FOR_TEMPORAL)) {
ret = OB_ERR_TOO_BIG_PRECISION;
LOG_USER_ERROR(OB_ERR_TOO_BIG_PRECISION, scale, "CAST",
static_cast<int64_t>(MAX_SCALE_FOR_TEMPORAL));
} else if (OB_UNLIKELY(0 <= scale && scale < MAX_SCALE_FOR_TEMPORAL)) {
// first check zero
if (strict &&
(value == ObTimeConverter::ZERO_DATE ||
value == ObTimeConverter::ZERO_DATETIME)) {
ret = OB_INVALID_DATE_VALUE;
LOG_WARN("Zero datetime is invalid in json_value.", K(value));
} else {
int64_t temp_value = value;
ObTimeConverter::round_datetime(scale, temp_value);
if (strict && temp_value != value) {
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("Invalid input value.", K(value), K(scale));
} else if (ObTimeConverter::is_valid_datetime(temp_value)) {
value = temp_value;
} else {
ret = OB_ERR_NULL_VALUE; // set null for res
LOG_DEBUG("Invalid datetime val, return set_null", K(temp_value));
}
}
}
return ret;
}
int JtFuncHelpler::time_scale_check(const ObAccuracy &accuracy, int64_t &value, bool strict)
{
INIT_SUCC(ret);
ObScale scale = accuracy.get_scale();
if (OB_LIKELY(0 <= scale && scale < MAX_SCALE_FOR_TEMPORAL)) {
int64_t temp_value = value;
ObTimeConverter::round_datetime(scale, temp_value);
if (strict && temp_value != value) { // round success
ret = OB_OPERATE_OVERFLOW;
LOG_WARN("Invalid input value.", K(value), K(scale));
} else {
value = temp_value;
}
} else {
// consistent with cast process do nothing
}
return ret;
}
// padding %padding_cnt character, we also need to convert collation type here.
// eg: select cast('abc' as nchar(100)) from dual;
// the space must be in utf16, because dst_type is nchar
int JtFuncHelpler::padding_char_for_cast(int64_t padding_cnt, const ObCollationType &padding_cs_type,
ObIAllocator &alloc, ObString &padding_res)
{
int ret = OB_SUCCESS;
padding_res.reset();
const ObCharsetType &cs = ObCharset::charset_type_by_coll(padding_cs_type);
char padding_char = (CHARSET_BINARY == cs) ? OB_PADDING_BINARY : OB_PADDING_CHAR;
int64_t padding_str_size = sizeof(padding_char) * padding_cnt;
char *padding_str_ptr = reinterpret_cast<char*>(alloc.alloc(padding_str_size));
if (OB_ISNULL(padding_str_ptr)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(ret));
} else if (CHARSET_BINARY == cs) {
MEMSET(padding_str_ptr, padding_char, padding_str_size);
padding_res.assign_ptr(padding_str_ptr, padding_str_size);
} else {
MEMSET(padding_str_ptr, padding_char, padding_str_size);
ObString padding_str(padding_str_size, padding_str_ptr);
if (OB_FAIL(ObExprUtil::convert_string_collation(padding_str,
ObCharset::get_system_collation(),
padding_res,
padding_cs_type,
alloc))) {
LOG_WARN("convert padding str collation faield", K(ret), K(padding_str),
K(padding_cs_type));
}
}
LOG_DEBUG("pad char done", K(ret), K(padding_cnt), K(padding_cs_type), K(padding_res));
return ret;
}
int JtFuncHelpler::cast_to_string(JtColNode* node,
common::ObIAllocator *allocator,
ObIJsonBase *j_base,
ObCollationType in_cs_type,
ObCollationType dst_cs_type,
common::ObAccuracy &accuracy,
ObObjType dst_type,
ObString &val,
bool is_trunc,
bool is_quote,
bool is_const)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_ISNULL(allocator)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("allocator is null", K(ret));
} else {
ObJsonBuffer j_buf(allocator);
if (OB_FAIL(j_base->print(j_buf, is_quote))) {
LOG_WARN("fail to_string as json", K(ret));
} else {
ObObjType in_type = ObLongTextType;
ObString temp_str_val(j_buf.length(), j_buf.ptr());
bool is_need_string_string_convert = ((CS_TYPE_BINARY == dst_cs_type) ||
(ObCharset::charset_type_by_coll(in_cs_type) !=
ObCharset::charset_type_by_coll(dst_cs_type)))
&& !(lib::is_mysql_mode() && temp_str_val.length() == 0);
if (is_need_string_string_convert) {
if (CS_TYPE_BINARY != in_cs_type
&& CS_TYPE_BINARY != dst_cs_type
&& (ObCharset::charset_type_by_coll(in_cs_type) !=
ObCharset::charset_type_by_coll(dst_cs_type))) {
char *buf = NULL;
const int64_t factor = 2;
int64_t buf_len = temp_str_val.length() * factor;
uint32_t result_len = 0;
buf = static_cast<char*>(allocator->alloc(buf_len));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("alloc memory failed", K(ret));
} else if (OB_FAIL(ObCharset::charset_convert(in_cs_type, temp_str_val.ptr(),
temp_str_val.length(), dst_cs_type, buf,
buf_len, result_len))) {
LOG_WARN("charset convert failed", K(ret));
} else {
val.assign_ptr(buf, result_len);
}
} else {
if (CS_TYPE_BINARY == in_cs_type || CS_TYPE_BINARY == dst_cs_type) {
// just copy string when in_cs_type or out_cs_type is binary
const ObCharsetInfo *cs = NULL;
int64_t align_offset = 0;
if (CS_TYPE_BINARY == in_cs_type && (NULL != (cs = ObCharset::get_charset(dst_cs_type)))) {
if (cs->mbminlen > 0 && temp_str_val.length() % cs->mbminlen != 0) {
align_offset = cs->mbminlen - temp_str_val.length() % cs->mbminlen;
}
}
int64_t len = align_offset + temp_str_val.length();
char *buf = reinterpret_cast<char*>(allocator->alloc(len));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else {
MEMMOVE(buf + align_offset, temp_str_val.ptr(), len - align_offset);
MEMSET(buf, 0, align_offset);
val.assign_ptr(buf, len);
}
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("same charset should not be here, just use cast_eval_arg", K(ret),
K(in_type), K(dst_type), K(in_cs_type), K(dst_cs_type));
}
}
} else {
val.assign_ptr(temp_str_val.ptr(), temp_str_val.length());
}
int32_t str_len_char;
ObLength max_accuracy_len;
if (lib::is_mysql_mode()) {
str_len_char = static_cast<int32_t>(ObCharset::strlen_char(dst_cs_type, val.ptr(), val.length()));
max_accuracy_len = (ob_obj_type_class(dst_type) == ObTextTC
|| ob_obj_type_class(dst_type) == ObJsonTC)
? ObAccuracy::DDL_DEFAULT_ACCURACY[dst_type].get_length()
: accuracy.get_length();
if (OB_SUCC(ret)) {
if (max_accuracy_len == DEFAULT_STR_LENGTH) { // default string len
} else if (max_accuracy_len <= 0 || str_len_char > max_accuracy_len) {
ret = OB_OPERATE_OVERFLOW;
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, "column", "json_table");
LOG_WARN("length oversize", K(ret), K(str_len_char), K(max_accuracy_len));
}
}
if (OB_SUCC(ret) && ObCharType == dst_type && CS_TYPE_BINARY == dst_cs_type) { // binary need padding
int64_t text_length = val.length();
if (max_accuracy_len > text_length) {
int64_t padding_cnt = max_accuracy_len - text_length;
ObString padding_res;
if (OB_FAIL(JtFuncHelpler::padding_char_for_cast(padding_cnt, dst_cs_type, *allocator,
padding_res))) {
LOG_WARN("padding char failed", K(ret), K(padding_cnt), K(dst_cs_type));
} else {
int64_t padding_size = padding_res.length() + val.length();
char *buf = reinterpret_cast<char*>(allocator->alloc(padding_size));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else {
MEMMOVE(buf, val.ptr(), val.length());
MEMMOVE(buf + val.length(), padding_res.ptr(), padding_res.length());
val.assign_ptr(buf, padding_size);
}
}
}
}
} else {
ObLengthSemantics senmactics = node->col_info_.data_type_.get_length_semantics();
// do str length check
str_len_char = static_cast<int32_t>(ObCharset::strlen_char(
senmactics == LS_BYTE ? CS_TYPE_BINARY : dst_cs_type, val.ptr(), val.length()));
max_accuracy_len = (dst_type == ObLongTextType) ? OB_MAX_LONGTEXT_LENGTH : accuracy.get_length();
max_accuracy_len *= (senmactics == LS_BYTE ? 1 : 2);
uint32_t byte_len = 0;
byte_len = ObCharset::charpos(senmactics == LS_BYTE ? CS_TYPE_BINARY : dst_cs_type, val.ptr(), str_len_char, max_accuracy_len);
if (OB_SUCC(ret)) {
if (max_accuracy_len == DEFAULT_STR_LENGTH) { // default string len
} else if (is_trunc && max_accuracy_len < str_len_char) {
if (!is_const &&
(node->col_info_.col_type_ == static_cast<int>(COL_TYPE_EXISTS)
|| j_base->json_type() == ObJsonNodeType::J_INT
|| j_base->json_type() == ObJsonNodeType::J_UINT
|| j_base->json_type() == ObJsonNodeType::J_BOOLEAN
|| j_base->json_type() == ObJsonNodeType::J_DOUBLE
|| j_base->json_type() == ObJsonNodeType::J_DECIMAL)) {
ret = OB_ERR_VALUE_EXCEEDED_MAX;
} else {
// bugfix:
// Q1:SELECT c1 ,jt.ww b_c1 FROM t1, json_table ( c2 columns( ww varchar2(2 char) truncate path '$.a')) jt ;
// Q2:SELECT c1 ,jt.ww b_c1 FROM t1, json_table ( c2 columns( ww varchar2(2 byte) truncate path '$.a')) jt;
// should not split in the middle of char
if (byte_len == 0) { // value has zero length
val.assign_ptr("", 0);
} else if (senmactics == LS_BYTE && dst_cs_type != CS_TYPE_BINARY) {
int64_t char_len; // not used
// zero max_accuracy_len not allowed
byte_len = ObCharset::max_bytes_charpos(dst_cs_type, val.ptr(), str_len_char, max_accuracy_len, char_len);
if (byte_len == 0) { // buffer not enough for one bytes
ret = OB_OPERATE_OVERFLOW;
} else {
val.assign_ptr(val.ptr(), byte_len);
}
} else {
val.assign_ptr(val.ptr(), byte_len);
}
}
} else if (max_accuracy_len <= 0 || str_len_char > max_accuracy_len) {
ret = OB_OPERATE_OVERFLOW;
}
}
}
}
}
return ret;
}
bool JtFuncHelpler::type_cast_to_string(JtColNode* node,
ObString &json_string,
common::ObIAllocator *allocator,
ObIJsonBase *j_base,
ObAccuracy &accuracy)
{
INIT_SUCC(ret);
ret = cast_to_string(node, allocator, j_base, CS_TYPE_BINARY, CS_TYPE_BINARY, accuracy, ObLongTextType, json_string);
return ret == 0 ? true : false;
}
int JtFuncHelpler::cast_to_datetime(JtColNode* node,
ObIJsonBase *j_base,
common::ObIAllocator *allocator,
const ObBasicSessionInfo *session,
common::ObAccuracy &accuracy,
int64_t &val)
{
INIT_SUCC(ret);
ObString json_string;
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
oceanbase::common::ObTimeConvertCtx cvrt_ctx(session->get_timezone_info(), false);
if (lib::is_oracle_mode()) {
if (OB_FAIL(common_get_nls_format(session, ObDateTimeType,
true,
cvrt_ctx.oracle_nls_format_))) {
LOG_WARN("common_get_nls_format failed", K(ret));
} else if (type_cast_to_string(node, json_string, allocator, j_base, accuracy) && json_string.length() > 0) {
ObJsonString json_str(json_string.ptr(),json_string.length());
if (OB_FAIL(json_str.to_datetime(val, &cvrt_ctx))) {
LOG_WARN("wrapper to datetime failed.", K(ret), K(*j_base));
}
} else if (OB_FAIL(j_base->to_datetime(val, &cvrt_ctx))) {
LOG_WARN("wrapper to datetime failed.", K(ret), K(*j_base));
} else if (OB_FAIL(datetime_scale_check(accuracy, val))) {
LOG_WARN("datetime_scale_check failed.", K(ret));
}
} else {
if (OB_FAIL(j_base->to_datetime(val, &cvrt_ctx))) {
LOG_WARN("wrapper to datetime failed.", K(ret), K(*j_base));
} else if (OB_FAIL(datetime_scale_check(accuracy, val))) {
LOG_WARN("datetime_scale_check failed.", K(ret));
}
}
}
return ret;
}
int JtFuncHelpler::cast_to_otimstamp(ObIJsonBase *j_base,
const ObBasicSessionInfo *session,
common::ObAccuracy &accuracy,
ObObjType dst_type,
ObOTimestampData &out_val)
{
INIT_SUCC(ret);
int64_t val;
oceanbase::common::ObTimeConvertCtx cvrt_ctx(NULL, dst_type == ObTimestampType);
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (is_oracle_mode() && j_base->is_json_number(j_base->json_type())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("can't cast to timestamps", K(ret));
} else {
cvrt_ctx.tz_info_ = session->get_timezone_info();
if (OB_FAIL(common_get_nls_format(session, ObDateTimeType,
true,
cvrt_ctx.oracle_nls_format_))) {
LOG_WARN("common_get_nls_format failed", K(ret));
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(j_base->to_datetime(val, &cvrt_ctx))) {
LOG_WARN("wrapper to datetime failed.", K(ret), K(*j_base));
} else if (dst_type == ObTimestampType) {
out_val.time_us_ = val;
out_val.time_ctx_.tail_nsec_ = 0;
} else {
if (OB_FAIL(ObTimeConverter::odate_to_otimestamp(val, cvrt_ctx.tz_info_, dst_type, out_val))) {
LOG_WARN("fail to timestamp_to_timestamp_tz", K(ret), K(val), K(dst_type));
}
}
if (OB_SUCC(ret)) {
ObScale scale = accuracy.get_scale();
if (OB_UNLIKELY(0 <= scale && scale < MAX_SCALE_FOR_ORACLE_TEMPORAL)) {
ObOTimestampData ot_data = ObTimeConverter::round_otimestamp(scale, out_val);
if (ObTimeConverter::is_valid_otimestamp(ot_data.time_us_,
static_cast<int32_t>(ot_data.time_ctx_.tail_nsec_))) {
out_val = ot_data;
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid otimestamp, set it null ", K(ot_data), K(scale), "orig_date", out_val);
}
}
}
return ret;
}
int JtFuncHelpler::cast_to_date(ObIJsonBase *j_base, int32_t &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_date(val))) {
LOG_WARN("wrapper to date failed.", K(ret), K(*j_base));
ret = OB_OPERATE_OVERFLOW;
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, "DATE", "json_value");
}
return ret;
}
int JtFuncHelpler::cast_to_time(ObIJsonBase *j_base,
common::ObAccuracy &accuracy,
int64_t &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_time(val))) {
LOG_WARN("wrapper to time failed.", K(ret), K(*j_base));
ret = OB_OPERATE_OVERFLOW;
LOG_USER_ERROR(OB_OPERATE_OVERFLOW, "TIME", "json_value");
} else if (OB_FAIL(time_scale_check(accuracy, val))) {
LOG_WARN("time_scale_check failed.", K(ret));
}
return ret;
}
int JtFuncHelpler::cast_to_year(ObIJsonBase *j_base, uint8_t &val)
{
INIT_SUCC(ret);
int64_t int_val;
const uint16 min_year = 1901;
const uint16 max_year = 2155;
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_int(int_val))) {
LOG_WARN("wrapper to year failed.", K(ret), K(*j_base));
} else if (lib::is_oracle_mode()
&& (0 != int_val && (int_val < min_year || int_val > max_year))) {
// different with cast, if 0 < int val < 100, do not add base year
LOG_DEBUG("int out of year range", K(int_val));
ret = OB_DATA_OUT_OF_RANGE;
} else if(OB_FAIL(ObTimeConverter::int_to_year(int_val, val))) {
LOG_WARN("int to year failed.", K(ret), K(int_val));
}
return ret;
}
int JtFuncHelpler::cast_to_float(ObIJsonBase *j_base, ObObjType dst_type, float &val)
{
INIT_SUCC(ret);
double tmp_val;
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_double(tmp_val))) {
LOG_WARN("wrapper to date failed.", K(ret), K(*j_base));
} else {
val = static_cast<float>(tmp_val);
if (lib::is_mysql_mode() && OB_FAIL(real_range_check(dst_type, tmp_val, val))) {
LOG_WARN("real_range_check failed", K(ret), K(tmp_val));
}
}
return ret;
}
int JtFuncHelpler::cast_to_double(ObIJsonBase *j_base, ObObjType dst_type, double &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_double(val))) {
LOG_WARN("wrapper to date failed.", K(ret), K(*j_base));
} else if (ObUDoubleType == dst_type && OB_FAIL(numeric_negative_check(val))) {
LOG_WARN("numeric_negative_check failed", K(ret), K(val));
}
return ret;
}
int JtFuncHelpler::cast_to_number(common::ObIAllocator *allocator,
ObIJsonBase *j_base,
common::ObAccuracy &accuracy,
ObObjType dst_type,
number::ObNumber &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_number(allocator, val))) {
LOG_WARN("fail to cast json as decimal", K(ret));
} else if (ObUNumberType == dst_type && OB_FAIL(numeric_negative_check(val))) {
LOG_WARN("numeric_negative_check failed", K(ret), K(val));
} else if (OB_FAIL(number_range_check(accuracy, allocator, val))) {
LOG_WARN("number_range_check failed", K(ret), K(val));
}
return ret;
}
int JtFuncHelpler::cast_to_bit(ObIJsonBase *j_base, uint64_t &val, common::ObAccuracy &accuracy)
{
INIT_SUCC(ret);
int64_t int_val;
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->to_int(int_val))) {
ret = OB_ERR_INVALID_JSON_VALUE_FOR_CAST;
LOG_WARN("fail get int from json", K(ret));
} else {
val = static_cast<uint64_t>(int_val);
if (OB_FAIL(bit_length_check(accuracy, val))) {
LOG_WARN("fail to check bit range", K(ret));
}
}
return ret;
}
int JtFuncHelpler::bit_length_check(const ObAccuracy &accuracy,
uint64_t &value)
{
int ret = OB_SUCCESS;
int32_t bit_len = 0;
int32_t dst_bit_len = accuracy.get_precision();
if (OB_FAIL(ObJsonBaseUtil::get_bit_len(value, bit_len))) {
LOG_WARN("fail to get_bit_length", K(ret), K(value), K(bit_len));
} else if(OB_UNLIKELY(bit_len <= 0)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("bit length is negative", K(ret), K(value), K(bit_len));
} else {
if (OB_UNLIKELY(bit_len > dst_bit_len)) {
ret = OB_ERR_DATA_TOO_LONG;
LOG_WARN("bit type length is too long", K(ret), K(bit_len),
K(dst_bit_len), K(value));
}
}
return ret;
}
int JtFuncHelpler::cast_to_json(common::ObIAllocator *allocator, ObIJsonBase *j_base, ObString &val)
{
INIT_SUCC(ret);
if (OB_ISNULL(j_base)) {
ret = OB_ERR_NULL_VALUE;
LOG_WARN("json base is null", K(ret));
} else if (OB_FAIL(j_base->get_raw_binary(val, allocator))) {
LOG_WARN("failed to get raw binary", K(ret));
}
return ret;
}
int JtFuncHelpler::cast_to_res(JtScanCtx* ctx, ObIJsonBase* js_val, JtColNode& col_node, bool enable_error = true)
{
INIT_SUCC(ret);
ObJtColInfo& col_info = col_node.get_column_def();
bool is_truncate = static_cast<bool>(col_info.truncate_);
ObExpr* expr = ctx->spec_ptr_->column_exprs_.at(col_info.output_column_idx_);
ObDatum& res = expr->locate_datum_for_write(*ctx->eval_ctx_);
ctx->res_obj_ = &res;
ObObjType dst_type = expr->datum_meta_.type_;
if (OB_FAIL(cast_json_to_res(ctx, js_val, col_node, res, enable_error))) {
LOG_WARN("fail to cast json to res", K(ret));
}
LOG_DEBUG("finish cast_to_res.", K(ret), K(dst_type));
return ret;
}
int JtFuncHelpler::cast_json_to_res(JtScanCtx* ctx, ObIJsonBase* js_val, JtColNode& col_node, ObDatum& res, bool enable_error)
{
INIT_SUCC(ret);
ObJtColInfo& col_info = col_node.get_column_def();
bool is_truncate = static_cast<bool>(col_info.truncate_);
ObExpr* expr = ctx->spec_ptr_->column_exprs_.at(col_info.output_column_idx_);
ObObjType dst_type = expr->datum_meta_.type_;
ObCollationType coll_type = expr->datum_meta_.cs_type_;
ObAccuracy accuracy = col_info.data_type_.get_accuracy();
ObCollationType dst_coll_type = col_info.data_type_.get_collation_type();
ObCollationType in_coll_type = ctx->is_charset_converted_
? CS_TYPE_UTF8MB4_BIN
: ctx->spec_ptr_->value_expr_->datum_meta_.cs_type_;
ObCollationLevel dst_coll_level = col_info.data_type_.get_collation_level();
if (OB_ISNULL(js_val)) {
res.set_null();
} else {
switch (dst_type) {
case ObNullType : {
res.set_null();
break;
}
case ObTinyIntType:
case ObSmallIntType:
case ObMediumIntType:
case ObInt32Type:
case ObIntType: {
int64_t val;
ret = cast_to_int(js_val, dst_type, val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
if (dst_type == ObIntType) {
res.set_int(val);
} else {
res.set_int32(static_cast<int32_t>(val));
}
}
break;
}
case ObUTinyIntType:
case ObUSmallIntType:
case ObUMediumIntType:
case ObUInt32Type:
case ObUInt64Type: {
uint64_t val;
ret = cast_to_uint(js_val, dst_type, val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
if (dst_type == ObUInt64Type) {
res.set_uint(val);
} else {
res.set_uint32(static_cast<uint32_t>(val));
}
}
break;
}
case ObDateTimeType: {
const ObBasicSessionInfo *session = ctx->exec_ctx_->get_my_session();
int64_t val;
ret = cast_to_datetime(&col_node, js_val, &ctx->row_alloc_, session, accuracy, val);
if (ret == OB_ERR_NULL_VALUE) {
res.set_null();
} else if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
res.set_datetime(val);
}
break;
}
case ObTimestampNanoType:
case ObTimestampTZType:
case ObTimestampLTZType:
case ObTimestampType: {
const ObBasicSessionInfo *session = ctx->exec_ctx_->get_my_session();
ObOTimestampData val;
ret = cast_to_otimstamp(js_val, session, accuracy, dst_type, val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
if (dst_type == ObTimestampTZType) {
res.set_otimestamp_tz(val);
} else if (dst_type == ObTimestampType) {
res.set_datetime(val.time_us_);
} else {
res.set_otimestamp_tiny(val);
}
}
break;
}
case ObDateType: {
int32_t val;
ret = cast_to_date(js_val, val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
res.set_date(val);
}
break;
}
case ObTimeType: {
int64_t val;
ret = cast_to_time(js_val, accuracy, val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
res.set_time(val);
}
break;
}
case ObYearType: {
uint8_t val;
ret = cast_to_year(js_val, val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
res.set_year(val);
}
break;
}
case ObNumberFloatType:
case ObFloatType:
case ObUFloatType: {
float out_val;
ret = cast_to_float(js_val, dst_type, out_val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {
res.set_float(out_val);
}
break;
}
case ObDoubleType:
case ObUDoubleType: {
double out_val;
ret = cast_to_double(js_val, dst_type, out_val);
if (OB_FAIL(ret) && enable_error) {
int tmp_ret = set_error_val(ctx, col_node, ret);
if (tmp_ret != OB_SUCCESS) {
LOG_WARN("failed to set error val.", K(tmp_ret));
}
} else {