Edit on GitHub

# sqlglot.dialects.mysql

View Source

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

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

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

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

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

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

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

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

    433        def show_sql(self, expression: exp.Show) -> str:
    434            this = f" {expression.name}"
    435            full = " FULL" if expression.args.get("full") else ""
    436            global_ = " GLOBAL" if expression.args.get("global") else ""
    437
    438            target = self.sql(expression, "target")
    439            target = f" {target}" if target else ""
    440            if expression.name in {"COLUMNS", "INDEX"}:
    441                target = f" FROM{target}"
    442            elif expression.name == "GRANTS":
    443                target = f" FOR{target}"
    444
    445            db = self._prefixed_sql("FROM", expression, "db")
    446
    447            like = self._prefixed_sql("LIKE", expression, "like")
    448            where = self.sql(expression, "where")
    449
    450            types = self.expressions(expression, key="types")
    451            types = f" {types}" if types else types
    452            query = self._prefixed_sql("FOR QUERY", expression, "query")
    453
    454            if expression.name == "PROFILE":
    455                offset = self._prefixed_sql("OFFSET", expression, "offset")
    456                limit = self._prefixed_sql("LIMIT", expression, "limit")
    457            else:
    458                offset = ""
    459                limit = self._oldstyle_limit_sql(expression)
    460
    461            log = self._prefixed_sql("IN", expression, "log")
    462            position = self._prefixed_sql("FROM", expression, "position")
    463
    464            channel = self._prefixed_sql("FOR CHANNEL", expression, "channel")
    465
    466            if expression.name == "ENGINE":
    467                mutex_or_status = " MUTEX" if expression.args.get("mutex") else " STATUS"
    468            else:
    469                mutex_or_status = ""
    470
    471            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
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_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
  afterjournalproperty_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
  select_sql
  schema_sql
  star_sql
  structkwarg_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
  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
  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