Edit on GitHub

sqlglot.dialects.postgres

  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    arrow_json_extract_scalar_sql,
  9    arrow_json_extract_sql,
 10    datestrtodate_sql,
 11    format_time_lambda,
 12    max_or_greatest,
 13    min_or_least,
 14    no_paren_current_date_sql,
 15    no_tablesample_sql,
 16    no_trycast_sql,
 17    rename_func,
 18    str_position_sql,
 19    timestamptrunc_sql,
 20    trim_sql,
 21)
 22from sqlglot.helper import seq_get
 23from sqlglot.parser import binary_range_parser
 24from sqlglot.tokens import TokenType
 25
 26DATE_DIFF_FACTOR = {
 27    "MICROSECOND": " * 1000000",
 28    "MILLISECOND": " * 1000",
 29    "SECOND": "",
 30    "MINUTE": " / 60",
 31    "HOUR": " / 3600",
 32    "DAY": " / 86400",
 33}
 34
 35
 36def _date_add_sql(kind):
 37    def func(self, expression):
 38        from sqlglot.optimizer.simplify import simplify
 39
 40        this = self.sql(expression, "this")
 41        unit = expression.args.get("unit")
 42        expression = simplify(expression.args["expression"])
 43
 44        if not isinstance(expression, exp.Literal):
 45            self.unsupported("Cannot add non literal")
 46
 47        expression = expression.copy()
 48        expression.args["is_string"] = True
 49        return f"{this} {kind} {self.sql(exp.Interval(this=expression, unit=unit))}"
 50
 51    return func
 52
 53
 54def _date_diff_sql(self, expression):
 55    unit = expression.text("unit").upper()
 56    factor = DATE_DIFF_FACTOR.get(unit)
 57
 58    end = f"CAST({expression.this} AS TIMESTAMP)"
 59    start = f"CAST({expression.expression} AS TIMESTAMP)"
 60
 61    if factor is not None:
 62        return f"CAST(EXTRACT(epoch FROM {end} - {start}){factor} AS BIGINT)"
 63
 64    age = f"AGE({end}, {start})"
 65
 66    if unit == "WEEK":
 67        unit = f"EXTRACT(year FROM {age}) * 48 + EXTRACT(month FROM {age}) * 4 + EXTRACT(day FROM {age}) / 7"
 68    elif unit == "MONTH":
 69        unit = f"EXTRACT(year FROM {age}) * 12 + EXTRACT(month FROM {age})"
 70    elif unit == "QUARTER":
 71        unit = f"EXTRACT(year FROM {age}) * 4 + EXTRACT(month FROM {age}) / 3"
 72    elif unit == "YEAR":
 73        unit = f"EXTRACT(year FROM {age})"
 74    else:
 75        unit = age
 76
 77    return f"CAST({unit} AS BIGINT)"
 78
 79
 80def _substring_sql(self, expression):
 81    this = self.sql(expression, "this")
 82    start = self.sql(expression, "start")
 83    length = self.sql(expression, "length")
 84
 85    from_part = f" FROM {start}" if start else ""
 86    for_part = f" FOR {length}" if length else ""
 87
 88    return f"SUBSTRING({this}{from_part}{for_part})"
 89
 90
 91def _string_agg_sql(self, expression):
 92    expression = expression.copy()
 93    separator = expression.args.get("separator") or exp.Literal.string(",")
 94
 95    order = ""
 96    this = expression.this
 97    if isinstance(this, exp.Order):
 98        if this.this:
 99            this = this.this.pop()
100        order = self.sql(expression.this)  # Order has a leading space
101
102    return f"STRING_AGG({self.format_args(this, separator)}{order})"
103
104
105def _datatype_sql(self, expression):
106    if expression.this == exp.DataType.Type.ARRAY:
107        return f"{self.expressions(expression, flat=True)}[]"
108    return self.datatype_sql(expression)
109
110
111def _auto_increment_to_serial(expression):
112    auto = expression.find(exp.AutoIncrementColumnConstraint)
113
114    if auto:
115        expression = expression.copy()
116        expression.args["constraints"].remove(auto.parent)
117        kind = expression.args["kind"]
118
119        if kind.this == exp.DataType.Type.INT:
120            kind.replace(exp.DataType(this=exp.DataType.Type.SERIAL))
121        elif kind.this == exp.DataType.Type.SMALLINT:
122            kind.replace(exp.DataType(this=exp.DataType.Type.SMALLSERIAL))
123        elif kind.this == exp.DataType.Type.BIGINT:
124            kind.replace(exp.DataType(this=exp.DataType.Type.BIGSERIAL))
125
126    return expression
127
128
129def _serial_to_generated(expression):
130    kind = expression.args["kind"]
131
132    if kind.this == exp.DataType.Type.SERIAL:
133        data_type = exp.DataType(this=exp.DataType.Type.INT)
134    elif kind.this == exp.DataType.Type.SMALLSERIAL:
135        data_type = exp.DataType(this=exp.DataType.Type.SMALLINT)
136    elif kind.this == exp.DataType.Type.BIGSERIAL:
137        data_type = exp.DataType(this=exp.DataType.Type.BIGINT)
138    else:
139        data_type = None
140
141    if data_type:
142        expression = expression.copy()
143        expression.args["kind"].replace(data_type)
144        constraints = expression.args["constraints"]
145        generated = exp.ColumnConstraint(kind=exp.GeneratedAsIdentityColumnConstraint(this=False))
146        notnull = exp.ColumnConstraint(kind=exp.NotNullColumnConstraint())
147        if notnull not in constraints:
148            constraints.insert(0, notnull)
149        if generated not in constraints:
150            constraints.insert(0, generated)
151
152    return expression
153
154
155def _generate_series(args):
156    # The goal is to convert step values like '1 day' or INTERVAL '1 day' into INTERVAL '1' day
157    step = seq_get(args, 2)
158
159    if step is None:
160        # Postgres allows calls with just two arguments -- the "step" argument defaults to 1
161        return exp.GenerateSeries.from_arg_list(args)
162
163    if step.is_string:
164        args[2] = exp.to_interval(step.this)
165    elif isinstance(step, exp.Interval) and not step.args.get("unit"):
166        args[2] = exp.to_interval(step.this.this)
167
168    return exp.GenerateSeries.from_arg_list(args)
169
170
171def _to_timestamp(args):
172    # TO_TIMESTAMP accepts either a single double argument or (text, text)
173    if len(args) == 1:
174        # https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
175        return exp.UnixToTime.from_arg_list(args)
176    # https://www.postgresql.org/docs/current/functions-formatting.html
177    return format_time_lambda(exp.StrToTime, "postgres")(args)
178
179
180class Postgres(Dialect):
181    null_ordering = "nulls_are_large"
182    time_format = "'YYYY-MM-DD HH24:MI:SS'"
183    time_mapping = {
184        "AM": "%p",
185        "PM": "%p",
186        "D": "%u",  # 1-based day of week
187        "DD": "%d",  # day of month
188        "DDD": "%j",  # zero padded day of year
189        "FMDD": "%-d",  # - is no leading zero for Python; same for FM in postgres
190        "FMDDD": "%-j",  # day of year
191        "FMHH12": "%-I",  # 9
192        "FMHH24": "%-H",  # 9
193        "FMMI": "%-M",  # Minute
194        "FMMM": "%-m",  # 1
195        "FMSS": "%-S",  # Second
196        "HH12": "%I",  # 09
197        "HH24": "%H",  # 09
198        "MI": "%M",  # zero padded minute
199        "MM": "%m",  # 01
200        "OF": "%z",  # utc offset
201        "SS": "%S",  # zero padded second
202        "TMDay": "%A",  # TM is locale dependent
203        "TMDy": "%a",
204        "TMMon": "%b",  # Sep
205        "TMMonth": "%B",  # September
206        "TZ": "%Z",  # uppercase timezone name
207        "US": "%f",  # zero padded microsecond
208        "WW": "%U",  # 1-based week of year
209        "YY": "%y",  # 15
210        "YYYY": "%Y",  # 2015
211    }
212
213    class Tokenizer(tokens.Tokenizer):
214        QUOTES = ["'", "$$"]
215
216        BIT_STRINGS = [("b'", "'"), ("B'", "'")]
217        HEX_STRINGS = [("x'", "'"), ("X'", "'")]
218        BYTE_STRINGS = [("e'", "'"), ("E'", "'")]
219
220        KEYWORDS = {
221            **tokens.Tokenizer.KEYWORDS,
222            "~~": TokenType.LIKE,
223            "~~*": TokenType.ILIKE,
224            "~*": TokenType.IRLIKE,
225            "~": TokenType.RLIKE,
226            "@>": TokenType.AT_GT,
227            "<@": TokenType.LT_AT,
228            "BEGIN": TokenType.COMMAND,
229            "BEGIN TRANSACTION": TokenType.BEGIN,
230            "BIGSERIAL": TokenType.BIGSERIAL,
231            "CHARACTER VARYING": TokenType.VARCHAR,
232            "DECLARE": TokenType.COMMAND,
233            "DO": TokenType.COMMAND,
234            "HSTORE": TokenType.HSTORE,
235            "JSONB": TokenType.JSONB,
236            "REFRESH": TokenType.COMMAND,
237            "REINDEX": TokenType.COMMAND,
238            "RESET": TokenType.COMMAND,
239            "RETURNING": TokenType.RETURNING,
240            "REVOKE": TokenType.COMMAND,
241            "SERIAL": TokenType.SERIAL,
242            "SMALLSERIAL": TokenType.SMALLSERIAL,
243            "TEMP": TokenType.TEMPORARY,
244            "CSTRING": TokenType.PSEUDO_TYPE,
245        }
246
247        SINGLE_TOKENS = {
248            **tokens.Tokenizer.SINGLE_TOKENS,
249            "$": TokenType.PARAMETER,
250        }
251
252        VAR_SINGLE_TOKENS = {"$"}
253
254    class Parser(parser.Parser):
255        STRICT_CAST = False
256
257        FUNCTIONS = {
258            **parser.Parser.FUNCTIONS,  # type: ignore
259            "DATE_TRUNC": lambda args: exp.TimestampTrunc(
260                this=seq_get(args, 1), unit=seq_get(args, 0)
261            ),
262            "GENERATE_SERIES": _generate_series,
263            "NOW": exp.CurrentTimestamp.from_arg_list,
264            "TO_CHAR": format_time_lambda(exp.TimeToStr, "postgres"),
265            "TO_TIMESTAMP": _to_timestamp,
266        }
267
268        FUNCTION_PARSERS = {
269            **parser.Parser.FUNCTION_PARSERS,
270            "DATE_PART": lambda self: self._parse_date_part(),
271        }
272
273        BITWISE = {
274            **parser.Parser.BITWISE,  # type: ignore
275            TokenType.HASH: exp.BitwiseXor,
276        }
277
278        EXPONENT = {
279            TokenType.CARET: exp.Pow,
280        }
281
282        RANGE_PARSERS = {
283            **parser.Parser.RANGE_PARSERS,  # type: ignore
284            TokenType.DAMP: binary_range_parser(exp.ArrayOverlaps),
285            TokenType.AT_GT: binary_range_parser(exp.ArrayContains),
286            TokenType.LT_AT: binary_range_parser(exp.ArrayContained),
287        }
288
289        def _parse_factor(self) -> t.Optional[exp.Expression]:
290            return self._parse_tokens(self._parse_exponent, self.FACTOR)
291
292        def _parse_exponent(self) -> t.Optional[exp.Expression]:
293            return self._parse_tokens(self._parse_unary, self.EXPONENT)
294
295        def _parse_date_part(self) -> exp.Expression:
296            part = self._parse_type()
297            self._match(TokenType.COMMA)
298            value = self._parse_bitwise()
299
300            if part and part.is_string:
301                part = exp.Var(this=part.name)
302
303            return self.expression(exp.Extract, this=part, expression=value)
304
305    class Generator(generator.Generator):
306        INTERVAL_ALLOWS_PLURAL_FORM = False
307        LOCKING_READS_SUPPORTED = True
308        JOIN_HINTS = False
309        TABLE_HINTS = False
310        PARAMETER_TOKEN = "$"
311
312        TYPE_MAPPING = {
313            **generator.Generator.TYPE_MAPPING,  # type: ignore
314            exp.DataType.Type.TINYINT: "SMALLINT",
315            exp.DataType.Type.FLOAT: "REAL",
316            exp.DataType.Type.DOUBLE: "DOUBLE PRECISION",
317            exp.DataType.Type.BINARY: "BYTEA",
318            exp.DataType.Type.VARBINARY: "BYTEA",
319            exp.DataType.Type.DATETIME: "TIMESTAMP",
320        }
321
322        TRANSFORMS = {
323            **generator.Generator.TRANSFORMS,  # type: ignore
324            exp.BitwiseXor: lambda self, e: self.binary(e, "#"),
325            exp.ColumnDef: transforms.preprocess(
326                [
327                    _auto_increment_to_serial,
328                    _serial_to_generated,
329                ],
330            ),
331            exp.JSONExtract: arrow_json_extract_sql,
332            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
333            exp.JSONBExtract: lambda self, e: self.binary(e, "#>"),
334            exp.JSONBExtractScalar: lambda self, e: self.binary(e, "#>>"),
335            exp.JSONBContains: lambda self, e: self.binary(e, "?"),
336            exp.Pow: lambda self, e: self.binary(e, "^"),
337            exp.CurrentDate: no_paren_current_date_sql,
338            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
339            exp.DateAdd: _date_add_sql("+"),
340            exp.DateStrToDate: datestrtodate_sql,
341            exp.DateSub: _date_add_sql("-"),
342            exp.DateDiff: _date_diff_sql,
343            exp.LogicalOr: rename_func("BOOL_OR"),
344            exp.LogicalAnd: rename_func("BOOL_AND"),
345            exp.Max: max_or_greatest,
346            exp.Min: min_or_least,
347            exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"),
348            exp.ArrayContains: lambda self, e: self.binary(e, "@>"),
349            exp.ArrayContained: lambda self, e: self.binary(e, "<@"),
350            exp.Merge: transforms.preprocess([transforms.remove_target_from_merge]),
351            exp.RegexpLike: lambda self, e: self.binary(e, "~"),
352            exp.RegexpILike: lambda self, e: self.binary(e, "~*"),
353            exp.StrPosition: str_position_sql,
354            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
355            exp.Substring: _substring_sql,
356            exp.TimestampTrunc: timestamptrunc_sql,
357            exp.TimeStrToTime: lambda self, e: f"CAST({self.sql(e, 'this')} AS TIMESTAMP)",
358            exp.TimeToStr: lambda self, e: f"TO_CHAR({self.sql(e, 'this')}, {self.format_time(e)})",
359            exp.TableSample: no_tablesample_sql,
360            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
361            exp.Trim: trim_sql,
362            exp.TryCast: no_trycast_sql,
363            exp.UnixToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')})",
364            exp.DataType: _datatype_sql,
365            exp.GroupConcat: _string_agg_sql,
366            exp.Array: lambda self, e: f"{self.normalize_func('ARRAY')}({self.sql(e.expressions[0])})"
367            if isinstance(seq_get(e.expressions, 0), exp.Select)
368            else f"{self.normalize_func('ARRAY')}[{self.expressions(e, flat=True)}]",
369        }
370
371        PROPERTIES_LOCATION = {
372            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
373            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
374            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
375        }
class Postgres(sqlglot.dialects.dialect.Dialect):
181class Postgres(Dialect):
182    null_ordering = "nulls_are_large"
183    time_format = "'YYYY-MM-DD HH24:MI:SS'"
184    time_mapping = {
185        "AM": "%p",
186        "PM": "%p",
187        "D": "%u",  # 1-based day of week
188        "DD": "%d",  # day of month
189        "DDD": "%j",  # zero padded day of year
190        "FMDD": "%-d",  # - is no leading zero for Python; same for FM in postgres
191        "FMDDD": "%-j",  # day of year
192        "FMHH12": "%-I",  # 9
193        "FMHH24": "%-H",  # 9
194        "FMMI": "%-M",  # Minute
195        "FMMM": "%-m",  # 1
196        "FMSS": "%-S",  # Second
197        "HH12": "%I",  # 09
198        "HH24": "%H",  # 09
199        "MI": "%M",  # zero padded minute
200        "MM": "%m",  # 01
201        "OF": "%z",  # utc offset
202        "SS": "%S",  # zero padded second
203        "TMDay": "%A",  # TM is locale dependent
204        "TMDy": "%a",
205        "TMMon": "%b",  # Sep
206        "TMMonth": "%B",  # September
207        "TZ": "%Z",  # uppercase timezone name
208        "US": "%f",  # zero padded microsecond
209        "WW": "%U",  # 1-based week of year
210        "YY": "%y",  # 15
211        "YYYY": "%Y",  # 2015
212    }
213
214    class Tokenizer(tokens.Tokenizer):
215        QUOTES = ["'", "$$"]
216
217        BIT_STRINGS = [("b'", "'"), ("B'", "'")]
218        HEX_STRINGS = [("x'", "'"), ("X'", "'")]
219        BYTE_STRINGS = [("e'", "'"), ("E'", "'")]
220
221        KEYWORDS = {
222            **tokens.Tokenizer.KEYWORDS,
223            "~~": TokenType.LIKE,
224            "~~*": TokenType.ILIKE,
225            "~*": TokenType.IRLIKE,
226            "~": TokenType.RLIKE,
227            "@>": TokenType.AT_GT,
228            "<@": TokenType.LT_AT,
229            "BEGIN": TokenType.COMMAND,
230            "BEGIN TRANSACTION": TokenType.BEGIN,
231            "BIGSERIAL": TokenType.BIGSERIAL,
232            "CHARACTER VARYING": TokenType.VARCHAR,
233            "DECLARE": TokenType.COMMAND,
234            "DO": TokenType.COMMAND,
235            "HSTORE": TokenType.HSTORE,
236            "JSONB": TokenType.JSONB,
237            "REFRESH": TokenType.COMMAND,
238            "REINDEX": TokenType.COMMAND,
239            "RESET": TokenType.COMMAND,
240            "RETURNING": TokenType.RETURNING,
241            "REVOKE": TokenType.COMMAND,
242            "SERIAL": TokenType.SERIAL,
243            "SMALLSERIAL": TokenType.SMALLSERIAL,
244            "TEMP": TokenType.TEMPORARY,
245            "CSTRING": TokenType.PSEUDO_TYPE,
246        }
247
248        SINGLE_TOKENS = {
249            **tokens.Tokenizer.SINGLE_TOKENS,
250            "$": TokenType.PARAMETER,
251        }
252
253        VAR_SINGLE_TOKENS = {"$"}
254
255    class Parser(parser.Parser):
256        STRICT_CAST = False
257
258        FUNCTIONS = {
259            **parser.Parser.FUNCTIONS,  # type: ignore
260            "DATE_TRUNC": lambda args: exp.TimestampTrunc(
261                this=seq_get(args, 1), unit=seq_get(args, 0)
262            ),
263            "GENERATE_SERIES": _generate_series,
264            "NOW": exp.CurrentTimestamp.from_arg_list,
265            "TO_CHAR": format_time_lambda(exp.TimeToStr, "postgres"),
266            "TO_TIMESTAMP": _to_timestamp,
267        }
268
269        FUNCTION_PARSERS = {
270            **parser.Parser.FUNCTION_PARSERS,
271            "DATE_PART": lambda self: self._parse_date_part(),
272        }
273
274        BITWISE = {
275            **parser.Parser.BITWISE,  # type: ignore
276            TokenType.HASH: exp.BitwiseXor,
277        }
278
279        EXPONENT = {
280            TokenType.CARET: exp.Pow,
281        }
282
283        RANGE_PARSERS = {
284            **parser.Parser.RANGE_PARSERS,  # type: ignore
285            TokenType.DAMP: binary_range_parser(exp.ArrayOverlaps),
286            TokenType.AT_GT: binary_range_parser(exp.ArrayContains),
287            TokenType.LT_AT: binary_range_parser(exp.ArrayContained),
288        }
289
290        def _parse_factor(self) -> t.Optional[exp.Expression]:
291            return self._parse_tokens(self._parse_exponent, self.FACTOR)
292
293        def _parse_exponent(self) -> t.Optional[exp.Expression]:
294            return self._parse_tokens(self._parse_unary, self.EXPONENT)
295
296        def _parse_date_part(self) -> exp.Expression:
297            part = self._parse_type()
298            self._match(TokenType.COMMA)
299            value = self._parse_bitwise()
300
301            if part and part.is_string:
302                part = exp.Var(this=part.name)
303
304            return self.expression(exp.Extract, this=part, expression=value)
305
306    class Generator(generator.Generator):
307        INTERVAL_ALLOWS_PLURAL_FORM = False
308        LOCKING_READS_SUPPORTED = True
309        JOIN_HINTS = False
310        TABLE_HINTS = False
311        PARAMETER_TOKEN = "$"
312
313        TYPE_MAPPING = {
314            **generator.Generator.TYPE_MAPPING,  # type: ignore
315            exp.DataType.Type.TINYINT: "SMALLINT",
316            exp.DataType.Type.FLOAT: "REAL",
317            exp.DataType.Type.DOUBLE: "DOUBLE PRECISION",
318            exp.DataType.Type.BINARY: "BYTEA",
319            exp.DataType.Type.VARBINARY: "BYTEA",
320            exp.DataType.Type.DATETIME: "TIMESTAMP",
321        }
322
323        TRANSFORMS = {
324            **generator.Generator.TRANSFORMS,  # type: ignore
325            exp.BitwiseXor: lambda self, e: self.binary(e, "#"),
326            exp.ColumnDef: transforms.preprocess(
327                [
328                    _auto_increment_to_serial,
329                    _serial_to_generated,
330                ],
331            ),
332            exp.JSONExtract: arrow_json_extract_sql,
333            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
334            exp.JSONBExtract: lambda self, e: self.binary(e, "#>"),
335            exp.JSONBExtractScalar: lambda self, e: self.binary(e, "#>>"),
336            exp.JSONBContains: lambda self, e: self.binary(e, "?"),
337            exp.Pow: lambda self, e: self.binary(e, "^"),
338            exp.CurrentDate: no_paren_current_date_sql,
339            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
340            exp.DateAdd: _date_add_sql("+"),
341            exp.DateStrToDate: datestrtodate_sql,
342            exp.DateSub: _date_add_sql("-"),
343            exp.DateDiff: _date_diff_sql,
344            exp.LogicalOr: rename_func("BOOL_OR"),
345            exp.LogicalAnd: rename_func("BOOL_AND"),
346            exp.Max: max_or_greatest,
347            exp.Min: min_or_least,
348            exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"),
349            exp.ArrayContains: lambda self, e: self.binary(e, "@>"),
350            exp.ArrayContained: lambda self, e: self.binary(e, "<@"),
351            exp.Merge: transforms.preprocess([transforms.remove_target_from_merge]),
352            exp.RegexpLike: lambda self, e: self.binary(e, "~"),
353            exp.RegexpILike: lambda self, e: self.binary(e, "~*"),
354            exp.StrPosition: str_position_sql,
355            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
356            exp.Substring: _substring_sql,
357            exp.TimestampTrunc: timestamptrunc_sql,
358            exp.TimeStrToTime: lambda self, e: f"CAST({self.sql(e, 'this')} AS TIMESTAMP)",
359            exp.TimeToStr: lambda self, e: f"TO_CHAR({self.sql(e, 'this')}, {self.format_time(e)})",
360            exp.TableSample: no_tablesample_sql,
361            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
362            exp.Trim: trim_sql,
363            exp.TryCast: no_trycast_sql,
364            exp.UnixToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')})",
365            exp.DataType: _datatype_sql,
366            exp.GroupConcat: _string_agg_sql,
367            exp.Array: lambda self, e: f"{self.normalize_func('ARRAY')}({self.sql(e.expressions[0])})"
368            if isinstance(seq_get(e.expressions, 0), exp.Select)
369            else f"{self.normalize_func('ARRAY')}[{self.expressions(e, flat=True)}]",
370        }
371
372        PROPERTIES_LOCATION = {
373            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
374            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
375            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
376        }
class Postgres.Tokenizer(sqlglot.tokens.Tokenizer):
214    class Tokenizer(tokens.Tokenizer):
215        QUOTES = ["'", "$$"]
216
217        BIT_STRINGS = [("b'", "'"), ("B'", "'")]
218        HEX_STRINGS = [("x'", "'"), ("X'", "'")]
219        BYTE_STRINGS = [("e'", "'"), ("E'", "'")]
220
221        KEYWORDS = {
222            **tokens.Tokenizer.KEYWORDS,
223            "~~": TokenType.LIKE,
224            "~~*": TokenType.ILIKE,
225            "~*": TokenType.IRLIKE,
226            "~": TokenType.RLIKE,
227            "@>": TokenType.AT_GT,
228            "<@": TokenType.LT_AT,
229            "BEGIN": TokenType.COMMAND,
230            "BEGIN TRANSACTION": TokenType.BEGIN,
231            "BIGSERIAL": TokenType.BIGSERIAL,
232            "CHARACTER VARYING": TokenType.VARCHAR,
233            "DECLARE": TokenType.COMMAND,
234            "DO": TokenType.COMMAND,
235            "HSTORE": TokenType.HSTORE,
236            "JSONB": TokenType.JSONB,
237            "REFRESH": TokenType.COMMAND,
238            "REINDEX": TokenType.COMMAND,
239            "RESET": TokenType.COMMAND,
240            "RETURNING": TokenType.RETURNING,
241            "REVOKE": TokenType.COMMAND,
242            "SERIAL": TokenType.SERIAL,
243            "SMALLSERIAL": TokenType.SMALLSERIAL,
244            "TEMP": TokenType.TEMPORARY,
245            "CSTRING": TokenType.PSEUDO_TYPE,
246        }
247
248        SINGLE_TOKENS = {
249            **tokens.Tokenizer.SINGLE_TOKENS,
250            "$": TokenType.PARAMETER,
251        }
252
253        VAR_SINGLE_TOKENS = {"$"}
class Postgres.Parser(sqlglot.parser.Parser):
255    class Parser(parser.Parser):
256        STRICT_CAST = False
257
258        FUNCTIONS = {
259            **parser.Parser.FUNCTIONS,  # type: ignore
260            "DATE_TRUNC": lambda args: exp.TimestampTrunc(
261                this=seq_get(args, 1), unit=seq_get(args, 0)
262            ),
263            "GENERATE_SERIES": _generate_series,
264            "NOW": exp.CurrentTimestamp.from_arg_list,
265            "TO_CHAR": format_time_lambda(exp.TimeToStr, "postgres"),
266            "TO_TIMESTAMP": _to_timestamp,
267        }
268
269        FUNCTION_PARSERS = {
270            **parser.Parser.FUNCTION_PARSERS,
271            "DATE_PART": lambda self: self._parse_date_part(),
272        }
273
274        BITWISE = {
275            **parser.Parser.BITWISE,  # type: ignore
276            TokenType.HASH: exp.BitwiseXor,
277        }
278
279        EXPONENT = {
280            TokenType.CARET: exp.Pow,
281        }
282
283        RANGE_PARSERS = {
284            **parser.Parser.RANGE_PARSERS,  # type: ignore
285            TokenType.DAMP: binary_range_parser(exp.ArrayOverlaps),
286            TokenType.AT_GT: binary_range_parser(exp.ArrayContains),
287            TokenType.LT_AT: binary_range_parser(exp.ArrayContained),
288        }
289
290        def _parse_factor(self) -> t.Optional[exp.Expression]:
291            return self._parse_tokens(self._parse_exponent, self.FACTOR)
292
293        def _parse_exponent(self) -> t.Optional[exp.Expression]:
294            return self._parse_tokens(self._parse_unary, self.EXPONENT)
295
296        def _parse_date_part(self) -> exp.Expression:
297            part = self._parse_type()
298            self._match(TokenType.COMMA)
299            value = self._parse_bitwise()
300
301            if part and part.is_string:
302                part = exp.Var(this=part.name)
303
304            return self.expression(exp.Extract, this=part, expression=value)

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"
class Postgres.Generator(sqlglot.generator.Generator):
306    class Generator(generator.Generator):
307        INTERVAL_ALLOWS_PLURAL_FORM = False
308        LOCKING_READS_SUPPORTED = True
309        JOIN_HINTS = False
310        TABLE_HINTS = False
311        PARAMETER_TOKEN = "$"
312
313        TYPE_MAPPING = {
314            **generator.Generator.TYPE_MAPPING,  # type: ignore
315            exp.DataType.Type.TINYINT: "SMALLINT",
316            exp.DataType.Type.FLOAT: "REAL",
317            exp.DataType.Type.DOUBLE: "DOUBLE PRECISION",
318            exp.DataType.Type.BINARY: "BYTEA",
319            exp.DataType.Type.VARBINARY: "BYTEA",
320            exp.DataType.Type.DATETIME: "TIMESTAMP",
321        }
322
323        TRANSFORMS = {
324            **generator.Generator.TRANSFORMS,  # type: ignore
325            exp.BitwiseXor: lambda self, e: self.binary(e, "#"),
326            exp.ColumnDef: transforms.preprocess(
327                [
328                    _auto_increment_to_serial,
329                    _serial_to_generated,
330                ],
331            ),
332            exp.JSONExtract: arrow_json_extract_sql,
333            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
334            exp.JSONBExtract: lambda self, e: self.binary(e, "#>"),
335            exp.JSONBExtractScalar: lambda self, e: self.binary(e, "#>>"),
336            exp.JSONBContains: lambda self, e: self.binary(e, "?"),
337            exp.Pow: lambda self, e: self.binary(e, "^"),
338            exp.CurrentDate: no_paren_current_date_sql,
339            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
340            exp.DateAdd: _date_add_sql("+"),
341            exp.DateStrToDate: datestrtodate_sql,
342            exp.DateSub: _date_add_sql("-"),
343            exp.DateDiff: _date_diff_sql,
344            exp.LogicalOr: rename_func("BOOL_OR"),
345            exp.LogicalAnd: rename_func("BOOL_AND"),
346            exp.Max: max_or_greatest,
347            exp.Min: min_or_least,
348            exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"),
349            exp.ArrayContains: lambda self, e: self.binary(e, "@>"),
350            exp.ArrayContained: lambda self, e: self.binary(e, "<@"),
351            exp.Merge: transforms.preprocess([transforms.remove_target_from_merge]),
352            exp.RegexpLike: lambda self, e: self.binary(e, "~"),
353            exp.RegexpILike: lambda self, e: self.binary(e, "~*"),
354            exp.StrPosition: str_position_sql,
355            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
356            exp.Substring: _substring_sql,
357            exp.TimestampTrunc: timestamptrunc_sql,
358            exp.TimeStrToTime: lambda self, e: f"CAST({self.sql(e, 'this')} AS TIMESTAMP)",
359            exp.TimeToStr: lambda self, e: f"TO_CHAR({self.sql(e, 'this')}, {self.format_time(e)})",
360            exp.TableSample: no_tablesample_sql,
361            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
362            exp.Trim: trim_sql,
363            exp.TryCast: no_trycast_sql,
364            exp.UnixToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')})",
365            exp.DataType: _datatype_sql,
366            exp.GroupConcat: _string_agg_sql,
367            exp.Array: lambda self, e: f"{self.normalize_func('ARRAY')}({self.sql(e.expressions[0])})"
368            if isinstance(seq_get(e.expressions, 0), exp.Select)
369            else f"{self.normalize_func('ARRAY')}[{self.expressions(e, flat=True)}]",
370        }
371
372        PROPERTIES_LOCATION = {
373            **generator.Generator.PROPERTIES_LOCATION,  # type: ignore
374            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
375            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
376        }

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.
  • 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
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
clone_sql
describe_sql
prepend_ctes
with_sql
cte_sql
tablealias_sql
bitstring_sql
hexstring_sql
bytestring_sql
datatypesize_sql
datatype_sql
directory_sql
delete_sql
drop_sql
except_sql
except_op
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
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
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