forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_raw_expr_util.cpp
More file actions
8851 lines (8489 loc) · 368 KB
/
Copy pathob_raw_expr_util.cpp
File metadata and controls
8851 lines (8489 loc) · 368 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_RESV
#include "sql/resolver/expr/ob_raw_expr_util.h"
#include "share/ob_define.h"
#include "share/ob_errno.h"
#include "lib/allocator/ob_malloc.h"
#include "lib/allocator/page_arena.h"
#include "lib/string/ob_string.h"
#include "lib/string/ob_sql_string.h"
#include "lib/json/ob_json.h"
#include "lib/json/ob_json_print_utils.h"
#include "sql/resolver/dml/ob_select_stmt.h"
#include "sql/resolver/expr/ob_raw_expr.h"
#include "sql/resolver/expr/ob_raw_expr_resolver_impl.h"
#include "sql/resolver/ob_resolver_utils.h"
#include "sql/parser/ob_parser_utils.h"
#include "sql/parser/ob_parser.h"
#include "sql/parser/ob_sql_parser.h"
#include "sql/session/ob_sql_session_info.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 "sql/engine/expr/ob_datum_cast.h"
#include "sql/engine/expr/ob_expr_cast.h"
#include "common/ob_smart_call.h"
#include "pl/ob_pl_resolver.h"
#include "sql/optimizer/ob_optimizer_util.h"
#include "sql/resolver/dml/ob_dml_resolver.h"
#include "sql/resolver/dml/ob_select_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() &&
!ob_is_rowid_tc(src_expr->get_result_type().get_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 (src_expr->get_result_type().is_raw()) {
func_type = T_FUN_SYS_RAWTOHEX;
func_name = N_RAWTOHEX;
} else if (dst_type.is_nvarchar2()) {
func_type = T_FUN_SYS_TO_NCHAR;
func_name = N_TO_NCHAR;
} 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->add_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));
}
}
r_type1 = ObLobType == r_type1 ? ObLongTextType : r_type1;
r_type2 = ObLobType == r_type2 ? ObLongTextType : r_type2;
if (OB_SUCC(ret) && lib::is_oracle_mode()) {
// oracle 模式下不支持 lob 进行条件比较
// oracle 模式 不支持json类型的比较
if (IS_COMPARISON_OP(op_type)
&& sub_expr1->is_called_in_sql() && sub_expr2->is_called_in_sql()) {
if (ObLongTextType == r_type1 || ObLongTextType == r_type2) {
ret = OB_ERR_INVALID_TYPE_FOR_OP;
LOG_WARN("can't calculate with lob type in oracle mode",
K(ret), K(r_type1), K(r_type2), K(op_type));
} else if (ObJsonType == r_type1 || ObJsonType == r_type2) {
ret = OB_ERR_INVALID_CMP_OP;
LOG_WARN("can't calculate with json type in oracle mode",
K(ret), K(r_type1), K(r_type2), K(op_type));
} else if (ob_is_user_defined_sql_type(r_type1) && ob_is_user_defined_sql_type(r_type2)) {
// other udt types not supported, xmltype does not have order or map member function
ret = OB_ERR_NO_ORDER_MAP_SQL;
LOG_WARN("cannot ORDER objects without MAP or ORDER method", K(ret));
}
}
}
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;
bool has_nstring = ob_is_nstring_type(r_type1) || ob_is_nstring_type(r_type2);
if (ob_is_string_or_lob_type(r_type1) && ob_is_nstring_type(r_type1) == has_nstring) {
cast_left = false;
}
if (ob_is_string_or_lob_type(r_type2) && ob_is_nstring_type(r_type2) == has_nstring) {
cast_right = false;
}
if (T_OP_LIKE == op_type) {
if (ob_is_rowid_tc(r_type1) && ob_is_string_tc(r_type2)) {
cast_left = true;
cast_right = false;
} else if (ob_is_string_tc(r_type1) && ob_is_rowid_tc(r_type2)) {
cast_right = true;
cast_left = false;
}
}
if (cast_left && cast_right) {
dir = ImplicitCastDirection::IC_TO_MIDDLE_TYPE;
middle_type = has_nstring ? ObNVarchar2Type : ObVarcharType;
} else if (cast_left) {
dir = ImplicitCastDirection::IC_A_TO_B;
r_type2 = has_nstring ? ObNVarchar2Type : ObVarcharType;
} else if (cast_right) {
dir = ImplicitCastDirection::IC_B_TO_A;
r_type1 = has_nstring ? ObNVarchar2Type : 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];
if ((ImplicitCastDirection::IC_B_TO_C == dir && ob_is_nchar(r_type1))
|| (ImplicitCastDirection::IC_A_TO_C == dir && ob_is_nchar(r_type2))) {
//nchar and varchar2, convert varchar to nvarchar2
r_type3 = ObNVarchar2Type;
}
}
break;
}
// raw 类型向 char 类型隐式转换的时候,实际会把 raw 类型隐式转换成 varchar 类型
if (ImplicitCastDirection::IC_A_TO_B == dir && ob_is_raw(r_type1) && ObCharType == r_type2) {
r_type3 = ObVarcharType;
dir = ImplicitCastDirection::IC_A_TO_C;
}
// 与interval的运算,不要做任何转换
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;
}
// oracle mode
// create table t (a float);
// column 'a' type is ObNumberFloatType
//
// select 1 - a from t;
// '1' parsed as ObDecimalIntType, cast direction is 'IC_B_TO_A', got:
// select 1 - cast(a as decimal_int(1, 0)) from t;
// which is wrong, for that 'a' may equal to 1.235.
// here, we change calc type back to ObNumberType.
if (ob_is_decimal_int_tc(dst_type)
&& r_type1 == ObNumberFloatType) {
dst_type = ObNumberType;
}
ObCastMode cast_mode = CM_NONE;
if (ObDecimalIntType == dst_type) {
// 如果是decimal int类型,accuracy和decimal int一致即可
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_cast_mode(cast_mode);
if (ob_is_nstring_type(dest_type.get_type())) {
dest_type.set_collation_type(OB_NOT_NULL(session_info)
? session_info->get_nls_collation_nation() : CS_TYPE_UTF16_BIN);
}
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_extra(new_expr->get_extra() | 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 (ob_is_decimal_int_tc(dst_type)
&& r_type2 == ObNumberFloatType) {
dst_type = ObNumberType;
}
if (ObDecimalIntType == dst_type) {
// 如果是decimal int类型,accuracy和decimal int一致即可
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_cast_mode(cast_mode);
if (ob_is_nstring_type(dest_type.get_type())) {
dest_type.set_collation_type(OB_NOT_NULL(session_info)
? session_info->get_nls_collation_nation() : CS_TYPE_UTF16_BIN);
}
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_extra(new_expr->get_extra() | 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_is_nstring_type(dest_type.get_type())) {
dest_type.set_collation_type(OB_NOT_NULL(session_info)
? session_info->get_nls_collation_nation() : CS_TYPE_UTF16_BIN);
}
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_is_nstring_type(dest_type.get_type())) {
dest_type.set_collation_type(OB_NOT_NULL(session_info)
? session_info->get_nls_collation_nation() : CS_TYPE_UTF16_BIN);
}
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);
if (ob_is_nstring_type(dest_type.get_type())) {
dest_type.set_collation_type(OB_NOT_NULL(session_info)
? session_info->get_nls_collation_nation() : CS_TYPE_UTF16_BIN);
}
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()
|| sub_expr1->get_result_type().is_blob_locator()
|| sub_expr2->get_result_type().is_blob_locator());
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()) {
//左(右)子节点为 T_OP_ROW 类型的 ObOpRawExpr 不会显式 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)
// 按正常流程走query_ref_expr的返回类型固定为bigint,应该取返回列的类型比较
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<ObExprResType> &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<ObExprResType> &column_types1 = query_ref_expr1->get_column_types();
column_types1.reset();
ObIArray<ObExprResType> &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));
OX (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_is_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)
{
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));
ObExprResType result_type;
ObSEArray<ObExprResType, 5> params_type;
const ObIArray<ObString> *extended_type_info = NULL;
// Step1: 处理Routine返回值
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));
OZ (pl::ObPLDataType::transform_from_iparam(iparam,
schema_guard,
session_info,
allocator,
sql_proxy,
ret_pl_type));
} 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);
if (OB_SUCC(ret)
&& lib::is_oracle_mode()
&& OB_INVALID_ID != func_info->get_package_id()
&& is_sys_tenant(pl::get_tenant_id_by_object_id(func_info->get_package_id()))
&& OB_NOT_NULL(ret_pl_type.get_data_type())
&& !(ret_pl_type.get_data_type()->get_meta_type().get_type() == ObLongTextType)
&& ob_is_string_type(ret_pl_type.get_data_type()->get_meta_type().get_type())) {
result_type.set_collation_type(ob_is_nstring(ret_pl_type.get_data_type()
->get_meta_type().get_type())
? session_info.get_nls_collation_nation()
: session_info.get_nls_collation());
}
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);
CK (OB_NOT_NULL(r_param));
OX (extended_type_info = &(r_param->get_extended_type_info()));
}
}
// Step2: 处理入参
for (int64_t i = 0; OB_SUCC(ret) && i < func_info->get_param_count(); ++i) {
ObIRoutineParam *iparam = NULL;
pl::ObPLDataType param_pl_type;
ObExprResType 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));
OZ (pl::ObPLDataType::transform_from_iparam(rparam,
schema_guard,
session_info,
allocator,
sql_proxy,
param_pl_type));
} 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: 将入参返回值类型加入rawexpr
OX (udf_raw_expr->set_result_type(result_type));
OZ (udf_raw_expr->set_params_type(params_type));
if (OB_NOT_NULL(extended_type_info)) {
OX (udf_raw_expr->set_enum_set_values(*extended_type_info));
}
#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)
{
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))) {
SQL_LOG(WARN, "failed to exec resovle udf exprs", K(ret), K(udf_info));
}
return ret;
}
/*!
* 解析UDF的参数列表(主要处理参数有默认值的情况, 以及通过名字指定参数的情况):
* 通过名字指定参数, 一定在参数列表的最后面
* 如: func(1, 2, x=>3, y=>4); 合法
* func(1, x=>3, 2); 非法(无法确定2的位置)
* 走到这个函数时在参数列表中没有通过名字指定的参数已经被加入到udf的raw expr
* 因此这个函数主要处理通过名字指定参数的部分
* 如果处理完所有的参数后还有参数空缺, 则尝试下是不是有默认值, 如果没有默认值则报错
*/
int ObRawExprUtils::resolve_udf_param_exprs(ObResolverParams ¶ms,
const ObIRoutineInfo *func_info,
ObUDFInfo &udf_info)
{
int ret = OB_SUCCESS;
ObArray<ObRawExpr*> param_exprs;
ObArray<ObString> param_names;
ObUDFRawExpr *udf_raw_expr = udf_info.ref_expr_;
// 通过名字指定参数统一记录在param_names_和param_exprs里面, 所以这里一定相等
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 {
// 处理剩余的参数, 默认值或者通过名字指定的参数
// Step 1: 首先初始化一个空的参数列表
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: 将通过名字指定的参数加入参数列表
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));