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
4459 lines (4287 loc) · 177 KB
/
ob_raw_expr_util.cpp
File metadata and controls
4459 lines (4287 loc) · 177 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 "sql/optimizer/ob_optimizer_util.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));
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) && share::is_oracle_mode()) {
// compare lob type date in oracle mode not allowed.
if (OB_UNLIKELY(ObLongTextType == r_type1 || ObLongTextType == r_type2) && IS_COMPARISON_OP(op_type)) {
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));
}
}
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];
switch (op_type) {
case T_OP_CNN:
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: {
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: {
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: {
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;
}
// when cast raw to char implicitly, it is cast to varchar actually.
if (ImplicitCastDirection::IC_A_TO_B == dir && ob_is_raw(r_type1) && ObCharType == r_type2) {
r_type3 = ObVarcharType;
dir = ImplicitCastDirection::IC_A_TO_C;
}
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));
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 = (ObNumberType == r_type2 ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][r_type2]
: ObAccuracy::MAX_ACCURACY2[1][r_type2]);
dest_type.set_precision(acc.get_precision());
dest_type.set_scale(acc.get_scale());
dest_type.set_type(r_type2);
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_A: {
ObAccuracy acc = (ObNumberType == r_type1 ? ObAccuracy::DDL_DEFAULT_ACCURACY2[1][r_type1]
: ObAccuracy::MAX_ACCURACY2[1][r_type1]);
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(r_type1);
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 {
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() ||
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 {
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()) {
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)
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 {
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::function_alias(ObRawExprFactory& expr_factory, ObSysFunRawExpr*& expr)
{
int ret = OB_SUCCESS;
int64_t decimal = 10;
int64_t binary = 2;
int64_t octal = 8;
if (OB_ISNULL(expr)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret), K(expr));
} else if (0 == expr->get_func_name().case_compare("bin")) {
// bin(N) is equivalent to CONV(N,10,2)
ObConstRawExpr* from_base = NULL;
ObConstRawExpr* to_base = NULL;
if (OB_FAIL(ObRawExprUtils::build_const_int_expr(expr_factory, ObIntType, decimal, from_base))) {
LOG_WARN("failed to create expr", K(ret));
} else if (OB_FAIL(ObRawExprUtils::build_const_int_expr(expr_factory, ObIntType, binary, to_base))) {
LOG_WARN("failed to create expr", K(ret));
} else if (OB_UNLIKELY(1 != expr->get_param_count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid param count", K(expr->get_param_count()));
} else if (OB_FAIL(expr->add_param_expr(from_base))) {
LOG_WARN("fail to add param expr", K(from_base));
} else if (OB_FAIL(expr->add_param_expr(to_base))) {
LOG_WARN("fail to add param expr", K(to_base));
} else {
expr->set_func_name("conv");
}
} else if (0 == expr->get_func_name().case_compare("oct")) {
// oct(N) is equivalent to CONV(N,10,8)
ObConstRawExpr* from_base = NULL;
ObConstRawExpr* to_base = NULL;
if (OB_FAIL(ObRawExprUtils::build_const_int_expr(expr_factory, ObIntType, decimal, from_base))) {
LOG_WARN("failed to create expr", K(ret));
} else if (OB_FAIL(ObRawExprUtils::build_const_int_expr(expr_factory, ObIntType, octal, to_base))) {
LOG_WARN("failed to create expr", K(ret));
} else if (OB_UNLIKELY(1 != expr->get_param_count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid param count", K(expr->get_param_count()));
} else if (OB_FAIL(expr->add_param_expr(from_base))) {
LOG_WARN("fail to add param expr", K(from_base), K(ret));
} else if (OB_FAIL(expr->add_param_expr(to_base))) {
LOG_WARN("fail to add param expr", K(to_base), K(ret));
} else {
expr->set_func_name("conv");
}
} else if (0 == expr->get_func_name().case_compare("lcase")) {
// lcase is synonym for lower
expr->set_func_name("lower");
} else if (0 == expr->get_func_name().case_compare("ucase")) {
// ucase is synonym for upper
expr->set_func_name("upper");
} else if (0 == expr->get_func_name().case_compare("power")) {
// don't alias "power" to "pow" in oracle mode, because oracle has no
// "pow" function.
if (!share::is_oracle_mode()) {
expr->set_func_name("pow");
}
} else if (0 == expr->get_func_name().case_compare("ws")) {
// ws is synonym for word_segment
expr->set_func_name("word_segment");
} else {
}
return ret;
}
int ObRawExprUtils::make_raw_expr_from_str(const char* expr_str, const int64_t buf_len,
ObExprResolveContext& resolve_ctx, ObRawExpr*& expr, ObIArray<ObQualifiedName>& columns,
ObIArray<ObVarInfo>& sys_vars, ObIArray<ObSubQueryInfo>* sub_query_info, ObIArray<ObAggFunRawExpr*>& aggr_exprs,
ObIArray<ObWinFunRawExpr*>& win_exprs)
{
int ret = OB_SUCCESS;
ObSqlString sql_str;
ParseResult parse_result;
ObParser parser(resolve_ctx.expr_factory_.get_allocator(), SMO_DEFAULT);
if (OB_ISNULL(expr_str) || OB_ISNULL(sub_query_info)) {
ret = OB_INVALID_ARGUMENT;
_LOG_WARN("expr_str is %p, sub_query_info = %p", expr_str, sub_query_info);
} else if (OB_FAIL(sql_str.append_fmt("SELECT %.*s", static_cast<int>(buf_len), expr_str))) {
LOG_WARN("fail to concat string", K(expr_str), K(ret));
} else if (OB_FAIL(parser.parse(sql_str.string(), parse_result))) {
_OB_LOG(WARN,
"parse: %p, %p, %p, msg=[%s], start_col_=[%d], end_col_[%d], "
"line_[%d], yycolumn[%d], yylineno_[%d]",
parse_result.yyscan_info_,
parse_result.result_tree_,
parse_result.malloc_pool_,
parse_result.error_msg_,
parse_result.start_col_,
parse_result.end_col_,
parse_result.line_,
parse_result.yycolumn_,
parse_result.yylineno_);
} else {
if (OB_UNLIKELY(OB_LOGGER.get_log_level() >= OB_LOG_LEVEL_DEBUG)) {
LOG_DEBUG("", "parser result", SJ(ObParserResultPrintWrapper(*parse_result.result_tree_)));
}
ParseNode* stmt_node = NULL;
ParseNode* select_node = NULL;
ParseNode* select_expr_list = NULL;
ParseNode* select_expr = NULL;
ParseNode* parsed_expr = NULL;
stmt_node = parse_result.result_tree_;
if (NULL == stmt_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(stmt_node));
} else if (T_STMT_LIST != stmt_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(stmt_node->type_));
}
if (OB_SUCC(ret)) {
select_node = stmt_node->children_[0];
if (NULL == select_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_node));
} else if (T_SELECT != select_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_node->type_));
}
}
if (OB_SUCC(ret)) {
select_expr_list = select_node->children_[PARSE_SELECT_SELECT];
if (NULL == select_expr_list) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_expr_list));
} else if (T_PROJECT_LIST != select_expr_list->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_expr_list->type_));
}
}
if (OB_SUCC(ret)) {
select_expr = select_expr_list->children_[0];
if (NULL == select_expr) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_expr));
} else if (T_PROJECT_STRING != select_expr->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_expr->type_));
}
}
if (OB_SUCC(ret)) {
parsed_expr = select_expr->children_[0];
if (OB_ISNULL(parsed_expr)) {
LOG_ERROR("internal arg is not correct", KP(parsed_expr));
}
}
if (OB_SUCC(ret)) {
ObArray<ObOpRawExpr*> op_exprs;
ObSEArray<ObUserVarIdentRawExpr*, 1> user_var_exprs;
ObRawExprResolverImpl expr_resolver(resolve_ctx);
// generate raw expr
if (OB_FAIL(expr_resolver.resolve(parsed_expr,
expr,
columns,
sys_vars,
*sub_query_info,
aggr_exprs,
win_exprs,
op_exprs,
user_var_exprs))) {
_LOG_WARN("failed to resolve expr tree, err=%d", ret);
}
}
// destroy syntax tree
parser.free_result(parse_result);
}
return ret;
}
int ObRawExprUtils::make_raw_expr_from_str(const ObString& expr_str, ObExprResolveContext& resolve_ctx,
ObRawExpr*& expr, ObIArray<ObQualifiedName>& column, ObIArray<ObVarInfo>& sys_vars,
ObIArray<ObSubQueryInfo>* sub_query_info, ObIArray<ObAggFunRawExpr*>& aggr_exprs,
ObIArray<ObWinFunRawExpr*>& win_exprs)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(sub_query_info)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(sub_query_info));
} else if (OB_FAIL(make_raw_expr_from_str(expr_str.ptr(),
expr_str.length(),
resolve_ctx,
expr,
column,
sys_vars,
sub_query_info,
aggr_exprs,
win_exprs))) {
LOG_WARN("fail to make_raw_expr_from_str", K(ret));
}
return ret;
}
int ObRawExprUtils::parse_default_expr_from_str(
const ObString& expr_str, ObCollationType expr_str_cs_type, ObIAllocator& allocator, const ParseNode*& node)
{
int ret = OB_SUCCESS;
ObSqlString sql_str;
ParseResult parse_result;
ObSQLMode sql_mode = SMO_DEFAULT;
if (share::is_oracle_mode()) {
sql_mode = DEFAULT_ORACLE_MODE | SMO_ORACLE;
}
ObSQLParser parser(allocator, sql_mode);
MEMSET(&parse_result, 0, sizeof(ParseResult));
parse_result.malloc_pool_ = &allocator;
parse_result.pl_parse_info_.is_pl_parse_ = true;
parse_result.pl_parse_info_.is_pl_parse_expr_ = true;
parse_result.sql_mode_ = sql_mode;
parse_result.charset_info_ = ObCharset::get_charset(expr_str_cs_type);
parse_result.is_not_utf8_connection_ = ObCharset::is_valid_collation(expr_str_cs_type)
? (ObCharset::charset_type_by_coll(expr_str_cs_type) != CHARSET_UTF8MB4)
: false;
if (OB_FAIL(sql_str.append_fmt("DO %.*s", expr_str.length(), expr_str.ptr()))) {
LOG_WARN("failed to concat expr str", K(expr_str), K(ret));
} else if (OB_FAIL(parser.parse(sql_str.string().ptr(), sql_str.string().length(), parse_result))) {
_OB_LOG(WARN,
"parse: %p, %p, %p, msg=[%s], start_col_=[%d], end_col_[%d], "
"line_[%d], yycolumn[%d], yylineno_[%d]",
parse_result.yyscan_info_,
parse_result.result_tree_,
parse_result.malloc_pool_,
parse_result.error_msg_,
parse_result.start_col_,
parse_result.end_col_,
parse_result.line_,
parse_result.yycolumn_,
parse_result.yylineno_);
} else {
if (OB_UNLIKELY(OB_LOGGER.get_log_level() >= OB_LOG_LEVEL_DEBUG)) {
LOG_DEBUG("", "parser result", SJ(ObParserResultPrintWrapper(*parse_result.result_tree_)));
}
ParseNode* expr_node = NULL;
if (OB_ISNULL(expr_node = parse_result.result_tree_->children_[0])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("failed to parse default expr node", K(ret), K(expr_str));
} else if (OB_UNLIKELY(T_DEFAULT != expr_node->type_) || OB_UNLIKELY(1 != expr_node->num_child_) ||
OB_ISNULL(expr_node->children_) || OB_ISNULL(expr_node->children_[0])) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN(
"expr node type is illegal", K(ret), K(expr_node->type_), K(expr_node->num_child_), K(expr_node->children_));
} else {
node = expr_node->children_[0];
}
}
return ret;
}
int ObRawExprUtils::parse_expr_node_from_str(const ObString& expr_str, ObIAllocator& allocator, const ParseNode*& node)
{
int ret = OB_SUCCESS;
ObSqlString sql_str;
ParseResult parse_result;
ObSQLMode sql_mode = SMO_DEFAULT;
if (share::is_oracle_mode()) {
sql_mode = DEFAULT_ORACLE_MODE | SMO_ORACLE;
}
ObParser parser(allocator, sql_mode);
if (OB_FAIL(sql_str.append_fmt("SELECT %.*s FROM DUAL", expr_str.length(), expr_str.ptr()))) {
LOG_WARN("fail to concat string", K(expr_str), K(ret));
} else if (OB_FAIL(parser.parse(sql_str.string(), parse_result))) {
_OB_LOG(WARN,
"parse: %p, %p, %p, msg=[%s], start_col_=[%d], end_col_[%d], "
"line_[%d], yycolumn[%d], yylineno_[%d]",
parse_result.yyscan_info_,
parse_result.result_tree_,
parse_result.malloc_pool_,
parse_result.error_msg_,
parse_result.start_col_,
parse_result.end_col_,
parse_result.line_,
parse_result.yycolumn_,
parse_result.yylineno_);
} else {
if (OB_UNLIKELY(OB_LOGGER.get_log_level() >= OB_LOG_LEVEL_DEBUG)) {
LOG_DEBUG("", "parser result", SJ(ObParserResultPrintWrapper(*parse_result.result_tree_)));
}
ParseNode* stmt_node = NULL;
ParseNode* select_node = NULL;
ParseNode* select_expr_list = NULL;
ParseNode* select_expr = NULL;
stmt_node = parse_result.result_tree_;
if (NULL == stmt_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(stmt_node));
} else if (T_STMT_LIST != stmt_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(stmt_node->type_));
}
if (OB_SUCC(ret)) {
select_node = stmt_node->children_[0];
if (NULL == select_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_node));
} else if (T_SELECT != select_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_node->type_));
}
}
if (OB_SUCC(ret)) {
select_expr_list = select_node->children_[PARSE_SELECT_SELECT];
if (NULL == select_expr_list) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_expr_list));
} else if (T_PROJECT_LIST != select_expr_list->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_expr_list->type_));
}
}
if (OB_SUCC(ret)) {
select_expr = select_expr_list->children_[0];
if (NULL == select_expr) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_expr));
} else if (T_PROJECT_STRING != select_expr->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_expr->type_));
}
}
if (OB_SUCC(ret)) {
node = select_expr->children_[0];
}
}
return ret;
}
int ObRawExprUtils::parse_bool_expr_node_from_str(
const common::ObString& expr_str, common::ObIAllocator& allocator, const ParseNode*& node)
{
int ret = OB_SUCCESS;
ObSqlString sql_str;
ParseResult parse_result;
ObSQLMode sql_mode = SMO_DEFAULT;
if (share::is_oracle_mode()) {
sql_mode = DEFAULT_ORACLE_MODE | SMO_ORACLE;
}
ObParser parser(allocator, sql_mode);
if (OB_FAIL(sql_str.append_fmt("SELECT 1 FROM DUAL WHERE %.*s", expr_str.length(), expr_str.ptr()))) {
LOG_WARN("fail to concat string", K(expr_str), K(ret));
} else if (OB_FAIL(parser.parse(sql_str.string(), parse_result))) {
_OB_LOG(WARN,
"parse: %p, %p, %p, msg=[%s], start_col_=[%d], end_col_[%d], "
"line_[%d], yycolumn[%d], yylineno_[%d]",
parse_result.yyscan_info_,
parse_result.result_tree_,
parse_result.malloc_pool_,
parse_result.error_msg_,
parse_result.start_col_,
parse_result.end_col_,
parse_result.line_,
parse_result.yycolumn_,
parse_result.yylineno_);
} else {
if (OB_UNLIKELY(OB_LOGGER.get_log_level() >= OB_LOG_LEVEL_DEBUG)) {
LOG_DEBUG("", "parser result", SJ(ObParserResultPrintWrapper(*parse_result.result_tree_)));
}
ParseNode* stmt_node = NULL;
ParseNode* select_node = NULL;
ParseNode* where_node = NULL;
ParseNode* expr_node = NULL;
stmt_node = parse_result.result_tree_;
if (NULL == stmt_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(stmt_node));
} else if (T_STMT_LIST != stmt_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(stmt_node->type_));
}
if (OB_SUCC(ret)) {
select_node = stmt_node->children_[0];
if (NULL == select_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(select_node));
} else if (T_SELECT != select_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(select_node->type_));
}
}
if (OB_SUCC(ret)) {
where_node = select_node->children_[PARSE_SELECT_WHERE];
if (NULL == where_node) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", KP(where_node));
} else if (T_WHERE_CLAUSE != where_node->type_) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("internal arg is not correct", K(where_node->type_));
}
}
if (OB_SUCC(ret)) {