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

##### Inherited Members

sqlglot.dialects.dialect.Dialect
  get_or_raise
  format_time
  parse
  parse_into
  generate
  transpile
  tokenize
  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            "LONGBLOB": TokenType.LONGBLOB,
    128            "LONGTEXT": TokenType.LONGTEXT,
    129            "MEDIUMBLOB": TokenType.MEDIUMBLOB,
    130            "MEDIUMTEXT": TokenType.MEDIUMTEXT,
    131            "SEPARATOR": TokenType.SEPARATOR,
    132            "START": TokenType.BEGIN,
    133            "_ARMSCII8": TokenType.INTRODUCER,
    134            "_ASCII": TokenType.INTRODUCER,
    135            "_BIG5": TokenType.INTRODUCER,
    136            "_BINARY": TokenType.INTRODUCER,
    137            "_CP1250": TokenType.INTRODUCER,
    138            "_CP1251": TokenType.INTRODUCER,
    139            "_CP1256": TokenType.INTRODUCER,
    140            "_CP1257": TokenType.INTRODUCER,
    141            "_CP850": TokenType.INTRODUCER,
    142            "_CP852": TokenType.INTRODUCER,
    143            "_CP866": TokenType.INTRODUCER,
    144            "_CP932": TokenType.INTRODUCER,
    145            "_DEC8": TokenType.INTRODUCER,
    146            "_EUCJPMS": TokenType.INTRODUCER,
    147            "_EUCKR": TokenType.INTRODUCER,
    148            "_GB18030": TokenType.INTRODUCER,
    149            "_GB2312": TokenType.INTRODUCER,
    150            "_GBK": TokenType.INTRODUCER,
    151            "_GEOSTD8": TokenType.INTRODUCER,
    152            "_GREEK": TokenType.INTRODUCER,
    153            "_HEBREW": TokenType.INTRODUCER,
    154            "_HP8": TokenType.INTRODUCER,
    155            "_KEYBCS2": TokenType.INTRODUCER,
    156            "_KOI8R": TokenType.INTRODUCER,
    157            "_KOI8U": TokenType.INTRODUCER,
    158            "_LATIN1": TokenType.INTRODUCER,
    159            "_LATIN2": TokenType.INTRODUCER,
    160            "_LATIN5": TokenType.INTRODUCER,
    161            "_LATIN7": TokenType.INTRODUCER,
    162            "_MACCE": TokenType.INTRODUCER,
    163            "_MACROMAN": TokenType.INTRODUCER,
    164            "_SJIS": TokenType.INTRODUCER,
    165            "_SWE7": TokenType.INTRODUCER,
    166            "_TIS620": TokenType.INTRODUCER,
    167            "_UCS2": TokenType.INTRODUCER,
    168            "_UJIS": TokenType.INTRODUCER,
    169            # https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
    170            "_UTF8": TokenType.INTRODUCER,
    171            "_UTF16": TokenType.INTRODUCER,
    172            "_UTF16LE": TokenType.INTRODUCER,
    173            "_UTF32": TokenType.INTRODUCER,
    174            "_UTF8MB3": TokenType.INTRODUCER,
    175            "_UTF8MB4": TokenType.INTRODUCER,
    176            "@@": TokenType.SESSION_PARAMETER,
    177        }
    178
    179        COMMANDS = tokens.Tokenizer.COMMANDS - {TokenType.SHOW}

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

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

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

###### Arguments:

- error_level: the desired error level. Default: ErrorLevel.RAISE
- error_message_context: determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 50.
- index_offset: Index offset for arrays eg ARRAY[0] vs ARRAY[1] as the head of a list. Default: 0
- alias_post_tablesample: If the table alias comes after tablesample. Default: False
- 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
- null_ordering: Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"

##### Inherited Members

sqlglot.parser.Parser
  Parser
  reset
  parse
  parse_into
  check_errors
  raise_error
  expression
  validate_expression

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

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

Generator interprets the given syntax tree and produces a SQL string as an output.

###### Arguments:

- time_mapping (dict): the dictionary of custom time mappings in which the key represents a python time format and the output the target time format
- time_trie (trie): a trie of the time_mapping keys
- pretty (bool): if set to True the returned string will be formatted. Default: False.
- quote_start (str): specifies which starting character to use to delimit quotes. Default: '.
- quote_end (str): specifies which ending character to use to delimit quotes. Default: '.
- identifier_start (str): specifies which starting character to use to delimit identifiers. Default: ".
- identifier_end (str): specifies which ending character to use to delimit identifiers. Default: ".
- bit_start (str): specifies which starting character to use to delimit bit literals. Default: None.
- bit_end (str): specifies which ending character to use to delimit bit literals. Default: None.
- hex_start (str): specifies which starting character to use to delimit hex literals. Default: None.
- hex_end (str): specifies which ending character to use to delimit hex literals. Default: None.
- byte_start (str): specifies which starting character to use to delimit byte literals. Default: None.
- byte_end (str): specifies which ending character to use to delimit byte literals. Default: None.
- raw_start (str): specifies which starting character to use to delimit raw literals. Default: None.
- raw_end (str): specifies which ending character to use to delimit raw literals. Default: None.
- identify (bool | str): 'always': always quote, 'safe': quote identifiers if they don't contain an upcase, True defaults to always.
- normalize (bool): if set to True all identifiers will lower cased
- string_escape (str): specifies a string escape character. Default: '.
- identifier_escape (str): specifies an identifier escape character. Default: ".
- pad (int): determines padding in a formatted string. Default: 2.
- indent (int): determines the size of indentation in a formatted string. Default: 4.
- unnest_column_only (bool): if true unnest table aliases are considered only as column aliases
- normalize_functions (str): normalize function names, "upper", "lower", or None Default: "upper"
- alias_post_tablesample (bool): if the table alias comes after tablesample Default: False
- unsupported_level (ErrorLevel): determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- null_ordering (str): Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
- max_unsupported (int): 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 (bool): if the the comma is leading or trailing in select statements 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

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

    437        def show_sql(self, expression: exp.Show) -> str:
    438            this = f" {expression.name}"
    439            full = " FULL" if expression.args.get("full") else ""
    440            global_ = " GLOBAL" if expression.args.get("global") else ""
    441
    442            target = self.sql(expression, "target")
    443            target = f" {target}" if target else ""
    444            if expression.name in {"COLUMNS", "INDEX"}:
    445                target = f" FROM{target}"
    446            elif expression.name == "GRANTS":
    447                target = f" FOR{target}"
    448
    449            db = self._prefixed_sql("FROM", expression, "db")
    450
    451            like = self._prefixed_sql("LIKE", expression, "like")
    452            where = self.sql(expression, "where")
    453
    454            types = self.expressions(expression, key="types")
    455            types = f" {types}" if types else types
    456            query = self._prefixed_sql("FOR QUERY", expression, "query")
    457
    458            if expression.name == "PROFILE":
    459                offset = self._prefixed_sql("OFFSET", expression, "offset")
    460                limit = self._prefixed_sql("LIMIT", expression, "limit")
    461            else:
    462                offset = ""
    463                limit = self._oldstyle_limit_sql(expression)
    464
    465            log = self._prefixed_sql("IN", expression, "log")
    466            position = self._prefixed_sql("FROM", expression, "position")
    467
    468            channel = self._prefixed_sql("FOR CHANNEL", expression, "channel")
    469
    470            if expression.name == "ENGINE":
    471                mutex_or_status = " MUTEX" if expression.args.get("mutex") else " STATUS"
    472            else:
    473                mutex_or_status = ""
    474
    475            return f"SHOW{full}{global_}{this}{target}{types}{db}{query}{log}{position}{channel}{mutex_or_status}{like}{where}{offset}{limit}"

##### Inherited Members

sqlglot.generator.Generator
  Generator
  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
  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
  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
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  after_having_modifiers
  after_limit_modifiers
  select_sql
  schema_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
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  concat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  unique_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
  cast_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
  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