sqlglot.dialects.snowflake
1from __future__ import annotations 2 3import typing as t 4 5from sqlglot import exp, generator, parser, tokens 6from sqlglot.dialects.dialect import ( 7 Dialect, 8 date_trunc_to_time, 9 datestrtodate_sql, 10 format_time_lambda, 11 inline_array_sql, 12 min_or_least, 13 rename_func, 14 timestamptrunc_sql, 15 timestrtotime_sql, 16 ts_or_ds_to_date_sql, 17 var_map_sql, 18) 19from sqlglot.expressions import Literal 20from sqlglot.helper import flatten, seq_get 21from sqlglot.parser import binary_range_parser 22from sqlglot.tokens import TokenType 23 24 25def _check_int(s): 26 if s[0] in ("-", "+"): 27 return s[1:].isdigit() 28 return s.isdigit() 29 30 31# from https://docs.snowflake.com/en/sql-reference/functions/to_timestamp.html 32def _snowflake_to_timestamp(args): 33 if len(args) == 2: 34 first_arg, second_arg = args 35 if second_arg.is_string: 36 # case: <string_expr> [ , <format> ] 37 return format_time_lambda(exp.StrToTime, "snowflake")(args) 38 39 # case: <numeric_expr> [ , <scale> ] 40 if second_arg.name not in ["0", "3", "9"]: 41 raise ValueError( 42 f"Scale for snowflake numeric timestamp is {second_arg}, but should be 0, 3, or 9" 43 ) 44 45 if second_arg.name == "0": 46 timescale = exp.UnixToTime.SECONDS 47 elif second_arg.name == "3": 48 timescale = exp.UnixToTime.MILLIS 49 elif second_arg.name == "9": 50 timescale = exp.UnixToTime.MICROS 51 52 return exp.UnixToTime(this=first_arg, scale=timescale) 53 54 first_arg = seq_get(args, 0) 55 if not isinstance(first_arg, Literal): 56 # case: <variant_expr> 57 return format_time_lambda(exp.StrToTime, "snowflake", default=True)(args) 58 59 if first_arg.is_string: 60 if _check_int(first_arg.this): 61 # case: <integer> 62 return exp.UnixToTime.from_arg_list(args) 63 64 # case: <date_expr> 65 return format_time_lambda(exp.StrToTime, "snowflake", default=True)(args) 66 67 # case: <numeric_expr> 68 return exp.UnixToTime.from_arg_list(args) 69 70 71def _unix_to_time_sql(self, expression): 72 scale = expression.args.get("scale") 73 timestamp = self.sql(expression, "this") 74 if scale in [None, exp.UnixToTime.SECONDS]: 75 return f"TO_TIMESTAMP({timestamp})" 76 if scale == exp.UnixToTime.MILLIS: 77 return f"TO_TIMESTAMP({timestamp}, 3)" 78 if scale == exp.UnixToTime.MICROS: 79 return f"TO_TIMESTAMP({timestamp}, 9)" 80 81 raise ValueError("Improper scale for timestamp") 82 83 84# https://docs.snowflake.com/en/sql-reference/functions/date_part.html 85# https://docs.snowflake.com/en/sql-reference/functions-date-time.html#label-supported-date-time-parts 86def _parse_date_part(self): 87 this = self._parse_var() or self._parse_type() 88 self._match(TokenType.COMMA) 89 expression = self._parse_bitwise() 90 91 name = this.name.upper() 92 if name.startswith("EPOCH"): 93 if name.startswith("EPOCH_MILLISECOND"): 94 scale = 10**3 95 elif name.startswith("EPOCH_MICROSECOND"): 96 scale = 10**6 97 elif name.startswith("EPOCH_NANOSECOND"): 98 scale = 10**9 99 else: 100 scale = None 101 102 ts = self.expression(exp.Cast, this=expression, to=exp.DataType.build("TIMESTAMP")) 103 to_unix = self.expression(exp.TimeToUnix, this=ts) 104 105 if scale: 106 to_unix = exp.Mul(this=to_unix, expression=exp.Literal.number(scale)) 107 108 return to_unix 109 110 return self.expression(exp.Extract, this=this, expression=expression) 111 112 113# https://docs.snowflake.com/en/sql-reference/functions/div0 114def _div0_to_if(args): 115 cond = exp.EQ(this=seq_get(args, 1), expression=exp.Literal.number(0)) 116 true = exp.Literal.number(0) 117 false = exp.Div(this=seq_get(args, 0), expression=seq_get(args, 1)) 118 return exp.If(this=cond, true=true, false=false) 119 120 121# https://docs.snowflake.com/en/sql-reference/functions/zeroifnull 122def _zeroifnull_to_if(args): 123 cond = exp.Is(this=seq_get(args, 0), expression=exp.Null()) 124 return exp.If(this=cond, true=exp.Literal.number(0), false=seq_get(args, 0)) 125 126 127# https://docs.snowflake.com/en/sql-reference/functions/zeroifnull 128def _nullifzero_to_if(args): 129 cond = exp.EQ(this=seq_get(args, 0), expression=exp.Literal.number(0)) 130 return exp.If(this=cond, true=exp.Null(), false=seq_get(args, 0)) 131 132 133def _datatype_sql(self, expression): 134 if expression.this == exp.DataType.Type.ARRAY: 135 return "ARRAY" 136 elif expression.this == exp.DataType.Type.MAP: 137 return "OBJECT" 138 return self.datatype_sql(expression) 139 140 141class Snowflake(Dialect): 142 null_ordering = "nulls_are_large" 143 time_format = "'yyyy-mm-dd hh24:mi:ss'" 144 145 time_mapping = { 146 "YYYY": "%Y", 147 "yyyy": "%Y", 148 "YY": "%y", 149 "yy": "%y", 150 "MMMM": "%B", 151 "mmmm": "%B", 152 "MON": "%b", 153 "mon": "%b", 154 "MM": "%m", 155 "mm": "%m", 156 "DD": "%d", 157 "dd": "%d", 158 "d": "%-d", 159 "DY": "%w", 160 "dy": "%w", 161 "HH24": "%H", 162 "hh24": "%H", 163 "HH12": "%I", 164 "hh12": "%I", 165 "MI": "%M", 166 "mi": "%M", 167 "SS": "%S", 168 "ss": "%S", 169 "FF": "%f", 170 "ff": "%f", 171 "FF6": "%f", 172 "ff6": "%f", 173 } 174 175 class Parser(parser.Parser): 176 FUNCTIONS = { 177 **parser.Parser.FUNCTIONS, 178 "ARRAYAGG": exp.ArrayAgg.from_arg_list, 179 "ARRAY_CONSTRUCT": exp.Array.from_arg_list, 180 "ARRAY_TO_STRING": exp.ArrayJoin.from_arg_list, 181 "DATE_TRUNC": date_trunc_to_time, 182 "DATEADD": lambda args: exp.DateAdd( 183 this=seq_get(args, 2), 184 expression=seq_get(args, 1), 185 unit=seq_get(args, 0), 186 ), 187 "DATEDIFF": lambda args: exp.DateDiff( 188 this=seq_get(args, 2), 189 expression=seq_get(args, 1), 190 unit=seq_get(args, 0), 191 ), 192 "DECODE": exp.Matches.from_arg_list, 193 "DIV0": _div0_to_if, 194 "IFF": exp.If.from_arg_list, 195 "NULLIFZERO": _nullifzero_to_if, 196 "OBJECT_CONSTRUCT": parser.parse_var_map, 197 "RLIKE": exp.RegexpLike.from_arg_list, 198 "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)), 199 "TO_ARRAY": exp.Array.from_arg_list, 200 "TO_VARCHAR": exp.ToChar.from_arg_list, 201 "TO_TIMESTAMP": _snowflake_to_timestamp, 202 "ZEROIFNULL": _zeroifnull_to_if, 203 } 204 205 FUNCTION_PARSERS = { 206 **parser.Parser.FUNCTION_PARSERS, 207 "DATE_PART": _parse_date_part, 208 } 209 FUNCTION_PARSERS.pop("TRIM") 210 211 FUNC_TOKENS = { 212 *parser.Parser.FUNC_TOKENS, 213 TokenType.RLIKE, 214 TokenType.TABLE, 215 } 216 217 COLUMN_OPERATORS = { 218 **parser.Parser.COLUMN_OPERATORS, # type: ignore 219 TokenType.COLON: lambda self, this, path: self.expression( 220 exp.Bracket, 221 this=this, 222 expressions=[path], 223 ), 224 } 225 226 RANGE_PARSERS = { 227 **parser.Parser.RANGE_PARSERS, # type: ignore 228 TokenType.LIKE_ANY: binary_range_parser(exp.LikeAny), 229 TokenType.ILIKE_ANY: binary_range_parser(exp.ILikeAny), 230 } 231 232 ALTER_PARSERS = { 233 **parser.Parser.ALTER_PARSERS, # type: ignore 234 "UNSET": lambda self: self._parse_alter_table_set_tag(unset=True), 235 "SET": lambda self: self._parse_alter_table_set_tag(), 236 } 237 238 def _parse_alter_table_set_tag(self, unset: bool = False) -> exp.Expression: 239 self._match_text_seq("TAG") 240 parser = t.cast(t.Callable, self._parse_id_var if unset else self._parse_conjunction) 241 return self.expression(exp.SetTag, expressions=self._parse_csv(parser), unset=unset) 242 243 class Tokenizer(tokens.Tokenizer): 244 QUOTES = ["'", "$$"] 245 STRING_ESCAPES = ["\\", "'"] 246 247 KEYWORDS = { 248 **tokens.Tokenizer.KEYWORDS, 249 "EXCLUDE": TokenType.EXCEPT, 250 "ILIKE ANY": TokenType.ILIKE_ANY, 251 "LIKE ANY": TokenType.LIKE_ANY, 252 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 253 "PUT": TokenType.COMMAND, 254 "RENAME": TokenType.REPLACE, 255 "TIMESTAMP_LTZ": TokenType.TIMESTAMPLTZ, 256 "TIMESTAMP_NTZ": TokenType.TIMESTAMP, 257 "TIMESTAMP_TZ": TokenType.TIMESTAMPTZ, 258 "TIMESTAMPNTZ": TokenType.TIMESTAMP, 259 "MINUS": TokenType.EXCEPT, 260 "SAMPLE": TokenType.TABLE_SAMPLE, 261 } 262 263 SINGLE_TOKENS = { 264 **tokens.Tokenizer.SINGLE_TOKENS, 265 "$": TokenType.PARAMETER, 266 } 267 268 class Generator(generator.Generator): 269 PARAMETER_TOKEN = "$" 270 MATCHED_BY_SOURCE = False 271 272 TRANSFORMS = { 273 **generator.Generator.TRANSFORMS, # type: ignore 274 exp.Array: inline_array_sql, 275 exp.ArrayConcat: rename_func("ARRAY_CAT"), 276 exp.ArrayJoin: rename_func("ARRAY_TO_STRING"), 277 exp.DateAdd: lambda self, e: self.func("DATEADD", e.text("unit"), e.expression, e.this), 278 exp.DateStrToDate: datestrtodate_sql, 279 exp.DataType: _datatype_sql, 280 exp.If: rename_func("IFF"), 281 exp.Map: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"), 282 exp.LogicalOr: rename_func("BOOLOR_AGG"), 283 exp.LogicalAnd: rename_func("BOOLAND_AGG"), 284 exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"), 285 exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}", 286 exp.Matches: rename_func("DECODE"), 287 exp.StrPosition: lambda self, e: self.func( 288 "POSITION", e.args.get("substr"), e.this, e.args.get("position") 289 ), 290 exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})", 291 exp.TimestampTrunc: timestamptrunc_sql, 292 exp.TimeStrToTime: timestrtotime_sql, 293 exp.TimeToUnix: lambda self, e: f"EXTRACT(epoch_second FROM {self.sql(e, 'this')})", 294 exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression), 295 exp.ToChar: lambda self, e: self.function_fallback_sql(e), 296 exp.TsOrDsToDate: ts_or_ds_to_date_sql("snowflake"), 297 exp.UnixToTime: _unix_to_time_sql, 298 exp.DayOfWeek: rename_func("DAYOFWEEK"), 299 exp.Min: min_or_least, 300 } 301 302 TYPE_MAPPING = { 303 **generator.Generator.TYPE_MAPPING, # type: ignore 304 exp.DataType.Type.TIMESTAMP: "TIMESTAMPNTZ", 305 } 306 307 STAR_MAPPING = { 308 "except": "EXCLUDE", 309 "replace": "RENAME", 310 } 311 312 PROPERTIES_LOCATION = { 313 **generator.Generator.PROPERTIES_LOCATION, # type: ignore 314 exp.SetProperty: exp.Properties.Location.UNSUPPORTED, 315 } 316 317 def ilikeany_sql(self, expression: exp.ILikeAny) -> str: 318 return self.binary(expression, "ILIKE ANY") 319 320 def likeany_sql(self, expression: exp.LikeAny) -> str: 321 return self.binary(expression, "LIKE ANY") 322 323 def except_op(self, expression): 324 if not expression.args.get("distinct", False): 325 self.unsupported("EXCEPT with All is not supported in Snowflake") 326 return super().except_op(expression) 327 328 def intersect_op(self, expression): 329 if not expression.args.get("distinct", False): 330 self.unsupported("INTERSECT with All is not supported in Snowflake") 331 return super().intersect_op(expression) 332 333 def values_sql(self, expression: exp.Values) -> str: 334 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted. 335 336 We also want to make sure that after we find matches where we need to unquote a column that we prevent users 337 from adding quotes to the column by using the `identify` argument when generating the SQL. 338 """ 339 alias = expression.args.get("alias") 340 if alias and alias.args.get("columns"): 341 expression = expression.transform( 342 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 343 if isinstance(node, exp.Identifier) 344 and isinstance(node.parent, exp.TableAlias) 345 and node.arg_key == "columns" 346 else node, 347 ) 348 return self.no_identify(lambda: super(self.__class__, self).values_sql(expression)) 349 return super().values_sql(expression) 350 351 def settag_sql(self, expression: exp.SetTag) -> str: 352 action = "UNSET" if expression.args.get("unset") else "SET" 353 return f"{action} TAG {self.expressions(expression)}" 354 355 def select_sql(self, expression: exp.Select) -> str: 356 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted and also 357 that all columns in a SELECT are unquoted. We also want to make sure that after we find matches where we need 358 to unquote a column that we prevent users from adding quotes to the column by using the `identify` argument when 359 generating the SQL. 360 361 Note: We make an assumption that any columns referenced in a VALUES expression should be unquoted throughout the 362 expression. This might not be true in a case where the same column name can be sourced from another table that can 363 properly quote but should be true in most cases. 364 """ 365 values_identifiers = set( 366 flatten( 367 (v.args.get("alias") or exp.Alias()).args.get("columns", []) 368 for v in expression.find_all(exp.Values) 369 ) 370 ) 371 if values_identifiers: 372 expression = expression.transform( 373 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 374 if isinstance(node, exp.Identifier) and node in values_identifiers 375 else node, 376 ) 377 return self.no_identify(lambda: super(self.__class__, self).select_sql(expression)) 378 return super().select_sql(expression) 379 380 def describe_sql(self, expression: exp.Describe) -> str: 381 # Default to table if kind is unknown 382 kind_value = expression.args.get("kind") or "TABLE" 383 kind = f" {kind_value}" if kind_value else "" 384 this = f" {self.sql(expression, 'this')}" 385 return f"DESCRIBE{kind}{this}" 386 387 def generatedasidentitycolumnconstraint_sql( 388 self, expression: exp.GeneratedAsIdentityColumnConstraint 389 ) -> str: 390 start = expression.args.get("start") 391 start = f" START {start}" if start else "" 392 increment = expression.args.get("increment") 393 increment = f" INCREMENT {increment}" if increment else "" 394 return f"AUTOINCREMENT{start}{increment}"
142class Snowflake(Dialect): 143 null_ordering = "nulls_are_large" 144 time_format = "'yyyy-mm-dd hh24:mi:ss'" 145 146 time_mapping = { 147 "YYYY": "%Y", 148 "yyyy": "%Y", 149 "YY": "%y", 150 "yy": "%y", 151 "MMMM": "%B", 152 "mmmm": "%B", 153 "MON": "%b", 154 "mon": "%b", 155 "MM": "%m", 156 "mm": "%m", 157 "DD": "%d", 158 "dd": "%d", 159 "d": "%-d", 160 "DY": "%w", 161 "dy": "%w", 162 "HH24": "%H", 163 "hh24": "%H", 164 "HH12": "%I", 165 "hh12": "%I", 166 "MI": "%M", 167 "mi": "%M", 168 "SS": "%S", 169 "ss": "%S", 170 "FF": "%f", 171 "ff": "%f", 172 "FF6": "%f", 173 "ff6": "%f", 174 } 175 176 class Parser(parser.Parser): 177 FUNCTIONS = { 178 **parser.Parser.FUNCTIONS, 179 "ARRAYAGG": exp.ArrayAgg.from_arg_list, 180 "ARRAY_CONSTRUCT": exp.Array.from_arg_list, 181 "ARRAY_TO_STRING": exp.ArrayJoin.from_arg_list, 182 "DATE_TRUNC": date_trunc_to_time, 183 "DATEADD": lambda args: exp.DateAdd( 184 this=seq_get(args, 2), 185 expression=seq_get(args, 1), 186 unit=seq_get(args, 0), 187 ), 188 "DATEDIFF": lambda args: exp.DateDiff( 189 this=seq_get(args, 2), 190 expression=seq_get(args, 1), 191 unit=seq_get(args, 0), 192 ), 193 "DECODE": exp.Matches.from_arg_list, 194 "DIV0": _div0_to_if, 195 "IFF": exp.If.from_arg_list, 196 "NULLIFZERO": _nullifzero_to_if, 197 "OBJECT_CONSTRUCT": parser.parse_var_map, 198 "RLIKE": exp.RegexpLike.from_arg_list, 199 "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)), 200 "TO_ARRAY": exp.Array.from_arg_list, 201 "TO_VARCHAR": exp.ToChar.from_arg_list, 202 "TO_TIMESTAMP": _snowflake_to_timestamp, 203 "ZEROIFNULL": _zeroifnull_to_if, 204 } 205 206 FUNCTION_PARSERS = { 207 **parser.Parser.FUNCTION_PARSERS, 208 "DATE_PART": _parse_date_part, 209 } 210 FUNCTION_PARSERS.pop("TRIM") 211 212 FUNC_TOKENS = { 213 *parser.Parser.FUNC_TOKENS, 214 TokenType.RLIKE, 215 TokenType.TABLE, 216 } 217 218 COLUMN_OPERATORS = { 219 **parser.Parser.COLUMN_OPERATORS, # type: ignore 220 TokenType.COLON: lambda self, this, path: self.expression( 221 exp.Bracket, 222 this=this, 223 expressions=[path], 224 ), 225 } 226 227 RANGE_PARSERS = { 228 **parser.Parser.RANGE_PARSERS, # type: ignore 229 TokenType.LIKE_ANY: binary_range_parser(exp.LikeAny), 230 TokenType.ILIKE_ANY: binary_range_parser(exp.ILikeAny), 231 } 232 233 ALTER_PARSERS = { 234 **parser.Parser.ALTER_PARSERS, # type: ignore 235 "UNSET": lambda self: self._parse_alter_table_set_tag(unset=True), 236 "SET": lambda self: self._parse_alter_table_set_tag(), 237 } 238 239 def _parse_alter_table_set_tag(self, unset: bool = False) -> exp.Expression: 240 self._match_text_seq("TAG") 241 parser = t.cast(t.Callable, self._parse_id_var if unset else self._parse_conjunction) 242 return self.expression(exp.SetTag, expressions=self._parse_csv(parser), unset=unset) 243 244 class Tokenizer(tokens.Tokenizer): 245 QUOTES = ["'", "$$"] 246 STRING_ESCAPES = ["\\", "'"] 247 248 KEYWORDS = { 249 **tokens.Tokenizer.KEYWORDS, 250 "EXCLUDE": TokenType.EXCEPT, 251 "ILIKE ANY": TokenType.ILIKE_ANY, 252 "LIKE ANY": TokenType.LIKE_ANY, 253 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 254 "PUT": TokenType.COMMAND, 255 "RENAME": TokenType.REPLACE, 256 "TIMESTAMP_LTZ": TokenType.TIMESTAMPLTZ, 257 "TIMESTAMP_NTZ": TokenType.TIMESTAMP, 258 "TIMESTAMP_TZ": TokenType.TIMESTAMPTZ, 259 "TIMESTAMPNTZ": TokenType.TIMESTAMP, 260 "MINUS": TokenType.EXCEPT, 261 "SAMPLE": TokenType.TABLE_SAMPLE, 262 } 263 264 SINGLE_TOKENS = { 265 **tokens.Tokenizer.SINGLE_TOKENS, 266 "$": TokenType.PARAMETER, 267 } 268 269 class Generator(generator.Generator): 270 PARAMETER_TOKEN = "$" 271 MATCHED_BY_SOURCE = False 272 273 TRANSFORMS = { 274 **generator.Generator.TRANSFORMS, # type: ignore 275 exp.Array: inline_array_sql, 276 exp.ArrayConcat: rename_func("ARRAY_CAT"), 277 exp.ArrayJoin: rename_func("ARRAY_TO_STRING"), 278 exp.DateAdd: lambda self, e: self.func("DATEADD", e.text("unit"), e.expression, e.this), 279 exp.DateStrToDate: datestrtodate_sql, 280 exp.DataType: _datatype_sql, 281 exp.If: rename_func("IFF"), 282 exp.Map: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"), 283 exp.LogicalOr: rename_func("BOOLOR_AGG"), 284 exp.LogicalAnd: rename_func("BOOLAND_AGG"), 285 exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"), 286 exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}", 287 exp.Matches: rename_func("DECODE"), 288 exp.StrPosition: lambda self, e: self.func( 289 "POSITION", e.args.get("substr"), e.this, e.args.get("position") 290 ), 291 exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})", 292 exp.TimestampTrunc: timestamptrunc_sql, 293 exp.TimeStrToTime: timestrtotime_sql, 294 exp.TimeToUnix: lambda self, e: f"EXTRACT(epoch_second FROM {self.sql(e, 'this')})", 295 exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression), 296 exp.ToChar: lambda self, e: self.function_fallback_sql(e), 297 exp.TsOrDsToDate: ts_or_ds_to_date_sql("snowflake"), 298 exp.UnixToTime: _unix_to_time_sql, 299 exp.DayOfWeek: rename_func("DAYOFWEEK"), 300 exp.Min: min_or_least, 301 } 302 303 TYPE_MAPPING = { 304 **generator.Generator.TYPE_MAPPING, # type: ignore 305 exp.DataType.Type.TIMESTAMP: "TIMESTAMPNTZ", 306 } 307 308 STAR_MAPPING = { 309 "except": "EXCLUDE", 310 "replace": "RENAME", 311 } 312 313 PROPERTIES_LOCATION = { 314 **generator.Generator.PROPERTIES_LOCATION, # type: ignore 315 exp.SetProperty: exp.Properties.Location.UNSUPPORTED, 316 } 317 318 def ilikeany_sql(self, expression: exp.ILikeAny) -> str: 319 return self.binary(expression, "ILIKE ANY") 320 321 def likeany_sql(self, expression: exp.LikeAny) -> str: 322 return self.binary(expression, "LIKE ANY") 323 324 def except_op(self, expression): 325 if not expression.args.get("distinct", False): 326 self.unsupported("EXCEPT with All is not supported in Snowflake") 327 return super().except_op(expression) 328 329 def intersect_op(self, expression): 330 if not expression.args.get("distinct", False): 331 self.unsupported("INTERSECT with All is not supported in Snowflake") 332 return super().intersect_op(expression) 333 334 def values_sql(self, expression: exp.Values) -> str: 335 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted. 336 337 We also want to make sure that after we find matches where we need to unquote a column that we prevent users 338 from adding quotes to the column by using the `identify` argument when generating the SQL. 339 """ 340 alias = expression.args.get("alias") 341 if alias and alias.args.get("columns"): 342 expression = expression.transform( 343 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 344 if isinstance(node, exp.Identifier) 345 and isinstance(node.parent, exp.TableAlias) 346 and node.arg_key == "columns" 347 else node, 348 ) 349 return self.no_identify(lambda: super(self.__class__, self).values_sql(expression)) 350 return super().values_sql(expression) 351 352 def settag_sql(self, expression: exp.SetTag) -> str: 353 action = "UNSET" if expression.args.get("unset") else "SET" 354 return f"{action} TAG {self.expressions(expression)}" 355 356 def select_sql(self, expression: exp.Select) -> str: 357 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted and also 358 that all columns in a SELECT are unquoted. We also want to make sure that after we find matches where we need 359 to unquote a column that we prevent users from adding quotes to the column by using the `identify` argument when 360 generating the SQL. 361 362 Note: We make an assumption that any columns referenced in a VALUES expression should be unquoted throughout the 363 expression. This might not be true in a case where the same column name can be sourced from another table that can 364 properly quote but should be true in most cases. 365 """ 366 values_identifiers = set( 367 flatten( 368 (v.args.get("alias") or exp.Alias()).args.get("columns", []) 369 for v in expression.find_all(exp.Values) 370 ) 371 ) 372 if values_identifiers: 373 expression = expression.transform( 374 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 375 if isinstance(node, exp.Identifier) and node in values_identifiers 376 else node, 377 ) 378 return self.no_identify(lambda: super(self.__class__, self).select_sql(expression)) 379 return super().select_sql(expression) 380 381 def describe_sql(self, expression: exp.Describe) -> str: 382 # Default to table if kind is unknown 383 kind_value = expression.args.get("kind") or "TABLE" 384 kind = f" {kind_value}" if kind_value else "" 385 this = f" {self.sql(expression, 'this')}" 386 return f"DESCRIBE{kind}{this}" 387 388 def generatedasidentitycolumnconstraint_sql( 389 self, expression: exp.GeneratedAsIdentityColumnConstraint 390 ) -> str: 391 start = expression.args.get("start") 392 start = f" START {start}" if start else "" 393 increment = expression.args.get("increment") 394 increment = f" INCREMENT {increment}" if increment else "" 395 return f"AUTOINCREMENT{start}{increment}"
176 class Parser(parser.Parser): 177 FUNCTIONS = { 178 **parser.Parser.FUNCTIONS, 179 "ARRAYAGG": exp.ArrayAgg.from_arg_list, 180 "ARRAY_CONSTRUCT": exp.Array.from_arg_list, 181 "ARRAY_TO_STRING": exp.ArrayJoin.from_arg_list, 182 "DATE_TRUNC": date_trunc_to_time, 183 "DATEADD": lambda args: exp.DateAdd( 184 this=seq_get(args, 2), 185 expression=seq_get(args, 1), 186 unit=seq_get(args, 0), 187 ), 188 "DATEDIFF": lambda args: exp.DateDiff( 189 this=seq_get(args, 2), 190 expression=seq_get(args, 1), 191 unit=seq_get(args, 0), 192 ), 193 "DECODE": exp.Matches.from_arg_list, 194 "DIV0": _div0_to_if, 195 "IFF": exp.If.from_arg_list, 196 "NULLIFZERO": _nullifzero_to_if, 197 "OBJECT_CONSTRUCT": parser.parse_var_map, 198 "RLIKE": exp.RegexpLike.from_arg_list, 199 "SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)), 200 "TO_ARRAY": exp.Array.from_arg_list, 201 "TO_VARCHAR": exp.ToChar.from_arg_list, 202 "TO_TIMESTAMP": _snowflake_to_timestamp, 203 "ZEROIFNULL": _zeroifnull_to_if, 204 } 205 206 FUNCTION_PARSERS = { 207 **parser.Parser.FUNCTION_PARSERS, 208 "DATE_PART": _parse_date_part, 209 } 210 FUNCTION_PARSERS.pop("TRIM") 211 212 FUNC_TOKENS = { 213 *parser.Parser.FUNC_TOKENS, 214 TokenType.RLIKE, 215 TokenType.TABLE, 216 } 217 218 COLUMN_OPERATORS = { 219 **parser.Parser.COLUMN_OPERATORS, # type: ignore 220 TokenType.COLON: lambda self, this, path: self.expression( 221 exp.Bracket, 222 this=this, 223 expressions=[path], 224 ), 225 } 226 227 RANGE_PARSERS = { 228 **parser.Parser.RANGE_PARSERS, # type: ignore 229 TokenType.LIKE_ANY: binary_range_parser(exp.LikeAny), 230 TokenType.ILIKE_ANY: binary_range_parser(exp.ILikeAny), 231 } 232 233 ALTER_PARSERS = { 234 **parser.Parser.ALTER_PARSERS, # type: ignore 235 "UNSET": lambda self: self._parse_alter_table_set_tag(unset=True), 236 "SET": lambda self: self._parse_alter_table_set_tag(), 237 } 238 239 def _parse_alter_table_set_tag(self, unset: bool = False) -> exp.Expression: 240 self._match_text_seq("TAG") 241 parser = t.cast(t.Callable, self._parse_id_var if unset else self._parse_conjunction) 242 return self.expression(exp.SetTag, expressions=self._parse_csv(parser), unset=unset)
Parser consumes a list of tokens produced by the sqlglot.tokens.Tokenizer
and produces
a parsed syntax tree.
Arguments:
- error_level: the desired error level. Default: ErrorLevel.RAISE
- error_message_context: determines the amount of context to capture from a query string when displaying the error message (in number of characters). Default: 50.
- index_offset: Index offset for arrays eg ARRAY[0] vs ARRAY[1] as the head of a list. Default: 0
- alias_post_tablesample: If the table alias comes after tablesample. Default: False
- max_errors: Maximum number of error messages to include in a raised ParseError. This is only relevant if error_level is ErrorLevel.RAISE. Default: 3
- null_ordering: Indicates the default null ordering method to use if not explicitly set. Options are "nulls_are_small", "nulls_are_large", "nulls_are_last". Default: "nulls_are_small"
Inherited Members
244 class Tokenizer(tokens.Tokenizer): 245 QUOTES = ["'", "$$"] 246 STRING_ESCAPES = ["\\", "'"] 247 248 KEYWORDS = { 249 **tokens.Tokenizer.KEYWORDS, 250 "EXCLUDE": TokenType.EXCEPT, 251 "ILIKE ANY": TokenType.ILIKE_ANY, 252 "LIKE ANY": TokenType.LIKE_ANY, 253 "MATCH_RECOGNIZE": TokenType.MATCH_RECOGNIZE, 254 "PUT": TokenType.COMMAND, 255 "RENAME": TokenType.REPLACE, 256 "TIMESTAMP_LTZ": TokenType.TIMESTAMPLTZ, 257 "TIMESTAMP_NTZ": TokenType.TIMESTAMP, 258 "TIMESTAMP_TZ": TokenType.TIMESTAMPTZ, 259 "TIMESTAMPNTZ": TokenType.TIMESTAMP, 260 "MINUS": TokenType.EXCEPT, 261 "SAMPLE": TokenType.TABLE_SAMPLE, 262 } 263 264 SINGLE_TOKENS = { 265 **tokens.Tokenizer.SINGLE_TOKENS, 266 "$": TokenType.PARAMETER, 267 }
Inherited Members
269 class Generator(generator.Generator): 270 PARAMETER_TOKEN = "$" 271 MATCHED_BY_SOURCE = False 272 273 TRANSFORMS = { 274 **generator.Generator.TRANSFORMS, # type: ignore 275 exp.Array: inline_array_sql, 276 exp.ArrayConcat: rename_func("ARRAY_CAT"), 277 exp.ArrayJoin: rename_func("ARRAY_TO_STRING"), 278 exp.DateAdd: lambda self, e: self.func("DATEADD", e.text("unit"), e.expression, e.this), 279 exp.DateStrToDate: datestrtodate_sql, 280 exp.DataType: _datatype_sql, 281 exp.If: rename_func("IFF"), 282 exp.Map: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"), 283 exp.LogicalOr: rename_func("BOOLOR_AGG"), 284 exp.LogicalAnd: rename_func("BOOLAND_AGG"), 285 exp.VarMap: lambda self, e: var_map_sql(self, e, "OBJECT_CONSTRUCT"), 286 exp.PartitionedByProperty: lambda self, e: f"PARTITION BY {self.sql(e, 'this')}", 287 exp.Matches: rename_func("DECODE"), 288 exp.StrPosition: lambda self, e: self.func( 289 "POSITION", e.args.get("substr"), e.this, e.args.get("position") 290 ), 291 exp.StrToTime: lambda self, e: f"TO_TIMESTAMP({self.sql(e, 'this')}, {self.format_time(e)})", 292 exp.TimestampTrunc: timestamptrunc_sql, 293 exp.TimeStrToTime: timestrtotime_sql, 294 exp.TimeToUnix: lambda self, e: f"EXTRACT(epoch_second FROM {self.sql(e, 'this')})", 295 exp.Trim: lambda self, e: self.func("TRIM", e.this, e.expression), 296 exp.ToChar: lambda self, e: self.function_fallback_sql(e), 297 exp.TsOrDsToDate: ts_or_ds_to_date_sql("snowflake"), 298 exp.UnixToTime: _unix_to_time_sql, 299 exp.DayOfWeek: rename_func("DAYOFWEEK"), 300 exp.Min: min_or_least, 301 } 302 303 TYPE_MAPPING = { 304 **generator.Generator.TYPE_MAPPING, # type: ignore 305 exp.DataType.Type.TIMESTAMP: "TIMESTAMPNTZ", 306 } 307 308 STAR_MAPPING = { 309 "except": "EXCLUDE", 310 "replace": "RENAME", 311 } 312 313 PROPERTIES_LOCATION = { 314 **generator.Generator.PROPERTIES_LOCATION, # type: ignore 315 exp.SetProperty: exp.Properties.Location.UNSUPPORTED, 316 } 317 318 def ilikeany_sql(self, expression: exp.ILikeAny) -> str: 319 return self.binary(expression, "ILIKE ANY") 320 321 def likeany_sql(self, expression: exp.LikeAny) -> str: 322 return self.binary(expression, "LIKE ANY") 323 324 def except_op(self, expression): 325 if not expression.args.get("distinct", False): 326 self.unsupported("EXCEPT with All is not supported in Snowflake") 327 return super().except_op(expression) 328 329 def intersect_op(self, expression): 330 if not expression.args.get("distinct", False): 331 self.unsupported("INTERSECT with All is not supported in Snowflake") 332 return super().intersect_op(expression) 333 334 def values_sql(self, expression: exp.Values) -> str: 335 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted. 336 337 We also want to make sure that after we find matches where we need to unquote a column that we prevent users 338 from adding quotes to the column by using the `identify` argument when generating the SQL. 339 """ 340 alias = expression.args.get("alias") 341 if alias and alias.args.get("columns"): 342 expression = expression.transform( 343 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 344 if isinstance(node, exp.Identifier) 345 and isinstance(node.parent, exp.TableAlias) 346 and node.arg_key == "columns" 347 else node, 348 ) 349 return self.no_identify(lambda: super(self.__class__, self).values_sql(expression)) 350 return super().values_sql(expression) 351 352 def settag_sql(self, expression: exp.SetTag) -> str: 353 action = "UNSET" if expression.args.get("unset") else "SET" 354 return f"{action} TAG {self.expressions(expression)}" 355 356 def select_sql(self, expression: exp.Select) -> str: 357 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted and also 358 that all columns in a SELECT are unquoted. We also want to make sure that after we find matches where we need 359 to unquote a column that we prevent users from adding quotes to the column by using the `identify` argument when 360 generating the SQL. 361 362 Note: We make an assumption that any columns referenced in a VALUES expression should be unquoted throughout the 363 expression. This might not be true in a case where the same column name can be sourced from another table that can 364 properly quote but should be true in most cases. 365 """ 366 values_identifiers = set( 367 flatten( 368 (v.args.get("alias") or exp.Alias()).args.get("columns", []) 369 for v in expression.find_all(exp.Values) 370 ) 371 ) 372 if values_identifiers: 373 expression = expression.transform( 374 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 375 if isinstance(node, exp.Identifier) and node in values_identifiers 376 else node, 377 ) 378 return self.no_identify(lambda: super(self.__class__, self).select_sql(expression)) 379 return super().select_sql(expression) 380 381 def describe_sql(self, expression: exp.Describe) -> str: 382 # Default to table if kind is unknown 383 kind_value = expression.args.get("kind") or "TABLE" 384 kind = f" {kind_value}" if kind_value else "" 385 this = f" {self.sql(expression, 'this')}" 386 return f"DESCRIBE{kind}{this}" 387 388 def generatedasidentitycolumnconstraint_sql( 389 self, expression: exp.GeneratedAsIdentityColumnConstraint 390 ) -> str: 391 start = expression.args.get("start") 392 start = f" START {start}" if start else "" 393 increment = expression.args.get("increment") 394 increment = f" INCREMENT {increment}" if increment else "" 395 return f"AUTOINCREMENT{start}{increment}"
Generator interprets the given syntax tree and produces a SQL string as an output.
Arguments:
- time_mapping (dict): the dictionary of custom time mappings in which the key represents a python time format and the output the target time format
- time_trie (trie): a trie of the time_mapping keys
- pretty (bool): if set to True the returned string will be formatted. Default: False.
- quote_start (str): specifies which starting character to use to delimit quotes. Default: '.
- quote_end (str): specifies which ending character to use to delimit quotes. Default: '.
- identifier_start (str): specifies which starting character to use to delimit identifiers. Default: ".
- identifier_end (str): specifies which ending character to use to delimit identifiers. Default: ".
- 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
334 def values_sql(self, expression: exp.Values) -> str: 335 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted. 336 337 We also want to make sure that after we find matches where we need to unquote a column that we prevent users 338 from adding quotes to the column by using the `identify` argument when generating the SQL. 339 """ 340 alias = expression.args.get("alias") 341 if alias and alias.args.get("columns"): 342 expression = expression.transform( 343 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 344 if isinstance(node, exp.Identifier) 345 and isinstance(node.parent, exp.TableAlias) 346 and node.arg_key == "columns" 347 else node, 348 ) 349 return self.no_identify(lambda: super(self.__class__, self).values_sql(expression)) 350 return super().values_sql(expression)
Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted.
We also want to make sure that after we find matches where we need to unquote a column that we prevent users
from adding quotes to the column by using the identify
argument when generating the SQL.
356 def select_sql(self, expression: exp.Select) -> str: 357 """Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted and also 358 that all columns in a SELECT are unquoted. We also want to make sure that after we find matches where we need 359 to unquote a column that we prevent users from adding quotes to the column by using the `identify` argument when 360 generating the SQL. 361 362 Note: We make an assumption that any columns referenced in a VALUES expression should be unquoted throughout the 363 expression. This might not be true in a case where the same column name can be sourced from another table that can 364 properly quote but should be true in most cases. 365 """ 366 values_identifiers = set( 367 flatten( 368 (v.args.get("alias") or exp.Alias()).args.get("columns", []) 369 for v in expression.find_all(exp.Values) 370 ) 371 ) 372 if values_identifiers: 373 expression = expression.transform( 374 lambda node: exp.Identifier(**{**node.args, "quoted": False}) 375 if isinstance(node, exp.Identifier) and node in values_identifiers 376 else node, 377 ) 378 return self.no_identify(lambda: super(self.__class__, self).select_sql(expression)) 379 return super().select_sql(expression)
Due to a bug in Snowflake we want to make sure that all columns in a VALUES table alias are unquoted and also
that all columns in a SELECT are unquoted. We also want to make sure that after we find matches where we need
to unquote a column that we prevent users from adding quotes to the column by using the identify
argument when
generating the SQL.
Note: We make an assumption that any columns referenced in a VALUES expression should be unquoted throughout the expression. This might not be true in a case where the same column name can be sourced from another table that can properly quote but should be true in most cases.
388 def generatedasidentitycolumnconstraint_sql( 389 self, expression: exp.GeneratedAsIdentityColumnConstraint 390 ) -> str: 391 start = expression.args.get("start") 392 start = f" START {start}" if start else "" 393 increment = expression.args.get("increment") 394 increment = f" INCREMENT {increment}" if increment else "" 395 return f"AUTOINCREMENT{start}{increment}"
Inherited Members
- sqlglot.generator.Generator
- Generator
- generate
- unsupported
- sep
- seg
- pad_comment
- maybe_comment
- wrap
- no_identify
- normalize_func
- indent
- sql
- uncache_sql
- cache_sql
- characterset_sql
- column_sql
- columndef_sql
- columnconstraint_sql
- autoincrementcolumnconstraint_sql
- compresscolumnconstraint_sql
- notnullcolumnconstraint_sql
- primarykeycolumnconstraint_sql
- uniquecolumnconstraint_sql
- create_sql
- prepend_ctes
- with_sql
- cte_sql
- tablealias_sql
- bitstring_sql
- hexstring_sql
- datatype_sql
- directory_sql
- delete_sql
- drop_sql
- except_sql
- 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
- introducer_sql
- pseudotype_sql
- returning_sql
- rowformatdelimitedproperty_sql
- table_sql
- tablesample_sql
- pivot_sql
- tuple_sql
- update_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
- lock_sql
- literal_sql
- loaddata_sql
- null_sql
- boolean_sql
- order_sql
- cluster_sql
- distribute_sql
- sort_sql
- ordered_sql
- matchrecognize_sql
- query_modifiers
- 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
- 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
- tochar_sql