forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_parser.cpp
More file actions
1325 lines (1262 loc) · 45.1 KB
/
Copy pathob_parser.cpp
File metadata and controls
1325 lines (1262 loc) · 45.1 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_PARSER
#include "ob_parser.h"
#include "lib/oblog/ob_log.h"
#include "parse_malloc.h"
#include "parse_node.h"
#include "ob_sql_parser.h"
#include "pl/parser/ob_pl_parser.h"
using namespace oceanbase::pl;
using namespace oceanbase::sql;
using namespace oceanbase::common;
ObParser::ObParser(common::ObIAllocator &allocator,
ObSQLMode mode,
ObCharsets4Parser charsets4parser,
QuestionMarkDefNameCtx *ctx)
:allocator_(&allocator),
sql_mode_(mode),
charsets4parser_(charsets4parser),
def_name_ctx_(ctx)
{}
ObParser::~ObParser()
{}
#define ISSPACE(c) ((c) == ' ' || (c) == '\n' || (c) == '\r' || (c) == '\t' || (c) == '\f' || (c) == '\v')
bool ObParser::is_pl_stmt(const ObString &stmt, bool *is_create_func, bool *is_call_procedure)
{
int ret = OB_SUCCESS;
State state = S_START;
State save_state = state;
const char *p = stmt.ptr();
const char *p_start = p;
const char *p_end = p + stmt.length();
bool is_pl = false;
bool is_not_pl = false;
const char *p_normal_start = nullptr;
while (p < p_end && !is_pl && !is_not_pl) {
switch (state) {
case S_START:
case S_CREATE:
case S_DD:
case S_BEGIN:
case S_DROP:
case S_ALTER:
case S_UPDATE:
case S_SUBMIT:
case S_CANCEL: {
if (ISSPACE(*p)) {
p++;
} else {
if ('-' == *p) {
if ((p + 1) < p_end && '-' == *(p + 1)) {
save_state = state;
state = S_COMMENT;
p += 2;
}
} else if ('/' == *p) {
if ((p + 1) < p_end && '*' == *(p + 1)) {
save_state = state;
state = S_C_COMMENT;
p += 2;
}
} else if ('#' == *p && lib::is_mysql_mode()) {
save_state = state;
state = S_COMMENT;
p += 1;
}
if (state != S_COMMENT && state != S_C_COMMENT) {
p_normal_start = p;
save_state = state;
state = S_NORMAL;
p++;
}
}
} break;
case S_COMMENT: {
if (*p == '\n') {
state = save_state;
}
p++;
} break;
case S_C_COMMENT: {
if (*p == '*') {
if ((p + 1 < p_end) && '/' == *(p + 1)) {
state = save_state;
p++;
}
}
p++;
} break;
case S_NORMAL: {
if (ISSPACE(*p) || (p == (p_end - 1))) {
int64_t length = (p - p_normal_start) + (ISSPACE(*p) ? 0 : 1);
ObString normal(length, p_normal_start);
state = transform_normal(
save_state, normal, is_pl, is_not_pl, is_create_func, is_call_procedure);
}
p++;
} break;
default: {
is_not_pl = true;
} break;
}
}
return is_pl;
}
// Check if stmt is explain statement.
// On success, set p_normal_start to first char after explain syntax; on error, set p_normal_start to null.
//
// For example, call is_explain_stmt() with SQL query below would return true,
// and p_normal_start would be set as shown below.
//
// explain update t1 set b=0 where a=1;update t1 set b=0 where a=2;
// ^
// p_normal_start
//
// Note that, when using is_explain_stmt(), we assume given SQL query has no syntax errors.
bool ObParser::is_explain_stmt(const ObString &stmt, const char *&p_normal_start)
{
State state = S_START;
State save_state = state;
const char *p = stmt.ptr();
const char *p_start = p;
const char *p_end = p + stmt.length();
bool is_explain = false;
bool has_error = false;
p_normal_start = nullptr;
int i = 0;
static const char *lower[S_MAX] = { nullptr };
static const char *upper[S_MAX] = { nullptr };
static bool inited = false;
if (!inited) {
lower[S_EXPLAIN] = "explain";
lower[S_EXPLAIN_BASIC] = "basic";
lower[S_EXPLAIN_EXTENDED] = "extended";
lower[S_EXPLAIN_EXTENDED_NOADDR] = "extended_noaddr";
lower[S_EXPLAIN_PARTITIONS] = "partitions";
upper[S_EXPLAIN] = "EXPLAIN";
upper[S_EXPLAIN_BASIC] = "BASIC";
upper[S_EXPLAIN_EXTENDED] = "EXTENDED";
upper[S_EXPLAIN_EXTENDED_NOADDR] = "EXTENDED_NOADDR";
upper[S_EXPLAIN_PARTITIONS] = "PARTITIONS";
inited = true;
}
while (p < p_end && !has_error) {
switch (state) {
case S_START:
case S_EXPLAIN_FORMAT: {
if (ISSPACE(*p)) {
p++;
} else {
if (!is_comment(p, p_end, save_state, state, S_INVALID) && state != S_INVALID) {
if (S_START == state) {
// p is at first key token (e.g. not comments or white spaces),
// move to S_EXPLAIN to check if there is 'explain' keyword
save_state = state;
state = S_EXPLAIN;
} else if (S_EXPLAIN_FORMAT == state) {
if (lower[S_EXPLAIN_BASIC][0] == *p || upper[S_EXPLAIN_BASIC][0] == *p) {
save_state = state;
state = S_EXPLAIN_BASIC;
} else if (lower[S_EXPLAIN_EXTENDED][0] == *p || upper[S_EXPLAIN_EXTENDED][0] == *p) {
save_state = state;
state = S_EXPLAIN_EXTENDED;
} else if (lower[S_EXPLAIN_PARTITIONS][0] == *p || upper[S_EXPLAIN_PARTITIONS][0] == *p) {
save_state = state;
state = S_EXPLAIN_PARTITIONS;
} else {
save_state = state;
state = S_NORMAL;
}
}
}
}
} break;
// since we have assumed SQL has no syntax errors,
// we will always find end tokens (e.g. '\n', '*/') for comments.
case S_COMMENT: {
if (*p == '\n') {
// end of '--' comments
state = save_state;
}
p++;
} break;
case S_C_COMMENT: {
if (*p == '*') {
if ((p + 1 < p_end) && '/' == *(p + 1)) {
// end of '/**/' comments
state = save_state;
p++;
}
}
p++;
} break;
case S_EXPLAIN:
case S_EXPLAIN_BASIC:
case S_EXPLAIN_EXTENDED:
case S_EXPLAIN_PARTITIONS: {
State next_state = S_NORMAL;
if (S_EXPLAIN == state) {
// after checking 'explain' keyword,
// we should move to S_EXPLAIN_FORMAT to check extra keywords such as 'basic'
next_state = S_EXPLAIN_FORMAT;
} else if (S_EXPLAIN_EXTENDED == state) {
// after checking 'extended' keyword,
// we should move to S_EXPLAIN_EXTENDED_NOADDR to check if 'extended_noaddr' matches
next_state = S_EXPLAIN_EXTENDED_NOADDR;
}
match_state(p,
is_explain,
has_error,
lower,
upper,
i,
save_state,
state,
next_state);
} break;
case S_EXPLAIN_EXTENDED_NOADDR: {
if (i == 0) {
// we have just finished checking 'extended' keyword,
// continue checking only if next letter is '_'
if ('_' == *p) {
i = strlen(lower[S_EXPLAIN_EXTENDED]);
} else {
state = S_NORMAL;
}
} else {
match_state(p,
is_explain,
has_error,
lower,
upper,
i,
save_state,
state,
S_NORMAL);
}
} break;
case S_NORMAL: {
// we have finished checking keywords,
// find start of first non-space and non-comment char
if (ISSPACE(*p)) {
p++;
} else if (!is_comment(p, p_end, save_state, state, S_NORMAL)) {
p_normal_start = p;
p = p_end;
}
} break;
case S_INVALID:
default: {
is_explain = false;
has_error = true;
} break;
}
}
return is_explain && !has_error;
}
// Check if p matches keyword at index i.
// When all letters have matched, set is_explain=true if there hasn't been any errors
// On success, move state to next_state; on error, move state to S_INVALID.
//
// Note that, when using match_state(), we assume index i is valid.
void ObParser::match_state(const char*&p,
bool &is_explain,
bool &has_error,
const char *lower[S_MAX],
const char *upper[S_MAX],
int &i,
State &save_state,
State &state,
State next_state)
{
if (lower[state][i] == *p || upper[state][i] == *p) {
i++;
p++;
if (i == strlen(lower[state])) {
is_explain = !has_error;
save_state = state;
state = next_state;
i = 0;
}
} else {
save_state = state;
state = S_INVALID;
}
}
// Check if comments start at p.
bool ObParser::is_comment(const char *&p,
const char *&p_end,
State &save_state,
State &state,
State error_state)
{
bool found_comment = false;
if ('-' == *p) {
if ((p + 1) < p_end && '-' == *(p + 1)) {
save_state = state;
state = S_COMMENT;
p += 2;
found_comment = true;
} else {
state = error_state;
}
} else if ('/' == *p) {
if ((p + 1) < p_end && '*' == *(p + 1)) {
save_state = state;
state = S_C_COMMENT;
p += 2;
found_comment = true;
} else {
state = error_state;
}
}
return found_comment;
}
ObParser::State ObParser::transform_normal(ObString &normal)
{
State state = S_INVALID;
#define IF(len, s, str) \
if (len == normal.length() && 0 == STRNCASECMP(normal.ptr(), str, len)) { \
state = s; \
}
#define ELSIF(len, s, str) \
else if (len == normal.length() && 0 == STRNCASECMP(normal.ptr(), str, len)) { \
state = s; \
}
#define ELSE() \
else { \
LOG_DEBUG("transform_normal", K(state), K(normal)); \
}
IF(6, S_CREATE, "create")
ELSIF(8, S_FUNCTION, "function")
ELSIF(9, S_PROCEDURE, "procedure")
ELSIF(7, S_PACKAGE, "package")
ELSIF(7, S_TRIGGER, "trigger")
ELSIF(5, S_EVENT, "event")
ELSIF(4, S_TYPE, "type")
ELSIF(2, S_OR, "or")
ELSIF(7, S_REPLACE, "replace")
ELSIF(7, S_DEFINER, "definer")
ELSIF(2, S_DO, "do")
ELSIF(2, S_DD, "dd")
ELSIF(5, S_BEGIN, "begin")
ELSIF(7, S_DECLARE, "declare")
ELSIF(2, S_DECLARE, "<<")
ELSIF(4, S_DROP, "drop")
ELSIF(4, S_CALL, "call")
ELSIF(5, S_ALTER, "alter")
ELSIF(6, S_UPDATE, "update")
ELSIF(2, S_OF, "of")
ELSIF(11, S_EDITIONABLE, "editionable")
ELSIF(14, S_EDITIONABLE, "noneditionable")
ELSIF(6, S_SIGNAL, "signal")
ELSIF(8, S_RESIGNAL, "resignal")
ELSIF(5, S_FORCE, "force")
ELSIF(6, S_SUBMIT, "submit")
ELSIF(6, S_CANCEL, "cancel")
ELSIF(3, S_JOB, "job")
ELSE()
if (S_INVALID == state
&& normal.length() >= 2 && 0 == STRNCASECMP(normal.ptr(), "<<", 2)) {
state = S_DECLARE;
}
if (S_INVALID == state
&& normal.length() >= 7 && 0 == STRNCASECMP(normal.ptr(), "definer", 7)) {
state = S_DEFINER;
}
#undef IF
#undef ELSIF
#undef ELSE
return state;
}
ObParser::State ObParser::transform_normal(
ObParser::State state, ObString &normal, bool &is_pl, bool &is_not_pl,
bool *is_create_func, bool *is_call_procedure)
{
switch (state) {
case S_START: {
State token = transform_normal(normal);
switch (token) {
case S_DO:
case S_DECLARE:
case S_PROCEDURE:
case S_FUNCTION:
case S_PACKAGE:
case S_TRIGGER:
case S_EVENT:
case S_TYPE:
case S_SIGNAL:
case S_RESIGNAL: {
is_pl = true;
} break;
case S_CALL: {
is_not_pl = false;
is_pl = lib::is_mysql_mode();
if (is_call_procedure != NULL) {
*is_call_procedure = true;
}
} break;
case S_CREATE:
case S_DD:
case S_BEGIN:
case S_DROP:
case S_ALTER:
case S_UPDATE:
case S_SUBMIT:
case S_CANCEL: {
state = token;
} break;
case S_INVALID:
default: {
is_not_pl = true;
} break;
}
} break;
case S_CREATE: {
State token = transform_normal(normal);
switch (token) {
case S_PROCEDURE:
case S_PACKAGE:
case S_TRIGGER:
case S_EVENT:
case S_TYPE:
case S_DEFINER: {
is_pl = true;
} break;
case S_OR:
case S_REPLACE:
case S_EDITIONABLE:
case S_FORCE: {
// do nothing ...
} break;
case S_FUNCTION: {
is_pl = true;
if (is_create_func != NULL) {
*is_create_func = true;
}
} break;
default: {
is_not_pl = true;
} break;
}
} break;
case S_DD: {
if (S_BEGIN == transform_normal(normal)) {
is_pl = true;
} else {
is_not_pl = true;
}
} break;
case S_BEGIN: {
if ((normal.length() > 0 && ';' == (normal.ptr()[0]))
|| (normal.length() >= 4 && 0 == STRNCASECMP(normal.ptr(), "work", 4))) {
is_not_pl = true;
} else {
is_pl = true;
}
} break;
case S_DROP:
case S_ALTER: {
State token = transform_normal(normal);
if (S_PROCEDURE == token || S_FUNCTION == token
|| S_PACKAGE == token || S_TRIGGER == token || S_EVENT == token || S_TYPE == token || S_DEFINER == token) {
is_pl = true;
} else {
is_not_pl = true;
}
} break;
case S_UPDATE: {
if (S_OF == transform_normal(normal)) {
is_pl = true;
} else {
is_not_pl = true;
}
} break;
case S_SUBMIT:
case S_CANCEL: {
State token = transform_normal(normal);
if (S_JOB == token) {
is_pl = true;
} else {
is_not_pl = true;
}
} break;
default: {
is_not_pl = true;
LOG_WARN_RET(common::OB_ERR_UNEXPECTED, "unexpected state", K(state));
} break;
}
return state;
}
bool ObParser::is_single_stmt(const ObString &stmt)
{
int64_t count = 0;
// Remove trailing extra spaces, otherwise 'select 1 from dual; ' this will cause an error
int64_t end_trim_offset = stmt.length();
while (end_trim_offset > 0 && ISSPACE(stmt[end_trim_offset - 1])) {
end_trim_offset--;
}
for (int64_t i = 0; i < end_trim_offset; i++) {
if (';' == stmt[i]) {
count++;
}
}
return (0 == count || (1 == count && stmt[end_trim_offset - 1] == ';'));
}
int ObParser::split_start_with_pl(const ObString &stmt,
ObIArray<ObString> &queries,
ObMPParseStat &parse_stat)
{
int ret = OB_SUCCESS;
int tmp_ret = OB_SUCCESS;
int64_t remain = stmt.length();
ParseResult parse_result;
ParseMode parse_mode = MULTI_MODE;
parse_stat.reset();
// Bypass parser's unfriendly handling of empty queries: remove trailing spaces ourselves
while (remain > 0 && ISSPACE(stmt[remain - 1])) {
--remain;
}
// Remove the last '\0', for compatibility with mysql
if (remain > 0 && '\0' == stmt[remain - 1]) {
--remain;
}
// Remove trailing spaces
while (remain > 0 && (ISSPACE(stmt[remain - 1]))) {
--remain;
}
// For special handling of empty statements
if (OB_UNLIKELY(0 >= remain)) {
ObString part; // empty string
ret = queries.push_back(part);
}
if (remain > 0 && OB_SUCC(ret) && !parse_stat.parse_fail_) {
ObArenaAllocator allocator(CURRENT_CONTEXT->get_malloc_allocator());
allocator.set_label("PLSplitStmt");
ObIAllocator *bak_allocator = allocator_;
allocator_ = &allocator;
ObString part(remain, stmt.ptr());
if (OB_FAIL(tmp_ret = parse(part, parse_result, parse_mode, false, true))) {
if(parse_result.result_tree_ != NULL && parse_result.result_tree_->num_child_ > 0) {
ret = OB_SUCCESS;
for (int64_t i = 0; OB_SUCC(ret) && i < parse_result.result_tree_->num_child_; ++i) {
int64_t str_len = parse_result.result_tree_->children_[i]->str_len_;
int64_t offset = parse_result.result_tree_->children_[i]->pos_;
ObString query(str_len, stmt.ptr() + offset);
OZ(queries.push_back(query));
}
int64_t success_len = 0;
OX(success_len = parse_result.result_tree_->children_[parse_result.result_tree_->num_child_ - 1]->str_len_ +
parse_result.result_tree_->children_[parse_result.result_tree_->num_child_ - 1]->pos_);
if(OB_SUCC(ret)) {
if (';' == stmt[success_len]) {
success_len++;
} else {
ret = OB_ERR_PARSE_SQL;
}
}
CK(success_len < remain);
ObString error_part(remain - success_len, stmt.ptr() + success_len);
OZ(queries.push_back(error_part));
} else {
ret = queries.push_back(part);
}
if (OB_SUCCESS == ret) {
parse_stat.parse_fail_ = true;
parse_stat.fail_query_idx_ = queries.count() - 1;
parse_stat.fail_ret_ = tmp_ret;
}
LOG_WARN("fail parse multi part", K(part), K(stmt), K(tmp_ret));
} else {
CK(remain == parse_result.end_col_);
CK(nullptr != bak_allocator);
CK(nullptr != parse_result.result_tree_);
for (int64_t i = 0; OB_SUCC(ret) && i < parse_result.result_tree_->num_child_; ++i) {
int64_t str_len = parse_result.result_tree_->children_[i]->str_len_;
int64_t offset = parse_result.result_tree_->children_[i]->pos_;
ObString query(str_len, stmt.ptr() + offset);
OZ(queries.push_back(query));
}
}
allocator_ = bak_allocator;
}
return ret;
}
// In multi-stmt mode(delimiter #) eg: create t1; create t2; create t3
// in the case of is_ret_first_stmt(false), queries will return the following stmts:
// queries[0]: create t1;
// queries[1]: create t2;
// queries[2]: create t3;
// in the case of is_ret_first_stmt(true) and it will return one element
// queries[0]: create t1;
// Actually, sql executor only executes the first stmt(create t1) and ignore the others
// even though try to execute stmts like 'create t1; create t2; create t3;'
int ObParser::split_multiple_stmt(const ObString &stmt,
ObIArray<ObString> &queries,
ObMPParseStat &parse_stat,
bool is_ret_first_stmt,
bool is_prepare)
{
int ret = OB_SUCCESS;
// For single SQL statement scenarios, first check is_single_stmt, then check is_pl_stmt, to avoid the overhead of string comparison for is_pl_stmt
if (is_single_stmt(stmt)) {
ObString query(stmt.length(), stmt.ptr());
ret = queries.push_back(query);
} else if (is_pl_stmt(stmt)) {
if (lib::is_mysql_mode()) {
ret = split_start_with_pl(stmt, queries, parse_stat);
} else {
ObString query(stmt.length(), stmt.ptr());
ret = queries.push_back(query);
}
} else {
ParseResult parse_result;
ParseMode parse_mode = MULTI_MODE;
int64_t offset = 0;
int64_t remain = stmt.length();
int64_t semicolon_offset = INT64_MIN;
parse_stat.reset();
// Bypass parser's unfriendly handling of empty queries: remove trailing spaces ourselves
while (remain > 0 && ISSPACE(stmt[remain - 1])) {
--remain;
}
// Remove the last '\0', for compatibility with mysql
if (remain > 0 && '\0' == stmt[remain - 1]) {
--remain;
}
// Remove trailing spaces and semicolons
while (remain > 0 && ((ISSPACE(stmt[remain - 1])) ||
(lib::is_mysql_mode() && stmt[remain - 1] == ';'))) {
if (stmt[remain - 1] == ';') {
semicolon_offset = remain - 1;
}
--remain;
}
// Leave at most one semicolon
remain = max(remain, semicolon_offset + 1);
// For special handling of empty statements
if (OB_UNLIKELY(0 >= remain)) {
ObString part; // empty string
ret = queries.push_back(part);
}
int tmp_ret = OB_SUCCESS;
bool need_continue = true;
while (remain > 0 && OB_SUCC(ret) && !parse_stat.parse_fail_ && need_continue) {
ObArenaAllocator allocator(CURRENT_CONTEXT->get_malloc_allocator());
allocator.set_label("SplitMultiStmt");
ObIAllocator *bak_allocator = allocator_;
allocator_ = &allocator;
int64_t str_len = 0;
//for save memory allocate in parser, we need try find the single stmt length in advance
//calc the end position of a single sql.
get_single_sql(stmt, offset, remain, str_len);
str_len = str_len == remain ? str_len : str_len + 1;
ObString part(str_len, stmt.ptr() + offset);
ObString remain_part(remain, stmt.ptr() + offset);
//first try parse part str, because it's have less length and need less memory
if (OB_FAIL(tmp_ret = parse(part, parse_result, parse_mode, false, true))) {
//if parser part str failed, then try parse all remain part, avoid parse many times
//bug:
tmp_ret = OB_SUCCESS;
tmp_ret = parse(remain_part, parse_result, parse_mode);
}
if (OB_SUCC(tmp_ret)) {
int32_t single_stmt_length = parse_result.end_col_;
if (is_ret_first_stmt) {
ObString first_query(single_stmt_length, stmt.ptr());
ret = queries.push_back(first_query);
need_continue = false; // only return the first stmt, so ignore the remaining stmts
} else {
if (is_pl_stmt(part) && lib::is_mysql_mode()) {
ObString query(remain, stmt.ptr() + offset);
allocator_ = bak_allocator;
ret = split_start_with_pl(query, queries, parse_stat);
need_continue = false;
} else {
ObString query(single_stmt_length,stmt.ptr() + offset);
ret = queries.push_back(query);
}
}
remain -= parse_result.end_col_;
offset += parse_result.end_col_;
if (remain < 0 || offset > stmt.length()) {
LOG_ERROR("split_multiple_stmt data error",
K(remain), K(offset), K(stmt.length()), K(ret));
}
} else {
ObString query(static_cast<int32_t>(remain), stmt.ptr() + offset);
ret = queries.push_back(query);
if (OB_SUCCESS == ret) {
parse_stat.parse_fail_ = true;
parse_stat.fail_query_idx_ = queries.count() - 1;
parse_stat.fail_ret_ = tmp_ret;
}
LOG_WARN("fail parse multi part", K(part), K(stmt), K(ret));
}
allocator_ = bak_allocator;
}
}
return ret;
}
int ObParser::check_is_insert(common::ObIArray<common::ObString> &queries, bool &is_ins)
{
int ret = OB_SUCCESS;
is_ins = false;
bool is_replace = false;
if (queries.count() < 1) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("queries is unexpected", K(ret));
} else if (queries.count() > 1) {
// query count greater than 1, multi query no optimization
} else {
ObString &sql_str = queries.at(0);
is_ins = (sql_str.length() > 6 && 0 == STRNCASECMP(sql_str.ptr(), "insert", 6));
is_replace = (sql_str.length() > 7 && 0 == STRNCASECMP(sql_str.ptr(), "replace", 7));
is_ins = is_ins | is_replace;
}
return ret;
}
int32_t ObParser::get_well_formed_errlen(const ObCharsetInfo *charset_info,
const char *err_ptr,
int32_t err_len)
{
static const int32_t max_error_length = 80;
err_len = std::min(err_len, max_error_length);
int32_t res_len = err_len;
//issue/29914471
if (OB_NOT_NULL(charset_info)) {
int err = 0;
int32_t well_formed_errlen =
static_cast<int32_t>(charset_info->cset->well_formed_len(charset_info,
err_ptr,
err_ptr + err_len,
UINT64_MAX,
&err));
LOG_DEBUG("check well_formed_errlen", K(well_formed_errlen), K(err_len),
K(err), K(charset_info->name));
if (err != 0
&& well_formed_errlen < err_len
&& err_len - well_formed_errlen < charset_info->mbmaxlen) {
res_len = well_formed_errlen;
}
}
return res_len;
}
// avoid separating sql by semicolons in quotes or comment.
void ObParser::get_single_sql(const common::ObString &stmt, int64_t offset, int64_t remain, int64_t &str_len) {
/* following two flags are used to mark wether we are in comment, if in comment, ';' can't be used to split sql*/
// in -- comment
bool comment_flag = false;
// in /*! comment */ or /* comment */
bool c_comment_flag = false;
/* following three flags are used to mark wether we are in quotes.*/
// in '', single quotes
bool sq_flag = false;
// in "", double quotes
bool dq_flag = false;
// in ``, backticks.
bool bt_flag = false;
bool is_escape = false;
bool in_comment = false;
bool in_string = false;
while (str_len < remain && (in_comment || (stmt[str_len + offset] != ';'))) {
if (!in_comment && !in_string) {
if (str_len + 1 >= remain) {
} else if ((stmt[str_len + offset] == '-' && stmt[str_len + offset + 1] == '-') || stmt[str_len + offset + 1] == '#') {
comment_flag = true;
} else if (stmt[str_len + offset] == '/' && stmt[str_len + offset + 1] == '*') {
c_comment_flag = true;
} else if (stmt[str_len + offset] == '\'') {
sq_flag = true;
} else if (stmt[str_len + offset] == '"') {
dq_flag = true;
} else if (stmt[str_len + offset] == '`') {
bt_flag = true;
}
} else if (in_comment) {
if (comment_flag) {
if (stmt[str_len + offset] == '\r' || stmt[str_len + offset] == '\n') {
comment_flag = false;
}
} else if (c_comment_flag) {
if (str_len + 1 >= remain) {
} else if (stmt[str_len + offset] == '*' && (str_len + 1 < remain) && stmt[str_len + offset + 1] == '/') {
c_comment_flag = false;
}
}
} else if (in_string) {
if (str_len + 1 >= remain) {
} else if (lib::is_mysql_mode() && !bt_flag && stmt[str_len + offset] == '\\') {
// in mysql mode, handle the escape char in '' and ""
++ str_len;
} else if (sq_flag) {
if (stmt[str_len + offset] == '\'') {
sq_flag = false;
}
} else if (dq_flag) {
if (stmt[str_len + offset] == '"') {
dq_flag = false;
}
} else if (bt_flag) {
if (stmt[str_len + offset] == '`') {
bt_flag = false;
}
}
}
++ str_len;
// update states.
in_comment = comment_flag || c_comment_flag;
in_string = sq_flag || bt_flag || dq_flag;
}
}
int ObParser::parse_sql(const ObString &stmt,
ParseResult &parse_result,
const bool no_throw_parser_error)
{
int ret = OB_SUCCESS;
ObSQLParser sql_parser(*(ObIAllocator*)(parse_result.malloc_pool_), sql_mode_);
ObString stmt_str = stmt;
if (OB_FAIL(sql_parser.parse(stmt.ptr(), stmt.length(), parse_result))) {
// if is multi_values_parser opt not need retry
if (lib::is_mysql_mode() && !parse_result.is_multi_values_parser_) {
parse_result.enable_compatible_comment_ = false;
parse_result.mysql_compatible_comment_ = false;
if (OB_FAIL(sql_parser.parse(stmt.ptr(), stmt.length(), parse_result))) {
//do nothing.
}
}
#ifdef NDEBUG
if (parse_result.may_contain_sensitive_data_) {
parse_result.contain_sensitive_data_ = true;
stmt_str = ObString(OB_MASKED_STR);
}
#endif
if (!no_throw_parser_error) {
LOG_INFO("failed to parse stmt as sql", K(stmt_str), K(ret));
}
} else if (parse_result.is_dynamic_sql_) {
memmove(parse_result.no_param_sql_ + parse_result.no_param_sql_len_,
parse_result.input_sql_ + parse_result.pl_parse_info_.last_pl_symbol_pos_,
parse_result.input_sql_len_ - parse_result.pl_parse_info_.last_pl_symbol_pos_);
parse_result.no_param_sql_len_
+= parse_result.input_sql_len_ - parse_result.pl_parse_info_.last_pl_symbol_pos_;
} else { /*do nothing*/ }
#ifndef NDEBUG
parse_result.contain_sensitive_data_ = false;
#endif
if (parse_result.is_fp_ || parse_result.is_multi_query_) {
if (OB_FAIL(ret) && !no_throw_parser_error) {
LOG_WARN("failed to fast parameterize", K(stmt_str), K(ret));
}
}
if (OB_SUCC(ret) &&
parse_result.enable_compatible_comment_ &&
parse_result.mysql_compatible_comment_) {
ret = OB_ERR_PARSE_SQL;
LOG_WARN("the sql is invalid", K(ret), K(stmt_str));
}
if (OB_FAIL(ret) && !no_throw_parser_error) {
auto err_charge_sql_mode = false;
LOG_WARN("failed to parse the statement",
K(stmt_str),
K(parse_result.is_fp_),
K(parse_result.is_multi_query_),
K(parse_result.yyscan_info_),
K(parse_result.result_tree_),
K(parse_result.malloc_pool_),
"message", parse_result.error_msg_,
"start_col", parse_result.start_col_,
"end_col", parse_result.end_col_,
K(parse_result.line_),
K(parse_result.yycolumn_),
K(parse_result.yylineno_),
K(parse_result.extra_errno_),
K(parse_result.is_for_remap_),
K(err_charge_sql_mode),
K(sql_mode_),
K(parse_result.sql_mode_),
K(parse_result.may_bool_value_),
K(ret));
int32_t error_offset = parse_result.start_col_ > 0 ? (parse_result.start_col_ - 1) : 0;
int32_t error_length = stmt.empty() ? 0 : get_well_formed_errlen(parse_result.charset_info_,
stmt.ptr() + error_offset,
stmt.length() - error_offset);
if (OB_ERR_PARSE_SQL == ret) {
LOG_USER_ERROR(OB_ERR_PARSE_SQL, ob_errpkt_strerror(OB_ERR_PARSER_SYNTAX, false),
error_length,
stmt.empty() ? NULL : stmt.ptr() + error_offset,
parse_result.line_ + 1);
} else {
// other errors handle outer side.
}
}
return ret;
}
int ObParser::parse(const ObString &query,
ParseResult &parse_result,
ParseMode parse_mode,
const bool is_batched_multi_stmt_split_on,
const bool no_throw_parser_error,
const bool is_pl_inner_parse,
const bool is_dbms_sql,
const bool is_parse_dynamic_sql)
{
int ret = OB_SUCCESS;
// Delete spaces at the end of the SQL statement
int64_t len = query.length();
while (len > 0 && ISSPACE(query[len - 1])) {
--len;
}
// For compatibility with mysql, after removing trailing spaces from the query sql, the last character can be removed if it is '\0', and only one '\0' can be removed.
// If it is MULTI_MODE, then the operation to remove the trailing '\0' has already been done
if (MULTI_MODE != parse_mode) {
// Remove the last '\0'
if (len > 0 && '\0' == query[len - 1]) {
--len;
}
// Remove trailing spaces
while (len > 0 && ISSPACE(query[len - 1])) {
--len;
}
}
ObString stmt(len, query.ptr());
memset(&parse_result, 0, sizeof(ParseResult));
parse_result.is_multi_values_parser_ = (INS_MULTI_VALUES == parse_mode);
parse_result.is_fp_ = (FP_MODE == parse_mode
|| FP_PARAMERIZE_AND_FILTER_HINT_MODE == parse_mode
|| FP_NO_PARAMERIZE_AND_FILTER_HINT_MODE== parse_mode);
parse_result.is_multi_query_ = (MULTI_MODE == parse_mode);
parse_result.is_ignore_hint_ = (FP_PARAMERIZE_AND_FILTER_HINT_MODE == parse_mode
|| FP_NO_PARAMERIZE_AND_FILTER_HINT_MODE == parse_mode);
parse_result.is_for_trigger_ = (TRIGGER_MODE == parse_mode);
parse_result.is_dynamic_sql_ = (DYNAMIC_SQL_MODE == parse_mode);
parse_result.is_dbms_sql_ = (DBMS_SQL_MODE == parse_mode) || is_dbms_sql;
parse_result.is_for_udr_ = (UDR_SQL_MODE == parse_mode);
parse_result.is_batched_multi_enabled_split_ = is_batched_multi_stmt_split_on;
parse_result.is_not_utf8_connection_ = ObCharset::is_valid_collation(charsets4parser_.string_collation_) ?
(ObCharset::charset_type_by_coll(charsets4parser_.string_collation_) != CHARSET_UTF8MB4) : false;
parse_result.malloc_pool_ = allocator_;
parse_result.semicolon_start_col_ = INT32_MAX;
parse_result.sql_mode_ = sql_mode_ & (~SMO_ORACLE);