sqlglot.dialects.snowflake API documentation

Edit on GitHub

# sqlglot.dialects.snowflake

View Source

      1from __future__ import annotations
      2
      3import typing as t
      4
      5from sqlglot import exp, generator, parser, tokens, transforms
      6from sqlglot.dialects.dialect import (
      7    Dialect,
      8    date_trunc_to_time,
      9    datestrtodate_sql,
     10    format_time_lambda,
     11    inline_array_sql,
     12    max_or_greatest,
     13    min_or_least,
     14    rename_func,
     15    timestamptrunc_sql,
     16    timestrtotime_sql,
     17    ts_or_ds_to_date_sql,
     18    var_map_sql,
     19)
     20from sqlglot.expressions import Literal
     21from sqlglot.helper import seq_get
     22from sqlglot.parser import binary_range_parser
     23from sqlglot.tokens import TokenType
     24
     25
     26def _check_int(s: str) -> bool:
     27    if s[0] in ("-", "+"):
     28        return s[1:].isdigit()
     29    return s.isdigit()
     30
     31
     32# from https://docs.snowflake.com/en/sql-reference/functions/to_timestamp.html
     33def _snowflake_to_timestamp(args: t.List) -> t.Union[exp.StrToTime, exp.UnixToTime]:
     34    if len(args) == 2:
     35        first_arg, second_arg = args
     36        if second_arg.is_string:
     37            # case: <string_expr> [ , <format> ]
     38            return format_time_lambda(exp.StrToTime, "snowflake")(args)
     39
     40        # case: <numeric_expr> [ , <scale> ]
     41        if second_arg.name not in ["0", "3", "9"]:
     42            raise ValueError(
     43                f"Scale for snowflake numeric timestamp is {second_arg}, but should be 0, 3, or 9"
     44            )
     45
     46        if second_arg.name == "0":
     47            timescale = exp.UnixToTime.SECONDS
     48        elif second_arg.name == "3":
     49            timescale = exp.UnixToTime.MILLIS
     50        elif second_arg.name == "9":
     51            timescale = exp.UnixToTime.MICROS
     52
     53        return exp.UnixToTime(this=first_arg, scale=timescale)
     54
     55    from sqlglot.optimizer.simplify import simplify_literals
     56
     57    # The first argument might be an expression like 40 * 365 * 86400, so we try to
     58    # reduce it using `simplify_literals` first and then check if it's a Literal.
     59    first_arg = seq_get(args, 0)
     60    if not isinstance(simplify_literals(first_arg, root=True), Literal):
     61        # case: <variant_expr>
     62        return format_time_lambda(exp.StrToTime, "snowflake", default=True)(args)
     63
     64    if first_arg.is_string:
     65        if _check_int(first_arg.this):
     66            # case: <integer>
     67            return exp.UnixToTime.from_arg_list(args)
     68
     69        # case: <date_expr>
     70        return format_time_lambda(exp.StrToTime, "snowflake", default=True)(args)
     71
     72    # case: <numeric_expr>
     73    return exp.UnixToTime.from_arg_list(args)
     74
     75
     76def _unix_to_time_sql(self: generator.Generator, expression: exp.UnixToTime) -> str:
     77    scale = expression.args.get("scale")
     78    timestamp = self.sql(expression, "this")
     79    if scale in [None, exp.UnixToTime.SECONDS]:
     80        return f"TO_TIMESTAMP({timestamp})"
     81    if scale == exp.UnixToTime.MILLIS:
     82        return f"TO_TIMESTAMP({timestamp}, 3)"
     83    if scale == exp.UnixToTime.MICROS:
     84        return f"TO_TIMESTAMP({timestamp}, 9)"
     85
     86    raise ValueError("Improper scale for timestamp")
     87
     88
     89# https://docs.snowflake.com/en/sql-reference/functions/date_part.html
     90# https://docs.snowflake.com/en/sql-reference/functions-date-time.html#label-supported-date-time-parts
     91def _parse_date_part(self: parser.Parser) -> t.Optional[exp.Expression]:
     92    this = self._parse_var() or self._parse_type()
     93
     94    if not this:
     95        return None
     96
     97    self._match(TokenType.COMMA)
     98    expression = self._parse_bitwise()
     99
    100    name = this.name.upper()
    101    if name.startswith("EPOCH"):
    102        if name.startswith("EPOCH_MILLISECOND"):
    103            scale = 10**3
    104        elif name.startswith("EPOCH_MICROSECOND"):
    105            scale = 10**6
    106        elif name.startswith("EPOCH_NANOSECOND"):
    107            scale = 10**9
    108        else:
    109            scale = None
    110
    111        ts = self.expression(exp.Cast, this=expression, to=exp.DataType.build("TIMESTAMP"))
    112        to_unix: exp.Expression = self.expression(exp.TimeToUnix, this=ts)
    113
    114        if scale:
    115            to_unix = exp.Mul(this=to_unix, expression=exp.Literal.number(scale))
    116
    117        return to_unix
    118
    119    return self.expression(exp.Extract, this=this, expression=expression)
    120
    121
    122# https://docs.snowflake.com/en/sql-reference/functions/div0
    123def _div0_to_if(args: t.List) -> exp.Expression:
    124    cond = exp.EQ(this=seq_get(args, 1), expression=exp.Literal.number(0))
    125    true = exp.Literal.number(0)
    126    false = exp.Div(this=seq_get(args, 0), expression=seq_get(args, 1))
    127    return exp.If(this=cond, true=true, false=false)
    128
    129
    130# https://docs.snowflake.com/en/sql-reference/functions/zeroifnull
    131def _zeroifnull_to_if(args: t.List) -> exp.Expression:
    132    cond = exp.Is(this=seq_get(args, 0), expression=exp.Null())
    133    return exp.If(this=cond, true=exp.Literal.number(0), false=seq_get(args, 0))
    134
    135
    136# https://docs.snowflake.com/en/sql-reference/functions/zeroifnull
    137def _nullifzero_to_if(args: t.List) -> exp.Expression:
    138    cond = exp.EQ(this=seq_get(args, 0), expression=exp.Literal.number(0))
    139    return exp.If(this=cond, true=exp.Null(), false=seq_get(args, 0))
    140
    141
    142def _datatype_sql(self: generator.Generator, expression: exp.DataType) -> str:
    143    if expression.this == exp.DataType.Type.ARRAY:
    144        return "ARRAY"
    145    elif expression.this == exp.DataType.Type.MAP:
    146        return "OBJECT"
    147    return self.datatype_sql(expression)
    148
    149
    150def _parse_convert_timezone(args: t.List) -> exp.Expression:
    151    if len(args) == 3:
    152        return exp.Anonymous(this="CONVERT_TIMEZONE", expressions=args)
    153    return exp.AtTimeZone(this=seq_get(args, 1), zone=seq_get(args, 0))
    154
    155
    156class Snowflake(Dialect):
    157    null_ordering = "nulls_are_large"
    158    time_format = "'yyyy-mm-dd hh24:mi:ss'"
    159
    160    time_mapping = {
    161        "YYYY": "%Y",
    162        "yyyy": "%Y",
    163        "YY": "%y",
    164        "yy": "%y",
    165        "MMMM": "%B",
    166        "mmmm": "%B",
    167        "MON": "%b",
    168        "mon": "%b",
    169        "MM": "%m",
    170        "mm": "%m",
    171        "DD": "%d",
    172        "dd": "%-d",
    173        "DY": "%a",
    174        "dy": "%w",
    175        "HH24": "%H",
    176        "hh24": "%H",
    177        "HH12": "%I",
    178        "hh12": "%I",
    179        "MI": "%M",
    180        "mi": "%M",
    181        "SS": "%S",
    182        "ss": "%S",
    183        "FF": "%f",
    184        "ff": "%f",
    185        "FF6": "%f",
    186        "ff6": "%f",
    187    }
    188
    189    class Parser(parser.Parser):
    190        IDENTIFY_PIVOT_STRINGS = True
    191
    192        FUNCTIONS = {
    193            **parser.Parser.FUNCTIONS,
    194            "ARRAYAGG": exp.ArrayAgg.from_arg_list,
    195            "ARRAY_CONSTRUCT": exp.Array.from_arg_list,
    196            "ARRAY_TO_STRING": exp.ArrayJoin.from_arg_list,
    197            "CONVERT_TIMEZONE": _parse_convert_timezone,
    198            "DATE_TRUNC": date_trunc_to_time,
    199            "DATEADD": lambda args: exp.DateAdd(
    200                this=seq_get(args, 2),
    201                expression=seq_get(args, 1),
    202                unit=seq_get(args, 0),
    203            ),
    204            "DATEDIFF": lambda args: exp.DateDiff(
    205                this=seq_get(args, 2),
    206                expression=seq_get(args, 1),
    207                unit=seq_get(args, 0),
    208            ),
    209            "DIV0": _div0_to_if,
    210            "IFF": exp.If.from_arg_list,
    211            "NULLIFZERO": _nullifzero_to_if,
    212            "OBJECT_CONSTRUCT": parser.parse_var_map,
    213            "RLIKE": exp.RegexpLike.from_arg_list,
    214            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
    215            "TO_ARRAY": exp.Array.from_arg_list,
    216            "TO_VARCHAR": exp.ToChar.from_arg_list,
    217            "TO_TIMESTAMP": _snowflake_to_timestamp,
    218            "ZEROIFNULL": _zeroifnull_to_if,
    219        }
    220
    221        FUNCTION_PARSERS = {
    222            **parser.Parser.FUNCTION_PARSERS,
    223            "DATE_PART": _parse_date_part,
    224        }
    225        FUNCTION_PARSERS.pop("TRIM")
    226
    227        FUNC_TOKENS = {
    228            *parser.Parser.FUNC_TOKENS,
    229            TokenType.RLIKE,
    230            TokenType.TABLE,
    231        }
    232
    233        COLUMN_OPERATORS = {
    234            **parser.Parser.COLUMN_OPERATORS,
    235            TokenType.COLON: lambda self, this, path: self.expression(
    236                exp.Bracket,
    237                this=this,
    238                expressions=[path],
    239            ),
    240        }
    241
    242        TIMESTAMPS = parser.Parser.TIMESTAMPS.copy() - {TokenType.TIME}
    243
    244        RANGE_PARSERS = {
    245            **parser.Parser.RANGE_PARSERS,
    246            TokenType.LIKE_ANY: binary_range_parser(exp.LikeAny),
    247            TokenType.ILIKE_ANY: binary_range_parser(exp.ILikeAny),
    248        }
    249
    250        ALTER_PARSERS = {
    251            **parser.Parser.ALTER_PARSERS,
    252            "UNSET": lambda self: self._parse_alter_table_set_tag(unset=True),
    253            "SET": lambda self: self._parse_alter_table_set_tag(),
    254        }
    255
    256        def _parse_alter_table_set_tag(self, unset: bool = False) -> exp.Expression:
    257            self._match_text_seq("TAG")
    258            parser = t.cast(t.Callable, self._parse_id_var if unset else self._parse_conjunction)
    259            return self.expression(exp.SetTag, expressions=self._parse_csv(parser), unset=unset)
    260
    261    class Tokenizer(tokens.Tokenizer):
    262        QUOTES = ["'", "$$"]
    263        STRING_ESCAPES = ["\\", "'"]
    264        HEX_STRINGS = [("x'", "'"), ("X'", "'")]
    265
    266        KEYWORDS = {
    267            **tokens.Tokenizer.KEYWORDS,
    268            "CHAR VARYING": TokenType.VARCHAR,
    269            "CHARACTER VARYING": TokenType.VARCHAR,
    270            "EXCLUDE": TokenType.EXCEPT,
    271            "ILIKE ANY": TokenType.ILIKE_ANY,
    272            "LIKE ANY": TokenType.LIKE_ANY,
    273            "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE,
    274            "MINUS": TokenType.EXCEPT,
    275            "NCHAR VARYING": TokenType.VARCHAR,
    276            "PUT": TokenType.COMMAND,
    277            "RENAME": TokenType.REPLACE,
    278            "TIMESTAMP_LTZ": TokenType.TIMESTAMPLTZ,
    279            "TIMESTAMP_NTZ": TokenType.TIMESTAMP,
    280            "TIMESTAMP_TZ": TokenType.TIMESTAMPTZ,
    281            "TIMESTAMPNTZ": TokenType.TIMESTAMP,
    282            "SAMPLE": TokenType.TABLE_SAMPLE,
    283        }
    284
    285        SINGLE_TOKENS = {
    286            **tokens.Tokenizer.SINGLE_TOKENS,
    287            "$": TokenType.PARAMETER,
    288        }
    289
    290        VAR_SINGLE_TOKENS = {"$"}
    291
    292    class Generator(generator.Generator):
    293        PARAMETER_TOKEN = "$"
    294        MATCHED_BY_SOURCE = False
    295        SINGLE_STRING_INTERVAL = True
    296        JOIN_HINTS = False
    297        TABLE_HINTS = False
    298
    299        TRANSFORMS = {
    300            **generator.Generator.TRANSFORMS,
    301            exp.Array: inline_array_sql,
    302            exp.ArrayConcat: rename_func("ARRAY_CAT"),
    303            exp.ArrayJoin: rename_func("ARRAY_TO_STRING"),
    304            exp.AtTimeZone: lambda self, e: self.func(
    305                "CONVERT_TIMEZONE", e.args.get("zone"), e.this
    306            ),
    307            exp.DateAdd: lambda self, e: self.func("DATEADD", e.text("unit"), e.expression, e.this),
    308            exp.DateDiff: lambda self, e: self.func(
    309                "DATEDIFF", e.text("unit"), e.expression, e.this
    310            ),
    311            exp.DateStrToDate: datestrtodate_sql,
    312            exp.DataType: _datatype_sql,
    313            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    314            exp.Extract: rename_func("DATE_PART"),
    315            exp.If: rename_func("IFF"),
    316            exp.LogicalAnd: rename_func("BOOLAND_AGG"),
    317            exp.LogicalOr: rename_func("BOOLOR_AGG"),
    318            exp.Map: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
    319            exp.Max: max_or_greatest,
    320            exp.Min: min_or_least,
    321            exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
    322            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
    323            exp.StarMap: rename_func("OBJECT_CONSTRUCT"),
    324            exp.StrPosition: lambda self, e: self.func(
    325                "POSITION", e.args.get("substr"), e.this, e.args.get("position")
    326            ),
    327            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
    328            exp.TimeStrToTime: timestrtotime_sql,
    329            exp.TimeToUnix: lambda self, e: f"EXTRACT(epoch_second FROM {self.sql(e, 'this')})",
    330            exp.TimeToStr: lambda self, e: self.func(
    331                "TO_CHAR", exp.cast(e.this, "timestamp"), self.format_time(e)
    332            ),
    333            exp.TimestampTrunc: timestamptrunc_sql,
    334            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
    335            exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression),
    336            exp.TsOrDsToDate: ts_or_ds_to_date_sql("snowflake"),
    337            exp.UnixToTime: _unix_to_time_sql,
    338            exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
    339        }
    340
    341        TYPE_MAPPING = {
    342            **generator.Generator.TYPE_MAPPING,
    343            exp.DataType.Type.TIMESTAMP: "TIMESTAMPNTZ",
    344        }
    345
    346        STAR_MAPPING = {
    347            "except": "EXCLUDE",
    348            "replace": "RENAME",
    349        }
    350
    351        PROPERTIES_LOCATION = {
    352            **generator.Generator.PROPERTIES_LOCATION,
    353            exp.SetProperty: exp.Properties.Location.UNSUPPORTED,
    354            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    355        }
    356
    357        def except_op(self, expression: exp.Except) -> str:
    358            if not expression.args.get("distinct", False):
    359                self.unsupported("EXCEPT with All is not supported in Snowflake")
    360            return super().except_op(expression)
    361
    362        def intersect_op(self, expression: exp.Intersect) -> str:
    363            if not expression.args.get("distinct", False):
    364                self.unsupported("INTERSECT with All is not supported in Snowflake")
    365            return super().intersect_op(expression)
    366
    367        def settag_sql(self, expression: exp.SetTag) -> str:
    368            action = "UNSET" if expression.args.get("unset") else "SET"
    369            return f"{action} TAG {self.expressions(expression)}"
    370
    371        def describe_sql(self, expression: exp.Describe) -> str:
    372            # Default to table if kind is unknown
    373            kind_value = expression.args.get("kind") or "TABLE"
    374            kind = f" {kind_value}" if kind_value else ""
    375            this = f" {self.sql(expression, 'this')}"
    376            return f"DESCRIBE{kind}{this}"
    377
    378        def generatedasidentitycolumnconstraint_sql(
    379            self, expression: exp.GeneratedAsIdentityColumnConstraint
    380        ) -> str:
    381            start = expression.args.get("start")
    382            start = f" START {start}" if start else ""
    383            increment = expression.args.get("increment")
    384            increment = f" INCREMENT {increment}" if increment else ""
    385            return f"AUTOINCREMENT{start}{increment}"

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

    157class Snowflake(Dialect):
    158    null_ordering = "nulls_are_large"
    159    time_format = "'yyyy-mm-dd hh24:mi:ss'"
    160
    161    time_mapping = {
    162        "YYYY": "%Y",
    163        "yyyy": "%Y",
    164        "YY": "%y",
    165        "yy": "%y",
    166        "MMMM": "%B",
    167        "mmmm": "%B",
    168        "MON": "%b",
    169        "mon": "%b",
    170        "MM": "%m",
    171        "mm": "%m",
    172        "DD": "%d",
    173        "dd": "%-d",
    174        "DY": "%a",
    175        "dy": "%w",
    176        "HH24": "%H",
    177        "hh24": "%H",
    178        "HH12": "%I",
    179        "hh12": "%I",
    180        "MI": "%M",
    181        "mi": "%M",
    182        "SS": "%S",
    183        "ss": "%S",
    184        "FF": "%f",
    185        "ff": "%f",
    186        "FF6": "%f",
    187        "ff6": "%f",
    188    }
    189
    190    class Parser(parser.Parser):
    191        IDENTIFY_PIVOT_STRINGS = True
    192
    193        FUNCTIONS = {
    194            **parser.Parser.FUNCTIONS,
    195            "ARRAYAGG": exp.ArrayAgg.from_arg_list,
    196            "ARRAY_CONSTRUCT": exp.Array.from_arg_list,
    197            "ARRAY_TO_STRING": exp.ArrayJoin.from_arg_list,
    198            "CONVERT_TIMEZONE": _parse_convert_timezone,
    199            "DATE_TRUNC": date_trunc_to_time,
    200            "DATEADD": lambda args: exp.DateAdd(
    201                this=seq_get(args, 2),
    202                expression=seq_get(args, 1),
    203                unit=seq_get(args, 0),
    204            ),
    205            "DATEDIFF": lambda args: exp.DateDiff(
    206                this=seq_get(args, 2),
    207                expression=seq_get(args, 1),
    208                unit=seq_get(args, 0),
    209            ),
    210            "DIV0": _div0_to_if,
    211            "IFF": exp.If.from_arg_list,
    212            "NULLIFZERO": _nullifzero_to_if,
    213            "OBJECT_CONSTRUCT": parser.parse_var_map,
    214            "RLIKE": exp.RegexpLike.from_arg_list,
    215            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
    216            "TO_ARRAY": exp.Array.from_arg_list,
    217            "TO_VARCHAR": exp.ToChar.from_arg_list,
    218            "TO_TIMESTAMP": _snowflake_to_timestamp,
    219            "ZEROIFNULL": _zeroifnull_to_if,
    220        }
    221
    222        FUNCTION_PARSERS = {
    223            **parser.Parser.FUNCTION_PARSERS,
    224            "DATE_PART": _parse_date_part,
    225        }
    226        FUNCTION_PARSERS.pop("TRIM")
    227
    228        FUNC_TOKENS = {
    229            *parser.Parser.FUNC_TOKENS,
    230            TokenType.RLIKE,
    231            TokenType.TABLE,
    232        }
    233
    234        COLUMN_OPERATORS = {
    235            **parser.Parser.COLUMN_OPERATORS,
    236            TokenType.COLON: lambda self, this, path: self.expression(
    237                exp.Bracket,
    238                this=this,
    239                expressions=[path],
    240            ),
    241        }
    242
    243        TIMESTAMPS = parser.Parser.TIMESTAMPS.copy() - {TokenType.TIME}
    244
    245        RANGE_PARSERS = {
    246            **parser.Parser.RANGE_PARSERS,
    247            TokenType.LIKE_ANY: binary_range_parser(exp.LikeAny),
    248            TokenType.ILIKE_ANY: binary_range_parser(exp.ILikeAny),
    249        }
    250
    251        ALTER_PARSERS = {
    252            **parser.Parser.ALTER_PARSERS,
    253            "UNSET": lambda self: self._parse_alter_table_set_tag(unset=True),
    254            "SET": lambda self: self._parse_alter_table_set_tag(),
    255        }
    256
    257        def _parse_alter_table_set_tag(self, unset: bool = False) -> exp.Expression:
    258            self._match_text_seq("TAG")
    259            parser = t.cast(t.Callable, self._parse_id_var if unset else self._parse_conjunction)
    260            return self.expression(exp.SetTag, expressions=self._parse_csv(parser), unset=unset)
    261
    262    class Tokenizer(tokens.Tokenizer):
    263        QUOTES = ["'", "$$"]
    264        STRING_ESCAPES = ["\\", "'"]
    265        HEX_STRINGS = [("x'", "'"), ("X'", "'")]
    266
    267        KEYWORDS = {
    268            **tokens.Tokenizer.KEYWORDS,
    269            "CHAR VARYING": TokenType.VARCHAR,
    270            "CHARACTER VARYING": TokenType.VARCHAR,
    271            "EXCLUDE": TokenType.EXCEPT,
    272            "ILIKE ANY": TokenType.ILIKE_ANY,
    273            "LIKE ANY": TokenType.LIKE_ANY,
    274            "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE,
    275            "MINUS": TokenType.EXCEPT,
    276            "NCHAR VARYING": TokenType.VARCHAR,
    277            "PUT": TokenType.COMMAND,
    278            "RENAME": TokenType.REPLACE,
    279            "TIMESTAMP_LTZ": TokenType.TIMESTAMPLTZ,
    280            "TIMESTAMP_NTZ": TokenType.TIMESTAMP,
    281            "TIMESTAMP_TZ": TokenType.TIMESTAMPTZ,
    282            "TIMESTAMPNTZ": TokenType.TIMESTAMP,
    283            "SAMPLE": TokenType.TABLE_SAMPLE,
    284        }
    285
    286        SINGLE_TOKENS = {
    287            **tokens.Tokenizer.SINGLE_TOKENS,
    288            "$": TokenType.PARAMETER,
    289        }
    290
    291        VAR_SINGLE_TOKENS = {"$"}
    292
    293    class Generator(generator.Generator):
    294        PARAMETER_TOKEN = "$"
    295        MATCHED_BY_SOURCE = False
    296        SINGLE_STRING_INTERVAL = True
    297        JOIN_HINTS = False
    298        TABLE_HINTS = False
    299
    300        TRANSFORMS = {
    301            **generator.Generator.TRANSFORMS,
    302            exp.Array: inline_array_sql,
    303            exp.ArrayConcat: rename_func("ARRAY_CAT"),
    304            exp.ArrayJoin: rename_func("ARRAY_TO_STRING"),
    305            exp.AtTimeZone: lambda self, e: self.func(
    306                "CONVERT_TIMEZONE", e.args.get("zone"), e.this
    307            ),
    308            exp.DateAdd: lambda self, e: self.func("DATEADD", e.text("unit"), e.expression, e.this),
    309            exp.DateDiff: lambda self, e: self.func(
    310                "DATEDIFF", e.text("unit"), e.expression, e.this
    311            ),
    312            exp.DateStrToDate: datestrtodate_sql,
    313            exp.DataType: _datatype_sql,
    314            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    315            exp.Extract: rename_func("DATE_PART"),
    316            exp.If: rename_func("IFF"),
    317            exp.LogicalAnd: rename_func("BOOLAND_AGG"),
    318            exp.LogicalOr: rename_func("BOOLOR_AGG"),
    319            exp.Map: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
    320            exp.Max: max_or_greatest,
    321            exp.Min: min_or_least,
    322            exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
    323            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
    324            exp.StarMap: rename_func("OBJECT_CONSTRUCT"),
    325            exp.StrPosition: lambda self, e: self.func(
    326                "POSITION", e.args.get("substr"), e.this, e.args.get("position")
    327            ),
    328            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
    329            exp.TimeStrToTime: timestrtotime_sql,
    330            exp.TimeToUnix: lambda self, e: f"EXTRACT(epoch_second FROM {self.sql(e, 'this')})",
    331            exp.TimeToStr: lambda self, e: self.func(
    332                "TO_CHAR", exp.cast(e.this, "timestamp"), self.format_time(e)
    333            ),
    334            exp.TimestampTrunc: timestamptrunc_sql,
    335            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
    336            exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression),
    337            exp.TsOrDsToDate: ts_or_ds_to_date_sql("snowflake"),
    338            exp.UnixToTime: _unix_to_time_sql,
    339            exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
    340        }
    341
    342        TYPE_MAPPING = {
    343            **generator.Generator.TYPE_MAPPING,
    344            exp.DataType.Type.TIMESTAMP: "TIMESTAMPNTZ",
    345        }
    346
    347        STAR_MAPPING = {
    348            "except": "EXCLUDE",
    349            "replace": "RENAME",
    350        }
    351
    352        PROPERTIES_LOCATION = {
    353            **generator.Generator.PROPERTIES_LOCATION,
    354            exp.SetProperty: exp.Properties.Location.UNSUPPORTED,
    355            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    356        }
    357
    358        def except_op(self, expression: exp.Except) -> str:
    359            if not expression.args.get("distinct", False):
    360                self.unsupported("EXCEPT with All is not supported in Snowflake")
    361            return super().except_op(expression)
    362
    363        def intersect_op(self, expression: exp.Intersect) -> str:
    364            if not expression.args.get("distinct", False):
    365                self.unsupported("INTERSECT with All is not supported in Snowflake")
    366            return super().intersect_op(expression)
    367
    368        def settag_sql(self, expression: exp.SetTag) -> str:
    369            action = "UNSET" if expression.args.get("unset") else "SET"
    370            return f"{action} TAG {self.expressions(expression)}"
    371
    372        def describe_sql(self, expression: exp.Describe) -> str:
    373            # Default to table if kind is unknown
    374            kind_value = expression.args.get("kind") or "TABLE"
    375            kind = f" {kind_value}" if kind_value else ""
    376            this = f" {self.sql(expression, 'this')}"
    377            return f"DESCRIBE{kind}{this}"
    378
    379        def generatedasidentitycolumnconstraint_sql(
    380            self, expression: exp.GeneratedAsIdentityColumnConstraint
    381        ) -> str:
    382            start = expression.args.get("start")
    383            start = f" START {start}" if start else ""
    384            increment = expression.args.get("increment")
    385            increment = f" INCREMENT {increment}" if increment else ""
    386            return f"AUTOINCREMENT{start}{increment}"

##### Inherited Members

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

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

    190    class Parser(parser.Parser):
    191        IDENTIFY_PIVOT_STRINGS = True
    192
    193        FUNCTIONS = {
    194            **parser.Parser.FUNCTIONS,
    195            "ARRAYAGG": exp.ArrayAgg.from_arg_list,
    196            "ARRAY_CONSTRUCT": exp.Array.from_arg_list,
    197            "ARRAY_TO_STRING": exp.ArrayJoin.from_arg_list,
    198            "CONVERT_TIMEZONE": _parse_convert_timezone,
    199            "DATE_TRUNC": date_trunc_to_time,
    200            "DATEADD": lambda args: exp.DateAdd(
    201                this=seq_get(args, 2),
    202                expression=seq_get(args, 1),
    203                unit=seq_get(args, 0),
    204            ),
    205            "DATEDIFF": lambda args: exp.DateDiff(
    206                this=seq_get(args, 2),
    207                expression=seq_get(args, 1),
    208                unit=seq_get(args, 0),
    209            ),
    210            "DIV0": _div0_to_if,
    211            "IFF": exp.If.from_arg_list,
    212            "NULLIFZERO": _nullifzero_to_if,
    213            "OBJECT_CONSTRUCT": parser.parse_var_map,
    214            "RLIKE": exp.RegexpLike.from_arg_list,
    215            "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
    216            "TO_ARRAY": exp.Array.from_arg_list,
    217            "TO_VARCHAR": exp.ToChar.from_arg_list,
    218            "TO_TIMESTAMP": _snowflake_to_timestamp,
    219            "ZEROIFNULL": _zeroifnull_to_if,
    220        }
    221
    222        FUNCTION_PARSERS = {
    223            **parser.Parser.FUNCTION_PARSERS,
    224            "DATE_PART": _parse_date_part,
    225        }
    226        FUNCTION_PARSERS.pop("TRIM")
    227
    228        FUNC_TOKENS = {
    229            *parser.Parser.FUNC_TOKENS,
    230            TokenType.RLIKE,
    231            TokenType.TABLE,
    232        }
    233
    234        COLUMN_OPERATORS = {
    235            **parser.Parser.COLUMN_OPERATORS,
    236            TokenType.COLON: lambda self, this, path: self.expression(
    237                exp.Bracket,
    238                this=this,
    239                expressions=[path],
    240            ),
    241        }
    242
    243        TIMESTAMPS = parser.Parser.TIMESTAMPS.copy() - {TokenType.TIME}
    244
    245        RANGE_PARSERS = {
    246            **parser.Parser.RANGE_PARSERS,
    247            TokenType.LIKE_ANY: binary_range_parser(exp.LikeAny),
    248            TokenType.ILIKE_ANY: binary_range_parser(exp.ILikeAny),
    249        }
    250
    251        ALTER_PARSERS = {
    252            **parser.Parser.ALTER_PARSERS,
    253            "UNSET": lambda self: self._parse_alter_table_set_tag(unset=True),
    254            "SET": lambda self: self._parse_alter_table_set_tag(),
    255        }
    256
    257        def _parse_alter_table_set_tag(self, unset: bool = False) -> exp.Expression:
    258            self._match_text_seq("TAG")
    259            parser = t.cast(t.Callable, self._parse_id_var if unset else self._parse_conjunction)
    260            return self.expression(exp.SetTag, expressions=self._parse_csv(parser), unset=unset)

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 Snowflake.Tokenizer(sqlglot.tokens.Tokenizer): View Source

    262    class Tokenizer(tokens.Tokenizer):
    263        QUOTES = ["'", "$$"]
    264        STRING_ESCAPES = ["\\", "'"]
    265        HEX_STRINGS = [("x'", "'"), ("X'", "'")]
    266
    267        KEYWORDS = {
    268            **tokens.Tokenizer.KEYWORDS,
    269            "CHAR VARYING": TokenType.VARCHAR,
    270            "CHARACTER VARYING": TokenType.VARCHAR,
    271            "EXCLUDE": TokenType.EXCEPT,
    272            "ILIKE ANY": TokenType.ILIKE_ANY,
    273            "LIKE ANY": TokenType.LIKE_ANY,
    274            "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE,
    275            "MINUS": TokenType.EXCEPT,
    276            "NCHAR VARYING": TokenType.VARCHAR,
    277            "PUT": TokenType.COMMAND,
    278            "RENAME": TokenType.REPLACE,
    279            "TIMESTAMP_LTZ": TokenType.TIMESTAMPLTZ,
    280            "TIMESTAMP_NTZ": TokenType.TIMESTAMP,
    281            "TIMESTAMP_TZ": TokenType.TIMESTAMPTZ,
    282            "TIMESTAMPNTZ": TokenType.TIMESTAMP,
    283            "SAMPLE": TokenType.TABLE_SAMPLE,
    284        }
    285
    286        SINGLE_TOKENS = {
    287            **tokens.Tokenizer.SINGLE_TOKENS,
    288            "$": TokenType.PARAMETER,
    289        }
    290
    291        VAR_SINGLE_TOKENS = {"$"}

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

    293    class Generator(generator.Generator):
    294        PARAMETER_TOKEN = "$"
    295        MATCHED_BY_SOURCE = False
    296        SINGLE_STRING_INTERVAL = True
    297        JOIN_HINTS = False
    298        TABLE_HINTS = False
    299
    300        TRANSFORMS = {
    301            **generator.Generator.TRANSFORMS,
    302            exp.Array: inline_array_sql,
    303            exp.ArrayConcat: rename_func("ARRAY_CAT"),
    304            exp.ArrayJoin: rename_func("ARRAY_TO_STRING"),
    305            exp.AtTimeZone: lambda self, e: self.func(
    306                "CONVERT_TIMEZONE", e.args.get("zone"), e.this
    307            ),
    308            exp.DateAdd: lambda self, e: self.func("DATEADD", e.text("unit"), e.expression, e.this),
    309            exp.DateDiff: lambda self, e: self.func(
    310                "DATEDIFF", e.text("unit"), e.expression, e.this
    311            ),
    312            exp.DateStrToDate: datestrtodate_sql,
    313            exp.DataType: _datatype_sql,
    314            exp.DayOfWeek: rename_func("DAYOFWEEK"),
    315            exp.Extract: rename_func("DATE_PART"),
    316            exp.If: rename_func("IFF"),
    317            exp.LogicalAnd: rename_func("BOOLAND_AGG"),
    318            exp.LogicalOr: rename_func("BOOLOR_AGG"),
    319            exp.Map: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
    320            exp.Max: max_or_greatest,
    321            exp.Min: min_or_least,
    322            exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}",
    323            exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
    324            exp.StarMap: rename_func("OBJECT_CONSTRUCT"),
    325            exp.StrPosition: lambda self, e: self.func(
    326                "POSITION", e.args.get("substr"), e.this, e.args.get("position")
    327            ),
    328            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
    329            exp.TimeStrToTime: timestrtotime_sql,
    330            exp.TimeToUnix: lambda self, e: f"EXTRACT(epoch_second FROM {self.sql(e, 'this')})",
    331            exp.TimeToStr: lambda self, e: self.func(
    332                "TO_CHAR", exp.cast(e.this, "timestamp"), self.format_time(e)
    333            ),
    334            exp.TimestampTrunc: timestamptrunc_sql,
    335            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
    336            exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression),
    337            exp.TsOrDsToDate: ts_or_ds_to_date_sql("snowflake"),
    338            exp.UnixToTime: _unix_to_time_sql,
    339            exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"),
    340        }
    341
    342        TYPE_MAPPING = {
    343            **generator.Generator.TYPE_MAPPING,
    344            exp.DataType.Type.TIMESTAMP: "TIMESTAMPNTZ",
    345        }
    346
    347        STAR_MAPPING = {
    348            "except": "EXCLUDE",
    349            "replace": "RENAME",
    350        }
    351
    352        PROPERTIES_LOCATION = {
    353            **generator.Generator.PROPERTIES_LOCATION,
    354            exp.SetProperty: exp.Properties.Location.UNSUPPORTED,
    355            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
    356        }
    357
    358        def except_op(self, expression: exp.Except) -> str:
    359            if not expression.args.get("distinct", False):
    360                self.unsupported("EXCEPT with All is not supported in Snowflake")
    361            return super().except_op(expression)
    362
    363        def intersect_op(self, expression: exp.Intersect) -> str:
    364            if not expression.args.get("distinct", False):
    365                self.unsupported("INTERSECT with All is not supported in Snowflake")
    366            return super().intersect_op(expression)
    367
    368        def settag_sql(self, expression: exp.SetTag) -> str:
    369            action = "UNSET" if expression.args.get("unset") else "SET"
    370            return f"{action} TAG {self.expressions(expression)}"
    371
    372        def describe_sql(self, expression: exp.Describe) -> str:
    373            # Default to table if kind is unknown
    374            kind_value = expression.args.get("kind") or "TABLE"
    375            kind = f" {kind_value}" if kind_value else ""
    376            this = f" {self.sql(expression, 'this')}"
    377            return f"DESCRIBE{kind}{this}"
    378
    379        def generatedasidentitycolumnconstraint_sql(
    380            self, expression: exp.GeneratedAsIdentityColumnConstraint
    381        ) -> str:
    382            start = expression.args.get("start")
    383            start = f" START {start}" if start else ""
    384            increment = expression.args.get("increment")
    385            increment = f" INCREMENT {increment}" if increment else ""
    386            return f"AUTOINCREMENT{start}{increment}"

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: ".
- bit_start (str): specifies which starting character to use to delimit bit literals. Default: None.
- bit_end (str): specifies which ending character to use to delimit bit literals. Default: None.
- hex_start (str): specifies which starting character to use to delimit hex literals. Default: None.
- hex_end (str): specifies which ending character to use to delimit hex literals. Default: None.
- byte_start (str): specifies which starting character to use to delimit byte literals. Default: None.
- byte_end (str): specifies which ending character to use to delimit byte literals. Default: None.
- raw_start (str): specifies which starting character to use to delimit raw literals. Default: None.
- raw_end (str): specifies which ending character to use to delimit raw literals. Default: None.
- 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 except_op(self, expression: sqlglot.expressions.Except) -> str: View Source

    358        def except_op(self, expression: exp.Except) -> str:
    359            if not expression.args.get("distinct", False):
    360                self.unsupported("EXCEPT with All is not supported in Snowflake")
    361            return super().except_op(expression)

def intersect_op(self, expression: sqlglot.expressions.Intersect) -> str: View Source

    363        def intersect_op(self, expression: exp.Intersect) -> str:
    364            if not expression.args.get("distinct", False):
    365                self.unsupported("INTERSECT with All is not supported in Snowflake")
    366            return super().intersect_op(expression)

def settag_sql(self, expression: sqlglot.expressions.SetTag) -> str: View Source

    368        def settag_sql(self, expression: exp.SetTag) -> str:
    369            action = "UNSET" if expression.args.get("unset") else "SET"
    370            return f"{action} TAG {self.expressions(expression)}"

def describe_sql(self, expression: sqlglot.expressions.Describe) -> str: View Source

    372        def describe_sql(self, expression: exp.Describe) -> str:
    373            # Default to table if kind is unknown
    374            kind_value = expression.args.get("kind") or "TABLE"
    375            kind = f" {kind_value}" if kind_value else ""
    376            this = f" {self.sql(expression, 'this')}"
    377            return f"DESCRIBE{kind}{this}"

def generatedasidentitycolumnconstraint_sql( self, expression: sqlglot.expressions.GeneratedAsIdentityColumnConstraint) -> str: View Source

    379        def generatedasidentitycolumnconstraint_sql(
    380            self, expression: exp.GeneratedAsIdentityColumnConstraint
    381        ) -> str:
    382            start = expression.args.get("start")
    383            start = f" START {start}" if start else ""
    384            increment = expression.args.get("increment")
    385            increment = f" INCREMENT {increment}" if increment else ""
    386            return f"AUTOINCREMENT{start}{increment}"

##### 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
  notnullcolumnconstraint_sql
  primarykeycolumnconstraint_sql
  uniquecolumnconstraint_sql
  create_sql
  clone_sql
  prepend_ctes
  with_sql
  cte_sql
  tablealias_sql
  bitstring_sql
  hexstring_sql
  bytestring_sql
  rawstring_sql
  datatypesize_sql
  datatype_sql
  directory_sql
  delete_sql
  drop_sql
  except_sql
  fetch_sql
  filter_sql
  hint_sql
  index_sql
  identifier_sql
  inputoutputformat_sql
  national_sql
  partition_sql
  properties_sql
  root_properties
  properties
  with_properties
  locate_properties
  property_sql
  likeproperty_sql
  fallbackproperty_sql
  journalproperty_sql
  freespaceproperty_sql
  checksumproperty_sql
  mergeblockratioproperty_sql
  datablocksizeproperty_sql
  blockcompressionproperty_sql
  isolatedloadingproperty_sql
  lockingproperty_sql
  withdataproperty_sql
  insert_sql
  intersect_sql
  introducer_sql
  pseudotype_sql
  onconflict_sql
  returning_sql
  rowformatdelimitedproperty_sql
  table_sql
  tablesample_sql
  pivot_sql
  tuple_sql
  update_sql
  values_sql
  var_sql
  into_sql
  from_sql
  group_sql
  having_sql
  join_sql
  lambda_sql
  lateral_sql
  limit_sql
  offset_sql
  setitem_sql
  set_sql
  pragma_sql
  lock_sql
  literal_sql
  loaddata_sql
  null_sql
  boolean_sql
  order_sql
  cluster_sql
  distribute_sql
  sort_sql
  ordered_sql
  matchrecognize_sql
  query_modifiers
  after_having_modifiers
  after_limit_modifiers
  select_sql
  schema_sql
  star_sql
  parameter_sql
  sessionparameter_sql
  placeholder_sql
  subquery_sql
  qualify_sql
  union_sql
  union_op
  unnest_sql
  where_sql
  window_sql
  partition_by_sql
  windowspec_sql
  withingroup_sql
  between_sql
  bracket_sql
  all_sql
  any_sql
  exists_sql
  case_sql
  constraint_sql
  nextvaluefor_sql
  extract_sql
  trim_sql
  concat_sql
  check_sql
  foreignkey_sql
  primarykey_sql
  unique_sql
  if_sql
  matchagainst_sql
  jsonkeyvalue_sql
  jsonobject_sql
  openjsoncolumndef_sql
  openjson_sql
  in_sql
  in_unnest_op
  interval_sql
  return_sql
  reference_sql
  anonymous_sql
  paren_sql
  neg_sql
  not_sql
  alias_sql
  aliases_sql
  attimezone_sql
  add_sql
  and_sql
  connector_sql
  bitwiseand_sql
  bitwiseleftshift_sql
  bitwisenot_sql
  bitwiseor_sql
  bitwiserightshift_sql
  bitwisexor_sql
  cast_sql
  currentdate_sql
  collate_sql
  command_sql
  comment_sql
  mergetreettlaction_sql
  mergetreettl_sql
  transaction_sql
  commit_sql
  rollback_sql
  altercolumn_sql
  renametable_sql
  altertable_sql
  droppartition_sql
  addconstraint_sql
  distinct_sql
  ignorenulls_sql
  respectnulls_sql
  intdiv_sql
  dpipe_sql
  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