forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_raw_expr_util.cpp
More file actions
8963 lines (8594 loc) · 373 KB
/
Copy pathob_raw_expr_util.cpp
File metadata and controls
8963 lines (8594 loc) · 373 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) 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_RESV
#include "ob_raw_expr_util.h"
#include "lib/json/ob_json_print_utils.h"
#include "sql/parser/ob_sql_parser.h"
#include "sql/engine/expr/ob_expr_to_type.h"
#include "sql/engine/expr/ob_expr_type_to_str.h"
#include "sql/engine/expr/ob_expr_column_conv.h"
#include "pl/ob_pl_resolver.h"
#include "sql/optimizer/ob_optimizer_util.h"
#include "sql/resolver/dml/ob_select_resolver.h"
#include "sql/resolver/dml/ob_inlist_resolver.h"
#include "lib/enumset/ob_enum_set_meta.h"
#include "src/sql/resolver/dml/ob_inlist_resolver.h"
namespace oceanbase
{
using namespace common;
using namespace share;
using namespace share::schema;
namespace sql
{
#define RESOLVE_ORALCE_IMLICIT_CAST_WARN_OR_ERR(warn_code, err_code) \
if ((session_info->get_sql_mode() & SMO_ERROR_ON_RESOLVE_CAST) > 0) {\
ret = err_code;\
} else {\
LOG_USER_WARN(warn_code);\
}
inline int ObRawExprUtils::resolve_op_expr_add_implicit_cast(ObRawExprFactory &expr_factory,
const ObSQLSessionInfo *session_info,
ObRawExpr *src_expr,
const ObExprResType &dst_type,
ObSysFunRawExpr *&func_expr)
{
int ret = OB_SUCCESS;
if (dst_type.is_varying_len_char_type()) {
ObItemType func_type = T_MAX;
const char* func_name = NULL;
if (OB_ISNULL(src_expr)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid args", K(ret), KP(src_expr));
} else if (dst_type.is_varchar()) {
func_type = T_FUN_SYS_TO_CHAR;
func_name = N_TO_CHAR;
}
if (OB_SUCC(ret)) {
if (OB_FAIL(expr_factory.create_raw_expr(func_type, func_expr))) {
LOG_WARN("create cast expr failed", K(ret));
} else if (OB_FAIL(func_expr->set_param_expr(src_expr))) {
LOG_WARN("add real param expr failed", K(ret));
} else {
func_expr->set_func_name(ObString::make_string(func_name));
if (OB_FAIL(func_expr->formalize(session_info))) {
LOG_WARN("formalize current expr failed", K(ret));
}
LOG_DEBUG("succ to create to char expr", K(dst_type));
}
}
} else {
OZ(ObRawExprUtils::create_cast_expr(expr_factory, src_expr, dst_type, func_expr, session_info,
dst_type.get_cast_mode() == CM_NONE,
dst_type.get_cast_mode()));
CK(OB_NOT_NULL(func_expr));
}
return ret;
}
int ObRawExprUtils::resolve_op_expr_implicit_cast(ObRawExprFactory &expr_factory,
const ObSQLSessionInfo *session_info,
ObItemType op_type,
ObRawExpr* &sub_expr1,
ObRawExpr* &sub_expr2)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(sub_expr1) || OB_ISNULL(sub_expr2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null expr", K(ret), K(sub_expr1), K(sub_expr2));
} else {
ObSysFunRawExpr *new_expr = NULL;
ObSysFunRawExpr *new_expr2 = NULL;
ObObjType r_type1 = ObMaxType;
ObObjType r_type2 = ObMaxType;
ObObjType r_type3 = ObMaxType;
r_type1 = sub_expr1->get_result_type().get_type();
r_type2 = sub_expr2->get_result_type().get_type();
//formalize to get expr's type
if (OB_SUCC(ret) && !ob_is_valid_obj_o_type(r_type1)) {
if (OB_FAIL(sub_expr1->formalize(session_info))) {
LOG_WARN("expr fail to formalize");
} else if (!ob_is_valid_obj_o_type(r_type1 = sub_expr1->get_result_type().get_type())) {
RESOLVE_ORALCE_IMLICIT_CAST_WARN_OR_ERR(OB_OBJ_TYPE_ERROR, OB_OBJ_TYPE_ERROR);
LOG_WARN("invalid oracle type after formalize type1", K(r_type1), K(*sub_expr1));
}
}
if (OB_SUCC(ret) && !ob_is_valid_obj_o_type(r_type2)) {
if (OB_FAIL(sub_expr2->formalize(session_info))) {
LOG_WARN("expr fail to formalize");
} else if (!ob_is_valid_obj_o_type(r_type2 = sub_expr2->get_result_type().get_type())) {
RESOLVE_ORALCE_IMLICIT_CAST_WARN_OR_ERR(OB_OBJ_TYPE_ERROR, OB_OBJ_TYPE_ERROR);
LOG_WARN("invalid oracle type after formalize type1", K(r_type2), K(*sub_expr2));
}
}
if (OB_SUCC(ret)) {
ObObjOType type1 = ob_obj_type_to_oracle_type(r_type1);
ObObjOType type2 = ob_obj_type_to_oracle_type(r_type2);
if (type1 >= ObOMaxType || type2 >= ObOMaxType) {
LOG_WARN("INVALID ORACLE TYPE", K(type1), K(type2), K(*sub_expr1), K(*sub_expr2));
RESOLVE_ORALCE_IMLICIT_CAST_WARN_OR_ERR(OB_OBJ_TYPE_ERROR, OB_ERR_INVALID_TYPE_FOR_OP);
} else if (ObONullType == type1 || ObONullType == type2) {
LOG_DEBUG("No need to cast with null", K(type1), K(type2));
} else {
ImplicitCastDirection dir = ImplicitCastDirection::IC_NOT_SUPPORT;
ObObjType middle_type = ObMaxType;
ObObjTypeClass tc1 = OBJ_O_TYPE_TO_CLASS[type1];
ObObjTypeClass tc2 = OBJ_O_TYPE_TO_CLASS[type2];
bool is_arith_op = false;
switch (op_type) {
case T_OP_CNN: {
// the accuracy deduced in this function is conflict with ObExprConcat::calc_result_typeN,
// so postpone deduce type of T_OP_CNN to ObExprConcat.
dir = ImplicitCastDirection::IC_NO_CAST;
break;
}
case T_OP_LIKE: {
/* for non-string data type: select c1||c2 from tab; => cast(c1 as varchar)||cast(c2 as varchar) */
bool cast_left = true;
bool cast_right = true;
if (cast_left && cast_right) {
dir = ImplicitCastDirection::IC_TO_MIDDLE_TYPE;
middle_type = ObVarcharType;
} else if (cast_left) {
dir = ImplicitCastDirection::IC_A_TO_B;
r_type2 = ObVarcharType;
} else if (cast_right) {
dir = ImplicitCastDirection::IC_B_TO_A;
r_type1 = ObVarcharType;
} else {
dir = ImplicitCastDirection::IC_NO_CAST;
}
break;
}
case T_OP_ADD: {
is_arith_op = true;
if (ob_is_oracle_datetime_tc(r_type1)) {
if (ob_is_numeric_type(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
} else if (ob_is_string_tc(r_type2)) {
r_type3 = ObNumberType;
dir = ImplicitCastDirection::IC_B_TO_C;
}
} else if (ob_is_oracle_datetime_tc(r_type2)) {
if (ob_is_numeric_type(r_type1)) {
dir = ImplicitCastDirection::IC_NO_CAST;
} else if (ob_is_string_tc(r_type1)) {
r_type3 = ObNumberType;
dir = ImplicitCastDirection::IC_A_TO_C;
}
}
if (ob_is_raw_tc(r_type1)) {
if (ob_is_string_tc(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
} else if (ob_is_raw_tc(r_type2)) {
if (ob_is_string_tc(r_type1)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
}
if (ImplicitCastDirection::IC_NOT_SUPPORT != dir) {
break;
}
}
case T_OP_MINUS: {
is_arith_op = true;
if (ob_is_oracle_datetime_tc(r_type1)) {
if (ob_is_numeric_type(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
} else if (ob_is_string_tc(r_type2)) {
r_type3 = ObNumberType;
dir = ImplicitCastDirection::IC_B_TO_C;
} else if (ob_is_oracle_datetime_tc(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
} else if (ob_is_oracle_datetime_tc(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
if (ob_is_raw_tc(r_type1)) {
if (ob_is_string_tc(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
} else if (ob_is_raw_tc(r_type2)) {
if (ob_is_string_tc(r_type1)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
}
if (ImplicitCastDirection::IC_NOT_SUPPORT != dir) {
break;
}
}
case T_OP_MUL:
case T_OP_DIV:
case T_OP_MOD: {
is_arith_op = true;
if (ob_is_raw_tc(r_type1)) {
if (ob_is_string_tc(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
} else if (ob_is_raw_tc(r_type2)) {
if (ob_is_string_tc(r_type1)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
}
if (ImplicitCastDirection::IC_NOT_SUPPORT != dir) {
break;
}
}
//todo timestamp with tz/ltz...
default:
if (OB_UNLIKELY(ObLongTextType == r_type1 || ObLongTextType == r_type2)) {
/* lob type return warning */
} else {
middle_type = ObNumberType;
dir = OB_OBJ_IMPLICIT_CAST_DIRECTION_FOR_ORACLE[type1][type2];
}
break;
}
// Calculation with interval, do not make any conversion
if (ob_is_interval_tc(r_type1) || ob_is_interval_tc(r_type2)) {
dir = ImplicitCastDirection::IC_NO_CAST;
}
LOG_DEBUG("Molly ORACLE IMPLICIT CAST DIR",
K(dir), K(type1), K(type2), K(r_type3), K(*sub_expr1), K(*sub_expr2), K(op_type));
ObExprResType dest_type;
switch (dir) {
case ImplicitCastDirection::IC_NO_CAST: {
//int is number(38) in oracle, when number div number, the result can be decimal.
//So we add cast int as number b4 do div
if (tc1 == ObIntTC && tc2 == ObIntTC && op_type == T_OP_DIV) {
ObAccuracy acc = ObAccuracy::DDL_DEFAULT_ACCURACY2[1][ObNumberType];
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
dest_type.set_collation_level(CS_LEVEL_NUMERIC);
dest_type.set_type(ObNumberType);
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr1,
dest_type,
new_expr))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr2,
dest_type,
new_expr2))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr) || OB_ISNULL(new_expr2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else if (OB_FAIL(new_expr2->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr1 = new_expr;
sub_expr2 = new_expr2;
}
}
break;
}
case ImplicitCastDirection::IC_A_TO_B: {
ObAccuracy acc;
ObObjType dst_type = r_type2;
if (is_arith_op && ob_is_string_tc(r_type1) && ObDecimalIntType == r_type2) {
dst_type = ObNumberType;
}
// if col cmps decint_const, do not cast col to ObDecimalIntType, use ObNumber instead
// i.e. cast(col as number) = cast(decint_const as number)
if (dst_type == ObDecimalIntType && sub_expr2->is_const_expr() && !sub_expr1->is_const_expr()) {
dst_type = ObNumberType;
}
ObCastMode cast_mode = CM_NONE;
if (ObDecimalIntType == dst_type) {
// If it is decimal int type, accuracy and decimal int consistency is sufficient
acc = sub_expr2->get_result_type().get_accuracy();
if (!ob_is_decimal_int(r_type1) && IS_STRICT_OP(op_type)) {
cast_mode |= ObRelationalExprOperator::get_const_cast_mode(op_type, false);
}
} else {
acc = (ObNumberType == dst_type ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][dst_type] :
ObAccuracy::MAX_ACCURACY2[1][dst_type]);
}
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
dest_type.set_type(dst_type);
dest_type.add_decimal_int_cast_mode(cast_mode);
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr1,
dest_type,
new_expr))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
if (sub_expr1->is_const_expr() && !sub_expr2->is_const_expr()
&& ObDecimalIntType == dst_type) {
ObCastMode extra_cm = ObRelationalExprOperator::get_const_cast_mode(op_type, false);
new_expr->set_cast_mode(new_expr->get_cast_mode() | extra_cm);
}
sub_expr1 = new_expr;
}
break;
}
case ImplicitCastDirection::IC_B_TO_A: {
ObAccuracy acc;
ObCastMode cast_mode = CM_NONE;
ObObjType dst_type = r_type1;
if (is_arith_op && ob_is_string_tc(r_type2) && ObDecimalIntType == r_type1) {
dst_type = ObNumberType;
}
// if col cmps decint_const, do not cast col to ObDecimalIntType, use ObNumber instead
// i.e. cast(col as number) = cast(decint_const as number)
if (dst_type == ObDecimalIntType && sub_expr1->is_const_expr() && !sub_expr2->is_const_expr()) {
dst_type = ObNumberType;
}
if (ObDecimalIntType == dst_type) {
// If it is decimal int type, accuracy and decimal int consistency is sufficient
acc = sub_expr1->get_result_type().get_accuracy();
if (!ob_is_decimal_int(r_type2) && IS_STRICT_OP(op_type)) {
cast_mode |= ObRelationalExprOperator::get_const_cast_mode(op_type, true);
}
} else {
acc = (ObNumberType == dst_type ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][dst_type] :
ObAccuracy::MAX_ACCURACY2[1][dst_type]);
}
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
//dest_type.set_collation_level(CS_LEVEL_NUMERIC);
dest_type.set_type(dst_type);
dest_type.add_decimal_int_cast_mode(cast_mode);
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr2,
dest_type,
new_expr))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else {
if (sub_expr2->is_const_expr() && !sub_expr1->is_const_expr() && ObDecimalIntType == dst_type) {
ObCastMode extra_cm = ObRelationalExprOperator::get_const_cast_mode(op_type, true);
new_expr->set_cast_mode(new_expr->get_cast_mode() | extra_cm);
}
sub_expr2 = new_expr;
}
break;
}
case ImplicitCastDirection::IC_A_TO_C: {
ObAccuracy acc = (ObNumberType == r_type3 ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][r_type3] : ObAccuracy::MAX_ACCURACY2[1][r_type3]);
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
dest_type.set_type(r_type3);
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr1,
dest_type,
new_expr))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr1 = new_expr;
}
break;
}
case ImplicitCastDirection::IC_B_TO_C: {
ObAccuracy acc = (ObNumberType == r_type3 ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][r_type3] : ObAccuracy::MAX_ACCURACY2[1][r_type3]);
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
dest_type.set_type(r_type3);
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr2,
dest_type,
new_expr))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr2 = new_expr;
}
break;
}
case ImplicitCastDirection::IC_TO_MIDDLE_TYPE: {
ObAccuracy acc = (ObNumberType == middle_type ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][middle_type] : ObAccuracy::MAX_ACCURACY2[1][middle_type]);
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
dest_type.set_collation_level(CS_LEVEL_NUMERIC);
dest_type.set_type(middle_type);
ObObjTypeClass middle_tc = OBJ_O_TYPE_TO_CLASS[middle_type];
//optimization: only need to cast string type to number type
if (middle_type == ObNumberType) {
if (ObStringTC == tc1 && (ObIntTC == tc2 || ObFloatTC == tc2 ||
ObDoubleTC == tc2 || ObNumberTC == tc2)) {
if (OB_FAIL(ObRawExprUtils::create_cast_expr(expr_factory,
sub_expr1,
dest_type,
new_expr,
session_info))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr1 = new_expr;
}
} else if (ObStringTC == tc2 && (ObIntTC == tc1 || ObFloatTC == tc1 ||
ObDoubleTC == tc1 || ObNumberTC == tc1)) {
if (OB_FAIL(ObRawExprUtils::create_cast_expr(expr_factory,
sub_expr2,
dest_type,
new_expr2,
session_info))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr2->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr2 = new_expr2;
}
} else {
LOG_WARN("create cast expr for implicit failed unexpected TO_MIDDLE_TYPE", K(tc1), K(tc2));
}
} else {
if (middle_tc != tc1) {
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr1,
dest_type,
new_expr))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr1 = new_expr;
}
}
if (middle_tc != tc2) {
if (OB_FAIL(resolve_op_expr_add_implicit_cast(expr_factory,
session_info,
sub_expr2,
dest_type,
new_expr2))) {
LOG_WARN("create cast expr for implicit failed", K(ret));
} else if (OB_ISNULL(new_expr2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(new_expr2->add_flag(IS_INNER_ADDED_EXPR))) {
LOG_WARN("failed to add flag", K(ret));
} else {
sub_expr2 = new_expr2;
}
}
}
break;
}
case ImplicitCastDirection::IC_NOT_SUPPORT:
{
//ObString type1_str = ObString(ob_obj_type_str(r_type1));
//ObString type2_str = ObString(ob_obj_type_str(r_type2));
//return warning or error when can't do implicite datatype convert
bool is_error = (session_info != NULL && ((session_info->get_sql_mode() & SMO_ERROR_ON_RESOLVE_CAST) > 0))
|| (sub_expr1->get_result_type().is_blob()
|| sub_expr2->get_result_type().is_blob());
if (is_error) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_OP, ob_obj_type_str(r_type1), ob_obj_type_str(r_type2));
} else {
if (sub_expr1->is_called_in_sql() && sub_expr2->is_called_in_sql()) {
LOG_USER_WARN(OB_ERR_INVALID_TYPE_FOR_OP, ob_obj_type_str(r_type1), ob_obj_type_str(r_type2));
}
}
LOG_WARN("expr get oracle implicit cast direction failed", K(is_error));
break;
}
}
}
}
}
return ret;
}
int ObRawExprUtils::resolve_op_expr_for_oracle_implicit_cast(ObRawExprFactory &expr_factory,
const ObSQLSessionInfo *session_info,
ObOpRawExpr* &b_expr)
{
int ret = OB_SUCCESS;
ObRawExpr *sub_expr1 = NULL;
ObRawExpr *sub_expr2 = NULL;
sub_expr1 = b_expr->get_param_expr(0);
sub_expr2 = b_expr->get_param_expr(1);
if (OB_ISNULL(sub_expr1) || OB_ISNULL(sub_expr2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null expr", K(ret), K(sub_expr1), K(sub_expr2));
} else if (T_OP_ROW == sub_expr1->get_expr_type() ||
T_OP_ROW == sub_expr2->get_expr_type()) {
// Left (right) child node of type T_OP_ROW ObOpRawExpr will not be explicitly cast
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get T_OP_ROW expr", K(ret), K(*sub_expr1), K(*sub_expr2));
} else {
if (!sub_expr1->is_query_ref_expr() && sub_expr2->is_query_ref_expr()) {
// t1.c1 =any (select c1 from t2)
// Follow the normal process, the return type of query_ref_expr is fixed as bigint, should compare with the type of the return column
ObQueryRefRawExpr *query_ref_expr = static_cast<ObQueryRefRawExpr *>(sub_expr2);
ObSelectStmt *query_stmt = query_ref_expr->get_ref_stmt();
ObRawExpr *select_expr = NULL;
if (OB_ISNULL(query_stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(query_stmt));
} else if (OB_UNLIKELY(1 != query_stmt->get_select_item_size())) {
ret = OB_ERR_INVALID_COLUMN_NUM;
LOG_USER_ERROR(OB_ERR_INVALID_COLUMN_NUM, 1L);
LOG_WARN("query_ref_expr should have 1 output column", K(query_ref_expr->get_output_column()));
} else if (OB_ISNULL(select_expr = query_stmt->get_select_item(0).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(select_expr));
} else if (OB_FAIL(resolve_op_expr_implicit_cast(expr_factory,
session_info,
b_expr->get_expr_type(),
sub_expr1,
select_expr))) {
LOG_WARN("failed to resolve_op_expr_implicit_cast", K(ret));
} else if (OB_ISNULL(select_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(b_expr->replace_param_expr(0, sub_expr1))) {
LOG_WARN("failed to replace_param_expr", K(ret));
} else {
query_stmt->get_select_item(0).expr_ = select_expr;
ObIArray<ObRawExprResType> &column_types = query_ref_expr->get_column_types();
column_types.reset();
if (OB_FAIL(column_types.push_back(select_expr->get_result_type()))) {
LOG_WARN("add column type failed", K(ret));
}
}
} else if (sub_expr1->is_query_ref_expr() && sub_expr2->is_query_ref_expr()) {
// (select c1 from t2 where t2.c1=1) =any (select c1 from t2)
ObQueryRefRawExpr *query_ref_expr1 = static_cast<ObQueryRefRawExpr *>(sub_expr1);
ObQueryRefRawExpr *query_ref_expr2 = static_cast<ObQueryRefRawExpr *>(sub_expr2);
ObSelectStmt *query_stmt1 = query_ref_expr1->get_ref_stmt();
ObSelectStmt *query_stmt2 = query_ref_expr2->get_ref_stmt();
ObRawExpr *select_expr1 = NULL;
ObRawExpr *select_expr2 = NULL;
if (OB_ISNULL(query_stmt1) || OB_ISNULL(query_stmt2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(query_stmt1), K(query_stmt2));
} else if (query_stmt1->get_select_item_size() != query_stmt2->get_select_item_size()) {
ret = OB_ERR_INVALID_COLUMN_NUM;
LOG_WARN("query expr should same output column", K(query_ref_expr1->get_output_column()),
K(query_ref_expr2->get_output_column()));
} else {
ObIArray<ObRawExprResType> &column_types1 = query_ref_expr1->get_column_types();
column_types1.reset();
ObIArray<ObRawExprResType> &column_types2 = query_ref_expr2->get_column_types();
column_types2.reset();
for (int64_t i = 0; OB_SUCC(ret) && i < query_stmt1->get_select_item_size(); ++i) {
if (OB_ISNULL(select_expr1 = query_stmt1->get_select_item(i).expr_) ||
OB_ISNULL(select_expr2 = query_stmt2->get_select_item(i).expr_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(ret), K(select_expr1), K(select_expr2));
} else if (OB_FAIL(resolve_op_expr_implicit_cast(expr_factory,
session_info,
b_expr->get_expr_type(),
select_expr1,
select_expr2))) {
LOG_WARN("failed to resolve_op_expr_implicit_cast", K(ret));
} else if (OB_ISNULL(select_expr1) || OB_ISNULL(select_expr2)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect null expr", K(ret));
} else if (OB_FAIL(column_types1.push_back(select_expr1->get_result_type()))) {
LOG_WARN("add column type failed", K(ret));
} else if (OB_FAIL(column_types2.push_back(select_expr2->get_result_type()))) {
LOG_WARN("add column type failed", K(ret));
} else {
query_stmt1->get_select_item(i).expr_ = select_expr1;
query_stmt2->get_select_item(i).expr_ = select_expr2;
}
}
}
} else {
if (OB_FAIL(resolve_op_expr_implicit_cast(expr_factory,
session_info,
b_expr->get_expr_type(),
sub_expr1,
sub_expr2))) {
LOG_WARN("failed to resolve_op_expr_implicit_cast", K(ret));
} else if (OB_FAIL(b_expr->replace_param_expr(0, sub_expr1))) {
LOG_WARN("failed to replace_param_expr", K(ret));
} else if (OB_FAIL(b_expr->replace_param_expr(1, sub_expr2))) {
LOG_WARN("failed to replace_param_expr", K(ret));
}
}
}
return ret;
}
int ObRawExprUtils::resolve_op_exprs_for_oracle_implicit_cast(ObRawExprFactory &expr_factory,
const ObSQLSessionInfo *session_info,
ObIArray<ObOpRawExpr*> &op_exprs)
{
int ret = OB_SUCCESS;
if (session_info == NULL){
LOG_WARN("can't get compatibility mode from session_info", K(session_info));
} else {
ObOpRawExpr *b_expr = NULL;
for (int64_t i = 0; OB_SUCC(ret) && i < op_exprs.count(); i++) {
b_expr = op_exprs.at(i);
if (OB_ISNULL(b_expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get null expr", K(ret));
} else if (b_expr->get_param_count() != 2) {
//TODO Molly case when
LOG_WARN("IMPLICIT UNEXPECTED BEXPR", K(*b_expr));
} else if (OB_FAIL(resolve_op_expr_for_oracle_implicit_cast(expr_factory, session_info,
b_expr))){
LOG_WARN("IMPLICIT UNEXPECTED BEXPR", K(*b_expr));
}
}
}
return ret;
}
int ObRawExprUtils::resolve_udf_common_info(const ObString &db_name,
const ObString &package_name,
int64_t udf_id,
int64_t package_id,
const ObIArray<int64_t> &subprogram_path,
int64_t udf_schema_version,
int64_t pkg_schema_version,
bool is_deterministic,
bool is_parallel_enable,
bool is_pkg_body_udf,
bool is_pl_agg,
int64_t type_id,
ObUDFInfo &udf_info)
{
int ret = OB_SUCCESS;
ObUDFRawExpr *udf_raw_expr = udf_info.ref_expr_;
CK (OB_NOT_NULL(udf_raw_expr));
OZ (udf_raw_expr->set_database_name(db_name));
OX (udf_raw_expr->set_package_name(package_name));
OZ (udf_raw_expr->set_subprogram_path(subprogram_path));
OX (udf_raw_expr->set_udf_id(udf_id));
OX (udf_raw_expr->set_pkg_id(package_id));
OX (udf_raw_expr->set_udf_deterministic(is_deterministic));
OX (udf_raw_expr->set_parallel_enable(is_parallel_enable));
OX (udf_raw_expr->set_udf_schema_version(udf_schema_version));
OX (udf_raw_expr->set_pkg_schema_version(pkg_schema_version));
OX (udf_raw_expr->set_pkg_body_udf(is_pkg_body_udf));
OX (udf_raw_expr->set_type_id(type_id));
OX (udf_raw_expr->set_is_aggregate_udf(is_pl_agg));
return ret;
}
int ObRawExprUtils::resolve_udf_param_types(const ObIRoutineInfo* func_info,
share::schema::ObSchemaGetterGuard &schema_guard,
sql::ObSQLSessionInfo &session_info,
common::ObIAllocator &allocator,
common::ObMySQLProxy &sql_proxy,
ObUDFInfo &udf_info,
pl::ObPLEnumSetCtx &enum_set_ctx)
{
int ret = OB_SUCCESS;
#define SET_RES_TYPE_BY_PL_TYPE(res_type, pl_type) \
if (OB_SUCC(ret)) { \
ObObjMeta meta; \
if (pl_type.is_obj_type()) { \
meta = pl_type.get_data_type()->get_meta_type(); \
meta.set_scale(meta.is_bit() ? \
pl_type.get_data_type()->get_accuracy().get_precision() : \
pl_type.get_data_type()->get_accuracy().get_scale()); \
res_type.set_meta(meta); \
res_type.set_accuracy(pl_type.get_data_type()->get_accuracy()); \
} else { \
meta.set_ext(); \
res_type.set_meta(meta); \
res_type.set_extend_type(pl_type.get_type());\
res_type.set_udt_id(pl_type.get_user_type_id()); \
} \
}
ObUDFRawExpr *udf_raw_expr = udf_info.ref_expr_;
CK (OB_NOT_NULL(udf_raw_expr));
ObRawExprResType result_type;
ObSEArray<ObRawExprResType, 5> params_type;
const ObIArray<ObString> *extended_type_info = NULL;
// Step1: Process Routine return value
const ObIRoutineParam *ret_param = func_info->get_ret_info();
pl::ObPLDataType ret_pl_type;
if (OB_SUCC(ret) && OB_NOT_NULL(ret_param)) {
if (ret_param->is_schema_routine_param()) {
const ObRoutineParam *iparam = static_cast<const ObRoutineParam*>(ret_param);
CK (OB_NOT_NULL(iparam));
OX (ret_pl_type.set_enum_set_ctx(&enum_set_ctx));
OZ (pl::ObPLDataType::transform_from_iparam(iparam,
schema_guard,
session_info,
allocator,
sql_proxy,
ret_pl_type,
NULL));
} else {
OX (ret_pl_type = ret_param->get_pl_data_type());
}
if (ret_pl_type.is_ref_cursor_type() || ret_pl_type.is_sys_refcursor_type()) {
OX (udf_raw_expr->set_is_return_sys_cursor(true));
}
SET_RES_TYPE_BY_PL_TYPE(result_type, ret_pl_type);
OX (udf_raw_expr->set_pls_type(ret_pl_type.get_pl_integer_type()));
if (OB_FAIL(ret)) {
} else if (!ret_pl_type.is_obj_type()) {
OX (result_type.set_udt_id(ret_pl_type.get_user_type_id()));
} else if (result_type.is_enum_or_set()) {
const ObRoutineParam* r_param = static_cast<const ObRoutineParam*>(ret_param);
uint16_t subschema_id = 0;
CK (OB_NOT_NULL(r_param));
OX (extended_type_info = &(r_param->get_extended_type_info()));
OZ (ObRawExprUtils::get_subschema_id(result_type, *extended_type_info, session_info, subschema_id));
OX (result_type.set_subschema_id(subschema_id));
OX (result_type.mark_sql_enum_set_with_subschema());
}
}
// Step2: process input parameters
for (int64_t i = 0; OB_SUCC(ret) && i < func_info->get_param_count(); ++i) {
ObIRoutineParam *iparam = NULL;
pl::ObPLDataType param_pl_type;
ObRawExprResType param_type;
OZ (func_info->get_routine_param(i, iparam));
CK (OB_NOT_NULL(iparam));
if (OB_FAIL(ret)) {
} else if (iparam->is_schema_routine_param()) {
const ObRoutineParam *rparam = static_cast<const ObRoutineParam*>(iparam);
CK (OB_NOT_NULL(rparam));
OX (param_pl_type.set_enum_set_ctx(&enum_set_ctx));
OZ (pl::ObPLDataType::transform_from_iparam(rparam,
schema_guard,
session_info,
allocator,
sql_proxy,
param_pl_type,
NULL));
} else {
OX (param_pl_type = iparam->get_pl_data_type());
}
SET_RES_TYPE_BY_PL_TYPE(param_type, param_pl_type);
OZ (params_type.push_back(param_type));
}
// Step3: Add the parameter return value type to rawexpr
OX (udf_raw_expr->set_result_type(result_type));
OZ (udf_raw_expr->set_params_type(params_type));
#undef SET_RES_TYPE_BY_PL_TYPE
return ret;
}
int ObRawExprUtils::resolve_udf_param_exprs(const ObIRoutineInfo* func_info,
pl::ObPLBlockNS &secondary_namespace_,
ObSchemaChecker &schema_checker,
sql::ObSQLSessionInfo &session_info,
ObIAllocator &allocator,
bool is_prepare_protocol,
sql::ObRawExprFactory &expr_factory,
common::ObMySQLProxy &sql_proxy,
ExternalParams *extern_param_info,
ObUDFInfo &udf_info,
pl::ObPLEnumSetCtx &enum_set_ctx)
{
int ret = OB_SUCCESS;
ObResolverParams params;
params.secondary_namespace_ = &(secondary_namespace_);
params.schema_checker_ = &(schema_checker);
params.session_info_ = &(session_info);
params.allocator_ = &(allocator);
params.is_prepare_protocol_ = is_prepare_protocol;
params.expr_factory_ = &(expr_factory);
params.sql_proxy_ = &(sql_proxy);
if (OB_NOT_NULL(extern_param_info)) {
params.external_param_info_.assign(*extern_param_info);
}
if (OB_FAIL(resolve_udf_param_exprs(params, func_info, udf_info, enum_set_ctx))) {
SQL_LOG(WARN, "failed to exec resovle udf exprs", K(ret), K(udf_info));
}
return ret;
}
/*!
* Parse the parameter list of UDF (mainly handling parameters with default values, as well as parameters specified by name):
* Parameters specified by name must be at the end of the parameter list
* E.g.: func(1, 2, x=>3, y=>4); valid
* func(1, x=>3, 2); invalid (cannot determine the position of 2)
* When reaching this function, parameters specified by name in the parameter list have already been added to the udf's raw expr
* Therefore, this function mainly handles parameters specified by name
* If there are still missing parameters after processing all parameters, it attempts to use default values; if no default values are available, it reports an error
*/
int ObRawExprUtils::resolve_udf_param_exprs(ObResolverParams ¶ms,
const ObIRoutineInfo *func_info,
ObUDFInfo &udf_info,
pl::ObPLEnumSetCtx &enum_set_ctx)
{
int ret = OB_SUCCESS;
ObArray<ObRawExpr*> param_exprs;
ObArray<ObString> param_names;
ObUDFRawExpr *udf_raw_expr = udf_info.ref_expr_;
// Specify parameters by name and uniformly record them in param_names_ and param_exprs, so they must be equal here
if (udf_info.param_names_.count() != udf_info.param_exprs_.count()) {
ret = OB_ERR_UNEXPECTED;
SQL_LOG(WARN, "names array not equal to exprs array count",
K(ret), K(udf_info.param_names_.count()), K(udf_info.param_exprs_.count()));
} else if ((udf_info.udf_param_num_ + udf_info.param_names_.count()) > func_info->get_param_count()) {
ret = OB_ERR_SP_WRONG_ARG_NUM;
LOG_USER_ERROR(OB_ERR_SP_WRONG_ARG_NUM, "FUNCTION", udf_info.udf_name_.ptr(),
static_cast<uint32_t>(func_info->get_param_count()),
static_cast<uint32_t>(udf_info.udf_param_num_ + udf_info.param_names_.count()));
SQL_LOG(WARN, "params count mismatch",
K(ret), K(udf_info.udf_name_), K(func_info->get_param_count()), K(udf_info));
} else if (OB_FAIL(udf_raw_expr->extend_param_exprs(func_info->get_param_count()))) {
LOG_WARN("failed to extend param exprs", K(ret));
} else {
// process the remaining parameters, default values or parameters specified by name
// Step 1: First initialize an empty parameter list
int64_t count = func_info->get_param_count() - udf_info.udf_param_num_;
for (int64_t i = 0; OB_SUCC(ret) && i < udf_info.udf_param_num_; ++i) {
ObString empty;
OZ (udf_raw_expr->add_param_name(empty));
}
for (int64_t i = 0; OB_SUCC(ret) && i < count; ++i) {
if (OB_FAIL(param_exprs.push_back(NULL))) {
SQL_LOG(WARN, "failed to push back", K(ret), K(i), K(udf_info));
} else if (OB_FAIL(param_names.push_back(ObString()))) {
SQL_LOG(WARN, "failed to push back", K(ret), K(i), K(udf_info));
}
}
// Step 2: Add the parameter specified by name to the parameter list
for (int64_t i = 0; OB_SUCC(ret) && i < udf_info.param_names_.count(); ++i) {
const ObString &name = udf_info.param_names_.at(i);
int64_t position = -1;
if (OB_FAIL(func_info->find_param_by_name(name, position))) {
SQL_LOG(WARN, "failed to find param by name", K(ret));
} else if (position < udf_info.udf_param_num_) {
ret = OB_ERR_SP_DUP_VAR;
SQL_LOG(WARN, "parameter dup", K(ret), K(name), K(position), K(i), K(udf_info));
} else {
// Note: The number of parameters not specified by name should be subtracted when adding to the parameter list
param_exprs.at(position - udf_info.udf_param_num_) = udf_info.param_exprs_.at(i);
param_names.at(position - udf_info.udf_param_num_) = name;
}
}
// Step 3: Handle missing parameters
for (int64_t i = 0; OB_SUCC(ret) && i < param_exprs.count(); ++i) {
if (OB_ISNULL(param_exprs.at(i))) {
const ParseNode *default_node = NULL;
ObRawExpr *default_expr = NULL;
ObConstRawExpr *const_default_expr = NULL;
ObString default_val;
ObIRoutineParam *routine_param = NULL;
if (OB_FAIL(func_info->get_routine_param(i + udf_info.udf_param_num_, routine_param))) {
SQL_LOG(WARN, "failed to get routine param", K(ret), K(i), K(udf_info));
} else if (FALSE_IT(default_val = routine_param->get_default_value())) {
} else if (OB_FAIL(ObSQLUtils::convert_sql_text_from_schema_for_resolve(
*(params.allocator_), params.session_info_->get_dtc_params(), default_val))) {
LOG_WARN("fail to get default value", K(ret));
} else if (OB_UNLIKELY(default_val.empty())) {
ret = OB_ERR_SP_WRONG_ARG_NUM;
LOG_USER_ERROR(OB_ERR_SP_WRONG_ARG_NUM, "FUNCTION", udf_info.udf_name_.ptr(),
static_cast<uint32_t>(func_info->get_param_count()),
static_cast<uint32_t>(udf_info.udf_param_num_ + udf_info.param_names_.count()));
SQL_LOG(WARN, "param count mismatch", K(ret), K(i), K(default_val));
} else if (OB_FAIL(ObRawExprUtils::parse_default_expr_from_str(
default_val, params.session_info_->get_charsets4parser(),
*(params.allocator_), default_node))) {
SQL_LOG(WARN, "failed to parse expr node from str", K(ret), K(i), K(default_val), K(udf_info));
} else if (OB_ISNULL(default_node)
|| OB_ISNULL(params.allocator_)
|| OB_ISNULL(params.expr_factory_)) {
ret = OB_ERR_UNEXPECTED;
SQL_LOG(WARN, "parameter is null",
K(ret), K(i), K(default_val), K(udf_info), K(default_node),
K(params.allocator_), K(params.expr_factory_), K(params.secondary_namespace_));
} else if (OB_FAIL(ObRawExprUtils::build_const_int_expr(
*(params.expr_factory_), ObNullType, 0, const_default_expr))) {
SQL_LOG(WARN, "failed build const int expr for default expr", K(ret), K(i));
} else {
ObObjMeta null_meta;
null_meta.set_null();
OX (default_expr = const_default_expr);
CK (OB_NOT_NULL(default_expr));
OX (const_default_expr->set_meta_type(null_meta));
OX (const_default_expr->set_expr_obj_meta(null_meta));
OX (param_exprs.at(i) = default_expr);
// rewrite param type, for do not cast default value
CK(udf_raw_expr->get_param_count() < udf_raw_expr->get_params_type().count());
OX (udf_raw_expr->get_params_type().at(
udf_raw_expr->get_param_count()).set_meta(null_meta));
}
}
OZ (udf_raw_expr->add_param_expr(param_exprs.at(i)));
OZ (udf_raw_expr->add_param_name(param_names.at(i)));
}
OV ((udf_info.udf_param_num_ + param_exprs.count()) == udf_raw_expr->get_param_count(),
OB_ERR_UNEXPECTED, K(udf_info.udf_param_num_), K(param_exprs.count()), K(udf_raw_expr->get_param_count()));
}
if (OB_SUCC(ret)
&& (func_info->get_param_count() != udf_info.udf_param_num_ + param_exprs.count())) {
ret = OB_ERR_SP_WRONG_ARG_NUM;
LOG_USER_ERROR(OB_ERR_SP_WRONG_ARG_NUM, "FUNCTION", udf_info.udf_name_.ptr(),
static_cast<uint32_t>(func_info->get_param_count()),
static_cast<uint32_t>(udf_info.udf_param_num_ + udf_info.param_names_.count()));
SQL_LOG(WARN, "params count mismatch",
K(ret), K(udf_info.udf_name_),
K(func_info->get_param_count()), K(udf_info));
}
// Step 4: Process function's OUT parameters
for (int64_t i = 0; OB_SUCC(ret) && i < func_info->get_param_count(); ++i) {
ObIRoutineParam* iparam = NULL;
pl::ObPLRoutineParamMode mode = pl::ObPLRoutineParamMode::PL_PARAM_INVALID;
OZ (func_info->get_routine_param(i, iparam));
CK (OB_NOT_NULL(iparam));
OX (mode = static_cast<pl::ObPLRoutineParamMode>(iparam->get_mode()));
if (OB_SUCC(ret) && lib::is_mysql_mode()) {
bool need_wrap = false;
OZ (ObRawExprUtils::need_wrap_to_string(udf_raw_expr->get_param_expr(i)->get_result_type(),
iparam->get_pl_data_type().get_obj_type(),
true,
need_wrap,
false));
if (OB_SUCC(ret) && need_wrap) {
ObSysFunRawExpr *out_expr = NULL;
OZ (ObRawExprUtils::create_type_to_str_expr(*(params.expr_factory_),
udf_raw_expr->get_param_expr(i),
out_expr,
params.session_info_,
true));
CK (OB_NOT_NULL(out_expr));
OZ (udf_raw_expr->replace_param_expr(i, out_expr));
}
}
if (OB_SUCC(ret)) {
if (pl::ObPLRoutineParamMode::PL_PARAM_OUT == mode
|| pl::ObPLRoutineParamMode::PL_PARAM_INOUT == mode) {
ObRawExpr* iexpr = udf_raw_expr->get_param_expr(i);
CK (OB_NOT_NULL(iexpr));
OZ (iexpr->formalize(params.session_info_));
if (OB_SUCC(ret)) { // udf output parameter, change param type to output type.
const ObRawExprResType &result_type = iexpr->get_result_type();
if (OB_SUCC(ret) && result_type.is_valid() && !result_type.is_null()) {
CK (udf_raw_expr->get_params_type().count() > i);
OX (udf_raw_expr->get_params_type().at(i) = result_type);