sqlglot.tokens API documentation

Edit on GitHub

# sqlglot.tokens

View Source

       1from __future__ import annotations
       2
       3import typing as t
       4from enum import auto
       5
       6from sqlglot.helper import AutoName
       7from sqlglot.trie import in_trie, new_trie
       8
       9
      10class TokenType(AutoName):
      11    L_PAREN = auto()
      12    R_PAREN = auto()
      13    L_BRACKET = auto()
      14    R_BRACKET = auto()
      15    L_BRACE = auto()
      16    R_BRACE = auto()
      17    COMMA = auto()
      18    DOT = auto()
      19    DASH = auto()
      20    PLUS = auto()
      21    COLON = auto()
      22    DCOLON = auto()
      23    SEMICOLON = auto()
      24    STAR = auto()
      25    BACKSLASH = auto()
      26    SLASH = auto()
      27    LT = auto()
      28    LTE = auto()
      29    GT = auto()
      30    GTE = auto()
      31    NOT = auto()
      32    EQ = auto()
      33    NEQ = auto()
      34    NULLSAFE_EQ = auto()
      35    AND = auto()
      36    OR = auto()
      37    AMP = auto()
      38    DPIPE = auto()
      39    PIPE = auto()
      40    CARET = auto()
      41    TILDA = auto()
      42    ARROW = auto()
      43    DARROW = auto()
      44    FARROW = auto()
      45    HASH = auto()
      46    HASH_ARROW = auto()
      47    DHASH_ARROW = auto()
      48    LR_ARROW = auto()
      49    LT_AT = auto()
      50    AT_GT = auto()
      51    DOLLAR = auto()
      52    PARAMETER = auto()
      53    SESSION_PARAMETER = auto()
      54    DAMP = auto()
      55
      56    BLOCK_START = auto()
      57    BLOCK_END = auto()
      58
      59    SPACE = auto()
      60    BREAK = auto()
      61
      62    STRING = auto()
      63    NUMBER = auto()
      64    IDENTIFIER = auto()
      65    DATABASE = auto()
      66    COLUMN = auto()
      67    COLUMN_DEF = auto()
      68    SCHEMA = auto()
      69    TABLE = auto()
      70    VAR = auto()
      71    BIT_STRING = auto()
      72    HEX_STRING = auto()
      73    BYTE_STRING = auto()
      74    NATIONAL_STRING = auto()
      75    RAW_STRING = auto()
      76
      77    # types
      78    BIT = auto()
      79    BOOLEAN = auto()
      80    TINYINT = auto()
      81    UTINYINT = auto()
      82    SMALLINT = auto()
      83    USMALLINT = auto()
      84    INT = auto()
      85    UINT = auto()
      86    BIGINT = auto()
      87    UBIGINT = auto()
      88    INT128 = auto()
      89    UINT128 = auto()
      90    INT256 = auto()
      91    UINT256 = auto()
      92    FLOAT = auto()
      93    DOUBLE = auto()
      94    DECIMAL = auto()
      95    BIGDECIMAL = auto()
      96    CHAR = auto()
      97    NCHAR = auto()
      98    VARCHAR = auto()
      99    NVARCHAR = auto()
     100    TEXT = auto()
     101    MEDIUMTEXT = auto()
     102    LONGTEXT = auto()
     103    MEDIUMBLOB = auto()
     104    LONGBLOB = auto()
     105    BINARY = auto()
     106    VARBINARY = auto()
     107    JSON = auto()
     108    JSONB = auto()
     109    TIME = auto()
     110    TIMESTAMP = auto()
     111    TIMESTAMPTZ = auto()
     112    TIMESTAMPLTZ = auto()
     113    DATETIME = auto()
     114    DATETIME64 = auto()
     115    DATE = auto()
     116    INT4RANGE = auto()
     117    INT4MULTIRANGE = auto()
     118    INT8RANGE = auto()
     119    INT8MULTIRANGE = auto()
     120    NUMRANGE = auto()
     121    NUMMULTIRANGE = auto()
     122    TSRANGE = auto()
     123    TSMULTIRANGE = auto()
     124    TSTZRANGE = auto()
     125    TSTZMULTIRANGE = auto()
     126    DATERANGE = auto()
     127    DATEMULTIRANGE = auto()
     128    UUID = auto()
     129    GEOGRAPHY = auto()
     130    NULLABLE = auto()
     131    GEOMETRY = auto()
     132    HLLSKETCH = auto()
     133    HSTORE = auto()
     134    SUPER = auto()
     135    SERIAL = auto()
     136    SMALLSERIAL = auto()
     137    BIGSERIAL = auto()
     138    XML = auto()
     139    UNIQUEIDENTIFIER = auto()
     140    MONEY = auto()
     141    SMALLMONEY = auto()
     142    ROWVERSION = auto()
     143    IMAGE = auto()
     144    VARIANT = auto()
     145    OBJECT = auto()
     146    INET = auto()
     147
     148    # keywords
     149    ALIAS = auto()
     150    ALTER = auto()
     151    ALWAYS = auto()
     152    ALL = auto()
     153    ANTI = auto()
     154    ANY = auto()
     155    APPLY = auto()
     156    ARRAY = auto()
     157    ASC = auto()
     158    ASOF = auto()
     159    AUTO_INCREMENT = auto()
     160    BEGIN = auto()
     161    BETWEEN = auto()
     162    CACHE = auto()
     163    CASE = auto()
     164    CHARACTER_SET = auto()
     165    COLLATE = auto()
     166    COMMAND = auto()
     167    COMMENT = auto()
     168    COMMIT = auto()
     169    CONSTRAINT = auto()
     170    CREATE = auto()
     171    CROSS = auto()
     172    CUBE = auto()
     173    CURRENT_DATE = auto()
     174    CURRENT_DATETIME = auto()
     175    CURRENT_TIME = auto()
     176    CURRENT_TIMESTAMP = auto()
     177    CURRENT_USER = auto()
     178    DEFAULT = auto()
     179    DELETE = auto()
     180    DESC = auto()
     181    DESCRIBE = auto()
     182    DICTIONARY = auto()
     183    DISTINCT = auto()
     184    DIV = auto()
     185    DROP = auto()
     186    ELSE = auto()
     187    END = auto()
     188    ESCAPE = auto()
     189    EXCEPT = auto()
     190    EXECUTE = auto()
     191    EXISTS = auto()
     192    FALSE = auto()
     193    FETCH = auto()
     194    FILTER = auto()
     195    FINAL = auto()
     196    FIRST = auto()
     197    FOR = auto()
     198    FOREIGN_KEY = auto()
     199    FORMAT = auto()
     200    FROM = auto()
     201    FULL = auto()
     202    FUNCTION = auto()
     203    GLOB = auto()
     204    GLOBAL = auto()
     205    GROUP_BY = auto()
     206    GROUPING_SETS = auto()
     207    HAVING = auto()
     208    HINT = auto()
     209    IF = auto()
     210    ILIKE = auto()
     211    ILIKE_ANY = auto()
     212    IN = auto()
     213    INDEX = auto()
     214    INNER = auto()
     215    INSERT = auto()
     216    INTERSECT = auto()
     217    INTERVAL = auto()
     218    INTO = auto()
     219    INTRODUCER = auto()
     220    IRLIKE = auto()
     221    IS = auto()
     222    ISNULL = auto()
     223    JOIN = auto()
     224    JOIN_MARKER = auto()
     225    KEEP = auto()
     226    LANGUAGE = auto()
     227    LATERAL = auto()
     228    LEFT = auto()
     229    LIKE = auto()
     230    LIKE_ANY = auto()
     231    LIMIT = auto()
     232    LOAD = auto()
     233    LOCK = auto()
     234    MAP = auto()
     235    MATCH_RECOGNIZE = auto()
     236    MERGE = auto()
     237    MOD = auto()
     238    NATURAL = auto()
     239    NEXT = auto()
     240    NEXT_VALUE_FOR = auto()
     241    NOTNULL = auto()
     242    NULL = auto()
     243    OFFSET = auto()
     244    ON = auto()
     245    ORDER_BY = auto()
     246    ORDERED = auto()
     247    ORDINALITY = auto()
     248    OUTER = auto()
     249    OVER = auto()
     250    OVERLAPS = auto()
     251    OVERWRITE = auto()
     252    PARTITION = auto()
     253    PARTITION_BY = auto()
     254    PERCENT = auto()
     255    PIVOT = auto()
     256    PLACEHOLDER = auto()
     257    PRAGMA = auto()
     258    PRIMARY_KEY = auto()
     259    PROCEDURE = auto()
     260    PROPERTIES = auto()
     261    PSEUDO_TYPE = auto()
     262    QUALIFY = auto()
     263    QUOTE = auto()
     264    RANGE = auto()
     265    RECURSIVE = auto()
     266    REPLACE = auto()
     267    RETURNING = auto()
     268    REFERENCES = auto()
     269    RIGHT = auto()
     270    RLIKE = auto()
     271    ROLLBACK = auto()
     272    ROLLUP = auto()
     273    ROW = auto()
     274    ROWS = auto()
     275    SELECT = auto()
     276    SEMI = auto()
     277    SEPARATOR = auto()
     278    SERDE_PROPERTIES = auto()
     279    SET = auto()
     280    SETTINGS = auto()
     281    SHOW = auto()
     282    SIMILAR_TO = auto()
     283    SOME = auto()
     284    STRUCT = auto()
     285    TABLE_SAMPLE = auto()
     286    TEMPORARY = auto()
     287    TOP = auto()
     288    THEN = auto()
     289    TRUE = auto()
     290    UNCACHE = auto()
     291    UNION = auto()
     292    UNNEST = auto()
     293    UNPIVOT = auto()
     294    UPDATE = auto()
     295    USE = auto()
     296    USING = auto()
     297    VALUES = auto()
     298    VIEW = auto()
     299    VOLATILE = auto()
     300    WHEN = auto()
     301    WHERE = auto()
     302    WINDOW = auto()
     303    WITH = auto()
     304    UNIQUE = auto()
     305
     306
     307class Token:
     308    __slots__ = ("token_type", "text", "line", "col", "start", "end", "comments")
     309
     310    @classmethod
     311    def number(cls, number: int) -> Token:
     312        """Returns a NUMBER token with `number` as its text."""
     313        return cls(TokenType.NUMBER, str(number))
     314
     315    @classmethod
     316    def string(cls, string: str) -> Token:
     317        """Returns a STRING token with `string` as its text."""
     318        return cls(TokenType.STRING, string)
     319
     320    @classmethod
     321    def identifier(cls, identifier: str) -> Token:
     322        """Returns an IDENTIFIER token with `identifier` as its text."""
     323        return cls(TokenType.IDENTIFIER, identifier)
     324
     325    @classmethod
     326    def var(cls, var: str) -> Token:
     327        """Returns an VAR token with `var` as its text."""
     328        return cls(TokenType.VAR, var)
     329
     330    def __init__(
     331        self,
     332        token_type: TokenType,
     333        text: str,
     334        line: int = 1,
     335        col: int = 1,
     336        start: int = 0,
     337        end: int = 0,
     338        comments: t.List[str] = [],
     339    ) -> None:
     340        """Token initializer.
     341
     342        Args:
     343            token_type: The TokenType Enum.
     344            text: The text of the token.
     345            line: The line that the token ends on.
     346            col: The column that the token ends on.
     347            start: The start index of the token.
     348            end: The ending index of the token.
     349        """
     350        self.token_type = token_type
     351        self.text = text
     352        self.line = line
     353        self.col = col
     354        self.start = start
     355        self.end = end
     356        self.comments = comments
     357
     358    def __repr__(self) -> str:
     359        attributes = ", ".join(f"{k}: {getattr(self, k)}" for k in self.__slots__)
     360        return f"<Token {attributes}>"
     361
     362
     363class _Tokenizer(type):
     364    def __new__(cls, clsname, bases, attrs):
     365        klass = super().__new__(cls, clsname, bases, attrs)
     366
     367        def _convert_quotes(arr: t.List[str | t.Tuple[str, str]]) -> t.Dict[str, str]:
     368            return dict(
     369                (item, item) if isinstance(item, str) else (item[0], item[1]) for item in arr
     370            )
     371
     372        def _quotes_to_format(
     373            token_type: TokenType, arr: t.List[str | t.Tuple[str, str]]
     374        ) -> t.Dict[str, t.Tuple[str, TokenType]]:
     375            return {k: (v, token_type) for k, v in _convert_quotes(arr).items()}
     376
     377        klass._QUOTES = _convert_quotes(klass.QUOTES)
     378        klass._IDENTIFIERS = _convert_quotes(klass.IDENTIFIERS)
     379
     380        klass._FORMAT_STRINGS = {
     381            **{
     382                p + s: (e, TokenType.NATIONAL_STRING)
     383                for s, e in klass._QUOTES.items()
     384                for p in ("n", "N")
     385            },
     386            **_quotes_to_format(TokenType.BIT_STRING, klass.BIT_STRINGS),
     387            **_quotes_to_format(TokenType.BYTE_STRING, klass.BYTE_STRINGS),
     388            **_quotes_to_format(TokenType.HEX_STRING, klass.HEX_STRINGS),
     389            **_quotes_to_format(TokenType.RAW_STRING, klass.RAW_STRINGS),
     390        }
     391
     392        klass._STRING_ESCAPES = set(klass.STRING_ESCAPES)
     393        klass._IDENTIFIER_ESCAPES = set(klass.IDENTIFIER_ESCAPES)
     394        klass._COMMENTS = dict(
     395            (comment, None) if isinstance(comment, str) else (comment[0], comment[1])
     396            for comment in klass.COMMENTS
     397        )
     398
     399        klass.KEYWORD_TRIE = new_trie(
     400            key.upper()
     401            for key in (
     402                *klass.KEYWORDS,
     403                *klass._COMMENTS,
     404                *klass._QUOTES,
     405                *klass._FORMAT_STRINGS,
     406            )
     407            if " " in key or any(single in key for single in klass.SINGLE_TOKENS)
     408        )
     409
     410        return klass
     411
     412
     413class Tokenizer(metaclass=_Tokenizer):
     414    SINGLE_TOKENS = {
     415        "(": TokenType.L_PAREN,
     416        ")": TokenType.R_PAREN,
     417        "[": TokenType.L_BRACKET,
     418        "]": TokenType.R_BRACKET,
     419        "{": TokenType.L_BRACE,
     420        "}": TokenType.R_BRACE,
     421        "&": TokenType.AMP,
     422        "^": TokenType.CARET,
     423        ":": TokenType.COLON,
     424        ",": TokenType.COMMA,
     425        ".": TokenType.DOT,
     426        "-": TokenType.DASH,
     427        "=": TokenType.EQ,
     428        ">": TokenType.GT,
     429        "<": TokenType.LT,
     430        "%": TokenType.MOD,
     431        "!": TokenType.NOT,
     432        "|": TokenType.PIPE,
     433        "+": TokenType.PLUS,
     434        ";": TokenType.SEMICOLON,
     435        "/": TokenType.SLASH,
     436        "\\": TokenType.BACKSLASH,
     437        "*": TokenType.STAR,
     438        "~": TokenType.TILDA,
     439        "?": TokenType.PLACEHOLDER,
     440        "@": TokenType.PARAMETER,
     441        # used for breaking a var like x'y' but nothing else
     442        # the token type doesn't matter
     443        "'": TokenType.QUOTE,
     444        "`": TokenType.IDENTIFIER,
     445        '"': TokenType.IDENTIFIER,
     446        "#": TokenType.HASH,
     447    }
     448
     449    BIT_STRINGS: t.List[str | t.Tuple[str, str]] = []
     450    BYTE_STRINGS: t.List[str | t.Tuple[str, str]] = []
     451    HEX_STRINGS: t.List[str | t.Tuple[str, str]] = []
     452    RAW_STRINGS: t.List[str | t.Tuple[str, str]] = []
     453    IDENTIFIERS: t.List[str | t.Tuple[str, str]] = ['"']
     454    IDENTIFIER_ESCAPES = ['"']
     455    QUOTES: t.List[t.Tuple[str, str] | str] = ["'"]
     456    STRING_ESCAPES = ["'"]
     457    VAR_SINGLE_TOKENS: t.Set[str] = set()
     458
     459    _COMMENTS: t.Dict[str, str] = {}
     460    _FORMAT_STRINGS: t.Dict[str, t.Tuple[str, TokenType]] = {}
     461    _IDENTIFIERS: t.Dict[str, str] = {}
     462    _IDENTIFIER_ESCAPES: t.Set[str] = set()
     463    _QUOTES: t.Dict[str, str] = {}
     464    _STRING_ESCAPES: t.Set[str] = set()
     465
     466    KEYWORDS: t.Dict[t.Optional[str], TokenType] = {
     467        **{f"{{%{postfix}": TokenType.BLOCK_START for postfix in ("", "+", "-")},
     468        **{f"{prefix}%}}": TokenType.BLOCK_END for prefix in ("", "+", "-")},
     469        "{{+": TokenType.BLOCK_START,
     470        "{{-": TokenType.BLOCK_START,
     471        "+}}": TokenType.BLOCK_END,
     472        "-}}": TokenType.BLOCK_END,
     473        "/*+": TokenType.HINT,
     474        "==": TokenType.EQ,
     475        "::": TokenType.DCOLON,
     476        "||": TokenType.DPIPE,
     477        ">=": TokenType.GTE,
     478        "<=": TokenType.LTE,
     479        "<>": TokenType.NEQ,
     480        "!=": TokenType.NEQ,
     481        "<=>": TokenType.NULLSAFE_EQ,
     482        "->": TokenType.ARROW,
     483        "->>": TokenType.DARROW,
     484        "=>": TokenType.FARROW,
     485        "#>": TokenType.HASH_ARROW,
     486        "#>>": TokenType.DHASH_ARROW,
     487        "<->": TokenType.LR_ARROW,
     488        "&&": TokenType.DAMP,
     489        "ALL": TokenType.ALL,
     490        "ALWAYS": TokenType.ALWAYS,
     491        "AND": TokenType.AND,
     492        "ANTI": TokenType.ANTI,
     493        "ANY": TokenType.ANY,
     494        "ASC": TokenType.ASC,
     495        "AS": TokenType.ALIAS,
     496        "ASOF": TokenType.ASOF,
     497        "AUTOINCREMENT": TokenType.AUTO_INCREMENT,
     498        "AUTO_INCREMENT": TokenType.AUTO_INCREMENT,
     499        "BEGIN": TokenType.BEGIN,
     500        "BETWEEN": TokenType.BETWEEN,
     501        "CACHE": TokenType.CACHE,
     502        "UNCACHE": TokenType.UNCACHE,
     503        "CASE": TokenType.CASE,
     504        "CHARACTER SET": TokenType.CHARACTER_SET,
     505        "COLLATE": TokenType.COLLATE,
     506        "COLUMN": TokenType.COLUMN,
     507        "COMMIT": TokenType.COMMIT,
     508        "CONSTRAINT": TokenType.CONSTRAINT,
     509        "CREATE": TokenType.CREATE,
     510        "CROSS": TokenType.CROSS,
     511        "CUBE": TokenType.CUBE,
     512        "CURRENT_DATE": TokenType.CURRENT_DATE,
     513        "CURRENT_TIME": TokenType.CURRENT_TIME,
     514        "CURRENT_TIMESTAMP": TokenType.CURRENT_TIMESTAMP,
     515        "CURRENT_USER": TokenType.CURRENT_USER,
     516        "DATABASE": TokenType.DATABASE,
     517        "DEFAULT": TokenType.DEFAULT,
     518        "DELETE": TokenType.DELETE,
     519        "DESC": TokenType.DESC,
     520        "DESCRIBE": TokenType.DESCRIBE,
     521        "DISTINCT": TokenType.DISTINCT,
     522        "DIV": TokenType.DIV,
     523        "DROP": TokenType.DROP,
     524        "ELSE": TokenType.ELSE,
     525        "END": TokenType.END,
     526        "ESCAPE": TokenType.ESCAPE,
     527        "EXCEPT": TokenType.EXCEPT,
     528        "EXECUTE": TokenType.EXECUTE,
     529        "EXISTS": TokenType.EXISTS,
     530        "FALSE": TokenType.FALSE,
     531        "FETCH": TokenType.FETCH,
     532        "FILTER": TokenType.FILTER,
     533        "FIRST": TokenType.FIRST,
     534        "FULL": TokenType.FULL,
     535        "FUNCTION": TokenType.FUNCTION,
     536        "FOR": TokenType.FOR,
     537        "FOREIGN KEY": TokenType.FOREIGN_KEY,
     538        "FORMAT": TokenType.FORMAT,
     539        "FROM": TokenType.FROM,
     540        "GEOGRAPHY": TokenType.GEOGRAPHY,
     541        "GEOMETRY": TokenType.GEOMETRY,
     542        "GLOB": TokenType.GLOB,
     543        "GROUP BY": TokenType.GROUP_BY,
     544        "GROUPING SETS": TokenType.GROUPING_SETS,
     545        "HAVING": TokenType.HAVING,
     546        "IF": TokenType.IF,
     547        "ILIKE": TokenType.ILIKE,
     548        "IN": TokenType.IN,
     549        "INDEX": TokenType.INDEX,
     550        "INET": TokenType.INET,
     551        "INNER": TokenType.INNER,
     552        "INSERT": TokenType.INSERT,
     553        "INTERVAL": TokenType.INTERVAL,
     554        "INTERSECT": TokenType.INTERSECT,
     555        "INTO": TokenType.INTO,
     556        "IS": TokenType.IS,
     557        "ISNULL": TokenType.ISNULL,
     558        "JOIN": TokenType.JOIN,
     559        "KEEP": TokenType.KEEP,
     560        "LATERAL": TokenType.LATERAL,
     561        "LEFT": TokenType.LEFT,
     562        "LIKE": TokenType.LIKE,
     563        "LIMIT": TokenType.LIMIT,
     564        "LOAD": TokenType.LOAD,
     565        "LOCK": TokenType.LOCK,
     566        "MERGE": TokenType.MERGE,
     567        "NATURAL": TokenType.NATURAL,
     568        "NEXT": TokenType.NEXT,
     569        "NEXT VALUE FOR": TokenType.NEXT_VALUE_FOR,
     570        "NOT": TokenType.NOT,
     571        "NOTNULL": TokenType.NOTNULL,
     572        "NULL": TokenType.NULL,
     573        "OBJECT": TokenType.OBJECT,
     574        "OFFSET": TokenType.OFFSET,
     575        "ON": TokenType.ON,
     576        "OR": TokenType.OR,
     577        "ORDER BY": TokenType.ORDER_BY,
     578        "ORDINALITY": TokenType.ORDINALITY,
     579        "OUTER": TokenType.OUTER,
     580        "OVER": TokenType.OVER,
     581        "OVERLAPS": TokenType.OVERLAPS,
     582        "OVERWRITE": TokenType.OVERWRITE,
     583        "PARTITION": TokenType.PARTITION,
     584        "PARTITION BY": TokenType.PARTITION_BY,
     585        "PARTITIONED BY": TokenType.PARTITION_BY,
     586        "PARTITIONED_BY": TokenType.PARTITION_BY,
     587        "PERCENT": TokenType.PERCENT,
     588        "PIVOT": TokenType.PIVOT,
     589        "PRAGMA": TokenType.PRAGMA,
     590        "PRIMARY KEY": TokenType.PRIMARY_KEY,
     591        "PROCEDURE": TokenType.PROCEDURE,
     592        "QUALIFY": TokenType.QUALIFY,
     593        "RANGE": TokenType.RANGE,
     594        "RECURSIVE": TokenType.RECURSIVE,
     595        "REGEXP": TokenType.RLIKE,
     596        "REPLACE": TokenType.REPLACE,
     597        "REFERENCES": TokenType.REFERENCES,
     598        "RIGHT": TokenType.RIGHT,
     599        "RLIKE": TokenType.RLIKE,
     600        "ROLLBACK": TokenType.ROLLBACK,
     601        "ROLLUP": TokenType.ROLLUP,
     602        "ROW": TokenType.ROW,
     603        "ROWS": TokenType.ROWS,
     604        "SCHEMA": TokenType.SCHEMA,
     605        "SELECT": TokenType.SELECT,
     606        "SEMI": TokenType.SEMI,
     607        "SET": TokenType.SET,
     608        "SETTINGS": TokenType.SETTINGS,
     609        "SHOW": TokenType.SHOW,
     610        "SIMILAR TO": TokenType.SIMILAR_TO,
     611        "SOME": TokenType.SOME,
     612        "TABLE": TokenType.TABLE,
     613        "TABLESAMPLE": TokenType.TABLE_SAMPLE,
     614        "TEMP": TokenType.TEMPORARY,
     615        "TEMPORARY": TokenType.TEMPORARY,
     616        "THEN": TokenType.THEN,
     617        "TRUE": TokenType.TRUE,
     618        "UNION": TokenType.UNION,
     619        "UNNEST": TokenType.UNNEST,
     620        "UNPIVOT": TokenType.UNPIVOT,
     621        "UPDATE": TokenType.UPDATE,
     622        "USE": TokenType.USE,
     623        "USING": TokenType.USING,
     624        "UUID": TokenType.UUID,
     625        "VALUES": TokenType.VALUES,
     626        "VIEW": TokenType.VIEW,
     627        "VOLATILE": TokenType.VOLATILE,
     628        "WHEN": TokenType.WHEN,
     629        "WHERE": TokenType.WHERE,
     630        "WINDOW": TokenType.WINDOW,
     631        "WITH": TokenType.WITH,
     632        "APPLY": TokenType.APPLY,
     633        "ARRAY": TokenType.ARRAY,
     634        "BIT": TokenType.BIT,
     635        "BOOL": TokenType.BOOLEAN,
     636        "BOOLEAN": TokenType.BOOLEAN,
     637        "BYTE": TokenType.TINYINT,
     638        "TINYINT": TokenType.TINYINT,
     639        "SHORT": TokenType.SMALLINT,
     640        "SMALLINT": TokenType.SMALLINT,
     641        "INT2": TokenType.SMALLINT,
     642        "INTEGER": TokenType.INT,
     643        "INT": TokenType.INT,
     644        "INT4": TokenType.INT,
     645        "LONG": TokenType.BIGINT,
     646        "BIGINT": TokenType.BIGINT,
     647        "INT8": TokenType.BIGINT,
     648        "DEC": TokenType.DECIMAL,
     649        "DECIMAL": TokenType.DECIMAL,
     650        "BIGDECIMAL": TokenType.BIGDECIMAL,
     651        "BIGNUMERIC": TokenType.BIGDECIMAL,
     652        "MAP": TokenType.MAP,
     653        "NULLABLE": TokenType.NULLABLE,
     654        "NUMBER": TokenType.DECIMAL,
     655        "NUMERIC": TokenType.DECIMAL,
     656        "FIXED": TokenType.DECIMAL,
     657        "REAL": TokenType.FLOAT,
     658        "FLOAT": TokenType.FLOAT,
     659        "FLOAT4": TokenType.FLOAT,
     660        "FLOAT8": TokenType.DOUBLE,
     661        "DOUBLE": TokenType.DOUBLE,
     662        "DOUBLE PRECISION": TokenType.DOUBLE,
     663        "JSON": TokenType.JSON,
     664        "CHAR": TokenType.CHAR,
     665        "CHARACTER": TokenType.CHAR,
     666        "NCHAR": TokenType.NCHAR,
     667        "VARCHAR": TokenType.VARCHAR,
     668        "VARCHAR2": TokenType.VARCHAR,
     669        "NVARCHAR": TokenType.NVARCHAR,
     670        "NVARCHAR2": TokenType.NVARCHAR,
     671        "STR": TokenType.TEXT,
     672        "STRING": TokenType.TEXT,
     673        "TEXT": TokenType.TEXT,
     674        "CLOB": TokenType.TEXT,
     675        "LONGVARCHAR": TokenType.TEXT,
     676        "BINARY": TokenType.BINARY,
     677        "BLOB": TokenType.VARBINARY,
     678        "BYTEA": TokenType.VARBINARY,
     679        "VARBINARY": TokenType.VARBINARY,
     680        "TIME": TokenType.TIME,
     681        "TIMESTAMP": TokenType.TIMESTAMP,
     682        "TIMESTAMPTZ": TokenType.TIMESTAMPTZ,
     683        "TIMESTAMPLTZ": TokenType.TIMESTAMPLTZ,
     684        "DATE": TokenType.DATE,
     685        "DATETIME": TokenType.DATETIME,
     686        "INT4RANGE": TokenType.INT4RANGE,
     687        "INT4MULTIRANGE": TokenType.INT4MULTIRANGE,
     688        "INT8RANGE": TokenType.INT8RANGE,
     689        "INT8MULTIRANGE": TokenType.INT8MULTIRANGE,
     690        "NUMRANGE": TokenType.NUMRANGE,
     691        "NUMMULTIRANGE": TokenType.NUMMULTIRANGE,
     692        "TSRANGE": TokenType.TSRANGE,
     693        "TSMULTIRANGE": TokenType.TSMULTIRANGE,
     694        "TSTZRANGE": TokenType.TSTZRANGE,
     695        "TSTZMULTIRANGE": TokenType.TSTZMULTIRANGE,
     696        "DATERANGE": TokenType.DATERANGE,
     697        "DATEMULTIRANGE": TokenType.DATEMULTIRANGE,
     698        "UNIQUE": TokenType.UNIQUE,
     699        "STRUCT": TokenType.STRUCT,
     700        "VARIANT": TokenType.VARIANT,
     701        "ALTER": TokenType.ALTER,
     702        "ANALYZE": TokenType.COMMAND,
     703        "CALL": TokenType.COMMAND,
     704        "COMMENT": TokenType.COMMENT,
     705        "COPY": TokenType.COMMAND,
     706        "EXPLAIN": TokenType.COMMAND,
     707        "GRANT": TokenType.COMMAND,
     708        "OPTIMIZE": TokenType.COMMAND,
     709        "PREPARE": TokenType.COMMAND,
     710        "TRUNCATE": TokenType.COMMAND,
     711        "VACUUM": TokenType.COMMAND,
     712    }
     713
     714    WHITE_SPACE: t.Dict[t.Optional[str], TokenType] = {
     715        " ": TokenType.SPACE,
     716        "\t": TokenType.SPACE,
     717        "\n": TokenType.BREAK,
     718        "\r": TokenType.BREAK,
     719        "\r\n": TokenType.BREAK,
     720    }
     721
     722    COMMANDS = {
     723        TokenType.COMMAND,
     724        TokenType.EXECUTE,
     725        TokenType.FETCH,
     726        TokenType.SHOW,
     727    }
     728
     729    COMMAND_PREFIX_TOKENS = {TokenType.SEMICOLON, TokenType.BEGIN}
     730
     731    # handle numeric literals like in hive (3L = BIGINT)
     732    NUMERIC_LITERALS: t.Dict[str, str] = {}
     733    ENCODE: t.Optional[str] = None
     734
     735    COMMENTS = ["--", ("/*", "*/"), ("{#", "#}")]
     736    KEYWORD_TRIE: t.Dict = {}  # autofilled
     737
     738    __slots__ = (
     739        "sql",
     740        "size",
     741        "tokens",
     742        "_start",
     743        "_current",
     744        "_line",
     745        "_col",
     746        "_comments",
     747        "_char",
     748        "_end",
     749        "_peek",
     750        "_prev_token_line",
     751        "identifiers_can_start_with_digit",
     752    )
     753
     754    def __init__(self) -> None:
     755        self.reset()
     756
     757    def reset(self) -> None:
     758        self.sql = ""
     759        self.size = 0
     760        self.tokens: t.List[Token] = []
     761        self._start = 0
     762        self._current = 0
     763        self._line = 1
     764        self._col = 0
     765        self._comments: t.List[str] = []
     766
     767        self._char = ""
     768        self._end = False
     769        self._peek = ""
     770        self._prev_token_line = -1
     771
     772    def tokenize(self, sql: str) -> t.List[Token]:
     773        """Returns a list of tokens corresponding to the SQL string `sql`."""
     774        self.reset()
     775        self.sql = sql
     776        self.size = len(sql)
     777
     778        try:
     779            self._scan()
     780        except Exception as e:
     781            start = max(self._current - 50, 0)
     782            end = min(self._current + 50, self.size - 1)
     783            context = self.sql[start:end]
     784            raise ValueError(f"Error tokenizing '{context}'") from e
     785
     786        return self.tokens
     787
     788    def _scan(self, until: t.Optional[t.Callable] = None) -> None:
     789        while self.size and not self._end:
     790            self._start = self._current
     791            self._advance()
     792
     793            if self._char is None:
     794                break
     795
     796            if self._char not in self.WHITE_SPACE:
     797                if self._char.isdigit():
     798                    self._scan_number()
     799                elif self._char in self._IDENTIFIERS:
     800                    self._scan_identifier(self._IDENTIFIERS[self._char])
     801                else:
     802                    self._scan_keywords()
     803
     804            if until and until():
     805                break
     806
     807        if self.tokens and self._comments:
     808            self.tokens[-1].comments.extend(self._comments)
     809
     810    def _chars(self, size: int) -> str:
     811        if size == 1:
     812            return self._char
     813
     814        start = self._current - 1
     815        end = start + size
     816
     817        return self.sql[start:end] if end <= self.size else ""
     818
     819    def _advance(self, i: int = 1, alnum: bool = False) -> None:
     820        if self.WHITE_SPACE.get(self._char) is TokenType.BREAK:
     821            self._col = 1
     822            self._line += 1
     823        else:
     824            self._col += i
     825
     826        self._current += i
     827        self._end = self._current >= self.size
     828        self._char = self.sql[self._current - 1]
     829        self._peek = "" if self._end else self.sql[self._current]
     830
     831        if alnum and self._char.isalnum():
     832            # Here we use local variables instead of attributes for better performance
     833            _col = self._col
     834            _current = self._current
     835            _end = self._end
     836            _peek = self._peek
     837
     838            while _peek.isalnum():
     839                _col += 1
     840                _current += 1
     841                _end = _current >= self.size
     842                _peek = "" if _end else self.sql[_current]
     843
     844            self._col = _col
     845            self._current = _current
     846            self._end = _end
     847            self._peek = _peek
     848            self._char = self.sql[_current - 1]
     849
     850    @property
     851    def _text(self) -> str:
     852        return self.sql[self._start : self._current]
     853
     854    def _add(self, token_type: TokenType, text: t.Optional[str] = None) -> None:
     855        self._prev_token_line = self._line
     856        self.tokens.append(
     857            Token(
     858                token_type,
     859                text=self._text if text is None else text,
     860                line=self._line,
     861                col=self._col,
     862                start=self._start,
     863                end=self._current - 1,
     864                comments=self._comments,
     865            )
     866        )
     867        self._comments = []
     868
     869        # If we have either a semicolon or a begin token before the command's token, we'll parse
     870        # whatever follows the command's token as a string
     871        if (
     872            token_type in self.COMMANDS
     873            and self._peek != ";"
     874            and (len(self.tokens) == 1 or self.tokens[-2].token_type in self.COMMAND_PREFIX_TOKENS)
     875        ):
     876            start = self._current
     877            tokens = len(self.tokens)
     878            self._scan(lambda: self._peek == ";")
     879            self.tokens = self.tokens[:tokens]
     880            text = self.sql[start : self._current].strip()
     881            if text:
     882                self._add(TokenType.STRING, text)
     883
     884    def _scan_keywords(self) -> None:
     885        size = 0
     886        word = None
     887        chars = self._text
     888        char = chars
     889        prev_space = False
     890        skip = False
     891        trie = self.KEYWORD_TRIE
     892        single_token = char in self.SINGLE_TOKENS
     893
     894        while chars:
     895            if skip:
     896                result = 1
     897            else:
     898                result, trie = in_trie(trie, char.upper())
     899
     900            if result == 0:
     901                break
     902            if result == 2:
     903                word = chars
     904
     905            size += 1
     906            end = self._current - 1 + size
     907
     908            if end < self.size:
     909                char = self.sql[end]
     910                single_token = single_token or char in self.SINGLE_TOKENS
     911                is_space = char in self.WHITE_SPACE
     912
     913                if not is_space or not prev_space:
     914                    if is_space:
     915                        char = " "
     916                    chars += char
     917                    prev_space = is_space
     918                    skip = False
     919                else:
     920                    skip = True
     921            else:
     922                char = ""
     923                chars = " "
     924
     925        word = None if not single_token and chars[-1] not in self.WHITE_SPACE else word
     926
     927        if not word:
     928            if self._char in self.SINGLE_TOKENS:
     929                self._add(self.SINGLE_TOKENS[self._char], text=self._char)
     930                return
     931            self._scan_var()
     932            return
     933
     934        if self._scan_string(word):
     935            return
     936        if self._scan_comment(word):
     937            return
     938
     939        self._advance(size - 1)
     940        word = word.upper()
     941        self._add(self.KEYWORDS[word], text=word)
     942
     943    def _scan_comment(self, comment_start: str) -> bool:
     944        if comment_start not in self._COMMENTS:
     945            return False
     946
     947        comment_start_line = self._line
     948        comment_start_size = len(comment_start)
     949        comment_end = self._COMMENTS[comment_start]
     950
     951        if comment_end:
     952            # Skip the comment's start delimiter
     953            self._advance(comment_start_size)
     954
     955            comment_end_size = len(comment_end)
     956            while not self._end and self._chars(comment_end_size) != comment_end:
     957                self._advance(alnum=True)
     958
     959            self._comments.append(self._text[comment_start_size : -comment_end_size + 1])
     960            self._advance(comment_end_size - 1)
     961        else:
     962            while not self._end and not self.WHITE_SPACE.get(self._peek) is TokenType.BREAK:
     963                self._advance(alnum=True)
     964            self._comments.append(self._text[comment_start_size:])
     965
     966        # Leading comment is attached to the succeeding token, whilst trailing comment to the preceding.
     967        # Multiple consecutive comments are preserved by appending them to the current comments list.
     968        if comment_start_line == self._prev_token_line:
     969            self.tokens[-1].comments.extend(self._comments)
     970            self._comments = []
     971            self._prev_token_line = self._line
     972
     973        return True
     974
     975    def _scan_number(self) -> None:
     976        if self._char == "0":
     977            peek = self._peek.upper()
     978            if peek == "B":
     979                return self._scan_bits() if self.BIT_STRINGS else self._add(TokenType.NUMBER)
     980            elif peek == "X":
     981                return self._scan_hex() if self.HEX_STRINGS else self._add(TokenType.NUMBER)
     982
     983        decimal = False
     984        scientific = 0
     985
     986        while True:
     987            if self._peek.isdigit():
     988                self._advance()
     989            elif self._peek == "." and not decimal:
     990                decimal = True
     991                self._advance()
     992            elif self._peek in ("-", "+") and scientific == 1:
     993                scientific += 1
     994                self._advance()
     995            elif self._peek.upper() == "E" and not scientific:
     996                scientific += 1
     997                self._advance()
     998            elif self._peek.isidentifier():
     999                number_text = self._text
    1000                literal = ""
    1001
    1002                while self._peek.strip() and self._peek not in self.SINGLE_TOKENS:
    1003                    literal += self._peek.upper()
    1004                    self._advance()
    1005
    1006                token_type = self.KEYWORDS.get(self.NUMERIC_LITERALS.get(literal))
    1007
    1008                if token_type:
    1009                    self._add(TokenType.NUMBER, number_text)
    1010                    self._add(TokenType.DCOLON, "::")
    1011                    return self._add(token_type, literal)
    1012                elif self.identifiers_can_start_with_digit:  # type: ignore
    1013                    return self._add(TokenType.VAR)
    1014
    1015                self._add(TokenType.NUMBER, number_text)
    1016                return self._advance(-len(literal))
    1017            else:
    1018                return self._add(TokenType.NUMBER)
    1019
    1020    def _scan_bits(self) -> None:
    1021        self._advance()
    1022        value = self._extract_value()
    1023        try:
    1024            # If `value` can't be converted to a binary, fallback to tokenizing it as an identifier
    1025            int(value, 2)
    1026            self._add(TokenType.BIT_STRING, value[2:])  # Drop the 0b
    1027        except ValueError:
    1028            self._add(TokenType.IDENTIFIER)
    1029
    1030    def _scan_hex(self) -> None:
    1031        self._advance()
    1032        value = self._extract_value()
    1033        try:
    1034            # If `value` can't be converted to a hex, fallback to tokenizing it as an identifier
    1035            int(value, 16)
    1036            self._add(TokenType.HEX_STRING, value[2:])  # Drop the 0x
    1037        except ValueError:
    1038            self._add(TokenType.IDENTIFIER)
    1039
    1040    def _extract_value(self) -> str:
    1041        while True:
    1042            char = self._peek.strip()
    1043            if char and char not in self.SINGLE_TOKENS:
    1044                self._advance(alnum=True)
    1045            else:
    1046                break
    1047
    1048        return self._text
    1049
    1050    def _scan_string(self, start: str) -> bool:
    1051        base = None
    1052        token_type = TokenType.STRING
    1053
    1054        if start in self._QUOTES:
    1055            end = self._QUOTES[start]
    1056        elif start in self._FORMAT_STRINGS:
    1057            end, token_type = self._FORMAT_STRINGS[start]
    1058
    1059            if token_type == TokenType.HEX_STRING:
    1060                base = 16
    1061            elif token_type == TokenType.BIT_STRING:
    1062                base = 2
    1063        else:
    1064            return False
    1065
    1066        self._advance(len(start))
    1067        text = self._extract_string(end)
    1068
    1069        if base:
    1070            try:
    1071                int(text, base)
    1072            except:
    1073                raise RuntimeError(
    1074                    f"Numeric string contains invalid characters from {self._line}:{self._start}"
    1075                )
    1076        else:
    1077            text = text.encode(self.ENCODE).decode(self.ENCODE) if self.ENCODE else text
    1078
    1079        self._add(token_type, text)
    1080        return True
    1081
    1082    def _scan_identifier(self, identifier_end: str) -> None:
    1083        self._advance()
    1084        text = self._extract_string(identifier_end, self._IDENTIFIER_ESCAPES)
    1085        self._add(TokenType.IDENTIFIER, text)
    1086
    1087    def _scan_var(self) -> None:
    1088        while True:
    1089            char = self._peek.strip()
    1090            if char and (char in self.VAR_SINGLE_TOKENS or char not in self.SINGLE_TOKENS):
    1091                self._advance(alnum=True)
    1092            else:
    1093                break
    1094
    1095        self._add(
    1096            TokenType.VAR
    1097            if self.tokens and self.tokens[-1].token_type == TokenType.PARAMETER
    1098            else self.KEYWORDS.get(self._text.upper(), TokenType.VAR)
    1099        )
    1100
    1101    def _extract_string(self, delimiter: str, escapes=None) -> str:
    1102        text = ""
    1103        delim_size = len(delimiter)
    1104        escapes = self._STRING_ESCAPES if escapes is None else escapes
    1105
    1106        while True:
    1107            if self._char in escapes and (self._peek == delimiter or self._peek in escapes):
    1108                if self._peek == delimiter:
    1109                    text += self._peek
    1110                else:
    1111                    text += self._char + self._peek
    1112
    1113                if self._current + 1 < self.size:
    1114                    self._advance(2)
    1115                else:
    1116                    raise RuntimeError(f"Missing {delimiter} from {self._line}:{self._current}")
    1117            else:
    1118                if self._chars(delim_size) == delimiter:
    1119                    if delim_size > 1:
    1120                        self._advance(delim_size - 1)
    1121                    break
    1122
    1123                if self._end:
    1124                    raise RuntimeError(f"Missing {delimiter} from {self._line}:{self._start}")
    1125
    1126                current = self._current - 1
    1127                self._advance(alnum=True)
    1128                text += self.sql[current : self._current - 1]
    1129
    1130        return text

class TokenType(sqlglot.helper.AutoName): View Source

     11class TokenType(AutoName):
     12    L_PAREN = auto()
     13    R_PAREN = auto()
     14    L_BRACKET = auto()
     15    R_BRACKET = auto()
     16    L_BRACE = auto()
     17    R_BRACE = auto()
     18    COMMA = auto()
     19    DOT = auto()
     20    DASH = auto()
     21    PLUS = auto()
     22    COLON = auto()
     23    DCOLON = auto()
     24    SEMICOLON = auto()
     25    STAR = auto()
     26    BACKSLASH = auto()
     27    SLASH = auto()
     28    LT = auto()
     29    LTE = auto()
     30    GT = auto()
     31    GTE = auto()
     32    NOT = auto()
     33    EQ = auto()
     34    NEQ = auto()
     35    NULLSAFE_EQ = auto()
     36    AND = auto()
     37    OR = auto()
     38    AMP = auto()
     39    DPIPE = auto()
     40    PIPE = auto()
     41    CARET = auto()
     42    TILDA = auto()
     43    ARROW = auto()
     44    DARROW = auto()
     45    FARROW = auto()
     46    HASH = auto()
     47    HASH_ARROW = auto()
     48    DHASH_ARROW = auto()
     49    LR_ARROW = auto()
     50    LT_AT = auto()
     51    AT_GT = auto()
     52    DOLLAR = auto()
     53    PARAMETER = auto()
     54    SESSION_PARAMETER = auto()
     55    DAMP = auto()
     56
     57    BLOCK_START = auto()
     58    BLOCK_END = auto()
     59
     60    SPACE = auto()
     61    BREAK = auto()
     62
     63    STRING = auto()
     64    NUMBER = auto()
     65    IDENTIFIER = auto()
     66    DATABASE = auto()
     67    COLUMN = auto()
     68    COLUMN_DEF = auto()
     69    SCHEMA = auto()
     70    TABLE = auto()
     71    VAR = auto()
     72    BIT_STRING = auto()
     73    HEX_STRING = auto()
     74    BYTE_STRING = auto()
     75    NATIONAL_STRING = auto()
     76    RAW_STRING = auto()
     77
     78    # types
     79    BIT = auto()
     80    BOOLEAN = auto()
     81    TINYINT = auto()
     82    UTINYINT = auto()
     83    SMALLINT = auto()
     84    USMALLINT = auto()
     85    INT = auto()
     86    UINT = auto()
     87    BIGINT = auto()
     88    UBIGINT = auto()
     89    INT128 = auto()
     90    UINT128 = auto()
     91    INT256 = auto()
     92    UINT256 = auto()
     93    FLOAT = auto()
     94    DOUBLE = auto()
     95    DECIMAL = auto()
     96    BIGDECIMAL = auto()
     97    CHAR = auto()
     98    NCHAR = auto()
     99    VARCHAR = auto()
    100    NVARCHAR = auto()
    101    TEXT = auto()
    102    MEDIUMTEXT = auto()
    103    LONGTEXT = auto()
    104    MEDIUMBLOB = auto()
    105    LONGBLOB = auto()
    106    BINARY = auto()
    107    VARBINARY = auto()
    108    JSON = auto()
    109    JSONB = auto()
    110    TIME = auto()
    111    TIMESTAMP = auto()
    112    TIMESTAMPTZ = auto()
    113    TIMESTAMPLTZ = auto()
    114    DATETIME = auto()
    115    DATETIME64 = auto()
    116    DATE = auto()
    117    INT4RANGE = auto()
    118    INT4MULTIRANGE = auto()
    119    INT8RANGE = auto()
    120    INT8MULTIRANGE = auto()
    121    NUMRANGE = auto()
    122    NUMMULTIRANGE = auto()
    123    TSRANGE = auto()
    124    TSMULTIRANGE = auto()
    125    TSTZRANGE = auto()
    126    TSTZMULTIRANGE = auto()
    127    DATERANGE = auto()
    128    DATEMULTIRANGE = auto()
    129    UUID = auto()
    130    GEOGRAPHY = auto()
    131    NULLABLE = auto()
    132    GEOMETRY = auto()
    133    HLLSKETCH = auto()
    134    HSTORE = auto()
    135    SUPER = auto()
    136    SERIAL = auto()
    137    SMALLSERIAL = auto()
    138    BIGSERIAL = auto()
    139    XML = auto()
    140    UNIQUEIDENTIFIER = auto()
    141    MONEY = auto()
    142    SMALLMONEY = auto()
    143    ROWVERSION = auto()
    144    IMAGE = auto()
    145    VARIANT = auto()
    146    OBJECT = auto()
    147    INET = auto()
    148
    149    # keywords
    150    ALIAS = auto()
    151    ALTER = auto()
    152    ALWAYS = auto()
    153    ALL = auto()
    154    ANTI = auto()
    155    ANY = auto()
    156    APPLY = auto()
    157    ARRAY = auto()
    158    ASC = auto()
    159    ASOF = auto()
    160    AUTO_INCREMENT = auto()
    161    BEGIN = auto()
    162    BETWEEN = auto()
    163    CACHE = auto()
    164    CASE = auto()
    165    CHARACTER_SET = auto()
    166    COLLATE = auto()
    167    COMMAND = auto()
    168    COMMENT = auto()
    169    COMMIT = auto()
    170    CONSTRAINT = auto()
    171    CREATE = auto()
    172    CROSS = auto()
    173    CUBE = auto()
    174    CURRENT_DATE = auto()
    175    CURRENT_DATETIME = auto()
    176    CURRENT_TIME = auto()
    177    CURRENT_TIMESTAMP = auto()
    178    CURRENT_USER = auto()
    179    DEFAULT = auto()
    180    DELETE = auto()
    181    DESC = auto()
    182    DESCRIBE = auto()
    183    DICTIONARY = auto()
    184    DISTINCT = auto()
    185    DIV = auto()
    186    DROP = auto()
    187    ELSE = auto()
    188    END = auto()
    189    ESCAPE = auto()
    190    EXCEPT = auto()
    191    EXECUTE = auto()
    192    EXISTS = auto()
    193    FALSE = auto()
    194    FETCH = auto()
    195    FILTER = auto()
    196    FINAL = auto()
    197    FIRST = auto()
    198    FOR = auto()
    199    FOREIGN_KEY = auto()
    200    FORMAT = auto()
    201    FROM = auto()
    202    FULL = auto()
    203    FUNCTION = auto()
    204    GLOB = auto()
    205    GLOBAL = auto()
    206    GROUP_BY = auto()
    207    GROUPING_SETS = auto()
    208    HAVING = auto()
    209    HINT = auto()
    210    IF = auto()
    211    ILIKE = auto()
    212    ILIKE_ANY = auto()
    213    IN = auto()
    214    INDEX = auto()
    215    INNER = auto()
    216    INSERT = auto()
    217    INTERSECT = auto()
    218    INTERVAL = auto()
    219    INTO = auto()
    220    INTRODUCER = auto()
    221    IRLIKE = auto()
    222    IS = auto()
    223    ISNULL = auto()
    224    JOIN = auto()
    225    JOIN_MARKER = auto()
    226    KEEP = auto()
    227    LANGUAGE = auto()
    228    LATERAL = auto()
    229    LEFT = auto()
    230    LIKE = auto()
    231    LIKE_ANY = auto()
    232    LIMIT = auto()
    233    LOAD = auto()
    234    LOCK = auto()
    235    MAP = auto()
    236    MATCH_RECOGNIZE = auto()
    237    MERGE = auto()
    238    MOD = auto()
    239    NATURAL = auto()
    240    NEXT = auto()
    241    NEXT_VALUE_FOR = auto()
    242    NOTNULL = auto()
    243    NULL = auto()
    244    OFFSET = auto()
    245    ON = auto()
    246    ORDER_BY = auto()
    247    ORDERED = auto()
    248    ORDINALITY = auto()
    249    OUTER = auto()
    250    OVER = auto()
    251    OVERLAPS = auto()
    252    OVERWRITE = auto()
    253    PARTITION = auto()
    254    PARTITION_BY = auto()
    255    PERCENT = auto()
    256    PIVOT = auto()
    257    PLACEHOLDER = auto()
    258    PRAGMA = auto()
    259    PRIMARY_KEY = auto()
    260    PROCEDURE = auto()
    261    PROPERTIES = auto()
    262    PSEUDO_TYPE = auto()
    263    QUALIFY = auto()
    264    QUOTE = auto()
    265    RANGE = auto()
    266    RECURSIVE = auto()
    267    REPLACE = auto()
    268    RETURNING = auto()
    269    REFERENCES = auto()
    270    RIGHT = auto()
    271    RLIKE = auto()
    272    ROLLBACK = auto()
    273    ROLLUP = auto()
    274    ROW = auto()
    275    ROWS = auto()
    276    SELECT = auto()
    277    SEMI = auto()
    278    SEPARATOR = auto()
    279    SERDE_PROPERTIES = auto()
    280    SET = auto()
    281    SETTINGS = auto()
    282    SHOW = auto()
    283    SIMILAR_TO = auto()
    284    SOME = auto()
    285    STRUCT = auto()
    286    TABLE_SAMPLE = auto()
    287    TEMPORARY = auto()
    288    TOP = auto()
    289    THEN = auto()
    290    TRUE = auto()
    291    UNCACHE = auto()
    292    UNION = auto()
    293    UNNEST = auto()
    294    UNPIVOT = auto()
    295    UPDATE = auto()
    296    USE = auto()
    297    USING = auto()
    298    VALUES = auto()
    299    VIEW = auto()
    300    VOLATILE = auto()
    301    WHEN = auto()
    302    WHERE = auto()
    303    WINDOW = auto()
    304    WITH = auto()
    305    UNIQUE = auto()

An enumeration.

L_PAREN = <TokenType.L_PAREN: 'L_PAREN'>
R_PAREN = <TokenType.R_PAREN: 'R_PAREN'>
L_BRACKET = <TokenType.L_BRACKET: 'L_BRACKET'>
R_BRACKET = <TokenType.R_BRACKET: 'R_BRACKET'>
L_BRACE = <TokenType.L_BRACE: 'L_BRACE'>
R_BRACE = <TokenType.R_BRACE: 'R_BRACE'>
COMMA = <TokenType.COMMA: 'COMMA'>
DOT = <TokenType.DOT: 'DOT'>
DASH = <TokenType.DASH: 'DASH'>
PLUS = <TokenType.PLUS: 'PLUS'>
COLON = <TokenType.COLON: 'COLON'>
DCOLON = <TokenType.DCOLON: 'DCOLON'>
SEMICOLON = <TokenType.SEMICOLON: 'SEMICOLON'>
STAR = <TokenType.STAR: 'STAR'>
BACKSLASH = <TokenType.BACKSLASH: 'BACKSLASH'>
SLASH = <TokenType.SLASH: 'SLASH'>
LT = <TokenType.LT: 'LT'>
LTE = <TokenType.LTE: 'LTE'>
GT = <TokenType.GT: 'GT'>
GTE = <TokenType.GTE: 'GTE'>
NOT = <TokenType.NOT: 'NOT'>
EQ = <TokenType.EQ: 'EQ'>
NEQ = <TokenType.NEQ: 'NEQ'>
NULLSAFE_EQ = <TokenType.NULLSAFE_EQ: 'NULLSAFE_EQ'>
AND = <TokenType.AND: 'AND'>
OR = <TokenType.OR: 'OR'>
AMP = <TokenType.AMP: 'AMP'>
DPIPE = <TokenType.DPIPE: 'DPIPE'>
PIPE = <TokenType.PIPE: 'PIPE'>
CARET = <TokenType.CARET: 'CARET'>
TILDA = <TokenType.TILDA: 'TILDA'>
ARROW = <TokenType.ARROW: 'ARROW'>
DARROW = <TokenType.DARROW: 'DARROW'>
FARROW = <TokenType.FARROW: 'FARROW'>
HASH = <TokenType.HASH: 'HASH'>
HASH_ARROW = <TokenType.HASH_ARROW: 'HASH_ARROW'>
DHASH_ARROW = <TokenType.DHASH_ARROW: 'DHASH_ARROW'>
LR_ARROW = <TokenType.LR_ARROW: 'LR_ARROW'>
LT_AT = <TokenType.LT_AT: 'LT_AT'>
AT_GT = <TokenType.AT_GT: 'AT_GT'>
DOLLAR = <TokenType.DOLLAR: 'DOLLAR'>
PARAMETER = <TokenType.PARAMETER: 'PARAMETER'>
SESSION_PARAMETER = <TokenType.SESSION_PARAMETER: 'SESSION_PARAMETER'>
DAMP = <TokenType.DAMP: 'DAMP'>
BLOCK_START = <TokenType.BLOCK_START: 'BLOCK_START'>
BLOCK_END = <TokenType.BLOCK_END: 'BLOCK_END'>
SPACE = <TokenType.SPACE: 'SPACE'>
BREAK = <TokenType.BREAK: 'BREAK'>
STRING = <TokenType.STRING: 'STRING'>
NUMBER = <TokenType.NUMBER: 'NUMBER'>
IDENTIFIER = <TokenType.IDENTIFIER: 'IDENTIFIER'>
DATABASE = <TokenType.DATABASE: 'DATABASE'>
COLUMN = <TokenType.COLUMN: 'COLUMN'>
COLUMN_DEF = <TokenType.COLUMN_DEF: 'COLUMN_DEF'>
SCHEMA = <TokenType.SCHEMA: 'SCHEMA'>
TABLE = <TokenType.TABLE: 'TABLE'>
VAR = <TokenType.VAR: 'VAR'>
BIT_STRING = <TokenType.BIT_STRING: 'BIT_STRING'>
HEX_STRING = <TokenType.HEX_STRING: 'HEX_STRING'>
BYTE_STRING = <TokenType.BYTE_STRING: 'BYTE_STRING'>
NATIONAL_STRING = <TokenType.NATIONAL_STRING: 'NATIONAL_STRING'>
RAW_STRING = <TokenType.RAW_STRING: 'RAW_STRING'>
BIT = <TokenType.BIT: 'BIT'>
BOOLEAN = <TokenType.BOOLEAN: 'BOOLEAN'>
TINYINT = <TokenType.TINYINT: 'TINYINT'>
UTINYINT = <TokenType.UTINYINT: 'UTINYINT'>
SMALLINT = <TokenType.SMALLINT: 'SMALLINT'>
USMALLINT = <TokenType.USMALLINT: 'USMALLINT'>
INT = <TokenType.INT: 'INT'>
UINT = <TokenType.UINT: 'UINT'>
BIGINT = <TokenType.BIGINT: 'BIGINT'>
UBIGINT = <TokenType.UBIGINT: 'UBIGINT'>
INT128 = <TokenType.INT128: 'INT128'>
UINT128 = <TokenType.UINT128: 'UINT128'>
INT256 = <TokenType.INT256: 'INT256'>
UINT256 = <TokenType.UINT256: 'UINT256'>
FLOAT = <TokenType.FLOAT: 'FLOAT'>
DOUBLE = <TokenType.DOUBLE: 'DOUBLE'>
DECIMAL = <TokenType.DECIMAL: 'DECIMAL'>
BIGDECIMAL = <TokenType.BIGDECIMAL: 'BIGDECIMAL'>
CHAR = <TokenType.CHAR: 'CHAR'>
NCHAR = <TokenType.NCHAR: 'NCHAR'>
VARCHAR = <TokenType.VARCHAR: 'VARCHAR'>
NVARCHAR = <TokenType.NVARCHAR: 'NVARCHAR'>
TEXT = <TokenType.TEXT: 'TEXT'>
MEDIUMTEXT = <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>
LONGTEXT = <TokenType.LONGTEXT: 'LONGTEXT'>
MEDIUMBLOB = <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>
LONGBLOB = <TokenType.LONGBLOB: 'LONGBLOB'>
BINARY = <TokenType.BINARY: 'BINARY'>
VARBINARY = <TokenType.VARBINARY: 'VARBINARY'>
JSON = <TokenType.JSON: 'JSON'>
JSONB = <TokenType.JSONB: 'JSONB'>
TIME = <TokenType.TIME: 'TIME'>
TIMESTAMP = <TokenType.TIMESTAMP: 'TIMESTAMP'>
TIMESTAMPTZ = <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>
TIMESTAMPLTZ = <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>
DATETIME = <TokenType.DATETIME: 'DATETIME'>
DATETIME64 = <TokenType.DATETIME64: 'DATETIME64'>
DATE = <TokenType.DATE: 'DATE'>
INT4RANGE = <TokenType.INT4RANGE: 'INT4RANGE'>
INT4MULTIRANGE = <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>
INT8RANGE = <TokenType.INT8RANGE: 'INT8RANGE'>
INT8MULTIRANGE = <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>
NUMRANGE = <TokenType.NUMRANGE: 'NUMRANGE'>
NUMMULTIRANGE = <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>
TSRANGE = <TokenType.TSRANGE: 'TSRANGE'>
TSMULTIRANGE = <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>
TSTZRANGE = <TokenType.TSTZRANGE: 'TSTZRANGE'>
TSTZMULTIRANGE = <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>
DATERANGE = <TokenType.DATERANGE: 'DATERANGE'>
DATEMULTIRANGE = <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>
UUID = <TokenType.UUID: 'UUID'>
GEOGRAPHY = <TokenType.GEOGRAPHY: 'GEOGRAPHY'>
NULLABLE = <TokenType.NULLABLE: 'NULLABLE'>
GEOMETRY = <TokenType.GEOMETRY: 'GEOMETRY'>
HLLSKETCH = <TokenType.HLLSKETCH: 'HLLSKETCH'>
HSTORE = <TokenType.HSTORE: 'HSTORE'>
SUPER = <TokenType.SUPER: 'SUPER'>
SERIAL = <TokenType.SERIAL: 'SERIAL'>
SMALLSERIAL = <TokenType.SMALLSERIAL: 'SMALLSERIAL'>
BIGSERIAL = <TokenType.BIGSERIAL: 'BIGSERIAL'>
XML = <TokenType.XML: 'XML'>
UNIQUEIDENTIFIER = <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>
MONEY = <TokenType.MONEY: 'MONEY'>
SMALLMONEY = <TokenType.SMALLMONEY: 'SMALLMONEY'>
ROWVERSION = <TokenType.ROWVERSION: 'ROWVERSION'>
IMAGE = <TokenType.IMAGE: 'IMAGE'>
VARIANT = <TokenType.VARIANT: 'VARIANT'>
OBJECT = <TokenType.OBJECT: 'OBJECT'>
INET = <TokenType.INET: 'INET'>
ALIAS = <TokenType.ALIAS: 'ALIAS'>
ALTER = <TokenType.ALTER: 'ALTER'>
ALWAYS = <TokenType.ALWAYS: 'ALWAYS'>
ALL = <TokenType.ALL: 'ALL'>
ANTI = <TokenType.ANTI: 'ANTI'>
ANY = <TokenType.ANY: 'ANY'>
APPLY = <TokenType.APPLY: 'APPLY'>
ARRAY = <TokenType.ARRAY: 'ARRAY'>
ASC = <TokenType.ASC: 'ASC'>
ASOF = <TokenType.ASOF: 'ASOF'>
AUTO_INCREMENT = <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>
BEGIN = <TokenType.BEGIN: 'BEGIN'>
BETWEEN = <TokenType.BETWEEN: 'BETWEEN'>
CACHE = <TokenType.CACHE: 'CACHE'>
CASE = <TokenType.CASE: 'CASE'>
CHARACTER_SET = <TokenType.CHARACTER_SET: 'CHARACTER_SET'>
COLLATE = <TokenType.COLLATE: 'COLLATE'>
COMMAND = <TokenType.COMMAND: 'COMMAND'>
COMMENT = <TokenType.COMMENT: 'COMMENT'>
COMMIT = <TokenType.COMMIT: 'COMMIT'>
CONSTRAINT = <TokenType.CONSTRAINT: 'CONSTRAINT'>
CREATE = <TokenType.CREATE: 'CREATE'>
CROSS = <TokenType.CROSS: 'CROSS'>
CUBE = <TokenType.CUBE: 'CUBE'>
CURRENT_DATE = <TokenType.CURRENT_DATE: 'CURRENT_DATE'>
CURRENT_DATETIME = <TokenType.CURRENT_DATETIME: 'CURRENT_DATETIME'>
CURRENT_TIME = <TokenType.CURRENT_TIME: 'CURRENT_TIME'>
CURRENT_TIMESTAMP = <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>
CURRENT_USER = <TokenType.CURRENT_USER: 'CURRENT_USER'>
DEFAULT = <TokenType.DEFAULT: 'DEFAULT'>
DELETE = <TokenType.DELETE: 'DELETE'>
DESC = <TokenType.DESC: 'DESC'>
DESCRIBE = <TokenType.DESCRIBE: 'DESCRIBE'>
DICTIONARY = <TokenType.DICTIONARY: 'DICTIONARY'>
DISTINCT = <TokenType.DISTINCT: 'DISTINCT'>
DIV = <TokenType.DIV: 'DIV'>
DROP = <TokenType.DROP: 'DROP'>
ELSE = <TokenType.ELSE: 'ELSE'>
END = <TokenType.END: 'END'>
ESCAPE = <TokenType.ESCAPE: 'ESCAPE'>
EXCEPT = <TokenType.EXCEPT: 'EXCEPT'>
EXECUTE = <TokenType.EXECUTE: 'EXECUTE'>
EXISTS = <TokenType.EXISTS: 'EXISTS'>
FALSE = <TokenType.FALSE: 'FALSE'>
FETCH = <TokenType.FETCH: 'FETCH'>
FILTER = <TokenType.FILTER: 'FILTER'>
FINAL = <TokenType.FINAL: 'FINAL'>
FIRST = <TokenType.FIRST: 'FIRST'>
FOR = <TokenType.FOR: 'FOR'>
FOREIGN_KEY = <TokenType.FOREIGN_KEY: 'FOREIGN_KEY'>
FORMAT = <TokenType.FORMAT: 'FORMAT'>
FROM = <TokenType.FROM: 'FROM'>
FULL = <TokenType.FULL: 'FULL'>
FUNCTION = <TokenType.FUNCTION: 'FUNCTION'>
GLOB = <TokenType.GLOB: 'GLOB'>
GLOBAL = <TokenType.GLOBAL: 'GLOBAL'>
GROUP_BY = <TokenType.GROUP_BY: 'GROUP_BY'>
GROUPING_SETS = <TokenType.GROUPING_SETS: 'GROUPING_SETS'>
HAVING = <TokenType.HAVING: 'HAVING'>
HINT = <TokenType.HINT: 'HINT'>
IF = <TokenType.IF: 'IF'>
ILIKE = <TokenType.ILIKE: 'ILIKE'>
ILIKE_ANY = <TokenType.ILIKE_ANY: 'ILIKE_ANY'>
IN = <TokenType.IN: 'IN'>
INDEX = <TokenType.INDEX: 'INDEX'>
INNER = <TokenType.INNER: 'INNER'>
INSERT = <TokenType.INSERT: 'INSERT'>
INTERSECT = <TokenType.INTERSECT: 'INTERSECT'>
INTERVAL = <TokenType.INTERVAL: 'INTERVAL'>
INTO = <TokenType.INTO: 'INTO'>
INTRODUCER = <TokenType.INTRODUCER: 'INTRODUCER'>
IRLIKE = <TokenType.IRLIKE: 'IRLIKE'>
IS = <TokenType.IS: 'IS'>
ISNULL = <TokenType.ISNULL: 'ISNULL'>
JOIN = <TokenType.JOIN: 'JOIN'>
JOIN_MARKER = <TokenType.JOIN_MARKER: 'JOIN_MARKER'>
KEEP = <TokenType.KEEP: 'KEEP'>
LANGUAGE = <TokenType.LANGUAGE: 'LANGUAGE'>
LATERAL = <TokenType.LATERAL: 'LATERAL'>
LEFT = <TokenType.LEFT: 'LEFT'>
LIKE = <TokenType.LIKE: 'LIKE'>
LIKE_ANY = <TokenType.LIKE_ANY: 'LIKE_ANY'>
LIMIT = <TokenType.LIMIT: 'LIMIT'>
LOAD = <TokenType.LOAD: 'LOAD'>
LOCK = <TokenType.LOCK: 'LOCK'>
MAP = <TokenType.MAP: 'MAP'>
MATCH_RECOGNIZE = <TokenType.MATCH_RECOGNIZE: 'MATCH_RECOGNIZE'>
MERGE = <TokenType.MERGE: 'MERGE'>
MOD = <TokenType.MOD: 'MOD'>
NATURAL = <TokenType.NATURAL: 'NATURAL'>
NEXT = <TokenType.NEXT: 'NEXT'>
NEXT_VALUE_FOR = <TokenType.NEXT_VALUE_FOR: 'NEXT_VALUE_FOR'>
NOTNULL = <TokenType.NOTNULL: 'NOTNULL'>
NULL = <TokenType.NULL: 'NULL'>
OFFSET = <TokenType.OFFSET: 'OFFSET'>
ON = <TokenType.ON: 'ON'>
ORDER_BY = <TokenType.ORDER_BY: 'ORDER_BY'>
ORDERED = <TokenType.ORDERED: 'ORDERED'>
ORDINALITY = <TokenType.ORDINALITY: 'ORDINALITY'>
OUTER = <TokenType.OUTER: 'OUTER'>
OVER = <TokenType.OVER: 'OVER'>
OVERLAPS = <TokenType.OVERLAPS: 'OVERLAPS'>
OVERWRITE = <TokenType.OVERWRITE: 'OVERWRITE'>
PARTITION = <TokenType.PARTITION: 'PARTITION'>
PARTITION_BY = <TokenType.PARTITION_BY: 'PARTITION_BY'>
PERCENT = <TokenType.PERCENT: 'PERCENT'>
PIVOT = <TokenType.PIVOT: 'PIVOT'>
PLACEHOLDER = <TokenType.PLACEHOLDER: 'PLACEHOLDER'>
PRAGMA = <TokenType.PRAGMA: 'PRAGMA'>
PRIMARY_KEY = <TokenType.PRIMARY_KEY: 'PRIMARY_KEY'>
PROCEDURE = <TokenType.PROCEDURE: 'PROCEDURE'>
PROPERTIES = <TokenType.PROPERTIES: 'PROPERTIES'>
PSEUDO_TYPE = <TokenType.PSEUDO_TYPE: 'PSEUDO_TYPE'>
QUALIFY = <TokenType.QUALIFY: 'QUALIFY'>
QUOTE = <TokenType.QUOTE: 'QUOTE'>
RANGE = <TokenType.RANGE: 'RANGE'>
RECURSIVE = <TokenType.RECURSIVE: 'RECURSIVE'>
REPLACE = <TokenType.REPLACE: 'REPLACE'>
RETURNING = <TokenType.RETURNING: 'RETURNING'>
REFERENCES = <TokenType.REFERENCES: 'REFERENCES'>
RIGHT = <TokenType.RIGHT: 'RIGHT'>
RLIKE = <TokenType.RLIKE: 'RLIKE'>
ROLLBACK = <TokenType.ROLLBACK: 'ROLLBACK'>
ROLLUP = <TokenType.ROLLUP: 'ROLLUP'>
ROW = <TokenType.ROW: 'ROW'>
ROWS = <TokenType.ROWS: 'ROWS'>
SELECT = <TokenType.SELECT: 'SELECT'>
SEMI = <TokenType.SEMI: 'SEMI'>
SEPARATOR = <TokenType.SEPARATOR: 'SEPARATOR'>
SERDE_PROPERTIES = <TokenType.SERDE_PROPERTIES: 'SERDE_PROPERTIES'>
SET = <TokenType.SET: 'SET'>
SETTINGS = <TokenType.SETTINGS: 'SETTINGS'>
SHOW = <TokenType.SHOW: 'SHOW'>
SIMILAR_TO = <TokenType.SIMILAR_TO: 'SIMILAR_TO'>
SOME = <TokenType.SOME: 'SOME'>
STRUCT = <TokenType.STRUCT: 'STRUCT'>
TABLE_SAMPLE = <TokenType.TABLE_SAMPLE: 'TABLE_SAMPLE'>
TEMPORARY = <TokenType.TEMPORARY: 'TEMPORARY'>
TOP = <TokenType.TOP: 'TOP'>
THEN = <TokenType.THEN: 'THEN'>
TRUE = <TokenType.TRUE: 'TRUE'>
UNCACHE = <TokenType.UNCACHE: 'UNCACHE'>
UNION = <TokenType.UNION: 'UNION'>
UNNEST = <TokenType.UNNEST: 'UNNEST'>
UNPIVOT = <TokenType.UNPIVOT: 'UNPIVOT'>
UPDATE = <TokenType.UPDATE: 'UPDATE'>
USE = <TokenType.USE: 'USE'>
USING = <TokenType.USING: 'USING'>
VALUES = <TokenType.VALUES: 'VALUES'>
VIEW = <TokenType.VIEW: 'VIEW'>
VOLATILE = <TokenType.VOLATILE: 'VOLATILE'>
WHEN = <TokenType.WHEN: 'WHEN'>
WHERE = <TokenType.WHERE: 'WHERE'>
WINDOW = <TokenType.WINDOW: 'WINDOW'>
WITH = <TokenType.WITH: 'WITH'>
UNIQUE = <TokenType.UNIQUE: 'UNIQUE'>

##### Inherited Members

enum.Enum
  name
  value

class Token: View Source

    308class Token:
    309    __slots__ = ("token_type", "text", "line", "col", "start", "end", "comments")
    310
    311    @classmethod
    312    def number(cls, number: int) -> Token:
    313        """Returns a NUMBER token with `number` as its text."""
    314        return cls(TokenType.NUMBER, str(number))
    315
    316    @classmethod
    317    def string(cls, string: str) -> Token:
    318        """Returns a STRING token with `string` as its text."""
    319        return cls(TokenType.STRING, string)
    320
    321    @classmethod
    322    def identifier(cls, identifier: str) -> Token:
    323        """Returns an IDENTIFIER token with `identifier` as its text."""
    324        return cls(TokenType.IDENTIFIER, identifier)
    325
    326    @classmethod
    327    def var(cls, var: str) -> Token:
    328        """Returns an VAR token with `var` as its text."""
    329        return cls(TokenType.VAR, var)
    330
    331    def __init__(
    332        self,
    333        token_type: TokenType,
    334        text: str,
    335        line: int = 1,
    336        col: int = 1,
    337        start: int = 0,
    338        end: int = 0,
    339        comments: t.List[str] = [],
    340    ) -> None:
    341        """Token initializer.
    342
    343        Args:
    344            token_type: The TokenType Enum.
    345            text: The text of the token.
    346            line: The line that the token ends on.
    347            col: The column that the token ends on.
    348            start: The start index of the token.
    349            end: The ending index of the token.
    350        """
    351        self.token_type = token_type
    352        self.text = text
    353        self.line = line
    354        self.col = col
    355        self.start = start
    356        self.end = end
    357        self.comments = comments
    358
    359    def __repr__(self) -> str:
    360        attributes = ", ".join(f"{k}: {getattr(self, k)}" for k in self.__slots__)
    361        return f"<Token {attributes}>"

Token( token_type: sqlglot.tokens.TokenType, text: str, line: int = 1, col: int = 1, start: int = 0, end: int = 0, comments: List[str] = []) View Source

    331    def __init__(
    332        self,
    333        token_type: TokenType,
    334        text: str,
    335        line: int = 1,
    336        col: int = 1,
    337        start: int = 0,
    338        end: int = 0,
    339        comments: t.List[str] = [],
    340    ) -> None:
    341        """Token initializer.
    342
    343        Args:
    344            token_type: The TokenType Enum.
    345            text: The text of the token.
    346            line: The line that the token ends on.
    347            col: The column that the token ends on.
    348            start: The start index of the token.
    349            end: The ending index of the token.
    350        """
    351        self.token_type = token_type
    352        self.text = text
    353        self.line = line
    354        self.col = col
    355        self.start = start
    356        self.end = end
    357        self.comments = comments

Token initializer.

###### Arguments:

- token_type: The TokenType Enum.
- text: The text of the token.
- line: The line that the token ends on.
- col: The column that the token ends on.
- start: The start index of the token.
- end: The ending index of the token.

@classmethod
def number(cls, number: int) -> sqlglot.tokens.Token: View Source

    311    @classmethod
    312    def number(cls, number: int) -> Token:
    313        """Returns a NUMBER token with `number` as its text."""
    314        return cls(TokenType.NUMBER, str(number))

Returns a NUMBER token with `number` as its text.

@classmethod
def string(cls, string: str) -> sqlglot.tokens.Token: View Source

    316    @classmethod
    317    def string(cls, string: str) -> Token:
    318        """Returns a STRING token with `string` as its text."""
    319        return cls(TokenType.STRING, string)

Returns a STRING token with `string` as its text.

@classmethod
def identifier(cls, identifier: str) -> sqlglot.tokens.Token: View Source

    321    @classmethod
    322    def identifier(cls, identifier: str) -> Token:
    323        """Returns an IDENTIFIER token with `identifier` as its text."""
    324        return cls(TokenType.IDENTIFIER, identifier)

Returns an IDENTIFIER token with `identifier` as its text.

@classmethod
def var(cls, var: str) -> sqlglot.tokens.Token: View Source

    326    @classmethod
    327    def var(cls, var: str) -> Token:
    328        """Returns an VAR token with `var` as its text."""
    329        return cls(TokenType.VAR, var)

Returns an VAR token with `var` as its text.

class Tokenizer: View Source

     414class Tokenizer(metaclass=_Tokenizer):
     415    SINGLE_TOKENS = {
     416        "(": TokenType.L_PAREN,
     417        ")": TokenType.R_PAREN,
     418        "[": TokenType.L_BRACKET,
     419        "]": TokenType.R_BRACKET,
     420        "{": TokenType.L_BRACE,
     421        "}": TokenType.R_BRACE,
     422        "&": TokenType.AMP,
     423        "^": TokenType.CARET,
     424        ":": TokenType.COLON,
     425        ",": TokenType.COMMA,
     426        ".": TokenType.DOT,
     427        "-": TokenType.DASH,
     428        "=": TokenType.EQ,
     429        ">": TokenType.GT,
     430        "<": TokenType.LT,
     431        "%": TokenType.MOD,
     432        "!": TokenType.NOT,
     433        "|": TokenType.PIPE,
     434        "+": TokenType.PLUS,
     435        ";": TokenType.SEMICOLON,
     436        "/": TokenType.SLASH,
     437        "\\": TokenType.BACKSLASH,
     438        "*": TokenType.STAR,
     439        "~": TokenType.TILDA,
     440        "?": TokenType.PLACEHOLDER,
     441        "@": TokenType.PARAMETER,
     442        # used for breaking a var like x'y' but nothing else
     443        # the token type doesn't matter
     444        "'": TokenType.QUOTE,
     445        "`": TokenType.IDENTIFIER,
     446        '"': TokenType.IDENTIFIER,
     447        "#": TokenType.HASH,
     448    }
     449
     450    BIT_STRINGS: t.List[str | t.Tuple[str, str]] = []
     451    BYTE_STRINGS: t.List[str | t.Tuple[str, str]] = []
     452    HEX_STRINGS: t.List[str | t.Tuple[str, str]] = []
     453    RAW_STRINGS: t.List[str | t.Tuple[str, str]] = []
     454    IDENTIFIERS: t.List[str | t.Tuple[str, str]] = ['"']
     455    IDENTIFIER_ESCAPES = ['"']
     456    QUOTES: t.List[t.Tuple[str, str] | str] = ["'"]
     457    STRING_ESCAPES = ["'"]
     458    VAR_SINGLE_TOKENS: t.Set[str] = set()
     459
     460    _COMMENTS: t.Dict[str, str] = {}
     461    _FORMAT_STRINGS: t.Dict[str, t.Tuple[str, TokenType]] = {}
     462    _IDENTIFIERS: t.Dict[str, str] = {}
     463    _IDENTIFIER_ESCAPES: t.Set[str] = set()
     464    _QUOTES: t.Dict[str, str] = {}
     465    _STRING_ESCAPES: t.Set[str] = set()
     466
     467    KEYWORDS: t.Dict[t.Optional[str], TokenType] = {
     468        **{f"{{%{postfix}": TokenType.BLOCK_START for postfix in ("", "+", "-")},
     469        **{f"{prefix}%}}": TokenType.BLOCK_END for prefix in ("", "+", "-")},
     470        "{{+": TokenType.BLOCK_START,
     471        "{{-": TokenType.BLOCK_START,
     472        "+}}": TokenType.BLOCK_END,
     473        "-}}": TokenType.BLOCK_END,
     474        "/*+": TokenType.HINT,
     475        "==": TokenType.EQ,
     476        "::": TokenType.DCOLON,
     477        "||": TokenType.DPIPE,
     478        ">=": TokenType.GTE,
     479        "<=": TokenType.LTE,
     480        "<>": TokenType.NEQ,
     481        "!=": TokenType.NEQ,
     482        "<=>": TokenType.NULLSAFE_EQ,
     483        "->": TokenType.ARROW,
     484        "->>": TokenType.DARROW,
     485        "=>": TokenType.FARROW,
     486        "#>": TokenType.HASH_ARROW,
     487        "#>>": TokenType.DHASH_ARROW,
     488        "<->": TokenType.LR_ARROW,
     489        "&&": TokenType.DAMP,
     490        "ALL": TokenType.ALL,
     491        "ALWAYS": TokenType.ALWAYS,
     492        "AND": TokenType.AND,
     493        "ANTI": TokenType.ANTI,
     494        "ANY": TokenType.ANY,
     495        "ASC": TokenType.ASC,
     496        "AS": TokenType.ALIAS,
     497        "ASOF": TokenType.ASOF,
     498        "AUTOINCREMENT": TokenType.AUTO_INCREMENT,
     499        "AUTO_INCREMENT": TokenType.AUTO_INCREMENT,
     500        "BEGIN": TokenType.BEGIN,
     501        "BETWEEN": TokenType.BETWEEN,
     502        "CACHE": TokenType.CACHE,
     503        "UNCACHE": TokenType.UNCACHE,
     504        "CASE": TokenType.CASE,
     505        "CHARACTER SET": TokenType.CHARACTER_SET,
     506        "COLLATE": TokenType.COLLATE,
     507        "COLUMN": TokenType.COLUMN,
     508        "COMMIT": TokenType.COMMIT,
     509        "CONSTRAINT": TokenType.CONSTRAINT,
     510        "CREATE": TokenType.CREATE,
     511        "CROSS": TokenType.CROSS,
     512        "CUBE": TokenType.CUBE,
     513        "CURRENT_DATE": TokenType.CURRENT_DATE,
     514        "CURRENT_TIME": TokenType.CURRENT_TIME,
     515        "CURRENT_TIMESTAMP": TokenType.CURRENT_TIMESTAMP,
     516        "CURRENT_USER": TokenType.CURRENT_USER,
     517        "DATABASE": TokenType.DATABASE,
     518        "DEFAULT": TokenType.DEFAULT,
     519        "DELETE": TokenType.DELETE,
     520        "DESC": TokenType.DESC,
     521        "DESCRIBE": TokenType.DESCRIBE,
     522        "DISTINCT": TokenType.DISTINCT,
     523        "DIV": TokenType.DIV,
     524        "DROP": TokenType.DROP,
     525        "ELSE": TokenType.ELSE,
     526        "END": TokenType.END,
     527        "ESCAPE": TokenType.ESCAPE,
     528        "EXCEPT": TokenType.EXCEPT,
     529        "EXECUTE": TokenType.EXECUTE,
     530        "EXISTS": TokenType.EXISTS,
     531        "FALSE": TokenType.FALSE,
     532        "FETCH": TokenType.FETCH,
     533        "FILTER": TokenType.FILTER,
     534        "FIRST": TokenType.FIRST,
     535        "FULL": TokenType.FULL,
     536        "FUNCTION": TokenType.FUNCTION,
     537        "FOR": TokenType.FOR,
     538        "FOREIGN KEY": TokenType.FOREIGN_KEY,
     539        "FORMAT": TokenType.FORMAT,
     540        "FROM": TokenType.FROM,
     541        "GEOGRAPHY": TokenType.GEOGRAPHY,
     542        "GEOMETRY": TokenType.GEOMETRY,
     543        "GLOB": TokenType.GLOB,
     544        "GROUP BY": TokenType.GROUP_BY,
     545        "GROUPING SETS": TokenType.GROUPING_SETS,
     546        "HAVING": TokenType.HAVING,
     547        "IF": TokenType.IF,
     548        "ILIKE": TokenType.ILIKE,
     549        "IN": TokenType.IN,
     550        "INDEX": TokenType.INDEX,
     551        "INET": TokenType.INET,
     552        "INNER": TokenType.INNER,
     553        "INSERT": TokenType.INSERT,
     554        "INTERVAL": TokenType.INTERVAL,
     555        "INTERSECT": TokenType.INTERSECT,
     556        "INTO": TokenType.INTO,
     557        "IS": TokenType.IS,
     558        "ISNULL": TokenType.ISNULL,
     559        "JOIN": TokenType.JOIN,
     560        "KEEP": TokenType.KEEP,
     561        "LATERAL": TokenType.LATERAL,
     562        "LEFT": TokenType.LEFT,
     563        "LIKE": TokenType.LIKE,
     564        "LIMIT": TokenType.LIMIT,
     565        "LOAD": TokenType.LOAD,
     566        "LOCK": TokenType.LOCK,
     567        "MERGE": TokenType.MERGE,
     568        "NATURAL": TokenType.NATURAL,
     569        "NEXT": TokenType.NEXT,
     570        "NEXT VALUE FOR": TokenType.NEXT_VALUE_FOR,
     571        "NOT": TokenType.NOT,
     572        "NOTNULL": TokenType.NOTNULL,
     573        "NULL": TokenType.NULL,
     574        "OBJECT": TokenType.OBJECT,
     575        "OFFSET": TokenType.OFFSET,
     576        "ON": TokenType.ON,
     577        "OR": TokenType.OR,
     578        "ORDER BY": TokenType.ORDER_BY,
     579        "ORDINALITY": TokenType.ORDINALITY,
     580        "OUTER": TokenType.OUTER,
     581        "OVER": TokenType.OVER,
     582        "OVERLAPS": TokenType.OVERLAPS,
     583        "OVERWRITE": TokenType.OVERWRITE,
     584        "PARTITION": TokenType.PARTITION,
     585        "PARTITION BY": TokenType.PARTITION_BY,
     586        "PARTITIONED BY": TokenType.PARTITION_BY,
     587        "PARTITIONED_BY": TokenType.PARTITION_BY,
     588        "PERCENT": TokenType.PERCENT,
     589        "PIVOT": TokenType.PIVOT,
     590        "PRAGMA": TokenType.PRAGMA,
     591        "PRIMARY KEY": TokenType.PRIMARY_KEY,
     592        "PROCEDURE": TokenType.PROCEDURE,
     593        "QUALIFY": TokenType.QUALIFY,
     594        "RANGE": TokenType.RANGE,
     595        "RECURSIVE": TokenType.RECURSIVE,
     596        "REGEXP": TokenType.RLIKE,
     597        "REPLACE": TokenType.REPLACE,
     598        "REFERENCES": TokenType.REFERENCES,
     599        "RIGHT": TokenType.RIGHT,
     600        "RLIKE": TokenType.RLIKE,
     601        "ROLLBACK": TokenType.ROLLBACK,
     602        "ROLLUP": TokenType.ROLLUP,
     603        "ROW": TokenType.ROW,
     604        "ROWS": TokenType.ROWS,
     605        "SCHEMA": TokenType.SCHEMA,
     606        "SELECT": TokenType.SELECT,
     607        "SEMI": TokenType.SEMI,
     608        "SET": TokenType.SET,
     609        "SETTINGS": TokenType.SETTINGS,
     610        "SHOW": TokenType.SHOW,
     611        "SIMILAR TO": TokenType.SIMILAR_TO,
     612        "SOME": TokenType.SOME,
     613        "TABLE": TokenType.TABLE,
     614        "TABLESAMPLE": TokenType.TABLE_SAMPLE,
     615        "TEMP": TokenType.TEMPORARY,
     616        "TEMPORARY": TokenType.TEMPORARY,
     617        "THEN": TokenType.THEN,
     618        "TRUE": TokenType.TRUE,
     619        "UNION": TokenType.UNION,
     620        "UNNEST": TokenType.UNNEST,
     621        "UNPIVOT": TokenType.UNPIVOT,
     622        "UPDATE": TokenType.UPDATE,
     623        "USE": TokenType.USE,
     624        "USING": TokenType.USING,
     625        "UUID": TokenType.UUID,
     626        "VALUES": TokenType.VALUES,
     627        "VIEW": TokenType.VIEW,
     628        "VOLATILE": TokenType.VOLATILE,
     629        "WHEN": TokenType.WHEN,
     630        "WHERE": TokenType.WHERE,
     631        "WINDOW": TokenType.WINDOW,
     632        "WITH": TokenType.WITH,
     633        "APPLY": TokenType.APPLY,
     634        "ARRAY": TokenType.ARRAY,
     635        "BIT": TokenType.BIT,
     636        "BOOL": TokenType.BOOLEAN,
     637        "BOOLEAN": TokenType.BOOLEAN,
     638        "BYTE": TokenType.TINYINT,
     639        "TINYINT": TokenType.TINYINT,
     640        "SHORT": TokenType.SMALLINT,
     641        "SMALLINT": TokenType.SMALLINT,
     642        "INT2": TokenType.SMALLINT,
     643        "INTEGER": TokenType.INT,
     644        "INT": TokenType.INT,
     645        "INT4": TokenType.INT,
     646        "LONG": TokenType.BIGINT,
     647        "BIGINT": TokenType.BIGINT,
     648        "INT8": TokenType.BIGINT,
     649        "DEC": TokenType.DECIMAL,
     650        "DECIMAL": TokenType.DECIMAL,
     651        "BIGDECIMAL": TokenType.BIGDECIMAL,
     652        "BIGNUMERIC": TokenType.BIGDECIMAL,
     653        "MAP": TokenType.MAP,
     654        "NULLABLE": TokenType.NULLABLE,
     655        "NUMBER": TokenType.DECIMAL,
     656        "NUMERIC": TokenType.DECIMAL,
     657        "FIXED": TokenType.DECIMAL,
     658        "REAL": TokenType.FLOAT,
     659        "FLOAT": TokenType.FLOAT,
     660        "FLOAT4": TokenType.FLOAT,
     661        "FLOAT8": TokenType.DOUBLE,
     662        "DOUBLE": TokenType.DOUBLE,
     663        "DOUBLE PRECISION": TokenType.DOUBLE,
     664        "JSON": TokenType.JSON,
     665        "CHAR": TokenType.CHAR,
     666        "CHARACTER": TokenType.CHAR,
     667        "NCHAR": TokenType.NCHAR,
     668        "VARCHAR": TokenType.VARCHAR,
     669        "VARCHAR2": TokenType.VARCHAR,
     670        "NVARCHAR": TokenType.NVARCHAR,
     671        "NVARCHAR2": TokenType.NVARCHAR,
     672        "STR": TokenType.TEXT,
     673        "STRING": TokenType.TEXT,
     674        "TEXT": TokenType.TEXT,
     675        "CLOB": TokenType.TEXT,
     676        "LONGVARCHAR": TokenType.TEXT,
     677        "BINARY": TokenType.BINARY,
     678        "BLOB": TokenType.VARBINARY,
     679        "BYTEA": TokenType.VARBINARY,
     680        "VARBINARY": TokenType.VARBINARY,
     681        "TIME": TokenType.TIME,
     682        "TIMESTAMP": TokenType.TIMESTAMP,
     683        "TIMESTAMPTZ": TokenType.TIMESTAMPTZ,
     684        "TIMESTAMPLTZ": TokenType.TIMESTAMPLTZ,
     685        "DATE": TokenType.DATE,
     686        "DATETIME": TokenType.DATETIME,
     687        "INT4RANGE": TokenType.INT4RANGE,
     688        "INT4MULTIRANGE": TokenType.INT4MULTIRANGE,
     689        "INT8RANGE": TokenType.INT8RANGE,
     690        "INT8MULTIRANGE": TokenType.INT8MULTIRANGE,
     691        "NUMRANGE": TokenType.NUMRANGE,
     692        "NUMMULTIRANGE": TokenType.NUMMULTIRANGE,
     693        "TSRANGE": TokenType.TSRANGE,
     694        "TSMULTIRANGE": TokenType.TSMULTIRANGE,
     695        "TSTZRANGE": TokenType.TSTZRANGE,
     696        "TSTZMULTIRANGE": TokenType.TSTZMULTIRANGE,
     697        "DATERANGE": TokenType.DATERANGE,
     698        "DATEMULTIRANGE": TokenType.DATEMULTIRANGE,
     699        "UNIQUE": TokenType.UNIQUE,
     700        "STRUCT": TokenType.STRUCT,
     701        "VARIANT": TokenType.VARIANT,
     702        "ALTER": TokenType.ALTER,
     703        "ANALYZE": TokenType.COMMAND,
     704        "CALL": TokenType.COMMAND,
     705        "COMMENT": TokenType.COMMENT,
     706        "COPY": TokenType.COMMAND,
     707        "EXPLAIN": TokenType.COMMAND,
     708        "GRANT": TokenType.COMMAND,
     709        "OPTIMIZE": TokenType.COMMAND,
     710        "PREPARE": TokenType.COMMAND,
     711        "TRUNCATE": TokenType.COMMAND,
     712        "VACUUM": TokenType.COMMAND,
     713    }
     714
     715    WHITE_SPACE: t.Dict[t.Optional[str], TokenType] = {
     716        " ": TokenType.SPACE,
     717        "\t": TokenType.SPACE,
     718        "\n": TokenType.BREAK,
     719        "\r": TokenType.BREAK,
     720        "\r\n": TokenType.BREAK,
     721    }
     722
     723    COMMANDS = {
     724        TokenType.COMMAND,
     725        TokenType.EXECUTE,
     726        TokenType.FETCH,
     727        TokenType.SHOW,
     728    }
     729
     730    COMMAND_PREFIX_TOKENS = {TokenType.SEMICOLON, TokenType.BEGIN}
     731
     732    # handle numeric literals like in hive (3L = BIGINT)
     733    NUMERIC_LITERALS: t.Dict[str, str] = {}
     734    ENCODE: t.Optional[str] = None
     735
     736    COMMENTS = ["--", ("/*", "*/"), ("{#", "#}")]
     737    KEYWORD_TRIE: t.Dict = {}  # autofilled
     738
     739    __slots__ = (
     740        "sql",
     741        "size",
     742        "tokens",
     743        "_start",
     744        "_current",
     745        "_line",
     746        "_col",
     747        "_comments",
     748        "_char",
     749        "_end",
     750        "_peek",
     751        "_prev_token_line",
     752        "identifiers_can_start_with_digit",
     753    )
     754
     755    def __init__(self) -> None:
     756        self.reset()
     757
     758    def reset(self) -> None:
     759        self.sql = ""
     760        self.size = 0
     761        self.tokens: t.List[Token] = []
     762        self._start = 0
     763        self._current = 0
     764        self._line = 1
     765        self._col = 0
     766        self._comments: t.List[str] = []
     767
     768        self._char = ""
     769        self._end = False
     770        self._peek = ""
     771        self._prev_token_line = -1
     772
     773    def tokenize(self, sql: str) -> t.List[Token]:
     774        """Returns a list of tokens corresponding to the SQL string `sql`."""
     775        self.reset()
     776        self.sql = sql
     777        self.size = len(sql)
     778
     779        try:
     780            self._scan()
     781        except Exception as e:
     782            start = max(self._current - 50, 0)
     783            end = min(self._current + 50, self.size - 1)
     784            context = self.sql[start:end]
     785            raise ValueError(f"Error tokenizing '{context}'") from e
     786
     787        return self.tokens
     788
     789    def _scan(self, until: t.Optional[t.Callable] = None) -> None:
     790        while self.size and not self._end:
     791            self._start = self._current
     792            self._advance()
     793
     794            if self._char is None:
     795                break
     796
     797            if self._char not in self.WHITE_SPACE:
     798                if self._char.isdigit():
     799                    self._scan_number()
     800                elif self._char in self._IDENTIFIERS:
     801                    self._scan_identifier(self._IDENTIFIERS[self._char])
     802                else:
     803                    self._scan_keywords()
     804
     805            if until and until():
     806                break
     807
     808        if self.tokens and self._comments:
     809            self.tokens[-1].comments.extend(self._comments)
     810
     811    def _chars(self, size: int) -> str:
     812        if size == 1:
     813            return self._char
     814
     815        start = self._current - 1
     816        end = start + size
     817
     818        return self.sql[start:end] if end <= self.size else ""
     819
     820    def _advance(self, i: int = 1, alnum: bool = False) -> None:
     821        if self.WHITE_SPACE.get(self._char) is TokenType.BREAK:
     822            self._col = 1
     823            self._line += 1
     824        else:
     825            self._col += i
     826
     827        self._current += i
     828        self._end = self._current >= self.size
     829        self._char = self.sql[self._current - 1]
     830        self._peek = "" if self._end else self.sql[self._current]
     831
     832        if alnum and self._char.isalnum():
     833            # Here we use local variables instead of attributes for better performance
     834            _col = self._col
     835            _current = self._current
     836            _end = self._end
     837            _peek = self._peek
     838
     839            while _peek.isalnum():
     840                _col += 1
     841                _current += 1
     842                _end = _current >= self.size
     843                _peek = "" if _end else self.sql[_current]
     844
     845            self._col = _col
     846            self._current = _current
     847            self._end = _end
     848            self._peek = _peek
     849            self._char = self.sql[_current - 1]
     850
     851    @property
     852    def _text(self) -> str:
     853        return self.sql[self._start : self._current]
     854
     855    def _add(self, token_type: TokenType, text: t.Optional[str] = None) -> None:
     856        self._prev_token_line = self._line
     857        self.tokens.append(
     858            Token(
     859                token_type,
     860                text=self._text if text is None else text,
     861                line=self._line,
     862                col=self._col,
     863                start=self._start,
     864                end=self._current - 1,
     865                comments=self._comments,
     866            )
     867        )
     868        self._comments = []
     869
     870        # If we have either a semicolon or a begin token before the command's token, we'll parse
     871        # whatever follows the command's token as a string
     872        if (
     873            token_type in self.COMMANDS
     874            and self._peek != ";"
     875            and (len(self.tokens) == 1 or self.tokens[-2].token_type in self.COMMAND_PREFIX_TOKENS)
     876        ):
     877            start = self._current
     878            tokens = len(self.tokens)
     879            self._scan(lambda: self._peek == ";")
     880            self.tokens = self.tokens[:tokens]
     881            text = self.sql[start : self._current].strip()
     882            if text:
     883                self._add(TokenType.STRING, text)
     884
     885    def _scan_keywords(self) -> None:
     886        size = 0
     887        word = None
     888        chars = self._text
     889        char = chars
     890        prev_space = False
     891        skip = False
     892        trie = self.KEYWORD_TRIE
     893        single_token = char in self.SINGLE_TOKENS
     894
     895        while chars:
     896            if skip:
     897                result = 1
     898            else:
     899                result, trie = in_trie(trie, char.upper())
     900
     901            if result == 0:
     902                break
     903            if result == 2:
     904                word = chars
     905
     906            size += 1
     907            end = self._current - 1 + size
     908
     909            if end < self.size:
     910                char = self.sql[end]
     911                single_token = single_token or char in self.SINGLE_TOKENS
     912                is_space = char in self.WHITE_SPACE
     913
     914                if not is_space or not prev_space:
     915                    if is_space:
     916                        char = " "
     917                    chars += char
     918                    prev_space = is_space
     919                    skip = False
     920                else:
     921                    skip = True
     922            else:
     923                char = ""
     924                chars = " "
     925
     926        word = None if not single_token and chars[-1] not in self.WHITE_SPACE else word
     927
     928        if not word:
     929            if self._char in self.SINGLE_TOKENS:
     930                self._add(self.SINGLE_TOKENS[self._char], text=self._char)
     931                return
     932            self._scan_var()
     933            return
     934
     935        if self._scan_string(word):
     936            return
     937        if self._scan_comment(word):
     938            return
     939
     940        self._advance(size - 1)
     941        word = word.upper()
     942        self._add(self.KEYWORDS[word], text=word)
     943
     944    def _scan_comment(self, comment_start: str) -> bool:
     945        if comment_start not in self._COMMENTS:
     946            return False
     947
     948        comment_start_line = self._line
     949        comment_start_size = len(comment_start)
     950        comment_end = self._COMMENTS[comment_start]
     951
     952        if comment_end:
     953            # Skip the comment's start delimiter
     954            self._advance(comment_start_size)
     955
     956            comment_end_size = len(comment_end)
     957            while not self._end and self._chars(comment_end_size) != comment_end:
     958                self._advance(alnum=True)
     959
     960            self._comments.append(self._text[comment_start_size : -comment_end_size + 1])
     961            self._advance(comment_end_size - 1)
     962        else:
     963            while not self._end and not self.WHITE_SPACE.get(self._peek) is TokenType.BREAK:
     964                self._advance(alnum=True)
     965            self._comments.append(self._text[comment_start_size:])
     966
     967        # Leading comment is attached to the succeeding token, whilst trailing comment to the preceding.
     968        # Multiple consecutive comments are preserved by appending them to the current comments list.
     969        if comment_start_line == self._prev_token_line:
     970            self.tokens[-1].comments.extend(self._comments)
     971            self._comments = []
     972            self._prev_token_line = self._line
     973
     974        return True
     975
     976    def _scan_number(self) -> None:
     977        if self._char == "0":
     978            peek = self._peek.upper()
     979            if peek == "B":
     980                return self._scan_bits() if self.BIT_STRINGS else self._add(TokenType.NUMBER)
     981            elif peek == "X":
     982                return self._scan_hex() if self.HEX_STRINGS else self._add(TokenType.NUMBER)
     983
     984        decimal = False
     985        scientific = 0
     986
     987        while True:
     988            if self._peek.isdigit():
     989                self._advance()
     990            elif self._peek == "." and not decimal:
     991                decimal = True
     992                self._advance()
     993            elif self._peek in ("-", "+") and scientific == 1:
     994                scientific += 1
     995                self._advance()
     996            elif self._peek.upper() == "E" and not scientific:
     997                scientific += 1
     998                self._advance()
     999            elif self._peek.isidentifier():
    1000                number_text = self._text
    1001                literal = ""
    1002
    1003                while self._peek.strip() and self._peek not in self.SINGLE_TOKENS:
    1004                    literal += self._peek.upper()
    1005                    self._advance()
    1006
    1007                token_type = self.KEYWORDS.get(self.NUMERIC_LITERALS.get(literal))
    1008
    1009                if token_type:
    1010                    self._add(TokenType.NUMBER, number_text)
    1011                    self._add(TokenType.DCOLON, "::")
    1012                    return self._add(token_type, literal)
    1013                elif self.identifiers_can_start_with_digit:  # type: ignore
    1014                    return self._add(TokenType.VAR)
    1015
    1016                self._add(TokenType.NUMBER, number_text)
    1017                return self._advance(-len(literal))
    1018            else:
    1019                return self._add(TokenType.NUMBER)
    1020
    1021    def _scan_bits(self) -> None:
    1022        self._advance()
    1023        value = self._extract_value()
    1024        try:
    1025            # If `value` can't be converted to a binary, fallback to tokenizing it as an identifier
    1026            int(value, 2)
    1027            self._add(TokenType.BIT_STRING, value[2:])  # Drop the 0b
    1028        except ValueError:
    1029            self._add(TokenType.IDENTIFIER)
    1030
    1031    def _scan_hex(self) -> None:
    1032        self._advance()
    1033        value = self._extract_value()
    1034        try:
    1035            # If `value` can't be converted to a hex, fallback to tokenizing it as an identifier
    1036            int(value, 16)
    1037            self._add(TokenType.HEX_STRING, value[2:])  # Drop the 0x
    1038        except ValueError:
    1039            self._add(TokenType.IDENTIFIER)
    1040
    1041    def _extract_value(self) -> str:
    1042        while True:
    1043            char = self._peek.strip()
    1044            if char and char not in self.SINGLE_TOKENS:
    1045                self._advance(alnum=True)
    1046            else:
    1047                break
    1048
    1049        return self._text
    1050
    1051    def _scan_string(self, start: str) -> bool:
    1052        base = None
    1053        token_type = TokenType.STRING
    1054
    1055        if start in self._QUOTES:
    1056            end = self._QUOTES[start]
    1057        elif start in self._FORMAT_STRINGS:
    1058            end, token_type = self._FORMAT_STRINGS[start]
    1059
    1060            if token_type == TokenType.HEX_STRING:
    1061                base = 16
    1062            elif token_type == TokenType.BIT_STRING:
    1063                base = 2
    1064        else:
    1065            return False
    1066
    1067        self._advance(len(start))
    1068        text = self._extract_string(end)
    1069
    1070        if base:
    1071            try:
    1072                int(text, base)
    1073            except:
    1074                raise RuntimeError(
    1075                    f"Numeric string contains invalid characters from {self._line}:{self._start}"
    1076                )
    1077        else:
    1078            text = text.encode(self.ENCODE).decode(self.ENCODE) if self.ENCODE else text
    1079
    1080        self._add(token_type, text)
    1081        return True
    1082
    1083    def _scan_identifier(self, identifier_end: str) -> None:
    1084        self._advance()
    1085        text = self._extract_string(identifier_end, self._IDENTIFIER_ESCAPES)
    1086        self._add(TokenType.IDENTIFIER, text)
    1087
    1088    def _scan_var(self) -> None:
    1089        while True:
    1090            char = self._peek.strip()
    1091            if char and (char in self.VAR_SINGLE_TOKENS or char not in self.SINGLE_TOKENS):
    1092                self._advance(alnum=True)
    1093            else:
    1094                break
    1095
    1096        self._add(
    1097            TokenType.VAR
    1098            if self.tokens and self.tokens[-1].token_type == TokenType.PARAMETER
    1099            else self.KEYWORDS.get(self._text.upper(), TokenType.VAR)
    1100        )
    1101
    1102    def _extract_string(self, delimiter: str, escapes=None) -> str:
    1103        text = ""
    1104        delim_size = len(delimiter)
    1105        escapes = self._STRING_ESCAPES if escapes is None else escapes
    1106
    1107        while True:
    1108            if self._char in escapes and (self._peek == delimiter or self._peek in escapes):
    1109                if self._peek == delimiter:
    1110                    text += self._peek
    1111                else:
    1112                    text += self._char + self._peek
    1113
    1114                if self._current + 1 < self.size:
    1115                    self._advance(2)
    1116                else:
    1117                    raise RuntimeError(f"Missing {delimiter} from {self._line}:{self._current}")
    1118            else:
    1119                if self._chars(delim_size) == delimiter:
    1120                    if delim_size > 1:
    1121                        self._advance(delim_size - 1)
    1122                    break
    1123
    1124                if self._end:
    1125                    raise RuntimeError(f"Missing {delimiter} from {self._line}:{self._start}")
    1126
    1127                current = self._current - 1
    1128                self._advance(alnum=True)
    1129                text += self.sql[current : self._current - 1]
    1130
    1131        return text

def reset(self) -> None: View Source

    758    def reset(self) -> None:
    759        self.sql = ""
    760        self.size = 0
    761        self.tokens: t.List[Token] = []
    762        self._start = 0
    763        self._current = 0
    764        self._line = 1
    765        self._col = 0
    766        self._comments: t.List[str] = []
    767
    768        self._char = ""
    769        self._end = False
    770        self._peek = ""
    771        self._prev_token_line = -1

def tokenize(self, sql: str) -> List[sqlglot.tokens.Token]: View Source

    773    def tokenize(self, sql: str) -> t.List[Token]:
    774        """Returns a list of tokens corresponding to the SQL string `sql`."""
    775        self.reset()
    776        self.sql = sql
    777        self.size = len(sql)
    778
    779        try:
    780            self._scan()
    781        except Exception as e:
    782            start = max(self._current - 50, 0)
    783            end = min(self._current + 50, self.size - 1)
    784            context = self.sql[start:end]
    785            raise ValueError(f"Error tokenizing '{context}'") from e
    786
    787        return self.tokens

Returns a list of tokens corresponding to the SQL string `sql`.