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

Retrieves the argument with key "this".

expression: Any
139    @property
140    def expression(self) -> t.Any:
141        """
142        Retrieves the argument with key "expression".
143        """
144        return self.args.get("expression")

Retrieves the argument with key "expression".

expressions: List[Any]
146    @property
147    def expressions(self) -> t.List[t.Any]:
148        """
149        Retrieves the argument with key "expressions".
150        """
151        return self.args.get("expressions") or []

Retrieves the argument with key "expressions".

def text(self, key) -> str:
153    def text(self, key) -> str:
154        """
155        Returns a textual representation of the argument corresponding to "key". This can only be used
156        for args that are strings or leaf Expression instances, such as identifiers and literals.
157        """
158        field = self.args.get(key)
159        if isinstance(field, str):
160            return field
161        if isinstance(field, (Identifier, Literal, Var)):
162            return field.this
163        if isinstance(field, (Star, Null)):
164            return field.name
165        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
167    @property
168    def is_string(self) -> bool:
169        """
170        Checks whether a Literal expression is a string.
171        """
172        return isinstance(self, Literal) and self.args["is_string"]

Checks whether a Literal expression is a string.

is_number: bool
174    @property
175    def is_number(self) -> bool:
176        """
177        Checks whether a Literal expression is a number.
178        """
179        return (isinstance(self, Literal) and not self.args["is_string"]) or (
180            isinstance(self, Neg) and self.this.is_number
181        )

Checks whether a Literal expression is a number.

def to_py(self) -> Any:
183    def to_py(self) -> t.Any:
184        """
185        Returns a Python object equivalent of the SQL node.
186        """
187        raise ValueError(f"{self} cannot be converted to a Python object.")

Returns a Python object equivalent of the SQL node.

is_int: bool
189    @property
190    def is_int(self) -> bool:
191        """
192        Checks whether an expression is an integer.
193        """
194        return self.is_number and isinstance(self.to_py(), int)

Checks whether an expression is an integer.

is_star: bool
196    @property
197    def is_star(self) -> bool:
198        """Checks whether an expression is a star."""
199        return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star))

Checks whether an expression is a star.

alias: str
201    @property
202    def alias(self) -> str:
203        """
204        Returns the alias of the expression, or an empty string if it's not aliased.
205        """
206        if isinstance(self.args.get("alias"), TableAlias):
207            return self.args["alias"].name
208        return self.text("alias")

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

alias_column_names: List[str]
210    @property
211    def alias_column_names(self) -> t.List[str]:
212        table_alias = self.args.get("alias")
213        if not table_alias:
214            return []
215        return [c.name for c in table_alias.args.get("columns") or []]
name: str
217    @property
218    def name(self) -> str:
219        return self.text("this")
alias_or_name: str
221    @property
222    def alias_or_name(self) -> str:
223        return self.alias or self.name
output_name: str
225    @property
226    def output_name(self) -> str:
227        """
228        Name of the output column if this expression is a selection.
229
230        If the Expression has no output name, an empty string is returned.
231
232        Example:
233            >>> from sqlglot import parse_one
234            >>> parse_one("SELECT a").expressions[0].output_name
235            'a'
236            >>> parse_one("SELECT b AS c").expressions[0].output_name
237            'c'
238            >>> parse_one("SELECT 1 + 2").expressions[0].output_name
239            ''
240        """
241        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]
243    @property
244    def type(self) -> t.Optional[DataType]:
245        return self._type
def is_type(self, *dtypes) -> bool:
253    def is_type(self, *dtypes) -> bool:
254        return self.type is not None and self.type.is_type(*dtypes)
def is_leaf(self) -> bool:
256    def is_leaf(self) -> bool:
257        return not any(isinstance(v, (Expression, list)) for v in self.args.values())
meta: Dict[str, Any]
259    @property
260    def meta(self) -> t.Dict[str, t.Any]:
261        if self._meta is None:
262            self._meta = {}
263        return self._meta
def copy(self):
299    def copy(self):
300        """
301        Returns a deep copy of the expression.
302        """
303        return deepcopy(self)

Returns a deep copy of the expression.

def add_comments(self, comments: Optional[List[str]] = None) -> None:
305    def add_comments(self, comments: t.Optional[t.List[str]] = None) -> None:
306        if self.comments is None:
307            self.comments = []
308
309        if comments:
310            for comment in comments:
311                _, *meta = comment.split(SQLGLOT_META)
312                if meta:
313                    for kv in "".join(meta).split(","):
314                        k, *v = kv.split("=")
315                        value = v[0].strip() if v else True
316                        self.meta[k.strip()] = value
317                self.comments.append(comment)
def pop_comments(self) -> List[str]:
319    def pop_comments(self) -> t.List[str]:
320        comments = self.comments or []
321        self.comments = None
322        return comments
def append(self, arg_key: str, value: Any) -> None:
324    def append(self, arg_key: str, value: t.Any) -> None:
325        """
326        Appends value to arg_key if it's a list or sets it as a new list.
327
328        Args:
329            arg_key (str): name of the list expression arg
330            value (Any): value to append to the list
331        """
332        if type(self.args.get(arg_key)) is not list:
333            self.args[arg_key] = []
334        self._set_parent(arg_key, value)
335        values = self.args[arg_key]
336        if hasattr(value, "parent"):
337            value.index = len(values)
338        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:
340    def set(
341        self,
342        arg_key: str,
343        value: t.Any,
344        index: t.Optional[int] = None,
345        overwrite: bool = True,
346    ) -> None:
347        """
348        Sets arg_key to value.
349
350        Args:
351            arg_key: name of the expression arg.
352            value: value to set the arg to.
353            index: if the arg is a list, this specifies what position to add the value in it.
354            overwrite: assuming an index is given, this determines whether to overwrite the
355                list entry instead of only inserting a new value (i.e., like list.insert).
356        """
357        if index is not None:
358            expressions = self.args.get(arg_key) or []
359
360            if seq_get(expressions, index) is None:
361                return
362            if value is None:
363                expressions.pop(index)
364                for v in expressions[index:]:
365                    v.index = v.index - 1
366                return
367
368            if isinstance(value, list):
369                expressions.pop(index)
370                expressions[index:index] = value
371            elif overwrite:
372                expressions[index] = value
373            else:
374                expressions.insert(index, value)
375
376            value = expressions
377        elif value is None:
378            self.args.pop(arg_key, None)
379            return
380
381        self.args[arg_key] = value
382        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
396    @property
397    def depth(self) -> int:
398        """
399        Returns the depth of this tree.
400        """
401        if self.parent:
402            return self.parent.depth + 1
403        return 0

Returns the depth of this tree.

def iter_expressions(self, reverse: bool = False) -> Iterator[Expression]:
405    def iter_expressions(self, reverse: bool = False) -> t.Iterator[Expression]:
406        """Yields the key and expression for all arguments, exploding list args."""
407        # remove tuple when python 3.7 is deprecated
408        for vs in reversed(tuple(self.args.values())) if reverse else self.args.values():  # type: ignore
409            if type(vs) is list:
410                for v in reversed(vs) if reverse else vs:  # type: ignore
411                    if hasattr(v, "parent"):
412                        yield v
413            else:
414                if hasattr(vs, "parent"):
415                    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]:
417    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
418        """
419        Returns the first node in this tree which matches at least one of
420        the specified types.
421
422        Args:
423            expression_types: the expression type(s) to match.
424            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
425
426        Returns:
427            The node which matches the criteria or None if no such node was found.
428        """
429        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]:
431    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
432        """
433        Returns a generator object which visits all nodes in this tree and only
434        yields those that match at least one of the specified expression types.
435
436        Args:
437            expression_types: the expression type(s) to match.
438            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
439
440        Returns:
441            The generator object.
442        """
443        for expression in self.walk(bfs=bfs):
444            if isinstance(expression, expression_types):
445                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]:
447    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
448        """
449        Returns a nearest parent matching expression_types.
450
451        Args:
452            expression_types: the expression type(s) to match.
453
454        Returns:
455            The parent node.
456        """
457        ancestor = self.parent
458        while ancestor and not isinstance(ancestor, expression_types):
459            ancestor = ancestor.parent
460        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]
462    @property
463    def parent_select(self) -> t.Optional[Select]:
464        """
465        Returns the parent select statement.
466        """
467        return self.find_ancestor(Select)

Returns the parent select statement.

same_parent: bool
469    @property
470    def same_parent(self) -> bool:
471        """Returns if the parent is the same class as itself."""
472        return type(self.parent) is self.__class__

Returns if the parent is the same class as itself.

def root(self) -> Expression:
474    def root(self) -> Expression:
475        """
476        Returns the root expression of this tree.
477        """
478        expression = self
479        while expression.parent:
480            expression = expression.parent
481        return expression

Returns the root expression of this tree.

def walk( self, bfs: bool = True, prune: Optional[Callable[[Expression], bool]] = None) -> Iterator[Expression]:
483    def walk(
484        self, bfs: bool = True, prune: t.Optional[t.Callable[[Expression], bool]] = None
485    ) -> t.Iterator[Expression]:
486        """
487        Returns a generator object which visits all nodes in this tree.
488
489        Args:
490            bfs: if set to True the BFS traversal order will be applied,
491                otherwise the DFS traversal will be used instead.
492            prune: callable that returns True if the generator should stop traversing
493                this branch of the tree.
494
495        Returns:
496            the generator object.
497        """
498        if bfs:
499            yield from self.bfs(prune=prune)
500        else:
501            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]:
503    def dfs(
504        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
505    ) -> t.Iterator[Expression]:
506        """
507        Returns a generator object which visits all nodes in this tree in
508        the DFS (Depth-first) order.
509
510        Returns:
511            The generator object.
512        """
513        stack = [self]
514
515        while stack:
516            node = stack.pop()
517
518            yield node
519
520            if prune and prune(node):
521                continue
522
523            for v in node.iter_expressions(reverse=True):
524                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]:
526    def bfs(
527        self, prune: t.Optional[t.Callable[[Expression], bool]] = None
528    ) -> t.Iterator[Expression]:
529        """
530        Returns a generator object which visits all nodes in this tree in
531        the BFS (Breadth-first) order.
532
533        Returns:
534            The generator object.
535        """
536        queue = deque([self])
537
538        while queue:
539            node = queue.popleft()
540
541            yield node
542
543            if prune and prune(node):
544                continue
545
546            for v in node.iter_expressions():
547                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):
549    def unnest(self):
550        """
551        Returns the first non parenthesis child or self.
552        """
553        expression = self
554        while type(expression) is Paren:
555            expression = expression.this
556        return expression

Returns the first non parenthesis child or self.

def unalias(self):
558    def unalias(self):
559        """
560        Returns the inner expression if this is an Alias.
561        """
562        if isinstance(self, Alias):
563            return self.this
564        return self

Returns the inner expression if this is an Alias.

def unnest_operands(self):
566    def unnest_operands(self):
567        """
568        Returns unnested operands as a tuple.
569        """
570        return tuple(arg.unnest() for arg in self.iter_expressions())

Returns unnested operands as a tuple.

def flatten(self, unnest=True):
572    def flatten(self, unnest=True):
573        """
574        Returns a generator which yields child nodes whose parents are the same class.
575
576        A AND B AND C -> [A, B, C]
577        """
578        for node in self.dfs(prune=lambda n: n.parent and type(n) is not self.__class__):
579            if type(node) is not self.__class__:
580                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:
588    def to_s(self) -> str:
589        """
590        Same as __repr__, but includes additional information which can be useful
591        for debugging, like empty or missing args and the AST nodes' object IDs.
592        """
593        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> str:
595    def sql(self, dialect: DialectType = None, **opts) -> str:
596        """
597        Returns SQL string representation of this tree.
598
599        Args:
600            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
601            opts: other `sqlglot.generator.Generator` options.
602
603        Returns:
604            The SQL string.
605        """
606        from sqlglot.dialects import Dialect
607
608        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:
610    def transform(self, fun: t.Callable, *args: t.Any, copy: bool = True, **kwargs) -> Expression:
611        """
612        Visits all tree nodes (excluding already transformed ones)
613        and applies the given transformation function to each node.
614
615        Args:
616            fun: a function which takes a node as an argument and returns a
617                new transformed node or the same node without modifications. If the function
618                returns None, then the corresponding node will be removed from the syntax tree.
619            copy: if set to True a new tree instance is constructed, otherwise the tree is
620                modified in place.
621
622        Returns:
623            The transformed tree.
624        """
625        root = None
626        new_node = None
627
628        for node in (self.copy() if copy else self).dfs(prune=lambda n: n is not new_node):
629            parent, arg_key, index = node.parent, node.arg_key, node.index
630            new_node = fun(node, *args, **kwargs)
631
632            if not root:
633                root = new_node
634            elif new_node is not node:
635                parent.set(arg_key, new_node, index)
636
637        assert root
638        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):
646    def replace(self, expression):
647        """
648        Swap out this expression with a new expression.
649
650        For example::
651
652            >>> tree = Select().select("x").from_("tbl")
653            >>> tree.find(Column).replace(column("y"))
654            Column(
655              this=Identifier(this=y, quoted=False))
656            >>> tree.sql()
657            'SELECT y FROM tbl'
658
659        Args:
660            expression: new node
661
662        Returns:
663            The new expression or expressions.
664        """
665        parent = self.parent
666
667        if not parent or parent is expression:
668            return expression
669
670        key = self.arg_key
671        value = parent.args.get(key)
672
673        if type(expression) is list and isinstance(value, Expression):
674            # We are trying to replace an Expression with a list, so it's assumed that
675            # the intention was to really replace the parent of this expression.
676            value.parent.replace(expression)
677        else:
678            parent.set(key, expression, self.index)
679
680        if expression is not self:
681            self.parent = None
682            self.arg_key = None
683            self.index = None
684
685        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:
687    def pop(self: E) -> E:
688        """
689        Remove this expression from its AST.
690
691        Returns:
692            The popped expression.
693        """
694        self.replace(None)
695        return self

Remove this expression from its AST.

Returns:

The popped expression.

def assert_is(self, type_: Type[~E]) -> ~E:
697    def assert_is(self, type_: t.Type[E]) -> E:
698        """
699        Assert that this `Expression` is an instance of `type_`.
700
701        If it is NOT an instance of `type_`, this raises an assertion error.
702        Otherwise, this returns this expression.
703
704        Examples:
705            This is useful for type security in chained expressions:
706
707            >>> import sqlglot
708            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
709            'SELECT x, z FROM y'
710        """
711        if not isinstance(self, type_):
712            raise AssertionError(f"{self} is not {type_}.")
713        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]:
715    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
716        """
717        Checks if this expression is valid (e.g. all mandatory args are set).
718
719        Args:
720            args: a sequence of values that were used to instantiate a Func expression. This is used
721                to check that the provided arguments don't exceed the function argument limit.
722
723        Returns:
724            A list of error messages for all possible errors that were found.
725        """
726        errors: t.List[str] = []
727
728        for k in self.args:
729            if k not in self.arg_types:
730                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
731        for k, mandatory in self.arg_types.items():
732            v = self.args.get(k)
733            if mandatory and (v is None or (isinstance(v, list) and not v)):
734                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
735
736        if (
737            args
738            and isinstance(self, Func)
739            and len(args) > len(self.arg_types)
740            and not self.is_var_len_args
741        ):
742            errors.append(
743                f"The number of provided arguments ({len(args)}) is greater than "
744                f"the maximum number of supported arguments ({len(self.arg_types)})"
745            )
746
747        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):
749    def dump(self):
750        """
751        Dump this Expression to a JSON-serializable dict.
752        """
753        from sqlglot.serde import dump
754
755        return dump(self)

Dump this Expression to a JSON-serializable dict.

@classmethod
def load(cls, obj):
757    @classmethod
758    def load(cls, obj):
759        """
760        Load a dict (as returned by `Expression.dump`) into an Expression instance.
761        """
762        from sqlglot.serde import load
763
764        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
766    def and_(
767        self,
768        *expressions: t.Optional[ExpOrStr],
769        dialect: DialectType = None,
770        copy: bool = True,
771        **opts,
772    ) -> Condition:
773        """
774        AND this condition with one or multiple expressions.
775
776        Example:
777            >>> condition("x=1").and_("y=1").sql()
778            'x = 1 AND y = 1'
779
780        Args:
781            *expressions: the SQL code strings to parse.
782                If an `Expression` instance is passed, it will be used as-is.
783            dialect: the dialect used to parse the input expression.
784            copy: whether to copy the involved expressions (only applies to Expressions).
785            opts: other options to use to parse the input expressions.
786
787        Returns:
788            The new And condition.
789        """
790        return and_(self, *expressions, dialect=dialect, copy=copy, **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).
  • 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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
792    def or_(
793        self,
794        *expressions: t.Optional[ExpOrStr],
795        dialect: DialectType = None,
796        copy: bool = True,
797        **opts,
798    ) -> Condition:
799        """
800        OR this condition with one or multiple expressions.
801
802        Example:
803            >>> condition("x=1").or_("y=1").sql()
804            'x = 1 OR y = 1'
805
806        Args:
807            *expressions: the SQL code strings to parse.
808                If an `Expression` instance is passed, it will be used as-is.
809            dialect: the dialect used to parse the input expression.
810            copy: whether to copy the involved expressions (only applies to Expressions).
811            opts: other options to use to parse the input expressions.
812
813        Returns:
814            The new Or condition.
815        """
816        return or_(self, *expressions, dialect=dialect, copy=copy, **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).
  • opts: other options to use to parse the input expressions.
Returns:

The new Or condition.

def not_(self, copy: bool = True):
818    def not_(self, copy: bool = True):
819        """
820        Wrap this condition with NOT.
821
822        Example:
823            >>> condition("x=1").not_().sql()
824            'NOT x = 1'
825
826        Args:
827            copy: whether to copy this object.
828
829        Returns:
830            The new Not instance.
831        """
832        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 as_( self, alias: str | Identifier, quoted: Optional[bool] = None, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Alias:
834    def as_(
835        self,
836        alias: str | Identifier,
837        quoted: t.Optional[bool] = None,
838        dialect: DialectType = None,
839        copy: bool = True,
840        **opts,
841    ) -> Alias:
842        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:
867    def isin(
868        self,
869        *expressions: t.Any,
870        query: t.Optional[ExpOrStr] = None,
871        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
872        copy: bool = True,
873        **opts,
874    ) -> In:
875        subquery = maybe_parse(query, copy=copy, **opts) if query else None
876        if subquery and not isinstance(subquery, Subquery):
877            subquery = subquery.subquery(copy=False)
878
879        return In(
880            this=maybe_copy(self, copy),
881            expressions=[convert(e, copy=copy) for e in expressions],
882            query=subquery,
883            unnest=(
884                Unnest(
885                    expressions=[
886                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
887                        for e in ensure_list(unnest)
888                    ]
889                )
890                if unnest
891                else None
892            ),
893        )
def between( self, low: Any, high: Any, copy: bool = True, **opts) -> Between:
895    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
896        return Between(
897            this=maybe_copy(self, copy),
898            low=convert(low, copy=copy, **opts),
899            high=convert(high, copy=copy, **opts),
900        )
def is_( self, other: Union[str, Expression]) -> Is:
902    def is_(self, other: ExpOrStr) -> Is:
903        return self._binop(Is, other)
def like( self, other: Union[str, Expression]) -> Like:
905    def like(self, other: ExpOrStr) -> Like:
906        return self._binop(Like, other)
def ilike( self, other: Union[str, Expression]) -> ILike:
908    def ilike(self, other: ExpOrStr) -> ILike:
909        return self._binop(ILike, other)
def eq(self, other: Any) -> EQ:
911    def eq(self, other: t.Any) -> EQ:
912        return self._binop(EQ, other)
def neq(self, other: Any) -> NEQ:
914    def neq(self, other: t.Any) -> NEQ:
915        return self._binop(NEQ, other)
def rlike( self, other: Union[str, Expression]) -> RegexpLike:
917    def rlike(self, other: ExpOrStr) -> RegexpLike:
918        return self._binop(RegexpLike, other)
def div( self, other: Union[str, Expression], typed: bool = False, safe: bool = False) -> Div:
920    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
921        div = self._binop(Div, other)
922        div.args["typed"] = typed
923        div.args["safe"] = safe
924        return div
def asc(self, nulls_first: bool = True) -> Ordered:
926    def asc(self, nulls_first: bool = True) -> Ordered:
927        return Ordered(this=self.copy(), nulls_first=nulls_first)
def desc(self, nulls_first: bool = False) -> Ordered:
929    def desc(self, nulls_first: bool = False) -> Ordered:
930        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):
1013class Condition(Expression):
1014    """Logical conditions like x AND y, or simply x"""

Logical conditions like x AND y, or simply x

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

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

key = 'predicate'
class DerivedTable(Expression):
1021class DerivedTable(Expression):
1022    @property
1023    def selects(self) -> t.List[Expression]:
1024        return self.this.selects if isinstance(self.this, Query) else []
1025
1026    @property
1027    def named_selects(self) -> t.List[str]:
1028        return [select.output_name for select in self.selects]
selects: List[Expression]
1022    @property
1023    def selects(self) -> t.List[Expression]:
1024        return self.this.selects if isinstance(self.this, Query) else []
named_selects: List[str]
1026    @property
1027    def named_selects(self) -> t.List[str]:
1028        return [select.output_name for select in self.selects]
key = 'derivedtable'
class Query(Expression):
1031class Query(Expression):
1032    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
1033        """
1034        Returns a `Subquery` that wraps around this query.
1035
1036        Example:
1037            >>> subquery = Select().select("x").from_("tbl").subquery()
1038            >>> Select().select("x").from_(subquery).sql()
1039            'SELECT x FROM (SELECT x FROM tbl)'
1040
1041        Args:
1042            alias: an optional alias for the subquery.
1043            copy: if `False`, modify this expression instance in-place.
1044        """
1045        instance = maybe_copy(self, copy)
1046        if not isinstance(alias, Expression):
1047            alias = TableAlias(this=to_identifier(alias)) if alias else None
1048
1049        return Subquery(this=instance, alias=alias)
1050
1051    def limit(
1052        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1053    ) -> Q:
1054        """
1055        Adds a LIMIT clause to this query.
1056
1057        Example:
1058            >>> select("1").union(select("1")).limit(1).sql()
1059            'SELECT 1 UNION SELECT 1 LIMIT 1'
1060
1061        Args:
1062            expression: the SQL code string to parse.
1063                This can also be an integer.
1064                If a `Limit` instance is passed, it will be used as-is.
1065                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
1066            dialect: the dialect used to parse the input expression.
1067            copy: if `False`, modify this expression instance in-place.
1068            opts: other options to use to parse the input expressions.
1069
1070        Returns:
1071            A limited Select expression.
1072        """
1073        return _apply_builder(
1074            expression=expression,
1075            instance=self,
1076            arg="limit",
1077            into=Limit,
1078            prefix="LIMIT",
1079            dialect=dialect,
1080            copy=copy,
1081            into_arg="expression",
1082            **opts,
1083        )
1084
1085    def offset(
1086        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1087    ) -> Q:
1088        """
1089        Set the OFFSET expression.
1090
1091        Example:
1092            >>> Select().from_("tbl").select("x").offset(10).sql()
1093            'SELECT x FROM tbl OFFSET 10'
1094
1095        Args:
1096            expression: the SQL code string to parse.
1097                This can also be an integer.
1098                If a `Offset` instance is passed, this is used as-is.
1099                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
1100            dialect: the dialect used to parse the input expression.
1101            copy: if `False`, modify this expression instance in-place.
1102            opts: other options to use to parse the input expressions.
1103
1104        Returns:
1105            The modified Select expression.
1106        """
1107        return _apply_builder(
1108            expression=expression,
1109            instance=self,
1110            arg="offset",
1111            into=Offset,
1112            prefix="OFFSET",
1113            dialect=dialect,
1114            copy=copy,
1115            into_arg="expression",
1116            **opts,
1117        )
1118
1119    def order_by(
1120        self: Q,
1121        *expressions: t.Optional[ExpOrStr],
1122        append: bool = True,
1123        dialect: DialectType = None,
1124        copy: bool = True,
1125        **opts,
1126    ) -> Q:
1127        """
1128        Set the ORDER BY expression.
1129
1130        Example:
1131            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
1132            'SELECT x FROM tbl ORDER BY x DESC'
1133
1134        Args:
1135            *expressions: the SQL code strings to parse.
1136                If a `Group` instance is passed, this is used as-is.
1137                If another `Expression` instance is passed, it will be wrapped in a `Order`.
1138            append: if `True`, add to any existing expressions.
1139                Otherwise, this flattens all the `Order` expression into a single expression.
1140            dialect: the dialect used to parse the input expression.
1141            copy: if `False`, modify this expression instance in-place.
1142            opts: other options to use to parse the input expressions.
1143
1144        Returns:
1145            The modified Select expression.
1146        """
1147        return _apply_child_list_builder(
1148            *expressions,
1149            instance=self,
1150            arg="order",
1151            append=append,
1152            copy=copy,
1153            prefix="ORDER BY",
1154            into=Order,
1155            dialect=dialect,
1156            **opts,
1157        )
1158
1159    @property
1160    def ctes(self) -> t.List[CTE]:
1161        """Returns a list of all the CTEs attached to this query."""
1162        with_ = self.args.get("with")
1163        return with_.expressions if with_ else []
1164
1165    @property
1166    def selects(self) -> t.List[Expression]:
1167        """Returns the query's projections."""
1168        raise NotImplementedError("Query objects must implement `selects`")
1169
1170    @property
1171    def named_selects(self) -> t.List[str]:
1172        """Returns the output names of the query's projections."""
1173        raise NotImplementedError("Query objects must implement `named_selects`")
1174
1175    def select(
1176        self: Q,
1177        *expressions: t.Optional[ExpOrStr],
1178        append: bool = True,
1179        dialect: DialectType = None,
1180        copy: bool = True,
1181        **opts,
1182    ) -> Q:
1183        """
1184        Append to or set the SELECT expressions.
1185
1186        Example:
1187            >>> Select().select("x", "y").sql()
1188            'SELECT x, y'
1189
1190        Args:
1191            *expressions: the SQL code strings to parse.
1192                If an `Expression` instance is passed, it will be used as-is.
1193            append: if `True`, add to any existing expressions.
1194                Otherwise, this resets the expressions.
1195            dialect: the dialect used to parse the input expressions.
1196            copy: if `False`, modify this expression instance in-place.
1197            opts: other options to use to parse the input expressions.
1198
1199        Returns:
1200            The modified Query expression.
1201        """
1202        raise NotImplementedError("Query objects must implement `select`")
1203
1204    def with_(
1205        self: Q,
1206        alias: ExpOrStr,
1207        as_: ExpOrStr,
1208        recursive: t.Optional[bool] = None,
1209        materialized: t.Optional[bool] = None,
1210        append: bool = True,
1211        dialect: DialectType = None,
1212        copy: bool = True,
1213        **opts,
1214    ) -> Q:
1215        """
1216        Append to or set the common table expressions.
1217
1218        Example:
1219            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
1220            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
1221
1222        Args:
1223            alias: the SQL code string to parse as the table name.
1224                If an `Expression` instance is passed, this is used as-is.
1225            as_: the SQL code string to parse as the table expression.
1226                If an `Expression` instance is passed, it will be used as-is.
1227            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1228            materialized: set the MATERIALIZED part of the expression.
1229            append: if `True`, add to any existing expressions.
1230                Otherwise, this resets the expressions.
1231            dialect: the dialect used to parse the input expression.
1232            copy: if `False`, modify this expression instance in-place.
1233            opts: other options to use to parse the input expressions.
1234
1235        Returns:
1236            The modified expression.
1237        """
1238        return _apply_cte_builder(
1239            self,
1240            alias,
1241            as_,
1242            recursive=recursive,
1243            materialized=materialized,
1244            append=append,
1245            dialect=dialect,
1246            copy=copy,
1247            **opts,
1248        )
1249
1250    def union(
1251        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1252    ) -> Union:
1253        """
1254        Builds a UNION expression.
1255
1256        Example:
1257            >>> import sqlglot
1258            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
1259            'SELECT * FROM foo UNION SELECT * FROM bla'
1260
1261        Args:
1262            expressions: the SQL code strings.
1263                If `Expression` instances are passed, they will be used as-is.
1264            distinct: set the DISTINCT flag if and only if this is true.
1265            dialect: the dialect used to parse the input expression.
1266            opts: other options to use to parse the input expressions.
1267
1268        Returns:
1269            The new Union expression.
1270        """
1271        return union(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1272
1273    def intersect(
1274        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1275    ) -> Intersect:
1276        """
1277        Builds an INTERSECT expression.
1278
1279        Example:
1280            >>> import sqlglot
1281            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
1282            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
1283
1284        Args:
1285            expressions: the SQL code strings.
1286                If `Expression` instances are passed, they will be used as-is.
1287            distinct: set the DISTINCT flag if and only if this is true.
1288            dialect: the dialect used to parse the input expression.
1289            opts: other options to use to parse the input expressions.
1290
1291        Returns:
1292            The new Intersect expression.
1293        """
1294        return intersect(self, *expressions, distinct=distinct, dialect=dialect, **opts)
1295
1296    def except_(
1297        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1298    ) -> Except:
1299        """
1300        Builds an EXCEPT expression.
1301
1302        Example:
1303            >>> import sqlglot
1304            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
1305            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
1306
1307        Args:
1308            expressions: the SQL code strings.
1309                If `Expression` instance are passed, they will be used as-is.
1310            distinct: set the DISTINCT flag if and only if this is true.
1311            dialect: the dialect used to parse the input expression.
1312            opts: other options to use to parse the input expressions.
1313
1314        Returns:
1315            The new Except expression.
1316        """
1317        return except_(self, *expressions, distinct=distinct, dialect=dialect, **opts)
def subquery( self, alias: Union[str, Expression, NoneType] = None, copy: bool = True) -> Subquery:
1032    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
1033        """
1034        Returns a `Subquery` that wraps around this query.
1035
1036        Example:
1037            >>> subquery = Select().select("x").from_("tbl").subquery()
1038            >>> Select().select("x").from_(subquery).sql()
1039            'SELECT x FROM (SELECT x FROM tbl)'
1040
1041        Args:
1042            alias: an optional alias for the subquery.
1043            copy: if `False`, modify this expression instance in-place.
1044        """
1045        instance = maybe_copy(self, copy)
1046        if not isinstance(alias, Expression):
1047            alias = TableAlias(this=to_identifier(alias)) if alias else None
1048
1049        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1051    def limit(
1052        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1053    ) -> Q:
1054        """
1055        Adds a LIMIT clause to this query.
1056
1057        Example:
1058            >>> select("1").union(select("1")).limit(1).sql()
1059            'SELECT 1 UNION SELECT 1 LIMIT 1'
1060
1061        Args:
1062            expression: the SQL code string to parse.
1063                This can also be an integer.
1064                If a `Limit` instance is passed, it will be used as-is.
1065                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
1066            dialect: the dialect used to parse the input expression.
1067            copy: if `False`, modify this expression instance in-place.
1068            opts: other options to use to parse the input expressions.
1069
1070        Returns:
1071            A limited Select expression.
1072        """
1073        return _apply_builder(
1074            expression=expression,
1075            instance=self,
1076            arg="limit",
1077            into=Limit,
1078            prefix="LIMIT",
1079            dialect=dialect,
1080            copy=copy,
1081            into_arg="expression",
1082            **opts,
1083        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1085    def offset(
1086        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1087    ) -> Q:
1088        """
1089        Set the OFFSET expression.
1090
1091        Example:
1092            >>> Select().from_("tbl").select("x").offset(10).sql()
1093            'SELECT x FROM tbl OFFSET 10'
1094
1095        Args:
1096            expression: the SQL code string to parse.
1097                This can also be an integer.
1098                If a `Offset` instance is passed, this is used as-is.
1099                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
1100            dialect: the dialect used to parse the input expression.
1101            copy: if `False`, modify this expression instance in-place.
1102            opts: other options to use to parse the input expressions.
1103
1104        Returns:
1105            The modified Select expression.
1106        """
1107        return _apply_builder(
1108            expression=expression,
1109            instance=self,
1110            arg="offset",
1111            into=Offset,
1112            prefix="OFFSET",
1113            dialect=dialect,
1114            copy=copy,
1115            into_arg="expression",
1116            **opts,
1117        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1119    def order_by(
1120        self: Q,
1121        *expressions: t.Optional[ExpOrStr],
1122        append: bool = True,
1123        dialect: DialectType = None,
1124        copy: bool = True,
1125        **opts,
1126    ) -> Q:
1127        """
1128        Set the ORDER BY expression.
1129
1130        Example:
1131            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
1132            'SELECT x FROM tbl ORDER BY x DESC'
1133
1134        Args:
1135            *expressions: the SQL code strings to parse.
1136                If a `Group` instance is passed, this is used as-is.
1137                If another `Expression` instance is passed, it will be wrapped in a `Order`.
1138            append: if `True`, add to any existing expressions.
1139                Otherwise, this flattens all the `Order` expression into a single expression.
1140            dialect: the dialect used to parse the input expression.
1141            copy: if `False`, modify this expression instance in-place.
1142            opts: other options to use to parse the input expressions.
1143
1144        Returns:
1145            The modified Select expression.
1146        """
1147        return _apply_child_list_builder(
1148            *expressions,
1149            instance=self,
1150            arg="order",
1151            append=append,
1152            copy=copy,
1153            prefix="ORDER BY",
1154            into=Order,
1155            dialect=dialect,
1156            **opts,
1157        )

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]
1159    @property
1160    def ctes(self) -> t.List[CTE]:
1161        """Returns a list of all the CTEs attached to this query."""
1162        with_ = self.args.get("with")
1163        return with_.expressions if with_ else []

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

selects: List[Expression]
1165    @property
1166    def selects(self) -> t.List[Expression]:
1167        """Returns the query's projections."""
1168        raise NotImplementedError("Query objects must implement `selects`")

Returns the query's projections.

named_selects: List[str]
1170    @property
1171    def named_selects(self) -> t.List[str]:
1172        """Returns the output names of the query's projections."""
1173        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1175    def select(
1176        self: Q,
1177        *expressions: t.Optional[ExpOrStr],
1178        append: bool = True,
1179        dialect: DialectType = None,
1180        copy: bool = True,
1181        **opts,
1182    ) -> Q:
1183        """
1184        Append to or set the SELECT expressions.
1185
1186        Example:
1187            >>> Select().select("x", "y").sql()
1188            'SELECT x, y'
1189
1190        Args:
1191            *expressions: the SQL code strings to parse.
1192                If an `Expression` instance is passed, it will be used as-is.
1193            append: if `True`, add to any existing expressions.
1194                Otherwise, this resets the expressions.
1195            dialect: the dialect used to parse the input expressions.
1196            copy: if `False`, modify this expression instance in-place.
1197            opts: other options to use to parse the input expressions.
1198
1199        Returns:
1200            The modified Query expression.
1201        """
1202        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 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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~Q:
1204    def with_(
1205        self: Q,
1206        alias: ExpOrStr,
1207        as_: ExpOrStr,
1208        recursive: t.Optional[bool] = None,
1209        materialized: t.Optional[bool] = None,
1210        append: bool = True,
1211        dialect: DialectType = None,
1212        copy: bool = True,
1213        **opts,
1214    ) -> Q:
1215        """
1216        Append to or set the common table expressions.
1217
1218        Example:
1219            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
1220            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
1221
1222        Args:
1223            alias: the SQL code string to parse as the table name.
1224                If an `Expression` instance is passed, this is used as-is.
1225            as_: the SQL code string to parse as the table expression.
1226                If an `Expression` instance is passed, it will be used as-is.
1227            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1228            materialized: set the MATERIALIZED part of the expression.
1229            append: if `True`, add to any existing expressions.
1230                Otherwise, this resets the expressions.
1231            dialect: the dialect used to parse the input expression.
1232            copy: if `False`, modify this expression instance in-place.
1233            opts: other options to use to parse the input expressions.
1234
1235        Returns:
1236            The modified expression.
1237        """
1238        return _apply_cte_builder(
1239            self,
1240            alias,
1241            as_,
1242            recursive=recursive,
1243            materialized=materialized,
1244            append=append,
1245            dialect=dialect,
1246            copy=copy,
1247            **opts,
1248        )

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.
  • 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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Union:
1250    def union(
1251        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1252    ) -> Union:
1253        """
1254        Builds a UNION expression.
1255
1256        Example:
1257            >>> import sqlglot
1258            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
1259            'SELECT * FROM foo UNION SELECT * FROM bla'
1260
1261        Args:
1262            expressions: the SQL code strings.
1263                If `Expression` instances are passed, they will be used as-is.
1264            distinct: set the DISTINCT flag if and only if this is true.
1265            dialect: the dialect used to parse the input expression.
1266            opts: other options to use to parse the input expressions.
1267
1268        Returns:
1269            The new Union expression.
1270        """
1271        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Intersect:
1273    def intersect(
1274        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1275    ) -> Intersect:
1276        """
1277        Builds an INTERSECT expression.
1278
1279        Example:
1280            >>> import sqlglot
1281            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
1282            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
1283
1284        Args:
1285            expressions: the SQL code strings.
1286                If `Expression` instances are passed, they will be used as-is.
1287            distinct: set the DISTINCT flag if and only if this is true.
1288            dialect: the dialect used to parse the input expression.
1289            opts: other options to use to parse the input expressions.
1290
1291        Returns:
1292            The new Intersect expression.
1293        """
1294        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Except:
1296    def except_(
1297        self, *expressions: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
1298    ) -> Except:
1299        """
1300        Builds an EXCEPT expression.
1301
1302        Example:
1303            >>> import sqlglot
1304            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
1305            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
1306
1307        Args:
1308            expressions: the SQL code strings.
1309                If `Expression` instance are passed, they will be used as-is.
1310            distinct: set the DISTINCT flag if and only if this is true.
1311            dialect: the dialect used to parse the input expression.
1312            opts: other options to use to parse the input expressions.
1313
1314        Returns:
1315            The new Except expression.
1316        """
1317        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):
1320class UDTF(DerivedTable):
1321    @property
1322    def selects(self) -> t.List[Expression]:
1323        alias = self.args.get("alias")
1324        return alias.columns if alias else []
selects: List[Expression]
1321    @property
1322    def selects(self) -> t.List[Expression]:
1323        alias = self.args.get("alias")
1324        return alias.columns if alias else []
key = 'udtf'
class Cache(Expression):
1327class Cache(Expression):
1328    arg_types = {
1329        "this": True,
1330        "lazy": False,
1331        "options": False,
1332        "expression": False,
1333    }
arg_types = {'this': True, 'lazy': False, 'options': False, 'expression': False}
key = 'cache'
class Uncache(Expression):
1336class Uncache(Expression):
1337    arg_types = {"this": True, "exists": False}
arg_types = {'this': True, 'exists': False}
key = 'uncache'
class Refresh(Expression):
1340class Refresh(Expression):
1341    pass
key = 'refresh'
class DDL(Expression):
1344class DDL(Expression):
1345    @property
1346    def ctes(self) -> t.List[CTE]:
1347        """Returns a list of all the CTEs attached to this statement."""
1348        with_ = self.args.get("with")
1349        return with_.expressions if with_ else []
1350
1351    @property
1352    def selects(self) -> t.List[Expression]:
1353        """If this statement contains a query (e.g. a CTAS), this returns the query's projections."""
1354        return self.expression.selects if isinstance(self.expression, Query) else []
1355
1356    @property
1357    def named_selects(self) -> t.List[str]:
1358        """
1359        If this statement contains a query (e.g. a CTAS), this returns the output
1360        names of the query's projections.
1361        """
1362        return self.expression.named_selects if isinstance(self.expression, Query) else []
ctes: List[CTE]
1345    @property
1346    def ctes(self) -> t.List[CTE]:
1347        """Returns a list of all the CTEs attached to this statement."""
1348        with_ = self.args.get("with")
1349        return with_.expressions if with_ else []

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

selects: List[Expression]
1351    @property
1352    def selects(self) -> t.List[Expression]:
1353        """If this statement contains a query (e.g. a CTAS), this returns the query's projections."""
1354        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]
1356    @property
1357    def named_selects(self) -> t.List[str]:
1358        """
1359        If this statement contains a query (e.g. a CTAS), this returns the output
1360        names of the query's projections.
1361        """
1362        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):
1365class DML(Expression):
1366    def returning(
1367        self,
1368        expression: ExpOrStr,
1369        dialect: DialectType = None,
1370        copy: bool = True,
1371        **opts,
1372    ) -> "Self":
1373        """
1374        Set the RETURNING expression. Not supported by all dialects.
1375
1376        Example:
1377            >>> delete("tbl").returning("*", dialect="postgres").sql()
1378            'DELETE FROM tbl RETURNING *'
1379
1380        Args:
1381            expression: the SQL code strings to parse.
1382                If an `Expression` instance is passed, it will be used as-is.
1383            dialect: the dialect used to parse the input expressions.
1384            copy: if `False`, modify this expression instance in-place.
1385            opts: other options to use to parse the input expressions.
1386
1387        Returns:
1388            Delete: the modified expression.
1389        """
1390        return _apply_builder(
1391            expression=expression,
1392            instance=self,
1393            arg="returning",
1394            prefix="RETURNING",
1395            dialect=dialect,
1396            copy=copy,
1397            into=Returning,
1398            **opts,
1399        )
def returning( self, expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> typing_extensions.Self:
1366    def returning(
1367        self,
1368        expression: ExpOrStr,
1369        dialect: DialectType = None,
1370        copy: bool = True,
1371        **opts,
1372    ) -> "Self":
1373        """
1374        Set the RETURNING expression. Not supported by all dialects.
1375
1376        Example:
1377            >>> delete("tbl").returning("*", dialect="postgres").sql()
1378            'DELETE FROM tbl RETURNING *'
1379
1380        Args:
1381            expression: the SQL code strings to parse.
1382                If an `Expression` instance is passed, it will be used as-is.
1383            dialect: the dialect used to parse the input expressions.
1384            copy: if `False`, modify this expression instance in-place.
1385            opts: other options to use to parse the input expressions.
1386
1387        Returns:
1388            Delete: the modified expression.
1389        """
1390        return _apply_builder(
1391            expression=expression,
1392            instance=self,
1393            arg="returning",
1394            prefix="RETURNING",
1395            dialect=dialect,
1396            copy=copy,
1397            into=Returning,
1398            **opts,
1399        )

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):
1402class Create(DDL):
1403    arg_types = {
1404        "with": False,
1405        "this": True,
1406        "kind": True,
1407        "expression": False,
1408        "exists": False,
1409        "properties": False,
1410        "replace": False,
1411        "refresh": False,
1412        "unique": False,
1413        "indexes": False,
1414        "no_schema_binding": False,
1415        "begin": False,
1416        "end": False,
1417        "clone": False,
1418        "concurrently": False,
1419        "clustered": False,
1420    }
1421
1422    @property
1423    def kind(self) -> t.Optional[str]:
1424        kind = self.args.get("kind")
1425        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]
1422    @property
1423    def kind(self) -> t.Optional[str]:
1424        kind = self.args.get("kind")
1425        return kind and kind.upper()
key = 'create'
class SequenceProperties(Expression):
1428class SequenceProperties(Expression):
1429    arg_types = {
1430        "increment": False,
1431        "minvalue": False,
1432        "maxvalue": False,
1433        "cache": False,
1434        "start": False,
1435        "owned": False,
1436        "options": False,
1437    }
arg_types = {'increment': False, 'minvalue': False, 'maxvalue': False, 'cache': False, 'start': False, 'owned': False, 'options': False}
key = 'sequenceproperties'
class TruncateTable(Expression):
1440class TruncateTable(Expression):
1441    arg_types = {
1442        "expressions": True,
1443        "is_database": False,
1444        "exists": False,
1445        "only": False,
1446        "cluster": False,
1447        "identity": False,
1448        "option": False,
1449        "partition": False,
1450    }
arg_types = {'expressions': True, 'is_database': False, 'exists': False, 'only': False, 'cluster': False, 'identity': False, 'option': False, 'partition': False}
key = 'truncatetable'
class Clone(Expression):
1456class Clone(Expression):
1457    arg_types = {"this": True, "shallow": False, "copy": False}
arg_types = {'this': True, 'shallow': False, 'copy': False}
key = 'clone'
class Describe(Expression):
1460class Describe(Expression):
1461    arg_types = {
1462        "this": True,
1463        "style": False,
1464        "kind": False,
1465        "expressions": False,
1466        "partition": False,
1467    }
arg_types = {'this': True, 'style': False, 'kind': False, 'expressions': False, 'partition': False}
key = 'describe'
class Summarize(Expression):
1471class Summarize(Expression):
1472    arg_types = {"this": True, "table": False}
arg_types = {'this': True, 'table': False}
key = 'summarize'
class Kill(Expression):
1475class Kill(Expression):
1476    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'kill'
class Pragma(Expression):
1479class Pragma(Expression):
1480    pass
key = 'pragma'
class Declare(Expression):
1483class Declare(Expression):
1484    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'declare'
class DeclareItem(Expression):
1487class DeclareItem(Expression):
1488    arg_types = {"this": True, "kind": True, "default": False}
arg_types = {'this': True, 'kind': True, 'default': False}
key = 'declareitem'
class Set(Expression):
1491class Set(Expression):
1492    arg_types = {"expressions": False, "unset": False, "tag": False}
arg_types = {'expressions': False, 'unset': False, 'tag': False}
key = 'set'
class Heredoc(Expression):
1495class Heredoc(Expression):
1496    arg_types = {"this": True, "tag": False}
arg_types = {'this': True, 'tag': False}
key = 'heredoc'
class SetItem(Expression):
1499class SetItem(Expression):
1500    arg_types = {
1501        "this": False,
1502        "expressions": False,
1503        "kind": False,
1504        "collate": False,  # MySQL SET NAMES statement
1505        "global": False,
1506    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'collate': False, 'global': False}
key = 'setitem'
class Show(Expression):
1509class Show(Expression):
1510    arg_types = {
1511        "this": True,
1512        "history": False,
1513        "terse": False,
1514        "target": False,
1515        "offset": False,
1516        "starts_with": False,
1517        "limit": False,
1518        "from": False,
1519        "like": False,
1520        "where": False,
1521        "db": False,
1522        "scope": False,
1523        "scope_kind": False,
1524        "full": False,
1525        "mutex": False,
1526        "query": False,
1527        "channel": False,
1528        "global": False,
1529        "log": False,
1530        "position": False,
1531        "types": False,
1532    }
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}
key = 'show'
class UserDefinedFunction(Expression):
1535class UserDefinedFunction(Expression):
1536    arg_types = {"this": True, "expressions": False, "wrapped": False}
arg_types = {'this': True, 'expressions': False, 'wrapped': False}
key = 'userdefinedfunction'
class CharacterSet(Expression):
1539class CharacterSet(Expression):
1540    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'characterset'
class With(Expression):
1543class With(Expression):
1544    arg_types = {"expressions": True, "recursive": False}
1545
1546    @property
1547    def recursive(self) -> bool:
1548        return bool(self.args.get("recursive"))
arg_types = {'expressions': True, 'recursive': False}
recursive: bool
1546    @property
1547    def recursive(self) -> bool:
1548        return bool(self.args.get("recursive"))
key = 'with'
class WithinGroup(Expression):
1551class WithinGroup(Expression):
1552    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'withingroup'
class CTE(DerivedTable):
1557class CTE(DerivedTable):
1558    arg_types = {
1559        "this": True,
1560        "alias": True,
1561        "scalar": False,
1562        "materialized": False,
1563    }
arg_types = {'this': True, 'alias': True, 'scalar': False, 'materialized': False}
key = 'cte'
class ProjectionDef(Expression):
1566class ProjectionDef(Expression):
1567    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'projectiondef'
class TableAlias(Expression):
1570class TableAlias(Expression):
1571    arg_types = {"this": False, "columns": False}
1572
1573    @property
1574    def columns(self):
1575        return self.args.get("columns") or []
arg_types = {'this': False, 'columns': False}
columns
1573    @property
1574    def columns(self):
1575        return self.args.get("columns") or []
key = 'tablealias'
class BitString(Condition):
1578class BitString(Condition):
1579    pass
key = 'bitstring'
class HexString(Condition):
1582class HexString(Condition):
1583    pass
key = 'hexstring'
class ByteString(Condition):
1586class ByteString(Condition):
1587    pass
key = 'bytestring'
class RawString(Condition):
1590class RawString(Condition):
1591    pass
key = 'rawstring'
class UnicodeString(Condition):
1594class UnicodeString(Condition):
1595    arg_types = {"this": True, "escape": False}
arg_types = {'this': True, 'escape': False}
key = 'unicodestring'
class Column(Condition):
1598class Column(Condition):
1599    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1600
1601    @property
1602    def table(self) -> str:
1603        return self.text("table")
1604
1605    @property
1606    def db(self) -> str:
1607        return self.text("db")
1608
1609    @property
1610    def catalog(self) -> str:
1611        return self.text("catalog")
1612
1613    @property
1614    def output_name(self) -> str:
1615        return self.name
1616
1617    @property
1618    def parts(self) -> t.List[Identifier]:
1619        """Return the parts of a column in order catalog, db, table, name."""
1620        return [
1621            t.cast(Identifier, self.args[part])
1622            for part in ("catalog", "db", "table", "this")
1623            if self.args.get(part)
1624        ]
1625
1626    def to_dot(self) -> Dot | Identifier:
1627        """Converts the column into a dot expression."""
1628        parts = self.parts
1629        parent = self.parent
1630
1631        while parent:
1632            if isinstance(parent, Dot):
1633                parts.append(parent.expression)
1634            parent = parent.parent
1635
1636        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
1601    @property
1602    def table(self) -> str:
1603        return self.text("table")
db: str
1605    @property
1606    def db(self) -> str:
1607        return self.text("db")
catalog: str
1609    @property
1610    def catalog(self) -> str:
1611        return self.text("catalog")
output_name: str
1613    @property
1614    def output_name(self) -> str:
1615        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]
1617    @property
1618    def parts(self) -> t.List[Identifier]:
1619        """Return the parts of a column in order catalog, db, table, name."""
1620        return [
1621            t.cast(Identifier, self.args[part])
1622            for part in ("catalog", "db", "table", "this")
1623            if self.args.get(part)
1624        ]

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

def to_dot(self) -> Dot | Identifier:
1626    def to_dot(self) -> Dot | Identifier:
1627        """Converts the column into a dot expression."""
1628        parts = self.parts
1629        parent = self.parent
1630
1631        while parent:
1632            if isinstance(parent, Dot):
1633                parts.append(parent.expression)
1634            parent = parent.parent
1635
1636        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]

Converts the column into a dot expression.

key = 'column'
class ColumnPosition(Expression):
1639class ColumnPosition(Expression):
1640    arg_types = {"this": False, "position": True}
arg_types = {'this': False, 'position': True}
key = 'columnposition'
class ColumnDef(Expression):
1643class ColumnDef(Expression):
1644    arg_types = {
1645        "this": True,
1646        "kind": False,
1647        "constraints": False,
1648        "exists": False,
1649        "position": False,
1650    }
1651
1652    @property
1653    def constraints(self) -> t.List[ColumnConstraint]:
1654        return self.args.get("constraints") or []
1655
1656    @property
1657    def kind(self) -> t.Optional[DataType]:
1658        return self.args.get("kind")
arg_types = {'this': True, 'kind': False, 'constraints': False, 'exists': False, 'position': False}
constraints: List[ColumnConstraint]
1652    @property
1653    def constraints(self) -> t.List[ColumnConstraint]:
1654        return self.args.get("constraints") or []
kind: Optional[DataType]
1656    @property
1657    def kind(self) -> t.Optional[DataType]:
1658        return self.args.get("kind")
key = 'columndef'
class AlterColumn(Expression):
1661class AlterColumn(Expression):
1662    arg_types = {
1663        "this": True,
1664        "dtype": False,
1665        "collate": False,
1666        "using": False,
1667        "default": False,
1668        "drop": False,
1669        "comment": False,
1670        "allow_null": False,
1671    }
arg_types = {'this': True, 'dtype': False, 'collate': False, 'using': False, 'default': False, 'drop': False, 'comment': False, 'allow_null': False}
key = 'altercolumn'
class AlterDistStyle(Expression):
1675class AlterDistStyle(Expression):
1676    pass
key = 'alterdiststyle'
class AlterSortKey(Expression):
1679class AlterSortKey(Expression):
1680    arg_types = {"this": False, "expressions": False, "compound": False}
arg_types = {'this': False, 'expressions': False, 'compound': False}
key = 'altersortkey'
class AlterSet(Expression):
1683class AlterSet(Expression):
1684    arg_types = {
1685        "expressions": False,
1686        "option": False,
1687        "tablespace": False,
1688        "access_method": False,
1689        "file_format": False,
1690        "copy_options": False,
1691        "tag": False,
1692        "location": False,
1693        "serde": False,
1694    }
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):
1697class RenameColumn(Expression):
1698    arg_types = {"this": True, "to": True, "exists": False}
arg_types = {'this': True, 'to': True, 'exists': False}
key = 'renamecolumn'
class AlterRename(Expression):
1701class AlterRename(Expression):
1702    pass
key = 'alterrename'
class SwapTable(Expression):
1705class SwapTable(Expression):
1706    pass
key = 'swaptable'
class Comment(Expression):
1709class Comment(Expression):
1710    arg_types = {
1711        "this": True,
1712        "kind": True,
1713        "expression": True,
1714        "exists": False,
1715        "materialized": False,
1716    }
arg_types = {'this': True, 'kind': True, 'expression': True, 'exists': False, 'materialized': False}
key = 'comment'
class Comprehension(Expression):
1719class Comprehension(Expression):
1720    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):
1724class MergeTreeTTLAction(Expression):
1725    arg_types = {
1726        "this": True,
1727        "delete": False,
1728        "recompress": False,
1729        "to_disk": False,
1730        "to_volume": False,
1731    }
arg_types = {'this': True, 'delete': False, 'recompress': False, 'to_disk': False, 'to_volume': False}
key = 'mergetreettlaction'
class MergeTreeTTL(Expression):
1735class MergeTreeTTL(Expression):
1736    arg_types = {
1737        "expressions": True,
1738        "where": False,
1739        "group": False,
1740        "aggregates": False,
1741    }
arg_types = {'expressions': True, 'where': False, 'group': False, 'aggregates': False}
key = 'mergetreettl'
class IndexConstraintOption(Expression):
1745class IndexConstraintOption(Expression):
1746    arg_types = {
1747        "key_block_size": False,
1748        "using": False,
1749        "parser": False,
1750        "comment": False,
1751        "visible": False,
1752        "engine_attr": False,
1753        "secondary_engine_attr": False,
1754    }
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):
1757class ColumnConstraint(Expression):
1758    arg_types = {"this": False, "kind": True}
1759
1760    @property
1761    def kind(self) -> ColumnConstraintKind:
1762        return self.args["kind"]
arg_types = {'this': False, 'kind': True}
kind: ColumnConstraintKind
1760    @property
1761    def kind(self) -> ColumnConstraintKind:
1762        return self.args["kind"]
key = 'columnconstraint'
class ColumnConstraintKind(Expression):
1765class ColumnConstraintKind(Expression):
1766    pass
key = 'columnconstraintkind'
class AutoIncrementColumnConstraint(ColumnConstraintKind):
1769class AutoIncrementColumnConstraint(ColumnConstraintKind):
1770    pass
key = 'autoincrementcolumnconstraint'
class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1773class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1774    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'periodforsystemtimeconstraint'
class CaseSpecificColumnConstraint(ColumnConstraintKind):
1777class CaseSpecificColumnConstraint(ColumnConstraintKind):
1778    arg_types = {"not_": True}
arg_types = {'not_': True}
key = 'casespecificcolumnconstraint'
class CharacterSetColumnConstraint(ColumnConstraintKind):
1781class CharacterSetColumnConstraint(ColumnConstraintKind):
1782    arg_types = {"this": True}
arg_types = {'this': True}
key = 'charactersetcolumnconstraint'
class CheckColumnConstraint(ColumnConstraintKind):
1785class CheckColumnConstraint(ColumnConstraintKind):
1786    arg_types = {"this": True, "enforced": False}
arg_types = {'this': True, 'enforced': False}
key = 'checkcolumnconstraint'
class ClusteredColumnConstraint(ColumnConstraintKind):
1789class ClusteredColumnConstraint(ColumnConstraintKind):
1790    pass
key = 'clusteredcolumnconstraint'
class CollateColumnConstraint(ColumnConstraintKind):
1793class CollateColumnConstraint(ColumnConstraintKind):
1794    pass
key = 'collatecolumnconstraint'
class CommentColumnConstraint(ColumnConstraintKind):
1797class CommentColumnConstraint(ColumnConstraintKind):
1798    pass
key = 'commentcolumnconstraint'
class CompressColumnConstraint(ColumnConstraintKind):
1801class CompressColumnConstraint(ColumnConstraintKind):
1802    arg_types = {"this": False}
arg_types = {'this': False}
key = 'compresscolumnconstraint'
class DateFormatColumnConstraint(ColumnConstraintKind):
1805class DateFormatColumnConstraint(ColumnConstraintKind):
1806    arg_types = {"this": True}
arg_types = {'this': True}
key = 'dateformatcolumnconstraint'
class DefaultColumnConstraint(ColumnConstraintKind):
1809class DefaultColumnConstraint(ColumnConstraintKind):
1810    pass
key = 'defaultcolumnconstraint'
class EncodeColumnConstraint(ColumnConstraintKind):
1813class EncodeColumnConstraint(ColumnConstraintKind):
1814    pass
key = 'encodecolumnconstraint'
class ExcludeColumnConstraint(ColumnConstraintKind):
1818class ExcludeColumnConstraint(ColumnConstraintKind):
1819    pass
key = 'excludecolumnconstraint'
class EphemeralColumnConstraint(ColumnConstraintKind):
1822class EphemeralColumnConstraint(ColumnConstraintKind):
1823    arg_types = {"this": False}
arg_types = {'this': False}
key = 'ephemeralcolumnconstraint'
class WithOperator(Expression):
1826class WithOperator(Expression):
1827    arg_types = {"this": True, "op": True}
arg_types = {'this': True, 'op': True}
key = 'withoperator'
class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1830class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1831    # this: True -> ALWAYS, this: False -> BY DEFAULT
1832    arg_types = {
1833        "this": False,
1834        "expression": False,
1835        "on_null": False,
1836        "start": False,
1837        "increment": False,
1838        "minvalue": False,
1839        "maxvalue": False,
1840        "cycle": False,
1841    }
arg_types = {'this': False, 'expression': False, 'on_null': False, 'start': False, 'increment': False, 'minvalue': False, 'maxvalue': False, 'cycle': False}
key = 'generatedasidentitycolumnconstraint'
class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1844class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1845    arg_types = {"start": False, "hidden": False}
arg_types = {'start': False, 'hidden': False}
key = 'generatedasrowcolumnconstraint'
class IndexColumnConstraint(ColumnConstraintKind):
1850class IndexColumnConstraint(ColumnConstraintKind):
1851    arg_types = {
1852        "this": False,
1853        "expressions": False,
1854        "kind": False,
1855        "index_type": False,
1856        "options": False,
1857        "expression": False,  # Clickhouse
1858        "granularity": False,
1859    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'index_type': False, 'options': False, 'expression': False, 'granularity': False}
key = 'indexcolumnconstraint'
class InlineLengthColumnConstraint(ColumnConstraintKind):
1862class InlineLengthColumnConstraint(ColumnConstraintKind):
1863    pass
key = 'inlinelengthcolumnconstraint'
class NonClusteredColumnConstraint(ColumnConstraintKind):
1866class NonClusteredColumnConstraint(ColumnConstraintKind):
1867    pass
key = 'nonclusteredcolumnconstraint'
class NotForReplicationColumnConstraint(ColumnConstraintKind):
1870class NotForReplicationColumnConstraint(ColumnConstraintKind):
1871    arg_types = {}
arg_types = {}
key = 'notforreplicationcolumnconstraint'
class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1875class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1876    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'maskingpolicycolumnconstraint'
class NotNullColumnConstraint(ColumnConstraintKind):
1879class NotNullColumnConstraint(ColumnConstraintKind):
1880    arg_types = {"allow_null": False}
arg_types = {'allow_null': False}
key = 'notnullcolumnconstraint'
class OnUpdateColumnConstraint(ColumnConstraintKind):
1884class OnUpdateColumnConstraint(ColumnConstraintKind):
1885    pass
key = 'onupdatecolumnconstraint'
class TagColumnConstraint(ColumnConstraintKind):
1889class TagColumnConstraint(ColumnConstraintKind):
1890    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'tagcolumnconstraint'
class TransformColumnConstraint(ColumnConstraintKind):
1894class TransformColumnConstraint(ColumnConstraintKind):
1895    pass
key = 'transformcolumnconstraint'
class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1898class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1899    arg_types = {"desc": False}
arg_types = {'desc': False}
key = 'primarykeycolumnconstraint'
class TitleColumnConstraint(ColumnConstraintKind):
1902class TitleColumnConstraint(ColumnConstraintKind):
1903    pass
key = 'titlecolumnconstraint'
class UniqueColumnConstraint(ColumnConstraintKind):
1906class UniqueColumnConstraint(ColumnConstraintKind):
1907    arg_types = {"this": False, "index_type": False, "on_conflict": False, "nulls": False}
arg_types = {'this': False, 'index_type': False, 'on_conflict': False, 'nulls': False}
key = 'uniquecolumnconstraint'
class UppercaseColumnConstraint(ColumnConstraintKind):
1910class UppercaseColumnConstraint(ColumnConstraintKind):
1911    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'uppercasecolumnconstraint'
class PathColumnConstraint(ColumnConstraintKind):
1914class PathColumnConstraint(ColumnConstraintKind):
1915    pass
key = 'pathcolumnconstraint'
class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
1919class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
1920    pass
key = 'projectionpolicycolumnconstraint'
class ComputedColumnConstraint(ColumnConstraintKind):
1925class ComputedColumnConstraint(ColumnConstraintKind):
1926    arg_types = {"this": True, "persisted": False, "not_null": False}
arg_types = {'this': True, 'persisted': False, 'not_null': False}
key = 'computedcolumnconstraint'
class Constraint(Expression):
1929class Constraint(Expression):
1930    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'constraint'
class Delete(DML):
1933class Delete(DML):
1934    arg_types = {
1935        "with": False,
1936        "this": False,
1937        "using": False,
1938        "where": False,
1939        "returning": False,
1940        "limit": False,
1941        "tables": False,  # Multiple-Table Syntax (MySQL)
1942        "cluster": False,  # Clickhouse
1943    }
1944
1945    def delete(
1946        self,
1947        table: ExpOrStr,
1948        dialect: DialectType = None,
1949        copy: bool = True,
1950        **opts,
1951    ) -> Delete:
1952        """
1953        Create a DELETE expression or replace the table on an existing DELETE expression.
1954
1955        Example:
1956            >>> delete("tbl").sql()
1957            'DELETE FROM tbl'
1958
1959        Args:
1960            table: the table from which to delete.
1961            dialect: the dialect used to parse the input expression.
1962            copy: if `False`, modify this expression instance in-place.
1963            opts: other options to use to parse the input expressions.
1964
1965        Returns:
1966            Delete: the modified expression.
1967        """
1968        return _apply_builder(
1969            expression=table,
1970            instance=self,
1971            arg="this",
1972            dialect=dialect,
1973            into=Table,
1974            copy=copy,
1975            **opts,
1976        )
1977
1978    def where(
1979        self,
1980        *expressions: t.Optional[ExpOrStr],
1981        append: bool = True,
1982        dialect: DialectType = None,
1983        copy: bool = True,
1984        **opts,
1985    ) -> Delete:
1986        """
1987        Append to or set the WHERE expressions.
1988
1989        Example:
1990            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1991            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1992
1993        Args:
1994            *expressions: the SQL code strings to parse.
1995                If an `Expression` instance is passed, it will be used as-is.
1996                Multiple expressions are combined with an AND operator.
1997            append: if `True`, AND the new expressions to any existing expression.
1998                Otherwise, this resets the expression.
1999            dialect: the dialect used to parse the input expressions.
2000            copy: if `False`, modify this expression instance in-place.
2001            opts: other options to use to parse the input expressions.
2002
2003        Returns:
2004            Delete: the modified expression.
2005        """
2006        return _apply_conjunction_builder(
2007            *expressions,
2008            instance=self,
2009            arg="where",
2010            append=append,
2011            into=Where,
2012            dialect=dialect,
2013            copy=copy,
2014            **opts,
2015        )
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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Delete:
1945    def delete(
1946        self,
1947        table: ExpOrStr,
1948        dialect: DialectType = None,
1949        copy: bool = True,
1950        **opts,
1951    ) -> Delete:
1952        """
1953        Create a DELETE expression or replace the table on an existing DELETE expression.
1954
1955        Example:
1956            >>> delete("tbl").sql()
1957            'DELETE FROM tbl'
1958
1959        Args:
1960            table: the table from which to delete.
1961            dialect: the dialect used to parse the input expression.
1962            copy: if `False`, modify this expression instance in-place.
1963            opts: other options to use to parse the input expressions.
1964
1965        Returns:
1966            Delete: the modified expression.
1967        """
1968        return _apply_builder(
1969            expression=table,
1970            instance=self,
1971            arg="this",
1972            dialect=dialect,
1973            into=Table,
1974            copy=copy,
1975            **opts,
1976        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Delete:
1978    def where(
1979        self,
1980        *expressions: t.Optional[ExpOrStr],
1981        append: bool = True,
1982        dialect: DialectType = None,
1983        copy: bool = True,
1984        **opts,
1985    ) -> Delete:
1986        """
1987        Append to or set the WHERE expressions.
1988
1989        Example:
1990            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1991            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1992
1993        Args:
1994            *expressions: the SQL code strings to parse.
1995                If an `Expression` instance is passed, it will be used as-is.
1996                Multiple expressions are combined with an AND operator.
1997            append: if `True`, AND the new expressions to any existing expression.
1998                Otherwise, this resets the expression.
1999            dialect: the dialect used to parse the input expressions.
2000            copy: if `False`, modify this expression instance in-place.
2001            opts: other options to use to parse the input expressions.
2002
2003        Returns:
2004            Delete: the modified expression.
2005        """
2006        return _apply_conjunction_builder(
2007            *expressions,
2008            instance=self,
2009            arg="where",
2010            append=append,
2011            into=Where,
2012            dialect=dialect,
2013            copy=copy,
2014            **opts,
2015        )

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):
2018class Drop(Expression):
2019    arg_types = {
2020        "this": False,
2021        "kind": False,
2022        "expressions": False,
2023        "exists": False,
2024        "temporary": False,
2025        "materialized": False,
2026        "cascade": False,
2027        "constraints": False,
2028        "purge": False,
2029        "cluster": False,
2030        "concurrently": False,
2031    }
2032
2033    @property
2034    def kind(self) -> t.Optional[str]:
2035        kind = self.args.get("kind")
2036        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]
2033    @property
2034    def kind(self) -> t.Optional[str]:
2035        kind = self.args.get("kind")
2036        return kind and kind.upper()
key = 'drop'
class Filter(Expression):
2039class Filter(Expression):
2040    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'filter'
class Check(Expression):
2043class Check(Expression):
2044    pass
key = 'check'
class Changes(Expression):
2047class Changes(Expression):
2048    arg_types = {"information": True, "at_before": False, "end": False}
arg_types = {'information': True, 'at_before': False, 'end': False}
key = 'changes'
class Connect(Expression):
2052class Connect(Expression):
2053    arg_types = {"start": False, "connect": True, "nocycle": False}
arg_types = {'start': False, 'connect': True, 'nocycle': False}
key = 'connect'
class CopyParameter(Expression):
2056class CopyParameter(Expression):
2057    arg_types = {"this": True, "expression": False, "expressions": False}
arg_types = {'this': True, 'expression': False, 'expressions': False}
key = 'copyparameter'
class Copy(DML):
2060class Copy(DML):
2061    arg_types = {
2062        "this": True,
2063        "kind": True,
2064        "files": True,
2065        "credentials": False,
2066        "format": False,
2067        "params": False,
2068    }
arg_types = {'this': True, 'kind': True, 'files': True, 'credentials': False, 'format': False, 'params': False}
key = 'copy'
class Credentials(Expression):
2071class Credentials(Expression):
2072    arg_types = {
2073        "credentials": False,
2074        "encryption": False,
2075        "storage": False,
2076        "iam_role": False,
2077        "region": False,
2078    }
arg_types = {'credentials': False, 'encryption': False, 'storage': False, 'iam_role': False, 'region': False}
key = 'credentials'
class Prior(Expression):
2081class Prior(Expression):
2082    pass
key = 'prior'
class Directory(Expression):
2085class Directory(Expression):
2086    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
2087    arg_types = {"this": True, "local": False, "row_format": False}
arg_types = {'this': True, 'local': False, 'row_format': False}
key = 'directory'
class ForeignKey(Expression):
2090class ForeignKey(Expression):
2091    arg_types = {
2092        "expressions": True,
2093        "reference": False,
2094        "delete": False,
2095        "update": False,
2096    }
arg_types = {'expressions': True, 'reference': False, 'delete': False, 'update': False}
key = 'foreignkey'
class ColumnPrefix(Expression):
2099class ColumnPrefix(Expression):
2100    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'columnprefix'
class PrimaryKey(Expression):
2103class PrimaryKey(Expression):
2104    arg_types = {"expressions": True, "options": False}
arg_types = {'expressions': True, 'options': False}
key = 'primarykey'
class Into(Expression):
2109class Into(Expression):
2110    arg_types = {
2111        "this": False,
2112        "temporary": False,
2113        "unlogged": False,
2114        "bulk_collect": False,
2115        "expressions": False,
2116    }
arg_types = {'this': False, 'temporary': False, 'unlogged': False, 'bulk_collect': False, 'expressions': False}
key = 'into'
class From(Expression):
2119class From(Expression):
2120    @property
2121    def name(self) -> str:
2122        return self.this.name
2123
2124    @property
2125    def alias_or_name(self) -> str:
2126        return self.this.alias_or_name
name: str
2120    @property
2121    def name(self) -> str:
2122        return self.this.name
alias_or_name: str
2124    @property
2125    def alias_or_name(self) -> str:
2126        return self.this.alias_or_name
key = 'from'
class Having(Expression):
2129class Having(Expression):
2130    pass
key = 'having'
class Hint(Expression):
2133class Hint(Expression):
2134    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'hint'
class JoinHint(Expression):
2137class JoinHint(Expression):
2138    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'joinhint'
class Identifier(Expression):
2141class Identifier(Expression):
2142    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
2143
2144    @property
2145    def quoted(self) -> bool:
2146        return bool(self.args.get("quoted"))
2147
2148    @property
2149    def hashable_args(self) -> t.Any:
2150        return (self.this, self.quoted)
2151
2152    @property
2153    def output_name(self) -> str:
2154        return self.name
arg_types = {'this': True, 'quoted': False, 'global': False, 'temporary': False}
quoted: bool
2144    @property
2145    def quoted(self) -> bool:
2146        return bool(self.args.get("quoted"))
hashable_args: Any
2148    @property
2149    def hashable_args(self) -> t.Any:
2150        return (self.this, self.quoted)
output_name: str
2152    @property
2153    def output_name(self) -> str:
2154        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):
2158class Opclass(Expression):
2159    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'opclass'
class Index(Expression):
2162class Index(Expression):
2163    arg_types = {
2164        "this": False,
2165        "table": False,
2166        "unique": False,
2167        "primary": False,
2168        "amp": False,  # teradata
2169        "params": False,
2170    }
arg_types = {'this': False, 'table': False, 'unique': False, 'primary': False, 'amp': False, 'params': False}
key = 'index'
class IndexParameters(Expression):
2173class IndexParameters(Expression):
2174    arg_types = {
2175        "using": False,
2176        "include": False,
2177        "columns": False,
2178        "with_storage": False,
2179        "partition_by": False,
2180        "tablespace": False,
2181        "where": False,
2182        "on": False,
2183    }
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):
2186class Insert(DDL, DML):
2187    arg_types = {
2188        "hint": False,
2189        "with": False,
2190        "is_function": False,
2191        "this": False,
2192        "expression": False,
2193        "conflict": False,
2194        "returning": False,
2195        "overwrite": False,
2196        "exists": False,
2197        "alternative": False,
2198        "where": False,
2199        "ignore": False,
2200        "by_name": False,
2201        "stored": False,
2202        "partition": False,
2203        "settings": False,
2204        "source": False,
2205    }
2206
2207    def with_(
2208        self,
2209        alias: ExpOrStr,
2210        as_: ExpOrStr,
2211        recursive: t.Optional[bool] = None,
2212        materialized: t.Optional[bool] = None,
2213        append: bool = True,
2214        dialect: DialectType = None,
2215        copy: bool = True,
2216        **opts,
2217    ) -> Insert:
2218        """
2219        Append to or set the common table expressions.
2220
2221        Example:
2222            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2223            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2224
2225        Args:
2226            alias: the SQL code string to parse as the table name.
2227                If an `Expression` instance is passed, this is used as-is.
2228            as_: the SQL code string to parse as the table expression.
2229                If an `Expression` instance is passed, it will be used as-is.
2230            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2231            materialized: set the MATERIALIZED part of the expression.
2232            append: if `True`, add to any existing expressions.
2233                Otherwise, this resets the expressions.
2234            dialect: the dialect used to parse the input expression.
2235            copy: if `False`, modify this expression instance in-place.
2236            opts: other options to use to parse the input expressions.
2237
2238        Returns:
2239            The modified expression.
2240        """
2241        return _apply_cte_builder(
2242            self,
2243            alias,
2244            as_,
2245            recursive=recursive,
2246            materialized=materialized,
2247            append=append,
2248            dialect=dialect,
2249            copy=copy,
2250            **opts,
2251        )
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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Insert:
2207    def with_(
2208        self,
2209        alias: ExpOrStr,
2210        as_: ExpOrStr,
2211        recursive: t.Optional[bool] = None,
2212        materialized: t.Optional[bool] = None,
2213        append: bool = True,
2214        dialect: DialectType = None,
2215        copy: bool = True,
2216        **opts,
2217    ) -> Insert:
2218        """
2219        Append to or set the common table expressions.
2220
2221        Example:
2222            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2223            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2224
2225        Args:
2226            alias: the SQL code string to parse as the table name.
2227                If an `Expression` instance is passed, this is used as-is.
2228            as_: the SQL code string to parse as the table expression.
2229                If an `Expression` instance is passed, it will be used as-is.
2230            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2231            materialized: set the MATERIALIZED part of the expression.
2232            append: if `True`, add to any existing expressions.
2233                Otherwise, this resets the expressions.
2234            dialect: the dialect used to parse the input expression.
2235            copy: if `False`, modify this expression instance in-place.
2236            opts: other options to use to parse the input expressions.
2237
2238        Returns:
2239            The modified expression.
2240        """
2241        return _apply_cte_builder(
2242            self,
2243            alias,
2244            as_,
2245            recursive=recursive,
2246            materialized=materialized,
2247            append=append,
2248            dialect=dialect,
2249            copy=copy,
2250            **opts,
2251        )

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):
2254class ConditionalInsert(Expression):
2255    arg_types = {"this": True, "expression": False, "else_": False}
arg_types = {'this': True, 'expression': False, 'else_': False}
key = 'conditionalinsert'
class MultitableInserts(Expression):
2258class MultitableInserts(Expression):
2259    arg_types = {"expressions": True, "kind": True, "source": True}
arg_types = {'expressions': True, 'kind': True, 'source': True}
key = 'multitableinserts'
class OnConflict(Expression):
2262class OnConflict(Expression):
2263    arg_types = {
2264        "duplicate": False,
2265        "expressions": False,
2266        "action": False,
2267        "conflict_keys": False,
2268        "constraint": False,
2269    }
arg_types = {'duplicate': False, 'expressions': False, 'action': False, 'conflict_keys': False, 'constraint': False}
key = 'onconflict'
class OnCondition(Expression):
2272class OnCondition(Expression):
2273    arg_types = {"error": False, "empty": False, "null": False}
arg_types = {'error': False, 'empty': False, 'null': False}
key = 'oncondition'
class Returning(Expression):
2276class Returning(Expression):
2277    arg_types = {"expressions": True, "into": False}
arg_types = {'expressions': True, 'into': False}
key = 'returning'
class Introducer(Expression):
2281class Introducer(Expression):
2282    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'introducer'
class National(Expression):
2286class National(Expression):
2287    pass
key = 'national'
class LoadData(Expression):
2290class LoadData(Expression):
2291    arg_types = {
2292        "this": True,
2293        "local": False,
2294        "overwrite": False,
2295        "inpath": True,
2296        "partition": False,
2297        "input_format": False,
2298        "serde": False,
2299    }
arg_types = {'this': True, 'local': False, 'overwrite': False, 'inpath': True, 'partition': False, 'input_format': False, 'serde': False}
key = 'loaddata'
class Partition(Expression):
2302class Partition(Expression):
2303    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'partition'
class PartitionRange(Expression):
2306class PartitionRange(Expression):
2307    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionrange'
class PartitionId(Expression):
2311class PartitionId(Expression):
2312    pass
key = 'partitionid'
class Fetch(Expression):
2315class Fetch(Expression):
2316    arg_types = {
2317        "direction": False,
2318        "count": False,
2319        "percent": False,
2320        "with_ties": False,
2321    }
arg_types = {'direction': False, 'count': False, 'percent': False, 'with_ties': False}
key = 'fetch'
class Grant(Expression):
2324class Grant(Expression):
2325    arg_types = {
2326        "privileges": True,
2327        "kind": False,
2328        "securable": True,
2329        "principals": True,
2330        "grant_option": False,
2331    }
arg_types = {'privileges': True, 'kind': False, 'securable': True, 'principals': True, 'grant_option': False}
key = 'grant'
class Group(Expression):
2334class Group(Expression):
2335    arg_types = {
2336        "expressions": False,
2337        "grouping_sets": False,
2338        "cube": False,
2339        "rollup": False,
2340        "totals": False,
2341        "all": False,
2342    }
arg_types = {'expressions': False, 'grouping_sets': False, 'cube': False, 'rollup': False, 'totals': False, 'all': False}
key = 'group'
class Cube(Expression):
2345class Cube(Expression):
2346    arg_types = {"expressions": False}
arg_types = {'expressions': False}
key = 'cube'
class Rollup(Expression):
2349class Rollup(Expression):
2350    arg_types = {"expressions": False}
arg_types = {'expressions': False}
key = 'rollup'
class GroupingSets(Expression):
2353class GroupingSets(Expression):
2354    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'groupingsets'
class Lambda(Expression):
2357class Lambda(Expression):
2358    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'lambda'
class Limit(Expression):
2361class Limit(Expression):
2362    arg_types = {"this": False, "expression": True, "offset": False, "expressions": False}
arg_types = {'this': False, 'expression': True, 'offset': False, 'expressions': False}
key = 'limit'
class Literal(Condition):
2365class Literal(Condition):
2366    arg_types = {"this": True, "is_string": True}
2367
2368    @property
2369    def hashable_args(self) -> t.Any:
2370        return (self.this, self.args.get("is_string"))
2371
2372    @classmethod
2373    def number(cls, number) -> Literal:
2374        return cls(this=str(number), is_string=False)
2375
2376    @classmethod
2377    def string(cls, string) -> Literal:
2378        return cls(this=str(string), is_string=True)
2379
2380    @property
2381    def output_name(self) -> str:
2382        return self.name
2383
2384    def to_py(self) -> int | str | Decimal:
2385        if self.is_number:
2386            try:
2387                return int(self.this)
2388            except ValueError:
2389                return Decimal(self.this)
2390        return self.this
arg_types = {'this': True, 'is_string': True}
hashable_args: Any
2368    @property
2369    def hashable_args(self) -> t.Any:
2370        return (self.this, self.args.get("is_string"))
@classmethod
def number(cls, number) -> Literal:
2372    @classmethod
2373    def number(cls, number) -> Literal:
2374        return cls(this=str(number), is_string=False)
@classmethod
def string(cls, string) -> Literal:
2376    @classmethod
2377    def string(cls, string) -> Literal:
2378        return cls(this=str(string), is_string=True)
output_name: str
2380    @property
2381    def output_name(self) -> str:
2382        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:
2384    def to_py(self) -> int | str | Decimal:
2385        if self.is_number:
2386            try:
2387                return int(self.this)
2388            except ValueError:
2389                return Decimal(self.this)
2390        return self.this

Returns a Python object equivalent of the SQL node.

key = 'literal'
class Join(Expression):
2393class Join(Expression):
2394    arg_types = {
2395        "this": True,
2396        "on": False,
2397        "side": False,
2398        "kind": False,
2399        "using": False,
2400        "method": False,
2401        "global": False,
2402        "hint": False,
2403        "match_condition": False,  # Snowflake
2404        "expressions": False,
2405    }
2406
2407    @property
2408    def method(self) -> str:
2409        return self.text("method").upper()
2410
2411    @property
2412    def kind(self) -> str:
2413        return self.text("kind").upper()
2414
2415    @property
2416    def side(self) -> str:
2417        return self.text("side").upper()
2418
2419    @property
2420    def hint(self) -> str:
2421        return self.text("hint").upper()
2422
2423    @property
2424    def alias_or_name(self) -> str:
2425        return self.this.alias_or_name
2426
2427    def on(
2428        self,
2429        *expressions: t.Optional[ExpOrStr],
2430        append: bool = True,
2431        dialect: DialectType = None,
2432        copy: bool = True,
2433        **opts,
2434    ) -> Join:
2435        """
2436        Append to or set the ON expressions.
2437
2438        Example:
2439            >>> import sqlglot
2440            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2441            'JOIN x ON y = 1'
2442
2443        Args:
2444            *expressions: the SQL code strings to parse.
2445                If an `Expression` instance is passed, it will be used as-is.
2446                Multiple expressions are combined with an AND operator.
2447            append: if `True`, AND the new expressions to any existing expression.
2448                Otherwise, this resets the expression.
2449            dialect: the dialect used to parse the input expressions.
2450            copy: if `False`, modify this expression instance in-place.
2451            opts: other options to use to parse the input expressions.
2452
2453        Returns:
2454            The modified Join expression.
2455        """
2456        join = _apply_conjunction_builder(
2457            *expressions,
2458            instance=self,
2459            arg="on",
2460            append=append,
2461            dialect=dialect,
2462            copy=copy,
2463            **opts,
2464        )
2465
2466        if join.kind == "CROSS":
2467            join.set("kind", None)
2468
2469        return join
2470
2471    def using(
2472        self,
2473        *expressions: t.Optional[ExpOrStr],
2474        append: bool = True,
2475        dialect: DialectType = None,
2476        copy: bool = True,
2477        **opts,
2478    ) -> Join:
2479        """
2480        Append to or set the USING expressions.
2481
2482        Example:
2483            >>> import sqlglot
2484            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2485            'JOIN x USING (foo, bla)'
2486
2487        Args:
2488            *expressions: the SQL code strings to parse.
2489                If an `Expression` instance is passed, it will be used as-is.
2490            append: if `True`, concatenate the new expressions to the existing "using" list.
2491                Otherwise, this resets the expression.
2492            dialect: the dialect used to parse the input expressions.
2493            copy: if `False`, modify this expression instance in-place.
2494            opts: other options to use to parse the input expressions.
2495
2496        Returns:
2497            The modified Join expression.
2498        """
2499        join = _apply_list_builder(
2500            *expressions,
2501            instance=self,
2502            arg="using",
2503            append=append,
2504            dialect=dialect,
2505            copy=copy,
2506            **opts,
2507        )
2508
2509        if join.kind == "CROSS":
2510            join.set("kind", None)
2511
2512        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}
method: str
2407    @property
2408    def method(self) -> str:
2409        return self.text("method").upper()
kind: str
2411    @property
2412    def kind(self) -> str:
2413        return self.text("kind").upper()
side: str
2415    @property
2416    def side(self) -> str:
2417        return self.text("side").upper()
hint: str
2419    @property
2420    def hint(self) -> str:
2421        return self.text("hint").upper()
alias_or_name: str
2423    @property
2424    def alias_or_name(self) -> str:
2425        return self.this.alias_or_name
def on( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Join:
2427    def on(
2428        self,
2429        *expressions: t.Optional[ExpOrStr],
2430        append: bool = True,
2431        dialect: DialectType = None,
2432        copy: bool = True,
2433        **opts,
2434    ) -> Join:
2435        """
2436        Append to or set the ON expressions.
2437
2438        Example:
2439            >>> import sqlglot
2440            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2441            'JOIN x ON y = 1'
2442
2443        Args:
2444            *expressions: the SQL code strings to parse.
2445                If an `Expression` instance is passed, it will be used as-is.
2446                Multiple expressions are combined with an AND operator.
2447            append: if `True`, AND the new expressions to any existing expression.
2448                Otherwise, this resets the expression.
2449            dialect: the dialect used to parse the input expressions.
2450            copy: if `False`, modify this expression instance in-place.
2451            opts: other options to use to parse the input expressions.
2452
2453        Returns:
2454            The modified Join expression.
2455        """
2456        join = _apply_conjunction_builder(
2457            *expressions,
2458            instance=self,
2459            arg="on",
2460            append=append,
2461            dialect=dialect,
2462            copy=copy,
2463            **opts,
2464        )
2465
2466        if join.kind == "CROSS":
2467            join.set("kind", None)
2468
2469        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Join:
2471    def using(
2472        self,
2473        *expressions: t.Optional[ExpOrStr],
2474        append: bool = True,
2475        dialect: DialectType = None,
2476        copy: bool = True,
2477        **opts,
2478    ) -> Join:
2479        """
2480        Append to or set the USING expressions.
2481
2482        Example:
2483            >>> import sqlglot
2484            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2485            'JOIN x USING (foo, bla)'
2486
2487        Args:
2488            *expressions: the SQL code strings to parse.
2489                If an `Expression` instance is passed, it will be used as-is.
2490            append: if `True`, concatenate the new expressions to the existing "using" list.
2491                Otherwise, this resets the expression.
2492            dialect: the dialect used to parse the input expressions.
2493            copy: if `False`, modify this expression instance in-place.
2494            opts: other options to use to parse the input expressions.
2495
2496        Returns:
2497            The modified Join expression.
2498        """
2499        join = _apply_list_builder(
2500            *expressions,
2501            instance=self,
2502            arg="using",
2503            append=append,
2504            dialect=dialect,
2505            copy=copy,
2506            **opts,
2507        )
2508
2509        if join.kind == "CROSS":
2510            join.set("kind", None)
2511
2512        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):
2515class Lateral(UDTF):
2516    arg_types = {
2517        "this": True,
2518        "view": False,
2519        "outer": False,
2520        "alias": False,
2521        "cross_apply": False,  # True -> CROSS APPLY, False -> OUTER APPLY
2522    }
arg_types = {'this': True, 'view': False, 'outer': False, 'alias': False, 'cross_apply': False}
key = 'lateral'
class MatchRecognizeMeasure(Expression):
2525class MatchRecognizeMeasure(Expression):
2526    arg_types = {
2527        "this": True,
2528        "window_frame": False,
2529    }
arg_types = {'this': True, 'window_frame': False}
key = 'matchrecognizemeasure'
class MatchRecognize(Expression):
2532class MatchRecognize(Expression):
2533    arg_types = {
2534        "partition_by": False,
2535        "order": False,
2536        "measures": False,
2537        "rows": False,
2538        "after": False,
2539        "pattern": False,
2540        "define": False,
2541        "alias": False,
2542    }
arg_types = {'partition_by': False, 'order': False, 'measures': False, 'rows': False, 'after': False, 'pattern': False, 'define': False, 'alias': False}
key = 'matchrecognize'
class Final(Expression):
2547class Final(Expression):
2548    pass
key = 'final'
class Offset(Expression):
2551class Offset(Expression):
2552    arg_types = {"this": False, "expression": True, "expressions": False}
arg_types = {'this': False, 'expression': True, 'expressions': False}
key = 'offset'
class Order(Expression):
2555class Order(Expression):
2556    arg_types = {"this": False, "expressions": True, "siblings": False}
arg_types = {'this': False, 'expressions': True, 'siblings': False}
key = 'order'
class WithFill(Expression):
2560class WithFill(Expression):
2561    arg_types = {
2562        "from": False,
2563        "to": False,
2564        "step": False,
2565        "interpolate": False,
2566    }
arg_types = {'from': False, 'to': False, 'step': False, 'interpolate': False}
key = 'withfill'
class Cluster(Order):
2571class Cluster(Order):
2572    pass
key = 'cluster'
class Distribute(Order):
2575class Distribute(Order):
2576    pass
key = 'distribute'
class Sort(Order):
2579class Sort(Order):
2580    pass
key = 'sort'
class Ordered(Expression):
2583class Ordered(Expression):
2584    arg_types = {"this": True, "desc": False, "nulls_first": True, "with_fill": False}
arg_types = {'this': True, 'desc': False, 'nulls_first': True, 'with_fill': False}
key = 'ordered'
class Property(Expression):
2587class Property(Expression):
2588    arg_types = {"this": True, "value": True}
arg_types = {'this': True, 'value': True}
key = 'property'
class GrantPrivilege(Expression):
2591class GrantPrivilege(Expression):
2592    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'grantprivilege'
class GrantPrincipal(Expression):
2595class GrantPrincipal(Expression):
2596    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'grantprincipal'
class AllowedValuesProperty(Expression):
2599class AllowedValuesProperty(Expression):
2600    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'allowedvaluesproperty'
class AlgorithmProperty(Property):
2603class AlgorithmProperty(Property):
2604    arg_types = {"this": True}
arg_types = {'this': True}
key = 'algorithmproperty'
class AutoIncrementProperty(Property):
2607class AutoIncrementProperty(Property):
2608    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autoincrementproperty'
class AutoRefreshProperty(Property):
2612class AutoRefreshProperty(Property):
2613    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autorefreshproperty'
class BackupProperty(Property):
2616class BackupProperty(Property):
2617    arg_types = {"this": True}
arg_types = {'this': True}
key = 'backupproperty'
class BlockCompressionProperty(Property):
2620class BlockCompressionProperty(Property):
2621    arg_types = {
2622        "autotemp": False,
2623        "always": False,
2624        "default": False,
2625        "manual": False,
2626        "never": False,
2627    }
arg_types = {'autotemp': False, 'always': False, 'default': False, 'manual': False, 'never': False}
key = 'blockcompressionproperty'
class CharacterSetProperty(Property):
2630class CharacterSetProperty(Property):
2631    arg_types = {"this": True, "default": True}
arg_types = {'this': True, 'default': True}
key = 'charactersetproperty'
class ChecksumProperty(Property):
2634class ChecksumProperty(Property):
2635    arg_types = {"on": False, "default": False}
arg_types = {'on': False, 'default': False}
key = 'checksumproperty'
class CollateProperty(Property):
2638class CollateProperty(Property):
2639    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'collateproperty'
class CopyGrantsProperty(Property):
2642class CopyGrantsProperty(Property):
2643    arg_types = {}
arg_types = {}
key = 'copygrantsproperty'
class DataBlocksizeProperty(Property):
2646class DataBlocksizeProperty(Property):
2647    arg_types = {
2648        "size": False,
2649        "units": False,
2650        "minimum": False,
2651        "maximum": False,
2652        "default": False,
2653    }
arg_types = {'size': False, 'units': False, 'minimum': False, 'maximum': False, 'default': False}
key = 'datablocksizeproperty'
class DataDeletionProperty(Property):
2656class DataDeletionProperty(Property):
2657    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):
2660class DefinerProperty(Property):
2661    arg_types = {"this": True}
arg_types = {'this': True}
key = 'definerproperty'
class DistKeyProperty(Property):
2664class DistKeyProperty(Property):
2665    arg_types = {"this": True}
arg_types = {'this': True}
key = 'distkeyproperty'
class DistributedByProperty(Property):
2670class DistributedByProperty(Property):
2671    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):
2674class DistStyleProperty(Property):
2675    arg_types = {"this": True}
arg_types = {'this': True}
key = 'diststyleproperty'
class DuplicateKeyProperty(Property):
2678class DuplicateKeyProperty(Property):
2679    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'duplicatekeyproperty'
class EngineProperty(Property):
2682class EngineProperty(Property):
2683    arg_types = {"this": True}
arg_types = {'this': True}
key = 'engineproperty'
class HeapProperty(Property):
2686class HeapProperty(Property):
2687    arg_types = {}
arg_types = {}
key = 'heapproperty'
class ToTableProperty(Property):
2690class ToTableProperty(Property):
2691    arg_types = {"this": True}
arg_types = {'this': True}
key = 'totableproperty'
class ExecuteAsProperty(Property):
2694class ExecuteAsProperty(Property):
2695    arg_types = {"this": True}
arg_types = {'this': True}
key = 'executeasproperty'
class ExternalProperty(Property):
2698class ExternalProperty(Property):
2699    arg_types = {"this": False}
arg_types = {'this': False}
key = 'externalproperty'
class FallbackProperty(Property):
2702class FallbackProperty(Property):
2703    arg_types = {"no": True, "protection": False}
arg_types = {'no': True, 'protection': False}
key = 'fallbackproperty'
class FileFormatProperty(Property):
2706class FileFormatProperty(Property):
2707    arg_types = {"this": True}
arg_types = {'this': True}
key = 'fileformatproperty'
class FreespaceProperty(Property):
2710class FreespaceProperty(Property):
2711    arg_types = {"this": True, "percent": False}
arg_types = {'this': True, 'percent': False}
key = 'freespaceproperty'
class GlobalProperty(Property):
2714class GlobalProperty(Property):
2715    arg_types = {}
arg_types = {}
key = 'globalproperty'
class IcebergProperty(Property):
2718class IcebergProperty(Property):
2719    arg_types = {}
arg_types = {}
key = 'icebergproperty'
class InheritsProperty(Property):
2722class InheritsProperty(Property):
2723    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'inheritsproperty'
class InputModelProperty(Property):
2726class InputModelProperty(Property):
2727    arg_types = {"this": True}
arg_types = {'this': True}
key = 'inputmodelproperty'
class OutputModelProperty(Property):
2730class OutputModelProperty(Property):
2731    arg_types = {"this": True}
arg_types = {'this': True}
key = 'outputmodelproperty'
class IsolatedLoadingProperty(Property):
2734class IsolatedLoadingProperty(Property):
2735    arg_types = {"no": False, "concurrent": False, "target": False}
arg_types = {'no': False, 'concurrent': False, 'target': False}
key = 'isolatedloadingproperty'
class JournalProperty(Property):
2738class JournalProperty(Property):
2739    arg_types = {
2740        "no": False,
2741        "dual": False,
2742        "before": False,
2743        "local": False,
2744        "after": False,
2745    }
arg_types = {'no': False, 'dual': False, 'before': False, 'local': False, 'after': False}
key = 'journalproperty'
class LanguageProperty(Property):
2748class LanguageProperty(Property):
2749    arg_types = {"this": True}
arg_types = {'this': True}
key = 'languageproperty'
class ClusteredByProperty(Property):
2753class ClusteredByProperty(Property):
2754    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
arg_types = {'expressions': True, 'sorted_by': False, 'buckets': True}
key = 'clusteredbyproperty'
class DictProperty(Property):
2757class DictProperty(Property):
2758    arg_types = {"this": True, "kind": True, "settings": False}
arg_types = {'this': True, 'kind': True, 'settings': False}
key = 'dictproperty'
class DictSubProperty(Property):
2761class DictSubProperty(Property):
2762    pass
key = 'dictsubproperty'
class DictRange(Property):
2765class DictRange(Property):
2766    arg_types = {"this": True, "min": True, "max": True}
arg_types = {'this': True, 'min': True, 'max': True}
key = 'dictrange'
class DynamicProperty(Property):
2769class DynamicProperty(Property):
2770    arg_types = {}
arg_types = {}
key = 'dynamicproperty'
class OnCluster(Property):
2775class OnCluster(Property):
2776    arg_types = {"this": True}
arg_types = {'this': True}
key = 'oncluster'
class EmptyProperty(Property):
2780class EmptyProperty(Property):
2781    arg_types = {}
arg_types = {}
key = 'emptyproperty'
class LikeProperty(Property):
2784class LikeProperty(Property):
2785    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'likeproperty'
class LocationProperty(Property):
2788class LocationProperty(Property):
2789    arg_types = {"this": True}
arg_types = {'this': True}
key = 'locationproperty'
class LockProperty(Property):
2792class LockProperty(Property):
2793    arg_types = {"this": True}
arg_types = {'this': True}
key = 'lockproperty'
class LockingProperty(Property):
2796class LockingProperty(Property):
2797    arg_types = {
2798        "this": False,
2799        "kind": True,
2800        "for_or_in": False,
2801        "lock_type": True,
2802        "override": False,
2803    }
arg_types = {'this': False, 'kind': True, 'for_or_in': False, 'lock_type': True, 'override': False}
key = 'lockingproperty'
class LogProperty(Property):
2806class LogProperty(Property):
2807    arg_types = {"no": True}
arg_types = {'no': True}
key = 'logproperty'
class MaterializedProperty(Property):
2810class MaterializedProperty(Property):
2811    arg_types = {"this": False}
arg_types = {'this': False}
key = 'materializedproperty'
class MergeBlockRatioProperty(Property):
2814class MergeBlockRatioProperty(Property):
2815    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):
2818class NoPrimaryIndexProperty(Property):
2819    arg_types = {}
arg_types = {}
key = 'noprimaryindexproperty'
class OnProperty(Property):
2822class OnProperty(Property):
2823    arg_types = {"this": True}
arg_types = {'this': True}
key = 'onproperty'
class OnCommitProperty(Property):
2826class OnCommitProperty(Property):
2827    arg_types = {"delete": False}
arg_types = {'delete': False}
key = 'oncommitproperty'
class PartitionedByProperty(Property):
2830class PartitionedByProperty(Property):
2831    arg_types = {"this": True}
arg_types = {'this': True}
key = 'partitionedbyproperty'
class PartitionBoundSpec(Expression):
2835class PartitionBoundSpec(Expression):
2836    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
2837    arg_types = {
2838        "this": False,
2839        "expression": False,
2840        "from_expressions": False,
2841        "to_expressions": False,
2842    }
arg_types = {'this': False, 'expression': False, 'from_expressions': False, 'to_expressions': False}
key = 'partitionboundspec'
class PartitionedOfProperty(Property):
2845class PartitionedOfProperty(Property):
2846    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
2847    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionedofproperty'
class StreamingTableProperty(Property):
2850class StreamingTableProperty(Property):
2851    arg_types = {}
arg_types = {}
key = 'streamingtableproperty'
class RemoteWithConnectionModelProperty(Property):
2854class RemoteWithConnectionModelProperty(Property):
2855    arg_types = {"this": True}
arg_types = {'this': True}
key = 'remotewithconnectionmodelproperty'
class ReturnsProperty(Property):
2858class ReturnsProperty(Property):
2859    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):
2862class StrictProperty(Property):
2863    arg_types = {}
arg_types = {}
key = 'strictproperty'
class RowFormatProperty(Property):
2866class RowFormatProperty(Property):
2867    arg_types = {"this": True}
arg_types = {'this': True}
key = 'rowformatproperty'
class RowFormatDelimitedProperty(Property):
2870class RowFormatDelimitedProperty(Property):
2871    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
2872    arg_types = {
2873        "fields": False,
2874        "escaped": False,
2875        "collection_items": False,
2876        "map_keys": False,
2877        "lines": False,
2878        "null": False,
2879        "serde": False,
2880    }
arg_types = {'fields': False, 'escaped': False, 'collection_items': False, 'map_keys': False, 'lines': False, 'null': False, 'serde': False}
key = 'rowformatdelimitedproperty'
class RowFormatSerdeProperty(Property):
2883class RowFormatSerdeProperty(Property):
2884    arg_types = {"this": True, "serde_properties": False}
arg_types = {'this': True, 'serde_properties': False}
key = 'rowformatserdeproperty'
class QueryTransform(Expression):
2888class QueryTransform(Expression):
2889    arg_types = {
2890        "expressions": True,
2891        "command_script": True,
2892        "schema": False,
2893        "row_format_before": False,
2894        "record_writer": False,
2895        "row_format_after": False,
2896        "record_reader": False,
2897    }
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):
2900class SampleProperty(Property):
2901    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sampleproperty'
class SecurityProperty(Property):
2905class SecurityProperty(Property):
2906    arg_types = {"this": True}
arg_types = {'this': True}
key = 'securityproperty'
class SchemaCommentProperty(Property):
2909class SchemaCommentProperty(Property):
2910    arg_types = {"this": True}
arg_types = {'this': True}
key = 'schemacommentproperty'
class SerdeProperties(Property):
2913class SerdeProperties(Property):
2914    arg_types = {"expressions": True, "with": False}
arg_types = {'expressions': True, 'with': False}
key = 'serdeproperties'
class SetProperty(Property):
2917class SetProperty(Property):
2918    arg_types = {"multi": True}
arg_types = {'multi': True}
key = 'setproperty'
class SharingProperty(Property):
2921class SharingProperty(Property):
2922    arg_types = {"this": False}
arg_types = {'this': False}
key = 'sharingproperty'
class SetConfigProperty(Property):
2925class SetConfigProperty(Property):
2926    arg_types = {"this": True}
arg_types = {'this': True}
key = 'setconfigproperty'
class SettingsProperty(Property):
2929class SettingsProperty(Property):
2930    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'settingsproperty'
class SortKeyProperty(Property):
2933class SortKeyProperty(Property):
2934    arg_types = {"this": True, "compound": False}
arg_types = {'this': True, 'compound': False}
key = 'sortkeyproperty'
class SqlReadWriteProperty(Property):
2937class SqlReadWriteProperty(Property):
2938    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sqlreadwriteproperty'
class SqlSecurityProperty(Property):
2941class SqlSecurityProperty(Property):
2942    arg_types = {"definer": True}
arg_types = {'definer': True}
key = 'sqlsecurityproperty'
class StabilityProperty(Property):
2945class StabilityProperty(Property):
2946    arg_types = {"this": True}
arg_types = {'this': True}
key = 'stabilityproperty'
class TemporaryProperty(Property):
2949class TemporaryProperty(Property):
2950    arg_types = {"this": False}
arg_types = {'this': False}
key = 'temporaryproperty'
class SecureProperty(Property):
2953class SecureProperty(Property):
2954    arg_types = {}
arg_types = {}
key = 'secureproperty'
class TransformModelProperty(Property):
2957class TransformModelProperty(Property):
2958    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'transformmodelproperty'
class TransientProperty(Property):
2961class TransientProperty(Property):
2962    arg_types = {"this": False}
arg_types = {'this': False}
key = 'transientproperty'
class UnloggedProperty(Property):
2965class UnloggedProperty(Property):
2966    arg_types = {}
arg_types = {}
key = 'unloggedproperty'
class ViewAttributeProperty(Property):
2970class ViewAttributeProperty(Property):
2971    arg_types = {"this": True}
arg_types = {'this': True}
key = 'viewattributeproperty'
class VolatileProperty(Property):
2974class VolatileProperty(Property):
2975    arg_types = {"this": False}
arg_types = {'this': False}
key = 'volatileproperty'
class WithDataProperty(Property):
2978class WithDataProperty(Property):
2979    arg_types = {"no": True, "statistics": False}
arg_types = {'no': True, 'statistics': False}
key = 'withdataproperty'
class WithJournalTableProperty(Property):
2982class WithJournalTableProperty(Property):
2983    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withjournaltableproperty'
class WithSchemaBindingProperty(Property):
2986class WithSchemaBindingProperty(Property):
2987    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withschemabindingproperty'
class WithSystemVersioningProperty(Property):
2990class WithSystemVersioningProperty(Property):
2991    arg_types = {
2992        "on": False,
2993        "this": False,
2994        "data_consistency": False,
2995        "retention_period": False,
2996        "with": True,
2997    }
arg_types = {'on': False, 'this': False, 'data_consistency': False, 'retention_period': False, 'with': True}
key = 'withsystemversioningproperty'
class WithProcedureOptions(Property):
3000class WithProcedureOptions(Property):
3001    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withprocedureoptions'
class Properties(Expression):
3004class Properties(Expression):
3005    arg_types = {"expressions": True}
3006
3007    NAME_TO_PROPERTY = {
3008        "ALGORITHM": AlgorithmProperty,
3009        "AUTO_INCREMENT": AutoIncrementProperty,
3010        "CHARACTER SET": CharacterSetProperty,
3011        "CLUSTERED_BY": ClusteredByProperty,
3012        "COLLATE": CollateProperty,
3013        "COMMENT": SchemaCommentProperty,
3014        "DEFINER": DefinerProperty,
3015        "DISTKEY": DistKeyProperty,
3016        "DISTRIBUTED_BY": DistributedByProperty,
3017        "DISTSTYLE": DistStyleProperty,
3018        "ENGINE": EngineProperty,
3019        "EXECUTE AS": ExecuteAsProperty,
3020        "FORMAT": FileFormatProperty,
3021        "LANGUAGE": LanguageProperty,
3022        "LOCATION": LocationProperty,
3023        "LOCK": LockProperty,
3024        "PARTITIONED_BY": PartitionedByProperty,
3025        "RETURNS": ReturnsProperty,
3026        "ROW_FORMAT": RowFormatProperty,
3027        "SORTKEY": SortKeyProperty,
3028    }
3029
3030    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
3031
3032    # CREATE property locations
3033    # Form: schema specified
3034    #   create [POST_CREATE]
3035    #     table a [POST_NAME]
3036    #     (b int) [POST_SCHEMA]
3037    #     with ([POST_WITH])
3038    #     index (b) [POST_INDEX]
3039    #
3040    # Form: alias selection
3041    #   create [POST_CREATE]
3042    #     table a [POST_NAME]
3043    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
3044    #     index (c) [POST_INDEX]
3045    class Location(AutoName):
3046        POST_CREATE = auto()
3047        POST_NAME = auto()
3048        POST_SCHEMA = auto()
3049        POST_WITH = auto()
3050        POST_ALIAS = auto()
3051        POST_EXPRESSION = auto()
3052        POST_INDEX = auto()
3053        UNSUPPORTED = auto()
3054
3055    @classmethod
3056    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3057        expressions = []
3058        for key, value in properties_dict.items():
3059            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3060            if property_cls:
3061                expressions.append(property_cls(this=convert(value)))
3062            else:
3063                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3064
3065        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'>, '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'>}
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 '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'}
@classmethod
def from_dict(cls, properties_dict: Dict) -> Properties:
3055    @classmethod
3056    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3057        expressions = []
3058        for key, value in properties_dict.items():
3059            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3060            if property_cls:
3061                expressions.append(property_cls(this=convert(value)))
3062            else:
3063                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3064
3065        return cls(expressions=expressions)
key = 'properties'
class Properties.Location(sqlglot.helper.AutoName):
3045    class Location(AutoName):
3046        POST_CREATE = auto()
3047        POST_NAME = auto()
3048        POST_SCHEMA = auto()
3049        POST_WITH = auto()
3050        POST_ALIAS = auto()
3051        POST_EXPRESSION = auto()
3052        POST_INDEX = auto()
3053        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):
3068class Qualify(Expression):
3069    pass
key = 'qualify'
class InputOutputFormat(Expression):
3072class InputOutputFormat(Expression):
3073    arg_types = {"input_format": False, "output_format": False}
arg_types = {'input_format': False, 'output_format': False}
key = 'inputoutputformat'
class Return(Expression):
3077class Return(Expression):
3078    pass
key = 'return'
class Reference(Expression):
3081class Reference(Expression):
3082    arg_types = {"this": True, "expressions": False, "options": False}
arg_types = {'this': True, 'expressions': False, 'options': False}
key = 'reference'
class Tuple(Expression):
3085class Tuple(Expression):
3086    arg_types = {"expressions": False}
3087
3088    def isin(
3089        self,
3090        *expressions: t.Any,
3091        query: t.Optional[ExpOrStr] = None,
3092        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3093        copy: bool = True,
3094        **opts,
3095    ) -> In:
3096        return In(
3097            this=maybe_copy(self, copy),
3098            expressions=[convert(e, copy=copy) for e in expressions],
3099            query=maybe_parse(query, copy=copy, **opts) if query else None,
3100            unnest=(
3101                Unnest(
3102                    expressions=[
3103                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3104                        for e in ensure_list(unnest)
3105                    ]
3106                )
3107                if unnest
3108                else None
3109            ),
3110        )
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:
3088    def isin(
3089        self,
3090        *expressions: t.Any,
3091        query: t.Optional[ExpOrStr] = None,
3092        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3093        copy: bool = True,
3094        **opts,
3095    ) -> In:
3096        return In(
3097            this=maybe_copy(self, copy),
3098            expressions=[convert(e, copy=copy) for e in expressions],
3099            query=maybe_parse(query, copy=copy, **opts) if query else None,
3100            unnest=(
3101                Unnest(
3102                    expressions=[
3103                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3104                        for e in ensure_list(unnest)
3105                    ]
3106                )
3107                if unnest
3108                else None
3109            ),
3110        )
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):
3141class QueryOption(Expression):
3142    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'queryoption'
class WithTableHint(Expression):
3146class WithTableHint(Expression):
3147    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withtablehint'
class IndexTableHint(Expression):
3151class IndexTableHint(Expression):
3152    arg_types = {"this": True, "expressions": False, "target": False}
arg_types = {'this': True, 'expressions': False, 'target': False}
key = 'indextablehint'
class HistoricalData(Expression):
3156class HistoricalData(Expression):
3157    arg_types = {"this": True, "kind": True, "expression": True}
arg_types = {'this': True, 'kind': True, 'expression': True}
key = 'historicaldata'
class Table(Expression):
3160class Table(Expression):
3161    arg_types = {
3162        "this": False,
3163        "alias": False,
3164        "db": False,
3165        "catalog": False,
3166        "laterals": False,
3167        "joins": False,
3168        "pivots": False,
3169        "hints": False,
3170        "system_time": False,
3171        "version": False,
3172        "format": False,
3173        "pattern": False,
3174        "ordinality": False,
3175        "when": False,
3176        "only": False,
3177        "partition": False,
3178        "changes": False,
3179        "rows_from": False,
3180        "sample": False,
3181    }
3182
3183    @property
3184    def name(self) -> str:
3185        if isinstance(self.this, Func):
3186            return ""
3187        return self.this.name
3188
3189    @property
3190    def db(self) -> str:
3191        return self.text("db")
3192
3193    @property
3194    def catalog(self) -> str:
3195        return self.text("catalog")
3196
3197    @property
3198    def selects(self) -> t.List[Expression]:
3199        return []
3200
3201    @property
3202    def named_selects(self) -> t.List[str]:
3203        return []
3204
3205    @property
3206    def parts(self) -> t.List[Expression]:
3207        """Return the parts of a table in order catalog, db, table."""
3208        parts: t.List[Expression] = []
3209
3210        for arg in ("catalog", "db", "this"):
3211            part = self.args.get(arg)
3212
3213            if isinstance(part, Dot):
3214                parts.extend(part.flatten())
3215            elif isinstance(part, Expression):
3216                parts.append(part)
3217
3218        return parts
3219
3220    def to_column(self, copy: bool = True) -> Alias | Column | Dot:
3221        parts = self.parts
3222        last_part = parts[-1]
3223
3224        if isinstance(last_part, Identifier):
3225            col = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3226        else:
3227            # This branch will be reached if a function or array is wrapped in a `Table`
3228            col = last_part
3229
3230        alias = self.args.get("alias")
3231        if alias:
3232            col = alias_(col, alias.this, copy=copy)
3233
3234        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
3183    @property
3184    def name(self) -> str:
3185        if isinstance(self.this, Func):
3186            return ""
3187        return self.this.name
db: str
3189    @property
3190    def db(self) -> str:
3191        return self.text("db")
catalog: str
3193    @property
3194    def catalog(self) -> str:
3195        return self.text("catalog")
selects: List[Expression]
3197    @property
3198    def selects(self) -> t.List[Expression]:
3199        return []
named_selects: List[str]
3201    @property
3202    def named_selects(self) -> t.List[str]:
3203        return []
parts: List[Expression]
3205    @property
3206    def parts(self) -> t.List[Expression]:
3207        """Return the parts of a table in order catalog, db, table."""
3208        parts: t.List[Expression] = []
3209
3210        for arg in ("catalog", "db", "this"):
3211            part = self.args.get(arg)
3212
3213            if isinstance(part, Dot):
3214                parts.extend(part.flatten())
3215            elif isinstance(part, Expression):
3216                parts.append(part)
3217
3218        return parts

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

def to_column( self, copy: bool = True) -> Alias | Column | Dot:
3220    def to_column(self, copy: bool = True) -> Alias | Column | Dot:
3221        parts = self.parts
3222        last_part = parts[-1]
3223
3224        if isinstance(last_part, Identifier):
3225            col = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3226        else:
3227            # This branch will be reached if a function or array is wrapped in a `Table`
3228            col = last_part
3229
3230        alias = self.args.get("alias")
3231        if alias:
3232            col = alias_(col, alias.this, copy=copy)
3233
3234        return col
key = 'table'
class SetOperation(Query):
3237class SetOperation(Query):
3238    arg_types = {
3239        "with": False,
3240        "this": True,
3241        "expression": True,
3242        "distinct": False,
3243        "by_name": False,
3244        **QUERY_MODIFIERS,
3245    }
3246
3247    def select(
3248        self: S,
3249        *expressions: t.Optional[ExpOrStr],
3250        append: bool = True,
3251        dialect: DialectType = None,
3252        copy: bool = True,
3253        **opts,
3254    ) -> S:
3255        this = maybe_copy(self, copy)
3256        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3257        this.expression.unnest().select(
3258            *expressions, append=append, dialect=dialect, copy=False, **opts
3259        )
3260        return this
3261
3262    @property
3263    def named_selects(self) -> t.List[str]:
3264        return self.this.unnest().named_selects
3265
3266    @property
3267    def is_star(self) -> bool:
3268        return self.this.is_star or self.expression.is_star
3269
3270    @property
3271    def selects(self) -> t.List[Expression]:
3272        return self.this.unnest().selects
3273
3274    @property
3275    def left(self) -> Query:
3276        return self.this
3277
3278    @property
3279    def right(self) -> Query:
3280        return self.expression
arg_types = {'with': False, 'this': True, 'expression': True, 'distinct': False, 'by_name': 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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> ~S:
3247    def select(
3248        self: S,
3249        *expressions: t.Optional[ExpOrStr],
3250        append: bool = True,
3251        dialect: DialectType = None,
3252        copy: bool = True,
3253        **opts,
3254    ) -> S:
3255        this = maybe_copy(self, copy)
3256        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3257        this.expression.unnest().select(
3258            *expressions, append=append, dialect=dialect, copy=False, **opts
3259        )
3260        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]
3262    @property
3263    def named_selects(self) -> t.List[str]:
3264        return self.this.unnest().named_selects

Returns the output names of the query's projections.

is_star: bool
3266    @property
3267    def is_star(self) -> bool:
3268        return self.this.is_star or self.expression.is_star

Checks whether an expression is a star.

selects: List[Expression]
3270    @property
3271    def selects(self) -> t.List[Expression]:
3272        return self.this.unnest().selects

Returns the query's projections.

left: Query
3274    @property
3275    def left(self) -> Query:
3276        return self.this
right: Query
3278    @property
3279    def right(self) -> Query:
3280        return self.expression
key = 'setoperation'
class Union(SetOperation):
3283class Union(SetOperation):
3284    pass
key = 'union'
class Except(SetOperation):
3287class Except(SetOperation):
3288    pass
key = 'except'
class Intersect(SetOperation):
3291class Intersect(SetOperation):
3292    pass
key = 'intersect'
class Update(DML):
3295class Update(DML):
3296    arg_types = {
3297        "with": False,
3298        "this": False,
3299        "expressions": True,
3300        "from": False,
3301        "where": False,
3302        "returning": False,
3303        "order": False,
3304        "limit": False,
3305    }
3306
3307    def table(
3308        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3309    ) -> Update:
3310        """
3311        Set the table to update.
3312
3313        Example:
3314            >>> Update().table("my_table").set_("x = 1").sql()
3315            'UPDATE my_table SET x = 1'
3316
3317        Args:
3318            expression : the SQL code strings to parse.
3319                If a `Table` instance is passed, this is used as-is.
3320                If another `Expression` instance is passed, it will be wrapped in a `Table`.
3321            dialect: the dialect used to parse the input expression.
3322            copy: if `False`, modify this expression instance in-place.
3323            opts: other options to use to parse the input expressions.
3324
3325        Returns:
3326            The modified Update expression.
3327        """
3328        return _apply_builder(
3329            expression=expression,
3330            instance=self,
3331            arg="this",
3332            into=Table,
3333            prefix=None,
3334            dialect=dialect,
3335            copy=copy,
3336            **opts,
3337        )
3338
3339    def set_(
3340        self,
3341        *expressions: ExpOrStr,
3342        append: bool = True,
3343        dialect: DialectType = None,
3344        copy: bool = True,
3345        **opts,
3346    ) -> Update:
3347        """
3348        Append to or set the SET expressions.
3349
3350        Example:
3351            >>> Update().table("my_table").set_("x = 1").sql()
3352            'UPDATE my_table SET x = 1'
3353
3354        Args:
3355            *expressions: the SQL code strings to parse.
3356                If `Expression` instance(s) are passed, they will be used as-is.
3357                Multiple expressions are combined with a comma.
3358            append: if `True`, add the new expressions to any existing SET expressions.
3359                Otherwise, this resets the expressions.
3360            dialect: the dialect used to parse the input expressions.
3361            copy: if `False`, modify this expression instance in-place.
3362            opts: other options to use to parse the input expressions.
3363        """
3364        return _apply_list_builder(
3365            *expressions,
3366            instance=self,
3367            arg="expressions",
3368            append=append,
3369            into=Expression,
3370            prefix=None,
3371            dialect=dialect,
3372            copy=copy,
3373            **opts,
3374        )
3375
3376    def where(
3377        self,
3378        *expressions: t.Optional[ExpOrStr],
3379        append: bool = True,
3380        dialect: DialectType = None,
3381        copy: bool = True,
3382        **opts,
3383    ) -> Select:
3384        """
3385        Append to or set the WHERE expressions.
3386
3387        Example:
3388            >>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
3389            "UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
3390
3391        Args:
3392            *expressions: the SQL code strings to parse.
3393                If an `Expression` instance is passed, it will be used as-is.
3394                Multiple expressions are combined with an AND operator.
3395            append: if `True`, AND the new expressions to any existing expression.
3396                Otherwise, this resets the expression.
3397            dialect: the dialect used to parse the input expressions.
3398            copy: if `False`, modify this expression instance in-place.
3399            opts: other options to use to parse the input expressions.
3400
3401        Returns:
3402            Select: the modified expression.
3403        """
3404        return _apply_conjunction_builder(
3405            *expressions,
3406            instance=self,
3407            arg="where",
3408            append=append,
3409            into=Where,
3410            dialect=dialect,
3411            copy=copy,
3412            **opts,
3413        )
3414
3415    def from_(
3416        self,
3417        expression: t.Optional[ExpOrStr] = None,
3418        dialect: DialectType = None,
3419        copy: bool = True,
3420        **opts,
3421    ) -> Update:
3422        """
3423        Set the FROM expression.
3424
3425        Example:
3426            >>> Update().table("my_table").set_("x = 1").from_("baz").sql()
3427            'UPDATE my_table SET x = 1 FROM baz'
3428
3429        Args:
3430            expression : the SQL code strings to parse.
3431                If a `From` instance is passed, this is used as-is.
3432                If another `Expression` instance is passed, it will be wrapped in a `From`.
3433                If nothing is passed in then a from is not applied to the expression
3434            dialect: the dialect used to parse the input expression.
3435            copy: if `False`, modify this expression instance in-place.
3436            opts: other options to use to parse the input expressions.
3437
3438        Returns:
3439            The modified Update expression.
3440        """
3441        if not expression:
3442            return maybe_copy(self, copy)
3443
3444        return _apply_builder(
3445            expression=expression,
3446            instance=self,
3447            arg="from",
3448            into=From,
3449            prefix="FROM",
3450            dialect=dialect,
3451            copy=copy,
3452            **opts,
3453        )
3454
3455    def with_(
3456        self,
3457        alias: ExpOrStr,
3458        as_: ExpOrStr,
3459        recursive: t.Optional[bool] = None,
3460        materialized: t.Optional[bool] = None,
3461        append: bool = True,
3462        dialect: DialectType = None,
3463        copy: bool = True,
3464        **opts,
3465    ) -> Update:
3466        """
3467        Append to or set the common table expressions.
3468
3469        Example:
3470            >>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
3471            'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
3472
3473        Args:
3474            alias: the SQL code string to parse as the table name.
3475                If an `Expression` instance is passed, this is used as-is.
3476            as_: the SQL code string to parse as the table expression.
3477                If an `Expression` instance is passed, it will be used as-is.
3478            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
3479            materialized: set the MATERIALIZED part of the expression.
3480            append: if `True`, add to any existing expressions.
3481                Otherwise, this resets the expressions.
3482            dialect: the dialect used to parse the input expression.
3483            copy: if `False`, modify this expression instance in-place.
3484            opts: other options to use to parse the input expressions.
3485
3486        Returns:
3487            The modified expression.
3488        """
3489        return _apply_cte_builder(
3490            self,
3491            alias,
3492            as_,
3493            recursive=recursive,
3494            materialized=materialized,
3495            append=append,
3496            dialect=dialect,
3497            copy=copy,
3498            **opts,
3499        )
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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3307    def table(
3308        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3309    ) -> Update:
3310        """
3311        Set the table to update.
3312
3313        Example:
3314            >>> Update().table("my_table").set_("x = 1").sql()
3315            'UPDATE my_table SET x = 1'
3316
3317        Args:
3318            expression : the SQL code strings to parse.
3319                If a `Table` instance is passed, this is used as-is.
3320                If another `Expression` instance is passed, it will be wrapped in a `Table`.
3321            dialect: the dialect used to parse the input expression.
3322            copy: if `False`, modify this expression instance in-place.
3323            opts: other options to use to parse the input expressions.
3324
3325        Returns:
3326            The modified Update expression.
3327        """
3328        return _apply_builder(
3329            expression=expression,
3330            instance=self,
3331            arg="this",
3332            into=Table,
3333            prefix=None,
3334            dialect=dialect,
3335            copy=copy,
3336            **opts,
3337        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3339    def set_(
3340        self,
3341        *expressions: ExpOrStr,
3342        append: bool = True,
3343        dialect: DialectType = None,
3344        copy: bool = True,
3345        **opts,
3346    ) -> Update:
3347        """
3348        Append to or set the SET expressions.
3349
3350        Example:
3351            >>> Update().table("my_table").set_("x = 1").sql()
3352            'UPDATE my_table SET x = 1'
3353
3354        Args:
3355            *expressions: the SQL code strings to parse.
3356                If `Expression` instance(s) are passed, they will be used as-is.
3357                Multiple expressions are combined with a comma.
3358            append: if `True`, add the new expressions to any existing SET expressions.
3359                Otherwise, this resets the expressions.
3360            dialect: the dialect used to parse the input expressions.
3361            copy: if `False`, modify this expression instance in-place.
3362            opts: other options to use to parse the input expressions.
3363        """
3364        return _apply_list_builder(
3365            *expressions,
3366            instance=self,
3367            arg="expressions",
3368            append=append,
3369            into=Expression,
3370            prefix=None,
3371            dialect=dialect,
3372            copy=copy,
3373            **opts,
3374        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3376    def where(
3377        self,
3378        *expressions: t.Optional[ExpOrStr],
3379        append: bool = True,
3380        dialect: DialectType = None,
3381        copy: bool = True,
3382        **opts,
3383    ) -> Select:
3384        """
3385        Append to or set the WHERE expressions.
3386
3387        Example:
3388            >>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
3389            "UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
3390
3391        Args:
3392            *expressions: the SQL code strings to parse.
3393                If an `Expression` instance is passed, it will be used as-is.
3394                Multiple expressions are combined with an AND operator.
3395            append: if `True`, AND the new expressions to any existing expression.
3396                Otherwise, this resets the expression.
3397            dialect: the dialect used to parse the input expressions.
3398            copy: if `False`, modify this expression instance in-place.
3399            opts: other options to use to parse the input expressions.
3400
3401        Returns:
3402            Select: the modified expression.
3403        """
3404        return _apply_conjunction_builder(
3405            *expressions,
3406            instance=self,
3407            arg="where",
3408            append=append,
3409            into=Where,
3410            dialect=dialect,
3411            copy=copy,
3412            **opts,
3413        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3415    def from_(
3416        self,
3417        expression: t.Optional[ExpOrStr] = None,
3418        dialect: DialectType = None,
3419        copy: bool = True,
3420        **opts,
3421    ) -> Update:
3422        """
3423        Set the FROM expression.
3424
3425        Example:
3426            >>> Update().table("my_table").set_("x = 1").from_("baz").sql()
3427            'UPDATE my_table SET x = 1 FROM baz'
3428
3429        Args:
3430            expression : the SQL code strings to parse.
3431                If a `From` instance is passed, this is used as-is.
3432                If another `Expression` instance is passed, it will be wrapped in a `From`.
3433                If nothing is passed in then a from is not applied to the expression
3434            dialect: the dialect used to parse the input expression.
3435            copy: if `False`, modify this expression instance in-place.
3436            opts: other options to use to parse the input expressions.
3437
3438        Returns:
3439            The modified Update expression.
3440        """
3441        if not expression:
3442            return maybe_copy(self, copy)
3443
3444        return _apply_builder(
3445            expression=expression,
3446            instance=self,
3447            arg="from",
3448            into=From,
3449            prefix="FROM",
3450            dialect=dialect,
3451            copy=copy,
3452            **opts,
3453        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Update:
3455    def with_(
3456        self,
3457        alias: ExpOrStr,
3458        as_: ExpOrStr,
3459        recursive: t.Optional[bool] = None,
3460        materialized: t.Optional[bool] = None,
3461        append: bool = True,
3462        dialect: DialectType = None,
3463        copy: bool = True,
3464        **opts,
3465    ) -> Update:
3466        """
3467        Append to or set the common table expressions.
3468
3469        Example:
3470            >>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
3471            'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
3472
3473        Args:
3474            alias: the SQL code string to parse as the table name.
3475                If an `Expression` instance is passed, this is used as-is.
3476            as_: the SQL code string to parse as the table expression.
3477                If an `Expression` instance is passed, it will be used as-is.
3478            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
3479            materialized: set the MATERIALIZED part of the expression.
3480            append: if `True`, add to any existing expressions.
3481                Otherwise, this resets the expressions.
3482            dialect: the dialect used to parse the input expression.
3483            copy: if `False`, modify this expression instance in-place.
3484            opts: other options to use to parse the input expressions.
3485
3486        Returns:
3487            The modified expression.
3488        """
3489        return _apply_cte_builder(
3490            self,
3491            alias,
3492            as_,
3493            recursive=recursive,
3494            materialized=materialized,
3495            append=append,
3496            dialect=dialect,
3497            copy=copy,
3498            **opts,
3499        )

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):
3502class Values(UDTF):
3503    arg_types = {"expressions": True, "alias": False}
arg_types = {'expressions': True, 'alias': False}
key = 'values'
class Var(Expression):
3506class Var(Expression):
3507    pass
key = 'var'
class Version(Expression):
3510class Version(Expression):
3511    """
3512    Time travel, iceberg, bigquery etc
3513    https://trino.io/docs/current/connector/iceberg.html?highlight=snapshot#using-snapshots
3514    https://www.databricks.com/blog/2019/02/04/introducing-delta-time-travel-for-large-scale-data-lakes.html
3515    https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#for_system_time_as_of
3516    https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-ver16
3517    this is either TIMESTAMP or VERSION
3518    kind is ("AS OF", "BETWEEN")
3519    """
3520
3521    arg_types = {"this": True, "kind": True, "expression": False}
arg_types = {'this': True, 'kind': True, 'expression': False}
key = 'version'
class Schema(Expression):
3524class Schema(Expression):
3525    arg_types = {"this": False, "expressions": False}
arg_types = {'this': False, 'expressions': False}
key = 'schema'
class Lock(Expression):
3530class Lock(Expression):
3531    arg_types = {"update": True, "expressions": False, "wait": False}
arg_types = {'update': True, 'expressions': False, 'wait': False}
key = 'lock'
class Select(Query):
3534class Select(Query):
3535    arg_types = {
3536        "with": False,
3537        "kind": False,
3538        "expressions": False,
3539        "hint": False,
3540        "distinct": False,
3541        "into": False,
3542        "from": False,
3543        "operation_modifiers": False,
3544        **QUERY_MODIFIERS,
3545    }
3546
3547    def from_(
3548        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3549    ) -> Select:
3550        """
3551        Set the FROM expression.
3552
3553        Example:
3554            >>> Select().from_("tbl").select("x").sql()
3555            'SELECT x FROM tbl'
3556
3557        Args:
3558            expression : the SQL code strings to parse.
3559                If a `From` instance is passed, this is used as-is.
3560                If another `Expression` instance is passed, it will be wrapped in a `From`.
3561            dialect: the dialect used to parse the input expression.
3562            copy: if `False`, modify this expression instance in-place.
3563            opts: other options to use to parse the input expressions.
3564
3565        Returns:
3566            The modified Select expression.
3567        """
3568        return _apply_builder(
3569            expression=expression,
3570            instance=self,
3571            arg="from",
3572            into=From,
3573            prefix="FROM",
3574            dialect=dialect,
3575            copy=copy,
3576            **opts,
3577        )
3578
3579    def group_by(
3580        self,
3581        *expressions: t.Optional[ExpOrStr],
3582        append: bool = True,
3583        dialect: DialectType = None,
3584        copy: bool = True,
3585        **opts,
3586    ) -> Select:
3587        """
3588        Set the GROUP BY expression.
3589
3590        Example:
3591            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
3592            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
3593
3594        Args:
3595            *expressions: the SQL code strings to parse.
3596                If a `Group` instance is passed, this is used as-is.
3597                If another `Expression` instance is passed, it will be wrapped in a `Group`.
3598                If nothing is passed in then a group by is not applied to the expression
3599            append: if `True`, add to any existing expressions.
3600                Otherwise, this flattens all the `Group` expression into a single expression.
3601            dialect: the dialect used to parse the input expression.
3602            copy: if `False`, modify this expression instance in-place.
3603            opts: other options to use to parse the input expressions.
3604
3605        Returns:
3606            The modified Select expression.
3607        """
3608        if not expressions:
3609            return self if not copy else self.copy()
3610
3611        return _apply_child_list_builder(
3612            *expressions,
3613            instance=self,
3614            arg="group",
3615            append=append,
3616            copy=copy,
3617            prefix="GROUP BY",
3618            into=Group,
3619            dialect=dialect,
3620            **opts,
3621        )
3622
3623    def sort_by(
3624        self,
3625        *expressions: t.Optional[ExpOrStr],
3626        append: bool = True,
3627        dialect: DialectType = None,
3628        copy: bool = True,
3629        **opts,
3630    ) -> Select:
3631        """
3632        Set the SORT BY expression.
3633
3634        Example:
3635            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
3636            'SELECT x FROM tbl SORT BY x DESC'
3637
3638        Args:
3639            *expressions: the SQL code strings to parse.
3640                If a `Group` instance is passed, this is used as-is.
3641                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
3642            append: if `True`, add to any existing expressions.
3643                Otherwise, this flattens all the `Order` expression into a single expression.
3644            dialect: the dialect used to parse the input expression.
3645            copy: if `False`, modify this expression instance in-place.
3646            opts: other options to use to parse the input expressions.
3647
3648        Returns:
3649            The modified Select expression.
3650        """
3651        return _apply_child_list_builder(
3652            *expressions,
3653            instance=self,
3654            arg="sort",
3655            append=append,
3656            copy=copy,
3657            prefix="SORT BY",
3658            into=Sort,
3659            dialect=dialect,
3660            **opts,
3661        )
3662
3663    def cluster_by(
3664        self,
3665        *expressions: t.Optional[ExpOrStr],
3666        append: bool = True,
3667        dialect: DialectType = None,
3668        copy: bool = True,
3669        **opts,
3670    ) -> Select:
3671        """
3672        Set the CLUSTER BY expression.
3673
3674        Example:
3675            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
3676            'SELECT x FROM tbl CLUSTER BY x DESC'
3677
3678        Args:
3679            *expressions: the SQL code strings to parse.
3680                If a `Group` instance is passed, this is used as-is.
3681                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
3682            append: if `True`, add to any existing expressions.
3683                Otherwise, this flattens all the `Order` expression into a single expression.
3684            dialect: the dialect used to parse the input expression.
3685            copy: if `False`, modify this expression instance in-place.
3686            opts: other options to use to parse the input expressions.
3687
3688        Returns:
3689            The modified Select expression.
3690        """
3691        return _apply_child_list_builder(
3692            *expressions,
3693            instance=self,
3694            arg="cluster",
3695            append=append,
3696            copy=copy,
3697            prefix="CLUSTER BY",
3698            into=Cluster,
3699            dialect=dialect,
3700            **opts,
3701        )
3702
3703    def select(
3704        self,
3705        *expressions: t.Optional[ExpOrStr],
3706        append: bool = True,
3707        dialect: DialectType = None,
3708        copy: bool = True,
3709        **opts,
3710    ) -> Select:
3711        return _apply_list_builder(
3712            *expressions,
3713            instance=self,
3714            arg="expressions",
3715            append=append,
3716            dialect=dialect,
3717            into=Expression,
3718            copy=copy,
3719            **opts,
3720        )
3721
3722    def lateral(
3723        self,
3724        *expressions: t.Optional[ExpOrStr],
3725        append: bool = True,
3726        dialect: DialectType = None,
3727        copy: bool = True,
3728        **opts,
3729    ) -> Select:
3730        """
3731        Append to or set the LATERAL expressions.
3732
3733        Example:
3734            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3735            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3736
3737        Args:
3738            *expressions: the SQL code strings to parse.
3739                If an `Expression` instance is passed, it will be used as-is.
3740            append: if `True`, add to any existing expressions.
3741                Otherwise, this resets the expressions.
3742            dialect: the dialect used to parse the input expressions.
3743            copy: if `False`, modify this expression instance in-place.
3744            opts: other options to use to parse the input expressions.
3745
3746        Returns:
3747            The modified Select expression.
3748        """
3749        return _apply_list_builder(
3750            *expressions,
3751            instance=self,
3752            arg="laterals",
3753            append=append,
3754            into=Lateral,
3755            prefix="LATERAL VIEW",
3756            dialect=dialect,
3757            copy=copy,
3758            **opts,
3759        )
3760
3761    def join(
3762        self,
3763        expression: ExpOrStr,
3764        on: t.Optional[ExpOrStr] = None,
3765        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3766        append: bool = True,
3767        join_type: t.Optional[str] = None,
3768        join_alias: t.Optional[Identifier | str] = None,
3769        dialect: DialectType = None,
3770        copy: bool = True,
3771        **opts,
3772    ) -> Select:
3773        """
3774        Append to or set the JOIN expressions.
3775
3776        Example:
3777            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
3778            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
3779
3780            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
3781            'SELECT 1 FROM a JOIN b USING (x, y, z)'
3782
3783            Use `join_type` to change the type of join:
3784
3785            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
3786            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
3787
3788        Args:
3789            expression: the SQL code string to parse.
3790                If an `Expression` instance is passed, it will be used as-is.
3791            on: optionally specify the join "on" criteria as a SQL string.
3792                If an `Expression` instance is passed, it will be used as-is.
3793            using: optionally specify the join "using" criteria as a SQL string.
3794                If an `Expression` instance is passed, it will be used as-is.
3795            append: if `True`, add to any existing expressions.
3796                Otherwise, this resets the expressions.
3797            join_type: if set, alter the parsed join type.
3798            join_alias: an optional alias for the joined source.
3799            dialect: the dialect used to parse the input expressions.
3800            copy: if `False`, modify this expression instance in-place.
3801            opts: other options to use to parse the input expressions.
3802
3803        Returns:
3804            Select: the modified expression.
3805        """
3806        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
3807
3808        try:
3809            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
3810        except ParseError:
3811            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
3812
3813        join = expression if isinstance(expression, Join) else Join(this=expression)
3814
3815        if isinstance(join.this, Select):
3816            join.this.replace(join.this.subquery())
3817
3818        if join_type:
3819            method: t.Optional[Token]
3820            side: t.Optional[Token]
3821            kind: t.Optional[Token]
3822
3823            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
3824
3825            if method:
3826                join.set("method", method.text)
3827            if side:
3828                join.set("side", side.text)
3829            if kind:
3830                join.set("kind", kind.text)
3831
3832        if on:
3833            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
3834            join.set("on", on)
3835
3836        if using:
3837            join = _apply_list_builder(
3838                *ensure_list(using),
3839                instance=join,
3840                arg="using",
3841                append=append,
3842                copy=copy,
3843                into=Identifier,
3844                **opts,
3845            )
3846
3847        if join_alias:
3848            join.set("this", alias_(join.this, join_alias, table=True))
3849
3850        return _apply_list_builder(
3851            join,
3852            instance=self,
3853            arg="joins",
3854            append=append,
3855            copy=copy,
3856            **opts,
3857        )
3858
3859    def where(
3860        self,
3861        *expressions: t.Optional[ExpOrStr],
3862        append: bool = True,
3863        dialect: DialectType = None,
3864        copy: bool = True,
3865        **opts,
3866    ) -> Select:
3867        """
3868        Append to or set the WHERE expressions.
3869
3870        Example:
3871            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
3872            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
3873
3874        Args:
3875            *expressions: the SQL code strings to parse.
3876                If an `Expression` instance is passed, it will be used as-is.
3877                Multiple expressions are combined with an AND operator.
3878            append: if `True`, AND the new expressions to any existing expression.
3879                Otherwise, this resets the expression.
3880            dialect: the dialect used to parse the input expressions.
3881            copy: if `False`, modify this expression instance in-place.
3882            opts: other options to use to parse the input expressions.
3883
3884        Returns:
3885            Select: the modified expression.
3886        """
3887        return _apply_conjunction_builder(
3888            *expressions,
3889            instance=self,
3890            arg="where",
3891            append=append,
3892            into=Where,
3893            dialect=dialect,
3894            copy=copy,
3895            **opts,
3896        )
3897
3898    def having(
3899        self,
3900        *expressions: t.Optional[ExpOrStr],
3901        append: bool = True,
3902        dialect: DialectType = None,
3903        copy: bool = True,
3904        **opts,
3905    ) -> Select:
3906        """
3907        Append to or set the HAVING expressions.
3908
3909        Example:
3910            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
3911            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
3912
3913        Args:
3914            *expressions: the SQL code strings to parse.
3915                If an `Expression` instance is passed, it will be used as-is.
3916                Multiple expressions are combined with an AND operator.
3917            append: if `True`, AND the new expressions to any existing expression.
3918                Otherwise, this resets the expression.
3919            dialect: the dialect used to parse the input expressions.
3920            copy: if `False`, modify this expression instance in-place.
3921            opts: other options to use to parse the input expressions.
3922
3923        Returns:
3924            The modified Select expression.
3925        """
3926        return _apply_conjunction_builder(
3927            *expressions,
3928            instance=self,
3929            arg="having",
3930            append=append,
3931            into=Having,
3932            dialect=dialect,
3933            copy=copy,
3934            **opts,
3935        )
3936
3937    def window(
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="windows",
3949            append=append,
3950            into=Window,
3951            dialect=dialect,
3952            copy=copy,
3953            **opts,
3954        )
3955
3956    def qualify(
3957        self,
3958        *expressions: t.Optional[ExpOrStr],
3959        append: bool = True,
3960        dialect: DialectType = None,
3961        copy: bool = True,
3962        **opts,
3963    ) -> Select:
3964        return _apply_conjunction_builder(
3965            *expressions,
3966            instance=self,
3967            arg="qualify",
3968            append=append,
3969            into=Qualify,
3970            dialect=dialect,
3971            copy=copy,
3972            **opts,
3973        )
3974
3975    def distinct(
3976        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
3977    ) -> Select:
3978        """
3979        Set the OFFSET expression.
3980
3981        Example:
3982            >>> Select().from_("tbl").select("x").distinct().sql()
3983            'SELECT DISTINCT x FROM tbl'
3984
3985        Args:
3986            ons: the expressions to distinct on
3987            distinct: whether the Select should be distinct
3988            copy: if `False`, modify this expression instance in-place.
3989
3990        Returns:
3991            Select: the modified expression.
3992        """
3993        instance = maybe_copy(self, copy)
3994        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
3995        instance.set("distinct", Distinct(on=on) if distinct else None)
3996        return instance
3997
3998    def ctas(
3999        self,
4000        table: ExpOrStr,
4001        properties: t.Optional[t.Dict] = None,
4002        dialect: DialectType = None,
4003        copy: bool = True,
4004        **opts,
4005    ) -> Create:
4006        """
4007        Convert this expression to a CREATE TABLE AS statement.
4008
4009        Example:
4010            >>> Select().select("*").from_("tbl").ctas("x").sql()
4011            'CREATE TABLE x AS SELECT * FROM tbl'
4012
4013        Args:
4014            table: the SQL code string to parse as the table name.
4015                If another `Expression` instance is passed, it will be used as-is.
4016            properties: an optional mapping of table properties
4017            dialect: the dialect used to parse the input table.
4018            copy: if `False`, modify this expression instance in-place.
4019            opts: other options to use to parse the input table.
4020
4021        Returns:
4022            The new Create expression.
4023        """
4024        instance = maybe_copy(self, copy)
4025        table_expression = maybe_parse(table, into=Table, dialect=dialect, **opts)
4026
4027        properties_expression = None
4028        if properties:
4029            properties_expression = Properties.from_dict(properties)
4030
4031        return Create(
4032            this=table_expression,
4033            kind="TABLE",
4034            expression=instance,
4035            properties=properties_expression,
4036        )
4037
4038    def lock(self, update: bool = True, copy: bool = True) -> Select:
4039        """
4040        Set the locking read mode for this expression.
4041
4042        Examples:
4043            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
4044            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
4045
4046            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
4047            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
4048
4049        Args:
4050            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
4051            copy: if `False`, modify this expression instance in-place.
4052
4053        Returns:
4054            The modified expression.
4055        """
4056        inst = maybe_copy(self, copy)
4057        inst.set("locks", [Lock(update=update)])
4058
4059        return inst
4060
4061    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
4062        """
4063        Set hints for this expression.
4064
4065        Examples:
4066            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
4067            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
4068
4069        Args:
4070            hints: The SQL code strings to parse as the hints.
4071                If an `Expression` instance is passed, it will be used as-is.
4072            dialect: The dialect used to parse the hints.
4073            copy: If `False`, modify this expression instance in-place.
4074
4075        Returns:
4076            The modified expression.
4077        """
4078        inst = maybe_copy(self, copy)
4079        inst.set(
4080            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
4081        )
4082
4083        return inst
4084
4085    @property
4086    def named_selects(self) -> t.List[str]:
4087        return [e.output_name for e in self.expressions if e.alias_or_name]
4088
4089    @property
4090    def is_star(self) -> bool:
4091        return any(expression.is_star for expression in self.expressions)
4092
4093    @property
4094    def selects(self) -> t.List[Expression]:
4095        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3547    def from_(
3548        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
3549    ) -> Select:
3550        """
3551        Set the FROM expression.
3552
3553        Example:
3554            >>> Select().from_("tbl").select("x").sql()
3555            'SELECT x FROM tbl'
3556
3557        Args:
3558            expression : the SQL code strings to parse.
3559                If a `From` instance is passed, this is used as-is.
3560                If another `Expression` instance is passed, it will be wrapped in a `From`.
3561            dialect: the dialect used to parse the input expression.
3562            copy: if `False`, modify this expression instance in-place.
3563            opts: other options to use to parse the input expressions.
3564
3565        Returns:
3566            The modified Select expression.
3567        """
3568        return _apply_builder(
3569            expression=expression,
3570            instance=self,
3571            arg="from",
3572            into=From,
3573            prefix="FROM",
3574            dialect=dialect,
3575            copy=copy,
3576            **opts,
3577        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3579    def group_by(
3580        self,
3581        *expressions: t.Optional[ExpOrStr],
3582        append: bool = True,
3583        dialect: DialectType = None,
3584        copy: bool = True,
3585        **opts,
3586    ) -> Select:
3587        """
3588        Set the GROUP BY expression.
3589
3590        Example:
3591            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
3592            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
3593
3594        Args:
3595            *expressions: the SQL code strings to parse.
3596                If a `Group` instance is passed, this is used as-is.
3597                If another `Expression` instance is passed, it will be wrapped in a `Group`.
3598                If nothing is passed in then a group by is not applied to the expression
3599            append: if `True`, add to any existing expressions.
3600                Otherwise, this flattens all the `Group` expression into a single expression.
3601            dialect: the dialect used to parse the input expression.
3602            copy: if `False`, modify this expression instance in-place.
3603            opts: other options to use to parse the input expressions.
3604
3605        Returns:
3606            The modified Select expression.
3607        """
3608        if not expressions:
3609            return self if not copy else self.copy()
3610
3611        return _apply_child_list_builder(
3612            *expressions,
3613            instance=self,
3614            arg="group",
3615            append=append,
3616            copy=copy,
3617            prefix="GROUP BY",
3618            into=Group,
3619            dialect=dialect,
3620            **opts,
3621        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3623    def sort_by(
3624        self,
3625        *expressions: t.Optional[ExpOrStr],
3626        append: bool = True,
3627        dialect: DialectType = None,
3628        copy: bool = True,
3629        **opts,
3630    ) -> Select:
3631        """
3632        Set the SORT BY expression.
3633
3634        Example:
3635            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
3636            'SELECT x FROM tbl SORT BY x DESC'
3637
3638        Args:
3639            *expressions: the SQL code strings to parse.
3640                If a `Group` instance is passed, this is used as-is.
3641                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
3642            append: if `True`, add to any existing expressions.
3643                Otherwise, this flattens all the `Order` expression into a single expression.
3644            dialect: the dialect used to parse the input expression.
3645            copy: if `False`, modify this expression instance in-place.
3646            opts: other options to use to parse the input expressions.
3647
3648        Returns:
3649            The modified Select expression.
3650        """
3651        return _apply_child_list_builder(
3652            *expressions,
3653            instance=self,
3654            arg="sort",
3655            append=append,
3656            copy=copy,
3657            prefix="SORT BY",
3658            into=Sort,
3659            dialect=dialect,
3660            **opts,
3661        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3663    def cluster_by(
3664        self,
3665        *expressions: t.Optional[ExpOrStr],
3666        append: bool = True,
3667        dialect: DialectType = None,
3668        copy: bool = True,
3669        **opts,
3670    ) -> Select:
3671        """
3672        Set the CLUSTER BY expression.
3673
3674        Example:
3675            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
3676            'SELECT x FROM tbl CLUSTER BY x DESC'
3677
3678        Args:
3679            *expressions: the SQL code strings to parse.
3680                If a `Group` instance is passed, this is used as-is.
3681                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
3682            append: if `True`, add to any existing expressions.
3683                Otherwise, this flattens all the `Order` expression into a single expression.
3684            dialect: the dialect used to parse the input expression.
3685            copy: if `False`, modify this expression instance in-place.
3686            opts: other options to use to parse the input expressions.
3687
3688        Returns:
3689            The modified Select expression.
3690        """
3691        return _apply_child_list_builder(
3692            *expressions,
3693            instance=self,
3694            arg="cluster",
3695            append=append,
3696            copy=copy,
3697            prefix="CLUSTER BY",
3698            into=Cluster,
3699            dialect=dialect,
3700            **opts,
3701        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3703    def select(
3704        self,
3705        *expressions: t.Optional[ExpOrStr],
3706        append: bool = True,
3707        dialect: DialectType = None,
3708        copy: bool = True,
3709        **opts,
3710    ) -> Select:
3711        return _apply_list_builder(
3712            *expressions,
3713            instance=self,
3714            arg="expressions",
3715            append=append,
3716            dialect=dialect,
3717            into=Expression,
3718            copy=copy,
3719            **opts,
3720        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3722    def lateral(
3723        self,
3724        *expressions: t.Optional[ExpOrStr],
3725        append: bool = True,
3726        dialect: DialectType = None,
3727        copy: bool = True,
3728        **opts,
3729    ) -> Select:
3730        """
3731        Append to or set the LATERAL expressions.
3732
3733        Example:
3734            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3735            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3736
3737        Args:
3738            *expressions: the SQL code strings to parse.
3739                If an `Expression` instance is passed, it will be used as-is.
3740            append: if `True`, add to any existing expressions.
3741                Otherwise, this resets the expressions.
3742            dialect: the dialect used to parse the input expressions.
3743            copy: if `False`, modify this expression instance in-place.
3744            opts: other options to use to parse the input expressions.
3745
3746        Returns:
3747            The modified Select expression.
3748        """
3749        return _apply_list_builder(
3750            *expressions,
3751            instance=self,
3752            arg="laterals",
3753            append=append,
3754            into=Lateral,
3755            prefix="LATERAL VIEW",
3756            dialect=dialect,
3757            copy=copy,
3758            **opts,
3759        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3761    def join(
3762        self,
3763        expression: ExpOrStr,
3764        on: t.Optional[ExpOrStr] = None,
3765        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3766        append: bool = True,
3767        join_type: t.Optional[str] = None,
3768        join_alias: t.Optional[Identifier | str] = None,
3769        dialect: DialectType = None,
3770        copy: bool = True,
3771        **opts,
3772    ) -> Select:
3773        """
3774        Append to or set the JOIN expressions.
3775
3776        Example:
3777            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
3778            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
3779
3780            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
3781            'SELECT 1 FROM a JOIN b USING (x, y, z)'
3782
3783            Use `join_type` to change the type of join:
3784
3785            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
3786            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
3787
3788        Args:
3789            expression: the SQL code string to parse.
3790                If an `Expression` instance is passed, it will be used as-is.
3791            on: optionally specify the join "on" criteria as a SQL string.
3792                If an `Expression` instance is passed, it will be used as-is.
3793            using: optionally specify the join "using" criteria as a SQL string.
3794                If an `Expression` instance is passed, it will be used as-is.
3795            append: if `True`, add to any existing expressions.
3796                Otherwise, this resets the expressions.
3797            join_type: if set, alter the parsed join type.
3798            join_alias: an optional alias for the joined source.
3799            dialect: the dialect used to parse the input expressions.
3800            copy: if `False`, modify this expression instance in-place.
3801            opts: other options to use to parse the input expressions.
3802
3803        Returns:
3804            Select: the modified expression.
3805        """
3806        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
3807
3808        try:
3809            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
3810        except ParseError:
3811            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
3812
3813        join = expression if isinstance(expression, Join) else Join(this=expression)
3814
3815        if isinstance(join.this, Select):
3816            join.this.replace(join.this.subquery())
3817
3818        if join_type:
3819            method: t.Optional[Token]
3820            side: t.Optional[Token]
3821            kind: t.Optional[Token]
3822
3823            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
3824
3825            if method:
3826                join.set("method", method.text)
3827            if side:
3828                join.set("side", side.text)
3829            if kind:
3830                join.set("kind", kind.text)
3831
3832        if on:
3833            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
3834            join.set("on", on)
3835
3836        if using:
3837            join = _apply_list_builder(
3838                *ensure_list(using),
3839                instance=join,
3840                arg="using",
3841                append=append,
3842                copy=copy,
3843                into=Identifier,
3844                **opts,
3845            )
3846
3847        if join_alias:
3848            join.set("this", alias_(join.this, join_alias, table=True))
3849
3850        return _apply_list_builder(
3851            join,
3852            instance=self,
3853            arg="joins",
3854            append=append,
3855            copy=copy,
3856            **opts,
3857        )

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 where( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3859    def where(
3860        self,
3861        *expressions: t.Optional[ExpOrStr],
3862        append: bool = True,
3863        dialect: DialectType = None,
3864        copy: bool = True,
3865        **opts,
3866    ) -> Select:
3867        """
3868        Append to or set the WHERE expressions.
3869
3870        Example:
3871            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
3872            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
3873
3874        Args:
3875            *expressions: the SQL code strings to parse.
3876                If an `Expression` instance is passed, it will be used as-is.
3877                Multiple expressions are combined with an AND operator.
3878            append: if `True`, AND the new expressions to any existing expression.
3879                Otherwise, this resets the expression.
3880            dialect: the dialect used to parse the input expressions.
3881            copy: if `False`, modify this expression instance in-place.
3882            opts: other options to use to parse the input expressions.
3883
3884        Returns:
3885            Select: the modified expression.
3886        """
3887        return _apply_conjunction_builder(
3888            *expressions,
3889            instance=self,
3890            arg="where",
3891            append=append,
3892            into=Where,
3893            dialect=dialect,
3894            copy=copy,
3895            **opts,
3896        )

Append to or set the WHERE expressions.

Example:
>>> 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:

Select: the modified expression.

def having( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3898    def having(
3899        self,
3900        *expressions: t.Optional[ExpOrStr],
3901        append: bool = True,
3902        dialect: DialectType = None,
3903        copy: bool = True,
3904        **opts,
3905    ) -> Select:
3906        """
3907        Append to or set the HAVING expressions.
3908
3909        Example:
3910            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
3911            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
3912
3913        Args:
3914            *expressions: the SQL code strings to parse.
3915                If an `Expression` instance is passed, it will be used as-is.
3916                Multiple expressions are combined with an AND operator.
3917            append: if `True`, AND the new expressions to any existing expression.
3918                Otherwise, this resets the expression.
3919            dialect: the dialect used to parse the input expressions.
3920            copy: if `False`, modify this expression instance in-place.
3921            opts: other options to use to parse the input expressions.
3922
3923        Returns:
3924            The modified Select expression.
3925        """
3926        return _apply_conjunction_builder(
3927            *expressions,
3928            instance=self,
3929            arg="having",
3930            append=append,
3931            into=Having,
3932            dialect=dialect,
3933            copy=copy,
3934            **opts,
3935        )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3937    def window(
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="windows",
3949            append=append,
3950            into=Window,
3951            dialect=dialect,
3952            copy=copy,
3953            **opts,
3954        )
def qualify( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
3956    def qualify(
3957        self,
3958        *expressions: t.Optional[ExpOrStr],
3959        append: bool = True,
3960        dialect: DialectType = None,
3961        copy: bool = True,
3962        **opts,
3963    ) -> Select:
3964        return _apply_conjunction_builder(
3965            *expressions,
3966            instance=self,
3967            arg="qualify",
3968            append=append,
3969            into=Qualify,
3970            dialect=dialect,
3971            copy=copy,
3972            **opts,
3973        )
def distinct( self, *ons: Union[str, Expression, NoneType], distinct: bool = True, copy: bool = True) -> Select:
3975    def distinct(
3976        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
3977    ) -> Select:
3978        """
3979        Set the OFFSET expression.
3980
3981        Example:
3982            >>> Select().from_("tbl").select("x").distinct().sql()
3983            'SELECT DISTINCT x FROM tbl'
3984
3985        Args:
3986            ons: the expressions to distinct on
3987            distinct: whether the Select should be distinct
3988            copy: if `False`, modify this expression instance in-place.
3989
3990        Returns:
3991            Select: the modified expression.
3992        """
3993        instance = maybe_copy(self, copy)
3994        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
3995        instance.set("distinct", Distinct(on=on) if distinct else None)
3996        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Create:
3998    def ctas(
3999        self,
4000        table: ExpOrStr,
4001        properties: t.Optional[t.Dict] = None,
4002        dialect: DialectType = None,
4003        copy: bool = True,
4004        **opts,
4005    ) -> Create:
4006        """
4007        Convert this expression to a CREATE TABLE AS statement.
4008
4009        Example:
4010            >>> Select().select("*").from_("tbl").ctas("x").sql()
4011            'CREATE TABLE x AS SELECT * FROM tbl'
4012
4013        Args:
4014            table: the SQL code string to parse as the table name.
4015                If another `Expression` instance is passed, it will be used as-is.
4016            properties: an optional mapping of table properties
4017            dialect: the dialect used to parse the input table.
4018            copy: if `False`, modify this expression instance in-place.
4019            opts: other options to use to parse the input table.
4020
4021        Returns:
4022            The new Create expression.
4023        """
4024        instance = maybe_copy(self, copy)
4025        table_expression = maybe_parse(table, into=Table, dialect=dialect, **opts)
4026
4027        properties_expression = None
4028        if properties:
4029            properties_expression = Properties.from_dict(properties)
4030
4031        return Create(
4032            this=table_expression,
4033            kind="TABLE",
4034            expression=instance,
4035            properties=properties_expression,
4036        )

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:
4038    def lock(self, update: bool = True, copy: bool = True) -> Select:
4039        """
4040        Set the locking read mode for this expression.
4041
4042        Examples:
4043            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
4044            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
4045
4046            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
4047            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
4048
4049        Args:
4050            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
4051            copy: if `False`, modify this expression instance in-place.
4052
4053        Returns:
4054            The modified expression.
4055        """
4056        inst = maybe_copy(self, copy)
4057        inst.set("locks", [Lock(update=update)])
4058
4059        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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True) -> Select:
4061    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
4062        """
4063        Set hints for this expression.
4064
4065        Examples:
4066            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
4067            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
4068
4069        Args:
4070            hints: The SQL code strings to parse as the hints.
4071                If an `Expression` instance is passed, it will be used as-is.
4072            dialect: The dialect used to parse the hints.
4073            copy: If `False`, modify this expression instance in-place.
4074
4075        Returns:
4076            The modified expression.
4077        """
4078        inst = maybe_copy(self, copy)
4079        inst.set(
4080            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
4081        )
4082
4083        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]
4085    @property
4086    def named_selects(self) -> t.List[str]:
4087        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
4089    @property
4090    def is_star(self) -> bool:
4091        return any(expression.is_star for expression in self.expressions)

Checks whether an expression is a star.

selects: List[Expression]
4093    @property
4094    def selects(self) -> t.List[Expression]:
4095        return self.expressions

Returns the query's projections.

key = 'select'
UNWRAPPED_QUERIES = (<class 'Select'>, <class 'SetOperation'>)
class Subquery(DerivedTable, Query):
4101class Subquery(DerivedTable, Query):
4102    arg_types = {
4103        "this": True,
4104        "alias": False,
4105        "with": False,
4106        **QUERY_MODIFIERS,
4107    }
4108
4109    def unnest(self):
4110        """Returns the first non subquery."""
4111        expression = self
4112        while isinstance(expression, Subquery):
4113            expression = expression.this
4114        return expression
4115
4116    def unwrap(self) -> Subquery:
4117        expression = self
4118        while expression.same_parent and expression.is_wrapper:
4119            expression = t.cast(Subquery, expression.parent)
4120        return expression
4121
4122    def select(
4123        self,
4124        *expressions: t.Optional[ExpOrStr],
4125        append: bool = True,
4126        dialect: DialectType = None,
4127        copy: bool = True,
4128        **opts,
4129    ) -> Subquery:
4130        this = maybe_copy(self, copy)
4131        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4132        return this
4133
4134    @property
4135    def is_wrapper(self) -> bool:
4136        """
4137        Whether this Subquery acts as a simple wrapper around another expression.
4138
4139        SELECT * FROM (((SELECT * FROM t)))
4140                      ^
4141                      This corresponds to a "wrapper" Subquery node
4142        """
4143        return all(v is None for k, v in self.args.items() if k != "this")
4144
4145    @property
4146    def is_star(self) -> bool:
4147        return self.this.is_star
4148
4149    @property
4150    def output_name(self) -> str:
4151        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):
4109    def unnest(self):
4110        """Returns the first non subquery."""
4111        expression = self
4112        while isinstance(expression, Subquery):
4113            expression = expression.this
4114        return expression

Returns the first non subquery.

def unwrap(self) -> Subquery:
4116    def unwrap(self) -> Subquery:
4117        expression = self
4118        while expression.same_parent and expression.is_wrapper:
4119            expression = t.cast(Subquery, expression.parent)
4120        return expression
def select( self, *expressions: Union[str, Expression, NoneType], append: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Subquery:
4122    def select(
4123        self,
4124        *expressions: t.Optional[ExpOrStr],
4125        append: bool = True,
4126        dialect: DialectType = None,
4127        copy: bool = True,
4128        **opts,
4129    ) -> Subquery:
4130        this = maybe_copy(self, copy)
4131        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4132        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
4134    @property
4135    def is_wrapper(self) -> bool:
4136        """
4137        Whether this Subquery acts as a simple wrapper around another expression.
4138
4139        SELECT * FROM (((SELECT * FROM t)))
4140                      ^
4141                      This corresponds to a "wrapper" Subquery node
4142        """
4143        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
4145    @property
4146    def is_star(self) -> bool:
4147        return self.this.is_star

Checks whether an expression is a star.

output_name: str
4149    @property
4150    def output_name(self) -> str:
4151        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):
4154class TableSample(Expression):
4155    arg_types = {
4156        "expressions": False,
4157        "method": False,
4158        "bucket_numerator": False,
4159        "bucket_denominator": False,
4160        "bucket_field": False,
4161        "percent": False,
4162        "rows": False,
4163        "size": False,
4164        "seed": False,
4165    }
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):
4168class Tag(Expression):
4169    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
4170
4171    arg_types = {
4172        "this": False,
4173        "prefix": False,
4174        "postfix": False,
4175    }

Tags are used for generating arbitrary sql like SELECT x.

arg_types = {'this': False, 'prefix': False, 'postfix': False}
key = 'tag'
class Pivot(Expression):
4180class Pivot(Expression):
4181    arg_types = {
4182        "this": False,
4183        "alias": False,
4184        "expressions": False,
4185        "field": False,
4186        "unpivot": False,
4187        "using": False,
4188        "group": False,
4189        "columns": False,
4190        "include_nulls": False,
4191        "default_on_null": False,
4192    }
4193
4194    @property
4195    def unpivot(self) -> bool:
4196        return bool(self.args.get("unpivot"))
arg_types = {'this': False, 'alias': False, 'expressions': False, 'field': False, 'unpivot': False, 'using': False, 'group': False, 'columns': False, 'include_nulls': False, 'default_on_null': False}
unpivot: bool
4194    @property
4195    def unpivot(self) -> bool:
4196        return bool(self.args.get("unpivot"))
key = 'pivot'
class Window(Condition):
4199class Window(Condition):
4200    arg_types = {
4201        "this": True,
4202        "partition_by": False,
4203        "order": False,
4204        "spec": False,
4205        "alias": False,
4206        "over": False,
4207        "first": False,
4208    }
arg_types = {'this': True, 'partition_by': False, 'order': False, 'spec': False, 'alias': False, 'over': False, 'first': False}
key = 'window'
class WindowSpec(Expression):
4211class WindowSpec(Expression):
4212    arg_types = {
4213        "kind": False,
4214        "start": False,
4215        "start_side": False,
4216        "end": False,
4217        "end_side": False,
4218    }
arg_types = {'kind': False, 'start': False, 'start_side': False, 'end': False, 'end_side': False}
key = 'windowspec'
class PreWhere(Expression):
4221class PreWhere(Expression):
4222    pass
key = 'prewhere'
class Where(Expression):
4225class Where(Expression):
4226    pass
key = 'where'
class Star(Expression):
4229class Star(Expression):
4230    arg_types = {"except": False, "replace": False, "rename": False}
4231
4232    @property
4233    def name(self) -> str:
4234        return "*"
4235
4236    @property
4237    def output_name(self) -> str:
4238        return self.name
arg_types = {'except': False, 'replace': False, 'rename': False}
name: str
4232    @property
4233    def name(self) -> str:
4234        return "*"
output_name: str
4236    @property
4237    def output_name(self) -> str:
4238        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):
4241class Parameter(Condition):
4242    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'parameter'
class SessionParameter(Condition):
4245class SessionParameter(Condition):
4246    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'sessionparameter'
class Placeholder(Condition):
4249class Placeholder(Condition):
4250    arg_types = {"this": False, "kind": False}
4251
4252    @property
4253    def name(self) -> str:
4254        return self.this or "?"
arg_types = {'this': False, 'kind': False}
name: str
4252    @property
4253    def name(self) -> str:
4254        return self.this or "?"
key = 'placeholder'
class Null(Condition):
4257class Null(Condition):
4258    arg_types: t.Dict[str, t.Any] = {}
4259
4260    @property
4261    def name(self) -> str:
4262        return "NULL"
4263
4264    def to_py(self) -> Lit[None]:
4265        return None
arg_types: Dict[str, Any] = {}
name: str
4260    @property
4261    def name(self) -> str:
4262        return "NULL"
def to_py(self) -> Literal[None]:
4264    def to_py(self) -> Lit[None]:
4265        return None

Returns a Python object equivalent of the SQL node.

key = 'null'
class Boolean(Condition):
4268class Boolean(Condition):
4269    def to_py(self) -> bool:
4270        return self.this
def to_py(self) -> bool:
4269    def to_py(self) -> bool:
4270        return self.this

Returns a Python object equivalent of the SQL node.

key = 'boolean'
class DataTypeParam(Expression):
4273class DataTypeParam(Expression):
4274    arg_types = {"this": True, "expression": False}
4275
4276    @property
4277    def name(self) -> str:
4278        return self.this.name
arg_types = {'this': True, 'expression': False}
name: str
4276    @property
4277    def name(self) -> str:
4278        return self.this.name
key = 'datatypeparam'
class DataType(Expression):
4283class DataType(Expression):
4284    arg_types = {
4285        "this": True,
4286        "expressions": False,
4287        "nested": False,
4288        "values": False,
4289        "prefix": False,
4290        "kind": False,
4291        "nullable": False,
4292    }
4293
4294    class Type(AutoName):
4295        ARRAY = auto()
4296        AGGREGATEFUNCTION = auto()
4297        SIMPLEAGGREGATEFUNCTION = auto()
4298        BIGDECIMAL = auto()
4299        BIGINT = auto()
4300        BIGSERIAL = auto()
4301        BINARY = auto()
4302        BIT = auto()
4303        BOOLEAN = auto()
4304        BPCHAR = auto()
4305        CHAR = auto()
4306        DATE = auto()
4307        DATE32 = auto()
4308        DATEMULTIRANGE = auto()
4309        DATERANGE = auto()
4310        DATETIME = auto()
4311        DATETIME64 = auto()
4312        DECIMAL = auto()
4313        DECIMAL32 = auto()
4314        DECIMAL64 = auto()
4315        DECIMAL128 = auto()
4316        DECIMAL256 = auto()
4317        DOUBLE = auto()
4318        ENUM = auto()
4319        ENUM8 = auto()
4320        ENUM16 = auto()
4321        FIXEDSTRING = auto()
4322        FLOAT = auto()
4323        GEOGRAPHY = auto()
4324        GEOMETRY = auto()
4325        POINT = auto()
4326        RING = auto()
4327        LINESTRING = auto()
4328        MULTILINESTRING = auto()
4329        POLYGON = auto()
4330        MULTIPOLYGON = auto()
4331        HLLSKETCH = auto()
4332        HSTORE = auto()
4333        IMAGE = auto()
4334        INET = auto()
4335        INT = auto()
4336        INT128 = auto()
4337        INT256 = auto()
4338        INT4MULTIRANGE = auto()
4339        INT4RANGE = auto()
4340        INT8MULTIRANGE = auto()
4341        INT8RANGE = auto()
4342        INTERVAL = auto()
4343        IPADDRESS = auto()
4344        IPPREFIX = auto()
4345        IPV4 = auto()
4346        IPV6 = auto()
4347        JSON = auto()
4348        JSONB = auto()
4349        LIST = auto()
4350        LONGBLOB = auto()
4351        LONGTEXT = auto()
4352        LOWCARDINALITY = auto()
4353        MAP = auto()
4354        MEDIUMBLOB = auto()
4355        MEDIUMINT = auto()
4356        MEDIUMTEXT = auto()
4357        MONEY = auto()
4358        NAME = auto()
4359        NCHAR = auto()
4360        NESTED = auto()
4361        NULL = auto()
4362        NUMMULTIRANGE = auto()
4363        NUMRANGE = auto()
4364        NVARCHAR = auto()
4365        OBJECT = auto()
4366        RANGE = auto()
4367        ROWVERSION = auto()
4368        SERIAL = auto()
4369        SET = auto()
4370        SMALLINT = auto()
4371        SMALLMONEY = auto()
4372        SMALLSERIAL = auto()
4373        STRUCT = auto()
4374        SUPER = auto()
4375        TEXT = auto()
4376        TINYBLOB = auto()
4377        TINYTEXT = auto()
4378        TIME = auto()
4379        TIMETZ = auto()
4380        TIMESTAMP = auto()
4381        TIMESTAMPNTZ = auto()
4382        TIMESTAMPLTZ = auto()
4383        TIMESTAMPTZ = auto()
4384        TIMESTAMP_S = auto()
4385        TIMESTAMP_MS = auto()
4386        TIMESTAMP_NS = auto()
4387        TINYINT = auto()
4388        TSMULTIRANGE = auto()
4389        TSRANGE = auto()
4390        TSTZMULTIRANGE = auto()
4391        TSTZRANGE = auto()
4392        UBIGINT = auto()
4393        UINT = auto()
4394        UINT128 = auto()
4395        UINT256 = auto()
4396        UMEDIUMINT = auto()
4397        UDECIMAL = auto()
4398        UNION = auto()
4399        UNIQUEIDENTIFIER = auto()
4400        UNKNOWN = auto()  # Sentinel value, useful for type annotation
4401        USERDEFINED = "USER-DEFINED"
4402        USMALLINT = auto()
4403        UTINYINT = auto()
4404        UUID = auto()
4405        VARBINARY = auto()
4406        VARCHAR = auto()
4407        VARIANT = auto()
4408        VECTOR = auto()
4409        XML = auto()
4410        YEAR = auto()
4411        TDIGEST = auto()
4412
4413    STRUCT_TYPES = {
4414        Type.NESTED,
4415        Type.OBJECT,
4416        Type.STRUCT,
4417        Type.UNION,
4418    }
4419
4420    ARRAY_TYPES = {
4421        Type.ARRAY,
4422        Type.LIST,
4423    }
4424
4425    NESTED_TYPES = {
4426        *STRUCT_TYPES,
4427        *ARRAY_TYPES,
4428        Type.MAP,
4429    }
4430
4431    TEXT_TYPES = {
4432        Type.CHAR,
4433        Type.NCHAR,
4434        Type.NVARCHAR,
4435        Type.TEXT,
4436        Type.VARCHAR,
4437        Type.NAME,
4438    }
4439
4440    SIGNED_INTEGER_TYPES = {
4441        Type.BIGINT,
4442        Type.INT,
4443        Type.INT128,
4444        Type.INT256,
4445        Type.MEDIUMINT,
4446        Type.SMALLINT,
4447        Type.TINYINT,
4448    }
4449
4450    UNSIGNED_INTEGER_TYPES = {
4451        Type.UBIGINT,
4452        Type.UINT,
4453        Type.UINT128,
4454        Type.UINT256,
4455        Type.UMEDIUMINT,
4456        Type.USMALLINT,
4457        Type.UTINYINT,
4458    }
4459
4460    INTEGER_TYPES = {
4461        *SIGNED_INTEGER_TYPES,
4462        *UNSIGNED_INTEGER_TYPES,
4463        Type.BIT,
4464    }
4465
4466    FLOAT_TYPES = {
4467        Type.DOUBLE,
4468        Type.FLOAT,
4469    }
4470
4471    REAL_TYPES = {
4472        *FLOAT_TYPES,
4473        Type.BIGDECIMAL,
4474        Type.DECIMAL,
4475        Type.DECIMAL32,
4476        Type.DECIMAL64,
4477        Type.DECIMAL128,
4478        Type.DECIMAL256,
4479        Type.MONEY,
4480        Type.SMALLMONEY,
4481        Type.UDECIMAL,
4482    }
4483
4484    NUMERIC_TYPES = {
4485        *INTEGER_TYPES,
4486        *REAL_TYPES,
4487    }
4488
4489    TEMPORAL_TYPES = {
4490        Type.DATE,
4491        Type.DATE32,
4492        Type.DATETIME,
4493        Type.DATETIME64,
4494        Type.TIME,
4495        Type.TIMESTAMP,
4496        Type.TIMESTAMPNTZ,
4497        Type.TIMESTAMPLTZ,
4498        Type.TIMESTAMPTZ,
4499        Type.TIMESTAMP_MS,
4500        Type.TIMESTAMP_NS,
4501        Type.TIMESTAMP_S,
4502        Type.TIMETZ,
4503    }
4504
4505    @classmethod
4506    def build(
4507        cls,
4508        dtype: DATA_TYPE,
4509        dialect: DialectType = None,
4510        udt: bool = False,
4511        copy: bool = True,
4512        **kwargs,
4513    ) -> DataType:
4514        """
4515        Constructs a DataType object.
4516
4517        Args:
4518            dtype: the data type of interest.
4519            dialect: the dialect to use for parsing `dtype`, in case it's a string.
4520            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
4521                DataType, thus creating a user-defined type.
4522            copy: whether to copy the data type.
4523            kwargs: additional arguments to pass in the constructor of DataType.
4524
4525        Returns:
4526            The constructed DataType object.
4527        """
4528        from sqlglot import parse_one
4529
4530        if isinstance(dtype, str):
4531            if dtype.upper() == "UNKNOWN":
4532                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
4533
4534            try:
4535                data_type_exp = parse_one(
4536                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
4537                )
4538            except ParseError:
4539                if udt:
4540                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4541                raise
4542        elif isinstance(dtype, DataType.Type):
4543            data_type_exp = DataType(this=dtype)
4544        elif isinstance(dtype, DataType):
4545            return maybe_copy(dtype, copy)
4546        else:
4547            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
4548
4549        return DataType(**{**data_type_exp.args, **kwargs})
4550
4551    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
4552        """
4553        Checks whether this DataType matches one of the provided data types. Nested types or precision
4554        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
4555
4556        Args:
4557            dtypes: the data types to compare this DataType to.
4558            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
4559                If false, it means that NULLABLE<INT> is equivalent to INT.
4560
4561        Returns:
4562            True, if and only if there is a type in `dtypes` which is equal to this DataType.
4563        """
4564        self_is_nullable = self.args.get("nullable")
4565        for dtype in dtypes:
4566            other_type = DataType.build(dtype, copy=False, udt=True)
4567            other_is_nullable = other_type.args.get("nullable")
4568            if (
4569                other_type.expressions
4570                or (check_nullable and (self_is_nullable or other_is_nullable))
4571                or self.this == DataType.Type.USERDEFINED
4572                or other_type.this == DataType.Type.USERDEFINED
4573            ):
4574                matches = self == other_type
4575            else:
4576                matches = self.this == other_type.this
4577
4578            if matches:
4579                return True
4580        return False
arg_types = {'this': True, 'expressions': False, 'nested': False, 'values': False, 'prefix': False, 'kind': False, 'nullable': False}
STRUCT_TYPES = {<Type.NESTED: 'NESTED'>, <Type.OBJECT: 'OBJECT'>, <Type.UNION: 'UNION'>, <Type.STRUCT: 'STRUCT'>}
ARRAY_TYPES = {<Type.LIST: 'LIST'>, <Type.ARRAY: 'ARRAY'>}
NESTED_TYPES = {<Type.NESTED: 'NESTED'>, <Type.MAP: 'MAP'>, <Type.STRUCT: 'STRUCT'>, <Type.LIST: 'LIST'>, <Type.OBJECT: 'OBJECT'>, <Type.ARRAY: 'ARRAY'>, <Type.UNION: 'UNION'>}
TEXT_TYPES = {<Type.TEXT: 'TEXT'>, <Type.VARCHAR: 'VARCHAR'>, <Type.NVARCHAR: 'NVARCHAR'>, <Type.NAME: 'NAME'>, <Type.CHAR: 'CHAR'>, <Type.NCHAR: 'NCHAR'>}
SIGNED_INTEGER_TYPES = {<Type.TINYINT: 'TINYINT'>, <Type.INT128: 'INT128'>, <Type.INT256: 'INT256'>, <Type.MEDIUMINT: 'MEDIUMINT'>, <Type.INT: 'INT'>, <Type.SMALLINT: 'SMALLINT'>, <Type.BIGINT: 'BIGINT'>}
UNSIGNED_INTEGER_TYPES = {<Type.UINT128: 'UINT128'>, <Type.UTINYINT: 'UTINYINT'>, <Type.UINT256: 'UINT256'>, <Type.UBIGINT: 'UBIGINT'>, <Type.USMALLINT: 'USMALLINT'>, <Type.UMEDIUMINT: 'UMEDIUMINT'>, <Type.UINT: 'UINT'>}
INTEGER_TYPES = {<Type.UINT128: 'UINT128'>, <Type.UTINYINT: 'UTINYINT'>, <Type.UINT256: 'UINT256'>, <Type.TINYINT: 'TINYINT'>, <Type.INT128: 'INT128'>, <Type.INT256: 'INT256'>, <Type.MEDIUMINT: 'MEDIUMINT'>, <Type.INT: 'INT'>, <Type.UBIGINT: 'UBIGINT'>, <Type.USMALLINT: 'USMALLINT'>, <Type.UMEDIUMINT: 'UMEDIUMINT'>, <Type.UINT: 'UINT'>, <Type.SMALLINT: 'SMALLINT'>, <Type.BIT: 'BIT'>, <Type.BIGINT: 'BIGINT'>}
FLOAT_TYPES = {<Type.DOUBLE: 'DOUBLE'>, <Type.FLOAT: 'FLOAT'>}
REAL_TYPES = {<Type.FLOAT: 'FLOAT'>, <Type.DECIMAL: 'DECIMAL'>, <Type.MONEY: 'MONEY'>, <Type.DECIMAL256: 'DECIMAL256'>, <Type.UDECIMAL: 'UDECIMAL'>, <Type.DECIMAL32: 'DECIMAL32'>, <Type.SMALLMONEY: 'SMALLMONEY'>, <Type.BIGDECIMAL: 'BIGDECIMAL'>, <Type.DOUBLE: 'DOUBLE'>, <Type.DECIMAL128: 'DECIMAL128'>, <Type.DECIMAL64: 'DECIMAL64'>}
NUMERIC_TYPES = {<Type.UTINYINT: 'UTINYINT'>, <Type.DECIMAL: 'DECIMAL'>, <Type.DECIMAL256: 'DECIMAL256'>, <Type.TINYINT: 'TINYINT'>, <Type.UDECIMAL: 'UDECIMAL'>, <Type.DECIMAL32: 'DECIMAL32'>, <Type.SMALLMONEY: 'SMALLMONEY'>, <Type.DECIMAL128: 'DECIMAL128'>, <Type.UBIGINT: 'UBIGINT'>, <Type.INT: 'INT'>, <Type.UMEDIUMINT: 'UMEDIUMINT'>, <Type.DOUBLE: 'DOUBLE'>, <Type.DECIMAL64: 'DECIMAL64'>, <Type.SMALLINT: 'SMALLINT'>, <Type.BIT: 'BIT'>, <Type.BIGINT: 'BIGINT'>, <Type.UINT128: 'UINT128'>, <Type.FLOAT: 'FLOAT'>, <Type.MONEY: 'MONEY'>, <Type.UINT256: 'UINT256'>, <Type.INT128: 'INT128'>, <Type.INT256: 'INT256'>, <Type.MEDIUMINT: 'MEDIUMINT'>, <Type.USMALLINT: 'USMALLINT'>, <Type.BIGDECIMAL: 'BIGDECIMAL'>, <Type.UINT: 'UINT'>}
TEMPORAL_TYPES = {<Type.TIME: 'TIME'>, <Type.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <Type.DATE: 'DATE'>, <Type.DATETIME: 'DATETIME'>, <Type.DATETIME64: 'DATETIME64'>, <Type.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <Type.DATE32: 'DATE32'>, <Type.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <Type.TIMESTAMPNTZ: 'TIMESTAMPNTZ'>, <Type.TIMESTAMP_S: 'TIMESTAMP_S'>, <Type.TIMETZ: 'TIMETZ'>, <Type.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <Type.TIMESTAMP: 'TIMESTAMP'>}
@classmethod
def build( cls, dtype: Union[str, DataType, DataType.Type], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, udt: bool = False, copy: bool = True, **kwargs) -> DataType:
4505    @classmethod
4506    def build(
4507        cls,
4508        dtype: DATA_TYPE,
4509        dialect: DialectType = None,
4510        udt: bool = False,
4511        copy: bool = True,
4512        **kwargs,
4513    ) -> DataType:
4514        """
4515        Constructs a DataType object.
4516
4517        Args:
4518            dtype: the data type of interest.
4519            dialect: the dialect to use for parsing `dtype`, in case it's a string.
4520            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
4521                DataType, thus creating a user-defined type.
4522            copy: whether to copy the data type.
4523            kwargs: additional arguments to pass in the constructor of DataType.
4524
4525        Returns:
4526            The constructed DataType object.
4527        """
4528        from sqlglot import parse_one
4529
4530        if isinstance(dtype, str):
4531            if dtype.upper() == "UNKNOWN":
4532                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
4533
4534            try:
4535                data_type_exp = parse_one(
4536                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
4537                )
4538            except ParseError:
4539                if udt:
4540                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
4541                raise
4542        elif isinstance(dtype, DataType.Type):
4543            data_type_exp = DataType(this=dtype)
4544        elif isinstance(dtype, DataType):
4545            return maybe_copy(dtype, copy)
4546        else:
4547            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
4548
4549        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, DataType, DataType.Type], check_nullable: bool = False) -> bool:
4551    def is_type(self, *dtypes: DATA_TYPE, check_nullable: bool = False) -> bool:
4552        """
4553        Checks whether this DataType matches one of the provided data types. Nested types or precision
4554        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
4555
4556        Args:
4557            dtypes: the data types to compare this DataType to.
4558            check_nullable: whether to take the NULLABLE type constructor into account for the comparison.
4559                If false, it means that NULLABLE<INT> is equivalent to INT.
4560
4561        Returns:
4562            True, if and only if there is a type in `dtypes` which is equal to this DataType.
4563        """
4564        self_is_nullable = self.args.get("nullable")
4565        for dtype in dtypes:
4566            other_type = DataType.build(dtype, copy=False, udt=True)
4567            other_is_nullable = other_type.args.get("nullable")
4568            if (
4569                other_type.expressions
4570                or (check_nullable and (self_is_nullable or other_is_nullable))
4571                or self.this == DataType.Type.USERDEFINED
4572                or other_type.this == DataType.Type.USERDEFINED
4573            ):
4574                matches = self == other_type
4575            else:
4576                matches = self.this == other_type.this
4577
4578            if matches:
4579                return True
4580        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):
4294    class Type(AutoName):
4295        ARRAY = auto()
4296        AGGREGATEFUNCTION = auto()
4297        SIMPLEAGGREGATEFUNCTION = auto()
4298        BIGDECIMAL = auto()
4299        BIGINT = auto()
4300        BIGSERIAL = auto()
4301        BINARY = auto()
4302        BIT = auto()
4303        BOOLEAN = auto()
4304        BPCHAR = auto()
4305        CHAR = auto()
4306        DATE = auto()
4307        DATE32 = auto()
4308        DATEMULTIRANGE = auto()
4309        DATERANGE = auto()
4310        DATETIME = auto()
4311        DATETIME64 = auto()
4312        DECIMAL = auto()
4313        DECIMAL32 = auto()
4314        DECIMAL64 = auto()
4315        DECIMAL128 = auto()
4316        DECIMAL256 = auto()
4317        DOUBLE = auto()
4318        ENUM = auto()
4319        ENUM8 = auto()
4320        ENUM16 = auto()
4321        FIXEDSTRING = auto()
4322        FLOAT = auto()
4323        GEOGRAPHY = auto()
4324        GEOMETRY = auto()
4325        POINT = auto()
4326        RING = auto()
4327        LINESTRING = auto()
4328        MULTILINESTRING = auto()
4329        POLYGON = auto()
4330        MULTIPOLYGON = auto()
4331        HLLSKETCH = auto()
4332        HSTORE = auto()
4333        IMAGE = auto()
4334        INET = auto()
4335        INT = auto()
4336        INT128 = auto()
4337        INT256 = auto()
4338        INT4MULTIRANGE = auto()
4339        INT4RANGE = auto()
4340        INT8MULTIRANGE = auto()
4341        INT8RANGE = auto()
4342        INTERVAL = auto()
4343        IPADDRESS = auto()
4344        IPPREFIX = auto()
4345        IPV4 = auto()
4346        IPV6 = auto()
4347        JSON = auto()
4348        JSONB = auto()
4349        LIST = auto()
4350        LONGBLOB = auto()
4351        LONGTEXT = auto()
4352        LOWCARDINALITY = auto()
4353        MAP = auto()
4354        MEDIUMBLOB = auto()
4355        MEDIUMINT = auto()
4356        MEDIUMTEXT = auto()
4357        MONEY = auto()
4358        NAME = auto()
4359        NCHAR = auto()
4360        NESTED = auto()
4361        NULL = auto()
4362        NUMMULTIRANGE = auto()
4363        NUMRANGE = auto()
4364        NVARCHAR = auto()
4365        OBJECT = auto()
4366        RANGE = auto()
4367        ROWVERSION = auto()
4368        SERIAL = auto()
4369        SET = auto()
4370        SMALLINT = auto()
4371        SMALLMONEY = auto()
4372        SMALLSERIAL = auto()
4373        STRUCT = auto()
4374        SUPER = auto()
4375        TEXT = auto()
4376        TINYBLOB = auto()
4377        TINYTEXT = auto()
4378        TIME = auto()
4379        TIMETZ = auto()
4380        TIMESTAMP = auto()
4381        TIMESTAMPNTZ = auto()
4382        TIMESTAMPLTZ = auto()
4383        TIMESTAMPTZ = auto()
4384        TIMESTAMP_S = auto()
4385        TIMESTAMP_MS = auto()
4386        TIMESTAMP_NS = auto()
4387        TINYINT = auto()
4388        TSMULTIRANGE = auto()
4389        TSRANGE = auto()
4390        TSTZMULTIRANGE = auto()
4391        TSTZRANGE = auto()
4392        UBIGINT = auto()
4393        UINT = auto()
4394        UINT128 = auto()
4395        UINT256 = auto()
4396        UMEDIUMINT = auto()
4397        UDECIMAL = auto()
4398        UNION = auto()
4399        UNIQUEIDENTIFIER = auto()
4400        UNKNOWN = auto()  # Sentinel value, useful for type annotation
4401        USERDEFINED = "USER-DEFINED"
4402        USMALLINT = auto()
4403        UTINYINT = auto()
4404        UUID = auto()
4405        VARBINARY = auto()
4406        VARCHAR = auto()
4407        VARIANT = auto()
4408        VECTOR = auto()
4409        XML = auto()
4410        YEAR = auto()
4411        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'>
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'>
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'>
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'>
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'>
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'>
UNION = <Type.UNION: 'UNION'>
UNIQUEIDENTIFIER = <Type.UNIQUEIDENTIFIER: 'UNIQUEIDENTIFIER'>
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'>
DATA_TYPE = typing.Union[str, DataType, DataType.Type]
class PseudoType(DataType):
4587class PseudoType(DataType):
4588    arg_types = {"this": True}
arg_types = {'this': True}
key = 'pseudotype'
class ObjectIdentifier(DataType):
4592class ObjectIdentifier(DataType):
4593    arg_types = {"this": True}
arg_types = {'this': True}
key = 'objectidentifier'
class SubqueryPredicate(Predicate):
4597class SubqueryPredicate(Predicate):
4598    pass
key = 'subquerypredicate'
class All(SubqueryPredicate):
4601class All(SubqueryPredicate):
4602    pass
key = 'all'
class Any(SubqueryPredicate):
4605class Any(SubqueryPredicate):
4606    pass
key = 'any'
class Command(Expression):
4611class Command(Expression):
4612    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'command'
class Transaction(Expression):
4615class Transaction(Expression):
4616    arg_types = {"this": False, "modes": False, "mark": False}
arg_types = {'this': False, 'modes': False, 'mark': False}
key = 'transaction'
class Commit(Expression):
4619class Commit(Expression):
4620    arg_types = {"chain": False, "this": False, "durability": False}
arg_types = {'chain': False, 'this': False, 'durability': False}
key = 'commit'
class Rollback(Expression):
4623class Rollback(Expression):
4624    arg_types = {"savepoint": False, "this": False}
arg_types = {'savepoint': False, 'this': False}
key = 'rollback'
class Alter(Expression):
4627class Alter(Expression):
4628    arg_types = {
4629        "this": True,
4630        "kind": True,
4631        "actions": True,
4632        "exists": False,
4633        "only": False,
4634        "options": False,
4635        "cluster": False,
4636        "not_valid": False,
4637    }
4638
4639    @property
4640    def kind(self) -> t.Optional[str]:
4641        kind = self.args.get("kind")
4642        return kind and kind.upper()
4643
4644    @property
4645    def actions(self) -> t.List[Expression]:
4646        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]
4639    @property
4640    def kind(self) -> t.Optional[str]:
4641        kind = self.args.get("kind")
4642        return kind and kind.upper()
actions: List[Expression]
4644    @property
4645    def actions(self) -> t.List[Expression]:
4646        return self.args.get("actions") or []
key = 'alter'
class AddConstraint(Expression):
4649class AddConstraint(Expression):
4650    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'addconstraint'
class DropPartition(Expression):
4653class DropPartition(Expression):
4654    arg_types = {"expressions": True, "exists": False}
arg_types = {'expressions': True, 'exists': False}
key = 'droppartition'
class ReplacePartition(Expression):
4658class ReplacePartition(Expression):
4659    arg_types = {"expression": True, "source": True}
arg_types = {'expression': True, 'source': True}
key = 'replacepartition'
class Binary(Condition):
4663class Binary(Condition):
4664    arg_types = {"this": True, "expression": True}
4665
4666    @property
4667    def left(self) -> Expression:
4668        return self.this
4669
4670    @property
4671    def right(self) -> Expression:
4672        return self.expression
arg_types = {'this': True, 'expression': True}
left: Expression
4666    @property
4667    def left(self) -> Expression:
4668        return self.this
right: Expression
4670    @property
4671    def right(self) -> Expression:
4672        return self.expression
key = 'binary'
class Add(Binary):
4675class Add(Binary):
4676    pass
key = 'add'
class Connector(Binary):
4679class Connector(Binary):
4680    pass
key = 'connector'
class And(Connector):
4683class And(Connector):
4684    pass
key = 'and'
class Or(Connector):
4687class Or(Connector):
4688    pass
key = 'or'
class BitwiseAnd(Binary):
4691class BitwiseAnd(Binary):
4692    pass
key = 'bitwiseand'
class BitwiseLeftShift(Binary):
4695class BitwiseLeftShift(Binary):
4696    pass
key = 'bitwiseleftshift'
class BitwiseOr(Binary):
4699class BitwiseOr(Binary):
4700    pass
key = 'bitwiseor'
class BitwiseRightShift(Binary):
4703class BitwiseRightShift(Binary):
4704    pass
key = 'bitwiserightshift'
class BitwiseXor(Binary):
4707class BitwiseXor(Binary):
4708    pass
key = 'bitwisexor'
class Div(Binary):
4711class Div(Binary):
4712    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):
4715class Overlaps(Binary):
4716    pass
key = 'overlaps'
class Dot(Binary):
4719class Dot(Binary):
4720    @property
4721    def is_star(self) -> bool:
4722        return self.expression.is_star
4723
4724    @property
4725    def name(self) -> str:
4726        return self.expression.name
4727
4728    @property
4729    def output_name(self) -> str:
4730        return self.name
4731
4732    @classmethod
4733    def build(self, expressions: t.Sequence[Expression]) -> Dot:
4734        """Build a Dot object with a sequence of expressions."""
4735        if len(expressions) < 2:
4736            raise ValueError("Dot requires >= 2 expressions.")
4737
4738        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))
4739
4740    @property
4741    def parts(self) -> t.List[Expression]:
4742        """Return the parts of a table / column in order catalog, db, table."""
4743        this, *parts = self.flatten()
4744
4745        parts.reverse()
4746
4747        for arg in COLUMN_PARTS:
4748            part = this.args.get(arg)
4749
4750            if isinstance(part, Expression):
4751                parts.append(part)
4752
4753        parts.reverse()
4754        return parts
is_star: bool
4720    @property
4721    def is_star(self) -> bool:
4722        return self.expression.is_star

Checks whether an expression is a star.

name: str
4724    @property
4725    def name(self) -> str:
4726        return self.expression.name
output_name: str
4728    @property
4729    def output_name(self) -> str:
4730        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:
4732    @classmethod
4733    def build(self, expressions: t.Sequence[Expression]) -> Dot:
4734        """Build a Dot object with a sequence of expressions."""
4735        if len(expressions) < 2:
4736            raise ValueError("Dot requires >= 2 expressions.")
4737
4738        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]
4740    @property
4741    def parts(self) -> t.List[Expression]:
4742        """Return the parts of a table / column in order catalog, db, table."""
4743        this, *parts = self.flatten()
4744
4745        parts.reverse()
4746
4747        for arg in COLUMN_PARTS:
4748            part = this.args.get(arg)
4749
4750            if isinstance(part, Expression):
4751                parts.append(part)
4752
4753        parts.reverse()
4754        return parts

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

key = 'dot'
class DPipe(Binary):
4757class DPipe(Binary):
4758    arg_types = {"this": True, "expression": True, "safe": False}
arg_types = {'this': True, 'expression': True, 'safe': False}
key = 'dpipe'
class EQ(Binary, Predicate):
4761class EQ(Binary, Predicate):
4762    pass
key = 'eq'
class NullSafeEQ(Binary, Predicate):
4765class NullSafeEQ(Binary, Predicate):
4766    pass
key = 'nullsafeeq'
class NullSafeNEQ(Binary, Predicate):
4769class NullSafeNEQ(Binary, Predicate):
4770    pass
key = 'nullsafeneq'
class PropertyEQ(Binary):
4774class PropertyEQ(Binary):
4775    pass
key = 'propertyeq'
class Distance(Binary):
4778class Distance(Binary):
4779    pass
key = 'distance'
class Escape(Binary):
4782class Escape(Binary):
4783    pass
key = 'escape'
class Glob(Binary, Predicate):
4786class Glob(Binary, Predicate):
4787    pass
key = 'glob'
class GT(Binary, Predicate):
4790class GT(Binary, Predicate):
4791    pass
key = 'gt'
class GTE(Binary, Predicate):
4794class GTE(Binary, Predicate):
4795    pass
key = 'gte'
class ILike(Binary, Predicate):
4798class ILike(Binary, Predicate):
4799    pass
key = 'ilike'
class ILikeAny(Binary, Predicate):
4802class ILikeAny(Binary, Predicate):
4803    pass
key = 'ilikeany'
class IntDiv(Binary):
4806class IntDiv(Binary):
4807    pass
key = 'intdiv'
class Is(Binary, Predicate):
4810class Is(Binary, Predicate):
4811    pass
key = 'is'
class Kwarg(Binary):
4814class Kwarg(Binary):
4815    """Kwarg in special functions like func(kwarg => y)."""

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

key = 'kwarg'
class Like(Binary, Predicate):
4818class Like(Binary, Predicate):
4819    pass
key = 'like'
class LikeAny(Binary, Predicate):
4822class LikeAny(Binary, Predicate):
4823    pass
key = 'likeany'
class LT(Binary, Predicate):
4826class LT(Binary, Predicate):
4827    pass
key = 'lt'
class LTE(Binary, Predicate):
4830class LTE(Binary, Predicate):
4831    pass
key = 'lte'
class Mod(Binary):
4834class Mod(Binary):
4835    pass
key = 'mod'
class Mul(Binary):
4838class Mul(Binary):
4839    pass
key = 'mul'
class NEQ(Binary, Predicate):
4842class NEQ(Binary, Predicate):
4843    pass
key = 'neq'
class Operator(Binary):
4847class Operator(Binary):
4848    arg_types = {"this": True, "operator": True, "expression": True}
arg_types = {'this': True, 'operator': True, 'expression': True}
key = 'operator'
class SimilarTo(Binary, Predicate):
4851class SimilarTo(Binary, Predicate):
4852    pass
key = 'similarto'
class Slice(Binary):
4855class Slice(Binary):
4856    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'slice'
class Sub(Binary):
4859class Sub(Binary):
4860    pass
key = 'sub'
class Unary(Condition):
4865class Unary(Condition):
4866    pass
key = 'unary'
class BitwiseNot(Unary):
4869class BitwiseNot(Unary):
4870    pass
key = 'bitwisenot'
class Not(Unary):
4873class Not(Unary):
4874    pass
key = 'not'
class Paren(Unary):
4877class Paren(Unary):
4878    @property
4879    def output_name(self) -> str:
4880        return self.this.name
output_name: str
4878    @property
4879    def output_name(self) -> str:
4880        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):
4883class Neg(Unary):
4884    def to_py(self) -> int | Decimal:
4885        if self.is_number:
4886            return self.this.to_py() * -1
4887        return super().to_py()
def to_py(self) -> int | decimal.Decimal:
4884    def to_py(self) -> int | Decimal:
4885        if self.is_number:
4886            return self.this.to_py() * -1
4887        return super().to_py()

Returns a Python object equivalent of the SQL node.

key = 'neg'
class Alias(Expression):
4890class Alias(Expression):
4891    arg_types = {"this": True, "alias": False}
4892
4893    @property
4894    def output_name(self) -> str:
4895        return self.alias
arg_types = {'this': True, 'alias': False}
output_name: str
4893    @property
4894    def output_name(self) -> str:
4895        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):
4900class PivotAlias(Alias):
4901    pass
key = 'pivotalias'
class PivotAny(Expression):
4906class PivotAny(Expression):
4907    arg_types = {"this": False}
arg_types = {'this': False}
key = 'pivotany'
class Aliases(Expression):
4910class Aliases(Expression):
4911    arg_types = {"this": True, "expressions": True}
4912
4913    @property
4914    def aliases(self):
4915        return self.expressions
arg_types = {'this': True, 'expressions': True}
aliases
4913    @property
4914    def aliases(self):
4915        return self.expressions
key = 'aliases'
class AtIndex(Expression):
4919class AtIndex(Expression):
4920    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'atindex'
class AtTimeZone(Expression):
4923class AtTimeZone(Expression):
4924    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'attimezone'
class FromTimeZone(Expression):
4927class FromTimeZone(Expression):
4928    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'fromtimezone'
class Between(Predicate):
4931class Between(Predicate):
4932    arg_types = {"this": True, "low": True, "high": True}
arg_types = {'this': True, 'low': True, 'high': True}
key = 'between'
class Bracket(Condition):
4935class Bracket(Condition):
4936    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
4937    arg_types = {
4938        "this": True,
4939        "expressions": True,
4940        "offset": False,
4941        "safe": False,
4942        "returns_list_for_maps": False,
4943    }
4944
4945    @property
4946    def output_name(self) -> str:
4947        if len(self.expressions) == 1:
4948            return self.expressions[0].output_name
4949
4950        return super().output_name
arg_types = {'this': True, 'expressions': True, 'offset': False, 'safe': False, 'returns_list_for_maps': False}
output_name: str
4945    @property
4946    def output_name(self) -> str:
4947        if len(self.expressions) == 1:
4948            return self.expressions[0].output_name
4949
4950        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):
4953class Distinct(Expression):
4954    arg_types = {"expressions": False, "on": False}
arg_types = {'expressions': False, 'on': False}
key = 'distinct'
class In(Predicate):
4957class In(Predicate):
4958    arg_types = {
4959        "this": True,
4960        "expressions": False,
4961        "query": False,
4962        "unnest": False,
4963        "field": False,
4964        "is_global": False,
4965    }
arg_types = {'this': True, 'expressions': False, 'query': False, 'unnest': False, 'field': False, 'is_global': False}
key = 'in'
class ForIn(Expression):
4969class ForIn(Expression):
4970    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'forin'
class TimeUnit(Expression):
4973class TimeUnit(Expression):
4974    """Automatically converts unit arg into a var."""
4975
4976    arg_types = {"unit": False}
4977
4978    UNABBREVIATED_UNIT_NAME = {
4979        "D": "DAY",
4980        "H": "HOUR",
4981        "M": "MINUTE",
4982        "MS": "MILLISECOND",
4983        "NS": "NANOSECOND",
4984        "Q": "QUARTER",
4985        "S": "SECOND",
4986        "US": "MICROSECOND",
4987        "W": "WEEK",
4988        "Y": "YEAR",
4989    }
4990
4991    VAR_LIKE = (Column, Literal, Var)
4992
4993    def __init__(self, **args):
4994        unit = args.get("unit")
4995        if isinstance(unit, self.VAR_LIKE):
4996            args["unit"] = Var(
4997                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
4998            )
4999        elif isinstance(unit, Week):
5000            unit.set("this", Var(this=unit.this.name.upper()))
5001
5002        super().__init__(**args)
5003
5004    @property
5005    def unit(self) -> t.Optional[Var | IntervalSpan]:
5006        return self.args.get("unit")

Automatically converts unit arg into a var.

TimeUnit(**args)
4993    def __init__(self, **args):
4994        unit = args.get("unit")
4995        if isinstance(unit, self.VAR_LIKE):
4996            args["unit"] = Var(
4997                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
4998            )
4999        elif isinstance(unit, Week):
5000            unit.set("this", Var(this=unit.this.name.upper()))
5001
5002        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]
5004    @property
5005    def unit(self) -> t.Optional[Var | IntervalSpan]:
5006        return self.args.get("unit")
key = 'timeunit'
class IntervalOp(TimeUnit):
5009class IntervalOp(TimeUnit):
5010    arg_types = {"unit": False, "expression": True}
5011
5012    def interval(self):
5013        return Interval(
5014            this=self.expression.copy(),
5015            unit=self.unit.copy() if self.unit else None,
5016        )
arg_types = {'unit': False, 'expression': True}
def interval(self):
5012    def interval(self):
5013        return Interval(
5014            this=self.expression.copy(),
5015            unit=self.unit.copy() if self.unit else None,
5016        )
key = 'intervalop'
class IntervalSpan(DataType):
5022class IntervalSpan(DataType):
5023    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'intervalspan'
class Interval(TimeUnit):
5026class Interval(TimeUnit):
5027    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key = 'interval'
class IgnoreNulls(Expression):
5030class IgnoreNulls(Expression):
5031    pass
key = 'ignorenulls'
class RespectNulls(Expression):
5034class RespectNulls(Expression):
5035    pass
key = 'respectnulls'
class HavingMax(Expression):
5039class HavingMax(Expression):
5040    arg_types = {"this": True, "expression": True, "max": True}
arg_types = {'this': True, 'expression': True, 'max': True}
key = 'havingmax'
class Func(Condition):
5044class Func(Condition):
5045    """
5046    The base class for all function expressions.
5047
5048    Attributes:
5049        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
5050            treated as a variable length argument and the argument's value will be stored as a list.
5051        _sql_names (list): the SQL name (1st item in the list) and aliases (subsequent items) for this
5052            function expression. These values are used to map this node to a name during parsing as
5053            well as to provide the function's name during SQL string generation. By default the SQL
5054            name is set to the expression's class name transformed to snake case.
5055    """
5056
5057    is_var_len_args = False
5058
5059    @classmethod
5060    def from_arg_list(cls, args):
5061        if cls.is_var_len_args:
5062            all_arg_keys = list(cls.arg_types)
5063            # If this function supports variable length argument treat the last argument as such.
5064            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5065            num_non_var = len(non_var_len_arg_keys)
5066
5067            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5068            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5069        else:
5070            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5071
5072        return cls(**args_dict)
5073
5074    @classmethod
5075    def sql_names(cls):
5076        if cls is Func:
5077            raise NotImplementedError(
5078                "SQL name is only supported by concrete function implementations"
5079            )
5080        if "_sql_names" not in cls.__dict__:
5081            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5082        return cls._sql_names
5083
5084    @classmethod
5085    def sql_name(cls):
5086        return cls.sql_names()[0]
5087
5088    @classmethod
5089    def default_parser_mappings(cls):
5090        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):
5059    @classmethod
5060    def from_arg_list(cls, args):
5061        if cls.is_var_len_args:
5062            all_arg_keys = list(cls.arg_types)
5063            # If this function supports variable length argument treat the last argument as such.
5064            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5065            num_non_var = len(non_var_len_arg_keys)
5066
5067            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5068            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5069        else:
5070            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5071
5072        return cls(**args_dict)
@classmethod
def sql_names(cls):
5074    @classmethod
5075    def sql_names(cls):
5076        if cls is Func:
5077            raise NotImplementedError(
5078                "SQL name is only supported by concrete function implementations"
5079            )
5080        if "_sql_names" not in cls.__dict__:
5081            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5082        return cls._sql_names
@classmethod
def sql_name(cls):
5084    @classmethod
5085    def sql_name(cls):
5086        return cls.sql_names()[0]
@classmethod
def default_parser_mappings(cls):
5088    @classmethod
5089    def default_parser_mappings(cls):
5090        return {name: cls.from_arg_list for name in cls.sql_names()}
key = 'func'
class AggFunc(Func):
5093class AggFunc(Func):
5094    pass
key = 'aggfunc'
class ParameterizedAgg(AggFunc):
5097class ParameterizedAgg(AggFunc):
5098    arg_types = {"this": True, "expressions": True, "params": True}
arg_types = {'this': True, 'expressions': True, 'params': True}
key = 'parameterizedagg'
class Abs(Func):
5101class Abs(Func):
5102    pass
key = 'abs'
class ArgMax(AggFunc):
5105class ArgMax(AggFunc):
5106    arg_types = {"this": True, "expression": True, "count": False}
5107    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmax'
class ArgMin(AggFunc):
5110class ArgMin(AggFunc):
5111    arg_types = {"this": True, "expression": True, "count": False}
5112    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmin'
class ApproxTopK(AggFunc):
5115class ApproxTopK(AggFunc):
5116    arg_types = {"this": True, "expression": False, "counters": False}
arg_types = {'this': True, 'expression': False, 'counters': False}
key = 'approxtopk'
class Flatten(Func):
5119class Flatten(Func):
5120    pass
key = 'flatten'
class Transform(Func):
5124class Transform(Func):
5125    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'transform'
class Anonymous(Func):
5128class Anonymous(Func):
5129    arg_types = {"this": True, "expressions": False}
5130    is_var_len_args = True
5131
5132    @property
5133    def name(self) -> str:
5134        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
5132    @property
5133    def name(self) -> str:
5134        return self.this if isinstance(self.this, str) else self.this.name
key = 'anonymous'
class AnonymousAggFunc(AggFunc):
5137class AnonymousAggFunc(AggFunc):
5138    arg_types = {"this": True, "expressions": False}
5139    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'anonymousaggfunc'
class CombinedAggFunc(AnonymousAggFunc):
5143class CombinedAggFunc(AnonymousAggFunc):
5144    arg_types = {"this": True, "expressions": False, "parts": True}
arg_types = {'this': True, 'expressions': False, 'parts': True}
key = 'combinedaggfunc'
class CombinedParameterizedAgg(ParameterizedAgg):
5147class CombinedParameterizedAgg(ParameterizedAgg):
5148    arg_types = {"this": True, "expressions": True, "params": True, "parts": True}
arg_types = {'this': True, 'expressions': True, 'params': True, 'parts': True}
key = 'combinedparameterizedagg'
class Hll(AggFunc):
5153class Hll(AggFunc):
5154    arg_types = {"this": True, "expressions": False}
5155    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'hll'
class ApproxDistinct(AggFunc):
5158class ApproxDistinct(AggFunc):
5159    arg_types = {"this": True, "accuracy": False}
5160    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
arg_types = {'this': True, 'accuracy': False}
key = 'approxdistinct'
class Apply(Func):
5163class Apply(Func):
5164    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'apply'
class Array(Func):
5167class Array(Func):
5168    arg_types = {"expressions": False, "bracket_notation": False}
5169    is_var_len_args = True
arg_types = {'expressions': False, 'bracket_notation': False}
is_var_len_args = True
key = 'array'
class ToArray(Func):
5173class ToArray(Func):
5174    pass
key = 'toarray'
class List(Func):
5178class List(Func):
5179    arg_types = {"expressions": False}
5180    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'list'
class Pad(Func):
5184class Pad(Func):
5185    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):
5190class ToChar(Func):
5191    arg_types = {"this": True, "format": False, "nlsparam": False}
arg_types = {'this': True, 'format': False, 'nlsparam': False}
key = 'tochar'
class ToNumber(Func):
5196class ToNumber(Func):
5197    arg_types = {
5198        "this": True,
5199        "format": False,
5200        "nlsparam": False,
5201        "precision": False,
5202        "scale": False,
5203    }
arg_types = {'this': True, 'format': False, 'nlsparam': False, 'precision': False, 'scale': False}
key = 'tonumber'
class ToDouble(Func):
5207class ToDouble(Func):
5208    arg_types = {
5209        "this": True,
5210        "format": False,
5211    }
arg_types = {'this': True, 'format': False}
key = 'todouble'
class Columns(Func):
5214class Columns(Func):
5215    arg_types = {"this": True, "unpack": False}
arg_types = {'this': True, 'unpack': False}
key = 'columns'
class Convert(Func):
5219class Convert(Func):
5220    arg_types = {"this": True, "expression": True, "style": False}
arg_types = {'this': True, 'expression': True, 'style': False}
key = 'convert'
class ConvertTimezone(Func):
5223class ConvertTimezone(Func):
5224    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):
5227class GenerateSeries(Func):
5228    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):
5234class ExplodingGenerateSeries(GenerateSeries):
5235    pass
key = 'explodinggenerateseries'
class ArrayAgg(AggFunc):
5238class ArrayAgg(AggFunc):
5239    arg_types = {"this": True, "nulls_excluded": False}
arg_types = {'this': True, 'nulls_excluded': False}
key = 'arrayagg'
class ArrayUniqueAgg(AggFunc):
5242class ArrayUniqueAgg(AggFunc):
5243    pass
key = 'arrayuniqueagg'
class ArrayAll(Func):
5246class ArrayAll(Func):
5247    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayall'
class ArrayAny(Func):
5251class ArrayAny(Func):
5252    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayany'
class ArrayConcat(Func):
5255class ArrayConcat(Func):
5256    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
5257    arg_types = {"this": True, "expressions": False}
5258    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'arrayconcat'
class ArrayConstructCompact(Func):
5261class ArrayConstructCompact(Func):
5262    arg_types = {"expressions": True}
5263    is_var_len_args = True
arg_types = {'expressions': True}
is_var_len_args = True
key = 'arrayconstructcompact'
class ArrayContains(Binary, Func):
5266class ArrayContains(Binary, Func):
5267    _sql_names = ["ARRAY_CONTAINS", "ARRAY_HAS"]
key = 'arraycontains'
class ArrayContainsAll(Binary, Func):
5270class ArrayContainsAll(Binary, Func):
5271    _sql_names = ["ARRAY_CONTAINS_ALL", "ARRAY_HAS_ALL"]
key = 'arraycontainsall'
class ArrayFilter(Func):
5274class ArrayFilter(Func):
5275    arg_types = {"this": True, "expression": True}
5276    _sql_names = ["FILTER", "ARRAY_FILTER"]
arg_types = {'this': True, 'expression': True}
key = 'arrayfilter'
class ArrayToString(Func):
5279class ArrayToString(Func):
5280    arg_types = {"this": True, "expression": True, "null": False}
5281    _sql_names = ["ARRAY_TO_STRING", "ARRAY_JOIN"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'arraytostring'
class String(Func):
5285class String(Func):
5286    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key = 'string'
class StringToArray(Func):
5289class StringToArray(Func):
5290    arg_types = {"this": True, "expression": True, "null": False}
5291    _sql_names = ["STRING_TO_ARRAY", "SPLIT_BY_STRING"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'stringtoarray'
class ArrayOverlaps(Binary, Func):
5294class ArrayOverlaps(Binary, Func):
5295    pass
key = 'arrayoverlaps'
class ArraySize(Func):
5298class ArraySize(Func):
5299    arg_types = {"this": True, "expression": False}
5300    _sql_names = ["ARRAY_SIZE", "ARRAY_LENGTH"]
arg_types = {'this': True, 'expression': False}
key = 'arraysize'
class ArraySort(Func):
5303class ArraySort(Func):
5304    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysort'
class ArraySum(Func):
5307class ArraySum(Func):
5308    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysum'
class ArrayUnionAgg(AggFunc):
5311class ArrayUnionAgg(AggFunc):
5312    pass
key = 'arrayunionagg'
class Avg(AggFunc):
5315class Avg(AggFunc):
5316    pass
key = 'avg'
class AnyValue(AggFunc):
5319class AnyValue(AggFunc):
5320    pass
key = 'anyvalue'
class Lag(AggFunc):
5323class Lag(AggFunc):
5324    arg_types = {"this": True, "offset": False, "default": False}
arg_types = {'this': True, 'offset': False, 'default': False}
key = 'lag'
class Lead(AggFunc):
5327class Lead(AggFunc):
5328    arg_types = {"this": True, "offset": False, "default": False}
arg_types = {'this': True, 'offset': False, 'default': False}
key = 'lead'
class First(AggFunc):
5333class First(AggFunc):
5334    pass
key = 'first'
class Last(AggFunc):
5337class Last(AggFunc):
5338    pass
key = 'last'
class FirstValue(AggFunc):
5341class FirstValue(AggFunc):
5342    pass
key = 'firstvalue'
class LastValue(AggFunc):
5345class LastValue(AggFunc):
5346    pass
key = 'lastvalue'
class NthValue(AggFunc):
5349class NthValue(AggFunc):
5350    arg_types = {"this": True, "offset": True}
arg_types = {'this': True, 'offset': True}
key = 'nthvalue'
class Case(Func):
5353class Case(Func):
5354    arg_types = {"this": False, "ifs": True, "default": False}
5355
5356    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5357        instance = maybe_copy(self, copy)
5358        instance.append(
5359            "ifs",
5360            If(
5361                this=maybe_parse(condition, copy=copy, **opts),
5362                true=maybe_parse(then, copy=copy, **opts),
5363            ),
5364        )
5365        return instance
5366
5367    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5368        instance = maybe_copy(self, copy)
5369        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5370        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:
5356    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5357        instance = maybe_copy(self, copy)
5358        instance.append(
5359            "ifs",
5360            If(
5361                this=maybe_parse(condition, copy=copy, **opts),
5362                true=maybe_parse(then, copy=copy, **opts),
5363            ),
5364        )
5365        return instance
def else_( self, condition: Union[str, Expression], copy: bool = True, **opts) -> Case:
5367    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5368        instance = maybe_copy(self, copy)
5369        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5370        return instance
key = 'case'
class Cast(Func):
5373class Cast(Func):
5374    arg_types = {
5375        "this": True,
5376        "to": True,
5377        "format": False,
5378        "safe": False,
5379        "action": False,
5380    }
5381
5382    @property
5383    def name(self) -> str:
5384        return self.this.name
5385
5386    @property
5387    def to(self) -> DataType:
5388        return self.args["to"]
5389
5390    @property
5391    def output_name(self) -> str:
5392        return self.name
5393
5394    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5395        """
5396        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5397        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5398        array<int> != array<float>.
5399
5400        Args:
5401            dtypes: the data types to compare this Cast's DataType to.
5402
5403        Returns:
5404            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5405        """
5406        return self.to.is_type(*dtypes)
arg_types = {'this': True, 'to': True, 'format': False, 'safe': False, 'action': False}
name: str
5382    @property
5383    def name(self) -> str:
5384        return self.this.name
to: DataType
5386    @property
5387    def to(self) -> DataType:
5388        return self.args["to"]
output_name: str
5390    @property
5391    def output_name(self) -> str:
5392        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, DataType, DataType.Type]) -> bool:
5394    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5395        """
5396        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5397        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5398        array<int> != array<float>.
5399
5400        Args:
5401            dtypes: the data types to compare this Cast's DataType to.
5402
5403        Returns:
5404            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5405        """
5406        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):
5409class TryCast(Cast):
5410    pass
key = 'trycast'
class Try(Func):
5413class Try(Func):
5414    pass
key = 'try'
class CastToStrType(Func):
5417class CastToStrType(Func):
5418    arg_types = {"this": True, "to": True}
arg_types = {'this': True, 'to': True}
key = 'casttostrtype'
class Collate(Binary, Func):
5421class Collate(Binary, Func):
5422    pass
key = 'collate'
class Ceil(Func):
5425class Ceil(Func):
5426    arg_types = {"this": True, "decimals": False}
5427    _sql_names = ["CEIL", "CEILING"]
arg_types = {'this': True, 'decimals': False}
key = 'ceil'
class Coalesce(Func):
5430class Coalesce(Func):
5431    arg_types = {"this": True, "expressions": False, "is_nvl": False}
5432    is_var_len_args = True
5433    _sql_names = ["COALESCE", "IFNULL", "NVL"]
arg_types = {'this': True, 'expressions': False, 'is_nvl': False}
is_var_len_args = True
key = 'coalesce'
class Chr(Func):
5436class Chr(Func):
5437    arg_types = {"expressions": True, "charset": False}
5438    is_var_len_args = True
5439    _sql_names = ["CHR", "CHAR"]
arg_types = {'expressions': True, 'charset': False}
is_var_len_args = True
key = 'chr'
class Concat(Func):
5442class Concat(Func):
5443    arg_types = {"expressions": True, "safe": False, "coalesce": False}
5444    is_var_len_args = True
arg_types = {'expressions': True, 'safe': False, 'coalesce': False}
is_var_len_args = True
key = 'concat'
class ConcatWs(Concat):
5447class ConcatWs(Concat):
5448    _sql_names = ["CONCAT_WS"]
key = 'concatws'
class ConnectByRoot(Func):
5452class ConnectByRoot(Func):
5453    pass
key = 'connectbyroot'
class Count(AggFunc):
5456class Count(AggFunc):
5457    arg_types = {"this": False, "expressions": False, "big_int": False}
5458    is_var_len_args = True
arg_types = {'this': False, 'expressions': False, 'big_int': False}
is_var_len_args = True
key = 'count'
class CountIf(AggFunc):
5461class CountIf(AggFunc):
5462    _sql_names = ["COUNT_IF", "COUNTIF"]
key = 'countif'
class Cbrt(Func):
5466class Cbrt(Func):
5467    pass
key = 'cbrt'
class CurrentDate(Func):
5470class CurrentDate(Func):
5471    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdate'
class CurrentDatetime(Func):
5474class CurrentDatetime(Func):
5475    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdatetime'
class CurrentTime(Func):
5478class CurrentTime(Func):
5479    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttime'
class CurrentTimestamp(Func):
5482class CurrentTimestamp(Func):
5483    arg_types = {"this": False, "sysdate": False}
arg_types = {'this': False, 'sysdate': False}
key = 'currenttimestamp'
class CurrentUser(Func):
5486class CurrentUser(Func):
5487    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentuser'
class DateAdd(Func, IntervalOp):
5490class DateAdd(Func, IntervalOp):
5491    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'dateadd'
class DateSub(Func, IntervalOp):
5494class DateSub(Func, IntervalOp):
5495    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datesub'
class DateDiff(Func, TimeUnit):
5498class DateDiff(Func, TimeUnit):
5499    _sql_names = ["DATEDIFF", "DATE_DIFF"]
5500    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datediff'
class DateTrunc(Func):
5503class DateTrunc(Func):
5504    arg_types = {"unit": True, "this": True, "zone": False}
5505
5506    def __init__(self, **args):
5507        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5508        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5509        unabbreviate = args.pop("unabbreviate", True)
5510
5511        unit = args.get("unit")
5512        if isinstance(unit, TimeUnit.VAR_LIKE):
5513            unit_name = unit.name.upper()
5514            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5515                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5516
5517            args["unit"] = Literal.string(unit_name)
5518        elif isinstance(unit, Week):
5519            unit.set("this", Literal.string(unit.this.name.upper()))
5520
5521        super().__init__(**args)
5522
5523    @property
5524    def unit(self) -> Expression:
5525        return self.args["unit"]
DateTrunc(**args)
5506    def __init__(self, **args):
5507        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5508        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5509        unabbreviate = args.pop("unabbreviate", True)
5510
5511        unit = args.get("unit")
5512        if isinstance(unit, TimeUnit.VAR_LIKE):
5513            unit_name = unit.name.upper()
5514            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5515                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5516
5517            args["unit"] = Literal.string(unit_name)
5518        elif isinstance(unit, Week):
5519            unit.set("this", Literal.string(unit.this.name.upper()))
5520
5521        super().__init__(**args)
arg_types = {'unit': True, 'this': True, 'zone': False}
unit: Expression
5523    @property
5524    def unit(self) -> Expression:
5525        return self.args["unit"]
key = 'datetrunc'
class Datetime(Func):
5530class Datetime(Func):
5531    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'datetime'
class DatetimeAdd(Func, IntervalOp):
5534class DatetimeAdd(Func, IntervalOp):
5535    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimeadd'
class DatetimeSub(Func, IntervalOp):
5538class DatetimeSub(Func, IntervalOp):
5539    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimesub'
class DatetimeDiff(Func, TimeUnit):
5542class DatetimeDiff(Func, TimeUnit):
5543    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimediff'
class DatetimeTrunc(Func, TimeUnit):
5546class DatetimeTrunc(Func, TimeUnit):
5547    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'datetimetrunc'
class DayOfWeek(Func):
5550class DayOfWeek(Func):
5551    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
key = 'dayofweek'
class DayOfWeekIso(Func):
5556class DayOfWeekIso(Func):
5557    _sql_names = ["DAYOFWEEK_ISO", "ISODOW"]
key = 'dayofweekiso'
class DayOfMonth(Func):
5560class DayOfMonth(Func):
5561    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
key = 'dayofmonth'
class DayOfYear(Func):
5564class DayOfYear(Func):
5565    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
key = 'dayofyear'
class ToDays(Func):
5568class ToDays(Func):
5569    pass
key = 'todays'
class WeekOfYear(Func):
5572class WeekOfYear(Func):
5573    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
key = 'weekofyear'
class MonthsBetween(Func):
5576class MonthsBetween(Func):
5577    arg_types = {"this": True, "expression": True, "roundoff": False}
arg_types = {'this': True, 'expression': True, 'roundoff': False}
key = 'monthsbetween'
class LastDay(Func, TimeUnit):
5580class LastDay(Func, TimeUnit):
5581    _sql_names = ["LAST_DAY", "LAST_DAY_OF_MONTH"]
5582    arg_types = {"this": True, "unit": False}
arg_types = {'this': True, 'unit': False}
key = 'lastday'
class Extract(Func):
5585class Extract(Func):
5586    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'extract'
class Exists(Func, SubqueryPredicate):
5589class Exists(Func, SubqueryPredicate):
5590    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'exists'
class Timestamp(Func):
5593class Timestamp(Func):
5594    arg_types = {"this": False, "zone": False, "with_tz": False}
arg_types = {'this': False, 'zone': False, 'with_tz': False}
key = 'timestamp'
class TimestampAdd(Func, TimeUnit):
5597class TimestampAdd(Func, TimeUnit):
5598    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampadd'
class TimestampSub(Func, TimeUnit):
5601class TimestampSub(Func, TimeUnit):
5602    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampsub'
class TimestampDiff(Func, TimeUnit):
5605class TimestampDiff(Func, TimeUnit):
5606    _sql_names = ["TIMESTAMPDIFF", "TIMESTAMP_DIFF"]
5607    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampdiff'
class TimestampTrunc(Func, TimeUnit):
5610class TimestampTrunc(Func, TimeUnit):
5611    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timestamptrunc'
class TimeAdd(Func, TimeUnit):
5614class TimeAdd(Func, TimeUnit):
5615    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timeadd'
class TimeSub(Func, TimeUnit):
5618class TimeSub(Func, TimeUnit):
5619    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timesub'
class TimeDiff(Func, TimeUnit):
5622class TimeDiff(Func, TimeUnit):
5623    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timediff'
class TimeTrunc(Func, TimeUnit):
5626class TimeTrunc(Func, TimeUnit):
5627    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timetrunc'
class DateFromParts(Func):
5630class DateFromParts(Func):
5631    _sql_names = ["DATE_FROM_PARTS", "DATEFROMPARTS"]
5632    arg_types = {"year": True, "month": True, "day": True}
arg_types = {'year': True, 'month': True, 'day': True}
key = 'datefromparts'
class TimeFromParts(Func):
5635class TimeFromParts(Func):
5636    _sql_names = ["TIME_FROM_PARTS", "TIMEFROMPARTS"]
5637    arg_types = {
5638        "hour": True,
5639        "min": True,
5640        "sec": True,
5641        "nano": False,
5642        "fractions": False,
5643        "precision": False,
5644    }
arg_types = {'hour': True, 'min': True, 'sec': True, 'nano': False, 'fractions': False, 'precision': False}
key = 'timefromparts'
class DateStrToDate(Func):
5647class DateStrToDate(Func):
5648    pass
key = 'datestrtodate'
class DateToDateStr(Func):
5651class DateToDateStr(Func):
5652    pass
key = 'datetodatestr'
class DateToDi(Func):
5655class DateToDi(Func):
5656    pass
key = 'datetodi'
class Date(Func):
5660class Date(Func):
5661    arg_types = {"this": False, "zone": False, "expressions": False}
5662    is_var_len_args = True
arg_types = {'this': False, 'zone': False, 'expressions': False}
is_var_len_args = True
key = 'date'
class Day(Func):
5665class Day(Func):
5666    pass
key = 'day'
class Decode(Func):
5669class Decode(Func):
5670    arg_types = {"this": True, "charset": True, "replace": False}
arg_types = {'this': True, 'charset': True, 'replace': False}
key = 'decode'
class DiToDate(Func):
5673class DiToDate(Func):
5674    pass
key = 'ditodate'
class Encode(Func):
5677class Encode(Func):
5678    arg_types = {"this": True, "charset": True}
arg_types = {'this': True, 'charset': True}
key = 'encode'
class Exp(Func):
5681class Exp(Func):
5682    pass
key = 'exp'
class Explode(Func, UDTF):
5686class Explode(Func, UDTF):
5687    arg_types = {"this": True, "expressions": False}
5688    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'explode'
class Inline(Func):
5692class Inline(Func):
5693    pass
key = 'inline'
class ExplodeOuter(Explode):
5696class ExplodeOuter(Explode):
5697    pass
key = 'explodeouter'
class Posexplode(Explode):
5700class Posexplode(Explode):
5701    pass
key = 'posexplode'
class PosexplodeOuter(Posexplode, ExplodeOuter):
5704class PosexplodeOuter(Posexplode, ExplodeOuter):
5705    pass
key = 'posexplodeouter'
class Unnest(Func, UDTF):
5708class Unnest(Func, UDTF):
5709    arg_types = {
5710        "expressions": True,
5711        "alias": False,
5712        "offset": False,
5713        "explode_array": False,
5714    }
5715
5716    @property
5717    def selects(self) -> t.List[Expression]:
5718        columns = super().selects
5719        offset = self.args.get("offset")
5720        if offset:
5721            columns = columns + [to_identifier("offset") if offset is True else offset]
5722        return columns
arg_types = {'expressions': True, 'alias': False, 'offset': False, 'explode_array': False}
selects: List[Expression]
5716    @property
5717    def selects(self) -> t.List[Expression]:
5718        columns = super().selects
5719        offset = self.args.get("offset")
5720        if offset:
5721            columns = columns + [to_identifier("offset") if offset is True else offset]
5722        return columns
key = 'unnest'
class Floor(Func):
5725class Floor(Func):
5726    arg_types = {"this": True, "decimals": False}
arg_types = {'this': True, 'decimals': False}
key = 'floor'
class FromBase64(Func):
5729class FromBase64(Func):
5730    pass
key = 'frombase64'
class ToBase64(Func):
5733class ToBase64(Func):
5734    pass
key = 'tobase64'
class FromISO8601Timestamp(Func):
5738class FromISO8601Timestamp(Func):
5739    _sql_names = ["FROM_ISO8601_TIMESTAMP"]
key = 'fromiso8601timestamp'
class GapFill(Func):
5742class GapFill(Func):
5743    arg_types = {
5744        "this": True,
5745        "ts_column": True,
5746        "bucket_width": True,
5747        "partitioning_columns": False,
5748        "value_columns": False,
5749        "origin": False,
5750        "ignore_nulls": False,
5751    }
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):
5755class GenerateDateArray(Func):
5756    arg_types = {"start": True, "end": True, "step": False}
arg_types = {'start': True, 'end': True, 'step': False}
key = 'generatedatearray'
class GenerateTimestampArray(Func):
5760class GenerateTimestampArray(Func):
5761    arg_types = {"start": True, "end": True, "step": True}
arg_types = {'start': True, 'end': True, 'step': True}
key = 'generatetimestamparray'
class Greatest(Func):
5764class Greatest(Func):
5765    arg_types = {"this": True, "expressions": False}
5766    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'greatest'
class OverflowTruncateBehavior(Expression):
5771class OverflowTruncateBehavior(Expression):
5772    arg_types = {"this": False, "with_count": True}
arg_types = {'this': False, 'with_count': True}
key = 'overflowtruncatebehavior'
class GroupConcat(AggFunc):
5775class GroupConcat(AggFunc):
5776    arg_types = {"this": True, "separator": False, "on_overflow": False}
arg_types = {'this': True, 'separator': False, 'on_overflow': False}
key = 'groupconcat'
class Hex(Func):
5779class Hex(Func):
5780    pass
key = 'hex'
class LowerHex(Hex):
5783class LowerHex(Hex):
5784    pass
key = 'lowerhex'
class Xor(Connector, Func):
5787class Xor(Connector, Func):
5788    arg_types = {"this": False, "expression": False, "expressions": False}
arg_types = {'this': False, 'expression': False, 'expressions': False}
key = 'xor'
class If(Func):
5791class If(Func):
5792    arg_types = {"this": True, "true": True, "false": False}
5793    _sql_names = ["IF", "IIF"]
arg_types = {'this': True, 'true': True, 'false': False}
key = 'if'
class Nullif(Func):
5796class Nullif(Func):
5797    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'nullif'
class Initcap(Func):
5800class Initcap(Func):
5801    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'initcap'
class IsNan(Func):
5804class IsNan(Func):
5805    _sql_names = ["IS_NAN", "ISNAN"]
key = 'isnan'
class IsInf(Func):
5808class IsInf(Func):
5809    _sql_names = ["IS_INF", "ISINF"]
key = 'isinf'
class JSON(Expression):
5813class JSON(Expression):
5814    arg_types = {"this": False, "with": False, "unique": False}
arg_types = {'this': False, 'with': False, 'unique': False}
key = 'json'
class JSONPath(Expression):
5817class JSONPath(Expression):
5818    arg_types = {"expressions": True, "escape": False}
5819
5820    @property
5821    def output_name(self) -> str:
5822        last_segment = self.expressions[-1].this
5823        return last_segment if isinstance(last_segment, str) else ""
arg_types = {'expressions': True, 'escape': False}
output_name: str
5820    @property
5821    def output_name(self) -> str:
5822        last_segment = self.expressions[-1].this
5823        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):
5826class JSONPathPart(Expression):
5827    arg_types = {}
arg_types = {}
key = 'jsonpathpart'
class JSONPathFilter(JSONPathPart):
5830class JSONPathFilter(JSONPathPart):
5831    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathfilter'
class JSONPathKey(JSONPathPart):
5834class JSONPathKey(JSONPathPart):
5835    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathkey'
class JSONPathRecursive(JSONPathPart):
5838class JSONPathRecursive(JSONPathPart):
5839    arg_types = {"this": False}
arg_types = {'this': False}
key = 'jsonpathrecursive'
class JSONPathRoot(JSONPathPart):
5842class JSONPathRoot(JSONPathPart):
5843    pass
key = 'jsonpathroot'
class JSONPathScript(JSONPathPart):
5846class JSONPathScript(JSONPathPart):
5847    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathscript'
class JSONPathSlice(JSONPathPart):
5850class JSONPathSlice(JSONPathPart):
5851    arg_types = {"start": False, "end": False, "step": False}
arg_types = {'start': False, 'end': False, 'step': False}
key = 'jsonpathslice'
class JSONPathSelector(JSONPathPart):
5854class JSONPathSelector(JSONPathPart):
5855    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathselector'
class JSONPathSubscript(JSONPathPart):
5858class JSONPathSubscript(JSONPathPart):
5859    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathsubscript'
class JSONPathUnion(JSONPathPart):
5862class JSONPathUnion(JSONPathPart):
5863    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonpathunion'
class JSONPathWildcard(JSONPathPart):
5866class JSONPathWildcard(JSONPathPart):
5867    pass
key = 'jsonpathwildcard'
class FormatJson(Expression):
5870class FormatJson(Expression):
5871    pass
key = 'formatjson'
class JSONKeyValue(Expression):
5874class JSONKeyValue(Expression):
5875    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'jsonkeyvalue'
class JSONObject(Func):
5878class JSONObject(Func):
5879    arg_types = {
5880        "expressions": False,
5881        "null_handling": False,
5882        "unique_keys": False,
5883        "return_type": False,
5884        "encoding": False,
5885    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobject'
class JSONObjectAgg(AggFunc):
5888class JSONObjectAgg(AggFunc):
5889    arg_types = {
5890        "expressions": False,
5891        "null_handling": False,
5892        "unique_keys": False,
5893        "return_type": False,
5894        "encoding": False,
5895    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobjectagg'
class JSONArray(Func):
5899class JSONArray(Func):
5900    arg_types = {
5901        "expressions": True,
5902        "null_handling": False,
5903        "return_type": False,
5904        "strict": False,
5905    }
arg_types = {'expressions': True, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarray'
class JSONArrayAgg(Func):
5909class JSONArrayAgg(Func):
5910    arg_types = {
5911        "this": True,
5912        "order": False,
5913        "null_handling": False,
5914        "return_type": False,
5915        "strict": False,
5916    }
arg_types = {'this': True, 'order': False, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarrayagg'
class JSONExists(Func):
5919class JSONExists(Func):
5920    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):
5925class JSONColumnDef(Expression):
5926    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):
5929class JSONSchema(Expression):
5930    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonschema'
class JSONValue(Expression):
5934class JSONValue(Expression):
5935    arg_types = {
5936        "this": True,
5937        "path": True,
5938        "returning": False,
5939        "on_condition": False,
5940    }
arg_types = {'this': True, 'path': True, 'returning': False, 'on_condition': False}
key = 'jsonvalue'
class JSONTable(Func):
5944class JSONTable(Func):
5945    arg_types = {
5946        "this": True,
5947        "schema": True,
5948        "path": False,
5949        "error_handling": False,
5950        "empty_handling": False,
5951    }
arg_types = {'this': True, 'schema': True, 'path': False, 'error_handling': False, 'empty_handling': False}
key = 'jsontable'
class ObjectInsert(Func):
5955class ObjectInsert(Func):
5956    arg_types = {
5957        "this": True,
5958        "key": True,
5959        "value": True,
5960        "update_flag": False,
5961    }
arg_types = {'this': True, 'key': True, 'value': True, 'update_flag': False}
key = 'objectinsert'
class OpenJSONColumnDef(Expression):
5964class OpenJSONColumnDef(Expression):
5965    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):
5968class OpenJSON(Func):
5969    arg_types = {"this": True, "path": False, "expressions": False}
arg_types = {'this': True, 'path': False, 'expressions': False}
key = 'openjson'
class JSONBContains(Binary, Func):
5972class JSONBContains(Binary, Func):
5973    _sql_names = ["JSONB_CONTAINS"]
key = 'jsonbcontains'
class JSONBExists(Func):
5976class JSONBExists(Func):
5977    arg_types = {"this": True, "path": True}
5978    _sql_names = ["JSONB_EXISTS"]
arg_types = {'this': True, 'path': True}
key = 'jsonbexists'
class JSONExtract(Binary, Func):
5981class JSONExtract(Binary, Func):
5982    arg_types = {
5983        "this": True,
5984        "expression": True,
5985        "only_json_types": False,
5986        "expressions": False,
5987        "variant_extract": False,
5988        "json_query": False,
5989        "option": False,
5990    }
5991    _sql_names = ["JSON_EXTRACT"]
5992    is_var_len_args = True
5993
5994    @property
5995    def output_name(self) -> str:
5996        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}
is_var_len_args = True
output_name: str
5994    @property
5995    def output_name(self) -> str:
5996        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 JSONExtractScalar(Binary, Func):
5999class JSONExtractScalar(Binary, Func):
6000    arg_types = {"this": True, "expression": True, "only_json_types": False, "expressions": False}
6001    _sql_names = ["JSON_EXTRACT_SCALAR"]
6002    is_var_len_args = True
6003
6004    @property
6005    def output_name(self) -> str:
6006        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
6004    @property
6005    def output_name(self) -> str:
6006        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):
6009class JSONBExtract(Binary, Func):
6010    _sql_names = ["JSONB_EXTRACT"]
key = 'jsonbextract'
class JSONBExtractScalar(Binary, Func):
6013class JSONBExtractScalar(Binary, Func):
6014    _sql_names = ["JSONB_EXTRACT_SCALAR"]
key = 'jsonbextractscalar'
class JSONFormat(Func):
6017class JSONFormat(Func):
6018    arg_types = {"this": False, "options": False}
6019    _sql_names = ["JSON_FORMAT"]
arg_types = {'this': False, 'options': False}
key = 'jsonformat'
class JSONArrayContains(Binary, Predicate, Func):
6023class JSONArrayContains(Binary, Predicate, Func):
6024    _sql_names = ["JSON_ARRAY_CONTAINS"]
key = 'jsonarraycontains'
class ParseJSON(Func):
6027class ParseJSON(Func):
6028    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
6029    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
6030    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
6031    arg_types = {"this": True, "expression": False, "safe": False}
arg_types = {'this': True, 'expression': False, 'safe': False}
key = 'parsejson'
class Least(Func):
6034class Least(Func):
6035    arg_types = {"this": True, "expressions": False}
6036    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'least'
class Left(Func):
6039class Left(Func):
6040    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'left'
class Length(Func):
6047class Length(Func):
6048    arg_types = {"this": True, "binary": False}
6049    _sql_names = ["LENGTH", "LEN"]
arg_types = {'this': True, 'binary': False}
key = 'length'
class Levenshtein(Func):
6052class Levenshtein(Func):
6053    arg_types = {
6054        "this": True,
6055        "expression": False,
6056        "ins_cost": False,
6057        "del_cost": False,
6058        "sub_cost": False,
6059        "max_dist": False,
6060    }
arg_types = {'this': True, 'expression': False, 'ins_cost': False, 'del_cost': False, 'sub_cost': False, 'max_dist': False}
key = 'levenshtein'
class Ln(Func):
6063class Ln(Func):
6064    pass
key = 'ln'
class Log(Func):
6067class Log(Func):
6068    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'log'
class LogicalOr(AggFunc):
6071class LogicalOr(AggFunc):
6072    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
key = 'logicalor'
class LogicalAnd(AggFunc):
6075class LogicalAnd(AggFunc):
6076    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
key = 'logicaland'
class Lower(Func):
6079class Lower(Func):
6080    _sql_names = ["LOWER", "LCASE"]
key = 'lower'
class Map(Func):
6083class Map(Func):
6084    arg_types = {"keys": False, "values": False}
6085
6086    @property
6087    def keys(self) -> t.List[Expression]:
6088        keys = self.args.get("keys")
6089        return keys.expressions if keys else []
6090
6091    @property
6092    def values(self) -> t.List[Expression]:
6093        values = self.args.get("values")
6094        return values.expressions if values else []
arg_types = {'keys': False, 'values': False}
keys: List[Expression]
6086    @property
6087    def keys(self) -> t.List[Expression]:
6088        keys = self.args.get("keys")
6089        return keys.expressions if keys else []
values: List[Expression]
6091    @property
6092    def values(self) -> t.List[Expression]:
6093        values = self.args.get("values")
6094        return values.expressions if values else []
key = 'map'
class ToMap(Func):
6098class ToMap(Func):
6099    pass
key = 'tomap'
class MapFromEntries(Func):
6102class MapFromEntries(Func):
6103    pass
key = 'mapfromentries'
class ScopeResolution(Expression):
6107class ScopeResolution(Expression):
6108    arg_types = {"this": False, "expression": True}
arg_types = {'this': False, 'expression': True}
key = 'scoperesolution'
class Stream(Expression):
6111class Stream(Expression):
6112    pass
key = 'stream'
class StarMap(Func):
6115class StarMap(Func):
6116    pass
key = 'starmap'
class VarMap(Func):
6119class VarMap(Func):
6120    arg_types = {"keys": True, "values": True}
6121    is_var_len_args = True
6122
6123    @property
6124    def keys(self) -> t.List[Expression]:
6125        return self.args["keys"].expressions
6126
6127    @property
6128    def values(self) -> t.List[Expression]:
6129        return self.args["values"].expressions
arg_types = {'keys': True, 'values': True}
is_var_len_args = True
keys: List[Expression]
6123    @property
6124    def keys(self) -> t.List[Expression]:
6125        return self.args["keys"].expressions
values: List[Expression]
6127    @property
6128    def values(self) -> t.List[Expression]:
6129        return self.args["values"].expressions
key = 'varmap'
class MatchAgainst(Func):
6133class MatchAgainst(Func):
6134    arg_types = {"this": True, "expressions": True, "modifier": False}
arg_types = {'this': True, 'expressions': True, 'modifier': False}
key = 'matchagainst'
class Max(AggFunc):
6137class Max(AggFunc):
6138    arg_types = {"this": True, "expressions": False}
6139    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'max'
class MD5(Func):
6142class MD5(Func):
6143    _sql_names = ["MD5"]
key = 'md5'
class MD5Digest(Func):
6147class MD5Digest(Func):
6148    _sql_names = ["MD5_DIGEST"]
key = 'md5digest'
class Median(AggFunc):
6151class Median(AggFunc):
6152    pass
key = 'median'
class Min(AggFunc):
6155class Min(AggFunc):
6156    arg_types = {"this": True, "expressions": False}
6157    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'min'
class Month(Func):
6160class Month(Func):
6161    pass
key = 'month'
class AddMonths(Func):
6164class AddMonths(Func):
6165    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'addmonths'
class Nvl2(Func):
6168class Nvl2(Func):
6169    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'nvl2'
class Normalize(Func):
6172class Normalize(Func):
6173    arg_types = {"this": True, "form": False}
arg_types = {'this': True, 'form': False}
key = 'normalize'
class Overlay(Func):
6176class Overlay(Func):
6177    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):
6181class Predict(Func):
6182    arg_types = {"this": True, "expression": True, "params_struct": False}
arg_types = {'this': True, 'expression': True, 'params_struct': False}
key = 'predict'
class Pow(Binary, Func):
6185class Pow(Binary, Func):
6186    _sql_names = ["POWER", "POW"]
key = 'pow'
class PercentileCont(AggFunc):
6189class PercentileCont(AggFunc):
6190    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentilecont'
class PercentileDisc(AggFunc):
6193class PercentileDisc(AggFunc):
6194    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentiledisc'
class Quantile(AggFunc):
6197class Quantile(AggFunc):
6198    arg_types = {"this": True, "quantile": True}
arg_types = {'this': True, 'quantile': True}
key = 'quantile'
class ApproxQuantile(Quantile):
6201class ApproxQuantile(Quantile):
6202    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):
6205class Quarter(Func):
6206    pass
key = 'quarter'
class Rand(Func):
6211class Rand(Func):
6212    _sql_names = ["RAND", "RANDOM"]
6213    arg_types = {"this": False, "lower": False, "upper": False}
arg_types = {'this': False, 'lower': False, 'upper': False}
key = 'rand'
class Randn(Func):
6216class Randn(Func):
6217    arg_types = {"this": False}
arg_types = {'this': False}
key = 'randn'
class RangeN(Func):
6220class RangeN(Func):
6221    arg_types = {"this": True, "expressions": True, "each": False}
arg_types = {'this': True, 'expressions': True, 'each': False}
key = 'rangen'
class ReadCSV(Func):
6224class ReadCSV(Func):
6225    _sql_names = ["READ_CSV"]
6226    is_var_len_args = True
6227    arg_types = {"this": True, "expressions": False}
is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
key = 'readcsv'
class Reduce(Func):
6230class Reduce(Func):
6231    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):
6234class RegexpExtract(Func):
6235    arg_types = {
6236        "this": True,
6237        "expression": True,  # The pattern
6238        "position": False,  # Only start searching the string from this index
6239        "occurrence": False,  # Skip the first `occurence-1` matches
6240        "parameters": False,  # Flags, eg "i" for case-insensitive
6241        "group": False,  # Which group to return
6242    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextract'
class RegexpReplace(Func):
6245class RegexpReplace(Func):
6246    arg_types = {
6247        "this": True,
6248        "expression": True,
6249        "replacement": False,
6250        "position": False,
6251        "occurrence": False,
6252        "modifiers": False,
6253    }
arg_types = {'this': True, 'expression': True, 'replacement': False, 'position': False, 'occurrence': False, 'modifiers': False}
key = 'regexpreplace'
class RegexpLike(Binary, Func):
6256class RegexpLike(Binary, Func):
6257    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexplike'
class RegexpILike(Binary, Func):
6260class RegexpILike(Binary, Func):
6261    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexpilike'
class RegexpSplit(Func):
6266class RegexpSplit(Func):
6267    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'regexpsplit'
class Repeat(Func):
6270class Repeat(Func):
6271    arg_types = {"this": True, "times": True}
arg_types = {'this': True, 'times': True}
key = 'repeat'
class Round(Func):
6276class Round(Func):
6277    arg_types = {"this": True, "decimals": False, "truncate": False}
arg_types = {'this': True, 'decimals': False, 'truncate': False}
key = 'round'
class RowNumber(Func):
6280class RowNumber(Func):
6281    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'rownumber'
class SafeDivide(Func):
6284class SafeDivide(Func):
6285    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'safedivide'
class SHA(Func):
6288class SHA(Func):
6289    _sql_names = ["SHA", "SHA1"]
key = 'sha'
class SHA2(Func):
6292class SHA2(Func):
6293    _sql_names = ["SHA2"]
6294    arg_types = {"this": True, "length": False}
arg_types = {'this': True, 'length': False}
key = 'sha2'
class Sign(Func):
6297class Sign(Func):
6298    _sql_names = ["SIGN", "SIGNUM"]
key = 'sign'
class SortArray(Func):
6301class SortArray(Func):
6302    arg_types = {"this": True, "asc": False}
arg_types = {'this': True, 'asc': False}
key = 'sortarray'
class Split(Func):
6305class Split(Func):
6306    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'split'
class SplitPart(Func):
6310class SplitPart(Func):
6311    arg_types = {"this": True, "delimiter": True, "part_index": True}
arg_types = {'this': True, 'delimiter': True, 'part_index': True}
key = 'splitpart'
class Substring(Func):
6316class Substring(Func):
6317    _sql_names = ["SUBSTRING", "SUBSTR"]
6318    arg_types = {"this": True, "start": False, "length": False}
arg_types = {'this': True, 'start': False, 'length': False}
key = 'substring'
class StandardHash(Func):
6321class StandardHash(Func):
6322    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'standardhash'
class StartsWith(Func):
6325class StartsWith(Func):
6326    _sql_names = ["STARTS_WITH", "STARTSWITH"]
6327    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'startswith'
class StrPosition(Func):
6330class StrPosition(Func):
6331    arg_types = {
6332        "this": True,
6333        "substr": True,
6334        "position": False,
6335        "instance": False,
6336    }
arg_types = {'this': True, 'substr': True, 'position': False, 'instance': False}
key = 'strposition'
class StrToDate(Func):
6339class StrToDate(Func):
6340    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'strtodate'
class StrToTime(Func):
6343class StrToTime(Func):
6344    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):
6349class StrToUnix(Func):
6350    arg_types = {"this": False, "format": False}
arg_types = {'this': False, 'format': False}
key = 'strtounix'
class StrToMap(Func):
6355class StrToMap(Func):
6356    arg_types = {
6357        "this": True,
6358        "pair_delim": False,
6359        "key_value_delim": False,
6360        "duplicate_resolution_callback": False,
6361    }
arg_types = {'this': True, 'pair_delim': False, 'key_value_delim': False, 'duplicate_resolution_callback': False}
key = 'strtomap'
class NumberToStr(Func):
6364class NumberToStr(Func):
6365    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'numbertostr'
class FromBase(Func):
6368class FromBase(Func):
6369    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'frombase'
class Struct(Func):
6372class Struct(Func):
6373    arg_types = {"expressions": False}
6374    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'struct'
class StructExtract(Func):
6377class StructExtract(Func):
6378    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'structextract'
class Stuff(Func):
6383class Stuff(Func):
6384    _sql_names = ["STUFF", "INSERT"]
6385    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):
6388class Sum(AggFunc):
6389    pass
key = 'sum'
class Sqrt(Func):
6392class Sqrt(Func):
6393    pass
key = 'sqrt'
class Stddev(AggFunc):
6396class Stddev(AggFunc):
6397    _sql_names = ["STDDEV", "STDEV"]
key = 'stddev'
class StddevPop(AggFunc):
6400class StddevPop(AggFunc):
6401    pass
key = 'stddevpop'
class StddevSamp(AggFunc):
6404class StddevSamp(AggFunc):
6405    pass
key = 'stddevsamp'
class Time(Func):
6409class Time(Func):
6410    arg_types = {"this": False, "zone": False}
arg_types = {'this': False, 'zone': False}
key = 'time'
class TimeToStr(Func):
6413class TimeToStr(Func):
6414    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):
6417class TimeToTimeStr(Func):
6418    pass
key = 'timetotimestr'
class TimeToUnix(Func):
6421class TimeToUnix(Func):
6422    pass
key = 'timetounix'
class TimeStrToDate(Func):
6425class TimeStrToDate(Func):
6426    pass
key = 'timestrtodate'
class TimeStrToTime(Func):
6429class TimeStrToTime(Func):
6430    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key = 'timestrtotime'
class TimeStrToUnix(Func):
6433class TimeStrToUnix(Func):
6434    pass
key = 'timestrtounix'
class Trim(Func):
6437class Trim(Func):
6438    arg_types = {
6439        "this": True,
6440        "expression": False,
6441        "position": False,
6442        "collation": False,
6443    }
arg_types = {'this': True, 'expression': False, 'position': False, 'collation': False}
key = 'trim'
class TsOrDsAdd(Func, TimeUnit):
6446class TsOrDsAdd(Func, TimeUnit):
6447    # return_type is used to correctly cast the arguments of this expression when transpiling it
6448    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
6449
6450    @property
6451    def return_type(self) -> DataType:
6452        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
6450    @property
6451    def return_type(self) -> DataType:
6452        return DataType.build(self.args.get("return_type") or DataType.Type.DATE)
key = 'tsordsadd'
class TsOrDsDiff(Func, TimeUnit):
6455class TsOrDsDiff(Func, TimeUnit):
6456    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'tsordsdiff'
class TsOrDsToDateStr(Func):
6459class TsOrDsToDateStr(Func):
6460    pass
key = 'tsordstodatestr'
class TsOrDsToDate(Func):
6463class TsOrDsToDate(Func):
6464    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'tsordstodate'
class TsOrDsToTime(Func):
6467class TsOrDsToTime(Func):
6468    pass
key = 'tsordstotime'
class TsOrDsToTimestamp(Func):
6471class TsOrDsToTimestamp(Func):
6472    pass
key = 'tsordstotimestamp'
class TsOrDiToDi(Func):
6475class TsOrDiToDi(Func):
6476    pass
key = 'tsorditodi'
class Unhex(Func):
6479class Unhex(Func):
6480    pass
key = 'unhex'
class UnixDate(Func):
6484class UnixDate(Func):
6485    pass
key = 'unixdate'
class UnixToStr(Func):
6488class UnixToStr(Func):
6489    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'unixtostr'
class UnixToTime(Func):
6494class UnixToTime(Func):
6495    arg_types = {
6496        "this": True,
6497        "scale": False,
6498        "zone": False,
6499        "hours": False,
6500        "minutes": False,
6501        "format": False,
6502    }
6503
6504    SECONDS = Literal.number(0)
6505    DECIS = Literal.number(1)
6506    CENTIS = Literal.number(2)
6507    MILLIS = Literal.number(3)
6508    DECIMILLIS = Literal.number(4)
6509    CENTIMILLIS = Literal.number(5)
6510    MICROS = Literal.number(6)
6511    DECIMICROS = Literal.number(7)
6512    CENTIMICROS = Literal.number(8)
6513    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):
6516class UnixToTimeStr(Func):
6517    pass
key = 'unixtotimestr'
class Uuid(Func):
6520class Uuid(Func):
6521    _sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "UUID_STRING"]
6522
6523    arg_types = {"this": False, "name": False}
arg_types = {'this': False, 'name': False}
key = 'uuid'
class TimestampFromParts(Func):
6526class TimestampFromParts(Func):
6527    _sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
6528    arg_types = {
6529        "year": True,
6530        "month": True,
6531        "day": True,
6532        "hour": True,
6533        "min": True,
6534        "sec": True,
6535        "nano": False,
6536        "zone": False,
6537        "milli": False,
6538    }
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):
6541class Upper(Func):
6542    _sql_names = ["UPPER", "UCASE"]
key = 'upper'
class Corr(Binary, AggFunc):
6545class Corr(Binary, AggFunc):
6546    pass
key = 'corr'
class Variance(AggFunc):
6549class Variance(AggFunc):
6550    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
key = 'variance'
class VariancePop(AggFunc):
6553class VariancePop(AggFunc):
6554    _sql_names = ["VARIANCE_POP", "VAR_POP"]
key = 'variancepop'
class CovarSamp(Binary, AggFunc):
6557class CovarSamp(Binary, AggFunc):
6558    pass
key = 'covarsamp'
class CovarPop(Binary, AggFunc):
6561class CovarPop(Binary, AggFunc):
6562    pass
key = 'covarpop'
class Week(Func):
6565class Week(Func):
6566    arg_types = {"this": True, "mode": False}
arg_types = {'this': True, 'mode': False}
key = 'week'
class XMLTable(Func):
6569class XMLTable(Func):
6570    arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False}
arg_types = {'this': True, 'passing': False, 'columns': False, 'by_ref': False}
key = 'xmltable'
class Year(Func):
6573class Year(Func):
6574    pass
key = 'year'
class Use(Expression):
6577class Use(Expression):
6578    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'use'
class Merge(DML):
6581class Merge(DML):
6582    arg_types = {
6583        "this": True,
6584        "using": True,
6585        "on": True,
6586        "expressions": True,
6587        "with": False,
6588        "returning": False,
6589    }
arg_types = {'this': True, 'using': True, 'on': True, 'expressions': True, 'with': False, 'returning': False}
key = 'merge'
class When(Func):
6592class When(Func):
6593    arg_types = {"matched": True, "source": False, "condition": False, "then": True}
arg_types = {'matched': True, 'source': False, 'condition': False, 'then': True}
key = 'when'
class NextValueFor(Func):
6598class NextValueFor(Func):
6599    arg_types = {"this": True, "order": False}
arg_types = {'this': True, 'order': False}
key = 'nextvaluefor'
class Semicolon(Expression):
6604class Semicolon(Expression):
6605    arg_types = {}
arg_types = {}
key = 'semicolon'
ALL_FUNCTIONS = [<class 'Abs'>, <class 'AddMonths'>, <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 'ArrayConstructCompact'>, <class 'ArrayContains'>, <class 'ArrayContainsAll'>, <class 'ArrayFilter'>, <class 'ArrayOverlaps'>, <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 'Convert'>, <class 'ConvertTimezone'>, <class 'Corr'>, <class 'Count'>, <class 'CountIf'>, <class 'CovarPop'>, <class 'CovarSamp'>, <class 'CurrentDate'>, <class 'CurrentDatetime'>, <class 'CurrentTime'>, <class 'CurrentTimestamp'>, <class 'CurrentUser'>, <class 'Date'>, <class 'DateAdd'>, <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 'Exists'>, <class 'Exp'>, <class 'Explode'>, <class 'ExplodeOuter'>, <class 'ExplodingGenerateSeries'>, <class 'Extract'>, <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 'IsInf'>, <class 'IsNan'>, <class 'JSONArray'>, <class 'JSONArrayAgg'>, <class 'JSONArrayContains'>, <class 'JSONBContains'>, <class 'JSONBExists'>, <class 'JSONBExtract'>, <class 'JSONBExtractScalar'>, <class 'JSONExists'>, <class 'JSONExtract'>, <class 'JSONExtractScalar'>, <class 'JSONFormat'>, <class 'JSONObject'>, <class 'JSONObjectAgg'>, <class 'JSONTable'>, <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 '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 '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 '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 '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 'TsOrDsToTime'>, <class 'TsOrDsToTimestamp'>, <class 'Unhex'>, <class 'UnixDate'>, <class 'UnixToStr'>, <class 'UnixToTime'>, <class 'UnixToTimeStr'>, <class 'Unnest'>, <class 'Upper'>, <class 'Uuid'>, <class 'VarMap'>, <class 'Variance'>, <class 'VariancePop'>, <class 'Week'>, <class 'WeekOfYear'>, <class 'When'>, <class 'XMLTable'>, <class 'Xor'>, <class 'Year'>]
FUNCTION_BY_NAME = {'ABS': <class 'Abs'>, 'ADD_MONTHS': <class 'AddMonths'>, '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_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_OVERLAPS': <class 'ArrayOverlaps'>, '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'>, 'CONVERT': <class 'Convert'>, 'CONVERT_TIMEZONE': <class 'ConvertTimezone'>, '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_TIME': <class 'CurrentTime'>, 'CURRENT_TIMESTAMP': <class 'CurrentTimestamp'>, 'CURRENT_USER': <class 'CurrentUser'>, 'DATE': <class 'Date'>, 'DATE_ADD': <class 'DateAdd'>, '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'>, 'EXISTS': <class 'Exists'>, 'EXP': <class 'Exp'>, 'EXPLODE': <class 'Explode'>, 'EXPLODE_OUTER': <class 'ExplodeOuter'>, 'EXPLODING_GENERATE_SERIES': <class 'ExplodingGenerateSeries'>, 'EXTRACT': <class 'Extract'>, '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'>, '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_EXISTS': <class 'JSONExists'>, 'JSON_EXTRACT': <class 'JSONExtract'>, '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'>, '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'>, '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'>, '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'>, '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_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'>, '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'>, '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_TIME': <class 'TsOrDsToTime'>, 'TS_OR_DS_TO_TIMESTAMP': <class 'TsOrDsToTimestamp'>, 'UNHEX': <class 'Unhex'>, 'UNIX_DATE': <class 'UnixDate'>, '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'>, 'WHEN': <class 'When'>, '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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, prefix: Optional[str] = None, copy: bool = False, **opts) -> Expression:
6645def maybe_parse(
6646    sql_or_expression: ExpOrStr,
6647    *,
6648    into: t.Optional[IntoType] = None,
6649    dialect: DialectType = None,
6650    prefix: t.Optional[str] = None,
6651    copy: bool = False,
6652    **opts,
6653) -> Expression:
6654    """Gracefully handle a possible string or expression.
6655
6656    Example:
6657        >>> maybe_parse("1")
6658        Literal(this=1, is_string=False)
6659        >>> maybe_parse(to_identifier("x"))
6660        Identifier(this=x, quoted=False)
6661
6662    Args:
6663        sql_or_expression: the SQL code string or an expression
6664        into: the SQLGlot Expression to parse into
6665        dialect: the dialect used to parse the input expressions (in the case that an
6666            input expression is a SQL string).
6667        prefix: a string to prefix the sql with before it gets parsed
6668            (automatically includes a space)
6669        copy: whether to copy the expression.
6670        **opts: other options to use to parse the input expressions (again, in the case
6671            that an input expression is a SQL string).
6672
6673    Returns:
6674        Expression: the parsed or given expression.
6675    """
6676    if isinstance(sql_or_expression, Expression):
6677        if copy:
6678            return sql_or_expression.copy()
6679        return sql_or_expression
6680
6681    if sql_or_expression is None:
6682        raise ParseError("SQL cannot be None")
6683
6684    import sqlglot
6685
6686    sql = str(sql_or_expression)
6687    if prefix:
6688        sql = f"{prefix} {sql}"
6689
6690    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):
6701def maybe_copy(instance, copy=True):
6702    return instance.copy() if copy and instance else instance
def union( *expressions: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Union:
6937def union(
6938    *expressions: ExpOrStr,
6939    distinct: bool = True,
6940    dialect: DialectType = None,
6941    copy: bool = True,
6942    **opts,
6943) -> Union:
6944    """
6945    Initializes a syntax tree for the `UNION` operation.
6946
6947    Example:
6948        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
6949        'SELECT * FROM foo UNION SELECT * FROM bla'
6950
6951    Args:
6952        expressions: the SQL code strings, corresponding to the `UNION`'s operands.
6953            If `Expression` instances are passed, they will be used as-is.
6954        distinct: set the DISTINCT flag if and only if this is true.
6955        dialect: the dialect used to parse the input expression.
6956        copy: whether to copy the expression.
6957        opts: other options to use to parse the input expressions.
6958
6959    Returns:
6960        The new Union instance.
6961    """
6962    assert len(expressions) >= 2, "At least two expressions are required by `union`."
6963    return _apply_set_operation(
6964        *expressions, set_operation=Union, distinct=distinct, dialect=dialect, copy=copy, **opts
6965    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Intersect:
6968def intersect(
6969    *expressions: ExpOrStr,
6970    distinct: bool = True,
6971    dialect: DialectType = None,
6972    copy: bool = True,
6973    **opts,
6974) -> Intersect:
6975    """
6976    Initializes a syntax tree for the `INTERSECT` operation.
6977
6978    Example:
6979        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
6980        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
6981
6982    Args:
6983        expressions: the SQL code strings, corresponding to the `INTERSECT`'s operands.
6984            If `Expression` instances are passed, they will be used as-is.
6985        distinct: set the DISTINCT flag if and only if this is true.
6986        dialect: the dialect used to parse the input expression.
6987        copy: whether to copy the expression.
6988        opts: other options to use to parse the input expressions.
6989
6990    Returns:
6991        The new Intersect instance.
6992    """
6993    assert len(expressions) >= 2, "At least two expressions are required by `intersect`."
6994    return _apply_set_operation(
6995        *expressions, set_operation=Intersect, distinct=distinct, dialect=dialect, copy=copy, **opts
6996    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Except:
6999def except_(
7000    *expressions: ExpOrStr,
7001    distinct: bool = True,
7002    dialect: DialectType = None,
7003    copy: bool = True,
7004    **opts,
7005) -> Except:
7006    """
7007    Initializes a syntax tree for the `EXCEPT` operation.
7008
7009    Example:
7010        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
7011        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
7012
7013    Args:
7014        expressions: the SQL code strings, corresponding to the `EXCEPT`'s operands.
7015            If `Expression` instances are passed, they will be used as-is.
7016        distinct: set the DISTINCT flag if and only if this is true.
7017        dialect: the dialect used to parse the input expression.
7018        copy: whether to copy the expression.
7019        opts: other options to use to parse the input expressions.
7020
7021    Returns:
7022        The new Except instance.
7023    """
7024    assert len(expressions) >= 2, "At least two expressions are required by `except_`."
7025    return _apply_set_operation(
7026        *expressions, set_operation=Except, distinct=distinct, dialect=dialect, copy=copy, **opts
7027    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Select:
7030def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7031    """
7032    Initializes a syntax tree from one or multiple SELECT expressions.
7033
7034    Example:
7035        >>> select("col1", "col2").from_("tbl").sql()
7036        'SELECT col1, col2 FROM tbl'
7037
7038    Args:
7039        *expressions: the SQL code string to parse as the expressions of a
7040            SELECT statement. If an Expression instance is passed, this is used as-is.
7041        dialect: the dialect used to parse the input expressions (in the case that an
7042            input expression is a SQL string).
7043        **opts: other options to use to parse the input expressions (again, in the case
7044            that an input expression is a SQL string).
7045
7046    Returns:
7047        Select: the syntax tree for the SELECT statement.
7048    """
7049    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Select:
7052def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7053    """
7054    Initializes a syntax tree from a FROM expression.
7055
7056    Example:
7057        >>> from_("tbl").select("col1", "col2").sql()
7058        'SELECT col1, col2 FROM tbl'
7059
7060    Args:
7061        *expression: the SQL code string to parse as the FROM expressions of a
7062            SELECT statement. If an Expression instance is passed, this is used as-is.
7063        dialect: the dialect used to parse the input expression (in the case that the
7064            input expression is a SQL string).
7065        **opts: other options to use to parse the input expressions (again, in the case
7066            that the input expression is a SQL string).
7067
7068    Returns:
7069        Select: the syntax tree for the SELECT statement.
7070    """
7071    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Update:
7074def update(
7075    table: str | Table,
7076    properties: t.Optional[dict] = None,
7077    where: t.Optional[ExpOrStr] = None,
7078    from_: t.Optional[ExpOrStr] = None,
7079    with_: t.Optional[t.Dict[str, ExpOrStr]] = None,
7080    dialect: DialectType = None,
7081    **opts,
7082) -> Update:
7083    """
7084    Creates an update statement.
7085
7086    Example:
7087        >>> 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()
7088        "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"
7089
7090    Args:
7091        properties: dictionary of properties to SET which are
7092            auto converted to sql objects eg None -> NULL
7093        where: sql conditional parsed into a WHERE statement
7094        from_: sql statement parsed into a FROM statement
7095        with_: dictionary of CTE aliases / select statements to include in a WITH clause.
7096        dialect: the dialect used to parse the input expressions.
7097        **opts: other options to use to parse the input expressions.
7098
7099    Returns:
7100        Update: the syntax tree for the UPDATE statement.
7101    """
7102    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
7103    if properties:
7104        update_expr.set(
7105            "expressions",
7106            [
7107                EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
7108                for k, v in properties.items()
7109            ],
7110        )
7111    if from_:
7112        update_expr.set(
7113            "from",
7114            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
7115        )
7116    if isinstance(where, Condition):
7117        where = Where(this=where)
7118    if where:
7119        update_expr.set(
7120            "where",
7121            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
7122        )
7123    if with_:
7124        cte_list = [
7125            alias_(CTE(this=maybe_parse(qry, dialect=dialect, **opts)), alias, table=True)
7126            for alias, qry in with_.items()
7127        ]
7128        update_expr.set(
7129            "with",
7130            With(expressions=cte_list),
7131        )
7132    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Delete:
7135def delete(
7136    table: ExpOrStr,
7137    where: t.Optional[ExpOrStr] = None,
7138    returning: t.Optional[ExpOrStr] = None,
7139    dialect: DialectType = None,
7140    **opts,
7141) -> Delete:
7142    """
7143    Builds a delete statement.
7144
7145    Example:
7146        >>> delete("my_table", where="id > 1").sql()
7147        'DELETE FROM my_table WHERE id > 1'
7148
7149    Args:
7150        where: sql conditional parsed into a WHERE statement
7151        returning: sql conditional parsed into a RETURNING statement
7152        dialect: the dialect used to parse the input expressions.
7153        **opts: other options to use to parse the input expressions.
7154
7155    Returns:
7156        Delete: the syntax tree for the DELETE statement.
7157    """
7158    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
7159    if where:
7160        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
7161    if returning:
7162        delete_expr = delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
7163    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Insert:
7166def insert(
7167    expression: ExpOrStr,
7168    into: ExpOrStr,
7169    columns: t.Optional[t.Sequence[str | Identifier]] = None,
7170    overwrite: t.Optional[bool] = None,
7171    returning: t.Optional[ExpOrStr] = None,
7172    dialect: DialectType = None,
7173    copy: bool = True,
7174    **opts,
7175) -> Insert:
7176    """
7177    Builds an INSERT statement.
7178
7179    Example:
7180        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
7181        'INSERT INTO tbl VALUES (1, 2, 3)'
7182
7183    Args:
7184        expression: the sql string or expression of the INSERT statement
7185        into: the tbl to insert data to.
7186        columns: optionally the table's column names.
7187        overwrite: whether to INSERT OVERWRITE or not.
7188        returning: sql conditional parsed into a RETURNING statement
7189        dialect: the dialect used to parse the input expressions.
7190        copy: whether to copy the expression.
7191        **opts: other options to use to parse the input expressions.
7192
7193    Returns:
7194        Insert: the syntax tree for the INSERT statement.
7195    """
7196    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
7197    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
7198
7199    if columns:
7200        this = Schema(this=this, expressions=[to_identifier(c, copy=copy) for c in columns])
7201
7202    insert = Insert(this=this, expression=expr, overwrite=overwrite)
7203
7204    if returning:
7205        insert = insert.returning(returning, dialect=dialect, copy=False, **opts)
7206
7207    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Merge:
7210def merge(
7211    *when_exprs: ExpOrStr,
7212    into: ExpOrStr,
7213    using: ExpOrStr,
7214    on: ExpOrStr,
7215    returning: t.Optional[ExpOrStr] = None,
7216    dialect: DialectType = None,
7217    copy: bool = True,
7218    **opts,
7219) -> Merge:
7220    """
7221    Builds a MERGE statement.
7222
7223    Example:
7224        >>> merge("WHEN MATCHED THEN UPDATE SET col1 = source_table.col1",
7225        ...       "WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)",
7226        ...       into="my_table",
7227        ...       using="source_table",
7228        ...       on="my_table.id = source_table.id").sql()
7229        '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)'
7230
7231    Args:
7232        *when_exprs: The WHEN clauses specifying actions for matched and unmatched rows.
7233        into: The target table to merge data into.
7234        using: The source table to merge data from.
7235        on: The join condition for the merge.
7236        returning: The columns to return from the merge.
7237        dialect: The dialect used to parse the input expressions.
7238        copy: Whether to copy the expression.
7239        **opts: Other options to use to parse the input expressions.
7240
7241    Returns:
7242        Merge: The syntax tree for the MERGE statement.
7243    """
7244    merge = Merge(
7245        this=maybe_parse(into, dialect=dialect, copy=copy, **opts),
7246        using=maybe_parse(using, dialect=dialect, copy=copy, **opts),
7247        on=maybe_parse(on, dialect=dialect, copy=copy, **opts),
7248        expressions=[
7249            maybe_parse(when_expr, dialect=dialect, copy=copy, into=When, **opts)
7250            for when_expr in when_exprs
7251        ],
7252    )
7253    if returning:
7254        merge = merge.returning(returning, dialect=dialect, copy=False, **opts)
7255
7256    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
7259def condition(
7260    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
7261) -> Condition:
7262    """
7263    Initialize a logical condition expression.
7264
7265    Example:
7266        >>> condition("x=1").sql()
7267        'x = 1'
7268
7269        This is helpful for composing larger logical syntax trees:
7270        >>> where = condition("x=1")
7271        >>> where = where.and_("y=1")
7272        >>> Select().from_("tbl").select("*").where(where).sql()
7273        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
7274
7275    Args:
7276        *expression: the SQL code string to parse.
7277            If an Expression instance is passed, this is used as-is.
7278        dialect: the dialect used to parse the input expression (in the case that the
7279            input expression is a SQL string).
7280        copy: Whether to copy `expression` (only applies to expressions).
7281        **opts: other options to use to parse the input expressions (again, in the case
7282            that the input expression is a SQL string).
7283
7284    Returns:
7285        The new Condition instance
7286    """
7287    return maybe_parse(
7288        expression,
7289        into=Condition,
7290        dialect=dialect,
7291        copy=copy,
7292        **opts,
7293    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
7296def and_(
7297    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
7298) -> Condition:
7299    """
7300    Combine multiple conditions with an AND logical operator.
7301
7302    Example:
7303        >>> and_("x=1", and_("y=1", "z=1")).sql()
7304        'x = 1 AND (y = 1 AND z = 1)'
7305
7306    Args:
7307        *expressions: the SQL code strings to parse.
7308            If an Expression instance is passed, this is used as-is.
7309        dialect: the dialect used to parse the input expression.
7310        copy: whether to copy `expressions` (only applies to Expressions).
7311        **opts: other options to use to parse the input expressions.
7312
7313    Returns:
7314        The new condition
7315    """
7316    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, **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).
  • **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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
7319def or_(
7320    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
7321) -> Condition:
7322    """
7323    Combine multiple conditions with an OR logical operator.
7324
7325    Example:
7326        >>> or_("x=1", or_("y=1", "z=1")).sql()
7327        'x = 1 OR (y = 1 OR z = 1)'
7328
7329    Args:
7330        *expressions: the SQL code strings to parse.
7331            If an Expression instance is passed, this is used as-is.
7332        dialect: the dialect used to parse the input expression.
7333        copy: whether to copy `expressions` (only applies to Expressions).
7334        **opts: other options to use to parse the input expressions.
7335
7336    Returns:
7337        The new condition
7338    """
7339    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, **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).
  • **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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
7342def xor(
7343    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
7344) -> Condition:
7345    """
7346    Combine multiple conditions with an XOR logical operator.
7347
7348    Example:
7349        >>> xor("x=1", xor("y=1", "z=1")).sql()
7350        'x = 1 XOR (y = 1 XOR z = 1)'
7351
7352    Args:
7353        *expressions: the SQL code strings to parse.
7354            If an Expression instance is passed, this is used as-is.
7355        dialect: the dialect used to parse the input expression.
7356        copy: whether to copy `expressions` (only applies to Expressions).
7357        **opts: other options to use to parse the input expressions.
7358
7359    Returns:
7360        The new condition
7361    """
7362    return t.cast(Condition, _combine(expressions, Xor, dialect, copy=copy, **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).
  • **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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Not:
7365def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
7366    """
7367    Wrap a condition with a NOT operator.
7368
7369    Example:
7370        >>> not_("this_suit='black'").sql()
7371        "NOT this_suit = 'black'"
7372
7373    Args:
7374        expression: the SQL code string to parse.
7375            If an Expression instance is passed, this is used as-is.
7376        dialect: the dialect used to parse the input expression.
7377        copy: whether to copy the expression or not.
7378        **opts: other options to use to parse the input expressions.
7379
7380    Returns:
7381        The new condition.
7382    """
7383    this = condition(
7384        expression,
7385        dialect=dialect,
7386        copy=copy,
7387        **opts,
7388    )
7389    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:
7392def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
7393    """
7394    Wrap an expression in parentheses.
7395
7396    Example:
7397        >>> paren("5 + 3").sql()
7398        '(5 + 3)'
7399
7400    Args:
7401        expression: the SQL code string to parse.
7402            If an Expression instance is passed, this is used as-is.
7403        copy: whether to copy the expression or not.
7404
7405    Returns:
7406        The wrapped expression.
7407    """
7408    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):
7424def to_identifier(name, quoted=None, copy=True):
7425    """Builds an identifier.
7426
7427    Args:
7428        name: The name to turn into an identifier.
7429        quoted: Whether to force quote the identifier.
7430        copy: Whether to copy name if it's an Identifier.
7431
7432    Returns:
7433        The identifier ast node.
7434    """
7435
7436    if name is None:
7437        return None
7438
7439    if isinstance(name, Identifier):
7440        identifier = maybe_copy(name, copy)
7441    elif isinstance(name, str):
7442        identifier = Identifier(
7443            this=name,
7444            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
7445        )
7446    else:
7447        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
7448    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None) -> Identifier:
7451def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
7452    """
7453    Parses a given string into an identifier.
7454
7455    Args:
7456        name: The name to parse into an identifier.
7457        dialect: The dialect to parse against.
7458
7459    Returns:
7460        The identifier ast node.
7461    """
7462    try:
7463        expression = maybe_parse(name, dialect=dialect, into=Identifier)
7464    except (ParseError, TokenError):
7465        expression = to_identifier(name)
7466
7467    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]+)\\s*([a-zA-Z]+)\\s*')
def to_interval( interval: str | Literal) -> Interval:
7473def to_interval(interval: str | Literal) -> Interval:
7474    """Builds an interval expression from a string like '1 day' or '5 months'."""
7475    if isinstance(interval, Literal):
7476        if not interval.is_string:
7477            raise ValueError("Invalid interval string.")
7478
7479        interval = interval.this
7480
7481    interval = maybe_parse(f"INTERVAL {interval}")
7482    assert isinstance(interval, Interval)
7483    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **kwargs) -> Table:
7486def to_table(
7487    sql_path: str | Table, dialect: DialectType = None, copy: bool = True, **kwargs
7488) -> Table:
7489    """
7490    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
7491    If a table is passed in then that table is returned.
7492
7493    Args:
7494        sql_path: a `[catalog].[schema].[table]` string.
7495        dialect: the source dialect according to which the table name will be parsed.
7496        copy: Whether to copy a table if it is passed in.
7497        kwargs: the kwargs to instantiate the resulting `Table` expression with.
7498
7499    Returns:
7500        A table expression.
7501    """
7502    if isinstance(sql_path, Table):
7503        return maybe_copy(sql_path, copy=copy)
7504
7505    table = maybe_parse(sql_path, into=Table, dialect=dialect)
7506
7507    for k, v in kwargs.items():
7508        table.set(k, v)
7509
7510    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **kwargs) -> Column:
7513def to_column(
7514    sql_path: str | Column,
7515    quoted: t.Optional[bool] = None,
7516    dialect: DialectType = None,
7517    copy: bool = True,
7518    **kwargs,
7519) -> Column:
7520    """
7521    Create a column from a `[table].[column]` sql path. Table is optional.
7522    If a column is passed in then that column is returned.
7523
7524    Args:
7525        sql_path: a `[table].[column]` string.
7526        quoted: Whether or not to force quote identifiers.
7527        dialect: the source dialect according to which the column name will be parsed.
7528        copy: Whether to copy a column if it is passed in.
7529        kwargs: the kwargs to instantiate the resulting `Column` expression with.
7530
7531    Returns:
7532        A column expression.
7533    """
7534    if isinstance(sql_path, Column):
7535        return maybe_copy(sql_path, copy=copy)
7536
7537    try:
7538        col = maybe_parse(sql_path, into=Column, dialect=dialect)
7539    except ParseError:
7540        return column(*reversed(sql_path.split(".")), quoted=quoted, **kwargs)
7541
7542    for k, v in kwargs.items():
7543        col.set(k, v)
7544
7545    if quoted:
7546        for i in col.find_all(Identifier):
7547            i.set("quoted", True)
7548
7549    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts):
7552def alias_(
7553    expression: ExpOrStr,
7554    alias: t.Optional[str | Identifier],
7555    table: bool | t.Sequence[str | Identifier] = False,
7556    quoted: t.Optional[bool] = None,
7557    dialect: DialectType = None,
7558    copy: bool = True,
7559    **opts,
7560):
7561    """Create an Alias expression.
7562
7563    Example:
7564        >>> alias_('foo', 'bar').sql()
7565        'foo AS bar'
7566
7567        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
7568        '(SELECT 1, 2) AS bar(a, b)'
7569
7570    Args:
7571        expression: the SQL code strings to parse.
7572            If an Expression instance is passed, this is used as-is.
7573        alias: the alias name to use. If the name has
7574            special characters it is quoted.
7575        table: Whether to create a table alias, can also be a list of columns.
7576        quoted: whether to quote the alias
7577        dialect: the dialect used to parse the input expression.
7578        copy: Whether to copy the expression.
7579        **opts: other options to use to parse the input expressions.
7580
7581    Returns:
7582        Alias: the aliased expression
7583    """
7584    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
7585    alias = to_identifier(alias, quoted=quoted)
7586
7587    if table:
7588        table_alias = TableAlias(this=alias)
7589        exp.set("alias", table_alias)
7590
7591        if not isinstance(table, bool):
7592            for column in table:
7593                table_alias.append("columns", to_identifier(column, quoted=quoted))
7594
7595        return exp
7596
7597    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
7598    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
7599    # for the complete Window expression.
7600    #
7601    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
7602
7603    if "alias" in exp.arg_types and not isinstance(exp, Window):
7604        exp.set("alias", alias)
7605        return exp
7606    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Select:
7609def subquery(
7610    expression: ExpOrStr,
7611    alias: t.Optional[Identifier | str] = None,
7612    dialect: DialectType = None,
7613    **opts,
7614) -> Select:
7615    """
7616    Build a subquery expression that's selected from.
7617
7618    Example:
7619        >>> subquery('select x from tbl', 'bar').select('x').sql()
7620        'SELECT x FROM (SELECT x FROM tbl) AS bar'
7621
7622    Args:
7623        expression: the SQL code strings to parse.
7624            If an Expression instance is passed, this is used as-is.
7625        alias: the alias name to use.
7626        dialect: the dialect used to parse the input expression.
7627        **opts: other options to use to parse the input expressions.
7628
7629    Returns:
7630        A new Select instance with the subquery expression included.
7631    """
7632
7633    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias, **opts)
7634    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):
7665def column(
7666    col,
7667    table=None,
7668    db=None,
7669    catalog=None,
7670    *,
7671    fields=None,
7672    quoted=None,
7673    copy=True,
7674):
7675    """
7676    Build a Column.
7677
7678    Args:
7679        col: Column name.
7680        table: Table name.
7681        db: Database name.
7682        catalog: Catalog name.
7683        fields: Additional fields using dots.
7684        quoted: Whether to force quotes on the column's identifiers.
7685        copy: Whether to copy identifiers if passed in.
7686
7687    Returns:
7688        The new Column instance.
7689    """
7690    this = Column(
7691        this=to_identifier(col, quoted=quoted, copy=copy),
7692        table=to_identifier(table, quoted=quoted, copy=copy),
7693        db=to_identifier(db, quoted=quoted, copy=copy),
7694        catalog=to_identifier(catalog, quoted=quoted, copy=copy),
7695    )
7696
7697    if fields:
7698        this = Dot.build(
7699            (this, *(to_identifier(field, quoted=quoted, copy=copy) for field in fields))
7700        )
7701    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, DataType, DataType.Type], copy: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Cast:
7704def cast(
7705    expression: ExpOrStr, to: DATA_TYPE, copy: bool = True, dialect: DialectType = None, **opts
7706) -> Cast:
7707    """Cast an expression to a data type.
7708
7709    Example:
7710        >>> cast('x + 1', 'int').sql()
7711        'CAST(x + 1 AS INT)'
7712
7713    Args:
7714        expression: The expression to cast.
7715        to: The datatype to cast to.
7716        copy: Whether to copy the supplied expressions.
7717        dialect: The target dialect. This is used to prevent a re-cast in the following scenario:
7718            - The expression to be cast is already a exp.Cast expression
7719            - The existing cast is to a type that is logically equivalent to new type
7720
7721            For example, if :expression='CAST(x as DATETIME)' and :to=Type.TIMESTAMP,
7722            but in the target dialect DATETIME is mapped to TIMESTAMP, then we will NOT return `CAST(x (as DATETIME) as TIMESTAMP)`
7723            and instead just return the original expression `CAST(x as DATETIME)`.
7724
7725            This is to prevent it being output as a double cast `CAST(x (as TIMESTAMP) as TIMESTAMP)` once the DATETIME -> TIMESTAMP
7726            mapping is applied in the target dialect generator.
7727
7728    Returns:
7729        The new Cast instance.
7730    """
7731    expr = maybe_parse(expression, copy=copy, dialect=dialect, **opts)
7732    data_type = DataType.build(to, copy=copy, dialect=dialect, **opts)
7733
7734    # dont re-cast if the expression is already a cast to the correct type
7735    if isinstance(expr, Cast):
7736        from sqlglot.dialects.dialect import Dialect
7737
7738        target_dialect = Dialect.get_or_raise(dialect)
7739        type_mapping = target_dialect.generator_class.TYPE_MAPPING
7740
7741        existing_cast_type: DataType.Type = expr.to.this
7742        new_cast_type: DataType.Type = data_type.this
7743        types_are_equivalent = type_mapping.get(
7744            existing_cast_type, existing_cast_type
7745        ) == type_mapping.get(new_cast_type, new_cast_type)
7746        if expr.is_type(data_type) or types_are_equivalent:
7747            return expr
7748
7749    expr = Cast(this=expr, to=data_type)
7750    expr.type = data_type
7751
7752    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:
7755def table_(
7756    table: Identifier | str,
7757    db: t.Optional[Identifier | str] = None,
7758    catalog: t.Optional[Identifier | str] = None,
7759    quoted: t.Optional[bool] = None,
7760    alias: t.Optional[Identifier | str] = None,
7761) -> Table:
7762    """Build a Table.
7763
7764    Args:
7765        table: Table name.
7766        db: Database name.
7767        catalog: Catalog name.
7768        quote: Whether to force quotes on the table's identifiers.
7769        alias: Table's alias.
7770
7771    Returns:
7772        The new Table instance.
7773    """
7774    return Table(
7775        this=to_identifier(table, quoted=quoted) if table else None,
7776        db=to_identifier(db, quoted=quoted) if db else None,
7777        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
7778        alias=TableAlias(this=to_identifier(alias)) if alias else None,
7779    )

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:
7782def values(
7783    values: t.Iterable[t.Tuple[t.Any, ...]],
7784    alias: t.Optional[str] = None,
7785    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
7786) -> Values:
7787    """Build VALUES statement.
7788
7789    Example:
7790        >>> values([(1, '2')]).sql()
7791        "VALUES (1, '2')"
7792
7793    Args:
7794        values: values statements that will be converted to SQL
7795        alias: optional alias
7796        columns: Optional list of ordered column names or ordered dictionary of column names to types.
7797         If either are provided then an alias is also required.
7798
7799    Returns:
7800        Values: the Values expression object
7801    """
7802    if columns and not alias:
7803        raise ValueError("Alias is required when providing columns")
7804
7805    return Values(
7806        expressions=[convert(tup) for tup in values],
7807        alias=(
7808            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
7809            if columns
7810            else (TableAlias(this=to_identifier(alias)) if alias else None)
7811        ),
7812    )

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:
7815def var(name: t.Optional[ExpOrStr]) -> Var:
7816    """Build a SQL variable.
7817
7818    Example:
7819        >>> repr(var('x'))
7820        'Var(this=x)'
7821
7822        >>> repr(var(column('x', table='y')))
7823        'Var(this=x)'
7824
7825    Args:
7826        name: The name of the var or an expression who's name will become the var.
7827
7828    Returns:
7829        The new variable node.
7830    """
7831    if not name:
7832        raise ValueError("Cannot convert empty name into var.")
7833
7834    if isinstance(name, Expression):
7835        name = name.name
7836    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None) -> Alter:
7839def rename_table(
7840    old_name: str | Table,
7841    new_name: str | Table,
7842    dialect: DialectType = None,
7843) -> Alter:
7844    """Build ALTER TABLE... RENAME... expression
7845
7846    Args:
7847        old_name: The old name of the table
7848        new_name: The new name of the table
7849        dialect: The dialect to parse the table.
7850
7851    Returns:
7852        Alter table expression
7853    """
7854    old_table = to_table(old_name, dialect=dialect)
7855    new_table = to_table(new_name, dialect=dialect)
7856    return Alter(
7857        this=old_table,
7858        kind="TABLE",
7859        actions=[
7860            AlterRename(this=new_table),
7861        ],
7862    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None) -> Alter:
7865def rename_column(
7866    table_name: str | Table,
7867    old_column_name: str | Column,
7868    new_column_name: str | Column,
7869    exists: t.Optional[bool] = None,
7870    dialect: DialectType = None,
7871) -> Alter:
7872    """Build ALTER TABLE... RENAME COLUMN... expression
7873
7874    Args:
7875        table_name: Name of the table
7876        old_column: The old name of the column
7877        new_column: The new name of the column
7878        exists: Whether to add the `IF EXISTS` clause
7879        dialect: The dialect to parse the table/column.
7880
7881    Returns:
7882        Alter table expression
7883    """
7884    table = to_table(table_name, dialect=dialect)
7885    old_column = to_column(old_column_name, dialect=dialect)
7886    new_column = to_column(new_column_name, dialect=dialect)
7887    return Alter(
7888        this=table,
7889        kind="TABLE",
7890        actions=[
7891            RenameColumn(this=old_column, to=new_column, exists=exists),
7892        ],
7893    )

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:
7896def convert(value: t.Any, copy: bool = False) -> Expression:
7897    """Convert a python value into an expression object.
7898
7899    Raises an error if a conversion is not possible.
7900
7901    Args:
7902        value: A python object.
7903        copy: Whether to copy `value` (only applies to Expressions and collections).
7904
7905    Returns:
7906        The equivalent expression object.
7907    """
7908    if isinstance(value, Expression):
7909        return maybe_copy(value, copy)
7910    if isinstance(value, str):
7911        return Literal.string(value)
7912    if isinstance(value, bool):
7913        return Boolean(this=value)
7914    if value is None or (isinstance(value, float) and math.isnan(value)):
7915        return null()
7916    if isinstance(value, numbers.Number):
7917        return Literal.number(value)
7918    if isinstance(value, bytes):
7919        return HexString(this=value.hex())
7920    if isinstance(value, datetime.datetime):
7921        datetime_literal = Literal.string(value.isoformat(sep=" "))
7922
7923        tz = None
7924        if value.tzinfo:
7925            # this works for zoneinfo.ZoneInfo, pytz.timezone and datetime.datetime.utc to return IANA timezone names like "America/Los_Angeles"
7926            # instead of abbreviations like "PDT". This is for consistency with other timezone handling functions in SQLGlot
7927            tz = Literal.string(str(value.tzinfo))
7928
7929        return TimeStrToTime(this=datetime_literal, zone=tz)
7930    if isinstance(value, datetime.date):
7931        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
7932        return DateStrToDate(this=date_literal)
7933    if isinstance(value, tuple):
7934        if hasattr(value, "_fields"):
7935            return Struct(
7936                expressions=[
7937                    PropertyEQ(
7938                        this=to_identifier(k), expression=convert(getattr(value, k), copy=copy)
7939                    )
7940                    for k in value._fields
7941                ]
7942            )
7943        return Tuple(expressions=[convert(v, copy=copy) for v in value])
7944    if isinstance(value, list):
7945        return Array(expressions=[convert(v, copy=copy) for v in value])
7946    if isinstance(value, dict):
7947        return Map(
7948            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
7949            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
7950        )
7951    if hasattr(value, "__dict__"):
7952        return Struct(
7953            expressions=[
7954                PropertyEQ(this=to_identifier(k), expression=convert(v, copy=copy))
7955                for k, v in value.__dict__.items()
7956            ]
7957        )
7958    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:
7961def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
7962    """
7963    Replace children of an expression with the result of a lambda fun(child) -> exp.
7964    """
7965    for k, v in tuple(expression.args.items()):
7966        is_list_arg = type(v) is list
7967
7968        child_nodes = v if is_list_arg else [v]
7969        new_child_nodes = []
7970
7971        for cn in child_nodes:
7972            if isinstance(cn, Expression):
7973                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
7974                    new_child_nodes.append(child_node)
7975            else:
7976                new_child_nodes.append(cn)
7977
7978        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:
7981def replace_tree(
7982    expression: Expression,
7983    fun: t.Callable,
7984    prune: t.Optional[t.Callable[[Expression], bool]] = None,
7985) -> Expression:
7986    """
7987    Replace an entire tree with the result of function calls on each node.
7988
7989    This will be traversed in reverse dfs, so leaves first.
7990    If new nodes are created as a result of function calls, they will also be traversed.
7991    """
7992    stack = list(expression.dfs(prune=prune))
7993
7994    while stack:
7995        node = stack.pop()
7996        new_node = fun(node)
7997
7998        if new_node is not node:
7999            node.replace(new_node)
8000
8001            if isinstance(new_node, Expression):
8002                stack.append(new_node)
8003
8004    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]:
8007def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
8008    """
8009    Return all table names referenced through columns in an expression.
8010
8011    Example:
8012        >>> import sqlglot
8013        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
8014        ['a', 'c']
8015
8016    Args:
8017        expression: expression to find table names.
8018        exclude: a table name to exclude
8019
8020    Returns:
8021        A list of unique names.
8022    """
8023    return {
8024        table
8025        for table in (column.table for column in expression.find_all(Column))
8026        if table and table != exclude
8027    }

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, identify: bool = False) -> str:
8030def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
8031    """Get the full name of a table as a string.
8032
8033    Args:
8034        table: Table expression node or string.
8035        dialect: The dialect to generate the table name for.
8036        identify: Determines when an identifier should be quoted. Possible values are:
8037            False (default): Never quote, except in cases where it's mandatory by the dialect.
8038            True: Always quote.
8039
8040    Examples:
8041        >>> from sqlglot import exp, parse_one
8042        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
8043        'a.b.c'
8044
8045    Returns:
8046        The table name.
8047    """
8048
8049    table = maybe_parse(table, into=Table, dialect=dialect)
8050
8051    if not table:
8052        raise ValueError(f"Cannot parse {table}")
8053
8054    return ".".join(
8055        (
8056            part.sql(dialect=dialect, identify=True, copy=False)
8057            if identify or not SAFE_IDENTIFIER_RE.match(part.name)
8058            else part.name
8059        )
8060        for part in table.parts
8061    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True) -> str:
8064def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
8065    """Returns a case normalized table name without quotes.
8066
8067    Args:
8068        table: the table to normalize
8069        dialect: the dialect to use for normalization rules
8070        copy: whether to copy the expression.
8071
8072    Examples:
8073        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
8074        'A-B.c'
8075    """
8076    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
8077
8078    return ".".join(
8079        p.name
8080        for p in normalize_identifiers(
8081            to_table(table, dialect=dialect, copy=copy), dialect=dialect
8082        ).parts
8083    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True) -> ~E:
8086def replace_tables(
8087    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
8088) -> E:
8089    """Replace all tables in expression according to the mapping.
8090
8091    Args:
8092        expression: expression node to be transformed and replaced.
8093        mapping: mapping of table names.
8094        dialect: the dialect of the mapping table
8095        copy: whether to copy the expression.
8096
8097    Examples:
8098        >>> from sqlglot import exp, parse_one
8099        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
8100        'SELECT * FROM c /* a.b */'
8101
8102    Returns:
8103        The mapped expression.
8104    """
8105
8106    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
8107
8108    def _replace_tables(node: Expression) -> Expression:
8109        if isinstance(node, Table):
8110            original = normalize_table_name(node, dialect=dialect)
8111            new_name = mapping.get(original)
8112
8113            if new_name:
8114                table = to_table(
8115                    new_name,
8116                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
8117                    dialect=dialect,
8118                )
8119                table.add_comments([original])
8120                return table
8121        return node
8122
8123    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:
8126def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
8127    """Replace placeholders in an expression.
8128
8129    Args:
8130        expression: expression node to be transformed and replaced.
8131        args: positional names that will substitute unnamed placeholders in the given order.
8132        kwargs: keyword arguments that will substitute named placeholders.
8133
8134    Examples:
8135        >>> from sqlglot import exp, parse_one
8136        >>> replace_placeholders(
8137        ...     parse_one("select * from :tbl where ? = ?"),
8138        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
8139        ... ).sql()
8140        "SELECT * FROM foo WHERE str_col = 'b'"
8141
8142    Returns:
8143        The mapped expression.
8144    """
8145
8146    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
8147        if isinstance(node, Placeholder):
8148            if node.this:
8149                new_name = kwargs.get(node.this)
8150                if new_name is not None:
8151                    return convert(new_name)
8152            else:
8153                try:
8154                    return convert(next(args))
8155                except StopIteration:
8156                    pass
8157        return node
8158
8159    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, Query], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True) -> Expression:
8162def expand(
8163    expression: Expression,
8164    sources: t.Dict[str, Query],
8165    dialect: DialectType = None,
8166    copy: bool = True,
8167) -> Expression:
8168    """Transforms an expression by expanding all referenced sources into subqueries.
8169
8170    Examples:
8171        >>> from sqlglot import parse_one
8172        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
8173        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
8174
8175        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
8176        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
8177
8178    Args:
8179        expression: The expression to expand.
8180        sources: A dictionary of name to Queries.
8181        dialect: The dialect of the sources dict.
8182        copy: Whether to copy the expression during transformation. Defaults to True.
8183
8184    Returns:
8185        The transformed expression.
8186    """
8187    sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
8188
8189    def _expand(node: Expression):
8190        if isinstance(node, Table):
8191            name = normalize_table_name(node, dialect=dialect)
8192            source = sources.get(name)
8193            if source:
8194                subquery = source.subquery(node.alias or name)
8195                subquery.comments = [f"source: {name}"]
8196                return subquery.transform(_expand, copy=False)
8197        return node
8198
8199    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 dictionary of name to Queries.
  • dialect: The dialect of the sources dict.
  • 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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **kwargs) -> Func:
8202def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
8203    """
8204    Returns a Func expression.
8205
8206    Examples:
8207        >>> func("abs", 5).sql()
8208        'ABS(5)'
8209
8210        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
8211        'CAST(5 AS DOUBLE)'
8212
8213    Args:
8214        name: the name of the function to build.
8215        args: the args used to instantiate the function of interest.
8216        copy: whether to copy the argument expressions.
8217        dialect: the source dialect.
8218        kwargs: the kwargs used to instantiate the function of interest.
8219
8220    Note:
8221        The arguments `args` and `kwargs` are mutually exclusive.
8222
8223    Returns:
8224        An instance of the function of interest, or an anonymous function, if `name` doesn't
8225        correspond to an existing `sqlglot.expressions.Func` class.
8226    """
8227    if args and kwargs:
8228        raise ValueError("Can't use both args and kwargs to instantiate a function.")
8229
8230    from sqlglot.dialects.dialect import Dialect
8231
8232    dialect = Dialect.get_or_raise(dialect)
8233
8234    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
8235    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
8236
8237    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
8238    if constructor:
8239        if converted:
8240            if "dialect" in constructor.__code__.co_varnames:
8241                function = constructor(converted, dialect=dialect)
8242            else:
8243                function = constructor(converted)
8244        elif constructor.__name__ == "from_arg_list":
8245            function = constructor.__self__(**kwargs)  # type: ignore
8246        else:
8247            constructor = FUNCTION_BY_NAME.get(name.upper())
8248            if constructor:
8249                function = constructor(**kwargs)
8250            else:
8251                raise ValueError(
8252                    f"Unable to convert '{name}' into a Func. Either manually construct "
8253                    "the Func expression of interest or parse the function call."
8254                )
8255    else:
8256        kwargs = kwargs or {"expressions": converted}
8257        function = Anonymous(this=name, **kwargs)
8258
8259    for error_message in function.error_messages(converted):
8260        raise ValueError(error_message)
8261
8262    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:
8265def case(
8266    expression: t.Optional[ExpOrStr] = None,
8267    **opts,
8268) -> Case:
8269    """
8270    Initialize a CASE statement.
8271
8272    Example:
8273        case().when("a = 1", "foo").else_("bar")
8274
8275    Args:
8276        expression: Optionally, the input expression (not all dialects support this)
8277        **opts: Extra keyword arguments for parsing `expression`
8278    """
8279    if expression is not None:
8280        this = maybe_parse(expression, **opts)
8281    else:
8282        this = None
8283    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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **kwargs) -> Array:
8286def array(
8287    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8288) -> Array:
8289    """
8290    Returns an array.
8291
8292    Examples:
8293        >>> array(1, 'x').sql()
8294        'ARRAY(1, x)'
8295
8296    Args:
8297        expressions: the expressions to add to the array.
8298        copy: whether to copy the argument expressions.
8299        dialect: the source dialect.
8300        kwargs: the kwargs used to instantiate the function of interest.
8301
8302    Returns:
8303        An array expression.
8304    """
8305    return Array(
8306        expressions=[
8307            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8308            for expression in expressions
8309        ]
8310    )

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.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **kwargs) -> Tuple:
8313def tuple_(
8314    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8315) -> Tuple:
8316    """
8317    Returns an tuple.
8318
8319    Examples:
8320        >>> tuple_(1, 'x').sql()
8321        '(1, x)'
8322
8323    Args:
8324        expressions: the expressions to add to the tuple.
8325        copy: whether to copy the argument expressions.
8326        dialect: the source dialect.
8327        kwargs: the kwargs used to instantiate the function of interest.
8328
8329    Returns:
8330        A tuple expression.
8331    """
8332    return Tuple(
8333        expressions=[
8334            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8335            for expression in expressions
8336        ]
8337    )

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:
8340def true() -> Boolean:
8341    """
8342    Returns a true Boolean expression.
8343    """
8344    return Boolean(this=True)

Returns a true Boolean expression.

def false() -> Boolean:
8347def false() -> Boolean:
8348    """
8349    Returns a false Boolean expression.
8350    """
8351    return Boolean(this=False)

Returns a false Boolean expression.

def null() -> Null:
8354def null() -> Null:
8355    """
8356    Returns a Null expression.
8357    """
8358    return Null()

Returns a Null expression.

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