Edit on GitHub

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    ARRAY_SIZE_NAME = "SIZE"
250    ALTER_SET_TYPE = ""
251
252    EXPRESSIONS_WITHOUT_NESTED_CTES = {
253        exp.Insert,
254        exp.Select,
255        exp.Subquery,
256        exp.SetOperation,
257    }
258
259    SUPPORTED_JSON_PATH_PARTS = {
260        exp.JSONPathKey,
261        exp.JSONPathRoot,
262        exp.JSONPathSubscript,
263        exp.JSONPathWildcard,
264    }
265
266    TYPE_MAPPING = {
267        **generator.Generator.TYPE_MAPPING,
268        exp.DType.BIT: "BOOLEAN",
269        exp.DType.BLOB: "BINARY",
270        exp.DType.DATETIME: "TIMESTAMP",
271        exp.DType.ROWVERSION: "BINARY",
272        exp.DType.TEXT: "STRING",
273        exp.DType.TIME: "TIMESTAMP",
274        exp.DType.TIMESTAMPNTZ: "TIMESTAMP",
275        exp.DType.TIMESTAMPTZ: "TIMESTAMP",
276        exp.DType.UTINYINT: "SMALLINT",
277        exp.DType.VARBINARY: "BINARY",
278    }
279
280    TRANSFORMS = {
281        **generator.Generator.TRANSFORMS,
282        exp.Property: property_sql,
283        exp.AnyValue: rename_func("FIRST"),
284        exp.ApproxDistinct: approx_count_distinct_sql,
285        exp.ArgMax: arg_max_or_min_no_count("MAX_BY"),
286        exp.ArgMin: arg_max_or_min_no_count("MIN_BY"),
287        exp.Array: transforms.preprocess([transforms.inherit_struct_field_names]),
288        exp.ArrayConcat: rename_func("CONCAT"),
289        exp.ArrayToString: lambda self, e: self.func("CONCAT_WS", e.expression, e.this),
290        exp.ArraySort: _array_sort_sql,
291        exp.With: no_recursive_cte_sql,
292        exp.DateAdd: _add_date_sql,
293        exp.DateDiff: _date_diff_sql,
294        exp.DateStrToDate: datestrtodate_sql,
295        exp.DateSub: _add_date_sql,
296        exp.DateToDi: lambda self, e: (
297            f"CAST(DATE_FORMAT({self.sql(e, 'this')}, {HIVE_DATEINT_FORMAT}) AS INT)"
298        ),
299        exp.DiToDate: lambda self, e: (
300            f"TO_DATE(CAST({self.sql(e, 'this')} AS STRING), {HIVE_DATEINT_FORMAT})"
301        ),
302        exp.StorageHandlerProperty: lambda self, e: f"STORED BY {self.sql(e, 'this')}",
303        exp.FromBase64: rename_func("UNBASE64"),
304        exp.GenerateSeries: sequence_sql,
305        exp.GenerateDateArray: sequence_sql,
306        exp.If: if_sql(),
307        exp.ILike: no_ilike_sql,
308        exp.IntDiv: lambda self, e: self.binary(e, "DIV"),
309        exp.IsNan: rename_func("ISNAN"),
310        exp.JSONExtract: lambda self, e: self.func("GET_JSON_OBJECT", e.this, e.expression),
311        exp.JSONExtractScalar: lambda self, e: self.func("GET_JSON_OBJECT", e.this, e.expression),
312        exp.JSONFormat: rename_func("TO_JSON"),
313        exp.Left: left_to_substring_sql,
314        exp.Map: var_map_sql,
315        exp.Max: max_or_greatest,
316        exp.MD5Digest: lambda self, e: self.func("UNHEX", self.func("MD5", e.this)),
317        exp.Min: min_or_least,
318        exp.MonthsBetween: lambda self, e: self.func("MONTHS_BETWEEN", e.this, e.expression),
319        exp.NotNullColumnConstraint: lambda _, e: "" if e.args.get("allow_null") else "NOT NULL",
320        exp.VarMap: var_map_sql,
321        exp.Create: preprocess(
322            [
323                remove_unique_constraints,
324                ctas_with_tmp_tables_to_create_tmp_view,
325                move_schema_columns_to_partitioned_by,
326            ]
327        ),
328        exp.Quantile: rename_func("PERCENTILE"),
329        exp.ApproxQuantile: rename_func("PERCENTILE_APPROX"),
330        exp.RegexpExtract: regexp_extract_sql,
331        exp.RegexpExtractAll: regexp_extract_sql,
332        exp.RegexpReplace: regexp_replace_sql,
333        exp.RegexpLike: lambda self, e: self.binary(e, "RLIKE"),
334        exp.RegexpSplit: rename_func("SPLIT"),
335        exp.Right: right_to_substring_sql,
336        exp.SchemaCommentProperty: lambda self, e: self.naked_property(e),
337        exp.ArrayUniqueAgg: rename_func("COLLECT_SET"),
338        exp.Split: lambda self, e: self.func(
339            "SPLIT", e.this, self.func("CONCAT", "'\\\\Q'", e.expression, "'\\\\E'")
340        ),
341        exp.Select: transforms.preprocess(
342            [
343                transforms.eliminate_qualify,
344                transforms.eliminate_distinct_on,
345                partial(transforms.unnest_to_explode, unnest_using_arrays_zip=False),
346                transforms.any_to_exists,
347            ]
348        ),
349        exp.StrPosition: lambda self, e: strposition_sql(
350            self, e, func_name="LOCATE", supports_position=True
351        ),
352        exp.StrToDate: _str_to_date_sql,
353        exp.StrToTime: _str_to_time_sql,
354        exp.StrToUnix: _str_to_unix_sql,
355        exp.StructExtract: struct_extract_sql,
356        exp.StarMap: rename_func("MAP"),
357        exp.Table: transforms.preprocess([transforms.unnest_generate_series]),
358        exp.TimeStrToDate: rename_func("TO_DATE"),
359        exp.TimeStrToTime: timestrtotime_sql,
360        exp.TimeStrToUnix: rename_func("UNIX_TIMESTAMP"),
361        exp.TimestampTrunc: lambda self, e: self.func(
362            "TRUNC", e.this, weekstart_unit_to_str(self, e)
363        ),
364        exp.TimeToUnix: rename_func("UNIX_TIMESTAMP"),
365        exp.ToBase64: rename_func("BASE64"),
366        exp.TsOrDiToDi: lambda self, e: (
367            f"CAST(SUBSTR(REPLACE(CAST({self.sql(e, 'this')} AS STRING), '-', ''), 1, 8) AS INT)"
368        ),
369        exp.TsOrDsAdd: _add_date_sql,
370        exp.TsOrDsDiff: _date_diff_sql,
371        exp.TsOrDsToDate: _to_date_sql,
372        exp.TryCast: no_trycast_sql,
373        exp.Trim: trim_sql,
374        exp.Unicode: rename_func("ASCII"),
375        exp.UnixToStr: lambda self, e: self.func(
376            "FROM_UNIXTIME", e.this, time_format("hive")(self, e)
377        ),
378        exp.UnixToTime: _unix_to_time_sql,
379        exp.UnixToTimeStr: rename_func("FROM_UNIXTIME"),
380        exp.Unnest: rename_func("EXPLODE"),
381        exp.PartitionedByProperty: lambda self, e: f"PARTITIONED BY {self.sql(e, 'this')}",
382        exp.NumberToStr: rename_func("FORMAT_NUMBER"),
383        exp.National: lambda self, e: self.national_sql(e, prefix=""),
384        exp.ClusteredColumnConstraint: lambda self, e: (
385            f"({self.expressions(e, 'this', indent=False)})"
386        ),
387        exp.NonClusteredColumnConstraint: lambda self, e: (
388            f"({self.expressions(e, 'this', indent=False)})"
389        ),
390        exp.NotForReplicationColumnConstraint: lambda *_: "",
391        exp.OnProperty: lambda *_: "",
392        exp.PartitionedByBucket: lambda self, e: self.func("BUCKET", e.expression, e.this),
393        exp.PartitionByTruncate: lambda self, e: self.func("TRUNCATE", e.expression, e.this),
394        exp.PrimaryKeyColumnConstraint: lambda *_: "PRIMARY KEY",
395        exp.WeekOfYear: rename_func("WEEKOFYEAR"),
396        exp.DayOfMonth: rename_func("DAYOFMONTH"),
397        exp.DayOfWeek: rename_func("DAYOFWEEK"),
398        exp.Levenshtein: unsupported_args("ins_cost", "del_cost", "sub_cost", "max_dist")(
399            rename_func("LEVENSHTEIN")
400        ),
401    }
402
403    PROPERTIES_LOCATION = {
404        **generator.Generator.PROPERTIES_LOCATION,
405        exp.FileFormatProperty: exp.Properties.Location.POST_SCHEMA,
406        exp.PartitionedByProperty: exp.Properties.Location.POST_SCHEMA,
407        exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
408        exp.WithDataProperty: exp.Properties.Location.UNSUPPORTED,
409    }
410
411    TS_OR_DS_EXPRESSIONS: t.ClassVar = HIVE_TS_OR_DS_EXPRESSIONS
412
413    IGNORE_NULLS_FUNCS: t.ClassVar = (exp.First, exp.Last, exp.FirstValue, exp.LastValue)
414
415    def format_time(
416        self,
417        expression: exp.Expr,
418        inverse_time_mapping: dict[str, str] | None = None,
419        inverse_time_trie: dict | None = None,
420    ) -> str | None:
421        # Inferred property because this method is reused by other dialects under Hive
422        is_dialect_strict = self.dialect.TIME_MAPPING.get("MM") == "%mstrict"
423
424        if (
425            is_dialect_strict
426            and inverse_time_mapping is None
427            and isinstance(expression, PARSE_TIME_EXPRESSIONS)
428        ):
429            # Render a lenient %m/%d non-padded (M/d) so single-digit sources stay parseable
430            return format_time(
431                _lenient_parse_format(self.sql(expression, "format")),
432                self.dialect.INVERSE_TIME_MAPPING,
433                self.dialect.INVERSE_TIME_TRIE,
434            )
435
436        return super().format_time(expression, inverse_time_mapping, inverse_time_trie)
437
438    def ignorenulls_sql(self, expression: exp.IgnoreNulls) -> str:
439        this = expression.this
440        if isinstance(this, self.IGNORE_NULLS_FUNCS):
441            return self.func(this.sql_name(), this.this, exp.true())
442
443        return super().ignorenulls_sql(expression)
444
445    def unnest_sql(self, expression: exp.Unnest) -> str:
446        return rename_func("EXPLODE")(self, expression)
447
448    def _jsonpathkey_sql(self, expression: exp.JSONPathKey) -> str:
449        if isinstance(expression.this, exp.JSONPathWildcard):
450            self.unsupported("Unsupported wildcard in JSONPathKey expression")
451            return ""
452
453        return super()._jsonpathkey_sql(expression)
454
455    def parameter_sql(self, expression: exp.Parameter) -> str:
456        this = self.sql(expression, "this")
457        expression_sql = self.sql(expression, "expression")
458
459        parent = expression.parent
460        this = f"{this}:{expression_sql}" if expression_sql else this
461
462        if isinstance(parent, exp.EQ) and isinstance(parent.parent, exp.SetItem):
463            # We need to produce SET key = value instead of SET ${key} = value
464            return this
465
466        return f"${{{this}}}"
467
468    def schema_sql(self, expression: exp.Schema) -> str:
469        for ordered in expression.find_all(exp.Ordered):
470            if ordered.args.get("desc") is False:
471                ordered.set("desc", None)
472
473        return super().schema_sql(expression)
474
475    def constraint_sql(self, expression: exp.Constraint) -> str:
476        for prop in list(expression.find_all(exp.Properties)):
477            prop.pop()
478
479        this = self.sql(expression, "this")
480        expressions = self.expressions(expression, sep=" ", flat=True)
481        return f"CONSTRAINT {this} {expressions}"
482
483    def rowformatserdeproperty_sql(self, expression: exp.RowFormatSerdeProperty) -> str:
484        serde_props = self.sql(expression, "serde_properties")
485        serde_props = f" {serde_props}" if serde_props else ""
486        return f"ROW FORMAT SERDE {self.sql(expression, 'this')}{serde_props}"
487
488    def arrayagg_sql(self, expression: exp.ArrayAgg) -> str:
489        return self.func(
490            "COLLECT_LIST",
491            expression.this.this if isinstance(expression.this, exp.Order) else expression.this,
492        )
493
494    # Hive/Spark lack native numeric TRUNC. CAST to BIGINT truncates toward zero (not rounds).
495    # Potential enhancement: a TRUNC_TEMPLATE using FLOOR/CEIL with scale (Spark 3.3+)
496    # could preserve decimals: CASE WHEN x >= 0 THEN FLOOR(x, d) ELSE CEIL(x, d) END
497    @unsupported_args("decimals")
498    def trunc_sql(self, expression: exp.Trunc) -> str:
499        return self.sql(exp.cast(expression.this, exp.DType.BIGINT))
500
501    def datatype_sql(self, expression: exp.DataType) -> str:
502        if expression.this in self.PARAMETERIZABLE_TEXT_TYPES and (
503            not expression.expressions or expression.expressions[0].name == "MAX"
504        ):
505            expression.set("this", exp.DType.TEXT)
506            expression.set("expressions", None)
507        elif expression.is_type(exp.DType.TEXT) and expression.expressions:
508            expression.set("this", exp.DType.VARCHAR)
509        elif expression.this in exp.DataType.TEMPORAL_TYPES:
510            expression.set("expressions", None)
511        elif expression.is_type("float"):
512            size_expression = expression.find(exp.DataTypeParam)
513            if size_expression:
514                size = int(size_expression.name)
515                expression.set("this", exp.DType.FLOAT if size <= 32 else exp.DType.DOUBLE)
516                expression.set("expressions", None)
517        return super().datatype_sql(expression)
518
519    def version_sql(self, expression: exp.Version) -> str:
520        sql = super().version_sql(expression)
521        return sql.replace("FOR ", "", 1)
522
523    def struct_sql(self, expression: exp.Struct) -> str:
524        values = []
525
526        for i, e in enumerate(expression.expressions):
527            if isinstance(e, exp.PropertyEQ):
528                self.unsupported("Hive does not support named structs.")
529                values.append(e.expression)
530            else:
531                values.append(e)
532
533        return self.func("STRUCT", *values)
534
535    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
536        return super().columndef_sql(
537            expression,
538            sep=(
539                ": "
540                if isinstance(expression.parent, exp.DataType)
541                and expression.parent.is_type("struct")
542                else sep
543            ),
544        )
545
546    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
547        if expression.args.get("exists"):
548            self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect")
549
550        this = self.sql(expression, "this")
551        new_name = self.sql(expression, "rename_to") or this
552        dtype = self.sql(expression, "dtype")
553        comment = (
554            f" COMMENT {self.sql(expression, 'comment')}" if self.sql(expression, "comment") else ""
555        )
556        default = self.sql(expression, "default")
557        visible = expression.args.get("visible")
558        allow_null = expression.args.get("allow_null")
559        drop = expression.args.get("drop")
560
561        if any([default, drop, visible]) or allow_null is not None:
562            self.unsupported("Unsupported CHANGE COLUMN syntax")
563
564        if not dtype:
565            self.unsupported("CHANGE COLUMN without a type is not supported")
566
567        return f"CHANGE COLUMN {this} {new_name} {dtype}{comment}"
568
569    def renamecolumn_sql(self, expression: exp.RenameColumn) -> str:
570        self.unsupported("Cannot rename columns without data type defined in Hive")
571        return ""
572
573    def alterset_sql(self, expression: exp.AlterSet) -> str:
574        exprs = self.expressions(expression, flat=True)
575        exprs = f" {exprs}" if exprs else ""
576        location = self.sql(expression, "location")
577        location = f" LOCATION {location}" if location else ""
578        file_format = self.expressions(expression, key="file_format", flat=True, sep=" ")
579        file_format = f" FILEFORMAT {file_format}" if file_format else ""
580        serde = self.sql(expression, "serde")
581        serde = f" SERDE {serde}" if serde else ""
582        tags = self.expressions(expression, key="tag", flat=True, sep="")
583        tags = f" TAGS {tags}" if tags else ""
584
585        return f"SET{serde}{exprs}{location}{file_format}{tags}"
586
587    def serdeproperties_sql(self, expression: exp.SerdeProperties) -> str:
588        prefix = "WITH " if expression.args.get("with_") else ""
589        exprs = self.expressions(expression, flat=True)
590
591        return f"{prefix}SERDEPROPERTIES ({exprs})"
592
593    def exists_sql(self, expression: exp.Exists) -> str:
594        if expression.expression:
595            return self.function_fallback_sql(expression)
596
597        return super().exists_sql(expression)
598
599    def timetostr_sql(self, expression: exp.TimeToStr) -> str:
600        this = expression.this
601        if isinstance(this, exp.TimeStrToTime):
602            this = this.this
603
604        return self.func("DATE_FORMAT", this, self.format_time(expression))
605
606    def usingproperty_sql(self, expression: exp.UsingProperty) -> str:
607        kind = expression.args.get("kind")
608        return f"USING {kind} {self.sql(expression, 'this')}"
609
610    def fileformatproperty_sql(self, expression: exp.FileFormatProperty) -> str:
611        if isinstance(expression.this, exp.InputOutputFormat):
612            this = self.sql(expression, "this")
613        else:
614            this = expression.name.upper()
615
616        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'")
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')
class HiveGenerator(sqlglot.generator.Generator):
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    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 expression.this in exp.DataType.TEMPORAL_TYPES:
511            expression.set("expressions", None)
512        elif expression.is_type("float"):
513            size_expression = expression.find(exp.DataTypeParam)
514            if size_expression:
515                size = int(size_expression.name)
516                expression.set("this", exp.DType.FLOAT if size <= 32 else exp.DType.DOUBLE)
517                expression.set("expressions", None)
518        return super().datatype_sql(expression)
519
520    def version_sql(self, expression: exp.Version) -> str:
521        sql = super().version_sql(expression)
522        return sql.replace("FOR ", "", 1)
523
524    def struct_sql(self, expression: exp.Struct) -> str:
525        values = []
526
527        for i, e in enumerate(expression.expressions):
528            if isinstance(e, exp.PropertyEQ):
529                self.unsupported("Hive does not support named structs.")
530                values.append(e.expression)
531            else:
532                values.append(e)
533
534        return self.func("STRUCT", *values)
535
536    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
537        return super().columndef_sql(
538            expression,
539            sep=(
540                ": "
541                if isinstance(expression.parent, exp.DataType)
542                and expression.parent.is_type("struct")
543                else sep
544            ),
545        )
546
547    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
548        if expression.args.get("exists"):
549            self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect")
550
551        this = self.sql(expression, "this")
552        new_name = self.sql(expression, "rename_to") or this
553        dtype = self.sql(expression, "dtype")
554        comment = (
555            f" COMMENT {self.sql(expression, 'comment')}" if self.sql(expression, "comment") else ""
556        )
557        default = self.sql(expression, "default")
558        visible = expression.args.get("visible")
559        allow_null = expression.args.get("allow_null")
560        drop = expression.args.get("drop")
561
562        if any([default, drop, visible]) or allow_null is not None:
563            self.unsupported("Unsupported CHANGE COLUMN syntax")
564
565        if not dtype:
566            self.unsupported("CHANGE COLUMN without a type is not supported")
567
568        return f"CHANGE COLUMN {this} {new_name} {dtype}{comment}"
569
570    def renamecolumn_sql(self, expression: exp.RenameColumn) -> str:
571        self.unsupported("Cannot rename columns without data type defined in Hive")
572        return ""
573
574    def alterset_sql(self, expression: exp.AlterSet) -> str:
575        exprs = self.expressions(expression, flat=True)
576        exprs = f" {exprs}" if exprs else ""
577        location = self.sql(expression, "location")
578        location = f" LOCATION {location}" if location else ""
579        file_format = self.expressions(expression, key="file_format", flat=True, sep=" ")
580        file_format = f" FILEFORMAT {file_format}" if file_format else ""
581        serde = self.sql(expression, "serde")
582        serde = f" SERDE {serde}" if serde else ""
583        tags = self.expressions(expression, key="tag", flat=True, sep="")
584        tags = f" TAGS {tags}" if tags else ""
585
586        return f"SET{serde}{exprs}{location}{file_format}{tags}"
587
588    def serdeproperties_sql(self, expression: exp.SerdeProperties) -> str:
589        prefix = "WITH " if expression.args.get("with_") else ""
590        exprs = self.expressions(expression, flat=True)
591
592        return f"{prefix}SERDEPROPERTIES ({exprs})"
593
594    def exists_sql(self, expression: exp.Exists) -> str:
595        if expression.expression:
596            return self.function_fallback_sql(expression)
597
598        return super().exists_sql(expression)
599
600    def timetostr_sql(self, expression: exp.TimeToStr) -> str:
601        this = expression.this
602        if isinstance(this, exp.TimeStrToTime):
603            this = this.this
604
605        return self.func("DATE_FORMAT", this, self.format_time(expression))
606
607    def usingproperty_sql(self, expression: exp.UsingProperty) -> str:
608        kind = expression.args.get("kind")
609        return f"USING {kind} {self.sql(expression, 'this')}"
610
611    def fileformatproperty_sql(self, expression: exp.FileFormatProperty) -> str:
612        if isinstance(expression.this, exp.InputOutputFormat):
613            this = self.sql(expression, "this")
614        else:
615            this = expression.name.upper()
616
617        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 WHERE clause. 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
BYTE_STRING_FUNCTION = 'UNHEX'
SELECT_KINDS: tuple[str, ...] = ()
TRY_SUPPORTED = False
SUPPORTS_UESCAPE = False
SUPPORTS_GROUPING_SETS_AS_SUFFIX = True
SUPPORTS_DECODE_CASE = False
LIMIT_FETCH = 'LIMIT'
TABLESAMPLE_WITH_METHOD = False
JOIN_HINTS = False
TABLE_HINTS = False
QUERY_HINTS = False
INDEX_ON = 'ON TABLE'
EXTRACT_ALLOWS_QUOTES = False
NVL2_SUPPORTED = False
LAST_DAY_SUPPORTS_DATE_PART = False
JSON_PATH_SINGLE_QUOTE_ESCAPE = True
SAFE_JSON_PATH_KEY_RE = re.compile('^[_\\-a-zA-Z][\\-\\w]*$')
SUPPORTS_TO_NUMBER = False
WITH_PROPERTIES_PREFIX = 'TBLPROPERTIES'
PARSE_JSON_NAME: str | None = 'PARSE_JSON'
PAD_FILL_PATTERN_IS_REQUIRED = True
SUPPORTS_MEDIAN = False
ARRAY_SIZE_NAME = 'SIZE'
ALTER_SET_TYPE = ''
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.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.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.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.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.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.DistStyleProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <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.IncludeProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.properties.InputModelProperty'>: <PropertiesLocation.POST_SCHEMA: 'POST_SCHEMA'>, <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.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.POST_SCHEMA: 'POST_SCHEMA'>, <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.ForceProperty'>: <PropertiesLocation.POST_CREATE: 'POST_CREATE'>}
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:
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)
def ignorenulls_sql(self, expression: sqlglot.expressions.core.IgnoreNulls) -> str:
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)
def unnest_sql(self, expression: sqlglot.expressions.array.Unnest) -> str:
446    def unnest_sql(self, expression: exp.Unnest) -> str:
447        return rename_func("EXPLODE")(self, expression)
def parameter_sql(self, expression: sqlglot.expressions.core.Parameter) -> str:
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}}}"
def schema_sql(self, expression: sqlglot.expressions.query.Schema) -> str:
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)
def constraint_sql(self, expression: sqlglot.expressions.constraints.Constraint) -> str:
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}"
def rowformatserdeproperty_sql( self, expression: sqlglot.expressions.properties.RowFormatSerdeProperty) -> str:
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}"
def arrayagg_sql(self, expression: sqlglot.expressions.aggregate.ArrayAgg) -> str:
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        )
@unsupported_args('decimals')
def trunc_sql(self, expression: sqlglot.expressions.math.Trunc) -> str:
498    @unsupported_args("decimals")
499    def trunc_sql(self, expression: exp.Trunc) -> str:
500        return self.sql(exp.cast(expression.this, exp.DType.BIGINT))
def datatype_sql(self, expression: sqlglot.expressions.datatypes.DataType) -> str:
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 expression.this in exp.DataType.TEMPORAL_TYPES:
511            expression.set("expressions", None)
512        elif expression.is_type("float"):
513            size_expression = expression.find(exp.DataTypeParam)
514            if size_expression:
515                size = int(size_expression.name)
516                expression.set("this", exp.DType.FLOAT if size <= 32 else exp.DType.DOUBLE)
517                expression.set("expressions", None)
518        return super().datatype_sql(expression)
def version_sql(self, expression: sqlglot.expressions.query.Version) -> str:
520    def version_sql(self, expression: exp.Version) -> str:
521        sql = super().version_sql(expression)
522        return sql.replace("FOR ", "", 1)
def struct_sql(self, expression: sqlglot.expressions.array.Struct) -> str:
524    def struct_sql(self, expression: exp.Struct) -> str:
525        values = []
526
527        for i, e in enumerate(expression.expressions):
528            if isinstance(e, exp.PropertyEQ):
529                self.unsupported("Hive does not support named structs.")
530                values.append(e.expression)
531            else:
532                values.append(e)
533
534        return self.func("STRUCT", *values)
def columndef_sql( self, expression: sqlglot.expressions.query.ColumnDef, sep: str = ' ') -> str:
536    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
537        return super().columndef_sql(
538            expression,
539            sep=(
540                ": "
541                if isinstance(expression.parent, exp.DataType)
542                and expression.parent.is_type("struct")
543                else sep
544            ),
545        )
def altercolumn_sql(self, expression: sqlglot.expressions.ddl.AlterColumn) -> str:
547    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
548        if expression.args.get("exists"):
549            self.unsupported("ALTER COLUMN IF EXISTS is not supported by this dialect")
550
551        this = self.sql(expression, "this")
552        new_name = self.sql(expression, "rename_to") or this
553        dtype = self.sql(expression, "dtype")
554        comment = (
555            f" COMMENT {self.sql(expression, 'comment')}" if self.sql(expression, "comment") else ""
556        )
557        default = self.sql(expression, "default")
558        visible = expression.args.get("visible")
559        allow_null = expression.args.get("allow_null")
560        drop = expression.args.get("drop")
561
562        if any([default, drop, visible]) or allow_null is not None:
563            self.unsupported("Unsupported CHANGE COLUMN syntax")
564
565        if not dtype:
566            self.unsupported("CHANGE COLUMN without a type is not supported")
567
568        return f"CHANGE COLUMN {this} {new_name} {dtype}{comment}"
def renamecolumn_sql(self, expression: sqlglot.expressions.ddl.RenameColumn) -> str:
570    def renamecolumn_sql(self, expression: exp.RenameColumn) -> str:
571        self.unsupported("Cannot rename columns without data type defined in Hive")
572        return ""
def alterset_sql(self, expression: sqlglot.expressions.ddl.AlterSet) -> str:
574    def alterset_sql(self, expression: exp.AlterSet) -> str:
575        exprs = self.expressions(expression, flat=True)
576        exprs = f" {exprs}" if exprs else ""
577        location = self.sql(expression, "location")
578        location = f" LOCATION {location}" if location else ""
579        file_format = self.expressions(expression, key="file_format", flat=True, sep=" ")
580        file_format = f" FILEFORMAT {file_format}" if file_format else ""
581        serde = self.sql(expression, "serde")
582        serde = f" SERDE {serde}" if serde else ""
583        tags = self.expressions(expression, key="tag", flat=True, sep="")
584        tags = f" TAGS {tags}" if tags else ""
585
586        return f"SET{serde}{exprs}{location}{file_format}{tags}"
def serdeproperties_sql(self, expression: sqlglot.expressions.properties.SerdeProperties) -> str:
588    def serdeproperties_sql(self, expression: exp.SerdeProperties) -> str:
589        prefix = "WITH " if expression.args.get("with_") else ""
590        exprs = self.expressions(expression, flat=True)
591
592        return f"{prefix}SERDEPROPERTIES ({exprs})"
def exists_sql(self, expression: sqlglot.expressions.functions.Exists) -> str:
594    def exists_sql(self, expression: exp.Exists) -> str:
595        if expression.expression:
596            return self.function_fallback_sql(expression)
597
598        return super().exists_sql(expression)
def timetostr_sql(self, expression: sqlglot.expressions.temporal.TimeToStr) -> str:
600    def timetostr_sql(self, expression: exp.TimeToStr) -> str:
601        this = expression.this
602        if isinstance(this, exp.TimeStrToTime):
603            this = this.this
604
605        return self.func("DATE_FORMAT", this, self.format_time(expression))
def usingproperty_sql(self, expression: sqlglot.expressions.properties.UsingProperty) -> str:
607    def usingproperty_sql(self, expression: exp.UsingProperty) -> str:
608        kind = expression.args.get("kind")
609        return f"USING {kind} {self.sql(expression, 'this')}"
def fileformatproperty_sql( self, expression: sqlglot.expressions.properties.FileFormatProperty) -> str:
611    def fileformatproperty_sql(self, expression: exp.FileFormatProperty) -> str:
612        if isinstance(expression.this, exp.InputOutputFormat):
613            this = self.sql(expression, "this")
614        else:
615            this = expression.name.upper()
616
617        return f"STORED AS {this}"
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
SINGLE_STRING_INTERVAL
INTERVAL_ALLOWS_PLURAL_FORM
AUTO_REFRESH_BARE_INTERVALS
LIMIT_ONLY_LITERALS
RENAME_TABLE_WITH_DB
GROUPINGS_SEP
INOUT_SEPARATOR
DIRECTED_JOINS
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
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
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
connect_sql
prior_sql
join_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
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
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
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