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

Returns the depth of this tree.

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

Returns the parent select statement.

same_parent: bool
474    @property
475    def same_parent(self) -> bool:
476        """Returns if the parent is the same class as itself."""
477        return type(self.parent) is self.__class__

Returns if the parent is the same class as itself.

def root(self) -> Expression:
479    def root(self) -> Expression:
480        """
481        Returns the root expression of this tree.
482        """
483        expression = self
484        while expression.parent:
485            expression = expression.parent
486        return expression

Returns the root expression of this tree.

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

Returns the first non parenthesis child or self.

def unalias(self):
563    def unalias(self):
564        """
565        Returns the inner expression if this is an Alias.
566        """
567        if isinstance(self, Alias):
568            return self.this
569        return self

Returns the inner expression if this is an Alias.

def unnest_operands(self):
571    def unnest_operands(self):
572        """
573        Returns unnested operands as a tuple.
574        """
575        return tuple(arg.unnest() for arg in self.iter_expressions())

Returns unnested operands as a tuple.

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

Remove this expression from its AST.

Returns:

The popped expression.

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

Dump this Expression to a JSON-serializable dict.

@classmethod
def load(cls, obj):
762    @classmethod
763    def load(cls, obj):
764        """
765        Load a dict (as returned by `Expression.dump`) into an Expression instance.
766        """
767        from sqlglot.serde import load
768
769        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, wrap: bool = True, **opts) -> Condition:
771    def and_(
772        self,
773        *expressions: t.Optional[ExpOrStr],
774        dialect: DialectType = None,
775        copy: bool = True,
776        wrap: bool = True,
777        **opts,
778    ) -> Condition:
779        """
780        AND this condition with one or multiple expressions.
781
782        Example:
783            >>> condition("x=1").and_("y=1").sql()
784            'x = 1 AND y = 1'
785
786        Args:
787            *expressions: the SQL code strings to parse.
788                If an `Expression` instance is passed, it will be used as-is.
789            dialect: the dialect used to parse the input expression.
790            copy: whether to copy the involved expressions (only applies to Expressions).
791            wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
792                precedence issues, but can be turned off when the produced AST is too deep and
793                causes recursion-related issues.
794            opts: other options to use to parse the input expressions.
795
796        Returns:
797            The new And condition.
798        """
799        return and_(self, *expressions, dialect=dialect, copy=copy, wrap=wrap, **opts)

AND this condition with one or multiple expressions.

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

The new And condition.

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

OR this condition with one or multiple expressions.

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

The new Or condition.

def not_(self, copy: bool = True):
831    def not_(self, copy: bool = True):
832        """
833        Wrap this condition with NOT.
834
835        Example:
836            >>> condition("x=1").not_().sql()
837            'NOT x = 1'
838
839        Args:
840            copy: whether to copy this object.
841
842        Returns:
843            The new Not instance.
844        """
845        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:
847    def as_(
848        self,
849        alias: str | Identifier,
850        quoted: t.Optional[bool] = None,
851        dialect: DialectType = None,
852        copy: bool = True,
853        **opts,
854    ) -> Alias:
855        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:
880    def isin(
881        self,
882        *expressions: t.Any,
883        query: t.Optional[ExpOrStr] = None,
884        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
885        copy: bool = True,
886        **opts,
887    ) -> In:
888        subquery = maybe_parse(query, copy=copy, **opts) if query else None
889        if subquery and not isinstance(subquery, Subquery):
890            subquery = subquery.subquery(copy=False)
891
892        return In(
893            this=maybe_copy(self, copy),
894            expressions=[convert(e, copy=copy) for e in expressions],
895            query=subquery,
896            unnest=(
897                Unnest(
898                    expressions=[
899                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
900                        for e in ensure_list(unnest)
901                    ]
902                )
903                if unnest
904                else None
905            ),
906        )
def between( self, low: Any, high: Any, copy: bool = True, **opts) -> Between:
908    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
909        return Between(
910            this=maybe_copy(self, copy),
911            low=convert(low, copy=copy, **opts),
912            high=convert(high, copy=copy, **opts),
913        )
def is_( self, other: Union[str, Expression]) -> Is:
915    def is_(self, other: ExpOrStr) -> Is:
916        return self._binop(Is, other)
def like( self, other: Union[str, Expression]) -> Like:
918    def like(self, other: ExpOrStr) -> Like:
919        return self._binop(Like, other)
def ilike( self, other: Union[str, Expression]) -> ILike:
921    def ilike(self, other: ExpOrStr) -> ILike:
922        return self._binop(ILike, other)
def eq(self, other: Any) -> EQ:
924    def eq(self, other: t.Any) -> EQ:
925        return self._binop(EQ, other)
def neq(self, other: Any) -> NEQ:
927    def neq(self, other: t.Any) -> NEQ:
928        return self._binop(NEQ, other)
def rlike( self, other: Union[str, Expression]) -> RegexpLike:
930    def rlike(self, other: ExpOrStr) -> RegexpLike:
931        return self._binop(RegexpLike, other)
def div( self, other: Union[str, Expression], typed: bool = False, safe: bool = False) -> Div:
933    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
934        div = self._binop(Div, other)
935        div.args["typed"] = typed
936        div.args["safe"] = safe
937        return div
def asc(self, nulls_first: bool = True) -> Ordered:
939    def asc(self, nulls_first: bool = True) -> Ordered:
940        return Ordered(this=self.copy(), nulls_first=nulls_first)
def desc(self, nulls_first: bool = False) -> Ordered:
942    def desc(self, nulls_first: bool = False) -> Ordered:
943        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):
1026class Condition(Expression):
1027    """Logical conditions like x AND y, or simply x"""

Logical conditions like x AND y, or simply x

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

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

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

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:
1098    def offset(
1099        self: Q, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
1100    ) -> Q:
1101        """
1102        Set the OFFSET expression.
1103
1104        Example:
1105            >>> Select().from_("tbl").select("x").offset(10).sql()
1106            'SELECT x FROM tbl OFFSET 10'
1107
1108        Args:
1109            expression: the SQL code string to parse.
1110                This can also be an integer.
1111                If a `Offset` instance is passed, this is used as-is.
1112                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
1113            dialect: the dialect used to parse the input expression.
1114            copy: if `False`, modify this expression instance in-place.
1115            opts: other options to use to parse the input expressions.
1116
1117        Returns:
1118            The modified Select expression.
1119        """
1120        return _apply_builder(
1121            expression=expression,
1122            instance=self,
1123            arg="offset",
1124            into=Offset,
1125            prefix="OFFSET",
1126            dialect=dialect,
1127            copy=copy,
1128            into_arg="expression",
1129            **opts,
1130        )

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:
1132    def order_by(
1133        self: Q,
1134        *expressions: t.Optional[ExpOrStr],
1135        append: bool = True,
1136        dialect: DialectType = None,
1137        copy: bool = True,
1138        **opts,
1139    ) -> Q:
1140        """
1141        Set the ORDER BY expression.
1142
1143        Example:
1144            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
1145            'SELECT x FROM tbl ORDER BY x DESC'
1146
1147        Args:
1148            *expressions: the SQL code strings to parse.
1149                If a `Group` instance is passed, this is used as-is.
1150                If another `Expression` instance is passed, it will be wrapped in a `Order`.
1151            append: if `True`, add to any existing expressions.
1152                Otherwise, this flattens all the `Order` expression into a single expression.
1153            dialect: the dialect used to parse the input expression.
1154            copy: if `False`, modify this expression instance in-place.
1155            opts: other options to use to parse the input expressions.
1156
1157        Returns:
1158            The modified Select expression.
1159        """
1160        return _apply_child_list_builder(
1161            *expressions,
1162            instance=self,
1163            arg="order",
1164            append=append,
1165            copy=copy,
1166            prefix="ORDER BY",
1167            into=Order,
1168            dialect=dialect,
1169            **opts,
1170        )

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

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

selects: List[Expression]
1178    @property
1179    def selects(self) -> t.List[Expression]:
1180        """Returns the query's projections."""
1181        raise NotImplementedError("Query objects must implement `selects`")

Returns the query's projections.

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

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

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

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

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):
1415class Create(DDL):
1416    arg_types = {
1417        "with": False,
1418        "this": True,
1419        "kind": True,
1420        "expression": False,
1421        "exists": False,
1422        "properties": False,
1423        "replace": False,
1424        "refresh": False,
1425        "unique": False,
1426        "indexes": False,
1427        "no_schema_binding": False,
1428        "begin": False,
1429        "end": False,
1430        "clone": False,
1431        "concurrently": False,
1432        "clustered": False,
1433    }
1434
1435    @property
1436    def kind(self) -> t.Optional[str]:
1437        kind = self.args.get("kind")
1438        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]
1435    @property
1436    def kind(self) -> t.Optional[str]:
1437        kind = self.args.get("kind")
1438        return kind and kind.upper()
key = 'create'
class SequenceProperties(Expression):
1441class SequenceProperties(Expression):
1442    arg_types = {
1443        "increment": False,
1444        "minvalue": False,
1445        "maxvalue": False,
1446        "cache": False,
1447        "start": False,
1448        "owned": False,
1449        "options": False,
1450    }
arg_types = {'increment': False, 'minvalue': False, 'maxvalue': False, 'cache': False, 'start': False, 'owned': False, 'options': False}
key = 'sequenceproperties'
class TruncateTable(Expression):
1453class TruncateTable(Expression):
1454    arg_types = {
1455        "expressions": True,
1456        "is_database": False,
1457        "exists": False,
1458        "only": False,
1459        "cluster": False,
1460        "identity": False,
1461        "option": False,
1462        "partition": False,
1463    }
arg_types = {'expressions': True, 'is_database': False, 'exists': False, 'only': False, 'cluster': False, 'identity': False, 'option': False, 'partition': False}
key = 'truncatetable'
class Clone(Expression):
1469class Clone(Expression):
1470    arg_types = {"this": True, "shallow": False, "copy": False}
arg_types = {'this': True, 'shallow': False, 'copy': False}
key = 'clone'
class Describe(Expression):
1473class Describe(Expression):
1474    arg_types = {
1475        "this": True,
1476        "style": False,
1477        "kind": False,
1478        "expressions": False,
1479        "partition": False,
1480        "format": False,
1481    }
arg_types = {'this': True, 'style': False, 'kind': False, 'expressions': False, 'partition': False, 'format': False}
key = 'describe'
class Attach(Expression):
1485class Attach(Expression):
1486    arg_types = {"this": True, "exists": False, "expressions": False}
arg_types = {'this': True, 'exists': False, 'expressions': False}
key = 'attach'
class Detach(Expression):
1490class Detach(Expression):
1491    arg_types = {"this": True, "exists": False}
arg_types = {'this': True, 'exists': False}
key = 'detach'
class Summarize(Expression):
1495class Summarize(Expression):
1496    arg_types = {"this": True, "table": False}
arg_types = {'this': True, 'table': False}
key = 'summarize'
class Kill(Expression):
1499class Kill(Expression):
1500    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'kill'
class Pragma(Expression):
1503class Pragma(Expression):
1504    pass
key = 'pragma'
class Declare(Expression):
1507class Declare(Expression):
1508    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'declare'
class DeclareItem(Expression):
1511class DeclareItem(Expression):
1512    arg_types = {"this": True, "kind": True, "default": False}
arg_types = {'this': True, 'kind': True, 'default': False}
key = 'declareitem'
class Set(Expression):
1515class Set(Expression):
1516    arg_types = {"expressions": False, "unset": False, "tag": False}
arg_types = {'expressions': False, 'unset': False, 'tag': False}
key = 'set'
class Heredoc(Expression):
1519class Heredoc(Expression):
1520    arg_types = {"this": True, "tag": False}
arg_types = {'this': True, 'tag': False}
key = 'heredoc'
class SetItem(Expression):
1523class SetItem(Expression):
1524    arg_types = {
1525        "this": False,
1526        "expressions": False,
1527        "kind": False,
1528        "collate": False,  # MySQL SET NAMES statement
1529        "global": False,
1530    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'collate': False, 'global': False}
key = 'setitem'
class Show(Expression):
1533class Show(Expression):
1534    arg_types = {
1535        "this": True,
1536        "history": False,
1537        "terse": False,
1538        "target": False,
1539        "offset": False,
1540        "starts_with": False,
1541        "limit": False,
1542        "from": False,
1543        "like": False,
1544        "where": False,
1545        "db": False,
1546        "scope": False,
1547        "scope_kind": False,
1548        "full": False,
1549        "mutex": False,
1550        "query": False,
1551        "channel": False,
1552        "global": False,
1553        "log": False,
1554        "position": False,
1555        "types": False,
1556    }
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):
1559class UserDefinedFunction(Expression):
1560    arg_types = {"this": True, "expressions": False, "wrapped": False}
arg_types = {'this': True, 'expressions': False, 'wrapped': False}
key = 'userdefinedfunction'
class CharacterSet(Expression):
1563class CharacterSet(Expression):
1564    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'characterset'
class With(Expression):
1567class With(Expression):
1568    arg_types = {"expressions": True, "recursive": False}
1569
1570    @property
1571    def recursive(self) -> bool:
1572        return bool(self.args.get("recursive"))
arg_types = {'expressions': True, 'recursive': False}
recursive: bool
1570    @property
1571    def recursive(self) -> bool:
1572        return bool(self.args.get("recursive"))
key = 'with'
class WithinGroup(Expression):
1575class WithinGroup(Expression):
1576    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'withingroup'
class CTE(DerivedTable):
1581class CTE(DerivedTable):
1582    arg_types = {
1583        "this": True,
1584        "alias": True,
1585        "scalar": False,
1586        "materialized": False,
1587    }
arg_types = {'this': True, 'alias': True, 'scalar': False, 'materialized': False}
key = 'cte'
class ProjectionDef(Expression):
1590class ProjectionDef(Expression):
1591    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'projectiondef'
class TableAlias(Expression):
1594class TableAlias(Expression):
1595    arg_types = {"this": False, "columns": False}
1596
1597    @property
1598    def columns(self):
1599        return self.args.get("columns") or []
arg_types = {'this': False, 'columns': False}
columns
1597    @property
1598    def columns(self):
1599        return self.args.get("columns") or []
key = 'tablealias'
class BitString(Condition):
1602class BitString(Condition):
1603    pass
key = 'bitstring'
class HexString(Condition):
1606class HexString(Condition):
1607    pass
key = 'hexstring'
class ByteString(Condition):
1610class ByteString(Condition):
1611    pass
key = 'bytestring'
class RawString(Condition):
1614class RawString(Condition):
1615    pass
key = 'rawstring'
class UnicodeString(Condition):
1618class UnicodeString(Condition):
1619    arg_types = {"this": True, "escape": False}
arg_types = {'this': True, 'escape': False}
key = 'unicodestring'
class Column(Condition):
1622class Column(Condition):
1623    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1624
1625    @property
1626    def table(self) -> str:
1627        return self.text("table")
1628
1629    @property
1630    def db(self) -> str:
1631        return self.text("db")
1632
1633    @property
1634    def catalog(self) -> str:
1635        return self.text("catalog")
1636
1637    @property
1638    def output_name(self) -> str:
1639        return self.name
1640
1641    @property
1642    def parts(self) -> t.List[Identifier]:
1643        """Return the parts of a column in order catalog, db, table, name."""
1644        return [
1645            t.cast(Identifier, self.args[part])
1646            for part in ("catalog", "db", "table", "this")
1647            if self.args.get(part)
1648        ]
1649
1650    def to_dot(self) -> Dot | Identifier:
1651        """Converts the column into a dot expression."""
1652        parts = self.parts
1653        parent = self.parent
1654
1655        while parent:
1656            if isinstance(parent, Dot):
1657                parts.append(parent.expression)
1658            parent = parent.parent
1659
1660        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
1625    @property
1626    def table(self) -> str:
1627        return self.text("table")
db: str
1629    @property
1630    def db(self) -> str:
1631        return self.text("db")
catalog: str
1633    @property
1634    def catalog(self) -> str:
1635        return self.text("catalog")
output_name: str
1637    @property
1638    def output_name(self) -> str:
1639        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]
1641    @property
1642    def parts(self) -> t.List[Identifier]:
1643        """Return the parts of a column in order catalog, db, table, name."""
1644        return [
1645            t.cast(Identifier, self.args[part])
1646            for part in ("catalog", "db", "table", "this")
1647            if self.args.get(part)
1648        ]

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

def to_dot(self) -> Dot | Identifier:
1650    def to_dot(self) -> Dot | Identifier:
1651        """Converts the column into a dot expression."""
1652        parts = self.parts
1653        parent = self.parent
1654
1655        while parent:
1656            if isinstance(parent, Dot):
1657                parts.append(parent.expression)
1658            parent = parent.parent
1659
1660        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]

Converts the column into a dot expression.

key = 'column'
class ColumnPosition(Expression):
1663class ColumnPosition(Expression):
1664    arg_types = {"this": False, "position": True}
arg_types = {'this': False, 'position': True}
key = 'columnposition'
class ColumnDef(Expression):
1667class ColumnDef(Expression):
1668    arg_types = {
1669        "this": True,
1670        "kind": False,
1671        "constraints": False,
1672        "exists": False,
1673        "position": False,
1674    }
1675
1676    @property
1677    def constraints(self) -> t.List[ColumnConstraint]:
1678        return self.args.get("constraints") or []
1679
1680    @property
1681    def kind(self) -> t.Optional[DataType]:
1682        return self.args.get("kind")
arg_types = {'this': True, 'kind': False, 'constraints': False, 'exists': False, 'position': False}
constraints: List[ColumnConstraint]
1676    @property
1677    def constraints(self) -> t.List[ColumnConstraint]:
1678        return self.args.get("constraints") or []
kind: Optional[DataType]
1680    @property
1681    def kind(self) -> t.Optional[DataType]:
1682        return self.args.get("kind")
key = 'columndef'
class AlterColumn(Expression):
1685class AlterColumn(Expression):
1686    arg_types = {
1687        "this": True,
1688        "dtype": False,
1689        "collate": False,
1690        "using": False,
1691        "default": False,
1692        "drop": False,
1693        "comment": False,
1694        "allow_null": False,
1695    }
arg_types = {'this': True, 'dtype': False, 'collate': False, 'using': False, 'default': False, 'drop': False, 'comment': False, 'allow_null': False}
key = 'altercolumn'
class AlterDistStyle(Expression):
1699class AlterDistStyle(Expression):
1700    pass
key = 'alterdiststyle'
class AlterSortKey(Expression):
1703class AlterSortKey(Expression):
1704    arg_types = {"this": False, "expressions": False, "compound": False}
arg_types = {'this': False, 'expressions': False, 'compound': False}
key = 'altersortkey'
class AlterSet(Expression):
1707class AlterSet(Expression):
1708    arg_types = {
1709        "expressions": False,
1710        "option": False,
1711        "tablespace": False,
1712        "access_method": False,
1713        "file_format": False,
1714        "copy_options": False,
1715        "tag": False,
1716        "location": False,
1717        "serde": False,
1718    }
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):
1721class RenameColumn(Expression):
1722    arg_types = {"this": True, "to": True, "exists": False}
arg_types = {'this': True, 'to': True, 'exists': False}
key = 'renamecolumn'
class AlterRename(Expression):
1725class AlterRename(Expression):
1726    pass
key = 'alterrename'
class SwapTable(Expression):
1729class SwapTable(Expression):
1730    pass
key = 'swaptable'
class Comment(Expression):
1733class Comment(Expression):
1734    arg_types = {
1735        "this": True,
1736        "kind": True,
1737        "expression": True,
1738        "exists": False,
1739        "materialized": False,
1740    }
arg_types = {'this': True, 'kind': True, 'expression': True, 'exists': False, 'materialized': False}
key = 'comment'
class Comprehension(Expression):
1743class Comprehension(Expression):
1744    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):
1748class MergeTreeTTLAction(Expression):
1749    arg_types = {
1750        "this": True,
1751        "delete": False,
1752        "recompress": False,
1753        "to_disk": False,
1754        "to_volume": False,
1755    }
arg_types = {'this': True, 'delete': False, 'recompress': False, 'to_disk': False, 'to_volume': False}
key = 'mergetreettlaction'
class MergeTreeTTL(Expression):
1759class MergeTreeTTL(Expression):
1760    arg_types = {
1761        "expressions": True,
1762        "where": False,
1763        "group": False,
1764        "aggregates": False,
1765    }
arg_types = {'expressions': True, 'where': False, 'group': False, 'aggregates': False}
key = 'mergetreettl'
class IndexConstraintOption(Expression):
1769class IndexConstraintOption(Expression):
1770    arg_types = {
1771        "key_block_size": False,
1772        "using": False,
1773        "parser": False,
1774        "comment": False,
1775        "visible": False,
1776        "engine_attr": False,
1777        "secondary_engine_attr": False,
1778    }
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):
1781class ColumnConstraint(Expression):
1782    arg_types = {"this": False, "kind": True}
1783
1784    @property
1785    def kind(self) -> ColumnConstraintKind:
1786        return self.args["kind"]
arg_types = {'this': False, 'kind': True}
kind: ColumnConstraintKind
1784    @property
1785    def kind(self) -> ColumnConstraintKind:
1786        return self.args["kind"]
key = 'columnconstraint'
class ColumnConstraintKind(Expression):
1789class ColumnConstraintKind(Expression):
1790    pass
key = 'columnconstraintkind'
class AutoIncrementColumnConstraint(ColumnConstraintKind):
1793class AutoIncrementColumnConstraint(ColumnConstraintKind):
1794    pass
key = 'autoincrementcolumnconstraint'
class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1797class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1798    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'periodforsystemtimeconstraint'
class CaseSpecificColumnConstraint(ColumnConstraintKind):
1801class CaseSpecificColumnConstraint(ColumnConstraintKind):
1802    arg_types = {"not_": True}
arg_types = {'not_': True}
key = 'casespecificcolumnconstraint'
class CharacterSetColumnConstraint(ColumnConstraintKind):
1805class CharacterSetColumnConstraint(ColumnConstraintKind):
1806    arg_types = {"this": True}
arg_types = {'this': True}
key = 'charactersetcolumnconstraint'
class CheckColumnConstraint(ColumnConstraintKind):
1809class CheckColumnConstraint(ColumnConstraintKind):
1810    arg_types = {"this": True, "enforced": False}
arg_types = {'this': True, 'enforced': False}
key = 'checkcolumnconstraint'
class ClusteredColumnConstraint(ColumnConstraintKind):
1813class ClusteredColumnConstraint(ColumnConstraintKind):
1814    pass
key = 'clusteredcolumnconstraint'
class CollateColumnConstraint(ColumnConstraintKind):
1817class CollateColumnConstraint(ColumnConstraintKind):
1818    pass
key = 'collatecolumnconstraint'
class CommentColumnConstraint(ColumnConstraintKind):
1821class CommentColumnConstraint(ColumnConstraintKind):
1822    pass
key = 'commentcolumnconstraint'
class CompressColumnConstraint(ColumnConstraintKind):
1825class CompressColumnConstraint(ColumnConstraintKind):
1826    arg_types = {"this": False}
arg_types = {'this': False}
key = 'compresscolumnconstraint'
class DateFormatColumnConstraint(ColumnConstraintKind):
1829class DateFormatColumnConstraint(ColumnConstraintKind):
1830    arg_types = {"this": True}
arg_types = {'this': True}
key = 'dateformatcolumnconstraint'
class DefaultColumnConstraint(ColumnConstraintKind):
1833class DefaultColumnConstraint(ColumnConstraintKind):
1834    pass
key = 'defaultcolumnconstraint'
class EncodeColumnConstraint(ColumnConstraintKind):
1837class EncodeColumnConstraint(ColumnConstraintKind):
1838    pass
key = 'encodecolumnconstraint'
class ExcludeColumnConstraint(ColumnConstraintKind):
1842class ExcludeColumnConstraint(ColumnConstraintKind):
1843    pass
key = 'excludecolumnconstraint'
class EphemeralColumnConstraint(ColumnConstraintKind):
1846class EphemeralColumnConstraint(ColumnConstraintKind):
1847    arg_types = {"this": False}
arg_types = {'this': False}
key = 'ephemeralcolumnconstraint'
class WithOperator(Expression):
1850class WithOperator(Expression):
1851    arg_types = {"this": True, "op": True}
arg_types = {'this': True, 'op': True}
key = 'withoperator'
class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1854class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1855    # this: True -> ALWAYS, this: False -> BY DEFAULT
1856    arg_types = {
1857        "this": False,
1858        "expression": False,
1859        "on_null": False,
1860        "start": False,
1861        "increment": False,
1862        "minvalue": False,
1863        "maxvalue": False,
1864        "cycle": False,
1865    }
arg_types = {'this': False, 'expression': False, 'on_null': False, 'start': False, 'increment': False, 'minvalue': False, 'maxvalue': False, 'cycle': False}
key = 'generatedasidentitycolumnconstraint'
class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1868class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1869    arg_types = {"start": False, "hidden": False}
arg_types = {'start': False, 'hidden': False}
key = 'generatedasrowcolumnconstraint'
class IndexColumnConstraint(ColumnConstraintKind):
1874class IndexColumnConstraint(ColumnConstraintKind):
1875    arg_types = {
1876        "this": False,
1877        "expressions": False,
1878        "kind": False,
1879        "index_type": False,
1880        "options": False,
1881        "expression": False,  # Clickhouse
1882        "granularity": False,
1883    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'index_type': False, 'options': False, 'expression': False, 'granularity': False}
key = 'indexcolumnconstraint'
class InlineLengthColumnConstraint(ColumnConstraintKind):
1886class InlineLengthColumnConstraint(ColumnConstraintKind):
1887    pass
key = 'inlinelengthcolumnconstraint'
class NonClusteredColumnConstraint(ColumnConstraintKind):
1890class NonClusteredColumnConstraint(ColumnConstraintKind):
1891    pass
key = 'nonclusteredcolumnconstraint'
class NotForReplicationColumnConstraint(ColumnConstraintKind):
1894class NotForReplicationColumnConstraint(ColumnConstraintKind):
1895    arg_types = {}
arg_types = {}
key = 'notforreplicationcolumnconstraint'
class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1899class MaskingPolicyColumnConstraint(ColumnConstraintKind):
1900    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'maskingpolicycolumnconstraint'
class NotNullColumnConstraint(ColumnConstraintKind):
1903class NotNullColumnConstraint(ColumnConstraintKind):
1904    arg_types = {"allow_null": False}
arg_types = {'allow_null': False}
key = 'notnullcolumnconstraint'
class OnUpdateColumnConstraint(ColumnConstraintKind):
1908class OnUpdateColumnConstraint(ColumnConstraintKind):
1909    pass
key = 'onupdatecolumnconstraint'
class TransformColumnConstraint(ColumnConstraintKind):
1913class TransformColumnConstraint(ColumnConstraintKind):
1914    pass
key = 'transformcolumnconstraint'
class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1917class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1918    arg_types = {"desc": False}
arg_types = {'desc': False}
key = 'primarykeycolumnconstraint'
class TitleColumnConstraint(ColumnConstraintKind):
1921class TitleColumnConstraint(ColumnConstraintKind):
1922    pass
key = 'titlecolumnconstraint'
class UniqueColumnConstraint(ColumnConstraintKind):
1925class UniqueColumnConstraint(ColumnConstraintKind):
1926    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):
1929class UppercaseColumnConstraint(ColumnConstraintKind):
1930    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'uppercasecolumnconstraint'
class WatermarkColumnConstraint(Expression):
1934class WatermarkColumnConstraint(Expression):
1935    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'watermarkcolumnconstraint'
class PathColumnConstraint(ColumnConstraintKind):
1938class PathColumnConstraint(ColumnConstraintKind):
1939    pass
key = 'pathcolumnconstraint'
class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
1943class ProjectionPolicyColumnConstraint(ColumnConstraintKind):
1944    pass
key = 'projectionpolicycolumnconstraint'
class ComputedColumnConstraint(ColumnConstraintKind):
1949class ComputedColumnConstraint(ColumnConstraintKind):
1950    arg_types = {"this": True, "persisted": False, "not_null": False}
arg_types = {'this': True, 'persisted': False, 'not_null': False}
key = 'computedcolumnconstraint'
class Constraint(Expression):
1953class Constraint(Expression):
1954    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'constraint'
class Delete(DML):
1957class Delete(DML):
1958    arg_types = {
1959        "with": False,
1960        "this": False,
1961        "using": False,
1962        "where": False,
1963        "returning": False,
1964        "limit": False,
1965        "tables": False,  # Multiple-Table Syntax (MySQL)
1966        "cluster": False,  # Clickhouse
1967    }
1968
1969    def delete(
1970        self,
1971        table: ExpOrStr,
1972        dialect: DialectType = None,
1973        copy: bool = True,
1974        **opts,
1975    ) -> Delete:
1976        """
1977        Create a DELETE expression or replace the table on an existing DELETE expression.
1978
1979        Example:
1980            >>> delete("tbl").sql()
1981            'DELETE FROM tbl'
1982
1983        Args:
1984            table: the table from which to delete.
1985            dialect: the dialect used to parse the input expression.
1986            copy: if `False`, modify this expression instance in-place.
1987            opts: other options to use to parse the input expressions.
1988
1989        Returns:
1990            Delete: the modified expression.
1991        """
1992        return _apply_builder(
1993            expression=table,
1994            instance=self,
1995            arg="this",
1996            dialect=dialect,
1997            into=Table,
1998            copy=copy,
1999            **opts,
2000        )
2001
2002    def where(
2003        self,
2004        *expressions: t.Optional[ExpOrStr],
2005        append: bool = True,
2006        dialect: DialectType = None,
2007        copy: bool = True,
2008        **opts,
2009    ) -> Delete:
2010        """
2011        Append to or set the WHERE expressions.
2012
2013        Example:
2014            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
2015            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
2016
2017        Args:
2018            *expressions: the SQL code strings to parse.
2019                If an `Expression` instance is passed, it will be used as-is.
2020                Multiple expressions are combined with an AND operator.
2021            append: if `True`, AND the new expressions to any existing expression.
2022                Otherwise, this resets the expression.
2023            dialect: the dialect used to parse the input expressions.
2024            copy: if `False`, modify this expression instance in-place.
2025            opts: other options to use to parse the input expressions.
2026
2027        Returns:
2028            Delete: the modified expression.
2029        """
2030        return _apply_conjunction_builder(
2031            *expressions,
2032            instance=self,
2033            arg="where",
2034            append=append,
2035            into=Where,
2036            dialect=dialect,
2037            copy=copy,
2038            **opts,
2039        )
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:
1969    def delete(
1970        self,
1971        table: ExpOrStr,
1972        dialect: DialectType = None,
1973        copy: bool = True,
1974        **opts,
1975    ) -> Delete:
1976        """
1977        Create a DELETE expression or replace the table on an existing DELETE expression.
1978
1979        Example:
1980            >>> delete("tbl").sql()
1981            'DELETE FROM tbl'
1982
1983        Args:
1984            table: the table from which to delete.
1985            dialect: the dialect used to parse the input expression.
1986            copy: if `False`, modify this expression instance in-place.
1987            opts: other options to use to parse the input expressions.
1988
1989        Returns:
1990            Delete: the modified expression.
1991        """
1992        return _apply_builder(
1993            expression=table,
1994            instance=self,
1995            arg="this",
1996            dialect=dialect,
1997            into=Table,
1998            copy=copy,
1999            **opts,
2000        )

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:
2002    def where(
2003        self,
2004        *expressions: t.Optional[ExpOrStr],
2005        append: bool = True,
2006        dialect: DialectType = None,
2007        copy: bool = True,
2008        **opts,
2009    ) -> Delete:
2010        """
2011        Append to or set the WHERE expressions.
2012
2013        Example:
2014            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
2015            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
2016
2017        Args:
2018            *expressions: the SQL code strings to parse.
2019                If an `Expression` instance is passed, it will be used as-is.
2020                Multiple expressions are combined with an AND operator.
2021            append: if `True`, AND the new expressions to any existing expression.
2022                Otherwise, this resets the expression.
2023            dialect: the dialect used to parse the input expressions.
2024            copy: if `False`, modify this expression instance in-place.
2025            opts: other options to use to parse the input expressions.
2026
2027        Returns:
2028            Delete: the modified expression.
2029        """
2030        return _apply_conjunction_builder(
2031            *expressions,
2032            instance=self,
2033            arg="where",
2034            append=append,
2035            into=Where,
2036            dialect=dialect,
2037            copy=copy,
2038            **opts,
2039        )

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):
2042class Drop(Expression):
2043    arg_types = {
2044        "this": False,
2045        "kind": False,
2046        "expressions": False,
2047        "exists": False,
2048        "temporary": False,
2049        "materialized": False,
2050        "cascade": False,
2051        "constraints": False,
2052        "purge": False,
2053        "cluster": False,
2054        "concurrently": False,
2055    }
2056
2057    @property
2058    def kind(self) -> t.Optional[str]:
2059        kind = self.args.get("kind")
2060        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]
2057    @property
2058    def kind(self) -> t.Optional[str]:
2059        kind = self.args.get("kind")
2060        return kind and kind.upper()
key = 'drop'
class Filter(Expression):
2063class Filter(Expression):
2064    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'filter'
class Check(Expression):
2067class Check(Expression):
2068    pass
key = 'check'
class Changes(Expression):
2071class Changes(Expression):
2072    arg_types = {"information": True, "at_before": False, "end": False}
arg_types = {'information': True, 'at_before': False, 'end': False}
key = 'changes'
class Connect(Expression):
2076class Connect(Expression):
2077    arg_types = {"start": False, "connect": True, "nocycle": False}
arg_types = {'start': False, 'connect': True, 'nocycle': False}
key = 'connect'
class CopyParameter(Expression):
2080class CopyParameter(Expression):
2081    arg_types = {"this": True, "expression": False, "expressions": False}
arg_types = {'this': True, 'expression': False, 'expressions': False}
key = 'copyparameter'
class Copy(DML):
2084class Copy(DML):
2085    arg_types = {
2086        "this": True,
2087        "kind": True,
2088        "files": True,
2089        "credentials": False,
2090        "format": False,
2091        "params": False,
2092    }
arg_types = {'this': True, 'kind': True, 'files': True, 'credentials': False, 'format': False, 'params': False}
key = 'copy'
class Credentials(Expression):
2095class Credentials(Expression):
2096    arg_types = {
2097        "credentials": False,
2098        "encryption": False,
2099        "storage": False,
2100        "iam_role": False,
2101        "region": False,
2102    }
arg_types = {'credentials': False, 'encryption': False, 'storage': False, 'iam_role': False, 'region': False}
key = 'credentials'
class Prior(Expression):
2105class Prior(Expression):
2106    pass
key = 'prior'
class Directory(Expression):
2109class Directory(Expression):
2110    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
2111    arg_types = {"this": True, "local": False, "row_format": False}
arg_types = {'this': True, 'local': False, 'row_format': False}
key = 'directory'
class ForeignKey(Expression):
2114class ForeignKey(Expression):
2115    arg_types = {
2116        "expressions": True,
2117        "reference": False,
2118        "delete": False,
2119        "update": False,
2120    }
arg_types = {'expressions': True, 'reference': False, 'delete': False, 'update': False}
key = 'foreignkey'
class ColumnPrefix(Expression):
2123class ColumnPrefix(Expression):
2124    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'columnprefix'
class PrimaryKey(Expression):
2127class PrimaryKey(Expression):
2128    arg_types = {"expressions": True, "options": False}
arg_types = {'expressions': True, 'options': False}
key = 'primarykey'
class Into(Expression):
2133class Into(Expression):
2134    arg_types = {
2135        "this": False,
2136        "temporary": False,
2137        "unlogged": False,
2138        "bulk_collect": False,
2139        "expressions": False,
2140    }
arg_types = {'this': False, 'temporary': False, 'unlogged': False, 'bulk_collect': False, 'expressions': False}
key = 'into'
class From(Expression):
2143class From(Expression):
2144    @property
2145    def name(self) -> str:
2146        return self.this.name
2147
2148    @property
2149    def alias_or_name(self) -> str:
2150        return self.this.alias_or_name
name: str
2144    @property
2145    def name(self) -> str:
2146        return self.this.name
alias_or_name: str
2148    @property
2149    def alias_or_name(self) -> str:
2150        return self.this.alias_or_name
key = 'from'
class Having(Expression):
2153class Having(Expression):
2154    pass
key = 'having'
class Hint(Expression):
2157class Hint(Expression):
2158    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'hint'
class JoinHint(Expression):
2161class JoinHint(Expression):
2162    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'joinhint'
class Identifier(Expression):
2165class Identifier(Expression):
2166    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
2167
2168    @property
2169    def quoted(self) -> bool:
2170        return bool(self.args.get("quoted"))
2171
2172    @property
2173    def hashable_args(self) -> t.Any:
2174        return (self.this, self.quoted)
2175
2176    @property
2177    def output_name(self) -> str:
2178        return self.name
arg_types = {'this': True, 'quoted': False, 'global': False, 'temporary': False}
quoted: bool
2168    @property
2169    def quoted(self) -> bool:
2170        return bool(self.args.get("quoted"))
hashable_args: Any
2172    @property
2173    def hashable_args(self) -> t.Any:
2174        return (self.this, self.quoted)
output_name: str
2176    @property
2177    def output_name(self) -> str:
2178        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):
2182class Opclass(Expression):
2183    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'opclass'
class Index(Expression):
2186class Index(Expression):
2187    arg_types = {
2188        "this": False,
2189        "table": False,
2190        "unique": False,
2191        "primary": False,
2192        "amp": False,  # teradata
2193        "params": False,
2194    }
arg_types = {'this': False, 'table': False, 'unique': False, 'primary': False, 'amp': False, 'params': False}
key = 'index'
class IndexParameters(Expression):
2197class IndexParameters(Expression):
2198    arg_types = {
2199        "using": False,
2200        "include": False,
2201        "columns": False,
2202        "with_storage": False,
2203        "partition_by": False,
2204        "tablespace": False,
2205        "where": False,
2206        "on": False,
2207    }
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):
2210class Insert(DDL, DML):
2211    arg_types = {
2212        "hint": False,
2213        "with": False,
2214        "is_function": False,
2215        "this": False,
2216        "expression": False,
2217        "conflict": False,
2218        "returning": False,
2219        "overwrite": False,
2220        "exists": False,
2221        "alternative": False,
2222        "where": False,
2223        "ignore": False,
2224        "by_name": False,
2225        "stored": False,
2226        "partition": False,
2227        "settings": False,
2228        "source": False,
2229    }
2230
2231    def with_(
2232        self,
2233        alias: ExpOrStr,
2234        as_: ExpOrStr,
2235        recursive: t.Optional[bool] = None,
2236        materialized: t.Optional[bool] = None,
2237        append: bool = True,
2238        dialect: DialectType = None,
2239        copy: bool = True,
2240        **opts,
2241    ) -> Insert:
2242        """
2243        Append to or set the common table expressions.
2244
2245        Example:
2246            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2247            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2248
2249        Args:
2250            alias: the SQL code string to parse as the table name.
2251                If an `Expression` instance is passed, this is used as-is.
2252            as_: the SQL code string to parse as the table expression.
2253                If an `Expression` instance is passed, it will be used as-is.
2254            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2255            materialized: set the MATERIALIZED part of the expression.
2256            append: if `True`, add to any existing expressions.
2257                Otherwise, this resets the expressions.
2258            dialect: the dialect used to parse the input expression.
2259            copy: if `False`, modify this expression instance in-place.
2260            opts: other options to use to parse the input expressions.
2261
2262        Returns:
2263            The modified expression.
2264        """
2265        return _apply_cte_builder(
2266            self,
2267            alias,
2268            as_,
2269            recursive=recursive,
2270            materialized=materialized,
2271            append=append,
2272            dialect=dialect,
2273            copy=copy,
2274            **opts,
2275        )
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:
2231    def with_(
2232        self,
2233        alias: ExpOrStr,
2234        as_: ExpOrStr,
2235        recursive: t.Optional[bool] = None,
2236        materialized: t.Optional[bool] = None,
2237        append: bool = True,
2238        dialect: DialectType = None,
2239        copy: bool = True,
2240        **opts,
2241    ) -> Insert:
2242        """
2243        Append to or set the common table expressions.
2244
2245        Example:
2246            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
2247            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
2248
2249        Args:
2250            alias: the SQL code string to parse as the table name.
2251                If an `Expression` instance is passed, this is used as-is.
2252            as_: the SQL code string to parse as the table expression.
2253                If an `Expression` instance is passed, it will be used as-is.
2254            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2255            materialized: set the MATERIALIZED part of the expression.
2256            append: if `True`, add to any existing expressions.
2257                Otherwise, this resets the expressions.
2258            dialect: the dialect used to parse the input expression.
2259            copy: if `False`, modify this expression instance in-place.
2260            opts: other options to use to parse the input expressions.
2261
2262        Returns:
2263            The modified expression.
2264        """
2265        return _apply_cte_builder(
2266            self,
2267            alias,
2268            as_,
2269            recursive=recursive,
2270            materialized=materialized,
2271            append=append,
2272            dialect=dialect,
2273            copy=copy,
2274            **opts,
2275        )

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):
2278class ConditionalInsert(Expression):
2279    arg_types = {"this": True, "expression": False, "else_": False}
arg_types = {'this': True, 'expression': False, 'else_': False}
key = 'conditionalinsert'
class MultitableInserts(Expression):
2282class MultitableInserts(Expression):
2283    arg_types = {"expressions": True, "kind": True, "source": True}
arg_types = {'expressions': True, 'kind': True, 'source': True}
key = 'multitableinserts'
class OnConflict(Expression):
2286class OnConflict(Expression):
2287    arg_types = {
2288        "duplicate": False,
2289        "expressions": False,
2290        "action": False,
2291        "conflict_keys": False,
2292        "constraint": False,
2293    }
arg_types = {'duplicate': False, 'expressions': False, 'action': False, 'conflict_keys': False, 'constraint': False}
key = 'onconflict'
class OnCondition(Expression):
2296class OnCondition(Expression):
2297    arg_types = {"error": False, "empty": False, "null": False}
arg_types = {'error': False, 'empty': False, 'null': False}
key = 'oncondition'
class Returning(Expression):
2300class Returning(Expression):
2301    arg_types = {"expressions": True, "into": False}
arg_types = {'expressions': True, 'into': False}
key = 'returning'
class Introducer(Expression):
2305class Introducer(Expression):
2306    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'introducer'
class National(Expression):
2310class National(Expression):
2311    pass
key = 'national'
class LoadData(Expression):
2314class LoadData(Expression):
2315    arg_types = {
2316        "this": True,
2317        "local": False,
2318        "overwrite": False,
2319        "inpath": True,
2320        "partition": False,
2321        "input_format": False,
2322        "serde": False,
2323    }
arg_types = {'this': True, 'local': False, 'overwrite': False, 'inpath': True, 'partition': False, 'input_format': False, 'serde': False}
key = 'loaddata'
class Partition(Expression):
2326class Partition(Expression):
2327    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'partition'
class PartitionRange(Expression):
2330class PartitionRange(Expression):
2331    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionrange'
class PartitionId(Expression):
2335class PartitionId(Expression):
2336    pass
key = 'partitionid'
class Fetch(Expression):
2339class Fetch(Expression):
2340    arg_types = {
2341        "direction": False,
2342        "count": False,
2343        "percent": False,
2344        "with_ties": False,
2345    }
arg_types = {'direction': False, 'count': False, 'percent': False, 'with_ties': False}
key = 'fetch'
class Grant(Expression):
2348class Grant(Expression):
2349    arg_types = {
2350        "privileges": True,
2351        "kind": False,
2352        "securable": True,
2353        "principals": True,
2354        "grant_option": False,
2355    }
arg_types = {'privileges': True, 'kind': False, 'securable': True, 'principals': True, 'grant_option': False}
key = 'grant'
class Group(Expression):
2358class Group(Expression):
2359    arg_types = {
2360        "expressions": False,
2361        "grouping_sets": False,
2362        "cube": False,
2363        "rollup": False,
2364        "totals": False,
2365        "all": False,
2366    }
arg_types = {'expressions': False, 'grouping_sets': False, 'cube': False, 'rollup': False, 'totals': False, 'all': False}
key = 'group'
class Cube(Expression):
2369class Cube(Expression):
2370    arg_types = {"expressions": False}
arg_types = {'expressions': False}
key = 'cube'
class Rollup(Expression):
2373class Rollup(Expression):
2374    arg_types = {"expressions": False}
arg_types = {'expressions': False}
key = 'rollup'
class GroupingSets(Expression):
2377class GroupingSets(Expression):
2378    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'groupingsets'
class Lambda(Expression):
2381class Lambda(Expression):
2382    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'lambda'
class Limit(Expression):
2385class Limit(Expression):
2386    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):
2389class Literal(Condition):
2390    arg_types = {"this": True, "is_string": True}
2391
2392    @property
2393    def hashable_args(self) -> t.Any:
2394        return (self.this, self.args.get("is_string"))
2395
2396    @classmethod
2397    def number(cls, number) -> Literal:
2398        return cls(this=str(number), is_string=False)
2399
2400    @classmethod
2401    def string(cls, string) -> Literal:
2402        return cls(this=str(string), is_string=True)
2403
2404    @property
2405    def output_name(self) -> str:
2406        return self.name
2407
2408    def to_py(self) -> int | str | Decimal:
2409        if self.is_number:
2410            try:
2411                return int(self.this)
2412            except ValueError:
2413                return Decimal(self.this)
2414        return self.this
arg_types = {'this': True, 'is_string': True}
hashable_args: Any
2392    @property
2393    def hashable_args(self) -> t.Any:
2394        return (self.this, self.args.get("is_string"))
@classmethod
def number(cls, number) -> Literal:
2396    @classmethod
2397    def number(cls, number) -> Literal:
2398        return cls(this=str(number), is_string=False)
@classmethod
def string(cls, string) -> Literal:
2400    @classmethod
2401    def string(cls, string) -> Literal:
2402        return cls(this=str(string), is_string=True)
output_name: str
2404    @property
2405    def output_name(self) -> str:
2406        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:
2408    def to_py(self) -> int | str | Decimal:
2409        if self.is_number:
2410            try:
2411                return int(self.this)
2412            except ValueError:
2413                return Decimal(self.this)
2414        return self.this

Returns a Python object equivalent of the SQL node.

key = 'literal'
class Join(Expression):
2417class Join(Expression):
2418    arg_types = {
2419        "this": True,
2420        "on": False,
2421        "side": False,
2422        "kind": False,
2423        "using": False,
2424        "method": False,
2425        "global": False,
2426        "hint": False,
2427        "match_condition": False,  # Snowflake
2428        "expressions": False,
2429    }
2430
2431    @property
2432    def method(self) -> str:
2433        return self.text("method").upper()
2434
2435    @property
2436    def kind(self) -> str:
2437        return self.text("kind").upper()
2438
2439    @property
2440    def side(self) -> str:
2441        return self.text("side").upper()
2442
2443    @property
2444    def hint(self) -> str:
2445        return self.text("hint").upper()
2446
2447    @property
2448    def alias_or_name(self) -> str:
2449        return self.this.alias_or_name
2450
2451    def on(
2452        self,
2453        *expressions: t.Optional[ExpOrStr],
2454        append: bool = True,
2455        dialect: DialectType = None,
2456        copy: bool = True,
2457        **opts,
2458    ) -> Join:
2459        """
2460        Append to or set the ON expressions.
2461
2462        Example:
2463            >>> import sqlglot
2464            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2465            'JOIN x ON y = 1'
2466
2467        Args:
2468            *expressions: the SQL code strings to parse.
2469                If an `Expression` instance is passed, it will be used as-is.
2470                Multiple expressions are combined with an AND operator.
2471            append: if `True`, AND the new expressions to any existing expression.
2472                Otherwise, this resets the expression.
2473            dialect: the dialect used to parse the input expressions.
2474            copy: if `False`, modify this expression instance in-place.
2475            opts: other options to use to parse the input expressions.
2476
2477        Returns:
2478            The modified Join expression.
2479        """
2480        join = _apply_conjunction_builder(
2481            *expressions,
2482            instance=self,
2483            arg="on",
2484            append=append,
2485            dialect=dialect,
2486            copy=copy,
2487            **opts,
2488        )
2489
2490        if join.kind == "CROSS":
2491            join.set("kind", None)
2492
2493        return join
2494
2495    def using(
2496        self,
2497        *expressions: t.Optional[ExpOrStr],
2498        append: bool = True,
2499        dialect: DialectType = None,
2500        copy: bool = True,
2501        **opts,
2502    ) -> Join:
2503        """
2504        Append to or set the USING expressions.
2505
2506        Example:
2507            >>> import sqlglot
2508            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2509            'JOIN x USING (foo, bla)'
2510
2511        Args:
2512            *expressions: the SQL code strings to parse.
2513                If an `Expression` instance is passed, it will be used as-is.
2514            append: if `True`, concatenate the new expressions to the existing "using" list.
2515                Otherwise, this resets the expression.
2516            dialect: the dialect used to parse the input expressions.
2517            copy: if `False`, modify this expression instance in-place.
2518            opts: other options to use to parse the input expressions.
2519
2520        Returns:
2521            The modified Join expression.
2522        """
2523        join = _apply_list_builder(
2524            *expressions,
2525            instance=self,
2526            arg="using",
2527            append=append,
2528            dialect=dialect,
2529            copy=copy,
2530            **opts,
2531        )
2532
2533        if join.kind == "CROSS":
2534            join.set("kind", None)
2535
2536        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
2431    @property
2432    def method(self) -> str:
2433        return self.text("method").upper()
kind: str
2435    @property
2436    def kind(self) -> str:
2437        return self.text("kind").upper()
side: str
2439    @property
2440    def side(self) -> str:
2441        return self.text("side").upper()
hint: str
2443    @property
2444    def hint(self) -> str:
2445        return self.text("hint").upper()
alias_or_name: str
2447    @property
2448    def alias_or_name(self) -> str:
2449        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:
2451    def on(
2452        self,
2453        *expressions: t.Optional[ExpOrStr],
2454        append: bool = True,
2455        dialect: DialectType = None,
2456        copy: bool = True,
2457        **opts,
2458    ) -> Join:
2459        """
2460        Append to or set the ON expressions.
2461
2462        Example:
2463            >>> import sqlglot
2464            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
2465            'JOIN x ON y = 1'
2466
2467        Args:
2468            *expressions: the SQL code strings to parse.
2469                If an `Expression` instance is passed, it will be used as-is.
2470                Multiple expressions are combined with an AND operator.
2471            append: if `True`, AND the new expressions to any existing expression.
2472                Otherwise, this resets the expression.
2473            dialect: the dialect used to parse the input expressions.
2474            copy: if `False`, modify this expression instance in-place.
2475            opts: other options to use to parse the input expressions.
2476
2477        Returns:
2478            The modified Join expression.
2479        """
2480        join = _apply_conjunction_builder(
2481            *expressions,
2482            instance=self,
2483            arg="on",
2484            append=append,
2485            dialect=dialect,
2486            copy=copy,
2487            **opts,
2488        )
2489
2490        if join.kind == "CROSS":
2491            join.set("kind", None)
2492
2493        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:
2495    def using(
2496        self,
2497        *expressions: t.Optional[ExpOrStr],
2498        append: bool = True,
2499        dialect: DialectType = None,
2500        copy: bool = True,
2501        **opts,
2502    ) -> Join:
2503        """
2504        Append to or set the USING expressions.
2505
2506        Example:
2507            >>> import sqlglot
2508            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
2509            'JOIN x USING (foo, bla)'
2510
2511        Args:
2512            *expressions: the SQL code strings to parse.
2513                If an `Expression` instance is passed, it will be used as-is.
2514            append: if `True`, concatenate the new expressions to the existing "using" list.
2515                Otherwise, this resets the expression.
2516            dialect: the dialect used to parse the input expressions.
2517            copy: if `False`, modify this expression instance in-place.
2518            opts: other options to use to parse the input expressions.
2519
2520        Returns:
2521            The modified Join expression.
2522        """
2523        join = _apply_list_builder(
2524            *expressions,
2525            instance=self,
2526            arg="using",
2527            append=append,
2528            dialect=dialect,
2529            copy=copy,
2530            **opts,
2531        )
2532
2533        if join.kind == "CROSS":
2534            join.set("kind", None)
2535
2536        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):
2539class Lateral(UDTF):
2540    arg_types = {
2541        "this": True,
2542        "view": False,
2543        "outer": False,
2544        "alias": False,
2545        "cross_apply": False,  # True -> CROSS APPLY, False -> OUTER APPLY
2546    }
arg_types = {'this': True, 'view': False, 'outer': False, 'alias': False, 'cross_apply': False}
key = 'lateral'
class MatchRecognizeMeasure(Expression):
2549class MatchRecognizeMeasure(Expression):
2550    arg_types = {
2551        "this": True,
2552        "window_frame": False,
2553    }
arg_types = {'this': True, 'window_frame': False}
key = 'matchrecognizemeasure'
class MatchRecognize(Expression):
2556class MatchRecognize(Expression):
2557    arg_types = {
2558        "partition_by": False,
2559        "order": False,
2560        "measures": False,
2561        "rows": False,
2562        "after": False,
2563        "pattern": False,
2564        "define": False,
2565        "alias": False,
2566    }
arg_types = {'partition_by': False, 'order': False, 'measures': False, 'rows': False, 'after': False, 'pattern': False, 'define': False, 'alias': False}
key = 'matchrecognize'
class Final(Expression):
2571class Final(Expression):
2572    pass
key = 'final'
class Offset(Expression):
2575class Offset(Expression):
2576    arg_types = {"this": False, "expression": True, "expressions": False}
arg_types = {'this': False, 'expression': True, 'expressions': False}
key = 'offset'
class Order(Expression):
2579class Order(Expression):
2580    arg_types = {"this": False, "expressions": True, "siblings": False}
arg_types = {'this': False, 'expressions': True, 'siblings': False}
key = 'order'
class WithFill(Expression):
2584class WithFill(Expression):
2585    arg_types = {
2586        "from": False,
2587        "to": False,
2588        "step": False,
2589        "interpolate": False,
2590    }
arg_types = {'from': False, 'to': False, 'step': False, 'interpolate': False}
key = 'withfill'
class Cluster(Order):
2595class Cluster(Order):
2596    pass
key = 'cluster'
class Distribute(Order):
2599class Distribute(Order):
2600    pass
key = 'distribute'
class Sort(Order):
2603class Sort(Order):
2604    pass
key = 'sort'
class Ordered(Expression):
2607class Ordered(Expression):
2608    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):
2611class Property(Expression):
2612    arg_types = {"this": True, "value": True}
arg_types = {'this': True, 'value': True}
key = 'property'
class GrantPrivilege(Expression):
2615class GrantPrivilege(Expression):
2616    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'grantprivilege'
class GrantPrincipal(Expression):
2619class GrantPrincipal(Expression):
2620    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'grantprincipal'
class AllowedValuesProperty(Expression):
2623class AllowedValuesProperty(Expression):
2624    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'allowedvaluesproperty'
class AlgorithmProperty(Property):
2627class AlgorithmProperty(Property):
2628    arg_types = {"this": True}
arg_types = {'this': True}
key = 'algorithmproperty'
class AutoIncrementProperty(Property):
2631class AutoIncrementProperty(Property):
2632    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autoincrementproperty'
class AutoRefreshProperty(Property):
2636class AutoRefreshProperty(Property):
2637    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autorefreshproperty'
class BackupProperty(Property):
2640class BackupProperty(Property):
2641    arg_types = {"this": True}
arg_types = {'this': True}
key = 'backupproperty'
class BlockCompressionProperty(Property):
2644class BlockCompressionProperty(Property):
2645    arg_types = {
2646        "autotemp": False,
2647        "always": False,
2648        "default": False,
2649        "manual": False,
2650        "never": False,
2651    }
arg_types = {'autotemp': False, 'always': False, 'default': False, 'manual': False, 'never': False}
key = 'blockcompressionproperty'
class CharacterSetProperty(Property):
2654class CharacterSetProperty(Property):
2655    arg_types = {"this": True, "default": True}
arg_types = {'this': True, 'default': True}
key = 'charactersetproperty'
class ChecksumProperty(Property):
2658class ChecksumProperty(Property):
2659    arg_types = {"on": False, "default": False}
arg_types = {'on': False, 'default': False}
key = 'checksumproperty'
class CollateProperty(Property):
2662class CollateProperty(Property):
2663    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'collateproperty'
class CopyGrantsProperty(Property):
2666class CopyGrantsProperty(Property):
2667    arg_types = {}
arg_types = {}
key = 'copygrantsproperty'
class DataBlocksizeProperty(Property):
2670class DataBlocksizeProperty(Property):
2671    arg_types = {
2672        "size": False,
2673        "units": False,
2674        "minimum": False,
2675        "maximum": False,
2676        "default": False,
2677    }
arg_types = {'size': False, 'units': False, 'minimum': False, 'maximum': False, 'default': False}
key = 'datablocksizeproperty'
class DataDeletionProperty(Property):
2680class DataDeletionProperty(Property):
2681    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):
2684class DefinerProperty(Property):
2685    arg_types = {"this": True}
arg_types = {'this': True}
key = 'definerproperty'
class DistKeyProperty(Property):
2688class DistKeyProperty(Property):
2689    arg_types = {"this": True}
arg_types = {'this': True}
key = 'distkeyproperty'
class DistributedByProperty(Property):
2694class DistributedByProperty(Property):
2695    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):
2698class DistStyleProperty(Property):
2699    arg_types = {"this": True}
arg_types = {'this': True}
key = 'diststyleproperty'
class DuplicateKeyProperty(Property):
2702class DuplicateKeyProperty(Property):
2703    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'duplicatekeyproperty'
class EngineProperty(Property):
2706class EngineProperty(Property):
2707    arg_types = {"this": True}
arg_types = {'this': True}
key = 'engineproperty'
class HeapProperty(Property):
2710class HeapProperty(Property):
2711    arg_types = {}
arg_types = {}
key = 'heapproperty'
class ToTableProperty(Property):
2714class ToTableProperty(Property):
2715    arg_types = {"this": True}
arg_types = {'this': True}
key = 'totableproperty'
class ExecuteAsProperty(Property):
2718class ExecuteAsProperty(Property):
2719    arg_types = {"this": True}
arg_types = {'this': True}
key = 'executeasproperty'
class ExternalProperty(Property):
2722class ExternalProperty(Property):
2723    arg_types = {"this": False}
arg_types = {'this': False}
key = 'externalproperty'
class FallbackProperty(Property):
2726class FallbackProperty(Property):
2727    arg_types = {"no": True, "protection": False}
arg_types = {'no': True, 'protection': False}
key = 'fallbackproperty'
class FileFormatProperty(Property):
2730class FileFormatProperty(Property):
2731    arg_types = {"this": True}
arg_types = {'this': True}
key = 'fileformatproperty'
class FreespaceProperty(Property):
2734class FreespaceProperty(Property):
2735    arg_types = {"this": True, "percent": False}
arg_types = {'this': True, 'percent': False}
key = 'freespaceproperty'
class GlobalProperty(Property):
2738class GlobalProperty(Property):
2739    arg_types = {}
arg_types = {}
key = 'globalproperty'
class IcebergProperty(Property):
2742class IcebergProperty(Property):
2743    arg_types = {}
arg_types = {}
key = 'icebergproperty'
class InheritsProperty(Property):
2746class InheritsProperty(Property):
2747    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'inheritsproperty'
class InputModelProperty(Property):
2750class InputModelProperty(Property):
2751    arg_types = {"this": True}
arg_types = {'this': True}
key = 'inputmodelproperty'
class OutputModelProperty(Property):
2754class OutputModelProperty(Property):
2755    arg_types = {"this": True}
arg_types = {'this': True}
key = 'outputmodelproperty'
class IsolatedLoadingProperty(Property):
2758class IsolatedLoadingProperty(Property):
2759    arg_types = {"no": False, "concurrent": False, "target": False}
arg_types = {'no': False, 'concurrent': False, 'target': False}
key = 'isolatedloadingproperty'
class JournalProperty(Property):
2762class JournalProperty(Property):
2763    arg_types = {
2764        "no": False,
2765        "dual": False,
2766        "before": False,
2767        "local": False,
2768        "after": False,
2769    }
arg_types = {'no': False, 'dual': False, 'before': False, 'local': False, 'after': False}
key = 'journalproperty'
class LanguageProperty(Property):
2772class LanguageProperty(Property):
2773    arg_types = {"this": True}
arg_types = {'this': True}
key = 'languageproperty'
class ClusteredByProperty(Property):
2777class ClusteredByProperty(Property):
2778    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
arg_types = {'expressions': True, 'sorted_by': False, 'buckets': True}
key = 'clusteredbyproperty'
class DictProperty(Property):
2781class DictProperty(Property):
2782    arg_types = {"this": True, "kind": True, "settings": False}
arg_types = {'this': True, 'kind': True, 'settings': False}
key = 'dictproperty'
class DictSubProperty(Property):
2785class DictSubProperty(Property):
2786    pass
key = 'dictsubproperty'
class DictRange(Property):
2789class DictRange(Property):
2790    arg_types = {"this": True, "min": True, "max": True}
arg_types = {'this': True, 'min': True, 'max': True}
key = 'dictrange'
class DynamicProperty(Property):
2793class DynamicProperty(Property):
2794    arg_types = {}
arg_types = {}
key = 'dynamicproperty'
class OnCluster(Property):
2799class OnCluster(Property):
2800    arg_types = {"this": True}
arg_types = {'this': True}
key = 'oncluster'
class EmptyProperty(Property):
2804class EmptyProperty(Property):
2805    arg_types = {}
arg_types = {}
key = 'emptyproperty'
class LikeProperty(Property):
2808class LikeProperty(Property):
2809    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'likeproperty'
class LocationProperty(Property):
2812class LocationProperty(Property):
2813    arg_types = {"this": True}
arg_types = {'this': True}
key = 'locationproperty'
class LockProperty(Property):
2816class LockProperty(Property):
2817    arg_types = {"this": True}
arg_types = {'this': True}
key = 'lockproperty'
class LockingProperty(Property):
2820class LockingProperty(Property):
2821    arg_types = {
2822        "this": False,
2823        "kind": True,
2824        "for_or_in": False,
2825        "lock_type": True,
2826        "override": False,
2827    }
arg_types = {'this': False, 'kind': True, 'for_or_in': False, 'lock_type': True, 'override': False}
key = 'lockingproperty'
class LogProperty(Property):
2830class LogProperty(Property):
2831    arg_types = {"no": True}
arg_types = {'no': True}
key = 'logproperty'
class MaterializedProperty(Property):
2834class MaterializedProperty(Property):
2835    arg_types = {"this": False}
arg_types = {'this': False}
key = 'materializedproperty'
class MergeBlockRatioProperty(Property):
2838class MergeBlockRatioProperty(Property):
2839    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):
2842class NoPrimaryIndexProperty(Property):
2843    arg_types = {}
arg_types = {}
key = 'noprimaryindexproperty'
class OnProperty(Property):
2846class OnProperty(Property):
2847    arg_types = {"this": True}
arg_types = {'this': True}
key = 'onproperty'
class OnCommitProperty(Property):
2850class OnCommitProperty(Property):
2851    arg_types = {"delete": False}
arg_types = {'delete': False}
key = 'oncommitproperty'
class PartitionedByProperty(Property):
2854class PartitionedByProperty(Property):
2855    arg_types = {"this": True}
arg_types = {'this': True}
key = 'partitionedbyproperty'
class PartitionBoundSpec(Expression):
2859class PartitionBoundSpec(Expression):
2860    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
2861    arg_types = {
2862        "this": False,
2863        "expression": False,
2864        "from_expressions": False,
2865        "to_expressions": False,
2866    }
arg_types = {'this': False, 'expression': False, 'from_expressions': False, 'to_expressions': False}
key = 'partitionboundspec'
class PartitionedOfProperty(Property):
2869class PartitionedOfProperty(Property):
2870    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
2871    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionedofproperty'
class StreamingTableProperty(Property):
2874class StreamingTableProperty(Property):
2875    arg_types = {}
arg_types = {}
key = 'streamingtableproperty'
class RemoteWithConnectionModelProperty(Property):
2878class RemoteWithConnectionModelProperty(Property):
2879    arg_types = {"this": True}
arg_types = {'this': True}
key = 'remotewithconnectionmodelproperty'
class ReturnsProperty(Property):
2882class ReturnsProperty(Property):
2883    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):
2886class StrictProperty(Property):
2887    arg_types = {}
arg_types = {}
key = 'strictproperty'
class RowFormatProperty(Property):
2890class RowFormatProperty(Property):
2891    arg_types = {"this": True}
arg_types = {'this': True}
key = 'rowformatproperty'
class RowFormatDelimitedProperty(Property):
2894class RowFormatDelimitedProperty(Property):
2895    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
2896    arg_types = {
2897        "fields": False,
2898        "escaped": False,
2899        "collection_items": False,
2900        "map_keys": False,
2901        "lines": False,
2902        "null": False,
2903        "serde": False,
2904    }
arg_types = {'fields': False, 'escaped': False, 'collection_items': False, 'map_keys': False, 'lines': False, 'null': False, 'serde': False}
key = 'rowformatdelimitedproperty'
class RowFormatSerdeProperty(Property):
2907class RowFormatSerdeProperty(Property):
2908    arg_types = {"this": True, "serde_properties": False}
arg_types = {'this': True, 'serde_properties': False}
key = 'rowformatserdeproperty'
class QueryTransform(Expression):
2912class QueryTransform(Expression):
2913    arg_types = {
2914        "expressions": True,
2915        "command_script": True,
2916        "schema": False,
2917        "row_format_before": False,
2918        "record_writer": False,
2919        "row_format_after": False,
2920        "record_reader": False,
2921    }
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):
2924class SampleProperty(Property):
2925    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sampleproperty'
class SecurityProperty(Property):
2929class SecurityProperty(Property):
2930    arg_types = {"this": True}
arg_types = {'this': True}
key = 'securityproperty'
class SchemaCommentProperty(Property):
2933class SchemaCommentProperty(Property):
2934    arg_types = {"this": True}
arg_types = {'this': True}
key = 'schemacommentproperty'
class SerdeProperties(Property):
2937class SerdeProperties(Property):
2938    arg_types = {"expressions": True, "with": False}
arg_types = {'expressions': True, 'with': False}
key = 'serdeproperties'
class SetProperty(Property):
2941class SetProperty(Property):
2942    arg_types = {"multi": True}
arg_types = {'multi': True}
key = 'setproperty'
class SharingProperty(Property):
2945class SharingProperty(Property):
2946    arg_types = {"this": False}
arg_types = {'this': False}
key = 'sharingproperty'
class SetConfigProperty(Property):
2949class SetConfigProperty(Property):
2950    arg_types = {"this": True}
arg_types = {'this': True}
key = 'setconfigproperty'
class SettingsProperty(Property):
2953class SettingsProperty(Property):
2954    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'settingsproperty'
class SortKeyProperty(Property):
2957class SortKeyProperty(Property):
2958    arg_types = {"this": True, "compound": False}
arg_types = {'this': True, 'compound': False}
key = 'sortkeyproperty'
class SqlReadWriteProperty(Property):
2961class SqlReadWriteProperty(Property):
2962    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sqlreadwriteproperty'
class SqlSecurityProperty(Property):
2965class SqlSecurityProperty(Property):
2966    arg_types = {"definer": True}
arg_types = {'definer': True}
key = 'sqlsecurityproperty'
class StabilityProperty(Property):
2969class StabilityProperty(Property):
2970    arg_types = {"this": True}
arg_types = {'this': True}
key = 'stabilityproperty'
class TemporaryProperty(Property):
2973class TemporaryProperty(Property):
2974    arg_types = {"this": False}
arg_types = {'this': False}
key = 'temporaryproperty'
class SecureProperty(Property):
2977class SecureProperty(Property):
2978    arg_types = {}
arg_types = {}
key = 'secureproperty'
class Tags(ColumnConstraintKind, Property):
2982class Tags(ColumnConstraintKind, Property):
2983    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'tags'
class TransformModelProperty(Property):
2986class TransformModelProperty(Property):
2987    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'transformmodelproperty'
class TransientProperty(Property):
2990class TransientProperty(Property):
2991    arg_types = {"this": False}
arg_types = {'this': False}
key = 'transientproperty'
class UnloggedProperty(Property):
2994class UnloggedProperty(Property):
2995    arg_types = {}
arg_types = {}
key = 'unloggedproperty'
class ViewAttributeProperty(Property):
2999class ViewAttributeProperty(Property):
3000    arg_types = {"this": True}
arg_types = {'this': True}
key = 'viewattributeproperty'
class VolatileProperty(Property):
3003class VolatileProperty(Property):
3004    arg_types = {"this": False}
arg_types = {'this': False}
key = 'volatileproperty'
class WithDataProperty(Property):
3007class WithDataProperty(Property):
3008    arg_types = {"no": True, "statistics": False}
arg_types = {'no': True, 'statistics': False}
key = 'withdataproperty'
class WithJournalTableProperty(Property):
3011class WithJournalTableProperty(Property):
3012    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withjournaltableproperty'
class WithSchemaBindingProperty(Property):
3015class WithSchemaBindingProperty(Property):
3016    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withschemabindingproperty'
class WithSystemVersioningProperty(Property):
3019class WithSystemVersioningProperty(Property):
3020    arg_types = {
3021        "on": False,
3022        "this": False,
3023        "data_consistency": False,
3024        "retention_period": False,
3025        "with": True,
3026    }
arg_types = {'on': False, 'this': False, 'data_consistency': False, 'retention_period': False, 'with': True}
key = 'withsystemversioningproperty'
class WithProcedureOptions(Property):
3029class WithProcedureOptions(Property):
3030    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withprocedureoptions'
class EncodeProperty(Property):
3033class EncodeProperty(Property):
3034    arg_types = {"this": True, "properties": False, "key": False}
arg_types = {'this': True, 'properties': False, 'key': False}
key = 'encodeproperty'
class IncludeProperty(Property):
3037class IncludeProperty(Property):
3038    arg_types = {"this": True, "alias": False, "column_def": False}
arg_types = {'this': True, 'alias': False, 'column_def': False}
key = 'includeproperty'
class Properties(Expression):
3041class Properties(Expression):
3042    arg_types = {"expressions": True}
3043
3044    NAME_TO_PROPERTY = {
3045        "ALGORITHM": AlgorithmProperty,
3046        "AUTO_INCREMENT": AutoIncrementProperty,
3047        "CHARACTER SET": CharacterSetProperty,
3048        "CLUSTERED_BY": ClusteredByProperty,
3049        "COLLATE": CollateProperty,
3050        "COMMENT": SchemaCommentProperty,
3051        "DEFINER": DefinerProperty,
3052        "DISTKEY": DistKeyProperty,
3053        "DISTRIBUTED_BY": DistributedByProperty,
3054        "DISTSTYLE": DistStyleProperty,
3055        "ENGINE": EngineProperty,
3056        "EXECUTE AS": ExecuteAsProperty,
3057        "FORMAT": FileFormatProperty,
3058        "LANGUAGE": LanguageProperty,
3059        "LOCATION": LocationProperty,
3060        "LOCK": LockProperty,
3061        "PARTITIONED_BY": PartitionedByProperty,
3062        "RETURNS": ReturnsProperty,
3063        "ROW_FORMAT": RowFormatProperty,
3064        "SORTKEY": SortKeyProperty,
3065        "ENCODE": EncodeProperty,
3066        "INCLUDE": IncludeProperty,
3067    }
3068
3069    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
3070
3071    # CREATE property locations
3072    # Form: schema specified
3073    #   create [POST_CREATE]
3074    #     table a [POST_NAME]
3075    #     (b int) [POST_SCHEMA]
3076    #     with ([POST_WITH])
3077    #     index (b) [POST_INDEX]
3078    #
3079    # Form: alias selection
3080    #   create [POST_CREATE]
3081    #     table a [POST_NAME]
3082    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
3083    #     index (c) [POST_INDEX]
3084    class Location(AutoName):
3085        POST_CREATE = auto()
3086        POST_NAME = auto()
3087        POST_SCHEMA = auto()
3088        POST_WITH = auto()
3089        POST_ALIAS = auto()
3090        POST_EXPRESSION = auto()
3091        POST_INDEX = auto()
3092        UNSUPPORTED = auto()
3093
3094    @classmethod
3095    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3096        expressions = []
3097        for key, value in properties_dict.items():
3098            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3099            if property_cls:
3100                expressions.append(property_cls(this=convert(value)))
3101            else:
3102                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3103
3104        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'>, 'ENCODE': <class 'EncodeProperty'>, 'INCLUDE': <class 'IncludeProperty'>}
PROPERTY_TO_NAME = {<class 'AlgorithmProperty'>: 'ALGORITHM', <class 'AutoIncrementProperty'>: 'AUTO_INCREMENT', <class 'CharacterSetProperty'>: 'CHARACTER SET', <class 'ClusteredByProperty'>: 'CLUSTERED_BY', <class 'CollateProperty'>: 'COLLATE', <class 'SchemaCommentProperty'>: 'COMMENT', <class 'DefinerProperty'>: 'DEFINER', <class 'DistKeyProperty'>: 'DISTKEY', <class 'DistributedByProperty'>: 'DISTRIBUTED_BY', <class 'DistStyleProperty'>: 'DISTSTYLE', <class 'EngineProperty'>: 'ENGINE', <class 'ExecuteAsProperty'>: 'EXECUTE AS', <class 'FileFormatProperty'>: 'FORMAT', <class 'LanguageProperty'>: 'LANGUAGE', <class 'LocationProperty'>: 'LOCATION', <class 'LockProperty'>: 'LOCK', <class 'PartitionedByProperty'>: 'PARTITIONED_BY', <class 'ReturnsProperty'>: 'RETURNS', <class 'RowFormatProperty'>: 'ROW_FORMAT', <class 'SortKeyProperty'>: 'SORTKEY', <class 'EncodeProperty'>: 'ENCODE', <class 'IncludeProperty'>: 'INCLUDE'}
@classmethod
def from_dict(cls, properties_dict: Dict) -> Properties:
3094    @classmethod
3095    def from_dict(cls, properties_dict: t.Dict) -> Properties:
3096        expressions = []
3097        for key, value in properties_dict.items():
3098            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
3099            if property_cls:
3100                expressions.append(property_cls(this=convert(value)))
3101            else:
3102                expressions.append(Property(this=Literal.string(key), value=convert(value)))
3103
3104        return cls(expressions=expressions)
key = 'properties'
class Properties.Location(sqlglot.helper.AutoName):
3084    class Location(AutoName):
3085        POST_CREATE = auto()
3086        POST_NAME = auto()
3087        POST_SCHEMA = auto()
3088        POST_WITH = auto()
3089        POST_ALIAS = auto()
3090        POST_EXPRESSION = auto()
3091        POST_INDEX = auto()
3092        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):
3107class Qualify(Expression):
3108    pass
key = 'qualify'
class InputOutputFormat(Expression):
3111class InputOutputFormat(Expression):
3112    arg_types = {"input_format": False, "output_format": False}
arg_types = {'input_format': False, 'output_format': False}
key = 'inputoutputformat'
class Return(Expression):
3116class Return(Expression):
3117    pass
key = 'return'
class Reference(Expression):
3120class Reference(Expression):
3121    arg_types = {"this": True, "expressions": False, "options": False}
arg_types = {'this': True, 'expressions': False, 'options': False}
key = 'reference'
class Tuple(Expression):
3124class Tuple(Expression):
3125    arg_types = {"expressions": False}
3126
3127    def isin(
3128        self,
3129        *expressions: t.Any,
3130        query: t.Optional[ExpOrStr] = None,
3131        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3132        copy: bool = True,
3133        **opts,
3134    ) -> In:
3135        return In(
3136            this=maybe_copy(self, copy),
3137            expressions=[convert(e, copy=copy) for e in expressions],
3138            query=maybe_parse(query, copy=copy, **opts) if query else None,
3139            unnest=(
3140                Unnest(
3141                    expressions=[
3142                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3143                        for e in ensure_list(unnest)
3144                    ]
3145                )
3146                if unnest
3147                else None
3148            ),
3149        )
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:
3127    def isin(
3128        self,
3129        *expressions: t.Any,
3130        query: t.Optional[ExpOrStr] = None,
3131        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
3132        copy: bool = True,
3133        **opts,
3134    ) -> In:
3135        return In(
3136            this=maybe_copy(self, copy),
3137            expressions=[convert(e, copy=copy) for e in expressions],
3138            query=maybe_parse(query, copy=copy, **opts) if query else None,
3139            unnest=(
3140                Unnest(
3141                    expressions=[
3142                        maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts)
3143                        for e in ensure_list(unnest)
3144                    ]
3145                )
3146                if unnest
3147                else None
3148            ),
3149        )
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):
3180class QueryOption(Expression):
3181    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'queryoption'
class WithTableHint(Expression):
3185class WithTableHint(Expression):
3186    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withtablehint'
class IndexTableHint(Expression):
3190class IndexTableHint(Expression):
3191    arg_types = {"this": True, "expressions": False, "target": False}
arg_types = {'this': True, 'expressions': False, 'target': False}
key = 'indextablehint'
class HistoricalData(Expression):
3195class HistoricalData(Expression):
3196    arg_types = {"this": True, "kind": True, "expression": True}
arg_types = {'this': True, 'kind': True, 'expression': True}
key = 'historicaldata'
class Table(Expression):
3199class Table(Expression):
3200    arg_types = {
3201        "this": False,
3202        "alias": False,
3203        "db": False,
3204        "catalog": False,
3205        "laterals": False,
3206        "joins": False,
3207        "pivots": False,
3208        "hints": False,
3209        "system_time": False,
3210        "version": False,
3211        "format": False,
3212        "pattern": False,
3213        "ordinality": False,
3214        "when": False,
3215        "only": False,
3216        "partition": False,
3217        "changes": False,
3218        "rows_from": False,
3219        "sample": False,
3220    }
3221
3222    @property
3223    def name(self) -> str:
3224        if isinstance(self.this, Func):
3225            return ""
3226        return self.this.name
3227
3228    @property
3229    def db(self) -> str:
3230        return self.text("db")
3231
3232    @property
3233    def catalog(self) -> str:
3234        return self.text("catalog")
3235
3236    @property
3237    def selects(self) -> t.List[Expression]:
3238        return []
3239
3240    @property
3241    def named_selects(self) -> t.List[str]:
3242        return []
3243
3244    @property
3245    def parts(self) -> t.List[Expression]:
3246        """Return the parts of a table in order catalog, db, table."""
3247        parts: t.List[Expression] = []
3248
3249        for arg in ("catalog", "db", "this"):
3250            part = self.args.get(arg)
3251
3252            if isinstance(part, Dot):
3253                parts.extend(part.flatten())
3254            elif isinstance(part, Expression):
3255                parts.append(part)
3256
3257        return parts
3258
3259    def to_column(self, copy: bool = True) -> Alias | Column | Dot:
3260        parts = self.parts
3261        last_part = parts[-1]
3262
3263        if isinstance(last_part, Identifier):
3264            col = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3265        else:
3266            # This branch will be reached if a function or array is wrapped in a `Table`
3267            col = last_part
3268
3269        alias = self.args.get("alias")
3270        if alias:
3271            col = alias_(col, alias.this, copy=copy)
3272
3273        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
3222    @property
3223    def name(self) -> str:
3224        if isinstance(self.this, Func):
3225            return ""
3226        return self.this.name
db: str
3228    @property
3229    def db(self) -> str:
3230        return self.text("db")
catalog: str
3232    @property
3233    def catalog(self) -> str:
3234        return self.text("catalog")
selects: List[Expression]
3236    @property
3237    def selects(self) -> t.List[Expression]:
3238        return []
named_selects: List[str]
3240    @property
3241    def named_selects(self) -> t.List[str]:
3242        return []
parts: List[Expression]
3244    @property
3245    def parts(self) -> t.List[Expression]:
3246        """Return the parts of a table in order catalog, db, table."""
3247        parts: t.List[Expression] = []
3248
3249        for arg in ("catalog", "db", "this"):
3250            part = self.args.get(arg)
3251
3252            if isinstance(part, Dot):
3253                parts.extend(part.flatten())
3254            elif isinstance(part, Expression):
3255                parts.append(part)
3256
3257        return parts

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

def to_column( self, copy: bool = True) -> Alias | Column | Dot:
3259    def to_column(self, copy: bool = True) -> Alias | Column | Dot:
3260        parts = self.parts
3261        last_part = parts[-1]
3262
3263        if isinstance(last_part, Identifier):
3264            col = column(*reversed(parts[0:4]), fields=parts[4:], copy=copy)  # type: ignore
3265        else:
3266            # This branch will be reached if a function or array is wrapped in a `Table`
3267            col = last_part
3268
3269        alias = self.args.get("alias")
3270        if alias:
3271            col = alias_(col, alias.this, copy=copy)
3272
3273        return col
key = 'table'
class SetOperation(Query):
3276class SetOperation(Query):
3277    arg_types = {
3278        "with": False,
3279        "this": True,
3280        "expression": True,
3281        "distinct": False,
3282        "by_name": False,
3283        **QUERY_MODIFIERS,
3284    }
3285
3286    def select(
3287        self: S,
3288        *expressions: t.Optional[ExpOrStr],
3289        append: bool = True,
3290        dialect: DialectType = None,
3291        copy: bool = True,
3292        **opts,
3293    ) -> S:
3294        this = maybe_copy(self, copy)
3295        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3296        this.expression.unnest().select(
3297            *expressions, append=append, dialect=dialect, copy=False, **opts
3298        )
3299        return this
3300
3301    @property
3302    def named_selects(self) -> t.List[str]:
3303        return self.this.unnest().named_selects
3304
3305    @property
3306    def is_star(self) -> bool:
3307        return self.this.is_star or self.expression.is_star
3308
3309    @property
3310    def selects(self) -> t.List[Expression]:
3311        return self.this.unnest().selects
3312
3313    @property
3314    def left(self) -> Query:
3315        return self.this
3316
3317    @property
3318    def right(self) -> Query:
3319        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:
3286    def select(
3287        self: S,
3288        *expressions: t.Optional[ExpOrStr],
3289        append: bool = True,
3290        dialect: DialectType = None,
3291        copy: bool = True,
3292        **opts,
3293    ) -> S:
3294        this = maybe_copy(self, copy)
3295        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
3296        this.expression.unnest().select(
3297            *expressions, append=append, dialect=dialect, copy=False, **opts
3298        )
3299        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]
3301    @property
3302    def named_selects(self) -> t.List[str]:
3303        return self.this.unnest().named_selects

Returns the output names of the query's projections.

is_star: bool
3305    @property
3306    def is_star(self) -> bool:
3307        return self.this.is_star or self.expression.is_star

Checks whether an expression is a star.

selects: List[Expression]
3309    @property
3310    def selects(self) -> t.List[Expression]:
3311        return self.this.unnest().selects

Returns the query's projections.

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

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:
3378    def set_(
3379        self,
3380        *expressions: ExpOrStr,
3381        append: bool = True,
3382        dialect: DialectType = None,
3383        copy: bool = True,
3384        **opts,
3385    ) -> Update:
3386        """
3387        Append to or set the SET expressions.
3388
3389        Example:
3390            >>> Update().table("my_table").set_("x = 1").sql()
3391            'UPDATE my_table SET x = 1'
3392
3393        Args:
3394            *expressions: the SQL code strings to parse.
3395                If `Expression` instance(s) are passed, they will be used as-is.
3396                Multiple expressions are combined with a comma.
3397            append: if `True`, add the new expressions to any existing SET expressions.
3398                Otherwise, this resets the expressions.
3399            dialect: the dialect used to parse the input expressions.
3400            copy: if `False`, modify this expression instance in-place.
3401            opts: other options to use to parse the input expressions.
3402        """
3403        return _apply_list_builder(
3404            *expressions,
3405            instance=self,
3406            arg="expressions",
3407            append=append,
3408            into=Expression,
3409            prefix=None,
3410            dialect=dialect,
3411            copy=copy,
3412            **opts,
3413        )

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:
3415    def where(
3416        self,
3417        *expressions: t.Optional[ExpOrStr],
3418        append: bool = True,
3419        dialect: DialectType = None,
3420        copy: bool = True,
3421        **opts,
3422    ) -> Select:
3423        """
3424        Append to or set the WHERE expressions.
3425
3426        Example:
3427            >>> Update().table("tbl").set_("x = 1").where("x = 'a' OR x < 'b'").sql()
3428            "UPDATE tbl SET x = 1 WHERE x = 'a' OR x < 'b'"
3429
3430        Args:
3431            *expressions: the SQL code strings to parse.
3432                If an `Expression` instance is passed, it will be used as-is.
3433                Multiple expressions are combined with an AND operator.
3434            append: if `True`, AND the new expressions to any existing expression.
3435                Otherwise, this resets the expression.
3436            dialect: the dialect used to parse the input expressions.
3437            copy: if `False`, modify this expression instance in-place.
3438            opts: other options to use to parse the input expressions.
3439
3440        Returns:
3441            Select: the modified expression.
3442        """
3443        return _apply_conjunction_builder(
3444            *expressions,
3445            instance=self,
3446            arg="where",
3447            append=append,
3448            into=Where,
3449            dialect=dialect,
3450            copy=copy,
3451            **opts,
3452        )

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:
3454    def from_(
3455        self,
3456        expression: t.Optional[ExpOrStr] = None,
3457        dialect: DialectType = None,
3458        copy: bool = True,
3459        **opts,
3460    ) -> Update:
3461        """
3462        Set the FROM expression.
3463
3464        Example:
3465            >>> Update().table("my_table").set_("x = 1").from_("baz").sql()
3466            'UPDATE my_table SET x = 1 FROM baz'
3467
3468        Args:
3469            expression : the SQL code strings to parse.
3470                If a `From` instance is passed, this is used as-is.
3471                If another `Expression` instance is passed, it will be wrapped in a `From`.
3472                If nothing is passed in then a from is not applied to the expression
3473            dialect: the dialect used to parse the input expression.
3474            copy: if `False`, modify this expression instance in-place.
3475            opts: other options to use to parse the input expressions.
3476
3477        Returns:
3478            The modified Update expression.
3479        """
3480        if not expression:
3481            return maybe_copy(self, copy)
3482
3483        return _apply_builder(
3484            expression=expression,
3485            instance=self,
3486            arg="from",
3487            into=From,
3488            prefix="FROM",
3489            dialect=dialect,
3490            copy=copy,
3491            **opts,
3492        )

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:
3494    def with_(
3495        self,
3496        alias: ExpOrStr,
3497        as_: ExpOrStr,
3498        recursive: t.Optional[bool] = None,
3499        materialized: t.Optional[bool] = None,
3500        append: bool = True,
3501        dialect: DialectType = None,
3502        copy: bool = True,
3503        **opts,
3504    ) -> Update:
3505        """
3506        Append to or set the common table expressions.
3507
3508        Example:
3509            >>> Update().table("my_table").set_("x = 1").from_("baz").with_("baz", "SELECT id FROM foo").sql()
3510            'WITH baz AS (SELECT id FROM foo) UPDATE my_table SET x = 1 FROM baz'
3511
3512        Args:
3513            alias: the SQL code string to parse as the table name.
3514                If an `Expression` instance is passed, this is used as-is.
3515            as_: the SQL code string to parse as the table expression.
3516                If an `Expression` instance is passed, it will be used as-is.
3517            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
3518            materialized: set the MATERIALIZED part of the expression.
3519            append: if `True`, add to any existing expressions.
3520                Otherwise, this resets the expressions.
3521            dialect: the dialect used to parse the input expression.
3522            copy: if `False`, modify this expression instance in-place.
3523            opts: other options to use to parse the input expressions.
3524
3525        Returns:
3526            The modified expression.
3527        """
3528        return _apply_cte_builder(
3529            self,
3530            alias,
3531            as_,
3532            recursive=recursive,
3533            materialized=materialized,
3534            append=append,
3535            dialect=dialect,
3536            copy=copy,
3537            **opts,
3538        )

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

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

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:
3662    def sort_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 SORT BY expression.
3672
3673        Example:
3674            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
3675            'SELECT x FROM tbl SORT 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 `SORT`.
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="sort",
3694            append=append,
3695            copy=copy,
3696            prefix="SORT BY",
3697            into=Sort,
3698            dialect=dialect,
3699            **opts,
3700        )

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:
3702    def cluster_by(
3703        self,
3704        *expressions: t.Optional[ExpOrStr],
3705        append: bool = True,
3706        dialect: DialectType = None,
3707        copy: bool = True,
3708        **opts,
3709    ) -> Select:
3710        """
3711        Set the CLUSTER BY expression.
3712
3713        Example:
3714            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
3715            'SELECT x FROM tbl CLUSTER BY x DESC'
3716
3717        Args:
3718            *expressions: the SQL code strings to parse.
3719                If a `Group` instance is passed, this is used as-is.
3720                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
3721            append: if `True`, add to any existing expressions.
3722                Otherwise, this flattens all the `Order` expression into a single expression.
3723            dialect: the dialect used to parse the input expression.
3724            copy: if `False`, modify this expression instance in-place.
3725            opts: other options to use to parse the input expressions.
3726
3727        Returns:
3728            The modified Select expression.
3729        """
3730        return _apply_child_list_builder(
3731            *expressions,
3732            instance=self,
3733            arg="cluster",
3734            append=append,
3735            copy=copy,
3736            prefix="CLUSTER BY",
3737            into=Cluster,
3738            dialect=dialect,
3739            **opts,
3740        )

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:
3742    def select(
3743        self,
3744        *expressions: t.Optional[ExpOrStr],
3745        append: bool = True,
3746        dialect: DialectType = None,
3747        copy: bool = True,
3748        **opts,
3749    ) -> Select:
3750        return _apply_list_builder(
3751            *expressions,
3752            instance=self,
3753            arg="expressions",
3754            append=append,
3755            dialect=dialect,
3756            into=Expression,
3757            copy=copy,
3758            **opts,
3759        )

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:
3761    def lateral(
3762        self,
3763        *expressions: t.Optional[ExpOrStr],
3764        append: bool = True,
3765        dialect: DialectType = None,
3766        copy: bool = True,
3767        **opts,
3768    ) -> Select:
3769        """
3770        Append to or set the LATERAL expressions.
3771
3772        Example:
3773            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3774            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3775
3776        Args:
3777            *expressions: the SQL code strings to parse.
3778                If an `Expression` instance is passed, it will be used as-is.
3779            append: if `True`, add to any existing expressions.
3780                Otherwise, this resets the expressions.
3781            dialect: the dialect used to parse the input expressions.
3782            copy: if `False`, modify this expression instance in-place.
3783            opts: other options to use to parse the input expressions.
3784
3785        Returns:
3786            The modified Select expression.
3787        """
3788        return _apply_list_builder(
3789            *expressions,
3790            instance=self,
3791            arg="laterals",
3792            append=append,
3793            into=Lateral,
3794            prefix="LATERAL VIEW",
3795            dialect=dialect,
3796            copy=copy,
3797            **opts,
3798        )

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:
3800    def join(
3801        self,
3802        expression: ExpOrStr,
3803        on: t.Optional[ExpOrStr] = None,
3804        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3805        append: bool = True,
3806        join_type: t.Optional[str] = None,
3807        join_alias: t.Optional[Identifier | str] = None,
3808        dialect: DialectType = None,
3809        copy: bool = True,
3810        **opts,
3811    ) -> Select:
3812        """
3813        Append to or set the JOIN expressions.
3814
3815        Example:
3816            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
3817            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
3818
3819            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
3820            'SELECT 1 FROM a JOIN b USING (x, y, z)'
3821
3822            Use `join_type` to change the type of join:
3823
3824            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
3825            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
3826
3827        Args:
3828            expression: the SQL code string to parse.
3829                If an `Expression` instance is passed, it will be used as-is.
3830            on: optionally specify the join "on" criteria as a SQL string.
3831                If an `Expression` instance is passed, it will be used as-is.
3832            using: optionally specify the join "using" criteria as a SQL string.
3833                If an `Expression` instance is passed, it will be used as-is.
3834            append: if `True`, add to any existing expressions.
3835                Otherwise, this resets the expressions.
3836            join_type: if set, alter the parsed join type.
3837            join_alias: an optional alias for the joined source.
3838            dialect: the dialect used to parse the input expressions.
3839            copy: if `False`, modify this expression instance in-place.
3840            opts: other options to use to parse the input expressions.
3841
3842        Returns:
3843            Select: the modified expression.
3844        """
3845        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
3846
3847        try:
3848            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
3849        except ParseError:
3850            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
3851
3852        join = expression if isinstance(expression, Join) else Join(this=expression)
3853
3854        if isinstance(join.this, Select):
3855            join.this.replace(join.this.subquery())
3856
3857        if join_type:
3858            method: t.Optional[Token]
3859            side: t.Optional[Token]
3860            kind: t.Optional[Token]
3861
3862            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
3863
3864            if method:
3865                join.set("method", method.text)
3866            if side:
3867                join.set("side", side.text)
3868            if kind:
3869                join.set("kind", kind.text)
3870
3871        if on:
3872            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
3873            join.set("on", on)
3874
3875        if using:
3876            join = _apply_list_builder(
3877                *ensure_list(using),
3878                instance=join,
3879                arg="using",
3880                append=append,
3881                copy=copy,
3882                into=Identifier,
3883                **opts,
3884            )
3885
3886        if join_alias:
3887            join.set("this", alias_(join.this, join_alias, table=True))
3888
3889        return _apply_list_builder(
3890            join,
3891            instance=self,
3892            arg="joins",
3893            append=append,
3894            copy=copy,
3895            **opts,
3896        )

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:
3898    def where(
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 WHERE expressions.
3908
3909        Example:
3910            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
3911            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
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            Select: the modified expression.
3925        """
3926        return _apply_conjunction_builder(
3927            *expressions,
3928            instance=self,
3929            arg="where",
3930            append=append,
3931            into=Where,
3932            dialect=dialect,
3933            copy=copy,
3934            **opts,
3935        )

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:
3937    def having(
3938        self,
3939        *expressions: t.Optional[ExpOrStr],
3940        append: bool = True,
3941        dialect: DialectType = None,
3942        copy: bool = True,
3943        **opts,
3944    ) -> Select:
3945        """
3946        Append to or set the HAVING expressions.
3947
3948        Example:
3949            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
3950            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
3951
3952        Args:
3953            *expressions: the SQL code strings to parse.
3954                If an `Expression` instance is passed, it will be used as-is.
3955                Multiple expressions are combined with an AND operator.
3956            append: if `True`, AND the new expressions to any existing expression.
3957                Otherwise, this resets the expression.
3958            dialect: the dialect used to parse the input expressions.
3959            copy: if `False`, modify this expression instance in-place.
3960            opts: other options to use to parse the input expressions.
3961
3962        Returns:
3963            The modified Select expression.
3964        """
3965        return _apply_conjunction_builder(
3966            *expressions,
3967            instance=self,
3968            arg="having",
3969            append=append,
3970            into=Having,
3971            dialect=dialect,
3972            copy=copy,
3973            **opts,
3974        )

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:
3976    def window(
3977        self,
3978        *expressions: t.Optional[ExpOrStr],
3979        append: bool = True,
3980        dialect: DialectType = None,
3981        copy: bool = True,
3982        **opts,
3983    ) -> Select:
3984        return _apply_list_builder(
3985            *expressions,
3986            instance=self,
3987            arg="windows",
3988            append=append,
3989            into=Window,
3990            dialect=dialect,
3991            copy=copy,
3992            **opts,
3993        )
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:
3995    def qualify(
3996        self,
3997        *expressions: t.Optional[ExpOrStr],
3998        append: bool = True,
3999        dialect: DialectType = None,
4000        copy: bool = True,
4001        **opts,
4002    ) -> Select:
4003        return _apply_conjunction_builder(
4004            *expressions,
4005            instance=self,
4006            arg="qualify",
4007            append=append,
4008            into=Qualify,
4009            dialect=dialect,
4010            copy=copy,
4011            **opts,
4012        )
def distinct( self, *ons: Union[str, Expression, NoneType], distinct: bool = True, copy: bool = True) -> Select:
4014    def distinct(
4015        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
4016    ) -> Select:
4017        """
4018        Set the OFFSET expression.
4019
4020        Example:
4021            >>> Select().from_("tbl").select("x").distinct().sql()
4022            'SELECT DISTINCT x FROM tbl'
4023
4024        Args:
4025            ons: the expressions to distinct on
4026            distinct: whether the Select should be distinct
4027            copy: if `False`, modify this expression instance in-place.
4028
4029        Returns:
4030            Select: the modified expression.
4031        """
4032        instance = maybe_copy(self, copy)
4033        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
4034        instance.set("distinct", Distinct(on=on) if distinct else None)
4035        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:
4037    def ctas(
4038        self,
4039        table: ExpOrStr,
4040        properties: t.Optional[t.Dict] = None,
4041        dialect: DialectType = None,
4042        copy: bool = True,
4043        **opts,
4044    ) -> Create:
4045        """
4046        Convert this expression to a CREATE TABLE AS statement.
4047
4048        Example:
4049            >>> Select().select("*").from_("tbl").ctas("x").sql()
4050            'CREATE TABLE x AS SELECT * FROM tbl'
4051
4052        Args:
4053            table: the SQL code string to parse as the table name.
4054                If another `Expression` instance is passed, it will be used as-is.
4055            properties: an optional mapping of table properties
4056            dialect: the dialect used to parse the input table.
4057            copy: if `False`, modify this expression instance in-place.
4058            opts: other options to use to parse the input table.
4059
4060        Returns:
4061            The new Create expression.
4062        """
4063        instance = maybe_copy(self, copy)
4064        table_expression = maybe_parse(table, into=Table, dialect=dialect, **opts)
4065
4066        properties_expression = None
4067        if properties:
4068            properties_expression = Properties.from_dict(properties)
4069
4070        return Create(
4071            this=table_expression,
4072            kind="TABLE",
4073            expression=instance,
4074            properties=properties_expression,
4075        )

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:
4077    def lock(self, update: bool = True, copy: bool = True) -> Select:
4078        """
4079        Set the locking read mode for this expression.
4080
4081        Examples:
4082            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
4083            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
4084
4085            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
4086            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
4087
4088        Args:
4089            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
4090            copy: if `False`, modify this expression instance in-place.
4091
4092        Returns:
4093            The modified expression.
4094        """
4095        inst = maybe_copy(self, copy)
4096        inst.set("locks", [Lock(update=update)])
4097
4098        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:
4100    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
4101        """
4102        Set hints for this expression.
4103
4104        Examples:
4105            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
4106            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
4107
4108        Args:
4109            hints: The SQL code strings to parse as the hints.
4110                If an `Expression` instance is passed, it will be used as-is.
4111            dialect: The dialect used to parse the hints.
4112            copy: If `False`, modify this expression instance in-place.
4113
4114        Returns:
4115            The modified expression.
4116        """
4117        inst = maybe_copy(self, copy)
4118        inst.set(
4119            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
4120        )
4121
4122        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]
4124    @property
4125    def named_selects(self) -> t.List[str]:
4126        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
4128    @property
4129    def is_star(self) -> bool:
4130        return any(expression.is_star for expression in self.expressions)

Checks whether an expression is a star.

selects: List[Expression]
4132    @property
4133    def selects(self) -> t.List[Expression]:
4134        return self.expressions

Returns the query's projections.

key = 'select'
UNWRAPPED_QUERIES = (<class 'Select'>, <class 'SetOperation'>)
class Subquery(DerivedTable, Query):
4140class Subquery(DerivedTable, Query):
4141    arg_types = {
4142        "this": True,
4143        "alias": False,
4144        "with": False,
4145        **QUERY_MODIFIERS,
4146    }
4147
4148    def unnest(self):
4149        """Returns the first non subquery."""
4150        expression = self
4151        while isinstance(expression, Subquery):
4152            expression = expression.this
4153        return expression
4154
4155    def unwrap(self) -> Subquery:
4156        expression = self
4157        while expression.same_parent and expression.is_wrapper:
4158            expression = t.cast(Subquery, expression.parent)
4159        return expression
4160
4161    def select(
4162        self,
4163        *expressions: t.Optional[ExpOrStr],
4164        append: bool = True,
4165        dialect: DialectType = None,
4166        copy: bool = True,
4167        **opts,
4168    ) -> Subquery:
4169        this = maybe_copy(self, copy)
4170        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4171        return this
4172
4173    @property
4174    def is_wrapper(self) -> bool:
4175        """
4176        Whether this Subquery acts as a simple wrapper around another expression.
4177
4178        SELECT * FROM (((SELECT * FROM t)))
4179                      ^
4180                      This corresponds to a "wrapper" Subquery node
4181        """
4182        return all(v is None for k, v in self.args.items() if k != "this")
4183
4184    @property
4185    def is_star(self) -> bool:
4186        return self.this.is_star
4187
4188    @property
4189    def output_name(self) -> str:
4190        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):
4148    def unnest(self):
4149        """Returns the first non subquery."""
4150        expression = self
4151        while isinstance(expression, Subquery):
4152            expression = expression.this
4153        return expression

Returns the first non subquery.

def unwrap(self) -> Subquery:
4155    def unwrap(self) -> Subquery:
4156        expression = self
4157        while expression.same_parent and expression.is_wrapper:
4158            expression = t.cast(Subquery, expression.parent)
4159        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:
4161    def select(
4162        self,
4163        *expressions: t.Optional[ExpOrStr],
4164        append: bool = True,
4165        dialect: DialectType = None,
4166        copy: bool = True,
4167        **opts,
4168    ) -> Subquery:
4169        this = maybe_copy(self, copy)
4170        this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
4171        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
4173    @property
4174    def is_wrapper(self) -> bool:
4175        """
4176        Whether this Subquery acts as a simple wrapper around another expression.
4177
4178        SELECT * FROM (((SELECT * FROM t)))
4179                      ^
4180                      This corresponds to a "wrapper" Subquery node
4181        """
4182        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
4184    @property
4185    def is_star(self) -> bool:
4186        return self.this.is_star

Checks whether an expression is a star.

output_name: str
4188    @property
4189    def output_name(self) -> str:
4190        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):
4193class TableSample(Expression):
4194    arg_types = {
4195        "expressions": False,
4196        "method": False,
4197        "bucket_numerator": False,
4198        "bucket_denominator": False,
4199        "bucket_field": False,
4200        "percent": False,
4201        "rows": False,
4202        "size": False,
4203        "seed": False,
4204    }
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):
4207class Tag(Expression):
4208    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
4209
4210    arg_types = {
4211        "this": False,
4212        "prefix": False,
4213        "postfix": False,
4214    }

Tags are used for generating arbitrary sql like SELECT x.

arg_types = {'this': False, 'prefix': False, 'postfix': False}
key = 'tag'
class Pivot(Expression):
4219class Pivot(Expression):
4220    arg_types = {
4221        "this": False,
4222        "alias": False,
4223        "expressions": False,
4224        "field": False,
4225        "unpivot": False,
4226        "using": False,
4227        "group": False,
4228        "columns": False,
4229        "include_nulls": False,
4230        "default_on_null": False,
4231    }
4232
4233    @property
4234    def unpivot(self) -> bool:
4235        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
4233    @property
4234    def unpivot(self) -> bool:
4235        return bool(self.args.get("unpivot"))
key = 'pivot'
class Window(Condition):
4238class Window(Condition):
4239    arg_types = {
4240        "this": True,
4241        "partition_by": False,
4242        "order": False,
4243        "spec": False,
4244        "alias": False,
4245        "over": False,
4246        "first": False,
4247    }
arg_types = {'this': True, 'partition_by': False, 'order': False, 'spec': False, 'alias': False, 'over': False, 'first': False}
key = 'window'
class WindowSpec(Expression):
4250class WindowSpec(Expression):
4251    arg_types = {
4252        "kind": False,
4253        "start": False,
4254        "start_side": False,
4255        "end": False,
4256        "end_side": False,
4257    }
arg_types = {'kind': False, 'start': False, 'start_side': False, 'end': False, 'end_side': False}
key = 'windowspec'
class PreWhere(Expression):
4260class PreWhere(Expression):
4261    pass
key = 'prewhere'
class Where(Expression):
4264class Where(Expression):
4265    pass
key = 'where'
class Star(Expression):
4268class Star(Expression):
4269    arg_types = {"except": False, "replace": False, "rename": False}
4270
4271    @property
4272    def name(self) -> str:
4273        return "*"
4274
4275    @property
4276    def output_name(self) -> str:
4277        return self.name
arg_types = {'except': False, 'replace': False, 'rename': False}
name: str
4271    @property
4272    def name(self) -> str:
4273        return "*"
output_name: str
4275    @property
4276    def output_name(self) -> str:
4277        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):
4280class Parameter(Condition):
4281    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'parameter'
class SessionParameter(Condition):
4284class SessionParameter(Condition):
4285    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'sessionparameter'
class Placeholder(Condition):
4288class Placeholder(Condition):
4289    arg_types = {"this": False, "kind": False}
4290
4291    @property
4292    def name(self) -> str:
4293        return self.this or "?"
arg_types = {'this': False, 'kind': False}
name: str
4291    @property
4292    def name(self) -> str:
4293        return self.this or "?"
key = 'placeholder'
class Null(Condition):
4296class Null(Condition):
4297    arg_types: t.Dict[str, t.Any] = {}
4298
4299    @property
4300    def name(self) -> str:
4301        return "NULL"
4302
4303    def to_py(self) -> Lit[None]:
4304        return None
arg_types: Dict[str, Any] = {}
name: str
4299    @property
4300    def name(self) -> str:
4301        return "NULL"
def to_py(self) -> Literal[None]:
4303    def to_py(self) -> Lit[None]:
4304        return None

Returns a Python object equivalent of the SQL node.

key = 'null'
class Boolean(Condition):
4307class Boolean(Condition):
4308    def to_py(self) -> bool:
4309        return self.this
def to_py(self) -> bool:
4308    def to_py(self) -> bool:
4309        return self.this

Returns a Python object equivalent of the SQL node.

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

Checks whether an expression is a star.

name: str
4767    @property
4768    def name(self) -> str:
4769        return self.expression.name
output_name: str
4771    @property
4772    def output_name(self) -> str:
4773        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:
4775    @classmethod
4776    def build(self, expressions: t.Sequence[Expression]) -> Dot:
4777        """Build a Dot object with a sequence of expressions."""
4778        if len(expressions) < 2:
4779            raise ValueError("Dot requires >= 2 expressions.")
4780
4781        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]
4783    @property
4784    def parts(self) -> t.List[Expression]:
4785        """Return the parts of a table / column in order catalog, db, table."""
4786        this, *parts = self.flatten()
4787
4788        parts.reverse()
4789
4790        for arg in COLUMN_PARTS:
4791            part = this.args.get(arg)
4792
4793            if isinstance(part, Expression):
4794                parts.append(part)
4795
4796        parts.reverse()
4797        return parts

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

key = 'dot'
class DPipe(Binary):
4800class DPipe(Binary):
4801    arg_types = {"this": True, "expression": True, "safe": False}
arg_types = {'this': True, 'expression': True, 'safe': False}
key = 'dpipe'
class EQ(Binary, Predicate):
4804class EQ(Binary, Predicate):
4805    pass
key = 'eq'
class NullSafeEQ(Binary, Predicate):
4808class NullSafeEQ(Binary, Predicate):
4809    pass
key = 'nullsafeeq'
class NullSafeNEQ(Binary, Predicate):
4812class NullSafeNEQ(Binary, Predicate):
4813    pass
key = 'nullsafeneq'
class PropertyEQ(Binary):
4817class PropertyEQ(Binary):
4818    pass
key = 'propertyeq'
class Distance(Binary):
4821class Distance(Binary):
4822    pass
key = 'distance'
class Escape(Binary):
4825class Escape(Binary):
4826    pass
key = 'escape'
class Glob(Binary, Predicate):
4829class Glob(Binary, Predicate):
4830    pass
key = 'glob'
class GT(Binary, Predicate):
4833class GT(Binary, Predicate):
4834    pass
key = 'gt'
class GTE(Binary, Predicate):
4837class GTE(Binary, Predicate):
4838    pass
key = 'gte'
class ILike(Binary, Predicate):
4841class ILike(Binary, Predicate):
4842    pass
key = 'ilike'
class ILikeAny(Binary, Predicate):
4845class ILikeAny(Binary, Predicate):
4846    pass
key = 'ilikeany'
class IntDiv(Binary):
4849class IntDiv(Binary):
4850    pass
key = 'intdiv'
class Is(Binary, Predicate):
4853class Is(Binary, Predicate):
4854    pass
key = 'is'
class Kwarg(Binary):
4857class Kwarg(Binary):
4858    """Kwarg in special functions like func(kwarg => y)."""

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

key = 'kwarg'
class Like(Binary, Predicate):
4861class Like(Binary, Predicate):
4862    pass
key = 'like'
class LikeAny(Binary, Predicate):
4865class LikeAny(Binary, Predicate):
4866    pass
key = 'likeany'
class LT(Binary, Predicate):
4869class LT(Binary, Predicate):
4870    pass
key = 'lt'
class LTE(Binary, Predicate):
4873class LTE(Binary, Predicate):
4874    pass
key = 'lte'
class Mod(Binary):
4877class Mod(Binary):
4878    pass
key = 'mod'
class Mul(Binary):
4881class Mul(Binary):
4882    pass
key = 'mul'
class NEQ(Binary, Predicate):
4885class NEQ(Binary, Predicate):
4886    pass
key = 'neq'
class Operator(Binary):
4890class Operator(Binary):
4891    arg_types = {"this": True, "operator": True, "expression": True}
arg_types = {'this': True, 'operator': True, 'expression': True}
key = 'operator'
class SimilarTo(Binary, Predicate):
4894class SimilarTo(Binary, Predicate):
4895    pass
key = 'similarto'
class Slice(Binary):
4898class Slice(Binary):
4899    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'slice'
class Sub(Binary):
4902class Sub(Binary):
4903    pass
key = 'sub'
class Unary(Condition):
4908class Unary(Condition):
4909    pass
key = 'unary'
class BitwiseNot(Unary):
4912class BitwiseNot(Unary):
4913    pass
key = 'bitwisenot'
class Not(Unary):
4916class Not(Unary):
4917    pass
key = 'not'
class Paren(Unary):
4920class Paren(Unary):
4921    @property
4922    def output_name(self) -> str:
4923        return self.this.name
output_name: str
4921    @property
4922    def output_name(self) -> str:
4923        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):
4926class Neg(Unary):
4927    def to_py(self) -> int | Decimal:
4928        if self.is_number:
4929            return self.this.to_py() * -1
4930        return super().to_py()
def to_py(self) -> int | decimal.Decimal:
4927    def to_py(self) -> int | Decimal:
4928        if self.is_number:
4929            return self.this.to_py() * -1
4930        return super().to_py()

Returns a Python object equivalent of the SQL node.

key = 'neg'
class Alias(Expression):
4933class Alias(Expression):
4934    arg_types = {"this": True, "alias": False}
4935
4936    @property
4937    def output_name(self) -> str:
4938        return self.alias
arg_types = {'this': True, 'alias': False}
output_name: str
4936    @property
4937    def output_name(self) -> str:
4938        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):
4943class PivotAlias(Alias):
4944    pass
key = 'pivotalias'
class PivotAny(Expression):
4949class PivotAny(Expression):
4950    arg_types = {"this": False}
arg_types = {'this': False}
key = 'pivotany'
class Aliases(Expression):
4953class Aliases(Expression):
4954    arg_types = {"this": True, "expressions": True}
4955
4956    @property
4957    def aliases(self):
4958        return self.expressions
arg_types = {'this': True, 'expressions': True}
aliases
4956    @property
4957    def aliases(self):
4958        return self.expressions
key = 'aliases'
class AtIndex(Expression):
4962class AtIndex(Expression):
4963    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'atindex'
class AtTimeZone(Expression):
4966class AtTimeZone(Expression):
4967    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'attimezone'
class FromTimeZone(Expression):
4970class FromTimeZone(Expression):
4971    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'fromtimezone'
class Between(Predicate):
4974class Between(Predicate):
4975    arg_types = {"this": True, "low": True, "high": True}
arg_types = {'this': True, 'low': True, 'high': True}
key = 'between'
class Bracket(Condition):
4978class Bracket(Condition):
4979    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
4980    arg_types = {
4981        "this": True,
4982        "expressions": True,
4983        "offset": False,
4984        "safe": False,
4985        "returns_list_for_maps": False,
4986    }
4987
4988    @property
4989    def output_name(self) -> str:
4990        if len(self.expressions) == 1:
4991            return self.expressions[0].output_name
4992
4993        return super().output_name
arg_types = {'this': True, 'expressions': True, 'offset': False, 'safe': False, 'returns_list_for_maps': False}
output_name: str
4988    @property
4989    def output_name(self) -> str:
4990        if len(self.expressions) == 1:
4991            return self.expressions[0].output_name
4992
4993        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):
4996class Distinct(Expression):
4997    arg_types = {"expressions": False, "on": False}
arg_types = {'expressions': False, 'on': False}
key = 'distinct'
class In(Predicate):
5000class In(Predicate):
5001    arg_types = {
5002        "this": True,
5003        "expressions": False,
5004        "query": False,
5005        "unnest": False,
5006        "field": False,
5007        "is_global": False,
5008    }
arg_types = {'this': True, 'expressions': False, 'query': False, 'unnest': False, 'field': False, 'is_global': False}
key = 'in'
class ForIn(Expression):
5012class ForIn(Expression):
5013    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'forin'
class TimeUnit(Expression):
5016class TimeUnit(Expression):
5017    """Automatically converts unit arg into a var."""
5018
5019    arg_types = {"unit": False}
5020
5021    UNABBREVIATED_UNIT_NAME = {
5022        "D": "DAY",
5023        "H": "HOUR",
5024        "M": "MINUTE",
5025        "MS": "MILLISECOND",
5026        "NS": "NANOSECOND",
5027        "Q": "QUARTER",
5028        "S": "SECOND",
5029        "US": "MICROSECOND",
5030        "W": "WEEK",
5031        "Y": "YEAR",
5032    }
5033
5034    VAR_LIKE = (Column, Literal, Var)
5035
5036    def __init__(self, **args):
5037        unit = args.get("unit")
5038        if isinstance(unit, self.VAR_LIKE):
5039            args["unit"] = Var(
5040                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
5041            )
5042        elif isinstance(unit, Week):
5043            unit.set("this", Var(this=unit.this.name.upper()))
5044
5045        super().__init__(**args)
5046
5047    @property
5048    def unit(self) -> t.Optional[Var | IntervalSpan]:
5049        return self.args.get("unit")

Automatically converts unit arg into a var.

TimeUnit(**args)
5036    def __init__(self, **args):
5037        unit = args.get("unit")
5038        if isinstance(unit, self.VAR_LIKE):
5039            args["unit"] = Var(
5040                this=(self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name).upper()
5041            )
5042        elif isinstance(unit, Week):
5043            unit.set("this", Var(this=unit.this.name.upper()))
5044
5045        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]
5047    @property
5048    def unit(self) -> t.Optional[Var | IntervalSpan]:
5049        return self.args.get("unit")
key = 'timeunit'
class IntervalOp(TimeUnit):
5052class IntervalOp(TimeUnit):
5053    arg_types = {"unit": False, "expression": True}
5054
5055    def interval(self):
5056        return Interval(
5057            this=self.expression.copy(),
5058            unit=self.unit.copy() if self.unit else None,
5059        )
arg_types = {'unit': False, 'expression': True}
def interval(self):
5055    def interval(self):
5056        return Interval(
5057            this=self.expression.copy(),
5058            unit=self.unit.copy() if self.unit else None,
5059        )
key = 'intervalop'
class IntervalSpan(DataType):
5065class IntervalSpan(DataType):
5066    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'intervalspan'
class Interval(TimeUnit):
5069class Interval(TimeUnit):
5070    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key = 'interval'
class IgnoreNulls(Expression):
5073class IgnoreNulls(Expression):
5074    pass
key = 'ignorenulls'
class RespectNulls(Expression):
5077class RespectNulls(Expression):
5078    pass
key = 'respectnulls'
class HavingMax(Expression):
5082class HavingMax(Expression):
5083    arg_types = {"this": True, "expression": True, "max": True}
arg_types = {'this': True, 'expression': True, 'max': True}
key = 'havingmax'
class Func(Condition):
5087class Func(Condition):
5088    """
5089    The base class for all function expressions.
5090
5091    Attributes:
5092        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
5093            treated as a variable length argument and the argument's value will be stored as a list.
5094        _sql_names (list): the SQL name (1st item in the list) and aliases (subsequent items) for this
5095            function expression. These values are used to map this node to a name during parsing as
5096            well as to provide the function's name during SQL string generation. By default the SQL
5097            name is set to the expression's class name transformed to snake case.
5098    """
5099
5100    is_var_len_args = False
5101
5102    @classmethod
5103    def from_arg_list(cls, args):
5104        if cls.is_var_len_args:
5105            all_arg_keys = list(cls.arg_types)
5106            # If this function supports variable length argument treat the last argument as such.
5107            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5108            num_non_var = len(non_var_len_arg_keys)
5109
5110            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5111            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5112        else:
5113            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5114
5115        return cls(**args_dict)
5116
5117    @classmethod
5118    def sql_names(cls):
5119        if cls is Func:
5120            raise NotImplementedError(
5121                "SQL name is only supported by concrete function implementations"
5122            )
5123        if "_sql_names" not in cls.__dict__:
5124            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5125        return cls._sql_names
5126
5127    @classmethod
5128    def sql_name(cls):
5129        return cls.sql_names()[0]
5130
5131    @classmethod
5132    def default_parser_mappings(cls):
5133        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):
5102    @classmethod
5103    def from_arg_list(cls, args):
5104        if cls.is_var_len_args:
5105            all_arg_keys = list(cls.arg_types)
5106            # If this function supports variable length argument treat the last argument as such.
5107            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
5108            num_non_var = len(non_var_len_arg_keys)
5109
5110            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
5111            args_dict[all_arg_keys[-1]] = args[num_non_var:]
5112        else:
5113            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
5114
5115        return cls(**args_dict)
@classmethod
def sql_names(cls):
5117    @classmethod
5118    def sql_names(cls):
5119        if cls is Func:
5120            raise NotImplementedError(
5121                "SQL name is only supported by concrete function implementations"
5122            )
5123        if "_sql_names" not in cls.__dict__:
5124            cls._sql_names = [camel_to_snake_case(cls.__name__)]
5125        return cls._sql_names
@classmethod
def sql_name(cls):
5127    @classmethod
5128    def sql_name(cls):
5129        return cls.sql_names()[0]
@classmethod
def default_parser_mappings(cls):
5131    @classmethod
5132    def default_parser_mappings(cls):
5133        return {name: cls.from_arg_list for name in cls.sql_names()}
key = 'func'
class AggFunc(Func):
5136class AggFunc(Func):
5137    pass
key = 'aggfunc'
class ParameterizedAgg(AggFunc):
5140class ParameterizedAgg(AggFunc):
5141    arg_types = {"this": True, "expressions": True, "params": True}
arg_types = {'this': True, 'expressions': True, 'params': True}
key = 'parameterizedagg'
class Abs(Func):
5144class Abs(Func):
5145    pass
key = 'abs'
class ArgMax(AggFunc):
5148class ArgMax(AggFunc):
5149    arg_types = {"this": True, "expression": True, "count": False}
5150    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmax'
class ArgMin(AggFunc):
5153class ArgMin(AggFunc):
5154    arg_types = {"this": True, "expression": True, "count": False}
5155    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmin'
class ApproxTopK(AggFunc):
5158class ApproxTopK(AggFunc):
5159    arg_types = {"this": True, "expression": False, "counters": False}
arg_types = {'this': True, 'expression': False, 'counters': False}
key = 'approxtopk'
class Flatten(Func):
5162class Flatten(Func):
5163    pass
key = 'flatten'
class Transform(Func):
5167class Transform(Func):
5168    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'transform'
class Anonymous(Func):
5171class Anonymous(Func):
5172    arg_types = {"this": True, "expressions": False}
5173    is_var_len_args = True
5174
5175    @property
5176    def name(self) -> str:
5177        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
5175    @property
5176    def name(self) -> str:
5177        return self.this if isinstance(self.this, str) else self.this.name
key = 'anonymous'
class AnonymousAggFunc(AggFunc):
5180class AnonymousAggFunc(AggFunc):
5181    arg_types = {"this": True, "expressions": False}
5182    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'anonymousaggfunc'
class CombinedAggFunc(AnonymousAggFunc):
5186class CombinedAggFunc(AnonymousAggFunc):
5187    arg_types = {"this": True, "expressions": False, "parts": True}
arg_types = {'this': True, 'expressions': False, 'parts': True}
key = 'combinedaggfunc'
class CombinedParameterizedAgg(ParameterizedAgg):
5190class CombinedParameterizedAgg(ParameterizedAgg):
5191    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):
5196class Hll(AggFunc):
5197    arg_types = {"this": True, "expressions": False}
5198    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'hll'
class ApproxDistinct(AggFunc):
5201class ApproxDistinct(AggFunc):
5202    arg_types = {"this": True, "accuracy": False}
5203    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
arg_types = {'this': True, 'accuracy': False}
key = 'approxdistinct'
class Apply(Func):
5206class Apply(Func):
5207    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'apply'
class Array(Func):
5210class Array(Func):
5211    arg_types = {"expressions": False, "bracket_notation": False}
5212    is_var_len_args = True
arg_types = {'expressions': False, 'bracket_notation': False}
is_var_len_args = True
key = 'array'
class ToArray(Func):
5216class ToArray(Func):
5217    pass
key = 'toarray'
class List(Func):
5221class List(Func):
5222    arg_types = {"expressions": False}
5223    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'list'
class Pad(Func):
5227class Pad(Func):
5228    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):
5233class ToChar(Func):
5234    arg_types = {"this": True, "format": False, "nlsparam": False}
arg_types = {'this': True, 'format': False, 'nlsparam': False}
key = 'tochar'
class ToNumber(Func):
5239class ToNumber(Func):
5240    arg_types = {
5241        "this": True,
5242        "format": False,
5243        "nlsparam": False,
5244        "precision": False,
5245        "scale": False,
5246    }
arg_types = {'this': True, 'format': False, 'nlsparam': False, 'precision': False, 'scale': False}
key = 'tonumber'
class ToDouble(Func):
5250class ToDouble(Func):
5251    arg_types = {
5252        "this": True,
5253        "format": False,
5254    }
arg_types = {'this': True, 'format': False}
key = 'todouble'
class Columns(Func):
5257class Columns(Func):
5258    arg_types = {"this": True, "unpack": False}
arg_types = {'this': True, 'unpack': False}
key = 'columns'
class Convert(Func):
5262class Convert(Func):
5263    arg_types = {"this": True, "expression": True, "style": False}
arg_types = {'this': True, 'expression': True, 'style': False}
key = 'convert'
class ConvertTimezone(Func):
5266class ConvertTimezone(Func):
5267    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):
5270class GenerateSeries(Func):
5271    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):
5277class ExplodingGenerateSeries(GenerateSeries):
5278    pass
key = 'explodinggenerateseries'
class ArrayAgg(AggFunc):
5281class ArrayAgg(AggFunc):
5282    arg_types = {"this": True, "nulls_excluded": False}
arg_types = {'this': True, 'nulls_excluded': False}
key = 'arrayagg'
class ArrayUniqueAgg(AggFunc):
5285class ArrayUniqueAgg(AggFunc):
5286    pass
key = 'arrayuniqueagg'
class ArrayAll(Func):
5289class ArrayAll(Func):
5290    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayall'
class ArrayAny(Func):
5294class ArrayAny(Func):
5295    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayany'
class ArrayConcat(Func):
5298class ArrayConcat(Func):
5299    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
5300    arg_types = {"this": True, "expressions": False}
5301    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'arrayconcat'
class ArrayConstructCompact(Func):
5304class ArrayConstructCompact(Func):
5305    arg_types = {"expressions": True}
5306    is_var_len_args = True
arg_types = {'expressions': True}
is_var_len_args = True
key = 'arrayconstructcompact'
class ArrayContains(Binary, Func):
5309class ArrayContains(Binary, Func):
5310    _sql_names = ["ARRAY_CONTAINS", "ARRAY_HAS"]
key = 'arraycontains'
class ArrayContainsAll(Binary, Func):
5313class ArrayContainsAll(Binary, Func):
5314    _sql_names = ["ARRAY_CONTAINS_ALL", "ARRAY_HAS_ALL"]
key = 'arraycontainsall'
class ArrayFilter(Func):
5317class ArrayFilter(Func):
5318    arg_types = {"this": True, "expression": True}
5319    _sql_names = ["FILTER", "ARRAY_FILTER"]
arg_types = {'this': True, 'expression': True}
key = 'arrayfilter'
class ArrayToString(Func):
5322class ArrayToString(Func):
5323    arg_types = {"this": True, "expression": True, "null": False}
5324    _sql_names = ["ARRAY_TO_STRING", "ARRAY_JOIN"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'arraytostring'
class String(Func):
5328class String(Func):
5329    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key = 'string'
class StringToArray(Func):
5332class StringToArray(Func):
5333    arg_types = {"this": True, "expression": True, "null": False}
5334    _sql_names = ["STRING_TO_ARRAY", "SPLIT_BY_STRING"]
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'stringtoarray'
class ArrayOverlaps(Binary, Func):
5337class ArrayOverlaps(Binary, Func):
5338    pass
key = 'arrayoverlaps'
class ArraySize(Func):
5341class ArraySize(Func):
5342    arg_types = {"this": True, "expression": False}
5343    _sql_names = ["ARRAY_SIZE", "ARRAY_LENGTH"]
arg_types = {'this': True, 'expression': False}
key = 'arraysize'
class ArraySort(Func):
5346class ArraySort(Func):
5347    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysort'
class ArraySum(Func):
5350class ArraySum(Func):
5351    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysum'
class ArrayUnionAgg(AggFunc):
5354class ArrayUnionAgg(AggFunc):
5355    pass
key = 'arrayunionagg'
class Avg(AggFunc):
5358class Avg(AggFunc):
5359    pass
key = 'avg'
class AnyValue(AggFunc):
5362class AnyValue(AggFunc):
5363    pass
key = 'anyvalue'
class Lag(AggFunc):
5366class Lag(AggFunc):
5367    arg_types = {"this": True, "offset": False, "default": False}
arg_types = {'this': True, 'offset': False, 'default': False}
key = 'lag'
class Lead(AggFunc):
5370class Lead(AggFunc):
5371    arg_types = {"this": True, "offset": False, "default": False}
arg_types = {'this': True, 'offset': False, 'default': False}
key = 'lead'
class First(AggFunc):
5376class First(AggFunc):
5377    pass
key = 'first'
class Last(AggFunc):
5380class Last(AggFunc):
5381    pass
key = 'last'
class FirstValue(AggFunc):
5384class FirstValue(AggFunc):
5385    pass
key = 'firstvalue'
class LastValue(AggFunc):
5388class LastValue(AggFunc):
5389    pass
key = 'lastvalue'
class NthValue(AggFunc):
5392class NthValue(AggFunc):
5393    arg_types = {"this": True, "offset": True}
arg_types = {'this': True, 'offset': True}
key = 'nthvalue'
class Case(Func):
5396class Case(Func):
5397    arg_types = {"this": False, "ifs": True, "default": False}
5398
5399    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5400        instance = maybe_copy(self, copy)
5401        instance.append(
5402            "ifs",
5403            If(
5404                this=maybe_parse(condition, copy=copy, **opts),
5405                true=maybe_parse(then, copy=copy, **opts),
5406            ),
5407        )
5408        return instance
5409
5410    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5411        instance = maybe_copy(self, copy)
5412        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5413        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:
5399    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
5400        instance = maybe_copy(self, copy)
5401        instance.append(
5402            "ifs",
5403            If(
5404                this=maybe_parse(condition, copy=copy, **opts),
5405                true=maybe_parse(then, copy=copy, **opts),
5406            ),
5407        )
5408        return instance
def else_( self, condition: Union[str, Expression], copy: bool = True, **opts) -> Case:
5410    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
5411        instance = maybe_copy(self, copy)
5412        instance.set("default", maybe_parse(condition, copy=copy, **opts))
5413        return instance
key = 'case'
class Cast(Func):
5416class Cast(Func):
5417    arg_types = {
5418        "this": True,
5419        "to": True,
5420        "format": False,
5421        "safe": False,
5422        "action": False,
5423    }
5424
5425    @property
5426    def name(self) -> str:
5427        return self.this.name
5428
5429    @property
5430    def to(self) -> DataType:
5431        return self.args["to"]
5432
5433    @property
5434    def output_name(self) -> str:
5435        return self.name
5436
5437    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5438        """
5439        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5440        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5441        array<int> != array<float>.
5442
5443        Args:
5444            dtypes: the data types to compare this Cast's DataType to.
5445
5446        Returns:
5447            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5448        """
5449        return self.to.is_type(*dtypes)
arg_types = {'this': True, 'to': True, 'format': False, 'safe': False, 'action': False}
name: str
5425    @property
5426    def name(self) -> str:
5427        return self.this.name
to: DataType
5429    @property
5430    def to(self) -> DataType:
5431        return self.args["to"]
output_name: str
5433    @property
5434    def output_name(self) -> str:
5435        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:
5437    def is_type(self, *dtypes: DATA_TYPE) -> bool:
5438        """
5439        Checks whether this Cast's DataType matches one of the provided data types. Nested types
5440        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
5441        array<int> != array<float>.
5442
5443        Args:
5444            dtypes: the data types to compare this Cast's DataType to.
5445
5446        Returns:
5447            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
5448        """
5449        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):
5452class TryCast(Cast):
5453    pass
key = 'trycast'
class Try(Func):
5456class Try(Func):
5457    pass
key = 'try'
class CastToStrType(Func):
5460class CastToStrType(Func):
5461    arg_types = {"this": True, "to": True}
arg_types = {'this': True, 'to': True}
key = 'casttostrtype'
class Collate(Binary, Func):
5464class Collate(Binary, Func):
5465    pass
key = 'collate'
class Ceil(Func):
5468class Ceil(Func):
5469    arg_types = {"this": True, "decimals": False}
5470    _sql_names = ["CEIL", "CEILING"]
arg_types = {'this': True, 'decimals': False}
key = 'ceil'
class Coalesce(Func):
5473class Coalesce(Func):
5474    arg_types = {"this": True, "expressions": False, "is_nvl": False}
5475    is_var_len_args = True
5476    _sql_names = ["COALESCE", "IFNULL", "NVL"]
arg_types = {'this': True, 'expressions': False, 'is_nvl': False}
is_var_len_args = True
key = 'coalesce'
class Chr(Func):
5479class Chr(Func):
5480    arg_types = {"expressions": True, "charset": False}
5481    is_var_len_args = True
5482    _sql_names = ["CHR", "CHAR"]
arg_types = {'expressions': True, 'charset': False}
is_var_len_args = True
key = 'chr'
class Concat(Func):
5485class Concat(Func):
5486    arg_types = {"expressions": True, "safe": False, "coalesce": False}
5487    is_var_len_args = True
arg_types = {'expressions': True, 'safe': False, 'coalesce': False}
is_var_len_args = True
key = 'concat'
class ConcatWs(Concat):
5490class ConcatWs(Concat):
5491    _sql_names = ["CONCAT_WS"]
key = 'concatws'
class Contains(Func):
5494class Contains(Func):
5495    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'contains'
class ConnectByRoot(Func):
5499class ConnectByRoot(Func):
5500    pass
key = 'connectbyroot'
class Count(AggFunc):
5503class Count(AggFunc):
5504    arg_types = {"this": False, "expressions": False, "big_int": False}
5505    is_var_len_args = True
arg_types = {'this': False, 'expressions': False, 'big_int': False}
is_var_len_args = True
key = 'count'
class CountIf(AggFunc):
5508class CountIf(AggFunc):
5509    _sql_names = ["COUNT_IF", "COUNTIF"]
key = 'countif'
class Cbrt(Func):
5513class Cbrt(Func):
5514    pass
key = 'cbrt'
class CurrentDate(Func):
5517class CurrentDate(Func):
5518    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdate'
class CurrentDatetime(Func):
5521class CurrentDatetime(Func):
5522    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdatetime'
class CurrentTime(Func):
5525class CurrentTime(Func):
5526    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttime'
class CurrentTimestamp(Func):
5529class CurrentTimestamp(Func):
5530    arg_types = {"this": False, "sysdate": False}
arg_types = {'this': False, 'sysdate': False}
key = 'currenttimestamp'
class CurrentUser(Func):
5533class CurrentUser(Func):
5534    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentuser'
class DateAdd(Func, IntervalOp):
5537class DateAdd(Func, IntervalOp):
5538    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'dateadd'
class DateSub(Func, IntervalOp):
5541class DateSub(Func, IntervalOp):
5542    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datesub'
class DateDiff(Func, TimeUnit):
5545class DateDiff(Func, TimeUnit):
5546    _sql_names = ["DATEDIFF", "DATE_DIFF"]
5547    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datediff'
class DateTrunc(Func):
5550class DateTrunc(Func):
5551    arg_types = {"unit": True, "this": True, "zone": False}
5552
5553    def __init__(self, **args):
5554        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5555        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5556        unabbreviate = args.pop("unabbreviate", True)
5557
5558        unit = args.get("unit")
5559        if isinstance(unit, TimeUnit.VAR_LIKE):
5560            unit_name = unit.name.upper()
5561            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5562                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5563
5564            args["unit"] = Literal.string(unit_name)
5565        elif isinstance(unit, Week):
5566            unit.set("this", Literal.string(unit.this.name.upper()))
5567
5568        super().__init__(**args)
5569
5570    @property
5571    def unit(self) -> Expression:
5572        return self.args["unit"]
DateTrunc(**args)
5553    def __init__(self, **args):
5554        # Across most dialects it's safe to unabbreviate the unit (e.g. 'Q' -> 'QUARTER') except Oracle
5555        # https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/ROUND-and-TRUNC-Date-Functions.html
5556        unabbreviate = args.pop("unabbreviate", True)
5557
5558        unit = args.get("unit")
5559        if isinstance(unit, TimeUnit.VAR_LIKE):
5560            unit_name = unit.name.upper()
5561            if unabbreviate and unit_name in TimeUnit.UNABBREVIATED_UNIT_NAME:
5562                unit_name = TimeUnit.UNABBREVIATED_UNIT_NAME[unit_name]
5563
5564            args["unit"] = Literal.string(unit_name)
5565        elif isinstance(unit, Week):
5566            unit.set("this", Literal.string(unit.this.name.upper()))
5567
5568        super().__init__(**args)
arg_types = {'unit': True, 'this': True, 'zone': False}
unit: Expression
5570    @property
5571    def unit(self) -> Expression:
5572        return self.args["unit"]
key = 'datetrunc'
class Datetime(Func):
5577class Datetime(Func):
5578    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'datetime'
class DatetimeAdd(Func, IntervalOp):
5581class DatetimeAdd(Func, IntervalOp):
5582    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimeadd'
class DatetimeSub(Func, IntervalOp):
5585class DatetimeSub(Func, IntervalOp):
5586    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimesub'
class DatetimeDiff(Func, TimeUnit):
5589class DatetimeDiff(Func, TimeUnit):
5590    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimediff'
class DatetimeTrunc(Func, TimeUnit):
5593class DatetimeTrunc(Func, TimeUnit):
5594    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'datetimetrunc'
class DayOfWeek(Func):
5597class DayOfWeek(Func):
5598    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
key = 'dayofweek'
class DayOfWeekIso(Func):
5603class DayOfWeekIso(Func):
5604    _sql_names = ["DAYOFWEEK_ISO", "ISODOW"]
key = 'dayofweekiso'
class DayOfMonth(Func):
5607class DayOfMonth(Func):
5608    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
key = 'dayofmonth'
class DayOfYear(Func):
5611class DayOfYear(Func):
5612    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
key = 'dayofyear'
class ToDays(Func):
5615class ToDays(Func):
5616    pass
key = 'todays'
class WeekOfYear(Func):
5619class WeekOfYear(Func):
5620    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
key = 'weekofyear'
class MonthsBetween(Func):
5623class MonthsBetween(Func):
5624    arg_types = {"this": True, "expression": True, "roundoff": False}
arg_types = {'this': True, 'expression': True, 'roundoff': False}
key = 'monthsbetween'
class MakeInterval(Func):
5627class MakeInterval(Func):
5628    arg_types = {
5629        "year": False,
5630        "month": False,
5631        "day": False,
5632        "hour": False,
5633        "minute": False,
5634        "second": False,
5635    }
arg_types = {'year': False, 'month': False, 'day': False, 'hour': False, 'minute': False, 'second': False}
key = 'makeinterval'
class LastDay(Func, TimeUnit):
5638class LastDay(Func, TimeUnit):
5639    _sql_names = ["LAST_DAY", "LAST_DAY_OF_MONTH"]
5640    arg_types = {"this": True, "unit": False}
arg_types = {'this': True, 'unit': False}
key = 'lastday'
class Extract(Func):
5643class Extract(Func):
5644    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'extract'
class Exists(Func, SubqueryPredicate):
5647class Exists(Func, SubqueryPredicate):
5648    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'exists'
class Timestamp(Func):
5651class Timestamp(Func):
5652    arg_types = {"this": False, "zone": False, "with_tz": False}
arg_types = {'this': False, 'zone': False, 'with_tz': False}
key = 'timestamp'
class TimestampAdd(Func, TimeUnit):
5655class TimestampAdd(Func, TimeUnit):
5656    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampadd'
class TimestampSub(Func, TimeUnit):
5659class TimestampSub(Func, TimeUnit):
5660    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampsub'
class TimestampDiff(Func, TimeUnit):
5663class TimestampDiff(Func, TimeUnit):
5664    _sql_names = ["TIMESTAMPDIFF", "TIMESTAMP_DIFF"]
5665    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampdiff'
class TimestampTrunc(Func, TimeUnit):
5668class TimestampTrunc(Func, TimeUnit):
5669    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timestamptrunc'
class TimeAdd(Func, TimeUnit):
5672class TimeAdd(Func, TimeUnit):
5673    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timeadd'
class TimeSub(Func, TimeUnit):
5676class TimeSub(Func, TimeUnit):
5677    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timesub'
class TimeDiff(Func, TimeUnit):
5680class TimeDiff(Func, TimeUnit):
5681    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timediff'
class TimeTrunc(Func, TimeUnit):
5684class TimeTrunc(Func, TimeUnit):
5685    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timetrunc'
class DateFromParts(Func):
5688class DateFromParts(Func):
5689    _sql_names = ["DATE_FROM_PARTS", "DATEFROMPARTS"]
5690    arg_types = {"year": True, "month": True, "day": True}
arg_types = {'year': True, 'month': True, 'day': True}
key = 'datefromparts'
class TimeFromParts(Func):
5693class TimeFromParts(Func):
5694    _sql_names = ["TIME_FROM_PARTS", "TIMEFROMPARTS"]
5695    arg_types = {
5696        "hour": True,
5697        "min": True,
5698        "sec": True,
5699        "nano": False,
5700        "fractions": False,
5701        "precision": False,
5702    }
arg_types = {'hour': True, 'min': True, 'sec': True, 'nano': False, 'fractions': False, 'precision': False}
key = 'timefromparts'
class DateStrToDate(Func):
5705class DateStrToDate(Func):
5706    pass
key = 'datestrtodate'
class DateToDateStr(Func):
5709class DateToDateStr(Func):
5710    pass
key = 'datetodatestr'
class DateToDi(Func):
5713class DateToDi(Func):
5714    pass
key = 'datetodi'
class Date(Func):
5718class Date(Func):
5719    arg_types = {"this": False, "zone": False, "expressions": False}
5720    is_var_len_args = True
arg_types = {'this': False, 'zone': False, 'expressions': False}
is_var_len_args = True
key = 'date'
class Day(Func):
5723class Day(Func):
5724    pass
key = 'day'
class Decode(Func):
5727class Decode(Func):
5728    arg_types = {"this": True, "charset": True, "replace": False}
arg_types = {'this': True, 'charset': True, 'replace': False}
key = 'decode'
class DiToDate(Func):
5731class DiToDate(Func):
5732    pass
key = 'ditodate'
class Encode(Func):
5735class Encode(Func):
5736    arg_types = {"this": True, "charset": True}
arg_types = {'this': True, 'charset': True}
key = 'encode'
class Exp(Func):
5739class Exp(Func):
5740    pass
key = 'exp'
class Explode(Func, UDTF):
5744class Explode(Func, UDTF):
5745    arg_types = {"this": True, "expressions": False}
5746    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'explode'
class Inline(Func):
5750class Inline(Func):
5751    pass
key = 'inline'
class ExplodeOuter(Explode):
5754class ExplodeOuter(Explode):
5755    pass
key = 'explodeouter'
class Posexplode(Explode):
5758class Posexplode(Explode):
5759    pass
key = 'posexplode'
class PosexplodeOuter(Posexplode, ExplodeOuter):
5762class PosexplodeOuter(Posexplode, ExplodeOuter):
5763    pass
key = 'posexplodeouter'
class Unnest(Func, UDTF):
5766class Unnest(Func, UDTF):
5767    arg_types = {
5768        "expressions": True,
5769        "alias": False,
5770        "offset": False,
5771        "explode_array": False,
5772    }
5773
5774    @property
5775    def selects(self) -> t.List[Expression]:
5776        columns = super().selects
5777        offset = self.args.get("offset")
5778        if offset:
5779            columns = columns + [to_identifier("offset") if offset is True else offset]
5780        return columns
arg_types = {'expressions': True, 'alias': False, 'offset': False, 'explode_array': False}
selects: List[Expression]
5774    @property
5775    def selects(self) -> t.List[Expression]:
5776        columns = super().selects
5777        offset = self.args.get("offset")
5778        if offset:
5779            columns = columns + [to_identifier("offset") if offset is True else offset]
5780        return columns
key = 'unnest'
class Floor(Func):
5783class Floor(Func):
5784    arg_types = {"this": True, "decimals": False}
arg_types = {'this': True, 'decimals': False}
key = 'floor'
class FromBase64(Func):
5787class FromBase64(Func):
5788    pass
key = 'frombase64'
class FeaturesAtTime(Func):
5791class FeaturesAtTime(Func):
5792    arg_types = {"this": True, "time": False, "num_rows": False, "ignore_feature_nulls": False}
arg_types = {'this': True, 'time': False, 'num_rows': False, 'ignore_feature_nulls': False}
key = 'featuresattime'
class ToBase64(Func):
5795class ToBase64(Func):
5796    pass
key = 'tobase64'
class FromISO8601Timestamp(Func):
5800class FromISO8601Timestamp(Func):
5801    _sql_names = ["FROM_ISO8601_TIMESTAMP"]
key = 'fromiso8601timestamp'
class GapFill(Func):
5804class GapFill(Func):
5805    arg_types = {
5806        "this": True,
5807        "ts_column": True,
5808        "bucket_width": True,
5809        "partitioning_columns": False,
5810        "value_columns": False,
5811        "origin": False,
5812        "ignore_nulls": False,
5813    }
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):
5817class GenerateDateArray(Func):
5818    arg_types = {"start": True, "end": True, "step": False}
arg_types = {'start': True, 'end': True, 'step': False}
key = 'generatedatearray'
class GenerateTimestampArray(Func):
5822class GenerateTimestampArray(Func):
5823    arg_types = {"start": True, "end": True, "step": True}
arg_types = {'start': True, 'end': True, 'step': True}
key = 'generatetimestamparray'
class Greatest(Func):
5826class Greatest(Func):
5827    arg_types = {"this": True, "expressions": False}
5828    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'greatest'
class OverflowTruncateBehavior(Expression):
5833class OverflowTruncateBehavior(Expression):
5834    arg_types = {"this": False, "with_count": True}
arg_types = {'this': False, 'with_count': True}
key = 'overflowtruncatebehavior'
class GroupConcat(AggFunc):
5837class GroupConcat(AggFunc):
5838    arg_types = {"this": True, "separator": False, "on_overflow": False}
arg_types = {'this': True, 'separator': False, 'on_overflow': False}
key = 'groupconcat'
class Hex(Func):
5841class Hex(Func):
5842    pass
key = 'hex'
class LowerHex(Hex):
5845class LowerHex(Hex):
5846    pass
key = 'lowerhex'
class Xor(Connector, Func):
5849class Xor(Connector, Func):
5850    arg_types = {"this": False, "expression": False, "expressions": False}
arg_types = {'this': False, 'expression': False, 'expressions': False}
key = 'xor'
class If(Func):
5853class If(Func):
5854    arg_types = {"this": True, "true": True, "false": False}
5855    _sql_names = ["IF", "IIF"]
arg_types = {'this': True, 'true': True, 'false': False}
key = 'if'
class Nullif(Func):
5858class Nullif(Func):
5859    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'nullif'
class Initcap(Func):
5862class Initcap(Func):
5863    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'initcap'
class IsNan(Func):
5866class IsNan(Func):
5867    _sql_names = ["IS_NAN", "ISNAN"]
key = 'isnan'
class Int64(Func):
5871class Int64(Func):
5872    pass
key = 'int64'
class IsInf(Func):
5875class IsInf(Func):
5876    _sql_names = ["IS_INF", "ISINF"]
key = 'isinf'
class JSON(Expression):
5880class JSON(Expression):
5881    arg_types = {"this": False, "with": False, "unique": False}
arg_types = {'this': False, 'with': False, 'unique': False}
key = 'json'
class JSONPath(Expression):
5884class JSONPath(Expression):
5885    arg_types = {"expressions": True, "escape": False}
5886
5887    @property
5888    def output_name(self) -> str:
5889        last_segment = self.expressions[-1].this
5890        return last_segment if isinstance(last_segment, str) else ""
arg_types = {'expressions': True, 'escape': False}
output_name: str
5887    @property
5888    def output_name(self) -> str:
5889        last_segment = self.expressions[-1].this
5890        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):
5893class JSONPathPart(Expression):
5894    arg_types = {}
arg_types = {}
key = 'jsonpathpart'
class JSONPathFilter(JSONPathPart):
5897class JSONPathFilter(JSONPathPart):
5898    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathfilter'
class JSONPathKey(JSONPathPart):
5901class JSONPathKey(JSONPathPart):
5902    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathkey'
class JSONPathRecursive(JSONPathPart):
5905class JSONPathRecursive(JSONPathPart):
5906    arg_types = {"this": False}
arg_types = {'this': False}
key = 'jsonpathrecursive'
class JSONPathRoot(JSONPathPart):
5909class JSONPathRoot(JSONPathPart):
5910    pass
key = 'jsonpathroot'
class JSONPathScript(JSONPathPart):
5913class JSONPathScript(JSONPathPart):
5914    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathscript'
class JSONPathSlice(JSONPathPart):
5917class JSONPathSlice(JSONPathPart):
5918    arg_types = {"start": False, "end": False, "step": False}
arg_types = {'start': False, 'end': False, 'step': False}
key = 'jsonpathslice'
class JSONPathSelector(JSONPathPart):
5921class JSONPathSelector(JSONPathPart):
5922    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathselector'
class JSONPathSubscript(JSONPathPart):
5925class JSONPathSubscript(JSONPathPart):
5926    arg_types = {"this": True}
arg_types = {'this': True}
key = 'jsonpathsubscript'
class JSONPathUnion(JSONPathPart):
5929class JSONPathUnion(JSONPathPart):
5930    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonpathunion'
class JSONPathWildcard(JSONPathPart):
5933class JSONPathWildcard(JSONPathPart):
5934    pass
key = 'jsonpathwildcard'
class FormatJson(Expression):
5937class FormatJson(Expression):
5938    pass
key = 'formatjson'
class JSONKeyValue(Expression):
5941class JSONKeyValue(Expression):
5942    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'jsonkeyvalue'
class JSONObject(Func):
5945class JSONObject(Func):
5946    arg_types = {
5947        "expressions": False,
5948        "null_handling": False,
5949        "unique_keys": False,
5950        "return_type": False,
5951        "encoding": False,
5952    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobject'
class JSONObjectAgg(AggFunc):
5955class JSONObjectAgg(AggFunc):
5956    arg_types = {
5957        "expressions": False,
5958        "null_handling": False,
5959        "unique_keys": False,
5960        "return_type": False,
5961        "encoding": False,
5962    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobjectagg'
class JSONArray(Func):
5966class JSONArray(Func):
5967    arg_types = {
5968        "expressions": True,
5969        "null_handling": False,
5970        "return_type": False,
5971        "strict": False,
5972    }
arg_types = {'expressions': True, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarray'
class JSONArrayAgg(Func):
5976class JSONArrayAgg(Func):
5977    arg_types = {
5978        "this": True,
5979        "order": False,
5980        "null_handling": False,
5981        "return_type": False,
5982        "strict": False,
5983    }
arg_types = {'this': True, 'order': False, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarrayagg'
class JSONExists(Func):
5986class JSONExists(Func):
5987    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):
5992class JSONColumnDef(Expression):
5993    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):
5996class JSONSchema(Expression):
5997    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonschema'
class JSONValue(Expression):
6001class JSONValue(Expression):
6002    arg_types = {
6003        "this": True,
6004        "path": True,
6005        "returning": False,
6006        "on_condition": False,
6007    }
arg_types = {'this': True, 'path': True, 'returning': False, 'on_condition': False}
key = 'jsonvalue'
class JSONValueArray(Func):
6010class JSONValueArray(Func):
6011    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'jsonvaluearray'
class JSONTable(Func):
6015class JSONTable(Func):
6016    arg_types = {
6017        "this": True,
6018        "schema": True,
6019        "path": False,
6020        "error_handling": False,
6021        "empty_handling": False,
6022    }
arg_types = {'this': True, 'schema': True, 'path': False, 'error_handling': False, 'empty_handling': False}
key = 'jsontable'
class ObjectInsert(Func):
6026class ObjectInsert(Func):
6027    arg_types = {
6028        "this": True,
6029        "key": True,
6030        "value": True,
6031        "update_flag": False,
6032    }
arg_types = {'this': True, 'key': True, 'value': True, 'update_flag': False}
key = 'objectinsert'
class OpenJSONColumnDef(Expression):
6035class OpenJSONColumnDef(Expression):
6036    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):
6039class OpenJSON(Func):
6040    arg_types = {"this": True, "path": False, "expressions": False}
arg_types = {'this': True, 'path': False, 'expressions': False}
key = 'openjson'
class JSONBContains(Binary, Func):
6043class JSONBContains(Binary, Func):
6044    _sql_names = ["JSONB_CONTAINS"]
key = 'jsonbcontains'
class JSONBExists(Func):
6047class JSONBExists(Func):
6048    arg_types = {"this": True, "path": True}
6049    _sql_names = ["JSONB_EXISTS"]
arg_types = {'this': True, 'path': True}
key = 'jsonbexists'
class JSONExtract(Binary, Func):
6052class JSONExtract(Binary, Func):
6053    arg_types = {
6054        "this": True,
6055        "expression": True,
6056        "only_json_types": False,
6057        "expressions": False,
6058        "variant_extract": False,
6059        "json_query": False,
6060        "option": False,
6061    }
6062    _sql_names = ["JSON_EXTRACT"]
6063    is_var_len_args = True
6064
6065    @property
6066    def output_name(self) -> str:
6067        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
6065    @property
6066    def output_name(self) -> str:
6067        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 JSONExtractArray(Func):
6070class JSONExtractArray(Func):
6071    arg_types = {"this": True, "expression": False}
6072    _sql_names = ["JSON_EXTRACT_ARRAY"]
arg_types = {'this': True, 'expression': False}
key = 'jsonextractarray'
class JSONExtractScalar(Binary, Func):
6075class JSONExtractScalar(Binary, Func):
6076    arg_types = {"this": True, "expression": True, "only_json_types": False, "expressions": False}
6077    _sql_names = ["JSON_EXTRACT_SCALAR"]
6078    is_var_len_args = True
6079
6080    @property
6081    def output_name(self) -> str:
6082        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
6080    @property
6081    def output_name(self) -> str:
6082        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):
6085class JSONBExtract(Binary, Func):
6086    _sql_names = ["JSONB_EXTRACT"]
key = 'jsonbextract'
class JSONBExtractScalar(Binary, Func):
6089class JSONBExtractScalar(Binary, Func):
6090    _sql_names = ["JSONB_EXTRACT_SCALAR"]
key = 'jsonbextractscalar'
class JSONFormat(Func):
6093class JSONFormat(Func):
6094    arg_types = {"this": False, "options": False}
6095    _sql_names = ["JSON_FORMAT"]
arg_types = {'this': False, 'options': False}
key = 'jsonformat'
class JSONArrayContains(Binary, Predicate, Func):
6099class JSONArrayContains(Binary, Predicate, Func):
6100    _sql_names = ["JSON_ARRAY_CONTAINS"]
key = 'jsonarraycontains'
class ParseJSON(Func):
6103class ParseJSON(Func):
6104    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
6105    # Snowflake also has TRY_PARSE_JSON, which is represented using `safe`
6106    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
6107    arg_types = {"this": True, "expression": False, "safe": False}
arg_types = {'this': True, 'expression': False, 'safe': False}
key = 'parsejson'
class Least(Func):
6110class Least(Func):
6111    arg_types = {"this": True, "expressions": False}
6112    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'least'
class Left(Func):
6115class Left(Func):
6116    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'left'
class Length(Func):
6123class Length(Func):
6124    arg_types = {"this": True, "binary": False}
6125    _sql_names = ["LENGTH", "LEN"]
arg_types = {'this': True, 'binary': False}
key = 'length'
class Levenshtein(Func):
6128class Levenshtein(Func):
6129    arg_types = {
6130        "this": True,
6131        "expression": False,
6132        "ins_cost": False,
6133        "del_cost": False,
6134        "sub_cost": False,
6135        "max_dist": False,
6136    }
arg_types = {'this': True, 'expression': False, 'ins_cost': False, 'del_cost': False, 'sub_cost': False, 'max_dist': False}
key = 'levenshtein'
class Ln(Func):
6139class Ln(Func):
6140    pass
key = 'ln'
class Log(Func):
6143class Log(Func):
6144    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'log'
class LogicalOr(AggFunc):
6147class LogicalOr(AggFunc):
6148    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
key = 'logicalor'
class LogicalAnd(AggFunc):
6151class LogicalAnd(AggFunc):
6152    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
key = 'logicaland'
class Lower(Func):
6155class Lower(Func):
6156    _sql_names = ["LOWER", "LCASE"]
key = 'lower'
class Map(Func):
6159class Map(Func):
6160    arg_types = {"keys": False, "values": False}
6161
6162    @property
6163    def keys(self) -> t.List[Expression]:
6164        keys = self.args.get("keys")
6165        return keys.expressions if keys else []
6166
6167    @property
6168    def values(self) -> t.List[Expression]:
6169        values = self.args.get("values")
6170        return values.expressions if values else []
arg_types = {'keys': False, 'values': False}
keys: List[Expression]
6162    @property
6163    def keys(self) -> t.List[Expression]:
6164        keys = self.args.get("keys")
6165        return keys.expressions if keys else []
values: List[Expression]
6167    @property
6168    def values(self) -> t.List[Expression]:
6169        values = self.args.get("values")
6170        return values.expressions if values else []
key = 'map'
class ToMap(Func):
6174class ToMap(Func):
6175    pass
key = 'tomap'
class MapFromEntries(Func):
6178class MapFromEntries(Func):
6179    pass
key = 'mapfromentries'
class ScopeResolution(Expression):
6183class ScopeResolution(Expression):
6184    arg_types = {"this": False, "expression": True}
arg_types = {'this': False, 'expression': True}
key = 'scoperesolution'
class Stream(Expression):
6187class Stream(Expression):
6188    pass
key = 'stream'
class StarMap(Func):
6191class StarMap(Func):
6192    pass
key = 'starmap'
class VarMap(Func):
6195class VarMap(Func):
6196    arg_types = {"keys": True, "values": True}
6197    is_var_len_args = True
6198
6199    @property
6200    def keys(self) -> t.List[Expression]:
6201        return self.args["keys"].expressions
6202
6203    @property
6204    def values(self) -> t.List[Expression]:
6205        return self.args["values"].expressions
arg_types = {'keys': True, 'values': True}
is_var_len_args = True
keys: List[Expression]
6199    @property
6200    def keys(self) -> t.List[Expression]:
6201        return self.args["keys"].expressions
values: List[Expression]
6203    @property
6204    def values(self) -> t.List[Expression]:
6205        return self.args["values"].expressions
key = 'varmap'
class MatchAgainst(Func):
6209class MatchAgainst(Func):
6210    arg_types = {"this": True, "expressions": True, "modifier": False}
arg_types = {'this': True, 'expressions': True, 'modifier': False}
key = 'matchagainst'
class Max(AggFunc):
6213class Max(AggFunc):
6214    arg_types = {"this": True, "expressions": False}
6215    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'max'
class MD5(Func):
6218class MD5(Func):
6219    _sql_names = ["MD5"]
key = 'md5'
class MD5Digest(Func):
6223class MD5Digest(Func):
6224    _sql_names = ["MD5_DIGEST"]
key = 'md5digest'
class Median(AggFunc):
6227class Median(AggFunc):
6228    pass
key = 'median'
class Min(AggFunc):
6231class Min(AggFunc):
6232    arg_types = {"this": True, "expressions": False}
6233    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'min'
class Month(Func):
6236class Month(Func):
6237    pass
key = 'month'
class AddMonths(Func):
6240class AddMonths(Func):
6241    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'addmonths'
class Nvl2(Func):
6244class Nvl2(Func):
6245    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'nvl2'
class Normalize(Func):
6248class Normalize(Func):
6249    arg_types = {"this": True, "form": False}
arg_types = {'this': True, 'form': False}
key = 'normalize'
class Overlay(Func):
6252class Overlay(Func):
6253    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):
6257class Predict(Func):
6258    arg_types = {"this": True, "expression": True, "params_struct": False}
arg_types = {'this': True, 'expression': True, 'params_struct': False}
key = 'predict'
class Pow(Binary, Func):
6261class Pow(Binary, Func):
6262    _sql_names = ["POWER", "POW"]
key = 'pow'
class PercentileCont(AggFunc):
6265class PercentileCont(AggFunc):
6266    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentilecont'
class PercentileDisc(AggFunc):
6269class PercentileDisc(AggFunc):
6270    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentiledisc'
class Quantile(AggFunc):
6273class Quantile(AggFunc):
6274    arg_types = {"this": True, "quantile": True}
arg_types = {'this': True, 'quantile': True}
key = 'quantile'
class ApproxQuantile(Quantile):
6277class ApproxQuantile(Quantile):
6278    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):
6281class Quarter(Func):
6282    pass
key = 'quarter'
class Rand(Func):
6287class Rand(Func):
6288    _sql_names = ["RAND", "RANDOM"]
6289    arg_types = {"this": False, "lower": False, "upper": False}
arg_types = {'this': False, 'lower': False, 'upper': False}
key = 'rand'
class Randn(Func):
6292class Randn(Func):
6293    arg_types = {"this": False}
arg_types = {'this': False}
key = 'randn'
class RangeN(Func):
6296class RangeN(Func):
6297    arg_types = {"this": True, "expressions": True, "each": False}
arg_types = {'this': True, 'expressions': True, 'each': False}
key = 'rangen'
class ReadCSV(Func):
6300class ReadCSV(Func):
6301    _sql_names = ["READ_CSV"]
6302    is_var_len_args = True
6303    arg_types = {"this": True, "expressions": False}
is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
key = 'readcsv'
class Reduce(Func):
6306class Reduce(Func):
6307    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):
6310class RegexpExtract(Func):
6311    arg_types = {
6312        "this": True,
6313        "expression": True,
6314        "position": False,
6315        "occurrence": False,
6316        "parameters": False,
6317        "group": False,
6318    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextract'
class RegexpExtractAll(Func):
6321class RegexpExtractAll(Func):
6322    arg_types = {
6323        "this": True,
6324        "expression": True,
6325        "position": False,
6326        "occurrence": False,
6327        "parameters": False,
6328        "group": False,
6329    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextractall'
class RegexpReplace(Func):
6332class RegexpReplace(Func):
6333    arg_types = {
6334        "this": True,
6335        "expression": True,
6336        "replacement": False,
6337        "position": False,
6338        "occurrence": False,
6339        "modifiers": False,
6340    }
arg_types = {'this': True, 'expression': True, 'replacement': False, 'position': False, 'occurrence': False, 'modifiers': False}
key = 'regexpreplace'
class RegexpLike(Binary, Func):
6343class RegexpLike(Binary, Func):
6344    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexplike'
class RegexpILike(Binary, Func):
6347class RegexpILike(Binary, Func):
6348    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexpilike'
class RegexpSplit(Func):
6353class RegexpSplit(Func):
6354    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'regexpsplit'
class Repeat(Func):
6357class Repeat(Func):
6358    arg_types = {"this": True, "times": True}
arg_types = {'this': True, 'times': True}
key = 'repeat'
class Round(Func):
6363class Round(Func):
6364    arg_types = {"this": True, "decimals": False, "truncate": False}
arg_types = {'this': True, 'decimals': False, 'truncate': False}
key = 'round'
class RowNumber(Func):
6367class RowNumber(Func):
6368    arg_types = {"this": False}
arg_types = {'this': False}
key = 'rownumber'
class SafeDivide(Func):
6371class SafeDivide(Func):
6372    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'safedivide'
class SHA(Func):
6375class SHA(Func):
6376    _sql_names = ["SHA", "SHA1"]
key = 'sha'
class SHA2(Func):
6379class SHA2(Func):
6380    _sql_names = ["SHA2"]
6381    arg_types = {"this": True, "length": False}
arg_types = {'this': True, 'length': False}
key = 'sha2'
class Sign(Func):
6384class Sign(Func):
6385    _sql_names = ["SIGN", "SIGNUM"]
key = 'sign'
class SortArray(Func):
6388class SortArray(Func):
6389    arg_types = {"this": True, "asc": False}
arg_types = {'this': True, 'asc': False}
key = 'sortarray'
class Split(Func):
6392class Split(Func):
6393    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'split'
class SplitPart(Func):
6397class SplitPart(Func):
6398    arg_types = {"this": True, "delimiter": True, "part_index": True}
arg_types = {'this': True, 'delimiter': True, 'part_index': True}
key = 'splitpart'
class Substring(Func):
6403class Substring(Func):
6404    _sql_names = ["SUBSTRING", "SUBSTR"]
6405    arg_types = {"this": True, "start": False, "length": False}
arg_types = {'this': True, 'start': False, 'length': False}
key = 'substring'
class StandardHash(Func):
6408class StandardHash(Func):
6409    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'standardhash'
class StartsWith(Func):
6412class StartsWith(Func):
6413    _sql_names = ["STARTS_WITH", "STARTSWITH"]
6414    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'startswith'
class StrPosition(Func):
6417class StrPosition(Func):
6418    arg_types = {
6419        "this": True,
6420        "substr": True,
6421        "position": False,
6422        "instance": False,
6423    }
arg_types = {'this': True, 'substr': True, 'position': False, 'instance': False}
key = 'strposition'
class StrToDate(Func):
6426class StrToDate(Func):
6427    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'strtodate'
class StrToTime(Func):
6430class StrToTime(Func):
6431    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):
6436class StrToUnix(Func):
6437    arg_types = {"this": False, "format": False}
arg_types = {'this': False, 'format': False}
key = 'strtounix'
class StrToMap(Func):
6442class StrToMap(Func):
6443    arg_types = {
6444        "this": True,
6445        "pair_delim": False,
6446        "key_value_delim": False,
6447        "duplicate_resolution_callback": False,
6448    }
arg_types = {'this': True, 'pair_delim': False, 'key_value_delim': False, 'duplicate_resolution_callback': False}
key = 'strtomap'
class NumberToStr(Func):
6451class NumberToStr(Func):
6452    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'numbertostr'
class FromBase(Func):
6455class FromBase(Func):
6456    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'frombase'
class Struct(Func):
6459class Struct(Func):
6460    arg_types = {"expressions": False}
6461    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'struct'
class StructExtract(Func):
6464class StructExtract(Func):
6465    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'structextract'
class Stuff(Func):
6470class Stuff(Func):
6471    _sql_names = ["STUFF", "INSERT"]
6472    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):
6475class Sum(AggFunc):
6476    pass
key = 'sum'
class Sqrt(Func):
6479class Sqrt(Func):
6480    pass
key = 'sqrt'
class Stddev(AggFunc):
6483class Stddev(AggFunc):
6484    _sql_names = ["STDDEV", "STDEV"]
key = 'stddev'
class StddevPop(AggFunc):
6487class StddevPop(AggFunc):
6488    pass
key = 'stddevpop'
class StddevSamp(AggFunc):
6491class StddevSamp(AggFunc):
6492    pass
key = 'stddevsamp'
class Time(Func):
6496class Time(Func):
6497    arg_types = {"this": False, "zone": False}
arg_types = {'this': False, 'zone': False}
key = 'time'
class TimeToStr(Func):
6500class TimeToStr(Func):
6501    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):
6504class TimeToTimeStr(Func):
6505    pass
key = 'timetotimestr'
class TimeToUnix(Func):
6508class TimeToUnix(Func):
6509    pass
key = 'timetounix'
class TimeStrToDate(Func):
6512class TimeStrToDate(Func):
6513    pass
key = 'timestrtodate'
class TimeStrToTime(Func):
6516class TimeStrToTime(Func):
6517    arg_types = {"this": True, "zone": False}
arg_types = {'this': True, 'zone': False}
key = 'timestrtotime'
class TimeStrToUnix(Func):
6520class TimeStrToUnix(Func):
6521    pass
key = 'timestrtounix'
class Trim(Func):
6524class Trim(Func):
6525    arg_types = {
6526        "this": True,
6527        "expression": False,
6528        "position": False,
6529        "collation": False,
6530    }
arg_types = {'this': True, 'expression': False, 'position': False, 'collation': False}
key = 'trim'
class TsOrDsAdd(Func, TimeUnit):
6533class TsOrDsAdd(Func, TimeUnit):
6534    # return_type is used to correctly cast the arguments of this expression when transpiling it
6535    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
6536
6537    @property
6538    def return_type(self) -> DataType:
6539        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
6537    @property
6538    def return_type(self) -> DataType:
6539        return DataType.build(self.args.get("return_type") or DataType.Type.DATE)
key = 'tsordsadd'
class TsOrDsDiff(Func, TimeUnit):
6542class TsOrDsDiff(Func, TimeUnit):
6543    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'tsordsdiff'
class TsOrDsToDateStr(Func):
6546class TsOrDsToDateStr(Func):
6547    pass
key = 'tsordstodatestr'
class TsOrDsToDate(Func):
6550class TsOrDsToDate(Func):
6551    arg_types = {"this": True, "format": False, "safe": False}
arg_types = {'this': True, 'format': False, 'safe': False}
key = 'tsordstodate'
class TsOrDsToDatetime(Func):
6554class TsOrDsToDatetime(Func):
6555    pass
key = 'tsordstodatetime'
class TsOrDsToTime(Func):
6558class TsOrDsToTime(Func):
6559    pass
key = 'tsordstotime'
class TsOrDsToTimestamp(Func):
6562class TsOrDsToTimestamp(Func):
6563    pass
key = 'tsordstotimestamp'
class TsOrDiToDi(Func):
6566class TsOrDiToDi(Func):
6567    pass
key = 'tsorditodi'
class Unhex(Func):
6570class Unhex(Func):
6571    pass
key = 'unhex'
class UnixDate(Func):
6575class UnixDate(Func):
6576    pass
key = 'unixdate'
class UnixToStr(Func):
6579class UnixToStr(Func):
6580    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'unixtostr'
class UnixToTime(Func):
6585class UnixToTime(Func):
6586    arg_types = {
6587        "this": True,
6588        "scale": False,
6589        "zone": False,
6590        "hours": False,
6591        "minutes": False,
6592        "format": False,
6593    }
6594
6595    SECONDS = Literal.number(0)
6596    DECIS = Literal.number(1)
6597    CENTIS = Literal.number(2)
6598    MILLIS = Literal.number(3)
6599    DECIMILLIS = Literal.number(4)
6600    CENTIMILLIS = Literal.number(5)
6601    MICROS = Literal.number(6)
6602    DECIMICROS = Literal.number(7)
6603    CENTIMICROS = Literal.number(8)
6604    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):
6607class UnixToTimeStr(Func):
6608    pass
key = 'unixtotimestr'
class UnixSeconds(Func):
6611class UnixSeconds(Func):
6612    pass
key = 'unixseconds'
class Uuid(Func):
6615class Uuid(Func):
6616    _sql_names = ["UUID", "GEN_RANDOM_UUID", "GENERATE_UUID", "UUID_STRING"]
6617
6618    arg_types = {"this": False, "name": False}
arg_types = {'this': False, 'name': False}
key = 'uuid'
class TimestampFromParts(Func):
6621class TimestampFromParts(Func):
6622    _sql_names = ["TIMESTAMP_FROM_PARTS", "TIMESTAMPFROMPARTS"]
6623    arg_types = {
6624        "year": True,
6625        "month": True,
6626        "day": True,
6627        "hour": True,
6628        "min": True,
6629        "sec": True,
6630        "nano": False,
6631        "zone": False,
6632        "milli": False,
6633    }
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):
6636class Upper(Func):
6637    _sql_names = ["UPPER", "UCASE"]
key = 'upper'
class Corr(Binary, AggFunc):
6640class Corr(Binary, AggFunc):
6641    pass
key = 'corr'
class Variance(AggFunc):
6644class Variance(AggFunc):
6645    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
key = 'variance'
class VariancePop(AggFunc):
6648class VariancePop(AggFunc):
6649    _sql_names = ["VARIANCE_POP", "VAR_POP"]
key = 'variancepop'
class CovarSamp(Binary, AggFunc):
6652class CovarSamp(Binary, AggFunc):
6653    pass
key = 'covarsamp'
class CovarPop(Binary, AggFunc):
6656class CovarPop(Binary, AggFunc):
6657    pass
key = 'covarpop'
class Week(Func):
6660class Week(Func):
6661    arg_types = {"this": True, "mode": False}
arg_types = {'this': True, 'mode': False}
key = 'week'
class XMLTable(Func):
6664class XMLTable(Func):
6665    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):
6668class Year(Func):
6669    pass
key = 'year'
class Use(Expression):
6672class Use(Expression):
6673    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'use'
class Merge(DML):
6676class Merge(DML):
6677    arg_types = {
6678        "this": True,
6679        "using": True,
6680        "on": True,
6681        "expressions": True,
6682        "with": False,
6683        "returning": False,
6684    }
arg_types = {'this': True, 'using': True, 'on': True, 'expressions': True, 'with': False, 'returning': False}
key = 'merge'
class When(Func):
6687class When(Func):
6688    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):
6693class NextValueFor(Func):
6694    arg_types = {"this": True, "order": False}
arg_types = {'this': True, 'order': False}
key = 'nextvaluefor'
class Semicolon(Expression):
6699class Semicolon(Expression):
6700    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 'Contains'>, <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 'FeaturesAtTime'>, <class 'First'>, <class 'FirstValue'>, <class 'Flatten'>, <class 'Floor'>, <class 'FromBase'>, <class 'FromBase64'>, <class 'FromISO8601Timestamp'>, <class 'GapFill'>, <class 'GenerateDateArray'>, <class 'GenerateSeries'>, <class 'GenerateTimestampArray'>, <class 'Greatest'>, <class 'GroupConcat'>, <class 'Hex'>, <class 'Hll'>, <class 'If'>, <class 'Initcap'>, <class 'Inline'>, <class 'Int64'>, <class 'IsInf'>, <class 'IsNan'>, <class 'JSONArray'>, <class 'JSONArrayAgg'>, <class 'JSONArrayContains'>, <class 'JSONBContains'>, <class 'JSONBExists'>, <class 'JSONBExtract'>, <class 'JSONBExtractScalar'>, <class 'JSONExists'>, <class 'JSONExtract'>, <class 'JSONExtractArray'>, <class 'JSONExtractScalar'>, <class 'JSONFormat'>, <class 'JSONObject'>, <class 'JSONObjectAgg'>, <class 'JSONTable'>, <class 'JSONValueArray'>, <class 'Lag'>, <class 'Last'>, <class 'LastDay'>, <class 'LastValue'>, <class 'Lead'>, <class 'Least'>, <class 'Left'>, <class 'Length'>, <class 'Levenshtein'>, <class 'List'>, <class 'Ln'>, <class 'Log'>, <class 'LogicalAnd'>, <class 'LogicalOr'>, <class 'Lower'>, <class 'LowerHex'>, <class 'MD5'>, <class 'MD5Digest'>, <class 'MakeInterval'>, <class 'Map'>, <class 'MapFromEntries'>, <class 'MatchAgainst'>, <class 'Max'>, <class 'Median'>, <class 'Min'>, <class 'Month'>, <class 'MonthsBetween'>, <class 'NextValueFor'>, <class 'Normalize'>, <class 'NthValue'>, <class 'Nullif'>, <class 'NumberToStr'>, <class 'Nvl2'>, <class 'ObjectInsert'>, <class 'OpenJSON'>, <class 'Overlay'>, <class 'Pad'>, <class 'ParameterizedAgg'>, <class 'ParseJSON'>, <class 'PercentileCont'>, <class 'PercentileDisc'>, <class 'Posexplode'>, <class 'PosexplodeOuter'>, <class 'Pow'>, <class 'Predict'>, <class 'Quantile'>, <class 'Quarter'>, <class 'Rand'>, <class 'Randn'>, <class 'RangeN'>, <class 'ReadCSV'>, <class 'Reduce'>, <class 'RegexpExtract'>, <class 'RegexpExtractAll'>, <class 'RegexpILike'>, <class 'RegexpLike'>, <class 'RegexpReplace'>, <class 'RegexpSplit'>, <class 'Repeat'>, <class 'Right'>, <class 'Round'>, <class 'RowNumber'>, <class 'SHA'>, <class 'SHA2'>, <class 'SafeDivide'>, <class 'Sign'>, <class 'SortArray'>, <class 'Split'>, <class 'SplitPart'>, <class 'Sqrt'>, <class 'StandardHash'>, <class 'StarMap'>, <class 'StartsWith'>, <class 'Stddev'>, <class 'StddevPop'>, <class 'StddevSamp'>, <class 'StrPosition'>, <class 'StrToDate'>, <class 'StrToMap'>, <class 'StrToTime'>, <class 'StrToUnix'>, <class 'String'>, <class 'StringToArray'>, <class 'Struct'>, <class 'StructExtract'>, <class 'Stuff'>, <class 'Substring'>, <class 'Sum'>, <class 'Time'>, <class 'TimeAdd'>, <class 'TimeDiff'>, <class 'TimeFromParts'>, <class 'TimeStrToDate'>, <class 'TimeStrToTime'>, <class 'TimeStrToUnix'>, <class 'TimeSub'>, <class 'TimeToStr'>, <class 'TimeToTimeStr'>, <class 'TimeToUnix'>, <class 'TimeTrunc'>, <class 'Timestamp'>, <class 'TimestampAdd'>, <class 'TimestampDiff'>, <class 'TimestampFromParts'>, <class 'TimestampSub'>, <class 'TimestampTrunc'>, <class 'ToArray'>, <class 'ToBase64'>, <class 'ToChar'>, <class 'ToDays'>, <class 'ToDouble'>, <class 'ToMap'>, <class 'ToNumber'>, <class 'Transform'>, <class 'Trim'>, <class 'Try'>, <class 'TryCast'>, <class 'TsOrDiToDi'>, <class 'TsOrDsAdd'>, <class 'TsOrDsDiff'>, <class 'TsOrDsToDate'>, <class 'TsOrDsToDateStr'>, <class 'TsOrDsToDatetime'>, <class 'TsOrDsToTime'>, <class 'TsOrDsToTimestamp'>, <class 'Unhex'>, <class 'UnixDate'>, <class 'UnixSeconds'>, <class 'UnixToStr'>, <class 'UnixToTime'>, <class 'UnixToTimeStr'>, <class 'Unnest'>, <class 'Upper'>, <class 'Uuid'>, <class 'VarMap'>, <class 'Variance'>, <class 'VariancePop'>, <class 'Week'>, <class 'WeekOfYear'>, <class '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'>, 'CONTAINS': <class 'Contains'>, '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'>, 'FEATURES_AT_TIME': <class 'FeaturesAtTime'>, 'FIRST': <class 'First'>, 'FIRST_VALUE': <class 'FirstValue'>, 'FLATTEN': <class 'Flatten'>, 'FLOOR': <class 'Floor'>, 'FROM_BASE': <class 'FromBase'>, 'FROM_BASE64': <class 'FromBase64'>, 'FROM_ISO8601_TIMESTAMP': <class 'FromISO8601Timestamp'>, 'GAP_FILL': <class 'GapFill'>, 'GENERATE_DATE_ARRAY': <class 'GenerateDateArray'>, 'GENERATE_SERIES': <class 'GenerateSeries'>, 'GENERATE_TIMESTAMP_ARRAY': <class 'GenerateTimestampArray'>, 'GREATEST': <class 'Greatest'>, 'GROUP_CONCAT': <class 'GroupConcat'>, 'HEX': <class 'Hex'>, 'HLL': <class 'Hll'>, 'IF': <class 'If'>, 'IIF': <class 'If'>, 'INITCAP': <class 'Initcap'>, 'INLINE': <class 'Inline'>, 'INT64': <class 'Int64'>, 'IS_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_ARRAY': <class 'JSONExtractArray'>, 'JSON_EXTRACT_SCALAR': <class 'JSONExtractScalar'>, 'JSON_FORMAT': <class 'JSONFormat'>, 'J_S_O_N_OBJECT': <class 'JSONObject'>, 'J_S_O_N_OBJECT_AGG': <class 'JSONObjectAgg'>, 'J_S_O_N_TABLE': <class 'JSONTable'>, 'J_S_O_N_VALUE_ARRAY': <class 'JSONValueArray'>, 'LAG': <class 'Lag'>, 'LAST': <class 'Last'>, 'LAST_DAY': <class 'LastDay'>, 'LAST_DAY_OF_MONTH': <class 'LastDay'>, 'LAST_VALUE': <class 'LastValue'>, 'LEAD': <class 'Lead'>, 'LEAST': <class 'Least'>, 'LEFT': <class 'Left'>, 'LENGTH': <class 'Length'>, 'LEN': <class 'Length'>, 'LEVENSHTEIN': <class 'Levenshtein'>, 'LIST': <class 'List'>, 'LN': <class 'Ln'>, 'LOG': <class 'Log'>, 'LOGICAL_AND': <class 'LogicalAnd'>, 'BOOL_AND': <class 'LogicalAnd'>, 'BOOLAND_AGG': <class 'LogicalAnd'>, 'LOGICAL_OR': <class 'LogicalOr'>, 'BOOL_OR': <class 'LogicalOr'>, 'BOOLOR_AGG': <class 'LogicalOr'>, 'LOWER': <class 'Lower'>, 'LCASE': <class 'Lower'>, 'LOWER_HEX': <class 'LowerHex'>, 'MD5': <class 'MD5'>, 'MD5_DIGEST': <class 'MD5Digest'>, 'MAKE_INTERVAL': <class 'MakeInterval'>, 'MAP': <class 'Map'>, 'MAP_FROM_ENTRIES': <class 'MapFromEntries'>, 'MATCH_AGAINST': <class 'MatchAgainst'>, 'MAX': <class 'Max'>, 'MEDIAN': <class 'Median'>, 'MIN': <class 'Min'>, 'MONTH': <class 'Month'>, 'MONTHS_BETWEEN': <class 'MonthsBetween'>, 'NEXT_VALUE_FOR': <class 'NextValueFor'>, 'NORMALIZE': <class 'Normalize'>, 'NTH_VALUE': <class 'NthValue'>, 'NULLIF': <class 'Nullif'>, 'NUMBER_TO_STR': <class 'NumberToStr'>, 'NVL2': <class 'Nvl2'>, 'OBJECT_INSERT': <class 'ObjectInsert'>, 'OPEN_J_S_O_N': <class 'OpenJSON'>, 'OVERLAY': <class 'Overlay'>, 'PAD': <class 'Pad'>, 'PARAMETERIZED_AGG': <class 'ParameterizedAgg'>, 'PARSE_JSON': <class 'ParseJSON'>, 'JSON_PARSE': <class 'ParseJSON'>, 'PERCENTILE_CONT': <class 'PercentileCont'>, 'PERCENTILE_DISC': <class 'PercentileDisc'>, 'POSEXPLODE': <class 'Posexplode'>, 'POSEXPLODE_OUTER': <class 'PosexplodeOuter'>, 'POWER': <class 'Pow'>, 'POW': <class 'Pow'>, 'PREDICT': <class 'Predict'>, 'QUANTILE': <class 'Quantile'>, 'QUARTER': <class 'Quarter'>, 'RAND': <class 'Rand'>, 'RANDOM': <class 'Rand'>, 'RANDN': <class 'Randn'>, 'RANGE_N': <class 'RangeN'>, 'READ_CSV': <class 'ReadCSV'>, 'REDUCE': <class 'Reduce'>, 'REGEXP_EXTRACT': <class 'RegexpExtract'>, 'REGEXP_EXTRACT_ALL': <class 'RegexpExtractAll'>, 'REGEXP_I_LIKE': <class 'RegexpILike'>, 'REGEXP_LIKE': <class 'RegexpLike'>, 'REGEXP_REPLACE': <class 'RegexpReplace'>, 'REGEXP_SPLIT': <class 'RegexpSplit'>, 'REPEAT': <class 'Repeat'>, 'RIGHT': <class 'Right'>, 'ROUND': <class 'Round'>, 'ROW_NUMBER': <class 'RowNumber'>, 'SHA': <class 'SHA'>, 'SHA1': <class 'SHA'>, 'SHA2': <class 'SHA2'>, 'SAFE_DIVIDE': <class 'SafeDivide'>, 'SIGN': <class 'Sign'>, 'SIGNUM': <class 'Sign'>, 'SORT_ARRAY': <class 'SortArray'>, 'SPLIT': <class 'Split'>, 'SPLIT_PART': <class 'SplitPart'>, 'SQRT': <class 'Sqrt'>, '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_DATETIME': <class 'TsOrDsToDatetime'>, 'TS_OR_DS_TO_TIME': <class 'TsOrDsToTime'>, 'TS_OR_DS_TO_TIMESTAMP': <class 'TsOrDsToTimestamp'>, 'UNHEX': <class 'Unhex'>, 'UNIX_DATE': <class 'UnixDate'>, 'UNIX_SECONDS': <class 'UnixSeconds'>, 'UNIX_TO_STR': <class 'UnixToStr'>, 'UNIX_TO_TIME': <class 'UnixToTime'>, 'UNIX_TO_TIME_STR': <class 'UnixToTimeStr'>, 'UNNEST': <class 'Unnest'>, 'UPPER': <class 'Upper'>, 'UCASE': <class 'Upper'>, 'UUID': <class 'Uuid'>, 'GEN_RANDOM_UUID': <class 'Uuid'>, 'GENERATE_UUID': <class 'Uuid'>, 'UUID_STRING': <class 'Uuid'>, 'VAR_MAP': <class 'VarMap'>, 'VARIANCE': <class 'Variance'>, 'VARIANCE_SAMP': <class 'Variance'>, 'VAR_SAMP': <class 'Variance'>, 'VARIANCE_POP': <class 'VariancePop'>, 'VAR_POP': <class 'VariancePop'>, 'WEEK': <class 'Week'>, 'WEEK_OF_YEAR': <class 'WeekOfYear'>, 'WEEKOFYEAR': <class 'WeekOfYear'>, '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:
6740def maybe_parse(
6741    sql_or_expression: ExpOrStr,
6742    *,
6743    into: t.Optional[IntoType] = None,
6744    dialect: DialectType = None,
6745    prefix: t.Optional[str] = None,
6746    copy: bool = False,
6747    **opts,
6748) -> Expression:
6749    """Gracefully handle a possible string or expression.
6750
6751    Example:
6752        >>> maybe_parse("1")
6753        Literal(this=1, is_string=False)
6754        >>> maybe_parse(to_identifier("x"))
6755        Identifier(this=x, quoted=False)
6756
6757    Args:
6758        sql_or_expression: the SQL code string or an expression
6759        into: the SQLGlot Expression to parse into
6760        dialect: the dialect used to parse the input expressions (in the case that an
6761            input expression is a SQL string).
6762        prefix: a string to prefix the sql with before it gets parsed
6763            (automatically includes a space)
6764        copy: whether to copy the expression.
6765        **opts: other options to use to parse the input expressions (again, in the case
6766            that an input expression is a SQL string).
6767
6768    Returns:
6769        Expression: the parsed or given expression.
6770    """
6771    if isinstance(sql_or_expression, Expression):
6772        if copy:
6773            return sql_or_expression.copy()
6774        return sql_or_expression
6775
6776    if sql_or_expression is None:
6777        raise ParseError("SQL cannot be None")
6778
6779    import sqlglot
6780
6781    sql = str(sql_or_expression)
6782    if prefix:
6783        sql = f"{prefix} {sql}"
6784
6785    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):
6796def maybe_copy(instance, copy=True):
6797    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:
7041def union(
7042    *expressions: ExpOrStr,
7043    distinct: bool = True,
7044    dialect: DialectType = None,
7045    copy: bool = True,
7046    **opts,
7047) -> Union:
7048    """
7049    Initializes a syntax tree for the `UNION` operation.
7050
7051    Example:
7052        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
7053        'SELECT * FROM foo UNION SELECT * FROM bla'
7054
7055    Args:
7056        expressions: the SQL code strings, corresponding to the `UNION`'s operands.
7057            If `Expression` instances are passed, they will be used as-is.
7058        distinct: set the DISTINCT flag if and only if this is true.
7059        dialect: the dialect used to parse the input expression.
7060        copy: whether to copy the expression.
7061        opts: other options to use to parse the input expressions.
7062
7063    Returns:
7064        The new Union instance.
7065    """
7066    assert len(expressions) >= 2, "At least two expressions are required by `union`."
7067    return _apply_set_operation(
7068        *expressions, set_operation=Union, distinct=distinct, dialect=dialect, copy=copy, **opts
7069    )

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:
7072def intersect(
7073    *expressions: ExpOrStr,
7074    distinct: bool = True,
7075    dialect: DialectType = None,
7076    copy: bool = True,
7077    **opts,
7078) -> Intersect:
7079    """
7080    Initializes a syntax tree for the `INTERSECT` operation.
7081
7082    Example:
7083        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
7084        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
7085
7086    Args:
7087        expressions: the SQL code strings, corresponding to the `INTERSECT`'s operands.
7088            If `Expression` instances are passed, they will be used as-is.
7089        distinct: set the DISTINCT flag if and only if this is true.
7090        dialect: the dialect used to parse the input expression.
7091        copy: whether to copy the expression.
7092        opts: other options to use to parse the input expressions.
7093
7094    Returns:
7095        The new Intersect instance.
7096    """
7097    assert len(expressions) >= 2, "At least two expressions are required by `intersect`."
7098    return _apply_set_operation(
7099        *expressions, set_operation=Intersect, distinct=distinct, dialect=dialect, copy=copy, **opts
7100    )

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:
7103def except_(
7104    *expressions: ExpOrStr,
7105    distinct: bool = True,
7106    dialect: DialectType = None,
7107    copy: bool = True,
7108    **opts,
7109) -> Except:
7110    """
7111    Initializes a syntax tree for the `EXCEPT` operation.
7112
7113    Example:
7114        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
7115        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
7116
7117    Args:
7118        expressions: the SQL code strings, corresponding to the `EXCEPT`'s operands.
7119            If `Expression` instances are passed, they will be used as-is.
7120        distinct: set the DISTINCT flag if and only if this is true.
7121        dialect: the dialect used to parse the input expression.
7122        copy: whether to copy the expression.
7123        opts: other options to use to parse the input expressions.
7124
7125    Returns:
7126        The new Except instance.
7127    """
7128    assert len(expressions) >= 2, "At least two expressions are required by `except_`."
7129    return _apply_set_operation(
7130        *expressions, set_operation=Except, distinct=distinct, dialect=dialect, copy=copy, **opts
7131    )

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:
7134def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7135    """
7136    Initializes a syntax tree from one or multiple SELECT expressions.
7137
7138    Example:
7139        >>> select("col1", "col2").from_("tbl").sql()
7140        'SELECT col1, col2 FROM tbl'
7141
7142    Args:
7143        *expressions: the SQL code string to parse as the expressions of a
7144            SELECT statement. If an Expression instance is passed, this is used as-is.
7145        dialect: the dialect used to parse the input expressions (in the case that an
7146            input expression is a SQL string).
7147        **opts: other options to use to parse the input expressions (again, in the case
7148            that an input expression is a SQL string).
7149
7150    Returns:
7151        Select: the syntax tree for the SELECT statement.
7152    """
7153    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:
7156def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
7157    """
7158    Initializes a syntax tree from a FROM expression.
7159
7160    Example:
7161        >>> from_("tbl").select("col1", "col2").sql()
7162        'SELECT col1, col2 FROM tbl'
7163
7164    Args:
7165        *expression: the SQL code string to parse as the FROM expressions of a
7166            SELECT statement. If an Expression instance is passed, this is used as-is.
7167        dialect: the dialect used to parse the input expression (in the case that the
7168            input expression is a SQL string).
7169        **opts: other options to use to parse the input expressions (again, in the case
7170            that the input expression is a SQL string).
7171
7172    Returns:
7173        Select: the syntax tree for the SELECT statement.
7174    """
7175    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:
7178def update(
7179    table: str | Table,
7180    properties: t.Optional[dict] = None,
7181    where: t.Optional[ExpOrStr] = None,
7182    from_: t.Optional[ExpOrStr] = None,
7183    with_: t.Optional[t.Dict[str, ExpOrStr]] = None,
7184    dialect: DialectType = None,
7185    **opts,
7186) -> Update:
7187    """
7188    Creates an update statement.
7189
7190    Example:
7191        >>> 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()
7192        "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"
7193
7194    Args:
7195        properties: dictionary of properties to SET which are
7196            auto converted to sql objects eg None -> NULL
7197        where: sql conditional parsed into a WHERE statement
7198        from_: sql statement parsed into a FROM statement
7199        with_: dictionary of CTE aliases / select statements to include in a WITH clause.
7200        dialect: the dialect used to parse the input expressions.
7201        **opts: other options to use to parse the input expressions.
7202
7203    Returns:
7204        Update: the syntax tree for the UPDATE statement.
7205    """
7206    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
7207    if properties:
7208        update_expr.set(
7209            "expressions",
7210            [
7211                EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
7212                for k, v in properties.items()
7213            ],
7214        )
7215    if from_:
7216        update_expr.set(
7217            "from",
7218            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
7219        )
7220    if isinstance(where, Condition):
7221        where = Where(this=where)
7222    if where:
7223        update_expr.set(
7224            "where",
7225            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
7226        )
7227    if with_:
7228        cte_list = [
7229            alias_(CTE(this=maybe_parse(qry, dialect=dialect, **opts)), alias, table=True)
7230            for alias, qry in with_.items()
7231        ]
7232        update_expr.set(
7233            "with",
7234            With(expressions=cte_list),
7235        )
7236    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:
7239def delete(
7240    table: ExpOrStr,
7241    where: t.Optional[ExpOrStr] = None,
7242    returning: t.Optional[ExpOrStr] = None,
7243    dialect: DialectType = None,
7244    **opts,
7245) -> Delete:
7246    """
7247    Builds a delete statement.
7248
7249    Example:
7250        >>> delete("my_table", where="id > 1").sql()
7251        'DELETE FROM my_table WHERE id > 1'
7252
7253    Args:
7254        where: sql conditional parsed into a WHERE statement
7255        returning: sql conditional parsed into a RETURNING statement
7256        dialect: the dialect used to parse the input expressions.
7257        **opts: other options to use to parse the input expressions.
7258
7259    Returns:
7260        Delete: the syntax tree for the DELETE statement.
7261    """
7262    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
7263    if where:
7264        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
7265    if returning:
7266        delete_expr = delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
7267    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:
7270def insert(
7271    expression: ExpOrStr,
7272    into: ExpOrStr,
7273    columns: t.Optional[t.Sequence[str | Identifier]] = None,
7274    overwrite: t.Optional[bool] = None,
7275    returning: t.Optional[ExpOrStr] = None,
7276    dialect: DialectType = None,
7277    copy: bool = True,
7278    **opts,
7279) -> Insert:
7280    """
7281    Builds an INSERT statement.
7282
7283    Example:
7284        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
7285        'INSERT INTO tbl VALUES (1, 2, 3)'
7286
7287    Args:
7288        expression: the sql string or expression of the INSERT statement
7289        into: the tbl to insert data to.
7290        columns: optionally the table's column names.
7291        overwrite: whether to INSERT OVERWRITE or not.
7292        returning: sql conditional parsed into a RETURNING statement
7293        dialect: the dialect used to parse the input expressions.
7294        copy: whether to copy the expression.
7295        **opts: other options to use to parse the input expressions.
7296
7297    Returns:
7298        Insert: the syntax tree for the INSERT statement.
7299    """
7300    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
7301    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
7302
7303    if columns:
7304        this = Schema(this=this, expressions=[to_identifier(c, copy=copy) for c in columns])
7305
7306    insert = Insert(this=this, expression=expr, overwrite=overwrite)
7307
7308    if returning:
7309        insert = insert.returning(returning, dialect=dialect, copy=False, **opts)
7310
7311    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:
7314def merge(
7315    *when_exprs: ExpOrStr,
7316    into: ExpOrStr,
7317    using: ExpOrStr,
7318    on: ExpOrStr,
7319    returning: t.Optional[ExpOrStr] = None,
7320    dialect: DialectType = None,
7321    copy: bool = True,
7322    **opts,
7323) -> Merge:
7324    """
7325    Builds a MERGE statement.
7326
7327    Example:
7328        >>> merge("WHEN MATCHED THEN UPDATE SET col1 = source_table.col1",
7329        ...       "WHEN NOT MATCHED THEN INSERT (col1) VALUES (source_table.col1)",
7330        ...       into="my_table",
7331        ...       using="source_table",
7332        ...       on="my_table.id = source_table.id").sql()
7333        '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)'
7334
7335    Args:
7336        *when_exprs: The WHEN clauses specifying actions for matched and unmatched rows.
7337        into: The target table to merge data into.
7338        using: The source table to merge data from.
7339        on: The join condition for the merge.
7340        returning: The columns to return from the merge.
7341        dialect: The dialect used to parse the input expressions.
7342        copy: Whether to copy the expression.
7343        **opts: Other options to use to parse the input expressions.
7344
7345    Returns:
7346        Merge: The syntax tree for the MERGE statement.
7347    """
7348    merge = Merge(
7349        this=maybe_parse(into, dialect=dialect, copy=copy, **opts),
7350        using=maybe_parse(using, dialect=dialect, copy=copy, **opts),
7351        on=maybe_parse(on, dialect=dialect, copy=copy, **opts),
7352        expressions=[
7353            maybe_parse(when_expr, dialect=dialect, copy=copy, into=When, **opts)
7354            for when_expr in when_exprs
7355        ],
7356    )
7357    if returning:
7358        merge = merge.returning(returning, dialect=dialect, copy=False, **opts)
7359
7360    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:
7363def condition(
7364    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
7365) -> Condition:
7366    """
7367    Initialize a logical condition expression.
7368
7369    Example:
7370        >>> condition("x=1").sql()
7371        'x = 1'
7372
7373        This is helpful for composing larger logical syntax trees:
7374        >>> where = condition("x=1")
7375        >>> where = where.and_("y=1")
7376        >>> Select().from_("tbl").select("*").where(where).sql()
7377        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
7378
7379    Args:
7380        *expression: the SQL code string to parse.
7381            If an Expression instance is passed, this is used as-is.
7382        dialect: the dialect used to parse the input expression (in the case that the
7383            input expression is a SQL string).
7384        copy: Whether to copy `expression` (only applies to expressions).
7385        **opts: other options to use to parse the input expressions (again, in the case
7386            that the input expression is a SQL string).
7387
7388    Returns:
7389        The new Condition instance
7390    """
7391    return maybe_parse(
7392        expression,
7393        into=Condition,
7394        dialect=dialect,
7395        copy=copy,
7396        **opts,
7397    )

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, wrap: bool = True, **opts) -> Condition:
7400def and_(
7401    *expressions: t.Optional[ExpOrStr],
7402    dialect: DialectType = None,
7403    copy: bool = True,
7404    wrap: bool = True,
7405    **opts,
7406) -> Condition:
7407    """
7408    Combine multiple conditions with an AND logical operator.
7409
7410    Example:
7411        >>> and_("x=1", and_("y=1", "z=1")).sql()
7412        'x = 1 AND (y = 1 AND z = 1)'
7413
7414    Args:
7415        *expressions: the SQL code strings to parse.
7416            If an Expression instance is passed, this is used as-is.
7417        dialect: the dialect used to parse the input expression.
7418        copy: whether to copy `expressions` (only applies to Expressions).
7419        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7420            precedence issues, but can be turned off when the produced AST is too deep and
7421            causes recursion-related issues.
7422        **opts: other options to use to parse the input expressions.
7423
7424    Returns:
7425        The new condition
7426    """
7427    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, wrap=wrap, **opts))

Combine multiple conditions with an AND logical operator.

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

The new condition

def or_( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
7430def or_(
7431    *expressions: t.Optional[ExpOrStr],
7432    dialect: DialectType = None,
7433    copy: bool = True,
7434    wrap: bool = True,
7435    **opts,
7436) -> Condition:
7437    """
7438    Combine multiple conditions with an OR logical operator.
7439
7440    Example:
7441        >>> or_("x=1", or_("y=1", "z=1")).sql()
7442        'x = 1 OR (y = 1 OR z = 1)'
7443
7444    Args:
7445        *expressions: the SQL code strings to parse.
7446            If an Expression instance is passed, this is used as-is.
7447        dialect: the dialect used to parse the input expression.
7448        copy: whether to copy `expressions` (only applies to Expressions).
7449        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7450            precedence issues, but can be turned off when the produced AST is too deep and
7451            causes recursion-related issues.
7452        **opts: other options to use to parse the input expressions.
7453
7454    Returns:
7455        The new condition
7456    """
7457    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, wrap=wrap, **opts))

Combine multiple conditions with an OR logical operator.

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

The new condition

def xor( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, wrap: bool = True, **opts) -> Condition:
7460def xor(
7461    *expressions: t.Optional[ExpOrStr],
7462    dialect: DialectType = None,
7463    copy: bool = True,
7464    wrap: bool = True,
7465    **opts,
7466) -> Condition:
7467    """
7468    Combine multiple conditions with an XOR logical operator.
7469
7470    Example:
7471        >>> xor("x=1", xor("y=1", "z=1")).sql()
7472        'x = 1 XOR (y = 1 XOR z = 1)'
7473
7474    Args:
7475        *expressions: the SQL code strings to parse.
7476            If an Expression instance is passed, this is used as-is.
7477        dialect: the dialect used to parse the input expression.
7478        copy: whether to copy `expressions` (only applies to Expressions).
7479        wrap: whether to wrap the operands in `Paren`s. This is true by default to avoid
7480            precedence issues, but can be turned off when the produced AST is too deep and
7481            causes recursion-related issues.
7482        **opts: other options to use to parse the input expressions.
7483
7484    Returns:
7485        The new condition
7486    """
7487    return t.cast(Condition, _combine(expressions, Xor, dialect, copy=copy, wrap=wrap, **opts))

Combine multiple conditions with an XOR logical operator.

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

The new condition

def not_( expression: Union[str, Expression], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Not:
7490def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
7491    """
7492    Wrap a condition with a NOT operator.
7493
7494    Example:
7495        >>> not_("this_suit='black'").sql()
7496        "NOT this_suit = 'black'"
7497
7498    Args:
7499        expression: the SQL code string to parse.
7500            If an Expression instance is passed, this is used as-is.
7501        dialect: the dialect used to parse the input expression.
7502        copy: whether to copy the expression or not.
7503        **opts: other options to use to parse the input expressions.
7504
7505    Returns:
7506        The new condition.
7507    """
7508    this = condition(
7509        expression,
7510        dialect=dialect,
7511        copy=copy,
7512        **opts,
7513    )
7514    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:
7517def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
7518    """
7519    Wrap an expression in parentheses.
7520
7521    Example:
7522        >>> paren("5 + 3").sql()
7523        '(5 + 3)'
7524
7525    Args:
7526        expression: the SQL code string to parse.
7527            If an Expression instance is passed, this is used as-is.
7528        copy: whether to copy the expression or not.
7529
7530    Returns:
7531        The wrapped expression.
7532    """
7533    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):
7549def to_identifier(name, quoted=None, copy=True):
7550    """Builds an identifier.
7551
7552    Args:
7553        name: The name to turn into an identifier.
7554        quoted: Whether to force quote the identifier.
7555        copy: Whether to copy name if it's an Identifier.
7556
7557    Returns:
7558        The identifier ast node.
7559    """
7560
7561    if name is None:
7562        return None
7563
7564    if isinstance(name, Identifier):
7565        identifier = maybe_copy(name, copy)
7566    elif isinstance(name, str):
7567        identifier = Identifier(
7568            this=name,
7569            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
7570        )
7571    else:
7572        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
7573    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:
7576def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
7577    """
7578    Parses a given string into an identifier.
7579
7580    Args:
7581        name: The name to parse into an identifier.
7582        dialect: The dialect to parse against.
7583
7584    Returns:
7585        The identifier ast node.
7586    """
7587    try:
7588        expression = maybe_parse(name, dialect=dialect, into=Identifier)
7589    except (ParseError, TokenError):
7590        expression = to_identifier(name)
7591
7592    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:
7598def to_interval(interval: str | Literal) -> Interval:
7599    """Builds an interval expression from a string like '1 day' or '5 months'."""
7600    if isinstance(interval, Literal):
7601        if not interval.is_string:
7602            raise ValueError("Invalid interval string.")
7603
7604        interval = interval.this
7605
7606    interval = maybe_parse(f"INTERVAL {interval}")
7607    assert isinstance(interval, Interval)
7608    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:
7611def to_table(
7612    sql_path: str | Table, dialect: DialectType = None, copy: bool = True, **kwargs
7613) -> Table:
7614    """
7615    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
7616    If a table is passed in then that table is returned.
7617
7618    Args:
7619        sql_path: a `[catalog].[schema].[table]` string.
7620        dialect: the source dialect according to which the table name will be parsed.
7621        copy: Whether to copy a table if it is passed in.
7622        kwargs: the kwargs to instantiate the resulting `Table` expression with.
7623
7624    Returns:
7625        A table expression.
7626    """
7627    if isinstance(sql_path, Table):
7628        return maybe_copy(sql_path, copy=copy)
7629
7630    table = maybe_parse(sql_path, into=Table, dialect=dialect)
7631
7632    for k, v in kwargs.items():
7633        table.set(k, v)
7634
7635    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:
7638def to_column(
7639    sql_path: str | Column,
7640    quoted: t.Optional[bool] = None,
7641    dialect: DialectType = None,
7642    copy: bool = True,
7643    **kwargs,
7644) -> Column:
7645    """
7646    Create a column from a `[table].[column]` sql path. Table is optional.
7647    If a column is passed in then that column is returned.
7648
7649    Args:
7650        sql_path: a `[table].[column]` string.
7651        quoted: Whether or not to force quote identifiers.
7652        dialect: the source dialect according to which the column name will be parsed.
7653        copy: Whether to copy a column if it is passed in.
7654        kwargs: the kwargs to instantiate the resulting `Column` expression with.
7655
7656    Returns:
7657        A column expression.
7658    """
7659    if isinstance(sql_path, Column):
7660        return maybe_copy(sql_path, copy=copy)
7661
7662    try:
7663        col = maybe_parse(sql_path, into=Column, dialect=dialect)
7664    except ParseError:
7665        return column(*reversed(sql_path.split(".")), quoted=quoted, **kwargs)
7666
7667    for k, v in kwargs.items():
7668        col.set(k, v)
7669
7670    if quoted:
7671        for i in col.find_all(Identifier):
7672            i.set("quoted", True)
7673
7674    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):
7677def alias_(
7678    expression: ExpOrStr,
7679    alias: t.Optional[str | Identifier],
7680    table: bool | t.Sequence[str | Identifier] = False,
7681    quoted: t.Optional[bool] = None,
7682    dialect: DialectType = None,
7683    copy: bool = True,
7684    **opts,
7685):
7686    """Create an Alias expression.
7687
7688    Example:
7689        >>> alias_('foo', 'bar').sql()
7690        'foo AS bar'
7691
7692        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
7693        '(SELECT 1, 2) AS bar(a, b)'
7694
7695    Args:
7696        expression: the SQL code strings to parse.
7697            If an Expression instance is passed, this is used as-is.
7698        alias: the alias name to use. If the name has
7699            special characters it is quoted.
7700        table: Whether to create a table alias, can also be a list of columns.
7701        quoted: whether to quote the alias
7702        dialect: the dialect used to parse the input expression.
7703        copy: Whether to copy the expression.
7704        **opts: other options to use to parse the input expressions.
7705
7706    Returns:
7707        Alias: the aliased expression
7708    """
7709    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
7710    alias = to_identifier(alias, quoted=quoted)
7711
7712    if table:
7713        table_alias = TableAlias(this=alias)
7714        exp.set("alias", table_alias)
7715
7716        if not isinstance(table, bool):
7717            for column in table:
7718                table_alias.append("columns", to_identifier(column, quoted=quoted))
7719
7720        return exp
7721
7722    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
7723    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
7724    # for the complete Window expression.
7725    #
7726    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
7727
7728    if "alias" in exp.arg_types and not isinstance(exp, Window):
7729        exp.set("alias", alias)
7730        return exp
7731    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:
7734def subquery(
7735    expression: ExpOrStr,
7736    alias: t.Optional[Identifier | str] = None,
7737    dialect: DialectType = None,
7738    **opts,
7739) -> Select:
7740    """
7741    Build a subquery expression that's selected from.
7742
7743    Example:
7744        >>> subquery('select x from tbl', 'bar').select('x').sql()
7745        'SELECT x FROM (SELECT x FROM tbl) AS bar'
7746
7747    Args:
7748        expression: the SQL code strings to parse.
7749            If an Expression instance is passed, this is used as-is.
7750        alias: the alias name to use.
7751        dialect: the dialect used to parse the input expression.
7752        **opts: other options to use to parse the input expressions.
7753
7754    Returns:
7755        A new Select instance with the subquery expression included.
7756    """
7757
7758    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias, **opts)
7759    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):
7790def column(
7791    col,
7792    table=None,
7793    db=None,
7794    catalog=None,
7795    *,
7796    fields=None,
7797    quoted=None,
7798    copy=True,
7799):
7800    """
7801    Build a Column.
7802
7803    Args:
7804        col: Column name.
7805        table: Table name.
7806        db: Database name.
7807        catalog: Catalog name.
7808        fields: Additional fields using dots.
7809        quoted: Whether to force quotes on the column's identifiers.
7810        copy: Whether to copy identifiers if passed in.
7811
7812    Returns:
7813        The new Column instance.
7814    """
7815    this = Column(
7816        this=to_identifier(col, quoted=quoted, copy=copy),
7817        table=to_identifier(table, quoted=quoted, copy=copy),
7818        db=to_identifier(db, quoted=quoted, copy=copy),
7819        catalog=to_identifier(catalog, quoted=quoted, copy=copy),
7820    )
7821
7822    if fields:
7823        this = Dot.build(
7824            (this, *(to_identifier(field, quoted=quoted, copy=copy) for field in fields))
7825        )
7826    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:
7829def cast(
7830    expression: ExpOrStr, to: DATA_TYPE, copy: bool = True, dialect: DialectType = None, **opts
7831) -> Cast:
7832    """Cast an expression to a data type.
7833
7834    Example:
7835        >>> cast('x + 1', 'int').sql()
7836        'CAST(x + 1 AS INT)'
7837
7838    Args:
7839        expression: The expression to cast.
7840        to: The datatype to cast to.
7841        copy: Whether to copy the supplied expressions.
7842        dialect: The target dialect. This is used to prevent a re-cast in the following scenario:
7843            - The expression to be cast is already a exp.Cast expression
7844            - The existing cast is to a type that is logically equivalent to new type
7845
7846            For example, if :expression='CAST(x as DATETIME)' and :to=Type.TIMESTAMP,
7847            but in the target dialect DATETIME is mapped to TIMESTAMP, then we will NOT return `CAST(x (as DATETIME) as TIMESTAMP)`
7848            and instead just return the original expression `CAST(x as DATETIME)`.
7849
7850            This is to prevent it being output as a double cast `CAST(x (as TIMESTAMP) as TIMESTAMP)` once the DATETIME -> TIMESTAMP
7851            mapping is applied in the target dialect generator.
7852
7853    Returns:
7854        The new Cast instance.
7855    """
7856    expr = maybe_parse(expression, copy=copy, dialect=dialect, **opts)
7857    data_type = DataType.build(to, copy=copy, dialect=dialect, **opts)
7858
7859    # dont re-cast if the expression is already a cast to the correct type
7860    if isinstance(expr, Cast):
7861        from sqlglot.dialects.dialect import Dialect
7862
7863        target_dialect = Dialect.get_or_raise(dialect)
7864        type_mapping = target_dialect.generator_class.TYPE_MAPPING
7865
7866        existing_cast_type: DataType.Type = expr.to.this
7867        new_cast_type: DataType.Type = data_type.this
7868        types_are_equivalent = type_mapping.get(
7869            existing_cast_type, existing_cast_type.value
7870        ) == type_mapping.get(new_cast_type, new_cast_type.value)
7871
7872        if expr.is_type(data_type) or types_are_equivalent:
7873            return expr
7874
7875    expr = Cast(this=expr, to=data_type)
7876    expr.type = data_type
7877
7878    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:
7881def table_(
7882    table: Identifier | str,
7883    db: t.Optional[Identifier | str] = None,
7884    catalog: t.Optional[Identifier | str] = None,
7885    quoted: t.Optional[bool] = None,
7886    alias: t.Optional[Identifier | str] = None,
7887) -> Table:
7888    """Build a Table.
7889
7890    Args:
7891        table: Table name.
7892        db: Database name.
7893        catalog: Catalog name.
7894        quote: Whether to force quotes on the table's identifiers.
7895        alias: Table's alias.
7896
7897    Returns:
7898        The new Table instance.
7899    """
7900    return Table(
7901        this=to_identifier(table, quoted=quoted) if table else None,
7902        db=to_identifier(db, quoted=quoted) if db else None,
7903        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
7904        alias=TableAlias(this=to_identifier(alias)) if alias else None,
7905    )

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:
7908def values(
7909    values: t.Iterable[t.Tuple[t.Any, ...]],
7910    alias: t.Optional[str] = None,
7911    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
7912) -> Values:
7913    """Build VALUES statement.
7914
7915    Example:
7916        >>> values([(1, '2')]).sql()
7917        "VALUES (1, '2')"
7918
7919    Args:
7920        values: values statements that will be converted to SQL
7921        alias: optional alias
7922        columns: Optional list of ordered column names or ordered dictionary of column names to types.
7923         If either are provided then an alias is also required.
7924
7925    Returns:
7926        Values: the Values expression object
7927    """
7928    if columns and not alias:
7929        raise ValueError("Alias is required when providing columns")
7930
7931    return Values(
7932        expressions=[convert(tup) for tup in values],
7933        alias=(
7934            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
7935            if columns
7936            else (TableAlias(this=to_identifier(alias)) if alias else None)
7937        ),
7938    )

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:
7941def var(name: t.Optional[ExpOrStr]) -> Var:
7942    """Build a SQL variable.
7943
7944    Example:
7945        >>> repr(var('x'))
7946        'Var(this=x)'
7947
7948        >>> repr(var(column('x', table='y')))
7949        'Var(this=x)'
7950
7951    Args:
7952        name: The name of the var or an expression who's name will become the var.
7953
7954    Returns:
7955        The new variable node.
7956    """
7957    if not name:
7958        raise ValueError("Cannot convert empty name into var.")
7959
7960    if isinstance(name, Expression):
7961        name = name.name
7962    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:
7965def rename_table(
7966    old_name: str | Table,
7967    new_name: str | Table,
7968    dialect: DialectType = None,
7969) -> Alter:
7970    """Build ALTER TABLE... RENAME... expression
7971
7972    Args:
7973        old_name: The old name of the table
7974        new_name: The new name of the table
7975        dialect: The dialect to parse the table.
7976
7977    Returns:
7978        Alter table expression
7979    """
7980    old_table = to_table(old_name, dialect=dialect)
7981    new_table = to_table(new_name, dialect=dialect)
7982    return Alter(
7983        this=old_table,
7984        kind="TABLE",
7985        actions=[
7986            AlterRename(this=new_table),
7987        ],
7988    )

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:
7991def rename_column(
7992    table_name: str | Table,
7993    old_column_name: str | Column,
7994    new_column_name: str | Column,
7995    exists: t.Optional[bool] = None,
7996    dialect: DialectType = None,
7997) -> Alter:
7998    """Build ALTER TABLE... RENAME COLUMN... expression
7999
8000    Args:
8001        table_name: Name of the table
8002        old_column: The old name of the column
8003        new_column: The new name of the column
8004        exists: Whether to add the `IF EXISTS` clause
8005        dialect: The dialect to parse the table/column.
8006
8007    Returns:
8008        Alter table expression
8009    """
8010    table = to_table(table_name, dialect=dialect)
8011    old_column = to_column(old_column_name, dialect=dialect)
8012    new_column = to_column(new_column_name, dialect=dialect)
8013    return Alter(
8014        this=table,
8015        kind="TABLE",
8016        actions=[
8017            RenameColumn(this=old_column, to=new_column, exists=exists),
8018        ],
8019    )

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:
8022def convert(value: t.Any, copy: bool = False) -> Expression:
8023    """Convert a python value into an expression object.
8024
8025    Raises an error if a conversion is not possible.
8026
8027    Args:
8028        value: A python object.
8029        copy: Whether to copy `value` (only applies to Expressions and collections).
8030
8031    Returns:
8032        The equivalent expression object.
8033    """
8034    if isinstance(value, Expression):
8035        return maybe_copy(value, copy)
8036    if isinstance(value, str):
8037        return Literal.string(value)
8038    if isinstance(value, bool):
8039        return Boolean(this=value)
8040    if value is None or (isinstance(value, float) and math.isnan(value)):
8041        return null()
8042    if isinstance(value, numbers.Number):
8043        return Literal.number(value)
8044    if isinstance(value, bytes):
8045        return HexString(this=value.hex())
8046    if isinstance(value, datetime.datetime):
8047        datetime_literal = Literal.string(value.isoformat(sep=" "))
8048
8049        tz = None
8050        if value.tzinfo:
8051            # this works for zoneinfo.ZoneInfo, pytz.timezone and datetime.datetime.utc to return IANA timezone names like "America/Los_Angeles"
8052            # instead of abbreviations like "PDT". This is for consistency with other timezone handling functions in SQLGlot
8053            tz = Literal.string(str(value.tzinfo))
8054
8055        return TimeStrToTime(this=datetime_literal, zone=tz)
8056    if isinstance(value, datetime.date):
8057        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
8058        return DateStrToDate(this=date_literal)
8059    if isinstance(value, tuple):
8060        if hasattr(value, "_fields"):
8061            return Struct(
8062                expressions=[
8063                    PropertyEQ(
8064                        this=to_identifier(k), expression=convert(getattr(value, k), copy=copy)
8065                    )
8066                    for k in value._fields
8067                ]
8068            )
8069        return Tuple(expressions=[convert(v, copy=copy) for v in value])
8070    if isinstance(value, list):
8071        return Array(expressions=[convert(v, copy=copy) for v in value])
8072    if isinstance(value, dict):
8073        return Map(
8074            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
8075            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
8076        )
8077    if hasattr(value, "__dict__"):
8078        return Struct(
8079            expressions=[
8080                PropertyEQ(this=to_identifier(k), expression=convert(v, copy=copy))
8081                for k, v in value.__dict__.items()
8082            ]
8083        )
8084    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:
8087def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
8088    """
8089    Replace children of an expression with the result of a lambda fun(child) -> exp.
8090    """
8091    for k, v in tuple(expression.args.items()):
8092        is_list_arg = type(v) is list
8093
8094        child_nodes = v if is_list_arg else [v]
8095        new_child_nodes = []
8096
8097        for cn in child_nodes:
8098            if isinstance(cn, Expression):
8099                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
8100                    new_child_nodes.append(child_node)
8101            else:
8102                new_child_nodes.append(cn)
8103
8104        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:
8107def replace_tree(
8108    expression: Expression,
8109    fun: t.Callable,
8110    prune: t.Optional[t.Callable[[Expression], bool]] = None,
8111) -> Expression:
8112    """
8113    Replace an entire tree with the result of function calls on each node.
8114
8115    This will be traversed in reverse dfs, so leaves first.
8116    If new nodes are created as a result of function calls, they will also be traversed.
8117    """
8118    stack = list(expression.dfs(prune=prune))
8119
8120    while stack:
8121        node = stack.pop()
8122        new_node = fun(node)
8123
8124        if new_node is not node:
8125            node.replace(new_node)
8126
8127            if isinstance(new_node, Expression):
8128                stack.append(new_node)
8129
8130    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]:
8133def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
8134    """
8135    Return all table names referenced through columns in an expression.
8136
8137    Example:
8138        >>> import sqlglot
8139        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
8140        ['a', 'c']
8141
8142    Args:
8143        expression: expression to find table names.
8144        exclude: a table name to exclude
8145
8146    Returns:
8147        A list of unique names.
8148    """
8149    return {
8150        table
8151        for table in (column.table for column in expression.find_all(Column))
8152        if table and table != exclude
8153    }

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:
8156def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
8157    """Get the full name of a table as a string.
8158
8159    Args:
8160        table: Table expression node or string.
8161        dialect: The dialect to generate the table name for.
8162        identify: Determines when an identifier should be quoted. Possible values are:
8163            False (default): Never quote, except in cases where it's mandatory by the dialect.
8164            True: Always quote.
8165
8166    Examples:
8167        >>> from sqlglot import exp, parse_one
8168        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
8169        'a.b.c'
8170
8171    Returns:
8172        The table name.
8173    """
8174
8175    table = maybe_parse(table, into=Table, dialect=dialect)
8176
8177    if not table:
8178        raise ValueError(f"Cannot parse {table}")
8179
8180    return ".".join(
8181        (
8182            part.sql(dialect=dialect, identify=True, copy=False, comments=False)
8183            if identify or not SAFE_IDENTIFIER_RE.match(part.name)
8184            else part.name
8185        )
8186        for part in table.parts
8187    )

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:
8190def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
8191    """Returns a case normalized table name without quotes.
8192
8193    Args:
8194        table: the table to normalize
8195        dialect: the dialect to use for normalization rules
8196        copy: whether to copy the expression.
8197
8198    Examples:
8199        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
8200        'A-B.c'
8201    """
8202    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
8203
8204    return ".".join(
8205        p.name
8206        for p in normalize_identifiers(
8207            to_table(table, dialect=dialect, copy=copy), dialect=dialect
8208        ).parts
8209    )

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:
8212def replace_tables(
8213    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
8214) -> E:
8215    """Replace all tables in expression according to the mapping.
8216
8217    Args:
8218        expression: expression node to be transformed and replaced.
8219        mapping: mapping of table names.
8220        dialect: the dialect of the mapping table
8221        copy: whether to copy the expression.
8222
8223    Examples:
8224        >>> from sqlglot import exp, parse_one
8225        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
8226        'SELECT * FROM c /* a.b */'
8227
8228    Returns:
8229        The mapped expression.
8230    """
8231
8232    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
8233
8234    def _replace_tables(node: Expression) -> Expression:
8235        if isinstance(node, Table):
8236            original = normalize_table_name(node, dialect=dialect)
8237            new_name = mapping.get(original)
8238
8239            if new_name:
8240                table = to_table(
8241                    new_name,
8242                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
8243                    dialect=dialect,
8244                )
8245                table.add_comments([original])
8246                return table
8247        return node
8248
8249    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:
8252def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
8253    """Replace placeholders in an expression.
8254
8255    Args:
8256        expression: expression node to be transformed and replaced.
8257        args: positional names that will substitute unnamed placeholders in the given order.
8258        kwargs: keyword arguments that will substitute named placeholders.
8259
8260    Examples:
8261        >>> from sqlglot import exp, parse_one
8262        >>> replace_placeholders(
8263        ...     parse_one("select * from :tbl where ? = ?"),
8264        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
8265        ... ).sql()
8266        "SELECT * FROM foo WHERE str_col = 'b'"
8267
8268    Returns:
8269        The mapped expression.
8270    """
8271
8272    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
8273        if isinstance(node, Placeholder):
8274            if node.this:
8275                new_name = kwargs.get(node.this)
8276                if new_name is not None:
8277                    return convert(new_name)
8278            else:
8279                try:
8280                    return convert(next(args))
8281                except StopIteration:
8282                    pass
8283        return node
8284
8285    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:
8288def expand(
8289    expression: Expression,
8290    sources: t.Dict[str, Query],
8291    dialect: DialectType = None,
8292    copy: bool = True,
8293) -> Expression:
8294    """Transforms an expression by expanding all referenced sources into subqueries.
8295
8296    Examples:
8297        >>> from sqlglot import parse_one
8298        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
8299        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
8300
8301        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
8302        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
8303
8304    Args:
8305        expression: The expression to expand.
8306        sources: A dictionary of name to Queries.
8307        dialect: The dialect of the sources dict.
8308        copy: Whether to copy the expression during transformation. Defaults to True.
8309
8310    Returns:
8311        The transformed expression.
8312    """
8313    sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
8314
8315    def _expand(node: Expression):
8316        if isinstance(node, Table):
8317            name = normalize_table_name(node, dialect=dialect)
8318            source = sources.get(name)
8319            if source:
8320                subquery = source.subquery(node.alias or name)
8321                subquery.comments = [f"source: {name}"]
8322                return subquery.transform(_expand, copy=False)
8323        return node
8324
8325    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:
8328def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
8329    """
8330    Returns a Func expression.
8331
8332    Examples:
8333        >>> func("abs", 5).sql()
8334        'ABS(5)'
8335
8336        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
8337        'CAST(5 AS DOUBLE)'
8338
8339    Args:
8340        name: the name of the function to build.
8341        args: the args used to instantiate the function of interest.
8342        copy: whether to copy the argument expressions.
8343        dialect: the source dialect.
8344        kwargs: the kwargs used to instantiate the function of interest.
8345
8346    Note:
8347        The arguments `args` and `kwargs` are mutually exclusive.
8348
8349    Returns:
8350        An instance of the function of interest, or an anonymous function, if `name` doesn't
8351        correspond to an existing `sqlglot.expressions.Func` class.
8352    """
8353    if args and kwargs:
8354        raise ValueError("Can't use both args and kwargs to instantiate a function.")
8355
8356    from sqlglot.dialects.dialect import Dialect
8357
8358    dialect = Dialect.get_or_raise(dialect)
8359
8360    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
8361    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
8362
8363    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
8364    if constructor:
8365        if converted:
8366            if "dialect" in constructor.__code__.co_varnames:
8367                function = constructor(converted, dialect=dialect)
8368            else:
8369                function = constructor(converted)
8370        elif constructor.__name__ == "from_arg_list":
8371            function = constructor.__self__(**kwargs)  # type: ignore
8372        else:
8373            constructor = FUNCTION_BY_NAME.get(name.upper())
8374            if constructor:
8375                function = constructor(**kwargs)
8376            else:
8377                raise ValueError(
8378                    f"Unable to convert '{name}' into a Func. Either manually construct "
8379                    "the Func expression of interest or parse the function call."
8380                )
8381    else:
8382        kwargs = kwargs or {"expressions": converted}
8383        function = Anonymous(this=name, **kwargs)
8384
8385    for error_message in function.error_messages(converted):
8386        raise ValueError(error_message)
8387
8388    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:
8391def case(
8392    expression: t.Optional[ExpOrStr] = None,
8393    **opts,
8394) -> Case:
8395    """
8396    Initialize a CASE statement.
8397
8398    Example:
8399        case().when("a = 1", "foo").else_("bar")
8400
8401    Args:
8402        expression: Optionally, the input expression (not all dialects support this)
8403        **opts: Extra keyword arguments for parsing `expression`
8404    """
8405    if expression is not None:
8406        this = maybe_parse(expression, **opts)
8407    else:
8408        this = None
8409    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:
8412def array(
8413    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8414) -> Array:
8415    """
8416    Returns an array.
8417
8418    Examples:
8419        >>> array(1, 'x').sql()
8420        'ARRAY(1, x)'
8421
8422    Args:
8423        expressions: the expressions to add to the array.
8424        copy: whether to copy the argument expressions.
8425        dialect: the source dialect.
8426        kwargs: the kwargs used to instantiate the function of interest.
8427
8428    Returns:
8429        An array expression.
8430    """
8431    return Array(
8432        expressions=[
8433            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8434            for expression in expressions
8435        ]
8436    )

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:
8439def tuple_(
8440    *expressions: ExpOrStr, copy: bool = True, dialect: DialectType = None, **kwargs
8441) -> Tuple:
8442    """
8443    Returns an tuple.
8444
8445    Examples:
8446        >>> tuple_(1, 'x').sql()
8447        '(1, x)'
8448
8449    Args:
8450        expressions: the expressions to add to the tuple.
8451        copy: whether to copy the argument expressions.
8452        dialect: the source dialect.
8453        kwargs: the kwargs used to instantiate the function of interest.
8454
8455    Returns:
8456        A tuple expression.
8457    """
8458    return Tuple(
8459        expressions=[
8460            maybe_parse(expression, copy=copy, dialect=dialect, **kwargs)
8461            for expression in expressions
8462        ]
8463    )

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:
8466def true() -> Boolean:
8467    """
8468    Returns a true Boolean expression.
8469    """
8470    return Boolean(this=True)

Returns a true Boolean expression.

def false() -> Boolean:
8473def false() -> Boolean:
8474    """
8475    Returns a false Boolean expression.
8476    """
8477    return Boolean(this=False)

Returns a false Boolean expression.

def null() -> Null:
8480def null() -> Null:
8481    """
8482    Returns a Null expression.
8483    """
8484    return Null()

Returns a Null expression.

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