File: parse_tree_column_attrs.h

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (1136 lines) | stat: -rw-r--r-- 33,413 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
/* Copyright (c) 2016, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef PARSE_TREE_COL_ATTRS_INCLUDED
#define PARSE_TREE_COL_ATTRS_INCLUDED

#include <assert.h>
#include <sys/types.h>  // ulong, uint. TODO: replace with cstdint

#include <optional>
#include <type_traits>
#include <vector>

#include "field_types.h"
#include "lex_string.h"
#include "m_ctype.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_compiler.h"

#include "my_inttypes.h"
#include "my_sys.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql/derror.h"
#include "sql/field.h"
#include "sql/gis/srid.h"
#include "sql/item.h"
#include "sql/item_timefunc.h"
#include "sql/mem_root_array.h"
#include "sql/parse_location.h"
#include "sql/parse_tree_helpers.h"  // move_cf_appliers
#include "sql/parse_tree_node_base.h"
#include "sql/parser_yystype.h"
#include "sql/sql_alter.h"
#include "sql/sql_check_constraint.h"  // Sql_check_constraint_spec
#include "sql/sql_class.h"
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_list.h"
#include "sql/sql_parse.h"
#include "sql/system_variables.h"

class String;

/**
  Parse context for column type attribute specific parse tree nodes.

  For internal use in the contextualization code.

  @ingroup ptn_column_attrs ptn_gcol_attrs
*/
struct Column_parse_context : public Parse_context {
  const bool is_generated;  ///< Owner column is a generated one.
  std::vector<CreateFieldApplier> cf_appliers;
  Column_parse_context(THD *thd_arg, Query_block *select_arg, bool is_generated)
      : Parse_context(thd_arg, select_arg), is_generated(is_generated) {}
};

/**
  Base class for all column attributes in @SQL{CREATE/ALTER TABLE}

  @ingroup ptn_column_attrs ptn_gcol_attrs
*/
class PT_column_attr_base : public Parse_tree_node_tmpl<Column_parse_context> {
 protected:
  PT_column_attr_base() = default;

 public:
  enum Attr_type {
    AT_NONE,
    AT_COLLATE_COLUMN_ATTR,
    AT_DEFAULT_COLUMN_ATTR,
    AT_GENERATED_DEFAULT_VAL_COLUMN_ATTR,
    AT_SECONDARY_COLUMN_ATTR,
    AT_NOT_NULL_COLUMN_ATTR,
    AT_NULL_COLUMN_ATTR,
    AT_UNIQUE_KEY_COLUMN_ATTR,
    AT_PRIMARY_KEY_COLUMN_ATTR,
    AT_CHECK_CONSTRAINT_COLUMN_ATTR,
    AT_CONSTRAINT_ENFORCEMENT_ATTR,
    AT_COMMENT_COLUMN_ATTR,
    AT_ON_UPDATE_COLUMN_ATTR,
    AT_AUTO_INCREMENT_COLUMN_ATTR,
    AT_SERIAL_DEFAULT_VALUE_COLUMN_ATTR,
    AT_COLUMN_FORMAT_COLUMN_ATTR,
    AT_STORAGE_MEDIA_COLUMN_ATTR,
    AT_SRID_COLUMN_ATTR,
    AT_COLUMN_VISIBILITY_ATTR
  };

  typedef decltype(Alter_info::flags) alter_info_flags_t;

  virtual void apply_type_flags(ulong *) const {}
  virtual void apply_alter_info_flags(ulonglong *) const {}
  virtual void apply_comment(LEX_CSTRING *) const {}
  virtual void apply_default_value(Item **) const {}
  virtual void apply_gen_default_value(Value_generator **) {}
  virtual void apply_on_update_value(Item **) const {}
  virtual void apply_srid_modifier(std::optional<gis::srid_t> *) const {}
  virtual bool apply_collation(Column_parse_context *,
                               const CHARSET_INFO **to [[maybe_unused]],
                               bool *has_explicit_collation
                               [[maybe_unused]]) const {
    return false;
  }
  virtual bool add_check_constraints(
      Sql_check_constraint_spec_list *check_const_list [[maybe_unused]]) {
    return false;
  }
  virtual Attr_type attr_type() const { return AT_NONE; }

  /**
    Check for the [NOT] ENFORCED characteristic.

    @returns true  if the [NOT] ENFORCED follows the CHECK(...) clause,
             false otherwise.
  */
  virtual bool has_constraint_enforcement() const { return false; }

  /**
    Check if constraint is enforced.
    Method must be called only when has_constraint_enforcement() is true (i.e
    when [NOT] ENFORCED follows the CHECK(...) clause).

    @returns true  if constraint is enforced.
             false otherwise.
  */
  virtual bool is_constraint_enforced() const { return false; }

  /**
    Update the ENFORCED/NOT ENFORCED state of the CHECK constraint.

    @param   enforced     true if ENFORCED, false if NOT ENFORCED.

    @returns false if success, true if error (e.g. if [NOT] ENFORCED follows
             something other than the CHECK clause.)
  */
  virtual bool set_constraint_enforcement(bool enforced [[maybe_unused]]) {
    return true;  // error
  }
};

/**
  Node for the @SQL{NULL} column attribute

  @ingroup ptn_column_attrs
*/
class PT_null_column_attr : public PT_column_attr_base {
 public:
  void apply_type_flags(ulong *type_flags) const override {
    *type_flags &= ~NOT_NULL_FLAG;
    *type_flags |= EXPLICIT_NULL_FLAG;
  }

  enum Attr_type attr_type() const override { return AT_NULL_COLUMN_ATTR; }
};

/**
  Node for the @SQL{NOT NULL} column attribute

  @ingroup ptn_column_attrs
*/
class PT_not_null_column_attr : public PT_column_attr_base {
  void apply_type_flags(ulong *type_flags) const override {
    *type_flags |= NOT_NULL_FLAG;
  }

  enum Attr_type attr_type() const override { return AT_NOT_NULL_COLUMN_ATTR; }
};

/**
  Node for the @SQL{NOT SECONDARY} column attribute

  @ingroup ptn_column_attrs
*/
class PT_secondary_column_attr : public PT_column_attr_base {
 public:
  void apply_type_flags(unsigned long *type_flags) const override {
    *type_flags |= NOT_SECONDARY_FLAG;
  }

  enum Attr_type attr_type() const override { return AT_SECONDARY_COLUMN_ATTR; }
};

/**
  Node for the @SQL{UNIQUE [KEY]} column attribute

  @ingroup ptn_column_attrs
*/
class PT_unique_key_column_attr : public PT_column_attr_base {
 public:
  void apply_type_flags(ulong *type_flags) const override {
    *type_flags |= UNIQUE_FLAG;
  }

  void apply_alter_info_flags(ulonglong *flags) const override {
    *flags |= Alter_info::ALTER_ADD_INDEX;
  }

  enum Attr_type attr_type() const override {
    return AT_UNIQUE_KEY_COLUMN_ATTR;
  }
};

/**
  Node for the @SQL{PRIMARY [KEY]} column attribute

  @ingroup ptn_column_attrs
*/
class PT_primary_key_column_attr : public PT_column_attr_base {
 public:
  void apply_type_flags(ulong *type_flags) const override {
    *type_flags |= PRI_KEY_FLAG | NOT_NULL_FLAG;
  }

  void apply_alter_info_flags(ulonglong *flags) const override {
    *flags |= Alter_info::ALTER_ADD_INDEX;
  }

  enum Attr_type attr_type() const override {
    return AT_PRIMARY_KEY_COLUMN_ATTR;
  }
};

/**
  Node for the @SQL{[CONSTRAINT [symbol]] CHECK '(' expr ')'} column attribute.

  @ingroup ptn_column_attrs
*/
class PT_check_constraint_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;
  Sql_check_constraint_spec col_cc_spec;

 public:
  explicit PT_check_constraint_column_attr(LEX_STRING &name, Item *expr) {
    col_cc_spec.name = name;
    col_cc_spec.check_expr = expr;
  }

  bool set_constraint_enforcement(bool enforced) override {
    col_cc_spec.is_enforced = enforced;
    return false;
  }

  void apply_alter_info_flags(ulonglong *flags) const override {
    *flags |= Alter_info::ADD_CHECK_CONSTRAINT;
  }

  bool add_check_constraints(
      Sql_check_constraint_spec_list *check_const_list) override {
    assert(check_const_list != nullptr);
    return (check_const_list->push_back(&col_cc_spec));
  }

  bool contextualize(Column_parse_context *pc) override {
    return (super::contextualize(pc) ||
            col_cc_spec.check_expr->itemize(pc, &col_cc_spec.check_expr));
  }

  enum Attr_type attr_type() const override {
    return AT_CHECK_CONSTRAINT_COLUMN_ATTR;
  }
};

/**
  Node for the @SQL{[NOT] ENFORCED} column attribute.

  @ingroup ptn_column_attrs
*/
class PT_constraint_enforcement_attr : public PT_column_attr_base {
 public:
  explicit PT_constraint_enforcement_attr(bool enforced)
      : m_enforced(enforced) {}

  bool has_constraint_enforcement() const override { return true; }

  bool is_constraint_enforced() const override { return m_enforced; }

  enum Attr_type attr_type() const override {
    return AT_CONSTRAINT_ENFORCEMENT_ATTR;
  }

 private:
  const bool m_enforced;
};

/**
  Node for the @SQL{COMMENT @<comment@>} column attribute

  @ingroup ptn_column_attrs
*/
class PT_comment_column_attr : public PT_column_attr_base {
  const LEX_CSTRING comment;

 public:
  explicit PT_comment_column_attr(const LEX_CSTRING &comment)
      : comment(comment) {}

  void apply_comment(LEX_CSTRING *to) const override { *to = comment; }

  enum Attr_type attr_type() const override { return AT_COMMENT_COLUMN_ATTR; }
};

/**
  Node for the @SQL{COLLATE @<collation@>} column attribute

  @ingroup ptn_column_attrs
*/
class PT_collate_column_attr : public PT_column_attr_base {
 public:
  explicit PT_collate_column_attr(const POS &pos, const CHARSET_INFO *collation)
      : m_pos(pos), m_collation(collation) {
    assert(m_collation != nullptr);
  }

  bool apply_collation(Column_parse_context *pc, const CHARSET_INFO **to,
                       bool *has_explicit_collation) const override {
    if (*has_explicit_collation) {
      pc->thd->syntax_error_at(m_pos, ER_INVALID_MULTIPLE_CLAUSES, "COLLATE");
      return true;
    }
    *has_explicit_collation = true;
    return merge_charset_and_collation(*to, m_collation, to);
  }

  enum Attr_type attr_type() const override { return AT_COLLATE_COLUMN_ATTR; }

 private:
  const POS m_pos;
  const CHARSET_INFO *const m_collation;
};

// Specific to non-generated columns only:

/**
  Node for the @SQL{DEFAULT @<expression@>} column attribute

  @ingroup ptn_not_gcol_attr
*/
class PT_default_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

  Item *item;

 public:
  explicit PT_default_column_attr(Item *item) : item(item) {}
  void apply_default_value(Item **value) const override { *value = item; }
  bool contextualize(Column_parse_context *pc) override {
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "DEFAULT", "generated column");
      return true;
    }
    return super::contextualize(pc) || item->itemize(pc, &item);
  }
  void apply_type_flags(ulong *type_flags) const override {
    if (item->type() == Item::NULL_ITEM) *type_flags |= EXPLICIT_NULL_FLAG;
  }

  enum Attr_type attr_type() const override { return AT_DEFAULT_COLUMN_ATTR; }
};

/**
  Node for the @SQL{UPDATE NOW[([@<precision@>])]} column attribute

  @ingroup ptn_not_gcol_attr
*/
class PT_on_update_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

  const uint8 precision;
  Item *item;

 public:
  explicit PT_on_update_column_attr(uint8 precision) : precision(precision) {}
  void apply_on_update_value(Item **value) const override { *value = item; }

  bool contextualize(Column_parse_context *pc) override {
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "ON UPDATE", "generated column");
      return true;
    }
    if (super::contextualize(pc)) return true;

    item = new (pc->thd->mem_root) Item_func_now_local(precision);
    return item == nullptr;
  }

  enum Attr_type attr_type() const override { return AT_ON_UPDATE_COLUMN_ATTR; }
};

/**
  Node for the @SQL{AUTO_INCREMENT} column attribute

  @ingroup ptn_not_gcol_attr
*/
class PT_auto_increment_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

 public:
  void apply_type_flags(ulong *type_flags) const override {
    *type_flags |= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG;
  }
  bool contextualize(Column_parse_context *pc) override {
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "AUTO_INCREMENT", "generated column");
      return true;
    }
    return super::contextualize(pc);
  }

  enum Attr_type attr_type() const override {
    return AT_AUTO_INCREMENT_COLUMN_ATTR;
  }
};

/**
  Node for the @SQL{SERIAL DEFAULT VALUE} column attribute

  @ingroup ptn_not_gcol_attr
*/
class PT_serial_default_value_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

 public:
  void apply_type_flags(ulong *type_flags) const override {
    *type_flags |= AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNIQUE_FLAG;
  }
  void apply_alter_info_flags(ulonglong *flags) const override {
    *flags |= Alter_info::ALTER_ADD_INDEX;
  }
  bool contextualize(Column_parse_context *pc) override {
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "SERIAL DEFAULT VALUE",
               "generated column");
      return true;
    }
    return super::contextualize(pc);
  }
  enum Attr_type attr_type() const override {
    return AT_SERIAL_DEFAULT_VALUE_COLUMN_ATTR;
  }
};

/**
  Node for the @SQL{COLUMN_FORMAT @<DEFAULT|FIXED|DYNAMIC@>} column attribute

  @ingroup ptn_not_gcol_attr
*/
class PT_column_format_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

  column_format_type format;

 public:
  explicit PT_column_format_column_attr(column_format_type format)
      : format(format) {}

  void apply_type_flags(ulong *type_flags) const override {
    *type_flags &= ~(FIELD_FLAGS_COLUMN_FORMAT_MASK);
    *type_flags |= format << FIELD_FLAGS_COLUMN_FORMAT;
  }
  bool contextualize(Column_parse_context *pc) override {
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "COLUMN_FORMAT", "generated column");
      return true;
    }
    return super::contextualize(pc);
  }
  enum Attr_type attr_type() const override {
    return AT_COLUMN_FORMAT_COLUMN_ATTR;
  }
};

/**
  Node for the @SQL{STORAGE @<DEFAULT|DISK|MEMORY@>} column attribute

  @ingroup ptn_not_gcol_attr
*/
class PT_storage_media_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

  ha_storage_media media;

 public:
  explicit PT_storage_media_column_attr(ha_storage_media media)
      : media(media) {}

  void apply_type_flags(ulong *type_flags) const override {
    *type_flags &= ~(FIELD_FLAGS_STORAGE_MEDIA_MASK);
    *type_flags |= media << FIELD_FLAGS_STORAGE_MEDIA;
  }
  bool contextualize(Column_parse_context *pc) override {
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "STORAGE", "generated column");
      return true;
    }
    return super::contextualize(pc);
  }
  enum Attr_type attr_type() const override {
    return AT_STORAGE_MEDIA_COLUMN_ATTR;
  }
};

/// Node for the SRID column attribute
class PT_srid_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

  gis::srid_t m_srid;

 public:
  explicit PT_srid_column_attr(gis::srid_t srid) : m_srid(srid) {}

  void apply_srid_modifier(std::optional<gis::srid_t> *srid) const override {
    *srid = m_srid;
  }

  enum Attr_type attr_type() const override { return AT_SRID_COLUMN_ATTR; }
};

/// Node for the generated default value, column attribute
class PT_generated_default_val_column_attr : public PT_column_attr_base {
  typedef PT_column_attr_base super;

 public:
  PT_generated_default_val_column_attr(Item *expr) {
    m_default_value_expression.expr_item = expr;
    m_default_value_expression.set_field_stored(true);
  }

  void apply_gen_default_value(
      Value_generator **default_value_expression) override {
    *default_value_expression = &m_default_value_expression;
  }

  bool contextualize(Column_parse_context *pc) override {
    // GC and default value expressions are mutually exclusive and thus only
    // one is allowed to be present on the same column definition.
    if (pc->is_generated) {
      my_error(ER_WRONG_USAGE, MYF(0), "DEFAULT", "generated column");
      return true;
    }
    Parse_context expr_pc(pc->thd, pc->select);
    return super::contextualize(pc) ||
           m_default_value_expression.expr_item->itemize(
               &expr_pc, &m_default_value_expression.expr_item);
  }

  enum Attr_type attr_type() const override {
    return AT_GENERATED_DEFAULT_VAL_COLUMN_ATTR;
  }

 private:
  Value_generator m_default_value_expression;
};

/**
  Node for the @SQL{VISIBLE|INVISIBLE} column attribute

  @ingroup ptn_column_attrs
*/
class PT_column_visibility_attr : public PT_column_attr_base {
 public:
  explicit PT_column_visibility_attr(bool is_visible)
      : m_is_visible(is_visible) {}
  void apply_type_flags(unsigned long *type_flags) const override {
    *type_flags &= ~FIELD_IS_INVISIBLE;
    if (!m_is_visible) *type_flags |= FIELD_IS_INVISIBLE;
  }

  enum Attr_type attr_type() const override {
    return AT_COLUMN_VISIBILITY_ATTR;
  }

 private:
  const bool m_is_visible;
};

// Type nodes:

/**
  Base class for all column type nodes

  @ingroup ptn_column_types
*/
class PT_type : public Parse_tree_node {
 public:
  const enum_field_types type;

 protected:
  explicit PT_type(enum_field_types type) : type(type) {}

 public:
  virtual ulong get_type_flags() const { return 0; }
  virtual const char *get_length() const { return nullptr; }
  virtual const char *get_dec() const { return nullptr; }
  virtual const CHARSET_INFO *get_charset() const { return nullptr; }
  virtual uint get_uint_geom_type() const { return 0; }
  virtual List<String> *get_interval_list() const { return nullptr; }
  virtual bool is_serial_type() const { return false; }
};

/**
  Node for numeric types

  Type list:
  * NUMERIC, REAL, DOUBLE, DECIMAL and FIXED,
  * INTEGER, INT, INT1, INT2, INT3, INT4, TINYINT, SMALLINT, MEDIUMINT and
    BIGINT.

  @ingroup ptn_column_types
*/
class PT_numeric_type : public PT_type {
  const char *length;
  const char *dec;
  ulong options;

  using Parent_type = std::remove_const<decltype(PT_type::type)>::type;

 public:
  PT_numeric_type(THD *thd, Numeric_type type_arg, const char *length,
                  const char *dec, ulong options)
      : PT_type(static_cast<Parent_type>(type_arg)),
        length(length),
        dec(dec),
        options(options) {
    assert((options & ~(UNSIGNED_FLAG | ZEROFILL_FLAG)) == 0);

    if (type_arg != Numeric_type::DECIMAL && dec != nullptr) {
      push_warning(thd, Sql_condition::SL_WARNING,
                   ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT,
                   ER_THD(thd, ER_WARN_DEPRECATED_FLOAT_DIGITS));
    }
    if (options & UNSIGNED_FLAG) {
      push_warning(thd, Sql_condition::SL_WARNING,
                   ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT,
                   ER_THD(thd, ER_WARN_DEPRECATED_FLOAT_UNSIGNED));
    }
  }
  PT_numeric_type(THD *thd, Int_type type_arg, const char *length,
                  ulong options)
      : PT_type(static_cast<enum_field_types>(type_arg)),
        length(length),
        dec(nullptr),
        options(options) {
    assert((options & ~(UNSIGNED_FLAG | ZEROFILL_FLAG)) == 0);

    if (length != nullptr) {
      push_warning(thd, Sql_condition::SL_WARNING,
                   ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT,
                   ER_THD(thd, ER_WARN_DEPRECATED_INTEGER_DISPLAY_WIDTH));
    }
  }

  ulong get_type_flags() const override {
    return (options & ZEROFILL_FLAG) ? (options | UNSIGNED_FLAG) : options;
  }
  const char *get_length() const override { return length; }
  const char *get_dec() const override { return dec; }
};

/**
  Node for the BIT type

  @ingroup ptn_column_types
*/
class PT_bit_type : public PT_type {
  const char *length;

 public:
  PT_bit_type() : PT_type(MYSQL_TYPE_BIT), length("1") {}
  explicit PT_bit_type(const char *length)
      : PT_type(MYSQL_TYPE_BIT), length(length) {}

  const char *get_length() const override { return length; }
};

/**
  Node for the BOOL/BOOLEAN type

  @ingroup ptn_column_types
*/
class PT_boolean_type : public PT_type {
 public:
  PT_boolean_type() : PT_type(MYSQL_TYPE_TINY) {}
  const char *get_length() const override { return "1"; }
};

enum class Char_type : ulong {
  CHAR = MYSQL_TYPE_STRING,
  VARCHAR = MYSQL_TYPE_VARCHAR,
  TEXT = MYSQL_TYPE_BLOB,
};

class PT_char_type : public PT_type {
  const char *length;
  const CHARSET_INFO *charset;
  const bool force_binary;

  using Parent_type = std::remove_const<decltype(PT_type::type)>::type;

 public:
  PT_char_type(Char_type char_type, const char *length,
               const CHARSET_INFO *charset, bool force_binary = false)
      : PT_type(static_cast<Parent_type>(char_type)),
        length(length),
        charset(charset),
        force_binary(force_binary) {
    assert(charset == nullptr || !force_binary);
  }
  PT_char_type(Char_type char_type, const CHARSET_INFO *charset,
               bool force_binary = false)
      : PT_char_type(char_type, "1", charset, force_binary) {}
  ulong get_type_flags() const override {
    return force_binary ? BINCMP_FLAG : 0;
  }
  const char *get_length() const override { return length; }
  const CHARSET_INFO *get_charset() const override { return charset; }
};

enum class Blob_type {
  TINY = MYSQL_TYPE_TINY_BLOB,
  MEDIUM = MYSQL_TYPE_MEDIUM_BLOB,
  LONG = MYSQL_TYPE_LONG_BLOB,
};

/**
  Node for BLOB types

  Types: BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB, LONG, LONG VARBINARY,
         LONG VARCHAR, TEXT, TINYTEXT, MEDIUMTEXT, LONGTEXT.

  @ingroup ptn_column_types
*/
class PT_blob_type : public PT_type {
  const char *length;
  const CHARSET_INFO *charset;
  const bool force_binary;

  using Parent_type = std::remove_const<decltype(PT_type::type)>::type;

 public:
  PT_blob_type(Blob_type blob_type, const CHARSET_INFO *charset,
               bool force_binary = false)
      : PT_type(static_cast<Parent_type>(blob_type)),
        length(nullptr),
        charset(charset),
        force_binary(force_binary) {
    assert(charset == nullptr || !force_binary);
  }
  explicit PT_blob_type(const char *length)
      : PT_type(MYSQL_TYPE_BLOB),
        length(length),
        charset(&my_charset_bin),
        force_binary(false) {}

  ulong get_type_flags() const override {
    return force_binary ? BINCMP_FLAG : 0;
  }
  const CHARSET_INFO *get_charset() const override { return charset; }
  const char *get_length() const override { return length; }
};

/**
  Node for the YEAR type

  @ingroup ptn_column_types
*/
class PT_year_type : public PT_type {
 public:
  PT_year_type() : PT_type(MYSQL_TYPE_YEAR) {}
};

/**
  Node for the DATE type

  @ingroup ptn_column_types
*/
class PT_date_type : public PT_type {
 public:
  PT_date_type() : PT_type(MYSQL_TYPE_DATE) {}
};

enum class Time_type : ulong {
  TIME = MYSQL_TYPE_TIME2,
  DATETIME = MYSQL_TYPE_DATETIME2,
};

/**
  Node for the TIME, TIMESTAMP and DATETIME types

  @ingroup ptn_column_types
*/
class PT_time_type : public PT_type {
  const char *dec;

  using Parent_type = std::remove_const<decltype(PT_type::type)>::type;

 public:
  PT_time_type(Time_type time_type, const char *dec)
      : PT_type(static_cast<Parent_type>(time_type)), dec(dec) {}

  const char *get_dec() const override { return dec; }
};

/**
  Node for the TIMESTAMP type

  @ingroup ptn_column_types
*/
class PT_timestamp_type : public PT_type {
  typedef PT_type super;

  const char *dec;
  ulong type_flags;

 public:
  explicit PT_timestamp_type(const char *dec)
      : super(MYSQL_TYPE_TIMESTAMP2), dec(dec), type_flags(0) {}

  const char *get_dec() const override { return dec; }
  ulong get_type_flags() const override { return type_flags; }

  bool contextualize(Parse_context *pc) override {
    if (super::contextualize(pc)) return true;
    /*
      TIMESTAMP fields are NOT NULL by default, unless the variable
      explicit_defaults_for_timestamp is true.
    */
    if (!pc->thd->variables.explicit_defaults_for_timestamp)
      type_flags = NOT_NULL_FLAG;
    /*
      To flag the current statement as dependent for binary
      logging on the session var. Extra copying to Lex is
      done in case prepared stmt.
    */
    pc->thd->lex->binlog_need_explicit_defaults_ts =
        pc->thd->binlog_need_explicit_defaults_ts = true;

    return false;
  }
};

/**
  Node for spatial types

  Types: GEOMETRY, GEOMCOLLECTION/GEOMETRYCOLLECTION, POINT, MULTIPOINT,
         LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON

  @ingroup ptn_column_types
*/
class PT_spacial_type : public PT_type {
  Field::geometry_type geo_type;

 public:
  explicit PT_spacial_type(Field::geometry_type geo_type)
      : PT_type(MYSQL_TYPE_GEOMETRY), geo_type(geo_type) {}

  const CHARSET_INFO *get_charset() const override { return &my_charset_bin; }
  uint get_uint_geom_type() const override { return geo_type; }
  const char *get_length() const override { return nullptr; }
};

enum class Enum_type { ENUM = MYSQL_TYPE_ENUM, SET = MYSQL_TYPE_SET };

template <Enum_type enum_type>
class PT_enum_type_tmpl : public PT_type {
  List<String> *const interval_list;
  const CHARSET_INFO *charset;
  const bool force_binary;

  using Parent_type = std::remove_const<decltype(PT_type::type)>::type;

 public:
  PT_enum_type_tmpl(List<String> *interval_list, const CHARSET_INFO *charset,
                    bool force_binary)
      : PT_type(static_cast<Parent_type>(enum_type)),
        interval_list(interval_list),
        charset(charset),
        force_binary(force_binary) {
    assert(charset == nullptr || !force_binary);
  }

  const CHARSET_INFO *get_charset() const override { return charset; }
  ulong get_type_flags() const override {
    return force_binary ? BINCMP_FLAG : 0;
  }
  List<String> *get_interval_list() const override { return interval_list; }
};

/**
  Node for the ENUM type

  @ingroup ptn_column_types
*/
typedef PT_enum_type_tmpl<Enum_type::ENUM> PT_enum_type;

/**
  Node for the SET type

  @ingroup ptn_column_types
*/
typedef PT_enum_type_tmpl<Enum_type::SET> PT_set_type;

class PT_serial_type : public PT_type {
 public:
  PT_serial_type() : PT_type(MYSQL_TYPE_LONGLONG) {}

  ulong get_type_flags() const override {
    return AUTO_INCREMENT_FLAG | NOT_NULL_FLAG | UNSIGNED_FLAG | UNIQUE_FLAG;
  }
  bool is_serial_type() const override { return true; }
};

/**
  Node for the JSON type

  @ingroup ptn_column_types
*/
class PT_json_type : public PT_type {
 public:
  PT_json_type() : PT_type(MYSQL_TYPE_JSON) {}
  const CHARSET_INFO *get_charset() const override { return &my_charset_bin; }
};

/**
  Base class for both generated and regular column definitions

  @ingroup ptn_create_table
*/
class PT_field_def_base : public Parse_tree_node {
  typedef Parse_tree_node super;
  typedef decltype(Alter_info::flags) alter_info_flags_t;

 public:
  enum_field_types type;
  ulong type_flags;
  const char *length;
  const char *dec;
  const CHARSET_INFO *charset;
  bool has_explicit_collation;
  uint uint_geom_type;
  List<String> *interval_list;
  alter_info_flags_t alter_info_flags;
  LEX_CSTRING comment;
  Item *default_value;
  Item *on_update_value;
  Value_generator *gcol_info;
  /// Holds the expression to generate default values
  Value_generator *default_val_info;
  std::optional<gis::srid_t> m_srid;
  // List of column check constraint's specification.
  Sql_check_constraint_spec_list *check_const_spec_list{nullptr};

 protected:
  PT_type *type_node;

  explicit PT_field_def_base(PT_type *type_node)
      : has_explicit_collation(false),
        alter_info_flags(0),
        comment(EMPTY_CSTR),
        default_value(nullptr),
        on_update_value(nullptr),
        gcol_info(nullptr),
        default_val_info(nullptr),
        type_node(type_node) {}

 public:
  bool contextualize(Parse_context *pc) override {
    if (super::contextualize(pc) || type_node->contextualize(pc)) return true;

    type = type_node->type;
    type_flags = type_node->get_type_flags();
    length = type_node->get_length();
    dec = type_node->get_dec();
    charset = type_node->get_charset();
    uint_geom_type = type_node->get_uint_geom_type();
    interval_list = type_node->get_interval_list();
    check_const_spec_list = new (pc->thd->mem_root)
        Sql_check_constraint_spec_list(pc->thd->mem_root);
    if (check_const_spec_list == nullptr) return true;  // OOM
    return false;
  }

 private:
  bool is_overridden(Mem_root_array<PT_column_attr_base *> *attrs,
                     PT_column_attr_base *cand) {
    // skip de-duplicating these types. NONE is used for (secondary) engine
    // attributes, cf. make_column_engine_attribute and
    // make_column_secondary_engine_attribute
    switch (cand->attr_type()) {
      case PT_column_attr_base::AT_NONE:  // engine attributes
      case PT_column_attr_base::AT_CHECK_CONSTRAINT_COLUMN_ATTR:  // additive
      case PT_column_attr_base::AT_COLLATE_COLUMN_ATTR:  // avoid test breaks
        return false;
      default:
        break;
    }

    for (auto attr : *attrs) {
      if (attr->attr_type() == cand->attr_type()) return true;
    }
    return false;
  }

 protected:
  bool contextualize_attrs(Column_parse_context *pc,
                           Mem_root_array<PT_column_attr_base *> *attrs) {
    if (attrs != nullptr) {
      if (attrs->size() > 1) {
        // Keep only last instance of most attributes that override any
        // preceding ones. This avoids contextualizing attributes that will
        // later be ignored anyway.
        Mem_root_array<PT_column_attr_base *> *unique_attrs = new (pc->mem_root)
            Mem_root_array<PT_column_attr_base *>(pc->mem_root);
        unique_attrs->reserve(attrs->size());
        for (long i = static_cast<long>(attrs->size()) - 1; i >= 0; i--) {
          if (is_overridden(unique_attrs, (*attrs)[i])) continue;
          unique_attrs->push_back((*attrs)[i]);  // reversed order..
        }
        attrs->clear();
        // Reverse order again to get back original order. Needed for attributes
        // which are opposed, e.g. NOT NULL vs NULL.
        for (auto attr : *unique_attrs) attrs->push_front(attr);
      }

      for (auto attr : *attrs) {
        if (attr->contextualize(pc)) return true;
        attr->apply_type_flags(&type_flags);
        attr->apply_alter_info_flags(&alter_info_flags);
        attr->apply_comment(&comment);
        attr->apply_default_value(&default_value);
        attr->apply_gen_default_value(&default_val_info);
        attr->apply_on_update_value(&on_update_value);
        attr->apply_srid_modifier(&m_srid);
        if (attr->apply_collation(pc, &charset, &has_explicit_collation))
          return true;
        if (attr->add_check_constraints(check_const_spec_list)) return true;
      }
    }
    return false;
  }
};

/**
  Base class for regular (non-generated) column definition nodes

  @ingroup ptn_create_table
*/
class PT_field_def : public PT_field_def_base {
  typedef PT_field_def_base super;

  Mem_root_array<PT_column_attr_base *> *opt_attrs;

 public:
  PT_field_def(PT_type *type_node_arg,
               Mem_root_array<PT_column_attr_base *> *opt_attrs)
      : super(type_node_arg), opt_attrs(opt_attrs) {}

  bool contextualize(Parse_context *pc_arg) override {
    Column_parse_context pc(pc_arg->thd, pc_arg->select, false);
    if (super::contextualize(&pc) || contextualize_attrs(&pc, opt_attrs))
      return true;

    move_cf_appliers(pc_arg, &pc);
    return false;
  }
};

/**
  Base class for generated column definition nodes

  @ingroup ptn_create_table
*/
class PT_generated_field_def : public PT_field_def_base {
  typedef PT_field_def_base super;

  const Virtual_or_stored virtual_or_stored;
  Item *expr;
  Mem_root_array<PT_column_attr_base *> *opt_attrs;

 public:
  PT_generated_field_def(PT_type *type_node_arg, Item *expr,
                         Virtual_or_stored virtual_or_stored,
                         Mem_root_array<PT_column_attr_base *> *opt_attrs)
      : super(type_node_arg),
        virtual_or_stored(virtual_or_stored),
        expr(expr),
        opt_attrs(opt_attrs) {}

  bool contextualize(Parse_context *pc_arg) override {
    Column_parse_context pc(pc_arg->thd, pc_arg->select, true);
    if (super::contextualize(&pc) || contextualize_attrs(&pc, opt_attrs) ||
        expr->itemize(&pc, &expr))
      return true;

    // column of type serial cannot be generated
    if (type_node->is_serial_type()) {
      my_error(ER_WRONG_USAGE, MYF(0), "SERIAL", "generated column");
      return true;
    }

    gcol_info = new (pc.mem_root) Value_generator;
    if (gcol_info == nullptr) return true;  // OOM
    gcol_info->expr_item = expr;
    if (virtual_or_stored == Virtual_or_stored::STORED)
      gcol_info->set_field_stored(true);
    gcol_info->set_field_type(type);

    return false;
  }
};

void move_cf_appliers(Parse_context *tddlpc, Column_parse_context *cpc);

#endif /* PARSE_TREE_COL_ATTRS_INCLUDED */