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

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

    310    def __init__(
    311        self,
    312        pretty: t.Optional[bool] = None,
    313        identify: str | bool = False,
    314        normalize: bool = False,
    315        pad: int = 2,
    316        indent: int = 2,
    317        normalize_functions: t.Optional[str | bool] = None,
    318        unsupported_level: ErrorLevel = ErrorLevel.WARN,
    319        max_unsupported: int = 3,
    320        leading_comma: bool = False,
    321        max_text_width: int = 80,
    322        comments: bool = True,
    323    ):
    324        import sqlglot
    325
    326        self.pretty = pretty if pretty is not None else sqlglot.pretty
    327        self.identify = identify
    328        self.normalize = normalize
    329        self.pad = pad
    330        self._indent = indent
    331        self.unsupported_level = unsupported_level
    332        self.max_unsupported = max_unsupported
    333        self.leading_comma = leading_comma
    334        self.max_text_width = max_text_width
    335        self.comments = comments
    336
    337        # This is both a Dialect property and a Generator argument, so we prioritize the latter
    338        self.normalize_functions = (
    339            self.NORMALIZE_FUNCTIONS if normalize_functions is None else normalize_functions
    340        )
    341
    342        self.unsupported_messages: t.List[str] = []
    343        self._escaped_quote_end: str = self.STRING_ESCAPE + self.QUOTE_END
    344        self._escaped_identifier_end: str = self.IDENTIFIER_ESCAPE + self.IDENTIFIER_END
    345        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
IS_BOOL_ALLOWED = True
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.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.Select'>, <class 'sqlglot.expressions.From'>, <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'
@classmethod
def can_identify(text: str, identify: str | bool = 'safe') -> bool: View Source

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

    347    def generate(
    348        self,
    349        expression: t.Optional[exp.Expression],
    350        cache: t.Optional[t.Dict[int, str]] = None,
    351    ) -> str:
    352        """
    353        Generates the SQL string corresponding to the given syntax tree.
    354
    355        Args:
    356            expression: The syntax tree.
    357            cache: An optional sql string cache. This leverages the hash of an Expression
    358                which can be slow to compute, so only use it if you set _hash on each node.
    359
    360        Returns:
    361            The SQL string corresponding to `expression`.
    362        """
    363        if cache is not None:
    364            self._cache = cache
    365
    366        self.unsupported_messages = []
    367        sql = self.sql(expression).strip()
    368        self._cache = None
    369
    370        if self.unsupported_level == ErrorLevel.IGNORE:
    371            return sql
    372
    373        if self.unsupported_level == ErrorLevel.WARN:
    374            for msg in self.unsupported_messages:
    375                logger.warning(msg)
    376        elif self.unsupported_level == ErrorLevel.RAISE and self.unsupported_messages:
    377            raise UnsupportedError(concat_messages(self.unsupported_messages, self.max_unsupported))
    378
    379        if self.pretty:
    380            sql = sql.replace(self.SENTINEL_LINE_BREAK, "\n")
    381        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

    383    def unsupported(self, message: str) -> None:
    384        if self.unsupported_level == ErrorLevel.IMMEDIATE:
    385            raise UnsupportedError(message)
    386        self.unsupported_messages.append(message)

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

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

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

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

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

    394    def pad_comment(self, comment: str) -> str:
    395        comment = " " + comment if comment[0].strip() else comment
    396        comment = comment + " " if comment[-1].strip() else comment
    397        return comment

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

    399    def maybe_comment(
    400        self,
    401        sql: str,
    402        expression: t.Optional[exp.Expression] = None,
    403        comments: t.Optional[t.List[str]] = None,
    404    ) -> str:
    405        comments = (
    406            ((expression and expression.comments) if comments is None else comments)  # type: ignore
    407            if self.comments
    408            else None
    409        )
    410
    411        if not comments or isinstance(expression, exp.Binary):
    412            return sql
    413
    414        sep = "\n" if self.pretty else " "
    415        comments_sql = sep.join(
    416            f"/*{self.pad_comment(comment)}*/" for comment in comments if comment
    417        )
    418
    419        if not comments_sql:
    420            return sql
    421
    422        if isinstance(expression, self.WITH_SEPARATED_COMMENTS):
    423            return (
    424                f"{self.sep()}{comments_sql}{sql}"
    425                if sql[0].isspace()
    426                else f"{comments_sql}{self.sep()}{sql}"
    427            )
    428
    429        return f"{sql} {comments_sql}"

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

    431    def wrap(self, expression: exp.Expression | str) -> str:
    432        this_sql = self.indent(
    433            self.sql(expression)
    434            if isinstance(expression, (exp.Select, exp.Union))
    435            else self.sql(expression, "this"),
    436            level=1,
    437            pad=0,
    438        )
    439        return f"({self.sep('')}{this_sql}{self.seg(')', sep='')}"

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

    441    def no_identify(self, func: t.Callable[..., str], *args, **kwargs) -> str:
    442        original = self.identify
    443        self.identify = False
    444        result = func(*args, **kwargs)
    445        self.identify = original
    446        return result

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

    448    def normalize_func(self, name: str) -> str:
    449        if self.normalize_functions == "upper" or self.normalize_functions is True:
    450            return name.upper()
    451        if self.normalize_functions == "lower":
    452            return name.lower()
    453        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

    455    def indent(
    456        self,
    457        sql: str,
    458        level: int = 0,
    459        pad: t.Optional[int] = None,
    460        skip_first: bool = False,
    461        skip_last: bool = False,
    462    ) -> str:
    463        if not self.pretty:
    464            return sql
    465
    466        pad = self.pad if pad is None else pad
    467        lines = sql.split("\n")
    468
    469        return "\n".join(
    470            line
    471            if (skip_first and i == 0) or (skip_last and i == len(lines) - 1)
    472            else f"{' ' * (level * self._indent + pad)}{line}"
    473            for i, line in enumerate(lines)
    474        )

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

    476    def sql(
    477        self,
    478        expression: t.Optional[str | exp.Expression],
    479        key: t.Optional[str] = None,
    480        comment: bool = True,
    481    ) -> str:
    482        if not expression:
    483            return ""
    484
    485        if isinstance(expression, str):
    486            return expression
    487
    488        if key:
    489            return self.sql(expression.args.get(key))
    490
    491        if self._cache is not None:
    492            expression_id = hash(expression)
    493
    494            if expression_id in self._cache:
    495                return self._cache[expression_id]
    496
    497        transform = self.TRANSFORMS.get(expression.__class__)
    498
    499        if callable(transform):
    500            sql = transform(self, expression)
    501        elif transform:
    502            sql = transform
    503        elif isinstance(expression, exp.Expression):
    504            exp_handler_name = f"{expression.key}_sql"
    505
    506            if hasattr(self, exp_handler_name):
    507                sql = getattr(self, exp_handler_name)(expression)
    508            elif isinstance(expression, exp.Func):
    509                sql = self.function_fallback_sql(expression)
    510            elif isinstance(expression, exp.Property):
    511                sql = self.property_sql(expression)
    512            else:
    513                raise ValueError(f"Unsupported expression type {expression.__class__.__name__}")
    514        else:
    515            raise ValueError(f"Expected an Expression. Received {type(expression)}: {expression}")
    516
    517        sql = self.maybe_comment(sql, expression) if self.comments and comment else sql
    518
    519        if self._cache is not None:
    520            self._cache[expression_id] = sql
    521        return sql

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

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

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

    528    def cache_sql(self, expression: exp.Cache) -> str:
    529        lazy = " LAZY" if expression.args.get("lazy") else ""
    530        table = self.sql(expression, "this")
    531        options = expression.args.get("options")
    532        options = f" OPTIONS({self.sql(options[0])} = {self.sql(options[1])})" if options else ""
    533        sql = self.sql(expression, "expression")
    534        sql = f" AS{self.sep()}{sql}" if sql else ""
    535        sql = f"CACHE{lazy} TABLE {table}{options}{sql}"
    536        return self.prepend_ctes(expression, sql)

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

    538    def characterset_sql(self, expression: exp.CharacterSet) -> str:
    539        if isinstance(expression.parent, exp.Cast):
    540            return f"CHAR CHARACTER SET {self.sql(expression, 'this')}"
    541        default = "DEFAULT " if expression.args.get("default") else ""
    542        return f"{default}CHARACTER SET={self.sql(expression, 'this')}"

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

    544    def column_sql(self, expression: exp.Column) -> str:
    545        return ".".join(
    546            self.sql(part)
    547            for part in (
    548                expression.args.get("catalog"),
    549                expression.args.get("db"),
    550                expression.args.get("table"),
    551                expression.args.get("this"),
    552            )
    553            if part
    554        )

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

    556    def columnposition_sql(self, expression: exp.ColumnPosition) -> str:
    557        this = self.sql(expression, "this")
    558        this = f" {this}" if this else ""
    559        position = self.sql(expression, "position")
    560        return f"{position}{this}"

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

    562    def columndef_sql(self, expression: exp.ColumnDef, sep: str = " ") -> str:
    563        column = self.sql(expression, "this")
    564        kind = self.sql(expression, "kind")
    565        constraints = self.expressions(expression, key="constraints", sep=" ", flat=True)
    566        exists = "IF NOT EXISTS " if expression.args.get("exists") else ""
    567        kind = f"{sep}{kind}" if kind else ""
    568        constraints = f" {constraints}" if constraints else ""
    569        position = self.sql(expression, "position")
    570        position = f" {position}" if position else ""
    571
    572        return f"{exists}{column}{kind}{constraints}{position}"

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

    574    def columnconstraint_sql(self, expression: exp.ColumnConstraint) -> str:
    575        this = self.sql(expression, "this")
    576        kind_sql = self.sql(expression, "kind").strip()
    577        return f"CONSTRAINT {this} {kind_sql}" if this else kind_sql

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

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

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

    582    def compresscolumnconstraint_sql(self, expression: exp.CompressColumnConstraint) -> str:
    583        if isinstance(expression.this, list):
    584            this = self.wrap(self.expressions(expression, key="this", flat=True))
    585        else:
    586            this = self.sql(expression, "this")
    587
    588        return f"COMPRESS {this}"

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

    590    def generatedasidentitycolumnconstraint_sql(
    591        self, expression: exp.GeneratedAsIdentityColumnConstraint
    592    ) -> str:
    593        this = ""
    594        if expression.this is not None:
    595            on_null = "ON NULL " if expression.args.get("on_null") else ""
    596            this = " ALWAYS " if expression.this else f" BY DEFAULT {on_null}"
    597
    598        start = expression.args.get("start")
    599        start = f"START WITH {start}" if start else ""
    600        increment = expression.args.get("increment")
    601        increment = f" INCREMENT BY {increment}" if increment else ""
    602        minvalue = expression.args.get("minvalue")
    603        minvalue = f" MINVALUE {minvalue}" if minvalue else ""
    604        maxvalue = expression.args.get("maxvalue")
    605        maxvalue = f" MAXVALUE {maxvalue}" if maxvalue else ""
    606        cycle = expression.args.get("cycle")
    607        cycle_sql = ""
    608
    609        if cycle is not None:
    610            cycle_sql = f"{' NO' if not cycle else ''} CYCLE"
    611            cycle_sql = cycle_sql.strip() if not start and not increment else cycle_sql
    612
    613        sequence_opts = ""
    614        if start or increment or cycle_sql:
    615            sequence_opts = f"{start}{increment}{minvalue}{maxvalue}{cycle_sql}"
    616            sequence_opts = f" ({sequence_opts.strip()})"
    617
    618        expr = self.sql(expression, "expression")
    619        expr = f"({expr})" if expr else "IDENTITY"
    620
    621        return f"GENERATED{this}AS {expr}{sequence_opts}"

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

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

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

    626    def primarykeycolumnconstraint_sql(self, expression: exp.PrimaryKeyColumnConstraint) -> str:
    627        desc = expression.args.get("desc")
    628        if desc is not None:
    629            return f"PRIMARY KEY{' DESC' if desc else ' ASC'}"
    630        return f"PRIMARY KEY"

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

    632    def uniquecolumnconstraint_sql(self, expression: exp.UniqueColumnConstraint) -> str:
    633        this = self.sql(expression, "this")
    634        this = f" {this}" if this else ""
    635        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

    637    def createable_sql(
    638        self, expression: exp.Create, locations: dict[exp.Properties.Location, list[exp.Property]]
    639    ) -> str:
    640        return self.sql(expression, "this")

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

    642    def create_sql(self, expression: exp.Create) -> str:
    643        kind = self.sql(expression, "kind").upper()
    644        properties = expression.args.get("properties")
    645        properties_locs = self.locate_properties(properties) if properties else {}
    646
    647        this = self.createable_sql(expression, properties_locs)
    648
    649        properties_sql = ""
    650        if properties_locs.get(exp.Properties.Location.POST_SCHEMA) or properties_locs.get(
    651            exp.Properties.Location.POST_WITH
    652        ):
    653            properties_sql = self.sql(
    654                exp.Properties(
    655                    expressions=[
    656                        *properties_locs[exp.Properties.Location.POST_SCHEMA],
    657                        *properties_locs[exp.Properties.Location.POST_WITH],
    658                    ]
    659                )
    660            )
    661
    662        begin = " BEGIN" if expression.args.get("begin") else ""
    663        expression_sql = self.sql(expression, "expression")
    664        if expression_sql:
    665            expression_sql = f"{begin}{self.sep()}{expression_sql}"
    666
    667            if self.CREATE_FUNCTION_RETURN_AS or not isinstance(expression.expression, exp.Return):
    668                if properties_locs.get(exp.Properties.Location.POST_ALIAS):
    669                    postalias_props_sql = self.properties(
    670                        exp.Properties(
    671                            expressions=properties_locs[exp.Properties.Location.POST_ALIAS]
    672                        ),
    673                        wrapped=False,
    674                    )
    675                    expression_sql = f" AS {postalias_props_sql}{expression_sql}"
    676                else:
    677                    expression_sql = f" AS{expression_sql}"
    678
    679        postindex_props_sql = ""
    680        if properties_locs.get(exp.Properties.Location.POST_INDEX):
    681            postindex_props_sql = self.properties(
    682                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_INDEX]),
    683                wrapped=False,
    684                prefix=" ",
    685            )
    686
    687        indexes = self.expressions(expression, key="indexes", indent=False, sep=" ")
    688        indexes = f" {indexes}" if indexes else ""
    689        index_sql = indexes + postindex_props_sql
    690
    691        replace = " OR REPLACE" if expression.args.get("replace") else ""
    692        unique = " UNIQUE" if expression.args.get("unique") else ""
    693
    694        postcreate_props_sql = ""
    695        if properties_locs.get(exp.Properties.Location.POST_CREATE):
    696            postcreate_props_sql = self.properties(
    697                exp.Properties(expressions=properties_locs[exp.Properties.Location.POST_CREATE]),
    698                sep=" ",
    699                prefix=" ",
    700                wrapped=False,
    701            )
    702
    703        modifiers = "".join((replace, unique, postcreate_props_sql))
    704
    705        postexpression_props_sql = ""
    706        if properties_locs.get(exp.Properties.Location.POST_EXPRESSION):
    707            postexpression_props_sql = self.properties(
    708                exp.Properties(
    709                    expressions=properties_locs[exp.Properties.Location.POST_EXPRESSION]
    710                ),
    711                sep=" ",
    712                prefix=" ",
    713                wrapped=False,
    714            )
    715
    716        exists_sql = " IF NOT EXISTS" if expression.args.get("exists") else ""
    717        no_schema_binding = (
    718            " WITH NO SCHEMA BINDING" if expression.args.get("no_schema_binding") else ""
    719        )
    720
    721        clone = self.sql(expression, "clone")
    722        clone = f" {clone}" if clone else ""
    723
    724        expression_sql = f"CREATE{modifiers} {kind}{exists_sql} {this}{properties_sql}{expression_sql}{postexpression_props_sql}{index_sql}{no_schema_binding}{clone}"
    725        return self.prepend_ctes(expression, expression_sql)

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

    727    def clone_sql(self, expression: exp.Clone) -> str:
    728        this = self.sql(expression, "this")
    729        when = self.sql(expression, "when")
    730
    731        if when:
    732            kind = self.sql(expression, "kind")
    733            expr = self.sql(expression, "expression")
    734            return f"CLONE {this} {when} ({kind} => {expr})"
    735
    736        return f"CLONE {this}"

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

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

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

    741    def prepend_ctes(self, expression: exp.Expression, sql: str) -> str:
    742        with_ = self.sql(expression, "with")
    743        if with_:
    744            sql = f"{with_}{self.sep()}{sql}"
    745        return sql

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

    747    def with_sql(self, expression: exp.With) -> str:
    748        sql = self.expressions(expression, flat=True)
    749        recursive = "RECURSIVE " if expression.args.get("recursive") else ""
    750
    751        return f"WITH {recursive}{sql}"

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

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

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

    757    def tablealias_sql(self, expression: exp.TableAlias) -> str:
    758        alias = self.sql(expression, "this")
    759        columns = self.expressions(expression, key="columns", flat=True)
    760        columns = f"({columns})" if columns else ""
    761        return f"{alias}{columns}"

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

    763    def bitstring_sql(self, expression: exp.BitString) -> str:
    764        this = self.sql(expression, "this")
    765        if self.BIT_START:
    766            return f"{self.BIT_START}{this}{self.BIT_END}"
    767        return f"{int(this, 2)}"

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

    769    def hexstring_sql(self, expression: exp.HexString) -> str:
    770        this = self.sql(expression, "this")
    771        if self.HEX_START:
    772            return f"{self.HEX_START}{this}{self.HEX_END}"
    773        return f"{int(this, 16)}"

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

    775    def bytestring_sql(self, expression: exp.ByteString) -> str:
    776        this = self.sql(expression, "this")
    777        if self.BYTE_START:
    778            return f"{self.BYTE_START}{this}{self.BYTE_END}"
    779        return this

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

    781    def rawstring_sql(self, expression: exp.RawString) -> str:
    782        string = expression.this
    783        if self.RAW_START:
    784            return f"{self.RAW_START}{self.escape_str(expression.this)}{self.RAW_END}"
    785        string = self.escape_str(string.replace("\\", "\\\\"))
    786        return f"{self.QUOTE_START}{string}{self.QUOTE_END}"

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

    788    def datatypesize_sql(self, expression: exp.DataTypeSize) -> str:
    789        this = self.sql(expression, "this")
    790        specifier = self.sql(expression, "expression")
    791        specifier = f" {specifier}" if specifier else ""
    792        return f"{this}{specifier}"

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

    794    def datatype_sql(self, expression: exp.DataType) -> str:
    795        type_value = expression.this
    796        type_sql = self.TYPE_MAPPING.get(type_value, type_value.value)
    797        nested = ""
    798        interior = self.expressions(expression, flat=True)
    799        values = ""
    800        if interior:
    801            if expression.args.get("nested"):
    802                nested = f"{self.STRUCT_DELIMITER[0]}{interior}{self.STRUCT_DELIMITER[1]}"
    803                if expression.args.get("values") is not None:
    804                    delimiters = ("[", "]") if type_value == exp.DataType.Type.ARRAY else ("(", ")")
    805                    values = self.expressions(expression, key="values", flat=True)
    806                    values = f"{delimiters[0]}{values}{delimiters[1]}"
    807            else:
    808                nested = f"({interior})"
    809
    810        return f"{type_sql}{nested}{values}"

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

    812    def directory_sql(self, expression: exp.Directory) -> str:
    813        local = "LOCAL " if expression.args.get("local") else ""
    814        row_format = self.sql(expression, "row_format")
    815        row_format = f" {row_format}" if row_format else ""
    816        return f"{local}DIRECTORY {self.sql(expression, 'this')}{row_format}"

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

    818    def delete_sql(self, expression: exp.Delete) -> str:
    819        this = self.sql(expression, "this")
    820        this = f" FROM {this}" if this else ""
    821        using_sql = (
    822            f" USING {self.expressions(expression, key='using', sep=', USING ')}"
    823            if expression.args.get("using")
    824            else ""
    825        )
    826        where_sql = self.sql(expression, "where")
    827        returning = self.sql(expression, "returning")
    828        limit = self.sql(expression, "limit")
    829        sql = f"DELETE{this}{using_sql}{where_sql}{returning}{limit}"
    830        return self.prepend_ctes(expression, sql)

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

    832    def drop_sql(self, expression: exp.Drop) -> str:
    833        this = self.sql(expression, "this")
    834        kind = expression.args["kind"]
    835        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    836        temporary = " TEMPORARY" if expression.args.get("temporary") else ""
    837        materialized = " MATERIALIZED" if expression.args.get("materialized") else ""
    838        cascade = " CASCADE" if expression.args.get("cascade") else ""
    839        constraints = " CONSTRAINTS" if expression.args.get("constraints") else ""
    840        purge = " PURGE" if expression.args.get("purge") else ""
    841        return (
    842            f"DROP{temporary}{materialized} {kind}{exists_sql}{this}{cascade}{constraints}{purge}"
    843        )

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

    845    def except_sql(self, expression: exp.Except) -> str:
    846        return self.prepend_ctes(
    847            expression,
    848            self.set_operation(expression, self.except_op(expression)),
    849        )

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

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

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

    854    def fetch_sql(self, expression: exp.Fetch) -> str:
    855        direction = expression.args.get("direction")
    856        direction = f" {direction.upper()}" if direction else ""
    857        count = expression.args.get("count")
    858        count = f" {count}" if count else ""
    859        if expression.args.get("percent"):
    860            count = f"{count} PERCENT"
    861        with_ties_or_only = "WITH TIES" if expression.args.get("with_ties") else "ONLY"
    862        return f"{self.seg('FETCH')}{direction}{count} ROWS {with_ties_or_only}"

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

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

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

    869    def hint_sql(self, expression: exp.Hint) -> str:
    870        if self.sql(expression, "this"):
    871            self.unsupported("Hints are not supported")
    872        return ""

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

    874    def index_sql(self, expression: exp.Index) -> str:
    875        unique = "UNIQUE " if expression.args.get("unique") else ""
    876        primary = "PRIMARY " if expression.args.get("primary") else ""
    877        amp = "AMP " if expression.args.get("amp") else ""
    878        name = f"{expression.name} " if expression.name else ""
    879        table = self.sql(expression, "table")
    880        table = f"{self.INDEX_ON} {table} " if table else ""
    881        using = self.sql(expression, "using")
    882        using = f"USING {using} " if using else ""
    883        index = "INDEX " if not table else ""
    884        columns = self.expressions(expression, key="columns", flat=True)
    885        columns = f"({columns})" if columns else ""
    886        partition_by = self.expressions(expression, key="partition_by", flat=True)
    887        partition_by = f" PARTITION BY {partition_by}" if partition_by else ""
    888        return f"{unique}{primary}{amp}{index}{name}{table}{using}{columns}{partition_by}"

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

    890    def identifier_sql(self, expression: exp.Identifier) -> str:
    891        text = expression.name
    892        lower = text.lower()
    893        text = lower if self.normalize and not expression.quoted else text
    894        text = text.replace(self.IDENTIFIER_END, self._escaped_identifier_end)
    895        if (
    896            expression.quoted
    897            or self.can_identify(text, self.identify)
    898            or lower in self.RESERVED_KEYWORDS
    899            or (not self.IDENTIFIERS_CAN_START_WITH_DIGIT and text[:1].isdigit())
    900        ):
    901            text = f"{self.IDENTIFIER_START}{text}{self.IDENTIFIER_END}"
    902        return text

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

    904    def inputoutputformat_sql(self, expression: exp.InputOutputFormat) -> str:
    905        input_format = self.sql(expression, "input_format")
    906        input_format = f"INPUTFORMAT {input_format}" if input_format else ""
    907        output_format = self.sql(expression, "output_format")
    908        output_format = f"OUTPUTFORMAT {output_format}" if output_format else ""
    909        return self.sep().join((input_format, output_format))

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

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

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

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

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

    918    def properties_sql(self, expression: exp.Properties) -> str:
    919        root_properties = []
    920        with_properties = []
    921
    922        for p in expression.expressions:
    923            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    924            if p_loc == exp.Properties.Location.POST_WITH:
    925                with_properties.append(p)
    926            elif p_loc == exp.Properties.Location.POST_SCHEMA:
    927                root_properties.append(p)
    928
    929        return self.root_properties(
    930            exp.Properties(expressions=root_properties)
    931        ) + self.with_properties(exp.Properties(expressions=with_properties))

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

    933    def root_properties(self, properties: exp.Properties) -> str:
    934        if properties.expressions:
    935            return self.sep() + self.expressions(properties, indent=False, sep=" ")
    936        return ""

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

    938    def properties(
    939        self,
    940        properties: exp.Properties,
    941        prefix: str = "",
    942        sep: str = ", ",
    943        suffix: str = "",
    944        wrapped: bool = True,
    945    ) -> str:
    946        if properties.expressions:
    947            expressions = self.expressions(properties, sep=sep, indent=False)
    948            expressions = self.wrap(expressions) if wrapped else expressions
    949            return f"{prefix}{' ' if prefix and prefix != ' ' else ''}{expressions}{suffix}"
    950        return ""

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

    952    def with_properties(self, properties: exp.Properties) -> str:
    953        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

    955    def locate_properties(
    956        self, properties: exp.Properties
    957    ) -> t.Dict[exp.Properties.Location, list[exp.Property]]:
    958        properties_locs: t.Dict[exp.Properties.Location, list[exp.Property]] = {
    959            key: [] for key in exp.Properties.Location
    960        }
    961
    962        for p in properties.expressions:
    963            p_loc = self.PROPERTIES_LOCATION[p.__class__]
    964            if p_loc == exp.Properties.Location.POST_NAME:
    965                properties_locs[exp.Properties.Location.POST_NAME].append(p)
    966            elif p_loc == exp.Properties.Location.POST_INDEX:
    967                properties_locs[exp.Properties.Location.POST_INDEX].append(p)
    968            elif p_loc == exp.Properties.Location.POST_SCHEMA:
    969                properties_locs[exp.Properties.Location.POST_SCHEMA].append(p)
    970            elif p_loc == exp.Properties.Location.POST_WITH:
    971                properties_locs[exp.Properties.Location.POST_WITH].append(p)
    972            elif p_loc == exp.Properties.Location.POST_CREATE:
    973                properties_locs[exp.Properties.Location.POST_CREATE].append(p)
    974            elif p_loc == exp.Properties.Location.POST_ALIAS:
    975                properties_locs[exp.Properties.Location.POST_ALIAS].append(p)
    976            elif p_loc == exp.Properties.Location.POST_EXPRESSION:
    977                properties_locs[exp.Properties.Location.POST_EXPRESSION].append(p)
    978            elif p_loc == exp.Properties.Location.UNSUPPORTED:
    979                self.unsupported(f"Unsupported property {p.key}")
    980
    981        return properties_locs

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

    983    def property_sql(self, expression: exp.Property) -> str:
    984        property_cls = expression.__class__
    985        if property_cls == exp.Property:
    986            return f"{expression.name}={self.sql(expression, 'value')}"
    987
    988        property_name = exp.Properties.PROPERTY_TO_NAME.get(property_cls)
    989        if not property_name:
    990            self.unsupported(f"Unsupported property {expression.key}")
    991
    992        return f"{property_name}={self.sql(expression, 'this')}"

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

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

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

     999    def fallbackproperty_sql(self, expression: exp.FallbackProperty) -> str:
    1000        no = "NO " if expression.args.get("no") else ""
    1001        protection = " PROTECTION" if expression.args.get("protection") else ""
    1002        return f"{no}FALLBACK{protection}"

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

    1004    def journalproperty_sql(self, expression: exp.JournalProperty) -> str:
    1005        no = "NO " if expression.args.get("no") else ""
    1006        local = expression.args.get("local")
    1007        local = f"{local} " if local else ""
    1008        dual = "DUAL " if expression.args.get("dual") else ""
    1009        before = "BEFORE " if expression.args.get("before") else ""
    1010        after = "AFTER " if expression.args.get("after") else ""
    1011        return f"{no}{local}{dual}{before}{after}JOURNAL"

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

    1013    def freespaceproperty_sql(self, expression: exp.FreespaceProperty) -> str:
    1014        freespace = self.sql(expression, "this")
    1015        percent = " PERCENT" if expression.args.get("percent") else ""
    1016        return f"FREESPACE={freespace}{percent}"

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

    1018    def checksumproperty_sql(self, expression: exp.ChecksumProperty) -> str:
    1019        if expression.args.get("default"):
    1020            property = "DEFAULT"
    1021        elif expression.args.get("on"):
    1022            property = "ON"
    1023        else:
    1024            property = "OFF"
    1025        return f"CHECKSUM={property}"

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

    1027    def mergeblockratioproperty_sql(self, expression: exp.MergeBlockRatioProperty) -> str:
    1028        if expression.args.get("no"):
    1029            return "NO MERGEBLOCKRATIO"
    1030        if expression.args.get("default"):
    1031            return "DEFAULT MERGEBLOCKRATIO"
    1032
    1033        percent = " PERCENT" if expression.args.get("percent") else ""
    1034        return f"MERGEBLOCKRATIO={self.sql(expression, 'this')}{percent}"

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

    1036    def datablocksizeproperty_sql(self, expression: exp.DataBlocksizeProperty) -> str:
    1037        default = expression.args.get("default")
    1038        minimum = expression.args.get("minimum")
    1039        maximum = expression.args.get("maximum")
    1040        if default or minimum or maximum:
    1041            if default:
    1042                prop = "DEFAULT"
    1043            elif minimum:
    1044                prop = "MINIMUM"
    1045            else:
    1046                prop = "MAXIMUM"
    1047            return f"{prop} DATABLOCKSIZE"
    1048        units = expression.args.get("units")
    1049        units = f" {units}" if units else ""
    1050        return f"DATABLOCKSIZE={self.sql(expression, 'size')}{units}"

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

    1052    def blockcompressionproperty_sql(self, expression: exp.BlockCompressionProperty) -> str:
    1053        autotemp = expression.args.get("autotemp")
    1054        always = expression.args.get("always")
    1055        default = expression.args.get("default")
    1056        manual = expression.args.get("manual")
    1057        never = expression.args.get("never")
    1058
    1059        if autotemp is not None:
    1060            prop = f"AUTOTEMP({self.expressions(autotemp)})"
    1061        elif always:
    1062            prop = "ALWAYS"
    1063        elif default:
    1064            prop = "DEFAULT"
    1065        elif manual:
    1066            prop = "MANUAL"
    1067        elif never:
    1068            prop = "NEVER"
    1069        return f"BLOCKCOMPRESSION={prop}"

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

    1071    def isolatedloadingproperty_sql(self, expression: exp.IsolatedLoadingProperty) -> str:
    1072        no = expression.args.get("no")
    1073        no = " NO" if no else ""
    1074        concurrent = expression.args.get("concurrent")
    1075        concurrent = " CONCURRENT" if concurrent else ""
    1076
    1077        for_ = ""
    1078        if expression.args.get("for_all"):
    1079            for_ = " FOR ALL"
    1080        elif expression.args.get("for_insert"):
    1081            for_ = " FOR INSERT"
    1082        elif expression.args.get("for_none"):
    1083            for_ = " FOR NONE"
    1084        return f"WITH{no}{concurrent} ISOLATED LOADING{for_}"

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

    1086    def lockingproperty_sql(self, expression: exp.LockingProperty) -> str:
    1087        kind = expression.args.get("kind")
    1088        this = f" {self.sql(expression, 'this')}" if expression.this else ""
    1089        for_or_in = expression.args.get("for_or_in")
    1090        lock_type = expression.args.get("lock_type")
    1091        override = " OVERRIDE" if expression.args.get("override") else ""
    1092        return f"LOCKING {kind}{this} {for_or_in} {lock_type}{override}"

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

    1094    def withdataproperty_sql(self, expression: exp.WithDataProperty) -> str:
    1095        data_sql = f"WITH {'NO ' if expression.args.get('no') else ''}DATA"
    1096        statistics = expression.args.get("statistics")
    1097        statistics_sql = ""
    1098        if statistics is not None:
    1099            statistics_sql = f" AND {'NO ' if not statistics else ''}STATISTICS"
    1100        return f"{data_sql}{statistics_sql}"

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

    1102    def insert_sql(self, expression: exp.Insert) -> str:
    1103        overwrite = expression.args.get("overwrite")
    1104
    1105        if isinstance(expression.this, exp.Directory):
    1106            this = " OVERWRITE" if overwrite else " INTO"
    1107        else:
    1108            this = " OVERWRITE TABLE" if overwrite else " INTO"
    1109
    1110        alternative = expression.args.get("alternative")
    1111        alternative = f" OR {alternative}" if alternative else ""
    1112        this = f"{this} {self.sql(expression, 'this')}"
    1113
    1114        exists = " IF EXISTS" if expression.args.get("exists") else ""
    1115        partition_sql = (
    1116            f" {self.sql(expression, 'partition')}" if expression.args.get("partition") else ""
    1117        )
    1118        where = self.sql(expression, "where")
    1119        where = f"{self.sep()}REPLACE WHERE {where}" if where else ""
    1120        expression_sql = f"{self.sep()}{self.sql(expression, 'expression')}"
    1121        conflict = self.sql(expression, "conflict")
    1122        returning = self.sql(expression, "returning")
    1123        sql = f"INSERT{alternative}{this}{exists}{partition_sql}{where}{expression_sql}{conflict}{returning}"
    1124        return self.prepend_ctes(expression, sql)

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

    1126    def intersect_sql(self, expression: exp.Intersect) -> str:
    1127        return self.prepend_ctes(
    1128            expression,
    1129            self.set_operation(expression, self.intersect_op(expression)),
    1130        )

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

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

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

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

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

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

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

    1141    def onconflict_sql(self, expression: exp.OnConflict) -> str:
    1142        conflict = "ON DUPLICATE KEY" if expression.args.get("duplicate") else "ON CONFLICT"
    1143        constraint = self.sql(expression, "constraint")
    1144        if constraint:
    1145            constraint = f"ON CONSTRAINT {constraint}"
    1146        key = self.expressions(expression, key="key", flat=True)
    1147        do = "" if expression.args.get("duplicate") else " DO "
    1148        nothing = "NOTHING" if expression.args.get("nothing") else ""
    1149        expressions = self.expressions(expression, flat=True)
    1150        if expressions:
    1151            expressions = f"UPDATE SET {expressions}"
    1152        return f"{self.seg(conflict)} {constraint}{key}{do}{nothing}{expressions}"

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

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

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

    1157    def rowformatdelimitedproperty_sql(self, expression: exp.RowFormatDelimitedProperty) -> str:
    1158        fields = expression.args.get("fields")
    1159        fields = f" FIELDS TERMINATED BY {fields}" if fields else ""
    1160        escaped = expression.args.get("escaped")
    1161        escaped = f" ESCAPED BY {escaped}" if escaped else ""
    1162        items = expression.args.get("collection_items")
    1163        items = f" COLLECTION ITEMS TERMINATED BY {items}" if items else ""
    1164        keys = expression.args.get("map_keys")
    1165        keys = f" MAP KEYS TERMINATED BY {keys}" if keys else ""
    1166        lines = expression.args.get("lines")
    1167        lines = f" LINES TERMINATED BY {lines}" if lines else ""
    1168        null = expression.args.get("null")
    1169        null = f" NULL DEFINED AS {null}" if null else ""
    1170        return f"ROW FORMAT DELIMITED{fields}{escaped}{items}{keys}{lines}{null}"

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

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

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

    1175    def indextablehint_sql(self, expression: exp.IndexTableHint) -> str:
    1176        this = f"{self.sql(expression, 'this')} INDEX"
    1177        target = self.sql(expression, "target")
    1178        target = f" FOR {target}" if target else ""
    1179        return f"{this}{target} ({self.expressions(expression, flat=True)})"

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

    1181    def table_sql(self, expression: exp.Table, sep: str = " AS ") -> str:
    1182        table = ".".join(
    1183            part
    1184            for part in [
    1185                self.sql(expression, "catalog"),
    1186                self.sql(expression, "db"),
    1187                self.sql(expression, "this"),
    1188            ]
    1189            if part
    1190        )
    1191
    1192        alias = self.sql(expression, "alias")
    1193        alias = f"{sep}{alias}" if alias else ""
    1194        hints = self.expressions(expression, key="hints", sep=" ")
    1195        hints = f" {hints}" if hints and self.TABLE_HINTS else ""
    1196        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1197        pivots = f" {pivots}" if pivots else ""
    1198        joins = self.expressions(expression, key="joins", sep="")
    1199        laterals = self.expressions(expression, key="laterals", sep="")
    1200        system_time = expression.args.get("system_time")
    1201        system_time = f" {self.sql(expression, 'system_time')}" if system_time else ""
    1202
    1203        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

    1205    def tablesample_sql(
    1206        self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
    1207    ) -> str:
    1208        if self.ALIAS_POST_TABLESAMPLE and expression.this.alias:
    1209            table = expression.this.copy()
    1210            table.set("alias", None)
    1211            this = self.sql(table)
    1212            alias = f"{sep}{self.sql(expression.this, 'alias')}"
    1213        else:
    1214            this = self.sql(expression, "this")
    1215            alias = ""
    1216        method = self.sql(expression, "method")
    1217        method = f"{method.upper()} " if method and self.TABLESAMPLE_WITH_METHOD else ""
    1218        numerator = self.sql(expression, "bucket_numerator")
    1219        denominator = self.sql(expression, "bucket_denominator")
    1220        field = self.sql(expression, "bucket_field")
    1221        field = f" ON {field}" if field else ""
    1222        bucket = f"BUCKET {numerator} OUT OF {denominator}{field}" if numerator else ""
    1223        percent = self.sql(expression, "percent")
    1224        percent = f"{percent} PERCENT" if percent else ""
    1225        rows = self.sql(expression, "rows")
    1226        rows = f"{rows} ROWS" if rows else ""
    1227        size = self.sql(expression, "size")
    1228        if size and self.TABLESAMPLE_SIZE_IS_PERCENT:
    1229            size = f"{size} PERCENT"
    1230        seed = self.sql(expression, "seed")
    1231        seed = f" {seed_prefix} ({seed})" if seed else ""
    1232        kind = expression.args.get("kind", "TABLESAMPLE")
    1233        return f"{this} {kind} {method}({bucket}{percent}{rows}{size}){seed}{alias}"

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

    1235    def pivot_sql(self, expression: exp.Pivot) -> str:
    1236        expressions = self.expressions(expression, flat=True)
    1237
    1238        if expression.this:
    1239            this = self.sql(expression, "this")
    1240            on = f"{self.seg('ON')} {expressions}"
    1241            using = self.expressions(expression, key="using", flat=True)
    1242            using = f"{self.seg('USING')} {using}" if using else ""
    1243            group = self.sql(expression, "group")
    1244            return f"PIVOT {this}{on}{using}{group}"
    1245
    1246        alias = self.sql(expression, "alias")
    1247        alias = f" AS {alias}" if alias else ""
    1248        unpivot = expression.args.get("unpivot")
    1249        direction = "UNPIVOT" if unpivot else "PIVOT"
    1250        field = self.sql(expression, "field")
    1251        return f"{direction}({expressions} FOR {field}){alias}"

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

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

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

    1256    def update_sql(self, expression: exp.Update) -> str:
    1257        this = self.sql(expression, "this")
    1258        set_sql = self.expressions(expression, flat=True)
    1259        from_sql = self.sql(expression, "from")
    1260        where_sql = self.sql(expression, "where")
    1261        returning = self.sql(expression, "returning")
    1262        limit = self.sql(expression, "limit")
    1263        sql = f"UPDATE {this} SET {set_sql}{from_sql}{where_sql}{returning}{limit}"
    1264        return self.prepend_ctes(expression, sql)

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

    1266    def values_sql(self, expression: exp.Values) -> str:
    1267        args = self.expressions(expression)
    1268        alias = self.sql(expression, "alias")
    1269        values = f"VALUES{self.seg('')}{args}"
    1270        values = (
    1271            f"({values})"
    1272            if self.WRAP_DERIVED_VALUES and (alias or isinstance(expression.parent, exp.From))
    1273            else values
    1274        )
    1275        return f"{values} AS {alias}" if alias else values

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

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

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

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

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

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

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

    1288    def group_sql(self, expression: exp.Group) -> str:
    1289        group_by = self.op_expressions("GROUP BY", expression)
    1290        grouping_sets = self.expressions(expression, key="grouping_sets", indent=False)
    1291        grouping_sets = (
    1292            f"{self.seg('GROUPING SETS')} {self.wrap(grouping_sets)}" if grouping_sets else ""
    1293        )
    1294
    1295        cube = expression.args.get("cube", [])
    1296        if seq_get(cube, 0) is True:
    1297            return f"{group_by}{self.seg('WITH CUBE')}"
    1298        else:
    1299            cube_sql = self.expressions(expression, key="cube", indent=False)
    1300            cube_sql = f"{self.seg('CUBE')} {self.wrap(cube_sql)}" if cube_sql else ""
    1301
    1302        rollup = expression.args.get("rollup", [])
    1303        if seq_get(rollup, 0) is True:
    1304            return f"{group_by}{self.seg('WITH ROLLUP')}"
    1305        else:
    1306            rollup_sql = self.expressions(expression, key="rollup", indent=False)
    1307            rollup_sql = f"{self.seg('ROLLUP')} {self.wrap(rollup_sql)}" if rollup_sql else ""
    1308
    1309        groupings = csv(
    1310            grouping_sets,
    1311            cube_sql,
    1312            rollup_sql,
    1313            self.seg("WITH TOTALS") if expression.args.get("totals") else "",
    1314            sep=self.GROUPINGS_SEP,
    1315        )
    1316
    1317        if expression.args.get("expressions") and groupings:
    1318            group_by = f"{group_by}{self.GROUPINGS_SEP}"
    1319
    1320        return f"{group_by}{groupings}"

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

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

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

    1326    def join_sql(self, expression: exp.Join) -> str:
    1327        op_sql = " ".join(
    1328            op
    1329            for op in (
    1330                expression.method,
    1331                "GLOBAL" if expression.args.get("global") else None,
    1332                expression.side,
    1333                expression.kind,
    1334                expression.hint if self.JOIN_HINTS else None,
    1335            )
    1336            if op
    1337        )
    1338        on_sql = self.sql(expression, "on")
    1339        using = expression.args.get("using")
    1340
    1341        if not on_sql and using:
    1342            on_sql = csv(*(self.sql(column) for column in using))
    1343
    1344        this_sql = self.sql(expression, "this")
    1345
    1346        if on_sql:
    1347            on_sql = self.indent(on_sql, skip_first=True)
    1348            space = self.seg(" " * self.pad) if self.pretty else " "
    1349            if using:
    1350                on_sql = f"{space}USING ({on_sql})"
    1351            else:
    1352                on_sql = f"{space}ON {on_sql}"
    1353        elif not op_sql:
    1354            return f", {this_sql}"
    1355
    1356        op_sql = f"{op_sql} JOIN" if op_sql else "JOIN"
    1357        return f"{self.seg(op_sql)} {this_sql}{on_sql}"

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

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

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

    1364    def lateral_sql(self, expression: exp.Lateral) -> str:
    1365        this = self.sql(expression, "this")
    1366
    1367        if isinstance(expression.this, exp.Subquery):
    1368            return f"LATERAL {this}"
    1369
    1370        if expression.args.get("view"):
    1371            alias = expression.args["alias"]
    1372            columns = self.expressions(alias, key="columns", flat=True)
    1373            table = f" {alias.name}" if alias.name else ""
    1374            columns = f" AS {columns}" if columns else ""
    1375            op_sql = self.seg(f"LATERAL VIEW{' OUTER' if expression.args.get('outer') else ''}")
    1376            return f"{op_sql}{self.sep()}{this}{table}{columns}"
    1377
    1378        alias = self.sql(expression, "alias")
    1379        alias = f" AS {alias}" if alias else ""
    1380        return f"LATERAL {this}{alias}"

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

    1382    def limit_sql(self, expression: exp.Limit) -> str:
    1383        this = self.sql(expression, "this")
    1384        args = ", ".join(
    1385            sql
    1386            for sql in (
    1387                self.sql(expression, "offset"),
    1388                self.sql(expression, "expression"),
    1389            )
    1390            if sql
    1391        )
    1392        return f"{this}{self.seg('LIMIT')} {args}"

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

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

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

    1398    def setitem_sql(self, expression: exp.SetItem) -> str:
    1399        kind = self.sql(expression, "kind")
    1400        kind = f"{kind} " if kind else ""
    1401        this = self.sql(expression, "this")
    1402        expressions = self.expressions(expression)
    1403        collate = self.sql(expression, "collate")
    1404        collate = f" COLLATE {collate}" if collate else ""
    1405        global_ = "GLOBAL " if expression.args.get("global") else ""
    1406        return f"{global_}{kind}{this}{expressions}{collate}"

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

    1408    def set_sql(self, expression: exp.Set) -> str:
    1409        expressions = (
    1410            f" {self.expressions(expression, flat=True)}" if expression.expressions else ""
    1411        )
    1412        return f"SET{expressions}"

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

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

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

    1417    def lock_sql(self, expression: exp.Lock) -> str:
    1418        if not self.LOCKING_READS_SUPPORTED:
    1419            self.unsupported("Locking reads using 'FOR UPDATE/SHARE' are not supported")
    1420            return ""
    1421
    1422        lock_type = "FOR UPDATE" if expression.args["update"] else "FOR SHARE"
    1423        expressions = self.expressions(expression, flat=True)
    1424        expressions = f" OF {expressions}" if expressions else ""
    1425        wait = expression.args.get("wait")
    1426
    1427        if wait is not None:
    1428            if isinstance(wait, exp.Literal):
    1429                wait = f" WAIT {self.sql(wait)}"
    1430            else:
    1431                wait = " NOWAIT" if wait else " SKIP LOCKED"
    1432
    1433        return f"{lock_type}{expressions}{wait or ''}"

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

    1435    def literal_sql(self, expression: exp.Literal) -> str:
    1436        text = expression.this or ""
    1437        if expression.is_string:
    1438            text = f"{self.QUOTE_START}{self.escape_str(text)}{self.QUOTE_END}"
    1439        return text

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

    1441    def escape_str(self, text: str) -> str:
    1442        text = text.replace(self.QUOTE_END, self._escaped_quote_end)
    1443        if self.pretty:
    1444            text = text.replace("\n", self.SENTINEL_LINE_BREAK)
    1445        return text

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

    1447    def loaddata_sql(self, expression: exp.LoadData) -> str:
    1448        local = " LOCAL" if expression.args.get("local") else ""
    1449        inpath = f" INPATH {self.sql(expression, 'inpath')}"
    1450        overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
    1451        this = f" INTO TABLE {self.sql(expression, 'this')}"
    1452        partition = self.sql(expression, "partition")
    1453        partition = f" {partition}" if partition else ""
    1454        input_format = self.sql(expression, "input_format")
    1455        input_format = f" INPUTFORMAT {input_format}" if input_format else ""
    1456        serde = self.sql(expression, "serde")
    1457        serde = f" SERDE {serde}" if serde else ""
    1458        return f"LOAD DATA{local}{inpath}{overwrite}{this}{partition}{input_format}{serde}"

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

    1460    def null_sql(self, *_) -> str:
    1461        return "NULL"

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

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

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

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

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

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

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

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

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

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

    1480    def ordered_sql(self, expression: exp.Ordered) -> str:
    1481        desc = expression.args.get("desc")
    1482        asc = not desc
    1483
    1484        nulls_first = expression.args.get("nulls_first")
    1485        nulls_last = not nulls_first
    1486        nulls_are_large = self.NULL_ORDERING == "nulls_are_large"
    1487        nulls_are_small = self.NULL_ORDERING == "nulls_are_small"
    1488        nulls_are_last = self.NULL_ORDERING == "nulls_are_last"
    1489
    1490        sort_order = " DESC" if desc else ""
    1491        nulls_sort_change = ""
    1492        if nulls_first and (
    1493            (asc and nulls_are_large) or (desc and nulls_are_small) or nulls_are_last
    1494        ):
    1495            nulls_sort_change = " NULLS FIRST"
    1496        elif (
    1497            nulls_last
    1498            and ((asc and nulls_are_small) or (desc and nulls_are_large))
    1499            and not nulls_are_last
    1500        ):
    1501            nulls_sort_change = " NULLS LAST"
    1502
    1503        if nulls_sort_change and not self.NULL_ORDERING_SUPPORTED:
    1504            self.unsupported(
    1505                "Sorting in an ORDER BY on NULLS FIRST/NULLS LAST is not supported by this dialect"
    1506            )
    1507            nulls_sort_change = ""
    1508
    1509        return f"{self.sql(expression, 'this')}{sort_order}{nulls_sort_change}"

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

    1511    def matchrecognize_sql(self, expression: exp.MatchRecognize) -> str:
    1512        partition = self.partition_by_sql(expression)
    1513        order = self.sql(expression, "order")
    1514        measures = self.expressions(expression, key="measures")
    1515        measures = self.seg(f"MEASURES{self.seg(measures)}") if measures else ""
    1516        rows = self.sql(expression, "rows")
    1517        rows = self.seg(rows) if rows else ""
    1518        after = self.sql(expression, "after")
    1519        after = self.seg(after) if after else ""
    1520        pattern = self.sql(expression, "pattern")
    1521        pattern = self.seg(f"PATTERN ({pattern})") if pattern else ""
    1522        definition_sqls = [
    1523            f"{self.sql(definition, 'alias')} AS {self.sql(definition, 'this')}"
    1524            for definition in expression.args.get("define", [])
    1525        ]
    1526        definitions = self.expressions(sqls=definition_sqls)
    1527        define = self.seg(f"DEFINE{self.seg(definitions)}") if definitions else ""
    1528        body = "".join(
    1529            (
    1530                partition,
    1531                order,
    1532                measures,
    1533                rows,
    1534                after,
    1535                pattern,
    1536                define,
    1537            )
    1538        )
    1539        alias = self.sql(expression, "alias")
    1540        alias = f" {alias}" if alias else ""
    1541        return f"{self.seg('MATCH_RECOGNIZE')} {self.wrap(body)}{alias}"

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

    1543    def query_modifiers(self, expression: exp.Expression, *sqls: str) -> str:
    1544        limit: t.Optional[exp.Fetch | exp.Limit] = expression.args.get("limit")
    1545
    1546        if self.LIMIT_FETCH == "LIMIT" and isinstance(limit, exp.Fetch):
    1547            limit = exp.Limit(expression=limit.args.get("count"))
    1548        elif self.LIMIT_FETCH == "FETCH" and isinstance(limit, exp.Limit):
    1549            limit = exp.Fetch(direction="FIRST", count=limit.expression)
    1550
    1551        fetch = isinstance(limit, exp.Fetch)
    1552
    1553        return csv(
    1554            *sqls,
    1555            *[self.sql(join) for join in expression.args.get("joins") or []],
    1556            self.sql(expression, "match"),
    1557            *[self.sql(lateral) for lateral in expression.args.get("laterals") or []],
    1558            self.sql(expression, "where"),
    1559            self.sql(expression, "group"),
    1560            self.sql(expression, "having"),
    1561            *self.after_having_modifiers(expression),
    1562            self.sql(expression, "order"),
    1563            *self.offset_limit_modifiers(expression, fetch, limit),
    1564            *self.after_limit_modifiers(expression),
    1565            sep="",
    1566        )

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

    1568    def offset_limit_modifiers(
    1569        self, expression: exp.Expression, fetch: bool, limit: t.Optional[exp.Fetch | exp.Limit]
    1570    ) -> t.List[str]:
    1571        return [
    1572            self.sql(expression, "offset") if fetch else self.sql(limit),
    1573            self.sql(limit) if fetch else self.sql(expression, "offset"),
    1574        ]

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

    1576    def after_having_modifiers(self, expression: exp.Expression) -> t.List[str]:
    1577        return [
    1578            self.sql(expression, "qualify"),
    1579            self.seg("WINDOW ") + self.expressions(expression, key="windows", flat=True)
    1580            if expression.args.get("windows")
    1581            else "",
    1582        ]

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

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

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

    1589    def select_sql(self, expression: exp.Select) -> str:
    1590        hint = self.sql(expression, "hint")
    1591        distinct = self.sql(expression, "distinct")
    1592        distinct = f" {distinct}" if distinct else ""
    1593        kind = self.sql(expression, "kind").upper()
    1594        expressions = self.expressions(expression)
    1595
    1596        if kind:
    1597            if kind in self.SELECT_KINDS:
    1598                kind = f" AS {kind}"
    1599            else:
    1600                if kind == "STRUCT":
    1601                    expressions = self.expressions(
    1602                        sqls=[
    1603                            self.sql(
    1604                                exp.Struct(
    1605                                    expressions=[
    1606                                        exp.column(e.output_name).eq(
    1607                                            e.this if isinstance(e, exp.Alias) else e
    1608                                        )
    1609                                        for e in expression.expressions
    1610                                    ]
    1611                                )
    1612                            )
    1613                        ]
    1614                    )
    1615                kind = ""
    1616
    1617        expressions = f"{self.sep()}{expressions}" if expressions else expressions
    1618        sql = self.query_modifiers(
    1619            expression,
    1620            f"SELECT{hint}{distinct}{kind}{expressions}",
    1621            self.sql(expression, "into", comment=False),
    1622            self.sql(expression, "from", comment=False),
    1623        )
    1624        return self.prepend_ctes(expression, sql)

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

    1626    def schema_sql(self, expression: exp.Schema) -> str:
    1627        this = self.sql(expression, "this")
    1628        this = f"{this} " if this else ""
    1629        sql = self.schema_columns_sql(expression)
    1630        return f"{this}{sql}"

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

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

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

    1635    def star_sql(self, expression: exp.Star) -> str:
    1636        except_ = self.expressions(expression, key="except", flat=True)
    1637        except_ = f"{self.seg(self.STAR_MAPPING['except'])} ({except_})" if except_ else ""
    1638        replace = self.expressions(expression, key="replace", flat=True)
    1639        replace = f"{self.seg(self.STAR_MAPPING['replace'])} ({replace})" if replace else ""
    1640        return f"*{except_}{replace}"

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

    1642    def parameter_sql(self, expression: exp.Parameter) -> str:
    1643        this = self.sql(expression, "this")
    1644        this = f"{{{this}}}" if expression.args.get("wrapped") else f"{this}"
    1645        return f"{self.PARAMETER_TOKEN}{this}"

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

    1647    def sessionparameter_sql(self, expression: exp.SessionParameter) -> str:
    1648        this = self.sql(expression, "this")
    1649        kind = expression.text("kind")
    1650        if kind:
    1651            kind = f"{kind}."
    1652        return f"@@{kind}{this}"

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

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

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

    1657    def subquery_sql(self, expression: exp.Subquery, sep: str = " AS ") -> str:
    1658        alias = self.sql(expression, "alias")
    1659        alias = f"{sep}{alias}" if alias else ""
    1660
    1661        pivots = self.expressions(expression, key="pivots", sep=" ", flat=True)
    1662        pivots = f" {pivots}" if pivots else ""
    1663
    1664        sql = self.query_modifiers(expression, self.wrap(expression), alias, pivots)
    1665        return self.prepend_ctes(expression, sql)

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

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

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

    1671    def union_sql(self, expression: exp.Union) -> str:
    1672        return self.prepend_ctes(
    1673            expression,
    1674            self.set_operation(expression, self.union_op(expression)),
    1675        )

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

    1677    def union_op(self, expression: exp.Union) -> str:
    1678        kind = " DISTINCT" if self.EXPLICIT_UNION else ""
    1679        kind = kind if expression.args.get("distinct") else " ALL"
    1680        return f"UNION{kind}"

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

    1682    def unnest_sql(self, expression: exp.Unnest) -> str:
    1683        args = self.expressions(expression, flat=True)
    1684        alias = expression.args.get("alias")
    1685        if alias and self.UNNEST_COLUMN_ONLY:
    1686            columns = alias.columns
    1687            alias = self.sql(columns[0]) if columns else ""
    1688        else:
    1689            alias = self.sql(expression, "alias")
    1690        alias = f" AS {alias}" if alias else alias
    1691        ordinality = " WITH ORDINALITY" if expression.args.get("ordinality") else ""
    1692        offset = expression.args.get("offset")
    1693        offset = f" WITH OFFSET AS {self.sql(offset)}" if offset else ""
    1694        return f"UNNEST({args}){ordinality}{alias}{offset}"

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

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

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

    1700    def window_sql(self, expression: exp.Window) -> str:
    1701        this = self.sql(expression, "this")
    1702        partition = self.partition_by_sql(expression)
    1703        order = expression.args.get("order")
    1704        order = self.order_sql(order, flat=True) if order else ""
    1705        spec = self.sql(expression, "spec")
    1706        alias = self.sql(expression, "alias")
    1707        over = self.sql(expression, "over") or "OVER"
    1708
    1709        this = f"{this} {'AS' if expression.arg_key == 'windows' else over}"
    1710
    1711        first = expression.args.get("first")
    1712        if first is None:
    1713            first = ""
    1714        else:
    1715            first = "FIRST" if first else "LAST"
    1716
    1717        if not partition and not order and not spec and alias:
    1718            return f"{this} {alias}"
    1719
    1720        args = " ".join(arg for arg in (alias, first, partition, order, spec) if arg)
    1721        return f"{this} ({args})"

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

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

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

    1727    def windowspec_sql(self, expression: exp.WindowSpec) -> str:
    1728        kind = self.sql(expression, "kind")
    1729        start = csv(self.sql(expression, "start"), self.sql(expression, "start_side"), sep=" ")
    1730        end = (
    1731            csv(self.sql(expression, "end"), self.sql(expression, "end_side"), sep=" ")
    1732            or "CURRENT ROW"
    1733        )
    1734        return f"{kind} BETWEEN {start} AND {end}"

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

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

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

    1741    def between_sql(self, expression: exp.Between) -> str:
    1742        this = self.sql(expression, "this")
    1743        low = self.sql(expression, "low")
    1744        high = self.sql(expression, "high")
    1745        return f"{this} BETWEEN {low} AND {high}"

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

    1747    def bracket_sql(self, expression: exp.Bracket) -> str:
    1748        expressions = apply_index_offset(expression.this, expression.expressions, self.INDEX_OFFSET)
    1749        expressions_sql = ", ".join(self.sql(e) for e in expressions)
    1750
    1751        return f"{self.sql(expression, 'this')}[{expressions_sql}]"

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

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

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

    1756    def any_sql(self, expression: exp.Any) -> str:
    1757        this = self.sql(expression, "this")
    1758        if isinstance(expression.this, exp.Subqueryable):
    1759            this = self.wrap(this)
    1760        return f"ANY {this}"

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

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

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

    1765    def case_sql(self, expression: exp.Case) -> str:
    1766        this = self.sql(expression, "this")
    1767        statements = [f"CASE {this}" if this else "CASE"]
    1768
    1769        for e in expression.args["ifs"]:
    1770            statements.append(f"WHEN {self.sql(e, 'this')}")
    1771            statements.append(f"THEN {self.sql(e, 'true')}")
    1772
    1773        default = self.sql(expression, "default")
    1774
    1775        if default:
    1776            statements.append(f"ELSE {default}")
    1777
    1778        statements.append("END")
    1779
    1780        if self.pretty and self.text_width(statements) > self.max_text_width:
    1781            return self.indent("\n".join(statements), skip_first=True, skip_last=True)
    1782
    1783        return " ".join(statements)

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

    1785    def constraint_sql(self, expression: exp.Constraint) -> str:
    1786        this = self.sql(expression, "this")
    1787        expressions = self.expressions(expression, flat=True)
    1788        return f"CONSTRAINT {this} {expressions}"

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

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

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

    1795    def extract_sql(self, expression: exp.Extract) -> str:
    1796        this = self.sql(expression, "this")
    1797        expression_sql = self.sql(expression, "expression")
    1798        return f"EXTRACT({this} FROM {expression_sql})"

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

    1800    def trim_sql(self, expression: exp.Trim) -> str:
    1801        trim_type = self.sql(expression, "position")
    1802
    1803        if trim_type == "LEADING":
    1804            return self.func("LTRIM", expression.this)
    1805        elif trim_type == "TRAILING":
    1806            return self.func("RTRIM", expression.this)
    1807        else:
    1808            return self.func("TRIM", expression.this, expression.expression)

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

    1810    def safeconcat_sql(self, expression: exp.SafeConcat) -> str:
    1811        expressions = expression.expressions
    1812        if self.STRICT_STRING_CONCAT:
    1813            expressions = (exp.cast(e, "text") for e in expressions)
    1814        return self.func("CONCAT", *expressions)

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

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

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

    1820    def foreignkey_sql(self, expression: exp.ForeignKey) -> str:
    1821        expressions = self.expressions(expression, flat=True)
    1822        reference = self.sql(expression, "reference")
    1823        reference = f" {reference}" if reference else ""
    1824        delete = self.sql(expression, "delete")
    1825        delete = f" ON DELETE {delete}" if delete else ""
    1826        update = self.sql(expression, "update")
    1827        update = f" ON UPDATE {update}" if update else ""
    1828        return f"FOREIGN KEY ({expressions}){reference}{delete}{update}"

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

    1830    def primarykey_sql(self, expression: exp.ForeignKey) -> str:
    1831        expressions = self.expressions(expression, flat=True)
    1832        options = self.expressions(expression, key="options", flat=True, sep=" ")
    1833        options = f" {options}" if options else ""
    1834        return f"PRIMARY KEY ({expressions}){options}"

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

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

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

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

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

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

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

    1847    def jsonobject_sql(self, expression: exp.JSONObject) -> str:
    1848        null_handling = expression.args.get("null_handling")
    1849        null_handling = f" {null_handling}" if null_handling else ""
    1850        unique_keys = expression.args.get("unique_keys")
    1851        if unique_keys is not None:
    1852            unique_keys = f" {'WITH' if unique_keys else 'WITHOUT'} UNIQUE KEYS"
    1853        else:
    1854            unique_keys = ""
    1855        return_type = self.sql(expression, "return_type")
    1856        return_type = f" RETURNING {return_type}" if return_type else ""
    1857        format_json = " FORMAT JSON" if expression.args.get("format_json") else ""
    1858        encoding = self.sql(expression, "encoding")
    1859        encoding = f" ENCODING {encoding}" if encoding else ""
    1860        return self.func(
    1861            "JSON_OBJECT",
    1862            *expression.expressions,
    1863            suffix=f"{null_handling}{unique_keys}{return_type}{format_json}{encoding})",
    1864        )

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

    1866    def openjsoncolumndef_sql(self, expression: exp.OpenJSONColumnDef) -> str:
    1867        this = self.sql(expression, "this")
    1868        kind = self.sql(expression, "kind")
    1869        path = self.sql(expression, "path")
    1870        path = f" {path}" if path else ""
    1871        as_json = " AS JSON" if expression.args.get("as_json") else ""
    1872        return f"{this} {kind}{path}{as_json}"

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

    1874    def openjson_sql(self, expression: exp.OpenJSON) -> str:
    1875        this = self.sql(expression, "this")
    1876        path = self.sql(expression, "path")
    1877        path = f", {path}" if path else ""
    1878        expressions = self.expressions(expression)
    1879        with_ = (
    1880            f" WITH ({self.seg(self.indent(expressions), sep='')}{self.seg(')', sep='')}"
    1881            if expressions
    1882            else ""
    1883        )
    1884        return f"OPENJSON({this}{path}){with_}"

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

    1886    def in_sql(self, expression: exp.In) -> str:
    1887        query = expression.args.get("query")
    1888        unnest = expression.args.get("unnest")
    1889        field = expression.args.get("field")
    1890        is_global = " GLOBAL" if expression.args.get("is_global") else ""
    1891
    1892        if query:
    1893            in_sql = self.wrap(query)
    1894        elif unnest:
    1895            in_sql = self.in_unnest_op(unnest)
    1896        elif field:
    1897            in_sql = self.sql(field)
    1898        else:
    1899            in_sql = f"({self.expressions(expression, flat=True)})"
    1900
    1901        return f"{self.sql(expression, 'this')}{is_global} IN {in_sql}"

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

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

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

    1906    def interval_sql(self, expression: exp.Interval) -> str:
    1907        unit = self.sql(expression, "unit")
    1908        if not self.INTERVAL_ALLOWS_PLURAL_FORM:
    1909            unit = self.TIME_PART_SINGULARS.get(unit.lower(), unit)
    1910        unit = f" {unit}" if unit else ""
    1911
    1912        if self.SINGLE_STRING_INTERVAL:
    1913            this = expression.this.name if expression.this else ""
    1914            return f"INTERVAL '{this}{unit}'" if this else f"INTERVAL{unit}"
    1915
    1916        this = self.sql(expression, "this")
    1917        if this:
    1918            unwrapped = isinstance(expression.this, self.UNWRAPPED_INTERVAL_VALUES)
    1919            this = f" {this}" if unwrapped else f" ({this})"
    1920
    1921        return f"INTERVAL{this}{unit}"

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

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

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

    1926    def reference_sql(self, expression: exp.Reference) -> str:
    1927        this = self.sql(expression, "this")
    1928        expressions = self.expressions(expression, flat=True)
    1929        expressions = f"({expressions})" if expressions else ""
    1930        options = self.expressions(expression, key="options", flat=True, sep=" ")
    1931        options = f" {options}" if options else ""
    1932        return f"REFERENCES {this}{expressions}{options}"

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

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

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

    1937    def paren_sql(self, expression: exp.Paren) -> str:
    1938        if isinstance(expression.unnest(), exp.Select):
    1939            sql = self.wrap(expression)
    1940        else:
    1941            sql = self.seg(self.indent(self.sql(expression, "this")), sep="")
    1942            sql = f"({sql}{self.seg(')', sep='')}"
    1943
    1944        return self.prepend_ctes(expression, sql)

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

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

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

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

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

    1955    def alias_sql(self, expression: exp.Alias) -> str:
    1956        alias = self.sql(expression, "alias")
    1957        alias = f" AS {alias}" if alias else ""
    1958        return f"{self.sql(expression, 'this')}{alias}"

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

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

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

    1963    def attimezone_sql(self, expression: exp.AtTimeZone) -> str:
    1964        this = self.sql(expression, "this")
    1965        zone = self.sql(expression, "zone")
    1966        return f"{this} AT TIME ZONE {zone}"

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

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

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

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

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

    1974    def connector_sql(self, expression: exp.Connector, op: str) -> str:
    1975        if not self.pretty:
    1976            return self.binary(expression, op)
    1977
    1978        sqls = tuple(
    1979            self.maybe_comment(self.sql(e), e, e.parent.comments or []) if i != 1 else self.sql(e)
    1980            for i, e in enumerate(expression.flatten(unnest=False))
    1981        )
    1982
    1983        sep = "\n" if self.text_width(sqls) > self.max_text_width else " "
    1984        return f"{sep}{op} ".join(sqls)

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

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

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

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

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

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

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

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

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

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

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

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

def cast_sql(self, expression: sqlglot.expressions.Cast) -> str: View Source

    2004    def cast_sql(self, expression: exp.Cast) -> str:
    2005        return f"CAST({self.sql(expression, 'this')} AS {self.sql(expression, 'to')})"

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

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

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

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

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

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

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

    2017    def comment_sql(self, expression: exp.Comment) -> str:
    2018        this = self.sql(expression, "this")
    2019        kind = expression.args["kind"]
    2020        exists_sql = " IF EXISTS " if expression.args.get("exists") else " "
    2021        expression_sql = self.sql(expression, "expression")
    2022        return f"COMMENT{exists_sql}ON {kind} {this} IS {expression_sql}"

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

    2024    def mergetreettlaction_sql(self, expression: exp.MergeTreeTTLAction) -> str:
    2025        this = self.sql(expression, "this")
    2026        delete = " DELETE" if expression.args.get("delete") else ""
    2027        recompress = self.sql(expression, "recompress")
    2028        recompress = f" RECOMPRESS {recompress}" if recompress else ""
    2029        to_disk = self.sql(expression, "to_disk")
    2030        to_disk = f" TO DISK {to_disk}" if to_disk else ""
    2031        to_volume = self.sql(expression, "to_volume")
    2032        to_volume = f" TO VOLUME {to_volume}" if to_volume else ""
    2033        return f"{this}{delete}{recompress}{to_disk}{to_volume}"

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

    2035    def mergetreettl_sql(self, expression: exp.MergeTreeTTL) -> str:
    2036        where = self.sql(expression, "where")
    2037        group = self.sql(expression, "group")
    2038        aggregates = self.expressions(expression, key="aggregates")
    2039        aggregates = self.seg("SET") + self.seg(aggregates) if aggregates else ""
    2040
    2041        if not (where or group or aggregates) and len(expression.expressions) == 1:
    2042            return f"TTL {self.expressions(expression, flat=True)}"
    2043
    2044        return f"TTL{self.seg(self.expressions(expression))}{where}{group}{aggregates}"

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

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

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

    2049    def commit_sql(self, expression: exp.Commit) -> str:
    2050        chain = expression.args.get("chain")
    2051        if chain is not None:
    2052            chain = " AND CHAIN" if chain else " AND NO CHAIN"
    2053
    2054        return f"COMMIT{chain or ''}"

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

    2056    def rollback_sql(self, expression: exp.Rollback) -> str:
    2057        savepoint = expression.args.get("savepoint")
    2058        savepoint = f" TO {savepoint}" if savepoint else ""
    2059        return f"ROLLBACK{savepoint}"

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

    2061    def altercolumn_sql(self, expression: exp.AlterColumn) -> str:
    2062        this = self.sql(expression, "this")
    2063
    2064        dtype = self.sql(expression, "dtype")
    2065        if dtype:
    2066            collate = self.sql(expression, "collate")
    2067            collate = f" COLLATE {collate}" if collate else ""
    2068            using = self.sql(expression, "using")
    2069            using = f" USING {using}" if using else ""
    2070            return f"ALTER COLUMN {this} TYPE {dtype}{collate}{using}"
    2071
    2072        default = self.sql(expression, "default")
    2073        if default:
    2074            return f"ALTER COLUMN {this} SET DEFAULT {default}"
    2075
    2076        if not expression.args.get("drop"):
    2077            self.unsupported("Unsupported ALTER COLUMN syntax")
    2078
    2079        return f"ALTER COLUMN {this} DROP DEFAULT"

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

    2081    def renametable_sql(self, expression: exp.RenameTable) -> str:
    2082        if not self.RENAME_TABLE_WITH_DB:
    2083            # Remove db from tables
    2084            expression = expression.transform(
    2085                lambda n: exp.table_(n.this) if isinstance(n, exp.Table) else n
    2086            )
    2087        this = self.sql(expression, "this")
    2088        return f"RENAME TO {this}"

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

    2090    def altertable_sql(self, expression: exp.AlterTable) -> str:
    2091        actions = expression.args["actions"]
    2092
    2093        if isinstance(actions[0], exp.ColumnDef):
    2094            actions = self.expressions(expression, key="actions", prefix="ADD COLUMN ")
    2095        elif isinstance(actions[0], exp.Schema):
    2096            actions = self.expressions(expression, key="actions", prefix="ADD COLUMNS ")
    2097        elif isinstance(actions[0], exp.Delete):
    2098            actions = self.expressions(expression, key="actions", flat=True)
    2099        else:
    2100            actions = self.expressions(expression, key="actions")
    2101
    2102        exists = " IF EXISTS" if expression.args.get("exists") else ""
    2103        return f"ALTER TABLE{exists} {self.sql(expression, 'this')} {actions}"

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

    2105    def droppartition_sql(self, expression: exp.DropPartition) -> str:
    2106        expressions = self.expressions(expression)
    2107        exists = " IF EXISTS " if expression.args.get("exists") else " "
    2108        return f"DROP{exists}{expressions}"

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

    2110    def addconstraint_sql(self, expression: exp.AddConstraint) -> str:
    2111        this = self.sql(expression, "this")
    2112        expression_ = self.sql(expression, "expression")
    2113        add_constraint = f"ADD CONSTRAINT {this}" if this else "ADD"
    2114
    2115        enforced = expression.args.get("enforced")
    2116        if enforced is not None:
    2117            return f"{add_constraint} CHECK ({expression_}){' ENFORCED' if enforced else ''}"
    2118
    2119        return f"{add_constraint} {expression_}"

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

    2121    def distinct_sql(self, expression: exp.Distinct) -> str:
    2122        this = self.expressions(expression, flat=True)
    2123        this = f" {this}" if this else ""
    2124
    2125        on = self.sql(expression, "on")
    2126        on = f" ON {on}" if on else ""
    2127        return f"DISTINCT{this}{on}"

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

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

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

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

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

    2135    def intdiv_sql(self, expression: exp.IntDiv) -> str:
    2136        return self.sql(
    2137            exp.Cast(
    2138                this=exp.Div(this=expression.this, expression=expression.expression),
    2139                to=exp.DataType(this=exp.DataType.Type.INT),
    2140            )
    2141        )

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

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

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

    2146    def safedpipe_sql(self, expression: exp.SafeDPipe) -> str:
    2147        if self.STRICT_STRING_CONCAT:
    2148            return self.func("CONCAT", *(exp.cast(e, "text") for e in expression.flatten()))
    2149        return self.dpipe_sql(expression)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    2184    def is_sql(self, expression: exp.Is) -> str:
    2185        if not self.IS_BOOL_ALLOWED and isinstance(expression.expression, exp.Boolean):
    2186            return self.sql(
    2187                expression.this if expression.expression.this else exp.not_(expression.this)
    2188            )
    2189        return self.binary(expression, "IS")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    2230    def trycast_sql(self, expression: exp.TryCast) -> str:
    2231        return f"TRY_CAST({self.sql(expression, 'this')} AS {self.sql(expression, 'to')})"

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

    2233    def use_sql(self, expression: exp.Use) -> str:
    2234        kind = self.sql(expression, "kind")
    2235        kind = f" {kind}" if kind else ""
    2236        this = self.sql(expression, "this")
    2237        this = f" {this}" if this else ""
    2238        return f"USE{kind}{this}"

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

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

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

    2244    def function_fallback_sql(self, expression: exp.Func) -> str:
    2245        args = []
    2246        for arg_value in expression.args.values():
    2247            if isinstance(arg_value, list):
    2248                for value in arg_value:
    2249                    args.append(value)
    2250            else:
    2251                args.append(arg_value)
    2252
    2253        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

    2255    def func(
    2256        self,
    2257        name: str,
    2258        *args: t.Optional[exp.Expression | str],
    2259        prefix: str = "(",
    2260        suffix: str = ")",
    2261    ) -> str:
    2262        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

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

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

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

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

    2273    def format_time(self, expression: exp.Expression) -> t.Optional[str]:
    2274        return format_time(
    2275            self.sql(expression, "format"), self.INVERSE_TIME_MAPPING, self.INVERSE_TIME_TRIE
    2276        )

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

    2278    def expressions(
    2279        self,
    2280        expression: t.Optional[exp.Expression] = None,
    2281        key: t.Optional[str] = None,
    2282        sqls: t.Optional[t.List[str]] = None,
    2283        flat: bool = False,
    2284        indent: bool = True,
    2285        sep: str = ", ",
    2286        prefix: str = "",
    2287    ) -> str:
    2288        expressions = expression.args.get(key or "expressions") if expression else sqls
    2289
    2290        if not expressions:
    2291            return ""
    2292
    2293        if flat:
    2294            return sep.join(self.sql(e) for e in expressions)
    2295
    2296        num_sqls = len(expressions)
    2297
    2298        # These are calculated once in case we have the leading_comma / pretty option set, correspondingly
    2299        pad = " " * self.pad
    2300        stripped_sep = sep.strip()
    2301
    2302        result_sqls = []
    2303        for i, e in enumerate(expressions):
    2304            sql = self.sql(e, comment=False)
    2305            comments = self.maybe_comment("", e) if isinstance(e, exp.Expression) else ""
    2306
    2307            if self.pretty:
    2308                if self.leading_comma:
    2309                    result_sqls.append(f"{sep if i > 0 else pad}{prefix}{sql}{comments}")
    2310                else:
    2311                    result_sqls.append(
    2312                        f"{prefix}{sql}{stripped_sep if i + 1 < num_sqls else ''}{comments}"
    2313                    )
    2314            else:
    2315                result_sqls.append(f"{prefix}{sql}{comments}{sep if i + 1 < num_sqls else ''}")
    2316
    2317        result_sql = "\n".join(result_sqls) if self.pretty else "".join(result_sqls)
    2318        return self.indent(result_sql, skip_first=False) if indent else result_sql

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

    2320    def op_expressions(self, op: str, expression: exp.Expression, flat: bool = False) -> str:
    2321        flat = flat or isinstance(expression.parent, exp.Properties)
    2322        expressions_sql = self.expressions(expression, flat=flat)
    2323        if flat:
    2324            return f"{op} {expressions_sql}"
    2325        return f"{self.seg(op)}{self.sep() if expressions_sql else ''}{expressions_sql}"

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

    2327    def naked_property(self, expression: exp.Property) -> str:
    2328        property_name = exp.Properties.PROPERTY_TO_NAME.get(expression.__class__)
    2329        if not property_name:
    2330            self.unsupported(f"Unsupported property {expression.__class__.__name__}")
    2331        return f"{property_name} {self.sql(expression, 'this')}"

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

    2333    def set_operation(self, expression: exp.Expression, op: str) -> str:
    2334        this = self.sql(expression, "this")
    2335        op = self.seg(op)
    2336        return self.query_modifiers(
    2337            expression, f"{this}{op}{self.sep()}{self.sql(expression, 'expression')}"
    2338        )

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

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

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

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

    2346    def userdefinedfunction_sql(self, expression: exp.UserDefinedFunction) -> str:
    2347        this = self.sql(expression, "this")
    2348        expressions = self.no_identify(self.expressions, expression)
    2349        expressions = (
    2350            self.wrap(expressions) if expression.args.get("wrapped") else f" {expressions}"
    2351        )
    2352        return f"{this}{expressions}"

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

    2354    def joinhint_sql(self, expression: exp.JoinHint) -> str:
    2355        this = self.sql(expression, "this")
    2356        expressions = self.expressions(expression, flat=True)
    2357        return f"{this}({expressions})"

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

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

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

    2362    def when_sql(self, expression: exp.When) -> str:
    2363        matched = "MATCHED" if expression.args["matched"] else "NOT MATCHED"
    2364        source = " BY SOURCE" if self.MATCHED_BY_SOURCE and expression.args.get("source") else ""
    2365        condition = self.sql(expression, "condition")
    2366        condition = f" AND {condition}" if condition else ""
    2367
    2368        then_expression = expression.args.get("then")
    2369        if isinstance(then_expression, exp.Insert):
    2370            then = f"INSERT {self.sql(then_expression, 'this')}"
    2371            if "expression" in then_expression.args:
    2372                then += f" VALUES {self.sql(then_expression, 'expression')}"
    2373        elif isinstance(then_expression, exp.Update):
    2374            if isinstance(then_expression.args.get("expressions"), exp.Star):
    2375                then = f"UPDATE {self.sql(then_expression, 'expressions')}"
    2376            else:
    2377                then = f"UPDATE SET {self.expressions(then_expression, flat=True)}"
    2378        else:
    2379            then = self.sql(then_expression)
    2380        return f"WHEN {matched}{source}{condition} THEN {then}"

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

    2382    def merge_sql(self, expression: exp.Merge) -> str:
    2383        this = self.sql(expression, "this")
    2384        using = f"USING {self.sql(expression, 'using')}"
    2385        on = f"ON {self.sql(expression, 'on')}"
    2386        return f"MERGE INTO {this} {using} {on} {self.expressions(expression, sep=' ')}"

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

    2388    def tochar_sql(self, expression: exp.ToChar) -> str:
    2389        if expression.args.get("format"):
    2390            self.unsupported("Format argument unsupported for TO_CHAR/TO_VARCHAR function")
    2391
    2392        return self.sql(exp.cast(expression.this, "text"))

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

    2394    def dictproperty_sql(self, expression: exp.DictProperty) -> str:
    2395        this = self.sql(expression, "this")
    2396        kind = self.sql(expression, "kind")
    2397        settings_sql = self.expressions(expression, key="settings", sep=" ")
    2398        args = f"({self.sep('')}{settings_sql}{self.seg(')', sep='')}" if settings_sql else "()"
    2399        return f"{this}({kind}{args})"

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

    2401    def dictrange_sql(self, expression: exp.DictRange) -> str:
    2402        this = self.sql(expression, "this")
    2403        max = self.sql(expression, "max")
    2404        min = self.sql(expression, "min")
    2405        return f"{this}(MIN {min} MAX {max})"

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

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

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

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

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

    2414def cached_generator(
    2415    cache: t.Optional[t.Dict[int, str]] = None
    2416) -> t.Callable[[exp.Expression], str]:
    2417    """Returns a cached generator."""
    2418    cache = {} if cache is None else cache
    2419    generator = Generator(normalize=True, identify="safe")
    2420    return lambda e: generator.generate(e, cache)

Returns a cached generator.