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

##### Inherited Members

enum.Enum
  name
  value

class Token: View Source

    316class Token:
    317    __slots__ = ("token_type", "text", "line", "col", "comments")
    318
    319    @classmethod
    320    def number(cls, number: int) -> Token:
    321        """Returns a NUMBER token with `number` as its text."""
    322        return cls(TokenType.NUMBER, str(number))
    323
    324    @classmethod
    325    def string(cls, string: str) -> Token:
    326        """Returns a STRING token with `string` as its text."""
    327        return cls(TokenType.STRING, string)
    328
    329    @classmethod
    330    def identifier(cls, identifier: str) -> Token:
    331        """Returns an IDENTIFIER token with `identifier` as its text."""
    332        return cls(TokenType.IDENTIFIER, identifier)
    333
    334    @classmethod
    335    def var(cls, var: str) -> Token:
    336        """Returns an VAR token with `var` as its text."""
    337        return cls(TokenType.VAR, var)
    338
    339    def __init__(
    340        self,
    341        token_type: TokenType,
    342        text: str,
    343        line: int = 1,
    344        col: int = 1,
    345        comments: t.List[str] = [],
    346    ) -> None:
    347        self.token_type = token_type
    348        self.text = text
    349        self.line = line
    350        self.col = max(col - len(text), 1)
    351        self.comments = comments
    352
    353    def __repr__(self) -> str:
    354        attributes = ", ".join(f"{k}: {getattr(self, k)}" for k in self.__slots__)
    355        return f"<Token {attributes}>"

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

    339    def __init__(
    340        self,
    341        token_type: TokenType,
    342        text: str,
    343        line: int = 1,
    344        col: int = 1,
    345        comments: t.List[str] = [],
    346    ) -> None:
    347        self.token_type = token_type
    348        self.text = text
    349        self.line = line
    350        self.col = max(col - len(text), 1)
    351        self.comments = comments

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

    319    @classmethod
    320    def number(cls, number: int) -> Token:
    321        """Returns a NUMBER token with `number` as its text."""
    322        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

    324    @classmethod
    325    def string(cls, string: str) -> Token:
    326        """Returns a STRING token with `string` as its text."""
    327        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

    329    @classmethod
    330    def identifier(cls, identifier: str) -> Token:
    331        """Returns an IDENTIFIER token with `identifier` as its text."""
    332        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

    334    @classmethod
    335    def var(cls, var: str) -> Token:
    336        """Returns an VAR token with `var` as its text."""
    337        return cls(TokenType.VAR, var)

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

class Tokenizer: View Source

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

def reset(self) -> None: View Source

    764    def reset(self) -> None:
    765        self.sql = ""
    766        self.size = 0
    767        self.tokens: t.List[Token] = []
    768        self._start = 0
    769        self._current = 0
    770        self._line = 1
    771        self._col = 1
    772        self._comments: t.List[str] = []
    773
    774        self._char = None
    775        self._end = None
    776        self._peek = None
    777        self._prev_token_line = -1
    778        self._prev_token_comments: t.List[str] = []
    779        self._prev_token_type = None

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

    781    def tokenize(self, sql: str) -> t.List[Token]:
    782        """Returns a list of tokens corresponding to the SQL string `sql`."""
    783        self.reset()
    784        self.sql = sql
    785        self.size = len(sql)
    786        self._scan()
    787        return self.tokens

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