sqlglot.dialects.tsql API documentation

Edit on GitHub

# sqlglot.dialects.tsql

View Source

      1from __future__ import annotations
      2
      3import re
      4import typing as t
      5
      6from sqlglot import exp, generator, parser, tokens
      7from sqlglot.dialects.dialect import (
      8    Dialect,
      9    max_or_greatest,
     10    min_or_least,
     11    parse_date_delta,
     12    rename_func,
     13)
     14from sqlglot.expressions import DataType
     15from sqlglot.helper import seq_get
     16from sqlglot.time import format_time
     17from sqlglot.tokens import TokenType
     18
     19FULL_FORMAT_TIME_MAPPING = {
     20    "weekday": "%A",
     21    "dw": "%A",
     22    "w": "%A",
     23    "month": "%B",
     24    "mm": "%B",
     25    "m": "%B",
     26}
     27
     28DATE_DELTA_INTERVAL = {
     29    "year": "year",
     30    "yyyy": "year",
     31    "yy": "year",
     32    "quarter": "quarter",
     33    "qq": "quarter",
     34    "q": "quarter",
     35    "month": "month",
     36    "mm": "month",
     37    "m": "month",
     38    "week": "week",
     39    "ww": "week",
     40    "wk": "week",
     41    "day": "day",
     42    "dd": "day",
     43    "d": "day",
     44}
     45
     46
     47DATE_FMT_RE = re.compile("([dD]{1,2})|([mM]{1,2})|([yY]{1,4})|([hH]{1,2})|([sS]{1,2})")
     48
     49# N = Numeric, C=Currency
     50TRANSPILE_SAFE_NUMBER_FMT = {"N", "C"}
     51
     52
     53def _format_time_lambda(exp_class, full_format_mapping=None, default=None):
     54    def _format_time(args):
     55        return exp_class(
     56            this=seq_get(args, 1),
     57            format=exp.Literal.string(
     58                format_time(
     59                    seq_get(args, 0).name or (TSQL.time_format if default is True else default),
     60                    {**TSQL.time_mapping, **FULL_FORMAT_TIME_MAPPING}
     61                    if full_format_mapping
     62                    else TSQL.time_mapping,
     63                )
     64            ),
     65        )
     66
     67    return _format_time
     68
     69
     70def _parse_format(args):
     71    fmt = seq_get(args, 1)
     72    number_fmt = fmt.name in TRANSPILE_SAFE_NUMBER_FMT or not DATE_FMT_RE.search(fmt.this)
     73    if number_fmt:
     74        return exp.NumberToStr(this=seq_get(args, 0), format=fmt)
     75    return exp.TimeToStr(
     76        this=seq_get(args, 0),
     77        format=exp.Literal.string(
     78            format_time(fmt.name, TSQL.format_time_mapping)
     79            if len(fmt.name) == 1
     80            else format_time(fmt.name, TSQL.time_mapping)
     81        ),
     82    )
     83
     84
     85def _parse_eomonth(args):
     86    date = seq_get(args, 0)
     87    month_lag = seq_get(args, 1)
     88    unit = DATE_DELTA_INTERVAL.get("month")
     89
     90    if month_lag is None:
     91        return exp.LastDateOfMonth(this=date)
     92
     93    # Remove month lag argument in parser as its compared with the number of arguments of the resulting class
     94    args.remove(month_lag)
     95
     96    return exp.LastDateOfMonth(this=exp.DateAdd(this=date, expression=month_lag, unit=unit))
     97
     98
     99def generate_date_delta_with_unit_sql(self, e):
    100    func = "DATEADD" if isinstance(e, exp.DateAdd) else "DATEDIFF"
    101    return self.func(func, e.text("unit"), e.expression, e.this)
    102
    103
    104def _format_sql(self, e):
    105    fmt = (
    106        e.args["format"]
    107        if isinstance(e, exp.NumberToStr)
    108        else exp.Literal.string(format_time(e.text("format"), TSQL.inverse_time_mapping))
    109    )
    110    return self.func("FORMAT", e.this, fmt)
    111
    112
    113def _string_agg_sql(self, e):
    114    e = e.copy()
    115
    116    this = e.this
    117    distinct = e.find(exp.Distinct)
    118    if distinct:
    119        # exp.Distinct can appear below an exp.Order or an exp.GroupConcat expression
    120        self.unsupported("T-SQL STRING_AGG doesn't support DISTINCT.")
    121        this = distinct.pop().expressions[0]
    122
    123    order = ""
    124    if isinstance(e.this, exp.Order):
    125        if e.this.this:
    126            this = e.this.this.pop()
    127        order = f" WITHIN GROUP ({self.sql(e.this)[1:]})"  # Order has a leading space
    128
    129    separator = e.args.get("separator") or exp.Literal.string(",")
    130    return f"STRING_AGG({self.format_args(this, separator)}){order}"
    131
    132
    133class TSQL(Dialect):
    134    null_ordering = "nulls_are_small"
    135    time_format = "'yyyy-mm-dd hh:mm:ss'"
    136
    137    time_mapping = {
    138        "year": "%Y",
    139        "qq": "%q",
    140        "q": "%q",
    141        "quarter": "%q",
    142        "dayofyear": "%j",
    143        "day": "%d",
    144        "dy": "%d",
    145        "y": "%Y",
    146        "week": "%W",
    147        "ww": "%W",
    148        "wk": "%W",
    149        "hour": "%h",
    150        "hh": "%I",
    151        "minute": "%M",
    152        "mi": "%M",
    153        "n": "%M",
    154        "second": "%S",
    155        "ss": "%S",
    156        "s": "%-S",
    157        "millisecond": "%f",
    158        "ms": "%f",
    159        "weekday": "%W",
    160        "dw": "%W",
    161        "month": "%m",
    162        "mm": "%M",
    163        "m": "%-M",
    164        "Y": "%Y",
    165        "YYYY": "%Y",
    166        "YY": "%y",
    167        "MMMM": "%B",
    168        "MMM": "%b",
    169        "MM": "%m",
    170        "M": "%-m",
    171        "dd": "%d",
    172        "d": "%-d",
    173        "HH": "%H",
    174        "H": "%-H",
    175        "h": "%-I",
    176        "S": "%f",
    177        "yyyy": "%Y",
    178        "yy": "%y",
    179    }
    180
    181    convert_format_mapping = {
    182        "0": "%b %d %Y %-I:%M%p",
    183        "1": "%m/%d/%y",
    184        "2": "%y.%m.%d",
    185        "3": "%d/%m/%y",
    186        "4": "%d.%m.%y",
    187        "5": "%d-%m-%y",
    188        "6": "%d %b %y",
    189        "7": "%b %d, %y",
    190        "8": "%H:%M:%S",
    191        "9": "%b %d %Y %-I:%M:%S:%f%p",
    192        "10": "mm-dd-yy",
    193        "11": "yy/mm/dd",
    194        "12": "yymmdd",
    195        "13": "%d %b %Y %H:%M:ss:%f",
    196        "14": "%H:%M:%S:%f",
    197        "20": "%Y-%m-%d %H:%M:%S",
    198        "21": "%Y-%m-%d %H:%M:%S.%f",
    199        "22": "%m/%d/%y %-I:%M:%S %p",
    200        "23": "%Y-%m-%d",
    201        "24": "%H:%M:%S",
    202        "25": "%Y-%m-%d %H:%M:%S.%f",
    203        "100": "%b %d %Y %-I:%M%p",
    204        "101": "%m/%d/%Y",
    205        "102": "%Y.%m.%d",
    206        "103": "%d/%m/%Y",
    207        "104": "%d.%m.%Y",
    208        "105": "%d-%m-%Y",
    209        "106": "%d %b %Y",
    210        "107": "%b %d, %Y",
    211        "108": "%H:%M:%S",
    212        "109": "%b %d %Y %-I:%M:%S:%f%p",
    213        "110": "%m-%d-%Y",
    214        "111": "%Y/%m/%d",
    215        "112": "%Y%m%d",
    216        "113": "%d %b %Y %H:%M:%S:%f",
    217        "114": "%H:%M:%S:%f",
    218        "120": "%Y-%m-%d %H:%M:%S",
    219        "121": "%Y-%m-%d %H:%M:%S.%f",
    220    }
    221    # not sure if complete
    222    format_time_mapping = {
    223        "y": "%B %Y",
    224        "d": "%m/%d/%Y",
    225        "H": "%-H",
    226        "h": "%-I",
    227        "s": "%Y-%m-%d %H:%M:%S",
    228        "D": "%A,%B,%Y",
    229        "f": "%A,%B,%Y %-I:%M %p",
    230        "F": "%A,%B,%Y %-I:%M:%S %p",
    231        "g": "%m/%d/%Y %-I:%M %p",
    232        "G": "%m/%d/%Y %-I:%M:%S %p",
    233        "M": "%B %-d",
    234        "m": "%B %-d",
    235        "O": "%Y-%m-%dT%H:%M:%S",
    236        "u": "%Y-%M-%D %H:%M:%S%z",
    237        "U": "%A, %B %D, %Y %H:%M:%S%z",
    238        "T": "%-I:%M:%S %p",
    239        "t": "%-I:%M",
    240        "Y": "%a %Y",
    241    }
    242
    243    class Tokenizer(tokens.Tokenizer):
    244        IDENTIFIERS = ['"', ("[", "]")]
    245
    246        QUOTES = ["'", '"']
    247
    248        KEYWORDS = {
    249            **tokens.Tokenizer.KEYWORDS,
    250            "DATETIME2": TokenType.DATETIME,
    251            "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
    252            "DECLARE": TokenType.COMMAND,
    253            "IMAGE": TokenType.IMAGE,
    254            "MONEY": TokenType.MONEY,
    255            "NTEXT": TokenType.TEXT,
    256            "NVARCHAR(MAX)": TokenType.TEXT,
    257            "PRINT": TokenType.COMMAND,
    258            "PROC": TokenType.PROCEDURE,
    259            "REAL": TokenType.FLOAT,
    260            "ROWVERSION": TokenType.ROWVERSION,
    261            "SMALLDATETIME": TokenType.DATETIME,
    262            "SMALLMONEY": TokenType.SMALLMONEY,
    263            "SQL_VARIANT": TokenType.VARIANT,
    264            "TIME": TokenType.TIMESTAMP,
    265            "TOP": TokenType.TOP,
    266            "UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
    267            "VARCHAR(MAX)": TokenType.TEXT,
    268            "XML": TokenType.XML,
    269        }
    270
    271        # TSQL allows @, # to appear as a variable/identifier prefix
    272        SINGLE_TOKENS = tokens.Tokenizer.SINGLE_TOKENS.copy()
    273        SINGLE_TOKENS.pop("#")
    274
    275    class Parser(parser.Parser):
    276        FUNCTIONS = {
    277            **parser.Parser.FUNCTIONS,  # type: ignore
    278            "CHARINDEX": lambda args: exp.StrPosition(
    279                this=seq_get(args, 1),
    280                substr=seq_get(args, 0),
    281                position=seq_get(args, 2),
    282            ),
    283            "DATEADD": parse_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL),
    284            "DATEDIFF": parse_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL),
    285            "DATENAME": _format_time_lambda(exp.TimeToStr, full_format_mapping=True),
    286            "DATEPART": _format_time_lambda(exp.TimeToStr),
    287            "EOMONTH": _parse_eomonth,
    288            "FORMAT": _parse_format,
    289            "GETDATE": exp.CurrentTimestamp.from_arg_list,
    290            "IIF": exp.If.from_arg_list,
    291            "ISNULL": exp.Coalesce.from_arg_list,
    292            "JSON_VALUE": exp.JSONExtractScalar.from_arg_list,
    293            "LEN": exp.Length.from_arg_list,
    294            "REPLICATE": exp.Repeat.from_arg_list,
    295            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
    296            "SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
    297        }
    298
    299        VAR_LENGTH_DATATYPES = {
    300            DataType.Type.NVARCHAR,
    301            DataType.Type.VARCHAR,
    302            DataType.Type.CHAR,
    303            DataType.Type.NCHAR,
    304        }
    305
    306        RETURNS_TABLE_TOKENS = parser.Parser.ID_VAR_TOKENS - {  # type: ignore
    307            TokenType.TABLE,
    308            *parser.Parser.TYPE_TOKENS,  # type: ignore
    309        }
    310
    311        STATEMENT_PARSERS = {
    312            **parser.Parser.STATEMENT_PARSERS,  # type: ignore
    313            TokenType.END: lambda self: self._parse_command(),
    314        }
    315
    316        LOG_BASE_FIRST = False
    317        LOG_DEFAULTS_TO_LN = True
    318
    319        def _parse_system_time(self) -> t.Optional[exp.Expression]:
    320            if not self._match_text_seq("FOR", "SYSTEM_TIME"):
    321                return None
    322
    323            if self._match_text_seq("AS", "OF"):
    324                system_time = self.expression(
    325                    exp.SystemTime, this=self._parse_bitwise(), kind="AS OF"
    326                )
    327            elif self._match_set((TokenType.FROM, TokenType.BETWEEN)):
    328                kind = self._prev.text
    329                this = self._parse_bitwise()
    330                self._match_texts(("TO", "AND"))
    331                expression = self._parse_bitwise()
    332                system_time = self.expression(
    333                    exp.SystemTime, this=this, expression=expression, kind=kind
    334                )
    335            elif self._match_text_seq("CONTAINED", "IN"):
    336                args = self._parse_wrapped_csv(self._parse_bitwise)
    337                system_time = self.expression(
    338                    exp.SystemTime,
    339                    this=seq_get(args, 0),
    340                    expression=seq_get(args, 1),
    341                    kind="CONTAINED IN",
    342                )
    343            elif self._match(TokenType.ALL):
    344                system_time = self.expression(exp.SystemTime, kind="ALL")
    345            else:
    346                system_time = None
    347                self.raise_error("Unable to parse FOR SYSTEM_TIME clause")
    348
    349            return system_time
    350
    351        def _parse_table_parts(self, schema: bool = False) -> exp.Expression:
    352            table = super()._parse_table_parts(schema=schema)
    353            table.set("system_time", self._parse_system_time())
    354            return table
    355
    356        def _parse_returns(self) -> exp.Expression:
    357            table = self._parse_id_var(any_token=False, tokens=self.RETURNS_TABLE_TOKENS)
    358            returns = super()._parse_returns()
    359            returns.set("table", table)
    360            return returns
    361
    362        def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]:
    363            to = self._parse_types()
    364            self._match(TokenType.COMMA)
    365            this = self._parse_conjunction()
    366
    367            if not to or not this:
    368                return None
    369
    370            # Retrieve length of datatype and override to default if not specified
    371            if seq_get(to.expressions, 0) is None and to.this in self.VAR_LENGTH_DATATYPES:
    372                to = exp.DataType.build(to.this, expressions=[exp.Literal.number(30)], nested=False)
    373
    374            # Check whether a conversion with format is applicable
    375            if self._match(TokenType.COMMA):
    376                format_val = self._parse_number()
    377                format_val_name = format_val.name if format_val else ""
    378
    379                if format_val_name not in TSQL.convert_format_mapping:
    380                    raise ValueError(
    381                        f"CONVERT function at T-SQL does not support format style {format_val_name}"
    382                    )
    383
    384                format_norm = exp.Literal.string(TSQL.convert_format_mapping[format_val_name])
    385
    386                # Check whether the convert entails a string to date format
    387                if to.this == DataType.Type.DATE:
    388                    return self.expression(exp.StrToDate, this=this, format=format_norm)
    389                # Check whether the convert entails a string to datetime format
    390                elif to.this == DataType.Type.DATETIME:
    391                    return self.expression(exp.StrToTime, this=this, format=format_norm)
    392                # Check whether the convert entails a date to string format
    393                elif to.this in self.VAR_LENGTH_DATATYPES:
    394                    return self.expression(
    395                        exp.Cast if strict else exp.TryCast,
    396                        to=to,
    397                        this=self.expression(exp.TimeToStr, this=this, format=format_norm),
    398                    )
    399                elif to.this == DataType.Type.TEXT:
    400                    return self.expression(exp.TimeToStr, this=this, format=format_norm)
    401
    402            # Entails a simple cast without any format requirement
    403            return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to)
    404
    405        def _parse_user_defined_function(
    406            self, kind: t.Optional[TokenType] = None
    407        ) -> t.Optional[exp.Expression]:
    408            this = super()._parse_user_defined_function(kind=kind)
    409
    410            if (
    411                kind == TokenType.FUNCTION
    412                or isinstance(this, exp.UserDefinedFunction)
    413                or self._match(TokenType.ALIAS, advance=False)
    414            ):
    415                return this
    416
    417            expressions = self._parse_csv(self._parse_function_parameter)
    418            return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)
    419
    420    class Generator(generator.Generator):
    421        LOCKING_READS_SUPPORTED = True
    422
    423        TYPE_MAPPING = {
    424            **generator.Generator.TYPE_MAPPING,  # type: ignore
    425            exp.DataType.Type.INT: "INTEGER",
    426            exp.DataType.Type.DECIMAL: "NUMERIC",
    427            exp.DataType.Type.DATETIME: "DATETIME2",
    428            exp.DataType.Type.VARIANT: "SQL_VARIANT",
    429        }
    430
    431        TRANSFORMS = {
    432            **generator.Generator.TRANSFORMS,  # type: ignore
    433            exp.DateAdd: generate_date_delta_with_unit_sql,
    434            exp.DateDiff: generate_date_delta_with_unit_sql,
    435            exp.CurrentDate: rename_func("GETDATE"),
    436            exp.CurrentTimestamp: rename_func("GETDATE"),
    437            exp.If: rename_func("IIF"),
    438            exp.NumberToStr: _format_sql,
    439            exp.TimeToStr: _format_sql,
    440            exp.GroupConcat: _string_agg_sql,
    441            exp.Max: max_or_greatest,
    442            exp.Min: min_or_least,
    443        }
    444
    445        TRANSFORMS.pop(exp.ReturnsProperty)
    446
    447        LIMIT_FETCH = "FETCH"
    448
    449        def offset_sql(self, expression: exp.Offset) -> str:
    450            return f"{super().offset_sql(expression)} ROWS"
    451
    452        def systemtime_sql(self, expression: exp.SystemTime) -> str:
    453            kind = expression.args["kind"]
    454            if kind == "ALL":
    455                return "FOR SYSTEM_TIME ALL"
    456
    457            start = self.sql(expression, "this")
    458            if kind == "AS OF":
    459                return f"FOR SYSTEM_TIME AS OF {start}"
    460
    461            end = self.sql(expression, "expression")
    462            if kind == "FROM":
    463                return f"FOR SYSTEM_TIME FROM {start} TO {end}"
    464            if kind == "BETWEEN":
    465                return f"FOR SYSTEM_TIME BETWEEN {start} AND {end}"
    466
    467            return f"FOR SYSTEM_TIME CONTAINED IN ({start}, {end})"
    468
    469        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
    470            table = expression.args.get("table")
    471            table = f"{table} " if table else ""
    472            return f"RETURNS {table}{self.sql(expression, 'this')}"

def generate_date_delta_with_unit_sql(self, e): View Source

    100def generate_date_delta_with_unit_sql(self, e):
    101    func = "DATEADD" if isinstance(e, exp.DateAdd) else "DATEDIFF"
    102    return self.func(func, e.text("unit"), e.expression, e.this)

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

    134class TSQL(Dialect):
    135    null_ordering = "nulls_are_small"
    136    time_format = "'yyyy-mm-dd hh:mm:ss'"
    137
    138    time_mapping = {
    139        "year": "%Y",
    140        "qq": "%q",
    141        "q": "%q",
    142        "quarter": "%q",
    143        "dayofyear": "%j",
    144        "day": "%d",
    145        "dy": "%d",
    146        "y": "%Y",
    147        "week": "%W",
    148        "ww": "%W",
    149        "wk": "%W",
    150        "hour": "%h",
    151        "hh": "%I",
    152        "minute": "%M",
    153        "mi": "%M",
    154        "n": "%M",
    155        "second": "%S",
    156        "ss": "%S",
    157        "s": "%-S",
    158        "millisecond": "%f",
    159        "ms": "%f",
    160        "weekday": "%W",
    161        "dw": "%W",
    162        "month": "%m",
    163        "mm": "%M",
    164        "m": "%-M",
    165        "Y": "%Y",
    166        "YYYY": "%Y",
    167        "YY": "%y",
    168        "MMMM": "%B",
    169        "MMM": "%b",
    170        "MM": "%m",
    171        "M": "%-m",
    172        "dd": "%d",
    173        "d": "%-d",
    174        "HH": "%H",
    175        "H": "%-H",
    176        "h": "%-I",
    177        "S": "%f",
    178        "yyyy": "%Y",
    179        "yy": "%y",
    180    }
    181
    182    convert_format_mapping = {
    183        "0": "%b %d %Y %-I:%M%p",
    184        "1": "%m/%d/%y",
    185        "2": "%y.%m.%d",
    186        "3": "%d/%m/%y",
    187        "4": "%d.%m.%y",
    188        "5": "%d-%m-%y",
    189        "6": "%d %b %y",
    190        "7": "%b %d, %y",
    191        "8": "%H:%M:%S",
    192        "9": "%b %d %Y %-I:%M:%S:%f%p",
    193        "10": "mm-dd-yy",
    194        "11": "yy/mm/dd",
    195        "12": "yymmdd",
    196        "13": "%d %b %Y %H:%M:ss:%f",
    197        "14": "%H:%M:%S:%f",
    198        "20": "%Y-%m-%d %H:%M:%S",
    199        "21": "%Y-%m-%d %H:%M:%S.%f",
    200        "22": "%m/%d/%y %-I:%M:%S %p",
    201        "23": "%Y-%m-%d",
    202        "24": "%H:%M:%S",
    203        "25": "%Y-%m-%d %H:%M:%S.%f",
    204        "100": "%b %d %Y %-I:%M%p",
    205        "101": "%m/%d/%Y",
    206        "102": "%Y.%m.%d",
    207        "103": "%d/%m/%Y",
    208        "104": "%d.%m.%Y",
    209        "105": "%d-%m-%Y",
    210        "106": "%d %b %Y",
    211        "107": "%b %d, %Y",
    212        "108": "%H:%M:%S",
    213        "109": "%b %d %Y %-I:%M:%S:%f%p",
    214        "110": "%m-%d-%Y",
    215        "111": "%Y/%m/%d",
    216        "112": "%Y%m%d",
    217        "113": "%d %b %Y %H:%M:%S:%f",
    218        "114": "%H:%M:%S:%f",
    219        "120": "%Y-%m-%d %H:%M:%S",
    220        "121": "%Y-%m-%d %H:%M:%S.%f",
    221    }
    222    # not sure if complete
    223    format_time_mapping = {
    224        "y": "%B %Y",
    225        "d": "%m/%d/%Y",
    226        "H": "%-H",
    227        "h": "%-I",
    228        "s": "%Y-%m-%d %H:%M:%S",
    229        "D": "%A,%B,%Y",
    230        "f": "%A,%B,%Y %-I:%M %p",
    231        "F": "%A,%B,%Y %-I:%M:%S %p",
    232        "g": "%m/%d/%Y %-I:%M %p",
    233        "G": "%m/%d/%Y %-I:%M:%S %p",
    234        "M": "%B %-d",
    235        "m": "%B %-d",
    236        "O": "%Y-%m-%dT%H:%M:%S",
    237        "u": "%Y-%M-%D %H:%M:%S%z",
    238        "U": "%A, %B %D, %Y %H:%M:%S%z",
    239        "T": "%-I:%M:%S %p",
    240        "t": "%-I:%M",
    241        "Y": "%a %Y",
    242    }
    243
    244    class Tokenizer(tokens.Tokenizer):
    245        IDENTIFIERS = ['"', ("[", "]")]
    246
    247        QUOTES = ["'", '"']
    248
    249        KEYWORDS = {
    250            **tokens.Tokenizer.KEYWORDS,
    251            "DATETIME2": TokenType.DATETIME,
    252            "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
    253            "DECLARE": TokenType.COMMAND,
    254            "IMAGE": TokenType.IMAGE,
    255            "MONEY": TokenType.MONEY,
    256            "NTEXT": TokenType.TEXT,
    257            "NVARCHAR(MAX)": TokenType.TEXT,
    258            "PRINT": TokenType.COMMAND,
    259            "PROC": TokenType.PROCEDURE,
    260            "REAL": TokenType.FLOAT,
    261            "ROWVERSION": TokenType.ROWVERSION,
    262            "SMALLDATETIME": TokenType.DATETIME,
    263            "SMALLMONEY": TokenType.SMALLMONEY,
    264            "SQL_VARIANT": TokenType.VARIANT,
    265            "TIME": TokenType.TIMESTAMP,
    266            "TOP": TokenType.TOP,
    267            "UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
    268            "VARCHAR(MAX)": TokenType.TEXT,
    269            "XML": TokenType.XML,
    270        }
    271
    272        # TSQL allows @, # to appear as a variable/identifier prefix
    273        SINGLE_TOKENS = tokens.Tokenizer.SINGLE_TOKENS.copy()
    274        SINGLE_TOKENS.pop("#")
    275
    276    class Parser(parser.Parser):
    277        FUNCTIONS = {
    278            **parser.Parser.FUNCTIONS,  # type: ignore
    279            "CHARINDEX": lambda args: exp.StrPosition(
    280                this=seq_get(args, 1),
    281                substr=seq_get(args, 0),
    282                position=seq_get(args, 2),
    283            ),
    284            "DATEADD": parse_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL),
    285            "DATEDIFF": parse_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL),
    286            "DATENAME": _format_time_lambda(exp.TimeToStr, full_format_mapping=True),
    287            "DATEPART": _format_time_lambda(exp.TimeToStr),
    288            "EOMONTH": _parse_eomonth,
    289            "FORMAT": _parse_format,
    290            "GETDATE": exp.CurrentTimestamp.from_arg_list,
    291            "IIF": exp.If.from_arg_list,
    292            "ISNULL": exp.Coalesce.from_arg_list,
    293            "JSON_VALUE": exp.JSONExtractScalar.from_arg_list,
    294            "LEN": exp.Length.from_arg_list,
    295            "REPLICATE": exp.Repeat.from_arg_list,
    296            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
    297            "SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
    298        }
    299
    300        VAR_LENGTH_DATATYPES = {
    301            DataType.Type.NVARCHAR,
    302            DataType.Type.VARCHAR,
    303            DataType.Type.CHAR,
    304            DataType.Type.NCHAR,
    305        }
    306
    307        RETURNS_TABLE_TOKENS = parser.Parser.ID_VAR_TOKENS - {  # type: ignore
    308            TokenType.TABLE,
    309            *parser.Parser.TYPE_TOKENS,  # type: ignore
    310        }
    311
    312        STATEMENT_PARSERS = {
    313            **parser.Parser.STATEMENT_PARSERS,  # type: ignore
    314            TokenType.END: lambda self: self._parse_command(),
    315        }
    316
    317        LOG_BASE_FIRST = False
    318        LOG_DEFAULTS_TO_LN = True
    319
    320        def _parse_system_time(self) -> t.Optional[exp.Expression]:
    321            if not self._match_text_seq("FOR", "SYSTEM_TIME"):
    322                return None
    323
    324            if self._match_text_seq("AS", "OF"):
    325                system_time = self.expression(
    326                    exp.SystemTime, this=self._parse_bitwise(), kind="AS OF"
    327                )
    328            elif self._match_set((TokenType.FROM, TokenType.BETWEEN)):
    329                kind = self._prev.text
    330                this = self._parse_bitwise()
    331                self._match_texts(("TO", "AND"))
    332                expression = self._parse_bitwise()
    333                system_time = self.expression(
    334                    exp.SystemTime, this=this, expression=expression, kind=kind
    335                )
    336            elif self._match_text_seq("CONTAINED", "IN"):
    337                args = self._parse_wrapped_csv(self._parse_bitwise)
    338                system_time = self.expression(
    339                    exp.SystemTime,
    340                    this=seq_get(args, 0),
    341                    expression=seq_get(args, 1),
    342                    kind="CONTAINED IN",
    343                )
    344            elif self._match(TokenType.ALL):
    345                system_time = self.expression(exp.SystemTime, kind="ALL")
    346            else:
    347                system_time = None
    348                self.raise_error("Unable to parse FOR SYSTEM_TIME clause")
    349
    350            return system_time
    351
    352        def _parse_table_parts(self, schema: bool = False) -> exp.Expression:
    353            table = super()._parse_table_parts(schema=schema)
    354            table.set("system_time", self._parse_system_time())
    355            return table
    356
    357        def _parse_returns(self) -> exp.Expression:
    358            table = self._parse_id_var(any_token=False, tokens=self.RETURNS_TABLE_TOKENS)
    359            returns = super()._parse_returns()
    360            returns.set("table", table)
    361            return returns
    362
    363        def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]:
    364            to = self._parse_types()
    365            self._match(TokenType.COMMA)
    366            this = self._parse_conjunction()
    367
    368            if not to or not this:
    369                return None
    370
    371            # Retrieve length of datatype and override to default if not specified
    372            if seq_get(to.expressions, 0) is None and to.this in self.VAR_LENGTH_DATATYPES:
    373                to = exp.DataType.build(to.this, expressions=[exp.Literal.number(30)], nested=False)
    374
    375            # Check whether a conversion with format is applicable
    376            if self._match(TokenType.COMMA):
    377                format_val = self._parse_number()
    378                format_val_name = format_val.name if format_val else ""
    379
    380                if format_val_name not in TSQL.convert_format_mapping:
    381                    raise ValueError(
    382                        f"CONVERT function at T-SQL does not support format style {format_val_name}"
    383                    )
    384
    385                format_norm = exp.Literal.string(TSQL.convert_format_mapping[format_val_name])
    386
    387                # Check whether the convert entails a string to date format
    388                if to.this == DataType.Type.DATE:
    389                    return self.expression(exp.StrToDate, this=this, format=format_norm)
    390                # Check whether the convert entails a string to datetime format
    391                elif to.this == DataType.Type.DATETIME:
    392                    return self.expression(exp.StrToTime, this=this, format=format_norm)
    393                # Check whether the convert entails a date to string format
    394                elif to.this in self.VAR_LENGTH_DATATYPES:
    395                    return self.expression(
    396                        exp.Cast if strict else exp.TryCast,
    397                        to=to,
    398                        this=self.expression(exp.TimeToStr, this=this, format=format_norm),
    399                    )
    400                elif to.this == DataType.Type.TEXT:
    401                    return self.expression(exp.TimeToStr, this=this, format=format_norm)
    402
    403            # Entails a simple cast without any format requirement
    404            return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to)
    405
    406        def _parse_user_defined_function(
    407            self, kind: t.Optional[TokenType] = None
    408        ) -> t.Optional[exp.Expression]:
    409            this = super()._parse_user_defined_function(kind=kind)
    410
    411            if (
    412                kind == TokenType.FUNCTION
    413                or isinstance(this, exp.UserDefinedFunction)
    414                or self._match(TokenType.ALIAS, advance=False)
    415            ):
    416                return this
    417
    418            expressions = self._parse_csv(self._parse_function_parameter)
    419            return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)
    420
    421    class Generator(generator.Generator):
    422        LOCKING_READS_SUPPORTED = True
    423
    424        TYPE_MAPPING = {
    425            **generator.Generator.TYPE_MAPPING,  # type: ignore
    426            exp.DataType.Type.INT: "INTEGER",
    427            exp.DataType.Type.DECIMAL: "NUMERIC",
    428            exp.DataType.Type.DATETIME: "DATETIME2",
    429            exp.DataType.Type.VARIANT: "SQL_VARIANT",
    430        }
    431
    432        TRANSFORMS = {
    433            **generator.Generator.TRANSFORMS,  # type: ignore
    434            exp.DateAdd: generate_date_delta_with_unit_sql,
    435            exp.DateDiff: generate_date_delta_with_unit_sql,
    436            exp.CurrentDate: rename_func("GETDATE"),
    437            exp.CurrentTimestamp: rename_func("GETDATE"),
    438            exp.If: rename_func("IIF"),
    439            exp.NumberToStr: _format_sql,
    440            exp.TimeToStr: _format_sql,
    441            exp.GroupConcat: _string_agg_sql,
    442            exp.Max: max_or_greatest,
    443            exp.Min: min_or_least,
    444        }
    445
    446        TRANSFORMS.pop(exp.ReturnsProperty)
    447
    448        LIMIT_FETCH = "FETCH"
    449
    450        def offset_sql(self, expression: exp.Offset) -> str:
    451            return f"{super().offset_sql(expression)} ROWS"
    452
    453        def systemtime_sql(self, expression: exp.SystemTime) -> str:
    454            kind = expression.args["kind"]
    455            if kind == "ALL":
    456                return "FOR SYSTEM_TIME ALL"
    457
    458            start = self.sql(expression, "this")
    459            if kind == "AS OF":
    460                return f"FOR SYSTEM_TIME AS OF {start}"
    461
    462            end = self.sql(expression, "expression")
    463            if kind == "FROM":
    464                return f"FOR SYSTEM_TIME FROM {start} TO {end}"
    465            if kind == "BETWEEN":
    466                return f"FOR SYSTEM_TIME BETWEEN {start} AND {end}"
    467
    468            return f"FOR SYSTEM_TIME CONTAINED IN ({start}, {end})"
    469
    470        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
    471            table = expression.args.get("table")
    472            table = f"{table} " if table else ""
    473            return f"RETURNS {table}{self.sql(expression, 'this')}"

##### Inherited Members

sqlglot.dialects.dialect.Dialect
  get_or_raise
  format_time
  parse
  parse_into
  generate
  transpile
  tokenize
  parser
  generator

class TSQL.Tokenizer(sqlglot.tokens.Tokenizer): View Source

    244    class Tokenizer(tokens.Tokenizer):
    245        IDENTIFIERS = ['"', ("[", "]")]
    246
    247        QUOTES = ["'", '"']
    248
    249        KEYWORDS = {
    250            **tokens.Tokenizer.KEYWORDS,
    251            "DATETIME2": TokenType.DATETIME,
    252            "DATETIMEOFFSET": TokenType.TIMESTAMPTZ,
    253            "DECLARE": TokenType.COMMAND,
    254            "IMAGE": TokenType.IMAGE,
    255            "MONEY": TokenType.MONEY,
    256            "NTEXT": TokenType.TEXT,
    257            "NVARCHAR(MAX)": TokenType.TEXT,
    258            "PRINT": TokenType.COMMAND,
    259            "PROC": TokenType.PROCEDURE,
    260            "REAL": TokenType.FLOAT,
    261            "ROWVERSION": TokenType.ROWVERSION,
    262            "SMALLDATETIME": TokenType.DATETIME,
    263            "SMALLMONEY": TokenType.SMALLMONEY,
    264            "SQL_VARIANT": TokenType.VARIANT,
    265            "TIME": TokenType.TIMESTAMP,
    266            "TOP": TokenType.TOP,
    267            "UNIQUEIDENTIFIER": TokenType.UNIQUEIDENTIFIER,
    268            "VARCHAR(MAX)": TokenType.TEXT,
    269            "XML": TokenType.XML,
    270        }
    271
    272        # TSQL allows @, # to appear as a variable/identifier prefix
    273        SINGLE_TOKENS = tokens.Tokenizer.SINGLE_TOKENS.copy()
    274        SINGLE_TOKENS.pop("#")

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

    276    class Parser(parser.Parser):
    277        FUNCTIONS = {
    278            **parser.Parser.FUNCTIONS,  # type: ignore
    279            "CHARINDEX": lambda args: exp.StrPosition(
    280                this=seq_get(args, 1),
    281                substr=seq_get(args, 0),
    282                position=seq_get(args, 2),
    283            ),
    284            "DATEADD": parse_date_delta(exp.DateAdd, unit_mapping=DATE_DELTA_INTERVAL),
    285            "DATEDIFF": parse_date_delta(exp.DateDiff, unit_mapping=DATE_DELTA_INTERVAL),
    286            "DATENAME": _format_time_lambda(exp.TimeToStr, full_format_mapping=True),
    287            "DATEPART": _format_time_lambda(exp.TimeToStr),
    288            "EOMONTH": _parse_eomonth,
    289            "FORMAT": _parse_format,
    290            "GETDATE": exp.CurrentTimestamp.from_arg_list,
    291            "IIF": exp.If.from_arg_list,
    292            "ISNULL": exp.Coalesce.from_arg_list,
    293            "JSON_VALUE": exp.JSONExtractScalar.from_arg_list,
    294            "LEN": exp.Length.from_arg_list,
    295            "REPLICATE": exp.Repeat.from_arg_list,
    296            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
    297            "SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
    298        }
    299
    300        VAR_LENGTH_DATATYPES = {
    301            DataType.Type.NVARCHAR,
    302            DataType.Type.VARCHAR,
    303            DataType.Type.CHAR,
    304            DataType.Type.NCHAR,
    305        }
    306
    307        RETURNS_TABLE_TOKENS = parser.Parser.ID_VAR_TOKENS - {  # type: ignore
    308            TokenType.TABLE,
    309            *parser.Parser.TYPE_TOKENS,  # type: ignore
    310        }
    311
    312        STATEMENT_PARSERS = {
    313            **parser.Parser.STATEMENT_PARSERS,  # type: ignore
    314            TokenType.END: lambda self: self._parse_command(),
    315        }
    316
    317        LOG_BASE_FIRST = False
    318        LOG_DEFAULTS_TO_LN = True
    319
    320        def _parse_system_time(self) -> t.Optional[exp.Expression]:
    321            if not self._match_text_seq("FOR", "SYSTEM_TIME"):
    322                return None
    323
    324            if self._match_text_seq("AS", "OF"):
    325                system_time = self.expression(
    326                    exp.SystemTime, this=self._parse_bitwise(), kind="AS OF"
    327                )
    328            elif self._match_set((TokenType.FROM, TokenType.BETWEEN)):
    329                kind = self._prev.text
    330                this = self._parse_bitwise()
    331                self._match_texts(("TO", "AND"))
    332                expression = self._parse_bitwise()
    333                system_time = self.expression(
    334                    exp.SystemTime, this=this, expression=expression, kind=kind
    335                )
    336            elif self._match_text_seq("CONTAINED", "IN"):
    337                args = self._parse_wrapped_csv(self._parse_bitwise)
    338                system_time = self.expression(
    339                    exp.SystemTime,
    340                    this=seq_get(args, 0),
    341                    expression=seq_get(args, 1),
    342                    kind="CONTAINED IN",
    343                )
    344            elif self._match(TokenType.ALL):
    345                system_time = self.expression(exp.SystemTime, kind="ALL")
    346            else:
    347                system_time = None
    348                self.raise_error("Unable to parse FOR SYSTEM_TIME clause")
    349
    350            return system_time
    351
    352        def _parse_table_parts(self, schema: bool = False) -> exp.Expression:
    353            table = super()._parse_table_parts(schema=schema)
    354            table.set("system_time", self._parse_system_time())
    355            return table
    356
    357        def _parse_returns(self) -> exp.Expression:
    358            table = self._parse_id_var(any_token=False, tokens=self.RETURNS_TABLE_TOKENS)
    359            returns = super()._parse_returns()
    360            returns.set("table", table)
    361            return returns
    362
    363        def _parse_convert(self, strict: bool) -> t.Optional[exp.Expression]:
    364            to = self._parse_types()
    365            self._match(TokenType.COMMA)
    366            this = self._parse_conjunction()
    367
    368            if not to or not this:
    369                return None
    370
    371            # Retrieve length of datatype and override to default if not specified
    372            if seq_get(to.expressions, 0) is None and to.this in self.VAR_LENGTH_DATATYPES:
    373                to = exp.DataType.build(to.this, expressions=[exp.Literal.number(30)], nested=False)
    374
    375            # Check whether a conversion with format is applicable
    376            if self._match(TokenType.COMMA):
    377                format_val = self._parse_number()
    378                format_val_name = format_val.name if format_val else ""
    379
    380                if format_val_name not in TSQL.convert_format_mapping:
    381                    raise ValueError(
    382                        f"CONVERT function at T-SQL does not support format style {format_val_name}"
    383                    )
    384
    385                format_norm = exp.Literal.string(TSQL.convert_format_mapping[format_val_name])
    386
    387                # Check whether the convert entails a string to date format
    388                if to.this == DataType.Type.DATE:
    389                    return self.expression(exp.StrToDate, this=this, format=format_norm)
    390                # Check whether the convert entails a string to datetime format
    391                elif to.this == DataType.Type.DATETIME:
    392                    return self.expression(exp.StrToTime, this=this, format=format_norm)
    393                # Check whether the convert entails a date to string format
    394                elif to.this in self.VAR_LENGTH_DATATYPES:
    395                    return self.expression(
    396                        exp.Cast if strict else exp.TryCast,
    397                        to=to,
    398                        this=self.expression(exp.TimeToStr, this=this, format=format_norm),
    399                    )
    400                elif to.this == DataType.Type.TEXT:
    401                    return self.expression(exp.TimeToStr, this=this, format=format_norm)
    402
    403            # Entails a simple cast without any format requirement
    404            return self.expression(exp.Cast if strict else exp.TryCast, this=this, to=to)
    405
    406        def _parse_user_defined_function(
    407            self, kind: t.Optional[TokenType] = None
    408        ) -> t.Optional[exp.Expression]:
    409            this = super()._parse_user_defined_function(kind=kind)
    410
    411            if (
    412                kind == TokenType.FUNCTION
    413                or isinstance(this, exp.UserDefinedFunction)
    414                or self._match(TokenType.ALIAS, advance=False)
    415            ):
    416                return this
    417
    418            expressions = self._parse_csv(self._parse_function_parameter)
    419            return self.expression(exp.UserDefinedFunction, this=this, expressions=expressions)

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

###### Arguments:

- error_level: the desired error level. Default: ErrorLevel.RAISE
- error_message_context: determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 50.
- index_offset: Index offset for arrays eg ARRAY[0] vs ARRAY[1] as the head of a list. Default: 0
- alias_post_tablesample: If the table alias comes after tablesample. Default: False
- max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3
- null_ordering: Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"

##### Inherited Members

sqlglot.parser.Parser
  Parser
  reset
  parse
  parse_into
  check_errors
  raise_error
  expression
  validate_expression

class TSQL.Generator(sqlglot.generator.Generator): View Source

    421    class Generator(generator.Generator):
    422        LOCKING_READS_SUPPORTED = True
    423
    424        TYPE_MAPPING = {
    425            **generator.Generator.TYPE_MAPPING,  # type: ignore
    426            exp.DataType.Type.INT: "INTEGER",
    427            exp.DataType.Type.DECIMAL: "NUMERIC",
    428            exp.DataType.Type.DATETIME: "DATETIME2",
    429            exp.DataType.Type.VARIANT: "SQL_VARIANT",
    430        }
    431
    432        TRANSFORMS = {
    433            **generator.Generator.TRANSFORMS,  # type: ignore
    434            exp.DateAdd: generate_date_delta_with_unit_sql,
    435            exp.DateDiff: generate_date_delta_with_unit_sql,
    436            exp.CurrentDate: rename_func("GETDATE"),
    437            exp.CurrentTimestamp: rename_func("GETDATE"),
    438            exp.If: rename_func("IIF"),
    439            exp.NumberToStr: _format_sql,
    440            exp.TimeToStr: _format_sql,
    441            exp.GroupConcat: _string_agg_sql,
    442            exp.Max: max_or_greatest,
    443            exp.Min: min_or_least,
    444        }
    445
    446        TRANSFORMS.pop(exp.ReturnsProperty)
    447
    448        LIMIT_FETCH = "FETCH"
    449
    450        def offset_sql(self, expression: exp.Offset) -> str:
    451            return f"{super().offset_sql(expression)} ROWS"
    452
    453        def systemtime_sql(self, expression: exp.SystemTime) -> str:
    454            kind = expression.args["kind"]
    455            if kind == "ALL":
    456                return "FOR SYSTEM_TIME ALL"
    457
    458            start = self.sql(expression, "this")
    459            if kind == "AS OF":
    460                return f"FOR SYSTEM_TIME AS OF {start}"
    461
    462            end = self.sql(expression, "expression")
    463            if kind == "FROM":
    464                return f"FOR SYSTEM_TIME FROM {start} TO {end}"
    465            if kind == "BETWEEN":
    466                return f"FOR SYSTEM_TIME BETWEEN {start} AND {end}"
    467
    468            return f"FOR SYSTEM_TIME CONTAINED IN ({start}, {end})"
    469
    470        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
    471            table = expression.args.get("table")
    472            table = f"{table} " if table else ""
    473            return f"RETURNS {table}{self.sql(expression, 'this')}"

Generator interprets the given syntax tree and produces a SQL string as an output.

###### Arguments:

- time_mapping (dict): the dictionary of custom time mappings in which the key represents a python time format and the output the target time format
- time_trie (trie): a trie of the time_mapping keys
- pretty (bool): if set to True the returned string will be formatted. Default: False.
- quote_start (str): specifies which starting character to use to delimit quotes. Default: '.
- quote_end (str): specifies which ending character to use to delimit quotes. Default: '.
- identifier_start (str): specifies which starting character to use to delimit identifiers. Default: ".
- identifier_end (str): specifies which ending character to use to delimit identifiers. Default: ".
- identify (bool | str): 'always': always quote, 'safe': quote identifiers if they don't contain an upcase, True defaults to always.
- normalize (bool): if set to True all identifiers will lower cased
- string_escape (str): specifies a string escape character. Default: '.
- identifier_escape (str): specifies an identifier escape character. Default: ".
- pad (int): determines padding in a formatted string. Default: 2.
- indent (int): determines the size of indentation in a formatted string. Default: 4.
- unnest_column_only (bool): if true unnest table aliases are considered only as column aliases
- normalize_functions (str): normalize function names, "upper", "lower", or None Default: "upper"
- alias_post_tablesample (bool): if the table alias comes after tablesample Default: False
- unsupported_level (ErrorLevel): determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- null_ordering (str): Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
- max_unsupported (int): Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
- leading_comma (bool): if the the comma is leading or trailing in select statements Default: False
- max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
- comments: Whether or not to preserve comments in the output SQL code. Default: True

def offset_sql(self, expression: sqlglot.expressions.Offset) -> str: View Source

    450        def offset_sql(self, expression: exp.Offset) -> str:
    451            return f"{super().offset_sql(expression)} ROWS"

def systemtime_sql(self, expression: sqlglot.expressions.SystemTime) -> str: View Source

    453        def systemtime_sql(self, expression: exp.SystemTime) -> str:
    454            kind = expression.args["kind"]
    455            if kind == "ALL":
    456                return "FOR SYSTEM_TIME ALL"
    457
    458            start = self.sql(expression, "this")
    459            if kind == "AS OF":
    460                return f"FOR SYSTEM_TIME AS OF {start}"
    461
    462            end = self.sql(expression, "expression")
    463            if kind == "FROM":
    464                return f"FOR SYSTEM_TIME FROM {start} TO {end}"
    465            if kind == "BETWEEN":
    466                return f"FOR SYSTEM_TIME BETWEEN {start} AND {end}"
    467
    468            return f"FOR SYSTEM_TIME CONTAINED IN ({start}, {end})"

def returnsproperty_sql(self, expression: sqlglot.expressions.ReturnsProperty) -> str: View Source

    470        def returnsproperty_sql(self, expression: exp.ReturnsProperty) -> str:
    471            table = expression.args.get("table")
    472            table = f"{table} " if table else ""
    473            return f"RETURNS {table}{self.sql(expression, 'this')}"

##### Inherited Members

sqlglot.generator.Generator
  Generator
  generate
  unsupported
  sep
  seg
  pad_comment
  maybe_comment
  wrap
  no_identify
  normalize_func
  indent
  sql
  uncache_sql
  cache_sql
  characterset_sql
  column_sql
  columnposition_sql
  columndef_sql
  columnconstraint_sql
  autoincrementcolumnconstraint_sql
  compresscolumnconstraint_sql
  generatedasidentitycolumnconstraint_sql
  notnullcolumnconstraint_sql
  primarykeycolumnconstraint_sql
  uniquecolumnconstraint_sql
  create_sql
  describe_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  except_op
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  afterjournalproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  intersect_op
  introducer_sql
  pseudotype_sql
  returning_sql
  rowformatdelimitedproperty_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  setitem_sql
  set_sql
  pragma_sql
  lock_sql
  literal_sql
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  select_sql
  schema_sql
  star_sql
  structkwarg_sql
  parameter_sql
  sessionparameter_sql
  placeholder_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  window_spec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  extract_sql
  trim_sql
  concat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  unique_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  cast_sql
  currentdate_sql
  collate_sql
  command_sql
  comment_sql
  transaction_sql
  commit_sql
  rollback_sql
  altercolumn_sql
  renametable_sql
  altertable_sql
  droppartition_sql
  addconstraint_sql
  distinct_sql
  ignorenulls_sql
  respectnulls_sql
  intdiv_sql
  dpipe_sql
  div_sql
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  ilikeany_sql
  is_sql
  like_sql
  likeany_sql
  similarto_sql
  lt_sql
  lte_sql
  mod_sql
  mul_sql
  neq_sql
  nullsafeeq_sql
  nullsafeneq_sql
  or_sql
  slice_sql
  sub_sql
  trycast_sql
  use_sql
  binary
  function_fallback_sql
  func
  format_args
  text_width
  format_time
  expressions
  op_expressions
  naked_property
  set_operation
  tag_sql
  token_sql
  userdefinedfunction_sql
  joinhint_sql
  kwarg_sql
  when_sql
  merge_sql
  tochar_sql