sqlglot.dialects.presto API documentation

Edit on GitHub

# sqlglot.dialects.presto

View Source

      1from __future__ import annotations
      2
      3from sqlglot import exp, generator, parser, tokens, transforms
      4from sqlglot.dialects.dialect import (
      5    Dialect,
      6    format_time_lambda,
      7    if_sql,
      8    no_ilike_sql,
      9    no_safe_divide_sql,
     10    rename_func,
     11    struct_extract_sql,
     12    timestrtotime_sql,
     13)
     14from sqlglot.dialects.mysql import MySQL
     15from sqlglot.errors import UnsupportedError
     16from sqlglot.helper import seq_get
     17from sqlglot.tokens import TokenType
     18
     19
     20def _approx_distinct_sql(self, expression):
     21    accuracy = expression.args.get("accuracy")
     22    accuracy = ", " + self.sql(accuracy) if accuracy else ""
     23    return f"APPROX_DISTINCT({self.sql(expression, 'this')}{accuracy})"
     24
     25
     26def _datatype_sql(self, expression):
     27    sql = self.datatype_sql(expression)
     28    if expression.this == exp.DataType.Type.TIMESTAMPTZ:
     29        sql = f"{sql} WITH TIME ZONE"
     30    return sql
     31
     32
     33def _explode_to_unnest_sql(self, expression):
     34    if isinstance(expression.this, (exp.Explode, exp.Posexplode)):
     35        return self.sql(
     36            exp.Join(
     37                this=exp.Unnest(
     38                    expressions=[expression.this.this],
     39                    alias=expression.args.get("alias"),
     40                    ordinality=isinstance(expression.this, exp.Posexplode),
     41                ),
     42                kind="cross",
     43            )
     44        )
     45    return self.lateral_sql(expression)
     46
     47
     48def _initcap_sql(self, expression):
     49    regex = r"(\w)(\w*)"
     50    return f"REGEXP_REPLACE({self.sql(expression, 'this')}, '{regex}', x -> UPPER(x[1]) || LOWER(x[2]))"
     51
     52
     53def _decode_sql(self, expression):
     54    _ensure_utf8(expression.args.get("charset"))
     55    return self.func("FROM_UTF8", expression.this, expression.args.get("replace"))
     56
     57
     58def _encode_sql(self, expression):
     59    _ensure_utf8(expression.args.get("charset"))
     60    return f"TO_UTF8({self.sql(expression, 'this')})"
     61
     62
     63def _no_sort_array(self, expression):
     64    if expression.args.get("asc") == exp.false():
     65        comparator = "(a, b) -> CASE WHEN a < b THEN 1 WHEN a > b THEN -1 ELSE 0 END"
     66    else:
     67        comparator = None
     68    return self.func("ARRAY_SORT", expression.this, comparator)
     69
     70
     71def _schema_sql(self, expression):
     72    if isinstance(expression.parent, exp.Property):
     73        columns = ", ".join(f"'{c.name}'" for c in expression.expressions)
     74        return f"ARRAY[{columns}]"
     75
     76    for schema in expression.parent.find_all(exp.Schema):
     77        if isinstance(schema.parent, exp.Property):
     78            expression = expression.copy()
     79            expression.expressions.extend(schema.expressions)
     80
     81    return self.schema_sql(expression)
     82
     83
     84def _quantile_sql(self, expression):
     85    self.unsupported("Presto does not support exact quantiles")
     86    return f"APPROX_PERCENTILE({self.sql(expression, 'this')}, {self.sql(expression, 'quantile')})"
     87
     88
     89def _str_to_time_sql(self, expression):
     90    return f"DATE_PARSE({self.sql(expression, 'this')}, {self.format_time(expression)})"
     91
     92
     93def _ts_or_ds_to_date_sql(self, expression):
     94    time_format = self.format_time(expression)
     95    if time_format and time_format not in (Presto.time_format, Presto.date_format):
     96        return f"CAST({_str_to_time_sql(self, expression)} AS DATE)"
     97    return f"CAST(SUBSTR(CAST({self.sql(expression, 'this')} AS VARCHAR), 1, 10) AS DATE)"
     98
     99
    100def _ts_or_ds_add_sql(self, expression):
    101    this = self.sql(expression, "this")
    102    e = self.sql(expression, "expression")
    103    unit = self.sql(expression, "unit") or "'day'"
    104    return f"DATE_ADD({unit}, {e}, DATE_PARSE(SUBSTR({this}, 1, 10), {Presto.date_format}))"
    105
    106
    107def _sequence_sql(self, expression):
    108    start = expression.args["start"]
    109    end = expression.args["end"]
    110    step = expression.args.get("step", 1)  # Postgres defaults to 1 for generate_series
    111
    112    target_type = None
    113
    114    if isinstance(start, exp.Cast):
    115        target_type = start.to
    116    elif isinstance(end, exp.Cast):
    117        target_type = end.to
    118
    119    if target_type and target_type.this == exp.DataType.Type.TIMESTAMP:
    120        to = target_type.copy()
    121
    122        if target_type is start.to:
    123            end = exp.Cast(this=end, to=to)
    124        else:
    125            start = exp.Cast(this=start, to=to)
    126
    127    return self.func("SEQUENCE", start, end, step)
    128
    129
    130def _ensure_utf8(charset):
    131    if charset.name.lower() != "utf-8":
    132        raise UnsupportedError(f"Unsupported charset {charset}")
    133
    134
    135def _approx_percentile(args):
    136    if len(args) == 4:
    137        return exp.ApproxQuantile(
    138            this=seq_get(args, 0),
    139            weight=seq_get(args, 1),
    140            quantile=seq_get(args, 2),
    141            accuracy=seq_get(args, 3),
    142        )
    143    if len(args) == 3:
    144        return exp.ApproxQuantile(
    145            this=seq_get(args, 0),
    146            quantile=seq_get(args, 1),
    147            accuracy=seq_get(args, 2),
    148        )
    149    return exp.ApproxQuantile.from_arg_list(args)
    150
    151
    152def _from_unixtime(args):
    153    if len(args) == 3:
    154        return exp.UnixToTime(
    155            this=seq_get(args, 0),
    156            hours=seq_get(args, 1),
    157            minutes=seq_get(args, 2),
    158        )
    159    if len(args) == 2:
    160        return exp.UnixToTime(
    161            this=seq_get(args, 0),
    162            zone=seq_get(args, 1),
    163        )
    164    return exp.UnixToTime.from_arg_list(args)
    165
    166
    167class Presto(Dialect):
    168    index_offset = 1
    169    null_ordering = "nulls_are_last"
    170    time_format = MySQL.time_format  # type: ignore
    171    time_mapping = MySQL.time_mapping  # type: ignore
    172
    173    class Tokenizer(tokens.Tokenizer):
    174        KEYWORDS = {
    175            **tokens.Tokenizer.KEYWORDS,
    176            "START": TokenType.BEGIN,
    177            "ROW": TokenType.STRUCT,
    178        }
    179
    180    class Parser(parser.Parser):
    181        FUNCTIONS = {
    182            **parser.Parser.FUNCTIONS,  # type: ignore
    183            "APPROX_DISTINCT": exp.ApproxDistinct.from_arg_list,
    184            "CARDINALITY": exp.ArraySize.from_arg_list,
    185            "CONTAINS": exp.ArrayContains.from_arg_list,
    186            "DATE_ADD": lambda args: exp.DateAdd(
    187                this=seq_get(args, 2),
    188                expression=seq_get(args, 1),
    189                unit=seq_get(args, 0),
    190            ),
    191            "DATE_DIFF": lambda args: exp.DateDiff(
    192                this=seq_get(args, 2),
    193                expression=seq_get(args, 1),
    194                unit=seq_get(args, 0),
    195            ),
    196            "DATE_FORMAT": format_time_lambda(exp.TimeToStr, "presto"),
    197            "DATE_PARSE": format_time_lambda(exp.StrToTime, "presto"),
    198            "FROM_UNIXTIME": _from_unixtime,
    199            "NOW": exp.CurrentTimestamp.from_arg_list,
    200            "STRPOS": lambda args: exp.StrPosition(
    201                this=seq_get(args, 0),
    202                substr=seq_get(args, 1),
    203                instance=seq_get(args, 2),
    204            ),
    205            "TO_UNIXTIME": exp.TimeToUnix.from_arg_list,
    206            "APPROX_PERCENTILE": _approx_percentile,
    207            "FROM_HEX": exp.Unhex.from_arg_list,
    208            "TO_HEX": exp.Hex.from_arg_list,
    209            "TO_UTF8": lambda args: exp.Encode(
    210                this=seq_get(args, 0), charset=exp.Literal.string("utf-8")
    211            ),
    212            "FROM_UTF8": lambda args: exp.Decode(
    213                this=seq_get(args, 0), replace=seq_get(args, 1), charset=exp.Literal.string("utf-8")
    214            ),
    215        }
    216        FUNCTION_PARSERS = parser.Parser.FUNCTION_PARSERS.copy()
    217        FUNCTION_PARSERS.pop("TRIM")
    218
    219    class Generator(generator.Generator):
    220        STRUCT_DELIMITER = ("(", ")")
    221
    222        PROPERTIES_LOCATION = {
    223            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
    224            exp.LocationProperty: exp.Properties.Location.UNSUPPORTED,
    225        }
    226
    227        TYPE_MAPPING = {
    228            **generator.Generator.TYPE_MAPPING,  # type: ignore
    229            exp.DataType.Type.INT: "INTEGER",
    230            exp.DataType.Type.FLOAT: "REAL",
    231            exp.DataType.Type.BINARY: "VARBINARY",
    232            exp.DataType.Type.TEXT: "VARCHAR",
    233            exp.DataType.Type.TIMESTAMPTZ: "TIMESTAMP",
    234            exp.DataType.Type.STRUCT: "ROW",
    235        }
    236
    237        TRANSFORMS = {
    238            **generator.Generator.TRANSFORMS,  # type: ignore
    239            **transforms.UNALIAS_GROUP,  # type: ignore
    240            exp.ApproxDistinct: _approx_distinct_sql,
    241            exp.Array: lambda self, e: f"ARRAY[{self.expressions(e, flat=True)}]",
    242            exp.ArrayConcat: rename_func("CONCAT"),
    243            exp.ArrayContains: rename_func("CONTAINS"),
    244            exp.ArraySize: rename_func("CARDINALITY"),
    245            exp.BitwiseAnd: lambda self, e: f"BITWISE_AND({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    246            exp.BitwiseLeftShift: lambda self, e: f"BITWISE_ARITHMETIC_SHIFT_LEFT({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    247            exp.BitwiseNot: lambda self, e: f"BITWISE_NOT({self.sql(e, 'this')})",
    248            exp.BitwiseOr: lambda self, e: f"BITWISE_OR({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    249            exp.BitwiseRightShift: lambda self, e: f"BITWISE_ARITHMETIC_SHIFT_RIGHT({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    250            exp.BitwiseXor: lambda self, e: f"BITWISE_XOR({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    251            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
    252            exp.DataType: _datatype_sql,
    253            exp.DateAdd: lambda self, e: f"""DATE_ADD({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""",
    254            exp.DateDiff: lambda self, e: f"""DATE_DIFF({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""",
    255            exp.DateStrToDate: lambda self, e: f"CAST(DATE_PARSE({self.sql(e, 'this')}, {Presto.date_format}) AS DATE)",
    256            exp.DateToDi: lambda self, e: f"CAST(DATE_FORMAT({self.sql(e, 'this')}, {Presto.dateint_format}) AS INT)",
    257            exp.Decode: _decode_sql,
    258            exp.DiToDate: lambda self, e: f"CAST(DATE_PARSE(CAST({self.sql(e, 'this')} AS VARCHAR), {Presto.dateint_format}) AS DATE)",
    259            exp.Encode: _encode_sql,
    260            exp.GenerateSeries: _sequence_sql,
    261            exp.Hex: rename_func("TO_HEX"),
    262            exp.If: if_sql,
    263            exp.ILike: no_ilike_sql,
    264            exp.Initcap: _initcap_sql,
    265            exp.Lateral: _explode_to_unnest_sql,
    266            exp.Levenshtein: rename_func("LEVENSHTEIN_DISTANCE"),
    267            exp.LogicalOr: rename_func("BOOL_OR"),
    268            exp.Quantile: _quantile_sql,
    269            exp.ApproxQuantile: rename_func("APPROX_PERCENTILE"),
    270            exp.SafeDivide: no_safe_divide_sql,
    271            exp.Schema: _schema_sql,
    272            exp.SortArray: _no_sort_array,
    273            exp.StrPosition: rename_func("STRPOS"),
    274            exp.StrToDate: lambda self, e: f"CAST({_str_to_time_sql(self, e)} AS DATE)",
    275            exp.StrToTime: _str_to_time_sql,
    276            exp.StrToUnix: lambda self, e: f"TO_UNIXTIME(DATE_PARSE({self.sql(e, 'this')}, {self.format_time(e)}))",
    277            exp.StructExtract: struct_extract_sql,
    278            exp.TableFormatProperty: lambda self, e: f"TABLE_FORMAT='{e.name.upper()}'",
    279            exp.FileFormatProperty: lambda self, e: f"FORMAT='{e.name.upper()}'",
    280            exp.TimeStrToDate: timestrtotime_sql,
    281            exp.TimeStrToTime: timestrtotime_sql,
    282            exp.TimeStrToUnix: lambda self, e: f"TO_UNIXTIME(DATE_PARSE({self.sql(e, 'this')}, {Presto.time_format}))",
    283            exp.TimeToStr: lambda self, e: f"DATE_FORMAT({self.sql(e, 'this')}, {self.format_time(e)})",
    284            exp.TimeToUnix: rename_func("TO_UNIXTIME"),
    285            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS VARCHAR), '-', ''), 1, 8) AS INT)",
    286            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    287            exp.TsOrDsToDate: _ts_or_ds_to_date_sql,
    288            exp.Unhex: rename_func("FROM_HEX"),
    289            exp.UnixToStr: lambda self, e: f"DATE_FORMAT(FROM_UNIXTIME({self.sql(e, 'this')}), {self.format_time(e)})",
    290            exp.UnixToTime: rename_func("FROM_UNIXTIME"),
    291            exp.UnixToTimeStr: lambda self, e: f"CAST(FROM_UNIXTIME({self.sql(e, 'this')}) AS VARCHAR)",
    292            exp.VariancePop: rename_func("VAR_POP"),
    293        }
    294
    295        def transaction_sql(self, expression):
    296            modes = expression.args.get("modes")
    297            modes = f" {', '.join(modes)}" if modes else ""
    298            return f"START TRANSACTION{modes}"

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

    168class Presto(Dialect):
    169    index_offset = 1
    170    null_ordering = "nulls_are_last"
    171    time_format = MySQL.time_format  # type: ignore
    172    time_mapping = MySQL.time_mapping  # type: ignore
    173
    174    class Tokenizer(tokens.Tokenizer):
    175        KEYWORDS = {
    176            **tokens.Tokenizer.KEYWORDS,
    177            "START": TokenType.BEGIN,
    178            "ROW": TokenType.STRUCT,
    179        }
    180
    181    class Parser(parser.Parser):
    182        FUNCTIONS = {
    183            **parser.Parser.FUNCTIONS,  # type: ignore
    184            "APPROX_DISTINCT": exp.ApproxDistinct.from_arg_list,
    185            "CARDINALITY": exp.ArraySize.from_arg_list,
    186            "CONTAINS": exp.ArrayContains.from_arg_list,
    187            "DATE_ADD": lambda args: exp.DateAdd(
    188                this=seq_get(args, 2),
    189                expression=seq_get(args, 1),
    190                unit=seq_get(args, 0),
    191            ),
    192            "DATE_DIFF": lambda args: exp.DateDiff(
    193                this=seq_get(args, 2),
    194                expression=seq_get(args, 1),
    195                unit=seq_get(args, 0),
    196            ),
    197            "DATE_FORMAT": format_time_lambda(exp.TimeToStr, "presto"),
    198            "DATE_PARSE": format_time_lambda(exp.StrToTime, "presto"),
    199            "FROM_UNIXTIME": _from_unixtime,
    200            "NOW": exp.CurrentTimestamp.from_arg_list,
    201            "STRPOS": lambda args: exp.StrPosition(
    202                this=seq_get(args, 0),
    203                substr=seq_get(args, 1),
    204                instance=seq_get(args, 2),
    205            ),
    206            "TO_UNIXTIME": exp.TimeToUnix.from_arg_list,
    207            "APPROX_PERCENTILE": _approx_percentile,
    208            "FROM_HEX": exp.Unhex.from_arg_list,
    209            "TO_HEX": exp.Hex.from_arg_list,
    210            "TO_UTF8": lambda args: exp.Encode(
    211                this=seq_get(args, 0), charset=exp.Literal.string("utf-8")
    212            ),
    213            "FROM_UTF8": lambda args: exp.Decode(
    214                this=seq_get(args, 0), replace=seq_get(args, 1), charset=exp.Literal.string("utf-8")
    215            ),
    216        }
    217        FUNCTION_PARSERS = parser.Parser.FUNCTION_PARSERS.copy()
    218        FUNCTION_PARSERS.pop("TRIM")
    219
    220    class Generator(generator.Generator):
    221        STRUCT_DELIMITER = ("(", ")")
    222
    223        PROPERTIES_LOCATION = {
    224            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
    225            exp.LocationProperty: exp.Properties.Location.UNSUPPORTED,
    226        }
    227
    228        TYPE_MAPPING = {
    229            **generator.Generator.TYPE_MAPPING,  # type: ignore
    230            exp.DataType.Type.INT: "INTEGER",
    231            exp.DataType.Type.FLOAT: "REAL",
    232            exp.DataType.Type.BINARY: "VARBINARY",
    233            exp.DataType.Type.TEXT: "VARCHAR",
    234            exp.DataType.Type.TIMESTAMPTZ: "TIMESTAMP",
    235            exp.DataType.Type.STRUCT: "ROW",
    236        }
    237
    238        TRANSFORMS = {
    239            **generator.Generator.TRANSFORMS,  # type: ignore
    240            **transforms.UNALIAS_GROUP,  # type: ignore
    241            exp.ApproxDistinct: _approx_distinct_sql,
    242            exp.Array: lambda self, e: f"ARRAY[{self.expressions(e, flat=True)}]",
    243            exp.ArrayConcat: rename_func("CONCAT"),
    244            exp.ArrayContains: rename_func("CONTAINS"),
    245            exp.ArraySize: rename_func("CARDINALITY"),
    246            exp.BitwiseAnd: lambda self, e: f"BITWISE_AND({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    247            exp.BitwiseLeftShift: lambda self, e: f"BITWISE_ARITHMETIC_SHIFT_LEFT({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    248            exp.BitwiseNot: lambda self, e: f"BITWISE_NOT({self.sql(e, 'this')})",
    249            exp.BitwiseOr: lambda self, e: f"BITWISE_OR({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    250            exp.BitwiseRightShift: lambda self, e: f"BITWISE_ARITHMETIC_SHIFT_RIGHT({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    251            exp.BitwiseXor: lambda self, e: f"BITWISE_XOR({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    252            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
    253            exp.DataType: _datatype_sql,
    254            exp.DateAdd: lambda self, e: f"""DATE_ADD({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""",
    255            exp.DateDiff: lambda self, e: f"""DATE_DIFF({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""",
    256            exp.DateStrToDate: lambda self, e: f"CAST(DATE_PARSE({self.sql(e, 'this')}, {Presto.date_format}) AS DATE)",
    257            exp.DateToDi: lambda self, e: f"CAST(DATE_FORMAT({self.sql(e, 'this')}, {Presto.dateint_format}) AS INT)",
    258            exp.Decode: _decode_sql,
    259            exp.DiToDate: lambda self, e: f"CAST(DATE_PARSE(CAST({self.sql(e, 'this')} AS VARCHAR), {Presto.dateint_format}) AS DATE)",
    260            exp.Encode: _encode_sql,
    261            exp.GenerateSeries: _sequence_sql,
    262            exp.Hex: rename_func("TO_HEX"),
    263            exp.If: if_sql,
    264            exp.ILike: no_ilike_sql,
    265            exp.Initcap: _initcap_sql,
    266            exp.Lateral: _explode_to_unnest_sql,
    267            exp.Levenshtein: rename_func("LEVENSHTEIN_DISTANCE"),
    268            exp.LogicalOr: rename_func("BOOL_OR"),
    269            exp.Quantile: _quantile_sql,
    270            exp.ApproxQuantile: rename_func("APPROX_PERCENTILE"),
    271            exp.SafeDivide: no_safe_divide_sql,
    272            exp.Schema: _schema_sql,
    273            exp.SortArray: _no_sort_array,
    274            exp.StrPosition: rename_func("STRPOS"),
    275            exp.StrToDate: lambda self, e: f"CAST({_str_to_time_sql(self, e)} AS DATE)",
    276            exp.StrToTime: _str_to_time_sql,
    277            exp.StrToUnix: lambda self, e: f"TO_UNIXTIME(DATE_PARSE({self.sql(e, 'this')}, {self.format_time(e)}))",
    278            exp.StructExtract: struct_extract_sql,
    279            exp.TableFormatProperty: lambda self, e: f"TABLE_FORMAT='{e.name.upper()}'",
    280            exp.FileFormatProperty: lambda self, e: f"FORMAT='{e.name.upper()}'",
    281            exp.TimeStrToDate: timestrtotime_sql,
    282            exp.TimeStrToTime: timestrtotime_sql,
    283            exp.TimeStrToUnix: lambda self, e: f"TO_UNIXTIME(DATE_PARSE({self.sql(e, 'this')}, {Presto.time_format}))",
    284            exp.TimeToStr: lambda self, e: f"DATE_FORMAT({self.sql(e, 'this')}, {self.format_time(e)})",
    285            exp.TimeToUnix: rename_func("TO_UNIXTIME"),
    286            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS VARCHAR), '-', ''), 1, 8) AS INT)",
    287            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    288            exp.TsOrDsToDate: _ts_or_ds_to_date_sql,
    289            exp.Unhex: rename_func("FROM_HEX"),
    290            exp.UnixToStr: lambda self, e: f"DATE_FORMAT(FROM_UNIXTIME({self.sql(e, 'this')}), {self.format_time(e)})",
    291            exp.UnixToTime: rename_func("FROM_UNIXTIME"),
    292            exp.UnixToTimeStr: lambda self, e: f"CAST(FROM_UNIXTIME({self.sql(e, 'this')}) AS VARCHAR)",
    293            exp.VariancePop: rename_func("VAR_POP"),
    294        }
    295
    296        def transaction_sql(self, expression):
    297            modes = expression.args.get("modes")
    298            modes = f" {', '.join(modes)}" if modes else ""
    299            return f"START TRANSACTION{modes}"

##### Inherited Members

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

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

    174    class Tokenizer(tokens.Tokenizer):
    175        KEYWORDS = {
    176            **tokens.Tokenizer.KEYWORDS,
    177            "START": TokenType.BEGIN,
    178            "ROW": TokenType.STRUCT,
    179        }

##### Inherited Members

sqlglot.tokens.Tokenizer
  reset
  tokenize

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

    181    class Parser(parser.Parser):
    182        FUNCTIONS = {
    183            **parser.Parser.FUNCTIONS,  # type: ignore
    184            "APPROX_DISTINCT": exp.ApproxDistinct.from_arg_list,
    185            "CARDINALITY": exp.ArraySize.from_arg_list,
    186            "CONTAINS": exp.ArrayContains.from_arg_list,
    187            "DATE_ADD": lambda args: exp.DateAdd(
    188                this=seq_get(args, 2),
    189                expression=seq_get(args, 1),
    190                unit=seq_get(args, 0),
    191            ),
    192            "DATE_DIFF": lambda args: exp.DateDiff(
    193                this=seq_get(args, 2),
    194                expression=seq_get(args, 1),
    195                unit=seq_get(args, 0),
    196            ),
    197            "DATE_FORMAT": format_time_lambda(exp.TimeToStr, "presto"),
    198            "DATE_PARSE": format_time_lambda(exp.StrToTime, "presto"),
    199            "FROM_UNIXTIME": _from_unixtime,
    200            "NOW": exp.CurrentTimestamp.from_arg_list,
    201            "STRPOS": lambda args: exp.StrPosition(
    202                this=seq_get(args, 0),
    203                substr=seq_get(args, 1),
    204                instance=seq_get(args, 2),
    205            ),
    206            "TO_UNIXTIME": exp.TimeToUnix.from_arg_list,
    207            "APPROX_PERCENTILE": _approx_percentile,
    208            "FROM_HEX": exp.Unhex.from_arg_list,
    209            "TO_HEX": exp.Hex.from_arg_list,
    210            "TO_UTF8": lambda args: exp.Encode(
    211                this=seq_get(args, 0), charset=exp.Literal.string("utf-8")
    212            ),
    213            "FROM_UTF8": lambda args: exp.Decode(
    214                this=seq_get(args, 0), replace=seq_get(args, 1), charset=exp.Literal.string("utf-8")
    215            ),
    216        }
    217        FUNCTION_PARSERS = parser.Parser.FUNCTION_PARSERS.copy()
    218        FUNCTION_PARSERS.pop("TRIM")

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 Presto.Generator(sqlglot.generator.Generator): View Source

    220    class Generator(generator.Generator):
    221        STRUCT_DELIMITER = ("(", ")")
    222
    223        PROPERTIES_LOCATION = {
    224            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
    225            exp.LocationProperty: exp.Properties.Location.UNSUPPORTED,
    226        }
    227
    228        TYPE_MAPPING = {
    229            **generator.Generator.TYPE_MAPPING,  # type: ignore
    230            exp.DataType.Type.INT: "INTEGER",
    231            exp.DataType.Type.FLOAT: "REAL",
    232            exp.DataType.Type.BINARY: "VARBINARY",
    233            exp.DataType.Type.TEXT: "VARCHAR",
    234            exp.DataType.Type.TIMESTAMPTZ: "TIMESTAMP",
    235            exp.DataType.Type.STRUCT: "ROW",
    236        }
    237
    238        TRANSFORMS = {
    239            **generator.Generator.TRANSFORMS,  # type: ignore
    240            **transforms.UNALIAS_GROUP,  # type: ignore
    241            exp.ApproxDistinct: _approx_distinct_sql,
    242            exp.Array: lambda self, e: f"ARRAY[{self.expressions(e, flat=True)}]",
    243            exp.ArrayConcat: rename_func("CONCAT"),
    244            exp.ArrayContains: rename_func("CONTAINS"),
    245            exp.ArraySize: rename_func("CARDINALITY"),
    246            exp.BitwiseAnd: lambda self, e: f"BITWISE_AND({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    247            exp.BitwiseLeftShift: lambda self, e: f"BITWISE_ARITHMETIC_SHIFT_LEFT({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    248            exp.BitwiseNot: lambda self, e: f"BITWISE_NOT({self.sql(e, 'this')})",
    249            exp.BitwiseOr: lambda self, e: f"BITWISE_OR({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    250            exp.BitwiseRightShift: lambda self, e: f"BITWISE_ARITHMETIC_SHIFT_RIGHT({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    251            exp.BitwiseXor: lambda self, e: f"BITWISE_XOR({self.sql(e, 'this')}, {self.sql(e, 'expression')})",
    252            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
    253            exp.DataType: _datatype_sql,
    254            exp.DateAdd: lambda self, e: f"""DATE_ADD({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""",
    255            exp.DateDiff: lambda self, e: f"""DATE_DIFF({self.sql(e, 'unit') or "'day'"}, {self.sql(e, 'expression')}, {self.sql(e, 'this')})""",
    256            exp.DateStrToDate: lambda self, e: f"CAST(DATE_PARSE({self.sql(e, 'this')}, {Presto.date_format}) AS DATE)",
    257            exp.DateToDi: lambda self, e: f"CAST(DATE_FORMAT({self.sql(e, 'this')}, {Presto.dateint_format}) AS INT)",
    258            exp.Decode: _decode_sql,
    259            exp.DiToDate: lambda self, e: f"CAST(DATE_PARSE(CAST({self.sql(e, 'this')} AS VARCHAR), {Presto.dateint_format}) AS DATE)",
    260            exp.Encode: _encode_sql,
    261            exp.GenerateSeries: _sequence_sql,
    262            exp.Hex: rename_func("TO_HEX"),
    263            exp.If: if_sql,
    264            exp.ILike: no_ilike_sql,
    265            exp.Initcap: _initcap_sql,
    266            exp.Lateral: _explode_to_unnest_sql,
    267            exp.Levenshtein: rename_func("LEVENSHTEIN_DISTANCE"),
    268            exp.LogicalOr: rename_func("BOOL_OR"),
    269            exp.Quantile: _quantile_sql,
    270            exp.ApproxQuantile: rename_func("APPROX_PERCENTILE"),
    271            exp.SafeDivide: no_safe_divide_sql,
    272            exp.Schema: _schema_sql,
    273            exp.SortArray: _no_sort_array,
    274            exp.StrPosition: rename_func("STRPOS"),
    275            exp.StrToDate: lambda self, e: f"CAST({_str_to_time_sql(self, e)} AS DATE)",
    276            exp.StrToTime: _str_to_time_sql,
    277            exp.StrToUnix: lambda self, e: f"TO_UNIXTIME(DATE_PARSE({self.sql(e, 'this')}, {self.format_time(e)}))",
    278            exp.StructExtract: struct_extract_sql,
    279            exp.TableFormatProperty: lambda self, e: f"TABLE_FORMAT='{e.name.upper()}'",
    280            exp.FileFormatProperty: lambda self, e: f"FORMAT='{e.name.upper()}'",
    281            exp.TimeStrToDate: timestrtotime_sql,
    282            exp.TimeStrToTime: timestrtotime_sql,
    283            exp.TimeStrToUnix: lambda self, e: f"TO_UNIXTIME(DATE_PARSE({self.sql(e, 'this')}, {Presto.time_format}))",
    284            exp.TimeToStr: lambda self, e: f"DATE_FORMAT({self.sql(e, 'this')}, {self.format_time(e)})",
    285            exp.TimeToUnix: rename_func("TO_UNIXTIME"),
    286            exp.TsOrDiToDi: lambda self, e: f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS VARCHAR), '-', ''), 1, 8) AS INT)",
    287            exp.TsOrDsAdd: _ts_or_ds_add_sql,
    288            exp.TsOrDsToDate: _ts_or_ds_to_date_sql,
    289            exp.Unhex: rename_func("FROM_HEX"),
    290            exp.UnixToStr: lambda self, e: f"DATE_FORMAT(FROM_UNIXTIME({self.sql(e, 'this')}), {self.format_time(e)})",
    291            exp.UnixToTime: rename_func("FROM_UNIXTIME"),
    292            exp.UnixToTimeStr: lambda self, e: f"CAST(FROM_UNIXTIME({self.sql(e, 'this')}) AS VARCHAR)",
    293            exp.VariancePop: rename_func("VAR_POP"),
    294        }
    295
    296        def transaction_sql(self, expression):
    297            modes = expression.args.get("modes")
    298            modes = f" {', '.join(modes)}" if modes else ""
    299            return f"START TRANSACTION{modes}"

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): if set to True all identifiers will be delimited by the corresponding character.
- 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 transaction_sql(self, expression): View Source

    296        def transaction_sql(self, expression):
    297            modes = expression.args.get("modes")
    298            modes = f" {', '.join(modes)}" if modes else ""
    299            return f"START TRANSACTION{modes}"

##### 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
  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
  offset_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
  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
  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
  floatdiv_sql
  overlaps_sql
  distance_sql
  dot_sql
  eq_sql
  escape_sql
  glob_sql
  gt_sql
  gte_sql
  ilike_sql
  is_sql
  like_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