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 select.


   1"""
   2## Expressions
   3
   4Every AST node in SQLGlot is represented by a subclass of `Expression`.
   5
   6This module contains the implementation of all supported `Expression` types. Additionally,
   7it exposes a number of helper functions, which are mainly used to programmatically build
   8SQL expressions, such as `sqlglot.expressions.select`.
   9
  10----
  11"""
  12
  13from __future__ import annotations
  14
  15import datetime
  16import math
  17import numbers
  18import re
  19import typing as t
  20from collections import deque
  21from copy import deepcopy
  22from enum import auto
  23from functools import reduce
  24
  25from sqlglot._typing import E
  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
  36
  37if t.TYPE_CHECKING:
  38    from sqlglot.dialects.dialect import DialectType
  39
  40
  41class _Expression(type):
  42    def __new__(cls, clsname, bases, attrs):
  43        klass = super().__new__(cls, clsname, bases, attrs)
  44
  45        # When an Expression class is created, its key is automatically set to be
  46        # the lowercase version of the class' name.
  47        klass.key = clsname.lower()
  48
  49        # This is so that docstrings are not inherited in pdoc
  50        klass.__doc__ = klass.__doc__ or ""
  51
  52        return klass
  53
  54
  55SQLGLOT_META = "sqlglot.meta"
  56TABLE_PARTS = ("this", "db", "catalog")
  57
  58
  59class Expression(metaclass=_Expression):
  60    """
  61    The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary
  62    context, such as its child expressions, their names (arg keys), and whether a given child expression
  63    is optional or not.
  64
  65    Attributes:
  66        key: a unique key for each class in the Expression hierarchy. This is useful for hashing
  67            and representing expressions as strings.
  68        arg_types: determines what arguments (child nodes) are supported by an expression. It
  69            maps arg keys to booleans that indicate whether the corresponding args are optional.
  70        parent: a reference to the parent expression (or None, in case of root expressions).
  71        arg_key: the arg key an expression is associated with, i.e. the name its parent expression
  72            uses to refer to it.
  73        comments: a list of comments that are associated with a given expression. This is used in
  74            order to preserve comments when transpiling SQL code.
  75        type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the
  76            optimizer, in order to enable some transformations that require type information.
  77        meta: a dictionary that can be used to store useful metadata for a given expression.
  78
  79    Example:
  80        >>> class Foo(Expression):
  81        ...     arg_types = {"this": True, "expression": False}
  82
  83        The above definition informs us that Foo is an Expression that requires an argument called
  84        "this" and may also optionally receive an argument called "expression".
  85
  86    Args:
  87        args: a mapping used for retrieving the arguments of an expression, given their arg keys.
  88    """
  89
  90    key = "expression"
  91    arg_types = {"this": True}
  92    __slots__ = ("args", "parent", "arg_key", "comments", "_type", "_meta", "_hash")
  93
  94    def __init__(self, **args: t.Any):
  95        self.args: t.Dict[str, t.Any] = args
  96        self.parent: t.Optional[Expression] = None
  97        self.arg_key: t.Optional[str] = None
  98        self.comments: t.Optional[t.List[str]] = None
  99        self._type: t.Optional[DataType] = None
 100        self._meta: t.Optional[t.Dict[str, t.Any]] = None
 101        self._hash: t.Optional[int] = None
 102
 103        for arg_key, value in self.args.items():
 104            self._set_parent(arg_key, value)
 105
 106    def __eq__(self, other) -> bool:
 107        return type(self) is type(other) and hash(self) == hash(other)
 108
 109    @property
 110    def hashable_args(self) -> t.Any:
 111        return frozenset(
 112            (k, tuple(_norm_arg(a) for a in v) if type(v) is list else _norm_arg(v))
 113            for k, v in self.args.items()
 114            if not (v is None or v is False or (type(v) is list and not v))
 115        )
 116
 117    def __hash__(self) -> int:
 118        if self._hash is not None:
 119            return self._hash
 120
 121        return hash((self.__class__, self.hashable_args))
 122
 123    @property
 124    def this(self) -> t.Any:
 125        """
 126        Retrieves the argument with key "this".
 127        """
 128        return self.args.get("this")
 129
 130    @property
 131    def expression(self) -> t.Any:
 132        """
 133        Retrieves the argument with key "expression".
 134        """
 135        return self.args.get("expression")
 136
 137    @property
 138    def expressions(self) -> t.List[t.Any]:
 139        """
 140        Retrieves the argument with key "expressions".
 141        """
 142        return self.args.get("expressions") or []
 143
 144    def text(self, key) -> str:
 145        """
 146        Returns a textual representation of the argument corresponding to "key". This can only be used
 147        for args that are strings or leaf Expression instances, such as identifiers and literals.
 148        """
 149        field = self.args.get(key)
 150        if isinstance(field, str):
 151            return field
 152        if isinstance(field, (Identifier, Literal, Var)):
 153            return field.this
 154        if isinstance(field, (Star, Null)):
 155            return field.name
 156        return ""
 157
 158    @property
 159    def is_string(self) -> bool:
 160        """
 161        Checks whether a Literal expression is a string.
 162        """
 163        return isinstance(self, Literal) and self.args["is_string"]
 164
 165    @property
 166    def is_number(self) -> bool:
 167        """
 168        Checks whether a Literal expression is a number.
 169        """
 170        return isinstance(self, Literal) and not self.args["is_string"]
 171
 172    @property
 173    def is_int(self) -> bool:
 174        """
 175        Checks whether a Literal expression is an integer.
 176        """
 177        if self.is_number:
 178            try:
 179                int(self.name)
 180                return True
 181            except ValueError:
 182                pass
 183        return False
 184
 185    @property
 186    def is_star(self) -> bool:
 187        """Checks whether an expression is a star."""
 188        return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star))
 189
 190    @property
 191    def alias(self) -> str:
 192        """
 193        Returns the alias of the expression, or an empty string if it's not aliased.
 194        """
 195        if isinstance(self.args.get("alias"), TableAlias):
 196            return self.args["alias"].name
 197        return self.text("alias")
 198
 199    @property
 200    def alias_column_names(self) -> t.List[str]:
 201        table_alias = self.args.get("alias")
 202        if not table_alias:
 203            return []
 204        return [c.name for c in table_alias.args.get("columns") or []]
 205
 206    @property
 207    def name(self) -> str:
 208        return self.text("this")
 209
 210    @property
 211    def alias_or_name(self) -> str:
 212        return self.alias or self.name
 213
 214    @property
 215    def output_name(self) -> str:
 216        """
 217        Name of the output column if this expression is a selection.
 218
 219        If the Expression has no output name, an empty string is returned.
 220
 221        Example:
 222            >>> from sqlglot import parse_one
 223            >>> parse_one("SELECT a").expressions[0].output_name
 224            'a'
 225            >>> parse_one("SELECT b AS c").expressions[0].output_name
 226            'c'
 227            >>> parse_one("SELECT 1 + 2").expressions[0].output_name
 228            ''
 229        """
 230        return ""
 231
 232    @property
 233    def type(self) -> t.Optional[DataType]:
 234        return self._type
 235
 236    @type.setter
 237    def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None:
 238        if dtype and not isinstance(dtype, DataType):
 239            dtype = DataType.build(dtype)
 240        self._type = dtype  # type: ignore
 241
 242    def is_type(self, *dtypes) -> bool:
 243        return self.type is not None and self.type.is_type(*dtypes)
 244
 245    @property
 246    def meta(self) -> t.Dict[str, t.Any]:
 247        if self._meta is None:
 248            self._meta = {}
 249        return self._meta
 250
 251    def __deepcopy__(self, memo):
 252        copy = self.__class__(**deepcopy(self.args))
 253        if self.comments is not None:
 254            copy.comments = deepcopy(self.comments)
 255
 256        if self._type is not None:
 257            copy._type = self._type.copy()
 258
 259        if self._meta is not None:
 260            copy._meta = deepcopy(self._meta)
 261
 262        return copy
 263
 264    def copy(self):
 265        """
 266        Returns a deep copy of the expression.
 267        """
 268        new = deepcopy(self)
 269        new.parent = self.parent
 270        return new
 271
 272    def add_comments(self, comments: t.Optional[t.List[str]]) -> None:
 273        if self.comments is None:
 274            self.comments = []
 275        if comments:
 276            for comment in comments:
 277                _, *meta = comment.split(SQLGLOT_META)
 278                if meta:
 279                    for kv in "".join(meta).split(","):
 280                        k, *v = kv.split("=")
 281                        value = v[0].strip() if v else True
 282                        self.meta[k.strip()] = value
 283                self.comments.append(comment)
 284
 285    def append(self, arg_key: str, value: t.Any) -> None:
 286        """
 287        Appends value to arg_key if it's a list or sets it as a new list.
 288
 289        Args:
 290            arg_key (str): name of the list expression arg
 291            value (Any): value to append to the list
 292        """
 293        if not isinstance(self.args.get(arg_key), list):
 294            self.args[arg_key] = []
 295        self.args[arg_key].append(value)
 296        self._set_parent(arg_key, value)
 297
 298    def set(self, arg_key: str, value: t.Any) -> None:
 299        """
 300        Sets arg_key to value.
 301
 302        Args:
 303            arg_key: name of the expression arg.
 304            value: value to set the arg to.
 305        """
 306        if value is None:
 307            self.args.pop(arg_key, None)
 308            return
 309
 310        self.args[arg_key] = value
 311        self._set_parent(arg_key, value)
 312
 313    def _set_parent(self, arg_key: str, value: t.Any) -> None:
 314        if hasattr(value, "parent"):
 315            value.parent = self
 316            value.arg_key = arg_key
 317        elif type(value) is list:
 318            for v in value:
 319                if hasattr(v, "parent"):
 320                    v.parent = self
 321                    v.arg_key = arg_key
 322
 323    @property
 324    def depth(self) -> int:
 325        """
 326        Returns the depth of this tree.
 327        """
 328        if self.parent:
 329            return self.parent.depth + 1
 330        return 0
 331
 332    def iter_expressions(self) -> t.Iterator[t.Tuple[str, Expression]]:
 333        """Yields the key and expression for all arguments, exploding list args."""
 334        for k, vs in self.args.items():
 335            if type(vs) is list:
 336                for v in vs:
 337                    if hasattr(v, "parent"):
 338                        yield k, v
 339            else:
 340                if hasattr(vs, "parent"):
 341                    yield k, vs
 342
 343    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
 344        """
 345        Returns the first node in this tree which matches at least one of
 346        the specified types.
 347
 348        Args:
 349            expression_types: the expression type(s) to match.
 350            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
 351
 352        Returns:
 353            The node which matches the criteria or None if no such node was found.
 354        """
 355        return next(self.find_all(*expression_types, bfs=bfs), None)
 356
 357    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
 358        """
 359        Returns a generator object which visits all nodes in this tree and only
 360        yields those that match at least one of the specified expression types.
 361
 362        Args:
 363            expression_types: the expression type(s) to match.
 364            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
 365
 366        Returns:
 367            The generator object.
 368        """
 369        for expression, *_ in self.walk(bfs=bfs):
 370            if isinstance(expression, expression_types):
 371                yield expression
 372
 373    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
 374        """
 375        Returns a nearest parent matching expression_types.
 376
 377        Args:
 378            expression_types: the expression type(s) to match.
 379
 380        Returns:
 381            The parent node.
 382        """
 383        ancestor = self.parent
 384        while ancestor and not isinstance(ancestor, expression_types):
 385            ancestor = ancestor.parent
 386        return t.cast(E, ancestor)
 387
 388    @property
 389    def parent_select(self) -> t.Optional[Select]:
 390        """
 391        Returns the parent select statement.
 392        """
 393        return self.find_ancestor(Select)
 394
 395    @property
 396    def same_parent(self) -> bool:
 397        """Returns if the parent is the same class as itself."""
 398        return type(self.parent) is self.__class__
 399
 400    def root(self) -> Expression:
 401        """
 402        Returns the root expression of this tree.
 403        """
 404        expression = self
 405        while expression.parent:
 406            expression = expression.parent
 407        return expression
 408
 409    def walk(self, bfs=True, prune=None):
 410        """
 411        Returns a generator object which visits all nodes in this tree.
 412
 413        Args:
 414            bfs (bool): if set to True the BFS traversal order will be applied,
 415                otherwise the DFS traversal will be used instead.
 416            prune ((node, parent, arg_key) -> bool): callable that returns True if
 417                the generator should stop traversing this branch of the tree.
 418
 419        Returns:
 420            the generator object.
 421        """
 422        if bfs:
 423            yield from self.bfs(prune=prune)
 424        else:
 425            yield from self.dfs(prune=prune)
 426
 427    def dfs(self, parent=None, key=None, prune=None):
 428        """
 429        Returns a generator object which visits all nodes in this tree in
 430        the DFS (Depth-first) order.
 431
 432        Returns:
 433            The generator object.
 434        """
 435        parent = parent or self.parent
 436        yield self, parent, key
 437        if prune and prune(self, parent, key):
 438            return
 439
 440        for k, v in self.iter_expressions():
 441            yield from v.dfs(self, k, prune)
 442
 443    def bfs(self, prune=None):
 444        """
 445        Returns a generator object which visits all nodes in this tree in
 446        the BFS (Breadth-first) order.
 447
 448        Returns:
 449            The generator object.
 450        """
 451        queue = deque([(self, self.parent, None)])
 452
 453        while queue:
 454            item, parent, key = queue.popleft()
 455
 456            yield item, parent, key
 457            if prune and prune(item, parent, key):
 458                continue
 459
 460            for k, v in item.iter_expressions():
 461                queue.append((v, item, k))
 462
 463    def unnest(self):
 464        """
 465        Returns the first non parenthesis child or self.
 466        """
 467        expression = self
 468        while type(expression) is Paren:
 469            expression = expression.this
 470        return expression
 471
 472    def unalias(self):
 473        """
 474        Returns the inner expression if this is an Alias.
 475        """
 476        if isinstance(self, Alias):
 477            return self.this
 478        return self
 479
 480    def unnest_operands(self):
 481        """
 482        Returns unnested operands as a tuple.
 483        """
 484        return tuple(arg.unnest() for _, arg in self.iter_expressions())
 485
 486    def flatten(self, unnest=True):
 487        """
 488        Returns a generator which yields child nodes whose parents are the same class.
 489
 490        A AND B AND C -> [A, B, C]
 491        """
 492        for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__):
 493            if not type(node) is self.__class__:
 494                yield node.unnest() if unnest and not isinstance(node, Subquery) else node
 495
 496    def __str__(self) -> str:
 497        return self.sql()
 498
 499    def __repr__(self) -> str:
 500        return self._to_s()
 501
 502    def sql(self, dialect: DialectType = None, **opts) -> str:
 503        """
 504        Returns SQL string representation of this tree.
 505
 506        Args:
 507            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
 508            opts: other `sqlglot.generator.Generator` options.
 509
 510        Returns:
 511            The SQL string.
 512        """
 513        from sqlglot.dialects import Dialect
 514
 515        return Dialect.get_or_raise(dialect).generate(self, **opts)
 516
 517    def _to_s(self, hide_missing: bool = True, level: int = 0) -> str:
 518        indent = "" if not level else "\n"
 519        indent += "".join(["  "] * level)
 520        left = f"({self.key.upper()} "
 521
 522        args: t.Dict[str, t.Any] = {
 523            k: ", ".join(
 524                v._to_s(hide_missing=hide_missing, level=level + 1)
 525                if hasattr(v, "_to_s")
 526                else str(v)
 527                for v in ensure_list(vs)
 528                if v is not None
 529            )
 530            for k, vs in self.args.items()
 531        }
 532        args["comments"] = self.comments
 533        args["type"] = self.type
 534        args = {k: v for k, v in args.items() if v or not hide_missing}
 535
 536        right = ", ".join(f"{k}: {v}" for k, v in args.items())
 537        right += ")"
 538
 539        return indent + left + right
 540
 541    def transform(self, fun, *args, copy=True, **kwargs):
 542        """
 543        Recursively visits all tree nodes (excluding already transformed ones)
 544        and applies the given transformation function to each node.
 545
 546        Args:
 547            fun (function): a function which takes a node as an argument and returns a
 548                new transformed node or the same node without modifications. If the function
 549                returns None, then the corresponding node will be removed from the syntax tree.
 550            copy (bool): if set to True a new tree instance is constructed, otherwise the tree is
 551                modified in place.
 552
 553        Returns:
 554            The transformed tree.
 555        """
 556        node = self.copy() if copy else self
 557        new_node = fun(node, *args, **kwargs)
 558
 559        if new_node is None or not isinstance(new_node, Expression):
 560            return new_node
 561        if new_node is not node:
 562            new_node.parent = node.parent
 563            return new_node
 564
 565        replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs))
 566        return new_node
 567
 568    @t.overload
 569    def replace(self, expression: E) -> E:
 570        ...
 571
 572    @t.overload
 573    def replace(self, expression: None) -> None:
 574        ...
 575
 576    def replace(self, expression):
 577        """
 578        Swap out this expression with a new expression.
 579
 580        For example::
 581
 582            >>> tree = Select().select("x").from_("tbl")
 583            >>> tree.find(Column).replace(Column(this="y"))
 584            (COLUMN this: y)
 585            >>> tree.sql()
 586            'SELECT y FROM tbl'
 587
 588        Args:
 589            expression: new node
 590
 591        Returns:
 592            The new expression or expressions.
 593        """
 594        if not self.parent:
 595            return expression
 596
 597        parent = self.parent
 598        self.parent = None
 599
 600        replace_children(parent, lambda child: expression if child is self else child)
 601        return expression
 602
 603    def pop(self: E) -> E:
 604        """
 605        Remove this expression from its AST.
 606
 607        Returns:
 608            The popped expression.
 609        """
 610        self.replace(None)
 611        return self
 612
 613    def assert_is(self, type_: t.Type[E]) -> E:
 614        """
 615        Assert that this `Expression` is an instance of `type_`.
 616
 617        If it is NOT an instance of `type_`, this raises an assertion error.
 618        Otherwise, this returns this expression.
 619
 620        Examples:
 621            This is useful for type security in chained expressions:
 622
 623            >>> import sqlglot
 624            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
 625            'SELECT x, z FROM y'
 626        """
 627        assert isinstance(self, type_)
 628        return self
 629
 630    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
 631        """
 632        Checks if this expression is valid (e.g. all mandatory args are set).
 633
 634        Args:
 635            args: a sequence of values that were used to instantiate a Func expression. This is used
 636                to check that the provided arguments don't exceed the function argument limit.
 637
 638        Returns:
 639            A list of error messages for all possible errors that were found.
 640        """
 641        errors: t.List[str] = []
 642
 643        for k in self.args:
 644            if k not in self.arg_types:
 645                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
 646        for k, mandatory in self.arg_types.items():
 647            v = self.args.get(k)
 648            if mandatory and (v is None or (isinstance(v, list) and not v)):
 649                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
 650
 651        if (
 652            args
 653            and isinstance(self, Func)
 654            and len(args) > len(self.arg_types)
 655            and not self.is_var_len_args
 656        ):
 657            errors.append(
 658                f"The number of provided arguments ({len(args)}) is greater than "
 659                f"the maximum number of supported arguments ({len(self.arg_types)})"
 660            )
 661
 662        return errors
 663
 664    def dump(self):
 665        """
 666        Dump this Expression to a JSON-serializable dict.
 667        """
 668        from sqlglot.serde import dump
 669
 670        return dump(self)
 671
 672    @classmethod
 673    def load(cls, obj):
 674        """
 675        Load a dict (as returned by `Expression.dump`) into an Expression instance.
 676        """
 677        from sqlglot.serde import load
 678
 679        return load(obj)
 680
 681    def and_(
 682        self,
 683        *expressions: t.Optional[ExpOrStr],
 684        dialect: DialectType = None,
 685        copy: bool = True,
 686        **opts,
 687    ) -> Condition:
 688        """
 689        AND this condition with one or multiple expressions.
 690
 691        Example:
 692            >>> condition("x=1").and_("y=1").sql()
 693            'x = 1 AND y = 1'
 694
 695        Args:
 696            *expressions: the SQL code strings to parse.
 697                If an `Expression` instance is passed, it will be used as-is.
 698            dialect: the dialect used to parse the input expression.
 699            copy: whether or not to copy the involved expressions (only applies to Expressions).
 700            opts: other options to use to parse the input expressions.
 701
 702        Returns:
 703            The new And condition.
 704        """
 705        return and_(self, *expressions, dialect=dialect, copy=copy, **opts)
 706
 707    def or_(
 708        self,
 709        *expressions: t.Optional[ExpOrStr],
 710        dialect: DialectType = None,
 711        copy: bool = True,
 712        **opts,
 713    ) -> Condition:
 714        """
 715        OR this condition with one or multiple expressions.
 716
 717        Example:
 718            >>> condition("x=1").or_("y=1").sql()
 719            'x = 1 OR y = 1'
 720
 721        Args:
 722            *expressions: the SQL code strings to parse.
 723                If an `Expression` instance is passed, it will be used as-is.
 724            dialect: the dialect used to parse the input expression.
 725            copy: whether or not to copy the involved expressions (only applies to Expressions).
 726            opts: other options to use to parse the input expressions.
 727
 728        Returns:
 729            The new Or condition.
 730        """
 731        return or_(self, *expressions, dialect=dialect, copy=copy, **opts)
 732
 733    def not_(self, copy: bool = True):
 734        """
 735        Wrap this condition with NOT.
 736
 737        Example:
 738            >>> condition("x=1").not_().sql()
 739            'NOT x = 1'
 740
 741        Args:
 742            copy: whether or not to copy this object.
 743
 744        Returns:
 745            The new Not instance.
 746        """
 747        return not_(self, copy=copy)
 748
 749    def as_(
 750        self,
 751        alias: str | Identifier,
 752        quoted: t.Optional[bool] = None,
 753        dialect: DialectType = None,
 754        copy: bool = True,
 755        **opts,
 756    ) -> Alias:
 757        return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts)
 758
 759    def _binop(self, klass: t.Type[E], other: t.Any, reverse: bool = False) -> E:
 760        this = self.copy()
 761        other = convert(other, copy=True)
 762        if not isinstance(this, klass) and not isinstance(other, klass):
 763            this = _wrap(this, Binary)
 764            other = _wrap(other, Binary)
 765        if reverse:
 766            return klass(this=other, expression=this)
 767        return klass(this=this, expression=other)
 768
 769    def __getitem__(self, other: ExpOrStr | t.Tuple[ExpOrStr]) -> Bracket:
 770        return Bracket(
 771            this=self.copy(), expressions=[convert(e, copy=True) for e in ensure_list(other)]
 772        )
 773
 774    def __iter__(self) -> t.Iterator:
 775        if "expressions" in self.arg_types:
 776            return iter(self.args.get("expressions") or [])
 777        # We define this because __getitem__ converts Expression into an iterable, which is
 778        # problematic because one can hit infinite loops if they do "for x in some_expr: ..."
 779        # See: https://peps.python.org/pep-0234/
 780        raise TypeError(f"'{self.__class__.__name__}' object is not iterable")
 781
 782    def isin(
 783        self,
 784        *expressions: t.Any,
 785        query: t.Optional[ExpOrStr] = None,
 786        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
 787        copy: bool = True,
 788        **opts,
 789    ) -> In:
 790        return In(
 791            this=maybe_copy(self, copy),
 792            expressions=[convert(e, copy=copy) for e in expressions],
 793            query=maybe_parse(query, copy=copy, **opts) if query else None,
 794            unnest=Unnest(
 795                expressions=[
 796                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
 797                ]
 798            )
 799            if unnest
 800            else None,
 801        )
 802
 803    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
 804        return Between(
 805            this=maybe_copy(self, copy),
 806            low=convert(low, copy=copy, **opts),
 807            high=convert(high, copy=copy, **opts),
 808        )
 809
 810    def is_(self, other: ExpOrStr) -> Is:
 811        return self._binop(Is, other)
 812
 813    def like(self, other: ExpOrStr) -> Like:
 814        return self._binop(Like, other)
 815
 816    def ilike(self, other: ExpOrStr) -> ILike:
 817        return self._binop(ILike, other)
 818
 819    def eq(self, other: t.Any) -> EQ:
 820        return self._binop(EQ, other)
 821
 822    def neq(self, other: t.Any) -> NEQ:
 823        return self._binop(NEQ, other)
 824
 825    def rlike(self, other: ExpOrStr) -> RegexpLike:
 826        return self._binop(RegexpLike, other)
 827
 828    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
 829        div = self._binop(Div, other)
 830        div.args["typed"] = typed
 831        div.args["safe"] = safe
 832        return div
 833
 834    def __lt__(self, other: t.Any) -> LT:
 835        return self._binop(LT, other)
 836
 837    def __le__(self, other: t.Any) -> LTE:
 838        return self._binop(LTE, other)
 839
 840    def __gt__(self, other: t.Any) -> GT:
 841        return self._binop(GT, other)
 842
 843    def __ge__(self, other: t.Any) -> GTE:
 844        return self._binop(GTE, other)
 845
 846    def __add__(self, other: t.Any) -> Add:
 847        return self._binop(Add, other)
 848
 849    def __radd__(self, other: t.Any) -> Add:
 850        return self._binop(Add, other, reverse=True)
 851
 852    def __sub__(self, other: t.Any) -> Sub:
 853        return self._binop(Sub, other)
 854
 855    def __rsub__(self, other: t.Any) -> Sub:
 856        return self._binop(Sub, other, reverse=True)
 857
 858    def __mul__(self, other: t.Any) -> Mul:
 859        return self._binop(Mul, other)
 860
 861    def __rmul__(self, other: t.Any) -> Mul:
 862        return self._binop(Mul, other, reverse=True)
 863
 864    def __truediv__(self, other: t.Any) -> Div:
 865        return self._binop(Div, other)
 866
 867    def __rtruediv__(self, other: t.Any) -> Div:
 868        return self._binop(Div, other, reverse=True)
 869
 870    def __floordiv__(self, other: t.Any) -> IntDiv:
 871        return self._binop(IntDiv, other)
 872
 873    def __rfloordiv__(self, other: t.Any) -> IntDiv:
 874        return self._binop(IntDiv, other, reverse=True)
 875
 876    def __mod__(self, other: t.Any) -> Mod:
 877        return self._binop(Mod, other)
 878
 879    def __rmod__(self, other: t.Any) -> Mod:
 880        return self._binop(Mod, other, reverse=True)
 881
 882    def __pow__(self, other: t.Any) -> Pow:
 883        return self._binop(Pow, other)
 884
 885    def __rpow__(self, other: t.Any) -> Pow:
 886        return self._binop(Pow, other, reverse=True)
 887
 888    def __and__(self, other: t.Any) -> And:
 889        return self._binop(And, other)
 890
 891    def __rand__(self, other: t.Any) -> And:
 892        return self._binop(And, other, reverse=True)
 893
 894    def __or__(self, other: t.Any) -> Or:
 895        return self._binop(Or, other)
 896
 897    def __ror__(self, other: t.Any) -> Or:
 898        return self._binop(Or, other, reverse=True)
 899
 900    def __neg__(self) -> Neg:
 901        return Neg(this=_wrap(self.copy(), Binary))
 902
 903    def __invert__(self) -> Not:
 904        return not_(self.copy())
 905
 906
 907IntoType = t.Union[
 908    str,
 909    t.Type[Expression],
 910    t.Collection[t.Union[str, t.Type[Expression]]],
 911]
 912ExpOrStr = t.Union[str, Expression]
 913
 914
 915class Condition(Expression):
 916    """Logical conditions like x AND y, or simply x"""
 917
 918
 919class Predicate(Condition):
 920    """Relationships like x = y, x > 1, x >= y."""
 921
 922
 923class DerivedTable(Expression):
 924    @property
 925    def selects(self) -> t.List[Expression]:
 926        return self.this.selects if isinstance(self.this, Subqueryable) else []
 927
 928    @property
 929    def named_selects(self) -> t.List[str]:
 930        return [select.output_name for select in self.selects]
 931
 932
 933class Unionable(Expression):
 934    def union(
 935        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 936    ) -> Unionable:
 937        """
 938        Builds a UNION expression.
 939
 940        Example:
 941            >>> import sqlglot
 942            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
 943            'SELECT * FROM foo UNION SELECT * FROM bla'
 944
 945        Args:
 946            expression: the SQL code string.
 947                If an `Expression` instance is passed, it will be used as-is.
 948            distinct: set the DISTINCT flag if and only if this is true.
 949            dialect: the dialect used to parse the input expression.
 950            opts: other options to use to parse the input expressions.
 951
 952        Returns:
 953            The new Union expression.
 954        """
 955        return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
 956
 957    def intersect(
 958        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 959    ) -> Unionable:
 960        """
 961        Builds an INTERSECT expression.
 962
 963        Example:
 964            >>> import sqlglot
 965            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
 966            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
 967
 968        Args:
 969            expression: the SQL code string.
 970                If an `Expression` instance is passed, it will be used as-is.
 971            distinct: set the DISTINCT flag if and only if this is true.
 972            dialect: the dialect used to parse the input expression.
 973            opts: other options to use to parse the input expressions.
 974
 975        Returns:
 976            The new Intersect expression.
 977        """
 978        return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
 979
 980    def except_(
 981        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 982    ) -> Unionable:
 983        """
 984        Builds an EXCEPT expression.
 985
 986        Example:
 987            >>> import sqlglot
 988            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
 989            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
 990
 991        Args:
 992            expression: the SQL code string.
 993                If an `Expression` instance is passed, it will be used as-is.
 994            distinct: set the DISTINCT flag if and only if this is true.
 995            dialect: the dialect used to parse the input expression.
 996            opts: other options to use to parse the input expressions.
 997
 998        Returns:
 999            The new Except expression.
1000        """
1001        return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
1002
1003
1004class UDTF(DerivedTable, Unionable):
1005    @property
1006    def selects(self) -> t.List[Expression]:
1007        alias = self.args.get("alias")
1008        return alias.columns if alias else []
1009
1010
1011class Cache(Expression):
1012    arg_types = {
1013        "this": True,
1014        "lazy": False,
1015        "options": False,
1016        "expression": False,
1017    }
1018
1019
1020class Uncache(Expression):
1021    arg_types = {"this": True, "exists": False}
1022
1023
1024class Refresh(Expression):
1025    pass
1026
1027
1028class DDL(Expression):
1029    @property
1030    def ctes(self):
1031        with_ = self.args.get("with")
1032        if not with_:
1033            return []
1034        return with_.expressions
1035
1036    @property
1037    def named_selects(self) -> t.List[str]:
1038        if isinstance(self.expression, Subqueryable):
1039            return self.expression.named_selects
1040        return []
1041
1042    @property
1043    def selects(self) -> t.List[Expression]:
1044        if isinstance(self.expression, Subqueryable):
1045            return self.expression.selects
1046        return []
1047
1048
1049class DML(Expression):
1050    def returning(
1051        self,
1052        expression: ExpOrStr,
1053        dialect: DialectType = None,
1054        copy: bool = True,
1055        **opts,
1056    ) -> DML:
1057        """
1058        Set the RETURNING expression. Not supported by all dialects.
1059
1060        Example:
1061            >>> delete("tbl").returning("*", dialect="postgres").sql()
1062            'DELETE FROM tbl RETURNING *'
1063
1064        Args:
1065            expression: the SQL code strings to parse.
1066                If an `Expression` instance is passed, it will be used as-is.
1067            dialect: the dialect used to parse the input expressions.
1068            copy: if `False`, modify this expression instance in-place.
1069            opts: other options to use to parse the input expressions.
1070
1071        Returns:
1072            Delete: the modified expression.
1073        """
1074        return _apply_builder(
1075            expression=expression,
1076            instance=self,
1077            arg="returning",
1078            prefix="RETURNING",
1079            dialect=dialect,
1080            copy=copy,
1081            into=Returning,
1082            **opts,
1083        )
1084
1085
1086class Create(DDL):
1087    arg_types = {
1088        "with": False,
1089        "this": True,
1090        "kind": True,
1091        "expression": False,
1092        "exists": False,
1093        "properties": False,
1094        "replace": False,
1095        "unique": False,
1096        "indexes": False,
1097        "no_schema_binding": False,
1098        "begin": False,
1099        "end": False,
1100        "clone": False,
1101    }
1102
1103
1104# https://docs.snowflake.com/en/sql-reference/sql/create-clone
1105# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_clone_statement
1106# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_copy
1107class Clone(Expression):
1108    arg_types = {"this": True, "shallow": False, "copy": False}
1109
1110
1111class Describe(Expression):
1112    arg_types = {"this": True, "kind": False, "expressions": False}
1113
1114
1115class Kill(Expression):
1116    arg_types = {"this": True, "kind": False}
1117
1118
1119class Pragma(Expression):
1120    pass
1121
1122
1123class Set(Expression):
1124    arg_types = {"expressions": False, "unset": False, "tag": False}
1125
1126
1127class SetItem(Expression):
1128    arg_types = {
1129        "this": False,
1130        "expressions": False,
1131        "kind": False,
1132        "collate": False,  # MySQL SET NAMES statement
1133        "global": False,
1134    }
1135
1136
1137class Show(Expression):
1138    arg_types = {
1139        "this": True,
1140        "target": False,
1141        "offset": False,
1142        "limit": False,
1143        "like": False,
1144        "where": False,
1145        "db": False,
1146        "scope": False,
1147        "scope_kind": False,
1148        "full": False,
1149        "mutex": False,
1150        "query": False,
1151        "channel": False,
1152        "global": False,
1153        "log": False,
1154        "position": False,
1155        "types": False,
1156    }
1157
1158
1159class UserDefinedFunction(Expression):
1160    arg_types = {"this": True, "expressions": False, "wrapped": False}
1161
1162
1163class CharacterSet(Expression):
1164    arg_types = {"this": True, "default": False}
1165
1166
1167class With(Expression):
1168    arg_types = {"expressions": True, "recursive": False}
1169
1170    @property
1171    def recursive(self) -> bool:
1172        return bool(self.args.get("recursive"))
1173
1174
1175class WithinGroup(Expression):
1176    arg_types = {"this": True, "expression": False}
1177
1178
1179# clickhouse supports scalar ctes
1180# https://clickhouse.com/docs/en/sql-reference/statements/select/with
1181class CTE(DerivedTable):
1182    arg_types = {"this": True, "alias": True, "scalar": False}
1183
1184
1185class TableAlias(Expression):
1186    arg_types = {"this": False, "columns": False}
1187
1188    @property
1189    def columns(self):
1190        return self.args.get("columns") or []
1191
1192
1193class BitString(Condition):
1194    pass
1195
1196
1197class HexString(Condition):
1198    pass
1199
1200
1201class ByteString(Condition):
1202    pass
1203
1204
1205class RawString(Condition):
1206    pass
1207
1208
1209class Column(Condition):
1210    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1211
1212    @property
1213    def table(self) -> str:
1214        return self.text("table")
1215
1216    @property
1217    def db(self) -> str:
1218        return self.text("db")
1219
1220    @property
1221    def catalog(self) -> str:
1222        return self.text("catalog")
1223
1224    @property
1225    def output_name(self) -> str:
1226        return self.name
1227
1228    @property
1229    def parts(self) -> t.List[Identifier]:
1230        """Return the parts of a column in order catalog, db, table, name."""
1231        return [
1232            t.cast(Identifier, self.args[part])
1233            for part in ("catalog", "db", "table", "this")
1234            if self.args.get(part)
1235        ]
1236
1237    def to_dot(self) -> Dot | Identifier:
1238        """Converts the column into a dot expression."""
1239        parts = self.parts
1240        parent = self.parent
1241
1242        while parent:
1243            if isinstance(parent, Dot):
1244                parts.append(parent.expression)
1245            parent = parent.parent
1246
1247        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]
1248
1249
1250class ColumnPosition(Expression):
1251    arg_types = {"this": False, "position": True}
1252
1253
1254class ColumnDef(Expression):
1255    arg_types = {
1256        "this": True,
1257        "kind": False,
1258        "constraints": False,
1259        "exists": False,
1260        "position": False,
1261    }
1262
1263    @property
1264    def constraints(self) -> t.List[ColumnConstraint]:
1265        return self.args.get("constraints") or []
1266
1267
1268class AlterColumn(Expression):
1269    arg_types = {
1270        "this": True,
1271        "dtype": False,
1272        "collate": False,
1273        "using": False,
1274        "default": False,
1275        "drop": False,
1276    }
1277
1278
1279class RenameTable(Expression):
1280    pass
1281
1282
1283class SwapTable(Expression):
1284    pass
1285
1286
1287class Comment(Expression):
1288    arg_types = {"this": True, "kind": True, "expression": True, "exists": False}
1289
1290
1291class Comprehension(Expression):
1292    arg_types = {"this": True, "expression": True, "iterator": True, "condition": False}
1293
1294
1295# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
1296class MergeTreeTTLAction(Expression):
1297    arg_types = {
1298        "this": True,
1299        "delete": False,
1300        "recompress": False,
1301        "to_disk": False,
1302        "to_volume": False,
1303    }
1304
1305
1306# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
1307class MergeTreeTTL(Expression):
1308    arg_types = {
1309        "expressions": True,
1310        "where": False,
1311        "group": False,
1312        "aggregates": False,
1313    }
1314
1315
1316# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1317class IndexConstraintOption(Expression):
1318    arg_types = {
1319        "key_block_size": False,
1320        "using": False,
1321        "parser": False,
1322        "comment": False,
1323        "visible": False,
1324        "engine_attr": False,
1325        "secondary_engine_attr": False,
1326    }
1327
1328
1329class ColumnConstraint(Expression):
1330    arg_types = {"this": False, "kind": True}
1331
1332    @property
1333    def kind(self) -> ColumnConstraintKind:
1334        return self.args["kind"]
1335
1336
1337class ColumnConstraintKind(Expression):
1338    pass
1339
1340
1341class AutoIncrementColumnConstraint(ColumnConstraintKind):
1342    pass
1343
1344
1345class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1346    arg_types = {"this": True, "expression": True}
1347
1348
1349class CaseSpecificColumnConstraint(ColumnConstraintKind):
1350    arg_types = {"not_": True}
1351
1352
1353class CharacterSetColumnConstraint(ColumnConstraintKind):
1354    arg_types = {"this": True}
1355
1356
1357class CheckColumnConstraint(ColumnConstraintKind):
1358    pass
1359
1360
1361class ClusteredColumnConstraint(ColumnConstraintKind):
1362    pass
1363
1364
1365class CollateColumnConstraint(ColumnConstraintKind):
1366    pass
1367
1368
1369class CommentColumnConstraint(ColumnConstraintKind):
1370    pass
1371
1372
1373class CompressColumnConstraint(ColumnConstraintKind):
1374    pass
1375
1376
1377class DateFormatColumnConstraint(ColumnConstraintKind):
1378    arg_types = {"this": True}
1379
1380
1381class DefaultColumnConstraint(ColumnConstraintKind):
1382    pass
1383
1384
1385class EncodeColumnConstraint(ColumnConstraintKind):
1386    pass
1387
1388
1389class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1390    # this: True -> ALWAYS, this: False -> BY DEFAULT
1391    arg_types = {
1392        "this": False,
1393        "expression": False,
1394        "on_null": False,
1395        "start": False,
1396        "increment": False,
1397        "minvalue": False,
1398        "maxvalue": False,
1399        "cycle": False,
1400    }
1401
1402
1403class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1404    arg_types = {"start": True, "hidden": False}
1405
1406
1407# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1408class IndexColumnConstraint(ColumnConstraintKind):
1409    arg_types = {
1410        "this": False,
1411        "schema": True,
1412        "kind": False,
1413        "index_type": False,
1414        "options": False,
1415    }
1416
1417
1418class InlineLengthColumnConstraint(ColumnConstraintKind):
1419    pass
1420
1421
1422class NonClusteredColumnConstraint(ColumnConstraintKind):
1423    pass
1424
1425
1426class NotForReplicationColumnConstraint(ColumnConstraintKind):
1427    arg_types = {}
1428
1429
1430class NotNullColumnConstraint(ColumnConstraintKind):
1431    arg_types = {"allow_null": False}
1432
1433
1434# https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
1435class OnUpdateColumnConstraint(ColumnConstraintKind):
1436    pass
1437
1438
1439# https://docs.snowflake.com/en/sql-reference/sql/create-external-table#optional-parameters
1440class TransformColumnConstraint(ColumnConstraintKind):
1441    pass
1442
1443
1444class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1445    arg_types = {"desc": False}
1446
1447
1448class TitleColumnConstraint(ColumnConstraintKind):
1449    pass
1450
1451
1452class UniqueColumnConstraint(ColumnConstraintKind):
1453    arg_types = {"this": False, "index_type": False}
1454
1455
1456class UppercaseColumnConstraint(ColumnConstraintKind):
1457    arg_types: t.Dict[str, t.Any] = {}
1458
1459
1460class PathColumnConstraint(ColumnConstraintKind):
1461    pass
1462
1463
1464# computed column expression
1465# https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver16
1466class ComputedColumnConstraint(ColumnConstraintKind):
1467    arg_types = {"this": True, "persisted": False, "not_null": False}
1468
1469
1470class Constraint(Expression):
1471    arg_types = {"this": True, "expressions": True}
1472
1473
1474class Delete(DML):
1475    arg_types = {
1476        "with": False,
1477        "this": False,
1478        "using": False,
1479        "where": False,
1480        "returning": False,
1481        "limit": False,
1482        "tables": False,  # Multiple-Table Syntax (MySQL)
1483    }
1484
1485    def delete(
1486        self,
1487        table: ExpOrStr,
1488        dialect: DialectType = None,
1489        copy: bool = True,
1490        **opts,
1491    ) -> Delete:
1492        """
1493        Create a DELETE expression or replace the table on an existing DELETE expression.
1494
1495        Example:
1496            >>> delete("tbl").sql()
1497            'DELETE FROM tbl'
1498
1499        Args:
1500            table: the table from which to delete.
1501            dialect: the dialect used to parse the input expression.
1502            copy: if `False`, modify this expression instance in-place.
1503            opts: other options to use to parse the input expressions.
1504
1505        Returns:
1506            Delete: the modified expression.
1507        """
1508        return _apply_builder(
1509            expression=table,
1510            instance=self,
1511            arg="this",
1512            dialect=dialect,
1513            into=Table,
1514            copy=copy,
1515            **opts,
1516        )
1517
1518    def where(
1519        self,
1520        *expressions: t.Optional[ExpOrStr],
1521        append: bool = True,
1522        dialect: DialectType = None,
1523        copy: bool = True,
1524        **opts,
1525    ) -> Delete:
1526        """
1527        Append to or set the WHERE expressions.
1528
1529        Example:
1530            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1531            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1532
1533        Args:
1534            *expressions: the SQL code strings to parse.
1535                If an `Expression` instance is passed, it will be used as-is.
1536                Multiple expressions are combined with an AND operator.
1537            append: if `True`, AND the new expressions to any existing expression.
1538                Otherwise, this resets the expression.
1539            dialect: the dialect used to parse the input expressions.
1540            copy: if `False`, modify this expression instance in-place.
1541            opts: other options to use to parse the input expressions.
1542
1543        Returns:
1544            Delete: the modified expression.
1545        """
1546        return _apply_conjunction_builder(
1547            *expressions,
1548            instance=self,
1549            arg="where",
1550            append=append,
1551            into=Where,
1552            dialect=dialect,
1553            copy=copy,
1554            **opts,
1555        )
1556
1557
1558class Drop(Expression):
1559    arg_types = {
1560        "this": False,
1561        "kind": False,
1562        "exists": False,
1563        "temporary": False,
1564        "materialized": False,
1565        "cascade": False,
1566        "constraints": False,
1567        "purge": False,
1568    }
1569
1570
1571class Filter(Expression):
1572    arg_types = {"this": True, "expression": True}
1573
1574
1575class Check(Expression):
1576    pass
1577
1578
1579# https://docs.snowflake.com/en/sql-reference/constructs/connect-by
1580class Connect(Expression):
1581    arg_types = {"start": False, "connect": True}
1582
1583
1584class Prior(Expression):
1585    pass
1586
1587
1588class Directory(Expression):
1589    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
1590    arg_types = {"this": True, "local": False, "row_format": False}
1591
1592
1593class ForeignKey(Expression):
1594    arg_types = {
1595        "expressions": True,
1596        "reference": False,
1597        "delete": False,
1598        "update": False,
1599    }
1600
1601
1602class ColumnPrefix(Expression):
1603    arg_types = {"this": True, "expression": True}
1604
1605
1606class PrimaryKey(Expression):
1607    arg_types = {"expressions": True, "options": False}
1608
1609
1610# https://www.postgresql.org/docs/9.1/sql-selectinto.html
1611# https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_INTO.html#r_SELECT_INTO-examples
1612class Into(Expression):
1613    arg_types = {"this": True, "temporary": False, "unlogged": False}
1614
1615
1616class From(Expression):
1617    @property
1618    def name(self) -> str:
1619        return self.this.name
1620
1621    @property
1622    def alias_or_name(self) -> str:
1623        return self.this.alias_or_name
1624
1625
1626class Having(Expression):
1627    pass
1628
1629
1630class Hint(Expression):
1631    arg_types = {"expressions": True}
1632
1633
1634class JoinHint(Expression):
1635    arg_types = {"this": True, "expressions": True}
1636
1637
1638class Identifier(Expression):
1639    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
1640
1641    @property
1642    def quoted(self) -> bool:
1643        return bool(self.args.get("quoted"))
1644
1645    @property
1646    def hashable_args(self) -> t.Any:
1647        return (self.this, self.quoted)
1648
1649    @property
1650    def output_name(self) -> str:
1651        return self.name
1652
1653
1654# https://www.postgresql.org/docs/current/indexes-opclass.html
1655class Opclass(Expression):
1656    arg_types = {"this": True, "expression": True}
1657
1658
1659class Index(Expression):
1660    arg_types = {
1661        "this": False,
1662        "table": False,
1663        "using": False,
1664        "where": False,
1665        "columns": False,
1666        "unique": False,
1667        "primary": False,
1668        "amp": False,  # teradata
1669        "partition_by": False,  # teradata
1670        "where": False,  # postgres partial indexes
1671    }
1672
1673
1674class Insert(DDL, DML):
1675    arg_types = {
1676        "with": False,
1677        "this": True,
1678        "expression": False,
1679        "conflict": False,
1680        "returning": False,
1681        "overwrite": False,
1682        "exists": False,
1683        "partition": False,
1684        "alternative": False,
1685        "where": False,
1686        "ignore": False,
1687        "by_name": False,
1688    }
1689
1690    def with_(
1691        self,
1692        alias: ExpOrStr,
1693        as_: ExpOrStr,
1694        recursive: t.Optional[bool] = None,
1695        append: bool = True,
1696        dialect: DialectType = None,
1697        copy: bool = True,
1698        **opts,
1699    ) -> Insert:
1700        """
1701        Append to or set the common table expressions.
1702
1703        Example:
1704            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
1705            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
1706
1707        Args:
1708            alias: the SQL code string to parse as the table name.
1709                If an `Expression` instance is passed, this is used as-is.
1710            as_: the SQL code string to parse as the table expression.
1711                If an `Expression` instance is passed, it will be used as-is.
1712            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1713            append: if `True`, add to any existing expressions.
1714                Otherwise, this resets the expressions.
1715            dialect: the dialect used to parse the input expression.
1716            copy: if `False`, modify this expression instance in-place.
1717            opts: other options to use to parse the input expressions.
1718
1719        Returns:
1720            The modified expression.
1721        """
1722        return _apply_cte_builder(
1723            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
1724        )
1725
1726
1727class OnConflict(Expression):
1728    arg_types = {
1729        "duplicate": False,
1730        "expressions": False,
1731        "nothing": False,
1732        "key": False,
1733        "constraint": False,
1734    }
1735
1736
1737class Returning(Expression):
1738    arg_types = {"expressions": True, "into": False}
1739
1740
1741# https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
1742class Introducer(Expression):
1743    arg_types = {"this": True, "expression": True}
1744
1745
1746# national char, like n'utf8'
1747class National(Expression):
1748    pass
1749
1750
1751class LoadData(Expression):
1752    arg_types = {
1753        "this": True,
1754        "local": False,
1755        "overwrite": False,
1756        "inpath": True,
1757        "partition": False,
1758        "input_format": False,
1759        "serde": False,
1760    }
1761
1762
1763class Partition(Expression):
1764    arg_types = {"expressions": True}
1765
1766
1767class Fetch(Expression):
1768    arg_types = {
1769        "direction": False,
1770        "count": False,
1771        "percent": False,
1772        "with_ties": False,
1773    }
1774
1775
1776class Group(Expression):
1777    arg_types = {
1778        "expressions": False,
1779        "grouping_sets": False,
1780        "cube": False,
1781        "rollup": False,
1782        "totals": False,
1783        "all": False,
1784    }
1785
1786
1787class Lambda(Expression):
1788    arg_types = {"this": True, "expressions": True}
1789
1790
1791class Limit(Expression):
1792    arg_types = {"this": False, "expression": True, "offset": False}
1793
1794
1795class Literal(Condition):
1796    arg_types = {"this": True, "is_string": True}
1797
1798    @property
1799    def hashable_args(self) -> t.Any:
1800        return (self.this, self.args.get("is_string"))
1801
1802    @classmethod
1803    def number(cls, number) -> Literal:
1804        return cls(this=str(number), is_string=False)
1805
1806    @classmethod
1807    def string(cls, string) -> Literal:
1808        return cls(this=str(string), is_string=True)
1809
1810    @property
1811    def output_name(self) -> str:
1812        return self.name
1813
1814
1815class Join(Expression):
1816    arg_types = {
1817        "this": True,
1818        "on": False,
1819        "side": False,
1820        "kind": False,
1821        "using": False,
1822        "method": False,
1823        "global": False,
1824        "hint": False,
1825    }
1826
1827    @property
1828    def method(self) -> str:
1829        return self.text("method").upper()
1830
1831    @property
1832    def kind(self) -> str:
1833        return self.text("kind").upper()
1834
1835    @property
1836    def side(self) -> str:
1837        return self.text("side").upper()
1838
1839    @property
1840    def hint(self) -> str:
1841        return self.text("hint").upper()
1842
1843    @property
1844    def alias_or_name(self) -> str:
1845        return self.this.alias_or_name
1846
1847    def on(
1848        self,
1849        *expressions: t.Optional[ExpOrStr],
1850        append: bool = True,
1851        dialect: DialectType = None,
1852        copy: bool = True,
1853        **opts,
1854    ) -> Join:
1855        """
1856        Append to or set the ON expressions.
1857
1858        Example:
1859            >>> import sqlglot
1860            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
1861            'JOIN x ON y = 1'
1862
1863        Args:
1864            *expressions: the SQL code strings to parse.
1865                If an `Expression` instance is passed, it will be used as-is.
1866                Multiple expressions are combined with an AND operator.
1867            append: if `True`, AND the new expressions to any existing expression.
1868                Otherwise, this resets the expression.
1869            dialect: the dialect used to parse the input expressions.
1870            copy: if `False`, modify this expression instance in-place.
1871            opts: other options to use to parse the input expressions.
1872
1873        Returns:
1874            The modified Join expression.
1875        """
1876        join = _apply_conjunction_builder(
1877            *expressions,
1878            instance=self,
1879            arg="on",
1880            append=append,
1881            dialect=dialect,
1882            copy=copy,
1883            **opts,
1884        )
1885
1886        if join.kind == "CROSS":
1887            join.set("kind", None)
1888
1889        return join
1890
1891    def using(
1892        self,
1893        *expressions: t.Optional[ExpOrStr],
1894        append: bool = True,
1895        dialect: DialectType = None,
1896        copy: bool = True,
1897        **opts,
1898    ) -> Join:
1899        """
1900        Append to or set the USING expressions.
1901
1902        Example:
1903            >>> import sqlglot
1904            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
1905            'JOIN x USING (foo, bla)'
1906
1907        Args:
1908            *expressions: the SQL code strings to parse.
1909                If an `Expression` instance is passed, it will be used as-is.
1910            append: if `True`, concatenate the new expressions to the existing "using" list.
1911                Otherwise, this resets the expression.
1912            dialect: the dialect used to parse the input expressions.
1913            copy: if `False`, modify this expression instance in-place.
1914            opts: other options to use to parse the input expressions.
1915
1916        Returns:
1917            The modified Join expression.
1918        """
1919        join = _apply_list_builder(
1920            *expressions,
1921            instance=self,
1922            arg="using",
1923            append=append,
1924            dialect=dialect,
1925            copy=copy,
1926            **opts,
1927        )
1928
1929        if join.kind == "CROSS":
1930            join.set("kind", None)
1931
1932        return join
1933
1934
1935class Lateral(UDTF):
1936    arg_types = {"this": True, "view": False, "outer": False, "alias": False}
1937
1938
1939class MatchRecognize(Expression):
1940    arg_types = {
1941        "partition_by": False,
1942        "order": False,
1943        "measures": False,
1944        "rows": False,
1945        "after": False,
1946        "pattern": False,
1947        "define": False,
1948        "alias": False,
1949    }
1950
1951
1952# Clickhouse FROM FINAL modifier
1953# https://clickhouse.com/docs/en/sql-reference/statements/select/from/#final-modifier
1954class Final(Expression):
1955    pass
1956
1957
1958class Offset(Expression):
1959    arg_types = {"this": False, "expression": True}
1960
1961
1962class Order(Expression):
1963    arg_types = {"this": False, "expressions": True}
1964
1965
1966# hive specific sorts
1967# https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy
1968class Cluster(Order):
1969    pass
1970
1971
1972class Distribute(Order):
1973    pass
1974
1975
1976class Sort(Order):
1977    pass
1978
1979
1980class Ordered(Expression):
1981    arg_types = {"this": True, "desc": False, "nulls_first": True}
1982
1983
1984class Property(Expression):
1985    arg_types = {"this": True, "value": True}
1986
1987
1988class AlgorithmProperty(Property):
1989    arg_types = {"this": True}
1990
1991
1992class AutoIncrementProperty(Property):
1993    arg_types = {"this": True}
1994
1995
1996class BlockCompressionProperty(Property):
1997    arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
1998
1999
2000class CharacterSetProperty(Property):
2001    arg_types = {"this": True, "default": True}
2002
2003
2004class ChecksumProperty(Property):
2005    arg_types = {"on": False, "default": False}
2006
2007
2008class CollateProperty(Property):
2009    arg_types = {"this": True, "default": False}
2010
2011
2012class CopyGrantsProperty(Property):
2013    arg_types = {}
2014
2015
2016class DataBlocksizeProperty(Property):
2017    arg_types = {
2018        "size": False,
2019        "units": False,
2020        "minimum": False,
2021        "maximum": False,
2022        "default": False,
2023    }
2024
2025
2026class DefinerProperty(Property):
2027    arg_types = {"this": True}
2028
2029
2030class DistKeyProperty(Property):
2031    arg_types = {"this": True}
2032
2033
2034class DistStyleProperty(Property):
2035    arg_types = {"this": True}
2036
2037
2038class EngineProperty(Property):
2039    arg_types = {"this": True}
2040
2041
2042class HeapProperty(Property):
2043    arg_types = {}
2044
2045
2046class ToTableProperty(Property):
2047    arg_types = {"this": True}
2048
2049
2050class ExecuteAsProperty(Property):
2051    arg_types = {"this": True}
2052
2053
2054class ExternalProperty(Property):
2055    arg_types = {"this": False}
2056
2057
2058class FallbackProperty(Property):
2059    arg_types = {"no": True, "protection": False}
2060
2061
2062class FileFormatProperty(Property):
2063    arg_types = {"this": True}
2064
2065
2066class FreespaceProperty(Property):
2067    arg_types = {"this": True, "percent": False}
2068
2069
2070class InputModelProperty(Property):
2071    arg_types = {"this": True}
2072
2073
2074class OutputModelProperty(Property):
2075    arg_types = {"this": True}
2076
2077
2078class IsolatedLoadingProperty(Property):
2079    arg_types = {
2080        "no": True,
2081        "concurrent": True,
2082        "for_all": True,
2083        "for_insert": True,
2084        "for_none": True,
2085    }
2086
2087
2088class JournalProperty(Property):
2089    arg_types = {
2090        "no": False,
2091        "dual": False,
2092        "before": False,
2093        "local": False,
2094        "after": False,
2095    }
2096
2097
2098class LanguageProperty(Property):
2099    arg_types = {"this": True}
2100
2101
2102# spark ddl
2103class ClusteredByProperty(Property):
2104    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
2105
2106
2107class DictProperty(Property):
2108    arg_types = {"this": True, "kind": True, "settings": False}
2109
2110
2111class DictSubProperty(Property):
2112    pass
2113
2114
2115class DictRange(Property):
2116    arg_types = {"this": True, "min": True, "max": True}
2117
2118
2119# Clickhouse CREATE ... ON CLUSTER modifier
2120# https://clickhouse.com/docs/en/sql-reference/distributed-ddl
2121class OnCluster(Property):
2122    arg_types = {"this": True}
2123
2124
2125class LikeProperty(Property):
2126    arg_types = {"this": True, "expressions": False}
2127
2128
2129class LocationProperty(Property):
2130    arg_types = {"this": True}
2131
2132
2133class LockingProperty(Property):
2134    arg_types = {
2135        "this": False,
2136        "kind": True,
2137        "for_or_in": False,
2138        "lock_type": True,
2139        "override": False,
2140    }
2141
2142
2143class LogProperty(Property):
2144    arg_types = {"no": True}
2145
2146
2147class MaterializedProperty(Property):
2148    arg_types = {"this": False}
2149
2150
2151class MergeBlockRatioProperty(Property):
2152    arg_types = {"this": False, "no": False, "default": False, "percent": False}
2153
2154
2155class NoPrimaryIndexProperty(Property):
2156    arg_types = {}
2157
2158
2159class OnProperty(Property):
2160    arg_types = {"this": True}
2161
2162
2163class OnCommitProperty(Property):
2164    arg_types = {"delete": False}
2165
2166
2167class PartitionedByProperty(Property):
2168    arg_types = {"this": True}
2169
2170
2171# https://www.postgresql.org/docs/current/sql-createtable.html
2172class PartitionBoundSpec(Expression):
2173    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
2174    arg_types = {
2175        "this": False,
2176        "expression": False,
2177        "from_expressions": False,
2178        "to_expressions": False,
2179    }
2180
2181
2182class PartitionedOfProperty(Property):
2183    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
2184    arg_types = {"this": True, "expression": True}
2185
2186
2187class RemoteWithConnectionModelProperty(Property):
2188    arg_types = {"this": True}
2189
2190
2191class ReturnsProperty(Property):
2192    arg_types = {"this": True, "is_table": False, "table": False}
2193
2194
2195class RowFormatProperty(Property):
2196    arg_types = {"this": True}
2197
2198
2199class RowFormatDelimitedProperty(Property):
2200    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
2201    arg_types = {
2202        "fields": False,
2203        "escaped": False,
2204        "collection_items": False,
2205        "map_keys": False,
2206        "lines": False,
2207        "null": False,
2208        "serde": False,
2209    }
2210
2211
2212class RowFormatSerdeProperty(Property):
2213    arg_types = {"this": True, "serde_properties": False}
2214
2215
2216# https://spark.apache.org/docs/3.1.2/sql-ref-syntax-qry-select-transform.html
2217class QueryTransform(Expression):
2218    arg_types = {
2219        "expressions": True,
2220        "command_script": True,
2221        "schema": False,
2222        "row_format_before": False,
2223        "record_writer": False,
2224        "row_format_after": False,
2225        "record_reader": False,
2226    }
2227
2228
2229class SampleProperty(Property):
2230    arg_types = {"this": True}
2231
2232
2233class SchemaCommentProperty(Property):
2234    arg_types = {"this": True}
2235
2236
2237class SerdeProperties(Property):
2238    arg_types = {"expressions": True}
2239
2240
2241class SetProperty(Property):
2242    arg_types = {"multi": True}
2243
2244
2245class SettingsProperty(Property):
2246    arg_types = {"expressions": True}
2247
2248
2249class SortKeyProperty(Property):
2250    arg_types = {"this": True, "compound": False}
2251
2252
2253class SqlSecurityProperty(Property):
2254    arg_types = {"definer": True}
2255
2256
2257class StabilityProperty(Property):
2258    arg_types = {"this": True}
2259
2260
2261class TemporaryProperty(Property):
2262    arg_types = {}
2263
2264
2265class TransformModelProperty(Property):
2266    arg_types = {"expressions": True}
2267
2268
2269class TransientProperty(Property):
2270    arg_types = {"this": False}
2271
2272
2273class VolatileProperty(Property):
2274    arg_types = {"this": False}
2275
2276
2277class WithDataProperty(Property):
2278    arg_types = {"no": True, "statistics": False}
2279
2280
2281class WithJournalTableProperty(Property):
2282    arg_types = {"this": True}
2283
2284
2285class WithSystemVersioningProperty(Property):
2286    # this -> history table name, expression -> data consistency check
2287    arg_types = {"this": False, "expression": False}
2288
2289
2290class Properties(Expression):
2291    arg_types = {"expressions": True}
2292
2293    NAME_TO_PROPERTY = {
2294        "ALGORITHM": AlgorithmProperty,
2295        "AUTO_INCREMENT": AutoIncrementProperty,
2296        "CHARACTER SET": CharacterSetProperty,
2297        "CLUSTERED_BY": ClusteredByProperty,
2298        "COLLATE": CollateProperty,
2299        "COMMENT": SchemaCommentProperty,
2300        "DEFINER": DefinerProperty,
2301        "DISTKEY": DistKeyProperty,
2302        "DISTSTYLE": DistStyleProperty,
2303        "ENGINE": EngineProperty,
2304        "EXECUTE AS": ExecuteAsProperty,
2305        "FORMAT": FileFormatProperty,
2306        "LANGUAGE": LanguageProperty,
2307        "LOCATION": LocationProperty,
2308        "PARTITIONED_BY": PartitionedByProperty,
2309        "RETURNS": ReturnsProperty,
2310        "ROW_FORMAT": RowFormatProperty,
2311        "SORTKEY": SortKeyProperty,
2312    }
2313
2314    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
2315
2316    # CREATE property locations
2317    # Form: schema specified
2318    #   create [POST_CREATE]
2319    #     table a [POST_NAME]
2320    #     (b int) [POST_SCHEMA]
2321    #     with ([POST_WITH])
2322    #     index (b) [POST_INDEX]
2323    #
2324    # Form: alias selection
2325    #   create [POST_CREATE]
2326    #     table a [POST_NAME]
2327    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
2328    #     index (c) [POST_INDEX]
2329    class Location(AutoName):
2330        POST_CREATE = auto()
2331        POST_NAME = auto()
2332        POST_SCHEMA = auto()
2333        POST_WITH = auto()
2334        POST_ALIAS = auto()
2335        POST_EXPRESSION = auto()
2336        POST_INDEX = auto()
2337        UNSUPPORTED = auto()
2338
2339    @classmethod
2340    def from_dict(cls, properties_dict: t.Dict) -> Properties:
2341        expressions = []
2342        for key, value in properties_dict.items():
2343            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
2344            if property_cls:
2345                expressions.append(property_cls(this=convert(value)))
2346            else:
2347                expressions.append(Property(this=Literal.string(key), value=convert(value)))
2348
2349        return cls(expressions=expressions)
2350
2351
2352class Qualify(Expression):
2353    pass
2354
2355
2356class InputOutputFormat(Expression):
2357    arg_types = {"input_format": False, "output_format": False}
2358
2359
2360# https://www.ibm.com/docs/en/ias?topic=procedures-return-statement-in-sql
2361class Return(Expression):
2362    pass
2363
2364
2365class Reference(Expression):
2366    arg_types = {"this": True, "expressions": False, "options": False}
2367
2368
2369class Tuple(Expression):
2370    arg_types = {"expressions": False}
2371
2372    def isin(
2373        self,
2374        *expressions: t.Any,
2375        query: t.Optional[ExpOrStr] = None,
2376        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
2377        copy: bool = True,
2378        **opts,
2379    ) -> In:
2380        return In(
2381            this=maybe_copy(self, copy),
2382            expressions=[convert(e, copy=copy) for e in expressions],
2383            query=maybe_parse(query, copy=copy, **opts) if query else None,
2384            unnest=Unnest(
2385                expressions=[
2386                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
2387                ]
2388            )
2389            if unnest
2390            else None,
2391        )
2392
2393
2394class Subqueryable(Unionable):
2395    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
2396        """
2397        Convert this expression to an aliased expression that can be used as a Subquery.
2398
2399        Example:
2400            >>> subquery = Select().select("x").from_("tbl").subquery()
2401            >>> Select().select("x").from_(subquery).sql()
2402            'SELECT x FROM (SELECT x FROM tbl)'
2403
2404        Args:
2405            alias (str | Identifier): an optional alias for the subquery
2406            copy (bool): if `False`, modify this expression instance in-place.
2407
2408        Returns:
2409            Alias: the subquery
2410        """
2411        instance = maybe_copy(self, copy)
2412        if not isinstance(alias, Expression):
2413            alias = TableAlias(this=to_identifier(alias)) if alias else None
2414
2415        return Subquery(this=instance, alias=alias)
2416
2417    def limit(
2418        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2419    ) -> Select:
2420        raise NotImplementedError
2421
2422    @property
2423    def ctes(self):
2424        with_ = self.args.get("with")
2425        if not with_:
2426            return []
2427        return with_.expressions
2428
2429    @property
2430    def selects(self) -> t.List[Expression]:
2431        raise NotImplementedError("Subqueryable objects must implement `selects`")
2432
2433    @property
2434    def named_selects(self) -> t.List[str]:
2435        raise NotImplementedError("Subqueryable objects must implement `named_selects`")
2436
2437    def select(
2438        self,
2439        *expressions: t.Optional[ExpOrStr],
2440        append: bool = True,
2441        dialect: DialectType = None,
2442        copy: bool = True,
2443        **opts,
2444    ) -> Subqueryable:
2445        raise NotImplementedError("Subqueryable objects must implement `select`")
2446
2447    def with_(
2448        self,
2449        alias: ExpOrStr,
2450        as_: ExpOrStr,
2451        recursive: t.Optional[bool] = None,
2452        append: bool = True,
2453        dialect: DialectType = None,
2454        copy: bool = True,
2455        **opts,
2456    ) -> Subqueryable:
2457        """
2458        Append to or set the common table expressions.
2459
2460        Example:
2461            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
2462            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
2463
2464        Args:
2465            alias: the SQL code string to parse as the table name.
2466                If an `Expression` instance is passed, this is used as-is.
2467            as_: the SQL code string to parse as the table expression.
2468                If an `Expression` instance is passed, it will be used as-is.
2469            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2470            append: if `True`, add to any existing expressions.
2471                Otherwise, this resets the expressions.
2472            dialect: the dialect used to parse the input expression.
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 expression.
2478        """
2479        return _apply_cte_builder(
2480            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
2481        )
2482
2483
2484QUERY_MODIFIERS = {
2485    "match": False,
2486    "laterals": False,
2487    "joins": False,
2488    "connect": False,
2489    "pivots": False,
2490    "where": False,
2491    "group": False,
2492    "having": False,
2493    "qualify": False,
2494    "windows": False,
2495    "distribute": False,
2496    "sort": False,
2497    "cluster": False,
2498    "order": False,
2499    "limit": False,
2500    "offset": False,
2501    "locks": False,
2502    "sample": False,
2503    "settings": False,
2504    "format": False,
2505}
2506
2507
2508# https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-ver16
2509class WithTableHint(Expression):
2510    arg_types = {"expressions": True}
2511
2512
2513# https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
2514class IndexTableHint(Expression):
2515    arg_types = {"this": True, "expressions": False, "target": False}
2516
2517
2518# https://docs.snowflake.com/en/sql-reference/constructs/at-before
2519class HistoricalData(Expression):
2520    arg_types = {"this": True, "kind": True, "expression": True}
2521
2522
2523class Table(Expression):
2524    arg_types = {
2525        "this": True,
2526        "alias": False,
2527        "db": False,
2528        "catalog": False,
2529        "laterals": False,
2530        "joins": False,
2531        "pivots": False,
2532        "hints": False,
2533        "system_time": False,
2534        "version": False,
2535        "format": False,
2536        "pattern": False,
2537        "index": False,
2538        "ordinality": False,
2539        "when": False,
2540    }
2541
2542    @property
2543    def name(self) -> str:
2544        if isinstance(self.this, Func):
2545            return ""
2546        return self.this.name
2547
2548    @property
2549    def db(self) -> str:
2550        return self.text("db")
2551
2552    @property
2553    def catalog(self) -> str:
2554        return self.text("catalog")
2555
2556    @property
2557    def selects(self) -> t.List[Expression]:
2558        return []
2559
2560    @property
2561    def named_selects(self) -> t.List[str]:
2562        return []
2563
2564    @property
2565    def parts(self) -> t.List[Expression]:
2566        """Return the parts of a table in order catalog, db, table."""
2567        parts: t.List[Expression] = []
2568
2569        for arg in ("catalog", "db", "this"):
2570            part = self.args.get(arg)
2571
2572            if isinstance(part, Dot):
2573                parts.extend(part.flatten())
2574            elif isinstance(part, Expression):
2575                parts.append(part)
2576
2577        return parts
2578
2579
2580class Union(Subqueryable):
2581    arg_types = {
2582        "with": False,
2583        "this": True,
2584        "expression": True,
2585        "distinct": False,
2586        "by_name": False,
2587        **QUERY_MODIFIERS,
2588    }
2589
2590    def limit(
2591        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2592    ) -> Select:
2593        """
2594        Set the LIMIT expression.
2595
2596        Example:
2597            >>> select("1").union(select("1")).limit(1).sql()
2598            'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1'
2599
2600        Args:
2601            expression: the SQL code string to parse.
2602                This can also be an integer.
2603                If a `Limit` instance is passed, this is used as-is.
2604                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2605            dialect: the dialect used to parse the input expression.
2606            copy: if `False`, modify this expression instance in-place.
2607            opts: other options to use to parse the input expressions.
2608
2609        Returns:
2610            The limited subqueryable.
2611        """
2612        return (
2613            select("*")
2614            .from_(self.subquery(alias="_l_0", copy=copy))
2615            .limit(expression, dialect=dialect, copy=False, **opts)
2616        )
2617
2618    def select(
2619        self,
2620        *expressions: t.Optional[ExpOrStr],
2621        append: bool = True,
2622        dialect: DialectType = None,
2623        copy: bool = True,
2624        **opts,
2625    ) -> Union:
2626        """Append to or set the SELECT of the union recursively.
2627
2628        Example:
2629            >>> from sqlglot import parse_one
2630            >>> parse_one("select a from x union select a from y union select a from z").select("b").sql()
2631            'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z'
2632
2633        Args:
2634            *expressions: the SQL code strings to parse.
2635                If an `Expression` instance is passed, it will be used as-is.
2636            append: if `True`, add to any existing expressions.
2637                Otherwise, this resets the expressions.
2638            dialect: the dialect used to parse the input expressions.
2639            copy: if `False`, modify this expression instance in-place.
2640            opts: other options to use to parse the input expressions.
2641
2642        Returns:
2643            Union: the modified expression.
2644        """
2645        this = self.copy() if copy else self
2646        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
2647        this.expression.unnest().select(
2648            *expressions, append=append, dialect=dialect, copy=False, **opts
2649        )
2650        return this
2651
2652    @property
2653    def named_selects(self) -> t.List[str]:
2654        return self.this.unnest().named_selects
2655
2656    @property
2657    def is_star(self) -> bool:
2658        return self.this.is_star or self.expression.is_star
2659
2660    @property
2661    def selects(self) -> t.List[Expression]:
2662        return self.this.unnest().selects
2663
2664    @property
2665    def left(self) -> Expression:
2666        return self.this
2667
2668    @property
2669    def right(self) -> Expression:
2670        return self.expression
2671
2672
2673class Except(Union):
2674    pass
2675
2676
2677class Intersect(Union):
2678    pass
2679
2680
2681class Unnest(UDTF):
2682    arg_types = {
2683        "expressions": True,
2684        "alias": False,
2685        "offset": False,
2686    }
2687
2688
2689class Update(Expression):
2690    arg_types = {
2691        "with": False,
2692        "this": False,
2693        "expressions": True,
2694        "from": False,
2695        "where": False,
2696        "returning": False,
2697        "order": False,
2698        "limit": False,
2699    }
2700
2701
2702class Values(UDTF):
2703    arg_types = {"expressions": True, "alias": False}
2704
2705
2706class Var(Expression):
2707    pass
2708
2709
2710class Version(Expression):
2711    """
2712    Time travel, iceberg, bigquery etc
2713    https://trino.io/docs/current/connector/iceberg.html?highlight=snapshot#using-snapshots
2714    https://www.databricks.com/blog/2019/02/04/introducing-delta-time-travel-for-large-scale-data-lakes.html
2715    https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#for_system_time_as_of
2716    https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-ver16
2717    this is either TIMESTAMP or VERSION
2718    kind is ("AS OF", "BETWEEN")
2719    """
2720
2721    arg_types = {"this": True, "kind": True, "expression": False}
2722
2723
2724class Schema(Expression):
2725    arg_types = {"this": False, "expressions": False}
2726
2727
2728# https://dev.mysql.com/doc/refman/8.0/en/select.html
2729# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html
2730class Lock(Expression):
2731    arg_types = {"update": True, "expressions": False, "wait": False}
2732
2733
2734class Select(Subqueryable):
2735    arg_types = {
2736        "with": False,
2737        "kind": False,
2738        "expressions": False,
2739        "hint": False,
2740        "distinct": False,
2741        "into": False,
2742        "from": False,
2743        **QUERY_MODIFIERS,
2744    }
2745
2746    def from_(
2747        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
2748    ) -> Select:
2749        """
2750        Set the FROM expression.
2751
2752        Example:
2753            >>> Select().from_("tbl").select("x").sql()
2754            'SELECT x FROM tbl'
2755
2756        Args:
2757            expression : the SQL code strings to parse.
2758                If a `From` instance is passed, this is used as-is.
2759                If another `Expression` instance is passed, it will be wrapped in a `From`.
2760            dialect: the dialect used to parse the input expression.
2761            copy: if `False`, modify this expression instance in-place.
2762            opts: other options to use to parse the input expressions.
2763
2764        Returns:
2765            The modified Select expression.
2766        """
2767        return _apply_builder(
2768            expression=expression,
2769            instance=self,
2770            arg="from",
2771            into=From,
2772            prefix="FROM",
2773            dialect=dialect,
2774            copy=copy,
2775            **opts,
2776        )
2777
2778    def group_by(
2779        self,
2780        *expressions: t.Optional[ExpOrStr],
2781        append: bool = True,
2782        dialect: DialectType = None,
2783        copy: bool = True,
2784        **opts,
2785    ) -> Select:
2786        """
2787        Set the GROUP BY expression.
2788
2789        Example:
2790            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
2791            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
2792
2793        Args:
2794            *expressions: the SQL code strings to parse.
2795                If a `Group` instance is passed, this is used as-is.
2796                If another `Expression` instance is passed, it will be wrapped in a `Group`.
2797                If nothing is passed in then a group by is not applied to the expression
2798            append: if `True`, add to any existing expressions.
2799                Otherwise, this flattens all the `Group` expression into a single expression.
2800            dialect: the dialect used to parse the input expression.
2801            copy: if `False`, modify this expression instance in-place.
2802            opts: other options to use to parse the input expressions.
2803
2804        Returns:
2805            The modified Select expression.
2806        """
2807        if not expressions:
2808            return self if not copy else self.copy()
2809
2810        return _apply_child_list_builder(
2811            *expressions,
2812            instance=self,
2813            arg="group",
2814            append=append,
2815            copy=copy,
2816            prefix="GROUP BY",
2817            into=Group,
2818            dialect=dialect,
2819            **opts,
2820        )
2821
2822    def order_by(
2823        self,
2824        *expressions: t.Optional[ExpOrStr],
2825        append: bool = True,
2826        dialect: DialectType = None,
2827        copy: bool = True,
2828        **opts,
2829    ) -> Select:
2830        """
2831        Set the ORDER BY expression.
2832
2833        Example:
2834            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
2835            'SELECT x FROM tbl ORDER BY x DESC'
2836
2837        Args:
2838            *expressions: the SQL code strings to parse.
2839                If a `Group` instance is passed, this is used as-is.
2840                If another `Expression` instance is passed, it will be wrapped in a `Order`.
2841            append: if `True`, add to any existing expressions.
2842                Otherwise, this flattens all the `Order` expression into a single expression.
2843            dialect: the dialect used to parse the input expression.
2844            copy: if `False`, modify this expression instance in-place.
2845            opts: other options to use to parse the input expressions.
2846
2847        Returns:
2848            The modified Select expression.
2849        """
2850        return _apply_child_list_builder(
2851            *expressions,
2852            instance=self,
2853            arg="order",
2854            append=append,
2855            copy=copy,
2856            prefix="ORDER BY",
2857            into=Order,
2858            dialect=dialect,
2859            **opts,
2860        )
2861
2862    def sort_by(
2863        self,
2864        *expressions: t.Optional[ExpOrStr],
2865        append: bool = True,
2866        dialect: DialectType = None,
2867        copy: bool = True,
2868        **opts,
2869    ) -> Select:
2870        """
2871        Set the SORT BY expression.
2872
2873        Example:
2874            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
2875            'SELECT x FROM tbl SORT BY x DESC'
2876
2877        Args:
2878            *expressions: the SQL code strings to parse.
2879                If a `Group` instance is passed, this is used as-is.
2880                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
2881            append: if `True`, add to any existing expressions.
2882                Otherwise, this flattens all the `Order` expression into a single expression.
2883            dialect: the dialect used to parse the input expression.
2884            copy: if `False`, modify this expression instance in-place.
2885            opts: other options to use to parse the input expressions.
2886
2887        Returns:
2888            The modified Select expression.
2889        """
2890        return _apply_child_list_builder(
2891            *expressions,
2892            instance=self,
2893            arg="sort",
2894            append=append,
2895            copy=copy,
2896            prefix="SORT BY",
2897            into=Sort,
2898            dialect=dialect,
2899            **opts,
2900        )
2901
2902    def cluster_by(
2903        self,
2904        *expressions: t.Optional[ExpOrStr],
2905        append: bool = True,
2906        dialect: DialectType = None,
2907        copy: bool = True,
2908        **opts,
2909    ) -> Select:
2910        """
2911        Set the CLUSTER BY expression.
2912
2913        Example:
2914            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
2915            'SELECT x FROM tbl CLUSTER BY x DESC'
2916
2917        Args:
2918            *expressions: the SQL code strings to parse.
2919                If a `Group` instance is passed, this is used as-is.
2920                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
2921            append: if `True`, add to any existing expressions.
2922                Otherwise, this flattens all the `Order` expression into a single expression.
2923            dialect: the dialect used to parse the input expression.
2924            copy: if `False`, modify this expression instance in-place.
2925            opts: other options to use to parse the input expressions.
2926
2927        Returns:
2928            The modified Select expression.
2929        """
2930        return _apply_child_list_builder(
2931            *expressions,
2932            instance=self,
2933            arg="cluster",
2934            append=append,
2935            copy=copy,
2936            prefix="CLUSTER BY",
2937            into=Cluster,
2938            dialect=dialect,
2939            **opts,
2940        )
2941
2942    def limit(
2943        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2944    ) -> Select:
2945        """
2946        Set the LIMIT expression.
2947
2948        Example:
2949            >>> Select().from_("tbl").select("x").limit(10).sql()
2950            'SELECT x FROM tbl LIMIT 10'
2951
2952        Args:
2953            expression: the SQL code string to parse.
2954                This can also be an integer.
2955                If a `Limit` instance is passed, this is used as-is.
2956                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2957            dialect: the dialect used to parse the input expression.
2958            copy: if `False`, modify this expression instance in-place.
2959            opts: other options to use to parse the input expressions.
2960
2961        Returns:
2962            Select: the modified expression.
2963        """
2964        return _apply_builder(
2965            expression=expression,
2966            instance=self,
2967            arg="limit",
2968            into=Limit,
2969            prefix="LIMIT",
2970            dialect=dialect,
2971            copy=copy,
2972            into_arg="expression",
2973            **opts,
2974        )
2975
2976    def offset(
2977        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2978    ) -> Select:
2979        """
2980        Set the OFFSET expression.
2981
2982        Example:
2983            >>> Select().from_("tbl").select("x").offset(10).sql()
2984            'SELECT x FROM tbl OFFSET 10'
2985
2986        Args:
2987            expression: the SQL code string to parse.
2988                This can also be an integer.
2989                If a `Offset` instance is passed, this is used as-is.
2990                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
2991            dialect: the dialect used to parse the input expression.
2992            copy: if `False`, modify this expression instance in-place.
2993            opts: other options to use to parse the input expressions.
2994
2995        Returns:
2996            The modified Select expression.
2997        """
2998        return _apply_builder(
2999            expression=expression,
3000            instance=self,
3001            arg="offset",
3002            into=Offset,
3003            prefix="OFFSET",
3004            dialect=dialect,
3005            copy=copy,
3006            into_arg="expression",
3007            **opts,
3008        )
3009
3010    def select(
3011        self,
3012        *expressions: t.Optional[ExpOrStr],
3013        append: bool = True,
3014        dialect: DialectType = None,
3015        copy: bool = True,
3016        **opts,
3017    ) -> Select:
3018        """
3019        Append to or set the SELECT expressions.
3020
3021        Example:
3022            >>> Select().select("x", "y").sql()
3023            'SELECT x, y'
3024
3025        Args:
3026            *expressions: the SQL code strings to parse.
3027                If an `Expression` instance is passed, it will be used as-is.
3028            append: if `True`, add to any existing expressions.
3029                Otherwise, this resets the expressions.
3030            dialect: the dialect used to parse the input expressions.
3031            copy: if `False`, modify this expression instance in-place.
3032            opts: other options to use to parse the input expressions.
3033
3034        Returns:
3035            The modified Select expression.
3036        """
3037        return _apply_list_builder(
3038            *expressions,
3039            instance=self,
3040            arg="expressions",
3041            append=append,
3042            dialect=dialect,
3043            copy=copy,
3044            **opts,
3045        )
3046
3047    def lateral(
3048        self,
3049        *expressions: t.Optional[ExpOrStr],
3050        append: bool = True,
3051        dialect: DialectType = None,
3052        copy: bool = True,
3053        **opts,
3054    ) -> Select:
3055        """
3056        Append to or set the LATERAL expressions.
3057
3058        Example:
3059            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3060            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3061
3062        Args:
3063            *expressions: the SQL code strings to parse.
3064                If an `Expression` instance is passed, it will be used as-is.
3065            append: if `True`, add to any existing expressions.
3066                Otherwise, this resets the expressions.
3067            dialect: the dialect used to parse the input expressions.
3068            copy: if `False`, modify this expression instance in-place.
3069            opts: other options to use to parse the input expressions.
3070
3071        Returns:
3072            The modified Select expression.
3073        """
3074        return _apply_list_builder(
3075            *expressions,
3076            instance=self,
3077            arg="laterals",
3078            append=append,
3079            into=Lateral,
3080            prefix="LATERAL VIEW",
3081            dialect=dialect,
3082            copy=copy,
3083            **opts,
3084        )
3085
3086    def join(
3087        self,
3088        expression: ExpOrStr,
3089        on: t.Optional[ExpOrStr] = None,
3090        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3091        append: bool = True,
3092        join_type: t.Optional[str] = None,
3093        join_alias: t.Optional[Identifier | str] = None,
3094        dialect: DialectType = None,
3095        copy: bool = True,
3096        **opts,
3097    ) -> Select:
3098        """
3099        Append to or set the JOIN expressions.
3100
3101        Example:
3102            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
3103            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
3104
3105            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
3106            'SELECT 1 FROM a JOIN b USING (x, y, z)'
3107
3108            Use `join_type` to change the type of join:
3109
3110            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
3111            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
3112
3113        Args:
3114            expression: the SQL code string to parse.
3115                If an `Expression` instance is passed, it will be used as-is.
3116            on: optionally specify the join "on" criteria as a SQL string.
3117                If an `Expression` instance is passed, it will be used as-is.
3118            using: optionally specify the join "using" criteria as a SQL string.
3119                If an `Expression` instance is passed, it will be used as-is.
3120            append: if `True`, add to any existing expressions.
3121                Otherwise, this resets the expressions.
3122            join_type: if set, alter the parsed join type.
3123            join_alias: an optional alias for the joined source.
3124            dialect: the dialect used to parse the input expressions.
3125            copy: if `False`, modify this expression instance in-place.
3126            opts: other options to use to parse the input expressions.
3127
3128        Returns:
3129            Select: the modified expression.
3130        """
3131        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
3132
3133        try:
3134            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
3135        except ParseError:
3136            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
3137
3138        join = expression if isinstance(expression, Join) else Join(this=expression)
3139
3140        if isinstance(join.this, Select):
3141            join.this.replace(join.this.subquery())
3142
3143        if join_type:
3144            method: t.Optional[Token]
3145            side: t.Optional[Token]
3146            kind: t.Optional[Token]
3147
3148            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
3149
3150            if method:
3151                join.set("method", method.text)
3152            if side:
3153                join.set("side", side.text)
3154            if kind:
3155                join.set("kind", kind.text)
3156
3157        if on:
3158            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
3159            join.set("on", on)
3160
3161        if using:
3162            join = _apply_list_builder(
3163                *ensure_list(using),
3164                instance=join,
3165                arg="using",
3166                append=append,
3167                copy=copy,
3168                into=Identifier,
3169                **opts,
3170            )
3171
3172        if join_alias:
3173            join.set("this", alias_(join.this, join_alias, table=True))
3174
3175        return _apply_list_builder(
3176            join,
3177            instance=self,
3178            arg="joins",
3179            append=append,
3180            copy=copy,
3181            **opts,
3182        )
3183
3184    def where(
3185        self,
3186        *expressions: t.Optional[ExpOrStr],
3187        append: bool = True,
3188        dialect: DialectType = None,
3189        copy: bool = True,
3190        **opts,
3191    ) -> Select:
3192        """
3193        Append to or set the WHERE expressions.
3194
3195        Example:
3196            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
3197            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
3198
3199        Args:
3200            *expressions: the SQL code strings to parse.
3201                If an `Expression` instance is passed, it will be used as-is.
3202                Multiple expressions are combined with an AND operator.
3203            append: if `True`, AND the new expressions to any existing expression.
3204                Otherwise, this resets the expression.
3205            dialect: the dialect used to parse the input expressions.
3206            copy: if `False`, modify this expression instance in-place.
3207            opts: other options to use to parse the input expressions.
3208
3209        Returns:
3210            Select: the modified expression.
3211        """
3212        return _apply_conjunction_builder(
3213            *expressions,
3214            instance=self,
3215            arg="where",
3216            append=append,
3217            into=Where,
3218            dialect=dialect,
3219            copy=copy,
3220            **opts,
3221        )
3222
3223    def having(
3224        self,
3225        *expressions: t.Optional[ExpOrStr],
3226        append: bool = True,
3227        dialect: DialectType = None,
3228        copy: bool = True,
3229        **opts,
3230    ) -> Select:
3231        """
3232        Append to or set the HAVING expressions.
3233
3234        Example:
3235            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
3236            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
3237
3238        Args:
3239            *expressions: the SQL code strings to parse.
3240                If an `Expression` instance is passed, it will be used as-is.
3241                Multiple expressions are combined with an AND operator.
3242            append: if `True`, AND the new expressions to any existing expression.
3243                Otherwise, this resets the expression.
3244            dialect: the dialect used to parse the input expressions.
3245            copy: if `False`, modify this expression instance in-place.
3246            opts: other options to use to parse the input expressions.
3247
3248        Returns:
3249            The modified Select expression.
3250        """
3251        return _apply_conjunction_builder(
3252            *expressions,
3253            instance=self,
3254            arg="having",
3255            append=append,
3256            into=Having,
3257            dialect=dialect,
3258            copy=copy,
3259            **opts,
3260        )
3261
3262    def window(
3263        self,
3264        *expressions: t.Optional[ExpOrStr],
3265        append: bool = True,
3266        dialect: DialectType = None,
3267        copy: bool = True,
3268        **opts,
3269    ) -> Select:
3270        return _apply_list_builder(
3271            *expressions,
3272            instance=self,
3273            arg="windows",
3274            append=append,
3275            into=Window,
3276            dialect=dialect,
3277            copy=copy,
3278            **opts,
3279        )
3280
3281    def qualify(
3282        self,
3283        *expressions: t.Optional[ExpOrStr],
3284        append: bool = True,
3285        dialect: DialectType = None,
3286        copy: bool = True,
3287        **opts,
3288    ) -> Select:
3289        return _apply_conjunction_builder(
3290            *expressions,
3291            instance=self,
3292            arg="qualify",
3293            append=append,
3294            into=Qualify,
3295            dialect=dialect,
3296            copy=copy,
3297            **opts,
3298        )
3299
3300    def distinct(
3301        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
3302    ) -> Select:
3303        """
3304        Set the OFFSET expression.
3305
3306        Example:
3307            >>> Select().from_("tbl").select("x").distinct().sql()
3308            'SELECT DISTINCT x FROM tbl'
3309
3310        Args:
3311            ons: the expressions to distinct on
3312            distinct: whether the Select should be distinct
3313            copy: if `False`, modify this expression instance in-place.
3314
3315        Returns:
3316            Select: the modified expression.
3317        """
3318        instance = maybe_copy(self, copy)
3319        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
3320        instance.set("distinct", Distinct(on=on) if distinct else None)
3321        return instance
3322
3323    def ctas(
3324        self,
3325        table: ExpOrStr,
3326        properties: t.Optional[t.Dict] = None,
3327        dialect: DialectType = None,
3328        copy: bool = True,
3329        **opts,
3330    ) -> Create:
3331        """
3332        Convert this expression to a CREATE TABLE AS statement.
3333
3334        Example:
3335            >>> Select().select("*").from_("tbl").ctas("x").sql()
3336            'CREATE TABLE x AS SELECT * FROM tbl'
3337
3338        Args:
3339            table: the SQL code string to parse as the table name.
3340                If another `Expression` instance is passed, it will be used as-is.
3341            properties: an optional mapping of table properties
3342            dialect: the dialect used to parse the input table.
3343            copy: if `False`, modify this expression instance in-place.
3344            opts: other options to use to parse the input table.
3345
3346        Returns:
3347            The new Create expression.
3348        """
3349        instance = maybe_copy(self, copy)
3350        table_expression = maybe_parse(
3351            table,
3352            into=Table,
3353            dialect=dialect,
3354            **opts,
3355        )
3356        properties_expression = None
3357        if properties:
3358            properties_expression = Properties.from_dict(properties)
3359
3360        return Create(
3361            this=table_expression,
3362            kind="table",
3363            expression=instance,
3364            properties=properties_expression,
3365        )
3366
3367    def lock(self, update: bool = True, copy: bool = True) -> Select:
3368        """
3369        Set the locking read mode for this expression.
3370
3371        Examples:
3372            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
3373            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
3374
3375            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
3376            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
3377
3378        Args:
3379            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
3380            copy: if `False`, modify this expression instance in-place.
3381
3382        Returns:
3383            The modified expression.
3384        """
3385        inst = maybe_copy(self, copy)
3386        inst.set("locks", [Lock(update=update)])
3387
3388        return inst
3389
3390    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
3391        """
3392        Set hints for this expression.
3393
3394        Examples:
3395            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
3396            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
3397
3398        Args:
3399            hints: The SQL code strings to parse as the hints.
3400                If an `Expression` instance is passed, it will be used as-is.
3401            dialect: The dialect used to parse the hints.
3402            copy: If `False`, modify this expression instance in-place.
3403
3404        Returns:
3405            The modified expression.
3406        """
3407        inst = maybe_copy(self, copy)
3408        inst.set(
3409            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
3410        )
3411
3412        return inst
3413
3414    @property
3415    def named_selects(self) -> t.List[str]:
3416        return [e.output_name for e in self.expressions if e.alias_or_name]
3417
3418    @property
3419    def is_star(self) -> bool:
3420        return any(expression.is_star for expression in self.expressions)
3421
3422    @property
3423    def selects(self) -> t.List[Expression]:
3424        return self.expressions
3425
3426
3427class Subquery(DerivedTable, Unionable):
3428    arg_types = {
3429        "this": True,
3430        "alias": False,
3431        "with": False,
3432        **QUERY_MODIFIERS,
3433    }
3434
3435    def unnest(self):
3436        """
3437        Returns the first non subquery.
3438        """
3439        expression = self
3440        while isinstance(expression, Subquery):
3441            expression = expression.this
3442        return expression
3443
3444    def unwrap(self) -> Subquery:
3445        expression = self
3446        while expression.same_parent and expression.is_wrapper:
3447            expression = t.cast(Subquery, expression.parent)
3448        return expression
3449
3450    @property
3451    def is_wrapper(self) -> bool:
3452        """
3453        Whether this Subquery acts as a simple wrapper around another expression.
3454
3455        SELECT * FROM (((SELECT * FROM t)))
3456                      ^
3457                      This corresponds to a "wrapper" Subquery node
3458        """
3459        return all(v is None for k, v in self.args.items() if k != "this")
3460
3461    @property
3462    def is_star(self) -> bool:
3463        return self.this.is_star
3464
3465    @property
3466    def output_name(self) -> str:
3467        return self.alias
3468
3469
3470class TableSample(Expression):
3471    arg_types = {
3472        "this": False,
3473        "expressions": False,
3474        "method": False,
3475        "bucket_numerator": False,
3476        "bucket_denominator": False,
3477        "bucket_field": False,
3478        "percent": False,
3479        "rows": False,
3480        "size": False,
3481        "seed": False,
3482        "kind": False,
3483    }
3484
3485
3486class Tag(Expression):
3487    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
3488
3489    arg_types = {
3490        "this": False,
3491        "prefix": False,
3492        "postfix": False,
3493    }
3494
3495
3496# Represents both the standard SQL PIVOT operator and DuckDB's "simplified" PIVOT syntax
3497# https://duckdb.org/docs/sql/statements/pivot
3498class Pivot(Expression):
3499    arg_types = {
3500        "this": False,
3501        "alias": False,
3502        "expressions": False,
3503        "field": False,
3504        "unpivot": False,
3505        "using": False,
3506        "group": False,
3507        "columns": False,
3508        "include_nulls": False,
3509    }
3510
3511
3512class Window(Condition):
3513    arg_types = {
3514        "this": True,
3515        "partition_by": False,
3516        "order": False,
3517        "spec": False,
3518        "alias": False,
3519        "over": False,
3520        "first": False,
3521    }
3522
3523
3524class WindowSpec(Expression):
3525    arg_types = {
3526        "kind": False,
3527        "start": False,
3528        "start_side": False,
3529        "end": False,
3530        "end_side": False,
3531    }
3532
3533
3534class Where(Expression):
3535    pass
3536
3537
3538class Star(Expression):
3539    arg_types = {"except": False, "replace": False}
3540
3541    @property
3542    def name(self) -> str:
3543        return "*"
3544
3545    @property
3546    def output_name(self) -> str:
3547        return self.name
3548
3549
3550class Parameter(Condition):
3551    arg_types = {"this": True, "expression": False}
3552
3553
3554class SessionParameter(Condition):
3555    arg_types = {"this": True, "kind": False}
3556
3557
3558class Placeholder(Condition):
3559    arg_types = {"this": False, "kind": False}
3560
3561
3562class Null(Condition):
3563    arg_types: t.Dict[str, t.Any] = {}
3564
3565    @property
3566    def name(self) -> str:
3567        return "NULL"
3568
3569
3570class Boolean(Condition):
3571    pass
3572
3573
3574class DataTypeParam(Expression):
3575    arg_types = {"this": True, "expression": False}
3576
3577
3578class DataType(Expression):
3579    arg_types = {
3580        "this": True,
3581        "expressions": False,
3582        "nested": False,
3583        "values": False,
3584        "prefix": False,
3585        "kind": False,
3586    }
3587
3588    class Type(AutoName):
3589        ARRAY = auto()
3590        BIGDECIMAL = auto()
3591        BIGINT = auto()
3592        BIGSERIAL = auto()
3593        BINARY = auto()
3594        BIT = auto()
3595        BOOLEAN = auto()
3596        CHAR = auto()
3597        DATE = auto()
3598        DATEMULTIRANGE = auto()
3599        DATERANGE = auto()
3600        DATETIME = auto()
3601        DATETIME64 = auto()
3602        DECIMAL = auto()
3603        DOUBLE = auto()
3604        ENUM = auto()
3605        ENUM8 = auto()
3606        ENUM16 = auto()
3607        FIXEDSTRING = auto()
3608        FLOAT = auto()
3609        GEOGRAPHY = auto()
3610        GEOMETRY = auto()
3611        HLLSKETCH = auto()
3612        HSTORE = auto()
3613        IMAGE = auto()
3614        INET = auto()
3615        INT = auto()
3616        INT128 = auto()
3617        INT256 = auto()
3618        INT4MULTIRANGE = auto()
3619        INT4RANGE = auto()
3620        INT8MULTIRANGE = auto()
3621        INT8RANGE = auto()
3622        INTERVAL = auto()
3623        IPADDRESS = auto()
3624        IPPREFIX = auto()
3625        JSON = auto()
3626        JSONB = auto()
3627        LONGBLOB = auto()
3628        LONGTEXT = auto()
3629        LOWCARDINALITY = auto()
3630        MAP = auto()
3631        MEDIUMBLOB = auto()
3632        MEDIUMINT = auto()
3633        MEDIUMTEXT = auto()
3634        MONEY = auto()
3635        NCHAR = auto()
3636        NESTED = auto()
3637        NULL = auto()
3638        NULLABLE = auto()
3639        NUMMULTIRANGE = auto()
3640        NUMRANGE = auto()
3641        NVARCHAR = auto()
3642        OBJECT = auto()
3643        ROWVERSION = auto()
3644        SERIAL = auto()
3645        SET = auto()
3646        SMALLINT = auto()
3647        SMALLMONEY = auto()
3648        SMALLSERIAL = auto()
3649        STRUCT = auto()
3650        SUPER = auto()
3651        TEXT = auto()
3652        TINYBLOB = auto()
3653        TINYTEXT = auto()
3654        TIME = auto()
3655        TIMETZ = auto()
3656        TIMESTAMP = auto()
3657        TIMESTAMPLTZ = auto()
3658        TIMESTAMPTZ = auto()
3659        TIMESTAMP_S = auto()
3660        TIMESTAMP_MS = auto()
3661        TIMESTAMP_NS = auto()
3662        TINYINT = auto()
3663        TSMULTIRANGE = auto()
3664        TSRANGE = auto()
3665        TSTZMULTIRANGE = auto()
3666        TSTZRANGE = auto()
3667        UBIGINT = auto()
3668        UINT = auto()
3669        UINT128 = auto()
3670        UINT256 = auto()
3671        UMEDIUMINT = auto()
3672        UDECIMAL = auto()
3673        UNIQUEIDENTIFIER = auto()
3674        UNKNOWN = auto()  # Sentinel value, useful for type annotation
3675        USERDEFINED = "USER-DEFINED"
3676        USMALLINT = auto()
3677        UTINYINT = auto()
3678        UUID = auto()
3679        VARBINARY = auto()
3680        VARCHAR = auto()
3681        VARIANT = auto()
3682        XML = auto()
3683        YEAR = auto()
3684
3685    TEXT_TYPES = {
3686        Type.CHAR,
3687        Type.NCHAR,
3688        Type.VARCHAR,
3689        Type.NVARCHAR,
3690        Type.TEXT,
3691    }
3692
3693    INTEGER_TYPES = {
3694        Type.INT,
3695        Type.TINYINT,
3696        Type.SMALLINT,
3697        Type.BIGINT,
3698        Type.INT128,
3699        Type.INT256,
3700        Type.BIT,
3701    }
3702
3703    FLOAT_TYPES = {
3704        Type.FLOAT,
3705        Type.DOUBLE,
3706    }
3707
3708    NUMERIC_TYPES = {
3709        *INTEGER_TYPES,
3710        *FLOAT_TYPES,
3711    }
3712
3713    TEMPORAL_TYPES = {
3714        Type.TIME,
3715        Type.TIMETZ,
3716        Type.TIMESTAMP,
3717        Type.TIMESTAMPTZ,
3718        Type.TIMESTAMPLTZ,
3719        Type.TIMESTAMP_S,
3720        Type.TIMESTAMP_MS,
3721        Type.TIMESTAMP_NS,
3722        Type.DATE,
3723        Type.DATETIME,
3724        Type.DATETIME64,
3725    }
3726
3727    @classmethod
3728    def build(
3729        cls,
3730        dtype: DATA_TYPE,
3731        dialect: DialectType = None,
3732        udt: bool = False,
3733        **kwargs,
3734    ) -> DataType:
3735        """
3736        Constructs a DataType object.
3737
3738        Args:
3739            dtype: the data type of interest.
3740            dialect: the dialect to use for parsing `dtype`, in case it's a string.
3741            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
3742                DataType, thus creating a user-defined type.
3743            kawrgs: additional arguments to pass in the constructor of DataType.
3744
3745        Returns:
3746            The constructed DataType object.
3747        """
3748        from sqlglot import parse_one
3749
3750        if isinstance(dtype, str):
3751            if dtype.upper() == "UNKNOWN":
3752                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
3753
3754            try:
3755                data_type_exp = parse_one(
3756                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
3757                )
3758            except ParseError:
3759                if udt:
3760                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
3761                raise
3762        elif isinstance(dtype, DataType.Type):
3763            data_type_exp = DataType(this=dtype)
3764        elif isinstance(dtype, DataType):
3765            return dtype
3766        else:
3767            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
3768
3769        return DataType(**{**data_type_exp.args, **kwargs})
3770
3771    def is_type(self, *dtypes: DATA_TYPE) -> bool:
3772        """
3773        Checks whether this DataType matches one of the provided data types. Nested types or precision
3774        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
3775
3776        Args:
3777            dtypes: the data types to compare this DataType to.
3778
3779        Returns:
3780            True, if and only if there is a type in `dtypes` which is equal to this DataType.
3781        """
3782        for dtype in dtypes:
3783            other = DataType.build(dtype, udt=True)
3784
3785            if (
3786                other.expressions
3787                or self.this == DataType.Type.USERDEFINED
3788                or other.this == DataType.Type.USERDEFINED
3789            ):
3790                matches = self == other
3791            else:
3792                matches = self.this == other.this
3793
3794            if matches:
3795                return True
3796        return False
3797
3798
3799DATA_TYPE = t.Union[str, DataType, DataType.Type]
3800
3801
3802# https://www.postgresql.org/docs/15/datatype-pseudo.html
3803class PseudoType(DataType):
3804    arg_types = {"this": True}
3805
3806
3807# https://www.postgresql.org/docs/15/datatype-oid.html
3808class ObjectIdentifier(DataType):
3809    arg_types = {"this": True}
3810
3811
3812# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...)
3813class SubqueryPredicate(Predicate):
3814    pass
3815
3816
3817class All(SubqueryPredicate):
3818    pass
3819
3820
3821class Any(SubqueryPredicate):
3822    pass
3823
3824
3825class Exists(SubqueryPredicate):
3826    pass
3827
3828
3829# Commands to interact with the databases or engines. For most of the command
3830# expressions we parse whatever comes after the command's name as a string.
3831class Command(Expression):
3832    arg_types = {"this": True, "expression": False}
3833
3834
3835class Transaction(Expression):
3836    arg_types = {"this": False, "modes": False, "mark": False}
3837
3838
3839class Commit(Expression):
3840    arg_types = {"chain": False, "this": False, "durability": False}
3841
3842
3843class Rollback(Expression):
3844    arg_types = {"savepoint": False, "this": False}
3845
3846
3847class AlterTable(Expression):
3848    arg_types = {"this": True, "actions": True, "exists": False, "only": False}
3849
3850
3851class AddConstraint(Expression):
3852    arg_types = {"this": False, "expression": False, "enforced": False}
3853
3854
3855class DropPartition(Expression):
3856    arg_types = {"expressions": True, "exists": False}
3857
3858
3859# Binary expressions like (ADD a b)
3860class Binary(Condition):
3861    arg_types = {"this": True, "expression": True}
3862
3863    @property
3864    def left(self) -> Expression:
3865        return self.this
3866
3867    @property
3868    def right(self) -> Expression:
3869        return self.expression
3870
3871
3872class Add(Binary):
3873    pass
3874
3875
3876class Connector(Binary):
3877    pass
3878
3879
3880class And(Connector):
3881    pass
3882
3883
3884class Or(Connector):
3885    pass
3886
3887
3888class BitwiseAnd(Binary):
3889    pass
3890
3891
3892class BitwiseLeftShift(Binary):
3893    pass
3894
3895
3896class BitwiseOr(Binary):
3897    pass
3898
3899
3900class BitwiseRightShift(Binary):
3901    pass
3902
3903
3904class BitwiseXor(Binary):
3905    pass
3906
3907
3908class Div(Binary):
3909    arg_types = {"this": True, "expression": True, "typed": False, "safe": False}
3910
3911
3912class Overlaps(Binary):
3913    pass
3914
3915
3916class Dot(Binary):
3917    @property
3918    def name(self) -> str:
3919        return self.expression.name
3920
3921    @property
3922    def output_name(self) -> str:
3923        return self.name
3924
3925    @classmethod
3926    def build(self, expressions: t.Sequence[Expression]) -> Dot:
3927        """Build a Dot object with a sequence of expressions."""
3928        if len(expressions) < 2:
3929            raise ValueError(f"Dot requires >= 2 expressions.")
3930
3931        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))
3932
3933    @property
3934    def parts(self) -> t.List[Expression]:
3935        """Return the parts of a table / column in order catalog, db, table."""
3936        this, *parts = self.flatten()
3937
3938        parts.reverse()
3939
3940        for arg in ("this", "table", "db", "catalog"):
3941            part = this.args.get(arg)
3942
3943            if isinstance(part, Expression):
3944                parts.append(part)
3945
3946        parts.reverse()
3947        return parts
3948
3949
3950class DPipe(Binary):
3951    arg_types = {"this": True, "expression": True, "safe": False}
3952
3953
3954class EQ(Binary, Predicate):
3955    pass
3956
3957
3958class NullSafeEQ(Binary, Predicate):
3959    pass
3960
3961
3962class NullSafeNEQ(Binary, Predicate):
3963    pass
3964
3965
3966# Represents e.g. := in DuckDB which is mostly used for setting parameters
3967class PropertyEQ(Binary):
3968    pass
3969
3970
3971class Distance(Binary):
3972    pass
3973
3974
3975class Escape(Binary):
3976    pass
3977
3978
3979class Glob(Binary, Predicate):
3980    pass
3981
3982
3983class GT(Binary, Predicate):
3984    pass
3985
3986
3987class GTE(Binary, Predicate):
3988    pass
3989
3990
3991class ILike(Binary, Predicate):
3992    pass
3993
3994
3995class ILikeAny(Binary, Predicate):
3996    pass
3997
3998
3999class IntDiv(Binary):
4000    pass
4001
4002
4003class Is(Binary, Predicate):
4004    pass
4005
4006
4007class Kwarg(Binary):
4008    """Kwarg in special functions like func(kwarg => y)."""
4009
4010
4011class Like(Binary, Predicate):
4012    pass
4013
4014
4015class LikeAny(Binary, Predicate):
4016    pass
4017
4018
4019class LT(Binary, Predicate):
4020    pass
4021
4022
4023class LTE(Binary, Predicate):
4024    pass
4025
4026
4027class Mod(Binary):
4028    pass
4029
4030
4031class Mul(Binary):
4032    pass
4033
4034
4035class NEQ(Binary, Predicate):
4036    pass
4037
4038
4039# https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH
4040class Operator(Binary):
4041    arg_types = {"this": True, "operator": True, "expression": True}
4042
4043
4044class SimilarTo(Binary, Predicate):
4045    pass
4046
4047
4048class Slice(Binary):
4049    arg_types = {"this": False, "expression": False}
4050
4051
4052class Sub(Binary):
4053    pass
4054
4055
4056class ArrayOverlaps(Binary):
4057    pass
4058
4059
4060# Unary Expressions
4061# (NOT a)
4062class Unary(Condition):
4063    pass
4064
4065
4066class BitwiseNot(Unary):
4067    pass
4068
4069
4070class Not(Unary):
4071    pass
4072
4073
4074class Paren(Unary):
4075    arg_types = {"this": True, "with": False}
4076
4077    @property
4078    def output_name(self) -> str:
4079        return self.this.name
4080
4081
4082class Neg(Unary):
4083    pass
4084
4085
4086class Alias(Expression):
4087    arg_types = {"this": True, "alias": False}
4088
4089    @property
4090    def output_name(self) -> str:
4091        return self.alias
4092
4093
4094class Aliases(Expression):
4095    arg_types = {"this": True, "expressions": True}
4096
4097    @property
4098    def aliases(self):
4099        return self.expressions
4100
4101
4102class AtTimeZone(Expression):
4103    arg_types = {"this": True, "zone": True}
4104
4105
4106class Between(Predicate):
4107    arg_types = {"this": True, "low": True, "high": True}
4108
4109
4110class Bracket(Condition):
4111    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
4112    arg_types = {"this": True, "expressions": True, "offset": False, "safe": False}
4113
4114    @property
4115    def output_name(self) -> str:
4116        if len(self.expressions) == 1:
4117            return self.expressions[0].output_name
4118
4119        return super().output_name
4120
4121
4122class Distinct(Expression):
4123    arg_types = {"expressions": False, "on": False}
4124
4125
4126class In(Predicate):
4127    arg_types = {
4128        "this": True,
4129        "expressions": False,
4130        "query": False,
4131        "unnest": False,
4132        "field": False,
4133        "is_global": False,
4134    }
4135
4136
4137# https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#for-in
4138class ForIn(Expression):
4139    arg_types = {"this": True, "expression": True}
4140
4141
4142class TimeUnit(Expression):
4143    """Automatically converts unit arg into a var."""
4144
4145    arg_types = {"unit": False}
4146
4147    UNABBREVIATED_UNIT_NAME = {
4148        "d": "day",
4149        "h": "hour",
4150        "m": "minute",
4151        "ms": "millisecond",
4152        "ns": "nanosecond",
4153        "q": "quarter",
4154        "s": "second",
4155        "us": "microsecond",
4156        "w": "week",
4157        "y": "year",
4158    }
4159
4160    VAR_LIKE = (Column, Literal, Var)
4161
4162    def __init__(self, **args):
4163        unit = args.get("unit")
4164        if isinstance(unit, self.VAR_LIKE):
4165            args["unit"] = Var(this=self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name)
4166        elif isinstance(unit, Week):
4167            unit.set("this", Var(this=unit.this.name))
4168
4169        super().__init__(**args)
4170
4171    @property
4172    def unit(self) -> t.Optional[Var]:
4173        return self.args.get("unit")
4174
4175
4176class IntervalOp(TimeUnit):
4177    arg_types = {"unit": True, "expression": True}
4178
4179    def interval(self):
4180        return Interval(
4181            this=self.expression.copy(),
4182            unit=self.unit.copy(),
4183        )
4184
4185
4186# https://www.oracletutorial.com/oracle-basics/oracle-interval/
4187# https://trino.io/docs/current/language/types.html#interval-day-to-second
4188# https://docs.databricks.com/en/sql/language-manual/data-types/interval-type.html
4189class IntervalSpan(DataType):
4190    arg_types = {"this": True, "expression": True}
4191
4192
4193class Interval(TimeUnit):
4194    arg_types = {"this": False, "unit": False}
4195
4196
4197class IgnoreNulls(Expression):
4198    pass
4199
4200
4201class RespectNulls(Expression):
4202    pass
4203
4204
4205# Functions
4206class Func(Condition):
4207    """
4208    The base class for all function expressions.
4209
4210    Attributes:
4211        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
4212            treated as a variable length argument and the argument's value will be stored as a list.
4213        _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items)
4214            for this function expression. These values are used to map this node to a name during parsing
4215            as well as to provide the function's name during SQL string generation. By default the SQL
4216            name is set to the expression's class name transformed to snake case.
4217    """
4218
4219    is_var_len_args = False
4220
4221    @classmethod
4222    def from_arg_list(cls, args):
4223        if cls.is_var_len_args:
4224            all_arg_keys = list(cls.arg_types)
4225            # If this function supports variable length argument treat the last argument as such.
4226            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
4227            num_non_var = len(non_var_len_arg_keys)
4228
4229            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
4230            args_dict[all_arg_keys[-1]] = args[num_non_var:]
4231        else:
4232            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
4233
4234        return cls(**args_dict)
4235
4236    @classmethod
4237    def sql_names(cls):
4238        if cls is Func:
4239            raise NotImplementedError(
4240                "SQL name is only supported by concrete function implementations"
4241            )
4242        if "_sql_names" not in cls.__dict__:
4243            cls._sql_names = [camel_to_snake_case(cls.__name__)]
4244        return cls._sql_names
4245
4246    @classmethod
4247    def sql_name(cls):
4248        return cls.sql_names()[0]
4249
4250    @classmethod
4251    def default_parser_mappings(cls):
4252        return {name: cls.from_arg_list for name in cls.sql_names()}
4253
4254
4255class AggFunc(Func):
4256    pass
4257
4258
4259class ParameterizedAgg(AggFunc):
4260    arg_types = {"this": True, "expressions": True, "params": True}
4261
4262
4263class Abs(Func):
4264    pass
4265
4266
4267class ArgMax(AggFunc):
4268    arg_types = {"this": True, "expression": True, "count": False}
4269    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
4270
4271
4272class ArgMin(AggFunc):
4273    arg_types = {"this": True, "expression": True, "count": False}
4274    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
4275
4276
4277class ApproxTopK(AggFunc):
4278    arg_types = {"this": True, "expression": False, "counters": False}
4279
4280
4281class Flatten(Func):
4282    pass
4283
4284
4285# https://spark.apache.org/docs/latest/api/sql/index.html#transform
4286class Transform(Func):
4287    arg_types = {"this": True, "expression": True}
4288
4289
4290class Anonymous(Func):
4291    arg_types = {"this": True, "expressions": False}
4292    is_var_len_args = True
4293
4294
4295# https://docs.snowflake.com/en/sql-reference/functions/hll
4296# https://docs.aws.amazon.com/redshift/latest/dg/r_HLL_function.html
4297class Hll(AggFunc):
4298    arg_types = {"this": True, "expressions": False}
4299    is_var_len_args = True
4300
4301
4302class ApproxDistinct(AggFunc):
4303    arg_types = {"this": True, "accuracy": False}
4304    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
4305
4306
4307class Array(Func):
4308    arg_types = {"expressions": False}
4309    is_var_len_args = True
4310
4311
4312# https://docs.snowflake.com/en/sql-reference/functions/to_char
4313# https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/TO_CHAR-number.html
4314class ToChar(Func):
4315    arg_types = {"this": True, "format": False, "nlsparam": False}
4316
4317
4318class GenerateSeries(Func):
4319    arg_types = {"start": True, "end": True, "step": False}
4320
4321
4322class ArrayAgg(AggFunc):
4323    pass
4324
4325
4326class ArrayUniqueAgg(AggFunc):
4327    pass
4328
4329
4330class ArrayAll(Func):
4331    arg_types = {"this": True, "expression": True}
4332
4333
4334class ArrayAny(Func):
4335    arg_types = {"this": True, "expression": True}
4336
4337
4338class ArrayConcat(Func):
4339    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
4340    arg_types = {"this": True, "expressions": False}
4341    is_var_len_args = True
4342
4343
4344class ArrayContains(Binary, Func):
4345    pass
4346
4347
4348class ArrayContained(Binary):
4349    pass
4350
4351
4352class ArrayFilter(Func):
4353    arg_types = {"this": True, "expression": True}
4354    _sql_names = ["FILTER", "ARRAY_FILTER"]
4355
4356
4357class ArrayJoin(Func):
4358    arg_types = {"this": True, "expression": True, "null": False}
4359
4360
4361class ArraySize(Func):
4362    arg_types = {"this": True, "expression": False}
4363
4364
4365class ArraySort(Func):
4366    arg_types = {"this": True, "expression": False}
4367
4368
4369class ArraySum(Func):
4370    pass
4371
4372
4373class ArrayUnionAgg(AggFunc):
4374    pass
4375
4376
4377class Avg(AggFunc):
4378    pass
4379
4380
4381class AnyValue(AggFunc):
4382    arg_types = {"this": True, "having": False, "max": False, "ignore_nulls": False}
4383
4384
4385class First(Func):
4386    arg_types = {"this": True, "ignore_nulls": False}
4387
4388
4389class Last(Func):
4390    arg_types = {"this": True, "ignore_nulls": False}
4391
4392
4393class Case(Func):
4394    arg_types = {"this": False, "ifs": True, "default": False}
4395
4396    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
4397        instance = maybe_copy(self, copy)
4398        instance.append(
4399            "ifs",
4400            If(
4401                this=maybe_parse(condition, copy=copy, **opts),
4402                true=maybe_parse(then, copy=copy, **opts),
4403            ),
4404        )
4405        return instance
4406
4407    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
4408        instance = maybe_copy(self, copy)
4409        instance.set("default", maybe_parse(condition, copy=copy, **opts))
4410        return instance
4411
4412
4413class Cast(Func):
4414    arg_types = {"this": True, "to": True, "format": False, "safe": False}
4415
4416    @property
4417    def name(self) -> str:
4418        return self.this.name
4419
4420    @property
4421    def to(self) -> DataType:
4422        return self.args["to"]
4423
4424    @property
4425    def output_name(self) -> str:
4426        return self.name
4427
4428    def is_type(self, *dtypes: DATA_TYPE) -> bool:
4429        """
4430        Checks whether this Cast's DataType matches one of the provided data types. Nested types
4431        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
4432        array<int> != array<float>.
4433
4434        Args:
4435            dtypes: the data types to compare this Cast's DataType to.
4436
4437        Returns:
4438            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
4439        """
4440        return self.to.is_type(*dtypes)
4441
4442
4443class TryCast(Cast):
4444    pass
4445
4446
4447class CastToStrType(Func):
4448    arg_types = {"this": True, "to": True}
4449
4450
4451class Collate(Binary, Func):
4452    pass
4453
4454
4455class Ceil(Func):
4456    arg_types = {"this": True, "decimals": False}
4457    _sql_names = ["CEIL", "CEILING"]
4458
4459
4460class Coalesce(Func):
4461    arg_types = {"this": True, "expressions": False}
4462    is_var_len_args = True
4463    _sql_names = ["COALESCE", "IFNULL", "NVL"]
4464
4465
4466class Chr(Func):
4467    arg_types = {"this": True, "charset": False, "expressions": False}
4468    is_var_len_args = True
4469    _sql_names = ["CHR", "CHAR"]
4470
4471
4472class Concat(Func):
4473    arg_types = {"expressions": True, "safe": False, "coalesce": False}
4474    is_var_len_args = True
4475
4476
4477class ConcatWs(Concat):
4478    _sql_names = ["CONCAT_WS"]
4479
4480
4481class Count(AggFunc):
4482    arg_types = {"this": False, "expressions": False}
4483    is_var_len_args = True
4484
4485
4486class CountIf(AggFunc):
4487    pass
4488
4489
4490class CurrentDate(Func):
4491    arg_types = {"this": False}
4492
4493
4494class CurrentDatetime(Func):
4495    arg_types = {"this": False}
4496
4497
4498class CurrentTime(Func):
4499    arg_types = {"this": False}
4500
4501
4502class CurrentTimestamp(Func):
4503    arg_types = {"this": False}
4504
4505
4506class CurrentUser(Func):
4507    arg_types = {"this": False}
4508
4509
4510class DateAdd(Func, IntervalOp):
4511    arg_types = {"this": True, "expression": True, "unit": False}
4512
4513
4514class DateSub(Func, IntervalOp):
4515    arg_types = {"this": True, "expression": True, "unit": False}
4516
4517
4518class DateDiff(Func, TimeUnit):
4519    _sql_names = ["DATEDIFF", "DATE_DIFF"]
4520    arg_types = {"this": True, "expression": True, "unit": False}
4521
4522
4523class DateTrunc(Func):
4524    arg_types = {"unit": True, "this": True, "zone": False}
4525
4526    @property
4527    def unit(self) -> Expression:
4528        return self.args["unit"]
4529
4530
4531class DatetimeAdd(Func, IntervalOp):
4532    arg_types = {"this": True, "expression": True, "unit": False}
4533
4534
4535class DatetimeSub(Func, IntervalOp):
4536    arg_types = {"this": True, "expression": True, "unit": False}
4537
4538
4539class DatetimeDiff(Func, TimeUnit):
4540    arg_types = {"this": True, "expression": True, "unit": False}
4541
4542
4543class DatetimeTrunc(Func, TimeUnit):
4544    arg_types = {"this": True, "unit": True, "zone": False}
4545
4546
4547class DayOfWeek(Func):
4548    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
4549
4550
4551class DayOfMonth(Func):
4552    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
4553
4554
4555class DayOfYear(Func):
4556    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
4557
4558
4559class ToDays(Func):
4560    pass
4561
4562
4563class WeekOfYear(Func):
4564    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
4565
4566
4567class MonthsBetween(Func):
4568    arg_types = {"this": True, "expression": True, "roundoff": False}
4569
4570
4571class LastDateOfMonth(Func):
4572    pass
4573
4574
4575class Extract(Func):
4576    arg_types = {"this": True, "expression": True}
4577
4578
4579class Timestamp(Func):
4580    arg_types = {"this": False, "expression": False}
4581
4582
4583class TimestampAdd(Func, TimeUnit):
4584    arg_types = {"this": True, "expression": True, "unit": False}
4585
4586
4587class TimestampSub(Func, TimeUnit):
4588    arg_types = {"this": True, "expression": True, "unit": False}
4589
4590
4591class TimestampDiff(Func, TimeUnit):
4592    arg_types = {"this": True, "expression": True, "unit": False}
4593
4594
4595class TimestampTrunc(Func, TimeUnit):
4596    arg_types = {"this": True, "unit": True, "zone": False}
4597
4598
4599class TimeAdd(Func, TimeUnit):
4600    arg_types = {"this": True, "expression": True, "unit": False}
4601
4602
4603class TimeSub(Func, TimeUnit):
4604    arg_types = {"this": True, "expression": True, "unit": False}
4605
4606
4607class TimeDiff(Func, TimeUnit):
4608    arg_types = {"this": True, "expression": True, "unit": False}
4609
4610
4611class TimeTrunc(Func, TimeUnit):
4612    arg_types = {"this": True, "unit": True, "zone": False}
4613
4614
4615class DateFromParts(Func):
4616    _sql_names = ["DATEFROMPARTS"]
4617    arg_types = {"year": True, "month": True, "day": True}
4618
4619
4620class DateStrToDate(Func):
4621    pass
4622
4623
4624class DateToDateStr(Func):
4625    pass
4626
4627
4628class DateToDi(Func):
4629    pass
4630
4631
4632# https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date
4633class Date(Func):
4634    arg_types = {"this": False, "zone": False, "expressions": False}
4635    is_var_len_args = True
4636
4637
4638class Day(Func):
4639    pass
4640
4641
4642class Decode(Func):
4643    arg_types = {"this": True, "charset": True, "replace": False}
4644
4645
4646class DiToDate(Func):
4647    pass
4648
4649
4650class Encode(Func):
4651    arg_types = {"this": True, "charset": True}
4652
4653
4654class Exp(Func):
4655    pass
4656
4657
4658# https://docs.snowflake.com/en/sql-reference/functions/flatten
4659class Explode(Func):
4660    arg_types = {"this": True, "expressions": False}
4661    is_var_len_args = True
4662
4663
4664class ExplodeOuter(Explode):
4665    pass
4666
4667
4668class Posexplode(Explode):
4669    pass
4670
4671
4672class PosexplodeOuter(Posexplode):
4673    pass
4674
4675
4676class Floor(Func):
4677    arg_types = {"this": True, "decimals": False}
4678
4679
4680class FromBase64(Func):
4681    pass
4682
4683
4684class ToBase64(Func):
4685    pass
4686
4687
4688class Greatest(Func):
4689    arg_types = {"this": True, "expressions": False}
4690    is_var_len_args = True
4691
4692
4693class GroupConcat(AggFunc):
4694    arg_types = {"this": True, "separator": False}
4695
4696
4697class Hex(Func):
4698    pass
4699
4700
4701class Xor(Connector, Func):
4702    arg_types = {"this": False, "expression": False, "expressions": False}
4703
4704
4705class If(Func):
4706    arg_types = {"this": True, "true": True, "false": False}
4707
4708
4709class Nullif(Func):
4710    arg_types = {"this": True, "expression": True}
4711
4712
4713class Initcap(Func):
4714    arg_types = {"this": True, "expression": False}
4715
4716
4717class IsNan(Func):
4718    _sql_names = ["IS_NAN", "ISNAN"]
4719
4720
4721class IsInf(Func):
4722    _sql_names = ["IS_INF", "ISINF"]
4723
4724
4725class FormatJson(Expression):
4726    pass
4727
4728
4729class JSONKeyValue(Expression):
4730    arg_types = {"this": True, "expression": True}
4731
4732
4733class JSONObject(Func):
4734    arg_types = {
4735        "expressions": False,
4736        "null_handling": False,
4737        "unique_keys": False,
4738        "return_type": False,
4739        "encoding": False,
4740    }
4741
4742
4743# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAY.html
4744class JSONArray(Func):
4745    arg_types = {
4746        "expressions": True,
4747        "null_handling": False,
4748        "return_type": False,
4749        "strict": False,
4750    }
4751
4752
4753# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAYAGG.html
4754class JSONArrayAgg(Func):
4755    arg_types = {
4756        "this": True,
4757        "order": False,
4758        "null_handling": False,
4759        "return_type": False,
4760        "strict": False,
4761    }
4762
4763
4764# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_TABLE.html
4765# Note: parsing of JSON column definitions is currently incomplete.
4766class JSONColumnDef(Expression):
4767    arg_types = {"this": False, "kind": False, "path": False, "nested_schema": False}
4768
4769
4770class JSONSchema(Expression):
4771    arg_types = {"expressions": True}
4772
4773
4774# # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_TABLE.html
4775class JSONTable(Func):
4776    arg_types = {
4777        "this": True,
4778        "schema": True,
4779        "path": False,
4780        "error_handling": False,
4781        "empty_handling": False,
4782    }
4783
4784
4785class OpenJSONColumnDef(Expression):
4786    arg_types = {"this": True, "kind": True, "path": False, "as_json": False}
4787
4788
4789class OpenJSON(Func):
4790    arg_types = {"this": True, "path": False, "expressions": False}
4791
4792
4793class JSONBContains(Binary):
4794    _sql_names = ["JSONB_CONTAINS"]
4795
4796
4797class JSONExtract(Binary, Func):
4798    _sql_names = ["JSON_EXTRACT"]
4799
4800
4801class JSONExtractScalar(JSONExtract):
4802    _sql_names = ["JSON_EXTRACT_SCALAR"]
4803
4804
4805class JSONBExtract(JSONExtract):
4806    _sql_names = ["JSONB_EXTRACT"]
4807
4808
4809class JSONBExtractScalar(JSONExtract):
4810    _sql_names = ["JSONB_EXTRACT_SCALAR"]
4811
4812
4813class JSONFormat(Func):
4814    arg_types = {"this": False, "options": False}
4815    _sql_names = ["JSON_FORMAT"]
4816
4817
4818# https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_member-of
4819class JSONArrayContains(Binary, Predicate, Func):
4820    _sql_names = ["JSON_ARRAY_CONTAINS"]
4821
4822
4823class ParseJSON(Func):
4824    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
4825    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
4826    arg_types = {"this": True, "expressions": False}
4827    is_var_len_args = True
4828
4829
4830class Least(Func):
4831    arg_types = {"this": True, "expressions": False}
4832    is_var_len_args = True
4833
4834
4835class Left(Func):
4836    arg_types = {"this": True, "expression": True}
4837
4838
4839class Right(Func):
4840    arg_types = {"this": True, "expression": True}
4841
4842
4843class Length(Func):
4844    _sql_names = ["LENGTH", "LEN"]
4845
4846
4847class Levenshtein(Func):
4848    arg_types = {
4849        "this": True,
4850        "expression": False,
4851        "ins_cost": False,
4852        "del_cost": False,
4853        "sub_cost": False,
4854    }
4855
4856
4857class Ln(Func):
4858    pass
4859
4860
4861class Log(Func):
4862    arg_types = {"this": True, "expression": False}
4863
4864
4865class Log2(Func):
4866    pass
4867
4868
4869class Log10(Func):
4870    pass
4871
4872
4873class LogicalOr(AggFunc):
4874    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
4875
4876
4877class LogicalAnd(AggFunc):
4878    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
4879
4880
4881class Lower(Func):
4882    _sql_names = ["LOWER", "LCASE"]
4883
4884
4885class Map(Func):
4886    arg_types = {"keys": False, "values": False}
4887
4888    @property
4889    def keys(self) -> t.List[Expression]:
4890        keys = self.args.get("keys")
4891        return keys.expressions if keys else []
4892
4893    @property
4894    def values(self) -> t.List[Expression]:
4895        values = self.args.get("values")
4896        return values.expressions if values else []
4897
4898
4899class MapFromEntries(Func):
4900    pass
4901
4902
4903class StarMap(Func):
4904    pass
4905
4906
4907class VarMap(Func):
4908    arg_types = {"keys": True, "values": True}
4909    is_var_len_args = True
4910
4911    @property
4912    def keys(self) -> t.List[Expression]:
4913        return self.args["keys"].expressions
4914
4915    @property
4916    def values(self) -> t.List[Expression]:
4917        return self.args["values"].expressions
4918
4919
4920# https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html
4921class MatchAgainst(Func):
4922    arg_types = {"this": True, "expressions": True, "modifier": False}
4923
4924
4925class Max(AggFunc):
4926    arg_types = {"this": True, "expressions": False}
4927    is_var_len_args = True
4928
4929
4930class MD5(Func):
4931    _sql_names = ["MD5"]
4932
4933
4934# Represents the variant of the MD5 function that returns a binary value
4935class MD5Digest(Func):
4936    _sql_names = ["MD5_DIGEST"]
4937
4938
4939class Min(AggFunc):
4940    arg_types = {"this": True, "expressions": False}
4941    is_var_len_args = True
4942
4943
4944class Month(Func):
4945    pass
4946
4947
4948class Nvl2(Func):
4949    arg_types = {"this": True, "true": True, "false": False}
4950
4951
4952# https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-predict#mlpredict_function
4953class Predict(Func):
4954    arg_types = {"this": True, "expression": True, "params_struct": False}
4955
4956
4957class Pow(Binary, Func):
4958    _sql_names = ["POWER", "POW"]
4959
4960
4961class PercentileCont(AggFunc):
4962    arg_types = {"this": True, "expression": False}
4963
4964
4965class PercentileDisc(AggFunc):
4966    arg_types = {"this": True, "expression": False}
4967
4968
4969class Quantile(AggFunc):
4970    arg_types = {"this": True, "quantile": True}
4971
4972
4973class ApproxQuantile(Quantile):
4974    arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
4975
4976
4977class RangeN(Func):
4978    arg_types = {"this": True, "expressions": True, "each": False}
4979
4980
4981class ReadCSV(Func):
4982    _sql_names = ["READ_CSV"]
4983    is_var_len_args = True
4984    arg_types = {"this": True, "expressions": False}
4985
4986
4987class Reduce(Func):
4988    arg_types = {"this": True, "initial": True, "merge": True, "finish": False}
4989
4990
4991class RegexpExtract(Func):
4992    arg_types = {
4993        "this": True,
4994        "expression": True,
4995        "position": False,
4996        "occurrence": False,
4997        "parameters": False,
4998        "group": False,
4999    }
5000
5001
5002class RegexpReplace(Func):
5003    arg_types = {
5004        "this": True,
5005        "expression": True,
5006        "replacement": True,
5007        "position": False,
5008        "occurrence": False,
5009        "parameters": False,
5010        "modifiers": False,
5011    }
5012
5013
5014class RegexpLike(Binary, Func):
5015    arg_types = {"this": True, "expression": True, "flag": False}
5016
5017
5018class RegexpILike(Binary, Func):
5019    arg_types = {"this": True, "expression": True, "flag": False}
5020
5021
5022# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split.html
5023# limit is the number of times a pattern is applied
5024class RegexpSplit(Func):
5025    arg_types = {"this": True, "expression": True, "limit": False}
5026
5027
5028class Repeat(Func):
5029    arg_types = {"this": True, "times": True}
5030
5031
5032class Round(Func):
5033    arg_types = {"this": True, "decimals": False}
5034
5035
5036class RowNumber(Func):
5037    arg_types: t.Dict[str, t.Any] = {}
5038
5039
5040class SafeDivide(Func):
5041    arg_types = {"this": True, "expression": True}
5042
5043
5044class SHA(Func):
5045    _sql_names = ["SHA", "SHA1"]
5046
5047
5048class SHA2(Func):
5049    _sql_names = ["SHA2"]
5050    arg_types = {"this": True, "length": False}
5051
5052
5053class SortArray(Func):
5054    arg_types = {"this": True, "asc": False}
5055
5056
5057class Split(Func):
5058    arg_types = {"this": True, "expression": True, "limit": False}
5059
5060
5061# Start may be omitted in the case of postgres
5062# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6
5063class Substring(Func):
5064    arg_types = {"this": True, "start": False, "length": False}
5065
5066
5067class StandardHash(Func):
5068    arg_types = {"this": True, "expression": False}
5069
5070
5071class StartsWith(Func):
5072    _sql_names = ["STARTS_WITH", "STARTSWITH"]
5073    arg_types = {"this": True, "expression": True}
5074
5075
5076class StrPosition(Func):
5077    arg_types = {
5078        "this": True,
5079        "substr": True,
5080        "position": False,
5081        "instance": False,
5082    }
5083
5084
5085class StrToDate(Func):
5086    arg_types = {"this": True, "format": True}
5087
5088
5089class StrToTime(Func):
5090    arg_types = {"this": True, "format": True, "zone": False}
5091
5092
5093# Spark allows unix_timestamp()
5094# https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.unix_timestamp.html
5095class StrToUnix(Func):
5096    arg_types = {"this": False, "format": False}
5097
5098
5099# https://prestodb.io/docs/current/functions/string.html
5100# https://spark.apache.org/docs/latest/api/sql/index.html#str_to_map
5101class StrToMap(Func):
5102    arg_types = {
5103        "this": True,
5104        "pair_delim": False,
5105        "key_value_delim": False,
5106        "duplicate_resolution_callback": False,
5107    }
5108
5109
5110class NumberToStr(Func):
5111    arg_types = {"this": True, "format": True, "culture": False}
5112
5113
5114class FromBase(Func):
5115    arg_types = {"this": True, "expression": True}
5116
5117
5118class Struct(Func):
5119    arg_types = {"expressions": False}
5120    is_var_len_args = True
5121
5122
5123class StructExtract(Func):
5124    arg_types = {"this": True, "expression": True}
5125
5126
5127# https://learn.microsoft.com/en-us/sql/t-sql/functions/stuff-transact-sql?view=sql-server-ver16
5128# https://docs.snowflake.com/en/sql-reference/functions/insert
5129class Stuff(Func):
5130    _sql_names = ["STUFF", "INSERT"]
5131    arg_types = {"this": True, "start": True, "length": True, "expression": True}
5132
5133
5134class Sum(AggFunc):
5135    pass
5136
5137
5138class Sqrt(Func):
5139    pass
5140
5141
5142class Stddev(AggFunc):
5143    pass
5144
5145
5146class StddevPop(AggFunc):
5147    pass
5148
5149
5150class StddevSamp(AggFunc):
5151    pass
5152
5153
5154class TimeToStr(Func):
5155    arg_types = {"this": True, "format": True, "culture": False}
5156
5157
5158class TimeToTimeStr(Func):
5159    pass
5160
5161
5162class TimeToUnix(Func):
5163    pass
5164
5165
5166class TimeStrToDate(Func):
5167    pass
5168
5169
5170class TimeStrToTime(Func):
5171    pass
5172
5173
5174class TimeStrToUnix(Func):
5175    pass
5176
5177
5178class Trim(Func):
5179    arg_types = {
5180        "this": True,
5181        "expression": False,
5182        "position": False,
5183        "collation": False,
5184    }
5185
5186
5187class TsOrDsAdd(Func, TimeUnit):
5188    # return_type is used to correctly cast the arguments of this expression when transpiling it
5189    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
5190
5191    @property
5192    def return_type(self) -> DataType:
5193        return DataType.build(self.args.get("return_type") or DataType.Type.DATE)
5194
5195
5196class TsOrDsDiff(Func, TimeUnit):
5197    arg_types = {"this": True, "expression": True, "unit": False}
5198
5199
5200class TsOrDsToDateStr(Func):
5201    pass
5202
5203
5204class TsOrDsToDate(Func):
5205    arg_types = {"this": True, "format": False}
5206
5207
5208class TsOrDiToDi(Func):
5209    pass
5210
5211
5212class Unhex(Func):
5213    pass
5214
5215
5216class UnixToStr(Func):
5217    arg_types = {"this": True, "format": False}
5218
5219
5220# https://prestodb.io/docs/current/functions/datetime.html
5221# presto has weird zone/hours/minutes
5222class UnixToTime(Func):
5223    arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False}
5224
5225    SECONDS = Literal.string("seconds")
5226    MILLIS = Literal.string("millis")
5227    MICROS = Literal.string("micros")
5228    NANOS = Literal.string("nanos")
5229
5230
5231class UnixToTimeStr(Func):
5232    pass
5233
5234
5235class TimestampFromParts(Func):
5236    """Constructs a timestamp given its constituent parts."""
5237
5238    arg_types = {
5239        "year": True,
5240        "month": True,
5241        "day": True,
5242        "hour": True,
5243        "min": True,
5244        "sec": True,
5245    }
5246
5247
5248class Upper(Func):
5249    _sql_names = ["UPPER", "UCASE"]
5250
5251
5252class Variance(AggFunc):
5253    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
5254
5255
5256class VariancePop(AggFunc):
5257    _sql_names = ["VARIANCE_POP", "VAR_POP"]
5258
5259
5260class Week(Func):
5261    arg_types = {"this": True, "mode": False}
5262
5263
5264class XMLTable(Func):
5265    arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False}
5266
5267
5268class Year(Func):
5269    pass
5270
5271
5272class Use(Expression):
5273    arg_types = {"this": True, "kind": False}
5274
5275
5276class Merge(Expression):
5277    arg_types = {"this": True, "using": True, "on": True, "expressions": True, "with": False}
5278
5279
5280class When(Func):
5281    arg_types = {"matched": True, "source": False, "condition": False, "then": True}
5282
5283
5284# https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqljnextvaluefor.html
5285# https://learn.microsoft.com/en-us/sql/t-sql/functions/next-value-for-transact-sql?view=sql-server-ver16
5286class NextValueFor(Func):
5287    arg_types = {"this": True, "order": False}
5288
5289
5290def _norm_arg(arg):
5291    return arg.lower() if type(arg) is str else arg
5292
5293
5294ALL_FUNCTIONS = subclasses(__name__, Func, (AggFunc, Anonymous, Func))
5295FUNCTION_BY_NAME = {name: func for func in ALL_FUNCTIONS for name in func.sql_names()}
5296
5297
5298# Helpers
5299@t.overload
5300def maybe_parse(
5301    sql_or_expression: ExpOrStr,
5302    *,
5303    into: t.Type[E],
5304    dialect: DialectType = None,
5305    prefix: t.Optional[str] = None,
5306    copy: bool = False,
5307    **opts,
5308) -> E:
5309    ...
5310
5311
5312@t.overload
5313def maybe_parse(
5314    sql_or_expression: str | E,
5315    *,
5316    into: t.Optional[IntoType] = None,
5317    dialect: DialectType = None,
5318    prefix: t.Optional[str] = None,
5319    copy: bool = False,
5320    **opts,
5321) -> E:
5322    ...
5323
5324
5325def maybe_parse(
5326    sql_or_expression: ExpOrStr,
5327    *,
5328    into: t.Optional[IntoType] = None,
5329    dialect: DialectType = None,
5330    prefix: t.Optional[str] = None,
5331    copy: bool = False,
5332    **opts,
5333) -> Expression:
5334    """Gracefully handle a possible string or expression.
5335
5336    Example:
5337        >>> maybe_parse("1")
5338        (LITERAL this: 1, is_string: False)
5339        >>> maybe_parse(to_identifier("x"))
5340        (IDENTIFIER this: x, quoted: False)
5341
5342    Args:
5343        sql_or_expression: the SQL code string or an expression
5344        into: the SQLGlot Expression to parse into
5345        dialect: the dialect used to parse the input expressions (in the case that an
5346            input expression is a SQL string).
5347        prefix: a string to prefix the sql with before it gets parsed
5348            (automatically includes a space)
5349        copy: whether or not to copy the expression.
5350        **opts: other options to use to parse the input expressions (again, in the case
5351            that an input expression is a SQL string).
5352
5353    Returns:
5354        Expression: the parsed or given expression.
5355    """
5356    if isinstance(sql_or_expression, Expression):
5357        if copy:
5358            return sql_or_expression.copy()
5359        return sql_or_expression
5360
5361    if sql_or_expression is None:
5362        raise ParseError(f"SQL cannot be None")
5363
5364    import sqlglot
5365
5366    sql = str(sql_or_expression)
5367    if prefix:
5368        sql = f"{prefix} {sql}"
5369
5370    return sqlglot.parse_one(sql, read=dialect, into=into, **opts)
5371
5372
5373@t.overload
5374def maybe_copy(instance: None, copy: bool = True) -> None:
5375    ...
5376
5377
5378@t.overload
5379def maybe_copy(instance: E, copy: bool = True) -> E:
5380    ...
5381
5382
5383def maybe_copy(instance, copy=True):
5384    return instance.copy() if copy and instance else instance
5385
5386
5387def _is_wrong_expression(expression, into):
5388    return isinstance(expression, Expression) and not isinstance(expression, into)
5389
5390
5391def _apply_builder(
5392    expression,
5393    instance,
5394    arg,
5395    copy=True,
5396    prefix=None,
5397    into=None,
5398    dialect=None,
5399    into_arg="this",
5400    **opts,
5401):
5402    if _is_wrong_expression(expression, into):
5403        expression = into(**{into_arg: expression})
5404    instance = maybe_copy(instance, copy)
5405    expression = maybe_parse(
5406        sql_or_expression=expression,
5407        prefix=prefix,
5408        into=into,
5409        dialect=dialect,
5410        **opts,
5411    )
5412    instance.set(arg, expression)
5413    return instance
5414
5415
5416def _apply_child_list_builder(
5417    *expressions,
5418    instance,
5419    arg,
5420    append=True,
5421    copy=True,
5422    prefix=None,
5423    into=None,
5424    dialect=None,
5425    properties=None,
5426    **opts,
5427):
5428    instance = maybe_copy(instance, copy)
5429    parsed = []
5430    for expression in expressions:
5431        if expression is not None:
5432            if _is_wrong_expression(expression, into):
5433                expression = into(expressions=[expression])
5434
5435            expression = maybe_parse(
5436                expression,
5437                into=into,
5438                dialect=dialect,
5439                prefix=prefix,
5440                **opts,
5441            )
5442            parsed.extend(expression.expressions)
5443
5444    existing = instance.args.get(arg)
5445    if append and existing:
5446        parsed = existing.expressions + parsed
5447
5448    child = into(expressions=parsed)
5449    for k, v in (properties or {}).items():
5450        child.set(k, v)
5451    instance.set(arg, child)
5452
5453    return instance
5454
5455
5456def _apply_list_builder(
5457    *expressions,
5458    instance,
5459    arg,
5460    append=True,
5461    copy=True,
5462    prefix=None,
5463    into=None,
5464    dialect=None,
5465    **opts,
5466):
5467    inst = maybe_copy(instance, copy)
5468
5469    expressions = [
5470        maybe_parse(
5471            sql_or_expression=expression,
5472            into=into,
5473            prefix=prefix,
5474            dialect=dialect,
5475            **opts,
5476        )
5477        for expression in expressions
5478        if expression is not None
5479    ]
5480
5481    existing_expressions = inst.args.get(arg)
5482    if append and existing_expressions:
5483        expressions = existing_expressions + expressions
5484
5485    inst.set(arg, expressions)
5486    return inst
5487
5488
5489def _apply_conjunction_builder(
5490    *expressions,
5491    instance,
5492    arg,
5493    into=None,
5494    append=True,
5495    copy=True,
5496    dialect=None,
5497    **opts,
5498):
5499    expressions = [exp for exp in expressions if exp is not None and exp != ""]
5500    if not expressions:
5501        return instance
5502
5503    inst = maybe_copy(instance, copy)
5504
5505    existing = inst.args.get(arg)
5506    if append and existing is not None:
5507        expressions = [existing.this if into else existing] + list(expressions)
5508
5509    node = and_(*expressions, dialect=dialect, copy=copy, **opts)
5510
5511    inst.set(arg, into(this=node) if into else node)
5512    return inst
5513
5514
5515def _apply_cte_builder(
5516    instance: E,
5517    alias: ExpOrStr,
5518    as_: ExpOrStr,
5519    recursive: t.Optional[bool] = None,
5520    append: bool = True,
5521    dialect: DialectType = None,
5522    copy: bool = True,
5523    **opts,
5524) -> E:
5525    alias_expression = maybe_parse(alias, dialect=dialect, into=TableAlias, **opts)
5526    as_expression = maybe_parse(as_, dialect=dialect, **opts)
5527    cte = CTE(this=as_expression, alias=alias_expression)
5528    return _apply_child_list_builder(
5529        cte,
5530        instance=instance,
5531        arg="with",
5532        append=append,
5533        copy=copy,
5534        into=With,
5535        properties={"recursive": recursive or False},
5536    )
5537
5538
5539def _combine(
5540    expressions: t.Sequence[t.Optional[ExpOrStr]],
5541    operator: t.Type[Connector],
5542    dialect: DialectType = None,
5543    copy: bool = True,
5544    **opts,
5545) -> Expression:
5546    conditions = [
5547        condition(expression, dialect=dialect, copy=copy, **opts)
5548        for expression in expressions
5549        if expression is not None
5550    ]
5551
5552    this, *rest = conditions
5553    if rest:
5554        this = _wrap(this, Connector)
5555    for expression in rest:
5556        this = operator(this=this, expression=_wrap(expression, Connector))
5557
5558    return this
5559
5560
5561def _wrap(expression: E, kind: t.Type[Expression]) -> E | Paren:
5562    return Paren(this=expression) if isinstance(expression, kind) else expression
5563
5564
5565def union(
5566    left: ExpOrStr,
5567    right: ExpOrStr,
5568    distinct: bool = True,
5569    dialect: DialectType = None,
5570    copy: bool = True,
5571    **opts,
5572) -> Union:
5573    """
5574    Initializes a syntax tree from one UNION expression.
5575
5576    Example:
5577        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
5578        'SELECT * FROM foo UNION SELECT * FROM bla'
5579
5580    Args:
5581        left: the SQL code string corresponding to the left-hand side.
5582            If an `Expression` instance is passed, it will be used as-is.
5583        right: the SQL code string corresponding to the right-hand side.
5584            If an `Expression` instance is passed, it will be used as-is.
5585        distinct: set the DISTINCT flag if and only if this is true.
5586        dialect: the dialect used to parse the input expression.
5587        copy: whether or not to copy the expression.
5588        opts: other options to use to parse the input expressions.
5589
5590    Returns:
5591        The new Union instance.
5592    """
5593    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5594    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5595
5596    return Union(this=left, expression=right, distinct=distinct)
5597
5598
5599def intersect(
5600    left: ExpOrStr,
5601    right: ExpOrStr,
5602    distinct: bool = True,
5603    dialect: DialectType = None,
5604    copy: bool = True,
5605    **opts,
5606) -> Intersect:
5607    """
5608    Initializes a syntax tree from one INTERSECT expression.
5609
5610    Example:
5611        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
5612        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
5613
5614    Args:
5615        left: the SQL code string corresponding to the left-hand side.
5616            If an `Expression` instance is passed, it will be used as-is.
5617        right: the SQL code string corresponding to the right-hand side.
5618            If an `Expression` instance is passed, it will be used as-is.
5619        distinct: set the DISTINCT flag if and only if this is true.
5620        dialect: the dialect used to parse the input expression.
5621        copy: whether or not to copy the expression.
5622        opts: other options to use to parse the input expressions.
5623
5624    Returns:
5625        The new Intersect instance.
5626    """
5627    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5628    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5629
5630    return Intersect(this=left, expression=right, distinct=distinct)
5631
5632
5633def except_(
5634    left: ExpOrStr,
5635    right: ExpOrStr,
5636    distinct: bool = True,
5637    dialect: DialectType = None,
5638    copy: bool = True,
5639    **opts,
5640) -> Except:
5641    """
5642    Initializes a syntax tree from one EXCEPT expression.
5643
5644    Example:
5645        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
5646        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
5647
5648    Args:
5649        left: the SQL code string corresponding to the left-hand side.
5650            If an `Expression` instance is passed, it will be used as-is.
5651        right: the SQL code string corresponding to the right-hand side.
5652            If an `Expression` instance is passed, it will be used as-is.
5653        distinct: set the DISTINCT flag if and only if this is true.
5654        dialect: the dialect used to parse the input expression.
5655        copy: whether or not to copy the expression.
5656        opts: other options to use to parse the input expressions.
5657
5658    Returns:
5659        The new Except instance.
5660    """
5661    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5662    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5663
5664    return Except(this=left, expression=right, distinct=distinct)
5665
5666
5667def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5668    """
5669    Initializes a syntax tree from one or multiple SELECT expressions.
5670
5671    Example:
5672        >>> select("col1", "col2").from_("tbl").sql()
5673        'SELECT col1, col2 FROM tbl'
5674
5675    Args:
5676        *expressions: the SQL code string to parse as the expressions of a
5677            SELECT statement. If an Expression instance is passed, this is used as-is.
5678        dialect: the dialect used to parse the input expressions (in the case that an
5679            input expression is a SQL string).
5680        **opts: other options to use to parse the input expressions (again, in the case
5681            that an input expression is a SQL string).
5682
5683    Returns:
5684        Select: the syntax tree for the SELECT statement.
5685    """
5686    return Select().select(*expressions, dialect=dialect, **opts)
5687
5688
5689def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5690    """
5691    Initializes a syntax tree from a FROM expression.
5692
5693    Example:
5694        >>> from_("tbl").select("col1", "col2").sql()
5695        'SELECT col1, col2 FROM tbl'
5696
5697    Args:
5698        *expression: the SQL code string to parse as the FROM expressions of a
5699            SELECT statement. If an Expression instance is passed, this is used as-is.
5700        dialect: the dialect used to parse the input expression (in the case that the
5701            input expression is a SQL string).
5702        **opts: other options to use to parse the input expressions (again, in the case
5703            that the input expression is a SQL string).
5704
5705    Returns:
5706        Select: the syntax tree for the SELECT statement.
5707    """
5708    return Select().from_(expression, dialect=dialect, **opts)
5709
5710
5711def update(
5712    table: str | Table,
5713    properties: dict,
5714    where: t.Optional[ExpOrStr] = None,
5715    from_: t.Optional[ExpOrStr] = None,
5716    dialect: DialectType = None,
5717    **opts,
5718) -> Update:
5719    """
5720    Creates an update statement.
5721
5722    Example:
5723        >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql()
5724        "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1"
5725
5726    Args:
5727        *properties: dictionary of properties to set which are
5728            auto converted to sql objects eg None -> NULL
5729        where: sql conditional parsed into a WHERE statement
5730        from_: sql statement parsed into a FROM statement
5731        dialect: the dialect used to parse the input expressions.
5732        **opts: other options to use to parse the input expressions.
5733
5734    Returns:
5735        Update: the syntax tree for the UPDATE statement.
5736    """
5737    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
5738    update_expr.set(
5739        "expressions",
5740        [
5741            EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
5742            for k, v in properties.items()
5743        ],
5744    )
5745    if from_:
5746        update_expr.set(
5747            "from",
5748            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
5749        )
5750    if isinstance(where, Condition):
5751        where = Where(this=where)
5752    if where:
5753        update_expr.set(
5754            "where",
5755            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
5756        )
5757    return update_expr
5758
5759
5760def delete(
5761    table: ExpOrStr,
5762    where: t.Optional[ExpOrStr] = None,
5763    returning: t.Optional[ExpOrStr] = None,
5764    dialect: DialectType = None,
5765    **opts,
5766) -> Delete:
5767    """
5768    Builds a delete statement.
5769
5770    Example:
5771        >>> delete("my_table", where="id > 1").sql()
5772        'DELETE FROM my_table WHERE id > 1'
5773
5774    Args:
5775        where: sql conditional parsed into a WHERE statement
5776        returning: sql conditional parsed into a RETURNING statement
5777        dialect: the dialect used to parse the input expressions.
5778        **opts: other options to use to parse the input expressions.
5779
5780    Returns:
5781        Delete: the syntax tree for the DELETE statement.
5782    """
5783    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
5784    if where:
5785        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
5786    if returning:
5787        delete_expr = t.cast(
5788            Delete, delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
5789        )
5790    return delete_expr
5791
5792
5793def insert(
5794    expression: ExpOrStr,
5795    into: ExpOrStr,
5796    columns: t.Optional[t.Sequence[ExpOrStr]] = None,
5797    overwrite: t.Optional[bool] = None,
5798    returning: t.Optional[ExpOrStr] = None,
5799    dialect: DialectType = None,
5800    copy: bool = True,
5801    **opts,
5802) -> Insert:
5803    """
5804    Builds an INSERT statement.
5805
5806    Example:
5807        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
5808        'INSERT INTO tbl VALUES (1, 2, 3)'
5809
5810    Args:
5811        expression: the sql string or expression of the INSERT statement
5812        into: the tbl to insert data to.
5813        columns: optionally the table's column names.
5814        overwrite: whether to INSERT OVERWRITE or not.
5815        returning: sql conditional parsed into a RETURNING statement
5816        dialect: the dialect used to parse the input expressions.
5817        copy: whether or not to copy the expression.
5818        **opts: other options to use to parse the input expressions.
5819
5820    Returns:
5821        Insert: the syntax tree for the INSERT statement.
5822    """
5823    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
5824    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
5825
5826    if columns:
5827        this = _apply_list_builder(
5828            *columns,
5829            instance=Schema(this=this),
5830            arg="expressions",
5831            into=Identifier,
5832            copy=False,
5833            dialect=dialect,
5834            **opts,
5835        )
5836
5837    insert = Insert(this=this, expression=expr, overwrite=overwrite)
5838
5839    if returning:
5840        insert = t.cast(Insert, insert.returning(returning, dialect=dialect, copy=False, **opts))
5841
5842    return insert
5843
5844
5845def condition(
5846    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
5847) -> Condition:
5848    """
5849    Initialize a logical condition expression.
5850
5851    Example:
5852        >>> condition("x=1").sql()
5853        'x = 1'
5854
5855        This is helpful for composing larger logical syntax trees:
5856        >>> where = condition("x=1")
5857        >>> where = where.and_("y=1")
5858        >>> Select().from_("tbl").select("*").where(where).sql()
5859        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
5860
5861    Args:
5862        *expression: the SQL code string to parse.
5863            If an Expression instance is passed, this is used as-is.
5864        dialect: the dialect used to parse the input expression (in the case that the
5865            input expression is a SQL string).
5866        copy: Whether or not to copy `expression` (only applies to expressions).
5867        **opts: other options to use to parse the input expressions (again, in the case
5868            that the input expression is a SQL string).
5869
5870    Returns:
5871        The new Condition instance
5872    """
5873    return maybe_parse(
5874        expression,
5875        into=Condition,
5876        dialect=dialect,
5877        copy=copy,
5878        **opts,
5879    )
5880
5881
5882def and_(
5883    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5884) -> Condition:
5885    """
5886    Combine multiple conditions with an AND logical operator.
5887
5888    Example:
5889        >>> and_("x=1", and_("y=1", "z=1")).sql()
5890        'x = 1 AND (y = 1 AND z = 1)'
5891
5892    Args:
5893        *expressions: the SQL code strings to parse.
5894            If an Expression instance is passed, this is used as-is.
5895        dialect: the dialect used to parse the input expression.
5896        copy: whether or not to copy `expressions` (only applies to Expressions).
5897        **opts: other options to use to parse the input expressions.
5898
5899    Returns:
5900        And: the new condition
5901    """
5902    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, **opts))
5903
5904
5905def or_(
5906    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5907) -> Condition:
5908    """
5909    Combine multiple conditions with an OR logical operator.
5910
5911    Example:
5912        >>> or_("x=1", or_("y=1", "z=1")).sql()
5913        'x = 1 OR (y = 1 OR z = 1)'
5914
5915    Args:
5916        *expressions: the SQL code strings to parse.
5917            If an Expression instance is passed, this is used as-is.
5918        dialect: the dialect used to parse the input expression.
5919        copy: whether or not to copy `expressions` (only applies to Expressions).
5920        **opts: other options to use to parse the input expressions.
5921
5922    Returns:
5923        Or: the new condition
5924    """
5925    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, **opts))
5926
5927
5928def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
5929    """
5930    Wrap a condition with a NOT operator.
5931
5932    Example:
5933        >>> not_("this_suit='black'").sql()
5934        "NOT this_suit = 'black'"
5935
5936    Args:
5937        expression: the SQL code string to parse.
5938            If an Expression instance is passed, this is used as-is.
5939        dialect: the dialect used to parse the input expression.
5940        copy: whether to copy the expression or not.
5941        **opts: other options to use to parse the input expressions.
5942
5943    Returns:
5944        The new condition.
5945    """
5946    this = condition(
5947        expression,
5948        dialect=dialect,
5949        copy=copy,
5950        **opts,
5951    )
5952    return Not(this=_wrap(this, Connector))
5953
5954
5955def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
5956    """
5957    Wrap an expression in parentheses.
5958
5959    Example:
5960        >>> paren("5 + 3").sql()
5961        '(5 + 3)'
5962
5963    Args:
5964        expression: the SQL code string to parse.
5965            If an Expression instance is passed, this is used as-is.
5966        copy: whether to copy the expression or not.
5967
5968    Returns:
5969        The wrapped expression.
5970    """
5971    return Paren(this=maybe_parse(expression, copy=copy))
5972
5973
5974SAFE_IDENTIFIER_RE = re.compile(r"^[_a-zA-Z][\w]*$")
5975
5976
5977@t.overload
5978def to_identifier(name: None, quoted: t.Optional[bool] = None, copy: bool = True) -> None:
5979    ...
5980
5981
5982@t.overload
5983def to_identifier(
5984    name: str | Identifier, quoted: t.Optional[bool] = None, copy: bool = True
5985) -> Identifier:
5986    ...
5987
5988
5989def to_identifier(name, quoted=None, copy=True):
5990    """Builds an identifier.
5991
5992    Args:
5993        name: The name to turn into an identifier.
5994        quoted: Whether or not force quote the identifier.
5995        copy: Whether or not to copy name if it's an Identifier.
5996
5997    Returns:
5998        The identifier ast node.
5999    """
6000
6001    if name is None:
6002        return None
6003
6004    if isinstance(name, Identifier):
6005        identifier = maybe_copy(name, copy)
6006    elif isinstance(name, str):
6007        identifier = Identifier(
6008            this=name,
6009            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
6010        )
6011    else:
6012        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
6013    return identifier
6014
6015
6016def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
6017    """
6018    Parses a given string into an identifier.
6019
6020    Args:
6021        name: The name to parse into an identifier.
6022        dialect: The dialect to parse against.
6023
6024    Returns:
6025        The identifier ast node.
6026    """
6027    try:
6028        expression = maybe_parse(name, dialect=dialect, into=Identifier)
6029    except ParseError:
6030        expression = to_identifier(name)
6031
6032    return expression
6033
6034
6035INTERVAL_STRING_RE = re.compile(r"\s*([0-9]+)\s*([a-zA-Z]+)\s*")
6036
6037
6038def to_interval(interval: str | Literal) -> Interval:
6039    """Builds an interval expression from a string like '1 day' or '5 months'."""
6040    if isinstance(interval, Literal):
6041        if not interval.is_string:
6042            raise ValueError("Invalid interval string.")
6043
6044        interval = interval.this
6045
6046    interval_parts = INTERVAL_STRING_RE.match(interval)  # type: ignore
6047
6048    if not interval_parts:
6049        raise ValueError("Invalid interval string.")
6050
6051    return Interval(
6052        this=Literal.string(interval_parts.group(1)),
6053        unit=Var(this=interval_parts.group(2)),
6054    )
6055
6056
6057@t.overload
6058def to_table(sql_path: str | Table, **kwargs) -> Table:
6059    ...
6060
6061
6062@t.overload
6063def to_table(sql_path: None, **kwargs) -> None:
6064    ...
6065
6066
6067def to_table(
6068    sql_path: t.Optional[str | Table], dialect: DialectType = None, copy: bool = True, **kwargs
6069) -> t.Optional[Table]:
6070    """
6071    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
6072    If a table is passed in then that table is returned.
6073
6074    Args:
6075        sql_path: a `[catalog].[schema].[table]` string.
6076        dialect: the source dialect according to which the table name will be parsed.
6077        copy: Whether or not to copy a table if it is passed in.
6078        kwargs: the kwargs to instantiate the resulting `Table` expression with.
6079
6080    Returns:
6081        A table expression.
6082    """
6083    if sql_path is None or isinstance(sql_path, Table):
6084        return maybe_copy(sql_path, copy=copy)
6085    if not isinstance(sql_path, str):
6086        raise ValueError(f"Invalid type provided for a table: {type(sql_path)}")
6087
6088    table = maybe_parse(sql_path, into=Table, dialect=dialect)
6089    if table:
6090        for k, v in kwargs.items():
6091            table.set(k, v)
6092
6093    return table
6094
6095
6096def to_column(sql_path: str | Column, **kwargs) -> Column:
6097    """
6098    Create a column from a `[table].[column]` sql path. Schema is optional.
6099
6100    If a column is passed in then that column is returned.
6101
6102    Args:
6103        sql_path: `[table].[column]` string
6104    Returns:
6105        Table: A column expression
6106    """
6107    if sql_path is None or isinstance(sql_path, Column):
6108        return sql_path
6109    if not isinstance(sql_path, str):
6110        raise ValueError(f"Invalid type provided for column: {type(sql_path)}")
6111    return column(*reversed(sql_path.split(".")), **kwargs)  # type: ignore
6112
6113
6114def alias_(
6115    expression: ExpOrStr,
6116    alias: str | Identifier,
6117    table: bool | t.Sequence[str | Identifier] = False,
6118    quoted: t.Optional[bool] = None,
6119    dialect: DialectType = None,
6120    copy: bool = True,
6121    **opts,
6122):
6123    """Create an Alias expression.
6124
6125    Example:
6126        >>> alias_('foo', 'bar').sql()
6127        'foo AS bar'
6128
6129        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
6130        '(SELECT 1, 2) AS bar(a, b)'
6131
6132    Args:
6133        expression: the SQL code strings to parse.
6134            If an Expression instance is passed, this is used as-is.
6135        alias: the alias name to use. If the name has
6136            special characters it is quoted.
6137        table: Whether or not to create a table alias, can also be a list of columns.
6138        quoted: whether or not to quote the alias
6139        dialect: the dialect used to parse the input expression.
6140        copy: Whether or not to copy the expression.
6141        **opts: other options to use to parse the input expressions.
6142
6143    Returns:
6144        Alias: the aliased expression
6145    """
6146    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
6147    alias = to_identifier(alias, quoted=quoted)
6148
6149    if table:
6150        table_alias = TableAlias(this=alias)
6151        exp.set("alias", table_alias)
6152
6153        if not isinstance(table, bool):
6154            for column in table:
6155                table_alias.append("columns", to_identifier(column, quoted=quoted))
6156
6157        return exp
6158
6159    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
6160    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
6161    # for the complete Window expression.
6162    #
6163    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
6164
6165    if "alias" in exp.arg_types and not isinstance(exp, Window):
6166        exp.set("alias", alias)
6167        return exp
6168    return Alias(this=exp, alias=alias)
6169
6170
6171def subquery(
6172    expression: ExpOrStr,
6173    alias: t.Optional[Identifier | str] = None,
6174    dialect: DialectType = None,
6175    **opts,
6176) -> Select:
6177    """
6178    Build a subquery expression.
6179
6180    Example:
6181        >>> subquery('select x from tbl', 'bar').select('x').sql()
6182        'SELECT x FROM (SELECT x FROM tbl) AS bar'
6183
6184    Args:
6185        expression: the SQL code strings to parse.
6186            If an Expression instance is passed, this is used as-is.
6187        alias: the alias name to use.
6188        dialect: the dialect used to parse the input expression.
6189        **opts: other options to use to parse the input expressions.
6190
6191    Returns:
6192        A new Select instance with the subquery expression included.
6193    """
6194
6195    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias)
6196    return Select().from_(expression, dialect=dialect, **opts)
6197
6198
6199def column(
6200    col: str | Identifier,
6201    table: t.Optional[str | Identifier] = None,
6202    db: t.Optional[str | Identifier] = None,
6203    catalog: t.Optional[str | Identifier] = None,
6204    quoted: t.Optional[bool] = None,
6205) -> Column:
6206    """
6207    Build a Column.
6208
6209    Args:
6210        col: Column name.
6211        table: Table name.
6212        db: Database name.
6213        catalog: Catalog name.
6214        quoted: Whether to force quotes on the column's identifiers.
6215
6216    Returns:
6217        The new Column instance.
6218    """
6219    return Column(
6220        this=to_identifier(col, quoted=quoted),
6221        table=to_identifier(table, quoted=quoted),
6222        db=to_identifier(db, quoted=quoted),
6223        catalog=to_identifier(catalog, quoted=quoted),
6224    )
6225
6226
6227def cast(expression: ExpOrStr, to: DATA_TYPE, **opts) -> Cast:
6228    """Cast an expression to a data type.
6229
6230    Example:
6231        >>> cast('x + 1', 'int').sql()
6232        'CAST(x + 1 AS INT)'
6233
6234    Args:
6235        expression: The expression to cast.
6236        to: The datatype to cast to.
6237
6238    Returns:
6239        The new Cast instance.
6240    """
6241    expression = maybe_parse(expression, **opts)
6242    data_type = DataType.build(to, **opts)
6243    expression = Cast(this=expression, to=data_type)
6244    expression.type = data_type
6245    return expression
6246
6247
6248def table_(
6249    table: Identifier | str,
6250    db: t.Optional[Identifier | str] = None,
6251    catalog: t.Optional[Identifier | str] = None,
6252    quoted: t.Optional[bool] = None,
6253    alias: t.Optional[Identifier | str] = None,
6254) -> Table:
6255    """Build a Table.
6256
6257    Args:
6258        table: Table name.
6259        db: Database name.
6260        catalog: Catalog name.
6261        quote: Whether to force quotes on the table's identifiers.
6262        alias: Table's alias.
6263
6264    Returns:
6265        The new Table instance.
6266    """
6267    return Table(
6268        this=to_identifier(table, quoted=quoted) if table else None,
6269        db=to_identifier(db, quoted=quoted) if db else None,
6270        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
6271        alias=TableAlias(this=to_identifier(alias)) if alias else None,
6272    )
6273
6274
6275def values(
6276    values: t.Iterable[t.Tuple[t.Any, ...]],
6277    alias: t.Optional[str] = None,
6278    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
6279) -> Values:
6280    """Build VALUES statement.
6281
6282    Example:
6283        >>> values([(1, '2')]).sql()
6284        "VALUES (1, '2')"
6285
6286    Args:
6287        values: values statements that will be converted to SQL
6288        alias: optional alias
6289        columns: Optional list of ordered column names or ordered dictionary of column names to types.
6290         If either are provided then an alias is also required.
6291
6292    Returns:
6293        Values: the Values expression object
6294    """
6295    if columns and not alias:
6296        raise ValueError("Alias is required when providing columns")
6297
6298    return Values(
6299        expressions=[convert(tup) for tup in values],
6300        alias=(
6301            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
6302            if columns
6303            else (TableAlias(this=to_identifier(alias)) if alias else None)
6304        ),
6305    )
6306
6307
6308def var(name: t.Optional[ExpOrStr]) -> Var:
6309    """Build a SQL variable.
6310
6311    Example:
6312        >>> repr(var('x'))
6313        '(VAR this: x)'
6314
6315        >>> repr(var(column('x', table='y')))
6316        '(VAR this: x)'
6317
6318    Args:
6319        name: The name of the var or an expression who's name will become the var.
6320
6321    Returns:
6322        The new variable node.
6323    """
6324    if not name:
6325        raise ValueError("Cannot convert empty name into var.")
6326
6327    if isinstance(name, Expression):
6328        name = name.name
6329    return Var(this=name)
6330
6331
6332def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable:
6333    """Build ALTER TABLE... RENAME... expression
6334
6335    Args:
6336        old_name: The old name of the table
6337        new_name: The new name of the table
6338
6339    Returns:
6340        Alter table expression
6341    """
6342    old_table = to_table(old_name)
6343    new_table = to_table(new_name)
6344    return AlterTable(
6345        this=old_table,
6346        actions=[
6347            RenameTable(this=new_table),
6348        ],
6349    )
6350
6351
6352def convert(value: t.Any, copy: bool = False) -> Expression:
6353    """Convert a python value into an expression object.
6354
6355    Raises an error if a conversion is not possible.
6356
6357    Args:
6358        value: A python object.
6359        copy: Whether or not to copy `value` (only applies to Expressions and collections).
6360
6361    Returns:
6362        Expression: the equivalent expression object.
6363    """
6364    if isinstance(value, Expression):
6365        return maybe_copy(value, copy)
6366    if isinstance(value, str):
6367        return Literal.string(value)
6368    if isinstance(value, bool):
6369        return Boolean(this=value)
6370    if value is None or (isinstance(value, float) and math.isnan(value)):
6371        return NULL
6372    if isinstance(value, numbers.Number):
6373        return Literal.number(value)
6374    if isinstance(value, datetime.datetime):
6375        datetime_literal = Literal.string(
6376            (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat()
6377        )
6378        return TimeStrToTime(this=datetime_literal)
6379    if isinstance(value, datetime.date):
6380        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
6381        return DateStrToDate(this=date_literal)
6382    if isinstance(value, tuple):
6383        return Tuple(expressions=[convert(v, copy=copy) for v in value])
6384    if isinstance(value, list):
6385        return Array(expressions=[convert(v, copy=copy) for v in value])
6386    if isinstance(value, dict):
6387        return Map(
6388            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
6389            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
6390        )
6391    raise ValueError(f"Cannot convert {value}")
6392
6393
6394def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
6395    """
6396    Replace children of an expression with the result of a lambda fun(child) -> exp.
6397    """
6398    for k, v in expression.args.items():
6399        is_list_arg = type(v) is list
6400
6401        child_nodes = v if is_list_arg else [v]
6402        new_child_nodes = []
6403
6404        for cn in child_nodes:
6405            if isinstance(cn, Expression):
6406                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
6407                    new_child_nodes.append(child_node)
6408                    child_node.parent = expression
6409                    child_node.arg_key = k
6410            else:
6411                new_child_nodes.append(cn)
6412
6413        expression.args[k] = new_child_nodes if is_list_arg else seq_get(new_child_nodes, 0)
6414
6415
6416def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
6417    """
6418    Return all table names referenced through columns in an expression.
6419
6420    Example:
6421        >>> import sqlglot
6422        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
6423        ['a', 'c']
6424
6425    Args:
6426        expression: expression to find table names.
6427        exclude: a table name to exclude
6428
6429    Returns:
6430        A list of unique names.
6431    """
6432    return {
6433        table
6434        for table in (column.table for column in expression.find_all(Column))
6435        if table and table != exclude
6436    }
6437
6438
6439def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
6440    """Get the full name of a table as a string.
6441
6442    Args:
6443        table: Table expression node or string.
6444        dialect: The dialect to generate the table name for.
6445        identify: Determines when an identifier should be quoted. Possible values are:
6446            False (default): Never quote, except in cases where it's mandatory by the dialect.
6447            True: Always quote.
6448
6449    Examples:
6450        >>> from sqlglot import exp, parse_one
6451        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
6452        'a.b.c'
6453
6454    Returns:
6455        The table name.
6456    """
6457
6458    table = maybe_parse(table, into=Table, dialect=dialect)
6459
6460    if not table:
6461        raise ValueError(f"Cannot parse {table}")
6462
6463    return ".".join(
6464        part.sql(dialect=dialect, identify=True)
6465        if identify or not SAFE_IDENTIFIER_RE.match(part.name)
6466        else part.name
6467        for part in table.parts
6468    )
6469
6470
6471def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
6472    """Returns a case normalized table name without quotes.
6473
6474    Args:
6475        table: the table to normalize
6476        dialect: the dialect to use for normalization rules
6477        copy: whether or not to copy the expression.
6478
6479    Examples:
6480        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
6481        'A-B.c'
6482    """
6483    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
6484
6485    return ".".join(
6486        p.name
6487        for p in normalize_identifiers(
6488            to_table(table, dialect=dialect, copy=copy), dialect=dialect
6489        ).parts
6490    )
6491
6492
6493def replace_tables(
6494    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
6495) -> E:
6496    """Replace all tables in expression according to the mapping.
6497
6498    Args:
6499        expression: expression node to be transformed and replaced.
6500        mapping: mapping of table names.
6501        dialect: the dialect of the mapping table
6502        copy: whether or not to copy the expression.
6503
6504    Examples:
6505        >>> from sqlglot import exp, parse_one
6506        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
6507        'SELECT * FROM c /* a.b */'
6508
6509    Returns:
6510        The mapped expression.
6511    """
6512
6513    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
6514
6515    def _replace_tables(node: Expression) -> Expression:
6516        if isinstance(node, Table):
6517            original = normalize_table_name(node, dialect=dialect)
6518            new_name = mapping.get(original)
6519
6520            if new_name:
6521                table = to_table(
6522                    new_name,
6523                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
6524                )
6525                table.add_comments([original])
6526                return table
6527        return node
6528
6529    return expression.transform(_replace_tables, copy=copy)
6530
6531
6532def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
6533    """Replace placeholders in an expression.
6534
6535    Args:
6536        expression: expression node to be transformed and replaced.
6537        args: positional names that will substitute unnamed placeholders in the given order.
6538        kwargs: keyword arguments that will substitute named placeholders.
6539
6540    Examples:
6541        >>> from sqlglot import exp, parse_one
6542        >>> replace_placeholders(
6543        ...     parse_one("select * from :tbl where ? = ?"),
6544        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
6545        ... ).sql()
6546        "SELECT * FROM foo WHERE str_col = 'b'"
6547
6548    Returns:
6549        The mapped expression.
6550    """
6551
6552    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
6553        if isinstance(node, Placeholder):
6554            if node.name:
6555                new_name = kwargs.get(node.name)
6556                if new_name:
6557                    return convert(new_name)
6558            else:
6559                try:
6560                    return convert(next(args))
6561                except StopIteration:
6562                    pass
6563        return node
6564
6565    return expression.transform(_replace_placeholders, iter(args), **kwargs)
6566
6567
6568def expand(
6569    expression: Expression,
6570    sources: t.Dict[str, Subqueryable],
6571    dialect: DialectType = None,
6572    copy: bool = True,
6573) -> Expression:
6574    """Transforms an expression by expanding all referenced sources into subqueries.
6575
6576    Examples:
6577        >>> from sqlglot import parse_one
6578        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
6579        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
6580
6581        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
6582        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
6583
6584    Args:
6585        expression: The expression to expand.
6586        sources: A dictionary of name to Subqueryables.
6587        dialect: The dialect of the sources dict.
6588        copy: Whether or not to copy the expression during transformation. Defaults to True.
6589
6590    Returns:
6591        The transformed expression.
6592    """
6593    sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
6594
6595    def _expand(node: Expression):
6596        if isinstance(node, Table):
6597            name = normalize_table_name(node, dialect=dialect)
6598            source = sources.get(name)
6599            if source:
6600                subquery = source.subquery(node.alias or name)
6601                subquery.comments = [f"source: {name}"]
6602                return subquery.transform(_expand, copy=False)
6603        return node
6604
6605    return expression.transform(_expand, copy=copy)
6606
6607
6608def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
6609    """
6610    Returns a Func expression.
6611
6612    Examples:
6613        >>> func("abs", 5).sql()
6614        'ABS(5)'
6615
6616        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
6617        'CAST(5 AS DOUBLE)'
6618
6619    Args:
6620        name: the name of the function to build.
6621        args: the args used to instantiate the function of interest.
6622        copy: whether or not to copy the argument expressions.
6623        dialect: the source dialect.
6624        kwargs: the kwargs used to instantiate the function of interest.
6625
6626    Note:
6627        The arguments `args` and `kwargs` are mutually exclusive.
6628
6629    Returns:
6630        An instance of the function of interest, or an anonymous function, if `name` doesn't
6631        correspond to an existing `sqlglot.expressions.Func` class.
6632    """
6633    if args and kwargs:
6634        raise ValueError("Can't use both args and kwargs to instantiate a function.")
6635
6636    from sqlglot.dialects.dialect import Dialect
6637
6638    dialect = Dialect.get_or_raise(dialect)
6639
6640    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
6641    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
6642
6643    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
6644    if constructor:
6645        if converted:
6646            if "dialect" in constructor.__code__.co_varnames:
6647                function = constructor(converted, dialect=dialect)
6648            else:
6649                function = constructor(converted)
6650        elif constructor.__name__ == "from_arg_list":
6651            function = constructor.__self__(**kwargs)  # type: ignore
6652        else:
6653            constructor = FUNCTION_BY_NAME.get(name.upper())
6654            if constructor:
6655                function = constructor(**kwargs)
6656            else:
6657                raise ValueError(
6658                    f"Unable to convert '{name}' into a Func. Either manually construct "
6659                    "the Func expression of interest or parse the function call."
6660                )
6661    else:
6662        kwargs = kwargs or {"expressions": converted}
6663        function = Anonymous(this=name, **kwargs)
6664
6665    for error_message in function.error_messages(converted):
6666        raise ValueError(error_message)
6667
6668    return function
6669
6670
6671def case(
6672    expression: t.Optional[ExpOrStr] = None,
6673    **opts,
6674) -> Case:
6675    """
6676    Initialize a CASE statement.
6677
6678    Example:
6679        case().when("a = 1", "foo").else_("bar")
6680
6681    Args:
6682        expression: Optionally, the input expression (not all dialects support this)
6683        **opts: Extra keyword arguments for parsing `expression`
6684    """
6685    if expression is not None:
6686        this = maybe_parse(expression, **opts)
6687    else:
6688        this = None
6689    return Case(this=this, ifs=[])
6690
6691
6692def cast_unless(
6693    expression: ExpOrStr,
6694    to: DATA_TYPE,
6695    *types: DATA_TYPE,
6696    **opts: t.Any,
6697) -> Expression | Cast:
6698    """
6699    Cast an expression to a data type unless it is a specified type.
6700
6701    Args:
6702        expression: The expression to cast.
6703        to: The data type to cast to.
6704        **types: The types to exclude from casting.
6705        **opts: Extra keyword arguments for parsing `expression`
6706    """
6707    expr = maybe_parse(expression, **opts)
6708    if expr.is_type(*types):
6709        return expr
6710    return cast(expr, to, **opts)
6711
6712
6713def true() -> Boolean:
6714    """
6715    Returns a true Boolean expression.
6716    """
6717    return Boolean(this=True)
6718
6719
6720def false() -> Boolean:
6721    """
6722    Returns a false Boolean expression.
6723    """
6724    return Boolean(this=False)
6725
6726
6727def null() -> Null:
6728    """
6729    Returns a Null expression.
6730    """
6731    return Null()
6732
6733
6734# TODO: deprecate this
6735TRUE = Boolean(this=True)
6736FALSE = Boolean(this=False)
6737NULL = Null()
SQLGLOT_META = 'sqlglot.meta'
TABLE_PARTS = ('this', 'db', 'catalog')
class Expression:
 60class Expression(metaclass=_Expression):
 61    """
 62    The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary
 63    context, such as its child expressions, their names (arg keys), and whether a given child expression
 64    is optional or not.
 65
 66    Attributes:
 67        key: a unique key for each class in the Expression hierarchy. This is useful for hashing
 68            and representing expressions as strings.
 69        arg_types: determines what arguments (child nodes) are supported by an expression. It
 70            maps arg keys to booleans that indicate whether the corresponding args are optional.
 71        parent: a reference to the parent expression (or None, in case of root expressions).
 72        arg_key: the arg key an expression is associated with, i.e. the name its parent expression
 73            uses to refer to it.
 74        comments: a list of comments that are associated with a given expression. This is used in
 75            order to preserve comments when transpiling SQL code.
 76        type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the
 77            optimizer, in order to enable some transformations that require type information.
 78        meta: a dictionary that can be used to store useful metadata for a given expression.
 79
 80    Example:
 81        >>> class Foo(Expression):
 82        ...     arg_types = {"this": True, "expression": False}
 83
 84        The above definition informs us that Foo is an Expression that requires an argument called
 85        "this" and may also optionally receive an argument called "expression".
 86
 87    Args:
 88        args: a mapping used for retrieving the arguments of an expression, given their arg keys.
 89    """
 90
 91    key = "expression"
 92    arg_types = {"this": True}
 93    __slots__ = ("args", "parent", "arg_key", "comments", "_type", "_meta", "_hash")
 94
 95    def __init__(self, **args: t.Any):
 96        self.args: t.Dict[str, t.Any] = args
 97        self.parent: t.Optional[Expression] = None
 98        self.arg_key: t.Optional[str] = None
 99        self.comments: t.Optional[t.List[str]] = None
100        self._type: t.Optional[DataType] = None
101        self._meta: t.Optional[t.Dict[str, t.Any]] = None
102        self._hash: t.Optional[int] = None
103
104        for arg_key, value in self.args.items():
105            self._set_parent(arg_key, value)
106
107    def __eq__(self, other) -> bool:
108        return type(self) is type(other) and hash(self) == hash(other)
109
110    @property
111    def hashable_args(self) -> t.Any:
112        return frozenset(
113            (k, tuple(_norm_arg(a) for a in v) if type(v) is list else _norm_arg(v))
114            for k, v in self.args.items()
115            if not (v is None or v is False or (type(v) is list and not v))
116        )
117
118    def __hash__(self) -> int:
119        if self._hash is not None:
120            return self._hash
121
122        return hash((self.__class__, self.hashable_args))
123
124    @property
125    def this(self) -> t.Any:
126        """
127        Retrieves the argument with key "this".
128        """
129        return self.args.get("this")
130
131    @property
132    def expression(self) -> t.Any:
133        """
134        Retrieves the argument with key "expression".
135        """
136        return self.args.get("expression")
137
138    @property
139    def expressions(self) -> t.List[t.Any]:
140        """
141        Retrieves the argument with key "expressions".
142        """
143        return self.args.get("expressions") or []
144
145    def text(self, key) -> str:
146        """
147        Returns a textual representation of the argument corresponding to "key". This can only be used
148        for args that are strings or leaf Expression instances, such as identifiers and literals.
149        """
150        field = self.args.get(key)
151        if isinstance(field, str):
152            return field
153        if isinstance(field, (Identifier, Literal, Var)):
154            return field.this
155        if isinstance(field, (Star, Null)):
156            return field.name
157        return ""
158
159    @property
160    def is_string(self) -> bool:
161        """
162        Checks whether a Literal expression is a string.
163        """
164        return isinstance(self, Literal) and self.args["is_string"]
165
166    @property
167    def is_number(self) -> bool:
168        """
169        Checks whether a Literal expression is a number.
170        """
171        return isinstance(self, Literal) and not self.args["is_string"]
172
173    @property
174    def is_int(self) -> bool:
175        """
176        Checks whether a Literal expression is an integer.
177        """
178        if self.is_number:
179            try:
180                int(self.name)
181                return True
182            except ValueError:
183                pass
184        return False
185
186    @property
187    def is_star(self) -> bool:
188        """Checks whether an expression is a star."""
189        return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star))
190
191    @property
192    def alias(self) -> str:
193        """
194        Returns the alias of the expression, or an empty string if it's not aliased.
195        """
196        if isinstance(self.args.get("alias"), TableAlias):
197            return self.args["alias"].name
198        return self.text("alias")
199
200    @property
201    def alias_column_names(self) -> t.List[str]:
202        table_alias = self.args.get("alias")
203        if not table_alias:
204            return []
205        return [c.name for c in table_alias.args.get("columns") or []]
206
207    @property
208    def name(self) -> str:
209        return self.text("this")
210
211    @property
212    def alias_or_name(self) -> str:
213        return self.alias or self.name
214
215    @property
216    def output_name(self) -> str:
217        """
218        Name of the output column if this expression is a selection.
219
220        If the Expression has no output name, an empty string is returned.
221
222        Example:
223            >>> from sqlglot import parse_one
224            >>> parse_one("SELECT a").expressions[0].output_name
225            'a'
226            >>> parse_one("SELECT b AS c").expressions[0].output_name
227            'c'
228            >>> parse_one("SELECT 1 + 2").expressions[0].output_name
229            ''
230        """
231        return ""
232
233    @property
234    def type(self) -> t.Optional[DataType]:
235        return self._type
236
237    @type.setter
238    def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None:
239        if dtype and not isinstance(dtype, DataType):
240            dtype = DataType.build(dtype)
241        self._type = dtype  # type: ignore
242
243    def is_type(self, *dtypes) -> bool:
244        return self.type is not None and self.type.is_type(*dtypes)
245
246    @property
247    def meta(self) -> t.Dict[str, t.Any]:
248        if self._meta is None:
249            self._meta = {}
250        return self._meta
251
252    def __deepcopy__(self, memo):
253        copy = self.__class__(**deepcopy(self.args))
254        if self.comments is not None:
255            copy.comments = deepcopy(self.comments)
256
257        if self._type is not None:
258            copy._type = self._type.copy()
259
260        if self._meta is not None:
261            copy._meta = deepcopy(self._meta)
262
263        return copy
264
265    def copy(self):
266        """
267        Returns a deep copy of the expression.
268        """
269        new = deepcopy(self)
270        new.parent = self.parent
271        return new
272
273    def add_comments(self, comments: t.Optional[t.List[str]]) -> None:
274        if self.comments is None:
275            self.comments = []
276        if comments:
277            for comment in comments:
278                _, *meta = comment.split(SQLGLOT_META)
279                if meta:
280                    for kv in "".join(meta).split(","):
281                        k, *v = kv.split("=")
282                        value = v[0].strip() if v else True
283                        self.meta[k.strip()] = value
284                self.comments.append(comment)
285
286    def append(self, arg_key: str, value: t.Any) -> None:
287        """
288        Appends value to arg_key if it's a list or sets it as a new list.
289
290        Args:
291            arg_key (str): name of the list expression arg
292            value (Any): value to append to the list
293        """
294        if not isinstance(self.args.get(arg_key), list):
295            self.args[arg_key] = []
296        self.args[arg_key].append(value)
297        self._set_parent(arg_key, value)
298
299    def set(self, arg_key: str, value: t.Any) -> None:
300        """
301        Sets arg_key to value.
302
303        Args:
304            arg_key: name of the expression arg.
305            value: value to set the arg to.
306        """
307        if value is None:
308            self.args.pop(arg_key, None)
309            return
310
311        self.args[arg_key] = value
312        self._set_parent(arg_key, value)
313
314    def _set_parent(self, arg_key: str, value: t.Any) -> None:
315        if hasattr(value, "parent"):
316            value.parent = self
317            value.arg_key = arg_key
318        elif type(value) is list:
319            for v in value:
320                if hasattr(v, "parent"):
321                    v.parent = self
322                    v.arg_key = arg_key
323
324    @property
325    def depth(self) -> int:
326        """
327        Returns the depth of this tree.
328        """
329        if self.parent:
330            return self.parent.depth + 1
331        return 0
332
333    def iter_expressions(self) -> t.Iterator[t.Tuple[str, Expression]]:
334        """Yields the key and expression for all arguments, exploding list args."""
335        for k, vs in self.args.items():
336            if type(vs) is list:
337                for v in vs:
338                    if hasattr(v, "parent"):
339                        yield k, v
340            else:
341                if hasattr(vs, "parent"):
342                    yield k, vs
343
344    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
345        """
346        Returns the first node in this tree which matches at least one of
347        the specified types.
348
349        Args:
350            expression_types: the expression type(s) to match.
351            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
352
353        Returns:
354            The node which matches the criteria or None if no such node was found.
355        """
356        return next(self.find_all(*expression_types, bfs=bfs), None)
357
358    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
359        """
360        Returns a generator object which visits all nodes in this tree and only
361        yields those that match at least one of the specified expression types.
362
363        Args:
364            expression_types: the expression type(s) to match.
365            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
366
367        Returns:
368            The generator object.
369        """
370        for expression, *_ in self.walk(bfs=bfs):
371            if isinstance(expression, expression_types):
372                yield expression
373
374    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
375        """
376        Returns a nearest parent matching expression_types.
377
378        Args:
379            expression_types: the expression type(s) to match.
380
381        Returns:
382            The parent node.
383        """
384        ancestor = self.parent
385        while ancestor and not isinstance(ancestor, expression_types):
386            ancestor = ancestor.parent
387        return t.cast(E, ancestor)
388
389    @property
390    def parent_select(self) -> t.Optional[Select]:
391        """
392        Returns the parent select statement.
393        """
394        return self.find_ancestor(Select)
395
396    @property
397    def same_parent(self) -> bool:
398        """Returns if the parent is the same class as itself."""
399        return type(self.parent) is self.__class__
400
401    def root(self) -> Expression:
402        """
403        Returns the root expression of this tree.
404        """
405        expression = self
406        while expression.parent:
407            expression = expression.parent
408        return expression
409
410    def walk(self, bfs=True, prune=None):
411        """
412        Returns a generator object which visits all nodes in this tree.
413
414        Args:
415            bfs (bool): if set to True the BFS traversal order will be applied,
416                otherwise the DFS traversal will be used instead.
417            prune ((node, parent, arg_key) -> bool): callable that returns True if
418                the generator should stop traversing this branch of the tree.
419
420        Returns:
421            the generator object.
422        """
423        if bfs:
424            yield from self.bfs(prune=prune)
425        else:
426            yield from self.dfs(prune=prune)
427
428    def dfs(self, parent=None, key=None, prune=None):
429        """
430        Returns a generator object which visits all nodes in this tree in
431        the DFS (Depth-first) order.
432
433        Returns:
434            The generator object.
435        """
436        parent = parent or self.parent
437        yield self, parent, key
438        if prune and prune(self, parent, key):
439            return
440
441        for k, v in self.iter_expressions():
442            yield from v.dfs(self, k, prune)
443
444    def bfs(self, prune=None):
445        """
446        Returns a generator object which visits all nodes in this tree in
447        the BFS (Breadth-first) order.
448
449        Returns:
450            The generator object.
451        """
452        queue = deque([(self, self.parent, None)])
453
454        while queue:
455            item, parent, key = queue.popleft()
456
457            yield item, parent, key
458            if prune and prune(item, parent, key):
459                continue
460
461            for k, v in item.iter_expressions():
462                queue.append((v, item, k))
463
464    def unnest(self):
465        """
466        Returns the first non parenthesis child or self.
467        """
468        expression = self
469        while type(expression) is Paren:
470            expression = expression.this
471        return expression
472
473    def unalias(self):
474        """
475        Returns the inner expression if this is an Alias.
476        """
477        if isinstance(self, Alias):
478            return self.this
479        return self
480
481    def unnest_operands(self):
482        """
483        Returns unnested operands as a tuple.
484        """
485        return tuple(arg.unnest() for _, arg in self.iter_expressions())
486
487    def flatten(self, unnest=True):
488        """
489        Returns a generator which yields child nodes whose parents are the same class.
490
491        A AND B AND C -> [A, B, C]
492        """
493        for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__):
494            if not type(node) is self.__class__:
495                yield node.unnest() if unnest and not isinstance(node, Subquery) else node
496
497    def __str__(self) -> str:
498        return self.sql()
499
500    def __repr__(self) -> str:
501        return self._to_s()
502
503    def sql(self, dialect: DialectType = None, **opts) -> str:
504        """
505        Returns SQL string representation of this tree.
506
507        Args:
508            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
509            opts: other `sqlglot.generator.Generator` options.
510
511        Returns:
512            The SQL string.
513        """
514        from sqlglot.dialects import Dialect
515
516        return Dialect.get_or_raise(dialect).generate(self, **opts)
517
518    def _to_s(self, hide_missing: bool = True, level: int = 0) -> str:
519        indent = "" if not level else "\n"
520        indent += "".join(["  "] * level)
521        left = f"({self.key.upper()} "
522
523        args: t.Dict[str, t.Any] = {
524            k: ", ".join(
525                v._to_s(hide_missing=hide_missing, level=level + 1)
526                if hasattr(v, "_to_s")
527                else str(v)
528                for v in ensure_list(vs)
529                if v is not None
530            )
531            for k, vs in self.args.items()
532        }
533        args["comments"] = self.comments
534        args["type"] = self.type
535        args = {k: v for k, v in args.items() if v or not hide_missing}
536
537        right = ", ".join(f"{k}: {v}" for k, v in args.items())
538        right += ")"
539
540        return indent + left + right
541
542    def transform(self, fun, *args, copy=True, **kwargs):
543        """
544        Recursively visits all tree nodes (excluding already transformed ones)
545        and applies the given transformation function to each node.
546
547        Args:
548            fun (function): a function which takes a node as an argument and returns a
549                new transformed node or the same node without modifications. If the function
550                returns None, then the corresponding node will be removed from the syntax tree.
551            copy (bool): if set to True a new tree instance is constructed, otherwise the tree is
552                modified in place.
553
554        Returns:
555            The transformed tree.
556        """
557        node = self.copy() if copy else self
558        new_node = fun(node, *args, **kwargs)
559
560        if new_node is None or not isinstance(new_node, Expression):
561            return new_node
562        if new_node is not node:
563            new_node.parent = node.parent
564            return new_node
565
566        replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs))
567        return new_node
568
569    @t.overload
570    def replace(self, expression: E) -> E:
571        ...
572
573    @t.overload
574    def replace(self, expression: None) -> None:
575        ...
576
577    def replace(self, expression):
578        """
579        Swap out this expression with a new expression.
580
581        For example::
582
583            >>> tree = Select().select("x").from_("tbl")
584            >>> tree.find(Column).replace(Column(this="y"))
585            (COLUMN this: y)
586            >>> tree.sql()
587            'SELECT y FROM tbl'
588
589        Args:
590            expression: new node
591
592        Returns:
593            The new expression or expressions.
594        """
595        if not self.parent:
596            return expression
597
598        parent = self.parent
599        self.parent = None
600
601        replace_children(parent, lambda child: expression if child is self else child)
602        return expression
603
604    def pop(self: E) -> E:
605        """
606        Remove this expression from its AST.
607
608        Returns:
609            The popped expression.
610        """
611        self.replace(None)
612        return self
613
614    def assert_is(self, type_: t.Type[E]) -> E:
615        """
616        Assert that this `Expression` is an instance of `type_`.
617
618        If it is NOT an instance of `type_`, this raises an assertion error.
619        Otherwise, this returns this expression.
620
621        Examples:
622            This is useful for type security in chained expressions:
623
624            >>> import sqlglot
625            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
626            'SELECT x, z FROM y'
627        """
628        assert isinstance(self, type_)
629        return self
630
631    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
632        """
633        Checks if this expression is valid (e.g. all mandatory args are set).
634
635        Args:
636            args: a sequence of values that were used to instantiate a Func expression. This is used
637                to check that the provided arguments don't exceed the function argument limit.
638
639        Returns:
640            A list of error messages for all possible errors that were found.
641        """
642        errors: t.List[str] = []
643
644        for k in self.args:
645            if k not in self.arg_types:
646                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
647        for k, mandatory in self.arg_types.items():
648            v = self.args.get(k)
649            if mandatory and (v is None or (isinstance(v, list) and not v)):
650                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
651
652        if (
653            args
654            and isinstance(self, Func)
655            and len(args) > len(self.arg_types)
656            and not self.is_var_len_args
657        ):
658            errors.append(
659                f"The number of provided arguments ({len(args)}) is greater than "
660                f"the maximum number of supported arguments ({len(self.arg_types)})"
661            )
662
663        return errors
664
665    def dump(self):
666        """
667        Dump this Expression to a JSON-serializable dict.
668        """
669        from sqlglot.serde import dump
670
671        return dump(self)
672
673    @classmethod
674    def load(cls, obj):
675        """
676        Load a dict (as returned by `Expression.dump`) into an Expression instance.
677        """
678        from sqlglot.serde import load
679
680        return load(obj)
681
682    def and_(
683        self,
684        *expressions: t.Optional[ExpOrStr],
685        dialect: DialectType = None,
686        copy: bool = True,
687        **opts,
688    ) -> Condition:
689        """
690        AND this condition with one or multiple expressions.
691
692        Example:
693            >>> condition("x=1").and_("y=1").sql()
694            'x = 1 AND y = 1'
695
696        Args:
697            *expressions: the SQL code strings to parse.
698                If an `Expression` instance is passed, it will be used as-is.
699            dialect: the dialect used to parse the input expression.
700            copy: whether or not to copy the involved expressions (only applies to Expressions).
701            opts: other options to use to parse the input expressions.
702
703        Returns:
704            The new And condition.
705        """
706        return and_(self, *expressions, dialect=dialect, copy=copy, **opts)
707
708    def or_(
709        self,
710        *expressions: t.Optional[ExpOrStr],
711        dialect: DialectType = None,
712        copy: bool = True,
713        **opts,
714    ) -> Condition:
715        """
716        OR this condition with one or multiple expressions.
717
718        Example:
719            >>> condition("x=1").or_("y=1").sql()
720            'x = 1 OR y = 1'
721
722        Args:
723            *expressions: the SQL code strings to parse.
724                If an `Expression` instance is passed, it will be used as-is.
725            dialect: the dialect used to parse the input expression.
726            copy: whether or not to copy the involved expressions (only applies to Expressions).
727            opts: other options to use to parse the input expressions.
728
729        Returns:
730            The new Or condition.
731        """
732        return or_(self, *expressions, dialect=dialect, copy=copy, **opts)
733
734    def not_(self, copy: bool = True):
735        """
736        Wrap this condition with NOT.
737
738        Example:
739            >>> condition("x=1").not_().sql()
740            'NOT x = 1'
741
742        Args:
743            copy: whether or not to copy this object.
744
745        Returns:
746            The new Not instance.
747        """
748        return not_(self, copy=copy)
749
750    def as_(
751        self,
752        alias: str | Identifier,
753        quoted: t.Optional[bool] = None,
754        dialect: DialectType = None,
755        copy: bool = True,
756        **opts,
757    ) -> Alias:
758        return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts)
759
760    def _binop(self, klass: t.Type[E], other: t.Any, reverse: bool = False) -> E:
761        this = self.copy()
762        other = convert(other, copy=True)
763        if not isinstance(this, klass) and not isinstance(other, klass):
764            this = _wrap(this, Binary)
765            other = _wrap(other, Binary)
766        if reverse:
767            return klass(this=other, expression=this)
768        return klass(this=this, expression=other)
769
770    def __getitem__(self, other: ExpOrStr | t.Tuple[ExpOrStr]) -> Bracket:
771        return Bracket(
772            this=self.copy(), expressions=[convert(e, copy=True) for e in ensure_list(other)]
773        )
774
775    def __iter__(self) -> t.Iterator:
776        if "expressions" in self.arg_types:
777            return iter(self.args.get("expressions") or [])
778        # We define this because __getitem__ converts Expression into an iterable, which is
779        # problematic because one can hit infinite loops if they do "for x in some_expr: ..."
780        # See: https://peps.python.org/pep-0234/
781        raise TypeError(f"'{self.__class__.__name__}' object is not iterable")
782
783    def isin(
784        self,
785        *expressions: t.Any,
786        query: t.Optional[ExpOrStr] = None,
787        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
788        copy: bool = True,
789        **opts,
790    ) -> In:
791        return In(
792            this=maybe_copy(self, copy),
793            expressions=[convert(e, copy=copy) for e in expressions],
794            query=maybe_parse(query, copy=copy, **opts) if query else None,
795            unnest=Unnest(
796                expressions=[
797                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
798                ]
799            )
800            if unnest
801            else None,
802        )
803
804    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
805        return Between(
806            this=maybe_copy(self, copy),
807            low=convert(low, copy=copy, **opts),
808            high=convert(high, copy=copy, **opts),
809        )
810
811    def is_(self, other: ExpOrStr) -> Is:
812        return self._binop(Is, other)
813
814    def like(self, other: ExpOrStr) -> Like:
815        return self._binop(Like, other)
816
817    def ilike(self, other: ExpOrStr) -> ILike:
818        return self._binop(ILike, other)
819
820    def eq(self, other: t.Any) -> EQ:
821        return self._binop(EQ, other)
822
823    def neq(self, other: t.Any) -> NEQ:
824        return self._binop(NEQ, other)
825
826    def rlike(self, other: ExpOrStr) -> RegexpLike:
827        return self._binop(RegexpLike, other)
828
829    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
830        div = self._binop(Div, other)
831        div.args["typed"] = typed
832        div.args["safe"] = safe
833        return div
834
835    def __lt__(self, other: t.Any) -> LT:
836        return self._binop(LT, other)
837
838    def __le__(self, other: t.Any) -> LTE:
839        return self._binop(LTE, other)
840
841    def __gt__(self, other: t.Any) -> GT:
842        return self._binop(GT, other)
843
844    def __ge__(self, other: t.Any) -> GTE:
845        return self._binop(GTE, other)
846
847    def __add__(self, other: t.Any) -> Add:
848        return self._binop(Add, other)
849
850    def __radd__(self, other: t.Any) -> Add:
851        return self._binop(Add, other, reverse=True)
852
853    def __sub__(self, other: t.Any) -> Sub:
854        return self._binop(Sub, other)
855
856    def __rsub__(self, other: t.Any) -> Sub:
857        return self._binop(Sub, other, reverse=True)
858
859    def __mul__(self, other: t.Any) -> Mul:
860        return self._binop(Mul, other)
861
862    def __rmul__(self, other: t.Any) -> Mul:
863        return self._binop(Mul, other, reverse=True)
864
865    def __truediv__(self, other: t.Any) -> Div:
866        return self._binop(Div, other)
867
868    def __rtruediv__(self, other: t.Any) -> Div:
869        return self._binop(Div, other, reverse=True)
870
871    def __floordiv__(self, other: t.Any) -> IntDiv:
872        return self._binop(IntDiv, other)
873
874    def __rfloordiv__(self, other: t.Any) -> IntDiv:
875        return self._binop(IntDiv, other, reverse=True)
876
877    def __mod__(self, other: t.Any) -> Mod:
878        return self._binop(Mod, other)
879
880    def __rmod__(self, other: t.Any) -> Mod:
881        return self._binop(Mod, other, reverse=True)
882
883    def __pow__(self, other: t.Any) -> Pow:
884        return self._binop(Pow, other)
885
886    def __rpow__(self, other: t.Any) -> Pow:
887        return self._binop(Pow, other, reverse=True)
888
889    def __and__(self, other: t.Any) -> And:
890        return self._binop(And, other)
891
892    def __rand__(self, other: t.Any) -> And:
893        return self._binop(And, other, reverse=True)
894
895    def __or__(self, other: t.Any) -> Or:
896        return self._binop(Or, other)
897
898    def __ror__(self, other: t.Any) -> Or:
899        return self._binop(Or, other, reverse=True)
900
901    def __neg__(self) -> Neg:
902        return Neg(this=_wrap(self.copy(), Binary))
903
904    def __invert__(self) -> Not:
905        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 what arguments (child nodes) are 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.
  • 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 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)
 95    def __init__(self, **args: t.Any):
 96        self.args: t.Dict[str, t.Any] = args
 97        self.parent: t.Optional[Expression] = None
 98        self.arg_key: t.Optional[str] = None
 99        self.comments: t.Optional[t.List[str]] = None
100        self._type: t.Optional[DataType] = None
101        self._meta: t.Optional[t.Dict[str, t.Any]] = None
102        self._hash: t.Optional[int] = None
103
104        for arg_key, value in self.args.items():
105            self._set_parent(arg_key, value)
key = 'expression'
arg_types = {'this': True}
args: Dict[str, Any]
parent: Optional[Expression]
arg_key: Optional[str]
comments: Optional[List[str]]
hashable_args: Any
this: Any

Retrieves the argument with key "this".

expression: Any

Retrieves the argument with key "expression".

expressions: List[Any]

Retrieves the argument with key "expressions".

def text(self, key) -> str:
145    def text(self, key) -> str:
146        """
147        Returns a textual representation of the argument corresponding to "key". This can only be used
148        for args that are strings or leaf Expression instances, such as identifiers and literals.
149        """
150        field = self.args.get(key)
151        if isinstance(field, str):
152            return field
153        if isinstance(field, (Identifier, Literal, Var)):
154            return field.this
155        if isinstance(field, (Star, Null)):
156            return field.name
157        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

Checks whether a Literal expression is a string.

is_number: bool

Checks whether a Literal expression is a number.

is_int: bool

Checks whether a Literal expression is an integer.

is_star: bool

Checks whether an expression is a star.

alias: str

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

alias_column_names: List[str]
name: str
alias_or_name: str
output_name: str

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]
def is_type(self, *dtypes) -> bool:
243    def is_type(self, *dtypes) -> bool:
244        return self.type is not None and self.type.is_type(*dtypes)
meta: Dict[str, Any]
def copy(self):
265    def copy(self):
266        """
267        Returns a deep copy of the expression.
268        """
269        new = deepcopy(self)
270        new.parent = self.parent
271        return new

Returns a deep copy of the expression.

def add_comments(self, comments: Optional[List[str]]) -> None:
273    def add_comments(self, comments: t.Optional[t.List[str]]) -> None:
274        if self.comments is None:
275            self.comments = []
276        if comments:
277            for comment in comments:
278                _, *meta = comment.split(SQLGLOT_META)
279                if meta:
280                    for kv in "".join(meta).split(","):
281                        k, *v = kv.split("=")
282                        value = v[0].strip() if v else True
283                        self.meta[k.strip()] = value
284                self.comments.append(comment)
def append(self, arg_key: str, value: Any) -> None:
286    def append(self, arg_key: str, value: t.Any) -> None:
287        """
288        Appends value to arg_key if it's a list or sets it as a new list.
289
290        Args:
291            arg_key (str): name of the list expression arg
292            value (Any): value to append to the list
293        """
294        if not isinstance(self.args.get(arg_key), list):
295            self.args[arg_key] = []
296        self.args[arg_key].append(value)
297        self._set_parent(arg_key, 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) -> None:
299    def set(self, arg_key: str, value: t.Any) -> None:
300        """
301        Sets arg_key to value.
302
303        Args:
304            arg_key: name of the expression arg.
305            value: value to set the arg to.
306        """
307        if value is None:
308            self.args.pop(arg_key, None)
309            return
310
311        self.args[arg_key] = value
312        self._set_parent(arg_key, value)

Sets arg_key to value.

Arguments:
  • arg_key: name of the expression arg.
  • value: value to set the arg to.
depth: int

Returns the depth of this tree.

def iter_expressions(self) -> Iterator[Tuple[str, Expression]]:
333    def iter_expressions(self) -> t.Iterator[t.Tuple[str, Expression]]:
334        """Yields the key and expression for all arguments, exploding list args."""
335        for k, vs in self.args.items():
336            if type(vs) is list:
337                for v in vs:
338                    if hasattr(v, "parent"):
339                        yield k, v
340            else:
341                if hasattr(vs, "parent"):
342                    yield k, vs

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

def find(self, *expression_types: Type[~E], bfs: bool = True) -> Optional[~E]:
344    def find(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Optional[E]:
345        """
346        Returns the first node in this tree which matches at least one of
347        the specified types.
348
349        Args:
350            expression_types: the expression type(s) to match.
351            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
352
353        Returns:
354            The node which matches the criteria or None if no such node was found.
355        """
356        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]:
358    def find_all(self, *expression_types: t.Type[E], bfs: bool = True) -> t.Iterator[E]:
359        """
360        Returns a generator object which visits all nodes in this tree and only
361        yields those that match at least one of the specified expression types.
362
363        Args:
364            expression_types: the expression type(s) to match.
365            bfs: whether to search the AST using the BFS algorithm (DFS is used if false).
366
367        Returns:
368            The generator object.
369        """
370        for expression, *_ in self.walk(bfs=bfs):
371            if isinstance(expression, expression_types):
372                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]:
374    def find_ancestor(self, *expression_types: t.Type[E]) -> t.Optional[E]:
375        """
376        Returns a nearest parent matching expression_types.
377
378        Args:
379            expression_types: the expression type(s) to match.
380
381        Returns:
382            The parent node.
383        """
384        ancestor = self.parent
385        while ancestor and not isinstance(ancestor, expression_types):
386            ancestor = ancestor.parent
387        return t.cast(E, ancestor)

Returns a nearest parent matching expression_types.

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

The parent node.

parent_select: Optional[Select]

Returns the parent select statement.

same_parent: bool

Returns if the parent is the same class as itself.

def root(self) -> Expression:
401    def root(self) -> Expression:
402        """
403        Returns the root expression of this tree.
404        """
405        expression = self
406        while expression.parent:
407            expression = expression.parent
408        return expression

Returns the root expression of this tree.

def walk(self, bfs=True, prune=None):
410    def walk(self, bfs=True, prune=None):
411        """
412        Returns a generator object which visits all nodes in this tree.
413
414        Args:
415            bfs (bool): if set to True the BFS traversal order will be applied,
416                otherwise the DFS traversal will be used instead.
417            prune ((node, parent, arg_key) -> bool): callable that returns True if
418                the generator should stop traversing this branch of the tree.
419
420        Returns:
421            the generator object.
422        """
423        if bfs:
424            yield from self.bfs(prune=prune)
425        else:
426            yield from self.dfs(prune=prune)

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

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

the generator object.

def dfs(self, parent=None, key=None, prune=None):
428    def dfs(self, parent=None, key=None, prune=None):
429        """
430        Returns a generator object which visits all nodes in this tree in
431        the DFS (Depth-first) order.
432
433        Returns:
434            The generator object.
435        """
436        parent = parent or self.parent
437        yield self, parent, key
438        if prune and prune(self, parent, key):
439            return
440
441        for k, v in self.iter_expressions():
442            yield from v.dfs(self, k, prune)

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=None):
444    def bfs(self, prune=None):
445        """
446        Returns a generator object which visits all nodes in this tree in
447        the BFS (Breadth-first) order.
448
449        Returns:
450            The generator object.
451        """
452        queue = deque([(self, self.parent, None)])
453
454        while queue:
455            item, parent, key = queue.popleft()
456
457            yield item, parent, key
458            if prune and prune(item, parent, key):
459                continue
460
461            for k, v in item.iter_expressions():
462                queue.append((v, item, k))

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

Returns:

The generator object.

def unnest(self):
464    def unnest(self):
465        """
466        Returns the first non parenthesis child or self.
467        """
468        expression = self
469        while type(expression) is Paren:
470            expression = expression.this
471        return expression

Returns the first non parenthesis child or self.

def unalias(self):
473    def unalias(self):
474        """
475        Returns the inner expression if this is an Alias.
476        """
477        if isinstance(self, Alias):
478            return self.this
479        return self

Returns the inner expression if this is an Alias.

def unnest_operands(self):
481    def unnest_operands(self):
482        """
483        Returns unnested operands as a tuple.
484        """
485        return tuple(arg.unnest() for _, arg in self.iter_expressions())

Returns unnested operands as a tuple.

def flatten(self, unnest=True):
487    def flatten(self, unnest=True):
488        """
489        Returns a generator which yields child nodes whose parents are the same class.
490
491        A AND B AND C -> [A, B, C]
492        """
493        for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__):
494            if not type(node) is self.__class__:
495                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 sql( self, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> str:
503    def sql(self, dialect: DialectType = None, **opts) -> str:
504        """
505        Returns SQL string representation of this tree.
506
507        Args:
508            dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
509            opts: other `sqlglot.generator.Generator` options.
510
511        Returns:
512            The SQL string.
513        """
514        from sqlglot.dialects import Dialect
515
516        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, *args, copy=True, **kwargs):
542    def transform(self, fun, *args, copy=True, **kwargs):
543        """
544        Recursively visits all tree nodes (excluding already transformed ones)
545        and applies the given transformation function to each node.
546
547        Args:
548            fun (function): a function which takes a node as an argument and returns a
549                new transformed node or the same node without modifications. If the function
550                returns None, then the corresponding node will be removed from the syntax tree.
551            copy (bool): if set to True a new tree instance is constructed, otherwise the tree is
552                modified in place.
553
554        Returns:
555            The transformed tree.
556        """
557        node = self.copy() if copy else self
558        new_node = fun(node, *args, **kwargs)
559
560        if new_node is None or not isinstance(new_node, Expression):
561            return new_node
562        if new_node is not node:
563            new_node.parent = node.parent
564            return new_node
565
566        replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs))
567        return new_node

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

Arguments:
  • fun (function): 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 (bool): 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):
577    def replace(self, expression):
578        """
579        Swap out this expression with a new expression.
580
581        For example::
582
583            >>> tree = Select().select("x").from_("tbl")
584            >>> tree.find(Column).replace(Column(this="y"))
585            (COLUMN this: y)
586            >>> tree.sql()
587            'SELECT y FROM tbl'
588
589        Args:
590            expression: new node
591
592        Returns:
593            The new expression or expressions.
594        """
595        if not self.parent:
596            return expression
597
598        parent = self.parent
599        self.parent = None
600
601        replace_children(parent, lambda child: expression if child is self else child)
602        return expression

Swap out this expression with a new expression.

For example::

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

The new expression or expressions.

def pop(self: ~E) -> ~E:
604    def pop(self: E) -> E:
605        """
606        Remove this expression from its AST.
607
608        Returns:
609            The popped expression.
610        """
611        self.replace(None)
612        return self

Remove this expression from its AST.

Returns:

The popped expression.

def assert_is(self, type_: Type[~E]) -> ~E:
614    def assert_is(self, type_: t.Type[E]) -> E:
615        """
616        Assert that this `Expression` is an instance of `type_`.
617
618        If it is NOT an instance of `type_`, this raises an assertion error.
619        Otherwise, this returns this expression.
620
621        Examples:
622            This is useful for type security in chained expressions:
623
624            >>> import sqlglot
625            >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql()
626            'SELECT x, z FROM y'
627        """
628        assert isinstance(self, type_)
629        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]:
631    def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]:
632        """
633        Checks if this expression is valid (e.g. all mandatory args are set).
634
635        Args:
636            args: a sequence of values that were used to instantiate a Func expression. This is used
637                to check that the provided arguments don't exceed the function argument limit.
638
639        Returns:
640            A list of error messages for all possible errors that were found.
641        """
642        errors: t.List[str] = []
643
644        for k in self.args:
645            if k not in self.arg_types:
646                errors.append(f"Unexpected keyword: '{k}' for {self.__class__}")
647        for k, mandatory in self.arg_types.items():
648            v = self.args.get(k)
649            if mandatory and (v is None or (isinstance(v, list) and not v)):
650                errors.append(f"Required keyword: '{k}' missing for {self.__class__}")
651
652        if (
653            args
654            and isinstance(self, Func)
655            and len(args) > len(self.arg_types)
656            and not self.is_var_len_args
657        ):
658            errors.append(
659                f"The number of provided arguments ({len(args)}) is greater than "
660                f"the maximum number of supported arguments ({len(self.arg_types)})"
661            )
662
663        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):
665    def dump(self):
666        """
667        Dump this Expression to a JSON-serializable dict.
668        """
669        from sqlglot.serde import dump
670
671        return dump(self)

Dump this Expression to a JSON-serializable dict.

@classmethod
def load(cls, obj):
673    @classmethod
674    def load(cls, obj):
675        """
676        Load a dict (as returned by `Expression.dump`) into an Expression instance.
677        """
678        from sqlglot.serde import load
679
680        return load(obj)

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

def and_( self, *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
682    def and_(
683        self,
684        *expressions: t.Optional[ExpOrStr],
685        dialect: DialectType = None,
686        copy: bool = True,
687        **opts,
688    ) -> Condition:
689        """
690        AND this condition with one or multiple expressions.
691
692        Example:
693            >>> condition("x=1").and_("y=1").sql()
694            'x = 1 AND y = 1'
695
696        Args:
697            *expressions: the SQL code strings to parse.
698                If an `Expression` instance is passed, it will be used as-is.
699            dialect: the dialect used to parse the input expression.
700            copy: whether or not to copy the involved expressions (only applies to Expressions).
701            opts: other options to use to parse the input expressions.
702
703        Returns:
704            The new And condition.
705        """
706        return and_(self, *expressions, dialect=dialect, copy=copy, **opts)

AND this condition with one or multiple expressions.

Example:
>>> condition("x=1").and_("y=1").sql()
'x = 1 AND y = 1'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether or not to copy the involved expressions (only applies to Expressions).
  • opts: other options to use to parse the input expressions.
Returns:

The new And condition.

def or_( self, *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
708    def or_(
709        self,
710        *expressions: t.Optional[ExpOrStr],
711        dialect: DialectType = None,
712        copy: bool = True,
713        **opts,
714    ) -> Condition:
715        """
716        OR this condition with one or multiple expressions.
717
718        Example:
719            >>> condition("x=1").or_("y=1").sql()
720            'x = 1 OR y = 1'
721
722        Args:
723            *expressions: the SQL code strings to parse.
724                If an `Expression` instance is passed, it will be used as-is.
725            dialect: the dialect used to parse the input expression.
726            copy: whether or not to copy the involved expressions (only applies to Expressions).
727            opts: other options to use to parse the input expressions.
728
729        Returns:
730            The new Or condition.
731        """
732        return or_(self, *expressions, dialect=dialect, copy=copy, **opts)

OR this condition with one or multiple expressions.

Example:
>>> condition("x=1").or_("y=1").sql()
'x = 1 OR y = 1'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, it will be used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether or not to copy the involved expressions (only applies to Expressions).
  • opts: other options to use to parse the input expressions.
Returns:

The new Or condition.

def not_(self, copy: bool = True):
734    def not_(self, copy: bool = True):
735        """
736        Wrap this condition with NOT.
737
738        Example:
739            >>> condition("x=1").not_().sql()
740            'NOT x = 1'
741
742        Args:
743            copy: whether or not to copy this object.
744
745        Returns:
746            The new Not instance.
747        """
748        return not_(self, copy=copy)

Wrap this condition with NOT.

Example:
>>> condition("x=1").not_().sql()
'NOT x = 1'
Arguments:
  • copy: whether or not 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:
750    def as_(
751        self,
752        alias: str | Identifier,
753        quoted: t.Optional[bool] = None,
754        dialect: DialectType = None,
755        copy: bool = True,
756        **opts,
757    ) -> Alias:
758        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:
783    def isin(
784        self,
785        *expressions: t.Any,
786        query: t.Optional[ExpOrStr] = None,
787        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
788        copy: bool = True,
789        **opts,
790    ) -> In:
791        return In(
792            this=maybe_copy(self, copy),
793            expressions=[convert(e, copy=copy) for e in expressions],
794            query=maybe_parse(query, copy=copy, **opts) if query else None,
795            unnest=Unnest(
796                expressions=[
797                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
798                ]
799            )
800            if unnest
801            else None,
802        )
def between( self, low: Any, high: Any, copy: bool = True, **opts) -> Between:
804    def between(self, low: t.Any, high: t.Any, copy: bool = True, **opts) -> Between:
805        return Between(
806            this=maybe_copy(self, copy),
807            low=convert(low, copy=copy, **opts),
808            high=convert(high, copy=copy, **opts),
809        )
def is_( self, other: Union[str, Expression]) -> Is:
811    def is_(self, other: ExpOrStr) -> Is:
812        return self._binop(Is, other)
def like( self, other: Union[str, Expression]) -> Like:
814    def like(self, other: ExpOrStr) -> Like:
815        return self._binop(Like, other)
def ilike( self, other: Union[str, Expression]) -> ILike:
817    def ilike(self, other: ExpOrStr) -> ILike:
818        return self._binop(ILike, other)
def eq(self, other: Any) -> EQ:
820    def eq(self, other: t.Any) -> EQ:
821        return self._binop(EQ, other)
def neq(self, other: Any) -> NEQ:
823    def neq(self, other: t.Any) -> NEQ:
824        return self._binop(NEQ, other)
def rlike( self, other: Union[str, Expression]) -> RegexpLike:
826    def rlike(self, other: ExpOrStr) -> RegexpLike:
827        return self._binop(RegexpLike, other)
def div( self, other: Union[str, Expression], typed: bool = False, safe: bool = False) -> Div:
829    def div(self, other: ExpOrStr, typed: bool = False, safe: bool = False) -> Div:
830        div = self._binop(Div, other)
831        div.args["typed"] = typed
832        div.args["safe"] = safe
833        return div
IntoType = typing.Union[str, typing.Type[Expression], typing.Collection[typing.Union[str, typing.Type[Expression]]]]
ExpOrStr = typing.Union[str, Expression]
class Condition(Expression):
916class Condition(Expression):
917    """Logical conditions like x AND y, or simply x"""

Logical conditions like x AND y, or simply x

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

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

key = 'predicate'
class DerivedTable(Expression):
924class DerivedTable(Expression):
925    @property
926    def selects(self) -> t.List[Expression]:
927        return self.this.selects if isinstance(self.this, Subqueryable) else []
928
929    @property
930    def named_selects(self) -> t.List[str]:
931        return [select.output_name for select in self.selects]
selects: List[Expression]
named_selects: List[str]
key = 'derivedtable'
class Unionable(Expression):
 934class Unionable(Expression):
 935    def union(
 936        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 937    ) -> Unionable:
 938        """
 939        Builds a UNION expression.
 940
 941        Example:
 942            >>> import sqlglot
 943            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
 944            'SELECT * FROM foo UNION SELECT * FROM bla'
 945
 946        Args:
 947            expression: the SQL code string.
 948                If an `Expression` instance is passed, it will be used as-is.
 949            distinct: set the DISTINCT flag if and only if this is true.
 950            dialect: the dialect used to parse the input expression.
 951            opts: other options to use to parse the input expressions.
 952
 953        Returns:
 954            The new Union expression.
 955        """
 956        return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
 957
 958    def intersect(
 959        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 960    ) -> Unionable:
 961        """
 962        Builds an INTERSECT expression.
 963
 964        Example:
 965            >>> import sqlglot
 966            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
 967            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
 968
 969        Args:
 970            expression: the SQL code string.
 971                If an `Expression` instance is passed, it will be used as-is.
 972            distinct: set the DISTINCT flag if and only if this is true.
 973            dialect: the dialect used to parse the input expression.
 974            opts: other options to use to parse the input expressions.
 975
 976        Returns:
 977            The new Intersect expression.
 978        """
 979        return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
 980
 981    def except_(
 982        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 983    ) -> Unionable:
 984        """
 985        Builds an EXCEPT expression.
 986
 987        Example:
 988            >>> import sqlglot
 989            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
 990            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
 991
 992        Args:
 993            expression: the SQL code string.
 994                If an `Expression` instance is passed, it will be used as-is.
 995            distinct: set the DISTINCT flag if and only if this is true.
 996            dialect: the dialect used to parse the input expression.
 997            opts: other options to use to parse the input expressions.
 998
 999        Returns:
1000            The new Except expression.
1001        """
1002        return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
def union( self, expression: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Unionable:
935    def union(
936        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
937    ) -> Unionable:
938        """
939        Builds a UNION expression.
940
941        Example:
942            >>> import sqlglot
943            >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql()
944            'SELECT * FROM foo UNION SELECT * FROM bla'
945
946        Args:
947            expression: the SQL code string.
948                If an `Expression` instance is passed, it will be used as-is.
949            distinct: set the DISTINCT flag if and only if this is true.
950            dialect: the dialect used to parse the input expression.
951            opts: other options to use to parse the input expressions.
952
953        Returns:
954            The new Union expression.
955        """
956        return union(left=self, right=expression, 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:
  • expression: the SQL code string. If an Expression instance is passed, it 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, expression: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Unionable:
958    def intersect(
959        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
960    ) -> Unionable:
961        """
962        Builds an INTERSECT expression.
963
964        Example:
965            >>> import sqlglot
966            >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql()
967            'SELECT * FROM foo INTERSECT SELECT * FROM bla'
968
969        Args:
970            expression: the SQL code string.
971                If an `Expression` instance is passed, it will be used as-is.
972            distinct: set the DISTINCT flag if and only if this is true.
973            dialect: the dialect used to parse the input expression.
974            opts: other options to use to parse the input expressions.
975
976        Returns:
977            The new Intersect expression.
978        """
979        return intersect(left=self, right=expression, 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:
  • expression: the SQL code string. If an Expression instance is passed, it 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, expression: Union[str, Expression], distinct: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Unionable:
 981    def except_(
 982        self, expression: ExpOrStr, distinct: bool = True, dialect: DialectType = None, **opts
 983    ) -> Unionable:
 984        """
 985        Builds an EXCEPT expression.
 986
 987        Example:
 988            >>> import sqlglot
 989            >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql()
 990            'SELECT * FROM foo EXCEPT SELECT * FROM bla'
 991
 992        Args:
 993            expression: the SQL code string.
 994                If an `Expression` instance is passed, it will be used as-is.
 995            distinct: set the DISTINCT flag if and only if this is true.
 996            dialect: the dialect used to parse the input expression.
 997            opts: other options to use to parse the input expressions.
 998
 999        Returns:
1000            The new Except expression.
1001        """
1002        return except_(left=self, right=expression, 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:
  • expression: the SQL code string. If an Expression instance is passed, it 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 = 'unionable'
class UDTF(DerivedTable, Unionable):
1005class UDTF(DerivedTable, Unionable):
1006    @property
1007    def selects(self) -> t.List[Expression]:
1008        alias = self.args.get("alias")
1009        return alias.columns if alias else []
selects: List[Expression]
key = 'udtf'
class Cache(Expression):
1012class Cache(Expression):
1013    arg_types = {
1014        "this": True,
1015        "lazy": False,
1016        "options": False,
1017        "expression": False,
1018    }
arg_types = {'this': True, 'lazy': False, 'options': False, 'expression': False}
key = 'cache'
class Uncache(Expression):
1021class Uncache(Expression):
1022    arg_types = {"this": True, "exists": False}
arg_types = {'this': True, 'exists': False}
key = 'uncache'
class Refresh(Expression):
1025class Refresh(Expression):
1026    pass
key = 'refresh'
class DDL(Expression):
1029class DDL(Expression):
1030    @property
1031    def ctes(self):
1032        with_ = self.args.get("with")
1033        if not with_:
1034            return []
1035        return with_.expressions
1036
1037    @property
1038    def named_selects(self) -> t.List[str]:
1039        if isinstance(self.expression, Subqueryable):
1040            return self.expression.named_selects
1041        return []
1042
1043    @property
1044    def selects(self) -> t.List[Expression]:
1045        if isinstance(self.expression, Subqueryable):
1046            return self.expression.selects
1047        return []
ctes
named_selects: List[str]
selects: List[Expression]
key = 'ddl'
class DML(Expression):
1050class DML(Expression):
1051    def returning(
1052        self,
1053        expression: ExpOrStr,
1054        dialect: DialectType = None,
1055        copy: bool = True,
1056        **opts,
1057    ) -> DML:
1058        """
1059        Set the RETURNING expression. Not supported by all dialects.
1060
1061        Example:
1062            >>> delete("tbl").returning("*", dialect="postgres").sql()
1063            'DELETE FROM tbl RETURNING *'
1064
1065        Args:
1066            expression: the SQL code strings to parse.
1067                If an `Expression` instance is passed, it will be used as-is.
1068            dialect: the dialect used to parse the input expressions.
1069            copy: if `False`, modify this expression instance in-place.
1070            opts: other options to use to parse the input expressions.
1071
1072        Returns:
1073            Delete: the modified expression.
1074        """
1075        return _apply_builder(
1076            expression=expression,
1077            instance=self,
1078            arg="returning",
1079            prefix="RETURNING",
1080            dialect=dialect,
1081            copy=copy,
1082            into=Returning,
1083            **opts,
1084        )
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) -> DML:
1051    def returning(
1052        self,
1053        expression: ExpOrStr,
1054        dialect: DialectType = None,
1055        copy: bool = True,
1056        **opts,
1057    ) -> DML:
1058        """
1059        Set the RETURNING expression. Not supported by all dialects.
1060
1061        Example:
1062            >>> delete("tbl").returning("*", dialect="postgres").sql()
1063            'DELETE FROM tbl RETURNING *'
1064
1065        Args:
1066            expression: the SQL code strings to parse.
1067                If an `Expression` instance is passed, it will be used as-is.
1068            dialect: the dialect used to parse the input expressions.
1069            copy: if `False`, modify this expression instance in-place.
1070            opts: other options to use to parse the input expressions.
1071
1072        Returns:
1073            Delete: the modified expression.
1074        """
1075        return _apply_builder(
1076            expression=expression,
1077            instance=self,
1078            arg="returning",
1079            prefix="RETURNING",
1080            dialect=dialect,
1081            copy=copy,
1082            into=Returning,
1083            **opts,
1084        )

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):
1087class Create(DDL):
1088    arg_types = {
1089        "with": False,
1090        "this": True,
1091        "kind": True,
1092        "expression": False,
1093        "exists": False,
1094        "properties": False,
1095        "replace": False,
1096        "unique": False,
1097        "indexes": False,
1098        "no_schema_binding": False,
1099        "begin": False,
1100        "end": False,
1101        "clone": False,
1102    }
arg_types = {'with': False, 'this': True, 'kind': True, 'expression': False, 'exists': False, 'properties': False, 'replace': False, 'unique': False, 'indexes': False, 'no_schema_binding': False, 'begin': False, 'end': False, 'clone': False}
key = 'create'
class Clone(Expression):
1108class Clone(Expression):
1109    arg_types = {"this": True, "shallow": False, "copy": False}
arg_types = {'this': True, 'shallow': False, 'copy': False}
key = 'clone'
class Describe(Expression):
1112class Describe(Expression):
1113    arg_types = {"this": True, "kind": False, "expressions": False}
arg_types = {'this': True, 'kind': False, 'expressions': False}
key = 'describe'
class Kill(Expression):
1116class Kill(Expression):
1117    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'kill'
class Pragma(Expression):
1120class Pragma(Expression):
1121    pass
key = 'pragma'
class Set(Expression):
1124class Set(Expression):
1125    arg_types = {"expressions": False, "unset": False, "tag": False}
arg_types = {'expressions': False, 'unset': False, 'tag': False}
key = 'set'
class SetItem(Expression):
1128class SetItem(Expression):
1129    arg_types = {
1130        "this": False,
1131        "expressions": False,
1132        "kind": False,
1133        "collate": False,  # MySQL SET NAMES statement
1134        "global": False,
1135    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'collate': False, 'global': False}
key = 'setitem'
class Show(Expression):
1138class Show(Expression):
1139    arg_types = {
1140        "this": True,
1141        "target": False,
1142        "offset": False,
1143        "limit": False,
1144        "like": False,
1145        "where": False,
1146        "db": False,
1147        "scope": False,
1148        "scope_kind": False,
1149        "full": False,
1150        "mutex": False,
1151        "query": False,
1152        "channel": False,
1153        "global": False,
1154        "log": False,
1155        "position": False,
1156        "types": False,
1157    }
arg_types = {'this': True, 'target': False, 'offset': False, 'limit': 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):
1160class UserDefinedFunction(Expression):
1161    arg_types = {"this": True, "expressions": False, "wrapped": False}
arg_types = {'this': True, 'expressions': False, 'wrapped': False}
key = 'userdefinedfunction'
class CharacterSet(Expression):
1164class CharacterSet(Expression):
1165    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'characterset'
class With(Expression):
1168class With(Expression):
1169    arg_types = {"expressions": True, "recursive": False}
1170
1171    @property
1172    def recursive(self) -> bool:
1173        return bool(self.args.get("recursive"))
arg_types = {'expressions': True, 'recursive': False}
recursive: bool
key = 'with'
class WithinGroup(Expression):
1176class WithinGroup(Expression):
1177    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'withingroup'
class CTE(DerivedTable):
1182class CTE(DerivedTable):
1183    arg_types = {"this": True, "alias": True, "scalar": False}
arg_types = {'this': True, 'alias': True, 'scalar': False}
key = 'cte'
class TableAlias(Expression):
1186class TableAlias(Expression):
1187    arg_types = {"this": False, "columns": False}
1188
1189    @property
1190    def columns(self):
1191        return self.args.get("columns") or []
arg_types = {'this': False, 'columns': False}
columns
key = 'tablealias'
class BitString(Condition):
1194class BitString(Condition):
1195    pass
key = 'bitstring'
class HexString(Condition):
1198class HexString(Condition):
1199    pass
key = 'hexstring'
class ByteString(Condition):
1202class ByteString(Condition):
1203    pass
key = 'bytestring'
class RawString(Condition):
1206class RawString(Condition):
1207    pass
key = 'rawstring'
class Column(Condition):
1210class Column(Condition):
1211    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1212
1213    @property
1214    def table(self) -> str:
1215        return self.text("table")
1216
1217    @property
1218    def db(self) -> str:
1219        return self.text("db")
1220
1221    @property
1222    def catalog(self) -> str:
1223        return self.text("catalog")
1224
1225    @property
1226    def output_name(self) -> str:
1227        return self.name
1228
1229    @property
1230    def parts(self) -> t.List[Identifier]:
1231        """Return the parts of a column in order catalog, db, table, name."""
1232        return [
1233            t.cast(Identifier, self.args[part])
1234            for part in ("catalog", "db", "table", "this")
1235            if self.args.get(part)
1236        ]
1237
1238    def to_dot(self) -> Dot | Identifier:
1239        """Converts the column into a dot expression."""
1240        parts = self.parts
1241        parent = self.parent
1242
1243        while parent:
1244            if isinstance(parent, Dot):
1245                parts.append(parent.expression)
1246            parent = parent.parent
1247
1248        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
db: str
catalog: str
output_name: str

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]

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

def to_dot(self) -> Dot | Identifier:
1238    def to_dot(self) -> Dot | Identifier:
1239        """Converts the column into a dot expression."""
1240        parts = self.parts
1241        parent = self.parent
1242
1243        while parent:
1244            if isinstance(parent, Dot):
1245                parts.append(parent.expression)
1246            parent = parent.parent
1247
1248        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]

Converts the column into a dot expression.

key = 'column'
class ColumnPosition(Expression):
1251class ColumnPosition(Expression):
1252    arg_types = {"this": False, "position": True}
arg_types = {'this': False, 'position': True}
key = 'columnposition'
class ColumnDef(Expression):
1255class ColumnDef(Expression):
1256    arg_types = {
1257        "this": True,
1258        "kind": False,
1259        "constraints": False,
1260        "exists": False,
1261        "position": False,
1262    }
1263
1264    @property
1265    def constraints(self) -> t.List[ColumnConstraint]:
1266        return self.args.get("constraints") or []
arg_types = {'this': True, 'kind': False, 'constraints': False, 'exists': False, 'position': False}
constraints: List[ColumnConstraint]
key = 'columndef'
class AlterColumn(Expression):
1269class AlterColumn(Expression):
1270    arg_types = {
1271        "this": True,
1272        "dtype": False,
1273        "collate": False,
1274        "using": False,
1275        "default": False,
1276        "drop": False,
1277    }
arg_types = {'this': True, 'dtype': False, 'collate': False, 'using': False, 'default': False, 'drop': False}
key = 'altercolumn'
class RenameTable(Expression):
1280class RenameTable(Expression):
1281    pass
key = 'renametable'
class SwapTable(Expression):
1284class SwapTable(Expression):
1285    pass
key = 'swaptable'
class Comment(Expression):
1288class Comment(Expression):
1289    arg_types = {"this": True, "kind": True, "expression": True, "exists": False}
arg_types = {'this': True, 'kind': True, 'expression': True, 'exists': False}
key = 'comment'
class Comprehension(Expression):
1292class Comprehension(Expression):
1293    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):
1297class MergeTreeTTLAction(Expression):
1298    arg_types = {
1299        "this": True,
1300        "delete": False,
1301        "recompress": False,
1302        "to_disk": False,
1303        "to_volume": False,
1304    }
arg_types = {'this': True, 'delete': False, 'recompress': False, 'to_disk': False, 'to_volume': False}
key = 'mergetreettlaction'
class MergeTreeTTL(Expression):
1308class MergeTreeTTL(Expression):
1309    arg_types = {
1310        "expressions": True,
1311        "where": False,
1312        "group": False,
1313        "aggregates": False,
1314    }
arg_types = {'expressions': True, 'where': False, 'group': False, 'aggregates': False}
key = 'mergetreettl'
class IndexConstraintOption(Expression):
1318class IndexConstraintOption(Expression):
1319    arg_types = {
1320        "key_block_size": False,
1321        "using": False,
1322        "parser": False,
1323        "comment": False,
1324        "visible": False,
1325        "engine_attr": False,
1326        "secondary_engine_attr": False,
1327    }
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):
1330class ColumnConstraint(Expression):
1331    arg_types = {"this": False, "kind": True}
1332
1333    @property
1334    def kind(self) -> ColumnConstraintKind:
1335        return self.args["kind"]
arg_types = {'this': False, 'kind': True}
key = 'columnconstraint'
class ColumnConstraintKind(Expression):
1338class ColumnConstraintKind(Expression):
1339    pass
key = 'columnconstraintkind'
class AutoIncrementColumnConstraint(ColumnConstraintKind):
1342class AutoIncrementColumnConstraint(ColumnConstraintKind):
1343    pass
key = 'autoincrementcolumnconstraint'
class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1346class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1347    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'periodforsystemtimeconstraint'
class CaseSpecificColumnConstraint(ColumnConstraintKind):
1350class CaseSpecificColumnConstraint(ColumnConstraintKind):
1351    arg_types = {"not_": True}
arg_types = {'not_': True}
key = 'casespecificcolumnconstraint'
class CharacterSetColumnConstraint(ColumnConstraintKind):
1354class CharacterSetColumnConstraint(ColumnConstraintKind):
1355    arg_types = {"this": True}
arg_types = {'this': True}
key = 'charactersetcolumnconstraint'
class CheckColumnConstraint(ColumnConstraintKind):
1358class CheckColumnConstraint(ColumnConstraintKind):
1359    pass
key = 'checkcolumnconstraint'
class ClusteredColumnConstraint(ColumnConstraintKind):
1362class ClusteredColumnConstraint(ColumnConstraintKind):
1363    pass
key = 'clusteredcolumnconstraint'
class CollateColumnConstraint(ColumnConstraintKind):
1366class CollateColumnConstraint(ColumnConstraintKind):
1367    pass
key = 'collatecolumnconstraint'
class CommentColumnConstraint(ColumnConstraintKind):
1370class CommentColumnConstraint(ColumnConstraintKind):
1371    pass
key = 'commentcolumnconstraint'
class CompressColumnConstraint(ColumnConstraintKind):
1374class CompressColumnConstraint(ColumnConstraintKind):
1375    pass
key = 'compresscolumnconstraint'
class DateFormatColumnConstraint(ColumnConstraintKind):
1378class DateFormatColumnConstraint(ColumnConstraintKind):
1379    arg_types = {"this": True}
arg_types = {'this': True}
key = 'dateformatcolumnconstraint'
class DefaultColumnConstraint(ColumnConstraintKind):
1382class DefaultColumnConstraint(ColumnConstraintKind):
1383    pass
key = 'defaultcolumnconstraint'
class EncodeColumnConstraint(ColumnConstraintKind):
1386class EncodeColumnConstraint(ColumnConstraintKind):
1387    pass
key = 'encodecolumnconstraint'
class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1390class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1391    # this: True -> ALWAYS, this: False -> BY DEFAULT
1392    arg_types = {
1393        "this": False,
1394        "expression": False,
1395        "on_null": False,
1396        "start": False,
1397        "increment": False,
1398        "minvalue": False,
1399        "maxvalue": False,
1400        "cycle": False,
1401    }
arg_types = {'this': False, 'expression': False, 'on_null': False, 'start': False, 'increment': False, 'minvalue': False, 'maxvalue': False, 'cycle': False}
key = 'generatedasidentitycolumnconstraint'
class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1404class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1405    arg_types = {"start": True, "hidden": False}
arg_types = {'start': True, 'hidden': False}
key = 'generatedasrowcolumnconstraint'
class IndexColumnConstraint(ColumnConstraintKind):
1409class IndexColumnConstraint(ColumnConstraintKind):
1410    arg_types = {
1411        "this": False,
1412        "schema": True,
1413        "kind": False,
1414        "index_type": False,
1415        "options": False,
1416    }
arg_types = {'this': False, 'schema': True, 'kind': False, 'index_type': False, 'options': False}
key = 'indexcolumnconstraint'
class InlineLengthColumnConstraint(ColumnConstraintKind):
1419class InlineLengthColumnConstraint(ColumnConstraintKind):
1420    pass
key = 'inlinelengthcolumnconstraint'
class NonClusteredColumnConstraint(ColumnConstraintKind):
1423class NonClusteredColumnConstraint(ColumnConstraintKind):
1424    pass
key = 'nonclusteredcolumnconstraint'
class NotForReplicationColumnConstraint(ColumnConstraintKind):
1427class NotForReplicationColumnConstraint(ColumnConstraintKind):
1428    arg_types = {}
arg_types = {}
key = 'notforreplicationcolumnconstraint'
class NotNullColumnConstraint(ColumnConstraintKind):
1431class NotNullColumnConstraint(ColumnConstraintKind):
1432    arg_types = {"allow_null": False}
arg_types = {'allow_null': False}
key = 'notnullcolumnconstraint'
class OnUpdateColumnConstraint(ColumnConstraintKind):
1436class OnUpdateColumnConstraint(ColumnConstraintKind):
1437    pass
key = 'onupdatecolumnconstraint'
class TransformColumnConstraint(ColumnConstraintKind):
1441class TransformColumnConstraint(ColumnConstraintKind):
1442    pass
key = 'transformcolumnconstraint'
class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1445class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1446    arg_types = {"desc": False}
arg_types = {'desc': False}
key = 'primarykeycolumnconstraint'
class TitleColumnConstraint(ColumnConstraintKind):
1449class TitleColumnConstraint(ColumnConstraintKind):
1450    pass
key = 'titlecolumnconstraint'
class UniqueColumnConstraint(ColumnConstraintKind):
1453class UniqueColumnConstraint(ColumnConstraintKind):
1454    arg_types = {"this": False, "index_type": False}
arg_types = {'this': False, 'index_type': False}
key = 'uniquecolumnconstraint'
class UppercaseColumnConstraint(ColumnConstraintKind):
1457class UppercaseColumnConstraint(ColumnConstraintKind):
1458    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'uppercasecolumnconstraint'
class PathColumnConstraint(ColumnConstraintKind):
1461class PathColumnConstraint(ColumnConstraintKind):
1462    pass
key = 'pathcolumnconstraint'
class ComputedColumnConstraint(ColumnConstraintKind):
1467class ComputedColumnConstraint(ColumnConstraintKind):
1468    arg_types = {"this": True, "persisted": False, "not_null": False}
arg_types = {'this': True, 'persisted': False, 'not_null': False}
key = 'computedcolumnconstraint'
class Constraint(Expression):
1471class Constraint(Expression):
1472    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'constraint'
class Delete(DML):
1475class Delete(DML):
1476    arg_types = {
1477        "with": False,
1478        "this": False,
1479        "using": False,
1480        "where": False,
1481        "returning": False,
1482        "limit": False,
1483        "tables": False,  # Multiple-Table Syntax (MySQL)
1484    }
1485
1486    def delete(
1487        self,
1488        table: ExpOrStr,
1489        dialect: DialectType = None,
1490        copy: bool = True,
1491        **opts,
1492    ) -> Delete:
1493        """
1494        Create a DELETE expression or replace the table on an existing DELETE expression.
1495
1496        Example:
1497            >>> delete("tbl").sql()
1498            'DELETE FROM tbl'
1499
1500        Args:
1501            table: the table from which to delete.
1502            dialect: the dialect used to parse the input expression.
1503            copy: if `False`, modify this expression instance in-place.
1504            opts: other options to use to parse the input expressions.
1505
1506        Returns:
1507            Delete: the modified expression.
1508        """
1509        return _apply_builder(
1510            expression=table,
1511            instance=self,
1512            arg="this",
1513            dialect=dialect,
1514            into=Table,
1515            copy=copy,
1516            **opts,
1517        )
1518
1519    def where(
1520        self,
1521        *expressions: t.Optional[ExpOrStr],
1522        append: bool = True,
1523        dialect: DialectType = None,
1524        copy: bool = True,
1525        **opts,
1526    ) -> Delete:
1527        """
1528        Append to or set the WHERE expressions.
1529
1530        Example:
1531            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1532            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1533
1534        Args:
1535            *expressions: the SQL code strings to parse.
1536                If an `Expression` instance is passed, it will be used as-is.
1537                Multiple expressions are combined with an AND operator.
1538            append: if `True`, AND the new expressions to any existing expression.
1539                Otherwise, this resets the expression.
1540            dialect: the dialect used to parse the input expressions.
1541            copy: if `False`, modify this expression instance in-place.
1542            opts: other options to use to parse the input expressions.
1543
1544        Returns:
1545            Delete: the modified expression.
1546        """
1547        return _apply_conjunction_builder(
1548            *expressions,
1549            instance=self,
1550            arg="where",
1551            append=append,
1552            into=Where,
1553            dialect=dialect,
1554            copy=copy,
1555            **opts,
1556        )
arg_types = {'with': False, 'this': False, 'using': False, 'where': False, 'returning': False, 'limit': False, 'tables': 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:
1486    def delete(
1487        self,
1488        table: ExpOrStr,
1489        dialect: DialectType = None,
1490        copy: bool = True,
1491        **opts,
1492    ) -> Delete:
1493        """
1494        Create a DELETE expression or replace the table on an existing DELETE expression.
1495
1496        Example:
1497            >>> delete("tbl").sql()
1498            'DELETE FROM tbl'
1499
1500        Args:
1501            table: the table from which to delete.
1502            dialect: the dialect used to parse the input expression.
1503            copy: if `False`, modify this expression instance in-place.
1504            opts: other options to use to parse the input expressions.
1505
1506        Returns:
1507            Delete: the modified expression.
1508        """
1509        return _apply_builder(
1510            expression=table,
1511            instance=self,
1512            arg="this",
1513            dialect=dialect,
1514            into=Table,
1515            copy=copy,
1516            **opts,
1517        )

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:
1519    def where(
1520        self,
1521        *expressions: t.Optional[ExpOrStr],
1522        append: bool = True,
1523        dialect: DialectType = None,
1524        copy: bool = True,
1525        **opts,
1526    ) -> Delete:
1527        """
1528        Append to or set the WHERE expressions.
1529
1530        Example:
1531            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1532            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1533
1534        Args:
1535            *expressions: the SQL code strings to parse.
1536                If an `Expression` instance is passed, it will be used as-is.
1537                Multiple expressions are combined with an AND operator.
1538            append: if `True`, AND the new expressions to any existing expression.
1539                Otherwise, this resets the expression.
1540            dialect: the dialect used to parse the input expressions.
1541            copy: if `False`, modify this expression instance in-place.
1542            opts: other options to use to parse the input expressions.
1543
1544        Returns:
1545            Delete: the modified expression.
1546        """
1547        return _apply_conjunction_builder(
1548            *expressions,
1549            instance=self,
1550            arg="where",
1551            append=append,
1552            into=Where,
1553            dialect=dialect,
1554            copy=copy,
1555            **opts,
1556        )

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):
1559class Drop(Expression):
1560    arg_types = {
1561        "this": False,
1562        "kind": False,
1563        "exists": False,
1564        "temporary": False,
1565        "materialized": False,
1566        "cascade": False,
1567        "constraints": False,
1568        "purge": False,
1569    }
arg_types = {'this': False, 'kind': False, 'exists': False, 'temporary': False, 'materialized': False, 'cascade': False, 'constraints': False, 'purge': False}
key = 'drop'
class Filter(Expression):
1572class Filter(Expression):
1573    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'filter'
class Check(Expression):
1576class Check(Expression):
1577    pass
key = 'check'
class Connect(Expression):
1581class Connect(Expression):
1582    arg_types = {"start": False, "connect": True}
arg_types = {'start': False, 'connect': True}
key = 'connect'
class Prior(Expression):
1585class Prior(Expression):
1586    pass
key = 'prior'
class Directory(Expression):
1589class Directory(Expression):
1590    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
1591    arg_types = {"this": True, "local": False, "row_format": False}
arg_types = {'this': True, 'local': False, 'row_format': False}
key = 'directory'
class ForeignKey(Expression):
1594class ForeignKey(Expression):
1595    arg_types = {
1596        "expressions": True,
1597        "reference": False,
1598        "delete": False,
1599        "update": False,
1600    }
arg_types = {'expressions': True, 'reference': False, 'delete': False, 'update': False}
key = 'foreignkey'
class ColumnPrefix(Expression):
1603class ColumnPrefix(Expression):
1604    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'columnprefix'
class PrimaryKey(Expression):
1607class PrimaryKey(Expression):
1608    arg_types = {"expressions": True, "options": False}
arg_types = {'expressions': True, 'options': False}
key = 'primarykey'
class Into(Expression):
1613class Into(Expression):
1614    arg_types = {"this": True, "temporary": False, "unlogged": False}
arg_types = {'this': True, 'temporary': False, 'unlogged': False}
key = 'into'
class From(Expression):
1617class From(Expression):
1618    @property
1619    def name(self) -> str:
1620        return self.this.name
1621
1622    @property
1623    def alias_or_name(self) -> str:
1624        return self.this.alias_or_name
name: str
alias_or_name: str
key = 'from'
class Having(Expression):
1627class Having(Expression):
1628    pass
key = 'having'
class Hint(Expression):
1631class Hint(Expression):
1632    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'hint'
class JoinHint(Expression):
1635class JoinHint(Expression):
1636    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'joinhint'
class Identifier(Expression):
1639class Identifier(Expression):
1640    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
1641
1642    @property
1643    def quoted(self) -> bool:
1644        return bool(self.args.get("quoted"))
1645
1646    @property
1647    def hashable_args(self) -> t.Any:
1648        return (self.this, self.quoted)
1649
1650    @property
1651    def output_name(self) -> str:
1652        return self.name
arg_types = {'this': True, 'quoted': False, 'global': False, 'temporary': False}
quoted: bool
hashable_args: Any
output_name: str

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):
1656class Opclass(Expression):
1657    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'opclass'
class Index(Expression):
1660class Index(Expression):
1661    arg_types = {
1662        "this": False,
1663        "table": False,
1664        "using": False,
1665        "where": False,
1666        "columns": False,
1667        "unique": False,
1668        "primary": False,
1669        "amp": False,  # teradata
1670        "partition_by": False,  # teradata
1671        "where": False,  # postgres partial indexes
1672    }
arg_types = {'this': False, 'table': False, 'using': False, 'where': False, 'columns': False, 'unique': False, 'primary': False, 'amp': False, 'partition_by': False}
key = 'index'
class Insert(DDL, DML):
1675class Insert(DDL, DML):
1676    arg_types = {
1677        "with": False,
1678        "this": True,
1679        "expression": False,
1680        "conflict": False,
1681        "returning": False,
1682        "overwrite": False,
1683        "exists": False,
1684        "partition": False,
1685        "alternative": False,
1686        "where": False,
1687        "ignore": False,
1688        "by_name": False,
1689    }
1690
1691    def with_(
1692        self,
1693        alias: ExpOrStr,
1694        as_: ExpOrStr,
1695        recursive: t.Optional[bool] = None,
1696        append: bool = True,
1697        dialect: DialectType = None,
1698        copy: bool = True,
1699        **opts,
1700    ) -> Insert:
1701        """
1702        Append to or set the common table expressions.
1703
1704        Example:
1705            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
1706            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
1707
1708        Args:
1709            alias: the SQL code string to parse as the table name.
1710                If an `Expression` instance is passed, this is used as-is.
1711            as_: the SQL code string to parse as the table expression.
1712                If an `Expression` instance is passed, it will be used as-is.
1713            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1714            append: if `True`, add to any existing expressions.
1715                Otherwise, this resets the expressions.
1716            dialect: the dialect used to parse the input expression.
1717            copy: if `False`, modify this expression instance in-place.
1718            opts: other options to use to parse the input expressions.
1719
1720        Returns:
1721            The modified expression.
1722        """
1723        return _apply_cte_builder(
1724            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
1725        )
arg_types = {'with': False, 'this': True, 'expression': False, 'conflict': False, 'returning': False, 'overwrite': False, 'exists': False, 'partition': False, 'alternative': False, 'where': False, 'ignore': False, 'by_name': False}
def with_( self, alias: Union[str, Expression], as_: Union[str, Expression], recursive: 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:
1691    def with_(
1692        self,
1693        alias: ExpOrStr,
1694        as_: ExpOrStr,
1695        recursive: t.Optional[bool] = None,
1696        append: bool = True,
1697        dialect: DialectType = None,
1698        copy: bool = True,
1699        **opts,
1700    ) -> Insert:
1701        """
1702        Append to or set the common table expressions.
1703
1704        Example:
1705            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
1706            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
1707
1708        Args:
1709            alias: the SQL code string to parse as the table name.
1710                If an `Expression` instance is passed, this is used as-is.
1711            as_: the SQL code string to parse as the table expression.
1712                If an `Expression` instance is passed, it will be used as-is.
1713            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1714            append: if `True`, add to any existing expressions.
1715                Otherwise, this resets the expressions.
1716            dialect: the dialect used to parse the input expression.
1717            copy: if `False`, modify this expression instance in-place.
1718            opts: other options to use to parse the input expressions.
1719
1720        Returns:
1721            The modified expression.
1722        """
1723        return _apply_cte_builder(
1724            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
1725        )

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.
  • 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 OnConflict(Expression):
1728class OnConflict(Expression):
1729    arg_types = {
1730        "duplicate": False,
1731        "expressions": False,
1732        "nothing": False,
1733        "key": False,
1734        "constraint": False,
1735    }
arg_types = {'duplicate': False, 'expressions': False, 'nothing': False, 'key': False, 'constraint': False}
key = 'onconflict'
class Returning(Expression):
1738class Returning(Expression):
1739    arg_types = {"expressions": True, "into": False}
arg_types = {'expressions': True, 'into': False}
key = 'returning'
class Introducer(Expression):
1743class Introducer(Expression):
1744    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'introducer'
class National(Expression):
1748class National(Expression):
1749    pass
key = 'national'
class LoadData(Expression):
1752class LoadData(Expression):
1753    arg_types = {
1754        "this": True,
1755        "local": False,
1756        "overwrite": False,
1757        "inpath": True,
1758        "partition": False,
1759        "input_format": False,
1760        "serde": False,
1761    }
arg_types = {'this': True, 'local': False, 'overwrite': False, 'inpath': True, 'partition': False, 'input_format': False, 'serde': False}
key = 'loaddata'
class Partition(Expression):
1764class Partition(Expression):
1765    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'partition'
class Fetch(Expression):
1768class Fetch(Expression):
1769    arg_types = {
1770        "direction": False,
1771        "count": False,
1772        "percent": False,
1773        "with_ties": False,
1774    }
arg_types = {'direction': False, 'count': False, 'percent': False, 'with_ties': False}
key = 'fetch'
class Group(Expression):
1777class Group(Expression):
1778    arg_types = {
1779        "expressions": False,
1780        "grouping_sets": False,
1781        "cube": False,
1782        "rollup": False,
1783        "totals": False,
1784        "all": False,
1785    }
arg_types = {'expressions': False, 'grouping_sets': False, 'cube': False, 'rollup': False, 'totals': False, 'all': False}
key = 'group'
class Lambda(Expression):
1788class Lambda(Expression):
1789    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'lambda'
class Limit(Expression):
1792class Limit(Expression):
1793    arg_types = {"this": False, "expression": True, "offset": False}
arg_types = {'this': False, 'expression': True, 'offset': False}
key = 'limit'
class Literal(Condition):
1796class Literal(Condition):
1797    arg_types = {"this": True, "is_string": True}
1798
1799    @property
1800    def hashable_args(self) -> t.Any:
1801        return (self.this, self.args.get("is_string"))
1802
1803    @classmethod
1804    def number(cls, number) -> Literal:
1805        return cls(this=str(number), is_string=False)
1806
1807    @classmethod
1808    def string(cls, string) -> Literal:
1809        return cls(this=str(string), is_string=True)
1810
1811    @property
1812    def output_name(self) -> str:
1813        return self.name
arg_types = {'this': True, 'is_string': True}
hashable_args: Any
@classmethod
def number(cls, number) -> Literal:
1803    @classmethod
1804    def number(cls, number) -> Literal:
1805        return cls(this=str(number), is_string=False)
@classmethod
def string(cls, string) -> Literal:
1807    @classmethod
1808    def string(cls, string) -> Literal:
1809        return cls(this=str(string), is_string=True)
output_name: str

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 = 'literal'
class Join(Expression):
1816class Join(Expression):
1817    arg_types = {
1818        "this": True,
1819        "on": False,
1820        "side": False,
1821        "kind": False,
1822        "using": False,
1823        "method": False,
1824        "global": False,
1825        "hint": False,
1826    }
1827
1828    @property
1829    def method(self) -> str:
1830        return self.text("method").upper()
1831
1832    @property
1833    def kind(self) -> str:
1834        return self.text("kind").upper()
1835
1836    @property
1837    def side(self) -> str:
1838        return self.text("side").upper()
1839
1840    @property
1841    def hint(self) -> str:
1842        return self.text("hint").upper()
1843
1844    @property
1845    def alias_or_name(self) -> str:
1846        return self.this.alias_or_name
1847
1848    def on(
1849        self,
1850        *expressions: t.Optional[ExpOrStr],
1851        append: bool = True,
1852        dialect: DialectType = None,
1853        copy: bool = True,
1854        **opts,
1855    ) -> Join:
1856        """
1857        Append to or set the ON expressions.
1858
1859        Example:
1860            >>> import sqlglot
1861            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
1862            'JOIN x ON y = 1'
1863
1864        Args:
1865            *expressions: the SQL code strings to parse.
1866                If an `Expression` instance is passed, it will be used as-is.
1867                Multiple expressions are combined with an AND operator.
1868            append: if `True`, AND the new expressions to any existing expression.
1869                Otherwise, this resets the expression.
1870            dialect: the dialect used to parse the input expressions.
1871            copy: if `False`, modify this expression instance in-place.
1872            opts: other options to use to parse the input expressions.
1873
1874        Returns:
1875            The modified Join expression.
1876        """
1877        join = _apply_conjunction_builder(
1878            *expressions,
1879            instance=self,
1880            arg="on",
1881            append=append,
1882            dialect=dialect,
1883            copy=copy,
1884            **opts,
1885        )
1886
1887        if join.kind == "CROSS":
1888            join.set("kind", None)
1889
1890        return join
1891
1892    def using(
1893        self,
1894        *expressions: t.Optional[ExpOrStr],
1895        append: bool = True,
1896        dialect: DialectType = None,
1897        copy: bool = True,
1898        **opts,
1899    ) -> Join:
1900        """
1901        Append to or set the USING expressions.
1902
1903        Example:
1904            >>> import sqlglot
1905            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
1906            'JOIN x USING (foo, bla)'
1907
1908        Args:
1909            *expressions: the SQL code strings to parse.
1910                If an `Expression` instance is passed, it will be used as-is.
1911            append: if `True`, concatenate the new expressions to the existing "using" list.
1912                Otherwise, this resets the expression.
1913            dialect: the dialect used to parse the input expressions.
1914            copy: if `False`, modify this expression instance in-place.
1915            opts: other options to use to parse the input expressions.
1916
1917        Returns:
1918            The modified Join expression.
1919        """
1920        join = _apply_list_builder(
1921            *expressions,
1922            instance=self,
1923            arg="using",
1924            append=append,
1925            dialect=dialect,
1926            copy=copy,
1927            **opts,
1928        )
1929
1930        if join.kind == "CROSS":
1931            join.set("kind", None)
1932
1933        return join
arg_types = {'this': True, 'on': False, 'side': False, 'kind': False, 'using': False, 'method': False, 'global': False, 'hint': False}
method: str
kind: str
side: str
hint: str
alias_or_name: str
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:
1848    def on(
1849        self,
1850        *expressions: t.Optional[ExpOrStr],
1851        append: bool = True,
1852        dialect: DialectType = None,
1853        copy: bool = True,
1854        **opts,
1855    ) -> Join:
1856        """
1857        Append to or set the ON expressions.
1858
1859        Example:
1860            >>> import sqlglot
1861            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
1862            'JOIN x ON y = 1'
1863
1864        Args:
1865            *expressions: the SQL code strings to parse.
1866                If an `Expression` instance is passed, it will be used as-is.
1867                Multiple expressions are combined with an AND operator.
1868            append: if `True`, AND the new expressions to any existing expression.
1869                Otherwise, this resets the expression.
1870            dialect: the dialect used to parse the input expressions.
1871            copy: if `False`, modify this expression instance in-place.
1872            opts: other options to use to parse the input expressions.
1873
1874        Returns:
1875            The modified Join expression.
1876        """
1877        join = _apply_conjunction_builder(
1878            *expressions,
1879            instance=self,
1880            arg="on",
1881            append=append,
1882            dialect=dialect,
1883            copy=copy,
1884            **opts,
1885        )
1886
1887        if join.kind == "CROSS":
1888            join.set("kind", None)
1889
1890        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:
1892    def using(
1893        self,
1894        *expressions: t.Optional[ExpOrStr],
1895        append: bool = True,
1896        dialect: DialectType = None,
1897        copy: bool = True,
1898        **opts,
1899    ) -> Join:
1900        """
1901        Append to or set the USING expressions.
1902
1903        Example:
1904            >>> import sqlglot
1905            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
1906            'JOIN x USING (foo, bla)'
1907
1908        Args:
1909            *expressions: the SQL code strings to parse.
1910                If an `Expression` instance is passed, it will be used as-is.
1911            append: if `True`, concatenate the new expressions to the existing "using" list.
1912                Otherwise, this resets the expression.
1913            dialect: the dialect used to parse the input expressions.
1914            copy: if `False`, modify this expression instance in-place.
1915            opts: other options to use to parse the input expressions.
1916
1917        Returns:
1918            The modified Join expression.
1919        """
1920        join = _apply_list_builder(
1921            *expressions,
1922            instance=self,
1923            arg="using",
1924            append=append,
1925            dialect=dialect,
1926            copy=copy,
1927            **opts,
1928        )
1929
1930        if join.kind == "CROSS":
1931            join.set("kind", None)
1932
1933        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):
1936class Lateral(UDTF):
1937    arg_types = {"this": True, "view": False, "outer": False, "alias": False}
arg_types = {'this': True, 'view': False, 'outer': False, 'alias': False}
key = 'lateral'
class MatchRecognize(Expression):
1940class MatchRecognize(Expression):
1941    arg_types = {
1942        "partition_by": False,
1943        "order": False,
1944        "measures": False,
1945        "rows": False,
1946        "after": False,
1947        "pattern": False,
1948        "define": False,
1949        "alias": False,
1950    }
arg_types = {'partition_by': False, 'order': False, 'measures': False, 'rows': False, 'after': False, 'pattern': False, 'define': False, 'alias': False}
key = 'matchrecognize'
class Final(Expression):
1955class Final(Expression):
1956    pass
key = 'final'
class Offset(Expression):
1959class Offset(Expression):
1960    arg_types = {"this": False, "expression": True}
arg_types = {'this': False, 'expression': True}
key = 'offset'
class Order(Expression):
1963class Order(Expression):
1964    arg_types = {"this": False, "expressions": True}
arg_types = {'this': False, 'expressions': True}
key = 'order'
class Cluster(Order):
1969class Cluster(Order):
1970    pass
key = 'cluster'
class Distribute(Order):
1973class Distribute(Order):
1974    pass
key = 'distribute'
class Sort(Order):
1977class Sort(Order):
1978    pass
key = 'sort'
class Ordered(Expression):
1981class Ordered(Expression):
1982    arg_types = {"this": True, "desc": False, "nulls_first": True}
arg_types = {'this': True, 'desc': False, 'nulls_first': True}
key = 'ordered'
class Property(Expression):
1985class Property(Expression):
1986    arg_types = {"this": True, "value": True}
arg_types = {'this': True, 'value': True}
key = 'property'
class AlgorithmProperty(Property):
1989class AlgorithmProperty(Property):
1990    arg_types = {"this": True}
arg_types = {'this': True}
key = 'algorithmproperty'
class AutoIncrementProperty(Property):
1993class AutoIncrementProperty(Property):
1994    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autoincrementproperty'
class BlockCompressionProperty(Property):
1997class BlockCompressionProperty(Property):
1998    arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
arg_types = {'autotemp': False, 'always': False, 'default': True, 'manual': True, 'never': True}
key = 'blockcompressionproperty'
class CharacterSetProperty(Property):
2001class CharacterSetProperty(Property):
2002    arg_types = {"this": True, "default": True}
arg_types = {'this': True, 'default': True}
key = 'charactersetproperty'
class ChecksumProperty(Property):
2005class ChecksumProperty(Property):
2006    arg_types = {"on": False, "default": False}
arg_types = {'on': False, 'default': False}
key = 'checksumproperty'
class CollateProperty(Property):
2009class CollateProperty(Property):
2010    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'collateproperty'
class CopyGrantsProperty(Property):
2013class CopyGrantsProperty(Property):
2014    arg_types = {}
arg_types = {}
key = 'copygrantsproperty'
class DataBlocksizeProperty(Property):
2017class DataBlocksizeProperty(Property):
2018    arg_types = {
2019        "size": False,
2020        "units": False,
2021        "minimum": False,
2022        "maximum": False,
2023        "default": False,
2024    }
arg_types = {'size': False, 'units': False, 'minimum': False, 'maximum': False, 'default': False}
key = 'datablocksizeproperty'
class DefinerProperty(Property):
2027class DefinerProperty(Property):
2028    arg_types = {"this": True}
arg_types = {'this': True}
key = 'definerproperty'
class DistKeyProperty(Property):
2031class DistKeyProperty(Property):
2032    arg_types = {"this": True}
arg_types = {'this': True}
key = 'distkeyproperty'
class DistStyleProperty(Property):
2035class DistStyleProperty(Property):
2036    arg_types = {"this": True}
arg_types = {'this': True}
key = 'diststyleproperty'
class EngineProperty(Property):
2039class EngineProperty(Property):
2040    arg_types = {"this": True}
arg_types = {'this': True}
key = 'engineproperty'
class HeapProperty(Property):
2043class HeapProperty(Property):
2044    arg_types = {}
arg_types = {}
key = 'heapproperty'
class ToTableProperty(Property):
2047class ToTableProperty(Property):
2048    arg_types = {"this": True}
arg_types = {'this': True}
key = 'totableproperty'
class ExecuteAsProperty(Property):
2051class ExecuteAsProperty(Property):
2052    arg_types = {"this": True}
arg_types = {'this': True}
key = 'executeasproperty'
class ExternalProperty(Property):
2055class ExternalProperty(Property):
2056    arg_types = {"this": False}
arg_types = {'this': False}
key = 'externalproperty'
class FallbackProperty(Property):
2059class FallbackProperty(Property):
2060    arg_types = {"no": True, "protection": False}
arg_types = {'no': True, 'protection': False}
key = 'fallbackproperty'
class FileFormatProperty(Property):
2063class FileFormatProperty(Property):
2064    arg_types = {"this": True}
arg_types = {'this': True}
key = 'fileformatproperty'
class FreespaceProperty(Property):
2067class FreespaceProperty(Property):
2068    arg_types = {"this": True, "percent": False}
arg_types = {'this': True, 'percent': False}
key = 'freespaceproperty'
class InputModelProperty(Property):
2071class InputModelProperty(Property):
2072    arg_types = {"this": True}
arg_types = {'this': True}
key = 'inputmodelproperty'
class OutputModelProperty(Property):
2075class OutputModelProperty(Property):
2076    arg_types = {"this": True}
arg_types = {'this': True}
key = 'outputmodelproperty'
class IsolatedLoadingProperty(Property):
2079class IsolatedLoadingProperty(Property):
2080    arg_types = {
2081        "no": True,
2082        "concurrent": True,
2083        "for_all": True,
2084        "for_insert": True,
2085        "for_none": True,
2086    }
arg_types = {'no': True, 'concurrent': True, 'for_all': True, 'for_insert': True, 'for_none': True}
key = 'isolatedloadingproperty'
class JournalProperty(Property):
2089class JournalProperty(Property):
2090    arg_types = {
2091        "no": False,
2092        "dual": False,
2093        "before": False,
2094        "local": False,
2095        "after": False,
2096    }
arg_types = {'no': False, 'dual': False, 'before': False, 'local': False, 'after': False}
key = 'journalproperty'
class LanguageProperty(Property):
2099class LanguageProperty(Property):
2100    arg_types = {"this": True}
arg_types = {'this': True}
key = 'languageproperty'
class ClusteredByProperty(Property):
2104class ClusteredByProperty(Property):
2105    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
arg_types = {'expressions': True, 'sorted_by': False, 'buckets': True}
key = 'clusteredbyproperty'
class DictProperty(Property):
2108class DictProperty(Property):
2109    arg_types = {"this": True, "kind": True, "settings": False}
arg_types = {'this': True, 'kind': True, 'settings': False}
key = 'dictproperty'
class DictSubProperty(Property):
2112class DictSubProperty(Property):
2113    pass
key = 'dictsubproperty'
class DictRange(Property):
2116class DictRange(Property):
2117    arg_types = {"this": True, "min": True, "max": True}
arg_types = {'this': True, 'min': True, 'max': True}
key = 'dictrange'
class OnCluster(Property):
2122class OnCluster(Property):
2123    arg_types = {"this": True}
arg_types = {'this': True}
key = 'oncluster'
class LikeProperty(Property):
2126class LikeProperty(Property):
2127    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'likeproperty'
class LocationProperty(Property):
2130class LocationProperty(Property):
2131    arg_types = {"this": True}
arg_types = {'this': True}
key = 'locationproperty'
class LockingProperty(Property):
2134class LockingProperty(Property):
2135    arg_types = {
2136        "this": False,
2137        "kind": True,
2138        "for_or_in": False,
2139        "lock_type": True,
2140        "override": False,
2141    }
arg_types = {'this': False, 'kind': True, 'for_or_in': False, 'lock_type': True, 'override': False}
key = 'lockingproperty'
class LogProperty(Property):
2144class LogProperty(Property):
2145    arg_types = {"no": True}
arg_types = {'no': True}
key = 'logproperty'
class MaterializedProperty(Property):
2148class MaterializedProperty(Property):
2149    arg_types = {"this": False}
arg_types = {'this': False}
key = 'materializedproperty'
class MergeBlockRatioProperty(Property):
2152class MergeBlockRatioProperty(Property):
2153    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):
2156class NoPrimaryIndexProperty(Property):
2157    arg_types = {}
arg_types = {}
key = 'noprimaryindexproperty'
class OnProperty(Property):
2160class OnProperty(Property):
2161    arg_types = {"this": True}
arg_types = {'this': True}
key = 'onproperty'
class OnCommitProperty(Property):
2164class OnCommitProperty(Property):
2165    arg_types = {"delete": False}
arg_types = {'delete': False}
key = 'oncommitproperty'
class PartitionedByProperty(Property):
2168class PartitionedByProperty(Property):
2169    arg_types = {"this": True}
arg_types = {'this': True}
key = 'partitionedbyproperty'
class PartitionBoundSpec(Expression):
2173class PartitionBoundSpec(Expression):
2174    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
2175    arg_types = {
2176        "this": False,
2177        "expression": False,
2178        "from_expressions": False,
2179        "to_expressions": False,
2180    }
arg_types = {'this': False, 'expression': False, 'from_expressions': False, 'to_expressions': False}
key = 'partitionboundspec'
class PartitionedOfProperty(Property):
2183class PartitionedOfProperty(Property):
2184    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
2185    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionedofproperty'
class RemoteWithConnectionModelProperty(Property):
2188class RemoteWithConnectionModelProperty(Property):
2189    arg_types = {"this": True}
arg_types = {'this': True}
key = 'remotewithconnectionmodelproperty'
class ReturnsProperty(Property):
2192class ReturnsProperty(Property):
2193    arg_types = {"this": True, "is_table": False, "table": False}
arg_types = {'this': True, 'is_table': False, 'table': False}
key = 'returnsproperty'
class RowFormatProperty(Property):
2196class RowFormatProperty(Property):
2197    arg_types = {"this": True}
arg_types = {'this': True}
key = 'rowformatproperty'
class RowFormatDelimitedProperty(Property):
2200class RowFormatDelimitedProperty(Property):
2201    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
2202    arg_types = {
2203        "fields": False,
2204        "escaped": False,
2205        "collection_items": False,
2206        "map_keys": False,
2207        "lines": False,
2208        "null": False,
2209        "serde": False,
2210    }
arg_types = {'fields': False, 'escaped': False, 'collection_items': False, 'map_keys': False, 'lines': False, 'null': False, 'serde': False}
key = 'rowformatdelimitedproperty'
class RowFormatSerdeProperty(Property):
2213class RowFormatSerdeProperty(Property):
2214    arg_types = {"this": True, "serde_properties": False}
arg_types = {'this': True, 'serde_properties': False}
key = 'rowformatserdeproperty'
class QueryTransform(Expression):
2218class QueryTransform(Expression):
2219    arg_types = {
2220        "expressions": True,
2221        "command_script": True,
2222        "schema": False,
2223        "row_format_before": False,
2224        "record_writer": False,
2225        "row_format_after": False,
2226        "record_reader": False,
2227    }
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):
2230class SampleProperty(Property):
2231    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sampleproperty'
class SchemaCommentProperty(Property):
2234class SchemaCommentProperty(Property):
2235    arg_types = {"this": True}
arg_types = {'this': True}
key = 'schemacommentproperty'
class SerdeProperties(Property):
2238class SerdeProperties(Property):
2239    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'serdeproperties'
class SetProperty(Property):
2242class SetProperty(Property):
2243    arg_types = {"multi": True}
arg_types = {'multi': True}
key = 'setproperty'
class SettingsProperty(Property):
2246class SettingsProperty(Property):
2247    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'settingsproperty'
class SortKeyProperty(Property):
2250class SortKeyProperty(Property):
2251    arg_types = {"this": True, "compound": False}
arg_types = {'this': True, 'compound': False}
key = 'sortkeyproperty'
class SqlSecurityProperty(Property):
2254class SqlSecurityProperty(Property):
2255    arg_types = {"definer": True}
arg_types = {'definer': True}
key = 'sqlsecurityproperty'
class StabilityProperty(Property):
2258class StabilityProperty(Property):
2259    arg_types = {"this": True}
arg_types = {'this': True}
key = 'stabilityproperty'
class TemporaryProperty(Property):
2262class TemporaryProperty(Property):
2263    arg_types = {}
arg_types = {}
key = 'temporaryproperty'
class TransformModelProperty(Property):
2266class TransformModelProperty(Property):
2267    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'transformmodelproperty'
class TransientProperty(Property):
2270class TransientProperty(Property):
2271    arg_types = {"this": False}
arg_types = {'this': False}
key = 'transientproperty'
class VolatileProperty(Property):
2274class VolatileProperty(Property):
2275    arg_types = {"this": False}
arg_types = {'this': False}
key = 'volatileproperty'
class WithDataProperty(Property):
2278class WithDataProperty(Property):
2279    arg_types = {"no": True, "statistics": False}
arg_types = {'no': True, 'statistics': False}
key = 'withdataproperty'
class WithJournalTableProperty(Property):
2282class WithJournalTableProperty(Property):
2283    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withjournaltableproperty'
class WithSystemVersioningProperty(Property):
2286class WithSystemVersioningProperty(Property):
2287    # this -> history table name, expression -> data consistency check
2288    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'withsystemversioningproperty'
class Properties(Expression):
2291class Properties(Expression):
2292    arg_types = {"expressions": True}
2293
2294    NAME_TO_PROPERTY = {
2295        "ALGORITHM": AlgorithmProperty,
2296        "AUTO_INCREMENT": AutoIncrementProperty,
2297        "CHARACTER SET": CharacterSetProperty,
2298        "CLUSTERED_BY": ClusteredByProperty,
2299        "COLLATE": CollateProperty,
2300        "COMMENT": SchemaCommentProperty,
2301        "DEFINER": DefinerProperty,
2302        "DISTKEY": DistKeyProperty,
2303        "DISTSTYLE": DistStyleProperty,
2304        "ENGINE": EngineProperty,
2305        "EXECUTE AS": ExecuteAsProperty,
2306        "FORMAT": FileFormatProperty,
2307        "LANGUAGE": LanguageProperty,
2308        "LOCATION": LocationProperty,
2309        "PARTITIONED_BY": PartitionedByProperty,
2310        "RETURNS": ReturnsProperty,
2311        "ROW_FORMAT": RowFormatProperty,
2312        "SORTKEY": SortKeyProperty,
2313    }
2314
2315    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
2316
2317    # CREATE property locations
2318    # Form: schema specified
2319    #   create [POST_CREATE]
2320    #     table a [POST_NAME]
2321    #     (b int) [POST_SCHEMA]
2322    #     with ([POST_WITH])
2323    #     index (b) [POST_INDEX]
2324    #
2325    # Form: alias selection
2326    #   create [POST_CREATE]
2327    #     table a [POST_NAME]
2328    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
2329    #     index (c) [POST_INDEX]
2330    class Location(AutoName):
2331        POST_CREATE = auto()
2332        POST_NAME = auto()
2333        POST_SCHEMA = auto()
2334        POST_WITH = auto()
2335        POST_ALIAS = auto()
2336        POST_EXPRESSION = auto()
2337        POST_INDEX = auto()
2338        UNSUPPORTED = auto()
2339
2340    @classmethod
2341    def from_dict(cls, properties_dict: t.Dict) -> Properties:
2342        expressions = []
2343        for key, value in properties_dict.items():
2344            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
2345            if property_cls:
2346                expressions.append(property_cls(this=convert(value)))
2347            else:
2348                expressions.append(Property(this=Literal.string(key), value=convert(value)))
2349
2350        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'>, 'DISTSTYLE': <class 'DistStyleProperty'>, 'ENGINE': <class 'EngineProperty'>, 'EXECUTE AS': <class 'ExecuteAsProperty'>, 'FORMAT': <class 'FileFormatProperty'>, 'LANGUAGE': <class 'LanguageProperty'>, 'LOCATION': <class 'LocationProperty'>, 'PARTITIONED_BY': <class 'PartitionedByProperty'>, 'RETURNS': <class 'ReturnsProperty'>, 'ROW_FORMAT': <class 'RowFormatProperty'>, 'SORTKEY': <class 'SortKeyProperty'>}
PROPERTY_TO_NAME = {<class 'AlgorithmProperty'>: 'ALGORITHM', <class 'AutoIncrementProperty'>: 'AUTO_INCREMENT', <class 'CharacterSetProperty'>: 'CHARACTER SET', <class 'ClusteredByProperty'>: 'CLUSTERED_BY', <class 'CollateProperty'>: 'COLLATE', <class 'SchemaCommentProperty'>: 'COMMENT', <class 'DefinerProperty'>: 'DEFINER', <class 'DistKeyProperty'>: 'DISTKEY', <class 'DistStyleProperty'>: 'DISTSTYLE', <class 'EngineProperty'>: 'ENGINE', <class 'ExecuteAsProperty'>: 'EXECUTE AS', <class 'FileFormatProperty'>: 'FORMAT', <class 'LanguageProperty'>: 'LANGUAGE', <class 'LocationProperty'>: 'LOCATION', <class 'PartitionedByProperty'>: 'PARTITIONED_BY', <class 'ReturnsProperty'>: 'RETURNS', <class 'RowFormatProperty'>: 'ROW_FORMAT', <class 'SortKeyProperty'>: 'SORTKEY'}
@classmethod
def from_dict(cls, properties_dict: Dict) -> Properties:
2340    @classmethod
2341    def from_dict(cls, properties_dict: t.Dict) -> Properties:
2342        expressions = []
2343        for key, value in properties_dict.items():
2344            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
2345            if property_cls:
2346                expressions.append(property_cls(this=convert(value)))
2347            else:
2348                expressions.append(Property(this=Literal.string(key), value=convert(value)))
2349
2350        return cls(expressions=expressions)
key = 'properties'
class Properties.Location(sqlglot.helper.AutoName):
2330    class Location(AutoName):
2331        POST_CREATE = auto()
2332        POST_NAME = auto()
2333        POST_SCHEMA = auto()
2334        POST_WITH = auto()
2335        POST_ALIAS = auto()
2336        POST_EXPRESSION = auto()
2337        POST_INDEX = auto()
2338        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'>
Inherited Members
enum.Enum
name
value
class Qualify(Expression):
2353class Qualify(Expression):
2354    pass
key = 'qualify'
class InputOutputFormat(Expression):
2357class InputOutputFormat(Expression):
2358    arg_types = {"input_format": False, "output_format": False}
arg_types = {'input_format': False, 'output_format': False}
key = 'inputoutputformat'
class Return(Expression):
2362class Return(Expression):
2363    pass
key = 'return'
class Reference(Expression):
2366class Reference(Expression):
2367    arg_types = {"this": True, "expressions": False, "options": False}
arg_types = {'this': True, 'expressions': False, 'options': False}
key = 'reference'
class Tuple(Expression):
2370class Tuple(Expression):
2371    arg_types = {"expressions": False}
2372
2373    def isin(
2374        self,
2375        *expressions: t.Any,
2376        query: t.Optional[ExpOrStr] = None,
2377        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
2378        copy: bool = True,
2379        **opts,
2380    ) -> In:
2381        return In(
2382            this=maybe_copy(self, copy),
2383            expressions=[convert(e, copy=copy) for e in expressions],
2384            query=maybe_parse(query, copy=copy, **opts) if query else None,
2385            unnest=Unnest(
2386                expressions=[
2387                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
2388                ]
2389            )
2390            if unnest
2391            else None,
2392        )
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:
2373    def isin(
2374        self,
2375        *expressions: t.Any,
2376        query: t.Optional[ExpOrStr] = None,
2377        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
2378        copy: bool = True,
2379        **opts,
2380    ) -> In:
2381        return In(
2382            this=maybe_copy(self, copy),
2383            expressions=[convert(e, copy=copy) for e in expressions],
2384            query=maybe_parse(query, copy=copy, **opts) if query else None,
2385            unnest=Unnest(
2386                expressions=[
2387                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
2388                ]
2389            )
2390            if unnest
2391            else None,
2392        )
key = 'tuple'
class Subqueryable(Unionable):
2395class Subqueryable(Unionable):
2396    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
2397        """
2398        Convert this expression to an aliased expression that can be used as a Subquery.
2399
2400        Example:
2401            >>> subquery = Select().select("x").from_("tbl").subquery()
2402            >>> Select().select("x").from_(subquery).sql()
2403            'SELECT x FROM (SELECT x FROM tbl)'
2404
2405        Args:
2406            alias (str | Identifier): an optional alias for the subquery
2407            copy (bool): if `False`, modify this expression instance in-place.
2408
2409        Returns:
2410            Alias: the subquery
2411        """
2412        instance = maybe_copy(self, copy)
2413        if not isinstance(alias, Expression):
2414            alias = TableAlias(this=to_identifier(alias)) if alias else None
2415
2416        return Subquery(this=instance, alias=alias)
2417
2418    def limit(
2419        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2420    ) -> Select:
2421        raise NotImplementedError
2422
2423    @property
2424    def ctes(self):
2425        with_ = self.args.get("with")
2426        if not with_:
2427            return []
2428        return with_.expressions
2429
2430    @property
2431    def selects(self) -> t.List[Expression]:
2432        raise NotImplementedError("Subqueryable objects must implement `selects`")
2433
2434    @property
2435    def named_selects(self) -> t.List[str]:
2436        raise NotImplementedError("Subqueryable objects must implement `named_selects`")
2437
2438    def select(
2439        self,
2440        *expressions: t.Optional[ExpOrStr],
2441        append: bool = True,
2442        dialect: DialectType = None,
2443        copy: bool = True,
2444        **opts,
2445    ) -> Subqueryable:
2446        raise NotImplementedError("Subqueryable objects must implement `select`")
2447
2448    def with_(
2449        self,
2450        alias: ExpOrStr,
2451        as_: ExpOrStr,
2452        recursive: t.Optional[bool] = None,
2453        append: bool = True,
2454        dialect: DialectType = None,
2455        copy: bool = True,
2456        **opts,
2457    ) -> Subqueryable:
2458        """
2459        Append to or set the common table expressions.
2460
2461        Example:
2462            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
2463            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
2464
2465        Args:
2466            alias: the SQL code string to parse as the table name.
2467                If an `Expression` instance is passed, this is used as-is.
2468            as_: the SQL code string to parse as the table expression.
2469                If an `Expression` instance is passed, it will be used as-is.
2470            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2471            append: if `True`, add to any existing expressions.
2472                Otherwise, this resets the expressions.
2473            dialect: the dialect used to parse the input expression.
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 expression.
2479        """
2480        return _apply_cte_builder(
2481            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
2482        )
def subquery( self, alias: Union[str, Expression, NoneType] = None, copy: bool = True) -> Subquery:
2396    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
2397        """
2398        Convert this expression to an aliased expression that can be used as a Subquery.
2399
2400        Example:
2401            >>> subquery = Select().select("x").from_("tbl").subquery()
2402            >>> Select().select("x").from_(subquery).sql()
2403            'SELECT x FROM (SELECT x FROM tbl)'
2404
2405        Args:
2406            alias (str | Identifier): an optional alias for the subquery
2407            copy (bool): if `False`, modify this expression instance in-place.
2408
2409        Returns:
2410            Alias: the subquery
2411        """
2412        instance = maybe_copy(self, copy)
2413        if not isinstance(alias, Expression):
2414            alias = TableAlias(this=to_identifier(alias)) if alias else None
2415
2416        return Subquery(this=instance, alias=alias)

Convert this expression to an aliased expression that can be used as a Subquery.

Example:
>>> subquery = Select().select("x").from_("tbl").subquery()
>>> Select().select("x").from_(subquery).sql()
'SELECT x FROM (SELECT x FROM tbl)'
Arguments:
  • alias (str | Identifier): an optional alias for the subquery
  • copy (bool): if False, modify this expression instance in-place.
Returns:

Alias: the subquery

def limit( self, expression: Union[str, Expression, int], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
2418    def limit(
2419        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2420    ) -> Select:
2421        raise NotImplementedError
ctes
selects: List[Expression]
named_selects: List[str]
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) -> Subqueryable:
2438    def select(
2439        self,
2440        *expressions: t.Optional[ExpOrStr],
2441        append: bool = True,
2442        dialect: DialectType = None,
2443        copy: bool = True,
2444        **opts,
2445    ) -> Subqueryable:
2446        raise NotImplementedError("Subqueryable objects must implement `select`")
def with_( self, alias: Union[str, Expression], as_: Union[str, Expression], recursive: Optional[bool] = None, append: bool = True, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Subqueryable:
2448    def with_(
2449        self,
2450        alias: ExpOrStr,
2451        as_: ExpOrStr,
2452        recursive: t.Optional[bool] = None,
2453        append: bool = True,
2454        dialect: DialectType = None,
2455        copy: bool = True,
2456        **opts,
2457    ) -> Subqueryable:
2458        """
2459        Append to or set the common table expressions.
2460
2461        Example:
2462            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
2463            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
2464
2465        Args:
2466            alias: the SQL code string to parse as the table name.
2467                If an `Expression` instance is passed, this is used as-is.
2468            as_: the SQL code string to parse as the table expression.
2469                If an `Expression` instance is passed, it will be used as-is.
2470            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2471            append: if `True`, add to any existing expressions.
2472                Otherwise, this resets the expressions.
2473            dialect: the dialect used to parse the input expression.
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 expression.
2479        """
2480        return _apply_cte_builder(
2481            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
2482        )

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.
  • 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 = 'subqueryable'
QUERY_MODIFIERS = {'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': 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}
class WithTableHint(Expression):
2510class WithTableHint(Expression):
2511    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withtablehint'
class IndexTableHint(Expression):
2515class IndexTableHint(Expression):
2516    arg_types = {"this": True, "expressions": False, "target": False}
arg_types = {'this': True, 'expressions': False, 'target': False}
key = 'indextablehint'
class HistoricalData(Expression):
2520class HistoricalData(Expression):
2521    arg_types = {"this": True, "kind": True, "expression": True}
arg_types = {'this': True, 'kind': True, 'expression': True}
key = 'historicaldata'
class Table(Expression):
2524class Table(Expression):
2525    arg_types = {
2526        "this": True,
2527        "alias": False,
2528        "db": False,
2529        "catalog": False,
2530        "laterals": False,
2531        "joins": False,
2532        "pivots": False,
2533        "hints": False,
2534        "system_time": False,
2535        "version": False,
2536        "format": False,
2537        "pattern": False,
2538        "index": False,
2539        "ordinality": False,
2540        "when": False,
2541    }
2542
2543    @property
2544    def name(self) -> str:
2545        if isinstance(self.this, Func):
2546            return ""
2547        return self.this.name
2548
2549    @property
2550    def db(self) -> str:
2551        return self.text("db")
2552
2553    @property
2554    def catalog(self) -> str:
2555        return self.text("catalog")
2556
2557    @property
2558    def selects(self) -> t.List[Expression]:
2559        return []
2560
2561    @property
2562    def named_selects(self) -> t.List[str]:
2563        return []
2564
2565    @property
2566    def parts(self) -> t.List[Expression]:
2567        """Return the parts of a table in order catalog, db, table."""
2568        parts: t.List[Expression] = []
2569
2570        for arg in ("catalog", "db", "this"):
2571            part = self.args.get(arg)
2572
2573            if isinstance(part, Dot):
2574                parts.extend(part.flatten())
2575            elif isinstance(part, Expression):
2576                parts.append(part)
2577
2578        return parts
arg_types = {'this': True, 'alias': False, 'db': False, 'catalog': False, 'laterals': False, 'joins': False, 'pivots': False, 'hints': False, 'system_time': False, 'version': False, 'format': False, 'pattern': False, 'index': False, 'ordinality': False, 'when': False}
name: str
db: str
catalog: str
selects: List[Expression]
named_selects: List[str]
parts: List[Expression]

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

key = 'table'
class Union(Subqueryable):
2581class Union(Subqueryable):
2582    arg_types = {
2583        "with": False,
2584        "this": True,
2585        "expression": True,
2586        "distinct": False,
2587        "by_name": False,
2588        **QUERY_MODIFIERS,
2589    }
2590
2591    def limit(
2592        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2593    ) -> Select:
2594        """
2595        Set the LIMIT expression.
2596
2597        Example:
2598            >>> select("1").union(select("1")).limit(1).sql()
2599            'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1'
2600
2601        Args:
2602            expression: the SQL code string to parse.
2603                This can also be an integer.
2604                If a `Limit` instance is passed, this is used as-is.
2605                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2606            dialect: the dialect used to parse the input expression.
2607            copy: if `False`, modify this expression instance in-place.
2608            opts: other options to use to parse the input expressions.
2609
2610        Returns:
2611            The limited subqueryable.
2612        """
2613        return (
2614            select("*")
2615            .from_(self.subquery(alias="_l_0", copy=copy))
2616            .limit(expression, dialect=dialect, copy=False, **opts)
2617        )
2618
2619    def select(
2620        self,
2621        *expressions: t.Optional[ExpOrStr],
2622        append: bool = True,
2623        dialect: DialectType = None,
2624        copy: bool = True,
2625        **opts,
2626    ) -> Union:
2627        """Append to or set the SELECT of the union recursively.
2628
2629        Example:
2630            >>> from sqlglot import parse_one
2631            >>> parse_one("select a from x union select a from y union select a from z").select("b").sql()
2632            'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z'
2633
2634        Args:
2635            *expressions: the SQL code strings to parse.
2636                If an `Expression` instance is passed, it will be used as-is.
2637            append: if `True`, add to any existing expressions.
2638                Otherwise, this resets the expressions.
2639            dialect: the dialect used to parse the input expressions.
2640            copy: if `False`, modify this expression instance in-place.
2641            opts: other options to use to parse the input expressions.
2642
2643        Returns:
2644            Union: the modified expression.
2645        """
2646        this = self.copy() if copy else self
2647        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
2648        this.expression.unnest().select(
2649            *expressions, append=append, dialect=dialect, copy=False, **opts
2650        )
2651        return this
2652
2653    @property
2654    def named_selects(self) -> t.List[str]:
2655        return self.this.unnest().named_selects
2656
2657    @property
2658    def is_star(self) -> bool:
2659        return self.this.is_star or self.expression.is_star
2660
2661    @property
2662    def selects(self) -> t.List[Expression]:
2663        return self.this.unnest().selects
2664
2665    @property
2666    def left(self) -> Expression:
2667        return self.this
2668
2669    @property
2670    def right(self) -> Expression:
2671        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, '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}
def limit( self, expression: Union[str, Expression, int], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
2591    def limit(
2592        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2593    ) -> Select:
2594        """
2595        Set the LIMIT expression.
2596
2597        Example:
2598            >>> select("1").union(select("1")).limit(1).sql()
2599            'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1'
2600
2601        Args:
2602            expression: the SQL code string to parse.
2603                This can also be an integer.
2604                If a `Limit` instance is passed, this is used as-is.
2605                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2606            dialect: the dialect used to parse the input expression.
2607            copy: if `False`, modify this expression instance in-place.
2608            opts: other options to use to parse the input expressions.
2609
2610        Returns:
2611            The limited subqueryable.
2612        """
2613        return (
2614            select("*")
2615            .from_(self.subquery(alias="_l_0", copy=copy))
2616            .limit(expression, dialect=dialect, copy=False, **opts)
2617        )

Set the LIMIT expression.

Example:
>>> select("1").union(select("1")).limit(1).sql()
'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1'
Arguments:
  • expression: the SQL code string to parse. This can also be an integer. If a Limit instance is passed, this is 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:

The limited subqueryable.

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) -> Union:
2619    def select(
2620        self,
2621        *expressions: t.Optional[ExpOrStr],
2622        append: bool = True,
2623        dialect: DialectType = None,
2624        copy: bool = True,
2625        **opts,
2626    ) -> Union:
2627        """Append to or set the SELECT of the union recursively.
2628
2629        Example:
2630            >>> from sqlglot import parse_one
2631            >>> parse_one("select a from x union select a from y union select a from z").select("b").sql()
2632            'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z'
2633
2634        Args:
2635            *expressions: the SQL code strings to parse.
2636                If an `Expression` instance is passed, it will be used as-is.
2637            append: if `True`, add to any existing expressions.
2638                Otherwise, this resets the expressions.
2639            dialect: the dialect used to parse the input expressions.
2640            copy: if `False`, modify this expression instance in-place.
2641            opts: other options to use to parse the input expressions.
2642
2643        Returns:
2644            Union: the modified expression.
2645        """
2646        this = self.copy() if copy else self
2647        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
2648        this.expression.unnest().select(
2649            *expressions, append=append, dialect=dialect, copy=False, **opts
2650        )
2651        return this

Append to or set the SELECT of the union recursively.

Example:
>>> from sqlglot import parse_one
>>> parse_one("select a from x union select a from y union select a from z").select("b").sql()
'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM 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:

Union: the modified expression.

named_selects: List[str]
is_star: bool

Checks whether an expression is a star.

selects: List[Expression]
left: Expression
right: Expression
key = 'union'
class Except(Union):
2674class Except(Union):
2675    pass
key = 'except'
class Intersect(Union):
2678class Intersect(Union):
2679    pass
key = 'intersect'
class Unnest(UDTF):
2682class Unnest(UDTF):
2683    arg_types = {
2684        "expressions": True,
2685        "alias": False,
2686        "offset": False,
2687    }
arg_types = {'expressions': True, 'alias': False, 'offset': False}
key = 'unnest'
class Update(Expression):
2690class Update(Expression):
2691    arg_types = {
2692        "with": False,
2693        "this": False,
2694        "expressions": True,
2695        "from": False,
2696        "where": False,
2697        "returning": False,
2698        "order": False,
2699        "limit": False,
2700    }
arg_types = {'with': False, 'this': False, 'expressions': True, 'from': False, 'where': False, 'returning': False, 'order': False, 'limit': False}
key = 'update'
class Values(UDTF):
2703class Values(UDTF):
2704    arg_types = {"expressions": True, "alias": False}
arg_types = {'expressions': True, 'alias': False}
key = 'values'
class Var(Expression):
2707class Var(Expression):
2708    pass
key = 'var'
class Version(Expression):
2711class Version(Expression):
2712    """
2713    Time travel, iceberg, bigquery etc
2714    https://trino.io/docs/current/connector/iceberg.html?highlight=snapshot#using-snapshots
2715    https://www.databricks.com/blog/2019/02/04/introducing-delta-time-travel-for-large-scale-data-lakes.html
2716    https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#for_system_time_as_of
2717    https://learn.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-ver16
2718    this is either TIMESTAMP or VERSION
2719    kind is ("AS OF", "BETWEEN")
2720    """
2721
2722    arg_types = {"this": True, "kind": True, "expression": False}
arg_types = {'this': True, 'kind': True, 'expression': False}
key = 'version'
class Schema(Expression):
2725class Schema(Expression):
2726    arg_types = {"this": False, "expressions": False}
arg_types = {'this': False, 'expressions': False}
key = 'schema'
class Lock(Expression):
2731class Lock(Expression):
2732    arg_types = {"update": True, "expressions": False, "wait": False}
arg_types = {'update': True, 'expressions': False, 'wait': False}
key = 'lock'
class Select(Subqueryable):
2735class Select(Subqueryable):
2736    arg_types = {
2737        "with": False,
2738        "kind": False,
2739        "expressions": False,
2740        "hint": False,
2741        "distinct": False,
2742        "into": False,
2743        "from": False,
2744        **QUERY_MODIFIERS,
2745    }
2746
2747    def from_(
2748        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
2749    ) -> Select:
2750        """
2751        Set the FROM expression.
2752
2753        Example:
2754            >>> Select().from_("tbl").select("x").sql()
2755            'SELECT x FROM tbl'
2756
2757        Args:
2758            expression : the SQL code strings to parse.
2759                If a `From` instance is passed, this is used as-is.
2760                If another `Expression` instance is passed, it will be wrapped in a `From`.
2761            dialect: the dialect used to parse the input expression.
2762            copy: if `False`, modify this expression instance in-place.
2763            opts: other options to use to parse the input expressions.
2764
2765        Returns:
2766            The modified Select expression.
2767        """
2768        return _apply_builder(
2769            expression=expression,
2770            instance=self,
2771            arg="from",
2772            into=From,
2773            prefix="FROM",
2774            dialect=dialect,
2775            copy=copy,
2776            **opts,
2777        )
2778
2779    def group_by(
2780        self,
2781        *expressions: t.Optional[ExpOrStr],
2782        append: bool = True,
2783        dialect: DialectType = None,
2784        copy: bool = True,
2785        **opts,
2786    ) -> Select:
2787        """
2788        Set the GROUP BY expression.
2789
2790        Example:
2791            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
2792            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
2793
2794        Args:
2795            *expressions: the SQL code strings to parse.
2796                If a `Group` instance is passed, this is used as-is.
2797                If another `Expression` instance is passed, it will be wrapped in a `Group`.
2798                If nothing is passed in then a group by is not applied to the expression
2799            append: if `True`, add to any existing expressions.
2800                Otherwise, this flattens all the `Group` expression into a single expression.
2801            dialect: the dialect used to parse the input expression.
2802            copy: if `False`, modify this expression instance in-place.
2803            opts: other options to use to parse the input expressions.
2804
2805        Returns:
2806            The modified Select expression.
2807        """
2808        if not expressions:
2809            return self if not copy else self.copy()
2810
2811        return _apply_child_list_builder(
2812            *expressions,
2813            instance=self,
2814            arg="group",
2815            append=append,
2816            copy=copy,
2817            prefix="GROUP BY",
2818            into=Group,
2819            dialect=dialect,
2820            **opts,
2821        )
2822
2823    def order_by(
2824        self,
2825        *expressions: t.Optional[ExpOrStr],
2826        append: bool = True,
2827        dialect: DialectType = None,
2828        copy: bool = True,
2829        **opts,
2830    ) -> Select:
2831        """
2832        Set the ORDER BY expression.
2833
2834        Example:
2835            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
2836            'SELECT x FROM tbl ORDER BY x DESC'
2837
2838        Args:
2839            *expressions: the SQL code strings to parse.
2840                If a `Group` instance is passed, this is used as-is.
2841                If another `Expression` instance is passed, it will be wrapped in a `Order`.
2842            append: if `True`, add to any existing expressions.
2843                Otherwise, this flattens all the `Order` expression into a single expression.
2844            dialect: the dialect used to parse the input expression.
2845            copy: if `False`, modify this expression instance in-place.
2846            opts: other options to use to parse the input expressions.
2847
2848        Returns:
2849            The modified Select expression.
2850        """
2851        return _apply_child_list_builder(
2852            *expressions,
2853            instance=self,
2854            arg="order",
2855            append=append,
2856            copy=copy,
2857            prefix="ORDER BY",
2858            into=Order,
2859            dialect=dialect,
2860            **opts,
2861        )
2862
2863    def sort_by(
2864        self,
2865        *expressions: t.Optional[ExpOrStr],
2866        append: bool = True,
2867        dialect: DialectType = None,
2868        copy: bool = True,
2869        **opts,
2870    ) -> Select:
2871        """
2872        Set the SORT BY expression.
2873
2874        Example:
2875            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
2876            'SELECT x FROM tbl SORT BY x DESC'
2877
2878        Args:
2879            *expressions: the SQL code strings to parse.
2880                If a `Group` instance is passed, this is used as-is.
2881                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
2882            append: if `True`, add to any existing expressions.
2883                Otherwise, this flattens all the `Order` expression into a single expression.
2884            dialect: the dialect used to parse the input expression.
2885            copy: if `False`, modify this expression instance in-place.
2886            opts: other options to use to parse the input expressions.
2887
2888        Returns:
2889            The modified Select expression.
2890        """
2891        return _apply_child_list_builder(
2892            *expressions,
2893            instance=self,
2894            arg="sort",
2895            append=append,
2896            copy=copy,
2897            prefix="SORT BY",
2898            into=Sort,
2899            dialect=dialect,
2900            **opts,
2901        )
2902
2903    def cluster_by(
2904        self,
2905        *expressions: t.Optional[ExpOrStr],
2906        append: bool = True,
2907        dialect: DialectType = None,
2908        copy: bool = True,
2909        **opts,
2910    ) -> Select:
2911        """
2912        Set the CLUSTER BY expression.
2913
2914        Example:
2915            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
2916            'SELECT x FROM tbl CLUSTER BY x DESC'
2917
2918        Args:
2919            *expressions: the SQL code strings to parse.
2920                If a `Group` instance is passed, this is used as-is.
2921                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
2922            append: if `True`, add to any existing expressions.
2923                Otherwise, this flattens all the `Order` expression into a single expression.
2924            dialect: the dialect used to parse the input expression.
2925            copy: if `False`, modify this expression instance in-place.
2926            opts: other options to use to parse the input expressions.
2927
2928        Returns:
2929            The modified Select expression.
2930        """
2931        return _apply_child_list_builder(
2932            *expressions,
2933            instance=self,
2934            arg="cluster",
2935            append=append,
2936            copy=copy,
2937            prefix="CLUSTER BY",
2938            into=Cluster,
2939            dialect=dialect,
2940            **opts,
2941        )
2942
2943    def limit(
2944        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2945    ) -> Select:
2946        """
2947        Set the LIMIT expression.
2948
2949        Example:
2950            >>> Select().from_("tbl").select("x").limit(10).sql()
2951            'SELECT x FROM tbl LIMIT 10'
2952
2953        Args:
2954            expression: the SQL code string to parse.
2955                This can also be an integer.
2956                If a `Limit` instance is passed, this is used as-is.
2957                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2958            dialect: the dialect used to parse the input expression.
2959            copy: if `False`, modify this expression instance in-place.
2960            opts: other options to use to parse the input expressions.
2961
2962        Returns:
2963            Select: the modified expression.
2964        """
2965        return _apply_builder(
2966            expression=expression,
2967            instance=self,
2968            arg="limit",
2969            into=Limit,
2970            prefix="LIMIT",
2971            dialect=dialect,
2972            copy=copy,
2973            into_arg="expression",
2974            **opts,
2975        )
2976
2977    def offset(
2978        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2979    ) -> Select:
2980        """
2981        Set the OFFSET expression.
2982
2983        Example:
2984            >>> Select().from_("tbl").select("x").offset(10).sql()
2985            'SELECT x FROM tbl OFFSET 10'
2986
2987        Args:
2988            expression: the SQL code string to parse.
2989                This can also be an integer.
2990                If a `Offset` instance is passed, this is used as-is.
2991                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
2992            dialect: the dialect used to parse the input expression.
2993            copy: if `False`, modify this expression instance in-place.
2994            opts: other options to use to parse the input expressions.
2995
2996        Returns:
2997            The modified Select expression.
2998        """
2999        return _apply_builder(
3000            expression=expression,
3001            instance=self,
3002            arg="offset",
3003            into=Offset,
3004            prefix="OFFSET",
3005            dialect=dialect,
3006            copy=copy,
3007            into_arg="expression",
3008            **opts,
3009        )
3010
3011    def select(
3012        self,
3013        *expressions: t.Optional[ExpOrStr],
3014        append: bool = True,
3015        dialect: DialectType = None,
3016        copy: bool = True,
3017        **opts,
3018    ) -> Select:
3019        """
3020        Append to or set the SELECT expressions.
3021
3022        Example:
3023            >>> Select().select("x", "y").sql()
3024            'SELECT x, y'
3025
3026        Args:
3027            *expressions: the SQL code strings to parse.
3028                If an `Expression` instance is passed, it will be used as-is.
3029            append: if `True`, add to any existing expressions.
3030                Otherwise, this resets the expressions.
3031            dialect: the dialect used to parse the input expressions.
3032            copy: if `False`, modify this expression instance in-place.
3033            opts: other options to use to parse the input expressions.
3034
3035        Returns:
3036            The modified Select expression.
3037        """
3038        return _apply_list_builder(
3039            *expressions,
3040            instance=self,
3041            arg="expressions",
3042            append=append,
3043            dialect=dialect,
3044            copy=copy,
3045            **opts,
3046        )
3047
3048    def lateral(
3049        self,
3050        *expressions: t.Optional[ExpOrStr],
3051        append: bool = True,
3052        dialect: DialectType = None,
3053        copy: bool = True,
3054        **opts,
3055    ) -> Select:
3056        """
3057        Append to or set the LATERAL expressions.
3058
3059        Example:
3060            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3061            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3062
3063        Args:
3064            *expressions: the SQL code strings to parse.
3065                If an `Expression` instance is passed, it will be used as-is.
3066            append: if `True`, add to any existing expressions.
3067                Otherwise, this resets the expressions.
3068            dialect: the dialect used to parse the input expressions.
3069            copy: if `False`, modify this expression instance in-place.
3070            opts: other options to use to parse the input expressions.
3071
3072        Returns:
3073            The modified Select expression.
3074        """
3075        return _apply_list_builder(
3076            *expressions,
3077            instance=self,
3078            arg="laterals",
3079            append=append,
3080            into=Lateral,
3081            prefix="LATERAL VIEW",
3082            dialect=dialect,
3083            copy=copy,
3084            **opts,
3085        )
3086
3087    def join(
3088        self,
3089        expression: ExpOrStr,
3090        on: t.Optional[ExpOrStr] = None,
3091        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3092        append: bool = True,
3093        join_type: t.Optional[str] = None,
3094        join_alias: t.Optional[Identifier | str] = None,
3095        dialect: DialectType = None,
3096        copy: bool = True,
3097        **opts,
3098    ) -> Select:
3099        """
3100        Append to or set the JOIN expressions.
3101
3102        Example:
3103            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
3104            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
3105
3106            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
3107            'SELECT 1 FROM a JOIN b USING (x, y, z)'
3108
3109            Use `join_type` to change the type of join:
3110
3111            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
3112            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
3113
3114        Args:
3115            expression: the SQL code string to parse.
3116                If an `Expression` instance is passed, it will be used as-is.
3117            on: optionally specify the join "on" criteria as a SQL string.
3118                If an `Expression` instance is passed, it will be used as-is.
3119            using: optionally specify the join "using" criteria as a SQL string.
3120                If an `Expression` instance is passed, it will be used as-is.
3121            append: if `True`, add to any existing expressions.
3122                Otherwise, this resets the expressions.
3123            join_type: if set, alter the parsed join type.
3124            join_alias: an optional alias for the joined source.
3125            dialect: the dialect used to parse the input expressions.
3126            copy: if `False`, modify this expression instance in-place.
3127            opts: other options to use to parse the input expressions.
3128
3129        Returns:
3130            Select: the modified expression.
3131        """
3132        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
3133
3134        try:
3135            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
3136        except ParseError:
3137            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
3138
3139        join = expression if isinstance(expression, Join) else Join(this=expression)
3140
3141        if isinstance(join.this, Select):
3142            join.this.replace(join.this.subquery())
3143
3144        if join_type:
3145            method: t.Optional[Token]
3146            side: t.Optional[Token]
3147            kind: t.Optional[Token]
3148
3149            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
3150
3151            if method:
3152                join.set("method", method.text)
3153            if side:
3154                join.set("side", side.text)
3155            if kind:
3156                join.set("kind", kind.text)
3157
3158        if on:
3159            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
3160            join.set("on", on)
3161
3162        if using:
3163            join = _apply_list_builder(
3164                *ensure_list(using),
3165                instance=join,
3166                arg="using",
3167                append=append,
3168                copy=copy,
3169                into=Identifier,
3170                **opts,
3171            )
3172
3173        if join_alias:
3174            join.set("this", alias_(join.this, join_alias, table=True))
3175
3176        return _apply_list_builder(
3177            join,
3178            instance=self,
3179            arg="joins",
3180            append=append,
3181            copy=copy,
3182            **opts,
3183        )
3184
3185    def where(
3186        self,
3187        *expressions: t.Optional[ExpOrStr],
3188        append: bool = True,
3189        dialect: DialectType = None,
3190        copy: bool = True,
3191        **opts,
3192    ) -> Select:
3193        """
3194        Append to or set the WHERE expressions.
3195
3196        Example:
3197            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
3198            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
3199
3200        Args:
3201            *expressions: the SQL code strings to parse.
3202                If an `Expression` instance is passed, it will be used as-is.
3203                Multiple expressions are combined with an AND operator.
3204            append: if `True`, AND the new expressions to any existing expression.
3205                Otherwise, this resets the expression.
3206            dialect: the dialect used to parse the input expressions.
3207            copy: if `False`, modify this expression instance in-place.
3208            opts: other options to use to parse the input expressions.
3209
3210        Returns:
3211            Select: the modified expression.
3212        """
3213        return _apply_conjunction_builder(
3214            *expressions,
3215            instance=self,
3216            arg="where",
3217            append=append,
3218            into=Where,
3219            dialect=dialect,
3220            copy=copy,
3221            **opts,
3222        )
3223
3224    def having(
3225        self,
3226        *expressions: t.Optional[ExpOrStr],
3227        append: bool = True,
3228        dialect: DialectType = None,
3229        copy: bool = True,
3230        **opts,
3231    ) -> Select:
3232        """
3233        Append to or set the HAVING expressions.
3234
3235        Example:
3236            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
3237            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
3238
3239        Args:
3240            *expressions: the SQL code strings to parse.
3241                If an `Expression` instance is passed, it will be used as-is.
3242                Multiple expressions are combined with an AND operator.
3243            append: if `True`, AND the new expressions to any existing expression.
3244                Otherwise, this resets the expression.
3245            dialect: the dialect used to parse the input expressions.
3246            copy: if `False`, modify this expression instance in-place.
3247            opts: other options to use to parse the input expressions.
3248
3249        Returns:
3250            The modified Select expression.
3251        """
3252        return _apply_conjunction_builder(
3253            *expressions,
3254            instance=self,
3255            arg="having",
3256            append=append,
3257            into=Having,
3258            dialect=dialect,
3259            copy=copy,
3260            **opts,
3261        )
3262
3263    def window(
3264        self,
3265        *expressions: t.Optional[ExpOrStr],
3266        append: bool = True,
3267        dialect: DialectType = None,
3268        copy: bool = True,
3269        **opts,
3270    ) -> Select:
3271        return _apply_list_builder(
3272            *expressions,
3273            instance=self,
3274            arg="windows",
3275            append=append,
3276            into=Window,
3277            dialect=dialect,
3278            copy=copy,
3279            **opts,
3280        )
3281
3282    def qualify(
3283        self,
3284        *expressions: t.Optional[ExpOrStr],
3285        append: bool = True,
3286        dialect: DialectType = None,
3287        copy: bool = True,
3288        **opts,
3289    ) -> Select:
3290        return _apply_conjunction_builder(
3291            *expressions,
3292            instance=self,
3293            arg="qualify",
3294            append=append,
3295            into=Qualify,
3296            dialect=dialect,
3297            copy=copy,
3298            **opts,
3299        )
3300
3301    def distinct(
3302        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
3303    ) -> Select:
3304        """
3305        Set the OFFSET expression.
3306
3307        Example:
3308            >>> Select().from_("tbl").select("x").distinct().sql()
3309            'SELECT DISTINCT x FROM tbl'
3310
3311        Args:
3312            ons: the expressions to distinct on
3313            distinct: whether the Select should be distinct
3314            copy: if `False`, modify this expression instance in-place.
3315
3316        Returns:
3317            Select: the modified expression.
3318        """
3319        instance = maybe_copy(self, copy)
3320        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
3321        instance.set("distinct", Distinct(on=on) if distinct else None)
3322        return instance
3323
3324    def ctas(
3325        self,
3326        table: ExpOrStr,
3327        properties: t.Optional[t.Dict] = None,
3328        dialect: DialectType = None,
3329        copy: bool = True,
3330        **opts,
3331    ) -> Create:
3332        """
3333        Convert this expression to a CREATE TABLE AS statement.
3334
3335        Example:
3336            >>> Select().select("*").from_("tbl").ctas("x").sql()
3337            'CREATE TABLE x AS SELECT * FROM tbl'
3338
3339        Args:
3340            table: the SQL code string to parse as the table name.
3341                If another `Expression` instance is passed, it will be used as-is.
3342            properties: an optional mapping of table properties
3343            dialect: the dialect used to parse the input table.
3344            copy: if `False`, modify this expression instance in-place.
3345            opts: other options to use to parse the input table.
3346
3347        Returns:
3348            The new Create expression.
3349        """
3350        instance = maybe_copy(self, copy)
3351        table_expression = maybe_parse(
3352            table,
3353            into=Table,
3354            dialect=dialect,
3355            **opts,
3356        )
3357        properties_expression = None
3358        if properties:
3359            properties_expression = Properties.from_dict(properties)
3360
3361        return Create(
3362            this=table_expression,
3363            kind="table",
3364            expression=instance,
3365            properties=properties_expression,
3366        )
3367
3368    def lock(self, update: bool = True, copy: bool = True) -> Select:
3369        """
3370        Set the locking read mode for this expression.
3371
3372        Examples:
3373            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
3374            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
3375
3376            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
3377            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
3378
3379        Args:
3380            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
3381            copy: if `False`, modify this expression instance in-place.
3382
3383        Returns:
3384            The modified expression.
3385        """
3386        inst = maybe_copy(self, copy)
3387        inst.set("locks", [Lock(update=update)])
3388
3389        return inst
3390
3391    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
3392        """
3393        Set hints for this expression.
3394
3395        Examples:
3396            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
3397            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
3398
3399        Args:
3400            hints: The SQL code strings to parse as the hints.
3401                If an `Expression` instance is passed, it will be used as-is.
3402            dialect: The dialect used to parse the hints.
3403            copy: If `False`, modify this expression instance in-place.
3404
3405        Returns:
3406            The modified expression.
3407        """
3408        inst = maybe_copy(self, copy)
3409        inst.set(
3410            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
3411        )
3412
3413        return inst
3414
3415    @property
3416    def named_selects(self) -> t.List[str]:
3417        return [e.output_name for e in self.expressions if e.alias_or_name]
3418
3419    @property
3420    def is_star(self) -> bool:
3421        return any(expression.is_star for expression in self.expressions)
3422
3423    @property
3424    def selects(self) -> t.List[Expression]:
3425        return self.expressions
arg_types = {'with': False, 'kind': False, 'expressions': False, 'hint': False, 'distinct': False, 'into': False, 'from': False, 'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': 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}
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:
2747    def from_(
2748        self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
2749    ) -> Select:
2750        """
2751        Set the FROM expression.
2752
2753        Example:
2754            >>> Select().from_("tbl").select("x").sql()
2755            'SELECT x FROM tbl'
2756
2757        Args:
2758            expression : the SQL code strings to parse.
2759                If a `From` instance is passed, this is used as-is.
2760                If another `Expression` instance is passed, it will be wrapped in a `From`.
2761            dialect: the dialect used to parse the input expression.
2762            copy: if `False`, modify this expression instance in-place.
2763            opts: other options to use to parse the input expressions.
2764
2765        Returns:
2766            The modified Select expression.
2767        """
2768        return _apply_builder(
2769            expression=expression,
2770            instance=self,
2771            arg="from",
2772            into=From,
2773            prefix="FROM",
2774            dialect=dialect,
2775            copy=copy,
2776            **opts,
2777        )

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:
2779    def group_by(
2780        self,
2781        *expressions: t.Optional[ExpOrStr],
2782        append: bool = True,
2783        dialect: DialectType = None,
2784        copy: bool = True,
2785        **opts,
2786    ) -> Select:
2787        """
2788        Set the GROUP BY expression.
2789
2790        Example:
2791            >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql()
2792            'SELECT x, COUNT(1) FROM tbl GROUP BY x'
2793
2794        Args:
2795            *expressions: the SQL code strings to parse.
2796                If a `Group` instance is passed, this is used as-is.
2797                If another `Expression` instance is passed, it will be wrapped in a `Group`.
2798                If nothing is passed in then a group by is not applied to the expression
2799            append: if `True`, add to any existing expressions.
2800                Otherwise, this flattens all the `Group` expression into a single expression.
2801            dialect: the dialect used to parse the input expression.
2802            copy: if `False`, modify this expression instance in-place.
2803            opts: other options to use to parse the input expressions.
2804
2805        Returns:
2806            The modified Select expression.
2807        """
2808        if not expressions:
2809            return self if not copy else self.copy()
2810
2811        return _apply_child_list_builder(
2812            *expressions,
2813            instance=self,
2814            arg="group",
2815            append=append,
2816            copy=copy,
2817            prefix="GROUP BY",
2818            into=Group,
2819            dialect=dialect,
2820            **opts,
2821        )

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 order_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:
2823    def order_by(
2824        self,
2825        *expressions: t.Optional[ExpOrStr],
2826        append: bool = True,
2827        dialect: DialectType = None,
2828        copy: bool = True,
2829        **opts,
2830    ) -> Select:
2831        """
2832        Set the ORDER BY expression.
2833
2834        Example:
2835            >>> Select().from_("tbl").select("x").order_by("x DESC").sql()
2836            'SELECT x FROM tbl ORDER BY x DESC'
2837
2838        Args:
2839            *expressions: the SQL code strings to parse.
2840                If a `Group` instance is passed, this is used as-is.
2841                If another `Expression` instance is passed, it will be wrapped in a `Order`.
2842            append: if `True`, add to any existing expressions.
2843                Otherwise, this flattens all the `Order` expression into a single expression.
2844            dialect: the dialect used to parse the input expression.
2845            copy: if `False`, modify this expression instance in-place.
2846            opts: other options to use to parse the input expressions.
2847
2848        Returns:
2849            The modified Select expression.
2850        """
2851        return _apply_child_list_builder(
2852            *expressions,
2853            instance=self,
2854            arg="order",
2855            append=append,
2856            copy=copy,
2857            prefix="ORDER BY",
2858            into=Order,
2859            dialect=dialect,
2860            **opts,
2861        )

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.

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:
2863    def sort_by(
2864        self,
2865        *expressions: t.Optional[ExpOrStr],
2866        append: bool = True,
2867        dialect: DialectType = None,
2868        copy: bool = True,
2869        **opts,
2870    ) -> Select:
2871        """
2872        Set the SORT BY expression.
2873
2874        Example:
2875            >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive")
2876            'SELECT x FROM tbl SORT BY x DESC'
2877
2878        Args:
2879            *expressions: the SQL code strings to parse.
2880                If a `Group` instance is passed, this is used as-is.
2881                If another `Expression` instance is passed, it will be wrapped in a `SORT`.
2882            append: if `True`, add to any existing expressions.
2883                Otherwise, this flattens all the `Order` expression into a single expression.
2884            dialect: the dialect used to parse the input expression.
2885            copy: if `False`, modify this expression instance in-place.
2886            opts: other options to use to parse the input expressions.
2887
2888        Returns:
2889            The modified Select expression.
2890        """
2891        return _apply_child_list_builder(
2892            *expressions,
2893            instance=self,
2894            arg="sort",
2895            append=append,
2896            copy=copy,
2897            prefix="SORT BY",
2898            into=Sort,
2899            dialect=dialect,
2900            **opts,
2901        )

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:
2903    def cluster_by(
2904        self,
2905        *expressions: t.Optional[ExpOrStr],
2906        append: bool = True,
2907        dialect: DialectType = None,
2908        copy: bool = True,
2909        **opts,
2910    ) -> Select:
2911        """
2912        Set the CLUSTER BY expression.
2913
2914        Example:
2915            >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive")
2916            'SELECT x FROM tbl CLUSTER BY x DESC'
2917
2918        Args:
2919            *expressions: the SQL code strings to parse.
2920                If a `Group` instance is passed, this is used as-is.
2921                If another `Expression` instance is passed, it will be wrapped in a `Cluster`.
2922            append: if `True`, add to any existing expressions.
2923                Otherwise, this flattens all the `Order` expression into a single expression.
2924            dialect: the dialect used to parse the input expression.
2925            copy: if `False`, modify this expression instance in-place.
2926            opts: other options to use to parse the input expressions.
2927
2928        Returns:
2929            The modified Select expression.
2930        """
2931        return _apply_child_list_builder(
2932            *expressions,
2933            instance=self,
2934            arg="cluster",
2935            append=append,
2936            copy=copy,
2937            prefix="CLUSTER BY",
2938            into=Cluster,
2939            dialect=dialect,
2940            **opts,
2941        )

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 limit( self, expression: Union[str, Expression, int], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
2943    def limit(
2944        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2945    ) -> Select:
2946        """
2947        Set the LIMIT expression.
2948
2949        Example:
2950            >>> Select().from_("tbl").select("x").limit(10).sql()
2951            'SELECT x FROM tbl LIMIT 10'
2952
2953        Args:
2954            expression: the SQL code string to parse.
2955                This can also be an integer.
2956                If a `Limit` instance is passed, this is used as-is.
2957                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2958            dialect: the dialect used to parse the input expression.
2959            copy: if `False`, modify this expression instance in-place.
2960            opts: other options to use to parse the input expressions.
2961
2962        Returns:
2963            Select: the modified expression.
2964        """
2965        return _apply_builder(
2966            expression=expression,
2967            instance=self,
2968            arg="limit",
2969            into=Limit,
2970            prefix="LIMIT",
2971            dialect=dialect,
2972            copy=copy,
2973            into_arg="expression",
2974            **opts,
2975        )

Set the LIMIT expression.

Example:
>>> Select().from_("tbl").select("x").limit(10).sql()
'SELECT x FROM tbl LIMIT 10'
Arguments:
  • expression: the SQL code string to parse. This can also be an integer. If a Limit instance is passed, this is 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:

Select: the modified expression.

def offset( self, expression: Union[str, Expression, int], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Select:
2977    def offset(
2978        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2979    ) -> Select:
2980        """
2981        Set the OFFSET expression.
2982
2983        Example:
2984            >>> Select().from_("tbl").select("x").offset(10).sql()
2985            'SELECT x FROM tbl OFFSET 10'
2986
2987        Args:
2988            expression: the SQL code string to parse.
2989                This can also be an integer.
2990                If a `Offset` instance is passed, this is used as-is.
2991                If another `Expression` instance is passed, it will be wrapped in a `Offset`.
2992            dialect: the dialect used to parse the input expression.
2993            copy: if `False`, modify this expression instance in-place.
2994            opts: other options to use to parse the input expressions.
2995
2996        Returns:
2997            The modified Select expression.
2998        """
2999        return _apply_builder(
3000            expression=expression,
3001            instance=self,
3002            arg="offset",
3003            into=Offset,
3004            prefix="OFFSET",
3005            dialect=dialect,
3006            copy=copy,
3007            into_arg="expression",
3008            **opts,
3009        )

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 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:
3011    def select(
3012        self,
3013        *expressions: t.Optional[ExpOrStr],
3014        append: bool = True,
3015        dialect: DialectType = None,
3016        copy: bool = True,
3017        **opts,
3018    ) -> Select:
3019        """
3020        Append to or set the SELECT expressions.
3021
3022        Example:
3023            >>> Select().select("x", "y").sql()
3024            'SELECT x, y'
3025
3026        Args:
3027            *expressions: the SQL code strings to parse.
3028                If an `Expression` instance is passed, it will be used as-is.
3029            append: if `True`, add to any existing expressions.
3030                Otherwise, this resets the expressions.
3031            dialect: the dialect used to parse the input expressions.
3032            copy: if `False`, modify this expression instance in-place.
3033            opts: other options to use to parse the input expressions.
3034
3035        Returns:
3036            The modified Select expression.
3037        """
3038        return _apply_list_builder(
3039            *expressions,
3040            instance=self,
3041            arg="expressions",
3042            append=append,
3043            dialect=dialect,
3044            copy=copy,
3045            **opts,
3046        )

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 Select 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:
3048    def lateral(
3049        self,
3050        *expressions: t.Optional[ExpOrStr],
3051        append: bool = True,
3052        dialect: DialectType = None,
3053        copy: bool = True,
3054        **opts,
3055    ) -> Select:
3056        """
3057        Append to or set the LATERAL expressions.
3058
3059        Example:
3060            >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql()
3061            'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z'
3062
3063        Args:
3064            *expressions: the SQL code strings to parse.
3065                If an `Expression` instance is passed, it will be used as-is.
3066            append: if `True`, add to any existing expressions.
3067                Otherwise, this resets the expressions.
3068            dialect: the dialect used to parse the input expressions.
3069            copy: if `False`, modify this expression instance in-place.
3070            opts: other options to use to parse the input expressions.
3071
3072        Returns:
3073            The modified Select expression.
3074        """
3075        return _apply_list_builder(
3076            *expressions,
3077            instance=self,
3078            arg="laterals",
3079            append=append,
3080            into=Lateral,
3081            prefix="LATERAL VIEW",
3082            dialect=dialect,
3083            copy=copy,
3084            **opts,
3085        )

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:
3087    def join(
3088        self,
3089        expression: ExpOrStr,
3090        on: t.Optional[ExpOrStr] = None,
3091        using: t.Optional[ExpOrStr | t.Collection[ExpOrStr]] = None,
3092        append: bool = True,
3093        join_type: t.Optional[str] = None,
3094        join_alias: t.Optional[Identifier | str] = None,
3095        dialect: DialectType = None,
3096        copy: bool = True,
3097        **opts,
3098    ) -> Select:
3099        """
3100        Append to or set the JOIN expressions.
3101
3102        Example:
3103            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql()
3104            'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
3105
3106            >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql()
3107            'SELECT 1 FROM a JOIN b USING (x, y, z)'
3108
3109            Use `join_type` to change the type of join:
3110
3111            >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql()
3112            'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
3113
3114        Args:
3115            expression: the SQL code string to parse.
3116                If an `Expression` instance is passed, it will be used as-is.
3117            on: optionally specify the join "on" criteria as a SQL string.
3118                If an `Expression` instance is passed, it will be used as-is.
3119            using: optionally specify the join "using" criteria as a SQL string.
3120                If an `Expression` instance is passed, it will be used as-is.
3121            append: if `True`, add to any existing expressions.
3122                Otherwise, this resets the expressions.
3123            join_type: if set, alter the parsed join type.
3124            join_alias: an optional alias for the joined source.
3125            dialect: the dialect used to parse the input expressions.
3126            copy: if `False`, modify this expression instance in-place.
3127            opts: other options to use to parse the input expressions.
3128
3129        Returns:
3130            Select: the modified expression.
3131        """
3132        parse_args: t.Dict[str, t.Any] = {"dialect": dialect, **opts}
3133
3134        try:
3135            expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args)
3136        except ParseError:
3137            expression = maybe_parse(expression, into=(Join, Expression), **parse_args)
3138
3139        join = expression if isinstance(expression, Join) else Join(this=expression)
3140
3141        if isinstance(join.this, Select):
3142            join.this.replace(join.this.subquery())
3143
3144        if join_type:
3145            method: t.Optional[Token]
3146            side: t.Optional[Token]
3147            kind: t.Optional[Token]
3148
3149            method, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args)  # type: ignore
3150
3151            if method:
3152                join.set("method", method.text)
3153            if side:
3154                join.set("side", side.text)
3155            if kind:
3156                join.set("kind", kind.text)
3157
3158        if on:
3159            on = and_(*ensure_list(on), dialect=dialect, copy=copy, **opts)
3160            join.set("on", on)
3161
3162        if using:
3163            join = _apply_list_builder(
3164                *ensure_list(using),
3165                instance=join,
3166                arg="using",
3167                append=append,
3168                copy=copy,
3169                into=Identifier,
3170                **opts,
3171            )
3172
3173        if join_alias:
3174            join.set("this", alias_(join.this, join_alias, table=True))
3175
3176        return _apply_list_builder(
3177            join,
3178            instance=self,
3179            arg="joins",
3180            append=append,
3181            copy=copy,
3182            **opts,
3183        )

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:
3185    def where(
3186        self,
3187        *expressions: t.Optional[ExpOrStr],
3188        append: bool = True,
3189        dialect: DialectType = None,
3190        copy: bool = True,
3191        **opts,
3192    ) -> Select:
3193        """
3194        Append to or set the WHERE expressions.
3195
3196        Example:
3197            >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql()
3198            "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'"
3199
3200        Args:
3201            *expressions: the SQL code strings to parse.
3202                If an `Expression` instance is passed, it will be used as-is.
3203                Multiple expressions are combined with an AND operator.
3204            append: if `True`, AND the new expressions to any existing expression.
3205                Otherwise, this resets the expression.
3206            dialect: the dialect used to parse the input expressions.
3207            copy: if `False`, modify this expression instance in-place.
3208            opts: other options to use to parse the input expressions.
3209
3210        Returns:
3211            Select: the modified expression.
3212        """
3213        return _apply_conjunction_builder(
3214            *expressions,
3215            instance=self,
3216            arg="where",
3217            append=append,
3218            into=Where,
3219            dialect=dialect,
3220            copy=copy,
3221            **opts,
3222        )

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:
3224    def having(
3225        self,
3226        *expressions: t.Optional[ExpOrStr],
3227        append: bool = True,
3228        dialect: DialectType = None,
3229        copy: bool = True,
3230        **opts,
3231    ) -> Select:
3232        """
3233        Append to or set the HAVING expressions.
3234
3235        Example:
3236            >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql()
3237            'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3'
3238
3239        Args:
3240            *expressions: the SQL code strings to parse.
3241                If an `Expression` instance is passed, it will be used as-is.
3242                Multiple expressions are combined with an AND operator.
3243            append: if `True`, AND the new expressions to any existing expression.
3244                Otherwise, this resets the expression.
3245            dialect: the dialect used to parse the input expressions.
3246            copy: if `False`, modify this expression instance in-place.
3247            opts: other options to use to parse the input expressions.
3248
3249        Returns:
3250            The modified Select expression.
3251        """
3252        return _apply_conjunction_builder(
3253            *expressions,
3254            instance=self,
3255            arg="having",
3256            append=append,
3257            into=Having,
3258            dialect=dialect,
3259            copy=copy,
3260            **opts,
3261        )

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:
3263    def window(
3264        self,
3265        *expressions: t.Optional[ExpOrStr],
3266        append: bool = True,
3267        dialect: DialectType = None,
3268        copy: bool = True,
3269        **opts,
3270    ) -> Select:
3271        return _apply_list_builder(
3272            *expressions,
3273            instance=self,
3274            arg="windows",
3275            append=append,
3276            into=Window,
3277            dialect=dialect,
3278            copy=copy,
3279            **opts,
3280        )
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:
3282    def qualify(
3283        self,
3284        *expressions: t.Optional[ExpOrStr],
3285        append: bool = True,
3286        dialect: DialectType = None,
3287        copy: bool = True,
3288        **opts,
3289    ) -> Select:
3290        return _apply_conjunction_builder(
3291            *expressions,
3292            instance=self,
3293            arg="qualify",
3294            append=append,
3295            into=Qualify,
3296            dialect=dialect,
3297            copy=copy,
3298            **opts,
3299        )
def distinct( self, *ons: Union[str, Expression, NoneType], distinct: bool = True, copy: bool = True) -> Select:
3301    def distinct(
3302        self, *ons: t.Optional[ExpOrStr], distinct: bool = True, copy: bool = True
3303    ) -> Select:
3304        """
3305        Set the OFFSET expression.
3306
3307        Example:
3308            >>> Select().from_("tbl").select("x").distinct().sql()
3309            'SELECT DISTINCT x FROM tbl'
3310
3311        Args:
3312            ons: the expressions to distinct on
3313            distinct: whether the Select should be distinct
3314            copy: if `False`, modify this expression instance in-place.
3315
3316        Returns:
3317            Select: the modified expression.
3318        """
3319        instance = maybe_copy(self, copy)
3320        on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons if on]) if ons else None
3321        instance.set("distinct", Distinct(on=on) if distinct else None)
3322        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:
3324    def ctas(
3325        self,
3326        table: ExpOrStr,
3327        properties: t.Optional[t.Dict] = None,
3328        dialect: DialectType = None,
3329        copy: bool = True,
3330        **opts,
3331    ) -> Create:
3332        """
3333        Convert this expression to a CREATE TABLE AS statement.
3334
3335        Example:
3336            >>> Select().select("*").from_("tbl").ctas("x").sql()
3337            'CREATE TABLE x AS SELECT * FROM tbl'
3338
3339        Args:
3340            table: the SQL code string to parse as the table name.
3341                If another `Expression` instance is passed, it will be used as-is.
3342            properties: an optional mapping of table properties
3343            dialect: the dialect used to parse the input table.
3344            copy: if `False`, modify this expression instance in-place.
3345            opts: other options to use to parse the input table.
3346
3347        Returns:
3348            The new Create expression.
3349        """
3350        instance = maybe_copy(self, copy)
3351        table_expression = maybe_parse(
3352            table,
3353            into=Table,
3354            dialect=dialect,
3355            **opts,
3356        )
3357        properties_expression = None
3358        if properties:
3359            properties_expression = Properties.from_dict(properties)
3360
3361        return Create(
3362            this=table_expression,
3363            kind="table",
3364            expression=instance,
3365            properties=properties_expression,
3366        )

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:
3368    def lock(self, update: bool = True, copy: bool = True) -> Select:
3369        """
3370        Set the locking read mode for this expression.
3371
3372        Examples:
3373            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
3374            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
3375
3376            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
3377            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
3378
3379        Args:
3380            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
3381            copy: if `False`, modify this expression instance in-place.
3382
3383        Returns:
3384            The modified expression.
3385        """
3386        inst = maybe_copy(self, copy)
3387        inst.set("locks", [Lock(update=update)])
3388
3389        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:
3391    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
3392        """
3393        Set hints for this expression.
3394
3395        Examples:
3396            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
3397            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
3398
3399        Args:
3400            hints: The SQL code strings to parse as the hints.
3401                If an `Expression` instance is passed, it will be used as-is.
3402            dialect: The dialect used to parse the hints.
3403            copy: If `False`, modify this expression instance in-place.
3404
3405        Returns:
3406            The modified expression.
3407        """
3408        inst = maybe_copy(self, copy)
3409        inst.set(
3410            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
3411        )
3412
3413        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]
is_star: bool

Checks whether an expression is a star.

selects: List[Expression]
key = 'select'
class Subquery(DerivedTable, Unionable):
3428class Subquery(DerivedTable, Unionable):
3429    arg_types = {
3430        "this": True,
3431        "alias": False,
3432        "with": False,
3433        **QUERY_MODIFIERS,
3434    }
3435
3436    def unnest(self):
3437        """
3438        Returns the first non subquery.
3439        """
3440        expression = self
3441        while isinstance(expression, Subquery):
3442            expression = expression.this
3443        return expression
3444
3445    def unwrap(self) -> Subquery:
3446        expression = self
3447        while expression.same_parent and expression.is_wrapper:
3448            expression = t.cast(Subquery, expression.parent)
3449        return expression
3450
3451    @property
3452    def is_wrapper(self) -> bool:
3453        """
3454        Whether this Subquery acts as a simple wrapper around another expression.
3455
3456        SELECT * FROM (((SELECT * FROM t)))
3457                      ^
3458                      This corresponds to a "wrapper" Subquery node
3459        """
3460        return all(v is None for k, v in self.args.items() if k != "this")
3461
3462    @property
3463    def is_star(self) -> bool:
3464        return self.this.is_star
3465
3466    @property
3467    def output_name(self) -> str:
3468        return self.alias
arg_types = {'this': True, 'alias': False, 'with': False, 'match': False, 'laterals': False, 'joins': False, 'connect': False, 'pivots': 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}
def unnest(self):
3436    def unnest(self):
3437        """
3438        Returns the first non subquery.
3439        """
3440        expression = self
3441        while isinstance(expression, Subquery):
3442            expression = expression.this
3443        return expression

Returns the first non subquery.

def unwrap(self) -> Subquery:
3445    def unwrap(self) -> Subquery:
3446        expression = self
3447        while expression.same_parent and expression.is_wrapper:
3448            expression = t.cast(Subquery, expression.parent)
3449        return expression
is_wrapper: bool

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

Checks whether an expression is a star.

output_name: str

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):
3471class TableSample(Expression):
3472    arg_types = {
3473        "this": False,
3474        "expressions": False,
3475        "method": False,
3476        "bucket_numerator": False,
3477        "bucket_denominator": False,
3478        "bucket_field": False,
3479        "percent": False,
3480        "rows": False,
3481        "size": False,
3482        "seed": False,
3483        "kind": False,
3484    }
arg_types = {'this': False, 'expressions': False, 'method': False, 'bucket_numerator': False, 'bucket_denominator': False, 'bucket_field': False, 'percent': False, 'rows': False, 'size': False, 'seed': False, 'kind': False}
key = 'tablesample'
class Tag(Expression):
3487class Tag(Expression):
3488    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
3489
3490    arg_types = {
3491        "this": False,
3492        "prefix": False,
3493        "postfix": False,
3494    }

Tags are used for generating arbitrary sql like SELECT x.

arg_types = {'this': False, 'prefix': False, 'postfix': False}
key = 'tag'
class Pivot(Expression):
3499class Pivot(Expression):
3500    arg_types = {
3501        "this": False,
3502        "alias": False,
3503        "expressions": False,
3504        "field": False,
3505        "unpivot": False,
3506        "using": False,
3507        "group": False,
3508        "columns": False,
3509        "include_nulls": False,
3510    }
arg_types = {'this': False, 'alias': False, 'expressions': False, 'field': False, 'unpivot': False, 'using': False, 'group': False, 'columns': False, 'include_nulls': False}
key = 'pivot'
class Window(Condition):
3513class Window(Condition):
3514    arg_types = {
3515        "this": True,
3516        "partition_by": False,
3517        "order": False,
3518        "spec": False,
3519        "alias": False,
3520        "over": False,
3521        "first": False,
3522    }
arg_types = {'this': True, 'partition_by': False, 'order': False, 'spec': False, 'alias': False, 'over': False, 'first': False}
key = 'window'
class WindowSpec(Expression):
3525class WindowSpec(Expression):
3526    arg_types = {
3527        "kind": False,
3528        "start": False,
3529        "start_side": False,
3530        "end": False,
3531        "end_side": False,
3532    }
arg_types = {'kind': False, 'start': False, 'start_side': False, 'end': False, 'end_side': False}
key = 'windowspec'
class Where(Expression):
3535class Where(Expression):
3536    pass
key = 'where'
class Star(Expression):
3539class Star(Expression):
3540    arg_types = {"except": False, "replace": False}
3541
3542    @property
3543    def name(self) -> str:
3544        return "*"
3545
3546    @property
3547    def output_name(self) -> str:
3548        return self.name
arg_types = {'except': False, 'replace': False}
name: str
output_name: str

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):
3551class Parameter(Condition):
3552    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'parameter'
class SessionParameter(Condition):
3555class SessionParameter(Condition):
3556    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'sessionparameter'
class Placeholder(Condition):
3559class Placeholder(Condition):
3560    arg_types = {"this": False, "kind": False}
arg_types = {'this': False, 'kind': False}
key = 'placeholder'
class Null(Condition):
3563class Null(Condition):
3564    arg_types: t.Dict[str, t.Any] = {}
3565
3566    @property
3567    def name(self) -> str:
3568        return "NULL"
arg_types: Dict[str, Any] = {}
name: str
key = 'null'
class Boolean(Condition):
3571class Boolean(Condition):
3572    pass
key = 'boolean'
class DataTypeParam(Expression):
3575class DataTypeParam(Expression):
3576    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'datatypeparam'
class DataType(Expression):
3579class DataType(Expression):
3580    arg_types = {
3581        "this": True,
3582        "expressions": False,
3583        "nested": False,
3584        "values": False,
3585        "prefix": False,
3586        "kind": False,
3587    }
3588
3589    class Type(AutoName):
3590        ARRAY = auto()
3591        BIGDECIMAL = auto()
3592        BIGINT = auto()
3593        BIGSERIAL = auto()
3594        BINARY = auto()
3595        BIT = auto()
3596        BOOLEAN = auto()
3597        CHAR = auto()
3598        DATE = auto()
3599        DATEMULTIRANGE = auto()
3600        DATERANGE = auto()
3601        DATETIME = auto()
3602        DATETIME64 = auto()
3603        DECIMAL = auto()
3604        DOUBLE = auto()
3605        ENUM = auto()
3606        ENUM8 = auto()
3607        ENUM16 = auto()
3608        FIXEDSTRING = auto()
3609        FLOAT = auto()
3610        GEOGRAPHY = auto()
3611        GEOMETRY = auto()
3612        HLLSKETCH = auto()
3613        HSTORE = auto()
3614        IMAGE = auto()
3615        INET = auto()
3616        INT = auto()
3617        INT128 = auto()
3618        INT256 = auto()
3619        INT4MULTIRANGE = auto()
3620        INT4RANGE = auto()
3621        INT8MULTIRANGE = auto()
3622        INT8RANGE = auto()
3623        INTERVAL = auto()
3624        IPADDRESS = auto()
3625        IPPREFIX = auto()
3626        JSON = auto()
3627        JSONB = auto()
3628        LONGBLOB = auto()
3629        LONGTEXT = auto()
3630        LOWCARDINALITY = auto()
3631        MAP = auto()
3632        MEDIUMBLOB = auto()
3633        MEDIUMINT = auto()
3634        MEDIUMTEXT = auto()
3635        MONEY = auto()
3636        NCHAR = auto()
3637        NESTED = auto()
3638        NULL = auto()
3639        NULLABLE = auto()
3640        NUMMULTIRANGE = auto()
3641        NUMRANGE = auto()
3642        NVARCHAR = auto()
3643        OBJECT = auto()
3644        ROWVERSION = auto()
3645        SERIAL = auto()
3646        SET = auto()
3647        SMALLINT = auto()
3648        SMALLMONEY = auto()
3649        SMALLSERIAL = auto()
3650        STRUCT = auto()
3651        SUPER = auto()
3652        TEXT = auto()
3653        TINYBLOB = auto()
3654        TINYTEXT = auto()
3655        TIME = auto()
3656        TIMETZ = auto()
3657        TIMESTAMP = auto()
3658        TIMESTAMPLTZ = auto()
3659        TIMESTAMPTZ = auto()
3660        TIMESTAMP_S = auto()
3661        TIMESTAMP_MS = auto()
3662        TIMESTAMP_NS = auto()
3663        TINYINT = auto()
3664        TSMULTIRANGE = auto()
3665        TSRANGE = auto()
3666        TSTZMULTIRANGE = auto()
3667        TSTZRANGE = auto()
3668        UBIGINT = auto()
3669        UINT = auto()
3670        UINT128 = auto()
3671        UINT256 = auto()
3672        UMEDIUMINT = auto()
3673        UDECIMAL = auto()
3674        UNIQUEIDENTIFIER = auto()
3675        UNKNOWN = auto()  # Sentinel value, useful for type annotation
3676        USERDEFINED = "USER-DEFINED"
3677        USMALLINT = auto()
3678        UTINYINT = auto()
3679        UUID = auto()
3680        VARBINARY = auto()
3681        VARCHAR = auto()
3682        VARIANT = auto()
3683        XML = auto()
3684        YEAR = auto()
3685
3686    TEXT_TYPES = {
3687        Type.CHAR,
3688        Type.NCHAR,
3689        Type.VARCHAR,
3690        Type.NVARCHAR,
3691        Type.TEXT,
3692    }
3693
3694    INTEGER_TYPES = {
3695        Type.INT,
3696        Type.TINYINT,
3697        Type.SMALLINT,
3698        Type.BIGINT,
3699        Type.INT128,
3700        Type.INT256,
3701        Type.BIT,
3702    }
3703
3704    FLOAT_TYPES = {
3705        Type.FLOAT,
3706        Type.DOUBLE,
3707    }
3708
3709    NUMERIC_TYPES = {
3710        *INTEGER_TYPES,
3711        *FLOAT_TYPES,
3712    }
3713
3714    TEMPORAL_TYPES = {
3715        Type.TIME,
3716        Type.TIMETZ,
3717        Type.TIMESTAMP,
3718        Type.TIMESTAMPTZ,
3719        Type.TIMESTAMPLTZ,
3720        Type.TIMESTAMP_S,
3721        Type.TIMESTAMP_MS,
3722        Type.TIMESTAMP_NS,
3723        Type.DATE,
3724        Type.DATETIME,
3725        Type.DATETIME64,
3726    }
3727
3728    @classmethod
3729    def build(
3730        cls,
3731        dtype: DATA_TYPE,
3732        dialect: DialectType = None,
3733        udt: bool = False,
3734        **kwargs,
3735    ) -> DataType:
3736        """
3737        Constructs a DataType object.
3738
3739        Args:
3740            dtype: the data type of interest.
3741            dialect: the dialect to use for parsing `dtype`, in case it's a string.
3742            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
3743                DataType, thus creating a user-defined type.
3744            kawrgs: additional arguments to pass in the constructor of DataType.
3745
3746        Returns:
3747            The constructed DataType object.
3748        """
3749        from sqlglot import parse_one
3750
3751        if isinstance(dtype, str):
3752            if dtype.upper() == "UNKNOWN":
3753                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
3754
3755            try:
3756                data_type_exp = parse_one(
3757                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
3758                )
3759            except ParseError:
3760                if udt:
3761                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
3762                raise
3763        elif isinstance(dtype, DataType.Type):
3764            data_type_exp = DataType(this=dtype)
3765        elif isinstance(dtype, DataType):
3766            return dtype
3767        else:
3768            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
3769
3770        return DataType(**{**data_type_exp.args, **kwargs})
3771
3772    def is_type(self, *dtypes: DATA_TYPE) -> bool:
3773        """
3774        Checks whether this DataType matches one of the provided data types. Nested types or precision
3775        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
3776
3777        Args:
3778            dtypes: the data types to compare this DataType to.
3779
3780        Returns:
3781            True, if and only if there is a type in `dtypes` which is equal to this DataType.
3782        """
3783        for dtype in dtypes:
3784            other = DataType.build(dtype, udt=True)
3785
3786            if (
3787                other.expressions
3788                or self.this == DataType.Type.USERDEFINED
3789                or other.this == DataType.Type.USERDEFINED
3790            ):
3791                matches = self == other
3792            else:
3793                matches = self.this == other.this
3794
3795            if matches:
3796                return True
3797        return False
arg_types = {'this': True, 'expressions': False, 'nested': False, 'values': False, 'prefix': False, 'kind': False}
TEXT_TYPES = {<Type.CHAR: 'CHAR'>, <Type.TEXT: 'TEXT'>, <Type.VARCHAR: 'VARCHAR'>, <Type.NCHAR: 'NCHAR'>, <Type.NVARCHAR: 'NVARCHAR'>}
INTEGER_TYPES = {<Type.SMALLINT: 'SMALLINT'>, <Type.INT128: 'INT128'>, <Type.INT256: 'INT256'>, <Type.BIGINT: 'BIGINT'>, <Type.BIT: 'BIT'>, <Type.TINYINT: 'TINYINT'>, <Type.INT: 'INT'>}
FLOAT_TYPES = {<Type.FLOAT: 'FLOAT'>, <Type.DOUBLE: 'DOUBLE'>}
NUMERIC_TYPES = {<Type.SMALLINT: 'SMALLINT'>, <Type.DOUBLE: 'DOUBLE'>, <Type.INT128: 'INT128'>, <Type.INT256: 'INT256'>, <Type.BIGINT: 'BIGINT'>, <Type.BIT: 'BIT'>, <Type.TINYINT: 'TINYINT'>, <Type.FLOAT: 'FLOAT'>, <Type.INT: 'INT'>}
TEMPORAL_TYPES = {<Type.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <Type.TIMETZ: 'TIMETZ'>, <Type.DATETIME64: 'DATETIME64'>, <Type.TIMESTAMP: 'TIMESTAMP'>, <Type.TIME: 'TIME'>, <Type.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <Type.TIMESTAMP_S: 'TIMESTAMP_S'>, <Type.DATETIME: 'DATETIME'>, <Type.DATE: 'DATE'>, <Type.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <Type.TIMESTAMP_NS: 'TIMESTAMP_NS'>}
@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, **kwargs) -> DataType:
3728    @classmethod
3729    def build(
3730        cls,
3731        dtype: DATA_TYPE,
3732        dialect: DialectType = None,
3733        udt: bool = False,
3734        **kwargs,
3735    ) -> DataType:
3736        """
3737        Constructs a DataType object.
3738
3739        Args:
3740            dtype: the data type of interest.
3741            dialect: the dialect to use for parsing `dtype`, in case it's a string.
3742            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
3743                DataType, thus creating a user-defined type.
3744            kawrgs: additional arguments to pass in the constructor of DataType.
3745
3746        Returns:
3747            The constructed DataType object.
3748        """
3749        from sqlglot import parse_one
3750
3751        if isinstance(dtype, str):
3752            if dtype.upper() == "UNKNOWN":
3753                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
3754
3755            try:
3756                data_type_exp = parse_one(
3757                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
3758                )
3759            except ParseError:
3760                if udt:
3761                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
3762                raise
3763        elif isinstance(dtype, DataType.Type):
3764            data_type_exp = DataType(this=dtype)
3765        elif isinstance(dtype, DataType):
3766            return dtype
3767        else:
3768            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
3769
3770        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.
  • kawrgs: additional arguments to pass in the constructor of DataType.
Returns:

The constructed DataType object.

def is_type( self, *dtypes: Union[str, DataType, DataType.Type]) -> bool:
3772    def is_type(self, *dtypes: DATA_TYPE) -> bool:
3773        """
3774        Checks whether this DataType matches one of the provided data types. Nested types or precision
3775        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
3776
3777        Args:
3778            dtypes: the data types to compare this DataType to.
3779
3780        Returns:
3781            True, if and only if there is a type in `dtypes` which is equal to this DataType.
3782        """
3783        for dtype in dtypes:
3784            other = DataType.build(dtype, udt=True)
3785
3786            if (
3787                other.expressions
3788                or self.this == DataType.Type.USERDEFINED
3789                or other.this == DataType.Type.USERDEFINED
3790            ):
3791                matches = self == other
3792            else:
3793                matches = self.this == other.this
3794
3795            if matches:
3796                return True
3797        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.
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):
3589    class Type(AutoName):
3590        ARRAY = auto()
3591        BIGDECIMAL = auto()
3592        BIGINT = auto()
3593        BIGSERIAL = auto()
3594        BINARY = auto()
3595        BIT = auto()
3596        BOOLEAN = auto()
3597        CHAR = auto()
3598        DATE = auto()
3599        DATEMULTIRANGE = auto()
3600        DATERANGE = auto()
3601        DATETIME = auto()
3602        DATETIME64 = auto()
3603        DECIMAL = auto()
3604        DOUBLE = auto()
3605        ENUM = auto()
3606        ENUM8 = auto()
3607        ENUM16 = auto()
3608        FIXEDSTRING = auto()
3609        FLOAT = auto()
3610        GEOGRAPHY = auto()
3611        GEOMETRY = auto()
3612        HLLSKETCH = auto()
3613        HSTORE = auto()
3614        IMAGE = auto()
3615        INET = auto()
3616        INT = auto()
3617        INT128 = auto()
3618        INT256 = auto()
3619        INT4MULTIRANGE = auto()
3620        INT4RANGE = auto()
3621        INT8MULTIRANGE = auto()
3622        INT8RANGE = auto()
3623        INTERVAL = auto()
3624        IPADDRESS = auto()
3625        IPPREFIX = auto()
3626        JSON = auto()
3627        JSONB = auto()
3628        LONGBLOB = auto()
3629        LONGTEXT = auto()
3630        LOWCARDINALITY = auto()
3631        MAP = auto()
3632        MEDIUMBLOB = auto()
3633        MEDIUMINT = auto()
3634        MEDIUMTEXT = auto()
3635        MONEY = auto()
3636        NCHAR = auto()
3637        NESTED = auto()
3638        NULL = auto()
3639        NULLABLE = auto()
3640        NUMMULTIRANGE = auto()
3641        NUMRANGE = auto()
3642        NVARCHAR = auto()
3643        OBJECT = auto()
3644        ROWVERSION = auto()
3645        SERIAL = auto()
3646        SET = auto()
3647        SMALLINT = auto()
3648        SMALLMONEY = auto()
3649        SMALLSERIAL = auto()
3650        STRUCT = auto()
3651        SUPER = auto()
3652        TEXT = auto()
3653        TINYBLOB = auto()
3654        TINYTEXT = auto()
3655        TIME = auto()
3656        TIMETZ = auto()
3657        TIMESTAMP = auto()
3658        TIMESTAMPLTZ = auto()
3659        TIMESTAMPTZ = auto()
3660        TIMESTAMP_S = auto()
3661        TIMESTAMP_MS = auto()
3662        TIMESTAMP_NS = auto()
3663        TINYINT = auto()
3664        TSMULTIRANGE = auto()
3665        TSRANGE = auto()
3666        TSTZMULTIRANGE = auto()
3667        TSTZRANGE = auto()
3668        UBIGINT = auto()
3669        UINT = auto()
3670        UINT128 = auto()
3671        UINT256 = auto()
3672        UMEDIUMINT = auto()
3673        UDECIMAL = auto()
3674        UNIQUEIDENTIFIER = auto()
3675        UNKNOWN = auto()  # Sentinel value, useful for type annotation
3676        USERDEFINED = "USER-DEFINED"
3677        USMALLINT = auto()
3678        UTINYINT = auto()
3679        UUID = auto()
3680        VARBINARY = auto()
3681        VARCHAR = auto()
3682        VARIANT = auto()
3683        XML = auto()
3684        YEAR = auto()

An enumeration.

ARRAY = <Type.ARRAY: 'ARRAY'>
BIGDECIMAL = <Type.BIGDECIMAL: 'BIGDECIMAL'>
BIGINT = <Type.BIGINT: 'BIGINT'>
BIGSERIAL = <Type.BIGSERIAL: 'BIGSERIAL'>
BINARY = <Type.BINARY: 'BINARY'>
BIT = <Type.BIT: 'BIT'>
BOOLEAN = <Type.BOOLEAN: 'BOOLEAN'>
CHAR = <Type.CHAR: 'CHAR'>
DATE = <Type.DATE: 'DATE'>
DATEMULTIRANGE = <Type.DATEMULTIRANGE: 'DATEMULTIRANGE'>
DATERANGE = <Type.DATERANGE: 'DATERANGE'>
DATETIME = <Type.DATETIME: 'DATETIME'>
DATETIME64 = <Type.DATETIME64: 'DATETIME64'>
DECIMAL = <Type.DECIMAL: 'DECIMAL'>
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'>
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'>
JSON = <Type.JSON: 'JSON'>
JSONB = <Type.JSONB: 'JSONB'>
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'>
NCHAR = <Type.NCHAR: 'NCHAR'>
NESTED = <Type.NESTED: 'NESTED'>
NULL = <Type.NULL: 'NULL'>
NULLABLE = <Type.NULLABLE: 'NULLABLE'>
NUMMULTIRANGE = <Type.NUMMULTIRANGE: 'NUMMULTIRANGE'>
NUMRANGE = <Type.NUMRANGE: 'NUMRANGE'>
NVARCHAR = <Type.NVARCHAR: 'NVARCHAR'>
OBJECT = <Type.OBJECT: 'OBJECT'>
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'>
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'>
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'>
XML = <Type.XML: 'XML'>
YEAR = <Type.YEAR: 'YEAR'>
Inherited Members
enum.Enum
name
value
DATA_TYPE = typing.Union[str, DataType, DataType.Type]
class PseudoType(DataType):
3804class PseudoType(DataType):
3805    arg_types = {"this": True}
arg_types = {'this': True}
key = 'pseudotype'
class ObjectIdentifier(DataType):
3809class ObjectIdentifier(DataType):
3810    arg_types = {"this": True}
arg_types = {'this': True}
key = 'objectidentifier'
class SubqueryPredicate(Predicate):
3814class SubqueryPredicate(Predicate):
3815    pass
key = 'subquerypredicate'
class All(SubqueryPredicate):
3818class All(SubqueryPredicate):
3819    pass
key = 'all'
class Any(SubqueryPredicate):
3822class Any(SubqueryPredicate):
3823    pass
key = 'any'
class Exists(SubqueryPredicate):
3826class Exists(SubqueryPredicate):
3827    pass
key = 'exists'
class Command(Expression):
3832class Command(Expression):
3833    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'command'
class Transaction(Expression):
3836class Transaction(Expression):
3837    arg_types = {"this": False, "modes": False, "mark": False}
arg_types = {'this': False, 'modes': False, 'mark': False}
key = 'transaction'
class Commit(Expression):
3840class Commit(Expression):
3841    arg_types = {"chain": False, "this": False, "durability": False}
arg_types = {'chain': False, 'this': False, 'durability': False}
key = 'commit'
class Rollback(Expression):
3844class Rollback(Expression):
3845    arg_types = {"savepoint": False, "this": False}
arg_types = {'savepoint': False, 'this': False}
key = 'rollback'
class AlterTable(Expression):
3848class AlterTable(Expression):
3849    arg_types = {"this": True, "actions": True, "exists": False, "only": False}
arg_types = {'this': True, 'actions': True, 'exists': False, 'only': False}
key = 'altertable'
class AddConstraint(Expression):
3852class AddConstraint(Expression):
3853    arg_types = {"this": False, "expression": False, "enforced": False}
arg_types = {'this': False, 'expression': False, 'enforced': False}
key = 'addconstraint'
class DropPartition(Expression):
3856class DropPartition(Expression):
3857    arg_types = {"expressions": True, "exists": False}
arg_types = {'expressions': True, 'exists': False}
key = 'droppartition'
class Binary(Condition):
3861class Binary(Condition):
3862    arg_types = {"this": True, "expression": True}
3863
3864    @property
3865    def left(self) -> Expression:
3866        return self.this
3867
3868    @property
3869    def right(self) -> Expression:
3870        return self.expression
arg_types = {'this': True, 'expression': True}
left: Expression
right: Expression
key = 'binary'
class Add(Binary):
3873class Add(Binary):
3874    pass
key = 'add'
class Connector(Binary):
3877class Connector(Binary):
3878    pass
key = 'connector'
class And(Connector):
3881class And(Connector):
3882    pass
key = 'and'
class Or(Connector):
3885class Or(Connector):
3886    pass
key = 'or'
class BitwiseAnd(Binary):
3889class BitwiseAnd(Binary):
3890    pass
key = 'bitwiseand'
class BitwiseLeftShift(Binary):
3893class BitwiseLeftShift(Binary):
3894    pass
key = 'bitwiseleftshift'
class BitwiseOr(Binary):
3897class BitwiseOr(Binary):
3898    pass
key = 'bitwiseor'
class BitwiseRightShift(Binary):
3901class BitwiseRightShift(Binary):
3902    pass
key = 'bitwiserightshift'
class BitwiseXor(Binary):
3905class BitwiseXor(Binary):
3906    pass
key = 'bitwisexor'
class Div(Binary):
3909class Div(Binary):
3910    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):
3913class Overlaps(Binary):
3914    pass
key = 'overlaps'
class Dot(Binary):
3917class Dot(Binary):
3918    @property
3919    def name(self) -> str:
3920        return self.expression.name
3921
3922    @property
3923    def output_name(self) -> str:
3924        return self.name
3925
3926    @classmethod
3927    def build(self, expressions: t.Sequence[Expression]) -> Dot:
3928        """Build a Dot object with a sequence of expressions."""
3929        if len(expressions) < 2:
3930            raise ValueError(f"Dot requires >= 2 expressions.")
3931
3932        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))
3933
3934    @property
3935    def parts(self) -> t.List[Expression]:
3936        """Return the parts of a table / column in order catalog, db, table."""
3937        this, *parts = self.flatten()
3938
3939        parts.reverse()
3940
3941        for arg in ("this", "table", "db", "catalog"):
3942            part = this.args.get(arg)
3943
3944            if isinstance(part, Expression):
3945                parts.append(part)
3946
3947        parts.reverse()
3948        return parts
name: str
output_name: str

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:
3926    @classmethod
3927    def build(self, expressions: t.Sequence[Expression]) -> Dot:
3928        """Build a Dot object with a sequence of expressions."""
3929        if len(expressions) < 2:
3930            raise ValueError(f"Dot requires >= 2 expressions.")
3931
3932        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]

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

key = 'dot'
class DPipe(Binary):
3951class DPipe(Binary):
3952    arg_types = {"this": True, "expression": True, "safe": False}
arg_types = {'this': True, 'expression': True, 'safe': False}
key = 'dpipe'
class EQ(Binary, Predicate):
3955class EQ(Binary, Predicate):
3956    pass
key = 'eq'
class NullSafeEQ(Binary, Predicate):
3959class NullSafeEQ(Binary, Predicate):
3960    pass
key = 'nullsafeeq'
class NullSafeNEQ(Binary, Predicate):
3963class NullSafeNEQ(Binary, Predicate):
3964    pass
key = 'nullsafeneq'
class PropertyEQ(Binary):
3968class PropertyEQ(Binary):
3969    pass
key = 'propertyeq'
class Distance(Binary):
3972class Distance(Binary):
3973    pass
key = 'distance'
class Escape(Binary):
3976class Escape(Binary):
3977    pass
key = 'escape'
class Glob(Binary, Predicate):
3980class Glob(Binary, Predicate):
3981    pass
key = 'glob'
class GT(Binary, Predicate):
3984class GT(Binary, Predicate):
3985    pass
key = 'gt'
class GTE(Binary, Predicate):
3988class GTE(Binary, Predicate):
3989    pass
key = 'gte'
class ILike(Binary, Predicate):
3992class ILike(Binary, Predicate):
3993    pass
key = 'ilike'
class ILikeAny(Binary, Predicate):
3996class ILikeAny(Binary, Predicate):
3997    pass
key = 'ilikeany'
class IntDiv(Binary):
4000class IntDiv(Binary):
4001    pass
key = 'intdiv'
class Is(Binary, Predicate):
4004class Is(Binary, Predicate):
4005    pass
key = 'is'
class Kwarg(Binary):
4008class Kwarg(Binary):
4009    """Kwarg in special functions like func(kwarg => y)."""

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

key = 'kwarg'
class Like(Binary, Predicate):
4012class Like(Binary, Predicate):
4013    pass
key = 'like'
class LikeAny(Binary, Predicate):
4016class LikeAny(Binary, Predicate):
4017    pass
key = 'likeany'
class LT(Binary, Predicate):
4020class LT(Binary, Predicate):
4021    pass
key = 'lt'
class LTE(Binary, Predicate):
4024class LTE(Binary, Predicate):
4025    pass
key = 'lte'
class Mod(Binary):
4028class Mod(Binary):
4029    pass
key = 'mod'
class Mul(Binary):
4032class Mul(Binary):
4033    pass
key = 'mul'
class NEQ(Binary, Predicate):
4036class NEQ(Binary, Predicate):
4037    pass
key = 'neq'
class Operator(Binary):
4041class Operator(Binary):
4042    arg_types = {"this": True, "operator": True, "expression": True}
arg_types = {'this': True, 'operator': True, 'expression': True}
key = 'operator'
class SimilarTo(Binary, Predicate):
4045class SimilarTo(Binary, Predicate):
4046    pass
key = 'similarto'
class Slice(Binary):
4049class Slice(Binary):
4050    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'slice'
class Sub(Binary):
4053class Sub(Binary):
4054    pass
key = 'sub'
class ArrayOverlaps(Binary):
4057class ArrayOverlaps(Binary):
4058    pass
key = 'arrayoverlaps'
class Unary(Condition):
4063class Unary(Condition):
4064    pass
key = 'unary'
class BitwiseNot(Unary):
4067class BitwiseNot(Unary):
4068    pass
key = 'bitwisenot'
class Not(Unary):
4071class Not(Unary):
4072    pass
key = 'not'
class Paren(Unary):
4075class Paren(Unary):
4076    arg_types = {"this": True, "with": False}
4077
4078    @property
4079    def output_name(self) -> str:
4080        return self.this.name
arg_types = {'this': True, 'with': False}
output_name: str

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):
4083class Neg(Unary):
4084    pass
key = 'neg'
class Alias(Expression):
4087class Alias(Expression):
4088    arg_types = {"this": True, "alias": False}
4089
4090    @property
4091    def output_name(self) -> str:
4092        return self.alias
arg_types = {'this': True, 'alias': False}
output_name: str

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 Aliases(Expression):
4095class Aliases(Expression):
4096    arg_types = {"this": True, "expressions": True}
4097
4098    @property
4099    def aliases(self):
4100        return self.expressions
arg_types = {'this': True, 'expressions': True}
aliases
key = 'aliases'
class AtTimeZone(Expression):
4103class AtTimeZone(Expression):
4104    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'attimezone'
class Between(Predicate):
4107class Between(Predicate):
4108    arg_types = {"this": True, "low": True, "high": True}
arg_types = {'this': True, 'low': True, 'high': True}
key = 'between'
class Bracket(Condition):
4111class Bracket(Condition):
4112    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
4113    arg_types = {"this": True, "expressions": True, "offset": False, "safe": False}
4114
4115    @property
4116    def output_name(self) -> str:
4117        if len(self.expressions) == 1:
4118            return self.expressions[0].output_name
4119
4120        return super().output_name
arg_types = {'this': True, 'expressions': True, 'offset': False, 'safe': False}
output_name: str

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):
4123class Distinct(Expression):
4124    arg_types = {"expressions": False, "on": False}
arg_types = {'expressions': False, 'on': False}
key = 'distinct'
class In(Predicate):
4127class In(Predicate):
4128    arg_types = {
4129        "this": True,
4130        "expressions": False,
4131        "query": False,
4132        "unnest": False,
4133        "field": False,
4134        "is_global": False,
4135    }
arg_types = {'this': True, 'expressions': False, 'query': False, 'unnest': False, 'field': False, 'is_global': False}
key = 'in'
class ForIn(Expression):
4139class ForIn(Expression):
4140    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'forin'
class TimeUnit(Expression):
4143class TimeUnit(Expression):
4144    """Automatically converts unit arg into a var."""
4145
4146    arg_types = {"unit": False}
4147
4148    UNABBREVIATED_UNIT_NAME = {
4149        "d": "day",
4150        "h": "hour",
4151        "m": "minute",
4152        "ms": "millisecond",
4153        "ns": "nanosecond",
4154        "q": "quarter",
4155        "s": "second",
4156        "us": "microsecond",
4157        "w": "week",
4158        "y": "year",
4159    }
4160
4161    VAR_LIKE = (Column, Literal, Var)
4162
4163    def __init__(self, **args):
4164        unit = args.get("unit")
4165        if isinstance(unit, self.VAR_LIKE):
4166            args["unit"] = Var(this=self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name)
4167        elif isinstance(unit, Week):
4168            unit.set("this", Var(this=unit.this.name))
4169
4170        super().__init__(**args)
4171
4172    @property
4173    def unit(self) -> t.Optional[Var]:
4174        return self.args.get("unit")

Automatically converts unit arg into a var.

TimeUnit(**args)
4163    def __init__(self, **args):
4164        unit = args.get("unit")
4165        if isinstance(unit, self.VAR_LIKE):
4166            args["unit"] = Var(this=self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name)
4167        elif isinstance(unit, Week):
4168            unit.set("this", Var(this=unit.this.name))
4169
4170        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: Optional[Var]
key = 'timeunit'
class IntervalOp(TimeUnit):
4177class IntervalOp(TimeUnit):
4178    arg_types = {"unit": True, "expression": True}
4179
4180    def interval(self):
4181        return Interval(
4182            this=self.expression.copy(),
4183            unit=self.unit.copy(),
4184        )
arg_types = {'unit': True, 'expression': True}
def interval(self):
4180    def interval(self):
4181        return Interval(
4182            this=self.expression.copy(),
4183            unit=self.unit.copy(),
4184        )
key = 'intervalop'
class IntervalSpan(DataType):
4190class IntervalSpan(DataType):
4191    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'intervalspan'
class Interval(TimeUnit):
4194class Interval(TimeUnit):
4195    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key = 'interval'
class IgnoreNulls(Expression):
4198class IgnoreNulls(Expression):
4199    pass
key = 'ignorenulls'
class RespectNulls(Expression):
4202class RespectNulls(Expression):
4203    pass
key = 'respectnulls'
class Func(Condition):
4207class Func(Condition):
4208    """
4209    The base class for all function expressions.
4210
4211    Attributes:
4212        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
4213            treated as a variable length argument and the argument's value will be stored as a list.
4214        _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items)
4215            for this function expression. These values are used to map this node to a name during parsing
4216            as well as to provide the function's name during SQL string generation. By default the SQL
4217            name is set to the expression's class name transformed to snake case.
4218    """
4219
4220    is_var_len_args = False
4221
4222    @classmethod
4223    def from_arg_list(cls, args):
4224        if cls.is_var_len_args:
4225            all_arg_keys = list(cls.arg_types)
4226            # If this function supports variable length argument treat the last argument as such.
4227            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
4228            num_non_var = len(non_var_len_arg_keys)
4229
4230            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
4231            args_dict[all_arg_keys[-1]] = args[num_non_var:]
4232        else:
4233            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
4234
4235        return cls(**args_dict)
4236
4237    @classmethod
4238    def sql_names(cls):
4239        if cls is Func:
4240            raise NotImplementedError(
4241                "SQL name is only supported by concrete function implementations"
4242            )
4243        if "_sql_names" not in cls.__dict__:
4244            cls._sql_names = [camel_to_snake_case(cls.__name__)]
4245        return cls._sql_names
4246
4247    @classmethod
4248    def sql_name(cls):
4249        return cls.sql_names()[0]
4250
4251    @classmethod
4252    def default_parser_mappings(cls):
4253        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): determines 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):
4222    @classmethod
4223    def from_arg_list(cls, args):
4224        if cls.is_var_len_args:
4225            all_arg_keys = list(cls.arg_types)
4226            # If this function supports variable length argument treat the last argument as such.
4227            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
4228            num_non_var = len(non_var_len_arg_keys)
4229
4230            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
4231            args_dict[all_arg_keys[-1]] = args[num_non_var:]
4232        else:
4233            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
4234
4235        return cls(**args_dict)
@classmethod
def sql_names(cls):
4237    @classmethod
4238    def sql_names(cls):
4239        if cls is Func:
4240            raise NotImplementedError(
4241                "SQL name is only supported by concrete function implementations"
4242            )
4243        if "_sql_names" not in cls.__dict__:
4244            cls._sql_names = [camel_to_snake_case(cls.__name__)]
4245        return cls._sql_names
@classmethod
def sql_name(cls):
4247    @classmethod
4248    def sql_name(cls):
4249        return cls.sql_names()[0]
@classmethod
def default_parser_mappings(cls):
4251    @classmethod
4252    def default_parser_mappings(cls):
4253        return {name: cls.from_arg_list for name in cls.sql_names()}
key = 'func'
class AggFunc(Func):
4256class AggFunc(Func):
4257    pass
key = 'aggfunc'
class ParameterizedAgg(AggFunc):
4260class ParameterizedAgg(AggFunc):
4261    arg_types = {"this": True, "expressions": True, "params": True}
arg_types = {'this': True, 'expressions': True, 'params': True}
key = 'parameterizedagg'
class Abs(Func):
4264class Abs(Func):
4265    pass
key = 'abs'
class ArgMax(AggFunc):
4268class ArgMax(AggFunc):
4269    arg_types = {"this": True, "expression": True, "count": False}
4270    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmax'
class ArgMin(AggFunc):
4273class ArgMin(AggFunc):
4274    arg_types = {"this": True, "expression": True, "count": False}
4275    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmin'
class ApproxTopK(AggFunc):
4278class ApproxTopK(AggFunc):
4279    arg_types = {"this": True, "expression": False, "counters": False}
arg_types = {'this': True, 'expression': False, 'counters': False}
key = 'approxtopk'
class Flatten(Func):
4282class Flatten(Func):
4283    pass
key = 'flatten'
class Transform(Func):
4287class Transform(Func):
4288    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'transform'
class Anonymous(Func):
4291class Anonymous(Func):
4292    arg_types = {"this": True, "expressions": False}
4293    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'anonymous'
class Hll(AggFunc):
4298class Hll(AggFunc):
4299    arg_types = {"this": True, "expressions": False}
4300    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'hll'
class ApproxDistinct(AggFunc):
4303class ApproxDistinct(AggFunc):
4304    arg_types = {"this": True, "accuracy": False}
4305    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
arg_types = {'this': True, 'accuracy': False}
key = 'approxdistinct'
class Array(Func):
4308class Array(Func):
4309    arg_types = {"expressions": False}
4310    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'array'
class ToChar(Func):
4315class ToChar(Func):
4316    arg_types = {"this": True, "format": False, "nlsparam": False}
arg_types = {'this': True, 'format': False, 'nlsparam': False}
key = 'tochar'
class GenerateSeries(Func):
4319class GenerateSeries(Func):
4320    arg_types = {"start": True, "end": True, "step": False}
arg_types = {'start': True, 'end': True, 'step': False}
key = 'generateseries'
class ArrayAgg(AggFunc):
4323class ArrayAgg(AggFunc):
4324    pass
key = 'arrayagg'
class ArrayUniqueAgg(AggFunc):
4327class ArrayUniqueAgg(AggFunc):
4328    pass
key = 'arrayuniqueagg'
class ArrayAll(Func):
4331class ArrayAll(Func):
4332    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayall'
class ArrayAny(Func):
4335class ArrayAny(Func):
4336    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayany'
class ArrayConcat(Func):
4339class ArrayConcat(Func):
4340    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
4341    arg_types = {"this": True, "expressions": False}
4342    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'arrayconcat'
class ArrayContains(Binary, Func):
4345class ArrayContains(Binary, Func):
4346    pass
key = 'arraycontains'
class ArrayContained(Binary):
4349class ArrayContained(Binary):
4350    pass
key = 'arraycontained'
class ArrayFilter(Func):
4353class ArrayFilter(Func):
4354    arg_types = {"this": True, "expression": True}
4355    _sql_names = ["FILTER", "ARRAY_FILTER"]
arg_types = {'this': True, 'expression': True}
key = 'arrayfilter'
class ArrayJoin(Func):
4358class ArrayJoin(Func):
4359    arg_types = {"this": True, "expression": True, "null": False}
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'arrayjoin'
class ArraySize(Func):
4362class ArraySize(Func):
4363    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysize'
class ArraySort(Func):
4366class ArraySort(Func):
4367    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysort'
class ArraySum(Func):
4370class ArraySum(Func):
4371    pass
key = 'arraysum'
class ArrayUnionAgg(AggFunc):
4374class ArrayUnionAgg(AggFunc):
4375    pass
key = 'arrayunionagg'
class Avg(AggFunc):
4378class Avg(AggFunc):
4379    pass
key = 'avg'
class AnyValue(AggFunc):
4382class AnyValue(AggFunc):
4383    arg_types = {"this": True, "having": False, "max": False, "ignore_nulls": False}
arg_types = {'this': True, 'having': False, 'max': False, 'ignore_nulls': False}
key = 'anyvalue'
class First(Func):
4386class First(Func):
4387    arg_types = {"this": True, "ignore_nulls": False}
arg_types = {'this': True, 'ignore_nulls': False}
key = 'first'
class Last(Func):
4390class Last(Func):
4391    arg_types = {"this": True, "ignore_nulls": False}
arg_types = {'this': True, 'ignore_nulls': False}
key = 'last'
class Case(Func):
4394class Case(Func):
4395    arg_types = {"this": False, "ifs": True, "default": False}
4396
4397    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
4398        instance = maybe_copy(self, copy)
4399        instance.append(
4400            "ifs",
4401            If(
4402                this=maybe_parse(condition, copy=copy, **opts),
4403                true=maybe_parse(then, copy=copy, **opts),
4404            ),
4405        )
4406        return instance
4407
4408    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
4409        instance = maybe_copy(self, copy)
4410        instance.set("default", maybe_parse(condition, copy=copy, **opts))
4411        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:
4397    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
4398        instance = maybe_copy(self, copy)
4399        instance.append(
4400            "ifs",
4401            If(
4402                this=maybe_parse(condition, copy=copy, **opts),
4403                true=maybe_parse(then, copy=copy, **opts),
4404            ),
4405        )
4406        return instance
def else_( self, condition: Union[str, Expression], copy: bool = True, **opts) -> Case:
4408    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
4409        instance = maybe_copy(self, copy)
4410        instance.set("default", maybe_parse(condition, copy=copy, **opts))
4411        return instance
key = 'case'
class Cast(Func):
4414class Cast(Func):
4415    arg_types = {"this": True, "to": True, "format": False, "safe": False}
4416
4417    @property
4418    def name(self) -> str:
4419        return self.this.name
4420
4421    @property
4422    def to(self) -> DataType:
4423        return self.args["to"]
4424
4425    @property
4426    def output_name(self) -> str:
4427        return self.name
4428
4429    def is_type(self, *dtypes: DATA_TYPE) -> bool:
4430        """
4431        Checks whether this Cast's DataType matches one of the provided data types. Nested types
4432        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
4433        array<int> != array<float>.
4434
4435        Args:
4436            dtypes: the data types to compare this Cast's DataType to.
4437
4438        Returns:
4439            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
4440        """
4441        return self.to.is_type(*dtypes)
arg_types = {'this': True, 'to': True, 'format': False, 'safe': False}
name: str
to: DataType
output_name: str

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:
4429    def is_type(self, *dtypes: DATA_TYPE) -> bool:
4430        """
4431        Checks whether this Cast's DataType matches one of the provided data types. Nested types
4432        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
4433        array<int> != array<float>.
4434
4435        Args:
4436            dtypes: the data types to compare this Cast's DataType to.
4437
4438        Returns:
4439            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
4440        """
4441        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):
4444class TryCast(Cast):
4445    pass
key = 'trycast'
class CastToStrType(Func):
4448class CastToStrType(Func):
4449    arg_types = {"this": True, "to": True}
arg_types = {'this': True, 'to': True}
key = 'casttostrtype'
class Collate(Binary, Func):
4452class Collate(Binary, Func):
4453    pass
key = 'collate'
class Ceil(Func):
4456class Ceil(Func):
4457    arg_types = {"this": True, "decimals": False}
4458    _sql_names = ["CEIL", "CEILING"]
arg_types = {'this': True, 'decimals': False}
key = 'ceil'
class Coalesce(Func):
4461class Coalesce(Func):
4462    arg_types = {"this": True, "expressions": False}
4463    is_var_len_args = True
4464    _sql_names = ["COALESCE", "IFNULL", "NVL"]
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'coalesce'
class Chr(Func):
4467class Chr(Func):
4468    arg_types = {"this": True, "charset": False, "expressions": False}
4469    is_var_len_args = True
4470    _sql_names = ["CHR", "CHAR"]
arg_types = {'this': True, 'charset': False, 'expressions': False}
is_var_len_args = True
key = 'chr'
class Concat(Func):
4473class Concat(Func):
4474    arg_types = {"expressions": True, "safe": False, "coalesce": False}
4475    is_var_len_args = True
arg_types = {'expressions': True, 'safe': False, 'coalesce': False}
is_var_len_args = True
key = 'concat'
class ConcatWs(Concat):
4478class ConcatWs(Concat):
4479    _sql_names = ["CONCAT_WS"]
key = 'concatws'
class Count(AggFunc):
4482class Count(AggFunc):
4483    arg_types = {"this": False, "expressions": False}
4484    is_var_len_args = True
arg_types = {'this': False, 'expressions': False}
is_var_len_args = True
key = 'count'
class CountIf(AggFunc):
4487class CountIf(AggFunc):
4488    pass
key = 'countif'
class CurrentDate(Func):
4491class CurrentDate(Func):
4492    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdate'
class CurrentDatetime(Func):
4495class CurrentDatetime(Func):
4496    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdatetime'
class CurrentTime(Func):
4499class CurrentTime(Func):
4500    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttime'
class CurrentTimestamp(Func):
4503class CurrentTimestamp(Func):
4504    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttimestamp'
class CurrentUser(Func):
4507class CurrentUser(Func):
4508    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentuser'
class DateAdd(Func, IntervalOp):
4511class DateAdd(Func, IntervalOp):
4512    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'dateadd'
class DateSub(Func, IntervalOp):
4515class DateSub(Func, IntervalOp):
4516    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datesub'
class DateDiff(Func, TimeUnit):
4519class DateDiff(Func, TimeUnit):
4520    _sql_names = ["DATEDIFF", "DATE_DIFF"]
4521    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datediff'
class DateTrunc(Func):
4524class DateTrunc(Func):
4525    arg_types = {"unit": True, "this": True, "zone": False}
4526
4527    @property
4528    def unit(self) -> Expression:
4529        return self.args["unit"]
arg_types = {'unit': True, 'this': True, 'zone': False}
unit: Expression
key = 'datetrunc'
class DatetimeAdd(Func, IntervalOp):
4532class DatetimeAdd(Func, IntervalOp):
4533    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimeadd'
class DatetimeSub(Func, IntervalOp):
4536class DatetimeSub(Func, IntervalOp):
4537    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimesub'
class DatetimeDiff(Func, TimeUnit):
4540class DatetimeDiff(Func, TimeUnit):
4541    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimediff'
class DatetimeTrunc(Func, TimeUnit):
4544class DatetimeTrunc(Func, TimeUnit):
4545    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'datetimetrunc'
class DayOfWeek(Func):
4548class DayOfWeek(Func):
4549    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
key = 'dayofweek'
class DayOfMonth(Func):
4552class DayOfMonth(Func):
4553    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
key = 'dayofmonth'
class DayOfYear(Func):
4556class DayOfYear(Func):
4557    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
key = 'dayofyear'
class ToDays(Func):
4560class ToDays(Func):
4561    pass
key = 'todays'
class WeekOfYear(Func):
4564class WeekOfYear(Func):
4565    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
key = 'weekofyear'
class MonthsBetween(Func):
4568class MonthsBetween(Func):
4569    arg_types = {"this": True, "expression": True, "roundoff": False}
arg_types = {'this': True, 'expression': True, 'roundoff': False}
key = 'monthsbetween'
class LastDateOfMonth(Func):
4572class LastDateOfMonth(Func):
4573    pass
key = 'lastdateofmonth'
class Extract(Func):
4576class Extract(Func):
4577    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'extract'
class Timestamp(Func):
4580class Timestamp(Func):
4581    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'timestamp'
class TimestampAdd(Func, TimeUnit):
4584class TimestampAdd(Func, TimeUnit):
4585    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampadd'
class TimestampSub(Func, TimeUnit):
4588class TimestampSub(Func, TimeUnit):
4589    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampsub'
class TimestampDiff(Func, TimeUnit):
4592class TimestampDiff(Func, TimeUnit):
4593    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampdiff'
class TimestampTrunc(Func, TimeUnit):
4596class TimestampTrunc(Func, TimeUnit):
4597    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timestamptrunc'
class TimeAdd(Func, TimeUnit):
4600class TimeAdd(Func, TimeUnit):
4601    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timeadd'
class TimeSub(Func, TimeUnit):
4604class TimeSub(Func, TimeUnit):
4605    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timesub'
class TimeDiff(Func, TimeUnit):
4608class TimeDiff(Func, TimeUnit):
4609    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timediff'
class TimeTrunc(Func, TimeUnit):
4612class TimeTrunc(Func, TimeUnit):
4613    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timetrunc'
class DateFromParts(Func):
4616class DateFromParts(Func):
4617    _sql_names = ["DATEFROMPARTS"]
4618    arg_types = {"year": True, "month": True, "day": True}
arg_types = {'year': True, 'month': True, 'day': True}
key = 'datefromparts'
class DateStrToDate(Func):
4621class DateStrToDate(Func):
4622    pass
key = 'datestrtodate'
class DateToDateStr(Func):
4625class DateToDateStr(Func):
4626    pass
key = 'datetodatestr'
class DateToDi(Func):
4629class DateToDi(Func):
4630    pass
key = 'datetodi'
class Date(Func):
4634class Date(Func):
4635    arg_types = {"this": False, "zone": False, "expressions": False}
4636    is_var_len_args = True
arg_types = {'this': False, 'zone': False, 'expressions': False}
is_var_len_args = True
key = 'date'
class Day(Func):
4639class Day(Func):
4640    pass
key = 'day'
class Decode(Func):
4643class Decode(Func):
4644    arg_types = {"this": True, "charset": True, "replace": False}
arg_types = {'this': True, 'charset': True, 'replace': False}
key = 'decode'
class DiToDate(Func):
4647class DiToDate(Func):
4648    pass
key = 'ditodate'
class Encode(Func):
4651class Encode(Func):
4652    arg_types = {"this": True, "charset": True}
arg_types = {'this': True, 'charset': True}
key = 'encode'
class Exp(Func):
4655class Exp(Func):
4656    pass
key = 'exp'
class Explode(Func):
4660class Explode(Func):
4661    arg_types = {"this": True, "expressions": False}
4662    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'explode'
class ExplodeOuter(Explode):
4665class ExplodeOuter(Explode):
4666    pass
key = 'explodeouter'
class Posexplode(Explode):
4669class Posexplode(Explode):
4670    pass
key = 'posexplode'
class PosexplodeOuter(Posexplode):
4673class PosexplodeOuter(Posexplode):
4674    pass
key = 'posexplodeouter'
class Floor(Func):
4677class Floor(Func):
4678    arg_types = {"this": True, "decimals": False}
arg_types = {'this': True, 'decimals': False}
key = 'floor'
class FromBase64(Func):
4681class FromBase64(Func):
4682    pass
key = 'frombase64'
class ToBase64(Func):
4685class ToBase64(Func):
4686    pass
key = 'tobase64'
class Greatest(Func):
4689class Greatest(Func):
4690    arg_types = {"this": True, "expressions": False}
4691    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'greatest'
class GroupConcat(AggFunc):
4694class GroupConcat(AggFunc):
4695    arg_types = {"this": True, "separator": False}
arg_types = {'this': True, 'separator': False}
key = 'groupconcat'
class Hex(Func):
4698class Hex(Func):
4699    pass
key = 'hex'
class Xor(Connector, Func):
4702class Xor(Connector, Func):
4703    arg_types = {"this": False, "expression": False, "expressions": False}
arg_types = {'this': False, 'expression': False, 'expressions': False}
key = 'xor'
class If(Func):
4706class If(Func):
4707    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'if'
class Nullif(Func):
4710class Nullif(Func):
4711    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'nullif'
class Initcap(Func):
4714class Initcap(Func):
4715    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'initcap'
class IsNan(Func):
4718class IsNan(Func):
4719    _sql_names = ["IS_NAN", "ISNAN"]
key = 'isnan'
class IsInf(Func):
4722class IsInf(Func):
4723    _sql_names = ["IS_INF", "ISINF"]
key = 'isinf'
class FormatJson(Expression):
4726class FormatJson(Expression):
4727    pass
key = 'formatjson'
class JSONKeyValue(Expression):
4730class JSONKeyValue(Expression):
4731    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'jsonkeyvalue'
class JSONObject(Func):
4734class JSONObject(Func):
4735    arg_types = {
4736        "expressions": False,
4737        "null_handling": False,
4738        "unique_keys": False,
4739        "return_type": False,
4740        "encoding": False,
4741    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobject'
class JSONArray(Func):
4745class JSONArray(Func):
4746    arg_types = {
4747        "expressions": True,
4748        "null_handling": False,
4749        "return_type": False,
4750        "strict": False,
4751    }
arg_types = {'expressions': True, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarray'
class JSONArrayAgg(Func):
4755class JSONArrayAgg(Func):
4756    arg_types = {
4757        "this": True,
4758        "order": False,
4759        "null_handling": False,
4760        "return_type": False,
4761        "strict": False,
4762    }
arg_types = {'this': True, 'order': False, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarrayagg'
class JSONColumnDef(Expression):
4767class JSONColumnDef(Expression):
4768    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):
4771class JSONSchema(Expression):
4772    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonschema'
class JSONTable(Func):
4776class JSONTable(Func):
4777    arg_types = {
4778        "this": True,
4779        "schema": True,
4780        "path": False,
4781        "error_handling": False,
4782        "empty_handling": False,
4783    }
arg_types = {'this': True, 'schema': True, 'path': False, 'error_handling': False, 'empty_handling': False}
key = 'jsontable'
class OpenJSONColumnDef(Expression):
4786class OpenJSONColumnDef(Expression):
4787    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):
4790class OpenJSON(Func):
4791    arg_types = {"this": True, "path": False, "expressions": False}
arg_types = {'this': True, 'path': False, 'expressions': False}
key = 'openjson'
class JSONBContains(Binary):
4794class JSONBContains(Binary):
4795    _sql_names = ["JSONB_CONTAINS"]
key = 'jsonbcontains'
class JSONExtract(Binary, Func):
4798class JSONExtract(Binary, Func):
4799    _sql_names = ["JSON_EXTRACT"]
key = 'jsonextract'
class JSONExtractScalar(JSONExtract):
4802class JSONExtractScalar(JSONExtract):
4803    _sql_names = ["JSON_EXTRACT_SCALAR"]
key = 'jsonextractscalar'
class JSONBExtract(JSONExtract):
4806class JSONBExtract(JSONExtract):
4807    _sql_names = ["JSONB_EXTRACT"]
key = 'jsonbextract'
class JSONBExtractScalar(JSONExtract):
4810class JSONBExtractScalar(JSONExtract):
4811    _sql_names = ["JSONB_EXTRACT_SCALAR"]
key = 'jsonbextractscalar'
class JSONFormat(Func):
4814class JSONFormat(Func):
4815    arg_types = {"this": False, "options": False}
4816    _sql_names = ["JSON_FORMAT"]
arg_types = {'this': False, 'options': False}
key = 'jsonformat'
class JSONArrayContains(Binary, Predicate, Func):
4820class JSONArrayContains(Binary, Predicate, Func):
4821    _sql_names = ["JSON_ARRAY_CONTAINS"]
key = 'jsonarraycontains'
class ParseJSON(Func):
4824class ParseJSON(Func):
4825    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
4826    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
4827    arg_types = {"this": True, "expressions": False}
4828    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'parsejson'
class Least(Func):
4831class Least(Func):
4832    arg_types = {"this": True, "expressions": False}
4833    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'least'
class Left(Func):
4836class Left(Func):
4837    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'left'
class Length(Func):
4844class Length(Func):
4845    _sql_names = ["LENGTH", "LEN"]
key = 'length'
class Levenshtein(Func):
4848class Levenshtein(Func):
4849    arg_types = {
4850        "this": True,
4851        "expression": False,
4852        "ins_cost": False,
4853        "del_cost": False,
4854        "sub_cost": False,
4855    }
arg_types = {'this': True, 'expression': False, 'ins_cost': False, 'del_cost': False, 'sub_cost': False}
key = 'levenshtein'
class Ln(Func):
4858class Ln(Func):
4859    pass
key = 'ln'
class Log(Func):
4862class Log(Func):
4863    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'log'
class Log2(Func):
4866class Log2(Func):
4867    pass
key = 'log2'
class Log10(Func):
4870class Log10(Func):
4871    pass
key = 'log10'
class LogicalOr(AggFunc):
4874class LogicalOr(AggFunc):
4875    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
key = 'logicalor'
class LogicalAnd(AggFunc):
4878class LogicalAnd(AggFunc):
4879    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
key = 'logicaland'
class Lower(Func):
4882class Lower(Func):
4883    _sql_names = ["LOWER", "LCASE"]
key = 'lower'
class Map(Func):
4886class Map(Func):
4887    arg_types = {"keys": False, "values": False}
4888
4889    @property
4890    def keys(self) -> t.List[Expression]:
4891        keys = self.args.get("keys")
4892        return keys.expressions if keys else []
4893
4894    @property
4895    def values(self) -> t.List[Expression]:
4896        values = self.args.get("values")
4897        return values.expressions if values else []
arg_types = {'keys': False, 'values': False}
keys: List[Expression]
values: List[Expression]
key = 'map'
class MapFromEntries(Func):
4900class MapFromEntries(Func):
4901    pass
key = 'mapfromentries'
class StarMap(Func):
4904class StarMap(Func):
4905    pass
key = 'starmap'
class VarMap(Func):
4908class VarMap(Func):
4909    arg_types = {"keys": True, "values": True}
4910    is_var_len_args = True
4911
4912    @property
4913    def keys(self) -> t.List[Expression]:
4914        return self.args["keys"].expressions
4915
4916    @property
4917    def values(self) -> t.List[Expression]:
4918        return self.args["values"].expressions
arg_types = {'keys': True, 'values': True}
is_var_len_args = True
keys: List[Expression]
values: List[Expression]
key = 'varmap'
class MatchAgainst(Func):
4922class MatchAgainst(Func):
4923    arg_types = {"this": True, "expressions": True, "modifier": False}
arg_types = {'this': True, 'expressions': True, 'modifier': False}
key = 'matchagainst'
class Max(AggFunc):
4926class Max(AggFunc):
4927    arg_types = {"this": True, "expressions": False}
4928    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'max'
class MD5(Func):
4931class MD5(Func):
4932    _sql_names = ["MD5"]
key = 'md5'
class MD5Digest(Func):
4936class MD5Digest(Func):
4937    _sql_names = ["MD5_DIGEST"]
key = 'md5digest'
class Min(AggFunc):
4940class Min(AggFunc):
4941    arg_types = {"this": True, "expressions": False}
4942    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'min'
class Month(Func):
4945class Month(Func):
4946    pass
key = 'month'
class Nvl2(Func):
4949class Nvl2(Func):
4950    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'nvl2'
class Predict(Func):
4954class Predict(Func):
4955    arg_types = {"this": True, "expression": True, "params_struct": False}
arg_types = {'this': True, 'expression': True, 'params_struct': False}
key = 'predict'
class Pow(Binary, Func):
4958class Pow(Binary, Func):
4959    _sql_names = ["POWER", "POW"]
key = 'pow'
class PercentileCont(AggFunc):
4962class PercentileCont(AggFunc):
4963    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentilecont'
class PercentileDisc(AggFunc):
4966class PercentileDisc(AggFunc):
4967    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentiledisc'
class Quantile(AggFunc):
4970class Quantile(AggFunc):
4971    arg_types = {"this": True, "quantile": True}
arg_types = {'this': True, 'quantile': True}
key = 'quantile'
class ApproxQuantile(Quantile):
4974class ApproxQuantile(Quantile):
4975    arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
arg_types = {'this': True, 'quantile': True, 'accuracy': False, 'weight': False}
key = 'approxquantile'
class RangeN(Func):
4978class RangeN(Func):
4979    arg_types = {"this": True, "expressions": True, "each": False}
arg_types = {'this': True, 'expressions': True, 'each': False}
key = 'rangen'
class ReadCSV(Func):
4982class ReadCSV(Func):
4983    _sql_names = ["READ_CSV"]
4984    is_var_len_args = True
4985    arg_types = {"this": True, "expressions": False}
is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
key = 'readcsv'
class Reduce(Func):
4988class Reduce(Func):
4989    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):
4992class RegexpExtract(Func):
4993    arg_types = {
4994        "this": True,
4995        "expression": True,
4996        "position": False,
4997        "occurrence": False,
4998        "parameters": False,
4999        "group": False,
5000    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextract'
class RegexpReplace(Func):
5003class RegexpReplace(Func):
5004    arg_types = {
5005        "this": True,
5006        "expression": True,
5007        "replacement": True,
5008        "position": False,
5009        "occurrence": False,
5010        "parameters": False,
5011        "modifiers": False,
5012    }
arg_types = {'this': True, 'expression': True, 'replacement': True, 'position': False, 'occurrence': False, 'parameters': False, 'modifiers': False}
key = 'regexpreplace'
class RegexpLike(Binary, Func):
5015class RegexpLike(Binary, Func):
5016    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexplike'
class RegexpILike(Binary, Func):
5019class RegexpILike(Binary, Func):
5020    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexpilike'
class RegexpSplit(Func):
5025class RegexpSplit(Func):
5026    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'regexpsplit'
class Repeat(Func):
5029class Repeat(Func):
5030    arg_types = {"this": True, "times": True}
arg_types = {'this': True, 'times': True}
key = 'repeat'
class Round(Func):
5033class Round(Func):
5034    arg_types = {"this": True, "decimals": False}
arg_types = {'this': True, 'decimals': False}
key = 'round'
class RowNumber(Func):
5037class RowNumber(Func):
5038    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'rownumber'
class SafeDivide(Func):
5041class SafeDivide(Func):
5042    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'safedivide'
class SHA(Func):
5045class SHA(Func):
5046    _sql_names = ["SHA", "SHA1"]
key = 'sha'
class SHA2(Func):
5049class SHA2(Func):
5050    _sql_names = ["SHA2"]
5051    arg_types = {"this": True, "length": False}
arg_types = {'this': True, 'length': False}
key = 'sha2'
class SortArray(Func):
5054class SortArray(Func):
5055    arg_types = {"this": True, "asc": False}
arg_types = {'this': True, 'asc': False}
key = 'sortarray'
class Split(Func):
5058class Split(Func):
5059    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'split'
class Substring(Func):
5064class Substring(Func):
5065    arg_types = {"this": True, "start": False, "length": False}
arg_types = {'this': True, 'start': False, 'length': False}
key = 'substring'
class StandardHash(Func):
5068class StandardHash(Func):
5069    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'standardhash'
class StartsWith(Func):
5072class StartsWith(Func):
5073    _sql_names = ["STARTS_WITH", "STARTSWITH"]
5074    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'startswith'
class StrPosition(Func):
5077class StrPosition(Func):
5078    arg_types = {
5079        "this": True,
5080        "substr": True,
5081        "position": False,
5082        "instance": False,
5083    }
arg_types = {'this': True, 'substr': True, 'position': False, 'instance': False}
key = 'strposition'
class StrToDate(Func):
5086class StrToDate(Func):
5087    arg_types = {"this": True, "format": True}
arg_types = {'this': True, 'format': True}
key = 'strtodate'
class StrToTime(Func):
5090class StrToTime(Func):
5091    arg_types = {"this": True, "format": True, "zone": False}
arg_types = {'this': True, 'format': True, 'zone': False}
key = 'strtotime'
class StrToUnix(Func):
5096class StrToUnix(Func):
5097    arg_types = {"this": False, "format": False}
arg_types = {'this': False, 'format': False}
key = 'strtounix'
class StrToMap(Func):
5102class StrToMap(Func):
5103    arg_types = {
5104        "this": True,
5105        "pair_delim": False,
5106        "key_value_delim": False,
5107        "duplicate_resolution_callback": False,
5108    }
arg_types = {'this': True, 'pair_delim': False, 'key_value_delim': False, 'duplicate_resolution_callback': False}
key = 'strtomap'
class NumberToStr(Func):
5111class NumberToStr(Func):
5112    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'numbertostr'
class FromBase(Func):
5115class FromBase(Func):
5116    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'frombase'
class Struct(Func):
5119class Struct(Func):
5120    arg_types = {"expressions": False}
5121    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'struct'
class StructExtract(Func):
5124class StructExtract(Func):
5125    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'structextract'
class Stuff(Func):
5130class Stuff(Func):
5131    _sql_names = ["STUFF", "INSERT"]
5132    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):
5135class Sum(AggFunc):
5136    pass
key = 'sum'
class Sqrt(Func):
5139class Sqrt(Func):
5140    pass
key = 'sqrt'
class Stddev(AggFunc):
5143class Stddev(AggFunc):
5144    pass
key = 'stddev'
class StddevPop(AggFunc):
5147class StddevPop(AggFunc):
5148    pass
key = 'stddevpop'
class StddevSamp(AggFunc):
5151class StddevSamp(AggFunc):
5152    pass
key = 'stddevsamp'
class TimeToStr(Func):
5155class TimeToStr(Func):
5156    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'timetostr'
class TimeToTimeStr(Func):
5159class TimeToTimeStr(Func):
5160    pass
key = 'timetotimestr'
class TimeToUnix(Func):
5163class TimeToUnix(Func):
5164    pass
key = 'timetounix'
class TimeStrToDate(Func):
5167class TimeStrToDate(Func):
5168    pass
key = 'timestrtodate'
class TimeStrToTime(Func):
5171class TimeStrToTime(Func):
5172    pass
key = 'timestrtotime'
class TimeStrToUnix(Func):
5175class TimeStrToUnix(Func):
5176    pass
key = 'timestrtounix'
class Trim(Func):
5179class Trim(Func):
5180    arg_types = {
5181        "this": True,
5182        "expression": False,
5183        "position": False,
5184        "collation": False,
5185    }
arg_types = {'this': True, 'expression': False, 'position': False, 'collation': False}
key = 'trim'
class TsOrDsAdd(Func, TimeUnit):
5188class TsOrDsAdd(Func, TimeUnit):
5189    # return_type is used to correctly cast the arguments of this expression when transpiling it
5190    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
5191
5192    @property
5193    def return_type(self) -> DataType:
5194        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
key = 'tsordsadd'
class TsOrDsDiff(Func, TimeUnit):
5197class TsOrDsDiff(Func, TimeUnit):
5198    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'tsordsdiff'
class TsOrDsToDateStr(Func):
5201class TsOrDsToDateStr(Func):
5202    pass
key = 'tsordstodatestr'
class TsOrDsToDate(Func):
5205class TsOrDsToDate(Func):
5206    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'tsordstodate'
class TsOrDiToDi(Func):
5209class TsOrDiToDi(Func):
5210    pass
key = 'tsorditodi'
class Unhex(Func):
5213class Unhex(Func):
5214    pass
key = 'unhex'
class UnixToStr(Func):
5217class UnixToStr(Func):
5218    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'unixtostr'
class UnixToTime(Func):
5223class UnixToTime(Func):
5224    arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False}
5225
5226    SECONDS = Literal.string("seconds")
5227    MILLIS = Literal.string("millis")
5228    MICROS = Literal.string("micros")
5229    NANOS = Literal.string("nanos")
arg_types = {'this': True, 'scale': False, 'zone': False, 'hours': False, 'minutes': False}
SECONDS = (LITERAL this: seconds, is_string: True)
MILLIS = (LITERAL this: millis, is_string: True)
MICROS = (LITERAL this: micros, is_string: True)
NANOS = (LITERAL this: nanos, is_string: True)
key = 'unixtotime'
class UnixToTimeStr(Func):
5232class UnixToTimeStr(Func):
5233    pass
key = 'unixtotimestr'
class TimestampFromParts(Func):
5236class TimestampFromParts(Func):
5237    """Constructs a timestamp given its constituent parts."""
5238
5239    arg_types = {
5240        "year": True,
5241        "month": True,
5242        "day": True,
5243        "hour": True,
5244        "min": True,
5245        "sec": True,
5246    }

Constructs a timestamp given its constituent parts.

arg_types = {'year': True, 'month': True, 'day': True, 'hour': True, 'min': True, 'sec': True}
key = 'timestampfromparts'
class Upper(Func):
5249class Upper(Func):
5250    _sql_names = ["UPPER", "UCASE"]
key = 'upper'
class Variance(AggFunc):
5253class Variance(AggFunc):
5254    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
key = 'variance'
class VariancePop(AggFunc):
5257class VariancePop(AggFunc):
5258    _sql_names = ["VARIANCE_POP", "VAR_POP"]
key = 'variancepop'
class Week(Func):
5261class Week(Func):
5262    arg_types = {"this": True, "mode": False}
arg_types = {'this': True, 'mode': False}
key = 'week'
class XMLTable(Func):
5265class XMLTable(Func):
5266    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):
5269class Year(Func):
5270    pass
key = 'year'
class Use(Expression):
5273class Use(Expression):
5274    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'use'
class Merge(Expression):
5277class Merge(Expression):
5278    arg_types = {"this": True, "using": True, "on": True, "expressions": True, "with": False}
arg_types = {'this': True, 'using': True, 'on': True, 'expressions': True, 'with': False}
key = 'merge'
class When(Func):
5281class When(Func):
5282    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):
5287class NextValueFor(Func):
5288    arg_types = {"this": True, "order": False}
arg_types = {'this': True, 'order': False}
key = 'nextvaluefor'
ALL_FUNCTIONS = [<class 'Abs'>, <class 'AnyValue'>, <class 'ApproxDistinct'>, <class 'ApproxQuantile'>, <class 'ApproxTopK'>, <class 'ArgMax'>, <class 'ArgMin'>, <class 'Array'>, <class 'ArrayAgg'>, <class 'ArrayAll'>, <class 'ArrayAny'>, <class 'ArrayConcat'>, <class 'ArrayContains'>, <class 'ArrayFilter'>, <class 'ArrayJoin'>, <class 'ArraySize'>, <class 'ArraySort'>, <class 'ArraySum'>, <class 'ArrayUnionAgg'>, <class 'ArrayUniqueAgg'>, <class 'Avg'>, <class 'Case'>, <class 'Cast'>, <class 'CastToStrType'>, <class 'Ceil'>, <class 'Chr'>, <class 'Coalesce'>, <class 'Collate'>, <class 'Concat'>, <class 'ConcatWs'>, <class 'Count'>, <class 'CountIf'>, <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 'DatetimeAdd'>, <class 'DatetimeDiff'>, <class 'DatetimeSub'>, <class 'DatetimeTrunc'>, <class 'Day'>, <class 'DayOfMonth'>, <class 'DayOfWeek'>, <class 'DayOfYear'>, <class 'Decode'>, <class 'DiToDate'>, <class 'Encode'>, <class 'Exp'>, <class 'Explode'>, <class 'ExplodeOuter'>, <class 'Extract'>, <class 'First'>, <class 'Flatten'>, <class 'Floor'>, <class 'FromBase'>, <class 'FromBase64'>, <class 'GenerateSeries'>, <class 'Greatest'>, <class 'GroupConcat'>, <class 'Hex'>, <class 'Hll'>, <class 'If'>, <class 'Initcap'>, <class 'IsInf'>, <class 'IsNan'>, <class 'JSONArray'>, <class 'JSONArrayAgg'>, <class 'JSONArrayContains'>, <class 'JSONBExtract'>, <class 'JSONBExtractScalar'>, <class 'JSONExtract'>, <class 'JSONExtractScalar'>, <class 'JSONFormat'>, <class 'JSONObject'>, <class 'JSONTable'>, <class 'Last'>, <class 'LastDateOfMonth'>, <class 'Least'>, <class 'Left'>, <class 'Length'>, <class 'Levenshtein'>, <class 'Ln'>, <class 'Log'>, <class 'Log10'>, <class 'Log2'>, <class 'LogicalAnd'>, <class 'LogicalOr'>, <class 'Lower'>, <class 'MD5'>, <class 'MD5Digest'>, <class 'Map'>, <class 'MapFromEntries'>, <class 'MatchAgainst'>, <class 'Max'>, <class 'Min'>, <class 'Month'>, <class 'MonthsBetween'>, <class 'NextValueFor'>, <class 'Nullif'>, <class 'NumberToStr'>, <class 'Nvl2'>, <class 'OpenJSON'>, <class 'ParameterizedAgg'>, <class 'ParseJSON'>, <class 'PercentileCont'>, <class 'PercentileDisc'>, <class 'Posexplode'>, <class 'PosexplodeOuter'>, <class 'Pow'>, <class 'Predict'>, <class 'Quantile'>, <class 'RangeN'>, <class 'ReadCSV'>, <class 'Reduce'>, <class 'RegexpExtract'>, <class 'RegexpILike'>, <class 'RegexpLike'>, <class 'RegexpReplace'>, <class 'RegexpSplit'>, <class 'Repeat'>, <class 'Right'>, <class 'Round'>, <class 'RowNumber'>, <class 'SHA'>, <class 'SHA2'>, <class 'SafeDivide'>, <class 'SortArray'>, <class 'Split'>, <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 'Struct'>, <class 'StructExtract'>, <class 'Stuff'>, <class 'Substring'>, <class 'Sum'>, <class 'TimeAdd'>, <class 'TimeDiff'>, <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 'ToBase64'>, <class 'ToChar'>, <class 'ToDays'>, <class 'Transform'>, <class 'Trim'>, <class 'TryCast'>, <class 'TsOrDiToDi'>, <class 'TsOrDsAdd'>, <class 'TsOrDsDiff'>, <class 'TsOrDsToDate'>, <class 'TsOrDsToDateStr'>, <class 'Unhex'>, <class 'UnixToStr'>, <class 'UnixToTime'>, <class 'UnixToTimeStr'>, <class 'Upper'>, <class 'VarMap'>, <class 'Variance'>, <class 'VariancePop'>, <class 'Week'>, <class 'WeekOfYear'>, <class 'When'>, <class 'XMLTable'>, <class 'Xor'>, <class 'Year'>]
FUNCTION_BY_NAME = {'ABS': <class 'Abs'>, 'ANY_VALUE': <class 'AnyValue'>, '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_CONTAINS': <class 'ArrayContains'>, 'FILTER': <class 'ArrayFilter'>, 'ARRAY_FILTER': <class 'ArrayFilter'>, 'ARRAY_JOIN': <class 'ArrayJoin'>, 'ARRAY_SIZE': <class 'ArraySize'>, 'ARRAY_SORT': <class 'ArraySort'>, 'ARRAY_SUM': <class 'ArraySum'>, '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'>, 'CEIL': <class 'Ceil'>, 'CEILING': <class 'Ceil'>, 'CHR': <class 'Chr'>, 'CHAR': <class 'Chr'>, 'COALESCE': <class 'Coalesce'>, 'IFNULL': <class 'Coalesce'>, 'NVL': <class 'Coalesce'>, 'COLLATE': <class 'Collate'>, 'CONCAT': <class 'Concat'>, 'CONCAT_WS': <class 'ConcatWs'>, 'COUNT': <class 'Count'>, 'COUNT_IF': <class 'CountIf'>, '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'>, '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_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'>, 'DAY_OF_YEAR': <class 'DayOfYear'>, 'DAYOFYEAR': <class 'DayOfYear'>, 'DECODE': <class 'Decode'>, 'DI_TO_DATE': <class 'DiToDate'>, 'ENCODE': <class 'Encode'>, 'EXP': <class 'Exp'>, 'EXPLODE': <class 'Explode'>, 'EXPLODE_OUTER': <class 'ExplodeOuter'>, 'EXTRACT': <class 'Extract'>, 'FIRST': <class 'First'>, 'FLATTEN': <class 'Flatten'>, 'FLOOR': <class 'Floor'>, 'FROM_BASE': <class 'FromBase'>, 'FROM_BASE64': <class 'FromBase64'>, 'GENERATE_SERIES': <class 'GenerateSeries'>, 'GREATEST': <class 'Greatest'>, 'GROUP_CONCAT': <class 'GroupConcat'>, 'HEX': <class 'Hex'>, 'HLL': <class 'Hll'>, 'IF': <class 'If'>, 'INITCAP': <class 'Initcap'>, '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_EXTRACT': <class 'JSONBExtract'>, 'JSONB_EXTRACT_SCALAR': <class 'JSONBExtractScalar'>, 'JSON_EXTRACT': <class 'JSONExtract'>, 'JSON_EXTRACT_SCALAR': <class 'JSONExtractScalar'>, 'JSON_FORMAT': <class 'JSONFormat'>, 'J_S_O_N_OBJECT': <class 'JSONObject'>, 'J_S_O_N_TABLE': <class 'JSONTable'>, 'LAST': <class 'Last'>, 'LAST_DATE_OF_MONTH': <class 'LastDateOfMonth'>, 'LEAST': <class 'Least'>, 'LEFT': <class 'Left'>, 'LENGTH': <class 'Length'>, 'LEN': <class 'Length'>, 'LEVENSHTEIN': <class 'Levenshtein'>, 'LN': <class 'Ln'>, 'LOG': <class 'Log'>, 'LOG10': <class 'Log10'>, 'LOG2': <class 'Log2'>, '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'>, 'MD5': <class 'MD5'>, 'MD5_DIGEST': <class 'MD5Digest'>, 'MAP': <class 'Map'>, 'MAP_FROM_ENTRIES': <class 'MapFromEntries'>, 'MATCH_AGAINST': <class 'MatchAgainst'>, 'MAX': <class 'Max'>, 'MIN': <class 'Min'>, 'MONTH': <class 'Month'>, 'MONTHS_BETWEEN': <class 'MonthsBetween'>, 'NEXT_VALUE_FOR': <class 'NextValueFor'>, 'NULLIF': <class 'Nullif'>, 'NUMBER_TO_STR': <class 'NumberToStr'>, 'NVL2': <class 'Nvl2'>, 'OPEN_J_S_O_N': <class 'OpenJSON'>, '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'>, 'RANGE_N': <class 'RangeN'>, 'READ_CSV': <class 'ReadCSV'>, 'REDUCE': <class 'Reduce'>, 'REGEXP_EXTRACT': <class 'RegexpExtract'>, 'REGEXP_I_LIKE': <class 'RegexpILike'>, 'REGEXP_LIKE': <class 'RegexpLike'>, 'REGEXP_REPLACE': <class 'RegexpReplace'>, 'REGEXP_SPLIT': <class 'RegexpSplit'>, 'REPEAT': <class 'Repeat'>, 'RIGHT': <class 'Right'>, 'ROUND': <class 'Round'>, 'ROW_NUMBER': <class 'RowNumber'>, 'SHA': <class 'SHA'>, 'SHA1': <class 'SHA'>, 'SHA2': <class 'SHA2'>, 'SAFE_DIVIDE': <class 'SafeDivide'>, 'SORT_ARRAY': <class 'SortArray'>, 'SPLIT': <class 'Split'>, 'SQRT': <class 'Sqrt'>, 'STANDARD_HASH': <class 'StandardHash'>, 'STAR_MAP': <class 'StarMap'>, 'STARTS_WITH': <class 'StartsWith'>, 'STARTSWITH': <class 'StartsWith'>, 'STDDEV': <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'>, 'STRUCT': <class 'Struct'>, 'STRUCT_EXTRACT': <class 'StructExtract'>, 'STUFF': <class 'Stuff'>, 'INSERT': <class 'Stuff'>, 'SUBSTRING': <class 'Substring'>, 'SUM': <class 'Sum'>, 'TIME_ADD': <class 'TimeAdd'>, 'TIME_DIFF': <class 'TimeDiff'>, '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'>, 'TIMESTAMP_DIFF': <class 'TimestampDiff'>, 'TIMESTAMP_FROM_PARTS': <class 'TimestampFromParts'>, 'TIMESTAMP_SUB': <class 'TimestampSub'>, 'TIMESTAMP_TRUNC': <class 'TimestampTrunc'>, 'TO_BASE64': <class 'ToBase64'>, 'TO_CHAR': <class 'ToChar'>, 'TO_DAYS': <class 'ToDays'>, 'TRANSFORM': <class 'Transform'>, 'TRIM': <class 'Trim'>, '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'>, 'UNHEX': <class 'Unhex'>, 'UNIX_TO_STR': <class 'UnixToStr'>, 'UNIX_TO_TIME': <class 'UnixToTime'>, 'UNIX_TO_TIME_STR': <class 'UnixToTimeStr'>, 'UPPER': <class 'Upper'>, 'UCASE': <class 'Upper'>, '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'>}
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:
5326def maybe_parse(
5327    sql_or_expression: ExpOrStr,
5328    *,
5329    into: t.Optional[IntoType] = None,
5330    dialect: DialectType = None,
5331    prefix: t.Optional[str] = None,
5332    copy: bool = False,
5333    **opts,
5334) -> Expression:
5335    """Gracefully handle a possible string or expression.
5336
5337    Example:
5338        >>> maybe_parse("1")
5339        (LITERAL this: 1, is_string: False)
5340        >>> maybe_parse(to_identifier("x"))
5341        (IDENTIFIER this: x, quoted: False)
5342
5343    Args:
5344        sql_or_expression: the SQL code string or an expression
5345        into: the SQLGlot Expression to parse into
5346        dialect: the dialect used to parse the input expressions (in the case that an
5347            input expression is a SQL string).
5348        prefix: a string to prefix the sql with before it gets parsed
5349            (automatically includes a space)
5350        copy: whether or not to copy the expression.
5351        **opts: other options to use to parse the input expressions (again, in the case
5352            that an input expression is a SQL string).
5353
5354    Returns:
5355        Expression: the parsed or given expression.
5356    """
5357    if isinstance(sql_or_expression, Expression):
5358        if copy:
5359            return sql_or_expression.copy()
5360        return sql_or_expression
5361
5362    if sql_or_expression is None:
5363        raise ParseError(f"SQL cannot be None")
5364
5365    import sqlglot
5366
5367    sql = str(sql_or_expression)
5368    if prefix:
5369        sql = f"{prefix} {sql}"
5370
5371    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 or not 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):
5384def maybe_copy(instance, copy=True):
5385    return instance.copy() if copy and instance else instance
def union( left: Union[str, Expression], right: 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:
5566def union(
5567    left: ExpOrStr,
5568    right: ExpOrStr,
5569    distinct: bool = True,
5570    dialect: DialectType = None,
5571    copy: bool = True,
5572    **opts,
5573) -> Union:
5574    """
5575    Initializes a syntax tree from one UNION expression.
5576
5577    Example:
5578        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
5579        'SELECT * FROM foo UNION SELECT * FROM bla'
5580
5581    Args:
5582        left: the SQL code string corresponding to the left-hand side.
5583            If an `Expression` instance is passed, it will be used as-is.
5584        right: the SQL code string corresponding to the right-hand side.
5585            If an `Expression` instance is passed, it will be used as-is.
5586        distinct: set the DISTINCT flag if and only if this is true.
5587        dialect: the dialect used to parse the input expression.
5588        copy: whether or not to copy the expression.
5589        opts: other options to use to parse the input expressions.
5590
5591    Returns:
5592        The new Union instance.
5593    """
5594    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5595    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5596
5597    return Union(this=left, expression=right, distinct=distinct)

Initializes a syntax tree from one UNION expression.

Example:
>>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
'SELECT * FROM foo UNION SELECT * FROM bla'
Arguments:
  • left: the SQL code string corresponding to the left-hand side. If an Expression instance is passed, it will be used as-is.
  • right: the SQL code string corresponding to the right-hand side. If an Expression instance is passed, it 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 or not to copy the expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Union instance.

def intersect( left: Union[str, Expression], right: 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:
5600def intersect(
5601    left: ExpOrStr,
5602    right: ExpOrStr,
5603    distinct: bool = True,
5604    dialect: DialectType = None,
5605    copy: bool = True,
5606    **opts,
5607) -> Intersect:
5608    """
5609    Initializes a syntax tree from one INTERSECT expression.
5610
5611    Example:
5612        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
5613        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
5614
5615    Args:
5616        left: the SQL code string corresponding to the left-hand side.
5617            If an `Expression` instance is passed, it will be used as-is.
5618        right: the SQL code string corresponding to the right-hand side.
5619            If an `Expression` instance is passed, it will be used as-is.
5620        distinct: set the DISTINCT flag if and only if this is true.
5621        dialect: the dialect used to parse the input expression.
5622        copy: whether or not to copy the expression.
5623        opts: other options to use to parse the input expressions.
5624
5625    Returns:
5626        The new Intersect instance.
5627    """
5628    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5629    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5630
5631    return Intersect(this=left, expression=right, distinct=distinct)

Initializes a syntax tree from one INTERSECT expression.

Example:
>>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
'SELECT * FROM foo INTERSECT SELECT * FROM bla'
Arguments:
  • left: the SQL code string corresponding to the left-hand side. If an Expression instance is passed, it will be used as-is.
  • right: the SQL code string corresponding to the right-hand side. If an Expression instance is passed, it 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 or not to copy the expression.
  • opts: other options to use to parse the input expressions.
Returns:

The new Intersect instance.

def except_( left: Union[str, Expression], right: 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:
5634def except_(
5635    left: ExpOrStr,
5636    right: ExpOrStr,
5637    distinct: bool = True,
5638    dialect: DialectType = None,
5639    copy: bool = True,
5640    **opts,
5641) -> Except:
5642    """
5643    Initializes a syntax tree from one EXCEPT expression.
5644
5645    Example:
5646        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
5647        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
5648
5649    Args:
5650        left: the SQL code string corresponding to the left-hand side.
5651            If an `Expression` instance is passed, it will be used as-is.
5652        right: the SQL code string corresponding to the right-hand side.
5653            If an `Expression` instance is passed, it will be used as-is.
5654        distinct: set the DISTINCT flag if and only if this is true.
5655        dialect: the dialect used to parse the input expression.
5656        copy: whether or not to copy the expression.
5657        opts: other options to use to parse the input expressions.
5658
5659    Returns:
5660        The new Except instance.
5661    """
5662    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5663    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5664
5665    return Except(this=left, expression=right, distinct=distinct)

Initializes a syntax tree from one EXCEPT expression.

Example:
>>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
'SELECT * FROM foo EXCEPT SELECT * FROM bla'
Arguments:
  • left: the SQL code string corresponding to the left-hand side. If an Expression instance is passed, it will be used as-is.
  • right: the SQL code string corresponding to the right-hand side. If an Expression instance is passed, it 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 or not 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:
5668def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5669    """
5670    Initializes a syntax tree from one or multiple SELECT expressions.
5671
5672    Example:
5673        >>> select("col1", "col2").from_("tbl").sql()
5674        'SELECT col1, col2 FROM tbl'
5675
5676    Args:
5677        *expressions: the SQL code string to parse as the expressions of a
5678            SELECT statement. If an Expression instance is passed, this is used as-is.
5679        dialect: the dialect used to parse the input expressions (in the case that an
5680            input expression is a SQL string).
5681        **opts: other options to use to parse the input expressions (again, in the case
5682            that an input expression is a SQL string).
5683
5684    Returns:
5685        Select: the syntax tree for the SELECT statement.
5686    """
5687    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:
5690def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5691    """
5692    Initializes a syntax tree from a FROM expression.
5693
5694    Example:
5695        >>> from_("tbl").select("col1", "col2").sql()
5696        'SELECT col1, col2 FROM tbl'
5697
5698    Args:
5699        *expression: the SQL code string to parse as the FROM expressions of a
5700            SELECT statement. If an Expression instance is passed, this is used as-is.
5701        dialect: the dialect used to parse the input expression (in the case that the
5702            input expression is a SQL string).
5703        **opts: other options to use to parse the input expressions (again, in the case
5704            that the input expression is a SQL string).
5705
5706    Returns:
5707        Select: the syntax tree for the SELECT statement.
5708    """
5709    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: dict, where: Union[str, Expression, NoneType] = None, from_: Union[str, Expression, NoneType] = None, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, **opts) -> Update:
5712def update(
5713    table: str | Table,
5714    properties: dict,
5715    where: t.Optional[ExpOrStr] = None,
5716    from_: t.Optional[ExpOrStr] = None,
5717    dialect: DialectType = None,
5718    **opts,
5719) -> Update:
5720    """
5721    Creates an update statement.
5722
5723    Example:
5724        >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql()
5725        "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1"
5726
5727    Args:
5728        *properties: dictionary of properties to set which are
5729            auto converted to sql objects eg None -> NULL
5730        where: sql conditional parsed into a WHERE statement
5731        from_: sql statement parsed into a FROM statement
5732        dialect: the dialect used to parse the input expressions.
5733        **opts: other options to use to parse the input expressions.
5734
5735    Returns:
5736        Update: the syntax tree for the UPDATE statement.
5737    """
5738    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
5739    update_expr.set(
5740        "expressions",
5741        [
5742            EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
5743            for k, v in properties.items()
5744        ],
5745    )
5746    if from_:
5747        update_expr.set(
5748            "from",
5749            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
5750        )
5751    if isinstance(where, Condition):
5752        where = Where(this=where)
5753    if where:
5754        update_expr.set(
5755            "where",
5756            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
5757        )
5758    return update_expr

Creates an update statement.

Example:
>>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql()
"UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1"
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
  • 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:
5761def delete(
5762    table: ExpOrStr,
5763    where: t.Optional[ExpOrStr] = None,
5764    returning: t.Optional[ExpOrStr] = None,
5765    dialect: DialectType = None,
5766    **opts,
5767) -> Delete:
5768    """
5769    Builds a delete statement.
5770
5771    Example:
5772        >>> delete("my_table", where="id > 1").sql()
5773        'DELETE FROM my_table WHERE id > 1'
5774
5775    Args:
5776        where: sql conditional parsed into a WHERE statement
5777        returning: sql conditional parsed into a RETURNING statement
5778        dialect: the dialect used to parse the input expressions.
5779        **opts: other options to use to parse the input expressions.
5780
5781    Returns:
5782        Delete: the syntax tree for the DELETE statement.
5783    """
5784    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
5785    if where:
5786        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
5787    if returning:
5788        delete_expr = t.cast(
5789            Delete, delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
5790        )
5791    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[Union[str, Expression]]] = 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:
5794def insert(
5795    expression: ExpOrStr,
5796    into: ExpOrStr,
5797    columns: t.Optional[t.Sequence[ExpOrStr]] = None,
5798    overwrite: t.Optional[bool] = None,
5799    returning: t.Optional[ExpOrStr] = None,
5800    dialect: DialectType = None,
5801    copy: bool = True,
5802    **opts,
5803) -> Insert:
5804    """
5805    Builds an INSERT statement.
5806
5807    Example:
5808        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
5809        'INSERT INTO tbl VALUES (1, 2, 3)'
5810
5811    Args:
5812        expression: the sql string or expression of the INSERT statement
5813        into: the tbl to insert data to.
5814        columns: optionally the table's column names.
5815        overwrite: whether to INSERT OVERWRITE or not.
5816        returning: sql conditional parsed into a RETURNING statement
5817        dialect: the dialect used to parse the input expressions.
5818        copy: whether or not to copy the expression.
5819        **opts: other options to use to parse the input expressions.
5820
5821    Returns:
5822        Insert: the syntax tree for the INSERT statement.
5823    """
5824    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
5825    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
5826
5827    if columns:
5828        this = _apply_list_builder(
5829            *columns,
5830            instance=Schema(this=this),
5831            arg="expressions",
5832            into=Identifier,
5833            copy=False,
5834            dialect=dialect,
5835            **opts,
5836        )
5837
5838    insert = Insert(this=this, expression=expr, overwrite=overwrite)
5839
5840    if returning:
5841        insert = t.cast(Insert, insert.returning(returning, dialect=dialect, copy=False, **opts))
5842
5843    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 or not to copy the expression.
  • **opts: other options to use to parse the input expressions.
Returns:

Insert: the syntax tree for the INSERT 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:
5846def condition(
5847    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
5848) -> Condition:
5849    """
5850    Initialize a logical condition expression.
5851
5852    Example:
5853        >>> condition("x=1").sql()
5854        'x = 1'
5855
5856        This is helpful for composing larger logical syntax trees:
5857        >>> where = condition("x=1")
5858        >>> where = where.and_("y=1")
5859        >>> Select().from_("tbl").select("*").where(where).sql()
5860        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
5861
5862    Args:
5863        *expression: the SQL code string to parse.
5864            If an Expression instance is passed, this is used as-is.
5865        dialect: the dialect used to parse the input expression (in the case that the
5866            input expression is a SQL string).
5867        copy: Whether or not to copy `expression` (only applies to expressions).
5868        **opts: other options to use to parse the input expressions (again, in the case
5869            that the input expression is a SQL string).
5870
5871    Returns:
5872        The new Condition instance
5873    """
5874    return maybe_parse(
5875        expression,
5876        into=Condition,
5877        dialect=dialect,
5878        copy=copy,
5879        **opts,
5880    )

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 or not to copy expression (only applies to expressions).
  • **opts: other options to use to parse the input expressions (again, in the case that the input expression is a SQL string).
Returns:

The new Condition instance

def and_( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
5883def and_(
5884    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5885) -> Condition:
5886    """
5887    Combine multiple conditions with an AND logical operator.
5888
5889    Example:
5890        >>> and_("x=1", and_("y=1", "z=1")).sql()
5891        'x = 1 AND (y = 1 AND z = 1)'
5892
5893    Args:
5894        *expressions: the SQL code strings to parse.
5895            If an Expression instance is passed, this is used as-is.
5896        dialect: the dialect used to parse the input expression.
5897        copy: whether or not to copy `expressions` (only applies to Expressions).
5898        **opts: other options to use to parse the input expressions.
5899
5900    Returns:
5901        And: the new condition
5902    """
5903    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, **opts))

Combine multiple conditions with an AND logical operator.

Example:
>>> and_("x=1", and_("y=1", "z=1")).sql()
'x = 1 AND (y = 1 AND z = 1)'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether or not to copy expressions (only applies to Expressions).
  • **opts: other options to use to parse the input expressions.
Returns:

And: the new condition

def or_( *expressions: Union[str, Expression, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **opts) -> Condition:
5906def or_(
5907    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5908) -> Condition:
5909    """
5910    Combine multiple conditions with an OR logical operator.
5911
5912    Example:
5913        >>> or_("x=1", or_("y=1", "z=1")).sql()
5914        'x = 1 OR (y = 1 OR z = 1)'
5915
5916    Args:
5917        *expressions: the SQL code strings to parse.
5918            If an Expression instance is passed, this is used as-is.
5919        dialect: the dialect used to parse the input expression.
5920        copy: whether or not to copy `expressions` (only applies to Expressions).
5921        **opts: other options to use to parse the input expressions.
5922
5923    Returns:
5924        Or: the new condition
5925    """
5926    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, **opts))

Combine multiple conditions with an OR logical operator.

Example:
>>> or_("x=1", or_("y=1", "z=1")).sql()
'x = 1 OR (y = 1 OR z = 1)'
Arguments:
  • *expressions: the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
  • dialect: the dialect used to parse the input expression.
  • copy: whether or not to copy expressions (only applies to Expressions).
  • **opts: other options to use to parse the input expressions.
Returns:

Or: 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:
5929def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
5930    """
5931    Wrap a condition with a NOT operator.
5932
5933    Example:
5934        >>> not_("this_suit='black'").sql()
5935        "NOT this_suit = 'black'"
5936
5937    Args:
5938        expression: the SQL code string to parse.
5939            If an Expression instance is passed, this is used as-is.
5940        dialect: the dialect used to parse the input expression.
5941        copy: whether to copy the expression or not.
5942        **opts: other options to use to parse the input expressions.
5943
5944    Returns:
5945        The new condition.
5946    """
5947    this = condition(
5948        expression,
5949        dialect=dialect,
5950        copy=copy,
5951        **opts,
5952    )
5953    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:
5956def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
5957    """
5958    Wrap an expression in parentheses.
5959
5960    Example:
5961        >>> paren("5 + 3").sql()
5962        '(5 + 3)'
5963
5964    Args:
5965        expression: the SQL code string to parse.
5966            If an Expression instance is passed, this is used as-is.
5967        copy: whether to copy the expression or not.
5968
5969    Returns:
5970        The wrapped expression.
5971    """
5972    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 = re.compile('^[_a-zA-Z][\\w]*$')
def to_identifier(name, quoted=None, copy=True):
5990def to_identifier(name, quoted=None, copy=True):
5991    """Builds an identifier.
5992
5993    Args:
5994        name: The name to turn into an identifier.
5995        quoted: Whether or not force quote the identifier.
5996        copy: Whether or not to copy name if it's an Identifier.
5997
5998    Returns:
5999        The identifier ast node.
6000    """
6001
6002    if name is None:
6003        return None
6004
6005    if isinstance(name, Identifier):
6006        identifier = maybe_copy(name, copy)
6007    elif isinstance(name, str):
6008        identifier = Identifier(
6009            this=name,
6010            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
6011        )
6012    else:
6013        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
6014    return identifier

Builds an identifier.

Arguments:
  • name: The name to turn into an identifier.
  • quoted: Whether or not force quote the identifier.
  • copy: Whether or not 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:
6017def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
6018    """
6019    Parses a given string into an identifier.
6020
6021    Args:
6022        name: The name to parse into an identifier.
6023        dialect: The dialect to parse against.
6024
6025    Returns:
6026        The identifier ast node.
6027    """
6028    try:
6029        expression = maybe_parse(name, dialect=dialect, into=Identifier)
6030    except ParseError:
6031        expression = to_identifier(name)
6032
6033    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:
6039def to_interval(interval: str | Literal) -> Interval:
6040    """Builds an interval expression from a string like '1 day' or '5 months'."""
6041    if isinstance(interval, Literal):
6042        if not interval.is_string:
6043            raise ValueError("Invalid interval string.")
6044
6045        interval = interval.this
6046
6047    interval_parts = INTERVAL_STRING_RE.match(interval)  # type: ignore
6048
6049    if not interval_parts:
6050        raise ValueError("Invalid interval string.")
6051
6052    return Interval(
6053        this=Literal.string(interval_parts.group(1)),
6054        unit=Var(this=interval_parts.group(2)),
6055    )

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

def to_table( sql_path: Union[str, Table, NoneType], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True, **kwargs) -> Optional[Table]:
6068def to_table(
6069    sql_path: t.Optional[str | Table], dialect: DialectType = None, copy: bool = True, **kwargs
6070) -> t.Optional[Table]:
6071    """
6072    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
6073    If a table is passed in then that table is returned.
6074
6075    Args:
6076        sql_path: a `[catalog].[schema].[table]` string.
6077        dialect: the source dialect according to which the table name will be parsed.
6078        copy: Whether or not to copy a table if it is passed in.
6079        kwargs: the kwargs to instantiate the resulting `Table` expression with.
6080
6081    Returns:
6082        A table expression.
6083    """
6084    if sql_path is None or isinstance(sql_path, Table):
6085        return maybe_copy(sql_path, copy=copy)
6086    if not isinstance(sql_path, str):
6087        raise ValueError(f"Invalid type provided for a table: {type(sql_path)}")
6088
6089    table = maybe_parse(sql_path, into=Table, dialect=dialect)
6090    if table:
6091        for k, v in kwargs.items():
6092            table.set(k, v)
6093
6094    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 or not 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, **kwargs) -> Column:
6097def to_column(sql_path: str | Column, **kwargs) -> Column:
6098    """
6099    Create a column from a `[table].[column]` sql path. Schema is optional.
6100
6101    If a column is passed in then that column is returned.
6102
6103    Args:
6104        sql_path: `[table].[column]` string
6105    Returns:
6106        Table: A column expression
6107    """
6108    if sql_path is None or isinstance(sql_path, Column):
6109        return sql_path
6110    if not isinstance(sql_path, str):
6111        raise ValueError(f"Invalid type provided for column: {type(sql_path)}")
6112    return column(*reversed(sql_path.split(".")), **kwargs)  # type: ignore

Create a column from a [table].[column] sql path. Schema is optional.

If a column is passed in then that column is returned.

Arguments:
  • sql_path: [table].[column] string
Returns:

Table: A column expression

def alias_( expression: Union[str, Expression], alias: str | Identifier, 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):
6115def alias_(
6116    expression: ExpOrStr,
6117    alias: str | Identifier,
6118    table: bool | t.Sequence[str | Identifier] = False,
6119    quoted: t.Optional[bool] = None,
6120    dialect: DialectType = None,
6121    copy: bool = True,
6122    **opts,
6123):
6124    """Create an Alias expression.
6125
6126    Example:
6127        >>> alias_('foo', 'bar').sql()
6128        'foo AS bar'
6129
6130        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
6131        '(SELECT 1, 2) AS bar(a, b)'
6132
6133    Args:
6134        expression: the SQL code strings to parse.
6135            If an Expression instance is passed, this is used as-is.
6136        alias: the alias name to use. If the name has
6137            special characters it is quoted.
6138        table: Whether or not to create a table alias, can also be a list of columns.
6139        quoted: whether or not to quote the alias
6140        dialect: the dialect used to parse the input expression.
6141        copy: Whether or not to copy the expression.
6142        **opts: other options to use to parse the input expressions.
6143
6144    Returns:
6145        Alias: the aliased expression
6146    """
6147    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
6148    alias = to_identifier(alias, quoted=quoted)
6149
6150    if table:
6151        table_alias = TableAlias(this=alias)
6152        exp.set("alias", table_alias)
6153
6154        if not isinstance(table, bool):
6155            for column in table:
6156                table_alias.append("columns", to_identifier(column, quoted=quoted))
6157
6158        return exp
6159
6160    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
6161    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
6162    # for the complete Window expression.
6163    #
6164    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
6165
6166    if "alias" in exp.arg_types and not isinstance(exp, Window):
6167        exp.set("alias", alias)
6168        return exp
6169    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 or not to create a table alias, can also be a list of columns.
  • quoted: whether or not to quote the alias
  • dialect: the dialect used to parse the input expression.
  • copy: Whether or not 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:
6172def subquery(
6173    expression: ExpOrStr,
6174    alias: t.Optional[Identifier | str] = None,
6175    dialect: DialectType = None,
6176    **opts,
6177) -> Select:
6178    """
6179    Build a subquery expression.
6180
6181    Example:
6182        >>> subquery('select x from tbl', 'bar').select('x').sql()
6183        'SELECT x FROM (SELECT x FROM tbl) AS bar'
6184
6185    Args:
6186        expression: the SQL code strings to parse.
6187            If an Expression instance is passed, this is used as-is.
6188        alias: the alias name to use.
6189        dialect: the dialect used to parse the input expression.
6190        **opts: other options to use to parse the input expressions.
6191
6192    Returns:
6193        A new Select instance with the subquery expression included.
6194    """
6195
6196    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias)
6197    return Select().from_(expression, dialect=dialect, **opts)

Build a subquery expression.

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: str | Identifier, table: Union[Identifier, str, NoneType] = None, db: Union[Identifier, str, NoneType] = None, catalog: Union[Identifier, str, NoneType] = None, quoted: Optional[bool] = None) -> Column:
6200def column(
6201    col: str | Identifier,
6202    table: t.Optional[str | Identifier] = None,
6203    db: t.Optional[str | Identifier] = None,
6204    catalog: t.Optional[str | Identifier] = None,
6205    quoted: t.Optional[bool] = None,
6206) -> Column:
6207    """
6208    Build a Column.
6209
6210    Args:
6211        col: Column name.
6212        table: Table name.
6213        db: Database name.
6214        catalog: Catalog name.
6215        quoted: Whether to force quotes on the column's identifiers.
6216
6217    Returns:
6218        The new Column instance.
6219    """
6220    return Column(
6221        this=to_identifier(col, quoted=quoted),
6222        table=to_identifier(table, quoted=quoted),
6223        db=to_identifier(db, quoted=quoted),
6224        catalog=to_identifier(catalog, quoted=quoted),
6225    )

Build a Column.

Arguments:
  • col: Column name.
  • table: Table name.
  • db: Database name.
  • catalog: Catalog name.
  • quoted: Whether to force quotes on the column's identifiers.
Returns:

The new Column instance.

def cast( expression: Union[str, Expression], to: Union[str, DataType, DataType.Type], **opts) -> Cast:
6228def cast(expression: ExpOrStr, to: DATA_TYPE, **opts) -> Cast:
6229    """Cast an expression to a data type.
6230
6231    Example:
6232        >>> cast('x + 1', 'int').sql()
6233        'CAST(x + 1 AS INT)'
6234
6235    Args:
6236        expression: The expression to cast.
6237        to: The datatype to cast to.
6238
6239    Returns:
6240        The new Cast instance.
6241    """
6242    expression = maybe_parse(expression, **opts)
6243    data_type = DataType.build(to, **opts)
6244    expression = Cast(this=expression, to=data_type)
6245    expression.type = data_type
6246    return expression

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.
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:
6249def table_(
6250    table: Identifier | str,
6251    db: t.Optional[Identifier | str] = None,
6252    catalog: t.Optional[Identifier | str] = None,
6253    quoted: t.Optional[bool] = None,
6254    alias: t.Optional[Identifier | str] = None,
6255) -> Table:
6256    """Build a Table.
6257
6258    Args:
6259        table: Table name.
6260        db: Database name.
6261        catalog: Catalog name.
6262        quote: Whether to force quotes on the table's identifiers.
6263        alias: Table's alias.
6264
6265    Returns:
6266        The new Table instance.
6267    """
6268    return Table(
6269        this=to_identifier(table, quoted=quoted) if table else None,
6270        db=to_identifier(db, quoted=quoted) if db else None,
6271        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
6272        alias=TableAlias(this=to_identifier(alias)) if alias else None,
6273    )

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:
6276def values(
6277    values: t.Iterable[t.Tuple[t.Any, ...]],
6278    alias: t.Optional[str] = None,
6279    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
6280) -> Values:
6281    """Build VALUES statement.
6282
6283    Example:
6284        >>> values([(1, '2')]).sql()
6285        "VALUES (1, '2')"
6286
6287    Args:
6288        values: values statements that will be converted to SQL
6289        alias: optional alias
6290        columns: Optional list of ordered column names or ordered dictionary of column names to types.
6291         If either are provided then an alias is also required.
6292
6293    Returns:
6294        Values: the Values expression object
6295    """
6296    if columns and not alias:
6297        raise ValueError("Alias is required when providing columns")
6298
6299    return Values(
6300        expressions=[convert(tup) for tup in values],
6301        alias=(
6302            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
6303            if columns
6304            else (TableAlias(this=to_identifier(alias)) if alias else None)
6305        ),
6306    )

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:
6309def var(name: t.Optional[ExpOrStr]) -> Var:
6310    """Build a SQL variable.
6311
6312    Example:
6313        >>> repr(var('x'))
6314        '(VAR this: x)'
6315
6316        >>> repr(var(column('x', table='y')))
6317        '(VAR this: x)'
6318
6319    Args:
6320        name: The name of the var or an expression who's name will become the var.
6321
6322    Returns:
6323        The new variable node.
6324    """
6325    if not name:
6326        raise ValueError("Cannot convert empty name into var.")
6327
6328    if isinstance(name, Expression):
6329        name = name.name
6330    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) -> AlterTable:
6333def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable:
6334    """Build ALTER TABLE... RENAME... expression
6335
6336    Args:
6337        old_name: The old name of the table
6338        new_name: The new name of the table
6339
6340    Returns:
6341        Alter table expression
6342    """
6343    old_table = to_table(old_name)
6344    new_table = to_table(new_name)
6345    return AlterTable(
6346        this=old_table,
6347        actions=[
6348            RenameTable(this=new_table),
6349        ],
6350    )

Build ALTER TABLE... RENAME... expression

Arguments:
  • old_name: The old name of the table
  • new_name: The new name of the table
Returns:

Alter table expression

def convert(value: Any, copy: bool = False) -> Expression:
6353def convert(value: t.Any, copy: bool = False) -> Expression:
6354    """Convert a python value into an expression object.
6355
6356    Raises an error if a conversion is not possible.
6357
6358    Args:
6359        value: A python object.
6360        copy: Whether or not to copy `value` (only applies to Expressions and collections).
6361
6362    Returns:
6363        Expression: the equivalent expression object.
6364    """
6365    if isinstance(value, Expression):
6366        return maybe_copy(value, copy)
6367    if isinstance(value, str):
6368        return Literal.string(value)
6369    if isinstance(value, bool):
6370        return Boolean(this=value)
6371    if value is None or (isinstance(value, float) and math.isnan(value)):
6372        return NULL
6373    if isinstance(value, numbers.Number):
6374        return Literal.number(value)
6375    if isinstance(value, datetime.datetime):
6376        datetime_literal = Literal.string(
6377            (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat()
6378        )
6379        return TimeStrToTime(this=datetime_literal)
6380    if isinstance(value, datetime.date):
6381        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
6382        return DateStrToDate(this=date_literal)
6383    if isinstance(value, tuple):
6384        return Tuple(expressions=[convert(v, copy=copy) for v in value])
6385    if isinstance(value, list):
6386        return Array(expressions=[convert(v, copy=copy) for v in value])
6387    if isinstance(value, dict):
6388        return Map(
6389            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
6390            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
6391        )
6392    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 or not to copy value (only applies to Expressions and collections).
Returns:

Expression: the equivalent expression object.

def replace_children( expression: Expression, fun: Callable, *args, **kwargs) -> None:
6395def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
6396    """
6397    Replace children of an expression with the result of a lambda fun(child) -> exp.
6398    """
6399    for k, v in expression.args.items():
6400        is_list_arg = type(v) is list
6401
6402        child_nodes = v if is_list_arg else [v]
6403        new_child_nodes = []
6404
6405        for cn in child_nodes:
6406            if isinstance(cn, Expression):
6407                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
6408                    new_child_nodes.append(child_node)
6409                    child_node.parent = expression
6410                    child_node.arg_key = k
6411            else:
6412                new_child_nodes.append(cn)
6413
6414        expression.args[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 column_table_names( expression: Expression, exclude: str = '') -> Set[str]:
6417def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
6418    """
6419    Return all table names referenced through columns in an expression.
6420
6421    Example:
6422        >>> import sqlglot
6423        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
6424        ['a', 'c']
6425
6426    Args:
6427        expression: expression to find table names.
6428        exclude: a table name to exclude
6429
6430    Returns:
6431        A list of unique names.
6432    """
6433    return {
6434        table
6435        for table in (column.table for column in expression.find_all(Column))
6436        if table and table != exclude
6437    }

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:
6440def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
6441    """Get the full name of a table as a string.
6442
6443    Args:
6444        table: Table expression node or string.
6445        dialect: The dialect to generate the table name for.
6446        identify: Determines when an identifier should be quoted. Possible values are:
6447            False (default): Never quote, except in cases where it's mandatory by the dialect.
6448            True: Always quote.
6449
6450    Examples:
6451        >>> from sqlglot import exp, parse_one
6452        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
6453        'a.b.c'
6454
6455    Returns:
6456        The table name.
6457    """
6458
6459    table = maybe_parse(table, into=Table, dialect=dialect)
6460
6461    if not table:
6462        raise ValueError(f"Cannot parse {table}")
6463
6464    return ".".join(
6465        part.sql(dialect=dialect, identify=True)
6466        if identify or not SAFE_IDENTIFIER_RE.match(part.name)
6467        else part.name
6468        for part in table.parts
6469    )

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:
6472def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
6473    """Returns a case normalized table name without quotes.
6474
6475    Args:
6476        table: the table to normalize
6477        dialect: the dialect to use for normalization rules
6478        copy: whether or not to copy the expression.
6479
6480    Examples:
6481        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
6482        'A-B.c'
6483    """
6484    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
6485
6486    return ".".join(
6487        p.name
6488        for p in normalize_identifiers(
6489            to_table(table, dialect=dialect, copy=copy), dialect=dialect
6490        ).parts
6491    )

Returns a case normalized table name without quotes.

Arguments:
  • table: the table to normalize
  • dialect: the dialect to use for normalization rules
  • copy: whether or not 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:
6494def replace_tables(
6495    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
6496) -> E:
6497    """Replace all tables in expression according to the mapping.
6498
6499    Args:
6500        expression: expression node to be transformed and replaced.
6501        mapping: mapping of table names.
6502        dialect: the dialect of the mapping table
6503        copy: whether or not to copy the expression.
6504
6505    Examples:
6506        >>> from sqlglot import exp, parse_one
6507        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
6508        'SELECT * FROM c /* a.b */'
6509
6510    Returns:
6511        The mapped expression.
6512    """
6513
6514    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
6515
6516    def _replace_tables(node: Expression) -> Expression:
6517        if isinstance(node, Table):
6518            original = normalize_table_name(node, dialect=dialect)
6519            new_name = mapping.get(original)
6520
6521            if new_name:
6522                table = to_table(
6523                    new_name,
6524                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
6525                )
6526                table.add_comments([original])
6527                return table
6528        return node
6529
6530    return expression.transform(_replace_tables, copy=copy)

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 or not 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:
6533def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
6534    """Replace placeholders in an expression.
6535
6536    Args:
6537        expression: expression node to be transformed and replaced.
6538        args: positional names that will substitute unnamed placeholders in the given order.
6539        kwargs: keyword arguments that will substitute named placeholders.
6540
6541    Examples:
6542        >>> from sqlglot import exp, parse_one
6543        >>> replace_placeholders(
6544        ...     parse_one("select * from :tbl where ? = ?"),
6545        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
6546        ... ).sql()
6547        "SELECT * FROM foo WHERE str_col = 'b'"
6548
6549    Returns:
6550        The mapped expression.
6551    """
6552
6553    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
6554        if isinstance(node, Placeholder):
6555            if node.name:
6556                new_name = kwargs.get(node.name)
6557                if new_name:
6558                    return convert(new_name)
6559            else:
6560                try:
6561                    return convert(next(args))
6562                except StopIteration:
6563                    pass
6564        return node
6565
6566    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, Subqueryable], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, copy: bool = True) -> Expression:
6569def expand(
6570    expression: Expression,
6571    sources: t.Dict[str, Subqueryable],
6572    dialect: DialectType = None,
6573    copy: bool = True,
6574) -> Expression:
6575    """Transforms an expression by expanding all referenced sources into subqueries.
6576
6577    Examples:
6578        >>> from sqlglot import parse_one
6579        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
6580        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
6581
6582        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
6583        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
6584
6585    Args:
6586        expression: The expression to expand.
6587        sources: A dictionary of name to Subqueryables.
6588        dialect: The dialect of the sources dict.
6589        copy: Whether or not to copy the expression during transformation. Defaults to True.
6590
6591    Returns:
6592        The transformed expression.
6593    """
6594    sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
6595
6596    def _expand(node: Expression):
6597        if isinstance(node, Table):
6598            name = normalize_table_name(node, dialect=dialect)
6599            source = sources.get(name)
6600            if source:
6601                subquery = source.subquery(node.alias or name)
6602                subquery.comments = [f"source: {name}"]
6603                return subquery.transform(_expand, copy=False)
6604        return node
6605
6606    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 Subqueryables.
  • dialect: The dialect of the sources dict.
  • copy: Whether or not 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:
6609def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
6610    """
6611    Returns a Func expression.
6612
6613    Examples:
6614        >>> func("abs", 5).sql()
6615        'ABS(5)'
6616
6617        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
6618        'CAST(5 AS DOUBLE)'
6619
6620    Args:
6621        name: the name of the function to build.
6622        args: the args used to instantiate the function of interest.
6623        copy: whether or not to copy the argument expressions.
6624        dialect: the source dialect.
6625        kwargs: the kwargs used to instantiate the function of interest.
6626
6627    Note:
6628        The arguments `args` and `kwargs` are mutually exclusive.
6629
6630    Returns:
6631        An instance of the function of interest, or an anonymous function, if `name` doesn't
6632        correspond to an existing `sqlglot.expressions.Func` class.
6633    """
6634    if args and kwargs:
6635        raise ValueError("Can't use both args and kwargs to instantiate a function.")
6636
6637    from sqlglot.dialects.dialect import Dialect
6638
6639    dialect = Dialect.get_or_raise(dialect)
6640
6641    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
6642    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
6643
6644    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
6645    if constructor:
6646        if converted:
6647            if "dialect" in constructor.__code__.co_varnames:
6648                function = constructor(converted, dialect=dialect)
6649            else:
6650                function = constructor(converted)
6651        elif constructor.__name__ == "from_arg_list":
6652            function = constructor.__self__(**kwargs)  # type: ignore
6653        else:
6654            constructor = FUNCTION_BY_NAME.get(name.upper())
6655            if constructor:
6656                function = constructor(**kwargs)
6657            else:
6658                raise ValueError(
6659                    f"Unable to convert '{name}' into a Func. Either manually construct "
6660                    "the Func expression of interest or parse the function call."
6661                )
6662    else:
6663        kwargs = kwargs or {"expressions": converted}
6664        function = Anonymous(this=name, **kwargs)
6665
6666    for error_message in function.error_messages(converted):
6667        raise ValueError(error_message)
6668
6669    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 or not 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 Func class.

def case( expression: Union[str, Expression, NoneType] = None, **opts) -> Case:
6672def case(
6673    expression: t.Optional[ExpOrStr] = None,
6674    **opts,
6675) -> Case:
6676    """
6677    Initialize a CASE statement.
6678
6679    Example:
6680        case().when("a = 1", "foo").else_("bar")
6681
6682    Args:
6683        expression: Optionally, the input expression (not all dialects support this)
6684        **opts: Extra keyword arguments for parsing `expression`
6685    """
6686    if expression is not None:
6687        this = maybe_parse(expression, **opts)
6688    else:
6689        this = None
6690    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 cast_unless( expression: Union[str, Expression], to: Union[str, DataType, DataType.Type], *types: Union[str, DataType, DataType.Type], **opts: Any) -> Expression | Cast:
6693def cast_unless(
6694    expression: ExpOrStr,
6695    to: DATA_TYPE,
6696    *types: DATA_TYPE,
6697    **opts: t.Any,
6698) -> Expression | Cast:
6699    """
6700    Cast an expression to a data type unless it is a specified type.
6701
6702    Args:
6703        expression: The expression to cast.
6704        to: The data type to cast to.
6705        **types: The types to exclude from casting.
6706        **opts: Extra keyword arguments for parsing `expression`
6707    """
6708    expr = maybe_parse(expression, **opts)
6709    if expr.is_type(*types):
6710        return expr
6711    return cast(expr, to, **opts)

Cast an expression to a data type unless it is a specified type.

Arguments:
  • expression: The expression to cast.
  • to: The data type to cast to.
  • **types: The types to exclude from casting.
  • **opts: Extra keyword arguments for parsing expression
def true() -> Boolean:
6714def true() -> Boolean:
6715    """
6716    Returns a true Boolean expression.
6717    """
6718    return Boolean(this=True)

Returns a true Boolean expression.

def false() -> Boolean:
6721def false() -> Boolean:
6722    """
6723    Returns a false Boolean expression.
6724    """
6725    return Boolean(this=False)

Returns a false Boolean expression.

def null() -> Null:
6728def null() -> Null:
6729    """
6730    Returns a Null expression.
6731    """
6732    return Null()

Returns a Null expression.

TRUE = (BOOLEAN this: True)
FALSE = (BOOLEAN this: False)
NULL = (NULL )