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