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

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

Arguments:
  • error_level: The desired error level. Default: ErrorLevel.IMMEDIATE
  • error_message_context: Determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 100
  • max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3
class Postgres.Generator(sqlglot.generator.Generator):
313    class Generator(generator.Generator):
314        SINGLE_STRING_INTERVAL = True
315        LOCKING_READS_SUPPORTED = True
316        JOIN_HINTS = False
317        TABLE_HINTS = False
318        PARAMETER_TOKEN = "$"
319
320        TYPE_MAPPING = {
321            **generator.Generator.TYPE_MAPPING,
322            exp.DataType.Type.TINYINT: "SMALLINT",
323            exp.DataType.Type.FLOAT: "REAL",
324            exp.DataType.Type.DOUBLE: "DOUBLE PRECISION",
325            exp.DataType.Type.BINARY: "BYTEA",
326            exp.DataType.Type.VARBINARY: "BYTEA",
327            exp.DataType.Type.DATETIME: "TIMESTAMP",
328        }
329
330        TRANSFORMS = {
331            **generator.Generator.TRANSFORMS,
332            exp.BitwiseXor: lambda self, e: self.binary(e, "#"),
333            exp.ColumnDef: transforms.preprocess([_auto_increment_to_serial, _serial_to_generated]),
334            exp.Explode: rename_func("UNNEST"),
335            exp.JSONExtract: arrow_json_extract_sql,
336            exp.JSONExtractScalar: arrow_json_extract_scalar_sql,
337            exp.JSONBExtract: lambda self, e: self.binary(e, "#>"),
338            exp.JSONBExtractScalar: lambda self, e: self.binary(e, "#>>"),
339            exp.JSONBContains: lambda self, e: self.binary(e, "?"),
340            exp.Pow: lambda self, e: self.binary(e, "^"),
341            exp.CurrentDate: no_paren_current_date_sql,
342            exp.CurrentTimestamp: lambda *_: "CURRENT_TIMESTAMP",
343            exp.DateAdd: _date_add_sql("+"),
344            exp.DateStrToDate: datestrtodate_sql,
345            exp.DateSub: _date_add_sql("-"),
346            exp.DateDiff: _date_diff_sql,
347            exp.LogicalOr: rename_func("BOOL_OR"),
348            exp.LogicalAnd: rename_func("BOOL_AND"),
349            exp.Max: max_or_greatest,
350            exp.Min: min_or_least,
351            exp.ArrayOverlaps: lambda self, e: self.binary(e, "&&"),
352            exp.ArrayContains: lambda self, e: self.binary(e, "@>"),
353            exp.ArrayContained: lambda self, e: self.binary(e, "<@"),
354            exp.Merge: transforms.preprocess([transforms.remove_target_from_merge]),
355            exp.Pivot: no_pivot_sql,
356            exp.RegexpLike: lambda self, e: self.binary(e, "~"),
357            exp.RegexpILike: lambda self, e: self.binary(e, "~*"),
358            exp.StrPosition: str_position_sql,
359            exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})",
360            exp.Substring: _substring_sql,
361            exp.TimestampTrunc: timestamptrunc_sql,
362            exp.TimeStrToTime: timestrtotime_sql,
363            exp.TimeToStr: lambda self, e: f"TO_CHAR({self.sql(e, 'this')}, {self.format_time(e)})",
364            exp.TableSample: no_tablesample_sql,
365            exp.ToChar: lambda self, e: self.function_fallback_sql(e),
366            exp.Trim: trim_sql,
367            exp.TryCast: no_trycast_sql,
368            exp.TsOrDsToDate: ts_or_ds_to_date_sql("postgres"),
369            exp.UnixToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')})",
370            exp.DataType: _datatype_sql,
371            exp.GroupConcat: _string_agg_sql,
372            exp.Array: lambda self, e: f"{self.normalize_func('ARRAY')}({self.sql(e.expressions[0])})"
373            if isinstance(seq_get(e.expressions, 0), exp.Select)
374            else f"{self.normalize_func('ARRAY')}[{self.expressions(e, flat=True)}]",
375        }
376
377        PROPERTIES_LOCATION = {
378            **generator.Generator.PROPERTIES_LOCATION,
379            exp.TransientProperty: exp.Properties.Location.UNSUPPORTED,
380            exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
381        }

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

Arguments:
  • pretty: Whether or not to format the produced SQL string. Default: False.
  • identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True or 'always': Always quote. 'safe': Only quote identifiers that are case insensitive.
  • normalize: Whether or not to normalize identifiers to lowercase. Default: False.
  • pad: Determines the pad size in a formatted string. Default: 2.
  • indent: Determines the indentation size in a formatted string. Default: 2.
  • normalize_functions: Whether or not to normalize all function names. Possible values are: "upper" or True (default): Convert names to uppercase. "lower": Convert names to lowercase. False: Disables function name normalization.
  • unsupported_level: Determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
  • max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
  • leading_comma: Determines whether or not the comma is leading or trailing in select expressions. This is only relevant when generating in pretty mode. Default: False
  • max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
  • comments: Whether or not to preserve comments in the output SQL code. Default: True
@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool:
247    @classmethod
248    def can_identify(cls, text: str, identify: str | bool = "safe") -> bool:
249        """Checks if text can be identified given an identify option.
250
251        Args:
252            text: The text to check.
253            identify:
254                "always" or `True`: Always returns true.
255                "safe": True if the identifier is case-insensitive.
256
257        Returns:
258            Whether or not the given text can be identified.
259        """
260        if identify is True or identify == "always":
261            return True
262
263        if identify == "safe":
264            return not cls.case_sensitive(text)
265
266        return False

Checks if text can be identified given an identify option.

Arguments:
  • text: The text to check.
  • identify: "always" or True: Always returns true. "safe": True if the identifier is case-insensitive.
Returns:

Whether or not the given text can be identified.

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
createable_sql
create_sql
clone_sql
describe_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
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
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
escape_str
loaddata_sql
null_sql
boolean_sql
order_sql
cluster_sql
distribute_sql
sort_sql
ordered_sql
matchrecognize_sql
query_modifiers
offset_limit_modifiers
after_having_modifiers
after_limit_modifiers
select_sql
schema_sql
schema_columns_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
safeconcat_sql
check_sql
foreignkey_sql
primarykey_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
safedpipe_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
dictproperty_sql
dictrange_sql
dictsubproperty_sql
oncluster_sql