sqlglot.dialects.mysql API documentation

Edit on GitHub

# sqlglot.dialects.mysql

View Source

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

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

##### Inherited Members

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

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize
  peek

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

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

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

###### Arguments:

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

##### 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

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

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

###### Arguments:

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

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

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

@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool: View Source

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

Checks if text can be identified given an identify option.

###### Arguments:

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

###### Returns:

> Whether or not the given text can be identified.

##### 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
  createable_sql
  create_sql
  clone_sql
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_sql
  rawstring_sql
  datatypesize_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  inputoutputformat_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  onconflict_sql
  returning_sql
  rowformatdelimitedproperty_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  offset_sql
  setitem_sql
  set_sql
  pragma_sql
  lock_sql
  literal_sql
  escape_str
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  offset_limit_modifiers
  after_having_modifiers
  after_limit_modifiers
  select_sql
  schema_sql
  schema_columns_sql
  star_sql
  parameter_sql
  sessionparameter_sql
  placeholder_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  windowspec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  safeconcat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  openjsoncolumndef_sql
  openjson_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  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
  safedpipe_sql
  div_sql
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  ilikeany_sql
  is_sql
  like_sql
  likeany_sql
  similarto_sql
  lt_sql
  lte_sql
  mod_sql
  mul_sql
  neq_sql
  nullsafeeq_sql
  nullsafeneq_sql
  or_sql
  slice_sql
  sub_sql
  trycast_sql
  use_sql
  binary
  function_fallback_sql
  func
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql
  tochar_sql
  dictproperty_sql
  dictrange_sql
  dictsubproperty_sql
  oncluster_sql