sqlglot.dialects.mysql API documentation

Edit on GitHub

# sqlglot.dialects.mysql

View Source

      1from __future__ import annotations
      2
      3import typing as t
      4
      5from sqlglot import exp, generator, parser, tokens, transforms
      6from sqlglot.dialects.dialect import (
      7    Dialect,
      8    arrow_json_extract_scalar_sql,
      9    datestrtodate_sql,
     10    format_time_lambda,
     11    locate_to_strposition,
     12    max_or_greatest,
     13    min_or_least,
     14    no_ilike_sql,
     15    no_paren_current_date_sql,
     16    no_pivot_sql,
     17    no_tablesample_sql,
     18    no_trycast_sql,
     19    parse_date_delta_with_interval,
     20    rename_func,
     21    strposition_to_locate_sql,
     22)
     23from sqlglot.helper import seq_get
     24from sqlglot.tokens import TokenType
     25
     26
     27def _show_parser(*args: t.Any, **kwargs: t.Any) -> t.Callable[[MySQL.Parser], exp.Show]:
     28    def _parse(self: MySQL.Parser) -> exp.Show:
     29        return self._parse_show_mysql(*args, **kwargs)
     30
     31    return _parse
     32
     33
     34def _date_trunc_sql(self: generator.Generator, expression: exp.DateTrunc) -> str:
     35    expr = self.sql(expression, "this")
     36    unit = expression.text("unit")
     37
     38    if unit == "day":
     39        return f"DATE({expr})"
     40
     41    if unit == "week":
     42        concat = f"CONCAT(YEAR({expr}), ' ', WEEK({expr}, 1), ' 1')"
     43        date_format = "%Y %u %w"
     44    elif unit == "month":
     45        concat = f"CONCAT(YEAR({expr}), ' ', MONTH({expr}), ' 1')"
     46        date_format = "%Y %c %e"
     47    elif unit == "quarter":
     48        concat = f"CONCAT(YEAR({expr}), ' ', QUARTER({expr}) * 3 - 2, ' 1')"
     49        date_format = "%Y %c %e"
     50    elif unit == "year":
     51        concat = f"CONCAT(YEAR({expr}), ' 1 1')"
     52        date_format = "%Y %c %e"
     53    else:
     54        self.unsupported(f"Unexpected interval unit: {unit}")
     55        return f"DATE({expr})"
     56
     57    return f"STR_TO_DATE({concat}, '{date_format}')"
     58
     59
     60def _str_to_date(args: t.List) -> exp.StrToDate:
     61    date_format = MySQL.format_time(seq_get(args, 1))
     62    return exp.StrToDate(this=seq_get(args, 0), format=date_format)
     63
     64
     65def _str_to_date_sql(self: generator.Generator, expression: exp.StrToDate | exp.StrToTime) -> str:
     66    date_format = self.format_time(expression)
     67    return f"STR_TO_DATE({self.sql(expression.this)}, {date_format})"
     68
     69
     70def _trim_sql(self: generator.Generator, expression: exp.Trim) -> str:
     71    target = self.sql(expression, "this")
     72    trim_type = self.sql(expression, "position")
     73    remove_chars = self.sql(expression, "expression")
     74
     75    # Use TRIM/LTRIM/RTRIM syntax if the expression isn't mysql-specific
     76    if not remove_chars:
     77        return self.trim_sql(expression)
     78
     79    trim_type = f"{trim_type} " if trim_type else ""
     80    remove_chars = f"{remove_chars} " if remove_chars else ""
     81    from_part = "FROM " if trim_type or remove_chars else ""
     82    return f"TRIM({trim_type}{remove_chars}{from_part}{target})"
     83
     84
     85def _date_add_sql(kind: str) -> t.Callable[[generator.Generator, exp.DateAdd | exp.DateSub], str]:
     86    def func(self: generator.Generator, expression: exp.DateAdd | exp.DateSub) -> str:
     87        this = self.sql(expression, "this")
     88        unit = expression.text("unit").upper() or "DAY"
     89        return (
     90            f"DATE_{kind}({this}, {self.sql(exp.Interval(this=expression.expression, unit=unit))})"
     91        )
     92
     93    return func
     94
     95
     96class MySQL(Dialect):
     97    TIME_FORMAT = "'%Y-%m-%d %T'"
     98
     99    # https://prestodb.io/docs/current/functions/datetime.html#mysql-date-functions
    100    TIME_MAPPING = {
    101        "%M": "%B",
    102        "%c": "%-m",
    103        "%e": "%-d",
    104        "%h": "%I",
    105        "%i": "%M",
    106        "%s": "%S",
    107        "%S": "%S",
    108        "%u": "%W",
    109        "%k": "%-H",
    110        "%l": "%-I",
    111        "%T": "%H:%M:%S",
    112        "%W": "%a",
    113    }
    114
    115    class Tokenizer(tokens.Tokenizer):
    116        QUOTES = ["'", '"']
    117        COMMENTS = ["--", "#", ("/*", "*/")]
    118        IDENTIFIERS = ["`"]
    119        STRING_ESCAPES = ["'", "\\"]
    120        BIT_STRINGS = [("b'", "'"), ("B'", "'"), ("0b", "")]
    121        HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", "")]
    122
    123        KEYWORDS = {
    124            **tokens.Tokenizer.KEYWORDS,
    125            "CHARSET": TokenType.CHARACTER_SET,
    126            "FORCE": TokenType.FORCE,
    127            "IGNORE": TokenType.IGNORE,
    128            "LONGBLOB": TokenType.LONGBLOB,
    129            "LONGTEXT": TokenType.LONGTEXT,
    130            "MEDIUMBLOB": TokenType.MEDIUMBLOB,
    131            "MEDIUMTEXT": TokenType.MEDIUMTEXT,
    132            "SEPARATOR": TokenType.SEPARATOR,
    133            "ENUM": TokenType.ENUM,
    134            "START": TokenType.BEGIN,
    135            "SIGNED": TokenType.BIGINT,
    136            "SIGNED INTEGER": TokenType.BIGINT,
    137            "UNSIGNED": TokenType.UBIGINT,
    138            "UNSIGNED INTEGER": TokenType.UBIGINT,
    139            "_ARMSCII8": TokenType.INTRODUCER,
    140            "_ASCII": TokenType.INTRODUCER,
    141            "_BIG5": TokenType.INTRODUCER,
    142            "_BINARY": TokenType.INTRODUCER,
    143            "_CP1250": TokenType.INTRODUCER,
    144            "_CP1251": TokenType.INTRODUCER,
    145            "_CP1256": TokenType.INTRODUCER,
    146            "_CP1257": TokenType.INTRODUCER,
    147            "_CP850": TokenType.INTRODUCER,
    148            "_CP852": TokenType.INTRODUCER,
    149            "_CP866": TokenType.INTRODUCER,
    150            "_CP932": TokenType.INTRODUCER,
    151            "_DEC8": TokenType.INTRODUCER,
    152            "_EUCJPMS": TokenType.INTRODUCER,
    153            "_EUCKR": TokenType.INTRODUCER,
    154            "_GB18030": TokenType.INTRODUCER,
    155            "_GB2312": TokenType.INTRODUCER,
    156            "_GBK": TokenType.INTRODUCER,
    157            "_GEOSTD8": TokenType.INTRODUCER,
    158            "_GREEK": TokenType.INTRODUCER,
    159            "_HEBREW": TokenType.INTRODUCER,
    160            "_HP8": TokenType.INTRODUCER,
    161            "_KEYBCS2": TokenType.INTRODUCER,
    162            "_KOI8R": TokenType.INTRODUCER,
    163            "_KOI8U": TokenType.INTRODUCER,
    164            "_LATIN1": TokenType.INTRODUCER,
    165            "_LATIN2": TokenType.INTRODUCER,
    166            "_LATIN5": TokenType.INTRODUCER,
    167            "_LATIN7": TokenType.INTRODUCER,
    168            "_MACCE": TokenType.INTRODUCER,
    169            "_MACROMAN": TokenType.INTRODUCER,
    170            "_SJIS": TokenType.INTRODUCER,
    171            "_SWE7": TokenType.INTRODUCER,
    172            "_TIS620": TokenType.INTRODUCER,
    173            "_UCS2": TokenType.INTRODUCER,
    174            "_UJIS": TokenType.INTRODUCER,
    175            # https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
    176            "_UTF8": TokenType.INTRODUCER,
    177            "_UTF16": TokenType.INTRODUCER,
    178            "_UTF16LE": TokenType.INTRODUCER,
    179            "_UTF32": TokenType.INTRODUCER,
    180            "_UTF8MB3": TokenType.INTRODUCER,
    181            "_UTF8MB4": TokenType.INTRODUCER,
    182            "@@": TokenType.SESSION_PARAMETER,
    183        }
    184
    185        COMMANDS = tokens.Tokenizer.COMMANDS - {TokenType.SHOW}
    186
    187    class Parser(parser.Parser):
    188        FUNC_TOKENS = {*parser.Parser.FUNC_TOKENS, TokenType.SCHEMA, TokenType.DATABASE}
    189        TABLE_ALIAS_TOKENS = (
    190            parser.Parser.TABLE_ALIAS_TOKENS - parser.Parser.TABLE_INDEX_HINT_TOKENS
    191        )
    192
    193        FUNCTIONS = {
    194            **parser.Parser.FUNCTIONS,
    195            "DATE_ADD": parse_date_delta_with_interval(exp.DateAdd),
    196            "DATE_FORMAT": format_time_lambda(exp.TimeToStr, "mysql"),
    197            "DATE_SUB": parse_date_delta_with_interval(exp.DateSub),
    198            "INSTR": lambda args: exp.StrPosition(substr=seq_get(args, 1), this=seq_get(args, 0)),
    199            "LOCATE": locate_to_strposition,
    200            "STR_TO_DATE": _str_to_date,
    201        }
    202
    203        FUNCTION_PARSERS = {
    204            **parser.Parser.FUNCTION_PARSERS,
    205            "GROUP_CONCAT": lambda self: self.expression(
    206                exp.GroupConcat,
    207                this=self._parse_lambda(),
    208                separator=self._match(TokenType.SEPARATOR) and self._parse_field(),
    209            ),
    210        }
    211
    212        STATEMENT_PARSERS = {
    213            **parser.Parser.STATEMENT_PARSERS,
    214            TokenType.SHOW: lambda self: self._parse_show(),
    215        }
    216
    217        SHOW_PARSERS = {
    218            "BINARY LOGS": _show_parser("BINARY LOGS"),
    219            "MASTER LOGS": _show_parser("BINARY LOGS"),
    220            "BINLOG EVENTS": _show_parser("BINLOG EVENTS"),
    221            "CHARACTER SET": _show_parser("CHARACTER SET"),
    222            "CHARSET": _show_parser("CHARACTER SET"),
    223            "COLLATION": _show_parser("COLLATION"),
    224            "FULL COLUMNS": _show_parser("COLUMNS", target="FROM", full=True),
    225            "COLUMNS": _show_parser("COLUMNS", target="FROM"),
    226            "CREATE DATABASE": _show_parser("CREATE DATABASE", target=True),
    227            "CREATE EVENT": _show_parser("CREATE EVENT", target=True),
    228            "CREATE FUNCTION": _show_parser("CREATE FUNCTION", target=True),
    229            "CREATE PROCEDURE": _show_parser("CREATE PROCEDURE", target=True),
    230            "CREATE TABLE": _show_parser("CREATE TABLE", target=True),
    231            "CREATE TRIGGER": _show_parser("CREATE TRIGGER", target=True),
    232            "CREATE VIEW": _show_parser("CREATE VIEW", target=True),
    233            "DATABASES": _show_parser("DATABASES"),
    234            "ENGINE": _show_parser("ENGINE", target=True),
    235            "STORAGE ENGINES": _show_parser("ENGINES"),
    236            "ENGINES": _show_parser("ENGINES"),
    237            "ERRORS": _show_parser("ERRORS"),
    238            "EVENTS": _show_parser("EVENTS"),
    239            "FUNCTION CODE": _show_parser("FUNCTION CODE", target=True),
    240            "FUNCTION STATUS": _show_parser("FUNCTION STATUS"),
    241            "GRANTS": _show_parser("GRANTS", target="FOR"),
    242            "INDEX": _show_parser("INDEX", target="FROM"),
    243            "MASTER STATUS": _show_parser("MASTER STATUS"),
    244            "OPEN TABLES": _show_parser("OPEN TABLES"),
    245            "PLUGINS": _show_parser("PLUGINS"),
    246            "PROCEDURE CODE": _show_parser("PROCEDURE CODE", target=True),
    247            "PROCEDURE STATUS": _show_parser("PROCEDURE STATUS"),
    248            "PRIVILEGES": _show_parser("PRIVILEGES"),
    249            "FULL PROCESSLIST": _show_parser("PROCESSLIST", full=True),
    250            "PROCESSLIST": _show_parser("PROCESSLIST"),
    251            "PROFILE": _show_parser("PROFILE"),
    252            "PROFILES": _show_parser("PROFILES"),
    253            "RELAYLOG EVENTS": _show_parser("RELAYLOG EVENTS"),
    254            "REPLICAS": _show_parser("REPLICAS"),
    255            "SLAVE HOSTS": _show_parser("REPLICAS"),
    256            "REPLICA STATUS": _show_parser("REPLICA STATUS"),
    257            "SLAVE STATUS": _show_parser("REPLICA STATUS"),
    258            "GLOBAL STATUS": _show_parser("STATUS", global_=True),
    259            "SESSION STATUS": _show_parser("STATUS"),
    260            "STATUS": _show_parser("STATUS"),
    261            "TABLE STATUS": _show_parser("TABLE STATUS"),
    262            "FULL TABLES": _show_parser("TABLES", full=True),
    263            "TABLES": _show_parser("TABLES"),
    264            "TRIGGERS": _show_parser("TRIGGERS"),
    265            "GLOBAL VARIABLES": _show_parser("VARIABLES", global_=True),
    266            "SESSION VARIABLES": _show_parser("VARIABLES"),
    267            "VARIABLES": _show_parser("VARIABLES"),
    268            "WARNINGS": _show_parser("WARNINGS"),
    269        }
    270
    271        SET_PARSERS = {
    272            **parser.Parser.SET_PARSERS,
    273            "PERSIST": lambda self: self._parse_set_item_assignment("PERSIST"),
    274            "PERSIST_ONLY": lambda self: self._parse_set_item_assignment("PERSIST_ONLY"),
    275            "CHARACTER SET": lambda self: self._parse_set_item_charset("CHARACTER SET"),
    276            "CHARSET": lambda self: self._parse_set_item_charset("CHARACTER SET"),
    277            "NAMES": lambda self: self._parse_set_item_names(),
    278        }
    279
    280        PROFILE_TYPES = {
    281            "ALL",
    282            "BLOCK IO",
    283            "CONTEXT SWITCHES",
    284            "CPU",
    285            "IPC",
    286            "MEMORY",
    287            "PAGE FAULTS",
    288            "SOURCE",
    289            "SWAPS",
    290        }
    291
    292        TYPE_TOKENS = {
    293            *parser.Parser.TYPE_TOKENS,
    294            TokenType.SET,
    295        }
    296
    297        ENUM_TYPE_TOKENS = {
    298            *parser.Parser.ENUM_TYPE_TOKENS,
    299            TokenType.SET,
    300        }
    301
    302        LOG_DEFAULTS_TO_LN = True
    303
    304        def _parse_show_mysql(
    305            self,
    306            this: str,
    307            target: bool | str = False,
    308            full: t.Optional[bool] = None,
    309            global_: t.Optional[bool] = None,
    310        ) -> exp.Show:
    311            if target:
    312                if isinstance(target, str):
    313                    self._match_text_seq(target)
    314                target_id = self._parse_id_var()
    315            else:
    316                target_id = None
    317
    318            log = self._parse_string() if self._match_text_seq("IN") else None
    319
    320            if this in {"BINLOG EVENTS", "RELAYLOG EVENTS"}:
    321                position = self._parse_number() if self._match_text_seq("FROM") else None
    322                db = None
    323            else:
    324                position = None
    325                db = None
    326
    327                if self._match(TokenType.FROM):
    328                    db = self._parse_id_var()
    329                elif self._match(TokenType.DOT):
    330                    db = target_id
    331                    target_id = self._parse_id_var()
    332
    333            channel = self._parse_id_var() if self._match_text_seq("FOR", "CHANNEL") else None
    334
    335            like = self._parse_string() if self._match_text_seq("LIKE") else None
    336            where = self._parse_where()
    337
    338            if this == "PROFILE":
    339                types = self._parse_csv(lambda: self._parse_var_from_options(self.PROFILE_TYPES))
    340                query = self._parse_number() if self._match_text_seq("FOR", "QUERY") else None
    341                offset = self._parse_number() if self._match_text_seq("OFFSET") else None
    342                limit = self._parse_number() if self._match_text_seq("LIMIT") else None
    343            else:
    344                types, query = None, None
    345                offset, limit = self._parse_oldstyle_limit()
    346
    347            mutex = True if self._match_text_seq("MUTEX") else None
    348            mutex = False if self._match_text_seq("STATUS") else mutex
    349
    350            return self.expression(
    351                exp.Show,
    352                this=this,
    353                target=target_id,
    354                full=full,
    355                log=log,
    356                position=position,
    357                db=db,
    358                channel=channel,
    359                like=like,
    360                where=where,
    361                types=types,
    362                query=query,
    363                offset=offset,
    364                limit=limit,
    365                mutex=mutex,
    366                **{"global": global_},  # type: ignore
    367            )
    368
    369        def _parse_oldstyle_limit(
    370            self,
    371        ) -> t.Tuple[t.Optional[exp.Expression], t.Optional[exp.Expression]]:
    372            limit = None
    373            offset = None
    374            if self._match_text_seq("LIMIT"):
    375                parts = self._parse_csv(self._parse_number)
    376                if len(parts) == 1:
    377                    limit = parts[0]
    378                elif len(parts) == 2:
    379                    limit = parts[1]
    380                    offset = parts[0]
    381
    382            return offset, limit
    383
    384        def _parse_set_item_charset(self, kind: str) -> exp.Expression:
    385            this = self._parse_string() or self._parse_id_var()
    386            return self.expression(exp.SetItem, this=this, kind=kind)
    387
    388        def _parse_set_item_names(self) -> exp.Expression:
    389            charset = self._parse_string() or self._parse_id_var()
    390            if self._match_text_seq("COLLATE"):
    391                collate = self._parse_string() or self._parse_id_var()
    392            else:
    393                collate = None
    394
    395            return self.expression(exp.SetItem, this=charset, collate=collate, kind="NAMES")
    396
    397    class Generator(generator.Generator):
    398        LOCKING_READS_SUPPORTED = True
    399        NULL_ORDERING_SUPPORTED = False
    400        JOIN_HINTS = False
    401        TABLE_HINTS = True
    402
    403        TRANSFORMS = {
    404            **generator.Generator.TRANSFORMS,
    405            exp.CurrentDate: no_paren_current_date_sql,
    406            exp.DateDiff: lambda self, e: self.func("DATEDIFF", e.this, e.expression),
    407            exp.DateAdd: _date_add_sql("ADD"),
    408            exp.DateStrToDate: datestrtodate_sql,
    409            exp.DateSub: _date_add_sql("SUB"),
    410            exp.DateTrunc: _date_trunc_sql,
    411            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    412            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    413            exp.DayOfYear: rename_func("DAYOFYEAR"),
    414            exp.GroupConcat: lambda self, e: f"""GROUP_CONCAT({self.sql(e, "this")} SEPARATOR {self.sql(e, "separator") or "','"})""",
    415            exp.ILike: no_ilike_sql,
    416            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    417            exp.Max: max_or_greatest,
    418            exp.Min: min_or_least,
    419            exp.NullSafeEQ: lambda self, e: self.binary(e, "<=>"),
    420            exp.NullSafeNEQ: lambda self, e: self.not_sql(self.binary(e, "<=>")),
    421            exp.Pivot: no_pivot_sql,
    422            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
    423            exp.StrPosition: strposition_to_locate_sql,
    424            exp.StrToDate: _str_to_date_sql,
    425            exp.StrToTime: _str_to_date_sql,
    426            exp.TableSample: no_tablesample_sql,
    427            exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"),
    428            exp.TimeToStr: lambda self, e: self.func("DATE_FORMAT", e.this, self.format_time(e)),
    429            exp.Trim: _trim_sql,
    430            exp.TryCast: no_trycast_sql,
    431            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    432        }
    433
    434        TYPE_MAPPING = generator.Generator.TYPE_MAPPING.copy()
    435        TYPE_MAPPING.pop(exp.DataType.Type.MEDIUMTEXT)
    436        TYPE_MAPPING.pop(exp.DataType.Type.LONGTEXT)
    437        TYPE_MAPPING.pop(exp.DataType.Type.MEDIUMBLOB)
    438        TYPE_MAPPING.pop(exp.DataType.Type.LONGBLOB)
    439
    440        PROPERTIES_LOCATION = {
    441            **generator.Generator.PROPERTIES_LOCATION,
    442            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
    443            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    444        }
    445
    446        LIMIT_FETCH = "LIMIT"
    447
    448        def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    449            """(U)BIGINT is not allowed in a CAST expression, so we use (UN)SIGNED instead."""
    450            if expression.to.this == exp.DataType.Type.BIGINT:
    451                to = "SIGNED"
    452            elif expression.to.this == exp.DataType.Type.UBIGINT:
    453                to = "UNSIGNED"
    454            else:
    455                return super().cast_sql(expression)
    456
    457            return f"CAST({self.sql(expression, 'this')} AS {to})"
    458
    459        def show_sql(self, expression: exp.Show) -> str:
    460            this = f" {expression.name}"
    461            full = " FULL" if expression.args.get("full") else ""
    462            global_ = " GLOBAL" if expression.args.get("global") else ""
    463
    464            target = self.sql(expression, "target")
    465            target = f" {target}" if target else ""
    466            if expression.name in {"COLUMNS", "INDEX"}:
    467                target = f" FROM{target}"
    468            elif expression.name == "GRANTS":
    469                target = f" FOR{target}"
    470
    471            db = self._prefixed_sql("FROM", expression, "db")
    472
    473            like = self._prefixed_sql("LIKE", expression, "like")
    474            where = self.sql(expression, "where")
    475
    476            types = self.expressions(expression, key="types")
    477            types = f" {types}" if types else types
    478            query = self._prefixed_sql("FOR QUERY", expression, "query")
    479
    480            if expression.name == "PROFILE":
    481                offset = self._prefixed_sql("OFFSET", expression, "offset")
    482                limit = self._prefixed_sql("LIMIT", expression, "limit")
    483            else:
    484                offset = ""
    485                limit = self._oldstyle_limit_sql(expression)
    486
    487            log = self._prefixed_sql("IN", expression, "log")
    488            position = self._prefixed_sql("FROM", expression, "position")
    489
    490            channel = self._prefixed_sql("FOR CHANNEL", expression, "channel")
    491
    492            if expression.name == "ENGINE":
    493                mutex_or_status = " MUTEX" if expression.args.get("mutex") else " STATUS"
    494            else:
    495                mutex_or_status = ""
    496
    497            return f"SHOW{full}{global_}{this}{target}{types}{db}{query}{log}{position}{channel}{mutex_or_status}{like}{where}{offset}{limit}"
    498
    499        def _prefixed_sql(self, prefix: str, expression: exp.Expression, arg: str) -> str:
    500            sql = self.sql(expression, arg)
    501            return f" {prefix} {sql}" if sql else ""
    502
    503        def _oldstyle_limit_sql(self, expression: exp.Show) -> str:
    504            limit = self.sql(expression, "limit")
    505            offset = self.sql(expression, "offset")
    506            if limit:
    507                limit_offset = f"{offset}, {limit}" if offset else limit
    508                return f" LIMIT {limit_offset}"
    509            return ""

class MySQL(sqlglot.dialects.dialect.Dialect): View Source

     97class MySQL(Dialect):
     98    TIME_FORMAT = "'%Y-%m-%d %T'"
     99
    100    # https://prestodb.io/docs/current/functions/datetime.html#mysql-date-functions
    101    TIME_MAPPING = {
    102        "%M": "%B",
    103        "%c": "%-m",
    104        "%e": "%-d",
    105        "%h": "%I",
    106        "%i": "%M",
    107        "%s": "%S",
    108        "%S": "%S",
    109        "%u": "%W",
    110        "%k": "%-H",
    111        "%l": "%-I",
    112        "%T": "%H:%M:%S",
    113        "%W": "%a",
    114    }
    115
    116    class Tokenizer(tokens.Tokenizer):
    117        QUOTES = ["'", '"']
    118        COMMENTS = ["--", "#", ("/*", "*/")]
    119        IDENTIFIERS = ["`"]
    120        STRING_ESCAPES = ["'", "\\"]
    121        BIT_STRINGS = [("b'", "'"), ("B'", "'"), ("0b", "")]
    122        HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", "")]
    123
    124        KEYWORDS = {
    125            **tokens.Tokenizer.KEYWORDS,
    126            "CHARSET": TokenType.CHARACTER_SET,
    127            "FORCE": TokenType.FORCE,
    128            "IGNORE": TokenType.IGNORE,
    129            "LONGBLOB": TokenType.LONGBLOB,
    130            "LONGTEXT": TokenType.LONGTEXT,
    131            "MEDIUMBLOB": TokenType.MEDIUMBLOB,
    132            "MEDIUMTEXT": TokenType.MEDIUMTEXT,
    133            "SEPARATOR": TokenType.SEPARATOR,
    134            "ENUM": TokenType.ENUM,
    135            "START": TokenType.BEGIN,
    136            "SIGNED": TokenType.BIGINT,
    137            "SIGNED INTEGER": TokenType.BIGINT,
    138            "UNSIGNED": TokenType.UBIGINT,
    139            "UNSIGNED INTEGER": TokenType.UBIGINT,
    140            "_ARMSCII8": TokenType.INTRODUCER,
    141            "_ASCII": TokenType.INTRODUCER,
    142            "_BIG5": TokenType.INTRODUCER,
    143            "_BINARY": TokenType.INTRODUCER,
    144            "_CP1250": TokenType.INTRODUCER,
    145            "_CP1251": TokenType.INTRODUCER,
    146            "_CP1256": TokenType.INTRODUCER,
    147            "_CP1257": TokenType.INTRODUCER,
    148            "_CP850": TokenType.INTRODUCER,
    149            "_CP852": TokenType.INTRODUCER,
    150            "_CP866": TokenType.INTRODUCER,
    151            "_CP932": TokenType.INTRODUCER,
    152            "_DEC8": TokenType.INTRODUCER,
    153            "_EUCJPMS": TokenType.INTRODUCER,
    154            "_EUCKR": TokenType.INTRODUCER,
    155            "_GB18030": TokenType.INTRODUCER,
    156            "_GB2312": TokenType.INTRODUCER,
    157            "_GBK": TokenType.INTRODUCER,
    158            "_GEOSTD8": TokenType.INTRODUCER,
    159            "_GREEK": TokenType.INTRODUCER,
    160            "_HEBREW": TokenType.INTRODUCER,
    161            "_HP8": TokenType.INTRODUCER,
    162            "_KEYBCS2": TokenType.INTRODUCER,
    163            "_KOI8R": TokenType.INTRODUCER,
    164            "_KOI8U": TokenType.INTRODUCER,
    165            "_LATIN1": TokenType.INTRODUCER,
    166            "_LATIN2": TokenType.INTRODUCER,
    167            "_LATIN5": TokenType.INTRODUCER,
    168            "_LATIN7": TokenType.INTRODUCER,
    169            "_MACCE": TokenType.INTRODUCER,
    170            "_MACROMAN": TokenType.INTRODUCER,
    171            "_SJIS": TokenType.INTRODUCER,
    172            "_SWE7": TokenType.INTRODUCER,
    173            "_TIS620": TokenType.INTRODUCER,
    174            "_UCS2": TokenType.INTRODUCER,
    175            "_UJIS": TokenType.INTRODUCER,
    176            # https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
    177            "_UTF8": TokenType.INTRODUCER,
    178            "_UTF16": TokenType.INTRODUCER,
    179            "_UTF16LE": TokenType.INTRODUCER,
    180            "_UTF32": TokenType.INTRODUCER,
    181            "_UTF8MB3": TokenType.INTRODUCER,
    182            "_UTF8MB4": TokenType.INTRODUCER,
    183            "@@": TokenType.SESSION_PARAMETER,
    184        }
    185
    186        COMMANDS = tokens.Tokenizer.COMMANDS - {TokenType.SHOW}
    187
    188    class Parser(parser.Parser):
    189        FUNC_TOKENS = {*parser.Parser.FUNC_TOKENS, TokenType.SCHEMA, TokenType.DATABASE}
    190        TABLE_ALIAS_TOKENS = (
    191            parser.Parser.TABLE_ALIAS_TOKENS - parser.Parser.TABLE_INDEX_HINT_TOKENS
    192        )
    193
    194        FUNCTIONS = {
    195            **parser.Parser.FUNCTIONS,
    196            "DATE_ADD": parse_date_delta_with_interval(exp.DateAdd),
    197            "DATE_FORMAT": format_time_lambda(exp.TimeToStr, "mysql"),
    198            "DATE_SUB": parse_date_delta_with_interval(exp.DateSub),
    199            "INSTR": lambda args: exp.StrPosition(substr=seq_get(args, 1), this=seq_get(args, 0)),
    200            "LOCATE": locate_to_strposition,
    201            "STR_TO_DATE": _str_to_date,
    202        }
    203
    204        FUNCTION_PARSERS = {
    205            **parser.Parser.FUNCTION_PARSERS,
    206            "GROUP_CONCAT": lambda self: self.expression(
    207                exp.GroupConcat,
    208                this=self._parse_lambda(),
    209                separator=self._match(TokenType.SEPARATOR) and self._parse_field(),
    210            ),
    211        }
    212
    213        STATEMENT_PARSERS = {
    214            **parser.Parser.STATEMENT_PARSERS,
    215            TokenType.SHOW: lambda self: self._parse_show(),
    216        }
    217
    218        SHOW_PARSERS = {
    219            "BINARY LOGS": _show_parser("BINARY LOGS"),
    220            "MASTER LOGS": _show_parser("BINARY LOGS"),
    221            "BINLOG EVENTS": _show_parser("BINLOG EVENTS"),
    222            "CHARACTER SET": _show_parser("CHARACTER SET"),
    223            "CHARSET": _show_parser("CHARACTER SET"),
    224            "COLLATION": _show_parser("COLLATION"),
    225            "FULL COLUMNS": _show_parser("COLUMNS", target="FROM", full=True),
    226            "COLUMNS": _show_parser("COLUMNS", target="FROM"),
    227            "CREATE DATABASE": _show_parser("CREATE DATABASE", target=True),
    228            "CREATE EVENT": _show_parser("CREATE EVENT", target=True),
    229            "CREATE FUNCTION": _show_parser("CREATE FUNCTION", target=True),
    230            "CREATE PROCEDURE": _show_parser("CREATE PROCEDURE", target=True),
    231            "CREATE TABLE": _show_parser("CREATE TABLE", target=True),
    232            "CREATE TRIGGER": _show_parser("CREATE TRIGGER", target=True),
    233            "CREATE VIEW": _show_parser("CREATE VIEW", target=True),
    234            "DATABASES": _show_parser("DATABASES"),
    235            "ENGINE": _show_parser("ENGINE", target=True),
    236            "STORAGE ENGINES": _show_parser("ENGINES"),
    237            "ENGINES": _show_parser("ENGINES"),
    238            "ERRORS": _show_parser("ERRORS"),
    239            "EVENTS": _show_parser("EVENTS"),
    240            "FUNCTION CODE": _show_parser("FUNCTION CODE", target=True),
    241            "FUNCTION STATUS": _show_parser("FUNCTION STATUS"),
    242            "GRANTS": _show_parser("GRANTS", target="FOR"),
    243            "INDEX": _show_parser("INDEX", target="FROM"),
    244            "MASTER STATUS": _show_parser("MASTER STATUS"),
    245            "OPEN TABLES": _show_parser("OPEN TABLES"),
    246            "PLUGINS": _show_parser("PLUGINS"),
    247            "PROCEDURE CODE": _show_parser("PROCEDURE CODE", target=True),
    248            "PROCEDURE STATUS": _show_parser("PROCEDURE STATUS"),
    249            "PRIVILEGES": _show_parser("PRIVILEGES"),
    250            "FULL PROCESSLIST": _show_parser("PROCESSLIST", full=True),
    251            "PROCESSLIST": _show_parser("PROCESSLIST"),
    252            "PROFILE": _show_parser("PROFILE"),
    253            "PROFILES": _show_parser("PROFILES"),
    254            "RELAYLOG EVENTS": _show_parser("RELAYLOG EVENTS"),
    255            "REPLICAS": _show_parser("REPLICAS"),
    256            "SLAVE HOSTS": _show_parser("REPLICAS"),
    257            "REPLICA STATUS": _show_parser("REPLICA STATUS"),
    258            "SLAVE STATUS": _show_parser("REPLICA STATUS"),
    259            "GLOBAL STATUS": _show_parser("STATUS", global_=True),
    260            "SESSION STATUS": _show_parser("STATUS"),
    261            "STATUS": _show_parser("STATUS"),
    262            "TABLE STATUS": _show_parser("TABLE STATUS"),
    263            "FULL TABLES": _show_parser("TABLES", full=True),
    264            "TABLES": _show_parser("TABLES"),
    265            "TRIGGERS": _show_parser("TRIGGERS"),
    266            "GLOBAL VARIABLES": _show_parser("VARIABLES", global_=True),
    267            "SESSION VARIABLES": _show_parser("VARIABLES"),
    268            "VARIABLES": _show_parser("VARIABLES"),
    269            "WARNINGS": _show_parser("WARNINGS"),
    270        }
    271
    272        SET_PARSERS = {
    273            **parser.Parser.SET_PARSERS,
    274            "PERSIST": lambda self: self._parse_set_item_assignment("PERSIST"),
    275            "PERSIST_ONLY": lambda self: self._parse_set_item_assignment("PERSIST_ONLY"),
    276            "CHARACTER SET": lambda self: self._parse_set_item_charset("CHARACTER SET"),
    277            "CHARSET": lambda self: self._parse_set_item_charset("CHARACTER SET"),
    278            "NAMES": lambda self: self._parse_set_item_names(),
    279        }
    280
    281        PROFILE_TYPES = {
    282            "ALL",
    283            "BLOCK IO",
    284            "CONTEXT SWITCHES",
    285            "CPU",
    286            "IPC",
    287            "MEMORY",
    288            "PAGE FAULTS",
    289            "SOURCE",
    290            "SWAPS",
    291        }
    292
    293        TYPE_TOKENS = {
    294            *parser.Parser.TYPE_TOKENS,
    295            TokenType.SET,
    296        }
    297
    298        ENUM_TYPE_TOKENS = {
    299            *parser.Parser.ENUM_TYPE_TOKENS,
    300            TokenType.SET,
    301        }
    302
    303        LOG_DEFAULTS_TO_LN = True
    304
    305        def _parse_show_mysql(
    306            self,
    307            this: str,
    308            target: bool | str = False,
    309            full: t.Optional[bool] = None,
    310            global_: t.Optional[bool] = None,
    311        ) -> exp.Show:
    312            if target:
    313                if isinstance(target, str):
    314                    self._match_text_seq(target)
    315                target_id = self._parse_id_var()
    316            else:
    317                target_id = None
    318
    319            log = self._parse_string() if self._match_text_seq("IN") else None
    320
    321            if this in {"BINLOG EVENTS", "RELAYLOG EVENTS"}:
    322                position = self._parse_number() if self._match_text_seq("FROM") else None
    323                db = None
    324            else:
    325                position = None
    326                db = None
    327
    328                if self._match(TokenType.FROM):
    329                    db = self._parse_id_var()
    330                elif self._match(TokenType.DOT):
    331                    db = target_id
    332                    target_id = self._parse_id_var()
    333
    334            channel = self._parse_id_var() if self._match_text_seq("FOR", "CHANNEL") else None
    335
    336            like = self._parse_string() if self._match_text_seq("LIKE") else None
    337            where = self._parse_where()
    338
    339            if this == "PROFILE":
    340                types = self._parse_csv(lambda: self._parse_var_from_options(self.PROFILE_TYPES))
    341                query = self._parse_number() if self._match_text_seq("FOR", "QUERY") else None
    342                offset = self._parse_number() if self._match_text_seq("OFFSET") else None
    343                limit = self._parse_number() if self._match_text_seq("LIMIT") else None
    344            else:
    345                types, query = None, None
    346                offset, limit = self._parse_oldstyle_limit()
    347
    348            mutex = True if self._match_text_seq("MUTEX") else None
    349            mutex = False if self._match_text_seq("STATUS") else mutex
    350
    351            return self.expression(
    352                exp.Show,
    353                this=this,
    354                target=target_id,
    355                full=full,
    356                log=log,
    357                position=position,
    358                db=db,
    359                channel=channel,
    360                like=like,
    361                where=where,
    362                types=types,
    363                query=query,
    364                offset=offset,
    365                limit=limit,
    366                mutex=mutex,
    367                **{"global": global_},  # type: ignore
    368            )
    369
    370        def _parse_oldstyle_limit(
    371            self,
    372        ) -> t.Tuple[t.Optional[exp.Expression], t.Optional[exp.Expression]]:
    373            limit = None
    374            offset = None
    375            if self._match_text_seq("LIMIT"):
    376                parts = self._parse_csv(self._parse_number)
    377                if len(parts) == 1:
    378                    limit = parts[0]
    379                elif len(parts) == 2:
    380                    limit = parts[1]
    381                    offset = parts[0]
    382
    383            return offset, limit
    384
    385        def _parse_set_item_charset(self, kind: str) -> exp.Expression:
    386            this = self._parse_string() or self._parse_id_var()
    387            return self.expression(exp.SetItem, this=this, kind=kind)
    388
    389        def _parse_set_item_names(self) -> exp.Expression:
    390            charset = self._parse_string() or self._parse_id_var()
    391            if self._match_text_seq("COLLATE"):
    392                collate = self._parse_string() or self._parse_id_var()
    393            else:
    394                collate = None
    395
    396            return self.expression(exp.SetItem, this=charset, collate=collate, kind="NAMES")
    397
    398    class Generator(generator.Generator):
    399        LOCKING_READS_SUPPORTED = True
    400        NULL_ORDERING_SUPPORTED = False
    401        JOIN_HINTS = False
    402        TABLE_HINTS = True
    403
    404        TRANSFORMS = {
    405            **generator.Generator.TRANSFORMS,
    406            exp.CurrentDate: no_paren_current_date_sql,
    407            exp.DateDiff: lambda self, e: self.func("DATEDIFF", e.this, e.expression),
    408            exp.DateAdd: _date_add_sql("ADD"),
    409            exp.DateStrToDate: datestrtodate_sql,
    410            exp.DateSub: _date_add_sql("SUB"),
    411            exp.DateTrunc: _date_trunc_sql,
    412            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    413            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    414            exp.DayOfYear: rename_func("DAYOFYEAR"),
    415            exp.GroupConcat: lambda self, e: f"""GROUP_CONCAT({self.sql(e, "this")} SEPARATOR {self.sql(e, "separator") or "','"})""",
    416            exp.ILike: no_ilike_sql,
    417            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    418            exp.Max: max_or_greatest,
    419            exp.Min: min_or_least,
    420            exp.NullSafeEQ: lambda self, e: self.binary(e, "<=>"),
    421            exp.NullSafeNEQ: lambda self, e: self.not_sql(self.binary(e, "<=>")),
    422            exp.Pivot: no_pivot_sql,
    423            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
    424            exp.StrPosition: strposition_to_locate_sql,
    425            exp.StrToDate: _str_to_date_sql,
    426            exp.StrToTime: _str_to_date_sql,
    427            exp.TableSample: no_tablesample_sql,
    428            exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"),
    429            exp.TimeToStr: lambda self, e: self.func("DATE_FORMAT", e.this, self.format_time(e)),
    430            exp.Trim: _trim_sql,
    431            exp.TryCast: no_trycast_sql,
    432            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    433        }
    434
    435        TYPE_MAPPING = generator.Generator.TYPE_MAPPING.copy()
    436        TYPE_MAPPING.pop(exp.DataType.Type.MEDIUMTEXT)
    437        TYPE_MAPPING.pop(exp.DataType.Type.LONGTEXT)
    438        TYPE_MAPPING.pop(exp.DataType.Type.MEDIUMBLOB)
    439        TYPE_MAPPING.pop(exp.DataType.Type.LONGBLOB)
    440
    441        PROPERTIES_LOCATION = {
    442            **generator.Generator.PROPERTIES_LOCATION,
    443            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
    444            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    445        }
    446
    447        LIMIT_FETCH = "LIMIT"
    448
    449        def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    450            """(U)BIGINT is not allowed in a CAST expression, so we use (UN)SIGNED instead."""
    451            if expression.to.this == exp.DataType.Type.BIGINT:
    452                to = "SIGNED"
    453            elif expression.to.this == exp.DataType.Type.UBIGINT:
    454                to = "UNSIGNED"
    455            else:
    456                return super().cast_sql(expression)
    457
    458            return f"CAST({self.sql(expression, 'this')} AS {to})"
    459
    460        def show_sql(self, expression: exp.Show) -> str:
    461            this = f" {expression.name}"
    462            full = " FULL" if expression.args.get("full") else ""
    463            global_ = " GLOBAL" if expression.args.get("global") else ""
    464
    465            target = self.sql(expression, "target")
    466            target = f" {target}" if target else ""
    467            if expression.name in {"COLUMNS", "INDEX"}:
    468                target = f" FROM{target}"
    469            elif expression.name == "GRANTS":
    470                target = f" FOR{target}"
    471
    472            db = self._prefixed_sql("FROM", expression, "db")
    473
    474            like = self._prefixed_sql("LIKE", expression, "like")
    475            where = self.sql(expression, "where")
    476
    477            types = self.expressions(expression, key="types")
    478            types = f" {types}" if types else types
    479            query = self._prefixed_sql("FOR QUERY", expression, "query")
    480
    481            if expression.name == "PROFILE":
    482                offset = self._prefixed_sql("OFFSET", expression, "offset")
    483                limit = self._prefixed_sql("LIMIT", expression, "limit")
    484            else:
    485                offset = ""
    486                limit = self._oldstyle_limit_sql(expression)
    487
    488            log = self._prefixed_sql("IN", expression, "log")
    489            position = self._prefixed_sql("FROM", expression, "position")
    490
    491            channel = self._prefixed_sql("FOR CHANNEL", expression, "channel")
    492
    493            if expression.name == "ENGINE":
    494                mutex_or_status = " MUTEX" if expression.args.get("mutex") else " STATUS"
    495            else:
    496                mutex_or_status = ""
    497
    498            return f"SHOW{full}{global_}{this}{target}{types}{db}{query}{log}{position}{channel}{mutex_or_status}{like}{where}{offset}{limit}"
    499
    500        def _prefixed_sql(self, prefix: str, expression: exp.Expression, arg: str) -> str:
    501            sql = self.sql(expression, arg)
    502            return f" {prefix} {sql}" if sql else ""
    503
    504        def _oldstyle_limit_sql(self, expression: exp.Show) -> str:
    505            limit = self.sql(expression, "limit")
    506            offset = self.sql(expression, "offset")
    507            if limit:
    508                limit_offset = f"{offset}, {limit}" if offset else limit
    509                return f" LIMIT {limit_offset}"
    510            return ""

TIME_FORMAT = "'%Y-%m-%d %T'"
TIME_MAPPING: Dict[str, str] = {'%M': '%B', '%c': '%-m', '%e': '%-d', '%h': '%I', '%i': '%M', '%s': '%S', '%S': '%S', '%u': '%W', '%k': '%-H', '%l': '%-I', '%T': '%H:%M:%S', '%W': '%a'}
tokenizer_class = <class 'sqlglot.dialects.mysql.MySQL.Tokenizer'>
parser_class = <class 'sqlglot.dialects.mysql.MySQL.Parser'>
generator_class = <class 'sqlglot.dialects.mysql.MySQL.Generator'>
TIME_TRIE: Dict = {'%': {'M': {0: True}, 'c': {0: True}, 'e': {0: True}, 'h': {0: True}, 'i': {0: True}, 's': {0: True}, 'S': {0: True}, 'u': {0: True}, 'k': {0: True}, 'l': {0: True}, 'T': {0: True}, 'W': {0: True}}}
FORMAT_TRIE: Dict = {'%': {'M': {0: True}, 'c': {0: True}, 'e': {0: True}, 'h': {0: True}, 'i': {0: True}, 's': {0: True}, 'S': {0: True}, 'u': {0: True}, 'k': {0: True}, 'l': {0: True}, 'T': {0: True}, 'W': {0: True}}}
INVERSE_TIME_MAPPING: Dict[str, str] = {'%B': '%M', '%-m': '%c', '%-d': '%e', '%I': '%h', '%M': '%i', '%S': '%S', '%W': '%u', '%-H': '%k', '%-I': '%l', '%H:%M:%S': '%T', '%a': '%W'}
INVERSE_TIME_TRIE: Dict = {'%': {'B': {0: True}, '-': {'m': {0: True}, 'd': {0: True}, 'H': {0: True}, 'I': {0: True}}, 'I': {0: True}, 'M': {0: True}, 'S': {0: True}, 'W': {0: True}, 'H': {':': {'%': {'M': {':': {'%': {'S': {0: True}}}}}}}, 'a': {0: True}}}
QUOTE_START = "'"
QUOTE_END = "'"
IDENTIFIER_START = '`'
IDENTIFIER_END = '`'
BIT_START = "b'"
BIT_END = "'"
HEX_START = "x'"
HEX_END = "'"
BYTE_START = None
BYTE_END = None
RAW_START = None
RAW_END = None

##### Inherited Members

sqlglot.dialects.dialect.Dialect
  INDEX_OFFSET
  UNNEST_COLUMN_ONLY
  ALIAS_POST_TABLESAMPLE
  RESOLVES_IDENTIFIERS_AS_UPPERCASE
  IDENTIFIERS_CAN_START_WITH_DIGIT
  STRICT_STRING_CONCAT
  NORMALIZE_FUNCTIONS
  NULL_ORDERING
  DATE_FORMAT
  DATEINT_FORMAT
  FORMAT_MAPPING
  get_or_raise
  format_time
  normalize_identifier
  case_sensitive
  can_identify
  quote_identifier
  parse
  parse_into
  generate
  transpile
  tokenize
  tokenizer
  parser
  generator

class MySQL.Tokenizer(sqlglot.tokens.Tokenizer): View Source

    116    class Tokenizer(tokens.Tokenizer):
    117        QUOTES = ["'", '"']
    118        COMMENTS = ["--", "#", ("/*", "*/")]
    119        IDENTIFIERS = ["`"]
    120        STRING_ESCAPES = ["'", "\\"]
    121        BIT_STRINGS = [("b'", "'"), ("B'", "'"), ("0b", "")]
    122        HEX_STRINGS = [("x'", "'"), ("X'", "'"), ("0x", "")]
    123
    124        KEYWORDS = {
    125            **tokens.Tokenizer.KEYWORDS,
    126            "CHARSET": TokenType.CHARACTER_SET,
    127            "FORCE": TokenType.FORCE,
    128            "IGNORE": TokenType.IGNORE,
    129            "LONGBLOB": TokenType.LONGBLOB,
    130            "LONGTEXT": TokenType.LONGTEXT,
    131            "MEDIUMBLOB": TokenType.MEDIUMBLOB,
    132            "MEDIUMTEXT": TokenType.MEDIUMTEXT,
    133            "SEPARATOR": TokenType.SEPARATOR,
    134            "ENUM": TokenType.ENUM,
    135            "START": TokenType.BEGIN,
    136            "SIGNED": TokenType.BIGINT,
    137            "SIGNED INTEGER": TokenType.BIGINT,
    138            "UNSIGNED": TokenType.UBIGINT,
    139            "UNSIGNED INTEGER": TokenType.UBIGINT,
    140            "_ARMSCII8": TokenType.INTRODUCER,
    141            "_ASCII": TokenType.INTRODUCER,
    142            "_BIG5": TokenType.INTRODUCER,
    143            "_BINARY": TokenType.INTRODUCER,
    144            "_CP1250": TokenType.INTRODUCER,
    145            "_CP1251": TokenType.INTRODUCER,
    146            "_CP1256": TokenType.INTRODUCER,
    147            "_CP1257": TokenType.INTRODUCER,
    148            "_CP850": TokenType.INTRODUCER,
    149            "_CP852": TokenType.INTRODUCER,
    150            "_CP866": TokenType.INTRODUCER,
    151            "_CP932": TokenType.INTRODUCER,
    152            "_DEC8": TokenType.INTRODUCER,
    153            "_EUCJPMS": TokenType.INTRODUCER,
    154            "_EUCKR": TokenType.INTRODUCER,
    155            "_GB18030": TokenType.INTRODUCER,
    156            "_GB2312": TokenType.INTRODUCER,
    157            "_GBK": TokenType.INTRODUCER,
    158            "_GEOSTD8": TokenType.INTRODUCER,
    159            "_GREEK": TokenType.INTRODUCER,
    160            "_HEBREW": TokenType.INTRODUCER,
    161            "_HP8": TokenType.INTRODUCER,
    162            "_KEYBCS2": TokenType.INTRODUCER,
    163            "_KOI8R": TokenType.INTRODUCER,
    164            "_KOI8U": TokenType.INTRODUCER,
    165            "_LATIN1": TokenType.INTRODUCER,
    166            "_LATIN2": TokenType.INTRODUCER,
    167            "_LATIN5": TokenType.INTRODUCER,
    168            "_LATIN7": TokenType.INTRODUCER,
    169            "_MACCE": TokenType.INTRODUCER,
    170            "_MACROMAN": TokenType.INTRODUCER,
    171            "_SJIS": TokenType.INTRODUCER,
    172            "_SWE7": TokenType.INTRODUCER,
    173            "_TIS620": TokenType.INTRODUCER,
    174            "_UCS2": TokenType.INTRODUCER,
    175            "_UJIS": TokenType.INTRODUCER,
    176            # https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
    177            "_UTF8": TokenType.INTRODUCER,
    178            "_UTF16": TokenType.INTRODUCER,
    179            "_UTF16LE": TokenType.INTRODUCER,
    180            "_UTF32": TokenType.INTRODUCER,
    181            "_UTF8MB3": TokenType.INTRODUCER,
    182            "_UTF8MB4": TokenType.INTRODUCER,
    183            "@@": TokenType.SESSION_PARAMETER,
    184        }
    185
    186        COMMANDS = tokens.Tokenizer.COMMANDS - {TokenType.SHOW}

QUOTES = ["'", '"']
COMMENTS = ['--', '#', ('/*', '*/')]
IDENTIFIERS = ['`']
STRING_ESCAPES = ["'", '\\']
BIT_STRINGS = [("b'", "'"), ("B'", "'"), ('0b', '')]
HEX_STRINGS = [("x'", "'"), ("X'", "'"), ('0x', '')]
KEYWORDS = {'{%': <TokenType.BLOCK_START: 'BLOCK_START'>, '{%+': <TokenType.BLOCK_START: 'BLOCK_START'>, '{%-': <TokenType.BLOCK_START: 'BLOCK_START'>, '%}': <TokenType.BLOCK_END: 'BLOCK_END'>, '+%}': <TokenType.BLOCK_END: 'BLOCK_END'>, '-%}': <TokenType.BLOCK_END: 'BLOCK_END'>, '{{+': <TokenType.BLOCK_START: 'BLOCK_START'>, '{{-': <TokenType.BLOCK_START: 'BLOCK_START'>, '+}}': <TokenType.BLOCK_END: 'BLOCK_END'>, '-}}': <TokenType.BLOCK_END: 'BLOCK_END'>, '/*+': <TokenType.HINT: 'HINT'>, '==': <TokenType.EQ: 'EQ'>, '::': <TokenType.DCOLON: 'DCOLON'>, '||': <TokenType.DPIPE: 'DPIPE'>, '>=': <TokenType.GTE: 'GTE'>, '<=': <TokenType.LTE: 'LTE'>, '<>': <TokenType.NEQ: 'NEQ'>, '!=': <TokenType.NEQ: 'NEQ'>, '<=>': <TokenType.NULLSAFE_EQ: 'NULLSAFE_EQ'>, '->': <TokenType.ARROW: 'ARROW'>, '->>': <TokenType.DARROW: 'DARROW'>, '=>': <TokenType.FARROW: 'FARROW'>, '#>': <TokenType.HASH_ARROW: 'HASH_ARROW'>, '#>>': <TokenType.DHASH_ARROW: 'DHASH_ARROW'>, '<->': <TokenType.LR_ARROW: 'LR_ARROW'>, '&&': <TokenType.DAMP: 'DAMP'>, 'ALL': <TokenType.ALL: 'ALL'>, 'ALWAYS': <TokenType.ALWAYS: 'ALWAYS'>, 'AND': <TokenType.AND: 'AND'>, 'ANTI': <TokenType.ANTI: 'ANTI'>, 'ANY': <TokenType.ANY: 'ANY'>, 'ASC': <TokenType.ASC: 'ASC'>, 'AS': <TokenType.ALIAS: 'ALIAS'>, 'ASOF': <TokenType.ASOF: 'ASOF'>, 'AUTOINCREMENT': <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, 'AUTO_INCREMENT': <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, 'BEGIN': <TokenType.BEGIN: 'BEGIN'>, 'BETWEEN': <TokenType.BETWEEN: 'BETWEEN'>, 'CACHE': <TokenType.CACHE: 'CACHE'>, 'UNCACHE': <TokenType.UNCACHE: 'UNCACHE'>, 'CASE': <TokenType.CASE: 'CASE'>, 'CHARACTER SET': <TokenType.CHARACTER_SET: 'CHARACTER_SET'>, 'CLUSTER BY': <TokenType.CLUSTER_BY: 'CLUSTER_BY'>, 'COLLATE': <TokenType.COLLATE: 'COLLATE'>, 'COLUMN': <TokenType.COLUMN: 'COLUMN'>, 'COMMIT': <TokenType.COMMIT: 'COMMIT'>, 'CONSTRAINT': <TokenType.CONSTRAINT: 'CONSTRAINT'>, 'CREATE': <TokenType.CREATE: 'CREATE'>, 'CROSS': <TokenType.CROSS: 'CROSS'>, 'CUBE': <TokenType.CUBE: 'CUBE'>, 'CURRENT_DATE': <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, 'CURRENT_TIME': <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, 'CURRENT_TIMESTAMP': <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>, 'CURRENT_USER': <TokenType.CURRENT_USER: 'CURRENT_USER'>, 'DATABASE': <TokenType.DATABASE: 'DATABASE'>, 'DEFAULT': <TokenType.DEFAULT: 'DEFAULT'>, 'DELETE': <TokenType.DELETE: 'DELETE'>, 'DESC': <TokenType.DESC: 'DESC'>, 'DESCRIBE': <TokenType.DESCRIBE: 'DESCRIBE'>, 'DISTINCT': <TokenType.DISTINCT: 'DISTINCT'>, 'DISTRIBUTE BY': <TokenType.DISTRIBUTE_BY: 'DISTRIBUTE_BY'>, 'DIV': <TokenType.DIV: 'DIV'>, 'DROP': <TokenType.DROP: 'DROP'>, 'ELSE': <TokenType.ELSE: 'ELSE'>, 'END': <TokenType.END: 'END'>, 'ESCAPE': <TokenType.ESCAPE: 'ESCAPE'>, 'EXCEPT': <TokenType.EXCEPT: 'EXCEPT'>, 'EXECUTE': <TokenType.EXECUTE: 'EXECUTE'>, 'EXISTS': <TokenType.EXISTS: 'EXISTS'>, 'FALSE': <TokenType.FALSE: 'FALSE'>, 'FETCH': <TokenType.FETCH: 'FETCH'>, 'FILTER': <TokenType.FILTER: 'FILTER'>, 'FIRST': <TokenType.FIRST: 'FIRST'>, 'FULL': <TokenType.FULL: 'FULL'>, 'FUNCTION': <TokenType.FUNCTION: 'FUNCTION'>, 'FOR': <TokenType.FOR: 'FOR'>, 'FOREIGN KEY': <TokenType.FOREIGN_KEY: 'FOREIGN_KEY'>, 'FORMAT': <TokenType.FORMAT: 'FORMAT'>, 'FROM': <TokenType.FROM: 'FROM'>, 'GEOGRAPHY': <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, 'GEOMETRY': <TokenType.GEOMETRY: 'GEOMETRY'>, 'GLOB': <TokenType.GLOB: 'GLOB'>, 'GROUP BY': <TokenType.GROUP_BY: 'GROUP_BY'>, 'GROUPING SETS': <TokenType.GROUPING_SETS: 'GROUPING_SETS'>, 'HAVING': <TokenType.HAVING: 'HAVING'>, 'IF': <TokenType.IF: 'IF'>, 'ILIKE': <TokenType.ILIKE: 'ILIKE'>, 'IN': <TokenType.IN: 'IN'>, 'INDEX': <TokenType.INDEX: 'INDEX'>, 'INET': <TokenType.INET: 'INET'>, 'INNER': <TokenType.INNER: 'INNER'>, 'INSERT': <TokenType.INSERT: 'INSERT'>, 'INTERVAL': <TokenType.INTERVAL: 'INTERVAL'>, 'INTERSECT': <TokenType.INTERSECT: 'INTERSECT'>, 'INTO': <TokenType.INTO: 'INTO'>, 'IS': <TokenType.IS: 'IS'>, 'ISNULL': <TokenType.ISNULL: 'ISNULL'>, 'JOIN': <TokenType.JOIN: 'JOIN'>, 'KEEP': <TokenType.KEEP: 'KEEP'>, 'LATERAL': <TokenType.LATERAL: 'LATERAL'>, 'LEFT': <TokenType.LEFT: 'LEFT'>, 'LIKE': <TokenType.LIKE: 'LIKE'>, 'LIMIT': <TokenType.LIMIT: 'LIMIT'>, 'LOAD': <TokenType.LOAD: 'LOAD'>, 'LOCK': <TokenType.LOCK: 'LOCK'>, 'MERGE': <TokenType.MERGE: 'MERGE'>, 'NATURAL': <TokenType.NATURAL: 'NATURAL'>, 'NEXT': <TokenType.NEXT: 'NEXT'>, 'NEXT VALUE FOR': <TokenType.NEXT_VALUE_FOR: 'NEXT_VALUE_FOR'>, 'NOT': <TokenType.NOT: 'NOT'>, 'NOTNULL': <TokenType.NOTNULL: 'NOTNULL'>, 'NULL': <TokenType.NULL: 'NULL'>, 'OBJECT': <TokenType.OBJECT: 'OBJECT'>, 'OFFSET': <TokenType.OFFSET: 'OFFSET'>, 'ON': <TokenType.ON: 'ON'>, 'OR': <TokenType.OR: 'OR'>, 'ORDER BY': <TokenType.ORDER_BY: 'ORDER_BY'>, 'ORDINALITY': <TokenType.ORDINALITY: 'ORDINALITY'>, 'OUTER': <TokenType.OUTER: 'OUTER'>, 'OVER': <TokenType.OVER: 'OVER'>, 'OVERLAPS': <TokenType.OVERLAPS: 'OVERLAPS'>, 'OVERWRITE': <TokenType.OVERWRITE: 'OVERWRITE'>, 'PARTITION': <TokenType.PARTITION: 'PARTITION'>, 'PARTITION BY': <TokenType.PARTITION_BY: 'PARTITION_BY'>, 'PARTITIONED BY': <TokenType.PARTITION_BY: 'PARTITION_BY'>, 'PARTITIONED_BY': <TokenType.PARTITION_BY: 'PARTITION_BY'>, 'PERCENT': <TokenType.PERCENT: 'PERCENT'>, 'PIVOT': <TokenType.PIVOT: 'PIVOT'>, 'PRAGMA': <TokenType.PRAGMA: 'PRAGMA'>, 'PRIMARY KEY': <TokenType.PRIMARY_KEY: 'PRIMARY_KEY'>, 'PROCEDURE': <TokenType.PROCEDURE: 'PROCEDURE'>, 'QUALIFY': <TokenType.QUALIFY: 'QUALIFY'>, 'RANGE': <TokenType.RANGE: 'RANGE'>, 'RECURSIVE': <TokenType.RECURSIVE: 'RECURSIVE'>, 'REGEXP': <TokenType.RLIKE: 'RLIKE'>, 'REPLACE': <TokenType.REPLACE: 'REPLACE'>, 'RETURNING': <TokenType.RETURNING: 'RETURNING'>, 'REFERENCES': <TokenType.REFERENCES: 'REFERENCES'>, 'RIGHT': <TokenType.RIGHT: 'RIGHT'>, 'RLIKE': <TokenType.RLIKE: 'RLIKE'>, 'ROLLBACK': <TokenType.ROLLBACK: 'ROLLBACK'>, 'ROLLUP': <TokenType.ROLLUP: 'ROLLUP'>, 'ROW': <TokenType.ROW: 'ROW'>, 'ROWS': <TokenType.ROWS: 'ROWS'>, 'SCHEMA': <TokenType.SCHEMA: 'SCHEMA'>, 'SELECT': <TokenType.SELECT: 'SELECT'>, 'SEMI': <TokenType.SEMI: 'SEMI'>, 'SET': <TokenType.SET: 'SET'>, 'SETTINGS': <TokenType.SETTINGS: 'SETTINGS'>, 'SHOW': <TokenType.SHOW: 'SHOW'>, 'SIMILAR TO': <TokenType.SIMILAR_TO: 'SIMILAR_TO'>, 'SOME': <TokenType.SOME: 'SOME'>, 'SORT BY': <TokenType.SORT_BY: 'SORT_BY'>, 'TABLE': <TokenType.TABLE: 'TABLE'>, 'TABLESAMPLE': <TokenType.TABLE_SAMPLE: 'TABLE_SAMPLE'>, 'TEMP': <TokenType.TEMPORARY: 'TEMPORARY'>, 'TEMPORARY': <TokenType.TEMPORARY: 'TEMPORARY'>, 'THEN': <TokenType.THEN: 'THEN'>, 'TRUE': <TokenType.TRUE: 'TRUE'>, 'UNION': <TokenType.UNION: 'UNION'>, 'UNNEST': <TokenType.UNNEST: 'UNNEST'>, 'UNPIVOT': <TokenType.UNPIVOT: 'UNPIVOT'>, 'UPDATE': <TokenType.UPDATE: 'UPDATE'>, 'USE': <TokenType.USE: 'USE'>, 'USING': <TokenType.USING: 'USING'>, 'UUID': <TokenType.UUID: 'UUID'>, 'VALUES': <TokenType.VALUES: 'VALUES'>, 'VIEW': <TokenType.VIEW: 'VIEW'>, 'VOLATILE': <TokenType.VOLATILE: 'VOLATILE'>, 'WHEN': <TokenType.WHEN: 'WHEN'>, 'WHERE': <TokenType.WHERE: 'WHERE'>, 'WINDOW': <TokenType.WINDOW: 'WINDOW'>, 'WITH': <TokenType.WITH: 'WITH'>, 'APPLY': <TokenType.APPLY: 'APPLY'>, 'ARRAY': <TokenType.ARRAY: 'ARRAY'>, 'BIT': <TokenType.BIT: 'BIT'>, 'BOOL': <TokenType.BOOLEAN: 'BOOLEAN'>, 'BOOLEAN': <TokenType.BOOLEAN: 'BOOLEAN'>, 'BYTE': <TokenType.TINYINT: 'TINYINT'>, 'TINYINT': <TokenType.TINYINT: 'TINYINT'>, 'SHORT': <TokenType.SMALLINT: 'SMALLINT'>, 'SMALLINT': <TokenType.SMALLINT: 'SMALLINT'>, 'INT2': <TokenType.SMALLINT: 'SMALLINT'>, 'INTEGER': <TokenType.INT: 'INT'>, 'INT': <TokenType.INT: 'INT'>, 'INT4': <TokenType.INT: 'INT'>, 'LONG': <TokenType.BIGINT: 'BIGINT'>, 'BIGINT': <TokenType.BIGINT: 'BIGINT'>, 'INT8': <TokenType.BIGINT: 'BIGINT'>, 'DEC': <TokenType.DECIMAL: 'DECIMAL'>, 'DECIMAL': <TokenType.DECIMAL: 'DECIMAL'>, 'BIGDECIMAL': <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, 'BIGNUMERIC': <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, 'MAP': <TokenType.MAP: 'MAP'>, 'NULLABLE': <TokenType.NULLABLE: 'NULLABLE'>, 'NUMBER': <TokenType.DECIMAL: 'DECIMAL'>, 'NUMERIC': <TokenType.DECIMAL: 'DECIMAL'>, 'FIXED': <TokenType.DECIMAL: 'DECIMAL'>, 'REAL': <TokenType.FLOAT: 'FLOAT'>, 'FLOAT': <TokenType.FLOAT: 'FLOAT'>, 'FLOAT4': <TokenType.FLOAT: 'FLOAT'>, 'FLOAT8': <TokenType.DOUBLE: 'DOUBLE'>, 'DOUBLE': <TokenType.DOUBLE: 'DOUBLE'>, 'DOUBLE PRECISION': <TokenType.DOUBLE: 'DOUBLE'>, 'JSON': <TokenType.JSON: 'JSON'>, 'CHAR': <TokenType.CHAR: 'CHAR'>, 'CHARACTER': <TokenType.CHAR: 'CHAR'>, 'NCHAR': <TokenType.NCHAR: 'NCHAR'>, 'VARCHAR': <TokenType.VARCHAR: 'VARCHAR'>, 'VARCHAR2': <TokenType.VARCHAR: 'VARCHAR'>, 'NVARCHAR': <TokenType.NVARCHAR: 'NVARCHAR'>, 'NVARCHAR2': <TokenType.NVARCHAR: 'NVARCHAR'>, 'STR': <TokenType.TEXT: 'TEXT'>, 'STRING': <TokenType.TEXT: 'TEXT'>, 'TEXT': <TokenType.TEXT: 'TEXT'>, 'CLOB': <TokenType.TEXT: 'TEXT'>, 'LONGVARCHAR': <TokenType.TEXT: 'TEXT'>, 'BINARY': <TokenType.BINARY: 'BINARY'>, 'BLOB': <TokenType.VARBINARY: 'VARBINARY'>, 'BYTEA': <TokenType.VARBINARY: 'VARBINARY'>, 'VARBINARY': <TokenType.VARBINARY: 'VARBINARY'>, 'TIME': <TokenType.TIME: 'TIME'>, 'TIMESTAMP': <TokenType.TIMESTAMP: 'TIMESTAMP'>, 'TIMESTAMPTZ': <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, 'TIMESTAMPLTZ': <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, 'DATE': <TokenType.DATE: 'DATE'>, 'DATETIME': <TokenType.DATETIME: 'DATETIME'>, 'INT4RANGE': <TokenType.INT4RANGE: 'INT4RANGE'>, 'INT4MULTIRANGE': <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, 'INT8RANGE': <TokenType.INT8RANGE: 'INT8RANGE'>, 'INT8MULTIRANGE': <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, 'NUMRANGE': <TokenType.NUMRANGE: 'NUMRANGE'>, 'NUMMULTIRANGE': <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, 'TSRANGE': <TokenType.TSRANGE: 'TSRANGE'>, 'TSMULTIRANGE': <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, 'TSTZRANGE': <TokenType.TSTZRANGE: 'TSTZRANGE'>, 'TSTZMULTIRANGE': <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, 'DATERANGE': <TokenType.DATERANGE: 'DATERANGE'>, 'DATEMULTIRANGE': <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, 'UNIQUE': <TokenType.UNIQUE: 'UNIQUE'>, 'STRUCT': <TokenType.STRUCT: 'STRUCT'>, 'VARIANT': <TokenType.VARIANT: 'VARIANT'>, 'ALTER': <TokenType.ALTER: 'ALTER'>, 'ANALYZE': <TokenType.COMMAND: 'COMMAND'>, 'CALL': <TokenType.COMMAND: 'COMMAND'>, 'COMMENT': <TokenType.COMMENT: 'COMMENT'>, 'COPY': <TokenType.COMMAND: 'COMMAND'>, 'EXPLAIN': <TokenType.COMMAND: 'COMMAND'>, 'GRANT': <TokenType.COMMAND: 'COMMAND'>, 'OPTIMIZE': <TokenType.COMMAND: 'COMMAND'>, 'PREPARE': <TokenType.COMMAND: 'COMMAND'>, 'TRUNCATE': <TokenType.COMMAND: 'COMMAND'>, 'VACUUM': <TokenType.COMMAND: 'COMMAND'>, 'USER-DEFINED': <TokenType.USERDEFINED: 'USERDEFINED'>, 'CHARSET': <TokenType.CHARACTER_SET: 'CHARACTER_SET'>, 'FORCE': <TokenType.FORCE: 'FORCE'>, 'IGNORE': <TokenType.IGNORE: 'IGNORE'>, 'LONGBLOB': <TokenType.LONGBLOB: 'LONGBLOB'>, 'LONGTEXT': <TokenType.LONGTEXT: 'LONGTEXT'>, 'MEDIUMBLOB': <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, 'MEDIUMTEXT': <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, 'SEPARATOR': <TokenType.SEPARATOR: 'SEPARATOR'>, 'ENUM': <TokenType.ENUM: 'ENUM'>, 'START': <TokenType.BEGIN: 'BEGIN'>, 'SIGNED': <TokenType.BIGINT: 'BIGINT'>, 'SIGNED INTEGER': <TokenType.BIGINT: 'BIGINT'>, 'UNSIGNED': <TokenType.UBIGINT: 'UBIGINT'>, 'UNSIGNED INTEGER': <TokenType.UBIGINT: 'UBIGINT'>, '_ARMSCII8': <TokenType.INTRODUCER: 'INTRODUCER'>, '_ASCII': <TokenType.INTRODUCER: 'INTRODUCER'>, '_BIG5': <TokenType.INTRODUCER: 'INTRODUCER'>, '_BINARY': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP1250': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP1251': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP1256': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP1257': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP850': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP852': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP866': <TokenType.INTRODUCER: 'INTRODUCER'>, '_CP932': <TokenType.INTRODUCER: 'INTRODUCER'>, '_DEC8': <TokenType.INTRODUCER: 'INTRODUCER'>, '_EUCJPMS': <TokenType.INTRODUCER: 'INTRODUCER'>, '_EUCKR': <TokenType.INTRODUCER: 'INTRODUCER'>, '_GB18030': <TokenType.INTRODUCER: 'INTRODUCER'>, '_GB2312': <TokenType.INTRODUCER: 'INTRODUCER'>, '_GBK': <TokenType.INTRODUCER: 'INTRODUCER'>, '_GEOSTD8': <TokenType.INTRODUCER: 'INTRODUCER'>, '_GREEK': <TokenType.INTRODUCER: 'INTRODUCER'>, '_HEBREW': <TokenType.INTRODUCER: 'INTRODUCER'>, '_HP8': <TokenType.INTRODUCER: 'INTRODUCER'>, '_KEYBCS2': <TokenType.INTRODUCER: 'INTRODUCER'>, '_KOI8R': <TokenType.INTRODUCER: 'INTRODUCER'>, '_KOI8U': <TokenType.INTRODUCER: 'INTRODUCER'>, '_LATIN1': <TokenType.INTRODUCER: 'INTRODUCER'>, '_LATIN2': <TokenType.INTRODUCER: 'INTRODUCER'>, '_LATIN5': <TokenType.INTRODUCER: 'INTRODUCER'>, '_LATIN7': <TokenType.INTRODUCER: 'INTRODUCER'>, '_MACCE': <TokenType.INTRODUCER: 'INTRODUCER'>, '_MACROMAN': <TokenType.INTRODUCER: 'INTRODUCER'>, '_SJIS': <TokenType.INTRODUCER: 'INTRODUCER'>, '_SWE7': <TokenType.INTRODUCER: 'INTRODUCER'>, '_TIS620': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UCS2': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UJIS': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UTF8': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UTF16': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UTF16LE': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UTF32': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UTF8MB3': <TokenType.INTRODUCER: 'INTRODUCER'>, '_UTF8MB4': <TokenType.INTRODUCER: 'INTRODUCER'>, '@@': <TokenType.SESSION_PARAMETER: 'SESSION_PARAMETER'>}
COMMANDS = {<TokenType.EXECUTE: 'EXECUTE'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.FETCH: 'FETCH'>}

##### Inherited Members

sqlglot.tokens.Tokenizer
  SINGLE_TOKENS
  BYTE_STRINGS
  RAW_STRINGS
  IDENTIFIER_ESCAPES
  VAR_SINGLE_TOKENS
  IDENTIFIERS_CAN_START_WITH_DIGIT
  WHITE_SPACE
  COMMAND_PREFIX_TOKENS
  NUMERIC_LITERALS
  ENCODE
  reset
  tokenize
  peek
  size
  sql
  tokens

class MySQL.Parser(sqlglot.parser.Parser): View Source

    188    class Parser(parser.Parser):
    189        FUNC_TOKENS = {*parser.Parser.FUNC_TOKENS, TokenType.SCHEMA, TokenType.DATABASE}
    190        TABLE_ALIAS_TOKENS = (
    191            parser.Parser.TABLE_ALIAS_TOKENS - parser.Parser.TABLE_INDEX_HINT_TOKENS
    192        )
    193
    194        FUNCTIONS = {
    195            **parser.Parser.FUNCTIONS,
    196            "DATE_ADD": parse_date_delta_with_interval(exp.DateAdd),
    197            "DATE_FORMAT": format_time_lambda(exp.TimeToStr, "mysql"),
    198            "DATE_SUB": parse_date_delta_with_interval(exp.DateSub),
    199            "INSTR": lambda args: exp.StrPosition(substr=seq_get(args, 1), this=seq_get(args, 0)),
    200            "LOCATE": locate_to_strposition,
    201            "STR_TO_DATE": _str_to_date,
    202        }
    203
    204        FUNCTION_PARSERS = {
    205            **parser.Parser.FUNCTION_PARSERS,
    206            "GROUP_CONCAT": lambda self: self.expression(
    207                exp.GroupConcat,
    208                this=self._parse_lambda(),
    209                separator=self._match(TokenType.SEPARATOR) and self._parse_field(),
    210            ),
    211        }
    212
    213        STATEMENT_PARSERS = {
    214            **parser.Parser.STATEMENT_PARSERS,
    215            TokenType.SHOW: lambda self: self._parse_show(),
    216        }
    217
    218        SHOW_PARSERS = {
    219            "BINARY LOGS": _show_parser("BINARY LOGS"),
    220            "MASTER LOGS": _show_parser("BINARY LOGS"),
    221            "BINLOG EVENTS": _show_parser("BINLOG EVENTS"),
    222            "CHARACTER SET": _show_parser("CHARACTER SET"),
    223            "CHARSET": _show_parser("CHARACTER SET"),
    224            "COLLATION": _show_parser("COLLATION"),
    225            "FULL COLUMNS": _show_parser("COLUMNS", target="FROM", full=True),
    226            "COLUMNS": _show_parser("COLUMNS", target="FROM"),
    227            "CREATE DATABASE": _show_parser("CREATE DATABASE", target=True),
    228            "CREATE EVENT": _show_parser("CREATE EVENT", target=True),
    229            "CREATE FUNCTION": _show_parser("CREATE FUNCTION", target=True),
    230            "CREATE PROCEDURE": _show_parser("CREATE PROCEDURE", target=True),
    231            "CREATE TABLE": _show_parser("CREATE TABLE", target=True),
    232            "CREATE TRIGGER": _show_parser("CREATE TRIGGER", target=True),
    233            "CREATE VIEW": _show_parser("CREATE VIEW", target=True),
    234            "DATABASES": _show_parser("DATABASES"),
    235            "ENGINE": _show_parser("ENGINE", target=True),
    236            "STORAGE ENGINES": _show_parser("ENGINES"),
    237            "ENGINES": _show_parser("ENGINES"),
    238            "ERRORS": _show_parser("ERRORS"),
    239            "EVENTS": _show_parser("EVENTS"),
    240            "FUNCTION CODE": _show_parser("FUNCTION CODE", target=True),
    241            "FUNCTION STATUS": _show_parser("FUNCTION STATUS"),
    242            "GRANTS": _show_parser("GRANTS", target="FOR"),
    243            "INDEX": _show_parser("INDEX", target="FROM"),
    244            "MASTER STATUS": _show_parser("MASTER STATUS"),
    245            "OPEN TABLES": _show_parser("OPEN TABLES"),
    246            "PLUGINS": _show_parser("PLUGINS"),
    247            "PROCEDURE CODE": _show_parser("PROCEDURE CODE", target=True),
    248            "PROCEDURE STATUS": _show_parser("PROCEDURE STATUS"),
    249            "PRIVILEGES": _show_parser("PRIVILEGES"),
    250            "FULL PROCESSLIST": _show_parser("PROCESSLIST", full=True),
    251            "PROCESSLIST": _show_parser("PROCESSLIST"),
    252            "PROFILE": _show_parser("PROFILE"),
    253            "PROFILES": _show_parser("PROFILES"),
    254            "RELAYLOG EVENTS": _show_parser("RELAYLOG EVENTS"),
    255            "REPLICAS": _show_parser("REPLICAS"),
    256            "SLAVE HOSTS": _show_parser("REPLICAS"),
    257            "REPLICA STATUS": _show_parser("REPLICA STATUS"),
    258            "SLAVE STATUS": _show_parser("REPLICA STATUS"),
    259            "GLOBAL STATUS": _show_parser("STATUS", global_=True),
    260            "SESSION STATUS": _show_parser("STATUS"),
    261            "STATUS": _show_parser("STATUS"),
    262            "TABLE STATUS": _show_parser("TABLE STATUS"),
    263            "FULL TABLES": _show_parser("TABLES", full=True),
    264            "TABLES": _show_parser("TABLES"),
    265            "TRIGGERS": _show_parser("TRIGGERS"),
    266            "GLOBAL VARIABLES": _show_parser("VARIABLES", global_=True),
    267            "SESSION VARIABLES": _show_parser("VARIABLES"),
    268            "VARIABLES": _show_parser("VARIABLES"),
    269            "WARNINGS": _show_parser("WARNINGS"),
    270        }
    271
    272        SET_PARSERS = {
    273            **parser.Parser.SET_PARSERS,
    274            "PERSIST": lambda self: self._parse_set_item_assignment("PERSIST"),
    275            "PERSIST_ONLY": lambda self: self._parse_set_item_assignment("PERSIST_ONLY"),
    276            "CHARACTER SET": lambda self: self._parse_set_item_charset("CHARACTER SET"),
    277            "CHARSET": lambda self: self._parse_set_item_charset("CHARACTER SET"),
    278            "NAMES": lambda self: self._parse_set_item_names(),
    279        }
    280
    281        PROFILE_TYPES = {
    282            "ALL",
    283            "BLOCK IO",
    284            "CONTEXT SWITCHES",
    285            "CPU",
    286            "IPC",
    287            "MEMORY",
    288            "PAGE FAULTS",
    289            "SOURCE",
    290            "SWAPS",
    291        }
    292
    293        TYPE_TOKENS = {
    294            *parser.Parser.TYPE_TOKENS,
    295            TokenType.SET,
    296        }
    297
    298        ENUM_TYPE_TOKENS = {
    299            *parser.Parser.ENUM_TYPE_TOKENS,
    300            TokenType.SET,
    301        }
    302
    303        LOG_DEFAULTS_TO_LN = True
    304
    305        def _parse_show_mysql(
    306            self,
    307            this: str,
    308            target: bool | str = False,
    309            full: t.Optional[bool] = None,
    310            global_: t.Optional[bool] = None,
    311        ) -> exp.Show:
    312            if target:
    313                if isinstance(target, str):
    314                    self._match_text_seq(target)
    315                target_id = self._parse_id_var()
    316            else:
    317                target_id = None
    318
    319            log = self._parse_string() if self._match_text_seq("IN") else None
    320
    321            if this in {"BINLOG EVENTS", "RELAYLOG EVENTS"}:
    322                position = self._parse_number() if self._match_text_seq("FROM") else None
    323                db = None
    324            else:
    325                position = None
    326                db = None
    327
    328                if self._match(TokenType.FROM):
    329                    db = self._parse_id_var()
    330                elif self._match(TokenType.DOT):
    331                    db = target_id
    332                    target_id = self._parse_id_var()
    333
    334            channel = self._parse_id_var() if self._match_text_seq("FOR", "CHANNEL") else None
    335
    336            like = self._parse_string() if self._match_text_seq("LIKE") else None
    337            where = self._parse_where()
    338
    339            if this == "PROFILE":
    340                types = self._parse_csv(lambda: self._parse_var_from_options(self.PROFILE_TYPES))
    341                query = self._parse_number() if self._match_text_seq("FOR", "QUERY") else None
    342                offset = self._parse_number() if self._match_text_seq("OFFSET") else None
    343                limit = self._parse_number() if self._match_text_seq("LIMIT") else None
    344            else:
    345                types, query = None, None
    346                offset, limit = self._parse_oldstyle_limit()
    347
    348            mutex = True if self._match_text_seq("MUTEX") else None
    349            mutex = False if self._match_text_seq("STATUS") else mutex
    350
    351            return self.expression(
    352                exp.Show,
    353                this=this,
    354                target=target_id,
    355                full=full,
    356                log=log,
    357                position=position,
    358                db=db,
    359                channel=channel,
    360                like=like,
    361                where=where,
    362                types=types,
    363                query=query,
    364                offset=offset,
    365                limit=limit,
    366                mutex=mutex,
    367                **{"global": global_},  # type: ignore
    368            )
    369
    370        def _parse_oldstyle_limit(
    371            self,
    372        ) -> t.Tuple[t.Optional[exp.Expression], t.Optional[exp.Expression]]:
    373            limit = None
    374            offset = None
    375            if self._match_text_seq("LIMIT"):
    376                parts = self._parse_csv(self._parse_number)
    377                if len(parts) == 1:
    378                    limit = parts[0]
    379                elif len(parts) == 2:
    380                    limit = parts[1]
    381                    offset = parts[0]
    382
    383            return offset, limit
    384
    385        def _parse_set_item_charset(self, kind: str) -> exp.Expression:
    386            this = self._parse_string() or self._parse_id_var()
    387            return self.expression(exp.SetItem, this=this, kind=kind)
    388
    389        def _parse_set_item_names(self) -> exp.Expression:
    390            charset = self._parse_string() or self._parse_id_var()
    391            if self._match_text_seq("COLLATE"):
    392                collate = self._parse_string() or self._parse_id_var()
    393            else:
    394                collate = None
    395
    396            return self.expression(exp.SetItem, this=charset, collate=collate, kind="NAMES")

Parser consumes a list of tokens produced by the Tokenizer and produces a parsed syntax tree.

###### Arguments:

- error_level: The desired error level. Default: ErrorLevel.IMMEDIATE
- error_message_context: Determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 100
- max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3

FUNC_TOKENS = {<TokenType.UNNEST: 'UNNEST'>, <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, <TokenType.FORMAT: 'FORMAT'>, <TokenType.CURRENT_DATETIME: 'CURRENT_DATETIME'>, <TokenType.USMALLINT: 'USMALLINT'>, <TokenType.INT: 'INT'>, <TokenType.UUID: 'UUID'>, <TokenType.IMAGE: 'IMAGE'>, <TokenType.SUPER: 'SUPER'>, <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <TokenType.OFFSET: 'OFFSET'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.JSON: 'JSON'>, <TokenType.PSEUDO_TYPE: 'PSEUDO_TYPE'>, <TokenType.MERGE: 'MERGE'>, <TokenType.DATETIME64: 'DATETIME64'>, <TokenType.PRIMARY_KEY: 'PRIMARY_KEY'>, <TokenType.USERDEFINED: 'USERDEFINED'>, <TokenType.SCHEMA: 'SCHEMA'>, <TokenType.SMALLSERIAL: 'SMALLSERIAL'>, <TokenType.SOME: 'SOME'>, <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, <TokenType.CHAR: 'CHAR'>, <TokenType.SERIAL: 'SERIAL'>, <TokenType.OBJECT: 'OBJECT'>, <TokenType.IDENTIFIER: 'IDENTIFIER'>, <TokenType.GEOMETRY: 'GEOMETRY'>, <TokenType.FLOAT: 'FLOAT'>, <TokenType.JSONB: 'JSONB'>, <TokenType.LONGTEXT: 'LONGTEXT'>, <TokenType.BIGSERIAL: 'BIGSERIAL'>, <TokenType.NCHAR: 'NCHAR'>, <TokenType.BIT: 'BIT'>, <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <TokenType.EXISTS: 'EXISTS'>, <TokenType.TABLE: 'TABLE'>, <TokenType.ARRAY: 'ARRAY'>, <TokenType.TIME: 'TIME'>, <TokenType.LONGBLOB: 'LONGBLOB'>, <TokenType.ROW: 'ROW'>, <TokenType.MAP: 'MAP'>, <TokenType.DATABASE: 'DATABASE'>, <TokenType.TSTZRANGE: 'TSTZRANGE'>, <TokenType.NULLABLE: 'NULLABLE'>, <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, <TokenType.RIGHT: 'RIGHT'>, <TokenType.INT8RANGE: 'INT8RANGE'>, <TokenType.WINDOW: 'WINDOW'>, <TokenType.VAR: 'VAR'>, <TokenType.MONEY: 'MONEY'>, <TokenType.UTINYINT: 'UTINYINT'>, <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, <TokenType.HSTORE: 'HSTORE'>, <TokenType.UINT128: 'UINT128'>, <TokenType.ANY: 'ANY'>, <TokenType.NVARCHAR: 'NVARCHAR'>, <TokenType.ALL: 'ALL'>, <TokenType.FILTER: 'FILTER'>, <TokenType.TINYINT: 'TINYINT'>, <TokenType.HLLSKETCH: 'HLLSKETCH'>, <TokenType.INDEX: 'INDEX'>, <TokenType.TSRANGE: 'TSRANGE'>, <TokenType.DATE: 'DATE'>, <TokenType.SMALLINT: 'SMALLINT'>, <TokenType.FIRST: 'FIRST'>, <TokenType.XML: 'XML'>, <TokenType.CURRENT_USER: 'CURRENT_USER'>, <TokenType.INET: 'INET'>, <TokenType.DATETIME: 'DATETIME'>, <TokenType.STRUCT: 'STRUCT'>, <TokenType.GLOB: 'GLOB'>, <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, <TokenType.TIMESTAMP: 'TIMESTAMP'>, <TokenType.RANGE: 'RANGE'>, <TokenType.DATERANGE: 'DATERANGE'>, <TokenType.UINT: 'UINT'>, <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, <TokenType.BIGINT: 'BIGINT'>, <TokenType.ENUM: 'ENUM'>, <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, <TokenType.UBIGINT: 'UBIGINT'>, <TokenType.DECIMAL: 'DECIMAL'>, <TokenType.ILIKE: 'ILIKE'>, <TokenType.INTERVAL: 'INTERVAL'>, <TokenType.SMALLMONEY: 'SMALLMONEY'>, <TokenType.TEXT: 'TEXT'>, <TokenType.INT128: 'INT128'>, <TokenType.UINT256: 'UINT256'>, <TokenType.LEFT: 'LEFT'>, <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, <TokenType.BINARY: 'BINARY'>, <TokenType.NUMRANGE: 'NUMRANGE'>, <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, <TokenType.VARBINARY: 'VARBINARY'>, <TokenType.BOOLEAN: 'BOOLEAN'>, <TokenType.REPLACE: 'REPLACE'>, <TokenType.ISNULL: 'ISNULL'>, <TokenType.LIKE: 'LIKE'>, <TokenType.INT256: 'INT256'>, <TokenType.DOUBLE: 'DOUBLE'>, <TokenType.VARIANT: 'VARIANT'>, <TokenType.ROWVERSION: 'ROWVERSION'>, <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, <TokenType.VARCHAR: 'VARCHAR'>, <TokenType.INT4RANGE: 'INT4RANGE'>, <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>, <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>}
TABLE_ALIAS_TOKENS = {<TokenType.IS: 'IS'>, <TokenType.SEMI: 'SEMI'>, <TokenType.FORMAT: 'FORMAT'>, <TokenType.USMALLINT: 'USMALLINT'>, <TokenType.PROCEDURE: 'PROCEDURE'>, <TokenType.SUPER: 'SUPER'>, <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <TokenType.ANTI: 'ANTI'>, <TokenType.DICTIONARY: 'DICTIONARY'>, <TokenType.SHOW: 'SHOW'>, <TokenType.UNPIVOT: 'UNPIVOT'>, <TokenType.UNIQUE: 'UNIQUE'>, <TokenType.MERGE: 'MERGE'>, <TokenType.USERDEFINED: 'USERDEFINED'>, <TokenType.SCHEMA: 'SCHEMA'>, <TokenType.BEGIN: 'BEGIN'>, <TokenType.SERIAL: 'SERIAL'>, <TokenType.OBJECT: 'OBJECT'>, <TokenType.FLOAT: 'FLOAT'>, <TokenType.ASC: 'ASC'>, <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <TokenType.EXISTS: 'EXISTS'>, <TokenType.VIEW: 'VIEW'>, <TokenType.TABLE: 'TABLE'>, <TokenType.ARRAY: 'ARRAY'>, <TokenType.TIME: 'TIME'>, <TokenType.LOAD: 'LOAD'>, <TokenType.ROW: 'ROW'>, <TokenType.DATABASE: 'DATABASE'>, <TokenType.TSTZRANGE: 'TSTZRANGE'>, <TokenType.DEFAULT: 'DEFAULT'>, <TokenType.NULLABLE: 'NULLABLE'>, <TokenType.INT8RANGE: 'INT8RANGE'>, <TokenType.VAR: 'VAR'>, <TokenType.CONSTRAINT: 'CONSTRAINT'>, <TokenType.FUNCTION: 'FUNCTION'>, <TokenType.UTINYINT: 'UTINYINT'>, <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, <TokenType.HSTORE: 'HSTORE'>, <TokenType.ESCAPE: 'ESCAPE'>, <TokenType.COLUMN: 'COLUMN'>, <TokenType.ALL: 'ALL'>, <TokenType.DESC: 'DESC'>, <TokenType.HLLSKETCH: 'HLLSKETCH'>, <TokenType.SMALLINT: 'SMALLINT'>, <TokenType.DATE: 'DATE'>, <TokenType.CASE: 'CASE'>, <TokenType.FIRST: 'FIRST'>, <TokenType.XML: 'XML'>, <TokenType.INET: 'INET'>, <TokenType.DATETIME: 'DATETIME'>, <TokenType.STRUCT: 'STRUCT'>, <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, <TokenType.RANGE: 'RANGE'>, <TokenType.CACHE: 'CACHE'>, <TokenType.NEXT: 'NEXT'>, <TokenType.UINT: 'UINT'>, <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, <TokenType.ENUM: 'ENUM'>, <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, <TokenType.UBIGINT: 'UBIGINT'>, <TokenType.SMALLMONEY: 'SMALLMONEY'>, <TokenType.UINT256: 'UINT256'>, <TokenType.PARTITION: 'PARTITION'>, <TokenType.OVERWRITE: 'OVERWRITE'>, <TokenType.REFERENCES: 'REFERENCES'>, <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, <TokenType.VARBINARY: 'VARBINARY'>, <TokenType.DELETE: 'DELETE'>, <TokenType.INT256: 'INT256'>, <TokenType.ISNULL: 'ISNULL'>, <TokenType.DOUBLE: 'DOUBLE'>, <TokenType.ROWVERSION: 'ROWVERSION'>, <TokenType.VARIANT: 'VARIANT'>, <TokenType.PERCENT: 'PERCENT'>, <TokenType.SET: 'SET'>, <TokenType.VARCHAR: 'VARCHAR'>, <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>, <TokenType.JSON: 'JSON'>, <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>, <TokenType.ROWS: 'ROWS'>, <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, <TokenType.CURRENT_DATETIME: 'CURRENT_DATETIME'>, <TokenType.INT: 'INT'>, <TokenType.UUID: 'UUID'>, <TokenType.IMAGE: 'IMAGE'>, <TokenType.DESCRIBE: 'DESCRIBE'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.PSEUDO_TYPE: 'PSEUDO_TYPE'>, <TokenType.END: 'END'>, <TokenType.DATETIME64: 'DATETIME64'>, <TokenType.SETTINGS: 'SETTINGS'>, <TokenType.SMALLSERIAL: 'SMALLSERIAL'>, <TokenType.SOME: 'SOME'>, <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, <TokenType.CHAR: 'CHAR'>, <TokenType.GEOMETRY: 'GEOMETRY'>, <TokenType.JSONB: 'JSONB'>, <TokenType.LONGTEXT: 'LONGTEXT'>, <TokenType.BIGSERIAL: 'BIGSERIAL'>, <TokenType.NCHAR: 'NCHAR'>, <TokenType.BIT: 'BIT'>, <TokenType.ORDINALITY: 'ORDINALITY'>, <TokenType.COLLATE: 'COLLATE'>, <TokenType.PRAGMA: 'PRAGMA'>, <TokenType.LONGBLOB: 'LONGBLOB'>, <TokenType.MAP: 'MAP'>, <TokenType.TEMPORARY: 'TEMPORARY'>, <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, <TokenType.VOLATILE: 'VOLATILE'>, <TokenType.MONEY: 'MONEY'>, <TokenType.UINT128: 'UINT128'>, <TokenType.ANY: 'ANY'>, <TokenType.NVARCHAR: 'NVARCHAR'>, <TokenType.PIVOT: 'PIVOT'>, <TokenType.FILTER: 'FILTER'>, <TokenType.TINYINT: 'TINYINT'>, <TokenType.INDEX: 'INDEX'>, <TokenType.TSRANGE: 'TSRANGE'>, <TokenType.UPDATE: 'UPDATE'>, <TokenType.CURRENT_USER: 'CURRENT_USER'>, <TokenType.TIMESTAMP: 'TIMESTAMP'>, <TokenType.TOP: 'TOP'>, <TokenType.DATERANGE: 'DATERANGE'>, <TokenType.BIGINT: 'BIGINT'>, <TokenType.DIV: 'DIV'>, <TokenType.DECIMAL: 'DECIMAL'>, <TokenType.INTERVAL: 'INTERVAL'>, <TokenType.TEXT: 'TEXT'>, <TokenType.INT128: 'INT128'>, <TokenType.COMMENT: 'COMMENT'>, <TokenType.COMMIT: 'COMMIT'>, <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, <TokenType.BINARY: 'BINARY'>, <TokenType.IF: 'IF'>, <TokenType.NUMRANGE: 'NUMRANGE'>, <TokenType.KEEP: 'KEEP'>, <TokenType.BOOLEAN: 'BOOLEAN'>, <TokenType.EXECUTE: 'EXECUTE'>, <TokenType.FALSE: 'FALSE'>, <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, <TokenType.INT4RANGE: 'INT4RANGE'>, <TokenType.TRUE: 'TRUE'>}
FUNCTIONS = {'ABS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Abs'>>, 'ANY_VALUE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.AnyValue'>>, 'APPROX_DISTINCT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxDistinct'>>, 'APPROX_COUNT_DISTINCT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxDistinct'>>, 'APPROX_QUANTILE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxQuantile'>>, 'ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Array'>>, 'ARRAY_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayAgg'>>, 'ARRAY_ALL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayAll'>>, 'ARRAY_ANY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayAny'>>, 'ARRAY_CONCAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayConcat'>>, 'ARRAY_CONTAINS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayContains'>>, 'FILTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayFilter'>>, 'ARRAY_FILTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayFilter'>>, 'ARRAY_JOIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayJoin'>>, 'ARRAY_SIZE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySize'>>, 'ARRAY_SORT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySort'>>, 'ARRAY_SUM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySum'>>, 'ARRAY_UNION_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayUnionAgg'>>, 'AVG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Avg'>>, 'CASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Case'>>, 'CAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Cast'>>, 'CAST_TO_STR_TYPE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CastToStrType'>>, 'CEIL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Ceil'>>, 'CEILING': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Ceil'>>, 'COALESCE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'IFNULL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'NVL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'CONCAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Concat'>>, 'CONCAT_WS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ConcatWs'>>, 'COUNT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Count'>>, 'COUNT_IF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CountIf'>>, 'CURRENT_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentDate'>>, 'CURRENT_DATETIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentDatetime'>>, 'CURRENT_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentTime'>>, 'CURRENT_TIMESTAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentTimestamp'>>, 'CURRENT_USER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentUser'>>, 'DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Date'>>, 'DATE_ADD': <function parse_date_delta_with_interval.<locals>.func>, 'DATEDIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateDiff'>>, 'DATE_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateDiff'>>, 'DATEFROMPARTS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateFromParts'>>, 'DATE_STR_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateStrToDate'>>, 'DATE_SUB': <function parse_date_delta_with_interval.<locals>.func>, 'DATE_TO_DATE_STR': <function Parser.<lambda>>, 'DATE_TO_DI': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateToDi'>>, 'DATE_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateTrunc'>>, 'DATETIME_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeAdd'>>, 'DATETIME_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeDiff'>>, 'DATETIME_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeSub'>>, 'DATETIME_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeTrunc'>>, 'DAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Day'>>, 'DAY_OF_MONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfMonth'>>, 'DAYOFMONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfMonth'>>, 'DAY_OF_WEEK': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfWeek'>>, 'DAYOFWEEK': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfWeek'>>, 'DAY_OF_YEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfYear'>>, 'DAYOFYEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfYear'>>, 'DECODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Decode'>>, 'DI_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DiToDate'>>, 'ENCODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Encode'>>, 'EXP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Exp'>>, 'EXPLODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Explode'>>, 'EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Extract'>>, 'FLOOR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Floor'>>, 'FROM_BASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.FromBase'>>, 'FROM_BASE64': <bound method Func.from_arg_list of <class 'sqlglot.expressions.FromBase64'>>, 'GENERATE_SERIES': <bound method Func.from_arg_list of <class 'sqlglot.expressions.GenerateSeries'>>, 'GREATEST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Greatest'>>, 'GROUP_CONCAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.GroupConcat'>>, 'HEX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Hex'>>, 'HLL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Hll'>>, 'IF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.If'>>, 'INITCAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Initcap'>>, 'JSONB_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONBExtract'>>, 'JSONB_EXTRACT_SCALAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONBExtractScalar'>>, 'JSON_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONExtract'>>, 'JSON_EXTRACT_SCALAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONExtractScalar'>>, 'JSON_FORMAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONFormat'>>, 'J_S_O_N_OBJECT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONObject'>>, 'LAST_DATE_OF_MONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LastDateOfMonth'>>, 'LEAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Least'>>, 'LEFT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Left'>>, 'LENGTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Length'>>, 'LEN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Length'>>, 'LEVENSHTEIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Levenshtein'>>, 'LN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Ln'>>, 'LOG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Log'>>, 'LOG10': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Log10'>>, 'LOG2': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Log2'>>, 'LOGICAL_AND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalAnd'>>, 'BOOL_AND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalAnd'>>, 'BOOLAND_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalAnd'>>, 'LOGICAL_OR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalOr'>>, 'BOOL_OR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalOr'>>, 'BOOLOR_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalOr'>>, 'LOWER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Lower'>>, 'LCASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Lower'>>, 'MD5': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MD5'>>, 'MAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Map'>>, 'MAP_FROM_ENTRIES': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MapFromEntries'>>, 'MATCH_AGAINST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MatchAgainst'>>, 'MAX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Max'>>, 'MIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Min'>>, 'MONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Month'>>, 'NEXT_VALUE_FOR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.NextValueFor'>>, 'NUMBER_TO_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.NumberToStr'>>, 'NVL2': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Nvl2'>>, 'OPEN_J_S_O_N': <bound method Func.from_arg_list of <class 'sqlglot.expressions.OpenJSON'>>, 'PARAMETERIZED_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ParameterizedAgg'>>, 'PERCENTILE_CONT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.PercentileCont'>>, 'PERCENTILE_DISC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.PercentileDisc'>>, 'POSEXPLODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Posexplode'>>, 'POWER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Pow'>>, 'POW': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Pow'>>, 'QUANTILE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Quantile'>>, 'RANGE_N': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RangeN'>>, 'READ_CSV': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ReadCSV'>>, 'REDUCE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Reduce'>>, 'REGEXP_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpExtract'>>, 'REGEXP_I_LIKE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpILike'>>, 'REGEXP_LIKE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpLike'>>, 'REGEXP_SPLIT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpSplit'>>, 'REPEAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Repeat'>>, 'RIGHT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Right'>>, 'ROUND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Round'>>, 'ROW_NUMBER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RowNumber'>>, 'SHA': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SHA'>>, 'SHA1': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SHA'>>, 'SHA2': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SHA2'>>, 'SAFE_CONCAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SafeConcat'>>, 'SAFE_DIVIDE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SafeDivide'>>, 'SET_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SetAgg'>>, 'SORT_ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SortArray'>>, 'SPLIT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Split'>>, 'SQRT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Sqrt'>>, 'STANDARD_HASH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StandardHash'>>, 'STAR_MAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StarMap'>>, 'STDDEV': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Stddev'>>, 'STDDEV_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StddevPop'>>, 'STDDEV_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StddevSamp'>>, 'STR_POSITION': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrPosition'>>, 'STR_TO_DATE': <function _str_to_date>, 'STR_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrToTime'>>, 'STR_TO_UNIX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrToUnix'>>, 'STRUCT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Struct'>>, 'STRUCT_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StructExtract'>>, 'SUBSTRING': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Substring'>>, 'SUM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Sum'>>, 'TIME_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeAdd'>>, 'TIME_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeDiff'>>, 'TIME_STR_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeStrToDate'>>, 'TIME_STR_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeStrToTime'>>, 'TIME_STR_TO_UNIX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeStrToUnix'>>, 'TIME_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeSub'>>, 'TIME_TO_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeToStr'>>, 'TIME_TO_TIME_STR': <function Parser.<lambda>>, 'TIME_TO_UNIX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeToUnix'>>, 'TIME_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeTrunc'>>, 'TIMESTAMP_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampAdd'>>, 'TIMESTAMP_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampDiff'>>, 'TIMESTAMP_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampSub'>>, 'TIMESTAMP_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampTrunc'>>, 'TO_BASE64': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToBase64'>>, 'TO_CHAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToChar'>>, 'TRIM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Trim'>>, 'TRY_CAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TryCast'>>, 'TS_OR_DI_TO_DI': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDiToDi'>>, 'TS_OR_DS_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsAdd'>>, 'TS_OR_DS_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsToDate'>>, 'TS_OR_DS_TO_DATE_STR': <function Parser.<lambda>>, 'UNHEX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Unhex'>>, 'UNIX_TO_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixToStr'>>, 'UNIX_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixToTime'>>, 'UNIX_TO_TIME_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixToTimeStr'>>, 'UPPER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Upper'>>, 'UCASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Upper'>>, 'VAR_MAP': <function parse_var_map>, 'VARIANCE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Variance'>>, 'VARIANCE_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Variance'>>, 'VAR_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Variance'>>, 'VARIANCE_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.VariancePop'>>, 'VAR_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.VariancePop'>>, 'WEEK': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Week'>>, 'WEEK_OF_YEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.WeekOfYear'>>, 'WEEKOFYEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.WeekOfYear'>>, 'WHEN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.When'>>, 'X_M_L_TABLE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.XMLTable'>>, 'YEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Year'>>, 'GLOB': <function Parser.<lambda>>, 'LIKE': <function parse_like>, 'DATE_FORMAT': <function format_time_lambda.<locals>._format_time>, 'INSTR': <function MySQL.Parser.<lambda>>, 'LOCATE': <function locate_to_strposition>}
FUNCTION_PARSERS = {'ANY_VALUE': <function Parser.<lambda>>, 'CAST': <function Parser.<lambda>>, 'CONCAT': <function Parser.<lambda>>, 'CONVERT': <function Parser.<lambda>>, 'DECODE': <function Parser.<lambda>>, 'EXTRACT': <function Parser.<lambda>>, 'JSON_OBJECT': <function Parser.<lambda>>, 'LOG': <function Parser.<lambda>>, 'MATCH': <function Parser.<lambda>>, 'OPENJSON': <function Parser.<lambda>>, 'POSITION': <function Parser.<lambda>>, 'SAFE_CAST': <function Parser.<lambda>>, 'STRING_AGG': <function Parser.<lambda>>, 'SUBSTRING': <function Parser.<lambda>>, 'TRIM': <function Parser.<lambda>>, 'TRY_CAST': <function Parser.<lambda>>, 'TRY_CONVERT': <function Parser.<lambda>>, 'GROUP_CONCAT': <function MySQL.Parser.<lambda>>}
STATEMENT_PARSERS = {<TokenType.ALTER: 'ALTER'>: <function Parser.<lambda>>, <TokenType.BEGIN: 'BEGIN'>: <function Parser.<lambda>>, <TokenType.CACHE: 'CACHE'>: <function Parser.<lambda>>, <TokenType.COMMIT: 'COMMIT'>: <function Parser.<lambda>>, <TokenType.COMMENT: 'COMMENT'>: <function Parser.<lambda>>, <TokenType.CREATE: 'CREATE'>: <function Parser.<lambda>>, <TokenType.DELETE: 'DELETE'>: <function Parser.<lambda>>, <TokenType.DESC: 'DESC'>: <function Parser.<lambda>>, <TokenType.DESCRIBE: 'DESCRIBE'>: <function Parser.<lambda>>, <TokenType.DROP: 'DROP'>: <function Parser.<lambda>>, <TokenType.END: 'END'>: <function Parser.<lambda>>, <TokenType.FROM: 'FROM'>: <function Parser.<lambda>>, <TokenType.INSERT: 'INSERT'>: <function Parser.<lambda>>, <TokenType.LOAD: 'LOAD'>: <function Parser.<lambda>>, <TokenType.MERGE: 'MERGE'>: <function Parser.<lambda>>, <TokenType.PIVOT: 'PIVOT'>: <function Parser.<lambda>>, <TokenType.PRAGMA: 'PRAGMA'>: <function Parser.<lambda>>, <TokenType.ROLLBACK: 'ROLLBACK'>: <function Parser.<lambda>>, <TokenType.SET: 'SET'>: <function Parser.<lambda>>, <TokenType.UNCACHE: 'UNCACHE'>: <function Parser.<lambda>>, <TokenType.UPDATE: 'UPDATE'>: <function Parser.<lambda>>, <TokenType.USE: 'USE'>: <function Parser.<lambda>>, <TokenType.SHOW: 'SHOW'>: <function MySQL.Parser.<lambda>>}
SHOW_PARSERS = {'BINARY LOGS': <function _show_parser.<locals>._parse>, 'MASTER LOGS': <function _show_parser.<locals>._parse>, 'BINLOG EVENTS': <function _show_parser.<locals>._parse>, 'CHARACTER SET': <function _show_parser.<locals>._parse>, 'CHARSET': <function _show_parser.<locals>._parse>, 'COLLATION': <function _show_parser.<locals>._parse>, 'FULL COLUMNS': <function _show_parser.<locals>._parse>, 'COLUMNS': <function _show_parser.<locals>._parse>, 'CREATE DATABASE': <function _show_parser.<locals>._parse>, 'CREATE EVENT': <function _show_parser.<locals>._parse>, 'CREATE FUNCTION': <function _show_parser.<locals>._parse>, 'CREATE PROCEDURE': <function _show_parser.<locals>._parse>, 'CREATE TABLE': <function _show_parser.<locals>._parse>, 'CREATE TRIGGER': <function _show_parser.<locals>._parse>, 'CREATE VIEW': <function _show_parser.<locals>._parse>, 'DATABASES': <function _show_parser.<locals>._parse>, 'ENGINE': <function _show_parser.<locals>._parse>, 'STORAGE ENGINES': <function _show_parser.<locals>._parse>, 'ENGINES': <function _show_parser.<locals>._parse>, 'ERRORS': <function _show_parser.<locals>._parse>, 'EVENTS': <function _show_parser.<locals>._parse>, 'FUNCTION CODE': <function _show_parser.<locals>._parse>, 'FUNCTION STATUS': <function _show_parser.<locals>._parse>, 'GRANTS': <function _show_parser.<locals>._parse>, 'INDEX': <function _show_parser.<locals>._parse>, 'MASTER STATUS': <function _show_parser.<locals>._parse>, 'OPEN TABLES': <function _show_parser.<locals>._parse>, 'PLUGINS': <function _show_parser.<locals>._parse>, 'PROCEDURE CODE': <function _show_parser.<locals>._parse>, 'PROCEDURE STATUS': <function _show_parser.<locals>._parse>, 'PRIVILEGES': <function _show_parser.<locals>._parse>, 'FULL PROCESSLIST': <function _show_parser.<locals>._parse>, 'PROCESSLIST': <function _show_parser.<locals>._parse>, 'PROFILE': <function _show_parser.<locals>._parse>, 'PROFILES': <function _show_parser.<locals>._parse>, 'RELAYLOG EVENTS': <function _show_parser.<locals>._parse>, 'REPLICAS': <function _show_parser.<locals>._parse>, 'SLAVE HOSTS': <function _show_parser.<locals>._parse>, 'REPLICA STATUS': <function _show_parser.<locals>._parse>, 'SLAVE STATUS': <function _show_parser.<locals>._parse>, 'GLOBAL STATUS': <function _show_parser.<locals>._parse>, 'SESSION STATUS': <function _show_parser.<locals>._parse>, 'STATUS': <function _show_parser.<locals>._parse>, 'TABLE STATUS': <function _show_parser.<locals>._parse>, 'FULL TABLES': <function _show_parser.<locals>._parse>, 'TABLES': <function _show_parser.<locals>._parse>, 'TRIGGERS': <function _show_parser.<locals>._parse>, 'GLOBAL VARIABLES': <function _show_parser.<locals>._parse>, 'SESSION VARIABLES': <function _show_parser.<locals>._parse>, 'VARIABLES': <function _show_parser.<locals>._parse>, 'WARNINGS': <function _show_parser.<locals>._parse>}
SET_PARSERS = {'GLOBAL': <function Parser.<lambda>>, 'LOCAL': <function Parser.<lambda>>, 'SESSION': <function Parser.<lambda>>, 'TRANSACTION': <function Parser.<lambda>>, 'PERSIST': <function MySQL.Parser.<lambda>>, 'PERSIST_ONLY': <function MySQL.Parser.<lambda>>, 'CHARACTER SET': <function MySQL.Parser.<lambda>>, 'CHARSET': <function MySQL.Parser.<lambda>>, 'NAMES': <function MySQL.Parser.<lambda>>}
PROFILE_TYPES = {'ALL', 'IPC', 'PAGE FAULTS', 'MEMORY', 'CONTEXT SWITCHES', 'CPU', 'SWAPS', 'SOURCE', 'BLOCK IO'}
TYPE_TOKENS = {<TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, <TokenType.USMALLINT: 'USMALLINT'>, <TokenType.INT: 'INT'>, <TokenType.UUID: 'UUID'>, <TokenType.IMAGE: 'IMAGE'>, <TokenType.SUPER: 'SUPER'>, <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <TokenType.PSEUDO_TYPE: 'PSEUDO_TYPE'>, <TokenType.DATETIME64: 'DATETIME64'>, <TokenType.USERDEFINED: 'USERDEFINED'>, <TokenType.SMALLSERIAL: 'SMALLSERIAL'>, <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, <TokenType.CHAR: 'CHAR'>, <TokenType.SERIAL: 'SERIAL'>, <TokenType.OBJECT: 'OBJECT'>, <TokenType.GEOMETRY: 'GEOMETRY'>, <TokenType.FLOAT: 'FLOAT'>, <TokenType.JSONB: 'JSONB'>, <TokenType.LONGTEXT: 'LONGTEXT'>, <TokenType.BIGSERIAL: 'BIGSERIAL'>, <TokenType.NCHAR: 'NCHAR'>, <TokenType.BIT: 'BIT'>, <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <TokenType.ARRAY: 'ARRAY'>, <TokenType.TIME: 'TIME'>, <TokenType.LONGBLOB: 'LONGBLOB'>, <TokenType.MAP: 'MAP'>, <TokenType.TSTZRANGE: 'TSTZRANGE'>, <TokenType.NULLABLE: 'NULLABLE'>, <TokenType.INT8RANGE: 'INT8RANGE'>, <TokenType.MONEY: 'MONEY'>, <TokenType.UTINYINT: 'UTINYINT'>, <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, <TokenType.HSTORE: 'HSTORE'>, <TokenType.UINT128: 'UINT128'>, <TokenType.NVARCHAR: 'NVARCHAR'>, <TokenType.TINYINT: 'TINYINT'>, <TokenType.HLLSKETCH: 'HLLSKETCH'>, <TokenType.TSRANGE: 'TSRANGE'>, <TokenType.SMALLINT: 'SMALLINT'>, <TokenType.DATE: 'DATE'>, <TokenType.XML: 'XML'>, <TokenType.INET: 'INET'>, <TokenType.DATETIME: 'DATETIME'>, <TokenType.STRUCT: 'STRUCT'>, <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, <TokenType.TIMESTAMP: 'TIMESTAMP'>, <TokenType.DATERANGE: 'DATERANGE'>, <TokenType.UINT: 'UINT'>, <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, <TokenType.BIGINT: 'BIGINT'>, <TokenType.ENUM: 'ENUM'>, <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, <TokenType.UBIGINT: 'UBIGINT'>, <TokenType.DECIMAL: 'DECIMAL'>, <TokenType.INTERVAL: 'INTERVAL'>, <TokenType.SMALLMONEY: 'SMALLMONEY'>, <TokenType.TEXT: 'TEXT'>, <TokenType.INT128: 'INT128'>, <TokenType.UINT256: 'UINT256'>, <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, <TokenType.BINARY: 'BINARY'>, <TokenType.NUMRANGE: 'NUMRANGE'>, <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, <TokenType.VARBINARY: 'VARBINARY'>, <TokenType.BOOLEAN: 'BOOLEAN'>, <TokenType.INT256: 'INT256'>, <TokenType.DOUBLE: 'DOUBLE'>, <TokenType.ROWVERSION: 'ROWVERSION'>, <TokenType.VARIANT: 'VARIANT'>, <TokenType.SET: 'SET'>, <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, <TokenType.VARCHAR: 'VARCHAR'>, <TokenType.INT4RANGE: 'INT4RANGE'>, <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>, <TokenType.JSON: 'JSON'>}
ENUM_TYPE_TOKENS = {<TokenType.ENUM: 'ENUM'>, <TokenType.SET: 'SET'>}
LOG_DEFAULTS_TO_LN = True
SHOW_TRIE: Dict = {'BINARY': {'LOGS': {0: True}}, 'MASTER': {'LOGS': {0: True}, 'STATUS': {0: True}}, 'BINLOG': {'EVENTS': {0: True}}, 'CHARACTER': {'SET': {0: True}}, 'CHARSET': {0: True}, 'COLLATION': {0: True}, 'FULL': {'COLUMNS': {0: True}, 'PROCESSLIST': {0: True}, 'TABLES': {0: True}}, 'COLUMNS': {0: True}, 'CREATE': {'DATABASE': {0: True}, 'EVENT': {0: True}, 'FUNCTION': {0: True}, 'PROCEDURE': {0: True}, 'TABLE': {0: True}, 'TRIGGER': {0: True}, 'VIEW': {0: True}}, 'DATABASES': {0: True}, 'ENGINE': {0: True}, 'STORAGE': {'ENGINES': {0: True}}, 'ENGINES': {0: True}, 'ERRORS': {0: True}, 'EVENTS': {0: True}, 'FUNCTION': {'CODE': {0: True}, 'STATUS': {0: True}}, 'GRANTS': {0: True}, 'INDEX': {0: True}, 'OPEN': {'TABLES': {0: True}}, 'PLUGINS': {0: True}, 'PROCEDURE': {'CODE': {0: True}, 'STATUS': {0: True}}, 'PRIVILEGES': {0: True}, 'PROCESSLIST': {0: True}, 'PROFILE': {0: True}, 'PROFILES': {0: True}, 'RELAYLOG': {'EVENTS': {0: True}}, 'REPLICAS': {0: True}, 'SLAVE': {'HOSTS': {0: True}, 'STATUS': {0: True}}, 'REPLICA': {'STATUS': {0: True}}, 'GLOBAL': {'STATUS': {0: True}, 'VARIABLES': {0: True}}, 'SESSION': {'STATUS': {0: True}, 'VARIABLES': {0: True}}, 'STATUS': {0: True}, 'TABLE': {'STATUS': {0: True}}, 'TABLES': {0: True}, 'TRIGGERS': {0: True}, 'VARIABLES': {0: True}, 'WARNINGS': {0: True}}
SET_TRIE: Dict = {'GLOBAL': {0: True}, 'LOCAL': {0: True}, 'SESSION': {0: True}, 'TRANSACTION': {0: True}, 'PERSIST': {0: True}, 'PERSIST_ONLY': {0: True}, 'CHARACTER': {'SET': {0: True}}, 'CHARSET': {0: True}, 'NAMES': {0: True}}
FORMAT_TRIE: Dict = {'%': {'M': {0: True}, 'c': {0: True}, 'e': {0: True}, 'h': {0: True}, 'i': {0: True}, 's': {0: True}, 'S': {0: True}, 'u': {0: True}, 'k': {0: True}, 'l': {0: True}, 'T': {0: True}, 'W': {0: True}}}
TIME_MAPPING: Dict[str, str] = {'%M': '%B', '%c': '%-m', '%e': '%-d', '%h': '%I', '%i': '%M', '%s': '%S', '%S': '%S', '%u': '%W', '%k': '%-H', '%l': '%-I', '%T': '%H:%M:%S', '%W': '%a'}
TIME_TRIE: Dict = {'%': {'M': {0: True}, 'c': {0: True}, 'e': {0: True}, 'h': {0: True}, 'i': {0: True}, 's': {0: True}, 'S': {0: True}, 'u': {0: True}, 'k': {0: True}, 'l': {0: True}, 'T': {0: True}, 'W': {0: True}}}

##### Inherited Members

sqlglot.parser.Parser
  Parser
  NO_PAREN_FUNCTIONS
  NESTED_TYPE_TOKENS
  SUBQUERY_PREDICATES
  RESERVED_KEYWORDS
  DB_CREATABLES
  CREATABLES
  ID_VAR_TOKENS
  INTERVAL_VARS
  COMMENT_TABLE_ALIAS_TOKENS
  UPDATE_ALIAS_TOKENS
  TRIM_TYPES
  CONJUNCTION
  EQUALITY
  COMPARISON
  BITWISE
  TERM
  FACTOR
  TIMESTAMPS
  SET_OPERATIONS
  JOIN_METHODS
  JOIN_SIDES
  JOIN_KINDS
  JOIN_HINTS
  LAMBDAS
  COLUMN_OPERATORS
  EXPRESSION_PARSERS
  UNARY_PARSERS
  PRIMARY_PARSERS
  PLACEHOLDER_PARSERS
  RANGE_PARSERS
  PROPERTY_PARSERS
  CONSTRAINT_PARSERS
  ALTER_PARSERS
  SCHEMA_UNNAMED_CONSTRAINTS
  NO_PAREN_FUNCTION_PARSERS
  FUNCTIONS_WITH_ALIASED_ARGS
  QUERY_MODIFIER_PARSERS
  TYPE_LITERAL_PARSERS
  MODIFIABLES
  DDL_SELECT_TOKENS
  PRE_VOLATILE_TOKENS
  TRANSACTION_KIND
  TRANSACTION_CHARACTERISTICS
  INSERT_ALTERNATIVES
  CLONE_KINDS
  TABLE_INDEX_HINT_TOKENS
  WINDOW_ALIAS_TOKENS
  WINDOW_BEFORE_PAREN_TOKENS
  WINDOW_SIDES
  ADD_CONSTRAINT_TOKENS
  STRICT_CAST
  CONCAT_NULL_OUTPUTS_STRING
  PREFIXED_PIVOT_COLUMNS
  IDENTIFY_PIVOT_STRINGS
  LOG_BASE_FIRST
  INDEX_OFFSET
  UNNEST_COLUMN_ONLY
  ALIAS_POST_TABLESAMPLE
  STRICT_STRING_CONCAT
  NULL_ORDERING
  FORMAT_MAPPING
  error_level
  error_message_context
  max_errors
  reset
  parse
  parse_into
  check_errors
  raise_error
  expression
  validate_expression
  errors
  sql

class MySQL.Generator(sqlglot.generator.Generator): View Source

    398    class Generator(generator.Generator):
    399        LOCKING_READS_SUPPORTED = True
    400        NULL_ORDERING_SUPPORTED = False
    401        JOIN_HINTS = False
    402        TABLE_HINTS = True
    403
    404        TRANSFORMS = {
    405            **generator.Generator.TRANSFORMS,
    406            exp.CurrentDate: no_paren_current_date_sql,
    407            exp.DateDiff: lambda self, e: self.func("DATEDIFF", e.this, e.expression),
    408            exp.DateAdd: _date_add_sql("ADD"),
    409            exp.DateStrToDate: datestrtodate_sql,
    410            exp.DateSub: _date_add_sql("SUB"),
    411            exp.DateTrunc: _date_trunc_sql,
    412            exp.DayOfMonth: rename_func("DAYOFMONTH"),
    413            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    414            exp.DayOfYear: rename_func("DAYOFYEAR"),
    415            exp.GroupConcat: lambda self, e: f"""GROUP_CONCAT({self.sql(e, "this")} SEPARATOR {self.sql(e, "separator") or "','"})""",
    416            exp.ILike: no_ilike_sql,
    417            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
    418            exp.Max: max_or_greatest,
    419            exp.Min: min_or_least,
    420            exp.NullSafeEQ: lambda self, e: self.binary(e, "<=>"),
    421            exp.NullSafeNEQ: lambda self, e: self.not_sql(self.binary(e, "<=>")),
    422            exp.Pivot: no_pivot_sql,
    423            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
    424            exp.StrPosition: strposition_to_locate_sql,
    425            exp.StrToDate: _str_to_date_sql,
    426            exp.StrToTime: _str_to_date_sql,
    427            exp.TableSample: no_tablesample_sql,
    428            exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"),
    429            exp.TimeToStr: lambda self, e: self.func("DATE_FORMAT", e.this, self.format_time(e)),
    430            exp.Trim: _trim_sql,
    431            exp.TryCast: no_trycast_sql,
    432            exp.WeekOfYear: rename_func("WEEKOFYEAR"),
    433        }
    434
    435        TYPE_MAPPING = generator.Generator.TYPE_MAPPING.copy()
    436        TYPE_MAPPING.pop(exp.DataType.Type.MEDIUMTEXT)
    437        TYPE_MAPPING.pop(exp.DataType.Type.LONGTEXT)
    438        TYPE_MAPPING.pop(exp.DataType.Type.MEDIUMBLOB)
    439        TYPE_MAPPING.pop(exp.DataType.Type.LONGBLOB)
    440
    441        PROPERTIES_LOCATION = {
    442            **generator.Generator.PROPERTIES_LOCATION,
    443            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
    444            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    445        }
    446
    447        LIMIT_FETCH = "LIMIT"
    448
    449        def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    450            """(U)BIGINT is not allowed in a CAST expression, so we use (UN)SIGNED instead."""
    451            if expression.to.this == exp.DataType.Type.BIGINT:
    452                to = "SIGNED"
    453            elif expression.to.this == exp.DataType.Type.UBIGINT:
    454                to = "UNSIGNED"
    455            else:
    456                return super().cast_sql(expression)
    457
    458            return f"CAST({self.sql(expression, 'this')} AS {to})"
    459
    460        def show_sql(self, expression: exp.Show) -> str:
    461            this = f" {expression.name}"
    462            full = " FULL" if expression.args.get("full") else ""
    463            global_ = " GLOBAL" if expression.args.get("global") else ""
    464
    465            target = self.sql(expression, "target")
    466            target = f" {target}" if target else ""
    467            if expression.name in {"COLUMNS", "INDEX"}:
    468                target = f" FROM{target}"
    469            elif expression.name == "GRANTS":
    470                target = f" FOR{target}"
    471
    472            db = self._prefixed_sql("FROM", expression, "db")
    473
    474            like = self._prefixed_sql("LIKE", expression, "like")
    475            where = self.sql(expression, "where")
    476
    477            types = self.expressions(expression, key="types")
    478            types = f" {types}" if types else types
    479            query = self._prefixed_sql("FOR QUERY", expression, "query")
    480
    481            if expression.name == "PROFILE":
    482                offset = self._prefixed_sql("OFFSET", expression, "offset")
    483                limit = self._prefixed_sql("LIMIT", expression, "limit")
    484            else:
    485                offset = ""
    486                limit = self._oldstyle_limit_sql(expression)
    487
    488            log = self._prefixed_sql("IN", expression, "log")
    489            position = self._prefixed_sql("FROM", expression, "position")
    490
    491            channel = self._prefixed_sql("FOR CHANNEL", expression, "channel")
    492
    493            if expression.name == "ENGINE":
    494                mutex_or_status = " MUTEX" if expression.args.get("mutex") else " STATUS"
    495            else:
    496                mutex_or_status = ""
    497
    498            return f"SHOW{full}{global_}{this}{target}{types}{db}{query}{log}{position}{channel}{mutex_or_status}{like}{where}{offset}{limit}"
    499
    500        def _prefixed_sql(self, prefix: str, expression: exp.Expression, arg: str) -> str:
    501            sql = self.sql(expression, arg)
    502            return f" {prefix} {sql}" if sql else ""
    503
    504        def _oldstyle_limit_sql(self, expression: exp.Show) -> str:
    505            limit = self.sql(expression, "limit")
    506            offset = self.sql(expression, "offset")
    507            if limit:
    508                limit_offset = f"{offset}, {limit}" if offset else limit
    509                return f" LIMIT {limit_offset}"
    510            return ""

Generator converts a given syntax tree to the corresponding SQL string.

###### Arguments:

- pretty: Whether or not to format the produced SQL string. Default: False.
- identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True or 'always': Always quote. 'safe': Only quote identifiers that are case insensitive.
- normalize: Whether or not to normalize identifiers to lowercase. Default: False.
- pad: Determines the pad size in a formatted string. Default: 2.
- indent: Determines the indentation size in a formatted string. Default: 2.
- normalize_functions: Whether or not to normalize all function names. Possible values are: "upper" or True (default): Convert names to uppercase. "lower": Convert names to lowercase. False: Disables function name normalization.
- unsupported_level: Determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
- leading_comma: Determines whether or not the comma is leading or trailing in select expressions. This is only relevant when generating in pretty mode. Default: False
- max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
- comments: Whether or not to preserve comments in the output SQL code. Default: True

LOCKING_READS_SUPPORTED = True
NULL_ORDERING_SUPPORTED = False
JOIN_HINTS = False
TABLE_HINTS = True
TRANSFORMS = {<class 'sqlglot.expressions.DateAdd'>: <function _date_add_sql.<locals>.func>, <class 'sqlglot.expressions.TsOrDsAdd'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CaseSpecificColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CharacterSetColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CharacterSetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CheckColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CollateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CopyGrantsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CommentColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.DateFormatColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.DefaultColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.EncodeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExecuteAsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExternalProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.InlineLengthColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LanguageProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LocationProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LogProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.MaterializedProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.NoPrimaryIndexProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnCommitProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnUpdateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.PathColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ReturnsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SettingsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SqlSecurityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.StabilityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TemporaryProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ToTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TransientProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TitleColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.UppercaseColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.VarMap'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.VolatileProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.WithJournalTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CurrentDate'>: <function no_paren_current_date_sql>, <class 'sqlglot.expressions.DateDiff'>: <function MySQL.Generator.<lambda>>, <class 'sqlglot.expressions.DateStrToDate'>: <function datestrtodate_sql>, <class 'sqlglot.expressions.DateSub'>: <function _date_add_sql.<locals>.func>, <class 'sqlglot.expressions.DateTrunc'>: <function _date_trunc_sql>, <class 'sqlglot.expressions.DayOfMonth'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.DayOfWeek'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.DayOfYear'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.GroupConcat'>: <function MySQL.Generator.<lambda>>, <class 'sqlglot.expressions.ILike'>: <function no_ilike_sql>, <class 'sqlglot.expressions.JSONExtractScalar'>: <function arrow_json_extract_scalar_sql>, <class 'sqlglot.expressions.Max'>: <function max_or_greatest>, <class 'sqlglot.expressions.Min'>: <function min_or_least>, <class 'sqlglot.expressions.NullSafeEQ'>: <function MySQL.Generator.<lambda>>, <class 'sqlglot.expressions.NullSafeNEQ'>: <function MySQL.Generator.<lambda>>, <class 'sqlglot.expressions.Pivot'>: <function no_pivot_sql>, <class 'sqlglot.expressions.Select'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.StrPosition'>: <function strposition_to_locate_sql>, <class 'sqlglot.expressions.StrToDate'>: <function _str_to_date_sql>, <class 'sqlglot.expressions.StrToTime'>: <function _str_to_date_sql>, <class 'sqlglot.expressions.TableSample'>: <function no_tablesample_sql>, <class 'sqlglot.expressions.TimeStrToUnix'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.TimeToStr'>: <function MySQL.Generator.<lambda>>, <class 'sqlglot.expressions.Trim'>: <function _trim_sql>, <class 'sqlglot.expressions.TryCast'>: <function no_trycast_sql>, <class 'sqlglot.expressions.WeekOfYear'>: <function rename_func.<locals>.<lambda>>}
TYPE_MAPPING = {<Type.NCHAR: 'NCHAR'>: 'CHAR', <Type.NVARCHAR: 'NVARCHAR'>: 'VARCHAR', <Type.INET: 'INET'>: 'INET'}
PROPERTIES_LOCATION = {<class 'sqlglot.expressions.AlgorithmProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.AutoIncrementProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.BlockCompressionProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.CharacterSetProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ChecksumProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.CollateProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.CopyGrantsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Cluster'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ClusteredByProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DataBlocksizeProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.DefinerProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.DictRange'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DictProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DistKeyProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DistStyleProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.EngineProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ExecuteAsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ExternalProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.FallbackProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.FileFormatProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.FreespaceProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.IsolatedLoadingProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.JournalProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.LanguageProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LikeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LocationProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LockingProperty'>: <Location.POST_ALIAS: 'POST_ALIAS'>, <class 'sqlglot.expressions.LogProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.MaterializedProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.MergeBlockRatioProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.NoPrimaryIndexProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.OnCommitProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.Order'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.PartitionedByProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.PrimaryKey'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Property'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.ReturnsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatDelimitedProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatSerdeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SchemaCommentProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SerdeProperties'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Set'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SettingsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SetProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.SortKeyProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SqlSecurityProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.StabilityProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.TemporaryProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.ToTableProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.TransientProperty'>: <Location.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.MergeTreeTTL'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.VolatileProperty'>: <Location.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.WithDataProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.WithJournalTableProperty'>: <Location.POST_NAME: 'POST_NAME'>}
LIMIT_FETCH = 'LIMIT'
def cast_sql( self, expression: sqlglot.expressions.Cast, safe_prefix: Optional[str] = None) -> str: View Source

    449        def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    450            """(U)BIGINT is not allowed in a CAST expression, so we use (UN)SIGNED instead."""
    451            if expression.to.this == exp.DataType.Type.BIGINT:
    452                to = "SIGNED"
    453            elif expression.to.this == exp.DataType.Type.UBIGINT:
    454                to = "UNSIGNED"
    455            else:
    456                return super().cast_sql(expression)
    457
    458            return f"CAST({self.sql(expression, 'this')} AS {to})"

(U)BIGINT is not allowed in a CAST expression, so we use (UN)SIGNED instead.

def show_sql(self, expression: sqlglot.expressions.Show) -> str: View Source

    460        def show_sql(self, expression: exp.Show) -> str:
    461            this = f" {expression.name}"
    462            full = " FULL" if expression.args.get("full") else ""
    463            global_ = " GLOBAL" if expression.args.get("global") else ""
    464
    465            target = self.sql(expression, "target")
    466            target = f" {target}" if target else ""
    467            if expression.name in {"COLUMNS", "INDEX"}:
    468                target = f" FROM{target}"
    469            elif expression.name == "GRANTS":
    470                target = f" FOR{target}"
    471
    472            db = self._prefixed_sql("FROM", expression, "db")
    473
    474            like = self._prefixed_sql("LIKE", expression, "like")
    475            where = self.sql(expression, "where")
    476
    477            types = self.expressions(expression, key="types")
    478            types = f" {types}" if types else types
    479            query = self._prefixed_sql("FOR QUERY", expression, "query")
    480
    481            if expression.name == "PROFILE":
    482                offset = self._prefixed_sql("OFFSET", expression, "offset")
    483                limit = self._prefixed_sql("LIMIT", expression, "limit")
    484            else:
    485                offset = ""
    486                limit = self._oldstyle_limit_sql(expression)
    487
    488            log = self._prefixed_sql("IN", expression, "log")
    489            position = self._prefixed_sql("FROM", expression, "position")
    490
    491            channel = self._prefixed_sql("FOR CHANNEL", expression, "channel")
    492
    493            if expression.name == "ENGINE":
    494                mutex_or_status = " MUTEX" if expression.args.get("mutex") else " STATUS"
    495            else:
    496                mutex_or_status = ""
    497
    498            return f"SHOW{full}{global_}{this}{target}{types}{db}{query}{log}{position}{channel}{mutex_or_status}{like}{where}{offset}{limit}"

SELECT_KINDS: Tuple[str, ...] = ()
INVERSE_TIME_MAPPING: Dict[str, str] = {'%B': '%M', '%-m': '%c', '%-d': '%e', '%I': '%h', '%M': '%i', '%S': '%S', '%W': '%u', '%-H': '%k', '%-I': '%l', '%H:%M:%S': '%T', '%a': '%W'}
INVERSE_TIME_TRIE: Dict = {'%': {'B': {0: True}, '-': {'m': {0: True}, 'd': {0: True}, 'H': {0: True}, 'I': {0: True}}, 'I': {0: True}, 'M': {0: True}, 'S': {0: True}, 'W': {0: True}, 'H': {':': {'%': {'M': {':': {'%': {'S': {0: True}}}}}}}, 'a': {0: True}}}
@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool: View Source

    247    @classmethod
    248    def can_identify(cls, text: str, identify: str | bool = "safe") -> bool:
    249        """Checks if text can be identified given an identify option.
    250
    251        Args:
    252            text: The text to check.
    253            identify:
    254                "always" or `True`: Always returns true.
    255                "safe": True if the identifier is case-insensitive.
    256
    257        Returns:
    258            Whether or not the given text can be identified.
    259        """
    260        if identify is True or identify == "always":
    261            return True
    262
    263        if identify == "safe":
    264            return not cls.case_sensitive(text)
    265
    266        return False

Checks if text can be identified given an identify option.

###### Arguments:

- text: The text to check.
- identify: "always" or `True`: Always returns true. "safe": True if the identifier is case-insensitive.

###### Returns:

> Whether or not the given text can be identified.

QUOTE_START = "'"
QUOTE_END = "'"
IDENTIFIER_START = '`'
IDENTIFIER_END = '`'
STRING_ESCAPE = "'"
IDENTIFIER_ESCAPE = '"'
BIT_START: Optional[str] = "b'"
BIT_END: Optional[str] = "'"
HEX_START: Optional[str] = "x'"
HEX_END: Optional[str] = "'"
BYTE_START: Optional[str] = None
BYTE_END: Optional[str] = None
RAW_START: Optional[str] = None
RAW_END: Optional[str] = None

##### Inherited Members

sqlglot.generator.Generator
  Generator
  EXPLICIT_UNION
  WRAP_DERIVED_VALUES
  CREATE_FUNCTION_RETURN_AS
  MATCHED_BY_SOURCE
  SINGLE_STRING_INTERVAL
  INTERVAL_ALLOWS_PLURAL_FORM
  TABLESAMPLE_WITH_METHOD
  TABLESAMPLE_SIZE_IS_PERCENT
  RENAME_TABLE_WITH_DB
  GROUPINGS_SEP
  INDEX_ON
  IS_BOOL_ALLOWED
  STAR_MAPPING
  TIME_PART_SINGULARS
  TOKEN_MAPPING
  STRUCT_DELIMITER
  PARAMETER_TOKEN
  RESERVED_KEYWORDS
  WITH_SEPARATED_COMMENTS
  UNWRAPPED_INTERVAL_VALUES
  SENTINEL_LINE_BREAK
  INDEX_OFFSET
  UNNEST_COLUMN_ONLY
  ALIAS_POST_TABLESAMPLE
  IDENTIFIERS_CAN_START_WITH_DIGIT
  STRICT_STRING_CONCAT
  NORMALIZE_FUNCTIONS
  NULL_ORDERING
  pretty
  identify
  normalize
  pad
  unsupported_level
  max_unsupported
  leading_comma
  max_text_width
  comments
  normalize_functions
  unsupported_messages
  generate
  unsupported
  sep
  seg
  pad_comment
  maybe_comment
  wrap
  no_identify
  normalize_func
  indent
  sql
  uncache_sql
  cache_sql
  characterset_sql
  column_sql
  columnposition_sql
  columndef_sql
  columnconstraint_sql
  autoincrementcolumnconstraint_sql
  compresscolumnconstraint_sql
  generatedasidentitycolumnconstraint_sql
  notnullcolumnconstraint_sql
  primarykeycolumnconstraint_sql
  uniquecolumnconstraint_sql
  createable_sql
  create_sql
  clone_sql
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_sql
  rawstring_sql
  datatypesize_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  inputoutputformat_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  onconflict_sql
  returning_sql
  rowformatdelimitedproperty_sql
  withtablehint_sql
  indextablehint_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  offset_sql
  setitem_sql
  set_sql
  pragma_sql
  lock_sql
  literal_sql
  escape_str
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  offset_limit_modifiers
  after_having_modifiers
  after_limit_modifiers
  select_sql
  schema_sql
  schema_columns_sql
  star_sql
  parameter_sql
  sessionparameter_sql
  placeholder_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  windowspec_sql
  withingroup_sql
  between_sql
  bracket_sql
  safebracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  safeconcat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  openjsoncolumndef_sql
  openjson_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  currentdate_sql
  collate_sql
  command_sql
  comment_sql
  mergetreettlaction_sql
  mergetreettl_sql
  transaction_sql
  commit_sql
  rollback_sql
  altercolumn_sql
  renametable_sql
  altertable_sql
  droppartition_sql
  addconstraint_sql
  distinct_sql
  ignorenulls_sql
  respectnulls_sql
  intdiv_sql
  dpipe_sql
  safedpipe_sql
  div_sql
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  ilikeany_sql
  is_sql
  like_sql
  likeany_sql
  similarto_sql
  lt_sql
  lte_sql
  mod_sql
  mul_sql
  neq_sql
  nullsafeeq_sql
  nullsafeneq_sql
  or_sql
  slice_sql
  sub_sql
  trycast_sql
  use_sql
  binary
  function_fallback_sql
  func
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql
  tochar_sql
  dictproperty_sql
  dictrange_sql
  dictsubproperty_sql
  oncluster_sql
  clusteredbyproperty_sql
  anyvalue_sql