Edit on GitHub

# sqlglot.generator

View Source

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

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

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

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

    329    def __init__(
    330        self,
    331        pretty: t.Optional[bool] = None,
    332        identify: str | bool = False,
    333        normalize: bool = False,
    334        pad: int = 2,
    335        indent: int = 2,
    336        normalize_functions: t.Optional[str | bool] = None,
    337        unsupported_level: ErrorLevel = ErrorLevel.WARN,
    338        max_unsupported: int = 3,
    339        leading_comma: bool = False,
    340        max_text_width: int = 80,
    341        comments: bool = True,
    342    ):
    343        import sqlglot
    344
    345        self.pretty = pretty if pretty is not None else sqlglot.pretty
    346        self.identify = identify
    347        self.normalize = normalize
    348        self.pad = pad
    349        self._indent = indent
    350        self.unsupported_level = unsupported_level
    351        self.max_unsupported = max_unsupported
    352        self.leading_comma = leading_comma
    353        self.max_text_width = max_text_width
    354        self.comments = comments
    355
    356        # This is both a Dialect property and a Generator argument, so we prioritize the latter
    357        self.normalize_functions = (
    358            self.NORMALIZE_FUNCTIONS if normalize_functions is None else normalize_functions
    359        )
    360
    361        self.unsupported_messages: t.List[str] = []
    362        self._escaped_quote_end: str = self.STRING_ESCAPE + self.QUOTE_END
    363        self._escaped_identifier_end: str = self.IDENTIFIER_ESCAPE + self.IDENTIFIER_END
    364        self._cache: t.Optional[t.Dict[int, str]] = None

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

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

Checks if text can be identified given an identify option.

###### Arguments:

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

###### Returns:

> Whether or not the given text can be identified.

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

    366    def generate(
    367        self,
    368        expression: t.Optional[exp.Expression],
    369        cache: t.Optional[t.Dict[int, str]] = None,
    370    ) -> str:
    371        """
    372        Generates the SQL string corresponding to the given syntax tree.
    373
    374        Args:
    375            expression: The syntax tree.
    376            cache: An optional sql string cache. This leverages the hash of an Expression
    377                which can be slow to compute, so only use it if you set _hash on each node.
    378
    379        Returns:
    380            The SQL string corresponding to `expression`.
    381        """
    382        if cache is not None:
    383            self._cache = cache
    384
    385        self.unsupported_messages = []
    386        sql = self.sql(expression).strip()
    387        self._cache = None
    388
    389        if self.unsupported_level == ErrorLevel.IGNORE:
    390            return sql
    391
    392        if self.unsupported_level == ErrorLevel.WARN:
    393            for msg in self.unsupported_messages:
    394                logger.warning(msg)
    395        elif self.unsupported_level == ErrorLevel.RAISE and self.unsupported_messages:
    396            raise UnsupportedError(concat_messages(self.unsupported_messages, self.max_unsupported))
    397
    398        if self.pretty:
    399            sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
    400        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

    402    def unsupported(self, message: str) -> None:
    403        if self.unsupported_level == ErrorLevel.IMMEDIATE:
    404            raise UnsupportedError(message)
    405        self.unsupported_messages.append(message)

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

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

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

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

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

    413    def pad_comment(self, comment: str) -> str:
    414        comment = " " + comment if comment[0].strip() else comment
    415        comment = comment + " " if comment[-1].strip() else comment
    416        return comment

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

    418    def maybe_comment(
    419        self,
    420        sql: str,
    421        expression: t.Optional[exp.Expression] = None,
    422        comments: t.Optional[t.List[str]] = None,
    423    ) -> str:
    424        comments = (
    425            ((expression and expression.comments) if comments is None else comments)  # type: ignore
    426            if self.comments
    427            else None
    428        )
    429
    430        if not comments or isinstance(expression, exp.Binary):
    431            return sql
    432
    433        sep = "\n" if self.pretty else " "
    434        comments_sql = sep.join(
    435            f"/*{self.pad_comment(comment)}*/" for comment in comments if comment
    436        )
    437
    438        if not comments_sql:
    439            return sql
    440
    441        if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
    442            return (
    443                f"{self.sep()}{comments_sql}{sql}"
    444                if sql[0].isspace()
    445                else f"{comments_sql}{self.sep()}{sql}"
    446            )
    447
    448        return f"{sql} {comments_sql}"

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

    450    def wrap(self, expression: exp.Expression | str) -> str:
    451        this_sql = self.indent(
    452            self.sql(expression)
    453            if isinstance(expression, (exp.Select, exp.Union))
    454            else self.sql(expression, "this"),
    455            level=1,
    456            pad=0,
    457        )
    458        return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"

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

    460    def no_identify(self, func: t.Callable[..., str], *args, **kwargs) -> str:
    461        original = self.identify
    462        self.identify = False
    463        result = func(*args, **kwargs)
    464        self.identify = original
    465        return result

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

    467    def normalize_func(self, name: str) -> str:
    468        if self.normalize_functions == "upper" or self.normalize_functions is True:
    469            return name.upper()
    470        if self.normalize_functions == "lower":
    471            return name.lower()
    472        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

    474    def indent(
    475        self,
    476        sql: str,
    477        level: int = 0,
    478        pad: t.Optional[int] = None,
    479        skip_first: bool = False,
    480        skip_last: bool = False,
    481    ) -> str:
    482        if not self.pretty:
    483            return sql
    484
    485        pad = self.pad if pad is None else pad
    486        lines = sql.split("\n")
    487
    488        return "\n".join(
    489            line
    490            if (skip_first and i == 0) or (skip_last and i == len(lines) - 1)
    491            else f"{' ' * (level * self._indent + pad)}{line}"
    492            for i, line in enumerate(lines)
    493        )

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

    495    def sql(
    496        self,
    497        expression: t.Optional[str | exp.Expression],
    498        key: t.Optional[str] = None,
    499        comment: bool = True,
    500    ) -> str:
    501        if not expression:
    502            return ""
    503
    504        if isinstance(expression, str):
    505            return expression
    506
    507        if key:
    508            value = expression.args.get(key)
    509            if value:
    510                return self.sql(value)
    511            return ""
    512
    513        if self._cache is not None:
    514            expression_id = hash(expression)
    515
    516            if expression_id in self._cache:
    517                return self._cache[expression_id]
    518
    519        transform = self.TRANSFORMS.get(expression.__class__)
    520
    521        if callable(transform):
    522            sql = transform(self, expression)
    523        elif transform:
    524            sql = transform
    525        elif isinstance(expression, exp.Expression):
    526            exp_handler_name = f"{expression.key}_sql"
    527
    528            if hasattr(self, exp_handler_name):
    529                sql = getattr(self, exp_handler_name)(expression)
    530            elif isinstance(expression, exp.Func):
    531                sql = self.function_fallback_sql(expression)
    532            elif isinstance(expression, exp.Property):
    533                sql = self.property_sql(expression)
    534            else:
    535                raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
    536        else:
    537            raise ValueError(f"Expected an Expression. Received {type(expression)}: {expression}")
    538
    539        sql = self.maybe_comment(sql, expression) if self.comments and comment else sql
    540
    541        if self._cache is not None:
    542            self._cache[expression_id] = sql
    543        return sql

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

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

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

    550    def cache_sql(self, expression: exp.Cache) -> str:
    551        lazy = " LAZY" if expression.args.get("lazy") else ""
    552        table = self.sql(expression, "this")
    553        options = expression.args.get("options")
    554        options = f" OPTIONS({self.sql(options[0])} = {self.sql(options[1])})" if options else ""
    555        sql = self.sql(expression, "expression")
    556        sql = f" AS{self.sep()}{sql}" if sql else ""
    557        sql = f"CACHE{lazy} TABLE {table}{options}{sql}"
    558        return self.prepend_ctes(expression, sql)

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

    560    def characterset_sql(self, expression: exp.CharacterSet) -> str:
    561        if isinstance(expression.parent, exp.Cast):
    562            return f"CHAR CHARACTER SET {self.sql(expression, 'this')}"
    563        default = "DEFAULT " if expression.args.get("default") else ""
    564        return f"{default}CHARACTER SET={self.sql(expression, 'this')}"

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

    566    def column_sql(self, expression: exp.Column) -> str:
    567        join_mark = " (+)" if expression.args.get("join_mark") else ""
    568
    569        if join_mark and not self.COLUMN_JOIN_MARKS_SUPPORTED:
    570            join_mark = ""
    571            self.unsupported("Outer join syntax using the (+) operator is not supported.")
    572
    573        column = ".".join(
    574            self.sql(part)
    575            for part in (
    576                expression.args.get("catalog"),
    577                expression.args.get("db"),
    578                expression.args.get("table"),
    579                expression.args.get("this"),
    580            )
    581            if part
    582        )
    583
    584        return f"{column}{join_mark}"

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

    586    def columnposition_sql(self, expression: exp.ColumnPosition) -> str:
    587        this = self.sql(expression, "this")
    588        this = f" {this}" if this else ""
    589        position = self.sql(expression, "position")
    590        return f"{position}{this}"

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

    592    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
    593        column = self.sql(expression, "this")
    594        kind = self.sql(expression, "kind")
    595        constraints = self.expressions(expression, key="constraints", sep=" ", flat=True)
    596        exists = "IF NOT EXISTS " if expression.args.get("exists") else ""
    597        kind = f"{sep}{kind}" if kind else ""
    598        constraints = f" {constraints}" if constraints else ""
    599        position = self.sql(expression, "position")
    600        position = f" {position}" if position else ""
    601
    602        return f"{exists}{column}{kind}{constraints}{position}"

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

    604    def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str:
    605        this = self.sql(expression, "this")
    606        kind_sql = self.sql(expression, "kind").strip()
    607        return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql

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

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

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

    612    def compresscolumnconstraint_sql(self, expression: exp.CompressColumnConstraint) -> str:
    613        if isinstance(expression.this, list):
    614            this = self.wrap(self.expressions(expression, key="this", flat=True))
    615        else:
    616            this = self.sql(expression, "this")
    617
    618        return f"COMPRESS {this}"

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

    620    def generatedasidentitycolumnconstraint_sql(
    621        self, expression: exp.GeneratedAsIdentityColumnConstraint
    622    ) -> str:
    623        this = ""
    624        if expression.this is not None:
    625            on_null = "ON NULL " if expression.args.get("on_null") else ""
    626            this = " ALWAYS " if expression.this else f" BY DEFAULT {on_null}"
    627
    628        start = expression.args.get("start")
    629        start = f"START WITH {start}" if start else ""
    630        increment = expression.args.get("increment")
    631        increment = f" INCREMENT BY {increment}" if increment else ""
    632        minvalue = expression.args.get("minvalue")
    633        minvalue = f" MINVALUE {minvalue}" if minvalue else ""
    634        maxvalue = expression.args.get("maxvalue")
    635        maxvalue = f" MAXVALUE {maxvalue}" if maxvalue else ""
    636        cycle = expression.args.get("cycle")
    637        cycle_sql = ""
    638
    639        if cycle is not None:
    640            cycle_sql = f"{' NO' if not cycle else ''} CYCLE"
    641            cycle_sql = cycle_sql.strip() if not start and not increment else cycle_sql
    642
    643        sequence_opts = ""
    644        if start or increment or cycle_sql:
    645            sequence_opts = f"{start}{increment}{minvalue}{maxvalue}{cycle_sql}"
    646            sequence_opts = f" ({sequence_opts.strip()})"
    647
    648        expr = self.sql(expression, "expression")
    649        expr = f"({expr})" if expr else "IDENTITY"
    650
    651        return f"GENERATED{this}AS {expr}{sequence_opts}"

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

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

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

    656    def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
    657        desc = expression.args.get("desc")
    658        if desc is not None:
    659            return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
    660        return f"PRIMARY KEY"

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

    662    def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
    663        this = self.sql(expression, "this")
    664        this = f" {this}" if this else ""
    665        return f"UNIQUE{this}"

def createable_sql( self, expression: sqlglot.expressions.Create, locations: dict[sqlglot.expressions.Properties.Location, list[sqlglot.expressions.Property]]) -> str: View Source

    667    def createable_sql(
    668        self, expression: exp.Create, locations: dict[exp.Properties.Location, list[exp.Property]]
    669    ) -> str:
    670        return self.sql(expression, "this")

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

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

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

    757    def clone_sql(self, expression: exp.Clone) -> str:
    758        this = self.sql(expression, "this")
    759        when = self.sql(expression, "when")
    760
    761        if when:
    762            kind = self.sql(expression, "kind")
    763            expr = self.sql(expression, "expression")
    764            return f"CLONE {this} {when} ({kind} => {expr})"
    765
    766        return f"CLONE {this}"

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

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

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

    771    def prepend_ctes(self, expression: exp.Expression, sql: str) -> str:
    772        with_ = self.sql(expression, "with")
    773        if with_:
    774            sql = f"{with_}{self.sep()}{sql}"
    775        return sql

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

    777    def with_sql(self, expression: exp.With) -> str:
    778        sql = self.expressions(expression, flat=True)
    779        recursive = "RECURSIVE " if expression.args.get("recursive") else ""
    780
    781        return f"WITH {recursive}{sql}"

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

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

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

    787    def tablealias_sql(self, expression: exp.TableAlias) -> str:
    788        alias = self.sql(expression, "this")
    789        columns = self.expressions(expression, key="columns", flat=True)
    790        columns = f"({columns})" if columns else ""
    791        return f"{alias}{columns}"

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

    793    def bitstring_sql(self, expression: exp.BitString) -> str:
    794        this = self.sql(expression, "this")
    795        if self.BIT_START:
    796            return f"{self.BIT_START}{this}{self.BIT_END}"
    797        return f"{int(this, 2)}"

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

    799    def hexstring_sql(self, expression: exp.HexString) -> str:
    800        this = self.sql(expression, "this")
    801        if self.HEX_START:
    802            return f"{self.HEX_START}{this}{self.HEX_END}"
    803        return f"{int(this, 16)}"

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

    805    def bytestring_sql(self, expression: exp.ByteString) -> str:
    806        this = self.sql(expression, "this")
    807        if self.BYTE_START:
    808            return f"{self.BYTE_START}{this}{self.BYTE_END}"
    809        return this

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

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

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

    815    def datatypesize_sql(self, expression: exp.DataTypeSize) -> str:
    816        this = self.sql(expression, "this")
    817        specifier = self.sql(expression, "expression")
    818        specifier = f" {specifier}" if specifier else ""
    819        return f"{this}{specifier}"

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

    821    def datatype_sql(self, expression: exp.DataType) -> str:
    822        type_value = expression.this
    823        type_sql = self.TYPE_MAPPING.get(type_value, type_value.value)
    824        nested = ""
    825        interior = self.expressions(expression, flat=True)
    826        values = ""
    827        if interior:
    828            if expression.args.get("nested"):
    829                nested = f"{self.STRUCT_DELIMITER[0]}{interior}{self.STRUCT_DELIMITER[1]}"
    830                if expression.args.get("values") is not None:
    831                    delimiters = ("[", "]") if type_value == exp.DataType.Type.ARRAY else ("(", ")")
    832                    values = self.expressions(expression, key="values", flat=True)
    833                    values = f"{delimiters[0]}{values}{delimiters[1]}"
    834            else:
    835                nested = f"({interior})"
    836
    837        return f"{type_sql}{nested}{values}"

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

    839    def directory_sql(self, expression: exp.Directory) -> str:
    840        local = "LOCAL " if expression.args.get("local") else ""
    841        row_format = self.sql(expression, "row_format")
    842        row_format = f" {row_format}" if row_format else ""
    843        return f"{local}DIRECTORY {self.sql(expression, 'this')}{row_format}"

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

    845    def delete_sql(self, expression: exp.Delete) -> str:
    846        this = self.sql(expression, "this")
    847        this = f" FROM {this}" if this else ""
    848        using = self.sql(expression, "using")
    849        using = f" USING {using}" if using else ""
    850        where = self.sql(expression, "where")
    851        returning = self.sql(expression, "returning")
    852        limit = self.sql(expression, "limit")
    853        tables = self.expressions(expression, key="tables")
    854        tables = f" {tables}" if tables else ""
    855        if self.RETURNING_END:
    856            expression_sql = f"{this}{using}{where}{returning}{limit}"
    857        else:
    858            expression_sql = f"{returning}{this}{using}{where}{limit}"
    859        return self.prepend_ctes(expression, f"DELETE{tables}{expression_sql}")

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

    861    def drop_sql(self, expression: exp.Drop) -> str:
    862        this = self.sql(expression, "this")
    863        kind = expression.args["kind"]
    864        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    865        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    866        materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
    867        cascade = " CASCADE" if expression.args.get("cascade") else ""
    868        constraints = " CONSTRAINTS" if expression.args.get("constraints") else ""
    869        purge = " PURGE" if expression.args.get("purge") else ""
    870        return (
    871            f"DROP{temporary}{materialized} {kind}{exists_sql}{this}{cascade}{constraints}{purge}"
    872        )

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

    874    def except_sql(self, expression: exp.Except) -> str:
    875        return self.prepend_ctes(
    876            expression,
    877            self.set_operation(expression, self.except_op(expression)),
    878        )

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

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

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

    883    def fetch_sql(self, expression: exp.Fetch) -> str:
    884        direction = expression.args.get("direction")
    885        direction = f" {direction.upper()}" if direction else ""
    886        count = expression.args.get("count")
    887        count = f" {count}" if count else ""
    888        if expression.args.get("percent"):
    889            count = f"{count} PERCENT"
    890        with_ties_or_only = "WITH TIES" if expression.args.get("with_ties") else "ONLY"
    891        return f"{self.seg('FETCH')}{direction}{count} ROWS {with_ties_or_only}"

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

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

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

    898    def hint_sql(self, expression: exp.Hint) -> str:
    899        if not self.QUERY_HINTS:
    900            self.unsupported("Hints are not supported")
    901            return ""
    902
    903        return f" /*+ {self.expressions(expression, sep=self.QUERY_HINT_SEP).strip()} */"

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

    905    def index_sql(self, expression: exp.Index) -> str:
    906        unique = "UNIQUE " if expression.args.get("unique") else ""
    907        primary = "PRIMARY " if expression.args.get("primary") else ""
    908        amp = "AMP " if expression.args.get("amp") else ""
    909        name = self.sql(expression, "this")
    910        name = f"{name} " if name else ""
    911        table = self.sql(expression, "table")
    912        table = f"{self.INDEX_ON} {table} " if table else ""
    913        using = self.sql(expression, "using")
    914        using = f"USING {using} " if using else ""
    915        index = "INDEX " if not table else ""
    916        columns = self.expressions(expression, key="columns", flat=True)
    917        columns = f"({columns})" if columns else ""
    918        partition_by = self.expressions(expression, key="partition_by", flat=True)
    919        partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
    920        return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}"

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

    922    def identifier_sql(self, expression: exp.Identifier) -> str:
    923        text = expression.name
    924        lower = text.lower()
    925        text = lower if self.normalize and not expression.quoted else text
    926        text = text.replace(self.IDENTIFIER_END, self._escaped_identifier_end)
    927        if (
    928            expression.quoted
    929            or self.can_identify(text, self.identify)
    930            or lower in self.RESERVED_KEYWORDS
    931            or (not self.IDENTIFIERS_CAN_START_WITH_DIGIT and text[:1].isdigit())
    932        ):
    933            text = f"{self.IDENTIFIER_START}{text}{self.IDENTIFIER_END}"
    934        return text

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

    936    def inputoutputformat_sql(self, expression: exp.InputOutputFormat) -> str:
    937        input_format = self.sql(expression, "input_format")
    938        input_format = f"INPUTFORMAT {input_format}" if input_format else ""
    939        output_format = self.sql(expression, "output_format")
    940        output_format = f"OUTPUTFORMAT {output_format}" if output_format else ""
    941        return self.sep().join((input_format, output_format))

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

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

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

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

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

    950    def properties_sql(self, expression: exp.Properties) -> str:
    951        root_properties = []
    952        with_properties = []
    953
    954        for p in expression.expressions:
    955            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    956            if p_loc == exp.Properties.Location.POST_WITH:
    957                with_properties.append(p)
    958            elif p_loc == exp.Properties.Location.POST_SCHEMA:
    959                root_properties.append(p)
    960
    961        return self.root_properties(
    962            exp.Properties(expressions=root_properties)
    963        ) + self.with_properties(exp.Properties(expressions=with_properties))

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

    965    def root_properties(self, properties: exp.Properties) -> str:
    966        if properties.expressions:
    967            return self.sep() + self.expressions(properties, indent=False, sep=" ")
    968        return ""

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

    970    def properties(
    971        self,
    972        properties: exp.Properties,
    973        prefix: str = "",
    974        sep: str = ", ",
    975        suffix: str = "",
    976        wrapped: bool = True,
    977    ) -> str:
    978        if properties.expressions:
    979            expressions = self.expressions(properties, sep=sep, indent=False)
    980            expressions = self.wrap(expressions) if wrapped else expressions
    981            return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
    982        return ""

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

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

def locate_properties( self, properties: sqlglot.expressions.Properties) -> Dict[sqlglot.expressions.Properties.Location, list[sqlglot.expressions.Property]]: View Source

     987    def locate_properties(
     988        self, properties: exp.Properties
     989    ) -> t.Dict[exp.Properties.Location, list[exp.Property]]:
     990        properties_locs: t.Dict[exp.Properties.Location, list[exp.Property]] = {
     991            key: [] for key in exp.Properties.Location
     992        }
     993
     994        for p in properties.expressions:
     995            p_loc = self.PROPERTIES_LOCATION[p.__class__]
     996            if p_loc == exp.Properties.Location.POST_NAME:
     997                properties_locs[exp.Properties.Location.POST_NAME].append(p)
     998            elif p_loc == exp.Properties.Location.POST_INDEX:
     999                properties_locs[exp.Properties.Location.POST_INDEX].append(p)
    1000            elif p_loc == exp.Properties.Location.POST_SCHEMA:
    1001                properties_locs[exp.Properties.Location.POST_SCHEMA].append(p)
    1002            elif p_loc == exp.Properties.Location.POST_WITH:
    1003                properties_locs[exp.Properties.Location.POST_WITH].append(p)
    1004            elif p_loc == exp.Properties.Location.POST_CREATE:
    1005                properties_locs[exp.Properties.Location.POST_CREATE].append(p)
    1006            elif p_loc == exp.Properties.Location.POST_ALIAS:
    1007                properties_locs[exp.Properties.Location.POST_ALIAS].append(p)
    1008            elif p_loc == exp.Properties.Location.POST_EXPRESSION:
    1009                properties_locs[exp.Properties.Location.POST_EXPRESSION].append(p)
    1010            elif p_loc == exp.Properties.Location.UNSUPPORTED:
    1011                self.unsupported(f"Unsupported property {p.key}")
    1012
    1013        return properties_locs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    1311    def values_sql(self, expression: exp.Values) -> str:
    1312        args = self.expressions(expression)
    1313        alias = self.sql(expression, "alias")
    1314        values = f"VALUES{self.seg('')}{args}"
    1315        values = (
    1316            f"({values})"
    1317            if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
    1318            else values
    1319        )
    1320        return f"{values} AS {alias}" if alias else values

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

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

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

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

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

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

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

    1333    def group_sql(self, expression: exp.Group) -> str:
    1334        group_by = self.op_expressions("GROUP BY", expression)
    1335
    1336        if expression.args.get("all"):
    1337            return f"{group_by} ALL"
    1338
    1339        grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
    1340        grouping_sets = (
    1341            f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
    1342        )
    1343
    1344        cube = expression.args.get("cube", [])
    1345        if seq_get(cube, 0) is True:
    1346            return f"{group_by}{self.seg('WITH CUBE')}"
    1347        else:
    1348            cube_sql = self.expressions(expression, key="cube", indent=False)
    1349            cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else ""
    1350
    1351        rollup = expression.args.get("rollup", [])
    1352        if seq_get(rollup, 0) is True:
    1353            return f"{group_by}{self.seg('WITH ROLLUP')}"
    1354        else:
    1355            rollup_sql = self.expressions(expression, key="rollup", indent=False)
    1356            rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else ""
    1357
    1358        groupings = csv(
    1359            grouping_sets,
    1360            cube_sql,
    1361            rollup_sql,
    1362            self.seg("WITH TOTALS") if expression.args.get("totals") else "",
    1363            sep=self.GROUPINGS_SEP,
    1364        )
    1365
    1366        if expression.args.get("expressions") and groupings:
    1367            group_by = f"{group_by}{self.GROUPINGS_SEP}"
    1368
    1369        return f"{group_by}{groupings}"

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

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

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

    1375    def join_sql(self, expression: exp.Join) -> str:
    1376        op_sql = " ".join(
    1377            op
    1378            for op in (
    1379                expression.method,
    1380                "GLOBAL" if expression.args.get("global") else None,
    1381                expression.side,
    1382                expression.kind,
    1383                expression.hint if self.JOIN_HINTS else None,
    1384            )
    1385            if op
    1386        )
    1387        on_sql = self.sql(expression, "on")
    1388        using = expression.args.get("using")
    1389
    1390        if not on_sql and using:
    1391            on_sql = csv(*(self.sql(column) for column in using))
    1392
    1393        this_sql = self.sql(expression, "this")
    1394
    1395        if on_sql:
    1396            on_sql = self.indent(on_sql, skip_first=True)
    1397            space = self.seg(" " * self.pad) if self.pretty else " "
    1398            if using:
    1399                on_sql = f"{space}USING ({on_sql})"
    1400            else:
    1401                on_sql = f"{space}ON {on_sql}"
    1402        elif not op_sql:
    1403            return f", {this_sql}"
    1404
    1405        op_sql = f"{op_sql} JOIN" if op_sql else "JOIN"
    1406        return f"{self.seg(op_sql)} {this_sql}{on_sql}"

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

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

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

    1413    def lateral_sql(self, expression: exp.Lateral) -> str:
    1414        this = self.sql(expression, "this")
    1415
    1416        if isinstance(expression.this, exp.Subquery):
    1417            return f"LATERAL {this}"
    1418
    1419        if expression.args.get("view"):
    1420            alias = expression.args["alias"]
    1421            columns = self.expressions(alias, key="columns", flat=True)
    1422            table = f" {alias.name}" if alias.name else ""
    1423            columns = f" AS {columns}" if columns else ""
    1424            op_sql = self.seg(f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}")
    1425            return f"{op_sql}{self.sep()}{this}{table}{columns}"
    1426
    1427        alias = self.sql(expression, "alias")
    1428        alias = f" AS {alias}" if alias else ""
    1429        return f"LATERAL {this}{alias}"

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

    1431    def limit_sql(self, expression: exp.Limit, top: bool = False) -> str:
    1432        this = self.sql(expression, "this")
    1433        args = ", ".join(
    1434            sql
    1435            for sql in (
    1436                self.sql(expression, "offset"),
    1437                self.sql(expression, "expression"),
    1438            )
    1439            if sql
    1440        )
    1441        return f"{this}{self.seg('TOP' if top else 'LIMIT')} {args}"

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

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

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

    1447    def setitem_sql(self, expression: exp.SetItem) -> str:
    1448        kind = self.sql(expression, "kind")
    1449        kind = f"{kind} " if kind else ""
    1450        this = self.sql(expression, "this")
    1451        expressions = self.expressions(expression)
    1452        collate = self.sql(expression, "collate")
    1453        collate = f" COLLATE {collate}" if collate else ""
    1454        global_ = "GLOBAL " if expression.args.get("global") else ""
    1455        return f"{global_}{kind}{this}{expressions}{collate}"

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

    1457    def set_sql(self, expression: exp.Set) -> str:
    1458        expressions = (
    1459            f" {self.expressions(expression, flat=True)}" if expression.expressions else ""
    1460        )
    1461        tag = " TAG" if expression.args.get("tag") else ""
    1462        return f"{'UNSET' if expression.args.get('unset') else 'SET'}{tag}{expressions}"

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

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

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

    1467    def lock_sql(self, expression: exp.Lock) -> str:
    1468        if not self.LOCKING_READS_SUPPORTED:
    1469            self.unsupported("Locking reads using 'FOR UPDATE/SHARE' are not supported")
    1470            return ""
    1471
    1472        lock_type = "FOR UPDATE" if expression.args["update"] else "FOR SHARE"
    1473        expressions = self.expressions(expression, flat=True)
    1474        expressions = f" OF {expressions}" if expressions else ""
    1475        wait = expression.args.get("wait")
    1476
    1477        if wait is not None:
    1478            if isinstance(wait, exp.Literal):
    1479                wait = f" WAIT {self.sql(wait)}"
    1480            else:
    1481                wait = " NOWAIT" if wait else " SKIP LOCKED"
    1482
    1483        return f"{lock_type}{expressions}{wait or ''}"

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

    1485    def literal_sql(self, expression: exp.Literal) -> str:
    1486        text = expression.this or ""
    1487        if expression.is_string:
    1488            text = f"{self.QUOTE_START}{self.escape_str(text)}{self.QUOTE_END}"
    1489        return text

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

    1491    def escape_str(self, text: str) -> str:
    1492        text = text.replace(self.QUOTE_END, self._escaped_quote_end)
    1493        if self.ESCAPE_LINE_BREAK:
    1494            text = text.replace("\n", "\\n")
    1495        elif self.pretty:
    1496            text = text.replace("\n", self.SENTINEL_LINE_BREAK)
    1497        return text

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

    1499    def loaddata_sql(self, expression: exp.LoadData) -> str:
    1500        local = " LOCAL" if expression.args.get("local") else ""
    1501        inpath = f" INPATH {self.sql(expression, 'inpath')}"
    1502        overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
    1503        this = f" INTO TABLE {self.sql(expression, 'this')}"
    1504        partition = self.sql(expression, "partition")
    1505        partition = f" {partition}" if partition else ""
    1506        input_format = self.sql(expression, "input_format")
    1507        input_format = f" INPUTFORMAT {input_format}" if input_format else ""
    1508        serde = self.sql(expression, "serde")
    1509        serde = f" SERDE {serde}" if serde else ""
    1510        return f"LOAD DATA{local}{inpath}{overwrite}{this}{partition}{input_format}{serde}"

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

    1512    def null_sql(self, *_) -> str:
    1513        return "NULL"

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

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

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

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

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

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

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

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

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

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

    1532    def ordered_sql(self, expression: exp.Ordered) -> str:
    1533        desc = expression.args.get("desc")
    1534        asc = not desc
    1535
    1536        nulls_first = expression.args.get("nulls_first")
    1537        nulls_last = not nulls_first
    1538        nulls_are_large = self.NULL_ORDERING == "nulls_are_large"
    1539        nulls_are_small = self.NULL_ORDERING == "nulls_are_small"
    1540        nulls_are_last = self.NULL_ORDERING == "nulls_are_last"
    1541
    1542        sort_order = " DESC" if desc else ""
    1543        nulls_sort_change = ""
    1544        if nulls_first and (
    1545            (asc and nulls_are_large) or (desc and nulls_are_small) or nulls_are_last
    1546        ):
    1547            nulls_sort_change = " NULLS FIRST"
    1548        elif (
    1549            nulls_last
    1550            and ((asc and nulls_are_small) or (desc and nulls_are_large))
    1551            and not nulls_are_last
    1552        ):
    1553            nulls_sort_change = " NULLS LAST"
    1554
    1555        if nulls_sort_change and not self.NULL_ORDERING_SUPPORTED:
    1556            self.unsupported(
    1557                "Sorting in an ORDER BY on NULLS FIRST/NULLS LAST is not supported by this dialect"
    1558            )
    1559            nulls_sort_change = ""
    1560
    1561        return f"{self.sql(expression, 'this')}{sort_order}{nulls_sort_change}"

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

    1563    def matchrecognize_sql(self, expression: exp.MatchRecognize) -> str:
    1564        partition = self.partition_by_sql(expression)
    1565        order = self.sql(expression, "order")
    1566        measures = self.expressions(expression, key="measures")
    1567        measures = self.seg(f"MEASURES{self.seg(measures)}") if measures else ""
    1568        rows = self.sql(expression, "rows")
    1569        rows = self.seg(rows) if rows else ""
    1570        after = self.sql(expression, "after")
    1571        after = self.seg(after) if after else ""
    1572        pattern = self.sql(expression, "pattern")
    1573        pattern = self.seg(f"PATTERN ({pattern})") if pattern else ""
    1574        definition_sqls = [
    1575            f"{self.sql(definition, 'alias')} AS {self.sql(definition, 'this')}"
    1576            for definition in expression.args.get("define", [])
    1577        ]
    1578        definitions = self.expressions(sqls=definition_sqls)
    1579        define = self.seg(f"DEFINE{self.seg(definitions)}") if definitions else ""
    1580        body = "".join(
    1581            (
    1582                partition,
    1583                order,
    1584                measures,
    1585                rows,
    1586                after,
    1587                pattern,
    1588                define,
    1589            )
    1590        )
    1591        alias = self.sql(expression, "alias")
    1592        alias = f" {alias}" if alias else ""
    1593        return f"{self.seg('MATCH_RECOGNIZE')} {self.wrap(body)}{alias}"

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

    1595    def query_modifiers(self, expression: exp.Expression, *sqls: str) -> str:
    1596        limit: t.Optional[exp.Fetch | exp.Limit] = expression.args.get("limit")
    1597
    1598        # If the limit is generated as TOP, we need to ensure it's not generated twice
    1599        with_offset_limit_modifiers = not isinstance(limit, exp.Limit) or not self.LIMIT_IS_TOP
    1600
    1601        if self.LIMIT_FETCH == "LIMIT" and isinstance(limit, exp.Fetch):
    1602            limit = exp.Limit(expression=limit.args.get("count"))
    1603        elif self.LIMIT_FETCH == "FETCH" and isinstance(limit, exp.Limit):
    1604            limit = exp.Fetch(direction="FIRST", count=limit.expression)
    1605
    1606        fetch = isinstance(limit, exp.Fetch)
    1607
    1608        offset_limit_modifiers = (
    1609            self.offset_limit_modifiers(expression, fetch, limit)
    1610            if with_offset_limit_modifiers
    1611            else []
    1612        )
    1613
    1614        return csv(
    1615            *sqls,
    1616            *[self.sql(join) for join in expression.args.get("joins") or []],
    1617            self.sql(expression, "match"),
    1618            *[self.sql(lateral) for lateral in expression.args.get("laterals") or []],
    1619            self.sql(expression, "where"),
    1620            self.sql(expression, "group"),
    1621            self.sql(expression, "having"),
    1622            *self.after_having_modifiers(expression),
    1623            self.sql(expression, "order"),
    1624            *offset_limit_modifiers,
    1625            *self.after_limit_modifiers(expression),
    1626            sep="",
    1627        )

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

    1629    def offset_limit_modifiers(
    1630        self, expression: exp.Expression, fetch: bool, limit: t.Optional[exp.Fetch | exp.Limit]
    1631    ) -> t.List[str]:
    1632        return [
    1633            self.sql(expression, "offset") if fetch else self.sql(limit),
    1634            self.sql(limit) if fetch else self.sql(expression, "offset"),
    1635        ]

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

    1637    def after_having_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1638        return [
    1639            self.sql(expression, "qualify"),
    1640            self.seg("WINDOW ") + self.expressions(expression, key="windows", flat=True)
    1641            if expression.args.get("windows")
    1642            else "",
    1643            self.sql(expression, "distribute"),
    1644            self.sql(expression, "sort"),
    1645            self.sql(expression, "cluster"),
    1646        ]

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

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

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

    1653    def select_sql(self, expression: exp.Select) -> str:
    1654        hint = self.sql(expression, "hint")
    1655        distinct = self.sql(expression, "distinct")
    1656        distinct = f" {distinct}" if distinct else ""
    1657        kind = self.sql(expression, "kind").upper()
    1658        limit = expression.args.get("limit")
    1659        top = (
    1660            self.limit_sql(limit, top=True)
    1661            if isinstance(limit, exp.Limit) and self.LIMIT_IS_TOP
    1662            else ""
    1663        )
    1664
    1665        expressions = self.expressions(expression)
    1666
    1667        if kind:
    1668            if kind in self.SELECT_KINDS:
    1669                kind = f" AS {kind}"
    1670            else:
    1671                if kind == "STRUCT":
    1672                    expressions = self.expressions(
    1673                        sqls=[
    1674                            self.sql(
    1675                                exp.Struct(
    1676                                    expressions=[
    1677                                        exp.column(e.output_name).eq(
    1678                                            e.this if isinstance(e, exp.Alias) else e
    1679                                        )
    1680                                        for e in expression.expressions
    1681                                    ]
    1682                                )
    1683                            )
    1684                        ]
    1685                    )
    1686                kind = ""
    1687
    1688        expressions = f"{self.sep()}{expressions}" if expressions else expressions
    1689        sql = self.query_modifiers(
    1690            expression,
    1691            f"SELECT{top}{hint}{distinct}{kind}{expressions}",
    1692            self.sql(expression, "into", comment=False),
    1693            self.sql(expression, "from", comment=False),
    1694        )
    1695        return self.prepend_ctes(expression, sql)

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

    1697    def schema_sql(self, expression: exp.Schema) -> str:
    1698        this = self.sql(expression, "this")
    1699        this = f"{this} " if this else ""
    1700        sql = self.schema_columns_sql(expression)
    1701        return f"{this}{sql}"

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

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

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

    1706    def star_sql(self, expression: exp.Star) -> str:
    1707        except_ = self.expressions(expression, key="except", flat=True)
    1708        except_ = f"{self.seg(self.STAR_MAPPING['except'])} ({except_})" if except_ else ""
    1709        replace = self.expressions(expression, key="replace", flat=True)
    1710        replace = f"{self.seg(self.STAR_MAPPING['replace'])} ({replace})" if replace else ""
    1711        return f"*{except_}{replace}"

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

    1713    def parameter_sql(self, expression: exp.Parameter) -> str:
    1714        this = self.sql(expression, "this")
    1715        this = f"{{{this}}}" if expression.args.get("wrapped") else f"{this}"
    1716        return f"{self.PARAMETER_TOKEN}{this}"

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

    1718    def sessionparameter_sql(self, expression: exp.SessionParameter) -> str:
    1719        this = self.sql(expression, "this")
    1720        kind = expression.text("kind")
    1721        if kind:
    1722            kind = f"{kind}."
    1723        return f"@@{kind}{this}"

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

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

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

    1728    def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str:
    1729        alias = self.sql(expression, "alias")
    1730        alias = f"{sep}{alias}" if alias else ""
    1731
    1732        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1733        pivots = f" {pivots}" if pivots else ""
    1734
    1735        sql = self.query_modifiers(expression, self.wrap(expression), alias, pivots)
    1736        return self.prepend_ctes(expression, sql)

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

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

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

    1742    def union_sql(self, expression: exp.Union) -> str:
    1743        return self.prepend_ctes(
    1744            expression,
    1745            self.set_operation(expression, self.union_op(expression)),
    1746        )

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

    1748    def union_op(self, expression: exp.Union) -> str:
    1749        kind = " DISTINCT" if self.EXPLICIT_UNION else ""
    1750        kind = kind if expression.args.get("distinct") else " ALL"
    1751        return f"UNION{kind}"

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

    1753    def unnest_sql(self, expression: exp.Unnest) -> str:
    1754        args = self.expressions(expression, flat=True)
    1755        alias = expression.args.get("alias")
    1756        if alias and self.UNNEST_COLUMN_ONLY:
    1757            columns = alias.columns
    1758            alias = self.sql(columns[0]) if columns else ""
    1759        else:
    1760            alias = self.sql(expression, "alias")
    1761        alias = f" AS {alias}" if alias else alias
    1762        ordinality = " WITH ORDINALITY" if expression.args.get("ordinality") else ""
    1763        offset = expression.args.get("offset")
    1764        offset = f" WITH OFFSET AS {self.sql(offset)}" if offset else ""
    1765        return f"UNNEST({args}){ordinality}{alias}{offset}"

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

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

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

    1771    def window_sql(self, expression: exp.Window) -> str:
    1772        this = self.sql(expression, "this")
    1773        partition = self.partition_by_sql(expression)
    1774        order = expression.args.get("order")
    1775        order = self.order_sql(order, flat=True) if order else ""
    1776        spec = self.sql(expression, "spec")
    1777        alias = self.sql(expression, "alias")
    1778        over = self.sql(expression, "over") or "OVER"
    1779
    1780        this = f"{this} {'AS' if expression.arg_key == 'windows' else over}"
    1781
    1782        first = expression.args.get("first")
    1783        if first is None:
    1784            first = ""
    1785        else:
    1786            first = "FIRST" if first else "LAST"
    1787
    1788        if not partition and not order and not spec and alias:
    1789            return f"{this} {alias}"
    1790
    1791        args = " ".join(arg for arg in (alias, first, partition, order, spec) if arg)
    1792        return f"{this} ({args})"

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

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

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

    1798    def windowspec_sql(self, expression: exp.WindowSpec) -> str:
    1799        kind = self.sql(expression, "kind")
    1800        start = csv(self.sql(expression, "start"), self.sql(expression, "start_side"), sep=" ")
    1801        end = (
    1802            csv(self.sql(expression, "end"), self.sql(expression, "end_side"), sep=" ")
    1803            or "CURRENT ROW"
    1804        )
    1805        return f"{kind} BETWEEN {start} AND {end}"

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

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

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

    1812    def between_sql(self, expression: exp.Between) -> str:
    1813        this = self.sql(expression, "this")
    1814        low = self.sql(expression, "low")
    1815        high = self.sql(expression, "high")
    1816        return f"{this} BETWEEN {low} AND {high}"

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

    1818    def bracket_sql(self, expression: exp.Bracket) -> str:
    1819        expressions = apply_index_offset(expression.this, expression.expressions, self.INDEX_OFFSET)
    1820        expressions_sql = ", ".join(self.sql(e) for e in expressions)
    1821
    1822        return f"{self.sql(expression, 'this')}[{expressions_sql}]"

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

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

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

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

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

    1830    def any_sql(self, expression: exp.Any) -> str:
    1831        this = self.sql(expression, "this")
    1832        if isinstance(expression.this, exp.Subqueryable):
    1833            this = self.wrap(this)
    1834        return f"ANY {this}"

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

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

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

    1839    def case_sql(self, expression: exp.Case) -> str:
    1840        this = self.sql(expression, "this")
    1841        statements = [f"CASE {this}" if this else "CASE"]
    1842
    1843        for e in expression.args["ifs"]:
    1844            statements.append(f"WHEN {self.sql(e, 'this')}")
    1845            statements.append(f"THEN {self.sql(e, 'true')}")
    1846
    1847        default = self.sql(expression, "default")
    1848
    1849        if default:
    1850            statements.append(f"ELSE {default}")
    1851
    1852        statements.append("END")
    1853
    1854        if self.pretty and self.text_width(statements) > self.max_text_width:
    1855            return self.indent("\n".join(statements), skip_first=True, skip_last=True)
    1856
    1857        return " ".join(statements)

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

    1859    def constraint_sql(self, expression: exp.Constraint) -> str:
    1860        this = self.sql(expression, "this")
    1861        expressions = self.expressions(expression, flat=True)
    1862        return f"CONSTRAINT {this} {expressions}"

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

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

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

    1869    def extract_sql(self, expression: exp.Extract) -> str:
    1870        this = self.sql(expression, "this")
    1871        expression_sql = self.sql(expression, "expression")
    1872        return f"EXTRACT({this} FROM {expression_sql})"

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

    1874    def trim_sql(self, expression: exp.Trim) -> str:
    1875        trim_type = self.sql(expression, "position")
    1876
    1877        if trim_type == "LEADING":
    1878            return self.func("LTRIM", expression.this)
    1879        elif trim_type == "TRAILING":
    1880            return self.func("RTRIM", expression.this)
    1881        else:
    1882            return self.func("TRIM", expression.this, expression.expression)

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

    1884    def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    1885        expressions = expression.expressions
    1886        if self.STRICT_STRING_CONCAT:
    1887            expressions = (exp.cast(e, "text") for e in expressions)
    1888        return self.func("CONCAT", *expressions)

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

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

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

    1894    def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
    1895        expressions = self.expressions(expression, flat=True)
    1896        reference = self.sql(expression, "reference")
    1897        reference = f" {reference}" if reference else ""
    1898        delete = self.sql(expression, "delete")
    1899        delete = f" ON DELETE {delete}" if delete else ""
    1900        update = self.sql(expression, "update")
    1901        update = f" ON UPDATE {update}" if update else ""
    1902        return f"FOREIGN KEY ({expressions}){reference}{delete}{update}"

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

    1904    def primarykey_sql(self, expression: exp.ForeignKey) -> str:
    1905        expressions = self.expressions(expression, flat=True)
    1906        options = self.expressions(expression, key="options", flat=True, sep=" ")
    1907        options = f" {options}" if options else ""
    1908        return f"PRIMARY KEY ({expressions}){options}"

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

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

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

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

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

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

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

    1921    def jsonobject_sql(self, expression: exp.JSONObject) -> str:
    1922        null_handling = expression.args.get("null_handling")
    1923        null_handling = f" {null_handling}" if null_handling else ""
    1924        unique_keys = expression.args.get("unique_keys")
    1925        if unique_keys is not None:
    1926            unique_keys = f" {'WITH' if unique_keys else 'WITHOUT'} UNIQUE KEYS"
    1927        else:
    1928            unique_keys = ""
    1929        return_type = self.sql(expression, "return_type")
    1930        return_type = f" RETURNING {return_type}" if return_type else ""
    1931        format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
    1932        encoding = self.sql(expression, "encoding")
    1933        encoding = f" ENCODING {encoding}" if encoding else ""
    1934        return self.func(
    1935            "JSON_OBJECT",
    1936            *expression.expressions,
    1937            suffix=f"{null_handling}{unique_keys}{return_type}{format_json}{encoding})",
    1938        )

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

    1940    def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
    1941        this = self.sql(expression, "this")
    1942        kind = self.sql(expression, "kind")
    1943        path = self.sql(expression, "path")
    1944        path = f" {path}" if path else ""
    1945        as_json = " AS JSON" if expression.args.get("as_json") else ""
    1946        return f"{this} {kind}{path}{as_json}"

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

    1948    def openjson_sql(self, expression: exp.OpenJSON) -> str:
    1949        this = self.sql(expression, "this")
    1950        path = self.sql(expression, "path")
    1951        path = f", {path}" if path else ""
    1952        expressions = self.expressions(expression)
    1953        with_ = (
    1954            f" WITH ({self.seg(self.indent(expressions), sep='')}{self.seg(')', sep='')}"
    1955            if expressions
    1956            else ""
    1957        )
    1958        return f"OPENJSON({this}{path}){with_}"

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

    1960    def in_sql(self, expression: exp.In) -> str:
    1961        query = expression.args.get("query")
    1962        unnest = expression.args.get("unnest")
    1963        field = expression.args.get("field")
    1964        is_global = " GLOBAL" if expression.args.get("is_global") else ""
    1965
    1966        if query:
    1967            in_sql = self.wrap(query)
    1968        elif unnest:
    1969            in_sql = self.in_unnest_op(unnest)
    1970        elif field:
    1971            in_sql = self.sql(field)
    1972        else:
    1973            in_sql = f"({self.expressions(expression, flat=True)})"
    1974
    1975        return f"{self.sql(expression, 'this')}{is_global} IN {in_sql}"

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

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

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

    1980    def interval_sql(self, expression: exp.Interval) -> str:
    1981        unit = self.sql(expression, "unit")
    1982        if not self.INTERVAL_ALLOWS_PLURAL_FORM:
    1983            unit = self.TIME_PART_SINGULARS.get(unit.lower(), unit)
    1984        unit = f" {unit}" if unit else ""
    1985
    1986        if self.SINGLE_STRING_INTERVAL:
    1987            this = expression.this.name if expression.this else ""
    1988            return f"INTERVAL '{this}{unit}'" if this else f"INTERVAL{unit}"
    1989
    1990        this = self.sql(expression, "this")
    1991        if this:
    1992            unwrapped = isinstance(expression.this, self.UNWRAPPED_INTERVAL_VALUES)
    1993            this = f" {this}" if unwrapped else f" ({this})"
    1994
    1995        return f"INTERVAL{this}{unit}"

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

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

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

    2000    def reference_sql(self, expression: exp.Reference) -> str:
    2001        this = self.sql(expression, "this")
    2002        expressions = self.expressions(expression, flat=True)
    2003        expressions = f"({expressions})" if expressions else ""
    2004        options = self.expressions(expression, key="options", flat=True, sep=" ")
    2005        options = f" {options}" if options else ""
    2006        return f"REFERENCES {this}{expressions}{options}"

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

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

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

    2011    def paren_sql(self, expression: exp.Paren) -> str:
    2012        if isinstance(expression.unnest(), exp.Select):
    2013            sql = self.wrap(expression)
    2014        else:
    2015            sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
    2016            sql = f"({sql}{self.seg(')', sep='')}"
    2017
    2018        return self.prepend_ctes(expression, sql)

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

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

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

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

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

    2029    def alias_sql(self, expression: exp.Alias) -> str:
    2030        alias = self.sql(expression, "alias")
    2031        alias = f" AS {alias}" if alias else ""
    2032        return f"{self.sql(expression, 'this')}{alias}"

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

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

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

    2037    def attimezone_sql(self, expression: exp.AtTimeZone) -> str:
    2038        this = self.sql(expression, "this")
    2039        zone = self.sql(expression, "zone")
    2040        return f"{this} AT TIME ZONE {zone}"

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

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

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

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

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

    2048    def xor_sql(self, expression: exp.And) -> str:
    2049        return self.connector_sql(expression, "XOR")

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

    2051    def connector_sql(self, expression: exp.Connector, op: str) -> str:
    2052        if not self.pretty:
    2053            return self.binary(expression, op)
    2054
    2055        sqls = tuple(
    2056            self.maybe_comment(self.sql(e), e, e.parent.comments or []) if i != 1 else self.sql(e)
    2057            for i, e in enumerate(expression.flatten(unnest=False))
    2058        )
    2059
    2060        sep = "\n" if self.text_width(sqls) > self.max_text_width else " "
    2061        return f"{sep}{op} ".join(sqls)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    2096    def comment_sql(self, expression: exp.Comment) -> str:
    2097        this = self.sql(expression, "this")
    2098        kind = expression.args["kind"]
    2099        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    2100        expression_sql = self.sql(expression, "expression")
    2101        return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"

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

    2103    def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
    2104        this = self.sql(expression, "this")
    2105        delete = " DELETE" if expression.args.get("delete") else ""
    2106        recompress = self.sql(expression, "recompress")
    2107        recompress = f" RECOMPRESS {recompress}" if recompress else ""
    2108        to_disk = self.sql(expression, "to_disk")
    2109        to_disk = f" TO DISK {to_disk}" if to_disk else ""
    2110        to_volume = self.sql(expression, "to_volume")
    2111        to_volume = f" TO VOLUME {to_volume}" if to_volume else ""
    2112        return f"{this}{delete}{recompress}{to_disk}{to_volume}"

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

    2114    def mergetreettl_sql(self, expression: exp.MergeTreeTTL) -> str:
    2115        where = self.sql(expression, "where")
    2116        group = self.sql(expression, "group")
    2117        aggregates = self.expressions(expression, key="aggregates")
    2118        aggregates = self.seg("SET") + self.seg(aggregates) if aggregates else ""
    2119
    2120        if not (where or group or aggregates) and len(expression.expressions) == 1:
    2121            return f"TTL {self.expressions(expression, flat=True)}"
    2122
    2123        return f"TTL{self.seg(self.expressions(expression))}{where}{group}{aggregates}"

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

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

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

    2128    def commit_sql(self, expression: exp.Commit) -> str:
    2129        chain = expression.args.get("chain")
    2130        if chain is not None:
    2131            chain = " AND CHAIN" if chain else " AND NO CHAIN"
    2132
    2133        return f"COMMIT{chain or ''}"

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

    2135    def rollback_sql(self, expression: exp.Rollback) -> str:
    2136        savepoint = expression.args.get("savepoint")
    2137        savepoint = f" TO {savepoint}" if savepoint else ""
    2138        return f"ROLLBACK{savepoint}"

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

    2140    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
    2141        this = self.sql(expression, "this")
    2142
    2143        dtype = self.sql(expression, "dtype")
    2144        if dtype:
    2145            collate = self.sql(expression, "collate")
    2146            collate = f" COLLATE {collate}" if collate else ""
    2147            using = self.sql(expression, "using")
    2148            using = f" USING {using}" if using else ""
    2149            return f"ALTER COLUMN {this} TYPE {dtype}{collate}{using}"
    2150
    2151        default = self.sql(expression, "default")
    2152        if default:
    2153            return f"ALTER COLUMN {this} SET DEFAULT {default}"
    2154
    2155        if not expression.args.get("drop"):
    2156            self.unsupported("Unsupported ALTER COLUMN syntax")
    2157
    2158        return f"ALTER COLUMN {this} DROP DEFAULT"

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

    2160    def renametable_sql(self, expression: exp.RenameTable) -> str:
    2161        if not self.RENAME_TABLE_WITH_DB:
    2162            # Remove db from tables
    2163            expression = expression.transform(
    2164                lambda n: exp.table_(n.this) if isinstance(n, exp.Table) else n
    2165            )
    2166        this = self.sql(expression, "this")
    2167        return f"RENAME TO {this}"

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

    2169    def altertable_sql(self, expression: exp.AlterTable) -> str:
    2170        actions = expression.args["actions"]
    2171
    2172        if isinstance(actions[0], exp.ColumnDef):
    2173            actions = self.expressions(expression, key="actions", prefix="ADD COLUMN ")
    2174        elif isinstance(actions[0], exp.Schema):
    2175            actions = self.expressions(expression, key="actions", prefix="ADD COLUMNS ")
    2176        elif isinstance(actions[0], exp.Delete):
    2177            actions = self.expressions(expression, key="actions", flat=True)
    2178        else:
    2179            actions = self.expressions(expression, key="actions")
    2180
    2181        exists = " IF EXISTS" if expression.args.get("exists") else ""
    2182        return f"ALTER TABLE{exists} {self.sql(expression, 'this')} {actions}"

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

    2184    def droppartition_sql(self, expression: exp.DropPartition) -> str:
    2185        expressions = self.expressions(expression)
    2186        exists = " IF EXISTS " if expression.args.get("exists") else " "
    2187        return f"DROP{exists}{expressions}"

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

    2189    def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
    2190        this = self.sql(expression, "this")
    2191        expression_ = self.sql(expression, "expression")
    2192        add_constraint = f"ADD CONSTRAINT {this}" if this else "ADD"
    2193
    2194        enforced = expression.args.get("enforced")
    2195        if enforced is not None:
    2196            return f"{add_constraint} CHECK ({expression_}){' ENFORCED' if enforced else ''}"
    2197
    2198        return f"{add_constraint} {expression_}"

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

    2200    def distinct_sql(self, expression: exp.Distinct) -> str:
    2201        this = self.expressions(expression, flat=True)
    2202        this = f" {this}" if this else ""
    2203
    2204        on = self.sql(expression, "on")
    2205        on = f" ON {on}" if on else ""
    2206        return f"DISTINCT{this}{on}"

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

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

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

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

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

    2214    def intdiv_sql(self, expression: exp.IntDiv) -> str:
    2215        return self.sql(
    2216            exp.Cast(
    2217                this=exp.Div(this=expression.this, expression=expression.expression),
    2218                to=exp.DataType(this=exp.DataType.Type.INT),
    2219            )
    2220        )

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

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

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

    2225    def safedpipe_sql(self, expression: exp.SafeDPipe) -> str:
    2226        if self.STRICT_STRING_CONCAT:
    2227            return self.func("CONCAT", *(exp.cast(e, "text") for e in expression.flatten()))
    2228        return self.dpipe_sql(expression)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    2263    def is_sql(self, expression: exp.Is) -> str:
    2264        if not self.IS_BOOL_ALLOWED and isinstance(expression.expression, exp.Boolean):
    2265            return self.sql(
    2266                expression.this if expression.expression.this else exp.not_(expression.this)
    2267            )
    2268        return self.binary(expression, "IS")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    2312    def use_sql(self, expression: exp.Use) -> str:
    2313        kind = self.sql(expression, "kind")
    2314        kind = f" {kind}" if kind else ""
    2315        this = self.sql(expression, "this")
    2316        this = f" {this}" if this else ""
    2317        return f"USE{kind}{this}"

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

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

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

    2323    def function_fallback_sql(self, expression: exp.Func) -> str:
    2324        args = []
    2325
    2326        for key in expression.arg_types:
    2327            arg_value = expression.args.get(key)
    2328
    2329            if isinstance(arg_value, list):
    2330                for value in arg_value:
    2331                    args.append(value)
    2332            elif arg_value is not None:
    2333                args.append(arg_value)
    2334
    2335        return self.func(expression.sql_name(), *args)

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

    2337    def func(
    2338        self,
    2339        name: str,
    2340        *args: t.Optional[exp.Expression | str],
    2341        prefix: str = "(",
    2342        suffix: str = ")",
    2343    ) -> str:
    2344        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

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

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

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

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

    2355    def format_time(self, expression: exp.Expression) -> t.Optional[str]:
    2356        return format_time(
    2357            self.sql(expression, "format"), self.INVERSE_TIME_MAPPING, self.INVERSE_TIME_TRIE
    2358        )

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

    2360    def expressions(
    2361        self,
    2362        expression: t.Optional[exp.Expression] = None,
    2363        key: t.Optional[str] = None,
    2364        sqls: t.Optional[t.List[str]] = None,
    2365        flat: bool = False,
    2366        indent: bool = True,
    2367        skip_first: bool = False,
    2368        sep: str = ", ",
    2369        prefix: str = "",
    2370    ) -> str:
    2371        expressions = expression.args.get(key or "expressions") if expression else sqls
    2372
    2373        if not expressions:
    2374            return ""
    2375
    2376        if flat:
    2377            return sep.join(self.sql(e) for e in expressions)
    2378
    2379        num_sqls = len(expressions)
    2380
    2381        # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
    2382        pad = " " * self.pad
    2383        stripped_sep = sep.strip()
    2384
    2385        result_sqls = []
    2386        for i, e in enumerate(expressions):
    2387            sql = self.sql(e, comment=False)
    2388            comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
    2389
    2390            if self.pretty:
    2391                if self.leading_comma:
    2392                    result_sqls.append(f"{sep if i > 0 else pad}{prefix}{sql}{comments}")
    2393                else:
    2394                    result_sqls.append(
    2395                        f"{prefix}{sql}{stripped_sep if i + 1 < num_sqls else ''}{comments}"
    2396                    )
    2397            else:
    2398                result_sqls.append(f"{prefix}{sql}{comments}{sep if i + 1 < num_sqls else ''}")
    2399
    2400        result_sql = "\n".join(result_sqls) if self.pretty else "".join(result_sqls)
    2401        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

    2403    def op_expressions(self, op: str, expression: exp.Expression, flat: bool = False) -> str:
    2404        flat = flat or isinstance(expression.parent, exp.Properties)
    2405        expressions_sql = self.expressions(expression, flat=flat)
    2406        if flat:
    2407            return f"{op} {expressions_sql}"
    2408        return f"{self.seg(op)}{self.sep() if expressions_sql else ''}{expressions_sql}"

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

    2410    def naked_property(self, expression: exp.Property) -> str:
    2411        property_name = exp.Properties.PROPERTY_TO_NAME.get(expression.__class__)
    2412        if not property_name:
    2413            self.unsupported(f"Unsupported property {expression.__class__.__name__}")
    2414        return f"{property_name} {self.sql(expression, 'this')}"

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

    2416    def set_operation(self, expression: exp.Expression, op: str) -> str:
    2417        this = self.sql(expression, "this")
    2418        op = self.seg(op)
    2419        return self.query_modifiers(
    2420            expression, f"{this}{op}{self.sep()}{self.sql(expression, 'expression')}"
    2421        )

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

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

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

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

    2429    def userdefinedfunction_sql(self, expression: exp.UserDefinedFunction) -> str:
    2430        this = self.sql(expression, "this")
    2431        expressions = self.no_identify(self.expressions, expression)
    2432        expressions = (
    2433            self.wrap(expressions) if expression.args.get("wrapped") else f" {expressions}"
    2434        )
    2435        return f"{this}{expressions}"

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

    2437    def joinhint_sql(self, expression: exp.JoinHint) -> str:
    2438        this = self.sql(expression, "this")
    2439        expressions = self.expressions(expression, flat=True)
    2440        return f"{this}({expressions})"

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

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

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

    2445    def when_sql(self, expression: exp.When) -> str:
    2446        matched = "MATCHED" if expression.args["matched"] else "NOT MATCHED"
    2447        source = " BY SOURCE" if self.MATCHED_BY_SOURCE and expression.args.get("source") else ""
    2448        condition = self.sql(expression, "condition")
    2449        condition = f" AND {condition}" if condition else ""
    2450
    2451        then_expression = expression.args.get("then")
    2452        if isinstance(then_expression, exp.Insert):
    2453            then = f"INSERT {self.sql(then_expression, 'this')}"
    2454            if "expression" in then_expression.args:
    2455                then += f" VALUES {self.sql(then_expression, 'expression')}"
    2456        elif isinstance(then_expression, exp.Update):
    2457            if isinstance(then_expression.args.get("expressions"), exp.Star):
    2458                then = f"UPDATE {self.sql(then_expression, 'expressions')}"
    2459            else:
    2460                then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
    2461        else:
    2462            then = self.sql(then_expression)
    2463        return f"WHEN {matched}{source}{condition} THEN {then}"

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

    2465    def merge_sql(self, expression: exp.Merge) -> str:
    2466        this = self.sql(expression, "this")
    2467        using = f"USING {self.sql(expression, 'using')}"
    2468        on = f"ON {self.sql(expression, 'on')}"
    2469        return f"MERGE INTO {this} {using} {on} {self.expressions(expression, sep=' ')}"

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

    2471    def tochar_sql(self, expression: exp.ToChar) -> str:
    2472        if expression.args.get("format"):
    2473            self.unsupported("Format argument unsupported for TO_CHAR/TO_VARCHAR function")
    2474
    2475        return self.sql(exp.cast(expression.this, "text"))

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

    2477    def dictproperty_sql(self, expression: exp.DictProperty) -> str:
    2478        this = self.sql(expression, "this")
    2479        kind = self.sql(expression, "kind")
    2480        settings_sql = self.expressions(expression, key="settings", sep=" ")
    2481        args = f"({self.sep('')}{settings_sql}{self.seg(')', sep='')}" if settings_sql else "()"
    2482        return f"{this}({kind}{args})"

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

    2484    def dictrange_sql(self, expression: exp.DictRange) -> str:
    2485        this = self.sql(expression, "this")
    2486        max = self.sql(expression, "max")
    2487        min = self.sql(expression, "min")
    2488        return f"{this}(MIN {min} MAX {max})"

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

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

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

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

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

    2496    def clusteredbyproperty_sql(self, expression: exp.ClusteredByProperty) -> str:
    2497        expressions = self.expressions(expression, key="expressions", flat=True)
    2498        sorted_by = self.expressions(expression, key="sorted_by", flat=True)
    2499        sorted_by = f" SORTED BY ({sorted_by})" if sorted_by else ""
    2500        buckets = self.sql(expression, "buckets")
    2501        return f"CLUSTERED BY ({expressions}){sorted_by} INTO {buckets} BUCKETS"

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

    2503    def anyvalue_sql(self, expression: exp.AnyValue) -> str:
    2504        this = self.sql(expression, "this")
    2505        having = self.sql(expression, "having")
    2506
    2507        if having:
    2508            this = f"{this} HAVING {'MAX' if expression.args.get('max') else 'MIN'} {having}"
    2509
    2510        return self.func("ANY_VALUE", this)

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

    2513def cached_generator(
    2514    cache: t.Optional[t.Dict[int, str]] = None
    2515) -> t.Callable[[exp.Expression], str]:
    2516    """Returns a cached generator."""
    2517    cache = {} if cache is None else cache
    2518    generator = Generator(normalize=True, identify="safe")
    2519    return lambda e: generator.generate(e, cache)

Returns a cached generator.