Edit on GitHub

# sqlglot.generator

View Source

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

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

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

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

    408    def __init__(
    409        self,
    410        pretty: t.Optional[bool] = None,
    411        identify: str | bool = False,
    412        normalize: bool = False,
    413        pad: int = 2,
    414        indent: int = 2,
    415        normalize_functions: t.Optional[str | bool] = None,
    416        unsupported_level: ErrorLevel = ErrorLevel.WARN,
    417        max_unsupported: int = 3,
    418        leading_comma: bool = False,
    419        max_text_width: int = 80,
    420        comments: bool = True,
    421    ):
    422        import sqlglot
    423
    424        self.pretty = pretty if pretty is not None else sqlglot.pretty
    425        self.identify = identify
    426        self.normalize = normalize
    427        self.pad = pad
    428        self._indent = indent
    429        self.unsupported_level = unsupported_level
    430        self.max_unsupported = max_unsupported
    431        self.leading_comma = leading_comma
    432        self.max_text_width = max_text_width
    433        self.comments = comments
    434
    435        # This is both a Dialect property and a Generator argument, so we prioritize the latter
    436        self.normalize_functions = (
    437            self.NORMALIZE_FUNCTIONS if normalize_functions is None else normalize_functions
    438        )
    439
    440        self.unsupported_messages: t.List[str] = []
    441        self._escaped_quote_end: str = self.TOKENIZER_CLASS.STRING_ESCAPES[0] + self.QUOTE_END
    442        self._escaped_identifier_end: str = (
    443            self.TOKENIZER_CLASS.IDENTIFIER_ESCAPES[0] + self.IDENTIFIER_END
    444        )
    445        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.ClusteredColumnConstraint'>: <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.InputModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.IntervalSpan'>: <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.NonClusteredColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.NotForReplicationColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnCommitProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OnUpdateColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.OutputModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.PathColumnConstraint'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.RemoteWithConnectionModelProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.ReturnsProperty'>: <function Generator.<lambda>>, <class 'sqlglot.expressions.SampleProperty'>: <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.TransformModelProperty'>: <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>>}
LOG_BASE_FIRST = True
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'
LIMIT_ONLY_LITERALS = False
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
TZ_TO_WITH_TIME_ZONE = False
NVL2_SUPPORTED = True
SELECT_KINDS: Tuple[str, ...] = ('STRUCT', 'VALUE')
VALUES_AS_TABLE = True
ALTER_TABLE_ADD_COLUMN_KEYWORD = True
UNNEST_WITH_ORDINALITY = True
AGGREGATE_FILTER_SUPPORTED = True
SEMI_ANTI_JOIN_WITH_SIDE = True
SUPPORTS_PARAMETERS = True
COMPUTED_COLUMN_WITH_TYPE = True
SUPPORTS_TABLE_COPY = True
TABLESAMPLE_REQUIRES_PARENS = True
COLLATE_IS_FUNC = False
DATA_TYPE_SPECIFIERS_ALLOWED = False
TYPE_MAPPING = {<Type.NCHAR: 'NCHAR'>: 'CHAR', <Type.NVARCHAR: 'NVARCHAR'>: 'VARCHAR', <Type.MEDIUMTEXT: 'MEDIUMTEXT'>: 'TEXT', <Type.LONGTEXT: 'LONGTEXT'>: 'TEXT', <Type.TINYTEXT: 'TINYTEXT'>: 'TEXT', <Type.MEDIUMBLOB: 'MEDIUMBLOB'>: 'BLOB', <Type.LONGBLOB: 'LONGBLOB'>: 'BLOB', <Type.TINYBLOB: 'TINYBLOB'>: '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.InputModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <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.OnProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.OnCommitProperty'>: <Location.POST_EXPRESSION: 'POST_EXPRESSION'>, <class 'sqlglot.expressions.Order'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <class 'sqlglot.expressions.OutputModelProperty'>: <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.RemoteWithConnectionModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <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.SampleProperty'>: <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.TransformModelProperty'>: <Location.POST_SCHEMA: 'POST_SCHEMA'>, <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.Create'>, <class 'sqlglot.expressions.Delete'>, <class 'sqlglot.expressions.Drop'>, <class 'sqlglot.expressions.From'>, <class 'sqlglot.expressions.Insert'>, <class 'sqlglot.expressions.Join'>, <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 = {}
INVERSE_ESCAPE_SEQUENCES: Dict[str, str] = {}
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'
@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool: View Source

    279    @classmethod
    280    def can_identify(cls, text: str, identify: str | bool = "safe") -> bool:
    281        """Checks if text can be identified given an identify option.
    282
    283        Args:
    284            text: The text to check.
    285            identify:
    286                "always" or `True`: Always returns true.
    287                "safe": True if the identifier is case-insensitive.
    288
    289        Returns:
    290            Whether or not the given text can be identified.
    291        """
    292        if identify is True or identify == "always":
    293            return True
    294
    295        if identify == "safe":
    296            return not cls.case_sensitive(text)
    297
    298        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 = '"'
TOKENIZER_CLASS = <class 'sqlglot.tokens.Tokenizer'>
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

    447    def generate(
    448        self,
    449        expression: t.Optional[exp.Expression],
    450        cache: t.Optional[t.Dict[int, str]] = None,
    451    ) -> str:
    452        """
    453        Generates the SQL string corresponding to the given syntax tree.
    454
    455        Args:
    456            expression: The syntax tree.
    457            cache: An optional sql string cache. This leverages the hash of an Expression
    458                which can be slow to compute, so only use it if you set _hash on each node.
    459
    460        Returns:
    461            The SQL string corresponding to `expression`.
    462        """
    463        if cache is not None:
    464            self._cache = cache
    465
    466        self.unsupported_messages = []
    467        sql = self.sql(expression).strip()
    468        self._cache = None
    469
    470        if self.unsupported_level == ErrorLevel.IGNORE:
    471            return sql
    472
    473        if self.unsupported_level == ErrorLevel.WARN:
    474            for msg in self.unsupported_messages:
    475                logger.warning(msg)
    476        elif self.unsupported_level == ErrorLevel.RAISE and self.unsupported_messages:
    477            raise UnsupportedError(concat_messages(self.unsupported_messages, self.max_unsupported))
    478
    479        if self.pretty:
    480            sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
    481        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

    483    def unsupported(self, message: str) -> None:
    484        if self.unsupported_level == ErrorLevel.IMMEDIATE:
    485            raise UnsupportedError(message)
    486        self.unsupported_messages.append(message)

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

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

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

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

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

    494    def pad_comment(self, comment: str) -> str:
    495        comment = " " + comment if comment[0].strip() else comment
    496        comment = comment + " " if comment[-1].strip() else comment
    497        return comment

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

    499    def maybe_comment(
    500        self,
    501        sql: str,
    502        expression: t.Optional[exp.Expression] = None,
    503        comments: t.Optional[t.List[str]] = None,
    504    ) -> str:
    505        comments = (
    506            ((expression and expression.comments) if comments is None else comments)  # type: ignore
    507            if self.comments
    508            else None
    509        )
    510
    511        if not comments or isinstance(expression, exp.Binary):
    512            return sql
    513
    514        comments_sql = " ".join(
    515            f"/*{self.pad_comment(comment)}*/" for comment in comments if comment
    516        )
    517
    518        if not comments_sql:
    519            return sql
    520
    521        if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
    522            return (
    523                f"{self.sep()}{comments_sql}{sql}"
    524                if sql[0].isspace()
    525                else f"{comments_sql}{self.sep()}{sql}"
    526            )
    527
    528        return f"{sql} {comments_sql}"

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

    530    def wrap(self, expression: exp.Expression | str) -> str:
    531        this_sql = self.indent(
    532            self.sql(expression)
    533            if isinstance(expression, (exp.Select, exp.Union))
    534            else self.sql(expression, "this"),
    535            level=1,
    536            pad=0,
    537        )
    538        return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"

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

    540    def no_identify(self, func: t.Callable[..., str], *args, **kwargs) -> str:
    541        original = self.identify
    542        self.identify = False
    543        result = func(*args, **kwargs)
    544        self.identify = original
    545        return result

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

    547    def normalize_func(self, name: str) -> str:
    548        if self.normalize_functions == "upper" or self.normalize_functions is True:
    549            return name.upper()
    550        if self.normalize_functions == "lower":
    551            return name.lower()
    552        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

    554    def indent(
    555        self,
    556        sql: str,
    557        level: int = 0,
    558        pad: t.Optional[int] = None,
    559        skip_first: bool = False,
    560        skip_last: bool = False,
    561    ) -> str:
    562        if not self.pretty:
    563            return sql
    564
    565        pad = self.pad if pad is None else pad
    566        lines = sql.split("\n")
    567
    568        return "\n".join(
    569            line
    570            if (skip_first and i == 0) or (skip_last and i == len(lines) - 1)
    571            else f"{' ' * (level * self._indent + pad)}{line}"
    572            for i, line in enumerate(lines)
    573        )

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

    575    def sql(
    576        self,
    577        expression: t.Optional[str | exp.Expression],
    578        key: t.Optional[str] = None,
    579        comment: bool = True,
    580    ) -> str:
    581        if not expression:
    582            return ""
    583
    584        if isinstance(expression, str):
    585            return expression
    586
    587        if key:
    588            value = expression.args.get(key)
    589            if value:
    590                return self.sql(value)
    591            return ""
    592
    593        if self._cache is not None:
    594            expression_id = hash(expression)
    595
    596            if expression_id in self._cache:
    597                return self._cache[expression_id]
    598
    599        transform = self.TRANSFORMS.get(expression.__class__)
    600
    601        if callable(transform):
    602            sql = transform(self, expression)
    603        elif transform:
    604            sql = transform
    605        elif isinstance(expression, exp.Expression):
    606            exp_handler_name = f"{expression.key}_sql"
    607
    608            if hasattr(self, exp_handler_name):
    609                sql = getattr(self, exp_handler_name)(expression)
    610            elif isinstance(expression, exp.Func):
    611                sql = self.function_fallback_sql(expression)
    612            elif isinstance(expression, exp.Property):
    613                sql = self.property_sql(expression)
    614            else:
    615                raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
    616        else:
    617            raise ValueError(f"Expected an Expression. Received {type(expression)}: {expression}")
    618
    619        sql = self.maybe_comment(sql, expression) if self.comments and comment else sql
    620
    621        if self._cache is not None:
    622            self._cache[expression_id] = sql
    623        return sql

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

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

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

    630    def cache_sql(self, expression: exp.Cache) -> str:
    631        lazy = " LAZY" if expression.args.get("lazy") else ""
    632        table = self.sql(expression, "this")
    633        options = expression.args.get("options")
    634        options = f" OPTIONS({self.sql(options[0])} = {self.sql(options[1])})" if options else ""
    635        sql = self.sql(expression, "expression")
    636        sql = f" AS{self.sep()}{sql}" if sql else ""
    637        sql = f"CACHE{lazy} TABLE {table}{options}{sql}"
    638        return self.prepend_ctes(expression, sql)

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

    640    def characterset_sql(self, expression: exp.CharacterSet) -> str:
    641        if isinstance(expression.parent, exp.Cast):
    642            return f"CHAR CHARACTER SET {self.sql(expression, 'this')}"
    643        default = "DEFAULT " if expression.args.get("default") else ""
    644        return f"{default}CHARACTER SET={self.sql(expression, 'this')}"

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

    646    def column_sql(self, expression: exp.Column) -> str:
    647        join_mark = " (+)" if expression.args.get("join_mark") else ""
    648
    649        if join_mark and not self.COLUMN_JOIN_MARKS_SUPPORTED:
    650            join_mark = ""
    651            self.unsupported("Outer join syntax using the (+) operator is not supported.")
    652
    653        column = ".".join(
    654            self.sql(part)
    655            for part in (
    656                expression.args.get("catalog"),
    657                expression.args.get("db"),
    658                expression.args.get("table"),
    659                expression.args.get("this"),
    660            )
    661            if part
    662        )
    663
    664        return f"{column}{join_mark}"

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

    666    def columnposition_sql(self, expression: exp.ColumnPosition) -> str:
    667        this = self.sql(expression, "this")
    668        this = f" {this}" if this else ""
    669        position = self.sql(expression, "position")
    670        return f"{position}{this}"

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

    672    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
    673        column = self.sql(expression, "this")
    674        kind = self.sql(expression, "kind")
    675        constraints = self.expressions(expression, key="constraints", sep=" ", flat=True)
    676        exists = "IF NOT EXISTS " if expression.args.get("exists") else ""
    677        kind = f"{sep}{kind}" if kind else ""
    678        constraints = f" {constraints}" if constraints else ""
    679        position = self.sql(expression, "position")
    680        position = f" {position}" if position else ""
    681
    682        if expression.find(exp.ComputedColumnConstraint) and not self.COMPUTED_COLUMN_WITH_TYPE:
    683            kind = ""
    684
    685        return f"{exists}{column}{kind}{constraints}{position}"

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

    687    def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str:
    688        this = self.sql(expression, "this")
    689        kind_sql = self.sql(expression, "kind").strip()
    690        return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql

def computedcolumnconstraint_sql(self, expression: sqlglot.expressions.ComputedColumnConstraint) -> str: View Source

    692    def computedcolumnconstraint_sql(self, expression: exp.ComputedColumnConstraint) -> str:
    693        this = self.sql(expression, "this")
    694        if expression.args.get("not_null"):
    695            persisted = " PERSISTED NOT NULL"
    696        elif expression.args.get("persisted"):
    697            persisted = " PERSISTED"
    698        else:
    699            persisted = ""
    700        return f"AS {this}{persisted}"

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

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

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

    705    def compresscolumnconstraint_sql(self, expression: exp.CompressColumnConstraint) -> str:
    706        if isinstance(expression.this, list):
    707            this = self.wrap(self.expressions(expression, key="this", flat=True))
    708        else:
    709            this = self.sql(expression, "this")
    710
    711        return f"COMPRESS {this}"

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

    713    def generatedasidentitycolumnconstraint_sql(
    714        self, expression: exp.GeneratedAsIdentityColumnConstraint
    715    ) -> str:
    716        this = ""
    717        if expression.this is not None:
    718            on_null = " ON NULL" if expression.args.get("on_null") else ""
    719            this = " ALWAYS" if expression.this else f" BY DEFAULT{on_null}"
    720
    721        start = expression.args.get("start")
    722        start = f"START WITH {start}" if start else ""
    723        increment = expression.args.get("increment")
    724        increment = f" INCREMENT BY {increment}" if increment else ""
    725        minvalue = expression.args.get("minvalue")
    726        minvalue = f" MINVALUE {minvalue}" if minvalue else ""
    727        maxvalue = expression.args.get("maxvalue")
    728        maxvalue = f" MAXVALUE {maxvalue}" if maxvalue else ""
    729        cycle = expression.args.get("cycle")
    730        cycle_sql = ""
    731
    732        if cycle is not None:
    733            cycle_sql = f"{' NO' if not cycle else ''} CYCLE"
    734            cycle_sql = cycle_sql.strip() if not start and not increment else cycle_sql
    735
    736        sequence_opts = ""
    737        if start or increment or cycle_sql:
    738            sequence_opts = f"{start}{increment}{minvalue}{maxvalue}{cycle_sql}"
    739            sequence_opts = f" ({sequence_opts.strip()})"
    740
    741        expr = self.sql(expression, "expression")
    742        expr = f"({expr})" if expr else "IDENTITY"
    743
    744        return f"GENERATED{this} AS {expr}{sequence_opts}"

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

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

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

    749    def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
    750        desc = expression.args.get("desc")
    751        if desc is not None:
    752            return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
    753        return f"PRIMARY KEY"

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

    755    def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
    756        this = self.sql(expression, "this")
    757        this = f" {this}" if this else ""
    758        index_type = expression.args.get("index_type")
    759        index_type = f" USING {index_type}" if index_type else ""
    760        return f"UNIQUE{this}{index_type}"

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

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

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

    765    def create_sql(self, expression: exp.Create) -> str:
    766        kind = self.sql(expression, "kind").upper()
    767        properties = expression.args.get("properties")
    768        properties_locs = self.locate_properties(properties) if properties else defaultdict()
    769
    770        this = self.createable_sql(expression, properties_locs)
    771
    772        properties_sql = ""
    773        if properties_locs.get(exp.Properties.Location.POST_SCHEMA) or properties_locs.get(
    774            exp.Properties.Location.POST_WITH
    775        ):
    776            properties_sql = self.sql(
    777                exp.Properties(
    778                    expressions=[
    779                        *properties_locs[exp.Properties.Location.POST_SCHEMA],
    780                        *properties_locs[exp.Properties.Location.POST_WITH],
    781                    ]
    782                )
    783            )
    784
    785        begin = " BEGIN" if expression.args.get("begin") else ""
    786        end = " END" if expression.args.get("end") else ""
    787
    788        expression_sql = self.sql(expression, "expression")
    789        if expression_sql:
    790            expression_sql = f"{begin}{self.sep()}{expression_sql}{end}"
    791
    792            if self.CREATE_FUNCTION_RETURN_AS or not isinstance(expression.expression, exp.Return):
    793                if properties_locs.get(exp.Properties.Location.POST_ALIAS):
    794                    postalias_props_sql = self.properties(
    795                        exp.Properties(
    796                            expressions=properties_locs[exp.Properties.Location.POST_ALIAS]
    797                        ),
    798                        wrapped=False,
    799                    )
    800                    expression_sql = f" AS {postalias_props_sql}{expression_sql}"
    801                else:
    802                    expression_sql = f" AS{expression_sql}"
    803
    804        postindex_props_sql = ""
    805        if properties_locs.get(exp.Properties.Location.POST_INDEX):
    806            postindex_props_sql = self.properties(
    807                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_INDEX]),
    808                wrapped=False,
    809                prefix=" ",
    810            )
    811
    812        indexes = self.expressions(expression, key="indexes", indent=False, sep=" ")
    813        indexes = f" {indexes}" if indexes else ""
    814        index_sql = indexes + postindex_props_sql
    815
    816        replace = " OR REPLACE" if expression.args.get("replace") else ""
    817        unique = " UNIQUE" if expression.args.get("unique") else ""
    818
    819        postcreate_props_sql = ""
    820        if properties_locs.get(exp.Properties.Location.POST_CREATE):
    821            postcreate_props_sql = self.properties(
    822                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_CREATE]),
    823                sep=" ",
    824                prefix=" ",
    825                wrapped=False,
    826            )
    827
    828        modifiers = "".join((replace, unique, postcreate_props_sql))
    829
    830        postexpression_props_sql = ""
    831        if properties_locs.get(exp.Properties.Location.POST_EXPRESSION):
    832            postexpression_props_sql = self.properties(
    833                exp.Properties(
    834                    expressions=properties_locs[exp.Properties.Location.POST_EXPRESSION]
    835                ),
    836                sep=" ",
    837                prefix=" ",
    838                wrapped=False,
    839            )
    840
    841        exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else ""
    842        no_schema_binding = (
    843            " WITH NO SCHEMA BINDING" if expression.args.get("no_schema_binding") else ""
    844        )
    845
    846        clone = self.sql(expression, "clone")
    847        clone = f" {clone}" if clone else ""
    848
    849        expression_sql = f"CREATE{modifiers} {kind}{exists_sql} {this}{properties_sql}{expression_sql}{postexpression_props_sql}{index_sql}{no_schema_binding}{clone}"
    850        return self.prepend_ctes(expression, expression_sql)

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

    852    def clone_sql(self, expression: exp.Clone) -> str:
    853        this = self.sql(expression, "this")
    854        shallow = "SHALLOW " if expression.args.get("shallow") else ""
    855        keyword = "COPY" if expression.args.get("copy") and self.SUPPORTS_TABLE_COPY else "CLONE"
    856        this = f"{shallow}{keyword} {this}"
    857        when = self.sql(expression, "when")
    858
    859        if when:
    860            kind = self.sql(expression, "kind")
    861            expr = self.sql(expression, "expression")
    862            return f"{this} {when} ({kind} => {expr})"
    863
    864        return this

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

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

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

    869    def prepend_ctes(self, expression: exp.Expression, sql: str) -> str:
    870        with_ = self.sql(expression, "with")
    871        if with_:
    872            sql = f"{with_}{self.sep()}{sql}"
    873        return sql

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

    875    def with_sql(self, expression: exp.With) -> str:
    876        sql = self.expressions(expression, flat=True)
    877        recursive = "RECURSIVE " if expression.args.get("recursive") else ""
    878
    879        return f"WITH {recursive}{sql}"

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

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

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

    885    def tablealias_sql(self, expression: exp.TableAlias) -> str:
    886        alias = self.sql(expression, "this")
    887        columns = self.expressions(expression, key="columns", flat=True)
    888        columns = f"({columns})" if columns else ""
    889        return f"{alias}{columns}"

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

    891    def bitstring_sql(self, expression: exp.BitString) -> str:
    892        this = self.sql(expression, "this")
    893        if self.BIT_START:
    894            return f"{self.BIT_START}{this}{self.BIT_END}"
    895        return f"{int(this, 2)}"

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

    897    def hexstring_sql(self, expression: exp.HexString) -> str:
    898        this = self.sql(expression, "this")
    899        if self.HEX_START:
    900            return f"{self.HEX_START}{this}{self.HEX_END}"
    901        return f"{int(this, 16)}"

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

    903    def bytestring_sql(self, expression: exp.ByteString) -> str:
    904        this = self.sql(expression, "this")
    905        if self.BYTE_START:
    906            return f"{self.BYTE_START}{this}{self.BYTE_END}"
    907        return this

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

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

def datatypeparam_sql(self, expression: sqlglot.expressions.DataTypeParam) -> str: View Source

    913    def datatypeparam_sql(self, expression: exp.DataTypeParam) -> str:
    914        this = self.sql(expression, "this")
    915        specifier = self.sql(expression, "expression")
    916        specifier = f" {specifier}" if specifier and self.DATA_TYPE_SPECIFIERS_ALLOWED else ""
    917        return f"{this}{specifier}"

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

    919    def datatype_sql(self, expression: exp.DataType) -> str:
    920        type_value = expression.this
    921
    922        if type_value == exp.DataType.Type.USERDEFINED and expression.args.get("kind"):
    923            type_sql = self.sql(expression, "kind")
    924        else:
    925            type_sql = (
    926                self.TYPE_MAPPING.get(type_value, type_value.value)
    927                if isinstance(type_value, exp.DataType.Type)
    928                else type_value
    929            )
    930
    931        nested = ""
    932        interior = self.expressions(expression, flat=True)
    933        values = ""
    934
    935        if interior:
    936            if expression.args.get("nested"):
    937                nested = f"{self.STRUCT_DELIMITER[0]}{interior}{self.STRUCT_DELIMITER[1]}"
    938                if expression.args.get("values") is not None:
    939                    delimiters = ("[", "]") if type_value == exp.DataType.Type.ARRAY else ("(", ")")
    940                    values = self.expressions(expression, key="values", flat=True)
    941                    values = f"{delimiters[0]}{values}{delimiters[1]}"
    942            elif type_value == exp.DataType.Type.INTERVAL:
    943                nested = f" {interior}"
    944            else:
    945                nested = f"({interior})"
    946
    947        type_sql = f"{type_sql}{nested}{values}"
    948        if self.TZ_TO_WITH_TIME_ZONE and type_value in (
    949            exp.DataType.Type.TIMETZ,
    950            exp.DataType.Type.TIMESTAMPTZ,
    951        ):
    952            type_sql = f"{type_sql} WITH TIME ZONE"
    953
    954        return type_sql

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

    956    def directory_sql(self, expression: exp.Directory) -> str:
    957        local = "LOCAL " if expression.args.get("local") else ""
    958        row_format = self.sql(expression, "row_format")
    959        row_format = f" {row_format}" if row_format else ""
    960        return f"{local}DIRECTORY {self.sql(expression, 'this')}{row_format}"

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

    962    def delete_sql(self, expression: exp.Delete) -> str:
    963        this = self.sql(expression, "this")
    964        this = f" FROM {this}" if this else ""
    965        using = self.sql(expression, "using")
    966        using = f" USING {using}" if using else ""
    967        where = self.sql(expression, "where")
    968        returning = self.sql(expression, "returning")
    969        limit = self.sql(expression, "limit")
    970        tables = self.expressions(expression, key="tables")
    971        tables = f" {tables}" if tables else ""
    972        if self.RETURNING_END:
    973            expression_sql = f"{this}{using}{where}{returning}{limit}"
    974        else:
    975            expression_sql = f"{returning}{this}{using}{where}{limit}"
    976        return self.prepend_ctes(expression, f"DELETE{tables}{expression_sql}")

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

    978    def drop_sql(self, expression: exp.Drop) -> str:
    979        this = self.sql(expression, "this")
    980        kind = expression.args["kind"]
    981        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    982        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    983        materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
    984        cascade = " CASCADE" if expression.args.get("cascade") else ""
    985        constraints = " CONSTRAINTS" if expression.args.get("constraints") else ""
    986        purge = " PURGE" if expression.args.get("purge") else ""
    987        return (
    988            f"DROP{temporary}{materialized} {kind}{exists_sql}{this}{cascade}{constraints}{purge}"
    989        )

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

    991    def except_sql(self, expression: exp.Except) -> str:
    992        return self.prepend_ctes(
    993            expression,
    994            self.set_operation(expression, self.except_op(expression)),
    995        )

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

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

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

    1000    def fetch_sql(self, expression: exp.Fetch) -> str:
    1001        direction = expression.args.get("direction")
    1002        direction = f" {direction.upper()}" if direction else ""
    1003        count = expression.args.get("count")
    1004        count = f" {count}" if count else ""
    1005        if expression.args.get("percent"):
    1006            count = f"{count} PERCENT"
    1007        with_ties_or_only = "WITH TIES" if expression.args.get("with_ties") else "ONLY"
    1008        return f"{self.seg('FETCH')}{direction}{count} ROWS {with_ties_or_only}"

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

    1010    def filter_sql(self, expression: exp.Filter) -> str:
    1011        if self.AGGREGATE_FILTER_SUPPORTED:
    1012            this = self.sql(expression, "this")
    1013            where = self.sql(expression, "expression").strip()
    1014            return f"{this} FILTER({where})"
    1015
    1016        agg = expression.this.copy()
    1017        agg_arg = agg.this
    1018        cond = expression.expression.this
    1019        agg_arg.replace(exp.If(this=cond.copy(), true=agg_arg.copy()))
    1020        return self.sql(agg)

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

    1022    def hint_sql(self, expression: exp.Hint) -> str:
    1023        if not self.QUERY_HINTS:
    1024            self.unsupported("Hints are not supported")
    1025            return ""
    1026
    1027        return f" /*+ {self.expressions(expression, sep=self.QUERY_HINT_SEP).strip()} */"

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

    1029    def index_sql(self, expression: exp.Index) -> str:
    1030        unique = "UNIQUE " if expression.args.get("unique") else ""
    1031        primary = "PRIMARY " if expression.args.get("primary") else ""
    1032        amp = "AMP " if expression.args.get("amp") else ""
    1033        name = self.sql(expression, "this")
    1034        name = f"{name} " if name else ""
    1035        table = self.sql(expression, "table")
    1036        table = f"{self.INDEX_ON} {table}" if table else ""
    1037        using = self.sql(expression, "using")
    1038        using = f" USING {using}" if using else ""
    1039        index = "INDEX " if not table else ""
    1040        columns = self.expressions(expression, key="columns", flat=True)
    1041        columns = f"({columns})" if columns else ""
    1042        partition_by = self.expressions(expression, key="partition_by", flat=True)
    1043        partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
    1044        where = self.sql(expression, "where")
    1045        return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}{where}"

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

    1047    def identifier_sql(self, expression: exp.Identifier) -> str:
    1048        text = expression.name
    1049        lower = text.lower()
    1050        text = lower if self.normalize and not expression.quoted else text
    1051        text = text.replace(self.IDENTIFIER_END, self._escaped_identifier_end)
    1052        if (
    1053            expression.quoted
    1054            or self.can_identify(text, self.identify)
    1055            or lower in self.RESERVED_KEYWORDS
    1056            or (not self.IDENTIFIERS_CAN_START_WITH_DIGIT and text[:1].isdigit())
    1057        ):
    1058            text = f"{self.IDENTIFIER_START}{text}{self.IDENTIFIER_END}"
    1059        return text

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

    1061    def inputoutputformat_sql(self, expression: exp.InputOutputFormat) -> str:
    1062        input_format = self.sql(expression, "input_format")
    1063        input_format = f"INPUTFORMAT {input_format}" if input_format else ""
    1064        output_format = self.sql(expression, "output_format")
    1065        output_format = f"OUTPUTFORMAT {output_format}" if output_format else ""
    1066        return self.sep().join((input_format, output_format))

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

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

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

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

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

    1075    def properties_sql(self, expression: exp.Properties) -> str:
    1076        root_properties = []
    1077        with_properties = []
    1078
    1079        for p in expression.expressions:
    1080            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    1081            if p_loc == exp.Properties.Location.POST_WITH:
    1082                with_properties.append(p.copy())
    1083            elif p_loc == exp.Properties.Location.POST_SCHEMA:
    1084                root_properties.append(p.copy())
    1085
    1086        return self.root_properties(
    1087            exp.Properties(expressions=root_properties)
    1088        ) + self.with_properties(exp.Properties(expressions=with_properties))

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

    1090    def root_properties(self, properties: exp.Properties) -> str:
    1091        if properties.expressions:
    1092            return self.sep() + self.expressions(properties, indent=False, sep=" ")
    1093        return ""

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

    1095    def properties(
    1096        self,
    1097        properties: exp.Properties,
    1098        prefix: str = "",
    1099        sep: str = ", ",
    1100        suffix: str = "",
    1101        wrapped: bool = True,
    1102    ) -> str:
    1103        if properties.expressions:
    1104            expressions = self.expressions(properties, sep=sep, indent=False)
    1105            if expressions:
    1106                expressions = self.wrap(expressions) if wrapped else expressions
    1107                return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
    1108        return ""

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

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

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

    1113    def locate_properties(self, properties: exp.Properties) -> t.DefaultDict:
    1114        properties_locs = defaultdict(list)
    1115        for p in properties.expressions:
    1116            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    1117            if p_loc != exp.Properties.Location.UNSUPPORTED:
    1118                properties_locs[p_loc].append(p.copy())
    1119            else:
    1120                self.unsupported(f"Unsupported property {p.key}")
    1121
    1122        return properties_locs

def property_name( self, expression: sqlglot.expressions.Property, string_key: bool = False) -> str: View Source

    1124    def property_name(self, expression: exp.Property, string_key: bool = False) -> str:
    1125        if isinstance(expression.this, exp.Dot):
    1126            return self.sql(expression, "this")
    1127        return f"'{expression.name}'" if string_key else expression.name

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

    1129    def property_sql(self, expression: exp.Property) -> str:
    1130        property_cls = expression.__class__
    1131        if property_cls == exp.Property:
    1132            return f"{self.property_name(expression)}={self.sql(expression, 'value')}"
    1133
    1134        property_name = exp.Properties.PROPERTY_TO_NAME.get(property_cls)
    1135        if not property_name:
    1136            self.unsupported(f"Unsupported property {expression.key}")
    1137
    1138        return f"{property_name}={self.sql(expression, 'this')}"

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

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

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

    1145    def fallbackproperty_sql(self, expression: exp.FallbackProperty) -> str:
    1146        no = "NO " if expression.args.get("no") else ""
    1147        protection = " PROTECTION" if expression.args.get("protection") else ""
    1148        return f"{no}FALLBACK{protection}"

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

    1150    def journalproperty_sql(self, expression: exp.JournalProperty) -> str:
    1151        no = "NO " if expression.args.get("no") else ""
    1152        local = expression.args.get("local")
    1153        local = f"{local} " if local else ""
    1154        dual = "DUAL " if expression.args.get("dual") else ""
    1155        before = "BEFORE " if expression.args.get("before") else ""
    1156        after = "AFTER " if expression.args.get("after") else ""
    1157        return f"{no}{local}{dual}{before}{after}JOURNAL"

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

    1159    def freespaceproperty_sql(self, expression: exp.FreespaceProperty) -> str:
    1160        freespace = self.sql(expression, "this")
    1161        percent = " PERCENT" if expression.args.get("percent") else ""
    1162        return f"FREESPACE={freespace}{percent}"

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

    1164    def checksumproperty_sql(self, expression: exp.ChecksumProperty) -> str:
    1165        if expression.args.get("default"):
    1166            property = "DEFAULT"
    1167        elif expression.args.get("on"):
    1168            property = "ON"
    1169        else:
    1170            property = "OFF"
    1171        return f"CHECKSUM={property}"

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

    1173    def mergeblockratioproperty_sql(self, expression: exp.MergeBlockRatioProperty) -> str:
    1174        if expression.args.get("no"):
    1175            return "NO MERGEBLOCKRATIO"
    1176        if expression.args.get("default"):
    1177            return "DEFAULT MERGEBLOCKRATIO"
    1178
    1179        percent = " PERCENT" if expression.args.get("percent") else ""
    1180        return f"MERGEBLOCKRATIO={self.sql(expression, 'this')}{percent}"

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

    1182    def datablocksizeproperty_sql(self, expression: exp.DataBlocksizeProperty) -> str:
    1183        default = expression.args.get("default")
    1184        minimum = expression.args.get("minimum")
    1185        maximum = expression.args.get("maximum")
    1186        if default or minimum or maximum:
    1187            if default:
    1188                prop = "DEFAULT"
    1189            elif minimum:
    1190                prop = "MINIMUM"
    1191            else:
    1192                prop = "MAXIMUM"
    1193            return f"{prop} DATABLOCKSIZE"
    1194        units = expression.args.get("units")
    1195        units = f" {units}" if units else ""
    1196        return f"DATABLOCKSIZE={self.sql(expression, 'size')}{units}"

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

    1198    def blockcompressionproperty_sql(self, expression: exp.BlockCompressionProperty) -> str:
    1199        autotemp = expression.args.get("autotemp")
    1200        always = expression.args.get("always")
    1201        default = expression.args.get("default")
    1202        manual = expression.args.get("manual")
    1203        never = expression.args.get("never")
    1204
    1205        if autotemp is not None:
    1206            prop = f"AUTOTEMP({self.expressions(autotemp)})"
    1207        elif always:
    1208            prop = "ALWAYS"
    1209        elif default:
    1210            prop = "DEFAULT"
    1211        elif manual:
    1212            prop = "MANUAL"
    1213        elif never:
    1214            prop = "NEVER"
    1215        return f"BLOCKCOMPRESSION={prop}"

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

    1217    def isolatedloadingproperty_sql(self, expression: exp.IsolatedLoadingProperty) -> str:
    1218        no = expression.args.get("no")
    1219        no = " NO" if no else ""
    1220        concurrent = expression.args.get("concurrent")
    1221        concurrent = " CONCURRENT" if concurrent else ""
    1222
    1223        for_ = ""
    1224        if expression.args.get("for_all"):
    1225            for_ = " FOR ALL"
    1226        elif expression.args.get("for_insert"):
    1227            for_ = " FOR INSERT"
    1228        elif expression.args.get("for_none"):
    1229            for_ = " FOR NONE"
    1230        return f"WITH{no}{concurrent} ISOLATED LOADING{for_}"

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

    1232    def lockingproperty_sql(self, expression: exp.LockingProperty) -> str:
    1233        kind = expression.args.get("kind")
    1234        this = f" {self.sql(expression, 'this')}" if expression.this else ""
    1235        for_or_in = expression.args.get("for_or_in")
    1236        for_or_in = f" {for_or_in}" if for_or_in else ""
    1237        lock_type = expression.args.get("lock_type")
    1238        override = " OVERRIDE" if expression.args.get("override") else ""
    1239        return f"LOCKING {kind}{this}{for_or_in} {lock_type}{override}"

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

    1241    def withdataproperty_sql(self, expression: exp.WithDataProperty) -> str:
    1242        data_sql = f"WITH {'NO ' if expression.args.get('no') else ''}DATA"
    1243        statistics = expression.args.get("statistics")
    1244        statistics_sql = ""
    1245        if statistics is not None:
    1246            statistics_sql = f" AND {'NO ' if not statistics else ''}STATISTICS"
    1247        return f"{data_sql}{statistics_sql}"

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

    1249    def insert_sql(self, expression: exp.Insert) -> str:
    1250        overwrite = expression.args.get("overwrite")
    1251
    1252        if isinstance(expression.this, exp.Directory):
    1253            this = " OVERWRITE" if overwrite else " INTO"
    1254        else:
    1255            this = " OVERWRITE TABLE" if overwrite else " INTO"
    1256
    1257        alternative = expression.args.get("alternative")
    1258        alternative = f" OR {alternative}" if alternative else ""
    1259        ignore = " IGNORE" if expression.args.get("ignore") else ""
    1260
    1261        this = f"{this} {self.sql(expression, 'this')}"
    1262
    1263        exists = " IF EXISTS" if expression.args.get("exists") else ""
    1264        partition_sql = (
    1265            f" {self.sql(expression, 'partition')}" if expression.args.get("partition") else ""
    1266        )
    1267        where = self.sql(expression, "where")
    1268        where = f"{self.sep()}REPLACE WHERE {where}" if where else ""
    1269        expression_sql = f"{self.sep()}{self.sql(expression, 'expression')}"
    1270        conflict = self.sql(expression, "conflict")
    1271        by_name = " BY NAME" if expression.args.get("by_name") else ""
    1272        returning = self.sql(expression, "returning")
    1273
    1274        if self.RETURNING_END:
    1275            expression_sql = f"{expression_sql}{conflict}{returning}"
    1276        else:
    1277            expression_sql = f"{returning}{expression_sql}{conflict}"
    1278
    1279        sql = f"INSERT{alternative}{ignore}{this}{by_name}{exists}{partition_sql}{where}{expression_sql}"
    1280        return self.prepend_ctes(expression, sql)

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

    1282    def intersect_sql(self, expression: exp.Intersect) -> str:
    1283        return self.prepend_ctes(
    1284            expression,
    1285            self.set_operation(expression, self.intersect_op(expression)),
    1286        )

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

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

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

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

def kill_sql(self, expression: sqlglot.expressions.Kill) -> str: View Source

    1294    def kill_sql(self, expression: exp.Kill) -> str:
    1295        kind = self.sql(expression, "kind")
    1296        kind = f" {kind}" if kind else ""
    1297        this = self.sql(expression, "this")
    1298        this = f" {this}" if this else ""
    1299        return f"KILL{kind}{this}"

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

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

def objectidentifier_sql(self, expression: sqlglot.expressions.ObjectIdentifier) -> str: View Source

    1304    def objectidentifier_sql(self, expression: exp.ObjectIdentifier) -> str:
    1305        return expression.name.upper()

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

    1307    def onconflict_sql(self, expression: exp.OnConflict) -> str:
    1308        conflict = "ON DUPLICATE KEY" if expression.args.get("duplicate") else "ON CONFLICT"
    1309        constraint = self.sql(expression, "constraint")
    1310        if constraint:
    1311            constraint = f"ON CONSTRAINT {constraint}"
    1312        key = self.expressions(expression, key="key", flat=True)
    1313        do = "" if expression.args.get("duplicate") else " DO "
    1314        nothing = "NOTHING" if expression.args.get("nothing") else ""
    1315        expressions = self.expressions(expression, flat=True)
    1316        set_keyword = "SET " if self.DUPLICATE_KEY_UPDATE_WITH_SET else ""
    1317        if expressions:
    1318            expressions = f"UPDATE {set_keyword}{expressions}"
    1319        return f"{self.seg(conflict)} {constraint}{key}{do}{nothing}{expressions}"

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

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

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

    1324    def rowformatdelimitedproperty_sql(self, expression: exp.RowFormatDelimitedProperty) -> str:
    1325        fields = expression.args.get("fields")
    1326        fields = f" FIELDS TERMINATED BY {fields}" if fields else ""
    1327        escaped = expression.args.get("escaped")
    1328        escaped = f" ESCAPED BY {escaped}" if escaped else ""
    1329        items = expression.args.get("collection_items")
    1330        items = f" COLLECTION ITEMS TERMINATED BY {items}" if items else ""
    1331        keys = expression.args.get("map_keys")
    1332        keys = f" MAP KEYS TERMINATED BY {keys}" if keys else ""
    1333        lines = expression.args.get("lines")
    1334        lines = f" LINES TERMINATED BY {lines}" if lines else ""
    1335        null = expression.args.get("null")
    1336        null = f" NULL DEFINED AS {null}" if null else ""
    1337        return f"ROW FORMAT DELIMITED{fields}{escaped}{items}{keys}{lines}{null}"

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

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

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

    1342    def indextablehint_sql(self, expression: exp.IndexTableHint) -> str:
    1343        this = f"{self.sql(expression, 'this')} INDEX"
    1344        target = self.sql(expression, "target")
    1345        target = f" FOR {target}" if target else ""
    1346        return f"{this}{target} ({self.expressions(expression, flat=True)})"

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

    1348    def table_sql(self, expression: exp.Table, sep: str = " AS ") -> str:
    1349        table = ".".join(
    1350            part
    1351            for part in [
    1352                self.sql(expression, "catalog"),
    1353                self.sql(expression, "db"),
    1354                self.sql(expression, "this"),
    1355            ]
    1356            if part
    1357        )
    1358
    1359        version = self.sql(expression, "version")
    1360        version = f" {version}" if version else ""
    1361        alias = self.sql(expression, "alias")
    1362        alias = f"{sep}{alias}" if alias else ""
    1363        hints = self.expressions(expression, key="hints", sep=" ")
    1364        hints = f" {hints}" if hints and self.TABLE_HINTS else ""
    1365        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1366        pivots = f" {pivots}" if pivots else ""
    1367        joins = self.expressions(expression, key="joins", sep="", skip_first=True)
    1368        laterals = self.expressions(expression, key="laterals", sep="")
    1369
    1370        file_format = self.sql(expression, "format")
    1371        if file_format:
    1372            pattern = self.sql(expression, "pattern")
    1373            pattern = f", PATTERN => {pattern}" if pattern else ""
    1374            file_format = f" (FILE_FORMAT => {file_format}{pattern})"
    1375
    1376        index = self.sql(expression, "index")
    1377        index = f" AT {index}" if index else ""
    1378
    1379        return f"{table}{version}{file_format}{alias}{index}{hints}{pivots}{joins}{laterals}"

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

    1381    def tablesample_sql(
    1382        self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    1383    ) -> str:
    1384        if self.ALIAS_POST_TABLESAMPLE and expression.this.alias:
    1385            table = expression.this.copy()
    1386            table.set("alias", None)
    1387            this = self.sql(table)
    1388            alias = f"{sep}{self.sql(expression.this, 'alias')}"
    1389        else:
    1390            this = self.sql(expression, "this")
    1391            alias = ""
    1392
    1393        method = self.sql(expression, "method")
    1394        method = f"{method.upper()} " if method and self.TABLESAMPLE_WITH_METHOD else ""
    1395        numerator = self.sql(expression, "bucket_numerator")
    1396        denominator = self.sql(expression, "bucket_denominator")
    1397        field = self.sql(expression, "bucket_field")
    1398        field = f" ON {field}" if field else ""
    1399        bucket = f"BUCKET {numerator} OUT OF {denominator}{field}" if numerator else ""
    1400        percent = self.sql(expression, "percent")
    1401        percent = f"{percent} PERCENT" if percent else ""
    1402        rows = self.sql(expression, "rows")
    1403        rows = f"{rows} ROWS" if rows else ""
    1404
    1405        size = self.sql(expression, "size")
    1406        if size and self.TABLESAMPLE_SIZE_IS_PERCENT:
    1407            size = f"{size} PERCENT"
    1408
    1409        seed = self.sql(expression, "seed")
    1410        seed = f" {seed_prefix} ({seed})" if seed else ""
    1411        kind = expression.args.get("kind", "TABLESAMPLE")
    1412
    1413        expr = f"{bucket}{percent}{rows}{size}"
    1414        if self.TABLESAMPLE_REQUIRES_PARENS:
    1415            expr = f"({expr})"
    1416
    1417        return f"{this} {kind} {method}{expr}{seed}{alias}"

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

    1419    def pivot_sql(self, expression: exp.Pivot) -> str:
    1420        expressions = self.expressions(expression, flat=True)
    1421
    1422        if expression.this:
    1423            this = self.sql(expression, "this")
    1424            if not expressions:
    1425                return f"UNPIVOT {this}"
    1426
    1427            on = f"{self.seg('ON')} {expressions}"
    1428            using = self.expressions(expression, key="using", flat=True)
    1429            using = f"{self.seg('USING')} {using}" if using else ""
    1430            group = self.sql(expression, "group")
    1431            return f"PIVOT {this}{on}{using}{group}"
    1432
    1433        alias = self.sql(expression, "alias")
    1434        alias = f" AS {alias}" if alias else ""
    1435        unpivot = expression.args.get("unpivot")
    1436        direction = "UNPIVOT" if unpivot else "PIVOT"
    1437        field = self.sql(expression, "field")
    1438        include_nulls = expression.args.get("include_nulls")
    1439        if include_nulls is not None:
    1440            nulls = " INCLUDE NULLS " if include_nulls else " EXCLUDE NULLS "
    1441        else:
    1442            nulls = ""
    1443        return f"{direction}{nulls}({expressions} FOR {field}){alias}"

def version_sql(self, expression: sqlglot.expressions.Version) -> str: View Source

    1445    def version_sql(self, expression: exp.Version) -> str:
    1446        this = f"FOR {expression.name}"
    1447        kind = expression.text("kind")
    1448        expr = self.sql(expression, "expression")
    1449        return f"{this} {kind} {expr}"

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

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

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

    1454    def update_sql(self, expression: exp.Update) -> str:
    1455        this = self.sql(expression, "this")
    1456        set_sql = self.expressions(expression, flat=True)
    1457        from_sql = self.sql(expression, "from")
    1458        where_sql = self.sql(expression, "where")
    1459        returning = self.sql(expression, "returning")
    1460        order = self.sql(expression, "order")
    1461        limit = self.sql(expression, "limit")
    1462        if self.RETURNING_END:
    1463            expression_sql = f"{from_sql}{where_sql}{returning}"
    1464        else:
    1465            expression_sql = f"{returning}{from_sql}{where_sql}"
    1466        sql = f"UPDATE {this} SET {set_sql}{expression_sql}{order}{limit}"
    1467        return self.prepend_ctes(expression, sql)

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

    1469    def values_sql(self, expression: exp.Values) -> str:
    1470        # The VALUES clause is still valid in an `INSERT INTO ..` statement, for example
    1471        if self.VALUES_AS_TABLE or not expression.find_ancestor(exp.From, exp.Join):
    1472            args = self.expressions(expression)
    1473            alias = self.sql(expression, "alias")
    1474            values = f"VALUES{self.seg('')}{args}"
    1475            values = (
    1476                f"({values})"
    1477                if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
    1478                else values
    1479            )
    1480            return f"{values} AS {alias}" if alias else values
    1481
    1482        # Converts `VALUES...` expression into a series of select unions.
    1483        expression = expression.copy()
    1484        alias_node = expression.args.get("alias")
    1485        column_names = alias_node and alias_node.columns
    1486
    1487        selects: t.List[exp.Subqueryable] = []
    1488
    1489        for i, tup in enumerate(expression.expressions):
    1490            row = tup.expressions
    1491
    1492            if i == 0 and column_names:
    1493                row = [
    1494                    exp.alias_(value, column_name) for value, column_name in zip(row, column_names)
    1495                ]
    1496
    1497            selects.append(exp.Select(expressions=row))
    1498
    1499        if self.pretty:
    1500            # This may result in poor performance for large-cardinality `VALUES` tables, due to
    1501            # the deep nesting of the resulting exp.Unions. If this is a problem, either increase
    1502            # `sys.setrecursionlimit` to avoid RecursionErrors, or don't set `pretty`.
    1503            subqueryable = reduce(lambda x, y: exp.union(x, y, distinct=False, copy=False), selects)
    1504            return self.subquery_sql(
    1505                subqueryable.subquery(alias_node and alias_node.this, copy=False)
    1506            )
    1507
    1508        alias = f" AS {self.sql(alias_node, 'this')}" if alias_node else ""
    1509        unions = " UNION ALL ".join(self.sql(select) for select in selects)
    1510        return f"({unions}){alias}"

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

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

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

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

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

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

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

    1523    def group_sql(self, expression: exp.Group) -> str:
    1524        group_by = self.op_expressions("GROUP BY", expression)
    1525
    1526        if expression.args.get("all"):
    1527            return f"{group_by} ALL"
    1528
    1529        grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
    1530        grouping_sets = (
    1531            f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
    1532        )
    1533
    1534        cube = expression.args.get("cube", [])
    1535        if seq_get(cube, 0) is True:
    1536            return f"{group_by}{self.seg('WITH CUBE')}"
    1537        else:
    1538            cube_sql = self.expressions(expression, key="cube", indent=False)
    1539            cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else ""
    1540
    1541        rollup = expression.args.get("rollup", [])
    1542        if seq_get(rollup, 0) is True:
    1543            return f"{group_by}{self.seg('WITH ROLLUP')}"
    1544        else:
    1545            rollup_sql = self.expressions(expression, key="rollup", indent=False)
    1546            rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else ""
    1547
    1548        groupings = csv(
    1549            grouping_sets,
    1550            cube_sql,
    1551            rollup_sql,
    1552            self.seg("WITH TOTALS") if expression.args.get("totals") else "",
    1553            sep=self.GROUPINGS_SEP,
    1554        )
    1555
    1556        if expression.args.get("expressions") and groupings:
    1557            group_by = f"{group_by}{self.GROUPINGS_SEP}"
    1558
    1559        return f"{group_by}{groupings}"

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

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

def connect_sql(self, expression: sqlglot.expressions.Connect) -> str: View Source

    1565    def connect_sql(self, expression: exp.Connect) -> str:
    1566        start = self.sql(expression, "start")
    1567        start = self.seg(f"START WITH {start}") if start else ""
    1568        connect = self.sql(expression, "connect")
    1569        connect = self.seg(f"CONNECT BY {connect}")
    1570        return start + connect

def prior_sql(self, expression: sqlglot.expressions.Prior) -> str: View Source

    1572    def prior_sql(self, expression: exp.Prior) -> str:
    1573        return f"PRIOR {self.sql(expression, 'this')}"

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

    1575    def join_sql(self, expression: exp.Join) -> str:
    1576        if not self.SEMI_ANTI_JOIN_WITH_SIDE and expression.kind in ("SEMI", "ANTI"):
    1577            side = None
    1578        else:
    1579            side = expression.side
    1580
    1581        op_sql = " ".join(
    1582            op
    1583            for op in (
    1584                expression.method,
    1585                "GLOBAL" if expression.args.get("global") else None,
    1586                side,
    1587                expression.kind,
    1588                expression.hint if self.JOIN_HINTS else None,
    1589            )
    1590            if op
    1591        )
    1592        on_sql = self.sql(expression, "on")
    1593        using = expression.args.get("using")
    1594
    1595        if not on_sql and using:
    1596            on_sql = csv(*(self.sql(column) for column in using))
    1597
    1598        this_sql = self.sql(expression, "this")
    1599
    1600        if on_sql:
    1601            on_sql = self.indent(on_sql, skip_first=True)
    1602            space = self.seg(" " * self.pad) if self.pretty else " "
    1603            if using:
    1604                on_sql = f"{space}USING ({on_sql})"
    1605            else:
    1606                on_sql = f"{space}ON {on_sql}"
    1607        elif not op_sql:
    1608            return f", {this_sql}"
    1609
    1610        op_sql = f"{op_sql} JOIN" if op_sql else "JOIN"
    1611        return f"{self.seg(op_sql)} {this_sql}{on_sql}"

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

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

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

    1618    def lateral_sql(self, expression: exp.Lateral) -> str:
    1619        this = self.sql(expression, "this")
    1620
    1621        if isinstance(expression.this, exp.Subquery):
    1622            return f"LATERAL {this}"
    1623
    1624        if expression.args.get("view"):
    1625            alias = expression.args["alias"]
    1626            columns = self.expressions(alias, key="columns", flat=True)
    1627            table = f" {alias.name}" if alias.name else ""
    1628            columns = f" AS {columns}" if columns else ""
    1629            op_sql = self.seg(f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}")
    1630            return f"{op_sql}{self.sep()}{this}{table}{columns}"
    1631
    1632        alias = self.sql(expression, "alias")
    1633        alias = f" AS {alias}" if alias else ""
    1634        return f"LATERAL {this}{alias}"

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

    1636    def limit_sql(self, expression: exp.Limit, top: bool = False) -> str:
    1637        this = self.sql(expression, "this")
    1638        args = ", ".join(
    1639            self.sql(self._simplify_unless_literal(e) if self.LIMIT_ONLY_LITERALS else e)
    1640            for e in (expression.args.get(k) for k in ("offset", "expression"))
    1641            if e
    1642        )
    1643        return f"{this}{self.seg('TOP' if top else 'LIMIT')} {args}"

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

    1645    def offset_sql(self, expression: exp.Offset) -> str:
    1646        this = self.sql(expression, "this")
    1647        expression = expression.expression
    1648        expression = (
    1649            self._simplify_unless_literal(expression) if self.LIMIT_ONLY_LITERALS else expression
    1650        )
    1651        return f"{this}{self.seg('OFFSET')} {self.sql(expression)}"

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

    1653    def setitem_sql(self, expression: exp.SetItem) -> str:
    1654        kind = self.sql(expression, "kind")
    1655        kind = f"{kind} " if kind else ""
    1656        this = self.sql(expression, "this")
    1657        expressions = self.expressions(expression)
    1658        collate = self.sql(expression, "collate")
    1659        collate = f" COLLATE {collate}" if collate else ""
    1660        global_ = "GLOBAL " if expression.args.get("global") else ""
    1661        return f"{global_}{kind}{this}{expressions}{collate}"

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

    1663    def set_sql(self, expression: exp.Set) -> str:
    1664        expressions = (
    1665            f" {self.expressions(expression, flat=True)}" if expression.expressions else ""
    1666        )
    1667        tag = " TAG" if expression.args.get("tag") else ""
    1668        return f"{'UNSET' if expression.args.get('unset') else 'SET'}{tag}{expressions}"

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

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

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

    1673    def lock_sql(self, expression: exp.Lock) -> str:
    1674        if not self.LOCKING_READS_SUPPORTED:
    1675            self.unsupported("Locking reads using 'FOR UPDATE/SHARE' are not supported")
    1676            return ""
    1677
    1678        lock_type = "FOR UPDATE" if expression.args["update"] else "FOR SHARE"
    1679        expressions = self.expressions(expression, flat=True)
    1680        expressions = f" OF {expressions}" if expressions else ""
    1681        wait = expression.args.get("wait")
    1682
    1683        if wait is not None:
    1684            if isinstance(wait, exp.Literal):
    1685                wait = f" WAIT {self.sql(wait)}"
    1686            else:
    1687                wait = " NOWAIT" if wait else " SKIP LOCKED"
    1688
    1689        return f"{lock_type}{expressions}{wait or ''}"

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

    1691    def literal_sql(self, expression: exp.Literal) -> str:
    1692        text = expression.this or ""
    1693        if expression.is_string:
    1694            text = f"{self.QUOTE_START}{self.escape_str(text)}{self.QUOTE_END}"
    1695        return text

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

    1697    def escape_str(self, text: str) -> str:
    1698        text = text.replace(self.QUOTE_END, self._escaped_quote_end)
    1699        if self.INVERSE_ESCAPE_SEQUENCES:
    1700            text = "".join(self.INVERSE_ESCAPE_SEQUENCES.get(ch, ch) for ch in text)
    1701        elif self.pretty:
    1702            text = text.replace("\n", self.SENTINEL_LINE_BREAK)
    1703        return text

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

    1705    def loaddata_sql(self, expression: exp.LoadData) -> str:
    1706        local = " LOCAL" if expression.args.get("local") else ""
    1707        inpath = f" INPATH {self.sql(expression, 'inpath')}"
    1708        overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
    1709        this = f" INTO TABLE {self.sql(expression, 'this')}"
    1710        partition = self.sql(expression, "partition")
    1711        partition = f" {partition}" if partition else ""
    1712        input_format = self.sql(expression, "input_format")
    1713        input_format = f" INPUTFORMAT {input_format}" if input_format else ""
    1714        serde = self.sql(expression, "serde")
    1715        serde = f" SERDE {serde}" if serde else ""
    1716        return f"LOAD DATA{local}{inpath}{overwrite}{this}{partition}{input_format}{serde}"

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

    1718    def null_sql(self, *_) -> str:
    1719        return "NULL"

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

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

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

    1724    def order_sql(self, expression: exp.Order, flat: bool = False) -> str:
    1725        this = self.sql(expression, "this")
    1726        this = f"{this} " if this else this
    1727        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

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

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

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

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

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

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

    1738    def ordered_sql(self, expression: exp.Ordered) -> str:
    1739        desc = expression.args.get("desc")
    1740        asc = not desc
    1741
    1742        nulls_first = expression.args.get("nulls_first")
    1743        nulls_last = not nulls_first
    1744        nulls_are_large = self.NULL_ORDERING == "nulls_are_large"
    1745        nulls_are_small = self.NULL_ORDERING == "nulls_are_small"
    1746        nulls_are_last = self.NULL_ORDERING == "nulls_are_last"
    1747
    1748        sort_order = " DESC" if desc else (" ASC" if desc is False else "")
    1749        nulls_sort_change = ""
    1750        if nulls_first and (
    1751            (asc and nulls_are_large) or (desc and nulls_are_small) or nulls_are_last
    1752        ):
    1753            nulls_sort_change = " NULLS FIRST"
    1754        elif (
    1755            nulls_last
    1756            and ((asc and nulls_are_small) or (desc and nulls_are_large))
    1757            and not nulls_are_last
    1758        ):
    1759            nulls_sort_change = " NULLS LAST"
    1760
    1761        if nulls_sort_change and not self.NULL_ORDERING_SUPPORTED:
    1762            self.unsupported(
    1763                "Sorting in an ORDER BY on NULLS FIRST/NULLS LAST is not supported by this dialect"
    1764            )
    1765            nulls_sort_change = ""
    1766
    1767        return f"{self.sql(expression, 'this')}{sort_order}{nulls_sort_change}"

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

    1769    def matchrecognize_sql(self, expression: exp.MatchRecognize) -> str:
    1770        partition = self.partition_by_sql(expression)
    1771        order = self.sql(expression, "order")
    1772        measures = self.expressions(expression, key="measures")
    1773        measures = self.seg(f"MEASURES{self.seg(measures)}") if measures else ""
    1774        rows = self.sql(expression, "rows")
    1775        rows = self.seg(rows) if rows else ""
    1776        after = self.sql(expression, "after")
    1777        after = self.seg(after) if after else ""
    1778        pattern = self.sql(expression, "pattern")
    1779        pattern = self.seg(f"PATTERN ({pattern})") if pattern else ""
    1780        definition_sqls = [
    1781            f"{self.sql(definition, 'alias')} AS {self.sql(definition, 'this')}"
    1782            for definition in expression.args.get("define", [])
    1783        ]
    1784        definitions = self.expressions(sqls=definition_sqls)
    1785        define = self.seg(f"DEFINE{self.seg(definitions)}") if definitions else ""
    1786        body = "".join(
    1787            (
    1788                partition,
    1789                order,
    1790                measures,
    1791                rows,
    1792                after,
    1793                pattern,
    1794                define,
    1795            )
    1796        )
    1797        alias = self.sql(expression, "alias")
    1798        alias = f" {alias}" if alias else ""
    1799        return f"{self.seg('MATCH_RECOGNIZE')} {self.wrap(body)}{alias}"

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

    1801    def query_modifiers(self, expression: exp.Expression, *sqls: str) -> str:
    1802        limit: t.Optional[exp.Fetch | exp.Limit] = expression.args.get("limit")
    1803
    1804        # If the limit is generated as TOP, we need to ensure it's not generated twice
    1805        with_offset_limit_modifiers = not isinstance(limit, exp.Limit) or not self.LIMIT_IS_TOP
    1806
    1807        if self.LIMIT_FETCH == "LIMIT" and isinstance(limit, exp.Fetch):
    1808            limit = exp.Limit(expression=exp.maybe_copy(limit.args.get("count")))
    1809        elif self.LIMIT_FETCH == "FETCH" and isinstance(limit, exp.Limit):
    1810            limit = exp.Fetch(direction="FIRST", count=exp.maybe_copy(limit.expression))
    1811
    1812        fetch = isinstance(limit, exp.Fetch)
    1813
    1814        offset_limit_modifiers = (
    1815            self.offset_limit_modifiers(expression, fetch, limit)
    1816            if with_offset_limit_modifiers
    1817            else []
    1818        )
    1819
    1820        return csv(
    1821            *sqls,
    1822            *[self.sql(join) for join in expression.args.get("joins") or []],
    1823            self.sql(expression, "connect"),
    1824            self.sql(expression, "match"),
    1825            *[self.sql(lateral) for lateral in expression.args.get("laterals") or []],
    1826            self.sql(expression, "where"),
    1827            self.sql(expression, "group"),
    1828            self.sql(expression, "having"),
    1829            *self.after_having_modifiers(expression),
    1830            self.sql(expression, "order"),
    1831            *offset_limit_modifiers,
    1832            *self.after_limit_modifiers(expression),
    1833            sep="",
    1834        )

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

    1836    def offset_limit_modifiers(
    1837        self, expression: exp.Expression, fetch: bool, limit: t.Optional[exp.Fetch | exp.Limit]
    1838    ) -> t.List[str]:
    1839        return [
    1840            self.sql(expression, "offset") if fetch else self.sql(limit),
    1841            self.sql(limit) if fetch else self.sql(expression, "offset"),
    1842        ]

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

    1844    def after_having_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1845        return [
    1846            self.sql(expression, "qualify"),
    1847            self.seg("WINDOW ") + self.expressions(expression, key="windows", flat=True)
    1848            if expression.args.get("windows")
    1849            else "",
    1850            self.sql(expression, "distribute"),
    1851            self.sql(expression, "sort"),
    1852            self.sql(expression, "cluster"),
    1853        ]

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

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

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

    1860    def select_sql(self, expression: exp.Select) -> str:
    1861        hint = self.sql(expression, "hint")
    1862        distinct = self.sql(expression, "distinct")
    1863        distinct = f" {distinct}" if distinct else ""
    1864        kind = self.sql(expression, "kind").upper()
    1865        limit = expression.args.get("limit")
    1866        top = (
    1867            self.limit_sql(limit, top=True)
    1868            if isinstance(limit, exp.Limit) and self.LIMIT_IS_TOP
    1869            else ""
    1870        )
    1871
    1872        expressions = self.expressions(expression)
    1873
    1874        if kind:
    1875            if kind in self.SELECT_KINDS:
    1876                kind = f" AS {kind}"
    1877            else:
    1878                if kind == "STRUCT":
    1879                    expressions = self.expressions(
    1880                        sqls=[
    1881                            self.sql(
    1882                                exp.Struct(
    1883                                    expressions=[
    1884                                        exp.column(e.output_name).eq(
    1885                                            e.this if isinstance(e, exp.Alias) else e
    1886                                        )
    1887                                        for e in expression.expressions
    1888                                    ]
    1889                                )
    1890                            )
    1891                        ]
    1892                    )
    1893                kind = ""
    1894
    1895        expressions = f"{self.sep()}{expressions}" if expressions else expressions
    1896        sql = self.query_modifiers(
    1897            expression,
    1898            f"SELECT{top}{hint}{distinct}{kind}{expressions}",
    1899            self.sql(expression, "into", comment=False),
    1900            self.sql(expression, "from", comment=False),
    1901        )
    1902        return self.prepend_ctes(expression, sql)

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

    1904    def schema_sql(self, expression: exp.Schema) -> str:
    1905        this = self.sql(expression, "this")
    1906        sql = self.schema_columns_sql(expression)
    1907        return f"{this} {sql}" if this and sql else this or sql

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

    1909    def schema_columns_sql(self, expression: exp.Schema) -> str:
    1910        if expression.expressions:
    1911            return f"({self.sep('')}{self.expressions(expression)}{self.seg(')', sep='')}"
    1912        return ""

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

    1914    def star_sql(self, expression: exp.Star) -> str:
    1915        except_ = self.expressions(expression, key="except", flat=True)
    1916        except_ = f"{self.seg(self.STAR_MAPPING['except'])} ({except_})" if except_ else ""
    1917        replace = self.expressions(expression, key="replace", flat=True)
    1918        replace = f"{self.seg(self.STAR_MAPPING['replace'])} ({replace})" if replace else ""
    1919        return f"*{except_}{replace}"

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

    1921    def parameter_sql(self, expression: exp.Parameter) -> str:
    1922        this = self.sql(expression, "this")
    1923        return f"{self.PARAMETER_TOKEN}{this}" if self.SUPPORTS_PARAMETERS else this

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

    1925    def sessionparameter_sql(self, expression: exp.SessionParameter) -> str:
    1926        this = self.sql(expression, "this")
    1927        kind = expression.text("kind")
    1928        if kind:
    1929            kind = f"{kind}."
    1930        return f"@@{kind}{this}"

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

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

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

    1935    def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str:
    1936        alias = self.sql(expression, "alias")
    1937        alias = f"{sep}{alias}" if alias else ""
    1938
    1939        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1940        pivots = f" {pivots}" if pivots else ""
    1941
    1942        sql = self.query_modifiers(expression, self.wrap(expression), alias, pivots)
    1943        return self.prepend_ctes(expression, sql)

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

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

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

    1949    def union_sql(self, expression: exp.Union) -> str:
    1950        return self.prepend_ctes(
    1951            expression,
    1952            self.set_operation(expression, self.union_op(expression)),
    1953        )

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

    1955    def union_op(self, expression: exp.Union) -> str:
    1956        kind = " DISTINCT" if self.EXPLICIT_UNION else ""
    1957        kind = kind if expression.args.get("distinct") else " ALL"
    1958        by_name = " BY NAME" if expression.args.get("by_name") else ""
    1959        return f"UNION{kind}{by_name}"

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

    1961    def unnest_sql(self, expression: exp.Unnest) -> str:
    1962        args = self.expressions(expression, flat=True)
    1963
    1964        alias = expression.args.get("alias")
    1965        offset = expression.args.get("offset")
    1966
    1967        if self.UNNEST_WITH_ORDINALITY:
    1968            if alias and isinstance(offset, exp.Expression):
    1969                alias = alias.copy()
    1970                alias.append("columns", offset.copy())
    1971
    1972        if alias and self.UNNEST_COLUMN_ONLY:
    1973            columns = alias.columns
    1974            alias = self.sql(columns[0]) if columns else ""
    1975        else:
    1976            alias = self.sql(alias)
    1977
    1978        alias = f" AS {alias}" if alias else alias
    1979        if self.UNNEST_WITH_ORDINALITY:
    1980            suffix = f" WITH ORDINALITY{alias}" if offset else alias
    1981        else:
    1982            if isinstance(offset, exp.Expression):
    1983                suffix = f"{alias} WITH OFFSET AS {self.sql(offset)}"
    1984            elif offset:
    1985                suffix = f"{alias} WITH OFFSET"
    1986            else:
    1987                suffix = alias
    1988
    1989        return f"UNNEST({args}){suffix}"

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

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

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

    1995    def window_sql(self, expression: exp.Window) -> str:
    1996        this = self.sql(expression, "this")
    1997        partition = self.partition_by_sql(expression)
    1998        order = expression.args.get("order")
    1999        order = self.order_sql(order, flat=True) if order else ""
    2000        spec = self.sql(expression, "spec")
    2001        alias = self.sql(expression, "alias")
    2002        over = self.sql(expression, "over") or "OVER"
    2003
    2004        this = f"{this} {'AS' if expression.arg_key == 'windows' else over}"
    2005
    2006        first = expression.args.get("first")
    2007        if first is None:
    2008            first = ""
    2009        else:
    2010            first = "FIRST" if first else "LAST"
    2011
    2012        if not partition and not order and not spec and alias:
    2013            return f"{this} {alias}"
    2014
    2015        args = " ".join(arg for arg in (alias, first, partition, order, spec) if arg)
    2016        return f"{this} ({args})"

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

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

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

    2022    def windowspec_sql(self, expression: exp.WindowSpec) -> str:
    2023        kind = self.sql(expression, "kind")
    2024        start = csv(self.sql(expression, "start"), self.sql(expression, "start_side"), sep=" ")
    2025        end = (
    2026            csv(self.sql(expression, "end"), self.sql(expression, "end_side"), sep=" ")
    2027            or "CURRENT ROW"
    2028        )
    2029        return f"{kind} BETWEEN {start} AND {end}"

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

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

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

    2036    def between_sql(self, expression: exp.Between) -> str:
    2037        this = self.sql(expression, "this")
    2038        low = self.sql(expression, "low")
    2039        high = self.sql(expression, "high")
    2040        return f"{this} BETWEEN {low} AND {high}"

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

    2042    def bracket_sql(self, expression: exp.Bracket) -> str:
    2043        expressions = apply_index_offset(expression.this, expression.expressions, self.INDEX_OFFSET)
    2044        expressions_sql = ", ".join(self.sql(e) for e in expressions)
    2045
    2046        return f"{self.sql(expression, 'this')}[{expressions_sql}]"

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

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

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

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

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

    2054    def any_sql(self, expression: exp.Any) -> str:
    2055        this = self.sql(expression, "this")
    2056        if isinstance(expression.this, exp.Subqueryable):
    2057            this = self.wrap(this)
    2058        return f"ANY {this}"

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

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

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

    2063    def case_sql(self, expression: exp.Case) -> str:
    2064        this = self.sql(expression, "this")
    2065        statements = [f"CASE {this}" if this else "CASE"]
    2066
    2067        for e in expression.args["ifs"]:
    2068            statements.append(f"WHEN {self.sql(e, 'this')}")
    2069            statements.append(f"THEN {self.sql(e, 'true')}")
    2070
    2071        default = self.sql(expression, "default")
    2072
    2073        if default:
    2074            statements.append(f"ELSE {default}")
    2075
    2076        statements.append("END")
    2077
    2078        if self.pretty and self.text_width(statements) > self.max_text_width:
    2079            return self.indent("\n".join(statements), skip_first=True, skip_last=True)
    2080
    2081        return " ".join(statements)

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

    2083    def constraint_sql(self, expression: exp.Constraint) -> str:
    2084        this = self.sql(expression, "this")
    2085        expressions = self.expressions(expression, flat=True)
    2086        return f"CONSTRAINT {this} {expressions}"

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

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

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

    2093    def extract_sql(self, expression: exp.Extract) -> str:
    2094        this = self.sql(expression, "this") if self.EXTRACT_ALLOWS_QUOTES else expression.this.name
    2095        expression_sql = self.sql(expression, "expression")
    2096        return f"EXTRACT({this} FROM {expression_sql})"

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

    2098    def trim_sql(self, expression: exp.Trim) -> str:
    2099        trim_type = self.sql(expression, "position")
    2100
    2101        if trim_type == "LEADING":
    2102            return self.func("LTRIM", expression.this)
    2103        elif trim_type == "TRAILING":
    2104            return self.func("RTRIM", expression.this)
    2105        else:
    2106            return self.func("TRIM", expression.this, expression.expression)

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

    2108    def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    2109        expressions = expression.expressions
    2110        if self.STRICT_STRING_CONCAT:
    2111            expressions = (exp.cast(e, "text") for e in expressions)
    2112        return self.func("CONCAT", *expressions)

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

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

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

    2118    def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
    2119        expressions = self.expressions(expression, flat=True)
    2120        reference = self.sql(expression, "reference")
    2121        reference = f" {reference}" if reference else ""
    2122        delete = self.sql(expression, "delete")
    2123        delete = f" ON DELETE {delete}" if delete else ""
    2124        update = self.sql(expression, "update")
    2125        update = f" ON UPDATE {update}" if update else ""
    2126        return f"FOREIGN KEY ({expressions}){reference}{delete}{update}"

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

    2128    def primarykey_sql(self, expression: exp.ForeignKey) -> str:
    2129        expressions = self.expressions(expression, flat=True)
    2130        options = self.expressions(expression, key="options", flat=True, sep=" ")
    2131        options = f" {options}" if options else ""
    2132        return f"PRIMARY KEY ({expressions}){options}"

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

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

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

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

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

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

def formatjson_sql(self, expression: sqlglot.expressions.FormatJson) -> str: View Source

    2146    def formatjson_sql(self, expression: exp.FormatJson) -> str:
    2147        return f"{self.sql(expression, 'this')} FORMAT JSON"

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

    2149    def jsonobject_sql(self, expression: exp.JSONObject) -> str:
    2150        null_handling = expression.args.get("null_handling")
    2151        null_handling = f" {null_handling}" if null_handling else ""
    2152        unique_keys = expression.args.get("unique_keys")
    2153        if unique_keys is not None:
    2154            unique_keys = f" {'WITH' if unique_keys else 'WITHOUT'} UNIQUE KEYS"
    2155        else:
    2156            unique_keys = ""
    2157        return_type = self.sql(expression, "return_type")
    2158        return_type = f" RETURNING {return_type}" if return_type else ""
    2159        encoding = self.sql(expression, "encoding")
    2160        encoding = f" ENCODING {encoding}" if encoding else ""
    2161        return self.func(
    2162            "JSON_OBJECT",
    2163            *expression.expressions,
    2164            suffix=f"{null_handling}{unique_keys}{return_type}{encoding})",
    2165        )

def jsonarray_sql(self, expression: sqlglot.expressions.JSONArray) -> str: View Source

    2167    def jsonarray_sql(self, expression: exp.JSONArray) -> str:
    2168        null_handling = expression.args.get("null_handling")
    2169        null_handling = f" {null_handling}" if null_handling else ""
    2170        return_type = self.sql(expression, "return_type")
    2171        return_type = f" RETURNING {return_type}" if return_type else ""
    2172        strict = " STRICT" if expression.args.get("strict") else ""
    2173        return self.func(
    2174            "JSON_ARRAY", *expression.expressions, suffix=f"{null_handling}{return_type}{strict})"
    2175        )

def jsonarrayagg_sql(self, expression: sqlglot.expressions.JSONArrayAgg) -> str: View Source

    2177    def jsonarrayagg_sql(self, expression: exp.JSONArrayAgg) -> str:
    2178        this = self.sql(expression, "this")
    2179        order = self.sql(expression, "order")
    2180        null_handling = expression.args.get("null_handling")
    2181        null_handling = f" {null_handling}" if null_handling else ""
    2182        return_type = self.sql(expression, "return_type")
    2183        return_type = f" RETURNING {return_type}" if return_type else ""
    2184        strict = " STRICT" if expression.args.get("strict") else ""
    2185        return self.func(
    2186            "JSON_ARRAYAGG",
    2187            this,
    2188            suffix=f"{order}{null_handling}{return_type}{strict})",
    2189        )

def jsoncolumndef_sql(self, expression: sqlglot.expressions.JSONColumnDef) -> str: View Source

    2191    def jsoncolumndef_sql(self, expression: exp.JSONColumnDef) -> str:
    2192        path = self.sql(expression, "path")
    2193        path = f" PATH {path}" if path else ""
    2194        nested_schema = self.sql(expression, "nested_schema")
    2195
    2196        if nested_schema:
    2197            return f"NESTED{path} {nested_schema}"
    2198
    2199        this = self.sql(expression, "this")
    2200        kind = self.sql(expression, "kind")
    2201        kind = f" {kind}" if kind else ""
    2202        return f"{this}{kind}{path}"

def jsonschema_sql(self, expression: sqlglot.expressions.JSONSchema) -> str: View Source

    2204    def jsonschema_sql(self, expression: exp.JSONSchema) -> str:
    2205        return self.func("COLUMNS", *expression.expressions)

def jsontable_sql(self, expression: sqlglot.expressions.JSONTable) -> str: View Source

    2207    def jsontable_sql(self, expression: exp.JSONTable) -> str:
    2208        this = self.sql(expression, "this")
    2209        path = self.sql(expression, "path")
    2210        path = f", {path}" if path else ""
    2211        error_handling = expression.args.get("error_handling")
    2212        error_handling = f" {error_handling}" if error_handling else ""
    2213        empty_handling = expression.args.get("empty_handling")
    2214        empty_handling = f" {empty_handling}" if empty_handling else ""
    2215        schema = self.sql(expression, "schema")
    2216        return self.func(
    2217            "JSON_TABLE", this, suffix=f"{path}{error_handling}{empty_handling} {schema})"
    2218        )

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

    2220    def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
    2221        this = self.sql(expression, "this")
    2222        kind = self.sql(expression, "kind")
    2223        path = self.sql(expression, "path")
    2224        path = f" {path}" if path else ""
    2225        as_json = " AS JSON" if expression.args.get("as_json") else ""
    2226        return f"{this} {kind}{path}{as_json}"

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

    2228    def openjson_sql(self, expression: exp.OpenJSON) -> str:
    2229        this = self.sql(expression, "this")
    2230        path = self.sql(expression, "path")
    2231        path = f", {path}" if path else ""
    2232        expressions = self.expressions(expression)
    2233        with_ = (
    2234            f" WITH ({self.seg(self.indent(expressions), sep='')}{self.seg(')', sep='')}"
    2235            if expressions
    2236            else ""
    2237        )
    2238        return f"OPENJSON({this}{path}){with_}"

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

    2240    def in_sql(self, expression: exp.In) -> str:
    2241        query = expression.args.get("query")
    2242        unnest = expression.args.get("unnest")
    2243        field = expression.args.get("field")
    2244        is_global = " GLOBAL" if expression.args.get("is_global") else ""
    2245
    2246        if query:
    2247            in_sql = self.wrap(query)
    2248        elif unnest:
    2249            in_sql = self.in_unnest_op(unnest)
    2250        elif field:
    2251            in_sql = self.sql(field)
    2252        else:
    2253            in_sql = f"({self.expressions(expression, flat=True)})"
    2254
    2255        return f"{self.sql(expression, 'this')}{is_global} IN {in_sql}"

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

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

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

    2260    def interval_sql(self, expression: exp.Interval) -> str:
    2261        unit = self.sql(expression, "unit")
    2262        if not self.INTERVAL_ALLOWS_PLURAL_FORM:
    2263            unit = self.TIME_PART_SINGULARS.get(unit.lower(), unit)
    2264        unit = f" {unit}" if unit else ""
    2265
    2266        if self.SINGLE_STRING_INTERVAL:
    2267            this = expression.this.name if expression.this else ""
    2268            return f"INTERVAL '{this}{unit}'" if this else f"INTERVAL{unit}"
    2269
    2270        this = self.sql(expression, "this")
    2271        if this:
    2272            unwrapped = isinstance(expression.this, self.UNWRAPPED_INTERVAL_VALUES)
    2273            this = f" {this}" if unwrapped else f" ({this})"
    2274
    2275        return f"INTERVAL{this}{unit}"

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

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

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

    2280    def reference_sql(self, expression: exp.Reference) -> str:
    2281        this = self.sql(expression, "this")
    2282        expressions = self.expressions(expression, flat=True)
    2283        expressions = f"({expressions})" if expressions else ""
    2284        options = self.expressions(expression, key="options", flat=True, sep=" ")
    2285        options = f" {options}" if options else ""
    2286        return f"REFERENCES {this}{expressions}{options}"

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

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

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

    2291    def paren_sql(self, expression: exp.Paren) -> str:
    2292        if isinstance(expression.unnest(), exp.Select):
    2293            sql = self.wrap(expression)
    2294        else:
    2295            sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
    2296            sql = f"({sql}{self.seg(')', sep='')}"
    2297
    2298        return self.prepend_ctes(expression, sql)

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

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

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

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

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

    2309    def alias_sql(self, expression: exp.Alias) -> str:
    2310        alias = self.sql(expression, "alias")
    2311        alias = f" AS {alias}" if alias else ""
    2312        return f"{self.sql(expression, 'this')}{alias}"

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

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

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

    2317    def attimezone_sql(self, expression: exp.AtTimeZone) -> str:
    2318        this = self.sql(expression, "this")
    2319        zone = self.sql(expression, "zone")
    2320        return f"{this} AT TIME ZONE {zone}"

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

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

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

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

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

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

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

    2331    def connector_sql(self, expression: exp.Connector, op: str) -> str:
    2332        if not self.pretty:
    2333            return self.binary(expression, op)
    2334
    2335        sqls = tuple(
    2336            self.maybe_comment(self.sql(e), e, e.parent.comments or []) if i != 1 else self.sql(e)
    2337            for i, e in enumerate(expression.flatten(unnest=False))
    2338        )
    2339
    2340        sep = "\n" if self.text_width(sqls) > self.max_text_width else " "
    2341        return f"{sep}{op} ".join(sqls)

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

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

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

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

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

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

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

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

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

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

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

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

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

    2361    def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) -> str:
    2362        format_sql = self.sql(expression, "format")
    2363        format_sql = f" FORMAT {format_sql}" if format_sql else ""
    2364        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

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

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

    2370    def collate_sql(self, expression: exp.Collate) -> str:
    2371        if self.COLLATE_IS_FUNC:
    2372            return self.function_fallback_sql(expression)
    2373        return self.binary(expression, "COLLATE")

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

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

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

    2378    def comment_sql(self, expression: exp.Comment) -> str:
    2379        this = self.sql(expression, "this")
    2380        kind = expression.args["kind"]
    2381        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    2382        expression_sql = self.sql(expression, "expression")
    2383        return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"

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

    2385    def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
    2386        this = self.sql(expression, "this")
    2387        delete = " DELETE" if expression.args.get("delete") else ""
    2388        recompress = self.sql(expression, "recompress")
    2389        recompress = f" RECOMPRESS {recompress}" if recompress else ""
    2390        to_disk = self.sql(expression, "to_disk")
    2391        to_disk = f" TO DISK {to_disk}" if to_disk else ""
    2392        to_volume = self.sql(expression, "to_volume")
    2393        to_volume = f" TO VOLUME {to_volume}" if to_volume else ""
    2394        return f"{this}{delete}{recompress}{to_disk}{to_volume}"

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

    2396    def mergetreettl_sql(self, expression: exp.MergeTreeTTL) -> str:
    2397        where = self.sql(expression, "where")
    2398        group = self.sql(expression, "group")
    2399        aggregates = self.expressions(expression, key="aggregates")
    2400        aggregates = self.seg("SET") + self.seg(aggregates) if aggregates else ""
    2401
    2402        if not (where or group or aggregates) and len(expression.expressions) == 1:
    2403            return f"TTL {self.expressions(expression, flat=True)}"
    2404
    2405        return f"TTL{self.seg(self.expressions(expression))}{where}{group}{aggregates}"

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

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

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

    2410    def commit_sql(self, expression: exp.Commit) -> str:
    2411        chain = expression.args.get("chain")
    2412        if chain is not None:
    2413            chain = " AND CHAIN" if chain else " AND NO CHAIN"
    2414
    2415        return f"COMMIT{chain or ''}"

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

    2417    def rollback_sql(self, expression: exp.Rollback) -> str:
    2418        savepoint = expression.args.get("savepoint")
    2419        savepoint = f" TO {savepoint}" if savepoint else ""
    2420        return f"ROLLBACK{savepoint}"

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

    2422    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
    2423        this = self.sql(expression, "this")
    2424
    2425        dtype = self.sql(expression, "dtype")
    2426        if dtype:
    2427            collate = self.sql(expression, "collate")
    2428            collate = f" COLLATE {collate}" if collate else ""
    2429            using = self.sql(expression, "using")
    2430            using = f" USING {using}" if using else ""
    2431            return f"ALTER COLUMN {this} SET DATA TYPE {dtype}{collate}{using}"
    2432
    2433        default = self.sql(expression, "default")
    2434        if default:
    2435            return f"ALTER COLUMN {this} SET DEFAULT {default}"
    2436
    2437        if not expression.args.get("drop"):
    2438            self.unsupported("Unsupported ALTER COLUMN syntax")
    2439
    2440        return f"ALTER COLUMN {this} DROP DEFAULT"

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

    2442    def renametable_sql(self, expression: exp.RenameTable) -> str:
    2443        if not self.RENAME_TABLE_WITH_DB:
    2444            # Remove db from tables
    2445            expression = expression.transform(
    2446                lambda n: exp.table_(n.this) if isinstance(n, exp.Table) else n
    2447            )
    2448        this = self.sql(expression, "this")
    2449        return f"RENAME TO {this}"

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

    2451    def altertable_sql(self, expression: exp.AlterTable) -> str:
    2452        actions = expression.args["actions"]
    2453
    2454        if isinstance(actions[0], exp.ColumnDef):
    2455            if self.ALTER_TABLE_ADD_COLUMN_KEYWORD:
    2456                actions = self.expressions(
    2457                    expression,
    2458                    key="actions",
    2459                    prefix="ADD COLUMN ",
    2460                )
    2461            else:
    2462                actions = f"ADD {self.expressions(expression, key='actions')}"
    2463        elif isinstance(actions[0], exp.Schema):
    2464            actions = self.expressions(expression, key="actions", prefix="ADD COLUMNS ")
    2465        elif isinstance(actions[0], exp.Delete):
    2466            actions = self.expressions(expression, key="actions", flat=True)
    2467        else:
    2468            actions = self.expressions(expression, key="actions", flat=True)
    2469
    2470        exists = " IF EXISTS" if expression.args.get("exists") else ""
    2471        only = " ONLY" if expression.args.get("only") else ""
    2472        return f"ALTER TABLE{exists}{only} {self.sql(expression, 'this')} {actions}"

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

    2474    def droppartition_sql(self, expression: exp.DropPartition) -> str:
    2475        expressions = self.expressions(expression)
    2476        exists = " IF EXISTS " if expression.args.get("exists") else " "
    2477        return f"DROP{exists}{expressions}"

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

    2479    def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
    2480        this = self.sql(expression, "this")
    2481        expression_ = self.sql(expression, "expression")
    2482        add_constraint = f"ADD CONSTRAINT {this}" if this else "ADD"
    2483
    2484        enforced = expression.args.get("enforced")
    2485        if enforced is not None:
    2486            return f"{add_constraint} CHECK ({expression_}){' ENFORCED' if enforced else ''}"
    2487
    2488        return f"{add_constraint} {expression_}"

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

    2490    def distinct_sql(self, expression: exp.Distinct) -> str:
    2491        this = self.expressions(expression, flat=True)
    2492        this = f" {this}" if this else ""
    2493
    2494        on = self.sql(expression, "on")
    2495        on = f" ON {on}" if on else ""
    2496        return f"DISTINCT{this}{on}"

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

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

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

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

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

    2504    def intdiv_sql(self, expression: exp.IntDiv) -> str:
    2505        return self.sql(
    2506            exp.Cast(
    2507                this=exp.Div(this=expression.this.copy(), expression=expression.expression.copy()),
    2508                to=exp.DataType(this=exp.DataType.Type.INT),
    2509            )
    2510        )

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

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

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

    2515    def safedpipe_sql(self, expression: exp.SafeDPipe) -> str:
    2516        if self.STRICT_STRING_CONCAT:
    2517            return self.func("CONCAT", *(exp.cast(e, "text") for e in expression.flatten()))
    2518        return self.dpipe_sql(expression)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    2553    def is_sql(self, expression: exp.Is) -> str:
    2554        if not self.IS_BOOL_ALLOWED and isinstance(expression.expression, exp.Boolean):
    2555            return self.sql(
    2556                expression.this if expression.expression.this else exp.not_(expression.this)
    2557            )
    2558        return self.binary(expression, "IS")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

def log_sql(self, expression: sqlglot.expressions.Log) -> str: View Source

    2602    def log_sql(self, expression: exp.Log) -> str:
    2603        args = list(expression.args.values())
    2604        if not self.LOG_BASE_FIRST:
    2605            args.reverse()
    2606        return self.func("LOG", *args)

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

    2608    def use_sql(self, expression: exp.Use) -> str:
    2609        kind = self.sql(expression, "kind")
    2610        kind = f" {kind}" if kind else ""
    2611        this = self.sql(expression, "this")
    2612        this = f" {this}" if this else ""
    2613        return f"USE{kind}{this}"

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

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

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

    2619    def function_fallback_sql(self, expression: exp.Func) -> str:
    2620        args = []
    2621
    2622        for key in expression.arg_types:
    2623            arg_value = expression.args.get(key)
    2624
    2625            if isinstance(arg_value, list):
    2626                for value in arg_value:
    2627                    args.append(value)
    2628            elif arg_value is not None:
    2629                args.append(arg_value)
    2630
    2631        if self.normalize_functions:
    2632            name = expression.sql_name()
    2633        else:
    2634            name = (expression._meta and expression.meta.get("name")) or expression.sql_name()
    2635
    2636        return self.func(name, *args)

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

    2638    def func(
    2639        self,
    2640        name: str,
    2641        *args: t.Optional[exp.Expression | str],
    2642        prefix: str = "(",
    2643        suffix: str = ")",
    2644    ) -> str:
    2645        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

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

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

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

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

    2656    def format_time(self, expression: exp.Expression) -> t.Optional[str]:
    2657        return format_time(
    2658            self.sql(expression, "format"), self.INVERSE_TIME_MAPPING, self.INVERSE_TIME_TRIE
    2659        )

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

    2661    def expressions(
    2662        self,
    2663        expression: t.Optional[exp.Expression] = None,
    2664        key: t.Optional[str] = None,
    2665        sqls: t.Optional[t.Collection[str | exp.Expression]] = None,
    2666        flat: bool = False,
    2667        indent: bool = True,
    2668        skip_first: bool = False,
    2669        sep: str = ", ",
    2670        prefix: str = "",
    2671    ) -> str:
    2672        expressions = expression.args.get(key or "expressions") if expression else sqls
    2673
    2674        if not expressions:
    2675            return ""
    2676
    2677        if flat:
    2678            return sep.join(sql for sql in (self.sql(e) for e in expressions) if sql)
    2679
    2680        num_sqls = len(expressions)
    2681
    2682        # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
    2683        pad = " " * self.pad
    2684        stripped_sep = sep.strip()
    2685
    2686        result_sqls = []
    2687        for i, e in enumerate(expressions):
    2688            sql = self.sql(e, comment=False)
    2689            if not sql:
    2690                continue
    2691
    2692            comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
    2693
    2694            if self.pretty:
    2695                if self.leading_comma:
    2696                    result_sqls.append(f"{sep if i > 0 else pad}{prefix}{sql}{comments}")
    2697                else:
    2698                    result_sqls.append(
    2699                        f"{prefix}{sql}{stripped_sep if i + 1 < num_sqls else ''}{comments}"
    2700                    )
    2701            else:
    2702                result_sqls.append(f"{prefix}{sql}{comments}{sep if i + 1 < num_sqls else ''}")
    2703
    2704        result_sql = "\n".join(result_sqls) if self.pretty else "".join(result_sqls)
    2705        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

    2707    def op_expressions(self, op: str, expression: exp.Expression, flat: bool = False) -> str:
    2708        flat = flat or isinstance(expression.parent, exp.Properties)
    2709        expressions_sql = self.expressions(expression, flat=flat)
    2710        if flat:
    2711            return f"{op} {expressions_sql}"
    2712        return f"{self.seg(op)}{self.sep() if expressions_sql else ''}{expressions_sql}"

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

    2714    def naked_property(self, expression: exp.Property) -> str:
    2715        property_name = exp.Properties.PROPERTY_TO_NAME.get(expression.__class__)
    2716        if not property_name:
    2717            self.unsupported(f"Unsupported property {expression.__class__.__name__}")
    2718        return f"{property_name} {self.sql(expression, 'this')}"

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

    2720    def set_operation(self, expression: exp.Expression, op: str) -> str:
    2721        this = self.sql(expression, "this")
    2722        op = self.seg(op)
    2723        return self.query_modifiers(
    2724            expression, f"{this}{op}{self.sep()}{self.sql(expression, 'expression')}"
    2725        )

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

    2727    def tag_sql(self, expression: exp.Tag) -> str:
    2728        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

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

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

    2733    def userdefinedfunction_sql(self, expression: exp.UserDefinedFunction) -> str:
    2734        this = self.sql(expression, "this")
    2735        expressions = self.no_identify(self.expressions, expression)
    2736        expressions = (
    2737            self.wrap(expressions) if expression.args.get("wrapped") else f" {expressions}"
    2738        )
    2739        return f"{this}{expressions}"

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

    2741    def joinhint_sql(self, expression: exp.JoinHint) -> str:
    2742        this = self.sql(expression, "this")
    2743        expressions = self.expressions(expression, flat=True)
    2744        return f"{this}({expressions})"

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

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

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

    2749    def when_sql(self, expression: exp.When) -> str:
    2750        matched = "MATCHED" if expression.args["matched"] else "NOT MATCHED"
    2751        source = " BY SOURCE" if self.MATCHED_BY_SOURCE and expression.args.get("source") else ""
    2752        condition = self.sql(expression, "condition")
    2753        condition = f" AND {condition}" if condition else ""
    2754
    2755        then_expression = expression.args.get("then")
    2756        if isinstance(then_expression, exp.Insert):
    2757            then = f"INSERT {self.sql(then_expression, 'this')}"
    2758            if "expression" in then_expression.args:
    2759                then += f" VALUES {self.sql(then_expression, 'expression')}"
    2760        elif isinstance(then_expression, exp.Update):
    2761            if isinstance(then_expression.args.get("expressions"), exp.Star):
    2762                then = f"UPDATE {self.sql(then_expression, 'expressions')}"
    2763            else:
    2764                then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
    2765        else:
    2766            then = self.sql(then_expression)
    2767        return f"WHEN {matched}{source}{condition} THEN {then}"

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

    2769    def merge_sql(self, expression: exp.Merge) -> str:
    2770        table = expression.this
    2771        table_alias = ""
    2772
    2773        hints = table.args.get("hints")
    2774        if hints and table.alias and isinstance(hints[0], exp.WithTableHint):
    2775            # T-SQL syntax is MERGE ... <target_table> [WITH (<merge_hint>)] [[AS] table_alias]
    2776            table = table.copy()
    2777            table_alias = f" AS {self.sql(table.args['alias'].pop())}"
    2778
    2779        this = self.sql(table)
    2780        using = f"USING {self.sql(expression, 'using')}"
    2781        on = f"ON {self.sql(expression, 'on')}"
    2782        expressions = self.expressions(expression, sep=" ")
    2783
    2784        return f"MERGE INTO {this}{table_alias} {using} {on} {expressions}"

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

    2786    def tochar_sql(self, expression: exp.ToChar) -> str:
    2787        if expression.args.get("format"):
    2788            self.unsupported("Format argument unsupported for TO_CHAR/TO_VARCHAR function")
    2789
    2790        return self.sql(exp.cast(expression.this, "text"))

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

    2792    def dictproperty_sql(self, expression: exp.DictProperty) -> str:
    2793        this = self.sql(expression, "this")
    2794        kind = self.sql(expression, "kind")
    2795        settings_sql = self.expressions(expression, key="settings", sep=" ")
    2796        args = f"({self.sep('')}{settings_sql}{self.seg(')', sep='')}" if settings_sql else "()"
    2797        return f"{this}({kind}{args})"

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

    2799    def dictrange_sql(self, expression: exp.DictRange) -> str:
    2800        this = self.sql(expression, "this")
    2801        max = self.sql(expression, "max")
    2802        min = self.sql(expression, "min")
    2803        return f"{this}(MIN {min} MAX {max})"

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

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

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

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

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

    2811    def clusteredbyproperty_sql(self, expression: exp.ClusteredByProperty) -> str:
    2812        expressions = self.expressions(expression, key="expressions", flat=True)
    2813        sorted_by = self.expressions(expression, key="sorted_by", flat=True)
    2814        sorted_by = f" SORTED BY ({sorted_by})" if sorted_by else ""
    2815        buckets = self.sql(expression, "buckets")
    2816        return f"CLUSTERED BY ({expressions}){sorted_by} INTO {buckets} BUCKETS"

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

    2818    def anyvalue_sql(self, expression: exp.AnyValue) -> str:
    2819        this = self.sql(expression, "this")
    2820        having = self.sql(expression, "having")
    2821
    2822        if having:
    2823            this = f"{this} HAVING {'MAX' if expression.args.get('max') else 'MIN'} {having}"
    2824
    2825        return self.func("ANY_VALUE", this)

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

    2827    def querytransform_sql(self, expression: exp.QueryTransform) -> str:
    2828        transform = self.func("TRANSFORM", *expression.expressions)
    2829        row_format_before = self.sql(expression, "row_format_before")
    2830        row_format_before = f" {row_format_before}" if row_format_before else ""
    2831        record_writer = self.sql(expression, "record_writer")
    2832        record_writer = f" RECORDWRITER {record_writer}" if record_writer else ""
    2833        using = f" USING {self.sql(expression, 'command_script')}"
    2834        schema = self.sql(expression, "schema")
    2835        schema = f" AS {schema}" if schema else ""
    2836        row_format_after = self.sql(expression, "row_format_after")
    2837        row_format_after = f" {row_format_after}" if row_format_after else ""
    2838        record_reader = self.sql(expression, "record_reader")
    2839        record_reader = f" RECORDREADER {record_reader}" if record_reader else ""
    2840        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

    2842    def indexconstraintoption_sql(self, expression: exp.IndexConstraintOption) -> str:
    2843        key_block_size = self.sql(expression, "key_block_size")
    2844        if key_block_size:
    2845            return f"KEY_BLOCK_SIZE = {key_block_size}"
    2846
    2847        using = self.sql(expression, "using")
    2848        if using:
    2849            return f"USING {using}"
    2850
    2851        parser = self.sql(expression, "parser")
    2852        if parser:
    2853            return f"WITH PARSER {parser}"
    2854
    2855        comment = self.sql(expression, "comment")
    2856        if comment:
    2857            return f"COMMENT {comment}"
    2858
    2859        visible = expression.args.get("visible")
    2860        if visible is not None:
    2861            return "VISIBLE" if visible else "INVISIBLE"
    2862
    2863        engine_attr = self.sql(expression, "engine_attr")
    2864        if engine_attr:
    2865            return f"ENGINE_ATTRIBUTE = {engine_attr}"
    2866
    2867        secondary_engine_attr = self.sql(expression, "secondary_engine_attr")
    2868        if secondary_engine_attr:
    2869            return f"SECONDARY_ENGINE_ATTRIBUTE = {secondary_engine_attr}"
    2870
    2871        self.unsupported("Unsupported index constraint option.")
    2872        return ""

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

    2874    def indexcolumnconstraint_sql(self, expression: exp.IndexColumnConstraint) -> str:
    2875        kind = self.sql(expression, "kind")
    2876        kind = f"{kind} INDEX" if kind else "INDEX"
    2877        this = self.sql(expression, "this")
    2878        this = f" {this}" if this else ""
    2879        index_type = self.sql(expression, "index_type")
    2880        index_type = f" USING {index_type}" if index_type else ""
    2881        schema = self.sql(expression, "schema")
    2882        schema = f" {schema}" if schema else ""
    2883        options = self.expressions(expression, key="options", sep=" ")
    2884        options = f" {options}" if options else ""
    2885        return f"{kind}{this}{index_type}{schema}{options}"

def nvl2_sql(self, expression: sqlglot.expressions.Nvl2) -> str: View Source

    2887    def nvl2_sql(self, expression: exp.Nvl2) -> str:
    2888        if self.NVL2_SUPPORTED:
    2889            return self.function_fallback_sql(expression)
    2890
    2891        case = exp.Case().when(
    2892            expression.this.is_(exp.null()).not_(copy=False),
    2893            expression.args["true"].copy(),
    2894            copy=False,
    2895        )
    2896        else_cond = expression.args.get("false")
    2897        if else_cond:
    2898            case.else_(else_cond.copy(), copy=False)
    2899
    2900        return self.sql(case)

def comprehension_sql(self, expression: sqlglot.expressions.Comprehension) -> str: View Source

    2902    def comprehension_sql(self, expression: exp.Comprehension) -> str:
    2903        this = self.sql(expression, "this")
    2904        expr = self.sql(expression, "expression")
    2905        iterator = self.sql(expression, "iterator")
    2906        condition = self.sql(expression, "condition")
    2907        condition = f" IF {condition}" if condition else ""
    2908        return f"{this} FOR {expr} IN {iterator}{condition}"

def columnprefix_sql(self, expression: sqlglot.expressions.ColumnPrefix) -> str: View Source

    2910    def columnprefix_sql(self, expression: exp.ColumnPrefix) -> str:
    2911        return f"{self.sql(expression, 'this')}({self.sql(expression, 'expression')})"

def opclass_sql(self, expression: sqlglot.expressions.Opclass) -> str: View Source

    2913    def opclass_sql(self, expression: exp.Opclass) -> str:
    2914        return f"{self.sql(expression, 'this')} {self.sql(expression, 'expression')}"

def predict_sql(self, expression: sqlglot.expressions.Predict) -> str: View Source

    2916    def predict_sql(self, expression: exp.Predict) -> str:
    2917        model = self.sql(expression, "this")
    2918        model = f"MODEL {model}"
    2919        table = self.sql(expression, "expression")
    2920        table = f"TABLE {table}" if not isinstance(expression.expression, exp.Subquery) else table
    2921        parameters = self.sql(expression, "params_struct")
    2922        return self.func("PREDICT", model, table, parameters or None)

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

    2933def cached_generator(
    2934    cache: t.Optional[t.Dict[int, str]] = None
    2935) -> t.Callable[[exp.Expression], str]:
    2936    """Returns a cached generator."""
    2937    cache = {} if cache is None else cache
    2938    generator = Generator(normalize=True, identify="safe")
    2939    return lambda e: generator.generate(e, cache)

Returns a cached generator.