sqlglot.generators.hive
1from __future__ import annotations 2 3import re 4import typing as t 5from functools import partial 6 7from sqlglot import exp, generator, transforms 8from sqlglot.dialects.dialect import ( 9 array_sort_order, 10 DATE_ADD_OR_SUB, 11 approx_count_distinct_sql, 12 arg_max_or_min_no_count, 13 datestrtodate_sql, 14 if_sql, 15 left_to_substring_sql, 16 max_or_greatest, 17 min_or_least, 18 no_ilike_sql, 19 no_recursive_cte_sql, 20 no_trycast_sql, 21 regexp_extract_sql, 22 regexp_replace_sql, 23 rename_func, 24 right_to_substring_sql, 25 strposition_sql, 26 struct_extract_sql, 27 time_format, 28 timestrtotime_sql, 29 trim_sql, 30 weekstart_unit_to_str, 31 var_map_sql, 32 sequence_sql, 33 property_sql, 34) 35from sqlglot.transforms import ( 36 remove_unique_constraints, 37 ctas_with_tmp_tables_to_create_tmp_view, 38 preprocess, 39 move_schema_columns_to_partitioned_by, 40) 41from sqlglot.generator import unsupported_args 42from sqlglot.time import format_time 43 44# These constants are duplicated from the Hive dialect class to avoid circular imports. 45# They must be kept in sync with Hive.TIME_FORMAT, Hive.DATE_FORMAT, Hive.DATEINT_FORMAT. 46HIVE_TIME_FORMAT = "'yyyy-MM-dd HH:mm:ss'" 47HIVE_DATE_FORMAT = "'yyyy-MM-dd'" 48HIVE_DATEINT_FORMAT = "'yyyyMMdd'" 49 50# The default formats above, as rendered by the lenient rewrite (non-padded month/day/time) 51HIVE_NON_PADDED_TIME_FORMATS = ("'yyyy-M-d H:m:s'", "'yyyy-M-d'") 52 53# Expressions that parse a string with a format (vs. formatting one, like TimeToStr). 54PARSE_TIME_EXPRESSIONS = (exp.StrToTime, exp.StrToDate, exp.StrToUnix, exp.TsOrDsToDate) 55 56CANONICAL_TIME_FORMAT = re.compile(r"%(?:[mdHIMS]strict|[-:].|.)") 57 58LAX_TO_NON_PADDED_FORMATS = { 59 "%m": "%-m", 60 "%d": "%-d", 61 "%H": "%-H", 62 "%I": "%-I", 63 "%M": "%-M", 64 "%S": "%-S", 65} 66 67 68def _lenient_parse_format(fmt: str) -> str: 69 """ 70 Changes a lax month/day/hour/minute/second in a canonical format to its non-padded form 71 (e.g. %m -> %-m), which java.time parses with or without a leading zero. This is only safe 72 for delimited specifiers, because adjacent fields parse greedily, so e.g. 'yyyyMd' (from 73 '%Y%m%d') can't even parse '20200101'. 74 75 The format is decomposed into specifiers (`formats`) and the interleaved literal text (`parts`). 76 Specifier i sits between parts[i] and parts[i + 1]. A specifier is changed only when its 77 neighbors don't touch a digit run, i.e., neither side is another specifier or a literal digit. 78 At the end, the pieces are zipped back together to produce the rewritten canonical format. 79 """ 80 parts = CANONICAL_TIME_FORMAT.split(fmt) 81 formats = CANONICAL_TIME_FORMAT.findall(fmt) 82 83 for i, fmt_ in enumerate(formats): 84 if fmt_ in LAX_TO_NON_PADDED_FORMATS: 85 left, right = parts[i], parts[i + 1] 86 left_adjacent = (not left and i > 0) or (left and left[-1].isdigit()) 87 right_adjacent = (not right and i < len(formats) - 1) or (right and right[0].isdigit()) 88 if not left_adjacent and not right_adjacent: 89 formats[i] = LAX_TO_NON_PADDED_FORMATS[fmt_] 90 91 return "".join(part + fmt_ for part, fmt_ in zip(parts, formats + [""])) 92 93 94# (FuncType, Multiplier) 95DATE_DELTA_INTERVAL = { 96 "YEAR": ("ADD_MONTHS", 12), 97 "MONTH": ("ADD_MONTHS", 1), 98 "QUARTER": ("ADD_MONTHS", 3), 99 "WEEK": ("DATE_ADD", 7), 100 "DAY": ("DATE_ADD", 1), 101} 102 103TIME_DIFF_FACTOR = { 104 "MILLISECOND": " * 1000", 105 "SECOND": "", 106 "MINUTE": " / 60", 107 "HOUR": " / 3600", 108} 109 110DIFF_MONTH_SWITCH = ("YEAR", "QUARTER", "MONTH") 111 112HIVE_TS_OR_DS_EXPRESSIONS: tuple[type[exp.Expr], ...] = ( 113 exp.DateDiff, 114 exp.Day, 115 exp.Month, 116 exp.Year, 117) 118 119 120def _add_date_sql(self: HiveGenerator, expression: DATE_ADD_OR_SUB) -> str: 121 if isinstance(expression, exp.TsOrDsAdd) and not expression.unit: 122 return self.func("DATE_ADD", expression.this, expression.expression) 123 124 unit = expression.text("unit").upper() 125 func, multiplier = DATE_DELTA_INTERVAL.get(unit, ("DATE_ADD", 1)) 126 127 if isinstance(expression, exp.DateSub): 128 multiplier *= -1 129 130 increment = expression.expression 131 if isinstance(increment, exp.Literal): 132 value = increment.to_py() if increment.is_number else int(increment.name) 133 increment = exp.Literal.number(value * multiplier) 134 elif multiplier != 1: 135 increment *= exp.Literal.number(multiplier) 136 137 return self.func(func, expression.this, increment) 138 139 140def _date_diff_sql(self: HiveGenerator, expression: exp.DateDiff | exp.TsOrDsDiff) -> str: 141 unit = expression.text("unit").upper() 142 143 factor = TIME_DIFF_FACTOR.get(unit) 144 if factor is not None: 145 left = self.sql(expression, "this") 146 right = self.sql(expression, "expression") 147 sec_diff = f"UNIX_TIMESTAMP({left}) - UNIX_TIMESTAMP({right})" 148 return f"({sec_diff}){factor}" if factor else sec_diff 149 150 months_between = unit in DIFF_MONTH_SWITCH 151 sql_func = "MONTHS_BETWEEN" if months_between else "DATEDIFF" 152 _, multiplier = DATE_DELTA_INTERVAL.get(unit, ("", 1)) 153 multiplier_sql = f" / {multiplier}" if multiplier > 1 else "" 154 diff_sql = f"{sql_func}({self.format_args(expression.this, expression.expression)})" 155 156 if months_between or multiplier_sql: 157 # MONTHS_BETWEEN returns a float, so we need to truncate the fractional part. 158 # For the same reason, we want to truncate if there's a divisor present. 159 diff_sql = f"CAST({diff_sql}{multiplier_sql} AS INT)" 160 161 return diff_sql 162 163 164@generator.unsupported_args(("expression", "Hive's SORT_ARRAY does not support a comparator.")) 165def _array_sort_sql(self: HiveGenerator, expression: exp.ArraySort) -> str: 166 asc, nulls_first = array_sort_order(expression) or (False, False) 167 if not asc or nulls_first is False: 168 self.unsupported("Hive's SORT_ARRAY only sorts ascending with NULLs first") 169 return self.func("SORT_ARRAY", expression.this) 170 171 172def _str_to_unix_sql(self: HiveGenerator, expression: exp.StrToUnix) -> str: 173 return self.func("UNIX_TIMESTAMP", expression.this, time_format("hive")(self, expression)) 174 175 176def _unix_to_time_sql(self: HiveGenerator, expression: exp.UnixToTime) -> str: 177 timestamp = self.sql(expression, "this") 178 scale = expression.args.get("scale") 179 if scale in (None, exp.UnixToTime.SECONDS): 180 return rename_func("FROM_UNIXTIME")(self, expression) 181 182 return f"FROM_UNIXTIME({timestamp} / POW(10, {scale}))" 183 184 185def _is_cast_time_format(self: HiveGenerator, expression: exp.Expr, time_format: str) -> bool: 186 """Checks whether CAST subsumes the expression's parse format.""" 187 if time_format in (HIVE_TIME_FORMAT, HIVE_DATE_FORMAT): 188 return True 189 190 if time_format in HIVE_NON_PADDED_TIME_FORMATS: 191 # The base render skips the lenient rewrite: a lax specifier pads back (e.g. %m -> MM), 192 # an explicit non-padded specifier (e.g. %-m) doesn't 193 padded_format = generator.Generator.format_time(self, expression) 194 return padded_format in (HIVE_TIME_FORMAT, HIVE_DATE_FORMAT) 195 196 return False 197 198 199def _str_to_date_sql(self: HiveGenerator, expression: exp.StrToDate) -> str: 200 this = self.sql(expression, "this") 201 time_format = self.format_time(expression) 202 if time_format and not _is_cast_time_format(self, expression, time_format): 203 this = f"FROM_UNIXTIME(UNIX_TIMESTAMP({this}, {time_format}))" 204 return f"CAST({this} AS DATE)" 205 206 207def _str_to_time_sql(self: HiveGenerator, expression: exp.StrToTime) -> str: 208 this = self.sql(expression, "this") 209 time_format = self.format_time(expression) 210 if time_format and not _is_cast_time_format(self, expression, time_format): 211 this = f"FROM_UNIXTIME(UNIX_TIMESTAMP({this}, {time_format}))" 212 return f"CAST({this} AS TIMESTAMP)" 213 214 215def _to_date_sql(self: HiveGenerator, expression: exp.TsOrDsToDate) -> str: 216 time_format = self.format_time(expression) 217 if time_format and not _is_cast_time_format(self, expression, time_format): 218 return self.func("TO_DATE", expression.this, time_format) 219 220 if isinstance(expression.parent, self.TS_OR_DS_EXPRESSIONS): 221 return self.sql(expression, "this") 222 223 return self.func("TO_DATE", expression.this) 224 225 226class HiveGenerator(generator.Generator): 227 BYTE_STRING_FUNCTION = "UNHEX" 228 SELECT_KINDS: tuple[str, ...] = () 229 TRY_SUPPORTED = False 230 SUPPORTS_UESCAPE = False 231 SUPPORTS_GROUPING_SETS_AS_SUFFIX = True 232 SUPPORTS_DECODE_CASE = False 233 LIMIT_FETCH = "LIMIT" 234 TABLESAMPLE_WITH_METHOD = False 235 JOIN_HINTS = False 236 TABLE_HINTS = False 237 QUERY_HINTS = False 238 INDEX_ON = "ON TABLE" 239 EXTRACT_ALLOWS_QUOTES = False 240 NVL2_SUPPORTED = False 241 LAST_DAY_SUPPORTS_DATE_PART = False 242 JSON_PATH_SINGLE_QUOTE_ESCAPE = True 243 SAFE_JSON_PATH_KEY_RE = re.compile(r"^[_\-a-zA-Z][\-\w]*$") 244 SUPPORTS_TO_NUMBER = False 245 WITH_PROPERTIES_PREFIX = "TBLPROPERTIES" 246 PARSE_JSON_NAME: str | None = "PARSE_JSON" 247 PAD_FILL_PATTERN_IS_REQUIRED = True 248 SUPPORTS_MEDIAN = False 249 SUPPORTS_TEMPORAL_PRECISION = False 250 ARRAY_SIZE_NAME = "SIZE" 251 ALTER_SET_TYPE = "" 252 253 EXPRESSIONS_WITHOUT_NESTED_CTES = { 254 exp.Insert, 255 exp.Select, 256 exp.Subquery, 257 exp.SetOperation, 258 } 259 260 SUPPORTED_JSON_PATH_PARTS = { 261 exp.JSONPathKey, 262 exp.JSONPathRoot, 263 exp.JSONPathSubscript, 264 exp.JSONPathWildcard, 265 } 266 267 TYPE_MAPPING = { 268 **generator.Generator.TYPE_MAPPING, 269 exp.DType.BIT: "BOOLEAN", 270 exp.DType.BLOB: "BINARY", 271 exp.DType.DATETIME: "TIMESTAMP", 272 exp.DType.ROWVERSION: "BINARY", 273 exp.DType.TEXT: "STRING", 274 exp.DType.TIME: "TIMESTAMP", 275 exp.DType.TIMESTAMPNTZ: "TIMESTAMP", 276 exp.DType.TIMESTAMPTZ: "TIMESTAMP", 277 exp.DType.UTINYINT: "SMALLINT", 278 exp.DType.VARBINARY: "BINARY", 279 } 280 281 TRANSFORMS = { 282 **generator.Generator.TRANSFORMS, 283 exp.Property: property_sql, 284 exp.AnyValue: rename_func("FIRST"), 285 exp.ApproxDistinct: approx_count_distinct_sql, 286 exp.ArgMax: arg_max_or_min_no_count("MAX_BY"), 287 exp.ArgMin: arg_max_or_min_no_count("MIN_BY"), 288 exp.Array: transforms.preprocess([transforms.inherit_struct_field_names]), 289 exp.ArrayConcat: rename_func("CONCAT"), 290 exp.ArrayToString: lambda self, e: self.func("CONCAT_WS", e.expression, e.this), 291 exp.ArraySort: _array_sort_sql, 292 exp.With: no_recursive_cte_sql, 293 exp.DateAdd: _add_date_sql, 294 exp.DateDiff: _date_diff_sql, 295 exp.DateStrToDate: datestrtodate_sql, 296 exp.DateSub: _add_date_sql, 297 exp.DateToDi: lambda self, e: ( 298 f"CAST(DATE_FORMAT({self.sql(e, 'this')}, {HIVE_DATEINT_FORMAT}) AS INT)" 299 ), 300 exp.DiToDate: lambda self, e: ( 301 f"TO_DATE(CAST({self.sql(e, 'this')} AS STRING), {HIVE_DATEINT_FORMAT})" 302 ), 303 exp.StorageHandlerProperty: lambda self, e: f"STORED BY {self.sql(e, 'this')}", 304 exp.FromBase64: rename_func("UNBASE64"), 305 exp.GenerateSeries: sequence_sql, 306 exp.GenerateDateArray: sequence_sql, 307 exp.If: if_sql(), 308 exp.ILike: no_ilike_sql, 309 exp.IntDiv: lambda self, e: self.binary(e, "DIV"), 310 exp.IsNan: rename_func("ISNAN"), 311 exp.JSONExtract: lambda self, e: self.func("GET_JSON_OBJECT", e.this, e.expression), 312 exp.JSONExtractScalar: lambda self, e: self.func("GET_JSON_OBJECT", e.this, e.expression), 313 exp.JSONFormat: rename_func("TO_JSON"), 314 exp.Left: left_to_substring_sql, 315 exp.Map: var_map_sql, 316 exp.Max: max_or_greatest, 317 exp.MD5Digest: lambda self, e: self.func("UNHEX", self.func("MD5", e.this)), 318 exp.Min: min_or_least, 319 exp.MonthsBetween: lambda self, e: self.func("MONTHS_BETWEEN", e.this, e.expression), 320 exp.NotNullColumnConstraint: lambda _, e: "" if e.args.get("allow_null") else "NOT NULL", 321 exp.VarMap: var_map_sql, 322 exp.Create: preprocess( 323 [ 324 remove_unique_constraints, 325 ctas_with_tmp_tables_to_create_tmp_view, 326 move_schema_columns_to_partitioned_by, 327 ] 328 ), 329 exp.Quantile: rename_func("PERCENTILE"), 330 exp.ApproxQuantile: rename_func("PERCENTILE_APPROX"), 331 exp.RegexpExtract: regexp_extract_sql, 332 exp.RegexpExtractAll: regexp_extract_sql, 333 exp.RegexpReplace: regexp_replace_sql, 334 exp.RegexpLike: lambda self, e: self.binary(e, "RLIKE"), 335 exp.RegexpSplit: rename_func("SPLIT"), 336 exp.Right: right_to_substring_sql, 337 exp.SchemaCommentProperty: lambda self, e: self.naked_property(e), 338 exp.ArrayUniqueAgg: rename_func("COLLECT_SET"), 339 exp.Split: lambda self, e: self.func( 340 "SPLIT", e.this, self.func("CONCAT", "'\\\\Q'", e.expression, "'\\\\E'") 341 ), 342 exp.Select: transforms.preprocess( 343 [ 344 transforms.eliminate_qualify, 345 transforms.eliminate_distinct_on, 346 partial(transforms.unnest_to_explode, unnest_using_arrays_zip=False), 347 transforms.any_to_exists, 348 ] 349 ), 350 exp.StrPosition: lambda self, e: strposition_sql( 351 self, e, func_name="LOCATE", supports_position=True 352 ), 353 exp.StrToDate: _str_to_date_sql, 354 exp.StrToTime: _str_to_time_sql, 355 exp.StrToUnix: _str_to_unix_sql, 356 exp.StructExtract: struct_extract_sql, 357 exp.StarMap: rename_func("MAP"), 358 exp.Table: transforms.preprocess([transforms.unnest_generate_series]), 359 exp.TimeStrToDate: rename_func("TO_DATE"), 360 exp.TimeStrToTime: timestrtotime_sql, 361 exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"), 362 exp.TimestampTrunc: lambda self, e: self.func( 363 "TRUNC", e.this, weekstart_unit_to_str(self, e) 364 ), 365 exp.TimeToUnix: rename_func("UNIX_TIMESTAMP"), 366 exp.ToBase64: rename_func("BASE64"), 367 exp.TsOrDiToDi: lambda self, e: ( 368 f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS STRING), '-', ''), 1, 8) AS INT)" 369 ), 370 exp.TsOrDsAdd: _add_date_sql, 371 exp.TsOrDsDiff: _date_diff_sql, 372 exp.TsOrDsToDate: _to_date_sql, 373 exp.TryCast: no_trycast_sql, 374 exp.Trim: trim_sql, 375 exp.Unicode: rename_func("ASCII"), 376 exp.UnixToStr: lambda self, e: self.func( 377 "FROM_UNIXTIME", e.this, time_format("hive")(self, e) 378 ), 379 exp.UnixToTime: _unix_to_time_sql, 380 exp.UnixToTimeStr: rename_func("FROM_UNIXTIME"), 381 exp.Unnest: rename_func("EXPLODE"), 382 exp.PartitionedByProperty: lambda self, e: f"PARTITIONED BY {self.sql(e, 'this')}", 383 exp.NumberToStr: rename_func("FORMAT_NUMBER"), 384 exp.National: lambda self, e: self.national_sql(e, prefix=""), 385 exp.ClusteredColumnConstraint: lambda self, e: ( 386 f"({self.expressions(e, 'this', indent=False)})" 387 ), 388 exp.NonClusteredColumnConstraint: lambda self, e: ( 389 f"({self.expressions(e, 'this', indent=False)})" 390 ), 391 exp.NotForReplicationColumnConstraint: lambda *_: "", 392 exp.OnProperty: lambda *_: "", 393 exp.PartitionedByBucket: lambda self, e: self.func("BUCKET", e.expression, e.this), 394 exp.PartitionByTruncate: lambda self, e: self.func("TRUNCATE", e.expression, e.this), 395 exp.PrimaryKeyColumnConstraint: lambda *_: "PRIMARY KEY", 396 exp.WeekOfYear: rename_func("WEEKOFYEAR"), 397 exp.DayOfMonth: rename_func("DAYOFMONTH"), 398 exp.DayOfWeek: rename_func("DAYOFWEEK"), 399 exp.Levenshtein: unsupported_args("ins_cost", "del_cost", "sub_cost", "max_dist")( 400 rename_func("LEVENSHTEIN") 401 ), 402 } 403 404 PROPERTIES_LOCATION = { 405 **generator.Generator.PROPERTIES_LOCATION, 406 exp.FileFormatProperty: exp.Properties.Location.POST_SCHEMA, 407 exp.PartitionedByProperty: exp.Properties.Location.POST_SCHEMA, 408 exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED, 409 exp.WithDataProperty: exp.Properties.Location.UNSUPPORTED, 410 } 411 412 TS_OR_DS_EXPRESSIONS: t.ClassVar = HIVE_TS_OR_DS_EXPRESSIONS 413 414 IGNORE_NULLS_FUNCS: t.ClassVar = (exp.First, exp.Last, exp.FirstValue, exp.LastValue) 415 416 def format_time( 417 self, 418 expression: exp.Expr, 419 inverse_time_mapping: dict[str, str] | None = None, 420 inverse_time_trie: dict | None = None, 421 ) -> str | None: 422 # Inferred property because this method is reused by other dialects under Hive 423 is_dialect_strict = self.dialect.TIME_MAPPING.get("MM") == "%mstrict" 424 425 if ( 426 is_dialect_strict 427 and inverse_time_mapping is None 428 and isinstance(expression, PARSE_TIME_EXPRESSIONS) 429 ): 430 # Render a lenient %m/%d non-padded (M/d) so single-digit sources stay parseable 431 return format_time( 432 _lenient_parse_format(self.sql(expression, "format")), 433 self.dialect.INVERSE_TIME_MAPPING, 434 self.dialect.INVERSE_TIME_TRIE, 435 ) 436 437 return super().format_time(expression, inverse_time_mapping, inverse_time_trie) 438 439 def ignorenulls_sql(self, expression: exp.IgnoreNulls) -> str: 440 this = expression.this 441 if isinstance(this, self.IGNORE_NULLS_FUNCS): 442 return self.func(this.sql_name(), this.this, exp.true()) 443 444 return super().ignorenulls_sql(expression) 445 446 def unnest_sql(self, expression: exp.Unnest) -> str: 447 return rename_func("EXPLODE")(self, expression) 448 449 def _jsonpathkey_sql(self, expression: exp.JSONPathKey) -> str: 450 if isinstance(expression.this, exp.JSONPathWildcard): 451 self.unsupported("Unsupported wildcard in JSONPathKey expression") 452 return "" 453 454 return super()._jsonpathkey_sql(expression) 455 456 def parameter_sql(self, expression: exp.Parameter) -> str: 457 this = self.sql(expression, "this") 458 expression_sql = self.sql(expression, "expression") 459 460 parent = expression.parent 461 this = f"{this}:{expression_sql}" if expression_sql else this 462 463 if isinstance(parent, exp.EQ) and isinstance(parent.parent, exp.SetItem): 464 # We need to produce SET key = value instead of SET ${key} = value 465 return this 466 467 return f"${{{this}}}" 468 469 def schema_sql(self, expression: exp.Schema) -> str: 470 for ordered in expression.find_all(exp.Ordered): 471 if ordered.args.get("desc") is False: 472 ordered.set("desc", None) 473 474 return super().schema_sql(expression) 475 476 def constraint_sql(self, expression: exp.Constraint) -> str: 477 for prop in list(expression.find_all(exp.Properties)): 478 prop.pop() 479 480 this = self.sql(expression, "this") 481 expressions = self.expressions(expression, sep=" ", flat=True) 482 return f"CONSTRAINT {this} {expressions}" 483 484 def rowformatserdeproperty_sql(self, expression: exp.RowFormatSerdeProperty) -> str: 485 serde_props = self.sql(expression, "serde_properties") 486 serde_props = f" {serde_props}" if serde_props else "" 487 return f"ROW FORMAT SERDE {self.sql(expression, 'this')}{serde_props}" 488 489 def arrayagg_sql(self, expression: exp.ArrayAgg) -> str: 490 return self.func( 491 "COLLECT_LIST", 492 expression.this.this if isinstance(expression.this, exp.Order) else expression.this, 493 ) 494 495 # Hive/Spark lack native numeric TRUNC. CAST to BIGINT truncates toward zero (not rounds). 496 # Potential enhancement: a TRUNC_TEMPLATE using FLOOR/CEIL with scale (Spark 3.3+) 497 # could preserve decimals: CASE WHEN x >= 0 THEN FLOOR(x, d) ELSE CEIL(x, d) END 498 @unsupported_args("decimals") 499 def trunc_sql(self, expression: exp.Trunc) -> str: 500 return self.sql(exp.cast(expression.this, exp.DType.BIGINT)) 501 502 def datatype_sql(self, expression: exp.DataType) -> str: 503 if expression.this in self.PARAMETERIZABLE_TEXT_TYPES and ( 504 not expression.expressions or expression.expressions[0].name == "MAX" 505 ): 506 expression.set("this", exp.DType.TEXT) 507 expression.set("expressions", None) 508 elif expression.is_type(exp.DType.TEXT) and expression.expressions: 509 expression.set("this", exp.DType.VARCHAR) 510 elif ( 511 not self.SUPPORTS_TEMPORAL_PRECISION and expression.this in exp.DataType.TEMPORAL_TYPES 512 ): 513 expression.set("expressions", None) 514 elif expression.is_type("float"): 515 size_expression = expression.find(exp.DataTypeParam) 516 if size_expression: 517 size = int(size_expression.name) 518 expression.set("this", exp.DType.FLOAT if size <= 32 else exp.DType.DOUBLE) 519 expression.set("expressions", None) 520 return super().datatype_sql(expression) 521 522 def version_sql(self, expression: exp.Version) -> str: 523 sql = super().version_sql(expression) 524 return sql.replace("FOR ", "", 1) 525 526 def struct_sql(self, expression: exp.Struct) -> str: 527 values = [] 528 529 for i, e in enumerate(expression.expressions): 530 if isinstance(e, exp.PropertyEQ): 531 self.unsupported("Hive does not support named structs.") 532 values.append(e.expression) 533 else: 534 values.append(e) 535 536 return self.func("STRUCT", *values) 537 538 def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str: 539 return super().columndef_sql( 540 expression, 541 sep=( 542 ": " 543 if isinstance(expression.parent, exp.DataType) 544 and expression.parent.is_type("struct") 545 else sep 546 ), 547 ) 548 549 def altercolumn_sql(self, expression: exp.AlterColumn) -> str: 550 if expression.args.get("exists"): 551 self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect") 552 553 this = self.sql(expression, "this") 554 new_name = self.sql(expression, "rename_to") or this 555 dtype = self.sql(expression, "dtype") 556 comment = ( 557 f" COMMENT {self.sql(expression, 'comment')}" if self.sql(expression, "comment") else "" 558 ) 559 default = self.sql(expression, "default") 560 visible = expression.args.get("visible") 561 allow_null = expression.args.get("allow_null") 562 drop = expression.args.get("drop") 563 564 if any([default, drop, visible]) or allow_null is not None: 565 self.unsupported("Unsupported CHANGE COLUMN syntax") 566 567 if not dtype: 568 self.unsupported("CHANGE COLUMN without a type is not supported") 569 570 return f"CHANGE COLUMN {this} {new_name} {dtype}{comment}" 571 572 def renamecolumn_sql(self, expression: exp.RenameColumn) -> str: 573 self.unsupported("Cannot rename columns without data type defined in Hive") 574 return "" 575 576 def alterset_sql(self, expression: exp.AlterSet) -> str: 577 exprs = self.expressions(expression, flat=True) 578 exprs = f" {exprs}" if exprs else "" 579 location = self.sql(expression, "location") 580 location = f" LOCATION {location}" if location else "" 581 file_format = self.expressions(expression, key="file_format", flat=True, sep=" ") 582 file_format = f" FILEFORMAT {file_format}" if file_format else "" 583 serde = self.sql(expression, "serde") 584 serde = f" SERDE {serde}" if serde else "" 585 tags = self.expressions(expression, key="tag", flat=True, sep="") 586 tags = f" TAGS {tags}" if tags else "" 587 588 return f"SET{serde}{exprs}{location}{file_format}{tags}" 589 590 def serdeproperties_sql(self, expression: exp.SerdeProperties) -> str: 591 prefix = "WITH " if expression.args.get("with_") else "" 592 exprs = self.expressions(expression, flat=True) 593 594 return f"{prefix}SERDEPROPERTIES ({exprs})" 595 596 def exists_sql(self, expression: exp.Exists) -> str: 597 if expression.expression: 598 return self.function_fallback_sql(expression) 599 600 return super().exists_sql(expression) 601 602 def timetostr_sql(self, expression: exp.TimeToStr) -> str: 603 this = expression.this 604 if isinstance(this, exp.TimeStrToTime): 605 this = this.this 606 607 return self.func("DATE_FORMAT", this, self.format_time(expression)) 608 609 def usingproperty_sql(self, expression: exp.UsingProperty) -> str: 610 kind = expression.args.get("kind") 611 return f"USING {kind} {self.sql(expression, 'this')}" 612 613 def fileformatproperty_sql(self, expression: exp.FileFormatProperty) -> str: 614 if isinstance(expression.this, exp.InputOutputFormat): 615 this = self.sql(expression, "this") 616 else: 617 this = expression.name.upper() 618 619 return f"STORED AS {this}"
HIVE_TIME_FORMAT =
"'yyyy-MM-dd HH:mm:ss'"
HIVE_DATE_FORMAT =
"'yyyy-MM-dd'"
HIVE_DATEINT_FORMAT =
"'yyyyMMdd'"
HIVE_NON_PADDED_TIME_FORMATS =
("'yyyy-M-d H:m:s'", "'yyyy-M-d'")
PARSE_TIME_EXPRESSIONS =
(<class 'sqlglot.expressions.temporal.StrToTime'>, <class 'sqlglot.expressions.temporal.StrToDate'>, <class 'sqlglot.expressions.temporal.StrToUnix'>, <class 'sqlglot.expressions.temporal.TsOrDsToDate'>)
CANONICAL_TIME_FORMAT =
re.compile('%(?:[mdHIMS]strict|[-:].|.)')
LAX_TO_NON_PADDED_FORMATS =
{'%m': '%-m', '%d': '%-d', '%H': '%-H', '%I': '%-I', '%M': '%-M', '%S': '%-S'}
DATE_DELTA_INTERVAL =
{'YEAR': ('ADD_MONTHS', 12), 'MONTH': ('ADD_MONTHS', 1), 'QUARTER': ('ADD_MONTHS', 3), 'WEEK': ('DATE_ADD', 7), 'DAY': ('DATE_ADD', 1)}
TIME_DIFF_FACTOR =
{'MILLISECOND': ' * 1000', 'SECOND': '', 'MINUTE': ' / 60', 'HOUR': ' / 3600'}
DIFF_MONTH_SWITCH =
('YEAR', 'QUARTER', 'MONTH')
HIVE_TS_OR_DS_EXPRESSIONS: tuple[type[sqlglot.expressions.core.Expr], ...] =
(<class 'sqlglot.expressions.temporal.DateDiff'>, <class 'sqlglot.expressions.temporal.Day'>, <class 'sqlglot.expressions.temporal.Month'>, <class 'sqlglot.expressions.temporal.Year'>)
227class HiveGenerator(generator.Generator): 228 BYTE_STRING_FUNCTION = "UNHEX" 229 SELECT_KINDS: tuple[str, ...] = () 230 TRY_SUPPORTED = False 231 SUPPORTS_UESCAPE = False 232 SUPPORTS_GROUPING_SETS_AS_SUFFIX = True 233 SUPPORTS_DECODE_CASE = False 234 LIMIT_FETCH = "LIMIT" 235 TABLESAMPLE_WITH_METHOD = False 236 JOIN_HINTS = False 237 TABLE_HINTS = False 238 QUERY_HINTS = False 239 INDEX_ON = "ON TABLE" 240 EXTRACT_ALLOWS_QUOTES = False 241 NVL2_SUPPORTED = False 242 LAST_DAY_SUPPORTS_DATE_PART = False 243 JSON_PATH_SINGLE_QUOTE_ESCAPE = True 244 SAFE_JSON_PATH_KEY_RE = re.compile(r"^[_\-a-zA-Z][\-\w]*$") 245 SUPPORTS_TO_NUMBER = False 246 WITH_PROPERTIES_PREFIX = "TBLPROPERTIES" 247 PARSE_JSON_NAME: str | None = "PARSE_JSON" 248 PAD_FILL_PATTERN_IS_REQUIRED = True 249 SUPPORTS_MEDIAN = False 250 SUPPORTS_TEMPORAL_PRECISION = False 251 ARRAY_SIZE_NAME = "SIZE" 252 ALTER_SET_TYPE = "" 253 254 EXPRESSIONS_WITHOUT_NESTED_CTES = { 255 exp.Insert, 256 exp.Select, 257 exp.Subquery, 258 exp.SetOperation, 259 } 260 261 SUPPORTED_JSON_PATH_PARTS = { 262 exp.JSONPathKey, 263 exp.JSONPathRoot, 264 exp.JSONPathSubscript, 265 exp.JSONPathWildcard, 266 } 267 268 TYPE_MAPPING = { 269 **generator.Generator.TYPE_MAPPING, 270 exp.DType.BIT: "BOOLEAN", 271 exp.DType.BLOB: "BINARY", 272 exp.DType.DATETIME: "TIMESTAMP", 273 exp.DType.ROWVERSION: "BINARY", 274 exp.DType.TEXT: "STRING", 275 exp.DType.TIME: "TIMESTAMP", 276 exp.DType.TIMESTAMPNTZ: "TIMESTAMP", 277 exp.DType.TIMESTAMPTZ: "TIMESTAMP", 278 exp.DType.UTINYINT: "SMALLINT", 279 exp.DType.VARBINARY: "BINARY", 280 } 281 282 TRANSFORMS = { 283 **generator.Generator.TRANSFORMS, 284 exp.Property: property_sql, 285 exp.AnyValue: rename_func("FIRST"), 286 exp.ApproxDistinct: approx_count_distinct_sql, 287 exp.ArgMax: arg_max_or_min_no_count("MAX_BY"), 288 exp.ArgMin: arg_max_or_min_no_count("MIN_BY"), 289 exp.Array: transforms.preprocess([transforms.inherit_struct_field_names]), 290 exp.ArrayConcat: rename_func("CONCAT"), 291 exp.ArrayToString: lambda self, e: self.func("CONCAT_WS", e.expression, e.this), 292 exp.ArraySort: _array_sort_sql, 293 exp.With: no_recursive_cte_sql, 294 exp.DateAdd: _add_date_sql, 295 exp.DateDiff: _date_diff_sql, 296 exp.DateStrToDate: datestrtodate_sql, 297 exp.DateSub: _add_date_sql, 298 exp.DateToDi: lambda self, e: ( 299 f"CAST(DATE_FORMAT({self.sql(e, 'this')}, {HIVE_DATEINT_FORMAT}) AS INT)" 300 ), 301 exp.DiToDate: lambda self, e: ( 302 f"TO_DATE(CAST({self.sql(e, 'this')} AS STRING), {HIVE_DATEINT_FORMAT})" 303 ), 304 exp.StorageHandlerProperty: lambda self, e: f"STORED BY {self.sql(e, 'this')}", 305 exp.FromBase64: rename_func("UNBASE64"), 306 exp.GenerateSeries: sequence_sql, 307 exp.GenerateDateArray: sequence_sql, 308 exp.If: if_sql(), 309 exp.ILike: no_ilike_sql, 310 exp.IntDiv: lambda self, e: self.binary(e, "DIV"), 311 exp.IsNan: rename_func("ISNAN"), 312 exp.JSONExtract: lambda self, e: self.func("GET_JSON_OBJECT", e.this, e.expression), 313 exp.JSONExtractScalar: lambda self, e: self.func("GET_JSON_OBJECT", e.this, e.expression), 314 exp.JSONFormat: rename_func("TO_JSON"), 315 exp.Left: left_to_substring_sql, 316 exp.Map: var_map_sql, 317 exp.Max: max_or_greatest, 318 exp.MD5Digest: lambda self, e: self.func("UNHEX", self.func("MD5", e.this)), 319 exp.Min: min_or_least, 320 exp.MonthsBetween: lambda self, e: self.func("MONTHS_BETWEEN", e.this, e.expression), 321 exp.NotNullColumnConstraint: lambda _, e: "" if e.args.get("allow_null") else "NOT NULL", 322 exp.VarMap: var_map_sql, 323 exp.Create: preprocess( 324 [ 325 remove_unique_constraints, 326 ctas_with_tmp_tables_to_create_tmp_view, 327 move_schema_columns_to_partitioned_by, 328 ] 329 ), 330 exp.Quantile: rename_func("PERCENTILE"), 331 exp.ApproxQuantile: rename_func("PERCENTILE_APPROX"), 332 exp.RegexpExtract: regexp_extract_sql, 333 exp.RegexpExtractAll: regexp_extract_sql, 334 exp.RegexpReplace: regexp_replace_sql, 335 exp.RegexpLike: lambda self, e: self.binary(e, "RLIKE"), 336 exp.RegexpSplit: rename_func("SPLIT"), 337 exp.Right: right_to_substring_sql, 338 exp.SchemaCommentProperty: lambda self, e: self.naked_property(e), 339 exp.ArrayUniqueAgg: rename_func("COLLECT_SET"), 340 exp.Split: lambda self, e: self.func( 341 "SPLIT", e.this, self.func("CONCAT", "'\\\\Q'", e.expression, "'\\\\E'") 342 ), 343 exp.Select: transforms.preprocess( 344 [ 345 transforms.eliminate_qualify, 346 transforms.eliminate_distinct_on, 347 partial(transforms.unnest_to_explode, unnest_using_arrays_zip=False), 348 transforms.any_to_exists, 349 ] 350 ), 351 exp.StrPosition: lambda self, e: strposition_sql( 352 self, e, func_name="LOCATE", supports_position=True 353 ), 354 exp.StrToDate: _str_to_date_sql, 355 exp.StrToTime: _str_to_time_sql, 356 exp.StrToUnix: _str_to_unix_sql, 357 exp.StructExtract: struct_extract_sql, 358 exp.StarMap: rename_func("MAP"), 359 exp.Table: transforms.preprocess([transforms.unnest_generate_series]), 360 exp.TimeStrToDate: rename_func("TO_DATE"), 361 exp.TimeStrToTime: timestrtotime_sql, 362 exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"), 363 exp.TimestampTrunc: lambda self, e: self.func( 364 "TRUNC", e.this, weekstart_unit_to_str(self, e) 365 ), 366 exp.TimeToUnix: rename_func("UNIX_TIMESTAMP"), 367 exp.ToBase64: rename_func("BASE64"), 368 exp.TsOrDiToDi: lambda self, e: ( 369 f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS STRING), '-', ''), 1, 8) AS INT)" 370 ), 371 exp.TsOrDsAdd: _add_date_sql, 372 exp.TsOrDsDiff: _date_diff_sql, 373 exp.TsOrDsToDate: _to_date_sql, 374 exp.TryCast: no_trycast_sql, 375 exp.Trim: trim_sql, 376 exp.Unicode: rename_func("ASCII"), 377 exp.UnixToStr: lambda self, e: self.func( 378 "FROM_UNIXTIME", e.this, time_format("hive")(self, e) 379 ), 380 exp.UnixToTime: _unix_to_time_sql, 381 exp.UnixToTimeStr: rename_func("FROM_UNIXTIME"), 382 exp.Unnest: rename_func("EXPLODE"), 383 exp.PartitionedByProperty: lambda self, e: f"PARTITIONED BY {self.sql(e, 'this')}", 384 exp.NumberToStr: rename_func("FORMAT_NUMBER"), 385 exp.National: lambda self, e: self.national_sql(e, prefix=""), 386 exp.ClusteredColumnConstraint: lambda self, e: ( 387 f"({self.expressions(e, 'this', indent=False)})" 388 ), 389 exp.NonClusteredColumnConstraint: lambda self, e: ( 390 f"({self.expressions(e, 'this', indent=False)})" 391 ), 392 exp.NotForReplicationColumnConstraint: lambda *_: "", 393 exp.OnProperty: lambda *_: "", 394 exp.PartitionedByBucket: lambda self, e: self.func("BUCKET", e.expression, e.this), 395 exp.PartitionByTruncate: lambda self, e: self.func("TRUNCATE", e.expression, e.this), 396 exp.PrimaryKeyColumnConstraint: lambda *_: "PRIMARY KEY", 397 exp.WeekOfYear: rename_func("WEEKOFYEAR"), 398 exp.DayOfMonth: rename_func("DAYOFMONTH"), 399 exp.DayOfWeek: rename_func("DAYOFWEEK"), 400 exp.Levenshtein: unsupported_args("ins_cost", "del_cost", "sub_cost", "max_dist")( 401 rename_func("LEVENSHTEIN") 402 ), 403 } 404 405 PROPERTIES_LOCATION = { 406 **generator.Generator.PROPERTIES_LOCATION, 407 exp.FileFormatProperty: exp.Properties.Location.POST_SCHEMA, 408 exp.PartitionedByProperty: exp.Properties.Location.POST_SCHEMA, 409 exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED, 410 exp.WithDataProperty: exp.Properties.Location.UNSUPPORTED, 411 } 412 413 TS_OR_DS_EXPRESSIONS: t.ClassVar = HIVE_TS_OR_DS_EXPRESSIONS 414 415 IGNORE_NULLS_FUNCS: t.ClassVar = (exp.First, exp.Last, exp.FirstValue, exp.LastValue) 416 417 def format_time( 418 self, 419 expression: exp.Expr, 420 inverse_time_mapping: dict[str, str] | None = None, 421 inverse_time_trie: dict | None = None, 422 ) -> str | None: 423 # Inferred property because this method is reused by other dialects under Hive 424 is_dialect_strict = self.dialect.TIME_MAPPING.get("MM") == "%mstrict" 425 426 if ( 427 is_dialect_strict 428 and inverse_time_mapping is None 429 and isinstance(expression, PARSE_TIME_EXPRESSIONS) 430 ): 431 # Render a lenient %m/%d non-padded (M/d) so single-digit sources stay parseable 432 return format_time( 433 _lenient_parse_format(self.sql(expression, "format")), 434 self.dialect.INVERSE_TIME_MAPPING, 435 self.dialect.INVERSE_TIME_TRIE, 436 ) 437 438 return super().format_time(expression, inverse_time_mapping, inverse_time_trie) 439 440 def ignorenulls_sql(self, expression: exp.IgnoreNulls) -> str: 441 this = expression.this 442 if isinstance(this, self.IGNORE_NULLS_FUNCS): 443 return self.func(this.sql_name(), this.this, exp.true()) 444 445 return super().ignorenulls_sql(expression) 446 447 def unnest_sql(self, expression: exp.Unnest) -> str: 448 return rename_func("EXPLODE")(self, expression) 449 450 def _jsonpathkey_sql(self, expression: exp.JSONPathKey) -> str: 451 if isinstance(expression.this, exp.JSONPathWildcard): 452 self.unsupported("Unsupported wildcard in JSONPathKey expression") 453 return "" 454 455 return super()._jsonpathkey_sql(expression) 456 457 def parameter_sql(self, expression: exp.Parameter) -> str: 458 this = self.sql(expression, "this") 459 expression_sql = self.sql(expression, "expression") 460 461 parent = expression.parent 462 this = f"{this}:{expression_sql}" if expression_sql else this 463 464 if isinstance(parent, exp.EQ) and isinstance(parent.parent, exp.SetItem): 465 # We need to produce SET key = value instead of SET ${key} = value 466 return this 467 468 return f"${{{this}}}" 469 470 def schema_sql(self, expression: exp.Schema) -> str: 471 for ordered in expression.find_all(exp.Ordered): 472 if ordered.args.get("desc") is False: 473 ordered.set("desc", None) 474 475 return super().schema_sql(expression) 476 477 def constraint_sql(self, expression: exp.Constraint) -> str: 478 for prop in list(expression.find_all(exp.Properties)): 479 prop.pop() 480 481 this = self.sql(expression, "this") 482 expressions = self.expressions(expression, sep=" ", flat=True) 483 return f"CONSTRAINT {this} {expressions}" 484 485 def rowformatserdeproperty_sql(self, expression: exp.RowFormatSerdeProperty) -> str: 486 serde_props = self.sql(expression, "serde_properties") 487 serde_props = f" {serde_props}" if serde_props else "" 488 return f"ROW FORMAT SERDE {self.sql(expression, 'this')}{serde_props}" 489 490 def arrayagg_sql(self, expression: exp.ArrayAgg) -> str: 491 return self.func( 492 "COLLECT_LIST", 493 expression.this.this if isinstance(expression.this, exp.Order) else expression.this, 494 ) 495 496 # Hive/Spark lack native numeric TRUNC. CAST to BIGINT truncates toward zero (not rounds). 497 # Potential enhancement: a TRUNC_TEMPLATE using FLOOR/CEIL with scale (Spark 3.3+) 498 # could preserve decimals: CASE WHEN x >= 0 THEN FLOOR(x, d) ELSE CEIL(x, d) END 499 @unsupported_args("decimals") 500 def trunc_sql(self, expression: exp.Trunc) -> str: 501 return self.sql(exp.cast(expression.this, exp.DType.BIGINT)) 502 503 def datatype_sql(self, expression: exp.DataType) -> str: 504 if expression.this in self.PARAMETERIZABLE_TEXT_TYPES and ( 505 not expression.expressions or expression.expressions[0].name == "MAX" 506 ): 507 expression.set("this", exp.DType.TEXT) 508 expression.set("expressions", None) 509 elif expression.is_type(exp.DType.TEXT) and expression.expressions: 510 expression.set("this", exp.DType.VARCHAR) 511 elif ( 512 not self.SUPPORTS_TEMPORAL_PRECISION and expression.this in exp.DataType.TEMPORAL_TYPES 513 ): 514 expression.set("expressions", None) 515 elif expression.is_type("float"): 516 size_expression = expression.find(exp.DataTypeParam) 517 if size_expression: 518 size = int(size_expression.name) 519 expression.set("this", exp.DType.FLOAT if size <= 32 else exp.DType.DOUBLE) 520 expression.set("expressions", None) 521 return super().datatype_sql(expression) 522 523 def version_sql(self, expression: exp.Version) -> str: 524 sql = super().version_sql(expression) 525 return sql.replace("FOR ", "", 1) 526 527 def struct_sql(self, expression: exp.Struct) -> str: 528 values = [] 529 530 for i, e in enumerate(expression.expressions): 531 if isinstance(e, exp.PropertyEQ): 532 self.unsupported("Hive does not support named structs.") 533 values.append(e.expression) 534 else: 535 values.append(e) 536 537 return self.func("STRUCT", *values) 538 539 def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str: 540 return super().columndef_sql( 541 expression, 542 sep=( 543 ": " 544 if isinstance(expression.parent, exp.DataType) 545 and expression.parent.is_type("struct") 546 else sep 547 ), 548 ) 549 550 def altercolumn_sql(self, expression: exp.AlterColumn) -> str: 551 if expression.args.get("exists"): 552 self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect") 553 554 this = self.sql(expression, "this") 555 new_name = self.sql(expression, "rename_to") or this 556 dtype = self.sql(expression, "dtype") 557 comment = ( 558 f" COMMENT {self.sql(expression, 'comment')}" if self.sql(expression, "comment") else "" 559 ) 560 default = self.sql(expression, "default") 561 visible = expression.args.get("visible") 562 allow_null = expression.args.get("allow_null") 563 drop = expression.args.get("drop") 564 565 if any([default, drop, visible]) or allow_null is not None: 566 self.unsupported("Unsupported CHANGE COLUMN syntax") 567 568 if not dtype: 569 self.unsupported("CHANGE COLUMN without a type is not supported") 570 571 return f"CHANGE COLUMN {this} {new_name} {dtype}{comment}" 572 573 def renamecolumn_sql(self, expression: exp.RenameColumn) -> str: 574 self.unsupported("Cannot rename columns without data type defined in Hive") 575 return "" 576 577 def alterset_sql(self, expression: exp.AlterSet) -> str: 578 exprs = self.expressions(expression, flat=True) 579 exprs = f" {exprs}" if exprs else "" 580 location = self.sql(expression, "location") 581 location = f" LOCATION {location}" if location else "" 582 file_format = self.expressions(expression, key="file_format", flat=True, sep=" ") 583 file_format = f" FILEFORMAT {file_format}" if file_format else "" 584 serde = self.sql(expression, "serde") 585 serde = f" SERDE {serde}" if serde else "" 586 tags = self.expressions(expression, key="tag", flat=True, sep="") 587 tags = f" TAGS {tags}" if tags else "" 588 589 return f"SET{serde}{exprs}{location}{file_format}{tags}" 590 591 def serdeproperties_sql(self, expression: exp.SerdeProperties) -> str: 592 prefix = "WITH " if expression.args.get("with_") else "" 593 exprs = self.expressions(expression, flat=True) 594 595 return f"{prefix}SERDEPROPERTIES ({exprs})" 596 597 def exists_sql(self, expression: exp.Exists) -> str: 598 if expression.expression: 599 return self.function_fallback_sql(expression) 600 601 return super().exists_sql(expression) 602 603 def timetostr_sql(self, expression: exp.TimeToStr) -> str: 604 this = expression.this 605 if isinstance(this, exp.TimeStrToTime): 606 this = this.this 607 608 return self.func("DATE_FORMAT", this, self.format_time(expression)) 609 610 def usingproperty_sql(self, expression: exp.UsingProperty) -> str: 611 kind = expression.args.get("kind") 612 return f"USING {kind} {self.sql(expression, 'this')}" 613 614 def fileformatproperty_sql(self, expression: exp.FileFormatProperty) -> str: 615 if isinstance(expression.this, exp.InputOutputFormat): 616 this = self.sql(expression, "this") 617 else: 618 this = expression.name.upper() 619 620 return f"STORED AS {this}"
Generator converts a given syntax tree to the corresponding SQL string.
Arguments:
- pretty: Whether 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: Always quote except for specials cases. 'safe': Only quote identifiers that are case insensitive.
- normalize: Whether to normalize identifiers to lowercase. Default: False.
- pad: The pad size in a formatted string. For example, this affects the indentation of a projection in a query, relative to its nesting level. Default: 2.
- indent: The indentation size in a formatted string. For example, this affects the
indentation of subqueries and filters under a
WHEREclause. Default: 2. - normalize_functions: How to normalize 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: Whether 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 to preserve comments in the output SQL code. Default: True
EXPRESSIONS_WITHOUT_NESTED_CTES =
{<class 'sqlglot.expressions.query.Select'>, <class 'sqlglot.expressions.query.SetOperation'>, <class 'sqlglot.expressions.dml.Insert'>, <class 'sqlglot.expressions.query.Subquery'>}
SUPPORTED_JSON_PATH_PARTS =
{<class 'sqlglot.expressions.query.JSONPathKey'>, <class 'sqlglot.expressions.query.JSONPathSubscript'>, <class 'sqlglot.expressions.query.JSONPathWildcard'>, <class 'sqlglot.expressions.query.JSONPathRoot'>}
TYPE_MAPPING =
{<DType.DATETIME2: 'DATETIME2'>: 'TIMESTAMP', <DType.NCHAR: 'NCHAR'>: 'CHAR', <DType.NVARCHAR: 'NVARCHAR'>: 'VARCHAR', <DType.MEDIUMTEXT: 'MEDIUMTEXT'>: 'TEXT', <DType.LONGTEXT: 'LONGTEXT'>: 'TEXT', <DType.TINYTEXT: 'TINYTEXT'>: 'TEXT', <DType.BLOB: 'BLOB'>: 'BINARY', <DType.MEDIUMBLOB: 'MEDIUMBLOB'>: 'BLOB', <DType.LONGBLOB: 'LONGBLOB'>: 'BLOB', <DType.TINYBLOB: 'TINYBLOB'>: 'BLOB', <DType.INET: 'INET'>: 'INET', <DType.ROWVERSION: 'ROWVERSION'>: 'BINARY', <DType.SMALLDATETIME: 'SMALLDATETIME'>: 'TIMESTAMP', <DType.BIT: 'BIT'>: 'BOOLEAN', <DType.DATETIME: 'DATETIME'>: 'TIMESTAMP', <DType.TEXT: 'TEXT'>: 'STRING', <DType.TIME: 'TIME'>: 'TIMESTAMP', <DType.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>: 'TIMESTAMP', <DType.TIMESTAMPTZ: 'TIMESTAMPTZ'>: 'TIMESTAMP', <DType.UTINYINT: 'UTINYINT'>: 'SMALLINT', <DType.VARBINARY: 'VARBINARY'>: 'BINARY'}
TRANSFORMS =
{<class 'sqlglot.expressions.query.JSONPathKey'>: <function <lambda>>, <class 'sqlglot.expressions.query.JSONPathRoot'>: <function <lambda>>, <class 'sqlglot.expressions.query.JSONPathSubscript'>: <function <lambda>>, <class 'sqlglot.expressions.query.JSONPathWildcard'>: <function <lambda>>, <class 'sqlglot.expressions.core.Adjacent'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.AllowedValuesProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.AnalyzeColumns'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.AnalyzeWith'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.array.ArrayContainedBy'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.array.ArrayContainsAll'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.array.ArrayOverlaps'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.AssumeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.AutoRefreshProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.BackupProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.BinaryColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.CaseSpecificColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.CalledOnNullInputProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.math.Ceil'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.CharacterSetColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.CharacterSetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.ClusteredColumnConstraint'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.constraints.CollateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.CommentColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.functions.ConnectByRoot'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.string.ConvertToCharset'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.CopyGrantsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.CopyTagsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.CredentialsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.functions.CurrentCatalog'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.functions.SessionUser'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.DateFormatColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.DefaultColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ApiProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ApplicationProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.CatalogProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ComputeProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.DatabaseProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.DynamicProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.EmptyProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.EncodeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.EndStatement'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.EnviromentProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.HandlerProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ParameterStyleProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.EphemeralColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.ExcludeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ExecuteAsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Except'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ExternalProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.math.Floor'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Get'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.GlobalProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.HeapProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.HybridProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.IcebergProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.IgnoreGrantsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.InheritsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.InlineLengthColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.InputModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Intersect'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.datatypes.IntervalSpan'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.functions.Int64'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONBContainsAnyTopKeys'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONBContainsAllTopKeys'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONBContainsTopKey'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONBDeleteAtPath'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONBPathExists'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONObject'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.json.JSONObjectAgg'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.LanguageProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.LocationProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.LogProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.MaskingProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.MaterializedProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.functions.NetFunc'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.NetworkProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.NonClusteredColumnConstraint'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.properties.NoPrimaryIndexProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.NotForReplicationColumnConstraint'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.properties.OnCommitProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.OnProperty'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.constraints.OnUpdateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.core.Operator'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.OutputModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.core.ExtendsLeft'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.core.ExtendsRight'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.PathColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.PartitionedByBucket'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.properties.PartitionByTruncate'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.core.PivotAny'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.array.PositionalColumn'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.ProjectionPolicyColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.InvisibleColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.ZeroFillColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Put'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.RemoteWithConnectionModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ReturnsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.RowAccessProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.core.SafeFunc'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SampleProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SecureProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SecurityIntegrationProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SetConfigProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SettingsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SharingProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SqlReadWriteProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.SqlSecurityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.StabilityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Stream'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.StreamingTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.StrictProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ddl.SwapTable'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.TableColumn'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.Tags'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.TemporaryProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.TitleColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.array.ToMap'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ToTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.TransformModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.TransientProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.VirtualProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ddl.TriggerExecute'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Union'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.UnloggedProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.UsingTemplateProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.UsingData'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.UppercaseColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.temporal.UtcDate'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.temporal.UtcTime'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.temporal.UtcTimestamp'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.query.Variadic'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.array.VarMap'>: <function var_map_sql>, <class 'sqlglot.expressions.properties.ViewAttributeProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.VolatileProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.WithJournalTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.WithProcedureOptions'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.WithSchemaBindingProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.constraints.WithOperator'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.WithoutRowidProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.ForceProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.properties.Property'>: <function property_sql>, <class 'sqlglot.expressions.aggregate.AnyValue'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.core.ApproxDistinct'>: <function approx_count_distinct_sql>, <class 'sqlglot.expressions.aggregate.ArgMax'>: <function arg_max_or_min_no_count.<locals>._arg_max_or_min_sql>, <class 'sqlglot.expressions.aggregate.ArgMin'>: <function arg_max_or_min_no_count.<locals>._arg_max_or_min_sql>, <class 'sqlglot.expressions.array.Array'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.array.ArrayConcat'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.array.ArrayToString'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.array.ArraySort'>: <function _array_sort_sql>, <class 'sqlglot.expressions.query.With'>: <function no_recursive_cte_sql>, <class 'sqlglot.expressions.temporal.DateAdd'>: <function _add_date_sql>, <class 'sqlglot.expressions.temporal.DateDiff'>: <function _date_diff_sql>, <class 'sqlglot.expressions.temporal.DateStrToDate'>: <function datestrtodate_sql>, <class 'sqlglot.expressions.temporal.DateSub'>: <function _add_date_sql>, <class 'sqlglot.expressions.temporal.DateToDi'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.temporal.DiToDate'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.properties.StorageHandlerProperty'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.string.FromBase64'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.array.GenerateSeries'>: <function sequence_sql>, <class 'sqlglot.expressions.temporal.GenerateDateArray'>: <function sequence_sql>, <class 'sqlglot.expressions.functions.If'>: <function if_sql.<locals>._if_sql>, <class 'sqlglot.expressions.core.ILike'>: <function no_ilike_sql>, <class 'sqlglot.expressions.core.IntDiv'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.math.IsNan'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.json.JSONExtract'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.json.JSONExtractScalar'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.json.JSONFormat'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.string.Left'>: <function left_to_substring_sql>, <class 'sqlglot.expressions.array.Map'>: <function var_map_sql>, <class 'sqlglot.expressions.aggregate.Max'>: <function max_or_greatest>, <class 'sqlglot.expressions.string.MD5Digest'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.aggregate.Min'>: <function min_or_least>, <class 'sqlglot.expressions.temporal.MonthsBetween'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.constraints.NotNullColumnConstraint'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.ddl.Create'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.aggregate.Quantile'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.aggregate.ApproxQuantile'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.string.RegexpExtract'>: <function regexp_extract_sql>, <class 'sqlglot.expressions.string.RegexpExtractAll'>: <function regexp_extract_sql>, <class 'sqlglot.expressions.string.RegexpReplace'>: <function regexp_replace_sql>, <class 'sqlglot.expressions.core.RegexpLike'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.string.RegexpSplit'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.string.Right'>: <function right_to_substring_sql>, <class 'sqlglot.expressions.properties.SchemaCommentProperty'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.aggregate.ArrayUniqueAgg'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.string.Split'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.query.Select'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.string.StrPosition'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.temporal.StrToDate'>: <function _str_to_date_sql>, <class 'sqlglot.expressions.temporal.StrToTime'>: <function _str_to_time_sql>, <class 'sqlglot.expressions.temporal.StrToUnix'>: <function _str_to_unix_sql>, <class 'sqlglot.expressions.array.StructExtract'>: <function struct_extract_sql>, <class 'sqlglot.expressions.array.StarMap'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.query.Table'>: <function preprocess.<locals>._to_sql>, <class 'sqlglot.expressions.temporal.TimeStrToDate'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.temporal.TimeStrToTime'>: <function timestrtotime_sql>, <class 'sqlglot.expressions.temporal.TimeStrToUnix'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.temporal.TimestampTrunc'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.temporal.TimeToUnix'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.string.ToBase64'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.temporal.TsOrDiToDi'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.temporal.TsOrDsAdd'>: <function _add_date_sql>, <class 'sqlglot.expressions.temporal.TsOrDsDiff'>: <function _date_diff_sql>, <class 'sqlglot.expressions.temporal.TsOrDsToDate'>: <function _to_date_sql>, <class 'sqlglot.expressions.functions.TryCast'>: <function no_trycast_sql>, <class 'sqlglot.expressions.string.Trim'>: <function trim_sql>, <class 'sqlglot.expressions.string.Unicode'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.temporal.UnixToStr'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.temporal.UnixToTime'>: <function _unix_to_time_sql>, <class 'sqlglot.expressions.temporal.UnixToTimeStr'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.array.Unnest'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.properties.PartitionedByProperty'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.string.NumberToStr'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.query.National'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.constraints.PrimaryKeyColumnConstraint'>: <function HiveGenerator.<lambda>>, <class 'sqlglot.expressions.temporal.WeekOfYear'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.temporal.DayOfMonth'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.temporal.DayOfWeek'>: <function rename_func.<locals>.<lambda>>, <class 'sqlglot.expressions.string.Levenshtein'>: <function rename_func.<locals>.<lambda>>}
PROPERTIES_LOCATION =
{<class 'sqlglot.expressions.properties.AggregateKeyProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.AllowedValuesProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.AlgorithmProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.ApiProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.ApplicationProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.AutoIncrementProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.AutoRefreshProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.BackupProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.BlockCompressionProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.CalledOnNullInputProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.CatalogProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.CharacterSetProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ChecksumProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.CollateProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ComputeProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.CopyGrantsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.CopyTagsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.query.Cluster'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ClusteredByProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ClusterProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DistributedByProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DuplicateKeyProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DataBlocksizeProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.DatabaseProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.DataDeletionProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DefinerProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.DictRange'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DictProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DynamicProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.DistKeyProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.DistStyleProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.EmptyProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.EncodeProperty'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.properties.EngineProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.EnviromentProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.HandlerProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ParameterStyleProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ExecuteAsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ExternalProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.FallbackProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.FileFormatProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.FreespaceProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.GlobalProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.HeapProperty'>: <PropertiesLocation.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.properties.HybridProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.InheritsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.IcebergProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.IgnoreGrantsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.IncludeProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.InputModelProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.InteractiveProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.IsolatedLoadingProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.JournalProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.LanguageProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.LikeProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.LocationProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.LockProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.LockingProperty'>: <PropertiesLocation.POST_ALIAS: 'POST_ALIAS'>, <class 'sqlglot.expressions.properties.LogProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.MaskingProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.MaterializedProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.MergeBlockRatioProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.ModuleProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.NetworkProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.NoPrimaryIndexProperty'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.properties.OnProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.OnCommitProperty'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.properties.OptionsProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.query.Order'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.OutputModelProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.PartitionedByProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.PartitionedOfProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.PhysicalProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.constraints.PrimaryKey'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.Property'>: <PropertiesLocation.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.properties.RefreshTriggerProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.RemoteWithConnectionModelProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ReturnsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.RollupProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.RowAccessProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.RowFormatProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.RowFormatDelimitedProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.RowFormatSerdeProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SampleProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SchemaCommentProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SecureProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.SecurityIntegrationProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.SerdeProperties'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ddl.Set'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SettingsProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SetProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.SetConfigProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SharingProperty'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.ddl.SequenceProperties'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.ddl.TriggerProperties'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.properties.SortKeyProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.SqlReadWriteProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.SqlSecurityProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.StabilityProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.StorageHandlerProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.StorageProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.StreamingTableProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.StrictProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.Tags'>: <PropertiesLocation.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.properties.TableSpaceProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.TemporaryProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.ToTableProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.TransientProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.TransformModelProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ddl.MergeTreeTTL'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.UnloggedProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.UsingProperty'>: <PropertiesLocation.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.properties.UsingTemplateProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.ViewAttributeProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.VirtualProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.properties.VolatileProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.WithDataProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.WithJournalTableProperty'>: <PropertiesLocation.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.properties.WithProcedureOptions'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.WithSchemaBindingProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.WithSystemVersioningProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.WithoutRowidProperty'>: <PropertiesLocation.UNSUPPORTED: 'UNSUPPORTED'>, <class 'sqlglot.expressions.properties.ForceProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>}
TS_OR_DS_EXPRESSIONS: ClassVar =
(<class 'sqlglot.expressions.temporal.DateDiff'>, <class 'sqlglot.expressions.temporal.Day'>, <class 'sqlglot.expressions.temporal.Month'>, <class 'sqlglot.expressions.temporal.Year'>)
IGNORE_NULLS_FUNCS: ClassVar =
(<class 'sqlglot.expressions.aggregate.First'>, <class 'sqlglot.expressions.aggregate.Last'>, <class 'sqlglot.expressions.aggregate.FirstValue'>, <class 'sqlglot.expressions.aggregate.LastValue'>)
def
format_time( self, expression: sqlglot.expressions.core.Expr, inverse_time_mapping: dict[str, str] | None = None, inverse_time_trie: dict | None = None) -> str | None:
417 def format_time( 418 self, 419 expression: exp.Expr, 420 inverse_time_mapping: dict[str, str] | None = None, 421 inverse_time_trie: dict | None = None, 422 ) -> str | None: 423 # Inferred property because this method is reused by other dialects under Hive 424 is_dialect_strict = self.dialect.TIME_MAPPING.get("MM") == "%mstrict" 425 426 if ( 427 is_dialect_strict 428 and inverse_time_mapping is None 429 and isinstance(expression, PARSE_TIME_EXPRESSIONS) 430 ): 431 # Render a lenient %m/%d non-padded (M/d) so single-digit sources stay parseable 432 return format_time( 433 _lenient_parse_format(self.sql(expression, "format")), 434 self.dialect.INVERSE_TIME_MAPPING, 435 self.dialect.INVERSE_TIME_TRIE, 436 ) 437 438 return super().format_time(expression, inverse_time_mapping, inverse_time_trie)
457 def parameter_sql(self, expression: exp.Parameter) -> str: 458 this = self.sql(expression, "this") 459 expression_sql = self.sql(expression, "expression") 460 461 parent = expression.parent 462 this = f"{this}:{expression_sql}" if expression_sql else this 463 464 if isinstance(parent, exp.EQ) and isinstance(parent.parent, exp.SetItem): 465 # We need to produce SET key = value instead of SET ${key} = value 466 return this 467 468 return f"${{{this}}}"
def
rowformatserdeproperty_sql( self, expression: sqlglot.expressions.properties.RowFormatSerdeProperty) -> str:
@unsupported_args('decimals')
def
trunc_sql(self, expression: sqlglot.expressions.math.Trunc) -> str:
503 def datatype_sql(self, expression: exp.DataType) -> str: 504 if expression.this in self.PARAMETERIZABLE_TEXT_TYPES and ( 505 not expression.expressions or expression.expressions[0].name == "MAX" 506 ): 507 expression.set("this", exp.DType.TEXT) 508 expression.set("expressions", None) 509 elif expression.is_type(exp.DType.TEXT) and expression.expressions: 510 expression.set("this", exp.DType.VARCHAR) 511 elif ( 512 not self.SUPPORTS_TEMPORAL_PRECISION and expression.this in exp.DataType.TEMPORAL_TYPES 513 ): 514 expression.set("expressions", None) 515 elif expression.is_type("float"): 516 size_expression = expression.find(exp.DataTypeParam) 517 if size_expression: 518 size = int(size_expression.name) 519 expression.set("this", exp.DType.FLOAT if size <= 32 else exp.DType.DOUBLE) 520 expression.set("expressions", None) 521 return super().datatype_sql(expression)
527 def struct_sql(self, expression: exp.Struct) -> str: 528 values = [] 529 530 for i, e in enumerate(expression.expressions): 531 if isinstance(e, exp.PropertyEQ): 532 self.unsupported("Hive does not support named structs.") 533 values.append(e.expression) 534 else: 535 values.append(e) 536 537 return self.func("STRUCT", *values)
550 def altercolumn_sql(self, expression: exp.AlterColumn) -> str: 551 if expression.args.get("exists"): 552 self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect") 553 554 this = self.sql(expression, "this") 555 new_name = self.sql(expression, "rename_to") or this 556 dtype = self.sql(expression, "dtype") 557 comment = ( 558 f" COMMENT {self.sql(expression, 'comment')}" if self.sql(expression, "comment") else "" 559 ) 560 default = self.sql(expression, "default") 561 visible = expression.args.get("visible") 562 allow_null = expression.args.get("allow_null") 563 drop = expression.args.get("drop") 564 565 if any([default, drop, visible]) or allow_null is not None: 566 self.unsupported("Unsupported CHANGE COLUMN syntax") 567 568 if not dtype: 569 self.unsupported("CHANGE COLUMN without a type is not supported") 570 571 return f"CHANGE COLUMN {this} {new_name} {dtype}{comment}"
577 def alterset_sql(self, expression: exp.AlterSet) -> str: 578 exprs = self.expressions(expression, flat=True) 579 exprs = f" {exprs}" if exprs else "" 580 location = self.sql(expression, "location") 581 location = f" LOCATION {location}" if location else "" 582 file_format = self.expressions(expression, key="file_format", flat=True, sep=" ") 583 file_format = f" FILEFORMAT {file_format}" if file_format else "" 584 serde = self.sql(expression, "serde") 585 serde = f" SERDE {serde}" if serde else "" 586 tags = self.expressions(expression, key="tag", flat=True, sep="") 587 tags = f" TAGS {tags}" if tags else "" 588 589 return f"SET{serde}{exprs}{location}{file_format}{tags}"
def
fileformatproperty_sql( self, expression: sqlglot.expressions.properties.FileFormatProperty) -> str:
Inherited Members
- sqlglot.generator.Generator
- Generator
- NULL_ORDERING_SUPPORTED
- WINDOW_FUNCS_WITH_NULL_ORDERING
- IGNORE_NULLS_IN_FUNC
- IGNORE_NULLS_BEFORE_ORDER
- LOCKING_READS_SUPPORTED
- EXCEPT_INTERSECT_SUPPORT_ALL_CLAUSE
- WRAP_DERIVED_VALUES
- CREATE_FUNCTION_RETURN_AS
- MATCHED_BY_SOURCE
- SUPPORTS_MERGE_WHERE
- MERGE_ALL_BY_NAME
- SINGLE_STRING_INTERVAL
- INTERVAL_ALLOWS_PLURAL_FORM
- AUTO_REFRESH_BARE_INTERVALS
- LIMIT_ONLY_LITERALS
- RENAME_TABLE_WITH_DB
- GROUPINGS_SEP
- INOUT_SEPARATOR
- DIRECTED_JOINS
- SUPPORTS_NEAREST_JOIN
- QUERY_HINT_SEP
- IS_BOOL_ALLOWED
- CAST_TO_INT_ROUNDS
- SAFE_INT_DIVISION
- DUPLICATE_KEY_UPDATE_WITH_SET
- LIMIT_IS_TOP
- RETURNING_END
- TZ_TO_WITH_TIME_ZONE
- VALUES_AS_TABLE
- ALTER_TABLE_INCLUDE_COLUMN_KEYWORD
- ALTER_TABLE_ADD_COLUMNS_WRAPPED
- UNNEST_WITH_ORDINALITY
- SEMI_ANTI_JOIN_WITH_SIDE
- COMPUTED_COLUMN_WITH_TYPE
- SUPPORTS_TABLE_COPY
- TABLESAMPLE_REQUIRES_PARENS
- TABLESAMPLE_SIZE_IS_ROWS
- TABLESAMPLE_KEYWORDS
- TABLESAMPLE_SEED_KEYWORD
- HISTORICAL_DATA_POST_ALIAS
- COLLATE_IS_FUNC
- DATA_TYPE_SPECIFIERS_ALLOWED
- ENSURE_BOOLS
- CTE_RECURSIVE_KEYWORD_REQUIRED
- SUPPORTS_SINGLE_ARG_CONCAT
- SUPPORTS_TABLE_ALIAS_COLUMNS
- SUPPORTS_NAMED_CTE_COLUMNS
- UNPIVOT_ALIASES_ARE_IDENTIFIERS
- PIVOT_ALIAS_WITH_AS
- JSON_KEY_VALUE_PAIR_SEP
- INSERT_OVERWRITE
- SUPPORTS_SELECT_INTO
- SUPPORTS_UNLOGGED_TABLES
- SUPPORTS_CREATE_TABLE_LIKE
- SUPPORTS_MODIFY_COLUMN
- SUPPORTS_CHANGE_COLUMN
- SUPPORTS_ALTER_COLUMN_NULLABILITY
- SUPPORTS_ALTER_COLUMN_IF_EXISTS
- LIKE_PROPERTY_INSIDE_SCHEMA
- MULTI_ARG_DISTINCT
- JSON_TYPE_REQUIRED_FOR_EXTRACTION
- JSON_PATH_BRACKETED_KEY_SUPPORTED
- JSON_PATH_KEY_QUOTED_FORCES_BRACKETS
- CAN_IMPLEMENT_ARRAY_ANY
- SUPPORTS_WINDOW_EXCLUDE
- SET_OP_MODIFIERS
- SET_OP_LIMITS
- SET_OP_PARENTHESIZED_OPERANDS
- COPY_PARAMS_ARE_WRAPPED
- COPY_PARAMS_EQ_REQUIRED
- COPY_HAS_INTO_KEYWORD
- UNICODE_SUBSTITUTE
- STAR_EXCEPT
- HEX_FUNC
- QUOTE_JSON_PATH
- SUPPORTS_EXPLODING_PROJECTIONS
- ARRAY_CONCAT_IS_VAR_LEN
- SUPPORTS_CONVERT_TIMEZONE
- SUPPORTS_UNIX_SECONDS
- ALTER_SET_WRAPPED
- NORMALIZE_EXTRACT_DATE_PARTS
- ARRAY_SIZE_DIM_REQUIRED
- SUPPORTS_BETWEEN_FLAGS
- SUPPORTS_LIKE_QUANTIFIERS
- MATCH_AGAINST_TABLE_PREFIX
- SET_ASSIGNMENT_REQUIRES_VARIABLE_KEYWORD
- DECLARE_DEFAULT_ASSIGNMENT
- UPDATE_STATEMENT_SUPPORTS_FROM
- STAR_EXCLUDE_REQUIRES_DERIVED_TABLE
- SUPPORTS_DROP_ALTER_ICEBERG_PROPERTY
- UNSUPPORTED_TYPES
- TYPE_PARAM_SETTINGS
- TIME_PART_SINGULARS
- AFTER_HAVING_MODIFIER_TRANSFORMS
- TOKEN_MAPPING
- STRUCT_DELIMITER
- PARAMETER_TOKEN
- NAMED_PLACEHOLDER_TOKEN
- EXPRESSION_PRECEDES_PROPERTIES_CREATABLES
- RESERVED_KEYWORDS
- WITH_SEPARATED_COMMENTS
- EXCLUDE_COMMENTS
- UNWRAPPED_INTERVAL_VALUES
- PARAMETERIZABLE_TEXT_TYPES
- RESPECT_IGNORE_NULLS_UNSUPPORTED_EXPRESSIONS
- MOD_OPERATOR
- MOD_PAREN_PARENT_TYPES
- ALTER_PARTITION_OPTIONS
- SENTINEL_LINE_BREAK
- pretty
- identify
- normalize
- pad
- unsupported_level
- max_unsupported
- leading_comma
- max_text_width
- comments
- dialect
- normalize_functions
- unsupported_messages
- generate
- preprocess
- unsupported
- sep
- seg
- sanitize_comment
- maybe_comment
- wrap
- no_identify
- normalize_func
- indent
- sql
- uncache_sql
- cache_sql
- characterset_sql
- column_parts
- column_sql
- pseudocolumn_sql
- columnposition_sql
- columnconstraint_sql
- computedcolumnconstraint_sql
- autoincrementcolumnconstraint_sql
- compresscolumnconstraint_sql
- generatedasidentitycolumnconstraint_sql
- generatedasrowcolumnconstraint_sql
- periodforsystemtimeconstraint_sql
- notnullcolumnconstraint_sql
- primarykeycolumnconstraint_sql
- uniquecolumnconstraint_sql
- inoutcolumnconstraint_sql
- createable_sql
- create_sql
- sequenceproperties_sql
- triggerproperties_sql
- triggerreferencing_sql
- triggerevent_sql
- clone_sql
- describe_sql
- heredoc_sql
- prepend_ctes
- with_sql
- cte_sql
- tablealias_sql
- bitstring_sql
- hexstring_sql
- bytestring_sql
- unicodestring_sql
- rawstring_sql
- datatypeparam_sql
- datatype_param_bound_limiter
- directory_sql
- delete_sql
- drop_sql
- set_operation
- set_operations
- fetch_sql
- limitoptions_sql
- filter_sql
- per_sql
- hint_sql
- indexparameters_sql
- index_sql
- dynamicidentifier_sql
- identifier_sql
- hex_sql
- lowerhex_sql
- inputoutputformat_sql
- national_sql
- partition_sql
- properties_sql
- root_properties
- properties
- with_properties
- locate_properties
- property_name
- property_sql
- uuidproperty_sql
- likeproperty_sql
- fallbackproperty_sql
- journalproperty_sql
- freespaceproperty_sql
- checksumproperty_sql
- mergeblockratioproperty_sql
- moduleproperty_sql
- datablocksizeproperty_sql
- blockcompressionproperty_sql
- isolatedloadingproperty_sql
- partitionboundspec_sql
- partitionedofproperty_sql
- lockingproperty_sql
- lockingstatement_sql
- withdataproperty_sql
- withsystemversioningproperty_sql
- insert_sql
- introducer_sql
- kill_sql
- pseudotype_sql
- objectidentifier_sql
- onconflict_sql
- returning_sql
- rowformatdelimitedproperty_sql
- withtablehint_sql
- indextablehint_sql
- historicaldata_sql
- table_parts
- table_sql
- tablefromrows_sql
- tablesample_sql
- pivot_sql
- tuple_sql
- update_sql
- values_sql
- var_sql
- into_sql
- from_sql
- groupingsets_sql
- rollup_sql
- rollupindex_sql
- rollupproperty_sql
- cube_sql
- group_sql
- having_sql
- connectto_sql
- disconnect_sql
- connect_sql
- prior_sql
- join_sql
- nearest_sql
- lambda_sql
- lateral_op
- lateral_sql
- limit_sql
- offset_sql
- setitem_sql
- set_sql
- queryband_sql
- pragma_sql
- lock_sql
- literal_sql
- escape_str
- loaddata_sql
- null_sql
- boolean_sql
- booland_sql
- boolor_sql
- order_sql
- withfill_sql
- cluster_sql
- clusterproperty_sql
- distribute_sql
- sort_sql
- ordered_sql
- matchrecognizemeasure_sql
- matchrecognize_sql
- query_modifiers
- options_modifier
- forclause_sql
- queryoption_sql
- offset_limit_modifiers
- after_limit_modifiers
- select_sql
- schema_columns_sql
- star_sql
- sessionparameter_sql
- placeholder_sql
- subquery_sql
- qualify_sql
- prewhere_sql
- where_sql
- window_sql
- partition_by_sql
- partitionbyexcluding_sql
- windowspec_sql
- withingroup_sql
- between_sql
- bracket_offset_expressions
- bracket_sql
- all_sql
- any_sql
- case_sql
- nextvaluefor_sql
- extract_sql
- trim_sql
- convert_concat_args
- concat_sql
- concatws_sql
- check_sql
- foreignkey_sql
- primarykey_sql
- timeserieskey_sql
- if_sql
- matchagainst_sql
- jsonkeyvalue_sql
- jsonpath_sql
- json_path_part
- formatjson_sql
- formatphrase_sql
- teradatatitle_sql
- jsonarray_sql
- jsonarrayagg_sql
- jsoncolumndef_sql
- jsonschema_sql
- jsontable_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
- pivotalias_sql
- aliases_sql
- atindex_sql
- attimezone_sql
- fromtimezone_sql
- fromiso8601date_sql
- fromiso8601timestamp_sql
- fromiso8601timestampnanos_sql
- add_sql
- and_sql
- or_sql
- xor_sql
- connector_sql
- bitwiseand_sql
- bitwiseleftshift_sql
- bitwisenot_sql
- bitwiseor_sql
- bitwiserightshift_sql
- bitwisexor_sql
- cast_sql
- strtotime_sql
- strtodate_sql
- parsedatetime_sql
- currentdate_sql
- collate_sql
- command_sql
- comment_sql
- mergetreettlaction_sql
- mergetreettl_sql
- transaction_sql
- commit_sql
- rollback_sql
- modifycolumn_sql
- alterindex_sql
- alterdiststyle_sql
- altersortkey_sql
- alterrename_sql
- alter_sql
- altersession_sql
- add_column_sql
- droppartition_sql
- dropprimarykey_sql
- addconstraint_sql
- replacecolumns_sql
- addpartition_sql
- distinct_sql
- respectnulls_sql
- havingmax_sql
- intdiv_sql
- dpipe_sql
- nullif_zero_divisor
- div_sql
- safedivide_sql
- overlaps_sql
- distance_sql
- distancend_sql
- dot_sql
- eq_sql
- propertyeq_sql
- escape_sql
- glob_sql
- gt_sql
- gte_sql
- is_sql
- like_sql
- ilike_sql
- match_sql
- similarto_sql
- lt_sql
- lte_sql
- mod_sql
- mul_sql
- neq_sql
- nullsafeeq_sql
- nullsafeneq_sql
- sub_sql
- trycast_sql
- jsoncast_sql
- try_sql
- log_sql
- use_sql
- binary
- ceil_floor
- function_fallback_sql
- function_name
- func
- format_args
- too_wide
- expressions
- op_expressions
- naked_property
- tag_sql
- token_sql
- userdefinedfunction_sql
- macrooverloads_sql
- macrooverload_sql
- joinhint_sql
- kwarg_sql
- when_sql
- whens_sql
- merge_sql
- tochar_sql
- tonumber_sql
- dictproperty_sql
- dictrange_sql
- dictsubproperty_sql
- aggregatekeyproperty_sql
- duplicatekeyproperty_sql
- uniquekeyproperty_sql
- distributedbyproperty_sql
- oncluster_sql
- clusteredbyproperty_sql
- anyvalue_sql
- querytransform_sql
- indexconstraintoption_sql
- checkcolumnconstraint_sql
- indexcolumnconstraint_sql
- nvl2_sql
- nthvalue_sql
- comprehension_sql
- columnprefix_sql
- opclass_sql
- predict_sql
- generateembedding_sql
- generatetext_sql
- generatetable_sql
- generatebool_sql
- generateint_sql
- generatedouble_sql
- mltranslate_sql
- mlforecast_sql
- aiforecast_sql
- featuresattime_sql
- vectorsearch_sql
- forin_sql
- refresh_sql
- toarray_sql
- tsordstotime_sql
- tsordstotimestamp_sql
- tsordstodatetime_sql
- tsordstodate_sql
- unixdate_sql
- lastday_sql
- dateadd_sql
- arrayinsert_sql
- arrayany_sql
- partitionrange_sql
- truncatetable_sql
- convert_sql
- copyparameter_sql
- credentials_sql
- copy_sql
- semicolon_sql
- datadeletionproperty_sql
- maskingpolicycolumnconstraint_sql
- gapfill_sql
- scope_resolution
- scoperesolution_sql
- parsejson_sql
- rand_sql
- changes_sql
- pad_sql
- summarize_sql
- explodinggenerateseries_sql
- converttimezone_sql
- json_sql
- jsonvalue_sql
- skipjsoncolumn_sql
- conditionalinsert_sql
- multitableinserts_sql
- oncondition_sql
- jsonextractquote_sql
- jsonexists_sql
- min_sql
- max_sql
- timeslice_sql
- slice_sql
- apply_sql
- grant_sql
- revoke_sql
- grantprivilege_sql
- grantprincipal_sql
- columns_sql
- overlay_sql
- todouble_sql
- string_sql
- median_sql
- overflowtruncatebehavior_sql
- unixseconds_sql
- arraysort_sql
- arraysize_sql
- attach_sql
- detach_sql
- attachoption_sql
- watermarkcolumnconstraint_sql
- encodeproperty_sql
- includeproperty_sql
- xmlelement_sql
- xmlkeyvalueoption_sql
- partitionbyrangeproperty_sql
- partitionbyrangepropertydynamic_sql
- unpivotcolumns_sql
- analyzesample_sql
- analyzestatistics_sql
- analyzehistogram_sql
- analyzedelete_sql
- analyzelistchainedrows_sql
- analyzevalidate_sql
- analyze_sql
- xmltable_sql
- xmlnamespace_sql
- export_sql
- declare_sql
- declareitem_sql
- recursivewithsearch_sql
- parameterizedagg_sql
- anonymousaggfunc_sql
- combinedaggfunc_sql
- combinedparameterizedagg_sql
- show_sql
- install_sql
- liststage_sql
- get_put_sql
- translatecharacters_sql
- decodecase_sql
- semanticview_sql
- getextract_sql
- datefromunixdate_sql
- space_sql
- buildproperty_sql
- refreshtriggerproperty_sql
- modelattribute_sql
- directorystage_sql
- uuid_sql
- initcap_sql
- localtime_sql
- localtimestamp_sql
- weekstart_name
- weekstart_sql
- chr_sql
- weightstring_sql
- block_sql
- functionspecification_sql
- storedprocedure_sql
- ifblock_sql
- casestatement_sql
- whileblock_sql
- loopblock_sql
- repeatblock_sql
- leave_sql
- iterate_sql
- execute_sql
- executesql_sql
- altermodifysqlsecurity_sql
- renameindex_sql