forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql_parser_base.h
More file actions
1393 lines (1305 loc) · 83.5 KB
/
Copy pathsql_parser_base.h
File metadata and controls
1393 lines (1305 loc) · 83.5 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.
*/
#ifndef OCEANBASE_SRC_SQL_PARSER_SQL_PARSER_BASE_H_
#define OCEANBASE_SRC_SQL_PARSER_SQL_PARSER_BASE_H_
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "lib/ob_date_unit_type.h"
#include "share/schema/ob_priv_type.h"
#include "sql/ob_trans_character.h"
#include "parse_node.h"
#include "parse_malloc.h"
#include "ob_non_reserved_keywords.h"
#include "parse_define.h"
#include "ob_parser_charset_utils.h"
#define MAX_VARCHAR_LENGTH 4194303
#define INT16NUM_OVERFLOW INT16_MAX
#define OUT_OF_STR_LEN -2
#define DEFAULT_STR_LENGTH -1
#define BINARY_COLLATION 63 /* Need refactoring, change to c++ parser to avoid duplicate definitions */
#define OB_STRXFRM_NLEVELS 6
#define OB_STRXFRM_DESC_SHIFT 8
#define OB_STRXFRM_REVERSE_SHIFT 16
#define OB_STRXFRM_PAD_WITH_SPACE 0x00000040
#define INVALID_COLLATION 0
#define INVALID_INDEX -1
#define PACKAGE_KEY_PREFIX_V1 "pkg."
#define PACKAGE_KEY_PREFIX_V2 "pkg.v2."
#define YYLEX_PARAM result->yyscan_info_
#define JOIN_MERGE_NODES(node1, node2) \
do { \
if (T_LINK_NODE == node1->type_) { \
merge_nodes(node1, result, T_TABLE_REFERENCES, node1); \
} \
if (T_LINK_NODE == node2->type_) { \
merge_nodes(node2, result, T_TABLE_REFERENCES, node2); \
} \
} while(0)
extern void yyerror(void *yylloc, ParseResult *p, char *s,...);
extern ParseNode *merge_tree(void *malloc_pool, int *fatal_error, ObItemType node_tag, ParseNode *source_tree);
extern ParseNode *new_terminal_node(void *malloc_pool, ObItemType type);
extern ParseNode *new_non_terminal_node(void *malloc_pool, ObItemType node_tag, int num, ...);
extern char *copy_expr_string(ParseResult *p, int expr_start, int expr_end);
extern int64_t ob_strntoll(const char *ptr, size_t len, int base, char **end, int *err);
extern int64_t ob_strntoull(const char *ptr, size_t len, int base, char **end, int *err);
extern int store_prentthese_info(int left, int right, ParseResult *result);
extern bool check_real_escape(const struct ObCharsetInfo *cs, char *str, int64_t str_len,
int64_t last_escape_check_pos);
int add_alias_name(ParseNode *node, ParseResult *result, int end);
#define ISSPACE(c) ((c) == ' ' || (c) == '\n' || (c) == '\r' || (c) == '\t' || (c) == '\f' || (c) == '\v')
#define YYABORT_WITH_ERROR(err_code) \
do { \
if (OB_UNLIKELY(NULL == result)) { \
(void)fprintf(stderr, "ERROR : result is NULL\n"); \
} else if (OB_PARSER_SUCCESS == result->extra_errno_) { \
result->extra_errno_ = err_code; \
} else {/*do nothing*/} \
YYABORT; \
} while(0)
#define YYABORT_NO_MEMORY YYABORT_WITH_ERROR(OB_PARSER_ERR_NO_MEMORY)
#define YYABORT_UNEXPECTED YYABORT_WITH_ERROR(OB_PARSER_ERR_UNEXPECTED)
#define YYABORT_TOO_BIG_DISPLAYWIDTH YYABORT_WITH_ERROR(OB_PARSER_ERR_TOO_BIG_DISPLAYWIDTH)
#define YYABORT_UNDECLARE_VAR YYABORT_WITH_ERROR(OB_PARSER_ERR_UNDECLARED_VAR)
#define YYABORT_NOT_VALID_ROUTINE_NAME YYABORT_WITH_ERROR(OB_PARSER_ERR_NOT_VALID_ROUTINE_NAME)
#define YYABORT_STRING_LITERAL_TOO_LONG(result) \
do { \
if (OB_UNLIKELY(NULL == result)) { \
(void)fprintf(stderr, "ERROR : result is NULL\n"); \
} else if (OB_PARSER_SUCCESS == result->extra_errno_) { \
result->extra_errno_ = OB_PARSER_ERR_STR_LITERAL_TOO_LONG;\
} else {/*do nothing*/} \
yyerror(yylloc, yyextra, "string literal is too long\n", yytext); \
return ERROR; \
} while(0)
#define YYABORT_PARSE_SQL_ERROR YYERROR
#define check_malloc(val_ptr) \
do { \
if (OB_UNLIKELY(NULL == val_ptr)) \
{ \
YY_FATAL_ERROR("No more space for malloc, start_col:%d, end_col:%d, line:%d", \
((ParseResult *)yyextra)->start_col_, \
((ParseResult *)yyextra)->end_col_, \
((ParseResult *)yyextra)->line_); \
} \
} while (0);
#define malloc_terminal_node(node, malloc_pool, type) \
do { \
if (OB_UNLIKELY(NULL == (node = new_terminal_node(malloc_pool, type)))) { \
yyerror(NULL, result, "No more space for malloc\n"); \
YYABORT_NO_MEMORY; \
} \
} while(0)
#define malloc_non_terminal_node(node, malloc_pool, node_tag, ...) \
do { \
if (OB_UNLIKELY(NULL == (node = new_non_terminal_node(malloc_pool, node_tag, ##__VA_ARGS__)))) {\
yyerror(NULL, result, "No more space for malloc\n"); \
YYABORT_NO_MEMORY; \
} \
} while(0)
#define malloc_list_node(node, malloc_pool, node_tag, ...) \
do { \
if (OB_UNLIKELY(NULL == (node = new_list_node(malloc_pool, node_tag, ##__VA_ARGS__)))) {\
yyerror(NULL, result, "No more space for malloc\n"); \
YYABORT_NO_MEMORY; \
} \
} while(0)
#define merge_nodes(node, result, node_tag, source_tree) \
do { \
if (OB_UNLIKELY(NULL == source_tree)) { \
node = NULL; \
} else if (OB_UNLIKELY(NULL == (node = merge_tree(result->malloc_pool_, &(result->extra_errno_), node_tag, source_tree)))) { \
yyerror(NULL, result, "No more space for merging nodes\n"); \
YYABORT_NO_MEMORY; \
} \
} while (0)
#define dup_expr_string(node, result, expr_start, expr_end) \
do { \
if (OB_UNLIKELY(NULL == node || NULL == result || NULL == result->input_sql_)) {\
yyerror(NULL, result, "invalid argument, node:%p, result:%p or input_sql is NULL\n", node, result);\
YYABORT_UNEXPECTED; \
} else if (OB_UNLIKELY(expr_start < 0 || expr_end < 0 || expr_start > expr_end)) { \
yyerror(NULL, result, "invalid argument, expr_start:%d, expr_end:%d\n", (int32_t)expr_start, (int32_t)expr_end);\
YYABORT_UNEXPECTED; \
} else { \
int start = expr_start; \
node->str_value_ = NULL; \
node->str_len_ = 0; \
while (start <= expr_end && ISSPACE(result->input_sql_[start - 1])) {\
start++; \
} \
if (start >= expr_start \
&& (OB_UNLIKELY((NULL == (node->str_value_ = copy_expr_string(result, start, expr_end)))))) { \
yyerror(NULL, result, "No more space for copying expression string\n"); \
YYABORT_NO_MEMORY; \
} else { \
node->str_len_ = expr_end - start + 1; \
} \
} \
} while (0)
#define dup_string(node, result, start, end) \
if (start > end \
|| (OB_UNLIKELY((NULL == (node->str_value_ = copy_expr_string(result, start, end)))))) { \
yyerror(NULL, result, "No more space for copying expression string\n"); \
YYABORT_NO_MEMORY; \
} else { \
node->str_len_ = end - start + 1; \
} \
#define dup_modify_key_string(node, result, expr_start, expr_end, prefix) \
do { \
if (OB_UNLIKELY(NULL == node || NULL == result || NULL == result->input_sql_)) {\
yyerror(NULL, result, "invalid argument, node:%p, result:%p or input_sql is NULL\n", node, result);\
YYABORT_UNEXPECTED; \
} else if (OB_UNLIKELY(expr_start < 0 || expr_end < 0 || expr_start > expr_end)) { \
yyerror(NULL, result, "invalid argument, expr_start:%d, expr_end:%d\n", (int32_t)expr_start, (int32_t)expr_end);\
YYABORT_UNEXPECTED; \
} else { \
int start = expr_start; \
node->str_value_ = NULL; \
node->str_len_ = 0; \
while (start <= expr_end && ISSPACE(result->input_sql_[start - 1])) {\
start++; \
} \
int len = expr_end - start + sizeof(prefix); \
char *expr_string = (char *)parse_malloc(len + 1, result->malloc_pool_); \
if (OB_UNLIKELY(NULL == expr_string)) { \
yyerror(NULL, result, "No more space for copying expression string\n"); \
YYABORT_NO_MEMORY; \
} else { \
memmove(expr_string, (prefix), strlen(prefix)); \
memmove(expr_string+strlen(prefix), result->input_sql_ + start - 1, expr_end - start + 1); \
expr_string[len] = '\0'; \
node->str_value_ = expr_string; \
node->str_len_ = len; \
} \
} \
} while (0)
#define get_non_reserved_node(node, malloc_pool, word_start, word_end) \
do { \
malloc_terminal_node(node, malloc_pool, T_IDENT); \
dup_expr_string(node, result, word_start, word_end); \
setup_token_pos_info(node, word_start - 1, word_end - word_start + 1); \
} while (0)
#define gen_expr_node(node, name, len, p, off) \
do { \
char *src = (char*)parse_malloc(len + 1, p->malloc_pool_); \
check_malloc(src); \
memmove(src, name, len); \
src[len] = '\0'; \
const NonReservedKeyword *word = NULL; \
if (NULL == (word = mysql_non_reserved_keyword_lookup(src))) \
{ \
if (p->is_not_utf8_connection_) { \
node->str_value_ = parse_str_convert_utf8(p->charset_info_, src, p->malloc_pool_, &(node->str_len_), &(p->extra_errno_)); \
check_identifier_convert_result(p->extra_errno_); \
} else { \
node->str_value_ = parse_strdup(src, p->malloc_pool_, &(node->str_len_)); \
} \
} else { \
node->str_value_ = parse_strndup(src, len, p->malloc_pool_); \
node->str_len_ = len; \
} \
check_malloc(node->str_value_); \
node->sql_str_off_ = off; \
setup_token_pos_info(node, off, node->str_len_); \
} while (0)
//oracle under generate non-reserved keyword node please use this macro, distinguish from mysql is done uppercase conversion
#define get_oracle_non_reserved_node(node, malloc_pool, expr_start, expr_end) \
do { \
malloc_terminal_node(node, malloc_pool, T_IDENT); \
if (OB_UNLIKELY(NULL == node || NULL == result || NULL == result->input_sql_)) {\
yyerror(NULL, result, "invalid argument, node:%p, result:%p or input_sql is NULL", node, result);\
YYABORT_UNEXPECTED; \
} else if (OB_UNLIKELY(expr_start < 0 || expr_end < 0 || expr_start > expr_end)) { \
yyerror(NULL, result, "invalid argument, expr_start:%d, expr_end:%d", (int32_t)expr_start, (int32_t)expr_end);\
YYABORT_UNEXPECTED; \
} else { \
int start = expr_start; \
char * upper_value = NULL; \
char * raw_str = NULL; \
node->str_value_ = NULL; \
node->str_len_ = 0; \
node->raw_text_ = NULL; \
node->text_len_ = 0; \
while (start <= expr_end && ISSPACE(result->input_sql_[start - 1])) {\
start++; \
} \
if ('"' == result->input_sql_[start - 1]) { \
start++; \
expr_end--; \
} \
if (start >= expr_start \
&& (OB_UNLIKELY((NULL == (upper_value = copy_expr_string(result, start, expr_end)))))) { \
yyerror(NULL, result, "No more space for copying expression string"); \
YYABORT_NO_MEMORY; \
} else { \
if (start >= expr_start \
&& (OB_UNLIKELY((NULL == (raw_str = copy_expr_string(result, start, expr_end)))))) { \
yyerror(NULL, result, "No more space for copying expression string"); \
YYABORT_NO_MEMORY; \
} else { \
node->raw_text_ = raw_str; \
node->text_len_ = expr_end - start + 1; \
node->str_value_ = str_toupper(upper_value, expr_end - start + 1); \
node->str_len_ = expr_end - start + 1; \
setup_token_pos_info(node, expr_start - 1, expr_end - expr_start + 1); \
} \
} \
} \
} while(0)
#define make_name_node(node, malloc_pool, name) \
do { \
malloc_terminal_node(node, malloc_pool, T_IDENT); \
node->str_value_ = parse_strdup(name, malloc_pool, &(node->str_len_));\
if (OB_UNLIKELY(NULL == node->str_value_)) { \
yyerror(NULL, result, "No more space for string duplicate\n"); \
YYABORT_NO_MEMORY; \
} \
} while (0)
#define dup_string_to_node(node, malloc_pool, name) \
do { \
if (OB_UNLIKELY(NULL == node)) { \
yyerror(NULL, result, "invalid arguments node: %p\n", node); \
YYABORT_UNEXPECTED; \
} else { \
node->str_value_ = parse_strdup(name, malloc_pool, &(node->str_len_));\
if (OB_UNLIKELY(NULL == node->str_value_)) { \
yyerror(NULL, result, "No more space for string duplicate\n"); \
YYABORT_NO_MEMORY; \
} \
} \
} while (0)
#define dup_node_string(node_src, node_dest, malloc_pool) \
do { \
if (OB_UNLIKELY((NULL == node_src || NULL == node_dest || NULL == node_src->str_value_))) {\
yyerror(NULL, result, "invalid arguments node_src: %p, node_dest: %p\n", node_src, node_dest); \
YYABORT_UNEXPECTED; \
} else { \
node_dest->str_value_ = parse_strndup(node_src->str_value_, node_src->str_len_, malloc_pool); \
if (OB_UNLIKELY(NULL == node_dest->str_value_)) { \
yyerror(NULL, result, "No more space for dup_node_string\n"); \
YYABORT_NO_MEMORY; \
} else { \
node_dest->str_len_ = node_src->str_len_; \
} \
} \
} while (0)
/* to minimize modification, we use stmt->value_ as question mark size */
#define question_mark_issue(node, result) \
do { \
if (OB_UNLIKELY(NULL == node || NULL == result)) { \
yyerror(NULL, result, "node or result is NULL\n"); \
YYABORT_UNEXPECTED; \
} else if (OB_UNLIKELY(INT64_MAX != node->value_)) { \
yyerror(NULL, result, "node value is not INT64_MAX\n"); \
YYABORT_UNEXPECTED; \
} else { \
node->value_ = result->question_mark_ctx_.count_; \
/* To handle ps + anonymous cancel setting question_mark_size to 0*/ \
/* with prefix as 0, is for handling multi stmt in 052 */ \
/* result->question_mark_size_ = 0; */ \
} \
} while (0)
#define check_question_mark(node, result) \
do { \
if (OB_UNLIKELY(NULL == node || NULL == result)) { \
yyerror(NULL, result, "node or result is NULL\n"); \
YYABORT_UNEXPECTED; \
} else if (OB_UNLIKELY(!result->pl_parse_info_.is_pl_parse_ && 0 != result->question_mark_ctx_.count_)) { \
/* If the SQL statement is from PL, do not check: */ \
yyerror(NULL, result, "Unknown column '?'\n"); \
YYABORT_PARSE_SQL_ERROR; \
} else { \
node->value_ = result->question_mark_ctx_.count_; \
} \
} while (0)
// Store a PL variable in the linked list
#define store_pl_symbol(node, head, tail) \
do { \
ParamList *param = (ParamList *)parse_malloc(sizeof(ParamList), result->malloc_pool_); \
if (OB_UNLIKELY(NULL == param)) { \
yyerror(NULL, result, "No more space for alloc ParamList\n"); \
YYABORT_NO_MEMORY; \
} else { \
param->node_ = node; \
param->next_ = NULL; \
if (NULL == head) { \
head = param; \
} else { \
tail->next_ = param; \
} \
tail = param; \
} \
} while (0)
/*
* copy current sql statement to the current QUESTIONMARK position, and replace that QUESTIONMARK with :idx QUESTIONMARK form
* When parsing the entire PL, when parsing one of the sql statements, it will go through here, so need to judge is_pl_parse_ and NULL == pl_ns_
* It may also go through here when preparing dynamic sql for Oracle mode
* The ? appearing in the sql of anonymous blocks in Oracle mode needs to be numbered as a whole in the PL, so this action is required, mysql does not need it
* During the replacement process, there may be insufficient Buffer space, if space is insufficient then expand the Buffer
*/
#define copy_and_replace_questionmark(result, start, end, idx) \
do { \
if (NULL == result) { \
YY_UNEXPECTED_ERROR("invalid var node\n"); \
} else if (result->is_fp_) { \
/* do nothing */ \
} else if ((result->pl_parse_info_.is_pl_parse_ && NULL == result->pl_parse_info_.pl_ns_) \
|| result->is_dynamic_sql_) { \
if (result->no_param_sql_len_ + (start - result->pl_parse_info_.last_pl_symbol_pos_ - 1) \
+ (int)(log10(idx)) + 3 \
> result->no_param_sql_buf_len_) { \
char *buf = parse_malloc(result->no_param_sql_buf_len_ * 2, result->malloc_pool_); \
if (OB_UNLIKELY(NULL == buf)) { \
YY_FATAL_ERROR("no memory to alloc\n"); \
} else { \
memmove(buf, result->no_param_sql_, result->no_param_sql_len_); \
result->no_param_sql_ = buf; \
result->no_param_sql_buf_len_ = result->no_param_sql_buf_len_ * 2; \
} \
} \
memmove(result->no_param_sql_ + result->no_param_sql_len_, \
result->input_sql_ + result->pl_parse_info_.last_pl_symbol_pos_, \
start - result->pl_parse_info_.last_pl_symbol_pos_ - 1); \
result->no_param_sql_len_ += start - result->pl_parse_info_.last_pl_symbol_pos_ - 1; \
result->pl_parse_info_.last_pl_symbol_pos_ = end; \
result->no_param_sql_[result->no_param_sql_len_++] = ':'; \
result->no_param_sql_len_ += sprintf(result->no_param_sql_ + result->no_param_sql_len_, "%ld", idx); \
} \
} while (0)
#define check_need_malloc(result, need_len) \
do { \
if (result->no_param_sql_len_ + need_len >= result->no_param_sql_buf_len_) { \
char *buf = parse_malloc(result->no_param_sql_buf_len_ * 2, result->malloc_pool_); \
if (OB_UNLIKELY(NULL == buf)) { \
yyerror(NULL, result, "fail to malloc\n"); \
YYABORT_NO_MEMORY; \
} else { \
memmove(buf, result->no_param_sql_, result->no_param_sql_len_); \
result->no_param_sql_ = buf; \
result->no_param_sql_buf_len_ = result->no_param_sql_buf_len_ * 2; \
} \
} \
} while (0)
// copy current SQL statement to current symbol position, and skip this variable
#define copy_and_skip_symbol(result, start, end) \
do { \
if (NULL == result) { \
yyerror(NULL, result, "invalid var node\n"); \
YYABORT_UNEXPECTED; \
} else if (NULL == result->pl_parse_info_.pl_ns_) { \
} else { \
check_need_malloc(result, start - result->pl_parse_info_.last_pl_symbol_pos_ - 1); \
memmove(result->no_param_sql_ + result->no_param_sql_len_, result->input_sql_ + result->pl_parse_info_.last_pl_symbol_pos_, start - result->pl_parse_info_.last_pl_symbol_pos_ - 1); \
result->no_param_sql_len_ += start - result->pl_parse_info_.last_pl_symbol_pos_ - 1; \
result->pl_parse_info_.last_pl_symbol_pos_ = end; \
} \
} while (0)
// Find the pl variable, and replace that variable with :int form
#define lookup_pl_exec_symbol(node, result, start, end, is_trigger_new, is_add_alas_name, is_report_error) \
do { \
if (OB_UNLIKELY((NULL == node || NULL == result || NULL == node->str_value_))) { \
yyerror(NULL, result, "invalid var node: %p\n", node); \
YYABORT_UNEXPECTED; \
} else if (NULL == result->pl_parse_info_.pl_ns_) { \
} else if (is_trigger_new) { \
copy_and_skip_symbol(result, start, end); \
check_need_malloc(result, 2 + node->children_[1]->str_len_ + node->children_[2]->str_len_); \
result->no_param_sql_[result->no_param_sql_len_++] = ':'; \
result->no_param_sql_len_ += sprintf(result->no_param_sql_ + result->no_param_sql_len_, "%.*s", (int)node->children_[1]->str_len_, node->children_[1]->str_value_); \
result->no_param_sql_[result->no_param_sql_len_++] = '.'; \
result->no_param_sql_len_ += sprintf(result->no_param_sql_ + result->no_param_sql_len_, "%.*s", (int)node->children_[2]->str_len_, node->children_[2]->str_value_); \
store_pl_symbol(node, result->param_nodes_, result->tail_param_node_); \
} else if (is_add_alas_name) { \
int64_t idx = INVALID_INDEX; \
int lookup_pl_symbol_ret = OB_PARSER_SUCCESS; \
if (NULL != node->children_[0] && T_COLUMN_REF == node->children_[0]->type_ && OB_UNLIKELY(0 != (lookup_pl_symbol_ret = lookup_pl_symbol(result->pl_parse_info_.pl_ns_, node->str_value_, node->str_len_, &idx)))) { \
yyerror(NULL, result, "failed to lookup pl symbol\n"); \
YYABORT_WITH_ERROR(lookup_pl_symbol_ret); \
} else if (NULL != node->children_[0] && T_COLUMN_REF == node->children_[0]->type_ && INVALID_INDEX == idx) { \
/*do nothing*/ \
} else {\
int ret = 0; \
if (0 == (ret = add_alias_name(node, result, end))) { \
} else if (OB_PARSER_ERR_NO_MEMORY == ret) { \
yyerror(NULL, result, "fail to malloc\n"); \
YYABORT_NO_MEMORY; \
} else { \
yyerror(NULL, result, "failed to lookup pl symbol\n"); \
YYABORT_UNEXPECTED; \
} \
} \
} \
else { \
int64_t idx = INVALID_INDEX; \
int lookup_pl_symbol_ret = OB_PARSER_SUCCESS; \
if (OB_UNLIKELY(0 != (lookup_pl_symbol_ret = lookup_pl_symbol(result->pl_parse_info_.pl_ns_, node->str_value_, node->str_len_, &idx)))) { \
yyerror(NULL, result, "failed to lookup pl symbol\n"); \
YYABORT_WITH_ERROR(lookup_pl_symbol_ret); \
} else if (INVALID_INDEX != idx) { \
copy_and_skip_symbol(result, start, end); \
check_need_malloc(result, 21); \
result->no_param_sql_[result->no_param_sql_len_++] = ':'; \
result->no_param_sql_len_ += sprintf(result->no_param_sql_ + result->no_param_sql_len_, "%ld", idx); \
store_pl_symbol(node, result->param_nodes_, result->tail_param_node_); \
} else if (is_report_error) { \
YYABORT_UNDECLARE_VAR; \
} else { /*do nothing*/ } \
} \
} while (0)
// Store dependency object to dependency object list
#define store_pl_ref_object_symbol(node, result, type) \
do { \
if (OB_UNLIKELY((NULL == node || NULL == result))) { \
yyerror(NULL, result, "invalid var node: %p\n", node); \
YYABORT_UNEXPECTED; \
} else if (NULL == result->pl_parse_info_.pl_ns_) { \
} else { \
RefObjList *object = (RefObjList *)parse_malloc(sizeof(RefObjList), result->malloc_pool_); \
if (OB_UNLIKELY(NULL == object)) { \
yyerror(NULL, result, "No more space for alloc ParamList\n"); \
YYABORT_NO_MEMORY; \
} else { \
object->type_ = type; \
object->node_ = node; \
object->next_ = NULL; \
if (NULL == result->pl_parse_info_.ref_object_nodes_) { \
result->pl_parse_info_.ref_object_nodes_ = object; \
} else { \
result->pl_parse_info_.tail_ref_object_node_->next_ = object; \
} \
result->pl_parse_info_.tail_ref_object_node_ = object; \
} \
} \
} while (0)
#define process_placeholder_for_dynamic \
do { \
if (p->pl_parse_info_.is_pl_parse_) { \
yylval->node->value_ = get_question_mark(&p->question_mark_ctx_, p->malloc_pool_, yytext); \
} else { \
yylval->node->value_ = p->question_mark_ctx_.count_++; \
} \
} while (0)
//////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
#define YY_USER_ACTION \
do { \
check_value(yylloc); \
ParseResult *p = (ParseResult *)yyextra; \
yylloc->first_line = yylloc->last_line = yylineno; \
yylloc->first_column = p->yycolumn_; \
yylloc->last_column = yylloc->first_column + yyleng - 1; \
p->yycolumn_ += yyleng; \
} while(0);
extern ParseNode *new_node(void *malloc_pool, ObItemType type, int num);
#define malloc_new_node(node, malloc_pool, type, num) \
do { \
if (OB_UNLIKELY(NULL == (node = new_node(malloc_pool, type, num)))) { \
YY_FATAL_ERROR("No more space for mallocing '%s'\n", yytext); \
} \
} while (0);
#define malloc_time_node(malloc_pool, type, find_char) \
do { \
ParseNode *node = NULL; \
malloc_new_node(node, malloc_pool, type, 0); \
char *begin = strchr(yytext, find_char); \
check_value(begin); \
char *end = strchr(begin + 1, find_char); \
check_value(end); \
char *dest = NULL; \
size_t len = end - begin - 1; \
dest = parse_strndup(begin + 1, len, malloc_pool); \
check_malloc(dest); \
node->str_value_ = dest; \
node->str_len_ = len; \
check_value(yylval); \
yylval->node = node; \
} while (0);
#define malloc_time_node_s(malloc_pool, type) malloc_time_node(malloc_pool, type, '\'');
#define malloc_time_node_d(malloc_pool, type) malloc_time_node(malloc_pool, type, '\"');
// special case json object ( key :ident/intnum)
#define malloc_object_key_node(malloc_pool) \
do { \
ParseNode *key_node = NULL; \
malloc_new_node(key_node, malloc_pool, T_CHAR, 0); \
char *begin_k = strchr(yytext, '\''); \
check_value(begin_k); \
char *end_k = strchr(begin_k + 1, '\''); \
check_value(end_k); \
char *dest = NULL; \
size_t len = end_k - begin_k - 1; \
dest = parse_strndup(begin_k + 1, len, malloc_pool); \
check_malloc(dest); \
key_node->str_value_ = dest; \
key_node->str_len_ = len; \
char *raw_dest = NULL; \
raw_dest = parse_strndup(begin_k + 1, len, malloc_pool); \
check_malloc(raw_dest); \
key_node->str_value_ = raw_dest; \
key_node->str_len_ = len; \
ParseNode *object_node = NULL; \
malloc_new_node(object_node, malloc_pool, T_LINK_NODE, 2); \
object_node->children_[0] = key_node; \
check_value(yylval); \
yylval->node = object_node; \
} while (0);
#define malloc_key_colon_ident_value_node(malloc_pool) \
do { \
malloc_object_key_node(malloc_pool); \
ParseNode *value_node = NULL; \
malloc_new_node(value_node, malloc_pool, T_IDENT, 0); \
char *begin_v = strchr(yytext, ':'); \
check_value(begin_v); \
size_t end_v = strlen(begin_v); \
size_t len_v = end_v - 1; \
char *dest_v = NULL; \
dest_v = parse_strndup(begin_v + 1, len_v, malloc_pool); \
check_malloc(dest_v); \
value_node->str_len_ = len_v; \
value_node->str_value_ = dest_v; \
yylval->node->children_[1] = value_node; \
check_value(yylval); \
} while (0);
#define malloc_key_colon_intnum_value_node(malloc_pool) \
do { \
malloc_object_key_node(malloc_pool); \
ParseNode *value_node = NULL; \
malloc_new_node(value_node, malloc_pool, T_INT, 0); \
char *begin_v = strchr(yytext, ':'); \
check_value(begin_v); \
size_t end_v = strlen(begin_v); \
size_t len_v = end_v - 1; \
char *dest_v = NULL; \
dest_v = parse_strndup(begin_v + 1, len_v, malloc_pool); \
check_malloc(dest_v); \
value_node->str_len_ = len_v; \
value_node->str_value_ = dest_v; \
yylval->node->children_[1] = value_node; \
check_value(yylval); \
} while (0);
#define check_value(val_ptr) \
do { \
if (OB_UNLIKELY(NULL == val_ptr)) \
{ \
((ParseResult *)yyextra)->extra_errno_ = OB_PARSER_ERR_UNEXPECTED; \
yyerror(yylloc, yyextra, "'%s' is NULL\n", #val_ptr); \
return ERROR; \
} \
} while (0);
#define check_identifier_convert_result(errno) \
do { \
if (OB_PARSER_ERR_ILLEGAL_NAME == errno) { \
yyerror(yylloc, yyextra, "name '%s' is illegal\n", yytext); \
return ERROR; \
} \
} while (0);
#define check_parser_size_overflow(errno) \
do { \
if (OB_PARSER_ERR_SIZE_OVERFLOW == errno) { \
yyerror(NULL, result, "stack overflow in parser\n"); \
YYABORT_PARSE_SQL_ERROR; \
} \
} while (0);
#define IS_FAST_PARAMETERIZE ((ParseResult *)yyextra)->is_fp_
#define IS_NEED_PARAMETERIZE ((ParseResult *)yyextra)->need_parameterize_
#define IS_FOR_TRIGGER ((ParseResult *)yyextra)->is_for_trigger_
#define IF_FOR_PREPROCESS ((ParseResult *)yyextra)->is_for_preprocess_
#define IS_FOR_REMAP ((ParseResult *)yyextra)->is_for_remap_
#define COPY_STRING(src, src_len, dst) \
do { \
if (IS_FAST_PARAMETERIZE && IS_NEED_PARAMETERIZE) { \
dst = src; \
} else { \
ParseResult *p = (ParseResult *)yyextra; \
dst = parse_strndup(src, src_len, p->malloc_pool_); \
check_malloc(dst); \
} \
} while (0);
#define COPY_STR_NODE_TO_TMP_LITERAL(str_node) \
do { \
ParseResult *p = (ParseResult *)yyextra; \
char **tmp_literal = &(p->tmp_literal_); \
/*str node encountered special characters, need to do escaping, need to create a temporary buffer, and copy the previously stored text to the buffer*/ \
if (NULL == *tmp_literal) { \
*tmp_literal = (char*) parse_malloc(p->input_sql_len_ + 1, p->malloc_pool_); \
check_malloc(*tmp_literal); \
} \
if (str_node->str_value_ != NULL) { \
memmove(((ParseResult *)yyextra)->tmp_literal_, str_node->str_value_, str_node->str_len_); \
str_node->str_value_ = NULL; \
} \
} while (0);
#define STORE_STR_CONTENT(str_node) \
do { \
ParseResult *p = (ParseResult *)yyextra; \
/*If it's the first time fast parser stores the value of str node, deep copy is not needed, a pointer to the sql text can be used directly, deep copy will be done next time a special character is encountered*/ \
if (str_node->str_len_ <= 0 && IS_FAST_PARAMETERIZE && IS_NEED_PARAMETERIZE) { \
str_node->str_value_ = p->input_sql_ + yylloc->first_column - 1; \
str_node->str_len_ = yyleng; \
} else { \
char **tmp_literal = &(p->tmp_literal_); \
if (NULL == *tmp_literal) { \
*tmp_literal = (char*) parse_malloc(p->input_sql_len_ + 1, p->malloc_pool_); \
check_malloc(*tmp_literal); \
} \
memmove(*tmp_literal + str_node->str_len_, yytext, yyleng); \
str_node->str_len_ += yyleng; \
} \
} while (0);
#define FORMAT_STR_NODE(str_node) \
do { \
ParseResult *p = (ParseResult *)yyextra; \
if (str_node->str_value_ == NULL && str_node->str_len_ > 0) { \
char *tmp_literal = p->tmp_literal_; \
tmp_literal[yylval->node->str_len_] = '\0'; \
str_node->str_value_ = parse_strndup(tmp_literal, str_node->str_len_ + 1, p->malloc_pool_); \
check_malloc(str_node->str_value_); \
} \
} while (0);
#define COPY_WRITE() \
do { \
ParseResult *p = (ParseResult *)yyextra; \
memmove(p->no_param_sql_ + p->no_param_sql_len_, yytext, yyleng); \
p->no_param_sql_len_ += yyleng; \
p->no_param_sql_[p->no_param_sql_len_] = '\0'; \
} while (0);
#define STORE_PARAM_NODE() \
do { \
ParseResult *p = (ParseResult *)yyextra; \
if (p->need_parameterize_) { \
check_value(yylval); \
check_value(yylval->node); \
size_t alloc_len = sizeof(ParamList); \
ParamList *param = (ParamList *)parse_malloc(alloc_len, p->malloc_pool_); \
check_malloc(param); \
STORE_PARAM_NODE_NEED_PARAMETERIZE(param, yylval->node, p, yylloc->first_column); \
} else { \
return OUTLINE_DEFAULT_TOKEN; \
} \
} while(0);
#define STORE_UNIT_TYPE_NODE(str_val) \
do { \
ParseResult *p = (ParseResult *)yyextra; \
if (p->need_parameterize_) { \
ParseNode* node = NULL; \
malloc_new_node(node, ((ParseResult *)yyextra)->malloc_pool_, T_INT, 0); \
check_value(yylval); \
yylval->node = node; \
yylval->node->raw_text_ = parse_strdup(yytext, \
((ParseResult *)yyextra)->malloc_pool_, \
&(yylval->node->text_len_)); \
check_malloc(yylval->node->raw_text_); \
node->str_value_ = parse_strdup((char *)str_val, \
((ParseResult*)yyextra)->malloc_pool_, \
&(node->str_len_)); \
check_malloc(node->str_value_); \
node->value_ = strtoll(node->str_value_, NULL, 10); \
STORE_PARAM_NODE() \
} else { \
return OUTLINE_DEFAULT_TOKEN; \
} \
} while(0);
#define HANDLE_UNIT_TYPE(type) \
do { \
if (IS_FAST_PARAMETERIZE) { \
ParseResult *p = (ParseResult *)yyextra; \
if (p->need_parameterize_) { \
STORE_UNIT_TYPE_NODE(ob_date_unit_type_num_str(DATE_UNIT_##type)); \
} else { \
return OUTLINE_DEFAULT_TOKEN; \
} \
} else { \
return type; \
} \
} while(0);
#define HANDLE_ESCAPE(presult) \
do { \
int with_back_slash = 1; \
unsigned char c = escaped_char(yytext[1], &with_back_slash); \
if (with_back_slash) \
{ \
presult->tmp_literal_[yylval->node->str_len_++] = '\\'; \
} \
presult->tmp_literal_[yylval->node->str_len_++] = c; \
} while(0);
/*
* When the escape character is a false escape, '\' is actually part of the previous character, it needs to be added to literal,
* call yyless, roll back the characters after '\' to the input stream for re-matching,
* because one extra character was parsed, yycolumn_ needs to be decremented by one,
* to ensure that YY_USER_ACTION can calculate the correct column position based on yycolumn_
*/
#define HANDLE_FALSE_ESCAPE(presult) \
do { \
presult->tmp_literal_[yylval->node->str_len_++] = '\\'; \
yyless(1); \
presult->yycolumn_--; \
} while(0);
/*
* Character sets with the escape_with_backslash_is_dangerous flag, such as big5, cp932, gbk, sjis
* The escape character (0x5C) may be part of a multibyte character, requiring special judgment
*/
#define CHECK_REAL_ESCAPE(is_real_escape) \
is_real_escape = check_real_escape(p->charset_info_, p->tmp_literal_, \
yylval->node->str_len_, p->last_escape_check_pos_)
/*
do { \
if (NULL != p->charset_info_ && p->charset_info_->escape_with_backslash_is_dangerous) { \
char *cur_pos = p->tmp_literal_ + yylval->node->str_len_; \
char *last_check_pos = p->tmp_literal_ + p->last_well_formed_len_; \
int error = 0; \
int expected_well_formed_len = cur_pos - last_check_pos; \
int real_well_formed_len = p->charset_info_->cset->well_formed_len( \
p->charset_info_, last_check_pos, cur_pos, UINT64_MAX, &error); \
if (error != 0) { \
*cur_pos = '\\'; \
if (real_well_formed_len == expected_well_formed_len - 1 \
&& p->charset_info_->cset->ismbchar(p->charset_info_, cur_pos - 1, cur_pos + 1)) { \
is_real_escape = false; \
p->last_well_formed_len_ = yylval->node->str_len_ + 1; \
} else { \
p->last_well_formed_len_ = yylval->node->str_len_; \
} \
} else { \
p->last_well_formed_len_ = yylval->node->str_len_; \
} \
} \
} while(0);*/
/*
#define CHECK_NODE_STRING_VALUE_ASCII(token, my_str_ptr, my_str_len) \
do { \
if (p->charset_info_ != NULL && p->is_not_utf8_connection_) { \
if (my_str_len != p->charset_info_->cset->numchars(p->charset_info_, \
my_str_ptr, \
my_str_ptr + my_str_len)) {\
p->extra_errno_ = OB_PARSER_ERR_ILLEGAL_NAME; \
yyerror(yylloc, yyextra, "multi-byte identifier '%.*s' not support in charset '%s'\n", \
my_str_len, my_str_ptr, p->charset_info_->csname); \
token = PARSER_SYNTAX_ERROR; \
} \
} \
} while (0)
*/
#define ADD_YYLINENO(_yytext, _yyleng) \
for (int32_t _i = 0; _i < _yyleng; ++_i) { \
if (_yytext[_i] == '\n') { \
++yylineno; \
} \
}
#define COPY_NUM_STRING(parse_result, node) \
do { \
if (IS_FAST_PARAMETERIZE) { \
if (parse_result->minus_ctx_.has_minus_) { \
int64_t start_pos = parse_result->minus_ctx_.raw_sql_offset_; \
int64_t cp_len = yylloc->first_column - start_pos - 1 + yyleng; \
COPY_STRING(p->input_sql_ + start_pos, cp_len, node->str_value_); \
node->str_len_ = cp_len; \
} else { \
COPY_STRING(p->input_sql_ + yylloc->first_column - 1, yyleng, node->str_value_); \
node->str_len_ = yyleng; \
} \
} else { \
COPY_STRING(p->input_sql_ + yylloc->first_column - 1, yyleng, node->str_value_); \
node->str_len_ = yyleng; \
} \
} while (0);
#define PARSE_INT_STR_MYSQL(param_node, malloc_pool, errno) \
do { \
if ('-' == param_node->str_value_[0]) { \
char *copied_str = parse_strndup(param_node->str_value_, param_node->str_len_, malloc_pool); \
if (OB_ISNULL(copied_str)) { \
YY_FATAL_ERROR("No more space for mallocing"); \
} else { \
int pos = 1; \
for (; pos < param_node->str_len_ && ISSPACE(copied_str[pos]); pos++) ; \
copied_str[--pos] = '-'; \
param_node->value_ = ob_strntoll(copied_str + pos, param_node->str_len_ - pos, 10, NULL, &errno); \
if (ERANGE == errno) { \
param_node->type_ = T_NUMBER; \
token_ret = DECIMAL_VAL; \
} \
} \
} else { \
uint64_t value = ob_strntoull(param_node->str_value_, param_node->str_len_, 10, NULL, &errno); \
param_node->value_ = value; \
if (ERANGE == errno) { \
param_node->type_ = T_NUMBER; \
token_ret = DECIMAL_VAL; \
} else if (value > INT64_MAX) { \
param_node->type_ = T_UINT64; \
} \
} \
} while (0);
#define PARSE_INT_STR_ORACLE(param_node, malloc_pool, errno) \
do { \
if ('-' == param_node->str_value_[0]) { \
char *copied_str = parse_strndup(param_node->str_value_, param_node->str_len_, malloc_pool); \
if (OB_ISNULL(copied_str)) { \
YY_FATAL_ERROR("No more space for mallocing"); \
} else { \
int pos = 1; \
for (; pos < param_node->str_len_ && ISSPACE(copied_str[pos]); pos++) ; \
copied_str[--pos] = '-'; \
param_node->value_ = ob_strntoll(copied_str + pos, param_node->str_len_ - pos, 10, NULL, &errno); \
if (ERANGE == errno) { \
param_node->type_ = T_NUMBER; \
token_ret = DECIMAL_VAL; \
} \
} \
} else { \
param_node->value_ = ob_strntoll(param_node->str_value_, param_node->str_len_, 10, NULL, &errno); \
if (ERANGE == errno) { \
param_node->type_ = T_NUMBER; \
token_ret = DECIMAL_VAL; \
} \
} \
} while (0);
#define RM_MULTI_STMT_END_P(p) \
do \
{ \
int pos = p->no_param_sql_len_ - 1; \
for (; pos >= 0 && ISSPACE(p->no_param_sql_[pos]); --pos) \
; \
p->no_param_sql_len_ = pos + 1; \
p->no_param_sql_[p->no_param_sql_len_] = '\0'; \
p->end_col_ = p->no_param_sql_len_; \
} while (0);
#define malloc_select_node(node, malloc_pool) \
malloc_non_terminal_node(node, malloc_pool, T_SELECT, PARSE_SELECT_MAX_IDX, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, \
NULL, /* PARSE_SELECT_WITH_CHECK_OPTION */ \
NULL /* PARSE_SELECT_INTO_EXTRA */);
// only used by setup_token_pos_info_and_dup_string for now
#define check_ret(stmt, loc, extra) \
int ret = (stmt); \
if (OB_UNLIKELY(ret != OB_PARSER_SUCCESS)) { \
yyerror((loc), (extra), "setup token pos info failed ret: %d\n", ret); \
}
#define REPUT_TOKEN_NEG_SIGN(TOKEN) \
ParseResult *p = (ParseResult *)yyextra; \
REPUT_NEG_SIGN(p); \
return TOKEN;
extern void setup_token_pos_info(ParseNode *node, int off, int len);
// setup pos info and copy sql from p->input_sql_([strat, end]) to node->str_value_
extern int setup_token_pos_info_and_dup_string(ParseNode *node,
ParseResult *p,
int start,
int end);
#ifdef SQL_PARSER_COMPILATION
int add_comment_list(ParseResult *p, const TokenPosInfo *info);
#endif