Edit on GitHub

Expressions

Every AST node in SQLGlot is represented by a subclass of Expression.

This module contains the implementation of all supported Expression types. Additionally, it exposes a number of helper functions, which are mainly used to programmatically build SQL expressions, such as sqlglot.expressions.select.


   1"""
   2## Expressions
   3
   4Every AST node in SQLGlot is represented by a subclass of `Expression`.
   5
   6This module contains the implementation of all supported `Expression` types. Additionally,
   7it exposes a number of helper functions, which are mainly used to programmatically build
   8SQL expressions, such as `sqlglot.expressions.select`.
   9
  10----
  11"""
  12
  13from __future__ import annotations
  14
  15import datetime
  16import math
  17import numbers
  18import re
  19import textwrap
  20import typing as t
  21from collections import deque
  22from copy import deepcopy
  23from decimal import Decimal
  24from enum import auto
  25from functools import reduce
  26
  27from sqlglot.errors import ErrorLevel, ParseError
  28from sqlglot.helper import (
  29    AutoName,
  30    camel_to_snake_case,
  31    ensure_collection,
  32    ensure_list,
  33    seq_get,
  34    split_num_words,
  35    subclasses,
  36    to_bool,
  37)
  38from sqlglot.tokens import Token, TokenError
  39
  40if t.TYPE_CHECKING:
  41    from typing_extensions import Self
  42
  43    from sqlglot._typing import E, Lit
  44    from sqlglot.dialects.dialect import DialectType
  45
  46    Q = t.TypeVar("Q", bound="Query")
  47    S = t.TypeVar("S", bound="SetOperation")
  48
  49
  50class _Expression(type):
  51    def __new__(cls, clsname, bases, attrs):
  52        klass = super().__new__(cls, clsname, bases, attrs)
  53
  54        # When an Expression class is created, its key is automatically set
  55        # to be the lowercase version of the class' name.
  56        klass.key = clsname.lower()
  57
  58        # This is so that docstrings are not inherited in pdoc
  59        klass.__doc__ = klass.__doc__ or ""
  60
  61        return klass
  62
  63
  64SQLGLOT_META = "sqlglot.meta"
  65SQLGLOT_ANONYMOUS = "sqlglot.anonymous"
  66TABLE_PARTS = ("this", "db", "catalog")
  67COLUMN_PARTS = ("this", "table", "db", "catalog")
  68POSITION_META_KEYS = ("line", "col", "start", "end")
  69
  70
  71class Expression(metaclass=_Expression):
  72    """
  73    The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary
  74    context, such as its child expressions, their names (arg keys), and whether a given child expression
  75    is optional or not.
  76
  77    Attributes:
  78        key: a unique key for each class in the Expression hierarchy. This is useful for hashing
  79            and representing expressions as strings.
  80        arg_types: determines the arguments (child nodes) supported by an expression. It maps
  81            arg keys to booleans that indicate whether the corresponding args are optional.
  82        parent: a reference to the parent expression (or None, in case of root expressions).
  83        arg_key: the arg key an expression is associated with, i.e. the name its parent expression
  84            uses to refer to it.
  85        index: the index of an expression if it is inside of a list argument in its parent.
  86        comments: a list of comments that are associated with a given expression. This is used in
  87            order to preserve comments when transpiling SQL code.
  88        type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the
  89            optimizer, in order to enable some transformations that require type information.
  90        meta: a dictionary that can be used to store useful metadata for a given expression.
  91
  92    Example:
  93        >>> class Foo(Expression):
  94        ...     arg_types = {"this": True, "expression": False}
  95
  96        The above definition informs us that Foo is an Expression that requires an argument called
  97        "this" and may also optionally receive an argument called "expression".
  98
  99    Args:
 100        args: a mapping used for retrieving the arguments of an expression, given their arg keys.
 101    """
 102
 103    key = "expression"
 104    arg_types = {"this": True}
 105    __slots__ = ("args", "parent", "arg_key", "index", "comments", "_type", "_meta", "_hash")
 106
 107    def __init__(self, **args: t.Any):
 108        self.args: t.Dict[str, t.Any] = args
 109        self.parent: t.Optional[Expression] = None
 110        self.arg_key: t.Optional[str] = None
 111        self.index: t.Optional[int] = None
 112        self.comments: t.Optional[t.List[str]] = None
 113        self._type: t.Optional[DataType] = None
 114        self._meta: t.Optional[t.Dict[str, t.Any]] = None
 115        self._hash: t.Optional[int] = None
 116
 117        for arg_key, value in self.args.items():
 118            self._set_parent(arg_key, value)
 119
 120    def __eq__(self, other) -> bool:
 121        return type(self) is type(other) and hash(self) == hash(other)
 122
 123    @property
 124    def hashable_args(self) -> t.Any:
 125        return frozenset(
 126            (k, tuple(_norm_arg(a) for a in v) if type(v) is list else _norm_arg(v))
 127            for k, v in self.args.items()
 128            if not (v is None or v is False or (type(v) is list and not v))
 129        )
 130
 131    def __hash__(self) -> int:
 132        if self._hash is not None:
 133            return self._hash
 134
 135        return hash((self.__class__, self.hashable_args))
 136
 137    @property
 138    def this(self) -> t.Any:
 139        """
 140        Retrieves the argument with key "this".
 141        """
 142        return self.args.get("this")
 143
 144    @property
 145    def expression(self) -> t.Any:
 146        """
 147        Retrieves the argument with key "expression".
 148        """
 149        return self.args.get("expression")
 150
 151    @property
 152    def expressions(self) -> t.List[t.Any]:
 153        """
 154        Retrieves the argument with key "expressions".
 155        """
 156        return self.args.get("expressions") or []
 157
 158    def text(self, key) -> str:
 159        """
 160        Returns a textual representation of the argument corresponding to "key". This can only be used
 161        for args that are strings or leaf Expression instances, such as identifiers and literals.
 162        """
 163        field = self.args.get(key)
 164        if isinstance(field, str):
 165            return field
 166        if isinstance(field, (Identifier, Literal, Var)):
 167            return field.this
 168        if isinstance(field, (Star, Null)):
 169            return field.name
 170        return ""
 171
 172    @property
 173    def is_string(self) -> bool:
 174        """
 175        Checks whether a Literal expression is a string.
 176        """
 177        return isinstance(self, Literal) and self.args["is_string"]
 178
 179    @property
 180    def is_number(self) -> bool:
 181        """
 182        Checks whether a Literal expression is a number.
 183        """
 184        return (isinstance(self, Literal) and not self.args["is_string"]) or (
 185            isinstance(self, Neg) and self.this.is_number
 186        )
 187
 188    def to_py(self) -> t.Any:
 189        """
 190        Returns a Python object equivalent of the SQL node.
 191        """
 192        raise ValueError(f"{self} cannot be converted to a Python object.")
 193
 194    @property
 195    def is_int(self) -> bool:
 196        """
 197        Checks whether an expression is an integer.
 198        """
 199        return self.is_number and isinstance(self.to_py(), int)
 200
 201    @property
 202    def is_star(self) -> bool:
 203        """Checks whether an expression is a star."""
 204        return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star))
 205
 206    @property
 207    def alias(self) -> str:
 208        """
 209        Returns the alias of the expression, or an empty string if it's not aliased.
 210        """
 211        if isinstance(self.args.get("alias"), TableAlias):
 212            return self.args["alias"].name
 213        return self.text("alias")
 214
 215    @property
 216    def alias_column_names(self) -> t.List[str]:
 217        table_alias = self.args.get("alias")
 218        if not table_alias:
 219            return []
 220        return [c.name for c in table_alias.args.get("columns") or []]
 221
 222    @property
 223    def name(self) -> str:
 224        return self.text("this")
 225
 226    @property
 227    def alias_or_name(self) -> str:
 228        return self.alias or self.name
 229
 230    @property
 231    def output_name(self) -> str:
 232        """
 233        Name of the output column if this expression is a selection.
 234
 235        If the Expression has no output name, an empty string is returned.
 236
 237        Example:
 238            >>> from sqlglot import parse_one
 239            >>> parse_one("SELECT a").expressions[0].output_name
 240            'a'
 241            >>> parse_one("SELECT b AS c").expressions[0].output_name
 242            'c'
 243            >>> parse_one("SELECT 1 + 2").expressions[0].output_name
 244            ''
 245        """
 246        return ""
 247
 248    @property
 249    def type(self) -> t.Optional[DataType]:
 250        return self._type
 251
 252    @type.setter
 253    def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None:
 254        if dtype and not isinstance(dtype, DataType):
 255            dtype = DataType.build(dtype)
 256        self._type = dtype  # type: ignore
 257
 258    def is_type(self, *dtypes) -> bool:
 259        return self.type is not None and self.type.is_type(*dtypes)
 260
 261    def is_leaf(self) -> bool:
 262        return not any(isinstance(v, (Expression, list)) for v in self.args.values())
 263
 264    @property
 265    def meta(self) -> t.Dict[str, t.Any]:
 266        if self._meta is None:
 267            self._meta = {}
 268        return self._meta
 269
 270    def __deepcopy__(self, memo):
 271        root = self.__class__()
 272        stack = [(self, root)]
 273
 274        while stack:
 275            node, copy = stack.pop()
 276
 277            if node.comments is not None:
 278                copy.comments = deepcopy(node.comments)
 279            if node._type is not None:
 280                copy._type = deepcopy(node._type)
 281            if node._meta is not None:
 282                copy._meta = deepcopy(node._meta)
 283            if node._hash is not None:
 284                copy._hash = node._hash
 285
 286            for k, vs in node.args.items():
 287                if hasattr(vs, "parent"):
 288                    stack.append((vs, vs.__class__()))
 289                    copy.set(k, stack[-1][-1])
 290                elif type(vs) is list:
 291                    copy.args[k] = []
 292
 293                    for v in vs:
 294                        if hasattr(v, "parent"):
 295                            stack.append((v, v.__class__()))
 296                            copy.append(k, stack[-1][-1])
 297                        else:
 298                            copy.append(k, v)
 299                else:
 300                    copy.args[k] = vs
 301
 302        return root
 303
 304    def copy(self) -> Self:
 305        """
 306        Returns a deep copy of the expression.
 307        """
 308        return deepcopy(self)
 309
 310    def add_comments(self, comments: t.Optional[t.List[str]] = None, prepend: bool = False) -> None:
 311        if self.comments is None:
 312            self.comments = []
 313
 314        if comments:
 315            for comment in comments:
 316                _, *meta = comment.split(SQLGLOT_META)
 317                if meta:
 318                    for kv in "".join(meta).split(","):
 319                        k, *v = kv.split("=")
 320                        value = v[0].strip() if v else True
 321                        self.meta[k.strip()] = to_bool(value)
 322
 323                if not prepend:
 324                    self.comments.append(comment)
 325
 326            if prepend:
 327                self.comments = comments + self.comments
 328
 329    def pop_comments(self) -> t.List[str]:
 330        comments = self.comments or []
 331        self.comments = None
 332        return comments
 333
 334    def append(self, arg_key: str, value: t.Any) -> None:
 335        """
 336        Appends value to arg_key if it's a list or sets it as a new list.
 337
 338        Args:
 339            arg_key (str): name of the list expression arg
 340            value (Any): value to append to the list
 341        """
 342        if type(self.args.get(arg_key)) is not list:
 343            self.args[arg_key] = []
 344        self._set_parent(arg_key, value)
 345        values = self.args[arg_key]
 346        if hasattr(value, "parent"):
 347            value.index = len(values)
 348        values.append(value)
 349
 350    def set(
 351        self,
 352        arg_key: str,
 353        value: t.Any,
 354        index: t.Optional[int] = None,
 355        overwrite: bool = True,
 356    ) -> None:
 357        """
 358        Sets arg_key to value.
 359
 360        Args:
 361            arg_key: name of the expression arg.
 362            value: value to set the arg to.
 363            index: if the arg is a list, this specifies what position to add the value in it.
 364            overwrite: assuming an index is given, this determines whether to overwrite the
 365                list entry instead of only inserting a new value (i.e., like list.insert).
 366        """
 367        if index is not None:
 368            expressions = self.args.get(arg_key) or []
 369
 370            if seq_get(expressions, index) is None:
 371                return
 372            if value is None:
 373                expressions.pop(index)
 374                for v in expressions[index:]:
 375                    v.index = v.index - 1
 376                return
 377
 378            if isinstance(value, list):
 379                expressions.pop(index)
 380                expressions[index:index] = value
 381            elif overwrite:
 382                expressions[index] = value
 383            else:
 384                expressions.insert(index, value)
 385
 386            value = expressions
 387        elif value is None:
 388            self.args.pop(arg_key, None)
 389            return
 390
 391        self.args[arg_key] = value
 392        self._set_parent(arg_key, value, index)
 393
 394    def _set_parent(self, arg_key: str, value: t.Any, index: t.Optional[int] = None) -> None:
 395        if hasattr(value, "parent"):
 396            value.parent = self
 397            value.arg_key = arg_key
 398            value.index = index
 399        elif type(value) is list:
 400            for index, v in enumerate(value):
 401                if hasattr(v, "parent"):
 402                    v.parent = self
 403                    v.arg_key = arg_key
 404                    v.index = index
 405
 406    @property
 407    def depth(self) -> int:
 408        """
 409        Returns the depth of this tree.
 410        """
 411        if self.parent:
 412            return self.parent.depth + 1
 413        return 0
 414
 415    def iter_expressions(self, reverse: bool = False) -> t.Iterator[Expression]:
 416        """Yields the key and expression for all arguments, exploding list args."""
 417        for vs in reversed(self.args.values()) if reverse else self.args.values():  # type: ignore
 418            if type(vs) is list:
 419                for v in reversed(vs) if reverse else vs:  # type: ignore
 420                    if hasattr(v, "parent"):
 421                        yield v
 422            else:
 423                if hasattr(vs, "parent"):
 424                    yield vs
 425
 426    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
 427        """
 428        Returns the first node in this tree which matches at least one of
 429        the specified types.
 430
 431        Args:
 432            expression_types: the expression type(s) to match.
 433            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
 434
 435        Returns:
 436            The node which matches the criteria or None if no such node was found.
 437        """
 438        return next(self.find_all(*expression_types, bfs=bfs), None)
 439
 440    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
 441        """
 442        Returns a generator object which visits all nodes in this tree and only
 443        yields those that match at least one of the specified expression types.
 444
 445        Args:
 446            expression_types: the expression type(s) to match.
 447            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
 448
 449        Returns:
 450            The generator object.
 451        """
 452        for expression in self.walk(bfs=bfs):
 453            if isinstance(expression, expression_types):
 454                yield expression
 455
 456    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
 457        """
 458        Returns a nearest parent matching expression_types.
 459
 460        Args:
 461            expression_types: the expression type(s) to match.
 462
 463        Returns:
 464            The parent node.
 465        """
 466        ancestor = self.parent
 467        while ancestor and not isinstance(ancestor, expression_types):
 468            ancestor = ancestor.parent
 469        return ancestor  # type: ignore
 470
 471    @property
 472    def parent_select(self) -> t.Optional[Select]:
 473        """
 474        Returns the parent select statement.
 475        """
 476        return self.find_ancestor(Select)
 477
 478    @property
 479    def same_parent(self) -> bool:
 480        """Returns if the parent is the same class as itself."""
 481        return type(self.parent) is self.__class__
 482
 483    def root(self) -> Expression:
 484        """
 485        Returns the root expression of this tree.
 486        """
 487        expression = self
 488        while expression.parent:
 489            expression = expression.parent
 490        return expression
 491
 492    def walk(
 493        self, bfs: bool = True, prune: t.Optional[t.Callable[[Expression], bool]] = None
 494    ) -> t.Iterator[Expression]:
 495        """
 496        Returns a generator object which visits all nodes in this tree.
 497
 498        Args:
 499            bfs: if set to True the BFS traversal order will be applied,
 500                otherwise the DFS traversal will be used instead.
 501            prune: callable that returns True if the generator should stop traversing
 502                this branch of the tree.
 503
 504        Returns:
 505            the generator object.
 506        """
 507        if bfs:
 508            yield from self.bfs(prune=prune)
 509        else:
 510            yield from self.dfs(prune=prune)
 511
 512    def dfs(
 513        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
 514    ) -> t.Iterator[Expression]:
 515        """
 516        Returns a generator object which visits all nodes in this tree in
 517        the DFS (Depth-first) order.
 518
 519        Returns:
 520            The generator object.
 521        """
 522        stack = [self]
 523
 524        while stack:
 525            node = stack.pop()
 526
 527            yield node
 528
 529            if prune and prune(node):
 530                continue
 531
 532            for v in node.iter_expressions(reverse=True):
 533                stack.append(v)
 534
 535    def bfs(
 536        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
 537    ) -> t.Iterator[Expression]:
 538        """
 539        Returns a generator object which visits all nodes in this tree in
 540        the BFS (Breadth-first) order.
 541
 542        Returns:
 543            The generator object.
 544        """
 545        queue = deque([self])
 546
 547        while queue:
 548            node = queue.popleft()
 549
 550            yield node
 551
 552            if prune and prune(node):
 553                continue
 554
 555            for v in node.iter_expressions():
 556                queue.append(v)
 557
 558    def unnest(self):
 559        """
 560        Returns the first non parenthesis child or self.
 561        """
 562        expression = self
 563        while type(expression) is Paren:
 564            expression = expression.this
 565        return expression
 566
 567    def unalias(self):
 568        """
 569        Returns the inner expression if this is an Alias.
 570        """
 571        if isinstance(self, Alias):
 572            return self.this
 573        return self
 574
 575    def unnest_operands(self):
 576        """
 577        Returns unnested operands as a tuple.
 578        """
 579        return tuple(arg.unnest() for arg in self.iter_expressions())
 580
 581    def flatten(self, unnest=True):
 582        """
 583        Returns a generator which yields child nodes whose parents are the same class.
 584
 585        A AND B AND C -> [A, B, C]
 586        """
 587        for node in self.dfs(prune=lambda n: n.parent and type(n) is not self.__class__):
 588            if type(node) is not self.__class__:
 589                yield node.unnest() if unnest and not isinstance(node, Subquery) else node
 590
 591    def __str__(self) -> str:
 592        return self.sql()
 593
 594    def __repr__(self) -> str:
 595        return _to_s(self)
 596
 597    def to_s(self) -> str:
 598        """
 599        Same as __repr__, but includes additional information which can be useful
 600        for debugging, like empty or missing args and the AST nodes' object IDs.
 601        """
 602        return _to_s(self, verbose=True)
 603
 604    def sql(self, dialect: DialectType = None, **opts) -> str:
 605        """
 606        Returns SQL string representation of this tree.
 607
 608        Args:
 609            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
 610            opts: other `sqlglot.generator.Generator` options.
 611
 612        Returns:
 613            The SQL string.
 614        """
 615        from sqlglot.dialects import Dialect
 616
 617        return Dialect.get_or_raise(dialect).generate(self, **opts)
 618
 619    def transform(self, fun: t.Callable, *args: t.Any, copy: bool = True, **kwargs) -> Expression:
 620        """
 621        Visits all tree nodes (excluding already transformed ones)
 622        and applies the given transformation function to each node.
 623
 624        Args:
 625            fun: a function which takes a node as an argument and returns a
 626                new transformed node or the same node without modifications. If the function
 627                returns None, then the corresponding node will be removed from the syntax tree.
 628            copy: if set to True a new tree instance is constructed, otherwise the tree is
 629                modified in place.
 630
 631        Returns:
 632            The transformed tree.
 633        """
 634        root = None
 635        new_node = None
 636
 637        for node in (self.copy() if copy else self).dfs(prune=lambda n: n is not new_node):
 638            parent, arg_key, index = node.parent, node.arg_key, node.index
 639            new_node = fun(node, *args, **kwargs)
 640
 641            if not root:
 642                root = new_node
 643            elif parent and arg_key and new_node is not node:
 644                parent.set(arg_key, new_node, index)
 645
 646        assert root
 647        return root.assert_is(Expression)
 648
 649    @t.overload
 650    def replace(self, expression: E) -> E: ...
 651
 652    @t.overload
 653    def replace(self, expression: None) -> None: ...
 654
 655    def replace(self, expression):
 656        """
 657        Swap out this expression with a new expression.
 658
 659        For example::
 660
 661            >>> tree = Select().select("x").from_("tbl")
 662            >>> tree.find(Column).replace(column("y"))
 663            Column(
 664              this=Identifier(this=y, quoted=False))
 665            >>> tree.sql()
 666            'SELECT y FROM tbl'
 667
 668        Args:
 669            expression: new node
 670
 671        Returns:
 672            The new expression or expressions.
 673        """
 674        parent = self.parent
 675
 676        if not parent or parent is expression:
 677            return expression
 678
 679        key = self.arg_key
 680        value = parent.args.get(key)
 681
 682        if type(expression) is list and isinstance(value, Expression):
 683            # We are trying to replace an Expression with a list, so it's assumed that
 684            # the intention was to really replace the parent of this expression.
 685            value.parent.replace(expression)
 686        else:
 687            parent.set(key, expression, self.index)
 688
 689        if expression is not self:
 690            self.parent = None
 691            self.arg_key = None
 692            self.index = None
 693
 694        return expression
 695
 696    def pop(self: E) -> E:
 697        """
 698        Remove this expression from its AST.
 699
 700        Returns:
 701            The popped expression.
 702        """
 703        self.replace(None)
 704        return self
 705
 706    def assert_is(self, type_: t.Type[E]) -> E:
 707        """
 708        Assert that this `Expression` is an instance of `type_`.
 709
 710        If it is NOT an instance of `type_`, this raises an assertion error.
 711        Otherwise, this returns this expression.
 712
 713        Examples:
 714            This is useful for type security in chained expressions:
 715
 716            >>> import sqlglot
 717            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
 718            'SELECT x, z FROM y'
 719        """
 720        if not isinstance(self, type_):
 721            raise AssertionError(f"{self} is not {type_}.")
 722        return self
 723
 724    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
 725        """
 726        Checks if this expression is valid (e.g. all mandatory args are set).
 727
 728        Args:
 729            args: a sequence of values that were used to instantiate a Func expression. This is used
 730                to check that the provided arguments don't exceed the function argument limit.
 731
 732        Returns:
 733            A list of error messages for all possible errors that were found.
 734        """
 735        errors: t.List[str] = []
 736
 737        for k in self.args:
 738            if k not in self.arg_types:
 739                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
 740        for k, mandatory in self.arg_types.items():
 741            v = self.args.get(k)
 742            if mandatory and (v is None or (isinstance(v, list) and not v)):
 743                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
 744
 745        if (
 746            args
 747            and isinstance(self, Func)
 748            and len(args) > len(self.arg_types)
 749            and not self.is_var_len_args
 750        ):
 751            errors.append(
 752                f"The number of provided arguments ({len(args)}) is greater than "
 753                f"the maximum number of supported arguments ({len(self.arg_types)})"
 754            )
 755
 756        return errors
 757
 758    def dump(self):
 759        """
 760        Dump this Expression to a JSON-serializable dict.
 761        """
 762        from sqlglot.serde import dump
 763
 764        return dump(self)
 765
 766    @classmethod
 767    def load(cls, obj):
 768        """
 769        Load a dict (as returned by `Expression.dump`) into an Expression instance.
 770        """
 771        from sqlglot.serde import load
 772
 773        return load(obj)
 774
 775    def and_(
 776        self,
 777        *expressions: t.Optional[ExpOrStr],
 778        dialect: DialectType = None,
 779        copy: bool = True,
 780        wrap: bool = True,
 781        **opts,
 782    ) -> Condition:
 783        """
 784        AND this condition with one or multiple expressions.
 785
 786        Example:
 787            >>> condition("x=1").and_("y=1").sql()
 788            'x = 1 AND y = 1'
 789
 790        Args:
 791            *expressions: the SQL code strings to parse.
 792                If an `Expression` instance is passed, it will be used as-is.
 793            dialect: the dialect used to parse the input expression.
 794            copy: whether to copy the involved expressions (only applies to Expressions).
 795            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
 796                precedence issues, but can be turned off when the produced AST is too deep and
 797                causes recursion-related issues.
 798            opts: other options to use to parse the input expressions.
 799
 800        Returns:
 801            The new And condition.
 802        """
 803        return and_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)
 804
 805    def or_(
 806        self,
 807        *expressions: t.Optional[ExpOrStr],
 808        dialect: DialectType = None,
 809        copy: bool = True,
 810        wrap: bool = True,
 811        **opts,
 812    ) -> Condition:
 813        """
 814        OR this condition with one or multiple expressions.
 815
 816        Example:
 817            >>> condition("x=1").or_("y=1").sql()
 818            'x = 1 OR y = 1'
 819
 820        Args:
 821            *expressions: the SQL code strings to parse.
 822                If an `Expression` instance is passed, it will be used as-is.
 823            dialect: the dialect used to parse the input expression.
 824            copy: whether to copy the involved expressions (only applies to Expressions).
 825            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
 826                precedence issues, but can be turned off when the produced AST is too deep and
 827                causes recursion-related issues.
 828            opts: other options to use to parse the input expressions.
 829
 830        Returns:
 831            The new Or condition.
 832        """
 833        return or_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)
 834
 835    def not_(self, copy: bool = True):
 836        """
 837        Wrap this condition with NOT.
 838
 839        Example:
 840            >>> condition("x=1").not_().sql()
 841            'NOT x = 1'
 842
 843        Args:
 844            copy: whether to copy this object.
 845
 846        Returns:
 847            The new Not instance.
 848        """
 849        return not_(self, copy=copy)
 850
 851    def update_positions(
 852        self: E, other: t.Optional[Token | Expression] = None, **kwargs: t.Any
 853    ) -> E:
 854        """
 855        Update this expression with positions from a token or other expression.
 856
 857        Args:
 858            other: a token or expression to update this expression with.
 859
 860        Returns:
 861            The updated expression.
 862        """
 863        if isinstance(other, Expression):
 864            self.meta.update({k: v for k, v in other.meta.items() if k in POSITION_META_KEYS})
 865        elif other is not None:
 866            self.meta.update(
 867                {
 868                    "line": other.line,
 869                    "col": other.col,
 870                    "start": other.start,
 871                    "end": other.end,
 872                }
 873            )
 874        self.meta.update({k: v for k, v in kwargs.items() if k in POSITION_META_KEYS})
 875        return self
 876
 877    def as_(
 878        self,
 879        alias: str | Identifier,
 880        quoted: t.Optional[bool] = None,
 881        dialect: DialectType = None,
 882        copy: bool = True,
 883        **opts,
 884    ) -> Alias:
 885        return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts)
 886
 887    def _binop(self, klass: t.Type[E], other: t.Any, reverse: bool = False) -> E:
 888        this = self.copy()
 889        other = convert(other, copy=True)
 890        if not isinstance(this, klass) and not isinstance(other, klass):
 891            this = _wrap(this, Binary)
 892            other = _wrap(other, Binary)
 893        if reverse:
 894            return klass(this=other, expression=this)
 895        return klass(this=this, expression=other)
 896
 897    def __getitem__(self, other: ExpOrStr | t.Tuple[ExpOrStr]) -> Bracket:
 898        return Bracket(
 899            this=self.copy(), expressions=[convert(e, copy=True) for e in ensure_list(other)]
 900        )
 901
 902    def __iter__(self) -> t.Iterator:
 903        if "expressions" in self.arg_types:
 904            return iter(self.args.get("expressions") or [])
 905        # We define this because __getitem__ converts Expression into an iterable, which is
 906        # problematic because one can hit infinite loops if they do "for x in some_expr: ..."
 907        # See: https://peps.python.org/pep-0234/
 908        raise TypeError(f"'{self.__class__.__name__}' object is not iterable")
 909
 910    def isin(
 911        self,
 912        *expressions: t.Any,
 913        query: t.Optional[ExpOrStr] = None,
 914        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
 915        copy: bool = True,
 916        **opts,
 917    ) -> In:
 918        subquery = maybe_parse(query, copy=copy, **opts) if query else None
 919        if subquery and not isinstance(subquery, Subquery):
 920            subquery = subquery.subquery(copy=False)
 921
 922        return In(
 923            this=maybe_copy(self, copy),
 924            expressions=[convert(e, copy=copy) for e in expressions],
 925            query=subquery,
 926            unnest=(
 927                Unnest(
 928                    expressions=[
 929                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
 930                        for e in ensure_list(unnest)
 931                    ]
 932                )
 933                if unnest
 934                else None
 935            ),
 936        )
 937
 938    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
 939        return Between(
 940            this=maybe_copy(self, copy),
 941            low=convert(low, copy=copy, **opts),
 942            high=convert(high, copy=copy, **opts),
 943        )
 944
 945    def is_(self, other: ExpOrStr) -> Is:
 946        return self._binop(Is, other)
 947
 948    def like(self, other: ExpOrStr) -> Like:
 949        return self._binop(Like, other)
 950
 951    def ilike(self, other: ExpOrStr) -> ILike:
 952        return self._binop(ILike, other)
 953
 954    def eq(self, other: t.Any) -> EQ:
 955        return self._binop(EQ, other)
 956
 957    def neq(self, other: t.Any) -> NEQ:
 958        return self._binop(NEQ, other)
 959
 960    def rlike(self, other: ExpOrStr) -> RegexpLike:
 961        return self._binop(RegexpLike, other)
 962
 963    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
 964        div = self._binop(Div, other)
 965        div.args["typed"] = typed
 966        div.args["safe"] = safe
 967        return div
 968
 969    def asc(self, nulls_first: bool = True) -> Ordered:
 970        return Ordered(this=self.copy(), nulls_first=nulls_first)
 971
 972    def desc(self, nulls_first: bool = False) -> Ordered:
 973        return Ordered(this=self.copy(), desc=True, nulls_first=nulls_first)
 974
 975    def __lt__(self, other: t.Any) -> LT:
 976        return self._binop(LT, other)
 977
 978    def __le__(self, other: t.Any) -> LTE:
 979        return self._binop(LTE, other)
 980
 981    def __gt__(self, other: t.Any) -> GT:
 982        return self._binop(GT, other)
 983
 984    def __ge__(self, other: t.Any) -> GTE:
 985        return self._binop(GTE, other)
 986
 987    def __add__(self, other: t.Any) -> Add:
 988        return self._binop(Add, other)
 989
 990    def __radd__(self, other: t.Any) -> Add:
 991        return self._binop(Add, other, reverse=True)
 992
 993    def __sub__(self, other: t.Any) -> Sub:
 994        return self._binop(Sub, other)
 995
 996    def __rsub__(self, other: t.Any) -> Sub:
 997        return self._binop(Sub, other, reverse=True)
 998
 999    def __mul__(self, other: t.Any) -> Mul:
1000        return self._binop(Mul, other)
1001
1002    def __rmul__(self, other: t.Any) -> Mul:
1003        return self._binop(Mul, other, reverse=True)
1004
1005    def __truediv__(self, other: t.Any) -> Div:
1006        return self._binop(Div, other)
1007
1008    def __rtruediv__(self, other: t.Any) -> Div:
1009        return self._binop(Div, other, reverse=True)
1010
1011    def __floordiv__(self, other: t.Any) -> IntDiv:
1012        return self._binop(IntDiv, other)
1013
1014    def __rfloordiv__(self, other: t.Any) -> IntDiv:
1015        return self._binop(IntDiv, other, reverse=True)
1016
1017    def __mod__(self, other: t.Any) -> Mod:
1018        return self._binop(Mod, other)
1019
1020    def __rmod__(self, other: t.Any) -> Mod:
1021        return self._binop(Mod, other, reverse=True)
1022
1023    def __pow__(self, other: t.Any) -> Pow:
1024        return self._binop(Pow, other)
1025
1026    def __rpow__(self, other: t.Any) -> Pow:
1027        return self._binop(Pow, other, reverse=True)
1028
1029    def __and__(self, other: t.Any) -> And:
1030        return self._binop(And, other)
1031
1032    def __rand__(self, other: t.Any) -> And:
1033        return self._binop(And, other, reverse=True)
1034
1035    def __or__(self, other: t.Any) -> Or:
1036        return self._binop(Or, other)
1037
1038    def __ror__(self, other: t.Any) -> Or:
1039        return self._binop(Or, other, reverse=True)
1040
1041    def __neg__(self) -> Neg:
1042        return Neg(this=_wrap(self.copy(), Binary))
1043
1044    def __invert__(self) -> Not:
1045        return not_(self.copy())
1046
1047
1048IntoType = t.Union[
1049    str,
1050    t.Type[Expression],
1051    t.Collection[t.Union[str, t.Type[Expression]]],
1052]
1053ExpOrStr = t.Union[str, Expression]
1054
1055
1056class Condition(Expression):
1057    """Logical conditions like x AND y, or simply x"""
1058
1059
1060class Predicate(Condition):
1061    """Relationships like x = y, x > 1, x >= y."""
1062
1063
1064class DerivedTable(Expression):
1065    @property
1066    def selects(self) -> t.List[Expression]:
1067        return self.this.selects if isinstance(self.this, Query) else []
1068
1069    @property
1070    def named_selects(self) -> t.List[str]:
1071        return [select.output_name for select in self.selects]
1072
1073
1074class Query(Expression):
1075    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
1076        """
1077        Returns a `Subquery` that wraps around this query.
1078
1079        Example:
1080            >>> subquery = Select().select("x").from_("tbl").subquery()
1081            >>> Select().select("x").from_(subquery).sql()
1082            'SELECT x FROM (SELECT x FROM tbl)'
1083
1084        Args:
1085            alias: an optional alias for the subquery.
1086            copy: if `False`, modify this expression instance in-place.
1087        """
1088        instance = maybe_copy(self, copy)
1089        if not isinstance(alias, Expression):
1090            alias = TableAlias(this=to_identifier(alias)) if alias else None
1091
1092        return Subquery(this=instance, alias=alias)
1093
1094    def limit(
1095        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1096    ) -> Q:
1097        """
1098        Adds a LIMIT clause to this query.
1099
1100        Example:
1101            >>> select("1").union(select("1")).limit(1).sql()
1102            'SELECT 1 UNION SELECT 1 LIMIT 1'
1103
1104        Args:
1105            expression: the SQL code string to parse.
1106                This can also be an integer.
1107                If a `Limit` instance is passed, it will be used as-is.
1108                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
1109            dialect: the dialect used to parse the input expression.
1110            copy: if `False`, modify this expression instance in-place.
1111            opts: other options to use to parse the input expressions.
1112
1113        Returns:
1114            A limited Select expression.
1115        """
1116        return _apply_builder(
1117            expression=expression,
1118            instance=self,
1119            arg="limit",
1120            into=Limit,
1121            prefix="LIMIT",
1122            dialect=dialect,
1123            copy=copy,
1124            into_arg="expression",
1125            **opts,
1126        )
1127
1128    def offset(
1129        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1130    ) -> Q:
1131        """
1132        Set the OFFSET expression.
1133
1134        Example:
1135            >>> Select().from_("tbl").select("x").offset(10).sql()
1136            'SELECT x FROM tbl OFFSET 10'
1137
1138        Args:
1139            expression: the SQL code string to parse.
1140                This can also be an integer.
1141                If a `Offset` instance is passed, this is used as-is.
1142                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
1143            dialect: the dialect used to parse the input expression.
1144            copy: if `False`, modify this expression instance in-place.
1145            opts: other options to use to parse the input expressions.
1146
1147        Returns:
1148            The modified Select expression.
1149        """
1150        return _apply_builder(
1151            expression=expression,
1152            instance=self,
1153            arg="offset",
1154            into=Offset,
1155            prefix="OFFSET",
1156            dialect=dialect,
1157            copy=copy,
1158            into_arg="expression",
1159            **opts,
1160        )
1161
1162    def order_by(
1163        self: Q,
1164        *expressions: t.Optional[ExpOrStr],
1165        append: bool = True,
1166        dialect: DialectType = None,
1167        copy: bool = True,
1168        **opts,
1169    ) -> Q:
1170        """
1171        Set the ORDER BY expression.
1172
1173        Example:
1174            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
1175            'SELECT x FROM tbl ORDER BY x DESC'
1176
1177        Args:
1178            *expressions: the SQL code strings to parse.
1179                If a `Group` instance is passed, this is used as-is.
1180                If another `Expression` instance is passed, it will be wrapped in a `Order`.
1181            append: if `True`, add to any existing expressions.
1182                Otherwise, this flattens all the `Order` expression into a single expression.
1183            dialect: the dialect used to parse the input expression.
1184            copy: if `False`, modify this expression instance in-place.
1185            opts: other options to use to parse the input expressions.
1186
1187        Returns:
1188            The modified Select expression.
1189        """
1190        return _apply_child_list_builder(
1191            *expressions,
1192            instance=self,
1193            arg="order",
1194            append=append,
1195            copy=copy,
1196            prefix="ORDER BY",
1197            into=Order,
1198            dialect=dialect,
1199            **opts,
1200        )
1201
1202    @property
1203    def ctes(self) -> t.List[CTE]:
1204        """Returns a list of all the CTEs attached to this query."""
1205        with_ = self.args.get("with")
1206        return with_.expressions if with_ else []
1207
1208    @property
1209    def selects(self) -> t.List[Expression]:
1210        """Returns the query's projections."""
1211        raise NotImplementedError("Query objects must implement `selects`")
1212
1213    @property
1214    def named_selects(self) -> t.List[str]:
1215        """Returns the output names of the query's projections."""
1216        raise NotImplementedError("Query objects must implement `named_selects`")
1217
1218    def select(
1219        self: Q,
1220        *expressions: t.Optional[ExpOrStr],
1221        append: bool = True,
1222        dialect: DialectType = None,
1223        copy: bool = True,
1224        **opts,
1225    ) -> Q:
1226        """
1227        Append to or set the SELECT expressions.
1228
1229        Example:
1230            >>> Select().select("x", "y").sql()
1231            'SELECT x, y'
1232
1233        Args:
1234            *expressions: the SQL code strings to parse.
1235                If an `Expression` instance is passed, it will be used as-is.
1236            append: if `True`, add to any existing expressions.
1237                Otherwise, this resets the expressions.
1238            dialect: the dialect used to parse the input expressions.
1239            copy: if `False`, modify this expression instance in-place.
1240            opts: other options to use to parse the input expressions.
1241
1242        Returns:
1243            The modified Query expression.
1244        """
1245        raise NotImplementedError("Query objects must implement `select`")
1246
1247    def where(
1248        self: Q,
1249        *expressions: t.Optional[ExpOrStr],
1250        append: bool = True,
1251        dialect: DialectType = None,
1252        copy: bool = True,
1253        **opts,
1254    ) -> Q:
1255        """
1256        Append to or set the WHERE expressions.
1257
1258        Examples:
1259            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
1260            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
1261
1262        Args:
1263            *expressions: the SQL code strings to parse.
1264                If an `Expression` instance is passed, it will be used as-is.
1265                Multiple expressions are combined with an AND operator.
1266            append: if `True`, AND the new expressions to any existing expression.
1267                Otherwise, this resets the expression.
1268            dialect: the dialect used to parse the input expressions.
1269            copy: if `False`, modify this expression instance in-place.
1270            opts: other options to use to parse the input expressions.
1271
1272        Returns:
1273            The modified expression.
1274        """
1275        return _apply_conjunction_builder(
1276            *[expr.this if isinstance(expr, Where) else expr for expr in expressions],
1277            instance=self,
1278            arg="where",
1279            append=append,
1280            into=Where,
1281            dialect=dialect,
1282            copy=copy,
1283            **opts,
1284        )
1285
1286    def with_(
1287        self: Q,
1288        alias: ExpOrStr,
1289        as_: ExpOrStr,
1290        recursive: t.Optional[bool] = None,
1291        materialized: t.Optional[bool] = None,
1292        append: bool = True,
1293        dialect: DialectType = None,
1294        copy: bool = True,
1295        scalar: bool = False,
1296        **opts,
1297    ) -> Q:
1298        """
1299        Append to or set the common table expressions.
1300
1301        Example:
1302            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
1303            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
1304
1305        Args:
1306            alias: the SQL code string to parse as the table name.
1307                If an `Expression` instance is passed, this is used as-is.
1308            as_: the SQL code string to parse as the table expression.
1309                If an `Expression` instance is passed, it will be used as-is.
1310            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1311            materialized: set the MATERIALIZED part of the expression.
1312            append: if `True`, add to any existing expressions.
1313                Otherwise, this resets the expressions.
1314            dialect: the dialect used to parse the input expression.
1315            copy: if `False`, modify this expression instance in-place.
1316            scalar: if `True`, this is a scalar common table expression.
1317            opts: other options to use to parse the input expressions.
1318
1319        Returns:
1320            The modified expression.
1321        """
1322        return _apply_cte_builder(
1323            self,
1324            alias,
1325            as_,
1326            recursive=recursive,
1327            materialized=materialized,
1328            append=append,
1329            dialect=dialect,
1330            copy=copy,
1331            scalar=scalar,
1332            **opts,
1333        )
1334
1335    def union(
1336        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1337    ) -> Union:
1338        """
1339        Builds a UNION expression.
1340
1341        Example:
1342            >>> import sqlglot
1343            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
1344            'SELECT * FROM foo UNION SELECT * FROM bla'
1345
1346        Args:
1347            expressions: the SQL code strings.
1348                If `Expression` instances are passed, they will be used as-is.
1349            distinct: set the DISTINCT flag if and only if this is true.
1350            dialect: the dialect used to parse the input expression.
1351            opts: other options to use to parse the input expressions.
1352
1353        Returns:
1354            The new Union expression.
1355        """
1356        return union(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1357
1358    def intersect(
1359        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1360    ) -> Intersect:
1361        """
1362        Builds an INTERSECT expression.
1363
1364        Example:
1365            >>> import sqlglot
1366            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
1367            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
1368
1369        Args:
1370            expressions: the SQL code strings.
1371                If `Expression` instances are passed, they will be used as-is.
1372            distinct: set the DISTINCT flag if and only if this is true.
1373            dialect: the dialect used to parse the input expression.
1374            opts: other options to use to parse the input expressions.
1375
1376        Returns:
1377            The new Intersect expression.
1378        """
1379        return intersect(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1380
1381    def except_(
1382        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1383    ) -> Except:
1384        """
1385        Builds an EXCEPT expression.
1386
1387        Example:
1388            >>> import sqlglot
1389            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
1390            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
1391
1392        Args:
1393            expressions: the SQL code strings.
1394                If `Expression` instance are passed, they will be used as-is.
1395            distinct: set the DISTINCT flag if and only if this is true.
1396            dialect: the dialect used to parse the input expression.
1397            opts: other options to use to parse the input expressions.
1398
1399        Returns:
1400            The new Except expression.
1401        """
1402        return except_(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1403
1404
1405class UDTF(DerivedTable):
1406    @property
1407    def selects(self) -> t.List[Expression]:
1408        alias = self.args.get("alias")
1409        return alias.columns if alias else []
1410
1411
1412class Cache(Expression):
1413    arg_types = {
1414        "this": True,
1415        "lazy": False,
1416        "options": False,
1417        "expression": False,
1418    }
1419
1420
1421class Uncache(Expression):
1422    arg_types = {"this": True, "exists": False}
1423
1424
1425class Refresh(Expression):
1426    pass
1427
1428
1429class DDL(Expression):
1430    @property
1431    def ctes(self) -> t.List[CTE]:
1432        """Returns a list of all the CTEs attached to this statement."""
1433        with_ = self.args.get("with")
1434        return with_.expressions if with_ else []
1435
1436    @property
1437    def selects(self) -> t.List[Expression]:
1438        """If this statement contains a query (e.g. a CTAS), this returns the query's projections."""
1439        return self.expression.selects if isinstance(self.expression, Query) else []
1440
1441    @property
1442    def named_selects(self) -> t.List[str]:
1443        """
1444        If this statement contains a query (e.g. a CTAS), this returns the output
1445        names of the query's projections.
1446        """
1447        return self.expression.named_selects if isinstance(self.expression, Query) else []
1448
1449
1450class DML(Expression):
1451    def returning(
1452        self,
1453        expression: ExpOrStr,
1454        dialect: DialectType = None,
1455        copy: bool = True,
1456        **opts,
1457    ) -> "Self":
1458        """
1459        Set the RETURNING expression. Not supported by all dialects.
1460
1461        Example:
1462            >>> delete("tbl").returning("*", dialect="postgres").sql()
1463            'DELETE FROM tbl RETURNING *'
1464
1465        Args:
1466            expression: the SQL code strings to parse.
1467                If an `Expression` instance is passed, it will be used as-is.
1468            dialect: the dialect used to parse the input expressions.
1469            copy: if `False`, modify this expression instance in-place.
1470            opts: other options to use to parse the input expressions.
1471
1472        Returns:
1473            Delete: the modified expression.
1474        """
1475        return _apply_builder(
1476            expression=expression,
1477            instance=self,
1478            arg="returning",
1479            prefix="RETURNING",
1480            dialect=dialect,
1481            copy=copy,
1482            into=Returning,
1483            **opts,
1484        )
1485
1486
1487class Create(DDL):
1488    arg_types = {
1489        "with": False,
1490        "this": True,
1491        "kind": True,
1492        "expression": False,
1493        "exists": False,
1494        "properties": False,
1495        "replace": False,
1496        "refresh": False,
1497        "unique": False,
1498        "indexes": False,
1499        "no_schema_binding": False,
1500        "begin": False,
1501        "end": False,
1502        "clone": False,
1503        "concurrently": False,
1504        "clustered": False,
1505    }
1506
1507    @property
1508    def kind(self) -> t.Optional[str]:
1509        kind = self.args.get("kind")
1510        return kind and kind.upper()
1511
1512
1513class SequenceProperties(Expression):
1514    arg_types = {
1515        "increment": False,
1516        "minvalue": False,
1517        "maxvalue": False,
1518        "cache": False,
1519        "start": False,
1520        "owned": False,
1521        "options": False,
1522    }
1523
1524
1525class TruncateTable(Expression):
1526    arg_types = {
1527        "expressions": True,
1528        "is_database": False,
1529        "exists": False,
1530        "only": False,
1531        "cluster": False,
1532        "identity": False,
1533        "option": False,
1534        "partition": False,
1535    }
1536
1537
1538# https://docs.snowflake.com/en/sql-reference/sql/create-clone
1539# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_clone_statement
1540# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_copy
1541class Clone(Expression):
1542    arg_types = {"this": True, "shallow": False, "copy": False}
1543
1544
1545class Describe(Expression):
1546    arg_types = {
1547        "this": True,
1548        "style": False,
1549        "kind": False,
1550        "expressions": False,
1551        "partition": False,
1552        "format": False,
1553    }
1554
1555
1556# https://duckdb.org/docs/sql/statements/attach.html#attach
1557class Attach(Expression):
1558    arg_types = {"this": True, "exists": False, "expressions": False}
1559
1560
1561# https://duckdb.org/docs/sql/statements/attach.html#detach
1562class Detach(Expression):
1563    arg_types = {"this": True, "exists": False}
1564
1565
1566# https://duckdb.org/docs/guides/meta/summarize.html
1567class Summarize(Expression):
1568    arg_types = {"this": True, "table": False}
1569
1570
1571class Kill(Expression):
1572    arg_types = {"this": True, "kind": False}
1573
1574
1575class Pragma(Expression):
1576    pass
1577
1578
1579class Declare(Expression):
1580    arg_types = {"expressions": True}
1581
1582
1583class DeclareItem(Expression):
1584    arg_types = {"this": True, "kind": True, "default": False}
1585
1586
1587class Set(Expression):
1588    arg_types = {"expressions": False, "unset": False, "tag": False}
1589
1590
1591class Heredoc(Expression):
1592    arg_types = {"this": True, "tag": False}
1593
1594
1595class SetItem(Expression):
1596    arg_types = {
1597        "this": False,
1598        "expressions": False,
1599        "kind": False,
1600        "collate": False,  # MySQL SET NAMES statement
1601        "global": False,
1602    }
1603
1604
1605class Show(Expression):
1606    arg_types = {
1607        "this": True,
1608        "history": False,
1609        "terse": False,
1610        "target": False,
1611        "offset": False,
1612        "starts_with": False,
1613        "limit": False,
1614        "from": False,
1615        "like": False,
1616        "where": False,
1617        "db": False,
1618        "scope": False,
1619        "scope_kind": False,
1620        "full": False,
1621        "mutex": False,
1622        "query": False,
1623        "channel": False,
1624        "global": False,
1625        "log": False,
1626        "position": False,
1627        "types": False,
1628        "privileges": False,
1629    }
1630
1631
1632class UserDefinedFunction(Expression):
1633    arg_types = {"this": True, "expressions": False, "wrapped": False}
1634
1635
1636class CharacterSet(Expression):
1637    arg_types = {"this": True, "default": False}
1638
1639
1640class RecursiveWithSearch(Expression):
1641    arg_types = {"kind": True, "this": True, "expression": True, "using": False}
1642
1643
1644class With(Expression):
1645    arg_types = {"expressions": True, "recursive": False, "search": False}
1646
1647    @property
1648    def recursive(self) -> bool:
1649        return bool(self.args.get("recursive"))
1650
1651
1652class WithinGroup(Expression):
1653    arg_types = {"this": True, "expression": False}
1654
1655
1656# clickhouse supports scalar ctes
1657# https://clickhouse.com/docs/en/sql-reference/statements/select/with
1658class CTE(DerivedTable):
1659    arg_types = {
1660        "this": True,
1661        "alias": True,
1662        "scalar": False,
1663        "materialized": False,
1664    }
1665
1666
1667class ProjectionDef(Expression):
1668    arg_types = {"this": True, "expression": True}
1669
1670
1671class TableAlias(Expression):
1672    arg_types = {"this": False, "columns": False}
1673
1674    @property
1675    def columns(self):
1676        return self.args.get("columns") or []
1677
1678
1679class BitString(Condition):
1680    pass
1681
1682
1683class HexString(Condition):
1684    arg_types = {"this": True, "is_integer": False}
1685
1686
1687class ByteString(Condition):
1688    pass
1689
1690
1691class RawString(Condition):
1692    pass
1693
1694
1695class UnicodeString(Condition):
1696    arg_types = {"this": True, "escape": False}
1697
1698
1699class Column(Condition):
1700    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1701
1702    @property
1703    def table(self) -> str:
1704        return self.text("table")
1705
1706    @property
1707    def db(self) -> str:
1708        return self.text("db")
1709
1710    @property
1711    def catalog(self) -> str:
1712        return self.text("catalog")
1713
1714    @property
1715    def output_name(self) -> str:
1716        return self.name
1717
1718    @property
1719    def parts(self) -> t.List[Identifier]:
1720        """Return the parts of a column in order catalog, db, table, name."""
1721        return [
1722            t.cast(Identifier, self.args[part])
1723            for part in ("catalog", "db", "table", "this")
1724            if self.args.get(part)
1725        ]
1726
1727    def to_dot(self, include_dots: bool = True) -> Dot | Identifier:
1728        """Converts the column into a dot expression."""
1729        parts = self.parts
1730        parent = self.parent
1731
1732        if include_dots:
1733            while isinstance(parent, Dot):
1734                parts.append(parent.expression)
1735                parent = parent.parent
1736
1737        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]
1738
1739
1740class ColumnPosition(Expression):
1741    arg_types = {"this": False, "position": True}
1742
1743
1744class ColumnDef(Expression):
1745    arg_types = {
1746        "this": True,
1747        "kind": False,
1748        "constraints": False,
1749        "exists": False,
1750        "position": False,
1751        "default": False,
1752        "output": False,
1753    }
1754
1755    @property
1756    def constraints(self) -> t.List[ColumnConstraint]:
1757        return self.args.get("constraints") or []
1758
1759    @property
1760    def kind(self) -> t.Optional[DataType]:
1761        return self.args.get("kind")
1762
1763
1764class AlterColumn(Expression):
1765    arg_types = {
1766        "this": True,
1767        "dtype": False,
1768        "collate": False,
1769        "using": False,
1770        "default": False,
1771        "drop": False,
1772        "comment": False,
1773        "allow_null": False,
1774        "visible": False,
1775    }
1776
1777
1778# https://dev.mysql.com/doc/refman/8.0/en/invisible-indexes.html
1779class AlterIndex(Expression):
1780    arg_types = {"this": True, "visible": True}
1781
1782
1783# https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html
1784class AlterDistStyle(Expression):
1785    pass
1786
1787
1788class AlterSortKey(Expression):
1789    arg_types = {"this": False, "expressions": False, "compound": False}
1790
1791
1792class AlterSet(Expression):
1793    arg_types = {
1794        "expressions": False,
1795        "option": False,
1796        "tablespace": False,
1797        "access_method": False,
1798        "file_format": False,
1799        "copy_options": False,
1800        "tag": False,
1801        "location": False,
1802        "serde": False,
1803    }
1804
1805
1806class RenameColumn(Expression):
1807    arg_types = {"this": True, "to": True, "exists": False}
1808
1809
1810class AlterRename(Expression):
1811    pass
1812
1813
1814class SwapTable(Expression):
1815    pass
1816
1817
1818class Comment(Expression):
1819    arg_types = {
1820        "this": True,
1821        "kind": True,
1822        "expression": True,
1823        "exists": False,
1824        "materialized": False,
1825    }
1826
1827
1828class Comprehension(Expression):
1829    arg_types = {"this": True, "expression": True, "iterator": True, "condition": False}
1830
1831
1832# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
1833class MergeTreeTTLAction(Expression):
1834    arg_types = {
1835        "this": True,
1836        "delete": False,
1837        "recompress": False,
1838        "to_disk": False,
1839        "to_volume": False,
1840    }
1841
1842
1843# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
1844class MergeTreeTTL(Expression):
1845    arg_types = {
1846        "expressions": True,
1847        "where": False,
1848        "group": False,
1849        "aggregates": False,
1850    }
1851
1852
1853# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1854class IndexConstraintOption(Expression):
1855    arg_types = {
1856        "key_block_size": False,
1857        "using": False,
1858        "parser": False,
1859        "comment": False,
1860        "visible": False,
1861        "engine_attr": False,
1862        "secondary_engine_attr": False,
1863    }
1864
1865
1866class ColumnConstraint(Expression):
1867    arg_types = {"this": False, "kind": True}
1868
1869    @property
1870    def kind(self) -> ColumnConstraintKind:
1871        return self.args["kind"]
1872
1873
1874class ColumnConstraintKind(Expression):
1875    pass
1876
1877
1878class AutoIncrementColumnConstraint(ColumnConstraintKind):
1879    pass
1880
1881
1882class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1883    arg_types = {"this": True, "expression": True}
1884
1885
1886class CaseSpecificColumnConstraint(ColumnConstraintKind):
1887    arg_types = {"not_": True}
1888
1889
1890class CharacterSetColumnConstraint(ColumnConstraintKind):
1891    arg_types = {"this": True}
1892
1893
1894class CheckColumnConstraint(ColumnConstraintKind):
1895    arg_types = {"this": True, "enforced": False}
1896
1897
1898class ClusteredColumnConstraint(ColumnConstraintKind):
1899    pass
1900
1901
1902class CollateColumnConstraint(ColumnConstraintKind):
1903    pass
1904
1905
1906class CommentColumnConstraint(ColumnConstraintKind):
1907    pass
1908
1909
1910class CompressColumnConstraint(ColumnConstraintKind):
1911    arg_types = {"this": False}
1912
1913
1914class DateFormatColumnConstraint(ColumnConstraintKind):
1915    arg_types = {"this": True}
1916
1917
1918class DefaultColumnConstraint(ColumnConstraintKind):
1919    pass
1920
1921
1922class EncodeColumnConstraint(ColumnConstraintKind):
1923    pass
1924
1925
1926# https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-EXCLUDE
1927class ExcludeColumnConstraint(ColumnConstraintKind):
1928    pass
1929
1930
1931class EphemeralColumnConstraint(ColumnConstraintKind):
1932    arg_types = {"this": False}
1933
1934
1935class WithOperator(Expression):
1936    arg_types = {"this": True, "op": True}
1937
1938
1939class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1940    # this: True -> ALWAYS, this: False -> BY DEFAULT
1941    arg_types = {
1942        "this": False,
1943        "expression": False,
1944        "on_null": False,
1945        "start": False,
1946        "increment": False,
1947        "minvalue": False,
1948        "maxvalue": False,
1949        "cycle": False,
1950    }
1951
1952
1953class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1954    arg_types = {"start": False, "hidden": False}
1955
1956
1957# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1958# https://github.com/ClickHouse/ClickHouse/blob/master/src/Parsers/ParserCreateQuery.h#L646
1959class IndexColumnConstraint(ColumnConstraintKind):
1960    arg_types = {
1961        "this": False,
1962        "expressions": False,
1963        "kind": False,
1964        "index_type": False,
1965        "options": False,
1966        "expression": False,  # Clickhouse
1967        "granularity": False,
1968    }
1969
1970
1971class InlineLengthColumnConstraint(ColumnConstraintKind):
1972    pass
1973
1974
1975class NonClusteredColumnConstraint(ColumnConstraintKind):
1976    pass
1977
1978
1979class NotForReplicationColumnConstraint(ColumnConstraintKind):
1980    arg_types = {}
1981
1982
1983# https://docs.snowflake.com/en/sql-reference/sql/create-table
1984class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1985    arg_types = {"this": True, "expressions": False}
1986
1987
1988class NotNullColumnConstraint(ColumnConstraintKind):
1989    arg_types = {"allow_null": False}
1990
1991
1992# https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
1993class OnUpdateColumnConstraint(ColumnConstraintKind):
1994    pass
1995
1996
1997class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1998    arg_types = {"desc": False, "options": False}
1999
2000
2001class TitleColumnConstraint(ColumnConstraintKind):
2002    pass
2003
2004
2005class UniqueColumnConstraint(ColumnConstraintKind):
2006    arg_types = {
2007        "this": False,
2008        "index_type": False,
2009        "on_conflict": False,
2010        "nulls": False,
2011        "options": False,
2012    }
2013
2014
2015class UppercaseColumnConstraint(ColumnConstraintKind):
2016    arg_types: t.Dict[str, t.Any] = {}
2017
2018
2019# https://docs.risingwave.com/processing/watermarks#syntax
2020class WatermarkColumnConstraint(Expression):
2021    arg_types = {"this": True, "expression": True}
2022
2023
2024class PathColumnConstraint(ColumnConstraintKind):
2025    pass
2026
2027
2028# https://docs.snowflake.com/en/sql-reference/sql/create-table
2029class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
2030    pass
2031
2032
2033# computed column expression
2034# https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver16
2035class ComputedColumnConstraint(ColumnConstraintKind):
2036    arg_types = {"this": True, "persisted": False, "not_null": False}
2037
2038
2039class Constraint(Expression):
2040    arg_types = {"this": True, "expressions": True}
2041
2042
2043class Delete(DML):
2044    arg_types = {
2045        "with": False,
2046        "this": False,
2047        "using": False,
2048        "where": False,
2049        "returning": False,
2050        "limit": False,
2051        "tables": False,  # Multiple-Table Syntax (MySQL)
2052        "cluster": False,  # Clickhouse
2053    }
2054
2055    def delete(
2056        self,
2057        table: ExpOrStr,
2058        dialect: DialectType = None,
2059        copy: bool = True,
2060        **opts,
2061    ) -> Delete:
2062        """
2063        Create a DELETE expression or replace the table on an existing DELETE expression.
2064
2065        Example:
2066            >>> delete("tbl").sql()
2067            'DELETE FROM tbl'
2068
2069        Args:
2070            table: the table from which to delete.
2071            dialect: the dialect used to parse the input expression.
2072            copy: if `False`, modify this expression instance in-place.
2073            opts: other options to use to parse the input expressions.
2074
2075        Returns:
2076            Delete: the modified expression.
2077        """
2078        return _apply_builder(
2079            expression=table,
2080            instance=self,
2081            arg="this",
2082            dialect=dialect,
2083            into=Table,
2084            copy=copy,
2085            **opts,
2086        )
2087
2088    def where(
2089        self,
2090        *expressions: t.Optional[ExpOrStr],
2091        append: bool = True,
2092        dialect: DialectType = None,
2093        copy: bool = True,
2094        **opts,
2095    ) -> Delete:
2096        """
2097        Append to or set the WHERE expressions.
2098
2099        Example:
2100            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
2101            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
2102
2103        Args:
2104            *expressions: the SQL code strings to parse.
2105                If an `Expression` instance is passed, it will be used as-is.
2106                Multiple expressions are combined with an AND operator.
2107            append: if `True`, AND the new expressions to any existing expression.
2108                Otherwise, this resets the expression.
2109            dialect: the dialect used to parse the input expressions.
2110            copy: if `False`, modify this expression instance in-place.
2111            opts: other options to use to parse the input expressions.
2112
2113        Returns:
2114            Delete: the modified expression.
2115        """
2116        return _apply_conjunction_builder(
2117            *expressions,
2118            instance=self,
2119            arg="where",
2120            append=append,
2121            into=Where,
2122            dialect=dialect,
2123            copy=copy,
2124            **opts,
2125        )
2126
2127
2128class Drop(Expression):
2129    arg_types = {
2130        "this": False,
2131        "kind": False,
2132        "expressions": False,
2133        "exists": False,
2134        "temporary": False,
2135        "materialized": False,
2136        "cascade": False,
2137        "constraints": False,
2138        "purge": False,
2139        "cluster": False,
2140        "concurrently": False,
2141    }
2142
2143    @property
2144    def kind(self) -> t.Optional[str]:
2145        kind = self.args.get("kind")
2146        return kind and kind.upper()
2147
2148
2149# https://cloud.google.com/bigquery/docs/reference/standard-sql/export-statements
2150class Export(Expression):
2151    arg_types = {"this": True, "connection": False, "options": True}
2152
2153
2154class Filter(Expression):
2155    arg_types = {"this": True, "expression": True}
2156
2157
2158class Check(Expression):
2159    pass
2160
2161
2162class Changes(Expression):
2163    arg_types = {"information": True, "at_before": False, "end": False}
2164
2165
2166# https://docs.snowflake.com/en/sql-reference/constructs/connect-by
2167class Connect(Expression):
2168    arg_types = {"start": False, "connect": True, "nocycle": False}
2169
2170
2171class CopyParameter(Expression):
2172    arg_types = {"this": True, "expression": False, "expressions": False}
2173
2174
2175class Copy(DML):
2176    arg_types = {
2177        "this": True,
2178        "kind": True,
2179        "files": True,
2180        "credentials": False,
2181        "format": False,
2182        "params": False,
2183    }
2184
2185
2186class Credentials(Expression):
2187    arg_types = {
2188        "credentials": False,
2189        "encryption": False,
2190        "storage": False,
2191        "iam_role": False,
2192        "region": False,
2193    }
2194
2195
2196class Prior(Expression):
2197    pass
2198
2199
2200class Directory(Expression):
2201    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
2202    arg_types = {"this": True, "local": False, "row_format": False}
2203
2204
2205class ForeignKey(Expression):
2206    arg_types = {
2207        "expressions": False,
2208        "reference": False,
2209        "delete": False,
2210        "update": False,
2211        "options": False,
2212    }
2213
2214
2215class ColumnPrefix(Expression):
2216    arg_types = {"this": True, "expression": True}
2217
2218
2219class PrimaryKey(Expression):
2220    arg_types = {"expressions": True, "options": False}
2221
2222
2223# https://www.postgresql.org/docs/9.1/sql-selectinto.html
2224# https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_INTO.html#r_SELECT_INTO-examples
2225class Into(Expression):
2226    arg_types = {
2227        "this": False,
2228        "temporary": False,
2229        "unlogged": False,
2230        "bulk_collect": False,
2231        "expressions": False,
2232    }
2233
2234
2235class From(Expression):
2236    @property
2237    def name(self) -> str:
2238        return self.this.name
2239
2240    @property
2241    def alias_or_name(self) -> str:
2242        return self.this.alias_or_name
2243
2244
2245class Having(Expression):
2246    pass
2247
2248
2249class Hint(Expression):
2250    arg_types = {"expressions": True}
2251
2252
2253class JoinHint(Expression):
2254    arg_types = {"this": True, "expressions": True}
2255
2256
2257class Identifier(Expression):
2258    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
2259
2260    @property
2261    def quoted(self) -> bool:
2262        return bool(self.args.get("quoted"))
2263
2264    @property
2265    def hashable_args(self) -> t.Any:
2266        return (self.this, self.quoted)
2267
2268    @property
2269    def output_name(self) -> str:
2270        return self.name
2271
2272
2273# https://www.postgresql.org/docs/current/indexes-opclass.html
2274class Opclass(Expression):
2275    arg_types = {"this": True, "expression": True}
2276
2277
2278class Index(Expression):
2279    arg_types = {
2280        "this": False,
2281        "table": False,
2282        "unique": False,
2283        "primary": False,
2284        "amp": False,  # teradata
2285        "params": False,
2286    }
2287
2288
2289class IndexParameters(Expression):
2290    arg_types = {
2291        "using": False,
2292        "include": False,
2293        "columns": False,
2294        "with_storage": False,
2295        "partition_by": False,
2296        "tablespace": False,
2297        "where": False,
2298        "on": False,
2299    }
2300
2301
2302class Insert(DDL, DML):
2303    arg_types = {
2304        "hint": False,
2305        "with": False,
2306        "is_function": False,
2307        "this": False,
2308        "expression": False,
2309        "conflict": False,
2310        "returning": False,
2311        "overwrite": False,
2312        "exists": False,
2313        "alternative": False,
2314        "where": False,
2315        "ignore": False,
2316        "by_name": False,
2317        "stored": False,
2318        "partition": False,
2319        "settings": False,
2320        "source": False,
2321    }
2322
2323    def with_(
2324        self,
2325        alias: ExpOrStr,
2326        as_: ExpOrStr,
2327        recursive: t.Optional[bool] = None,
2328        materialized: t.Optional[bool] = None,
2329        append: bool = True,
2330        dialect: DialectType = None,
2331        copy: bool = True,
2332        **opts,
2333    ) -> Insert:
2334        """
2335        Append to or set the common table expressions.
2336
2337        Example:
2338            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2339            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2340
2341        Args:
2342            alias: the SQL code string to parse as the table name.
2343                If an `Expression` instance is passed, this is used as-is.
2344            as_: the SQL code string to parse as the table expression.
2345                If an `Expression` instance is passed, it will be used as-is.
2346            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2347            materialized: set the MATERIALIZED part of the expression.
2348            append: if `True`, add to any existing expressions.
2349                Otherwise, this resets the expressions.
2350            dialect: the dialect used to parse the input expression.
2351            copy: if `False`, modify this expression instance in-place.
2352            opts: other options to use to parse the input expressions.
2353
2354        Returns:
2355            The modified expression.
2356        """
2357        return _apply_cte_builder(
2358            self,
2359            alias,
2360            as_,
2361            recursive=recursive,
2362            materialized=materialized,
2363            append=append,
2364            dialect=dialect,
2365            copy=copy,
2366            **opts,
2367        )
2368
2369
2370class ConditionalInsert(Expression):
2371    arg_types = {"this": True, "expression": False, "else_": False}
2372
2373
2374class MultitableInserts(Expression):
2375    arg_types = {"expressions": True, "kind": True, "source": True}
2376
2377
2378class OnConflict(Expression):
2379    arg_types = {
2380        "duplicate": False,
2381        "expressions": False,
2382        "action": False,
2383        "conflict_keys": False,
2384        "constraint": False,
2385        "where": False,
2386    }
2387
2388
2389class OnCondition(Expression):
2390    arg_types = {"error": False, "empty": False, "null": False}
2391
2392
2393class Returning(Expression):
2394    arg_types = {"expressions": True, "into": False}
2395
2396
2397# https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
2398class Introducer(Expression):
2399    arg_types = {"this": True, "expression": True}
2400
2401
2402# national char, like n'utf8'
2403class National(Expression):
2404    pass
2405
2406
2407class LoadData(Expression):
2408    arg_types = {
2409        "this": True,
2410        "local": False,
2411        "overwrite": False,
2412        "inpath": True,
2413        "partition": False,
2414        "input_format": False,
2415        "serde": False,
2416    }
2417
2418
2419class Partition(Expression):
2420    arg_types = {"expressions": True, "subpartition": False}
2421
2422
2423class PartitionRange(Expression):
2424    arg_types = {"this": True, "expression": True}
2425
2426
2427# https://clickhouse.com/docs/en/sql-reference/statements/alter/partition#how-to-set-partition-expression
2428class PartitionId(Expression):
2429    pass
2430
2431
2432class Fetch(Expression):
2433    arg_types = {
2434        "direction": False,
2435        "count": False,
2436        "limit_options": False,
2437    }
2438
2439
2440class Grant(Expression):
2441    arg_types = {
2442        "privileges": True,
2443        "kind": False,
2444        "securable": True,
2445        "principals": True,
2446        "grant_option": False,
2447    }
2448
2449
2450class Group(Expression):
2451    arg_types = {
2452        "expressions": False,
2453        "grouping_sets": False,
2454        "cube": False,
2455        "rollup": False,
2456        "totals": False,
2457        "all": False,
2458    }
2459
2460
2461class Cube(Expression):
2462    arg_types = {"expressions": False}
2463
2464
2465class Rollup(Expression):
2466    arg_types = {"expressions": False}
2467
2468
2469class GroupingSets(Expression):
2470    arg_types = {"expressions": True}
2471
2472
2473class Lambda(Expression):
2474    arg_types = {"this": True, "expressions": True}
2475
2476
2477class Limit(Expression):
2478    arg_types = {
2479        "this": False,
2480        "expression": True,
2481        "offset": False,
2482        "limit_options": False,
2483        "expressions": False,
2484    }
2485
2486
2487class LimitOptions(Expression):
2488    arg_types = {
2489        "percent": False,
2490        "rows": False,
2491        "with_ties": False,
2492    }
2493
2494
2495class Literal(Condition):
2496    arg_types = {"this": True, "is_string": True}
2497
2498    @property
2499    def hashable_args(self) -> t.Any:
2500        return (self.this, self.args.get("is_string"))
2501
2502    @classmethod
2503    def number(cls, number) -> Literal:
2504        return cls(this=str(number), is_string=False)
2505
2506    @classmethod
2507    def string(cls, string) -> Literal:
2508        return cls(this=str(string), is_string=True)
2509
2510    @property
2511    def output_name(self) -> str:
2512        return self.name
2513
2514    def to_py(self) -> int | str | Decimal:
2515        if self.is_number:
2516            try:
2517                return int(self.this)
2518            except ValueError:
2519                return Decimal(self.this)
2520        return self.this
2521
2522
2523class Join(Expression):
2524    arg_types = {
2525        "this": True,
2526        "on": False,
2527        "side": False,
2528        "kind": False,
2529        "using": False,
2530        "method": False,
2531        "global": False,
2532        "hint": False,
2533        "match_condition": False,  # Snowflake
2534        "expressions": False,
2535        "pivots": False,
2536    }
2537
2538    @property
2539    def method(self) -> str:
2540        return self.text("method").upper()
2541
2542    @property
2543    def kind(self) -> str:
2544        return self.text("kind").upper()
2545
2546    @property
2547    def side(self) -> str:
2548        return self.text("side").upper()
2549
2550    @property
2551    def hint(self) -> str:
2552        return self.text("hint").upper()
2553
2554    @property
2555    def alias_or_name(self) -> str:
2556        return self.this.alias_or_name
2557
2558    @property
2559    def is_semi_or_anti_join(self) -> bool:
2560        return self.kind in ("SEMI", "ANTI")
2561
2562    def on(
2563        self,
2564        *expressions: t.Optional[ExpOrStr],
2565        append: bool = True,
2566        dialect: DialectType = None,
2567        copy: bool = True,
2568        **opts,
2569    ) -> Join:
2570        """
2571        Append to or set the ON expressions.
2572
2573        Example:
2574            >>> import sqlglot
2575            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2576            'JOIN x ON y = 1'
2577
2578        Args:
2579            *expressions: the SQL code strings to parse.
2580                If an `Expression` instance is passed, it will be used as-is.
2581                Multiple expressions are combined with an AND operator.
2582            append: if `True`, AND the new expressions to any existing expression.
2583                Otherwise, this resets the expression.
2584            dialect: the dialect used to parse the input expressions.
2585            copy: if `False`, modify this expression instance in-place.
2586            opts: other options to use to parse the input expressions.
2587
2588        Returns:
2589            The modified Join expression.
2590        """
2591        join = _apply_conjunction_builder(
2592            *expressions,
2593            instance=self,
2594            arg="on",
2595            append=append,
2596            dialect=dialect,
2597            copy=copy,
2598            **opts,
2599        )
2600
2601        if join.kind == "CROSS":
2602            join.set("kind", None)
2603
2604        return join
2605
2606    def using(
2607        self,
2608        *expressions: t.Optional[ExpOrStr],
2609        append: bool = True,
2610        dialect: DialectType = None,
2611        copy: bool = True,
2612        **opts,
2613    ) -> Join:
2614        """
2615        Append to or set the USING expressions.
2616
2617        Example:
2618            >>> import sqlglot
2619            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2620            'JOIN x USING (foo, bla)'
2621
2622        Args:
2623            *expressions: the SQL code strings to parse.
2624                If an `Expression` instance is passed, it will be used as-is.
2625            append: if `True`, concatenate the new expressions to the existing "using" list.
2626                Otherwise, this resets the expression.
2627            dialect: the dialect used to parse the input expressions.
2628            copy: if `False`, modify this expression instance in-place.
2629            opts: other options to use to parse the input expressions.
2630
2631        Returns:
2632            The modified Join expression.
2633        """
2634        join = _apply_list_builder(
2635            *expressions,
2636            instance=self,
2637            arg="using",
2638            append=append,
2639            dialect=dialect,
2640            copy=copy,
2641            **opts,
2642        )
2643
2644        if join.kind == "CROSS":
2645            join.set("kind", None)
2646
2647        return join
2648
2649
2650class Lateral(UDTF):
2651    arg_types = {
2652        "this": True,
2653        "view": False,
2654        "outer": False,
2655        "alias": False,
2656        "cross_apply": False,  # True -> CROSS APPLY, False -> OUTER APPLY
2657        "ordinality": False,
2658    }
2659
2660
2661# https://docs.snowflake.com/sql-reference/literals-table
2662# https://docs.snowflake.com/en/sql-reference/functions-table#using-a-table-function
2663class TableFromRows(UDTF):
2664    arg_types = {
2665        "this": True,
2666        "alias": False,
2667        "joins": False,
2668        "pivots": False,
2669        "sample": False,
2670    }
2671
2672
2673class MatchRecognizeMeasure(Expression):
2674    arg_types = {
2675        "this": True,
2676        "window_frame": False,
2677    }
2678
2679
2680class MatchRecognize(Expression):
2681    arg_types = {
2682        "partition_by": False,
2683        "order": False,
2684        "measures": False,
2685        "rows": False,
2686        "after": False,
2687        "pattern": False,
2688        "define": False,
2689        "alias": False,
2690    }
2691
2692
2693# Clickhouse FROM FINAL modifier
2694# https://clickhouse.com/docs/en/sql-reference/statements/select/from/#final-modifier
2695class Final(Expression):
2696    pass
2697
2698
2699class Offset(Expression):
2700    arg_types = {"this": False, "expression": True, "expressions": False}
2701
2702
2703class Order(Expression):
2704    arg_types = {"this": False, "expressions": True, "siblings": False}
2705
2706
2707# https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier
2708class WithFill(Expression):
2709    arg_types = {
2710        "from": False,
2711        "to": False,
2712        "step": False,
2713        "interpolate": False,
2714    }
2715
2716
2717# hive specific sorts
2718# https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy
2719class Cluster(Order):
2720    pass
2721
2722
2723class Distribute(Order):
2724    pass
2725
2726
2727class Sort(Order):
2728    pass
2729
2730
2731class Ordered(Expression):
2732    arg_types = {"this": True, "desc": False, "nulls_first": True, "with_fill": False}
2733
2734    @property
2735    def name(self) -> str:
2736        return self.this.name
2737
2738
2739class Property(Expression):
2740    arg_types = {"this": True, "value": True}
2741
2742
2743class GrantPrivilege(Expression):
2744    arg_types = {"this": True, "expressions": False}
2745
2746
2747class GrantPrincipal(Expression):
2748    arg_types = {"this": True, "kind": False}
2749
2750
2751class AllowedValuesProperty(Expression):
2752    arg_types = {"expressions": True}
2753
2754
2755class AlgorithmProperty(Property):
2756    arg_types = {"this": True}
2757
2758
2759class AutoIncrementProperty(Property):
2760    arg_types = {"this": True}
2761
2762
2763# https://docs.aws.amazon.com/prescriptive-guidance/latest/materialized-views-redshift/refreshing-materialized-views.html
2764class AutoRefreshProperty(Property):
2765    arg_types = {"this": True}
2766
2767
2768class BackupProperty(Property):
2769    arg_types = {"this": True}
2770
2771
2772class BlockCompressionProperty(Property):
2773    arg_types = {
2774        "autotemp": False,
2775        "always": False,
2776        "default": False,
2777        "manual": False,
2778        "never": False,
2779    }
2780
2781
2782class CharacterSetProperty(Property):
2783    arg_types = {"this": True, "default": True}
2784
2785
2786class ChecksumProperty(Property):
2787    arg_types = {"on": False, "default": False}
2788
2789
2790class CollateProperty(Property):
2791    arg_types = {"this": True, "default": False}
2792
2793
2794class CopyGrantsProperty(Property):
2795    arg_types = {}
2796
2797
2798class DataBlocksizeProperty(Property):
2799    arg_types = {
2800        "size": False,
2801        "units": False,
2802        "minimum": False,
2803        "maximum": False,
2804        "default": False,
2805    }
2806
2807
2808class DataDeletionProperty(Property):
2809    arg_types = {"on": True, "filter_col": False, "retention_period": False}
2810
2811
2812class DefinerProperty(Property):
2813    arg_types = {"this": True}
2814
2815
2816class DistKeyProperty(Property):
2817    arg_types = {"this": True}
2818
2819
2820# https://docs.starrocks.io/docs/sql-reference/sql-statements/data-definition/CREATE_TABLE/#distribution_desc
2821# https://doris.apache.org/docs/sql-manual/sql-statements/Data-Definition-Statements/Create/CREATE-TABLE?_highlight=create&_highlight=table#distribution_desc
2822class DistributedByProperty(Property):
2823    arg_types = {"expressions": False, "kind": True, "buckets": False, "order": False}
2824
2825
2826class DistStyleProperty(Property):
2827    arg_types = {"this": True}
2828
2829
2830class DuplicateKeyProperty(Property):
2831    arg_types = {"expressions": True}
2832
2833
2834class EngineProperty(Property):
2835    arg_types = {"this": True}
2836
2837
2838class HeapProperty(Property):
2839    arg_types = {}
2840
2841
2842class ToTableProperty(Property):
2843    arg_types = {"this": True}
2844
2845
2846class ExecuteAsProperty(Property):
2847    arg_types = {"this": True}
2848
2849
2850class ExternalProperty(Property):
2851    arg_types = {"this": False}
2852
2853
2854class FallbackProperty(Property):
2855    arg_types = {"no": True, "protection": False}
2856
2857
2858class FileFormatProperty(Property):
2859    arg_types = {"this": False, "expressions": False}
2860
2861
2862class CredentialsProperty(Property):
2863    arg_types = {"expressions": True}
2864
2865
2866class FreespaceProperty(Property):
2867    arg_types = {"this": True, "percent": False}
2868
2869
2870class GlobalProperty(Property):
2871    arg_types = {}
2872
2873
2874class IcebergProperty(Property):
2875    arg_types = {}
2876
2877
2878class InheritsProperty(Property):
2879    arg_types = {"expressions": True}
2880
2881
2882class InputModelProperty(Property):
2883    arg_types = {"this": True}
2884
2885
2886class OutputModelProperty(Property):
2887    arg_types = {"this": True}
2888
2889
2890class IsolatedLoadingProperty(Property):
2891    arg_types = {"no": False, "concurrent": False, "target": False}
2892
2893
2894class JournalProperty(Property):
2895    arg_types = {
2896        "no": False,
2897        "dual": False,
2898        "before": False,
2899        "local": False,
2900        "after": False,
2901    }
2902
2903
2904class LanguageProperty(Property):
2905    arg_types = {"this": True}
2906
2907
2908class EnviromentProperty(Property):
2909    arg_types = {"expressions": True}
2910
2911
2912# spark ddl
2913class ClusteredByProperty(Property):
2914    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
2915
2916
2917class DictProperty(Property):
2918    arg_types = {"this": True, "kind": True, "settings": False}
2919
2920
2921class DictSubProperty(Property):
2922    pass
2923
2924
2925class DictRange(Property):
2926    arg_types = {"this": True, "min": True, "max": True}
2927
2928
2929class DynamicProperty(Property):
2930    arg_types = {}
2931
2932
2933# Clickhouse CREATE ... ON CLUSTER modifier
2934# https://clickhouse.com/docs/en/sql-reference/distributed-ddl
2935class OnCluster(Property):
2936    arg_types = {"this": True}
2937
2938
2939# Clickhouse EMPTY table "property"
2940class EmptyProperty(Property):
2941    arg_types = {}
2942
2943
2944class LikeProperty(Property):
2945    arg_types = {"this": True, "expressions": False}
2946
2947
2948class LocationProperty(Property):
2949    arg_types = {"this": True}
2950
2951
2952class LockProperty(Property):
2953    arg_types = {"this": True}
2954
2955
2956class LockingProperty(Property):
2957    arg_types = {
2958        "this": False,
2959        "kind": True,
2960        "for_or_in": False,
2961        "lock_type": True,
2962        "override": False,
2963    }
2964
2965
2966class LogProperty(Property):
2967    arg_types = {"no": True}
2968
2969
2970class MaterializedProperty(Property):
2971    arg_types = {"this": False}
2972
2973
2974class MergeBlockRatioProperty(Property):
2975    arg_types = {"this": False, "no": False, "default": False, "percent": False}
2976
2977
2978class NoPrimaryIndexProperty(Property):
2979    arg_types = {}
2980
2981
2982class OnProperty(Property):
2983    arg_types = {"this": True}
2984
2985
2986class OnCommitProperty(Property):
2987    arg_types = {"delete": False}
2988
2989
2990class PartitionedByProperty(Property):
2991    arg_types = {"this": True}
2992
2993
2994class PartitionedByBucket(Property):
2995    arg_types = {"this": True, "expression": True}
2996
2997
2998class PartitionByTruncate(Property):
2999    arg_types = {"this": True, "expression": True}
3000
3001
3002# https://docs.starrocks.io/docs/sql-reference/sql-statements/table_bucket_part_index/CREATE_TABLE/
3003class PartitionByRangeProperty(Property):
3004    arg_types = {"partition_expressions": True, "create_expressions": True}
3005
3006
3007# https://docs.starrocks.io/docs/table_design/data_distribution/#range-partitioning
3008class PartitionByRangePropertyDynamic(Expression):
3009    arg_types = {"this": False, "start": True, "end": True, "every": True}
3010
3011
3012# https://docs.starrocks.io/docs/sql-reference/sql-statements/table_bucket_part_index/CREATE_TABLE/
3013class UniqueKeyProperty(Property):
3014    arg_types = {"expressions": True}
3015
3016
3017# https://www.postgresql.org/docs/current/sql-createtable.html
3018class PartitionBoundSpec(Expression):
3019    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
3020    arg_types = {
3021        "this": False,
3022        "expression": False,
3023        "from_expressions": False,
3024        "to_expressions": False,
3025    }
3026
3027
3028class PartitionedOfProperty(Property):
3029    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
3030    arg_types = {"this": True, "expression": True}
3031
3032
3033class StreamingTableProperty(Property):
3034    arg_types = {}
3035
3036
3037class RemoteWithConnectionModelProperty(Property):
3038    arg_types = {"this": True}
3039
3040
3041class ReturnsProperty(Property):
3042    arg_types = {"this": False, "is_table": False, "table": False, "null": False}
3043
3044
3045class StrictProperty(Property):
3046    arg_types = {}
3047
3048
3049class RowFormatProperty(Property):
3050    arg_types = {"this": True}
3051
3052
3053class RowFormatDelimitedProperty(Property):
3054    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
3055    arg_types = {
3056        "fields": False,
3057        "escaped": False,
3058        "collection_items": False,
3059        "map_keys": False,
3060        "lines": False,
3061        "null": False,
3062        "serde": False,
3063    }
3064
3065
3066class RowFormatSerdeProperty(Property):
3067    arg_types = {"this": True, "serde_properties": False}
3068
3069
3070# https://spark.apache.org/docs/3.1.2/sql-ref-syntax-qry-select-transform.html
3071class QueryTransform(Expression):
3072    arg_types = {
3073        "expressions": True,
3074        "command_script": True,
3075        "schema": False,
3076        "row_format_before": False,
3077        "record_writer": False,
3078        "row_format_after": False,
3079        "record_reader": False,
3080    }
3081
3082
3083class SampleProperty(Property):
3084    arg_types = {"this": True}
3085
3086
3087# https://prestodb.io/docs/current/sql/create-view.html#synopsis
3088class SecurityProperty(Property):
3089    arg_types = {"this": True}
3090
3091
3092class SchemaCommentProperty(Property):
3093    arg_types = {"this": True}
3094
3095
3096class SerdeProperties(Property):
3097    arg_types = {"expressions": True, "with": False}
3098
3099
3100class SetProperty(Property):
3101    arg_types = {"multi": True}
3102
3103
3104class SharingProperty(Property):
3105    arg_types = {"this": False}
3106
3107
3108class SetConfigProperty(Property):
3109    arg_types = {"this": True}
3110
3111
3112class SettingsProperty(Property):
3113    arg_types = {"expressions": True}
3114
3115
3116class SortKeyProperty(Property):
3117    arg_types = {"this": True, "compound": False}
3118
3119
3120class SqlReadWriteProperty(Property):
3121    arg_types = {"this": True}
3122
3123
3124class SqlSecurityProperty(Property):
3125    arg_types = {"definer": True}
3126
3127
3128class StabilityProperty(Property):
3129    arg_types = {"this": True}
3130
3131
3132class StorageHandlerProperty(Property):
3133    arg_types = {"this": True}
3134
3135
3136class TemporaryProperty(Property):
3137    arg_types = {"this": False}
3138
3139
3140class SecureProperty(Property):
3141    arg_types = {}
3142
3143
3144# https://docs.snowflake.com/en/sql-reference/sql/create-table
3145class Tags(ColumnConstraintKind, Property):
3146    arg_types = {"expressions": True}
3147
3148
3149class TransformModelProperty(Property):
3150    arg_types = {"expressions": True}
3151
3152
3153class TransientProperty(Property):
3154    arg_types = {"this": False}
3155
3156
3157class UnloggedProperty(Property):
3158    arg_types = {}
3159
3160
3161# https://docs.snowflake.com/en/sql-reference/sql/create-table#create-table-using-template
3162class UsingTemplateProperty(Property):
3163    arg_types = {"this": True}
3164
3165
3166# https://learn.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql?view=sql-server-ver16
3167class ViewAttributeProperty(Property):
3168    arg_types = {"this": True}
3169
3170
3171class VolatileProperty(Property):
3172    arg_types = {"this": False}
3173
3174
3175class WithDataProperty(Property):
3176    arg_types = {"no": True, "statistics": False}
3177
3178
3179class WithJournalTableProperty(Property):
3180    arg_types = {"this": True}
3181
3182
3183class WithSchemaBindingProperty(Property):
3184    arg_types = {"this": True}
3185
3186
3187class WithSystemVersioningProperty(Property):
3188    arg_types = {
3189        "on": False,
3190        "this": False,
3191        "data_consistency": False,
3192        "retention_period": False,
3193        "with": True,
3194    }
3195
3196
3197class WithProcedureOptions(Property):
3198    arg_types = {"expressions": True}
3199
3200
3201class EncodeProperty(Property):
3202    arg_types = {"this": True, "properties": False, "key": False}
3203
3204
3205class IncludeProperty(Property):
3206    arg_types = {"this": True, "alias": False, "column_def": False}
3207
3208
3209class ForceProperty(Property):
3210    arg_types = {}
3211
3212
3213class Properties(Expression):
3214    arg_types = {"expressions": True}
3215
3216    NAME_TO_PROPERTY = {
3217        "ALGORITHM": AlgorithmProperty,
3218        "AUTO_INCREMENT": AutoIncrementProperty,
3219        "CHARACTER SET": CharacterSetProperty,
3220        "CLUSTERED_BY": ClusteredByProperty,
3221        "COLLATE": CollateProperty,
3222        "COMMENT": SchemaCommentProperty,
3223        "CREDENTIALS": CredentialsProperty,
3224        "DEFINER": DefinerProperty,
3225        "DISTKEY": DistKeyProperty,
3226        "DISTRIBUTED_BY": DistributedByProperty,
3227        "DISTSTYLE": DistStyleProperty,
3228        "ENGINE": EngineProperty,
3229        "EXECUTE AS": ExecuteAsProperty,
3230        "FORMAT": FileFormatProperty,
3231        "LANGUAGE": LanguageProperty,
3232        "LOCATION": LocationProperty,
3233        "LOCK": LockProperty,
3234        "PARTITIONED_BY": PartitionedByProperty,
3235        "RETURNS": ReturnsProperty,
3236        "ROW_FORMAT": RowFormatProperty,
3237        "SORTKEY": SortKeyProperty,
3238        "ENCODE": EncodeProperty,
3239        "INCLUDE": IncludeProperty,
3240    }
3241
3242    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
3243
3244    # CREATE property locations
3245    # Form: schema specified
3246    #   create [POST_CREATE]
3247    #     table a [POST_NAME]
3248    #     (b int) [POST_SCHEMA]
3249    #     with ([POST_WITH])
3250    #     index (b) [POST_INDEX]
3251    #
3252    # Form: alias selection
3253    #   create [POST_CREATE]
3254    #     table a [POST_NAME]
3255    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
3256    #     index (c) [POST_INDEX]
3257    class Location(AutoName):
3258        POST_CREATE = auto()
3259        POST_NAME = auto()
3260        POST_SCHEMA = auto()
3261        POST_WITH = auto()
3262        POST_ALIAS = auto()
3263        POST_EXPRESSION = auto()
3264        POST_INDEX = auto()
3265        UNSUPPORTED = auto()
3266
3267    @classmethod
3268    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3269        expressions = []
3270        for key, value in properties_dict.items():
3271            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3272            if property_cls:
3273                expressions.append(property_cls(this=convert(value)))
3274            else:
3275                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3276
3277        return cls(expressions=expressions)
3278
3279
3280class Qualify(Expression):
3281    pass
3282
3283
3284class InputOutputFormat(Expression):
3285    arg_types = {"input_format": False, "output_format": False}
3286
3287
3288# https://www.ibm.com/docs/en/ias?topic=procedures-return-statement-in-sql
3289class Return(Expression):
3290    pass
3291
3292
3293class Reference(Expression):
3294    arg_types = {"this": True, "expressions": False, "options": False}
3295
3296
3297class Tuple(Expression):
3298    arg_types = {"expressions": False}
3299
3300    def isin(
3301        self,
3302        *expressions: t.Any,
3303        query: t.Optional[ExpOrStr] = None,
3304        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3305        copy: bool = True,
3306        **opts,
3307    ) -> In:
3308        return In(
3309            this=maybe_copy(self, copy),
3310            expressions=[convert(e, copy=copy) for e in expressions],
3311            query=maybe_parse(query, copy=copy, **opts) if query else None,
3312            unnest=(
3313                Unnest(
3314                    expressions=[
3315                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3316                        for e in ensure_list(unnest)
3317                    ]
3318                )
3319                if unnest
3320                else None
3321            ),
3322        )
3323
3324
3325QUERY_MODIFIERS = {
3326    "match": False,
3327    "laterals": False,
3328    "joins": False,
3329    "connect": False,
3330    "pivots": False,
3331    "prewhere": False,
3332    "where": False,
3333    "group": False,
3334    "having": False,
3335    "qualify": False,
3336    "windows": False,
3337    "distribute": False,
3338    "sort": False,
3339    "cluster": False,
3340    "order": False,
3341    "limit": False,
3342    "offset": False,
3343    "locks": False,
3344    "sample": False,
3345    "settings": False,
3346    "format": False,
3347    "options": False,
3348}
3349
3350
3351# https://learn.microsoft.com/en-us/sql/t-sql/queries/option-clause-transact-sql?view=sql-server-ver16
3352# https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query?view=sql-server-ver16
3353class QueryOption(Expression):
3354    arg_types = {"this": True, "expression": False}
3355
3356
3357# https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-ver16
3358class WithTableHint(Expression):
3359    arg_types = {"expressions": True}
3360
3361
3362# https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
3363class IndexTableHint(Expression):
3364    arg_types = {"this": True, "expressions": False, "target": False}
3365
3366
3367# https://docs.snowflake.com/en/sql-reference/constructs/at-before
3368class HistoricalData(Expression):
3369    arg_types = {"this": True, "kind": True, "expression": True}
3370
3371
3372# https://docs.snowflake.com/en/sql-reference/sql/put
3373class Put(Expression):
3374    arg_types = {"this": True, "target": True, "properties": False}
3375
3376
3377# https://docs.snowflake.com/en/sql-reference/sql/get
3378class Get(Expression):
3379    arg_types = {"this": True, "target": True, "properties": False}
3380
3381
3382class Table(Expression):
3383    arg_types = {
3384        "this": False,
3385        "alias": False,
3386        "db": False,
3387        "catalog": False,
3388        "laterals": False,
3389        "joins": False,
3390        "pivots": False,
3391        "hints": False,
3392        "system_time": False,
3393        "version": False,
3394        "format": False,
3395        "pattern": False,
3396        "ordinality": False,
3397        "when": False,
3398        "only": False,
3399        "partition": False,
3400        "changes": False,
3401        "rows_from": False,
3402        "sample": False,
3403    }
3404
3405    @property
3406    def name(self) -> str:
3407        if not self.this or isinstance(self.this, Func):
3408            return ""
3409        return self.this.name
3410
3411    @property
3412    def db(self) -> str:
3413        return self.text("db")
3414
3415    @property
3416    def catalog(self) -> str:
3417        return self.text("catalog")
3418
3419    @property
3420    def selects(self) -> t.List[Expression]:
3421        return []
3422
3423    @property
3424    def named_selects(self) -> t.List[str]:
3425        return []
3426
3427    @property
3428    def parts(self) -> t.List[Expression]:
3429        """Return the parts of a table in order catalog, db, table."""
3430        parts: t.List[Expression] = []
3431
3432        for arg in ("catalog", "db", "this"):
3433            part = self.args.get(arg)
3434
3435            if isinstance(part, Dot):
3436                parts.extend(part.flatten())
3437            elif isinstance(part, Expression):
3438                parts.append(part)
3439
3440        return parts
3441
3442    def to_column(self, copy: bool = True) -> Expression:
3443        parts = self.parts
3444        last_part = parts[-1]
3445
3446        if isinstance(last_part, Identifier):
3447            col: Expression = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3448        else:
3449            # This branch will be reached if a function or array is wrapped in a `Table`
3450            col = last_part
3451
3452        alias = self.args.get("alias")
3453        if alias:
3454            col = alias_(col, alias.this, copy=copy)
3455
3456        return col
3457
3458
3459class SetOperation(Query):
3460    arg_types = {
3461        "with": False,
3462        "this": True,
3463        "expression": True,
3464        "distinct": False,
3465        "by_name": False,
3466        "side": False,
3467        "kind": False,
3468        "on": False,
3469        **QUERY_MODIFIERS,
3470    }
3471
3472    def select(
3473        self: S,
3474        *expressions: t.Optional[ExpOrStr],
3475        append: bool = True,
3476        dialect: DialectType = None,
3477        copy: bool = True,
3478        **opts,
3479    ) -> S:
3480        this = maybe_copy(self, copy)
3481        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3482        this.expression.unnest().select(
3483            *expressions, append=append, dialect=dialect, copy=False, **opts
3484        )
3485        return this
3486
3487    @property
3488    def named_selects(self) -> t.List[str]:
3489        return self.this.unnest().named_selects
3490
3491    @property
3492    def is_star(self) -> bool:
3493        return self.this.is_star or self.expression.is_star
3494
3495    @property
3496    def selects(self) -> t.List[Expression]:
3497        return self.this.unnest().selects
3498
3499    @property
3500    def left(self) -> Query:
3501        return self.this
3502
3503    @property
3504    def right(self) -> Query:
3505        return self.expression
3506
3507    @property
3508    def kind(self) -> str:
3509        return self.text("kind").upper()
3510
3511    @property
3512    def side(self) -> str:
3513        return self.text("side").upper()
3514
3515
3516class Union(SetOperation):
3517    pass
3518
3519
3520class Except(SetOperation):
3521    pass
3522
3523
3524class Intersect(SetOperation):
3525    pass
3526
3527
3528class Update(DML):
3529    arg_types = {
3530        "with": False,
3531        "this": False,
3532        "expressions": True,
3533        "from": False,
3534        "where": False,
3535        "returning": False,
3536        "order": False,
3537        "limit": False,
3538    }
3539
3540    def table(
3541        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3542    ) -> Update:
3543        """
3544        Set the table to update.
3545
3546        Example:
3547            >>> Update().table("my_table").set_("x = 1").sql()
3548            'UPDATE my_table SET x = 1'
3549
3550        Args:
3551            expression : the SQL code strings to parse.
3552                If a `Table` instance is passed, this is used as-is.
3553                If another `Expression` instance is passed, it will be wrapped in a `Table`.
3554            dialect: the dialect used to parse the input expression.
3555            copy: if `False`, modify this expression instance in-place.
3556            opts: other options to use to parse the input expressions.
3557
3558        Returns:
3559            The modified Update expression.
3560        """
3561        return _apply_builder(
3562            expression=expression,
3563            instance=self,
3564            arg="this",
3565            into=Table,
3566            prefix=None,
3567            dialect=dialect,
3568            copy=copy,
3569            **opts,
3570        )
3571
3572    def set_(
3573        self,
3574        *expressions: ExpOrStr,
3575        append: bool = True,
3576        dialect: DialectType = None,
3577        copy: bool = True,
3578        **opts,
3579    ) -> Update:
3580        """
3581        Append to or set the SET expressions.
3582
3583        Example:
3584            >>> Update().table("my_table").set_("x = 1").sql()
3585            'UPDATE my_table SET x = 1'
3586
3587        Args:
3588            *expressions: the SQL code strings to parse.
3589                If `Expression` instance(s) are passed, they will be used as-is.
3590                Multiple expressions are combined with a comma.
3591            append: if `True`, add the new expressions to any existing SET expressions.
3592                Otherwise, this resets the expressions.
3593            dialect: the dialect used to parse the input expressions.
3594            copy: if `False`, modify this expression instance in-place.
3595            opts: other options to use to parse the input expressions.
3596        """
3597        return _apply_list_builder(
3598            *expressions,
3599            instance=self,
3600            arg="expressions",
3601            append=append,
3602            into=Expression,
3603            prefix=None,
3604            dialect=dialect,
3605            copy=copy,
3606            **opts,
3607        )
3608
3609    def where(
3610        self,
3611        *expressions: t.Optional[ExpOrStr],
3612        append: bool = True,
3613        dialect: DialectType = None,
3614        copy: bool = True,
3615        **opts,
3616    ) -> Select:
3617        """
3618        Append to or set the WHERE expressions.
3619
3620        Example:
3621            >>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
3622            "UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
3623
3624        Args:
3625            *expressions: the SQL code strings to parse.
3626                If an `Expression` instance is passed, it will be used as-is.
3627                Multiple expressions are combined with an AND operator.
3628            append: if `True`, AND the new expressions to any existing expression.
3629                Otherwise, this resets the expression.
3630            dialect: the dialect used to parse the input expressions.
3631            copy: if `False`, modify this expression instance in-place.
3632            opts: other options to use to parse the input expressions.
3633
3634        Returns:
3635            Select: the modified expression.
3636        """
3637        return _apply_conjunction_builder(
3638            *expressions,
3639            instance=self,
3640            arg="where",
3641            append=append,
3642            into=Where,
3643            dialect=dialect,
3644            copy=copy,
3645            **opts,
3646        )
3647
3648    def from_(
3649        self,
3650        expression: t.Optional[ExpOrStr] = None,
3651        dialect: DialectType = None,
3652        copy: bool = True,
3653        **opts,
3654    ) -> Update:
3655        """
3656        Set the FROM expression.
3657
3658        Example:
3659            >>> Update().table("my_table").set_("x = 1").from_("baz").sql()
3660            'UPDATE my_table SET x = 1 FROM baz'
3661
3662        Args:
3663            expression : the SQL code strings to parse.
3664                If a `From` instance is passed, this is used as-is.
3665                If another `Expression` instance is passed, it will be wrapped in a `From`.
3666                If nothing is passed in then a from is not applied to the expression
3667            dialect: the dialect used to parse the input expression.
3668            copy: if `False`, modify this expression instance in-place.
3669            opts: other options to use to parse the input expressions.
3670
3671        Returns:
3672            The modified Update expression.
3673        """
3674        if not expression:
3675            return maybe_copy(self, copy)
3676
3677        return _apply_builder(
3678            expression=expression,
3679            instance=self,
3680            arg="from",
3681            into=From,
3682            prefix="FROM",
3683            dialect=dialect,
3684            copy=copy,
3685            **opts,
3686        )
3687
3688    def with_(
3689        self,
3690        alias: ExpOrStr,
3691        as_: ExpOrStr,
3692        recursive: t.Optional[bool] = None,
3693        materialized: t.Optional[bool] = None,
3694        append: bool = True,
3695        dialect: DialectType = None,
3696        copy: bool = True,
3697        **opts,
3698    ) -> Update:
3699        """
3700        Append to or set the common table expressions.
3701
3702        Example:
3703            >>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
3704            'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
3705
3706        Args:
3707            alias: the SQL code string to parse as the table name.
3708                If an `Expression` instance is passed, this is used as-is.
3709            as_: the SQL code string to parse as the table expression.
3710                If an `Expression` instance is passed, it will be used as-is.
3711            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
3712            materialized: set the MATERIALIZED part of the expression.
3713            append: if `True`, add to any existing expressions.
3714                Otherwise, this resets the expressions.
3715            dialect: the dialect used to parse the input expression.
3716            copy: if `False`, modify this expression instance in-place.
3717            opts: other options to use to parse the input expressions.
3718
3719        Returns:
3720            The modified expression.
3721        """
3722        return _apply_cte_builder(
3723            self,
3724            alias,
3725            as_,
3726            recursive=recursive,
3727            materialized=materialized,
3728            append=append,
3729            dialect=dialect,
3730            copy=copy,
3731            **opts,
3732        )
3733
3734
3735class Values(UDTF):
3736    arg_types = {"expressions": True, "alias": False}
3737
3738
3739class Var(Expression):
3740    pass
3741
3742
3743class Version(Expression):
3744    """
3745    Time travel, iceberg, bigquery etc
3746    https://trino.io/docs/current/connector/iceberg.html?highlight=snapshot#using-snapshots
3747    https://www.databricks.com/blog/2019/02/04/introducing-delta-time-travel-for-large-scale-data-lakes.html
3748    https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#for_system_time_as_of
3749    https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-ver16
3750    this is either TIMESTAMP or VERSION
3751    kind is ("AS OF", "BETWEEN")
3752    """
3753
3754    arg_types = {"this": True, "kind": True, "expression": False}
3755
3756
3757class Schema(Expression):
3758    arg_types = {"this": False, "expressions": False}
3759
3760
3761# https://dev.mysql.com/doc/refman/8.0/en/select.html
3762# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html
3763class Lock(Expression):
3764    arg_types = {"update": True, "expressions": False, "wait": False}
3765
3766
3767class Select(Query):
3768    arg_types = {
3769        "with": False,
3770        "kind": False,
3771        "expressions": False,
3772        "hint": False,
3773        "distinct": False,
3774        "into": False,
3775        "from": False,
3776        "operation_modifiers": False,
3777        **QUERY_MODIFIERS,
3778    }
3779
3780    def from_(
3781        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3782    ) -> Select:
3783        """
3784        Set the FROM expression.
3785
3786        Example:
3787            >>> Select().from_("tbl").select("x").sql()
3788            'SELECT x FROM tbl'
3789
3790        Args:
3791            expression : the SQL code strings to parse.
3792                If a `From` instance is passed, this is used as-is.
3793                If another `Expression` instance is passed, it will be wrapped in a `From`.
3794            dialect: the dialect used to parse the input expression.
3795            copy: if `False`, modify this expression instance in-place.
3796            opts: other options to use to parse the input expressions.
3797
3798        Returns:
3799            The modified Select expression.
3800        """
3801        return _apply_builder(
3802            expression=expression,
3803            instance=self,
3804            arg="from",
3805            into=From,
3806            prefix="FROM",
3807            dialect=dialect,
3808            copy=copy,
3809            **opts,
3810        )
3811
3812    def group_by(
3813        self,
3814        *expressions: t.Optional[ExpOrStr],
3815        append: bool = True,
3816        dialect: DialectType = None,
3817        copy: bool = True,
3818        **opts,
3819    ) -> Select:
3820        """
3821        Set the GROUP BY expression.
3822
3823        Example:
3824            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
3825            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
3826
3827        Args:
3828            *expressions: the SQL code strings to parse.
3829                If a `Group` instance is passed, this is used as-is.
3830                If another `Expression` instance is passed, it will be wrapped in a `Group`.
3831                If nothing is passed in then a group by is not applied to the expression
3832            append: if `True`, add to any existing expressions.
3833                Otherwise, this flattens all the `Group` expression into a single expression.
3834            dialect: the dialect used to parse the input expression.
3835            copy: if `False`, modify this expression instance in-place.
3836            opts: other options to use to parse the input expressions.
3837
3838        Returns:
3839            The modified Select expression.
3840        """
3841        if not expressions:
3842            return self if not copy else self.copy()
3843
3844        return _apply_child_list_builder(
3845            *expressions,
3846            instance=self,
3847            arg="group",
3848            append=append,
3849            copy=copy,
3850            prefix="GROUP BY",
3851            into=Group,
3852            dialect=dialect,
3853            **opts,
3854        )
3855
3856    def sort_by(
3857        self,
3858        *expressions: t.Optional[ExpOrStr],
3859        append: bool = True,
3860        dialect: DialectType = None,
3861        copy: bool = True,
3862        **opts,
3863    ) -> Select:
3864        """
3865        Set the SORT BY expression.
3866
3867        Example:
3868            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
3869            'SELECT x FROM tbl SORT BY x DESC'
3870
3871        Args:
3872            *expressions: the SQL code strings to parse.
3873                If a `Group` instance is passed, this is used as-is.
3874                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
3875            append: if `True`, add to any existing expressions.
3876                Otherwise, this flattens all the `Order` expression into a single expression.
3877            dialect: the dialect used to parse the input expression.
3878            copy: if `False`, modify this expression instance in-place.
3879            opts: other options to use to parse the input expressions.
3880
3881        Returns:
3882            The modified Select expression.
3883        """
3884        return _apply_child_list_builder(
3885            *expressions,
3886            instance=self,
3887            arg="sort",
3888            append=append,
3889            copy=copy,
3890            prefix="SORT BY",
3891            into=Sort,
3892            dialect=dialect,
3893            **opts,
3894        )
3895
3896    def cluster_by(
3897        self,
3898        *expressions: t.Optional[ExpOrStr],
3899        append: bool = True,
3900        dialect: DialectType = None,
3901        copy: bool = True,
3902        **opts,
3903    ) -> Select:
3904        """
3905        Set the CLUSTER BY expression.
3906
3907        Example:
3908            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
3909            'SELECT x FROM tbl CLUSTER BY x DESC'
3910
3911        Args:
3912            *expressions: the SQL code strings to parse.
3913                If a `Group` instance is passed, this is used as-is.
3914                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
3915            append: if `True`, add to any existing expressions.
3916                Otherwise, this flattens all the `Order` expression into a single expression.
3917            dialect: the dialect used to parse the input expression.
3918            copy: if `False`, modify this expression instance in-place.
3919            opts: other options to use to parse the input expressions.
3920
3921        Returns:
3922            The modified Select expression.
3923        """
3924        return _apply_child_list_builder(
3925            *expressions,
3926            instance=self,
3927            arg="cluster",
3928            append=append,
3929            copy=copy,
3930            prefix="CLUSTER BY",
3931            into=Cluster,
3932            dialect=dialect,
3933            **opts,
3934        )
3935
3936    def select(
3937        self,
3938        *expressions: t.Optional[ExpOrStr],
3939        append: bool = True,
3940        dialect: DialectType = None,
3941        copy: bool = True,
3942        **opts,
3943    ) -> Select:
3944        return _apply_list_builder(
3945            *expressions,
3946            instance=self,
3947            arg="expressions",
3948            append=append,
3949            dialect=dialect,
3950            into=Expression,
3951            copy=copy,
3952            **opts,
3953        )
3954
3955    def lateral(
3956        self,
3957        *expressions: t.Optional[ExpOrStr],
3958        append: bool = True,
3959        dialect: DialectType = None,
3960        copy: bool = True,
3961        **opts,
3962    ) -> Select:
3963        """
3964        Append to or set the LATERAL expressions.
3965
3966        Example:
3967            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3968            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3969
3970        Args:
3971            *expressions: the SQL code strings to parse.
3972                If an `Expression` instance is passed, it will be used as-is.
3973            append: if `True`, add to any existing expressions.
3974                Otherwise, this resets the expressions.
3975            dialect: the dialect used to parse the input expressions.
3976            copy: if `False`, modify this expression instance in-place.
3977            opts: other options to use to parse the input expressions.
3978
3979        Returns:
3980            The modified Select expression.
3981        """
3982        return _apply_list_builder(
3983            *expressions,
3984            instance=self,
3985            arg="laterals",
3986            append=append,
3987            into=Lateral,
3988            prefix="LATERAL VIEW",
3989            dialect=dialect,
3990            copy=copy,
3991            **opts,
3992        )
3993
3994    def join(
3995        self,
3996        expression: ExpOrStr,
3997        on: t.Optional[ExpOrStr] = None,
3998        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3999        append: bool = True,
4000        join_type: t.Optional[str] = None,
4001        join_alias: t.Optional[Identifier | str] = None,
4002        dialect: DialectType = None,
4003        copy: bool = True,
4004        **opts,
4005    ) -> Select:
4006        """
4007        Append to or set the JOIN expressions.
4008
4009        Example:
4010            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
4011            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
4012
4013            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
4014            'SELECT 1 FROM a JOIN b USING (x, y, z)'
4015
4016            Use `join_type` to change the type of join:
4017
4018            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
4019            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
4020
4021        Args:
4022            expression: the SQL code string to parse.
4023                If an `Expression` instance is passed, it will be used as-is.
4024            on: optionally specify the join "on" criteria as a SQL string.
4025                If an `Expression` instance is passed, it will be used as-is.
4026            using: optionally specify the join "using" criteria as a SQL string.
4027                If an `Expression` instance is passed, it will be used as-is.
4028            append: if `True`, add to any existing expressions.
4029                Otherwise, this resets the expressions.
4030            join_type: if set, alter the parsed join type.
4031            join_alias: an optional alias for the joined source.
4032            dialect: the dialect used to parse the input expressions.
4033            copy: if `False`, modify this expression instance in-place.
4034            opts: other options to use to parse the input expressions.
4035
4036        Returns:
4037            Select: the modified expression.
4038        """
4039        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
4040
4041        try:
4042            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
4043        except ParseError:
4044            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
4045
4046        join = expression if isinstance(expression, Join) else Join(this=expression)
4047
4048        if isinstance(join.this, Select):
4049            join.this.replace(join.this.subquery())
4050
4051        if join_type:
4052            method: t.Optional[Token]
4053            side: t.Optional[Token]
4054            kind: t.Optional[Token]
4055
4056            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
4057
4058            if method:
4059                join.set("method", method.text)
4060            if side:
4061                join.set("side", side.text)
4062            if kind:
4063                join.set("kind", kind.text)
4064
4065        if on:
4066            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
4067            join.set("on", on)
4068
4069        if using:
4070            join = _apply_list_builder(
4071                *ensure_list(using),
4072                instance=join,
4073                arg="using",
4074                append=append,
4075                copy=copy,
4076                into=Identifier,
4077                **opts,
4078            )
4079
4080        if join_alias:
4081            join.set("this", alias_(join.this, join_alias, table=True))
4082
4083        return _apply_list_builder(
4084            join,
4085            instance=self,
4086            arg="joins",
4087            append=append,
4088            copy=copy,
4089            **opts,
4090        )
4091
4092    def having(
4093        self,
4094        *expressions: t.Optional[ExpOrStr],
4095        append: bool = True,
4096        dialect: DialectType = None,
4097        copy: bool = True,
4098        **opts,
4099    ) -> Select:
4100        """
4101        Append to or set the HAVING expressions.
4102
4103        Example:
4104            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
4105            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
4106
4107        Args:
4108            *expressions: the SQL code strings to parse.
4109                If an `Expression` instance is passed, it will be used as-is.
4110                Multiple expressions are combined with an AND operator.
4111            append: if `True`, AND the new expressions to any existing expression.
4112                Otherwise, this resets the expression.
4113            dialect: the dialect used to parse the input expressions.
4114            copy: if `False`, modify this expression instance in-place.
4115            opts: other options to use to parse the input expressions.
4116
4117        Returns:
4118            The modified Select expression.
4119        """
4120        return _apply_conjunction_builder(
4121            *expressions,
4122            instance=self,
4123            arg="having",
4124            append=append,
4125            into=Having,
4126            dialect=dialect,
4127            copy=copy,
4128            **opts,
4129        )
4130
4131    def window(
4132        self,
4133        *expressions: t.Optional[ExpOrStr],
4134        append: bool = True,
4135        dialect: DialectType = None,
4136        copy: bool = True,
4137        **opts,
4138    ) -> Select:
4139        return _apply_list_builder(
4140            *expressions,
4141            instance=self,
4142            arg="windows",
4143            append=append,
4144            into=Window,
4145            dialect=dialect,
4146            copy=copy,
4147            **opts,
4148        )
4149
4150    def qualify(
4151        self,
4152        *expressions: t.Optional[ExpOrStr],
4153        append: bool = True,
4154        dialect: DialectType = None,
4155        copy: bool = True,
4156        **opts,
4157    ) -> Select:
4158        return _apply_conjunction_builder(
4159            *expressions,
4160            instance=self,
4161            arg="qualify",
4162            append=append,
4163            into=Qualify,
4164            dialect=dialect,
4165            copy=copy,
4166            **opts,
4167        )
4168
4169    def distinct(
4170        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
4171    ) -> Select:
4172        """
4173        Set the OFFSET expression.
4174
4175        Example:
4176            >>> Select().from_("tbl").select("x").distinct().sql()
4177            'SELECT DISTINCT x FROM tbl'
4178
4179        Args:
4180            ons: the expressions to distinct on
4181            distinct: whether the Select should be distinct
4182            copy: if `False`, modify this expression instance in-place.
4183
4184        Returns:
4185            Select: the modified expression.
4186        """
4187        instance = maybe_copy(self, copy)
4188        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
4189        instance.set("distinct", Distinct(on=on) if distinct else None)
4190        return instance
4191
4192    def ctas(
4193        self,
4194        table: ExpOrStr,
4195        properties: t.Optional[t.Dict] = None,
4196        dialect: DialectType = None,
4197        copy: bool = True,
4198        **opts,
4199    ) -> Create:
4200        """
4201        Convert this expression to a CREATE TABLE AS statement.
4202
4203        Example:
4204            >>> Select().select("*").from_("tbl").ctas("x").sql()
4205            'CREATE TABLE x AS SELECT * FROM tbl'
4206
4207        Args:
4208            table: the SQL code string to parse as the table name.
4209                If another `Expression` instance is passed, it will be used as-is.
4210            properties: an optional mapping of table properties
4211            dialect: the dialect used to parse the input table.
4212            copy: if `False`, modify this expression instance in-place.
4213            opts: other options to use to parse the input table.
4214
4215        Returns:
4216            The new Create expression.
4217        """
4218        instance = maybe_copy(self, copy)
4219        table_expression = maybe_parse(table, into=Table, dialect=dialect, **opts)
4220
4221        properties_expression = None
4222        if properties:
4223            properties_expression = Properties.from_dict(properties)
4224
4225        return Create(
4226            this=table_expression,
4227            kind="TABLE",
4228            expression=instance,
4229            properties=properties_expression,
4230        )
4231
4232    def lock(self, update: bool = True, copy: bool = True) -> Select:
4233        """
4234        Set the locking read mode for this expression.
4235
4236        Examples:
4237            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
4238            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
4239
4240            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
4241            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
4242
4243        Args:
4244            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
4245            copy: if `False`, modify this expression instance in-place.
4246
4247        Returns:
4248            The modified expression.
4249        """
4250        inst = maybe_copy(self, copy)
4251        inst.set("locks", [Lock(update=update)])
4252
4253        return inst
4254
4255    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
4256        """
4257        Set hints for this expression.
4258
4259        Examples:
4260            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
4261            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
4262
4263        Args:
4264            hints: The SQL code strings to parse as the hints.
4265                If an `Expression` instance is passed, it will be used as-is.
4266            dialect: The dialect used to parse the hints.
4267            copy: If `False`, modify this expression instance in-place.
4268
4269        Returns:
4270            The modified expression.
4271        """
4272        inst = maybe_copy(self, copy)
4273        inst.set(
4274            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
4275        )
4276
4277        return inst
4278
4279    @property
4280    def named_selects(self) -> t.List[str]:
4281        return [e.output_name for e in self.expressions if e.alias_or_name]
4282
4283    @property
4284    def is_star(self) -> bool:
4285        return any(expression.is_star for expression in self.expressions)
4286
4287    @property
4288    def selects(self) -> t.List[Expression]:
4289        return self.expressions
4290
4291
4292UNWRAPPED_QUERIES = (Select, SetOperation)
4293
4294
4295class Subquery(DerivedTable, Query):
4296    arg_types = {
4297        "this": True,
4298        "alias": False,
4299        "with": False,
4300        **QUERY_MODIFIERS,
4301    }
4302
4303    def unnest(self):
4304        """Returns the first non subquery."""
4305        expression = self
4306        while isinstance(expression, Subquery):
4307            expression = expression.this
4308        return expression
4309
4310    def unwrap(self) -> Subquery:
4311        expression = self
4312        while expression.same_parent and expression.is_wrapper:
4313            expression = t.cast(Subquery, expression.parent)
4314        return expression
4315
4316    def select(
4317        self,
4318        *expressions: t.Optional[ExpOrStr],
4319        append: bool = True,
4320        dialect: DialectType = None,
4321        copy: bool = True,
4322        **opts,
4323    ) -> Subquery:
4324        this = maybe_copy(self, copy)
4325        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4326        return this
4327
4328    @property
4329    def is_wrapper(self) -> bool:
4330        """
4331        Whether this Subquery acts as a simple wrapper around another expression.
4332
4333        SELECT * FROM (((SELECT * FROM t)))
4334                      ^
4335                      This corresponds to a "wrapper" Subquery node
4336        """
4337        return all(v is None for k, v in self.args.items() if k != "this")
4338
4339    @property
4340    def is_star(self) -> bool:
4341        return self.this.is_star
4342
4343    @property
4344    def output_name(self) -> str:
4345        return self.alias
4346
4347
4348class TableSample(Expression):
4349    arg_types = {
4350        "expressions": False,
4351        "method": False,
4352        "bucket_numerator": False,
4353        "bucket_denominator": False,
4354        "bucket_field": False,
4355        "percent": False,
4356        "rows": False,
4357        "size": False,
4358        "seed": False,
4359    }
4360
4361
4362class Tag(Expression):
4363    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
4364
4365    arg_types = {
4366        "this": False,
4367        "prefix": False,
4368        "postfix": False,
4369    }
4370
4371
4372# Represents both the standard SQL PIVOT operator and DuckDB's "simplified" PIVOT syntax
4373# https://duckdb.org/docs/sql/statements/pivot
4374class Pivot(Expression):
4375    arg_types = {
4376        "this": False,
4377        "alias": False,
4378        "expressions": False,
4379        "fields": False,
4380        "unpivot": False,
4381        "using": False,
4382        "group": False,
4383        "columns": False,
4384        "include_nulls": False,
4385        "default_on_null": False,
4386        "into": False,
4387    }
4388
4389    @property
4390    def unpivot(self) -> bool:
4391        return bool(self.args.get("unpivot"))
4392
4393    @property
4394    def fields(self) -> t.List[Expression]:
4395        return self.args.get("fields", [])
4396
4397
4398# https://duckdb.org/docs/sql/statements/unpivot#simplified-unpivot-syntax
4399# UNPIVOT ... INTO [NAME <col_name> VALUE <col_value>][...,]
4400class UnpivotColumns(Expression):
4401    arg_types = {"this": True, "expressions": True}
4402
4403
4404class Window(Condition):
4405    arg_types = {
4406        "this": True,
4407        "partition_by": False,
4408        "order": False,
4409        "spec": False,
4410        "alias": False,
4411        "over": False,
4412        "first": False,
4413    }
4414
4415
4416class WindowSpec(Expression):
4417    arg_types = {
4418        "kind": False,
4419        "start": False,
4420        "start_side": False,
4421        "end": False,
4422        "end_side": False,
4423        "exclude": False,
4424    }
4425
4426
4427class PreWhere(Expression):
4428    pass
4429
4430
4431class Where(Expression):
4432    pass
4433
4434
4435class Star(Expression):
4436    arg_types = {"except": False, "replace": False, "rename": False}
4437
4438    @property
4439    def name(self) -> str:
4440        return "*"
4441
4442    @property
4443    def output_name(self) -> str:
4444        return self.name
4445
4446
4447class Parameter(Condition):
4448    arg_types = {"this": True, "expression": False}
4449
4450
4451class SessionParameter(Condition):
4452    arg_types = {"this": True, "kind": False}
4453
4454
4455class Placeholder(Condition):
4456    arg_types = {"this": False, "kind": False}
4457
4458    @property
4459    def name(self) -> str:
4460        return self.this or "?"
4461
4462
4463class Null(Condition):
4464    arg_types: t.Dict[str, t.Any] = {}
4465
4466    @property
4467    def name(self) -> str:
4468        return "NULL"
4469
4470    def to_py(self) -> Lit[None]:
4471        return None
4472
4473
4474class Boolean(Condition):
4475    def to_py(self) -> bool:
4476        return self.this
4477
4478
4479class DataTypeParam(Expression):
4480    arg_types = {"this": True, "expression": False}
4481
4482    @property
4483    def name(self) -> str:
4484        return self.this.name
4485
4486
4487# The `nullable` arg is helpful when transpiling types from other dialects to ClickHouse, which
4488# assumes non-nullable types by default. Values `None` and `True` mean the type is nullable.
4489class DataType(Expression):
4490    arg_types = {
4491        "this": True,
4492        "expressions": False,
4493        "nested": False,
4494        "values": False,
4495        "prefix": False,
4496        "kind": False,
4497        "nullable": False,
4498    }
4499
4500    class Type(AutoName):
4501        ARRAY = auto()
4502        AGGREGATEFUNCTION = auto()
4503        SIMPLEAGGREGATEFUNCTION = auto()
4504        BIGDECIMAL = auto()
4505        BIGINT = auto()
4506        BIGSERIAL = auto()
4507        BINARY = auto()
4508        BIT = auto()
4509        BLOB = auto()
4510        BOOLEAN = auto()
4511        BPCHAR = auto()
4512        CHAR = auto()
4513        DATE = auto()
4514        DATE32 = auto()
4515        DATEMULTIRANGE = auto()
4516        DATERANGE = auto()
4517        DATETIME = auto()
4518        DATETIME2 = auto()
4519        DATETIME64 = auto()
4520        DECIMAL = auto()
4521        DECIMAL32 = auto()
4522        DECIMAL64 = auto()
4523        DECIMAL128 = auto()
4524        DECIMAL256 = auto()
4525        DOUBLE = auto()
4526        DYNAMIC = auto()
4527        ENUM = auto()
4528        ENUM8 = auto()
4529        ENUM16 = auto()
4530        FIXEDSTRING = auto()
4531        FLOAT = auto()
4532        GEOGRAPHY = auto()
4533        GEOMETRY = auto()
4534        POINT = auto()
4535        RING = auto()
4536        LINESTRING = auto()
4537        MULTILINESTRING = auto()
4538        POLYGON = auto()
4539        MULTIPOLYGON = auto()
4540        HLLSKETCH = auto()
4541        HSTORE = auto()
4542        IMAGE = auto()
4543        INET = auto()
4544        INT = auto()
4545        INT128 = auto()
4546        INT256 = auto()
4547        INT4MULTIRANGE = auto()
4548        INT4RANGE = auto()
4549        INT8MULTIRANGE = auto()
4550        INT8RANGE = auto()
4551        INTERVAL = auto()
4552        IPADDRESS = auto()
4553        IPPREFIX = auto()
4554        IPV4 = auto()
4555        IPV6 = auto()
4556        JSON = auto()
4557        JSONB = auto()
4558        LIST = auto()
4559        LONGBLOB = auto()
4560        LONGTEXT = auto()
4561        LOWCARDINALITY = auto()
4562        MAP = auto()
4563        MEDIUMBLOB = auto()
4564        MEDIUMINT = auto()
4565        MEDIUMTEXT = auto()
4566        MONEY = auto()
4567        NAME = auto()
4568        NCHAR = auto()
4569        NESTED = auto()
4570        NOTHING = auto()
4571        NULL = auto()
4572        NUMMULTIRANGE = auto()
4573        NUMRANGE = auto()
4574        NVARCHAR = auto()
4575        OBJECT = auto()
4576        RANGE = auto()
4577        ROWVERSION = auto()
4578        SERIAL = auto()
4579        SET = auto()
4580        SMALLDATETIME = auto()
4581        SMALLINT = auto()
4582        SMALLMONEY = auto()
4583        SMALLSERIAL = auto()
4584        STRUCT = auto()
4585        SUPER = auto()
4586        TEXT = auto()
4587        TINYBLOB = auto()
4588        TINYTEXT = auto()
4589        TIME = auto()
4590        TIMETZ = auto()
4591        TIMESTAMP = auto()
4592        TIMESTAMPNTZ = auto()
4593        TIMESTAMPLTZ = auto()
4594        TIMESTAMPTZ = auto()
4595        TIMESTAMP_S = auto()
4596        TIMESTAMP_MS = auto()
4597        TIMESTAMP_NS = auto()
4598        TINYINT = auto()
4599        TSMULTIRANGE = auto()
4600        TSRANGE = auto()
4601        TSTZMULTIRANGE = auto()
4602        TSTZRANGE = auto()
4603        UBIGINT = auto()
4604        UINT = auto()
4605        UINT128 = auto()
4606        UINT256 = auto()
4607        UMEDIUMINT = auto()
4608        UDECIMAL = auto()
4609        UDOUBLE = auto()
4610        UNION = auto()
4611        UNKNOWN = auto()  # Sentinel value, useful for type annotation
4612        USERDEFINED = "USER-DEFINED"
4613        USMALLINT = auto()
4614        UTINYINT = auto()
4615        UUID = auto()
4616        VARBINARY = auto()
4617        VARCHAR = auto()
4618        VARIANT = auto()
4619        VECTOR = auto()
4620        XML = auto()
4621        YEAR = auto()
4622        TDIGEST = auto()
4623
4624    STRUCT_TYPES = {
4625        Type.NESTED,
4626        Type.OBJECT,
4627        Type.STRUCT,
4628        Type.UNION,
4629    }
4630
4631    ARRAY_TYPES = {
4632        Type.ARRAY,
4633        Type.LIST,
4634    }
4635
4636    NESTED_TYPES = {
4637        *STRUCT_TYPES,
4638        *ARRAY_TYPES,
4639        Type.MAP,
4640    }
4641
4642    TEXT_TYPES = {
4643        Type.CHAR,
4644        Type.NCHAR,
4645        Type.NVARCHAR,
4646        Type.TEXT,
4647        Type.VARCHAR,
4648        Type.NAME,
4649    }
4650
4651    SIGNED_INTEGER_TYPES = {
4652        Type.BIGINT,
4653        Type.INT,
4654        Type.INT128,
4655        Type.INT256,
4656        Type.MEDIUMINT,
4657        Type.SMALLINT,
4658        Type.TINYINT,
4659    }
4660
4661    UNSIGNED_INTEGER_TYPES = {
4662        Type.UBIGINT,
4663        Type.UINT,
4664        Type.UINT128,
4665        Type.UINT256,
4666        Type.UMEDIUMINT,
4667        Type.USMALLINT,
4668        Type.UTINYINT,
4669    }
4670
4671    INTEGER_TYPES = {
4672        *SIGNED_INTEGER_TYPES,
4673        *UNSIGNED_INTEGER_TYPES,
4674        Type.BIT,
4675    }
4676
4677    FLOAT_TYPES = {
4678        Type.DOUBLE,
4679        Type.FLOAT,
4680    }
4681
4682    REAL_TYPES = {
4683        *FLOAT_TYPES,
4684        Type.BIGDECIMAL,
4685        Type.DECIMAL,
4686        Type.DECIMAL32,
4687        Type.DECIMAL64,
4688        Type.DECIMAL128,
4689        Type.DECIMAL256,
4690        Type.MONEY,
4691        Type.SMALLMONEY,
4692        Type.UDECIMAL,
4693        Type.UDOUBLE,
4694    }
4695
4696    NUMERIC_TYPES = {
4697        *INTEGER_TYPES,
4698        *REAL_TYPES,
4699    }
4700
4701    TEMPORAL_TYPES = {
4702        Type.DATE,
4703        Type.DATE32,
4704        Type.DATETIME,
4705        Type.DATETIME2,
4706        Type.DATETIME64,
4707        Type.SMALLDATETIME,
4708        Type.TIME,
4709        Type.TIMESTAMP,
4710        Type.TIMESTAMPNTZ,
4711        Type.TIMESTAMPLTZ,
4712        Type.TIMESTAMPTZ,
4713        Type.TIMESTAMP_MS,
4714        Type.TIMESTAMP_NS,
4715        Type.TIMESTAMP_S,
4716        Type.TIMETZ,
4717    }
4718
4719    @classmethod
4720    def build(
4721        cls,
4722        dtype: DATA_TYPE,
4723        dialect: DialectType = None,
4724        udt: bool = False,
4725        copy: bool = True,
4726        **kwargs,
4727    ) -> DataType:
4728        """
4729        Constructs a DataType object.
4730
4731        Args:
4732            dtype: the data type of interest.
4733            dialect: the dialect to use for parsing `dtype`, in case it's a string.
4734            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
4735                DataType, thus creating a user-defined type.
4736            copy: whether to copy the data type.
4737            kwargs: additional arguments to pass in the constructor of DataType.
4738
4739        Returns:
4740            The constructed DataType object.
4741        """
4742        from sqlglot import parse_one
4743
4744        if isinstance(dtype, str):
4745            if dtype.upper() == "UNKNOWN":
4746                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
4747
4748            try:
4749                data_type_exp = parse_one(
4750                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
4751                )
4752            except ParseError:
4753                if udt:
4754                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4755                raise
4756        elif isinstance(dtype, (Identifier, Dot)) and udt:
4757            return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4758        elif isinstance(dtype, DataType.Type):
4759            data_type_exp = DataType(this=dtype)
4760        elif isinstance(dtype, DataType):
4761            return maybe_copy(dtype, copy)
4762        else:
4763            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
4764
4765        return DataType(**{**data_type_exp.args, **kwargs})
4766
4767    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
4768        """
4769        Checks whether this DataType matches one of the provided data types. Nested types or precision
4770        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
4771
4772        Args:
4773            dtypes: the data types to compare this DataType to.
4774            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
4775                If false, it means that NULLABLE<INT> is equivalent to INT.
4776
4777        Returns:
4778            True, if and only if there is a type in `dtypes` which is equal to this DataType.
4779        """
4780        self_is_nullable = self.args.get("nullable")
4781        for dtype in dtypes:
4782            other_type = DataType.build(dtype, copy=False, udt=True)
4783            other_is_nullable = other_type.args.get("nullable")
4784            if (
4785                other_type.expressions
4786                or (check_nullable and (self_is_nullable or other_is_nullable))
4787                or self.this == DataType.Type.USERDEFINED
4788                or other_type.this == DataType.Type.USERDEFINED
4789            ):
4790                matches = self == other_type
4791            else:
4792                matches = self.this == other_type.this
4793
4794            if matches:
4795                return True
4796        return False
4797
4798
4799# https://www.postgresql.org/docs/15/datatype-pseudo.html
4800class PseudoType(DataType):
4801    arg_types = {"this": True}
4802
4803
4804# https://www.postgresql.org/docs/15/datatype-oid.html
4805class ObjectIdentifier(DataType):
4806    arg_types = {"this": True}
4807
4808
4809# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...)
4810class SubqueryPredicate(Predicate):
4811    pass
4812
4813
4814class All(SubqueryPredicate):
4815    pass
4816
4817
4818class Any(SubqueryPredicate):
4819    pass
4820
4821
4822# Commands to interact with the databases or engines. For most of the command
4823# expressions we parse whatever comes after the command's name as a string.
4824class Command(Expression):
4825    arg_types = {"this": True, "expression": False}
4826
4827
4828class Transaction(Expression):
4829    arg_types = {"this": False, "modes": False, "mark": False}
4830
4831
4832class Commit(Expression):
4833    arg_types = {"chain": False, "this": False, "durability": False}
4834
4835
4836class Rollback(Expression):
4837    arg_types = {"savepoint": False, "this": False}
4838
4839
4840class Alter(Expression):
4841    arg_types = {
4842        "this": True,
4843        "kind": True,
4844        "actions": True,
4845        "exists": False,
4846        "only": False,
4847        "options": False,
4848        "cluster": False,
4849        "not_valid": False,
4850    }
4851
4852    @property
4853    def kind(self) -> t.Optional[str]:
4854        kind = self.args.get("kind")
4855        return kind and kind.upper()
4856
4857    @property
4858    def actions(self) -> t.List[Expression]:
4859        return self.args.get("actions") or []
4860
4861
4862class Analyze(Expression):
4863    arg_types = {
4864        "kind": False,
4865        "this": False,
4866        "options": False,
4867        "mode": False,
4868        "partition": False,
4869        "expression": False,
4870        "properties": False,
4871    }
4872
4873
4874class AnalyzeStatistics(Expression):
4875    arg_types = {
4876        "kind": True,
4877        "option": False,
4878        "this": False,
4879        "expressions": False,
4880    }
4881
4882
4883class AnalyzeHistogram(Expression):
4884    arg_types = {
4885        "this": True,
4886        "expressions": True,
4887        "expression": False,
4888        "update_options": False,
4889    }
4890
4891
4892class AnalyzeSample(Expression):
4893    arg_types = {"kind": True, "sample": True}
4894
4895
4896class AnalyzeListChainedRows(Expression):
4897    arg_types = {"expression": False}
4898
4899
4900class AnalyzeDelete(Expression):
4901    arg_types = {"kind": False}
4902
4903
4904class AnalyzeWith(Expression):
4905    arg_types = {"expressions": True}
4906
4907
4908class AnalyzeValidate(Expression):
4909    arg_types = {
4910        "kind": True,
4911        "this": False,
4912        "expression": False,
4913    }
4914
4915
4916class AnalyzeColumns(Expression):
4917    pass
4918
4919
4920class UsingData(Expression):
4921    pass
4922
4923
4924class AddConstraint(Expression):
4925    arg_types = {"expressions": True}
4926
4927
4928class AddPartition(Expression):
4929    arg_types = {"this": True, "exists": False}
4930
4931
4932class AttachOption(Expression):
4933    arg_types = {"this": True, "expression": False}
4934
4935
4936class DropPartition(Expression):
4937    arg_types = {"expressions": True, "exists": False}
4938
4939
4940# https://clickhouse.com/docs/en/sql-reference/statements/alter/partition#replace-partition
4941class ReplacePartition(Expression):
4942    arg_types = {"expression": True, "source": True}
4943
4944
4945# Binary expressions like (ADD a b)
4946class Binary(Condition):
4947    arg_types = {"this": True, "expression": True}
4948
4949    @property
4950    def left(self) -> Expression:
4951        return self.this
4952
4953    @property
4954    def right(self) -> Expression:
4955        return self.expression
4956
4957
4958class Add(Binary):
4959    pass
4960
4961
4962class Connector(Binary):
4963    pass
4964
4965
4966class BitwiseAnd(Binary):
4967    pass
4968
4969
4970class BitwiseLeftShift(Binary):
4971    pass
4972
4973
4974class BitwiseOr(Binary):
4975    pass
4976
4977
4978class BitwiseRightShift(Binary):
4979    pass
4980
4981
4982class BitwiseXor(Binary):
4983    pass
4984
4985
4986class Div(Binary):
4987    arg_types = {"this": True, "expression": True, "typed": False, "safe": False}
4988
4989
4990class Overlaps(Binary):
4991    pass
4992
4993
4994class Dot(Binary):
4995    @property
4996    def is_star(self) -> bool:
4997        return self.expression.is_star
4998
4999    @property
5000    def name(self) -> str:
5001        return self.expression.name
5002
5003    @property
5004    def output_name(self) -> str:
5005        return self.name
5006
5007    @classmethod
5008    def build(self, expressions: t.Sequence[Expression]) -> Dot:
5009        """Build a Dot object with a sequence of expressions."""
5010        if len(expressions) < 2:
5011            raise ValueError("Dot requires >= 2 expressions.")
5012
5013        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))
5014
5015    @property
5016    def parts(self) -> t.List[Expression]:
5017        """Return the parts of a table / column in order catalog, db, table."""
5018        this, *parts = self.flatten()
5019
5020        parts.reverse()
5021
5022        for arg in COLUMN_PARTS:
5023            part = this.args.get(arg)
5024
5025            if isinstance(part, Expression):
5026                parts.append(part)
5027
5028        parts.reverse()
5029        return parts
5030
5031
5032DATA_TYPE = t.Union[str, Identifier, Dot, DataType, DataType.Type]
5033
5034
5035class DPipe(Binary):
5036    arg_types = {"this": True, "expression": True, "safe": False}
5037
5038
5039class EQ(Binary, Predicate):
5040    pass
5041
5042
5043class NullSafeEQ(Binary, Predicate):
5044    pass
5045
5046
5047class NullSafeNEQ(Binary, Predicate):
5048    pass
5049
5050
5051# Represents e.g. := in DuckDB which is mostly used for setting parameters
5052class PropertyEQ(Binary):
5053    pass
5054
5055
5056class Distance(Binary):
5057    pass
5058
5059
5060class Escape(Binary):
5061    pass
5062
5063
5064class Glob(Binary, Predicate):
5065    pass
5066
5067
5068class GT(Binary, Predicate):
5069    pass
5070
5071
5072class GTE(Binary, Predicate):
5073    pass
5074
5075
5076class ILike(Binary, Predicate):
5077    pass
5078
5079
5080class ILikeAny(Binary, Predicate):
5081    pass
5082
5083
5084class IntDiv(Binary):
5085    pass
5086
5087
5088class Is(Binary, Predicate):
5089    pass
5090
5091
5092class Kwarg(Binary):
5093    """Kwarg in special functions like func(kwarg => y)."""
5094
5095
5096class Like(Binary, Predicate):
5097    pass
5098
5099
5100class LikeAny(Binary, Predicate):
5101    pass
5102
5103
5104class LT(Binary, Predicate):
5105    pass
5106
5107
5108class LTE(Binary, Predicate):
5109    pass
5110
5111
5112class Mod(Binary):
5113    pass
5114
5115
5116class Mul(Binary):
5117    pass
5118
5119
5120class NEQ(Binary, Predicate):
5121    pass
5122
5123
5124# https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH
5125class Operator(Binary):
5126    arg_types = {"this": True, "operator": True, "expression": True}
5127
5128
5129class SimilarTo(Binary, Predicate):
5130    pass
5131
5132
5133class Slice(Binary):
5134    arg_types = {"this": False, "expression": False}
5135
5136
5137class Sub(Binary):
5138    pass
5139
5140
5141# Unary Expressions
5142# (NOT a)
5143class Unary(Condition):
5144    pass
5145
5146
5147class BitwiseNot(Unary):
5148    pass
5149
5150
5151class Not(Unary):
5152    pass
5153
5154
5155class Paren(Unary):
5156    @property
5157    def output_name(self) -> str:
5158        return self.this.name
5159
5160
5161class Neg(Unary):
5162    def to_py(self) -> int | Decimal:
5163        if self.is_number:
5164            return self.this.to_py() * -1
5165        return super().to_py()
5166
5167
5168class Alias(Expression):
5169    arg_types = {"this": True, "alias": False}
5170
5171    @property
5172    def output_name(self) -> str:
5173        return self.alias
5174
5175
5176# BigQuery requires the UNPIVOT column list aliases to be either strings or ints, but
5177# other dialects require identifiers. This enables us to transpile between them easily.
5178class PivotAlias(Alias):
5179    pass
5180
5181
5182# Represents Snowflake's ANY [ ORDER BY ... ] syntax
5183# https://docs.snowflake.com/en/sql-reference/constructs/pivot
5184class PivotAny(Expression):
5185    arg_types = {"this": False}
5186
5187
5188class Aliases(Expression):
5189    arg_types = {"this": True, "expressions": True}
5190
5191    @property
5192    def aliases(self):
5193        return self.expressions
5194
5195
5196# https://docs.aws.amazon.com/redshift/latest/dg/query-super.html
5197class AtIndex(Expression):
5198    arg_types = {"this": True, "expression": True}
5199
5200
5201class AtTimeZone(Expression):
5202    arg_types = {"this": True, "zone": True}
5203
5204
5205class FromTimeZone(Expression):
5206    arg_types = {"this": True, "zone": True}
5207
5208
5209class Between(Predicate):
5210    arg_types = {"this": True, "low": True, "high": True}
5211
5212
5213class Bracket(Condition):
5214    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
5215    arg_types = {
5216        "this": True,
5217        "expressions": True,
5218        "offset": False,
5219        "safe": False,
5220        "returns_list_for_maps": False,
5221    }
5222
5223    @property
5224    def output_name(self) -> str:
5225        if len(self.expressions) == 1:
5226            return self.expressions[0].output_name
5227
5228        return super().output_name
5229
5230
5231class Distinct(Expression):
5232    arg_types = {"expressions": False, "on": False}
5233
5234
5235class In(Predicate):
5236    arg_types = {
5237        "this": True,
5238        "expressions": False,
5239        "query": False,
5240        "unnest": False,
5241        "field": False,
5242        "is_global": False,
5243    }
5244
5245
5246# https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#for-in
5247class ForIn(Expression):
5248    arg_types = {"this": True, "expression": True}
5249
5250
5251class TimeUnit(Expression):
5252    """Automatically converts unit arg into a var."""
5253
5254    arg_types = {"unit": False}
5255
5256    UNABBREVIATED_UNIT_NAME = {
5257        "D": "DAY",
5258        "H": "HOUR",
5259        "M": "MINUTE",
5260        "MS": "MILLISECOND",
5261        "NS": "NANOSECOND",
5262        "Q": "QUARTER",
5263        "S": "SECOND",
5264        "US": "MICROSECOND",
5265        "W": "WEEK",
5266        "Y": "YEAR",
5267    }
5268
5269    VAR_LIKE = (Column, Literal, Var)
5270
5271    def __init__(self, **args):
5272        unit = args.get("unit")
5273        if isinstance(unit, self.VAR_LIKE):
5274            args["unit"] = Var(
5275                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
5276            )
5277        elif isinstance(unit, Week):
5278            unit.set("this", Var(this=unit.this.name.upper()))
5279
5280        super().__init__(**args)
5281
5282    @property
5283    def unit(self) -> t.Optional[Var | IntervalSpan]:
5284        return self.args.get("unit")
5285
5286
5287class IntervalOp(TimeUnit):
5288    arg_types = {"unit": False, "expression": True}
5289
5290    def interval(self):
5291        return Interval(
5292            this=self.expression.copy(),
5293            unit=self.unit.copy() if self.unit else None,
5294        )
5295
5296
5297# https://www.oracletutorial.com/oracle-basics/oracle-interval/
5298# https://trino.io/docs/current/language/types.html#interval-day-to-second
5299# https://docs.databricks.com/en/sql/language-manual/data-types/interval-type.html
5300class IntervalSpan(DataType):
5301    arg_types = {"this": True, "expression": True}
5302
5303
5304class Interval(TimeUnit):
5305    arg_types = {"this": False, "unit": False}
5306
5307
5308class IgnoreNulls(Expression):
5309    pass
5310
5311
5312class RespectNulls(Expression):
5313    pass
5314
5315
5316# https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate-function-calls#max_min_clause
5317class HavingMax(Expression):
5318    arg_types = {"this": True, "expression": True, "max": True}
5319
5320
5321# Functions
5322class Func(Condition):
5323    """
5324    The base class for all function expressions.
5325
5326    Attributes:
5327        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
5328            treated as a variable length argument and the argument's value will be stored as a list.
5329        _sql_names (list): the SQL name (1st item in the list) and aliases (subsequent items) for this
5330            function expression. These values are used to map this node to a name during parsing as
5331            well as to provide the function's name during SQL string generation. By default the SQL
5332            name is set to the expression's class name transformed to snake case.
5333    """
5334
5335    is_var_len_args = False
5336
5337    @classmethod
5338    def from_arg_list(cls, args):
5339        if cls.is_var_len_args:
5340            all_arg_keys = list(cls.arg_types)
5341            # If this function supports variable length argument treat the last argument as such.
5342            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5343            num_non_var = len(non_var_len_arg_keys)
5344
5345            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5346            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5347        else:
5348            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5349
5350        return cls(**args_dict)
5351
5352    @classmethod
5353    def sql_names(cls):
5354        if cls is Func:
5355            raise NotImplementedError(
5356                "SQL name is only supported by concrete function implementations"
5357            )
5358        if "_sql_names" not in cls.__dict__:
5359            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5360        return cls._sql_names
5361
5362    @classmethod
5363    def sql_name(cls):
5364        return cls.sql_names()[0]
5365
5366    @classmethod
5367    def default_parser_mappings(cls):
5368        return {name: cls.from_arg_list for name in cls.sql_names()}
5369
5370
5371class AggFunc(Func):
5372    pass
5373
5374
5375class ArrayRemove(Func):
5376    arg_types = {"this": True, "expression": True}
5377
5378
5379class ParameterizedAgg(AggFunc):
5380    arg_types = {"this": True, "expressions": True, "params": True}
5381
5382
5383class Abs(Func):
5384    pass
5385
5386
5387class ArgMax(AggFunc):
5388    arg_types = {"this": True, "expression": True, "count": False}
5389    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
5390
5391
5392class ArgMin(AggFunc):
5393    arg_types = {"this": True, "expression": True, "count": False}
5394    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
5395
5396
5397class ApproxTopK(AggFunc):
5398    arg_types = {"this": True, "expression": False, "counters": False}
5399
5400
5401class Flatten(Func):
5402    pass
5403
5404
5405# https://spark.apache.org/docs/latest/api/sql/index.html#transform
5406class Transform(Func):
5407    arg_types = {"this": True, "expression": True}
5408
5409
5410class Anonymous(Func):
5411    arg_types = {"this": True, "expressions": False}
5412    is_var_len_args = True
5413
5414    @property
5415    def name(self) -> str:
5416        return self.this if isinstance(self.this, str) else self.this.name
5417
5418
5419class AnonymousAggFunc(AggFunc):
5420    arg_types = {"this": True, "expressions": False}
5421    is_var_len_args = True
5422
5423
5424# https://clickhouse.com/docs/en/sql-reference/aggregate-functions/combinators
5425class CombinedAggFunc(AnonymousAggFunc):
5426    arg_types = {"this": True, "expressions": False}
5427
5428
5429class CombinedParameterizedAgg(ParameterizedAgg):
5430    arg_types = {"this": True, "expressions": True, "params": True}
5431
5432
5433# https://docs.snowflake.com/en/sql-reference/functions/hll
5434# https://docs.aws.amazon.com/redshift/latest/dg/r_HLL_function.html
5435class Hll(AggFunc):
5436    arg_types = {"this": True, "expressions": False}
5437    is_var_len_args = True
5438
5439
5440class ApproxDistinct(AggFunc):
5441    arg_types = {"this": True, "accuracy": False}
5442    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
5443
5444
5445class Apply(Func):
5446    arg_types = {"this": True, "expression": True}
5447
5448
5449class Array(Func):
5450    arg_types = {"expressions": False, "bracket_notation": False}
5451    is_var_len_args = True
5452
5453
5454# https://docs.snowflake.com/en/sql-reference/functions/to_array
5455class ToArray(Func):
5456    pass
5457
5458
5459# https://materialize.com/docs/sql/types/list/
5460class List(Func):
5461    arg_types = {"expressions": False}
5462    is_var_len_args = True
5463
5464
5465# String pad, kind True -> LPAD, False -> RPAD
5466class Pad(Func):
5467    arg_types = {"this": True, "expression": True, "fill_pattern": False, "is_left": True}
5468
5469
5470# https://docs.snowflake.com/en/sql-reference/functions/to_char
5471# https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/TO_CHAR-number.html
5472class ToChar(Func):
5473    arg_types = {"this": True, "format": False, "nlsparam": False}
5474
5475
5476# https://docs.snowflake.com/en/sql-reference/functions/to_decimal
5477# https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/TO_NUMBER.html
5478class ToNumber(Func):
5479    arg_types = {
5480        "this": True,
5481        "format": False,
5482        "nlsparam": False,
5483        "precision": False,
5484        "scale": False,
5485    }
5486
5487
5488# https://docs.snowflake.com/en/sql-reference/functions/to_double
5489class ToDouble(Func):
5490    arg_types = {
5491        "this": True,
5492        "format": False,
5493    }
5494
5495
5496class Columns(Func):
5497    arg_types = {"this": True, "unpack": False}
5498
5499
5500# https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver16#syntax
5501class Convert(Func):
5502    arg_types = {"this": True, "expression": True, "style": False}
5503
5504
5505# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/CONVERT.html
5506class ConvertToCharset(Func):
5507    arg_types = {"this": True, "dest": True, "source": False}
5508
5509
5510class ConvertTimezone(Func):
5511    arg_types = {"source_tz": False, "target_tz": True, "timestamp": True}
5512
5513
5514class GenerateSeries(Func):
5515    arg_types = {"start": True, "end": True, "step": False, "is_end_exclusive": False}
5516
5517
5518# Postgres' GENERATE_SERIES function returns a row set, i.e. it implicitly explodes when it's
5519# used in a projection, so this expression is a helper that facilitates transpilation to other
5520# dialects. For example, we'd generate UNNEST(GENERATE_SERIES(...)) in DuckDB
5521class ExplodingGenerateSeries(GenerateSeries):
5522    pass
5523
5524
5525class ArrayAgg(AggFunc):
5526    arg_types = {"this": True, "nulls_excluded": False}
5527
5528
5529class ArrayUniqueAgg(AggFunc):
5530    pass
5531
5532
5533class ArrayAll(Func):
5534    arg_types = {"this": True, "expression": True}
5535
5536
5537# Represents Python's `any(f(x) for x in array)`, where `array` is `this` and `f` is `expression`
5538class ArrayAny(Func):
5539    arg_types = {"this": True, "expression": True}
5540
5541
5542class ArrayConcat(Func):
5543    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
5544    arg_types = {"this": True, "expressions": False}
5545    is_var_len_args = True
5546
5547
5548class ArrayConcatAgg(AggFunc):
5549    pass
5550
5551
5552class ArrayConstructCompact(Func):
5553    arg_types = {"expressions": True}
5554    is_var_len_args = True
5555
5556
5557class ArrayContains(Binary, Func):
5558    _sql_names = ["ARRAY_CONTAINS", "ARRAY_HAS"]
5559
5560
5561class ArrayContainsAll(Binary, Func):
5562    _sql_names = ["ARRAY_CONTAINS_ALL", "ARRAY_HAS_ALL"]
5563
5564
5565class ArrayFilter(Func):
5566    arg_types = {"this": True, "expression": True}
5567    _sql_names = ["FILTER", "ARRAY_FILTER"]
5568
5569
5570class ArrayToString(Func):
5571    arg_types = {"this": True, "expression": True, "null": False}
5572    _sql_names = ["ARRAY_TO_STRING", "ARRAY_JOIN"]
5573
5574
5575class ArrayIntersect(Func):
5576    arg_types = {"expressions": True}
5577    is_var_len_args = True
5578    _sql_names = ["ARRAY_INTERSECT", "ARRAY_INTERSECTION"]
5579
5580
5581class StPoint(Func):
5582    arg_types = {"this": True, "expression": True, "null": False}
5583    _sql_names = ["ST_POINT", "ST_MAKEPOINT"]
5584
5585
5586class StDistance(Func):
5587    arg_types = {"this": True, "expression": True, "use_spheroid": False}
5588
5589
5590# https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#string
5591class String(Func):
5592    arg_types = {"this": True, "zone": False}
5593
5594
5595class StringToArray(Func):
5596    arg_types = {"this": True, "expression": True, "null": False}
5597    _sql_names = ["STRING_TO_ARRAY", "SPLIT_BY_STRING", "STRTOK_TO_ARRAY"]
5598
5599
5600class ArrayOverlaps(Binary, Func):
5601    pass
5602
5603
5604class ArraySize(Func):
5605    arg_types = {"this": True, "expression": False}
5606    _sql_names = ["ARRAY_SIZE", "ARRAY_LENGTH"]
5607
5608
5609class ArraySort(Func):
5610    arg_types = {"this": True, "expression": False}
5611
5612
5613class ArraySum(Func):
5614    arg_types = {"this": True, "expression": False}
5615
5616
5617class ArrayUnionAgg(AggFunc):
5618    pass
5619
5620
5621class Avg(AggFunc):
5622    pass
5623
5624
5625class AnyValue(AggFunc):
5626    pass
5627
5628
5629class Lag(AggFunc):
5630    arg_types = {"this": True, "offset": False, "default": False}
5631
5632
5633class Lead(AggFunc):
5634    arg_types = {"this": True, "offset": False, "default": False}
5635
5636
5637# some dialects have a distinction between first and first_value, usually first is an aggregate func
5638# and first_value is a window func
5639class First(AggFunc):
5640    pass
5641
5642
5643class Last(AggFunc):
5644    pass
5645
5646
5647class FirstValue(AggFunc):
5648    pass
5649
5650
5651class LastValue(AggFunc):
5652    pass
5653
5654
5655class NthValue(AggFunc):
5656    arg_types = {"this": True, "offset": True}
5657
5658
5659class Case(Func):
5660    arg_types = {"this": False, "ifs": True, "default": False}
5661
5662    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5663        instance = maybe_copy(self, copy)
5664        instance.append(
5665            "ifs",
5666            If(
5667                this=maybe_parse(condition, copy=copy, **opts),
5668                true=maybe_parse(then, copy=copy, **opts),
5669            ),
5670        )
5671        return instance
5672
5673    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5674        instance = maybe_copy(self, copy)
5675        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5676        return instance
5677
5678
5679class Cast(Func):
5680    arg_types = {
5681        "this": True,
5682        "to": True,
5683        "format": False,
5684        "safe": False,
5685        "action": False,
5686        "default": False,
5687    }
5688
5689    @property
5690    def name(self) -> str:
5691        return self.this.name
5692
5693    @property
5694    def to(self) -> DataType:
5695        return self.args["to"]
5696
5697    @property
5698    def output_name(self) -> str:
5699        return self.name
5700
5701    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5702        """
5703        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5704        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5705        array<int> != array<float>.
5706
5707        Args:
5708            dtypes: the data types to compare this Cast's DataType to.
5709
5710        Returns:
5711            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5712        """
5713        return self.to.is_type(*dtypes)
5714
5715
5716class TryCast(Cast):
5717    pass
5718
5719
5720# https://clickhouse.com/docs/sql-reference/data-types/newjson#reading-json-paths-as-sub-columns
5721class JSONCast(Cast):
5722    pass
5723
5724
5725class Try(Func):
5726    pass
5727
5728
5729class CastToStrType(Func):
5730    arg_types = {"this": True, "to": True}
5731
5732
5733# https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Functions-Expressions-and-Predicates/String-Operators-and-Functions/TRANSLATE/TRANSLATE-Function-Syntax
5734class TranslateCharacters(Expression):
5735    arg_types = {"this": True, "expression": True, "with_error": False}
5736
5737
5738class Collate(Binary, Func):
5739    pass
5740
5741
5742class Ceil(Func):
5743    arg_types = {"this": True, "decimals": False, "to": False}
5744    _sql_names = ["CEIL", "CEILING"]
5745
5746
5747class Coalesce(Func):
5748    arg_types = {"this": True, "expressions": False, "is_nvl": False, "is_null": False}
5749    is_var_len_args = True
5750    _sql_names = ["COALESCE", "IFNULL", "NVL"]
5751
5752
5753class Chr(Func):
5754    arg_types = {"expressions": True, "charset": False}
5755    is_var_len_args = True
5756    _sql_names = ["CHR", "CHAR"]
5757
5758
5759class Concat(Func):
5760    arg_types = {"expressions": True, "safe": False, "coalesce": False}
5761    is_var_len_args = True
5762
5763
5764class ConcatWs(Concat):
5765    _sql_names = ["CONCAT_WS"]
5766
5767
5768class Contains(Func):
5769    arg_types = {"this": True, "expression": True}
5770
5771
5772# https://docs.oracle.com/cd/B13789_01/server.101/b10759/operators004.htm#i1035022
5773class ConnectByRoot(Func):
5774    pass
5775
5776
5777class Count(AggFunc):
5778    arg_types = {"this": False, "expressions": False, "big_int": False}
5779    is_var_len_args = True
5780
5781
5782class CountIf(AggFunc):
5783    _sql_names = ["COUNT_IF", "COUNTIF"]
5784
5785
5786# cube root
5787class Cbrt(Func):
5788    pass
5789
5790
5791class CurrentDate(Func):
5792    arg_types = {"this": False}
5793
5794
5795class CurrentDatetime(Func):
5796    arg_types = {"this": False}
5797
5798
5799class CurrentTime(Func):
5800    arg_types = {"this": False}
5801
5802
5803class CurrentTimestamp(Func):
5804    arg_types = {"this": False, "sysdate": False}
5805
5806
5807class CurrentSchema(Func):
5808    arg_types = {"this": False}
5809
5810
5811class CurrentUser(Func):
5812    arg_types = {"this": False}
5813
5814
5815class DateAdd(Func, IntervalOp):
5816    arg_types = {"this": True, "expression": True, "unit": False}
5817
5818
5819class DateBin(Func, IntervalOp):
5820    arg_types = {"this": True, "expression": True, "unit": False, "zone": False}
5821
5822
5823class DateSub(Func, IntervalOp):
5824    arg_types = {"this": True, "expression": True, "unit": False}
5825
5826
5827class DateDiff(Func, TimeUnit):
5828    _sql_names = ["DATEDIFF", "DATE_DIFF"]
5829    arg_types = {"this": True, "expression": True, "unit": False, "zone": False}
5830
5831
5832class DateTrunc(Func):
5833    arg_types = {"unit": True, "this": True, "zone": False}
5834
5835    def __init__(self, **args):
5836        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5837        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5838        unabbreviate = args.pop("unabbreviate", True)
5839
5840        unit = args.get("unit")
5841        if isinstance(unit, TimeUnit.VAR_LIKE):
5842            unit_name = unit.name.upper()
5843            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5844                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5845
5846            args["unit"] = Literal.string(unit_name)
5847        elif isinstance(unit, Week):
5848            unit.set("this", Literal.string(unit.this.name.upper()))
5849
5850        super().__init__(**args)
5851
5852    @property
5853    def unit(self) -> Expression:
5854        return self.args["unit"]
5855
5856
5857# https://cloud.google.com/bigquery/docs/reference/standard-sql/datetime_functions#datetime
5858# expression can either be time_expr or time_zone
5859class Datetime(Func):
5860    arg_types = {"this": True, "expression": False}
5861
5862
5863class DatetimeAdd(Func, IntervalOp):
5864    arg_types = {"this": True, "expression": True, "unit": False}
5865
5866
5867class DatetimeSub(Func, IntervalOp):
5868    arg_types = {"this": True, "expression": True, "unit": False}
5869
5870
5871class DatetimeDiff(Func, TimeUnit):
5872    arg_types = {"this": True, "expression": True, "unit": False}
5873
5874
5875class DatetimeTrunc(Func, TimeUnit):
5876    arg_types = {"this": True, "unit": True, "zone": False}
5877
5878
5879class DayOfWeek(Func):
5880    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
5881
5882
5883# https://duckdb.org/docs/sql/functions/datepart.html#part-specifiers-only-usable-as-date-part-specifiers
5884# ISO day of week function in duckdb is ISODOW
5885class DayOfWeekIso(Func):
5886    _sql_names = ["DAYOFWEEK_ISO", "ISODOW"]
5887
5888
5889class DayOfMonth(Func):
5890    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
5891
5892
5893class DayOfYear(Func):
5894    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
5895
5896
5897class ToDays(Func):
5898    pass
5899
5900
5901class WeekOfYear(Func):
5902    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
5903
5904
5905class MonthsBetween(Func):
5906    arg_types = {"this": True, "expression": True, "roundoff": False}
5907
5908
5909class MakeInterval(Func):
5910    arg_types = {
5911        "year": False,
5912        "month": False,
5913        "day": False,
5914        "hour": False,
5915        "minute": False,
5916        "second": False,
5917    }
5918
5919
5920class LastDay(Func, TimeUnit):
5921    _sql_names = ["LAST_DAY", "LAST_DAY_OF_MONTH"]
5922    arg_types = {"this": True, "unit": False}
5923
5924
5925class Extract(Func):
5926    arg_types = {"this": True, "expression": True}
5927
5928
5929class Exists(Func, SubqueryPredicate):
5930    arg_types = {"this": True, "expression": False}
5931
5932
5933class Timestamp(Func):
5934    arg_types = {"this": False, "zone": False, "with_tz": False}
5935
5936
5937class TimestampAdd(Func, TimeUnit):
5938    arg_types = {"this": True, "expression": True, "unit": False}
5939
5940
5941class TimestampSub(Func, TimeUnit):
5942    arg_types = {"this": True, "expression": True, "unit": False}
5943
5944
5945class TimestampDiff(Func, TimeUnit):
5946    _sql_names = ["TIMESTAMPDIFF", "TIMESTAMP_DIFF"]
5947    arg_types = {"this": True, "expression": True, "unit": False}
5948
5949
5950class TimestampTrunc(Func, TimeUnit):
5951    arg_types = {"this": True, "unit": True, "zone": False}
5952
5953
5954class TimeAdd(Func, TimeUnit):
5955    arg_types = {"this": True, "expression": True, "unit": False}
5956
5957
5958class TimeSub(Func, TimeUnit):
5959    arg_types = {"this": True, "expression": True, "unit": False}
5960
5961
5962class TimeDiff(Func, TimeUnit):
5963    arg_types = {"this": True, "expression": True, "unit": False}
5964
5965
5966class TimeTrunc(Func, TimeUnit):
5967    arg_types = {"this": True, "unit": True, "zone": False}
5968
5969
5970class DateFromParts(Func):
5971    _sql_names = ["DATE_FROM_PARTS", "DATEFROMPARTS"]
5972    arg_types = {"year": True, "month": True, "day": True}
5973
5974
5975class TimeFromParts(Func):
5976    _sql_names = ["TIME_FROM_PARTS", "TIMEFROMPARTS"]
5977    arg_types = {
5978        "hour": True,
5979        "min": True,
5980        "sec": True,
5981        "nano": False,
5982        "fractions": False,
5983        "precision": False,
5984    }
5985
5986
5987class DateStrToDate(Func):
5988    pass
5989
5990
5991class DateToDateStr(Func):
5992    pass
5993
5994
5995class DateToDi(Func):
5996    pass
5997
5998
5999# https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date
6000class Date(Func):
6001    arg_types = {"this": False, "zone": False, "expressions": False}
6002    is_var_len_args = True
6003
6004
6005class Day(Func):
6006    pass
6007
6008
6009class Decode(Func):
6010    arg_types = {"this": True, "charset": True, "replace": False}
6011
6012
6013class DiToDate(Func):
6014    pass
6015
6016
6017class Encode(Func):
6018    arg_types = {"this": True, "charset": True}
6019
6020
6021class Exp(Func):
6022    pass
6023
6024
6025# https://docs.snowflake.com/en/sql-reference/functions/flatten
6026class Explode(Func, UDTF):
6027    arg_types = {"this": True, "expressions": False}
6028    is_var_len_args = True
6029
6030
6031# https://spark.apache.org/docs/latest/api/sql/#inline
6032class Inline(Func):
6033    pass
6034
6035
6036class ExplodeOuter(Explode):
6037    pass
6038
6039
6040class Posexplode(Explode):
6041    pass
6042
6043
6044class PosexplodeOuter(Posexplode, ExplodeOuter):
6045    pass
6046
6047
6048class Unnest(Func, UDTF):
6049    arg_types = {
6050        "expressions": True,
6051        "alias": False,
6052        "offset": False,
6053        "explode_array": False,
6054    }
6055
6056    @property
6057    def selects(self) -> t.List[Expression]:
6058        columns = super().selects
6059        offset = self.args.get("offset")
6060        if offset:
6061            columns = columns + [to_identifier("offset") if offset is True else offset]
6062        return columns
6063
6064
6065class Floor(Func):
6066    arg_types = {"this": True, "decimals": False, "to": False}
6067
6068
6069class FromBase64(Func):
6070    pass
6071
6072
6073class FeaturesAtTime(Func):
6074    arg_types = {"this": True, "time": False, "num_rows": False, "ignore_feature_nulls": False}
6075
6076
6077class ToBase64(Func):
6078    pass
6079
6080
6081# https://trino.io/docs/current/functions/datetime.html#from_iso8601_timestamp
6082class FromISO8601Timestamp(Func):
6083    _sql_names = ["FROM_ISO8601_TIMESTAMP"]
6084
6085
6086class GapFill(Func):
6087    arg_types = {
6088        "this": True,
6089        "ts_column": True,
6090        "bucket_width": True,
6091        "partitioning_columns": False,
6092        "value_columns": False,
6093        "origin": False,
6094        "ignore_nulls": False,
6095    }
6096
6097
6098# https://cloud.google.com/bigquery/docs/reference/standard-sql/array_functions#generate_date_array
6099class GenerateDateArray(Func):
6100    arg_types = {"start": True, "end": True, "step": False}
6101
6102
6103# https://cloud.google.com/bigquery/docs/reference/standard-sql/array_functions#generate_timestamp_array
6104class GenerateTimestampArray(Func):
6105    arg_types = {"start": True, "end": True, "step": True}
6106
6107
6108class Greatest(Func):
6109    arg_types = {"this": True, "expressions": False}
6110    is_var_len_args = True
6111
6112
6113# Trino's `ON OVERFLOW TRUNCATE [filler_string] {WITH | WITHOUT} COUNT`
6114# https://trino.io/docs/current/functions/aggregate.html#listagg
6115class OverflowTruncateBehavior(Expression):
6116    arg_types = {"this": False, "with_count": True}
6117
6118
6119class GroupConcat(AggFunc):
6120    arg_types = {"this": True, "separator": False, "on_overflow": False}
6121
6122
6123class Hex(Func):
6124    pass
6125
6126
6127class LowerHex(Hex):
6128    pass
6129
6130
6131class And(Connector, Func):
6132    pass
6133
6134
6135class Or(Connector, Func):
6136    pass
6137
6138
6139class Xor(Connector, Func):
6140    arg_types = {"this": False, "expression": False, "expressions": False}
6141
6142
6143class If(Func):
6144    arg_types = {"this": True, "true": True, "false": False}
6145    _sql_names = ["IF", "IIF"]
6146
6147
6148class Nullif(Func):
6149    arg_types = {"this": True, "expression": True}
6150
6151
6152class Initcap(Func):
6153    arg_types = {"this": True, "expression": False}
6154
6155
6156class IsAscii(Func):
6157    pass
6158
6159
6160class IsNan(Func):
6161    _sql_names = ["IS_NAN", "ISNAN"]
6162
6163
6164# https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#int64_for_json
6165class Int64(Func):
6166    pass
6167
6168
6169class IsInf(Func):
6170    _sql_names = ["IS_INF", "ISINF"]
6171
6172
6173# https://www.postgresql.org/docs/current/functions-json.html
6174class JSON(Expression):
6175    arg_types = {"this": False, "with": False, "unique": False}
6176
6177
6178class JSONPath(Expression):
6179    arg_types = {"expressions": True, "escape": False}
6180
6181    @property
6182    def output_name(self) -> str:
6183        last_segment = self.expressions[-1].this
6184        return last_segment if isinstance(last_segment, str) else ""
6185
6186
6187class JSONPathPart(Expression):
6188    arg_types = {}
6189
6190
6191class JSONPathFilter(JSONPathPart):
6192    arg_types = {"this": True}
6193
6194
6195class JSONPathKey(JSONPathPart):
6196    arg_types = {"this": True}
6197
6198
6199class JSONPathRecursive(JSONPathPart):
6200    arg_types = {"this": False}
6201
6202
6203class JSONPathRoot(JSONPathPart):
6204    pass
6205
6206
6207class JSONPathScript(JSONPathPart):
6208    arg_types = {"this": True}
6209
6210
6211class JSONPathSlice(JSONPathPart):
6212    arg_types = {"start": False, "end": False, "step": False}
6213
6214
6215class JSONPathSelector(JSONPathPart):
6216    arg_types = {"this": True}
6217
6218
6219class JSONPathSubscript(JSONPathPart):
6220    arg_types = {"this": True}
6221
6222
6223class JSONPathUnion(JSONPathPart):
6224    arg_types = {"expressions": True}
6225
6226
6227class JSONPathWildcard(JSONPathPart):
6228    pass
6229
6230
6231class FormatJson(Expression):
6232    pass
6233
6234
6235class JSONKeyValue(Expression):
6236    arg_types = {"this": True, "expression": True}
6237
6238
6239class JSONObject(Func):
6240    arg_types = {
6241        "expressions": False,
6242        "null_handling": False,
6243        "unique_keys": False,
6244        "return_type": False,
6245        "encoding": False,
6246    }
6247
6248
6249class JSONObjectAgg(AggFunc):
6250    arg_types = {
6251        "expressions": False,
6252        "null_handling": False,
6253        "unique_keys": False,
6254        "return_type": False,
6255        "encoding": False,
6256    }
6257
6258
6259# https://www.postgresql.org/docs/9.5/functions-aggregate.html
6260class JSONBObjectAgg(AggFunc):
6261    arg_types = {"this": True, "expression": True}
6262
6263
6264# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAY.html
6265class JSONArray(Func):
6266    arg_types = {
6267        "expressions": True,
6268        "null_handling": False,
6269        "return_type": False,
6270        "strict": False,
6271    }
6272
6273
6274# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAYAGG.html
6275class JSONArrayAgg(Func):
6276    arg_types = {
6277        "this": True,
6278        "order": False,
6279        "null_handling": False,
6280        "return_type": False,
6281        "strict": False,
6282    }
6283
6284
6285class JSONExists(Func):
6286    arg_types = {"this": True, "path": True, "passing": False, "on_condition": False}
6287
6288
6289# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_TABLE.html
6290# Note: parsing of JSON column definitions is currently incomplete.
6291class JSONColumnDef(Expression):
6292    arg_types = {"this": False, "kind": False, "path": False, "nested_schema": False}
6293
6294
6295class JSONSchema(Expression):
6296    arg_types = {"expressions": True}
6297
6298
6299# https://dev.mysql.com/doc/refman/8.4/en/json-search-functions.html#function_json-value
6300class JSONValue(Expression):
6301    arg_types = {
6302        "this": True,
6303        "path": True,
6304        "returning": False,
6305        "on_condition": False,
6306    }
6307
6308
6309class JSONValueArray(Func):
6310    arg_types = {"this": True, "expression": False}
6311
6312
6313# # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_TABLE.html
6314class JSONTable(Func):
6315    arg_types = {
6316        "this": True,
6317        "schema": True,
6318        "path": False,
6319        "error_handling": False,
6320        "empty_handling": False,
6321    }
6322
6323
6324# https://docs.snowflake.com/en/sql-reference/functions/object_insert
6325class ObjectInsert(Func):
6326    arg_types = {
6327        "this": True,
6328        "key": True,
6329        "value": True,
6330        "update_flag": False,
6331    }
6332
6333
6334class OpenJSONColumnDef(Expression):
6335    arg_types = {"this": True, "kind": True, "path": False, "as_json": False}
6336
6337
6338class OpenJSON(Func):
6339    arg_types = {"this": True, "path": False, "expressions": False}
6340
6341
6342class JSONBContains(Binary, Func):
6343    _sql_names = ["JSONB_CONTAINS"]
6344
6345
6346class JSONBExists(Func):
6347    arg_types = {"this": True, "path": True}
6348    _sql_names = ["JSONB_EXISTS"]
6349
6350
6351class JSONExtract(Binary, Func):
6352    arg_types = {
6353        "this": True,
6354        "expression": True,
6355        "only_json_types": False,
6356        "expressions": False,
6357        "variant_extract": False,
6358        "json_query": False,
6359        "option": False,
6360        "quote": False,
6361        "on_condition": False,
6362    }
6363    _sql_names = ["JSON_EXTRACT"]
6364    is_var_len_args = True
6365
6366    @property
6367    def output_name(self) -> str:
6368        return self.expression.output_name if not self.expressions else ""
6369
6370
6371# https://trino.io/docs/current/functions/json.html#json-query
6372class JSONExtractQuote(Expression):
6373    arg_types = {
6374        "option": True,
6375        "scalar": False,
6376    }
6377
6378
6379class JSONExtractArray(Func):
6380    arg_types = {"this": True, "expression": False}
6381    _sql_names = ["JSON_EXTRACT_ARRAY"]
6382
6383
6384class JSONExtractScalar(Binary, Func):
6385    arg_types = {"this": True, "expression": True, "only_json_types": False, "expressions": False}
6386    _sql_names = ["JSON_EXTRACT_SCALAR"]
6387    is_var_len_args = True
6388
6389    @property
6390    def output_name(self) -> str:
6391        return self.expression.output_name
6392
6393
6394class JSONBExtract(Binary, Func):
6395    _sql_names = ["JSONB_EXTRACT"]
6396
6397
6398class JSONBExtractScalar(Binary, Func):
6399    _sql_names = ["JSONB_EXTRACT_SCALAR"]
6400
6401
6402class JSONFormat(Func):
6403    arg_types = {"this": False, "options": False, "is_json": False}
6404    _sql_names = ["JSON_FORMAT"]
6405
6406
6407# https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_member-of
6408class JSONArrayContains(Binary, Predicate, Func):
6409    _sql_names = ["JSON_ARRAY_CONTAINS"]
6410
6411
6412class ParseJSON(Func):
6413    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
6414    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
6415    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
6416    arg_types = {"this": True, "expression": False, "safe": False}
6417
6418
6419class Least(Func):
6420    arg_types = {"this": True, "expressions": False}
6421    is_var_len_args = True
6422
6423
6424class Left(Func):
6425    arg_types = {"this": True, "expression": True}
6426
6427
6428class Right(Func):
6429    arg_types = {"this": True, "expression": True}
6430
6431
6432class Length(Func):
6433    arg_types = {"this": True, "binary": False, "encoding": False}
6434    _sql_names = ["LENGTH", "LEN", "CHAR_LENGTH", "CHARACTER_LENGTH"]
6435
6436
6437class Levenshtein(Func):
6438    arg_types = {
6439        "this": True,
6440        "expression": False,
6441        "ins_cost": False,
6442        "del_cost": False,
6443        "sub_cost": False,
6444        "max_dist": False,
6445    }
6446
6447
6448class Ln(Func):
6449    pass
6450
6451
6452class Log(Func):
6453    arg_types = {"this": True, "expression": False}
6454
6455
6456class LogicalOr(AggFunc):
6457    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
6458
6459
6460class LogicalAnd(AggFunc):
6461    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
6462
6463
6464class Lower(Func):
6465    _sql_names = ["LOWER", "LCASE"]
6466
6467
6468class Map(Func):
6469    arg_types = {"keys": False, "values": False}
6470
6471    @property
6472    def keys(self) -> t.List[Expression]:
6473        keys = self.args.get("keys")
6474        return keys.expressions if keys else []
6475
6476    @property
6477    def values(self) -> t.List[Expression]:
6478        values = self.args.get("values")
6479        return values.expressions if values else []
6480
6481
6482# Represents the MAP {...} syntax in DuckDB - basically convert a struct to a MAP
6483class ToMap(Func):
6484    pass
6485
6486
6487class MapFromEntries(Func):
6488    pass
6489
6490
6491# https://learn.microsoft.com/en-us/sql/t-sql/language-elements/scope-resolution-operator-transact-sql?view=sql-server-ver16
6492class ScopeResolution(Expression):
6493    arg_types = {"this": False, "expression": True}
6494
6495
6496class Stream(Expression):
6497    pass
6498
6499
6500class StarMap(Func):
6501    pass
6502
6503
6504class VarMap(Func):
6505    arg_types = {"keys": True, "values": True}
6506    is_var_len_args = True
6507
6508    @property
6509    def keys(self) -> t.List[Expression]:
6510        return self.args["keys"].expressions
6511
6512    @property
6513    def values(self) -> t.List[Expression]:
6514        return self.args["values"].expressions
6515
6516
6517# https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html
6518class MatchAgainst(Func):
6519    arg_types = {"this": True, "expressions": True, "modifier": False}
6520
6521
6522class Max(AggFunc):
6523    arg_types = {"this": True, "expressions": False}
6524    is_var_len_args = True
6525
6526
6527class MD5(Func):
6528    _sql_names = ["MD5"]
6529
6530
6531# Represents the variant of the MD5 function that returns a binary value
6532class MD5Digest(Func):
6533    _sql_names = ["MD5_DIGEST"]
6534
6535
6536class Median(AggFunc):
6537    pass
6538
6539
6540class Min(AggFunc):
6541    arg_types = {"this": True, "expressions": False}
6542    is_var_len_args = True
6543
6544
6545class Month(Func):
6546    pass
6547
6548
6549class AddMonths(Func):
6550    arg_types = {"this": True, "expression": True}
6551
6552
6553class Nvl2(Func):
6554    arg_types = {"this": True, "true": True, "false": False}
6555
6556
6557class Normalize(Func):
6558    arg_types = {"this": True, "form": False}
6559
6560
6561class Overlay(Func):
6562    arg_types = {"this": True, "expression": True, "from": True, "for": False}
6563
6564
6565# https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-predict#mlpredict_function
6566class Predict(Func):
6567    arg_types = {"this": True, "expression": True, "params_struct": False}
6568
6569
6570class Pow(Binary, Func):
6571    _sql_names = ["POWER", "POW"]
6572
6573
6574class PercentileCont(AggFunc):
6575    arg_types = {"this": True, "expression": False}
6576
6577
6578class PercentileDisc(AggFunc):
6579    arg_types = {"this": True, "expression": False}
6580
6581
6582class Quantile(AggFunc):
6583    arg_types = {"this": True, "quantile": True}
6584
6585
6586class ApproxQuantile(Quantile):
6587    arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
6588
6589
6590class Quarter(Func):
6591    pass
6592
6593
6594# https://docs.teradata.com/r/Enterprise_IntelliFlex_VMware/SQL-Functions-Expressions-and-Predicates/Arithmetic-Trigonometric-Hyperbolic-Operators/Functions/RANDOM/RANDOM-Function-Syntax
6595# teradata lower and upper bounds
6596class Rand(Func):
6597    _sql_names = ["RAND", "RANDOM"]
6598    arg_types = {"this": False, "lower": False, "upper": False}
6599
6600
6601class Randn(Func):
6602    arg_types = {"this": False}
6603
6604
6605class RangeN(Func):
6606    arg_types = {"this": True, "expressions": True, "each": False}
6607
6608
6609class ReadCSV(Func):
6610    _sql_names = ["READ_CSV"]
6611    is_var_len_args = True
6612    arg_types = {"this": True, "expressions": False}
6613
6614
6615class Reduce(Func):
6616    arg_types = {"this": True, "initial": True, "merge": True, "finish": False}
6617
6618
6619class RegexpExtract(Func):
6620    arg_types = {
6621        "this": True,
6622        "expression": True,
6623        "position": False,
6624        "occurrence": False,
6625        "parameters": False,
6626        "group": False,
6627    }
6628
6629
6630class RegexpExtractAll(Func):
6631    arg_types = {
6632        "this": True,
6633        "expression": True,
6634        "position": False,
6635        "occurrence": False,
6636        "parameters": False,
6637        "group": False,
6638    }
6639
6640
6641class RegexpReplace(Func):
6642    arg_types = {
6643        "this": True,
6644        "expression": True,
6645        "replacement": False,
6646        "position": False,
6647        "occurrence": False,
6648        "modifiers": False,
6649    }
6650
6651
6652class RegexpLike(Binary, Func):
6653    arg_types = {"this": True, "expression": True, "flag": False}
6654
6655
6656class RegexpILike(Binary, Func):
6657    arg_types = {"this": True, "expression": True, "flag": False}
6658
6659
6660# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split.html
6661# limit is the number of times a pattern is applied
6662class RegexpSplit(Func):
6663    arg_types = {"this": True, "expression": True, "limit": False}
6664
6665
6666class Repeat(Func):
6667    arg_types = {"this": True, "times": True}
6668
6669
6670# https://learn.microsoft.com/en-us/sql/t-sql/functions/round-transact-sql?view=sql-server-ver16
6671# tsql third argument function == trunctaion if not 0
6672class Round(Func):
6673    arg_types = {"this": True, "decimals": False, "truncate": False}
6674
6675
6676class RowNumber(Func):
6677    arg_types = {"this": False}
6678
6679
6680class SafeDivide(Func):
6681    arg_types = {"this": True, "expression": True}
6682
6683
6684class SHA(Func):
6685    _sql_names = ["SHA", "SHA1"]
6686
6687
6688class SHA2(Func):
6689    _sql_names = ["SHA2"]
6690    arg_types = {"this": True, "length": False}
6691
6692
6693class Sign(Func):
6694    _sql_names = ["SIGN", "SIGNUM"]
6695
6696
6697class SortArray(Func):
6698    arg_types = {"this": True, "asc": False}
6699
6700
6701class Split(Func):
6702    arg_types = {"this": True, "expression": True, "limit": False}
6703
6704
6705# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split_part.html
6706class SplitPart(Func):
6707    arg_types = {"this": True, "delimiter": True, "part_index": True}
6708
6709
6710# Start may be omitted in the case of postgres
6711# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6
6712class Substring(Func):
6713    _sql_names = ["SUBSTRING", "SUBSTR"]
6714    arg_types = {"this": True, "start": False, "length": False}
6715
6716
6717class StandardHash(Func):
6718    arg_types = {"this": True, "expression": False}
6719
6720
6721class StartsWith(Func):
6722    _sql_names = ["STARTS_WITH", "STARTSWITH"]
6723    arg_types = {"this": True, "expression": True}
6724
6725
6726class EndsWith(Func):
6727    _sql_names = ["ENDS_WITH", "ENDSWITH"]
6728    arg_types = {"this": True, "expression": True}
6729
6730
6731class StrPosition(Func):
6732    arg_types = {
6733        "this": True,
6734        "substr": True,
6735        "position": False,
6736        "occurrence": False,
6737    }
6738
6739
6740class StrToDate(Func):
6741    arg_types = {"this": True, "format": False, "safe": False}
6742
6743
6744class StrToTime(Func):
6745    arg_types = {"this": True, "format": True, "zone": False, "safe": False}
6746
6747
6748# Spark allows unix_timestamp()
6749# https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.unix_timestamp.html
6750class StrToUnix(Func):
6751    arg_types = {"this": False, "format": False}
6752
6753
6754# https://prestodb.io/docs/current/functions/string.html
6755# https://spark.apache.org/docs/latest/api/sql/index.html#str_to_map
6756class StrToMap(Func):
6757    arg_types = {
6758        "this": True,
6759        "pair_delim": False,
6760        "key_value_delim": False,
6761        "duplicate_resolution_callback": False,
6762    }
6763
6764
6765class NumberToStr(Func):
6766    arg_types = {"this": True, "format": True, "culture": False}
6767
6768
6769class FromBase(Func):
6770    arg_types = {"this": True, "expression": True}
6771
6772
6773class Struct(Func):
6774    arg_types = {"expressions": False}
6775    is_var_len_args = True
6776
6777
6778class StructExtract(Func):
6779    arg_types = {"this": True, "expression": True}
6780
6781
6782# https://learn.microsoft.com/en-us/sql/t-sql/functions/stuff-transact-sql?view=sql-server-ver16
6783# https://docs.snowflake.com/en/sql-reference/functions/insert
6784class Stuff(Func):
6785    _sql_names = ["STUFF", "INSERT"]
6786    arg_types = {"this": True, "start": True, "length": True, "expression": True}
6787
6788
6789class Sum(AggFunc):
6790    pass
6791
6792
6793class Sqrt(Func):
6794    pass
6795
6796
6797class Stddev(AggFunc):
6798    _sql_names = ["STDDEV", "STDEV"]
6799
6800
6801class StddevPop(AggFunc):
6802    pass
6803
6804
6805class StddevSamp(AggFunc):
6806    pass
6807
6808
6809# https://cloud.google.com/bigquery/docs/reference/standard-sql/time_functions#time
6810class Time(Func):
6811    arg_types = {"this": False, "zone": False}
6812
6813
6814class TimeToStr(Func):
6815    arg_types = {"this": True, "format": True, "culture": False, "zone": False}
6816
6817
6818class TimeToTimeStr(Func):
6819    pass
6820
6821
6822class TimeToUnix(Func):
6823    pass
6824
6825
6826class TimeStrToDate(Func):
6827    pass
6828
6829
6830class TimeStrToTime(Func):
6831    arg_types = {"this": True, "zone": False}
6832
6833
6834class TimeStrToUnix(Func):
6835    pass
6836
6837
6838class Trim(Func):
6839    arg_types = {
6840        "this": True,
6841        "expression": False,
6842        "position": False,
6843        "collation": False,
6844    }
6845
6846
6847class TsOrDsAdd(Func, TimeUnit):
6848    # return_type is used to correctly cast the arguments of this expression when transpiling it
6849    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
6850
6851    @property
6852    def return_type(self) -> DataType:
6853        return DataType.build(self.args.get("return_type") or DataType.Type.DATE)
6854
6855
6856class TsOrDsDiff(Func, TimeUnit):
6857    arg_types = {"this": True, "expression": True, "unit": False}
6858
6859
6860class TsOrDsToDateStr(Func):
6861    pass
6862
6863
6864class TsOrDsToDate(Func):
6865    arg_types = {"this": True, "format": False, "safe": False}
6866
6867
6868class TsOrDsToDatetime(Func):
6869    pass
6870
6871
6872class TsOrDsToTime(Func):
6873    arg_types = {"this": True, "format": False, "safe": False}
6874
6875
6876class TsOrDsToTimestamp(Func):
6877    pass
6878
6879
6880class TsOrDiToDi(Func):
6881    pass
6882
6883
6884class Unhex(Func):
6885    arg_types = {"this": True, "expression": False}
6886
6887
6888class Unicode(Func):
6889    pass
6890
6891
6892# https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#unix_date
6893class UnixDate(Func):
6894    pass
6895
6896
6897class UnixToStr(Func):
6898    arg_types = {"this": True, "format": False}
6899
6900
6901# https://prestodb.io/docs/current/functions/datetime.html
6902# presto has weird zone/hours/minutes
6903class UnixToTime(Func):
6904    arg_types = {
6905        "this": True,
6906        "scale": False,
6907        "zone": False,
6908        "hours": False,
6909        "minutes": False,
6910        "format": False,
6911    }
6912
6913    SECONDS = Literal.number(0)
6914    DECIS = Literal.number(1)
6915    CENTIS = Literal.number(2)
6916    MILLIS = Literal.number(3)
6917    DECIMILLIS = Literal.number(4)
6918    CENTIMILLIS = Literal.number(5)
6919    MICROS = Literal.number(6)
6920    DECIMICROS = Literal.number(7)
6921    CENTIMICROS = Literal.number(8)
6922    NANOS = Literal.number(9)
6923
6924
6925class UnixToTimeStr(Func):
6926    pass
6927
6928
6929class UnixSeconds(Func):
6930    pass
6931
6932
6933class Uuid(Func):
6934    _sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "UUID_STRING"]
6935
6936    arg_types = {"this": False, "name": False}
6937
6938
6939class TimestampFromParts(Func):
6940    _sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
6941    arg_types = {
6942        "year": True,
6943        "month": True,
6944        "day": True,
6945        "hour": True,
6946        "min": True,
6947        "sec": True,
6948        "nano": False,
6949        "zone": False,
6950        "milli": False,
6951    }
6952
6953
6954class Upper(Func):
6955    _sql_names = ["UPPER", "UCASE"]
6956
6957
6958class Corr(Binary, AggFunc):
6959    pass
6960
6961
6962class Variance(AggFunc):
6963    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
6964
6965
6966class VariancePop(AggFunc):
6967    _sql_names = ["VARIANCE_POP", "VAR_POP"]
6968
6969
6970class CovarSamp(Binary, AggFunc):
6971    pass
6972
6973
6974class CovarPop(Binary, AggFunc):
6975    pass
6976
6977
6978class Week(Func):
6979    arg_types = {"this": True, "mode": False}
6980
6981
6982class XMLElement(Func):
6983    _sql_names = ["XMLELEMENT"]
6984    arg_types = {"this": True, "expressions": False}
6985
6986
6987class XMLTable(Func):
6988    arg_types = {
6989        "this": True,
6990        "namespaces": False,
6991        "passing": False,
6992        "columns": False,
6993        "by_ref": False,
6994    }
6995
6996
6997class XMLNamespace(Expression):
6998    pass
6999
7000
7001# https://learn.microsoft.com/en-us/sql/t-sql/queries/select-for-clause-transact-sql?view=sql-server-ver17#syntax
7002class XMLKeyValueOption(Expression):
7003    arg_types = {"this": True, "expression": False}
7004
7005
7006class Year(Func):
7007    pass
7008
7009
7010class Use(Expression):
7011    arg_types = {"this": False, "expressions": False, "kind": False}
7012
7013
7014class Merge(DML):
7015    arg_types = {
7016        "this": True,
7017        "using": True,
7018        "on": True,
7019        "whens": True,
7020        "with": False,
7021        "returning": False,
7022    }
7023
7024
7025class When(Expression):
7026    arg_types = {"matched": True, "source": False, "condition": False, "then": True}
7027
7028
7029class Whens(Expression):
7030    """Wraps around one or more WHEN [NOT] MATCHED [...] clauses."""
7031
7032    arg_types = {"expressions": True}
7033
7034
7035# https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqljnextvaluefor.html
7036# https://learn.microsoft.com/en-us/sql/t-sql/functions/next-value-for-transact-sql?view=sql-server-ver16
7037class NextValueFor(Func):
7038    arg_types = {"this": True, "order": False}
7039
7040
7041# Refers to a trailing semi-colon. This is only used to preserve trailing comments
7042# select 1; -- my comment
7043class Semicolon(Expression):
7044    arg_types = {}
7045
7046
7047def _norm_arg(arg):
7048    return arg.lower() if type(arg) is str else arg
7049
7050
7051ALL_FUNCTIONS = subclasses(__name__, Func, (AggFunc, Anonymous, Func))
7052FUNCTION_BY_NAME = {name: func for func in ALL_FUNCTIONS for name in func.sql_names()}
7053
7054JSON_PATH_PARTS = subclasses(__name__, JSONPathPart, (JSONPathPart,))
7055
7056PERCENTILES = (PercentileCont, PercentileDisc)
7057
7058
7059# Helpers
7060@t.overload
7061def maybe_parse(
7062    sql_or_expression: ExpOrStr,
7063    *,
7064    into: t.Type[E],
7065    dialect: DialectType = None,
7066    prefix: t.Optional[str] = None,
7067    copy: bool = False,
7068    **opts,
7069) -> E: ...
7070
7071
7072@t.overload
7073def maybe_parse(
7074    sql_or_expression: str | E,
7075    *,
7076    into: t.Optional[IntoType] = None,
7077    dialect: DialectType = None,
7078    prefix: t.Optional[str] = None,
7079    copy: bool = False,
7080    **opts,
7081) -> E: ...
7082
7083
7084def maybe_parse(
7085    sql_or_expression: ExpOrStr,
7086    *,
7087    into: t.Optional[IntoType] = None,
7088    dialect: DialectType = None,
7089    prefix: t.Optional[str] = None,
7090    copy: bool = False,
7091    **opts,
7092) -> Expression:
7093    """Gracefully handle a possible string or expression.
7094
7095    Example:
7096        >>> maybe_parse("1")
7097        Literal(this=1, is_string=False)
7098        >>> maybe_parse(to_identifier("x"))
7099        Identifier(this=x, quoted=False)
7100
7101    Args:
7102        sql_or_expression: the SQL code string or an expression
7103        into: the SQLGlot Expression to parse into
7104        dialect: the dialect used to parse the input expressions (in the case that an
7105            input expression is a SQL string).
7106        prefix: a string to prefix the sql with before it gets parsed
7107            (automatically includes a space)
7108        copy: whether to copy the expression.
7109        **opts: other options to use to parse the input expressions (again, in the case
7110            that an input expression is a SQL string).
7111
7112    Returns:
7113        Expression: the parsed or given expression.
7114    """
7115    if isinstance(sql_or_expression, Expression):
7116        if copy:
7117            return sql_or_expression.copy()
7118        return sql_or_expression
7119
7120    if sql_or_expression is None:
7121        raise ParseError("SQL cannot be None")
7122
7123    import sqlglot
7124
7125    sql = str(sql_or_expression)
7126    if prefix:
7127        sql = f"{prefix} {sql}"
7128
7129    return sqlglot.parse_one(sql, read=dialect, into=into, **opts)
7130
7131
7132@t.overload
7133def maybe_copy(instance: None, copy: bool = True) -> None: ...
7134
7135
7136@t.overload
7137def maybe_copy(instance: E, copy: bool = True) -> E: ...
7138
7139
7140def maybe_copy(instance, copy=True):
7141    return instance.copy() if copy and instance else instance
7142
7143
7144def _to_s(node: t.Any, verbose: bool = False, level: int = 0, repr_str: bool = False) -> str:
7145    """Generate a textual representation of an Expression tree"""
7146    indent = "\n" + ("  " * (level + 1))
7147    delim = f",{indent}"
7148
7149    if isinstance(node, Expression):
7150        args = {k: v for k, v in node.args.items() if (v is not None and v != []) or verbose}
7151
7152        if (node.type or verbose) and not isinstance(node, DataType):
7153            args["_type"] = node.type
7154        if node.comments or verbose:
7155            args["_comments"] = node.comments
7156
7157        if verbose:
7158            args["_id"] = id(node)
7159
7160        # Inline leaves for a more compact representation
7161        if node.is_leaf():
7162            indent = ""
7163            delim = ", "
7164
7165        repr_str = node.is_string or (isinstance(node, Identifier) and node.quoted)
7166        items = delim.join(
7167            [f"{k}={_to_s(v, verbose, level + 1, repr_str=repr_str)}" for k, v in args.items()]
7168        )
7169        return f"{node.__class__.__name__}({indent}{items})"
7170
7171    if isinstance(node, list):
7172        items = delim.join(_to_s(i, verbose, level + 1) for i in node)
7173        items = f"{indent}{items}" if items else ""
7174        return f"[{items}]"
7175
7176    # We use the representation of the string to avoid stripping out important whitespace
7177    if repr_str and isinstance(node, str):
7178        node = repr(node)
7179
7180    # Indent multiline strings to match the current level
7181    return indent.join(textwrap.dedent(str(node).strip("\n")).splitlines())
7182
7183
7184def _is_wrong_expression(expression, into):
7185    return isinstance(expression, Expression) and not isinstance(expression, into)
7186
7187
7188def _apply_builder(
7189    expression,
7190    instance,
7191    arg,
7192    copy=True,
7193    prefix=None,
7194    into=None,
7195    dialect=None,
7196    into_arg="this",
7197    **opts,
7198):
7199    if _is_wrong_expression(expression, into):
7200        expression = into(**{into_arg: expression})
7201    instance = maybe_copy(instance, copy)
7202    expression = maybe_parse(
7203        sql_or_expression=expression,
7204        prefix=prefix,
7205        into=into,
7206        dialect=dialect,
7207        **opts,
7208    )
7209    instance.set(arg, expression)
7210    return instance
7211
7212
7213def _apply_child_list_builder(
7214    *expressions,
7215    instance,
7216    arg,
7217    append=True,
7218    copy=True,
7219    prefix=None,
7220    into=None,
7221    dialect=None,
7222    properties=None,
7223    **opts,
7224):
7225    instance = maybe_copy(instance, copy)
7226    parsed = []
7227    properties = {} if properties is None else properties
7228
7229    for expression in expressions:
7230        if expression is not None:
7231            if _is_wrong_expression(expression, into):
7232                expression = into(expressions=[expression])
7233
7234            expression = maybe_parse(
7235                expression,
7236                into=into,
7237                dialect=dialect,
7238                prefix=prefix,
7239                **opts,
7240            )
7241            for k, v in expression.args.items():
7242                if k == "expressions":
7243                    parsed.extend(v)
7244                else:
7245                    properties[k] = v
7246
7247    existing = instance.args.get(arg)
7248    if append and existing:
7249        parsed = existing.expressions + parsed
7250
7251    child = into(expressions=parsed)
7252    for k, v in properties.items():
7253        child.set(k, v)
7254    instance.set(arg, child)
7255
7256    return instance
7257
7258
7259def _apply_list_builder(
7260    *expressions,
7261    instance,
7262    arg,
7263    append=True,
7264    copy=True,
7265    prefix=None,
7266    into=None,
7267    dialect=None,
7268    **opts,
7269):
7270    inst = maybe_copy(instance, copy)
7271
7272    expressions = [
7273        maybe_parse(
7274            sql_or_expression=expression,
7275            into=into,
7276            prefix=prefix,
7277            dialect=dialect,
7278            **opts,
7279        )
7280        for expression in expressions
7281        if expression is not None
7282    ]
7283
7284    existing_expressions = inst.args.get(arg)
7285    if append and existing_expressions:
7286        expressions = existing_expressions + expressions
7287
7288    inst.set(arg, expressions)
7289    return inst
7290
7291
7292def _apply_conjunction_builder(
7293    *expressions,
7294    instance,
7295    arg,
7296    into=None,
7297    append=True,
7298    copy=True,
7299    dialect=None,
7300    **opts,
7301):
7302    expressions = [exp for exp in expressions if exp is not None and exp != ""]
7303    if not expressions:
7304        return instance
7305
7306    inst = maybe_copy(instance, copy)
7307
7308    existing = inst.args.get(arg)
7309    if append and existing is not None:
7310        expressions = [existing.this if into else existing] + list(expressions)
7311
7312    node = and_(*expressions, dialect=dialect, copy=copy, **opts)
7313
7314    inst.set(arg, into(this=node) if into else node)
7315    return inst
7316
7317
7318def _apply_cte_builder(
7319    instance: E,
7320    alias: ExpOrStr,
7321    as_: ExpOrStr,
7322    recursive: t.Optional[bool] = None,
7323    materialized: t.Optional[bool] = None,
7324    append: bool = True,
7325    dialect: DialectType = None,
7326    copy: bool = True,
7327    scalar: bool = False,
7328    **opts,
7329) -> E:
7330    alias_expression = maybe_parse(alias, dialect=dialect, into=TableAlias, **opts)
7331    as_expression = maybe_parse(as_, dialect=dialect, copy=copy, **opts)
7332    if scalar and not isinstance(as_expression, Subquery):
7333        # scalar CTE must be wrapped in a subquery
7334        as_expression = Subquery(this=as_expression)
7335    cte = CTE(this=as_expression, alias=alias_expression, materialized=materialized, scalar=scalar)
7336    return _apply_child_list_builder(
7337        cte,
7338        instance=instance,
7339        arg="with",
7340        append=append,
7341        copy=copy,
7342        into=With,
7343        properties={"recursive": recursive or False},
7344    )
7345
7346
7347def _combine(
7348    expressions: t.Sequence[t.Optional[ExpOrStr]],
7349    operator: t.Type[Connector],
7350    dialect: DialectType = None,
7351    copy: bool = True,
7352    wrap: bool = True,
7353    **opts,
7354) -> Expression:
7355    conditions = [
7356        condition(expression, dialect=dialect, copy=copy, **opts)
7357        for expression in expressions
7358        if expression is not None
7359    ]
7360
7361    this, *rest = conditions
7362    if rest and wrap:
7363        this = _wrap(this, Connector)
7364    for expression in rest:
7365        this = operator(this=this, expression=_wrap(expression, Connector) if wrap else expression)
7366
7367    return this
7368
7369
7370@t.overload
7371def _wrap(expression: None, kind: t.Type[Expression]) -> None: ...
7372
7373
7374@t.overload
7375def _wrap(expression: E, kind: t.Type[Expression]) -> E | Paren: ...
7376
7377
7378def _wrap(expression: t.Optional[E], kind: t.Type[Expression]) -> t.Optional[E] | Paren:
7379    return Paren(this=expression) if isinstance(expression, kind) else expression
7380
7381
7382def _apply_set_operation(
7383    *expressions: ExpOrStr,
7384    set_operation: t.Type[S],
7385    distinct: bool = True,
7386    dialect: DialectType = None,
7387    copy: bool = True,
7388    **opts,
7389) -> S:
7390    return reduce(
7391        lambda x, y: set_operation(this=x, expression=y, distinct=distinct, **opts),
7392        (maybe_parse(e, dialect=dialect, copy=copy, **opts) for e in expressions),
7393    )
7394
7395
7396def union(
7397    *expressions: ExpOrStr,
7398    distinct: bool = True,
7399    dialect: DialectType = None,
7400    copy: bool = True,
7401    **opts,
7402) -> Union:
7403    """
7404    Initializes a syntax tree for the `UNION` operation.
7405
7406    Example:
7407        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
7408        'SELECT * FROM foo UNION SELECT * FROM bla'
7409
7410    Args:
7411        expressions: the SQL code strings, corresponding to the `UNION`'s operands.
7412            If `Expression` instances are passed, they will be used as-is.
7413        distinct: set the DISTINCT flag if and only if this is true.
7414        dialect: the dialect used to parse the input expression.
7415        copy: whether to copy the expression.
7416        opts: other options to use to parse the input expressions.
7417
7418    Returns:
7419        The new Union instance.
7420    """
7421    assert len(expressions) >= 2, "At least two expressions are required by `union`."
7422    return _apply_set_operation(
7423        *expressions, set_operation=Union, distinct=distinct, dialect=dialect, copy=copy, **opts
7424    )
7425
7426
7427def intersect(
7428    *expressions: ExpOrStr,
7429    distinct: bool = True,
7430    dialect: DialectType = None,
7431    copy: bool = True,
7432    **opts,
7433) -> Intersect:
7434    """
7435    Initializes a syntax tree for the `INTERSECT` operation.
7436
7437    Example:
7438        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
7439        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
7440
7441    Args:
7442        expressions: the SQL code strings, corresponding to the `INTERSECT`'s operands.
7443            If `Expression` instances are passed, they will be used as-is.
7444        distinct: set the DISTINCT flag if and only if this is true.
7445        dialect: the dialect used to parse the input expression.
7446        copy: whether to copy the expression.
7447        opts: other options to use to parse the input expressions.
7448
7449    Returns:
7450        The new Intersect instance.
7451    """
7452    assert len(expressions) >= 2, "At least two expressions are required by `intersect`."
7453    return _apply_set_operation(
7454        *expressions, set_operation=Intersect, distinct=distinct, dialect=dialect, copy=copy, **opts
7455    )
7456
7457
7458def except_(
7459    *expressions: ExpOrStr,
7460    distinct: bool = True,
7461    dialect: DialectType = None,
7462    copy: bool = True,
7463    **opts,
7464) -> Except:
7465    """
7466    Initializes a syntax tree for the `EXCEPT` operation.
7467
7468    Example:
7469        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
7470        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
7471
7472    Args:
7473        expressions: the SQL code strings, corresponding to the `EXCEPT`'s operands.
7474            If `Expression` instances are passed, they will be used as-is.
7475        distinct: set the DISTINCT flag if and only if this is true.
7476        dialect: the dialect used to parse the input expression.
7477        copy: whether to copy the expression.
7478        opts: other options to use to parse the input expressions.
7479
7480    Returns:
7481        The new Except instance.
7482    """
7483    assert len(expressions) >= 2, "At least two expressions are required by `except_`."
7484    return _apply_set_operation(
7485        *expressions, set_operation=Except, distinct=distinct, dialect=dialect, copy=copy, **opts
7486    )
7487
7488
7489def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7490    """
7491    Initializes a syntax tree from one or multiple SELECT expressions.
7492
7493    Example:
7494        >>> select("col1", "col2").from_("tbl").sql()
7495        'SELECT col1, col2 FROM tbl'
7496
7497    Args:
7498        *expressions: the SQL code string to parse as the expressions of a
7499            SELECT statement. If an Expression instance is passed, this is used as-is.
7500        dialect: the dialect used to parse the input expressions (in the case that an
7501            input expression is a SQL string).
7502        **opts: other options to use to parse the input expressions (again, in the case
7503            that an input expression is a SQL string).
7504
7505    Returns:
7506        Select: the syntax tree for the SELECT statement.
7507    """
7508    return Select().select(*expressions, dialect=dialect, **opts)
7509
7510
7511def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7512    """
7513    Initializes a syntax tree from a FROM expression.
7514
7515    Example:
7516        >>> from_("tbl").select("col1", "col2").sql()
7517        'SELECT col1, col2 FROM tbl'
7518
7519    Args:
7520        *expression: the SQL code string to parse as the FROM expressions of a
7521            SELECT statement. If an Expression instance is passed, this is used as-is.
7522        dialect: the dialect used to parse the input expression (in the case that the
7523            input expression is a SQL string).
7524        **opts: other options to use to parse the input expressions (again, in the case
7525            that the input expression is a SQL string).
7526
7527    Returns:
7528        Select: the syntax tree for the SELECT statement.
7529    """
7530    return Select().from_(expression, dialect=dialect, **opts)
7531
7532
7533def update(
7534    table: str | Table,
7535    properties: t.Optional[dict] = None,
7536    where: t.Optional[ExpOrStr] = None,
7537    from_: t.Optional[ExpOrStr] = None,
7538    with_: t.Optional[t.Dict[str, ExpOrStr]] = None,
7539    dialect: DialectType = None,
7540    **opts,
7541) -> Update:
7542    """
7543    Creates an update statement.
7544
7545    Example:
7546        >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz_cte", where="baz_cte.id > 1 and my_table.id = baz_cte.id", with_={"baz_cte": "SELECT id FROM foo"}).sql()
7547        "WITH baz_cte AS (SELECT id FROM foo) UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz_cte WHERE baz_cte.id > 1 AND my_table.id = baz_cte.id"
7548
7549    Args:
7550        properties: dictionary of properties to SET which are
7551            auto converted to sql objects eg None -> NULL
7552        where: sql conditional parsed into a WHERE statement
7553        from_: sql statement parsed into a FROM statement
7554        with_: dictionary of CTE aliases / select statements to include in a WITH clause.
7555        dialect: the dialect used to parse the input expressions.
7556        **opts: other options to use to parse the input expressions.
7557
7558    Returns:
7559        Update: the syntax tree for the UPDATE statement.
7560    """
7561    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
7562    if properties:
7563        update_expr.set(
7564            "expressions",
7565            [
7566                EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
7567                for k, v in properties.items()
7568            ],
7569        )
7570    if from_:
7571        update_expr.set(
7572            "from",
7573            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
7574        )
7575    if isinstance(where, Condition):
7576        where = Where(this=where)
7577    if where:
7578        update_expr.set(
7579            "where",
7580            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
7581        )
7582    if with_:
7583        cte_list = [
7584            alias_(CTE(this=maybe_parse(qry, dialect=dialect, **opts)), alias, table=True)
7585            for alias, qry in with_.items()
7586        ]
7587        update_expr.set(
7588            "with",
7589            With(expressions=cte_list),
7590        )
7591    return update_expr
7592
7593
7594def delete(
7595    table: ExpOrStr,
7596    where: t.Optional[ExpOrStr] = None,
7597    returning: t.Optional[ExpOrStr] = None,
7598    dialect: DialectType = None,
7599    **opts,
7600) -> Delete:
7601    """
7602    Builds a delete statement.
7603
7604    Example:
7605        >>> delete("my_table", where="id > 1").sql()
7606        'DELETE FROM my_table WHERE id > 1'
7607
7608    Args:
7609        where: sql conditional parsed into a WHERE statement
7610        returning: sql conditional parsed into a RETURNING statement
7611        dialect: the dialect used to parse the input expressions.
7612        **opts: other options to use to parse the input expressions.
7613
7614    Returns:
7615        Delete: the syntax tree for the DELETE statement.
7616    """
7617    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
7618    if where:
7619        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
7620    if returning:
7621        delete_expr = delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
7622    return delete_expr
7623
7624
7625def insert(
7626    expression: ExpOrStr,
7627    into: ExpOrStr,
7628    columns: t.Optional[t.Sequence[str | Identifier]] = None,
7629    overwrite: t.Optional[bool] = None,
7630    returning: t.Optional[ExpOrStr] = None,
7631    dialect: DialectType = None,
7632    copy: bool = True,
7633    **opts,
7634) -> Insert:
7635    """
7636    Builds an INSERT statement.
7637
7638    Example:
7639        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
7640        'INSERT INTO tbl VALUES (1, 2, 3)'
7641
7642    Args:
7643        expression: the sql string or expression of the INSERT statement
7644        into: the tbl to insert data to.
7645        columns: optionally the table's column names.
7646        overwrite: whether to INSERT OVERWRITE or not.
7647        returning: sql conditional parsed into a RETURNING statement
7648        dialect: the dialect used to parse the input expressions.
7649        copy: whether to copy the expression.
7650        **opts: other options to use to parse the input expressions.
7651
7652    Returns:
7653        Insert: the syntax tree for the INSERT statement.
7654    """
7655    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
7656    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
7657
7658    if columns:
7659        this = Schema(this=this, expressions=[to_identifier(c, copy=copy) for c in columns])
7660
7661    insert = Insert(this=this, expression=expr, overwrite=overwrite)
7662
7663    if returning:
7664        insert = insert.returning(returning, dialect=dialect, copy=False, **opts)
7665
7666    return insert
7667
7668
7669def merge(
7670    *when_exprs: ExpOrStr,
7671    into: ExpOrStr,
7672    using: ExpOrStr,
7673    on: ExpOrStr,
7674    returning: t.Optional[ExpOrStr] = None,
7675    dialect: DialectType = None,
7676    copy: bool = True,
7677    **opts,
7678) -> Merge:
7679    """
7680    Builds a MERGE statement.
7681
7682    Example:
7683        >>> merge("WHEN MATCHED THEN UPDATE SET col1 = source_table.col1",
7684        ...       "WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)",
7685        ...       into="my_table",
7686        ...       using="source_table",
7687        ...       on="my_table.id = source_table.id").sql()
7688        'MERGE INTO my_table USING source_table ON my_table.id = source_table.id WHEN MATCHED THEN UPDATE SET col1 = source_table.col1 WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)'
7689
7690    Args:
7691        *when_exprs: The WHEN clauses specifying actions for matched and unmatched rows.
7692        into: The target table to merge data into.
7693        using: The source table to merge data from.
7694        on: The join condition for the merge.
7695        returning: The columns to return from the merge.
7696        dialect: The dialect used to parse the input expressions.
7697        copy: Whether to copy the expression.
7698        **opts: Other options to use to parse the input expressions.
7699
7700    Returns:
7701        Merge: The syntax tree for the MERGE statement.
7702    """
7703    expressions: t.List[Expression] = []
7704    for when_expr in when_exprs:
7705        expression = maybe_parse(when_expr, dialect=dialect, copy=copy, into=Whens, **opts)
7706        expressions.extend([expression] if isinstance(expression, When) else expression.expressions)
7707
7708    merge = Merge(
7709        this=maybe_parse(into, dialect=dialect, copy=copy, **opts),
7710        using=maybe_parse(using, dialect=dialect, copy=copy, **opts),
7711        on=maybe_parse(on, dialect=dialect, copy=copy, **opts),
7712        whens=Whens(expressions=expressions),
7713    )
7714    if returning:
7715        merge = merge.returning(returning, dialect=dialect, copy=False, **opts)
7716
7717    return merge
7718
7719
7720def condition(
7721    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
7722) -> Condition:
7723    """
7724    Initialize a logical condition expression.
7725
7726    Example:
7727        >>> condition("x=1").sql()
7728        'x = 1'
7729
7730        This is helpful for composing larger logical syntax trees:
7731        >>> where = condition("x=1")
7732        >>> where = where.and_("y=1")
7733        >>> Select().from_("tbl").select("*").where(where).sql()
7734        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
7735
7736    Args:
7737        *expression: the SQL code string to parse.
7738            If an Expression instance is passed, this is used as-is.
7739        dialect: the dialect used to parse the input expression (in the case that the
7740            input expression is a SQL string).
7741        copy: Whether to copy `expression` (only applies to expressions).
7742        **opts: other options to use to parse the input expressions (again, in the case
7743            that the input expression is a SQL string).
7744
7745    Returns:
7746        The new Condition instance
7747    """
7748    return maybe_parse(
7749        expression,
7750        into=Condition,
7751        dialect=dialect,
7752        copy=copy,
7753        **opts,
7754    )
7755
7756
7757def and_(
7758    *expressions: t.Optional[ExpOrStr],
7759    dialect: DialectType = None,
7760    copy: bool = True,
7761    wrap: bool = True,
7762    **opts,
7763) -> Condition:
7764    """
7765    Combine multiple conditions with an AND logical operator.
7766
7767    Example:
7768        >>> and_("x=1", and_("y=1", "z=1")).sql()
7769        'x = 1 AND (y = 1 AND z = 1)'
7770
7771    Args:
7772        *expressions: the SQL code strings to parse.
7773            If an Expression instance is passed, this is used as-is.
7774        dialect: the dialect used to parse the input expression.
7775        copy: whether to copy `expressions` (only applies to Expressions).
7776        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7777            precedence issues, but can be turned off when the produced AST is too deep and
7778            causes recursion-related issues.
7779        **opts: other options to use to parse the input expressions.
7780
7781    Returns:
7782        The new condition
7783    """
7784    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, wrap=wrap, **opts))
7785
7786
7787def or_(
7788    *expressions: t.Optional[ExpOrStr],
7789    dialect: DialectType = None,
7790    copy: bool = True,
7791    wrap: bool = True,
7792    **opts,
7793) -> Condition:
7794    """
7795    Combine multiple conditions with an OR logical operator.
7796
7797    Example:
7798        >>> or_("x=1", or_("y=1", "z=1")).sql()
7799        'x = 1 OR (y = 1 OR z = 1)'
7800
7801    Args:
7802        *expressions: the SQL code strings to parse.
7803            If an Expression instance is passed, this is used as-is.
7804        dialect: the dialect used to parse the input expression.
7805        copy: whether to copy `expressions` (only applies to Expressions).
7806        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7807            precedence issues, but can be turned off when the produced AST is too deep and
7808            causes recursion-related issues.
7809        **opts: other options to use to parse the input expressions.
7810
7811    Returns:
7812        The new condition
7813    """
7814    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, wrap=wrap, **opts))
7815
7816
7817def xor(
7818    *expressions: t.Optional[ExpOrStr],
7819    dialect: DialectType = None,
7820    copy: bool = True,
7821    wrap: bool = True,
7822    **opts,
7823) -> Condition:
7824    """
7825    Combine multiple conditions with an XOR logical operator.
7826
7827    Example:
7828        >>> xor("x=1", xor("y=1", "z=1")).sql()
7829        'x = 1 XOR (y = 1 XOR z = 1)'
7830
7831    Args:
7832        *expressions: the SQL code strings to parse.
7833            If an Expression instance is passed, this is used as-is.
7834        dialect: the dialect used to parse the input expression.
7835        copy: whether to copy `expressions` (only applies to Expressions).
7836        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7837            precedence issues, but can be turned off when the produced AST is too deep and
7838            causes recursion-related issues.
7839        **opts: other options to use to parse the input expressions.
7840
7841    Returns:
7842        The new condition
7843    """
7844    return t.cast(Condition, _combine(expressions, Xor, dialect, copy=copy, wrap=wrap, **opts))
7845
7846
7847def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
7848    """
7849    Wrap a condition with a NOT operator.
7850
7851    Example:
7852        >>> not_("this_suit='black'").sql()
7853        "NOT this_suit = 'black'"
7854
7855    Args:
7856        expression: the SQL code string to parse.
7857            If an Expression instance is passed, this is used as-is.
7858        dialect: the dialect used to parse the input expression.
7859        copy: whether to copy the expression or not.
7860        **opts: other options to use to parse the input expressions.
7861
7862    Returns:
7863        The new condition.
7864    """
7865    this = condition(
7866        expression,
7867        dialect=dialect,
7868        copy=copy,
7869        **opts,
7870    )
7871    return Not(this=_wrap(this, Connector))
7872
7873
7874def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
7875    """
7876    Wrap an expression in parentheses.
7877
7878    Example:
7879        >>> paren("5 + 3").sql()
7880        '(5 + 3)'
7881
7882    Args:
7883        expression: the SQL code string to parse.
7884            If an Expression instance is passed, this is used as-is.
7885        copy: whether to copy the expression or not.
7886
7887    Returns:
7888        The wrapped expression.
7889    """
7890    return Paren(this=maybe_parse(expression, copy=copy))
7891
7892
7893SAFE_IDENTIFIER_RE: t.Pattern[str] = re.compile(r"^[_a-zA-Z][\w]*$")
7894
7895
7896@t.overload
7897def to_identifier(name: None, quoted: t.Optional[bool] = None, copy: bool = True) -> None: ...
7898
7899
7900@t.overload
7901def to_identifier(
7902    name: str | Identifier, quoted: t.Optional[bool] = None, copy: bool = True
7903) -> Identifier: ...
7904
7905
7906def to_identifier(name, quoted=None, copy=True):
7907    """Builds an identifier.
7908
7909    Args:
7910        name: The name to turn into an identifier.
7911        quoted: Whether to force quote the identifier.
7912        copy: Whether to copy name if it's an Identifier.
7913
7914    Returns:
7915        The identifier ast node.
7916    """
7917
7918    if name is None:
7919        return None
7920
7921    if isinstance(name, Identifier):
7922        identifier = maybe_copy(name, copy)
7923    elif isinstance(name, str):
7924        identifier = Identifier(
7925            this=name,
7926            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
7927        )
7928    else:
7929        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
7930    return identifier
7931
7932
7933def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
7934    """
7935    Parses a given string into an identifier.
7936
7937    Args:
7938        name: The name to parse into an identifier.
7939        dialect: The dialect to parse against.
7940
7941    Returns:
7942        The identifier ast node.
7943    """
7944    try:
7945        expression = maybe_parse(name, dialect=dialect, into=Identifier)
7946    except (ParseError, TokenError):
7947        expression = to_identifier(name)
7948
7949    return expression
7950
7951
7952INTERVAL_STRING_RE = re.compile(r"\s*(-?[0-9]+(?:\.[0-9]+)?)\s*([a-zA-Z]+)\s*")
7953
7954
7955def to_interval(interval: str | Literal) -> Interval:
7956    """Builds an interval expression from a string like '1 day' or '5 months'."""
7957    if isinstance(interval, Literal):
7958        if not interval.is_string:
7959            raise ValueError("Invalid interval string.")
7960
7961        interval = interval.this
7962
7963    interval = maybe_parse(f"INTERVAL {interval}")
7964    assert isinstance(interval, Interval)
7965    return interval
7966
7967
7968def to_table(
7969    sql_path: str | Table, dialect: DialectType = None, copy: bool = True, **kwargs
7970) -> Table:
7971    """
7972    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
7973    If a table is passed in then that table is returned.
7974
7975    Args:
7976        sql_path: a `[catalog].[schema].[table]` string.
7977        dialect: the source dialect according to which the table name will be parsed.
7978        copy: Whether to copy a table if it is passed in.
7979        kwargs: the kwargs to instantiate the resulting `Table` expression with.
7980
7981    Returns:
7982        A table expression.
7983    """
7984    if isinstance(sql_path, Table):
7985        return maybe_copy(sql_path, copy=copy)
7986
7987    try:
7988        table = maybe_parse(sql_path, into=Table, dialect=dialect)
7989    except ParseError:
7990        catalog, db, this = split_num_words(sql_path, ".", 3)
7991
7992        if not this:
7993            raise
7994
7995        table = table_(this, db=db, catalog=catalog)
7996
7997    for k, v in kwargs.items():
7998        table.set(k, v)
7999
8000    return table
8001
8002
8003def to_column(
8004    sql_path: str | Column,
8005    quoted: t.Optional[bool] = None,
8006    dialect: DialectType = None,
8007    copy: bool = True,
8008    **kwargs,
8009) -> Column:
8010    """
8011    Create a column from a `[table].[column]` sql path. Table is optional.
8012    If a column is passed in then that column is returned.
8013
8014    Args:
8015        sql_path: a `[table].[column]` string.
8016        quoted: Whether or not to force quote identifiers.
8017        dialect: the source dialect according to which the column name will be parsed.
8018        copy: Whether to copy a column if it is passed in.
8019        kwargs: the kwargs to instantiate the resulting `Column` expression with.
8020
8021    Returns:
8022        A column expression.
8023    """
8024    if isinstance(sql_path, Column):
8025        return maybe_copy(sql_path, copy=copy)
8026
8027    try:
8028        col = maybe_parse(sql_path, into=Column, dialect=dialect)
8029    except ParseError:
8030        return column(*reversed(sql_path.split(".")), quoted=quoted, **kwargs)
8031
8032    for k, v in kwargs.items():
8033        col.set(k, v)
8034
8035    if quoted:
8036        for i in col.find_all(Identifier):
8037            i.set("quoted", True)
8038
8039    return col
8040
8041
8042def alias_(
8043    expression: ExpOrStr,
8044    alias: t.Optional[str | Identifier],
8045    table: bool | t.Sequence[str | Identifier] = False,
8046    quoted: t.Optional[bool] = None,
8047    dialect: DialectType = None,
8048    copy: bool = True,
8049    **opts,
8050):
8051    """Create an Alias expression.
8052
8053    Example:
8054        >>> alias_('foo', 'bar').sql()
8055        'foo AS bar'
8056
8057        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
8058        '(SELECT 1, 2) AS bar(a, b)'
8059
8060    Args:
8061        expression: the SQL code strings to parse.
8062            If an Expression instance is passed, this is used as-is.
8063        alias: the alias name to use. If the name has
8064            special characters it is quoted.
8065        table: Whether to create a table alias, can also be a list of columns.
8066        quoted: whether to quote the alias
8067        dialect: the dialect used to parse the input expression.
8068        copy: Whether to copy the expression.
8069        **opts: other options to use to parse the input expressions.
8070
8071    Returns:
8072        Alias: the aliased expression
8073    """
8074    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
8075    alias = to_identifier(alias, quoted=quoted)
8076
8077    if table:
8078        table_alias = TableAlias(this=alias)
8079        exp.set("alias", table_alias)
8080
8081        if not isinstance(table, bool):
8082            for column in table:
8083                table_alias.append("columns", to_identifier(column, quoted=quoted))
8084
8085        return exp
8086
8087    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
8088    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
8089    # for the complete Window expression.
8090    #
8091    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
8092
8093    if "alias" in exp.arg_types and not isinstance(exp, Window):
8094        exp.set("alias", alias)
8095        return exp
8096    return Alias(this=exp, alias=alias)
8097
8098
8099def subquery(
8100    expression: ExpOrStr,
8101    alias: t.Optional[Identifier | str] = None,
8102    dialect: DialectType = None,
8103    **opts,
8104) -> Select:
8105    """
8106    Build a subquery expression that's selected from.
8107
8108    Example:
8109        >>> subquery('select x from tbl', 'bar').select('x').sql()
8110        'SELECT x FROM (SELECT x FROM tbl) AS bar'
8111
8112    Args:
8113        expression: the SQL code strings to parse.
8114            If an Expression instance is passed, this is used as-is.
8115        alias: the alias name to use.
8116        dialect: the dialect used to parse the input expression.
8117        **opts: other options to use to parse the input expressions.
8118
8119    Returns:
8120        A new Select instance with the subquery expression included.
8121    """
8122
8123    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias, **opts)
8124    return Select().from_(expression, dialect=dialect, **opts)
8125
8126
8127@t.overload
8128def column(
8129    col: str | Identifier,
8130    table: t.Optional[str | Identifier] = None,
8131    db: t.Optional[str | Identifier] = None,
8132    catalog: t.Optional[str | Identifier] = None,
8133    *,
8134    fields: t.Collection[t.Union[str, Identifier]],
8135    quoted: t.Optional[bool] = None,
8136    copy: bool = True,
8137) -> Dot:
8138    pass
8139
8140
8141@t.overload
8142def column(
8143    col: str | Identifier | Star,
8144    table: t.Optional[str | Identifier] = None,
8145    db: t.Optional[str | Identifier] = None,
8146    catalog: t.Optional[str | Identifier] = None,
8147    *,
8148    fields: Lit[None] = None,
8149    quoted: t.Optional[bool] = None,
8150    copy: bool = True,
8151) -> Column:
8152    pass
8153
8154
8155def column(
8156    col,
8157    table=None,
8158    db=None,
8159    catalog=None,
8160    *,
8161    fields=None,
8162    quoted=None,
8163    copy=True,
8164):
8165    """
8166    Build a Column.
8167
8168    Args:
8169        col: Column name.
8170        table: Table name.
8171        db: Database name.
8172        catalog: Catalog name.
8173        fields: Additional fields using dots.
8174        quoted: Whether to force quotes on the column's identifiers.
8175        copy: Whether to copy identifiers if passed in.
8176
8177    Returns:
8178        The new Column instance.
8179    """
8180    if not isinstance(col, Star):
8181        col = to_identifier(col, quoted=quoted, copy=copy)
8182
8183    this = Column(
8184        this=col,
8185        table=to_identifier(table, quoted=quoted, copy=copy),
8186        db=to_identifier(db, quoted=quoted, copy=copy),
8187        catalog=to_identifier(catalog, quoted=quoted, copy=copy),
8188    )
8189
8190    if fields:
8191        this = Dot.build(
8192            (this, *(to_identifier(field, quoted=quoted, copy=copy) for field in fields))
8193        )
8194    return this
8195
8196
8197def cast(
8198    expression: ExpOrStr, to: DATA_TYPE, copy: bool = True, dialect: DialectType = None, **opts
8199) -> Cast:
8200    """Cast an expression to a data type.
8201
8202    Example:
8203        >>> cast('x + 1', 'int').sql()
8204        'CAST(x + 1 AS INT)'
8205
8206    Args:
8207        expression: The expression to cast.
8208        to: The datatype to cast to.
8209        copy: Whether to copy the supplied expressions.
8210        dialect: The target dialect. This is used to prevent a re-cast in the following scenario:
8211            - The expression to be cast is already a exp.Cast expression
8212            - The existing cast is to a type that is logically equivalent to new type
8213
8214            For example, if :expression='CAST(x as DATETIME)' and :to=Type.TIMESTAMP,
8215            but in the target dialect DATETIME is mapped to TIMESTAMP, then we will NOT return `CAST(x (as DATETIME) as TIMESTAMP)`
8216            and instead just return the original expression `CAST(x as DATETIME)`.
8217
8218            This is to prevent it being output as a double cast `CAST(x (as TIMESTAMP) as TIMESTAMP)` once the DATETIME -> TIMESTAMP
8219            mapping is applied in the target dialect generator.
8220
8221    Returns:
8222        The new Cast instance.
8223    """
8224    expr = maybe_parse(expression, copy=copy, dialect=dialect, **opts)
8225    data_type = DataType.build(to, copy=copy, dialect=dialect, **opts)
8226
8227    # dont re-cast if the expression is already a cast to the correct type
8228    if isinstance(expr, Cast):
8229        from sqlglot.dialects.dialect import Dialect
8230
8231        target_dialect = Dialect.get_or_raise(dialect)
8232        type_mapping = target_dialect.generator_class.TYPE_MAPPING
8233
8234        existing_cast_type: DataType.Type = expr.to.this
8235        new_cast_type: DataType.Type = data_type.this
8236        types_are_equivalent = type_mapping.get(
8237            existing_cast_type, existing_cast_type.value
8238        ) == type_mapping.get(new_cast_type, new_cast_type.value)
8239
8240        if expr.is_type(data_type) or types_are_equivalent:
8241            return expr
8242
8243    expr = Cast(this=expr, to=data_type)
8244    expr.type = data_type
8245
8246    return expr
8247
8248
8249def table_(
8250    table: Identifier | str,
8251    db: t.Optional[Identifier | str] = None,
8252    catalog: t.Optional[Identifier | str] = None,
8253    quoted: t.Optional[bool] = None,
8254    alias: t.Optional[Identifier | str] = None,
8255) -> Table:
8256    """Build a Table.
8257
8258    Args:
8259        table: Table name.
8260        db: Database name.
8261        catalog: Catalog name.
8262        quote: Whether to force quotes on the table's identifiers.
8263        alias: Table's alias.
8264
8265    Returns:
8266        The new Table instance.
8267    """
8268    return Table(
8269        this=to_identifier(table, quoted=quoted) if table else None,
8270        db=to_identifier(db, quoted=quoted) if db else None,
8271        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
8272        alias=TableAlias(this=to_identifier(alias)) if alias else None,
8273    )
8274
8275
8276def values(
8277    values: t.Iterable[t.Tuple[t.Any, ...]],
8278    alias: t.Optional[str] = None,
8279    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
8280) -> Values:
8281    """Build VALUES statement.
8282
8283    Example:
8284        >>> values([(1, '2')]).sql()
8285        "VALUES (1, '2')"
8286
8287    Args:
8288        values: values statements that will be converted to SQL
8289        alias: optional alias
8290        columns: Optional list of ordered column names or ordered dictionary of column names to types.
8291         If either are provided then an alias is also required.
8292
8293    Returns:
8294        Values: the Values expression object
8295    """
8296    if columns and not alias:
8297        raise ValueError("Alias is required when providing columns")
8298
8299    return Values(
8300        expressions=[convert(tup) for tup in values],
8301        alias=(
8302            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
8303            if columns
8304            else (TableAlias(this=to_identifier(alias)) if alias else None)
8305        ),
8306    )
8307
8308
8309def var(name: t.Optional[ExpOrStr]) -> Var:
8310    """Build a SQL variable.
8311
8312    Example:
8313        >>> repr(var('x'))
8314        'Var(this=x)'
8315
8316        >>> repr(var(column('x', table='y')))
8317        'Var(this=x)'
8318
8319    Args:
8320        name: The name of the var or an expression who's name will become the var.
8321
8322    Returns:
8323        The new variable node.
8324    """
8325    if not name:
8326        raise ValueError("Cannot convert empty name into var.")
8327
8328    if isinstance(name, Expression):
8329        name = name.name
8330    return Var(this=name)
8331
8332
8333def rename_table(
8334    old_name: str | Table,
8335    new_name: str | Table,
8336    dialect: DialectType = None,
8337) -> Alter:
8338    """Build ALTER TABLE... RENAME... expression
8339
8340    Args:
8341        old_name: The old name of the table
8342        new_name: The new name of the table
8343        dialect: The dialect to parse the table.
8344
8345    Returns:
8346        Alter table expression
8347    """
8348    old_table = to_table(old_name, dialect=dialect)
8349    new_table = to_table(new_name, dialect=dialect)
8350    return Alter(
8351        this=old_table,
8352        kind="TABLE",
8353        actions=[
8354            AlterRename(this=new_table),
8355        ],
8356    )
8357
8358
8359def rename_column(
8360    table_name: str | Table,
8361    old_column_name: str | Column,
8362    new_column_name: str | Column,
8363    exists: t.Optional[bool] = None,
8364    dialect: DialectType = None,
8365) -> Alter:
8366    """Build ALTER TABLE... RENAME COLUMN... expression
8367
8368    Args:
8369        table_name: Name of the table
8370        old_column: The old name of the column
8371        new_column: The new name of the column
8372        exists: Whether to add the `IF EXISTS` clause
8373        dialect: The dialect to parse the table/column.
8374
8375    Returns:
8376        Alter table expression
8377    """
8378    table = to_table(table_name, dialect=dialect)
8379    old_column = to_column(old_column_name, dialect=dialect)
8380    new_column = to_column(new_column_name, dialect=dialect)
8381    return Alter(
8382        this=table,
8383        kind="TABLE",
8384        actions=[
8385            RenameColumn(this=old_column, to=new_column, exists=exists),
8386        ],
8387    )
8388
8389
8390def convert(value: t.Any, copy: bool = False) -> Expression:
8391    """Convert a python value into an expression object.
8392
8393    Raises an error if a conversion is not possible.
8394
8395    Args:
8396        value: A python object.
8397        copy: Whether to copy `value` (only applies to Expressions and collections).
8398
8399    Returns:
8400        The equivalent expression object.
8401    """
8402    if isinstance(value, Expression):
8403        return maybe_copy(value, copy)
8404    if isinstance(value, str):
8405        return Literal.string(value)
8406    if isinstance(value, bool):
8407        return Boolean(this=value)
8408    if value is None or (isinstance(value, float) and math.isnan(value)):
8409        return null()
8410    if isinstance(value, numbers.Number):
8411        return Literal.number(value)
8412    if isinstance(value, bytes):
8413        return HexString(this=value.hex())
8414    if isinstance(value, datetime.datetime):
8415        datetime_literal = Literal.string(value.isoformat(sep=" "))
8416
8417        tz = None
8418        if value.tzinfo:
8419            # this works for zoneinfo.ZoneInfo, pytz.timezone and datetime.datetime.utc to return IANA timezone names like "America/Los_Angeles"
8420            # instead of abbreviations like "PDT". This is for consistency with other timezone handling functions in SQLGlot
8421            tz = Literal.string(str(value.tzinfo))
8422
8423        return TimeStrToTime(this=datetime_literal, zone=tz)
8424    if isinstance(value, datetime.date):
8425        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
8426        return DateStrToDate(this=date_literal)
8427    if isinstance(value, tuple):
8428        if hasattr(value, "_fields"):
8429            return Struct(
8430                expressions=[
8431                    PropertyEQ(
8432                        this=to_identifier(k), expression=convert(getattr(value, k), copy=copy)
8433                    )
8434                    for k in value._fields
8435                ]
8436            )
8437        return Tuple(expressions=[convert(v, copy=copy) for v in value])
8438    if isinstance(value, list):
8439        return Array(expressions=[convert(v, copy=copy) for v in value])
8440    if isinstance(value, dict):
8441        return Map(
8442            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
8443            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
8444        )
8445    if hasattr(value, "__dict__"):
8446        return Struct(
8447            expressions=[
8448                PropertyEQ(this=to_identifier(k), expression=convert(v, copy=copy))
8449                for k, v in value.__dict__.items()
8450            ]
8451        )
8452    raise ValueError(f"Cannot convert {value}")
8453
8454
8455def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
8456    """
8457    Replace children of an expression with the result of a lambda fun(child) -> exp.
8458    """
8459    for k, v in tuple(expression.args.items()):
8460        is_list_arg = type(v) is list
8461
8462        child_nodes = v if is_list_arg else [v]
8463        new_child_nodes = []
8464
8465        for cn in child_nodes:
8466            if isinstance(cn, Expression):
8467                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
8468                    new_child_nodes.append(child_node)
8469            else:
8470                new_child_nodes.append(cn)
8471
8472        expression.set(k, new_child_nodes if is_list_arg else seq_get(new_child_nodes, 0))
8473
8474
8475def replace_tree(
8476    expression: Expression,
8477    fun: t.Callable,
8478    prune: t.Optional[t.Callable[[Expression], bool]] = None,
8479) -> Expression:
8480    """
8481    Replace an entire tree with the result of function calls on each node.
8482
8483    This will be traversed in reverse dfs, so leaves first.
8484    If new nodes are created as a result of function calls, they will also be traversed.
8485    """
8486    stack = list(expression.dfs(prune=prune))
8487
8488    while stack:
8489        node = stack.pop()
8490        new_node = fun(node)
8491
8492        if new_node is not node:
8493            node.replace(new_node)
8494
8495            if isinstance(new_node, Expression):
8496                stack.append(new_node)
8497
8498    return new_node
8499
8500
8501def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
8502    """
8503    Return all table names referenced through columns in an expression.
8504
8505    Example:
8506        >>> import sqlglot
8507        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
8508        ['a', 'c']
8509
8510    Args:
8511        expression: expression to find table names.
8512        exclude: a table name to exclude
8513
8514    Returns:
8515        A list of unique names.
8516    """
8517    return {
8518        table
8519        for table in (column.table for column in expression.find_all(Column))
8520        if table and table != exclude
8521    }
8522
8523
8524def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
8525    """Get the full name of a table as a string.
8526
8527    Args:
8528        table: Table expression node or string.
8529        dialect: The dialect to generate the table name for.
8530        identify: Determines when an identifier should be quoted. Possible values are:
8531            False (default): Never quote, except in cases where it's mandatory by the dialect.
8532            True: Always quote.
8533
8534    Examples:
8535        >>> from sqlglot import exp, parse_one
8536        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
8537        'a.b.c'
8538
8539    Returns:
8540        The table name.
8541    """
8542
8543    table = maybe_parse(table, into=Table, dialect=dialect)
8544
8545    if not table:
8546        raise ValueError(f"Cannot parse {table}")
8547
8548    return ".".join(
8549        (
8550            part.sql(dialect=dialect, identify=True, copy=False, comments=False)
8551            if identify or not SAFE_IDENTIFIER_RE.match(part.name)
8552            else part.name
8553        )
8554        for part in table.parts
8555    )
8556
8557
8558def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
8559    """Returns a case normalized table name without quotes.
8560
8561    Args:
8562        table: the table to normalize
8563        dialect: the dialect to use for normalization rules
8564        copy: whether to copy the expression.
8565
8566    Examples:
8567        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
8568        'A-B.c'
8569    """
8570    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
8571
8572    return ".".join(
8573        p.name
8574        for p in normalize_identifiers(
8575            to_table(table, dialect=dialect, copy=copy), dialect=dialect
8576        ).parts
8577    )
8578
8579
8580def replace_tables(
8581    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
8582) -> E:
8583    """Replace all tables in expression according to the mapping.
8584
8585    Args:
8586        expression: expression node to be transformed and replaced.
8587        mapping: mapping of table names.
8588        dialect: the dialect of the mapping table
8589        copy: whether to copy the expression.
8590
8591    Examples:
8592        >>> from sqlglot import exp, parse_one
8593        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
8594        'SELECT * FROM c /* a.b */'
8595
8596    Returns:
8597        The mapped expression.
8598    """
8599
8600    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
8601
8602    def _replace_tables(node: Expression) -> Expression:
8603        if isinstance(node, Table) and node.meta.get("replace") is not False:
8604            original = normalize_table_name(node, dialect=dialect)
8605            new_name = mapping.get(original)
8606
8607            if new_name:
8608                table = to_table(
8609                    new_name,
8610                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
8611                    dialect=dialect,
8612                )
8613                table.add_comments([original])
8614                return table
8615        return node
8616
8617    return expression.transform(_replace_tables, copy=copy)  # type: ignore
8618
8619
8620def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
8621    """Replace placeholders in an expression.
8622
8623    Args:
8624        expression: expression node to be transformed and replaced.
8625        args: positional names that will substitute unnamed placeholders in the given order.
8626        kwargs: keyword arguments that will substitute named placeholders.
8627
8628    Examples:
8629        >>> from sqlglot import exp, parse_one
8630        >>> replace_placeholders(
8631        ...     parse_one("select * from :tbl where ? = ?"),
8632        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
8633        ... ).sql()
8634        "SELECT * FROM foo WHERE str_col = 'b'"
8635
8636    Returns:
8637        The mapped expression.
8638    """
8639
8640    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
8641        if isinstance(node, Placeholder):
8642            if node.this:
8643                new_name = kwargs.get(node.this)
8644                if new_name is not None:
8645                    return convert(new_name)
8646            else:
8647                try:
8648                    return convert(next(args))
8649                except StopIteration:
8650                    pass
8651        return node
8652
8653    return expression.transform(_replace_placeholders, iter(args), **kwargs)
8654
8655
8656def expand(
8657    expression: Expression,
8658    sources: t.Dict[str, Query | t.Callable[[], Query]],
8659    dialect: DialectType = None,
8660    copy: bool = True,
8661) -> Expression:
8662    """Transforms an expression by expanding all referenced sources into subqueries.
8663
8664    Examples:
8665        >>> from sqlglot import parse_one
8666        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
8667        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
8668
8669        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
8670        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
8671
8672    Args:
8673        expression: The expression to expand.
8674        sources: A dict of name to query or a callable that provides a query on demand.
8675        dialect: The dialect of the sources dict or the callable.
8676        copy: Whether to copy the expression during transformation. Defaults to True.
8677
8678    Returns:
8679        The transformed expression.
8680    """
8681    normalized_sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
8682
8683    def _expand(node: Expression):
8684        if isinstance(node, Table):
8685            name = normalize_table_name(node, dialect=dialect)
8686            source = normalized_sources.get(name)
8687
8688            if source:
8689                # Create a subquery with the same alias (or table name if no alias)
8690                parsed_source = source() if callable(source) else source
8691                subquery = parsed_source.subquery(node.alias or name)
8692                subquery.comments = [f"source: {name}"]
8693
8694                # Continue expanding within the subquery
8695                return subquery.transform(_expand, copy=False)
8696
8697        return node
8698
8699    return expression.transform(_expand, copy=copy)
8700
8701
8702def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
8703    """
8704    Returns a Func expression.
8705
8706    Examples:
8707        >>> func("abs", 5).sql()
8708        'ABS(5)'
8709
8710        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
8711        'CAST(5 AS DOUBLE)'
8712
8713    Args:
8714        name: the name of the function to build.
8715        args: the args used to instantiate the function of interest.
8716        copy: whether to copy the argument expressions.
8717        dialect: the source dialect.
8718        kwargs: the kwargs used to instantiate the function of interest.
8719
8720    Note:
8721        The arguments `args` and `kwargs` are mutually exclusive.
8722
8723    Returns:
8724        An instance of the function of interest, or an anonymous function, if `name` doesn't
8725        correspond to an existing `sqlglot.expressions.Func` class.
8726    """
8727    if args and kwargs:
8728        raise ValueError("Can't use both args and kwargs to instantiate a function.")
8729
8730    from sqlglot.dialects.dialect import Dialect
8731
8732    dialect = Dialect.get_or_raise(dialect)
8733
8734    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
8735    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
8736
8737    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
8738    if constructor:
8739        if converted:
8740            if "dialect" in constructor.__code__.co_varnames:
8741                function = constructor(converted, dialect=dialect)
8742            else:
8743                function = constructor(converted)
8744        elif constructor.__name__ == "from_arg_list":
8745            function = constructor.__self__(**kwargs)  # type: ignore
8746        else:
8747            constructor = FUNCTION_BY_NAME.get(name.upper())
8748            if constructor:
8749                function = constructor(**kwargs)
8750            else:
8751                raise ValueError(
8752                    f"Unable to convert '{name}' into a Func. Either manually construct "
8753                    "the Func expression of interest or parse the function call."
8754                )
8755    else:
8756        kwargs = kwargs or {"expressions": converted}
8757        function = Anonymous(this=name, **kwargs)
8758
8759    for error_message in function.error_messages(converted):
8760        raise ValueError(error_message)
8761
8762    return function
8763
8764
8765def case(
8766    expression: t.Optional[ExpOrStr] = None,
8767    **opts,
8768) -> Case:
8769    """
8770    Initialize a CASE statement.
8771
8772    Example:
8773        case().when("a = 1", "foo").else_("bar")
8774
8775    Args:
8776        expression: Optionally, the input expression (not all dialects support this)
8777        **opts: Extra keyword arguments for parsing `expression`
8778    """
8779    if expression is not None:
8780        this = maybe_parse(expression, **opts)
8781    else:
8782        this = None
8783    return Case(this=this, ifs=[])
8784
8785
8786def array(
8787    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8788) -> Array:
8789    """
8790    Returns an array.
8791
8792    Examples:
8793        >>> array(1, 'x').sql()
8794        'ARRAY(1, x)'
8795
8796    Args:
8797        expressions: the expressions to add to the array.
8798        copy: whether to copy the argument expressions.
8799        dialect: the source dialect.
8800        kwargs: the kwargs used to instantiate the function of interest.
8801
8802    Returns:
8803        An array expression.
8804    """
8805    return Array(
8806        expressions=[
8807            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8808            for expression in expressions
8809        ]
8810    )
8811
8812
8813def tuple_(
8814    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8815) -> Tuple:
8816    """
8817    Returns an tuple.
8818
8819    Examples:
8820        >>> tuple_(1, 'x').sql()
8821        '(1, x)'
8822
8823    Args:
8824        expressions: the expressions to add to the tuple.
8825        copy: whether to copy the argument expressions.
8826        dialect: the source dialect.
8827        kwargs: the kwargs used to instantiate the function of interest.
8828
8829    Returns:
8830        A tuple expression.
8831    """
8832    return Tuple(
8833        expressions=[
8834            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8835            for expression in expressions
8836        ]
8837    )
8838
8839
8840def true() -> Boolean:
8841    """
8842    Returns a true Boolean expression.
8843    """
8844    return Boolean(this=True)
8845
8846
8847def false() -> Boolean:
8848    """
8849    Returns a false Boolean expression.
8850    """
8851    return Boolean(this=False)
8852
8853
8854def null() -> Null:
8855    """
8856    Returns a Null expression.
8857    """
8858    return Null()
8859
8860
8861NONNULL_CONSTANTS = (
8862    Literal,
8863    Boolean,
8864)
8865
8866CONSTANTS = (
8867    Literal,
8868    Boolean,
8869    Null,
8870)
SQLGLOT_META = 'sqlglot.meta'
SQLGLOT_ANONYMOUS = 'sqlglot.anonymous'
TABLE_PARTS = ('this', 'db', 'catalog')
COLUMN_PARTS = ('this', 'table', 'db', 'catalog')
POSITION_META_KEYS = ('line', 'col', 'start', 'end')
class Expression:
  72class Expression(metaclass=_Expression):
  73    """
  74    The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary
  75    context, such as its child expressions, their names (arg keys), and whether a given child expression
  76    is optional or not.
  77
  78    Attributes:
  79        key: a unique key for each class in the Expression hierarchy. This is useful for hashing
  80            and representing expressions as strings.
  81        arg_types: determines the arguments (child nodes) supported by an expression. It maps
  82            arg keys to booleans that indicate whether the corresponding args are optional.
  83        parent: a reference to the parent expression (or None, in case of root expressions).
  84        arg_key: the arg key an expression is associated with, i.e. the name its parent expression
  85            uses to refer to it.
  86        index: the index of an expression if it is inside of a list argument in its parent.
  87        comments: a list of comments that are associated with a given expression. This is used in
  88            order to preserve comments when transpiling SQL code.
  89        type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the
  90            optimizer, in order to enable some transformations that require type information.
  91        meta: a dictionary that can be used to store useful metadata for a given expression.
  92
  93    Example:
  94        >>> class Foo(Expression):
  95        ...     arg_types = {"this": True, "expression": False}
  96
  97        The above definition informs us that Foo is an Expression that requires an argument called
  98        "this" and may also optionally receive an argument called "expression".
  99
 100    Args:
 101        args: a mapping used for retrieving the arguments of an expression, given their arg keys.
 102    """
 103
 104    key = "expression"
 105    arg_types = {"this": True}
 106    __slots__ = ("args", "parent", "arg_key", "index", "comments", "_type", "_meta", "_hash")
 107
 108    def __init__(self, **args: t.Any):
 109        self.args: t.Dict[str, t.Any] = args
 110        self.parent: t.Optional[Expression] = None
 111        self.arg_key: t.Optional[str] = None
 112        self.index: t.Optional[int] = None
 113        self.comments: t.Optional[t.List[str]] = None
 114        self._type: t.Optional[DataType] = None
 115        self._meta: t.Optional[t.Dict[str, t.Any]] = None
 116        self._hash: t.Optional[int] = None
 117
 118        for arg_key, value in self.args.items():
 119            self._set_parent(arg_key, value)
 120
 121    def __eq__(self, other) -> bool:
 122        return type(self) is type(other) and hash(self) == hash(other)
 123
 124    @property
 125    def hashable_args(self) -> t.Any:
 126        return frozenset(
 127            (k, tuple(_norm_arg(a) for a in v) if type(v) is list else _norm_arg(v))
 128            for k, v in self.args.items()
 129            if not (v is None or v is False or (type(v) is list and not v))
 130        )
 131
 132    def __hash__(self) -> int:
 133        if self._hash is not None:
 134            return self._hash
 135
 136        return hash((self.__class__, self.hashable_args))
 137
 138    @property
 139    def this(self) -> t.Any:
 140        """
 141        Retrieves the argument with key "this".
 142        """
 143        return self.args.get("this")
 144
 145    @property
 146    def expression(self) -> t.Any:
 147        """
 148        Retrieves the argument with key "expression".
 149        """
 150        return self.args.get("expression")
 151
 152    @property
 153    def expressions(self) -> t.List[t.Any]:
 154        """
 155        Retrieves the argument with key "expressions".
 156        """
 157        return self.args.get("expressions") or []
 158
 159    def text(self, key) -> str:
 160        """
 161        Returns a textual representation of the argument corresponding to "key". This can only be used
 162        for args that are strings or leaf Expression instances, such as identifiers and literals.
 163        """
 164        field = self.args.get(key)
 165        if isinstance(field, str):
 166            return field
 167        if isinstance(field, (Identifier, Literal, Var)):
 168            return field.this
 169        if isinstance(field, (Star, Null)):
 170            return field.name
 171        return ""
 172
 173    @property
 174    def is_string(self) -> bool:
 175        """
 176        Checks whether a Literal expression is a string.
 177        """
 178        return isinstance(self, Literal) and self.args["is_string"]
 179
 180    @property
 181    def is_number(self) -> bool:
 182        """
 183        Checks whether a Literal expression is a number.
 184        """
 185        return (isinstance(self, Literal) and not self.args["is_string"]) or (
 186            isinstance(self, Neg) and self.this.is_number
 187        )
 188
 189    def to_py(self) -> t.Any:
 190        """
 191        Returns a Python object equivalent of the SQL node.
 192        """
 193        raise ValueError(f"{self} cannot be converted to a Python object.")
 194
 195    @property
 196    def is_int(self) -> bool:
 197        """
 198        Checks whether an expression is an integer.
 199        """
 200        return self.is_number and isinstance(self.to_py(), int)
 201
 202    @property
 203    def is_star(self) -> bool:
 204        """Checks whether an expression is a star."""
 205        return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star))
 206
 207    @property
 208    def alias(self) -> str:
 209        """
 210        Returns the alias of the expression, or an empty string if it's not aliased.
 211        """
 212        if isinstance(self.args.get("alias"), TableAlias):
 213            return self.args["alias"].name
 214        return self.text("alias")
 215
 216    @property
 217    def alias_column_names(self) -> t.List[str]:
 218        table_alias = self.args.get("alias")
 219        if not table_alias:
 220            return []
 221        return [c.name for c in table_alias.args.get("columns") or []]
 222
 223    @property
 224    def name(self) -> str:
 225        return self.text("this")
 226
 227    @property
 228    def alias_or_name(self) -> str:
 229        return self.alias or self.name
 230
 231    @property
 232    def output_name(self) -> str:
 233        """
 234        Name of the output column if this expression is a selection.
 235
 236        If the Expression has no output name, an empty string is returned.
 237
 238        Example:
 239            >>> from sqlglot import parse_one
 240            >>> parse_one("SELECT a").expressions[0].output_name
 241            'a'
 242            >>> parse_one("SELECT b AS c").expressions[0].output_name
 243            'c'
 244            >>> parse_one("SELECT 1 + 2").expressions[0].output_name
 245            ''
 246        """
 247        return ""
 248
 249    @property
 250    def type(self) -> t.Optional[DataType]:
 251        return self._type
 252
 253    @type.setter
 254    def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None:
 255        if dtype and not isinstance(dtype, DataType):
 256            dtype = DataType.build(dtype)
 257        self._type = dtype  # type: ignore
 258
 259    def is_type(self, *dtypes) -> bool:
 260        return self.type is not None and self.type.is_type(*dtypes)
 261
 262    def is_leaf(self) -> bool:
 263        return not any(isinstance(v, (Expression, list)) for v in self.args.values())
 264
 265    @property
 266    def meta(self) -> t.Dict[str, t.Any]:
 267        if self._meta is None:
 268            self._meta = {}
 269        return self._meta
 270
 271    def __deepcopy__(self, memo):
 272        root = self.__class__()
 273        stack = [(self, root)]
 274
 275        while stack:
 276            node, copy = stack.pop()
 277
 278            if node.comments is not None:
 279                copy.comments = deepcopy(node.comments)
 280            if node._type is not None:
 281                copy._type = deepcopy(node._type)
 282            if node._meta is not None:
 283                copy._meta = deepcopy(node._meta)
 284            if node._hash is not None:
 285                copy._hash = node._hash
 286
 287            for k, vs in node.args.items():
 288                if hasattr(vs, "parent"):
 289                    stack.append((vs, vs.__class__()))
 290                    copy.set(k, stack[-1][-1])
 291                elif type(vs) is list:
 292                    copy.args[k] = []
 293
 294                    for v in vs:
 295                        if hasattr(v, "parent"):
 296                            stack.append((v, v.__class__()))
 297                            copy.append(k, stack[-1][-1])
 298                        else:
 299                            copy.append(k, v)
 300                else:
 301                    copy.args[k] = vs
 302
 303        return root
 304
 305    def copy(self) -> Self:
 306        """
 307        Returns a deep copy of the expression.
 308        """
 309        return deepcopy(self)
 310
 311    def add_comments(self, comments: t.Optional[t.List[str]] = None, prepend: bool = False) -> None:
 312        if self.comments is None:
 313            self.comments = []
 314
 315        if comments:
 316            for comment in comments:
 317                _, *meta = comment.split(SQLGLOT_META)
 318                if meta:
 319                    for kv in "".join(meta).split(","):
 320                        k, *v = kv.split("=")
 321                        value = v[0].strip() if v else True
 322                        self.meta[k.strip()] = to_bool(value)
 323
 324                if not prepend:
 325                    self.comments.append(comment)
 326
 327            if prepend:
 328                self.comments = comments + self.comments
 329
 330    def pop_comments(self) -> t.List[str]:
 331        comments = self.comments or []
 332        self.comments = None
 333        return comments
 334
 335    def append(self, arg_key: str, value: t.Any) -> None:
 336        """
 337        Appends value to arg_key if it's a list or sets it as a new list.
 338
 339        Args:
 340            arg_key (str): name of the list expression arg
 341            value (Any): value to append to the list
 342        """
 343        if type(self.args.get(arg_key)) is not list:
 344            self.args[arg_key] = []
 345        self._set_parent(arg_key, value)
 346        values = self.args[arg_key]
 347        if hasattr(value, "parent"):
 348            value.index = len(values)
 349        values.append(value)
 350
 351    def set(
 352        self,
 353        arg_key: str,
 354        value: t.Any,
 355        index: t.Optional[int] = None,
 356        overwrite: bool = True,
 357    ) -> None:
 358        """
 359        Sets arg_key to value.
 360
 361        Args:
 362            arg_key: name of the expression arg.
 363            value: value to set the arg to.
 364            index: if the arg is a list, this specifies what position to add the value in it.
 365            overwrite: assuming an index is given, this determines whether to overwrite the
 366                list entry instead of only inserting a new value (i.e., like list.insert).
 367        """
 368        if index is not None:
 369            expressions = self.args.get(arg_key) or []
 370
 371            if seq_get(expressions, index) is None:
 372                return
 373            if value is None:
 374                expressions.pop(index)
 375                for v in expressions[index:]:
 376                    v.index = v.index - 1
 377                return
 378
 379            if isinstance(value, list):
 380                expressions.pop(index)
 381                expressions[index:index] = value
 382            elif overwrite:
 383                expressions[index] = value
 384            else:
 385                expressions.insert(index, value)
 386
 387            value = expressions
 388        elif value is None:
 389            self.args.pop(arg_key, None)
 390            return
 391
 392        self.args[arg_key] = value
 393        self._set_parent(arg_key, value, index)
 394
 395    def _set_parent(self, arg_key: str, value: t.Any, index: t.Optional[int] = None) -> None:
 396        if hasattr(value, "parent"):
 397            value.parent = self
 398            value.arg_key = arg_key
 399            value.index = index
 400        elif type(value) is list:
 401            for index, v in enumerate(value):
 402                if hasattr(v, "parent"):
 403                    v.parent = self
 404                    v.arg_key = arg_key
 405                    v.index = index
 406
 407    @property
 408    def depth(self) -> int:
 409        """
 410        Returns the depth of this tree.
 411        """
 412        if self.parent:
 413            return self.parent.depth + 1
 414        return 0
 415
 416    def iter_expressions(self, reverse: bool = False) -> t.Iterator[Expression]:
 417        """Yields the key and expression for all arguments, exploding list args."""
 418        for vs in reversed(self.args.values()) if reverse else self.args.values():  # type: ignore
 419            if type(vs) is list:
 420                for v in reversed(vs) if reverse else vs:  # type: ignore
 421                    if hasattr(v, "parent"):
 422                        yield v
 423            else:
 424                if hasattr(vs, "parent"):
 425                    yield vs
 426
 427    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
 428        """
 429        Returns the first node in this tree which matches at least one of
 430        the specified types.
 431
 432        Args:
 433            expression_types: the expression type(s) to match.
 434            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
 435
 436        Returns:
 437            The node which matches the criteria or None if no such node was found.
 438        """
 439        return next(self.find_all(*expression_types, bfs=bfs), None)
 440
 441    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
 442        """
 443        Returns a generator object which visits all nodes in this tree and only
 444        yields those that match at least one of the specified expression types.
 445
 446        Args:
 447            expression_types: the expression type(s) to match.
 448            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
 449
 450        Returns:
 451            The generator object.
 452        """
 453        for expression in self.walk(bfs=bfs):
 454            if isinstance(expression, expression_types):
 455                yield expression
 456
 457    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
 458        """
 459        Returns a nearest parent matching expression_types.
 460
 461        Args:
 462            expression_types: the expression type(s) to match.
 463
 464        Returns:
 465            The parent node.
 466        """
 467        ancestor = self.parent
 468        while ancestor and not isinstance(ancestor, expression_types):
 469            ancestor = ancestor.parent
 470        return ancestor  # type: ignore
 471
 472    @property
 473    def parent_select(self) -> t.Optional[Select]:
 474        """
 475        Returns the parent select statement.
 476        """
 477        return self.find_ancestor(Select)
 478
 479    @property
 480    def same_parent(self) -> bool:
 481        """Returns if the parent is the same class as itself."""
 482        return type(self.parent) is self.__class__
 483
 484    def root(self) -> Expression:
 485        """
 486        Returns the root expression of this tree.
 487        """
 488        expression = self
 489        while expression.parent:
 490            expression = expression.parent
 491        return expression
 492
 493    def walk(
 494        self, bfs: bool = True, prune: t.Optional[t.Callable[[Expression], bool]] = None
 495    ) -> t.Iterator[Expression]:
 496        """
 497        Returns a generator object which visits all nodes in this tree.
 498
 499        Args:
 500            bfs: if set to True the BFS traversal order will be applied,
 501                otherwise the DFS traversal will be used instead.
 502            prune: callable that returns True if the generator should stop traversing
 503                this branch of the tree.
 504
 505        Returns:
 506            the generator object.
 507        """
 508        if bfs:
 509            yield from self.bfs(prune=prune)
 510        else:
 511            yield from self.dfs(prune=prune)
 512
 513    def dfs(
 514        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
 515    ) -> t.Iterator[Expression]:
 516        """
 517        Returns a generator object which visits all nodes in this tree in
 518        the DFS (Depth-first) order.
 519
 520        Returns:
 521            The generator object.
 522        """
 523        stack = [self]
 524
 525        while stack:
 526            node = stack.pop()
 527
 528            yield node
 529
 530            if prune and prune(node):
 531                continue
 532
 533            for v in node.iter_expressions(reverse=True):
 534                stack.append(v)
 535
 536    def bfs(
 537        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
 538    ) -> t.Iterator[Expression]:
 539        """
 540        Returns a generator object which visits all nodes in this tree in
 541        the BFS (Breadth-first) order.
 542
 543        Returns:
 544            The generator object.
 545        """
 546        queue = deque([self])
 547
 548        while queue:
 549            node = queue.popleft()
 550
 551            yield node
 552
 553            if prune and prune(node):
 554                continue
 555
 556            for v in node.iter_expressions():
 557                queue.append(v)
 558
 559    def unnest(self):
 560        """
 561        Returns the first non parenthesis child or self.
 562        """
 563        expression = self
 564        while type(expression) is Paren:
 565            expression = expression.this
 566        return expression
 567
 568    def unalias(self):
 569        """
 570        Returns the inner expression if this is an Alias.
 571        """
 572        if isinstance(self, Alias):
 573            return self.this
 574        return self
 575
 576    def unnest_operands(self):
 577        """
 578        Returns unnested operands as a tuple.
 579        """
 580        return tuple(arg.unnest() for arg in self.iter_expressions())
 581
 582    def flatten(self, unnest=True):
 583        """
 584        Returns a generator which yields child nodes whose parents are the same class.
 585
 586        A AND B AND C -> [A, B, C]
 587        """
 588        for node in self.dfs(prune=lambda n: n.parent and type(n) is not self.__class__):
 589            if type(node) is not self.__class__:
 590                yield node.unnest() if unnest and not isinstance(node, Subquery) else node
 591
 592    def __str__(self) -> str:
 593        return self.sql()
 594
 595    def __repr__(self) -> str:
 596        return _to_s(self)
 597
 598    def to_s(self) -> str:
 599        """
 600        Same as __repr__, but includes additional information which can be useful
 601        for debugging, like empty or missing args and the AST nodes' object IDs.
 602        """
 603        return _to_s(self, verbose=True)
 604
 605    def sql(self, dialect: DialectType = None, **opts) -> str:
 606        """
 607        Returns SQL string representation of this tree.
 608
 609        Args:
 610            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
 611            opts: other `sqlglot.generator.Generator` options.
 612
 613        Returns:
 614            The SQL string.
 615        """
 616        from sqlglot.dialects import Dialect
 617
 618        return Dialect.get_or_raise(dialect).generate(self, **opts)
 619
 620    def transform(self, fun: t.Callable, *args: t.Any, copy: bool = True, **kwargs) -> Expression:
 621        """
 622        Visits all tree nodes (excluding already transformed ones)
 623        and applies the given transformation function to each node.
 624
 625        Args:
 626            fun: a function which takes a node as an argument and returns a
 627                new transformed node or the same node without modifications. If the function
 628                returns None, then the corresponding node will be removed from the syntax tree.
 629            copy: if set to True a new tree instance is constructed, otherwise the tree is
 630                modified in place.
 631
 632        Returns:
 633            The transformed tree.
 634        """
 635        root = None
 636        new_node = None
 637
 638        for node in (self.copy() if copy else self).dfs(prune=lambda n: n is not new_node):
 639            parent, arg_key, index = node.parent, node.arg_key, node.index
 640            new_node = fun(node, *args, **kwargs)
 641
 642            if not root:
 643                root = new_node
 644            elif parent and arg_key and new_node is not node:
 645                parent.set(arg_key, new_node, index)
 646
 647        assert root
 648        return root.assert_is(Expression)
 649
 650    @t.overload
 651    def replace(self, expression: E) -> E: ...
 652
 653    @t.overload
 654    def replace(self, expression: None) -> None: ...
 655
 656    def replace(self, expression):
 657        """
 658        Swap out this expression with a new expression.
 659
 660        For example::
 661
 662            >>> tree = Select().select("x").from_("tbl")
 663            >>> tree.find(Column).replace(column("y"))
 664            Column(
 665              this=Identifier(this=y, quoted=False))
 666            >>> tree.sql()
 667            'SELECT y FROM tbl'
 668
 669        Args:
 670            expression: new node
 671
 672        Returns:
 673            The new expression or expressions.
 674        """
 675        parent = self.parent
 676
 677        if not parent or parent is expression:
 678            return expression
 679
 680        key = self.arg_key
 681        value = parent.args.get(key)
 682
 683        if type(expression) is list and isinstance(value, Expression):
 684            # We are trying to replace an Expression with a list, so it's assumed that
 685            # the intention was to really replace the parent of this expression.
 686            value.parent.replace(expression)
 687        else:
 688            parent.set(key, expression, self.index)
 689
 690        if expression is not self:
 691            self.parent = None
 692            self.arg_key = None
 693            self.index = None
 694
 695        return expression
 696
 697    def pop(self: E) -> E:
 698        """
 699        Remove this expression from its AST.
 700
 701        Returns:
 702            The popped expression.
 703        """
 704        self.replace(None)
 705        return self
 706
 707    def assert_is(self, type_: t.Type[E]) -> E:
 708        """
 709        Assert that this `Expression` is an instance of `type_`.
 710
 711        If it is NOT an instance of `type_`, this raises an assertion error.
 712        Otherwise, this returns this expression.
 713
 714        Examples:
 715            This is useful for type security in chained expressions:
 716
 717            >>> import sqlglot
 718            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
 719            'SELECT x, z FROM y'
 720        """
 721        if not isinstance(self, type_):
 722            raise AssertionError(f"{self} is not {type_}.")
 723        return self
 724
 725    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
 726        """
 727        Checks if this expression is valid (e.g. all mandatory args are set).
 728
 729        Args:
 730            args: a sequence of values that were used to instantiate a Func expression. This is used
 731                to check that the provided arguments don't exceed the function argument limit.
 732
 733        Returns:
 734            A list of error messages for all possible errors that were found.
 735        """
 736        errors: t.List[str] = []
 737
 738        for k in self.args:
 739            if k not in self.arg_types:
 740                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
 741        for k, mandatory in self.arg_types.items():
 742            v = self.args.get(k)
 743            if mandatory and (v is None or (isinstance(v, list) and not v)):
 744                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
 745
 746        if (
 747            args
 748            and isinstance(self, Func)
 749            and len(args) > len(self.arg_types)
 750            and not self.is_var_len_args
 751        ):
 752            errors.append(
 753                f"The number of provided arguments ({len(args)}) is greater than "
 754                f"the maximum number of supported arguments ({len(self.arg_types)})"
 755            )
 756
 757        return errors
 758
 759    def dump(self):
 760        """
 761        Dump this Expression to a JSON-serializable dict.
 762        """
 763        from sqlglot.serde import dump
 764
 765        return dump(self)
 766
 767    @classmethod
 768    def load(cls, obj):
 769        """
 770        Load a dict (as returned by `Expression.dump`) into an Expression instance.
 771        """
 772        from sqlglot.serde import load
 773
 774        return load(obj)
 775
 776    def and_(
 777        self,
 778        *expressions: t.Optional[ExpOrStr],
 779        dialect: DialectType = None,
 780        copy: bool = True,
 781        wrap: bool = True,
 782        **opts,
 783    ) -> Condition:
 784        """
 785        AND this condition with one or multiple expressions.
 786
 787        Example:
 788            >>> condition("x=1").and_("y=1").sql()
 789            'x = 1 AND y = 1'
 790
 791        Args:
 792            *expressions: the SQL code strings to parse.
 793                If an `Expression` instance is passed, it will be used as-is.
 794            dialect: the dialect used to parse the input expression.
 795            copy: whether to copy the involved expressions (only applies to Expressions).
 796            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
 797                precedence issues, but can be turned off when the produced AST is too deep and
 798                causes recursion-related issues.
 799            opts: other options to use to parse the input expressions.
 800
 801        Returns:
 802            The new And condition.
 803        """
 804        return and_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)
 805
 806    def or_(
 807        self,
 808        *expressions: t.Optional[ExpOrStr],
 809        dialect: DialectType = None,
 810        copy: bool = True,
 811        wrap: bool = True,
 812        **opts,
 813    ) -> Condition:
 814        """
 815        OR this condition with one or multiple expressions.
 816
 817        Example:
 818            >>> condition("x=1").or_("y=1").sql()
 819            'x = 1 OR y = 1'
 820
 821        Args:
 822            *expressions: the SQL code strings to parse.
 823                If an `Expression` instance is passed, it will be used as-is.
 824            dialect: the dialect used to parse the input expression.
 825            copy: whether to copy the involved expressions (only applies to Expressions).
 826            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
 827                precedence issues, but can be turned off when the produced AST is too deep and
 828                causes recursion-related issues.
 829            opts: other options to use to parse the input expressions.
 830
 831        Returns:
 832            The new Or condition.
 833        """
 834        return or_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)
 835
 836    def not_(self, copy: bool = True):
 837        """
 838        Wrap this condition with NOT.
 839
 840        Example:
 841            >>> condition("x=1").not_().sql()
 842            'NOT x = 1'
 843
 844        Args:
 845            copy: whether to copy this object.
 846
 847        Returns:
 848            The new Not instance.
 849        """
 850        return not_(self, copy=copy)
 851
 852    def update_positions(
 853        self: E, other: t.Optional[Token | Expression] = None, **kwargs: t.Any
 854    ) -> E:
 855        """
 856        Update this expression with positions from a token or other expression.
 857
 858        Args:
 859            other: a token or expression to update this expression with.
 860
 861        Returns:
 862            The updated expression.
 863        """
 864        if isinstance(other, Expression):
 865            self.meta.update({k: v for k, v in other.meta.items() if k in POSITION_META_KEYS})
 866        elif other is not None:
 867            self.meta.update(
 868                {
 869                    "line": other.line,
 870                    "col": other.col,
 871                    "start": other.start,
 872                    "end": other.end,
 873                }
 874            )
 875        self.meta.update({k: v for k, v in kwargs.items() if k in POSITION_META_KEYS})
 876        return self
 877
 878    def as_(
 879        self,
 880        alias: str | Identifier,
 881        quoted: t.Optional[bool] = None,
 882        dialect: DialectType = None,
 883        copy: bool = True,
 884        **opts,
 885    ) -> Alias:
 886        return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts)
 887
 888    def _binop(self, klass: t.Type[E], other: t.Any, reverse: bool = False) -> E:
 889        this = self.copy()
 890        other = convert(other, copy=True)
 891        if not isinstance(this, klass) and not isinstance(other, klass):
 892            this = _wrap(this, Binary)
 893            other = _wrap(other, Binary)
 894        if reverse:
 895            return klass(this=other, expression=this)
 896        return klass(this=this, expression=other)
 897
 898    def __getitem__(self, other: ExpOrStr | t.Tuple[ExpOrStr]) -> Bracket:
 899        return Bracket(
 900            this=self.copy(), expressions=[convert(e, copy=True) for e in ensure_list(other)]
 901        )
 902
 903    def __iter__(self) -> t.Iterator:
 904        if "expressions" in self.arg_types:
 905            return iter(self.args.get("expressions") or [])
 906        # We define this because __getitem__ converts Expression into an iterable, which is
 907        # problematic because one can hit infinite loops if they do "for x in some_expr: ..."
 908        # See: https://peps.python.org/pep-0234/
 909        raise TypeError(f"'{self.__class__.__name__}' object is not iterable")
 910
 911    def isin(
 912        self,
 913        *expressions: t.Any,
 914        query: t.Optional[ExpOrStr] = None,
 915        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
 916        copy: bool = True,
 917        **opts,
 918    ) -> In:
 919        subquery = maybe_parse(query, copy=copy, **opts) if query else None
 920        if subquery and not isinstance(subquery, Subquery):
 921            subquery = subquery.subquery(copy=False)
 922
 923        return In(
 924            this=maybe_copy(self, copy),
 925            expressions=[convert(e, copy=copy) for e in expressions],
 926            query=subquery,
 927            unnest=(
 928                Unnest(
 929                    expressions=[
 930                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
 931                        for e in ensure_list(unnest)
 932                    ]
 933                )
 934                if unnest
 935                else None
 936            ),
 937        )
 938
 939    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
 940        return Between(
 941            this=maybe_copy(self, copy),
 942            low=convert(low, copy=copy, **opts),
 943            high=convert(high, copy=copy, **opts),
 944        )
 945
 946    def is_(self, other: ExpOrStr) -> Is:
 947        return self._binop(Is, other)
 948
 949    def like(self, other: ExpOrStr) -> Like:
 950        return self._binop(Like, other)
 951
 952    def ilike(self, other: ExpOrStr) -> ILike:
 953        return self._binop(ILike, other)
 954
 955    def eq(self, other: t.Any) -> EQ:
 956        return self._binop(EQ, other)
 957
 958    def neq(self, other: t.Any) -> NEQ:
 959        return self._binop(NEQ, other)
 960
 961    def rlike(self, other: ExpOrStr) -> RegexpLike:
 962        return self._binop(RegexpLike, other)
 963
 964    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
 965        div = self._binop(Div, other)
 966        div.args["typed"] = typed
 967        div.args["safe"] = safe
 968        return div
 969
 970    def asc(self, nulls_first: bool = True) -> Ordered:
 971        return Ordered(this=self.copy(), nulls_first=nulls_first)
 972
 973    def desc(self, nulls_first: bool = False) -> Ordered:
 974        return Ordered(this=self.copy(), desc=True, nulls_first=nulls_first)
 975
 976    def __lt__(self, other: t.Any) -> LT:
 977        return self._binop(LT, other)
 978
 979    def __le__(self, other: t.Any) -> LTE:
 980        return self._binop(LTE, other)
 981
 982    def __gt__(self, other: t.Any) -> GT:
 983        return self._binop(GT, other)
 984
 985    def __ge__(self, other: t.Any) -> GTE:
 986        return self._binop(GTE, other)
 987
 988    def __add__(self, other: t.Any) -> Add:
 989        return self._binop(Add, other)
 990
 991    def __radd__(self, other: t.Any) -> Add:
 992        return self._binop(Add, other, reverse=True)
 993
 994    def __sub__(self, other: t.Any) -> Sub:
 995        return self._binop(Sub, other)
 996
 997    def __rsub__(self, other: t.Any) -> Sub:
 998        return self._binop(Sub, other, reverse=True)
 999
1000    def __mul__(self, other: t.Any) -> Mul:
1001        return self._binop(Mul, other)
1002
1003    def __rmul__(self, other: t.Any) -> Mul:
1004        return self._binop(Mul, other, reverse=True)
1005
1006    def __truediv__(self, other: t.Any) -> Div:
1007        return self._binop(Div, other)
1008
1009    def __rtruediv__(self, other: t.Any) -> Div:
1010        return self._binop(Div, other, reverse=True)
1011
1012    def __floordiv__(self, other: t.Any) -> IntDiv:
1013        return self._binop(IntDiv, other)
1014
1015    def __rfloordiv__(self, other: t.Any) -> IntDiv:
1016        return self._binop(IntDiv, other, reverse=True)
1017
1018    def __mod__(self, other: t.Any) -> Mod:
1019        return self._binop(Mod, other)
1020
1021    def __rmod__(self, other: t.Any) -> Mod:
1022        return self._binop(Mod, other, reverse=True)
1023
1024    def __pow__(self, other: t.Any) -> Pow:
1025        return self._binop(Pow, other)
1026
1027    def __rpow__(self, other: t.Any) -> Pow:
1028        return self._binop(Pow, other, reverse=True)
1029
1030    def __and__(self, other: t.Any) -> And:
1031        return self._binop(And, other)
1032
1033    def __rand__(self, other: t.Any) -> And:
1034        return self._binop(And, other, reverse=True)
1035
1036    def __or__(self, other: t.Any) -> Or:
1037        return self._binop(Or, other)
1038
1039    def __ror__(self, other: t.Any) -> Or:
1040        return self._binop(Or, other, reverse=True)
1041
1042    def __neg__(self) -> Neg:
1043        return Neg(this=_wrap(self.copy(), Binary))
1044
1045    def __invert__(self) -> Not:
1046        return not_(self.copy())

The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary context, such as its child expressions, their names (arg keys), and whether a given child expression is optional or not.

Attributes:
  • key: a unique key for each class in the Expression hierarchy. This is useful for hashing and representing expressions as strings.
  • arg_types: determines the arguments (child nodes) supported by an expression. It maps arg keys to booleans that indicate whether the corresponding args are optional.
  • parent: a reference to the parent expression (or None, in case of root expressions).
  • arg_key: the arg key an expression is associated with, i.e. the name its parent expression uses to refer to it.
  • index: the index of an expression if it is inside of a list argument in its parent.
  • comments: a list of comments that are associated with a given expression. This is used in order to preserve comments when transpiling SQL code.
  • type: the sqlglot.expressions.DataType type of an expression. This is inferred by the optimizer, in order to enable some transformations that require type information.
  • meta: a dictionary that can be used to store useful metadata for a given expression.
Example:
>>> class Foo(Expression):
...     arg_types = {"this": True, "expression": False}

The above definition informs us that Foo is an Expression that requires an argument called "this" and may also optionally receive an argument called "expression".

Arguments:
  • args: a mapping used for retrieving the arguments of an expression, given their arg keys.
Expression(**args: Any)
108    def __init__(self, **args: t.Any):
109        self.args: t.Dict[str, t.Any] = args
110        self.parent: t.Optional[Expression] = None
111        self.arg_key: t.Optional[str] = None
112        self.index: t.Optional[int] = None
113        self.comments: t.Optional[t.List[str]] = None
114        self._type: t.Optional[DataType] = None
115        self._meta: t.Optional[t.Dict[str, t.Any]] = None
116        self._hash: t.Optional[int] = None
117
118        for arg_key, value in self.args.items():
119            self._set_parent(arg_key, value)
key = 'expression'
arg_types = {'this': True}
args: Dict[str, Any]
parent: Optional[Expression]
arg_key: Optional[str]
index: Optional[int]
comments: Optional[List[str]]
hashable_args: Any
124    @property
125    def hashable_args(self) -> t.Any:
126        return frozenset(
127            (k, tuple(_norm_arg(a) for a in v) if type(v) is list else _norm_arg(v))
128            for k, v in self.args.items()
129            if not (v is None or v is False or (type(v) is list and not v))
130        )
this: Any
138    @property
139    def this(self) -> t.Any:
140        """
141        Retrieves the argument with key "this".
142        """
143        return self.args.get("this")

Retrieves the argument with key "this".

expression: Any
145    @property
146    def expression(self) -> t.Any:
147        """
148        Retrieves the argument with key "expression".
149        """
150        return self.args.get("expression")

Retrieves the argument with key "expression".

expressions: List[Any]
152    @property
153    def expressions(self) -> t.List[t.Any]:
154        """
155        Retrieves the argument with key "expressions".
156        """
157        return self.args.get("expressions") or []

Retrieves the argument with key "expressions".

def text(self, key) -> str:
159    def text(self, key) -> str:
160        """
161        Returns a textual representation of the argument corresponding to "key". This can only be used
162        for args that are strings or leaf Expression instances, such as identifiers and literals.
163        """
164        field = self.args.get(key)
165        if isinstance(field, str):
166            return field
167        if isinstance(field, (Identifier, Literal, Var)):
168            return field.this
169        if isinstance(field, (Star, Null)):
170            return field.name
171        return ""

Returns a textual representation of the argument corresponding to "key". This can only be used for args that are strings or leaf Expression instances, such as identifiers and literals.

is_string: bool
173    @property
174    def is_string(self) -> bool:
175        """
176        Checks whether a Literal expression is a string.
177        """
178        return isinstance(self, Literal) and self.args["is_string"]

Checks whether a Literal expression is a string.

is_number: bool
180    @property
181    def is_number(self) -> bool:
182        """
183        Checks whether a Literal expression is a number.
184        """
185        return (isinstance(self, Literal) and not self.args["is_string"]) or (
186            isinstance(self, Neg) and self.this.is_number
187        )

Checks whether a Literal expression is a number.

def to_py(self) -> Any:
189    def to_py(self) -> t.Any:
190        """
191        Returns a Python object equivalent of the SQL node.
192        """
193        raise ValueError(f"{self} cannot be converted to a Python object.")

Returns a Python object equivalent of the SQL node.

is_int: bool
195    @property
196    def is_int(self) -> bool:
197        """
198        Checks whether an expression is an integer.
199        """
200        return self.is_number and isinstance(self.to_py(), int)

Checks whether an expression is an integer.

is_star: bool
202    @property
203    def is_star(self) -> bool:
204        """Checks whether an expression is a star."""
205        return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star))

Checks whether an expression is a star.

alias: str
207    @property
208    def alias(self) -> str:
209        """
210        Returns the alias of the expression, or an empty string if it's not aliased.
211        """
212        if isinstance(self.args.get("alias"), TableAlias):
213            return self.args["alias"].name
214        return self.text("alias")

Returns the alias of the expression, or an empty string if it's not aliased.

alias_column_names: List[str]
216    @property
217    def alias_column_names(self) -> t.List[str]:
218        table_alias = self.args.get("alias")
219        if not table_alias:
220            return []
221        return [c.name for c in table_alias.args.get("columns") or []]
name: str
223    @property
224    def name(self) -> str:
225        return self.text("this")
alias_or_name: str
227    @property
228    def alias_or_name(self) -> str:
229        return self.alias or self.name
output_name: str
231    @property
232    def output_name(self) -> str:
233        """
234        Name of the output column if this expression is a selection.
235
236        If the Expression has no output name, an empty string is returned.
237
238        Example:
239            >>> from sqlglot import parse_one
240            >>> parse_one("SELECT a").expressions[0].output_name
241            'a'
242            >>> parse_one("SELECT b AS c").expressions[0].output_name
243            'c'
244            >>> parse_one("SELECT 1 + 2").expressions[0].output_name
245            ''
246        """
247        return ""

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
type: Optional[DataType]
249    @property
250    def type(self) -> t.Optional[DataType]:
251        return self._type
def is_type(self, *dtypes) -> bool:
259    def is_type(self, *dtypes) -> bool:
260        return self.type is not None and self.type.is_type(*dtypes)
def is_leaf(self) -> bool:
262    def is_leaf(self) -> bool:
263        return not any(isinstance(v, (Expression, list)) for v in self.args.values())
meta: Dict[str, Any]
265    @property
266    def meta(self) -> t.Dict[str, t.Any]:
267        if self._meta is None:
268            self._meta = {}
269        return self._meta
def copy(self) -> typing_extensions.Self:
305    def copy(self) -> Self:
306        """
307        Returns a deep copy of the expression.
308        """
309        return deepcopy(self)

Returns a deep copy of the expression.

def add_comments( self, comments: Optional[List[str]] = None, prepend: bool = False) -> None:
311    def add_comments(self, comments: t.Optional[t.List[str]] = None, prepend: bool = False) -> None:
312        if self.comments is None:
313            self.comments = []
314
315        if comments:
316            for comment in comments:
317                _, *meta = comment.split(SQLGLOT_META)
318                if meta:
319                    for kv in "".join(meta).split(","):
320                        k, *v = kv.split("=")
321                        value = v[0].strip() if v else True
322                        self.meta[k.strip()] = to_bool(value)
323
324                if not prepend:
325                    self.comments.append(comment)
326
327            if prepend:
328                self.comments = comments + self.comments
def pop_comments(self) -> List[str]:
330    def pop_comments(self) -> t.List[str]:
331        comments = self.comments or []
332        self.comments = None
333        return comments
def append(self, arg_key: str, value: Any) -> None:
335    def append(self, arg_key: str, value: t.Any) -> None:
336        """
337        Appends value to arg_key if it's a list or sets it as a new list.
338
339        Args:
340            arg_key (str): name of the list expression arg
341            value (Any): value to append to the list
342        """
343        if type(self.args.get(arg_key)) is not list:
344            self.args[arg_key] = []
345        self._set_parent(arg_key, value)
346        values = self.args[arg_key]
347        if hasattr(value, "parent"):
348            value.index = len(values)
349        values.append(value)

Appends value to arg_key if it's a list or sets it as a new list.

Arguments:
  • arg_key (str): name of the list expression arg
  • value (Any): value to append to the list
def set( self, arg_key: str, value: Any, index: Optional[int] = None, overwrite: bool = True) -> None:
351    def set(
352        self,
353        arg_key: str,
354        value: t.Any,
355        index: t.Optional[int] = None,
356        overwrite: bool = True,
357    ) -> None:
358        """
359        Sets arg_key to value.
360
361        Args:
362            arg_key: name of the expression arg.
363            value: value to set the arg to.
364            index: if the arg is a list, this specifies what position to add the value in it.
365            overwrite: assuming an index is given, this determines whether to overwrite the
366                list entry instead of only inserting a new value (i.e., like list.insert).
367        """
368        if index is not None:
369            expressions = self.args.get(arg_key) or []
370
371            if seq_get(expressions, index) is None:
372                return
373            if value is None:
374                expressions.pop(index)
375                for v in expressions[index:]:
376                    v.index = v.index - 1
377                return
378
379            if isinstance(value, list):
380                expressions.pop(index)
381                expressions[index:index] = value
382            elif overwrite:
383                expressions[index] = value
384            else:
385                expressions.insert(index, value)
386
387            value = expressions
388        elif value is None:
389            self.args.pop(arg_key, None)
390            return
391
392        self.args[arg_key] = value
393        self._set_parent(arg_key, value, index)

Sets arg_key to value.

Arguments:
  • arg_key: name of the expression arg.
  • value: value to set the arg to.
  • index: if the arg is a list, this specifies what position to add the value in it.
  • overwrite: assuming an index is given, this determines whether to overwrite the list entry instead of only inserting a new value (i.e., like list.insert).
depth: int
407    @property
408    def depth(self) -> int:
409        """
410        Returns the depth of this tree.
411        """
412        if self.parent:
413            return self.parent.depth + 1
414        return 0

Returns the depth of this tree.

def iter_expressions(self, reverse: bool = False) -> Iterator[Expression]:
416    def iter_expressions(self, reverse: bool = False) -> t.Iterator[Expression]:
417        """Yields the key and expression for all arguments, exploding list args."""
418        for vs in reversed(self.args.values()) if reverse else self.args.values():  # type: ignore
419            if type(vs) is list:
420                for v in reversed(vs) if reverse else vs:  # type: ignore
421                    if hasattr(v, "parent"):
422                        yield v
423            else:
424                if hasattr(vs, "parent"):
425                    yield vs

Yields the key and expression for all arguments, exploding list args.

def find(self, *expression_types: Type[~E], bfs: bool = True) -> Optional[~E]:
427    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
428        """
429        Returns the first node in this tree which matches at least one of
430        the specified types.
431
432        Args:
433            expression_types: the expression type(s) to match.
434            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
435
436        Returns:
437            The node which matches the criteria or None if no such node was found.
438        """
439        return next(self.find_all(*expression_types, bfs=bfs), None)

Returns the first node in this tree which matches at least one of the specified types.

Arguments:
  • expression_types: the expression type(s) to match.
  • bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
Returns:

The node which matches the criteria or None if no such node was found.

def find_all(self, *expression_types: Type[~E], bfs: bool = True) -> Iterator[~E]:
441    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
442        """
443        Returns a generator object which visits all nodes in this tree and only
444        yields those that match at least one of the specified expression types.
445
446        Args:
447            expression_types: the expression type(s) to match.
448            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
449
450        Returns:
451            The generator object.
452        """
453        for expression in self.walk(bfs=bfs):
454            if isinstance(expression, expression_types):
455                yield expression

Returns a generator object which visits all nodes in this tree and only yields those that match at least one of the specified expression types.

Arguments:
  • expression_types: the expression type(s) to match.
  • bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
Returns:

The generator object.

def find_ancestor(self, *expression_types: Type[~E]) -> Optional[~E]:
457    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
458        """
459        Returns a nearest parent matching expression_types.
460
461        Args:
462            expression_types: the expression type(s) to match.
463
464        Returns:
465            The parent node.
466        """
467        ancestor = self.parent
468        while ancestor and not isinstance(ancestor, expression_types):
469            ancestor = ancestor.parent
470        return ancestor  # type: ignore

Returns a nearest parent matching expression_types.

Arguments:
  • expression_types: the expression type(s) to match.
Returns:

The parent node.

parent_select: Optional[Select]
472    @property
473    def parent_select(self) -> t.Optional[Select]:
474        """
475        Returns the parent select statement.
476        """
477        return self.find_ancestor(Select)

Returns the parent select statement.

same_parent: bool
479    @property
480    def same_parent(self) -> bool:
481        """Returns if the parent is the same class as itself."""
482        return type(self.parent) is self.__class__

Returns if the parent is the same class as itself.

def root(self) -> Expression:
484    def root(self) -> Expression:
485        """
486        Returns the root expression of this tree.
487        """
488        expression = self
489        while expression.parent:
490            expression = expression.parent
491        return expression

Returns the root expression of this tree.

def walk( self, bfs: bool = True, prune: Optional[Callable[[Expression], bool]] = None) -> Iterator[Expression]:
493    def walk(
494        self, bfs: bool = True, prune: t.Optional[t.Callable[[Expression], bool]] = None
495    ) -> t.Iterator[Expression]:
496        """
497        Returns a generator object which visits all nodes in this tree.
498
499        Args:
500            bfs: if set to True the BFS traversal order will be applied,
501                otherwise the DFS traversal will be used instead.
502            prune: callable that returns True if the generator should stop traversing
503                this branch of the tree.
504
505        Returns:
506            the generator object.
507        """
508        if bfs:
509            yield from self.bfs(prune=prune)
510        else:
511            yield from self.dfs(prune=prune)

Returns a generator object which visits all nodes in this tree.

Arguments:
  • bfs: if set to True the BFS traversal order will be applied, otherwise the DFS traversal will be used instead.
  • prune: callable that returns True if the generator should stop traversing this branch of the tree.
Returns:

the generator object.

def dfs( self, prune: Optional[Callable[[Expression], bool]] = None) -> Iterator[Expression]:
513    def dfs(
514        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
515    ) -> t.Iterator[Expression]:
516        """
517        Returns a generator object which visits all nodes in this tree in
518        the DFS (Depth-first) order.
519
520        Returns:
521            The generator object.
522        """
523        stack = [self]
524
525        while stack:
526            node = stack.pop()
527
528            yield node
529
530            if prune and prune(node):
531                continue
532
533            for v in node.iter_expressions(reverse=True):
534                stack.append(v)

Returns a generator object which visits all nodes in this tree in the DFS (Depth-first) order.

Returns:

The generator object.

def bfs( self, prune: Optional[Callable[[Expression], bool]] = None) -> Iterator[Expression]:
536    def bfs(
537        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
538    ) -> t.Iterator[Expression]:
539        """
540        Returns a generator object which visits all nodes in this tree in
541        the BFS (Breadth-first) order.
542
543        Returns:
544            The generator object.
545        """
546        queue = deque([self])
547
548        while queue:
549            node = queue.popleft()
550
551            yield node
552
553            if prune and prune(node):
554                continue
555
556            for v in node.iter_expressions():
557                queue.append(v)

Returns a generator object which visits all nodes in this tree in the BFS (Breadth-first) order.

Returns:

The generator object.

def unnest(self):
559    def unnest(self):
560        """
561        Returns the first non parenthesis child or self.
562        """
563        expression = self
564        while type(expression) is Paren:
565            expression = expression.this
566        return expression

Returns the first non parenthesis child or self.

def unalias(self):
568    def unalias(self):
569        """
570        Returns the inner expression if this is an Alias.
571        """
572        if isinstance(self, Alias):
573            return self.this
574        return self

Returns the inner expression if this is an Alias.

def unnest_operands(self):
576    def unnest_operands(self):
577        """
578        Returns unnested operands as a tuple.
579        """
580        return tuple(arg.unnest() for arg in self.iter_expressions())

Returns unnested operands as a tuple.

def flatten(self, unnest=True):
582    def flatten(self, unnest=True):
583        """
584        Returns a generator which yields child nodes whose parents are the same class.
585
586        A AND B AND C -> [A, B, C]
587        """
588        for node in self.dfs(prune=lambda n: n.parent and type(n) is not self.__class__):
589            if type(node) is not self.__class__:
590                yield node.unnest() if unnest and not isinstance(node, Subquery) else node

Returns a generator which yields child nodes whose parents are the same class.

A AND B AND C -> [A, B, C]

def to_s(self) -> str:
598    def to_s(self) -> str:
599        """
600        Same as __repr__, but includes additional information which can be useful
601        for debugging, like empty or missing args and the AST nodes' object IDs.
602        """
603        return _to_s(self, verbose=True)

Same as __repr__, but includes additional information which can be useful for debugging, like empty or missing args and the AST nodes' object IDs.

def sql( self, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> str:
605    def sql(self, dialect: DialectType = None, **opts) -> str:
606        """
607        Returns SQL string representation of this tree.
608
609        Args:
610            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
611            opts: other `sqlglot.generator.Generator` options.
612
613        Returns:
614            The SQL string.
615        """
616        from sqlglot.dialects import Dialect
617
618        return Dialect.get_or_raise(dialect).generate(self, **opts)

Returns SQL string representation of this tree.

Arguments:
  • dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
  • opts: other sqlglot.generator.Generator options.
Returns:

The SQL string.

def transform( self, fun: Callable, *args: Any, copy: bool = True, **kwargs) -> Expression:
620    def transform(self, fun: t.Callable, *args: t.Any, copy: bool = True, **kwargs) -> Expression:
621        """
622        Visits all tree nodes (excluding already transformed ones)
623        and applies the given transformation function to each node.
624
625        Args:
626            fun: a function which takes a node as an argument and returns a
627                new transformed node or the same node without modifications. If the function
628                returns None, then the corresponding node will be removed from the syntax tree.
629            copy: if set to True a new tree instance is constructed, otherwise the tree is
630                modified in place.
631
632        Returns:
633            The transformed tree.
634        """
635        root = None
636        new_node = None
637
638        for node in (self.copy() if copy else self).dfs(prune=lambda n: n is not new_node):
639            parent, arg_key, index = node.parent, node.arg_key, node.index
640            new_node = fun(node, *args, **kwargs)
641
642            if not root:
643                root = new_node
644            elif parent and arg_key and new_node is not node:
645                parent.set(arg_key, new_node, index)
646
647        assert root
648        return root.assert_is(Expression)

Visits all tree nodes (excluding already transformed ones) and applies the given transformation function to each node.

Arguments:
  • fun: a function which takes a node as an argument and returns a new transformed node or the same node without modifications. If the function returns None, then the corresponding node will be removed from the syntax tree.
  • copy: if set to True a new tree instance is constructed, otherwise the tree is modified in place.
Returns:

The transformed tree.

def replace(self, expression):
656    def replace(self, expression):
657        """
658        Swap out this expression with a new expression.
659
660        For example::
661
662            >>> tree = Select().select("x").from_("tbl")
663            >>> tree.find(Column).replace(column("y"))
664            Column(
665              this=Identifier(this=y, quoted=False))
666            >>> tree.sql()
667            'SELECT y FROM tbl'
668
669        Args:
670            expression: new node
671
672        Returns:
673            The new expression or expressions.
674        """
675        parent = self.parent
676
677        if not parent or parent is expression:
678            return expression
679
680        key = self.arg_key
681        value = parent.args.get(key)
682
683        if type(expression) is list and isinstance(value, Expression):
684            # We are trying to replace an Expression with a list, so it's assumed that
685            # the intention was to really replace the parent of this expression.
686            value.parent.replace(expression)
687        else:
688            parent.set(key, expression, self.index)
689
690        if expression is not self:
691            self.parent = None
692            self.arg_key = None
693            self.index = None
694
695        return expression

Swap out this expression with a new expression.

For example::

>>> tree = Select().select("x").from_("tbl")
>>> tree.find(Column).replace(column("y"))
Column(
  this=Identifier(this=y, quoted=False))
>>> tree.sql()
'SELECT y FROM tbl'
Arguments:
  • expression: new node
Returns:

The new expression or expressions.

def pop(self: ~E) -> ~E:
697    def pop(self: E) -> E:
698        """
699        Remove this expression from its AST.
700
701        Returns:
702            The popped expression.
703        """
704        self.replace(None)
705        return self

Remove this expression from its AST.

Returns:

The popped expression.

def assert_is(self, type_: Type[~E]) -> ~E:
707    def assert_is(self, type_: t.Type[E]) -> E:
708        """
709        Assert that this `Expression` is an instance of `type_`.
710
711        If it is NOT an instance of `type_`, this raises an assertion error.
712        Otherwise, this returns this expression.
713
714        Examples:
715            This is useful for type security in chained expressions:
716
717            >>> import sqlglot
718            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
719            'SELECT x, z FROM y'
720        """
721        if not isinstance(self, type_):
722            raise AssertionError(f"{self} is not {type_}.")
723        return self

Assert that this Expression is an instance of type_.

If it is NOT an instance of type_, this raises an assertion error. Otherwise, this returns this expression.

Examples:

This is useful for type security in chained expressions:

>>> import sqlglot
>>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
'SELECT x, z FROM y'
def error_messages(self, args: Optional[Sequence] = None) -> List[str]:
725    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
726        """
727        Checks if this expression is valid (e.g. all mandatory args are set).
728
729        Args:
730            args: a sequence of values that were used to instantiate a Func expression. This is used
731                to check that the provided arguments don't exceed the function argument limit.
732
733        Returns:
734            A list of error messages for all possible errors that were found.
735        """
736        errors: t.List[str] = []
737
738        for k in self.args:
739            if k not in self.arg_types:
740                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
741        for k, mandatory in self.arg_types.items():
742            v = self.args.get(k)
743            if mandatory and (v is None or (isinstance(v, list) and not v)):
744                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
745
746        if (
747            args
748            and isinstance(self, Func)
749            and len(args) > len(self.arg_types)
750            and not self.is_var_len_args
751        ):
752            errors.append(
753                f"The number of provided arguments ({len(args)}) is greater than "
754                f"the maximum number of supported arguments ({len(self.arg_types)})"
755            )
756
757        return errors

Checks if this expression is valid (e.g. all mandatory args are set).

Arguments:
  • args: a sequence of values that were used to instantiate a Func expression. This is used to check that the provided arguments don't exceed the function argument limit.
Returns:

A list of error messages for all possible errors that were found.

def dump(self):
759    def dump(self):
760        """
761        Dump this Expression to a JSON-serializable dict.
762        """
763        from sqlglot.serde import dump
764
765        return dump(self)

Dump this Expression to a JSON-serializable dict.

@classmethod
def load(cls, obj):
767    @classmethod
768    def load(cls, obj):
769        """
770        Load a dict (as returned by `Expression.dump`) into an Expression instance.
771        """
772        from sqlglot.serde import load
773
774        return load(obj)

Load a dict (as returned by Expression.dump) into an Expression instance.

def and_( self, *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
776    def and_(
777        self,
778        *expressions: t.Optional[ExpOrStr],
779        dialect: DialectType = None,
780        copy: bool = True,
781        wrap: bool = True,
782        **opts,
783    ) -> Condition:
784        """
785        AND this condition with one or multiple expressions.
786
787        Example:
788            >>> condition("x=1").and_("y=1").sql()
789            'x = 1 AND y = 1'
790
791        Args:
792            *expressions: the SQL code strings to parse.
793                If an `Expression` instance is passed, it will be used as-is.
794            dialect: the dialect used to parse the input expression.
795            copy: whether to copy the involved expressions (only applies to Expressions).
796            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
797                precedence issues, but can be turned off when the produced AST is too deep and
798                causes recursion-related issues.
799            opts: other options to use to parse the input expressions.
800
801        Returns:
802            The new And condition.
803        """
804        return and_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)

AND this condition with one or multiple expressions.

Example:
>>> condition("x=1").and_("y=1").sql()
'x = 1 AND y = 1'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy the involved expressions (only applies to Expressions).
  • wrap: whether to wrap the operands in Parens. This is true by default to avoid precedence issues, but can be turned off when the produced AST is too deep and causes recursion-related issues.
  • opts: other options to use to parse the input expressions.
Returns:

The new And condition.

def or_( self, *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
806    def or_(
807        self,
808        *expressions: t.Optional[ExpOrStr],
809        dialect: DialectType = None,
810        copy: bool = True,
811        wrap: bool = True,
812        **opts,
813    ) -> Condition:
814        """
815        OR this condition with one or multiple expressions.
816
817        Example:
818            >>> condition("x=1").or_("y=1").sql()
819            'x = 1 OR y = 1'
820
821        Args:
822            *expressions: the SQL code strings to parse.
823                If an `Expression` instance is passed, it will be used as-is.
824            dialect: the dialect used to parse the input expression.
825            copy: whether to copy the involved expressions (only applies to Expressions).
826            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
827                precedence issues, but can be turned off when the produced AST is too deep and
828                causes recursion-related issues.
829            opts: other options to use to parse the input expressions.
830
831        Returns:
832            The new Or condition.
833        """
834        return or_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)

OR this condition with one or multiple expressions.

Example:
>>> condition("x=1").or_("y=1").sql()
'x = 1 OR y = 1'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy the involved expressions (only applies to Expressions).
  • wrap: whether to wrap the operands in Parens. This is true by default to avoid precedence issues, but can be turned off when the produced AST is too deep and causes recursion-related issues.
  • opts: other options to use to parse the input expressions.
Returns:

The new Or condition.

def not_(self, copy: bool = True):
836    def not_(self, copy: bool = True):
837        """
838        Wrap this condition with NOT.
839
840        Example:
841            >>> condition("x=1").not_().sql()
842            'NOT x = 1'
843
844        Args:
845            copy: whether to copy this object.
846
847        Returns:
848            The new Not instance.
849        """
850        return not_(self, copy=copy)

Wrap this condition with NOT.

Example:
>>> condition("x=1").not_().sql()
'NOT x = 1'
Arguments:
  • copy: whether to copy this object.
Returns:

The new Not instance.

def update_positions( self: ~E, other: Union[sqlglot.tokens.Token, Expression, NoneType] = None, **kwargs: Any) -> ~E:
852    def update_positions(
853        self: E, other: t.Optional[Token | Expression] = None, **kwargs: t.Any
854    ) -> E:
855        """
856        Update this expression with positions from a token or other expression.
857
858        Args:
859            other: a token or expression to update this expression with.
860
861        Returns:
862            The updated expression.
863        """
864        if isinstance(other, Expression):
865            self.meta.update({k: v for k, v in other.meta.items() if k in POSITION_META_KEYS})
866        elif other is not None:
867            self.meta.update(
868                {
869                    "line": other.line,
870                    "col": other.col,
871                    "start": other.start,
872                    "end": other.end,
873                }
874            )
875        self.meta.update({k: v for k, v in kwargs.items() if k in POSITION_META_KEYS})
876        return self

Update this expression with positions from a token or other expression.

Arguments:
  • other: a token or expression to update this expression with.
Returns:

The updated expression.

def as_( self, alias: str | Identifier, quoted: Optional[bool] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Alias:
878    def as_(
879        self,
880        alias: str | Identifier,
881        quoted: t.Optional[bool] = None,
882        dialect: DialectType = None,
883        copy: bool = True,
884        **opts,
885    ) -> Alias:
886        return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts)
def isin( self, *expressions: Any, query: Union[str, Expression, NoneType] = None, unnest: Union[str, Expression, NoneType, Collection[Union[str, Expression]]] = None, copy: bool = True, **opts) -> In:
911    def isin(
912        self,
913        *expressions: t.Any,
914        query: t.Optional[ExpOrStr] = None,
915        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
916        copy: bool = True,
917        **opts,
918    ) -> In:
919        subquery = maybe_parse(query, copy=copy, **opts) if query else None
920        if subquery and not isinstance(subquery, Subquery):
921            subquery = subquery.subquery(copy=False)
922
923        return In(
924            this=maybe_copy(self, copy),
925            expressions=[convert(e, copy=copy) for e in expressions],
926            query=subquery,
927            unnest=(
928                Unnest(
929                    expressions=[
930                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
931                        for e in ensure_list(unnest)
932                    ]
933                )
934                if unnest
935                else None
936            ),
937        )
def between( self, low: Any, high: Any, copy: bool = True, **opts) -> Between:
939    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
940        return Between(
941            this=maybe_copy(self, copy),
942            low=convert(low, copy=copy, **opts),
943            high=convert(high, copy=copy, **opts),
944        )
def is_( self, other: Union[str, Expression]) -> Is:
946    def is_(self, other: ExpOrStr) -> Is:
947        return self._binop(Is, other)
def like( self, other: Union[str, Expression]) -> Like:
949    def like(self, other: ExpOrStr) -> Like:
950        return self._binop(Like, other)
def ilike( self, other: Union[str, Expression]) -> ILike:
952    def ilike(self, other: ExpOrStr) -> ILike:
953        return self._binop(ILike, other)
def eq(self, other: Any) -> EQ:
955    def eq(self, other: t.Any) -> EQ:
956        return self._binop(EQ, other)
def neq(self, other: Any) -> NEQ:
958    def neq(self, other: t.Any) -> NEQ:
959        return self._binop(NEQ, other)
def rlike( self, other: Union[str, Expression]) -> RegexpLike:
961    def rlike(self, other: ExpOrStr) -> RegexpLike:
962        return self._binop(RegexpLike, other)
def div( self, other: Union[str, Expression], typed: bool = False, safe: bool = False) -> Div:
964    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
965        div = self._binop(Div, other)
966        div.args["typed"] = typed
967        div.args["safe"] = safe
968        return div
def asc(self, nulls_first: bool = True) -> Ordered:
970    def asc(self, nulls_first: bool = True) -> Ordered:
971        return Ordered(this=self.copy(), nulls_first=nulls_first)
def desc(self, nulls_first: bool = False) -> Ordered:
973    def desc(self, nulls_first: bool = False) -> Ordered:
974        return Ordered(this=self.copy(), desc=True, nulls_first=nulls_first)
IntoType = typing.Union[str, typing.Type[Expression], typing.Collection[typing.Union[str, typing.Type[Expression]]]]
ExpOrStr = typing.Union[str, Expression]
class Condition(Expression):
1057class Condition(Expression):
1058    """Logical conditions like x AND y, or simply x"""

Logical conditions like x AND y, or simply x

key = 'condition'
class Predicate(Condition):
1061class Predicate(Condition):
1062    """Relationships like x = y, x > 1, x >= y."""

Relationships like x = y, x > 1, x >= y.

key = 'predicate'
class DerivedTable(Expression):
1065class DerivedTable(Expression):
1066    @property
1067    def selects(self) -> t.List[Expression]:
1068        return self.this.selects if isinstance(self.this, Query) else []
1069
1070    @property
1071    def named_selects(self) -> t.List[str]:
1072        return [select.output_name for select in self.selects]
selects: List[Expression]
1066    @property
1067    def selects(self) -> t.List[Expression]:
1068        return self.this.selects if isinstance(self.this, Query) else []
named_selects: List[str]
1070    @property
1071    def named_selects(self) -> t.List[str]:
1072        return [select.output_name for select in self.selects]
key = 'derivedtable'
class Query(Expression):
1075class Query(Expression):
1076    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
1077        """
1078        Returns a `Subquery` that wraps around this query.
1079
1080        Example:
1081            >>> subquery = Select().select("x").from_("tbl").subquery()
1082            >>> Select().select("x").from_(subquery).sql()
1083            'SELECT x FROM (SELECT x FROM tbl)'
1084
1085        Args:
1086            alias: an optional alias for the subquery.
1087            copy: if `False`, modify this expression instance in-place.
1088        """
1089        instance = maybe_copy(self, copy)
1090        if not isinstance(alias, Expression):
1091            alias = TableAlias(this=to_identifier(alias)) if alias else None
1092
1093        return Subquery(this=instance, alias=alias)
1094
1095    def limit(
1096        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1097    ) -> Q:
1098        """
1099        Adds a LIMIT clause to this query.
1100
1101        Example:
1102            >>> select("1").union(select("1")).limit(1).sql()
1103            'SELECT 1 UNION SELECT 1 LIMIT 1'
1104
1105        Args:
1106            expression: the SQL code string to parse.
1107                This can also be an integer.
1108                If a `Limit` instance is passed, it will be used as-is.
1109                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
1110            dialect: the dialect used to parse the input expression.
1111            copy: if `False`, modify this expression instance in-place.
1112            opts: other options to use to parse the input expressions.
1113
1114        Returns:
1115            A limited Select expression.
1116        """
1117        return _apply_builder(
1118            expression=expression,
1119            instance=self,
1120            arg="limit",
1121            into=Limit,
1122            prefix="LIMIT",
1123            dialect=dialect,
1124            copy=copy,
1125            into_arg="expression",
1126            **opts,
1127        )
1128
1129    def offset(
1130        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1131    ) -> Q:
1132        """
1133        Set the OFFSET expression.
1134
1135        Example:
1136            >>> Select().from_("tbl").select("x").offset(10).sql()
1137            'SELECT x FROM tbl OFFSET 10'
1138
1139        Args:
1140            expression: the SQL code string to parse.
1141                This can also be an integer.
1142                If a `Offset` instance is passed, this is used as-is.
1143                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
1144            dialect: the dialect used to parse the input expression.
1145            copy: if `False`, modify this expression instance in-place.
1146            opts: other options to use to parse the input expressions.
1147
1148        Returns:
1149            The modified Select expression.
1150        """
1151        return _apply_builder(
1152            expression=expression,
1153            instance=self,
1154            arg="offset",
1155            into=Offset,
1156            prefix="OFFSET",
1157            dialect=dialect,
1158            copy=copy,
1159            into_arg="expression",
1160            **opts,
1161        )
1162
1163    def order_by(
1164        self: Q,
1165        *expressions: t.Optional[ExpOrStr],
1166        append: bool = True,
1167        dialect: DialectType = None,
1168        copy: bool = True,
1169        **opts,
1170    ) -> Q:
1171        """
1172        Set the ORDER BY expression.
1173
1174        Example:
1175            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
1176            'SELECT x FROM tbl ORDER BY x DESC'
1177
1178        Args:
1179            *expressions: the SQL code strings to parse.
1180                If a `Group` instance is passed, this is used as-is.
1181                If another `Expression` instance is passed, it will be wrapped in a `Order`.
1182            append: if `True`, add to any existing expressions.
1183                Otherwise, this flattens all the `Order` expression into a single expression.
1184            dialect: the dialect used to parse the input expression.
1185            copy: if `False`, modify this expression instance in-place.
1186            opts: other options to use to parse the input expressions.
1187
1188        Returns:
1189            The modified Select expression.
1190        """
1191        return _apply_child_list_builder(
1192            *expressions,
1193            instance=self,
1194            arg="order",
1195            append=append,
1196            copy=copy,
1197            prefix="ORDER BY",
1198            into=Order,
1199            dialect=dialect,
1200            **opts,
1201        )
1202
1203    @property
1204    def ctes(self) -> t.List[CTE]:
1205        """Returns a list of all the CTEs attached to this query."""
1206        with_ = self.args.get("with")
1207        return with_.expressions if with_ else []
1208
1209    @property
1210    def selects(self) -> t.List[Expression]:
1211        """Returns the query's projections."""
1212        raise NotImplementedError("Query objects must implement `selects`")
1213
1214    @property
1215    def named_selects(self) -> t.List[str]:
1216        """Returns the output names of the query's projections."""
1217        raise NotImplementedError("Query objects must implement `named_selects`")
1218
1219    def select(
1220        self: Q,
1221        *expressions: t.Optional[ExpOrStr],
1222        append: bool = True,
1223        dialect: DialectType = None,
1224        copy: bool = True,
1225        **opts,
1226    ) -> Q:
1227        """
1228        Append to or set the SELECT expressions.
1229
1230        Example:
1231            >>> Select().select("x", "y").sql()
1232            'SELECT x, y'
1233
1234        Args:
1235            *expressions: the SQL code strings to parse.
1236                If an `Expression` instance is passed, it will be used as-is.
1237            append: if `True`, add to any existing expressions.
1238                Otherwise, this resets the expressions.
1239            dialect: the dialect used to parse the input expressions.
1240            copy: if `False`, modify this expression instance in-place.
1241            opts: other options to use to parse the input expressions.
1242
1243        Returns:
1244            The modified Query expression.
1245        """
1246        raise NotImplementedError("Query objects must implement `select`")
1247
1248    def where(
1249        self: Q,
1250        *expressions: t.Optional[ExpOrStr],
1251        append: bool = True,
1252        dialect: DialectType = None,
1253        copy: bool = True,
1254        **opts,
1255    ) -> Q:
1256        """
1257        Append to or set the WHERE expressions.
1258
1259        Examples:
1260            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
1261            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
1262
1263        Args:
1264            *expressions: the SQL code strings to parse.
1265                If an `Expression` instance is passed, it will be used as-is.
1266                Multiple expressions are combined with an AND operator.
1267            append: if `True`, AND the new expressions to any existing expression.
1268                Otherwise, this resets the expression.
1269            dialect: the dialect used to parse the input expressions.
1270            copy: if `False`, modify this expression instance in-place.
1271            opts: other options to use to parse the input expressions.
1272
1273        Returns:
1274            The modified expression.
1275        """
1276        return _apply_conjunction_builder(
1277            *[expr.this if isinstance(expr, Where) else expr for expr in expressions],
1278            instance=self,
1279            arg="where",
1280            append=append,
1281            into=Where,
1282            dialect=dialect,
1283            copy=copy,
1284            **opts,
1285        )
1286
1287    def with_(
1288        self: Q,
1289        alias: ExpOrStr,
1290        as_: ExpOrStr,
1291        recursive: t.Optional[bool] = None,
1292        materialized: t.Optional[bool] = None,
1293        append: bool = True,
1294        dialect: DialectType = None,
1295        copy: bool = True,
1296        scalar: bool = False,
1297        **opts,
1298    ) -> Q:
1299        """
1300        Append to or set the common table expressions.
1301
1302        Example:
1303            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
1304            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
1305
1306        Args:
1307            alias: the SQL code string to parse as the table name.
1308                If an `Expression` instance is passed, this is used as-is.
1309            as_: the SQL code string to parse as the table expression.
1310                If an `Expression` instance is passed, it will be used as-is.
1311            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1312            materialized: set the MATERIALIZED part of the expression.
1313            append: if `True`, add to any existing expressions.
1314                Otherwise, this resets the expressions.
1315            dialect: the dialect used to parse the input expression.
1316            copy: if `False`, modify this expression instance in-place.
1317            scalar: if `True`, this is a scalar common table expression.
1318            opts: other options to use to parse the input expressions.
1319
1320        Returns:
1321            The modified expression.
1322        """
1323        return _apply_cte_builder(
1324            self,
1325            alias,
1326            as_,
1327            recursive=recursive,
1328            materialized=materialized,
1329            append=append,
1330            dialect=dialect,
1331            copy=copy,
1332            scalar=scalar,
1333            **opts,
1334        )
1335
1336    def union(
1337        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1338    ) -> Union:
1339        """
1340        Builds a UNION expression.
1341
1342        Example:
1343            >>> import sqlglot
1344            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
1345            'SELECT * FROM foo UNION SELECT * FROM bla'
1346
1347        Args:
1348            expressions: the SQL code strings.
1349                If `Expression` instances are passed, they will be used as-is.
1350            distinct: set the DISTINCT flag if and only if this is true.
1351            dialect: the dialect used to parse the input expression.
1352            opts: other options to use to parse the input expressions.
1353
1354        Returns:
1355            The new Union expression.
1356        """
1357        return union(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1358
1359    def intersect(
1360        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1361    ) -> Intersect:
1362        """
1363        Builds an INTERSECT expression.
1364
1365        Example:
1366            >>> import sqlglot
1367            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
1368            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
1369
1370        Args:
1371            expressions: the SQL code strings.
1372                If `Expression` instances are passed, they will be used as-is.
1373            distinct: set the DISTINCT flag if and only if this is true.
1374            dialect: the dialect used to parse the input expression.
1375            opts: other options to use to parse the input expressions.
1376
1377        Returns:
1378            The new Intersect expression.
1379        """
1380        return intersect(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1381
1382    def except_(
1383        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1384    ) -> Except:
1385        """
1386        Builds an EXCEPT expression.
1387
1388        Example:
1389            >>> import sqlglot
1390            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
1391            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
1392
1393        Args:
1394            expressions: the SQL code strings.
1395                If `Expression` instance are passed, they will be used as-is.
1396            distinct: set the DISTINCT flag if and only if this is true.
1397            dialect: the dialect used to parse the input expression.
1398            opts: other options to use to parse the input expressions.
1399
1400        Returns:
1401            The new Except expression.
1402        """
1403        return except_(self, *expressions, distinct=distinct, dialect=dialect, **opts)
def subquery( self, alias: Union[str, Expression, NoneType] = None, copy: bool = True) -> Subquery:
1076    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
1077        """
1078        Returns a `Subquery` that wraps around this query.
1079
1080        Example:
1081            >>> subquery = Select().select("x").from_("tbl").subquery()
1082            >>> Select().select("x").from_(subquery).sql()
1083            'SELECT x FROM (SELECT x FROM tbl)'
1084
1085        Args:
1086            alias: an optional alias for the subquery.
1087            copy: if `False`, modify this expression instance in-place.
1088        """
1089        instance = maybe_copy(self, copy)
1090        if not isinstance(alias, Expression):
1091            alias = TableAlias(this=to_identifier(alias)) if alias else None
1092
1093        return Subquery(this=instance, alias=alias)

Returns a Subquery that wraps around this query.

Example:
>>> subquery = Select().select("x").from_("tbl").subquery()
>>> Select().select("x").from_(subquery).sql()
'SELECT x FROM (SELECT x FROM tbl)'
Arguments:
  • alias: an optional alias for the subquery.
  • copy: if False, modify this expression instance in-place.
def limit( self: ~Q, expression: Union[str, Expression, int], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1095    def limit(
1096        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1097    ) -> Q:
1098        """
1099        Adds a LIMIT clause to this query.
1100
1101        Example:
1102            >>> select("1").union(select("1")).limit(1).sql()
1103            'SELECT 1 UNION SELECT 1 LIMIT 1'
1104
1105        Args:
1106            expression: the SQL code string to parse.
1107                This can also be an integer.
1108                If a `Limit` instance is passed, it will be used as-is.
1109                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
1110            dialect: the dialect used to parse the input expression.
1111            copy: if `False`, modify this expression instance in-place.
1112            opts: other options to use to parse the input expressions.
1113
1114        Returns:
1115            A limited Select expression.
1116        """
1117        return _apply_builder(
1118            expression=expression,
1119            instance=self,
1120            arg="limit",
1121            into=Limit,
1122            prefix="LIMIT",
1123            dialect=dialect,
1124            copy=copy,
1125            into_arg="expression",
1126            **opts,
1127        )

Adds a LIMIT clause to this query.

Example:
>>> select("1").union(select("1")).limit(1).sql()
'SELECT 1 UNION SELECT 1 LIMIT 1'
Arguments:
  • expression: the SQL code string to parse. This can also be an integer. If a Limit instance is passed, it will be used as-is. If another Expression instance is passed, it will be wrapped in a Limit.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

A limited Select expression.

def offset( self: ~Q, expression: Union[str, Expression, int], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1129    def offset(
1130        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1131    ) -> Q:
1132        """
1133        Set the OFFSET expression.
1134
1135        Example:
1136            >>> Select().from_("tbl").select("x").offset(10).sql()
1137            'SELECT x FROM tbl OFFSET 10'
1138
1139        Args:
1140            expression: the SQL code string to parse.
1141                This can also be an integer.
1142                If a `Offset` instance is passed, this is used as-is.
1143                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
1144            dialect: the dialect used to parse the input expression.
1145            copy: if `False`, modify this expression instance in-place.
1146            opts: other options to use to parse the input expressions.
1147
1148        Returns:
1149            The modified Select expression.
1150        """
1151        return _apply_builder(
1152            expression=expression,
1153            instance=self,
1154            arg="offset",
1155            into=Offset,
1156            prefix="OFFSET",
1157            dialect=dialect,
1158            copy=copy,
1159            into_arg="expression",
1160            **opts,
1161        )

Set the OFFSET expression.

Example:
>>> Select().from_("tbl").select("x").offset(10).sql()
'SELECT x FROM tbl OFFSET 10'
Arguments:
  • expression: the SQL code string to parse. This can also be an integer. If a Offset instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a Offset.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def order_by( self: ~Q, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1163    def order_by(
1164        self: Q,
1165        *expressions: t.Optional[ExpOrStr],
1166        append: bool = True,
1167        dialect: DialectType = None,
1168        copy: bool = True,
1169        **opts,
1170    ) -> Q:
1171        """
1172        Set the ORDER BY expression.
1173
1174        Example:
1175            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
1176            'SELECT x FROM tbl ORDER BY x DESC'
1177
1178        Args:
1179            *expressions: the SQL code strings to parse.
1180                If a `Group` instance is passed, this is used as-is.
1181                If another `Expression` instance is passed, it will be wrapped in a `Order`.
1182            append: if `True`, add to any existing expressions.
1183                Otherwise, this flattens all the `Order` expression into a single expression.
1184            dialect: the dialect used to parse the input expression.
1185            copy: if `False`, modify this expression instance in-place.
1186            opts: other options to use to parse the input expressions.
1187
1188        Returns:
1189            The modified Select expression.
1190        """
1191        return _apply_child_list_builder(
1192            *expressions,
1193            instance=self,
1194            arg="order",
1195            append=append,
1196            copy=copy,
1197            prefix="ORDER BY",
1198            into=Order,
1199            dialect=dialect,
1200            **opts,
1201        )

Set the ORDER BY expression.

Example:
>>> Select().from_("tbl").select("x").order_by("x DESC").sql()
'SELECT x FROM tbl ORDER BY x DESC'
Arguments:
  • *expressions: the SQL code strings to parse. If a Group instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a Order.
  • append: if True, add to any existing expressions. Otherwise, this flattens all the Order expression into a single expression.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

ctes: List[CTE]
1203    @property
1204    def ctes(self) -> t.List[CTE]:
1205        """Returns a list of all the CTEs attached to this query."""
1206        with_ = self.args.get("with")
1207        return with_.expressions if with_ else []

Returns a list of all the CTEs attached to this query.

selects: List[Expression]
1209    @property
1210    def selects(self) -> t.List[Expression]:
1211        """Returns the query's projections."""
1212        raise NotImplementedError("Query objects must implement `selects`")

Returns the query's projections.

named_selects: List[str]
1214    @property
1215    def named_selects(self) -> t.List[str]:
1216        """Returns the output names of the query's projections."""
1217        raise NotImplementedError("Query objects must implement `named_selects`")

Returns the output names of the query's projections.

def select( self: ~Q, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1219    def select(
1220        self: Q,
1221        *expressions: t.Optional[ExpOrStr],
1222        append: bool = True,
1223        dialect: DialectType = None,
1224        copy: bool = True,
1225        **opts,
1226    ) -> Q:
1227        """
1228        Append to or set the SELECT expressions.
1229
1230        Example:
1231            >>> Select().select("x", "y").sql()
1232            'SELECT x, y'
1233
1234        Args:
1235            *expressions: the SQL code strings to parse.
1236                If an `Expression` instance is passed, it will be used as-is.
1237            append: if `True`, add to any existing expressions.
1238                Otherwise, this resets the expressions.
1239            dialect: the dialect used to parse the input expressions.
1240            copy: if `False`, modify this expression instance in-place.
1241            opts: other options to use to parse the input expressions.
1242
1243        Returns:
1244            The modified Query expression.
1245        """
1246        raise NotImplementedError("Query objects must implement `select`")

Append to or set the SELECT expressions.

Example:
>>> Select().select("x", "y").sql()
'SELECT x, y'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Query expression.

def where( self: ~Q, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1248    def where(
1249        self: Q,
1250        *expressions: t.Optional[ExpOrStr],
1251        append: bool = True,
1252        dialect: DialectType = None,
1253        copy: bool = True,
1254        **opts,
1255    ) -> Q:
1256        """
1257        Append to or set the WHERE expressions.
1258
1259        Examples:
1260            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
1261            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
1262
1263        Args:
1264            *expressions: the SQL code strings to parse.
1265                If an `Expression` instance is passed, it will be used as-is.
1266                Multiple expressions are combined with an AND operator.
1267            append: if `True`, AND the new expressions to any existing expression.
1268                Otherwise, this resets the expression.
1269            dialect: the dialect used to parse the input expressions.
1270            copy: if `False`, modify this expression instance in-place.
1271            opts: other options to use to parse the input expressions.
1272
1273        Returns:
1274            The modified expression.
1275        """
1276        return _apply_conjunction_builder(
1277            *[expr.this if isinstance(expr, Where) else expr for expr in expressions],
1278            instance=self,
1279            arg="where",
1280            append=append,
1281            into=Where,
1282            dialect=dialect,
1283            copy=copy,
1284            **opts,
1285        )

Append to or set the WHERE expressions.

Examples:
>>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
"SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is. Multiple expressions are combined with an AND operator.
  • append: if True, AND the new expressions to any existing expression. Otherwise, this resets the expression.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified expression.

def with_( self: ~Q, alias: Union[str, Expression], as_: Union[str, Expression], recursive: Optional[bool] = None, materialized: Optional[bool] = None, append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, scalar: bool = False, **opts) -> ~Q:
1287    def with_(
1288        self: Q,
1289        alias: ExpOrStr,
1290        as_: ExpOrStr,
1291        recursive: t.Optional[bool] = None,
1292        materialized: t.Optional[bool] = None,
1293        append: bool = True,
1294        dialect: DialectType = None,
1295        copy: bool = True,
1296        scalar: bool = False,
1297        **opts,
1298    ) -> Q:
1299        """
1300        Append to or set the common table expressions.
1301
1302        Example:
1303            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
1304            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
1305
1306        Args:
1307            alias: the SQL code string to parse as the table name.
1308                If an `Expression` instance is passed, this is used as-is.
1309            as_: the SQL code string to parse as the table expression.
1310                If an `Expression` instance is passed, it will be used as-is.
1311            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1312            materialized: set the MATERIALIZED part of the expression.
1313            append: if `True`, add to any existing expressions.
1314                Otherwise, this resets the expressions.
1315            dialect: the dialect used to parse the input expression.
1316            copy: if `False`, modify this expression instance in-place.
1317            scalar: if `True`, this is a scalar common table expression.
1318            opts: other options to use to parse the input expressions.
1319
1320        Returns:
1321            The modified expression.
1322        """
1323        return _apply_cte_builder(
1324            self,
1325            alias,
1326            as_,
1327            recursive=recursive,
1328            materialized=materialized,
1329            append=append,
1330            dialect=dialect,
1331            copy=copy,
1332            scalar=scalar,
1333            **opts,
1334        )

Append to or set the common table expressions.

Example:
>>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
Arguments:
  • alias: the SQL code string to parse as the table name. If an Expression instance is passed, this is used as-is.
  • as_: the SQL code string to parse as the table expression. If an Expression instance is passed, it will be used as-is.
  • recursive: set the RECURSIVE part of the expression. Defaults to False.
  • materialized: set the MATERIALIZED part of the expression.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • scalar: if True, this is a scalar common table expression.
  • opts: other options to use to parse the input expressions.
Returns:

The modified expression.

def union( self, *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Union:
1336    def union(
1337        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1338    ) -> Union:
1339        """
1340        Builds a UNION expression.
1341
1342        Example:
1343            >>> import sqlglot
1344            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
1345            'SELECT * FROM foo UNION SELECT * FROM bla'
1346
1347        Args:
1348            expressions: the SQL code strings.
1349                If `Expression` instances are passed, they will be used as-is.
1350            distinct: set the DISTINCT flag if and only if this is true.
1351            dialect: the dialect used to parse the input expression.
1352            opts: other options to use to parse the input expressions.
1353
1354        Returns:
1355            The new Union expression.
1356        """
1357        return union(self, *expressions, distinct=distinct, dialect=dialect, **opts)

Builds a UNION expression.

Example:
>>> import sqlglot
>>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
'SELECT * FROM foo UNION SELECT * FROM bla'
Arguments:
  • expressions: the SQL code strings. If Expression instances are passed, they will be used as-is.
  • distinct: set the DISTINCT flag if and only if this is true.
  • dialect: the dialect used to parse the input expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Union expression.

def intersect( self, *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Intersect:
1359    def intersect(
1360        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1361    ) -> Intersect:
1362        """
1363        Builds an INTERSECT expression.
1364
1365        Example:
1366            >>> import sqlglot
1367            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
1368            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
1369
1370        Args:
1371            expressions: the SQL code strings.
1372                If `Expression` instances are passed, they will be used as-is.
1373            distinct: set the DISTINCT flag if and only if this is true.
1374            dialect: the dialect used to parse the input expression.
1375            opts: other options to use to parse the input expressions.
1376
1377        Returns:
1378            The new Intersect expression.
1379        """
1380        return intersect(self, *expressions, distinct=distinct, dialect=dialect, **opts)

Builds an INTERSECT expression.

Example:
>>> import sqlglot
>>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
'SELECT * FROM foo INTERSECT SELECT * FROM bla'
Arguments:
  • expressions: the SQL code strings. If Expression instances are passed, they will be used as-is.
  • distinct: set the DISTINCT flag if and only if this is true.
  • dialect: the dialect used to parse the input expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Intersect expression.

def except_( self, *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Except:
1382    def except_(
1383        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1384    ) -> Except:
1385        """
1386        Builds an EXCEPT expression.
1387
1388        Example:
1389            >>> import sqlglot
1390            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
1391            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
1392
1393        Args:
1394            expressions: the SQL code strings.
1395                If `Expression` instance are passed, they will be used as-is.
1396            distinct: set the DISTINCT flag if and only if this is true.
1397            dialect: the dialect used to parse the input expression.
1398            opts: other options to use to parse the input expressions.
1399
1400        Returns:
1401            The new Except expression.
1402        """
1403        return except_(self, *expressions, distinct=distinct, dialect=dialect, **opts)

Builds an EXCEPT expression.

Example:
>>> import sqlglot
>>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
'SELECT * FROM foo EXCEPT SELECT * FROM bla'
Arguments:
  • expressions: the SQL code strings. If Expression instance are passed, they will be used as-is.
  • distinct: set the DISTINCT flag if and only if this is true.
  • dialect: the dialect used to parse the input expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Except expression.

key = 'query'
class UDTF(DerivedTable):
1406class UDTF(DerivedTable):
1407    @property
1408    def selects(self) -> t.List[Expression]:
1409        alias = self.args.get("alias")
1410        return alias.columns if alias else []
selects: List[Expression]
1407    @property
1408    def selects(self) -> t.List[Expression]:
1409        alias = self.args.get("alias")
1410        return alias.columns if alias else []
key = 'udtf'
class Cache(Expression):
1413class Cache(Expression):
1414    arg_types = {
1415        "this": True,
1416        "lazy": False,
1417        "options": False,
1418        "expression": False,
1419    }
arg_types = {'this': True, 'lazy': False, 'options': False, 'expression': False}
key = 'cache'
class Uncache(Expression):
1422class Uncache(Expression):
1423    arg_types = {"this": True, "exists": False}
arg_types = {'this': True, 'exists': False}
key = 'uncache'
class Refresh(Expression):
1426class Refresh(Expression):
1427    pass
key = 'refresh'
class DDL(Expression):
1430class DDL(Expression):
1431    @property
1432    def ctes(self) -> t.List[CTE]:
1433        """Returns a list of all the CTEs attached to this statement."""
1434        with_ = self.args.get("with")
1435        return with_.expressions if with_ else []
1436
1437    @property
1438    def selects(self) -> t.List[Expression]:
1439        """If this statement contains a query (e.g. a CTAS), this returns the query's projections."""
1440        return self.expression.selects if isinstance(self.expression, Query) else []
1441
1442    @property
1443    def named_selects(self) -> t.List[str]:
1444        """
1445        If this statement contains a query (e.g. a CTAS), this returns the output
1446        names of the query's projections.
1447        """
1448        return self.expression.named_selects if isinstance(self.expression, Query) else []
ctes: List[CTE]
1431    @property
1432    def ctes(self) -> t.List[CTE]:
1433        """Returns a list of all the CTEs attached to this statement."""
1434        with_ = self.args.get("with")
1435        return with_.expressions if with_ else []

Returns a list of all the CTEs attached to this statement.

selects: List[Expression]
1437    @property
1438    def selects(self) -> t.List[Expression]:
1439        """If this statement contains a query (e.g. a CTAS), this returns the query's projections."""
1440        return self.expression.selects if isinstance(self.expression, Query) else []

If this statement contains a query (e.g. a CTAS), this returns the query's projections.

named_selects: List[str]
1442    @property
1443    def named_selects(self) -> t.List[str]:
1444        """
1445        If this statement contains a query (e.g. a CTAS), this returns the output
1446        names of the query's projections.
1447        """
1448        return self.expression.named_selects if isinstance(self.expression, Query) else []

If this statement contains a query (e.g. a CTAS), this returns the output names of the query's projections.

key = 'ddl'
class DML(Expression):
1451class DML(Expression):
1452    def returning(
1453        self,
1454        expression: ExpOrStr,
1455        dialect: DialectType = None,
1456        copy: bool = True,
1457        **opts,
1458    ) -> "Self":
1459        """
1460        Set the RETURNING expression. Not supported by all dialects.
1461
1462        Example:
1463            >>> delete("tbl").returning("*", dialect="postgres").sql()
1464            'DELETE FROM tbl RETURNING *'
1465
1466        Args:
1467            expression: the SQL code strings to parse.
1468                If an `Expression` instance is passed, it will be used as-is.
1469            dialect: the dialect used to parse the input expressions.
1470            copy: if `False`, modify this expression instance in-place.
1471            opts: other options to use to parse the input expressions.
1472
1473        Returns:
1474            Delete: the modified expression.
1475        """
1476        return _apply_builder(
1477            expression=expression,
1478            instance=self,
1479            arg="returning",
1480            prefix="RETURNING",
1481            dialect=dialect,
1482            copy=copy,
1483            into=Returning,
1484            **opts,
1485        )
def returning( self, expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> typing_extensions.Self:
1452    def returning(
1453        self,
1454        expression: ExpOrStr,
1455        dialect: DialectType = None,
1456        copy: bool = True,
1457        **opts,
1458    ) -> "Self":
1459        """
1460        Set the RETURNING expression. Not supported by all dialects.
1461
1462        Example:
1463            >>> delete("tbl").returning("*", dialect="postgres").sql()
1464            'DELETE FROM tbl RETURNING *'
1465
1466        Args:
1467            expression: the SQL code strings to parse.
1468                If an `Expression` instance is passed, it will be used as-is.
1469            dialect: the dialect used to parse the input expressions.
1470            copy: if `False`, modify this expression instance in-place.
1471            opts: other options to use to parse the input expressions.
1472
1473        Returns:
1474            Delete: the modified expression.
1475        """
1476        return _apply_builder(
1477            expression=expression,
1478            instance=self,
1479            arg="returning",
1480            prefix="RETURNING",
1481            dialect=dialect,
1482            copy=copy,
1483            into=Returning,
1484            **opts,
1485        )

Set the RETURNING expression. Not supported by all dialects.

Example:
>>> delete("tbl").returning("*", dialect="postgres").sql()
'DELETE FROM tbl RETURNING *'
Arguments:
  • expression: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

Delete: the modified expression.

key = 'dml'
class Create(DDL):
1488class Create(DDL):
1489    arg_types = {
1490        "with": False,
1491        "this": True,
1492        "kind": True,
1493        "expression": False,
1494        "exists": False,
1495        "properties": False,
1496        "replace": False,
1497        "refresh": False,
1498        "unique": False,
1499        "indexes": False,
1500        "no_schema_binding": False,
1501        "begin": False,
1502        "end": False,
1503        "clone": False,
1504        "concurrently": False,
1505        "clustered": False,
1506    }
1507
1508    @property
1509    def kind(self) -> t.Optional[str]:
1510        kind = self.args.get("kind")
1511        return kind and kind.upper()
arg_types = {'with': False, 'this': True, 'kind': True, 'expression': False, 'exists': False, 'properties': False, 'replace': False, 'refresh': False, 'unique': False, 'indexes': False, 'no_schema_binding': False, 'begin': False, 'end': False, 'clone': False, 'concurrently': False, 'clustered': False}
kind: Optional[str]
1508    @property
1509    def kind(self) -> t.Optional[str]:
1510        kind = self.args.get("kind")
1511        return kind and kind.upper()
key = 'create'
class SequenceProperties(Expression):
1514class SequenceProperties(Expression):
1515    arg_types = {
1516        "increment": False,
1517        "minvalue": False,
1518        "maxvalue": False,
1519        "cache": False,
1520        "start": False,
1521        "owned": False,
1522        "options": False,
1523    }
arg_types = {'increment': False, 'minvalue': False, 'maxvalue': False, 'cache': False, 'start': False, 'owned': False, 'options': False}
key = 'sequenceproperties'
class TruncateTable(Expression):
1526class TruncateTable(Expression):
1527    arg_types = {
1528        "expressions": True,
1529        "is_database": False,
1530        "exists": False,
1531        "only": False,
1532        "cluster": False,
1533        "identity": False,
1534        "option": False,
1535        "partition": False,
1536    }
arg_types = {'expressions': True, 'is_database': False, 'exists': False, 'only': False, 'cluster': False, 'identity': False, 'option': False, 'partition': False}
key = 'truncatetable'
class Clone(Expression):
1542class Clone(Expression):
1543    arg_types = {"this": True, "shallow": False, "copy": False}
arg_types = {'this': True, 'shallow': False, 'copy': False}
key = 'clone'
class Describe(Expression):
1546class Describe(Expression):
1547    arg_types = {
1548        "this": True,
1549        "style": False,
1550        "kind": False,
1551        "expressions": False,
1552        "partition": False,
1553        "format": False,
1554    }
arg_types = {'this': True, 'style': False, 'kind': False, 'expressions': False, 'partition': False, 'format': False}
key = 'describe'
class Attach(Expression):
1558class Attach(Expression):
1559    arg_types = {"this": True, "exists": False, "expressions": False}
arg_types = {'this': True, 'exists': False, 'expressions': False}
key = 'attach'
class Detach(Expression):
1563class Detach(Expression):
1564    arg_types = {"this": True, "exists": False}
arg_types = {'this': True, 'exists': False}
key = 'detach'
class Summarize(Expression):
1568class Summarize(Expression):
1569    arg_types = {"this": True, "table": False}
arg_types = {'this': True, 'table': False}
key = 'summarize'
class Kill(Expression):
1572class Kill(Expression):
1573    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'kill'
class Pragma(Expression):
1576class Pragma(Expression):
1577    pass
key = 'pragma'
class Declare(Expression):
1580class Declare(Expression):
1581    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'declare'
class DeclareItem(Expression):
1584class DeclareItem(Expression):
1585    arg_types = {"this": True, "kind": True, "default": False}
arg_types = {'this': True, 'kind': True, 'default': False}
key = 'declareitem'
class Set(Expression):
1588class Set(Expression):
1589    arg_types = {"expressions": False, "unset": False, "tag": False}
arg_types = {'expressions': False, 'unset': False, 'tag': False}
key = 'set'
class Heredoc(Expression):
1592class Heredoc(Expression):
1593    arg_types = {"this": True, "tag": False}
arg_types = {'this': True, 'tag': False}
key = 'heredoc'
class SetItem(Expression):
1596class SetItem(Expression):
1597    arg_types = {
1598        "this": False,
1599        "expressions": False,
1600        "kind": False,
1601        "collate": False,  # MySQL SET NAMES statement
1602        "global": False,
1603    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'collate': False, 'global': False}
key = 'setitem'
class Show(Expression):
1606class Show(Expression):
1607    arg_types = {
1608        "this": True,
1609        "history": False,
1610        "terse": False,
1611        "target": False,
1612        "offset": False,
1613        "starts_with": False,
1614        "limit": False,
1615        "from": False,
1616        "like": False,
1617        "where": False,
1618        "db": False,
1619        "scope": False,
1620        "scope_kind": False,
1621        "full": False,
1622        "mutex": False,
1623        "query": False,
1624        "channel": False,
1625        "global": False,
1626        "log": False,
1627        "position": False,
1628        "types": False,
1629        "privileges": False,
1630    }
arg_types = {'this': True, 'history': False, 'terse': False, 'target': False, 'offset': False, 'starts_with': False, 'limit': False, 'from': False, 'like': False, 'where': False, 'db': False, 'scope': False, 'scope_kind': False, 'full': False, 'mutex': False, 'query': False, 'channel': False, 'global': False, 'log': False, 'position': False, 'types': False, 'privileges': False}
key = 'show'
class UserDefinedFunction(Expression):
1633class UserDefinedFunction(Expression):
1634    arg_types = {"this": True, "expressions": False, "wrapped": False}
arg_types = {'this': True, 'expressions': False, 'wrapped': False}
key = 'userdefinedfunction'
class CharacterSet(Expression):
1637class CharacterSet(Expression):
1638    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'characterset'
class RecursiveWithSearch(Expression):
1641class RecursiveWithSearch(Expression):
1642    arg_types = {"kind": True, "this": True, "expression": True, "using": False}
arg_types = {'kind': True, 'this': True, 'expression': True, 'using': False}
key = 'recursivewithsearch'
class With(Expression):
1645class With(Expression):
1646    arg_types = {"expressions": True, "recursive": False, "search": False}
1647
1648    @property
1649    def recursive(self) -> bool:
1650        return bool(self.args.get("recursive"))
arg_types = {'expressions': True, 'recursive': False, 'search': False}
recursive: bool
1648    @property
1649    def recursive(self) -> bool:
1650        return bool(self.args.get("recursive"))
key = 'with'
class WithinGroup(Expression):
1653class WithinGroup(Expression):
1654    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'withingroup'
class CTE(DerivedTable):
1659class CTE(DerivedTable):
1660    arg_types = {
1661        "this": True,
1662        "alias": True,
1663        "scalar": False,
1664        "materialized": False,
1665    }
arg_types = {'this': True, 'alias': True, 'scalar': False, 'materialized': False}
key = 'cte'
class ProjectionDef(Expression):
1668class ProjectionDef(Expression):
1669    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'projectiondef'
class TableAlias(Expression):
1672class TableAlias(Expression):
1673    arg_types = {"this": False, "columns": False}
1674
1675    @property
1676    def columns(self):
1677        return self.args.get("columns") or []
arg_types = {'this': False, 'columns': False}
columns
1675    @property
1676    def columns(self):
1677        return self.args.get("columns") or []
key = 'tablealias'
class BitString(Condition):
1680class BitString(Condition):
1681    pass
key = 'bitstring'
class HexString(Condition):
1684class HexString(Condition):
1685    arg_types = {"this": True, "is_integer": False}
arg_types = {'this': True, 'is_integer': False}
key = 'hexstring'
class ByteString(Condition):
1688class ByteString(Condition):
1689    pass
key = 'bytestring'
class RawString(Condition):
1692class RawString(Condition):
1693    pass
key = 'rawstring'
class UnicodeString(Condition):
1696class UnicodeString(Condition):
1697    arg_types = {"this": True, "escape": False}
arg_types = {'this': True, 'escape': False}
key = 'unicodestring'
class Column(Condition):
1700class Column(Condition):
1701    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1702
1703    @property
1704    def table(self) -> str:
1705        return self.text("table")
1706
1707    @property
1708    def db(self) -> str:
1709        return self.text("db")
1710
1711    @property
1712    def catalog(self) -> str:
1713        return self.text("catalog")
1714
1715    @property
1716    def output_name(self) -> str:
1717        return self.name
1718
1719    @property
1720    def parts(self) -> t.List[Identifier]:
1721        """Return the parts of a column in order catalog, db, table, name."""
1722        return [
1723            t.cast(Identifier, self.args[part])
1724            for part in ("catalog", "db", "table", "this")
1725            if self.args.get(part)
1726        ]
1727
1728    def to_dot(self, include_dots: bool = True) -> Dot | Identifier:
1729        """Converts the column into a dot expression."""
1730        parts = self.parts
1731        parent = self.parent
1732
1733        if include_dots:
1734            while isinstance(parent, Dot):
1735                parts.append(parent.expression)
1736                parent = parent.parent
1737
1738        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]
arg_types = {'this': True, 'table': False, 'db': False, 'catalog': False, 'join_mark': False}
table: str
1703    @property
1704    def table(self) -> str:
1705        return self.text("table")
db: str
1707    @property
1708    def db(self) -> str:
1709        return self.text("db")
catalog: str
1711    @property
1712    def catalog(self) -> str:
1713        return self.text("catalog")
output_name: str
1715    @property
1716    def output_name(self) -> str:
1717        return self.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
parts: List[Identifier]
1719    @property
1720    def parts(self) -> t.List[Identifier]:
1721        """Return the parts of a column in order catalog, db, table, name."""
1722        return [
1723            t.cast(Identifier, self.args[part])
1724            for part in ("catalog", "db", "table", "this")
1725            if self.args.get(part)
1726        ]

Return the parts of a column in order catalog, db, table, name.

def to_dot( self, include_dots: bool = True) -> Dot | Identifier:
1728    def to_dot(self, include_dots: bool = True) -> Dot | Identifier:
1729        """Converts the column into a dot expression."""
1730        parts = self.parts
1731        parent = self.parent
1732
1733        if include_dots:
1734            while isinstance(parent, Dot):
1735                parts.append(parent.expression)
1736                parent = parent.parent
1737
1738        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]

Converts the column into a dot expression.

key = 'column'
class ColumnPosition(Expression):
1741class ColumnPosition(Expression):
1742    arg_types = {"this": False, "position": True}
arg_types = {'this': False, 'position': True}
key = 'columnposition'
class ColumnDef(Expression):
1745class ColumnDef(Expression):
1746    arg_types = {
1747        "this": True,
1748        "kind": False,
1749        "constraints": False,
1750        "exists": False,
1751        "position": False,
1752        "default": False,
1753        "output": False,
1754    }
1755
1756    @property
1757    def constraints(self) -> t.List[ColumnConstraint]:
1758        return self.args.get("constraints") or []
1759
1760    @property
1761    def kind(self) -> t.Optional[DataType]:
1762        return self.args.get("kind")
arg_types = {'this': True, 'kind': False, 'constraints': False, 'exists': False, 'position': False, 'default': False, 'output': False}
constraints: List[ColumnConstraint]
1756    @property
1757    def constraints(self) -> t.List[ColumnConstraint]:
1758        return self.args.get("constraints") or []
kind: Optional[DataType]
1760    @property
1761    def kind(self) -> t.Optional[DataType]:
1762        return self.args.get("kind")
key = 'columndef'
class AlterColumn(Expression):
1765class AlterColumn(Expression):
1766    arg_types = {
1767        "this": True,
1768        "dtype": False,
1769        "collate": False,
1770        "using": False,
1771        "default": False,
1772        "drop": False,
1773        "comment": False,
1774        "allow_null": False,
1775        "visible": False,
1776    }
arg_types = {'this': True, 'dtype': False, 'collate': False, 'using': False, 'default': False, 'drop': False, 'comment': False, 'allow_null': False, 'visible': False}
key = 'altercolumn'
class AlterIndex(Expression):
1780class AlterIndex(Expression):
1781    arg_types = {"this": True, "visible": True}
arg_types = {'this': True, 'visible': True}
key = 'alterindex'
class AlterDistStyle(Expression):
1785class AlterDistStyle(Expression):
1786    pass
key = 'alterdiststyle'
class AlterSortKey(Expression):
1789class AlterSortKey(Expression):
1790    arg_types = {"this": False, "expressions": False, "compound": False}
arg_types = {'this': False, 'expressions': False, 'compound': False}
key = 'altersortkey'
class AlterSet(Expression):
1793class AlterSet(Expression):
1794    arg_types = {
1795        "expressions": False,
1796        "option": False,
1797        "tablespace": False,
1798        "access_method": False,
1799        "file_format": False,
1800        "copy_options": False,
1801        "tag": False,
1802        "location": False,
1803        "serde": False,
1804    }
arg_types = {'expressions': False, 'option': False, 'tablespace': False, 'access_method': False, 'file_format': False, 'copy_options': False, 'tag': False, 'location': False, 'serde': False}
key = 'alterset'
class RenameColumn(Expression):
1807class RenameColumn(Expression):
1808    arg_types = {"this": True, "to": True, "exists": False}
arg_types = {'this': True, 'to': True, 'exists': False}
key = 'renamecolumn'
class AlterRename(Expression):
1811class AlterRename(Expression):
1812    pass
key = 'alterrename'
class SwapTable(Expression):
1815class SwapTable(Expression):
1816    pass
key = 'swaptable'
class Comment(Expression):
1819class Comment(Expression):
1820    arg_types = {
1821        "this": True,
1822        "kind": True,
1823        "expression": True,
1824        "exists": False,
1825        "materialized": False,
1826    }
arg_types = {'this': True, 'kind': True, 'expression': True, 'exists': False, 'materialized': False}
key = 'comment'
class Comprehension(Expression):
1829class Comprehension(Expression):
1830    arg_types = {"this": True, "expression": True, "iterator": True, "condition": False}
arg_types = {'this': True, 'expression': True, 'iterator': True, 'condition': False}
key = 'comprehension'
class MergeTreeTTLAction(Expression):
1834class MergeTreeTTLAction(Expression):
1835    arg_types = {
1836        "this": True,
1837        "delete": False,
1838        "recompress": False,
1839        "to_disk": False,
1840        "to_volume": False,
1841    }
arg_types = {'this': True, 'delete': False, 'recompress': False, 'to_disk': False, 'to_volume': False}
key = 'mergetreettlaction'
class MergeTreeTTL(Expression):
1845class MergeTreeTTL(Expression):
1846    arg_types = {
1847        "expressions": True,
1848        "where": False,
1849        "group": False,
1850        "aggregates": False,
1851    }
arg_types = {'expressions': True, 'where': False, 'group': False, 'aggregates': False}
key = 'mergetreettl'
class IndexConstraintOption(Expression):
1855class IndexConstraintOption(Expression):
1856    arg_types = {
1857        "key_block_size": False,
1858        "using": False,
1859        "parser": False,
1860        "comment": False,
1861        "visible": False,
1862        "engine_attr": False,
1863        "secondary_engine_attr": False,
1864    }
arg_types = {'key_block_size': False, 'using': False, 'parser': False, 'comment': False, 'visible': False, 'engine_attr': False, 'secondary_engine_attr': False}
key = 'indexconstraintoption'
class ColumnConstraint(Expression):
1867class ColumnConstraint(Expression):
1868    arg_types = {"this": False, "kind": True}
1869
1870    @property
1871    def kind(self) -> ColumnConstraintKind:
1872        return self.args["kind"]
arg_types = {'this': False, 'kind': True}
kind: ColumnConstraintKind
1870    @property
1871    def kind(self) -> ColumnConstraintKind:
1872        return self.args["kind"]
key = 'columnconstraint'
class ColumnConstraintKind(Expression):
1875class ColumnConstraintKind(Expression):
1876    pass
key = 'columnconstraintkind'
class AutoIncrementColumnConstraint(ColumnConstraintKind):
1879class AutoIncrementColumnConstraint(ColumnConstraintKind):
1880    pass
key = 'autoincrementcolumnconstraint'
class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1883class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1884    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'periodforsystemtimeconstraint'
class CaseSpecificColumnConstraint(ColumnConstraintKind):
1887class CaseSpecificColumnConstraint(ColumnConstraintKind):
1888    arg_types = {"not_": True}
arg_types = {'not_': True}
key = 'casespecificcolumnconstraint'
class CharacterSetColumnConstraint(ColumnConstraintKind):
1891class CharacterSetColumnConstraint(ColumnConstraintKind):
1892    arg_types = {"this": True}
arg_types = {'this': True}
key = 'charactersetcolumnconstraint'
class CheckColumnConstraint(ColumnConstraintKind):
1895class CheckColumnConstraint(ColumnConstraintKind):
1896    arg_types = {"this": True, "enforced": False}
arg_types = {'this': True, 'enforced': False}
key = 'checkcolumnconstraint'
class ClusteredColumnConstraint(ColumnConstraintKind):
1899class ClusteredColumnConstraint(ColumnConstraintKind):
1900    pass
key = 'clusteredcolumnconstraint'
class CollateColumnConstraint(ColumnConstraintKind):
1903class CollateColumnConstraint(ColumnConstraintKind):
1904    pass
key = 'collatecolumnconstraint'
class CommentColumnConstraint(ColumnConstraintKind):
1907class CommentColumnConstraint(ColumnConstraintKind):
1908    pass
key = 'commentcolumnconstraint'
class CompressColumnConstraint(ColumnConstraintKind):
1911class CompressColumnConstraint(ColumnConstraintKind):
1912    arg_types = {"this": False}
arg_types = {'this': False}
key = 'compresscolumnconstraint'
class DateFormatColumnConstraint(ColumnConstraintKind):
1915class DateFormatColumnConstraint(ColumnConstraintKind):
1916    arg_types = {"this": True}
arg_types = {'this': True}
key = 'dateformatcolumnconstraint'
class DefaultColumnConstraint(ColumnConstraintKind):
1919class DefaultColumnConstraint(ColumnConstraintKind):
1920    pass
key = 'defaultcolumnconstraint'
class EncodeColumnConstraint(ColumnConstraintKind):
1923class EncodeColumnConstraint(ColumnConstraintKind):
1924    pass
key = 'encodecolumnconstraint'
class ExcludeColumnConstraint(ColumnConstraintKind):
1928class ExcludeColumnConstraint(ColumnConstraintKind):
1929    pass
key = 'excludecolumnconstraint'
class EphemeralColumnConstraint(ColumnConstraintKind):
1932class EphemeralColumnConstraint(ColumnConstraintKind):
1933    arg_types = {"this": False}
arg_types = {'this': False}
key = 'ephemeralcolumnconstraint'
class WithOperator(Expression):
1936class WithOperator(Expression):
1937    arg_types = {"this": True, "op": True}
arg_types = {'this': True, 'op': True}
key = 'withoperator'
class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1940class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1941    # this: True -> ALWAYS, this: False -> BY DEFAULT
1942    arg_types = {
1943        "this": False,
1944        "expression": False,
1945        "on_null": False,
1946        "start": False,
1947        "increment": False,
1948        "minvalue": False,
1949        "maxvalue": False,
1950        "cycle": False,
1951    }
arg_types = {'this': False, 'expression': False, 'on_null': False, 'start': False, 'increment': False, 'minvalue': False, 'maxvalue': False, 'cycle': False}
key = 'generatedasidentitycolumnconstraint'
class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1954class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1955    arg_types = {"start": False, "hidden": False}
arg_types = {'start': False, 'hidden': False}
key = 'generatedasrowcolumnconstraint'
class IndexColumnConstraint(ColumnConstraintKind):
1960class IndexColumnConstraint(ColumnConstraintKind):
1961    arg_types = {
1962        "this": False,
1963        "expressions": False,
1964        "kind": False,
1965        "index_type": False,
1966        "options": False,
1967        "expression": False,  # Clickhouse
1968        "granularity": False,
1969    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'index_type': False, 'options': False, 'expression': False, 'granularity': False}
key = 'indexcolumnconstraint'
class InlineLengthColumnConstraint(ColumnConstraintKind):
1972class InlineLengthColumnConstraint(ColumnConstraintKind):
1973    pass
key = 'inlinelengthcolumnconstraint'
class NonClusteredColumnConstraint(ColumnConstraintKind):
1976class NonClusteredColumnConstraint(ColumnConstraintKind):
1977    pass
key = 'nonclusteredcolumnconstraint'
class NotForReplicationColumnConstraint(ColumnConstraintKind):
1980class NotForReplicationColumnConstraint(ColumnConstraintKind):
1981    arg_types = {}
arg_types = {}
key = 'notforreplicationcolumnconstraint'
class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1985class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1986    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'maskingpolicycolumnconstraint'
class NotNullColumnConstraint(ColumnConstraintKind):
1989class NotNullColumnConstraint(ColumnConstraintKind):
1990    arg_types = {"allow_null": False}
arg_types = {'allow_null': False}
key = 'notnullcolumnconstraint'
class OnUpdateColumnConstraint(ColumnConstraintKind):
1994class OnUpdateColumnConstraint(ColumnConstraintKind):
1995    pass
key = 'onupdatecolumnconstraint'
class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1998class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1999    arg_types = {"desc": False, "options": False}
arg_types = {'desc': False, 'options': False}
key = 'primarykeycolumnconstraint'
class TitleColumnConstraint(ColumnConstraintKind):
2002class TitleColumnConstraint(ColumnConstraintKind):
2003    pass
key = 'titlecolumnconstraint'
class UniqueColumnConstraint(ColumnConstraintKind):
2006class UniqueColumnConstraint(ColumnConstraintKind):
2007    arg_types = {
2008        "this": False,
2009        "index_type": False,
2010        "on_conflict": False,
2011        "nulls": False,
2012        "options": False,
2013    }
arg_types = {'this': False, 'index_type': False, 'on_conflict': False, 'nulls': False, 'options': False}
key = 'uniquecolumnconstraint'
class UppercaseColumnConstraint(ColumnConstraintKind):
2016class UppercaseColumnConstraint(ColumnConstraintKind):
2017    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'uppercasecolumnconstraint'
class WatermarkColumnConstraint(Expression):
2021class WatermarkColumnConstraint(Expression):
2022    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'watermarkcolumnconstraint'
class PathColumnConstraint(ColumnConstraintKind):
2025class PathColumnConstraint(ColumnConstraintKind):
2026    pass
key = 'pathcolumnconstraint'
class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
2030class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
2031    pass
key = 'projectionpolicycolumnconstraint'
class ComputedColumnConstraint(ColumnConstraintKind):
2036class ComputedColumnConstraint(ColumnConstraintKind):
2037    arg_types = {"this": True, "persisted": False, "not_null": False}
arg_types = {'this': True, 'persisted': False, 'not_null': False}
key = 'computedcolumnconstraint'
class Constraint(Expression):
2040class Constraint(Expression):
2041    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'constraint'
class Delete(DML):
2044class Delete(DML):
2045    arg_types = {
2046        "with": False,
2047        "this": False,
2048        "using": False,
2049        "where": False,
2050        "returning": False,
2051        "limit": False,
2052        "tables": False,  # Multiple-Table Syntax (MySQL)
2053        "cluster": False,  # Clickhouse
2054    }
2055
2056    def delete(
2057        self,
2058        table: ExpOrStr,
2059        dialect: DialectType = None,
2060        copy: bool = True,
2061        **opts,
2062    ) -> Delete:
2063        """
2064        Create a DELETE expression or replace the table on an existing DELETE expression.
2065
2066        Example:
2067            >>> delete("tbl").sql()
2068            'DELETE FROM tbl'
2069
2070        Args:
2071            table: the table from which to delete.
2072            dialect: the dialect used to parse the input expression.
2073            copy: if `False`, modify this expression instance in-place.
2074            opts: other options to use to parse the input expressions.
2075
2076        Returns:
2077            Delete: the modified expression.
2078        """
2079        return _apply_builder(
2080            expression=table,
2081            instance=self,
2082            arg="this",
2083            dialect=dialect,
2084            into=Table,
2085            copy=copy,
2086            **opts,
2087        )
2088
2089    def where(
2090        self,
2091        *expressions: t.Optional[ExpOrStr],
2092        append: bool = True,
2093        dialect: DialectType = None,
2094        copy: bool = True,
2095        **opts,
2096    ) -> Delete:
2097        """
2098        Append to or set the WHERE expressions.
2099
2100        Example:
2101            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
2102            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
2103
2104        Args:
2105            *expressions: the SQL code strings to parse.
2106                If an `Expression` instance is passed, it will be used as-is.
2107                Multiple expressions are combined with an AND operator.
2108            append: if `True`, AND the new expressions to any existing expression.
2109                Otherwise, this resets the expression.
2110            dialect: the dialect used to parse the input expressions.
2111            copy: if `False`, modify this expression instance in-place.
2112            opts: other options to use to parse the input expressions.
2113
2114        Returns:
2115            Delete: the modified expression.
2116        """
2117        return _apply_conjunction_builder(
2118            *expressions,
2119            instance=self,
2120            arg="where",
2121            append=append,
2122            into=Where,
2123            dialect=dialect,
2124            copy=copy,
2125            **opts,
2126        )
arg_types = {'with': False, 'this': False, 'using': False, 'where': False, 'returning': False, 'limit': False, 'tables': False, 'cluster': False}
def delete( self, table: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Delete:
2056    def delete(
2057        self,
2058        table: ExpOrStr,
2059        dialect: DialectType = None,
2060        copy: bool = True,
2061        **opts,
2062    ) -> Delete:
2063        """
2064        Create a DELETE expression or replace the table on an existing DELETE expression.
2065
2066        Example:
2067            >>> delete("tbl").sql()
2068            'DELETE FROM tbl'
2069
2070        Args:
2071            table: the table from which to delete.
2072            dialect: the dialect used to parse the input expression.
2073            copy: if `False`, modify this expression instance in-place.
2074            opts: other options to use to parse the input expressions.
2075
2076        Returns:
2077            Delete: the modified expression.
2078        """
2079        return _apply_builder(
2080            expression=table,
2081            instance=self,
2082            arg="this",
2083            dialect=dialect,
2084            into=Table,
2085            copy=copy,
2086            **opts,
2087        )

Create a DELETE expression or replace the table on an existing DELETE expression.

Example:
>>> delete("tbl").sql()
'DELETE FROM tbl'
Arguments:
  • table: the table from which to delete.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

Delete: the modified expression.

def where( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Delete:
2089    def where(
2090        self,
2091        *expressions: t.Optional[ExpOrStr],
2092        append: bool = True,
2093        dialect: DialectType = None,
2094        copy: bool = True,
2095        **opts,
2096    ) -> Delete:
2097        """
2098        Append to or set the WHERE expressions.
2099
2100        Example:
2101            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
2102            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
2103
2104        Args:
2105            *expressions: the SQL code strings to parse.
2106                If an `Expression` instance is passed, it will be used as-is.
2107                Multiple expressions are combined with an AND operator.
2108            append: if `True`, AND the new expressions to any existing expression.
2109                Otherwise, this resets the expression.
2110            dialect: the dialect used to parse the input expressions.
2111            copy: if `False`, modify this expression instance in-place.
2112            opts: other options to use to parse the input expressions.
2113
2114        Returns:
2115            Delete: the modified expression.
2116        """
2117        return _apply_conjunction_builder(
2118            *expressions,
2119            instance=self,
2120            arg="where",
2121            append=append,
2122            into=Where,
2123            dialect=dialect,
2124            copy=copy,
2125            **opts,
2126        )

Append to or set the WHERE expressions.

Example:
>>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
"DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is. Multiple expressions are combined with an AND operator.
  • append: if True, AND the new expressions to any existing expression. Otherwise, this resets the expression.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

Delete: the modified expression.

key = 'delete'
class Drop(Expression):
2129class Drop(Expression):
2130    arg_types = {
2131        "this": False,
2132        "kind": False,
2133        "expressions": False,
2134        "exists": False,
2135        "temporary": False,
2136        "materialized": False,
2137        "cascade": False,
2138        "constraints": False,
2139        "purge": False,
2140        "cluster": False,
2141        "concurrently": False,
2142    }
2143
2144    @property
2145    def kind(self) -> t.Optional[str]:
2146        kind = self.args.get("kind")
2147        return kind and kind.upper()
arg_types = {'this': False, 'kind': False, 'expressions': False, 'exists': False, 'temporary': False, 'materialized': False, 'cascade': False, 'constraints': False, 'purge': False, 'cluster': False, 'concurrently': False}
kind: Optional[str]
2144    @property
2145    def kind(self) -> t.Optional[str]:
2146        kind = self.args.get("kind")
2147        return kind and kind.upper()
key = 'drop'
class Export(Expression):
2151class Export(Expression):
2152    arg_types = {"this": True, "connection": False, "options": True}
arg_types = {'this': True, 'connection': False, 'options': True}
key = 'export'
class Filter(Expression):
2155class Filter(Expression):
2156    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'filter'
class Check(Expression):
2159class Check(Expression):
2160    pass
key = 'check'
class Changes(Expression):
2163class Changes(Expression):
2164    arg_types = {"information": True, "at_before": False, "end": False}
arg_types = {'information': True, 'at_before': False, 'end': False}
key = 'changes'
class Connect(Expression):
2168class Connect(Expression):
2169    arg_types = {"start": False, "connect": True, "nocycle": False}
arg_types = {'start': False, 'connect': True, 'nocycle': False}
key = 'connect'
class CopyParameter(Expression):
2172class CopyParameter(Expression):
2173    arg_types = {"this": True, "expression": False, "expressions": False}
arg_types = {'this': True, 'expression': False, 'expressions': False}
key = 'copyparameter'
class Copy(DML):
2176class Copy(DML):
2177    arg_types = {
2178        "this": True,
2179        "kind": True,
2180        "files": True,
2181        "credentials": False,
2182        "format": False,
2183        "params": False,
2184    }
arg_types = {'this': True, 'kind': True, 'files': True, 'credentials': False, 'format': False, 'params': False}
key = 'copy'
class Credentials(Expression):
2187class Credentials(Expression):
2188    arg_types = {
2189        "credentials": False,
2190        "encryption": False,
2191        "storage": False,
2192        "iam_role": False,
2193        "region": False,
2194    }
arg_types = {'credentials': False, 'encryption': False, 'storage': False, 'iam_role': False, 'region': False}
key = 'credentials'
class Prior(Expression):
2197class Prior(Expression):
2198    pass
key = 'prior'
class Directory(Expression):
2201class Directory(Expression):
2202    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
2203    arg_types = {"this": True, "local": False, "row_format": False}
arg_types = {'this': True, 'local': False, 'row_format': False}
key = 'directory'
class ForeignKey(Expression):
2206class ForeignKey(Expression):
2207    arg_types = {
2208        "expressions": False,
2209        "reference": False,
2210        "delete": False,
2211        "update": False,
2212        "options": False,
2213    }
arg_types = {'expressions': False, 'reference': False, 'delete': False, 'update': False, 'options': False}
key = 'foreignkey'
class ColumnPrefix(Expression):
2216class ColumnPrefix(Expression):
2217    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'columnprefix'
class PrimaryKey(Expression):
2220class PrimaryKey(Expression):
2221    arg_types = {"expressions": True, "options": False}
arg_types = {'expressions': True, 'options': False}
key = 'primarykey'
class Into(Expression):
2226class Into(Expression):
2227    arg_types = {
2228        "this": False,
2229        "temporary": False,
2230        "unlogged": False,
2231        "bulk_collect": False,
2232        "expressions": False,
2233    }
arg_types = {'this': False, 'temporary': False, 'unlogged': False, 'bulk_collect': False, 'expressions': False}
key = 'into'
class From(Expression):
2236class From(Expression):
2237    @property
2238    def name(self) -> str:
2239        return self.this.name
2240
2241    @property
2242    def alias_or_name(self) -> str:
2243        return self.this.alias_or_name
name: str
2237    @property
2238    def name(self) -> str:
2239        return self.this.name
alias_or_name: str
2241    @property
2242    def alias_or_name(self) -> str:
2243        return self.this.alias_or_name
key = 'from'
class Having(Expression):
2246class Having(Expression):
2247    pass
key = 'having'
class Hint(Expression):
2250class Hint(Expression):
2251    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'hint'
class JoinHint(Expression):
2254class JoinHint(Expression):
2255    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'joinhint'
class Identifier(Expression):
2258class Identifier(Expression):
2259    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
2260
2261    @property
2262    def quoted(self) -> bool:
2263        return bool(self.args.get("quoted"))
2264
2265    @property
2266    def hashable_args(self) -> t.Any:
2267        return (self.this, self.quoted)
2268
2269    @property
2270    def output_name(self) -> str:
2271        return self.name
arg_types = {'this': True, 'quoted': False, 'global': False, 'temporary': False}
quoted: bool
2261    @property
2262    def quoted(self) -> bool:
2263        return bool(self.args.get("quoted"))
hashable_args: Any
2265    @property
2266    def hashable_args(self) -> t.Any:
2267        return (self.this, self.quoted)
output_name: str
2269    @property
2270    def output_name(self) -> str:
2271        return self.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'identifier'
class Opclass(Expression):
2275class Opclass(Expression):
2276    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'opclass'
class Index(Expression):
2279class Index(Expression):
2280    arg_types = {
2281        "this": False,
2282        "table": False,
2283        "unique": False,
2284        "primary": False,
2285        "amp": False,  # teradata
2286        "params": False,
2287    }
arg_types = {'this': False, 'table': False, 'unique': False, 'primary': False, 'amp': False, 'params': False}
key = 'index'
class IndexParameters(Expression):
2290class IndexParameters(Expression):
2291    arg_types = {
2292        "using": False,
2293        "include": False,
2294        "columns": False,
2295        "with_storage": False,
2296        "partition_by": False,
2297        "tablespace": False,
2298        "where": False,
2299        "on": False,
2300    }
arg_types = {'using': False, 'include': False, 'columns': False, 'with_storage': False, 'partition_by': False, 'tablespace': False, 'where': False, 'on': False}
key = 'indexparameters'
class Insert(DDL, DML):
2303class Insert(DDL, DML):
2304    arg_types = {
2305        "hint": False,
2306        "with": False,
2307        "is_function": False,
2308        "this": False,
2309        "expression": False,
2310        "conflict": False,
2311        "returning": False,
2312        "overwrite": False,
2313        "exists": False,
2314        "alternative": False,
2315        "where": False,
2316        "ignore": False,
2317        "by_name": False,
2318        "stored": False,
2319        "partition": False,
2320        "settings": False,
2321        "source": False,
2322    }
2323
2324    def with_(
2325        self,
2326        alias: ExpOrStr,
2327        as_: ExpOrStr,
2328        recursive: t.Optional[bool] = None,
2329        materialized: t.Optional[bool] = None,
2330        append: bool = True,
2331        dialect: DialectType = None,
2332        copy: bool = True,
2333        **opts,
2334    ) -> Insert:
2335        """
2336        Append to or set the common table expressions.
2337
2338        Example:
2339            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2340            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2341
2342        Args:
2343            alias: the SQL code string to parse as the table name.
2344                If an `Expression` instance is passed, this is used as-is.
2345            as_: the SQL code string to parse as the table expression.
2346                If an `Expression` instance is passed, it will be used as-is.
2347            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2348            materialized: set the MATERIALIZED part of the expression.
2349            append: if `True`, add to any existing expressions.
2350                Otherwise, this resets the expressions.
2351            dialect: the dialect used to parse the input expression.
2352            copy: if `False`, modify this expression instance in-place.
2353            opts: other options to use to parse the input expressions.
2354
2355        Returns:
2356            The modified expression.
2357        """
2358        return _apply_cte_builder(
2359            self,
2360            alias,
2361            as_,
2362            recursive=recursive,
2363            materialized=materialized,
2364            append=append,
2365            dialect=dialect,
2366            copy=copy,
2367            **opts,
2368        )
arg_types = {'hint': False, 'with': False, 'is_function': False, 'this': False, 'expression': False, 'conflict': False, 'returning': False, 'overwrite': False, 'exists': False, 'alternative': False, 'where': False, 'ignore': False, 'by_name': False, 'stored': False, 'partition': False, 'settings': False, 'source': False}
def with_( self, alias: Union[str, Expression], as_: Union[str, Expression], recursive: Optional[bool] = None, materialized: Optional[bool] = None, append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Insert:
2324    def with_(
2325        self,
2326        alias: ExpOrStr,
2327        as_: ExpOrStr,
2328        recursive: t.Optional[bool] = None,
2329        materialized: t.Optional[bool] = None,
2330        append: bool = True,
2331        dialect: DialectType = None,
2332        copy: bool = True,
2333        **opts,
2334    ) -> Insert:
2335        """
2336        Append to or set the common table expressions.
2337
2338        Example:
2339            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2340            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2341
2342        Args:
2343            alias: the SQL code string to parse as the table name.
2344                If an `Expression` instance is passed, this is used as-is.
2345            as_: the SQL code string to parse as the table expression.
2346                If an `Expression` instance is passed, it will be used as-is.
2347            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2348            materialized: set the MATERIALIZED part of the expression.
2349            append: if `True`, add to any existing expressions.
2350                Otherwise, this resets the expressions.
2351            dialect: the dialect used to parse the input expression.
2352            copy: if `False`, modify this expression instance in-place.
2353            opts: other options to use to parse the input expressions.
2354
2355        Returns:
2356            The modified expression.
2357        """
2358        return _apply_cte_builder(
2359            self,
2360            alias,
2361            as_,
2362            recursive=recursive,
2363            materialized=materialized,
2364            append=append,
2365            dialect=dialect,
2366            copy=copy,
2367            **opts,
2368        )

Append to or set the common table expressions.

Example:
>>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
Arguments:
  • alias: the SQL code string to parse as the table name. If an Expression instance is passed, this is used as-is.
  • as_: the SQL code string to parse as the table expression. If an Expression instance is passed, it will be used as-is.
  • recursive: set the RECURSIVE part of the expression. Defaults to False.
  • materialized: set the MATERIALIZED part of the expression.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified expression.

key = 'insert'
class ConditionalInsert(Expression):
2371class ConditionalInsert(Expression):
2372    arg_types = {"this": True, "expression": False, "else_": False}
arg_types = {'this': True, 'expression': False, 'else_': False}
key = 'conditionalinsert'
class MultitableInserts(Expression):
2375class MultitableInserts(Expression):
2376    arg_types = {"expressions": True, "kind": True, "source": True}
arg_types = {'expressions': True, 'kind': True, 'source': True}
key = 'multitableinserts'
class OnConflict(Expression):
2379class OnConflict(Expression):
2380    arg_types = {
2381        "duplicate": False,
2382        "expressions": False,
2383        "action": False,
2384        "conflict_keys": False,
2385        "constraint": False,
2386        "where": False,
2387    }
arg_types = {'duplicate': False, 'expressions': False, 'action': False, 'conflict_keys': False, 'constraint': False, 'where': False}
key = 'onconflict'
class OnCondition(Expression):
2390class OnCondition(Expression):
2391    arg_types = {"error": False, "empty": False, "null": False}
arg_types = {'error': False, 'empty': False, 'null': False}
key = 'oncondition'
class Returning(Expression):
2394class Returning(Expression):
2395    arg_types = {"expressions": True, "into": False}
arg_types = {'expressions': True, 'into': False}
key = 'returning'
class Introducer(Expression):
2399class Introducer(Expression):
2400    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'introducer'
class National(Expression):
2404class National(Expression):
2405    pass
key = 'national'
class LoadData(Expression):
2408class LoadData(Expression):
2409    arg_types = {
2410        "this": True,
2411        "local": False,
2412        "overwrite": False,
2413        "inpath": True,
2414        "partition": False,
2415        "input_format": False,
2416        "serde": False,
2417    }
arg_types = {'this': True, 'local': False, 'overwrite': False, 'inpath': True, 'partition': False, 'input_format': False, 'serde': False}
key = 'loaddata'
class Partition(Expression):
2420class Partition(Expression):
2421    arg_types = {"expressions": True, "subpartition": False}
arg_types = {'expressions': True, 'subpartition': False}
key = 'partition'
class PartitionRange(Expression):
2424class PartitionRange(Expression):
2425    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionrange'
class PartitionId(Expression):
2429class PartitionId(Expression):
2430    pass
key = 'partitionid'
class Fetch(Expression):
2433class Fetch(Expression):
2434    arg_types = {
2435        "direction": False,
2436        "count": False,
2437        "limit_options": False,
2438    }
arg_types = {'direction': False, 'count': False, 'limit_options': False}
key = 'fetch'
class Grant(Expression):
2441class Grant(Expression):
2442    arg_types = {
2443        "privileges": True,
2444        "kind": False,
2445        "securable": True,
2446        "principals": True,
2447        "grant_option": False,
2448    }
arg_types = {'privileges': True, 'kind': False, 'securable': True, 'principals': True, 'grant_option': False}
key = 'grant'
class Group(Expression):
2451class Group(Expression):
2452    arg_types = {
2453        "expressions": False,
2454        "grouping_sets": False,
2455        "cube": False,
2456        "rollup": False,
2457        "totals": False,
2458        "all": False,
2459    }
arg_types = {'expressions': False, 'grouping_sets': False, 'cube': False, 'rollup': False, 'totals': False, 'all': False}
key = 'group'
class Cube(Expression):
2462class Cube(Expression):
2463    arg_types = {"expressions": False}
arg_types = {'expressions': False}
key = 'cube'
class Rollup(Expression):
2466class Rollup(Expression):
2467    arg_types = {"expressions": False}
arg_types = {'expressions': False}
key = 'rollup'
class GroupingSets(Expression):
2470class GroupingSets(Expression):
2471    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'groupingsets'
class Lambda(Expression):
2474class Lambda(Expression):
2475    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'lambda'
class Limit(Expression):
2478class Limit(Expression):
2479    arg_types = {
2480        "this": False,
2481        "expression": True,
2482        "offset": False,
2483        "limit_options": False,
2484        "expressions": False,
2485    }
arg_types = {'this': False, 'expression': True, 'offset': False, 'limit_options': False, 'expressions': False}
key = 'limit'
class LimitOptions(Expression):
2488class LimitOptions(Expression):
2489    arg_types = {
2490        "percent": False,
2491        "rows": False,
2492        "with_ties": False,
2493    }
arg_types = {'percent': False, 'rows': False, 'with_ties': False}
key = 'limitoptions'
class Literal(Condition):
2496class Literal(Condition):
2497    arg_types = {"this": True, "is_string": True}
2498
2499    @property
2500    def hashable_args(self) -> t.Any:
2501        return (self.this, self.args.get("is_string"))
2502
2503    @classmethod
2504    def number(cls, number) -> Literal:
2505        return cls(this=str(number), is_string=False)
2506
2507    @classmethod
2508    def string(cls, string) -> Literal:
2509        return cls(this=str(string), is_string=True)
2510
2511    @property
2512    def output_name(self) -> str:
2513        return self.name
2514
2515    def to_py(self) -> int | str | Decimal:
2516        if self.is_number:
2517            try:
2518                return int(self.this)
2519            except ValueError:
2520                return Decimal(self.this)
2521        return self.this
arg_types = {'this': True, 'is_string': True}
hashable_args: Any
2499    @property
2500    def hashable_args(self) -> t.Any:
2501        return (self.this, self.args.get("is_string"))
@classmethod
def number(cls, number) -> Literal:
2503    @classmethod
2504    def number(cls, number) -> Literal:
2505        return cls(this=str(number), is_string=False)
@classmethod
def string(cls, string) -> Literal:
2507    @classmethod
2508    def string(cls, string) -> Literal:
2509        return cls(this=str(string), is_string=True)
output_name: str
2511    @property
2512    def output_name(self) -> str:
2513        return self.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
def to_py(self) -> int | str | decimal.Decimal:
2515    def to_py(self) -> int | str | Decimal:
2516        if self.is_number:
2517            try:
2518                return int(self.this)
2519            except ValueError:
2520                return Decimal(self.this)
2521        return self.this

Returns a Python object equivalent of the SQL node.

key = 'literal'
class Join(Expression):
2524class Join(Expression):
2525    arg_types = {
2526        "this": True,
2527        "on": False,
2528        "side": False,
2529        "kind": False,
2530        "using": False,
2531        "method": False,
2532        "global": False,
2533        "hint": False,
2534        "match_condition": False,  # Snowflake
2535        "expressions": False,
2536        "pivots": False,
2537    }
2538
2539    @property
2540    def method(self) -> str:
2541        return self.text("method").upper()
2542
2543    @property
2544    def kind(self) -> str:
2545        return self.text("kind").upper()
2546
2547    @property
2548    def side(self) -> str:
2549        return self.text("side").upper()
2550
2551    @property
2552    def hint(self) -> str:
2553        return self.text("hint").upper()
2554
2555    @property
2556    def alias_or_name(self) -> str:
2557        return self.this.alias_or_name
2558
2559    @property
2560    def is_semi_or_anti_join(self) -> bool:
2561        return self.kind in ("SEMI", "ANTI")
2562
2563    def on(
2564        self,
2565        *expressions: t.Optional[ExpOrStr],
2566        append: bool = True,
2567        dialect: DialectType = None,
2568        copy: bool = True,
2569        **opts,
2570    ) -> Join:
2571        """
2572        Append to or set the ON expressions.
2573
2574        Example:
2575            >>> import sqlglot
2576            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2577            'JOIN x ON y = 1'
2578
2579        Args:
2580            *expressions: the SQL code strings to parse.
2581                If an `Expression` instance is passed, it will be used as-is.
2582                Multiple expressions are combined with an AND operator.
2583            append: if `True`, AND the new expressions to any existing expression.
2584                Otherwise, this resets the expression.
2585            dialect: the dialect used to parse the input expressions.
2586            copy: if `False`, modify this expression instance in-place.
2587            opts: other options to use to parse the input expressions.
2588
2589        Returns:
2590            The modified Join expression.
2591        """
2592        join = _apply_conjunction_builder(
2593            *expressions,
2594            instance=self,
2595            arg="on",
2596            append=append,
2597            dialect=dialect,
2598            copy=copy,
2599            **opts,
2600        )
2601
2602        if join.kind == "CROSS":
2603            join.set("kind", None)
2604
2605        return join
2606
2607    def using(
2608        self,
2609        *expressions: t.Optional[ExpOrStr],
2610        append: bool = True,
2611        dialect: DialectType = None,
2612        copy: bool = True,
2613        **opts,
2614    ) -> Join:
2615        """
2616        Append to or set the USING expressions.
2617
2618        Example:
2619            >>> import sqlglot
2620            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2621            'JOIN x USING (foo, bla)'
2622
2623        Args:
2624            *expressions: the SQL code strings to parse.
2625                If an `Expression` instance is passed, it will be used as-is.
2626            append: if `True`, concatenate the new expressions to the existing "using" list.
2627                Otherwise, this resets the expression.
2628            dialect: the dialect used to parse the input expressions.
2629            copy: if `False`, modify this expression instance in-place.
2630            opts: other options to use to parse the input expressions.
2631
2632        Returns:
2633            The modified Join expression.
2634        """
2635        join = _apply_list_builder(
2636            *expressions,
2637            instance=self,
2638            arg="using",
2639            append=append,
2640            dialect=dialect,
2641            copy=copy,
2642            **opts,
2643        )
2644
2645        if join.kind == "CROSS":
2646            join.set("kind", None)
2647
2648        return join
arg_types = {'this': True, 'on': False, 'side': False, 'kind': False, 'using': False, 'method': False, 'global': False, 'hint': False, 'match_condition': False, 'expressions': False, 'pivots': False}
method: str
2539    @property
2540    def method(self) -> str:
2541        return self.text("method").upper()
kind: str
2543    @property
2544    def kind(self) -> str:
2545        return self.text("kind").upper()
side: str
2547    @property
2548    def side(self) -> str:
2549        return self.text("side").upper()
hint: str
2551    @property
2552    def hint(self) -> str:
2553        return self.text("hint").upper()
alias_or_name: str
2555    @property
2556    def alias_or_name(self) -> str:
2557        return self.this.alias_or_name
is_semi_or_anti_join: bool
2559    @property
2560    def is_semi_or_anti_join(self) -> bool:
2561        return self.kind in ("SEMI", "ANTI")
def on( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Join:
2563    def on(
2564        self,
2565        *expressions: t.Optional[ExpOrStr],
2566        append: bool = True,
2567        dialect: DialectType = None,
2568        copy: bool = True,
2569        **opts,
2570    ) -> Join:
2571        """
2572        Append to or set the ON expressions.
2573
2574        Example:
2575            >>> import sqlglot
2576            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2577            'JOIN x ON y = 1'
2578
2579        Args:
2580            *expressions: the SQL code strings to parse.
2581                If an `Expression` instance is passed, it will be used as-is.
2582                Multiple expressions are combined with an AND operator.
2583            append: if `True`, AND the new expressions to any existing expression.
2584                Otherwise, this resets the expression.
2585            dialect: the dialect used to parse the input expressions.
2586            copy: if `False`, modify this expression instance in-place.
2587            opts: other options to use to parse the input expressions.
2588
2589        Returns:
2590            The modified Join expression.
2591        """
2592        join = _apply_conjunction_builder(
2593            *expressions,
2594            instance=self,
2595            arg="on",
2596            append=append,
2597            dialect=dialect,
2598            copy=copy,
2599            **opts,
2600        )
2601
2602        if join.kind == "CROSS":
2603            join.set("kind", None)
2604
2605        return join

Append to or set the ON expressions.

Example:
>>> import sqlglot
>>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
'JOIN x ON y = 1'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is. Multiple expressions are combined with an AND operator.
  • append: if True, AND the new expressions to any existing expression. Otherwise, this resets the expression.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Join expression.

def using( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Join:
2607    def using(
2608        self,
2609        *expressions: t.Optional[ExpOrStr],
2610        append: bool = True,
2611        dialect: DialectType = None,
2612        copy: bool = True,
2613        **opts,
2614    ) -> Join:
2615        """
2616        Append to or set the USING expressions.
2617
2618        Example:
2619            >>> import sqlglot
2620            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2621            'JOIN x USING (foo, bla)'
2622
2623        Args:
2624            *expressions: the SQL code strings to parse.
2625                If an `Expression` instance is passed, it will be used as-is.
2626            append: if `True`, concatenate the new expressions to the existing "using" list.
2627                Otherwise, this resets the expression.
2628            dialect: the dialect used to parse the input expressions.
2629            copy: if `False`, modify this expression instance in-place.
2630            opts: other options to use to parse the input expressions.
2631
2632        Returns:
2633            The modified Join expression.
2634        """
2635        join = _apply_list_builder(
2636            *expressions,
2637            instance=self,
2638            arg="using",
2639            append=append,
2640            dialect=dialect,
2641            copy=copy,
2642            **opts,
2643        )
2644
2645        if join.kind == "CROSS":
2646            join.set("kind", None)
2647
2648        return join

Append to or set the USING expressions.

Example:
>>> import sqlglot
>>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
'JOIN x USING (foo, bla)'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • append: if True, concatenate the new expressions to the existing "using" list. Otherwise, this resets the expression.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Join expression.

key = 'join'
class Lateral(UDTF):
2651class Lateral(UDTF):
2652    arg_types = {
2653        "this": True,
2654        "view": False,
2655        "outer": False,
2656        "alias": False,
2657        "cross_apply": False,  # True -> CROSS APPLY, False -> OUTER APPLY
2658        "ordinality": False,
2659    }
arg_types = {'this': True, 'view': False, 'outer': False, 'alias': False, 'cross_apply': False, 'ordinality': False}
key = 'lateral'
class TableFromRows(UDTF):
2664class TableFromRows(UDTF):
2665    arg_types = {
2666        "this": True,
2667        "alias": False,
2668        "joins": False,
2669        "pivots": False,
2670        "sample": False,
2671    }
arg_types = {'this': True, 'alias': False, 'joins': False, 'pivots': False, 'sample': False}
key = 'tablefromrows'
class MatchRecognizeMeasure(Expression):
2674class MatchRecognizeMeasure(Expression):
2675    arg_types = {
2676        "this": True,
2677        "window_frame": False,
2678    }
arg_types = {'this': True, 'window_frame': False}
key = 'matchrecognizemeasure'
class MatchRecognize(Expression):
2681class MatchRecognize(Expression):
2682    arg_types = {
2683        "partition_by": False,
2684        "order": False,
2685        "measures": False,
2686        "rows": False,
2687        "after": False,
2688        "pattern": False,
2689        "define": False,
2690        "alias": False,
2691    }
arg_types = {'partition_by': False, 'order': False, 'measures': False, 'rows': False, 'after': False, 'pattern': False, 'define': False, 'alias': False}
key = 'matchrecognize'
class Final(Expression):
2696class Final(Expression):
2697    pass
key = 'final'
class Offset(Expression):
2700class Offset(Expression):
2701    arg_types = {"this": False, "expression": True, "expressions": False}
arg_types = {'this': False, 'expression': True, 'expressions': False}
key = 'offset'
class Order(Expression):
2704class Order(Expression):
2705    arg_types = {"this": False, "expressions": True, "siblings": False}
arg_types = {'this': False, 'expressions': True, 'siblings': False}
key = 'order'
class WithFill(Expression):
2709class WithFill(Expression):
2710    arg_types = {
2711        "from": False,
2712        "to": False,
2713        "step": False,
2714        "interpolate": False,
2715    }
arg_types = {'from': False, 'to': False, 'step': False, 'interpolate': False}
key = 'withfill'
class Cluster(Order):
2720class Cluster(Order):
2721    pass
key = 'cluster'
class Distribute(Order):
2724class Distribute(Order):
2725    pass
key = 'distribute'
class Sort(Order):
2728class Sort(Order):
2729    pass
key = 'sort'
class Ordered(Expression):
2732class Ordered(Expression):
2733    arg_types = {"this": True, "desc": False, "nulls_first": True, "with_fill": False}
2734
2735    @property
2736    def name(self) -> str:
2737        return self.this.name
arg_types = {'this': True, 'desc': False, 'nulls_first': True, 'with_fill': False}
name: str
2735    @property
2736    def name(self) -> str:
2737        return self.this.name
key = 'ordered'
class Property(Expression):
2740class Property(Expression):
2741    arg_types = {"this": True, "value": True}
arg_types = {'this': True, 'value': True}
key = 'property'
class GrantPrivilege(Expression):
2744class GrantPrivilege(Expression):
2745    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'grantprivilege'
class GrantPrincipal(Expression):
2748class GrantPrincipal(Expression):
2749    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'grantprincipal'
class AllowedValuesProperty(Expression):
2752class AllowedValuesProperty(Expression):
2753    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'allowedvaluesproperty'
class AlgorithmProperty(Property):
2756class AlgorithmProperty(Property):
2757    arg_types = {"this": True}
arg_types = {'this': True}
key = 'algorithmproperty'
class AutoIncrementProperty(Property):
2760class AutoIncrementProperty(Property):
2761    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autoincrementproperty'
class AutoRefreshProperty(Property):
2765class AutoRefreshProperty(Property):
2766    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autorefreshproperty'
class BackupProperty(Property):
2769class BackupProperty(Property):
2770    arg_types = {"this": True}
arg_types = {'this': True}
key = 'backupproperty'
class BlockCompressionProperty(Property):
2773class BlockCompressionProperty(Property):
2774    arg_types = {
2775        "autotemp": False,
2776        "always": False,
2777        "default": False,
2778        "manual": False,
2779        "never": False,
2780    }
arg_types = {'autotemp': False, 'always': False, 'default': False, 'manual': False, 'never': False}
key = 'blockcompressionproperty'
class CharacterSetProperty(Property):
2783class CharacterSetProperty(Property):
2784    arg_types = {"this": True, "default": True}
arg_types = {'this': True, 'default': True}
key = 'charactersetproperty'
class ChecksumProperty(Property):
2787class ChecksumProperty(Property):
2788    arg_types = {"on": False, "default": False}
arg_types = {'on': False, 'default': False}
key = 'checksumproperty'
class CollateProperty(Property):
2791class CollateProperty(Property):
2792    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'collateproperty'
class CopyGrantsProperty(Property):
2795class CopyGrantsProperty(Property):
2796    arg_types = {}
arg_types = {}
key = 'copygrantsproperty'
class DataBlocksizeProperty(Property):
2799class DataBlocksizeProperty(Property):
2800    arg_types = {
2801        "size": False,
2802        "units": False,
2803        "minimum": False,
2804        "maximum": False,
2805        "default": False,
2806    }
arg_types = {'size': False, 'units': False, 'minimum': False, 'maximum': False, 'default': False}
key = 'datablocksizeproperty'
class DataDeletionProperty(Property):
2809class DataDeletionProperty(Property):
2810    arg_types = {"on": True, "filter_col": False, "retention_period": False}
arg_types = {'on': True, 'filter_col': False, 'retention_period': False}
key = 'datadeletionproperty'
class DefinerProperty(Property):
2813class DefinerProperty(Property):
2814    arg_types = {"this": True}
arg_types = {'this': True}
key = 'definerproperty'
class DistKeyProperty(Property):
2817class DistKeyProperty(Property):
2818    arg_types = {"this": True}
arg_types = {'this': True}
key = 'distkeyproperty'
class DistributedByProperty(Property):
2823class DistributedByProperty(Property):
2824    arg_types = {"expressions": False, "kind": True, "buckets": False, "order": False}
arg_types = {'expressions': False, 'kind': True, 'buckets': False, 'order': False}
key = 'distributedbyproperty'
class DistStyleProperty(Property):
2827class DistStyleProperty(Property):
2828    arg_types = {"this": True}
arg_types = {'this': True}
key = 'diststyleproperty'
class DuplicateKeyProperty(Property):
2831class DuplicateKeyProperty(Property):
2832    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'duplicatekeyproperty'
class EngineProperty(Property):
2835class EngineProperty(Property):
2836    arg_types = {"this": True}
arg_types = {'this': True}
key = 'engineproperty'
class HeapProperty(Property):
2839class HeapProperty(Property):
2840    arg_types = {}
arg_types = {}
key = 'heapproperty'
class ToTableProperty(Property):
2843class ToTableProperty(Property):
2844    arg_types = {"this": True}
arg_types = {'this': True}
key = 'totableproperty'
class ExecuteAsProperty(Property):
2847class ExecuteAsProperty(Property):
2848    arg_types = {"this": True}
arg_types = {'this': True}
key = 'executeasproperty'
class ExternalProperty(Property):
2851class ExternalProperty(Property):
2852    arg_types = {"this": False}
arg_types = {'this': False}
key = 'externalproperty'
class FallbackProperty(Property):
2855class FallbackProperty(Property):
2856    arg_types = {"no": True, "protection": False}
arg_types = {'no': True, 'protection': False}
key = 'fallbackproperty'
class FileFormatProperty(Property):
2859class FileFormatProperty(Property):
2860    arg_types = {"this": False, "expressions": False}
arg_types = {'this': False, 'expressions': False}
key = 'fileformatproperty'
class CredentialsProperty(Property):
2863class CredentialsProperty(Property):
2864    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'credentialsproperty'
class FreespaceProperty(Property):
2867class FreespaceProperty(Property):
2868    arg_types = {"this": True, "percent": False}
arg_types = {'this': True, 'percent': False}
key = 'freespaceproperty'
class GlobalProperty(Property):
2871class GlobalProperty(Property):
2872    arg_types = {}
arg_types = {}
key = 'globalproperty'
class IcebergProperty(Property):
2875class IcebergProperty(Property):
2876    arg_types = {}
arg_types = {}
key = 'icebergproperty'
class InheritsProperty(Property):
2879class InheritsProperty(Property):
2880    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'inheritsproperty'
class InputModelProperty(Property):
2883class InputModelProperty(Property):
2884    arg_types = {"this": True}
arg_types = {'this': True}
key = 'inputmodelproperty'
class OutputModelProperty(Property):
2887class OutputModelProperty(Property):
2888    arg_types = {"this": True}
arg_types = {'this': True}
key = 'outputmodelproperty'
class IsolatedLoadingProperty(Property):
2891class IsolatedLoadingProperty(Property):
2892    arg_types = {"no": False, "concurrent": False, "target": False}
arg_types = {'no': False, 'concurrent': False, 'target': False}
key = 'isolatedloadingproperty'
class JournalProperty(Property):
2895class JournalProperty(Property):
2896    arg_types = {
2897        "no": False,
2898        "dual": False,
2899        "before": False,
2900        "local": False,
2901        "after": False,
2902    }
arg_types = {'no': False, 'dual': False, 'before': False, 'local': False, 'after': False}
key = 'journalproperty'
class LanguageProperty(Property):
2905class LanguageProperty(Property):
2906    arg_types = {"this": True}
arg_types = {'this': True}
key = 'languageproperty'
class EnviromentProperty(Property):
2909class EnviromentProperty(Property):
2910    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'enviromentproperty'
class ClusteredByProperty(Property):
2914class ClusteredByProperty(Property):
2915    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
arg_types = {'expressions': True, 'sorted_by': False, 'buckets': True}
key = 'clusteredbyproperty'
class DictProperty(Property):
2918class DictProperty(Property):
2919    arg_types = {"this": True, "kind": True, "settings": False}
arg_types = {'this': True, 'kind': True, 'settings': False}
key = 'dictproperty'
class DictSubProperty(Property):
2922class DictSubProperty(Property):
2923    pass
key = 'dictsubproperty'
class DictRange(Property):
2926class DictRange(Property):
2927    arg_types = {"this": True, "min": True, "max": True}
arg_types = {'this': True, 'min': True, 'max': True}
key = 'dictrange'
class DynamicProperty(Property):
2930class DynamicProperty(Property):
2931    arg_types = {}
arg_types = {}
key = 'dynamicproperty'
class OnCluster(Property):
2936class OnCluster(Property):
2937    arg_types = {"this": True}
arg_types = {'this': True}
key = 'oncluster'
class EmptyProperty(Property):
2941class EmptyProperty(Property):
2942    arg_types = {}
arg_types = {}
key = 'emptyproperty'
class LikeProperty(Property):
2945class LikeProperty(Property):
2946    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'likeproperty'
class LocationProperty(Property):
2949class LocationProperty(Property):
2950    arg_types = {"this": True}
arg_types = {'this': True}
key = 'locationproperty'
class LockProperty(Property):
2953class LockProperty(Property):
2954    arg_types = {"this": True}
arg_types = {'this': True}
key = 'lockproperty'
class LockingProperty(Property):
2957class LockingProperty(Property):
2958    arg_types = {
2959        "this": False,
2960        "kind": True,
2961        "for_or_in": False,
2962        "lock_type": True,
2963        "override": False,
2964    }
arg_types = {'this': False, 'kind': True, 'for_or_in': False, 'lock_type': True, 'override': False}
key = 'lockingproperty'
class LogProperty(Property):
2967class LogProperty(Property):
2968    arg_types = {"no": True}
arg_types = {'no': True}
key = 'logproperty'
class MaterializedProperty(Property):
2971class MaterializedProperty(Property):
2972    arg_types = {"this": False}
arg_types = {'this': False}
key = 'materializedproperty'
class MergeBlockRatioProperty(Property):
2975class MergeBlockRatioProperty(Property):
2976    arg_types = {"this": False, "no": False, "default": False, "percent": False}
arg_types = {'this': False, 'no': False, 'default': False, 'percent': False}
key = 'mergeblockratioproperty'
class NoPrimaryIndexProperty(Property):
2979class NoPrimaryIndexProperty(Property):
2980    arg_types = {}
arg_types = {}
key = 'noprimaryindexproperty'
class OnProperty(Property):
2983class OnProperty(Property):
2984    arg_types = {"this": True}
arg_types = {'this': True}
key = 'onproperty'
class OnCommitProperty(Property):
2987class OnCommitProperty(Property):
2988    arg_types = {"delete": False}
arg_types = {'delete': False}
key = 'oncommitproperty'
class PartitionedByProperty(Property):
2991class PartitionedByProperty(Property):
2992    arg_types = {"this": True}
arg_types = {'this': True}
key = 'partitionedbyproperty'
class PartitionedByBucket(Property):
2995class PartitionedByBucket(Property):
2996    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionedbybucket'
class PartitionByTruncate(Property):
2999class PartitionByTruncate(Property):
3000    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionbytruncate'
class PartitionByRangeProperty(Property):
3004class PartitionByRangeProperty(Property):
3005    arg_types = {"partition_expressions": True, "create_expressions": True}
arg_types = {'partition_expressions': True, 'create_expressions': True}
key = 'partitionbyrangeproperty'
class PartitionByRangePropertyDynamic(Expression):
3009class PartitionByRangePropertyDynamic(Expression):
3010    arg_types = {"this": False, "start": True, "end": True, "every": True}
arg_types = {'this': False, 'start': True, 'end': True, 'every': True}
key = 'partitionbyrangepropertydynamic'
class UniqueKeyProperty(Property):
3014class UniqueKeyProperty(Property):
3015    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'uniquekeyproperty'
class PartitionBoundSpec(Expression):
3019class PartitionBoundSpec(Expression):
3020    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
3021    arg_types = {
3022        "this": False,
3023        "expression": False,
3024        "from_expressions": False,
3025        "to_expressions": False,
3026    }
arg_types = {'this': False, 'expression': False, 'from_expressions': False, 'to_expressions': False}
key = 'partitionboundspec'
class PartitionedOfProperty(Property):
3029class PartitionedOfProperty(Property):
3030    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
3031    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionedofproperty'
class StreamingTableProperty(Property):
3034class StreamingTableProperty(Property):
3035    arg_types = {}
arg_types = {}
key = 'streamingtableproperty'
class RemoteWithConnectionModelProperty(Property):
3038class RemoteWithConnectionModelProperty(Property):
3039    arg_types = {"this": True}
arg_types = {'this': True}
key = 'remotewithconnectionmodelproperty'
class ReturnsProperty(Property):
3042class ReturnsProperty(Property):
3043    arg_types = {"this": False, "is_table": False, "table": False, "null": False}
arg_types = {'this': False, 'is_table': False, 'table': False, 'null': False}
key = 'returnsproperty'
class StrictProperty(Property):
3046class StrictProperty(Property):
3047    arg_types = {}
arg_types = {}
key = 'strictproperty'
class RowFormatProperty(Property):
3050class RowFormatProperty(Property):
3051    arg_types = {"this": True}
arg_types = {'this': True}
key = 'rowformatproperty'
class RowFormatDelimitedProperty(Property):
3054class RowFormatDelimitedProperty(Property):
3055    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
3056    arg_types = {
3057        "fields": False,
3058        "escaped": False,
3059        "collection_items": False,
3060        "map_keys": False,
3061        "lines": False,
3062        "null": False,
3063        "serde": False,
3064    }
arg_types = {'fields': False, 'escaped': False, 'collection_items': False, 'map_keys': False, 'lines': False, 'null': False, 'serde': False}
key = 'rowformatdelimitedproperty'
class RowFormatSerdeProperty(Property):
3067class RowFormatSerdeProperty(Property):
3068    arg_types = {"this": True, "serde_properties": False}
arg_types = {'this': True, 'serde_properties': False}
key = 'rowformatserdeproperty'
class QueryTransform(Expression):
3072class QueryTransform(Expression):
3073    arg_types = {
3074        "expressions": True,
3075        "command_script": True,
3076        "schema": False,
3077        "row_format_before": False,
3078        "record_writer": False,
3079        "row_format_after": False,
3080        "record_reader": False,
3081    }
arg_types = {'expressions': True, 'command_script': True, 'schema': False, 'row_format_before': False, 'record_writer': False, 'row_format_after': False, 'record_reader': False}
key = 'querytransform'
class SampleProperty(Property):
3084class SampleProperty(Property):
3085    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sampleproperty'
class SecurityProperty(Property):
3089class SecurityProperty(Property):
3090    arg_types = {"this": True}
arg_types = {'this': True}
key = 'securityproperty'
class SchemaCommentProperty(Property):
3093class SchemaCommentProperty(Property):
3094    arg_types = {"this": True}
arg_types = {'this': True}
key = 'schemacommentproperty'
class SerdeProperties(Property):
3097class SerdeProperties(Property):
3098    arg_types = {"expressions": True, "with": False}
arg_types = {'expressions': True, 'with': False}
key = 'serdeproperties'
class SetProperty(Property):
3101class SetProperty(Property):
3102    arg_types = {"multi": True}
arg_types = {'multi': True}
key = 'setproperty'
class SharingProperty(Property):
3105class SharingProperty(Property):
3106    arg_types = {"this": False}
arg_types = {'this': False}
key = 'sharingproperty'
class SetConfigProperty(Property):
3109class SetConfigProperty(Property):
3110    arg_types = {"this": True}
arg_types = {'this': True}
key = 'setconfigproperty'
class SettingsProperty(Property):
3113class SettingsProperty(Property):
3114    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'settingsproperty'
class SortKeyProperty(Property):
3117class SortKeyProperty(Property):
3118    arg_types = {"this": True, "compound": False}
arg_types = {'this': True, 'compound': False}
key = 'sortkeyproperty'
class SqlReadWriteProperty(Property):
3121class SqlReadWriteProperty(Property):
3122    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sqlreadwriteproperty'
class SqlSecurityProperty(Property):
3125class SqlSecurityProperty(Property):
3126    arg_types = {"definer": True}
arg_types = {'definer': True}
key = 'sqlsecurityproperty'
class StabilityProperty(Property):
3129class StabilityProperty(Property):
3130    arg_types = {"this": True}
arg_types = {'this': True}
key = 'stabilityproperty'
class StorageHandlerProperty(Property):
3133class StorageHandlerProperty(Property):
3134    arg_types = {"this": True}
arg_types = {'this': True}
key = 'storagehandlerproperty'
class TemporaryProperty(Property):
3137class TemporaryProperty(Property):
3138    arg_types = {"this": False}
arg_types = {'this': False}
key = 'temporaryproperty'
class SecureProperty(Property):
3141class SecureProperty(Property):
3142    arg_types = {}
arg_types = {}
key = 'secureproperty'
class Tags(ColumnConstraintKind, Property):
3146class Tags(ColumnConstraintKind, Property):
3147    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'tags'
class TransformModelProperty(Property):
3150class TransformModelProperty(Property):
3151    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'transformmodelproperty'
class TransientProperty(Property):
3154class TransientProperty(Property):
3155    arg_types = {"this": False}
arg_types = {'this': False}
key = 'transientproperty'
class UnloggedProperty(Property):
3158class UnloggedProperty(Property):
3159    arg_types = {}
arg_types = {}
key = 'unloggedproperty'
class UsingTemplateProperty(Property):
3163class UsingTemplateProperty(Property):
3164    arg_types = {"this": True}
arg_types = {'this': True}
key = 'usingtemplateproperty'
class ViewAttributeProperty(Property):
3168class ViewAttributeProperty(Property):
3169    arg_types = {"this": True}
arg_types = {'this': True}
key = 'viewattributeproperty'
class VolatileProperty(Property):
3172class VolatileProperty(Property):
3173    arg_types = {"this": False}
arg_types = {'this': False}
key = 'volatileproperty'
class WithDataProperty(Property):
3176class WithDataProperty(Property):
3177    arg_types = {"no": True, "statistics": False}
arg_types = {'no': True, 'statistics': False}
key = 'withdataproperty'
class WithJournalTableProperty(Property):
3180class WithJournalTableProperty(Property):
3181    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withjournaltableproperty'
class WithSchemaBindingProperty(Property):
3184class WithSchemaBindingProperty(Property):
3185    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withschemabindingproperty'
class WithSystemVersioningProperty(Property):
3188class WithSystemVersioningProperty(Property):
3189    arg_types = {
3190        "on": False,
3191        "this": False,
3192        "data_consistency": False,
3193        "retention_period": False,
3194        "with": True,
3195    }
arg_types = {'on': False, 'this': False, 'data_consistency': False, 'retention_period': False, 'with': True}
key = 'withsystemversioningproperty'
class WithProcedureOptions(Property):
3198class WithProcedureOptions(Property):
3199    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withprocedureoptions'
class EncodeProperty(Property):
3202class EncodeProperty(Property):
3203    arg_types = {"this": True, "properties": False, "key": False}
arg_types = {'this': True, 'properties': False, 'key': False}
key = 'encodeproperty'
class IncludeProperty(Property):
3206class IncludeProperty(Property):
3207    arg_types = {"this": True, "alias": False, "column_def": False}
arg_types = {'this': True, 'alias': False, 'column_def': False}
key = 'includeproperty'
class ForceProperty(Property):
3210class ForceProperty(Property):
3211    arg_types = {}
arg_types = {}
key = 'forceproperty'
class Properties(Expression):
3214class Properties(Expression):
3215    arg_types = {"expressions": True}
3216
3217    NAME_TO_PROPERTY = {
3218        "ALGORITHM": AlgorithmProperty,
3219        "AUTO_INCREMENT": AutoIncrementProperty,
3220        "CHARACTER SET": CharacterSetProperty,
3221        "CLUSTERED_BY": ClusteredByProperty,
3222        "COLLATE": CollateProperty,
3223        "COMMENT": SchemaCommentProperty,
3224        "CREDENTIALS": CredentialsProperty,
3225        "DEFINER": DefinerProperty,
3226        "DISTKEY": DistKeyProperty,
3227        "DISTRIBUTED_BY": DistributedByProperty,
3228        "DISTSTYLE": DistStyleProperty,
3229        "ENGINE": EngineProperty,
3230        "EXECUTE AS": ExecuteAsProperty,
3231        "FORMAT": FileFormatProperty,
3232        "LANGUAGE": LanguageProperty,
3233        "LOCATION": LocationProperty,
3234        "LOCK": LockProperty,
3235        "PARTITIONED_BY": PartitionedByProperty,
3236        "RETURNS": ReturnsProperty,
3237        "ROW_FORMAT": RowFormatProperty,
3238        "SORTKEY": SortKeyProperty,
3239        "ENCODE": EncodeProperty,
3240        "INCLUDE": IncludeProperty,
3241    }
3242
3243    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
3244
3245    # CREATE property locations
3246    # Form: schema specified
3247    #   create [POST_CREATE]
3248    #     table a [POST_NAME]
3249    #     (b int) [POST_SCHEMA]
3250    #     with ([POST_WITH])
3251    #     index (b) [POST_INDEX]
3252    #
3253    # Form: alias selection
3254    #   create [POST_CREATE]
3255    #     table a [POST_NAME]
3256    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
3257    #     index (c) [POST_INDEX]
3258    class Location(AutoName):
3259        POST_CREATE = auto()
3260        POST_NAME = auto()
3261        POST_SCHEMA = auto()
3262        POST_WITH = auto()
3263        POST_ALIAS = auto()
3264        POST_EXPRESSION = auto()
3265        POST_INDEX = auto()
3266        UNSUPPORTED = auto()
3267
3268    @classmethod
3269    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3270        expressions = []
3271        for key, value in properties_dict.items():
3272            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3273            if property_cls:
3274                expressions.append(property_cls(this=convert(value)))
3275            else:
3276                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3277
3278        return cls(expressions=expressions)
arg_types = {'expressions': True}
NAME_TO_PROPERTY = {'ALGORITHM': <class 'AlgorithmProperty'>, 'AUTO_INCREMENT': <class 'AutoIncrementProperty'>, 'CHARACTER SET': <class 'CharacterSetProperty'>, 'CLUSTERED_BY': <class 'ClusteredByProperty'>, 'COLLATE': <class 'CollateProperty'>, 'COMMENT': <class 'SchemaCommentProperty'>, 'CREDENTIALS': <class 'CredentialsProperty'>, 'DEFINER': <class 'DefinerProperty'>, 'DISTKEY': <class 'DistKeyProperty'>, 'DISTRIBUTED_BY': <class 'DistributedByProperty'>, 'DISTSTYLE': <class 'DistStyleProperty'>, 'ENGINE': <class 'EngineProperty'>, 'EXECUTE AS': <class 'ExecuteAsProperty'>, 'FORMAT': <class 'FileFormatProperty'>, 'LANGUAGE': <class 'LanguageProperty'>, 'LOCATION': <class 'LocationProperty'>, 'LOCK': <class 'LockProperty'>, 'PARTITIONED_BY': <class 'PartitionedByProperty'>, 'RETURNS': <class 'ReturnsProperty'>, 'ROW_FORMAT': <class 'RowFormatProperty'>, 'SORTKEY': <class 'SortKeyProperty'>, 'ENCODE': <class 'EncodeProperty'>, 'INCLUDE': <class 'IncludeProperty'>}
PROPERTY_TO_NAME = {<class 'AlgorithmProperty'>: 'ALGORITHM', <class 'AutoIncrementProperty'>: 'AUTO_INCREMENT', <class 'CharacterSetProperty'>: 'CHARACTER SET', <class 'ClusteredByProperty'>: 'CLUSTERED_BY', <class 'CollateProperty'>: 'COLLATE', <class 'SchemaCommentProperty'>: 'COMMENT', <class 'CredentialsProperty'>: 'CREDENTIALS', <class 'DefinerProperty'>: 'DEFINER', <class 'DistKeyProperty'>: 'DISTKEY', <class 'DistributedByProperty'>: 'DISTRIBUTED_BY', <class 'DistStyleProperty'>: 'DISTSTYLE', <class 'EngineProperty'>: 'ENGINE', <class 'ExecuteAsProperty'>: 'EXECUTE AS', <class 'FileFormatProperty'>: 'FORMAT', <class 'LanguageProperty'>: 'LANGUAGE', <class 'LocationProperty'>: 'LOCATION', <class 'LockProperty'>: 'LOCK', <class 'PartitionedByProperty'>: 'PARTITIONED_BY', <class 'ReturnsProperty'>: 'RETURNS', <class 'RowFormatProperty'>: 'ROW_FORMAT', <class 'SortKeyProperty'>: 'SORTKEY', <class 'EncodeProperty'>: 'ENCODE', <class 'IncludeProperty'>: 'INCLUDE'}
@classmethod
def from_dict(cls, properties_dict: Dict) -> Properties:
3268    @classmethod
3269    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3270        expressions = []
3271        for key, value in properties_dict.items():
3272            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3273            if property_cls:
3274                expressions.append(property_cls(this=convert(value)))
3275            else:
3276                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3277
3278        return cls(expressions=expressions)
key = 'properties'
class Properties.Location(sqlglot.helper.AutoName):
3258    class Location(AutoName):
3259        POST_CREATE = auto()
3260        POST_NAME = auto()
3261        POST_SCHEMA = auto()
3262        POST_WITH = auto()
3263        POST_ALIAS = auto()
3264        POST_EXPRESSION = auto()
3265        POST_INDEX = auto()
3266        UNSUPPORTED = auto()

An enumeration.

POST_CREATE = <Location.POST_CREATE: 'POST_CREATE'>
POST_NAME = <Location.POST_NAME: 'POST_NAME'>
POST_SCHEMA = <Location.POST_SCHEMA: 'POST_SCHEMA'>
POST_WITH = <Location.POST_WITH: 'POST_WITH'>
POST_ALIAS = <Location.POST_ALIAS: 'POST_ALIAS'>
POST_EXPRESSION = <Location.POST_EXPRESSION: 'POST_EXPRESSION'>
POST_INDEX = <Location.POST_INDEX: 'POST_INDEX'>
UNSUPPORTED = <Location.UNSUPPORTED: 'UNSUPPORTED'>
class Qualify(Expression):
3281class Qualify(Expression):
3282    pass
key = 'qualify'
class InputOutputFormat(Expression):
3285class InputOutputFormat(Expression):
3286    arg_types = {"input_format": False, "output_format": False}
arg_types = {'input_format': False, 'output_format': False}
key = 'inputoutputformat'
class Return(Expression):
3290class Return(Expression):
3291    pass
key = 'return'
class Reference(Expression):
3294class Reference(Expression):
3295    arg_types = {"this": True, "expressions": False, "options": False}
arg_types = {'this': True, 'expressions': False, 'options': False}
key = 'reference'
class Tuple(Expression):
3298class Tuple(Expression):
3299    arg_types = {"expressions": False}
3300
3301    def isin(
3302        self,
3303        *expressions: t.Any,
3304        query: t.Optional[ExpOrStr] = None,
3305        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3306        copy: bool = True,
3307        **opts,
3308    ) -> In:
3309        return In(
3310            this=maybe_copy(self, copy),
3311            expressions=[convert(e, copy=copy) for e in expressions],
3312            query=maybe_parse(query, copy=copy, **opts) if query else None,
3313            unnest=(
3314                Unnest(
3315                    expressions=[
3316                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3317                        for e in ensure_list(unnest)
3318                    ]
3319                )
3320                if unnest
3321                else None
3322            ),
3323        )
arg_types = {'expressions': False}
def isin( self, *expressions: Any, query: Union[str, Expression, NoneType] = None, unnest: Union[str, Expression, NoneType, Collection[Union[str, Expression]]] = None, copy: bool = True, **opts) -> In:
3301    def isin(
3302        self,
3303        *expressions: t.Any,
3304        query: t.Optional[ExpOrStr] = None,
3305        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3306        copy: bool = True,
3307        **opts,
3308    ) -> In:
3309        return In(
3310            this=maybe_copy(self, copy),
3311            expressions=[convert(e, copy=copy) for e in expressions],
3312            query=maybe_parse(query, copy=copy, **opts) if query else None,
3313            unnest=(
3314                Unnest(
3315                    expressions=[
3316                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3317                        for e in ensure_list(unnest)
3318                    ]
3319                )
3320                if unnest
3321                else None
3322            ),
3323        )
key = 'tuple'
QUERY_MODIFIERS = {'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': False, 'prewhere': False, 'where': False, 'group': False, 'having': False, 'qualify': False, 'windows': False, 'distribute': False, 'sort': False, 'cluster': False, 'order': False, 'limit': False, 'offset': False, 'locks': False, 'sample': False, 'settings': False, 'format': False, 'options': False}
class QueryOption(Expression):
3354class QueryOption(Expression):
3355    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'queryoption'
class WithTableHint(Expression):
3359class WithTableHint(Expression):
3360    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withtablehint'
class IndexTableHint(Expression):
3364class IndexTableHint(Expression):
3365    arg_types = {"this": True, "expressions": False, "target": False}
arg_types = {'this': True, 'expressions': False, 'target': False}
key = 'indextablehint'
class HistoricalData(Expression):
3369class HistoricalData(Expression):
3370    arg_types = {"this": True, "kind": True, "expression": True}
arg_types = {'this': True, 'kind': True, 'expression': True}
key = 'historicaldata'
class Put(Expression):
3374class Put(Expression):
3375    arg_types = {"this": True, "target": True, "properties": False}
arg_types = {'this': True, 'target': True, 'properties': False}
key = 'put'
class Get(Expression):
3379class Get(Expression):
3380    arg_types = {"this": True, "target": True, "properties": False}
arg_types = {'this': True, 'target': True, 'properties': False}
key = 'get'
class Table(Expression):
3383class Table(Expression):
3384    arg_types = {
3385        "this": False,
3386        "alias": False,
3387        "db": False,
3388        "catalog": False,
3389        "laterals": False,
3390        "joins": False,
3391        "pivots": False,
3392        "hints": False,
3393        "system_time": False,
3394        "version": False,
3395        "format": False,
3396        "pattern": False,
3397        "ordinality": False,
3398        "when": False,
3399        "only": False,
3400        "partition": False,
3401        "changes": False,
3402        "rows_from": False,
3403        "sample": False,
3404    }
3405
3406    @property
3407    def name(self) -> str:
3408        if not self.this or isinstance(self.this, Func):
3409            return ""
3410        return self.this.name
3411
3412    @property
3413    def db(self) -> str:
3414        return self.text("db")
3415
3416    @property
3417    def catalog(self) -> str:
3418        return self.text("catalog")
3419
3420    @property
3421    def selects(self) -> t.List[Expression]:
3422        return []
3423
3424    @property
3425    def named_selects(self) -> t.List[str]:
3426        return []
3427
3428    @property
3429    def parts(self) -> t.List[Expression]:
3430        """Return the parts of a table in order catalog, db, table."""
3431        parts: t.List[Expression] = []
3432
3433        for arg in ("catalog", "db", "this"):
3434            part = self.args.get(arg)
3435
3436            if isinstance(part, Dot):
3437                parts.extend(part.flatten())
3438            elif isinstance(part, Expression):
3439                parts.append(part)
3440
3441        return parts
3442
3443    def to_column(self, copy: bool = True) -> Expression:
3444        parts = self.parts
3445        last_part = parts[-1]
3446
3447        if isinstance(last_part, Identifier):
3448            col: Expression = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3449        else:
3450            # This branch will be reached if a function or array is wrapped in a `Table`
3451            col = last_part
3452
3453        alias = self.args.get("alias")
3454        if alias:
3455            col = alias_(col, alias.this, copy=copy)
3456
3457        return col
arg_types = {'this': False, 'alias': False, 'db': False, 'catalog': False, 'laterals': False, 'joins': False, 'pivots': False, 'hints': False, 'system_time': False, 'version': False, 'format': False, 'pattern': False, 'ordinality': False, 'when': False, 'only': False, 'partition': False, 'changes': False, 'rows_from': False, 'sample': False}
name: str
3406    @property
3407    def name(self) -> str:
3408        if not self.this or isinstance(self.this, Func):
3409            return ""
3410        return self.this.name
db: str
3412    @property
3413    def db(self) -> str:
3414        return self.text("db")
catalog: str
3416    @property
3417    def catalog(self) -> str:
3418        return self.text("catalog")
selects: List[Expression]
3420    @property
3421    def selects(self) -> t.List[Expression]:
3422        return []
named_selects: List[str]
3424    @property
3425    def named_selects(self) -> t.List[str]:
3426        return []
parts: List[Expression]
3428    @property
3429    def parts(self) -> t.List[Expression]:
3430        """Return the parts of a table in order catalog, db, table."""
3431        parts: t.List[Expression] = []
3432
3433        for arg in ("catalog", "db", "this"):
3434            part = self.args.get(arg)
3435
3436            if isinstance(part, Dot):
3437                parts.extend(part.flatten())
3438            elif isinstance(part, Expression):
3439                parts.append(part)
3440
3441        return parts

Return the parts of a table in order catalog, db, table.

def to_column(self, copy: bool = True) -> Expression:
3443    def to_column(self, copy: bool = True) -> Expression:
3444        parts = self.parts
3445        last_part = parts[-1]
3446
3447        if isinstance(last_part, Identifier):
3448            col: Expression = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3449        else:
3450            # This branch will be reached if a function or array is wrapped in a `Table`
3451            col = last_part
3452
3453        alias = self.args.get("alias")
3454        if alias:
3455            col = alias_(col, alias.this, copy=copy)
3456
3457        return col
key = 'table'
class SetOperation(Query):
3460class SetOperation(Query):
3461    arg_types = {
3462        "with": False,
3463        "this": True,
3464        "expression": True,
3465        "distinct": False,
3466        "by_name": False,
3467        "side": False,
3468        "kind": False,
3469        "on": False,
3470        **QUERY_MODIFIERS,
3471    }
3472
3473    def select(
3474        self: S,
3475        *expressions: t.Optional[ExpOrStr],
3476        append: bool = True,
3477        dialect: DialectType = None,
3478        copy: bool = True,
3479        **opts,
3480    ) -> S:
3481        this = maybe_copy(self, copy)
3482        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3483        this.expression.unnest().select(
3484            *expressions, append=append, dialect=dialect, copy=False, **opts
3485        )
3486        return this
3487
3488    @property
3489    def named_selects(self) -> t.List[str]:
3490        return self.this.unnest().named_selects
3491
3492    @property
3493    def is_star(self) -> bool:
3494        return self.this.is_star or self.expression.is_star
3495
3496    @property
3497    def selects(self) -> t.List[Expression]:
3498        return self.this.unnest().selects
3499
3500    @property
3501    def left(self) -> Query:
3502        return self.this
3503
3504    @property
3505    def right(self) -> Query:
3506        return self.expression
3507
3508    @property
3509    def kind(self) -> str:
3510        return self.text("kind").upper()
3511
3512    @property
3513    def side(self) -> str:
3514        return self.text("side").upper()
arg_types = {'with': False, 'this': True, 'expression': True, 'distinct': False, 'by_name': False, 'side': False, 'kind': False, 'on': False, 'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': False, 'prewhere': False, 'where': False, 'group': False, 'having': False, 'qualify': False, 'windows': False, 'distribute': False, 'sort': False, 'cluster': False, 'order': False, 'limit': False, 'offset': False, 'locks': False, 'sample': False, 'settings': False, 'format': False, 'options': False}
def select( self: ~S, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~S:
3473    def select(
3474        self: S,
3475        *expressions: t.Optional[ExpOrStr],
3476        append: bool = True,
3477        dialect: DialectType = None,
3478        copy: bool = True,
3479        **opts,
3480    ) -> S:
3481        this = maybe_copy(self, copy)
3482        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3483        this.expression.unnest().select(
3484            *expressions, append=append, dialect=dialect, copy=False, **opts
3485        )
3486        return this

Append to or set the SELECT expressions.

Example:
>>> Select().select("x", "y").sql()
'SELECT x, y'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Query expression.

named_selects: List[str]
3488    @property
3489    def named_selects(self) -> t.List[str]:
3490        return self.this.unnest().named_selects

Returns the output names of the query's projections.

is_star: bool
3492    @property
3493    def is_star(self) -> bool:
3494        return self.this.is_star or self.expression.is_star

Checks whether an expression is a star.

selects: List[Expression]
3496    @property
3497    def selects(self) -> t.List[Expression]:
3498        return self.this.unnest().selects

Returns the query's projections.

left: Query
3500    @property
3501    def left(self) -> Query:
3502        return self.this
right: Query
3504    @property
3505    def right(self) -> Query:
3506        return self.expression
kind: str
3508    @property
3509    def kind(self) -> str:
3510        return self.text("kind").upper()
side: str
3512    @property
3513    def side(self) -> str:
3514        return self.text("side").upper()
key = 'setoperation'
class Union(SetOperation):
3517class Union(SetOperation):
3518    pass
key = 'union'
class Except(SetOperation):
3521class Except(SetOperation):
3522    pass
key = 'except'
class Intersect(SetOperation):
3525class Intersect(SetOperation):
3526    pass
key = 'intersect'
class Update(DML):
3529class Update(DML):
3530    arg_types = {
3531        "with": False,
3532        "this": False,
3533        "expressions": True,
3534        "from": False,
3535        "where": False,
3536        "returning": False,
3537        "order": False,
3538        "limit": False,
3539    }
3540
3541    def table(
3542        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3543    ) -> Update:
3544        """
3545        Set the table to update.
3546
3547        Example:
3548            >>> Update().table("my_table").set_("x = 1").sql()
3549            'UPDATE my_table SET x = 1'
3550
3551        Args:
3552            expression : the SQL code strings to parse.
3553                If a `Table` instance is passed, this is used as-is.
3554                If another `Expression` instance is passed, it will be wrapped in a `Table`.
3555            dialect: the dialect used to parse the input expression.
3556            copy: if `False`, modify this expression instance in-place.
3557            opts: other options to use to parse the input expressions.
3558
3559        Returns:
3560            The modified Update expression.
3561        """
3562        return _apply_builder(
3563            expression=expression,
3564            instance=self,
3565            arg="this",
3566            into=Table,
3567            prefix=None,
3568            dialect=dialect,
3569            copy=copy,
3570            **opts,
3571        )
3572
3573    def set_(
3574        self,
3575        *expressions: ExpOrStr,
3576        append: bool = True,
3577        dialect: DialectType = None,
3578        copy: bool = True,
3579        **opts,
3580    ) -> Update:
3581        """
3582        Append to or set the SET expressions.
3583
3584        Example:
3585            >>> Update().table("my_table").set_("x = 1").sql()
3586            'UPDATE my_table SET x = 1'
3587
3588        Args:
3589            *expressions: the SQL code strings to parse.
3590                If `Expression` instance(s) are passed, they will be used as-is.
3591                Multiple expressions are combined with a comma.
3592            append: if `True`, add the new expressions to any existing SET expressions.
3593                Otherwise, this resets the expressions.
3594            dialect: the dialect used to parse the input expressions.
3595            copy: if `False`, modify this expression instance in-place.
3596            opts: other options to use to parse the input expressions.
3597        """
3598        return _apply_list_builder(
3599            *expressions,
3600            instance=self,
3601            arg="expressions",
3602            append=append,
3603            into=Expression,
3604            prefix=None,
3605            dialect=dialect,
3606            copy=copy,
3607            **opts,
3608        )
3609
3610    def where(
3611        self,
3612        *expressions: t.Optional[ExpOrStr],
3613        append: bool = True,
3614        dialect: DialectType = None,
3615        copy: bool = True,
3616        **opts,
3617    ) -> Select:
3618        """
3619        Append to or set the WHERE expressions.
3620
3621        Example:
3622            >>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
3623            "UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
3624
3625        Args:
3626            *expressions: the SQL code strings to parse.
3627                If an `Expression` instance is passed, it will be used as-is.
3628                Multiple expressions are combined with an AND operator.
3629            append: if `True`, AND the new expressions to any existing expression.
3630                Otherwise, this resets the expression.
3631            dialect: the dialect used to parse the input expressions.
3632            copy: if `False`, modify this expression instance in-place.
3633            opts: other options to use to parse the input expressions.
3634
3635        Returns:
3636            Select: the modified expression.
3637        """
3638        return _apply_conjunction_builder(
3639            *expressions,
3640            instance=self,
3641            arg="where",
3642            append=append,
3643            into=Where,
3644            dialect=dialect,
3645            copy=copy,
3646            **opts,
3647        )
3648
3649    def from_(
3650        self,
3651        expression: t.Optional[ExpOrStr] = None,
3652        dialect: DialectType = None,
3653        copy: bool = True,
3654        **opts,
3655    ) -> Update:
3656        """
3657        Set the FROM expression.
3658
3659        Example:
3660            >>> Update().table("my_table").set_("x = 1").from_("baz").sql()
3661            'UPDATE my_table SET x = 1 FROM baz'
3662
3663        Args:
3664            expression : the SQL code strings to parse.
3665                If a `From` instance is passed, this is used as-is.
3666                If another `Expression` instance is passed, it will be wrapped in a `From`.
3667                If nothing is passed in then a from is not applied to the expression
3668            dialect: the dialect used to parse the input expression.
3669            copy: if `False`, modify this expression instance in-place.
3670            opts: other options to use to parse the input expressions.
3671
3672        Returns:
3673            The modified Update expression.
3674        """
3675        if not expression:
3676            return maybe_copy(self, copy)
3677
3678        return _apply_builder(
3679            expression=expression,
3680            instance=self,
3681            arg="from",
3682            into=From,
3683            prefix="FROM",
3684            dialect=dialect,
3685            copy=copy,
3686            **opts,
3687        )
3688
3689    def with_(
3690        self,
3691        alias: ExpOrStr,
3692        as_: ExpOrStr,
3693        recursive: t.Optional[bool] = None,
3694        materialized: t.Optional[bool] = None,
3695        append: bool = True,
3696        dialect: DialectType = None,
3697        copy: bool = True,
3698        **opts,
3699    ) -> Update:
3700        """
3701        Append to or set the common table expressions.
3702
3703        Example:
3704            >>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
3705            'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
3706
3707        Args:
3708            alias: the SQL code string to parse as the table name.
3709                If an `Expression` instance is passed, this is used as-is.
3710            as_: the SQL code string to parse as the table expression.
3711                If an `Expression` instance is passed, it will be used as-is.
3712            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
3713            materialized: set the MATERIALIZED part of the expression.
3714            append: if `True`, add to any existing expressions.
3715                Otherwise, this resets the expressions.
3716            dialect: the dialect used to parse the input expression.
3717            copy: if `False`, modify this expression instance in-place.
3718            opts: other options to use to parse the input expressions.
3719
3720        Returns:
3721            The modified expression.
3722        """
3723        return _apply_cte_builder(
3724            self,
3725            alias,
3726            as_,
3727            recursive=recursive,
3728            materialized=materialized,
3729            append=append,
3730            dialect=dialect,
3731            copy=copy,
3732            **opts,
3733        )
arg_types = {'with': False, 'this': False, 'expressions': True, 'from': False, 'where': False, 'returning': False, 'order': False, 'limit': False}
def table( self, expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3541    def table(
3542        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3543    ) -> Update:
3544        """
3545        Set the table to update.
3546
3547        Example:
3548            >>> Update().table("my_table").set_("x = 1").sql()
3549            'UPDATE my_table SET x = 1'
3550
3551        Args:
3552            expression : the SQL code strings to parse.
3553                If a `Table` instance is passed, this is used as-is.
3554                If another `Expression` instance is passed, it will be wrapped in a `Table`.
3555            dialect: the dialect used to parse the input expression.
3556            copy: if `False`, modify this expression instance in-place.
3557            opts: other options to use to parse the input expressions.
3558
3559        Returns:
3560            The modified Update expression.
3561        """
3562        return _apply_builder(
3563            expression=expression,
3564            instance=self,
3565            arg="this",
3566            into=Table,
3567            prefix=None,
3568            dialect=dialect,
3569            copy=copy,
3570            **opts,
3571        )

Set the table to update.

Example:
>>> Update().table("my_table").set_("x = 1").sql()
'UPDATE my_table SET x = 1'
Arguments:
  • expression : the SQL code strings to parse. If a Table instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a Table.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Update expression.

def set_( self, *expressions: Union[str, Expression], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3573    def set_(
3574        self,
3575        *expressions: ExpOrStr,
3576        append: bool = True,
3577        dialect: DialectType = None,
3578        copy: bool = True,
3579        **opts,
3580    ) -> Update:
3581        """
3582        Append to or set the SET expressions.
3583
3584        Example:
3585            >>> Update().table("my_table").set_("x = 1").sql()
3586            'UPDATE my_table SET x = 1'
3587
3588        Args:
3589            *expressions: the SQL code strings to parse.
3590                If `Expression` instance(s) are passed, they will be used as-is.
3591                Multiple expressions are combined with a comma.
3592            append: if `True`, add the new expressions to any existing SET expressions.
3593                Otherwise, this resets the expressions.
3594            dialect: the dialect used to parse the input expressions.
3595            copy: if `False`, modify this expression instance in-place.
3596            opts: other options to use to parse the input expressions.
3597        """
3598        return _apply_list_builder(
3599            *expressions,
3600            instance=self,
3601            arg="expressions",
3602            append=append,
3603            into=Expression,
3604            prefix=None,
3605            dialect=dialect,
3606            copy=copy,
3607            **opts,
3608        )

Append to or set the SET expressions.

Example:
>>> Update().table("my_table").set_("x = 1").sql()
'UPDATE my_table SET x = 1'
Arguments:
  • *expressions: the SQL code strings to parse. If Expression instance(s) are passed, they will be used as-is. Multiple expressions are combined with a comma.
  • append: if True, add the new expressions to any existing SET expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
def where( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3610    def where(
3611        self,
3612        *expressions: t.Optional[ExpOrStr],
3613        append: bool = True,
3614        dialect: DialectType = None,
3615        copy: bool = True,
3616        **opts,
3617    ) -> Select:
3618        """
3619        Append to or set the WHERE expressions.
3620
3621        Example:
3622            >>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
3623            "UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
3624
3625        Args:
3626            *expressions: the SQL code strings to parse.
3627                If an `Expression` instance is passed, it will be used as-is.
3628                Multiple expressions are combined with an AND operator.
3629            append: if `True`, AND the new expressions to any existing expression.
3630                Otherwise, this resets the expression.
3631            dialect: the dialect used to parse the input expressions.
3632            copy: if `False`, modify this expression instance in-place.
3633            opts: other options to use to parse the input expressions.
3634
3635        Returns:
3636            Select: the modified expression.
3637        """
3638        return _apply_conjunction_builder(
3639            *expressions,
3640            instance=self,
3641            arg="where",
3642            append=append,
3643            into=Where,
3644            dialect=dialect,
3645            copy=copy,
3646            **opts,
3647        )

Append to or set the WHERE expressions.

Example:
>>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
"UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is. Multiple expressions are combined with an AND operator.
  • append: if True, AND the new expressions to any existing expression. Otherwise, this resets the expression.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

Select: the modified expression.

def from_( self, expression: Union[str, Expression, NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3649    def from_(
3650        self,
3651        expression: t.Optional[ExpOrStr] = None,
3652        dialect: DialectType = None,
3653        copy: bool = True,
3654        **opts,
3655    ) -> Update:
3656        """
3657        Set the FROM expression.
3658
3659        Example:
3660            >>> Update().table("my_table").set_("x = 1").from_("baz").sql()
3661            'UPDATE my_table SET x = 1 FROM baz'
3662
3663        Args:
3664            expression : the SQL code strings to parse.
3665                If a `From` instance is passed, this is used as-is.
3666                If another `Expression` instance is passed, it will be wrapped in a `From`.
3667                If nothing is passed in then a from is not applied to the expression
3668            dialect: the dialect used to parse the input expression.
3669            copy: if `False`, modify this expression instance in-place.
3670            opts: other options to use to parse the input expressions.
3671
3672        Returns:
3673            The modified Update expression.
3674        """
3675        if not expression:
3676            return maybe_copy(self, copy)
3677
3678        return _apply_builder(
3679            expression=expression,
3680            instance=self,
3681            arg="from",
3682            into=From,
3683            prefix="FROM",
3684            dialect=dialect,
3685            copy=copy,
3686            **opts,
3687        )

Set the FROM expression.

Example:
>>> Update().table("my_table").set_("x = 1").from_("baz").sql()
'UPDATE my_table SET x = 1 FROM baz'
Arguments:
  • expression : the SQL code strings to parse. If a From instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a From. If nothing is passed in then a from is not applied to the expression
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Update expression.

def with_( self, alias: Union[str, Expression], as_: Union[str, Expression], recursive: Optional[bool] = None, materialized: Optional[bool] = None, append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3689    def with_(
3690        self,
3691        alias: ExpOrStr,
3692        as_: ExpOrStr,
3693        recursive: t.Optional[bool] = None,
3694        materialized: t.Optional[bool] = None,
3695        append: bool = True,
3696        dialect: DialectType = None,
3697        copy: bool = True,
3698        **opts,
3699    ) -> Update:
3700        """
3701        Append to or set the common table expressions.
3702
3703        Example:
3704            >>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
3705            'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
3706
3707        Args:
3708            alias: the SQL code string to parse as the table name.
3709                If an `Expression` instance is passed, this is used as-is.
3710            as_: the SQL code string to parse as the table expression.
3711                If an `Expression` instance is passed, it will be used as-is.
3712            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
3713            materialized: set the MATERIALIZED part of the expression.
3714            append: if `True`, add to any existing expressions.
3715                Otherwise, this resets the expressions.
3716            dialect: the dialect used to parse the input expression.
3717            copy: if `False`, modify this expression instance in-place.
3718            opts: other options to use to parse the input expressions.
3719
3720        Returns:
3721            The modified expression.
3722        """
3723        return _apply_cte_builder(
3724            self,
3725            alias,
3726            as_,
3727            recursive=recursive,
3728            materialized=materialized,
3729            append=append,
3730            dialect=dialect,
3731            copy=copy,
3732            **opts,
3733        )

Append to or set the common table expressions.

Example:
>>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
Arguments:
  • alias: the SQL code string to parse as the table name. If an Expression instance is passed, this is used as-is.
  • as_: the SQL code string to parse as the table expression. If an Expression instance is passed, it will be used as-is.
  • recursive: set the RECURSIVE part of the expression. Defaults to False.
  • materialized: set the MATERIALIZED part of the expression.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified expression.

key = 'update'
class Values(UDTF):
3736class Values(UDTF):
3737    arg_types = {"expressions": True, "alias": False}
arg_types = {'expressions': True, 'alias': False}
key = 'values'
class Var(Expression):
3740class Var(Expression):
3741    pass
key = 'var'
class Version(Expression):
3744class Version(Expression):
3745    """
3746    Time travel, iceberg, bigquery etc
3747    https://trino.io/docs/current/connector/iceberg.html?highlight=snapshot#using-snapshots
3748    https://www.databricks.com/blog/2019/02/04/introducing-delta-time-travel-for-large-scale-data-lakes.html
3749    https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#for_system_time_as_of
3750    https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-ver16
3751    this is either TIMESTAMP or VERSION
3752    kind is ("AS OF", "BETWEEN")
3753    """
3754
3755    arg_types = {"this": True, "kind": True, "expression": False}
arg_types = {'this': True, 'kind': True, 'expression': False}
key = 'version'
class Schema(Expression):
3758class Schema(Expression):
3759    arg_types = {"this": False, "expressions": False}
arg_types = {'this': False, 'expressions': False}
key = 'schema'
class Lock(Expression):
3764class Lock(Expression):
3765    arg_types = {"update": True, "expressions": False, "wait": False}
arg_types = {'update': True, 'expressions': False, 'wait': False}
key = 'lock'
class Select(Query):
3768class Select(Query):
3769    arg_types = {
3770        "with": False,
3771        "kind": False,
3772        "expressions": False,
3773        "hint": False,
3774        "distinct": False,
3775        "into": False,
3776        "from": False,
3777        "operation_modifiers": False,
3778        **QUERY_MODIFIERS,
3779    }
3780
3781    def from_(
3782        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3783    ) -> Select:
3784        """
3785        Set the FROM expression.
3786
3787        Example:
3788            >>> Select().from_("tbl").select("x").sql()
3789            'SELECT x FROM tbl'
3790
3791        Args:
3792            expression : the SQL code strings to parse.
3793                If a `From` instance is passed, this is used as-is.
3794                If another `Expression` instance is passed, it will be wrapped in a `From`.
3795            dialect: the dialect used to parse the input expression.
3796            copy: if `False`, modify this expression instance in-place.
3797            opts: other options to use to parse the input expressions.
3798
3799        Returns:
3800            The modified Select expression.
3801        """
3802        return _apply_builder(
3803            expression=expression,
3804            instance=self,
3805            arg="from",
3806            into=From,
3807            prefix="FROM",
3808            dialect=dialect,
3809            copy=copy,
3810            **opts,
3811        )
3812
3813    def group_by(
3814        self,
3815        *expressions: t.Optional[ExpOrStr],
3816        append: bool = True,
3817        dialect: DialectType = None,
3818        copy: bool = True,
3819        **opts,
3820    ) -> Select:
3821        """
3822        Set the GROUP BY expression.
3823
3824        Example:
3825            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
3826            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
3827
3828        Args:
3829            *expressions: the SQL code strings to parse.
3830                If a `Group` instance is passed, this is used as-is.
3831                If another `Expression` instance is passed, it will be wrapped in a `Group`.
3832                If nothing is passed in then a group by is not applied to the expression
3833            append: if `True`, add to any existing expressions.
3834                Otherwise, this flattens all the `Group` expression into a single expression.
3835            dialect: the dialect used to parse the input expression.
3836            copy: if `False`, modify this expression instance in-place.
3837            opts: other options to use to parse the input expressions.
3838
3839        Returns:
3840            The modified Select expression.
3841        """
3842        if not expressions:
3843            return self if not copy else self.copy()
3844
3845        return _apply_child_list_builder(
3846            *expressions,
3847            instance=self,
3848            arg="group",
3849            append=append,
3850            copy=copy,
3851            prefix="GROUP BY",
3852            into=Group,
3853            dialect=dialect,
3854            **opts,
3855        )
3856
3857    def sort_by(
3858        self,
3859        *expressions: t.Optional[ExpOrStr],
3860        append: bool = True,
3861        dialect: DialectType = None,
3862        copy: bool = True,
3863        **opts,
3864    ) -> Select:
3865        """
3866        Set the SORT BY expression.
3867
3868        Example:
3869            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
3870            'SELECT x FROM tbl SORT BY x DESC'
3871
3872        Args:
3873            *expressions: the SQL code strings to parse.
3874                If a `Group` instance is passed, this is used as-is.
3875                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
3876            append: if `True`, add to any existing expressions.
3877                Otherwise, this flattens all the `Order` expression into a single expression.
3878            dialect: the dialect used to parse the input expression.
3879            copy: if `False`, modify this expression instance in-place.
3880            opts: other options to use to parse the input expressions.
3881
3882        Returns:
3883            The modified Select expression.
3884        """
3885        return _apply_child_list_builder(
3886            *expressions,
3887            instance=self,
3888            arg="sort",
3889            append=append,
3890            copy=copy,
3891            prefix="SORT BY",
3892            into=Sort,
3893            dialect=dialect,
3894            **opts,
3895        )
3896
3897    def cluster_by(
3898        self,
3899        *expressions: t.Optional[ExpOrStr],
3900        append: bool = True,
3901        dialect: DialectType = None,
3902        copy: bool = True,
3903        **opts,
3904    ) -> Select:
3905        """
3906        Set the CLUSTER BY expression.
3907
3908        Example:
3909            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
3910            'SELECT x FROM tbl CLUSTER BY x DESC'
3911
3912        Args:
3913            *expressions: the SQL code strings to parse.
3914                If a `Group` instance is passed, this is used as-is.
3915                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
3916            append: if `True`, add to any existing expressions.
3917                Otherwise, this flattens all the `Order` expression into a single expression.
3918            dialect: the dialect used to parse the input expression.
3919            copy: if `False`, modify this expression instance in-place.
3920            opts: other options to use to parse the input expressions.
3921
3922        Returns:
3923            The modified Select expression.
3924        """
3925        return _apply_child_list_builder(
3926            *expressions,
3927            instance=self,
3928            arg="cluster",
3929            append=append,
3930            copy=copy,
3931            prefix="CLUSTER BY",
3932            into=Cluster,
3933            dialect=dialect,
3934            **opts,
3935        )
3936
3937    def select(
3938        self,
3939        *expressions: t.Optional[ExpOrStr],
3940        append: bool = True,
3941        dialect: DialectType = None,
3942        copy: bool = True,
3943        **opts,
3944    ) -> Select:
3945        return _apply_list_builder(
3946            *expressions,
3947            instance=self,
3948            arg="expressions",
3949            append=append,
3950            dialect=dialect,
3951            into=Expression,
3952            copy=copy,
3953            **opts,
3954        )
3955
3956    def lateral(
3957        self,
3958        *expressions: t.Optional[ExpOrStr],
3959        append: bool = True,
3960        dialect: DialectType = None,
3961        copy: bool = True,
3962        **opts,
3963    ) -> Select:
3964        """
3965        Append to or set the LATERAL expressions.
3966
3967        Example:
3968            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3969            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3970
3971        Args:
3972            *expressions: the SQL code strings to parse.
3973                If an `Expression` instance is passed, it will be used as-is.
3974            append: if `True`, add to any existing expressions.
3975                Otherwise, this resets the expressions.
3976            dialect: the dialect used to parse the input expressions.
3977            copy: if `False`, modify this expression instance in-place.
3978            opts: other options to use to parse the input expressions.
3979
3980        Returns:
3981            The modified Select expression.
3982        """
3983        return _apply_list_builder(
3984            *expressions,
3985            instance=self,
3986            arg="laterals",
3987            append=append,
3988            into=Lateral,
3989            prefix="LATERAL VIEW",
3990            dialect=dialect,
3991            copy=copy,
3992            **opts,
3993        )
3994
3995    def join(
3996        self,
3997        expression: ExpOrStr,
3998        on: t.Optional[ExpOrStr] = None,
3999        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
4000        append: bool = True,
4001        join_type: t.Optional[str] = None,
4002        join_alias: t.Optional[Identifier | str] = None,
4003        dialect: DialectType = None,
4004        copy: bool = True,
4005        **opts,
4006    ) -> Select:
4007        """
4008        Append to or set the JOIN expressions.
4009
4010        Example:
4011            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
4012            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
4013
4014            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
4015            'SELECT 1 FROM a JOIN b USING (x, y, z)'
4016
4017            Use `join_type` to change the type of join:
4018
4019            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
4020            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
4021
4022        Args:
4023            expression: the SQL code string to parse.
4024                If an `Expression` instance is passed, it will be used as-is.
4025            on: optionally specify the join "on" criteria as a SQL string.
4026                If an `Expression` instance is passed, it will be used as-is.
4027            using: optionally specify the join "using" criteria as a SQL string.
4028                If an `Expression` instance is passed, it will be used as-is.
4029            append: if `True`, add to any existing expressions.
4030                Otherwise, this resets the expressions.
4031            join_type: if set, alter the parsed join type.
4032            join_alias: an optional alias for the joined source.
4033            dialect: the dialect used to parse the input expressions.
4034            copy: if `False`, modify this expression instance in-place.
4035            opts: other options to use to parse the input expressions.
4036
4037        Returns:
4038            Select: the modified expression.
4039        """
4040        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
4041
4042        try:
4043            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
4044        except ParseError:
4045            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
4046
4047        join = expression if isinstance(expression, Join) else Join(this=expression)
4048
4049        if isinstance(join.this, Select):
4050            join.this.replace(join.this.subquery())
4051
4052        if join_type:
4053            method: t.Optional[Token]
4054            side: t.Optional[Token]
4055            kind: t.Optional[Token]
4056
4057            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
4058
4059            if method:
4060                join.set("method", method.text)
4061            if side:
4062                join.set("side", side.text)
4063            if kind:
4064                join.set("kind", kind.text)
4065
4066        if on:
4067            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
4068            join.set("on", on)
4069
4070        if using:
4071            join = _apply_list_builder(
4072                *ensure_list(using),
4073                instance=join,
4074                arg="using",
4075                append=append,
4076                copy=copy,
4077                into=Identifier,
4078                **opts,
4079            )
4080
4081        if join_alias:
4082            join.set("this", alias_(join.this, join_alias, table=True))
4083
4084        return _apply_list_builder(
4085            join,
4086            instance=self,
4087            arg="joins",
4088            append=append,
4089            copy=copy,
4090            **opts,
4091        )
4092
4093    def having(
4094        self,
4095        *expressions: t.Optional[ExpOrStr],
4096        append: bool = True,
4097        dialect: DialectType = None,
4098        copy: bool = True,
4099        **opts,
4100    ) -> Select:
4101        """
4102        Append to or set the HAVING expressions.
4103
4104        Example:
4105            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
4106            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
4107
4108        Args:
4109            *expressions: the SQL code strings to parse.
4110                If an `Expression` instance is passed, it will be used as-is.
4111                Multiple expressions are combined with an AND operator.
4112            append: if `True`, AND the new expressions to any existing expression.
4113                Otherwise, this resets the expression.
4114            dialect: the dialect used to parse the input expressions.
4115            copy: if `False`, modify this expression instance in-place.
4116            opts: other options to use to parse the input expressions.
4117
4118        Returns:
4119            The modified Select expression.
4120        """
4121        return _apply_conjunction_builder(
4122            *expressions,
4123            instance=self,
4124            arg="having",
4125            append=append,
4126            into=Having,
4127            dialect=dialect,
4128            copy=copy,
4129            **opts,
4130        )
4131
4132    def window(
4133        self,
4134        *expressions: t.Optional[ExpOrStr],
4135        append: bool = True,
4136        dialect: DialectType = None,
4137        copy: bool = True,
4138        **opts,
4139    ) -> Select:
4140        return _apply_list_builder(
4141            *expressions,
4142            instance=self,
4143            arg="windows",
4144            append=append,
4145            into=Window,
4146            dialect=dialect,
4147            copy=copy,
4148            **opts,
4149        )
4150
4151    def qualify(
4152        self,
4153        *expressions: t.Optional[ExpOrStr],
4154        append: bool = True,
4155        dialect: DialectType = None,
4156        copy: bool = True,
4157        **opts,
4158    ) -> Select:
4159        return _apply_conjunction_builder(
4160            *expressions,
4161            instance=self,
4162            arg="qualify",
4163            append=append,
4164            into=Qualify,
4165            dialect=dialect,
4166            copy=copy,
4167            **opts,
4168        )
4169
4170    def distinct(
4171        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
4172    ) -> Select:
4173        """
4174        Set the OFFSET expression.
4175
4176        Example:
4177            >>> Select().from_("tbl").select("x").distinct().sql()
4178            'SELECT DISTINCT x FROM tbl'
4179
4180        Args:
4181            ons: the expressions to distinct on
4182            distinct: whether the Select should be distinct
4183            copy: if `False`, modify this expression instance in-place.
4184
4185        Returns:
4186            Select: the modified expression.
4187        """
4188        instance = maybe_copy(self, copy)
4189        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
4190        instance.set("distinct", Distinct(on=on) if distinct else None)
4191        return instance
4192
4193    def ctas(
4194        self,
4195        table: ExpOrStr,
4196        properties: t.Optional[t.Dict] = None,
4197        dialect: DialectType = None,
4198        copy: bool = True,
4199        **opts,
4200    ) -> Create:
4201        """
4202        Convert this expression to a CREATE TABLE AS statement.
4203
4204        Example:
4205            >>> Select().select("*").from_("tbl").ctas("x").sql()
4206            'CREATE TABLE x AS SELECT * FROM tbl'
4207
4208        Args:
4209            table: the SQL code string to parse as the table name.
4210                If another `Expression` instance is passed, it will be used as-is.
4211            properties: an optional mapping of table properties
4212            dialect: the dialect used to parse the input table.
4213            copy: if `False`, modify this expression instance in-place.
4214            opts: other options to use to parse the input table.
4215
4216        Returns:
4217            The new Create expression.
4218        """
4219        instance = maybe_copy(self, copy)
4220        table_expression = maybe_parse(table, into=Table, dialect=dialect, **opts)
4221
4222        properties_expression = None
4223        if properties:
4224            properties_expression = Properties.from_dict(properties)
4225
4226        return Create(
4227            this=table_expression,
4228            kind="TABLE",
4229            expression=instance,
4230            properties=properties_expression,
4231        )
4232
4233    def lock(self, update: bool = True, copy: bool = True) -> Select:
4234        """
4235        Set the locking read mode for this expression.
4236
4237        Examples:
4238            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
4239            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
4240
4241            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
4242            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
4243
4244        Args:
4245            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
4246            copy: if `False`, modify this expression instance in-place.
4247
4248        Returns:
4249            The modified expression.
4250        """
4251        inst = maybe_copy(self, copy)
4252        inst.set("locks", [Lock(update=update)])
4253
4254        return inst
4255
4256    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
4257        """
4258        Set hints for this expression.
4259
4260        Examples:
4261            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
4262            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
4263
4264        Args:
4265            hints: The SQL code strings to parse as the hints.
4266                If an `Expression` instance is passed, it will be used as-is.
4267            dialect: The dialect used to parse the hints.
4268            copy: If `False`, modify this expression instance in-place.
4269
4270        Returns:
4271            The modified expression.
4272        """
4273        inst = maybe_copy(self, copy)
4274        inst.set(
4275            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
4276        )
4277
4278        return inst
4279
4280    @property
4281    def named_selects(self) -> t.List[str]:
4282        return [e.output_name for e in self.expressions if e.alias_or_name]
4283
4284    @property
4285    def is_star(self) -> bool:
4286        return any(expression.is_star for expression in self.expressions)
4287
4288    @property
4289    def selects(self) -> t.List[Expression]:
4290        return self.expressions
arg_types = {'with': False, 'kind': False, 'expressions': False, 'hint': False, 'distinct': False, 'into': False, 'from': False, 'operation_modifiers': False, 'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': False, 'prewhere': False, 'where': False, 'group': False, 'having': False, 'qualify': False, 'windows': False, 'distribute': False, 'sort': False, 'cluster': False, 'order': False, 'limit': False, 'offset': False, 'locks': False, 'sample': False, 'settings': False, 'format': False, 'options': False}
def from_( self, expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3781    def from_(
3782        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3783    ) -> Select:
3784        """
3785        Set the FROM expression.
3786
3787        Example:
3788            >>> Select().from_("tbl").select("x").sql()
3789            'SELECT x FROM tbl'
3790
3791        Args:
3792            expression : the SQL code strings to parse.
3793                If a `From` instance is passed, this is used as-is.
3794                If another `Expression` instance is passed, it will be wrapped in a `From`.
3795            dialect: the dialect used to parse the input expression.
3796            copy: if `False`, modify this expression instance in-place.
3797            opts: other options to use to parse the input expressions.
3798
3799        Returns:
3800            The modified Select expression.
3801        """
3802        return _apply_builder(
3803            expression=expression,
3804            instance=self,
3805            arg="from",
3806            into=From,
3807            prefix="FROM",
3808            dialect=dialect,
3809            copy=copy,
3810            **opts,
3811        )

Set the FROM expression.

Example:
>>> Select().from_("tbl").select("x").sql()
'SELECT x FROM tbl'
Arguments:
  • expression : the SQL code strings to parse. If a From instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a From.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def group_by( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3813    def group_by(
3814        self,
3815        *expressions: t.Optional[ExpOrStr],
3816        append: bool = True,
3817        dialect: DialectType = None,
3818        copy: bool = True,
3819        **opts,
3820    ) -> Select:
3821        """
3822        Set the GROUP BY expression.
3823
3824        Example:
3825            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
3826            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
3827
3828        Args:
3829            *expressions: the SQL code strings to parse.
3830                If a `Group` instance is passed, this is used as-is.
3831                If another `Expression` instance is passed, it will be wrapped in a `Group`.
3832                If nothing is passed in then a group by is not applied to the expression
3833            append: if `True`, add to any existing expressions.
3834                Otherwise, this flattens all the `Group` expression into a single expression.
3835            dialect: the dialect used to parse the input expression.
3836            copy: if `False`, modify this expression instance in-place.
3837            opts: other options to use to parse the input expressions.
3838
3839        Returns:
3840            The modified Select expression.
3841        """
3842        if not expressions:
3843            return self if not copy else self.copy()
3844
3845        return _apply_child_list_builder(
3846            *expressions,
3847            instance=self,
3848            arg="group",
3849            append=append,
3850            copy=copy,
3851            prefix="GROUP BY",
3852            into=Group,
3853            dialect=dialect,
3854            **opts,
3855        )

Set the GROUP BY expression.

Example:
>>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
'SELECT x, COUNT(1) FROM tbl GROUP BY x'
Arguments:
  • *expressions: the SQL code strings to parse. If a Group instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a Group. If nothing is passed in then a group by is not applied to the expression
  • append: if True, add to any existing expressions. Otherwise, this flattens all the Group expression into a single expression.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def sort_by( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3857    def sort_by(
3858        self,
3859        *expressions: t.Optional[ExpOrStr],
3860        append: bool = True,
3861        dialect: DialectType = None,
3862        copy: bool = True,
3863        **opts,
3864    ) -> Select:
3865        """
3866        Set the SORT BY expression.
3867
3868        Example:
3869            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
3870            'SELECT x FROM tbl SORT BY x DESC'
3871
3872        Args:
3873            *expressions: the SQL code strings to parse.
3874                If a `Group` instance is passed, this is used as-is.
3875                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
3876            append: if `True`, add to any existing expressions.
3877                Otherwise, this flattens all the `Order` expression into a single expression.
3878            dialect: the dialect used to parse the input expression.
3879            copy: if `False`, modify this expression instance in-place.
3880            opts: other options to use to parse the input expressions.
3881
3882        Returns:
3883            The modified Select expression.
3884        """
3885        return _apply_child_list_builder(
3886            *expressions,
3887            instance=self,
3888            arg="sort",
3889            append=append,
3890            copy=copy,
3891            prefix="SORT BY",
3892            into=Sort,
3893            dialect=dialect,
3894            **opts,
3895        )

Set the SORT BY expression.

Example:
>>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
'SELECT x FROM tbl SORT BY x DESC'
Arguments:
  • *expressions: the SQL code strings to parse. If a Group instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a SORT.
  • append: if True, add to any existing expressions. Otherwise, this flattens all the Order expression into a single expression.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def cluster_by( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3897    def cluster_by(
3898        self,
3899        *expressions: t.Optional[ExpOrStr],
3900        append: bool = True,
3901        dialect: DialectType = None,
3902        copy: bool = True,
3903        **opts,
3904    ) -> Select:
3905        """
3906        Set the CLUSTER BY expression.
3907
3908        Example:
3909            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
3910            'SELECT x FROM tbl CLUSTER BY x DESC'
3911
3912        Args:
3913            *expressions: the SQL code strings to parse.
3914                If a `Group` instance is passed, this is used as-is.
3915                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
3916            append: if `True`, add to any existing expressions.
3917                Otherwise, this flattens all the `Order` expression into a single expression.
3918            dialect: the dialect used to parse the input expression.
3919            copy: if `False`, modify this expression instance in-place.
3920            opts: other options to use to parse the input expressions.
3921
3922        Returns:
3923            The modified Select expression.
3924        """
3925        return _apply_child_list_builder(
3926            *expressions,
3927            instance=self,
3928            arg="cluster",
3929            append=append,
3930            copy=copy,
3931            prefix="CLUSTER BY",
3932            into=Cluster,
3933            dialect=dialect,
3934            **opts,
3935        )

Set the CLUSTER BY expression.

Example:
>>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
'SELECT x FROM tbl CLUSTER BY x DESC'
Arguments:
  • *expressions: the SQL code strings to parse. If a Group instance is passed, this is used as-is. If another Expression instance is passed, it will be wrapped in a Cluster.
  • append: if True, add to any existing expressions. Otherwise, this flattens all the Order expression into a single expression.
  • dialect: the dialect used to parse the input expression.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def select( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3937    def select(
3938        self,
3939        *expressions: t.Optional[ExpOrStr],
3940        append: bool = True,
3941        dialect: DialectType = None,
3942        copy: bool = True,
3943        **opts,
3944    ) -> Select:
3945        return _apply_list_builder(
3946            *expressions,
3947            instance=self,
3948            arg="expressions",
3949            append=append,
3950            dialect=dialect,
3951            into=Expression,
3952            copy=copy,
3953            **opts,
3954        )

Append to or set the SELECT expressions.

Example:
>>> Select().select("x", "y").sql()
'SELECT x, y'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Query expression.

def lateral( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3956    def lateral(
3957        self,
3958        *expressions: t.Optional[ExpOrStr],
3959        append: bool = True,
3960        dialect: DialectType = None,
3961        copy: bool = True,
3962        **opts,
3963    ) -> Select:
3964        """
3965        Append to or set the LATERAL expressions.
3966
3967        Example:
3968            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3969            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3970
3971        Args:
3972            *expressions: the SQL code strings to parse.
3973                If an `Expression` instance is passed, it will be used as-is.
3974            append: if `True`, add to any existing expressions.
3975                Otherwise, this resets the expressions.
3976            dialect: the dialect used to parse the input expressions.
3977            copy: if `False`, modify this expression instance in-place.
3978            opts: other options to use to parse the input expressions.
3979
3980        Returns:
3981            The modified Select expression.
3982        """
3983        return _apply_list_builder(
3984            *expressions,
3985            instance=self,
3986            arg="laterals",
3987            append=append,
3988            into=Lateral,
3989            prefix="LATERAL VIEW",
3990            dialect=dialect,
3991            copy=copy,
3992            **opts,
3993        )

Append to or set the LATERAL expressions.

Example:
>>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def join( self, expression: Union[str, Expression], on: Union[str, Expression, NoneType] = None, using: Union[str, Expression, Collection[Union[str, Expression]], NoneType] = None, append: bool = True, join_type: Optional[str] = None, join_alias: Union[Identifier, str, NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3995    def join(
3996        self,
3997        expression: ExpOrStr,
3998        on: t.Optional[ExpOrStr] = None,
3999        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
4000        append: bool = True,
4001        join_type: t.Optional[str] = None,
4002        join_alias: t.Optional[Identifier | str] = None,
4003        dialect: DialectType = None,
4004        copy: bool = True,
4005        **opts,
4006    ) -> Select:
4007        """
4008        Append to or set the JOIN expressions.
4009
4010        Example:
4011            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
4012            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
4013
4014            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
4015            'SELECT 1 FROM a JOIN b USING (x, y, z)'
4016
4017            Use `join_type` to change the type of join:
4018
4019            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
4020            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
4021
4022        Args:
4023            expression: the SQL code string to parse.
4024                If an `Expression` instance is passed, it will be used as-is.
4025            on: optionally specify the join "on" criteria as a SQL string.
4026                If an `Expression` instance is passed, it will be used as-is.
4027            using: optionally specify the join "using" criteria as a SQL string.
4028                If an `Expression` instance is passed, it will be used as-is.
4029            append: if `True`, add to any existing expressions.
4030                Otherwise, this resets the expressions.
4031            join_type: if set, alter the parsed join type.
4032            join_alias: an optional alias for the joined source.
4033            dialect: the dialect used to parse the input expressions.
4034            copy: if `False`, modify this expression instance in-place.
4035            opts: other options to use to parse the input expressions.
4036
4037        Returns:
4038            Select: the modified expression.
4039        """
4040        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
4041
4042        try:
4043            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
4044        except ParseError:
4045            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
4046
4047        join = expression if isinstance(expression, Join) else Join(this=expression)
4048
4049        if isinstance(join.this, Select):
4050            join.this.replace(join.this.subquery())
4051
4052        if join_type:
4053            method: t.Optional[Token]
4054            side: t.Optional[Token]
4055            kind: t.Optional[Token]
4056
4057            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
4058
4059            if method:
4060                join.set("method", method.text)
4061            if side:
4062                join.set("side", side.text)
4063            if kind:
4064                join.set("kind", kind.text)
4065
4066        if on:
4067            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
4068            join.set("on", on)
4069
4070        if using:
4071            join = _apply_list_builder(
4072                *ensure_list(using),
4073                instance=join,
4074                arg="using",
4075                append=append,
4076                copy=copy,
4077                into=Identifier,
4078                **opts,
4079            )
4080
4081        if join_alias:
4082            join.set("this", alias_(join.this, join_alias, table=True))
4083
4084        return _apply_list_builder(
4085            join,
4086            instance=self,
4087            arg="joins",
4088            append=append,
4089            copy=copy,
4090            **opts,
4091        )

Append to or set the JOIN expressions.

Example:
>>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
>>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
'SELECT 1 FROM a JOIN b USING (x, y, z)'

Use join_type to change the type of join:

>>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
Arguments:
  • expression: the SQL code string to parse. If an Expression instance is passed, it will be used as-is.
  • on: optionally specify the join "on" criteria as a SQL string. If an Expression instance is passed, it will be used as-is.
  • using: optionally specify the join "using" criteria as a SQL string. If an Expression instance is passed, it will be used as-is.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • join_type: if set, alter the parsed join type.
  • join_alias: an optional alias for the joined source.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

Select: the modified expression.

def having( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
4093    def having(
4094        self,
4095        *expressions: t.Optional[ExpOrStr],
4096        append: bool = True,
4097        dialect: DialectType = None,
4098        copy: bool = True,
4099        **opts,
4100    ) -> Select:
4101        """
4102        Append to or set the HAVING expressions.
4103
4104        Example:
4105            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
4106            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
4107
4108        Args:
4109            *expressions: the SQL code strings to parse.
4110                If an `Expression` instance is passed, it will be used as-is.
4111                Multiple expressions are combined with an AND operator.
4112            append: if `True`, AND the new expressions to any existing expression.
4113                Otherwise, this resets the expression.
4114            dialect: the dialect used to parse the input expressions.
4115            copy: if `False`, modify this expression instance in-place.
4116            opts: other options to use to parse the input expressions.
4117
4118        Returns:
4119            The modified Select expression.
4120        """
4121        return _apply_conjunction_builder(
4122            *expressions,
4123            instance=self,
4124            arg="having",
4125            append=append,
4126            into=Having,
4127            dialect=dialect,
4128            copy=copy,
4129            **opts,
4130        )

Append to or set the HAVING expressions.

Example:
>>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is. Multiple expressions are combined with an AND operator.
  • append: if True, AND the new expressions to any existing expression. Otherwise, this resets the expression.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Select expression.

def window( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
4132    def window(
4133        self,
4134        *expressions: t.Optional[ExpOrStr],
4135        append: bool = True,
4136        dialect: DialectType = None,
4137        copy: bool = True,
4138        **opts,
4139    ) -> Select:
4140        return _apply_list_builder(
4141            *expressions,
4142            instance=self,
4143            arg="windows",
4144            append=append,
4145            into=Window,
4146            dialect=dialect,
4147            copy=copy,
4148            **opts,
4149        )
def qualify( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
4151    def qualify(
4152        self,
4153        *expressions: t.Optional[ExpOrStr],
4154        append: bool = True,
4155        dialect: DialectType = None,
4156        copy: bool = True,
4157        **opts,
4158    ) -> Select:
4159        return _apply_conjunction_builder(
4160            *expressions,
4161            instance=self,
4162            arg="qualify",
4163            append=append,
4164            into=Qualify,
4165            dialect=dialect,
4166            copy=copy,
4167            **opts,
4168        )
def distinct( self, *ons: Union[str, Expression, NoneType], distinct: bool = True, copy: bool = True) -> Select:
4170    def distinct(
4171        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
4172    ) -> Select:
4173        """
4174        Set the OFFSET expression.
4175
4176        Example:
4177            >>> Select().from_("tbl").select("x").distinct().sql()
4178            'SELECT DISTINCT x FROM tbl'
4179
4180        Args:
4181            ons: the expressions to distinct on
4182            distinct: whether the Select should be distinct
4183            copy: if `False`, modify this expression instance in-place.
4184
4185        Returns:
4186            Select: the modified expression.
4187        """
4188        instance = maybe_copy(self, copy)
4189        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
4190        instance.set("distinct", Distinct(on=on) if distinct else None)
4191        return instance

Set the OFFSET expression.

Example:
>>> Select().from_("tbl").select("x").distinct().sql()
'SELECT DISTINCT x FROM tbl'
Arguments:
  • ons: the expressions to distinct on
  • distinct: whether the Select should be distinct
  • copy: if False, modify this expression instance in-place.
Returns:

Select: the modified expression.

def ctas( self, table: Union[str, Expression], properties: Optional[Dict] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Create:
4193    def ctas(
4194        self,
4195        table: ExpOrStr,
4196        properties: t.Optional[t.Dict] = None,
4197        dialect: DialectType = None,
4198        copy: bool = True,
4199        **opts,
4200    ) -> Create:
4201        """
4202        Convert this expression to a CREATE TABLE AS statement.
4203
4204        Example:
4205            >>> Select().select("*").from_("tbl").ctas("x").sql()
4206            'CREATE TABLE x AS SELECT * FROM tbl'
4207
4208        Args:
4209            table: the SQL code string to parse as the table name.
4210                If another `Expression` instance is passed, it will be used as-is.
4211            properties: an optional mapping of table properties
4212            dialect: the dialect used to parse the input table.
4213            copy: if `False`, modify this expression instance in-place.
4214            opts: other options to use to parse the input table.
4215
4216        Returns:
4217            The new Create expression.
4218        """
4219        instance = maybe_copy(self, copy)
4220        table_expression = maybe_parse(table, into=Table, dialect=dialect, **opts)
4221
4222        properties_expression = None
4223        if properties:
4224            properties_expression = Properties.from_dict(properties)
4225
4226        return Create(
4227            this=table_expression,
4228            kind="TABLE",
4229            expression=instance,
4230            properties=properties_expression,
4231        )

Convert this expression to a CREATE TABLE AS statement.

Example:
>>> Select().select("*").from_("tbl").ctas("x").sql()
'CREATE TABLE x AS SELECT * FROM tbl'
Arguments:
  • table: the SQL code string to parse as the table name. If another Expression instance is passed, it will be used as-is.
  • properties: an optional mapping of table properties
  • dialect: the dialect used to parse the input table.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input table.
Returns:

The new Create expression.

def lock( self, update: bool = True, copy: bool = True) -> Select:
4233    def lock(self, update: bool = True, copy: bool = True) -> Select:
4234        """
4235        Set the locking read mode for this expression.
4236
4237        Examples:
4238            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
4239            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
4240
4241            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
4242            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
4243
4244        Args:
4245            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
4246            copy: if `False`, modify this expression instance in-place.
4247
4248        Returns:
4249            The modified expression.
4250        """
4251        inst = maybe_copy(self, copy)
4252        inst.set("locks", [Lock(update=update)])
4253
4254        return inst

Set the locking read mode for this expression.

Examples:
>>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
"SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
>>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
"SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
Arguments:
  • update: if True, the locking type will be FOR UPDATE, else it will be FOR SHARE.
  • copy: if False, modify this expression instance in-place.
Returns:

The modified expression.

def hint( self, *hints: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True) -> Select:
4256    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
4257        """
4258        Set hints for this expression.
4259
4260        Examples:
4261            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
4262            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
4263
4264        Args:
4265            hints: The SQL code strings to parse as the hints.
4266                If an `Expression` instance is passed, it will be used as-is.
4267            dialect: The dialect used to parse the hints.
4268            copy: If `False`, modify this expression instance in-place.
4269
4270        Returns:
4271            The modified expression.
4272        """
4273        inst = maybe_copy(self, copy)
4274        inst.set(
4275            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
4276        )
4277
4278        return inst

Set hints for this expression.

Examples:
>>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
'SELECT /*+ BROADCAST(y) */ x FROM tbl'
Arguments:
  • hints: The SQL code strings to parse as the hints. If an Expression instance is passed, it will be used as-is.
  • dialect: The dialect used to parse the hints.
  • copy: If False, modify this expression instance in-place.
Returns:

The modified expression.

named_selects: List[str]
4280    @property
4281    def named_selects(self) -> t.List[str]:
4282        return [e.output_name for e in self.expressions if e.alias_or_name]

Returns the output names of the query's projections.

is_star: bool
4284    @property
4285    def is_star(self) -> bool:
4286        return any(expression.is_star for expression in self.expressions)

Checks whether an expression is a star.

selects: List[Expression]
4288    @property
4289    def selects(self) -> t.List[Expression]:
4290        return self.expressions

Returns the query's projections.

key = 'select'
UNWRAPPED_QUERIES = (<class 'Select'>, <class 'SetOperation'>)
class Subquery(DerivedTable, Query):
4296class Subquery(DerivedTable, Query):
4297    arg_types = {
4298        "this": True,
4299        "alias": False,
4300        "with": False,
4301        **QUERY_MODIFIERS,
4302    }
4303
4304    def unnest(self):
4305        """Returns the first non subquery."""
4306        expression = self
4307        while isinstance(expression, Subquery):
4308            expression = expression.this
4309        return expression
4310
4311    def unwrap(self) -> Subquery:
4312        expression = self
4313        while expression.same_parent and expression.is_wrapper:
4314            expression = t.cast(Subquery, expression.parent)
4315        return expression
4316
4317    def select(
4318        self,
4319        *expressions: t.Optional[ExpOrStr],
4320        append: bool = True,
4321        dialect: DialectType = None,
4322        copy: bool = True,
4323        **opts,
4324    ) -> Subquery:
4325        this = maybe_copy(self, copy)
4326        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4327        return this
4328
4329    @property
4330    def is_wrapper(self) -> bool:
4331        """
4332        Whether this Subquery acts as a simple wrapper around another expression.
4333
4334        SELECT * FROM (((SELECT * FROM t)))
4335                      ^
4336                      This corresponds to a "wrapper" Subquery node
4337        """
4338        return all(v is None for k, v in self.args.items() if k != "this")
4339
4340    @property
4341    def is_star(self) -> bool:
4342        return self.this.is_star
4343
4344    @property
4345    def output_name(self) -> str:
4346        return self.alias
arg_types = {'this': True, 'alias': False, 'with': False, 'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': False, 'prewhere': False, 'where': False, 'group': False, 'having': False, 'qualify': False, 'windows': False, 'distribute': False, 'sort': False, 'cluster': False, 'order': False, 'limit': False, 'offset': False, 'locks': False, 'sample': False, 'settings': False, 'format': False, 'options': False}
def unnest(self):
4304    def unnest(self):
4305        """Returns the first non subquery."""
4306        expression = self
4307        while isinstance(expression, Subquery):
4308            expression = expression.this
4309        return expression

Returns the first non subquery.

def unwrap(self) -> Subquery:
4311    def unwrap(self) -> Subquery:
4312        expression = self
4313        while expression.same_parent and expression.is_wrapper:
4314            expression = t.cast(Subquery, expression.parent)
4315        return expression
def select( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Subquery:
4317    def select(
4318        self,
4319        *expressions: t.Optional[ExpOrStr],
4320        append: bool = True,
4321        dialect: DialectType = None,
4322        copy: bool = True,
4323        **opts,
4324    ) -> Subquery:
4325        this = maybe_copy(self, copy)
4326        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4327        return this

Append to or set the SELECT expressions.

Example:
>>> Select().select("x", "y").sql()
'SELECT x, y'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • append: if True, add to any existing expressions. Otherwise, this resets the expressions.
  • dialect: the dialect used to parse the input expressions.
  • copy: if False, modify this expression instance in-place.
  • opts: other options to use to parse the input expressions.
Returns:

The modified Query expression.

is_wrapper: bool
4329    @property
4330    def is_wrapper(self) -> bool:
4331        """
4332        Whether this Subquery acts as a simple wrapper around another expression.
4333
4334        SELECT * FROM (((SELECT * FROM t)))
4335                      ^
4336                      This corresponds to a "wrapper" Subquery node
4337        """
4338        return all(v is None for k, v in self.args.items() if k != "this")

Whether this Subquery acts as a simple wrapper around another expression.

SELECT * FROM (((SELECT * FROM t))) ^ This corresponds to a "wrapper" Subquery node

is_star: bool
4340    @property
4341    def is_star(self) -> bool:
4342        return self.this.is_star

Checks whether an expression is a star.

output_name: str
4344    @property
4345    def output_name(self) -> str:
4346        return self.alias

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'subquery'
class TableSample(Expression):
4349class TableSample(Expression):
4350    arg_types = {
4351        "expressions": False,
4352        "method": False,
4353        "bucket_numerator": False,
4354        "bucket_denominator": False,
4355        "bucket_field": False,
4356        "percent": False,
4357        "rows": False,
4358        "size": False,
4359        "seed": False,
4360    }
arg_types = {'expressions': False, 'method': False, 'bucket_numerator': False, 'bucket_denominator': False, 'bucket_field': False, 'percent': False, 'rows': False, 'size': False, 'seed': False}
key = 'tablesample'
class Tag(Expression):
4363class Tag(Expression):
4364    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
4365
4366    arg_types = {
4367        "this": False,
4368        "prefix": False,
4369        "postfix": False,
4370    }

Tags are used for generating arbitrary sql like SELECT x.

arg_types = {'this': False, 'prefix': False, 'postfix': False}
key = 'tag'
class Pivot(Expression):
4375class Pivot(Expression):
4376    arg_types = {
4377        "this": False,
4378        "alias": False,
4379        "expressions": False,
4380        "fields": False,
4381        "unpivot": False,
4382        "using": False,
4383        "group": False,
4384        "columns": False,
4385        "include_nulls": False,
4386        "default_on_null": False,
4387        "into": False,
4388    }
4389
4390    @property
4391    def unpivot(self) -> bool:
4392        return bool(self.args.get("unpivot"))
4393
4394    @property
4395    def fields(self) -> t.List[Expression]:
4396        return self.args.get("fields", [])
arg_types = {'this': False, 'alias': False, 'expressions': False, 'fields': False, 'unpivot': False, 'using': False, 'group': False, 'columns': False, 'include_nulls': False, 'default_on_null': False, 'into': False}
unpivot: bool
4390    @property
4391    def unpivot(self) -> bool:
4392        return bool(self.args.get("unpivot"))
fields: List[Expression]
4394    @property
4395    def fields(self) -> t.List[Expression]:
4396        return self.args.get("fields", [])
key = 'pivot'
class UnpivotColumns(Expression):
4401class UnpivotColumns(Expression):
4402    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'unpivotcolumns'
class Window(Condition):
4405class Window(Condition):
4406    arg_types = {
4407        "this": True,
4408        "partition_by": False,
4409        "order": False,
4410        "spec": False,
4411        "alias": False,
4412        "over": False,
4413        "first": False,
4414    }
arg_types = {'this': True, 'partition_by': False, 'order': False, 'spec': False, 'alias': False, 'over': False, 'first': False}
key = 'window'
class WindowSpec(Expression):
4417class WindowSpec(Expression):
4418    arg_types = {
4419        "kind": False,
4420        "start": False,
4421        "start_side": False,
4422        "end": False,
4423        "end_side": False,
4424        "exclude": False,
4425    }
arg_types = {'kind': False, 'start': False, 'start_side': False, 'end': False, 'end_side': False, 'exclude': False}
key = 'windowspec'
class PreWhere(Expression):
4428class PreWhere(Expression):
4429    pass
key = 'prewhere'
class Where(Expression):
4432class Where(Expression):
4433    pass
key = 'where'
class Star(Expression):
4436class Star(Expression):
4437    arg_types = {"except": False, "replace": False, "rename": False}
4438
4439    @property
4440    def name(self) -> str:
4441        return "*"
4442
4443    @property
4444    def output_name(self) -> str:
4445        return self.name
arg_types = {'except': False, 'replace': False, 'rename': False}
name: str
4439    @property
4440    def name(self) -> str:
4441        return "*"
output_name: str
4443    @property
4444    def output_name(self) -> str:
4445        return self.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'star'
class Parameter(Condition):
4448class Parameter(Condition):
4449    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'parameter'
class SessionParameter(Condition):
4452class SessionParameter(Condition):
4453    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'sessionparameter'
class Placeholder(Condition):
4456class Placeholder(Condition):
4457    arg_types = {"this": False, "kind": False}
4458
4459    @property
4460    def name(self) -> str:
4461        return self.this or "?"
arg_types = {'this': False, 'kind': False}
name: str
4459    @property
4460    def name(self) -> str:
4461        return self.this or "?"
key = 'placeholder'
class Null(Condition):
4464class Null(Condition):
4465    arg_types: t.Dict[str, t.Any] = {}
4466
4467    @property
4468    def name(self) -> str:
4469        return "NULL"
4470
4471    def to_py(self) -> Lit[None]:
4472        return None
arg_types: Dict[str, Any] = {}
name: str
4467    @property
4468    def name(self) -> str:
4469        return "NULL"
def to_py(self) -> Literal[None]:
4471    def to_py(self) -> Lit[None]:
4472        return None

Returns a Python object equivalent of the SQL node.

key = 'null'
class Boolean(Condition):
4475class Boolean(Condition):
4476    def to_py(self) -> bool:
4477        return self.this
def to_py(self) -> bool:
4476    def to_py(self) -> bool:
4477        return self.this

Returns a Python object equivalent of the SQL node.

key = 'boolean'
class DataTypeParam(Expression):
4480class DataTypeParam(Expression):
4481    arg_types = {"this": True, "expression": False}
4482
4483    @property
4484    def name(self) -> str:
4485        return self.this.name
arg_types = {'this': True, 'expression': False}
name: str
4483    @property
4484    def name(self) -> str:
4485        return self.this.name
key = 'datatypeparam'
class DataType(Expression):
4490class DataType(Expression):
4491    arg_types = {
4492        "this": True,
4493        "expressions": False,
4494        "nested": False,
4495        "values": False,
4496        "prefix": False,
4497        "kind": False,
4498        "nullable": False,
4499    }
4500
4501    class Type(AutoName):
4502        ARRAY = auto()
4503        AGGREGATEFUNCTION = auto()
4504        SIMPLEAGGREGATEFUNCTION = auto()
4505        BIGDECIMAL = auto()
4506        BIGINT = auto()
4507        BIGSERIAL = auto()
4508        BINARY = auto()
4509        BIT = auto()
4510        BLOB = auto()
4511        BOOLEAN = auto()
4512        BPCHAR = auto()
4513        CHAR = auto()
4514        DATE = auto()
4515        DATE32 = auto()
4516        DATEMULTIRANGE = auto()
4517        DATERANGE = auto()
4518        DATETIME = auto()
4519        DATETIME2 = auto()
4520        DATETIME64 = auto()
4521        DECIMAL = auto()
4522        DECIMAL32 = auto()
4523        DECIMAL64 = auto()
4524        DECIMAL128 = auto()
4525        DECIMAL256 = auto()
4526        DOUBLE = auto()
4527        DYNAMIC = auto()
4528        ENUM = auto()
4529        ENUM8 = auto()
4530        ENUM16 = auto()
4531        FIXEDSTRING = auto()
4532        FLOAT = auto()
4533        GEOGRAPHY = auto()
4534        GEOMETRY = auto()
4535        POINT = auto()
4536        RING = auto()
4537        LINESTRING = auto()
4538        MULTILINESTRING = auto()
4539        POLYGON = auto()
4540        MULTIPOLYGON = auto()
4541        HLLSKETCH = auto()
4542        HSTORE = auto()
4543        IMAGE = auto()
4544        INET = auto()
4545        INT = auto()
4546        INT128 = auto()
4547        INT256 = auto()
4548        INT4MULTIRANGE = auto()
4549        INT4RANGE = auto()
4550        INT8MULTIRANGE = auto()
4551        INT8RANGE = auto()
4552        INTERVAL = auto()
4553        IPADDRESS = auto()
4554        IPPREFIX = auto()
4555        IPV4 = auto()
4556        IPV6 = auto()
4557        JSON = auto()
4558        JSONB = auto()
4559        LIST = auto()
4560        LONGBLOB = auto()
4561        LONGTEXT = auto()
4562        LOWCARDINALITY = auto()
4563        MAP = auto()
4564        MEDIUMBLOB = auto()
4565        MEDIUMINT = auto()
4566        MEDIUMTEXT = auto()
4567        MONEY = auto()
4568        NAME = auto()
4569        NCHAR = auto()
4570        NESTED = auto()
4571        NOTHING = auto()
4572        NULL = auto()
4573        NUMMULTIRANGE = auto()
4574        NUMRANGE = auto()
4575        NVARCHAR = auto()
4576        OBJECT = auto()
4577        RANGE = auto()
4578        ROWVERSION = auto()
4579        SERIAL = auto()
4580        SET = auto()
4581        SMALLDATETIME = auto()
4582        SMALLINT = auto()
4583        SMALLMONEY = auto()
4584        SMALLSERIAL = auto()
4585        STRUCT = auto()
4586        SUPER = auto()
4587        TEXT = auto()
4588        TINYBLOB = auto()
4589        TINYTEXT = auto()
4590        TIME = auto()
4591        TIMETZ = auto()
4592        TIMESTAMP = auto()
4593        TIMESTAMPNTZ = auto()
4594        TIMESTAMPLTZ = auto()
4595        TIMESTAMPTZ = auto()
4596        TIMESTAMP_S = auto()
4597        TIMESTAMP_MS = auto()
4598        TIMESTAMP_NS = auto()
4599        TINYINT = auto()
4600        TSMULTIRANGE = auto()
4601        TSRANGE = auto()
4602        TSTZMULTIRANGE = auto()
4603        TSTZRANGE = auto()
4604        UBIGINT = auto()
4605        UINT = auto()
4606        UINT128 = auto()
4607        UINT256 = auto()
4608        UMEDIUMINT = auto()
4609        UDECIMAL = auto()
4610        UDOUBLE = auto()
4611        UNION = auto()
4612        UNKNOWN = auto()  # Sentinel value, useful for type annotation
4613        USERDEFINED = "USER-DEFINED"
4614        USMALLINT = auto()
4615        UTINYINT = auto()
4616        UUID = auto()
4617        VARBINARY = auto()
4618        VARCHAR = auto()
4619        VARIANT = auto()
4620        VECTOR = auto()
4621        XML = auto()
4622        YEAR = auto()
4623        TDIGEST = auto()
4624
4625    STRUCT_TYPES = {
4626        Type.NESTED,
4627        Type.OBJECT,
4628        Type.STRUCT,
4629        Type.UNION,
4630    }
4631
4632    ARRAY_TYPES = {
4633        Type.ARRAY,
4634        Type.LIST,
4635    }
4636
4637    NESTED_TYPES = {
4638        *STRUCT_TYPES,
4639        *ARRAY_TYPES,
4640        Type.MAP,
4641    }
4642
4643    TEXT_TYPES = {
4644        Type.CHAR,
4645        Type.NCHAR,
4646        Type.NVARCHAR,
4647        Type.TEXT,
4648        Type.VARCHAR,
4649        Type.NAME,
4650    }
4651
4652    SIGNED_INTEGER_TYPES = {
4653        Type.BIGINT,
4654        Type.INT,
4655        Type.INT128,
4656        Type.INT256,
4657        Type.MEDIUMINT,
4658        Type.SMALLINT,
4659        Type.TINYINT,
4660    }
4661
4662    UNSIGNED_INTEGER_TYPES = {
4663        Type.UBIGINT,
4664        Type.UINT,
4665        Type.UINT128,
4666        Type.UINT256,
4667        Type.UMEDIUMINT,
4668        Type.USMALLINT,
4669        Type.UTINYINT,
4670    }
4671
4672    INTEGER_TYPES = {
4673        *SIGNED_INTEGER_TYPES,
4674        *UNSIGNED_INTEGER_TYPES,
4675        Type.BIT,
4676    }
4677
4678    FLOAT_TYPES = {
4679        Type.DOUBLE,
4680        Type.FLOAT,
4681    }
4682
4683    REAL_TYPES = {
4684        *FLOAT_TYPES,
4685        Type.BIGDECIMAL,
4686        Type.DECIMAL,
4687        Type.DECIMAL32,
4688        Type.DECIMAL64,
4689        Type.DECIMAL128,
4690        Type.DECIMAL256,
4691        Type.MONEY,
4692        Type.SMALLMONEY,
4693        Type.UDECIMAL,
4694        Type.UDOUBLE,
4695    }
4696
4697    NUMERIC_TYPES = {
4698        *INTEGER_TYPES,
4699        *REAL_TYPES,
4700    }
4701
4702    TEMPORAL_TYPES = {
4703        Type.DATE,
4704        Type.DATE32,
4705        Type.DATETIME,
4706        Type.DATETIME2,
4707        Type.DATETIME64,
4708        Type.SMALLDATETIME,
4709        Type.TIME,
4710        Type.TIMESTAMP,
4711        Type.TIMESTAMPNTZ,
4712        Type.TIMESTAMPLTZ,
4713        Type.TIMESTAMPTZ,
4714        Type.TIMESTAMP_MS,
4715        Type.TIMESTAMP_NS,
4716        Type.TIMESTAMP_S,
4717        Type.TIMETZ,
4718    }
4719
4720    @classmethod
4721    def build(
4722        cls,
4723        dtype: DATA_TYPE,
4724        dialect: DialectType = None,
4725        udt: bool = False,
4726        copy: bool = True,
4727        **kwargs,
4728    ) -> DataType:
4729        """
4730        Constructs a DataType object.
4731
4732        Args:
4733            dtype: the data type of interest.
4734            dialect: the dialect to use for parsing `dtype`, in case it's a string.
4735            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
4736                DataType, thus creating a user-defined type.
4737            copy: whether to copy the data type.
4738            kwargs: additional arguments to pass in the constructor of DataType.
4739
4740        Returns:
4741            The constructed DataType object.
4742        """
4743        from sqlglot import parse_one
4744
4745        if isinstance(dtype, str):
4746            if dtype.upper() == "UNKNOWN":
4747                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
4748
4749            try:
4750                data_type_exp = parse_one(
4751                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
4752                )
4753            except ParseError:
4754                if udt:
4755                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4756                raise
4757        elif isinstance(dtype, (Identifier, Dot)) and udt:
4758            return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4759        elif isinstance(dtype, DataType.Type):
4760            data_type_exp = DataType(this=dtype)
4761        elif isinstance(dtype, DataType):
4762            return maybe_copy(dtype, copy)
4763        else:
4764            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
4765
4766        return DataType(**{**data_type_exp.args, **kwargs})
4767
4768    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
4769        """
4770        Checks whether this DataType matches one of the provided data types. Nested types or precision
4771        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
4772
4773        Args:
4774            dtypes: the data types to compare this DataType to.
4775            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
4776                If false, it means that NULLABLE<INT> is equivalent to INT.
4777
4778        Returns:
4779            True, if and only if there is a type in `dtypes` which is equal to this DataType.
4780        """
4781        self_is_nullable = self.args.get("nullable")
4782        for dtype in dtypes:
4783            other_type = DataType.build(dtype, copy=False, udt=True)
4784            other_is_nullable = other_type.args.get("nullable")
4785            if (
4786                other_type.expressions
4787                or (check_nullable and (self_is_nullable or other_is_nullable))
4788                or self.this == DataType.Type.USERDEFINED
4789                or other_type.this == DataType.Type.USERDEFINED
4790            ):
4791                matches = self == other_type
4792            else:
4793                matches = self.this == other_type.this
4794
4795            if matches:
4796                return True
4797        return False
arg_types = {'this': True, 'expressions': False, 'nested': False, 'values': False, 'prefix': False, 'kind': False, 'nullable': False}
STRUCT_TYPES = {<Type.UNION: 'UNION'>, <Type.STRUCT: 'STRUCT'>, <Type.NESTED: 'NESTED'>, <Type.OBJECT: 'OBJECT'>}
ARRAY_TYPES = {<Type.LIST: 'LIST'>, <Type.ARRAY: 'ARRAY'>}
NESTED_TYPES = {<Type.LIST: 'LIST'>, <Type.NESTED: 'NESTED'>, <Type.UNION: 'UNION'>, <Type.OBJECT: 'OBJECT'>, <Type.MAP: 'MAP'>, <Type.STRUCT: 'STRUCT'>, <Type.ARRAY: 'ARRAY'>}
TEXT_TYPES = {<Type.VARCHAR: 'VARCHAR'>, <Type.NAME: 'NAME'>, <Type.NCHAR: 'NCHAR'>, <Type.TEXT: 'TEXT'>, <Type.NVARCHAR: 'NVARCHAR'>, <Type.CHAR: 'CHAR'>}
SIGNED_INTEGER_TYPES = {<Type.SMALLINT: 'SMALLINT'>, <Type.TINYINT: 'TINYINT'>, <Type.INT128: 'INT128'>, <Type.MEDIUMINT: 'MEDIUMINT'>, <Type.INT: 'INT'>, <Type.BIGINT: 'BIGINT'>, <Type.INT256: 'INT256'>}
UNSIGNED_INTEGER_TYPES = {<Type.UBIGINT: 'UBIGINT'>, <Type.UINT256: 'UINT256'>, <Type.UTINYINT: 'UTINYINT'>, <Type.UMEDIUMINT: 'UMEDIUMINT'>, <Type.UINT: 'UINT'>, <Type.USMALLINT: 'USMALLINT'>, <Type.UINT128: 'UINT128'>}
INTEGER_TYPES = {<Type.UBIGINT: 'UBIGINT'>, <Type.BIT: 'BIT'>, <Type.UINT256: 'UINT256'>, <Type.SMALLINT: 'SMALLINT'>, <Type.TINYINT: 'TINYINT'>, <Type.UTINYINT: 'UTINYINT'>, <Type.INT128: 'INT128'>, <Type.UMEDIUMINT: 'UMEDIUMINT'>, <Type.MEDIUMINT: 'MEDIUMINT'>, <Type.INT: 'INT'>, <Type.UINT: 'UINT'>, <Type.BIGINT: 'BIGINT'>, <Type.USMALLINT: 'USMALLINT'>, <Type.INT256: 'INT256'>, <Type.UINT128: 'UINT128'>}
FLOAT_TYPES = {<Type.DOUBLE: 'DOUBLE'>, <Type.FLOAT: 'FLOAT'>}
REAL_TYPES = {<Type.BIGDECIMAL: 'BIGDECIMAL'>, <Type.SMALLMONEY: 'SMALLMONEY'>, <Type.FLOAT: 'FLOAT'>, <Type.DECIMAL128: 'DECIMAL128'>, <Type.UDECIMAL: 'UDECIMAL'>, <Type.UDOUBLE: 'UDOUBLE'>, <Type.MONEY: 'MONEY'>, <Type.DECIMAL256: 'DECIMAL256'>, <Type.DECIMAL32: 'DECIMAL32'>, <Type.DECIMAL64: 'DECIMAL64'>, <Type.DECIMAL: 'DECIMAL'>, <Type.DOUBLE: 'DOUBLE'>}
NUMERIC_TYPES = {<Type.SMALLMONEY: 'SMALLMONEY'>, <Type.UBIGINT: 'UBIGINT'>, <Type.BIT: 'BIT'>, <Type.UTINYINT: 'UTINYINT'>, <Type.DOUBLE: 'DOUBLE'>, <Type.UMEDIUMINT: 'UMEDIUMINT'>, <Type.DECIMAL256: 'DECIMAL256'>, <Type.MEDIUMINT: 'MEDIUMINT'>, <Type.DECIMAL: 'DECIMAL'>, <Type.USMALLINT: 'USMALLINT'>, <Type.UINT128: 'UINT128'>, <Type.BIGDECIMAL: 'BIGDECIMAL'>, <Type.UDECIMAL: 'UDECIMAL'>, <Type.FLOAT: 'FLOAT'>, <Type.DECIMAL128: 'DECIMAL128'>, <Type.UINT256: 'UINT256'>, <Type.UDOUBLE: 'UDOUBLE'>, <Type.SMALLINT: 'SMALLINT'>, <Type.TINYINT: 'TINYINT'>, <Type.INT128: 'INT128'>, <Type.MONEY: 'MONEY'>, <Type.DECIMAL32: 'DECIMAL32'>, <Type.DECIMAL64: 'DECIMAL64'>, <Type.INT: 'INT'>, <Type.UINT: 'UINT'>, <Type.BIGINT: 'BIGINT'>, <Type.INT256: 'INT256'>}
TEMPORAL_TYPES = {<Type.DATETIME64: 'DATETIME64'>, <Type.TIMESTAMP: 'TIMESTAMP'>, <Type.TIMESTAMP_S: 'TIMESTAMP_S'>, <Type.DATE: 'DATE'>, <Type.TIMETZ: 'TIMETZ'>, <Type.TIME: 'TIME'>, <Type.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <Type.DATE32: 'DATE32'>, <Type.DATETIME: 'DATETIME'>, <Type.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <Type.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, <Type.SMALLDATETIME: 'SMALLDATETIME'>, <Type.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <Type.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <Type.DATETIME2: 'DATETIME2'>}
@classmethod
def build( cls, dtype: Union[str, Identifier, Dot, DataType, DataType.Type], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, udt: bool = False, copy: bool = True, **kwargs) -> DataType:
4720    @classmethod
4721    def build(
4722        cls,
4723        dtype: DATA_TYPE,
4724        dialect: DialectType = None,
4725        udt: bool = False,
4726        copy: bool = True,
4727        **kwargs,
4728    ) -> DataType:
4729        """
4730        Constructs a DataType object.
4731
4732        Args:
4733            dtype: the data type of interest.
4734            dialect: the dialect to use for parsing `dtype`, in case it's a string.
4735            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
4736                DataType, thus creating a user-defined type.
4737            copy: whether to copy the data type.
4738            kwargs: additional arguments to pass in the constructor of DataType.
4739
4740        Returns:
4741            The constructed DataType object.
4742        """
4743        from sqlglot import parse_one
4744
4745        if isinstance(dtype, str):
4746            if dtype.upper() == "UNKNOWN":
4747                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
4748
4749            try:
4750                data_type_exp = parse_one(
4751                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
4752                )
4753            except ParseError:
4754                if udt:
4755                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4756                raise
4757        elif isinstance(dtype, (Identifier, Dot)) and udt:
4758            return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4759        elif isinstance(dtype, DataType.Type):
4760            data_type_exp = DataType(this=dtype)
4761        elif isinstance(dtype, DataType):
4762            return maybe_copy(dtype, copy)
4763        else:
4764            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
4765
4766        return DataType(**{**data_type_exp.args, **kwargs})

Constructs a DataType object.

Arguments:
  • dtype: the data type of interest.
  • dialect: the dialect to use for parsing dtype, in case it's a string.
  • udt: when set to True, dtype will be used as-is if it can't be parsed into a DataType, thus creating a user-defined type.
  • copy: whether to copy the data type.
  • kwargs: additional arguments to pass in the constructor of DataType.
Returns:

The constructed DataType object.

def is_type( self, *dtypes: Union[str, Identifier, Dot, DataType, DataType.Type], check_nullable: bool = False) -> bool:
4768    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
4769        """
4770        Checks whether this DataType matches one of the provided data types. Nested types or precision
4771        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
4772
4773        Args:
4774            dtypes: the data types to compare this DataType to.
4775            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
4776                If false, it means that NULLABLE<INT> is equivalent to INT.
4777
4778        Returns:
4779            True, if and only if there is a type in `dtypes` which is equal to this DataType.
4780        """
4781        self_is_nullable = self.args.get("nullable")
4782        for dtype in dtypes:
4783            other_type = DataType.build(dtype, copy=False, udt=True)
4784            other_is_nullable = other_type.args.get("nullable")
4785            if (
4786                other_type.expressions
4787                or (check_nullable and (self_is_nullable or other_is_nullable))
4788                or self.this == DataType.Type.USERDEFINED
4789                or other_type.this == DataType.Type.USERDEFINED
4790            ):
4791                matches = self == other_type
4792            else:
4793                matches = self.this == other_type.this
4794
4795            if matches:
4796                return True
4797        return False

Checks whether this DataType matches one of the provided data types. Nested types or precision will be compared using "structural equivalence" semantics, so e.g. array != array.

Arguments:
  • dtypes: the data types to compare this DataType to.
  • check_nullable: whether to take the NULLABLE type constructor into account for the comparison. If false, it means that NULLABLE is equivalent to INT.
Returns:

True, if and only if there is a type in dtypes which is equal to this DataType.

key = 'datatype'
class DataType.Type(sqlglot.helper.AutoName):
4501    class Type(AutoName):
4502        ARRAY = auto()
4503        AGGREGATEFUNCTION = auto()
4504        SIMPLEAGGREGATEFUNCTION = auto()
4505        BIGDECIMAL = auto()
4506        BIGINT = auto()
4507        BIGSERIAL = auto()
4508        BINARY = auto()
4509        BIT = auto()
4510        BLOB = auto()
4511        BOOLEAN = auto()
4512        BPCHAR = auto()
4513        CHAR = auto()
4514        DATE = auto()
4515        DATE32 = auto()
4516        DATEMULTIRANGE = auto()
4517        DATERANGE = auto()
4518        DATETIME = auto()
4519        DATETIME2 = auto()
4520        DATETIME64 = auto()
4521        DECIMAL = auto()
4522        DECIMAL32 = auto()
4523        DECIMAL64 = auto()
4524        DECIMAL128 = auto()
4525        DECIMAL256 = auto()
4526        DOUBLE = auto()
4527        DYNAMIC = auto()
4528        ENUM = auto()
4529        ENUM8 = auto()
4530        ENUM16 = auto()
4531        FIXEDSTRING = auto()
4532        FLOAT = auto()
4533        GEOGRAPHY = auto()
4534        GEOMETRY = auto()
4535        POINT = auto()
4536        RING = auto()
4537        LINESTRING = auto()
4538        MULTILINESTRING = auto()
4539        POLYGON = auto()
4540        MULTIPOLYGON = auto()
4541        HLLSKETCH = auto()
4542        HSTORE = auto()
4543        IMAGE = auto()
4544        INET = auto()
4545        INT = auto()
4546        INT128 = auto()
4547        INT256 = auto()
4548        INT4MULTIRANGE = auto()
4549        INT4RANGE = auto()
4550        INT8MULTIRANGE = auto()
4551        INT8RANGE = auto()
4552        INTERVAL = auto()
4553        IPADDRESS = auto()
4554        IPPREFIX = auto()
4555        IPV4 = auto()
4556        IPV6 = auto()
4557        JSON = auto()
4558        JSONB = auto()
4559        LIST = auto()
4560        LONGBLOB = auto()
4561        LONGTEXT = auto()
4562        LOWCARDINALITY = auto()
4563        MAP = auto()
4564        MEDIUMBLOB = auto()
4565        MEDIUMINT = auto()
4566        MEDIUMTEXT = auto()
4567        MONEY = auto()
4568        NAME = auto()
4569        NCHAR = auto()
4570        NESTED = auto()
4571        NOTHING = auto()
4572        NULL = auto()
4573        NUMMULTIRANGE = auto()
4574        NUMRANGE = auto()
4575        NVARCHAR = auto()
4576        OBJECT = auto()
4577        RANGE = auto()
4578        ROWVERSION = auto()
4579        SERIAL = auto()
4580        SET = auto()
4581        SMALLDATETIME = auto()
4582        SMALLINT = auto()
4583        SMALLMONEY = auto()
4584        SMALLSERIAL = auto()
4585        STRUCT = auto()
4586        SUPER = auto()
4587        TEXT = auto()
4588        TINYBLOB = auto()
4589        TINYTEXT = auto()
4590        TIME = auto()
4591        TIMETZ = auto()
4592        TIMESTAMP = auto()
4593        TIMESTAMPNTZ = auto()
4594        TIMESTAMPLTZ = auto()
4595        TIMESTAMPTZ = auto()
4596        TIMESTAMP_S = auto()
4597        TIMESTAMP_MS = auto()
4598        TIMESTAMP_NS = auto()
4599        TINYINT = auto()
4600        TSMULTIRANGE = auto()
4601        TSRANGE = auto()
4602        TSTZMULTIRANGE = auto()
4603        TSTZRANGE = auto()
4604        UBIGINT = auto()
4605        UINT = auto()
4606        UINT128 = auto()
4607        UINT256 = auto()
4608        UMEDIUMINT = auto()
4609        UDECIMAL = auto()
4610        UDOUBLE = auto()
4611        UNION = auto()
4612        UNKNOWN = auto()  # Sentinel value, useful for type annotation
4613        USERDEFINED = "USER-DEFINED"
4614        USMALLINT = auto()
4615        UTINYINT = auto()
4616        UUID = auto()
4617        VARBINARY = auto()
4618        VARCHAR = auto()
4619        VARIANT = auto()
4620        VECTOR = auto()
4621        XML = auto()
4622        YEAR = auto()
4623        TDIGEST = auto()

An enumeration.

ARRAY = <Type.ARRAY: 'ARRAY'>
AGGREGATEFUNCTION = <Type.AGGREGATEFUNCTION: 'AGGREGATEFUNCTION'>
SIMPLEAGGREGATEFUNCTION = <Type.SIMPLEAGGREGATEFUNCTION: 'SIMPLEAGGREGATEFUNCTION'>
BIGDECIMAL = <Type.BIGDECIMAL: 'BIGDECIMAL'>
BIGINT = <Type.BIGINT: 'BIGINT'>
BIGSERIAL = <Type.BIGSERIAL: 'BIGSERIAL'>
BINARY = <Type.BINARY: 'BINARY'>
BIT = <Type.BIT: 'BIT'>
BLOB = <Type.BLOB: 'BLOB'>
BOOLEAN = <Type.BOOLEAN: 'BOOLEAN'>
BPCHAR = <Type.BPCHAR: 'BPCHAR'>
CHAR = <Type.CHAR: 'CHAR'>
DATE = <Type.DATE: 'DATE'>
DATE32 = <Type.DATE32: 'DATE32'>
DATEMULTIRANGE = <Type.DATEMULTIRANGE: 'DATEMULTIRANGE'>
DATERANGE = <Type.DATERANGE: 'DATERANGE'>
DATETIME = <Type.DATETIME: 'DATETIME'>
DATETIME2 = <Type.DATETIME2: 'DATETIME2'>
DATETIME64 = <Type.DATETIME64: 'DATETIME64'>
DECIMAL = <Type.DECIMAL: 'DECIMAL'>
DECIMAL32 = <Type.DECIMAL32: 'DECIMAL32'>
DECIMAL64 = <Type.DECIMAL64: 'DECIMAL64'>
DECIMAL128 = <Type.DECIMAL128: 'DECIMAL128'>
DECIMAL256 = <Type.DECIMAL256: 'DECIMAL256'>
DOUBLE = <Type.DOUBLE: 'DOUBLE'>
DYNAMIC = <Type.DYNAMIC: 'DYNAMIC'>
ENUM = <Type.ENUM: 'ENUM'>
ENUM8 = <Type.ENUM8: 'ENUM8'>
ENUM16 = <Type.ENUM16: 'ENUM16'>
FIXEDSTRING = <Type.FIXEDSTRING: 'FIXEDSTRING'>
FLOAT = <Type.FLOAT: 'FLOAT'>
GEOGRAPHY = <Type.GEOGRAPHY: 'GEOGRAPHY'>
GEOMETRY = <Type.GEOMETRY: 'GEOMETRY'>
POINT = <Type.POINT: 'POINT'>
RING = <Type.RING: 'RING'>
LINESTRING = <Type.LINESTRING: 'LINESTRING'>
MULTILINESTRING = <Type.MULTILINESTRING: 'MULTILINESTRING'>
POLYGON = <Type.POLYGON: 'POLYGON'>
MULTIPOLYGON = <Type.MULTIPOLYGON: 'MULTIPOLYGON'>
HLLSKETCH = <Type.HLLSKETCH: 'HLLSKETCH'>
HSTORE = <Type.HSTORE: 'HSTORE'>
IMAGE = <Type.IMAGE: 'IMAGE'>
INET = <Type.INET: 'INET'>
INT = <Type.INT: 'INT'>
INT128 = <Type.INT128: 'INT128'>
INT256 = <Type.INT256: 'INT256'>
INT4MULTIRANGE = <Type.INT4MULTIRANGE: 'INT4MULTIRANGE'>
INT4RANGE = <Type.INT4RANGE: 'INT4RANGE'>
INT8MULTIRANGE = <Type.INT8MULTIRANGE: 'INT8MULTIRANGE'>
INT8RANGE = <Type.INT8RANGE: 'INT8RANGE'>
INTERVAL = <Type.INTERVAL: 'INTERVAL'>
IPADDRESS = <Type.IPADDRESS: 'IPADDRESS'>
IPPREFIX = <Type.IPPREFIX: 'IPPREFIX'>
IPV4 = <Type.IPV4: 'IPV4'>
IPV6 = <Type.IPV6: 'IPV6'>
JSON = <Type.JSON: 'JSON'>
JSONB = <Type.JSONB: 'JSONB'>
LIST = <Type.LIST: 'LIST'>
LONGBLOB = <Type.LONGBLOB: 'LONGBLOB'>
LONGTEXT = <Type.LONGTEXT: 'LONGTEXT'>
LOWCARDINALITY = <Type.LOWCARDINALITY: 'LOWCARDINALITY'>
MAP = <Type.MAP: 'MAP'>
MEDIUMBLOB = <Type.MEDIUMBLOB: 'MEDIUMBLOB'>
MEDIUMINT = <Type.MEDIUMINT: 'MEDIUMINT'>
MEDIUMTEXT = <Type.MEDIUMTEXT: 'MEDIUMTEXT'>
MONEY = <Type.MONEY: 'MONEY'>
NAME = <Type.NAME: 'NAME'>
NCHAR = <Type.NCHAR: 'NCHAR'>
NESTED = <Type.NESTED: 'NESTED'>
NOTHING = <Type.NOTHING: 'NOTHING'>
NULL = <Type.NULL: 'NULL'>
NUMMULTIRANGE = <Type.NUMMULTIRANGE: 'NUMMULTIRANGE'>
NUMRANGE = <Type.NUMRANGE: 'NUMRANGE'>
NVARCHAR = <Type.NVARCHAR: 'NVARCHAR'>
OBJECT = <Type.OBJECT: 'OBJECT'>
RANGE = <Type.RANGE: 'RANGE'>
ROWVERSION = <Type.ROWVERSION: 'ROWVERSION'>
SERIAL = <Type.SERIAL: 'SERIAL'>
SET = <Type.SET: 'SET'>
SMALLDATETIME = <Type.SMALLDATETIME: 'SMALLDATETIME'>
SMALLINT = <Type.SMALLINT: 'SMALLINT'>
SMALLMONEY = <Type.SMALLMONEY: 'SMALLMONEY'>
SMALLSERIAL = <Type.SMALLSERIAL: 'SMALLSERIAL'>
STRUCT = <Type.STRUCT: 'STRUCT'>
SUPER = <Type.SUPER: 'SUPER'>
TEXT = <Type.TEXT: 'TEXT'>
TINYBLOB = <Type.TINYBLOB: 'TINYBLOB'>
TINYTEXT = <Type.TINYTEXT: 'TINYTEXT'>
TIME = <Type.TIME: 'TIME'>
TIMETZ = <Type.TIMETZ: 'TIMETZ'>
TIMESTAMP = <Type.TIMESTAMP: 'TIMESTAMP'>
TIMESTAMPNTZ = <Type.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>
TIMESTAMPLTZ = <Type.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>
TIMESTAMPTZ = <Type.TIMESTAMPTZ: 'TIMESTAMPTZ'>
TIMESTAMP_S = <Type.TIMESTAMP_S: 'TIMESTAMP_S'>
TIMESTAMP_MS = <Type.TIMESTAMP_MS: 'TIMESTAMP_MS'>
TIMESTAMP_NS = <Type.TIMESTAMP_NS: 'TIMESTAMP_NS'>
TINYINT = <Type.TINYINT: 'TINYINT'>
TSMULTIRANGE = <Type.TSMULTIRANGE: 'TSMULTIRANGE'>
TSRANGE = <Type.TSRANGE: 'TSRANGE'>
TSTZMULTIRANGE = <Type.TSTZMULTIRANGE: 'TSTZMULTIRANGE'>
TSTZRANGE = <Type.TSTZRANGE: 'TSTZRANGE'>
UBIGINT = <Type.UBIGINT: 'UBIGINT'>
UINT = <Type.UINT: 'UINT'>
UINT128 = <Type.UINT128: 'UINT128'>
UINT256 = <Type.UINT256: 'UINT256'>
UMEDIUMINT = <Type.UMEDIUMINT: 'UMEDIUMINT'>
UDECIMAL = <Type.UDECIMAL: 'UDECIMAL'>
UDOUBLE = <Type.UDOUBLE: 'UDOUBLE'>
UNION = <Type.UNION: 'UNION'>
UNKNOWN = <Type.UNKNOWN: 'UNKNOWN'>
USERDEFINED = <Type.USERDEFINED: 'USER-DEFINED'>
USMALLINT = <Type.USMALLINT: 'USMALLINT'>
UTINYINT = <Type.UTINYINT: 'UTINYINT'>
UUID = <Type.UUID: 'UUID'>
VARBINARY = <Type.VARBINARY: 'VARBINARY'>
VARCHAR = <Type.VARCHAR: 'VARCHAR'>
VARIANT = <Type.VARIANT: 'VARIANT'>
VECTOR = <Type.VECTOR: 'VECTOR'>
XML = <Type.XML: 'XML'>
YEAR = <Type.YEAR: 'YEAR'>
TDIGEST = <Type.TDIGEST: 'TDIGEST'>
class PseudoType(DataType):
4801class PseudoType(DataType):
4802    arg_types = {"this": True}
arg_types = {'this': True}
key = 'pseudotype'
class ObjectIdentifier(DataType):
4806class ObjectIdentifier(DataType):
4807    arg_types = {"this": True}
arg_types = {'this': True}
key = 'objectidentifier'
class SubqueryPredicate(Predicate):
4811class SubqueryPredicate(Predicate):
4812    pass
key = 'subquerypredicate'
class All(SubqueryPredicate):
4815class All(SubqueryPredicate):
4816    pass
key = 'all'
class Any(SubqueryPredicate):
4819class Any(SubqueryPredicate):
4820    pass
key = 'any'
class Command(Expression):
4825class Command(Expression):
4826    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'command'
class Transaction(Expression):
4829class Transaction(Expression):
4830    arg_types = {"this": False, "modes": False, "mark": False}
arg_types = {'this': False, 'modes': False, 'mark': False}
key = 'transaction'
class Commit(Expression):
4833class Commit(Expression):
4834    arg_types = {"chain": False, "this": False, "durability": False}
arg_types = {'chain': False, 'this': False, 'durability': False}
key = 'commit'
class Rollback(Expression):
4837class Rollback(Expression):
4838    arg_types = {"savepoint": False, "this": False}
arg_types = {'savepoint': False, 'this': False}
key = 'rollback'
class Alter(Expression):
4841class Alter(Expression):
4842    arg_types = {
4843        "this": True,
4844        "kind": True,
4845        "actions": True,
4846        "exists": False,
4847        "only": False,
4848        "options": False,
4849        "cluster": False,
4850        "not_valid": False,
4851    }
4852
4853    @property
4854    def kind(self) -> t.Optional[str]:
4855        kind = self.args.get("kind")
4856        return kind and kind.upper()
4857
4858    @property
4859    def actions(self) -> t.List[Expression]:
4860        return self.args.get("actions") or []
arg_types = {'this': True, 'kind': True, 'actions': True, 'exists': False, 'only': False, 'options': False, 'cluster': False, 'not_valid': False}
kind: Optional[str]
4853    @property
4854    def kind(self) -> t.Optional[str]:
4855        kind = self.args.get("kind")
4856        return kind and kind.upper()
actions: List[Expression]
4858    @property
4859    def actions(self) -> t.List[Expression]:
4860        return self.args.get("actions") or []
key = 'alter'
class Analyze(Expression):
4863class Analyze(Expression):
4864    arg_types = {
4865        "kind": False,
4866        "this": False,
4867        "options": False,
4868        "mode": False,
4869        "partition": False,
4870        "expression": False,
4871        "properties": False,
4872    }
arg_types = {'kind': False, 'this': False, 'options': False, 'mode': False, 'partition': False, 'expression': False, 'properties': False}
key = 'analyze'
class AnalyzeStatistics(Expression):
4875class AnalyzeStatistics(Expression):
4876    arg_types = {
4877        "kind": True,
4878        "option": False,
4879        "this": False,
4880        "expressions": False,
4881    }
arg_types = {'kind': True, 'option': False, 'this': False, 'expressions': False}
key = 'analyzestatistics'
class AnalyzeHistogram(Expression):
4884class AnalyzeHistogram(Expression):
4885    arg_types = {
4886        "this": True,
4887        "expressions": True,
4888        "expression": False,
4889        "update_options": False,
4890    }
arg_types = {'this': True, 'expressions': True, 'expression': False, 'update_options': False}
key = 'analyzehistogram'
class AnalyzeSample(Expression):
4893class AnalyzeSample(Expression):
4894    arg_types = {"kind": True, "sample": True}
arg_types = {'kind': True, 'sample': True}
key = 'analyzesample'
class AnalyzeListChainedRows(Expression):
4897class AnalyzeListChainedRows(Expression):
4898    arg_types = {"expression": False}
arg_types = {'expression': False}
key = 'analyzelistchainedrows'
class AnalyzeDelete(Expression):
4901class AnalyzeDelete(Expression):
4902    arg_types = {"kind": False}
arg_types = {'kind': False}
key = 'analyzedelete'
class AnalyzeWith(Expression):
4905class AnalyzeWith(Expression):
4906    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'analyzewith'
class AnalyzeValidate(Expression):
4909class AnalyzeValidate(Expression):
4910    arg_types = {
4911        "kind": True,
4912        "this": False,
4913        "expression": False,
4914    }
arg_types = {'kind': True, 'this': False, 'expression': False}
key = 'analyzevalidate'
class AnalyzeColumns(Expression):
4917class AnalyzeColumns(Expression):
4918    pass
key = 'analyzecolumns'
class UsingData(Expression):
4921class UsingData(Expression):
4922    pass
key = 'usingdata'
class AddConstraint(Expression):
4925class AddConstraint(Expression):
4926    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'addconstraint'
class AddPartition(Expression):
4929class AddPartition(Expression):
4930    arg_types = {"this": True, "exists": False}
arg_types = {'this': True, 'exists': False}
key = 'addpartition'
class AttachOption(Expression):
4933class AttachOption(Expression):
4934    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'attachoption'
class DropPartition(Expression):
4937class DropPartition(Expression):
4938    arg_types = {"expressions": True, "exists": False}
arg_types = {'expressions': True, 'exists': False}
key = 'droppartition'
class ReplacePartition(Expression):
4942class ReplacePartition(Expression):
4943    arg_types = {"expression": True, "source": True}
arg_types = {'expression': True, 'source': True}
key = 'replacepartition'
class Binary(Condition):
4947class Binary(Condition):
4948    arg_types = {"this": True, "expression": True}
4949
4950    @property
4951    def left(self) -> Expression:
4952        return self.this
4953
4954    @property
4955    def right(self) -> Expression:
4956        return self.expression
arg_types = {'this': True, 'expression': True}
left: Expression
4950    @property
4951    def left(self) -> Expression:
4952        return self.this
right: Expression
4954    @property
4955    def right(self) -> Expression:
4956        return self.expression
key = 'binary'
class Add(Binary):
4959class Add(Binary):
4960    pass
key = 'add'
class Connector(Binary):
4963class Connector(Binary):
4964    pass
key = 'connector'
class BitwiseAnd(Binary):
4967class BitwiseAnd(Binary):
4968    pass
key = 'bitwiseand'
class BitwiseLeftShift(Binary):
4971class BitwiseLeftShift(Binary):
4972    pass
key = 'bitwiseleftshift'
class BitwiseOr(Binary):
4975class BitwiseOr(Binary):
4976    pass
key = 'bitwiseor'
class BitwiseRightShift(Binary):
4979class BitwiseRightShift(Binary):
4980    pass
key = 'bitwiserightshift'
class BitwiseXor(Binary):
4983class BitwiseXor(Binary):
4984    pass
key = 'bitwisexor'
class Div(Binary):
4987class Div(Binary):
4988    arg_types = {"this": True, "expression": True, "typed": False, "safe": False}
arg_types = {'this': True, 'expression': True, 'typed': False, 'safe': False}
key = 'div'
class Overlaps(Binary):
4991class Overlaps(Binary):
4992    pass
key = 'overlaps'
class Dot(Binary):
4995class Dot(Binary):
4996    @property
4997    def is_star(self) -> bool:
4998        return self.expression.is_star
4999
5000    @property
5001    def name(self) -> str:
5002        return self.expression.name
5003
5004    @property
5005    def output_name(self) -> str:
5006        return self.name
5007
5008    @classmethod
5009    def build(self, expressions: t.Sequence[Expression]) -> Dot:
5010        """Build a Dot object with a sequence of expressions."""
5011        if len(expressions) < 2:
5012            raise ValueError("Dot requires >= 2 expressions.")
5013
5014        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))
5015
5016    @property
5017    def parts(self) -> t.List[Expression]:
5018        """Return the parts of a table / column in order catalog, db, table."""
5019        this, *parts = self.flatten()
5020
5021        parts.reverse()
5022
5023        for arg in COLUMN_PARTS:
5024            part = this.args.get(arg)
5025
5026            if isinstance(part, Expression):
5027                parts.append(part)
5028
5029        parts.reverse()
5030        return parts
is_star: bool
4996    @property
4997    def is_star(self) -> bool:
4998        return self.expression.is_star

Checks whether an expression is a star.

name: str
5000    @property
5001    def name(self) -> str:
5002        return self.expression.name
output_name: str
5004    @property
5005    def output_name(self) -> str:
5006        return self.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
@classmethod
def build( self, expressions: Sequence[Expression]) -> Dot:
5008    @classmethod
5009    def build(self, expressions: t.Sequence[Expression]) -> Dot:
5010        """Build a Dot object with a sequence of expressions."""
5011        if len(expressions) < 2:
5012            raise ValueError("Dot requires >= 2 expressions.")
5013
5014        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))

Build a Dot object with a sequence of expressions.

parts: List[Expression]
5016    @property
5017    def parts(self) -> t.List[Expression]:
5018        """Return the parts of a table / column in order catalog, db, table."""
5019        this, *parts = self.flatten()
5020
5021        parts.reverse()
5022
5023        for arg in COLUMN_PARTS:
5024            part = this.args.get(arg)
5025
5026            if isinstance(part, Expression):
5027                parts.append(part)
5028
5029        parts.reverse()
5030        return parts

Return the parts of a table / column in order catalog, db, table.

key = 'dot'
DATA_TYPE = typing.Union[str, Identifier, Dot, DataType, DataType.Type]
class DPipe(Binary):
5036class DPipe(Binary):
5037    arg_types = {"this": True, "expression": True, "safe": False}
arg_types = {'this': True, 'expression': True, 'safe': False}
key = 'dpipe'
class EQ(Binary, Predicate):
5040class EQ(Binary, Predicate):
5041    pass
key = 'eq'
class NullSafeEQ(Binary, Predicate):
5044class NullSafeEQ(Binary, Predicate):
5045    pass
key = 'nullsafeeq'
class NullSafeNEQ(Binary, Predicate):
5048class NullSafeNEQ(Binary, Predicate):
5049    pass
key = 'nullsafeneq'
class PropertyEQ(Binary):
5053class PropertyEQ(Binary):
5054    pass
key = 'propertyeq'
class Distance(Binary):
5057class Distance(Binary):
5058    pass
key = 'distance'
class Escape(Binary):
5061class Escape(Binary):
5062    pass
key = 'escape'
class Glob(Binary, Predicate):
5065class Glob(Binary, Predicate):
5066    pass
key = 'glob'
class GT(Binary, Predicate):
5069class GT(Binary, Predicate):
5070    pass
key = 'gt'
class GTE(Binary, Predicate):
5073class GTE(Binary, Predicate):
5074    pass
key = 'gte'
class ILike(Binary, Predicate):
5077class ILike(Binary, Predicate):
5078    pass
key = 'ilike'
class ILikeAny(Binary, Predicate):
5081class ILikeAny(Binary, Predicate):
5082    pass
key = 'ilikeany'
class IntDiv(Binary):
5085class IntDiv(Binary):
5086    pass
key = 'intdiv'
class Is(Binary, Predicate):
5089class Is(Binary, Predicate):
5090    pass
key = 'is'
class Kwarg(Binary):
5093class Kwarg(Binary):
5094    """Kwarg in special functions like func(kwarg => y)."""

Kwarg in special functions like func(kwarg => y).

key = 'kwarg'
class Like(Binary, Predicate):
5097class Like(Binary, Predicate):
5098    pass
key = 'like'
class LikeAny(Binary, Predicate):
5101class LikeAny(Binary, Predicate):
5102    pass
key = 'likeany'
class LT(Binary, Predicate):
5105class LT(Binary, Predicate):
5106    pass
key = 'lt'
class LTE(Binary, Predicate):
5109class LTE(Binary, Predicate):
5110    pass
key = 'lte'
class Mod(Binary):
5113class Mod(Binary):
5114    pass
key = 'mod'
class Mul(Binary):
5117class Mul(Binary):
5118    pass
key = 'mul'
class NEQ(Binary, Predicate):
5121class NEQ(Binary, Predicate):
5122    pass
key = 'neq'
class Operator(Binary):
5126class Operator(Binary):
5127    arg_types = {"this": True, "operator": True, "expression": True}
arg_types = {'this': True, 'operator': True, 'expression': True}
key = 'operator'
class SimilarTo(Binary, Predicate):
5130class SimilarTo(Binary, Predicate):
5131    pass
key = 'similarto'
class Slice(Binary):
5134class Slice(Binary):
5135    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'slice'
class Sub(Binary):
5138class Sub(Binary):
5139    pass
key = 'sub'
class Unary(Condition):
5144class Unary(Condition):
5145    pass
key = 'unary'
class BitwiseNot(Unary):
5148class BitwiseNot(Unary):
5149    pass
key = 'bitwisenot'
class Not(Unary):
5152class Not(Unary):
5153    pass
key = 'not'
class Paren(Unary):
5156class Paren(Unary):
5157    @property
5158    def output_name(self) -> str:
5159        return self.this.name
output_name: str
5157    @property
5158    def output_name(self) -> str:
5159        return self.this.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'paren'
class Neg(Unary):
5162class Neg(Unary):
5163    def to_py(self) -> int | Decimal:
5164        if self.is_number:
5165            return self.this.to_py() * -1
5166        return super().to_py()
def to_py(self) -> int | decimal.Decimal:
5163    def to_py(self) -> int | Decimal:
5164        if self.is_number:
5165            return self.this.to_py() * -1
5166        return super().to_py()

Returns a Python object equivalent of the SQL node.

key = 'neg'
class Alias(Expression):
5169class Alias(Expression):
5170    arg_types = {"this": True, "alias": False}
5171
5172    @property
5173    def output_name(self) -> str:
5174        return self.alias
arg_types = {'this': True, 'alias': False}
output_name: str
5172    @property
5173    def output_name(self) -> str:
5174        return self.alias

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'alias'
class PivotAlias(Alias):
5179class PivotAlias(Alias):
5180    pass
key = 'pivotalias'
class PivotAny(Expression):
5185class PivotAny(Expression):
5186    arg_types = {"this": False}
arg_types = {'this': False}
key = 'pivotany'
class Aliases(Expression):
5189class Aliases(Expression):
5190    arg_types = {"this": True, "expressions": True}
5191
5192    @property
5193    def aliases(self):
5194        return self.expressions
arg_types = {'this': True, 'expressions': True}
aliases
5192    @property
5193    def aliases(self):
5194        return self.expressions
key = 'aliases'
class AtIndex(Expression):
5198class AtIndex(Expression):
5199    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'atindex'
class AtTimeZone(Expression):
5202class AtTimeZone(Expression):
5203    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'attimezone'
class FromTimeZone(Expression):
5206class FromTimeZone(Expression):
5207    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'fromtimezone'
class Between(Predicate):
5210class Between(Predicate):
5211    arg_types = {"this": True, "low": True, "high": True}
arg_types = {'this': True, 'low': True, 'high': True}
key = 'between'
class Bracket(Condition):
5214class Bracket(Condition):
5215    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
5216    arg_types = {
5217        "this": True,
5218        "expressions": True,
5219        "offset": False,
5220        "safe": False,
5221        "returns_list_for_maps": False,
5222    }
5223
5224    @property
5225    def output_name(self) -> str:
5226        if len(self.expressions) == 1:
5227            return self.expressions[0].output_name
5228
5229        return super().output_name
arg_types = {'this': True, 'expressions': True, 'offset': False, 'safe': False, 'returns_list_for_maps': False}
output_name: str
5224    @property
5225    def output_name(self) -> str:
5226        if len(self.expressions) == 1:
5227            return self.expressions[0].output_name
5228
5229        return super().output_name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'bracket'
class Distinct(Expression):
5232class Distinct(Expression):
5233    arg_types = {"expressions": False, "on": False}
arg_types = {'expressions': False, 'on': False}
key = 'distinct'
class In(Predicate):
5236class In(Predicate):
5237    arg_types = {
5238        "this": True,
5239        "expressions": False,
5240        "query": False,
5241        "unnest": False,
5242        "field": False,
5243        "is_global": False,
5244    }
arg_types = {'this': True, 'expressions': False, 'query': False, 'unnest': False, 'field': False, 'is_global': False}
key = 'in'
class ForIn(Expression):
5248class ForIn(Expression):
5249    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'forin'
class TimeUnit(Expression):
5252class TimeUnit(Expression):
5253    """Automatically converts unit arg into a var."""
5254
5255    arg_types = {"unit": False}
5256
5257    UNABBREVIATED_UNIT_NAME = {
5258        "D": "DAY",
5259        "H": "HOUR",
5260        "M": "MINUTE",
5261        "MS": "MILLISECOND",
5262        "NS": "NANOSECOND",
5263        "Q": "QUARTER",
5264        "S": "SECOND",
5265        "US": "MICROSECOND",
5266        "W": "WEEK",
5267        "Y": "YEAR",
5268    }
5269
5270    VAR_LIKE = (Column, Literal, Var)
5271
5272    def __init__(self, **args):
5273        unit = args.get("unit")
5274        if isinstance(unit, self.VAR_LIKE):
5275            args["unit"] = Var(
5276                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
5277            )
5278        elif isinstance(unit, Week):
5279            unit.set("this", Var(this=unit.this.name.upper()))
5280
5281        super().__init__(**args)
5282
5283    @property
5284    def unit(self) -> t.Optional[Var | IntervalSpan]:
5285        return self.args.get("unit")

Automatically converts unit arg into a var.

TimeUnit(**args)
5272    def __init__(self, **args):
5273        unit = args.get("unit")
5274        if isinstance(unit, self.VAR_LIKE):
5275            args["unit"] = Var(
5276                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
5277            )
5278        elif isinstance(unit, Week):
5279            unit.set("this", Var(this=unit.this.name.upper()))
5280
5281        super().__init__(**args)
arg_types = {'unit': False}
UNABBREVIATED_UNIT_NAME = {'D': 'DAY', 'H': 'HOUR', 'M': 'MINUTE', 'MS': 'MILLISECOND', 'NS': 'NANOSECOND', 'Q': 'QUARTER', 'S': 'SECOND', 'US': 'MICROSECOND', 'W': 'WEEK', 'Y': 'YEAR'}
VAR_LIKE = (<class 'Column'>, <class 'Literal'>, <class 'Var'>)
unit: Union[Var, IntervalSpan, NoneType]
5283    @property
5284    def unit(self) -> t.Optional[Var | IntervalSpan]:
5285        return self.args.get("unit")
key = 'timeunit'
class IntervalOp(TimeUnit):
5288class IntervalOp(TimeUnit):
5289    arg_types = {"unit": False, "expression": True}
5290
5291    def interval(self):
5292        return Interval(
5293            this=self.expression.copy(),
5294            unit=self.unit.copy() if self.unit else None,
5295        )
arg_types = {'unit': False, 'expression': True}
def interval(self):
5291    def interval(self):
5292        return Interval(
5293            this=self.expression.copy(),
5294            unit=self.unit.copy() if self.unit else None,
5295        )
key = 'intervalop'
class IntervalSpan(DataType):
5301class IntervalSpan(DataType):
5302    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'intervalspan'
class Interval(TimeUnit):
5305class Interval(TimeUnit):
5306    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key = 'interval'
class IgnoreNulls(Expression):
5309class IgnoreNulls(Expression):
5310    pass
key = 'ignorenulls'
class RespectNulls(Expression):
5313class RespectNulls(Expression):
5314    pass
key = 'respectnulls'
class HavingMax(Expression):
5318class HavingMax(Expression):
5319    arg_types = {"this": True, "expression": True, "max": True}
arg_types = {'this': True, 'expression': True, 'max': True}
key = 'havingmax'
class Func(Condition):
5323class Func(Condition):
5324    """
5325    The base class for all function expressions.
5326
5327    Attributes:
5328        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
5329            treated as a variable length argument and the argument's value will be stored as a list.
5330        _sql_names (list): the SQL name (1st item in the list) and aliases (subsequent items) for this
5331            function expression. These values are used to map this node to a name during parsing as
5332            well as to provide the function's name during SQL string generation. By default the SQL
5333            name is set to the expression's class name transformed to snake case.
5334    """
5335
5336    is_var_len_args = False
5337
5338    @classmethod
5339    def from_arg_list(cls, args):
5340        if cls.is_var_len_args:
5341            all_arg_keys = list(cls.arg_types)
5342            # If this function supports variable length argument treat the last argument as such.
5343            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5344            num_non_var = len(non_var_len_arg_keys)
5345
5346            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5347            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5348        else:
5349            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5350
5351        return cls(**args_dict)
5352
5353    @classmethod
5354    def sql_names(cls):
5355        if cls is Func:
5356            raise NotImplementedError(
5357                "SQL name is only supported by concrete function implementations"
5358            )
5359        if "_sql_names" not in cls.__dict__:
5360            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5361        return cls._sql_names
5362
5363    @classmethod
5364    def sql_name(cls):
5365        return cls.sql_names()[0]
5366
5367    @classmethod
5368    def default_parser_mappings(cls):
5369        return {name: cls.from_arg_list for name in cls.sql_names()}

The base class for all function expressions.

Attributes:
  • is_var_len_args (bool): if set to True the last argument defined in arg_types will be treated as a variable length argument and the argument's value will be stored as a list.
  • _sql_names (list): the SQL name (1st item in the list) and aliases (subsequent items) for this function expression. These values are used to map this node to a name during parsing as well as to provide the function's name during SQL string generation. By default the SQL name is set to the expression's class name transformed to snake case.
is_var_len_args = False
@classmethod
def from_arg_list(cls, args):
5338    @classmethod
5339    def from_arg_list(cls, args):
5340        if cls.is_var_len_args:
5341            all_arg_keys = list(cls.arg_types)
5342            # If this function supports variable length argument treat the last argument as such.
5343            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5344            num_non_var = len(non_var_len_arg_keys)
5345
5346            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5347            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5348        else:
5349            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5350
5351        return cls(**args_dict)
@classmethod
def sql_names(cls):
5353    @classmethod
5354    def sql_names(cls):
5355        if cls is Func:
5356            raise NotImplementedError(
5357                "SQL name is only supported by concrete function implementations"
5358            )
5359        if "_sql_names" not in cls.__dict__:
5360            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5361        return cls._sql_names
@classmethod
def sql_name(cls):
5363    @classmethod
5364    def sql_name(cls):
5365        return cls.sql_names()[0]
@classmethod
def default_parser_mappings(cls):
5367    @classmethod
5368    def default_parser_mappings(cls):
5369        return {name: cls.from_arg_list for name in cls.sql_names()}
key = 'func'
class AggFunc(Func):
5372class AggFunc(Func):
5373    pass
key = 'aggfunc'
class ArrayRemove(Func):
5376class ArrayRemove(Func):
5377    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayremove'
class ParameterizedAgg(AggFunc):
5380class ParameterizedAgg(AggFunc):
5381    arg_types = {"this": True, "expressions": True, "params": True}
arg_types = {'this': True, 'expressions': True, 'params': True}
key = 'parameterizedagg'
class Abs(Func):
5384class Abs(Func):
5385    pass
key = 'abs'
class ArgMax(AggFunc):
5388class ArgMax(AggFunc):
5389    arg_types = {"this": True, "expression": True, "count": False}
5390    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmax'
class ArgMin(AggFunc):
5393class ArgMin(AggFunc):
5394    arg_types = {"this": True, "expression": True, "count": False}
5395    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmin'
class ApproxTopK(AggFunc):
5398class ApproxTopK(AggFunc):
5399    arg_types = {"this": True, "expression": False, "counters": False}
arg_types = {'this': True, 'expression': False, 'counters': False}
key = 'approxtopk'
class Flatten(Func):
5402class Flatten(Func):
5403    pass
key = 'flatten'
class Transform(Func):
5407class Transform(Func):
5408    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'transform'
class Anonymous(Func):
5411class Anonymous(Func):
5412    arg_types = {"this": True, "expressions": False}
5413    is_var_len_args = True
5414
5415    @property
5416    def name(self) -> str:
5417        return self.this if isinstance(self.this, str) else self.this.name
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
name: str
5415    @property
5416    def name(self) -> str:
5417        return self.this if isinstance(self.this, str) else self.this.name
key = 'anonymous'
class AnonymousAggFunc(AggFunc):
5420class AnonymousAggFunc(AggFunc):
5421    arg_types = {"this": True, "expressions": False}
5422    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'anonymousaggfunc'
class CombinedAggFunc(AnonymousAggFunc):
5426class CombinedAggFunc(AnonymousAggFunc):
5427    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'combinedaggfunc'
class CombinedParameterizedAgg(ParameterizedAgg):
5430class CombinedParameterizedAgg(ParameterizedAgg):
5431    arg_types = {"this": True, "expressions": True, "params": True}
arg_types = {'this': True, 'expressions': True, 'params': True}
key = 'combinedparameterizedagg'
class Hll(AggFunc):
5436class Hll(AggFunc):
5437    arg_types = {"this": True, "expressions": False}
5438    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'hll'
class ApproxDistinct(AggFunc):
5441class ApproxDistinct(AggFunc):
5442    arg_types = {"this": True, "accuracy": False}
5443    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
arg_types = {'this': True, 'accuracy': False}
key = 'approxdistinct'
class Apply(Func):
5446class Apply(Func):
5447    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'apply'
class Array(Func):
5450class Array(Func):
5451    arg_types = {"expressions": False, "bracket_notation": False}
5452    is_var_len_args = True
arg_types = {'expressions': False, 'bracket_notation': False}
is_var_len_args = True
key = 'array'
class ToArray(Func):
5456class ToArray(Func):
5457    pass
key = 'toarray'
class List(Func):
5461class List(Func):
5462    arg_types = {"expressions": False}
5463    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'list'
class Pad(Func):
5467class Pad(Func):
5468    arg_types = {"this": True, "expression": True, "fill_pattern": False, "is_left": True}
arg_types = {'this': True, 'expression': True, 'fill_pattern': False, 'is_left': True}
key = 'pad'
class ToChar(Func):
5473class ToChar(Func):
5474    arg_types = {"this": True, "format": False, "nlsparam": False}
arg_types = {'this': True, 'format': False, 'nlsparam': False}
key = 'tochar'
class ToNumber(Func):
5479class ToNumber(Func):
5480    arg_types = {
5481        "this": True,
5482        "format": False,
5483        "nlsparam": False,
5484        "precision": False,
5485        "scale": False,
5486    }
arg_types = {'this': True, 'format': False, 'nlsparam': False, 'precision': False, 'scale': False}
key = 'tonumber'
class ToDouble(Func):
5490class ToDouble(Func):
5491    arg_types = {
5492        "this": True,
5493        "format": False,
5494    }
arg_types = {'this': True, 'format': False}
key = 'todouble'
class Columns(Func):
5497class Columns(Func):
5498    arg_types = {"this": True, "unpack": False}
arg_types = {'this': True, 'unpack': False}
key = 'columns'
class Convert(Func):
5502class Convert(Func):
5503    arg_types = {"this": True, "expression": True, "style": False}
arg_types = {'this': True, 'expression': True, 'style': False}
key = 'convert'
class ConvertToCharset(Func):
5507class ConvertToCharset(Func):
5508    arg_types = {"this": True, "dest": True, "source": False}
arg_types = {'this': True, 'dest': True, 'source': False}
key = 'converttocharset'
class ConvertTimezone(Func):
5511class ConvertTimezone(Func):
5512    arg_types = {"source_tz": False, "target_tz": True, "timestamp": True}
arg_types = {'source_tz': False, 'target_tz': True, 'timestamp': True}
key = 'converttimezone'
class GenerateSeries(Func):
5515class GenerateSeries(Func):
5516    arg_types = {"start": True, "end": True, "step": False, "is_end_exclusive": False}
arg_types = {'start': True, 'end': True, 'step': False, 'is_end_exclusive': False}
key = 'generateseries'
class ExplodingGenerateSeries(GenerateSeries):
5522class ExplodingGenerateSeries(GenerateSeries):
5523    pass
key = 'explodinggenerateseries'
class ArrayAgg(AggFunc):
5526class ArrayAgg(AggFunc):
5527    arg_types = {"this": True, "nulls_excluded": False}
arg_types = {'this': True, 'nulls_excluded': False}
key = 'arrayagg'
class ArrayUniqueAgg(AggFunc):
5530class ArrayUniqueAgg(AggFunc):
5531    pass
key = 'arrayuniqueagg'
class ArrayAll(Func):
5534class ArrayAll(Func):
5535    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayall'
class ArrayAny(Func):
5539class ArrayAny(Func):
5540    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayany'
class ArrayConcat(Func):
5543class ArrayConcat(Func):
5544    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
5545    arg_types = {"this": True, "expressions": False}
5546    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'arrayconcat'
class ArrayConcatAgg(AggFunc):
5549class ArrayConcatAgg(AggFunc):
5550    pass
key = 'arrayconcatagg'
class ArrayConstructCompact(Func):
5553class ArrayConstructCompact(Func):
5554    arg_types = {"expressions": True}
5555    is_var_len_args = True
arg_types = {'expressions': True}
is_var_len_args = True
key = 'arrayconstructcompact'
class ArrayContains(Binary, Func):
5558class ArrayContains(Binary, Func):
5559    _sql_names = ["ARRAY_CONTAINS", "ARRAY_HAS"]
key = 'arraycontains'
class ArrayContainsAll(Binary, Func):
5562class ArrayContainsAll(Binary, Func):
5563    _sql_names = ["ARRAY_CONTAINS_ALL", "ARRAY_HAS_ALL"]
key = 'arraycontainsall'
class ArrayFilter(Func):
5566class ArrayFilter(Func):
5567    arg_types = {"this": True, "expression": True}
5568    _sql_names = ["FILTER", "ARRAY_FILTER"]
arg_types = {'this': True, 'expression': True}
key = 'arrayfilter'
class ArrayToString(Func):
5571class ArrayToString(Func):
5572    arg_types = {"this": True, "expression": True, "null": False}
5573    _sql_names = ["ARRAY_TO_STRING", "ARRAY_JOIN"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'arraytostring'
class ArrayIntersect(Func):
5576class ArrayIntersect(Func):
5577    arg_types = {"expressions": True}
5578    is_var_len_args = True
5579    _sql_names = ["ARRAY_INTERSECT", "ARRAY_INTERSECTION"]
arg_types = {'expressions': True}
is_var_len_args = True
key = 'arrayintersect'
class StPoint(Func):
5582class StPoint(Func):
5583    arg_types = {"this": True, "expression": True, "null": False}
5584    _sql_names = ["ST_POINT", "ST_MAKEPOINT"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'stpoint'
class StDistance(Func):
5587class StDistance(Func):
5588    arg_types = {"this": True, "expression": True, "use_spheroid": False}
arg_types = {'this': True, 'expression': True, 'use_spheroid': False}
key = 'stdistance'
class String(Func):
5592class String(Func):
5593    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key = 'string'
class StringToArray(Func):
5596class StringToArray(Func):
5597    arg_types = {"this": True, "expression": True, "null": False}
5598    _sql_names = ["STRING_TO_ARRAY", "SPLIT_BY_STRING", "STRTOK_TO_ARRAY"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'stringtoarray'
class ArrayOverlaps(Binary, Func):
5601class ArrayOverlaps(Binary, Func):
5602    pass
key = 'arrayoverlaps'
class ArraySize(Func):
5605class ArraySize(Func):
5606    arg_types = {"this": True, "expression": False}
5607    _sql_names = ["ARRAY_SIZE", "ARRAY_LENGTH"]
arg_types = {'this': True, 'expression': False}
key = 'arraysize'
class ArraySort(Func):
5610class ArraySort(Func):
5611    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysort'
class ArraySum(Func):
5614class ArraySum(Func):
5615    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysum'
class ArrayUnionAgg(AggFunc):
5618class ArrayUnionAgg(AggFunc):
5619    pass
key = 'arrayunionagg'
class Avg(AggFunc):
5622class Avg(AggFunc):
5623    pass
key = 'avg'
class AnyValue(AggFunc):
5626class AnyValue(AggFunc):
5627    pass
key = 'anyvalue'
class Lag(AggFunc):
5630class Lag(AggFunc):
5631    arg_types = {"this": True, "offset": False, "default": False}
arg_types = {'this': True, 'offset': False, 'default': False}
key = 'lag'
class Lead(AggFunc):
5634class Lead(AggFunc):
5635    arg_types = {"this": True, "offset": False, "default": False}
arg_types = {'this': True, 'offset': False, 'default': False}
key = 'lead'
class First(AggFunc):
5640class First(AggFunc):
5641    pass
key = 'first'
class Last(AggFunc):
5644class Last(AggFunc):
5645    pass
key = 'last'
class FirstValue(AggFunc):
5648class FirstValue(AggFunc):
5649    pass
key = 'firstvalue'
class LastValue(AggFunc):
5652class LastValue(AggFunc):
5653    pass
key = 'lastvalue'
class NthValue(AggFunc):
5656class NthValue(AggFunc):
5657    arg_types = {"this": True, "offset": True}
arg_types = {'this': True, 'offset': True}
key = 'nthvalue'
class Case(Func):
5660class Case(Func):
5661    arg_types = {"this": False, "ifs": True, "default": False}
5662
5663    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5664        instance = maybe_copy(self, copy)
5665        instance.append(
5666            "ifs",
5667            If(
5668                this=maybe_parse(condition, copy=copy, **opts),
5669                true=maybe_parse(then, copy=copy, **opts),
5670            ),
5671        )
5672        return instance
5673
5674    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5675        instance = maybe_copy(self, copy)
5676        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5677        return instance
arg_types = {'this': False, 'ifs': True, 'default': False}
def when( self, condition: Union[str, Expression], then: Union[str, Expression], copy: bool = True, **opts) -> Case:
5663    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5664        instance = maybe_copy(self, copy)
5665        instance.append(
5666            "ifs",
5667            If(
5668                this=maybe_parse(condition, copy=copy, **opts),
5669                true=maybe_parse(then, copy=copy, **opts),
5670            ),
5671        )
5672        return instance
def else_( self, condition: Union[str, Expression], copy: bool = True, **opts) -> Case:
5674    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5675        instance = maybe_copy(self, copy)
5676        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5677        return instance
key = 'case'
class Cast(Func):
5680class Cast(Func):
5681    arg_types = {
5682        "this": True,
5683        "to": True,
5684        "format": False,
5685        "safe": False,
5686        "action": False,
5687        "default": False,
5688    }
5689
5690    @property
5691    def name(self) -> str:
5692        return self.this.name
5693
5694    @property
5695    def to(self) -> DataType:
5696        return self.args["to"]
5697
5698    @property
5699    def output_name(self) -> str:
5700        return self.name
5701
5702    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5703        """
5704        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5705        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5706        array<int> != array<float>.
5707
5708        Args:
5709            dtypes: the data types to compare this Cast's DataType to.
5710
5711        Returns:
5712            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5713        """
5714        return self.to.is_type(*dtypes)
arg_types = {'this': True, 'to': True, 'format': False, 'safe': False, 'action': False, 'default': False}
name: str
5690    @property
5691    def name(self) -> str:
5692        return self.this.name
to: DataType
5694    @property
5695    def to(self) -> DataType:
5696        return self.args["to"]
output_name: str
5698    @property
5699    def output_name(self) -> str:
5700        return self.name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
def is_type( self, *dtypes: Union[str, Identifier, Dot, DataType, DataType.Type]) -> bool:
5702    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5703        """
5704        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5705        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5706        array<int> != array<float>.
5707
5708        Args:
5709            dtypes: the data types to compare this Cast's DataType to.
5710
5711        Returns:
5712            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5713        """
5714        return self.to.is_type(*dtypes)

Checks whether this Cast's DataType matches one of the provided data types. Nested types like arrays or structs will be compared using "structural equivalence" semantics, so e.g. array != array.

Arguments:
  • dtypes: the data types to compare this Cast's DataType to.
Returns:

True, if and only if there is a type in dtypes which is equal to this Cast's DataType.

key = 'cast'
class TryCast(Cast):
5717class TryCast(Cast):
5718    pass
key = 'trycast'
class JSONCast(Cast):
5722class JSONCast(Cast):
5723    pass
key = 'jsoncast'
class Try(Func):
5726class Try(Func):
5727    pass
key = 'try'
class CastToStrType(Func):
5730class CastToStrType(Func):
5731    arg_types = {"this": True, "to": True}
arg_types = {'this': True, 'to': True}
key = 'casttostrtype'
class TranslateCharacters(Expression):
5735class TranslateCharacters(Expression):
5736    arg_types = {"this": True, "expression": True, "with_error": False}
arg_types = {'this': True, 'expression': True, 'with_error': False}
key = 'translatecharacters'
class Collate(Binary, Func):
5739class Collate(Binary, Func):
5740    pass
key = 'collate'
class Ceil(Func):
5743class Ceil(Func):
5744    arg_types = {"this": True, "decimals": False, "to": False}
5745    _sql_names = ["CEIL", "CEILING"]
arg_types = {'this': True, 'decimals': False, 'to': False}
key = 'ceil'
class Coalesce(Func):
5748class Coalesce(Func):
5749    arg_types = {"this": True, "expressions": False, "is_nvl": False, "is_null": False}
5750    is_var_len_args = True
5751    _sql_names = ["COALESCE", "IFNULL", "NVL"]
arg_types = {'this': True, 'expressions': False, 'is_nvl': False, 'is_null': False}
is_var_len_args = True
key = 'coalesce'
class Chr(Func):
5754class Chr(Func):
5755    arg_types = {"expressions": True, "charset": False}
5756    is_var_len_args = True
5757    _sql_names = ["CHR", "CHAR"]
arg_types = {'expressions': True, 'charset': False}
is_var_len_args = True
key = 'chr'
class Concat(Func):
5760class Concat(Func):
5761    arg_types = {"expressions": True, "safe": False, "coalesce": False}
5762    is_var_len_args = True
arg_types = {'expressions': True, 'safe': False, 'coalesce': False}
is_var_len_args = True
key = 'concat'
class ConcatWs(Concat):
5765class ConcatWs(Concat):
5766    _sql_names = ["CONCAT_WS"]
key = 'concatws'
class Contains(Func):
5769class Contains(Func):
5770    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'contains'
class ConnectByRoot(Func):
5774class ConnectByRoot(Func):
5775    pass
key = 'connectbyroot'
class Count(AggFunc):
5778class Count(AggFunc):
5779    arg_types = {"this": False, "expressions": False, "big_int": False}
5780    is_var_len_args = True
arg_types = {'this': False, 'expressions': False, 'big_int': False}
is_var_len_args = True
key = 'count'
class CountIf(AggFunc):
5783class CountIf(AggFunc):
5784    _sql_names = ["COUNT_IF", "COUNTIF"]
key = 'countif'
class Cbrt(Func):
5788class Cbrt(Func):
5789    pass
key = 'cbrt'
class CurrentDate(Func):
5792class CurrentDate(Func):
5793    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdate'
class CurrentDatetime(Func):
5796class CurrentDatetime(Func):
5797    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdatetime'
class CurrentTime(Func):
5800class CurrentTime(Func):
5801    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttime'
class CurrentTimestamp(Func):
5804class CurrentTimestamp(Func):
5805    arg_types = {"this": False, "sysdate": False}
arg_types = {'this': False, 'sysdate': False}
key = 'currenttimestamp'
class CurrentSchema(Func):
5808class CurrentSchema(Func):
5809    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentschema'
class CurrentUser(Func):
5812class CurrentUser(Func):
5813    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentuser'
class DateAdd(Func, IntervalOp):
5816class DateAdd(Func, IntervalOp):
5817    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'dateadd'
class DateBin(Func, IntervalOp):
5820class DateBin(Func, IntervalOp):
5821    arg_types = {"this": True, "expression": True, "unit": False, "zone": False}
arg_types = {'this': True, 'expression': True, 'unit': False, 'zone': False}
key = 'datebin'
class DateSub(Func, IntervalOp):
5824class DateSub(Func, IntervalOp):
5825    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datesub'
class DateDiff(Func, TimeUnit):
5828class DateDiff(Func, TimeUnit):
5829    _sql_names = ["DATEDIFF", "DATE_DIFF"]
5830    arg_types = {"this": True, "expression": True, "unit": False, "zone": False}
arg_types = {'this': True, 'expression': True, 'unit': False, 'zone': False}
key = 'datediff'
class DateTrunc(Func):
5833class DateTrunc(Func):
5834    arg_types = {"unit": True, "this": True, "zone": False}
5835
5836    def __init__(self, **args):
5837        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5838        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5839        unabbreviate = args.pop("unabbreviate", True)
5840
5841        unit = args.get("unit")
5842        if isinstance(unit, TimeUnit.VAR_LIKE):
5843            unit_name = unit.name.upper()
5844            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5845                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5846
5847            args["unit"] = Literal.string(unit_name)
5848        elif isinstance(unit, Week):
5849            unit.set("this", Literal.string(unit.this.name.upper()))
5850
5851        super().__init__(**args)
5852
5853    @property
5854    def unit(self) -> Expression:
5855        return self.args["unit"]
DateTrunc(**args)
5836    def __init__(self, **args):
5837        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5838        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5839        unabbreviate = args.pop("unabbreviate", True)
5840
5841        unit = args.get("unit")
5842        if isinstance(unit, TimeUnit.VAR_LIKE):
5843            unit_name = unit.name.upper()
5844            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5845                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5846
5847            args["unit"] = Literal.string(unit_name)
5848        elif isinstance(unit, Week):
5849            unit.set("this", Literal.string(unit.this.name.upper()))
5850
5851        super().__init__(**args)
arg_types = {'unit': True, 'this': True, 'zone': False}
unit: Expression
5853    @property
5854    def unit(self) -> Expression:
5855        return self.args["unit"]
key = 'datetrunc'
class Datetime(Func):
5860class Datetime(Func):
5861    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'datetime'
class DatetimeAdd(Func, IntervalOp):
5864class DatetimeAdd(Func, IntervalOp):
5865    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimeadd'
class DatetimeSub(Func, IntervalOp):
5868class DatetimeSub(Func, IntervalOp):
5869    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimesub'
class DatetimeDiff(Func, TimeUnit):
5872class DatetimeDiff(Func, TimeUnit):
5873    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimediff'
class DatetimeTrunc(Func, TimeUnit):
5876class DatetimeTrunc(Func, TimeUnit):
5877    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'datetimetrunc'
class DayOfWeek(Func):
5880class DayOfWeek(Func):
5881    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
key = 'dayofweek'
class DayOfWeekIso(Func):
5886class DayOfWeekIso(Func):
5887    _sql_names = ["DAYOFWEEK_ISO", "ISODOW"]
key = 'dayofweekiso'
class DayOfMonth(Func):
5890class DayOfMonth(Func):
5891    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
key = 'dayofmonth'
class DayOfYear(Func):
5894class DayOfYear(Func):
5895    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
key = 'dayofyear'
class ToDays(Func):
5898class ToDays(Func):
5899    pass
key = 'todays'
class WeekOfYear(Func):
5902class WeekOfYear(Func):
5903    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
key = 'weekofyear'
class MonthsBetween(Func):
5906class MonthsBetween(Func):
5907    arg_types = {"this": True, "expression": True, "roundoff": False}
arg_types = {'this': True, 'expression': True, 'roundoff': False}
key = 'monthsbetween'
class MakeInterval(Func):
5910class MakeInterval(Func):
5911    arg_types = {
5912        "year": False,
5913        "month": False,
5914        "day": False,
5915        "hour": False,
5916        "minute": False,
5917        "second": False,
5918    }
arg_types = {'year': False, 'month': False, 'day': False, 'hour': False, 'minute': False, 'second': False}
key = 'makeinterval'
class LastDay(Func, TimeUnit):
5921class LastDay(Func, TimeUnit):
5922    _sql_names = ["LAST_DAY", "LAST_DAY_OF_MONTH"]
5923    arg_types = {"this": True, "unit": False}
arg_types = {'this': True, 'unit': False}
key = 'lastday'
class Extract(Func):
5926class Extract(Func):
5927    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'extract'
class Exists(Func, SubqueryPredicate):
5930class Exists(Func, SubqueryPredicate):
5931    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'exists'
class Timestamp(Func):
5934class Timestamp(Func):
5935    arg_types = {"this": False, "zone": False, "with_tz": False}
arg_types = {'this': False, 'zone': False, 'with_tz': False}
key = 'timestamp'
class TimestampAdd(Func, TimeUnit):
5938class TimestampAdd(Func, TimeUnit):
5939    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampadd'
class TimestampSub(Func, TimeUnit):
5942class TimestampSub(Func, TimeUnit):
5943    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampsub'
class TimestampDiff(Func, TimeUnit):
5946class TimestampDiff(Func, TimeUnit):
5947    _sql_names = ["TIMESTAMPDIFF", "TIMESTAMP_DIFF"]
5948    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampdiff'
class TimestampTrunc(Func, TimeUnit):
5951class TimestampTrunc(Func, TimeUnit):
5952    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timestamptrunc'
class TimeAdd(Func, TimeUnit):
5955class TimeAdd(Func, TimeUnit):
5956    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timeadd'
class TimeSub(Func, TimeUnit):
5959class TimeSub(Func, TimeUnit):
5960    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timesub'
class TimeDiff(Func, TimeUnit):
5963class TimeDiff(Func, TimeUnit):
5964    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timediff'
class TimeTrunc(Func, TimeUnit):
5967class TimeTrunc(Func, TimeUnit):
5968    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timetrunc'
class DateFromParts(Func):
5971class DateFromParts(Func):
5972    _sql_names = ["DATE_FROM_PARTS", "DATEFROMPARTS"]
5973    arg_types = {"year": True, "month": True, "day": True}
arg_types = {'year': True, 'month': True, 'day': True}
key = 'datefromparts'
class TimeFromParts(Func):
5976class TimeFromParts(Func):
5977    _sql_names = ["TIME_FROM_PARTS", "TIMEFROMPARTS"]
5978    arg_types = {
5979        "hour": True,
5980        "min": True,
5981        "sec": True,
5982        "nano": False,
5983        "fractions": False,
5984        "precision": False,
5985    }
arg_types = {'hour': True, 'min': True, 'sec': True, 'nano': False, 'fractions': False, 'precision': False}
key = 'timefromparts'
class DateStrToDate(Func):
5988class DateStrToDate(Func):
5989    pass
key = 'datestrtodate'
class DateToDateStr(Func):
5992class DateToDateStr(Func):
5993    pass
key = 'datetodatestr'
class DateToDi(Func):
5996class DateToDi(Func):
5997    pass
key = 'datetodi'
class Date(Func):
6001class Date(Func):
6002    arg_types = {"this": False, "zone": False, "expressions": False}
6003    is_var_len_args = True
arg_types = {'this': False, 'zone': False, 'expressions': False}
is_var_len_args = True
key = 'date'
class Day(Func):
6006class Day(Func):
6007    pass
key = 'day'
class Decode(Func):
6010class Decode(Func):
6011    arg_types = {"this": True, "charset": True, "replace": False}
arg_types = {'this': True, 'charset': True, 'replace': False}
key = 'decode'
class DiToDate(Func):
6014class DiToDate(Func):
6015    pass
key = 'ditodate'
class Encode(Func):
6018class Encode(Func):
6019    arg_types = {"this": True, "charset": True}
arg_types = {'this': True, 'charset': True}
key = 'encode'
class Exp(Func):
6022class Exp(Func):
6023    pass
key = 'exp'
class Explode(Func, UDTF):
6027class Explode(Func, UDTF):
6028    arg_types = {"this": True, "expressions": False}
6029    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'explode'
class Inline(Func):
6033class Inline(Func):
6034    pass
key = 'inline'
class ExplodeOuter(Explode):
6037class ExplodeOuter(Explode):
6038    pass
key = 'explodeouter'
class Posexplode(Explode):
6041class Posexplode(Explode):
6042    pass
key = 'posexplode'
class PosexplodeOuter(Posexplode, ExplodeOuter):
6045class PosexplodeOuter(Posexplode, ExplodeOuter):
6046    pass
key = 'posexplodeouter'
class Unnest(Func, UDTF):
6049class Unnest(Func, UDTF):
6050    arg_types = {
6051        "expressions": True,
6052        "alias": False,
6053        "offset": False,
6054        "explode_array": False,
6055    }
6056
6057    @property
6058    def selects(self) -> t.List[Expression]:
6059        columns = super().selects
6060        offset = self.args.get("offset")
6061        if offset:
6062            columns = columns + [to_identifier("offset") if offset is True else offset]
6063        return columns
arg_types = {'expressions': True, 'alias': False, 'offset': False, 'explode_array': False}
selects: List[Expression]
6057    @property
6058    def selects(self) -> t.List[Expression]:
6059        columns = super().selects
6060        offset = self.args.get("offset")
6061        if offset:
6062            columns = columns + [to_identifier("offset") if offset is True else offset]
6063        return columns
key = 'unnest'
class Floor(Func):
6066class Floor(Func):
6067    arg_types = {"this": True, "decimals": False, "to": False}
arg_types = {'this': True, 'decimals': False, 'to': False}
key = 'floor'
class FromBase64(Func):
6070class FromBase64(Func):
6071    pass
key = 'frombase64'
class FeaturesAtTime(Func):
6074class FeaturesAtTime(Func):
6075    arg_types = {"this": True, "time": False, "num_rows": False, "ignore_feature_nulls": False}
arg_types = {'this': True, 'time': False, 'num_rows': False, 'ignore_feature_nulls': False}
key = 'featuresattime'
class ToBase64(Func):
6078class ToBase64(Func):
6079    pass
key = 'tobase64'
class FromISO8601Timestamp(Func):
6083class FromISO8601Timestamp(Func):
6084    _sql_names = ["FROM_ISO8601_TIMESTAMP"]
key = 'fromiso8601timestamp'
class GapFill(Func):
6087class GapFill(Func):
6088    arg_types = {
6089        "this": True,
6090        "ts_column": True,
6091        "bucket_width": True,
6092        "partitioning_columns": False,
6093        "value_columns": False,
6094        "origin": False,
6095        "ignore_nulls": False,
6096    }
arg_types = {'this': True, 'ts_column': True, 'bucket_width': True, 'partitioning_columns': False, 'value_columns': False, 'origin': False, 'ignore_nulls': False}
key = 'gapfill'
class GenerateDateArray(Func):
6100class GenerateDateArray(Func):
6101    arg_types = {"start": True, "end": True, "step": False}
arg_types = {'start': True, 'end': True, 'step': False}
key = 'generatedatearray'
class GenerateTimestampArray(Func):
6105class GenerateTimestampArray(Func):
6106    arg_types = {"start": True, "end": True, "step": True}
arg_types = {'start': True, 'end': True, 'step': True}
key = 'generatetimestamparray'
class Greatest(Func):
6109class Greatest(Func):
6110    arg_types = {"this": True, "expressions": False}
6111    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'greatest'
class OverflowTruncateBehavior(Expression):
6116class OverflowTruncateBehavior(Expression):
6117    arg_types = {"this": False, "with_count": True}
arg_types = {'this': False, 'with_count': True}
key = 'overflowtruncatebehavior'
class GroupConcat(AggFunc):
6120class GroupConcat(AggFunc):
6121    arg_types = {"this": True, "separator": False, "on_overflow": False}
arg_types = {'this': True, 'separator': False, 'on_overflow': False}
key = 'groupconcat'
class Hex(Func):
6124class Hex(Func):
6125    pass
key = 'hex'
class LowerHex(Hex):
6128class LowerHex(Hex):
6129    pass
key = 'lowerhex'
class And(Connector, Func):
6132class And(Connector, Func):
6133    pass
key = 'and'
class Or(Connector, Func):
6136class Or(Connector, Func):
6137    pass
key = 'or'
class Xor(Connector, Func):
6140class Xor(Connector, Func):
6141    arg_types = {"this": False, "expression": False, "expressions": False}
arg_types = {'this': False, 'expression': False, 'expressions': False}
key = 'xor'
class If(Func):
6144class If(Func):
6145    arg_types = {"this": True, "true": True, "false": False}
6146    _sql_names = ["IF", "IIF"]
arg_types = {'this': True, 'true': True, 'false': False}
key = 'if'
class Nullif(Func):
6149class Nullif(Func):
6150    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'nullif'
class Initcap(Func):
6153class Initcap(Func):
6154    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'initcap'
class IsAscii(Func):
6157class IsAscii(Func):
6158    pass
key = 'isascii'
class IsNan(Func):
6161class IsNan(Func):
6162    _sql_names = ["IS_NAN", "ISNAN"]
key = 'isnan'
class Int64(Func):
6166class Int64(Func):
6167    pass
key = 'int64'
class IsInf(Func):
6170class IsInf(Func):
6171    _sql_names = ["IS_INF", "ISINF"]
key = 'isinf'
class JSON(Expression):
6175class JSON(Expression):
6176    arg_types = {"this": False, "with": False, "unique": False}
arg_types = {'this': False, 'with': False, 'unique': False}
key = 'json'
class JSONPath(Expression):
6179class JSONPath(Expression):
6180    arg_types = {"expressions": True, "escape": False}
6181
6182    @property
6183    def output_name(self) -> str:
6184        last_segment = self.expressions[-1].this
6185        return last_segment if isinstance(last_segment, str) else ""
arg_types = {'expressions': True, 'escape': False}
output_name: str
6182    @property
6183    def output_name(self) -> str:
6184        last_segment = self.expressions[-1].this
6185        return last_segment if isinstance(last_segment, str) else ""

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'jsonpath'
class JSONPathPart(Expression):
6188class JSONPathPart(Expression):
6189    arg_types = {}
arg_types = {}
key = 'jsonpathpart'
class JSONPathFilter(JSONPathPart):
6192class JSONPathFilter(JSONPathPart):
6193    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathfilter'
class JSONPathKey(JSONPathPart):
6196class JSONPathKey(JSONPathPart):
6197    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathkey'
class JSONPathRecursive(JSONPathPart):
6200class JSONPathRecursive(JSONPathPart):
6201    arg_types = {"this": False}
arg_types = {'this': False}
key = 'jsonpathrecursive'
class JSONPathRoot(JSONPathPart):
6204class JSONPathRoot(JSONPathPart):
6205    pass
key = 'jsonpathroot'
class JSONPathScript(JSONPathPart):
6208class JSONPathScript(JSONPathPart):
6209    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathscript'
class JSONPathSlice(JSONPathPart):
6212class JSONPathSlice(JSONPathPart):
6213    arg_types = {"start": False, "end": False, "step": False}
arg_types = {'start': False, 'end': False, 'step': False}
key = 'jsonpathslice'
class JSONPathSelector(JSONPathPart):
6216class JSONPathSelector(JSONPathPart):
6217    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathselector'
class JSONPathSubscript(JSONPathPart):
6220class JSONPathSubscript(JSONPathPart):
6221    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathsubscript'
class JSONPathUnion(JSONPathPart):
6224class JSONPathUnion(JSONPathPart):
6225    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonpathunion'
class JSONPathWildcard(JSONPathPart):
6228class JSONPathWildcard(JSONPathPart):
6229    pass
key = 'jsonpathwildcard'
class FormatJson(Expression):
6232class FormatJson(Expression):
6233    pass
key = 'formatjson'
class JSONKeyValue(Expression):
6236class JSONKeyValue(Expression):
6237    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'jsonkeyvalue'
class JSONObject(Func):
6240class JSONObject(Func):
6241    arg_types = {
6242        "expressions": False,
6243        "null_handling": False,
6244        "unique_keys": False,
6245        "return_type": False,
6246        "encoding": False,
6247    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobject'
class JSONObjectAgg(AggFunc):
6250class JSONObjectAgg(AggFunc):
6251    arg_types = {
6252        "expressions": False,
6253        "null_handling": False,
6254        "unique_keys": False,
6255        "return_type": False,
6256        "encoding": False,
6257    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobjectagg'
class JSONBObjectAgg(AggFunc):
6261class JSONBObjectAgg(AggFunc):
6262    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'jsonbobjectagg'
class JSONArray(Func):
6266class JSONArray(Func):
6267    arg_types = {
6268        "expressions": True,
6269        "null_handling": False,
6270        "return_type": False,
6271        "strict": False,
6272    }
arg_types = {'expressions': True, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarray'
class JSONArrayAgg(Func):
6276class JSONArrayAgg(Func):
6277    arg_types = {
6278        "this": True,
6279        "order": False,
6280        "null_handling": False,
6281        "return_type": False,
6282        "strict": False,
6283    }
arg_types = {'this': True, 'order': False, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarrayagg'
class JSONExists(Func):
6286class JSONExists(Func):
6287    arg_types = {"this": True, "path": True, "passing": False, "on_condition": False}
arg_types = {'this': True, 'path': True, 'passing': False, 'on_condition': False}
key = 'jsonexists'
class JSONColumnDef(Expression):
6292class JSONColumnDef(Expression):
6293    arg_types = {"this": False, "kind": False, "path": False, "nested_schema": False}
arg_types = {'this': False, 'kind': False, 'path': False, 'nested_schema': False}
key = 'jsoncolumndef'
class JSONSchema(Expression):
6296class JSONSchema(Expression):
6297    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonschema'
class JSONValue(Expression):
6301class JSONValue(Expression):
6302    arg_types = {
6303        "this": True,
6304        "path": True,
6305        "returning": False,
6306        "on_condition": False,
6307    }
arg_types = {'this': True, 'path': True, 'returning': False, 'on_condition': False}
key = 'jsonvalue'
class JSONValueArray(Func):
6310class JSONValueArray(Func):
6311    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'jsonvaluearray'
class JSONTable(Func):
6315class JSONTable(Func):
6316    arg_types = {
6317        "this": True,
6318        "schema": True,
6319        "path": False,
6320        "error_handling": False,
6321        "empty_handling": False,
6322    }
arg_types = {'this': True, 'schema': True, 'path': False, 'error_handling': False, 'empty_handling': False}
key = 'jsontable'
class ObjectInsert(Func):
6326class ObjectInsert(Func):
6327    arg_types = {
6328        "this": True,
6329        "key": True,
6330        "value": True,
6331        "update_flag": False,
6332    }
arg_types = {'this': True, 'key': True, 'value': True, 'update_flag': False}
key = 'objectinsert'
class OpenJSONColumnDef(Expression):
6335class OpenJSONColumnDef(Expression):
6336    arg_types = {"this": True, "kind": True, "path": False, "as_json": False}
arg_types = {'this': True, 'kind': True, 'path': False, 'as_json': False}
key = 'openjsoncolumndef'
class OpenJSON(Func):
6339class OpenJSON(Func):
6340    arg_types = {"this": True, "path": False, "expressions": False}
arg_types = {'this': True, 'path': False, 'expressions': False}
key = 'openjson'
class JSONBContains(Binary, Func):
6343class JSONBContains(Binary, Func):
6344    _sql_names = ["JSONB_CONTAINS"]
key = 'jsonbcontains'
class JSONBExists(Func):
6347class JSONBExists(Func):
6348    arg_types = {"this": True, "path": True}
6349    _sql_names = ["JSONB_EXISTS"]
arg_types = {'this': True, 'path': True}
key = 'jsonbexists'
class JSONExtract(Binary, Func):
6352class JSONExtract(Binary, Func):
6353    arg_types = {
6354        "this": True,
6355        "expression": True,
6356        "only_json_types": False,
6357        "expressions": False,
6358        "variant_extract": False,
6359        "json_query": False,
6360        "option": False,
6361        "quote": False,
6362        "on_condition": False,
6363    }
6364    _sql_names = ["JSON_EXTRACT"]
6365    is_var_len_args = True
6366
6367    @property
6368    def output_name(self) -> str:
6369        return self.expression.output_name if not self.expressions else ""
arg_types = {'this': True, 'expression': True, 'only_json_types': False, 'expressions': False, 'variant_extract': False, 'json_query': False, 'option': False, 'quote': False, 'on_condition': False}
is_var_len_args = True
output_name: str
6367    @property
6368    def output_name(self) -> str:
6369        return self.expression.output_name if not self.expressions else ""

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'jsonextract'
class JSONExtractQuote(Expression):
6373class JSONExtractQuote(Expression):
6374    arg_types = {
6375        "option": True,
6376        "scalar": False,
6377    }
arg_types = {'option': True, 'scalar': False}
key = 'jsonextractquote'
class JSONExtractArray(Func):
6380class JSONExtractArray(Func):
6381    arg_types = {"this": True, "expression": False}
6382    _sql_names = ["JSON_EXTRACT_ARRAY"]
arg_types = {'this': True, 'expression': False}
key = 'jsonextractarray'
class JSONExtractScalar(Binary, Func):
6385class JSONExtractScalar(Binary, Func):
6386    arg_types = {"this": True, "expression": True, "only_json_types": False, "expressions": False}
6387    _sql_names = ["JSON_EXTRACT_SCALAR"]
6388    is_var_len_args = True
6389
6390    @property
6391    def output_name(self) -> str:
6392        return self.expression.output_name
arg_types = {'this': True, 'expression': True, 'only_json_types': False, 'expressions': False}
is_var_len_args = True
output_name: str
6390    @property
6391    def output_name(self) -> str:
6392        return self.expression.output_name

Name of the output column if this expression is a selection.

If the Expression has no output name, an empty string is returned.

Example:
>>> from sqlglot import parse_one
>>> parse_one("SELECT a")sqlglot.expressions[0].output_name
'a'
>>> parse_one("SELECT b AS c")sqlglot.expressions[0].output_name
'c'
>>> parse_one("SELECT 1 + 2")sqlglot.expressions[0].output_name
''
key = 'jsonextractscalar'
class JSONBExtract(Binary, Func):
6395class JSONBExtract(Binary, Func):
6396    _sql_names = ["JSONB_EXTRACT"]
key = 'jsonbextract'
class JSONBExtractScalar(Binary, Func):
6399class JSONBExtractScalar(Binary, Func):
6400    _sql_names = ["JSONB_EXTRACT_SCALAR"]
key = 'jsonbextractscalar'
class JSONFormat(Func):
6403class JSONFormat(Func):
6404    arg_types = {"this": False, "options": False, "is_json": False}
6405    _sql_names = ["JSON_FORMAT"]
arg_types = {'this': False, 'options': False, 'is_json': False}
key = 'jsonformat'
class JSONArrayContains(Binary, Predicate, Func):
6409class JSONArrayContains(Binary, Predicate, Func):
6410    _sql_names = ["JSON_ARRAY_CONTAINS"]
key = 'jsonarraycontains'
class ParseJSON(Func):
6413class ParseJSON(Func):
6414    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
6415    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
6416    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
6417    arg_types = {"this": True, "expression": False, "safe": False}
arg_types = {'this': True, 'expression': False, 'safe': False}
key = 'parsejson'
class Least(Func):
6420class Least(Func):
6421    arg_types = {"this": True, "expressions": False}
6422    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'least'
class Left(Func):
6425class Left(Func):
6426    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'left'
class Length(Func):
6433class Length(Func):
6434    arg_types = {"this": True, "binary": False, "encoding": False}
6435    _sql_names = ["LENGTH", "LEN", "CHAR_LENGTH", "CHARACTER_LENGTH"]
arg_types = {'this': True, 'binary': False, 'encoding': False}
key = 'length'
class Levenshtein(Func):
6438class Levenshtein(Func):
6439    arg_types = {
6440        "this": True,
6441        "expression": False,
6442        "ins_cost": False,
6443        "del_cost": False,
6444        "sub_cost": False,
6445        "max_dist": False,
6446    }
arg_types = {'this': True, 'expression': False, 'ins_cost': False, 'del_cost': False, 'sub_cost': False, 'max_dist': False}
key = 'levenshtein'
class Ln(Func):
6449class Ln(Func):
6450    pass
key = 'ln'
class Log(Func):
6453class Log(Func):
6454    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'log'
class LogicalOr(AggFunc):
6457class LogicalOr(AggFunc):
6458    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
key = 'logicalor'
class LogicalAnd(AggFunc):
6461class LogicalAnd(AggFunc):
6462    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
key = 'logicaland'
class Lower(Func):
6465class Lower(Func):
6466    _sql_names = ["LOWER", "LCASE"]
key = 'lower'
class Map(Func):
6469class Map(Func):
6470    arg_types = {"keys": False, "values": False}
6471
6472    @property
6473    def keys(self) -> t.List[Expression]:
6474        keys = self.args.get("keys")
6475        return keys.expressions if keys else []
6476
6477    @property
6478    def values(self) -> t.List[Expression]:
6479        values = self.args.get("values")
6480        return values.expressions if values else []
arg_types = {'keys': False, 'values': False}
keys: List[Expression]
6472    @property
6473    def keys(self) -> t.List[Expression]:
6474        keys = self.args.get("keys")
6475        return keys.expressions if keys else []
values: List[Expression]
6477    @property
6478    def values(self) -> t.List[Expression]:
6479        values = self.args.get("values")
6480        return values.expressions if values else []
key = 'map'
class ToMap(Func):
6484class ToMap(Func):
6485    pass
key = 'tomap'
class MapFromEntries(Func):
6488class MapFromEntries(Func):
6489    pass
key = 'mapfromentries'
class ScopeResolution(Expression):
6493class ScopeResolution(Expression):
6494    arg_types = {"this": False, "expression": True}
arg_types = {'this': False, 'expression': True}
key = 'scoperesolution'
class Stream(Expression):
6497class Stream(Expression):
6498    pass
key = 'stream'
class StarMap(Func):
6501class StarMap(Func):
6502    pass
key = 'starmap'
class VarMap(Func):
6505class VarMap(Func):
6506    arg_types = {"keys": True, "values": True}
6507    is_var_len_args = True
6508
6509    @property
6510    def keys(self) -> t.List[Expression]:
6511        return self.args["keys"].expressions
6512
6513    @property
6514    def values(self) -> t.List[Expression]:
6515        return self.args["values"].expressions
arg_types = {'keys': True, 'values': True}
is_var_len_args = True
keys: List[Expression]
6509    @property
6510    def keys(self) -> t.List[Expression]:
6511        return self.args["keys"].expressions
values: List[Expression]
6513    @property
6514    def values(self) -> t.List[Expression]:
6515        return self.args["values"].expressions
key = 'varmap'
class MatchAgainst(Func):
6519class MatchAgainst(Func):
6520    arg_types = {"this": True, "expressions": True, "modifier": False}
arg_types = {'this': True, 'expressions': True, 'modifier': False}
key = 'matchagainst'
class Max(AggFunc):
6523class Max(AggFunc):
6524    arg_types = {"this": True, "expressions": False}
6525    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'max'
class MD5(Func):
6528class MD5(Func):
6529    _sql_names = ["MD5"]
key = 'md5'
class MD5Digest(Func):
6533class MD5Digest(Func):
6534    _sql_names = ["MD5_DIGEST"]
key = 'md5digest'
class Median(AggFunc):
6537class Median(AggFunc):
6538    pass
key = 'median'
class Min(AggFunc):
6541class Min(AggFunc):
6542    arg_types = {"this": True, "expressions": False}
6543    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'min'
class Month(Func):
6546class Month(Func):
6547    pass
key = 'month'
class AddMonths(Func):
6550class AddMonths(Func):
6551    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'addmonths'
class Nvl2(Func):
6554class Nvl2(Func):
6555    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'nvl2'
class Normalize(Func):
6558class Normalize(Func):
6559    arg_types = {"this": True, "form": False}
arg_types = {'this': True, 'form': False}
key = 'normalize'
class Overlay(Func):
6562class Overlay(Func):
6563    arg_types = {"this": True, "expression": True, "from": True, "for": False}
arg_types = {'this': True, 'expression': True, 'from': True, 'for': False}
key = 'overlay'
class Predict(Func):
6567class Predict(Func):
6568    arg_types = {"this": True, "expression": True, "params_struct": False}
arg_types = {'this': True, 'expression': True, 'params_struct': False}
key = 'predict'
class Pow(Binary, Func):
6571class Pow(Binary, Func):
6572    _sql_names = ["POWER", "POW"]
key = 'pow'
class PercentileCont(AggFunc):
6575class PercentileCont(AggFunc):
6576    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentilecont'
class PercentileDisc(AggFunc):
6579class PercentileDisc(AggFunc):
6580    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentiledisc'
class Quantile(AggFunc):
6583class Quantile(AggFunc):
6584    arg_types = {"this": True, "quantile": True}
arg_types = {'this': True, 'quantile': True}
key = 'quantile'
class ApproxQuantile(Quantile):
6587class ApproxQuantile(Quantile):
6588    arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
arg_types = {'this': True, 'quantile': True, 'accuracy': False, 'weight': False}
key = 'approxquantile'
class Quarter(Func):
6591class Quarter(Func):
6592    pass
key = 'quarter'
class Rand(Func):
6597class Rand(Func):
6598    _sql_names = ["RAND", "RANDOM"]
6599    arg_types = {"this": False, "lower": False, "upper": False}
arg_types = {'this': False, 'lower': False, 'upper': False}
key = 'rand'
class Randn(Func):
6602class Randn(Func):
6603    arg_types = {"this": False}
arg_types = {'this': False}
key = 'randn'
class RangeN(Func):
6606class RangeN(Func):
6607    arg_types = {"this": True, "expressions": True, "each": False}
arg_types = {'this': True, 'expressions': True, 'each': False}
key = 'rangen'
class ReadCSV(Func):
6610class ReadCSV(Func):
6611    _sql_names = ["READ_CSV"]
6612    is_var_len_args = True
6613    arg_types = {"this": True, "expressions": False}
is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
key = 'readcsv'
class Reduce(Func):
6616class Reduce(Func):
6617    arg_types = {"this": True, "initial": True, "merge": True, "finish": False}
arg_types = {'this': True, 'initial': True, 'merge': True, 'finish': False}
key = 'reduce'
class RegexpExtract(Func):
6620class RegexpExtract(Func):
6621    arg_types = {
6622        "this": True,
6623        "expression": True,
6624        "position": False,
6625        "occurrence": False,
6626        "parameters": False,
6627        "group": False,
6628    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextract'
class RegexpExtractAll(Func):
6631class RegexpExtractAll(Func):
6632    arg_types = {
6633        "this": True,
6634        "expression": True,
6635        "position": False,
6636        "occurrence": False,
6637        "parameters": False,
6638        "group": False,
6639    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextractall'
class RegexpReplace(Func):
6642class RegexpReplace(Func):
6643    arg_types = {
6644        "this": True,
6645        "expression": True,
6646        "replacement": False,
6647        "position": False,
6648        "occurrence": False,
6649        "modifiers": False,
6650    }
arg_types = {'this': True, 'expression': True, 'replacement': False, 'position': False, 'occurrence': False, 'modifiers': False}
key = 'regexpreplace'
class RegexpLike(Binary, Func):
6653class RegexpLike(Binary, Func):
6654    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexplike'
class RegexpILike(Binary, Func):
6657class RegexpILike(Binary, Func):
6658    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexpilike'
class RegexpSplit(Func):
6663class RegexpSplit(Func):
6664    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'regexpsplit'
class Repeat(Func):
6667class Repeat(Func):
6668    arg_types = {"this": True, "times": True}
arg_types = {'this': True, 'times': True}
key = 'repeat'
class Round(Func):
6673class Round(Func):
6674    arg_types = {"this": True, "decimals": False, "truncate": False}
arg_types = {'this': True, 'decimals': False, 'truncate': False}
key = 'round'
class RowNumber(Func):
6677class RowNumber(Func):
6678    arg_types = {"this": False}
arg_types = {'this': False}
key = 'rownumber'
class SafeDivide(Func):
6681class SafeDivide(Func):
6682    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'safedivide'
class SHA(Func):
6685class SHA(Func):
6686    _sql_names = ["SHA", "SHA1"]
key = 'sha'
class SHA2(Func):
6689class SHA2(Func):
6690    _sql_names = ["SHA2"]
6691    arg_types = {"this": True, "length": False}
arg_types = {'this': True, 'length': False}
key = 'sha2'
class Sign(Func):
6694class Sign(Func):
6695    _sql_names = ["SIGN", "SIGNUM"]
key = 'sign'
class SortArray(Func):
6698class SortArray(Func):
6699    arg_types = {"this": True, "asc": False}
arg_types = {'this': True, 'asc': False}
key = 'sortarray'
class Split(Func):
6702class Split(Func):
6703    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'split'
class SplitPart(Func):
6707class SplitPart(Func):
6708    arg_types = {"this": True, "delimiter": True, "part_index": True}
arg_types = {'this': True, 'delimiter': True, 'part_index': True}
key = 'splitpart'
class Substring(Func):
6713class Substring(Func):
6714    _sql_names = ["SUBSTRING", "SUBSTR"]
6715    arg_types = {"this": True, "start": False, "length": False}
arg_types = {'this': True, 'start': False, 'length': False}
key = 'substring'
class StandardHash(Func):
6718class StandardHash(Func):
6719    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'standardhash'
class StartsWith(Func):
6722class StartsWith(Func):
6723    _sql_names = ["STARTS_WITH", "STARTSWITH"]
6724    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'startswith'
class EndsWith(Func):
6727class EndsWith(Func):
6728    _sql_names = ["ENDS_WITH", "ENDSWITH"]
6729    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'endswith'
class StrPosition(Func):
6732class StrPosition(Func):
6733    arg_types = {
6734        "this": True,
6735        "substr": True,
6736        "position": False,
6737        "occurrence": False,
6738    }
arg_types = {'this': True, 'substr': True, 'position': False, 'occurrence': False}
key = 'strposition'
class StrToDate(Func):
6741class StrToDate(Func):
6742    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'strtodate'
class StrToTime(Func):
6745class StrToTime(Func):
6746    arg_types = {"this": True, "format": True, "zone": False, "safe": False}
arg_types = {'this': True, 'format': True, 'zone': False, 'safe': False}
key = 'strtotime'
class StrToUnix(Func):
6751class StrToUnix(Func):
6752    arg_types = {"this": False, "format": False}
arg_types = {'this': False, 'format': False}
key = 'strtounix'
class StrToMap(Func):
6757class StrToMap(Func):
6758    arg_types = {
6759        "this": True,
6760        "pair_delim": False,
6761        "key_value_delim": False,
6762        "duplicate_resolution_callback": False,
6763    }
arg_types = {'this': True, 'pair_delim': False, 'key_value_delim': False, 'duplicate_resolution_callback': False}
key = 'strtomap'
class NumberToStr(Func):
6766class NumberToStr(Func):
6767    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'numbertostr'
class FromBase(Func):
6770class FromBase(Func):
6771    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'frombase'
class Struct(Func):
6774class Struct(Func):
6775    arg_types = {"expressions": False}
6776    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'struct'
class StructExtract(Func):
6779class StructExtract(Func):
6780    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'structextract'
class Stuff(Func):
6785class Stuff(Func):
6786    _sql_names = ["STUFF", "INSERT"]
6787    arg_types = {"this": True, "start": True, "length": True, "expression": True}
arg_types = {'this': True, 'start': True, 'length': True, 'expression': True}
key = 'stuff'
class Sum(AggFunc):
6790class Sum(AggFunc):
6791    pass
key = 'sum'
class Sqrt(Func):
6794class Sqrt(Func):
6795    pass
key = 'sqrt'
class Stddev(AggFunc):
6798class Stddev(AggFunc):
6799    _sql_names = ["STDDEV", "STDEV"]
key = 'stddev'
class StddevPop(AggFunc):
6802class StddevPop(AggFunc):
6803    pass
key = 'stddevpop'
class StddevSamp(AggFunc):
6806class StddevSamp(AggFunc):
6807    pass
key = 'stddevsamp'
class Time(Func):
6811class Time(Func):
6812    arg_types = {"this": False, "zone": False}
arg_types = {'this': False, 'zone': False}
key = 'time'
class TimeToStr(Func):
6815class TimeToStr(Func):
6816    arg_types = {"this": True, "format": True, "culture": False, "zone": False}
arg_types = {'this': True, 'format': True, 'culture': False, 'zone': False}
key = 'timetostr'
class TimeToTimeStr(Func):
6819class TimeToTimeStr(Func):
6820    pass
key = 'timetotimestr'
class TimeToUnix(Func):
6823class TimeToUnix(Func):
6824    pass
key = 'timetounix'
class TimeStrToDate(Func):
6827class TimeStrToDate(Func):
6828    pass
key = 'timestrtodate'
class TimeStrToTime(Func):
6831class TimeStrToTime(Func):
6832    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key = 'timestrtotime'
class TimeStrToUnix(Func):
6835class TimeStrToUnix(Func):
6836    pass
key = 'timestrtounix'
class Trim(Func):
6839class Trim(Func):
6840    arg_types = {
6841        "this": True,
6842        "expression": False,
6843        "position": False,
6844        "collation": False,
6845    }
arg_types = {'this': True, 'expression': False, 'position': False, 'collation': False}
key = 'trim'
class TsOrDsAdd(Func, TimeUnit):
6848class TsOrDsAdd(Func, TimeUnit):
6849    # return_type is used to correctly cast the arguments of this expression when transpiling it
6850    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
6851
6852    @property
6853    def return_type(self) -> DataType:
6854        return DataType.build(self.args.get("return_type") or DataType.Type.DATE)
arg_types = {'this': True, 'expression': True, 'unit': False, 'return_type': False}
return_type: DataType
6852    @property
6853    def return_type(self) -> DataType:
6854        return DataType.build(self.args.get("return_type") or DataType.Type.DATE)
key = 'tsordsadd'
class TsOrDsDiff(Func, TimeUnit):
6857class TsOrDsDiff(Func, TimeUnit):
6858    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'tsordsdiff'
class TsOrDsToDateStr(Func):
6861class TsOrDsToDateStr(Func):
6862    pass
key = 'tsordstodatestr'
class TsOrDsToDate(Func):
6865class TsOrDsToDate(Func):
6866    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'tsordstodate'
class TsOrDsToDatetime(Func):
6869class TsOrDsToDatetime(Func):
6870    pass
key = 'tsordstodatetime'
class TsOrDsToTime(Func):
6873class TsOrDsToTime(Func):
6874    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'tsordstotime'
class TsOrDsToTimestamp(Func):
6877class TsOrDsToTimestamp(Func):
6878    pass
key = 'tsordstotimestamp'
class TsOrDiToDi(Func):
6881class TsOrDiToDi(Func):
6882    pass
key = 'tsorditodi'
class Unhex(Func):
6885class Unhex(Func):
6886    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'unhex'
class Unicode(Func):
6889class Unicode(Func):
6890    pass
key = 'unicode'
class UnixDate(Func):
6894class UnixDate(Func):
6895    pass
key = 'unixdate'
class UnixToStr(Func):
6898class UnixToStr(Func):
6899    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'unixtostr'
class UnixToTime(Func):
6904class UnixToTime(Func):
6905    arg_types = {
6906        "this": True,
6907        "scale": False,
6908        "zone": False,
6909        "hours": False,
6910        "minutes": False,
6911        "format": False,
6912    }
6913
6914    SECONDS = Literal.number(0)
6915    DECIS = Literal.number(1)
6916    CENTIS = Literal.number(2)
6917    MILLIS = Literal.number(3)
6918    DECIMILLIS = Literal.number(4)
6919    CENTIMILLIS = Literal.number(5)
6920    MICROS = Literal.number(6)
6921    DECIMICROS = Literal.number(7)
6922    CENTIMICROS = Literal.number(8)
6923    NANOS = Literal.number(9)
arg_types = {'this': True, 'scale': False, 'zone': False, 'hours': False, 'minutes': False, 'format': False}
SECONDS = Literal(this=0, is_string=False)
DECIS = Literal(this=1, is_string=False)
CENTIS = Literal(this=2, is_string=False)
MILLIS = Literal(this=3, is_string=False)
DECIMILLIS = Literal(this=4, is_string=False)
CENTIMILLIS = Literal(this=5, is_string=False)
MICROS = Literal(this=6, is_string=False)
DECIMICROS = Literal(this=7, is_string=False)
CENTIMICROS = Literal(this=8, is_string=False)
NANOS = Literal(this=9, is_string=False)
key = 'unixtotime'
class UnixToTimeStr(Func):
6926class UnixToTimeStr(Func):
6927    pass
key = 'unixtotimestr'
class UnixSeconds(Func):
6930class UnixSeconds(Func):
6931    pass
key = 'unixseconds'
class Uuid(Func):
6934class Uuid(Func):
6935    _sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "UUID_STRING"]
6936
6937    arg_types = {"this": False, "name": False}
arg_types = {'this': False, 'name': False}
key = 'uuid'
class TimestampFromParts(Func):
6940class TimestampFromParts(Func):
6941    _sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
6942    arg_types = {
6943        "year": True,
6944        "month": True,
6945        "day": True,
6946        "hour": True,
6947        "min": True,
6948        "sec": True,
6949        "nano": False,
6950        "zone": False,
6951        "milli": False,
6952    }
arg_types = {'year': True, 'month': True, 'day': True, 'hour': True, 'min': True, 'sec': True, 'nano': False, 'zone': False, 'milli': False}
key = 'timestampfromparts'
class Upper(Func):
6955class Upper(Func):
6956    _sql_names = ["UPPER", "UCASE"]
key = 'upper'
class Corr(Binary, AggFunc):
6959class Corr(Binary, AggFunc):
6960    pass
key = 'corr'
class Variance(AggFunc):
6963class Variance(AggFunc):
6964    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
key = 'variance'
class VariancePop(AggFunc):
6967class VariancePop(AggFunc):
6968    _sql_names = ["VARIANCE_POP", "VAR_POP"]
key = 'variancepop'
class CovarSamp(Binary, AggFunc):
6971class CovarSamp(Binary, AggFunc):
6972    pass
key = 'covarsamp'
class CovarPop(Binary, AggFunc):
6975class CovarPop(Binary, AggFunc):
6976    pass
key = 'covarpop'
class Week(Func):
6979class Week(Func):
6980    arg_types = {"this": True, "mode": False}
arg_types = {'this': True, 'mode': False}
key = 'week'
class XMLElement(Func):
6983class XMLElement(Func):
6984    _sql_names = ["XMLELEMENT"]
6985    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'xmlelement'
class XMLTable(Func):
6988class XMLTable(Func):
6989    arg_types = {
6990        "this": True,
6991        "namespaces": False,
6992        "passing": False,
6993        "columns": False,
6994        "by_ref": False,
6995    }
arg_types = {'this': True, 'namespaces': False, 'passing': False, 'columns': False, 'by_ref': False}
key = 'xmltable'
class XMLNamespace(Expression):
6998class XMLNamespace(Expression):
6999    pass
key = 'xmlnamespace'
class XMLKeyValueOption(Expression):
7003class XMLKeyValueOption(Expression):
7004    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'xmlkeyvalueoption'
class Year(Func):
7007class Year(Func):
7008    pass
key = 'year'
class Use(Expression):
7011class Use(Expression):
7012    arg_types = {"this": False, "expressions": False, "kind": False}
arg_types = {'this': False, 'expressions': False, 'kind': False}
key = 'use'
class Merge(DML):
7015class Merge(DML):
7016    arg_types = {
7017        "this": True,
7018        "using": True,
7019        "on": True,
7020        "whens": True,
7021        "with": False,
7022        "returning": False,
7023    }
arg_types = {'this': True, 'using': True, 'on': True, 'whens': True, 'with': False, 'returning': False}
key = 'merge'
class When(Expression):
7026class When(Expression):
7027    arg_types = {"matched": True, "source": False, "condition": False, "then": True}
arg_types = {'matched': True, 'source': False, 'condition': False, 'then': True}
key = 'when'
class Whens(Expression):
7030class Whens(Expression):
7031    """Wraps around one or more WHEN [NOT] MATCHED [...] clauses."""
7032
7033    arg_types = {"expressions": True}

Wraps around one or more WHEN [NOT] MATCHED [...] clauses.

arg_types = {'expressions': True}
key = 'whens'
class NextValueFor(Func):
7038class NextValueFor(Func):
7039    arg_types = {"this": True, "order": False}
arg_types = {'this': True, 'order': False}
key = 'nextvaluefor'
class Semicolon(Expression):
7044class Semicolon(Expression):
7045    arg_types = {}
arg_types = {}
key = 'semicolon'
ALL_FUNCTIONS = [<class 'Abs'>, <class 'AddMonths'>, <class 'And'>, <class 'AnonymousAggFunc'>, <class 'AnyValue'>, <class 'Apply'>, <class 'ApproxDistinct'>, <class 'ApproxQuantile'>, <class 'ApproxTopK'>, <class 'ArgMax'>, <class 'ArgMin'>, <class 'Array'>, <class 'ArrayAgg'>, <class 'ArrayAll'>, <class 'ArrayAny'>, <class 'ArrayConcat'>, <class 'ArrayConcatAgg'>, <class 'ArrayConstructCompact'>, <class 'ArrayContains'>, <class 'ArrayContainsAll'>, <class 'ArrayFilter'>, <class 'ArrayIntersect'>, <class 'ArrayOverlaps'>, <class 'ArrayRemove'>, <class 'ArraySize'>, <class 'ArraySort'>, <class 'ArraySum'>, <class 'ArrayToString'>, <class 'ArrayUnionAgg'>, <class 'ArrayUniqueAgg'>, <class 'Avg'>, <class 'Case'>, <class 'Cast'>, <class 'CastToStrType'>, <class 'Cbrt'>, <class 'Ceil'>, <class 'Chr'>, <class 'Coalesce'>, <class 'Collate'>, <class 'Columns'>, <class 'CombinedAggFunc'>, <class 'CombinedParameterizedAgg'>, <class 'Concat'>, <class 'ConcatWs'>, <class 'ConnectByRoot'>, <class 'Contains'>, <class 'Convert'>, <class 'ConvertTimezone'>, <class 'ConvertToCharset'>, <class 'Corr'>, <class 'Count'>, <class 'CountIf'>, <class 'CovarPop'>, <class 'CovarSamp'>, <class 'CurrentDate'>, <class 'CurrentDatetime'>, <class 'CurrentSchema'>, <class 'CurrentTime'>, <class 'CurrentTimestamp'>, <class 'CurrentUser'>, <class 'Date'>, <class 'DateAdd'>, <class 'DateBin'>, <class 'DateDiff'>, <class 'DateFromParts'>, <class 'DateStrToDate'>, <class 'DateSub'>, <class 'DateToDateStr'>, <class 'DateToDi'>, <class 'DateTrunc'>, <class 'Datetime'>, <class 'DatetimeAdd'>, <class 'DatetimeDiff'>, <class 'DatetimeSub'>, <class 'DatetimeTrunc'>, <class 'Day'>, <class 'DayOfMonth'>, <class 'DayOfWeek'>, <class 'DayOfWeekIso'>, <class 'DayOfYear'>, <class 'Decode'>, <class 'DiToDate'>, <class 'Encode'>, <class 'EndsWith'>, <class 'Exists'>, <class 'Exp'>, <class 'Explode'>, <class 'ExplodeOuter'>, <class 'ExplodingGenerateSeries'>, <class 'Extract'>, <class 'FeaturesAtTime'>, <class 'First'>, <class 'FirstValue'>, <class 'Flatten'>, <class 'Floor'>, <class 'FromBase'>, <class 'FromBase64'>, <class 'FromISO8601Timestamp'>, <class 'GapFill'>, <class 'GenerateDateArray'>, <class 'GenerateSeries'>, <class 'GenerateTimestampArray'>, <class 'Greatest'>, <class 'GroupConcat'>, <class 'Hex'>, <class 'Hll'>, <class 'If'>, <class 'Initcap'>, <class 'Inline'>, <class 'Int64'>, <class 'IsAscii'>, <class 'IsInf'>, <class 'IsNan'>, <class 'JSONArray'>, <class 'JSONArrayAgg'>, <class 'JSONArrayContains'>, <class 'JSONBContains'>, <class 'JSONBExists'>, <class 'JSONBExtract'>, <class 'JSONBExtractScalar'>, <class 'JSONBObjectAgg'>, <class 'JSONCast'>, <class 'JSONExists'>, <class 'JSONExtract'>, <class 'JSONExtractArray'>, <class 'JSONExtractScalar'>, <class 'JSONFormat'>, <class 'JSONObject'>, <class 'JSONObjectAgg'>, <class 'JSONTable'>, <class 'JSONValueArray'>, <class 'Lag'>, <class 'Last'>, <class 'LastDay'>, <class 'LastValue'>, <class 'Lead'>, <class 'Least'>, <class 'Left'>, <class 'Length'>, <class 'Levenshtein'>, <class 'List'>, <class 'Ln'>, <class 'Log'>, <class 'LogicalAnd'>, <class 'LogicalOr'>, <class 'Lower'>, <class 'LowerHex'>, <class 'MD5'>, <class 'MD5Digest'>, <class 'MakeInterval'>, <class 'Map'>, <class 'MapFromEntries'>, <class 'MatchAgainst'>, <class 'Max'>, <class 'Median'>, <class 'Min'>, <class 'Month'>, <class 'MonthsBetween'>, <class 'NextValueFor'>, <class 'Normalize'>, <class 'NthValue'>, <class 'Nullif'>, <class 'NumberToStr'>, <class 'Nvl2'>, <class 'ObjectInsert'>, <class 'OpenJSON'>, <class 'Or'>, <class 'Overlay'>, <class 'Pad'>, <class 'ParameterizedAgg'>, <class 'ParseJSON'>, <class 'PercentileCont'>, <class 'PercentileDisc'>, <class 'Posexplode'>, <class 'PosexplodeOuter'>, <class 'Pow'>, <class 'Predict'>, <class 'Quantile'>, <class 'Quarter'>, <class 'Rand'>, <class 'Randn'>, <class 'RangeN'>, <class 'ReadCSV'>, <class 'Reduce'>, <class 'RegexpExtract'>, <class 'RegexpExtractAll'>, <class 'RegexpILike'>, <class 'RegexpLike'>, <class 'RegexpReplace'>, <class 'RegexpSplit'>, <class 'Repeat'>, <class 'Right'>, <class 'Round'>, <class 'RowNumber'>, <class 'SHA'>, <class 'SHA2'>, <class 'SafeDivide'>, <class 'Sign'>, <class 'SortArray'>, <class 'Split'>, <class 'SplitPart'>, <class 'Sqrt'>, <class 'StDistance'>, <class 'StPoint'>, <class 'StandardHash'>, <class 'StarMap'>, <class 'StartsWith'>, <class 'Stddev'>, <class 'StddevPop'>, <class 'StddevSamp'>, <class 'StrPosition'>, <class 'StrToDate'>, <class 'StrToMap'>, <class 'StrToTime'>, <class 'StrToUnix'>, <class 'String'>, <class 'StringToArray'>, <class 'Struct'>, <class 'StructExtract'>, <class 'Stuff'>, <class 'Substring'>, <class 'Sum'>, <class 'Time'>, <class 'TimeAdd'>, <class 'TimeDiff'>, <class 'TimeFromParts'>, <class 'TimeStrToDate'>, <class 'TimeStrToTime'>, <class 'TimeStrToUnix'>, <class 'TimeSub'>, <class 'TimeToStr'>, <class 'TimeToTimeStr'>, <class 'TimeToUnix'>, <class 'TimeTrunc'>, <class 'Timestamp'>, <class 'TimestampAdd'>, <class 'TimestampDiff'>, <class 'TimestampFromParts'>, <class 'TimestampSub'>, <class 'TimestampTrunc'>, <class 'ToArray'>, <class 'ToBase64'>, <class 'ToChar'>, <class 'ToDays'>, <class 'ToDouble'>, <class 'ToMap'>, <class 'ToNumber'>, <class 'Transform'>, <class 'Trim'>, <class 'Try'>, <class 'TryCast'>, <class 'TsOrDiToDi'>, <class 'TsOrDsAdd'>, <class 'TsOrDsDiff'>, <class 'TsOrDsToDate'>, <class 'TsOrDsToDateStr'>, <class 'TsOrDsToDatetime'>, <class 'TsOrDsToTime'>, <class 'TsOrDsToTimestamp'>, <class 'Unhex'>, <class 'Unicode'>, <class 'UnixDate'>, <class 'UnixSeconds'>, <class 'UnixToStr'>, <class 'UnixToTime'>, <class 'UnixToTimeStr'>, <class 'Unnest'>, <class 'Upper'>, <class 'Uuid'>, <class 'VarMap'>, <class 'Variance'>, <class 'VariancePop'>, <class 'Week'>, <class 'WeekOfYear'>, <class 'XMLElement'>, <class 'XMLTable'>, <class 'Xor'>, <class 'Year'>]
FUNCTION_BY_NAME = {'ABS': <class 'Abs'>, 'ADD_MONTHS': <class 'AddMonths'>, 'AND': <class 'And'>, 'ANONYMOUS_AGG_FUNC': <class 'AnonymousAggFunc'>, 'ANY_VALUE': <class 'AnyValue'>, 'APPLY': <class 'Apply'>, 'APPROX_DISTINCT': <class 'ApproxDistinct'>, 'APPROX_COUNT_DISTINCT': <class 'ApproxDistinct'>, 'APPROX_QUANTILE': <class 'ApproxQuantile'>, 'APPROX_TOP_K': <class 'ApproxTopK'>, 'ARG_MAX': <class 'ArgMax'>, 'ARGMAX': <class 'ArgMax'>, 'MAX_BY': <class 'ArgMax'>, 'ARG_MIN': <class 'ArgMin'>, 'ARGMIN': <class 'ArgMin'>, 'MIN_BY': <class 'ArgMin'>, 'ARRAY': <class 'Array'>, 'ARRAY_AGG': <class 'ArrayAgg'>, 'ARRAY_ALL': <class 'ArrayAll'>, 'ARRAY_ANY': <class 'ArrayAny'>, 'ARRAY_CONCAT': <class 'ArrayConcat'>, 'ARRAY_CAT': <class 'ArrayConcat'>, 'ARRAY_CONCAT_AGG': <class 'ArrayConcatAgg'>, 'ARRAY_CONSTRUCT_COMPACT': <class 'ArrayConstructCompact'>, 'ARRAY_CONTAINS': <class 'ArrayContains'>, 'ARRAY_HAS': <class 'ArrayContains'>, 'ARRAY_CONTAINS_ALL': <class 'ArrayContainsAll'>, 'ARRAY_HAS_ALL': <class 'ArrayContainsAll'>, 'FILTER': <class 'ArrayFilter'>, 'ARRAY_FILTER': <class 'ArrayFilter'>, 'ARRAY_INTERSECT': <class 'ArrayIntersect'>, 'ARRAY_INTERSECTION': <class 'ArrayIntersect'>, 'ARRAY_OVERLAPS': <class 'ArrayOverlaps'>, 'ARRAY_REMOVE': <class 'ArrayRemove'>, 'ARRAY_SIZE': <class 'ArraySize'>, 'ARRAY_LENGTH': <class 'ArraySize'>, 'ARRAY_SORT': <class 'ArraySort'>, 'ARRAY_SUM': <class 'ArraySum'>, 'ARRAY_TO_STRING': <class 'ArrayToString'>, 'ARRAY_JOIN': <class 'ArrayToString'>, 'ARRAY_UNION_AGG': <class 'ArrayUnionAgg'>, 'ARRAY_UNIQUE_AGG': <class 'ArrayUniqueAgg'>, 'AVG': <class 'Avg'>, 'CASE': <class 'Case'>, 'CAST': <class 'Cast'>, 'CAST_TO_STR_TYPE': <class 'CastToStrType'>, 'CBRT': <class 'Cbrt'>, 'CEIL': <class 'Ceil'>, 'CEILING': <class 'Ceil'>, 'CHR': <class 'Chr'>, 'CHAR': <class 'Chr'>, 'COALESCE': <class 'Coalesce'>, 'IFNULL': <class 'Coalesce'>, 'NVL': <class 'Coalesce'>, 'COLLATE': <class 'Collate'>, 'COLUMNS': <class 'Columns'>, 'COMBINED_AGG_FUNC': <class 'CombinedAggFunc'>, 'COMBINED_PARAMETERIZED_AGG': <class 'CombinedParameterizedAgg'>, 'CONCAT': <class 'Concat'>, 'CONCAT_WS': <class 'ConcatWs'>, 'CONNECT_BY_ROOT': <class 'ConnectByRoot'>, 'CONTAINS': <class 'Contains'>, 'CONVERT': <class 'Convert'>, 'CONVERT_TIMEZONE': <class 'ConvertTimezone'>, 'CONVERT_TO_CHARSET': <class 'ConvertToCharset'>, 'CORR': <class 'Corr'>, 'COUNT': <class 'Count'>, 'COUNT_IF': <class 'CountIf'>, 'COUNTIF': <class 'CountIf'>, 'COVAR_POP': <class 'CovarPop'>, 'COVAR_SAMP': <class 'CovarSamp'>, 'CURRENT_DATE': <class 'CurrentDate'>, 'CURRENT_DATETIME': <class 'CurrentDatetime'>, 'CURRENT_SCHEMA': <class 'CurrentSchema'>, 'CURRENT_TIME': <class 'CurrentTime'>, 'CURRENT_TIMESTAMP': <class 'CurrentTimestamp'>, 'CURRENT_USER': <class 'CurrentUser'>, 'DATE': <class 'Date'>, 'DATE_ADD': <class 'DateAdd'>, 'DATE_BIN': <class 'DateBin'>, 'DATEDIFF': <class 'DateDiff'>, 'DATE_DIFF': <class 'DateDiff'>, 'DATE_FROM_PARTS': <class 'DateFromParts'>, 'DATEFROMPARTS': <class 'DateFromParts'>, 'DATE_STR_TO_DATE': <class 'DateStrToDate'>, 'DATE_SUB': <class 'DateSub'>, 'DATE_TO_DATE_STR': <class 'DateToDateStr'>, 'DATE_TO_DI': <class 'DateToDi'>, 'DATE_TRUNC': <class 'DateTrunc'>, 'DATETIME': <class 'Datetime'>, 'DATETIME_ADD': <class 'DatetimeAdd'>, 'DATETIME_DIFF': <class 'DatetimeDiff'>, 'DATETIME_SUB': <class 'DatetimeSub'>, 'DATETIME_TRUNC': <class 'DatetimeTrunc'>, 'DAY': <class 'Day'>, 'DAY_OF_MONTH': <class 'DayOfMonth'>, 'DAYOFMONTH': <class 'DayOfMonth'>, 'DAY_OF_WEEK': <class 'DayOfWeek'>, 'DAYOFWEEK': <class 'DayOfWeek'>, 'DAYOFWEEK_ISO': <class 'DayOfWeekIso'>, 'ISODOW': <class 'DayOfWeekIso'>, 'DAY_OF_YEAR': <class 'DayOfYear'>, 'DAYOFYEAR': <class 'DayOfYear'>, 'DECODE': <class 'Decode'>, 'DI_TO_DATE': <class 'DiToDate'>, 'ENCODE': <class 'Encode'>, 'ENDS_WITH': <class 'EndsWith'>, 'ENDSWITH': <class 'EndsWith'>, 'EXISTS': <class 'Exists'>, 'EXP': <class 'Exp'>, 'EXPLODE': <class 'Explode'>, 'EXPLODE_OUTER': <class 'ExplodeOuter'>, 'EXPLODING_GENERATE_SERIES': <class 'ExplodingGenerateSeries'>, 'EXTRACT': <class 'Extract'>, 'FEATURES_AT_TIME': <class 'FeaturesAtTime'>, 'FIRST': <class 'First'>, 'FIRST_VALUE': <class 'FirstValue'>, 'FLATTEN': <class 'Flatten'>, 'FLOOR': <class 'Floor'>, 'FROM_BASE': <class 'FromBase'>, 'FROM_BASE64': <class 'FromBase64'>, 'FROM_ISO8601_TIMESTAMP': <class 'FromISO8601Timestamp'>, 'GAP_FILL': <class 'GapFill'>, 'GENERATE_DATE_ARRAY': <class 'GenerateDateArray'>, 'GENERATE_SERIES': <class 'GenerateSeries'>, 'GENERATE_TIMESTAMP_ARRAY': <class 'GenerateTimestampArray'>, 'GREATEST': <class 'Greatest'>, 'GROUP_CONCAT': <class 'GroupConcat'>, 'HEX': <class 'Hex'>, 'HLL': <class 'Hll'>, 'IF': <class 'If'>, 'IIF': <class 'If'>, 'INITCAP': <class 'Initcap'>, 'INLINE': <class 'Inline'>, 'INT64': <class 'Int64'>, 'IS_ASCII': <class 'IsAscii'>, 'IS_INF': <class 'IsInf'>, 'ISINF': <class 'IsInf'>, 'IS_NAN': <class 'IsNan'>, 'ISNAN': <class 'IsNan'>, 'J_S_O_N_ARRAY': <class 'JSONArray'>, 'J_S_O_N_ARRAY_AGG': <class 'JSONArrayAgg'>, 'JSON_ARRAY_CONTAINS': <class 'JSONArrayContains'>, 'JSONB_CONTAINS': <class 'JSONBContains'>, 'JSONB_EXISTS': <class 'JSONBExists'>, 'JSONB_EXTRACT': <class 'JSONBExtract'>, 'JSONB_EXTRACT_SCALAR': <class 'JSONBExtractScalar'>, 'J_S_O_N_B_OBJECT_AGG': <class 'JSONBObjectAgg'>, 'J_S_O_N_CAST': <class 'JSONCast'>, 'J_S_O_N_EXISTS': <class 'JSONExists'>, 'JSON_EXTRACT': <class 'JSONExtract'>, 'JSON_EXTRACT_ARRAY': <class 'JSONExtractArray'>, 'JSON_EXTRACT_SCALAR': <class 'JSONExtractScalar'>, 'JSON_FORMAT': <class 'JSONFormat'>, 'J_S_O_N_OBJECT': <class 'JSONObject'>, 'J_S_O_N_OBJECT_AGG': <class 'JSONObjectAgg'>, 'J_S_O_N_TABLE': <class 'JSONTable'>, 'J_S_O_N_VALUE_ARRAY': <class 'JSONValueArray'>, 'LAG': <class 'Lag'>, 'LAST': <class 'Last'>, 'LAST_DAY': <class 'LastDay'>, 'LAST_DAY_OF_MONTH': <class 'LastDay'>, 'LAST_VALUE': <class 'LastValue'>, 'LEAD': <class 'Lead'>, 'LEAST': <class 'Least'>, 'LEFT': <class 'Left'>, 'LENGTH': <class 'Length'>, 'LEN': <class 'Length'>, 'CHAR_LENGTH': <class 'Length'>, 'CHARACTER_LENGTH': <class 'Length'>, 'LEVENSHTEIN': <class 'Levenshtein'>, 'LIST': <class 'List'>, 'LN': <class 'Ln'>, 'LOG': <class 'Log'>, 'LOGICAL_AND': <class 'LogicalAnd'>, 'BOOL_AND': <class 'LogicalAnd'>, 'BOOLAND_AGG': <class 'LogicalAnd'>, 'LOGICAL_OR': <class 'LogicalOr'>, 'BOOL_OR': <class 'LogicalOr'>, 'BOOLOR_AGG': <class 'LogicalOr'>, 'LOWER': <class 'Lower'>, 'LCASE': <class 'Lower'>, 'LOWER_HEX': <class 'LowerHex'>, 'MD5': <class 'MD5'>, 'MD5_DIGEST': <class 'MD5Digest'>, 'MAKE_INTERVAL': <class 'MakeInterval'>, 'MAP': <class 'Map'>, 'MAP_FROM_ENTRIES': <class 'MapFromEntries'>, 'MATCH_AGAINST': <class 'MatchAgainst'>, 'MAX': <class 'Max'>, 'MEDIAN': <class 'Median'>, 'MIN': <class 'Min'>, 'MONTH': <class 'Month'>, 'MONTHS_BETWEEN': <class 'MonthsBetween'>, 'NEXT_VALUE_FOR': <class 'NextValueFor'>, 'NORMALIZE': <class 'Normalize'>, 'NTH_VALUE': <class 'NthValue'>, 'NULLIF': <class 'Nullif'>, 'NUMBER_TO_STR': <class 'NumberToStr'>, 'NVL2': <class 'Nvl2'>, 'OBJECT_INSERT': <class 'ObjectInsert'>, 'OPEN_J_S_O_N': <class 'OpenJSON'>, 'OR': <class 'Or'>, 'OVERLAY': <class 'Overlay'>, 'PAD': <class 'Pad'>, 'PARAMETERIZED_AGG': <class 'ParameterizedAgg'>, 'PARSE_JSON': <class 'ParseJSON'>, 'JSON_PARSE': <class 'ParseJSON'>, 'PERCENTILE_CONT': <class 'PercentileCont'>, 'PERCENTILE_DISC': <class 'PercentileDisc'>, 'POSEXPLODE': <class 'Posexplode'>, 'POSEXPLODE_OUTER': <class 'PosexplodeOuter'>, 'POWER': <class 'Pow'>, 'POW': <class 'Pow'>, 'PREDICT': <class 'Predict'>, 'QUANTILE': <class 'Quantile'>, 'QUARTER': <class 'Quarter'>, 'RAND': <class 'Rand'>, 'RANDOM': <class 'Rand'>, 'RANDN': <class 'Randn'>, 'RANGE_N': <class 'RangeN'>, 'READ_CSV': <class 'ReadCSV'>, 'REDUCE': <class 'Reduce'>, 'REGEXP_EXTRACT': <class 'RegexpExtract'>, 'REGEXP_EXTRACT_ALL': <class 'RegexpExtractAll'>, 'REGEXP_I_LIKE': <class 'RegexpILike'>, 'REGEXP_LIKE': <class 'RegexpLike'>, 'REGEXP_REPLACE': <class 'RegexpReplace'>, 'REGEXP_SPLIT': <class 'RegexpSplit'>, 'REPEAT': <class 'Repeat'>, 'RIGHT': <class 'Right'>, 'ROUND': <class 'Round'>, 'ROW_NUMBER': <class 'RowNumber'>, 'SHA': <class 'SHA'>, 'SHA1': <class 'SHA'>, 'SHA2': <class 'SHA2'>, 'SAFE_DIVIDE': <class 'SafeDivide'>, 'SIGN': <class 'Sign'>, 'SIGNUM': <class 'Sign'>, 'SORT_ARRAY': <class 'SortArray'>, 'SPLIT': <class 'Split'>, 'SPLIT_PART': <class 'SplitPart'>, 'SQRT': <class 'Sqrt'>, 'ST_DISTANCE': <class 'StDistance'>, 'ST_POINT': <class 'StPoint'>, 'ST_MAKEPOINT': <class 'StPoint'>, 'STANDARD_HASH': <class 'StandardHash'>, 'STAR_MAP': <class 'StarMap'>, 'STARTS_WITH': <class 'StartsWith'>, 'STARTSWITH': <class 'StartsWith'>, 'STDDEV': <class 'Stddev'>, 'STDEV': <class 'Stddev'>, 'STDDEV_POP': <class 'StddevPop'>, 'STDDEV_SAMP': <class 'StddevSamp'>, 'STR_POSITION': <class 'StrPosition'>, 'STR_TO_DATE': <class 'StrToDate'>, 'STR_TO_MAP': <class 'StrToMap'>, 'STR_TO_TIME': <class 'StrToTime'>, 'STR_TO_UNIX': <class 'StrToUnix'>, 'STRING': <class 'String'>, 'STRING_TO_ARRAY': <class 'StringToArray'>, 'SPLIT_BY_STRING': <class 'StringToArray'>, 'STRTOK_TO_ARRAY': <class 'StringToArray'>, 'STRUCT': <class 'Struct'>, 'STRUCT_EXTRACT': <class 'StructExtract'>, 'STUFF': <class 'Stuff'>, 'INSERT': <class 'Stuff'>, 'SUBSTRING': <class 'Substring'>, 'SUBSTR': <class 'Substring'>, 'SUM': <class 'Sum'>, 'TIME': <class 'Time'>, 'TIME_ADD': <class 'TimeAdd'>, 'TIME_DIFF': <class 'TimeDiff'>, 'TIME_FROM_PARTS': <class 'TimeFromParts'>, 'TIMEFROMPARTS': <class 'TimeFromParts'>, 'TIME_STR_TO_DATE': <class 'TimeStrToDate'>, 'TIME_STR_TO_TIME': <class 'TimeStrToTime'>, 'TIME_STR_TO_UNIX': <class 'TimeStrToUnix'>, 'TIME_SUB': <class 'TimeSub'>, 'TIME_TO_STR': <class 'TimeToStr'>, 'TIME_TO_TIME_STR': <class 'TimeToTimeStr'>, 'TIME_TO_UNIX': <class 'TimeToUnix'>, 'TIME_TRUNC': <class 'TimeTrunc'>, 'TIMESTAMP': <class 'Timestamp'>, 'TIMESTAMP_ADD': <class 'TimestampAdd'>, 'TIMESTAMPDIFF': <class 'TimestampDiff'>, 'TIMESTAMP_DIFF': <class 'TimestampDiff'>, 'TIMESTAMP_FROM_PARTS': <class 'TimestampFromParts'>, 'TIMESTAMPFROMPARTS': <class 'TimestampFromParts'>, 'TIMESTAMP_SUB': <class 'TimestampSub'>, 'TIMESTAMP_TRUNC': <class 'TimestampTrunc'>, 'TO_ARRAY': <class 'ToArray'>, 'TO_BASE64': <class 'ToBase64'>, 'TO_CHAR': <class 'ToChar'>, 'TO_DAYS': <class 'ToDays'>, 'TO_DOUBLE': <class 'ToDouble'>, 'TO_MAP': <class 'ToMap'>, 'TO_NUMBER': <class 'ToNumber'>, 'TRANSFORM': <class 'Transform'>, 'TRIM': <class 'Trim'>, 'TRY': <class 'Try'>, 'TRY_CAST': <class 'TryCast'>, 'TS_OR_DI_TO_DI': <class 'TsOrDiToDi'>, 'TS_OR_DS_ADD': <class 'TsOrDsAdd'>, 'TS_OR_DS_DIFF': <class 'TsOrDsDiff'>, 'TS_OR_DS_TO_DATE': <class 'TsOrDsToDate'>, 'TS_OR_DS_TO_DATE_STR': <class 'TsOrDsToDateStr'>, 'TS_OR_DS_TO_DATETIME': <class 'TsOrDsToDatetime'>, 'TS_OR_DS_TO_TIME': <class 'TsOrDsToTime'>, 'TS_OR_DS_TO_TIMESTAMP': <class 'TsOrDsToTimestamp'>, 'UNHEX': <class 'Unhex'>, 'UNICODE': <class 'Unicode'>, 'UNIX_DATE': <class 'UnixDate'>, 'UNIX_SECONDS': <class 'UnixSeconds'>, 'UNIX_TO_STR': <class 'UnixToStr'>, 'UNIX_TO_TIME': <class 'UnixToTime'>, 'UNIX_TO_TIME_STR': <class 'UnixToTimeStr'>, 'UNNEST': <class 'Unnest'>, 'UPPER': <class 'Upper'>, 'UCASE': <class 'Upper'>, 'UUID': <class 'Uuid'>, 'GEN_RANDOM_UUID': <class 'Uuid'>, 'GENERATE_UUID': <class 'Uuid'>, 'UUID_STRING': <class 'Uuid'>, 'VAR_MAP': <class 'VarMap'>, 'VARIANCE': <class 'Variance'>, 'VARIANCE_SAMP': <class 'Variance'>, 'VAR_SAMP': <class 'Variance'>, 'VARIANCE_POP': <class 'VariancePop'>, 'VAR_POP': <class 'VariancePop'>, 'WEEK': <class 'Week'>, 'WEEK_OF_YEAR': <class 'WeekOfYear'>, 'WEEKOFYEAR': <class 'WeekOfYear'>, 'XMLELEMENT': <class 'XMLElement'>, 'X_M_L_TABLE': <class 'XMLTable'>, 'XOR': <class 'Xor'>, 'YEAR': <class 'Year'>}
JSON_PATH_PARTS = [<class 'JSONPathFilter'>, <class 'JSONPathKey'>, <class 'JSONPathRecursive'>, <class 'JSONPathRoot'>, <class 'JSONPathScript'>, <class 'JSONPathSelector'>, <class 'JSONPathSlice'>, <class 'JSONPathSubscript'>, <class 'JSONPathUnion'>, <class 'JSONPathWildcard'>]
PERCENTILES = (<class 'PercentileCont'>, <class 'PercentileDisc'>)
def maybe_parse( sql_or_expression: Union[str, Expression], *, into: Union[str, Type[Expression], Collection[Union[str, Type[Expression]]], NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, prefix: Optional[str] = None, copy: bool = False, **opts) -> Expression:
7085def maybe_parse(
7086    sql_or_expression: ExpOrStr,
7087    *,
7088    into: t.Optional[IntoType] = None,
7089    dialect: DialectType = None,
7090    prefix: t.Optional[str] = None,
7091    copy: bool = False,
7092    **opts,
7093) -> Expression:
7094    """Gracefully handle a possible string or expression.
7095
7096    Example:
7097        >>> maybe_parse("1")
7098        Literal(this=1, is_string=False)
7099        >>> maybe_parse(to_identifier("x"))
7100        Identifier(this=x, quoted=False)
7101
7102    Args:
7103        sql_or_expression: the SQL code string or an expression
7104        into: the SQLGlot Expression to parse into
7105        dialect: the dialect used to parse the input expressions (in the case that an
7106            input expression is a SQL string).
7107        prefix: a string to prefix the sql with before it gets parsed
7108            (automatically includes a space)
7109        copy: whether to copy the expression.
7110        **opts: other options to use to parse the input expressions (again, in the case
7111            that an input expression is a SQL string).
7112
7113    Returns:
7114        Expression: the parsed or given expression.
7115    """
7116    if isinstance(sql_or_expression, Expression):
7117        if copy:
7118            return sql_or_expression.copy()
7119        return sql_or_expression
7120
7121    if sql_or_expression is None:
7122        raise ParseError("SQL cannot be None")
7123
7124    import sqlglot
7125
7126    sql = str(sql_or_expression)
7127    if prefix:
7128        sql = f"{prefix} {sql}"
7129
7130    return sqlglot.parse_one(sql, read=dialect, into=into, **opts)

Gracefully handle a possible string or expression.

Example:
>>> maybe_parse("1")
Literal(this=1, is_string=False)
>>> maybe_parse(to_identifier("x"))
Identifier(this=x, quoted=False)
Arguments:
  • sql_or_expression: the SQL code string or an expression
  • into: the SQLGlot Expression to parse into
  • dialect: the dialect used to parse the input expressions (in the case that an input expression is a SQL string).
  • prefix: a string to prefix the sql with before it gets parsed (automatically includes a space)
  • copy: whether to copy the expression.
  • **opts: other options to use to parse the input expressions (again, in the case that an input expression is a SQL string).
Returns:

Expression: the parsed or given expression.

def maybe_copy(instance, copy=True):
7141def maybe_copy(instance, copy=True):
7142    return instance.copy() if copy and instance else instance
def union( *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Union:
7397def union(
7398    *expressions: ExpOrStr,
7399    distinct: bool = True,
7400    dialect: DialectType = None,
7401    copy: bool = True,
7402    **opts,
7403) -> Union:
7404    """
7405    Initializes a syntax tree for the `UNION` operation.
7406
7407    Example:
7408        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
7409        'SELECT * FROM foo UNION SELECT * FROM bla'
7410
7411    Args:
7412        expressions: the SQL code strings, corresponding to the `UNION`'s operands.
7413            If `Expression` instances are passed, they will be used as-is.
7414        distinct: set the DISTINCT flag if and only if this is true.
7415        dialect: the dialect used to parse the input expression.
7416        copy: whether to copy the expression.
7417        opts: other options to use to parse the input expressions.
7418
7419    Returns:
7420        The new Union instance.
7421    """
7422    assert len(expressions) >= 2, "At least two expressions are required by `union`."
7423    return _apply_set_operation(
7424        *expressions, set_operation=Union, distinct=distinct, dialect=dialect, copy=copy, **opts
7425    )

Initializes a syntax tree for the UNION operation.

Example:
>>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
'SELECT * FROM foo UNION SELECT * FROM bla'
Arguments:
  • expressions: the SQL code strings, corresponding to the UNION's operands. If Expression instances are passed, they will be used as-is.
  • distinct: set the DISTINCT flag if and only if this is true.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy the expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Union instance.

def intersect( *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Intersect:
7428def intersect(
7429    *expressions: ExpOrStr,
7430    distinct: bool = True,
7431    dialect: DialectType = None,
7432    copy: bool = True,
7433    **opts,
7434) -> Intersect:
7435    """
7436    Initializes a syntax tree for the `INTERSECT` operation.
7437
7438    Example:
7439        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
7440        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
7441
7442    Args:
7443        expressions: the SQL code strings, corresponding to the `INTERSECT`'s operands.
7444            If `Expression` instances are passed, they will be used as-is.
7445        distinct: set the DISTINCT flag if and only if this is true.
7446        dialect: the dialect used to parse the input expression.
7447        copy: whether to copy the expression.
7448        opts: other options to use to parse the input expressions.
7449
7450    Returns:
7451        The new Intersect instance.
7452    """
7453    assert len(expressions) >= 2, "At least two expressions are required by `intersect`."
7454    return _apply_set_operation(
7455        *expressions, set_operation=Intersect, distinct=distinct, dialect=dialect, copy=copy, **opts
7456    )

Initializes a syntax tree for the INTERSECT operation.

Example:
>>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
'SELECT * FROM foo INTERSECT SELECT * FROM bla'
Arguments:
  • expressions: the SQL code strings, corresponding to the INTERSECT's operands. If Expression instances are passed, they will be used as-is.
  • distinct: set the DISTINCT flag if and only if this is true.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy the expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Intersect instance.

def except_( *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Except:
7459def except_(
7460    *expressions: ExpOrStr,
7461    distinct: bool = True,
7462    dialect: DialectType = None,
7463    copy: bool = True,
7464    **opts,
7465) -> Except:
7466    """
7467    Initializes a syntax tree for the `EXCEPT` operation.
7468
7469    Example:
7470        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
7471        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
7472
7473    Args:
7474        expressions: the SQL code strings, corresponding to the `EXCEPT`'s operands.
7475            If `Expression` instances are passed, they will be used as-is.
7476        distinct: set the DISTINCT flag if and only if this is true.
7477        dialect: the dialect used to parse the input expression.
7478        copy: whether to copy the expression.
7479        opts: other options to use to parse the input expressions.
7480
7481    Returns:
7482        The new Except instance.
7483    """
7484    assert len(expressions) >= 2, "At least two expressions are required by `except_`."
7485    return _apply_set_operation(
7486        *expressions, set_operation=Except, distinct=distinct, dialect=dialect, copy=copy, **opts
7487    )

Initializes a syntax tree for the EXCEPT operation.

Example:
>>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
'SELECT * FROM foo EXCEPT SELECT * FROM bla'
Arguments:
  • expressions: the SQL code strings, corresponding to the EXCEPT's operands. If Expression instances are passed, they will be used as-is.
  • distinct: set the DISTINCT flag if and only if this is true.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy the expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Except instance.

def select( *expressions: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Select:
7490def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7491    """
7492    Initializes a syntax tree from one or multiple SELECT expressions.
7493
7494    Example:
7495        >>> select("col1", "col2").from_("tbl").sql()
7496        'SELECT col1, col2 FROM tbl'
7497
7498    Args:
7499        *expressions: the SQL code string to parse as the expressions of a
7500            SELECT statement. If an Expression instance is passed, this is used as-is.
7501        dialect: the dialect used to parse the input expressions (in the case that an
7502            input expression is a SQL string).
7503        **opts: other options to use to parse the input expressions (again, in the case
7504            that an input expression is a SQL string).
7505
7506    Returns:
7507        Select: the syntax tree for the SELECT statement.
7508    """
7509    return Select().select(*expressions, dialect=dialect, **opts)

Initializes a syntax tree from one or multiple SELECT expressions.

Example:
>>> select("col1", "col2").from_("tbl").sql()
'SELECT col1, col2 FROM tbl'
Arguments:
  • *expressions: the SQL code string to parse as the expressions of a SELECT statement. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expressions (in the case that an input expression is a SQL string).
  • **opts: other options to use to parse the input expressions (again, in the case that an input expression is a SQL string).
Returns:

Select: the syntax tree for the SELECT statement.

def from_( expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Select:
7512def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7513    """
7514    Initializes a syntax tree from a FROM expression.
7515
7516    Example:
7517        >>> from_("tbl").select("col1", "col2").sql()
7518        'SELECT col1, col2 FROM tbl'
7519
7520    Args:
7521        *expression: the SQL code string to parse as the FROM expressions of a
7522            SELECT statement. If an Expression instance is passed, this is used as-is.
7523        dialect: the dialect used to parse the input expression (in the case that the
7524            input expression is a SQL string).
7525        **opts: other options to use to parse the input expressions (again, in the case
7526            that the input expression is a SQL string).
7527
7528    Returns:
7529        Select: the syntax tree for the SELECT statement.
7530    """
7531    return Select().from_(expression, dialect=dialect, **opts)

Initializes a syntax tree from a FROM expression.

Example:
>>> from_("tbl").select("col1", "col2").sql()
'SELECT col1, col2 FROM tbl'
Arguments:
  • *expression: the SQL code string to parse as the FROM expressions of a SELECT statement. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression (in the case that the input expression is a SQL string).
  • **opts: other options to use to parse the input expressions (again, in the case that the input expression is a SQL string).
Returns:

Select: the syntax tree for the SELECT statement.

def update( table: str | Table, properties: Optional[dict] = None, where: Union[str, Expression, NoneType] = None, from_: Union[str, Expression, NoneType] = None, with_: Optional[Dict[str, Union[str, Expression]]] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Update:
7534def update(
7535    table: str | Table,
7536    properties: t.Optional[dict] = None,
7537    where: t.Optional[ExpOrStr] = None,
7538    from_: t.Optional[ExpOrStr] = None,
7539    with_: t.Optional[t.Dict[str, ExpOrStr]] = None,
7540    dialect: DialectType = None,
7541    **opts,
7542) -> Update:
7543    """
7544    Creates an update statement.
7545
7546    Example:
7547        >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz_cte", where="baz_cte.id > 1 and my_table.id = baz_cte.id", with_={"baz_cte": "SELECT id FROM foo"}).sql()
7548        "WITH baz_cte AS (SELECT id FROM foo) UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz_cte WHERE baz_cte.id > 1 AND my_table.id = baz_cte.id"
7549
7550    Args:
7551        properties: dictionary of properties to SET which are
7552            auto converted to sql objects eg None -> NULL
7553        where: sql conditional parsed into a WHERE statement
7554        from_: sql statement parsed into a FROM statement
7555        with_: dictionary of CTE aliases / select statements to include in a WITH clause.
7556        dialect: the dialect used to parse the input expressions.
7557        **opts: other options to use to parse the input expressions.
7558
7559    Returns:
7560        Update: the syntax tree for the UPDATE statement.
7561    """
7562    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
7563    if properties:
7564        update_expr.set(
7565            "expressions",
7566            [
7567                EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
7568                for k, v in properties.items()
7569            ],
7570        )
7571    if from_:
7572        update_expr.set(
7573            "from",
7574            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
7575        )
7576    if isinstance(where, Condition):
7577        where = Where(this=where)
7578    if where:
7579        update_expr.set(
7580            "where",
7581            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
7582        )
7583    if with_:
7584        cte_list = [
7585            alias_(CTE(this=maybe_parse(qry, dialect=dialect, **opts)), alias, table=True)
7586            for alias, qry in with_.items()
7587        ]
7588        update_expr.set(
7589            "with",
7590            With(expressions=cte_list),
7591        )
7592    return update_expr

Creates an update statement.

Example:
>>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz_cte", where="baz_cte.id > 1 and my_table.id = baz_cte.id", with_={"baz_cte": "SELECT id FROM foo"}).sql()
"WITH baz_cte AS (SELECT id FROM foo) UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz_cte WHERE baz_cte.id > 1 AND my_table.id = baz_cte.id"
Arguments:
  • properties: dictionary of properties to SET which are auto converted to sql objects eg None -> NULL
  • where: sql conditional parsed into a WHERE statement
  • from_: sql statement parsed into a FROM statement
  • with_: dictionary of CTE aliases / select statements to include in a WITH clause.
  • dialect: the dialect used to parse the input expressions.
  • **opts: other options to use to parse the input expressions.
Returns:

Update: the syntax tree for the UPDATE statement.

def delete( table: Union[str, Expression], where: Union[str, Expression, NoneType] = None, returning: Union[str, Expression, NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Delete:
7595def delete(
7596    table: ExpOrStr,
7597    where: t.Optional[ExpOrStr] = None,
7598    returning: t.Optional[ExpOrStr] = None,
7599    dialect: DialectType = None,
7600    **opts,
7601) -> Delete:
7602    """
7603    Builds a delete statement.
7604
7605    Example:
7606        >>> delete("my_table", where="id > 1").sql()
7607        'DELETE FROM my_table WHERE id > 1'
7608
7609    Args:
7610        where: sql conditional parsed into a WHERE statement
7611        returning: sql conditional parsed into a RETURNING statement
7612        dialect: the dialect used to parse the input expressions.
7613        **opts: other options to use to parse the input expressions.
7614
7615    Returns:
7616        Delete: the syntax tree for the DELETE statement.
7617    """
7618    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
7619    if where:
7620        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
7621    if returning:
7622        delete_expr = delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
7623    return delete_expr

Builds a delete statement.

Example:
>>> delete("my_table", where="id > 1").sql()
'DELETE FROM my_table WHERE id > 1'
Arguments:
  • where: sql conditional parsed into a WHERE statement
  • returning: sql conditional parsed into a RETURNING statement
  • dialect: the dialect used to parse the input expressions.
  • **opts: other options to use to parse the input expressions.
Returns:

Delete: the syntax tree for the DELETE statement.

def insert( expression: Union[str, Expression], into: Union[str, Expression], columns: Optional[Sequence[str | Identifier]] = None, overwrite: Optional[bool] = None, returning: Union[str, Expression, NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Insert:
7626def insert(
7627    expression: ExpOrStr,
7628    into: ExpOrStr,
7629    columns: t.Optional[t.Sequence[str | Identifier]] = None,
7630    overwrite: t.Optional[bool] = None,
7631    returning: t.Optional[ExpOrStr] = None,
7632    dialect: DialectType = None,
7633    copy: bool = True,
7634    **opts,
7635) -> Insert:
7636    """
7637    Builds an INSERT statement.
7638
7639    Example:
7640        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
7641        'INSERT INTO tbl VALUES (1, 2, 3)'
7642
7643    Args:
7644        expression: the sql string or expression of the INSERT statement
7645        into: the tbl to insert data to.
7646        columns: optionally the table's column names.
7647        overwrite: whether to INSERT OVERWRITE or not.
7648        returning: sql conditional parsed into a RETURNING statement
7649        dialect: the dialect used to parse the input expressions.
7650        copy: whether to copy the expression.
7651        **opts: other options to use to parse the input expressions.
7652
7653    Returns:
7654        Insert: the syntax tree for the INSERT statement.
7655    """
7656    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
7657    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
7658
7659    if columns:
7660        this = Schema(this=this, expressions=[to_identifier(c, copy=copy) for c in columns])
7661
7662    insert = Insert(this=this, expression=expr, overwrite=overwrite)
7663
7664    if returning:
7665        insert = insert.returning(returning, dialect=dialect, copy=False, **opts)
7666
7667    return insert

Builds an INSERT statement.

Example:
>>> insert("VALUES (1, 2, 3)", "tbl").sql()
'INSERT INTO tbl VALUES (1, 2, 3)'
Arguments:
  • expression: the sql string or expression of the INSERT statement
  • into: the tbl to insert data to.
  • columns: optionally the table's column names.
  • overwrite: whether to INSERT OVERWRITE or not.
  • returning: sql conditional parsed into a RETURNING statement
  • dialect: the dialect used to parse the input expressions.
  • copy: whether to copy the expression.
  • **opts: other options to use to parse the input expressions.
Returns:

Insert: the syntax tree for the INSERT statement.

def merge( *when_exprs: Union[str, Expression], into: Union[str, Expression], using: Union[str, Expression], on: Union[str, Expression], returning: Union[str, Expression, NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Merge:
7670def merge(
7671    *when_exprs: ExpOrStr,
7672    into: ExpOrStr,
7673    using: ExpOrStr,
7674    on: ExpOrStr,
7675    returning: t.Optional[ExpOrStr] = None,
7676    dialect: DialectType = None,
7677    copy: bool = True,
7678    **opts,
7679) -> Merge:
7680    """
7681    Builds a MERGE statement.
7682
7683    Example:
7684        >>> merge("WHEN MATCHED THEN UPDATE SET col1 = source_table.col1",
7685        ...       "WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)",
7686        ...       into="my_table",
7687        ...       using="source_table",
7688        ...       on="my_table.id = source_table.id").sql()
7689        'MERGE INTO my_table USING source_table ON my_table.id = source_table.id WHEN MATCHED THEN UPDATE SET col1 = source_table.col1 WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)'
7690
7691    Args:
7692        *when_exprs: The WHEN clauses specifying actions for matched and unmatched rows.
7693        into: The target table to merge data into.
7694        using: The source table to merge data from.
7695        on: The join condition for the merge.
7696        returning: The columns to return from the merge.
7697        dialect: The dialect used to parse the input expressions.
7698        copy: Whether to copy the expression.
7699        **opts: Other options to use to parse the input expressions.
7700
7701    Returns:
7702        Merge: The syntax tree for the MERGE statement.
7703    """
7704    expressions: t.List[Expression] = []
7705    for when_expr in when_exprs:
7706        expression = maybe_parse(when_expr, dialect=dialect, copy=copy, into=Whens, **opts)
7707        expressions.extend([expression] if isinstance(expression, When) else expression.expressions)
7708
7709    merge = Merge(
7710        this=maybe_parse(into, dialect=dialect, copy=copy, **opts),
7711        using=maybe_parse(using, dialect=dialect, copy=copy, **opts),
7712        on=maybe_parse(on, dialect=dialect, copy=copy, **opts),
7713        whens=Whens(expressions=expressions),
7714    )
7715    if returning:
7716        merge = merge.returning(returning, dialect=dialect, copy=False, **opts)
7717
7718    return merge

Builds a MERGE statement.

Example:
>>> merge("WHEN MATCHED THEN UPDATE SET col1 = source_table.col1",
...       "WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)",
...       into="my_table",
...       using="source_table",
...       on="my_table.id = source_table.id").sql()
'MERGE INTO my_table USING source_table ON my_table.id = source_table.id WHEN MATCHED THEN UPDATE SET col1 = source_table.col1 WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)'
Arguments:
  • *when_exprs: The WHEN clauses specifying actions for matched and unmatched rows.
  • into: The target table to merge data into.
  • using: The source table to merge data from.
  • on: The join condition for the merge.
  • returning: The columns to return from the merge.
  • dialect: The dialect used to parse the input expressions.
  • copy: Whether to copy the expression.
  • **opts: Other options to use to parse the input expressions.
Returns:

Merge: The syntax tree for the MERGE statement.

def condition( expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
7721def condition(
7722    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
7723) -> Condition:
7724    """
7725    Initialize a logical condition expression.
7726
7727    Example:
7728        >>> condition("x=1").sql()
7729        'x = 1'
7730
7731        This is helpful for composing larger logical syntax trees:
7732        >>> where = condition("x=1")
7733        >>> where = where.and_("y=1")
7734        >>> Select().from_("tbl").select("*").where(where).sql()
7735        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
7736
7737    Args:
7738        *expression: the SQL code string to parse.
7739            If an Expression instance is passed, this is used as-is.
7740        dialect: the dialect used to parse the input expression (in the case that the
7741            input expression is a SQL string).
7742        copy: Whether to copy `expression` (only applies to expressions).
7743        **opts: other options to use to parse the input expressions (again, in the case
7744            that the input expression is a SQL string).
7745
7746    Returns:
7747        The new Condition instance
7748    """
7749    return maybe_parse(
7750        expression,
7751        into=Condition,
7752        dialect=dialect,
7753        copy=copy,
7754        **opts,
7755    )

Initialize a logical condition expression.

Example:
>>> condition("x=1").sql()
'x = 1'

This is helpful for composing larger logical syntax trees:

>>> where = condition("x=1")
>>> where = where.and_("y=1")
>>> Select().from_("tbl").select("*").where(where).sql()
'SELECT * FROM tbl WHERE x = 1 AND y = 1'
Arguments:
  • *expression: the SQL code string to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression (in the case that the input expression is a SQL string).
  • copy: Whether to copy expression (only applies to expressions).
  • **opts: other options to use to parse the input expressions (again, in the case that the input expression is a SQL string).
Returns:

The new Condition instance

def and_( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
7758def and_(
7759    *expressions: t.Optional[ExpOrStr],
7760    dialect: DialectType = None,
7761    copy: bool = True,
7762    wrap: bool = True,
7763    **opts,
7764) -> Condition:
7765    """
7766    Combine multiple conditions with an AND logical operator.
7767
7768    Example:
7769        >>> and_("x=1", and_("y=1", "z=1")).sql()
7770        'x = 1 AND (y = 1 AND z = 1)'
7771
7772    Args:
7773        *expressions: the SQL code strings to parse.
7774            If an Expression instance is passed, this is used as-is.
7775        dialect: the dialect used to parse the input expression.
7776        copy: whether to copy `expressions` (only applies to Expressions).
7777        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7778            precedence issues, but can be turned off when the produced AST is too deep and
7779            causes recursion-related issues.
7780        **opts: other options to use to parse the input expressions.
7781
7782    Returns:
7783        The new condition
7784    """
7785    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, wrap=wrap, **opts))

Combine multiple conditions with an AND logical operator.

Example:
>>> and_("x=1", and_("y=1", "z=1")).sql()
'x = 1 AND (y = 1 AND z = 1)'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy expressions (only applies to Expressions).
  • wrap: whether to wrap the operands in Parens. This is true by default to avoid precedence issues, but can be turned off when the produced AST is too deep and causes recursion-related issues.
  • **opts: other options to use to parse the input expressions.
Returns:

The new condition

def or_( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
7788def or_(
7789    *expressions: t.Optional[ExpOrStr],
7790    dialect: DialectType = None,
7791    copy: bool = True,
7792    wrap: bool = True,
7793    **opts,
7794) -> Condition:
7795    """
7796    Combine multiple conditions with an OR logical operator.
7797
7798    Example:
7799        >>> or_("x=1", or_("y=1", "z=1")).sql()
7800        'x = 1 OR (y = 1 OR z = 1)'
7801
7802    Args:
7803        *expressions: the SQL code strings to parse.
7804            If an Expression instance is passed, this is used as-is.
7805        dialect: the dialect used to parse the input expression.
7806        copy: whether to copy `expressions` (only applies to Expressions).
7807        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7808            precedence issues, but can be turned off when the produced AST is too deep and
7809            causes recursion-related issues.
7810        **opts: other options to use to parse the input expressions.
7811
7812    Returns:
7813        The new condition
7814    """
7815    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, wrap=wrap, **opts))

Combine multiple conditions with an OR logical operator.

Example:
>>> or_("x=1", or_("y=1", "z=1")).sql()
'x = 1 OR (y = 1 OR z = 1)'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy expressions (only applies to Expressions).
  • wrap: whether to wrap the operands in Parens. This is true by default to avoid precedence issues, but can be turned off when the produced AST is too deep and causes recursion-related issues.
  • **opts: other options to use to parse the input expressions.
Returns:

The new condition

def xor( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
7818def xor(
7819    *expressions: t.Optional[ExpOrStr],
7820    dialect: DialectType = None,
7821    copy: bool = True,
7822    wrap: bool = True,
7823    **opts,
7824) -> Condition:
7825    """
7826    Combine multiple conditions with an XOR logical operator.
7827
7828    Example:
7829        >>> xor("x=1", xor("y=1", "z=1")).sql()
7830        'x = 1 XOR (y = 1 XOR z = 1)'
7831
7832    Args:
7833        *expressions: the SQL code strings to parse.
7834            If an Expression instance is passed, this is used as-is.
7835        dialect: the dialect used to parse the input expression.
7836        copy: whether to copy `expressions` (only applies to Expressions).
7837        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7838            precedence issues, but can be turned off when the produced AST is too deep and
7839            causes recursion-related issues.
7840        **opts: other options to use to parse the input expressions.
7841
7842    Returns:
7843        The new condition
7844    """
7845    return t.cast(Condition, _combine(expressions, Xor, dialect, copy=copy, wrap=wrap, **opts))

Combine multiple conditions with an XOR logical operator.

Example:
>>> xor("x=1", xor("y=1", "z=1")).sql()
'x = 1 XOR (y = 1 XOR z = 1)'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy expressions (only applies to Expressions).
  • wrap: whether to wrap the operands in Parens. This is true by default to avoid precedence issues, but can be turned off when the produced AST is too deep and causes recursion-related issues.
  • **opts: other options to use to parse the input expressions.
Returns:

The new condition

def not_( expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts) -> Not:
7848def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
7849    """
7850    Wrap a condition with a NOT operator.
7851
7852    Example:
7853        >>> not_("this_suit='black'").sql()
7854        "NOT this_suit = 'black'"
7855
7856    Args:
7857        expression: the SQL code string to parse.
7858            If an Expression instance is passed, this is used as-is.
7859        dialect: the dialect used to parse the input expression.
7860        copy: whether to copy the expression or not.
7861        **opts: other options to use to parse the input expressions.
7862
7863    Returns:
7864        The new condition.
7865    """
7866    this = condition(
7867        expression,
7868        dialect=dialect,
7869        copy=copy,
7870        **opts,
7871    )
7872    return Not(this=_wrap(this, Connector))

Wrap a condition with a NOT operator.

Example:
>>> not_("this_suit='black'").sql()
"NOT this_suit = 'black'"
Arguments:
  • expression: the SQL code string to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether to copy the expression or not.
  • **opts: other options to use to parse the input expressions.
Returns:

The new condition.

def paren( expression: Union[str, Expression], copy: bool = True) -> Paren:
7875def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
7876    """
7877    Wrap an expression in parentheses.
7878
7879    Example:
7880        >>> paren("5 + 3").sql()
7881        '(5 + 3)'
7882
7883    Args:
7884        expression: the SQL code string to parse.
7885            If an Expression instance is passed, this is used as-is.
7886        copy: whether to copy the expression or not.
7887
7888    Returns:
7889        The wrapped expression.
7890    """
7891    return Paren(this=maybe_parse(expression, copy=copy))

Wrap an expression in parentheses.

Example:
>>> paren("5 + 3").sql()
'(5 + 3)'
Arguments:
  • expression: the SQL code string to parse. If an Expression instance is passed, this is used as-is.
  • copy: whether to copy the expression or not.
Returns:

The wrapped expression.

SAFE_IDENTIFIER_RE: Pattern[str] = re.compile('^[_a-zA-Z][\\w]*$')
def to_identifier(name, quoted=None, copy=True):
7907def to_identifier(name, quoted=None, copy=True):
7908    """Builds an identifier.
7909
7910    Args:
7911        name: The name to turn into an identifier.
7912        quoted: Whether to force quote the identifier.
7913        copy: Whether to copy name if it's an Identifier.
7914
7915    Returns:
7916        The identifier ast node.
7917    """
7918
7919    if name is None:
7920        return None
7921
7922    if isinstance(name, Identifier):
7923        identifier = maybe_copy(name, copy)
7924    elif isinstance(name, str):
7925        identifier = Identifier(
7926            this=name,
7927            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
7928        )
7929    else:
7930        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
7931    return identifier

Builds an identifier.

Arguments:
  • name: The name to turn into an identifier.
  • quoted: Whether to force quote the identifier.
  • copy: Whether to copy name if it's an Identifier.
Returns:

The identifier ast node.

def parse_identifier( name: str | Identifier, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None) -> Identifier:
7934def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
7935    """
7936    Parses a given string into an identifier.
7937
7938    Args:
7939        name: The name to parse into an identifier.
7940        dialect: The dialect to parse against.
7941
7942    Returns:
7943        The identifier ast node.
7944    """
7945    try:
7946        expression = maybe_parse(name, dialect=dialect, into=Identifier)
7947    except (ParseError, TokenError):
7948        expression = to_identifier(name)
7949
7950    return expression

Parses a given string into an identifier.

Arguments:
  • name: The name to parse into an identifier.
  • dialect: The dialect to parse against.
Returns:

The identifier ast node.

INTERVAL_STRING_RE = re.compile('\\s*(-?[0-9]+(?:\\.[0-9]+)?)\\s*([a-zA-Z]+)\\s*')
def to_interval( interval: str | Literal) -> Interval:
7956def to_interval(interval: str | Literal) -> Interval:
7957    """Builds an interval expression from a string like '1 day' or '5 months'."""
7958    if isinstance(interval, Literal):
7959        if not interval.is_string:
7960            raise ValueError("Invalid interval string.")
7961
7962        interval = interval.this
7963
7964    interval = maybe_parse(f"INTERVAL {interval}")
7965    assert isinstance(interval, Interval)
7966    return interval

Builds an interval expression from a string like '1 day' or '5 months'.

def to_table( sql_path: str | Table, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **kwargs) -> Table:
7969def to_table(
7970    sql_path: str | Table, dialect: DialectType = None, copy: bool = True, **kwargs
7971) -> Table:
7972    """
7973    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
7974    If a table is passed in then that table is returned.
7975
7976    Args:
7977        sql_path: a `[catalog].[schema].[table]` string.
7978        dialect: the source dialect according to which the table name will be parsed.
7979        copy: Whether to copy a table if it is passed in.
7980        kwargs: the kwargs to instantiate the resulting `Table` expression with.
7981
7982    Returns:
7983        A table expression.
7984    """
7985    if isinstance(sql_path, Table):
7986        return maybe_copy(sql_path, copy=copy)
7987
7988    try:
7989        table = maybe_parse(sql_path, into=Table, dialect=dialect)
7990    except ParseError:
7991        catalog, db, this = split_num_words(sql_path, ".", 3)
7992
7993        if not this:
7994            raise
7995
7996        table = table_(this, db=db, catalog=catalog)
7997
7998    for k, v in kwargs.items():
7999        table.set(k, v)
8000
8001    return table

Create a table expression from a [catalog].[schema].[table] sql path. Catalog and schema are optional. If a table is passed in then that table is returned.

Arguments:
  • sql_path: a [catalog].[schema].[table] string.
  • dialect: the source dialect according to which the table name will be parsed.
  • copy: Whether to copy a table if it is passed in.
  • kwargs: the kwargs to instantiate the resulting Table expression with.
Returns:

A table expression.

def to_column( sql_path: str | Column, quoted: Optional[bool] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **kwargs) -> Column:
8004def to_column(
8005    sql_path: str | Column,
8006    quoted: t.Optional[bool] = None,
8007    dialect: DialectType = None,
8008    copy: bool = True,
8009    **kwargs,
8010) -> Column:
8011    """
8012    Create a column from a `[table].[column]` sql path. Table is optional.
8013    If a column is passed in then that column is returned.
8014
8015    Args:
8016        sql_path: a `[table].[column]` string.
8017        quoted: Whether or not to force quote identifiers.
8018        dialect: the source dialect according to which the column name will be parsed.
8019        copy: Whether to copy a column if it is passed in.
8020        kwargs: the kwargs to instantiate the resulting `Column` expression with.
8021
8022    Returns:
8023        A column expression.
8024    """
8025    if isinstance(sql_path, Column):
8026        return maybe_copy(sql_path, copy=copy)
8027
8028    try:
8029        col = maybe_parse(sql_path, into=Column, dialect=dialect)
8030    except ParseError:
8031        return column(*reversed(sql_path.split(".")), quoted=quoted, **kwargs)
8032
8033    for k, v in kwargs.items():
8034        col.set(k, v)
8035
8036    if quoted:
8037        for i in col.find_all(Identifier):
8038            i.set("quoted", True)
8039
8040    return col

Create a column from a [table].[column] sql path. Table is optional. If a column is passed in then that column is returned.

Arguments:
  • sql_path: a [table].[column] string.
  • quoted: Whether or not to force quote identifiers.
  • dialect: the source dialect according to which the column name will be parsed.
  • copy: Whether to copy a column if it is passed in.
  • kwargs: the kwargs to instantiate the resulting Column expression with.
Returns:

A column expression.

def alias_( expression: Union[str, Expression], alias: Union[Identifier, str, NoneType], table: Union[bool, Sequence[str | Identifier]] = False, quoted: Optional[bool] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True, **opts):
8043def alias_(
8044    expression: ExpOrStr,
8045    alias: t.Optional[str | Identifier],
8046    table: bool | t.Sequence[str | Identifier] = False,
8047    quoted: t.Optional[bool] = None,
8048    dialect: DialectType = None,
8049    copy: bool = True,
8050    **opts,
8051):
8052    """Create an Alias expression.
8053
8054    Example:
8055        >>> alias_('foo', 'bar').sql()
8056        'foo AS bar'
8057
8058        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
8059        '(SELECT 1, 2) AS bar(a, b)'
8060
8061    Args:
8062        expression: the SQL code strings to parse.
8063            If an Expression instance is passed, this is used as-is.
8064        alias: the alias name to use. If the name has
8065            special characters it is quoted.
8066        table: Whether to create a table alias, can also be a list of columns.
8067        quoted: whether to quote the alias
8068        dialect: the dialect used to parse the input expression.
8069        copy: Whether to copy the expression.
8070        **opts: other options to use to parse the input expressions.
8071
8072    Returns:
8073        Alias: the aliased expression
8074    """
8075    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
8076    alias = to_identifier(alias, quoted=quoted)
8077
8078    if table:
8079        table_alias = TableAlias(this=alias)
8080        exp.set("alias", table_alias)
8081
8082        if not isinstance(table, bool):
8083            for column in table:
8084                table_alias.append("columns", to_identifier(column, quoted=quoted))
8085
8086        return exp
8087
8088    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
8089    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
8090    # for the complete Window expression.
8091    #
8092    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
8093
8094    if "alias" in exp.arg_types and not isinstance(exp, Window):
8095        exp.set("alias", alias)
8096        return exp
8097    return Alias(this=exp, alias=alias)

Create an Alias expression.

Example:
>>> alias_('foo', 'bar').sql()
'foo AS bar'
>>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
'(SELECT 1, 2) AS bar(a, b)'
Arguments:
  • expression: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • alias: the alias name to use. If the name has special characters it is quoted.
  • table: Whether to create a table alias, can also be a list of columns.
  • quoted: whether to quote the alias
  • dialect: the dialect used to parse the input expression.
  • copy: Whether to copy the expression.
  • **opts: other options to use to parse the input expressions.
Returns:

Alias: the aliased expression

def subquery( expression: Union[str, Expression], alias: Union[Identifier, str, NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Select:
8100def subquery(
8101    expression: ExpOrStr,
8102    alias: t.Optional[Identifier | str] = None,
8103    dialect: DialectType = None,
8104    **opts,
8105) -> Select:
8106    """
8107    Build a subquery expression that's selected from.
8108
8109    Example:
8110        >>> subquery('select x from tbl', 'bar').select('x').sql()
8111        'SELECT x FROM (SELECT x FROM tbl) AS bar'
8112
8113    Args:
8114        expression: the SQL code strings to parse.
8115            If an Expression instance is passed, this is used as-is.
8116        alias: the alias name to use.
8117        dialect: the dialect used to parse the input expression.
8118        **opts: other options to use to parse the input expressions.
8119
8120    Returns:
8121        A new Select instance with the subquery expression included.
8122    """
8123
8124    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias, **opts)
8125    return Select().from_(expression, dialect=dialect, **opts)

Build a subquery expression that's selected from.

Example:
>>> subquery('select x from tbl', 'bar').select('x').sql()
'SELECT x FROM (SELECT x FROM tbl) AS bar'
Arguments:
  • expression: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • alias: the alias name to use.
  • dialect: the dialect used to parse the input expression.
  • **opts: other options to use to parse the input expressions.
Returns:

A new Select instance with the subquery expression included.

def column( col, table=None, db=None, catalog=None, *, fields=None, quoted=None, copy=True):
8156def column(
8157    col,
8158    table=None,
8159    db=None,
8160    catalog=None,
8161    *,
8162    fields=None,
8163    quoted=None,
8164    copy=True,
8165):
8166    """
8167    Build a Column.
8168
8169    Args:
8170        col: Column name.
8171        table: Table name.
8172        db: Database name.
8173        catalog: Catalog name.
8174        fields: Additional fields using dots.
8175        quoted: Whether to force quotes on the column's identifiers.
8176        copy: Whether to copy identifiers if passed in.
8177
8178    Returns:
8179        The new Column instance.
8180    """
8181    if not isinstance(col, Star):
8182        col = to_identifier(col, quoted=quoted, copy=copy)
8183
8184    this = Column(
8185        this=col,
8186        table=to_identifier(table, quoted=quoted, copy=copy),
8187        db=to_identifier(db, quoted=quoted, copy=copy),
8188        catalog=to_identifier(catalog, quoted=quoted, copy=copy),
8189    )
8190
8191    if fields:
8192        this = Dot.build(
8193            (this, *(to_identifier(field, quoted=quoted, copy=copy) for field in fields))
8194        )
8195    return this

Build a Column.

Arguments:
  • col: Column name.
  • table: Table name.
  • db: Database name.
  • catalog: Catalog name.
  • fields: Additional fields using dots.
  • quoted: Whether to force quotes on the column's identifiers.
  • copy: Whether to copy identifiers if passed in.
Returns:

The new Column instance.

def cast( expression: Union[str, Expression], to: Union[str, Identifier, Dot, DataType, DataType.Type], copy: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **opts) -> Cast:
8198def cast(
8199    expression: ExpOrStr, to: DATA_TYPE, copy: bool = True, dialect: DialectType = None, **opts
8200) -> Cast:
8201    """Cast an expression to a data type.
8202
8203    Example:
8204        >>> cast('x + 1', 'int').sql()
8205        'CAST(x + 1 AS INT)'
8206
8207    Args:
8208        expression: The expression to cast.
8209        to: The datatype to cast to.
8210        copy: Whether to copy the supplied expressions.
8211        dialect: The target dialect. This is used to prevent a re-cast in the following scenario:
8212            - The expression to be cast is already a exp.Cast expression
8213            - The existing cast is to a type that is logically equivalent to new type
8214
8215            For example, if :expression='CAST(x as DATETIME)' and :to=Type.TIMESTAMP,
8216            but in the target dialect DATETIME is mapped to TIMESTAMP, then we will NOT return `CAST(x (as DATETIME) as TIMESTAMP)`
8217            and instead just return the original expression `CAST(x as DATETIME)`.
8218
8219            This is to prevent it being output as a double cast `CAST(x (as TIMESTAMP) as TIMESTAMP)` once the DATETIME -> TIMESTAMP
8220            mapping is applied in the target dialect generator.
8221
8222    Returns:
8223        The new Cast instance.
8224    """
8225    expr = maybe_parse(expression, copy=copy, dialect=dialect, **opts)
8226    data_type = DataType.build(to, copy=copy, dialect=dialect, **opts)
8227
8228    # dont re-cast if the expression is already a cast to the correct type
8229    if isinstance(expr, Cast):
8230        from sqlglot.dialects.dialect import Dialect
8231
8232        target_dialect = Dialect.get_or_raise(dialect)
8233        type_mapping = target_dialect.generator_class.TYPE_MAPPING
8234
8235        existing_cast_type: DataType.Type = expr.to.this
8236        new_cast_type: DataType.Type = data_type.this
8237        types_are_equivalent = type_mapping.get(
8238            existing_cast_type, existing_cast_type.value
8239        ) == type_mapping.get(new_cast_type, new_cast_type.value)
8240
8241        if expr.is_type(data_type) or types_are_equivalent:
8242            return expr
8243
8244    expr = Cast(this=expr, to=data_type)
8245    expr.type = data_type
8246
8247    return expr

Cast an expression to a data type.

Example:
>>> cast('x + 1', 'int').sql()
'CAST(x + 1 AS INT)'
Arguments:
  • expression: The expression to cast.
  • to: The datatype to cast to.
  • copy: Whether to copy the supplied expressions.
  • dialect: The target dialect. This is used to prevent a re-cast in the following scenario:

    • The expression to be cast is already a exp.Cast expression
    • The existing cast is to a type that is logically equivalent to new type

    For example, if :expression='CAST(x as DATETIME)' and :to=Type.TIMESTAMP, but in the target dialect DATETIME is mapped to TIMESTAMP, then we will NOT return CAST(x (as DATETIME) as TIMESTAMP) and instead just return the original expression CAST(x as DATETIME).

    This is to prevent it being output as a double cast CAST(x (as TIMESTAMP) as TIMESTAMP) once the DATETIME -> TIMESTAMP mapping is applied in the target dialect generator.

Returns:

The new Cast instance.

def table_( table: Identifier | str, db: Union[Identifier, str, NoneType] = None, catalog: Union[Identifier, str, NoneType] = None, quoted: Optional[bool] = None, alias: Union[Identifier, str, NoneType] = None) -> Table:
8250def table_(
8251    table: Identifier | str,
8252    db: t.Optional[Identifier | str] = None,
8253    catalog: t.Optional[Identifier | str] = None,
8254    quoted: t.Optional[bool] = None,
8255    alias: t.Optional[Identifier | str] = None,
8256) -> Table:
8257    """Build a Table.
8258
8259    Args:
8260        table: Table name.
8261        db: Database name.
8262        catalog: Catalog name.
8263        quote: Whether to force quotes on the table's identifiers.
8264        alias: Table's alias.
8265
8266    Returns:
8267        The new Table instance.
8268    """
8269    return Table(
8270        this=to_identifier(table, quoted=quoted) if table else None,
8271        db=to_identifier(db, quoted=quoted) if db else None,
8272        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
8273        alias=TableAlias(this=to_identifier(alias)) if alias else None,
8274    )

Build a Table.

Arguments:
  • table: Table name.
  • db: Database name.
  • catalog: Catalog name.
  • quote: Whether to force quotes on the table's identifiers.
  • alias: Table's alias.
Returns:

The new Table instance.

def values( values: Iterable[Tuple[Any, ...]], alias: Optional[str] = None, columns: Union[Iterable[str], Dict[str, DataType], NoneType] = None) -> Values:
8277def values(
8278    values: t.Iterable[t.Tuple[t.Any, ...]],
8279    alias: t.Optional[str] = None,
8280    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
8281) -> Values:
8282    """Build VALUES statement.
8283
8284    Example:
8285        >>> values([(1, '2')]).sql()
8286        "VALUES (1, '2')"
8287
8288    Args:
8289        values: values statements that will be converted to SQL
8290        alias: optional alias
8291        columns: Optional list of ordered column names or ordered dictionary of column names to types.
8292         If either are provided then an alias is also required.
8293
8294    Returns:
8295        Values: the Values expression object
8296    """
8297    if columns and not alias:
8298        raise ValueError("Alias is required when providing columns")
8299
8300    return Values(
8301        expressions=[convert(tup) for tup in values],
8302        alias=(
8303            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
8304            if columns
8305            else (TableAlias(this=to_identifier(alias)) if alias else None)
8306        ),
8307    )

Build VALUES statement.

Example:
>>> values([(1, '2')]).sql()
"VALUES (1, '2')"
Arguments:
  • values: values statements that will be converted to SQL
  • alias: optional alias
  • columns: Optional list of ordered column names or ordered dictionary of column names to types. If either are provided then an alias is also required.
Returns:

Values: the Values expression object

def var( name: Union[str, Expression, NoneType]) -> Var:
8310def var(name: t.Optional[ExpOrStr]) -> Var:
8311    """Build a SQL variable.
8312
8313    Example:
8314        >>> repr(var('x'))
8315        'Var(this=x)'
8316
8317        >>> repr(var(column('x', table='y')))
8318        'Var(this=x)'
8319
8320    Args:
8321        name: The name of the var or an expression who's name will become the var.
8322
8323    Returns:
8324        The new variable node.
8325    """
8326    if not name:
8327        raise ValueError("Cannot convert empty name into var.")
8328
8329    if isinstance(name, Expression):
8330        name = name.name
8331    return Var(this=name)

Build a SQL variable.

Example:
>>> repr(var('x'))
'Var(this=x)'
>>> repr(var(column('x', table='y')))
'Var(this=x)'
Arguments:
  • name: The name of the var or an expression who's name will become the var.
Returns:

The new variable node.

def rename_table( old_name: str | Table, new_name: str | Table, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None) -> Alter:
8334def rename_table(
8335    old_name: str | Table,
8336    new_name: str | Table,
8337    dialect: DialectType = None,
8338) -> Alter:
8339    """Build ALTER TABLE... RENAME... expression
8340
8341    Args:
8342        old_name: The old name of the table
8343        new_name: The new name of the table
8344        dialect: The dialect to parse the table.
8345
8346    Returns:
8347        Alter table expression
8348    """
8349    old_table = to_table(old_name, dialect=dialect)
8350    new_table = to_table(new_name, dialect=dialect)
8351    return Alter(
8352        this=old_table,
8353        kind="TABLE",
8354        actions=[
8355            AlterRename(this=new_table),
8356        ],
8357    )

Build ALTER TABLE... RENAME... expression

Arguments:
  • old_name: The old name of the table
  • new_name: The new name of the table
  • dialect: The dialect to parse the table.
Returns:

Alter table expression

def rename_column( table_name: str | Table, old_column_name: str | Column, new_column_name: str | Column, exists: Optional[bool] = None, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None) -> Alter:
8360def rename_column(
8361    table_name: str | Table,
8362    old_column_name: str | Column,
8363    new_column_name: str | Column,
8364    exists: t.Optional[bool] = None,
8365    dialect: DialectType = None,
8366) -> Alter:
8367    """Build ALTER TABLE... RENAME COLUMN... expression
8368
8369    Args:
8370        table_name: Name of the table
8371        old_column: The old name of the column
8372        new_column: The new name of the column
8373        exists: Whether to add the `IF EXISTS` clause
8374        dialect: The dialect to parse the table/column.
8375
8376    Returns:
8377        Alter table expression
8378    """
8379    table = to_table(table_name, dialect=dialect)
8380    old_column = to_column(old_column_name, dialect=dialect)
8381    new_column = to_column(new_column_name, dialect=dialect)
8382    return Alter(
8383        this=table,
8384        kind="TABLE",
8385        actions=[
8386            RenameColumn(this=old_column, to=new_column, exists=exists),
8387        ],
8388    )

Build ALTER TABLE... RENAME COLUMN... expression

Arguments:
  • table_name: Name of the table
  • old_column: The old name of the column
  • new_column: The new name of the column
  • exists: Whether to add the IF EXISTS clause
  • dialect: The dialect to parse the table/column.
Returns:

Alter table expression

def convert(value: Any, copy: bool = False) -> Expression:
8391def convert(value: t.Any, copy: bool = False) -> Expression:
8392    """Convert a python value into an expression object.
8393
8394    Raises an error if a conversion is not possible.
8395
8396    Args:
8397        value: A python object.
8398        copy: Whether to copy `value` (only applies to Expressions and collections).
8399
8400    Returns:
8401        The equivalent expression object.
8402    """
8403    if isinstance(value, Expression):
8404        return maybe_copy(value, copy)
8405    if isinstance(value, str):
8406        return Literal.string(value)
8407    if isinstance(value, bool):
8408        return Boolean(this=value)
8409    if value is None or (isinstance(value, float) and math.isnan(value)):
8410        return null()
8411    if isinstance(value, numbers.Number):
8412        return Literal.number(value)
8413    if isinstance(value, bytes):
8414        return HexString(this=value.hex())
8415    if isinstance(value, datetime.datetime):
8416        datetime_literal = Literal.string(value.isoformat(sep=" "))
8417
8418        tz = None
8419        if value.tzinfo:
8420            # this works for zoneinfo.ZoneInfo, pytz.timezone and datetime.datetime.utc to return IANA timezone names like "America/Los_Angeles"
8421            # instead of abbreviations like "PDT". This is for consistency with other timezone handling functions in SQLGlot
8422            tz = Literal.string(str(value.tzinfo))
8423
8424        return TimeStrToTime(this=datetime_literal, zone=tz)
8425    if isinstance(value, datetime.date):
8426        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
8427        return DateStrToDate(this=date_literal)
8428    if isinstance(value, tuple):
8429        if hasattr(value, "_fields"):
8430            return Struct(
8431                expressions=[
8432                    PropertyEQ(
8433                        this=to_identifier(k), expression=convert(getattr(value, k), copy=copy)
8434                    )
8435                    for k in value._fields
8436                ]
8437            )
8438        return Tuple(expressions=[convert(v, copy=copy) for v in value])
8439    if isinstance(value, list):
8440        return Array(expressions=[convert(v, copy=copy) for v in value])
8441    if isinstance(value, dict):
8442        return Map(
8443            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
8444            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
8445        )
8446    if hasattr(value, "__dict__"):
8447        return Struct(
8448            expressions=[
8449                PropertyEQ(this=to_identifier(k), expression=convert(v, copy=copy))
8450                for k, v in value.__dict__.items()
8451            ]
8452        )
8453    raise ValueError(f"Cannot convert {value}")

Convert a python value into an expression object.

Raises an error if a conversion is not possible.

Arguments:
  • value: A python object.
  • copy: Whether to copy value (only applies to Expressions and collections).
Returns:

The equivalent expression object.

def replace_children( expression: Expression, fun: Callable, *args, **kwargs) -> None:
8456def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
8457    """
8458    Replace children of an expression with the result of a lambda fun(child) -> exp.
8459    """
8460    for k, v in tuple(expression.args.items()):
8461        is_list_arg = type(v) is list
8462
8463        child_nodes = v if is_list_arg else [v]
8464        new_child_nodes = []
8465
8466        for cn in child_nodes:
8467            if isinstance(cn, Expression):
8468                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
8469                    new_child_nodes.append(child_node)
8470            else:
8471                new_child_nodes.append(cn)
8472
8473        expression.set(k, new_child_nodes if is_list_arg else seq_get(new_child_nodes, 0))

Replace children of an expression with the result of a lambda fun(child) -> exp.

def replace_tree( expression: Expression, fun: Callable, prune: Optional[Callable[[Expression], bool]] = None) -> Expression:
8476def replace_tree(
8477    expression: Expression,
8478    fun: t.Callable,
8479    prune: t.Optional[t.Callable[[Expression], bool]] = None,
8480) -> Expression:
8481    """
8482    Replace an entire tree with the result of function calls on each node.
8483
8484    This will be traversed in reverse dfs, so leaves first.
8485    If new nodes are created as a result of function calls, they will also be traversed.
8486    """
8487    stack = list(expression.dfs(prune=prune))
8488
8489    while stack:
8490        node = stack.pop()
8491        new_node = fun(node)
8492
8493        if new_node is not node:
8494            node.replace(new_node)
8495
8496            if isinstance(new_node, Expression):
8497                stack.append(new_node)
8498
8499    return new_node

Replace an entire tree with the result of function calls on each node.

This will be traversed in reverse dfs, so leaves first. If new nodes are created as a result of function calls, they will also be traversed.

def column_table_names( expression: Expression, exclude: str = '') -> Set[str]:
8502def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
8503    """
8504    Return all table names referenced through columns in an expression.
8505
8506    Example:
8507        >>> import sqlglot
8508        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
8509        ['a', 'c']
8510
8511    Args:
8512        expression: expression to find table names.
8513        exclude: a table name to exclude
8514
8515    Returns:
8516        A list of unique names.
8517    """
8518    return {
8519        table
8520        for table in (column.table for column in expression.find_all(Column))
8521        if table and table != exclude
8522    }

Return all table names referenced through columns in an expression.

Example:
>>> import sqlglot
>>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
['a', 'c']
Arguments:
  • expression: expression to find table names.
  • exclude: a table name to exclude
Returns:

A list of unique names.

def table_name( table: Table | str, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, identify: bool = False) -> str:
8525def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
8526    """Get the full name of a table as a string.
8527
8528    Args:
8529        table: Table expression node or string.
8530        dialect: The dialect to generate the table name for.
8531        identify: Determines when an identifier should be quoted. Possible values are:
8532            False (default): Never quote, except in cases where it's mandatory by the dialect.
8533            True: Always quote.
8534
8535    Examples:
8536        >>> from sqlglot import exp, parse_one
8537        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
8538        'a.b.c'
8539
8540    Returns:
8541        The table name.
8542    """
8543
8544    table = maybe_parse(table, into=Table, dialect=dialect)
8545
8546    if not table:
8547        raise ValueError(f"Cannot parse {table}")
8548
8549    return ".".join(
8550        (
8551            part.sql(dialect=dialect, identify=True, copy=False, comments=False)
8552            if identify or not SAFE_IDENTIFIER_RE.match(part.name)
8553            else part.name
8554        )
8555        for part in table.parts
8556    )

Get the full name of a table as a string.

Arguments:
  • table: Table expression node or string.
  • dialect: The dialect to generate the table name for.
  • identify: Determines when an identifier should be quoted. Possible values are: False (default): Never quote, except in cases where it's mandatory by the dialect. True: Always quote.
Examples:
>>> from sqlglot import exp, parse_one
>>> table_name(parse_one("select * from a.b.c").find(exp.Table))
'a.b.c'
Returns:

The table name.

def normalize_table_name( table: str | Table, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True) -> str:
8559def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
8560    """Returns a case normalized table name without quotes.
8561
8562    Args:
8563        table: the table to normalize
8564        dialect: the dialect to use for normalization rules
8565        copy: whether to copy the expression.
8566
8567    Examples:
8568        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
8569        'A-B.c'
8570    """
8571    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
8572
8573    return ".".join(
8574        p.name
8575        for p in normalize_identifiers(
8576            to_table(table, dialect=dialect, copy=copy), dialect=dialect
8577        ).parts
8578    )

Returns a case normalized table name without quotes.

Arguments:
  • table: the table to normalize
  • dialect: the dialect to use for normalization rules
  • copy: whether to copy the expression.
Examples:
>>> normalize_table_name("`A-B`.c", dialect="bigquery")
'A-B.c'
def replace_tables( expression: ~E, mapping: Dict[str, str], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True) -> ~E:
8581def replace_tables(
8582    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
8583) -> E:
8584    """Replace all tables in expression according to the mapping.
8585
8586    Args:
8587        expression: expression node to be transformed and replaced.
8588        mapping: mapping of table names.
8589        dialect: the dialect of the mapping table
8590        copy: whether to copy the expression.
8591
8592    Examples:
8593        >>> from sqlglot import exp, parse_one
8594        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
8595        'SELECT * FROM c /* a.b */'
8596
8597    Returns:
8598        The mapped expression.
8599    """
8600
8601    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
8602
8603    def _replace_tables(node: Expression) -> Expression:
8604        if isinstance(node, Table) and node.meta.get("replace") is not False:
8605            original = normalize_table_name(node, dialect=dialect)
8606            new_name = mapping.get(original)
8607
8608            if new_name:
8609                table = to_table(
8610                    new_name,
8611                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
8612                    dialect=dialect,
8613                )
8614                table.add_comments([original])
8615                return table
8616        return node
8617
8618    return expression.transform(_replace_tables, copy=copy)  # type: ignore

Replace all tables in expression according to the mapping.

Arguments:
  • expression: expression node to be transformed and replaced.
  • mapping: mapping of table names.
  • dialect: the dialect of the mapping table
  • copy: whether to copy the expression.
Examples:
>>> from sqlglot import exp, parse_one
>>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
'SELECT * FROM c /* a.b */'
Returns:

The mapped expression.

def replace_placeholders( expression: Expression, *args, **kwargs) -> Expression:
8621def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
8622    """Replace placeholders in an expression.
8623
8624    Args:
8625        expression: expression node to be transformed and replaced.
8626        args: positional names that will substitute unnamed placeholders in the given order.
8627        kwargs: keyword arguments that will substitute named placeholders.
8628
8629    Examples:
8630        >>> from sqlglot import exp, parse_one
8631        >>> replace_placeholders(
8632        ...     parse_one("select * from :tbl where ? = ?"),
8633        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
8634        ... ).sql()
8635        "SELECT * FROM foo WHERE str_col = 'b'"
8636
8637    Returns:
8638        The mapped expression.
8639    """
8640
8641    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
8642        if isinstance(node, Placeholder):
8643            if node.this:
8644                new_name = kwargs.get(node.this)
8645                if new_name is not None:
8646                    return convert(new_name)
8647            else:
8648                try:
8649                    return convert(next(args))
8650                except StopIteration:
8651                    pass
8652        return node
8653
8654    return expression.transform(_replace_placeholders, iter(args), **kwargs)

Replace placeholders in an expression.

Arguments:
  • expression: expression node to be transformed and replaced.
  • args: positional names that will substitute unnamed placeholders in the given order.
  • kwargs: keyword arguments that will substitute named placeholders.
Examples:
>>> from sqlglot import exp, parse_one
>>> replace_placeholders(
...     parse_one("select * from :tbl where ? = ?"),
...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
... ).sql()
"SELECT * FROM foo WHERE str_col = 'b'"
Returns:

The mapped expression.

def expand( expression: Expression, sources: Dict[str, Union[Query, Callable[[], Query]]], dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, copy: bool = True) -> Expression:
8657def expand(
8658    expression: Expression,
8659    sources: t.Dict[str, Query | t.Callable[[], Query]],
8660    dialect: DialectType = None,
8661    copy: bool = True,
8662) -> Expression:
8663    """Transforms an expression by expanding all referenced sources into subqueries.
8664
8665    Examples:
8666        >>> from sqlglot import parse_one
8667        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
8668        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
8669
8670        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
8671        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
8672
8673    Args:
8674        expression: The expression to expand.
8675        sources: A dict of name to query or a callable that provides a query on demand.
8676        dialect: The dialect of the sources dict or the callable.
8677        copy: Whether to copy the expression during transformation. Defaults to True.
8678
8679    Returns:
8680        The transformed expression.
8681    """
8682    normalized_sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
8683
8684    def _expand(node: Expression):
8685        if isinstance(node, Table):
8686            name = normalize_table_name(node, dialect=dialect)
8687            source = normalized_sources.get(name)
8688
8689            if source:
8690                # Create a subquery with the same alias (or table name if no alias)
8691                parsed_source = source() if callable(source) else source
8692                subquery = parsed_source.subquery(node.alias or name)
8693                subquery.comments = [f"source: {name}"]
8694
8695                # Continue expanding within the subquery
8696                return subquery.transform(_expand, copy=False)
8697
8698        return node
8699
8700    return expression.transform(_expand, copy=copy)

Transforms an expression by expanding all referenced sources into subqueries.

Examples:
>>> from sqlglot import parse_one
>>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
>>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
Arguments:
  • expression: The expression to expand.
  • sources: A dict of name to query or a callable that provides a query on demand.
  • dialect: The dialect of the sources dict or the callable.
  • copy: Whether to copy the expression during transformation. Defaults to True.
Returns:

The transformed expression.

def func( name: str, *args, copy: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **kwargs) -> Func:
8703def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
8704    """
8705    Returns a Func expression.
8706
8707    Examples:
8708        >>> func("abs", 5).sql()
8709        'ABS(5)'
8710
8711        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
8712        'CAST(5 AS DOUBLE)'
8713
8714    Args:
8715        name: the name of the function to build.
8716        args: the args used to instantiate the function of interest.
8717        copy: whether to copy the argument expressions.
8718        dialect: the source dialect.
8719        kwargs: the kwargs used to instantiate the function of interest.
8720
8721    Note:
8722        The arguments `args` and `kwargs` are mutually exclusive.
8723
8724    Returns:
8725        An instance of the function of interest, or an anonymous function, if `name` doesn't
8726        correspond to an existing `sqlglot.expressions.Func` class.
8727    """
8728    if args and kwargs:
8729        raise ValueError("Can't use both args and kwargs to instantiate a function.")
8730
8731    from sqlglot.dialects.dialect import Dialect
8732
8733    dialect = Dialect.get_or_raise(dialect)
8734
8735    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
8736    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
8737
8738    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
8739    if constructor:
8740        if converted:
8741            if "dialect" in constructor.__code__.co_varnames:
8742                function = constructor(converted, dialect=dialect)
8743            else:
8744                function = constructor(converted)
8745        elif constructor.__name__ == "from_arg_list":
8746            function = constructor.__self__(**kwargs)  # type: ignore
8747        else:
8748            constructor = FUNCTION_BY_NAME.get(name.upper())
8749            if constructor:
8750                function = constructor(**kwargs)
8751            else:
8752                raise ValueError(
8753                    f"Unable to convert '{name}' into a Func. Either manually construct "
8754                    "the Func expression of interest or parse the function call."
8755                )
8756    else:
8757        kwargs = kwargs or {"expressions": converted}
8758        function = Anonymous(this=name, **kwargs)
8759
8760    for error_message in function.error_messages(converted):
8761        raise ValueError(error_message)
8762
8763    return function

Returns a Func expression.

Examples:
>>> func("abs", 5).sql()
'ABS(5)'
>>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
'CAST(5 AS DOUBLE)'
Arguments:
  • name: the name of the function to build.
  • args: the args used to instantiate the function of interest.
  • copy: whether to copy the argument expressions.
  • dialect: the source dialect.
  • kwargs: the kwargs used to instantiate the function of interest.
Note:

The arguments args and kwargs are mutually exclusive.

Returns:

An instance of the function of interest, or an anonymous function, if name doesn't correspond to an existing sqlglot.expressions.Func class.

def case( expression: Union[str, Expression, NoneType] = None, **opts) -> Case:
8766def case(
8767    expression: t.Optional[ExpOrStr] = None,
8768    **opts,
8769) -> Case:
8770    """
8771    Initialize a CASE statement.
8772
8773    Example:
8774        case().when("a = 1", "foo").else_("bar")
8775
8776    Args:
8777        expression: Optionally, the input expression (not all dialects support this)
8778        **opts: Extra keyword arguments for parsing `expression`
8779    """
8780    if expression is not None:
8781        this = maybe_parse(expression, **opts)
8782    else:
8783        this = None
8784    return Case(this=this, ifs=[])

Initialize a CASE statement.

Example:

case().when("a = 1", "foo").else_("bar")

Arguments:
  • expression: Optionally, the input expression (not all dialects support this)
  • **opts: Extra keyword arguments for parsing expression
def array( *expressions: Union[str, Expression], copy: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **kwargs) -> Array:
8787def array(
8788    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8789) -> Array:
8790    """
8791    Returns an array.
8792
8793    Examples:
8794        >>> array(1, 'x').sql()
8795        'ARRAY(1, x)'
8796
8797    Args:
8798        expressions: the expressions to add to the array.
8799        copy: whether to copy the argument expressions.
8800        dialect: the source dialect.
8801        kwargs: the kwargs used to instantiate the function of interest.
8802
8803    Returns:
8804        An array expression.
8805    """
8806    return Array(
8807        expressions=[
8808            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8809            for expression in expressions
8810        ]
8811    )

Returns an array.

Examples:
>>> array(1, 'x').sql()
'ARRAY(1, x)'
Arguments:
  • expressions: the expressions to add to the array.
  • copy: whether to copy the argument expressions.
  • dialect: the source dialect.
  • kwargs: the kwargs used to instantiate the function of interest.
Returns:

An array expression.

def tuple_( *expressions: Union[str, Expression], copy: bool = True, dialect: Union[str, sqlglot.dialects.Dialect, Type[sqlglot.dialects.Dialect], NoneType] = None, **kwargs) -> Tuple:
8814def tuple_(
8815    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8816) -> Tuple:
8817    """
8818    Returns an tuple.
8819
8820    Examples:
8821        >>> tuple_(1, 'x').sql()
8822        '(1, x)'
8823
8824    Args:
8825        expressions: the expressions to add to the tuple.
8826        copy: whether to copy the argument expressions.
8827        dialect: the source dialect.
8828        kwargs: the kwargs used to instantiate the function of interest.
8829
8830    Returns:
8831        A tuple expression.
8832    """
8833    return Tuple(
8834        expressions=[
8835            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8836            for expression in expressions
8837        ]
8838    )

Returns an tuple.

Examples:
>>> tuple_(1, 'x').sql()
'(1, x)'
Arguments:
  • expressions: the expressions to add to the tuple.
  • copy: whether to copy the argument expressions.
  • dialect: the source dialect.
  • kwargs: the kwargs used to instantiate the function of interest.
Returns:

A tuple expression.

def true() -> Boolean:
8841def true() -> Boolean:
8842    """
8843    Returns a true Boolean expression.
8844    """
8845    return Boolean(this=True)

Returns a true Boolean expression.

def false() -> Boolean:
8848def false() -> Boolean:
8849    """
8850    Returns a false Boolean expression.
8851    """
8852    return Boolean(this=False)

Returns a false Boolean expression.

def null() -> Null:
8855def null() -> Null:
8856    """
8857    Returns a Null expression.
8858    """
8859    return Null()

Returns a Null expression.

NONNULL_CONSTANTS = (<class 'Literal'>, <class 'Boolean'>)
CONSTANTS = (<class 'Literal'>, <class 'Boolean'>, <class 'Null'>)