Edit on GitHub

# sqlglot.generator

View Source

       1from __future__ import annotations
       2
       3import logging
       4import typing as t
       5from collections import defaultdict
       6
       7from sqlglot import exp
       8from sqlglot.errors import ErrorLevel, UnsupportedError, concat_messages
       9from sqlglot.helper import apply_index_offset, csv, seq_get
      10from sqlglot.time import format_time
      11from sqlglot.tokens import TokenType
      12
      13logger = logging.getLogger("sqlglot")
      14
      15
      16class Generator:
      17    """
      18    Generator converts a given syntax tree to the corresponding SQL string.
      19
      20    Args:
      21        pretty: Whether or not to format the produced SQL string.
      22            Default: False.
      23        identify: Determines when an identifier should be quoted. Possible values are:
      24            False (default): Never quote, except in cases where it's mandatory by the dialect.
      25            True or 'always': Always quote.
      26            'safe': Only quote identifiers that are case insensitive.
      27        normalize: Whether or not to normalize identifiers to lowercase.
      28            Default: False.
      29        pad: Determines the pad size in a formatted string.
      30            Default: 2.
      31        indent: Determines the indentation size in a formatted string.
      32            Default: 2.
      33        normalize_functions: Whether or not to normalize all function names. Possible values are:
      34            "upper" or True (default): Convert names to uppercase.
      35            "lower": Convert names to lowercase.
      36            False: Disables function name normalization.
      37        unsupported_level: Determines the generator's behavior when it encounters unsupported expressions.
      38            Default ErrorLevel.WARN.
      39        max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError.
      40            This is only relevant if unsupported_level is ErrorLevel.RAISE.
      41            Default: 3
      42        leading_comma: Determines whether or not the comma is leading or trailing in select expressions.
      43            This is only relevant when generating in pretty mode.
      44            Default: False
      45        max_text_width: The max number of characters in a segment before creating new lines in pretty mode.
      46            The default is on the smaller end because the length only represents a segment and not the true
      47            line length.
      48            Default: 80
      49        comments: Whether or not to preserve comments in the output SQL code.
      50            Default: True
      51    """
      52
      53    TRANSFORMS = {
      54        exp.DateAdd: lambda self, e: self.func(
      55            "DATE_ADD", e.this, e.expression, exp.Literal.string(e.text("unit"))
      56        ),
      57        exp.TsOrDsAdd: lambda self, e: self.func(
      58            "TS_OR_DS_ADD", e.this, e.expression, exp.Literal.string(e.text("unit"))
      59        ),
      60        exp.CaseSpecificColumnConstraint: lambda self, e: f"{'NOT ' if e.args.get('not_') else ''}CASESPECIFIC",
      61        exp.CharacterSetColumnConstraint: lambda self, e: f"CHARACTER SET {self.sql(e, 'this')}",
      62        exp.CharacterSetProperty: lambda self, e: f"{'DEFAULT ' if e.args.get('default') else ''}CHARACTER SET={self.sql(e, 'this')}",
      63        exp.CheckColumnConstraint: lambda self, e: f"CHECK ({self.sql(e, 'this')})",
      64        exp.CollateColumnConstraint: lambda self, e: f"COLLATE {self.sql(e, 'this')}",
      65        exp.CopyGrantsProperty: lambda self, e: "COPY GRANTS",
      66        exp.CommentColumnConstraint: lambda self, e: f"COMMENT {self.sql(e, 'this')}",
      67        exp.DateFormatColumnConstraint: lambda self, e: f"FORMAT {self.sql(e, 'this')}",
      68        exp.DefaultColumnConstraint: lambda self, e: f"DEFAULT {self.sql(e, 'this')}",
      69        exp.EncodeColumnConstraint: lambda self, e: f"ENCODE {self.sql(e, 'this')}",
      70        exp.ExecuteAsProperty: lambda self, e: self.naked_property(e),
      71        exp.ExternalProperty: lambda self, e: "EXTERNAL",
      72        exp.HeapProperty: lambda self, e: "HEAP",
      73        exp.InlineLengthColumnConstraint: lambda self, e: f"INLINE LENGTH {self.sql(e, 'this')}",
      74        exp.LanguageProperty: lambda self, e: self.naked_property(e),
      75        exp.LocationProperty: lambda self, e: self.naked_property(e),
      76        exp.LogProperty: lambda self, e: f"{'NO ' if e.args.get('no') else ''}LOG",
      77        exp.MaterializedProperty: lambda self, e: "MATERIALIZED",
      78        exp.NoPrimaryIndexProperty: lambda self, e: "NO PRIMARY INDEX",
      79        exp.OnCommitProperty: lambda self, e: f"ON COMMIT {'DELETE' if e.args.get('delete') else 'PRESERVE'} ROWS",
      80        exp.OnUpdateColumnConstraint: lambda self, e: f"ON UPDATE {self.sql(e, 'this')}",
      81        exp.PathColumnConstraint: lambda self, e: f"PATH {self.sql(e, 'this')}",
      82        exp.ReturnsProperty: lambda self, e: self.naked_property(e),
      83        exp.SetProperty: lambda self, e: f"{'MULTI' if e.args.get('multi') else ''}SET",
      84        exp.SettingsProperty: lambda self, e: f"SETTINGS{self.seg('')}{(self.expressions(e))}",
      85        exp.SqlSecurityProperty: lambda self, e: f"SQL SECURITY {'DEFINER' if e.args.get('definer') else 'INVOKER'}",
      86        exp.StabilityProperty: lambda self, e: e.name,
      87        exp.TemporaryProperty: lambda self, e: f"TEMPORARY",
      88        exp.ToTableProperty: lambda self, e: f"TO {self.sql(e.this)}",
      89        exp.TransientProperty: lambda self, e: "TRANSIENT",
      90        exp.TitleColumnConstraint: lambda self, e: f"TITLE {self.sql(e, 'this')}",
      91        exp.UppercaseColumnConstraint: lambda self, e: f"UPPERCASE",
      92        exp.VarMap: lambda self, e: self.func("MAP", e.args["keys"], e.args["values"]),
      93        exp.VolatileProperty: lambda self, e: "VOLATILE",
      94        exp.WithJournalTableProperty: lambda self, e: f"WITH JOURNAL TABLE={self.sql(e, 'this')}",
      95    }
      96
      97    # Whether or not null ordering is supported in order by
      98    NULL_ORDERING_SUPPORTED = True
      99
     100    # Whether or not locking reads (i.e. SELECT ... FOR UPDATE/SHARE) are supported
     101    LOCKING_READS_SUPPORTED = False
     102
     103    # Always do union distinct or union all
     104    EXPLICIT_UNION = False
     105
     106    # Wrap derived values in parens, usually standard but spark doesn't support it
     107    WRAP_DERIVED_VALUES = True
     108
     109    # Whether or not create function uses an AS before the RETURN
     110    CREATE_FUNCTION_RETURN_AS = True
     111
     112    # Whether or not MERGE ... WHEN MATCHED BY SOURCE is allowed
     113    MATCHED_BY_SOURCE = True
     114
     115    # Whether or not the INTERVAL expression works only with values like '1 day'
     116    SINGLE_STRING_INTERVAL = False
     117
     118    # Whether or not the plural form of date parts like day (i.e. "days") is supported in INTERVALs
     119    INTERVAL_ALLOWS_PLURAL_FORM = True
     120
     121    # Whether or not the TABLESAMPLE clause supports a method name, like BERNOULLI
     122    TABLESAMPLE_WITH_METHOD = True
     123
     124    # Whether or not to treat the number in TABLESAMPLE (50) as a percentage
     125    TABLESAMPLE_SIZE_IS_PERCENT = False
     126
     127    # Whether or not limit and fetch are supported (possible values: "ALL", "LIMIT", "FETCH")
     128    LIMIT_FETCH = "ALL"
     129
     130    # Whether or not a table is allowed to be renamed with a db
     131    RENAME_TABLE_WITH_DB = True
     132
     133    # The separator for grouping sets and rollups
     134    GROUPINGS_SEP = ","
     135
     136    # The string used for creating an index on a table
     137    INDEX_ON = "ON"
     138
     139    # Whether or not join hints should be generated
     140    JOIN_HINTS = True
     141
     142    # Whether or not table hints should be generated
     143    TABLE_HINTS = True
     144
     145    # Whether or not query hints should be generated
     146    QUERY_HINTS = True
     147
     148    # What kind of separator to use for query hints
     149    QUERY_HINT_SEP = ", "
     150
     151    # Whether or not comparing against booleans (e.g. x IS TRUE) is supported
     152    IS_BOOL_ALLOWED = True
     153
     154    # Whether or not to include the "SET" keyword in the "INSERT ... ON DUPLICATE KEY UPDATE" statement
     155    DUPLICATE_KEY_UPDATE_WITH_SET = True
     156
     157    # Whether or not to generate the limit as TOP <value> instead of LIMIT <value>
     158    LIMIT_IS_TOP = False
     159
     160    # Whether or not to generate INSERT INTO ... RETURNING or INSERT INTO RETURNING ...
     161    RETURNING_END = True
     162
     163    # Whether or not to generate the (+) suffix for columns used in old-style join conditions
     164    COLUMN_JOIN_MARKS_SUPPORTED = False
     165
     166    # Whether or not to generate an unquoted value for EXTRACT's date part argument
     167    EXTRACT_ALLOWS_QUOTES = True
     168
     169    # https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
     170    SELECT_KINDS: t.Tuple[str, ...] = ("STRUCT", "VALUE")
     171
     172    # Whether or not VALUES statements can be used as derived tables.
     173    # MySQL 5 and Redshift do not allow this, so when False, it will convert
     174    # SELECT * VALUES into SELECT UNION
     175    VALUES_AS_TABLE = True
     176
     177    TYPE_MAPPING = {
     178        exp.DataType.Type.NCHAR: "CHAR",
     179        exp.DataType.Type.NVARCHAR: "VARCHAR",
     180        exp.DataType.Type.MEDIUMTEXT: "TEXT",
     181        exp.DataType.Type.LONGTEXT: "TEXT",
     182        exp.DataType.Type.MEDIUMBLOB: "BLOB",
     183        exp.DataType.Type.LONGBLOB: "BLOB",
     184        exp.DataType.Type.INET: "INET",
     185    }
     186
     187    STAR_MAPPING = {
     188        "except": "EXCEPT",
     189        "replace": "REPLACE",
     190    }
     191
     192    TIME_PART_SINGULARS = {
     193        "microseconds": "microsecond",
     194        "seconds": "second",
     195        "minutes": "minute",
     196        "hours": "hour",
     197        "days": "day",
     198        "weeks": "week",
     199        "months": "month",
     200        "quarters": "quarter",
     201        "years": "year",
     202    }
     203
     204    TOKEN_MAPPING: t.Dict[TokenType, str] = {}
     205
     206    STRUCT_DELIMITER = ("<", ">")
     207
     208    PARAMETER_TOKEN = "@"
     209
     210    PROPERTIES_LOCATION = {
     211        exp.AlgorithmProperty: exp.Properties.Location.POST_CREATE,
     212        exp.AutoIncrementProperty: exp.Properties.Location.POST_SCHEMA,
     213        exp.BlockCompressionProperty: exp.Properties.Location.POST_NAME,
     214        exp.CharacterSetProperty: exp.Properties.Location.POST_SCHEMA,
     215        exp.ChecksumProperty: exp.Properties.Location.POST_NAME,
     216        exp.CollateProperty: exp.Properties.Location.POST_SCHEMA,
     217        exp.CopyGrantsProperty: exp.Properties.Location.POST_SCHEMA,
     218        exp.Cluster: exp.Properties.Location.POST_SCHEMA,
     219        exp.ClusteredByProperty: exp.Properties.Location.POST_SCHEMA,
     220        exp.DataBlocksizeProperty: exp.Properties.Location.POST_NAME,
     221        exp.DefinerProperty: exp.Properties.Location.POST_CREATE,
     222        exp.DictRange: exp.Properties.Location.POST_SCHEMA,
     223        exp.DictProperty: exp.Properties.Location.POST_SCHEMA,
     224        exp.DistKeyProperty: exp.Properties.Location.POST_SCHEMA,
     225        exp.DistStyleProperty: exp.Properties.Location.POST_SCHEMA,
     226        exp.EngineProperty: exp.Properties.Location.POST_SCHEMA,
     227        exp.ExecuteAsProperty: exp.Properties.Location.POST_SCHEMA,
     228        exp.ExternalProperty: exp.Properties.Location.POST_CREATE,
     229        exp.FallbackProperty: exp.Properties.Location.POST_NAME,
     230        exp.FileFormatProperty: exp.Properties.Location.POST_WITH,
     231        exp.FreespaceProperty: exp.Properties.Location.POST_NAME,
     232        exp.HeapProperty: exp.Properties.Location.POST_WITH,
     233        exp.IsolatedLoadingProperty: exp.Properties.Location.POST_NAME,
     234        exp.JournalProperty: exp.Properties.Location.POST_NAME,
     235        exp.LanguageProperty: exp.Properties.Location.POST_SCHEMA,
     236        exp.LikeProperty: exp.Properties.Location.POST_SCHEMA,
     237        exp.LocationProperty: exp.Properties.Location.POST_SCHEMA,
     238        exp.LockingProperty: exp.Properties.Location.POST_ALIAS,
     239        exp.LogProperty: exp.Properties.Location.POST_NAME,
     240        exp.MaterializedProperty: exp.Properties.Location.POST_CREATE,
     241        exp.MergeBlockRatioProperty: exp.Properties.Location.POST_NAME,
     242        exp.NoPrimaryIndexProperty: exp.Properties.Location.POST_EXPRESSION,
     243        exp.OnCommitProperty: exp.Properties.Location.POST_EXPRESSION,
     244        exp.Order: exp.Properties.Location.POST_SCHEMA,
     245        exp.PartitionedByProperty: exp.Properties.Location.POST_WITH,
     246        exp.PrimaryKey: exp.Properties.Location.POST_SCHEMA,
     247        exp.Property: exp.Properties.Location.POST_WITH,
     248        exp.ReturnsProperty: exp.Properties.Location.POST_SCHEMA,
     249        exp.RowFormatProperty: exp.Properties.Location.POST_SCHEMA,
     250        exp.RowFormatDelimitedProperty: exp.Properties.Location.POST_SCHEMA,
     251        exp.RowFormatSerdeProperty: exp.Properties.Location.POST_SCHEMA,
     252        exp.SchemaCommentProperty: exp.Properties.Location.POST_SCHEMA,
     253        exp.SerdeProperties: exp.Properties.Location.POST_SCHEMA,
     254        exp.Set: exp.Properties.Location.POST_SCHEMA,
     255        exp.SettingsProperty: exp.Properties.Location.POST_SCHEMA,
     256        exp.SetProperty: exp.Properties.Location.POST_CREATE,
     257        exp.SortKeyProperty: exp.Properties.Location.POST_SCHEMA,
     258        exp.SqlSecurityProperty: exp.Properties.Location.POST_CREATE,
     259        exp.StabilityProperty: exp.Properties.Location.POST_SCHEMA,
     260        exp.TemporaryProperty: exp.Properties.Location.POST_CREATE,
     261        exp.ToTableProperty: exp.Properties.Location.POST_SCHEMA,
     262        exp.TransientProperty: exp.Properties.Location.POST_CREATE,
     263        exp.MergeTreeTTL: exp.Properties.Location.POST_SCHEMA,
     264        exp.VolatileProperty: exp.Properties.Location.POST_CREATE,
     265        exp.WithDataProperty: exp.Properties.Location.POST_EXPRESSION,
     266        exp.WithJournalTableProperty: exp.Properties.Location.POST_NAME,
     267    }
     268
     269    # Keywords that can't be used as unquoted identifier names
     270    RESERVED_KEYWORDS: t.Set[str] = set()
     271
     272    # Expressions whose comments are separated from them for better formatting
     273    WITH_SEPARATED_COMMENTS: t.Tuple[t.Type[exp.Expression], ...] = (
     274        exp.Delete,
     275        exp.Drop,
     276        exp.From,
     277        exp.Insert,
     278        exp.Select,
     279        exp.Update,
     280        exp.Where,
     281        exp.With,
     282    )
     283
     284    # Expressions that can remain unwrapped when appearing in the context of an INTERVAL
     285    UNWRAPPED_INTERVAL_VALUES: t.Tuple[t.Type[exp.Expression], ...] = (
     286        exp.Column,
     287        exp.Literal,
     288        exp.Neg,
     289        exp.Paren,
     290    )
     291
     292    SENTINEL_LINE_BREAK = "__SQLGLOT__LB__"
     293
     294    # Autofilled
     295    INVERSE_TIME_MAPPING: t.Dict[str, str] = {}
     296    INVERSE_TIME_TRIE: t.Dict = {}
     297    INDEX_OFFSET = 0
     298    UNNEST_COLUMN_ONLY = False
     299    ALIAS_POST_TABLESAMPLE = False
     300    IDENTIFIERS_CAN_START_WITH_DIGIT = False
     301    STRICT_STRING_CONCAT = False
     302    NORMALIZE_FUNCTIONS: bool | str = "upper"
     303    NULL_ORDERING = "nulls_are_small"
     304    ESCAPE_LINE_BREAK = False
     305
     306    can_identify: t.Callable[[str, str | bool], bool]
     307
     308    # Delimiters for quotes, identifiers and the corresponding escape characters
     309    QUOTE_START = "'"
     310    QUOTE_END = "'"
     311    IDENTIFIER_START = '"'
     312    IDENTIFIER_END = '"'
     313    STRING_ESCAPE = "'"
     314    IDENTIFIER_ESCAPE = '"'
     315
     316    # Delimiters for bit, hex, byte and raw literals
     317    BIT_START: t.Optional[str] = None
     318    BIT_END: t.Optional[str] = None
     319    HEX_START: t.Optional[str] = None
     320    HEX_END: t.Optional[str] = None
     321    BYTE_START: t.Optional[str] = None
     322    BYTE_END: t.Optional[str] = None
     323
     324    __slots__ = (
     325        "pretty",
     326        "identify",
     327        "normalize",
     328        "pad",
     329        "_indent",
     330        "normalize_functions",
     331        "unsupported_level",
     332        "max_unsupported",
     333        "leading_comma",
     334        "max_text_width",
     335        "comments",
     336        "unsupported_messages",
     337        "_escaped_quote_end",
     338        "_escaped_identifier_end",
     339        "_cache",
     340    )
     341
     342    def __init__(
     343        self,
     344        pretty: t.Optional[bool] = None,
     345        identify: str | bool = False,
     346        normalize: bool = False,
     347        pad: int = 2,
     348        indent: int = 2,
     349        normalize_functions: t.Optional[str | bool] = None,
     350        unsupported_level: ErrorLevel = ErrorLevel.WARN,
     351        max_unsupported: int = 3,
     352        leading_comma: bool = False,
     353        max_text_width: int = 80,
     354        comments: bool = True,
     355    ):
     356        import sqlglot
     357
     358        self.pretty = pretty if pretty is not None else sqlglot.pretty
     359        self.identify = identify
     360        self.normalize = normalize
     361        self.pad = pad
     362        self._indent = indent
     363        self.unsupported_level = unsupported_level
     364        self.max_unsupported = max_unsupported
     365        self.leading_comma = leading_comma
     366        self.max_text_width = max_text_width
     367        self.comments = comments
     368
     369        # This is both a Dialect property and a Generator argument, so we prioritize the latter
     370        self.normalize_functions = (
     371            self.NORMALIZE_FUNCTIONS if normalize_functions is None else normalize_functions
     372        )
     373
     374        self.unsupported_messages: t.List[str] = []
     375        self._escaped_quote_end: str = self.STRING_ESCAPE + self.QUOTE_END
     376        self._escaped_identifier_end: str = self.IDENTIFIER_ESCAPE + self.IDENTIFIER_END
     377        self._cache: t.Optional[t.Dict[int, str]] = None
     378
     379    def generate(
     380        self,
     381        expression: t.Optional[exp.Expression],
     382        cache: t.Optional[t.Dict[int, str]] = None,
     383    ) -> str:
     384        """
     385        Generates the SQL string corresponding to the given syntax tree.
     386
     387        Args:
     388            expression: The syntax tree.
     389            cache: An optional sql string cache. This leverages the hash of an Expression
     390                which can be slow to compute, so only use it if you set _hash on each node.
     391
     392        Returns:
     393            The SQL string corresponding to `expression`.
     394        """
     395        if cache is not None:
     396            self._cache = cache
     397
     398        self.unsupported_messages = []
     399        sql = self.sql(expression).strip()
     400        self._cache = None
     401
     402        if self.unsupported_level == ErrorLevel.IGNORE:
     403            return sql
     404
     405        if self.unsupported_level == ErrorLevel.WARN:
     406            for msg in self.unsupported_messages:
     407                logger.warning(msg)
     408        elif self.unsupported_level == ErrorLevel.RAISE and self.unsupported_messages:
     409            raise UnsupportedError(concat_messages(self.unsupported_messages, self.max_unsupported))
     410
     411        if self.pretty:
     412            sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
     413        return sql
     414
     415    def unsupported(self, message: str) -> None:
     416        if self.unsupported_level == ErrorLevel.IMMEDIATE:
     417            raise UnsupportedError(message)
     418        self.unsupported_messages.append(message)
     419
     420    def sep(self, sep: str = " ") -> str:
     421        return f"{sep.strip()}\n" if self.pretty else sep
     422
     423    def seg(self, sql: str, sep: str = " ") -> str:
     424        return f"{self.sep(sep)}{sql}"
     425
     426    def pad_comment(self, comment: str) -> str:
     427        comment = " " + comment if comment[0].strip() else comment
     428        comment = comment + " " if comment[-1].strip() else comment
     429        return comment
     430
     431    def maybe_comment(
     432        self,
     433        sql: str,
     434        expression: t.Optional[exp.Expression] = None,
     435        comments: t.Optional[t.List[str]] = None,
     436    ) -> str:
     437        comments = (
     438            ((expression and expression.comments) if comments is None else comments)  # type: ignore
     439            if self.comments
     440            else None
     441        )
     442
     443        if not comments or isinstance(expression, exp.Binary):
     444            return sql
     445
     446        sep = "\n" if self.pretty else " "
     447        comments_sql = sep.join(
     448            f"/*{self.pad_comment(comment)}*/" for comment in comments if comment
     449        )
     450
     451        if not comments_sql:
     452            return sql
     453
     454        if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
     455            return (
     456                f"{self.sep()}{comments_sql}{sql}"
     457                if sql[0].isspace()
     458                else f"{comments_sql}{self.sep()}{sql}"
     459            )
     460
     461        return f"{sql} {comments_sql}"
     462
     463    def wrap(self, expression: exp.Expression | str) -> str:
     464        this_sql = self.indent(
     465            self.sql(expression)
     466            if isinstance(expression, (exp.Select, exp.Union))
     467            else self.sql(expression, "this"),
     468            level=1,
     469            pad=0,
     470        )
     471        return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"
     472
     473    def no_identify(self, func: t.Callable[..., str], *args, **kwargs) -> str:
     474        original = self.identify
     475        self.identify = False
     476        result = func(*args, **kwargs)
     477        self.identify = original
     478        return result
     479
     480    def normalize_func(self, name: str) -> str:
     481        if self.normalize_functions == "upper" or self.normalize_functions is True:
     482            return name.upper()
     483        if self.normalize_functions == "lower":
     484            return name.lower()
     485        return name
     486
     487    def indent(
     488        self,
     489        sql: str,
     490        level: int = 0,
     491        pad: t.Optional[int] = None,
     492        skip_first: bool = False,
     493        skip_last: bool = False,
     494    ) -> str:
     495        if not self.pretty:
     496            return sql
     497
     498        pad = self.pad if pad is None else pad
     499        lines = sql.split("\n")
     500
     501        return "\n".join(
     502            line
     503            if (skip_first and i == 0) or (skip_last and i == len(lines) - 1)
     504            else f"{' ' * (level * self._indent + pad)}{line}"
     505            for i, line in enumerate(lines)
     506        )
     507
     508    def sql(
     509        self,
     510        expression: t.Optional[str | exp.Expression],
     511        key: t.Optional[str] = None,
     512        comment: bool = True,
     513    ) -> str:
     514        if not expression:
     515            return ""
     516
     517        if isinstance(expression, str):
     518            return expression
     519
     520        if key:
     521            value = expression.args.get(key)
     522            if value:
     523                return self.sql(value)
     524            return ""
     525
     526        if self._cache is not None:
     527            expression_id = hash(expression)
     528
     529            if expression_id in self._cache:
     530                return self._cache[expression_id]
     531
     532        transform = self.TRANSFORMS.get(expression.__class__)
     533
     534        if callable(transform):
     535            sql = transform(self, expression)
     536        elif transform:
     537            sql = transform
     538        elif isinstance(expression, exp.Expression):
     539            exp_handler_name = f"{expression.key}_sql"
     540
     541            if hasattr(self, exp_handler_name):
     542                sql = getattr(self, exp_handler_name)(expression)
     543            elif isinstance(expression, exp.Func):
     544                sql = self.function_fallback_sql(expression)
     545            elif isinstance(expression, exp.Property):
     546                sql = self.property_sql(expression)
     547            else:
     548                raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
     549        else:
     550            raise ValueError(f"Expected an Expression. Received {type(expression)}: {expression}")
     551
     552        sql = self.maybe_comment(sql, expression) if self.comments and comment else sql
     553
     554        if self._cache is not None:
     555            self._cache[expression_id] = sql
     556        return sql
     557
     558    def uncache_sql(self, expression: exp.Uncache) -> str:
     559        table = self.sql(expression, "this")
     560        exists_sql = " IF EXISTS" if expression.args.get("exists") else ""
     561        return f"UNCACHE TABLE{exists_sql} {table}"
     562
     563    def cache_sql(self, expression: exp.Cache) -> str:
     564        lazy = " LAZY" if expression.args.get("lazy") else ""
     565        table = self.sql(expression, "this")
     566        options = expression.args.get("options")
     567        options = f" OPTIONS({self.sql(options[0])} = {self.sql(options[1])})" if options else ""
     568        sql = self.sql(expression, "expression")
     569        sql = f" AS{self.sep()}{sql}" if sql else ""
     570        sql = f"CACHE{lazy} TABLE {table}{options}{sql}"
     571        return self.prepend_ctes(expression, sql)
     572
     573    def characterset_sql(self, expression: exp.CharacterSet) -> str:
     574        if isinstance(expression.parent, exp.Cast):
     575            return f"CHAR CHARACTER SET {self.sql(expression, 'this')}"
     576        default = "DEFAULT " if expression.args.get("default") else ""
     577        return f"{default}CHARACTER SET={self.sql(expression, 'this')}"
     578
     579    def column_sql(self, expression: exp.Column) -> str:
     580        join_mark = " (+)" if expression.args.get("join_mark") else ""
     581
     582        if join_mark and not self.COLUMN_JOIN_MARKS_SUPPORTED:
     583            join_mark = ""
     584            self.unsupported("Outer join syntax using the (+) operator is not supported.")
     585
     586        column = ".".join(
     587            self.sql(part)
     588            for part in (
     589                expression.args.get("catalog"),
     590                expression.args.get("db"),
     591                expression.args.get("table"),
     592                expression.args.get("this"),
     593            )
     594            if part
     595        )
     596
     597        return f"{column}{join_mark}"
     598
     599    def columnposition_sql(self, expression: exp.ColumnPosition) -> str:
     600        this = self.sql(expression, "this")
     601        this = f" {this}" if this else ""
     602        position = self.sql(expression, "position")
     603        return f"{position}{this}"
     604
     605    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
     606        column = self.sql(expression, "this")
     607        kind = self.sql(expression, "kind")
     608        constraints = self.expressions(expression, key="constraints", sep=" ", flat=True)
     609        exists = "IF NOT EXISTS " if expression.args.get("exists") else ""
     610        kind = f"{sep}{kind}" if kind else ""
     611        constraints = f" {constraints}" if constraints else ""
     612        position = self.sql(expression, "position")
     613        position = f" {position}" if position else ""
     614
     615        return f"{exists}{column}{kind}{constraints}{position}"
     616
     617    def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str:
     618        this = self.sql(expression, "this")
     619        kind_sql = self.sql(expression, "kind").strip()
     620        return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql
     621
     622    def autoincrementcolumnconstraint_sql(self, _) -> str:
     623        return self.token_sql(TokenType.AUTO_INCREMENT)
     624
     625    def compresscolumnconstraint_sql(self, expression: exp.CompressColumnConstraint) -> str:
     626        if isinstance(expression.this, list):
     627            this = self.wrap(self.expressions(expression, key="this", flat=True))
     628        else:
     629            this = self.sql(expression, "this")
     630
     631        return f"COMPRESS {this}"
     632
     633    def generatedasidentitycolumnconstraint_sql(
     634        self, expression: exp.GeneratedAsIdentityColumnConstraint
     635    ) -> str:
     636        this = ""
     637        if expression.this is not None:
     638            on_null = "ON NULL " if expression.args.get("on_null") else ""
     639            this = " ALWAYS " if expression.this else f" BY DEFAULT {on_null}"
     640
     641        start = expression.args.get("start")
     642        start = f"START WITH {start}" if start else ""
     643        increment = expression.args.get("increment")
     644        increment = f" INCREMENT BY {increment}" if increment else ""
     645        minvalue = expression.args.get("minvalue")
     646        minvalue = f" MINVALUE {minvalue}" if minvalue else ""
     647        maxvalue = expression.args.get("maxvalue")
     648        maxvalue = f" MAXVALUE {maxvalue}" if maxvalue else ""
     649        cycle = expression.args.get("cycle")
     650        cycle_sql = ""
     651
     652        if cycle is not None:
     653            cycle_sql = f"{' NO' if not cycle else ''} CYCLE"
     654            cycle_sql = cycle_sql.strip() if not start and not increment else cycle_sql
     655
     656        sequence_opts = ""
     657        if start or increment or cycle_sql:
     658            sequence_opts = f"{start}{increment}{minvalue}{maxvalue}{cycle_sql}"
     659            sequence_opts = f" ({sequence_opts.strip()})"
     660
     661        expr = self.sql(expression, "expression")
     662        expr = f"({expr})" if expr else "IDENTITY"
     663
     664        return f"GENERATED{this}AS {expr}{sequence_opts}"
     665
     666    def notnullcolumnconstraint_sql(self, expression: exp.NotNullColumnConstraint) -> str:
     667        return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL"
     668
     669    def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
     670        desc = expression.args.get("desc")
     671        if desc is not None:
     672            return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
     673        return f"PRIMARY KEY"
     674
     675    def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
     676        this = self.sql(expression, "this")
     677        this = f" {this}" if this else ""
     678        return f"UNIQUE{this}"
     679
     680    def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
     681        return self.sql(expression, "this")
     682
     683    def create_sql(self, expression: exp.Create) -> str:
     684        kind = self.sql(expression, "kind").upper()
     685        properties = expression.args.get("properties")
     686        properties_locs = self.locate_properties(properties) if properties else defaultdict()
     687
     688        this = self.createable_sql(expression, properties_locs)
     689
     690        properties_sql = ""
     691        if properties_locs.get(exp.Properties.Location.POST_SCHEMA) or properties_locs.get(
     692            exp.Properties.Location.POST_WITH
     693        ):
     694            properties_sql = self.sql(
     695                exp.Properties(
     696                    expressions=[
     697                        *properties_locs[exp.Properties.Location.POST_SCHEMA],
     698                        *properties_locs[exp.Properties.Location.POST_WITH],
     699                    ]
     700                )
     701            )
     702
     703        begin = " BEGIN" if expression.args.get("begin") else ""
     704        expression_sql = self.sql(expression, "expression")
     705        if expression_sql:
     706            expression_sql = f"{begin}{self.sep()}{expression_sql}"
     707
     708            if self.CREATE_FUNCTION_RETURN_AS or not isinstance(expression.expression, exp.Return):
     709                if properties_locs.get(exp.Properties.Location.POST_ALIAS):
     710                    postalias_props_sql = self.properties(
     711                        exp.Properties(
     712                            expressions=properties_locs[exp.Properties.Location.POST_ALIAS]
     713                        ),
     714                        wrapped=False,
     715                    )
     716                    expression_sql = f" AS {postalias_props_sql}{expression_sql}"
     717                else:
     718                    expression_sql = f" AS{expression_sql}"
     719
     720        postindex_props_sql = ""
     721        if properties_locs.get(exp.Properties.Location.POST_INDEX):
     722            postindex_props_sql = self.properties(
     723                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_INDEX]),
     724                wrapped=False,
     725                prefix=" ",
     726            )
     727
     728        indexes = self.expressions(expression, key="indexes", indent=False, sep=" ")
     729        indexes = f" {indexes}" if indexes else ""
     730        index_sql = indexes + postindex_props_sql
     731
     732        replace = " OR REPLACE" if expression.args.get("replace") else ""
     733        unique = " UNIQUE" if expression.args.get("unique") else ""
     734
     735        postcreate_props_sql = ""
     736        if properties_locs.get(exp.Properties.Location.POST_CREATE):
     737            postcreate_props_sql = self.properties(
     738                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_CREATE]),
     739                sep=" ",
     740                prefix=" ",
     741                wrapped=False,
     742            )
     743
     744        modifiers = "".join((replace, unique, postcreate_props_sql))
     745
     746        postexpression_props_sql = ""
     747        if properties_locs.get(exp.Properties.Location.POST_EXPRESSION):
     748            postexpression_props_sql = self.properties(
     749                exp.Properties(
     750                    expressions=properties_locs[exp.Properties.Location.POST_EXPRESSION]
     751                ),
     752                sep=" ",
     753                prefix=" ",
     754                wrapped=False,
     755            )
     756
     757        exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else ""
     758        no_schema_binding = (
     759            " WITH NO SCHEMA BINDING" if expression.args.get("no_schema_binding") else ""
     760        )
     761
     762        clone = self.sql(expression, "clone")
     763        clone = f" {clone}" if clone else ""
     764
     765        expression_sql = f"CREATE{modifiers} {kind}{exists_sql} {this}{properties_sql}{expression_sql}{postexpression_props_sql}{index_sql}{no_schema_binding}{clone}"
     766        return self.prepend_ctes(expression, expression_sql)
     767
     768    def clone_sql(self, expression: exp.Clone) -> str:
     769        this = self.sql(expression, "this")
     770        when = self.sql(expression, "when")
     771
     772        if when:
     773            kind = self.sql(expression, "kind")
     774            expr = self.sql(expression, "expression")
     775            return f"CLONE {this} {when} ({kind} => {expr})"
     776
     777        return f"CLONE {this}"
     778
     779    def describe_sql(self, expression: exp.Describe) -> str:
     780        return f"DESCRIBE {self.sql(expression, 'this')}"
     781
     782    def prepend_ctes(self, expression: exp.Expression, sql: str) -> str:
     783        with_ = self.sql(expression, "with")
     784        if with_:
     785            sql = f"{with_}{self.sep()}{sql}"
     786        return sql
     787
     788    def with_sql(self, expression: exp.With) -> str:
     789        sql = self.expressions(expression, flat=True)
     790        recursive = "RECURSIVE " if expression.args.get("recursive") else ""
     791
     792        return f"WITH {recursive}{sql}"
     793
     794    def cte_sql(self, expression: exp.CTE) -> str:
     795        alias = self.sql(expression, "alias")
     796        return f"{alias} AS {self.wrap(expression)}"
     797
     798    def tablealias_sql(self, expression: exp.TableAlias) -> str:
     799        alias = self.sql(expression, "this")
     800        columns = self.expressions(expression, key="columns", flat=True)
     801        columns = f"({columns})" if columns else ""
     802        return f"{alias}{columns}"
     803
     804    def bitstring_sql(self, expression: exp.BitString) -> str:
     805        this = self.sql(expression, "this")
     806        if self.BIT_START:
     807            return f"{self.BIT_START}{this}{self.BIT_END}"
     808        return f"{int(this, 2)}"
     809
     810    def hexstring_sql(self, expression: exp.HexString) -> str:
     811        this = self.sql(expression, "this")
     812        if self.HEX_START:
     813            return f"{self.HEX_START}{this}{self.HEX_END}"
     814        return f"{int(this, 16)}"
     815
     816    def bytestring_sql(self, expression: exp.ByteString) -> str:
     817        this = self.sql(expression, "this")
     818        if self.BYTE_START:
     819            return f"{self.BYTE_START}{this}{self.BYTE_END}"
     820        return this
     821
     822    def rawstring_sql(self, expression: exp.RawString) -> str:
     823        string = self.escape_str(expression.this.replace("\\", "\\\\"))
     824        return f"{self.QUOTE_START}{string}{self.QUOTE_END}"
     825
     826    def datatypesize_sql(self, expression: exp.DataTypeSize) -> str:
     827        this = self.sql(expression, "this")
     828        specifier = self.sql(expression, "expression")
     829        specifier = f" {specifier}" if specifier else ""
     830        return f"{this}{specifier}"
     831
     832    def datatype_sql(self, expression: exp.DataType) -> str:
     833        type_value = expression.this
     834        type_sql = (
     835            self.TYPE_MAPPING.get(type_value, type_value.value)
     836            if isinstance(type_value, exp.DataType.Type)
     837            else type_value
     838        )
     839        nested = ""
     840        interior = self.expressions(expression, flat=True)
     841        values = ""
     842        if interior:
     843            if expression.args.get("nested"):
     844                nested = f"{self.STRUCT_DELIMITER[0]}{interior}{self.STRUCT_DELIMITER[1]}"
     845                if expression.args.get("values") is not None:
     846                    delimiters = ("[", "]") if type_value == exp.DataType.Type.ARRAY else ("(", ")")
     847                    values = self.expressions(expression, key="values", flat=True)
     848                    values = f"{delimiters[0]}{values}{delimiters[1]}"
     849            else:
     850                nested = f"({interior})"
     851
     852        return f"{type_sql}{nested}{values}"
     853
     854    def directory_sql(self, expression: exp.Directory) -> str:
     855        local = "LOCAL " if expression.args.get("local") else ""
     856        row_format = self.sql(expression, "row_format")
     857        row_format = f" {row_format}" if row_format else ""
     858        return f"{local}DIRECTORY {self.sql(expression, 'this')}{row_format}"
     859
     860    def delete_sql(self, expression: exp.Delete) -> str:
     861        this = self.sql(expression, "this")
     862        this = f" FROM {this}" if this else ""
     863        using = self.sql(expression, "using")
     864        using = f" USING {using}" if using else ""
     865        where = self.sql(expression, "where")
     866        returning = self.sql(expression, "returning")
     867        limit = self.sql(expression, "limit")
     868        tables = self.expressions(expression, key="tables")
     869        tables = f" {tables}" if tables else ""
     870        if self.RETURNING_END:
     871            expression_sql = f"{this}{using}{where}{returning}{limit}"
     872        else:
     873            expression_sql = f"{returning}{this}{using}{where}{limit}"
     874        return self.prepend_ctes(expression, f"DELETE{tables}{expression_sql}")
     875
     876    def drop_sql(self, expression: exp.Drop) -> str:
     877        this = self.sql(expression, "this")
     878        kind = expression.args["kind"]
     879        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
     880        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
     881        materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
     882        cascade = " CASCADE" if expression.args.get("cascade") else ""
     883        constraints = " CONSTRAINTS" if expression.args.get("constraints") else ""
     884        purge = " PURGE" if expression.args.get("purge") else ""
     885        return (
     886            f"DROP{temporary}{materialized} {kind}{exists_sql}{this}{cascade}{constraints}{purge}"
     887        )
     888
     889    def except_sql(self, expression: exp.Except) -> str:
     890        return self.prepend_ctes(
     891            expression,
     892            self.set_operation(expression, self.except_op(expression)),
     893        )
     894
     895    def except_op(self, expression: exp.Except) -> str:
     896        return f"EXCEPT{'' if expression.args.get('distinct') else ' ALL'}"
     897
     898    def fetch_sql(self, expression: exp.Fetch) -> str:
     899        direction = expression.args.get("direction")
     900        direction = f" {direction.upper()}" if direction else ""
     901        count = expression.args.get("count")
     902        count = f" {count}" if count else ""
     903        if expression.args.get("percent"):
     904            count = f"{count} PERCENT"
     905        with_ties_or_only = "WITH TIES" if expression.args.get("with_ties") else "ONLY"
     906        return f"{self.seg('FETCH')}{direction}{count} ROWS {with_ties_or_only}"
     907
     908    def filter_sql(self, expression: exp.Filter) -> str:
     909        this = self.sql(expression, "this")
     910        where = self.sql(expression, "expression")[1:]  # where has a leading space
     911        return f"{this} FILTER({where})"
     912
     913    def hint_sql(self, expression: exp.Hint) -> str:
     914        if not self.QUERY_HINTS:
     915            self.unsupported("Hints are not supported")
     916            return ""
     917
     918        return f" /*+ {self.expressions(expression, sep=self.QUERY_HINT_SEP).strip()} */"
     919
     920    def index_sql(self, expression: exp.Index) -> str:
     921        unique = "UNIQUE " if expression.args.get("unique") else ""
     922        primary = "PRIMARY " if expression.args.get("primary") else ""
     923        amp = "AMP " if expression.args.get("amp") else ""
     924        name = self.sql(expression, "this")
     925        name = f"{name} " if name else ""
     926        table = self.sql(expression, "table")
     927        table = f"{self.INDEX_ON} {table} " if table else ""
     928        using = self.sql(expression, "using")
     929        using = f"USING {using} " if using else ""
     930        index = "INDEX " if not table else ""
     931        columns = self.expressions(expression, key="columns", flat=True)
     932        columns = f"({columns})" if columns else ""
     933        partition_by = self.expressions(expression, key="partition_by", flat=True)
     934        partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
     935        return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}"
     936
     937    def identifier_sql(self, expression: exp.Identifier) -> str:
     938        text = expression.name
     939        lower = text.lower()
     940        text = lower if self.normalize and not expression.quoted else text
     941        text = text.replace(self.IDENTIFIER_END, self._escaped_identifier_end)
     942        if (
     943            expression.quoted
     944            or self.can_identify(text, self.identify)
     945            or lower in self.RESERVED_KEYWORDS
     946            or (not self.IDENTIFIERS_CAN_START_WITH_DIGIT and text[:1].isdigit())
     947        ):
     948            text = f"{self.IDENTIFIER_START}{text}{self.IDENTIFIER_END}"
     949        return text
     950
     951    def inputoutputformat_sql(self, expression: exp.InputOutputFormat) -> str:
     952        input_format = self.sql(expression, "input_format")
     953        input_format = f"INPUTFORMAT {input_format}" if input_format else ""
     954        output_format = self.sql(expression, "output_format")
     955        output_format = f"OUTPUTFORMAT {output_format}" if output_format else ""
     956        return self.sep().join((input_format, output_format))
     957
     958    def national_sql(self, expression: exp.National, prefix: str = "N") -> str:
     959        string = self.sql(exp.Literal.string(expression.name))
     960        return f"{prefix}{string}"
     961
     962    def partition_sql(self, expression: exp.Partition) -> str:
     963        return f"PARTITION({self.expressions(expression, flat=True)})"
     964
     965    def properties_sql(self, expression: exp.Properties) -> str:
     966        root_properties = []
     967        with_properties = []
     968
     969        for p in expression.expressions:
     970            p_loc = self.PROPERTIES_LOCATION[p.__class__]
     971            if p_loc == exp.Properties.Location.POST_WITH:
     972                with_properties.append(p.copy())
     973            elif p_loc == exp.Properties.Location.POST_SCHEMA:
     974                root_properties.append(p.copy())
     975
     976        return self.root_properties(
     977            exp.Properties(expressions=root_properties)
     978        ) + self.with_properties(exp.Properties(expressions=with_properties))
     979
     980    def root_properties(self, properties: exp.Properties) -> str:
     981        if properties.expressions:
     982            return self.sep() + self.expressions(properties, indent=False, sep=" ")
     983        return ""
     984
     985    def properties(
     986        self,
     987        properties: exp.Properties,
     988        prefix: str = "",
     989        sep: str = ", ",
     990        suffix: str = "",
     991        wrapped: bool = True,
     992    ) -> str:
     993        if properties.expressions:
     994            expressions = self.expressions(properties, sep=sep, indent=False)
     995            if expressions:
     996                expressions = self.wrap(expressions) if wrapped else expressions
     997                return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
     998        return ""
     999
    1000    def with_properties(self, properties: exp.Properties) -> str:
    1001        return self.properties(properties, prefix=self.seg("WITH"))
    1002
    1003    def locate_properties(self, properties: exp.Properties) -> t.DefaultDict:
    1004        properties_locs = defaultdict(list)
    1005        for p in properties.expressions:
    1006            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    1007            if p_loc != exp.Properties.Location.UNSUPPORTED:
    1008                properties_locs[p_loc].append(p.copy())
    1009            else:
    1010                self.unsupported(f"Unsupported property {p.key}")
    1011
    1012        return properties_locs
    1013
    1014    def property_sql(self, expression: exp.Property) -> str:
    1015        property_cls = expression.__class__
    1016        if property_cls == exp.Property:
    1017            return f"{expression.name}={self.sql(expression, 'value')}"
    1018
    1019        property_name = exp.Properties.PROPERTY_TO_NAME.get(property_cls)
    1020        if not property_name:
    1021            self.unsupported(f"Unsupported property {expression.key}")
    1022
    1023        return f"{property_name}={self.sql(expression, 'this')}"
    1024
    1025    def likeproperty_sql(self, expression: exp.LikeProperty) -> str:
    1026        options = " ".join(f"{e.name} {self.sql(e, 'value')}" for e in expression.expressions)
    1027        options = f" {options}" if options else ""
    1028        return f"LIKE {self.sql(expression, 'this')}{options}"
    1029
    1030    def fallbackproperty_sql(self, expression: exp.FallbackProperty) -> str:
    1031        no = "NO " if expression.args.get("no") else ""
    1032        protection = " PROTECTION" if expression.args.get("protection") else ""
    1033        return f"{no}FALLBACK{protection}"
    1034
    1035    def journalproperty_sql(self, expression: exp.JournalProperty) -> str:
    1036        no = "NO " if expression.args.get("no") else ""
    1037        local = expression.args.get("local")
    1038        local = f"{local} " if local else ""
    1039        dual = "DUAL " if expression.args.get("dual") else ""
    1040        before = "BEFORE " if expression.args.get("before") else ""
    1041        after = "AFTER " if expression.args.get("after") else ""
    1042        return f"{no}{local}{dual}{before}{after}JOURNAL"
    1043
    1044    def freespaceproperty_sql(self, expression: exp.FreespaceProperty) -> str:
    1045        freespace = self.sql(expression, "this")
    1046        percent = " PERCENT" if expression.args.get("percent") else ""
    1047        return f"FREESPACE={freespace}{percent}"
    1048
    1049    def checksumproperty_sql(self, expression: exp.ChecksumProperty) -> str:
    1050        if expression.args.get("default"):
    1051            property = "DEFAULT"
    1052        elif expression.args.get("on"):
    1053            property = "ON"
    1054        else:
    1055            property = "OFF"
    1056        return f"CHECKSUM={property}"
    1057
    1058    def mergeblockratioproperty_sql(self, expression: exp.MergeBlockRatioProperty) -> str:
    1059        if expression.args.get("no"):
    1060            return "NO MERGEBLOCKRATIO"
    1061        if expression.args.get("default"):
    1062            return "DEFAULT MERGEBLOCKRATIO"
    1063
    1064        percent = " PERCENT" if expression.args.get("percent") else ""
    1065        return f"MERGEBLOCKRATIO={self.sql(expression, 'this')}{percent}"
    1066
    1067    def datablocksizeproperty_sql(self, expression: exp.DataBlocksizeProperty) -> str:
    1068        default = expression.args.get("default")
    1069        minimum = expression.args.get("minimum")
    1070        maximum = expression.args.get("maximum")
    1071        if default or minimum or maximum:
    1072            if default:
    1073                prop = "DEFAULT"
    1074            elif minimum:
    1075                prop = "MINIMUM"
    1076            else:
    1077                prop = "MAXIMUM"
    1078            return f"{prop} DATABLOCKSIZE"
    1079        units = expression.args.get("units")
    1080        units = f" {units}" if units else ""
    1081        return f"DATABLOCKSIZE={self.sql(expression, 'size')}{units}"
    1082
    1083    def blockcompressionproperty_sql(self, expression: exp.BlockCompressionProperty) -> str:
    1084        autotemp = expression.args.get("autotemp")
    1085        always = expression.args.get("always")
    1086        default = expression.args.get("default")
    1087        manual = expression.args.get("manual")
    1088        never = expression.args.get("never")
    1089
    1090        if autotemp is not None:
    1091            prop = f"AUTOTEMP({self.expressions(autotemp)})"
    1092        elif always:
    1093            prop = "ALWAYS"
    1094        elif default:
    1095            prop = "DEFAULT"
    1096        elif manual:
    1097            prop = "MANUAL"
    1098        elif never:
    1099            prop = "NEVER"
    1100        return f"BLOCKCOMPRESSION={prop}"
    1101
    1102    def isolatedloadingproperty_sql(self, expression: exp.IsolatedLoadingProperty) -> str:
    1103        no = expression.args.get("no")
    1104        no = " NO" if no else ""
    1105        concurrent = expression.args.get("concurrent")
    1106        concurrent = " CONCURRENT" if concurrent else ""
    1107
    1108        for_ = ""
    1109        if expression.args.get("for_all"):
    1110            for_ = " FOR ALL"
    1111        elif expression.args.get("for_insert"):
    1112            for_ = " FOR INSERT"
    1113        elif expression.args.get("for_none"):
    1114            for_ = " FOR NONE"
    1115        return f"WITH{no}{concurrent} ISOLATED LOADING{for_}"
    1116
    1117    def lockingproperty_sql(self, expression: exp.LockingProperty) -> str:
    1118        kind = expression.args.get("kind")
    1119        this = f" {self.sql(expression, 'this')}" if expression.this else ""
    1120        for_or_in = expression.args.get("for_or_in")
    1121        lock_type = expression.args.get("lock_type")
    1122        override = " OVERRIDE" if expression.args.get("override") else ""
    1123        return f"LOCKING {kind}{this} {for_or_in} {lock_type}{override}"
    1124
    1125    def withdataproperty_sql(self, expression: exp.WithDataProperty) -> str:
    1126        data_sql = f"WITH {'NO ' if expression.args.get('no') else ''}DATA"
    1127        statistics = expression.args.get("statistics")
    1128        statistics_sql = ""
    1129        if statistics is not None:
    1130            statistics_sql = f" AND {'NO ' if not statistics else ''}STATISTICS"
    1131        return f"{data_sql}{statistics_sql}"
    1132
    1133    def insert_sql(self, expression: exp.Insert) -> str:
    1134        overwrite = expression.args.get("overwrite")
    1135
    1136        if isinstance(expression.this, exp.Directory):
    1137            this = " OVERWRITE" if overwrite else " INTO"
    1138        else:
    1139            this = " OVERWRITE TABLE" if overwrite else " INTO"
    1140
    1141        alternative = expression.args.get("alternative")
    1142        alternative = f" OR {alternative}" if alternative else ""
    1143        ignore = " IGNORE" if expression.args.get("ignore") else ""
    1144
    1145        this = f"{this} {self.sql(expression, 'this')}"
    1146
    1147        exists = " IF EXISTS" if expression.args.get("exists") else ""
    1148        partition_sql = (
    1149            f" {self.sql(expression, 'partition')}" if expression.args.get("partition") else ""
    1150        )
    1151        where = self.sql(expression, "where")
    1152        where = f"{self.sep()}REPLACE WHERE {where}" if where else ""
    1153        expression_sql = f"{self.sep()}{self.sql(expression, 'expression')}"
    1154        conflict = self.sql(expression, "conflict")
    1155        returning = self.sql(expression, "returning")
    1156
    1157        if self.RETURNING_END:
    1158            expression_sql = f"{expression_sql}{conflict}{returning}"
    1159        else:
    1160            expression_sql = f"{returning}{expression_sql}{conflict}"
    1161
    1162        sql = f"INSERT{alternative}{ignore}{this}{exists}{partition_sql}{where}{expression_sql}"
    1163        return self.prepend_ctes(expression, sql)
    1164
    1165    def intersect_sql(self, expression: exp.Intersect) -> str:
    1166        return self.prepend_ctes(
    1167            expression,
    1168            self.set_operation(expression, self.intersect_op(expression)),
    1169        )
    1170
    1171    def intersect_op(self, expression: exp.Intersect) -> str:
    1172        return f"INTERSECT{'' if expression.args.get('distinct') else ' ALL'}"
    1173
    1174    def introducer_sql(self, expression: exp.Introducer) -> str:
    1175        return f"{self.sql(expression, 'this')} {self.sql(expression, 'expression')}"
    1176
    1177    def pseudotype_sql(self, expression: exp.PseudoType) -> str:
    1178        return expression.name.upper()
    1179
    1180    def onconflict_sql(self, expression: exp.OnConflict) -> str:
    1181        conflict = "ON DUPLICATE KEY" if expression.args.get("duplicate") else "ON CONFLICT"
    1182        constraint = self.sql(expression, "constraint")
    1183        if constraint:
    1184            constraint = f"ON CONSTRAINT {constraint}"
    1185        key = self.expressions(expression, key="key", flat=True)
    1186        do = "" if expression.args.get("duplicate") else " DO "
    1187        nothing = "NOTHING" if expression.args.get("nothing") else ""
    1188        expressions = self.expressions(expression, flat=True)
    1189        set_keyword = "SET " if self.DUPLICATE_KEY_UPDATE_WITH_SET else ""
    1190        if expressions:
    1191            expressions = f"UPDATE {set_keyword}{expressions}"
    1192        return f"{self.seg(conflict)} {constraint}{key}{do}{nothing}{expressions}"
    1193
    1194    def returning_sql(self, expression: exp.Returning) -> str:
    1195        return f"{self.seg('RETURNING')} {self.expressions(expression, flat=True)}"
    1196
    1197    def rowformatdelimitedproperty_sql(self, expression: exp.RowFormatDelimitedProperty) -> str:
    1198        fields = expression.args.get("fields")
    1199        fields = f" FIELDS TERMINATED BY {fields}" if fields else ""
    1200        escaped = expression.args.get("escaped")
    1201        escaped = f" ESCAPED BY {escaped}" if escaped else ""
    1202        items = expression.args.get("collection_items")
    1203        items = f" COLLECTION ITEMS TERMINATED BY {items}" if items else ""
    1204        keys = expression.args.get("map_keys")
    1205        keys = f" MAP KEYS TERMINATED BY {keys}" if keys else ""
    1206        lines = expression.args.get("lines")
    1207        lines = f" LINES TERMINATED BY {lines}" if lines else ""
    1208        null = expression.args.get("null")
    1209        null = f" NULL DEFINED AS {null}" if null else ""
    1210        return f"ROW FORMAT DELIMITED{fields}{escaped}{items}{keys}{lines}{null}"
    1211
    1212    def withtablehint_sql(self, expression: exp.WithTableHint) -> str:
    1213        return f"WITH ({self.expressions(expression, flat=True)})"
    1214
    1215    def indextablehint_sql(self, expression: exp.IndexTableHint) -> str:
    1216        this = f"{self.sql(expression, 'this')} INDEX"
    1217        target = self.sql(expression, "target")
    1218        target = f" FOR {target}" if target else ""
    1219        return f"{this}{target} ({self.expressions(expression, flat=True)})"
    1220
    1221    def table_sql(self, expression: exp.Table, sep: str = " AS ") -> str:
    1222        table = ".".join(
    1223            part
    1224            for part in [
    1225                self.sql(expression, "catalog"),
    1226                self.sql(expression, "db"),
    1227                self.sql(expression, "this"),
    1228            ]
    1229            if part
    1230        )
    1231
    1232        alias = self.sql(expression, "alias")
    1233        alias = f"{sep}{alias}" if alias else ""
    1234        hints = self.expressions(expression, key="hints", sep=" ")
    1235        hints = f" {hints}" if hints and self.TABLE_HINTS else ""
    1236        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1237        pivots = f" {pivots}" if pivots else ""
    1238        joins = self.expressions(expression, key="joins", sep="", skip_first=True)
    1239        laterals = self.expressions(expression, key="laterals", sep="")
    1240        system_time = expression.args.get("system_time")
    1241        system_time = f" {self.sql(expression, 'system_time')}" if system_time else ""
    1242
    1243        return f"{table}{system_time}{alias}{hints}{pivots}{joins}{laterals}"
    1244
    1245    def tablesample_sql(
    1246        self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    1247    ) -> str:
    1248        if self.ALIAS_POST_TABLESAMPLE and expression.this.alias:
    1249            table = expression.this.copy()
    1250            table.set("alias", None)
    1251            this = self.sql(table)
    1252            alias = f"{sep}{self.sql(expression.this, 'alias')}"
    1253        else:
    1254            this = self.sql(expression, "this")
    1255            alias = ""
    1256        method = self.sql(expression, "method")
    1257        method = f"{method.upper()} " if method and self.TABLESAMPLE_WITH_METHOD else ""
    1258        numerator = self.sql(expression, "bucket_numerator")
    1259        denominator = self.sql(expression, "bucket_denominator")
    1260        field = self.sql(expression, "bucket_field")
    1261        field = f" ON {field}" if field else ""
    1262        bucket = f"BUCKET {numerator} OUT OF {denominator}{field}" if numerator else ""
    1263        percent = self.sql(expression, "percent")
    1264        percent = f"{percent} PERCENT" if percent else ""
    1265        rows = self.sql(expression, "rows")
    1266        rows = f"{rows} ROWS" if rows else ""
    1267        size = self.sql(expression, "size")
    1268        if size and self.TABLESAMPLE_SIZE_IS_PERCENT:
    1269            size = f"{size} PERCENT"
    1270        seed = self.sql(expression, "seed")
    1271        seed = f" {seed_prefix} ({seed})" if seed else ""
    1272        kind = expression.args.get("kind", "TABLESAMPLE")
    1273        return f"{this} {kind} {method}({bucket}{percent}{rows}{size}){seed}{alias}"
    1274
    1275    def pivot_sql(self, expression: exp.Pivot) -> str:
    1276        expressions = self.expressions(expression, flat=True)
    1277
    1278        if expression.this:
    1279            this = self.sql(expression, "this")
    1280            on = f"{self.seg('ON')} {expressions}"
    1281            using = self.expressions(expression, key="using", flat=True)
    1282            using = f"{self.seg('USING')} {using}" if using else ""
    1283            group = self.sql(expression, "group")
    1284            return f"PIVOT {this}{on}{using}{group}"
    1285
    1286        alias = self.sql(expression, "alias")
    1287        alias = f" AS {alias}" if alias else ""
    1288        unpivot = expression.args.get("unpivot")
    1289        direction = "UNPIVOT" if unpivot else "PIVOT"
    1290        field = self.sql(expression, "field")
    1291        return f"{direction}({expressions} FOR {field}){alias}"
    1292
    1293    def tuple_sql(self, expression: exp.Tuple) -> str:
    1294        return f"({self.expressions(expression, flat=True)})"
    1295
    1296    def update_sql(self, expression: exp.Update) -> str:
    1297        this = self.sql(expression, "this")
    1298        set_sql = self.expressions(expression, flat=True)
    1299        from_sql = self.sql(expression, "from")
    1300        where_sql = self.sql(expression, "where")
    1301        returning = self.sql(expression, "returning")
    1302        limit = self.sql(expression, "limit")
    1303        if self.RETURNING_END:
    1304            expression_sql = f"{from_sql}{where_sql}{returning}{limit}"
    1305        else:
    1306            expression_sql = f"{returning}{from_sql}{where_sql}{limit}"
    1307        sql = f"UPDATE {this} SET {set_sql}{expression_sql}"
    1308        return self.prepend_ctes(expression, sql)
    1309
    1310    def values_sql(self, expression: exp.Values) -> str:
    1311        # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example
    1312        if self.VALUES_AS_TABLE or not expression.find_ancestor(exp.From, exp.Join):
    1313            args = self.expressions(expression)
    1314            alias = self.sql(expression, "alias")
    1315            values = f"VALUES{self.seg('')}{args}"
    1316            values = (
    1317                f"({values})"
    1318                if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
    1319                else values
    1320            )
    1321            return f"{values} AS {alias}" if alias else values
    1322
    1323        # Converts `VALUES...` expression into a series of select unions.
    1324        # Note: If you have a lot of unions then this will result in a large number of recursive statements to
    1325        # evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be
    1326        # very slow.
    1327        expression = expression.copy()
    1328        column_names = expression.alias and expression.args["alias"].columns
    1329
    1330        selects = []
    1331
    1332        for i, tup in enumerate(expression.expressions):
    1333            row = tup.expressions
    1334
    1335            if i == 0 and column_names:
    1336                row = [
    1337                    exp.alias_(value, column_name) for value, column_name in zip(row, column_names)
    1338                ]
    1339
    1340            selects.append(exp.Select(expressions=row))
    1341
    1342        subquery_expression: exp.Select | exp.Union = selects[0]
    1343        if len(selects) > 1:
    1344            for select in selects[1:]:
    1345                subquery_expression = exp.union(
    1346                    subquery_expression, select, distinct=False, copy=False
    1347                )
    1348
    1349        return self.subquery_sql(subquery_expression.subquery(expression.alias, copy=False))
    1350
    1351    def var_sql(self, expression: exp.Var) -> str:
    1352        return self.sql(expression, "this")
    1353
    1354    def into_sql(self, expression: exp.Into) -> str:
    1355        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    1356        unlogged = " UNLOGGED" if expression.args.get("unlogged") else ""
    1357        return f"{self.seg('INTO')}{temporary or unlogged} {self.sql(expression, 'this')}"
    1358
    1359    def from_sql(self, expression: exp.From) -> str:
    1360        return f"{self.seg('FROM')} {self.sql(expression, 'this')}"
    1361
    1362    def group_sql(self, expression: exp.Group) -> str:
    1363        group_by = self.op_expressions("GROUP BY", expression)
    1364
    1365        if expression.args.get("all"):
    1366            return f"{group_by} ALL"
    1367
    1368        grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
    1369        grouping_sets = (
    1370            f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
    1371        )
    1372
    1373        cube = expression.args.get("cube", [])
    1374        if seq_get(cube, 0) is True:
    1375            return f"{group_by}{self.seg('WITH CUBE')}"
    1376        else:
    1377            cube_sql = self.expressions(expression, key="cube", indent=False)
    1378            cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else ""
    1379
    1380        rollup = expression.args.get("rollup", [])
    1381        if seq_get(rollup, 0) is True:
    1382            return f"{group_by}{self.seg('WITH ROLLUP')}"
    1383        else:
    1384            rollup_sql = self.expressions(expression, key="rollup", indent=False)
    1385            rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else ""
    1386
    1387        groupings = csv(
    1388            grouping_sets,
    1389            cube_sql,
    1390            rollup_sql,
    1391            self.seg("WITH TOTALS") if expression.args.get("totals") else "",
    1392            sep=self.GROUPINGS_SEP,
    1393        )
    1394
    1395        if expression.args.get("expressions") and groupings:
    1396            group_by = f"{group_by}{self.GROUPINGS_SEP}"
    1397
    1398        return f"{group_by}{groupings}"
    1399
    1400    def having_sql(self, expression: exp.Having) -> str:
    1401        this = self.indent(self.sql(expression, "this"))
    1402        return f"{self.seg('HAVING')}{self.sep()}{this}"
    1403
    1404    def join_sql(self, expression: exp.Join) -> str:
    1405        op_sql = " ".join(
    1406            op
    1407            for op in (
    1408                expression.method,
    1409                "GLOBAL" if expression.args.get("global") else None,
    1410                expression.side,
    1411                expression.kind,
    1412                expression.hint if self.JOIN_HINTS else None,
    1413            )
    1414            if op
    1415        )
    1416        on_sql = self.sql(expression, "on")
    1417        using = expression.args.get("using")
    1418
    1419        if not on_sql and using:
    1420            on_sql = csv(*(self.sql(column) for column in using))
    1421
    1422        this_sql = self.sql(expression, "this")
    1423
    1424        if on_sql:
    1425            on_sql = self.indent(on_sql, skip_first=True)
    1426            space = self.seg(" " * self.pad) if self.pretty else " "
    1427            if using:
    1428                on_sql = f"{space}USING ({on_sql})"
    1429            else:
    1430                on_sql = f"{space}ON {on_sql}"
    1431        elif not op_sql:
    1432            return f", {this_sql}"
    1433
    1434        op_sql = f"{op_sql} JOIN" if op_sql else "JOIN"
    1435        return f"{self.seg(op_sql)} {this_sql}{on_sql}"
    1436
    1437    def lambda_sql(self, expression: exp.Lambda, arrow_sep: str = "->") -> str:
    1438        args = self.expressions(expression, flat=True)
    1439        args = f"({args})" if len(args.split(",")) > 1 else args
    1440        return f"{args} {arrow_sep} {self.sql(expression, 'this')}"
    1441
    1442    def lateral_sql(self, expression: exp.Lateral) -> str:
    1443        this = self.sql(expression, "this")
    1444
    1445        if isinstance(expression.this, exp.Subquery):
    1446            return f"LATERAL {this}"
    1447
    1448        if expression.args.get("view"):
    1449            alias = expression.args["alias"]
    1450            columns = self.expressions(alias, key="columns", flat=True)
    1451            table = f" {alias.name}" if alias.name else ""
    1452            columns = f" AS {columns}" if columns else ""
    1453            op_sql = self.seg(f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}")
    1454            return f"{op_sql}{self.sep()}{this}{table}{columns}"
    1455
    1456        alias = self.sql(expression, "alias")
    1457        alias = f" AS {alias}" if alias else ""
    1458        return f"LATERAL {this}{alias}"
    1459
    1460    def limit_sql(self, expression: exp.Limit, top: bool = False) -> str:
    1461        this = self.sql(expression, "this")
    1462        args = ", ".join(
    1463            sql
    1464            for sql in (
    1465                self.sql(expression, "offset"),
    1466                self.sql(expression, "expression"),
    1467            )
    1468            if sql
    1469        )
    1470        return f"{this}{self.seg('TOP' if top else 'LIMIT')} {args}"
    1471
    1472    def offset_sql(self, expression: exp.Offset) -> str:
    1473        this = self.sql(expression, "this")
    1474        return f"{this}{self.seg('OFFSET')} {self.sql(expression, 'expression')}"
    1475
    1476    def setitem_sql(self, expression: exp.SetItem) -> str:
    1477        kind = self.sql(expression, "kind")
    1478        kind = f"{kind} " if kind else ""
    1479        this = self.sql(expression, "this")
    1480        expressions = self.expressions(expression)
    1481        collate = self.sql(expression, "collate")
    1482        collate = f" COLLATE {collate}" if collate else ""
    1483        global_ = "GLOBAL " if expression.args.get("global") else ""
    1484        return f"{global_}{kind}{this}{expressions}{collate}"
    1485
    1486    def set_sql(self, expression: exp.Set) -> str:
    1487        expressions = (
    1488            f" {self.expressions(expression, flat=True)}" if expression.expressions else ""
    1489        )
    1490        tag = " TAG" if expression.args.get("tag") else ""
    1491        return f"{'UNSET' if expression.args.get('unset') else 'SET'}{tag}{expressions}"
    1492
    1493    def pragma_sql(self, expression: exp.Pragma) -> str:
    1494        return f"PRAGMA {self.sql(expression, 'this')}"
    1495
    1496    def lock_sql(self, expression: exp.Lock) -> str:
    1497        if not self.LOCKING_READS_SUPPORTED:
    1498            self.unsupported("Locking reads using 'FOR UPDATE/SHARE' are not supported")
    1499            return ""
    1500
    1501        lock_type = "FOR UPDATE" if expression.args["update"] else "FOR SHARE"
    1502        expressions = self.expressions(expression, flat=True)
    1503        expressions = f" OF {expressions}" if expressions else ""
    1504        wait = expression.args.get("wait")
    1505
    1506        if wait is not None:
    1507            if isinstance(wait, exp.Literal):
    1508                wait = f" WAIT {self.sql(wait)}"
    1509            else:
    1510                wait = " NOWAIT" if wait else " SKIP LOCKED"
    1511
    1512        return f"{lock_type}{expressions}{wait or ''}"
    1513
    1514    def literal_sql(self, expression: exp.Literal) -> str:
    1515        text = expression.this or ""
    1516        if expression.is_string:
    1517            text = f"{self.QUOTE_START}{self.escape_str(text)}{self.QUOTE_END}"
    1518        return text
    1519
    1520    def escape_str(self, text: str) -> str:
    1521        text = text.replace(self.QUOTE_END, self._escaped_quote_end)
    1522        if self.ESCAPE_LINE_BREAK:
    1523            text = text.replace("\n", "\\n")
    1524        elif self.pretty:
    1525            text = text.replace("\n", self.SENTINEL_LINE_BREAK)
    1526        return text
    1527
    1528    def loaddata_sql(self, expression: exp.LoadData) -> str:
    1529        local = " LOCAL" if expression.args.get("local") else ""
    1530        inpath = f" INPATH {self.sql(expression, 'inpath')}"
    1531        overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
    1532        this = f" INTO TABLE {self.sql(expression, 'this')}"
    1533        partition = self.sql(expression, "partition")
    1534        partition = f" {partition}" if partition else ""
    1535        input_format = self.sql(expression, "input_format")
    1536        input_format = f" INPUTFORMAT {input_format}" if input_format else ""
    1537        serde = self.sql(expression, "serde")
    1538        serde = f" SERDE {serde}" if serde else ""
    1539        return f"LOAD DATA{local}{inpath}{overwrite}{this}{partition}{input_format}{serde}"
    1540
    1541    def null_sql(self, *_) -> str:
    1542        return "NULL"
    1543
    1544    def boolean_sql(self, expression: exp.Boolean) -> str:
    1545        return "TRUE" if expression.this else "FALSE"
    1546
    1547    def order_sql(self, expression: exp.Order, flat: bool = False) -> str:
    1548        this = self.sql(expression, "this")
    1549        this = f"{this} " if this else this
    1550        return self.op_expressions(f"{this}ORDER BY", expression, flat=this or flat)  # type: ignore
    1551
    1552    def cluster_sql(self, expression: exp.Cluster) -> str:
    1553        return self.op_expressions("CLUSTER BY", expression)
    1554
    1555    def distribute_sql(self, expression: exp.Distribute) -> str:
    1556        return self.op_expressions("DISTRIBUTE BY", expression)
    1557
    1558    def sort_sql(self, expression: exp.Sort) -> str:
    1559        return self.op_expressions("SORT BY", expression)
    1560
    1561    def ordered_sql(self, expression: exp.Ordered) -> str:
    1562        desc = expression.args.get("desc")
    1563        asc = not desc
    1564
    1565        nulls_first = expression.args.get("nulls_first")
    1566        nulls_last = not nulls_first
    1567        nulls_are_large = self.NULL_ORDERING == "nulls_are_large"
    1568        nulls_are_small = self.NULL_ORDERING == "nulls_are_small"
    1569        nulls_are_last = self.NULL_ORDERING == "nulls_are_last"
    1570
    1571        sort_order = " DESC" if desc else ""
    1572        nulls_sort_change = ""
    1573        if nulls_first and (
    1574            (asc and nulls_are_large) or (desc and nulls_are_small) or nulls_are_last
    1575        ):
    1576            nulls_sort_change = " NULLS FIRST"
    1577        elif (
    1578            nulls_last
    1579            and ((asc and nulls_are_small) or (desc and nulls_are_large))
    1580            and not nulls_are_last
    1581        ):
    1582            nulls_sort_change = " NULLS LAST"
    1583
    1584        if nulls_sort_change and not self.NULL_ORDERING_SUPPORTED:
    1585            self.unsupported(
    1586                "Sorting in an ORDER BY on NULLS FIRST/NULLS LAST is not supported by this dialect"
    1587            )
    1588            nulls_sort_change = ""
    1589
    1590        return f"{self.sql(expression, 'this')}{sort_order}{nulls_sort_change}"
    1591
    1592    def matchrecognize_sql(self, expression: exp.MatchRecognize) -> str:
    1593        partition = self.partition_by_sql(expression)
    1594        order = self.sql(expression, "order")
    1595        measures = self.expressions(expression, key="measures")
    1596        measures = self.seg(f"MEASURES{self.seg(measures)}") if measures else ""
    1597        rows = self.sql(expression, "rows")
    1598        rows = self.seg(rows) if rows else ""
    1599        after = self.sql(expression, "after")
    1600        after = self.seg(after) if after else ""
    1601        pattern = self.sql(expression, "pattern")
    1602        pattern = self.seg(f"PATTERN ({pattern})") if pattern else ""
    1603        definition_sqls = [
    1604            f"{self.sql(definition, 'alias')} AS {self.sql(definition, 'this')}"
    1605            for definition in expression.args.get("define", [])
    1606        ]
    1607        definitions = self.expressions(sqls=definition_sqls)
    1608        define = self.seg(f"DEFINE{self.seg(definitions)}") if definitions else ""
    1609        body = "".join(
    1610            (
    1611                partition,
    1612                order,
    1613                measures,
    1614                rows,
    1615                after,
    1616                pattern,
    1617                define,
    1618            )
    1619        )
    1620        alias = self.sql(expression, "alias")
    1621        alias = f" {alias}" if alias else ""
    1622        return f"{self.seg('MATCH_RECOGNIZE')} {self.wrap(body)}{alias}"
    1623
    1624    def query_modifiers(self, expression: exp.Expression, *sqls: str) -> str:
    1625        limit: t.Optional[exp.Fetch | exp.Limit] = expression.args.get("limit")
    1626
    1627        # If the limit is generated as TOP, we need to ensure it's not generated twice
    1628        with_offset_limit_modifiers = not isinstance(limit, exp.Limit) or not self.LIMIT_IS_TOP
    1629
    1630        if self.LIMIT_FETCH == "LIMIT" and isinstance(limit, exp.Fetch):
    1631            limit = exp.Limit(expression=exp.maybe_copy(limit.args.get("count")))
    1632        elif self.LIMIT_FETCH == "FETCH" and isinstance(limit, exp.Limit):
    1633            limit = exp.Fetch(direction="FIRST", count=exp.maybe_copy(limit.expression))
    1634
    1635        fetch = isinstance(limit, exp.Fetch)
    1636
    1637        offset_limit_modifiers = (
    1638            self.offset_limit_modifiers(expression, fetch, limit)
    1639            if with_offset_limit_modifiers
    1640            else []
    1641        )
    1642
    1643        return csv(
    1644            *sqls,
    1645            *[self.sql(join) for join in expression.args.get("joins") or []],
    1646            self.sql(expression, "match"),
    1647            *[self.sql(lateral) for lateral in expression.args.get("laterals") or []],
    1648            self.sql(expression, "where"),
    1649            self.sql(expression, "group"),
    1650            self.sql(expression, "having"),
    1651            *self.after_having_modifiers(expression),
    1652            self.sql(expression, "order"),
    1653            *offset_limit_modifiers,
    1654            *self.after_limit_modifiers(expression),
    1655            sep="",
    1656        )
    1657
    1658    def offset_limit_modifiers(
    1659        self, expression: exp.Expression, fetch: bool, limit: t.Optional[exp.Fetch | exp.Limit]
    1660    ) -> t.List[str]:
    1661        return [
    1662            self.sql(expression, "offset") if fetch else self.sql(limit),
    1663            self.sql(limit) if fetch else self.sql(expression, "offset"),
    1664        ]
    1665
    1666    def after_having_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1667        return [
    1668            self.sql(expression, "qualify"),
    1669            self.seg("WINDOW ") + self.expressions(expression, key="windows", flat=True)
    1670            if expression.args.get("windows")
    1671            else "",
    1672            self.sql(expression, "distribute"),
    1673            self.sql(expression, "sort"),
    1674            self.sql(expression, "cluster"),
    1675        ]
    1676
    1677    def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1678        locks = self.expressions(expression, key="locks", sep=" ")
    1679        locks = f" {locks}" if locks else ""
    1680        return [locks, self.sql(expression, "sample")]
    1681
    1682    def select_sql(self, expression: exp.Select) -> str:
    1683        hint = self.sql(expression, "hint")
    1684        distinct = self.sql(expression, "distinct")
    1685        distinct = f" {distinct}" if distinct else ""
    1686        kind = self.sql(expression, "kind").upper()
    1687        limit = expression.args.get("limit")
    1688        top = (
    1689            self.limit_sql(limit, top=True)
    1690            if isinstance(limit, exp.Limit) and self.LIMIT_IS_TOP
    1691            else ""
    1692        )
    1693
    1694        expressions = self.expressions(expression)
    1695
    1696        if kind:
    1697            if kind in self.SELECT_KINDS:
    1698                kind = f" AS {kind}"
    1699            else:
    1700                if kind == "STRUCT":
    1701                    expressions = self.expressions(
    1702                        sqls=[
    1703                            self.sql(
    1704                                exp.Struct(
    1705                                    expressions=[
    1706                                        exp.column(e.output_name).eq(
    1707                                            e.this if isinstance(e, exp.Alias) else e
    1708                                        )
    1709                                        for e in expression.expressions
    1710                                    ]
    1711                                )
    1712                            )
    1713                        ]
    1714                    )
    1715                kind = ""
    1716
    1717        expressions = f"{self.sep()}{expressions}" if expressions else expressions
    1718        sql = self.query_modifiers(
    1719            expression,
    1720            f"SELECT{top}{hint}{distinct}{kind}{expressions}",
    1721            self.sql(expression, "into", comment=False),
    1722            self.sql(expression, "from", comment=False),
    1723        )
    1724        return self.prepend_ctes(expression, sql)
    1725
    1726    def schema_sql(self, expression: exp.Schema) -> str:
    1727        this = self.sql(expression, "this")
    1728        this = f"{this} " if this else ""
    1729        sql = self.schema_columns_sql(expression)
    1730        return f"{this}{sql}"
    1731
    1732    def schema_columns_sql(self, expression: exp.Schema) -> str:
    1733        return f"({self.sep('')}{self.expressions(expression)}{self.seg(')', sep='')}"
    1734
    1735    def star_sql(self, expression: exp.Star) -> str:
    1736        except_ = self.expressions(expression, key="except", flat=True)
    1737        except_ = f"{self.seg(self.STAR_MAPPING['except'])} ({except_})" if except_ else ""
    1738        replace = self.expressions(expression, key="replace", flat=True)
    1739        replace = f"{self.seg(self.STAR_MAPPING['replace'])} ({replace})" if replace else ""
    1740        return f"*{except_}{replace}"
    1741
    1742    def parameter_sql(self, expression: exp.Parameter) -> str:
    1743        this = self.sql(expression, "this")
    1744        this = f"{{{this}}}" if expression.args.get("wrapped") else f"{this}"
    1745        return f"{self.PARAMETER_TOKEN}{this}"
    1746
    1747    def sessionparameter_sql(self, expression: exp.SessionParameter) -> str:
    1748        this = self.sql(expression, "this")
    1749        kind = expression.text("kind")
    1750        if kind:
    1751            kind = f"{kind}."
    1752        return f"@@{kind}{this}"
    1753
    1754    def placeholder_sql(self, expression: exp.Placeholder) -> str:
    1755        return f":{expression.name}" if expression.name else "?"
    1756
    1757    def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str:
    1758        alias = self.sql(expression, "alias")
    1759        alias = f"{sep}{alias}" if alias else ""
    1760
    1761        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1762        pivots = f" {pivots}" if pivots else ""
    1763
    1764        sql = self.query_modifiers(expression, self.wrap(expression), alias, pivots)
    1765        return self.prepend_ctes(expression, sql)
    1766
    1767    def qualify_sql(self, expression: exp.Qualify) -> str:
    1768        this = self.indent(self.sql(expression, "this"))
    1769        return f"{self.seg('QUALIFY')}{self.sep()}{this}"
    1770
    1771    def union_sql(self, expression: exp.Union) -> str:
    1772        return self.prepend_ctes(
    1773            expression,
    1774            self.set_operation(expression, self.union_op(expression)),
    1775        )
    1776
    1777    def union_op(self, expression: exp.Union) -> str:
    1778        kind = " DISTINCT" if self.EXPLICIT_UNION else ""
    1779        kind = kind if expression.args.get("distinct") else " ALL"
    1780        return f"UNION{kind}"
    1781
    1782    def unnest_sql(self, expression: exp.Unnest) -> str:
    1783        args = self.expressions(expression, flat=True)
    1784        alias = expression.args.get("alias")
    1785        if alias and self.UNNEST_COLUMN_ONLY:
    1786            columns = alias.columns
    1787            alias = self.sql(columns[0]) if columns else ""
    1788        else:
    1789            alias = self.sql(expression, "alias")
    1790        alias = f" AS {alias}" if alias else alias
    1791        ordinality = " WITH ORDINALITY" if expression.args.get("ordinality") else ""
    1792        offset = expression.args.get("offset")
    1793        offset = f" WITH OFFSET AS {self.sql(offset)}" if offset else ""
    1794        return f"UNNEST({args}){ordinality}{alias}{offset}"
    1795
    1796    def where_sql(self, expression: exp.Where) -> str:
    1797        this = self.indent(self.sql(expression, "this"))
    1798        return f"{self.seg('WHERE')}{self.sep()}{this}"
    1799
    1800    def window_sql(self, expression: exp.Window) -> str:
    1801        this = self.sql(expression, "this")
    1802        partition = self.partition_by_sql(expression)
    1803        order = expression.args.get("order")
    1804        order = self.order_sql(order, flat=True) if order else ""
    1805        spec = self.sql(expression, "spec")
    1806        alias = self.sql(expression, "alias")
    1807        over = self.sql(expression, "over") or "OVER"
    1808
    1809        this = f"{this} {'AS' if expression.arg_key == 'windows' else over}"
    1810
    1811        first = expression.args.get("first")
    1812        if first is None:
    1813            first = ""
    1814        else:
    1815            first = "FIRST" if first else "LAST"
    1816
    1817        if not partition and not order and not spec and alias:
    1818            return f"{this} {alias}"
    1819
    1820        args = " ".join(arg for arg in (alias, first, partition, order, spec) if arg)
    1821        return f"{this} ({args})"
    1822
    1823    def partition_by_sql(self, expression: exp.Window | exp.MatchRecognize) -> str:
    1824        partition = self.expressions(expression, key="partition_by", flat=True)
    1825        return f"PARTITION BY {partition}" if partition else ""
    1826
    1827    def windowspec_sql(self, expression: exp.WindowSpec) -> str:
    1828        kind = self.sql(expression, "kind")
    1829        start = csv(self.sql(expression, "start"), self.sql(expression, "start_side"), sep=" ")
    1830        end = (
    1831            csv(self.sql(expression, "end"), self.sql(expression, "end_side"), sep=" ")
    1832            or "CURRENT ROW"
    1833        )
    1834        return f"{kind} BETWEEN {start} AND {end}"
    1835
    1836    def withingroup_sql(self, expression: exp.WithinGroup) -> str:
    1837        this = self.sql(expression, "this")
    1838        expression_sql = self.sql(expression, "expression")[1:]  # order has a leading space
    1839        return f"{this} WITHIN GROUP ({expression_sql})"
    1840
    1841    def between_sql(self, expression: exp.Between) -> str:
    1842        this = self.sql(expression, "this")
    1843        low = self.sql(expression, "low")
    1844        high = self.sql(expression, "high")
    1845        return f"{this} BETWEEN {low} AND {high}"
    1846
    1847    def bracket_sql(self, expression: exp.Bracket) -> str:
    1848        expressions = apply_index_offset(expression.this, expression.expressions, self.INDEX_OFFSET)
    1849        expressions_sql = ", ".join(self.sql(e) for e in expressions)
    1850
    1851        return f"{self.sql(expression, 'this')}[{expressions_sql}]"
    1852
    1853    def safebracket_sql(self, expression: exp.SafeBracket) -> str:
    1854        return self.bracket_sql(expression)
    1855
    1856    def all_sql(self, expression: exp.All) -> str:
    1857        return f"ALL {self.wrap(expression)}"
    1858
    1859    def any_sql(self, expression: exp.Any) -> str:
    1860        this = self.sql(expression, "this")
    1861        if isinstance(expression.this, exp.Subqueryable):
    1862            this = self.wrap(this)
    1863        return f"ANY {this}"
    1864
    1865    def exists_sql(self, expression: exp.Exists) -> str:
    1866        return f"EXISTS{self.wrap(expression)}"
    1867
    1868    def case_sql(self, expression: exp.Case) -> str:
    1869        this = self.sql(expression, "this")
    1870        statements = [f"CASE {this}" if this else "CASE"]
    1871
    1872        for e in expression.args["ifs"]:
    1873            statements.append(f"WHEN {self.sql(e, 'this')}")
    1874            statements.append(f"THEN {self.sql(e, 'true')}")
    1875
    1876        default = self.sql(expression, "default")
    1877
    1878        if default:
    1879            statements.append(f"ELSE {default}")
    1880
    1881        statements.append("END")
    1882
    1883        if self.pretty and self.text_width(statements) > self.max_text_width:
    1884            return self.indent("\n".join(statements), skip_first=True, skip_last=True)
    1885
    1886        return " ".join(statements)
    1887
    1888    def constraint_sql(self, expression: exp.Constraint) -> str:
    1889        this = self.sql(expression, "this")
    1890        expressions = self.expressions(expression, flat=True)
    1891        return f"CONSTRAINT {this} {expressions}"
    1892
    1893    def nextvaluefor_sql(self, expression: exp.NextValueFor) -> str:
    1894        order = expression.args.get("order")
    1895        order = f" OVER ({self.order_sql(order, flat=True)})" if order else ""
    1896        return f"NEXT VALUE FOR {self.sql(expression, 'this')}{order}"
    1897
    1898    def extract_sql(self, expression: exp.Extract) -> str:
    1899        this = self.sql(expression, "this") if self.EXTRACT_ALLOWS_QUOTES else expression.this.name
    1900        expression_sql = self.sql(expression, "expression")
    1901        return f"EXTRACT({this} FROM {expression_sql})"
    1902
    1903    def trim_sql(self, expression: exp.Trim) -> str:
    1904        trim_type = self.sql(expression, "position")
    1905
    1906        if trim_type == "LEADING":
    1907            return self.func("LTRIM", expression.this)
    1908        elif trim_type == "TRAILING":
    1909            return self.func("RTRIM", expression.this)
    1910        else:
    1911            return self.func("TRIM", expression.this, expression.expression)
    1912
    1913    def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    1914        expressions = expression.expressions
    1915        if self.STRICT_STRING_CONCAT:
    1916            expressions = (exp.cast(e, "text") for e in expressions)
    1917        return self.func("CONCAT", *expressions)
    1918
    1919    def check_sql(self, expression: exp.Check) -> str:
    1920        this = self.sql(expression, key="this")
    1921        return f"CHECK ({this})"
    1922
    1923    def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
    1924        expressions = self.expressions(expression, flat=True)
    1925        reference = self.sql(expression, "reference")
    1926        reference = f" {reference}" if reference else ""
    1927        delete = self.sql(expression, "delete")
    1928        delete = f" ON DELETE {delete}" if delete else ""
    1929        update = self.sql(expression, "update")
    1930        update = f" ON UPDATE {update}" if update else ""
    1931        return f"FOREIGN KEY ({expressions}){reference}{delete}{update}"
    1932
    1933    def primarykey_sql(self, expression: exp.ForeignKey) -> str:
    1934        expressions = self.expressions(expression, flat=True)
    1935        options = self.expressions(expression, key="options", flat=True, sep=" ")
    1936        options = f" {options}" if options else ""
    1937        return f"PRIMARY KEY ({expressions}){options}"
    1938
    1939    def if_sql(self, expression: exp.If) -> str:
    1940        expression = expression.copy()
    1941        return self.case_sql(exp.Case(ifs=[expression], default=expression.args.get("false")))
    1942
    1943    def matchagainst_sql(self, expression: exp.MatchAgainst) -> str:
    1944        modifier = expression.args.get("modifier")
    1945        modifier = f" {modifier}" if modifier else ""
    1946        return f"{self.func('MATCH', *expression.expressions)} AGAINST({self.sql(expression, 'this')}{modifier})"
    1947
    1948    def jsonkeyvalue_sql(self, expression: exp.JSONKeyValue) -> str:
    1949        return f"{self.sql(expression, 'this')}: {self.sql(expression, 'expression')}"
    1950
    1951    def jsonobject_sql(self, expression: exp.JSONObject) -> str:
    1952        null_handling = expression.args.get("null_handling")
    1953        null_handling = f" {null_handling}" if null_handling else ""
    1954        unique_keys = expression.args.get("unique_keys")
    1955        if unique_keys is not None:
    1956            unique_keys = f" {'WITH' if unique_keys else 'WITHOUT'} UNIQUE KEYS"
    1957        else:
    1958            unique_keys = ""
    1959        return_type = self.sql(expression, "return_type")
    1960        return_type = f" RETURNING {return_type}" if return_type else ""
    1961        format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
    1962        encoding = self.sql(expression, "encoding")
    1963        encoding = f" ENCODING {encoding}" if encoding else ""
    1964        return self.func(
    1965            "JSON_OBJECT",
    1966            *expression.expressions,
    1967            suffix=f"{null_handling}{unique_keys}{return_type}{format_json}{encoding})",
    1968        )
    1969
    1970    def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
    1971        this = self.sql(expression, "this")
    1972        kind = self.sql(expression, "kind")
    1973        path = self.sql(expression, "path")
    1974        path = f" {path}" if path else ""
    1975        as_json = " AS JSON" if expression.args.get("as_json") else ""
    1976        return f"{this} {kind}{path}{as_json}"
    1977
    1978    def openjson_sql(self, expression: exp.OpenJSON) -> str:
    1979        this = self.sql(expression, "this")
    1980        path = self.sql(expression, "path")
    1981        path = f", {path}" if path else ""
    1982        expressions = self.expressions(expression)
    1983        with_ = (
    1984            f" WITH ({self.seg(self.indent(expressions), sep='')}{self.seg(')', sep='')}"
    1985            if expressions
    1986            else ""
    1987        )
    1988        return f"OPENJSON({this}{path}){with_}"
    1989
    1990    def in_sql(self, expression: exp.In) -> str:
    1991        query = expression.args.get("query")
    1992        unnest = expression.args.get("unnest")
    1993        field = expression.args.get("field")
    1994        is_global = " GLOBAL" if expression.args.get("is_global") else ""
    1995
    1996        if query:
    1997            in_sql = self.wrap(query)
    1998        elif unnest:
    1999            in_sql = self.in_unnest_op(unnest)
    2000        elif field:
    2001            in_sql = self.sql(field)
    2002        else:
    2003            in_sql = f"({self.expressions(expression, flat=True)})"
    2004
    2005        return f"{self.sql(expression, 'this')}{is_global} IN {in_sql}"
    2006
    2007    def in_unnest_op(self, unnest: exp.Unnest) -> str:
    2008        return f"(SELECT {self.sql(unnest)})"
    2009
    2010    def interval_sql(self, expression: exp.Interval) -> str:
    2011        unit = self.sql(expression, "unit")
    2012        if not self.INTERVAL_ALLOWS_PLURAL_FORM:
    2013            unit = self.TIME_PART_SINGULARS.get(unit.lower(), unit)
    2014        unit = f" {unit}" if unit else ""
    2015
    2016        if self.SINGLE_STRING_INTERVAL:
    2017            this = expression.this.name if expression.this else ""
    2018            return f"INTERVAL '{this}{unit}'" if this else f"INTERVAL{unit}"
    2019
    2020        this = self.sql(expression, "this")
    2021        if this:
    2022            unwrapped = isinstance(expression.this, self.UNWRAPPED_INTERVAL_VALUES)
    2023            this = f" {this}" if unwrapped else f" ({this})"
    2024
    2025        return f"INTERVAL{this}{unit}"
    2026
    2027    def return_sql(self, expression: exp.Return) -> str:
    2028        return f"RETURN {self.sql(expression, 'this')}"
    2029
    2030    def reference_sql(self, expression: exp.Reference) -> str:
    2031        this = self.sql(expression, "this")
    2032        expressions = self.expressions(expression, flat=True)
    2033        expressions = f"({expressions})" if expressions else ""
    2034        options = self.expressions(expression, key="options", flat=True, sep=" ")
    2035        options = f" {options}" if options else ""
    2036        return f"REFERENCES {this}{expressions}{options}"
    2037
    2038    def anonymous_sql(self, expression: exp.Anonymous) -> str:
    2039        return self.func(expression.name, *expression.expressions)
    2040
    2041    def paren_sql(self, expression: exp.Paren) -> str:
    2042        if isinstance(expression.unnest(), exp.Select):
    2043            sql = self.wrap(expression)
    2044        else:
    2045            sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
    2046            sql = f"({sql}{self.seg(')', sep='')}"
    2047
    2048        return self.prepend_ctes(expression, sql)
    2049
    2050    def neg_sql(self, expression: exp.Neg) -> str:
    2051        # This makes sure we don't convert "- - 5" to "--5", which is a comment
    2052        this_sql = self.sql(expression, "this")
    2053        sep = " " if this_sql[0] == "-" else ""
    2054        return f"-{sep}{this_sql}"
    2055
    2056    def not_sql(self, expression: exp.Not) -> str:
    2057        return f"NOT {self.sql(expression, 'this')}"
    2058
    2059    def alias_sql(self, expression: exp.Alias) -> str:
    2060        alias = self.sql(expression, "alias")
    2061        alias = f" AS {alias}" if alias else ""
    2062        return f"{self.sql(expression, 'this')}{alias}"
    2063
    2064    def aliases_sql(self, expression: exp.Aliases) -> str:
    2065        return f"{self.sql(expression, 'this')} AS ({self.expressions(expression, flat=True)})"
    2066
    2067    def attimezone_sql(self, expression: exp.AtTimeZone) -> str:
    2068        this = self.sql(expression, "this")
    2069        zone = self.sql(expression, "zone")
    2070        return f"{this} AT TIME ZONE {zone}"
    2071
    2072    def add_sql(self, expression: exp.Add) -> str:
    2073        return self.binary(expression, "+")
    2074
    2075    def and_sql(self, expression: exp.And) -> str:
    2076        return self.connector_sql(expression, "AND")
    2077
    2078    def xor_sql(self, expression: exp.Xor) -> str:
    2079        return self.connector_sql(expression, "XOR")
    2080
    2081    def connector_sql(self, expression: exp.Connector, op: str) -> str:
    2082        if not self.pretty:
    2083            return self.binary(expression, op)
    2084
    2085        sqls = tuple(
    2086            self.maybe_comment(self.sql(e), e, e.parent.comments or []) if i != 1 else self.sql(e)
    2087            for i, e in enumerate(expression.flatten(unnest=False))
    2088        )
    2089
    2090        sep = "\n" if self.text_width(sqls) > self.max_text_width else " "
    2091        return f"{sep}{op} ".join(sqls)
    2092
    2093    def bitwiseand_sql(self, expression: exp.BitwiseAnd) -> str:
    2094        return self.binary(expression, "&")
    2095
    2096    def bitwiseleftshift_sql(self, expression: exp.BitwiseLeftShift) -> str:
    2097        return self.binary(expression, "<<")
    2098
    2099    def bitwisenot_sql(self, expression: exp.BitwiseNot) -> str:
    2100        return f"~{self.sql(expression, 'this')}"
    2101
    2102    def bitwiseor_sql(self, expression: exp.BitwiseOr) -> str:
    2103        return self.binary(expression, "|")
    2104
    2105    def bitwiserightshift_sql(self, expression: exp.BitwiseRightShift) -> str:
    2106        return self.binary(expression, ">>")
    2107
    2108    def bitwisexor_sql(self, expression: exp.BitwiseXor) -> str:
    2109        return self.binary(expression, "^")
    2110
    2111    def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    2112        format_sql = self.sql(expression, "format")
    2113        format_sql = f" FORMAT {format_sql}" if format_sql else ""
    2114        return f"{safe_prefix or ''}CAST({self.sql(expression, 'this')} AS {self.sql(expression, 'to')}{format_sql})"
    2115
    2116    def currentdate_sql(self, expression: exp.CurrentDate) -> str:
    2117        zone = self.sql(expression, "this")
    2118        return f"CURRENT_DATE({zone})" if zone else "CURRENT_DATE"
    2119
    2120    def collate_sql(self, expression: exp.Collate) -> str:
    2121        return self.binary(expression, "COLLATE")
    2122
    2123    def command_sql(self, expression: exp.Command) -> str:
    2124        return f"{self.sql(expression, 'this').upper()} {expression.text('expression').strip()}"
    2125
    2126    def comment_sql(self, expression: exp.Comment) -> str:
    2127        this = self.sql(expression, "this")
    2128        kind = expression.args["kind"]
    2129        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    2130        expression_sql = self.sql(expression, "expression")
    2131        return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"
    2132
    2133    def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
    2134        this = self.sql(expression, "this")
    2135        delete = " DELETE" if expression.args.get("delete") else ""
    2136        recompress = self.sql(expression, "recompress")
    2137        recompress = f" RECOMPRESS {recompress}" if recompress else ""
    2138        to_disk = self.sql(expression, "to_disk")
    2139        to_disk = f" TO DISK {to_disk}" if to_disk else ""
    2140        to_volume = self.sql(expression, "to_volume")
    2141        to_volume = f" TO VOLUME {to_volume}" if to_volume else ""
    2142        return f"{this}{delete}{recompress}{to_disk}{to_volume}"
    2143
    2144    def mergetreettl_sql(self, expression: exp.MergeTreeTTL) -> str:
    2145        where = self.sql(expression, "where")
    2146        group = self.sql(expression, "group")
    2147        aggregates = self.expressions(expression, key="aggregates")
    2148        aggregates = self.seg("SET") + self.seg(aggregates) if aggregates else ""
    2149
    2150        if not (where or group or aggregates) and len(expression.expressions) == 1:
    2151            return f"TTL {self.expressions(expression, flat=True)}"
    2152
    2153        return f"TTL{self.seg(self.expressions(expression))}{where}{group}{aggregates}"
    2154
    2155    def transaction_sql(self, expression: exp.Transaction) -> str:
    2156        return "BEGIN"
    2157
    2158    def commit_sql(self, expression: exp.Commit) -> str:
    2159        chain = expression.args.get("chain")
    2160        if chain is not None:
    2161            chain = " AND CHAIN" if chain else " AND NO CHAIN"
    2162
    2163        return f"COMMIT{chain or ''}"
    2164
    2165    def rollback_sql(self, expression: exp.Rollback) -> str:
    2166        savepoint = expression.args.get("savepoint")
    2167        savepoint = f" TO {savepoint}" if savepoint else ""
    2168        return f"ROLLBACK{savepoint}"
    2169
    2170    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
    2171        this = self.sql(expression, "this")
    2172
    2173        dtype = self.sql(expression, "dtype")
    2174        if dtype:
    2175            collate = self.sql(expression, "collate")
    2176            collate = f" COLLATE {collate}" if collate else ""
    2177            using = self.sql(expression, "using")
    2178            using = f" USING {using}" if using else ""
    2179            return f"ALTER COLUMN {this} TYPE {dtype}{collate}{using}"
    2180
    2181        default = self.sql(expression, "default")
    2182        if default:
    2183            return f"ALTER COLUMN {this} SET DEFAULT {default}"
    2184
    2185        if not expression.args.get("drop"):
    2186            self.unsupported("Unsupported ALTER COLUMN syntax")
    2187
    2188        return f"ALTER COLUMN {this} DROP DEFAULT"
    2189
    2190    def renametable_sql(self, expression: exp.RenameTable) -> str:
    2191        if not self.RENAME_TABLE_WITH_DB:
    2192            # Remove db from tables
    2193            expression = expression.transform(
    2194                lambda n: exp.table_(n.this) if isinstance(n, exp.Table) else n
    2195            )
    2196        this = self.sql(expression, "this")
    2197        return f"RENAME TO {this}"
    2198
    2199    def altertable_sql(self, expression: exp.AlterTable) -> str:
    2200        actions = expression.args["actions"]
    2201
    2202        if isinstance(actions[0], exp.ColumnDef):
    2203            actions = self.expressions(expression, key="actions", prefix="ADD COLUMN ")
    2204        elif isinstance(actions[0], exp.Schema):
    2205            actions = self.expressions(expression, key="actions", prefix="ADD COLUMNS ")
    2206        elif isinstance(actions[0], exp.Delete):
    2207            actions = self.expressions(expression, key="actions", flat=True)
    2208        else:
    2209            actions = self.expressions(expression, key="actions")
    2210
    2211        exists = " IF EXISTS" if expression.args.get("exists") else ""
    2212        return f"ALTER TABLE{exists} {self.sql(expression, 'this')} {actions}"
    2213
    2214    def droppartition_sql(self, expression: exp.DropPartition) -> str:
    2215        expressions = self.expressions(expression)
    2216        exists = " IF EXISTS " if expression.args.get("exists") else " "
    2217        return f"DROP{exists}{expressions}"
    2218
    2219    def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
    2220        this = self.sql(expression, "this")
    2221        expression_ = self.sql(expression, "expression")
    2222        add_constraint = f"ADD CONSTRAINT {this}" if this else "ADD"
    2223
    2224        enforced = expression.args.get("enforced")
    2225        if enforced is not None:
    2226            return f"{add_constraint} CHECK ({expression_}){' ENFORCED' if enforced else ''}"
    2227
    2228        return f"{add_constraint} {expression_}"
    2229
    2230    def distinct_sql(self, expression: exp.Distinct) -> str:
    2231        this = self.expressions(expression, flat=True)
    2232        this = f" {this}" if this else ""
    2233
    2234        on = self.sql(expression, "on")
    2235        on = f" ON {on}" if on else ""
    2236        return f"DISTINCT{this}{on}"
    2237
    2238    def ignorenulls_sql(self, expression: exp.IgnoreNulls) -> str:
    2239        return f"{self.sql(expression, 'this')} IGNORE NULLS"
    2240
    2241    def respectnulls_sql(self, expression: exp.RespectNulls) -> str:
    2242        return f"{self.sql(expression, 'this')} RESPECT NULLS"
    2243
    2244    def intdiv_sql(self, expression: exp.IntDiv) -> str:
    2245        return self.sql(
    2246            exp.Cast(
    2247                this=exp.Div(this=expression.this.copy(), expression=expression.expression.copy()),
    2248                to=exp.DataType(this=exp.DataType.Type.INT),
    2249            )
    2250        )
    2251
    2252    def dpipe_sql(self, expression: exp.DPipe) -> str:
    2253        return self.binary(expression, "||")
    2254
    2255    def safedpipe_sql(self, expression: exp.SafeDPipe) -> str:
    2256        if self.STRICT_STRING_CONCAT:
    2257            return self.func("CONCAT", *(exp.cast(e, "text") for e in expression.flatten()))
    2258        return self.dpipe_sql(expression)
    2259
    2260    def div_sql(self, expression: exp.Div) -> str:
    2261        return self.binary(expression, "/")
    2262
    2263    def overlaps_sql(self, expression: exp.Overlaps) -> str:
    2264        return self.binary(expression, "OVERLAPS")
    2265
    2266    def distance_sql(self, expression: exp.Distance) -> str:
    2267        return self.binary(expression, "<->")
    2268
    2269    def dot_sql(self, expression: exp.Dot) -> str:
    2270        return f"{self.sql(expression, 'this')}.{self.sql(expression, 'expression')}"
    2271
    2272    def eq_sql(self, expression: exp.EQ) -> str:
    2273        return self.binary(expression, "=")
    2274
    2275    def escape_sql(self, expression: exp.Escape) -> str:
    2276        return self.binary(expression, "ESCAPE")
    2277
    2278    def glob_sql(self, expression: exp.Glob) -> str:
    2279        return self.binary(expression, "GLOB")
    2280
    2281    def gt_sql(self, expression: exp.GT) -> str:
    2282        return self.binary(expression, ">")
    2283
    2284    def gte_sql(self, expression: exp.GTE) -> str:
    2285        return self.binary(expression, ">=")
    2286
    2287    def ilike_sql(self, expression: exp.ILike) -> str:
    2288        return self.binary(expression, "ILIKE")
    2289
    2290    def ilikeany_sql(self, expression: exp.ILikeAny) -> str:
    2291        return self.binary(expression, "ILIKE ANY")
    2292
    2293    def is_sql(self, expression: exp.Is) -> str:
    2294        if not self.IS_BOOL_ALLOWED and isinstance(expression.expression, exp.Boolean):
    2295            return self.sql(
    2296                expression.this if expression.expression.this else exp.not_(expression.this)
    2297            )
    2298        return self.binary(expression, "IS")
    2299
    2300    def like_sql(self, expression: exp.Like) -> str:
    2301        return self.binary(expression, "LIKE")
    2302
    2303    def likeany_sql(self, expression: exp.LikeAny) -> str:
    2304        return self.binary(expression, "LIKE ANY")
    2305
    2306    def similarto_sql(self, expression: exp.SimilarTo) -> str:
    2307        return self.binary(expression, "SIMILAR TO")
    2308
    2309    def lt_sql(self, expression: exp.LT) -> str:
    2310        return self.binary(expression, "<")
    2311
    2312    def lte_sql(self, expression: exp.LTE) -> str:
    2313        return self.binary(expression, "<=")
    2314
    2315    def mod_sql(self, expression: exp.Mod) -> str:
    2316        return self.binary(expression, "%")
    2317
    2318    def mul_sql(self, expression: exp.Mul) -> str:
    2319        return self.binary(expression, "*")
    2320
    2321    def neq_sql(self, expression: exp.NEQ) -> str:
    2322        return self.binary(expression, "<>")
    2323
    2324    def nullsafeeq_sql(self, expression: exp.NullSafeEQ) -> str:
    2325        return self.binary(expression, "IS NOT DISTINCT FROM")
    2326
    2327    def nullsafeneq_sql(self, expression: exp.NullSafeNEQ) -> str:
    2328        return self.binary(expression, "IS DISTINCT FROM")
    2329
    2330    def or_sql(self, expression: exp.Or) -> str:
    2331        return self.connector_sql(expression, "OR")
    2332
    2333    def slice_sql(self, expression: exp.Slice) -> str:
    2334        return self.binary(expression, ":")
    2335
    2336    def sub_sql(self, expression: exp.Sub) -> str:
    2337        return self.binary(expression, "-")
    2338
    2339    def trycast_sql(self, expression: exp.TryCast) -> str:
    2340        return self.cast_sql(expression, safe_prefix="TRY_")
    2341
    2342    def use_sql(self, expression: exp.Use) -> str:
    2343        kind = self.sql(expression, "kind")
    2344        kind = f" {kind}" if kind else ""
    2345        this = self.sql(expression, "this")
    2346        this = f" {this}" if this else ""
    2347        return f"USE{kind}{this}"
    2348
    2349    def binary(self, expression: exp.Binary, op: str) -> str:
    2350        op = self.maybe_comment(op, comments=expression.comments)
    2351        return f"{self.sql(expression, 'this')} {op} {self.sql(expression, 'expression')}"
    2352
    2353    def function_fallback_sql(self, expression: exp.Func) -> str:
    2354        args = []
    2355
    2356        for key in expression.arg_types:
    2357            arg_value = expression.args.get(key)
    2358
    2359            if isinstance(arg_value, list):
    2360                for value in arg_value:
    2361                    args.append(value)
    2362            elif arg_value is not None:
    2363                args.append(arg_value)
    2364
    2365        if self.normalize_functions:
    2366            name = expression.sql_name()
    2367        else:
    2368            name = (expression._meta and expression.meta.get("name")) or expression.sql_name()
    2369
    2370        return self.func(name, *args)
    2371
    2372    def func(
    2373        self,
    2374        name: str,
    2375        *args: t.Optional[exp.Expression | str],
    2376        prefix: str = "(",
    2377        suffix: str = ")",
    2378    ) -> str:
    2379        return f"{self.normalize_func(name)}{prefix}{self.format_args(*args)}{suffix}"
    2380
    2381    def format_args(self, *args: t.Optional[str | exp.Expression]) -> str:
    2382        arg_sqls = tuple(self.sql(arg) for arg in args if arg is not None)
    2383        if self.pretty and self.text_width(arg_sqls) > self.max_text_width:
    2384            return self.indent("\n" + f",\n".join(arg_sqls) + "\n", skip_first=True, skip_last=True)
    2385        return ", ".join(arg_sqls)
    2386
    2387    def text_width(self, args: t.Iterable) -> int:
    2388        return sum(len(arg) for arg in args)
    2389
    2390    def format_time(self, expression: exp.Expression) -> t.Optional[str]:
    2391        return format_time(
    2392            self.sql(expression, "format"), self.INVERSE_TIME_MAPPING, self.INVERSE_TIME_TRIE
    2393        )
    2394
    2395    def expressions(
    2396        self,
    2397        expression: t.Optional[exp.Expression] = None,
    2398        key: t.Optional[str] = None,
    2399        sqls: t.Optional[t.List[str]] = None,
    2400        flat: bool = False,
    2401        indent: bool = True,
    2402        skip_first: bool = False,
    2403        sep: str = ", ",
    2404        prefix: str = "",
    2405    ) -> str:
    2406        expressions = expression.args.get(key or "expressions") if expression else sqls
    2407
    2408        if not expressions:
    2409            return ""
    2410
    2411        if flat:
    2412            return sep.join(sql for sql in (self.sql(e) for e in expressions) if sql)
    2413
    2414        num_sqls = len(expressions)
    2415
    2416        # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
    2417        pad = " " * self.pad
    2418        stripped_sep = sep.strip()
    2419
    2420        result_sqls = []
    2421        for i, e in enumerate(expressions):
    2422            sql = self.sql(e, comment=False)
    2423            if not sql:
    2424                continue
    2425
    2426            comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
    2427
    2428            if self.pretty:
    2429                if self.leading_comma:
    2430                    result_sqls.append(f"{sep if i > 0 else pad}{prefix}{sql}{comments}")
    2431                else:
    2432                    result_sqls.append(
    2433                        f"{prefix}{sql}{stripped_sep if i + 1 < num_sqls else ''}{comments}"
    2434                    )
    2435            else:
    2436                result_sqls.append(f"{prefix}{sql}{comments}{sep if i + 1 < num_sqls else ''}")
    2437
    2438        result_sql = "\n".join(result_sqls) if self.pretty else "".join(result_sqls)
    2439        return self.indent(result_sql, skip_first=skip_first) if indent else result_sql
    2440
    2441    def op_expressions(self, op: str, expression: exp.Expression, flat: bool = False) -> str:
    2442        flat = flat or isinstance(expression.parent, exp.Properties)
    2443        expressions_sql = self.expressions(expression, flat=flat)
    2444        if flat:
    2445            return f"{op} {expressions_sql}"
    2446        return f"{self.seg(op)}{self.sep() if expressions_sql else ''}{expressions_sql}"
    2447
    2448    def naked_property(self, expression: exp.Property) -> str:
    2449        property_name = exp.Properties.PROPERTY_TO_NAME.get(expression.__class__)
    2450        if not property_name:
    2451            self.unsupported(f"Unsupported property {expression.__class__.__name__}")
    2452        return f"{property_name} {self.sql(expression, 'this')}"
    2453
    2454    def set_operation(self, expression: exp.Expression, op: str) -> str:
    2455        this = self.sql(expression, "this")
    2456        op = self.seg(op)
    2457        return self.query_modifiers(
    2458            expression, f"{this}{op}{self.sep()}{self.sql(expression, 'expression')}"
    2459        )
    2460
    2461    def tag_sql(self, expression: exp.Tag) -> str:
    2462        return f"{expression.args.get('prefix')}{self.sql(expression.this)}{expression.args.get('postfix')}"
    2463
    2464    def token_sql(self, token_type: TokenType) -> str:
    2465        return self.TOKEN_MAPPING.get(token_type, token_type.name)
    2466
    2467    def userdefinedfunction_sql(self, expression: exp.UserDefinedFunction) -> str:
    2468        this = self.sql(expression, "this")
    2469        expressions = self.no_identify(self.expressions, expression)
    2470        expressions = (
    2471            self.wrap(expressions) if expression.args.get("wrapped") else f" {expressions}"
    2472        )
    2473        return f"{this}{expressions}"
    2474
    2475    def joinhint_sql(self, expression: exp.JoinHint) -> str:
    2476        this = self.sql(expression, "this")
    2477        expressions = self.expressions(expression, flat=True)
    2478        return f"{this}({expressions})"
    2479
    2480    def kwarg_sql(self, expression: exp.Kwarg) -> str:
    2481        return self.binary(expression, "=>")
    2482
    2483    def when_sql(self, expression: exp.When) -> str:
    2484        matched = "MATCHED" if expression.args["matched"] else "NOT MATCHED"
    2485        source = " BY SOURCE" if self.MATCHED_BY_SOURCE and expression.args.get("source") else ""
    2486        condition = self.sql(expression, "condition")
    2487        condition = f" AND {condition}" if condition else ""
    2488
    2489        then_expression = expression.args.get("then")
    2490        if isinstance(then_expression, exp.Insert):
    2491            then = f"INSERT {self.sql(then_expression, 'this')}"
    2492            if "expression" in then_expression.args:
    2493                then += f" VALUES {self.sql(then_expression, 'expression')}"
    2494        elif isinstance(then_expression, exp.Update):
    2495            if isinstance(then_expression.args.get("expressions"), exp.Star):
    2496                then = f"UPDATE {self.sql(then_expression, 'expressions')}"
    2497            else:
    2498                then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
    2499        else:
    2500            then = self.sql(then_expression)
    2501        return f"WHEN {matched}{source}{condition} THEN {then}"
    2502
    2503    def merge_sql(self, expression: exp.Merge) -> str:
    2504        this = self.sql(expression, "this")
    2505        using = f"USING {self.sql(expression, 'using')}"
    2506        on = f"ON {self.sql(expression, 'on')}"
    2507        return f"MERGE INTO {this} {using} {on} {self.expressions(expression, sep=' ')}"
    2508
    2509    def tochar_sql(self, expression: exp.ToChar) -> str:
    2510        if expression.args.get("format"):
    2511            self.unsupported("Format argument unsupported for TO_CHAR/TO_VARCHAR function")
    2512
    2513        return self.sql(exp.cast(expression.this, "text"))
    2514
    2515    def dictproperty_sql(self, expression: exp.DictProperty) -> str:
    2516        this = self.sql(expression, "this")
    2517        kind = self.sql(expression, "kind")
    2518        settings_sql = self.expressions(expression, key="settings", sep=" ")
    2519        args = f"({self.sep('')}{settings_sql}{self.seg(')', sep='')}" if settings_sql else "()"
    2520        return f"{this}({kind}{args})"
    2521
    2522    def dictrange_sql(self, expression: exp.DictRange) -> str:
    2523        this = self.sql(expression, "this")
    2524        max = self.sql(expression, "max")
    2525        min = self.sql(expression, "min")
    2526        return f"{this}(MIN {min} MAX {max})"
    2527
    2528    def dictsubproperty_sql(self, expression: exp.DictSubProperty) -> str:
    2529        return f"{self.sql(expression, 'this')} {self.sql(expression, 'value')}"
    2530
    2531    def oncluster_sql(self, expression: exp.OnCluster) -> str:
    2532        return ""
    2533
    2534    def clusteredbyproperty_sql(self, expression: exp.ClusteredByProperty) -> str:
    2535        expressions = self.expressions(expression, key="expressions", flat=True)
    2536        sorted_by = self.expressions(expression, key="sorted_by", flat=True)
    2537        sorted_by = f" SORTED BY ({sorted_by})" if sorted_by else ""
    2538        buckets = self.sql(expression, "buckets")
    2539        return f"CLUSTERED BY ({expressions}){sorted_by} INTO {buckets} BUCKETS"
    2540
    2541    def anyvalue_sql(self, expression: exp.AnyValue) -> str:
    2542        this = self.sql(expression, "this")
    2543        having = self.sql(expression, "having")
    2544
    2545        if having:
    2546            this = f"{this} HAVING {'MAX' if expression.args.get('max') else 'MIN'} {having}"
    2547
    2548        return self.func("ANY_VALUE", this)
    2549
    2550    def querytransform_sql(self, expression: exp.QueryTransform) -> str:
    2551        transform = self.func("TRANSFORM", *expression.expressions)
    2552        row_format_before = self.sql(expression, "row_format_before")
    2553        row_format_before = f" {row_format_before}" if row_format_before else ""
    2554        record_writer = self.sql(expression, "record_writer")
    2555        record_writer = f" RECORDWRITER {record_writer}" if record_writer else ""
    2556        using = f" USING {self.sql(expression, 'command_script')}"
    2557        schema = self.sql(expression, "schema")
    2558        schema = f" AS {schema}" if schema else ""
    2559        row_format_after = self.sql(expression, "row_format_after")
    2560        row_format_after = f" {row_format_after}" if row_format_after else ""
    2561        record_reader = self.sql(expression, "record_reader")
    2562        record_reader = f" RECORDREADER {record_reader}" if record_reader else ""
    2563        return f"{transform}{row_format_before}{record_writer}{using}{schema}{row_format_after}{record_reader}"
    2564
    2565    def indexconstraintoption_sql(self, expression: exp.IndexConstraintOption) -> str:
    2566        key_block_size = self.sql(expression, "key_block_size")
    2567        if key_block_size:
    2568            return f"KEY_BLOCK_SIZE = {key_block_size}"
    2569
    2570        using = self.sql(expression, "using")
    2571        if using:
    2572            return f"USING {using}"
    2573
    2574        parser = self.sql(expression, "parser")
    2575        if parser:
    2576            return f"WITH PARSER {parser}"
    2577
    2578        comment = self.sql(expression, "comment")
    2579        if comment:
    2580            return f"COMMENT {comment}"
    2581
    2582        visible = expression.args.get("visible")
    2583        if visible is not None:
    2584            return "VISIBLE" if visible else "INVISIBLE"
    2585
    2586        engine_attr = self.sql(expression, "engine_attr")
    2587        if engine_attr:
    2588            return f"ENGINE_ATTRIBUTE = {engine_attr}"
    2589
    2590        secondary_engine_attr = self.sql(expression, "secondary_engine_attr")
    2591        if secondary_engine_attr:
    2592            return f"SECONDARY_ENGINE_ATTRIBUTE = {secondary_engine_attr}"
    2593
    2594        self.unsupported("Unsupported index constraint option.")
    2595        return ""
    2596
    2597    def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> str:
    2598        kind = self.sql(expression, "kind")
    2599        kind = f"{kind} INDEX" if kind else "INDEX"
    2600        this = self.sql(expression, "this")
    2601        this = f" {this}" if this else ""
    2602        type_ = self.sql(expression, "type")
    2603        type_ = f" USING {type_}" if type_ else ""
    2604        schema = self.sql(expression, "schema")
    2605        schema = f" {schema}" if schema else ""
    2606        options = self.expressions(expression, key="options", sep=" ")
    2607        options = f" {options}" if options else ""
    2608        return f"{kind}{this}{type_}{schema}{options}"
    2609
    2610
    2611def cached_generator(
    2612    cache: t.Optional[t.Dict[int, str]] = None
    2613) -> t.Callable[[exp.Expression], str]:
    2614    """Returns a cached generator."""
    2615    cache = {} if cache is None else cache
    2616    generator = Generator(normalize=True, identify="safe")
    2617    return lambda e: generator.generate(e, cache)

logger = <Logger sqlglot (WARNING)>
class Generator: View Source

      17class Generator:
      18    """
      19    Generator converts a given syntax tree to the corresponding SQL string.
      20
      21    Args:
      22        pretty: Whether or not to format the produced SQL string.
      23            Default: False.
      24        identify: Determines when an identifier should be quoted. Possible values are:
      25            False (default): Never quote, except in cases where it's mandatory by the dialect.
      26            True or 'always': Always quote.
      27            'safe': Only quote identifiers that are case insensitive.
      28        normalize: Whether or not to normalize identifiers to lowercase.
      29            Default: False.
      30        pad: Determines the pad size in a formatted string.
      31            Default: 2.
      32        indent: Determines the indentation size in a formatted string.
      33            Default: 2.
      34        normalize_functions: Whether or not to normalize all function names. Possible values are:
      35            "upper" or True (default): Convert names to uppercase.
      36            "lower": Convert names to lowercase.
      37            False: Disables function name normalization.
      38        unsupported_level: Determines the generator's behavior when it encounters unsupported expressions.
      39            Default ErrorLevel.WARN.
      40        max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError.
      41            This is only relevant if unsupported_level is ErrorLevel.RAISE.
      42            Default: 3
      43        leading_comma: Determines whether or not the comma is leading or trailing in select expressions.
      44            This is only relevant when generating in pretty mode.
      45            Default: False
      46        max_text_width: The max number of characters in a segment before creating new lines in pretty mode.
      47            The default is on the smaller end because the length only represents a segment and not the true
      48            line length.
      49            Default: 80
      50        comments: Whether or not to preserve comments in the output SQL code.
      51            Default: True
      52    """
      53
      54    TRANSFORMS = {
      55        exp.DateAdd: lambda self, e: self.func(
      56            "DATE_ADD", e.this, e.expression, exp.Literal.string(e.text("unit"))
      57        ),
      58        exp.TsOrDsAdd: lambda self, e: self.func(
      59            "TS_OR_DS_ADD", e.this, e.expression, exp.Literal.string(e.text("unit"))
      60        ),
      61        exp.CaseSpecificColumnConstraint: lambda self, e: f"{'NOT ' if e.args.get('not_') else ''}CASESPECIFIC",
      62        exp.CharacterSetColumnConstraint: lambda self, e: f"CHARACTER SET {self.sql(e, 'this')}",
      63        exp.CharacterSetProperty: lambda self, e: f"{'DEFAULT ' if e.args.get('default') else ''}CHARACTER SET={self.sql(e, 'this')}",
      64        exp.CheckColumnConstraint: lambda self, e: f"CHECK ({self.sql(e, 'this')})",
      65        exp.CollateColumnConstraint: lambda self, e: f"COLLATE {self.sql(e, 'this')}",
      66        exp.CopyGrantsProperty: lambda self, e: "COPY GRANTS",
      67        exp.CommentColumnConstraint: lambda self, e: f"COMMENT {self.sql(e, 'this')}",
      68        exp.DateFormatColumnConstraint: lambda self, e: f"FORMAT {self.sql(e, 'this')}",
      69        exp.DefaultColumnConstraint: lambda self, e: f"DEFAULT {self.sql(e, 'this')}",
      70        exp.EncodeColumnConstraint: lambda self, e: f"ENCODE {self.sql(e, 'this')}",
      71        exp.ExecuteAsProperty: lambda self, e: self.naked_property(e),
      72        exp.ExternalProperty: lambda self, e: "EXTERNAL",
      73        exp.HeapProperty: lambda self, e: "HEAP",
      74        exp.InlineLengthColumnConstraint: lambda self, e: f"INLINE LENGTH {self.sql(e, 'this')}",
      75        exp.LanguageProperty: lambda self, e: self.naked_property(e),
      76        exp.LocationProperty: lambda self, e: self.naked_property(e),
      77        exp.LogProperty: lambda self, e: f"{'NO ' if e.args.get('no') else ''}LOG",
      78        exp.MaterializedProperty: lambda self, e: "MATERIALIZED",
      79        exp.NoPrimaryIndexProperty: lambda self, e: "NO PRIMARY INDEX",
      80        exp.OnCommitProperty: lambda self, e: f"ON COMMIT {'DELETE' if e.args.get('delete') else 'PRESERVE'} ROWS",
      81        exp.OnUpdateColumnConstraint: lambda self, e: f"ON UPDATE {self.sql(e, 'this')}",
      82        exp.PathColumnConstraint: lambda self, e: f"PATH {self.sql(e, 'this')}",
      83        exp.ReturnsProperty: lambda self, e: self.naked_property(e),
      84        exp.SetProperty: lambda self, e: f"{'MULTI' if e.args.get('multi') else ''}SET",
      85        exp.SettingsProperty: lambda self, e: f"SETTINGS{self.seg('')}{(self.expressions(e))}",
      86        exp.SqlSecurityProperty: lambda self, e: f"SQL SECURITY {'DEFINER' if e.args.get('definer') else 'INVOKER'}",
      87        exp.StabilityProperty: lambda self, e: e.name,
      88        exp.TemporaryProperty: lambda self, e: f"TEMPORARY",
      89        exp.ToTableProperty: lambda self, e: f"TO {self.sql(e.this)}",
      90        exp.TransientProperty: lambda self, e: "TRANSIENT",
      91        exp.TitleColumnConstraint: lambda self, e: f"TITLE {self.sql(e, 'this')}",
      92        exp.UppercaseColumnConstraint: lambda self, e: f"UPPERCASE",
      93        exp.VarMap: lambda self, e: self.func("MAP", e.args["keys"], e.args["values"]),
      94        exp.VolatileProperty: lambda self, e: "VOLATILE",
      95        exp.WithJournalTableProperty: lambda self, e: f"WITH JOURNAL TABLE={self.sql(e, 'this')}",
      96    }
      97
      98    # Whether or not null ordering is supported in order by
      99    NULL_ORDERING_SUPPORTED = True
     100
     101    # Whether or not locking reads (i.e. SELECT ... FOR UPDATE/SHARE) are supported
     102    LOCKING_READS_SUPPORTED = False
     103
     104    # Always do union distinct or union all
     105    EXPLICIT_UNION = False
     106
     107    # Wrap derived values in parens, usually standard but spark doesn't support it
     108    WRAP_DERIVED_VALUES = True
     109
     110    # Whether or not create function uses an AS before the RETURN
     111    CREATE_FUNCTION_RETURN_AS = True
     112
     113    # Whether or not MERGE ... WHEN MATCHED BY SOURCE is allowed
     114    MATCHED_BY_SOURCE = True
     115
     116    # Whether or not the INTERVAL expression works only with values like '1 day'
     117    SINGLE_STRING_INTERVAL = False
     118
     119    # Whether or not the plural form of date parts like day (i.e. "days") is supported in INTERVALs
     120    INTERVAL_ALLOWS_PLURAL_FORM = True
     121
     122    # Whether or not the TABLESAMPLE clause supports a method name, like BERNOULLI
     123    TABLESAMPLE_WITH_METHOD = True
     124
     125    # Whether or not to treat the number in TABLESAMPLE (50) as a percentage
     126    TABLESAMPLE_SIZE_IS_PERCENT = False
     127
     128    # Whether or not limit and fetch are supported (possible values: "ALL", "LIMIT", "FETCH")
     129    LIMIT_FETCH = "ALL"
     130
     131    # Whether or not a table is allowed to be renamed with a db
     132    RENAME_TABLE_WITH_DB = True
     133
     134    # The separator for grouping sets and rollups
     135    GROUPINGS_SEP = ","
     136
     137    # The string used for creating an index on a table
     138    INDEX_ON = "ON"
     139
     140    # Whether or not join hints should be generated
     141    JOIN_HINTS = True
     142
     143    # Whether or not table hints should be generated
     144    TABLE_HINTS = True
     145
     146    # Whether or not query hints should be generated
     147    QUERY_HINTS = True
     148
     149    # What kind of separator to use for query hints
     150    QUERY_HINT_SEP = ", "
     151
     152    # Whether or not comparing against booleans (e.g. x IS TRUE) is supported
     153    IS_BOOL_ALLOWED = True
     154
     155    # Whether or not to include the "SET" keyword in the "INSERT ... ON DUPLICATE KEY UPDATE" statement
     156    DUPLICATE_KEY_UPDATE_WITH_SET = True
     157
     158    # Whether or not to generate the limit as TOP <value> instead of LIMIT <value>
     159    LIMIT_IS_TOP = False
     160
     161    # Whether or not to generate INSERT INTO ... RETURNING or INSERT INTO RETURNING ...
     162    RETURNING_END = True
     163
     164    # Whether or not to generate the (+) suffix for columns used in old-style join conditions
     165    COLUMN_JOIN_MARKS_SUPPORTED = False
     166
     167    # Whether or not to generate an unquoted value for EXTRACT's date part argument
     168    EXTRACT_ALLOWS_QUOTES = True
     169
     170    # https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax
     171    SELECT_KINDS: t.Tuple[str, ...] = ("STRUCT", "VALUE")
     172
     173    # Whether or not VALUES statements can be used as derived tables.
     174    # MySQL 5 and Redshift do not allow this, so when False, it will convert
     175    # SELECT * VALUES into SELECT UNION
     176    VALUES_AS_TABLE = True
     177
     178    TYPE_MAPPING = {
     179        exp.DataType.Type.NCHAR: "CHAR",
     180        exp.DataType.Type.NVARCHAR: "VARCHAR",
     181        exp.DataType.Type.MEDIUMTEXT: "TEXT",
     182        exp.DataType.Type.LONGTEXT: "TEXT",
     183        exp.DataType.Type.MEDIUMBLOB: "BLOB",
     184        exp.DataType.Type.LONGBLOB: "BLOB",
     185        exp.DataType.Type.INET: "INET",
     186    }
     187
     188    STAR_MAPPING = {
     189        "except": "EXCEPT",
     190        "replace": "REPLACE",
     191    }
     192
     193    TIME_PART_SINGULARS = {
     194        "microseconds": "microsecond",
     195        "seconds": "second",
     196        "minutes": "minute",
     197        "hours": "hour",
     198        "days": "day",
     199        "weeks": "week",
     200        "months": "month",
     201        "quarters": "quarter",
     202        "years": "year",
     203    }
     204
     205    TOKEN_MAPPING: t.Dict[TokenType, str] = {}
     206
     207    STRUCT_DELIMITER = ("<", ">")
     208
     209    PARAMETER_TOKEN = "@"
     210
     211    PROPERTIES_LOCATION = {
     212        exp.AlgorithmProperty: exp.Properties.Location.POST_CREATE,
     213        exp.AutoIncrementProperty: exp.Properties.Location.POST_SCHEMA,
     214        exp.BlockCompressionProperty: exp.Properties.Location.POST_NAME,
     215        exp.CharacterSetProperty: exp.Properties.Location.POST_SCHEMA,
     216        exp.ChecksumProperty: exp.Properties.Location.POST_NAME,
     217        exp.CollateProperty: exp.Properties.Location.POST_SCHEMA,
     218        exp.CopyGrantsProperty: exp.Properties.Location.POST_SCHEMA,
     219        exp.Cluster: exp.Properties.Location.POST_SCHEMA,
     220        exp.ClusteredByProperty: exp.Properties.Location.POST_SCHEMA,
     221        exp.DataBlocksizeProperty: exp.Properties.Location.POST_NAME,
     222        exp.DefinerProperty: exp.Properties.Location.POST_CREATE,
     223        exp.DictRange: exp.Properties.Location.POST_SCHEMA,
     224        exp.DictProperty: exp.Properties.Location.POST_SCHEMA,
     225        exp.DistKeyProperty: exp.Properties.Location.POST_SCHEMA,
     226        exp.DistStyleProperty: exp.Properties.Location.POST_SCHEMA,
     227        exp.EngineProperty: exp.Properties.Location.POST_SCHEMA,
     228        exp.ExecuteAsProperty: exp.Properties.Location.POST_SCHEMA,
     229        exp.ExternalProperty: exp.Properties.Location.POST_CREATE,
     230        exp.FallbackProperty: exp.Properties.Location.POST_NAME,
     231        exp.FileFormatProperty: exp.Properties.Location.POST_WITH,
     232        exp.FreespaceProperty: exp.Properties.Location.POST_NAME,
     233        exp.HeapProperty: exp.Properties.Location.POST_WITH,
     234        exp.IsolatedLoadingProperty: exp.Properties.Location.POST_NAME,
     235        exp.JournalProperty: exp.Properties.Location.POST_NAME,
     236        exp.LanguageProperty: exp.Properties.Location.POST_SCHEMA,
     237        exp.LikeProperty: exp.Properties.Location.POST_SCHEMA,
     238        exp.LocationProperty: exp.Properties.Location.POST_SCHEMA,
     239        exp.LockingProperty: exp.Properties.Location.POST_ALIAS,
     240        exp.LogProperty: exp.Properties.Location.POST_NAME,
     241        exp.MaterializedProperty: exp.Properties.Location.POST_CREATE,
     242        exp.MergeBlockRatioProperty: exp.Properties.Location.POST_NAME,
     243        exp.NoPrimaryIndexProperty: exp.Properties.Location.POST_EXPRESSION,
     244        exp.OnCommitProperty: exp.Properties.Location.POST_EXPRESSION,
     245        exp.Order: exp.Properties.Location.POST_SCHEMA,
     246        exp.PartitionedByProperty: exp.Properties.Location.POST_WITH,
     247        exp.PrimaryKey: exp.Properties.Location.POST_SCHEMA,
     248        exp.Property: exp.Properties.Location.POST_WITH,
     249        exp.ReturnsProperty: exp.Properties.Location.POST_SCHEMA,
     250        exp.RowFormatProperty: exp.Properties.Location.POST_SCHEMA,
     251        exp.RowFormatDelimitedProperty: exp.Properties.Location.POST_SCHEMA,
     252        exp.RowFormatSerdeProperty: exp.Properties.Location.POST_SCHEMA,
     253        exp.SchemaCommentProperty: exp.Properties.Location.POST_SCHEMA,
     254        exp.SerdeProperties: exp.Properties.Location.POST_SCHEMA,
     255        exp.Set: exp.Properties.Location.POST_SCHEMA,
     256        exp.SettingsProperty: exp.Properties.Location.POST_SCHEMA,
     257        exp.SetProperty: exp.Properties.Location.POST_CREATE,
     258        exp.SortKeyProperty: exp.Properties.Location.POST_SCHEMA,
     259        exp.SqlSecurityProperty: exp.Properties.Location.POST_CREATE,
     260        exp.StabilityProperty: exp.Properties.Location.POST_SCHEMA,
     261        exp.TemporaryProperty: exp.Properties.Location.POST_CREATE,
     262        exp.ToTableProperty: exp.Properties.Location.POST_SCHEMA,
     263        exp.TransientProperty: exp.Properties.Location.POST_CREATE,
     264        exp.MergeTreeTTL: exp.Properties.Location.POST_SCHEMA,
     265        exp.VolatileProperty: exp.Properties.Location.POST_CREATE,
     266        exp.WithDataProperty: exp.Properties.Location.POST_EXPRESSION,
     267        exp.WithJournalTableProperty: exp.Properties.Location.POST_NAME,
     268    }
     269
     270    # Keywords that can't be used as unquoted identifier names
     271    RESERVED_KEYWORDS: t.Set[str] = set()
     272
     273    # Expressions whose comments are separated from them for better formatting
     274    WITH_SEPARATED_COMMENTS: t.Tuple[t.Type[exp.Expression], ...] = (
     275        exp.Delete,
     276        exp.Drop,
     277        exp.From,
     278        exp.Insert,
     279        exp.Select,
     280        exp.Update,
     281        exp.Where,
     282        exp.With,
     283    )
     284
     285    # Expressions that can remain unwrapped when appearing in the context of an INTERVAL
     286    UNWRAPPED_INTERVAL_VALUES: t.Tuple[t.Type[exp.Expression], ...] = (
     287        exp.Column,
     288        exp.Literal,
     289        exp.Neg,
     290        exp.Paren,
     291    )
     292
     293    SENTINEL_LINE_BREAK = "__SQLGLOT__LB__"
     294
     295    # Autofilled
     296    INVERSE_TIME_MAPPING: t.Dict[str, str] = {}
     297    INVERSE_TIME_TRIE: t.Dict = {}
     298    INDEX_OFFSET = 0
     299    UNNEST_COLUMN_ONLY = False
     300    ALIAS_POST_TABLESAMPLE = False
     301    IDENTIFIERS_CAN_START_WITH_DIGIT = False
     302    STRICT_STRING_CONCAT = False
     303    NORMALIZE_FUNCTIONS: bool | str = "upper"
     304    NULL_ORDERING = "nulls_are_small"
     305    ESCAPE_LINE_BREAK = False
     306
     307    can_identify: t.Callable[[str, str | bool], bool]
     308
     309    # Delimiters for quotes, identifiers and the corresponding escape characters
     310    QUOTE_START = "'"
     311    QUOTE_END = "'"
     312    IDENTIFIER_START = '"'
     313    IDENTIFIER_END = '"'
     314    STRING_ESCAPE = "'"
     315    IDENTIFIER_ESCAPE = '"'
     316
     317    # Delimiters for bit, hex, byte and raw literals
     318    BIT_START: t.Optional[str] = None
     319    BIT_END: t.Optional[str] = None
     320    HEX_START: t.Optional[str] = None
     321    HEX_END: t.Optional[str] = None
     322    BYTE_START: t.Optional[str] = None
     323    BYTE_END: t.Optional[str] = None
     324
     325    __slots__ = (
     326        "pretty",
     327        "identify",
     328        "normalize",
     329        "pad",
     330        "_indent",
     331        "normalize_functions",
     332        "unsupported_level",
     333        "max_unsupported",
     334        "leading_comma",
     335        "max_text_width",
     336        "comments",
     337        "unsupported_messages",
     338        "_escaped_quote_end",
     339        "_escaped_identifier_end",
     340        "_cache",
     341    )
     342
     343    def __init__(
     344        self,
     345        pretty: t.Optional[bool] = None,
     346        identify: str | bool = False,
     347        normalize: bool = False,
     348        pad: int = 2,
     349        indent: int = 2,
     350        normalize_functions: t.Optional[str | bool] = None,
     351        unsupported_level: ErrorLevel = ErrorLevel.WARN,
     352        max_unsupported: int = 3,
     353        leading_comma: bool = False,
     354        max_text_width: int = 80,
     355        comments: bool = True,
     356    ):
     357        import sqlglot
     358
     359        self.pretty = pretty if pretty is not None else sqlglot.pretty
     360        self.identify = identify
     361        self.normalize = normalize
     362        self.pad = pad
     363        self._indent = indent
     364        self.unsupported_level = unsupported_level
     365        self.max_unsupported = max_unsupported
     366        self.leading_comma = leading_comma
     367        self.max_text_width = max_text_width
     368        self.comments = comments
     369
     370        # This is both a Dialect property and a Generator argument, so we prioritize the latter
     371        self.normalize_functions = (
     372            self.NORMALIZE_FUNCTIONS if normalize_functions is None else normalize_functions
     373        )
     374
     375        self.unsupported_messages: t.List[str] = []
     376        self._escaped_quote_end: str = self.STRING_ESCAPE + self.QUOTE_END
     377        self._escaped_identifier_end: str = self.IDENTIFIER_ESCAPE + self.IDENTIFIER_END
     378        self._cache: t.Optional[t.Dict[int, str]] = None
     379
     380    def generate(
     381        self,
     382        expression: t.Optional[exp.Expression],
     383        cache: t.Optional[t.Dict[int, str]] = None,
     384    ) -> str:
     385        """
     386        Generates the SQL string corresponding to the given syntax tree.
     387
     388        Args:
     389            expression: The syntax tree.
     390            cache: An optional sql string cache. This leverages the hash of an Expression
     391                which can be slow to compute, so only use it if you set _hash on each node.
     392
     393        Returns:
     394            The SQL string corresponding to `expression`.
     395        """
     396        if cache is not None:
     397            self._cache = cache
     398
     399        self.unsupported_messages = []
     400        sql = self.sql(expression).strip()
     401        self._cache = None
     402
     403        if self.unsupported_level == ErrorLevel.IGNORE:
     404            return sql
     405
     406        if self.unsupported_level == ErrorLevel.WARN:
     407            for msg in self.unsupported_messages:
     408                logger.warning(msg)
     409        elif self.unsupported_level == ErrorLevel.RAISE and self.unsupported_messages:
     410            raise UnsupportedError(concat_messages(self.unsupported_messages, self.max_unsupported))
     411
     412        if self.pretty:
     413            sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
     414        return sql
     415
     416    def unsupported(self, message: str) -> None:
     417        if self.unsupported_level == ErrorLevel.IMMEDIATE:
     418            raise UnsupportedError(message)
     419        self.unsupported_messages.append(message)
     420
     421    def sep(self, sep: str = " ") -> str:
     422        return f"{sep.strip()}\n" if self.pretty else sep
     423
     424    def seg(self, sql: str, sep: str = " ") -> str:
     425        return f"{self.sep(sep)}{sql}"
     426
     427    def pad_comment(self, comment: str) -> str:
     428        comment = " " + comment if comment[0].strip() else comment
     429        comment = comment + " " if comment[-1].strip() else comment
     430        return comment
     431
     432    def maybe_comment(
     433        self,
     434        sql: str,
     435        expression: t.Optional[exp.Expression] = None,
     436        comments: t.Optional[t.List[str]] = None,
     437    ) -> str:
     438        comments = (
     439            ((expression and expression.comments) if comments is None else comments)  # type: ignore
     440            if self.comments
     441            else None
     442        )
     443
     444        if not comments or isinstance(expression, exp.Binary):
     445            return sql
     446
     447        sep = "\n" if self.pretty else " "
     448        comments_sql = sep.join(
     449            f"/*{self.pad_comment(comment)}*/" for comment in comments if comment
     450        )
     451
     452        if not comments_sql:
     453            return sql
     454
     455        if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
     456            return (
     457                f"{self.sep()}{comments_sql}{sql}"
     458                if sql[0].isspace()
     459                else f"{comments_sql}{self.sep()}{sql}"
     460            )
     461
     462        return f"{sql} {comments_sql}"
     463
     464    def wrap(self, expression: exp.Expression | str) -> str:
     465        this_sql = self.indent(
     466            self.sql(expression)
     467            if isinstance(expression, (exp.Select, exp.Union))
     468            else self.sql(expression, "this"),
     469            level=1,
     470            pad=0,
     471        )
     472        return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"
     473
     474    def no_identify(self, func: t.Callable[..., str], *args, **kwargs) -> str:
     475        original = self.identify
     476        self.identify = False
     477        result = func(*args, **kwargs)
     478        self.identify = original
     479        return result
     480
     481    def normalize_func(self, name: str) -> str:
     482        if self.normalize_functions == "upper" or self.normalize_functions is True:
     483            return name.upper()
     484        if self.normalize_functions == "lower":
     485            return name.lower()
     486        return name
     487
     488    def indent(
     489        self,
     490        sql: str,
     491        level: int = 0,
     492        pad: t.Optional[int] = None,
     493        skip_first: bool = False,
     494        skip_last: bool = False,
     495    ) -> str:
     496        if not self.pretty:
     497            return sql
     498
     499        pad = self.pad if pad is None else pad
     500        lines = sql.split("\n")
     501
     502        return "\n".join(
     503            line
     504            if (skip_first and i == 0) or (skip_last and i == len(lines) - 1)
     505            else f"{' ' * (level * self._indent + pad)}{line}"
     506            for i, line in enumerate(lines)
     507        )
     508
     509    def sql(
     510        self,
     511        expression: t.Optional[str | exp.Expression],
     512        key: t.Optional[str] = None,
     513        comment: bool = True,
     514    ) -> str:
     515        if not expression:
     516            return ""
     517
     518        if isinstance(expression, str):
     519            return expression
     520
     521        if key:
     522            value = expression.args.get(key)
     523            if value:
     524                return self.sql(value)
     525            return ""
     526
     527        if self._cache is not None:
     528            expression_id = hash(expression)
     529
     530            if expression_id in self._cache:
     531                return self._cache[expression_id]
     532
     533        transform = self.TRANSFORMS.get(expression.__class__)
     534
     535        if callable(transform):
     536            sql = transform(self, expression)
     537        elif transform:
     538            sql = transform
     539        elif isinstance(expression, exp.Expression):
     540            exp_handler_name = f"{expression.key}_sql"
     541
     542            if hasattr(self, exp_handler_name):
     543                sql = getattr(self, exp_handler_name)(expression)
     544            elif isinstance(expression, exp.Func):
     545                sql = self.function_fallback_sql(expression)
     546            elif isinstance(expression, exp.Property):
     547                sql = self.property_sql(expression)
     548            else:
     549                raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
     550        else:
     551            raise ValueError(f"Expected an Expression. Received {type(expression)}: {expression}")
     552
     553        sql = self.maybe_comment(sql, expression) if self.comments and comment else sql
     554
     555        if self._cache is not None:
     556            self._cache[expression_id] = sql
     557        return sql
     558
     559    def uncache_sql(self, expression: exp.Uncache) -> str:
     560        table = self.sql(expression, "this")
     561        exists_sql = " IF EXISTS" if expression.args.get("exists") else ""
     562        return f"UNCACHE TABLE{exists_sql} {table}"
     563
     564    def cache_sql(self, expression: exp.Cache) -> str:
     565        lazy = " LAZY" if expression.args.get("lazy") else ""
     566        table = self.sql(expression, "this")
     567        options = expression.args.get("options")
     568        options = f" OPTIONS({self.sql(options[0])} = {self.sql(options[1])})" if options else ""
     569        sql = self.sql(expression, "expression")
     570        sql = f" AS{self.sep()}{sql}" if sql else ""
     571        sql = f"CACHE{lazy} TABLE {table}{options}{sql}"
     572        return self.prepend_ctes(expression, sql)
     573
     574    def characterset_sql(self, expression: exp.CharacterSet) -> str:
     575        if isinstance(expression.parent, exp.Cast):
     576            return f"CHAR CHARACTER SET {self.sql(expression, 'this')}"
     577        default = "DEFAULT " if expression.args.get("default") else ""
     578        return f"{default}CHARACTER SET={self.sql(expression, 'this')}"
     579
     580    def column_sql(self, expression: exp.Column) -> str:
     581        join_mark = " (+)" if expression.args.get("join_mark") else ""
     582
     583        if join_mark and not self.COLUMN_JOIN_MARKS_SUPPORTED:
     584            join_mark = ""
     585            self.unsupported("Outer join syntax using the (+) operator is not supported.")
     586
     587        column = ".".join(
     588            self.sql(part)
     589            for part in (
     590                expression.args.get("catalog"),
     591                expression.args.get("db"),
     592                expression.args.get("table"),
     593                expression.args.get("this"),
     594            )
     595            if part
     596        )
     597
     598        return f"{column}{join_mark}"
     599
     600    def columnposition_sql(self, expression: exp.ColumnPosition) -> str:
     601        this = self.sql(expression, "this")
     602        this = f" {this}" if this else ""
     603        position = self.sql(expression, "position")
     604        return f"{position}{this}"
     605
     606    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
     607        column = self.sql(expression, "this")
     608        kind = self.sql(expression, "kind")
     609        constraints = self.expressions(expression, key="constraints", sep=" ", flat=True)
     610        exists = "IF NOT EXISTS " if expression.args.get("exists") else ""
     611        kind = f"{sep}{kind}" if kind else ""
     612        constraints = f" {constraints}" if constraints else ""
     613        position = self.sql(expression, "position")
     614        position = f" {position}" if position else ""
     615
     616        return f"{exists}{column}{kind}{constraints}{position}"
     617
     618    def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str:
     619        this = self.sql(expression, "this")
     620        kind_sql = self.sql(expression, "kind").strip()
     621        return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql
     622
     623    def autoincrementcolumnconstraint_sql(self, _) -> str:
     624        return self.token_sql(TokenType.AUTO_INCREMENT)
     625
     626    def compresscolumnconstraint_sql(self, expression: exp.CompressColumnConstraint) -> str:
     627        if isinstance(expression.this, list):
     628            this = self.wrap(self.expressions(expression, key="this", flat=True))
     629        else:
     630            this = self.sql(expression, "this")
     631
     632        return f"COMPRESS {this}"
     633
     634    def generatedasidentitycolumnconstraint_sql(
     635        self, expression: exp.GeneratedAsIdentityColumnConstraint
     636    ) -> str:
     637        this = ""
     638        if expression.this is not None:
     639            on_null = "ON NULL " if expression.args.get("on_null") else ""
     640            this = " ALWAYS " if expression.this else f" BY DEFAULT {on_null}"
     641
     642        start = expression.args.get("start")
     643        start = f"START WITH {start}" if start else ""
     644        increment = expression.args.get("increment")
     645        increment = f" INCREMENT BY {increment}" if increment else ""
     646        minvalue = expression.args.get("minvalue")
     647        minvalue = f" MINVALUE {minvalue}" if minvalue else ""
     648        maxvalue = expression.args.get("maxvalue")
     649        maxvalue = f" MAXVALUE {maxvalue}" if maxvalue else ""
     650        cycle = expression.args.get("cycle")
     651        cycle_sql = ""
     652
     653        if cycle is not None:
     654            cycle_sql = f"{' NO' if not cycle else ''} CYCLE"
     655            cycle_sql = cycle_sql.strip() if not start and not increment else cycle_sql
     656
     657        sequence_opts = ""
     658        if start or increment or cycle_sql:
     659            sequence_opts = f"{start}{increment}{minvalue}{maxvalue}{cycle_sql}"
     660            sequence_opts = f" ({sequence_opts.strip()})"
     661
     662        expr = self.sql(expression, "expression")
     663        expr = f"({expr})" if expr else "IDENTITY"
     664
     665        return f"GENERATED{this}AS {expr}{sequence_opts}"
     666
     667    def notnullcolumnconstraint_sql(self, expression: exp.NotNullColumnConstraint) -> str:
     668        return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL"
     669
     670    def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
     671        desc = expression.args.get("desc")
     672        if desc is not None:
     673            return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
     674        return f"PRIMARY KEY"
     675
     676    def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
     677        this = self.sql(expression, "this")
     678        this = f" {this}" if this else ""
     679        return f"UNIQUE{this}"
     680
     681    def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
     682        return self.sql(expression, "this")
     683
     684    def create_sql(self, expression: exp.Create) -> str:
     685        kind = self.sql(expression, "kind").upper()
     686        properties = expression.args.get("properties")
     687        properties_locs = self.locate_properties(properties) if properties else defaultdict()
     688
     689        this = self.createable_sql(expression, properties_locs)
     690
     691        properties_sql = ""
     692        if properties_locs.get(exp.Properties.Location.POST_SCHEMA) or properties_locs.get(
     693            exp.Properties.Location.POST_WITH
     694        ):
     695            properties_sql = self.sql(
     696                exp.Properties(
     697                    expressions=[
     698                        *properties_locs[exp.Properties.Location.POST_SCHEMA],
     699                        *properties_locs[exp.Properties.Location.POST_WITH],
     700                    ]
     701                )
     702            )
     703
     704        begin = " BEGIN" if expression.args.get("begin") else ""
     705        expression_sql = self.sql(expression, "expression")
     706        if expression_sql:
     707            expression_sql = f"{begin}{self.sep()}{expression_sql}"
     708
     709            if self.CREATE_FUNCTION_RETURN_AS or not isinstance(expression.expression, exp.Return):
     710                if properties_locs.get(exp.Properties.Location.POST_ALIAS):
     711                    postalias_props_sql = self.properties(
     712                        exp.Properties(
     713                            expressions=properties_locs[exp.Properties.Location.POST_ALIAS]
     714                        ),
     715                        wrapped=False,
     716                    )
     717                    expression_sql = f" AS {postalias_props_sql}{expression_sql}"
     718                else:
     719                    expression_sql = f" AS{expression_sql}"
     720
     721        postindex_props_sql = ""
     722        if properties_locs.get(exp.Properties.Location.POST_INDEX):
     723            postindex_props_sql = self.properties(
     724                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_INDEX]),
     725                wrapped=False,
     726                prefix=" ",
     727            )
     728
     729        indexes = self.expressions(expression, key="indexes", indent=False, sep=" ")
     730        indexes = f" {indexes}" if indexes else ""
     731        index_sql = indexes + postindex_props_sql
     732
     733        replace = " OR REPLACE" if expression.args.get("replace") else ""
     734        unique = " UNIQUE" if expression.args.get("unique") else ""
     735
     736        postcreate_props_sql = ""
     737        if properties_locs.get(exp.Properties.Location.POST_CREATE):
     738            postcreate_props_sql = self.properties(
     739                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_CREATE]),
     740                sep=" ",
     741                prefix=" ",
     742                wrapped=False,
     743            )
     744
     745        modifiers = "".join((replace, unique, postcreate_props_sql))
     746
     747        postexpression_props_sql = ""
     748        if properties_locs.get(exp.Properties.Location.POST_EXPRESSION):
     749            postexpression_props_sql = self.properties(
     750                exp.Properties(
     751                    expressions=properties_locs[exp.Properties.Location.POST_EXPRESSION]
     752                ),
     753                sep=" ",
     754                prefix=" ",
     755                wrapped=False,
     756            )
     757
     758        exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else ""
     759        no_schema_binding = (
     760            " WITH NO SCHEMA BINDING" if expression.args.get("no_schema_binding") else ""
     761        )
     762
     763        clone = self.sql(expression, "clone")
     764        clone = f" {clone}" if clone else ""
     765
     766        expression_sql = f"CREATE{modifiers} {kind}{exists_sql} {this}{properties_sql}{expression_sql}{postexpression_props_sql}{index_sql}{no_schema_binding}{clone}"
     767        return self.prepend_ctes(expression, expression_sql)
     768
     769    def clone_sql(self, expression: exp.Clone) -> str:
     770        this = self.sql(expression, "this")
     771        when = self.sql(expression, "when")
     772
     773        if when:
     774            kind = self.sql(expression, "kind")
     775            expr = self.sql(expression, "expression")
     776            return f"CLONE {this} {when} ({kind} => {expr})"
     777
     778        return f"CLONE {this}"
     779
     780    def describe_sql(self, expression: exp.Describe) -> str:
     781        return f"DESCRIBE {self.sql(expression, 'this')}"
     782
     783    def prepend_ctes(self, expression: exp.Expression, sql: str) -> str:
     784        with_ = self.sql(expression, "with")
     785        if with_:
     786            sql = f"{with_}{self.sep()}{sql}"
     787        return sql
     788
     789    def with_sql(self, expression: exp.With) -> str:
     790        sql = self.expressions(expression, flat=True)
     791        recursive = "RECURSIVE " if expression.args.get("recursive") else ""
     792
     793        return f"WITH {recursive}{sql}"
     794
     795    def cte_sql(self, expression: exp.CTE) -> str:
     796        alias = self.sql(expression, "alias")
     797        return f"{alias} AS {self.wrap(expression)}"
     798
     799    def tablealias_sql(self, expression: exp.TableAlias) -> str:
     800        alias = self.sql(expression, "this")
     801        columns = self.expressions(expression, key="columns", flat=True)
     802        columns = f"({columns})" if columns else ""
     803        return f"{alias}{columns}"
     804
     805    def bitstring_sql(self, expression: exp.BitString) -> str:
     806        this = self.sql(expression, "this")
     807        if self.BIT_START:
     808            return f"{self.BIT_START}{this}{self.BIT_END}"
     809        return f"{int(this, 2)}"
     810
     811    def hexstring_sql(self, expression: exp.HexString) -> str:
     812        this = self.sql(expression, "this")
     813        if self.HEX_START:
     814            return f"{self.HEX_START}{this}{self.HEX_END}"
     815        return f"{int(this, 16)}"
     816
     817    def bytestring_sql(self, expression: exp.ByteString) -> str:
     818        this = self.sql(expression, "this")
     819        if self.BYTE_START:
     820            return f"{self.BYTE_START}{this}{self.BYTE_END}"
     821        return this
     822
     823    def rawstring_sql(self, expression: exp.RawString) -> str:
     824        string = self.escape_str(expression.this.replace("\\", "\\\\"))
     825        return f"{self.QUOTE_START}{string}{self.QUOTE_END}"
     826
     827    def datatypesize_sql(self, expression: exp.DataTypeSize) -> str:
     828        this = self.sql(expression, "this")
     829        specifier = self.sql(expression, "expression")
     830        specifier = f" {specifier}" if specifier else ""
     831        return f"{this}{specifier}"
     832
     833    def datatype_sql(self, expression: exp.DataType) -> str:
     834        type_value = expression.this
     835        type_sql = (
     836            self.TYPE_MAPPING.get(type_value, type_value.value)
     837            if isinstance(type_value, exp.DataType.Type)
     838            else type_value
     839        )
     840        nested = ""
     841        interior = self.expressions(expression, flat=True)
     842        values = ""
     843        if interior:
     844            if expression.args.get("nested"):
     845                nested = f"{self.STRUCT_DELIMITER[0]}{interior}{self.STRUCT_DELIMITER[1]}"
     846                if expression.args.get("values") is not None:
     847                    delimiters = ("[", "]") if type_value == exp.DataType.Type.ARRAY else ("(", ")")
     848                    values = self.expressions(expression, key="values", flat=True)
     849                    values = f"{delimiters[0]}{values}{delimiters[1]}"
     850            else:
     851                nested = f"({interior})"
     852
     853        return f"{type_sql}{nested}{values}"
     854
     855    def directory_sql(self, expression: exp.Directory) -> str:
     856        local = "LOCAL " if expression.args.get("local") else ""
     857        row_format = self.sql(expression, "row_format")
     858        row_format = f" {row_format}" if row_format else ""
     859        return f"{local}DIRECTORY {self.sql(expression, 'this')}{row_format}"
     860
     861    def delete_sql(self, expression: exp.Delete) -> str:
     862        this = self.sql(expression, "this")
     863        this = f" FROM {this}" if this else ""
     864        using = self.sql(expression, "using")
     865        using = f" USING {using}" if using else ""
     866        where = self.sql(expression, "where")
     867        returning = self.sql(expression, "returning")
     868        limit = self.sql(expression, "limit")
     869        tables = self.expressions(expression, key="tables")
     870        tables = f" {tables}" if tables else ""
     871        if self.RETURNING_END:
     872            expression_sql = f"{this}{using}{where}{returning}{limit}"
     873        else:
     874            expression_sql = f"{returning}{this}{using}{where}{limit}"
     875        return self.prepend_ctes(expression, f"DELETE{tables}{expression_sql}")
     876
     877    def drop_sql(self, expression: exp.Drop) -> str:
     878        this = self.sql(expression, "this")
     879        kind = expression.args["kind"]
     880        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
     881        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
     882        materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
     883        cascade = " CASCADE" if expression.args.get("cascade") else ""
     884        constraints = " CONSTRAINTS" if expression.args.get("constraints") else ""
     885        purge = " PURGE" if expression.args.get("purge") else ""
     886        return (
     887            f"DROP{temporary}{materialized} {kind}{exists_sql}{this}{cascade}{constraints}{purge}"
     888        )
     889
     890    def except_sql(self, expression: exp.Except) -> str:
     891        return self.prepend_ctes(
     892            expression,
     893            self.set_operation(expression, self.except_op(expression)),
     894        )
     895
     896    def except_op(self, expression: exp.Except) -> str:
     897        return f"EXCEPT{'' if expression.args.get('distinct') else ' ALL'}"
     898
     899    def fetch_sql(self, expression: exp.Fetch) -> str:
     900        direction = expression.args.get("direction")
     901        direction = f" {direction.upper()}" if direction else ""
     902        count = expression.args.get("count")
     903        count = f" {count}" if count else ""
     904        if expression.args.get("percent"):
     905            count = f"{count} PERCENT"
     906        with_ties_or_only = "WITH TIES" if expression.args.get("with_ties") else "ONLY"
     907        return f"{self.seg('FETCH')}{direction}{count} ROWS {with_ties_or_only}"
     908
     909    def filter_sql(self, expression: exp.Filter) -> str:
     910        this = self.sql(expression, "this")
     911        where = self.sql(expression, "expression")[1:]  # where has a leading space
     912        return f"{this} FILTER({where})"
     913
     914    def hint_sql(self, expression: exp.Hint) -> str:
     915        if not self.QUERY_HINTS:
     916            self.unsupported("Hints are not supported")
     917            return ""
     918
     919        return f" /*+ {self.expressions(expression, sep=self.QUERY_HINT_SEP).strip()} */"
     920
     921    def index_sql(self, expression: exp.Index) -> str:
     922        unique = "UNIQUE " if expression.args.get("unique") else ""
     923        primary = "PRIMARY " if expression.args.get("primary") else ""
     924        amp = "AMP " if expression.args.get("amp") else ""
     925        name = self.sql(expression, "this")
     926        name = f"{name} " if name else ""
     927        table = self.sql(expression, "table")
     928        table = f"{self.INDEX_ON} {table} " if table else ""
     929        using = self.sql(expression, "using")
     930        using = f"USING {using} " if using else ""
     931        index = "INDEX " if not table else ""
     932        columns = self.expressions(expression, key="columns", flat=True)
     933        columns = f"({columns})" if columns else ""
     934        partition_by = self.expressions(expression, key="partition_by", flat=True)
     935        partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
     936        return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}"
     937
     938    def identifier_sql(self, expression: exp.Identifier) -> str:
     939        text = expression.name
     940        lower = text.lower()
     941        text = lower if self.normalize and not expression.quoted else text
     942        text = text.replace(self.IDENTIFIER_END, self._escaped_identifier_end)
     943        if (
     944            expression.quoted
     945            or self.can_identify(text, self.identify)
     946            or lower in self.RESERVED_KEYWORDS
     947            or (not self.IDENTIFIERS_CAN_START_WITH_DIGIT and text[:1].isdigit())
     948        ):
     949            text = f"{self.IDENTIFIER_START}{text}{self.IDENTIFIER_END}"
     950        return text
     951
     952    def inputoutputformat_sql(self, expression: exp.InputOutputFormat) -> str:
     953        input_format = self.sql(expression, "input_format")
     954        input_format = f"INPUTFORMAT {input_format}" if input_format else ""
     955        output_format = self.sql(expression, "output_format")
     956        output_format = f"OUTPUTFORMAT {output_format}" if output_format else ""
     957        return self.sep().join((input_format, output_format))
     958
     959    def national_sql(self, expression: exp.National, prefix: str = "N") -> str:
     960        string = self.sql(exp.Literal.string(expression.name))
     961        return f"{prefix}{string}"
     962
     963    def partition_sql(self, expression: exp.Partition) -> str:
     964        return f"PARTITION({self.expressions(expression, flat=True)})"
     965
     966    def properties_sql(self, expression: exp.Properties) -> str:
     967        root_properties = []
     968        with_properties = []
     969
     970        for p in expression.expressions:
     971            p_loc = self.PROPERTIES_LOCATION[p.__class__]
     972            if p_loc == exp.Properties.Location.POST_WITH:
     973                with_properties.append(p.copy())
     974            elif p_loc == exp.Properties.Location.POST_SCHEMA:
     975                root_properties.append(p.copy())
     976
     977        return self.root_properties(
     978            exp.Properties(expressions=root_properties)
     979        ) + self.with_properties(exp.Properties(expressions=with_properties))
     980
     981    def root_properties(self, properties: exp.Properties) -> str:
     982        if properties.expressions:
     983            return self.sep() + self.expressions(properties, indent=False, sep=" ")
     984        return ""
     985
     986    def properties(
     987        self,
     988        properties: exp.Properties,
     989        prefix: str = "",
     990        sep: str = ", ",
     991        suffix: str = "",
     992        wrapped: bool = True,
     993    ) -> str:
     994        if properties.expressions:
     995            expressions = self.expressions(properties, sep=sep, indent=False)
     996            if expressions:
     997                expressions = self.wrap(expressions) if wrapped else expressions
     998                return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
     999        return ""
    1000
    1001    def with_properties(self, properties: exp.Properties) -> str:
    1002        return self.properties(properties, prefix=self.seg("WITH"))
    1003
    1004    def locate_properties(self, properties: exp.Properties) -> t.DefaultDict:
    1005        properties_locs = defaultdict(list)
    1006        for p in properties.expressions:
    1007            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    1008            if p_loc != exp.Properties.Location.UNSUPPORTED:
    1009                properties_locs[p_loc].append(p.copy())
    1010            else:
    1011                self.unsupported(f"Unsupported property {p.key}")
    1012
    1013        return properties_locs
    1014
    1015    def property_sql(self, expression: exp.Property) -> str:
    1016        property_cls = expression.__class__
    1017        if property_cls == exp.Property:
    1018            return f"{expression.name}={self.sql(expression, 'value')}"
    1019
    1020        property_name = exp.Properties.PROPERTY_TO_NAME.get(property_cls)
    1021        if not property_name:
    1022            self.unsupported(f"Unsupported property {expression.key}")
    1023
    1024        return f"{property_name}={self.sql(expression, 'this')}"
    1025
    1026    def likeproperty_sql(self, expression: exp.LikeProperty) -> str:
    1027        options = " ".join(f"{e.name} {self.sql(e, 'value')}" for e in expression.expressions)
    1028        options = f" {options}" if options else ""
    1029        return f"LIKE {self.sql(expression, 'this')}{options}"
    1030
    1031    def fallbackproperty_sql(self, expression: exp.FallbackProperty) -> str:
    1032        no = "NO " if expression.args.get("no") else ""
    1033        protection = " PROTECTION" if expression.args.get("protection") else ""
    1034        return f"{no}FALLBACK{protection}"
    1035
    1036    def journalproperty_sql(self, expression: exp.JournalProperty) -> str:
    1037        no = "NO " if expression.args.get("no") else ""
    1038        local = expression.args.get("local")
    1039        local = f"{local} " if local else ""
    1040        dual = "DUAL " if expression.args.get("dual") else ""
    1041        before = "BEFORE " if expression.args.get("before") else ""
    1042        after = "AFTER " if expression.args.get("after") else ""
    1043        return f"{no}{local}{dual}{before}{after}JOURNAL"
    1044
    1045    def freespaceproperty_sql(self, expression: exp.FreespaceProperty) -> str:
    1046        freespace = self.sql(expression, "this")
    1047        percent = " PERCENT" if expression.args.get("percent") else ""
    1048        return f"FREESPACE={freespace}{percent}"
    1049
    1050    def checksumproperty_sql(self, expression: exp.ChecksumProperty) -> str:
    1051        if expression.args.get("default"):
    1052            property = "DEFAULT"
    1053        elif expression.args.get("on"):
    1054            property = "ON"
    1055        else:
    1056            property = "OFF"
    1057        return f"CHECKSUM={property}"
    1058
    1059    def mergeblockratioproperty_sql(self, expression: exp.MergeBlockRatioProperty) -> str:
    1060        if expression.args.get("no"):
    1061            return "NO MERGEBLOCKRATIO"
    1062        if expression.args.get("default"):
    1063            return "DEFAULT MERGEBLOCKRATIO"
    1064
    1065        percent = " PERCENT" if expression.args.get("percent") else ""
    1066        return f"MERGEBLOCKRATIO={self.sql(expression, 'this')}{percent}"
    1067
    1068    def datablocksizeproperty_sql(self, expression: exp.DataBlocksizeProperty) -> str:
    1069        default = expression.args.get("default")
    1070        minimum = expression.args.get("minimum")
    1071        maximum = expression.args.get("maximum")
    1072        if default or minimum or maximum:
    1073            if default:
    1074                prop = "DEFAULT"
    1075            elif minimum:
    1076                prop = "MINIMUM"
    1077            else:
    1078                prop = "MAXIMUM"
    1079            return f"{prop} DATABLOCKSIZE"
    1080        units = expression.args.get("units")
    1081        units = f" {units}" if units else ""
    1082        return f"DATABLOCKSIZE={self.sql(expression, 'size')}{units}"
    1083
    1084    def blockcompressionproperty_sql(self, expression: exp.BlockCompressionProperty) -> str:
    1085        autotemp = expression.args.get("autotemp")
    1086        always = expression.args.get("always")
    1087        default = expression.args.get("default")
    1088        manual = expression.args.get("manual")
    1089        never = expression.args.get("never")
    1090
    1091        if autotemp is not None:
    1092            prop = f"AUTOTEMP({self.expressions(autotemp)})"
    1093        elif always:
    1094            prop = "ALWAYS"
    1095        elif default:
    1096            prop = "DEFAULT"
    1097        elif manual:
    1098            prop = "MANUAL"
    1099        elif never:
    1100            prop = "NEVER"
    1101        return f"BLOCKCOMPRESSION={prop}"
    1102
    1103    def isolatedloadingproperty_sql(self, expression: exp.IsolatedLoadingProperty) -> str:
    1104        no = expression.args.get("no")
    1105        no = " NO" if no else ""
    1106        concurrent = expression.args.get("concurrent")
    1107        concurrent = " CONCURRENT" if concurrent else ""
    1108
    1109        for_ = ""
    1110        if expression.args.get("for_all"):
    1111            for_ = " FOR ALL"
    1112        elif expression.args.get("for_insert"):
    1113            for_ = " FOR INSERT"
    1114        elif expression.args.get("for_none"):
    1115            for_ = " FOR NONE"
    1116        return f"WITH{no}{concurrent} ISOLATED LOADING{for_}"
    1117
    1118    def lockingproperty_sql(self, expression: exp.LockingProperty) -> str:
    1119        kind = expression.args.get("kind")
    1120        this = f" {self.sql(expression, 'this')}" if expression.this else ""
    1121        for_or_in = expression.args.get("for_or_in")
    1122        lock_type = expression.args.get("lock_type")
    1123        override = " OVERRIDE" if expression.args.get("override") else ""
    1124        return f"LOCKING {kind}{this} {for_or_in} {lock_type}{override}"
    1125
    1126    def withdataproperty_sql(self, expression: exp.WithDataProperty) -> str:
    1127        data_sql = f"WITH {'NO ' if expression.args.get('no') else ''}DATA"
    1128        statistics = expression.args.get("statistics")
    1129        statistics_sql = ""
    1130        if statistics is not None:
    1131            statistics_sql = f" AND {'NO ' if not statistics else ''}STATISTICS"
    1132        return f"{data_sql}{statistics_sql}"
    1133
    1134    def insert_sql(self, expression: exp.Insert) -> str:
    1135        overwrite = expression.args.get("overwrite")
    1136
    1137        if isinstance(expression.this, exp.Directory):
    1138            this = " OVERWRITE" if overwrite else " INTO"
    1139        else:
    1140            this = " OVERWRITE TABLE" if overwrite else " INTO"
    1141
    1142        alternative = expression.args.get("alternative")
    1143        alternative = f" OR {alternative}" if alternative else ""
    1144        ignore = " IGNORE" if expression.args.get("ignore") else ""
    1145
    1146        this = f"{this} {self.sql(expression, 'this')}"
    1147
    1148        exists = " IF EXISTS" if expression.args.get("exists") else ""
    1149        partition_sql = (
    1150            f" {self.sql(expression, 'partition')}" if expression.args.get("partition") else ""
    1151        )
    1152        where = self.sql(expression, "where")
    1153        where = f"{self.sep()}REPLACE WHERE {where}" if where else ""
    1154        expression_sql = f"{self.sep()}{self.sql(expression, 'expression')}"
    1155        conflict = self.sql(expression, "conflict")
    1156        returning = self.sql(expression, "returning")
    1157
    1158        if self.RETURNING_END:
    1159            expression_sql = f"{expression_sql}{conflict}{returning}"
    1160        else:
    1161            expression_sql = f"{returning}{expression_sql}{conflict}"
    1162
    1163        sql = f"INSERT{alternative}{ignore}{this}{exists}{partition_sql}{where}{expression_sql}"
    1164        return self.prepend_ctes(expression, sql)
    1165
    1166    def intersect_sql(self, expression: exp.Intersect) -> str:
    1167        return self.prepend_ctes(
    1168            expression,
    1169            self.set_operation(expression, self.intersect_op(expression)),
    1170        )
    1171
    1172    def intersect_op(self, expression: exp.Intersect) -> str:
    1173        return f"INTERSECT{'' if expression.args.get('distinct') else ' ALL'}"
    1174
    1175    def introducer_sql(self, expression: exp.Introducer) -> str:
    1176        return f"{self.sql(expression, 'this')} {self.sql(expression, 'expression')}"
    1177
    1178    def pseudotype_sql(self, expression: exp.PseudoType) -> str:
    1179        return expression.name.upper()
    1180
    1181    def onconflict_sql(self, expression: exp.OnConflict) -> str:
    1182        conflict = "ON DUPLICATE KEY" if expression.args.get("duplicate") else "ON CONFLICT"
    1183        constraint = self.sql(expression, "constraint")
    1184        if constraint:
    1185            constraint = f"ON CONSTRAINT {constraint}"
    1186        key = self.expressions(expression, key="key", flat=True)
    1187        do = "" if expression.args.get("duplicate") else " DO "
    1188        nothing = "NOTHING" if expression.args.get("nothing") else ""
    1189        expressions = self.expressions(expression, flat=True)
    1190        set_keyword = "SET " if self.DUPLICATE_KEY_UPDATE_WITH_SET else ""
    1191        if expressions:
    1192            expressions = f"UPDATE {set_keyword}{expressions}"
    1193        return f"{self.seg(conflict)} {constraint}{key}{do}{nothing}{expressions}"
    1194
    1195    def returning_sql(self, expression: exp.Returning) -> str:
    1196        return f"{self.seg('RETURNING')} {self.expressions(expression, flat=True)}"
    1197
    1198    def rowformatdelimitedproperty_sql(self, expression: exp.RowFormatDelimitedProperty) -> str:
    1199        fields = expression.args.get("fields")
    1200        fields = f" FIELDS TERMINATED BY {fields}" if fields else ""
    1201        escaped = expression.args.get("escaped")
    1202        escaped = f" ESCAPED BY {escaped}" if escaped else ""
    1203        items = expression.args.get("collection_items")
    1204        items = f" COLLECTION ITEMS TERMINATED BY {items}" if items else ""
    1205        keys = expression.args.get("map_keys")
    1206        keys = f" MAP KEYS TERMINATED BY {keys}" if keys else ""
    1207        lines = expression.args.get("lines")
    1208        lines = f" LINES TERMINATED BY {lines}" if lines else ""
    1209        null = expression.args.get("null")
    1210        null = f" NULL DEFINED AS {null}" if null else ""
    1211        return f"ROW FORMAT DELIMITED{fields}{escaped}{items}{keys}{lines}{null}"
    1212
    1213    def withtablehint_sql(self, expression: exp.WithTableHint) -> str:
    1214        return f"WITH ({self.expressions(expression, flat=True)})"
    1215
    1216    def indextablehint_sql(self, expression: exp.IndexTableHint) -> str:
    1217        this = f"{self.sql(expression, 'this')} INDEX"
    1218        target = self.sql(expression, "target")
    1219        target = f" FOR {target}" if target else ""
    1220        return f"{this}{target} ({self.expressions(expression, flat=True)})"
    1221
    1222    def table_sql(self, expression: exp.Table, sep: str = " AS ") -> str:
    1223        table = ".".join(
    1224            part
    1225            for part in [
    1226                self.sql(expression, "catalog"),
    1227                self.sql(expression, "db"),
    1228                self.sql(expression, "this"),
    1229            ]
    1230            if part
    1231        )
    1232
    1233        alias = self.sql(expression, "alias")
    1234        alias = f"{sep}{alias}" if alias else ""
    1235        hints = self.expressions(expression, key="hints", sep=" ")
    1236        hints = f" {hints}" if hints and self.TABLE_HINTS else ""
    1237        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1238        pivots = f" {pivots}" if pivots else ""
    1239        joins = self.expressions(expression, key="joins", sep="", skip_first=True)
    1240        laterals = self.expressions(expression, key="laterals", sep="")
    1241        system_time = expression.args.get("system_time")
    1242        system_time = f" {self.sql(expression, 'system_time')}" if system_time else ""
    1243
    1244        return f"{table}{system_time}{alias}{hints}{pivots}{joins}{laterals}"
    1245
    1246    def tablesample_sql(
    1247        self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    1248    ) -> str:
    1249        if self.ALIAS_POST_TABLESAMPLE and expression.this.alias:
    1250            table = expression.this.copy()
    1251            table.set("alias", None)
    1252            this = self.sql(table)
    1253            alias = f"{sep}{self.sql(expression.this, 'alias')}"
    1254        else:
    1255            this = self.sql(expression, "this")
    1256            alias = ""
    1257        method = self.sql(expression, "method")
    1258        method = f"{method.upper()} " if method and self.TABLESAMPLE_WITH_METHOD else ""
    1259        numerator = self.sql(expression, "bucket_numerator")
    1260        denominator = self.sql(expression, "bucket_denominator")
    1261        field = self.sql(expression, "bucket_field")
    1262        field = f" ON {field}" if field else ""
    1263        bucket = f"BUCKET {numerator} OUT OF {denominator}{field}" if numerator else ""
    1264        percent = self.sql(expression, "percent")
    1265        percent = f"{percent} PERCENT" if percent else ""
    1266        rows = self.sql(expression, "rows")
    1267        rows = f"{rows} ROWS" if rows else ""
    1268        size = self.sql(expression, "size")
    1269        if size and self.TABLESAMPLE_SIZE_IS_PERCENT:
    1270            size = f"{size} PERCENT"
    1271        seed = self.sql(expression, "seed")
    1272        seed = f" {seed_prefix} ({seed})" if seed else ""
    1273        kind = expression.args.get("kind", "TABLESAMPLE")
    1274        return f"{this} {kind} {method}({bucket}{percent}{rows}{size}){seed}{alias}"
    1275
    1276    def pivot_sql(self, expression: exp.Pivot) -> str:
    1277        expressions = self.expressions(expression, flat=True)
    1278
    1279        if expression.this:
    1280            this = self.sql(expression, "this")
    1281            on = f"{self.seg('ON')} {expressions}"
    1282            using = self.expressions(expression, key="using", flat=True)
    1283            using = f"{self.seg('USING')} {using}" if using else ""
    1284            group = self.sql(expression, "group")
    1285            return f"PIVOT {this}{on}{using}{group}"
    1286
    1287        alias = self.sql(expression, "alias")
    1288        alias = f" AS {alias}" if alias else ""
    1289        unpivot = expression.args.get("unpivot")
    1290        direction = "UNPIVOT" if unpivot else "PIVOT"
    1291        field = self.sql(expression, "field")
    1292        return f"{direction}({expressions} FOR {field}){alias}"
    1293
    1294    def tuple_sql(self, expression: exp.Tuple) -> str:
    1295        return f"({self.expressions(expression, flat=True)})"
    1296
    1297    def update_sql(self, expression: exp.Update) -> str:
    1298        this = self.sql(expression, "this")
    1299        set_sql = self.expressions(expression, flat=True)
    1300        from_sql = self.sql(expression, "from")
    1301        where_sql = self.sql(expression, "where")
    1302        returning = self.sql(expression, "returning")
    1303        limit = self.sql(expression, "limit")
    1304        if self.RETURNING_END:
    1305            expression_sql = f"{from_sql}{where_sql}{returning}{limit}"
    1306        else:
    1307            expression_sql = f"{returning}{from_sql}{where_sql}{limit}"
    1308        sql = f"UPDATE {this} SET {set_sql}{expression_sql}"
    1309        return self.prepend_ctes(expression, sql)
    1310
    1311    def values_sql(self, expression: exp.Values) -> str:
    1312        # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example
    1313        if self.VALUES_AS_TABLE or not expression.find_ancestor(exp.From, exp.Join):
    1314            args = self.expressions(expression)
    1315            alias = self.sql(expression, "alias")
    1316            values = f"VALUES{self.seg('')}{args}"
    1317            values = (
    1318                f"({values})"
    1319                if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
    1320                else values
    1321            )
    1322            return f"{values} AS {alias}" if alias else values
    1323
    1324        # Converts `VALUES...` expression into a series of select unions.
    1325        # Note: If you have a lot of unions then this will result in a large number of recursive statements to
    1326        # evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be
    1327        # very slow.
    1328        expression = expression.copy()
    1329        column_names = expression.alias and expression.args["alias"].columns
    1330
    1331        selects = []
    1332
    1333        for i, tup in enumerate(expression.expressions):
    1334            row = tup.expressions
    1335
    1336            if i == 0 and column_names:
    1337                row = [
    1338                    exp.alias_(value, column_name) for value, column_name in zip(row, column_names)
    1339                ]
    1340
    1341            selects.append(exp.Select(expressions=row))
    1342
    1343        subquery_expression: exp.Select | exp.Union = selects[0]
    1344        if len(selects) > 1:
    1345            for select in selects[1:]:
    1346                subquery_expression = exp.union(
    1347                    subquery_expression, select, distinct=False, copy=False
    1348                )
    1349
    1350        return self.subquery_sql(subquery_expression.subquery(expression.alias, copy=False))
    1351
    1352    def var_sql(self, expression: exp.Var) -> str:
    1353        return self.sql(expression, "this")
    1354
    1355    def into_sql(self, expression: exp.Into) -> str:
    1356        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    1357        unlogged = " UNLOGGED" if expression.args.get("unlogged") else ""
    1358        return f"{self.seg('INTO')}{temporary or unlogged} {self.sql(expression, 'this')}"
    1359
    1360    def from_sql(self, expression: exp.From) -> str:
    1361        return f"{self.seg('FROM')} {self.sql(expression, 'this')}"
    1362
    1363    def group_sql(self, expression: exp.Group) -> str:
    1364        group_by = self.op_expressions("GROUP BY", expression)
    1365
    1366        if expression.args.get("all"):
    1367            return f"{group_by} ALL"
    1368
    1369        grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
    1370        grouping_sets = (
    1371            f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
    1372        )
    1373
    1374        cube = expression.args.get("cube", [])
    1375        if seq_get(cube, 0) is True:
    1376            return f"{group_by}{self.seg('WITH CUBE')}"
    1377        else:
    1378            cube_sql = self.expressions(expression, key="cube", indent=False)
    1379            cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else ""
    1380
    1381        rollup = expression.args.get("rollup", [])
    1382        if seq_get(rollup, 0) is True:
    1383            return f"{group_by}{self.seg('WITH ROLLUP')}"
    1384        else:
    1385            rollup_sql = self.expressions(expression, key="rollup", indent=False)
    1386            rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else ""
    1387
    1388        groupings = csv(
    1389            grouping_sets,
    1390            cube_sql,
    1391            rollup_sql,
    1392            self.seg("WITH TOTALS") if expression.args.get("totals") else "",
    1393            sep=self.GROUPINGS_SEP,
    1394        )
    1395
    1396        if expression.args.get("expressions") and groupings:
    1397            group_by = f"{group_by}{self.GROUPINGS_SEP}"
    1398
    1399        return f"{group_by}{groupings}"
    1400
    1401    def having_sql(self, expression: exp.Having) -> str:
    1402        this = self.indent(self.sql(expression, "this"))
    1403        return f"{self.seg('HAVING')}{self.sep()}{this}"
    1404
    1405    def join_sql(self, expression: exp.Join) -> str:
    1406        op_sql = " ".join(
    1407            op
    1408            for op in (
    1409                expression.method,
    1410                "GLOBAL" if expression.args.get("global") else None,
    1411                expression.side,
    1412                expression.kind,
    1413                expression.hint if self.JOIN_HINTS else None,
    1414            )
    1415            if op
    1416        )
    1417        on_sql = self.sql(expression, "on")
    1418        using = expression.args.get("using")
    1419
    1420        if not on_sql and using:
    1421            on_sql = csv(*(self.sql(column) for column in using))
    1422
    1423        this_sql = self.sql(expression, "this")
    1424
    1425        if on_sql:
    1426            on_sql = self.indent(on_sql, skip_first=True)
    1427            space = self.seg(" " * self.pad) if self.pretty else " "
    1428            if using:
    1429                on_sql = f"{space}USING ({on_sql})"
    1430            else:
    1431                on_sql = f"{space}ON {on_sql}"
    1432        elif not op_sql:
    1433            return f", {this_sql}"
    1434
    1435        op_sql = f"{op_sql} JOIN" if op_sql else "JOIN"
    1436        return f"{self.seg(op_sql)} {this_sql}{on_sql}"
    1437
    1438    def lambda_sql(self, expression: exp.Lambda, arrow_sep: str = "->") -> str:
    1439        args = self.expressions(expression, flat=True)
    1440        args = f"({args})" if len(args.split(",")) > 1 else args
    1441        return f"{args} {arrow_sep} {self.sql(expression, 'this')}"
    1442
    1443    def lateral_sql(self, expression: exp.Lateral) -> str:
    1444        this = self.sql(expression, "this")
    1445
    1446        if isinstance(expression.this, exp.Subquery):
    1447            return f"LATERAL {this}"
    1448
    1449        if expression.args.get("view"):
    1450            alias = expression.args["alias"]
    1451            columns = self.expressions(alias, key="columns", flat=True)
    1452            table = f" {alias.name}" if alias.name else ""
    1453            columns = f" AS {columns}" if columns else ""
    1454            op_sql = self.seg(f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}")
    1455            return f"{op_sql}{self.sep()}{this}{table}{columns}"
    1456
    1457        alias = self.sql(expression, "alias")
    1458        alias = f" AS {alias}" if alias else ""
    1459        return f"LATERAL {this}{alias}"
    1460
    1461    def limit_sql(self, expression: exp.Limit, top: bool = False) -> str:
    1462        this = self.sql(expression, "this")
    1463        args = ", ".join(
    1464            sql
    1465            for sql in (
    1466                self.sql(expression, "offset"),
    1467                self.sql(expression, "expression"),
    1468            )
    1469            if sql
    1470        )
    1471        return f"{this}{self.seg('TOP' if top else 'LIMIT')} {args}"
    1472
    1473    def offset_sql(self, expression: exp.Offset) -> str:
    1474        this = self.sql(expression, "this")
    1475        return f"{this}{self.seg('OFFSET')} {self.sql(expression, 'expression')}"
    1476
    1477    def setitem_sql(self, expression: exp.SetItem) -> str:
    1478        kind = self.sql(expression, "kind")
    1479        kind = f"{kind} " if kind else ""
    1480        this = self.sql(expression, "this")
    1481        expressions = self.expressions(expression)
    1482        collate = self.sql(expression, "collate")
    1483        collate = f" COLLATE {collate}" if collate else ""
    1484        global_ = "GLOBAL " if expression.args.get("global") else ""
    1485        return f"{global_}{kind}{this}{expressions}{collate}"
    1486
    1487    def set_sql(self, expression: exp.Set) -> str:
    1488        expressions = (
    1489            f" {self.expressions(expression, flat=True)}" if expression.expressions else ""
    1490        )
    1491        tag = " TAG" if expression.args.get("tag") else ""
    1492        return f"{'UNSET' if expression.args.get('unset') else 'SET'}{tag}{expressions}"
    1493
    1494    def pragma_sql(self, expression: exp.Pragma) -> str:
    1495        return f"PRAGMA {self.sql(expression, 'this')}"
    1496
    1497    def lock_sql(self, expression: exp.Lock) -> str:
    1498        if not self.LOCKING_READS_SUPPORTED:
    1499            self.unsupported("Locking reads using 'FOR UPDATE/SHARE' are not supported")
    1500            return ""
    1501
    1502        lock_type = "FOR UPDATE" if expression.args["update"] else "FOR SHARE"
    1503        expressions = self.expressions(expression, flat=True)
    1504        expressions = f" OF {expressions}" if expressions else ""
    1505        wait = expression.args.get("wait")
    1506
    1507        if wait is not None:
    1508            if isinstance(wait, exp.Literal):
    1509                wait = f" WAIT {self.sql(wait)}"
    1510            else:
    1511                wait = " NOWAIT" if wait else " SKIP LOCKED"
    1512
    1513        return f"{lock_type}{expressions}{wait or ''}"
    1514
    1515    def literal_sql(self, expression: exp.Literal) -> str:
    1516        text = expression.this or ""
    1517        if expression.is_string:
    1518            text = f"{self.QUOTE_START}{self.escape_str(text)}{self.QUOTE_END}"
    1519        return text
    1520
    1521    def escape_str(self, text: str) -> str:
    1522        text = text.replace(self.QUOTE_END, self._escaped_quote_end)
    1523        if self.ESCAPE_LINE_BREAK:
    1524            text = text.replace("\n", "\\n")
    1525        elif self.pretty:
    1526            text = text.replace("\n", self.SENTINEL_LINE_BREAK)
    1527        return text
    1528
    1529    def loaddata_sql(self, expression: exp.LoadData) -> str:
    1530        local = " LOCAL" if expression.args.get("local") else ""
    1531        inpath = f" INPATH {self.sql(expression, 'inpath')}"
    1532        overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
    1533        this = f" INTO TABLE {self.sql(expression, 'this')}"
    1534        partition = self.sql(expression, "partition")
    1535        partition = f" {partition}" if partition else ""
    1536        input_format = self.sql(expression, "input_format")
    1537        input_format = f" INPUTFORMAT {input_format}" if input_format else ""
    1538        serde = self.sql(expression, "serde")
    1539        serde = f" SERDE {serde}" if serde else ""
    1540        return f"LOAD DATA{local}{inpath}{overwrite}{this}{partition}{input_format}{serde}"
    1541
    1542    def null_sql(self, *_) -> str:
    1543        return "NULL"
    1544
    1545    def boolean_sql(self, expression: exp.Boolean) -> str:
    1546        return "TRUE" if expression.this else "FALSE"
    1547
    1548    def order_sql(self, expression: exp.Order, flat: bool = False) -> str:
    1549        this = self.sql(expression, "this")
    1550        this = f"{this} " if this else this
    1551        return self.op_expressions(f"{this}ORDER BY", expression, flat=this or flat)  # type: ignore
    1552
    1553    def cluster_sql(self, expression: exp.Cluster) -> str:
    1554        return self.op_expressions("CLUSTER BY", expression)
    1555
    1556    def distribute_sql(self, expression: exp.Distribute) -> str:
    1557        return self.op_expressions("DISTRIBUTE BY", expression)
    1558
    1559    def sort_sql(self, expression: exp.Sort) -> str:
    1560        return self.op_expressions("SORT BY", expression)
    1561
    1562    def ordered_sql(self, expression: exp.Ordered) -> str:
    1563        desc = expression.args.get("desc")
    1564        asc = not desc
    1565
    1566        nulls_first = expression.args.get("nulls_first")
    1567        nulls_last = not nulls_first
    1568        nulls_are_large = self.NULL_ORDERING == "nulls_are_large"
    1569        nulls_are_small = self.NULL_ORDERING == "nulls_are_small"
    1570        nulls_are_last = self.NULL_ORDERING == "nulls_are_last"
    1571
    1572        sort_order = " DESC" if desc else ""
    1573        nulls_sort_change = ""
    1574        if nulls_first and (
    1575            (asc and nulls_are_large) or (desc and nulls_are_small) or nulls_are_last
    1576        ):
    1577            nulls_sort_change = " NULLS FIRST"
    1578        elif (
    1579            nulls_last
    1580            and ((asc and nulls_are_small) or (desc and nulls_are_large))
    1581            and not nulls_are_last
    1582        ):
    1583            nulls_sort_change = " NULLS LAST"
    1584
    1585        if nulls_sort_change and not self.NULL_ORDERING_SUPPORTED:
    1586            self.unsupported(
    1587                "Sorting in an ORDER BY on NULLS FIRST/NULLS LAST is not supported by this dialect"
    1588            )
    1589            nulls_sort_change = ""
    1590
    1591        return f"{self.sql(expression, 'this')}{sort_order}{nulls_sort_change}"
    1592
    1593    def matchrecognize_sql(self, expression: exp.MatchRecognize) -> str:
    1594        partition = self.partition_by_sql(expression)
    1595        order = self.sql(expression, "order")
    1596        measures = self.expressions(expression, key="measures")
    1597        measures = self.seg(f"MEASURES{self.seg(measures)}") if measures else ""
    1598        rows = self.sql(expression, "rows")
    1599        rows = self.seg(rows) if rows else ""
    1600        after = self.sql(expression, "after")
    1601        after = self.seg(after) if after else ""
    1602        pattern = self.sql(expression, "pattern")
    1603        pattern = self.seg(f"PATTERN ({pattern})") if pattern else ""
    1604        definition_sqls = [
    1605            f"{self.sql(definition, 'alias')} AS {self.sql(definition, 'this')}"
    1606            for definition in expression.args.get("define", [])
    1607        ]
    1608        definitions = self.expressions(sqls=definition_sqls)
    1609        define = self.seg(f"DEFINE{self.seg(definitions)}") if definitions else ""
    1610        body = "".join(
    1611            (
    1612                partition,
    1613                order,
    1614                measures,
    1615                rows,
    1616                after,
    1617                pattern,
    1618                define,
    1619            )
    1620        )
    1621        alias = self.sql(expression, "alias")
    1622        alias = f" {alias}" if alias else ""
    1623        return f"{self.seg('MATCH_RECOGNIZE')} {self.wrap(body)}{alias}"
    1624
    1625    def query_modifiers(self, expression: exp.Expression, *sqls: str) -> str:
    1626        limit: t.Optional[exp.Fetch | exp.Limit] = expression.args.get("limit")
    1627
    1628        # If the limit is generated as TOP, we need to ensure it's not generated twice
    1629        with_offset_limit_modifiers = not isinstance(limit, exp.Limit) or not self.LIMIT_IS_TOP
    1630
    1631        if self.LIMIT_FETCH == "LIMIT" and isinstance(limit, exp.Fetch):
    1632            limit = exp.Limit(expression=exp.maybe_copy(limit.args.get("count")))
    1633        elif self.LIMIT_FETCH == "FETCH" and isinstance(limit, exp.Limit):
    1634            limit = exp.Fetch(direction="FIRST", count=exp.maybe_copy(limit.expression))
    1635
    1636        fetch = isinstance(limit, exp.Fetch)
    1637
    1638        offset_limit_modifiers = (
    1639            self.offset_limit_modifiers(expression, fetch, limit)
    1640            if with_offset_limit_modifiers
    1641            else []
    1642        )
    1643
    1644        return csv(
    1645            *sqls,
    1646            *[self.sql(join) for join in expression.args.get("joins") or []],
    1647            self.sql(expression, "match"),
    1648            *[self.sql(lateral) for lateral in expression.args.get("laterals") or []],
    1649            self.sql(expression, "where"),
    1650            self.sql(expression, "group"),
    1651            self.sql(expression, "having"),
    1652            *self.after_having_modifiers(expression),
    1653            self.sql(expression, "order"),
    1654            *offset_limit_modifiers,
    1655            *self.after_limit_modifiers(expression),
    1656            sep="",
    1657        )
    1658
    1659    def offset_limit_modifiers(
    1660        self, expression: exp.Expression, fetch: bool, limit: t.Optional[exp.Fetch | exp.Limit]
    1661    ) -> t.List[str]:
    1662        return [
    1663            self.sql(expression, "offset") if fetch else self.sql(limit),
    1664            self.sql(limit) if fetch else self.sql(expression, "offset"),
    1665        ]
    1666
    1667    def after_having_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1668        return [
    1669            self.sql(expression, "qualify"),
    1670            self.seg("WINDOW ") + self.expressions(expression, key="windows", flat=True)
    1671            if expression.args.get("windows")
    1672            else "",
    1673            self.sql(expression, "distribute"),
    1674            self.sql(expression, "sort"),
    1675            self.sql(expression, "cluster"),
    1676        ]
    1677
    1678    def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1679        locks = self.expressions(expression, key="locks", sep=" ")
    1680        locks = f" {locks}" if locks else ""
    1681        return [locks, self.sql(expression, "sample")]
    1682
    1683    def select_sql(self, expression: exp.Select) -> str:
    1684        hint = self.sql(expression, "hint")
    1685        distinct = self.sql(expression, "distinct")
    1686        distinct = f" {distinct}" if distinct else ""
    1687        kind = self.sql(expression, "kind").upper()
    1688        limit = expression.args.get("limit")
    1689        top = (
    1690            self.limit_sql(limit, top=True)
    1691            if isinstance(limit, exp.Limit) and self.LIMIT_IS_TOP
    1692            else ""
    1693        )
    1694
    1695        expressions = self.expressions(expression)
    1696
    1697        if kind:
    1698            if kind in self.SELECT_KINDS:
    1699                kind = f" AS {kind}"
    1700            else:
    1701                if kind == "STRUCT":
    1702                    expressions = self.expressions(
    1703                        sqls=[
    1704                            self.sql(
    1705                                exp.Struct(
    1706                                    expressions=[
    1707                                        exp.column(e.output_name).eq(
    1708                                            e.this if isinstance(e, exp.Alias) else e
    1709                                        )
    1710                                        for e in expression.expressions
    1711                                    ]
    1712                                )
    1713                            )
    1714                        ]
    1715                    )
    1716                kind = ""
    1717
    1718        expressions = f"{self.sep()}{expressions}" if expressions else expressions
    1719        sql = self.query_modifiers(
    1720            expression,
    1721            f"SELECT{top}{hint}{distinct}{kind}{expressions}",
    1722            self.sql(expression, "into", comment=False),
    1723            self.sql(expression, "from", comment=False),
    1724        )
    1725        return self.prepend_ctes(expression, sql)
    1726
    1727    def schema_sql(self, expression: exp.Schema) -> str:
    1728        this = self.sql(expression, "this")
    1729        this = f"{this} " if this else ""
    1730        sql = self.schema_columns_sql(expression)
    1731        return f"{this}{sql}"
    1732
    1733    def schema_columns_sql(self, expression: exp.Schema) -> str:
    1734        return f"({self.sep('')}{self.expressions(expression)}{self.seg(')', sep='')}"
    1735
    1736    def star_sql(self, expression: exp.Star) -> str:
    1737        except_ = self.expressions(expression, key="except", flat=True)
    1738        except_ = f"{self.seg(self.STAR_MAPPING['except'])} ({except_})" if except_ else ""
    1739        replace = self.expressions(expression, key="replace", flat=True)
    1740        replace = f"{self.seg(self.STAR_MAPPING['replace'])} ({replace})" if replace else ""
    1741        return f"*{except_}{replace}"
    1742
    1743    def parameter_sql(self, expression: exp.Parameter) -> str:
    1744        this = self.sql(expression, "this")
    1745        this = f"{{{this}}}" if expression.args.get("wrapped") else f"{this}"
    1746        return f"{self.PARAMETER_TOKEN}{this}"
    1747
    1748    def sessionparameter_sql(self, expression: exp.SessionParameter) -> str:
    1749        this = self.sql(expression, "this")
    1750        kind = expression.text("kind")
    1751        if kind:
    1752            kind = f"{kind}."
    1753        return f"@@{kind}{this}"
    1754
    1755    def placeholder_sql(self, expression: exp.Placeholder) -> str:
    1756        return f":{expression.name}" if expression.name else "?"
    1757
    1758    def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str:
    1759        alias = self.sql(expression, "alias")
    1760        alias = f"{sep}{alias}" if alias else ""
    1761
    1762        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1763        pivots = f" {pivots}" if pivots else ""
    1764
    1765        sql = self.query_modifiers(expression, self.wrap(expression), alias, pivots)
    1766        return self.prepend_ctes(expression, sql)
    1767
    1768    def qualify_sql(self, expression: exp.Qualify) -> str:
    1769        this = self.indent(self.sql(expression, "this"))
    1770        return f"{self.seg('QUALIFY')}{self.sep()}{this}"
    1771
    1772    def union_sql(self, expression: exp.Union) -> str:
    1773        return self.prepend_ctes(
    1774            expression,
    1775            self.set_operation(expression, self.union_op(expression)),
    1776        )
    1777
    1778    def union_op(self, expression: exp.Union) -> str:
    1779        kind = " DISTINCT" if self.EXPLICIT_UNION else ""
    1780        kind = kind if expression.args.get("distinct") else " ALL"
    1781        return f"UNION{kind}"
    1782
    1783    def unnest_sql(self, expression: exp.Unnest) -> str:
    1784        args = self.expressions(expression, flat=True)
    1785        alias = expression.args.get("alias")
    1786        if alias and self.UNNEST_COLUMN_ONLY:
    1787            columns = alias.columns
    1788            alias = self.sql(columns[0]) if columns else ""
    1789        else:
    1790            alias = self.sql(expression, "alias")
    1791        alias = f" AS {alias}" if alias else alias
    1792        ordinality = " WITH ORDINALITY" if expression.args.get("ordinality") else ""
    1793        offset = expression.args.get("offset")
    1794        offset = f" WITH OFFSET AS {self.sql(offset)}" if offset else ""
    1795        return f"UNNEST({args}){ordinality}{alias}{offset}"
    1796
    1797    def where_sql(self, expression: exp.Where) -> str:
    1798        this = self.indent(self.sql(expression, "this"))
    1799        return f"{self.seg('WHERE')}{self.sep()}{this}"
    1800
    1801    def window_sql(self, expression: exp.Window) -> str:
    1802        this = self.sql(expression, "this")
    1803        partition = self.partition_by_sql(expression)
    1804        order = expression.args.get("order")
    1805        order = self.order_sql(order, flat=True) if order else ""
    1806        spec = self.sql(expression, "spec")
    1807        alias = self.sql(expression, "alias")
    1808        over = self.sql(expression, "over") or "OVER"
    1809
    1810        this = f"{this} {'AS' if expression.arg_key == 'windows' else over}"
    1811
    1812        first = expression.args.get("first")
    1813        if first is None:
    1814            first = ""
    1815        else:
    1816            first = "FIRST" if first else "LAST"
    1817
    1818        if not partition and not order and not spec and alias:
    1819            return f"{this} {alias}"
    1820
    1821        args = " ".join(arg for arg in (alias, first, partition, order, spec) if arg)
    1822        return f"{this} ({args})"
    1823
    1824    def partition_by_sql(self, expression: exp.Window | exp.MatchRecognize) -> str:
    1825        partition = self.expressions(expression, key="partition_by", flat=True)
    1826        return f"PARTITION BY {partition}" if partition else ""
    1827
    1828    def windowspec_sql(self, expression: exp.WindowSpec) -> str:
    1829        kind = self.sql(expression, "kind")
    1830        start = csv(self.sql(expression, "start"), self.sql(expression, "start_side"), sep=" ")
    1831        end = (
    1832            csv(self.sql(expression, "end"), self.sql(expression, "end_side"), sep=" ")
    1833            or "CURRENT ROW"
    1834        )
    1835        return f"{kind} BETWEEN {start} AND {end}"
    1836
    1837    def withingroup_sql(self, expression: exp.WithinGroup) -> str:
    1838        this = self.sql(expression, "this")
    1839        expression_sql = self.sql(expression, "expression")[1:]  # order has a leading space
    1840        return f"{this} WITHIN GROUP ({expression_sql})"
    1841
    1842    def between_sql(self, expression: exp.Between) -> str:
    1843        this = self.sql(expression, "this")
    1844        low = self.sql(expression, "low")
    1845        high = self.sql(expression, "high")
    1846        return f"{this} BETWEEN {low} AND {high}"
    1847
    1848    def bracket_sql(self, expression: exp.Bracket) -> str:
    1849        expressions = apply_index_offset(expression.this, expression.expressions, self.INDEX_OFFSET)
    1850        expressions_sql = ", ".join(self.sql(e) for e in expressions)
    1851
    1852        return f"{self.sql(expression, 'this')}[{expressions_sql}]"
    1853
    1854    def safebracket_sql(self, expression: exp.SafeBracket) -> str:
    1855        return self.bracket_sql(expression)
    1856
    1857    def all_sql(self, expression: exp.All) -> str:
    1858        return f"ALL {self.wrap(expression)}"
    1859
    1860    def any_sql(self, expression: exp.Any) -> str:
    1861        this = self.sql(expression, "this")
    1862        if isinstance(expression.this, exp.Subqueryable):
    1863            this = self.wrap(this)
    1864        return f"ANY {this}"
    1865
    1866    def exists_sql(self, expression: exp.Exists) -> str:
    1867        return f"EXISTS{self.wrap(expression)}"
    1868
    1869    def case_sql(self, expression: exp.Case) -> str:
    1870        this = self.sql(expression, "this")
    1871        statements = [f"CASE {this}" if this else "CASE"]
    1872
    1873        for e in expression.args["ifs"]:
    1874            statements.append(f"WHEN {self.sql(e, 'this')}")
    1875            statements.append(f"THEN {self.sql(e, 'true')}")
    1876
    1877        default = self.sql(expression, "default")
    1878
    1879        if default:
    1880            statements.append(f"ELSE {default}")
    1881
    1882        statements.append("END")
    1883
    1884        if self.pretty and self.text_width(statements) > self.max_text_width:
    1885            return self.indent("\n".join(statements), skip_first=True, skip_last=True)
    1886
    1887        return " ".join(statements)
    1888
    1889    def constraint_sql(self, expression: exp.Constraint) -> str:
    1890        this = self.sql(expression, "this")
    1891        expressions = self.expressions(expression, flat=True)
    1892        return f"CONSTRAINT {this} {expressions}"
    1893
    1894    def nextvaluefor_sql(self, expression: exp.NextValueFor) -> str:
    1895        order = expression.args.get("order")
    1896        order = f" OVER ({self.order_sql(order, flat=True)})" if order else ""
    1897        return f"NEXT VALUE FOR {self.sql(expression, 'this')}{order}"
    1898
    1899    def extract_sql(self, expression: exp.Extract) -> str:
    1900        this = self.sql(expression, "this") if self.EXTRACT_ALLOWS_QUOTES else expression.this.name
    1901        expression_sql = self.sql(expression, "expression")
    1902        return f"EXTRACT({this} FROM {expression_sql})"
    1903
    1904    def trim_sql(self, expression: exp.Trim) -> str:
    1905        trim_type = self.sql(expression, "position")
    1906
    1907        if trim_type == "LEADING":
    1908            return self.func("LTRIM", expression.this)
    1909        elif trim_type == "TRAILING":
    1910            return self.func("RTRIM", expression.this)
    1911        else:
    1912            return self.func("TRIM", expression.this, expression.expression)
    1913
    1914    def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    1915        expressions = expression.expressions
    1916        if self.STRICT_STRING_CONCAT:
    1917            expressions = (exp.cast(e, "text") for e in expressions)
    1918        return self.func("CONCAT", *expressions)
    1919
    1920    def check_sql(self, expression: exp.Check) -> str:
    1921        this = self.sql(expression, key="this")
    1922        return f"CHECK ({this})"
    1923
    1924    def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
    1925        expressions = self.expressions(expression, flat=True)
    1926        reference = self.sql(expression, "reference")
    1927        reference = f" {reference}" if reference else ""
    1928        delete = self.sql(expression, "delete")
    1929        delete = f" ON DELETE {delete}" if delete else ""
    1930        update = self.sql(expression, "update")
    1931        update = f" ON UPDATE {update}" if update else ""
    1932        return f"FOREIGN KEY ({expressions}){reference}{delete}{update}"
    1933
    1934    def primarykey_sql(self, expression: exp.ForeignKey) -> str:
    1935        expressions = self.expressions(expression, flat=True)
    1936        options = self.expressions(expression, key="options", flat=True, sep=" ")
    1937        options = f" {options}" if options else ""
    1938        return f"PRIMARY KEY ({expressions}){options}"
    1939
    1940    def if_sql(self, expression: exp.If) -> str:
    1941        expression = expression.copy()
    1942        return self.case_sql(exp.Case(ifs=[expression], default=expression.args.get("false")))
    1943
    1944    def matchagainst_sql(self, expression: exp.MatchAgainst) -> str:
    1945        modifier = expression.args.get("modifier")
    1946        modifier = f" {modifier}" if modifier else ""
    1947        return f"{self.func('MATCH', *expression.expressions)} AGAINST({self.sql(expression, 'this')}{modifier})"
    1948
    1949    def jsonkeyvalue_sql(self, expression: exp.JSONKeyValue) -> str:
    1950        return f"{self.sql(expression, 'this')}: {self.sql(expression, 'expression')}"
    1951
    1952    def jsonobject_sql(self, expression: exp.JSONObject) -> str:
    1953        null_handling = expression.args.get("null_handling")
    1954        null_handling = f" {null_handling}" if null_handling else ""
    1955        unique_keys = expression.args.get("unique_keys")
    1956        if unique_keys is not None:
    1957            unique_keys = f" {'WITH' if unique_keys else 'WITHOUT'} UNIQUE KEYS"
    1958        else:
    1959            unique_keys = ""
    1960        return_type = self.sql(expression, "return_type")
    1961        return_type = f" RETURNING {return_type}" if return_type else ""
    1962        format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
    1963        encoding = self.sql(expression, "encoding")
    1964        encoding = f" ENCODING {encoding}" if encoding else ""
    1965        return self.func(
    1966            "JSON_OBJECT",
    1967            *expression.expressions,
    1968            suffix=f"{null_handling}{unique_keys}{return_type}{format_json}{encoding})",
    1969        )
    1970
    1971    def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
    1972        this = self.sql(expression, "this")
    1973        kind = self.sql(expression, "kind")
    1974        path = self.sql(expression, "path")
    1975        path = f" {path}" if path else ""
    1976        as_json = " AS JSON" if expression.args.get("as_json") else ""
    1977        return f"{this} {kind}{path}{as_json}"
    1978
    1979    def openjson_sql(self, expression: exp.OpenJSON) -> str:
    1980        this = self.sql(expression, "this")
    1981        path = self.sql(expression, "path")
    1982        path = f", {path}" if path else ""
    1983        expressions = self.expressions(expression)
    1984        with_ = (
    1985            f" WITH ({self.seg(self.indent(expressions), sep='')}{self.seg(')', sep='')}"
    1986            if expressions
    1987            else ""
    1988        )
    1989        return f"OPENJSON({this}{path}){with_}"
    1990
    1991    def in_sql(self, expression: exp.In) -> str:
    1992        query = expression.args.get("query")
    1993        unnest = expression.args.get("unnest")
    1994        field = expression.args.get("field")
    1995        is_global = " GLOBAL" if expression.args.get("is_global") else ""
    1996
    1997        if query:
    1998            in_sql = self.wrap(query)
    1999        elif unnest:
    2000            in_sql = self.in_unnest_op(unnest)
    2001        elif field:
    2002            in_sql = self.sql(field)
    2003        else:
    2004            in_sql = f"({self.expressions(expression, flat=True)})"
    2005
    2006        return f"{self.sql(expression, 'this')}{is_global} IN {in_sql}"
    2007
    2008    def in_unnest_op(self, unnest: exp.Unnest) -> str:
    2009        return f"(SELECT {self.sql(unnest)})"
    2010
    2011    def interval_sql(self, expression: exp.Interval) -> str:
    2012        unit = self.sql(expression, "unit")
    2013        if not self.INTERVAL_ALLOWS_PLURAL_FORM:
    2014            unit = self.TIME_PART_SINGULARS.get(unit.lower(), unit)
    2015        unit = f" {unit}" if unit else ""
    2016
    2017        if self.SINGLE_STRING_INTERVAL:
    2018            this = expression.this.name if expression.this else ""
    2019            return f"INTERVAL '{this}{unit}'" if this else f"INTERVAL{unit}"
    2020
    2021        this = self.sql(expression, "this")
    2022        if this:
    2023            unwrapped = isinstance(expression.this, self.UNWRAPPED_INTERVAL_VALUES)
    2024            this = f" {this}" if unwrapped else f" ({this})"
    2025
    2026        return f"INTERVAL{this}{unit}"
    2027
    2028    def return_sql(self, expression: exp.Return) -> str:
    2029        return f"RETURN {self.sql(expression, 'this')}"
    2030
    2031    def reference_sql(self, expression: exp.Reference) -> str:
    2032        this = self.sql(expression, "this")
    2033        expressions = self.expressions(expression, flat=True)
    2034        expressions = f"({expressions})" if expressions else ""
    2035        options = self.expressions(expression, key="options", flat=True, sep=" ")
    2036        options = f" {options}" if options else ""
    2037        return f"REFERENCES {this}{expressions}{options}"
    2038
    2039    def anonymous_sql(self, expression: exp.Anonymous) -> str:
    2040        return self.func(expression.name, *expression.expressions)
    2041
    2042    def paren_sql(self, expression: exp.Paren) -> str:
    2043        if isinstance(expression.unnest(), exp.Select):
    2044            sql = self.wrap(expression)
    2045        else:
    2046            sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
    2047            sql = f"({sql}{self.seg(')', sep='')}"
    2048
    2049        return self.prepend_ctes(expression, sql)
    2050
    2051    def neg_sql(self, expression: exp.Neg) -> str:
    2052        # This makes sure we don't convert "- - 5" to "--5", which is a comment
    2053        this_sql = self.sql(expression, "this")
    2054        sep = " " if this_sql[0] == "-" else ""
    2055        return f"-{sep}{this_sql}"
    2056
    2057    def not_sql(self, expression: exp.Not) -> str:
    2058        return f"NOT {self.sql(expression, 'this')}"
    2059
    2060    def alias_sql(self, expression: exp.Alias) -> str:
    2061        alias = self.sql(expression, "alias")
    2062        alias = f" AS {alias}" if alias else ""
    2063        return f"{self.sql(expression, 'this')}{alias}"
    2064
    2065    def aliases_sql(self, expression: exp.Aliases) -> str:
    2066        return f"{self.sql(expression, 'this')} AS ({self.expressions(expression, flat=True)})"
    2067
    2068    def attimezone_sql(self, expression: exp.AtTimeZone) -> str:
    2069        this = self.sql(expression, "this")
    2070        zone = self.sql(expression, "zone")
    2071        return f"{this} AT TIME ZONE {zone}"
    2072
    2073    def add_sql(self, expression: exp.Add) -> str:
    2074        return self.binary(expression, "+")
    2075
    2076    def and_sql(self, expression: exp.And) -> str:
    2077        return self.connector_sql(expression, "AND")
    2078
    2079    def xor_sql(self, expression: exp.Xor) -> str:
    2080        return self.connector_sql(expression, "XOR")
    2081
    2082    def connector_sql(self, expression: exp.Connector, op: str) -> str:
    2083        if not self.pretty:
    2084            return self.binary(expression, op)
    2085
    2086        sqls = tuple(
    2087            self.maybe_comment(self.sql(e), e, e.parent.comments or []) if i != 1 else self.sql(e)
    2088            for i, e in enumerate(expression.flatten(unnest=False))
    2089        )
    2090
    2091        sep = "\n" if self.text_width(sqls) > self.max_text_width else " "
    2092        return f"{sep}{op} ".join(sqls)
    2093
    2094    def bitwiseand_sql(self, expression: exp.BitwiseAnd) -> str:
    2095        return self.binary(expression, "&")
    2096
    2097    def bitwiseleftshift_sql(self, expression: exp.BitwiseLeftShift) -> str:
    2098        return self.binary(expression, "<<")
    2099
    2100    def bitwisenot_sql(self, expression: exp.BitwiseNot) -> str:
    2101        return f"~{self.sql(expression, 'this')}"
    2102
    2103    def bitwiseor_sql(self, expression: exp.BitwiseOr) -> str:
    2104        return self.binary(expression, "|")
    2105
    2106    def bitwiserightshift_sql(self, expression: exp.BitwiseRightShift) -> str:
    2107        return self.binary(expression, ">>")
    2108
    2109    def bitwisexor_sql(self, expression: exp.BitwiseXor) -> str:
    2110        return self.binary(expression, "^")
    2111
    2112    def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    2113        format_sql = self.sql(expression, "format")
    2114        format_sql = f" FORMAT {format_sql}" if format_sql else ""
    2115        return f"{safe_prefix or ''}CAST({self.sql(expression, 'this')} AS {self.sql(expression, 'to')}{format_sql})"
    2116
    2117    def currentdate_sql(self, expression: exp.CurrentDate) -> str:
    2118        zone = self.sql(expression, "this")
    2119        return f"CURRENT_DATE({zone})" if zone else "CURRENT_DATE"
    2120
    2121    def collate_sql(self, expression: exp.Collate) -> str:
    2122        return self.binary(expression, "COLLATE")
    2123
    2124    def command_sql(self, expression: exp.Command) -> str:
    2125        return f"{self.sql(expression, 'this').upper()} {expression.text('expression').strip()}"
    2126
    2127    def comment_sql(self, expression: exp.Comment) -> str:
    2128        this = self.sql(expression, "this")
    2129        kind = expression.args["kind"]
    2130        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    2131        expression_sql = self.sql(expression, "expression")
    2132        return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"
    2133
    2134    def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
    2135        this = self.sql(expression, "this")
    2136        delete = " DELETE" if expression.args.get("delete") else ""
    2137        recompress = self.sql(expression, "recompress")
    2138        recompress = f" RECOMPRESS {recompress}" if recompress else ""
    2139        to_disk = self.sql(expression, "to_disk")
    2140        to_disk = f" TO DISK {to_disk}" if to_disk else ""
    2141        to_volume = self.sql(expression, "to_volume")
    2142        to_volume = f" TO VOLUME {to_volume}" if to_volume else ""
    2143        return f"{this}{delete}{recompress}{to_disk}{to_volume}"
    2144
    2145    def mergetreettl_sql(self, expression: exp.MergeTreeTTL) -> str:
    2146        where = self.sql(expression, "where")
    2147        group = self.sql(expression, "group")
    2148        aggregates = self.expressions(expression, key="aggregates")
    2149        aggregates = self.seg("SET") + self.seg(aggregates) if aggregates else ""
    2150
    2151        if not (where or group or aggregates) and len(expression.expressions) == 1:
    2152            return f"TTL {self.expressions(expression, flat=True)}"
    2153
    2154        return f"TTL{self.seg(self.expressions(expression))}{where}{group}{aggregates}"
    2155
    2156    def transaction_sql(self, expression: exp.Transaction) -> str:
    2157        return "BEGIN"
    2158
    2159    def commit_sql(self, expression: exp.Commit) -> str:
    2160        chain = expression.args.get("chain")
    2161        if chain is not None:
    2162            chain = " AND CHAIN" if chain else " AND NO CHAIN"
    2163
    2164        return f"COMMIT{chain or ''}"
    2165
    2166    def rollback_sql(self, expression: exp.Rollback) -> str:
    2167        savepoint = expression.args.get("savepoint")
    2168        savepoint = f" TO {savepoint}" if savepoint else ""
    2169        return f"ROLLBACK{savepoint}"
    2170
    2171    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
    2172        this = self.sql(expression, "this")
    2173
    2174        dtype = self.sql(expression, "dtype")
    2175        if dtype:
    2176            collate = self.sql(expression, "collate")
    2177            collate = f" COLLATE {collate}" if collate else ""
    2178            using = self.sql(expression, "using")
    2179            using = f" USING {using}" if using else ""
    2180            return f"ALTER COLUMN {this} TYPE {dtype}{collate}{using}"
    2181
    2182        default = self.sql(expression, "default")
    2183        if default:
    2184            return f"ALTER COLUMN {this} SET DEFAULT {default}"
    2185
    2186        if not expression.args.get("drop"):
    2187            self.unsupported("Unsupported ALTER COLUMN syntax")
    2188
    2189        return f"ALTER COLUMN {this} DROP DEFAULT"
    2190
    2191    def renametable_sql(self, expression: exp.RenameTable) -> str:
    2192        if not self.RENAME_TABLE_WITH_DB:
    2193            # Remove db from tables
    2194            expression = expression.transform(
    2195                lambda n: exp.table_(n.this) if isinstance(n, exp.Table) else n
    2196            )
    2197        this = self.sql(expression, "this")
    2198        return f"RENAME TO {this}"
    2199
    2200    def altertable_sql(self, expression: exp.AlterTable) -> str:
    2201        actions = expression.args["actions"]
    2202
    2203        if isinstance(actions[0], exp.ColumnDef):
    2204            actions = self.expressions(expression, key="actions", prefix="ADD COLUMN ")
    2205        elif isinstance(actions[0], exp.Schema):
    2206            actions = self.expressions(expression, key="actions", prefix="ADD COLUMNS ")
    2207        elif isinstance(actions[0], exp.Delete):
    2208            actions = self.expressions(expression, key="actions", flat=True)
    2209        else:
    2210            actions = self.expressions(expression, key="actions")
    2211
    2212        exists = " IF EXISTS" if expression.args.get("exists") else ""
    2213        return f"ALTER TABLE{exists} {self.sql(expression, 'this')} {actions}"
    2214
    2215    def droppartition_sql(self, expression: exp.DropPartition) -> str:
    2216        expressions = self.expressions(expression)
    2217        exists = " IF EXISTS " if expression.args.get("exists") else " "
    2218        return f"DROP{exists}{expressions}"
    2219
    2220    def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
    2221        this = self.sql(expression, "this")
    2222        expression_ = self.sql(expression, "expression")
    2223        add_constraint = f"ADD CONSTRAINT {this}" if this else "ADD"
    2224
    2225        enforced = expression.args.get("enforced")
    2226        if enforced is not None:
    2227            return f"{add_constraint} CHECK ({expression_}){' ENFORCED' if enforced else ''}"
    2228
    2229        return f"{add_constraint} {expression_}"
    2230
    2231    def distinct_sql(self, expression: exp.Distinct) -> str:
    2232        this = self.expressions(expression, flat=True)
    2233        this = f" {this}" if this else ""
    2234
    2235        on = self.sql(expression, "on")
    2236        on = f" ON {on}" if on else ""
    2237        return f"DISTINCT{this}{on}"
    2238
    2239    def ignorenulls_sql(self, expression: exp.IgnoreNulls) -> str:
    2240        return f"{self.sql(expression, 'this')} IGNORE NULLS"
    2241
    2242    def respectnulls_sql(self, expression: exp.RespectNulls) -> str:
    2243        return f"{self.sql(expression, 'this')} RESPECT NULLS"
    2244
    2245    def intdiv_sql(self, expression: exp.IntDiv) -> str:
    2246        return self.sql(
    2247            exp.Cast(
    2248                this=exp.Div(this=expression.this.copy(), expression=expression.expression.copy()),
    2249                to=exp.DataType(this=exp.DataType.Type.INT),
    2250            )
    2251        )
    2252
    2253    def dpipe_sql(self, expression: exp.DPipe) -> str:
    2254        return self.binary(expression, "||")
    2255
    2256    def safedpipe_sql(self, expression: exp.SafeDPipe) -> str:
    2257        if self.STRICT_STRING_CONCAT:
    2258            return self.func("CONCAT", *(exp.cast(e, "text") for e in expression.flatten()))
    2259        return self.dpipe_sql(expression)
    2260
    2261    def div_sql(self, expression: exp.Div) -> str:
    2262        return self.binary(expression, "/")
    2263
    2264    def overlaps_sql(self, expression: exp.Overlaps) -> str:
    2265        return self.binary(expression, "OVERLAPS")
    2266
    2267    def distance_sql(self, expression: exp.Distance) -> str:
    2268        return self.binary(expression, "<->")
    2269
    2270    def dot_sql(self, expression: exp.Dot) -> str:
    2271        return f"{self.sql(expression, 'this')}.{self.sql(expression, 'expression')}"
    2272
    2273    def eq_sql(self, expression: exp.EQ) -> str:
    2274        return self.binary(expression, "=")
    2275
    2276    def escape_sql(self, expression: exp.Escape) -> str:
    2277        return self.binary(expression, "ESCAPE")
    2278
    2279    def glob_sql(self, expression: exp.Glob) -> str:
    2280        return self.binary(expression, "GLOB")
    2281
    2282    def gt_sql(self, expression: exp.GT) -> str:
    2283        return self.binary(expression, ">")
    2284
    2285    def gte_sql(self, expression: exp.GTE) -> str:
    2286        return self.binary(expression, ">=")
    2287
    2288    def ilike_sql(self, expression: exp.ILike) -> str:
    2289        return self.binary(expression, "ILIKE")
    2290
    2291    def ilikeany_sql(self, expression: exp.ILikeAny) -> str:
    2292        return self.binary(expression, "ILIKE ANY")
    2293
    2294    def is_sql(self, expression: exp.Is) -> str:
    2295        if not self.IS_BOOL_ALLOWED and isinstance(expression.expression, exp.Boolean):
    2296            return self.sql(
    2297                expression.this if expression.expression.this else exp.not_(expression.this)
    2298            )
    2299        return self.binary(expression, "IS")
    2300
    2301    def like_sql(self, expression: exp.Like) -> str:
    2302        return self.binary(expression, "LIKE")
    2303
    2304    def likeany_sql(self, expression: exp.LikeAny) -> str:
    2305        return self.binary(expression, "LIKE ANY")
    2306
    2307    def similarto_sql(self, expression: exp.SimilarTo) -> str:
    2308        return self.binary(expression, "SIMILAR TO")
    2309
    2310    def lt_sql(self, expression: exp.LT) -> str:
    2311        return self.binary(expression, "<")
    2312
    2313    def lte_sql(self, expression: exp.LTE) -> str:
    2314        return self.binary(expression, "<=")
    2315
    2316    def mod_sql(self, expression: exp.Mod) -> str:
    2317        return self.binary(expression, "%")
    2318
    2319    def mul_sql(self, expression: exp.Mul) -> str:
    2320        return self.binary(expression, "*")
    2321
    2322    def neq_sql(self, expression: exp.NEQ) -> str:
    2323        return self.binary(expression, "<>")
    2324
    2325    def nullsafeeq_sql(self, expression: exp.NullSafeEQ) -> str:
    2326        return self.binary(expression, "IS NOT DISTINCT FROM")
    2327
    2328    def nullsafeneq_sql(self, expression: exp.NullSafeNEQ) -> str:
    2329        return self.binary(expression, "IS DISTINCT FROM")
    2330
    2331    def or_sql(self, expression: exp.Or) -> str:
    2332        return self.connector_sql(expression, "OR")
    2333
    2334    def slice_sql(self, expression: exp.Slice) -> str:
    2335        return self.binary(expression, ":")
    2336
    2337    def sub_sql(self, expression: exp.Sub) -> str:
    2338        return self.binary(expression, "-")
    2339
    2340    def trycast_sql(self, expression: exp.TryCast) -> str:
    2341        return self.cast_sql(expression, safe_prefix="TRY_")
    2342
    2343    def use_sql(self, expression: exp.Use) -> str:
    2344        kind = self.sql(expression, "kind")
    2345        kind = f" {kind}" if kind else ""
    2346        this = self.sql(expression, "this")
    2347        this = f" {this}" if this else ""
    2348        return f"USE{kind}{this}"
    2349
    2350    def binary(self, expression: exp.Binary, op: str) -> str:
    2351        op = self.maybe_comment(op, comments=expression.comments)
    2352        return f"{self.sql(expression, 'this')} {op} {self.sql(expression, 'expression')}"
    2353
    2354    def function_fallback_sql(self, expression: exp.Func) -> str:
    2355        args = []
    2356
    2357        for key in expression.arg_types:
    2358            arg_value = expression.args.get(key)
    2359
    2360            if isinstance(arg_value, list):
    2361                for value in arg_value:
    2362                    args.append(value)
    2363            elif arg_value is not None:
    2364                args.append(arg_value)
    2365
    2366        if self.normalize_functions:
    2367            name = expression.sql_name()
    2368        else:
    2369            name = (expression._meta and expression.meta.get("name")) or expression.sql_name()
    2370
    2371        return self.func(name, *args)
    2372
    2373    def func(
    2374        self,
    2375        name: str,
    2376        *args: t.Optional[exp.Expression | str],
    2377        prefix: str = "(",
    2378        suffix: str = ")",
    2379    ) -> str:
    2380        return f"{self.normalize_func(name)}{prefix}{self.format_args(*args)}{suffix}"
    2381
    2382    def format_args(self, *args: t.Optional[str | exp.Expression]) -> str:
    2383        arg_sqls = tuple(self.sql(arg) for arg in args if arg is not None)
    2384        if self.pretty and self.text_width(arg_sqls) > self.max_text_width:
    2385            return self.indent("\n" + f",\n".join(arg_sqls) + "\n", skip_first=True, skip_last=True)
    2386        return ", ".join(arg_sqls)
    2387
    2388    def text_width(self, args: t.Iterable) -> int:
    2389        return sum(len(arg) for arg in args)
    2390
    2391    def format_time(self, expression: exp.Expression) -> t.Optional[str]:
    2392        return format_time(
    2393            self.sql(expression, "format"), self.INVERSE_TIME_MAPPING, self.INVERSE_TIME_TRIE
    2394        )
    2395
    2396    def expressions(
    2397        self,
    2398        expression: t.Optional[exp.Expression] = None,
    2399        key: t.Optional[str] = None,
    2400        sqls: t.Optional[t.List[str]] = None,
    2401        flat: bool = False,
    2402        indent: bool = True,
    2403        skip_first: bool = False,
    2404        sep: str = ", ",
    2405        prefix: str = "",
    2406    ) -> str:
    2407        expressions = expression.args.get(key or "expressions") if expression else sqls
    2408
    2409        if not expressions:
    2410            return ""
    2411
    2412        if flat:
    2413            return sep.join(sql for sql in (self.sql(e) for e in expressions) if sql)
    2414
    2415        num_sqls = len(expressions)
    2416
    2417        # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
    2418        pad = " " * self.pad
    2419        stripped_sep = sep.strip()
    2420
    2421        result_sqls = []
    2422        for i, e in enumerate(expressions):
    2423            sql = self.sql(e, comment=False)
    2424            if not sql:
    2425                continue
    2426
    2427            comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
    2428
    2429            if self.pretty:
    2430                if self.leading_comma:
    2431                    result_sqls.append(f"{sep if i > 0 else pad}{prefix}{sql}{comments}")
    2432                else:
    2433                    result_sqls.append(
    2434                        f"{prefix}{sql}{stripped_sep if i + 1 < num_sqls else ''}{comments}"
    2435                    )
    2436            else:
    2437                result_sqls.append(f"{prefix}{sql}{comments}{sep if i + 1 < num_sqls else ''}")
    2438
    2439        result_sql = "\n".join(result_sqls) if self.pretty else "".join(result_sqls)
    2440        return self.indent(result_sql, skip_first=skip_first) if indent else result_sql
    2441
    2442    def op_expressions(self, op: str, expression: exp.Expression, flat: bool = False) -> str:
    2443        flat = flat or isinstance(expression.parent, exp.Properties)
    2444        expressions_sql = self.expressions(expression, flat=flat)
    2445        if flat:
    2446            return f"{op} {expressions_sql}"
    2447        return f"{self.seg(op)}{self.sep() if expressions_sql else ''}{expressions_sql}"
    2448
    2449    def naked_property(self, expression: exp.Property) -> str:
    2450        property_name = exp.Properties.PROPERTY_TO_NAME.get(expression.__class__)
    2451        if not property_name:
    2452            self.unsupported(f"Unsupported property {expression.__class__.__name__}")
    2453        return f"{property_name} {self.sql(expression, 'this')}"
    2454
    2455    def set_operation(self, expression: exp.Expression, op: str) -> str:
    2456        this = self.sql(expression, "this")
    2457        op = self.seg(op)
    2458        return self.query_modifiers(
    2459            expression, f"{this}{op}{self.sep()}{self.sql(expression, 'expression')}"
    2460        )
    2461
    2462    def tag_sql(self, expression: exp.Tag) -> str:
    2463        return f"{expression.args.get('prefix')}{self.sql(expression.this)}{expression.args.get('postfix')}"
    2464
    2465    def token_sql(self, token_type: TokenType) -> str:
    2466        return self.TOKEN_MAPPING.get(token_type, token_type.name)
    2467
    2468    def userdefinedfunction_sql(self, expression: exp.UserDefinedFunction) -> str:
    2469        this = self.sql(expression, "this")
    2470        expressions = self.no_identify(self.expressions, expression)
    2471        expressions = (
    2472            self.wrap(expressions) if expression.args.get("wrapped") else f" {expressions}"
    2473        )
    2474        return f"{this}{expressions}"
    2475
    2476    def joinhint_sql(self, expression: exp.JoinHint) -> str:
    2477        this = self.sql(expression, "this")
    2478        expressions = self.expressions(expression, flat=True)
    2479        return f"{this}({expressions})"
    2480
    2481    def kwarg_sql(self, expression: exp.Kwarg) -> str:
    2482        return self.binary(expression, "=>")
    2483
    2484    def when_sql(self, expression: exp.When) -> str:
    2485        matched = "MATCHED" if expression.args["matched"] else "NOT MATCHED"
    2486        source = " BY SOURCE" if self.MATCHED_BY_SOURCE and expression.args.get("source") else ""
    2487        condition = self.sql(expression, "condition")
    2488        condition = f" AND {condition}" if condition else ""
    2489
    2490        then_expression = expression.args.get("then")
    2491        if isinstance(then_expression, exp.Insert):
    2492            then = f"INSERT {self.sql(then_expression, 'this')}"
    2493            if "expression" in then_expression.args:
    2494                then += f" VALUES {self.sql(then_expression, 'expression')}"
    2495        elif isinstance(then_expression, exp.Update):
    2496            if isinstance(then_expression.args.get("expressions"), exp.Star):
    2497                then = f"UPDATE {self.sql(then_expression, 'expressions')}"
    2498            else:
    2499                then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
    2500        else:
    2501            then = self.sql(then_expression)
    2502        return f"WHEN {matched}{source}{condition} THEN {then}"
    2503
    2504    def merge_sql(self, expression: exp.Merge) -> str:
    2505        this = self.sql(expression, "this")
    2506        using = f"USING {self.sql(expression, 'using')}"
    2507        on = f"ON {self.sql(expression, 'on')}"
    2508        return f"MERGE INTO {this} {using} {on} {self.expressions(expression, sep=' ')}"
    2509
    2510    def tochar_sql(self, expression: exp.ToChar) -> str:
    2511        if expression.args.get("format"):
    2512            self.unsupported("Format argument unsupported for TO_CHAR/TO_VARCHAR function")
    2513
    2514        return self.sql(exp.cast(expression.this, "text"))
    2515
    2516    def dictproperty_sql(self, expression: exp.DictProperty) -> str:
    2517        this = self.sql(expression, "this")
    2518        kind = self.sql(expression, "kind")
    2519        settings_sql = self.expressions(expression, key="settings", sep=" ")
    2520        args = f"({self.sep('')}{settings_sql}{self.seg(')', sep='')}" if settings_sql else "()"
    2521        return f"{this}({kind}{args})"
    2522
    2523    def dictrange_sql(self, expression: exp.DictRange) -> str:
    2524        this = self.sql(expression, "this")
    2525        max = self.sql(expression, "max")
    2526        min = self.sql(expression, "min")
    2527        return f"{this}(MIN {min} MAX {max})"
    2528
    2529    def dictsubproperty_sql(self, expression: exp.DictSubProperty) -> str:
    2530        return f"{self.sql(expression, 'this')} {self.sql(expression, 'value')}"
    2531
    2532    def oncluster_sql(self, expression: exp.OnCluster) -> str:
    2533        return ""
    2534
    2535    def clusteredbyproperty_sql(self, expression: exp.ClusteredByProperty) -> str:
    2536        expressions = self.expressions(expression, key="expressions", flat=True)
    2537        sorted_by = self.expressions(expression, key="sorted_by", flat=True)
    2538        sorted_by = f" SORTED BY ({sorted_by})" if sorted_by else ""
    2539        buckets = self.sql(expression, "buckets")
    2540        return f"CLUSTERED BY ({expressions}){sorted_by} INTO {buckets} BUCKETS"
    2541
    2542    def anyvalue_sql(self, expression: exp.AnyValue) -> str:
    2543        this = self.sql(expression, "this")
    2544        having = self.sql(expression, "having")
    2545
    2546        if having:
    2547            this = f"{this} HAVING {'MAX' if expression.args.get('max') else 'MIN'} {having}"
    2548
    2549        return self.func("ANY_VALUE", this)
    2550
    2551    def querytransform_sql(self, expression: exp.QueryTransform) -> str:
    2552        transform = self.func("TRANSFORM", *expression.expressions)
    2553        row_format_before = self.sql(expression, "row_format_before")
    2554        row_format_before = f" {row_format_before}" if row_format_before else ""
    2555        record_writer = self.sql(expression, "record_writer")
    2556        record_writer = f" RECORDWRITER {record_writer}" if record_writer else ""
    2557        using = f" USING {self.sql(expression, 'command_script')}"
    2558        schema = self.sql(expression, "schema")
    2559        schema = f" AS {schema}" if schema else ""
    2560        row_format_after = self.sql(expression, "row_format_after")
    2561        row_format_after = f" {row_format_after}" if row_format_after else ""
    2562        record_reader = self.sql(expression, "record_reader")
    2563        record_reader = f" RECORDREADER {record_reader}" if record_reader else ""
    2564        return f"{transform}{row_format_before}{record_writer}{using}{schema}{row_format_after}{record_reader}"
    2565
    2566    def indexconstraintoption_sql(self, expression: exp.IndexConstraintOption) -> str:
    2567        key_block_size = self.sql(expression, "key_block_size")
    2568        if key_block_size:
    2569            return f"KEY_BLOCK_SIZE = {key_block_size}"
    2570
    2571        using = self.sql(expression, "using")
    2572        if using:
    2573            return f"USING {using}"
    2574
    2575        parser = self.sql(expression, "parser")
    2576        if parser:
    2577            return f"WITH PARSER {parser}"
    2578
    2579        comment = self.sql(expression, "comment")
    2580        if comment:
    2581            return f"COMMENT {comment}"
    2582
    2583        visible = expression.args.get("visible")
    2584        if visible is not None:
    2585            return "VISIBLE" if visible else "INVISIBLE"
    2586
    2587        engine_attr = self.sql(expression, "engine_attr")
    2588        if engine_attr:
    2589            return f"ENGINE_ATTRIBUTE = {engine_attr}"
    2590
    2591        secondary_engine_attr = self.sql(expression, "secondary_engine_attr")
    2592        if secondary_engine_attr:
    2593            return f"SECONDARY_ENGINE_ATTRIBUTE = {secondary_engine_attr}"
    2594
    2595        self.unsupported("Unsupported index constraint option.")
    2596        return ""
    2597
    2598    def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> str:
    2599        kind = self.sql(expression, "kind")
    2600        kind = f"{kind} INDEX" if kind else "INDEX"
    2601        this = self.sql(expression, "this")
    2602        this = f" {this}" if this else ""
    2603        type_ = self.sql(expression, "type")
    2604        type_ = f" USING {type_}" if type_ else ""
    2605        schema = self.sql(expression, "schema")
    2606        schema = f" {schema}" if schema else ""
    2607        options = self.expressions(expression, key="options", sep=" ")
    2608        options = f" {options}" if options else ""
    2609        return f"{kind}{this}{type_}{schema}{options}"

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

###### Arguments:

- pretty: Whether or not to format the produced SQL string. Default: False.
- identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True or 'always': Always quote. 'safe': Only quote identifiers that are case insensitive.
- normalize: Whether or not to normalize identifiers to lowercase. Default: False.
- pad: Determines the pad size in a formatted string. Default: 2.
- indent: Determines the indentation size in a formatted string. Default: 2.
- normalize_functions: Whether or not to normalize all function names. Possible values are: "upper" or True (default): Convert names to uppercase. "lower": Convert names to lowercase. False: Disables function name normalization.
- unsupported_level: Determines the generator's behavior when it encounters unsupported expressions. Default ErrorLevel.WARN.
- max_unsupported: Maximum number of unsupported messages to include in a raised UnsupportedError. This is only relevant if unsupported_level is ErrorLevel.RAISE. Default: 3
- leading_comma: Determines whether or not the comma is leading or trailing in select expressions. This is only relevant when generating in pretty mode. Default: False
- max_text_width: The max number of characters in a segment before creating new lines in pretty mode. The default is on the smaller end because the length only represents a segment and not the true line length. Default: 80
- comments: Whether or not to preserve comments in the output SQL code. Default: True

Generator( pretty: Optional[bool] = None, identify: str | bool = False, normalize: bool = False, pad: int = 2, indent: int = 2, normalize_functions: Union[str, bool, NoneType] = None, unsupported_level: sqlglot.errors.ErrorLevel = <ErrorLevel.WARN: 'WARN'>, max_unsupported: int = 3, leading_comma: bool = False, max_text_width: int = 80, comments: bool = True) View Source

    343    def __init__(
    344        self,
    345        pretty: t.Optional[bool] = None,
    346        identify: str | bool = False,
    347        normalize: bool = False,
    348        pad: int = 2,
    349        indent: int = 2,
    350        normalize_functions: t.Optional[str | bool] = None,
    351        unsupported_level: ErrorLevel = ErrorLevel.WARN,
    352        max_unsupported: int = 3,
    353        leading_comma: bool = False,
    354        max_text_width: int = 80,
    355        comments: bool = True,
    356    ):
    357        import sqlglot
    358
    359        self.pretty = pretty if pretty is not None else sqlglot.pretty
    360        self.identify = identify
    361        self.normalize = normalize
    362        self.pad = pad
    363        self._indent = indent
    364        self.unsupported_level = unsupported_level
    365        self.max_unsupported = max_unsupported
    366        self.leading_comma = leading_comma
    367        self.max_text_width = max_text_width
    368        self.comments = comments
    369
    370        # This is both a Dialect property and a Generator argument, so we prioritize the latter
    371        self.normalize_functions = (
    372            self.NORMALIZE_FUNCTIONS if normalize_functions is None else normalize_functions
    373        )
    374
    375        self.unsupported_messages: t.List[str] = []
    376        self._escaped_quote_end: str = self.STRING_ESCAPE + self.QUOTE_END
    377        self._escaped_identifier_end: str = self.IDENTIFIER_ESCAPE + self.IDENTIFIER_END
    378        self._cache: t.Optional[t.Dict[int, str]] = None

TRANSFORMS = {<class 'sqlglot.expressions.DateAdd'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TsOrDsAdd'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CaseSpecificColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CharacterSetColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CharacterSetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CheckColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CollateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CopyGrantsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.CommentColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.DateFormatColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.DefaultColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.EncodeColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExecuteAsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ExternalProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.HeapProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.InlineLengthColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LanguageProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LocationProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.LogProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.MaterializedProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.NoPrimaryIndexProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnCommitProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnUpdateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.PathColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ReturnsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SetProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SettingsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SqlSecurityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.StabilityProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TemporaryProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ToTableProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TransientProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.TitleColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.UppercaseColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.VarMap'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.VolatileProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.WithJournalTableProperty'>: <function Generator.<lambda>>}
NULL_ORDERING_SUPPORTED = True
LOCKING_READS_SUPPORTED = False
EXPLICIT_UNION = False
WRAP_DERIVED_VALUES = True
CREATE_FUNCTION_RETURN_AS = True
MATCHED_BY_SOURCE = True
SINGLE_STRING_INTERVAL = False
INTERVAL_ALLOWS_PLURAL_FORM = True
TABLESAMPLE_WITH_METHOD = True
TABLESAMPLE_SIZE_IS_PERCENT = False
LIMIT_FETCH = 'ALL'
RENAME_TABLE_WITH_DB = True
GROUPINGS_SEP = ','
INDEX_ON = 'ON'
JOIN_HINTS = True
TABLE_HINTS = True
QUERY_HINTS = True
QUERY_HINT_SEP = ', '
IS_BOOL_ALLOWED = True
DUPLICATE_KEY_UPDATE_WITH_SET = True
LIMIT_IS_TOP = False
RETURNING_END = True
COLUMN_JOIN_MARKS_SUPPORTED = False
EXTRACT_ALLOWS_QUOTES = True
SELECT_KINDS: Tuple[str, ...] = ('STRUCT', 'VALUE')
VALUES_AS_TABLE = True
TYPE_MAPPING = {<Type.NCHAR: 'NCHAR'>: 'CHAR', <Type.NVARCHAR: 'NVARCHAR'>: 'VARCHAR', <Type.MEDIUMTEXT: 'MEDIUMTEXT'>: 'TEXT', <Type.LONGTEXT: 'LONGTEXT'>: 'TEXT', <Type.MEDIUMBLOB: 'MEDIUMBLOB'>: 'BLOB', <Type.LONGBLOB: 'LONGBLOB'>: 'BLOB', <Type.INET: 'INET'>: 'INET'}
STAR_MAPPING = {'except': 'EXCEPT', 'replace': 'REPLACE'}
TIME_PART_SINGULARS = {'microseconds': 'microsecond', 'seconds': 'second', 'minutes': 'minute', 'hours': 'hour', 'days': 'day', 'weeks': 'week', 'months': 'month', 'quarters': 'quarter', 'years': 'year'}
TOKEN_MAPPING: Dict[sqlglot.tokens.TokenType, str] = {}
STRUCT_DELIMITER = ('<', '>')
PARAMETER_TOKEN = '@'
PROPERTIES_LOCATION = {<class 'sqlglot.expressions.AlgorithmProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.AutoIncrementProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.BlockCompressionProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.CharacterSetProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ChecksumProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.CollateProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.CopyGrantsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Cluster'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ClusteredByProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DataBlocksizeProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.DefinerProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.DictRange'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DictProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DistKeyProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.DistStyleProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.EngineProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ExecuteAsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.ExternalProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.FallbackProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.FileFormatProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.FreespaceProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.HeapProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.IsolatedLoadingProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.JournalProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.LanguageProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LikeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LocationProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.LockingProperty'>: <Location.POST_ALIAS: 'POST_ALIAS'>, <class 'sqlglot.expressions.LogProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.MaterializedProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.MergeBlockRatioProperty'>: <Location.POST_NAME: 'POST_NAME'>, <class 'sqlglot.expressions.NoPrimaryIndexProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.OnCommitProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.Order'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.PartitionedByProperty'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.PrimaryKey'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Property'>: <Location.POST_WITH: 'POST_WITH'>, <class 'sqlglot.expressions.ReturnsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatDelimitedProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.RowFormatSerdeProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SchemaCommentProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SerdeProperties'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.Set'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SettingsProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SetProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.SortKeyProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.SqlSecurityProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.StabilityProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.TemporaryProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.ToTableProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.TransientProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.MergeTreeTTL'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.VolatileProperty'>: <Location.POST_CREATE: 'POST_CREATE'>, <class 'sqlglot.expressions.WithDataProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.WithJournalTableProperty'>: <Location.POST_NAME: 'POST_NAME'>}
RESERVED_KEYWORDS: Set[str] = set()
WITH_SEPARATED_COMMENTS: Tuple[Type[sqlglot.expressions.Expression], ...] = (<class 'sqlglot.expressions.Delete'>, <class 'sqlglot.expressions.Drop'>, <class 'sqlglot.expressions.From'>, <class 'sqlglot.expressions.Insert'>, <class 'sqlglot.expressions.Select'>, <class 'sqlglot.expressions.Update'>, <class 'sqlglot.expressions.Where'>, <class 'sqlglot.expressions.With'>)
UNWRAPPED_INTERVAL_VALUES: Tuple[Type[sqlglot.expressions.Expression], ...] = (<class 'sqlglot.expressions.Column'>, <class 'sqlglot.expressions.Literal'>, <class 'sqlglot.expressions.Neg'>, <class 'sqlglot.expressions.Paren'>)
SENTINEL_LINE_BREAK = '__SQLGLOT__LB__'
INVERSE_TIME_MAPPING: Dict[str, str] = {}
INVERSE_TIME_TRIE: Dict = {}
INDEX_OFFSET = 0
UNNEST_COLUMN_ONLY = False
ALIAS_POST_TABLESAMPLE = False
IDENTIFIERS_CAN_START_WITH_DIGIT = False
STRICT_STRING_CONCAT = False
NORMALIZE_FUNCTIONS: bool | str = 'upper'
NULL_ORDERING = 'nulls_are_small'
ESCAPE_LINE_BREAK = False
@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool: View Source

    253    @classmethod
    254    def can_identify(cls, text: str, identify: str | bool = "safe") -> bool:
    255        """Checks if text can be identified given an identify option.
    256
    257        Args:
    258            text: The text to check.
    259            identify:
    260                "always" or `True`: Always returns true.
    261                "safe": True if the identifier is case-insensitive.
    262
    263        Returns:
    264            Whether or not the given text can be identified.
    265        """
    266        if identify is True or identify == "always":
    267            return True
    268
    269        if identify == "safe":
    270            return not cls.case_sensitive(text)
    271
    272        return False

Checks if text can be identified given an identify option.

###### Arguments:

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

###### Returns:

> Whether or not the given text can be identified.

QUOTE_START = "'"
QUOTE_END = "'"
IDENTIFIER_START = '"'
IDENTIFIER_END = '"'
STRING_ESCAPE = "'"
IDENTIFIER_ESCAPE = '"'
BIT_START: Optional[str] = None
BIT_END: Optional[str] = None
HEX_START: Optional[str] = None
HEX_END: Optional[str] = None
BYTE_START: Optional[str] = None
BYTE_END: Optional[str] = None
pretty
identify
normalize
pad
unsupported_level
max_unsupported
leading_comma
max_text_width
comments
normalize_functions
unsupported_messages: List[str]
def generate( self, expression: Optional[sqlglot.expressions.Expression], cache: Optional[Dict[int, str]] = None) -> str: View Source

    380    def generate(
    381        self,
    382        expression: t.Optional[exp.Expression],
    383        cache: t.Optional[t.Dict[int, str]] = None,
    384    ) -> str:
    385        """
    386        Generates the SQL string corresponding to the given syntax tree.
    387
    388        Args:
    389            expression: The syntax tree.
    390            cache: An optional sql string cache. This leverages the hash of an Expression
    391                which can be slow to compute, so only use it if you set _hash on each node.
    392
    393        Returns:
    394            The SQL string corresponding to `expression`.
    395        """
    396        if cache is not None:
    397            self._cache = cache
    398
    399        self.unsupported_messages = []
    400        sql = self.sql(expression).strip()
    401        self._cache = None
    402
    403        if self.unsupported_level == ErrorLevel.IGNORE:
    404            return sql
    405
    406        if self.unsupported_level == ErrorLevel.WARN:
    407            for msg in self.unsupported_messages:
    408                logger.warning(msg)
    409        elif self.unsupported_level == ErrorLevel.RAISE and self.unsupported_messages:
    410            raise UnsupportedError(concat_messages(self.unsupported_messages, self.max_unsupported))
    411
    412        if self.pretty:
    413            sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
    414        return sql

Generates the SQL string corresponding to the given syntax tree.

###### Arguments:

- expression: The syntax tree.
- cache: An optional sql string cache. This leverages the hash of an Expression which can be slow to compute, so only use it if you set _hash on each node.

###### Returns:

> The SQL string corresponding to `expression`.

def unsupported(self, message: str) -> None: View Source

    416    def unsupported(self, message: str) -> None:
    417        if self.unsupported_level == ErrorLevel.IMMEDIATE:
    418            raise UnsupportedError(message)
    419        self.unsupported_messages.append(message)

def sep(self, sep: str = ' ') -> str: View Source

    421    def sep(self, sep: str = " ") -> str:
    422        return f"{sep.strip()}\n" if self.pretty else sep

def seg(self, sql: str, sep: str = ' ') -> str: View Source

    424    def seg(self, sql: str, sep: str = " ") -> str:
    425        return f"{self.sep(sep)}{sql}"

def pad_comment(self, comment: str) -> str: View Source

    427    def pad_comment(self, comment: str) -> str:
    428        comment = " " + comment if comment[0].strip() else comment
    429        comment = comment + " " if comment[-1].strip() else comment
    430        return comment

def maybe_comment( self, sql: str, expression: Optional[sqlglot.expressions.Expression] = None, comments: Optional[List[str]] = None) -> str: View Source

    432    def maybe_comment(
    433        self,
    434        sql: str,
    435        expression: t.Optional[exp.Expression] = None,
    436        comments: t.Optional[t.List[str]] = None,
    437    ) -> str:
    438        comments = (
    439            ((expression and expression.comments) if comments is None else comments)  # type: ignore
    440            if self.comments
    441            else None
    442        )
    443
    444        if not comments or isinstance(expression, exp.Binary):
    445            return sql
    446
    447        sep = "\n" if self.pretty else " "
    448        comments_sql = sep.join(
    449            f"/*{self.pad_comment(comment)}*/" for comment in comments if comment
    450        )
    451
    452        if not comments_sql:
    453            return sql
    454
    455        if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
    456            return (
    457                f"{self.sep()}{comments_sql}{sql}"
    458                if sql[0].isspace()
    459                else f"{comments_sql}{self.sep()}{sql}"
    460            )
    461
    462        return f"{sql} {comments_sql}"

def wrap(self, expression: sqlglot.expressions.Expression | str) -> str: View Source

    464    def wrap(self, expression: exp.Expression | str) -> str:
    465        this_sql = self.indent(
    466            self.sql(expression)
    467            if isinstance(expression, (exp.Select, exp.Union))
    468            else self.sql(expression, "this"),
    469            level=1,
    470            pad=0,
    471        )
    472        return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"

def no_identify(self, func: Callable[..., str], *args, **kwargs) -> str: View Source

    474    def no_identify(self, func: t.Callable[..., str], *args, **kwargs) -> str:
    475        original = self.identify
    476        self.identify = False
    477        result = func(*args, **kwargs)
    478        self.identify = original
    479        return result

def normalize_func(self, name: str) -> str: View Source

    481    def normalize_func(self, name: str) -> str:
    482        if self.normalize_functions == "upper" or self.normalize_functions is True:
    483            return name.upper()
    484        if self.normalize_functions == "lower":
    485            return name.lower()
    486        return name

def indent( self, sql: str, level: int = 0, pad: Optional[int] = None, skip_first: bool = False, skip_last: bool = False) -> str: View Source

    488    def indent(
    489        self,
    490        sql: str,
    491        level: int = 0,
    492        pad: t.Optional[int] = None,
    493        skip_first: bool = False,
    494        skip_last: bool = False,
    495    ) -> str:
    496        if not self.pretty:
    497            return sql
    498
    499        pad = self.pad if pad is None else pad
    500        lines = sql.split("\n")
    501
    502        return "\n".join(
    503            line
    504            if (skip_first and i == 0) or (skip_last and i == len(lines) - 1)
    505            else f"{' ' * (level * self._indent + pad)}{line}"
    506            for i, line in enumerate(lines)
    507        )

def sql( self, expression: Union[str, sqlglot.expressions.Expression, NoneType], key: Optional[str] = None, comment: bool = True) -> str: View Source

    509    def sql(
    510        self,
    511        expression: t.Optional[str | exp.Expression],
    512        key: t.Optional[str] = None,
    513        comment: bool = True,
    514    ) -> str:
    515        if not expression:
    516            return ""
    517
    518        if isinstance(expression, str):
    519            return expression
    520
    521        if key:
    522            value = expression.args.get(key)
    523            if value:
    524                return self.sql(value)
    525            return ""
    526
    527        if self._cache is not None:
    528            expression_id = hash(expression)
    529
    530            if expression_id in self._cache:
    531                return self._cache[expression_id]
    532
    533        transform = self.TRANSFORMS.get(expression.__class__)
    534
    535        if callable(transform):
    536            sql = transform(self, expression)
    537        elif transform:
    538            sql = transform
    539        elif isinstance(expression, exp.Expression):
    540            exp_handler_name = f"{expression.key}_sql"
    541
    542            if hasattr(self, exp_handler_name):
    543                sql = getattr(self, exp_handler_name)(expression)
    544            elif isinstance(expression, exp.Func):
    545                sql = self.function_fallback_sql(expression)
    546            elif isinstance(expression, exp.Property):
    547                sql = self.property_sql(expression)
    548            else:
    549                raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
    550        else:
    551            raise ValueError(f"Expected an Expression. Received {type(expression)}: {expression}")
    552
    553        sql = self.maybe_comment(sql, expression) if self.comments and comment else sql
    554
    555        if self._cache is not None:
    556            self._cache[expression_id] = sql
    557        return sql

def uncache_sql(self, expression: sqlglot.expressions.Uncache) -> str: View Source

    559    def uncache_sql(self, expression: exp.Uncache) -> str:
    560        table = self.sql(expression, "this")
    561        exists_sql = " IF EXISTS" if expression.args.get("exists") else ""
    562        return f"UNCACHE TABLE{exists_sql} {table}"

def cache_sql(self, expression: sqlglot.expressions.Cache) -> str: View Source

    564    def cache_sql(self, expression: exp.Cache) -> str:
    565        lazy = " LAZY" if expression.args.get("lazy") else ""
    566        table = self.sql(expression, "this")
    567        options = expression.args.get("options")
    568        options = f" OPTIONS({self.sql(options[0])} = {self.sql(options[1])})" if options else ""
    569        sql = self.sql(expression, "expression")
    570        sql = f" AS{self.sep()}{sql}" if sql else ""
    571        sql = f"CACHE{lazy} TABLE {table}{options}{sql}"
    572        return self.prepend_ctes(expression, sql)

def characterset_sql(self, expression: sqlglot.expressions.CharacterSet) -> str: View Source

    574    def characterset_sql(self, expression: exp.CharacterSet) -> str:
    575        if isinstance(expression.parent, exp.Cast):
    576            return f"CHAR CHARACTER SET {self.sql(expression, 'this')}"
    577        default = "DEFAULT " if expression.args.get("default") else ""
    578        return f"{default}CHARACTER SET={self.sql(expression, 'this')}"

def column_sql(self, expression: sqlglot.expressions.Column) -> str: View Source

    580    def column_sql(self, expression: exp.Column) -> str:
    581        join_mark = " (+)" if expression.args.get("join_mark") else ""
    582
    583        if join_mark and not self.COLUMN_JOIN_MARKS_SUPPORTED:
    584            join_mark = ""
    585            self.unsupported("Outer join syntax using the (+) operator is not supported.")
    586
    587        column = ".".join(
    588            self.sql(part)
    589            for part in (
    590                expression.args.get("catalog"),
    591                expression.args.get("db"),
    592                expression.args.get("table"),
    593                expression.args.get("this"),
    594            )
    595            if part
    596        )
    597
    598        return f"{column}{join_mark}"

def columnposition_sql(self, expression: sqlglot.expressions.ColumnPosition) -> str: View Source

    600    def columnposition_sql(self, expression: exp.ColumnPosition) -> str:
    601        this = self.sql(expression, "this")
    602        this = f" {this}" if this else ""
    603        position = self.sql(expression, "position")
    604        return f"{position}{this}"

def columndef_sql(self, expression: sqlglot.expressions.ColumnDef, sep: str = ' ') -> str: View Source

    606    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
    607        column = self.sql(expression, "this")
    608        kind = self.sql(expression, "kind")
    609        constraints = self.expressions(expression, key="constraints", sep=" ", flat=True)
    610        exists = "IF NOT EXISTS " if expression.args.get("exists") else ""
    611        kind = f"{sep}{kind}" if kind else ""
    612        constraints = f" {constraints}" if constraints else ""
    613        position = self.sql(expression, "position")
    614        position = f" {position}" if position else ""
    615
    616        return f"{exists}{column}{kind}{constraints}{position}"

def columnconstraint_sql(self, expression: sqlglot.expressions.ColumnConstraint) -> str: View Source

    618    def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str:
    619        this = self.sql(expression, "this")
    620        kind_sql = self.sql(expression, "kind").strip()
    621        return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql

def autoincrementcolumnconstraint_sql(self, _) -> str: View Source

    623    def autoincrementcolumnconstraint_sql(self, _) -> str:
    624        return self.token_sql(TokenType.AUTO_INCREMENT)

def compresscolumnconstraint_sql(self, expression: sqlglot.expressions.CompressColumnConstraint) -> str: View Source

    626    def compresscolumnconstraint_sql(self, expression: exp.CompressColumnConstraint) -> str:
    627        if isinstance(expression.this, list):
    628            this = self.wrap(self.expressions(expression, key="this", flat=True))
    629        else:
    630            this = self.sql(expression, "this")
    631
    632        return f"COMPRESS {this}"

def generatedasidentitycolumnconstraint_sql( self, expression: sqlglot.expressions.GeneratedAsIdentityColumnConstraint) -> str: View Source

    634    def generatedasidentitycolumnconstraint_sql(
    635        self, expression: exp.GeneratedAsIdentityColumnConstraint
    636    ) -> str:
    637        this = ""
    638        if expression.this is not None:
    639            on_null = "ON NULL " if expression.args.get("on_null") else ""
    640            this = " ALWAYS " if expression.this else f" BY DEFAULT {on_null}"
    641
    642        start = expression.args.get("start")
    643        start = f"START WITH {start}" if start else ""
    644        increment = expression.args.get("increment")
    645        increment = f" INCREMENT BY {increment}" if increment else ""
    646        minvalue = expression.args.get("minvalue")
    647        minvalue = f" MINVALUE {minvalue}" if minvalue else ""
    648        maxvalue = expression.args.get("maxvalue")
    649        maxvalue = f" MAXVALUE {maxvalue}" if maxvalue else ""
    650        cycle = expression.args.get("cycle")
    651        cycle_sql = ""
    652
    653        if cycle is not None:
    654            cycle_sql = f"{' NO' if not cycle else ''} CYCLE"
    655            cycle_sql = cycle_sql.strip() if not start and not increment else cycle_sql
    656
    657        sequence_opts = ""
    658        if start or increment or cycle_sql:
    659            sequence_opts = f"{start}{increment}{minvalue}{maxvalue}{cycle_sql}"
    660            sequence_opts = f" ({sequence_opts.strip()})"
    661
    662        expr = self.sql(expression, "expression")
    663        expr = f"({expr})" if expr else "IDENTITY"
    664
    665        return f"GENERATED{this}AS {expr}{sequence_opts}"

def notnullcolumnconstraint_sql(self, expression: sqlglot.expressions.NotNullColumnConstraint) -> str: View Source

    667    def notnullcolumnconstraint_sql(self, expression: exp.NotNullColumnConstraint) -> str:
    668        return f"{'' if expression.args.get('allow_null') else 'NOT '}NULL"

def primarykeycolumnconstraint_sql(self, expression: sqlglot.expressions.PrimaryKeyColumnConstraint) -> str: View Source

    670    def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
    671        desc = expression.args.get("desc")
    672        if desc is not None:
    673            return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
    674        return f"PRIMARY KEY"

def uniquecolumnconstraint_sql(self, expression: sqlglot.expressions.UniqueColumnConstraint) -> str: View Source

    676    def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
    677        this = self.sql(expression, "this")
    678        this = f" {this}" if this else ""
    679        return f"UNIQUE{this}"

def createable_sql( self, expression: sqlglot.expressions.Create, locations: DefaultDict) -> str: View Source

    681    def createable_sql(self, expression: exp.Create, locations: t.DefaultDict) -> str:
    682        return self.sql(expression, "this")

def create_sql(self, expression: sqlglot.expressions.Create) -> str: View Source

    684    def create_sql(self, expression: exp.Create) -> str:
    685        kind = self.sql(expression, "kind").upper()
    686        properties = expression.args.get("properties")
    687        properties_locs = self.locate_properties(properties) if properties else defaultdict()
    688
    689        this = self.createable_sql(expression, properties_locs)
    690
    691        properties_sql = ""
    692        if properties_locs.get(exp.Properties.Location.POST_SCHEMA) or properties_locs.get(
    693            exp.Properties.Location.POST_WITH
    694        ):
    695            properties_sql = self.sql(
    696                exp.Properties(
    697                    expressions=[
    698                        *properties_locs[exp.Properties.Location.POST_SCHEMA],
    699                        *properties_locs[exp.Properties.Location.POST_WITH],
    700                    ]
    701                )
    702            )
    703
    704        begin = " BEGIN" if expression.args.get("begin") else ""
    705        expression_sql = self.sql(expression, "expression")
    706        if expression_sql:
    707            expression_sql = f"{begin}{self.sep()}{expression_sql}"
    708
    709            if self.CREATE_FUNCTION_RETURN_AS or not isinstance(expression.expression, exp.Return):
    710                if properties_locs.get(exp.Properties.Location.POST_ALIAS):
    711                    postalias_props_sql = self.properties(
    712                        exp.Properties(
    713                            expressions=properties_locs[exp.Properties.Location.POST_ALIAS]
    714                        ),
    715                        wrapped=False,
    716                    )
    717                    expression_sql = f" AS {postalias_props_sql}{expression_sql}"
    718                else:
    719                    expression_sql = f" AS{expression_sql}"
    720
    721        postindex_props_sql = ""
    722        if properties_locs.get(exp.Properties.Location.POST_INDEX):
    723            postindex_props_sql = self.properties(
    724                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_INDEX]),
    725                wrapped=False,
    726                prefix=" ",
    727            )
    728
    729        indexes = self.expressions(expression, key="indexes", indent=False, sep=" ")
    730        indexes = f" {indexes}" if indexes else ""
    731        index_sql = indexes + postindex_props_sql
    732
    733        replace = " OR REPLACE" if expression.args.get("replace") else ""
    734        unique = " UNIQUE" if expression.args.get("unique") else ""
    735
    736        postcreate_props_sql = ""
    737        if properties_locs.get(exp.Properties.Location.POST_CREATE):
    738            postcreate_props_sql = self.properties(
    739                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_CREATE]),
    740                sep=" ",
    741                prefix=" ",
    742                wrapped=False,
    743            )
    744
    745        modifiers = "".join((replace, unique, postcreate_props_sql))
    746
    747        postexpression_props_sql = ""
    748        if properties_locs.get(exp.Properties.Location.POST_EXPRESSION):
    749            postexpression_props_sql = self.properties(
    750                exp.Properties(
    751                    expressions=properties_locs[exp.Properties.Location.POST_EXPRESSION]
    752                ),
    753                sep=" ",
    754                prefix=" ",
    755                wrapped=False,
    756            )
    757
    758        exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else ""
    759        no_schema_binding = (
    760            " WITH NO SCHEMA BINDING" if expression.args.get("no_schema_binding") else ""
    761        )
    762
    763        clone = self.sql(expression, "clone")
    764        clone = f" {clone}" if clone else ""
    765
    766        expression_sql = f"CREATE{modifiers} {kind}{exists_sql} {this}{properties_sql}{expression_sql}{postexpression_props_sql}{index_sql}{no_schema_binding}{clone}"
    767        return self.prepend_ctes(expression, expression_sql)

def clone_sql(self, expression: sqlglot.expressions.Clone) -> str: View Source

    769    def clone_sql(self, expression: exp.Clone) -> str:
    770        this = self.sql(expression, "this")
    771        when = self.sql(expression, "when")
    772
    773        if when:
    774            kind = self.sql(expression, "kind")
    775            expr = self.sql(expression, "expression")
    776            return f"CLONE {this} {when} ({kind} => {expr})"
    777
    778        return f"CLONE {this}"

def describe_sql(self, expression: sqlglot.expressions.Describe) -> str: View Source

    780    def describe_sql(self, expression: exp.Describe) -> str:
    781        return f"DESCRIBE {self.sql(expression, 'this')}"

def prepend_ctes(self, expression: sqlglot.expressions.Expression, sql: str) -> str: View Source

    783    def prepend_ctes(self, expression: exp.Expression, sql: str) -> str:
    784        with_ = self.sql(expression, "with")
    785        if with_:
    786            sql = f"{with_}{self.sep()}{sql}"
    787        return sql

def with_sql(self, expression: sqlglot.expressions.With) -> str: View Source

    789    def with_sql(self, expression: exp.With) -> str:
    790        sql = self.expressions(expression, flat=True)
    791        recursive = "RECURSIVE " if expression.args.get("recursive") else ""
    792
    793        return f"WITH {recursive}{sql}"

def cte_sql(self, expression: sqlglot.expressions.CTE) -> str: View Source

    795    def cte_sql(self, expression: exp.CTE) -> str:
    796        alias = self.sql(expression, "alias")
    797        return f"{alias} AS {self.wrap(expression)}"

def tablealias_sql(self, expression: sqlglot.expressions.TableAlias) -> str: View Source

    799    def tablealias_sql(self, expression: exp.TableAlias) -> str:
    800        alias = self.sql(expression, "this")
    801        columns = self.expressions(expression, key="columns", flat=True)
    802        columns = f"({columns})" if columns else ""
    803        return f"{alias}{columns}"

def bitstring_sql(self, expression: sqlglot.expressions.BitString) -> str: View Source

    805    def bitstring_sql(self, expression: exp.BitString) -> str:
    806        this = self.sql(expression, "this")
    807        if self.BIT_START:
    808            return f"{self.BIT_START}{this}{self.BIT_END}"
    809        return f"{int(this, 2)}"

def hexstring_sql(self, expression: sqlglot.expressions.HexString) -> str: View Source

    811    def hexstring_sql(self, expression: exp.HexString) -> str:
    812        this = self.sql(expression, "this")
    813        if self.HEX_START:
    814            return f"{self.HEX_START}{this}{self.HEX_END}"
    815        return f"{int(this, 16)}"

def bytestring_sql(self, expression: sqlglot.expressions.ByteString) -> str: View Source

    817    def bytestring_sql(self, expression: exp.ByteString) -> str:
    818        this = self.sql(expression, "this")
    819        if self.BYTE_START:
    820            return f"{self.BYTE_START}{this}{self.BYTE_END}"
    821        return this

def rawstring_sql(self, expression: sqlglot.expressions.RawString) -> str: View Source

    823    def rawstring_sql(self, expression: exp.RawString) -> str:
    824        string = self.escape_str(expression.this.replace("\\", "\\\\"))
    825        return f"{self.QUOTE_START}{string}{self.QUOTE_END}"

def datatypesize_sql(self, expression: sqlglot.expressions.DataTypeSize) -> str: View Source

    827    def datatypesize_sql(self, expression: exp.DataTypeSize) -> str:
    828        this = self.sql(expression, "this")
    829        specifier = self.sql(expression, "expression")
    830        specifier = f" {specifier}" if specifier else ""
    831        return f"{this}{specifier}"

def datatype_sql(self, expression: sqlglot.expressions.DataType) -> str: View Source

    833    def datatype_sql(self, expression: exp.DataType) -> str:
    834        type_value = expression.this
    835        type_sql = (
    836            self.TYPE_MAPPING.get(type_value, type_value.value)
    837            if isinstance(type_value, exp.DataType.Type)
    838            else type_value
    839        )
    840        nested = ""
    841        interior = self.expressions(expression, flat=True)
    842        values = ""
    843        if interior:
    844            if expression.args.get("nested"):
    845                nested = f"{self.STRUCT_DELIMITER[0]}{interior}{self.STRUCT_DELIMITER[1]}"
    846                if expression.args.get("values") is not None:
    847                    delimiters = ("[", "]") if type_value == exp.DataType.Type.ARRAY else ("(", ")")
    848                    values = self.expressions(expression, key="values", flat=True)
    849                    values = f"{delimiters[0]}{values}{delimiters[1]}"
    850            else:
    851                nested = f"({interior})"
    852
    853        return f"{type_sql}{nested}{values}"

def directory_sql(self, expression: sqlglot.expressions.Directory) -> str: View Source

    855    def directory_sql(self, expression: exp.Directory) -> str:
    856        local = "LOCAL " if expression.args.get("local") else ""
    857        row_format = self.sql(expression, "row_format")
    858        row_format = f" {row_format}" if row_format else ""
    859        return f"{local}DIRECTORY {self.sql(expression, 'this')}{row_format}"

def delete_sql(self, expression: sqlglot.expressions.Delete) -> str: View Source

    861    def delete_sql(self, expression: exp.Delete) -> str:
    862        this = self.sql(expression, "this")
    863        this = f" FROM {this}" if this else ""
    864        using = self.sql(expression, "using")
    865        using = f" USING {using}" if using else ""
    866        where = self.sql(expression, "where")
    867        returning = self.sql(expression, "returning")
    868        limit = self.sql(expression, "limit")
    869        tables = self.expressions(expression, key="tables")
    870        tables = f" {tables}" if tables else ""
    871        if self.RETURNING_END:
    872            expression_sql = f"{this}{using}{where}{returning}{limit}"
    873        else:
    874            expression_sql = f"{returning}{this}{using}{where}{limit}"
    875        return self.prepend_ctes(expression, f"DELETE{tables}{expression_sql}")

def drop_sql(self, expression: sqlglot.expressions.Drop) -> str: View Source

    877    def drop_sql(self, expression: exp.Drop) -> str:
    878        this = self.sql(expression, "this")
    879        kind = expression.args["kind"]
    880        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    881        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    882        materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
    883        cascade = " CASCADE" if expression.args.get("cascade") else ""
    884        constraints = " CONSTRAINTS" if expression.args.get("constraints") else ""
    885        purge = " PURGE" if expression.args.get("purge") else ""
    886        return (
    887            f"DROP{temporary}{materialized} {kind}{exists_sql}{this}{cascade}{constraints}{purge}"
    888        )

def except_sql(self, expression: sqlglot.expressions.Except) -> str: View Source

    890    def except_sql(self, expression: exp.Except) -> str:
    891        return self.prepend_ctes(
    892            expression,
    893            self.set_operation(expression, self.except_op(expression)),
    894        )

def except_op(self, expression: sqlglot.expressions.Except) -> str: View Source

    896    def except_op(self, expression: exp.Except) -> str:
    897        return f"EXCEPT{'' if expression.args.get('distinct') else ' ALL'}"

def fetch_sql(self, expression: sqlglot.expressions.Fetch) -> str: View Source

    899    def fetch_sql(self, expression: exp.Fetch) -> str:
    900        direction = expression.args.get("direction")
    901        direction = f" {direction.upper()}" if direction else ""
    902        count = expression.args.get("count")
    903        count = f" {count}" if count else ""
    904        if expression.args.get("percent"):
    905            count = f"{count} PERCENT"
    906        with_ties_or_only = "WITH TIES" if expression.args.get("with_ties") else "ONLY"
    907        return f"{self.seg('FETCH')}{direction}{count} ROWS {with_ties_or_only}"

def filter_sql(self, expression: sqlglot.expressions.Filter) -> str: View Source

    909    def filter_sql(self, expression: exp.Filter) -> str:
    910        this = self.sql(expression, "this")
    911        where = self.sql(expression, "expression")[1:]  # where has a leading space
    912        return f"{this} FILTER({where})"

def hint_sql(self, expression: sqlglot.expressions.Hint) -> str: View Source

    914    def hint_sql(self, expression: exp.Hint) -> str:
    915        if not self.QUERY_HINTS:
    916            self.unsupported("Hints are not supported")
    917            return ""
    918
    919        return f" /*+ {self.expressions(expression, sep=self.QUERY_HINT_SEP).strip()} */"

def index_sql(self, expression: sqlglot.expressions.Index) -> str: View Source

    921    def index_sql(self, expression: exp.Index) -> str:
    922        unique = "UNIQUE " if expression.args.get("unique") else ""
    923        primary = "PRIMARY " if expression.args.get("primary") else ""
    924        amp = "AMP " if expression.args.get("amp") else ""
    925        name = self.sql(expression, "this")
    926        name = f"{name} " if name else ""
    927        table = self.sql(expression, "table")
    928        table = f"{self.INDEX_ON} {table} " if table else ""
    929        using = self.sql(expression, "using")
    930        using = f"USING {using} " if using else ""
    931        index = "INDEX " if not table else ""
    932        columns = self.expressions(expression, key="columns", flat=True)
    933        columns = f"({columns})" if columns else ""
    934        partition_by = self.expressions(expression, key="partition_by", flat=True)
    935        partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
    936        return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}"

def identifier_sql(self, expression: sqlglot.expressions.Identifier) -> str: View Source

    938    def identifier_sql(self, expression: exp.Identifier) -> str:
    939        text = expression.name
    940        lower = text.lower()
    941        text = lower if self.normalize and not expression.quoted else text
    942        text = text.replace(self.IDENTIFIER_END, self._escaped_identifier_end)
    943        if (
    944            expression.quoted
    945            or self.can_identify(text, self.identify)
    946            or lower in self.RESERVED_KEYWORDS
    947            or (not self.IDENTIFIERS_CAN_START_WITH_DIGIT and text[:1].isdigit())
    948        ):
    949            text = f"{self.IDENTIFIER_START}{text}{self.IDENTIFIER_END}"
    950        return text

def inputoutputformat_sql(self, expression: sqlglot.expressions.InputOutputFormat) -> str: View Source

    952    def inputoutputformat_sql(self, expression: exp.InputOutputFormat) -> str:
    953        input_format = self.sql(expression, "input_format")
    954        input_format = f"INPUTFORMAT {input_format}" if input_format else ""
    955        output_format = self.sql(expression, "output_format")
    956        output_format = f"OUTPUTFORMAT {output_format}" if output_format else ""
    957        return self.sep().join((input_format, output_format))

def national_sql(self, expression: sqlglot.expressions.National, prefix: str = 'N') -> str: View Source

    959    def national_sql(self, expression: exp.National, prefix: str = "N") -> str:
    960        string = self.sql(exp.Literal.string(expression.name))
    961        return f"{prefix}{string}"

def partition_sql(self, expression: sqlglot.expressions.Partition) -> str: View Source

    963    def partition_sql(self, expression: exp.Partition) -> str:
    964        return f"PARTITION({self.expressions(expression, flat=True)})"

def properties_sql(self, expression: sqlglot.expressions.Properties) -> str: View Source

    966    def properties_sql(self, expression: exp.Properties) -> str:
    967        root_properties = []
    968        with_properties = []
    969
    970        for p in expression.expressions:
    971            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    972            if p_loc == exp.Properties.Location.POST_WITH:
    973                with_properties.append(p.copy())
    974            elif p_loc == exp.Properties.Location.POST_SCHEMA:
    975                root_properties.append(p.copy())
    976
    977        return self.root_properties(
    978            exp.Properties(expressions=root_properties)
    979        ) + self.with_properties(exp.Properties(expressions=with_properties))

def root_properties(self, properties: sqlglot.expressions.Properties) -> str: View Source

    981    def root_properties(self, properties: exp.Properties) -> str:
    982        if properties.expressions:
    983            return self.sep() + self.expressions(properties, indent=False, sep=" ")
    984        return ""

def properties( self, properties: sqlglot.expressions.Properties, prefix: str = '', sep: str = ', ', suffix: str = '', wrapped: bool = True) -> str: View Source

    986    def properties(
    987        self,
    988        properties: exp.Properties,
    989        prefix: str = "",
    990        sep: str = ", ",
    991        suffix: str = "",
    992        wrapped: bool = True,
    993    ) -> str:
    994        if properties.expressions:
    995            expressions = self.expressions(properties, sep=sep, indent=False)
    996            if expressions:
    997                expressions = self.wrap(expressions) if wrapped else expressions
    998                return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
    999        return ""

def with_properties(self, properties: sqlglot.expressions.Properties) -> str: View Source

    1001    def with_properties(self, properties: exp.Properties) -> str:
    1002        return self.properties(properties, prefix=self.seg("WITH"))

def locate_properties(self, properties: sqlglot.expressions.Properties) -> DefaultDict: View Source

    1004    def locate_properties(self, properties: exp.Properties) -> t.DefaultDict:
    1005        properties_locs = defaultdict(list)
    1006        for p in properties.expressions:
    1007            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    1008            if p_loc != exp.Properties.Location.UNSUPPORTED:
    1009                properties_locs[p_loc].append(p.copy())
    1010            else:
    1011                self.unsupported(f"Unsupported property {p.key}")
    1012
    1013        return properties_locs

def property_sql(self, expression: sqlglot.expressions.Property) -> str: View Source

    1015    def property_sql(self, expression: exp.Property) -> str:
    1016        property_cls = expression.__class__
    1017        if property_cls == exp.Property:
    1018            return f"{expression.name}={self.sql(expression, 'value')}"
    1019
    1020        property_name = exp.Properties.PROPERTY_TO_NAME.get(property_cls)
    1021        if not property_name:
    1022            self.unsupported(f"Unsupported property {expression.key}")
    1023
    1024        return f"{property_name}={self.sql(expression, 'this')}"

def likeproperty_sql(self, expression: sqlglot.expressions.LikeProperty) -> str: View Source

    1026    def likeproperty_sql(self, expression: exp.LikeProperty) -> str:
    1027        options = " ".join(f"{e.name} {self.sql(e, 'value')}" for e in expression.expressions)
    1028        options = f" {options}" if options else ""
    1029        return f"LIKE {self.sql(expression, 'this')}{options}"

def fallbackproperty_sql(self, expression: sqlglot.expressions.FallbackProperty) -> str: View Source

    1031    def fallbackproperty_sql(self, expression: exp.FallbackProperty) -> str:
    1032        no = "NO " if expression.args.get("no") else ""
    1033        protection = " PROTECTION" if expression.args.get("protection") else ""
    1034        return f"{no}FALLBACK{protection}"

def journalproperty_sql(self, expression: sqlglot.expressions.JournalProperty) -> str: View Source

    1036    def journalproperty_sql(self, expression: exp.JournalProperty) -> str:
    1037        no = "NO " if expression.args.get("no") else ""
    1038        local = expression.args.get("local")
    1039        local = f"{local} " if local else ""
    1040        dual = "DUAL " if expression.args.get("dual") else ""
    1041        before = "BEFORE " if expression.args.get("before") else ""
    1042        after = "AFTER " if expression.args.get("after") else ""
    1043        return f"{no}{local}{dual}{before}{after}JOURNAL"

def freespaceproperty_sql(self, expression: sqlglot.expressions.FreespaceProperty) -> str: View Source

    1045    def freespaceproperty_sql(self, expression: exp.FreespaceProperty) -> str:
    1046        freespace = self.sql(expression, "this")
    1047        percent = " PERCENT" if expression.args.get("percent") else ""
    1048        return f"FREESPACE={freespace}{percent}"

def checksumproperty_sql(self, expression: sqlglot.expressions.ChecksumProperty) -> str: View Source

    1050    def checksumproperty_sql(self, expression: exp.ChecksumProperty) -> str:
    1051        if expression.args.get("default"):
    1052            property = "DEFAULT"
    1053        elif expression.args.get("on"):
    1054            property = "ON"
    1055        else:
    1056            property = "OFF"
    1057        return f"CHECKSUM={property}"

def mergeblockratioproperty_sql(self, expression: sqlglot.expressions.MergeBlockRatioProperty) -> str: View Source

    1059    def mergeblockratioproperty_sql(self, expression: exp.MergeBlockRatioProperty) -> str:
    1060        if expression.args.get("no"):
    1061            return "NO MERGEBLOCKRATIO"
    1062        if expression.args.get("default"):
    1063            return "DEFAULT MERGEBLOCKRATIO"
    1064
    1065        percent = " PERCENT" if expression.args.get("percent") else ""
    1066        return f"MERGEBLOCKRATIO={self.sql(expression, 'this')}{percent}"

def datablocksizeproperty_sql(self, expression: sqlglot.expressions.DataBlocksizeProperty) -> str: View Source

    1068    def datablocksizeproperty_sql(self, expression: exp.DataBlocksizeProperty) -> str:
    1069        default = expression.args.get("default")
    1070        minimum = expression.args.get("minimum")
    1071        maximum = expression.args.get("maximum")
    1072        if default or minimum or maximum:
    1073            if default:
    1074                prop = "DEFAULT"
    1075            elif minimum:
    1076                prop = "MINIMUM"
    1077            else:
    1078                prop = "MAXIMUM"
    1079            return f"{prop} DATABLOCKSIZE"
    1080        units = expression.args.get("units")
    1081        units = f" {units}" if units else ""
    1082        return f"DATABLOCKSIZE={self.sql(expression, 'size')}{units}"

def blockcompressionproperty_sql(self, expression: sqlglot.expressions.BlockCompressionProperty) -> str: View Source

    1084    def blockcompressionproperty_sql(self, expression: exp.BlockCompressionProperty) -> str:
    1085        autotemp = expression.args.get("autotemp")
    1086        always = expression.args.get("always")
    1087        default = expression.args.get("default")
    1088        manual = expression.args.get("manual")
    1089        never = expression.args.get("never")
    1090
    1091        if autotemp is not None:
    1092            prop = f"AUTOTEMP({self.expressions(autotemp)})"
    1093        elif always:
    1094            prop = "ALWAYS"
    1095        elif default:
    1096            prop = "DEFAULT"
    1097        elif manual:
    1098            prop = "MANUAL"
    1099        elif never:
    1100            prop = "NEVER"
    1101        return f"BLOCKCOMPRESSION={prop}"

def isolatedloadingproperty_sql(self, expression: sqlglot.expressions.IsolatedLoadingProperty) -> str: View Source

    1103    def isolatedloadingproperty_sql(self, expression: exp.IsolatedLoadingProperty) -> str:
    1104        no = expression.args.get("no")
    1105        no = " NO" if no else ""
    1106        concurrent = expression.args.get("concurrent")
    1107        concurrent = " CONCURRENT" if concurrent else ""
    1108
    1109        for_ = ""
    1110        if expression.args.get("for_all"):
    1111            for_ = " FOR ALL"
    1112        elif expression.args.get("for_insert"):
    1113            for_ = " FOR INSERT"
    1114        elif expression.args.get("for_none"):
    1115            for_ = " FOR NONE"
    1116        return f"WITH{no}{concurrent} ISOLATED LOADING{for_}"

def lockingproperty_sql(self, expression: sqlglot.expressions.LockingProperty) -> str: View Source

    1118    def lockingproperty_sql(self, expression: exp.LockingProperty) -> str:
    1119        kind = expression.args.get("kind")
    1120        this = f" {self.sql(expression, 'this')}" if expression.this else ""
    1121        for_or_in = expression.args.get("for_or_in")
    1122        lock_type = expression.args.get("lock_type")
    1123        override = " OVERRIDE" if expression.args.get("override") else ""
    1124        return f"LOCKING {kind}{this} {for_or_in} {lock_type}{override}"

def withdataproperty_sql(self, expression: sqlglot.expressions.WithDataProperty) -> str: View Source

    1126    def withdataproperty_sql(self, expression: exp.WithDataProperty) -> str:
    1127        data_sql = f"WITH {'NO ' if expression.args.get('no') else ''}DATA"
    1128        statistics = expression.args.get("statistics")
    1129        statistics_sql = ""
    1130        if statistics is not None:
    1131            statistics_sql = f" AND {'NO ' if not statistics else ''}STATISTICS"
    1132        return f"{data_sql}{statistics_sql}"

def insert_sql(self, expression: sqlglot.expressions.Insert) -> str: View Source

    1134    def insert_sql(self, expression: exp.Insert) -> str:
    1135        overwrite = expression.args.get("overwrite")
    1136
    1137        if isinstance(expression.this, exp.Directory):
    1138            this = " OVERWRITE" if overwrite else " INTO"
    1139        else:
    1140            this = " OVERWRITE TABLE" if overwrite else " INTO"
    1141
    1142        alternative = expression.args.get("alternative")
    1143        alternative = f" OR {alternative}" if alternative else ""
    1144        ignore = " IGNORE" if expression.args.get("ignore") else ""
    1145
    1146        this = f"{this} {self.sql(expression, 'this')}"
    1147
    1148        exists = " IF EXISTS" if expression.args.get("exists") else ""
    1149        partition_sql = (
    1150            f" {self.sql(expression, 'partition')}" if expression.args.get("partition") else ""
    1151        )
    1152        where = self.sql(expression, "where")
    1153        where = f"{self.sep()}REPLACE WHERE {where}" if where else ""
    1154        expression_sql = f"{self.sep()}{self.sql(expression, 'expression')}"
    1155        conflict = self.sql(expression, "conflict")
    1156        returning = self.sql(expression, "returning")
    1157
    1158        if self.RETURNING_END:
    1159            expression_sql = f"{expression_sql}{conflict}{returning}"
    1160        else:
    1161            expression_sql = f"{returning}{expression_sql}{conflict}"
    1162
    1163        sql = f"INSERT{alternative}{ignore}{this}{exists}{partition_sql}{where}{expression_sql}"
    1164        return self.prepend_ctes(expression, sql)

def intersect_sql(self, expression: sqlglot.expressions.Intersect) -> str: View Source

    1166    def intersect_sql(self, expression: exp.Intersect) -> str:
    1167        return self.prepend_ctes(
    1168            expression,
    1169            self.set_operation(expression, self.intersect_op(expression)),
    1170        )

def intersect_op(self, expression: sqlglot.expressions.Intersect) -> str: View Source

    1172    def intersect_op(self, expression: exp.Intersect) -> str:
    1173        return f"INTERSECT{'' if expression.args.get('distinct') else ' ALL'}"

def introducer_sql(self, expression: sqlglot.expressions.Introducer) -> str: View Source

    1175    def introducer_sql(self, expression: exp.Introducer) -> str:
    1176        return f"{self.sql(expression, 'this')} {self.sql(expression, 'expression')}"

def pseudotype_sql(self, expression: sqlglot.expressions.PseudoType) -> str: View Source

    1178    def pseudotype_sql(self, expression: exp.PseudoType) -> str:
    1179        return expression.name.upper()

def onconflict_sql(self, expression: sqlglot.expressions.OnConflict) -> str: View Source

    1181    def onconflict_sql(self, expression: exp.OnConflict) -> str:
    1182        conflict = "ON DUPLICATE KEY" if expression.args.get("duplicate") else "ON CONFLICT"
    1183        constraint = self.sql(expression, "constraint")
    1184        if constraint:
    1185            constraint = f"ON CONSTRAINT {constraint}"
    1186        key = self.expressions(expression, key="key", flat=True)
    1187        do = "" if expression.args.get("duplicate") else " DO "
    1188        nothing = "NOTHING" if expression.args.get("nothing") else ""
    1189        expressions = self.expressions(expression, flat=True)
    1190        set_keyword = "SET " if self.DUPLICATE_KEY_UPDATE_WITH_SET else ""
    1191        if expressions:
    1192            expressions = f"UPDATE {set_keyword}{expressions}"
    1193        return f"{self.seg(conflict)} {constraint}{key}{do}{nothing}{expressions}"

def returning_sql(self, expression: sqlglot.expressions.Returning) -> str: View Source

    1195    def returning_sql(self, expression: exp.Returning) -> str:
    1196        return f"{self.seg('RETURNING')} {self.expressions(expression, flat=True)}"

def rowformatdelimitedproperty_sql(self, expression: sqlglot.expressions.RowFormatDelimitedProperty) -> str: View Source

    1198    def rowformatdelimitedproperty_sql(self, expression: exp.RowFormatDelimitedProperty) -> str:
    1199        fields = expression.args.get("fields")
    1200        fields = f" FIELDS TERMINATED BY {fields}" if fields else ""
    1201        escaped = expression.args.get("escaped")
    1202        escaped = f" ESCAPED BY {escaped}" if escaped else ""
    1203        items = expression.args.get("collection_items")
    1204        items = f" COLLECTION ITEMS TERMINATED BY {items}" if items else ""
    1205        keys = expression.args.get("map_keys")
    1206        keys = f" MAP KEYS TERMINATED BY {keys}" if keys else ""
    1207        lines = expression.args.get("lines")
    1208        lines = f" LINES TERMINATED BY {lines}" if lines else ""
    1209        null = expression.args.get("null")
    1210        null = f" NULL DEFINED AS {null}" if null else ""
    1211        return f"ROW FORMAT DELIMITED{fields}{escaped}{items}{keys}{lines}{null}"

def withtablehint_sql(self, expression: sqlglot.expressions.WithTableHint) -> str: View Source

    1213    def withtablehint_sql(self, expression: exp.WithTableHint) -> str:
    1214        return f"WITH ({self.expressions(expression, flat=True)})"

def indextablehint_sql(self, expression: sqlglot.expressions.IndexTableHint) -> str: View Source

    1216    def indextablehint_sql(self, expression: exp.IndexTableHint) -> str:
    1217        this = f"{self.sql(expression, 'this')} INDEX"
    1218        target = self.sql(expression, "target")
    1219        target = f" FOR {target}" if target else ""
    1220        return f"{this}{target} ({self.expressions(expression, flat=True)})"

def table_sql(self, expression: sqlglot.expressions.Table, sep: str = ' AS ') -> str: View Source

    1222    def table_sql(self, expression: exp.Table, sep: str = " AS ") -> str:
    1223        table = ".".join(
    1224            part
    1225            for part in [
    1226                self.sql(expression, "catalog"),
    1227                self.sql(expression, "db"),
    1228                self.sql(expression, "this"),
    1229            ]
    1230            if part
    1231        )
    1232
    1233        alias = self.sql(expression, "alias")
    1234        alias = f"{sep}{alias}" if alias else ""
    1235        hints = self.expressions(expression, key="hints", sep=" ")
    1236        hints = f" {hints}" if hints and self.TABLE_HINTS else ""
    1237        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1238        pivots = f" {pivots}" if pivots else ""
    1239        joins = self.expressions(expression, key="joins", sep="", skip_first=True)
    1240        laterals = self.expressions(expression, key="laterals", sep="")
    1241        system_time = expression.args.get("system_time")
    1242        system_time = f" {self.sql(expression, 'system_time')}" if system_time else ""
    1243
    1244        return f"{table}{system_time}{alias}{hints}{pivots}{joins}{laterals}"

def tablesample_sql( self, expression: sqlglot.expressions.TableSample, seed_prefix: str = 'SEED', sep=' AS ') -> str: View Source

    1246    def tablesample_sql(
    1247        self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    1248    ) -> str:
    1249        if self.ALIAS_POST_TABLESAMPLE and expression.this.alias:
    1250            table = expression.this.copy()
    1251            table.set("alias", None)
    1252            this = self.sql(table)
    1253            alias = f"{sep}{self.sql(expression.this, 'alias')}"
    1254        else:
    1255            this = self.sql(expression, "this")
    1256            alias = ""
    1257        method = self.sql(expression, "method")
    1258        method = f"{method.upper()} " if method and self.TABLESAMPLE_WITH_METHOD else ""
    1259        numerator = self.sql(expression, "bucket_numerator")
    1260        denominator = self.sql(expression, "bucket_denominator")
    1261        field = self.sql(expression, "bucket_field")
    1262        field = f" ON {field}" if field else ""
    1263        bucket = f"BUCKET {numerator} OUT OF {denominator}{field}" if numerator else ""
    1264        percent = self.sql(expression, "percent")
    1265        percent = f"{percent} PERCENT" if percent else ""
    1266        rows = self.sql(expression, "rows")
    1267        rows = f"{rows} ROWS" if rows else ""
    1268        size = self.sql(expression, "size")
    1269        if size and self.TABLESAMPLE_SIZE_IS_PERCENT:
    1270            size = f"{size} PERCENT"
    1271        seed = self.sql(expression, "seed")
    1272        seed = f" {seed_prefix} ({seed})" if seed else ""
    1273        kind = expression.args.get("kind", "TABLESAMPLE")
    1274        return f"{this} {kind} {method}({bucket}{percent}{rows}{size}){seed}{alias}"

def pivot_sql(self, expression: sqlglot.expressions.Pivot) -> str: View Source

    1276    def pivot_sql(self, expression: exp.Pivot) -> str:
    1277        expressions = self.expressions(expression, flat=True)
    1278
    1279        if expression.this:
    1280            this = self.sql(expression, "this")
    1281            on = f"{self.seg('ON')} {expressions}"
    1282            using = self.expressions(expression, key="using", flat=True)
    1283            using = f"{self.seg('USING')} {using}" if using else ""
    1284            group = self.sql(expression, "group")
    1285            return f"PIVOT {this}{on}{using}{group}"
    1286
    1287        alias = self.sql(expression, "alias")
    1288        alias = f" AS {alias}" if alias else ""
    1289        unpivot = expression.args.get("unpivot")
    1290        direction = "UNPIVOT" if unpivot else "PIVOT"
    1291        field = self.sql(expression, "field")
    1292        return f"{direction}({expressions} FOR {field}){alias}"

def tuple_sql(self, expression: sqlglot.expressions.Tuple) -> str: View Source

    1294    def tuple_sql(self, expression: exp.Tuple) -> str:
    1295        return f"({self.expressions(expression, flat=True)})"

def update_sql(self, expression: sqlglot.expressions.Update) -> str: View Source

    1297    def update_sql(self, expression: exp.Update) -> str:
    1298        this = self.sql(expression, "this")
    1299        set_sql = self.expressions(expression, flat=True)
    1300        from_sql = self.sql(expression, "from")
    1301        where_sql = self.sql(expression, "where")
    1302        returning = self.sql(expression, "returning")
    1303        limit = self.sql(expression, "limit")
    1304        if self.RETURNING_END:
    1305            expression_sql = f"{from_sql}{where_sql}{returning}{limit}"
    1306        else:
    1307            expression_sql = f"{returning}{from_sql}{where_sql}{limit}"
    1308        sql = f"UPDATE {this} SET {set_sql}{expression_sql}"
    1309        return self.prepend_ctes(expression, sql)

def values_sql(self, expression: sqlglot.expressions.Values) -> str: View Source

    1311    def values_sql(self, expression: exp.Values) -> str:
    1312        # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example
    1313        if self.VALUES_AS_TABLE or not expression.find_ancestor(exp.From, exp.Join):
    1314            args = self.expressions(expression)
    1315            alias = self.sql(expression, "alias")
    1316            values = f"VALUES{self.seg('')}{args}"
    1317            values = (
    1318                f"({values})"
    1319                if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
    1320                else values
    1321            )
    1322            return f"{values} AS {alias}" if alias else values
    1323
    1324        # Converts `VALUES...` expression into a series of select unions.
    1325        # Note: If you have a lot of unions then this will result in a large number of recursive statements to
    1326        # evaluate the expression. You may need to increase `sys.setrecursionlimit` to run and it can also be
    1327        # very slow.
    1328        expression = expression.copy()
    1329        column_names = expression.alias and expression.args["alias"].columns
    1330
    1331        selects = []
    1332
    1333        for i, tup in enumerate(expression.expressions):
    1334            row = tup.expressions
    1335
    1336            if i == 0 and column_names:
    1337                row = [
    1338                    exp.alias_(value, column_name) for value, column_name in zip(row, column_names)
    1339                ]
    1340
    1341            selects.append(exp.Select(expressions=row))
    1342
    1343        subquery_expression: exp.Select | exp.Union = selects[0]
    1344        if len(selects) > 1:
    1345            for select in selects[1:]:
    1346                subquery_expression = exp.union(
    1347                    subquery_expression, select, distinct=False, copy=False
    1348                )
    1349
    1350        return self.subquery_sql(subquery_expression.subquery(expression.alias, copy=False))

def var_sql(self, expression: sqlglot.expressions.Var) -> str: View Source

    1352    def var_sql(self, expression: exp.Var) -> str:
    1353        return self.sql(expression, "this")

def into_sql(self, expression: sqlglot.expressions.Into) -> str: View Source

    1355    def into_sql(self, expression: exp.Into) -> str:
    1356        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    1357        unlogged = " UNLOGGED" if expression.args.get("unlogged") else ""
    1358        return f"{self.seg('INTO')}{temporary or unlogged} {self.sql(expression, 'this')}"

def from_sql(self, expression: sqlglot.expressions.From) -> str: View Source

    1360    def from_sql(self, expression: exp.From) -> str:
    1361        return f"{self.seg('FROM')} {self.sql(expression, 'this')}"

def group_sql(self, expression: sqlglot.expressions.Group) -> str: View Source

    1363    def group_sql(self, expression: exp.Group) -> str:
    1364        group_by = self.op_expressions("GROUP BY", expression)
    1365
    1366        if expression.args.get("all"):
    1367            return f"{group_by} ALL"
    1368
    1369        grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
    1370        grouping_sets = (
    1371            f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
    1372        )
    1373
    1374        cube = expression.args.get("cube", [])
    1375        if seq_get(cube, 0) is True:
    1376            return f"{group_by}{self.seg('WITH CUBE')}"
    1377        else:
    1378            cube_sql = self.expressions(expression, key="cube", indent=False)
    1379            cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else ""
    1380
    1381        rollup = expression.args.get("rollup", [])
    1382        if seq_get(rollup, 0) is True:
    1383            return f"{group_by}{self.seg('WITH ROLLUP')}"
    1384        else:
    1385            rollup_sql = self.expressions(expression, key="rollup", indent=False)
    1386            rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else ""
    1387
    1388        groupings = csv(
    1389            grouping_sets,
    1390            cube_sql,
    1391            rollup_sql,
    1392            self.seg("WITH TOTALS") if expression.args.get("totals") else "",
    1393            sep=self.GROUPINGS_SEP,
    1394        )
    1395
    1396        if expression.args.get("expressions") and groupings:
    1397            group_by = f"{group_by}{self.GROUPINGS_SEP}"
    1398
    1399        return f"{group_by}{groupings}"

def having_sql(self, expression: sqlglot.expressions.Having) -> str: View Source

    1401    def having_sql(self, expression: exp.Having) -> str:
    1402        this = self.indent(self.sql(expression, "this"))
    1403        return f"{self.seg('HAVING')}{self.sep()}{this}"

def join_sql(self, expression: sqlglot.expressions.Join) -> str: View Source

    1405    def join_sql(self, expression: exp.Join) -> str:
    1406        op_sql = " ".join(
    1407            op
    1408            for op in (
    1409                expression.method,
    1410                "GLOBAL" if expression.args.get("global") else None,
    1411                expression.side,
    1412                expression.kind,
    1413                expression.hint if self.JOIN_HINTS else None,
    1414            )
    1415            if op
    1416        )
    1417        on_sql = self.sql(expression, "on")
    1418        using = expression.args.get("using")
    1419
    1420        if not on_sql and using:
    1421            on_sql = csv(*(self.sql(column) for column in using))
    1422
    1423        this_sql = self.sql(expression, "this")
    1424
    1425        if on_sql:
    1426            on_sql = self.indent(on_sql, skip_first=True)
    1427            space = self.seg(" " * self.pad) if self.pretty else " "
    1428            if using:
    1429                on_sql = f"{space}USING ({on_sql})"
    1430            else:
    1431                on_sql = f"{space}ON {on_sql}"
    1432        elif not op_sql:
    1433            return f", {this_sql}"
    1434
    1435        op_sql = f"{op_sql} JOIN" if op_sql else "JOIN"
    1436        return f"{self.seg(op_sql)} {this_sql}{on_sql}"

def lambda_sql( self, expression: sqlglot.expressions.Lambda, arrow_sep: str = '->') -> str: View Source

    1438    def lambda_sql(self, expression: exp.Lambda, arrow_sep: str = "->") -> str:
    1439        args = self.expressions(expression, flat=True)
    1440        args = f"({args})" if len(args.split(",")) > 1 else args
    1441        return f"{args} {arrow_sep} {self.sql(expression, 'this')}"

def lateral_sql(self, expression: sqlglot.expressions.Lateral) -> str: View Source

    1443    def lateral_sql(self, expression: exp.Lateral) -> str:
    1444        this = self.sql(expression, "this")
    1445
    1446        if isinstance(expression.this, exp.Subquery):
    1447            return f"LATERAL {this}"
    1448
    1449        if expression.args.get("view"):
    1450            alias = expression.args["alias"]
    1451            columns = self.expressions(alias, key="columns", flat=True)
    1452            table = f" {alias.name}" if alias.name else ""
    1453            columns = f" AS {columns}" if columns else ""
    1454            op_sql = self.seg(f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}")
    1455            return f"{op_sql}{self.sep()}{this}{table}{columns}"
    1456
    1457        alias = self.sql(expression, "alias")
    1458        alias = f" AS {alias}" if alias else ""
    1459        return f"LATERAL {this}{alias}"

def limit_sql(self, expression: sqlglot.expressions.Limit, top: bool = False) -> str: View Source

    1461    def limit_sql(self, expression: exp.Limit, top: bool = False) -> str:
    1462        this = self.sql(expression, "this")
    1463        args = ", ".join(
    1464            sql
    1465            for sql in (
    1466                self.sql(expression, "offset"),
    1467                self.sql(expression, "expression"),
    1468            )
    1469            if sql
    1470        )
    1471        return f"{this}{self.seg('TOP' if top else 'LIMIT')} {args}"

def offset_sql(self, expression: sqlglot.expressions.Offset) -> str: View Source

    1473    def offset_sql(self, expression: exp.Offset) -> str:
    1474        this = self.sql(expression, "this")
    1475        return f"{this}{self.seg('OFFSET')} {self.sql(expression, 'expression')}"

def setitem_sql(self, expression: sqlglot.expressions.SetItem) -> str: View Source

    1477    def setitem_sql(self, expression: exp.SetItem) -> str:
    1478        kind = self.sql(expression, "kind")
    1479        kind = f"{kind} " if kind else ""
    1480        this = self.sql(expression, "this")
    1481        expressions = self.expressions(expression)
    1482        collate = self.sql(expression, "collate")
    1483        collate = f" COLLATE {collate}" if collate else ""
    1484        global_ = "GLOBAL " if expression.args.get("global") else ""
    1485        return f"{global_}{kind}{this}{expressions}{collate}"

def set_sql(self, expression: sqlglot.expressions.Set) -> str: View Source

    1487    def set_sql(self, expression: exp.Set) -> str:
    1488        expressions = (
    1489            f" {self.expressions(expression, flat=True)}" if expression.expressions else ""
    1490        )
    1491        tag = " TAG" if expression.args.get("tag") else ""
    1492        return f"{'UNSET' if expression.args.get('unset') else 'SET'}{tag}{expressions}"

def pragma_sql(self, expression: sqlglot.expressions.Pragma) -> str: View Source

    1494    def pragma_sql(self, expression: exp.Pragma) -> str:
    1495        return f"PRAGMA {self.sql(expression, 'this')}"

def lock_sql(self, expression: sqlglot.expressions.Lock) -> str: View Source

    1497    def lock_sql(self, expression: exp.Lock) -> str:
    1498        if not self.LOCKING_READS_SUPPORTED:
    1499            self.unsupported("Locking reads using 'FOR UPDATE/SHARE' are not supported")
    1500            return ""
    1501
    1502        lock_type = "FOR UPDATE" if expression.args["update"] else "FOR SHARE"
    1503        expressions = self.expressions(expression, flat=True)
    1504        expressions = f" OF {expressions}" if expressions else ""
    1505        wait = expression.args.get("wait")
    1506
    1507        if wait is not None:
    1508            if isinstance(wait, exp.Literal):
    1509                wait = f" WAIT {self.sql(wait)}"
    1510            else:
    1511                wait = " NOWAIT" if wait else " SKIP LOCKED"
    1512
    1513        return f"{lock_type}{expressions}{wait or ''}"

def literal_sql(self, expression: sqlglot.expressions.Literal) -> str: View Source

    1515    def literal_sql(self, expression: exp.Literal) -> str:
    1516        text = expression.this or ""
    1517        if expression.is_string:
    1518            text = f"{self.QUOTE_START}{self.escape_str(text)}{self.QUOTE_END}"
    1519        return text

def escape_str(self, text: str) -> str: View Source

    1521    def escape_str(self, text: str) -> str:
    1522        text = text.replace(self.QUOTE_END, self._escaped_quote_end)
    1523        if self.ESCAPE_LINE_BREAK:
    1524            text = text.replace("\n", "\\n")
    1525        elif self.pretty:
    1526            text = text.replace("\n", self.SENTINEL_LINE_BREAK)
    1527        return text

def loaddata_sql(self, expression: sqlglot.expressions.LoadData) -> str: View Source

    1529    def loaddata_sql(self, expression: exp.LoadData) -> str:
    1530        local = " LOCAL" if expression.args.get("local") else ""
    1531        inpath = f" INPATH {self.sql(expression, 'inpath')}"
    1532        overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
    1533        this = f" INTO TABLE {self.sql(expression, 'this')}"
    1534        partition = self.sql(expression, "partition")
    1535        partition = f" {partition}" if partition else ""
    1536        input_format = self.sql(expression, "input_format")
    1537        input_format = f" INPUTFORMAT {input_format}" if input_format else ""
    1538        serde = self.sql(expression, "serde")
    1539        serde = f" SERDE {serde}" if serde else ""
    1540        return f"LOAD DATA{local}{inpath}{overwrite}{this}{partition}{input_format}{serde}"

def null_sql(self, *_) -> str: View Source

    1542    def null_sql(self, *_) -> str:
    1543        return "NULL"

def boolean_sql(self, expression: sqlglot.expressions.Boolean) -> str: View Source

    1545    def boolean_sql(self, expression: exp.Boolean) -> str:
    1546        return "TRUE" if expression.this else "FALSE"

def order_sql(self, expression: sqlglot.expressions.Order, flat: bool = False) -> str: View Source

    1548    def order_sql(self, expression: exp.Order, flat: bool = False) -> str:
    1549        this = self.sql(expression, "this")
    1550        this = f"{this} " if this else this
    1551        return self.op_expressions(f"{this}ORDER BY", expression, flat=this or flat)  # type: ignore

def cluster_sql(self, expression: sqlglot.expressions.Cluster) -> str: View Source

    1553    def cluster_sql(self, expression: exp.Cluster) -> str:
    1554        return self.op_expressions("CLUSTER BY", expression)

def distribute_sql(self, expression: sqlglot.expressions.Distribute) -> str: View Source

    1556    def distribute_sql(self, expression: exp.Distribute) -> str:
    1557        return self.op_expressions("DISTRIBUTE BY", expression)

def sort_sql(self, expression: sqlglot.expressions.Sort) -> str: View Source

    1559    def sort_sql(self, expression: exp.Sort) -> str:
    1560        return self.op_expressions("SORT BY", expression)

def ordered_sql(self, expression: sqlglot.expressions.Ordered) -> str: View Source

    1562    def ordered_sql(self, expression: exp.Ordered) -> str:
    1563        desc = expression.args.get("desc")
    1564        asc = not desc
    1565
    1566        nulls_first = expression.args.get("nulls_first")
    1567        nulls_last = not nulls_first
    1568        nulls_are_large = self.NULL_ORDERING == "nulls_are_large"
    1569        nulls_are_small = self.NULL_ORDERING == "nulls_are_small"
    1570        nulls_are_last = self.NULL_ORDERING == "nulls_are_last"
    1571
    1572        sort_order = " DESC" if desc else ""
    1573        nulls_sort_change = ""
    1574        if nulls_first and (
    1575            (asc and nulls_are_large) or (desc and nulls_are_small) or nulls_are_last
    1576        ):
    1577            nulls_sort_change = " NULLS FIRST"
    1578        elif (
    1579            nulls_last
    1580            and ((asc and nulls_are_small) or (desc and nulls_are_large))
    1581            and not nulls_are_last
    1582        ):
    1583            nulls_sort_change = " NULLS LAST"
    1584
    1585        if nulls_sort_change and not self.NULL_ORDERING_SUPPORTED:
    1586            self.unsupported(
    1587                "Sorting in an ORDER BY on NULLS FIRST/NULLS LAST is not supported by this dialect"
    1588            )
    1589            nulls_sort_change = ""
    1590
    1591        return f"{self.sql(expression, 'this')}{sort_order}{nulls_sort_change}"

def matchrecognize_sql(self, expression: sqlglot.expressions.MatchRecognize) -> str: View Source

    1593    def matchrecognize_sql(self, expression: exp.MatchRecognize) -> str:
    1594        partition = self.partition_by_sql(expression)
    1595        order = self.sql(expression, "order")
    1596        measures = self.expressions(expression, key="measures")
    1597        measures = self.seg(f"MEASURES{self.seg(measures)}") if measures else ""
    1598        rows = self.sql(expression, "rows")
    1599        rows = self.seg(rows) if rows else ""
    1600        after = self.sql(expression, "after")
    1601        after = self.seg(after) if after else ""
    1602        pattern = self.sql(expression, "pattern")
    1603        pattern = self.seg(f"PATTERN ({pattern})") if pattern else ""
    1604        definition_sqls = [
    1605            f"{self.sql(definition, 'alias')} AS {self.sql(definition, 'this')}"
    1606            for definition in expression.args.get("define", [])
    1607        ]
    1608        definitions = self.expressions(sqls=definition_sqls)
    1609        define = self.seg(f"DEFINE{self.seg(definitions)}") if definitions else ""
    1610        body = "".join(
    1611            (
    1612                partition,
    1613                order,
    1614                measures,
    1615                rows,
    1616                after,
    1617                pattern,
    1618                define,
    1619            )
    1620        )
    1621        alias = self.sql(expression, "alias")
    1622        alias = f" {alias}" if alias else ""
    1623        return f"{self.seg('MATCH_RECOGNIZE')} {self.wrap(body)}{alias}"

def query_modifiers(self, expression: sqlglot.expressions.Expression, *sqls: str) -> str: View Source

    1625    def query_modifiers(self, expression: exp.Expression, *sqls: str) -> str:
    1626        limit: t.Optional[exp.Fetch | exp.Limit] = expression.args.get("limit")
    1627
    1628        # If the limit is generated as TOP, we need to ensure it's not generated twice
    1629        with_offset_limit_modifiers = not isinstance(limit, exp.Limit) or not self.LIMIT_IS_TOP
    1630
    1631        if self.LIMIT_FETCH == "LIMIT" and isinstance(limit, exp.Fetch):
    1632            limit = exp.Limit(expression=exp.maybe_copy(limit.args.get("count")))
    1633        elif self.LIMIT_FETCH == "FETCH" and isinstance(limit, exp.Limit):
    1634            limit = exp.Fetch(direction="FIRST", count=exp.maybe_copy(limit.expression))
    1635
    1636        fetch = isinstance(limit, exp.Fetch)
    1637
    1638        offset_limit_modifiers = (
    1639            self.offset_limit_modifiers(expression, fetch, limit)
    1640            if with_offset_limit_modifiers
    1641            else []
    1642        )
    1643
    1644        return csv(
    1645            *sqls,
    1646            *[self.sql(join) for join in expression.args.get("joins") or []],
    1647            self.sql(expression, "match"),
    1648            *[self.sql(lateral) for lateral in expression.args.get("laterals") or []],
    1649            self.sql(expression, "where"),
    1650            self.sql(expression, "group"),
    1651            self.sql(expression, "having"),
    1652            *self.after_having_modifiers(expression),
    1653            self.sql(expression, "order"),
    1654            *offset_limit_modifiers,
    1655            *self.after_limit_modifiers(expression),
    1656            sep="",
    1657        )

def offset_limit_modifiers( self, expression: sqlglot.expressions.Expression, fetch: bool, limit: Union[sqlglot.expressions.Fetch, sqlglot.expressions.Limit, NoneType]) -> List[str]: View Source

    1659    def offset_limit_modifiers(
    1660        self, expression: exp.Expression, fetch: bool, limit: t.Optional[exp.Fetch | exp.Limit]
    1661    ) -> t.List[str]:
    1662        return [
    1663            self.sql(expression, "offset") if fetch else self.sql(limit),
    1664            self.sql(limit) if fetch else self.sql(expression, "offset"),
    1665        ]

def after_having_modifiers(self, expression: sqlglot.expressions.Expression) -> List[str]: View Source

    1667    def after_having_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1668        return [
    1669            self.sql(expression, "qualify"),
    1670            self.seg("WINDOW ") + self.expressions(expression, key="windows", flat=True)
    1671            if expression.args.get("windows")
    1672            else "",
    1673            self.sql(expression, "distribute"),
    1674            self.sql(expression, "sort"),
    1675            self.sql(expression, "cluster"),
    1676        ]

def after_limit_modifiers(self, expression: sqlglot.expressions.Expression) -> List[str]: View Source

    1678    def after_limit_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1679        locks = self.expressions(expression, key="locks", sep=" ")
    1680        locks = f" {locks}" if locks else ""
    1681        return [locks, self.sql(expression, "sample")]

def select_sql(self, expression: sqlglot.expressions.Select) -> str: View Source

    1683    def select_sql(self, expression: exp.Select) -> str:
    1684        hint = self.sql(expression, "hint")
    1685        distinct = self.sql(expression, "distinct")
    1686        distinct = f" {distinct}" if distinct else ""
    1687        kind = self.sql(expression, "kind").upper()
    1688        limit = expression.args.get("limit")
    1689        top = (
    1690            self.limit_sql(limit, top=True)
    1691            if isinstance(limit, exp.Limit) and self.LIMIT_IS_TOP
    1692            else ""
    1693        )
    1694
    1695        expressions = self.expressions(expression)
    1696
    1697        if kind:
    1698            if kind in self.SELECT_KINDS:
    1699                kind = f" AS {kind}"
    1700            else:
    1701                if kind == "STRUCT":
    1702                    expressions = self.expressions(
    1703                        sqls=[
    1704                            self.sql(
    1705                                exp.Struct(
    1706                                    expressions=[
    1707                                        exp.column(e.output_name).eq(
    1708                                            e.this if isinstance(e, exp.Alias) else e
    1709                                        )
    1710                                        for e in expression.expressions
    1711                                    ]
    1712                                )
    1713                            )
    1714                        ]
    1715                    )
    1716                kind = ""
    1717
    1718        expressions = f"{self.sep()}{expressions}" if expressions else expressions
    1719        sql = self.query_modifiers(
    1720            expression,
    1721            f"SELECT{top}{hint}{distinct}{kind}{expressions}",
    1722            self.sql(expression, "into", comment=False),
    1723            self.sql(expression, "from", comment=False),
    1724        )
    1725        return self.prepend_ctes(expression, sql)

def schema_sql(self, expression: sqlglot.expressions.Schema) -> str: View Source

    1727    def schema_sql(self, expression: exp.Schema) -> str:
    1728        this = self.sql(expression, "this")
    1729        this = f"{this} " if this else ""
    1730        sql = self.schema_columns_sql(expression)
    1731        return f"{this}{sql}"

def schema_columns_sql(self, expression: sqlglot.expressions.Schema) -> str: View Source

    1733    def schema_columns_sql(self, expression: exp.Schema) -> str:
    1734        return f"({self.sep('')}{self.expressions(expression)}{self.seg(')', sep='')}"

def star_sql(self, expression: sqlglot.expressions.Star) -> str: View Source

    1736    def star_sql(self, expression: exp.Star) -> str:
    1737        except_ = self.expressions(expression, key="except", flat=True)
    1738        except_ = f"{self.seg(self.STAR_MAPPING['except'])} ({except_})" if except_ else ""
    1739        replace = self.expressions(expression, key="replace", flat=True)
    1740        replace = f"{self.seg(self.STAR_MAPPING['replace'])} ({replace})" if replace else ""
    1741        return f"*{except_}{replace}"

def parameter_sql(self, expression: sqlglot.expressions.Parameter) -> str: View Source

    1743    def parameter_sql(self, expression: exp.Parameter) -> str:
    1744        this = self.sql(expression, "this")
    1745        this = f"{{{this}}}" if expression.args.get("wrapped") else f"{this}"
    1746        return f"{self.PARAMETER_TOKEN}{this}"

def sessionparameter_sql(self, expression: sqlglot.expressions.SessionParameter) -> str: View Source

    1748    def sessionparameter_sql(self, expression: exp.SessionParameter) -> str:
    1749        this = self.sql(expression, "this")
    1750        kind = expression.text("kind")
    1751        if kind:
    1752            kind = f"{kind}."
    1753        return f"@@{kind}{this}"

def placeholder_sql(self, expression: sqlglot.expressions.Placeholder) -> str: View Source

    1755    def placeholder_sql(self, expression: exp.Placeholder) -> str:
    1756        return f":{expression.name}" if expression.name else "?"

def subquery_sql(self, expression: sqlglot.expressions.Subquery, sep: str = ' AS ') -> str: View Source

    1758    def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str:
    1759        alias = self.sql(expression, "alias")
    1760        alias = f"{sep}{alias}" if alias else ""
    1761
    1762        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1763        pivots = f" {pivots}" if pivots else ""
    1764
    1765        sql = self.query_modifiers(expression, self.wrap(expression), alias, pivots)
    1766        return self.prepend_ctes(expression, sql)

def qualify_sql(self, expression: sqlglot.expressions.Qualify) -> str: View Source

    1768    def qualify_sql(self, expression: exp.Qualify) -> str:
    1769        this = self.indent(self.sql(expression, "this"))
    1770        return f"{self.seg('QUALIFY')}{self.sep()}{this}"

def union_sql(self, expression: sqlglot.expressions.Union) -> str: View Source

    1772    def union_sql(self, expression: exp.Union) -> str:
    1773        return self.prepend_ctes(
    1774            expression,
    1775            self.set_operation(expression, self.union_op(expression)),
    1776        )

def union_op(self, expression: sqlglot.expressions.Union) -> str: View Source

    1778    def union_op(self, expression: exp.Union) -> str:
    1779        kind = " DISTINCT" if self.EXPLICIT_UNION else ""
    1780        kind = kind if expression.args.get("distinct") else " ALL"
    1781        return f"UNION{kind}"

def unnest_sql(self, expression: sqlglot.expressions.Unnest) -> str: View Source

    1783    def unnest_sql(self, expression: exp.Unnest) -> str:
    1784        args = self.expressions(expression, flat=True)
    1785        alias = expression.args.get("alias")
    1786        if alias and self.UNNEST_COLUMN_ONLY:
    1787            columns = alias.columns
    1788            alias = self.sql(columns[0]) if columns else ""
    1789        else:
    1790            alias = self.sql(expression, "alias")
    1791        alias = f" AS {alias}" if alias else alias
    1792        ordinality = " WITH ORDINALITY" if expression.args.get("ordinality") else ""
    1793        offset = expression.args.get("offset")
    1794        offset = f" WITH OFFSET AS {self.sql(offset)}" if offset else ""
    1795        return f"UNNEST({args}){ordinality}{alias}{offset}"

def where_sql(self, expression: sqlglot.expressions.Where) -> str: View Source

    1797    def where_sql(self, expression: exp.Where) -> str:
    1798        this = self.indent(self.sql(expression, "this"))
    1799        return f"{self.seg('WHERE')}{self.sep()}{this}"

def window_sql(self, expression: sqlglot.expressions.Window) -> str: View Source

    1801    def window_sql(self, expression: exp.Window) -> str:
    1802        this = self.sql(expression, "this")
    1803        partition = self.partition_by_sql(expression)
    1804        order = expression.args.get("order")
    1805        order = self.order_sql(order, flat=True) if order else ""
    1806        spec = self.sql(expression, "spec")
    1807        alias = self.sql(expression, "alias")
    1808        over = self.sql(expression, "over") or "OVER"
    1809
    1810        this = f"{this} {'AS' if expression.arg_key == 'windows' else over}"
    1811
    1812        first = expression.args.get("first")
    1813        if first is None:
    1814            first = ""
    1815        else:
    1816            first = "FIRST" if first else "LAST"
    1817
    1818        if not partition and not order and not spec and alias:
    1819            return f"{this} {alias}"
    1820
    1821        args = " ".join(arg for arg in (alias, first, partition, order, spec) if arg)
    1822        return f"{this} ({args})"

def partition_by_sql( self, expression: sqlglot.expressions.Window | sqlglot.expressions.MatchRecognize) -> str: View Source

    1824    def partition_by_sql(self, expression: exp.Window | exp.MatchRecognize) -> str:
    1825        partition = self.expressions(expression, key="partition_by", flat=True)
    1826        return f"PARTITION BY {partition}" if partition else ""

def windowspec_sql(self, expression: sqlglot.expressions.WindowSpec) -> str: View Source

    1828    def windowspec_sql(self, expression: exp.WindowSpec) -> str:
    1829        kind = self.sql(expression, "kind")
    1830        start = csv(self.sql(expression, "start"), self.sql(expression, "start_side"), sep=" ")
    1831        end = (
    1832            csv(self.sql(expression, "end"), self.sql(expression, "end_side"), sep=" ")
    1833            or "CURRENT ROW"
    1834        )
    1835        return f"{kind} BETWEEN {start} AND {end}"

def withingroup_sql(self, expression: sqlglot.expressions.WithinGroup) -> str: View Source

    1837    def withingroup_sql(self, expression: exp.WithinGroup) -> str:
    1838        this = self.sql(expression, "this")
    1839        expression_sql = self.sql(expression, "expression")[1:]  # order has a leading space
    1840        return f"{this} WITHIN GROUP ({expression_sql})"

def between_sql(self, expression: sqlglot.expressions.Between) -> str: View Source

    1842    def between_sql(self, expression: exp.Between) -> str:
    1843        this = self.sql(expression, "this")
    1844        low = self.sql(expression, "low")
    1845        high = self.sql(expression, "high")
    1846        return f"{this} BETWEEN {low} AND {high}"

def bracket_sql(self, expression: sqlglot.expressions.Bracket) -> str: View Source

    1848    def bracket_sql(self, expression: exp.Bracket) -> str:
    1849        expressions = apply_index_offset(expression.this, expression.expressions, self.INDEX_OFFSET)
    1850        expressions_sql = ", ".join(self.sql(e) for e in expressions)
    1851
    1852        return f"{self.sql(expression, 'this')}[{expressions_sql}]"

def safebracket_sql(self, expression: sqlglot.expressions.SafeBracket) -> str: View Source

    1854    def safebracket_sql(self, expression: exp.SafeBracket) -> str:
    1855        return self.bracket_sql(expression)

def all_sql(self, expression: sqlglot.expressions.All) -> str: View Source

    1857    def all_sql(self, expression: exp.All) -> str:
    1858        return f"ALL {self.wrap(expression)}"

def any_sql(self, expression: sqlglot.expressions.Any) -> str: View Source

    1860    def any_sql(self, expression: exp.Any) -> str:
    1861        this = self.sql(expression, "this")
    1862        if isinstance(expression.this, exp.Subqueryable):
    1863            this = self.wrap(this)
    1864        return f"ANY {this}"

def exists_sql(self, expression: sqlglot.expressions.Exists) -> str: View Source

    1866    def exists_sql(self, expression: exp.Exists) -> str:
    1867        return f"EXISTS{self.wrap(expression)}"

def case_sql(self, expression: sqlglot.expressions.Case) -> str: View Source

    1869    def case_sql(self, expression: exp.Case) -> str:
    1870        this = self.sql(expression, "this")
    1871        statements = [f"CASE {this}" if this else "CASE"]
    1872
    1873        for e in expression.args["ifs"]:
    1874            statements.append(f"WHEN {self.sql(e, 'this')}")
    1875            statements.append(f"THEN {self.sql(e, 'true')}")
    1876
    1877        default = self.sql(expression, "default")
    1878
    1879        if default:
    1880            statements.append(f"ELSE {default}")
    1881
    1882        statements.append("END")
    1883
    1884        if self.pretty and self.text_width(statements) > self.max_text_width:
    1885            return self.indent("\n".join(statements), skip_first=True, skip_last=True)
    1886
    1887        return " ".join(statements)

def constraint_sql(self, expression: sqlglot.expressions.Constraint) -> str: View Source

    1889    def constraint_sql(self, expression: exp.Constraint) -> str:
    1890        this = self.sql(expression, "this")
    1891        expressions = self.expressions(expression, flat=True)
    1892        return f"CONSTRAINT {this} {expressions}"

def nextvaluefor_sql(self, expression: sqlglot.expressions.NextValueFor) -> str: View Source

    1894    def nextvaluefor_sql(self, expression: exp.NextValueFor) -> str:
    1895        order = expression.args.get("order")
    1896        order = f" OVER ({self.order_sql(order, flat=True)})" if order else ""
    1897        return f"NEXT VALUE FOR {self.sql(expression, 'this')}{order}"

def extract_sql(self, expression: sqlglot.expressions.Extract) -> str: View Source

    1899    def extract_sql(self, expression: exp.Extract) -> str:
    1900        this = self.sql(expression, "this") if self.EXTRACT_ALLOWS_QUOTES else expression.this.name
    1901        expression_sql = self.sql(expression, "expression")
    1902        return f"EXTRACT({this} FROM {expression_sql})"

def trim_sql(self, expression: sqlglot.expressions.Trim) -> str: View Source

    1904    def trim_sql(self, expression: exp.Trim) -> str:
    1905        trim_type = self.sql(expression, "position")
    1906
    1907        if trim_type == "LEADING":
    1908            return self.func("LTRIM", expression.this)
    1909        elif trim_type == "TRAILING":
    1910            return self.func("RTRIM", expression.this)
    1911        else:
    1912            return self.func("TRIM", expression.this, expression.expression)

def safeconcat_sql(self, expression: sqlglot.expressions.SafeConcat) -> str: View Source

    1914    def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    1915        expressions = expression.expressions
    1916        if self.STRICT_STRING_CONCAT:
    1917            expressions = (exp.cast(e, "text") for e in expressions)
    1918        return self.func("CONCAT", *expressions)

def check_sql(self, expression: sqlglot.expressions.Check) -> str: View Source

    1920    def check_sql(self, expression: exp.Check) -> str:
    1921        this = self.sql(expression, key="this")
    1922        return f"CHECK ({this})"

def foreignkey_sql(self, expression: sqlglot.expressions.ForeignKey) -> str: View Source

    1924    def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
    1925        expressions = self.expressions(expression, flat=True)
    1926        reference = self.sql(expression, "reference")
    1927        reference = f" {reference}" if reference else ""
    1928        delete = self.sql(expression, "delete")
    1929        delete = f" ON DELETE {delete}" if delete else ""
    1930        update = self.sql(expression, "update")
    1931        update = f" ON UPDATE {update}" if update else ""
    1932        return f"FOREIGN KEY ({expressions}){reference}{delete}{update}"

def primarykey_sql(self, expression: sqlglot.expressions.ForeignKey) -> str: View Source

    1934    def primarykey_sql(self, expression: exp.ForeignKey) -> str:
    1935        expressions = self.expressions(expression, flat=True)
    1936        options = self.expressions(expression, key="options", flat=True, sep=" ")
    1937        options = f" {options}" if options else ""
    1938        return f"PRIMARY KEY ({expressions}){options}"

def if_sql(self, expression: sqlglot.expressions.If) -> str: View Source

    1940    def if_sql(self, expression: exp.If) -> str:
    1941        expression = expression.copy()
    1942        return self.case_sql(exp.Case(ifs=[expression], default=expression.args.get("false")))

def matchagainst_sql(self, expression: sqlglot.expressions.MatchAgainst) -> str: View Source

    1944    def matchagainst_sql(self, expression: exp.MatchAgainst) -> str:
    1945        modifier = expression.args.get("modifier")
    1946        modifier = f" {modifier}" if modifier else ""
    1947        return f"{self.func('MATCH', *expression.expressions)} AGAINST({self.sql(expression, 'this')}{modifier})"

def jsonkeyvalue_sql(self, expression: sqlglot.expressions.JSONKeyValue) -> str: View Source

    1949    def jsonkeyvalue_sql(self, expression: exp.JSONKeyValue) -> str:
    1950        return f"{self.sql(expression, 'this')}: {self.sql(expression, 'expression')}"

def jsonobject_sql(self, expression: sqlglot.expressions.JSONObject) -> str: View Source

    1952    def jsonobject_sql(self, expression: exp.JSONObject) -> str:
    1953        null_handling = expression.args.get("null_handling")
    1954        null_handling = f" {null_handling}" if null_handling else ""
    1955        unique_keys = expression.args.get("unique_keys")
    1956        if unique_keys is not None:
    1957            unique_keys = f" {'WITH' if unique_keys else 'WITHOUT'} UNIQUE KEYS"
    1958        else:
    1959            unique_keys = ""
    1960        return_type = self.sql(expression, "return_type")
    1961        return_type = f" RETURNING {return_type}" if return_type else ""
    1962        format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
    1963        encoding = self.sql(expression, "encoding")
    1964        encoding = f" ENCODING {encoding}" if encoding else ""
    1965        return self.func(
    1966            "JSON_OBJECT",
    1967            *expression.expressions,
    1968            suffix=f"{null_handling}{unique_keys}{return_type}{format_json}{encoding})",
    1969        )

def openjsoncolumndef_sql(self, expression: sqlglot.expressions.OpenJSONColumnDef) -> str: View Source

    1971    def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
    1972        this = self.sql(expression, "this")
    1973        kind = self.sql(expression, "kind")
    1974        path = self.sql(expression, "path")
    1975        path = f" {path}" if path else ""
    1976        as_json = " AS JSON" if expression.args.get("as_json") else ""
    1977        return f"{this} {kind}{path}{as_json}"

def openjson_sql(self, expression: sqlglot.expressions.OpenJSON) -> str: View Source

    1979    def openjson_sql(self, expression: exp.OpenJSON) -> str:
    1980        this = self.sql(expression, "this")
    1981        path = self.sql(expression, "path")
    1982        path = f", {path}" if path else ""
    1983        expressions = self.expressions(expression)
    1984        with_ = (
    1985            f" WITH ({self.seg(self.indent(expressions), sep='')}{self.seg(')', sep='')}"
    1986            if expressions
    1987            else ""
    1988        )
    1989        return f"OPENJSON({this}{path}){with_}"

def in_sql(self, expression: sqlglot.expressions.In) -> str: View Source

    1991    def in_sql(self, expression: exp.In) -> str:
    1992        query = expression.args.get("query")
    1993        unnest = expression.args.get("unnest")
    1994        field = expression.args.get("field")
    1995        is_global = " GLOBAL" if expression.args.get("is_global") else ""
    1996
    1997        if query:
    1998            in_sql = self.wrap(query)
    1999        elif unnest:
    2000            in_sql = self.in_unnest_op(unnest)
    2001        elif field:
    2002            in_sql = self.sql(field)
    2003        else:
    2004            in_sql = f"({self.expressions(expression, flat=True)})"
    2005
    2006        return f"{self.sql(expression, 'this')}{is_global} IN {in_sql}"

def in_unnest_op(self, unnest: sqlglot.expressions.Unnest) -> str: View Source

    2008    def in_unnest_op(self, unnest: exp.Unnest) -> str:
    2009        return f"(SELECT {self.sql(unnest)})"

def interval_sql(self, expression: sqlglot.expressions.Interval) -> str: View Source

    2011    def interval_sql(self, expression: exp.Interval) -> str:
    2012        unit = self.sql(expression, "unit")
    2013        if not self.INTERVAL_ALLOWS_PLURAL_FORM:
    2014            unit = self.TIME_PART_SINGULARS.get(unit.lower(), unit)
    2015        unit = f" {unit}" if unit else ""
    2016
    2017        if self.SINGLE_STRING_INTERVAL:
    2018            this = expression.this.name if expression.this else ""
    2019            return f"INTERVAL '{this}{unit}'" if this else f"INTERVAL{unit}"
    2020
    2021        this = self.sql(expression, "this")
    2022        if this:
    2023            unwrapped = isinstance(expression.this, self.UNWRAPPED_INTERVAL_VALUES)
    2024            this = f" {this}" if unwrapped else f" ({this})"
    2025
    2026        return f"INTERVAL{this}{unit}"

def return_sql(self, expression: sqlglot.expressions.Return) -> str: View Source

    2028    def return_sql(self, expression: exp.Return) -> str:
    2029        return f"RETURN {self.sql(expression, 'this')}"

def reference_sql(self, expression: sqlglot.expressions.Reference) -> str: View Source

    2031    def reference_sql(self, expression: exp.Reference) -> str:
    2032        this = self.sql(expression, "this")
    2033        expressions = self.expressions(expression, flat=True)
    2034        expressions = f"({expressions})" if expressions else ""
    2035        options = self.expressions(expression, key="options", flat=True, sep=" ")
    2036        options = f" {options}" if options else ""
    2037        return f"REFERENCES {this}{expressions}{options}"

def anonymous_sql(self, expression: sqlglot.expressions.Anonymous) -> str: View Source

    2039    def anonymous_sql(self, expression: exp.Anonymous) -> str:
    2040        return self.func(expression.name, *expression.expressions)

def paren_sql(self, expression: sqlglot.expressions.Paren) -> str: View Source

    2042    def paren_sql(self, expression: exp.Paren) -> str:
    2043        if isinstance(expression.unnest(), exp.Select):
    2044            sql = self.wrap(expression)
    2045        else:
    2046            sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
    2047            sql = f"({sql}{self.seg(')', sep='')}"
    2048
    2049        return self.prepend_ctes(expression, sql)

def neg_sql(self, expression: sqlglot.expressions.Neg) -> str: View Source

    2051    def neg_sql(self, expression: exp.Neg) -> str:
    2052        # This makes sure we don't convert "- - 5" to "--5", which is a comment
    2053        this_sql = self.sql(expression, "this")
    2054        sep = " " if this_sql[0] == "-" else ""
    2055        return f"-{sep}{this_sql}"

def not_sql(self, expression: sqlglot.expressions.Not) -> str: View Source

    2057    def not_sql(self, expression: exp.Not) -> str:
    2058        return f"NOT {self.sql(expression, 'this')}"

def alias_sql(self, expression: sqlglot.expressions.Alias) -> str: View Source

    2060    def alias_sql(self, expression: exp.Alias) -> str:
    2061        alias = self.sql(expression, "alias")
    2062        alias = f" AS {alias}" if alias else ""
    2063        return f"{self.sql(expression, 'this')}{alias}"

def aliases_sql(self, expression: sqlglot.expressions.Aliases) -> str: View Source

    2065    def aliases_sql(self, expression: exp.Aliases) -> str:
    2066        return f"{self.sql(expression, 'this')} AS ({self.expressions(expression, flat=True)})"

def attimezone_sql(self, expression: sqlglot.expressions.AtTimeZone) -> str: View Source

    2068    def attimezone_sql(self, expression: exp.AtTimeZone) -> str:
    2069        this = self.sql(expression, "this")
    2070        zone = self.sql(expression, "zone")
    2071        return f"{this} AT TIME ZONE {zone}"

def add_sql(self, expression: sqlglot.expressions.Add) -> str: View Source

    2073    def add_sql(self, expression: exp.Add) -> str:
    2074        return self.binary(expression, "+")

def and_sql(self, expression: sqlglot.expressions.And) -> str: View Source

    2076    def and_sql(self, expression: exp.And) -> str:
    2077        return self.connector_sql(expression, "AND")

def xor_sql(self, expression: sqlglot.expressions.Xor) -> str: View Source

    2079    def xor_sql(self, expression: exp.Xor) -> str:
    2080        return self.connector_sql(expression, "XOR")

def connector_sql(self, expression: sqlglot.expressions.Connector, op: str) -> str: View Source

    2082    def connector_sql(self, expression: exp.Connector, op: str) -> str:
    2083        if not self.pretty:
    2084            return self.binary(expression, op)
    2085
    2086        sqls = tuple(
    2087            self.maybe_comment(self.sql(e), e, e.parent.comments or []) if i != 1 else self.sql(e)
    2088            for i, e in enumerate(expression.flatten(unnest=False))
    2089        )
    2090
    2091        sep = "\n" if self.text_width(sqls) > self.max_text_width else " "
    2092        return f"{sep}{op} ".join(sqls)

def bitwiseand_sql(self, expression: sqlglot.expressions.BitwiseAnd) -> str: View Source

    2094    def bitwiseand_sql(self, expression: exp.BitwiseAnd) -> str:
    2095        return self.binary(expression, "&")

def bitwiseleftshift_sql(self, expression: sqlglot.expressions.BitwiseLeftShift) -> str: View Source

    2097    def bitwiseleftshift_sql(self, expression: exp.BitwiseLeftShift) -> str:
    2098        return self.binary(expression, "<<")

def bitwisenot_sql(self, expression: sqlglot.expressions.BitwiseNot) -> str: View Source

    2100    def bitwisenot_sql(self, expression: exp.BitwiseNot) -> str:
    2101        return f"~{self.sql(expression, 'this')}"

def bitwiseor_sql(self, expression: sqlglot.expressions.BitwiseOr) -> str: View Source

    2103    def bitwiseor_sql(self, expression: exp.BitwiseOr) -> str:
    2104        return self.binary(expression, "|")

def bitwiserightshift_sql(self, expression: sqlglot.expressions.BitwiseRightShift) -> str: View Source

    2106    def bitwiserightshift_sql(self, expression: exp.BitwiseRightShift) -> str:
    2107        return self.binary(expression, ">>")

def bitwisexor_sql(self, expression: sqlglot.expressions.BitwiseXor) -> str: View Source

    2109    def bitwisexor_sql(self, expression: exp.BitwiseXor) -> str:
    2110        return self.binary(expression, "^")

def cast_sql( self, expression: sqlglot.expressions.Cast, safe_prefix: Optional[str] = None) -> str: View Source

    2112    def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    2113        format_sql = self.sql(expression, "format")
    2114        format_sql = f" FORMAT {format_sql}" if format_sql else ""
    2115        return f"{safe_prefix or ''}CAST({self.sql(expression, 'this')} AS {self.sql(expression, 'to')}{format_sql})"

def currentdate_sql(self, expression: sqlglot.expressions.CurrentDate) -> str: View Source

    2117    def currentdate_sql(self, expression: exp.CurrentDate) -> str:
    2118        zone = self.sql(expression, "this")
    2119        return f"CURRENT_DATE({zone})" if zone else "CURRENT_DATE"

def collate_sql(self, expression: sqlglot.expressions.Collate) -> str: View Source

    2121    def collate_sql(self, expression: exp.Collate) -> str:
    2122        return self.binary(expression, "COLLATE")

def command_sql(self, expression: sqlglot.expressions.Command) -> str: View Source

    2124    def command_sql(self, expression: exp.Command) -> str:
    2125        return f"{self.sql(expression, 'this').upper()} {expression.text('expression').strip()}"

def comment_sql(self, expression: sqlglot.expressions.Comment) -> str: View Source

    2127    def comment_sql(self, expression: exp.Comment) -> str:
    2128        this = self.sql(expression, "this")
    2129        kind = expression.args["kind"]
    2130        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    2131        expression_sql = self.sql(expression, "expression")
    2132        return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"

def mergetreettlaction_sql(self, expression: sqlglot.expressions.MergeTreeTTLAction) -> str: View Source

    2134    def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
    2135        this = self.sql(expression, "this")
    2136        delete = " DELETE" if expression.args.get("delete") else ""
    2137        recompress = self.sql(expression, "recompress")
    2138        recompress = f" RECOMPRESS {recompress}" if recompress else ""
    2139        to_disk = self.sql(expression, "to_disk")
    2140        to_disk = f" TO DISK {to_disk}" if to_disk else ""
    2141        to_volume = self.sql(expression, "to_volume")
    2142        to_volume = f" TO VOLUME {to_volume}" if to_volume else ""
    2143        return f"{this}{delete}{recompress}{to_disk}{to_volume}"

def mergetreettl_sql(self, expression: sqlglot.expressions.MergeTreeTTL) -> str: View Source

    2145    def mergetreettl_sql(self, expression: exp.MergeTreeTTL) -> str:
    2146        where = self.sql(expression, "where")
    2147        group = self.sql(expression, "group")
    2148        aggregates = self.expressions(expression, key="aggregates")
    2149        aggregates = self.seg("SET") + self.seg(aggregates) if aggregates else ""
    2150
    2151        if not (where or group or aggregates) and len(expression.expressions) == 1:
    2152            return f"TTL {self.expressions(expression, flat=True)}"
    2153
    2154        return f"TTL{self.seg(self.expressions(expression))}{where}{group}{aggregates}"

def transaction_sql(self, expression: sqlglot.expressions.Transaction) -> str: View Source

    2156    def transaction_sql(self, expression: exp.Transaction) -> str:
    2157        return "BEGIN"

def commit_sql(self, expression: sqlglot.expressions.Commit) -> str: View Source

    2159    def commit_sql(self, expression: exp.Commit) -> str:
    2160        chain = expression.args.get("chain")
    2161        if chain is not None:
    2162            chain = " AND CHAIN" if chain else " AND NO CHAIN"
    2163
    2164        return f"COMMIT{chain or ''}"

def rollback_sql(self, expression: sqlglot.expressions.Rollback) -> str: View Source

    2166    def rollback_sql(self, expression: exp.Rollback) -> str:
    2167        savepoint = expression.args.get("savepoint")
    2168        savepoint = f" TO {savepoint}" if savepoint else ""
    2169        return f"ROLLBACK{savepoint}"

def altercolumn_sql(self, expression: sqlglot.expressions.AlterColumn) -> str: View Source

    2171    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
    2172        this = self.sql(expression, "this")
    2173
    2174        dtype = self.sql(expression, "dtype")
    2175        if dtype:
    2176            collate = self.sql(expression, "collate")
    2177            collate = f" COLLATE {collate}" if collate else ""
    2178            using = self.sql(expression, "using")
    2179            using = f" USING {using}" if using else ""
    2180            return f"ALTER COLUMN {this} TYPE {dtype}{collate}{using}"
    2181
    2182        default = self.sql(expression, "default")
    2183        if default:
    2184            return f"ALTER COLUMN {this} SET DEFAULT {default}"
    2185
    2186        if not expression.args.get("drop"):
    2187            self.unsupported("Unsupported ALTER COLUMN syntax")
    2188
    2189        return f"ALTER COLUMN {this} DROP DEFAULT"

def renametable_sql(self, expression: sqlglot.expressions.RenameTable) -> str: View Source

    2191    def renametable_sql(self, expression: exp.RenameTable) -> str:
    2192        if not self.RENAME_TABLE_WITH_DB:
    2193            # Remove db from tables
    2194            expression = expression.transform(
    2195                lambda n: exp.table_(n.this) if isinstance(n, exp.Table) else n
    2196            )
    2197        this = self.sql(expression, "this")
    2198        return f"RENAME TO {this}"

def altertable_sql(self, expression: sqlglot.expressions.AlterTable) -> str: View Source

    2200    def altertable_sql(self, expression: exp.AlterTable) -> str:
    2201        actions = expression.args["actions"]
    2202
    2203        if isinstance(actions[0], exp.ColumnDef):
    2204            actions = self.expressions(expression, key="actions", prefix="ADD COLUMN ")
    2205        elif isinstance(actions[0], exp.Schema):
    2206            actions = self.expressions(expression, key="actions", prefix="ADD COLUMNS ")
    2207        elif isinstance(actions[0], exp.Delete):
    2208            actions = self.expressions(expression, key="actions", flat=True)
    2209        else:
    2210            actions = self.expressions(expression, key="actions")
    2211
    2212        exists = " IF EXISTS" if expression.args.get("exists") else ""
    2213        return f"ALTER TABLE{exists} {self.sql(expression, 'this')} {actions}"

def droppartition_sql(self, expression: sqlglot.expressions.DropPartition) -> str: View Source

    2215    def droppartition_sql(self, expression: exp.DropPartition) -> str:
    2216        expressions = self.expressions(expression)
    2217        exists = " IF EXISTS " if expression.args.get("exists") else " "
    2218        return f"DROP{exists}{expressions}"

def addconstraint_sql(self, expression: sqlglot.expressions.AddConstraint) -> str: View Source

    2220    def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
    2221        this = self.sql(expression, "this")
    2222        expression_ = self.sql(expression, "expression")
    2223        add_constraint = f"ADD CONSTRAINT {this}" if this else "ADD"
    2224
    2225        enforced = expression.args.get("enforced")
    2226        if enforced is not None:
    2227            return f"{add_constraint} CHECK ({expression_}){' ENFORCED' if enforced else ''}"
    2228
    2229        return f"{add_constraint} {expression_}"

def distinct_sql(self, expression: sqlglot.expressions.Distinct) -> str: View Source

    2231    def distinct_sql(self, expression: exp.Distinct) -> str:
    2232        this = self.expressions(expression, flat=True)
    2233        this = f" {this}" if this else ""
    2234
    2235        on = self.sql(expression, "on")
    2236        on = f" ON {on}" if on else ""
    2237        return f"DISTINCT{this}{on}"

def ignorenulls_sql(self, expression: sqlglot.expressions.IgnoreNulls) -> str: View Source

    2239    def ignorenulls_sql(self, expression: exp.IgnoreNulls) -> str:
    2240        return f"{self.sql(expression, 'this')} IGNORE NULLS"

def respectnulls_sql(self, expression: sqlglot.expressions.RespectNulls) -> str: View Source

    2242    def respectnulls_sql(self, expression: exp.RespectNulls) -> str:
    2243        return f"{self.sql(expression, 'this')} RESPECT NULLS"

def intdiv_sql(self, expression: sqlglot.expressions.IntDiv) -> str: View Source

    2245    def intdiv_sql(self, expression: exp.IntDiv) -> str:
    2246        return self.sql(
    2247            exp.Cast(
    2248                this=exp.Div(this=expression.this.copy(), expression=expression.expression.copy()),
    2249                to=exp.DataType(this=exp.DataType.Type.INT),
    2250            )
    2251        )

def dpipe_sql(self, expression: sqlglot.expressions.DPipe) -> str: View Source

    2253    def dpipe_sql(self, expression: exp.DPipe) -> str:
    2254        return self.binary(expression, "||")

def safedpipe_sql(self, expression: sqlglot.expressions.SafeDPipe) -> str: View Source

    2256    def safedpipe_sql(self, expression: exp.SafeDPipe) -> str:
    2257        if self.STRICT_STRING_CONCAT:
    2258            return self.func("CONCAT", *(exp.cast(e, "text") for e in expression.flatten()))
    2259        return self.dpipe_sql(expression)

def div_sql(self, expression: sqlglot.expressions.Div) -> str: View Source

    2261    def div_sql(self, expression: exp.Div) -> str:
    2262        return self.binary(expression, "/")

def overlaps_sql(self, expression: sqlglot.expressions.Overlaps) -> str: View Source

    2264    def overlaps_sql(self, expression: exp.Overlaps) -> str:
    2265        return self.binary(expression, "OVERLAPS")

def distance_sql(self, expression: sqlglot.expressions.Distance) -> str: View Source

    2267    def distance_sql(self, expression: exp.Distance) -> str:
    2268        return self.binary(expression, "<->")

def dot_sql(self, expression: sqlglot.expressions.Dot) -> str: View Source

    2270    def dot_sql(self, expression: exp.Dot) -> str:
    2271        return f"{self.sql(expression, 'this')}.{self.sql(expression, 'expression')}"

def eq_sql(self, expression: sqlglot.expressions.EQ) -> str: View Source

    2273    def eq_sql(self, expression: exp.EQ) -> str:
    2274        return self.binary(expression, "=")

def escape_sql(self, expression: sqlglot.expressions.Escape) -> str: View Source

    2276    def escape_sql(self, expression: exp.Escape) -> str:
    2277        return self.binary(expression, "ESCAPE")

def glob_sql(self, expression: sqlglot.expressions.Glob) -> str: View Source

    2279    def glob_sql(self, expression: exp.Glob) -> str:
    2280        return self.binary(expression, "GLOB")

def gt_sql(self, expression: sqlglot.expressions.GT) -> str: View Source

    2282    def gt_sql(self, expression: exp.GT) -> str:
    2283        return self.binary(expression, ">")

def gte_sql(self, expression: sqlglot.expressions.GTE) -> str: View Source

    2285    def gte_sql(self, expression: exp.GTE) -> str:
    2286        return self.binary(expression, ">=")

def ilike_sql(self, expression: sqlglot.expressions.ILike) -> str: View Source

    2288    def ilike_sql(self, expression: exp.ILike) -> str:
    2289        return self.binary(expression, "ILIKE")

def ilikeany_sql(self, expression: sqlglot.expressions.ILikeAny) -> str: View Source

    2291    def ilikeany_sql(self, expression: exp.ILikeAny) -> str:
    2292        return self.binary(expression, "ILIKE ANY")

def is_sql(self, expression: sqlglot.expressions.Is) -> str: View Source

    2294    def is_sql(self, expression: exp.Is) -> str:
    2295        if not self.IS_BOOL_ALLOWED and isinstance(expression.expression, exp.Boolean):
    2296            return self.sql(
    2297                expression.this if expression.expression.this else exp.not_(expression.this)
    2298            )
    2299        return self.binary(expression, "IS")

def like_sql(self, expression: sqlglot.expressions.Like) -> str: View Source

    2301    def like_sql(self, expression: exp.Like) -> str:
    2302        return self.binary(expression, "LIKE")

def likeany_sql(self, expression: sqlglot.expressions.LikeAny) -> str: View Source

    2304    def likeany_sql(self, expression: exp.LikeAny) -> str:
    2305        return self.binary(expression, "LIKE ANY")

def similarto_sql(self, expression: sqlglot.expressions.SimilarTo) -> str: View Source

    2307    def similarto_sql(self, expression: exp.SimilarTo) -> str:
    2308        return self.binary(expression, "SIMILAR TO")

def lt_sql(self, expression: sqlglot.expressions.LT) -> str: View Source

    2310    def lt_sql(self, expression: exp.LT) -> str:
    2311        return self.binary(expression, "<")

def lte_sql(self, expression: sqlglot.expressions.LTE) -> str: View Source

    2313    def lte_sql(self, expression: exp.LTE) -> str:
    2314        return self.binary(expression, "<=")

def mod_sql(self, expression: sqlglot.expressions.Mod) -> str: View Source

    2316    def mod_sql(self, expression: exp.Mod) -> str:
    2317        return self.binary(expression, "%")

def mul_sql(self, expression: sqlglot.expressions.Mul) -> str: View Source

    2319    def mul_sql(self, expression: exp.Mul) -> str:
    2320        return self.binary(expression, "*")

def neq_sql(self, expression: sqlglot.expressions.NEQ) -> str: View Source

    2322    def neq_sql(self, expression: exp.NEQ) -> str:
    2323        return self.binary(expression, "<>")

def nullsafeeq_sql(self, expression: sqlglot.expressions.NullSafeEQ) -> str: View Source

    2325    def nullsafeeq_sql(self, expression: exp.NullSafeEQ) -> str:
    2326        return self.binary(expression, "IS NOT DISTINCT FROM")

def nullsafeneq_sql(self, expression: sqlglot.expressions.NullSafeNEQ) -> str: View Source

    2328    def nullsafeneq_sql(self, expression: exp.NullSafeNEQ) -> str:
    2329        return self.binary(expression, "IS DISTINCT FROM")

def or_sql(self, expression: sqlglot.expressions.Or) -> str: View Source

    2331    def or_sql(self, expression: exp.Or) -> str:
    2332        return self.connector_sql(expression, "OR")

def slice_sql(self, expression: sqlglot.expressions.Slice) -> str: View Source

    2334    def slice_sql(self, expression: exp.Slice) -> str:
    2335        return self.binary(expression, ":")

def sub_sql(self, expression: sqlglot.expressions.Sub) -> str: View Source

    2337    def sub_sql(self, expression: exp.Sub) -> str:
    2338        return self.binary(expression, "-")

def trycast_sql(self, expression: sqlglot.expressions.TryCast) -> str: View Source

    2340    def trycast_sql(self, expression: exp.TryCast) -> str:
    2341        return self.cast_sql(expression, safe_prefix="TRY_")

def use_sql(self, expression: sqlglot.expressions.Use) -> str: View Source

    2343    def use_sql(self, expression: exp.Use) -> str:
    2344        kind = self.sql(expression, "kind")
    2345        kind = f" {kind}" if kind else ""
    2346        this = self.sql(expression, "this")
    2347        this = f" {this}" if this else ""
    2348        return f"USE{kind}{this}"

def binary(self, expression: sqlglot.expressions.Binary, op: str) -> str: View Source

    2350    def binary(self, expression: exp.Binary, op: str) -> str:
    2351        op = self.maybe_comment(op, comments=expression.comments)
    2352        return f"{self.sql(expression, 'this')} {op} {self.sql(expression, 'expression')}"

def function_fallback_sql(self, expression: sqlglot.expressions.Func) -> str: View Source

    2354    def function_fallback_sql(self, expression: exp.Func) -> str:
    2355        args = []
    2356
    2357        for key in expression.arg_types:
    2358            arg_value = expression.args.get(key)
    2359
    2360            if isinstance(arg_value, list):
    2361                for value in arg_value:
    2362                    args.append(value)
    2363            elif arg_value is not None:
    2364                args.append(arg_value)
    2365
    2366        if self.normalize_functions:
    2367            name = expression.sql_name()
    2368        else:
    2369            name = (expression._meta and expression.meta.get("name")) or expression.sql_name()
    2370
    2371        return self.func(name, *args)

def func( self, name: str, *args: Union[str, sqlglot.expressions.Expression, NoneType], prefix: str = '(', suffix: str = ')') -> str: View Source

    2373    def func(
    2374        self,
    2375        name: str,
    2376        *args: t.Optional[exp.Expression | str],
    2377        prefix: str = "(",
    2378        suffix: str = ")",
    2379    ) -> str:
    2380        return f"{self.normalize_func(name)}{prefix}{self.format_args(*args)}{suffix}"

def format_args(self, *args: Union[str, sqlglot.expressions.Expression, NoneType]) -> str: View Source

    2382    def format_args(self, *args: t.Optional[str | exp.Expression]) -> str:
    2383        arg_sqls = tuple(self.sql(arg) for arg in args if arg is not None)
    2384        if self.pretty and self.text_width(arg_sqls) > self.max_text_width:
    2385            return self.indent("\n" + f",\n".join(arg_sqls) + "\n", skip_first=True, skip_last=True)
    2386        return ", ".join(arg_sqls)

def text_width(self, args: Iterable) -> int: View Source

    2388    def text_width(self, args: t.Iterable) -> int:
    2389        return sum(len(arg) for arg in args)

def format_time(self, expression: sqlglot.expressions.Expression) -> Optional[str]: View Source

    2391    def format_time(self, expression: exp.Expression) -> t.Optional[str]:
    2392        return format_time(
    2393            self.sql(expression, "format"), self.INVERSE_TIME_MAPPING, self.INVERSE_TIME_TRIE
    2394        )

def expressions( self, expression: Optional[sqlglot.expressions.Expression] = None, key: Optional[str] = None, sqls: Optional[List[str]] = None, flat: bool = False, indent: bool = True, skip_first: bool = False, sep: str = ', ', prefix: str = '') -> str: View Source

    2396    def expressions(
    2397        self,
    2398        expression: t.Optional[exp.Expression] = None,
    2399        key: t.Optional[str] = None,
    2400        sqls: t.Optional[t.List[str]] = None,
    2401        flat: bool = False,
    2402        indent: bool = True,
    2403        skip_first: bool = False,
    2404        sep: str = ", ",
    2405        prefix: str = "",
    2406    ) -> str:
    2407        expressions = expression.args.get(key or "expressions") if expression else sqls
    2408
    2409        if not expressions:
    2410            return ""
    2411
    2412        if flat:
    2413            return sep.join(sql for sql in (self.sql(e) for e in expressions) if sql)
    2414
    2415        num_sqls = len(expressions)
    2416
    2417        # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
    2418        pad = " " * self.pad
    2419        stripped_sep = sep.strip()
    2420
    2421        result_sqls = []
    2422        for i, e in enumerate(expressions):
    2423            sql = self.sql(e, comment=False)
    2424            if not sql:
    2425                continue
    2426
    2427            comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
    2428
    2429            if self.pretty:
    2430                if self.leading_comma:
    2431                    result_sqls.append(f"{sep if i > 0 else pad}{prefix}{sql}{comments}")
    2432                else:
    2433                    result_sqls.append(
    2434                        f"{prefix}{sql}{stripped_sep if i + 1 < num_sqls else ''}{comments}"
    2435                    )
    2436            else:
    2437                result_sqls.append(f"{prefix}{sql}{comments}{sep if i + 1 < num_sqls else ''}")
    2438
    2439        result_sql = "\n".join(result_sqls) if self.pretty else "".join(result_sqls)
    2440        return self.indent(result_sql, skip_first=skip_first) if indent else result_sql

def op_expressions( self, op: str, expression: sqlglot.expressions.Expression, flat: bool = False) -> str: View Source

    2442    def op_expressions(self, op: str, expression: exp.Expression, flat: bool = False) -> str:
    2443        flat = flat or isinstance(expression.parent, exp.Properties)
    2444        expressions_sql = self.expressions(expression, flat=flat)
    2445        if flat:
    2446            return f"{op} {expressions_sql}"
    2447        return f"{self.seg(op)}{self.sep() if expressions_sql else ''}{expressions_sql}"

def naked_property(self, expression: sqlglot.expressions.Property) -> str: View Source

    2449    def naked_property(self, expression: exp.Property) -> str:
    2450        property_name = exp.Properties.PROPERTY_TO_NAME.get(expression.__class__)
    2451        if not property_name:
    2452            self.unsupported(f"Unsupported property {expression.__class__.__name__}")
    2453        return f"{property_name} {self.sql(expression, 'this')}"

def set_operation(self, expression: sqlglot.expressions.Expression, op: str) -> str: View Source

    2455    def set_operation(self, expression: exp.Expression, op: str) -> str:
    2456        this = self.sql(expression, "this")
    2457        op = self.seg(op)
    2458        return self.query_modifiers(
    2459            expression, f"{this}{op}{self.sep()}{self.sql(expression, 'expression')}"
    2460        )

def tag_sql(self, expression: sqlglot.expressions.Tag) -> str: View Source

    2462    def tag_sql(self, expression: exp.Tag) -> str:
    2463        return f"{expression.args.get('prefix')}{self.sql(expression.this)}{expression.args.get('postfix')}"

def token_sql(self, token_type: sqlglot.tokens.TokenType) -> str: View Source

    2465    def token_sql(self, token_type: TokenType) -> str:
    2466        return self.TOKEN_MAPPING.get(token_type, token_type.name)

def userdefinedfunction_sql(self, expression: sqlglot.expressions.UserDefinedFunction) -> str: View Source

    2468    def userdefinedfunction_sql(self, expression: exp.UserDefinedFunction) -> str:
    2469        this = self.sql(expression, "this")
    2470        expressions = self.no_identify(self.expressions, expression)
    2471        expressions = (
    2472            self.wrap(expressions) if expression.args.get("wrapped") else f" {expressions}"
    2473        )
    2474        return f"{this}{expressions}"

def joinhint_sql(self, expression: sqlglot.expressions.JoinHint) -> str: View Source

    2476    def joinhint_sql(self, expression: exp.JoinHint) -> str:
    2477        this = self.sql(expression, "this")
    2478        expressions = self.expressions(expression, flat=True)
    2479        return f"{this}({expressions})"

def kwarg_sql(self, expression: sqlglot.expressions.Kwarg) -> str: View Source

    2481    def kwarg_sql(self, expression: exp.Kwarg) -> str:
    2482        return self.binary(expression, "=>")

def when_sql(self, expression: sqlglot.expressions.When) -> str: View Source

    2484    def when_sql(self, expression: exp.When) -> str:
    2485        matched = "MATCHED" if expression.args["matched"] else "NOT MATCHED"
    2486        source = " BY SOURCE" if self.MATCHED_BY_SOURCE and expression.args.get("source") else ""
    2487        condition = self.sql(expression, "condition")
    2488        condition = f" AND {condition}" if condition else ""
    2489
    2490        then_expression = expression.args.get("then")
    2491        if isinstance(then_expression, exp.Insert):
    2492            then = f"INSERT {self.sql(then_expression, 'this')}"
    2493            if "expression" in then_expression.args:
    2494                then += f" VALUES {self.sql(then_expression, 'expression')}"
    2495        elif isinstance(then_expression, exp.Update):
    2496            if isinstance(then_expression.args.get("expressions"), exp.Star):
    2497                then = f"UPDATE {self.sql(then_expression, 'expressions')}"
    2498            else:
    2499                then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
    2500        else:
    2501            then = self.sql(then_expression)
    2502        return f"WHEN {matched}{source}{condition} THEN {then}"

def merge_sql(self, expression: sqlglot.expressions.Merge) -> str: View Source

    2504    def merge_sql(self, expression: exp.Merge) -> str:
    2505        this = self.sql(expression, "this")
    2506        using = f"USING {self.sql(expression, 'using')}"
    2507        on = f"ON {self.sql(expression, 'on')}"
    2508        return f"MERGE INTO {this} {using} {on} {self.expressions(expression, sep=' ')}"

def tochar_sql(self, expression: sqlglot.expressions.ToChar) -> str: View Source

    2510    def tochar_sql(self, expression: exp.ToChar) -> str:
    2511        if expression.args.get("format"):
    2512            self.unsupported("Format argument unsupported for TO_CHAR/TO_VARCHAR function")
    2513
    2514        return self.sql(exp.cast(expression.this, "text"))

def dictproperty_sql(self, expression: sqlglot.expressions.DictProperty) -> str: View Source

    2516    def dictproperty_sql(self, expression: exp.DictProperty) -> str:
    2517        this = self.sql(expression, "this")
    2518        kind = self.sql(expression, "kind")
    2519        settings_sql = self.expressions(expression, key="settings", sep=" ")
    2520        args = f"({self.sep('')}{settings_sql}{self.seg(')', sep='')}" if settings_sql else "()"
    2521        return f"{this}({kind}{args})"

def dictrange_sql(self, expression: sqlglot.expressions.DictRange) -> str: View Source

    2523    def dictrange_sql(self, expression: exp.DictRange) -> str:
    2524        this = self.sql(expression, "this")
    2525        max = self.sql(expression, "max")
    2526        min = self.sql(expression, "min")
    2527        return f"{this}(MIN {min} MAX {max})"

def dictsubproperty_sql(self, expression: sqlglot.expressions.DictSubProperty) -> str: View Source

    2529    def dictsubproperty_sql(self, expression: exp.DictSubProperty) -> str:
    2530        return f"{self.sql(expression, 'this')} {self.sql(expression, 'value')}"

def oncluster_sql(self, expression: sqlglot.expressions.OnCluster) -> str: View Source

    2532    def oncluster_sql(self, expression: exp.OnCluster) -> str:
    2533        return ""

def clusteredbyproperty_sql(self, expression: sqlglot.expressions.ClusteredByProperty) -> str: View Source

    2535    def clusteredbyproperty_sql(self, expression: exp.ClusteredByProperty) -> str:
    2536        expressions = self.expressions(expression, key="expressions", flat=True)
    2537        sorted_by = self.expressions(expression, key="sorted_by", flat=True)
    2538        sorted_by = f" SORTED BY ({sorted_by})" if sorted_by else ""
    2539        buckets = self.sql(expression, "buckets")
    2540        return f"CLUSTERED BY ({expressions}){sorted_by} INTO {buckets} BUCKETS"

def anyvalue_sql(self, expression: sqlglot.expressions.AnyValue) -> str: View Source

    2542    def anyvalue_sql(self, expression: exp.AnyValue) -> str:
    2543        this = self.sql(expression, "this")
    2544        having = self.sql(expression, "having")
    2545
    2546        if having:
    2547            this = f"{this} HAVING {'MAX' if expression.args.get('max') else 'MIN'} {having}"
    2548
    2549        return self.func("ANY_VALUE", this)

def querytransform_sql(self, expression: sqlglot.expressions.QueryTransform) -> str: View Source

    2551    def querytransform_sql(self, expression: exp.QueryTransform) -> str:
    2552        transform = self.func("TRANSFORM", *expression.expressions)
    2553        row_format_before = self.sql(expression, "row_format_before")
    2554        row_format_before = f" {row_format_before}" if row_format_before else ""
    2555        record_writer = self.sql(expression, "record_writer")
    2556        record_writer = f" RECORDWRITER {record_writer}" if record_writer else ""
    2557        using = f" USING {self.sql(expression, 'command_script')}"
    2558        schema = self.sql(expression, "schema")
    2559        schema = f" AS {schema}" if schema else ""
    2560        row_format_after = self.sql(expression, "row_format_after")
    2561        row_format_after = f" {row_format_after}" if row_format_after else ""
    2562        record_reader = self.sql(expression, "record_reader")
    2563        record_reader = f" RECORDREADER {record_reader}" if record_reader else ""
    2564        return f"{transform}{row_format_before}{record_writer}{using}{schema}{row_format_after}{record_reader}"

def indexconstraintoption_sql(self, expression: sqlglot.expressions.IndexConstraintOption) -> str: View Source

    2566    def indexconstraintoption_sql(self, expression: exp.IndexConstraintOption) -> str:
    2567        key_block_size = self.sql(expression, "key_block_size")
    2568        if key_block_size:
    2569            return f"KEY_BLOCK_SIZE = {key_block_size}"
    2570
    2571        using = self.sql(expression, "using")
    2572        if using:
    2573            return f"USING {using}"
    2574
    2575        parser = self.sql(expression, "parser")
    2576        if parser:
    2577            return f"WITH PARSER {parser}"
    2578
    2579        comment = self.sql(expression, "comment")
    2580        if comment:
    2581            return f"COMMENT {comment}"
    2582
    2583        visible = expression.args.get("visible")
    2584        if visible is not None:
    2585            return "VISIBLE" if visible else "INVISIBLE"
    2586
    2587        engine_attr = self.sql(expression, "engine_attr")
    2588        if engine_attr:
    2589            return f"ENGINE_ATTRIBUTE = {engine_attr}"
    2590
    2591        secondary_engine_attr = self.sql(expression, "secondary_engine_attr")
    2592        if secondary_engine_attr:
    2593            return f"SECONDARY_ENGINE_ATTRIBUTE = {secondary_engine_attr}"
    2594
    2595        self.unsupported("Unsupported index constraint option.")
    2596        return ""

def indexcolumnconstraint_sql(self, expression: sqlglot.expressions.IndexColumnConstraint) -> str: View Source

    2598    def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> str:
    2599        kind = self.sql(expression, "kind")
    2600        kind = f"{kind} INDEX" if kind else "INDEX"
    2601        this = self.sql(expression, "this")
    2602        this = f" {this}" if this else ""
    2603        type_ = self.sql(expression, "type")
    2604        type_ = f" USING {type_}" if type_ else ""
    2605        schema = self.sql(expression, "schema")
    2606        schema = f" {schema}" if schema else ""
    2607        options = self.expressions(expression, key="options", sep=" ")
    2608        options = f" {options}" if options else ""
    2609        return f"{kind}{this}{type_}{schema}{options}"

def cached_generator( cache: Optional[Dict[int, str]] = None) -> Callable[[sqlglot.expressions.Expression], str]: View Source

    2612def cached_generator(
    2613    cache: t.Optional[t.Dict[int, str]] = None
    2614) -> t.Callable[[exp.Expression], str]:
    2615    """Returns a cached generator."""
    2616    cache = {} if cache is None else cache
    2617    generator = Generator(normalize=True, identify="safe")
    2618    return lambda e: generator.generate(e, cache)

Returns a cached generator.