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 = {
1109        "this": True,
1110        "when": False,
1111        "kind": False,
1112        "shallow": False,
1113        "expression": False,
1114        "copy": False,
1115    }
1116
1117
1118class Describe(Expression):
1119    arg_types = {"this": True, "kind": False, "expressions": False}
1120
1121
1122class Kill(Expression):
1123    arg_types = {"this": True, "kind": False}
1124
1125
1126class Pragma(Expression):
1127    pass
1128
1129
1130class Set(Expression):
1131    arg_types = {"expressions": False, "unset": False, "tag": False}
1132
1133
1134class SetItem(Expression):
1135    arg_types = {
1136        "this": False,
1137        "expressions": False,
1138        "kind": False,
1139        "collate": False,  # MySQL SET NAMES statement
1140        "global": False,
1141    }
1142
1143
1144class Show(Expression):
1145    arg_types = {
1146        "this": True,
1147        "target": False,
1148        "offset": False,
1149        "limit": False,
1150        "like": False,
1151        "where": False,
1152        "db": False,
1153        "scope": False,
1154        "scope_kind": False,
1155        "full": False,
1156        "mutex": False,
1157        "query": False,
1158        "channel": False,
1159        "global": False,
1160        "log": False,
1161        "position": False,
1162        "types": False,
1163    }
1164
1165
1166class UserDefinedFunction(Expression):
1167    arg_types = {"this": True, "expressions": False, "wrapped": False}
1168
1169
1170class CharacterSet(Expression):
1171    arg_types = {"this": True, "default": False}
1172
1173
1174class With(Expression):
1175    arg_types = {"expressions": True, "recursive": False}
1176
1177    @property
1178    def recursive(self) -> bool:
1179        return bool(self.args.get("recursive"))
1180
1181
1182class WithinGroup(Expression):
1183    arg_types = {"this": True, "expression": False}
1184
1185
1186# clickhouse supports scalar ctes
1187# https://clickhouse.com/docs/en/sql-reference/statements/select/with
1188class CTE(DerivedTable):
1189    arg_types = {"this": True, "alias": True, "scalar": False}
1190
1191
1192class TableAlias(Expression):
1193    arg_types = {"this": False, "columns": False}
1194
1195    @property
1196    def columns(self):
1197        return self.args.get("columns") or []
1198
1199
1200class BitString(Condition):
1201    pass
1202
1203
1204class HexString(Condition):
1205    pass
1206
1207
1208class ByteString(Condition):
1209    pass
1210
1211
1212class RawString(Condition):
1213    pass
1214
1215
1216class Column(Condition):
1217    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1218
1219    @property
1220    def table(self) -> str:
1221        return self.text("table")
1222
1223    @property
1224    def db(self) -> str:
1225        return self.text("db")
1226
1227    @property
1228    def catalog(self) -> str:
1229        return self.text("catalog")
1230
1231    @property
1232    def output_name(self) -> str:
1233        return self.name
1234
1235    @property
1236    def parts(self) -> t.List[Identifier]:
1237        """Return the parts of a column in order catalog, db, table, name."""
1238        return [
1239            t.cast(Identifier, self.args[part])
1240            for part in ("catalog", "db", "table", "this")
1241            if self.args.get(part)
1242        ]
1243
1244    def to_dot(self) -> Dot | Identifier:
1245        """Converts the column into a dot expression."""
1246        parts = self.parts
1247        parent = self.parent
1248
1249        while parent:
1250            if isinstance(parent, Dot):
1251                parts.append(parent.expression)
1252            parent = parent.parent
1253
1254        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]
1255
1256
1257class ColumnPosition(Expression):
1258    arg_types = {"this": False, "position": True}
1259
1260
1261class ColumnDef(Expression):
1262    arg_types = {
1263        "this": True,
1264        "kind": False,
1265        "constraints": False,
1266        "exists": False,
1267        "position": False,
1268    }
1269
1270    @property
1271    def constraints(self) -> t.List[ColumnConstraint]:
1272        return self.args.get("constraints") or []
1273
1274
1275class AlterColumn(Expression):
1276    arg_types = {
1277        "this": True,
1278        "dtype": False,
1279        "collate": False,
1280        "using": False,
1281        "default": False,
1282        "drop": False,
1283    }
1284
1285
1286class RenameTable(Expression):
1287    pass
1288
1289
1290class SwapTable(Expression):
1291    pass
1292
1293
1294class Comment(Expression):
1295    arg_types = {"this": True, "kind": True, "expression": True, "exists": False}
1296
1297
1298class Comprehension(Expression):
1299    arg_types = {"this": True, "expression": True, "iterator": True, "condition": False}
1300
1301
1302# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
1303class MergeTreeTTLAction(Expression):
1304    arg_types = {
1305        "this": True,
1306        "delete": False,
1307        "recompress": False,
1308        "to_disk": False,
1309        "to_volume": False,
1310    }
1311
1312
1313# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl
1314class MergeTreeTTL(Expression):
1315    arg_types = {
1316        "expressions": True,
1317        "where": False,
1318        "group": False,
1319        "aggregates": False,
1320    }
1321
1322
1323# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1324class IndexConstraintOption(Expression):
1325    arg_types = {
1326        "key_block_size": False,
1327        "using": False,
1328        "parser": False,
1329        "comment": False,
1330        "visible": False,
1331        "engine_attr": False,
1332        "secondary_engine_attr": False,
1333    }
1334
1335
1336class ColumnConstraint(Expression):
1337    arg_types = {"this": False, "kind": True}
1338
1339    @property
1340    def kind(self) -> ColumnConstraintKind:
1341        return self.args["kind"]
1342
1343
1344class ColumnConstraintKind(Expression):
1345    pass
1346
1347
1348class AutoIncrementColumnConstraint(ColumnConstraintKind):
1349    pass
1350
1351
1352class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1353    arg_types = {"this": True, "expression": True}
1354
1355
1356class CaseSpecificColumnConstraint(ColumnConstraintKind):
1357    arg_types = {"not_": True}
1358
1359
1360class CharacterSetColumnConstraint(ColumnConstraintKind):
1361    arg_types = {"this": True}
1362
1363
1364class CheckColumnConstraint(ColumnConstraintKind):
1365    pass
1366
1367
1368class ClusteredColumnConstraint(ColumnConstraintKind):
1369    pass
1370
1371
1372class CollateColumnConstraint(ColumnConstraintKind):
1373    pass
1374
1375
1376class CommentColumnConstraint(ColumnConstraintKind):
1377    pass
1378
1379
1380class CompressColumnConstraint(ColumnConstraintKind):
1381    pass
1382
1383
1384class DateFormatColumnConstraint(ColumnConstraintKind):
1385    arg_types = {"this": True}
1386
1387
1388class DefaultColumnConstraint(ColumnConstraintKind):
1389    pass
1390
1391
1392class EncodeColumnConstraint(ColumnConstraintKind):
1393    pass
1394
1395
1396class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1397    # this: True -> ALWAYS, this: False -> BY DEFAULT
1398    arg_types = {
1399        "this": False,
1400        "expression": False,
1401        "on_null": False,
1402        "start": False,
1403        "increment": False,
1404        "minvalue": False,
1405        "maxvalue": False,
1406        "cycle": False,
1407    }
1408
1409
1410class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1411    arg_types = {"start": True, "hidden": False}
1412
1413
1414# https://dev.mysql.com/doc/refman/8.0/en/create-table.html
1415class IndexColumnConstraint(ColumnConstraintKind):
1416    arg_types = {
1417        "this": False,
1418        "schema": True,
1419        "kind": False,
1420        "index_type": False,
1421        "options": False,
1422    }
1423
1424
1425class InlineLengthColumnConstraint(ColumnConstraintKind):
1426    pass
1427
1428
1429class NonClusteredColumnConstraint(ColumnConstraintKind):
1430    pass
1431
1432
1433class NotForReplicationColumnConstraint(ColumnConstraintKind):
1434    arg_types = {}
1435
1436
1437class NotNullColumnConstraint(ColumnConstraintKind):
1438    arg_types = {"allow_null": False}
1439
1440
1441# https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
1442class OnUpdateColumnConstraint(ColumnConstraintKind):
1443    pass
1444
1445
1446# https://docs.snowflake.com/en/sql-reference/sql/create-external-table#optional-parameters
1447class TransformColumnConstraint(ColumnConstraintKind):
1448    pass
1449
1450
1451class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1452    arg_types = {"desc": False}
1453
1454
1455class TitleColumnConstraint(ColumnConstraintKind):
1456    pass
1457
1458
1459class UniqueColumnConstraint(ColumnConstraintKind):
1460    arg_types = {"this": False, "index_type": False}
1461
1462
1463class UppercaseColumnConstraint(ColumnConstraintKind):
1464    arg_types: t.Dict[str, t.Any] = {}
1465
1466
1467class PathColumnConstraint(ColumnConstraintKind):
1468    pass
1469
1470
1471# computed column expression
1472# https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql?view=sql-server-ver16
1473class ComputedColumnConstraint(ColumnConstraintKind):
1474    arg_types = {"this": True, "persisted": False, "not_null": False}
1475
1476
1477class Constraint(Expression):
1478    arg_types = {"this": True, "expressions": True}
1479
1480
1481class Delete(DML):
1482    arg_types = {
1483        "with": False,
1484        "this": False,
1485        "using": False,
1486        "where": False,
1487        "returning": False,
1488        "limit": False,
1489        "tables": False,  # Multiple-Table Syntax (MySQL)
1490    }
1491
1492    def delete(
1493        self,
1494        table: ExpOrStr,
1495        dialect: DialectType = None,
1496        copy: bool = True,
1497        **opts,
1498    ) -> Delete:
1499        """
1500        Create a DELETE expression or replace the table on an existing DELETE expression.
1501
1502        Example:
1503            >>> delete("tbl").sql()
1504            'DELETE FROM tbl'
1505
1506        Args:
1507            table: the table from which to delete.
1508            dialect: the dialect used to parse the input expression.
1509            copy: if `False`, modify this expression instance in-place.
1510            opts: other options to use to parse the input expressions.
1511
1512        Returns:
1513            Delete: the modified expression.
1514        """
1515        return _apply_builder(
1516            expression=table,
1517            instance=self,
1518            arg="this",
1519            dialect=dialect,
1520            into=Table,
1521            copy=copy,
1522            **opts,
1523        )
1524
1525    def where(
1526        self,
1527        *expressions: t.Optional[ExpOrStr],
1528        append: bool = True,
1529        dialect: DialectType = None,
1530        copy: bool = True,
1531        **opts,
1532    ) -> Delete:
1533        """
1534        Append to or set the WHERE expressions.
1535
1536        Example:
1537            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1538            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1539
1540        Args:
1541            *expressions: the SQL code strings to parse.
1542                If an `Expression` instance is passed, it will be used as-is.
1543                Multiple expressions are combined with an AND operator.
1544            append: if `True`, AND the new expressions to any existing expression.
1545                Otherwise, this resets the expression.
1546            dialect: the dialect used to parse the input expressions.
1547            copy: if `False`, modify this expression instance in-place.
1548            opts: other options to use to parse the input expressions.
1549
1550        Returns:
1551            Delete: the modified expression.
1552        """
1553        return _apply_conjunction_builder(
1554            *expressions,
1555            instance=self,
1556            arg="where",
1557            append=append,
1558            into=Where,
1559            dialect=dialect,
1560            copy=copy,
1561            **opts,
1562        )
1563
1564
1565class Drop(Expression):
1566    arg_types = {
1567        "this": False,
1568        "kind": False,
1569        "exists": False,
1570        "temporary": False,
1571        "materialized": False,
1572        "cascade": False,
1573        "constraints": False,
1574        "purge": False,
1575    }
1576
1577
1578class Filter(Expression):
1579    arg_types = {"this": True, "expression": True}
1580
1581
1582class Check(Expression):
1583    pass
1584
1585
1586# https://docs.snowflake.com/en/sql-reference/constructs/connect-by
1587class Connect(Expression):
1588    arg_types = {"start": False, "connect": True}
1589
1590
1591class Prior(Expression):
1592    pass
1593
1594
1595class Directory(Expression):
1596    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
1597    arg_types = {"this": True, "local": False, "row_format": False}
1598
1599
1600class ForeignKey(Expression):
1601    arg_types = {
1602        "expressions": True,
1603        "reference": False,
1604        "delete": False,
1605        "update": False,
1606    }
1607
1608
1609class ColumnPrefix(Expression):
1610    arg_types = {"this": True, "expression": True}
1611
1612
1613class PrimaryKey(Expression):
1614    arg_types = {"expressions": True, "options": False}
1615
1616
1617# https://www.postgresql.org/docs/9.1/sql-selectinto.html
1618# https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_INTO.html#r_SELECT_INTO-examples
1619class Into(Expression):
1620    arg_types = {"this": True, "temporary": False, "unlogged": False}
1621
1622
1623class From(Expression):
1624    @property
1625    def name(self) -> str:
1626        return self.this.name
1627
1628    @property
1629    def alias_or_name(self) -> str:
1630        return self.this.alias_or_name
1631
1632
1633class Having(Expression):
1634    pass
1635
1636
1637class Hint(Expression):
1638    arg_types = {"expressions": True}
1639
1640
1641class JoinHint(Expression):
1642    arg_types = {"this": True, "expressions": True}
1643
1644
1645class Identifier(Expression):
1646    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
1647
1648    @property
1649    def quoted(self) -> bool:
1650        return bool(self.args.get("quoted"))
1651
1652    @property
1653    def hashable_args(self) -> t.Any:
1654        return (self.this, self.quoted)
1655
1656    @property
1657    def output_name(self) -> str:
1658        return self.name
1659
1660
1661# https://www.postgresql.org/docs/current/indexes-opclass.html
1662class Opclass(Expression):
1663    arg_types = {"this": True, "expression": True}
1664
1665
1666class Index(Expression):
1667    arg_types = {
1668        "this": False,
1669        "table": False,
1670        "using": False,
1671        "where": False,
1672        "columns": False,
1673        "unique": False,
1674        "primary": False,
1675        "amp": False,  # teradata
1676        "partition_by": False,  # teradata
1677        "where": False,  # postgres partial indexes
1678    }
1679
1680
1681class Insert(DDL, DML):
1682    arg_types = {
1683        "with": False,
1684        "this": True,
1685        "expression": False,
1686        "conflict": False,
1687        "returning": False,
1688        "overwrite": False,
1689        "exists": False,
1690        "partition": False,
1691        "alternative": False,
1692        "where": False,
1693        "ignore": False,
1694        "by_name": False,
1695    }
1696
1697    def with_(
1698        self,
1699        alias: ExpOrStr,
1700        as_: ExpOrStr,
1701        recursive: t.Optional[bool] = None,
1702        append: bool = True,
1703        dialect: DialectType = None,
1704        copy: bool = True,
1705        **opts,
1706    ) -> Insert:
1707        """
1708        Append to or set the common table expressions.
1709
1710        Example:
1711            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
1712            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
1713
1714        Args:
1715            alias: the SQL code string to parse as the table name.
1716                If an `Expression` instance is passed, this is used as-is.
1717            as_: the SQL code string to parse as the table expression.
1718                If an `Expression` instance is passed, it will be used as-is.
1719            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1720            append: if `True`, add to any existing expressions.
1721                Otherwise, this resets the expressions.
1722            dialect: the dialect used to parse the input expression.
1723            copy: if `False`, modify this expression instance in-place.
1724            opts: other options to use to parse the input expressions.
1725
1726        Returns:
1727            The modified expression.
1728        """
1729        return _apply_cte_builder(
1730            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
1731        )
1732
1733
1734class OnConflict(Expression):
1735    arg_types = {
1736        "duplicate": False,
1737        "expressions": False,
1738        "nothing": False,
1739        "key": False,
1740        "constraint": False,
1741    }
1742
1743
1744class Returning(Expression):
1745    arg_types = {"expressions": True, "into": False}
1746
1747
1748# https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html
1749class Introducer(Expression):
1750    arg_types = {"this": True, "expression": True}
1751
1752
1753# national char, like n'utf8'
1754class National(Expression):
1755    pass
1756
1757
1758class LoadData(Expression):
1759    arg_types = {
1760        "this": True,
1761        "local": False,
1762        "overwrite": False,
1763        "inpath": True,
1764        "partition": False,
1765        "input_format": False,
1766        "serde": False,
1767    }
1768
1769
1770class Partition(Expression):
1771    arg_types = {"expressions": True}
1772
1773
1774class Fetch(Expression):
1775    arg_types = {
1776        "direction": False,
1777        "count": False,
1778        "percent": False,
1779        "with_ties": False,
1780    }
1781
1782
1783class Group(Expression):
1784    arg_types = {
1785        "expressions": False,
1786        "grouping_sets": False,
1787        "cube": False,
1788        "rollup": False,
1789        "totals": False,
1790        "all": False,
1791    }
1792
1793
1794class Lambda(Expression):
1795    arg_types = {"this": True, "expressions": True}
1796
1797
1798class Limit(Expression):
1799    arg_types = {"this": False, "expression": True, "offset": False}
1800
1801
1802class Literal(Condition):
1803    arg_types = {"this": True, "is_string": True}
1804
1805    @property
1806    def hashable_args(self) -> t.Any:
1807        return (self.this, self.args.get("is_string"))
1808
1809    @classmethod
1810    def number(cls, number) -> Literal:
1811        return cls(this=str(number), is_string=False)
1812
1813    @classmethod
1814    def string(cls, string) -> Literal:
1815        return cls(this=str(string), is_string=True)
1816
1817    @property
1818    def output_name(self) -> str:
1819        return self.name
1820
1821
1822class Join(Expression):
1823    arg_types = {
1824        "this": True,
1825        "on": False,
1826        "side": False,
1827        "kind": False,
1828        "using": False,
1829        "method": False,
1830        "global": False,
1831        "hint": False,
1832    }
1833
1834    @property
1835    def method(self) -> str:
1836        return self.text("method").upper()
1837
1838    @property
1839    def kind(self) -> str:
1840        return self.text("kind").upper()
1841
1842    @property
1843    def side(self) -> str:
1844        return self.text("side").upper()
1845
1846    @property
1847    def hint(self) -> str:
1848        return self.text("hint").upper()
1849
1850    @property
1851    def alias_or_name(self) -> str:
1852        return self.this.alias_or_name
1853
1854    def on(
1855        self,
1856        *expressions: t.Optional[ExpOrStr],
1857        append: bool = True,
1858        dialect: DialectType = None,
1859        copy: bool = True,
1860        **opts,
1861    ) -> Join:
1862        """
1863        Append to or set the ON expressions.
1864
1865        Example:
1866            >>> import sqlglot
1867            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
1868            'JOIN x ON y = 1'
1869
1870        Args:
1871            *expressions: the SQL code strings to parse.
1872                If an `Expression` instance is passed, it will be used as-is.
1873                Multiple expressions are combined with an AND operator.
1874            append: if `True`, AND the new expressions to any existing expression.
1875                Otherwise, this resets the expression.
1876            dialect: the dialect used to parse the input expressions.
1877            copy: if `False`, modify this expression instance in-place.
1878            opts: other options to use to parse the input expressions.
1879
1880        Returns:
1881            The modified Join expression.
1882        """
1883        join = _apply_conjunction_builder(
1884            *expressions,
1885            instance=self,
1886            arg="on",
1887            append=append,
1888            dialect=dialect,
1889            copy=copy,
1890            **opts,
1891        )
1892
1893        if join.kind == "CROSS":
1894            join.set("kind", None)
1895
1896        return join
1897
1898    def using(
1899        self,
1900        *expressions: t.Optional[ExpOrStr],
1901        append: bool = True,
1902        dialect: DialectType = None,
1903        copy: bool = True,
1904        **opts,
1905    ) -> Join:
1906        """
1907        Append to or set the USING expressions.
1908
1909        Example:
1910            >>> import sqlglot
1911            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
1912            'JOIN x USING (foo, bla)'
1913
1914        Args:
1915            *expressions: the SQL code strings to parse.
1916                If an `Expression` instance is passed, it will be used as-is.
1917            append: if `True`, concatenate the new expressions to the existing "using" list.
1918                Otherwise, this resets the expression.
1919            dialect: the dialect used to parse the input expressions.
1920            copy: if `False`, modify this expression instance in-place.
1921            opts: other options to use to parse the input expressions.
1922
1923        Returns:
1924            The modified Join expression.
1925        """
1926        join = _apply_list_builder(
1927            *expressions,
1928            instance=self,
1929            arg="using",
1930            append=append,
1931            dialect=dialect,
1932            copy=copy,
1933            **opts,
1934        )
1935
1936        if join.kind == "CROSS":
1937            join.set("kind", None)
1938
1939        return join
1940
1941
1942class Lateral(UDTF):
1943    arg_types = {"this": True, "view": False, "outer": False, "alias": False}
1944
1945
1946class MatchRecognize(Expression):
1947    arg_types = {
1948        "partition_by": False,
1949        "order": False,
1950        "measures": False,
1951        "rows": False,
1952        "after": False,
1953        "pattern": False,
1954        "define": False,
1955        "alias": False,
1956    }
1957
1958
1959# Clickhouse FROM FINAL modifier
1960# https://clickhouse.com/docs/en/sql-reference/statements/select/from/#final-modifier
1961class Final(Expression):
1962    pass
1963
1964
1965class Offset(Expression):
1966    arg_types = {"this": False, "expression": True}
1967
1968
1969class Order(Expression):
1970    arg_types = {"this": False, "expressions": True}
1971
1972
1973# hive specific sorts
1974# https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy
1975class Cluster(Order):
1976    pass
1977
1978
1979class Distribute(Order):
1980    pass
1981
1982
1983class Sort(Order):
1984    pass
1985
1986
1987class Ordered(Expression):
1988    arg_types = {"this": True, "desc": False, "nulls_first": True}
1989
1990
1991class Property(Expression):
1992    arg_types = {"this": True, "value": True}
1993
1994
1995class AlgorithmProperty(Property):
1996    arg_types = {"this": True}
1997
1998
1999class AutoIncrementProperty(Property):
2000    arg_types = {"this": True}
2001
2002
2003class BlockCompressionProperty(Property):
2004    arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
2005
2006
2007class CharacterSetProperty(Property):
2008    arg_types = {"this": True, "default": True}
2009
2010
2011class ChecksumProperty(Property):
2012    arg_types = {"on": False, "default": False}
2013
2014
2015class CollateProperty(Property):
2016    arg_types = {"this": True, "default": False}
2017
2018
2019class CopyGrantsProperty(Property):
2020    arg_types = {}
2021
2022
2023class DataBlocksizeProperty(Property):
2024    arg_types = {
2025        "size": False,
2026        "units": False,
2027        "minimum": False,
2028        "maximum": False,
2029        "default": False,
2030    }
2031
2032
2033class DefinerProperty(Property):
2034    arg_types = {"this": True}
2035
2036
2037class DistKeyProperty(Property):
2038    arg_types = {"this": True}
2039
2040
2041class DistStyleProperty(Property):
2042    arg_types = {"this": True}
2043
2044
2045class EngineProperty(Property):
2046    arg_types = {"this": True}
2047
2048
2049class HeapProperty(Property):
2050    arg_types = {}
2051
2052
2053class ToTableProperty(Property):
2054    arg_types = {"this": True}
2055
2056
2057class ExecuteAsProperty(Property):
2058    arg_types = {"this": True}
2059
2060
2061class ExternalProperty(Property):
2062    arg_types = {"this": False}
2063
2064
2065class FallbackProperty(Property):
2066    arg_types = {"no": True, "protection": False}
2067
2068
2069class FileFormatProperty(Property):
2070    arg_types = {"this": True}
2071
2072
2073class FreespaceProperty(Property):
2074    arg_types = {"this": True, "percent": False}
2075
2076
2077class InputModelProperty(Property):
2078    arg_types = {"this": True}
2079
2080
2081class OutputModelProperty(Property):
2082    arg_types = {"this": True}
2083
2084
2085class IsolatedLoadingProperty(Property):
2086    arg_types = {
2087        "no": True,
2088        "concurrent": True,
2089        "for_all": True,
2090        "for_insert": True,
2091        "for_none": True,
2092    }
2093
2094
2095class JournalProperty(Property):
2096    arg_types = {
2097        "no": False,
2098        "dual": False,
2099        "before": False,
2100        "local": False,
2101        "after": False,
2102    }
2103
2104
2105class LanguageProperty(Property):
2106    arg_types = {"this": True}
2107
2108
2109# spark ddl
2110class ClusteredByProperty(Property):
2111    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
2112
2113
2114class DictProperty(Property):
2115    arg_types = {"this": True, "kind": True, "settings": False}
2116
2117
2118class DictSubProperty(Property):
2119    pass
2120
2121
2122class DictRange(Property):
2123    arg_types = {"this": True, "min": True, "max": True}
2124
2125
2126# Clickhouse CREATE ... ON CLUSTER modifier
2127# https://clickhouse.com/docs/en/sql-reference/distributed-ddl
2128class OnCluster(Property):
2129    arg_types = {"this": True}
2130
2131
2132class LikeProperty(Property):
2133    arg_types = {"this": True, "expressions": False}
2134
2135
2136class LocationProperty(Property):
2137    arg_types = {"this": True}
2138
2139
2140class LockingProperty(Property):
2141    arg_types = {
2142        "this": False,
2143        "kind": True,
2144        "for_or_in": False,
2145        "lock_type": True,
2146        "override": False,
2147    }
2148
2149
2150class LogProperty(Property):
2151    arg_types = {"no": True}
2152
2153
2154class MaterializedProperty(Property):
2155    arg_types = {"this": False}
2156
2157
2158class MergeBlockRatioProperty(Property):
2159    arg_types = {"this": False, "no": False, "default": False, "percent": False}
2160
2161
2162class NoPrimaryIndexProperty(Property):
2163    arg_types = {}
2164
2165
2166class OnProperty(Property):
2167    arg_types = {"this": True}
2168
2169
2170class OnCommitProperty(Property):
2171    arg_types = {"delete": False}
2172
2173
2174class PartitionedByProperty(Property):
2175    arg_types = {"this": True}
2176
2177
2178# https://www.postgresql.org/docs/current/sql-createtable.html
2179class PartitionBoundSpec(Expression):
2180    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
2181    arg_types = {
2182        "this": False,
2183        "expression": False,
2184        "from_expressions": False,
2185        "to_expressions": False,
2186    }
2187
2188
2189class PartitionedOfProperty(Property):
2190    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
2191    arg_types = {"this": True, "expression": True}
2192
2193
2194class RemoteWithConnectionModelProperty(Property):
2195    arg_types = {"this": True}
2196
2197
2198class ReturnsProperty(Property):
2199    arg_types = {"this": True, "is_table": False, "table": False}
2200
2201
2202class RowFormatProperty(Property):
2203    arg_types = {"this": True}
2204
2205
2206class RowFormatDelimitedProperty(Property):
2207    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
2208    arg_types = {
2209        "fields": False,
2210        "escaped": False,
2211        "collection_items": False,
2212        "map_keys": False,
2213        "lines": False,
2214        "null": False,
2215        "serde": False,
2216    }
2217
2218
2219class RowFormatSerdeProperty(Property):
2220    arg_types = {"this": True, "serde_properties": False}
2221
2222
2223# https://spark.apache.org/docs/3.1.2/sql-ref-syntax-qry-select-transform.html
2224class QueryTransform(Expression):
2225    arg_types = {
2226        "expressions": True,
2227        "command_script": True,
2228        "schema": False,
2229        "row_format_before": False,
2230        "record_writer": False,
2231        "row_format_after": False,
2232        "record_reader": False,
2233    }
2234
2235
2236class SampleProperty(Property):
2237    arg_types = {"this": True}
2238
2239
2240class SchemaCommentProperty(Property):
2241    arg_types = {"this": True}
2242
2243
2244class SerdeProperties(Property):
2245    arg_types = {"expressions": True}
2246
2247
2248class SetProperty(Property):
2249    arg_types = {"multi": True}
2250
2251
2252class SettingsProperty(Property):
2253    arg_types = {"expressions": True}
2254
2255
2256class SortKeyProperty(Property):
2257    arg_types = {"this": True, "compound": False}
2258
2259
2260class SqlSecurityProperty(Property):
2261    arg_types = {"definer": True}
2262
2263
2264class StabilityProperty(Property):
2265    arg_types = {"this": True}
2266
2267
2268class TemporaryProperty(Property):
2269    arg_types = {}
2270
2271
2272class TransformModelProperty(Property):
2273    arg_types = {"expressions": True}
2274
2275
2276class TransientProperty(Property):
2277    arg_types = {"this": False}
2278
2279
2280class VolatileProperty(Property):
2281    arg_types = {"this": False}
2282
2283
2284class WithDataProperty(Property):
2285    arg_types = {"no": True, "statistics": False}
2286
2287
2288class WithJournalTableProperty(Property):
2289    arg_types = {"this": True}
2290
2291
2292class WithSystemVersioningProperty(Property):
2293    # this -> history table name, expression -> data consistency check
2294    arg_types = {"this": False, "expression": False}
2295
2296
2297class Properties(Expression):
2298    arg_types = {"expressions": True}
2299
2300    NAME_TO_PROPERTY = {
2301        "ALGORITHM": AlgorithmProperty,
2302        "AUTO_INCREMENT": AutoIncrementProperty,
2303        "CHARACTER SET": CharacterSetProperty,
2304        "CLUSTERED_BY": ClusteredByProperty,
2305        "COLLATE": CollateProperty,
2306        "COMMENT": SchemaCommentProperty,
2307        "DEFINER": DefinerProperty,
2308        "DISTKEY": DistKeyProperty,
2309        "DISTSTYLE": DistStyleProperty,
2310        "ENGINE": EngineProperty,
2311        "EXECUTE AS": ExecuteAsProperty,
2312        "FORMAT": FileFormatProperty,
2313        "LANGUAGE": LanguageProperty,
2314        "LOCATION": LocationProperty,
2315        "PARTITIONED_BY": PartitionedByProperty,
2316        "RETURNS": ReturnsProperty,
2317        "ROW_FORMAT": RowFormatProperty,
2318        "SORTKEY": SortKeyProperty,
2319    }
2320
2321    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
2322
2323    # CREATE property locations
2324    # Form: schema specified
2325    #   create [POST_CREATE]
2326    #     table a [POST_NAME]
2327    #     (b int) [POST_SCHEMA]
2328    #     with ([POST_WITH])
2329    #     index (b) [POST_INDEX]
2330    #
2331    # Form: alias selection
2332    #   create [POST_CREATE]
2333    #     table a [POST_NAME]
2334    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
2335    #     index (c) [POST_INDEX]
2336    class Location(AutoName):
2337        POST_CREATE = auto()
2338        POST_NAME = auto()
2339        POST_SCHEMA = auto()
2340        POST_WITH = auto()
2341        POST_ALIAS = auto()
2342        POST_EXPRESSION = auto()
2343        POST_INDEX = auto()
2344        UNSUPPORTED = auto()
2345
2346    @classmethod
2347    def from_dict(cls, properties_dict: t.Dict) -> Properties:
2348        expressions = []
2349        for key, value in properties_dict.items():
2350            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
2351            if property_cls:
2352                expressions.append(property_cls(this=convert(value)))
2353            else:
2354                expressions.append(Property(this=Literal.string(key), value=convert(value)))
2355
2356        return cls(expressions=expressions)
2357
2358
2359class Qualify(Expression):
2360    pass
2361
2362
2363class InputOutputFormat(Expression):
2364    arg_types = {"input_format": False, "output_format": False}
2365
2366
2367# https://www.ibm.com/docs/en/ias?topic=procedures-return-statement-in-sql
2368class Return(Expression):
2369    pass
2370
2371
2372class Reference(Expression):
2373    arg_types = {"this": True, "expressions": False, "options": False}
2374
2375
2376class Tuple(Expression):
2377    arg_types = {"expressions": False}
2378
2379    def isin(
2380        self,
2381        *expressions: t.Any,
2382        query: t.Optional[ExpOrStr] = None,
2383        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
2384        copy: bool = True,
2385        **opts,
2386    ) -> In:
2387        return In(
2388            this=maybe_copy(self, copy),
2389            expressions=[convert(e, copy=copy) for e in expressions],
2390            query=maybe_parse(query, copy=copy, **opts) if query else None,
2391            unnest=Unnest(
2392                expressions=[
2393                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
2394                ]
2395            )
2396            if unnest
2397            else None,
2398        )
2399
2400
2401class Subqueryable(Unionable):
2402    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
2403        """
2404        Convert this expression to an aliased expression that can be used as a Subquery.
2405
2406        Example:
2407            >>> subquery = Select().select("x").from_("tbl").subquery()
2408            >>> Select().select("x").from_(subquery).sql()
2409            'SELECT x FROM (SELECT x FROM tbl)'
2410
2411        Args:
2412            alias (str | Identifier): an optional alias for the subquery
2413            copy (bool): if `False`, modify this expression instance in-place.
2414
2415        Returns:
2416            Alias: the subquery
2417        """
2418        instance = maybe_copy(self, copy)
2419        if not isinstance(alias, Expression):
2420            alias = TableAlias(this=to_identifier(alias)) if alias else None
2421
2422        return Subquery(this=instance, alias=alias)
2423
2424    def limit(
2425        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2426    ) -> Select:
2427        raise NotImplementedError
2428
2429    @property
2430    def ctes(self):
2431        with_ = self.args.get("with")
2432        if not with_:
2433            return []
2434        return with_.expressions
2435
2436    @property
2437    def selects(self) -> t.List[Expression]:
2438        raise NotImplementedError("Subqueryable objects must implement `selects`")
2439
2440    @property
2441    def named_selects(self) -> t.List[str]:
2442        raise NotImplementedError("Subqueryable objects must implement `named_selects`")
2443
2444    def select(
2445        self,
2446        *expressions: t.Optional[ExpOrStr],
2447        append: bool = True,
2448        dialect: DialectType = None,
2449        copy: bool = True,
2450        **opts,
2451    ) -> Subqueryable:
2452        raise NotImplementedError("Subqueryable objects must implement `select`")
2453
2454    def with_(
2455        self,
2456        alias: ExpOrStr,
2457        as_: ExpOrStr,
2458        recursive: t.Optional[bool] = None,
2459        append: bool = True,
2460        dialect: DialectType = None,
2461        copy: bool = True,
2462        **opts,
2463    ) -> Subqueryable:
2464        """
2465        Append to or set the common table expressions.
2466
2467        Example:
2468            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
2469            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
2470
2471        Args:
2472            alias: the SQL code string to parse as the table name.
2473                If an `Expression` instance is passed, this is used as-is.
2474            as_: the SQL code string to parse as the table expression.
2475                If an `Expression` instance is passed, it will be used as-is.
2476            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2477            append: if `True`, add to any existing expressions.
2478                Otherwise, this resets the expressions.
2479            dialect: the dialect used to parse the input expression.
2480            copy: if `False`, modify this expression instance in-place.
2481            opts: other options to use to parse the input expressions.
2482
2483        Returns:
2484            The modified expression.
2485        """
2486        return _apply_cte_builder(
2487            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
2488        )
2489
2490
2491QUERY_MODIFIERS = {
2492    "match": False,
2493    "laterals": False,
2494    "joins": False,
2495    "connect": False,
2496    "pivots": False,
2497    "where": False,
2498    "group": False,
2499    "having": False,
2500    "qualify": False,
2501    "windows": False,
2502    "distribute": False,
2503    "sort": False,
2504    "cluster": False,
2505    "order": False,
2506    "limit": False,
2507    "offset": False,
2508    "locks": False,
2509    "sample": False,
2510    "settings": False,
2511    "format": False,
2512}
2513
2514
2515# https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table?view=sql-server-ver16
2516class WithTableHint(Expression):
2517    arg_types = {"expressions": True}
2518
2519
2520# https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
2521class IndexTableHint(Expression):
2522    arg_types = {"this": True, "expressions": False, "target": False}
2523
2524
2525class Table(Expression):
2526    arg_types = {
2527        "this": True,
2528        "alias": False,
2529        "db": False,
2530        "catalog": False,
2531        "laterals": False,
2532        "joins": False,
2533        "pivots": False,
2534        "hints": False,
2535        "system_time": False,
2536        "version": False,
2537        "format": False,
2538        "pattern": False,
2539        "index": False,
2540        "ordinality": 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
2579
2580
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
2672
2673
2674class Except(Union):
2675    pass
2676
2677
2678class Intersect(Union):
2679    pass
2680
2681
2682class Unnest(UDTF):
2683    arg_types = {
2684        "expressions": True,
2685        "alias": False,
2686        "offset": False,
2687    }
2688
2689
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    }
2701
2702
2703class Values(UDTF):
2704    arg_types = {"expressions": True, "alias": False}
2705
2706
2707class Var(Expression):
2708    pass
2709
2710
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}
2723
2724
2725class Schema(Expression):
2726    arg_types = {"this": False, "expressions": False}
2727
2728
2729# https://dev.mysql.com/doc/refman/8.0/en/select.html
2730# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html
2731class Lock(Expression):
2732    arg_types = {"update": True, "expressions": False, "wait": False}
2733
2734
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
3426
3427
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
3469
3470
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    }
3485
3486
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    }
3495
3496
3497# Represents both the standard SQL PIVOT operator and DuckDB's "simplified" PIVOT syntax
3498# https://duckdb.org/docs/sql/statements/pivot
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    }
3511
3512
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    }
3523
3524
3525class WindowSpec(Expression):
3526    arg_types = {
3527        "kind": False,
3528        "start": False,
3529        "start_side": False,
3530        "end": False,
3531        "end_side": False,
3532    }
3533
3534
3535class Where(Expression):
3536    pass
3537
3538
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
3549
3550
3551class Parameter(Condition):
3552    arg_types = {"this": True, "expression": False}
3553
3554
3555class SessionParameter(Condition):
3556    arg_types = {"this": True, "kind": False}
3557
3558
3559class Placeholder(Condition):
3560    arg_types = {"this": False, "kind": False}
3561
3562
3563class Null(Condition):
3564    arg_types: t.Dict[str, t.Any] = {}
3565
3566    @property
3567    def name(self) -> str:
3568        return "NULL"
3569
3570
3571class Boolean(Condition):
3572    pass
3573
3574
3575class DataTypeParam(Expression):
3576    arg_types = {"this": True, "expression": False}
3577
3578
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
3798
3799
3800DATA_TYPE = t.Union[str, DataType, DataType.Type]
3801
3802
3803# https://www.postgresql.org/docs/15/datatype-pseudo.html
3804class PseudoType(DataType):
3805    arg_types = {"this": True}
3806
3807
3808# https://www.postgresql.org/docs/15/datatype-oid.html
3809class ObjectIdentifier(DataType):
3810    arg_types = {"this": True}
3811
3812
3813# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...)
3814class SubqueryPredicate(Predicate):
3815    pass
3816
3817
3818class All(SubqueryPredicate):
3819    pass
3820
3821
3822class Any(SubqueryPredicate):
3823    pass
3824
3825
3826class Exists(SubqueryPredicate):
3827    pass
3828
3829
3830# Commands to interact with the databases or engines. For most of the command
3831# expressions we parse whatever comes after the command's name as a string.
3832class Command(Expression):
3833    arg_types = {"this": True, "expression": False}
3834
3835
3836class Transaction(Expression):
3837    arg_types = {"this": False, "modes": False, "mark": False}
3838
3839
3840class Commit(Expression):
3841    arg_types = {"chain": False, "this": False, "durability": False}
3842
3843
3844class Rollback(Expression):
3845    arg_types = {"savepoint": False, "this": False}
3846
3847
3848class AlterTable(Expression):
3849    arg_types = {"this": True, "actions": True, "exists": False, "only": False}
3850
3851
3852class AddConstraint(Expression):
3853    arg_types = {"this": False, "expression": False, "enforced": False}
3854
3855
3856class DropPartition(Expression):
3857    arg_types = {"expressions": True, "exists": False}
3858
3859
3860# Binary expressions like (ADD a b)
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
3871
3872
3873class Add(Binary):
3874    pass
3875
3876
3877class Connector(Binary):
3878    pass
3879
3880
3881class And(Connector):
3882    pass
3883
3884
3885class Or(Connector):
3886    pass
3887
3888
3889class BitwiseAnd(Binary):
3890    pass
3891
3892
3893class BitwiseLeftShift(Binary):
3894    pass
3895
3896
3897class BitwiseOr(Binary):
3898    pass
3899
3900
3901class BitwiseRightShift(Binary):
3902    pass
3903
3904
3905class BitwiseXor(Binary):
3906    pass
3907
3908
3909class Div(Binary):
3910    arg_types = {"this": True, "expression": True, "typed": False, "safe": False}
3911
3912
3913class Overlaps(Binary):
3914    pass
3915
3916
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
3949
3950
3951class DPipe(Binary):
3952    arg_types = {"this": True, "expression": True, "safe": False}
3953
3954
3955class EQ(Binary, Predicate):
3956    pass
3957
3958
3959class NullSafeEQ(Binary, Predicate):
3960    pass
3961
3962
3963class NullSafeNEQ(Binary, Predicate):
3964    pass
3965
3966
3967# Represents e.g. := in DuckDB which is mostly used for setting parameters
3968class PropertyEQ(Binary):
3969    pass
3970
3971
3972class Distance(Binary):
3973    pass
3974
3975
3976class Escape(Binary):
3977    pass
3978
3979
3980class Glob(Binary, Predicate):
3981    pass
3982
3983
3984class GT(Binary, Predicate):
3985    pass
3986
3987
3988class GTE(Binary, Predicate):
3989    pass
3990
3991
3992class ILike(Binary, Predicate):
3993    pass
3994
3995
3996class ILikeAny(Binary, Predicate):
3997    pass
3998
3999
4000class IntDiv(Binary):
4001    pass
4002
4003
4004class Is(Binary, Predicate):
4005    pass
4006
4007
4008class Kwarg(Binary):
4009    """Kwarg in special functions like func(kwarg => y)."""
4010
4011
4012class Like(Binary, Predicate):
4013    pass
4014
4015
4016class LikeAny(Binary, Predicate):
4017    pass
4018
4019
4020class LT(Binary, Predicate):
4021    pass
4022
4023
4024class LTE(Binary, Predicate):
4025    pass
4026
4027
4028class Mod(Binary):
4029    pass
4030
4031
4032class Mul(Binary):
4033    pass
4034
4035
4036class NEQ(Binary, Predicate):
4037    pass
4038
4039
4040# https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH
4041class Operator(Binary):
4042    arg_types = {"this": True, "operator": True, "expression": True}
4043
4044
4045class SimilarTo(Binary, Predicate):
4046    pass
4047
4048
4049class Slice(Binary):
4050    arg_types = {"this": False, "expression": False}
4051
4052
4053class Sub(Binary):
4054    pass
4055
4056
4057class ArrayOverlaps(Binary):
4058    pass
4059
4060
4061# Unary Expressions
4062# (NOT a)
4063class Unary(Condition):
4064    pass
4065
4066
4067class BitwiseNot(Unary):
4068    pass
4069
4070
4071class Not(Unary):
4072    pass
4073
4074
4075class Paren(Unary):
4076    arg_types = {"this": True, "with": False}
4077
4078    @property
4079    def output_name(self) -> str:
4080        return self.this.name
4081
4082
4083class Neg(Unary):
4084    pass
4085
4086
4087class Alias(Expression):
4088    arg_types = {"this": True, "alias": False}
4089
4090    @property
4091    def output_name(self) -> str:
4092        return self.alias
4093
4094
4095class Aliases(Expression):
4096    arg_types = {"this": True, "expressions": True}
4097
4098    @property
4099    def aliases(self):
4100        return self.expressions
4101
4102
4103class AtTimeZone(Expression):
4104    arg_types = {"this": True, "zone": True}
4105
4106
4107class Between(Predicate):
4108    arg_types = {"this": True, "low": True, "high": True}
4109
4110
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
4121
4122
4123class Distinct(Expression):
4124    arg_types = {"expressions": False, "on": False}
4125
4126
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    }
4136
4137
4138# https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#for-in
4139class ForIn(Expression):
4140    arg_types = {"this": True, "expression": True}
4141
4142
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")
4175
4176
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        )
4185
4186
4187# https://www.oracletutorial.com/oracle-basics/oracle-interval/
4188# https://trino.io/docs/current/language/types.html#interval-day-to-second
4189# https://docs.databricks.com/en/sql/language-manual/data-types/interval-type.html
4190class IntervalSpan(DataType):
4191    arg_types = {"this": True, "expression": True}
4192
4193
4194class Interval(TimeUnit):
4195    arg_types = {"this": False, "unit": False}
4196
4197
4198class IgnoreNulls(Expression):
4199    pass
4200
4201
4202class RespectNulls(Expression):
4203    pass
4204
4205
4206# Functions
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()}
4254
4255
4256class AggFunc(Func):
4257    pass
4258
4259
4260class ParameterizedAgg(AggFunc):
4261    arg_types = {"this": True, "expressions": True, "params": True}
4262
4263
4264class Abs(Func):
4265    pass
4266
4267
4268class ArgMax(AggFunc):
4269    arg_types = {"this": True, "expression": True, "count": False}
4270    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
4271
4272
4273class ArgMin(AggFunc):
4274    arg_types = {"this": True, "expression": True, "count": False}
4275    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
4276
4277
4278class ApproxTopK(AggFunc):
4279    arg_types = {"this": True, "expression": False, "counters": False}
4280
4281
4282class Flatten(Func):
4283    pass
4284
4285
4286# https://spark.apache.org/docs/latest/api/sql/index.html#transform
4287class Transform(Func):
4288    arg_types = {"this": True, "expression": True}
4289
4290
4291class Anonymous(Func):
4292    arg_types = {"this": True, "expressions": False}
4293    is_var_len_args = True
4294
4295
4296# https://docs.snowflake.com/en/sql-reference/functions/hll
4297# https://docs.aws.amazon.com/redshift/latest/dg/r_HLL_function.html
4298class Hll(AggFunc):
4299    arg_types = {"this": True, "expressions": False}
4300    is_var_len_args = True
4301
4302
4303class ApproxDistinct(AggFunc):
4304    arg_types = {"this": True, "accuracy": False}
4305    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
4306
4307
4308class Array(Func):
4309    arg_types = {"expressions": False}
4310    is_var_len_args = True
4311
4312
4313# https://docs.snowflake.com/en/sql-reference/functions/to_char
4314# https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/TO_CHAR-number.html
4315class ToChar(Func):
4316    arg_types = {"this": True, "format": False, "nlsparam": False}
4317
4318
4319class GenerateSeries(Func):
4320    arg_types = {"start": True, "end": True, "step": False}
4321
4322
4323class ArrayAgg(AggFunc):
4324    pass
4325
4326
4327class ArrayUniqueAgg(AggFunc):
4328    pass
4329
4330
4331class ArrayAll(Func):
4332    arg_types = {"this": True, "expression": True}
4333
4334
4335class ArrayAny(Func):
4336    arg_types = {"this": True, "expression": True}
4337
4338
4339class ArrayConcat(Func):
4340    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
4341    arg_types = {"this": True, "expressions": False}
4342    is_var_len_args = True
4343
4344
4345class ArrayContains(Binary, Func):
4346    pass
4347
4348
4349class ArrayContained(Binary):
4350    pass
4351
4352
4353class ArrayFilter(Func):
4354    arg_types = {"this": True, "expression": True}
4355    _sql_names = ["FILTER", "ARRAY_FILTER"]
4356
4357
4358class ArrayJoin(Func):
4359    arg_types = {"this": True, "expression": True, "null": False}
4360
4361
4362class ArraySize(Func):
4363    arg_types = {"this": True, "expression": False}
4364
4365
4366class ArraySort(Func):
4367    arg_types = {"this": True, "expression": False}
4368
4369
4370class ArraySum(Func):
4371    pass
4372
4373
4374class ArrayUnionAgg(AggFunc):
4375    pass
4376
4377
4378class Avg(AggFunc):
4379    pass
4380
4381
4382class AnyValue(AggFunc):
4383    arg_types = {"this": True, "having": False, "max": False, "ignore_nulls": False}
4384
4385
4386class First(Func):
4387    arg_types = {"this": True, "ignore_nulls": False}
4388
4389
4390class Last(Func):
4391    arg_types = {"this": True, "ignore_nulls": False}
4392
4393
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
4412
4413
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)
4442
4443
4444class TryCast(Cast):
4445    pass
4446
4447
4448class CastToStrType(Func):
4449    arg_types = {"this": True, "to": True}
4450
4451
4452class Collate(Binary, Func):
4453    pass
4454
4455
4456class Ceil(Func):
4457    arg_types = {"this": True, "decimals": False}
4458    _sql_names = ["CEIL", "CEILING"]
4459
4460
4461class Coalesce(Func):
4462    arg_types = {"this": True, "expressions": False}
4463    is_var_len_args = True
4464    _sql_names = ["COALESCE", "IFNULL", "NVL"]
4465
4466
4467class Chr(Func):
4468    arg_types = {"this": True, "charset": False, "expressions": False}
4469    is_var_len_args = True
4470    _sql_names = ["CHR", "CHAR"]
4471
4472
4473class Concat(Func):
4474    arg_types = {"expressions": True, "safe": False}
4475    is_var_len_args = True
4476
4477
4478class ConcatWs(Concat):
4479    _sql_names = ["CONCAT_WS"]
4480
4481
4482class Count(AggFunc):
4483    arg_types = {"this": False, "expressions": False}
4484    is_var_len_args = True
4485
4486
4487class CountIf(AggFunc):
4488    pass
4489
4490
4491class CurrentDate(Func):
4492    arg_types = {"this": False}
4493
4494
4495class CurrentDatetime(Func):
4496    arg_types = {"this": False}
4497
4498
4499class CurrentTime(Func):
4500    arg_types = {"this": False}
4501
4502
4503class CurrentTimestamp(Func):
4504    arg_types = {"this": False}
4505
4506
4507class CurrentUser(Func):
4508    arg_types = {"this": False}
4509
4510
4511class DateAdd(Func, IntervalOp):
4512    arg_types = {"this": True, "expression": True, "unit": False}
4513
4514
4515class DateSub(Func, IntervalOp):
4516    arg_types = {"this": True, "expression": True, "unit": False}
4517
4518
4519class DateDiff(Func, TimeUnit):
4520    _sql_names = ["DATEDIFF", "DATE_DIFF"]
4521    arg_types = {"this": True, "expression": True, "unit": False}
4522
4523
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"]
4530
4531
4532class DatetimeAdd(Func, IntervalOp):
4533    arg_types = {"this": True, "expression": True, "unit": False}
4534
4535
4536class DatetimeSub(Func, IntervalOp):
4537    arg_types = {"this": True, "expression": True, "unit": False}
4538
4539
4540class DatetimeDiff(Func, TimeUnit):
4541    arg_types = {"this": True, "expression": True, "unit": False}
4542
4543
4544class DatetimeTrunc(Func, TimeUnit):
4545    arg_types = {"this": True, "unit": True, "zone": False}
4546
4547
4548class DayOfWeek(Func):
4549    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
4550
4551
4552class DayOfMonth(Func):
4553    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
4554
4555
4556class DayOfYear(Func):
4557    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
4558
4559
4560class ToDays(Func):
4561    pass
4562
4563
4564class WeekOfYear(Func):
4565    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
4566
4567
4568class MonthsBetween(Func):
4569    arg_types = {"this": True, "expression": True, "roundoff": False}
4570
4571
4572class LastDateOfMonth(Func):
4573    pass
4574
4575
4576class Extract(Func):
4577    arg_types = {"this": True, "expression": True}
4578
4579
4580class Timestamp(Func):
4581    arg_types = {"this": False, "expression": False}
4582
4583
4584class TimestampAdd(Func, TimeUnit):
4585    arg_types = {"this": True, "expression": True, "unit": False}
4586
4587
4588class TimestampSub(Func, TimeUnit):
4589    arg_types = {"this": True, "expression": True, "unit": False}
4590
4591
4592class TimestampDiff(Func, TimeUnit):
4593    arg_types = {"this": True, "expression": True, "unit": False}
4594
4595
4596class TimestampTrunc(Func, TimeUnit):
4597    arg_types = {"this": True, "unit": True, "zone": False}
4598
4599
4600class TimeAdd(Func, TimeUnit):
4601    arg_types = {"this": True, "expression": True, "unit": False}
4602
4603
4604class TimeSub(Func, TimeUnit):
4605    arg_types = {"this": True, "expression": True, "unit": False}
4606
4607
4608class TimeDiff(Func, TimeUnit):
4609    arg_types = {"this": True, "expression": True, "unit": False}
4610
4611
4612class TimeTrunc(Func, TimeUnit):
4613    arg_types = {"this": True, "unit": True, "zone": False}
4614
4615
4616class DateFromParts(Func):
4617    _sql_names = ["DATEFROMPARTS"]
4618    arg_types = {"year": True, "month": True, "day": True}
4619
4620
4621class DateStrToDate(Func):
4622    pass
4623
4624
4625class DateToDateStr(Func):
4626    pass
4627
4628
4629class DateToDi(Func):
4630    pass
4631
4632
4633# https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date
4634class Date(Func):
4635    arg_types = {"this": False, "zone": False, "expressions": False}
4636    is_var_len_args = True
4637
4638
4639class Day(Func):
4640    pass
4641
4642
4643class Decode(Func):
4644    arg_types = {"this": True, "charset": True, "replace": False}
4645
4646
4647class DiToDate(Func):
4648    pass
4649
4650
4651class Encode(Func):
4652    arg_types = {"this": True, "charset": True}
4653
4654
4655class Exp(Func):
4656    pass
4657
4658
4659# https://docs.snowflake.com/en/sql-reference/functions/flatten
4660class Explode(Func):
4661    arg_types = {"this": True, "expressions": False}
4662    is_var_len_args = True
4663
4664
4665class ExplodeOuter(Explode):
4666    pass
4667
4668
4669class Posexplode(Explode):
4670    pass
4671
4672
4673class PosexplodeOuter(Posexplode):
4674    pass
4675
4676
4677class Floor(Func):
4678    arg_types = {"this": True, "decimals": False}
4679
4680
4681class FromBase64(Func):
4682    pass
4683
4684
4685class ToBase64(Func):
4686    pass
4687
4688
4689class Greatest(Func):
4690    arg_types = {"this": True, "expressions": False}
4691    is_var_len_args = True
4692
4693
4694class GroupConcat(AggFunc):
4695    arg_types = {"this": True, "separator": False}
4696
4697
4698class Hex(Func):
4699    pass
4700
4701
4702class Xor(Connector, Func):
4703    arg_types = {"this": False, "expression": False, "expressions": False}
4704
4705
4706class If(Func):
4707    arg_types = {"this": True, "true": True, "false": False}
4708
4709
4710class Nullif(Func):
4711    arg_types = {"this": True, "expression": True}
4712
4713
4714class Initcap(Func):
4715    arg_types = {"this": True, "expression": False}
4716
4717
4718class IsNan(Func):
4719    _sql_names = ["IS_NAN", "ISNAN"]
4720
4721
4722class IsInf(Func):
4723    _sql_names = ["IS_INF", "ISINF"]
4724
4725
4726class FormatJson(Expression):
4727    pass
4728
4729
4730class JSONKeyValue(Expression):
4731    arg_types = {"this": True, "expression": True}
4732
4733
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    }
4742
4743
4744# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAY.html
4745class JSONArray(Func):
4746    arg_types = {
4747        "expressions": True,
4748        "null_handling": False,
4749        "return_type": False,
4750        "strict": False,
4751    }
4752
4753
4754# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAYAGG.html
4755class JSONArrayAgg(Func):
4756    arg_types = {
4757        "this": True,
4758        "order": False,
4759        "null_handling": False,
4760        "return_type": False,
4761        "strict": False,
4762    }
4763
4764
4765# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_TABLE.html
4766# Note: parsing of JSON column definitions is currently incomplete.
4767class JSONColumnDef(Expression):
4768    arg_types = {"this": False, "kind": False, "path": False, "nested_schema": False}
4769
4770
4771class JSONSchema(Expression):
4772    arg_types = {"expressions": True}
4773
4774
4775# # https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_TABLE.html
4776class JSONTable(Func):
4777    arg_types = {
4778        "this": True,
4779        "schema": True,
4780        "path": False,
4781        "error_handling": False,
4782        "empty_handling": False,
4783    }
4784
4785
4786class OpenJSONColumnDef(Expression):
4787    arg_types = {"this": True, "kind": True, "path": False, "as_json": False}
4788
4789
4790class OpenJSON(Func):
4791    arg_types = {"this": True, "path": False, "expressions": False}
4792
4793
4794class JSONBContains(Binary):
4795    _sql_names = ["JSONB_CONTAINS"]
4796
4797
4798class JSONExtract(Binary, Func):
4799    _sql_names = ["JSON_EXTRACT"]
4800
4801
4802class JSONExtractScalar(JSONExtract):
4803    _sql_names = ["JSON_EXTRACT_SCALAR"]
4804
4805
4806class JSONBExtract(JSONExtract):
4807    _sql_names = ["JSONB_EXTRACT"]
4808
4809
4810class JSONBExtractScalar(JSONExtract):
4811    _sql_names = ["JSONB_EXTRACT_SCALAR"]
4812
4813
4814class JSONFormat(Func):
4815    arg_types = {"this": False, "options": False}
4816    _sql_names = ["JSON_FORMAT"]
4817
4818
4819# https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_member-of
4820class JSONArrayContains(Binary, Predicate, Func):
4821    _sql_names = ["JSON_ARRAY_CONTAINS"]
4822
4823
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
4829
4830
4831class Least(Func):
4832    arg_types = {"this": True, "expressions": False}
4833    is_var_len_args = True
4834
4835
4836class Left(Func):
4837    arg_types = {"this": True, "expression": True}
4838
4839
4840class Right(Func):
4841    arg_types = {"this": True, "expression": True}
4842
4843
4844class Length(Func):
4845    _sql_names = ["LENGTH", "LEN"]
4846
4847
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    }
4856
4857
4858class Ln(Func):
4859    pass
4860
4861
4862class Log(Func):
4863    arg_types = {"this": True, "expression": False}
4864
4865
4866class Log2(Func):
4867    pass
4868
4869
4870class Log10(Func):
4871    pass
4872
4873
4874class LogicalOr(AggFunc):
4875    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
4876
4877
4878class LogicalAnd(AggFunc):
4879    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
4880
4881
4882class Lower(Func):
4883    _sql_names = ["LOWER", "LCASE"]
4884
4885
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 []
4898
4899
4900class MapFromEntries(Func):
4901    pass
4902
4903
4904class StarMap(Func):
4905    pass
4906
4907
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
4919
4920
4921# https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html
4922class MatchAgainst(Func):
4923    arg_types = {"this": True, "expressions": True, "modifier": False}
4924
4925
4926class Max(AggFunc):
4927    arg_types = {"this": True, "expressions": False}
4928    is_var_len_args = True
4929
4930
4931class MD5(Func):
4932    _sql_names = ["MD5"]
4933
4934
4935# Represents the variant of the MD5 function that returns a binary value
4936class MD5Digest(Func):
4937    _sql_names = ["MD5_DIGEST"]
4938
4939
4940class Min(AggFunc):
4941    arg_types = {"this": True, "expressions": False}
4942    is_var_len_args = True
4943
4944
4945class Month(Func):
4946    pass
4947
4948
4949class Nvl2(Func):
4950    arg_types = {"this": True, "true": True, "false": False}
4951
4952
4953# https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-predict#mlpredict_function
4954class Predict(Func):
4955    arg_types = {"this": True, "expression": True, "params_struct": False}
4956
4957
4958class Pow(Binary, Func):
4959    _sql_names = ["POWER", "POW"]
4960
4961
4962class PercentileCont(AggFunc):
4963    arg_types = {"this": True, "expression": False}
4964
4965
4966class PercentileDisc(AggFunc):
4967    arg_types = {"this": True, "expression": False}
4968
4969
4970class Quantile(AggFunc):
4971    arg_types = {"this": True, "quantile": True}
4972
4973
4974class ApproxQuantile(Quantile):
4975    arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
4976
4977
4978class RangeN(Func):
4979    arg_types = {"this": True, "expressions": True, "each": False}
4980
4981
4982class ReadCSV(Func):
4983    _sql_names = ["READ_CSV"]
4984    is_var_len_args = True
4985    arg_types = {"this": True, "expressions": False}
4986
4987
4988class Reduce(Func):
4989    arg_types = {"this": True, "initial": True, "merge": True, "finish": False}
4990
4991
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    }
5001
5002
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    }
5013
5014
5015class RegexpLike(Binary, Func):
5016    arg_types = {"this": True, "expression": True, "flag": False}
5017
5018
5019class RegexpILike(Binary, Func):
5020    arg_types = {"this": True, "expression": True, "flag": False}
5021
5022
5023# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split.html
5024# limit is the number of times a pattern is applied
5025class RegexpSplit(Func):
5026    arg_types = {"this": True, "expression": True, "limit": False}
5027
5028
5029class Repeat(Func):
5030    arg_types = {"this": True, "times": True}
5031
5032
5033class Round(Func):
5034    arg_types = {"this": True, "decimals": False}
5035
5036
5037class RowNumber(Func):
5038    arg_types: t.Dict[str, t.Any] = {}
5039
5040
5041class SafeDivide(Func):
5042    arg_types = {"this": True, "expression": True}
5043
5044
5045class SHA(Func):
5046    _sql_names = ["SHA", "SHA1"]
5047
5048
5049class SHA2(Func):
5050    _sql_names = ["SHA2"]
5051    arg_types = {"this": True, "length": False}
5052
5053
5054class SortArray(Func):
5055    arg_types = {"this": True, "asc": False}
5056
5057
5058class Split(Func):
5059    arg_types = {"this": True, "expression": True, "limit": False}
5060
5061
5062# Start may be omitted in the case of postgres
5063# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6
5064class Substring(Func):
5065    arg_types = {"this": True, "start": False, "length": False}
5066
5067
5068class StandardHash(Func):
5069    arg_types = {"this": True, "expression": False}
5070
5071
5072class StartsWith(Func):
5073    _sql_names = ["STARTS_WITH", "STARTSWITH"]
5074    arg_types = {"this": True, "expression": True}
5075
5076
5077class StrPosition(Func):
5078    arg_types = {
5079        "this": True,
5080        "substr": True,
5081        "position": False,
5082        "instance": False,
5083    }
5084
5085
5086class StrToDate(Func):
5087    arg_types = {"this": True, "format": True}
5088
5089
5090class StrToTime(Func):
5091    arg_types = {"this": True, "format": True, "zone": False}
5092
5093
5094# Spark allows unix_timestamp()
5095# https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.unix_timestamp.html
5096class StrToUnix(Func):
5097    arg_types = {"this": False, "format": False}
5098
5099
5100# https://prestodb.io/docs/current/functions/string.html
5101# https://spark.apache.org/docs/latest/api/sql/index.html#str_to_map
5102class StrToMap(Func):
5103    arg_types = {
5104        "this": True,
5105        "pair_delim": False,
5106        "key_value_delim": False,
5107        "duplicate_resolution_callback": False,
5108    }
5109
5110
5111class NumberToStr(Func):
5112    arg_types = {"this": True, "format": True, "culture": False}
5113
5114
5115class FromBase(Func):
5116    arg_types = {"this": True, "expression": True}
5117
5118
5119class Struct(Func):
5120    arg_types = {"expressions": False}
5121    is_var_len_args = True
5122
5123
5124class StructExtract(Func):
5125    arg_types = {"this": True, "expression": True}
5126
5127
5128# https://learn.microsoft.com/en-us/sql/t-sql/functions/stuff-transact-sql?view=sql-server-ver16
5129# https://docs.snowflake.com/en/sql-reference/functions/insert
5130class Stuff(Func):
5131    _sql_names = ["STUFF", "INSERT"]
5132    arg_types = {"this": True, "start": True, "length": True, "expression": True}
5133
5134
5135class Sum(AggFunc):
5136    pass
5137
5138
5139class Sqrt(Func):
5140    pass
5141
5142
5143class Stddev(AggFunc):
5144    pass
5145
5146
5147class StddevPop(AggFunc):
5148    pass
5149
5150
5151class StddevSamp(AggFunc):
5152    pass
5153
5154
5155class TimeToStr(Func):
5156    arg_types = {"this": True, "format": True, "culture": False}
5157
5158
5159class TimeToTimeStr(Func):
5160    pass
5161
5162
5163class TimeToUnix(Func):
5164    pass
5165
5166
5167class TimeStrToDate(Func):
5168    pass
5169
5170
5171class TimeStrToTime(Func):
5172    pass
5173
5174
5175class TimeStrToUnix(Func):
5176    pass
5177
5178
5179class Trim(Func):
5180    arg_types = {
5181        "this": True,
5182        "expression": False,
5183        "position": False,
5184        "collation": False,
5185    }
5186
5187
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)
5195
5196
5197class TsOrDsDiff(Func, TimeUnit):
5198    arg_types = {"this": True, "expression": True, "unit": False}
5199
5200
5201class TsOrDsToDateStr(Func):
5202    pass
5203
5204
5205class TsOrDsToDate(Func):
5206    arg_types = {"this": True, "format": False}
5207
5208
5209class TsOrDiToDi(Func):
5210    pass
5211
5212
5213class Unhex(Func):
5214    pass
5215
5216
5217class UnixToStr(Func):
5218    arg_types = {"this": True, "format": False}
5219
5220
5221# https://prestodb.io/docs/current/functions/datetime.html
5222# presto has weird zone/hours/minutes
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")
5230
5231
5232class UnixToTimeStr(Func):
5233    pass
5234
5235
5236class Upper(Func):
5237    _sql_names = ["UPPER", "UCASE"]
5238
5239
5240class Variance(AggFunc):
5241    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
5242
5243
5244class VariancePop(AggFunc):
5245    _sql_names = ["VARIANCE_POP", "VAR_POP"]
5246
5247
5248class Week(Func):
5249    arg_types = {"this": True, "mode": False}
5250
5251
5252class XMLTable(Func):
5253    arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False}
5254
5255
5256class Year(Func):
5257    pass
5258
5259
5260class Use(Expression):
5261    arg_types = {"this": True, "kind": False}
5262
5263
5264class Merge(Expression):
5265    arg_types = {"this": True, "using": True, "on": True, "expressions": True, "with": False}
5266
5267
5268class When(Func):
5269    arg_types = {"matched": True, "source": False, "condition": False, "then": True}
5270
5271
5272# https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqljnextvaluefor.html
5273# https://learn.microsoft.com/en-us/sql/t-sql/functions/next-value-for-transact-sql?view=sql-server-ver16
5274class NextValueFor(Func):
5275    arg_types = {"this": True, "order": False}
5276
5277
5278def _norm_arg(arg):
5279    return arg.lower() if type(arg) is str else arg
5280
5281
5282ALL_FUNCTIONS = subclasses(__name__, Func, (AggFunc, Anonymous, Func))
5283FUNCTION_BY_NAME = {name: func for func in ALL_FUNCTIONS for name in func.sql_names()}
5284
5285
5286# Helpers
5287@t.overload
5288def maybe_parse(
5289    sql_or_expression: ExpOrStr,
5290    *,
5291    into: t.Type[E],
5292    dialect: DialectType = None,
5293    prefix: t.Optional[str] = None,
5294    copy: bool = False,
5295    **opts,
5296) -> E:
5297    ...
5298
5299
5300@t.overload
5301def maybe_parse(
5302    sql_or_expression: str | E,
5303    *,
5304    into: t.Optional[IntoType] = None,
5305    dialect: DialectType = None,
5306    prefix: t.Optional[str] = None,
5307    copy: bool = False,
5308    **opts,
5309) -> E:
5310    ...
5311
5312
5313def maybe_parse(
5314    sql_or_expression: ExpOrStr,
5315    *,
5316    into: t.Optional[IntoType] = None,
5317    dialect: DialectType = None,
5318    prefix: t.Optional[str] = None,
5319    copy: bool = False,
5320    **opts,
5321) -> Expression:
5322    """Gracefully handle a possible string or expression.
5323
5324    Example:
5325        >>> maybe_parse("1")
5326        (LITERAL this: 1, is_string: False)
5327        >>> maybe_parse(to_identifier("x"))
5328        (IDENTIFIER this: x, quoted: False)
5329
5330    Args:
5331        sql_or_expression: the SQL code string or an expression
5332        into: the SQLGlot Expression to parse into
5333        dialect: the dialect used to parse the input expressions (in the case that an
5334            input expression is a SQL string).
5335        prefix: a string to prefix the sql with before it gets parsed
5336            (automatically includes a space)
5337        copy: whether or not to copy the expression.
5338        **opts: other options to use to parse the input expressions (again, in the case
5339            that an input expression is a SQL string).
5340
5341    Returns:
5342        Expression: the parsed or given expression.
5343    """
5344    if isinstance(sql_or_expression, Expression):
5345        if copy:
5346            return sql_or_expression.copy()
5347        return sql_or_expression
5348
5349    if sql_or_expression is None:
5350        raise ParseError(f"SQL cannot be None")
5351
5352    import sqlglot
5353
5354    sql = str(sql_or_expression)
5355    if prefix:
5356        sql = f"{prefix} {sql}"
5357
5358    return sqlglot.parse_one(sql, read=dialect, into=into, **opts)
5359
5360
5361@t.overload
5362def maybe_copy(instance: None, copy: bool = True) -> None:
5363    ...
5364
5365
5366@t.overload
5367def maybe_copy(instance: E, copy: bool = True) -> E:
5368    ...
5369
5370
5371def maybe_copy(instance, copy=True):
5372    return instance.copy() if copy and instance else instance
5373
5374
5375def _is_wrong_expression(expression, into):
5376    return isinstance(expression, Expression) and not isinstance(expression, into)
5377
5378
5379def _apply_builder(
5380    expression,
5381    instance,
5382    arg,
5383    copy=True,
5384    prefix=None,
5385    into=None,
5386    dialect=None,
5387    into_arg="this",
5388    **opts,
5389):
5390    if _is_wrong_expression(expression, into):
5391        expression = into(**{into_arg: expression})
5392    instance = maybe_copy(instance, copy)
5393    expression = maybe_parse(
5394        sql_or_expression=expression,
5395        prefix=prefix,
5396        into=into,
5397        dialect=dialect,
5398        **opts,
5399    )
5400    instance.set(arg, expression)
5401    return instance
5402
5403
5404def _apply_child_list_builder(
5405    *expressions,
5406    instance,
5407    arg,
5408    append=True,
5409    copy=True,
5410    prefix=None,
5411    into=None,
5412    dialect=None,
5413    properties=None,
5414    **opts,
5415):
5416    instance = maybe_copy(instance, copy)
5417    parsed = []
5418    for expression in expressions:
5419        if expression is not None:
5420            if _is_wrong_expression(expression, into):
5421                expression = into(expressions=[expression])
5422
5423            expression = maybe_parse(
5424                expression,
5425                into=into,
5426                dialect=dialect,
5427                prefix=prefix,
5428                **opts,
5429            )
5430            parsed.extend(expression.expressions)
5431
5432    existing = instance.args.get(arg)
5433    if append and existing:
5434        parsed = existing.expressions + parsed
5435
5436    child = into(expressions=parsed)
5437    for k, v in (properties or {}).items():
5438        child.set(k, v)
5439    instance.set(arg, child)
5440
5441    return instance
5442
5443
5444def _apply_list_builder(
5445    *expressions,
5446    instance,
5447    arg,
5448    append=True,
5449    copy=True,
5450    prefix=None,
5451    into=None,
5452    dialect=None,
5453    **opts,
5454):
5455    inst = maybe_copy(instance, copy)
5456
5457    expressions = [
5458        maybe_parse(
5459            sql_or_expression=expression,
5460            into=into,
5461            prefix=prefix,
5462            dialect=dialect,
5463            **opts,
5464        )
5465        for expression in expressions
5466        if expression is not None
5467    ]
5468
5469    existing_expressions = inst.args.get(arg)
5470    if append and existing_expressions:
5471        expressions = existing_expressions + expressions
5472
5473    inst.set(arg, expressions)
5474    return inst
5475
5476
5477def _apply_conjunction_builder(
5478    *expressions,
5479    instance,
5480    arg,
5481    into=None,
5482    append=True,
5483    copy=True,
5484    dialect=None,
5485    **opts,
5486):
5487    expressions = [exp for exp in expressions if exp is not None and exp != ""]
5488    if not expressions:
5489        return instance
5490
5491    inst = maybe_copy(instance, copy)
5492
5493    existing = inst.args.get(arg)
5494    if append and existing is not None:
5495        expressions = [existing.this if into else existing] + list(expressions)
5496
5497    node = and_(*expressions, dialect=dialect, copy=copy, **opts)
5498
5499    inst.set(arg, into(this=node) if into else node)
5500    return inst
5501
5502
5503def _apply_cte_builder(
5504    instance: E,
5505    alias: ExpOrStr,
5506    as_: ExpOrStr,
5507    recursive: t.Optional[bool] = None,
5508    append: bool = True,
5509    dialect: DialectType = None,
5510    copy: bool = True,
5511    **opts,
5512) -> E:
5513    alias_expression = maybe_parse(alias, dialect=dialect, into=TableAlias, **opts)
5514    as_expression = maybe_parse(as_, dialect=dialect, **opts)
5515    cte = CTE(this=as_expression, alias=alias_expression)
5516    return _apply_child_list_builder(
5517        cte,
5518        instance=instance,
5519        arg="with",
5520        append=append,
5521        copy=copy,
5522        into=With,
5523        properties={"recursive": recursive or False},
5524    )
5525
5526
5527def _combine(
5528    expressions: t.Sequence[t.Optional[ExpOrStr]],
5529    operator: t.Type[Connector],
5530    dialect: DialectType = None,
5531    copy: bool = True,
5532    **opts,
5533) -> Expression:
5534    conditions = [
5535        condition(expression, dialect=dialect, copy=copy, **opts)
5536        for expression in expressions
5537        if expression is not None
5538    ]
5539
5540    this, *rest = conditions
5541    if rest:
5542        this = _wrap(this, Connector)
5543    for expression in rest:
5544        this = operator(this=this, expression=_wrap(expression, Connector))
5545
5546    return this
5547
5548
5549def _wrap(expression: E, kind: t.Type[Expression]) -> E | Paren:
5550    return Paren(this=expression) if isinstance(expression, kind) else expression
5551
5552
5553def union(
5554    left: ExpOrStr,
5555    right: ExpOrStr,
5556    distinct: bool = True,
5557    dialect: DialectType = None,
5558    copy: bool = True,
5559    **opts,
5560) -> Union:
5561    """
5562    Initializes a syntax tree from one UNION expression.
5563
5564    Example:
5565        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
5566        'SELECT * FROM foo UNION SELECT * FROM bla'
5567
5568    Args:
5569        left: the SQL code string corresponding to the left-hand side.
5570            If an `Expression` instance is passed, it will be used as-is.
5571        right: the SQL code string corresponding to the right-hand side.
5572            If an `Expression` instance is passed, it will be used as-is.
5573        distinct: set the DISTINCT flag if and only if this is true.
5574        dialect: the dialect used to parse the input expression.
5575        copy: whether or not to copy the expression.
5576        opts: other options to use to parse the input expressions.
5577
5578    Returns:
5579        The new Union instance.
5580    """
5581    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5582    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5583
5584    return Union(this=left, expression=right, distinct=distinct)
5585
5586
5587def intersect(
5588    left: ExpOrStr,
5589    right: ExpOrStr,
5590    distinct: bool = True,
5591    dialect: DialectType = None,
5592    copy: bool = True,
5593    **opts,
5594) -> Intersect:
5595    """
5596    Initializes a syntax tree from one INTERSECT expression.
5597
5598    Example:
5599        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
5600        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
5601
5602    Args:
5603        left: the SQL code string corresponding to the left-hand side.
5604            If an `Expression` instance is passed, it will be used as-is.
5605        right: the SQL code string corresponding to the right-hand side.
5606            If an `Expression` instance is passed, it will be used as-is.
5607        distinct: set the DISTINCT flag if and only if this is true.
5608        dialect: the dialect used to parse the input expression.
5609        copy: whether or not to copy the expression.
5610        opts: other options to use to parse the input expressions.
5611
5612    Returns:
5613        The new Intersect instance.
5614    """
5615    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5616    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5617
5618    return Intersect(this=left, expression=right, distinct=distinct)
5619
5620
5621def except_(
5622    left: ExpOrStr,
5623    right: ExpOrStr,
5624    distinct: bool = True,
5625    dialect: DialectType = None,
5626    copy: bool = True,
5627    **opts,
5628) -> Except:
5629    """
5630    Initializes a syntax tree from one EXCEPT expression.
5631
5632    Example:
5633        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
5634        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
5635
5636    Args:
5637        left: the SQL code string corresponding to the left-hand side.
5638            If an `Expression` instance is passed, it will be used as-is.
5639        right: the SQL code string corresponding to the right-hand side.
5640            If an `Expression` instance is passed, it will be used as-is.
5641        distinct: set the DISTINCT flag if and only if this is true.
5642        dialect: the dialect used to parse the input expression.
5643        copy: whether or not to copy the expression.
5644        opts: other options to use to parse the input expressions.
5645
5646    Returns:
5647        The new Except instance.
5648    """
5649    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5650    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5651
5652    return Except(this=left, expression=right, distinct=distinct)
5653
5654
5655def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5656    """
5657    Initializes a syntax tree from one or multiple SELECT expressions.
5658
5659    Example:
5660        >>> select("col1", "col2").from_("tbl").sql()
5661        'SELECT col1, col2 FROM tbl'
5662
5663    Args:
5664        *expressions: the SQL code string to parse as the expressions of a
5665            SELECT statement. If an Expression instance is passed, this is used as-is.
5666        dialect: the dialect used to parse the input expressions (in the case that an
5667            input expression is a SQL string).
5668        **opts: other options to use to parse the input expressions (again, in the case
5669            that an input expression is a SQL string).
5670
5671    Returns:
5672        Select: the syntax tree for the SELECT statement.
5673    """
5674    return Select().select(*expressions, dialect=dialect, **opts)
5675
5676
5677def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5678    """
5679    Initializes a syntax tree from a FROM expression.
5680
5681    Example:
5682        >>> from_("tbl").select("col1", "col2").sql()
5683        'SELECT col1, col2 FROM tbl'
5684
5685    Args:
5686        *expression: the SQL code string to parse as the FROM expressions of a
5687            SELECT statement. If an Expression instance is passed, this is used as-is.
5688        dialect: the dialect used to parse the input expression (in the case that the
5689            input expression is a SQL string).
5690        **opts: other options to use to parse the input expressions (again, in the case
5691            that the input expression is a SQL string).
5692
5693    Returns:
5694        Select: the syntax tree for the SELECT statement.
5695    """
5696    return Select().from_(expression, dialect=dialect, **opts)
5697
5698
5699def update(
5700    table: str | Table,
5701    properties: dict,
5702    where: t.Optional[ExpOrStr] = None,
5703    from_: t.Optional[ExpOrStr] = None,
5704    dialect: DialectType = None,
5705    **opts,
5706) -> Update:
5707    """
5708    Creates an update statement.
5709
5710    Example:
5711        >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql()
5712        "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1"
5713
5714    Args:
5715        *properties: dictionary of properties to set which are
5716            auto converted to sql objects eg None -> NULL
5717        where: sql conditional parsed into a WHERE statement
5718        from_: sql statement parsed into a FROM statement
5719        dialect: the dialect used to parse the input expressions.
5720        **opts: other options to use to parse the input expressions.
5721
5722    Returns:
5723        Update: the syntax tree for the UPDATE statement.
5724    """
5725    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
5726    update_expr.set(
5727        "expressions",
5728        [
5729            EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
5730            for k, v in properties.items()
5731        ],
5732    )
5733    if from_:
5734        update_expr.set(
5735            "from",
5736            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
5737        )
5738    if isinstance(where, Condition):
5739        where = Where(this=where)
5740    if where:
5741        update_expr.set(
5742            "where",
5743            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
5744        )
5745    return update_expr
5746
5747
5748def delete(
5749    table: ExpOrStr,
5750    where: t.Optional[ExpOrStr] = None,
5751    returning: t.Optional[ExpOrStr] = None,
5752    dialect: DialectType = None,
5753    **opts,
5754) -> Delete:
5755    """
5756    Builds a delete statement.
5757
5758    Example:
5759        >>> delete("my_table", where="id > 1").sql()
5760        'DELETE FROM my_table WHERE id > 1'
5761
5762    Args:
5763        where: sql conditional parsed into a WHERE statement
5764        returning: sql conditional parsed into a RETURNING statement
5765        dialect: the dialect used to parse the input expressions.
5766        **opts: other options to use to parse the input expressions.
5767
5768    Returns:
5769        Delete: the syntax tree for the DELETE statement.
5770    """
5771    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
5772    if where:
5773        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
5774    if returning:
5775        delete_expr = t.cast(
5776            Delete, delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
5777        )
5778    return delete_expr
5779
5780
5781def insert(
5782    expression: ExpOrStr,
5783    into: ExpOrStr,
5784    columns: t.Optional[t.Sequence[ExpOrStr]] = None,
5785    overwrite: t.Optional[bool] = None,
5786    returning: t.Optional[ExpOrStr] = None,
5787    dialect: DialectType = None,
5788    copy: bool = True,
5789    **opts,
5790) -> Insert:
5791    """
5792    Builds an INSERT statement.
5793
5794    Example:
5795        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
5796        'INSERT INTO tbl VALUES (1, 2, 3)'
5797
5798    Args:
5799        expression: the sql string or expression of the INSERT statement
5800        into: the tbl to insert data to.
5801        columns: optionally the table's column names.
5802        overwrite: whether to INSERT OVERWRITE or not.
5803        returning: sql conditional parsed into a RETURNING statement
5804        dialect: the dialect used to parse the input expressions.
5805        copy: whether or not to copy the expression.
5806        **opts: other options to use to parse the input expressions.
5807
5808    Returns:
5809        Insert: the syntax tree for the INSERT statement.
5810    """
5811    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
5812    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
5813
5814    if columns:
5815        this = _apply_list_builder(
5816            *columns,
5817            instance=Schema(this=this),
5818            arg="expressions",
5819            into=Identifier,
5820            copy=False,
5821            dialect=dialect,
5822            **opts,
5823        )
5824
5825    insert = Insert(this=this, expression=expr, overwrite=overwrite)
5826
5827    if returning:
5828        insert = t.cast(Insert, insert.returning(returning, dialect=dialect, copy=False, **opts))
5829
5830    return insert
5831
5832
5833def condition(
5834    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
5835) -> Condition:
5836    """
5837    Initialize a logical condition expression.
5838
5839    Example:
5840        >>> condition("x=1").sql()
5841        'x = 1'
5842
5843        This is helpful for composing larger logical syntax trees:
5844        >>> where = condition("x=1")
5845        >>> where = where.and_("y=1")
5846        >>> Select().from_("tbl").select("*").where(where).sql()
5847        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
5848
5849    Args:
5850        *expression: the SQL code string to parse.
5851            If an Expression instance is passed, this is used as-is.
5852        dialect: the dialect used to parse the input expression (in the case that the
5853            input expression is a SQL string).
5854        copy: Whether or not to copy `expression` (only applies to expressions).
5855        **opts: other options to use to parse the input expressions (again, in the case
5856            that the input expression is a SQL string).
5857
5858    Returns:
5859        The new Condition instance
5860    """
5861    return maybe_parse(
5862        expression,
5863        into=Condition,
5864        dialect=dialect,
5865        copy=copy,
5866        **opts,
5867    )
5868
5869
5870def and_(
5871    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5872) -> Condition:
5873    """
5874    Combine multiple conditions with an AND logical operator.
5875
5876    Example:
5877        >>> and_("x=1", and_("y=1", "z=1")).sql()
5878        'x = 1 AND (y = 1 AND z = 1)'
5879
5880    Args:
5881        *expressions: the SQL code strings to parse.
5882            If an Expression instance is passed, this is used as-is.
5883        dialect: the dialect used to parse the input expression.
5884        copy: whether or not to copy `expressions` (only applies to Expressions).
5885        **opts: other options to use to parse the input expressions.
5886
5887    Returns:
5888        And: the new condition
5889    """
5890    return t.cast(Condition, _combine(expressions, And, dialect, copy=copy, **opts))
5891
5892
5893def or_(
5894    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5895) -> Condition:
5896    """
5897    Combine multiple conditions with an OR logical operator.
5898
5899    Example:
5900        >>> or_("x=1", or_("y=1", "z=1")).sql()
5901        'x = 1 OR (y = 1 OR z = 1)'
5902
5903    Args:
5904        *expressions: the SQL code strings to parse.
5905            If an Expression instance is passed, this is used as-is.
5906        dialect: the dialect used to parse the input expression.
5907        copy: whether or not to copy `expressions` (only applies to Expressions).
5908        **opts: other options to use to parse the input expressions.
5909
5910    Returns:
5911        Or: the new condition
5912    """
5913    return t.cast(Condition, _combine(expressions, Or, dialect, copy=copy, **opts))
5914
5915
5916def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
5917    """
5918    Wrap a condition with a NOT operator.
5919
5920    Example:
5921        >>> not_("this_suit='black'").sql()
5922        "NOT this_suit = 'black'"
5923
5924    Args:
5925        expression: the SQL code string to parse.
5926            If an Expression instance is passed, this is used as-is.
5927        dialect: the dialect used to parse the input expression.
5928        copy: whether to copy the expression or not.
5929        **opts: other options to use to parse the input expressions.
5930
5931    Returns:
5932        The new condition.
5933    """
5934    this = condition(
5935        expression,
5936        dialect=dialect,
5937        copy=copy,
5938        **opts,
5939    )
5940    return Not(this=_wrap(this, Connector))
5941
5942
5943def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
5944    """
5945    Wrap an expression in parentheses.
5946
5947    Example:
5948        >>> paren("5 + 3").sql()
5949        '(5 + 3)'
5950
5951    Args:
5952        expression: the SQL code string to parse.
5953            If an Expression instance is passed, this is used as-is.
5954        copy: whether to copy the expression or not.
5955
5956    Returns:
5957        The wrapped expression.
5958    """
5959    return Paren(this=maybe_parse(expression, copy=copy))
5960
5961
5962SAFE_IDENTIFIER_RE = re.compile(r"^[_a-zA-Z][\w]*$")
5963
5964
5965@t.overload
5966def to_identifier(name: None, quoted: t.Optional[bool] = None, copy: bool = True) -> None:
5967    ...
5968
5969
5970@t.overload
5971def to_identifier(
5972    name: str | Identifier, quoted: t.Optional[bool] = None, copy: bool = True
5973) -> Identifier:
5974    ...
5975
5976
5977def to_identifier(name, quoted=None, copy=True):
5978    """Builds an identifier.
5979
5980    Args:
5981        name: The name to turn into an identifier.
5982        quoted: Whether or not force quote the identifier.
5983        copy: Whether or not to copy name if it's an Identifier.
5984
5985    Returns:
5986        The identifier ast node.
5987    """
5988
5989    if name is None:
5990        return None
5991
5992    if isinstance(name, Identifier):
5993        identifier = maybe_copy(name, copy)
5994    elif isinstance(name, str):
5995        identifier = Identifier(
5996            this=name,
5997            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
5998        )
5999    else:
6000        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
6001    return identifier
6002
6003
6004def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
6005    """
6006    Parses a given string into an identifier.
6007
6008    Args:
6009        name: The name to parse into an identifier.
6010        dialect: The dialect to parse against.
6011
6012    Returns:
6013        The identifier ast node.
6014    """
6015    try:
6016        expression = maybe_parse(name, dialect=dialect, into=Identifier)
6017    except ParseError:
6018        expression = to_identifier(name)
6019
6020    return expression
6021
6022
6023INTERVAL_STRING_RE = re.compile(r"\s*([0-9]+)\s*([a-zA-Z]+)\s*")
6024
6025
6026def to_interval(interval: str | Literal) -> Interval:
6027    """Builds an interval expression from a string like '1 day' or '5 months'."""
6028    if isinstance(interval, Literal):
6029        if not interval.is_string:
6030            raise ValueError("Invalid interval string.")
6031
6032        interval = interval.this
6033
6034    interval_parts = INTERVAL_STRING_RE.match(interval)  # type: ignore
6035
6036    if not interval_parts:
6037        raise ValueError("Invalid interval string.")
6038
6039    return Interval(
6040        this=Literal.string(interval_parts.group(1)),
6041        unit=Var(this=interval_parts.group(2)),
6042    )
6043
6044
6045@t.overload
6046def to_table(sql_path: str | Table, **kwargs) -> Table:
6047    ...
6048
6049
6050@t.overload
6051def to_table(sql_path: None, **kwargs) -> None:
6052    ...
6053
6054
6055def to_table(
6056    sql_path: t.Optional[str | Table], dialect: DialectType = None, copy: bool = True, **kwargs
6057) -> t.Optional[Table]:
6058    """
6059    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
6060    If a table is passed in then that table is returned.
6061
6062    Args:
6063        sql_path: a `[catalog].[schema].[table]` string.
6064        dialect: the source dialect according to which the table name will be parsed.
6065        copy: Whether or not to copy a table if it is passed in.
6066        kwargs: the kwargs to instantiate the resulting `Table` expression with.
6067
6068    Returns:
6069        A table expression.
6070    """
6071    if sql_path is None or isinstance(sql_path, Table):
6072        return maybe_copy(sql_path, copy=copy)
6073    if not isinstance(sql_path, str):
6074        raise ValueError(f"Invalid type provided for a table: {type(sql_path)}")
6075
6076    table = maybe_parse(sql_path, into=Table, dialect=dialect)
6077    if table:
6078        for k, v in kwargs.items():
6079            table.set(k, v)
6080
6081    return table
6082
6083
6084def to_column(sql_path: str | Column, **kwargs) -> Column:
6085    """
6086    Create a column from a `[table].[column]` sql path. Schema is optional.
6087
6088    If a column is passed in then that column is returned.
6089
6090    Args:
6091        sql_path: `[table].[column]` string
6092    Returns:
6093        Table: A column expression
6094    """
6095    if sql_path is None or isinstance(sql_path, Column):
6096        return sql_path
6097    if not isinstance(sql_path, str):
6098        raise ValueError(f"Invalid type provided for column: {type(sql_path)}")
6099    return column(*reversed(sql_path.split(".")), **kwargs)  # type: ignore
6100
6101
6102def alias_(
6103    expression: ExpOrStr,
6104    alias: str | Identifier,
6105    table: bool | t.Sequence[str | Identifier] = False,
6106    quoted: t.Optional[bool] = None,
6107    dialect: DialectType = None,
6108    copy: bool = True,
6109    **opts,
6110):
6111    """Create an Alias expression.
6112
6113    Example:
6114        >>> alias_('foo', 'bar').sql()
6115        'foo AS bar'
6116
6117        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
6118        '(SELECT 1, 2) AS bar(a, b)'
6119
6120    Args:
6121        expression: the SQL code strings to parse.
6122            If an Expression instance is passed, this is used as-is.
6123        alias: the alias name to use. If the name has
6124            special characters it is quoted.
6125        table: Whether or not to create a table alias, can also be a list of columns.
6126        quoted: whether or not to quote the alias
6127        dialect: the dialect used to parse the input expression.
6128        copy: Whether or not to copy the expression.
6129        **opts: other options to use to parse the input expressions.
6130
6131    Returns:
6132        Alias: the aliased expression
6133    """
6134    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
6135    alias = to_identifier(alias, quoted=quoted)
6136
6137    if table:
6138        table_alias = TableAlias(this=alias)
6139        exp.set("alias", table_alias)
6140
6141        if not isinstance(table, bool):
6142            for column in table:
6143                table_alias.append("columns", to_identifier(column, quoted=quoted))
6144
6145        return exp
6146
6147    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
6148    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
6149    # for the complete Window expression.
6150    #
6151    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
6152
6153    if "alias" in exp.arg_types and not isinstance(exp, Window):
6154        exp.set("alias", alias)
6155        return exp
6156    return Alias(this=exp, alias=alias)
6157
6158
6159def subquery(
6160    expression: ExpOrStr,
6161    alias: t.Optional[Identifier | str] = None,
6162    dialect: DialectType = None,
6163    **opts,
6164) -> Select:
6165    """
6166    Build a subquery expression.
6167
6168    Example:
6169        >>> subquery('select x from tbl', 'bar').select('x').sql()
6170        'SELECT x FROM (SELECT x FROM tbl) AS bar'
6171
6172    Args:
6173        expression: the SQL code strings to parse.
6174            If an Expression instance is passed, this is used as-is.
6175        alias: the alias name to use.
6176        dialect: the dialect used to parse the input expression.
6177        **opts: other options to use to parse the input expressions.
6178
6179    Returns:
6180        A new Select instance with the subquery expression included.
6181    """
6182
6183    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias)
6184    return Select().from_(expression, dialect=dialect, **opts)
6185
6186
6187def column(
6188    col: str | Identifier,
6189    table: t.Optional[str | Identifier] = None,
6190    db: t.Optional[str | Identifier] = None,
6191    catalog: t.Optional[str | Identifier] = None,
6192    quoted: t.Optional[bool] = None,
6193) -> Column:
6194    """
6195    Build a Column.
6196
6197    Args:
6198        col: Column name.
6199        table: Table name.
6200        db: Database name.
6201        catalog: Catalog name.
6202        quoted: Whether to force quotes on the column's identifiers.
6203
6204    Returns:
6205        The new Column instance.
6206    """
6207    return Column(
6208        this=to_identifier(col, quoted=quoted),
6209        table=to_identifier(table, quoted=quoted),
6210        db=to_identifier(db, quoted=quoted),
6211        catalog=to_identifier(catalog, quoted=quoted),
6212    )
6213
6214
6215def cast(expression: ExpOrStr, to: DATA_TYPE, **opts) -> Cast:
6216    """Cast an expression to a data type.
6217
6218    Example:
6219        >>> cast('x + 1', 'int').sql()
6220        'CAST(x + 1 AS INT)'
6221
6222    Args:
6223        expression: The expression to cast.
6224        to: The datatype to cast to.
6225
6226    Returns:
6227        The new Cast instance.
6228    """
6229    expression = maybe_parse(expression, **opts)
6230    data_type = DataType.build(to, **opts)
6231    expression = Cast(this=expression, to=data_type)
6232    expression.type = data_type
6233    return expression
6234
6235
6236def table_(
6237    table: Identifier | str,
6238    db: t.Optional[Identifier | str] = None,
6239    catalog: t.Optional[Identifier | str] = None,
6240    quoted: t.Optional[bool] = None,
6241    alias: t.Optional[Identifier | str] = None,
6242) -> Table:
6243    """Build a Table.
6244
6245    Args:
6246        table: Table name.
6247        db: Database name.
6248        catalog: Catalog name.
6249        quote: Whether to force quotes on the table's identifiers.
6250        alias: Table's alias.
6251
6252    Returns:
6253        The new Table instance.
6254    """
6255    return Table(
6256        this=to_identifier(table, quoted=quoted) if table else None,
6257        db=to_identifier(db, quoted=quoted) if db else None,
6258        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
6259        alias=TableAlias(this=to_identifier(alias)) if alias else None,
6260    )
6261
6262
6263def values(
6264    values: t.Iterable[t.Tuple[t.Any, ...]],
6265    alias: t.Optional[str] = None,
6266    columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None,
6267) -> Values:
6268    """Build VALUES statement.
6269
6270    Example:
6271        >>> values([(1, '2')]).sql()
6272        "VALUES (1, '2')"
6273
6274    Args:
6275        values: values statements that will be converted to SQL
6276        alias: optional alias
6277        columns: Optional list of ordered column names or ordered dictionary of column names to types.
6278         If either are provided then an alias is also required.
6279
6280    Returns:
6281        Values: the Values expression object
6282    """
6283    if columns and not alias:
6284        raise ValueError("Alias is required when providing columns")
6285
6286    return Values(
6287        expressions=[convert(tup) for tup in values],
6288        alias=(
6289            TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns])
6290            if columns
6291            else (TableAlias(this=to_identifier(alias)) if alias else None)
6292        ),
6293    )
6294
6295
6296def var(name: t.Optional[ExpOrStr]) -> Var:
6297    """Build a SQL variable.
6298
6299    Example:
6300        >>> repr(var('x'))
6301        '(VAR this: x)'
6302
6303        >>> repr(var(column('x', table='y')))
6304        '(VAR this: x)'
6305
6306    Args:
6307        name: The name of the var or an expression who's name will become the var.
6308
6309    Returns:
6310        The new variable node.
6311    """
6312    if not name:
6313        raise ValueError("Cannot convert empty name into var.")
6314
6315    if isinstance(name, Expression):
6316        name = name.name
6317    return Var(this=name)
6318
6319
6320def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable:
6321    """Build ALTER TABLE... RENAME... expression
6322
6323    Args:
6324        old_name: The old name of the table
6325        new_name: The new name of the table
6326
6327    Returns:
6328        Alter table expression
6329    """
6330    old_table = to_table(old_name)
6331    new_table = to_table(new_name)
6332    return AlterTable(
6333        this=old_table,
6334        actions=[
6335            RenameTable(this=new_table),
6336        ],
6337    )
6338
6339
6340def convert(value: t.Any, copy: bool = False) -> Expression:
6341    """Convert a python value into an expression object.
6342
6343    Raises an error if a conversion is not possible.
6344
6345    Args:
6346        value: A python object.
6347        copy: Whether or not to copy `value` (only applies to Expressions and collections).
6348
6349    Returns:
6350        Expression: the equivalent expression object.
6351    """
6352    if isinstance(value, Expression):
6353        return maybe_copy(value, copy)
6354    if isinstance(value, str):
6355        return Literal.string(value)
6356    if isinstance(value, bool):
6357        return Boolean(this=value)
6358    if value is None or (isinstance(value, float) and math.isnan(value)):
6359        return NULL
6360    if isinstance(value, numbers.Number):
6361        return Literal.number(value)
6362    if isinstance(value, datetime.datetime):
6363        datetime_literal = Literal.string(
6364            (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat()
6365        )
6366        return TimeStrToTime(this=datetime_literal)
6367    if isinstance(value, datetime.date):
6368        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
6369        return DateStrToDate(this=date_literal)
6370    if isinstance(value, tuple):
6371        return Tuple(expressions=[convert(v, copy=copy) for v in value])
6372    if isinstance(value, list):
6373        return Array(expressions=[convert(v, copy=copy) for v in value])
6374    if isinstance(value, dict):
6375        return Map(
6376            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
6377            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
6378        )
6379    raise ValueError(f"Cannot convert {value}")
6380
6381
6382def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
6383    """
6384    Replace children of an expression with the result of a lambda fun(child) -> exp.
6385    """
6386    for k, v in expression.args.items():
6387        is_list_arg = type(v) is list
6388
6389        child_nodes = v if is_list_arg else [v]
6390        new_child_nodes = []
6391
6392        for cn in child_nodes:
6393            if isinstance(cn, Expression):
6394                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
6395                    new_child_nodes.append(child_node)
6396                    child_node.parent = expression
6397                    child_node.arg_key = k
6398            else:
6399                new_child_nodes.append(cn)
6400
6401        expression.args[k] = new_child_nodes if is_list_arg else seq_get(new_child_nodes, 0)
6402
6403
6404def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
6405    """
6406    Return all table names referenced through columns in an expression.
6407
6408    Example:
6409        >>> import sqlglot
6410        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
6411        ['a', 'c']
6412
6413    Args:
6414        expression: expression to find table names.
6415        exclude: a table name to exclude
6416
6417    Returns:
6418        A list of unique names.
6419    """
6420    return {
6421        table
6422        for table in (column.table for column in expression.find_all(Column))
6423        if table and table != exclude
6424    }
6425
6426
6427def table_name(table: Table | str, dialect: DialectType = None, identify: bool = False) -> str:
6428    """Get the full name of a table as a string.
6429
6430    Args:
6431        table: Table expression node or string.
6432        dialect: The dialect to generate the table name for.
6433        identify: Determines when an identifier should be quoted. Possible values are:
6434            False (default): Never quote, except in cases where it's mandatory by the dialect.
6435            True: Always quote.
6436
6437    Examples:
6438        >>> from sqlglot import exp, parse_one
6439        >>> table_name(parse_one("select * from a.b.c").find(exp.Table))
6440        'a.b.c'
6441
6442    Returns:
6443        The table name.
6444    """
6445
6446    table = maybe_parse(table, into=Table, dialect=dialect)
6447
6448    if not table:
6449        raise ValueError(f"Cannot parse {table}")
6450
6451    return ".".join(
6452        part.sql(dialect=dialect, identify=True)
6453        if identify or not SAFE_IDENTIFIER_RE.match(part.name)
6454        else part.name
6455        for part in table.parts
6456    )
6457
6458
6459def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
6460    """Returns a case normalized table name without quotes.
6461
6462    Args:
6463        table: the table to normalize
6464        dialect: the dialect to use for normalization rules
6465        copy: whether or not to copy the expression.
6466
6467    Examples:
6468        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
6469        'A-B.c'
6470    """
6471    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
6472
6473    return ".".join(
6474        p.name
6475        for p in normalize_identifiers(
6476            to_table(table, dialect=dialect, copy=copy), dialect=dialect
6477        ).parts
6478    )
6479
6480
6481def replace_tables(
6482    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
6483) -> E:
6484    """Replace all tables in expression according to the mapping.
6485
6486    Args:
6487        expression: expression node to be transformed and replaced.
6488        mapping: mapping of table names.
6489        dialect: the dialect of the mapping table
6490        copy: whether or not to copy the expression.
6491
6492    Examples:
6493        >>> from sqlglot import exp, parse_one
6494        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
6495        'SELECT * FROM c'
6496
6497    Returns:
6498        The mapped expression.
6499    """
6500
6501    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
6502
6503    def _replace_tables(node: Expression) -> Expression:
6504        if isinstance(node, Table):
6505            new_name = mapping.get(normalize_table_name(node, dialect=dialect))
6506
6507            if new_name:
6508                return to_table(
6509                    new_name,
6510                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
6511                )
6512        return node
6513
6514    return expression.transform(_replace_tables, copy=copy)
6515
6516
6517def replace_placeholders(expression: Expression, *args, **kwargs) -> Expression:
6518    """Replace placeholders in an expression.
6519
6520    Args:
6521        expression: expression node to be transformed and replaced.
6522        args: positional names that will substitute unnamed placeholders in the given order.
6523        kwargs: keyword arguments that will substitute named placeholders.
6524
6525    Examples:
6526        >>> from sqlglot import exp, parse_one
6527        >>> replace_placeholders(
6528        ...     parse_one("select * from :tbl where ? = ?"),
6529        ...     exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo")
6530        ... ).sql()
6531        "SELECT * FROM foo WHERE str_col = 'b'"
6532
6533    Returns:
6534        The mapped expression.
6535    """
6536
6537    def _replace_placeholders(node: Expression, args, **kwargs) -> Expression:
6538        if isinstance(node, Placeholder):
6539            if node.name:
6540                new_name = kwargs.get(node.name)
6541                if new_name:
6542                    return convert(new_name)
6543            else:
6544                try:
6545                    return convert(next(args))
6546                except StopIteration:
6547                    pass
6548        return node
6549
6550    return expression.transform(_replace_placeholders, iter(args), **kwargs)
6551
6552
6553def expand(
6554    expression: Expression,
6555    sources: t.Dict[str, Subqueryable],
6556    dialect: DialectType = None,
6557    copy: bool = True,
6558) -> Expression:
6559    """Transforms an expression by expanding all referenced sources into subqueries.
6560
6561    Examples:
6562        >>> from sqlglot import parse_one
6563        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql()
6564        'SELECT * FROM (SELECT * FROM y) AS z /* source: x */'
6565
6566        >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql()
6567        'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */'
6568
6569    Args:
6570        expression: The expression to expand.
6571        sources: A dictionary of name to Subqueryables.
6572        dialect: The dialect of the sources dict.
6573        copy: Whether or not to copy the expression during transformation. Defaults to True.
6574
6575    Returns:
6576        The transformed expression.
6577    """
6578    sources = {normalize_table_name(k, dialect=dialect): v for k, v in sources.items()}
6579
6580    def _expand(node: Expression):
6581        if isinstance(node, Table):
6582            name = normalize_table_name(node, dialect=dialect)
6583            source = sources.get(name)
6584            if source:
6585                subquery = source.subquery(node.alias or name)
6586                subquery.comments = [f"source: {name}"]
6587                return subquery.transform(_expand, copy=False)
6588        return node
6589
6590    return expression.transform(_expand, copy=copy)
6591
6592
6593def func(name: str, *args, copy: bool = True, dialect: DialectType = None, **kwargs) -> Func:
6594    """
6595    Returns a Func expression.
6596
6597    Examples:
6598        >>> func("abs", 5).sql()
6599        'ABS(5)'
6600
6601        >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql()
6602        'CAST(5 AS DOUBLE)'
6603
6604    Args:
6605        name: the name of the function to build.
6606        args: the args used to instantiate the function of interest.
6607        copy: whether or not to copy the argument expressions.
6608        dialect: the source dialect.
6609        kwargs: the kwargs used to instantiate the function of interest.
6610
6611    Note:
6612        The arguments `args` and `kwargs` are mutually exclusive.
6613
6614    Returns:
6615        An instance of the function of interest, or an anonymous function, if `name` doesn't
6616        correspond to an existing `sqlglot.expressions.Func` class.
6617    """
6618    if args and kwargs:
6619        raise ValueError("Can't use both args and kwargs to instantiate a function.")
6620
6621    from sqlglot.dialects.dialect import Dialect
6622
6623    dialect = Dialect.get_or_raise(dialect)
6624
6625    converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect, copy=copy) for arg in args]
6626    kwargs = {key: maybe_parse(value, dialect=dialect, copy=copy) for key, value in kwargs.items()}
6627
6628    constructor = dialect.parser_class.FUNCTIONS.get(name.upper())
6629    if constructor:
6630        if converted:
6631            if "dialect" in constructor.__code__.co_varnames:
6632                function = constructor(converted, dialect=dialect)
6633            else:
6634                function = constructor(converted)
6635        elif constructor.__name__ == "from_arg_list":
6636            function = constructor.__self__(**kwargs)  # type: ignore
6637        else:
6638            constructor = FUNCTION_BY_NAME.get(name.upper())
6639            if constructor:
6640                function = constructor(**kwargs)
6641            else:
6642                raise ValueError(
6643                    f"Unable to convert '{name}' into a Func. Either manually construct "
6644                    "the Func expression of interest or parse the function call."
6645                )
6646    else:
6647        kwargs = kwargs or {"expressions": converted}
6648        function = Anonymous(this=name, **kwargs)
6649
6650    for error_message in function.error_messages(converted):
6651        raise ValueError(error_message)
6652
6653    return function
6654
6655
6656def case(
6657    expression: t.Optional[ExpOrStr] = None,
6658    **opts,
6659) -> Case:
6660    """
6661    Initialize a CASE statement.
6662
6663    Example:
6664        case().when("a = 1", "foo").else_("bar")
6665
6666    Args:
6667        expression: Optionally, the input expression (not all dialects support this)
6668        **opts: Extra keyword arguments for parsing `expression`
6669    """
6670    if expression is not None:
6671        this = maybe_parse(expression, **opts)
6672    else:
6673        this = None
6674    return Case(this=this, ifs=[])
6675
6676
6677def true() -> Boolean:
6678    """
6679    Returns a true Boolean expression.
6680    """
6681    return Boolean(this=True)
6682
6683
6684def false() -> Boolean:
6685    """
6686    Returns a false Boolean expression.
6687    """
6688    return Boolean(this=False)
6689
6690
6691def null() -> Null:
6692    """
6693    Returns a Null expression.
6694    """
6695    return Null()
6696
6697
6698# TODO: deprecate this
6699TRUE = Boolean(this=True)
6700FALSE = Boolean(this=False)
6701NULL = 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 = {
1110        "this": True,
1111        "when": False,
1112        "kind": False,
1113        "shallow": False,
1114        "expression": False,
1115        "copy": False,
1116    }
arg_types = {'this': True, 'when': False, 'kind': False, 'shallow': False, 'expression': False, 'copy': False}
key = 'clone'
class Describe(Expression):
1119class Describe(Expression):
1120    arg_types = {"this": True, "kind": False, "expressions": False}
arg_types = {'this': True, 'kind': False, 'expressions': False}
key = 'describe'
class Kill(Expression):
1123class Kill(Expression):
1124    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'kill'
class Pragma(Expression):
1127class Pragma(Expression):
1128    pass
key = 'pragma'
class Set(Expression):
1131class Set(Expression):
1132    arg_types = {"expressions": False, "unset": False, "tag": False}
arg_types = {'expressions': False, 'unset': False, 'tag': False}
key = 'set'
class SetItem(Expression):
1135class SetItem(Expression):
1136    arg_types = {
1137        "this": False,
1138        "expressions": False,
1139        "kind": False,
1140        "collate": False,  # MySQL SET NAMES statement
1141        "global": False,
1142    }
arg_types = {'this': False, 'expressions': False, 'kind': False, 'collate': False, 'global': False}
key = 'setitem'
class Show(Expression):
1145class Show(Expression):
1146    arg_types = {
1147        "this": True,
1148        "target": False,
1149        "offset": False,
1150        "limit": False,
1151        "like": False,
1152        "where": False,
1153        "db": False,
1154        "scope": False,
1155        "scope_kind": False,
1156        "full": False,
1157        "mutex": False,
1158        "query": False,
1159        "channel": False,
1160        "global": False,
1161        "log": False,
1162        "position": False,
1163        "types": False,
1164    }
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):
1167class UserDefinedFunction(Expression):
1168    arg_types = {"this": True, "expressions": False, "wrapped": False}
arg_types = {'this': True, 'expressions': False, 'wrapped': False}
key = 'userdefinedfunction'
class CharacterSet(Expression):
1171class CharacterSet(Expression):
1172    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'characterset'
class With(Expression):
1175class With(Expression):
1176    arg_types = {"expressions": True, "recursive": False}
1177
1178    @property
1179    def recursive(self) -> bool:
1180        return bool(self.args.get("recursive"))
arg_types = {'expressions': True, 'recursive': False}
recursive: bool
key = 'with'
class WithinGroup(Expression):
1183class WithinGroup(Expression):
1184    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'withingroup'
class CTE(DerivedTable):
1189class CTE(DerivedTable):
1190    arg_types = {"this": True, "alias": True, "scalar": False}
arg_types = {'this': True, 'alias': True, 'scalar': False}
key = 'cte'
class TableAlias(Expression):
1193class TableAlias(Expression):
1194    arg_types = {"this": False, "columns": False}
1195
1196    @property
1197    def columns(self):
1198        return self.args.get("columns") or []
arg_types = {'this': False, 'columns': False}
columns
key = 'tablealias'
class BitString(Condition):
1201class BitString(Condition):
1202    pass
key = 'bitstring'
class HexString(Condition):
1205class HexString(Condition):
1206    pass
key = 'hexstring'
class ByteString(Condition):
1209class ByteString(Condition):
1210    pass
key = 'bytestring'
class RawString(Condition):
1213class RawString(Condition):
1214    pass
key = 'rawstring'
class Column(Condition):
1217class Column(Condition):
1218    arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False}
1219
1220    @property
1221    def table(self) -> str:
1222        return self.text("table")
1223
1224    @property
1225    def db(self) -> str:
1226        return self.text("db")
1227
1228    @property
1229    def catalog(self) -> str:
1230        return self.text("catalog")
1231
1232    @property
1233    def output_name(self) -> str:
1234        return self.name
1235
1236    @property
1237    def parts(self) -> t.List[Identifier]:
1238        """Return the parts of a column in order catalog, db, table, name."""
1239        return [
1240            t.cast(Identifier, self.args[part])
1241            for part in ("catalog", "db", "table", "this")
1242            if self.args.get(part)
1243        ]
1244
1245    def to_dot(self) -> Dot | Identifier:
1246        """Converts the column into a dot expression."""
1247        parts = self.parts
1248        parent = self.parent
1249
1250        while parent:
1251            if isinstance(parent, Dot):
1252                parts.append(parent.expression)
1253            parent = parent.parent
1254
1255        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:
1245    def to_dot(self) -> Dot | Identifier:
1246        """Converts the column into a dot expression."""
1247        parts = self.parts
1248        parent = self.parent
1249
1250        while parent:
1251            if isinstance(parent, Dot):
1252                parts.append(parent.expression)
1253            parent = parent.parent
1254
1255        return Dot.build(deepcopy(parts)) if len(parts) > 1 else parts[0]

Converts the column into a dot expression.

key = 'column'
class ColumnPosition(Expression):
1258class ColumnPosition(Expression):
1259    arg_types = {"this": False, "position": True}
arg_types = {'this': False, 'position': True}
key = 'columnposition'
class ColumnDef(Expression):
1262class ColumnDef(Expression):
1263    arg_types = {
1264        "this": True,
1265        "kind": False,
1266        "constraints": False,
1267        "exists": False,
1268        "position": False,
1269    }
1270
1271    @property
1272    def constraints(self) -> t.List[ColumnConstraint]:
1273        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):
1276class AlterColumn(Expression):
1277    arg_types = {
1278        "this": True,
1279        "dtype": False,
1280        "collate": False,
1281        "using": False,
1282        "default": False,
1283        "drop": False,
1284    }
arg_types = {'this': True, 'dtype': False, 'collate': False, 'using': False, 'default': False, 'drop': False}
key = 'altercolumn'
class RenameTable(Expression):
1287class RenameTable(Expression):
1288    pass
key = 'renametable'
class SwapTable(Expression):
1291class SwapTable(Expression):
1292    pass
key = 'swaptable'
class Comment(Expression):
1295class Comment(Expression):
1296    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):
1299class Comprehension(Expression):
1300    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):
1304class MergeTreeTTLAction(Expression):
1305    arg_types = {
1306        "this": True,
1307        "delete": False,
1308        "recompress": False,
1309        "to_disk": False,
1310        "to_volume": False,
1311    }
arg_types = {'this': True, 'delete': False, 'recompress': False, 'to_disk': False, 'to_volume': False}
key = 'mergetreettlaction'
class MergeTreeTTL(Expression):
1315class MergeTreeTTL(Expression):
1316    arg_types = {
1317        "expressions": True,
1318        "where": False,
1319        "group": False,
1320        "aggregates": False,
1321    }
arg_types = {'expressions': True, 'where': False, 'group': False, 'aggregates': False}
key = 'mergetreettl'
class IndexConstraintOption(Expression):
1325class IndexConstraintOption(Expression):
1326    arg_types = {
1327        "key_block_size": False,
1328        "using": False,
1329        "parser": False,
1330        "comment": False,
1331        "visible": False,
1332        "engine_attr": False,
1333        "secondary_engine_attr": False,
1334    }
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):
1337class ColumnConstraint(Expression):
1338    arg_types = {"this": False, "kind": True}
1339
1340    @property
1341    def kind(self) -> ColumnConstraintKind:
1342        return self.args["kind"]
arg_types = {'this': False, 'kind': True}
key = 'columnconstraint'
class ColumnConstraintKind(Expression):
1345class ColumnConstraintKind(Expression):
1346    pass
key = 'columnconstraintkind'
class AutoIncrementColumnConstraint(ColumnConstraintKind):
1349class AutoIncrementColumnConstraint(ColumnConstraintKind):
1350    pass
key = 'autoincrementcolumnconstraint'
class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1353class PeriodForSystemTimeConstraint(ColumnConstraintKind):
1354    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'periodforsystemtimeconstraint'
class CaseSpecificColumnConstraint(ColumnConstraintKind):
1357class CaseSpecificColumnConstraint(ColumnConstraintKind):
1358    arg_types = {"not_": True}
arg_types = {'not_': True}
key = 'casespecificcolumnconstraint'
class CharacterSetColumnConstraint(ColumnConstraintKind):
1361class CharacterSetColumnConstraint(ColumnConstraintKind):
1362    arg_types = {"this": True}
arg_types = {'this': True}
key = 'charactersetcolumnconstraint'
class CheckColumnConstraint(ColumnConstraintKind):
1365class CheckColumnConstraint(ColumnConstraintKind):
1366    pass
key = 'checkcolumnconstraint'
class ClusteredColumnConstraint(ColumnConstraintKind):
1369class ClusteredColumnConstraint(ColumnConstraintKind):
1370    pass
key = 'clusteredcolumnconstraint'
class CollateColumnConstraint(ColumnConstraintKind):
1373class CollateColumnConstraint(ColumnConstraintKind):
1374    pass
key = 'collatecolumnconstraint'
class CommentColumnConstraint(ColumnConstraintKind):
1377class CommentColumnConstraint(ColumnConstraintKind):
1378    pass
key = 'commentcolumnconstraint'
class CompressColumnConstraint(ColumnConstraintKind):
1381class CompressColumnConstraint(ColumnConstraintKind):
1382    pass
key = 'compresscolumnconstraint'
class DateFormatColumnConstraint(ColumnConstraintKind):
1385class DateFormatColumnConstraint(ColumnConstraintKind):
1386    arg_types = {"this": True}
arg_types = {'this': True}
key = 'dateformatcolumnconstraint'
class DefaultColumnConstraint(ColumnConstraintKind):
1389class DefaultColumnConstraint(ColumnConstraintKind):
1390    pass
key = 'defaultcolumnconstraint'
class EncodeColumnConstraint(ColumnConstraintKind):
1393class EncodeColumnConstraint(ColumnConstraintKind):
1394    pass
key = 'encodecolumnconstraint'
class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1397class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind):
1398    # this: True -> ALWAYS, this: False -> BY DEFAULT
1399    arg_types = {
1400        "this": False,
1401        "expression": False,
1402        "on_null": False,
1403        "start": False,
1404        "increment": False,
1405        "minvalue": False,
1406        "maxvalue": False,
1407        "cycle": False,
1408    }
arg_types = {'this': False, 'expression': False, 'on_null': False, 'start': False, 'increment': False, 'minvalue': False, 'maxvalue': False, 'cycle': False}
key = 'generatedasidentitycolumnconstraint'
class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1411class GeneratedAsRowColumnConstraint(ColumnConstraintKind):
1412    arg_types = {"start": True, "hidden": False}
arg_types = {'start': True, 'hidden': False}
key = 'generatedasrowcolumnconstraint'
class IndexColumnConstraint(ColumnConstraintKind):
1416class IndexColumnConstraint(ColumnConstraintKind):
1417    arg_types = {
1418        "this": False,
1419        "schema": True,
1420        "kind": False,
1421        "index_type": False,
1422        "options": False,
1423    }
arg_types = {'this': False, 'schema': True, 'kind': False, 'index_type': False, 'options': False}
key = 'indexcolumnconstraint'
class InlineLengthColumnConstraint(ColumnConstraintKind):
1426class InlineLengthColumnConstraint(ColumnConstraintKind):
1427    pass
key = 'inlinelengthcolumnconstraint'
class NonClusteredColumnConstraint(ColumnConstraintKind):
1430class NonClusteredColumnConstraint(ColumnConstraintKind):
1431    pass
key = 'nonclusteredcolumnconstraint'
class NotForReplicationColumnConstraint(ColumnConstraintKind):
1434class NotForReplicationColumnConstraint(ColumnConstraintKind):
1435    arg_types = {}
arg_types = {}
key = 'notforreplicationcolumnconstraint'
class NotNullColumnConstraint(ColumnConstraintKind):
1438class NotNullColumnConstraint(ColumnConstraintKind):
1439    arg_types = {"allow_null": False}
arg_types = {'allow_null': False}
key = 'notnullcolumnconstraint'
class OnUpdateColumnConstraint(ColumnConstraintKind):
1443class OnUpdateColumnConstraint(ColumnConstraintKind):
1444    pass
key = 'onupdatecolumnconstraint'
class TransformColumnConstraint(ColumnConstraintKind):
1448class TransformColumnConstraint(ColumnConstraintKind):
1449    pass
key = 'transformcolumnconstraint'
class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1452class PrimaryKeyColumnConstraint(ColumnConstraintKind):
1453    arg_types = {"desc": False}
arg_types = {'desc': False}
key = 'primarykeycolumnconstraint'
class TitleColumnConstraint(ColumnConstraintKind):
1456class TitleColumnConstraint(ColumnConstraintKind):
1457    pass
key = 'titlecolumnconstraint'
class UniqueColumnConstraint(ColumnConstraintKind):
1460class UniqueColumnConstraint(ColumnConstraintKind):
1461    arg_types = {"this": False, "index_type": False}
arg_types = {'this': False, 'index_type': False}
key = 'uniquecolumnconstraint'
class UppercaseColumnConstraint(ColumnConstraintKind):
1464class UppercaseColumnConstraint(ColumnConstraintKind):
1465    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'uppercasecolumnconstraint'
class PathColumnConstraint(ColumnConstraintKind):
1468class PathColumnConstraint(ColumnConstraintKind):
1469    pass
key = 'pathcolumnconstraint'
class ComputedColumnConstraint(ColumnConstraintKind):
1474class ComputedColumnConstraint(ColumnConstraintKind):
1475    arg_types = {"this": True, "persisted": False, "not_null": False}
arg_types = {'this': True, 'persisted': False, 'not_null': False}
key = 'computedcolumnconstraint'
class Constraint(Expression):
1478class Constraint(Expression):
1479    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'constraint'
class Delete(DML):
1482class Delete(DML):
1483    arg_types = {
1484        "with": False,
1485        "this": False,
1486        "using": False,
1487        "where": False,
1488        "returning": False,
1489        "limit": False,
1490        "tables": False,  # Multiple-Table Syntax (MySQL)
1491    }
1492
1493    def delete(
1494        self,
1495        table: ExpOrStr,
1496        dialect: DialectType = None,
1497        copy: bool = True,
1498        **opts,
1499    ) -> Delete:
1500        """
1501        Create a DELETE expression or replace the table on an existing DELETE expression.
1502
1503        Example:
1504            >>> delete("tbl").sql()
1505            'DELETE FROM tbl'
1506
1507        Args:
1508            table: the table from which to delete.
1509            dialect: the dialect used to parse the input expression.
1510            copy: if `False`, modify this expression instance in-place.
1511            opts: other options to use to parse the input expressions.
1512
1513        Returns:
1514            Delete: the modified expression.
1515        """
1516        return _apply_builder(
1517            expression=table,
1518            instance=self,
1519            arg="this",
1520            dialect=dialect,
1521            into=Table,
1522            copy=copy,
1523            **opts,
1524        )
1525
1526    def where(
1527        self,
1528        *expressions: t.Optional[ExpOrStr],
1529        append: bool = True,
1530        dialect: DialectType = None,
1531        copy: bool = True,
1532        **opts,
1533    ) -> Delete:
1534        """
1535        Append to or set the WHERE expressions.
1536
1537        Example:
1538            >>> delete("tbl").where("x = 'a' OR x < 'b'").sql()
1539            "DELETE FROM tbl WHERE x = 'a' OR x < 'b'"
1540
1541        Args:
1542            *expressions: the SQL code strings to parse.
1543                If an `Expression` instance is passed, it will be used as-is.
1544                Multiple expressions are combined with an AND operator.
1545            append: if `True`, AND the new expressions to any existing expression.
1546                Otherwise, this resets the expression.
1547            dialect: the dialect used to parse the input expressions.
1548            copy: if `False`, modify this expression instance in-place.
1549            opts: other options to use to parse the input expressions.
1550
1551        Returns:
1552            Delete: the modified expression.
1553        """
1554        return _apply_conjunction_builder(
1555            *expressions,
1556            instance=self,
1557            arg="where",
1558            append=append,
1559            into=Where,
1560            dialect=dialect,
1561            copy=copy,
1562            **opts,
1563        )
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:
1493    def delete(
1494        self,
1495        table: ExpOrStr,
1496        dialect: DialectType = None,
1497        copy: bool = True,
1498        **opts,
1499    ) -> Delete:
1500        """
1501        Create a DELETE expression or replace the table on an existing DELETE expression.
1502
1503        Example:
1504            >>> delete("tbl").sql()
1505            'DELETE FROM tbl'
1506
1507        Args:
1508            table: the table from which to delete.
1509            dialect: the dialect used to parse the input expression.
1510            copy: if `False`, modify this expression instance in-place.
1511            opts: other options to use to parse the input expressions.
1512
1513        Returns:
1514            Delete: the modified expression.
1515        """
1516        return _apply_builder(
1517            expression=table,
1518            instance=self,
1519            arg="this",
1520            dialect=dialect,
1521            into=Table,
1522            copy=copy,
1523            **opts,
1524        )

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

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):
1566class Drop(Expression):
1567    arg_types = {
1568        "this": False,
1569        "kind": False,
1570        "exists": False,
1571        "temporary": False,
1572        "materialized": False,
1573        "cascade": False,
1574        "constraints": False,
1575        "purge": False,
1576    }
arg_types = {'this': False, 'kind': False, 'exists': False, 'temporary': False, 'materialized': False, 'cascade': False, 'constraints': False, 'purge': False}
key = 'drop'
class Filter(Expression):
1579class Filter(Expression):
1580    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'filter'
class Check(Expression):
1583class Check(Expression):
1584    pass
key = 'check'
class Connect(Expression):
1588class Connect(Expression):
1589    arg_types = {"start": False, "connect": True}
arg_types = {'start': False, 'connect': True}
key = 'connect'
class Prior(Expression):
1592class Prior(Expression):
1593    pass
key = 'prior'
class Directory(Expression):
1596class Directory(Expression):
1597    # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html
1598    arg_types = {"this": True, "local": False, "row_format": False}
arg_types = {'this': True, 'local': False, 'row_format': False}
key = 'directory'
class ForeignKey(Expression):
1601class ForeignKey(Expression):
1602    arg_types = {
1603        "expressions": True,
1604        "reference": False,
1605        "delete": False,
1606        "update": False,
1607    }
arg_types = {'expressions': True, 'reference': False, 'delete': False, 'update': False}
key = 'foreignkey'
class ColumnPrefix(Expression):
1610class ColumnPrefix(Expression):
1611    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'columnprefix'
class PrimaryKey(Expression):
1614class PrimaryKey(Expression):
1615    arg_types = {"expressions": True, "options": False}
arg_types = {'expressions': True, 'options': False}
key = 'primarykey'
class Into(Expression):
1620class Into(Expression):
1621    arg_types = {"this": True, "temporary": False, "unlogged": False}
arg_types = {'this': True, 'temporary': False, 'unlogged': False}
key = 'into'
class From(Expression):
1624class From(Expression):
1625    @property
1626    def name(self) -> str:
1627        return self.this.name
1628
1629    @property
1630    def alias_or_name(self) -> str:
1631        return self.this.alias_or_name
name: str
alias_or_name: str
key = 'from'
class Having(Expression):
1634class Having(Expression):
1635    pass
key = 'having'
class Hint(Expression):
1638class Hint(Expression):
1639    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'hint'
class JoinHint(Expression):
1642class JoinHint(Expression):
1643    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'joinhint'
class Identifier(Expression):
1646class Identifier(Expression):
1647    arg_types = {"this": True, "quoted": False, "global": False, "temporary": False}
1648
1649    @property
1650    def quoted(self) -> bool:
1651        return bool(self.args.get("quoted"))
1652
1653    @property
1654    def hashable_args(self) -> t.Any:
1655        return (self.this, self.quoted)
1656
1657    @property
1658    def output_name(self) -> str:
1659        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):
1663class Opclass(Expression):
1664    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'opclass'
class Index(Expression):
1667class Index(Expression):
1668    arg_types = {
1669        "this": False,
1670        "table": False,
1671        "using": False,
1672        "where": False,
1673        "columns": False,
1674        "unique": False,
1675        "primary": False,
1676        "amp": False,  # teradata
1677        "partition_by": False,  # teradata
1678        "where": False,  # postgres partial indexes
1679    }
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):
1682class Insert(DDL, DML):
1683    arg_types = {
1684        "with": False,
1685        "this": True,
1686        "expression": False,
1687        "conflict": False,
1688        "returning": False,
1689        "overwrite": False,
1690        "exists": False,
1691        "partition": False,
1692        "alternative": False,
1693        "where": False,
1694        "ignore": False,
1695        "by_name": False,
1696    }
1697
1698    def with_(
1699        self,
1700        alias: ExpOrStr,
1701        as_: ExpOrStr,
1702        recursive: t.Optional[bool] = None,
1703        append: bool = True,
1704        dialect: DialectType = None,
1705        copy: bool = True,
1706        **opts,
1707    ) -> Insert:
1708        """
1709        Append to or set the common table expressions.
1710
1711        Example:
1712            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
1713            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
1714
1715        Args:
1716            alias: the SQL code string to parse as the table name.
1717                If an `Expression` instance is passed, this is used as-is.
1718            as_: the SQL code string to parse as the table expression.
1719                If an `Expression` instance is passed, it will be used as-is.
1720            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1721            append: if `True`, add to any existing expressions.
1722                Otherwise, this resets the expressions.
1723            dialect: the dialect used to parse the input expression.
1724            copy: if `False`, modify this expression instance in-place.
1725            opts: other options to use to parse the input expressions.
1726
1727        Returns:
1728            The modified expression.
1729        """
1730        return _apply_cte_builder(
1731            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
1732        )
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:
1698    def with_(
1699        self,
1700        alias: ExpOrStr,
1701        as_: ExpOrStr,
1702        recursive: t.Optional[bool] = None,
1703        append: bool = True,
1704        dialect: DialectType = None,
1705        copy: bool = True,
1706        **opts,
1707    ) -> Insert:
1708        """
1709        Append to or set the common table expressions.
1710
1711        Example:
1712            >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql()
1713            'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte'
1714
1715        Args:
1716            alias: the SQL code string to parse as the table name.
1717                If an `Expression` instance is passed, this is used as-is.
1718            as_: the SQL code string to parse as the table expression.
1719                If an `Expression` instance is passed, it will be used as-is.
1720            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
1721            append: if `True`, add to any existing expressions.
1722                Otherwise, this resets the expressions.
1723            dialect: the dialect used to parse the input expression.
1724            copy: if `False`, modify this expression instance in-place.
1725            opts: other options to use to parse the input expressions.
1726
1727        Returns:
1728            The modified expression.
1729        """
1730        return _apply_cte_builder(
1731            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
1732        )

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):
1735class OnConflict(Expression):
1736    arg_types = {
1737        "duplicate": False,
1738        "expressions": False,
1739        "nothing": False,
1740        "key": False,
1741        "constraint": False,
1742    }
arg_types = {'duplicate': False, 'expressions': False, 'nothing': False, 'key': False, 'constraint': False}
key = 'onconflict'
class Returning(Expression):
1745class Returning(Expression):
1746    arg_types = {"expressions": True, "into": False}
arg_types = {'expressions': True, 'into': False}
key = 'returning'
class Introducer(Expression):
1750class Introducer(Expression):
1751    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'introducer'
class National(Expression):
1755class National(Expression):
1756    pass
key = 'national'
class LoadData(Expression):
1759class LoadData(Expression):
1760    arg_types = {
1761        "this": True,
1762        "local": False,
1763        "overwrite": False,
1764        "inpath": True,
1765        "partition": False,
1766        "input_format": False,
1767        "serde": False,
1768    }
arg_types = {'this': True, 'local': False, 'overwrite': False, 'inpath': True, 'partition': False, 'input_format': False, 'serde': False}
key = 'loaddata'
class Partition(Expression):
1771class Partition(Expression):
1772    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'partition'
class Fetch(Expression):
1775class Fetch(Expression):
1776    arg_types = {
1777        "direction": False,
1778        "count": False,
1779        "percent": False,
1780        "with_ties": False,
1781    }
arg_types = {'direction': False, 'count': False, 'percent': False, 'with_ties': False}
key = 'fetch'
class Group(Expression):
1784class Group(Expression):
1785    arg_types = {
1786        "expressions": False,
1787        "grouping_sets": False,
1788        "cube": False,
1789        "rollup": False,
1790        "totals": False,
1791        "all": False,
1792    }
arg_types = {'expressions': False, 'grouping_sets': False, 'cube': False, 'rollup': False, 'totals': False, 'all': False}
key = 'group'
class Lambda(Expression):
1795class Lambda(Expression):
1796    arg_types = {"this": True, "expressions": True}
arg_types = {'this': True, 'expressions': True}
key = 'lambda'
class Limit(Expression):
1799class Limit(Expression):
1800    arg_types = {"this": False, "expression": True, "offset": False}
arg_types = {'this': False, 'expression': True, 'offset': False}
key = 'limit'
class Literal(Condition):
1803class Literal(Condition):
1804    arg_types = {"this": True, "is_string": True}
1805
1806    @property
1807    def hashable_args(self) -> t.Any:
1808        return (self.this, self.args.get("is_string"))
1809
1810    @classmethod
1811    def number(cls, number) -> Literal:
1812        return cls(this=str(number), is_string=False)
1813
1814    @classmethod
1815    def string(cls, string) -> Literal:
1816        return cls(this=str(string), is_string=True)
1817
1818    @property
1819    def output_name(self) -> str:
1820        return self.name
arg_types = {'this': True, 'is_string': True}
hashable_args: Any
@classmethod
def number(cls, number) -> Literal:
1810    @classmethod
1811    def number(cls, number) -> Literal:
1812        return cls(this=str(number), is_string=False)
@classmethod
def string(cls, string) -> Literal:
1814    @classmethod
1815    def string(cls, string) -> Literal:
1816        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):
1823class Join(Expression):
1824    arg_types = {
1825        "this": True,
1826        "on": False,
1827        "side": False,
1828        "kind": False,
1829        "using": False,
1830        "method": False,
1831        "global": False,
1832        "hint": False,
1833    }
1834
1835    @property
1836    def method(self) -> str:
1837        return self.text("method").upper()
1838
1839    @property
1840    def kind(self) -> str:
1841        return self.text("kind").upper()
1842
1843    @property
1844    def side(self) -> str:
1845        return self.text("side").upper()
1846
1847    @property
1848    def hint(self) -> str:
1849        return self.text("hint").upper()
1850
1851    @property
1852    def alias_or_name(self) -> str:
1853        return self.this.alias_or_name
1854
1855    def on(
1856        self,
1857        *expressions: t.Optional[ExpOrStr],
1858        append: bool = True,
1859        dialect: DialectType = None,
1860        copy: bool = True,
1861        **opts,
1862    ) -> Join:
1863        """
1864        Append to or set the ON expressions.
1865
1866        Example:
1867            >>> import sqlglot
1868            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
1869            'JOIN x ON y = 1'
1870
1871        Args:
1872            *expressions: the SQL code strings to parse.
1873                If an `Expression` instance is passed, it will be used as-is.
1874                Multiple expressions are combined with an AND operator.
1875            append: if `True`, AND the new expressions to any existing expression.
1876                Otherwise, this resets the expression.
1877            dialect: the dialect used to parse the input expressions.
1878            copy: if `False`, modify this expression instance in-place.
1879            opts: other options to use to parse the input expressions.
1880
1881        Returns:
1882            The modified Join expression.
1883        """
1884        join = _apply_conjunction_builder(
1885            *expressions,
1886            instance=self,
1887            arg="on",
1888            append=append,
1889            dialect=dialect,
1890            copy=copy,
1891            **opts,
1892        )
1893
1894        if join.kind == "CROSS":
1895            join.set("kind", None)
1896
1897        return join
1898
1899    def using(
1900        self,
1901        *expressions: t.Optional[ExpOrStr],
1902        append: bool = True,
1903        dialect: DialectType = None,
1904        copy: bool = True,
1905        **opts,
1906    ) -> Join:
1907        """
1908        Append to or set the USING expressions.
1909
1910        Example:
1911            >>> import sqlglot
1912            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
1913            'JOIN x USING (foo, bla)'
1914
1915        Args:
1916            *expressions: the SQL code strings to parse.
1917                If an `Expression` instance is passed, it will be used as-is.
1918            append: if `True`, concatenate the new expressions to the existing "using" list.
1919                Otherwise, this resets the expression.
1920            dialect: the dialect used to parse the input expressions.
1921            copy: if `False`, modify this expression instance in-place.
1922            opts: other options to use to parse the input expressions.
1923
1924        Returns:
1925            The modified Join expression.
1926        """
1927        join = _apply_list_builder(
1928            *expressions,
1929            instance=self,
1930            arg="using",
1931            append=append,
1932            dialect=dialect,
1933            copy=copy,
1934            **opts,
1935        )
1936
1937        if join.kind == "CROSS":
1938            join.set("kind", None)
1939
1940        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:
1855    def on(
1856        self,
1857        *expressions: t.Optional[ExpOrStr],
1858        append: bool = True,
1859        dialect: DialectType = None,
1860        copy: bool = True,
1861        **opts,
1862    ) -> Join:
1863        """
1864        Append to or set the ON expressions.
1865
1866        Example:
1867            >>> import sqlglot
1868            >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql()
1869            'JOIN x ON y = 1'
1870
1871        Args:
1872            *expressions: the SQL code strings to parse.
1873                If an `Expression` instance is passed, it will be used as-is.
1874                Multiple expressions are combined with an AND operator.
1875            append: if `True`, AND the new expressions to any existing expression.
1876                Otherwise, this resets the expression.
1877            dialect: the dialect used to parse the input expressions.
1878            copy: if `False`, modify this expression instance in-place.
1879            opts: other options to use to parse the input expressions.
1880
1881        Returns:
1882            The modified Join expression.
1883        """
1884        join = _apply_conjunction_builder(
1885            *expressions,
1886            instance=self,
1887            arg="on",
1888            append=append,
1889            dialect=dialect,
1890            copy=copy,
1891            **opts,
1892        )
1893
1894        if join.kind == "CROSS":
1895            join.set("kind", None)
1896
1897        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:
1899    def using(
1900        self,
1901        *expressions: t.Optional[ExpOrStr],
1902        append: bool = True,
1903        dialect: DialectType = None,
1904        copy: bool = True,
1905        **opts,
1906    ) -> Join:
1907        """
1908        Append to or set the USING expressions.
1909
1910        Example:
1911            >>> import sqlglot
1912            >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql()
1913            'JOIN x USING (foo, bla)'
1914
1915        Args:
1916            *expressions: the SQL code strings to parse.
1917                If an `Expression` instance is passed, it will be used as-is.
1918            append: if `True`, concatenate the new expressions to the existing "using" list.
1919                Otherwise, this resets the expression.
1920            dialect: the dialect used to parse the input expressions.
1921            copy: if `False`, modify this expression instance in-place.
1922            opts: other options to use to parse the input expressions.
1923
1924        Returns:
1925            The modified Join expression.
1926        """
1927        join = _apply_list_builder(
1928            *expressions,
1929            instance=self,
1930            arg="using",
1931            append=append,
1932            dialect=dialect,
1933            copy=copy,
1934            **opts,
1935        )
1936
1937        if join.kind == "CROSS":
1938            join.set("kind", None)
1939
1940        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):
1943class Lateral(UDTF):
1944    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):
1947class MatchRecognize(Expression):
1948    arg_types = {
1949        "partition_by": False,
1950        "order": False,
1951        "measures": False,
1952        "rows": False,
1953        "after": False,
1954        "pattern": False,
1955        "define": False,
1956        "alias": False,
1957    }
arg_types = {'partition_by': False, 'order': False, 'measures': False, 'rows': False, 'after': False, 'pattern': False, 'define': False, 'alias': False}
key = 'matchrecognize'
class Final(Expression):
1962class Final(Expression):
1963    pass
key = 'final'
class Offset(Expression):
1966class Offset(Expression):
1967    arg_types = {"this": False, "expression": True}
arg_types = {'this': False, 'expression': True}
key = 'offset'
class Order(Expression):
1970class Order(Expression):
1971    arg_types = {"this": False, "expressions": True}
arg_types = {'this': False, 'expressions': True}
key = 'order'
class Cluster(Order):
1976class Cluster(Order):
1977    pass
key = 'cluster'
class Distribute(Order):
1980class Distribute(Order):
1981    pass
key = 'distribute'
class Sort(Order):
1984class Sort(Order):
1985    pass
key = 'sort'
class Ordered(Expression):
1988class Ordered(Expression):
1989    arg_types = {"this": True, "desc": False, "nulls_first": True}
arg_types = {'this': True, 'desc': False, 'nulls_first': True}
key = 'ordered'
class Property(Expression):
1992class Property(Expression):
1993    arg_types = {"this": True, "value": True}
arg_types = {'this': True, 'value': True}
key = 'property'
class AlgorithmProperty(Property):
1996class AlgorithmProperty(Property):
1997    arg_types = {"this": True}
arg_types = {'this': True}
key = 'algorithmproperty'
class AutoIncrementProperty(Property):
2000class AutoIncrementProperty(Property):
2001    arg_types = {"this": True}
arg_types = {'this': True}
key = 'autoincrementproperty'
class BlockCompressionProperty(Property):
2004class BlockCompressionProperty(Property):
2005    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):
2008class CharacterSetProperty(Property):
2009    arg_types = {"this": True, "default": True}
arg_types = {'this': True, 'default': True}
key = 'charactersetproperty'
class ChecksumProperty(Property):
2012class ChecksumProperty(Property):
2013    arg_types = {"on": False, "default": False}
arg_types = {'on': False, 'default': False}
key = 'checksumproperty'
class CollateProperty(Property):
2016class CollateProperty(Property):
2017    arg_types = {"this": True, "default": False}
arg_types = {'this': True, 'default': False}
key = 'collateproperty'
class CopyGrantsProperty(Property):
2020class CopyGrantsProperty(Property):
2021    arg_types = {}
arg_types = {}
key = 'copygrantsproperty'
class DataBlocksizeProperty(Property):
2024class DataBlocksizeProperty(Property):
2025    arg_types = {
2026        "size": False,
2027        "units": False,
2028        "minimum": False,
2029        "maximum": False,
2030        "default": False,
2031    }
arg_types = {'size': False, 'units': False, 'minimum': False, 'maximum': False, 'default': False}
key = 'datablocksizeproperty'
class DefinerProperty(Property):
2034class DefinerProperty(Property):
2035    arg_types = {"this": True}
arg_types = {'this': True}
key = 'definerproperty'
class DistKeyProperty(Property):
2038class DistKeyProperty(Property):
2039    arg_types = {"this": True}
arg_types = {'this': True}
key = 'distkeyproperty'
class DistStyleProperty(Property):
2042class DistStyleProperty(Property):
2043    arg_types = {"this": True}
arg_types = {'this': True}
key = 'diststyleproperty'
class EngineProperty(Property):
2046class EngineProperty(Property):
2047    arg_types = {"this": True}
arg_types = {'this': True}
key = 'engineproperty'
class HeapProperty(Property):
2050class HeapProperty(Property):
2051    arg_types = {}
arg_types = {}
key = 'heapproperty'
class ToTableProperty(Property):
2054class ToTableProperty(Property):
2055    arg_types = {"this": True}
arg_types = {'this': True}
key = 'totableproperty'
class ExecuteAsProperty(Property):
2058class ExecuteAsProperty(Property):
2059    arg_types = {"this": True}
arg_types = {'this': True}
key = 'executeasproperty'
class ExternalProperty(Property):
2062class ExternalProperty(Property):
2063    arg_types = {"this": False}
arg_types = {'this': False}
key = 'externalproperty'
class FallbackProperty(Property):
2066class FallbackProperty(Property):
2067    arg_types = {"no": True, "protection": False}
arg_types = {'no': True, 'protection': False}
key = 'fallbackproperty'
class FileFormatProperty(Property):
2070class FileFormatProperty(Property):
2071    arg_types = {"this": True}
arg_types = {'this': True}
key = 'fileformatproperty'
class FreespaceProperty(Property):
2074class FreespaceProperty(Property):
2075    arg_types = {"this": True, "percent": False}
arg_types = {'this': True, 'percent': False}
key = 'freespaceproperty'
class InputModelProperty(Property):
2078class InputModelProperty(Property):
2079    arg_types = {"this": True}
arg_types = {'this': True}
key = 'inputmodelproperty'
class OutputModelProperty(Property):
2082class OutputModelProperty(Property):
2083    arg_types = {"this": True}
arg_types = {'this': True}
key = 'outputmodelproperty'
class IsolatedLoadingProperty(Property):
2086class IsolatedLoadingProperty(Property):
2087    arg_types = {
2088        "no": True,
2089        "concurrent": True,
2090        "for_all": True,
2091        "for_insert": True,
2092        "for_none": True,
2093    }
arg_types = {'no': True, 'concurrent': True, 'for_all': True, 'for_insert': True, 'for_none': True}
key = 'isolatedloadingproperty'
class JournalProperty(Property):
2096class JournalProperty(Property):
2097    arg_types = {
2098        "no": False,
2099        "dual": False,
2100        "before": False,
2101        "local": False,
2102        "after": False,
2103    }
arg_types = {'no': False, 'dual': False, 'before': False, 'local': False, 'after': False}
key = 'journalproperty'
class LanguageProperty(Property):
2106class LanguageProperty(Property):
2107    arg_types = {"this": True}
arg_types = {'this': True}
key = 'languageproperty'
class ClusteredByProperty(Property):
2111class ClusteredByProperty(Property):
2112    arg_types = {"expressions": True, "sorted_by": False, "buckets": True}
arg_types = {'expressions': True, 'sorted_by': False, 'buckets': True}
key = 'clusteredbyproperty'
class DictProperty(Property):
2115class DictProperty(Property):
2116    arg_types = {"this": True, "kind": True, "settings": False}
arg_types = {'this': True, 'kind': True, 'settings': False}
key = 'dictproperty'
class DictSubProperty(Property):
2119class DictSubProperty(Property):
2120    pass
key = 'dictsubproperty'
class DictRange(Property):
2123class DictRange(Property):
2124    arg_types = {"this": True, "min": True, "max": True}
arg_types = {'this': True, 'min': True, 'max': True}
key = 'dictrange'
class OnCluster(Property):
2129class OnCluster(Property):
2130    arg_types = {"this": True}
arg_types = {'this': True}
key = 'oncluster'
class LikeProperty(Property):
2133class LikeProperty(Property):
2134    arg_types = {"this": True, "expressions": False}
arg_types = {'this': True, 'expressions': False}
key = 'likeproperty'
class LocationProperty(Property):
2137class LocationProperty(Property):
2138    arg_types = {"this": True}
arg_types = {'this': True}
key = 'locationproperty'
class LockingProperty(Property):
2141class LockingProperty(Property):
2142    arg_types = {
2143        "this": False,
2144        "kind": True,
2145        "for_or_in": False,
2146        "lock_type": True,
2147        "override": False,
2148    }
arg_types = {'this': False, 'kind': True, 'for_or_in': False, 'lock_type': True, 'override': False}
key = 'lockingproperty'
class LogProperty(Property):
2151class LogProperty(Property):
2152    arg_types = {"no": True}
arg_types = {'no': True}
key = 'logproperty'
class MaterializedProperty(Property):
2155class MaterializedProperty(Property):
2156    arg_types = {"this": False}
arg_types = {'this': False}
key = 'materializedproperty'
class MergeBlockRatioProperty(Property):
2159class MergeBlockRatioProperty(Property):
2160    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):
2163class NoPrimaryIndexProperty(Property):
2164    arg_types = {}
arg_types = {}
key = 'noprimaryindexproperty'
class OnProperty(Property):
2167class OnProperty(Property):
2168    arg_types = {"this": True}
arg_types = {'this': True}
key = 'onproperty'
class OnCommitProperty(Property):
2171class OnCommitProperty(Property):
2172    arg_types = {"delete": False}
arg_types = {'delete': False}
key = 'oncommitproperty'
class PartitionedByProperty(Property):
2175class PartitionedByProperty(Property):
2176    arg_types = {"this": True}
arg_types = {'this': True}
key = 'partitionedbyproperty'
class PartitionBoundSpec(Expression):
2180class PartitionBoundSpec(Expression):
2181    # this -> IN / MODULUS, expression -> REMAINDER, from_expressions -> FROM (...), to_expressions -> TO (...)
2182    arg_types = {
2183        "this": False,
2184        "expression": False,
2185        "from_expressions": False,
2186        "to_expressions": False,
2187    }
arg_types = {'this': False, 'expression': False, 'from_expressions': False, 'to_expressions': False}
key = 'partitionboundspec'
class PartitionedOfProperty(Property):
2190class PartitionedOfProperty(Property):
2191    # this -> parent_table (schema), expression -> FOR VALUES ... / DEFAULT
2192    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'partitionedofproperty'
class RemoteWithConnectionModelProperty(Property):
2195class RemoteWithConnectionModelProperty(Property):
2196    arg_types = {"this": True}
arg_types = {'this': True}
key = 'remotewithconnectionmodelproperty'
class ReturnsProperty(Property):
2199class ReturnsProperty(Property):
2200    arg_types = {"this": True, "is_table": False, "table": False}
arg_types = {'this': True, 'is_table': False, 'table': False}
key = 'returnsproperty'
class RowFormatProperty(Property):
2203class RowFormatProperty(Property):
2204    arg_types = {"this": True}
arg_types = {'this': True}
key = 'rowformatproperty'
class RowFormatDelimitedProperty(Property):
2207class RowFormatDelimitedProperty(Property):
2208    # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml
2209    arg_types = {
2210        "fields": False,
2211        "escaped": False,
2212        "collection_items": False,
2213        "map_keys": False,
2214        "lines": False,
2215        "null": False,
2216        "serde": False,
2217    }
arg_types = {'fields': False, 'escaped': False, 'collection_items': False, 'map_keys': False, 'lines': False, 'null': False, 'serde': False}
key = 'rowformatdelimitedproperty'
class RowFormatSerdeProperty(Property):
2220class RowFormatSerdeProperty(Property):
2221    arg_types = {"this": True, "serde_properties": False}
arg_types = {'this': True, 'serde_properties': False}
key = 'rowformatserdeproperty'
class QueryTransform(Expression):
2225class QueryTransform(Expression):
2226    arg_types = {
2227        "expressions": True,
2228        "command_script": True,
2229        "schema": False,
2230        "row_format_before": False,
2231        "record_writer": False,
2232        "row_format_after": False,
2233        "record_reader": False,
2234    }
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):
2237class SampleProperty(Property):
2238    arg_types = {"this": True}
arg_types = {'this': True}
key = 'sampleproperty'
class SchemaCommentProperty(Property):
2241class SchemaCommentProperty(Property):
2242    arg_types = {"this": True}
arg_types = {'this': True}
key = 'schemacommentproperty'
class SerdeProperties(Property):
2245class SerdeProperties(Property):
2246    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'serdeproperties'
class SetProperty(Property):
2249class SetProperty(Property):
2250    arg_types = {"multi": True}
arg_types = {'multi': True}
key = 'setproperty'
class SettingsProperty(Property):
2253class SettingsProperty(Property):
2254    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'settingsproperty'
class SortKeyProperty(Property):
2257class SortKeyProperty(Property):
2258    arg_types = {"this": True, "compound": False}
arg_types = {'this': True, 'compound': False}
key = 'sortkeyproperty'
class SqlSecurityProperty(Property):
2261class SqlSecurityProperty(Property):
2262    arg_types = {"definer": True}
arg_types = {'definer': True}
key = 'sqlsecurityproperty'
class StabilityProperty(Property):
2265class StabilityProperty(Property):
2266    arg_types = {"this": True}
arg_types = {'this': True}
key = 'stabilityproperty'
class TemporaryProperty(Property):
2269class TemporaryProperty(Property):
2270    arg_types = {}
arg_types = {}
key = 'temporaryproperty'
class TransformModelProperty(Property):
2273class TransformModelProperty(Property):
2274    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'transformmodelproperty'
class TransientProperty(Property):
2277class TransientProperty(Property):
2278    arg_types = {"this": False}
arg_types = {'this': False}
key = 'transientproperty'
class VolatileProperty(Property):
2281class VolatileProperty(Property):
2282    arg_types = {"this": False}
arg_types = {'this': False}
key = 'volatileproperty'
class WithDataProperty(Property):
2285class WithDataProperty(Property):
2286    arg_types = {"no": True, "statistics": False}
arg_types = {'no': True, 'statistics': False}
key = 'withdataproperty'
class WithJournalTableProperty(Property):
2289class WithJournalTableProperty(Property):
2290    arg_types = {"this": True}
arg_types = {'this': True}
key = 'withjournaltableproperty'
class WithSystemVersioningProperty(Property):
2293class WithSystemVersioningProperty(Property):
2294    # this -> history table name, expression -> data consistency check
2295    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'withsystemversioningproperty'
class Properties(Expression):
2298class Properties(Expression):
2299    arg_types = {"expressions": True}
2300
2301    NAME_TO_PROPERTY = {
2302        "ALGORITHM": AlgorithmProperty,
2303        "AUTO_INCREMENT": AutoIncrementProperty,
2304        "CHARACTER SET": CharacterSetProperty,
2305        "CLUSTERED_BY": ClusteredByProperty,
2306        "COLLATE": CollateProperty,
2307        "COMMENT": SchemaCommentProperty,
2308        "DEFINER": DefinerProperty,
2309        "DISTKEY": DistKeyProperty,
2310        "DISTSTYLE": DistStyleProperty,
2311        "ENGINE": EngineProperty,
2312        "EXECUTE AS": ExecuteAsProperty,
2313        "FORMAT": FileFormatProperty,
2314        "LANGUAGE": LanguageProperty,
2315        "LOCATION": LocationProperty,
2316        "PARTITIONED_BY": PartitionedByProperty,
2317        "RETURNS": ReturnsProperty,
2318        "ROW_FORMAT": RowFormatProperty,
2319        "SORTKEY": SortKeyProperty,
2320    }
2321
2322    PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()}
2323
2324    # CREATE property locations
2325    # Form: schema specified
2326    #   create [POST_CREATE]
2327    #     table a [POST_NAME]
2328    #     (b int) [POST_SCHEMA]
2329    #     with ([POST_WITH])
2330    #     index (b) [POST_INDEX]
2331    #
2332    # Form: alias selection
2333    #   create [POST_CREATE]
2334    #     table a [POST_NAME]
2335    #     as [POST_ALIAS] (select * from b) [POST_EXPRESSION]
2336    #     index (c) [POST_INDEX]
2337    class Location(AutoName):
2338        POST_CREATE = auto()
2339        POST_NAME = auto()
2340        POST_SCHEMA = auto()
2341        POST_WITH = auto()
2342        POST_ALIAS = auto()
2343        POST_EXPRESSION = auto()
2344        POST_INDEX = auto()
2345        UNSUPPORTED = auto()
2346
2347    @classmethod
2348    def from_dict(cls, properties_dict: t.Dict) -> Properties:
2349        expressions = []
2350        for key, value in properties_dict.items():
2351            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
2352            if property_cls:
2353                expressions.append(property_cls(this=convert(value)))
2354            else:
2355                expressions.append(Property(this=Literal.string(key), value=convert(value)))
2356
2357        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:
2347    @classmethod
2348    def from_dict(cls, properties_dict: t.Dict) -> Properties:
2349        expressions = []
2350        for key, value in properties_dict.items():
2351            property_cls = cls.NAME_TO_PROPERTY.get(key.upper())
2352            if property_cls:
2353                expressions.append(property_cls(this=convert(value)))
2354            else:
2355                expressions.append(Property(this=Literal.string(key), value=convert(value)))
2356
2357        return cls(expressions=expressions)
key = 'properties'
class Properties.Location(sqlglot.helper.AutoName):
2337    class Location(AutoName):
2338        POST_CREATE = auto()
2339        POST_NAME = auto()
2340        POST_SCHEMA = auto()
2341        POST_WITH = auto()
2342        POST_ALIAS = auto()
2343        POST_EXPRESSION = auto()
2344        POST_INDEX = auto()
2345        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):
2360class Qualify(Expression):
2361    pass
key = 'qualify'
class InputOutputFormat(Expression):
2364class InputOutputFormat(Expression):
2365    arg_types = {"input_format": False, "output_format": False}
arg_types = {'input_format': False, 'output_format': False}
key = 'inputoutputformat'
class Return(Expression):
2369class Return(Expression):
2370    pass
key = 'return'
class Reference(Expression):
2373class Reference(Expression):
2374    arg_types = {"this": True, "expressions": False, "options": False}
arg_types = {'this': True, 'expressions': False, 'options': False}
key = 'reference'
class Tuple(Expression):
2377class Tuple(Expression):
2378    arg_types = {"expressions": False}
2379
2380    def isin(
2381        self,
2382        *expressions: t.Any,
2383        query: t.Optional[ExpOrStr] = None,
2384        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
2385        copy: bool = True,
2386        **opts,
2387    ) -> In:
2388        return In(
2389            this=maybe_copy(self, copy),
2390            expressions=[convert(e, copy=copy) for e in expressions],
2391            query=maybe_parse(query, copy=copy, **opts) if query else None,
2392            unnest=Unnest(
2393                expressions=[
2394                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
2395                ]
2396            )
2397            if unnest
2398            else None,
2399        )
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:
2380    def isin(
2381        self,
2382        *expressions: t.Any,
2383        query: t.Optional[ExpOrStr] = None,
2384        unnest: t.Optional[ExpOrStr] | t.Collection[ExpOrStr] = None,
2385        copy: bool = True,
2386        **opts,
2387    ) -> In:
2388        return In(
2389            this=maybe_copy(self, copy),
2390            expressions=[convert(e, copy=copy) for e in expressions],
2391            query=maybe_parse(query, copy=copy, **opts) if query else None,
2392            unnest=Unnest(
2393                expressions=[
2394                    maybe_parse(t.cast(ExpOrStr, e), copy=copy, **opts) for e in ensure_list(unnest)
2395                ]
2396            )
2397            if unnest
2398            else None,
2399        )
key = 'tuple'
class Subqueryable(Unionable):
2402class Subqueryable(Unionable):
2403    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
2404        """
2405        Convert this expression to an aliased expression that can be used as a Subquery.
2406
2407        Example:
2408            >>> subquery = Select().select("x").from_("tbl").subquery()
2409            >>> Select().select("x").from_(subquery).sql()
2410            'SELECT x FROM (SELECT x FROM tbl)'
2411
2412        Args:
2413            alias (str | Identifier): an optional alias for the subquery
2414            copy (bool): if `False`, modify this expression instance in-place.
2415
2416        Returns:
2417            Alias: the subquery
2418        """
2419        instance = maybe_copy(self, copy)
2420        if not isinstance(alias, Expression):
2421            alias = TableAlias(this=to_identifier(alias)) if alias else None
2422
2423        return Subquery(this=instance, alias=alias)
2424
2425    def limit(
2426        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2427    ) -> Select:
2428        raise NotImplementedError
2429
2430    @property
2431    def ctes(self):
2432        with_ = self.args.get("with")
2433        if not with_:
2434            return []
2435        return with_.expressions
2436
2437    @property
2438    def selects(self) -> t.List[Expression]:
2439        raise NotImplementedError("Subqueryable objects must implement `selects`")
2440
2441    @property
2442    def named_selects(self) -> t.List[str]:
2443        raise NotImplementedError("Subqueryable objects must implement `named_selects`")
2444
2445    def select(
2446        self,
2447        *expressions: t.Optional[ExpOrStr],
2448        append: bool = True,
2449        dialect: DialectType = None,
2450        copy: bool = True,
2451        **opts,
2452    ) -> Subqueryable:
2453        raise NotImplementedError("Subqueryable objects must implement `select`")
2454
2455    def with_(
2456        self,
2457        alias: ExpOrStr,
2458        as_: ExpOrStr,
2459        recursive: t.Optional[bool] = None,
2460        append: bool = True,
2461        dialect: DialectType = None,
2462        copy: bool = True,
2463        **opts,
2464    ) -> Subqueryable:
2465        """
2466        Append to or set the common table expressions.
2467
2468        Example:
2469            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
2470            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
2471
2472        Args:
2473            alias: the SQL code string to parse as the table name.
2474                If an `Expression` instance is passed, this is used as-is.
2475            as_: the SQL code string to parse as the table expression.
2476                If an `Expression` instance is passed, it will be used as-is.
2477            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2478            append: if `True`, add to any existing expressions.
2479                Otherwise, this resets the expressions.
2480            dialect: the dialect used to parse the input expression.
2481            copy: if `False`, modify this expression instance in-place.
2482            opts: other options to use to parse the input expressions.
2483
2484        Returns:
2485            The modified expression.
2486        """
2487        return _apply_cte_builder(
2488            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
2489        )
def subquery( self, alias: Union[str, Expression, NoneType] = None, copy: bool = True) -> Subquery:
2403    def subquery(self, alias: t.Optional[ExpOrStr] = None, copy: bool = True) -> Subquery:
2404        """
2405        Convert this expression to an aliased expression that can be used as a Subquery.
2406
2407        Example:
2408            >>> subquery = Select().select("x").from_("tbl").subquery()
2409            >>> Select().select("x").from_(subquery).sql()
2410            'SELECT x FROM (SELECT x FROM tbl)'
2411
2412        Args:
2413            alias (str | Identifier): an optional alias for the subquery
2414            copy (bool): if `False`, modify this expression instance in-place.
2415
2416        Returns:
2417            Alias: the subquery
2418        """
2419        instance = maybe_copy(self, copy)
2420        if not isinstance(alias, Expression):
2421            alias = TableAlias(this=to_identifier(alias)) if alias else None
2422
2423        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:
2425    def limit(
2426        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2427    ) -> Select:
2428        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:
2445    def select(
2446        self,
2447        *expressions: t.Optional[ExpOrStr],
2448        append: bool = True,
2449        dialect: DialectType = None,
2450        copy: bool = True,
2451        **opts,
2452    ) -> Subqueryable:
2453        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:
2455    def with_(
2456        self,
2457        alias: ExpOrStr,
2458        as_: ExpOrStr,
2459        recursive: t.Optional[bool] = None,
2460        append: bool = True,
2461        dialect: DialectType = None,
2462        copy: bool = True,
2463        **opts,
2464    ) -> Subqueryable:
2465        """
2466        Append to or set the common table expressions.
2467
2468        Example:
2469            >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql()
2470            'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2'
2471
2472        Args:
2473            alias: the SQL code string to parse as the table name.
2474                If an `Expression` instance is passed, this is used as-is.
2475            as_: the SQL code string to parse as the table expression.
2476                If an `Expression` instance is passed, it will be used as-is.
2477            recursive: set the RECURSIVE part of the expression. Defaults to `False`.
2478            append: if `True`, add to any existing expressions.
2479                Otherwise, this resets the expressions.
2480            dialect: the dialect used to parse the input expression.
2481            copy: if `False`, modify this expression instance in-place.
2482            opts: other options to use to parse the input expressions.
2483
2484        Returns:
2485            The modified expression.
2486        """
2487        return _apply_cte_builder(
2488            self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts
2489        )

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):
2517class WithTableHint(Expression):
2518    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'withtablehint'
class IndexTableHint(Expression):
2522class IndexTableHint(Expression):
2523    arg_types = {"this": True, "expressions": False, "target": False}
arg_types = {'this': True, 'expressions': False, 'target': False}
key = 'indextablehint'
class Table(Expression):
2526class Table(Expression):
2527    arg_types = {
2528        "this": True,
2529        "alias": False,
2530        "db": False,
2531        "catalog": False,
2532        "laterals": False,
2533        "joins": False,
2534        "pivots": False,
2535        "hints": False,
2536        "system_time": False,
2537        "version": False,
2538        "format": False,
2539        "pattern": False,
2540        "index": False,
2541        "ordinality": False,
2542    }
2543
2544    @property
2545    def name(self) -> str:
2546        if isinstance(self.this, Func):
2547            return ""
2548        return self.this.name
2549
2550    @property
2551    def db(self) -> str:
2552        return self.text("db")
2553
2554    @property
2555    def catalog(self) -> str:
2556        return self.text("catalog")
2557
2558    @property
2559    def selects(self) -> t.List[Expression]:
2560        return []
2561
2562    @property
2563    def named_selects(self) -> t.List[str]:
2564        return []
2565
2566    @property
2567    def parts(self) -> t.List[Expression]:
2568        """Return the parts of a table in order catalog, db, table."""
2569        parts: t.List[Expression] = []
2570
2571        for arg in ("catalog", "db", "this"):
2572            part = self.args.get(arg)
2573
2574            if isinstance(part, Dot):
2575                parts.extend(part.flatten())
2576            elif isinstance(part, Expression):
2577                parts.append(part)
2578
2579        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}
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):
2582class Union(Subqueryable):
2583    arg_types = {
2584        "with": False,
2585        "this": True,
2586        "expression": True,
2587        "distinct": False,
2588        "by_name": False,
2589        **QUERY_MODIFIERS,
2590    }
2591
2592    def limit(
2593        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2594    ) -> Select:
2595        """
2596        Set the LIMIT expression.
2597
2598        Example:
2599            >>> select("1").union(select("1")).limit(1).sql()
2600            'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1'
2601
2602        Args:
2603            expression: the SQL code string to parse.
2604                This can also be an integer.
2605                If a `Limit` instance is passed, this is used as-is.
2606                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2607            dialect: the dialect used to parse the input expression.
2608            copy: if `False`, modify this expression instance in-place.
2609            opts: other options to use to parse the input expressions.
2610
2611        Returns:
2612            The limited subqueryable.
2613        """
2614        return (
2615            select("*")
2616            .from_(self.subquery(alias="_l_0", copy=copy))
2617            .limit(expression, dialect=dialect, copy=False, **opts)
2618        )
2619
2620    def select(
2621        self,
2622        *expressions: t.Optional[ExpOrStr],
2623        append: bool = True,
2624        dialect: DialectType = None,
2625        copy: bool = True,
2626        **opts,
2627    ) -> Union:
2628        """Append to or set the SELECT of the union recursively.
2629
2630        Example:
2631            >>> from sqlglot import parse_one
2632            >>> parse_one("select a from x union select a from y union select a from z").select("b").sql()
2633            'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z'
2634
2635        Args:
2636            *expressions: the SQL code strings to parse.
2637                If an `Expression` instance is passed, it will be used as-is.
2638            append: if `True`, add to any existing expressions.
2639                Otherwise, this resets the expressions.
2640            dialect: the dialect used to parse the input expressions.
2641            copy: if `False`, modify this expression instance in-place.
2642            opts: other options to use to parse the input expressions.
2643
2644        Returns:
2645            Union: the modified expression.
2646        """
2647        this = self.copy() if copy else self
2648        this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts)
2649        this.expression.unnest().select(
2650            *expressions, append=append, dialect=dialect, copy=False, **opts
2651        )
2652        return this
2653
2654    @property
2655    def named_selects(self) -> t.List[str]:
2656        return self.this.unnest().named_selects
2657
2658    @property
2659    def is_star(self) -> bool:
2660        return self.this.is_star or self.expression.is_star
2661
2662    @property
2663    def selects(self) -> t.List[Expression]:
2664        return self.this.unnest().selects
2665
2666    @property
2667    def left(self) -> Expression:
2668        return self.this
2669
2670    @property
2671    def right(self) -> Expression:
2672        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:
2592    def limit(
2593        self, expression: ExpOrStr | int, dialect: DialectType = None, copy: bool = True, **opts
2594    ) -> Select:
2595        """
2596        Set the LIMIT expression.
2597
2598        Example:
2599            >>> select("1").union(select("1")).limit(1).sql()
2600            'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1'
2601
2602        Args:
2603            expression: the SQL code string to parse.
2604                This can also be an integer.
2605                If a `Limit` instance is passed, this is used as-is.
2606                If another `Expression` instance is passed, it will be wrapped in a `Limit`.
2607            dialect: the dialect used to parse the input expression.
2608            copy: if `False`, modify this expression instance in-place.
2609            opts: other options to use to parse the input expressions.
2610
2611        Returns:
2612            The limited subqueryable.
2613        """
2614        return (
2615            select("*")
2616            .from_(self.subquery(alias="_l_0", copy=copy))
2617            .limit(expression, dialect=dialect, copy=False, **opts)
2618        )

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

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

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

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

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

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

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

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

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

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

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

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

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

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:
3369    def lock(self, update: bool = True, copy: bool = True) -> Select:
3370        """
3371        Set the locking read mode for this expression.
3372
3373        Examples:
3374            >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql")
3375            "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE"
3376
3377            >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql")
3378            "SELECT x FROM tbl WHERE x = 'a' FOR SHARE"
3379
3380        Args:
3381            update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`.
3382            copy: if `False`, modify this expression instance in-place.
3383
3384        Returns:
3385            The modified expression.
3386        """
3387        inst = maybe_copy(self, copy)
3388        inst.set("locks", [Lock(update=update)])
3389
3390        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:
3392    def hint(self, *hints: ExpOrStr, dialect: DialectType = None, copy: bool = True) -> Select:
3393        """
3394        Set hints for this expression.
3395
3396        Examples:
3397            >>> Select().select("x").from_("tbl").hint("BROADCAST(y)").sql(dialect="spark")
3398            'SELECT /*+ BROADCAST(y) */ x FROM tbl'
3399
3400        Args:
3401            hints: The SQL code strings to parse as the hints.
3402                If an `Expression` instance is passed, it will be used as-is.
3403            dialect: The dialect used to parse the hints.
3404            copy: If `False`, modify this expression instance in-place.
3405
3406        Returns:
3407            The modified expression.
3408        """
3409        inst = maybe_copy(self, copy)
3410        inst.set(
3411            "hint", Hint(expressions=[maybe_parse(h, copy=copy, dialect=dialect) for h in hints])
3412        )
3413
3414        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):
3429class Subquery(DerivedTable, Unionable):
3430    arg_types = {
3431        "this": True,
3432        "alias": False,
3433        "with": False,
3434        **QUERY_MODIFIERS,
3435    }
3436
3437    def unnest(self):
3438        """
3439        Returns the first non subquery.
3440        """
3441        expression = self
3442        while isinstance(expression, Subquery):
3443            expression = expression.this
3444        return expression
3445
3446    def unwrap(self) -> Subquery:
3447        expression = self
3448        while expression.same_parent and expression.is_wrapper:
3449            expression = t.cast(Subquery, expression.parent)
3450        return expression
3451
3452    @property
3453    def is_wrapper(self) -> bool:
3454        """
3455        Whether this Subquery acts as a simple wrapper around another expression.
3456
3457        SELECT * FROM (((SELECT * FROM t)))
3458                      ^
3459                      This corresponds to a "wrapper" Subquery node
3460        """
3461        return all(v is None for k, v in self.args.items() if k != "this")
3462
3463    @property
3464    def is_star(self) -> bool:
3465        return self.this.is_star
3466
3467    @property
3468    def output_name(self) -> str:
3469        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):
3437    def unnest(self):
3438        """
3439        Returns the first non subquery.
3440        """
3441        expression = self
3442        while isinstance(expression, Subquery):
3443            expression = expression.this
3444        return expression

Returns the first non subquery.

def unwrap(self) -> Subquery:
3446    def unwrap(self) -> Subquery:
3447        expression = self
3448        while expression.same_parent and expression.is_wrapper:
3449            expression = t.cast(Subquery, expression.parent)
3450        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):
3472class TableSample(Expression):
3473    arg_types = {
3474        "this": False,
3475        "expressions": False,
3476        "method": False,
3477        "bucket_numerator": False,
3478        "bucket_denominator": False,
3479        "bucket_field": False,
3480        "percent": False,
3481        "rows": False,
3482        "size": False,
3483        "seed": False,
3484        "kind": False,
3485    }
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):
3488class Tag(Expression):
3489    """Tags are used for generating arbitrary sql like SELECT <span>x</span>."""
3490
3491    arg_types = {
3492        "this": False,
3493        "prefix": False,
3494        "postfix": False,
3495    }

Tags are used for generating arbitrary sql like SELECT x.

arg_types = {'this': False, 'prefix': False, 'postfix': False}
key = 'tag'
class Pivot(Expression):
3500class Pivot(Expression):
3501    arg_types = {
3502        "this": False,
3503        "alias": False,
3504        "expressions": False,
3505        "field": False,
3506        "unpivot": False,
3507        "using": False,
3508        "group": False,
3509        "columns": False,
3510        "include_nulls": False,
3511    }
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):
3514class Window(Condition):
3515    arg_types = {
3516        "this": True,
3517        "partition_by": False,
3518        "order": False,
3519        "spec": False,
3520        "alias": False,
3521        "over": False,
3522        "first": False,
3523    }
arg_types = {'this': True, 'partition_by': False, 'order': False, 'spec': False, 'alias': False, 'over': False, 'first': False}
key = 'window'
class WindowSpec(Expression):
3526class WindowSpec(Expression):
3527    arg_types = {
3528        "kind": False,
3529        "start": False,
3530        "start_side": False,
3531        "end": False,
3532        "end_side": False,
3533    }
arg_types = {'kind': False, 'start': False, 'start_side': False, 'end': False, 'end_side': False}
key = 'windowspec'
class Where(Expression):
3536class Where(Expression):
3537    pass
key = 'where'
class Star(Expression):
3540class Star(Expression):
3541    arg_types = {"except": False, "replace": False}
3542
3543    @property
3544    def name(self) -> str:
3545        return "*"
3546
3547    @property
3548    def output_name(self) -> str:
3549        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):
3552class Parameter(Condition):
3553    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'parameter'
class SessionParameter(Condition):
3556class SessionParameter(Condition):
3557    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'sessionparameter'
class Placeholder(Condition):
3560class Placeholder(Condition):
3561    arg_types = {"this": False, "kind": False}
arg_types = {'this': False, 'kind': False}
key = 'placeholder'
class Null(Condition):
3564class Null(Condition):
3565    arg_types: t.Dict[str, t.Any] = {}
3566
3567    @property
3568    def name(self) -> str:
3569        return "NULL"
arg_types: Dict[str, Any] = {}
name: str
key = 'null'
class Boolean(Condition):
3572class Boolean(Condition):
3573    pass
key = 'boolean'
class DataTypeParam(Expression):
3576class DataTypeParam(Expression):
3577    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'datatypeparam'
class DataType(Expression):
3580class DataType(Expression):
3581    arg_types = {
3582        "this": True,
3583        "expressions": False,
3584        "nested": False,
3585        "values": False,
3586        "prefix": False,
3587        "kind": False,
3588    }
3589
3590    class Type(AutoName):
3591        ARRAY = auto()
3592        BIGDECIMAL = auto()
3593        BIGINT = auto()
3594        BIGSERIAL = auto()
3595        BINARY = auto()
3596        BIT = auto()
3597        BOOLEAN = auto()
3598        CHAR = auto()
3599        DATE = auto()
3600        DATEMULTIRANGE = auto()
3601        DATERANGE = auto()
3602        DATETIME = auto()
3603        DATETIME64 = auto()
3604        DECIMAL = auto()
3605        DOUBLE = auto()
3606        ENUM = auto()
3607        ENUM8 = auto()
3608        ENUM16 = auto()
3609        FIXEDSTRING = auto()
3610        FLOAT = auto()
3611        GEOGRAPHY = auto()
3612        GEOMETRY = auto()
3613        HLLSKETCH = auto()
3614        HSTORE = auto()
3615        IMAGE = auto()
3616        INET = auto()
3617        INT = auto()
3618        INT128 = auto()
3619        INT256 = auto()
3620        INT4MULTIRANGE = auto()
3621        INT4RANGE = auto()
3622        INT8MULTIRANGE = auto()
3623        INT8RANGE = auto()
3624        INTERVAL = auto()
3625        IPADDRESS = auto()
3626        IPPREFIX = auto()
3627        JSON = auto()
3628        JSONB = auto()
3629        LONGBLOB = auto()
3630        LONGTEXT = auto()
3631        LOWCARDINALITY = auto()
3632        MAP = auto()
3633        MEDIUMBLOB = auto()
3634        MEDIUMINT = auto()
3635        MEDIUMTEXT = auto()
3636        MONEY = auto()
3637        NCHAR = auto()
3638        NESTED = auto()
3639        NULL = auto()
3640        NULLABLE = auto()
3641        NUMMULTIRANGE = auto()
3642        NUMRANGE = auto()
3643        NVARCHAR = auto()
3644        OBJECT = auto()
3645        ROWVERSION = auto()
3646        SERIAL = auto()
3647        SET = auto()
3648        SMALLINT = auto()
3649        SMALLMONEY = auto()
3650        SMALLSERIAL = auto()
3651        STRUCT = auto()
3652        SUPER = auto()
3653        TEXT = auto()
3654        TINYBLOB = auto()
3655        TINYTEXT = auto()
3656        TIME = auto()
3657        TIMETZ = auto()
3658        TIMESTAMP = auto()
3659        TIMESTAMPLTZ = auto()
3660        TIMESTAMPTZ = auto()
3661        TIMESTAMP_S = auto()
3662        TIMESTAMP_MS = auto()
3663        TIMESTAMP_NS = auto()
3664        TINYINT = auto()
3665        TSMULTIRANGE = auto()
3666        TSRANGE = auto()
3667        TSTZMULTIRANGE = auto()
3668        TSTZRANGE = auto()
3669        UBIGINT = auto()
3670        UINT = auto()
3671        UINT128 = auto()
3672        UINT256 = auto()
3673        UMEDIUMINT = auto()
3674        UDECIMAL = auto()
3675        UNIQUEIDENTIFIER = auto()
3676        UNKNOWN = auto()  # Sentinel value, useful for type annotation
3677        USERDEFINED = "USER-DEFINED"
3678        USMALLINT = auto()
3679        UTINYINT = auto()
3680        UUID = auto()
3681        VARBINARY = auto()
3682        VARCHAR = auto()
3683        VARIANT = auto()
3684        XML = auto()
3685        YEAR = auto()
3686
3687    TEXT_TYPES = {
3688        Type.CHAR,
3689        Type.NCHAR,
3690        Type.VARCHAR,
3691        Type.NVARCHAR,
3692        Type.TEXT,
3693    }
3694
3695    INTEGER_TYPES = {
3696        Type.INT,
3697        Type.TINYINT,
3698        Type.SMALLINT,
3699        Type.BIGINT,
3700        Type.INT128,
3701        Type.INT256,
3702        Type.BIT,
3703    }
3704
3705    FLOAT_TYPES = {
3706        Type.FLOAT,
3707        Type.DOUBLE,
3708    }
3709
3710    NUMERIC_TYPES = {
3711        *INTEGER_TYPES,
3712        *FLOAT_TYPES,
3713    }
3714
3715    TEMPORAL_TYPES = {
3716        Type.TIME,
3717        Type.TIMETZ,
3718        Type.TIMESTAMP,
3719        Type.TIMESTAMPTZ,
3720        Type.TIMESTAMPLTZ,
3721        Type.TIMESTAMP_S,
3722        Type.TIMESTAMP_MS,
3723        Type.TIMESTAMP_NS,
3724        Type.DATE,
3725        Type.DATETIME,
3726        Type.DATETIME64,
3727    }
3728
3729    @classmethod
3730    def build(
3731        cls,
3732        dtype: DATA_TYPE,
3733        dialect: DialectType = None,
3734        udt: bool = False,
3735        **kwargs,
3736    ) -> DataType:
3737        """
3738        Constructs a DataType object.
3739
3740        Args:
3741            dtype: the data type of interest.
3742            dialect: the dialect to use for parsing `dtype`, in case it's a string.
3743            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
3744                DataType, thus creating a user-defined type.
3745            kawrgs: additional arguments to pass in the constructor of DataType.
3746
3747        Returns:
3748            The constructed DataType object.
3749        """
3750        from sqlglot import parse_one
3751
3752        if isinstance(dtype, str):
3753            if dtype.upper() == "UNKNOWN":
3754                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
3755
3756            try:
3757                data_type_exp = parse_one(
3758                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
3759                )
3760            except ParseError:
3761                if udt:
3762                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
3763                raise
3764        elif isinstance(dtype, DataType.Type):
3765            data_type_exp = DataType(this=dtype)
3766        elif isinstance(dtype, DataType):
3767            return dtype
3768        else:
3769            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
3770
3771        return DataType(**{**data_type_exp.args, **kwargs})
3772
3773    def is_type(self, *dtypes: DATA_TYPE) -> bool:
3774        """
3775        Checks whether this DataType matches one of the provided data types. Nested types or precision
3776        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
3777
3778        Args:
3779            dtypes: the data types to compare this DataType to.
3780
3781        Returns:
3782            True, if and only if there is a type in `dtypes` which is equal to this DataType.
3783        """
3784        for dtype in dtypes:
3785            other = DataType.build(dtype, udt=True)
3786
3787            if (
3788                other.expressions
3789                or self.this == DataType.Type.USERDEFINED
3790                or other.this == DataType.Type.USERDEFINED
3791            ):
3792                matches = self == other
3793            else:
3794                matches = self.this == other.this
3795
3796            if matches:
3797                return True
3798        return False
arg_types = {'this': True, 'expressions': False, 'nested': False, 'values': False, 'prefix': False, 'kind': False}
TEXT_TYPES = {<Type.VARCHAR: 'VARCHAR'>, <Type.CHAR: 'CHAR'>, <Type.NVARCHAR: 'NVARCHAR'>, <Type.TEXT: 'TEXT'>, <Type.NCHAR: 'NCHAR'>}
INTEGER_TYPES = {<Type.INT: 'INT'>, <Type.BIGINT: 'BIGINT'>, <Type.BIT: 'BIT'>, <Type.SMALLINT: 'SMALLINT'>, <Type.TINYINT: 'TINYINT'>, <Type.INT256: 'INT256'>, <Type.INT128: 'INT128'>}
FLOAT_TYPES = {<Type.DOUBLE: 'DOUBLE'>, <Type.FLOAT: 'FLOAT'>}
NUMERIC_TYPES = {<Type.DOUBLE: 'DOUBLE'>, <Type.INT: 'INT'>, <Type.BIGINT: 'BIGINT'>, <Type.BIT: 'BIT'>, <Type.SMALLINT: 'SMALLINT'>, <Type.FLOAT: 'FLOAT'>, <Type.TINYINT: 'TINYINT'>, <Type.INT256: 'INT256'>, <Type.INT128: 'INT128'>}
TEMPORAL_TYPES = {<Type.TIMESTAMPLTZ: 'TIMESTAMPLTZ'>, <Type.TIMESTAMPTZ: 'TIMESTAMPTZ'>, <Type.TIMESTAMP_NS: 'TIMESTAMP_NS'>, <Type.DATETIME64: 'DATETIME64'>, <Type.TIMESTAMP_S: 'TIMESTAMP_S'>, <Type.DATETIME: 'DATETIME'>, <Type.TIME: 'TIME'>, <Type.DATE: 'DATE'>, <Type.TIMESTAMP_MS: 'TIMESTAMP_MS'>, <Type.TIMETZ: 'TIMETZ'>, <Type.TIMESTAMP: 'TIMESTAMP'>}
@classmethod
def build( cls, dtype: Union[str, DataType, DataType.Type], dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, udt: bool = False, **kwargs) -> DataType:
3729    @classmethod
3730    def build(
3731        cls,
3732        dtype: DATA_TYPE,
3733        dialect: DialectType = None,
3734        udt: bool = False,
3735        **kwargs,
3736    ) -> DataType:
3737        """
3738        Constructs a DataType object.
3739
3740        Args:
3741            dtype: the data type of interest.
3742            dialect: the dialect to use for parsing `dtype`, in case it's a string.
3743            udt: when set to True, `dtype` will be used as-is if it can't be parsed into a
3744                DataType, thus creating a user-defined type.
3745            kawrgs: additional arguments to pass in the constructor of DataType.
3746
3747        Returns:
3748            The constructed DataType object.
3749        """
3750        from sqlglot import parse_one
3751
3752        if isinstance(dtype, str):
3753            if dtype.upper() == "UNKNOWN":
3754                return DataType(this=DataType.Type.UNKNOWN, **kwargs)
3755
3756            try:
3757                data_type_exp = parse_one(
3758                    dtype, read=dialect, into=DataType, error_level=ErrorLevel.IGNORE
3759                )
3760            except ParseError:
3761                if udt:
3762                    return DataType(this=DataType.Type.USERDEFINED, kind=dtype, **kwargs)
3763                raise
3764        elif isinstance(dtype, DataType.Type):
3765            data_type_exp = DataType(this=dtype)
3766        elif isinstance(dtype, DataType):
3767            return dtype
3768        else:
3769            raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type")
3770
3771        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:
3773    def is_type(self, *dtypes: DATA_TYPE) -> bool:
3774        """
3775        Checks whether this DataType matches one of the provided data types. Nested types or precision
3776        will be compared using "structural equivalence" semantics, so e.g. array<int> != array<float>.
3777
3778        Args:
3779            dtypes: the data types to compare this DataType to.
3780
3781        Returns:
3782            True, if and only if there is a type in `dtypes` which is equal to this DataType.
3783        """
3784        for dtype in dtypes:
3785            other = DataType.build(dtype, udt=True)
3786
3787            if (
3788                other.expressions
3789                or self.this == DataType.Type.USERDEFINED
3790                or other.this == DataType.Type.USERDEFINED
3791            ):
3792                matches = self == other
3793            else:
3794                matches = self.this == other.this
3795
3796            if matches:
3797                return True
3798        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):
3590    class Type(AutoName):
3591        ARRAY = auto()
3592        BIGDECIMAL = auto()
3593        BIGINT = auto()
3594        BIGSERIAL = auto()
3595        BINARY = auto()
3596        BIT = auto()
3597        BOOLEAN = auto()
3598        CHAR = auto()
3599        DATE = auto()
3600        DATEMULTIRANGE = auto()
3601        DATERANGE = auto()
3602        DATETIME = auto()
3603        DATETIME64 = auto()
3604        DECIMAL = auto()
3605        DOUBLE = auto()
3606        ENUM = auto()
3607        ENUM8 = auto()
3608        ENUM16 = auto()
3609        FIXEDSTRING = auto()
3610        FLOAT = auto()
3611        GEOGRAPHY = auto()
3612        GEOMETRY = auto()
3613        HLLSKETCH = auto()
3614        HSTORE = auto()
3615        IMAGE = auto()
3616        INET = auto()
3617        INT = auto()
3618        INT128 = auto()
3619        INT256 = auto()
3620        INT4MULTIRANGE = auto()
3621        INT4RANGE = auto()
3622        INT8MULTIRANGE = auto()
3623        INT8RANGE = auto()
3624        INTERVAL = auto()
3625        IPADDRESS = auto()
3626        IPPREFIX = auto()
3627        JSON = auto()
3628        JSONB = auto()
3629        LONGBLOB = auto()
3630        LONGTEXT = auto()
3631        LOWCARDINALITY = auto()
3632        MAP = auto()
3633        MEDIUMBLOB = auto()
3634        MEDIUMINT = auto()
3635        MEDIUMTEXT = auto()
3636        MONEY = auto()
3637        NCHAR = auto()
3638        NESTED = auto()
3639        NULL = auto()
3640        NULLABLE = auto()
3641        NUMMULTIRANGE = auto()
3642        NUMRANGE = auto()
3643        NVARCHAR = auto()
3644        OBJECT = auto()
3645        ROWVERSION = auto()
3646        SERIAL = auto()
3647        SET = auto()
3648        SMALLINT = auto()
3649        SMALLMONEY = auto()
3650        SMALLSERIAL = auto()
3651        STRUCT = auto()
3652        SUPER = auto()
3653        TEXT = auto()
3654        TINYBLOB = auto()
3655        TINYTEXT = auto()
3656        TIME = auto()
3657        TIMETZ = auto()
3658        TIMESTAMP = auto()
3659        TIMESTAMPLTZ = auto()
3660        TIMESTAMPTZ = auto()
3661        TIMESTAMP_S = auto()
3662        TIMESTAMP_MS = auto()
3663        TIMESTAMP_NS = auto()
3664        TINYINT = auto()
3665        TSMULTIRANGE = auto()
3666        TSRANGE = auto()
3667        TSTZMULTIRANGE = auto()
3668        TSTZRANGE = auto()
3669        UBIGINT = auto()
3670        UINT = auto()
3671        UINT128 = auto()
3672        UINT256 = auto()
3673        UMEDIUMINT = auto()
3674        UDECIMAL = auto()
3675        UNIQUEIDENTIFIER = auto()
3676        UNKNOWN = auto()  # Sentinel value, useful for type annotation
3677        USERDEFINED = "USER-DEFINED"
3678        USMALLINT = auto()
3679        UTINYINT = auto()
3680        UUID = auto()
3681        VARBINARY = auto()
3682        VARCHAR = auto()
3683        VARIANT = auto()
3684        XML = auto()
3685        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):
3805class PseudoType(DataType):
3806    arg_types = {"this": True}
arg_types = {'this': True}
key = 'pseudotype'
class ObjectIdentifier(DataType):
3810class ObjectIdentifier(DataType):
3811    arg_types = {"this": True}
arg_types = {'this': True}
key = 'objectidentifier'
class SubqueryPredicate(Predicate):
3815class SubqueryPredicate(Predicate):
3816    pass
key = 'subquerypredicate'
class All(SubqueryPredicate):
3819class All(SubqueryPredicate):
3820    pass
key = 'all'
class Any(SubqueryPredicate):
3823class Any(SubqueryPredicate):
3824    pass
key = 'any'
class Exists(SubqueryPredicate):
3827class Exists(SubqueryPredicate):
3828    pass
key = 'exists'
class Command(Expression):
3833class Command(Expression):
3834    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'command'
class Transaction(Expression):
3837class Transaction(Expression):
3838    arg_types = {"this": False, "modes": False, "mark": False}
arg_types = {'this': False, 'modes': False, 'mark': False}
key = 'transaction'
class Commit(Expression):
3841class Commit(Expression):
3842    arg_types = {"chain": False, "this": False, "durability": False}
arg_types = {'chain': False, 'this': False, 'durability': False}
key = 'commit'
class Rollback(Expression):
3845class Rollback(Expression):
3846    arg_types = {"savepoint": False, "this": False}
arg_types = {'savepoint': False, 'this': False}
key = 'rollback'
class AlterTable(Expression):
3849class AlterTable(Expression):
3850    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):
3853class AddConstraint(Expression):
3854    arg_types = {"this": False, "expression": False, "enforced": False}
arg_types = {'this': False, 'expression': False, 'enforced': False}
key = 'addconstraint'
class DropPartition(Expression):
3857class DropPartition(Expression):
3858    arg_types = {"expressions": True, "exists": False}
arg_types = {'expressions': True, 'exists': False}
key = 'droppartition'
class Binary(Condition):
3862class Binary(Condition):
3863    arg_types = {"this": True, "expression": True}
3864
3865    @property
3866    def left(self) -> Expression:
3867        return self.this
3868
3869    @property
3870    def right(self) -> Expression:
3871        return self.expression
arg_types = {'this': True, 'expression': True}
left: Expression
right: Expression
key = 'binary'
class Add(Binary):
3874class Add(Binary):
3875    pass
key = 'add'
class Connector(Binary):
3878class Connector(Binary):
3879    pass
key = 'connector'
class And(Connector):
3882class And(Connector):
3883    pass
key = 'and'
class Or(Connector):
3886class Or(Connector):
3887    pass
key = 'or'
class BitwiseAnd(Binary):
3890class BitwiseAnd(Binary):
3891    pass
key = 'bitwiseand'
class BitwiseLeftShift(Binary):
3894class BitwiseLeftShift(Binary):
3895    pass
key = 'bitwiseleftshift'
class BitwiseOr(Binary):
3898class BitwiseOr(Binary):
3899    pass
key = 'bitwiseor'
class BitwiseRightShift(Binary):
3902class BitwiseRightShift(Binary):
3903    pass
key = 'bitwiserightshift'
class BitwiseXor(Binary):
3906class BitwiseXor(Binary):
3907    pass
key = 'bitwisexor'
class Div(Binary):
3910class Div(Binary):
3911    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):
3914class Overlaps(Binary):
3915    pass
key = 'overlaps'
class Dot(Binary):
3918class Dot(Binary):
3919    @property
3920    def name(self) -> str:
3921        return self.expression.name
3922
3923    @property
3924    def output_name(self) -> str:
3925        return self.name
3926
3927    @classmethod
3928    def build(self, expressions: t.Sequence[Expression]) -> Dot:
3929        """Build a Dot object with a sequence of expressions."""
3930        if len(expressions) < 2:
3931            raise ValueError(f"Dot requires >= 2 expressions.")
3932
3933        return t.cast(Dot, reduce(lambda x, y: Dot(this=x, expression=y), expressions))
3934
3935    @property
3936    def parts(self) -> t.List[Expression]:
3937        """Return the parts of a table / column in order catalog, db, table."""
3938        this, *parts = self.flatten()
3939
3940        parts.reverse()
3941
3942        for arg in ("this", "table", "db", "catalog"):
3943            part = this.args.get(arg)
3944
3945            if isinstance(part, Expression):
3946                parts.append(part)
3947
3948        parts.reverse()
3949        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:
3927    @classmethod
3928    def build(self, expressions: t.Sequence[Expression]) -> Dot:
3929        """Build a Dot object with a sequence of expressions."""
3930        if len(expressions) < 2:
3931            raise ValueError(f"Dot requires >= 2 expressions.")
3932
3933        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):
3952class DPipe(Binary):
3953    arg_types = {"this": True, "expression": True, "safe": False}
arg_types = {'this': True, 'expression': True, 'safe': False}
key = 'dpipe'
class EQ(Binary, Predicate):
3956class EQ(Binary, Predicate):
3957    pass
key = 'eq'
class NullSafeEQ(Binary, Predicate):
3960class NullSafeEQ(Binary, Predicate):
3961    pass
key = 'nullsafeeq'
class NullSafeNEQ(Binary, Predicate):
3964class NullSafeNEQ(Binary, Predicate):
3965    pass
key = 'nullsafeneq'
class PropertyEQ(Binary):
3969class PropertyEQ(Binary):
3970    pass
key = 'propertyeq'
class Distance(Binary):
3973class Distance(Binary):
3974    pass
key = 'distance'
class Escape(Binary):
3977class Escape(Binary):
3978    pass
key = 'escape'
class Glob(Binary, Predicate):
3981class Glob(Binary, Predicate):
3982    pass
key = 'glob'
class GT(Binary, Predicate):
3985class GT(Binary, Predicate):
3986    pass
key = 'gt'
class GTE(Binary, Predicate):
3989class GTE(Binary, Predicate):
3990    pass
key = 'gte'
class ILike(Binary, Predicate):
3993class ILike(Binary, Predicate):
3994    pass
key = 'ilike'
class ILikeAny(Binary, Predicate):
3997class ILikeAny(Binary, Predicate):
3998    pass
key = 'ilikeany'
class IntDiv(Binary):
4001class IntDiv(Binary):
4002    pass
key = 'intdiv'
class Is(Binary, Predicate):
4005class Is(Binary, Predicate):
4006    pass
key = 'is'
class Kwarg(Binary):
4009class Kwarg(Binary):
4010    """Kwarg in special functions like func(kwarg => y)."""

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

key = 'kwarg'
class Like(Binary, Predicate):
4013class Like(Binary, Predicate):
4014    pass
key = 'like'
class LikeAny(Binary, Predicate):
4017class LikeAny(Binary, Predicate):
4018    pass
key = 'likeany'
class LT(Binary, Predicate):
4021class LT(Binary, Predicate):
4022    pass
key = 'lt'
class LTE(Binary, Predicate):
4025class LTE(Binary, Predicate):
4026    pass
key = 'lte'
class Mod(Binary):
4029class Mod(Binary):
4030    pass
key = 'mod'
class Mul(Binary):
4033class Mul(Binary):
4034    pass
key = 'mul'
class NEQ(Binary, Predicate):
4037class NEQ(Binary, Predicate):
4038    pass
key = 'neq'
class Operator(Binary):
4042class Operator(Binary):
4043    arg_types = {"this": True, "operator": True, "expression": True}
arg_types = {'this': True, 'operator': True, 'expression': True}
key = 'operator'
class SimilarTo(Binary, Predicate):
4046class SimilarTo(Binary, Predicate):
4047    pass
key = 'similarto'
class Slice(Binary):
4050class Slice(Binary):
4051    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'slice'
class Sub(Binary):
4054class Sub(Binary):
4055    pass
key = 'sub'
class ArrayOverlaps(Binary):
4058class ArrayOverlaps(Binary):
4059    pass
key = 'arrayoverlaps'
class Unary(Condition):
4064class Unary(Condition):
4065    pass
key = 'unary'
class BitwiseNot(Unary):
4068class BitwiseNot(Unary):
4069    pass
key = 'bitwisenot'
class Not(Unary):
4072class Not(Unary):
4073    pass
key = 'not'
class Paren(Unary):
4076class Paren(Unary):
4077    arg_types = {"this": True, "with": False}
4078
4079    @property
4080    def output_name(self) -> str:
4081        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):
4084class Neg(Unary):
4085    pass
key = 'neg'
class Alias(Expression):
4088class Alias(Expression):
4089    arg_types = {"this": True, "alias": False}
4090
4091    @property
4092    def output_name(self) -> str:
4093        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):
4096class Aliases(Expression):
4097    arg_types = {"this": True, "expressions": True}
4098
4099    @property
4100    def aliases(self):
4101        return self.expressions
arg_types = {'this': True, 'expressions': True}
aliases
key = 'aliases'
class AtTimeZone(Expression):
4104class AtTimeZone(Expression):
4105    arg_types = {"this": True, "zone": True}
arg_types = {'this': True, 'zone': True}
key = 'attimezone'
class Between(Predicate):
4108class Between(Predicate):
4109    arg_types = {"this": True, "low": True, "high": True}
arg_types = {'this': True, 'low': True, 'high': True}
key = 'between'
class Bracket(Condition):
4112class Bracket(Condition):
4113    # https://cloud.google.com/bigquery/docs/reference/standard-sql/operators#array_subscript_operator
4114    arg_types = {"this": True, "expressions": True, "offset": False, "safe": False}
4115
4116    @property
4117    def output_name(self) -> str:
4118        if len(self.expressions) == 1:
4119            return self.expressions[0].output_name
4120
4121        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):
4124class Distinct(Expression):
4125    arg_types = {"expressions": False, "on": False}
arg_types = {'expressions': False, 'on': False}
key = 'distinct'
class In(Predicate):
4128class In(Predicate):
4129    arg_types = {
4130        "this": True,
4131        "expressions": False,
4132        "query": False,
4133        "unnest": False,
4134        "field": False,
4135        "is_global": False,
4136    }
arg_types = {'this': True, 'expressions': False, 'query': False, 'unnest': False, 'field': False, 'is_global': False}
key = 'in'
class ForIn(Expression):
4140class ForIn(Expression):
4141    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'forin'
class TimeUnit(Expression):
4144class TimeUnit(Expression):
4145    """Automatically converts unit arg into a var."""
4146
4147    arg_types = {"unit": False}
4148
4149    UNABBREVIATED_UNIT_NAME = {
4150        "d": "day",
4151        "h": "hour",
4152        "m": "minute",
4153        "ms": "millisecond",
4154        "ns": "nanosecond",
4155        "q": "quarter",
4156        "s": "second",
4157        "us": "microsecond",
4158        "w": "week",
4159        "y": "year",
4160    }
4161
4162    VAR_LIKE = (Column, Literal, Var)
4163
4164    def __init__(self, **args):
4165        unit = args.get("unit")
4166        if isinstance(unit, self.VAR_LIKE):
4167            args["unit"] = Var(this=self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name)
4168        elif isinstance(unit, Week):
4169            unit.set("this", Var(this=unit.this.name))
4170
4171        super().__init__(**args)
4172
4173    @property
4174    def unit(self) -> t.Optional[Var]:
4175        return self.args.get("unit")

Automatically converts unit arg into a var.

TimeUnit(**args)
4164    def __init__(self, **args):
4165        unit = args.get("unit")
4166        if isinstance(unit, self.VAR_LIKE):
4167            args["unit"] = Var(this=self.UNABBREVIATED_UNIT_NAME.get(unit.name) or unit.name)
4168        elif isinstance(unit, Week):
4169            unit.set("this", Var(this=unit.this.name))
4170
4171        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):
4178class IntervalOp(TimeUnit):
4179    arg_types = {"unit": True, "expression": True}
4180
4181    def interval(self):
4182        return Interval(
4183            this=self.expression.copy(),
4184            unit=self.unit.copy(),
4185        )
arg_types = {'unit': True, 'expression': True}
def interval(self):
4181    def interval(self):
4182        return Interval(
4183            this=self.expression.copy(),
4184            unit=self.unit.copy(),
4185        )
key = 'intervalop'
class IntervalSpan(DataType):
4191class IntervalSpan(DataType):
4192    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'intervalspan'
class Interval(TimeUnit):
4195class Interval(TimeUnit):
4196    arg_types = {"this": False, "unit": False}
arg_types = {'this': False, 'unit': False}
key = 'interval'
class IgnoreNulls(Expression):
4199class IgnoreNulls(Expression):
4200    pass
key = 'ignorenulls'
class RespectNulls(Expression):
4203class RespectNulls(Expression):
4204    pass
key = 'respectnulls'
class Func(Condition):
4208class Func(Condition):
4209    """
4210    The base class for all function expressions.
4211
4212    Attributes:
4213        is_var_len_args (bool): if set to True the last argument defined in arg_types will be
4214            treated as a variable length argument and the argument's value will be stored as a list.
4215        _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items)
4216            for this function expression. These values are used to map this node to a name during parsing
4217            as well as to provide the function's name during SQL string generation. By default the SQL
4218            name is set to the expression's class name transformed to snake case.
4219    """
4220
4221    is_var_len_args = False
4222
4223    @classmethod
4224    def from_arg_list(cls, args):
4225        if cls.is_var_len_args:
4226            all_arg_keys = list(cls.arg_types)
4227            # If this function supports variable length argument treat the last argument as such.
4228            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
4229            num_non_var = len(non_var_len_arg_keys)
4230
4231            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
4232            args_dict[all_arg_keys[-1]] = args[num_non_var:]
4233        else:
4234            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
4235
4236        return cls(**args_dict)
4237
4238    @classmethod
4239    def sql_names(cls):
4240        if cls is Func:
4241            raise NotImplementedError(
4242                "SQL name is only supported by concrete function implementations"
4243            )
4244        if "_sql_names" not in cls.__dict__:
4245            cls._sql_names = [camel_to_snake_case(cls.__name__)]
4246        return cls._sql_names
4247
4248    @classmethod
4249    def sql_name(cls):
4250        return cls.sql_names()[0]
4251
4252    @classmethod
4253    def default_parser_mappings(cls):
4254        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):
4223    @classmethod
4224    def from_arg_list(cls, args):
4225        if cls.is_var_len_args:
4226            all_arg_keys = list(cls.arg_types)
4227            # If this function supports variable length argument treat the last argument as such.
4228            non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys
4229            num_non_var = len(non_var_len_arg_keys)
4230
4231            args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)}
4232            args_dict[all_arg_keys[-1]] = args[num_non_var:]
4233        else:
4234            args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)}
4235
4236        return cls(**args_dict)
@classmethod
def sql_names(cls):
4238    @classmethod
4239    def sql_names(cls):
4240        if cls is Func:
4241            raise NotImplementedError(
4242                "SQL name is only supported by concrete function implementations"
4243            )
4244        if "_sql_names" not in cls.__dict__:
4245            cls._sql_names = [camel_to_snake_case(cls.__name__)]
4246        return cls._sql_names
@classmethod
def sql_name(cls):
4248    @classmethod
4249    def sql_name(cls):
4250        return cls.sql_names()[0]
@classmethod
def default_parser_mappings(cls):
4252    @classmethod
4253    def default_parser_mappings(cls):
4254        return {name: cls.from_arg_list for name in cls.sql_names()}
key = 'func'
class AggFunc(Func):
4257class AggFunc(Func):
4258    pass
key = 'aggfunc'
class ParameterizedAgg(AggFunc):
4261class ParameterizedAgg(AggFunc):
4262    arg_types = {"this": True, "expressions": True, "params": True}
arg_types = {'this': True, 'expressions': True, 'params': True}
key = 'parameterizedagg'
class Abs(Func):
4265class Abs(Func):
4266    pass
key = 'abs'
class ArgMax(AggFunc):
4269class ArgMax(AggFunc):
4270    arg_types = {"this": True, "expression": True, "count": False}
4271    _sql_names = ["ARG_MAX", "ARGMAX", "MAX_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmax'
class ArgMin(AggFunc):
4274class ArgMin(AggFunc):
4275    arg_types = {"this": True, "expression": True, "count": False}
4276    _sql_names = ["ARG_MIN", "ARGMIN", "MIN_BY"]
arg_types = {'this': True, 'expression': True, 'count': False}
key = 'argmin'
class ApproxTopK(AggFunc):
4279class ApproxTopK(AggFunc):
4280    arg_types = {"this": True, "expression": False, "counters": False}
arg_types = {'this': True, 'expression': False, 'counters': False}
key = 'approxtopk'
class Flatten(Func):
4283class Flatten(Func):
4284    pass
key = 'flatten'
class Transform(Func):
4288class Transform(Func):
4289    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'transform'
class Anonymous(Func):
4292class Anonymous(Func):
4293    arg_types = {"this": True, "expressions": False}
4294    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'anonymous'
class Hll(AggFunc):
4299class Hll(AggFunc):
4300    arg_types = {"this": True, "expressions": False}
4301    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'hll'
class ApproxDistinct(AggFunc):
4304class ApproxDistinct(AggFunc):
4305    arg_types = {"this": True, "accuracy": False}
4306    _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
arg_types = {'this': True, 'accuracy': False}
key = 'approxdistinct'
class Array(Func):
4309class Array(Func):
4310    arg_types = {"expressions": False}
4311    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'array'
class ToChar(Func):
4316class ToChar(Func):
4317    arg_types = {"this": True, "format": False, "nlsparam": False}
arg_types = {'this': True, 'format': False, 'nlsparam': False}
key = 'tochar'
class GenerateSeries(Func):
4320class GenerateSeries(Func):
4321    arg_types = {"start": True, "end": True, "step": False}
arg_types = {'start': True, 'end': True, 'step': False}
key = 'generateseries'
class ArrayAgg(AggFunc):
4324class ArrayAgg(AggFunc):
4325    pass
key = 'arrayagg'
class ArrayUniqueAgg(AggFunc):
4328class ArrayUniqueAgg(AggFunc):
4329    pass
key = 'arrayuniqueagg'
class ArrayAll(Func):
4332class ArrayAll(Func):
4333    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayall'
class ArrayAny(Func):
4336class ArrayAny(Func):
4337    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'arrayany'
class ArrayConcat(Func):
4340class ArrayConcat(Func):
4341    _sql_names = ["ARRAY_CONCAT", "ARRAY_CAT"]
4342    arg_types = {"this": True, "expressions": False}
4343    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'arrayconcat'
class ArrayContains(Binary, Func):
4346class ArrayContains(Binary, Func):
4347    pass
key = 'arraycontains'
class ArrayContained(Binary):
4350class ArrayContained(Binary):
4351    pass
key = 'arraycontained'
class ArrayFilter(Func):
4354class ArrayFilter(Func):
4355    arg_types = {"this": True, "expression": True}
4356    _sql_names = ["FILTER", "ARRAY_FILTER"]
arg_types = {'this': True, 'expression': True}
key = 'arrayfilter'
class ArrayJoin(Func):
4359class ArrayJoin(Func):
4360    arg_types = {"this": True, "expression": True, "null": False}
arg_types = {'this': True, 'expression': True, 'null': False}
key = 'arrayjoin'
class ArraySize(Func):
4363class ArraySize(Func):
4364    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysize'
class ArraySort(Func):
4367class ArraySort(Func):
4368    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'arraysort'
class ArraySum(Func):
4371class ArraySum(Func):
4372    pass
key = 'arraysum'
class ArrayUnionAgg(AggFunc):
4375class ArrayUnionAgg(AggFunc):
4376    pass
key = 'arrayunionagg'
class Avg(AggFunc):
4379class Avg(AggFunc):
4380    pass
key = 'avg'
class AnyValue(AggFunc):
4383class AnyValue(AggFunc):
4384    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):
4387class First(Func):
4388    arg_types = {"this": True, "ignore_nulls": False}
arg_types = {'this': True, 'ignore_nulls': False}
key = 'first'
class Last(Func):
4391class Last(Func):
4392    arg_types = {"this": True, "ignore_nulls": False}
arg_types = {'this': True, 'ignore_nulls': False}
key = 'last'
class Case(Func):
4395class Case(Func):
4396    arg_types = {"this": False, "ifs": True, "default": False}
4397
4398    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
4399        instance = maybe_copy(self, copy)
4400        instance.append(
4401            "ifs",
4402            If(
4403                this=maybe_parse(condition, copy=copy, **opts),
4404                true=maybe_parse(then, copy=copy, **opts),
4405            ),
4406        )
4407        return instance
4408
4409    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
4410        instance = maybe_copy(self, copy)
4411        instance.set("default", maybe_parse(condition, copy=copy, **opts))
4412        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:
4398    def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case:
4399        instance = maybe_copy(self, copy)
4400        instance.append(
4401            "ifs",
4402            If(
4403                this=maybe_parse(condition, copy=copy, **opts),
4404                true=maybe_parse(then, copy=copy, **opts),
4405            ),
4406        )
4407        return instance
def else_( self, condition: Union[str, Expression], copy: bool = True, **opts) -> Case:
4409    def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case:
4410        instance = maybe_copy(self, copy)
4411        instance.set("default", maybe_parse(condition, copy=copy, **opts))
4412        return instance
key = 'case'
class Cast(Func):
4415class Cast(Func):
4416    arg_types = {"this": True, "to": True, "format": False, "safe": False}
4417
4418    @property
4419    def name(self) -> str:
4420        return self.this.name
4421
4422    @property
4423    def to(self) -> DataType:
4424        return self.args["to"]
4425
4426    @property
4427    def output_name(self) -> str:
4428        return self.name
4429
4430    def is_type(self, *dtypes: DATA_TYPE) -> bool:
4431        """
4432        Checks whether this Cast's DataType matches one of the provided data types. Nested types
4433        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
4434        array<int> != array<float>.
4435
4436        Args:
4437            dtypes: the data types to compare this Cast's DataType to.
4438
4439        Returns:
4440            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
4441        """
4442        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:
4430    def is_type(self, *dtypes: DATA_TYPE) -> bool:
4431        """
4432        Checks whether this Cast's DataType matches one of the provided data types. Nested types
4433        like arrays or structs will be compared using "structural equivalence" semantics, so e.g.
4434        array<int> != array<float>.
4435
4436        Args:
4437            dtypes: the data types to compare this Cast's DataType to.
4438
4439        Returns:
4440            True, if and only if there is a type in `dtypes` which is equal to this Cast's DataType.
4441        """
4442        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):
4445class TryCast(Cast):
4446    pass
key = 'trycast'
class CastToStrType(Func):
4449class CastToStrType(Func):
4450    arg_types = {"this": True, "to": True}
arg_types = {'this': True, 'to': True}
key = 'casttostrtype'
class Collate(Binary, Func):
4453class Collate(Binary, Func):
4454    pass
key = 'collate'
class Ceil(Func):
4457class Ceil(Func):
4458    arg_types = {"this": True, "decimals": False}
4459    _sql_names = ["CEIL", "CEILING"]
arg_types = {'this': True, 'decimals': False}
key = 'ceil'
class Coalesce(Func):
4462class Coalesce(Func):
4463    arg_types = {"this": True, "expressions": False}
4464    is_var_len_args = True
4465    _sql_names = ["COALESCE", "IFNULL", "NVL"]
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'coalesce'
class Chr(Func):
4468class Chr(Func):
4469    arg_types = {"this": True, "charset": False, "expressions": False}
4470    is_var_len_args = True
4471    _sql_names = ["CHR", "CHAR"]
arg_types = {'this': True, 'charset': False, 'expressions': False}
is_var_len_args = True
key = 'chr'
class Concat(Func):
4474class Concat(Func):
4475    arg_types = {"expressions": True, "safe": False}
4476    is_var_len_args = True
arg_types = {'expressions': True, 'safe': False}
is_var_len_args = True
key = 'concat'
class ConcatWs(Concat):
4479class ConcatWs(Concat):
4480    _sql_names = ["CONCAT_WS"]
key = 'concatws'
class Count(AggFunc):
4483class Count(AggFunc):
4484    arg_types = {"this": False, "expressions": False}
4485    is_var_len_args = True
arg_types = {'this': False, 'expressions': False}
is_var_len_args = True
key = 'count'
class CountIf(AggFunc):
4488class CountIf(AggFunc):
4489    pass
key = 'countif'
class CurrentDate(Func):
4492class CurrentDate(Func):
4493    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdate'
class CurrentDatetime(Func):
4496class CurrentDatetime(Func):
4497    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentdatetime'
class CurrentTime(Func):
4500class CurrentTime(Func):
4501    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttime'
class CurrentTimestamp(Func):
4504class CurrentTimestamp(Func):
4505    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currenttimestamp'
class CurrentUser(Func):
4508class CurrentUser(Func):
4509    arg_types = {"this": False}
arg_types = {'this': False}
key = 'currentuser'
class DateAdd(Func, IntervalOp):
4512class DateAdd(Func, IntervalOp):
4513    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'dateadd'
class DateSub(Func, IntervalOp):
4516class DateSub(Func, IntervalOp):
4517    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datesub'
class DateDiff(Func, TimeUnit):
4520class DateDiff(Func, TimeUnit):
4521    _sql_names = ["DATEDIFF", "DATE_DIFF"]
4522    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datediff'
class DateTrunc(Func):
4525class DateTrunc(Func):
4526    arg_types = {"unit": True, "this": True, "zone": False}
4527
4528    @property
4529    def unit(self) -> Expression:
4530        return self.args["unit"]
arg_types = {'unit': True, 'this': True, 'zone': False}
unit: Expression
key = 'datetrunc'
class DatetimeAdd(Func, IntervalOp):
4533class DatetimeAdd(Func, IntervalOp):
4534    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimeadd'
class DatetimeSub(Func, IntervalOp):
4537class DatetimeSub(Func, IntervalOp):
4538    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimesub'
class DatetimeDiff(Func, TimeUnit):
4541class DatetimeDiff(Func, TimeUnit):
4542    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'datetimediff'
class DatetimeTrunc(Func, TimeUnit):
4545class DatetimeTrunc(Func, TimeUnit):
4546    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'datetimetrunc'
class DayOfWeek(Func):
4549class DayOfWeek(Func):
4550    _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"]
key = 'dayofweek'
class DayOfMonth(Func):
4553class DayOfMonth(Func):
4554    _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"]
key = 'dayofmonth'
class DayOfYear(Func):
4557class DayOfYear(Func):
4558    _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"]
key = 'dayofyear'
class ToDays(Func):
4561class ToDays(Func):
4562    pass
key = 'todays'
class WeekOfYear(Func):
4565class WeekOfYear(Func):
4566    _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"]
key = 'weekofyear'
class MonthsBetween(Func):
4569class MonthsBetween(Func):
4570    arg_types = {"this": True, "expression": True, "roundoff": False}
arg_types = {'this': True, 'expression': True, 'roundoff': False}
key = 'monthsbetween'
class LastDateOfMonth(Func):
4573class LastDateOfMonth(Func):
4574    pass
key = 'lastdateofmonth'
class Extract(Func):
4577class Extract(Func):
4578    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'extract'
class Timestamp(Func):
4581class Timestamp(Func):
4582    arg_types = {"this": False, "expression": False}
arg_types = {'this': False, 'expression': False}
key = 'timestamp'
class TimestampAdd(Func, TimeUnit):
4585class TimestampAdd(Func, TimeUnit):
4586    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampadd'
class TimestampSub(Func, TimeUnit):
4589class TimestampSub(Func, TimeUnit):
4590    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampsub'
class TimestampDiff(Func, TimeUnit):
4593class TimestampDiff(Func, TimeUnit):
4594    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timestampdiff'
class TimestampTrunc(Func, TimeUnit):
4597class TimestampTrunc(Func, TimeUnit):
4598    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timestamptrunc'
class TimeAdd(Func, TimeUnit):
4601class TimeAdd(Func, TimeUnit):
4602    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timeadd'
class TimeSub(Func, TimeUnit):
4605class TimeSub(Func, TimeUnit):
4606    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timesub'
class TimeDiff(Func, TimeUnit):
4609class TimeDiff(Func, TimeUnit):
4610    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'timediff'
class TimeTrunc(Func, TimeUnit):
4613class TimeTrunc(Func, TimeUnit):
4614    arg_types = {"this": True, "unit": True, "zone": False}
arg_types = {'this': True, 'unit': True, 'zone': False}
key = 'timetrunc'
class DateFromParts(Func):
4617class DateFromParts(Func):
4618    _sql_names = ["DATEFROMPARTS"]
4619    arg_types = {"year": True, "month": True, "day": True}
arg_types = {'year': True, 'month': True, 'day': True}
key = 'datefromparts'
class DateStrToDate(Func):
4622class DateStrToDate(Func):
4623    pass
key = 'datestrtodate'
class DateToDateStr(Func):
4626class DateToDateStr(Func):
4627    pass
key = 'datetodatestr'
class DateToDi(Func):
4630class DateToDi(Func):
4631    pass
key = 'datetodi'
class Date(Func):
4635class Date(Func):
4636    arg_types = {"this": False, "zone": False, "expressions": False}
4637    is_var_len_args = True
arg_types = {'this': False, 'zone': False, 'expressions': False}
is_var_len_args = True
key = 'date'
class Day(Func):
4640class Day(Func):
4641    pass
key = 'day'
class Decode(Func):
4644class Decode(Func):
4645    arg_types = {"this": True, "charset": True, "replace": False}
arg_types = {'this': True, 'charset': True, 'replace': False}
key = 'decode'
class DiToDate(Func):
4648class DiToDate(Func):
4649    pass
key = 'ditodate'
class Encode(Func):
4652class Encode(Func):
4653    arg_types = {"this": True, "charset": True}
arg_types = {'this': True, 'charset': True}
key = 'encode'
class Exp(Func):
4656class Exp(Func):
4657    pass
key = 'exp'
class Explode(Func):
4661class Explode(Func):
4662    arg_types = {"this": True, "expressions": False}
4663    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'explode'
class ExplodeOuter(Explode):
4666class ExplodeOuter(Explode):
4667    pass
key = 'explodeouter'
class Posexplode(Explode):
4670class Posexplode(Explode):
4671    pass
key = 'posexplode'
class PosexplodeOuter(Posexplode):
4674class PosexplodeOuter(Posexplode):
4675    pass
key = 'posexplodeouter'
class Floor(Func):
4678class Floor(Func):
4679    arg_types = {"this": True, "decimals": False}
arg_types = {'this': True, 'decimals': False}
key = 'floor'
class FromBase64(Func):
4682class FromBase64(Func):
4683    pass
key = 'frombase64'
class ToBase64(Func):
4686class ToBase64(Func):
4687    pass
key = 'tobase64'
class Greatest(Func):
4690class Greatest(Func):
4691    arg_types = {"this": True, "expressions": False}
4692    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'greatest'
class GroupConcat(AggFunc):
4695class GroupConcat(AggFunc):
4696    arg_types = {"this": True, "separator": False}
arg_types = {'this': True, 'separator': False}
key = 'groupconcat'
class Hex(Func):
4699class Hex(Func):
4700    pass
key = 'hex'
class Xor(Connector, Func):
4703class Xor(Connector, Func):
4704    arg_types = {"this": False, "expression": False, "expressions": False}
arg_types = {'this': False, 'expression': False, 'expressions': False}
key = 'xor'
class If(Func):
4707class If(Func):
4708    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'if'
class Nullif(Func):
4711class Nullif(Func):
4712    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'nullif'
class Initcap(Func):
4715class Initcap(Func):
4716    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'initcap'
class IsNan(Func):
4719class IsNan(Func):
4720    _sql_names = ["IS_NAN", "ISNAN"]
key = 'isnan'
class IsInf(Func):
4723class IsInf(Func):
4724    _sql_names = ["IS_INF", "ISINF"]
key = 'isinf'
class FormatJson(Expression):
4727class FormatJson(Expression):
4728    pass
key = 'formatjson'
class JSONKeyValue(Expression):
4731class JSONKeyValue(Expression):
4732    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'jsonkeyvalue'
class JSONObject(Func):
4735class JSONObject(Func):
4736    arg_types = {
4737        "expressions": False,
4738        "null_handling": False,
4739        "unique_keys": False,
4740        "return_type": False,
4741        "encoding": False,
4742    }
arg_types = {'expressions': False, 'null_handling': False, 'unique_keys': False, 'return_type': False, 'encoding': False}
key = 'jsonobject'
class JSONArray(Func):
4746class JSONArray(Func):
4747    arg_types = {
4748        "expressions": True,
4749        "null_handling": False,
4750        "return_type": False,
4751        "strict": False,
4752    }
arg_types = {'expressions': True, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarray'
class JSONArrayAgg(Func):
4756class JSONArrayAgg(Func):
4757    arg_types = {
4758        "this": True,
4759        "order": False,
4760        "null_handling": False,
4761        "return_type": False,
4762        "strict": False,
4763    }
arg_types = {'this': True, 'order': False, 'null_handling': False, 'return_type': False, 'strict': False}
key = 'jsonarrayagg'
class JSONColumnDef(Expression):
4768class JSONColumnDef(Expression):
4769    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):
4772class JSONSchema(Expression):
4773    arg_types = {"expressions": True}
arg_types = {'expressions': True}
key = 'jsonschema'
class JSONTable(Func):
4777class JSONTable(Func):
4778    arg_types = {
4779        "this": True,
4780        "schema": True,
4781        "path": False,
4782        "error_handling": False,
4783        "empty_handling": False,
4784    }
arg_types = {'this': True, 'schema': True, 'path': False, 'error_handling': False, 'empty_handling': False}
key = 'jsontable'
class OpenJSONColumnDef(Expression):
4787class OpenJSONColumnDef(Expression):
4788    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):
4791class OpenJSON(Func):
4792    arg_types = {"this": True, "path": False, "expressions": False}
arg_types = {'this': True, 'path': False, 'expressions': False}
key = 'openjson'
class JSONBContains(Binary):
4795class JSONBContains(Binary):
4796    _sql_names = ["JSONB_CONTAINS"]
key = 'jsonbcontains'
class JSONExtract(Binary, Func):
4799class JSONExtract(Binary, Func):
4800    _sql_names = ["JSON_EXTRACT"]
key = 'jsonextract'
class JSONExtractScalar(JSONExtract):
4803class JSONExtractScalar(JSONExtract):
4804    _sql_names = ["JSON_EXTRACT_SCALAR"]
key = 'jsonextractscalar'
class JSONBExtract(JSONExtract):
4807class JSONBExtract(JSONExtract):
4808    _sql_names = ["JSONB_EXTRACT"]
key = 'jsonbextract'
class JSONBExtractScalar(JSONExtract):
4811class JSONBExtractScalar(JSONExtract):
4812    _sql_names = ["JSONB_EXTRACT_SCALAR"]
key = 'jsonbextractscalar'
class JSONFormat(Func):
4815class JSONFormat(Func):
4816    arg_types = {"this": False, "options": False}
4817    _sql_names = ["JSON_FORMAT"]
arg_types = {'this': False, 'options': False}
key = 'jsonformat'
class JSONArrayContains(Binary, Predicate, Func):
4821class JSONArrayContains(Binary, Predicate, Func):
4822    _sql_names = ["JSON_ARRAY_CONTAINS"]
key = 'jsonarraycontains'
class ParseJSON(Func):
4825class ParseJSON(Func):
4826    # BigQuery, Snowflake have PARSE_JSON, Presto has JSON_PARSE
4827    _sql_names = ["PARSE_JSON", "JSON_PARSE"]
4828    arg_types = {"this": True, "expressions": False}
4829    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'parsejson'
class Least(Func):
4832class Least(Func):
4833    arg_types = {"this": True, "expressions": False}
4834    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'least'
class Left(Func):
4837class Left(Func):
4838    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'left'
class Length(Func):
4845class Length(Func):
4846    _sql_names = ["LENGTH", "LEN"]
key = 'length'
class Levenshtein(Func):
4849class Levenshtein(Func):
4850    arg_types = {
4851        "this": True,
4852        "expression": False,
4853        "ins_cost": False,
4854        "del_cost": False,
4855        "sub_cost": False,
4856    }
arg_types = {'this': True, 'expression': False, 'ins_cost': False, 'del_cost': False, 'sub_cost': False}
key = 'levenshtein'
class Ln(Func):
4859class Ln(Func):
4860    pass
key = 'ln'
class Log(Func):
4863class Log(Func):
4864    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'log'
class Log2(Func):
4867class Log2(Func):
4868    pass
key = 'log2'
class Log10(Func):
4871class Log10(Func):
4872    pass
key = 'log10'
class LogicalOr(AggFunc):
4875class LogicalOr(AggFunc):
4876    _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"]
key = 'logicalor'
class LogicalAnd(AggFunc):
4879class LogicalAnd(AggFunc):
4880    _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"]
key = 'logicaland'
class Lower(Func):
4883class Lower(Func):
4884    _sql_names = ["LOWER", "LCASE"]
key = 'lower'
class Map(Func):
4887class Map(Func):
4888    arg_types = {"keys": False, "values": False}
4889
4890    @property
4891    def keys(self) -> t.List[Expression]:
4892        keys = self.args.get("keys")
4893        return keys.expressions if keys else []
4894
4895    @property
4896    def values(self) -> t.List[Expression]:
4897        values = self.args.get("values")
4898        return values.expressions if values else []
arg_types = {'keys': False, 'values': False}
keys: List[Expression]
values: List[Expression]
key = 'map'
class MapFromEntries(Func):
4901class MapFromEntries(Func):
4902    pass
key = 'mapfromentries'
class StarMap(Func):
4905class StarMap(Func):
4906    pass
key = 'starmap'
class VarMap(Func):
4909class VarMap(Func):
4910    arg_types = {"keys": True, "values": True}
4911    is_var_len_args = True
4912
4913    @property
4914    def keys(self) -> t.List[Expression]:
4915        return self.args["keys"].expressions
4916
4917    @property
4918    def values(self) -> t.List[Expression]:
4919        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):
4923class MatchAgainst(Func):
4924    arg_types = {"this": True, "expressions": True, "modifier": False}
arg_types = {'this': True, 'expressions': True, 'modifier': False}
key = 'matchagainst'
class Max(AggFunc):
4927class Max(AggFunc):
4928    arg_types = {"this": True, "expressions": False}
4929    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'max'
class MD5(Func):
4932class MD5(Func):
4933    _sql_names = ["MD5"]
key = 'md5'
class MD5Digest(Func):
4937class MD5Digest(Func):
4938    _sql_names = ["MD5_DIGEST"]
key = 'md5digest'
class Min(AggFunc):
4941class Min(AggFunc):
4942    arg_types = {"this": True, "expressions": False}
4943    is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
is_var_len_args = True
key = 'min'
class Month(Func):
4946class Month(Func):
4947    pass
key = 'month'
class Nvl2(Func):
4950class Nvl2(Func):
4951    arg_types = {"this": True, "true": True, "false": False}
arg_types = {'this': True, 'true': True, 'false': False}
key = 'nvl2'
class Predict(Func):
4955class Predict(Func):
4956    arg_types = {"this": True, "expression": True, "params_struct": False}
arg_types = {'this': True, 'expression': True, 'params_struct': False}
key = 'predict'
class Pow(Binary, Func):
4959class Pow(Binary, Func):
4960    _sql_names = ["POWER", "POW"]
key = 'pow'
class PercentileCont(AggFunc):
4963class PercentileCont(AggFunc):
4964    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentilecont'
class PercentileDisc(AggFunc):
4967class PercentileDisc(AggFunc):
4968    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'percentiledisc'
class Quantile(AggFunc):
4971class Quantile(AggFunc):
4972    arg_types = {"this": True, "quantile": True}
arg_types = {'this': True, 'quantile': True}
key = 'quantile'
class ApproxQuantile(Quantile):
4975class ApproxQuantile(Quantile):
4976    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):
4979class RangeN(Func):
4980    arg_types = {"this": True, "expressions": True, "each": False}
arg_types = {'this': True, 'expressions': True, 'each': False}
key = 'rangen'
class ReadCSV(Func):
4983class ReadCSV(Func):
4984    _sql_names = ["READ_CSV"]
4985    is_var_len_args = True
4986    arg_types = {"this": True, "expressions": False}
is_var_len_args = True
arg_types = {'this': True, 'expressions': False}
key = 'readcsv'
class Reduce(Func):
4989class Reduce(Func):
4990    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):
4993class RegexpExtract(Func):
4994    arg_types = {
4995        "this": True,
4996        "expression": True,
4997        "position": False,
4998        "occurrence": False,
4999        "parameters": False,
5000        "group": False,
5001    }
arg_types = {'this': True, 'expression': True, 'position': False, 'occurrence': False, 'parameters': False, 'group': False}
key = 'regexpextract'
class RegexpReplace(Func):
5004class RegexpReplace(Func):
5005    arg_types = {
5006        "this": True,
5007        "expression": True,
5008        "replacement": True,
5009        "position": False,
5010        "occurrence": False,
5011        "parameters": False,
5012        "modifiers": False,
5013    }
arg_types = {'this': True, 'expression': True, 'replacement': True, 'position': False, 'occurrence': False, 'parameters': False, 'modifiers': False}
key = 'regexpreplace'
class RegexpLike(Binary, Func):
5016class RegexpLike(Binary, Func):
5017    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexplike'
class RegexpILike(Binary, Func):
5020class RegexpILike(Binary, Func):
5021    arg_types = {"this": True, "expression": True, "flag": False}
arg_types = {'this': True, 'expression': True, 'flag': False}
key = 'regexpilike'
class RegexpSplit(Func):
5026class RegexpSplit(Func):
5027    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'regexpsplit'
class Repeat(Func):
5030class Repeat(Func):
5031    arg_types = {"this": True, "times": True}
arg_types = {'this': True, 'times': True}
key = 'repeat'
class Round(Func):
5034class Round(Func):
5035    arg_types = {"this": True, "decimals": False}
arg_types = {'this': True, 'decimals': False}
key = 'round'
class RowNumber(Func):
5038class RowNumber(Func):
5039    arg_types: t.Dict[str, t.Any] = {}
arg_types: Dict[str, Any] = {}
key = 'rownumber'
class SafeDivide(Func):
5042class SafeDivide(Func):
5043    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'safedivide'
class SHA(Func):
5046class SHA(Func):
5047    _sql_names = ["SHA", "SHA1"]
key = 'sha'
class SHA2(Func):
5050class SHA2(Func):
5051    _sql_names = ["SHA2"]
5052    arg_types = {"this": True, "length": False}
arg_types = {'this': True, 'length': False}
key = 'sha2'
class SortArray(Func):
5055class SortArray(Func):
5056    arg_types = {"this": True, "asc": False}
arg_types = {'this': True, 'asc': False}
key = 'sortarray'
class Split(Func):
5059class Split(Func):
5060    arg_types = {"this": True, "expression": True, "limit": False}
arg_types = {'this': True, 'expression': True, 'limit': False}
key = 'split'
class Substring(Func):
5065class Substring(Func):
5066    arg_types = {"this": True, "start": False, "length": False}
arg_types = {'this': True, 'start': False, 'length': False}
key = 'substring'
class StandardHash(Func):
5069class StandardHash(Func):
5070    arg_types = {"this": True, "expression": False}
arg_types = {'this': True, 'expression': False}
key = 'standardhash'
class StartsWith(Func):
5073class StartsWith(Func):
5074    _sql_names = ["STARTS_WITH", "STARTSWITH"]
5075    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'startswith'
class StrPosition(Func):
5078class StrPosition(Func):
5079    arg_types = {
5080        "this": True,
5081        "substr": True,
5082        "position": False,
5083        "instance": False,
5084    }
arg_types = {'this': True, 'substr': True, 'position': False, 'instance': False}
key = 'strposition'
class StrToDate(Func):
5087class StrToDate(Func):
5088    arg_types = {"this": True, "format": True}
arg_types = {'this': True, 'format': True}
key = 'strtodate'
class StrToTime(Func):
5091class StrToTime(Func):
5092    arg_types = {"this": True, "format": True, "zone": False}
arg_types = {'this': True, 'format': True, 'zone': False}
key = 'strtotime'
class StrToUnix(Func):
5097class StrToUnix(Func):
5098    arg_types = {"this": False, "format": False}
arg_types = {'this': False, 'format': False}
key = 'strtounix'
class StrToMap(Func):
5103class StrToMap(Func):
5104    arg_types = {
5105        "this": True,
5106        "pair_delim": False,
5107        "key_value_delim": False,
5108        "duplicate_resolution_callback": False,
5109    }
arg_types = {'this': True, 'pair_delim': False, 'key_value_delim': False, 'duplicate_resolution_callback': False}
key = 'strtomap'
class NumberToStr(Func):
5112class NumberToStr(Func):
5113    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'numbertostr'
class FromBase(Func):
5116class FromBase(Func):
5117    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'frombase'
class Struct(Func):
5120class Struct(Func):
5121    arg_types = {"expressions": False}
5122    is_var_len_args = True
arg_types = {'expressions': False}
is_var_len_args = True
key = 'struct'
class StructExtract(Func):
5125class StructExtract(Func):
5126    arg_types = {"this": True, "expression": True}
arg_types = {'this': True, 'expression': True}
key = 'structextract'
class Stuff(Func):
5131class Stuff(Func):
5132    _sql_names = ["STUFF", "INSERT"]
5133    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):
5136class Sum(AggFunc):
5137    pass
key = 'sum'
class Sqrt(Func):
5140class Sqrt(Func):
5141    pass
key = 'sqrt'
class Stddev(AggFunc):
5144class Stddev(AggFunc):
5145    pass
key = 'stddev'
class StddevPop(AggFunc):
5148class StddevPop(AggFunc):
5149    pass
key = 'stddevpop'
class StddevSamp(AggFunc):
5152class StddevSamp(AggFunc):
5153    pass
key = 'stddevsamp'
class TimeToStr(Func):
5156class TimeToStr(Func):
5157    arg_types = {"this": True, "format": True, "culture": False}
arg_types = {'this': True, 'format': True, 'culture': False}
key = 'timetostr'
class TimeToTimeStr(Func):
5160class TimeToTimeStr(Func):
5161    pass
key = 'timetotimestr'
class TimeToUnix(Func):
5164class TimeToUnix(Func):
5165    pass
key = 'timetounix'
class TimeStrToDate(Func):
5168class TimeStrToDate(Func):
5169    pass
key = 'timestrtodate'
class TimeStrToTime(Func):
5172class TimeStrToTime(Func):
5173    pass
key = 'timestrtotime'
class TimeStrToUnix(Func):
5176class TimeStrToUnix(Func):
5177    pass
key = 'timestrtounix'
class Trim(Func):
5180class Trim(Func):
5181    arg_types = {
5182        "this": True,
5183        "expression": False,
5184        "position": False,
5185        "collation": False,
5186    }
arg_types = {'this': True, 'expression': False, 'position': False, 'collation': False}
key = 'trim'
class TsOrDsAdd(Func, TimeUnit):
5189class TsOrDsAdd(Func, TimeUnit):
5190    # return_type is used to correctly cast the arguments of this expression when transpiling it
5191    arg_types = {"this": True, "expression": True, "unit": False, "return_type": False}
5192
5193    @property
5194    def return_type(self) -> DataType:
5195        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):
5198class TsOrDsDiff(Func, TimeUnit):
5199    arg_types = {"this": True, "expression": True, "unit": False}
arg_types = {'this': True, 'expression': True, 'unit': False}
key = 'tsordsdiff'
class TsOrDsToDateStr(Func):
5202class TsOrDsToDateStr(Func):
5203    pass
key = 'tsordstodatestr'
class TsOrDsToDate(Func):
5206class TsOrDsToDate(Func):
5207    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'tsordstodate'
class TsOrDiToDi(Func):
5210class TsOrDiToDi(Func):
5211    pass
key = 'tsorditodi'
class Unhex(Func):
5214class Unhex(Func):
5215    pass
key = 'unhex'
class UnixToStr(Func):
5218class UnixToStr(Func):
5219    arg_types = {"this": True, "format": False}
arg_types = {'this': True, 'format': False}
key = 'unixtostr'
class UnixToTime(Func):
5224class UnixToTime(Func):
5225    arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False}
5226
5227    SECONDS = Literal.string("seconds")
5228    MILLIS = Literal.string("millis")
5229    MICROS = Literal.string("micros")
5230    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):
5233class UnixToTimeStr(Func):
5234    pass
key = 'unixtotimestr'
class Upper(Func):
5237class Upper(Func):
5238    _sql_names = ["UPPER", "UCASE"]
key = 'upper'
class Variance(AggFunc):
5241class Variance(AggFunc):
5242    _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"]
key = 'variance'
class VariancePop(AggFunc):
5245class VariancePop(AggFunc):
5246    _sql_names = ["VARIANCE_POP", "VAR_POP"]
key = 'variancepop'
class Week(Func):
5249class Week(Func):
5250    arg_types = {"this": True, "mode": False}
arg_types = {'this': True, 'mode': False}
key = 'week'
class XMLTable(Func):
5253class XMLTable(Func):
5254    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):
5257class Year(Func):
5258    pass
key = 'year'
class Use(Expression):
5261class Use(Expression):
5262    arg_types = {"this": True, "kind": False}
arg_types = {'this': True, 'kind': False}
key = 'use'
class Merge(Expression):
5265class Merge(Expression):
5266    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):
5269class When(Func):
5270    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):
5275class NextValueFor(Func):
5276    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 '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_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:
5314def maybe_parse(
5315    sql_or_expression: ExpOrStr,
5316    *,
5317    into: t.Optional[IntoType] = None,
5318    dialect: DialectType = None,
5319    prefix: t.Optional[str] = None,
5320    copy: bool = False,
5321    **opts,
5322) -> Expression:
5323    """Gracefully handle a possible string or expression.
5324
5325    Example:
5326        >>> maybe_parse("1")
5327        (LITERAL this: 1, is_string: False)
5328        >>> maybe_parse(to_identifier("x"))
5329        (IDENTIFIER this: x, quoted: False)
5330
5331    Args:
5332        sql_or_expression: the SQL code string or an expression
5333        into: the SQLGlot Expression to parse into
5334        dialect: the dialect used to parse the input expressions (in the case that an
5335            input expression is a SQL string).
5336        prefix: a string to prefix the sql with before it gets parsed
5337            (automatically includes a space)
5338        copy: whether or not to copy the expression.
5339        **opts: other options to use to parse the input expressions (again, in the case
5340            that an input expression is a SQL string).
5341
5342    Returns:
5343        Expression: the parsed or given expression.
5344    """
5345    if isinstance(sql_or_expression, Expression):
5346        if copy:
5347            return sql_or_expression.copy()
5348        return sql_or_expression
5349
5350    if sql_or_expression is None:
5351        raise ParseError(f"SQL cannot be None")
5352
5353    import sqlglot
5354
5355    sql = str(sql_or_expression)
5356    if prefix:
5357        sql = f"{prefix} {sql}"
5358
5359    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):
5372def maybe_copy(instance, copy=True):
5373    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:
5554def union(
5555    left: ExpOrStr,
5556    right: ExpOrStr,
5557    distinct: bool = True,
5558    dialect: DialectType = None,
5559    copy: bool = True,
5560    **opts,
5561) -> Union:
5562    """
5563    Initializes a syntax tree from one UNION expression.
5564
5565    Example:
5566        >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql()
5567        'SELECT * FROM foo UNION SELECT * FROM bla'
5568
5569    Args:
5570        left: the SQL code string corresponding to the left-hand side.
5571            If an `Expression` instance is passed, it will be used as-is.
5572        right: the SQL code string corresponding to the right-hand side.
5573            If an `Expression` instance is passed, it will be used as-is.
5574        distinct: set the DISTINCT flag if and only if this is true.
5575        dialect: the dialect used to parse the input expression.
5576        copy: whether or not to copy the expression.
5577        opts: other options to use to parse the input expressions.
5578
5579    Returns:
5580        The new Union instance.
5581    """
5582    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5583    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5584
5585    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:
5588def intersect(
5589    left: ExpOrStr,
5590    right: ExpOrStr,
5591    distinct: bool = True,
5592    dialect: DialectType = None,
5593    copy: bool = True,
5594    **opts,
5595) -> Intersect:
5596    """
5597    Initializes a syntax tree from one INTERSECT expression.
5598
5599    Example:
5600        >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql()
5601        'SELECT * FROM foo INTERSECT SELECT * FROM bla'
5602
5603    Args:
5604        left: the SQL code string corresponding to the left-hand side.
5605            If an `Expression` instance is passed, it will be used as-is.
5606        right: the SQL code string corresponding to the right-hand side.
5607            If an `Expression` instance is passed, it will be used as-is.
5608        distinct: set the DISTINCT flag if and only if this is true.
5609        dialect: the dialect used to parse the input expression.
5610        copy: whether or not to copy the expression.
5611        opts: other options to use to parse the input expressions.
5612
5613    Returns:
5614        The new Intersect instance.
5615    """
5616    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5617    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5618
5619    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:
5622def except_(
5623    left: ExpOrStr,
5624    right: ExpOrStr,
5625    distinct: bool = True,
5626    dialect: DialectType = None,
5627    copy: bool = True,
5628    **opts,
5629) -> Except:
5630    """
5631    Initializes a syntax tree from one EXCEPT expression.
5632
5633    Example:
5634        >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql()
5635        'SELECT * FROM foo EXCEPT SELECT * FROM bla'
5636
5637    Args:
5638        left: the SQL code string corresponding to the left-hand side.
5639            If an `Expression` instance is passed, it will be used as-is.
5640        right: the SQL code string corresponding to the right-hand side.
5641            If an `Expression` instance is passed, it will be used as-is.
5642        distinct: set the DISTINCT flag if and only if this is true.
5643        dialect: the dialect used to parse the input expression.
5644        copy: whether or not to copy the expression.
5645        opts: other options to use to parse the input expressions.
5646
5647    Returns:
5648        The new Except instance.
5649    """
5650    left = maybe_parse(sql_or_expression=left, dialect=dialect, copy=copy, **opts)
5651    right = maybe_parse(sql_or_expression=right, dialect=dialect, copy=copy, **opts)
5652
5653    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:
5656def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5657    """
5658    Initializes a syntax tree from one or multiple SELECT expressions.
5659
5660    Example:
5661        >>> select("col1", "col2").from_("tbl").sql()
5662        'SELECT col1, col2 FROM tbl'
5663
5664    Args:
5665        *expressions: the SQL code string to parse as the expressions of a
5666            SELECT statement. If an Expression instance is passed, this is used as-is.
5667        dialect: the dialect used to parse the input expressions (in the case that an
5668            input expression is a SQL string).
5669        **opts: other options to use to parse the input expressions (again, in the case
5670            that an input expression is a SQL string).
5671
5672    Returns:
5673        Select: the syntax tree for the SELECT statement.
5674    """
5675    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:
5678def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select:
5679    """
5680    Initializes a syntax tree from a FROM expression.
5681
5682    Example:
5683        >>> from_("tbl").select("col1", "col2").sql()
5684        'SELECT col1, col2 FROM tbl'
5685
5686    Args:
5687        *expression: the SQL code string to parse as the FROM expressions of a
5688            SELECT statement. If an Expression instance is passed, this is used as-is.
5689        dialect: the dialect used to parse the input expression (in the case that the
5690            input expression is a SQL string).
5691        **opts: other options to use to parse the input expressions (again, in the case
5692            that the input expression is a SQL string).
5693
5694    Returns:
5695        Select: the syntax tree for the SELECT statement.
5696    """
5697    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:
5700def update(
5701    table: str | Table,
5702    properties: dict,
5703    where: t.Optional[ExpOrStr] = None,
5704    from_: t.Optional[ExpOrStr] = None,
5705    dialect: DialectType = None,
5706    **opts,
5707) -> Update:
5708    """
5709    Creates an update statement.
5710
5711    Example:
5712        >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql()
5713        "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1"
5714
5715    Args:
5716        *properties: dictionary of properties to set which are
5717            auto converted to sql objects eg None -> NULL
5718        where: sql conditional parsed into a WHERE statement
5719        from_: sql statement parsed into a FROM statement
5720        dialect: the dialect used to parse the input expressions.
5721        **opts: other options to use to parse the input expressions.
5722
5723    Returns:
5724        Update: the syntax tree for the UPDATE statement.
5725    """
5726    update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect))
5727    update_expr.set(
5728        "expressions",
5729        [
5730            EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v))
5731            for k, v in properties.items()
5732        ],
5733    )
5734    if from_:
5735        update_expr.set(
5736            "from",
5737            maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts),
5738        )
5739    if isinstance(where, Condition):
5740        where = Where(this=where)
5741    if where:
5742        update_expr.set(
5743            "where",
5744            maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts),
5745        )
5746    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:
5749def delete(
5750    table: ExpOrStr,
5751    where: t.Optional[ExpOrStr] = None,
5752    returning: t.Optional[ExpOrStr] = None,
5753    dialect: DialectType = None,
5754    **opts,
5755) -> Delete:
5756    """
5757    Builds a delete statement.
5758
5759    Example:
5760        >>> delete("my_table", where="id > 1").sql()
5761        'DELETE FROM my_table WHERE id > 1'
5762
5763    Args:
5764        where: sql conditional parsed into a WHERE statement
5765        returning: sql conditional parsed into a RETURNING statement
5766        dialect: the dialect used to parse the input expressions.
5767        **opts: other options to use to parse the input expressions.
5768
5769    Returns:
5770        Delete: the syntax tree for the DELETE statement.
5771    """
5772    delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts)
5773    if where:
5774        delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts)
5775    if returning:
5776        delete_expr = t.cast(
5777            Delete, delete_expr.returning(returning, dialect=dialect, copy=False, **opts)
5778        )
5779    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:
5782def insert(
5783    expression: ExpOrStr,
5784    into: ExpOrStr,
5785    columns: t.Optional[t.Sequence[ExpOrStr]] = None,
5786    overwrite: t.Optional[bool] = None,
5787    returning: t.Optional[ExpOrStr] = None,
5788    dialect: DialectType = None,
5789    copy: bool = True,
5790    **opts,
5791) -> Insert:
5792    """
5793    Builds an INSERT statement.
5794
5795    Example:
5796        >>> insert("VALUES (1, 2, 3)", "tbl").sql()
5797        'INSERT INTO tbl VALUES (1, 2, 3)'
5798
5799    Args:
5800        expression: the sql string or expression of the INSERT statement
5801        into: the tbl to insert data to.
5802        columns: optionally the table's column names.
5803        overwrite: whether to INSERT OVERWRITE or not.
5804        returning: sql conditional parsed into a RETURNING statement
5805        dialect: the dialect used to parse the input expressions.
5806        copy: whether or not to copy the expression.
5807        **opts: other options to use to parse the input expressions.
5808
5809    Returns:
5810        Insert: the syntax tree for the INSERT statement.
5811    """
5812    expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
5813    this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts)
5814
5815    if columns:
5816        this = _apply_list_builder(
5817            *columns,
5818            instance=Schema(this=this),
5819            arg="expressions",
5820            into=Identifier,
5821            copy=False,
5822            dialect=dialect,
5823            **opts,
5824        )
5825
5826    insert = Insert(this=this, expression=expr, overwrite=overwrite)
5827
5828    if returning:
5829        insert = t.cast(Insert, insert.returning(returning, dialect=dialect, copy=False, **opts))
5830
5831    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:
5834def condition(
5835    expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts
5836) -> Condition:
5837    """
5838    Initialize a logical condition expression.
5839
5840    Example:
5841        >>> condition("x=1").sql()
5842        'x = 1'
5843
5844        This is helpful for composing larger logical syntax trees:
5845        >>> where = condition("x=1")
5846        >>> where = where.and_("y=1")
5847        >>> Select().from_("tbl").select("*").where(where).sql()
5848        'SELECT * FROM tbl WHERE x = 1 AND y = 1'
5849
5850    Args:
5851        *expression: the SQL code string to parse.
5852            If an Expression instance is passed, this is used as-is.
5853        dialect: the dialect used to parse the input expression (in the case that the
5854            input expression is a SQL string).
5855        copy: Whether or not to copy `expression` (only applies to expressions).
5856        **opts: other options to use to parse the input expressions (again, in the case
5857            that the input expression is a SQL string).
5858
5859    Returns:
5860        The new Condition instance
5861    """
5862    return maybe_parse(
5863        expression,
5864        into=Condition,
5865        dialect=dialect,
5866        copy=copy,
5867        **opts,
5868    )

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:
5871def and_(
5872    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5873) -> Condition:
5874    """
5875    Combine multiple conditions with an AND logical operator.
5876
5877    Example:
5878        >>> and_("x=1", and_("y=1", "z=1")).sql()
5879        'x = 1 AND (y = 1 AND z = 1)'
5880
5881    Args:
5882        *expressions: the SQL code strings to parse.
5883            If an Expression instance is passed, this is used as-is.
5884        dialect: the dialect used to parse the input expression.
5885        copy: whether or not to copy `expressions` (only applies to Expressions).
5886        **opts: other options to use to parse the input expressions.
5887
5888    Returns:
5889        And: the new condition
5890    """
5891    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:
5894def or_(
5895    *expressions: t.Optional[ExpOrStr], dialect: DialectType = None, copy: bool = True, **opts
5896) -> Condition:
5897    """
5898    Combine multiple conditions with an OR logical operator.
5899
5900    Example:
5901        >>> or_("x=1", or_("y=1", "z=1")).sql()
5902        'x = 1 OR (y = 1 OR z = 1)'
5903
5904    Args:
5905        *expressions: the SQL code strings to parse.
5906            If an Expression instance is passed, this is used as-is.
5907        dialect: the dialect used to parse the input expression.
5908        copy: whether or not to copy `expressions` (only applies to Expressions).
5909        **opts: other options to use to parse the input expressions.
5910
5911    Returns:
5912        Or: the new condition
5913    """
5914    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:
5917def not_(expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts) -> Not:
5918    """
5919    Wrap a condition with a NOT operator.
5920
5921    Example:
5922        >>> not_("this_suit='black'").sql()
5923        "NOT this_suit = 'black'"
5924
5925    Args:
5926        expression: the SQL code string to parse.
5927            If an Expression instance is passed, this is used as-is.
5928        dialect: the dialect used to parse the input expression.
5929        copy: whether to copy the expression or not.
5930        **opts: other options to use to parse the input expressions.
5931
5932    Returns:
5933        The new condition.
5934    """
5935    this = condition(
5936        expression,
5937        dialect=dialect,
5938        copy=copy,
5939        **opts,
5940    )
5941    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:
5944def paren(expression: ExpOrStr, copy: bool = True) -> Paren:
5945    """
5946    Wrap an expression in parentheses.
5947
5948    Example:
5949        >>> paren("5 + 3").sql()
5950        '(5 + 3)'
5951
5952    Args:
5953        expression: the SQL code string to parse.
5954            If an Expression instance is passed, this is used as-is.
5955        copy: whether to copy the expression or not.
5956
5957    Returns:
5958        The wrapped expression.
5959    """
5960    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):
5978def to_identifier(name, quoted=None, copy=True):
5979    """Builds an identifier.
5980
5981    Args:
5982        name: The name to turn into an identifier.
5983        quoted: Whether or not force quote the identifier.
5984        copy: Whether or not to copy name if it's an Identifier.
5985
5986    Returns:
5987        The identifier ast node.
5988    """
5989
5990    if name is None:
5991        return None
5992
5993    if isinstance(name, Identifier):
5994        identifier = maybe_copy(name, copy)
5995    elif isinstance(name, str):
5996        identifier = Identifier(
5997            this=name,
5998            quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted,
5999        )
6000    else:
6001        raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}")
6002    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:
6005def parse_identifier(name: str | Identifier, dialect: DialectType = None) -> Identifier:
6006    """
6007    Parses a given string into an identifier.
6008
6009    Args:
6010        name: The name to parse into an identifier.
6011        dialect: The dialect to parse against.
6012
6013    Returns:
6014        The identifier ast node.
6015    """
6016    try:
6017        expression = maybe_parse(name, dialect=dialect, into=Identifier)
6018    except ParseError:
6019        expression = to_identifier(name)
6020
6021    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:
6027def to_interval(interval: str | Literal) -> Interval:
6028    """Builds an interval expression from a string like '1 day' or '5 months'."""
6029    if isinstance(interval, Literal):
6030        if not interval.is_string:
6031            raise ValueError("Invalid interval string.")
6032
6033        interval = interval.this
6034
6035    interval_parts = INTERVAL_STRING_RE.match(interval)  # type: ignore
6036
6037    if not interval_parts:
6038        raise ValueError("Invalid interval string.")
6039
6040    return Interval(
6041        this=Literal.string(interval_parts.group(1)),
6042        unit=Var(this=interval_parts.group(2)),
6043    )

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]:
6056def to_table(
6057    sql_path: t.Optional[str | Table], dialect: DialectType = None, copy: bool = True, **kwargs
6058) -> t.Optional[Table]:
6059    """
6060    Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional.
6061    If a table is passed in then that table is returned.
6062
6063    Args:
6064        sql_path: a `[catalog].[schema].[table]` string.
6065        dialect: the source dialect according to which the table name will be parsed.
6066        copy: Whether or not to copy a table if it is passed in.
6067        kwargs: the kwargs to instantiate the resulting `Table` expression with.
6068
6069    Returns:
6070        A table expression.
6071    """
6072    if sql_path is None or isinstance(sql_path, Table):
6073        return maybe_copy(sql_path, copy=copy)
6074    if not isinstance(sql_path, str):
6075        raise ValueError(f"Invalid type provided for a table: {type(sql_path)}")
6076
6077    table = maybe_parse(sql_path, into=Table, dialect=dialect)
6078    if table:
6079        for k, v in kwargs.items():
6080            table.set(k, v)
6081
6082    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:
6085def to_column(sql_path: str | Column, **kwargs) -> Column:
6086    """
6087    Create a column from a `[table].[column]` sql path. Schema is optional.
6088
6089    If a column is passed in then that column is returned.
6090
6091    Args:
6092        sql_path: `[table].[column]` string
6093    Returns:
6094        Table: A column expression
6095    """
6096    if sql_path is None or isinstance(sql_path, Column):
6097        return sql_path
6098    if not isinstance(sql_path, str):
6099        raise ValueError(f"Invalid type provided for column: {type(sql_path)}")
6100    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):
6103def alias_(
6104    expression: ExpOrStr,
6105    alias: str | Identifier,
6106    table: bool | t.Sequence[str | Identifier] = False,
6107    quoted: t.Optional[bool] = None,
6108    dialect: DialectType = None,
6109    copy: bool = True,
6110    **opts,
6111):
6112    """Create an Alias expression.
6113
6114    Example:
6115        >>> alias_('foo', 'bar').sql()
6116        'foo AS bar'
6117
6118        >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql()
6119        '(SELECT 1, 2) AS bar(a, b)'
6120
6121    Args:
6122        expression: the SQL code strings to parse.
6123            If an Expression instance is passed, this is used as-is.
6124        alias: the alias name to use. If the name has
6125            special characters it is quoted.
6126        table: Whether or not to create a table alias, can also be a list of columns.
6127        quoted: whether or not to quote the alias
6128        dialect: the dialect used to parse the input expression.
6129        copy: Whether or not to copy the expression.
6130        **opts: other options to use to parse the input expressions.
6131
6132    Returns:
6133        Alias: the aliased expression
6134    """
6135    exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts)
6136    alias = to_identifier(alias, quoted=quoted)
6137
6138    if table:
6139        table_alias = TableAlias(this=alias)
6140        exp.set("alias", table_alias)
6141
6142        if not isinstance(table, bool):
6143            for column in table:
6144                table_alias.append("columns", to_identifier(column, quoted=quoted))
6145
6146        return exp
6147
6148    # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in
6149    # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node
6150    # for the complete Window expression.
6151    #
6152    # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls
6153
6154    if "alias" in exp.arg_types and not isinstance(exp, Window):
6155        exp.set("alias", alias)
6156        return exp
6157    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:
6160def subquery(
6161    expression: ExpOrStr,
6162    alias: t.Optional[Identifier | str] = None,
6163    dialect: DialectType = None,
6164    **opts,
6165) -> Select:
6166    """
6167    Build a subquery expression.
6168
6169    Example:
6170        >>> subquery('select x from tbl', 'bar').select('x').sql()
6171        'SELECT x FROM (SELECT x FROM tbl) AS bar'
6172
6173    Args:
6174        expression: the SQL code strings to parse.
6175            If an Expression instance is passed, this is used as-is.
6176        alias: the alias name to use.
6177        dialect: the dialect used to parse the input expression.
6178        **opts: other options to use to parse the input expressions.
6179
6180    Returns:
6181        A new Select instance with the subquery expression included.
6182    """
6183
6184    expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias)
6185    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:
6188def column(
6189    col: str | Identifier,
6190    table: t.Optional[str | Identifier] = None,
6191    db: t.Optional[str | Identifier] = None,
6192    catalog: t.Optional[str | Identifier] = None,
6193    quoted: t.Optional[bool] = None,
6194) -> Column:
6195    """
6196    Build a Column.
6197
6198    Args:
6199        col: Column name.
6200        table: Table name.
6201        db: Database name.
6202        catalog: Catalog name.
6203        quoted: Whether to force quotes on the column's identifiers.
6204
6205    Returns:
6206        The new Column instance.
6207    """
6208    return Column(
6209        this=to_identifier(col, quoted=quoted),
6210        table=to_identifier(table, quoted=quoted),
6211        db=to_identifier(db, quoted=quoted),
6212        catalog=to_identifier(catalog, quoted=quoted),
6213    )

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:
6216def cast(expression: ExpOrStr, to: DATA_TYPE, **opts) -> Cast:
6217    """Cast an expression to a data type.
6218
6219    Example:
6220        >>> cast('x + 1', 'int').sql()
6221        'CAST(x + 1 AS INT)'
6222
6223    Args:
6224        expression: The expression to cast.
6225        to: The datatype to cast to.
6226
6227    Returns:
6228        The new Cast instance.
6229    """
6230    expression = maybe_parse(expression, **opts)
6231    data_type = DataType.build(to, **opts)
6232    expression = Cast(this=expression, to=data_type)
6233    expression.type = data_type
6234    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:
6237def table_(
6238    table: Identifier | str,
6239    db: t.Optional[Identifier | str] = None,
6240    catalog: t.Optional[Identifier | str] = None,
6241    quoted: t.Optional[bool] = None,
6242    alias: t.Optional[Identifier | str] = None,
6243) -> Table:
6244    """Build a Table.
6245
6246    Args:
6247        table: Table name.
6248        db: Database name.
6249        catalog: Catalog name.
6250        quote: Whether to force quotes on the table's identifiers.
6251        alias: Table's alias.
6252
6253    Returns:
6254        The new Table instance.
6255    """
6256    return Table(
6257        this=to_identifier(table, quoted=quoted) if table else None,
6258        db=to_identifier(db, quoted=quoted) if db else None,
6259        catalog=to_identifier(catalog, quoted=quoted) if catalog else None,
6260        alias=TableAlias(this=to_identifier(alias)) if alias else None,
6261    )

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

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:
6297def var(name: t.Optional[ExpOrStr]) -> Var:
6298    """Build a SQL variable.
6299
6300    Example:
6301        >>> repr(var('x'))
6302        '(VAR this: x)'
6303
6304        >>> repr(var(column('x', table='y')))
6305        '(VAR this: x)'
6306
6307    Args:
6308        name: The name of the var or an expression who's name will become the var.
6309
6310    Returns:
6311        The new variable node.
6312    """
6313    if not name:
6314        raise ValueError("Cannot convert empty name into var.")
6315
6316    if isinstance(name, Expression):
6317        name = name.name
6318    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:
6321def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable:
6322    """Build ALTER TABLE... RENAME... expression
6323
6324    Args:
6325        old_name: The old name of the table
6326        new_name: The new name of the table
6327
6328    Returns:
6329        Alter table expression
6330    """
6331    old_table = to_table(old_name)
6332    new_table = to_table(new_name)
6333    return AlterTable(
6334        this=old_table,
6335        actions=[
6336            RenameTable(this=new_table),
6337        ],
6338    )

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:
6341def convert(value: t.Any, copy: bool = False) -> Expression:
6342    """Convert a python value into an expression object.
6343
6344    Raises an error if a conversion is not possible.
6345
6346    Args:
6347        value: A python object.
6348        copy: Whether or not to copy `value` (only applies to Expressions and collections).
6349
6350    Returns:
6351        Expression: the equivalent expression object.
6352    """
6353    if isinstance(value, Expression):
6354        return maybe_copy(value, copy)
6355    if isinstance(value, str):
6356        return Literal.string(value)
6357    if isinstance(value, bool):
6358        return Boolean(this=value)
6359    if value is None or (isinstance(value, float) and math.isnan(value)):
6360        return NULL
6361    if isinstance(value, numbers.Number):
6362        return Literal.number(value)
6363    if isinstance(value, datetime.datetime):
6364        datetime_literal = Literal.string(
6365            (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat()
6366        )
6367        return TimeStrToTime(this=datetime_literal)
6368    if isinstance(value, datetime.date):
6369        date_literal = Literal.string(value.strftime("%Y-%m-%d"))
6370        return DateStrToDate(this=date_literal)
6371    if isinstance(value, tuple):
6372        return Tuple(expressions=[convert(v, copy=copy) for v in value])
6373    if isinstance(value, list):
6374        return Array(expressions=[convert(v, copy=copy) for v in value])
6375    if isinstance(value, dict):
6376        return Map(
6377            keys=Array(expressions=[convert(k, copy=copy) for k in value]),
6378            values=Array(expressions=[convert(v, copy=copy) for v in value.values()]),
6379        )
6380    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:
6383def replace_children(expression: Expression, fun: t.Callable, *args, **kwargs) -> None:
6384    """
6385    Replace children of an expression with the result of a lambda fun(child) -> exp.
6386    """
6387    for k, v in expression.args.items():
6388        is_list_arg = type(v) is list
6389
6390        child_nodes = v if is_list_arg else [v]
6391        new_child_nodes = []
6392
6393        for cn in child_nodes:
6394            if isinstance(cn, Expression):
6395                for child_node in ensure_collection(fun(cn, *args, **kwargs)):
6396                    new_child_nodes.append(child_node)
6397                    child_node.parent = expression
6398                    child_node.arg_key = k
6399            else:
6400                new_child_nodes.append(cn)
6401
6402        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]:
6405def column_table_names(expression: Expression, exclude: str = "") -> t.Set[str]:
6406    """
6407    Return all table names referenced through columns in an expression.
6408
6409    Example:
6410        >>> import sqlglot
6411        >>> sorted(column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")))
6412        ['a', 'c']
6413
6414    Args:
6415        expression: expression to find table names.
6416        exclude: a table name to exclude
6417
6418    Returns:
6419        A list of unique names.
6420    """
6421    return {
6422        table
6423        for table in (column.table for column in expression.find_all(Column))
6424        if table and table != exclude
6425    }

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

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:
6460def normalize_table_name(table: str | Table, dialect: DialectType = None, copy: bool = True) -> str:
6461    """Returns a case normalized table name without quotes.
6462
6463    Args:
6464        table: the table to normalize
6465        dialect: the dialect to use for normalization rules
6466        copy: whether or not to copy the expression.
6467
6468    Examples:
6469        >>> normalize_table_name("`A-B`.c", dialect="bigquery")
6470        'A-B.c'
6471    """
6472    from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
6473
6474    return ".".join(
6475        p.name
6476        for p in normalize_identifiers(
6477            to_table(table, dialect=dialect, copy=copy), dialect=dialect
6478        ).parts
6479    )

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:
6482def replace_tables(
6483    expression: E, mapping: t.Dict[str, str], dialect: DialectType = None, copy: bool = True
6484) -> E:
6485    """Replace all tables in expression according to the mapping.
6486
6487    Args:
6488        expression: expression node to be transformed and replaced.
6489        mapping: mapping of table names.
6490        dialect: the dialect of the mapping table
6491        copy: whether or not to copy the expression.
6492
6493    Examples:
6494        >>> from sqlglot import exp, parse_one
6495        >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql()
6496        'SELECT * FROM c'
6497
6498    Returns:
6499        The mapped expression.
6500    """
6501
6502    mapping = {normalize_table_name(k, dialect=dialect): v for k, v in mapping.items()}
6503
6504    def _replace_tables(node: Expression) -> Expression:
6505        if isinstance(node, Table):
6506            new_name = mapping.get(normalize_table_name(node, dialect=dialect))
6507
6508            if new_name:
6509                return to_table(
6510                    new_name,
6511                    **{k: v for k, v in node.args.items() if k not in TABLE_PARTS},
6512                )
6513        return node
6514
6515    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'
Returns:

The mapped expression.

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

Returns a true Boolean expression.

def false() -> Boolean:
6685def false() -> Boolean:
6686    """
6687    Returns a false Boolean expression.
6688    """
6689    return Boolean(this=False)

Returns a false Boolean expression.

def null() -> Null:
6692def null() -> Null:
6693    """
6694    Returns a Null expression.
6695    """
6696    return Null()

Returns a Null expression.

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