Edit on GitHub

sqlglot.dialects.tsql

   1from __future__ import annotations
   2
   3import datetime
   4import re
   5import typing as t
   6from functools import partial
   7
   8from sqlglot import exp, generator, parser, tokens, transforms
   9from sqlglot.dialects.dialect import (
  10    Dialect,
  11    NormalizationStrategy,
  12    any_value_to_max_sql,
  13    date_delta_sql,
  14    datestrtodate_sql,
  15    generatedasidentitycolumnconstraint_sql,
  16    max_or_greatest,
  17    min_or_least,
  18    build_date_delta,
  19    rename_func,
  20    trim_sql,
  21)
  22from sqlglot.helper import seq_get
  23from sqlglot.time import format_time
  24from sqlglot.tokens import TokenType
  25
  26if t.TYPE_CHECKING:
  27    from sqlglot._typing import E
  28
  29FULL_FORMAT_TIME_MAPPING = {
  30    "weekday": "%A",
  31    "dw": "%A",
  32    "w": "%A",
  33    "month": "%B",
  34    "mm": "%B",
  35    "m": "%B",
  36}
  37
  38DATE_DELTA_INTERVAL = {
  39    "year": "year",
  40    "yyyy": "year",
  41    "yy": "year",
  42    "quarter": "quarter",
  43    "qq": "quarter",
  44    "q": "quarter",
  45    "month": "month",
  46    "mm": "month",
  47    "m": "month",
  48    "week": "week",
  49    "ww": "week",
  50    "wk": "week",
  51    "day": "day",
  52    "dd": "day",
  53    "d": "day",
  54}
  55
  56
  57DATE_FMT_RE = re.compile("([dD]{1,2})|([mM]{1,2})|([yY]{1,4})|([hH]{1,2})|([sS]{1,2})")
  58
  59# N = Numeric, C=Currency
  60TRANSPILE_SAFE_NUMBER_FMT = {"N", "C"}
  61
  62DEFAULT_START_DATE = datetime.date(1900, 1, 1)
  63
  64BIT_TYPES = {exp.EQ, exp.NEQ, exp.Is, exp.In, exp.Select, exp.Alias}
  65
  66# Unsupported options:
  67# - OPTIMIZE FOR ( @variable_name { UNKNOWN | = <literal_constant> } [ , ...n ] )
  68# - TABLE HINT
  69OPTIONS: parser.OPTIONS_TYPE = {
  70    **dict.fromkeys(
  71        (
  72            "DISABLE_OPTIMIZED_PLAN_FORCING",
  73            "FAST",
  74            "IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX",
  75            "LABEL",
  76            "MAXDOP",
  77            "MAXRECURSION",
  78            "MAX_GRANT_PERCENT",
  79            "MIN_GRANT_PERCENT",
  80            "NO_PERFORMANCE_SPOOL",
  81            "QUERYTRACEON",
  82            "RECOMPILE",
  83        ),
  84        tuple(),
  85    ),
  86    "CONCAT": ("UNION",),
  87    "DISABLE": ("EXTERNALPUSHDOWN", "SCALEOUTEXECUTION"),
  88    "EXPAND": ("VIEWS",),
  89    "FORCE": ("EXTERNALPUSHDOWN", "ORDER", "SCALEOUTEXECUTION"),
  90    "HASH": ("GROUP", "JOIN", "UNION"),
  91    "KEEP": ("PLAN",),
  92    "KEEPFIXED": ("PLAN",),
  93    "LOOP": ("JOIN",),
  94    "MERGE": ("JOIN", "UNION"),
  95    "OPTIMIZE": (("FOR", "UNKNOWN"),),
  96    "ORDER": ("GROUP",),
  97    "PARAMETERIZATION": ("FORCED", "SIMPLE"),
  98    "ROBUST": ("PLAN",),
  99    "USE": ("PLAN",),
 100}
 101
 102OPTIONS_THAT_REQUIRE_EQUAL = ("MAX_GRANT_PERCENT", "MIN_GRANT_PERCENT", "LABEL")
 103
 104
 105def _build_formatted_time(
 106    exp_class: t.Type[E], full_format_mapping: t.Optional[bool] = None
 107) -> t.Callable[[t.List], E]:
 108    def _builder(args: t.List) -> E:
 109        assert len(args) == 2
 110
 111        return exp_class(
 112            this=exp.cast(args[1], exp.DataType.Type.DATETIME),
 113            format=exp.Literal.string(
 114                format_time(
 115                    args[0].name.lower(),
 116                    (
 117                        {**TSQL.TIME_MAPPING, **FULL_FORMAT_TIME_MAPPING}
 118                        if full_format_mapping
 119                        else TSQL.TIME_MAPPING
 120                    ),
 121                )
 122            ),
 123        )
 124
 125    return _builder
 126
 127
 128def _build_format(args: t.List) -> exp.NumberToStr | exp.TimeToStr:
 129    this = seq_get(args, 0)
 130    fmt = seq_get(args, 1)
 131    culture = seq_get(args, 2)
 132
 133    number_fmt = fmt and (fmt.name in TRANSPILE_SAFE_NUMBER_FMT or not DATE_FMT_RE.search(fmt.name))
 134
 135    if number_fmt:
 136        return exp.NumberToStr(this=this, format=fmt, culture=culture)
 137
 138    if fmt:
 139        fmt = exp.Literal.string(
 140            format_time(fmt.name, TSQL.FORMAT_TIME_MAPPING)
 141            if len(fmt.name) == 1
 142            else format_time(fmt.name, TSQL.TIME_MAPPING)
 143        )
 144
 145    return exp.TimeToStr(this=this, format=fmt, culture=culture)
 146
 147
 148def _build_eomonth(args: t.List) -> exp.LastDay:
 149    date = exp.TsOrDsToDate(this=seq_get(args, 0))
 150    month_lag = seq_get(args, 1)
 151
 152    if month_lag is None:
 153        this: exp.Expression = date
 154    else:
 155        unit = DATE_DELTA_INTERVAL.get("month")
 156        this = exp.DateAdd(this=date, expression=month_lag, unit=unit and exp.var(unit))
 157
 158    return exp.LastDay(this=this)
 159
 160
 161def _build_hashbytes(args: t.List) -> exp.Expression:
 162    kind, data = args
 163    kind = kind.name.upper() if kind.is_string else ""
 164
 165    if kind == "MD5":
 166        args.pop(0)
 167        return exp.MD5(this=data)
 168    if kind in ("SHA", "SHA1"):
 169        args.pop(0)
 170        return exp.SHA(this=data)
 171    if kind == "SHA2_256":
 172        return exp.SHA2(this=data, length=exp.Literal.number(256))
 173    if kind == "SHA2_512":
 174        return exp.SHA2(this=data, length=exp.Literal.number(512))
 175
 176    return exp.func("HASHBYTES", *args)
 177
 178
 179DATEPART_ONLY_FORMATS = {"DW", "HOUR", "QUARTER"}
 180
 181
 182def _format_sql(self: TSQL.Generator, expression: exp.NumberToStr | exp.TimeToStr) -> str:
 183    fmt = expression.args["format"]
 184
 185    if not isinstance(expression, exp.NumberToStr):
 186        if fmt.is_string:
 187            mapped_fmt = format_time(fmt.name, TSQL.INVERSE_TIME_MAPPING)
 188
 189            name = (mapped_fmt or "").upper()
 190            if name in DATEPART_ONLY_FORMATS:
 191                return self.func("DATEPART", name, expression.this)
 192
 193            fmt_sql = self.sql(exp.Literal.string(mapped_fmt))
 194        else:
 195            fmt_sql = self.format_time(expression) or self.sql(fmt)
 196    else:
 197        fmt_sql = self.sql(fmt)
 198
 199    return self.func("FORMAT", expression.this, fmt_sql, expression.args.get("culture"))
 200
 201
 202def _string_agg_sql(self: TSQL.Generator, expression: exp.GroupConcat) -> str:
 203    this = expression.this
 204    distinct = expression.find(exp.Distinct)
 205    if distinct:
 206        # exp.Distinct can appear below an exp.Order or an exp.GroupConcat expression
 207        self.unsupported("T-SQL STRING_AGG doesn't support DISTINCT.")
 208        this = distinct.pop().expressions[0]
 209
 210    order = ""
 211    if isinstance(expression.this, exp.Order):
 212        if expression.this.this:
 213            this = expression.this.this.pop()
 214        # Order has a leading space
 215        order = f" WITHIN GROUP ({self.sql(expression.this)[1:]})"
 216
 217    separator = expression.args.get("separator") or exp.Literal.string(",")
 218    return f"STRING_AGG({self.format_args(this, separator)}){order}"
 219
 220
 221def _build_date_delta(
 222    exp_class: t.Type[E], unit_mapping: t.Optional[t.Dict[str, str]] = None
 223) -> t.Callable[[t.List], E]:
 224    def _builder(args: t.List) -> E:
 225        unit = seq_get(args, 0)
 226        if unit and unit_mapping:
 227            unit = exp.var(unit_mapping.get(unit.name.lower(), unit.name))
 228
 229        start_date = seq_get(args, 1)
 230        if start_date and start_date.is_number:
 231            # Numeric types are valid DATETIME values
 232            if start_date.is_int:
 233                adds = DEFAULT_START_DATE + datetime.timedelta(days=int(start_date.this))
 234                start_date = exp.Literal.string(adds.strftime("%F"))
 235            else:
 236                # We currently don't handle float values, i.e. they're not converted to equivalent DATETIMEs.
 237                # This is not a problem when generating T-SQL code, it is when transpiling to other dialects.
 238                return exp_class(this=seq_get(args, 2), expression=start_date, unit=unit)
 239
 240        return exp_class(
 241            this=exp.TimeStrToTime(this=seq_get(args, 2)),
 242            expression=exp.TimeStrToTime(this=start_date),
 243            unit=unit,
 244        )
 245
 246    return _builder
 247
 248
 249def qualify_derived_table_outputs(expression: exp.Expression) -> exp.Expression:
 250    """Ensures all (unnamed) output columns are aliased for CTEs and Subqueries."""
 251    alias = expression.args.get("alias")
 252
 253    if (
 254        isinstance(expression, (exp.CTE, exp.Subquery))
 255        and isinstance(alias, exp.TableAlias)
 256        and not alias.columns
 257    ):
 258        from sqlglot.optimizer.qualify_columns import qualify_outputs
 259
 260        # We keep track of the unaliased column projection indexes instead of the expressions
 261        # themselves, because the latter are going to be replaced by new nodes when the aliases
 262        # are added and hence we won't be able to reach these newly added Alias parents
 263        query = expression.this
 264        unaliased_column_indexes = (
 265            i for i, c in enumerate(query.selects) if isinstance(c, exp.Column) and not c.alias
 266        )
 267
 268        qualify_outputs(query)
 269
 270        # Preserve the quoting information of columns for newly added Alias nodes
 271        query_selects = query.selects
 272        for select_index in unaliased_column_indexes:
 273            alias = query_selects[select_index]
 274            column = alias.this
 275            if isinstance(column.this, exp.Identifier):
 276                alias.args["alias"].set("quoted", column.this.quoted)
 277
 278    return expression
 279
 280
 281# https://learn.microsoft.com/en-us/sql/t-sql/functions/datetimefromparts-transact-sql?view=sql-server-ver16#syntax
 282def _build_datetimefromparts(args: t.List) -> exp.TimestampFromParts:
 283    return exp.TimestampFromParts(
 284        year=seq_get(args, 0),
 285        month=seq_get(args, 1),
 286        day=seq_get(args, 2),
 287        hour=seq_get(args, 3),
 288        min=seq_get(args, 4),
 289        sec=seq_get(args, 5),
 290        milli=seq_get(args, 6),
 291    )
 292
 293
 294# https://learn.microsoft.com/en-us/sql/t-sql/functions/timefromparts-transact-sql?view=sql-server-ver16#syntax
 295def _build_timefromparts(args: t.List) -> exp.TimeFromParts:
 296    return exp.TimeFromParts(
 297        hour=seq_get(args, 0),
 298        min=seq_get(args, 1),
 299        sec=seq_get(args, 2),
 300        fractions=seq_get(args, 3),
 301        precision=seq_get(args, 4),
 302    )
 303
 304
 305def _build_with_arg_as_text(
 306    klass: t.Type[exp.Expression],
 307) -> t.Callable[[t.List[exp.Expression]], exp.Expression]:
 308    def _parse(args: t.List[exp.Expression]) -> exp.Expression:
 309        this = seq_get(args, 0)
 310
 311        if this and not this.is_string:
 312            this = exp.cast(this, exp.DataType.Type.TEXT)
 313
 314        expression = seq_get(args, 1)
 315        kwargs = {"this": this}
 316
 317        if expression:
 318            kwargs["expression"] = expression
 319
 320        return klass(**kwargs)
 321
 322    return _parse
 323
 324
 325def _json_extract_sql(
 326    self: TSQL.Generator, expression: exp.JSONExtract | exp.JSONExtractScalar
 327) -> str:
 328    json_query = self.func("JSON_QUERY", expression.this, expression.expression)
 329    json_value = self.func("JSON_VALUE", expression.this, expression.expression)
 330    return self.func("ISNULL", json_query, json_value)
 331
 332
 333class TSQL(Dialect):
 334    NORMALIZATION_STRATEGY = NormalizationStrategy.CASE_INSENSITIVE
 335    TIME_FORMAT = "'yyyy-mm-dd hh:mm:ss'"
 336    SUPPORTS_SEMI_ANTI_JOIN = False
 337    LOG_BASE_FIRST = False
 338    TYPED_DIVISION = True
 339    CONCAT_COALESCE = True
 340
 341    TIME_MAPPING = {
 342        "year": "%Y",
 343        "dayofyear": "%j",
 344        "day": "%d",
 345        "dy": "%d",
 346        "y": "%Y",
 347        "week": "%W",
 348        "ww": "%W",
 349        "wk": "%W",
 350        "hour": "%h",
 351        "hh": "%I",
 352        "minute": "%M",
 353        "mi": "%M",
 354        "n": "%M",
 355        "second": "%S",
 356        "ss": "%S",
 357        "s": "%-S",
 358        "millisecond": "%f",
 359        "ms": "%f",
 360        "weekday": "%W",
 361        "dw": "%W",
 362        "month": "%m",
 363        "mm": "%M",
 364        "m": "%-M",
 365        "Y": "%Y",
 366        "YYYY": "%Y",
 367        "YY": "%y",
 368        "MMMM": "%B",
 369        "MMM": "%b",
 370        "MM": "%m",
 371        "M": "%-m",
 372        "dddd": "%A",
 373        "dd": "%d",
 374        "d": "%-d",
 375        "HH": "%H",
 376        "H": "%-H",
 377        "h": "%-I",
 378        "S": "%f",
 379        "yyyy": "%Y",
 380        "yy": "%y",
 381    }
 382
 383    CONVERT_FORMAT_MAPPING = {
 384        "0": "%b %d %Y %-I:%M%p",
 385        "1": "%m/%d/%y",
 386        "2": "%y.%m.%d",
 387        "3": "%d/%m/%y",
 388        "4": "%d.%m.%y",
 389        "5": "%d-%m-%y",
 390        "6": "%d %b %y",
 391        "7": "%b %d, %y",
 392        "8": "%H:%M:%S",
 393        "9": "%b %d %Y %-I:%M:%S:%f%p",
 394        "10": "mm-dd-yy",
 395        "11": "yy/mm/dd",
 396        "12": "yymmdd",
 397        "13": "%d %b %Y %H:%M:ss:%f",
 398        "14": "%H:%M:%S:%f",
 399        "20": "%Y-%m-%d %H:%M:%S",
 400        "21": "%Y-%m-%d %H:%M:%S.%f",
 401        "22": "%m/%d/%y %-I:%M:%S %p",
 402        "23": "%Y-%m-%d",
 403        "24": "%H:%M:%S",
 404        "25": "%Y-%m-%d %H:%M:%S.%f",
 405        "100": "%b %d %Y %-I:%M%p",
 406        "101": "%m/%d/%Y",
 407        "102": "%Y.%m.%d",
 408        "103": "%d/%m/%Y",
 409        "104": "%d.%m.%Y",
 410        "105": "%d-%m-%Y",
 411        "106": "%d %b %Y",
 412        "107": "%b %d, %Y",
 413        "108": "%H:%M:%S",
 414        "109": "%b %d %Y %-I:%M:%S:%f%p",
 415        "110": "%m-%d-%Y",
 416        "111": "%Y/%m/%d",
 417        "112": "%Y%m%d",
 418        "113": "%d %b %Y %H:%M:%S:%f",
 419        "114": "%H:%M:%S:%f",
 420        "120": "%Y-%m-%d %H:%M:%S",
 421        "121": "%Y-%m-%d %H:%M:%S.%f",
 422    }
 423
 424    FORMAT_TIME_MAPPING = {
 425        "y": "%B %Y",
 426        "d": "%m/%d/%Y",
 427        "H": "%-H",
 428        "h": "%-I",
 429        "s": "%Y-%m-%d %H:%M:%S",
 430        "D": "%A,%B,%Y",
 431        "f": "%A,%B,%Y %-I:%M %p",
 432        "F": "%A,%B,%Y %-I:%M:%S %p",
 433        "g": "%m/%d/%Y %-I:%M %p",
 434        "G": "%m/%d/%Y %-I:%M:%S %p",
 435        "M": "%B %-d",
 436        "m": "%B %-d",
 437        "O": "%Y-%m-%dT%H:%M:%S",
 438        "u": "%Y-%M-%D %H:%M:%S%z",
 439        "U": "%A, %B %D, %Y %H:%M:%S%z",
 440        "T": "%-I:%M:%S %p",
 441        "t": "%-I:%M",
 442        "Y": "%a %Y",
 443    }
 444
 445    class Tokenizer(tokens.Tokenizer):
 446        IDENTIFIERS = [("[", "]"), '"']
 447        QUOTES = ["'", '"']
 448        HEX_STRINGS = [("0x", ""), ("0X", "")]
 449        VAR_SINGLE_TOKENS = {"@", "$", "#"}
 450
 451        KEYWORDS = {
 452            **tokens.Tokenizer.KEYWORDS,
 453            "CLUSTERED INDEX": TokenType.INDEX,
 454            "DATETIME2": TokenType.DATETIME,
 455            "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
 456            "DECLARE": TokenType.DECLARE,
 457            "EXEC": TokenType.COMMAND,
 458            "FOR SYSTEM_TIME": TokenType.TIMESTAMP_SNAPSHOT,
 459            "IMAGE": TokenType.IMAGE,
 460            "MONEY": TokenType.MONEY,
 461            "NONCLUSTERED INDEX": TokenType.INDEX,
 462            "NTEXT": TokenType.TEXT,
 463            "OPTION": TokenType.OPTION,
 464            "OUTPUT": TokenType.RETURNING,
 465            "PRINT": TokenType.COMMAND,
 466            "PROC": TokenType.PROCEDURE,
 467            "REAL": TokenType.FLOAT,
 468            "ROWVERSION": TokenType.ROWVERSION,
 469            "SMALLDATETIME": TokenType.DATETIME,
 470            "SMALLMONEY": TokenType.SMALLMONEY,
 471            "SQL_VARIANT": TokenType.VARIANT,
 472            "SYSTEM_USER": TokenType.CURRENT_USER,
 473            "TOP": TokenType.TOP,
 474            "TIMESTAMP": TokenType.ROWVERSION,
 475            "TINYINT": TokenType.UTINYINT,
 476            "UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
 477            "UPDATE STATISTICS": TokenType.COMMAND,
 478            "XML": TokenType.XML,
 479        }
 480
 481        COMMANDS = {*tokens.Tokenizer.COMMANDS, TokenType.END}
 482
 483    class Parser(parser.Parser):
 484        SET_REQUIRES_ASSIGNMENT_DELIMITER = False
 485        LOG_DEFAULTS_TO_LN = True
 486        ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False
 487        STRING_ALIASES = True
 488        NO_PAREN_IF_COMMANDS = False
 489
 490        QUERY_MODIFIER_PARSERS = {
 491            **parser.Parser.QUERY_MODIFIER_PARSERS,
 492            TokenType.OPTION: lambda self: ("options", self._parse_options()),
 493        }
 494
 495        FUNCTIONS = {
 496            **parser.Parser.FUNCTIONS,
 497            "CHARINDEX": lambda args: exp.StrPosition(
 498                this=seq_get(args, 1),
 499                substr=seq_get(args, 0),
 500                position=seq_get(args, 2),
 501            ),
 502            "DATEADD": build_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL),
 503            "DATEDIFF": _build_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL),
 504            "DATENAME": _build_formatted_time(exp.TimeToStr, full_format_mapping=True),
 505            "DATEPART": _build_formatted_time(exp.TimeToStr),
 506            "DATETIMEFROMPARTS": _build_datetimefromparts,
 507            "EOMONTH": _build_eomonth,
 508            "FORMAT": _build_format,
 509            "GETDATE": exp.CurrentTimestamp.from_arg_list,
 510            "HASHBYTES": _build_hashbytes,
 511            "ISNULL": exp.Coalesce.from_arg_list,
 512            "JSON_QUERY": parser.build_extract_json_with_path(exp.JSONExtract),
 513            "JSON_VALUE": parser.build_extract_json_with_path(exp.JSONExtractScalar),
 514            "LEN": _build_with_arg_as_text(exp.Length),
 515            "LEFT": _build_with_arg_as_text(exp.Left),
 516            "RIGHT": _build_with_arg_as_text(exp.Right),
 517            "REPLICATE": exp.Repeat.from_arg_list,
 518            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
 519            "SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
 520            "SUSER_NAME": exp.CurrentUser.from_arg_list,
 521            "SUSER_SNAME": exp.CurrentUser.from_arg_list,
 522            "SYSTEM_USER": exp.CurrentUser.from_arg_list,
 523            "TIMEFROMPARTS": _build_timefromparts,
 524        }
 525
 526        JOIN_HINTS = {"LOOP", "HASH", "MERGE", "REMOTE"}
 527
 528        RETURNS_TABLE_TOKENS = parser.Parser.ID_VAR_TOKENS - {
 529            TokenType.TABLE,
 530            *parser.Parser.TYPE_TOKENS,
 531        }
 532
 533        STATEMENT_PARSERS = {
 534            **parser.Parser.STATEMENT_PARSERS,
 535            TokenType.DECLARE: lambda self: self._parse_declare(),
 536        }
 537
 538        def _parse_options(self) -> t.Optional[t.List[exp.Expression]]:
 539            if not self._match(TokenType.OPTION):
 540                return None
 541
 542            def _parse_option() -> t.Optional[exp.Expression]:
 543                option = self._parse_var_from_options(OPTIONS)
 544                if not option:
 545                    return None
 546
 547                self._match(TokenType.EQ)
 548                return self.expression(
 549                    exp.QueryOption, this=option, expression=self._parse_primary_or_var()
 550                )
 551
 552            return self._parse_wrapped_csv(_parse_option)
 553
 554        def _parse_projections(self) -> t.List[exp.Expression]:
 555            """
 556            T-SQL supports the syntax alias = expression in the SELECT's projection list,
 557            so we transform all parsed Selects to convert their EQ projections into Aliases.
 558
 559            See: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-clause-transact-sql?view=sql-server-ver16#syntax
 560            """
 561            return [
 562                (
 563                    exp.alias_(projection.expression, projection.this.this, copy=False)
 564                    if isinstance(projection, exp.EQ) and isinstance(projection.this, exp.Column)
 565                    else projection
 566                )
 567                for projection in super()._parse_projections()
 568            ]
 569
 570        def _parse_commit_or_rollback(self) -> exp.Commit | exp.Rollback:
 571            """Applies to SQL Server and Azure SQL Database
 572            COMMIT [ { TRAN | TRANSACTION }
 573                [ transaction_name | @tran_name_variable ] ]
 574                [ WITH ( DELAYED_DURABILITY = { OFF | ON } ) ]
 575
 576            ROLLBACK { TRAN | TRANSACTION }
 577                [ transaction_name | @tran_name_variable
 578                | savepoint_name | @savepoint_variable ]
 579            """
 580            rollback = self._prev.token_type == TokenType.ROLLBACK
 581
 582            self._match_texts(("TRAN", "TRANSACTION"))
 583            this = self._parse_id_var()
 584
 585            if rollback:
 586                return self.expression(exp.Rollback, this=this)
 587
 588            durability = None
 589            if self._match_pair(TokenType.WITH, TokenType.L_PAREN):
 590                self._match_text_seq("DELAYED_DURABILITY")
 591                self._match(TokenType.EQ)
 592
 593                if self._match_text_seq("OFF"):
 594                    durability = False
 595                else:
 596                    self._match(TokenType.ON)
 597                    durability = True
 598
 599                self._match_r_paren()
 600
 601            return self.expression(exp.Commit, this=this, durability=durability)
 602
 603        def _parse_transaction(self) -> exp.Transaction | exp.Command:
 604            """Applies to SQL Server and Azure SQL Database
 605            BEGIN { TRAN | TRANSACTION }
 606            [ { transaction_name | @tran_name_variable }
 607            [ WITH MARK [ 'description' ] ]
 608            ]
 609            """
 610            if self._match_texts(("TRAN", "TRANSACTION")):
 611                transaction = self.expression(exp.Transaction, this=self._parse_id_var())
 612                if self._match_text_seq("WITH", "MARK"):
 613                    transaction.set("mark", self._parse_string())
 614
 615                return transaction
 616
 617            return self._parse_as_command(self._prev)
 618
 619        def _parse_returns(self) -> exp.ReturnsProperty:
 620            table = self._parse_id_var(any_token=False, tokens=self.RETURNS_TABLE_TOKENS)
 621            returns = super()._parse_returns()
 622            returns.set("table", table)
 623            return returns
 624
 625        def _parse_convert(
 626            self, strict: bool, safe: t.Optional[bool] = None
 627        ) -> t.Optional[exp.Expression]:
 628            this = self._parse_types()
 629            self._match(TokenType.COMMA)
 630            args = [this, *self._parse_csv(self._parse_assignment)]
 631            convert = exp.Convert.from_arg_list(args)
 632            convert.set("safe", safe)
 633            convert.set("strict", strict)
 634            return convert
 635
 636        def _parse_user_defined_function(
 637            self, kind: t.Optional[TokenType] = None
 638        ) -> t.Optional[exp.Expression]:
 639            this = super()._parse_user_defined_function(kind=kind)
 640
 641            if (
 642                kind == TokenType.FUNCTION
 643                or isinstance(this, exp.UserDefinedFunction)
 644                or self._match(TokenType.ALIAS, advance=False)
 645            ):
 646                return this
 647
 648            expressions = self._parse_csv(self._parse_function_parameter)
 649            return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)
 650
 651        def _parse_id_var(
 652            self,
 653            any_token: bool = True,
 654            tokens: t.Optional[t.Collection[TokenType]] = None,
 655        ) -> t.Optional[exp.Expression]:
 656            is_temporary = self._match(TokenType.HASH)
 657            is_global = is_temporary and self._match(TokenType.HASH)
 658
 659            this = super()._parse_id_var(any_token=any_token, tokens=tokens)
 660            if this:
 661                if is_global:
 662                    this.set("global", True)
 663                elif is_temporary:
 664                    this.set("temporary", True)
 665
 666            return this
 667
 668        def _parse_create(self) -> exp.Create | exp.Command:
 669            create = super()._parse_create()
 670
 671            if isinstance(create, exp.Create):
 672                table = create.this.this if isinstance(create.this, exp.Schema) else create.this
 673                if isinstance(table, exp.Table) and table.this.args.get("temporary"):
 674                    if not create.args.get("properties"):
 675                        create.set("properties", exp.Properties(expressions=[]))
 676
 677                    create.args["properties"].append("expressions", exp.TemporaryProperty())
 678
 679            return create
 680
 681        def _parse_if(self) -> t.Optional[exp.Expression]:
 682            index = self._index
 683
 684            if self._match_text_seq("OBJECT_ID"):
 685                self._parse_wrapped_csv(self._parse_string)
 686                if self._match_text_seq("IS", "NOT", "NULL") and self._match(TokenType.DROP):
 687                    return self._parse_drop(exists=True)
 688                self._retreat(index)
 689
 690            return super()._parse_if()
 691
 692        def _parse_unique(self) -> exp.UniqueColumnConstraint:
 693            if self._match_texts(("CLUSTERED", "NONCLUSTERED")):
 694                this = self.CONSTRAINT_PARSERS[self._prev.text.upper()](self)
 695            else:
 696                this = self._parse_schema(self._parse_id_var(any_token=False))
 697
 698            return self.expression(exp.UniqueColumnConstraint, this=this)
 699
 700        def _parse_partition(self) -> t.Optional[exp.Partition]:
 701            if not self._match_text_seq("WITH", "(", "PARTITIONS"):
 702                return None
 703
 704            def parse_range():
 705                low = self._parse_bitwise()
 706                high = self._parse_bitwise() if self._match_text_seq("TO") else None
 707
 708                return (
 709                    self.expression(exp.PartitionRange, this=low, expression=high) if high else low
 710                )
 711
 712            partition = self.expression(
 713                exp.Partition, expressions=self._parse_wrapped_csv(parse_range)
 714            )
 715
 716            self._match_r_paren()
 717
 718            return partition
 719
 720        def _parse_declare(self) -> exp.Declare | exp.Command:
 721            index = self._index
 722            expressions = self._try_parse(partial(self._parse_csv, self._parse_declareitem))
 723
 724            if not expressions or self._curr:
 725                self._retreat(index)
 726                return self._parse_as_command(self._prev)
 727
 728            return self.expression(exp.Declare, expressions=expressions)
 729
 730        def _parse_declareitem(self) -> t.Optional[exp.DeclareItem]:
 731            var = self._parse_id_var()
 732            if not var:
 733                return None
 734
 735            value = None
 736            self._match(TokenType.ALIAS)
 737            if self._match(TokenType.TABLE):
 738                data_type = self._parse_schema()
 739            else:
 740                data_type = self._parse_types()
 741                if self._match(TokenType.EQ):
 742                    value = self._parse_bitwise()
 743
 744            return self.expression(exp.DeclareItem, this=var, kind=data_type, default=value)
 745
 746    class Generator(generator.Generator):
 747        LIMIT_IS_TOP = True
 748        QUERY_HINTS = False
 749        RETURNING_END = False
 750        NVL2_SUPPORTED = False
 751        ALTER_TABLE_INCLUDE_COLUMN_KEYWORD = False
 752        LIMIT_FETCH = "FETCH"
 753        COMPUTED_COLUMN_WITH_TYPE = False
 754        CTE_RECURSIVE_KEYWORD_REQUIRED = False
 755        ENSURE_BOOLS = True
 756        NULL_ORDERING_SUPPORTED = None
 757        SUPPORTS_SINGLE_ARG_CONCAT = False
 758        TABLESAMPLE_SEED_KEYWORD = "REPEATABLE"
 759        SUPPORTS_SELECT_INTO = True
 760        JSON_PATH_BRACKETED_KEY_SUPPORTED = False
 761        SUPPORTS_TO_NUMBER = False
 762        SET_OP_MODIFIERS = False
 763        COPY_PARAMS_EQ_REQUIRED = True
 764
 765        EXPRESSIONS_WITHOUT_NESTED_CTES = {
 766            exp.Delete,
 767            exp.Insert,
 768            exp.Intersect,
 769            exp.Except,
 770            exp.Merge,
 771            exp.Select,
 772            exp.Subquery,
 773            exp.Union,
 774            exp.Update,
 775        }
 776
 777        SUPPORTED_JSON_PATH_PARTS = {
 778            exp.JSONPathKey,
 779            exp.JSONPathRoot,
 780            exp.JSONPathSubscript,
 781        }
 782
 783        TYPE_MAPPING = {
 784            **generator.Generator.TYPE_MAPPING,
 785            exp.DataType.Type.BOOLEAN: "BIT",
 786            exp.DataType.Type.DECIMAL: "NUMERIC",
 787            exp.DataType.Type.DATETIME: "DATETIME2",
 788            exp.DataType.Type.DOUBLE: "FLOAT",
 789            exp.DataType.Type.INT: "INTEGER",
 790            exp.DataType.Type.ROWVERSION: "ROWVERSION",
 791            exp.DataType.Type.TEXT: "VARCHAR(MAX)",
 792            exp.DataType.Type.TIMESTAMP: "DATETIME2",
 793            exp.DataType.Type.TIMESTAMPTZ: "DATETIMEOFFSET",
 794            exp.DataType.Type.UTINYINT: "TINYINT",
 795            exp.DataType.Type.VARIANT: "SQL_VARIANT",
 796        }
 797
 798        TYPE_MAPPING.pop(exp.DataType.Type.NCHAR)
 799        TYPE_MAPPING.pop(exp.DataType.Type.NVARCHAR)
 800
 801        TRANSFORMS = {
 802            **generator.Generator.TRANSFORMS,
 803            exp.AnyValue: any_value_to_max_sql,
 804            exp.ArrayToString: rename_func("STRING_AGG"),
 805            exp.AutoIncrementColumnConstraint: lambda *_: "IDENTITY",
 806            exp.DateAdd: date_delta_sql("DATEADD"),
 807            exp.DateDiff: date_delta_sql("DATEDIFF"),
 808            exp.CTE: transforms.preprocess([qualify_derived_table_outputs]),
 809            exp.CurrentDate: rename_func("GETDATE"),
 810            exp.CurrentTimestamp: rename_func("GETDATE"),
 811            exp.DateStrToDate: datestrtodate_sql,
 812            exp.Extract: rename_func("DATEPART"),
 813            exp.GeneratedAsIdentityColumnConstraint: generatedasidentitycolumnconstraint_sql,
 814            exp.GroupConcat: _string_agg_sql,
 815            exp.If: rename_func("IIF"),
 816            exp.JSONExtract: _json_extract_sql,
 817            exp.JSONExtractScalar: _json_extract_sql,
 818            exp.LastDay: lambda self, e: self.func("EOMONTH", e.this),
 819            exp.Max: max_or_greatest,
 820            exp.MD5: lambda self, e: self.func("HASHBYTES", exp.Literal.string("MD5"), e.this),
 821            exp.Min: min_or_least,
 822            exp.NumberToStr: _format_sql,
 823            exp.ParseJSON: lambda self, e: self.sql(e, "this"),
 824            exp.Repeat: rename_func("REPLICATE"),
 825            exp.Select: transforms.preprocess(
 826                [
 827                    transforms.eliminate_distinct_on,
 828                    transforms.eliminate_semi_and_anti_joins,
 829                    transforms.eliminate_qualify,
 830                ]
 831            ),
 832            exp.StrPosition: lambda self, e: self.func(
 833                "CHARINDEX", e.args.get("substr"), e.this, e.args.get("position")
 834            ),
 835            exp.Subquery: transforms.preprocess([qualify_derived_table_outputs]),
 836            exp.SHA: lambda self, e: self.func("HASHBYTES", exp.Literal.string("SHA1"), e.this),
 837            exp.SHA2: lambda self, e: self.func(
 838                "HASHBYTES", exp.Literal.string(f"SHA2_{e.args.get('length', 256)}"), e.this
 839            ),
 840            exp.TemporaryProperty: lambda self, e: "",
 841            exp.TimeStrToTime: lambda self, e: self.sql(
 842                exp.cast(e.this, exp.DataType.Type.DATETIME)
 843            ),
 844            exp.TimeToStr: _format_sql,
 845            exp.Trim: trim_sql,
 846            exp.TsOrDsAdd: date_delta_sql("DATEADD", cast=True),
 847            exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
 848        }
 849
 850        TRANSFORMS.pop(exp.ReturnsProperty)
 851
 852        PROPERTIES_LOCATION = {
 853            **generator.Generator.PROPERTIES_LOCATION,
 854            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
 855        }
 856
 857        def select_sql(self, expression: exp.Select) -> str:
 858            if expression.args.get("offset"):
 859                if not expression.args.get("order"):
 860                    # ORDER BY is required in order to use OFFSET in a query, so we use
 861                    # a noop order by, since we don't really care about the order.
 862                    # See: https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
 863                    expression.order_by(exp.select(exp.null()).subquery(), copy=False)
 864
 865                limit = expression.args.get("limit")
 866                if isinstance(limit, exp.Limit):
 867                    # TOP and OFFSET can't be combined, we need use FETCH instead of TOP
 868                    # we replace here because otherwise TOP would be generated in select_sql
 869                    limit.replace(exp.Fetch(direction="FIRST", count=limit.expression))
 870
 871            return super().select_sql(expression)
 872
 873        def convert_sql(self, expression: exp.Convert) -> str:
 874            name = "TRY_CONVERT" if expression.args.get("safe") else "CONVERT"
 875            return self.func(
 876                name, expression.this, expression.expression, expression.args.get("style")
 877            )
 878
 879        def queryoption_sql(self, expression: exp.QueryOption) -> str:
 880            option = self.sql(expression, "this")
 881            value = self.sql(expression, "expression")
 882            if value:
 883                optional_equal_sign = "= " if option in OPTIONS_THAT_REQUIRE_EQUAL else ""
 884                return f"{option} {optional_equal_sign}{value}"
 885            return option
 886
 887        def lateral_op(self, expression: exp.Lateral) -> str:
 888            cross_apply = expression.args.get("cross_apply")
 889            if cross_apply is True:
 890                return "CROSS APPLY"
 891            if cross_apply is False:
 892                return "OUTER APPLY"
 893
 894            # TODO: perhaps we can check if the parent is a Join and transpile it appropriately
 895            self.unsupported("LATERAL clause is not supported.")
 896            return "LATERAL"
 897
 898        def timefromparts_sql(self, expression: exp.TimeFromParts) -> str:
 899            nano = expression.args.get("nano")
 900            if nano is not None:
 901                nano.pop()
 902                self.unsupported("Specifying nanoseconds is not supported in TIMEFROMPARTS.")
 903
 904            if expression.args.get("fractions") is None:
 905                expression.set("fractions", exp.Literal.number(0))
 906            if expression.args.get("precision") is None:
 907                expression.set("precision", exp.Literal.number(0))
 908
 909            return rename_func("TIMEFROMPARTS")(self, expression)
 910
 911        def timestampfromparts_sql(self, expression: exp.TimestampFromParts) -> str:
 912            zone = expression.args.get("zone")
 913            if zone is not None:
 914                zone.pop()
 915                self.unsupported("Time zone is not supported in DATETIMEFROMPARTS.")
 916
 917            nano = expression.args.get("nano")
 918            if nano is not None:
 919                nano.pop()
 920                self.unsupported("Specifying nanoseconds is not supported in DATETIMEFROMPARTS.")
 921
 922            if expression.args.get("milli") is None:
 923                expression.set("milli", exp.Literal.number(0))
 924
 925            return rename_func("DATETIMEFROMPARTS")(self, expression)
 926
 927        def setitem_sql(self, expression: exp.SetItem) -> str:
 928            this = expression.this
 929            if isinstance(this, exp.EQ) and not isinstance(this.left, exp.Parameter):
 930                # T-SQL does not use '=' in SET command, except when the LHS is a variable.
 931                return f"{self.sql(this.left)} {self.sql(this.right)}"
 932
 933            return super().setitem_sql(expression)
 934
 935        def boolean_sql(self, expression: exp.Boolean) -> str:
 936            if type(expression.parent) in BIT_TYPES:
 937                return "1" if expression.this else "0"
 938
 939            return "(1 = 1)" if expression.this else "(1 = 0)"
 940
 941        def is_sql(self, expression: exp.Is) -> str:
 942            if isinstance(expression.expression, exp.Boolean):
 943                return self.binary(expression, "=")
 944            return self.binary(expression, "IS")
 945
 946        def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
 947            sql = self.sql(expression, "this")
 948            properties = expression.args.get("properties")
 949
 950            if sql[:1] != "#" and any(
 951                isinstance(prop, exp.TemporaryProperty)
 952                for prop in (properties.expressions if properties else [])
 953            ):
 954                sql = f"[#{sql[1:]}" if sql.startswith("[") else f"#{sql}"
 955
 956            return sql
 957
 958        def create_sql(self, expression: exp.Create) -> str:
 959            kind = expression.kind
 960            exists = expression.args.pop("exists", None)
 961
 962            if kind == "VIEW":
 963                expression.this.set("catalog", None)
 964
 965            sql = super().create_sql(expression)
 966
 967            like_property = expression.find(exp.LikeProperty)
 968            if like_property:
 969                ctas_expression = like_property.this
 970            else:
 971                ctas_expression = expression.expression
 972
 973            table = expression.find(exp.Table)
 974
 975            # Convert CTAS statement to SELECT .. INTO ..
 976            if kind == "TABLE" and ctas_expression:
 977                ctas_with = ctas_expression.args.get("with")
 978                if ctas_with:
 979                    ctas_with = ctas_with.pop()
 980
 981                if isinstance(ctas_expression, exp.UNWRAPPED_QUERIES):
 982                    ctas_expression = ctas_expression.subquery()
 983
 984                select_into = exp.select("*").from_(exp.alias_(ctas_expression, "temp", table=True))
 985                select_into.set("into", exp.Into(this=table))
 986                select_into.set("with", ctas_with)
 987
 988                if like_property:
 989                    select_into.limit(0, copy=False)
 990
 991                sql = self.sql(select_into)
 992
 993            if exists:
 994                identifier = self.sql(exp.Literal.string(exp.table_name(table) if table else ""))
 995                sql = self.sql(exp.Literal.string(sql))
 996                if kind == "SCHEMA":
 997                    sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE schema_name = {identifier}) EXEC({sql})"""
 998                elif kind == "TABLE":
 999                    assert table
1000                    where = exp.and_(
1001                        exp.column("table_name").eq(table.name),
1002                        exp.column("table_schema").eq(table.db) if table.db else None,
1003                        exp.column("table_catalog").eq(table.catalog) if table.catalog else None,
1004                    )
1005                    sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE {where}) EXEC({sql})"""
1006                elif kind == "INDEX":
1007                    index = self.sql(exp.Literal.string(expression.this.text("this")))
1008                    sql = f"""IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id({identifier}) AND name = {index}) EXEC({sql})"""
1009            elif expression.args.get("replace"):
1010                sql = sql.replace("CREATE OR REPLACE ", "CREATE OR ALTER ", 1)
1011
1012            return self.prepend_ctes(expression, sql)
1013
1014        def offset_sql(self, expression: exp.Offset) -> str:
1015            return f"{super().offset_sql(expression)} ROWS"
1016
1017        def version_sql(self, expression: exp.Version) -> str:
1018            name = "SYSTEM_TIME" if expression.name == "TIMESTAMP" else expression.name
1019            this = f"FOR {name}"
1020            expr = expression.expression
1021            kind = expression.text("kind")
1022            if kind in ("FROM", "BETWEEN"):
1023                args = expr.expressions
1024                sep = "TO" if kind == "FROM" else "AND"
1025                expr_sql = f"{self.sql(seq_get(args, 0))} {sep} {self.sql(seq_get(args, 1))}"
1026            else:
1027                expr_sql = self.sql(expr)
1028
1029            expr_sql = f" {expr_sql}" if expr_sql else ""
1030            return f"{this} {kind}{expr_sql}"
1031
1032        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
1033            table = expression.args.get("table")
1034            table = f"{table} " if table else ""
1035            return f"RETURNS {table}{self.sql(expression, 'this')}"
1036
1037        def returning_sql(self, expression: exp.Returning) -> str:
1038            into = self.sql(expression, "into")
1039            into = self.seg(f"INTO {into}") if into else ""
1040            return f"{self.seg('OUTPUT')} {self.expressions(expression, flat=True)}{into}"
1041
1042        def transaction_sql(self, expression: exp.Transaction) -> str:
1043            this = self.sql(expression, "this")
1044            this = f" {this}" if this else ""
1045            mark = self.sql(expression, "mark")
1046            mark = f" WITH MARK {mark}" if mark else ""
1047            return f"BEGIN TRANSACTION{this}{mark}"
1048
1049        def commit_sql(self, expression: exp.Commit) -> str:
1050            this = self.sql(expression, "this")
1051            this = f" {this}" if this else ""
1052            durability = expression.args.get("durability")
1053            durability = (
1054                f" WITH (DELAYED_DURABILITY = {'ON' if durability else 'OFF'})"
1055                if durability is not None
1056                else ""
1057            )
1058            return f"COMMIT TRANSACTION{this}{durability}"
1059
1060        def rollback_sql(self, expression: exp.Rollback) -> str:
1061            this = self.sql(expression, "this")
1062            this = f" {this}" if this else ""
1063            return f"ROLLBACK TRANSACTION{this}"
1064
1065        def identifier_sql(self, expression: exp.Identifier) -> str:
1066            identifier = super().identifier_sql(expression)
1067
1068            if expression.args.get("global"):
1069                identifier = f"##{identifier}"
1070            elif expression.args.get("temporary"):
1071                identifier = f"#{identifier}"
1072
1073            return identifier
1074
1075        def constraint_sql(self, expression: exp.Constraint) -> str:
1076            this = self.sql(expression, "this")
1077            expressions = self.expressions(expression, flat=True, sep=" ")
1078            return f"CONSTRAINT {this} {expressions}"
1079
1080        def length_sql(self, expression: exp.Length) -> str:
1081            return self._uncast_text(expression, "LEN")
1082
1083        def right_sql(self, expression: exp.Right) -> str:
1084            return self._uncast_text(expression, "RIGHT")
1085
1086        def left_sql(self, expression: exp.Left) -> str:
1087            return self._uncast_text(expression, "LEFT")
1088
1089        def _uncast_text(self, expression: exp.Expression, name: str) -> str:
1090            this = expression.this
1091            if isinstance(this, exp.Cast) and this.is_type(exp.DataType.Type.TEXT):
1092                this_sql = self.sql(this, "this")
1093            else:
1094                this_sql = self.sql(this)
1095            expression_sql = self.sql(expression, "expression")
1096            return self.func(name, this_sql, expression_sql if expression_sql else None)
1097
1098        def partition_sql(self, expression: exp.Partition) -> str:
1099            return f"WITH (PARTITIONS({self.expressions(expression, flat=True)}))"
1100
1101        def altertable_sql(self, expression: exp.AlterTable) -> str:
1102            action = seq_get(expression.args.get("actions") or [], 0)
1103            if isinstance(action, exp.RenameTable):
1104                return f"EXEC sp_rename '{self.sql(expression.this)}', '{action.this.name}'"
1105            return super().altertable_sql(expression)
1106
1107        def drop_sql(self, expression: exp.Drop) -> str:
1108            if expression.args["kind"] == "VIEW":
1109                expression.this.set("catalog", None)
1110            return super().drop_sql(expression)
1111
1112        def declare_sql(self, expression: exp.Declare) -> str:
1113            return f"DECLARE {self.expressions(expression, flat=True)}"
1114
1115        def declareitem_sql(self, expression: exp.DeclareItem) -> str:
1116            variable = self.sql(expression, "this")
1117            default = self.sql(expression, "default")
1118            default = f" = {default}" if default else ""
1119
1120            kind = self.sql(expression, "kind")
1121            if isinstance(expression.args.get("kind"), exp.Schema):
1122                kind = f"TABLE {kind}"
1123
1124            return f"{variable} AS {kind}{default}"
1125
1126        def options_modifier(self, expression: exp.Expression) -> str:
1127            options = self.expressions(expression, key="options")
1128            return f" OPTION{self.wrap(options)}" if options else ""
FULL_FORMAT_TIME_MAPPING = {'weekday': '%A', 'dw': '%A', 'w': '%A', 'month': '%B', 'mm': '%B', 'm': '%B'}
DATE_DELTA_INTERVAL = {'year': 'year', 'yyyy': 'year', 'yy': 'year', 'quarter': 'quarter', 'qq': 'quarter', 'q': 'quarter', 'month': 'month', 'mm': 'month', 'm': 'month', 'week': 'week', 'ww': 'week', 'wk': 'week', 'day': 'day', 'dd': 'day', 'd': 'day'}
DATE_FMT_RE = re.compile('([dD]{1,2})|([mM]{1,2})|([yY]{1,4})|([hH]{1,2})|([sS]{1,2})')
TRANSPILE_SAFE_NUMBER_FMT = {'N', 'C'}
DEFAULT_START_DATE = datetime.date(1900, 1, 1)
OPTIONS: Dict[str, Sequence[Union[Sequence[str], str]]] = {'DISABLE_OPTIMIZED_PLAN_FORCING': (), 'FAST': (), 'IGNORE_NONCLUSTERED_COLUMNSTORE_INDEX': (), 'LABEL': (), 'MAXDOP': (), 'MAXRECURSION': (), 'MAX_GRANT_PERCENT': (), 'MIN_GRANT_PERCENT': (), 'NO_PERFORMANCE_SPOOL': (), 'QUERYTRACEON': (), 'RECOMPILE': (), 'CONCAT': ('UNION',), 'DISABLE': ('EXTERNALPUSHDOWN', 'SCALEOUTEXECUTION'), 'EXPAND': ('VIEWS',), 'FORCE': ('EXTERNALPUSHDOWN', 'ORDER', 'SCALEOUTEXECUTION'), 'HASH': ('GROUP', 'JOIN', 'UNION'), 'KEEP': ('PLAN',), 'KEEPFIXED': ('PLAN',), 'LOOP': ('JOIN',), 'MERGE': ('JOIN', 'UNION'), 'OPTIMIZE': (('FOR', 'UNKNOWN'),), 'ORDER': ('GROUP',), 'PARAMETERIZATION': ('FORCED', 'SIMPLE'), 'ROBUST': ('PLAN',), 'USE': ('PLAN',)}
OPTIONS_THAT_REQUIRE_EQUAL = ('MAX_GRANT_PERCENT', 'MIN_GRANT_PERCENT', 'LABEL')
DATEPART_ONLY_FORMATS = {'HOUR', 'QUARTER', 'DW'}
def qualify_derived_table_outputs( expression: sqlglot.expressions.Expression) -> sqlglot.expressions.Expression:
250def qualify_derived_table_outputs(expression: exp.Expression) -> exp.Expression:
251    """Ensures all (unnamed) output columns are aliased for CTEs and Subqueries."""
252    alias = expression.args.get("alias")
253
254    if (
255        isinstance(expression, (exp.CTE, exp.Subquery))
256        and isinstance(alias, exp.TableAlias)
257        and not alias.columns
258    ):
259        from sqlglot.optimizer.qualify_columns import qualify_outputs
260
261        # We keep track of the unaliased column projection indexes instead of the expressions
262        # themselves, because the latter are going to be replaced by new nodes when the aliases
263        # are added and hence we won't be able to reach these newly added Alias parents
264        query = expression.this
265        unaliased_column_indexes = (
266            i for i, c in enumerate(query.selects) if isinstance(c, exp.Column) and not c.alias
267        )
268
269        qualify_outputs(query)
270
271        # Preserve the quoting information of columns for newly added Alias nodes
272        query_selects = query.selects
273        for select_index in unaliased_column_indexes:
274            alias = query_selects[select_index]
275            column = alias.this
276            if isinstance(column.this, exp.Identifier):
277                alias.args["alias"].set("quoted", column.this.quoted)
278
279    return expression

Ensures all (unnamed) output columns are aliased for CTEs and Subqueries.

class TSQL(sqlglot.dialects.dialect.Dialect):
 334class TSQL(Dialect):
 335    NORMALIZATION_STRATEGY = NormalizationStrategy.CASE_INSENSITIVE
 336    TIME_FORMAT = "'yyyy-mm-dd hh:mm:ss'"
 337    SUPPORTS_SEMI_ANTI_JOIN = False
 338    LOG_BASE_FIRST = False
 339    TYPED_DIVISION = True
 340    CONCAT_COALESCE = True
 341
 342    TIME_MAPPING = {
 343        "year": "%Y",
 344        "dayofyear": "%j",
 345        "day": "%d",
 346        "dy": "%d",
 347        "y": "%Y",
 348        "week": "%W",
 349        "ww": "%W",
 350        "wk": "%W",
 351        "hour": "%h",
 352        "hh": "%I",
 353        "minute": "%M",
 354        "mi": "%M",
 355        "n": "%M",
 356        "second": "%S",
 357        "ss": "%S",
 358        "s": "%-S",
 359        "millisecond": "%f",
 360        "ms": "%f",
 361        "weekday": "%W",
 362        "dw": "%W",
 363        "month": "%m",
 364        "mm": "%M",
 365        "m": "%-M",
 366        "Y": "%Y",
 367        "YYYY": "%Y",
 368        "YY": "%y",
 369        "MMMM": "%B",
 370        "MMM": "%b",
 371        "MM": "%m",
 372        "M": "%-m",
 373        "dddd": "%A",
 374        "dd": "%d",
 375        "d": "%-d",
 376        "HH": "%H",
 377        "H": "%-H",
 378        "h": "%-I",
 379        "S": "%f",
 380        "yyyy": "%Y",
 381        "yy": "%y",
 382    }
 383
 384    CONVERT_FORMAT_MAPPING = {
 385        "0": "%b %d %Y %-I:%M%p",
 386        "1": "%m/%d/%y",
 387        "2": "%y.%m.%d",
 388        "3": "%d/%m/%y",
 389        "4": "%d.%m.%y",
 390        "5": "%d-%m-%y",
 391        "6": "%d %b %y",
 392        "7": "%b %d, %y",
 393        "8": "%H:%M:%S",
 394        "9": "%b %d %Y %-I:%M:%S:%f%p",
 395        "10": "mm-dd-yy",
 396        "11": "yy/mm/dd",
 397        "12": "yymmdd",
 398        "13": "%d %b %Y %H:%M:ss:%f",
 399        "14": "%H:%M:%S:%f",
 400        "20": "%Y-%m-%d %H:%M:%S",
 401        "21": "%Y-%m-%d %H:%M:%S.%f",
 402        "22": "%m/%d/%y %-I:%M:%S %p",
 403        "23": "%Y-%m-%d",
 404        "24": "%H:%M:%S",
 405        "25": "%Y-%m-%d %H:%M:%S.%f",
 406        "100": "%b %d %Y %-I:%M%p",
 407        "101": "%m/%d/%Y",
 408        "102": "%Y.%m.%d",
 409        "103": "%d/%m/%Y",
 410        "104": "%d.%m.%Y",
 411        "105": "%d-%m-%Y",
 412        "106": "%d %b %Y",
 413        "107": "%b %d, %Y",
 414        "108": "%H:%M:%S",
 415        "109": "%b %d %Y %-I:%M:%S:%f%p",
 416        "110": "%m-%d-%Y",
 417        "111": "%Y/%m/%d",
 418        "112": "%Y%m%d",
 419        "113": "%d %b %Y %H:%M:%S:%f",
 420        "114": "%H:%M:%S:%f",
 421        "120": "%Y-%m-%d %H:%M:%S",
 422        "121": "%Y-%m-%d %H:%M:%S.%f",
 423    }
 424
 425    FORMAT_TIME_MAPPING = {
 426        "y": "%B %Y",
 427        "d": "%m/%d/%Y",
 428        "H": "%-H",
 429        "h": "%-I",
 430        "s": "%Y-%m-%d %H:%M:%S",
 431        "D": "%A,%B,%Y",
 432        "f": "%A,%B,%Y %-I:%M %p",
 433        "F": "%A,%B,%Y %-I:%M:%S %p",
 434        "g": "%m/%d/%Y %-I:%M %p",
 435        "G": "%m/%d/%Y %-I:%M:%S %p",
 436        "M": "%B %-d",
 437        "m": "%B %-d",
 438        "O": "%Y-%m-%dT%H:%M:%S",
 439        "u": "%Y-%M-%D %H:%M:%S%z",
 440        "U": "%A, %B %D, %Y %H:%M:%S%z",
 441        "T": "%-I:%M:%S %p",
 442        "t": "%-I:%M",
 443        "Y": "%a %Y",
 444    }
 445
 446    class Tokenizer(tokens.Tokenizer):
 447        IDENTIFIERS = [("[", "]"), '"']
 448        QUOTES = ["'", '"']
 449        HEX_STRINGS = [("0x", ""), ("0X", "")]
 450        VAR_SINGLE_TOKENS = {"@", "$", "#"}
 451
 452        KEYWORDS = {
 453            **tokens.Tokenizer.KEYWORDS,
 454            "CLUSTERED INDEX": TokenType.INDEX,
 455            "DATETIME2": TokenType.DATETIME,
 456            "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
 457            "DECLARE": TokenType.DECLARE,
 458            "EXEC": TokenType.COMMAND,
 459            "FOR SYSTEM_TIME": TokenType.TIMESTAMP_SNAPSHOT,
 460            "IMAGE": TokenType.IMAGE,
 461            "MONEY": TokenType.MONEY,
 462            "NONCLUSTERED INDEX": TokenType.INDEX,
 463            "NTEXT": TokenType.TEXT,
 464            "OPTION": TokenType.OPTION,
 465            "OUTPUT": TokenType.RETURNING,
 466            "PRINT": TokenType.COMMAND,
 467            "PROC": TokenType.PROCEDURE,
 468            "REAL": TokenType.FLOAT,
 469            "ROWVERSION": TokenType.ROWVERSION,
 470            "SMALLDATETIME": TokenType.DATETIME,
 471            "SMALLMONEY": TokenType.SMALLMONEY,
 472            "SQL_VARIANT": TokenType.VARIANT,
 473            "SYSTEM_USER": TokenType.CURRENT_USER,
 474            "TOP": TokenType.TOP,
 475            "TIMESTAMP": TokenType.ROWVERSION,
 476            "TINYINT": TokenType.UTINYINT,
 477            "UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
 478            "UPDATE STATISTICS": TokenType.COMMAND,
 479            "XML": TokenType.XML,
 480        }
 481
 482        COMMANDS = {*tokens.Tokenizer.COMMANDS, TokenType.END}
 483
 484    class Parser(parser.Parser):
 485        SET_REQUIRES_ASSIGNMENT_DELIMITER = False
 486        LOG_DEFAULTS_TO_LN = True
 487        ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False
 488        STRING_ALIASES = True
 489        NO_PAREN_IF_COMMANDS = False
 490
 491        QUERY_MODIFIER_PARSERS = {
 492            **parser.Parser.QUERY_MODIFIER_PARSERS,
 493            TokenType.OPTION: lambda self: ("options", self._parse_options()),
 494        }
 495
 496        FUNCTIONS = {
 497            **parser.Parser.FUNCTIONS,
 498            "CHARINDEX": lambda args: exp.StrPosition(
 499                this=seq_get(args, 1),
 500                substr=seq_get(args, 0),
 501                position=seq_get(args, 2),
 502            ),
 503            "DATEADD": build_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL),
 504            "DATEDIFF": _build_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL),
 505            "DATENAME": _build_formatted_time(exp.TimeToStr, full_format_mapping=True),
 506            "DATEPART": _build_formatted_time(exp.TimeToStr),
 507            "DATETIMEFROMPARTS": _build_datetimefromparts,
 508            "EOMONTH": _build_eomonth,
 509            "FORMAT": _build_format,
 510            "GETDATE": exp.CurrentTimestamp.from_arg_list,
 511            "HASHBYTES": _build_hashbytes,
 512            "ISNULL": exp.Coalesce.from_arg_list,
 513            "JSON_QUERY": parser.build_extract_json_with_path(exp.JSONExtract),
 514            "JSON_VALUE": parser.build_extract_json_with_path(exp.JSONExtractScalar),
 515            "LEN": _build_with_arg_as_text(exp.Length),
 516            "LEFT": _build_with_arg_as_text(exp.Left),
 517            "RIGHT": _build_with_arg_as_text(exp.Right),
 518            "REPLICATE": exp.Repeat.from_arg_list,
 519            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
 520            "SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
 521            "SUSER_NAME": exp.CurrentUser.from_arg_list,
 522            "SUSER_SNAME": exp.CurrentUser.from_arg_list,
 523            "SYSTEM_USER": exp.CurrentUser.from_arg_list,
 524            "TIMEFROMPARTS": _build_timefromparts,
 525        }
 526
 527        JOIN_HINTS = {"LOOP", "HASH", "MERGE", "REMOTE"}
 528
 529        RETURNS_TABLE_TOKENS = parser.Parser.ID_VAR_TOKENS - {
 530            TokenType.TABLE,
 531            *parser.Parser.TYPE_TOKENS,
 532        }
 533
 534        STATEMENT_PARSERS = {
 535            **parser.Parser.STATEMENT_PARSERS,
 536            TokenType.DECLARE: lambda self: self._parse_declare(),
 537        }
 538
 539        def _parse_options(self) -> t.Optional[t.List[exp.Expression]]:
 540            if not self._match(TokenType.OPTION):
 541                return None
 542
 543            def _parse_option() -> t.Optional[exp.Expression]:
 544                option = self._parse_var_from_options(OPTIONS)
 545                if not option:
 546                    return None
 547
 548                self._match(TokenType.EQ)
 549                return self.expression(
 550                    exp.QueryOption, this=option, expression=self._parse_primary_or_var()
 551                )
 552
 553            return self._parse_wrapped_csv(_parse_option)
 554
 555        def _parse_projections(self) -> t.List[exp.Expression]:
 556            """
 557            T-SQL supports the syntax alias = expression in the SELECT's projection list,
 558            so we transform all parsed Selects to convert their EQ projections into Aliases.
 559
 560            See: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-clause-transact-sql?view=sql-server-ver16#syntax
 561            """
 562            return [
 563                (
 564                    exp.alias_(projection.expression, projection.this.this, copy=False)
 565                    if isinstance(projection, exp.EQ) and isinstance(projection.this, exp.Column)
 566                    else projection
 567                )
 568                for projection in super()._parse_projections()
 569            ]
 570
 571        def _parse_commit_or_rollback(self) -> exp.Commit | exp.Rollback:
 572            """Applies to SQL Server and Azure SQL Database
 573            COMMIT [ { TRAN | TRANSACTION }
 574                [ transaction_name | @tran_name_variable ] ]
 575                [ WITH ( DELAYED_DURABILITY = { OFF | ON } ) ]
 576
 577            ROLLBACK { TRAN | TRANSACTION }
 578                [ transaction_name | @tran_name_variable
 579                | savepoint_name | @savepoint_variable ]
 580            """
 581            rollback = self._prev.token_type == TokenType.ROLLBACK
 582
 583            self._match_texts(("TRAN", "TRANSACTION"))
 584            this = self._parse_id_var()
 585
 586            if rollback:
 587                return self.expression(exp.Rollback, this=this)
 588
 589            durability = None
 590            if self._match_pair(TokenType.WITH, TokenType.L_PAREN):
 591                self._match_text_seq("DELAYED_DURABILITY")
 592                self._match(TokenType.EQ)
 593
 594                if self._match_text_seq("OFF"):
 595                    durability = False
 596                else:
 597                    self._match(TokenType.ON)
 598                    durability = True
 599
 600                self._match_r_paren()
 601
 602            return self.expression(exp.Commit, this=this, durability=durability)
 603
 604        def _parse_transaction(self) -> exp.Transaction | exp.Command:
 605            """Applies to SQL Server and Azure SQL Database
 606            BEGIN { TRAN | TRANSACTION }
 607            [ { transaction_name | @tran_name_variable }
 608            [ WITH MARK [ 'description' ] ]
 609            ]
 610            """
 611            if self._match_texts(("TRAN", "TRANSACTION")):
 612                transaction = self.expression(exp.Transaction, this=self._parse_id_var())
 613                if self._match_text_seq("WITH", "MARK"):
 614                    transaction.set("mark", self._parse_string())
 615
 616                return transaction
 617
 618            return self._parse_as_command(self._prev)
 619
 620        def _parse_returns(self) -> exp.ReturnsProperty:
 621            table = self._parse_id_var(any_token=False, tokens=self.RETURNS_TABLE_TOKENS)
 622            returns = super()._parse_returns()
 623            returns.set("table", table)
 624            return returns
 625
 626        def _parse_convert(
 627            self, strict: bool, safe: t.Optional[bool] = None
 628        ) -> t.Optional[exp.Expression]:
 629            this = self._parse_types()
 630            self._match(TokenType.COMMA)
 631            args = [this, *self._parse_csv(self._parse_assignment)]
 632            convert = exp.Convert.from_arg_list(args)
 633            convert.set("safe", safe)
 634            convert.set("strict", strict)
 635            return convert
 636
 637        def _parse_user_defined_function(
 638            self, kind: t.Optional[TokenType] = None
 639        ) -> t.Optional[exp.Expression]:
 640            this = super()._parse_user_defined_function(kind=kind)
 641
 642            if (
 643                kind == TokenType.FUNCTION
 644                or isinstance(this, exp.UserDefinedFunction)
 645                or self._match(TokenType.ALIAS, advance=False)
 646            ):
 647                return this
 648
 649            expressions = self._parse_csv(self._parse_function_parameter)
 650            return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)
 651
 652        def _parse_id_var(
 653            self,
 654            any_token: bool = True,
 655            tokens: t.Optional[t.Collection[TokenType]] = None,
 656        ) -> t.Optional[exp.Expression]:
 657            is_temporary = self._match(TokenType.HASH)
 658            is_global = is_temporary and self._match(TokenType.HASH)
 659
 660            this = super()._parse_id_var(any_token=any_token, tokens=tokens)
 661            if this:
 662                if is_global:
 663                    this.set("global", True)
 664                elif is_temporary:
 665                    this.set("temporary", True)
 666
 667            return this
 668
 669        def _parse_create(self) -> exp.Create | exp.Command:
 670            create = super()._parse_create()
 671
 672            if isinstance(create, exp.Create):
 673                table = create.this.this if isinstance(create.this, exp.Schema) else create.this
 674                if isinstance(table, exp.Table) and table.this.args.get("temporary"):
 675                    if not create.args.get("properties"):
 676                        create.set("properties", exp.Properties(expressions=[]))
 677
 678                    create.args["properties"].append("expressions", exp.TemporaryProperty())
 679
 680            return create
 681
 682        def _parse_if(self) -> t.Optional[exp.Expression]:
 683            index = self._index
 684
 685            if self._match_text_seq("OBJECT_ID"):
 686                self._parse_wrapped_csv(self._parse_string)
 687                if self._match_text_seq("IS", "NOT", "NULL") and self._match(TokenType.DROP):
 688                    return self._parse_drop(exists=True)
 689                self._retreat(index)
 690
 691            return super()._parse_if()
 692
 693        def _parse_unique(self) -> exp.UniqueColumnConstraint:
 694            if self._match_texts(("CLUSTERED", "NONCLUSTERED")):
 695                this = self.CONSTRAINT_PARSERS[self._prev.text.upper()](self)
 696            else:
 697                this = self._parse_schema(self._parse_id_var(any_token=False))
 698
 699            return self.expression(exp.UniqueColumnConstraint, this=this)
 700
 701        def _parse_partition(self) -> t.Optional[exp.Partition]:
 702            if not self._match_text_seq("WITH", "(", "PARTITIONS"):
 703                return None
 704
 705            def parse_range():
 706                low = self._parse_bitwise()
 707                high = self._parse_bitwise() if self._match_text_seq("TO") else None
 708
 709                return (
 710                    self.expression(exp.PartitionRange, this=low, expression=high) if high else low
 711                )
 712
 713            partition = self.expression(
 714                exp.Partition, expressions=self._parse_wrapped_csv(parse_range)
 715            )
 716
 717            self._match_r_paren()
 718
 719            return partition
 720
 721        def _parse_declare(self) -> exp.Declare | exp.Command:
 722            index = self._index
 723            expressions = self._try_parse(partial(self._parse_csv, self._parse_declareitem))
 724
 725            if not expressions or self._curr:
 726                self._retreat(index)
 727                return self._parse_as_command(self._prev)
 728
 729            return self.expression(exp.Declare, expressions=expressions)
 730
 731        def _parse_declareitem(self) -> t.Optional[exp.DeclareItem]:
 732            var = self._parse_id_var()
 733            if not var:
 734                return None
 735
 736            value = None
 737            self._match(TokenType.ALIAS)
 738            if self._match(TokenType.TABLE):
 739                data_type = self._parse_schema()
 740            else:
 741                data_type = self._parse_types()
 742                if self._match(TokenType.EQ):
 743                    value = self._parse_bitwise()
 744
 745            return self.expression(exp.DeclareItem, this=var, kind=data_type, default=value)
 746
 747    class Generator(generator.Generator):
 748        LIMIT_IS_TOP = True
 749        QUERY_HINTS = False
 750        RETURNING_END = False
 751        NVL2_SUPPORTED = False
 752        ALTER_TABLE_INCLUDE_COLUMN_KEYWORD = False
 753        LIMIT_FETCH = "FETCH"
 754        COMPUTED_COLUMN_WITH_TYPE = False
 755        CTE_RECURSIVE_KEYWORD_REQUIRED = False
 756        ENSURE_BOOLS = True
 757        NULL_ORDERING_SUPPORTED = None
 758        SUPPORTS_SINGLE_ARG_CONCAT = False
 759        TABLESAMPLE_SEED_KEYWORD = "REPEATABLE"
 760        SUPPORTS_SELECT_INTO = True
 761        JSON_PATH_BRACKETED_KEY_SUPPORTED = False
 762        SUPPORTS_TO_NUMBER = False
 763        SET_OP_MODIFIERS = False
 764        COPY_PARAMS_EQ_REQUIRED = True
 765
 766        EXPRESSIONS_WITHOUT_NESTED_CTES = {
 767            exp.Delete,
 768            exp.Insert,
 769            exp.Intersect,
 770            exp.Except,
 771            exp.Merge,
 772            exp.Select,
 773            exp.Subquery,
 774            exp.Union,
 775            exp.Update,
 776        }
 777
 778        SUPPORTED_JSON_PATH_PARTS = {
 779            exp.JSONPathKey,
 780            exp.JSONPathRoot,
 781            exp.JSONPathSubscript,
 782        }
 783
 784        TYPE_MAPPING = {
 785            **generator.Generator.TYPE_MAPPING,
 786            exp.DataType.Type.BOOLEAN: "BIT",
 787            exp.DataType.Type.DECIMAL: "NUMERIC",
 788            exp.DataType.Type.DATETIME: "DATETIME2",
 789            exp.DataType.Type.DOUBLE: "FLOAT",
 790            exp.DataType.Type.INT: "INTEGER",
 791            exp.DataType.Type.ROWVERSION: "ROWVERSION",
 792            exp.DataType.Type.TEXT: "VARCHAR(MAX)",
 793            exp.DataType.Type.TIMESTAMP: "DATETIME2",
 794            exp.DataType.Type.TIMESTAMPTZ: "DATETIMEOFFSET",
 795            exp.DataType.Type.UTINYINT: "TINYINT",
 796            exp.DataType.Type.VARIANT: "SQL_VARIANT",
 797        }
 798
 799        TYPE_MAPPING.pop(exp.DataType.Type.NCHAR)
 800        TYPE_MAPPING.pop(exp.DataType.Type.NVARCHAR)
 801
 802        TRANSFORMS = {
 803            **generator.Generator.TRANSFORMS,
 804            exp.AnyValue: any_value_to_max_sql,
 805            exp.ArrayToString: rename_func("STRING_AGG"),
 806            exp.AutoIncrementColumnConstraint: lambda *_: "IDENTITY",
 807            exp.DateAdd: date_delta_sql("DATEADD"),
 808            exp.DateDiff: date_delta_sql("DATEDIFF"),
 809            exp.CTE: transforms.preprocess([qualify_derived_table_outputs]),
 810            exp.CurrentDate: rename_func("GETDATE"),
 811            exp.CurrentTimestamp: rename_func("GETDATE"),
 812            exp.DateStrToDate: datestrtodate_sql,
 813            exp.Extract: rename_func("DATEPART"),
 814            exp.GeneratedAsIdentityColumnConstraint: generatedasidentitycolumnconstraint_sql,
 815            exp.GroupConcat: _string_agg_sql,
 816            exp.If: rename_func("IIF"),
 817            exp.JSONExtract: _json_extract_sql,
 818            exp.JSONExtractScalar: _json_extract_sql,
 819            exp.LastDay: lambda self, e: self.func("EOMONTH", e.this),
 820            exp.Max: max_or_greatest,
 821            exp.MD5: lambda self, e: self.func("HASHBYTES", exp.Literal.string("MD5"), e.this),
 822            exp.Min: min_or_least,
 823            exp.NumberToStr: _format_sql,
 824            exp.ParseJSON: lambda self, e: self.sql(e, "this"),
 825            exp.Repeat: rename_func("REPLICATE"),
 826            exp.Select: transforms.preprocess(
 827                [
 828                    transforms.eliminate_distinct_on,
 829                    transforms.eliminate_semi_and_anti_joins,
 830                    transforms.eliminate_qualify,
 831                ]
 832            ),
 833            exp.StrPosition: lambda self, e: self.func(
 834                "CHARINDEX", e.args.get("substr"), e.this, e.args.get("position")
 835            ),
 836            exp.Subquery: transforms.preprocess([qualify_derived_table_outputs]),
 837            exp.SHA: lambda self, e: self.func("HASHBYTES", exp.Literal.string("SHA1"), e.this),
 838            exp.SHA2: lambda self, e: self.func(
 839                "HASHBYTES", exp.Literal.string(f"SHA2_{e.args.get('length', 256)}"), e.this
 840            ),
 841            exp.TemporaryProperty: lambda self, e: "",
 842            exp.TimeStrToTime: lambda self, e: self.sql(
 843                exp.cast(e.this, exp.DataType.Type.DATETIME)
 844            ),
 845            exp.TimeToStr: _format_sql,
 846            exp.Trim: trim_sql,
 847            exp.TsOrDsAdd: date_delta_sql("DATEADD", cast=True),
 848            exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
 849        }
 850
 851        TRANSFORMS.pop(exp.ReturnsProperty)
 852
 853        PROPERTIES_LOCATION = {
 854            **generator.Generator.PROPERTIES_LOCATION,
 855            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
 856        }
 857
 858        def select_sql(self, expression: exp.Select) -> str:
 859            if expression.args.get("offset"):
 860                if not expression.args.get("order"):
 861                    # ORDER BY is required in order to use OFFSET in a query, so we use
 862                    # a noop order by, since we don't really care about the order.
 863                    # See: https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
 864                    expression.order_by(exp.select(exp.null()).subquery(), copy=False)
 865
 866                limit = expression.args.get("limit")
 867                if isinstance(limit, exp.Limit):
 868                    # TOP and OFFSET can't be combined, we need use FETCH instead of TOP
 869                    # we replace here because otherwise TOP would be generated in select_sql
 870                    limit.replace(exp.Fetch(direction="FIRST", count=limit.expression))
 871
 872            return super().select_sql(expression)
 873
 874        def convert_sql(self, expression: exp.Convert) -> str:
 875            name = "TRY_CONVERT" if expression.args.get("safe") else "CONVERT"
 876            return self.func(
 877                name, expression.this, expression.expression, expression.args.get("style")
 878            )
 879
 880        def queryoption_sql(self, expression: exp.QueryOption) -> str:
 881            option = self.sql(expression, "this")
 882            value = self.sql(expression, "expression")
 883            if value:
 884                optional_equal_sign = "= " if option in OPTIONS_THAT_REQUIRE_EQUAL else ""
 885                return f"{option} {optional_equal_sign}{value}"
 886            return option
 887
 888        def lateral_op(self, expression: exp.Lateral) -> str:
 889            cross_apply = expression.args.get("cross_apply")
 890            if cross_apply is True:
 891                return "CROSS APPLY"
 892            if cross_apply is False:
 893                return "OUTER APPLY"
 894
 895            # TODO: perhaps we can check if the parent is a Join and transpile it appropriately
 896            self.unsupported("LATERAL clause is not supported.")
 897            return "LATERAL"
 898
 899        def timefromparts_sql(self, expression: exp.TimeFromParts) -> str:
 900            nano = expression.args.get("nano")
 901            if nano is not None:
 902                nano.pop()
 903                self.unsupported("Specifying nanoseconds is not supported in TIMEFROMPARTS.")
 904
 905            if expression.args.get("fractions") is None:
 906                expression.set("fractions", exp.Literal.number(0))
 907            if expression.args.get("precision") is None:
 908                expression.set("precision", exp.Literal.number(0))
 909
 910            return rename_func("TIMEFROMPARTS")(self, expression)
 911
 912        def timestampfromparts_sql(self, expression: exp.TimestampFromParts) -> str:
 913            zone = expression.args.get("zone")
 914            if zone is not None:
 915                zone.pop()
 916                self.unsupported("Time zone is not supported in DATETIMEFROMPARTS.")
 917
 918            nano = expression.args.get("nano")
 919            if nano is not None:
 920                nano.pop()
 921                self.unsupported("Specifying nanoseconds is not supported in DATETIMEFROMPARTS.")
 922
 923            if expression.args.get("milli") is None:
 924                expression.set("milli", exp.Literal.number(0))
 925
 926            return rename_func("DATETIMEFROMPARTS")(self, expression)
 927
 928        def setitem_sql(self, expression: exp.SetItem) -> str:
 929            this = expression.this
 930            if isinstance(this, exp.EQ) and not isinstance(this.left, exp.Parameter):
 931                # T-SQL does not use '=' in SET command, except when the LHS is a variable.
 932                return f"{self.sql(this.left)} {self.sql(this.right)}"
 933
 934            return super().setitem_sql(expression)
 935
 936        def boolean_sql(self, expression: exp.Boolean) -> str:
 937            if type(expression.parent) in BIT_TYPES:
 938                return "1" if expression.this else "0"
 939
 940            return "(1 = 1)" if expression.this else "(1 = 0)"
 941
 942        def is_sql(self, expression: exp.Is) -> str:
 943            if isinstance(expression.expression, exp.Boolean):
 944                return self.binary(expression, "=")
 945            return self.binary(expression, "IS")
 946
 947        def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
 948            sql = self.sql(expression, "this")
 949            properties = expression.args.get("properties")
 950
 951            if sql[:1] != "#" and any(
 952                isinstance(prop, exp.TemporaryProperty)
 953                for prop in (properties.expressions if properties else [])
 954            ):
 955                sql = f"[#{sql[1:]}" if sql.startswith("[") else f"#{sql}"
 956
 957            return sql
 958
 959        def create_sql(self, expression: exp.Create) -> str:
 960            kind = expression.kind
 961            exists = expression.args.pop("exists", None)
 962
 963            if kind == "VIEW":
 964                expression.this.set("catalog", None)
 965
 966            sql = super().create_sql(expression)
 967
 968            like_property = expression.find(exp.LikeProperty)
 969            if like_property:
 970                ctas_expression = like_property.this
 971            else:
 972                ctas_expression = expression.expression
 973
 974            table = expression.find(exp.Table)
 975
 976            # Convert CTAS statement to SELECT .. INTO ..
 977            if kind == "TABLE" and ctas_expression:
 978                ctas_with = ctas_expression.args.get("with")
 979                if ctas_with:
 980                    ctas_with = ctas_with.pop()
 981
 982                if isinstance(ctas_expression, exp.UNWRAPPED_QUERIES):
 983                    ctas_expression = ctas_expression.subquery()
 984
 985                select_into = exp.select("*").from_(exp.alias_(ctas_expression, "temp", table=True))
 986                select_into.set("into", exp.Into(this=table))
 987                select_into.set("with", ctas_with)
 988
 989                if like_property:
 990                    select_into.limit(0, copy=False)
 991
 992                sql = self.sql(select_into)
 993
 994            if exists:
 995                identifier = self.sql(exp.Literal.string(exp.table_name(table) if table else ""))
 996                sql = self.sql(exp.Literal.string(sql))
 997                if kind == "SCHEMA":
 998                    sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE schema_name = {identifier}) EXEC({sql})"""
 999                elif kind == "TABLE":
1000                    assert table
1001                    where = exp.and_(
1002                        exp.column("table_name").eq(table.name),
1003                        exp.column("table_schema").eq(table.db) if table.db else None,
1004                        exp.column("table_catalog").eq(table.catalog) if table.catalog else None,
1005                    )
1006                    sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE {where}) EXEC({sql})"""
1007                elif kind == "INDEX":
1008                    index = self.sql(exp.Literal.string(expression.this.text("this")))
1009                    sql = f"""IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id({identifier}) AND name = {index}) EXEC({sql})"""
1010            elif expression.args.get("replace"):
1011                sql = sql.replace("CREATE OR REPLACE ", "CREATE OR ALTER ", 1)
1012
1013            return self.prepend_ctes(expression, sql)
1014
1015        def offset_sql(self, expression: exp.Offset) -> str:
1016            return f"{super().offset_sql(expression)} ROWS"
1017
1018        def version_sql(self, expression: exp.Version) -> str:
1019            name = "SYSTEM_TIME" if expression.name == "TIMESTAMP" else expression.name
1020            this = f"FOR {name}"
1021            expr = expression.expression
1022            kind = expression.text("kind")
1023            if kind in ("FROM", "BETWEEN"):
1024                args = expr.expressions
1025                sep = "TO" if kind == "FROM" else "AND"
1026                expr_sql = f"{self.sql(seq_get(args, 0))} {sep} {self.sql(seq_get(args, 1))}"
1027            else:
1028                expr_sql = self.sql(expr)
1029
1030            expr_sql = f" {expr_sql}" if expr_sql else ""
1031            return f"{this} {kind}{expr_sql}"
1032
1033        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
1034            table = expression.args.get("table")
1035            table = f"{table} " if table else ""
1036            return f"RETURNS {table}{self.sql(expression, 'this')}"
1037
1038        def returning_sql(self, expression: exp.Returning) -> str:
1039            into = self.sql(expression, "into")
1040            into = self.seg(f"INTO {into}") if into else ""
1041            return f"{self.seg('OUTPUT')} {self.expressions(expression, flat=True)}{into}"
1042
1043        def transaction_sql(self, expression: exp.Transaction) -> str:
1044            this = self.sql(expression, "this")
1045            this = f" {this}" if this else ""
1046            mark = self.sql(expression, "mark")
1047            mark = f" WITH MARK {mark}" if mark else ""
1048            return f"BEGIN TRANSACTION{this}{mark}"
1049
1050        def commit_sql(self, expression: exp.Commit) -> str:
1051            this = self.sql(expression, "this")
1052            this = f" {this}" if this else ""
1053            durability = expression.args.get("durability")
1054            durability = (
1055                f" WITH (DELAYED_DURABILITY = {'ON' if durability else 'OFF'})"
1056                if durability is not None
1057                else ""
1058            )
1059            return f"COMMIT TRANSACTION{this}{durability}"
1060
1061        def rollback_sql(self, expression: exp.Rollback) -> str:
1062            this = self.sql(expression, "this")
1063            this = f" {this}" if this else ""
1064            return f"ROLLBACK TRANSACTION{this}"
1065
1066        def identifier_sql(self, expression: exp.Identifier) -> str:
1067            identifier = super().identifier_sql(expression)
1068
1069            if expression.args.get("global"):
1070                identifier = f"##{identifier}"
1071            elif expression.args.get("temporary"):
1072                identifier = f"#{identifier}"
1073
1074            return identifier
1075
1076        def constraint_sql(self, expression: exp.Constraint) -> str:
1077            this = self.sql(expression, "this")
1078            expressions = self.expressions(expression, flat=True, sep=" ")
1079            return f"CONSTRAINT {this} {expressions}"
1080
1081        def length_sql(self, expression: exp.Length) -> str:
1082            return self._uncast_text(expression, "LEN")
1083
1084        def right_sql(self, expression: exp.Right) -> str:
1085            return self._uncast_text(expression, "RIGHT")
1086
1087        def left_sql(self, expression: exp.Left) -> str:
1088            return self._uncast_text(expression, "LEFT")
1089
1090        def _uncast_text(self, expression: exp.Expression, name: str) -> str:
1091            this = expression.this
1092            if isinstance(this, exp.Cast) and this.is_type(exp.DataType.Type.TEXT):
1093                this_sql = self.sql(this, "this")
1094            else:
1095                this_sql = self.sql(this)
1096            expression_sql = self.sql(expression, "expression")
1097            return self.func(name, this_sql, expression_sql if expression_sql else None)
1098
1099        def partition_sql(self, expression: exp.Partition) -> str:
1100            return f"WITH (PARTITIONS({self.expressions(expression, flat=True)}))"
1101
1102        def altertable_sql(self, expression: exp.AlterTable) -> str:
1103            action = seq_get(expression.args.get("actions") or [], 0)
1104            if isinstance(action, exp.RenameTable):
1105                return f"EXEC sp_rename '{self.sql(expression.this)}', '{action.this.name}'"
1106            return super().altertable_sql(expression)
1107
1108        def drop_sql(self, expression: exp.Drop) -> str:
1109            if expression.args["kind"] == "VIEW":
1110                expression.this.set("catalog", None)
1111            return super().drop_sql(expression)
1112
1113        def declare_sql(self, expression: exp.Declare) -> str:
1114            return f"DECLARE {self.expressions(expression, flat=True)}"
1115
1116        def declareitem_sql(self, expression: exp.DeclareItem) -> str:
1117            variable = self.sql(expression, "this")
1118            default = self.sql(expression, "default")
1119            default = f" = {default}" if default else ""
1120
1121            kind = self.sql(expression, "kind")
1122            if isinstance(expression.args.get("kind"), exp.Schema):
1123                kind = f"TABLE {kind}"
1124
1125            return f"{variable} AS {kind}{default}"
1126
1127        def options_modifier(self, expression: exp.Expression) -> str:
1128            options = self.expressions(expression, key="options")
1129            return f" OPTION{self.wrap(options)}" if options else ""
NORMALIZATION_STRATEGY = <NormalizationStrategy.CASE_INSENSITIVE: 'CASE_INSENSITIVE'>

Specifies the strategy according to which identifiers should be normalized.

TIME_FORMAT = "'yyyy-mm-dd hh:mm:ss'"
SUPPORTS_SEMI_ANTI_JOIN = False

Whether SEMI or ANTI joins are supported.

LOG_BASE_FIRST: Optional[bool] = False

Whether the base comes first in the LOG function. Possible values: True, False, None (two arguments are not supported by LOG)

TYPED_DIVISION = True

Whether the behavior of a / b depends on the types of a and b. False means a / b is always float division. True means a / b is integer division if both a and b are integers.

CONCAT_COALESCE = True

A NULL arg in CONCAT yields NULL by default, but in some dialects it yields an empty string.

TIME_MAPPING: Dict[str, str] = {'year': '%Y', 'dayofyear': '%j', 'day': '%d', 'dy': '%d', 'y': '%Y', 'week': '%W', 'ww': '%W', 'wk': '%W', 'hour': '%h', 'hh': '%I', 'minute': '%M', 'mi': '%M', 'n': '%M', 'second': '%S', 'ss': '%S', 's': '%-S', 'millisecond': '%f', 'ms': '%f', 'weekday': '%W', 'dw': '%W', 'month': '%m', 'mm': '%M', 'm': '%-M', 'Y': '%Y', 'YYYY': '%Y', 'YY': '%y', 'MMMM': '%B', 'MMM': '%b', 'MM': '%m', 'M': '%-m', 'dddd': '%A', 'dd': '%d', 'd': '%-d', 'HH': '%H', 'H': '%-H', 'h': '%-I', 'S': '%f', 'yyyy': '%Y', 'yy': '%y'}

Associates this dialect's time formats with their equivalent Python strftime formats.

CONVERT_FORMAT_MAPPING = {'0': '%b %d %Y %-I:%M%p', '1': '%m/%d/%y', '2': '%y.%m.%d', '3': '%d/%m/%y', '4': '%d.%m.%y', '5': '%d-%m-%y', '6': '%d %b %y', '7': '%b %d, %y', '8': '%H:%M:%S', '9': '%b %d %Y %-I:%M:%S:%f%p', '10': 'mm-dd-yy', '11': 'yy/mm/dd', '12': 'yymmdd', '13': '%d %b %Y %H:%M:ss:%f', '14': '%H:%M:%S:%f', '20': '%Y-%m-%d %H:%M:%S', '21': '%Y-%m-%d %H:%M:%S.%f', '22': '%m/%d/%y %-I:%M:%S %p', '23': '%Y-%m-%d', '24': '%H:%M:%S', '25': '%Y-%m-%d %H:%M:%S.%f', '100': '%b %d %Y %-I:%M%p', '101': '%m/%d/%Y', '102': '%Y.%m.%d', '103': '%d/%m/%Y', '104': '%d.%m.%Y', '105': '%d-%m-%Y', '106': '%d %b %Y', '107': '%b %d, %Y', '108': '%H:%M:%S', '109': '%b %d %Y %-I:%M:%S:%f%p', '110': '%m-%d-%Y', '111': '%Y/%m/%d', '112': '%Y%m%d', '113': '%d %b %Y %H:%M:%S:%f', '114': '%H:%M:%S:%f', '120': '%Y-%m-%d %H:%M:%S', '121': '%Y-%m-%d %H:%M:%S.%f'}
FORMAT_TIME_MAPPING = {'y': '%B %Y', 'd': '%m/%d/%Y', 'H': '%-H', 'h': '%-I', 's': '%Y-%m-%d %H:%M:%S', 'D': '%A,%B,%Y', 'f': '%A,%B,%Y %-I:%M %p', 'F': '%A,%B,%Y %-I:%M:%S %p', 'g': '%m/%d/%Y %-I:%M %p', 'G': '%m/%d/%Y %-I:%M:%S %p', 'M': '%B %-d', 'm': '%B %-d', 'O': '%Y-%m-%dT%H:%M:%S', 'u': '%Y-%M-%D %H:%M:%S%z', 'U': '%A, %B %D, %Y %H:%M:%S%z', 'T': '%-I:%M:%S %p', 't': '%-I:%M', 'Y': '%a %Y'}
SUPPORTS_COLUMN_JOIN_MARKS = False

Whether the old-style outer join (+) syntax is supported.

tokenizer_class = <class 'TSQL.Tokenizer'>
jsonpath_tokenizer_class = <class 'sqlglot.tokens.JSONPathTokenizer'>
parser_class = <class 'TSQL.Parser'>
generator_class = <class 'TSQL.Generator'>
TIME_TRIE: Dict = {'y': {'e': {'a': {'r': {0: True}}}, 0: True, 'y': {'y': {'y': {0: True}}, 0: True}}, 'd': {'a': {'y': {'o': {'f': {'y': {'e': {'a': {'r': {0: True}}}}}}, 0: True}}, 'y': {0: True}, 'w': {0: True}, 'd': {'d': {'d': {0: True}}, 0: True}, 0: True}, 'w': {'e': {'e': {'k': {0: True, 'd': {'a': {'y': {0: True}}}}}}, 'w': {0: True}, 'k': {0: True}}, 'h': {'o': {'u': {'r': {0: True}}}, 'h': {0: True}, 0: True}, 'm': {'i': {'n': {'u': {'t': {'e': {0: True}}}}, 0: True, 'l': {'l': {'i': {'s': {'e': {'c': {'o': {'n': {'d': {0: True}}}}}}}}}}, 's': {0: True}, 'o': {'n': {'t': {'h': {0: True}}}}, 'm': {0: True}, 0: True}, 'n': {0: True}, 's': {'e': {'c': {'o': {'n': {'d': {0: True}}}}}, 's': {0: True}, 0: True}, 'Y': {0: True, 'Y': {'Y': {'Y': {0: True}}, 0: True}}, 'M': {'M': {'M': {'M': {0: True}, 0: True}, 0: True}, 0: True}, 'H': {'H': {0: True}, 0: True}, 'S': {0: True}}
FORMAT_TRIE: Dict = {'y': {'e': {'a': {'r': {0: True}}}, 0: True, 'y': {'y': {'y': {0: True}}, 0: True}}, 'd': {'a': {'y': {'o': {'f': {'y': {'e': {'a': {'r': {0: True}}}}}}, 0: True}}, 'y': {0: True}, 'w': {0: True}, 'd': {'d': {'d': {0: True}}, 0: True}, 0: True}, 'w': {'e': {'e': {'k': {0: True, 'd': {'a': {'y': {0: True}}}}}}, 'w': {0: True}, 'k': {0: True}}, 'h': {'o': {'u': {'r': {0: True}}}, 'h': {0: True}, 0: True}, 'm': {'i': {'n': {'u': {'t': {'e': {0: True}}}}, 0: True, 'l': {'l': {'i': {'s': {'e': {'c': {'o': {'n': {'d': {0: True}}}}}}}}}}, 's': {0: True}, 'o': {'n': {'t': {'h': {0: True}}}}, 'm': {0: True}, 0: True}, 'n': {0: True}, 's': {'e': {'c': {'o': {'n': {'d': {0: True}}}}}, 's': {0: True}, 0: True}, 'Y': {0: True, 'Y': {'Y': {'Y': {0: True}}, 0: True}}, 'M': {'M': {'M': {'M': {0: True}, 0: True}, 0: True}, 0: True}, 'H': {'H': {0: True}, 0: True}, 'S': {0: True}}
INVERSE_TIME_MAPPING: Dict[str, str] = {'%Y': 'yyyy', '%j': 'dayofyear', '%d': 'dd', '%W': 'dw', '%h': 'hour', '%I': 'hh', '%M': 'mm', '%S': 'ss', '%-S': 's', '%f': 'S', '%m': 'MM', '%-M': 'm', '%y': 'yy', '%B': 'MMMM', '%b': 'MMM', '%-m': 'M', '%A': 'dddd', '%-d': 'd', '%H': 'HH', '%-H': 'H', '%-I': 'h'}
INVERSE_TIME_TRIE: Dict = {'%': {'Y': {0: True}, 'j': {0: True}, 'd': {0: True}, 'W': {0: True}, 'h': {0: True}, 'I': {0: True}, 'M': {0: True}, 'S': {0: True}, '-': {'S': {0: True}, 'M': {0: True}, 'm': {0: True}, 'd': {0: True}, 'H': {0: True}, 'I': {0: True}}, 'f': {0: True}, 'm': {0: True}, 'y': {0: True}, 'B': {0: True}, 'b': {0: True}, 'A': {0: True}, 'H': {0: True}}}
ESCAPED_SEQUENCES: Dict[str, str] = {}
QUOTE_START = "'"
QUOTE_END = "'"
IDENTIFIER_START = '['
IDENTIFIER_END = ']'
BIT_START: Optional[str] = None
BIT_END: Optional[str] = None
HEX_START: Optional[str] = '0x'
HEX_END: Optional[str] = ''
BYTE_START: Optional[str] = None
BYTE_END: Optional[str] = None
UNICODE_START: Optional[str] = None
UNICODE_END: Optional[str] = None
class TSQL.Tokenizer(sqlglot.tokens.Tokenizer):
446    class Tokenizer(tokens.Tokenizer):
447        IDENTIFIERS = [("[", "]"), '"']
448        QUOTES = ["'", '"']
449        HEX_STRINGS = [("0x", ""), ("0X", "")]
450        VAR_SINGLE_TOKENS = {"@", "$", "#"}
451
452        KEYWORDS = {
453            **tokens.Tokenizer.KEYWORDS,
454            "CLUSTERED INDEX": TokenType.INDEX,
455            "DATETIME2": TokenType.DATETIME,
456            "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
457            "DECLARE": TokenType.DECLARE,
458            "EXEC": TokenType.COMMAND,
459            "FOR SYSTEM_TIME": TokenType.TIMESTAMP_SNAPSHOT,
460            "IMAGE": TokenType.IMAGE,
461            "MONEY": TokenType.MONEY,
462            "NONCLUSTERED INDEX": TokenType.INDEX,
463            "NTEXT": TokenType.TEXT,
464            "OPTION": TokenType.OPTION,
465            "OUTPUT": TokenType.RETURNING,
466            "PRINT": TokenType.COMMAND,
467            "PROC": TokenType.PROCEDURE,
468            "REAL": TokenType.FLOAT,
469            "ROWVERSION": TokenType.ROWVERSION,
470            "SMALLDATETIME": TokenType.DATETIME,
471            "SMALLMONEY": TokenType.SMALLMONEY,
472            "SQL_VARIANT": TokenType.VARIANT,
473            "SYSTEM_USER": TokenType.CURRENT_USER,
474            "TOP": TokenType.TOP,
475            "TIMESTAMP": TokenType.ROWVERSION,
476            "TINYINT": TokenType.UTINYINT,
477            "UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
478            "UPDATE STATISTICS": TokenType.COMMAND,
479            "XML": TokenType.XML,
480        }
481
482        COMMANDS = {*tokens.Tokenizer.COMMANDS, TokenType.END}
IDENTIFIERS = [('[', ']'), '"']
QUOTES = ["'", '"']
HEX_STRINGS = [('0x', ''), ('0X', '')]
VAR_SINGLE_TOKENS = {'#', '@', '$'}
KEYWORDS = {'{%': <TokenType.BLOCK_START: 'BLOCK_START'>, '{%+': <TokenType.BLOCK_START: 'BLOCK_START'>, '{%-': <TokenType.BLOCK_START: 'BLOCK_START'>, '%}': <TokenType.BLOCK_END: 'BLOCK_END'>, '+%}': <TokenType.BLOCK_END: 'BLOCK_END'>, '-%}': <TokenType.BLOCK_END: 'BLOCK_END'>, '{{+': <TokenType.BLOCK_START: 'BLOCK_START'>, '{{-': <TokenType.BLOCK_START: 'BLOCK_START'>, '+}}': <TokenType.BLOCK_END: 'BLOCK_END'>, '-}}': <TokenType.BLOCK_END: 'BLOCK_END'>, '/*+': <TokenType.HINT: 'HINT'>, '==': <TokenType.EQ: 'EQ'>, '::': <TokenType.DCOLON: 'DCOLON'>, '||': <TokenType.DPIPE: 'DPIPE'>, '>=': <TokenType.GTE: 'GTE'>, '<=': <TokenType.LTE: 'LTE'>, '<>': <TokenType.NEQ: 'NEQ'>, '!=': <TokenType.NEQ: 'NEQ'>, ':=': <TokenType.COLON_EQ: 'COLON_EQ'>, '<=>': <TokenType.NULLSAFE_EQ: 'NULLSAFE_EQ'>, '->': <TokenType.ARROW: 'ARROW'>, '->>': <TokenType.DARROW: 'DARROW'>, '=>': <TokenType.FARROW: 'FARROW'>, '#>': <TokenType.HASH_ARROW: 'HASH_ARROW'>, '#>>': <TokenType.DHASH_ARROW: 'DHASH_ARROW'>, '<->': <TokenType.LR_ARROW: 'LR_ARROW'>, '&&': <TokenType.DAMP: 'DAMP'>, '??': <TokenType.DQMARK: 'DQMARK'>, 'ALL': <TokenType.ALL: 'ALL'>, 'ALWAYS': <TokenType.ALWAYS: 'ALWAYS'>, 'AND': <TokenType.AND: 'AND'>, 'ANTI': <TokenType.ANTI: 'ANTI'>, 'ANY': <TokenType.ANY: 'ANY'>, 'ASC': <TokenType.ASC: 'ASC'>, 'AS': <TokenType.ALIAS: 'ALIAS'>, 'ASOF': <TokenType.ASOF: 'ASOF'>, 'AUTOINCREMENT': <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, 'AUTO_INCREMENT': <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, 'BEGIN': <TokenType.BEGIN: 'BEGIN'>, 'BETWEEN': <TokenType.BETWEEN: 'BETWEEN'>, 'CACHE': <TokenType.CACHE: 'CACHE'>, 'UNCACHE': <TokenType.UNCACHE: 'UNCACHE'>, 'CASE': <TokenType.CASE: 'CASE'>, 'CHARACTER SET': <TokenType.CHARACTER_SET: 'CHARACTER_SET'>, 'CLUSTER BY': <TokenType.CLUSTER_BY: 'CLUSTER_BY'>, 'COLLATE': <TokenType.COLLATE: 'COLLATE'>, 'COLUMN': <TokenType.COLUMN: 'COLUMN'>, 'COMMIT': <TokenType.COMMIT: 'COMMIT'>, 'CONNECT BY': <TokenType.CONNECT_BY: 'CONNECT_BY'>, 'CONSTRAINT': <TokenType.CONSTRAINT: 'CONSTRAINT'>, 'COPY': <TokenType.COPY: 'COPY'>, 'CREATE': <TokenType.CREATE: 'CREATE'>, 'CROSS': <TokenType.CROSS: 'CROSS'>, 'CUBE': <TokenType.CUBE: 'CUBE'>, 'CURRENT_DATE': <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, 'CURRENT_TIME': <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, 'CURRENT_TIMESTAMP': <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>, 'CURRENT_USER': <TokenType.CURRENT_USER: 'CURRENT_USER'>, 'DATABASE': <TokenType.DATABASE: 'DATABASE'>, 'DEFAULT': <TokenType.DEFAULT: 'DEFAULT'>, 'DELETE': <TokenType.DELETE: 'DELETE'>, 'DESC': <TokenType.DESC: 'DESC'>, 'DESCRIBE': <TokenType.DESCRIBE: 'DESCRIBE'>, 'DISTINCT': <TokenType.DISTINCT: 'DISTINCT'>, 'DISTRIBUTE BY': <TokenType.DISTRIBUTE_BY: 'DISTRIBUTE_BY'>, 'DIV': <TokenType.DIV: 'DIV'>, 'DROP': <TokenType.DROP: 'DROP'>, 'ELSE': <TokenType.ELSE: 'ELSE'>, 'END': <TokenType.END: 'END'>, 'ENUM': <TokenType.ENUM: 'ENUM'>, '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'>, 'FIRST': <TokenType.FIRST: 'FIRST'>, 'FULL': <TokenType.FULL: 'FULL'>, 'FUNCTION': <TokenType.FUNCTION: 'FUNCTION'>, 'FOR': <TokenType.FOR: 'FOR'>, 'FOREIGN KEY': <TokenType.FOREIGN_KEY: 'FOREIGN_KEY'>, 'FORMAT': <TokenType.FORMAT: 'FORMAT'>, 'FROM': <TokenType.FROM: 'FROM'>, 'GEOGRAPHY': <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, 'GEOMETRY': <TokenType.GEOMETRY: 'GEOMETRY'>, 'GLOB': <TokenType.GLOB: 'GLOB'>, 'GROUP BY': <TokenType.GROUP_BY: 'GROUP_BY'>, 'GROUPING SETS': <TokenType.GROUPING_SETS: 'GROUPING_SETS'>, 'HAVING': <TokenType.HAVING: 'HAVING'>, 'ILIKE': <TokenType.ILIKE: 'ILIKE'>, 'IN': <TokenType.IN: 'IN'>, 'INDEX': <TokenType.INDEX: 'INDEX'>, 'INET': <TokenType.INET: 'INET'>, 'INNER': <TokenType.INNER: 'INNER'>, 'INSERT': <TokenType.INSERT: 'INSERT'>, 'INTERVAL': <TokenType.INTERVAL: 'INTERVAL'>, 'INTERSECT': <TokenType.INTERSECT: 'INTERSECT'>, 'INTO': <TokenType.INTO: 'INTO'>, 'IS': <TokenType.IS: 'IS'>, 'ISNULL': <TokenType.ISNULL: 'ISNULL'>, 'JOIN': <TokenType.JOIN: 'JOIN'>, 'KEEP': <TokenType.KEEP: 'KEEP'>, 'KILL': <TokenType.KILL: 'KILL'>, 'LATERAL': <TokenType.LATERAL: 'LATERAL'>, 'LEFT': <TokenType.LEFT: 'LEFT'>, 'LIKE': <TokenType.LIKE: 'LIKE'>, 'LIMIT': <TokenType.LIMIT: 'LIMIT'>, 'LOAD': <TokenType.LOAD: 'LOAD'>, 'LOCK': <TokenType.LOCK: 'LOCK'>, 'MERGE': <TokenType.MERGE: 'MERGE'>, 'NATURAL': <TokenType.NATURAL: 'NATURAL'>, 'NEXT': <TokenType.NEXT: 'NEXT'>, 'NOT': <TokenType.NOT: 'NOT'>, 'NOTNULL': <TokenType.NOTNULL: 'NOTNULL'>, 'NULL': <TokenType.NULL: 'NULL'>, 'OBJECT': <TokenType.OBJECT: 'OBJECT'>, 'OFFSET': <TokenType.OFFSET: 'OFFSET'>, 'ON': <TokenType.ON: 'ON'>, 'OR': <TokenType.OR: 'OR'>, 'XOR': <TokenType.XOR: 'XOR'>, 'ORDER BY': <TokenType.ORDER_BY: 'ORDER_BY'>, 'ORDINALITY': <TokenType.ORDINALITY: 'ORDINALITY'>, 'OUTER': <TokenType.OUTER: 'OUTER'>, 'OVER': <TokenType.OVER: 'OVER'>, 'OVERLAPS': <TokenType.OVERLAPS: 'OVERLAPS'>, 'OVERWRITE': <TokenType.OVERWRITE: 'OVERWRITE'>, 'PARTITION': <TokenType.PARTITION: 'PARTITION'>, 'PARTITION BY': <TokenType.PARTITION_BY: 'PARTITION_BY'>, 'PARTITIONED BY': <TokenType.PARTITION_BY: 'PARTITION_BY'>, 'PARTITIONED_BY': <TokenType.PARTITION_BY: 'PARTITION_BY'>, 'PERCENT': <TokenType.PERCENT: 'PERCENT'>, 'PIVOT': <TokenType.PIVOT: 'PIVOT'>, 'PRAGMA': <TokenType.PRAGMA: 'PRAGMA'>, 'PRIMARY KEY': <TokenType.PRIMARY_KEY: 'PRIMARY_KEY'>, 'PROCEDURE': <TokenType.PROCEDURE: 'PROCEDURE'>, 'QUALIFY': <TokenType.QUALIFY: 'QUALIFY'>, 'RANGE': <TokenType.RANGE: 'RANGE'>, 'RECURSIVE': <TokenType.RECURSIVE: 'RECURSIVE'>, 'REGEXP': <TokenType.RLIKE: 'RLIKE'>, 'REPLACE': <TokenType.REPLACE: 'REPLACE'>, 'RETURNING': <TokenType.RETURNING: 'RETURNING'>, 'REFERENCES': <TokenType.REFERENCES: 'REFERENCES'>, 'RIGHT': <TokenType.RIGHT: 'RIGHT'>, 'RLIKE': <TokenType.RLIKE: 'RLIKE'>, 'ROLLBACK': <TokenType.ROLLBACK: 'ROLLBACK'>, 'ROLLUP': <TokenType.ROLLUP: 'ROLLUP'>, 'ROW': <TokenType.ROW: 'ROW'>, 'ROWS': <TokenType.ROWS: 'ROWS'>, 'SCHEMA': <TokenType.SCHEMA: 'SCHEMA'>, 'SELECT': <TokenType.SELECT: 'SELECT'>, 'SEMI': <TokenType.SEMI: 'SEMI'>, 'SET': <TokenType.SET: 'SET'>, 'SETTINGS': <TokenType.SETTINGS: 'SETTINGS'>, 'SHOW': <TokenType.SHOW: 'SHOW'>, 'SIMILAR TO': <TokenType.SIMILAR_TO: 'SIMILAR_TO'>, 'SOME': <TokenType.SOME: 'SOME'>, 'SORT BY': <TokenType.SORT_BY: 'SORT_BY'>, 'START WITH': <TokenType.START_WITH: 'START_WITH'>, 'STRAIGHT_JOIN': <TokenType.STRAIGHT_JOIN: 'STRAIGHT_JOIN'>, 'TABLE': <TokenType.TABLE: 'TABLE'>, 'TABLESAMPLE': <TokenType.TABLE_SAMPLE: 'TABLE_SAMPLE'>, 'TEMP': <TokenType.TEMPORARY: 'TEMPORARY'>, 'TEMPORARY': <TokenType.TEMPORARY: 'TEMPORARY'>, 'THEN': <TokenType.THEN: 'THEN'>, 'TRUE': <TokenType.TRUE: 'TRUE'>, 'TRUNCATE': <TokenType.TRUNCATE: 'TRUNCATE'>, 'UNION': <TokenType.UNION: 'UNION'>, 'UNKNOWN': <TokenType.UNKNOWN: 'UNKNOWN'>, 'UNNEST': <TokenType.UNNEST: 'UNNEST'>, 'UNPIVOT': <TokenType.UNPIVOT: 'UNPIVOT'>, 'UPDATE': <TokenType.UPDATE: 'UPDATE'>, 'USE': <TokenType.USE: 'USE'>, 'USING': <TokenType.USING: 'USING'>, 'UUID': <TokenType.UUID: 'UUID'>, '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'>, 'APPLY': <TokenType.APPLY: 'APPLY'>, 'ARRAY': <TokenType.ARRAY: 'ARRAY'>, 'BIT': <TokenType.BIT: 'BIT'>, 'BOOL': <TokenType.BOOLEAN: 'BOOLEAN'>, 'BOOLEAN': <TokenType.BOOLEAN: 'BOOLEAN'>, 'BYTE': <TokenType.TINYINT: 'TINYINT'>, 'MEDIUMINT': <TokenType.MEDIUMINT: 'MEDIUMINT'>, 'INT1': <TokenType.TINYINT: 'TINYINT'>, 'TINYINT': <TokenType.UTINYINT: 'UTINYINT'>, 'INT16': <TokenType.SMALLINT: 'SMALLINT'>, 'SHORT': <TokenType.SMALLINT: 'SMALLINT'>, 'SMALLINT': <TokenType.SMALLINT: 'SMALLINT'>, 'INT128': <TokenType.INT128: 'INT128'>, 'HUGEINT': <TokenType.INT128: 'INT128'>, 'INT2': <TokenType.SMALLINT: 'SMALLINT'>, 'INTEGER': <TokenType.INT: 'INT'>, 'INT': <TokenType.INT: 'INT'>, 'INT4': <TokenType.INT: 'INT'>, 'INT32': <TokenType.INT: 'INT'>, 'INT64': <TokenType.BIGINT: 'BIGINT'>, 'LONG': <TokenType.BIGINT: 'BIGINT'>, 'BIGINT': <TokenType.BIGINT: 'BIGINT'>, 'INT8': <TokenType.TINYINT: 'TINYINT'>, 'UINT': <TokenType.UINT: 'UINT'>, 'DEC': <TokenType.DECIMAL: 'DECIMAL'>, 'DECIMAL': <TokenType.DECIMAL: 'DECIMAL'>, 'BIGDECIMAL': <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, 'BIGNUMERIC': <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, 'LIST': <TokenType.LIST: 'LIST'>, 'MAP': <TokenType.MAP: 'MAP'>, 'NULLABLE': <TokenType.NULLABLE: 'NULLABLE'>, 'NUMBER': <TokenType.DECIMAL: 'DECIMAL'>, 'NUMERIC': <TokenType.DECIMAL: 'DECIMAL'>, 'FIXED': <TokenType.DECIMAL: 'DECIMAL'>, 'REAL': <TokenType.FLOAT: 'FLOAT'>, 'FLOAT': <TokenType.FLOAT: 'FLOAT'>, 'FLOAT4': <TokenType.FLOAT: 'FLOAT'>, 'FLOAT8': <TokenType.DOUBLE: 'DOUBLE'>, 'DOUBLE': <TokenType.DOUBLE: 'DOUBLE'>, 'DOUBLE PRECISION': <TokenType.DOUBLE: 'DOUBLE'>, 'JSON': <TokenType.JSON: 'JSON'>, 'JSONB': <TokenType.JSONB: 'JSONB'>, 'CHAR': <TokenType.CHAR: 'CHAR'>, 'CHARACTER': <TokenType.CHAR: 'CHAR'>, 'NCHAR': <TokenType.NCHAR: 'NCHAR'>, 'VARCHAR': <TokenType.VARCHAR: 'VARCHAR'>, 'VARCHAR2': <TokenType.VARCHAR: 'VARCHAR'>, 'NVARCHAR': <TokenType.NVARCHAR: 'NVARCHAR'>, 'NVARCHAR2': <TokenType.NVARCHAR: 'NVARCHAR'>, 'BPCHAR': <TokenType.BPCHAR: 'BPCHAR'>, 'STR': <TokenType.TEXT: 'TEXT'>, 'STRING': <TokenType.TEXT: 'TEXT'>, 'TEXT': <TokenType.TEXT: 'TEXT'>, 'LONGTEXT': <TokenType.LONGTEXT: 'LONGTEXT'>, 'MEDIUMTEXT': <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, 'TINYTEXT': <TokenType.TINYTEXT: 'TINYTEXT'>, 'CLOB': <TokenType.TEXT: 'TEXT'>, 'LONGVARCHAR': <TokenType.TEXT: 'TEXT'>, 'BINARY': <TokenType.BINARY: 'BINARY'>, 'BLOB': <TokenType.VARBINARY: 'VARBINARY'>, 'LONGBLOB': <TokenType.LONGBLOB: 'LONGBLOB'>, 'MEDIUMBLOB': <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, 'TINYBLOB': <TokenType.TINYBLOB: 'TINYBLOB'>, 'BYTEA': <TokenType.VARBINARY: 'VARBINARY'>, 'VARBINARY': <TokenType.VARBINARY: 'VARBINARY'>, 'TIME': <TokenType.TIME: 'TIME'>, 'TIMETZ': <TokenType.TIMETZ: 'TIMETZ'>, 'TIMESTAMP': <TokenType.ROWVERSION: 'ROWVERSION'>, 'TIMESTAMPTZ': <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, 'TIMESTAMPLTZ': <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, 'TIMESTAMP_LTZ': <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, 'TIMESTAMPNTZ': <TokenType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, 'TIMESTAMP_NTZ': <TokenType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, 'DATE': <TokenType.DATE: 'DATE'>, 'DATETIME': <TokenType.DATETIME: 'DATETIME'>, 'INT4RANGE': <TokenType.INT4RANGE: 'INT4RANGE'>, 'INT4MULTIRANGE': <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, 'INT8RANGE': <TokenType.INT8RANGE: 'INT8RANGE'>, 'INT8MULTIRANGE': <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, 'NUMRANGE': <TokenType.NUMRANGE: 'NUMRANGE'>, 'NUMMULTIRANGE': <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, 'TSRANGE': <TokenType.TSRANGE: 'TSRANGE'>, 'TSMULTIRANGE': <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, 'TSTZRANGE': <TokenType.TSTZRANGE: 'TSTZRANGE'>, 'TSTZMULTIRANGE': <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, 'DATERANGE': <TokenType.DATERANGE: 'DATERANGE'>, 'DATEMULTIRANGE': <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, 'UNIQUE': <TokenType.UNIQUE: 'UNIQUE'>, 'STRUCT': <TokenType.STRUCT: 'STRUCT'>, 'SEQUENCE': <TokenType.SEQUENCE: 'SEQUENCE'>, 'VARIANT': <TokenType.VARIANT: 'VARIANT'>, 'ALTER': <TokenType.ALTER: 'ALTER'>, 'ANALYZE': <TokenType.COMMAND: 'COMMAND'>, 'CALL': <TokenType.COMMAND: 'COMMAND'>, 'COMMENT': <TokenType.COMMENT: 'COMMENT'>, 'EXPLAIN': <TokenType.COMMAND: 'COMMAND'>, 'GRANT': <TokenType.COMMAND: 'COMMAND'>, 'OPTIMIZE': <TokenType.COMMAND: 'COMMAND'>, 'PREPARE': <TokenType.COMMAND: 'COMMAND'>, 'VACUUM': <TokenType.COMMAND: 'COMMAND'>, 'USER-DEFINED': <TokenType.USERDEFINED: 'USERDEFINED'>, 'FOR VERSION': <TokenType.VERSION_SNAPSHOT: 'VERSION_SNAPSHOT'>, 'FOR TIMESTAMP': <TokenType.TIMESTAMP_SNAPSHOT: 'TIMESTAMP_SNAPSHOT'>, 'CLUSTERED INDEX': <TokenType.INDEX: 'INDEX'>, 'DATETIME2': <TokenType.DATETIME: 'DATETIME'>, 'DATETIMEOFFSET': <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, 'DECLARE': <TokenType.DECLARE: 'DECLARE'>, 'EXEC': <TokenType.COMMAND: 'COMMAND'>, 'FOR SYSTEM_TIME': <TokenType.TIMESTAMP_SNAPSHOT: 'TIMESTAMP_SNAPSHOT'>, 'IMAGE': <TokenType.IMAGE: 'IMAGE'>, 'MONEY': <TokenType.MONEY: 'MONEY'>, 'NONCLUSTERED INDEX': <TokenType.INDEX: 'INDEX'>, 'NTEXT': <TokenType.TEXT: 'TEXT'>, 'OPTION': <TokenType.OPTION: 'OPTION'>, 'OUTPUT': <TokenType.RETURNING: 'RETURNING'>, 'PRINT': <TokenType.COMMAND: 'COMMAND'>, 'PROC': <TokenType.PROCEDURE: 'PROCEDURE'>, 'ROWVERSION': <TokenType.ROWVERSION: 'ROWVERSION'>, 'SMALLDATETIME': <TokenType.DATETIME: 'DATETIME'>, 'SMALLMONEY': <TokenType.SMALLMONEY: 'SMALLMONEY'>, 'SQL_VARIANT': <TokenType.VARIANT: 'VARIANT'>, 'SYSTEM_USER': <TokenType.CURRENT_USER: 'CURRENT_USER'>, 'TOP': <TokenType.TOP: 'TOP'>, 'UNIQUEIDENTIFIER': <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>, 'UPDATE STATISTICS': <TokenType.COMMAND: 'COMMAND'>, 'XML': <TokenType.XML: 'XML'>}
COMMANDS = {<TokenType.FETCH: 'FETCH'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.END: 'END'>, <TokenType.EXECUTE: 'EXECUTE'>, <TokenType.SHOW: 'SHOW'>}
class TSQL.Parser(sqlglot.parser.Parser):
484    class Parser(parser.Parser):
485        SET_REQUIRES_ASSIGNMENT_DELIMITER = False
486        LOG_DEFAULTS_TO_LN = True
487        ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False
488        STRING_ALIASES = True
489        NO_PAREN_IF_COMMANDS = False
490
491        QUERY_MODIFIER_PARSERS = {
492            **parser.Parser.QUERY_MODIFIER_PARSERS,
493            TokenType.OPTION: lambda self: ("options", self._parse_options()),
494        }
495
496        FUNCTIONS = {
497            **parser.Parser.FUNCTIONS,
498            "CHARINDEX": lambda args: exp.StrPosition(
499                this=seq_get(args, 1),
500                substr=seq_get(args, 0),
501                position=seq_get(args, 2),
502            ),
503            "DATEADD": build_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL),
504            "DATEDIFF": _build_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL),
505            "DATENAME": _build_formatted_time(exp.TimeToStr, full_format_mapping=True),
506            "DATEPART": _build_formatted_time(exp.TimeToStr),
507            "DATETIMEFROMPARTS": _build_datetimefromparts,
508            "EOMONTH": _build_eomonth,
509            "FORMAT": _build_format,
510            "GETDATE": exp.CurrentTimestamp.from_arg_list,
511            "HASHBYTES": _build_hashbytes,
512            "ISNULL": exp.Coalesce.from_arg_list,
513            "JSON_QUERY": parser.build_extract_json_with_path(exp.JSONExtract),
514            "JSON_VALUE": parser.build_extract_json_with_path(exp.JSONExtractScalar),
515            "LEN": _build_with_arg_as_text(exp.Length),
516            "LEFT": _build_with_arg_as_text(exp.Left),
517            "RIGHT": _build_with_arg_as_text(exp.Right),
518            "REPLICATE": exp.Repeat.from_arg_list,
519            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
520            "SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
521            "SUSER_NAME": exp.CurrentUser.from_arg_list,
522            "SUSER_SNAME": exp.CurrentUser.from_arg_list,
523            "SYSTEM_USER": exp.CurrentUser.from_arg_list,
524            "TIMEFROMPARTS": _build_timefromparts,
525        }
526
527        JOIN_HINTS = {"LOOP", "HASH", "MERGE", "REMOTE"}
528
529        RETURNS_TABLE_TOKENS = parser.Parser.ID_VAR_TOKENS - {
530            TokenType.TABLE,
531            *parser.Parser.TYPE_TOKENS,
532        }
533
534        STATEMENT_PARSERS = {
535            **parser.Parser.STATEMENT_PARSERS,
536            TokenType.DECLARE: lambda self: self._parse_declare(),
537        }
538
539        def _parse_options(self) -> t.Optional[t.List[exp.Expression]]:
540            if not self._match(TokenType.OPTION):
541                return None
542
543            def _parse_option() -> t.Optional[exp.Expression]:
544                option = self._parse_var_from_options(OPTIONS)
545                if not option:
546                    return None
547
548                self._match(TokenType.EQ)
549                return self.expression(
550                    exp.QueryOption, this=option, expression=self._parse_primary_or_var()
551                )
552
553            return self._parse_wrapped_csv(_parse_option)
554
555        def _parse_projections(self) -> t.List[exp.Expression]:
556            """
557            T-SQL supports the syntax alias = expression in the SELECT's projection list,
558            so we transform all parsed Selects to convert their EQ projections into Aliases.
559
560            See: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-clause-transact-sql?view=sql-server-ver16#syntax
561            """
562            return [
563                (
564                    exp.alias_(projection.expression, projection.this.this, copy=False)
565                    if isinstance(projection, exp.EQ) and isinstance(projection.this, exp.Column)
566                    else projection
567                )
568                for projection in super()._parse_projections()
569            ]
570
571        def _parse_commit_or_rollback(self) -> exp.Commit | exp.Rollback:
572            """Applies to SQL Server and Azure SQL Database
573            COMMIT [ { TRAN | TRANSACTION }
574                [ transaction_name | @tran_name_variable ] ]
575                [ WITH ( DELAYED_DURABILITY = { OFF | ON } ) ]
576
577            ROLLBACK { TRAN | TRANSACTION }
578                [ transaction_name | @tran_name_variable
579                | savepoint_name | @savepoint_variable ]
580            """
581            rollback = self._prev.token_type == TokenType.ROLLBACK
582
583            self._match_texts(("TRAN", "TRANSACTION"))
584            this = self._parse_id_var()
585
586            if rollback:
587                return self.expression(exp.Rollback, this=this)
588
589            durability = None
590            if self._match_pair(TokenType.WITH, TokenType.L_PAREN):
591                self._match_text_seq("DELAYED_DURABILITY")
592                self._match(TokenType.EQ)
593
594                if self._match_text_seq("OFF"):
595                    durability = False
596                else:
597                    self._match(TokenType.ON)
598                    durability = True
599
600                self._match_r_paren()
601
602            return self.expression(exp.Commit, this=this, durability=durability)
603
604        def _parse_transaction(self) -> exp.Transaction | exp.Command:
605            """Applies to SQL Server and Azure SQL Database
606            BEGIN { TRAN | TRANSACTION }
607            [ { transaction_name | @tran_name_variable }
608            [ WITH MARK [ 'description' ] ]
609            ]
610            """
611            if self._match_texts(("TRAN", "TRANSACTION")):
612                transaction = self.expression(exp.Transaction, this=self._parse_id_var())
613                if self._match_text_seq("WITH", "MARK"):
614                    transaction.set("mark", self._parse_string())
615
616                return transaction
617
618            return self._parse_as_command(self._prev)
619
620        def _parse_returns(self) -> exp.ReturnsProperty:
621            table = self._parse_id_var(any_token=False, tokens=self.RETURNS_TABLE_TOKENS)
622            returns = super()._parse_returns()
623            returns.set("table", table)
624            return returns
625
626        def _parse_convert(
627            self, strict: bool, safe: t.Optional[bool] = None
628        ) -> t.Optional[exp.Expression]:
629            this = self._parse_types()
630            self._match(TokenType.COMMA)
631            args = [this, *self._parse_csv(self._parse_assignment)]
632            convert = exp.Convert.from_arg_list(args)
633            convert.set("safe", safe)
634            convert.set("strict", strict)
635            return convert
636
637        def _parse_user_defined_function(
638            self, kind: t.Optional[TokenType] = None
639        ) -> t.Optional[exp.Expression]:
640            this = super()._parse_user_defined_function(kind=kind)
641
642            if (
643                kind == TokenType.FUNCTION
644                or isinstance(this, exp.UserDefinedFunction)
645                or self._match(TokenType.ALIAS, advance=False)
646            ):
647                return this
648
649            expressions = self._parse_csv(self._parse_function_parameter)
650            return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)
651
652        def _parse_id_var(
653            self,
654            any_token: bool = True,
655            tokens: t.Optional[t.Collection[TokenType]] = None,
656        ) -> t.Optional[exp.Expression]:
657            is_temporary = self._match(TokenType.HASH)
658            is_global = is_temporary and self._match(TokenType.HASH)
659
660            this = super()._parse_id_var(any_token=any_token, tokens=tokens)
661            if this:
662                if is_global:
663                    this.set("global", True)
664                elif is_temporary:
665                    this.set("temporary", True)
666
667            return this
668
669        def _parse_create(self) -> exp.Create | exp.Command:
670            create = super()._parse_create()
671
672            if isinstance(create, exp.Create):
673                table = create.this.this if isinstance(create.this, exp.Schema) else create.this
674                if isinstance(table, exp.Table) and table.this.args.get("temporary"):
675                    if not create.args.get("properties"):
676                        create.set("properties", exp.Properties(expressions=[]))
677
678                    create.args["properties"].append("expressions", exp.TemporaryProperty())
679
680            return create
681
682        def _parse_if(self) -> t.Optional[exp.Expression]:
683            index = self._index
684
685            if self._match_text_seq("OBJECT_ID"):
686                self._parse_wrapped_csv(self._parse_string)
687                if self._match_text_seq("IS", "NOT", "NULL") and self._match(TokenType.DROP):
688                    return self._parse_drop(exists=True)
689                self._retreat(index)
690
691            return super()._parse_if()
692
693        def _parse_unique(self) -> exp.UniqueColumnConstraint:
694            if self._match_texts(("CLUSTERED", "NONCLUSTERED")):
695                this = self.CONSTRAINT_PARSERS[self._prev.text.upper()](self)
696            else:
697                this = self._parse_schema(self._parse_id_var(any_token=False))
698
699            return self.expression(exp.UniqueColumnConstraint, this=this)
700
701        def _parse_partition(self) -> t.Optional[exp.Partition]:
702            if not self._match_text_seq("WITH", "(", "PARTITIONS"):
703                return None
704
705            def parse_range():
706                low = self._parse_bitwise()
707                high = self._parse_bitwise() if self._match_text_seq("TO") else None
708
709                return (
710                    self.expression(exp.PartitionRange, this=low, expression=high) if high else low
711                )
712
713            partition = self.expression(
714                exp.Partition, expressions=self._parse_wrapped_csv(parse_range)
715            )
716
717            self._match_r_paren()
718
719            return partition
720
721        def _parse_declare(self) -> exp.Declare | exp.Command:
722            index = self._index
723            expressions = self._try_parse(partial(self._parse_csv, self._parse_declareitem))
724
725            if not expressions or self._curr:
726                self._retreat(index)
727                return self._parse_as_command(self._prev)
728
729            return self.expression(exp.Declare, expressions=expressions)
730
731        def _parse_declareitem(self) -> t.Optional[exp.DeclareItem]:
732            var = self._parse_id_var()
733            if not var:
734                return None
735
736            value = None
737            self._match(TokenType.ALIAS)
738            if self._match(TokenType.TABLE):
739                data_type = self._parse_schema()
740            else:
741                data_type = self._parse_types()
742                if self._match(TokenType.EQ):
743                    value = self._parse_bitwise()
744
745            return self.expression(exp.DeclareItem, this=var, kind=data_type, default=value)

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

Arguments:
  • error_level: The desired error level. Default: ErrorLevel.IMMEDIATE
  • error_message_context: The amount of context to capture from a query string when displaying the error message (in number of characters). Default: 100
  • max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3
SET_REQUIRES_ASSIGNMENT_DELIMITER = False
LOG_DEFAULTS_TO_LN = True
ALTER_TABLE_ADD_REQUIRED_FOR_EACH_COLUMN = False
STRING_ALIASES = True
NO_PAREN_IF_COMMANDS = False
QUERY_MODIFIER_PARSERS = {<TokenType.MATCH_RECOGNIZE: 'MATCH_RECOGNIZE'>: <function Parser.<lambda>>, <TokenType.PREWHERE: 'PREWHERE'>: <function Parser.<lambda>>, <TokenType.WHERE: 'WHERE'>: <function Parser.<lambda>>, <TokenType.GROUP_BY: 'GROUP_BY'>: <function Parser.<lambda>>, <TokenType.HAVING: 'HAVING'>: <function Parser.<lambda>>, <TokenType.QUALIFY: 'QUALIFY'>: <function Parser.<lambda>>, <TokenType.WINDOW: 'WINDOW'>: <function Parser.<lambda>>, <TokenType.ORDER_BY: 'ORDER_BY'>: <function Parser.<lambda>>, <TokenType.LIMIT: 'LIMIT'>: <function Parser.<lambda>>, <TokenType.FETCH: 'FETCH'>: <function Parser.<lambda>>, <TokenType.OFFSET: 'OFFSET'>: <function Parser.<lambda>>, <TokenType.FOR: 'FOR'>: <function Parser.<lambda>>, <TokenType.LOCK: 'LOCK'>: <function Parser.<lambda>>, <TokenType.TABLE_SAMPLE: 'TABLE_SAMPLE'>: <function Parser.<lambda>>, <TokenType.USING: 'USING'>: <function Parser.<lambda>>, <TokenType.CLUSTER_BY: 'CLUSTER_BY'>: <function Parser.<lambda>>, <TokenType.DISTRIBUTE_BY: 'DISTRIBUTE_BY'>: <function Parser.<lambda>>, <TokenType.SORT_BY: 'SORT_BY'>: <function Parser.<lambda>>, <TokenType.CONNECT_BY: 'CONNECT_BY'>: <function Parser.<lambda>>, <TokenType.START_WITH: 'START_WITH'>: <function Parser.<lambda>>, <TokenType.OPTION: 'OPTION'>: <function TSQL.Parser.<lambda>>}
FUNCTIONS = {'ABS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Abs'>>, 'ADD_MONTHS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.AddMonths'>>, 'ANONYMOUS_AGG_FUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.AnonymousAggFunc'>>, 'ANY_VALUE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.AnyValue'>>, 'APPROX_DISTINCT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxDistinct'>>, 'APPROX_COUNT_DISTINCT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxDistinct'>>, 'APPROX_QUANTILE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxQuantile'>>, 'APPROX_TOP_K': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ApproxTopK'>>, 'ARG_MAX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArgMax'>>, 'ARGMAX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArgMax'>>, 'MAX_BY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArgMax'>>, 'ARG_MIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArgMin'>>, 'ARGMIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArgMin'>>, 'MIN_BY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArgMin'>>, 'ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Array'>>, 'ARRAY_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayAgg'>>, 'ARRAY_ALL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayAll'>>, 'ARRAY_ANY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayAny'>>, 'ARRAY_CONCAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayConcat'>>, 'ARRAY_CAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayConcat'>>, 'ARRAY_CONSTRUCT_COMPACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayConstructCompact'>>, 'ARRAY_CONTAINS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayContains'>>, 'ARRAY_HAS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayContains'>>, 'ARRAY_CONTAINS_ALL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayContainsAll'>>, 'ARRAY_HAS_ALL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayContainsAll'>>, 'FILTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayFilter'>>, 'ARRAY_FILTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayFilter'>>, 'ARRAY_OVERLAPS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayOverlaps'>>, 'ARRAY_SIZE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySize'>>, 'ARRAY_LENGTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySize'>>, 'ARRAY_SORT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySort'>>, 'ARRAY_SUM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArraySum'>>, 'ARRAY_TO_STRING': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayToString'>>, 'ARRAY_JOIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayToString'>>, 'ARRAY_UNION_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayUnionAgg'>>, 'ARRAY_UNIQUE_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ArrayUniqueAgg'>>, 'AVG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Avg'>>, 'CASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Case'>>, 'CAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Cast'>>, 'CAST_TO_STR_TYPE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CastToStrType'>>, 'CBRT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Cbrt'>>, 'CEIL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Ceil'>>, 'CEILING': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Ceil'>>, 'CHR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Chr'>>, 'CHAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Chr'>>, 'COALESCE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'IFNULL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'NVL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'COLLATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Collate'>>, 'COMBINED_AGG_FUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CombinedAggFunc'>>, 'COMBINED_PARAMETERIZED_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CombinedParameterizedAgg'>>, 'CONCAT': <function Parser.<lambda>>, 'CONCAT_WS': <function Parser.<lambda>>, 'CONNECT_BY_ROOT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ConnectByRoot'>>, 'CONVERT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Convert'>>, 'CORR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Corr'>>, 'COUNT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Count'>>, 'COUNT_IF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CountIf'>>, 'COUNTIF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CountIf'>>, 'COVAR_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CovarPop'>>, 'COVAR_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CovarSamp'>>, 'CURRENT_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentDate'>>, 'CURRENT_DATETIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentDatetime'>>, 'CURRENT_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentTime'>>, 'CURRENT_TIMESTAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentTimestamp'>>, 'CURRENT_USER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentUser'>>, 'DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Date'>>, 'DATE_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateAdd'>>, 'DATEDIFF': <function _build_date_delta.<locals>._builder>, 'DATE_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateDiff'>>, 'DATE_FROM_PARTS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateFromParts'>>, 'DATEFROMPARTS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateFromParts'>>, 'DATE_STR_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateStrToDate'>>, 'DATE_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateSub'>>, 'DATE_TO_DATE_STR': <function Parser.<lambda>>, 'DATE_TO_DI': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateToDi'>>, 'DATE_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DateTrunc'>>, 'DATETIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Datetime'>>, 'DATETIME_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeAdd'>>, 'DATETIME_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeDiff'>>, 'DATETIME_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeSub'>>, 'DATETIME_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DatetimeTrunc'>>, 'DAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Day'>>, 'DAY_OF_MONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfMonth'>>, 'DAYOFMONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfMonth'>>, 'DAY_OF_WEEK': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfWeek'>>, 'DAYOFWEEK': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfWeek'>>, 'DAY_OF_YEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfYear'>>, 'DAYOFYEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DayOfYear'>>, 'DECODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Decode'>>, 'DI_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.DiToDate'>>, 'ENCODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Encode'>>, 'EXP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Exp'>>, 'EXPLODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Explode'>>, 'EXPLODE_OUTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ExplodeOuter'>>, 'EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Extract'>>, 'FIRST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.First'>>, 'FIRST_VALUE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.FirstValue'>>, 'FLATTEN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Flatten'>>, 'FLOOR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Floor'>>, 'FROM_BASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.FromBase'>>, 'FROM_BASE64': <bound method Func.from_arg_list of <class 'sqlglot.expressions.FromBase64'>>, 'GAP_FILL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.GapFill'>>, 'GENERATE_DATE_ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.GenerateDateArray'>>, 'GENERATE_SERIES': <bound method Func.from_arg_list of <class 'sqlglot.expressions.GenerateSeries'>>, 'GREATEST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Greatest'>>, 'GROUP_CONCAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.GroupConcat'>>, 'HEX': <function build_hex>, 'HLL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Hll'>>, 'IF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.If'>>, 'IIF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.If'>>, 'INITCAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Initcap'>>, 'IS_INF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.IsInf'>>, 'ISINF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.IsInf'>>, 'IS_NAN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.IsNan'>>, 'ISNAN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.IsNan'>>, 'J_S_O_N_ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONArray'>>, 'J_S_O_N_ARRAY_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONArrayAgg'>>, 'JSON_ARRAY_CONTAINS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONArrayContains'>>, 'JSONB_CONTAINS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONBContains'>>, 'JSONB_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONBExtract'>>, 'JSONB_EXTRACT_SCALAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONBExtractScalar'>>, 'JSON_EXTRACT': <function build_extract_json_with_path.<locals>._builder>, 'JSON_EXTRACT_SCALAR': <function build_extract_json_with_path.<locals>._builder>, 'JSON_FORMAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONFormat'>>, 'J_S_O_N_OBJECT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONObject'>>, 'J_S_O_N_OBJECT_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONObjectAgg'>>, 'J_S_O_N_TABLE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.JSONTable'>>, 'LAG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Lag'>>, 'LAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Last'>>, 'LAST_DAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LastDay'>>, 'LAST_DAY_OF_MONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LastDay'>>, 'LAST_VALUE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LastValue'>>, 'LEAD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Lead'>>, 'LEAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Least'>>, 'LEFT': <function _build_with_arg_as_text.<locals>._parse>, 'LENGTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Length'>>, 'LEN': <function _build_with_arg_as_text.<locals>._parse>, 'LEVENSHTEIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Levenshtein'>>, 'LIST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.List'>>, 'LN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Ln'>>, 'LOG': <function build_logarithm>, 'LOGICAL_AND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalAnd'>>, 'BOOL_AND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalAnd'>>, 'BOOLAND_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalAnd'>>, 'LOGICAL_OR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalOr'>>, 'BOOL_OR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalOr'>>, 'BOOLOR_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LogicalOr'>>, 'LOWER': <function build_lower>, 'LCASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Lower'>>, 'LOWER_HEX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.LowerHex'>>, 'MD5': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MD5'>>, 'MD5_DIGEST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MD5Digest'>>, 'MAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Map'>>, 'MAP_FROM_ENTRIES': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MapFromEntries'>>, 'MATCH_AGAINST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MatchAgainst'>>, 'MAX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Max'>>, 'MIN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Min'>>, 'MONTH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Month'>>, 'MONTHS_BETWEEN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.MonthsBetween'>>, 'NEXT_VALUE_FOR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.NextValueFor'>>, 'NTH_VALUE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.NthValue'>>, 'NULLIF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Nullif'>>, 'NUMBER_TO_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.NumberToStr'>>, 'NVL2': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Nvl2'>>, 'OPEN_J_S_O_N': <bound method Func.from_arg_list of <class 'sqlglot.expressions.OpenJSON'>>, 'PARAMETERIZED_AGG': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ParameterizedAgg'>>, 'PARSE_JSON': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ParseJSON'>>, 'JSON_PARSE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ParseJSON'>>, 'PERCENTILE_CONT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.PercentileCont'>>, 'PERCENTILE_DISC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.PercentileDisc'>>, 'POSEXPLODE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Posexplode'>>, 'POSEXPLODE_OUTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.PosexplodeOuter'>>, 'POWER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Pow'>>, 'POW': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Pow'>>, 'PREDICT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Predict'>>, 'QUANTILE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Quantile'>>, 'QUARTER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Quarter'>>, 'RAND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Rand'>>, 'RANDOM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Rand'>>, 'RANDN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Randn'>>, 'RANGE_N': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RangeN'>>, 'READ_CSV': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ReadCSV'>>, 'REDUCE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Reduce'>>, 'REGEXP_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpExtract'>>, 'REGEXP_I_LIKE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpILike'>>, 'REGEXP_LIKE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpLike'>>, 'REGEXP_REPLACE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpReplace'>>, 'REGEXP_SPLIT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RegexpSplit'>>, 'REPEAT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Repeat'>>, 'RIGHT': <function _build_with_arg_as_text.<locals>._parse>, 'ROUND': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Round'>>, 'ROW_NUMBER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.RowNumber'>>, 'SHA': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SHA'>>, 'SHA1': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SHA'>>, 'SHA2': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SHA2'>>, 'SAFE_DIVIDE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SafeDivide'>>, 'SIGN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Sign'>>, 'SIGNUM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Sign'>>, 'SORT_ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.SortArray'>>, 'SPLIT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Split'>>, 'SQRT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Sqrt'>>, 'STANDARD_HASH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StandardHash'>>, 'STAR_MAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StarMap'>>, 'STARTS_WITH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StartsWith'>>, 'STARTSWITH': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StartsWith'>>, 'STDDEV': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Stddev'>>, 'STDDEV_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StddevPop'>>, 'STDDEV_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StddevSamp'>>, 'STR_POSITION': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrPosition'>>, 'STR_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrToDate'>>, 'STR_TO_MAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrToMap'>>, 'STR_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrToTime'>>, 'STR_TO_UNIX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StrToUnix'>>, 'STRING_TO_ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StringToArray'>>, 'SPLIT_BY_STRING': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StringToArray'>>, 'STRUCT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Struct'>>, 'STRUCT_EXTRACT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.StructExtract'>>, 'STUFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Stuff'>>, 'INSERT': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Stuff'>>, 'SUBSTRING': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Substring'>>, 'SUM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Sum'>>, 'TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Time'>>, 'TIME_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeAdd'>>, 'TIME_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeDiff'>>, 'TIME_FROM_PARTS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeFromParts'>>, 'TIMEFROMPARTS': <function _build_timefromparts>, 'TIME_STR_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeStrToDate'>>, 'TIME_STR_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeStrToTime'>>, 'TIME_STR_TO_UNIX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeStrToUnix'>>, 'TIME_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeSub'>>, 'TIME_TO_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeToStr'>>, 'TIME_TO_TIME_STR': <function Parser.<lambda>>, 'TIME_TO_UNIX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeToUnix'>>, 'TIME_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimeTrunc'>>, 'TIMESTAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Timestamp'>>, 'TIMESTAMP_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampAdd'>>, 'TIMESTAMPDIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampDiff'>>, 'TIMESTAMP_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampDiff'>>, 'TIMESTAMP_FROM_PARTS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampFromParts'>>, 'TIMESTAMPFROMPARTS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampFromParts'>>, 'TIMESTAMP_SUB': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampSub'>>, 'TIMESTAMP_TRUNC': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TimestampTrunc'>>, 'TO_ARRAY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToArray'>>, 'TO_BASE64': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToBase64'>>, 'TO_CHAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToChar'>>, 'TO_DAYS': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToDays'>>, 'TO_MAP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToMap'>>, 'TO_NUMBER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.ToNumber'>>, 'TRANSFORM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Transform'>>, 'TRIM': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Trim'>>, 'TRY': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Try'>>, 'TRY_CAST': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TryCast'>>, 'TS_OR_DI_TO_DI': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDiToDi'>>, 'TS_OR_DS_ADD': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsAdd'>>, 'TS_OR_DS_DIFF': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsDiff'>>, 'TS_OR_DS_TO_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsToDate'>>, 'TS_OR_DS_TO_DATE_STR': <function Parser.<lambda>>, 'TS_OR_DS_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsToTime'>>, 'TS_OR_DS_TO_TIMESTAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.TsOrDsToTimestamp'>>, 'UNHEX': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Unhex'>>, 'UNIX_DATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixDate'>>, 'UNIX_TO_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixToStr'>>, 'UNIX_TO_TIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixToTime'>>, 'UNIX_TO_TIME_STR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.UnixToTimeStr'>>, 'UNNEST': <function Parser.<lambda>>, 'UPPER': <function build_upper>, 'UCASE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Upper'>>, 'VAR_MAP': <function build_var_map>, 'VARIANCE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Variance'>>, 'VARIANCE_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Variance'>>, 'VAR_SAMP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Variance'>>, 'VARIANCE_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.VariancePop'>>, 'VAR_POP': <bound method Func.from_arg_list of <class 'sqlglot.expressions.VariancePop'>>, 'WEEK': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Week'>>, 'WEEK_OF_YEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.WeekOfYear'>>, 'WEEKOFYEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.WeekOfYear'>>, 'WHEN': <bound method Func.from_arg_list of <class 'sqlglot.expressions.When'>>, 'X_M_L_TABLE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.XMLTable'>>, 'XOR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Xor'>>, 'YEAR': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Year'>>, 'GLOB': <function Parser.<lambda>>, 'JSON_EXTRACT_PATH_TEXT': <function build_extract_json_with_path.<locals>._builder>, 'LIKE': <function build_like>, 'LOG2': <function Parser.<lambda>>, 'LOG10': <function Parser.<lambda>>, 'MOD': <function build_mod>, 'TO_HEX': <function build_hex>, 'CHARINDEX': <function TSQL.Parser.<lambda>>, 'DATEADD': <function build_date_delta.<locals>._builder>, 'DATENAME': <function _build_formatted_time.<locals>._builder>, 'DATEPART': <function _build_formatted_time.<locals>._builder>, 'DATETIMEFROMPARTS': <function _build_datetimefromparts>, 'EOMONTH': <function _build_eomonth>, 'FORMAT': <function _build_format>, 'GETDATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentTimestamp'>>, 'HASHBYTES': <function _build_hashbytes>, 'ISNULL': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Coalesce'>>, 'JSON_QUERY': <function build_extract_json_with_path.<locals>._builder>, 'JSON_VALUE': <function build_extract_json_with_path.<locals>._builder>, 'REPLICATE': <bound method Func.from_arg_list of <class 'sqlglot.expressions.Repeat'>>, 'SQUARE': <function TSQL.Parser.<lambda>>, 'SYSDATETIME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentTimestamp'>>, 'SUSER_NAME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentUser'>>, 'SUSER_SNAME': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentUser'>>, 'SYSTEM_USER': <bound method Func.from_arg_list of <class 'sqlglot.expressions.CurrentUser'>>}
JOIN_HINTS = {'MERGE', 'LOOP', 'HASH', 'REMOTE'}
RETURNS_TABLE_TOKENS = {<TokenType.COMMIT: 'COMMIT'>, <TokenType.CURRENT_USER: 'CURRENT_USER'>, <TokenType.DICTIONARY: 'DICTIONARY'>, <TokenType.LOAD: 'LOAD'>, <TokenType.EXISTS: 'EXISTS'>, <TokenType.ESCAPE: 'ESCAPE'>, <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.PERCENT: 'PERCENT'>, <TokenType.TRUNCATE: 'TRUNCATE'>, <TokenType.CONSTRAINT: 'CONSTRAINT'>, <TokenType.CACHE: 'CACHE'>, <TokenType.OFFSET: 'OFFSET'>, <TokenType.VIEW: 'VIEW'>, <TokenType.FALSE: 'FALSE'>, <TokenType.WINDOW: 'WINDOW'>, <TokenType.FIRST: 'FIRST'>, <TokenType.UNIQUE: 'UNIQUE'>, <TokenType.SCHEMA: 'SCHEMA'>, <TokenType.DESCRIBE: 'DESCRIBE'>, <TokenType.FILTER: 'FILTER'>, <TokenType.USE: 'USE'>, <TokenType.STORAGE_INTEGRATION: 'STORAGE_INTEGRATION'>, <TokenType.CASE: 'CASE'>, <TokenType.DESC: 'DESC'>, <TokenType.CURRENT_DATETIME: 'CURRENT_DATETIME'>, <TokenType.BEGIN: 'BEGIN'>, <TokenType.KILL: 'KILL'>, <TokenType.OPERATOR: 'OPERATOR'>, <TokenType.FINAL: 'FINAL'>, <TokenType.REPLACE: 'REPLACE'>, <TokenType.DELETE: 'DELETE'>, <TokenType.VAR: 'VAR'>, <TokenType.UPDATE: 'UPDATE'>, <TokenType.ROW: 'ROW'>, <TokenType.TRUE: 'TRUE'>, <TokenType.PRAGMA: 'PRAGMA'>, <TokenType.WAREHOUSE: 'WAREHOUSE'>, <TokenType.OVERLAPS: 'OVERLAPS'>, <TokenType.ALL: 'ALL'>, <TokenType.ROWS: 'ROWS'>, <TokenType.NEXT: 'NEXT'>, <TokenType.ANY: 'ANY'>, <TokenType.OVERWRITE: 'OVERWRITE'>, <TokenType.REFRESH: 'REFRESH'>, <TokenType.STREAMLIT: 'STREAMLIT'>, <TokenType.PIVOT: 'PIVOT'>, <TokenType.TOP: 'TOP'>, <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, <TokenType.SEQUENCE: 'SEQUENCE'>, <TokenType.PROCEDURE: 'PROCEDURE'>, <TokenType.FOREIGN_KEY: 'FOREIGN_KEY'>, <TokenType.IDENTIFIER: 'IDENTIFIER'>, <TokenType.FULL: 'FULL'>, <TokenType.ANTI: 'ANTI'>, <TokenType.COPY: 'COPY'>, <TokenType.DIV: 'DIV'>, <TokenType.FUNCTION: 'FUNCTION'>, <TokenType.COLLATE: 'COLLATE'>, <TokenType.RANGE: 'RANGE'>, <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, <TokenType.ASC: 'ASC'>, <TokenType.ROLLUP: 'ROLLUP'>, <TokenType.EXECUTE: 'EXECUTE'>, <TokenType.ASOF: 'ASOF'>, <TokenType.DATABASE: 'DATABASE'>, <TokenType.REFERENCES: 'REFERENCES'>, <TokenType.UNNEST: 'UNNEST'>, <TokenType.MERGE: 'MERGE'>, <TokenType.SEMI: 'SEMI'>, <TokenType.RIGHT: 'RIGHT'>, <TokenType.COMMENT: 'COMMENT'>, <TokenType.KEEP: 'KEEP'>, <TokenType.NATURAL: 'NATURAL'>, <TokenType.TAG: 'TAG'>, <TokenType.SOME: 'SOME'>, <TokenType.MODEL: 'MODEL'>, <TokenType.RECURSIVE: 'RECURSIVE'>, <TokenType.PARTITION: 'PARTITION'>, <TokenType.APPLY: 'APPLY'>, <TokenType.ISNULL: 'ISNULL'>, <TokenType.SHOW: 'SHOW'>, <TokenType.UNPIVOT: 'UNPIVOT'>, <TokenType.SET: 'SET'>, <TokenType.COLUMN: 'COLUMN'>, <TokenType.END: 'END'>, <TokenType.FORMAT: 'FORMAT'>, <TokenType.LEFT: 'LEFT'>, <TokenType.ORDINALITY: 'ORDINALITY'>, <TokenType.INDEX: 'INDEX'>, <TokenType.DEFAULT: 'DEFAULT'>, <TokenType.IS: 'IS'>, <TokenType.TEMPORARY: 'TEMPORARY'>, <TokenType.VOLATILE: 'VOLATILE'>, <TokenType.SETTINGS: 'SETTINGS'>}
STATEMENT_PARSERS = {<TokenType.ALTER: 'ALTER'>: <function Parser.<lambda>>, <TokenType.BEGIN: 'BEGIN'>: <function Parser.<lambda>>, <TokenType.CACHE: 'CACHE'>: <function Parser.<lambda>>, <TokenType.COMMENT: 'COMMENT'>: <function Parser.<lambda>>, <TokenType.COMMIT: 'COMMIT'>: <function Parser.<lambda>>, <TokenType.COPY: 'COPY'>: <function Parser.<lambda>>, <TokenType.CREATE: 'CREATE'>: <function Parser.<lambda>>, <TokenType.DELETE: 'DELETE'>: <function Parser.<lambda>>, <TokenType.DESC: 'DESC'>: <function Parser.<lambda>>, <TokenType.DESCRIBE: 'DESCRIBE'>: <function Parser.<lambda>>, <TokenType.DROP: 'DROP'>: <function Parser.<lambda>>, <TokenType.INSERT: 'INSERT'>: <function Parser.<lambda>>, <TokenType.KILL: 'KILL'>: <function Parser.<lambda>>, <TokenType.LOAD: 'LOAD'>: <function Parser.<lambda>>, <TokenType.MERGE: 'MERGE'>: <function Parser.<lambda>>, <TokenType.PIVOT: 'PIVOT'>: <function Parser.<lambda>>, <TokenType.PRAGMA: 'PRAGMA'>: <function Parser.<lambda>>, <TokenType.REFRESH: 'REFRESH'>: <function Parser.<lambda>>, <TokenType.ROLLBACK: 'ROLLBACK'>: <function Parser.<lambda>>, <TokenType.SET: 'SET'>: <function Parser.<lambda>>, <TokenType.TRUNCATE: 'TRUNCATE'>: <function Parser.<lambda>>, <TokenType.UNCACHE: 'UNCACHE'>: <function Parser.<lambda>>, <TokenType.UPDATE: 'UPDATE'>: <function Parser.<lambda>>, <TokenType.USE: 'USE'>: <function Parser.<lambda>>, <TokenType.SEMICOLON: 'SEMICOLON'>: <function Parser.<lambda>>, <TokenType.DECLARE: 'DECLARE'>: <function TSQL.Parser.<lambda>>}
ID_VAR_TOKENS = {<TokenType.SERIAL: 'SERIAL'>, <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, <TokenType.MAP: 'MAP'>, <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, <TokenType.COMMIT: 'COMMIT'>, <TokenType.NESTED: 'NESTED'>, <TokenType.CURRENT_USER: 'CURRENT_USER'>, <TokenType.DICTIONARY: 'DICTIONARY'>, <TokenType.SMALLINT: 'SMALLINT'>, <TokenType.TABLE: 'TABLE'>, <TokenType.LOAD: 'LOAD'>, <TokenType.EXISTS: 'EXISTS'>, <TokenType.ESCAPE: 'ESCAPE'>, <TokenType.TDIGEST: 'TDIGEST'>, <TokenType.SIMPLEAGGREGATEFUNCTION: 'SIMPLEAGGREGATEFUNCTION'>, <TokenType.DECIMAL: 'DECIMAL'>, <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, <TokenType.BIT: 'BIT'>, <TokenType.OBJECT_IDENTIFIER: 'OBJECT_IDENTIFIER'>, <TokenType.DATE32: 'DATE32'>, <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, <TokenType.TIME: 'TIME'>, <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>, <TokenType.SMALLSERIAL: 'SMALLSERIAL'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.TINYBLOB: 'TINYBLOB'>, <TokenType.SUPER: 'SUPER'>, <TokenType.PERCENT: 'PERCENT'>, <TokenType.USMALLINT: 'USMALLINT'>, <TokenType.TRUNCATE: 'TRUNCATE'>, <TokenType.NCHAR: 'NCHAR'>, <TokenType.HLLSKETCH: 'HLLSKETCH'>, <TokenType.CONSTRAINT: 'CONSTRAINT'>, <TokenType.CACHE: 'CACHE'>, <TokenType.BIGSERIAL: 'BIGSERIAL'>, <TokenType.OFFSET: 'OFFSET'>, <TokenType.VIEW: 'VIEW'>, <TokenType.FALSE: 'FALSE'>, <TokenType.WINDOW: 'WINDOW'>, <TokenType.FIRST: 'FIRST'>, <TokenType.JSONB: 'JSONB'>, <TokenType.NULL: 'NULL'>, <TokenType.UINT: 'UINT'>, <TokenType.INET: 'INET'>, <TokenType.UNIQUE: 'UNIQUE'>, <TokenType.SCHEMA: 'SCHEMA'>, <TokenType.DESCRIBE: 'DESCRIBE'>, <TokenType.FILTER: 'FILTER'>, <TokenType.NULLABLE: 'NULLABLE'>, <TokenType.DATETIME: 'DATETIME'>, <TokenType.VARIANT: 'VARIANT'>, <TokenType.USE: 'USE'>, <TokenType.FIXEDSTRING: 'FIXEDSTRING'>, <TokenType.INT: 'INT'>, <TokenType.IPV6: 'IPV6'>, <TokenType.FLOAT: 'FLOAT'>, <TokenType.STORAGE_INTEGRATION: 'STORAGE_INTEGRATION'>, <TokenType.CASE: 'CASE'>, <TokenType.INT8RANGE: 'INT8RANGE'>, <TokenType.DESC: 'DESC'>, <TokenType.CURRENT_DATETIME: 'CURRENT_DATETIME'>, <TokenType.BEGIN: 'BEGIN'>, <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, <TokenType.KILL: 'KILL'>, <TokenType.TSRANGE: 'TSRANGE'>, <TokenType.IPPREFIX: 'IPPREFIX'>, <TokenType.TINYINT: 'TINYINT'>, <TokenType.STRUCT: 'STRUCT'>, <TokenType.OPERATOR: 'OPERATOR'>, <TokenType.FINAL: 'FINAL'>, <TokenType.REPLACE: 'REPLACE'>, <TokenType.ENUM8: 'ENUM8'>, <TokenType.INT4RANGE: 'INT4RANGE'>, <TokenType.BOOLEAN: 'BOOLEAN'>, <TokenType.DELETE: 'DELETE'>, <TokenType.VAR: 'VAR'>, <TokenType.NVARCHAR: 'NVARCHAR'>, <TokenType.TEXT: 'TEXT'>, <TokenType.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <TokenType.UPDATE: 'UPDATE'>, <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, <TokenType.ROW: 'ROW'>, <TokenType.NAME: 'NAME'>, <TokenType.UINT128: 'UINT128'>, <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>, <TokenType.TRUE: 'TRUE'>, <TokenType.PRAGMA: 'PRAGMA'>, <TokenType.WAREHOUSE: 'WAREHOUSE'>, <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, <TokenType.JSON: 'JSON'>, <TokenType.OVERLAPS: 'OVERLAPS'>, <TokenType.ALL: 'ALL'>, <TokenType.TIMETZ: 'TIMETZ'>, <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, <TokenType.UDECIMAL: 'UDECIMAL'>, <TokenType.ROWS: 'ROWS'>, <TokenType.XML: 'XML'>, <TokenType.UBIGINT: 'UBIGINT'>, <TokenType.PSEUDO_TYPE: 'PSEUDO_TYPE'>, <TokenType.NEXT: 'NEXT'>, <TokenType.UNKNOWN: 'UNKNOWN'>, <TokenType.ROWVERSION: 'ROWVERSION'>, <TokenType.ANY: 'ANY'>, <TokenType.MONEY: 'MONEY'>, <TokenType.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <TokenType.OVERWRITE: 'OVERWRITE'>, <TokenType.OBJECT: 'OBJECT'>, <TokenType.LONGTEXT: 'LONGTEXT'>, <TokenType.UINT256: 'UINT256'>, <TokenType.REFRESH: 'REFRESH'>, <TokenType.LIST: 'LIST'>, <TokenType.STREAMLIT: 'STREAMLIT'>, <TokenType.VARCHAR: 'VARCHAR'>, <TokenType.DATE: 'DATE'>, <TokenType.INT256: 'INT256'>, <TokenType.PIVOT: 'PIVOT'>, <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <TokenType.TOP: 'TOP'>, <TokenType.LOWCARDINALITY: 'LOWCARDINALITY'>, <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, <TokenType.SEQUENCE: 'SEQUENCE'>, <TokenType.PROCEDURE: 'PROCEDURE'>, <TokenType.FOREIGN_KEY: 'FOREIGN_KEY'>, <TokenType.ENUM: 'ENUM'>, <TokenType.IDENTIFIER: 'IDENTIFIER'>, <TokenType.FULL: 'FULL'>, <TokenType.MEDIUMINT: 'MEDIUMINT'>, <TokenType.IPADDRESS: 'IPADDRESS'>, <TokenType.ANTI: 'ANTI'>, <TokenType.COPY: 'COPY'>, <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, <TokenType.VARBINARY: 'VARBINARY'>, <TokenType.YEAR: 'YEAR'>, <TokenType.TSTZRANGE: 'TSTZRANGE'>, <TokenType.DIV: 'DIV'>, <TokenType.FUNCTION: 'FUNCTION'>, <TokenType.COLLATE: 'COLLATE'>, <TokenType.ENUM16: 'ENUM16'>, <TokenType.RANGE: 'RANGE'>, <TokenType.INT128: 'INT128'>, <TokenType.UTINYINT: 'UTINYINT'>, <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, <TokenType.ASC: 'ASC'>, <TokenType.ROLLUP: 'ROLLUP'>, <TokenType.DATERANGE: 'DATERANGE'>, <TokenType.EXECUTE: 'EXECUTE'>, <TokenType.ASOF: 'ASOF'>, <TokenType.DATABASE: 'DATABASE'>, <TokenType.REFERENCES: 'REFERENCES'>, <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, <TokenType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, <TokenType.UNNEST: 'UNNEST'>, <TokenType.DOUBLE: 'DOUBLE'>, <TokenType.TINYTEXT: 'TINYTEXT'>, <TokenType.MERGE: 'MERGE'>, <TokenType.TIMESTAMP_S: 'TIMESTAMP_S'>, <TokenType.USERDEFINED: 'USERDEFINED'>, <TokenType.SEMI: 'SEMI'>, <TokenType.IMAGE: 'IMAGE'>, <TokenType.RIGHT: 'RIGHT'>, <TokenType.COMMENT: 'COMMENT'>, <TokenType.KEEP: 'KEEP'>, <TokenType.HSTORE: 'HSTORE'>, <TokenType.AGGREGATEFUNCTION: 'AGGREGATEFUNCTION'>, <TokenType.NATURAL: 'NATURAL'>, <TokenType.TAG: 'TAG'>, <TokenType.SOME: 'SOME'>, <TokenType.LONGBLOB: 'LONGBLOB'>, <TokenType.MODEL: 'MODEL'>, <TokenType.GEOMETRY: 'GEOMETRY'>, <TokenType.BIGINT: 'BIGINT'>, <TokenType.TIMESTAMP: 'TIMESTAMP'>, <TokenType.SMALLMONEY: 'SMALLMONEY'>, <TokenType.IPV4: 'IPV4'>, <TokenType.RECURSIVE: 'RECURSIVE'>, <TokenType.PARTITION: 'PARTITION'>, <TokenType.APPLY: 'APPLY'>, <TokenType.BINARY: 'BINARY'>, <TokenType.ISNULL: 'ISNULL'>, <TokenType.SHOW: 'SHOW'>, <TokenType.UNPIVOT: 'UNPIVOT'>, <TokenType.SET: 'SET'>, <TokenType.STRAIGHT_JOIN: 'STRAIGHT_JOIN'>, <TokenType.BPCHAR: 'BPCHAR'>, <TokenType.NUMRANGE: 'NUMRANGE'>, <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, <TokenType.COLUMN: 'COLUMN'>, <TokenType.END: 'END'>, <TokenType.CHAR: 'CHAR'>, <TokenType.FORMAT: 'FORMAT'>, <TokenType.LEFT: 'LEFT'>, <TokenType.ORDINALITY: 'ORDINALITY'>, <TokenType.INDEX: 'INDEX'>, <TokenType.INTERVAL: 'INTERVAL'>, <TokenType.ARRAY: 'ARRAY'>, <TokenType.UUID: 'UUID'>, <TokenType.DEFAULT: 'DEFAULT'>, <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <TokenType.DATETIME64: 'DATETIME64'>, <TokenType.IS: 'IS'>, <TokenType.TEMPORARY: 'TEMPORARY'>, <TokenType.VOLATILE: 'VOLATILE'>, <TokenType.SETTINGS: 'SETTINGS'>, <TokenType.UMEDIUMINT: 'UMEDIUMINT'>}
TABLE_ALIAS_TOKENS = {<TokenType.SERIAL: 'SERIAL'>, <TokenType.TSMULTIRANGE: 'TSMULTIRANGE'>, <TokenType.MAP: 'MAP'>, <TokenType.GEOGRAPHY: 'GEOGRAPHY'>, <TokenType.COMMIT: 'COMMIT'>, <TokenType.NESTED: 'NESTED'>, <TokenType.CURRENT_USER: 'CURRENT_USER'>, <TokenType.DICTIONARY: 'DICTIONARY'>, <TokenType.SMALLINT: 'SMALLINT'>, <TokenType.TABLE: 'TABLE'>, <TokenType.LOAD: 'LOAD'>, <TokenType.EXISTS: 'EXISTS'>, <TokenType.ESCAPE: 'ESCAPE'>, <TokenType.TDIGEST: 'TDIGEST'>, <TokenType.SIMPLEAGGREGATEFUNCTION: 'SIMPLEAGGREGATEFUNCTION'>, <TokenType.DECIMAL: 'DECIMAL'>, <TokenType.INT4MULTIRANGE: 'INT4MULTIRANGE'>, <TokenType.BIT: 'BIT'>, <TokenType.OBJECT_IDENTIFIER: 'OBJECT_IDENTIFIER'>, <TokenType.DATE32: 'DATE32'>, <TokenType.CURRENT_TIME: 'CURRENT_TIME'>, <TokenType.TIME: 'TIME'>, <TokenType.CURRENT_TIMESTAMP: 'CURRENT_TIMESTAMP'>, <TokenType.SMALLSERIAL: 'SMALLSERIAL'>, <TokenType.COMMAND: 'COMMAND'>, <TokenType.TINYBLOB: 'TINYBLOB'>, <TokenType.SUPER: 'SUPER'>, <TokenType.PERCENT: 'PERCENT'>, <TokenType.USMALLINT: 'USMALLINT'>, <TokenType.TRUNCATE: 'TRUNCATE'>, <TokenType.NCHAR: 'NCHAR'>, <TokenType.HLLSKETCH: 'HLLSKETCH'>, <TokenType.CONSTRAINT: 'CONSTRAINT'>, <TokenType.CACHE: 'CACHE'>, <TokenType.BIGSERIAL: 'BIGSERIAL'>, <TokenType.VIEW: 'VIEW'>, <TokenType.FALSE: 'FALSE'>, <TokenType.JSONB: 'JSONB'>, <TokenType.FIRST: 'FIRST'>, <TokenType.NULL: 'NULL'>, <TokenType.UINT: 'UINT'>, <TokenType.INET: 'INET'>, <TokenType.UNIQUE: 'UNIQUE'>, <TokenType.SCHEMA: 'SCHEMA'>, <TokenType.DESCRIBE: 'DESCRIBE'>, <TokenType.FILTER: 'FILTER'>, <TokenType.NULLABLE: 'NULLABLE'>, <TokenType.DATETIME: 'DATETIME'>, <TokenType.VARIANT: 'VARIANT'>, <TokenType.USE: 'USE'>, <TokenType.FIXEDSTRING: 'FIXEDSTRING'>, <TokenType.INT: 'INT'>, <TokenType.IPV6: 'IPV6'>, <TokenType.FLOAT: 'FLOAT'>, <TokenType.STORAGE_INTEGRATION: 'STORAGE_INTEGRATION'>, <TokenType.CASE: 'CASE'>, <TokenType.INT8RANGE: 'INT8RANGE'>, <TokenType.DESC: 'DESC'>, <TokenType.CURRENT_DATETIME: 'CURRENT_DATETIME'>, <TokenType.BEGIN: 'BEGIN'>, <TokenType.INT8MULTIRANGE: 'INT8MULTIRANGE'>, <TokenType.KILL: 'KILL'>, <TokenType.TSRANGE: 'TSRANGE'>, <TokenType.IPPREFIX: 'IPPREFIX'>, <TokenType.TINYINT: 'TINYINT'>, <TokenType.STRUCT: 'STRUCT'>, <TokenType.OPERATOR: 'OPERATOR'>, <TokenType.FINAL: 'FINAL'>, <TokenType.REPLACE: 'REPLACE'>, <TokenType.ENUM8: 'ENUM8'>, <TokenType.INT4RANGE: 'INT4RANGE'>, <TokenType.BOOLEAN: 'BOOLEAN'>, <TokenType.DELETE: 'DELETE'>, <TokenType.VAR: 'VAR'>, <TokenType.NVARCHAR: 'NVARCHAR'>, <TokenType.TEXT: 'TEXT'>, <TokenType.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <TokenType.UPDATE: 'UPDATE'>, <TokenType.BIGDECIMAL: 'BIGDECIMAL'>, <TokenType.ROW: 'ROW'>, <TokenType.NAME: 'NAME'>, <TokenType.UINT128: 'UINT128'>, <TokenType.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>, <TokenType.TRUE: 'TRUE'>, <TokenType.PRAGMA: 'PRAGMA'>, <TokenType.WAREHOUSE: 'WAREHOUSE'>, <TokenType.NUMMULTIRANGE: 'NUMMULTIRANGE'>, <TokenType.JSON: 'JSON'>, <TokenType.OVERLAPS: 'OVERLAPS'>, <TokenType.ALL: 'ALL'>, <TokenType.TIMETZ: 'TIMETZ'>, <TokenType.MEDIUMTEXT: 'MEDIUMTEXT'>, <TokenType.UDECIMAL: 'UDECIMAL'>, <TokenType.ROWS: 'ROWS'>, <TokenType.XML: 'XML'>, <TokenType.UBIGINT: 'UBIGINT'>, <TokenType.PSEUDO_TYPE: 'PSEUDO_TYPE'>, <TokenType.NEXT: 'NEXT'>, <TokenType.UNKNOWN: 'UNKNOWN'>, <TokenType.ROWVERSION: 'ROWVERSION'>, <TokenType.ANY: 'ANY'>, <TokenType.MONEY: 'MONEY'>, <TokenType.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <TokenType.OVERWRITE: 'OVERWRITE'>, <TokenType.OBJECT: 'OBJECT'>, <TokenType.LONGTEXT: 'LONGTEXT'>, <TokenType.UINT256: 'UINT256'>, <TokenType.REFRESH: 'REFRESH'>, <TokenType.LIST: 'LIST'>, <TokenType.STREAMLIT: 'STREAMLIT'>, <TokenType.VARCHAR: 'VARCHAR'>, <TokenType.INT256: 'INT256'>, <TokenType.PIVOT: 'PIVOT'>, <TokenType.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <TokenType.TOP: 'TOP'>, <TokenType.LOWCARDINALITY: 'LOWCARDINALITY'>, <TokenType.CURRENT_DATE: 'CURRENT_DATE'>, <TokenType.SEQUENCE: 'SEQUENCE'>, <TokenType.PROCEDURE: 'PROCEDURE'>, <TokenType.FOREIGN_KEY: 'FOREIGN_KEY'>, <TokenType.ENUM: 'ENUM'>, <TokenType.IDENTIFIER: 'IDENTIFIER'>, <TokenType.MEDIUMINT: 'MEDIUMINT'>, <TokenType.IPADDRESS: 'IPADDRESS'>, <TokenType.ANTI: 'ANTI'>, <TokenType.COPY: 'COPY'>, <TokenType.DATEMULTIRANGE: 'DATEMULTIRANGE'>, <TokenType.VARBINARY: 'VARBINARY'>, <TokenType.YEAR: 'YEAR'>, <TokenType.TSTZRANGE: 'TSTZRANGE'>, <TokenType.DIV: 'DIV'>, <TokenType.FUNCTION: 'FUNCTION'>, <TokenType.COLLATE: 'COLLATE'>, <TokenType.ENUM16: 'ENUM16'>, <TokenType.RANGE: 'RANGE'>, <TokenType.INT128: 'INT128'>, <TokenType.UTINYINT: 'UTINYINT'>, <TokenType.AUTO_INCREMENT: 'AUTO_INCREMENT'>, <TokenType.ASC: 'ASC'>, <TokenType.ROLLUP: 'ROLLUP'>, <TokenType.DATERANGE: 'DATERANGE'>, <TokenType.EXECUTE: 'EXECUTE'>, <TokenType.DATABASE: 'DATABASE'>, <TokenType.REFERENCES: 'REFERENCES'>, <TokenType.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>, <TokenType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, <TokenType.UNNEST: 'UNNEST'>, <TokenType.DOUBLE: 'DOUBLE'>, <TokenType.TINYTEXT: 'TINYTEXT'>, <TokenType.MERGE: 'MERGE'>, <TokenType.TIMESTAMP_S: 'TIMESTAMP_S'>, <TokenType.USERDEFINED: 'USERDEFINED'>, <TokenType.SEMI: 'SEMI'>, <TokenType.IMAGE: 'IMAGE'>, <TokenType.COMMENT: 'COMMENT'>, <TokenType.KEEP: 'KEEP'>, <TokenType.HSTORE: 'HSTORE'>, <TokenType.AGGREGATEFUNCTION: 'AGGREGATEFUNCTION'>, <TokenType.TAG: 'TAG'>, <TokenType.SOME: 'SOME'>, <TokenType.LONGBLOB: 'LONGBLOB'>, <TokenType.MODEL: 'MODEL'>, <TokenType.GEOMETRY: 'GEOMETRY'>, <TokenType.BIGINT: 'BIGINT'>, <TokenType.TIMESTAMP: 'TIMESTAMP'>, <TokenType.SMALLMONEY: 'SMALLMONEY'>, <TokenType.IPV4: 'IPV4'>, <TokenType.RECURSIVE: 'RECURSIVE'>, <TokenType.PARTITION: 'PARTITION'>, <TokenType.BINARY: 'BINARY'>, <TokenType.ISNULL: 'ISNULL'>, <TokenType.SHOW: 'SHOW'>, <TokenType.UNPIVOT: 'UNPIVOT'>, <TokenType.SET: 'SET'>, <TokenType.STRAIGHT_JOIN: 'STRAIGHT_JOIN'>, <TokenType.BPCHAR: 'BPCHAR'>, <TokenType.NUMRANGE: 'NUMRANGE'>, <TokenType.MEDIUMBLOB: 'MEDIUMBLOB'>, <TokenType.COLUMN: 'COLUMN'>, <TokenType.VOLATILE: 'VOLATILE'>, <TokenType.END: 'END'>, <TokenType.CHAR: 'CHAR'>, <TokenType.FORMAT: 'FORMAT'>, <TokenType.ORDINALITY: 'ORDINALITY'>, <TokenType.INDEX: 'INDEX'>, <TokenType.INTERVAL: 'INTERVAL'>, <TokenType.ARRAY: 'ARRAY'>, <TokenType.UUID: 'UUID'>, <TokenType.DEFAULT: 'DEFAULT'>, <TokenType.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <TokenType.DATETIME64: 'DATETIME64'>, <TokenType.IS: 'IS'>, <TokenType.TEMPORARY: 'TEMPORARY'>, <TokenType.DATE: 'DATE'>, <TokenType.SETTINGS: 'SETTINGS'>, <TokenType.UMEDIUMINT: 'UMEDIUMINT'>}
SHOW_TRIE: Dict = {}
SET_TRIE: Dict = {'GLOBAL': {0: True}, 'LOCAL': {0: True}, 'SESSION': {0: True}, 'TRANSACTION': {0: True}}
Inherited Members
sqlglot.parser.Parser
Parser
NO_PAREN_FUNCTIONS
STRUCT_TYPE_TOKENS
NESTED_TYPE_TOKENS
ENUM_TYPE_TOKENS
AGGREGATE_TYPE_TOKENS
TYPE_TOKENS
SIGNED_TO_UNSIGNED_TYPE_TOKEN
SUBQUERY_PREDICATES
RESERVED_TOKENS
DB_CREATABLES
CREATABLES
INTERVAL_VARS
ALIAS_TOKENS
ARRAY_CONSTRUCTORS
COMMENT_TABLE_ALIAS_TOKENS
UPDATE_ALIAS_TOKENS
TRIM_TYPES
FUNC_TOKENS
CONJUNCTION
ASSIGNMENT
DISJUNCTION
EQUALITY
COMPARISON
BITWISE
TERM
FACTOR
EXPONENT
TIMES
TIMESTAMPS
SET_OPERATIONS
JOIN_METHODS
JOIN_SIDES
JOIN_KINDS
LAMBDAS
COLUMN_OPERATORS
EXPRESSION_PARSERS
UNARY_PARSERS
STRING_PARSERS
NUMERIC_PARSERS
PRIMARY_PARSERS
PLACEHOLDER_PARSERS
RANGE_PARSERS
PROPERTY_PARSERS
CONSTRAINT_PARSERS
ALTER_PARSERS
ALTER_ALTER_PARSERS
SCHEMA_UNNAMED_CONSTRAINTS
NO_PAREN_FUNCTION_PARSERS
INVALID_FUNC_NAME_TOKENS
FUNCTIONS_WITH_ALIASED_ARGS
KEY_VALUE_DEFINITIONS
FUNCTION_PARSERS
SET_PARSERS
SHOW_PARSERS
TYPE_LITERAL_PARSERS
TYPE_CONVERTERS
DDL_SELECT_TOKENS
PRE_VOLATILE_TOKENS
TRANSACTION_KIND
TRANSACTION_CHARACTERISTICS
CONFLICT_ACTIONS
CREATE_SEQUENCE
ISOLATED_LOADING_OPTIONS
USABLES
CAST_ACTIONS
INSERT_ALTERNATIVES
CLONE_KEYWORDS
HISTORICAL_DATA_KIND
OPCLASS_FOLLOW_KEYWORDS
OPTYPE_FOLLOW_TOKENS
TABLE_INDEX_HINT_TOKENS
VIEW_ATTRIBUTES
WINDOW_ALIAS_TOKENS
WINDOW_BEFORE_PAREN_TOKENS
WINDOW_SIDES
JSON_KEY_VALUE_SEPARATOR_TOKENS
FETCH_TOKENS
ADD_CONSTRAINT_TOKENS
DISTINCT_TOKENS
NULL_TOKENS
UNNEST_OFFSET_ALIAS_TOKENS
SELECT_START_TOKENS
COPY_INTO_VARLEN_OPTIONS
STRICT_CAST
PREFIXED_PIVOT_COLUMNS
IDENTIFY_PIVOT_STRINGS
TABLESAMPLE_CSV
DEFAULT_SAMPLING_METHOD
TRIM_PATTERN_FIRST
MODIFIERS_ATTACHED_TO_SET_OP
SET_OP_MODIFIERS
JSON_ARROWS_REQUIRE_JSON_TYPE
COLON_IS_JSON_EXTRACT
VALUES_FOLLOWED_BY_PAREN
SUPPORTS_IMPLICIT_UNNEST
INTERVAL_SPANS
SUPPORTS_PARTITION_SELECTION
error_level
error_message_context
max_errors
dialect
reset
parse
parse_into
check_errors
raise_error
expression
validate_expression
errors
sql
class TSQL.Generator(sqlglot.generator.Generator):
 747    class Generator(generator.Generator):
 748        LIMIT_IS_TOP = True
 749        QUERY_HINTS = False
 750        RETURNING_END = False
 751        NVL2_SUPPORTED = False
 752        ALTER_TABLE_INCLUDE_COLUMN_KEYWORD = False
 753        LIMIT_FETCH = "FETCH"
 754        COMPUTED_COLUMN_WITH_TYPE = False
 755        CTE_RECURSIVE_KEYWORD_REQUIRED = False
 756        ENSURE_BOOLS = True
 757        NULL_ORDERING_SUPPORTED = None
 758        SUPPORTS_SINGLE_ARG_CONCAT = False
 759        TABLESAMPLE_SEED_KEYWORD = "REPEATABLE"
 760        SUPPORTS_SELECT_INTO = True
 761        JSON_PATH_BRACKETED_KEY_SUPPORTED = False
 762        SUPPORTS_TO_NUMBER = False
 763        SET_OP_MODIFIERS = False
 764        COPY_PARAMS_EQ_REQUIRED = True
 765
 766        EXPRESSIONS_WITHOUT_NESTED_CTES = {
 767            exp.Delete,
 768            exp.Insert,
 769            exp.Intersect,
 770            exp.Except,
 771            exp.Merge,
 772            exp.Select,
 773            exp.Subquery,
 774            exp.Union,
 775            exp.Update,
 776        }
 777
 778        SUPPORTED_JSON_PATH_PARTS = {
 779            exp.JSONPathKey,
 780            exp.JSONPathRoot,
 781            exp.JSONPathSubscript,
 782        }
 783
 784        TYPE_MAPPING = {
 785            **generator.Generator.TYPE_MAPPING,
 786            exp.DataType.Type.BOOLEAN: "BIT",
 787            exp.DataType.Type.DECIMAL: "NUMERIC",
 788            exp.DataType.Type.DATETIME: "DATETIME2",
 789            exp.DataType.Type.DOUBLE: "FLOAT",
 790            exp.DataType.Type.INT: "INTEGER",
 791            exp.DataType.Type.ROWVERSION: "ROWVERSION",
 792            exp.DataType.Type.TEXT: "VARCHAR(MAX)",
 793            exp.DataType.Type.TIMESTAMP: "DATETIME2",
 794            exp.DataType.Type.TIMESTAMPTZ: "DATETIMEOFFSET",
 795            exp.DataType.Type.UTINYINT: "TINYINT",
 796            exp.DataType.Type.VARIANT: "SQL_VARIANT",
 797        }
 798
 799        TYPE_MAPPING.pop(exp.DataType.Type.NCHAR)
 800        TYPE_MAPPING.pop(exp.DataType.Type.NVARCHAR)
 801
 802        TRANSFORMS = {
 803            **generator.Generator.TRANSFORMS,
 804            exp.AnyValue: any_value_to_max_sql,
 805            exp.ArrayToString: rename_func("STRING_AGG"),
 806            exp.AutoIncrementColumnConstraint: lambda *_: "IDENTITY",
 807            exp.DateAdd: date_delta_sql("DATEADD"),
 808            exp.DateDiff: date_delta_sql("DATEDIFF"),
 809            exp.CTE: transforms.preprocess([qualify_derived_table_outputs]),
 810            exp.CurrentDate: rename_func("GETDATE"),
 811            exp.CurrentTimestamp: rename_func("GETDATE"),
 812            exp.DateStrToDate: datestrtodate_sql,
 813            exp.Extract: rename_func("DATEPART"),
 814            exp.GeneratedAsIdentityColumnConstraint: generatedasidentitycolumnconstraint_sql,
 815            exp.GroupConcat: _string_agg_sql,
 816            exp.If: rename_func("IIF"),
 817            exp.JSONExtract: _json_extract_sql,
 818            exp.JSONExtractScalar: _json_extract_sql,
 819            exp.LastDay: lambda self, e: self.func("EOMONTH", e.this),
 820            exp.Max: max_or_greatest,
 821            exp.MD5: lambda self, e: self.func("HASHBYTES", exp.Literal.string("MD5"), e.this),
 822            exp.Min: min_or_least,
 823            exp.NumberToStr: _format_sql,
 824            exp.ParseJSON: lambda self, e: self.sql(e, "this"),
 825            exp.Repeat: rename_func("REPLICATE"),
 826            exp.Select: transforms.preprocess(
 827                [
 828                    transforms.eliminate_distinct_on,
 829                    transforms.eliminate_semi_and_anti_joins,
 830                    transforms.eliminate_qualify,
 831                ]
 832            ),
 833            exp.StrPosition: lambda self, e: self.func(
 834                "CHARINDEX", e.args.get("substr"), e.this, e.args.get("position")
 835            ),
 836            exp.Subquery: transforms.preprocess([qualify_derived_table_outputs]),
 837            exp.SHA: lambda self, e: self.func("HASHBYTES", exp.Literal.string("SHA1"), e.this),
 838            exp.SHA2: lambda self, e: self.func(
 839                "HASHBYTES", exp.Literal.string(f"SHA2_{e.args.get('length', 256)}"), e.this
 840            ),
 841            exp.TemporaryProperty: lambda self, e: "",
 842            exp.TimeStrToTime: lambda self, e: self.sql(
 843                exp.cast(e.this, exp.DataType.Type.DATETIME)
 844            ),
 845            exp.TimeToStr: _format_sql,
 846            exp.Trim: trim_sql,
 847            exp.TsOrDsAdd: date_delta_sql("DATEADD", cast=True),
 848            exp.TsOrDsDiff: date_delta_sql("DATEDIFF"),
 849        }
 850
 851        TRANSFORMS.pop(exp.ReturnsProperty)
 852
 853        PROPERTIES_LOCATION = {
 854            **generator.Generator.PROPERTIES_LOCATION,
 855            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
 856        }
 857
 858        def select_sql(self, expression: exp.Select) -> str:
 859            if expression.args.get("offset"):
 860                if not expression.args.get("order"):
 861                    # ORDER BY is required in order to use OFFSET in a query, so we use
 862                    # a noop order by, since we don't really care about the order.
 863                    # See: https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
 864                    expression.order_by(exp.select(exp.null()).subquery(), copy=False)
 865
 866                limit = expression.args.get("limit")
 867                if isinstance(limit, exp.Limit):
 868                    # TOP and OFFSET can't be combined, we need use FETCH instead of TOP
 869                    # we replace here because otherwise TOP would be generated in select_sql
 870                    limit.replace(exp.Fetch(direction="FIRST", count=limit.expression))
 871
 872            return super().select_sql(expression)
 873
 874        def convert_sql(self, expression: exp.Convert) -> str:
 875            name = "TRY_CONVERT" if expression.args.get("safe") else "CONVERT"
 876            return self.func(
 877                name, expression.this, expression.expression, expression.args.get("style")
 878            )
 879
 880        def queryoption_sql(self, expression: exp.QueryOption) -> str:
 881            option = self.sql(expression, "this")
 882            value = self.sql(expression, "expression")
 883            if value:
 884                optional_equal_sign = "= " if option in OPTIONS_THAT_REQUIRE_EQUAL else ""
 885                return f"{option} {optional_equal_sign}{value}"
 886            return option
 887
 888        def lateral_op(self, expression: exp.Lateral) -> str:
 889            cross_apply = expression.args.get("cross_apply")
 890            if cross_apply is True:
 891                return "CROSS APPLY"
 892            if cross_apply is False:
 893                return "OUTER APPLY"
 894
 895            # TODO: perhaps we can check if the parent is a Join and transpile it appropriately
 896            self.unsupported("LATERAL clause is not supported.")
 897            return "LATERAL"
 898
 899        def timefromparts_sql(self, expression: exp.TimeFromParts) -> str:
 900            nano = expression.args.get("nano")
 901            if nano is not None:
 902                nano.pop()
 903                self.unsupported("Specifying nanoseconds is not supported in TIMEFROMPARTS.")
 904
 905            if expression.args.get("fractions") is None:
 906                expression.set("fractions", exp.Literal.number(0))
 907            if expression.args.get("precision") is None:
 908                expression.set("precision", exp.Literal.number(0))
 909
 910            return rename_func("TIMEFROMPARTS")(self, expression)
 911
 912        def timestampfromparts_sql(self, expression: exp.TimestampFromParts) -> str:
 913            zone = expression.args.get("zone")
 914            if zone is not None:
 915                zone.pop()
 916                self.unsupported("Time zone is not supported in DATETIMEFROMPARTS.")
 917
 918            nano = expression.args.get("nano")
 919            if nano is not None:
 920                nano.pop()
 921                self.unsupported("Specifying nanoseconds is not supported in DATETIMEFROMPARTS.")
 922
 923            if expression.args.get("milli") is None:
 924                expression.set("milli", exp.Literal.number(0))
 925
 926            return rename_func("DATETIMEFROMPARTS")(self, expression)
 927
 928        def setitem_sql(self, expression: exp.SetItem) -> str:
 929            this = expression.this
 930            if isinstance(this, exp.EQ) and not isinstance(this.left, exp.Parameter):
 931                # T-SQL does not use '=' in SET command, except when the LHS is a variable.
 932                return f"{self.sql(this.left)} {self.sql(this.right)}"
 933
 934            return super().setitem_sql(expression)
 935
 936        def boolean_sql(self, expression: exp.Boolean) -> str:
 937            if type(expression.parent) in BIT_TYPES:
 938                return "1" if expression.this else "0"
 939
 940            return "(1 = 1)" if expression.this else "(1 = 0)"
 941
 942        def is_sql(self, expression: exp.Is) -> str:
 943            if isinstance(expression.expression, exp.Boolean):
 944                return self.binary(expression, "=")
 945            return self.binary(expression, "IS")
 946
 947        def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
 948            sql = self.sql(expression, "this")
 949            properties = expression.args.get("properties")
 950
 951            if sql[:1] != "#" and any(
 952                isinstance(prop, exp.TemporaryProperty)
 953                for prop in (properties.expressions if properties else [])
 954            ):
 955                sql = f"[#{sql[1:]}" if sql.startswith("[") else f"#{sql}"
 956
 957            return sql
 958
 959        def create_sql(self, expression: exp.Create) -> str:
 960            kind = expression.kind
 961            exists = expression.args.pop("exists", None)
 962
 963            if kind == "VIEW":
 964                expression.this.set("catalog", None)
 965
 966            sql = super().create_sql(expression)
 967
 968            like_property = expression.find(exp.LikeProperty)
 969            if like_property:
 970                ctas_expression = like_property.this
 971            else:
 972                ctas_expression = expression.expression
 973
 974            table = expression.find(exp.Table)
 975
 976            # Convert CTAS statement to SELECT .. INTO ..
 977            if kind == "TABLE" and ctas_expression:
 978                ctas_with = ctas_expression.args.get("with")
 979                if ctas_with:
 980                    ctas_with = ctas_with.pop()
 981
 982                if isinstance(ctas_expression, exp.UNWRAPPED_QUERIES):
 983                    ctas_expression = ctas_expression.subquery()
 984
 985                select_into = exp.select("*").from_(exp.alias_(ctas_expression, "temp", table=True))
 986                select_into.set("into", exp.Into(this=table))
 987                select_into.set("with", ctas_with)
 988
 989                if like_property:
 990                    select_into.limit(0, copy=False)
 991
 992                sql = self.sql(select_into)
 993
 994            if exists:
 995                identifier = self.sql(exp.Literal.string(exp.table_name(table) if table else ""))
 996                sql = self.sql(exp.Literal.string(sql))
 997                if kind == "SCHEMA":
 998                    sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.schemata WHERE schema_name = {identifier}) EXEC({sql})"""
 999                elif kind == "TABLE":
1000                    assert table
1001                    where = exp.and_(
1002                        exp.column("table_name").eq(table.name),
1003                        exp.column("table_schema").eq(table.db) if table.db else None,
1004                        exp.column("table_catalog").eq(table.catalog) if table.catalog else None,
1005                    )
1006                    sql = f"""IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE {where}) EXEC({sql})"""
1007                elif kind == "INDEX":
1008                    index = self.sql(exp.Literal.string(expression.this.text("this")))
1009                    sql = f"""IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = object_id({identifier}) AND name = {index}) EXEC({sql})"""
1010            elif expression.args.get("replace"):
1011                sql = sql.replace("CREATE OR REPLACE ", "CREATE OR ALTER ", 1)
1012
1013            return self.prepend_ctes(expression, sql)
1014
1015        def offset_sql(self, expression: exp.Offset) -> str:
1016            return f"{super().offset_sql(expression)} ROWS"
1017
1018        def version_sql(self, expression: exp.Version) -> str:
1019            name = "SYSTEM_TIME" if expression.name == "TIMESTAMP" else expression.name
1020            this = f"FOR {name}"
1021            expr = expression.expression
1022            kind = expression.text("kind")
1023            if kind in ("FROM", "BETWEEN"):
1024                args = expr.expressions
1025                sep = "TO" if kind == "FROM" else "AND"
1026                expr_sql = f"{self.sql(seq_get(args, 0))} {sep} {self.sql(seq_get(args, 1))}"
1027            else:
1028                expr_sql = self.sql(expr)
1029
1030            expr_sql = f" {expr_sql}" if expr_sql else ""
1031            return f"{this} {kind}{expr_sql}"
1032
1033        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
1034            table = expression.args.get("table")
1035            table = f"{table} " if table else ""
1036            return f"RETURNS {table}{self.sql(expression, 'this')}"
1037
1038        def returning_sql(self, expression: exp.Returning) -> str:
1039            into = self.sql(expression, "into")
1040            into = self.seg(f"INTO {into}") if into else ""
1041            return f"{self.seg('OUTPUT')} {self.expressions(expression, flat=True)}{into}"
1042
1043        def transaction_sql(self, expression: exp.Transaction) -> str:
1044            this = self.sql(expression, "this")
1045            this = f" {this}" if this else ""
1046            mark = self.sql(expression, "mark")
1047            mark = f" WITH MARK {mark}" if mark else ""
1048            return f"BEGIN TRANSACTION{this}{mark}"
1049
1050        def commit_sql(self, expression: exp.Commit) -> str:
1051            this = self.sql(expression, "this")
1052            this = f" {this}" if this else ""
1053            durability = expression.args.get("durability")
1054            durability = (
1055                f" WITH (DELAYED_DURABILITY = {'ON' if durability else 'OFF'})"
1056                if durability is not None
1057                else ""
1058            )
1059            return f"COMMIT TRANSACTION{this}{durability}"
1060
1061        def rollback_sql(self, expression: exp.Rollback) -> str:
1062            this = self.sql(expression, "this")
1063            this = f" {this}" if this else ""
1064            return f"ROLLBACK TRANSACTION{this}"
1065
1066        def identifier_sql(self, expression: exp.Identifier) -> str:
1067            identifier = super().identifier_sql(expression)
1068
1069            if expression.args.get("global"):
1070                identifier = f"##{identifier}"
1071            elif expression.args.get("temporary"):
1072                identifier = f"#{identifier}"
1073
1074            return identifier
1075
1076        def constraint_sql(self, expression: exp.Constraint) -> str:
1077            this = self.sql(expression, "this")
1078            expressions = self.expressions(expression, flat=True, sep=" ")
1079            return f"CONSTRAINT {this} {expressions}"
1080
1081        def length_sql(self, expression: exp.Length) -> str:
1082            return self._uncast_text(expression, "LEN")
1083
1084        def right_sql(self, expression: exp.Right) -> str:
1085            return self._uncast_text(expression, "RIGHT")
1086
1087        def left_sql(self, expression: exp.Left) -> str:
1088            return self._uncast_text(expression, "LEFT")
1089
1090        def _uncast_text(self, expression: exp.Expression, name: str) -> str:
1091            this = expression.this
1092            if isinstance(this, exp.Cast) and this.is_type(exp.DataType.Type.TEXT):
1093                this_sql = self.sql(this, "this")
1094            else:
1095                this_sql = self.sql(this)
1096            expression_sql = self.sql(expression, "expression")
1097            return self.func(name, this_sql, expression_sql if expression_sql else None)
1098
1099        def partition_sql(self, expression: exp.Partition) -> str:
1100            return f"WITH (PARTITIONS({self.expressions(expression, flat=True)}))"
1101
1102        def altertable_sql(self, expression: exp.AlterTable) -> str:
1103            action = seq_get(expression.args.get("actions") or [], 0)
1104            if isinstance(action, exp.RenameTable):
1105                return f"EXEC sp_rename '{self.sql(expression.this)}', '{action.this.name}'"
1106            return super().altertable_sql(expression)
1107
1108        def drop_sql(self, expression: exp.Drop) -> str:
1109            if expression.args["kind"] == "VIEW":
1110                expression.this.set("catalog", None)
1111            return super().drop_sql(expression)
1112
1113        def declare_sql(self, expression: exp.Declare) -> str:
1114            return f"DECLARE {self.expressions(expression, flat=True)}"
1115
1116        def declareitem_sql(self, expression: exp.DeclareItem) -> str:
1117            variable = self.sql(expression, "this")
1118            default = self.sql(expression, "default")
1119            default = f" = {default}" if default else ""
1120
1121            kind = self.sql(expression, "kind")
1122            if isinstance(expression.args.get("kind"), exp.Schema):
1123                kind = f"TABLE {kind}"
1124
1125            return f"{variable} AS {kind}{default}"
1126
1127        def options_modifier(self, expression: exp.Expression) -> str:
1128            options = self.expressions(expression, key="options")
1129            return f" OPTION{self.wrap(options)}" if options else ""

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

Arguments:
  • pretty: Whether to format the produced SQL string. Default: False.
  • identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True or 'always': Always quote. 'safe': Only quote identifiers that are case insensitive.
  • normalize: Whether to normalize identifiers to lowercase. Default: False.
  • pad: The pad size in a formatted string. For example, this affects the indentation of a projection in a query, relative to its nesting level. Default: 2.
  • indent: The indentation size in a formatted string. For example, this affects the indentation of subqueries and filters under a WHERE clause. Default: 2.
  • normalize_functions: How to normalize function names. Possible values are: "upper" or True (default): Convert names to uppercase. "lower": Convert names to lowercase. False: Disables function name normalization.
  • unsupported_level: Determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
  • max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
  • leading_comma: Whether the comma is leading or trailing in select expressions. This is only relevant when generating in pretty mode. Default: False
  • max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
  • comments: Whether to preserve comments in the output SQL code. Default: True
LIMIT_IS_TOP = True
QUERY_HINTS = False
RETURNING_END = False
NVL2_SUPPORTED = False
ALTER_TABLE_INCLUDE_COLUMN_KEYWORD = False
LIMIT_FETCH = 'FETCH'
COMPUTED_COLUMN_WITH_TYPE = False
CTE_RECURSIVE_KEYWORD_REQUIRED = False
ENSURE_BOOLS = True
NULL_ORDERING_SUPPORTED = None
SUPPORTS_SINGLE_ARG_CONCAT = False
TABLESAMPLE_SEED_KEYWORD = 'REPEATABLE'
SUPPORTS_SELECT_INTO = True
JSON_PATH_BRACKETED_KEY_SUPPORTED = False
SUPPORTS_TO_NUMBER = False
SET_OP_MODIFIERS = False
COPY_PARAMS_EQ_REQUIRED = True
TYPE_MAPPING = {<Type.MEDIUMTEXT: 'MEDIUMTEXT'>: 'TEXT', <Type.LONGTEXT: 'LONGTEXT'>: 'TEXT', <Type.TINYTEXT: 'TINYTEXT'>: 'TEXT', <Type.MEDIUMBLOB: 'MEDIUMBLOB'>: 'BLOB', <Type.LONGBLOB: 'LONGBLOB'>: 'BLOB', <Type.TINYBLOB: 'TINYBLOB'>: 'BLOB', <Type.INET: 'INET'>: 'INET', <Type.ROWVERSION: 'ROWVERSION'>: 'ROWVERSION', <Type.BOOLEAN: 'BOOLEAN'>: 'BIT', <Type.DECIMAL: 'DECIMAL'>: 'NUMERIC', <Type.DATETIME: 'DATETIME'>: 'DATETIME2', <Type.DOUBLE: 'DOUBLE'>: 'FLOAT', <Type.INT: 'INT'>: 'INTEGER', <Type.TEXT: 'TEXT'>: 'VARCHAR(MAX)', <Type.TIMESTAMP: 'TIMESTAMP'>: 'DATETIME2', <Type.TIMESTAMPTZ: 'TIMESTAMPTZ'>: 'DATETIMEOFFSET', <Type.UTINYINT: 'UTINYINT'>: 'TINYINT', <Type.VARIANT: 'VARIANT'>: 'SQL_VARIANT'}
TRANSFORMS = {<class 'sqlglot.expressions.JSONPathKey'>: <function <lambda>>, <class 'sqlglot.expressions.JSONPathRoot'>: <function <lambda>>, <class 'sqlglot.expressions.JSONPathSubscript'>: <function <lambda>>, <class 'sqlglot.expressions.AllowedValuesProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.AutoRefreshProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.BackupProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CaseSpecificColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CharacterSetColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CharacterSetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ClusteredColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CollateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CommentColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CopyGrantsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.DateFormatColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.DefaultColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.EncodeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.EphemeralColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExcludeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExecuteAsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExternalProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.GlobalProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.HeapProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.IcebergProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.InheritsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.InlineLengthColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.InputModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.IntervalSpan'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.JSONExtract'>: <function _json_extract_sql>, <class 'sqlglot.expressions.JSONExtractScalar'>: <function _json_extract_sql>, <class 'sqlglot.expressions.LanguageProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LocationProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LogProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.MaterializedProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.NonClusteredColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.NoPrimaryIndexProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.NotForReplicationColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnCommitProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnUpdateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OutputModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.PathColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ProjectionPolicyColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.RemoteWithConnectionModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SampleProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SecureProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SetConfigProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SettingsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SharingProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SqlReadWriteProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SqlSecurityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.StabilityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.StrictProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TemporaryProperty'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.TagColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TitleColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.Timestamp'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ToMap'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ToTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TransformModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TransientProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.UppercaseColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.UnloggedProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.VarMap'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ViewAttributeProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.VolatileProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.WithJournalTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.WithOperator'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.AnyValue'>: <function any_value_to_max_sql>, <class 'sqlglot.expressions.ArrayToString'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.AutoIncrementColumnConstraint'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.DateAdd'>: <function date_delta_sql.<locals>._delta_sql>, <class 'sqlglot.expressions.DateDiff'>: <function date_delta_sql.<locals>._delta_sql>, <class 'sqlglot.expressions.CTE'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.CurrentDate'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.CurrentTimestamp'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.DateStrToDate'>: <function datestrtodate_sql>, <class 'sqlglot.expressions.Extract'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.GeneratedAsIdentityColumnConstraint'>: <function generatedasidentitycolumnconstraint_sql>, <class 'sqlglot.expressions.GroupConcat'>: <function _string_agg_sql>, <class 'sqlglot.expressions.If'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.LastDay'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.Max'>: <function max_or_greatest>, <class 'sqlglot.expressions.MD5'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.Min'>: <function min_or_least>, <class 'sqlglot.expressions.NumberToStr'>: <function _format_sql>, <class 'sqlglot.expressions.ParseJSON'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.Repeat'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.Select'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.StrPosition'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.Subquery'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.SHA'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.SHA2'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.TimeStrToTime'>: <function TSQL.Generator.<lambda>>, <class 'sqlglot.expressions.TimeToStr'>: <function _format_sql>, <class 'sqlglot.expressions.Trim'>: <function trim_sql>, <class 'sqlglot.expressions.TsOrDsAdd'>: <function date_delta_sql.<locals>._delta_sql>, <class 'sqlglot.expressions.TsOrDsDiff'>: <function date_delta_sql.<locals>._delta_sql>}
PROPERTIES_LOCATION = {<class 'sqlglot.expressions.AllowedValuesProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.AlgorithmProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.AutoIncrementProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.AutoRefreshProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.BackupProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.BlockCompressionProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.CharacterSetProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ChecksumProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.CollateProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.CopyGrantsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Cluster'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ClusteredByProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DataBlocksizeProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.DataDeletionProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DefinerProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.DictRange'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DictProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DistKeyProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DistStyleProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.EngineProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ExecuteAsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ExternalProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.FallbackProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.FileFormatProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.FreespaceProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.GlobalProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.HeapProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.InheritsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.IcebergProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.InputModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.IsolatedLoadingProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.JournalProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.LanguageProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LikeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LocationProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LockProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LockingProperty'>: <Location.POST_ALIAS: 'POST_ALIAS'>, <class 'sqlglot.expressions.LogProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.MaterializedProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.MergeBlockRatioProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.NoPrimaryIndexProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.OnProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.OnCommitProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.Order'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.OutputModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.PartitionedByProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.PartitionedOfProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.PrimaryKey'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Property'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.RemoteWithConnectionModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ReturnsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatDelimitedProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatSerdeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SampleProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SchemaCommentProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SecureProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.SerdeProperties'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Set'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SettingsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SetProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.SetConfigProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SharingProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.SequenceProperties'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.SortKeyProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SqlReadWriteProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SqlSecurityProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.StabilityProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.StrictProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.TemporaryProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.ToTableProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.TransientProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.TransformModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.MergeTreeTTL'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.UnloggedProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.ViewAttributeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.VolatileProperty'>: <Location.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.WithDataProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.WithJournalTableProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.WithSystemVersioningProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>}
def select_sql(self, expression: sqlglot.expressions.Select) -> str:
858        def select_sql(self, expression: exp.Select) -> str:
859            if expression.args.get("offset"):
860                if not expression.args.get("order"):
861                    # ORDER BY is required in order to use OFFSET in a query, so we use
862                    # a noop order by, since we don't really care about the order.
863                    # See: https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
864                    expression.order_by(exp.select(exp.null()).subquery(), copy=False)
865
866                limit = expression.args.get("limit")
867                if isinstance(limit, exp.Limit):
868                    # TOP and OFFSET can't be combined, we need use FETCH instead of TOP
869                    # we replace here because otherwise TOP would be generated in select_sql
870                    limit.replace(exp.Fetch(direction="FIRST", count=limit.expression))
871
872            return super().select_sql(expression)
def convert_sql(self, expression: sqlglot.expressions.Convert) -> str:
874        def convert_sql(self, expression: exp.Convert) -> str:
875            name = "TRY_CONVERT" if expression.args.get("safe") else "CONVERT"
876            return self.func(
877                name, expression.this, expression.expression, expression.args.get("style")
878            )
def queryoption_sql(self, expression: sqlglot.expressions.QueryOption) -> str:
880        def queryoption_sql(self, expression: exp.QueryOption) -> str:
881            option = self.sql(expression, "this")
882            value = self.sql(expression, "expression")
883            if value:
884                optional_equal_sign = "= " if option in OPTIONS_THAT_REQUIRE_EQUAL else ""
885                return f"{option} {optional_equal_sign}{value}"
886            return option
def lateral_op(self, expression: sqlglot.expressions.Lateral) -> str:
888        def lateral_op(self, expression: exp.Lateral) -> str:
889            cross_apply = expression.args.get("cross_apply")
890            if cross_apply is True:
891                return "CROSS APPLY"
892            if cross_apply is False:
893                return "OUTER APPLY"
894
895            # TODO: perhaps we can check if the parent is a Join and transpile it appropriately
896            self.unsupported("LATERAL clause is not supported.")
897            return "LATERAL"
def timefromparts_sql(self, expression: sqlglot.expressions.TimeFromParts) -> str:
899        def timefromparts_sql(self, expression: exp.TimeFromParts) -> str:
900            nano = expression.args.get("nano")
901            if nano is not None:
902                nano.pop()
903                self.unsupported("Specifying nanoseconds is not supported in TIMEFROMPARTS.")
904
905            if expression.args.get("fractions") is None:
906                expression.set("fractions", exp.Literal.number(0))
907            if expression.args.get("precision") is None:
908                expression.set("precision", exp.Literal.number(0))
909
910            return rename_func("TIMEFROMPARTS")(self, expression)
def timestampfromparts_sql(self, expression: sqlglot.expressions.TimestampFromParts) -> str:
912        def timestampfromparts_sql(self, expression: exp.TimestampFromParts) -> str:
913            zone = expression.args.get("zone")
914            if zone is not None:
915                zone.pop()
916                self.unsupported("Time zone is not supported in DATETIMEFROMPARTS.")
917
918            nano = expression.args.get("nano")
919            if nano is not None:
920                nano.pop()
921                self.unsupported("Specifying nanoseconds is not supported in DATETIMEFROMPARTS.")
922
923            if expression.args.get("milli") is None:
924                expression.set("milli", exp.Literal.number(0))
925
926            return rename_func("DATETIMEFROMPARTS")(self, expression)
def setitem_sql(self, expression: sqlglot.expressions.SetItem) -> str:
928        def setitem_sql(self, expression: exp.SetItem) -> str:
929            this = expression.this
930            if isinstance(this, exp.EQ) and not isinstance(this.left, exp.Parameter):
931                # T-SQL does not use '=' in SET command, except when the LHS is a variable.
932                return f"{self.sql(this.left)} {self.sql(this.right)}"
933
934            return super().setitem_sql(expression)
def boolean_sql(self, expression: sqlglot.expressions.Boolean) -> str:
936        def boolean_sql(self, expression: exp.Boolean) -> str:
937            if type(expression.parent) in BIT_TYPES:
938                return "1" if expression.this else "0"
939
940            return "(1 = 1)" if expression.this else "(1 = 0)"
def is_sql(self, expression: sqlglot.expressions.Is) -> str:
942        def is_sql(self, expression: exp.Is) -> str:
943            if isinstance(expression.expression, exp.Boolean):
944                return self.binary(expression, "=")
945            return self.binary(expression, "IS")
def createable_sql( self, expression: sqlglot.expressions.Create, locations: DefaultDict) -> str:
947        def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
948            sql = self.sql(expression, "this")
949            properties = expression.args.get("properties")
950
951            if sql[:1] != "#" and any(
952                isinstance(prop, exp.TemporaryProperty)
953                for prop in (properties.expressions if properties else [])
954            ):
955                sql = f"[#{sql[1:]}" if sql.startswith("[") else f"#{sql}"
956
957            return sql
def create_sql(self, expression: sqlglot.expressions.Create) -> str:
 959        def create_sql(self, expression: exp.Create) -> str:
 960            kind = expression.kind
 961            exists = expression.args.pop("exists", None)
 962
 963            if kind == "VIEW":
 964                expression.this.set("catalog", None)
 965
 966            sql = super().create_sql(expression)
 967