Expressions
Every AST node in SQLGlot is represented by a subclass of Expression.
This module contains the implementation of all supported Expression types. Additionally,
it exposes a number of helper functions, which are mainly used to programmatically build
SQL expressions, such as sqlglot.expressions.select.
1""" 2## Expressions 3 4Every AST node in SQLGlot is represented by a subclass of `Expression`. 5 6This module contains the implementation of all supported `Expression` types. Additionally, 7it exposes a number of helper functions, which are mainly used to programmatically build 8SQL expressions, such as `sqlglot.expressions.select`. 9 10---- 11""" 12 13from __future__ import annotations 14 15import datetime 16import math 17import numbers 18import re 19import typing as t 20from collections import deque 21from copy import deepcopy 22from enum import auto 23 24from sqlglot.errors import ParseError 25from sqlglot.helper import ( 26 AutoName, 27 camel_to_snake_case, 28 ensure_collection, 29 ensure_list, 30 seq_get, 31 split_num_words, 32 subclasses, 33) 34from sqlglot.tokens import Token 35 36if t.TYPE_CHECKING: 37 from sqlglot.dialects.dialect import DialectType 38 39E = t.TypeVar("E", bound="Expression") 40 41 42class _Expression(type): 43 def __new__(cls, clsname, bases, attrs): 44 klass = super().__new__(cls, clsname, bases, attrs) 45 46 # When an Expression class is created, its key is automatically set to be 47 # the lowercase version of the class' name. 48 klass.key = clsname.lower() 49 50 # This is so that docstrings are not inherited in pdoc 51 klass.__doc__ = klass.__doc__ or "" 52 53 return klass 54 55 56class Expression(metaclass=_Expression): 57 """ 58 The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary 59 context, such as its child expressions, their names (arg keys), and whether a given child expression 60 is optional or not. 61 62 Attributes: 63 key: a unique key for each class in the Expression hierarchy. This is useful for hashing 64 and representing expressions as strings. 65 arg_types: determines what arguments (child nodes) are supported by an expression. It 66 maps arg keys to booleans that indicate whether the corresponding args are optional. 67 parent: a reference to the parent expression (or None, in case of root expressions). 68 arg_key: the arg key an expression is associated with, i.e. the name its parent expression 69 uses to refer to it. 70 comments: a list of comments that are associated with a given expression. This is used in 71 order to preserve comments when transpiling SQL code. 72 _type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the 73 optimizer, in order to enable some transformations that require type information. 74 75 Example: 76 >>> class Foo(Expression): 77 ... arg_types = {"this": True, "expression": False} 78 79 The above definition informs us that Foo is an Expression that requires an argument called 80 "this" and may also optionally receive an argument called "expression". 81 82 Args: 83 args: a mapping used for retrieving the arguments of an expression, given their arg keys. 84 """ 85 86 key = "expression" 87 arg_types = {"this": True} 88 __slots__ = ("args", "parent", "arg_key", "comments", "_type", "_meta", "_hash") 89 90 def __init__(self, **args: t.Any): 91 self.args: t.Dict[str, t.Any] = args 92 self.parent: t.Optional[Expression] = None 93 self.arg_key: t.Optional[str] = None 94 self.comments: t.Optional[t.List[str]] = None 95 self._type: t.Optional[DataType] = None 96 self._meta: t.Optional[t.Dict[str, t.Any]] = None 97 self._hash: t.Optional[int] = None 98 99 for arg_key, value in self.args.items(): 100 self._set_parent(arg_key, value) 101 102 def __eq__(self, other) -> bool: 103 return type(self) is type(other) and hash(self) == hash(other) 104 105 @property 106 def hashable_args(self) -> t.Any: 107 args = (self.args.get(k) for k in self.arg_types) 108 109 return tuple( 110 (tuple(_norm_arg(a) for a in arg) if arg else None) 111 if type(arg) is list 112 else (_norm_arg(arg) if arg is not None and arg is not False else None) 113 for arg in args 114 ) 115 116 def __hash__(self) -> int: 117 if self._hash is not None: 118 return self._hash 119 120 return hash((self.__class__, self.hashable_args)) 121 122 @property 123 def this(self): 124 """ 125 Retrieves the argument with key "this". 126 """ 127 return self.args.get("this") 128 129 @property 130 def expression(self): 131 """ 132 Retrieves the argument with key "expression". 133 """ 134 return self.args.get("expression") 135 136 @property 137 def expressions(self): 138 """ 139 Retrieves the argument with key "expressions". 140 """ 141 return self.args.get("expressions") or [] 142 143 def text(self, key) -> str: 144 """ 145 Returns a textual representation of the argument corresponding to "key". This can only be used 146 for args that are strings or leaf Expression instances, such as identifiers and literals. 147 """ 148 field = self.args.get(key) 149 if isinstance(field, str): 150 return field 151 if isinstance(field, (Identifier, Literal, Var)): 152 return field.this 153 if isinstance(field, (Star, Null)): 154 return field.name 155 return "" 156 157 @property 158 def is_string(self) -> bool: 159 """ 160 Checks whether a Literal expression is a string. 161 """ 162 return isinstance(self, Literal) and self.args["is_string"] 163 164 @property 165 def is_number(self) -> bool: 166 """ 167 Checks whether a Literal expression is a number. 168 """ 169 return isinstance(self, Literal) and not self.args["is_string"] 170 171 @property 172 def is_int(self) -> bool: 173 """ 174 Checks whether a Literal expression is an integer. 175 """ 176 if self.is_number: 177 try: 178 int(self.name) 179 return True 180 except ValueError: 181 pass 182 return False 183 184 @property 185 def is_star(self) -> bool: 186 """Checks whether an expression is a star.""" 187 return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star)) 188 189 @property 190 def alias(self) -> str: 191 """ 192 Returns the alias of the expression, or an empty string if it's not aliased. 193 """ 194 if isinstance(self.args.get("alias"), TableAlias): 195 return self.args["alias"].name 196 return self.text("alias") 197 198 @property 199 def name(self) -> str: 200 return self.text("this") 201 202 @property 203 def alias_or_name(self): 204 return self.alias or self.name 205 206 @property 207 def output_name(self): 208 """ 209 Name of the output column if this expression is a selection. 210 211 If the Expression has no output name, an empty string is returned. 212 213 Example: 214 >>> from sqlglot import parse_one 215 >>> parse_one("SELECT a").expressions[0].output_name 216 'a' 217 >>> parse_one("SELECT b AS c").expressions[0].output_name 218 'c' 219 >>> parse_one("SELECT 1 + 2").expressions[0].output_name 220 '' 221 """ 222 return "" 223 224 @property 225 def type(self) -> t.Optional[DataType]: 226 return self._type 227 228 @type.setter 229 def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None: 230 if dtype and not isinstance(dtype, DataType): 231 dtype = DataType.build(dtype) 232 self._type = dtype # type: ignore 233 234 @property 235 def meta(self) -> t.Dict[str, t.Any]: 236 if self._meta is None: 237 self._meta = {} 238 return self._meta 239 240 def __deepcopy__(self, memo): 241 copy = self.__class__(**deepcopy(self.args)) 242 if self.comments is not None: 243 copy.comments = deepcopy(self.comments) 244 245 if self._type is not None: 246 copy._type = self._type.copy() 247 248 if self._meta is not None: 249 copy._meta = deepcopy(self._meta) 250 251 return copy 252 253 def copy(self): 254 """ 255 Returns a deep copy of the expression. 256 """ 257 new = deepcopy(self) 258 new.parent = self.parent 259 return new 260 261 def add_comments(self, comments: t.Optional[t.List[str]]) -> None: 262 if self.comments is None: 263 self.comments = [] 264 if comments: 265 self.comments.extend(comments) 266 267 def append(self, arg_key, value): 268 """ 269 Appends value to arg_key if it's a list or sets it as a new list. 270 271 Args: 272 arg_key (str): name of the list expression arg 273 value (Any): value to append to the list 274 """ 275 if not isinstance(self.args.get(arg_key), list): 276 self.args[arg_key] = [] 277 self.args[arg_key].append(value) 278 self._set_parent(arg_key, value) 279 280 def set(self, arg_key, value): 281 """ 282 Sets `arg_key` to `value`. 283 284 Args: 285 arg_key (str): name of the expression arg. 286 value: value to set the arg to. 287 """ 288 self.args[arg_key] = value 289 self._set_parent(arg_key, value) 290 291 def _set_parent(self, arg_key, value): 292 if hasattr(value, "parent"): 293 value.parent = self 294 value.arg_key = arg_key 295 elif type(value) is list: 296 for v in value: 297 if hasattr(v, "parent"): 298 v.parent = self 299 v.arg_key = arg_key 300 301 @property 302 def depth(self): 303 """ 304 Returns the depth of this tree. 305 """ 306 if self.parent: 307 return self.parent.depth + 1 308 return 0 309 310 def iter_expressions(self) -> t.Iterator[t.Tuple[str, Expression]]: 311 """Yields the key and expression for all arguments, exploding list args.""" 312 for k, vs in self.args.items(): 313 if type(vs) is list: 314 for v in vs: 315 if hasattr(v, "parent"): 316 yield k, v 317 else: 318 if hasattr(vs, "parent"): 319 yield k, vs 320 321 def find(self, *expression_types: t.Type[E], bfs=True) -> E | None: 322 """ 323 Returns the first node in this tree which matches at least one of 324 the specified types. 325 326 Args: 327 expression_types: the expression type(s) to match. 328 329 Returns: 330 The node which matches the criteria or None if no such node was found. 331 """ 332 return next(self.find_all(*expression_types, bfs=bfs), None) 333 334 def find_all(self, *expression_types: t.Type[E], bfs=True) -> t.Iterator[E]: 335 """ 336 Returns a generator object which visits all nodes in this tree and only 337 yields those that match at least one of the specified expression types. 338 339 Args: 340 expression_types: the expression type(s) to match. 341 342 Returns: 343 The generator object. 344 """ 345 for expression, *_ in self.walk(bfs=bfs): 346 if isinstance(expression, expression_types): 347 yield expression 348 349 def find_ancestor(self, *expression_types: t.Type[E]) -> E | None: 350 """ 351 Returns a nearest parent matching expression_types. 352 353 Args: 354 expression_types: the expression type(s) to match. 355 356 Returns: 357 The parent node. 358 """ 359 ancestor = self.parent 360 while ancestor and not isinstance(ancestor, expression_types): 361 ancestor = ancestor.parent 362 return t.cast(E, ancestor) 363 364 @property 365 def parent_select(self): 366 """ 367 Returns the parent select statement. 368 """ 369 return self.find_ancestor(Select) 370 371 @property 372 def same_parent(self): 373 """Returns if the parent is the same class as itself.""" 374 return type(self.parent) is self.__class__ 375 376 def root(self) -> Expression: 377 """ 378 Returns the root expression of this tree. 379 """ 380 expression = self 381 while expression.parent: 382 expression = expression.parent 383 return expression 384 385 def walk(self, bfs=True, prune=None): 386 """ 387 Returns a generator object which visits all nodes in this tree. 388 389 Args: 390 bfs (bool): if set to True the BFS traversal order will be applied, 391 otherwise the DFS traversal will be used instead. 392 prune ((node, parent, arg_key) -> bool): callable that returns True if 393 the generator should stop traversing this branch of the tree. 394 395 Returns: 396 the generator object. 397 """ 398 if bfs: 399 yield from self.bfs(prune=prune) 400 else: 401 yield from self.dfs(prune=prune) 402 403 def dfs(self, parent=None, key=None, prune=None): 404 """ 405 Returns a generator object which visits all nodes in this tree in 406 the DFS (Depth-first) order. 407 408 Returns: 409 The generator object. 410 """ 411 parent = parent or self.parent 412 yield self, parent, key 413 if prune and prune(self, parent, key): 414 return 415 416 for k, v in self.iter_expressions(): 417 yield from v.dfs(self, k, prune) 418 419 def bfs(self, prune=None): 420 """ 421 Returns a generator object which visits all nodes in this tree in 422 the BFS (Breadth-first) order. 423 424 Returns: 425 The generator object. 426 """ 427 queue = deque([(self, self.parent, None)]) 428 429 while queue: 430 item, parent, key = queue.popleft() 431 432 yield item, parent, key 433 if prune and prune(item, parent, key): 434 continue 435 436 for k, v in item.iter_expressions(): 437 queue.append((v, item, k)) 438 439 def unnest(self): 440 """ 441 Returns the first non parenthesis child or self. 442 """ 443 expression = self 444 while type(expression) is Paren: 445 expression = expression.this 446 return expression 447 448 def unalias(self): 449 """ 450 Returns the inner expression if this is an Alias. 451 """ 452 if isinstance(self, Alias): 453 return self.this 454 return self 455 456 def unnest_operands(self): 457 """ 458 Returns unnested operands as a tuple. 459 """ 460 return tuple(arg.unnest() for _, arg in self.iter_expressions()) 461 462 def flatten(self, unnest=True): 463 """ 464 Returns a generator which yields child nodes who's parents are the same class. 465 466 A AND B AND C -> [A, B, C] 467 """ 468 for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__): 469 if not type(node) is self.__class__: 470 yield node.unnest() if unnest else node 471 472 def __str__(self): 473 return self.sql() 474 475 def __repr__(self): 476 return self._to_s() 477 478 def sql(self, dialect: DialectType = None, **opts) -> str: 479 """ 480 Returns SQL string representation of this tree. 481 482 Args: 483 dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql"). 484 opts: other `sqlglot.generator.Generator` options. 485 486 Returns: 487 The SQL string. 488 """ 489 from sqlglot.dialects import Dialect 490 491 return Dialect.get_or_raise(dialect)().generate(self, **opts) 492 493 def _to_s(self, hide_missing: bool = True, level: int = 0) -> str: 494 indent = "" if not level else "\n" 495 indent += "".join([" "] * level) 496 left = f"({self.key.upper()} " 497 498 args: t.Dict[str, t.Any] = { 499 k: ", ".join( 500 v._to_s(hide_missing=hide_missing, level=level + 1) 501 if hasattr(v, "_to_s") 502 else str(v) 503 for v in ensure_list(vs) 504 if v is not None 505 ) 506 for k, vs in self.args.items() 507 } 508 args["comments"] = self.comments 509 args["type"] = self.type 510 args = {k: v for k, v in args.items() if v or not hide_missing} 511 512 right = ", ".join(f"{k}: {v}" for k, v in args.items()) 513 right += ")" 514 515 return indent + left + right 516 517 def transform(self, fun, *args, copy=True, **kwargs): 518 """ 519 Recursively visits all tree nodes (excluding already transformed ones) 520 and applies the given transformation function to each node. 521 522 Args: 523 fun (function): a function which takes a node as an argument and returns a 524 new transformed node or the same node without modifications. If the function 525 returns None, then the corresponding node will be removed from the syntax tree. 526 copy (bool): if set to True a new tree instance is constructed, otherwise the tree is 527 modified in place. 528 529 Returns: 530 The transformed tree. 531 """ 532 node = self.copy() if copy else self 533 new_node = fun(node, *args, **kwargs) 534 535 if new_node is None or not isinstance(new_node, Expression): 536 return new_node 537 if new_node is not node: 538 new_node.parent = node.parent 539 return new_node 540 541 replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs)) 542 return new_node 543 544 def replace(self, expression): 545 """ 546 Swap out this expression with a new expression. 547 548 For example:: 549 550 >>> tree = Select().select("x").from_("tbl") 551 >>> tree.find(Column).replace(Column(this="y")) 552 (COLUMN this: y) 553 >>> tree.sql() 554 'SELECT y FROM tbl' 555 556 Args: 557 expression (Expression|None): new node 558 559 Returns: 560 The new expression or expressions. 561 """ 562 if not self.parent: 563 return expression 564 565 parent = self.parent 566 self.parent = None 567 568 replace_children(parent, lambda child: expression if child is self else child) 569 return expression 570 571 def pop(self): 572 """ 573 Remove this expression from its AST. 574 575 Returns: 576 The popped expression. 577 """ 578 self.replace(None) 579 return self 580 581 def assert_is(self, type_): 582 """ 583 Assert that this `Expression` is an instance of `type_`. 584 585 If it is NOT an instance of `type_`, this raises an assertion error. 586 Otherwise, this returns this expression. 587 588 Examples: 589 This is useful for type security in chained expressions: 590 591 >>> import sqlglot 592 >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql() 593 'SELECT x, z FROM y' 594 """ 595 assert isinstance(self, type_) 596 return self 597 598 def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]: 599 """ 600 Checks if this expression is valid (e.g. all mandatory args are set). 601 602 Args: 603 args: a sequence of values that were used to instantiate a Func expression. This is used 604 to check that the provided arguments don't exceed the function argument limit. 605 606 Returns: 607 A list of error messages for all possible errors that were found. 608 """ 609 errors: t.List[str] = [] 610 611 for k in self.args: 612 if k not in self.arg_types: 613 errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") 614 for k, mandatory in self.arg_types.items(): 615 v = self.args.get(k) 616 if mandatory and (v is None or (isinstance(v, list) and not v)): 617 errors.append(f"Required keyword: '{k}' missing for {self.__class__}") 618 619 if ( 620 args 621 and isinstance(self, Func) 622 and len(args) > len(self.arg_types) 623 and not self.is_var_len_args 624 ): 625 errors.append( 626 f"The number of provided arguments ({len(args)}) is greater than " 627 f"the maximum number of supported arguments ({len(self.arg_types)})" 628 ) 629 630 return errors 631 632 def dump(self): 633 """ 634 Dump this Expression to a JSON-serializable dict. 635 """ 636 from sqlglot.serde import dump 637 638 return dump(self) 639 640 @classmethod 641 def load(cls, obj): 642 """ 643 Load a dict (as returned by `Expression.dump`) into an Expression instance. 644 """ 645 from sqlglot.serde import load 646 647 return load(obj) 648 649 650IntoType = t.Union[ 651 str, 652 t.Type[Expression], 653 t.Collection[t.Union[str, t.Type[Expression]]], 654] 655ExpOrStr = t.Union[str, Expression] 656 657 658class Condition(Expression): 659 def and_(self, *expressions, dialect=None, copy=True, **opts): 660 """ 661 AND this condition with one or multiple expressions. 662 663 Example: 664 >>> condition("x=1").and_("y=1").sql() 665 'x = 1 AND y = 1' 666 667 Args: 668 *expressions (str | Expression): the SQL code strings to parse. 669 If an `Expression` instance is passed, it will be used as-is. 670 dialect (str): the dialect used to parse the input expression. 671 copy (bool): whether or not to copy the involved expressions (only applies to Expressions). 672 opts (kwargs): other options to use to parse the input expressions. 673 674 Returns: 675 And: the new condition. 676 """ 677 return and_(self, *expressions, dialect=dialect, copy=copy, **opts) 678 679 def or_(self, *expressions, dialect=None, copy=True, **opts): 680 """ 681 OR this condition with one or multiple expressions. 682 683 Example: 684 >>> condition("x=1").or_("y=1").sql() 685 'x = 1 OR y = 1' 686 687 Args: 688 *expressions (str | Expression): the SQL code strings to parse. 689 If an `Expression` instance is passed, it will be used as-is. 690 dialect (str): the dialect used to parse the input expression. 691 copy (bool): whether or not to copy the involved expressions (only applies to Expressions). 692 opts (kwargs): other options to use to parse the input expressions. 693 694 Returns: 695 Or: the new condition. 696 """ 697 return or_(self, *expressions, dialect=dialect, copy=copy, **opts) 698 699 def not_(self, copy=True): 700 """ 701 Wrap this condition with NOT. 702 703 Example: 704 >>> condition("x=1").not_().sql() 705 'NOT x = 1' 706 707 Args: 708 copy (bool): whether or not to copy this object. 709 710 Returns: 711 Not: the new condition. 712 """ 713 return not_(self, copy=copy) 714 715 def as_( 716 self, 717 alias: str | Identifier, 718 quoted: t.Optional[bool] = None, 719 dialect: DialectType = None, 720 copy: bool = True, 721 **opts, 722 ) -> Alias: 723 return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts) 724 725 def _binop(self, klass: t.Type[E], other: t.Any, reverse: bool = False) -> E: 726 this = self.copy() 727 other = convert(other, copy=True) 728 if not isinstance(this, klass) and not isinstance(other, klass): 729 this = _wrap(this, Binary) 730 other = _wrap(other, Binary) 731 if reverse: 732 return klass(this=other, expression=this) 733 return klass(this=this, expression=other) 734 735 def __getitem__(self, other: ExpOrStr | t.Tuple[ExpOrStr]): 736 return Bracket( 737 this=self.copy(), expressions=[convert(e, copy=True) for e in ensure_list(other)] 738 ) 739 740 def isin( 741 self, *expressions: t.Any, query: t.Optional[ExpOrStr] = None, copy=True, **opts 742 ) -> In: 743 return In( 744 this=_maybe_copy(self, copy), 745 expressions=[convert(e, copy=copy) for e in expressions], 746 query=maybe_parse(query, copy=copy, **opts) if query else None, 747 ) 748 749 def between(self, low: t.Any, high: t.Any, copy=True, **opts) -> Between: 750 return Between( 751 this=_maybe_copy(self, copy), 752 low=convert(low, copy=copy, **opts), 753 high=convert(high, copy=copy, **opts), 754 ) 755 756 def is_(self, other: ExpOrStr) -> Is: 757 return self._binop(Is, other) 758 759 def like(self, other: ExpOrStr) -> Like: 760 return self._binop(Like, other) 761 762 def ilike(self, other: ExpOrStr) -> ILike: 763 return self._binop(ILike, other) 764 765 def eq(self, other: t.Any) -> EQ: 766 return self._binop(EQ, other) 767 768 def neq(self, other: t.Any) -> NEQ: 769 return self._binop(NEQ, other) 770 771 def rlike(self, other: ExpOrStr) -> RegexpLike: 772 return self._binop(RegexpLike, other) 773 774 def __lt__(self, other: t.Any) -> LT: 775 return self._binop(LT, other) 776 777 def __le__(self, other: t.Any) -> LTE: 778 return self._binop(LTE, other) 779 780 def __gt__(self, other: t.Any) -> GT: 781 return self._binop(GT, other) 782 783 def __ge__(self, other: t.Any) -> GTE: 784 return self._binop(GTE, other) 785 786 def __add__(self, other: t.Any) -> Add: 787 return self._binop(Add, other) 788 789 def __radd__(self, other: t.Any) -> Add: 790 return self._binop(Add, other, reverse=True) 791 792 def __sub__(self, other: t.Any) -> Sub: 793 return self._binop(Sub, other) 794 795 def __rsub__(self, other: t.Any) -> Sub: 796 return self._binop(Sub, other, reverse=True) 797 798 def __mul__(self, other: t.Any) -> Mul: 799 return self._binop(Mul, other) 800 801 def __rmul__(self, other: t.Any) -> Mul: 802 return self._binop(Mul, other, reverse=True) 803 804 def __truediv__(self, other: t.Any) -> Div: 805 return self._binop(Div, other) 806 807 def __rtruediv__(self, other: t.Any) -> Div: 808 return self._binop(Div, other, reverse=True) 809 810 def __floordiv__(self, other: t.Any) -> IntDiv: 811 return self._binop(IntDiv, other) 812 813 def __rfloordiv__(self, other: t.Any) -> IntDiv: 814 return self._binop(IntDiv, other, reverse=True) 815 816 def __mod__(self, other: t.Any) -> Mod: 817 return self._binop(Mod, other) 818 819 def __rmod__(self, other: t.Any) -> Mod: 820 return self._binop(Mod, other, reverse=True) 821 822 def __pow__(self, other: t.Any) -> Pow: 823 return self._binop(Pow, other) 824 825 def __rpow__(self, other: t.Any) -> Pow: 826 return self._binop(Pow, other, reverse=True) 827 828 def __and__(self, other: t.Any) -> And: 829 return self._binop(And, other) 830 831 def __rand__(self, other: t.Any) -> And: 832 return self._binop(And, other, reverse=True) 833 834 def __or__(self, other: t.Any) -> Or: 835 return self._binop(Or, other) 836 837 def __ror__(self, other: t.Any) -> Or: 838 return self._binop(Or, other, reverse=True) 839 840 def __neg__(self) -> Neg: 841 return Neg(this=_wrap(self.copy(), Binary)) 842 843 def __invert__(self) -> Not: 844 return not_(self.copy()) 845 846 847class Predicate(Condition): 848 """Relationships like x = y, x > 1, x >= y.""" 849 850 851class DerivedTable(Expression): 852 @property 853 def alias_column_names(self): 854 table_alias = self.args.get("alias") 855 if not table_alias: 856 return [] 857 column_list = table_alias.assert_is(TableAlias).args.get("columns") or [] 858 return [c.name for c in column_list] 859 860 @property 861 def selects(self): 862 return self.this.selects if isinstance(self.this, Subqueryable) else [] 863 864 @property 865 def named_selects(self): 866 return [select.output_name for select in self.selects] 867 868 869class Unionable(Expression): 870 def union(self, expression, distinct=True, dialect=None, **opts): 871 """ 872 Builds a UNION expression. 873 874 Example: 875 >>> import sqlglot 876 >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql() 877 'SELECT * FROM foo UNION SELECT * FROM bla' 878 879 Args: 880 expression (str | Expression): the SQL code string. 881 If an `Expression` instance is passed, it will be used as-is. 882 distinct (bool): set the DISTINCT flag if and only if this is true. 883 dialect (str): the dialect used to parse the input expression. 884 opts (kwargs): other options to use to parse the input expressions. 885 Returns: 886 Union: the Union expression. 887 """ 888 return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 889 890 def intersect(self, expression, distinct=True, dialect=None, **opts): 891 """ 892 Builds an INTERSECT expression. 893 894 Example: 895 >>> import sqlglot 896 >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql() 897 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 898 899 Args: 900 expression (str | Expression): the SQL code string. 901 If an `Expression` instance is passed, it will be used as-is. 902 distinct (bool): set the DISTINCT flag if and only if this is true. 903 dialect (str): the dialect used to parse the input expression. 904 opts (kwargs): other options to use to parse the input expressions. 905 Returns: 906 Intersect: the Intersect expression 907 """ 908 return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 909 910 def except_(self, expression, distinct=True, dialect=None, **opts): 911 """ 912 Builds an EXCEPT expression. 913 914 Example: 915 >>> import sqlglot 916 >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql() 917 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 918 919 Args: 920 expression (str | Expression): the SQL code string. 921 If an `Expression` instance is passed, it will be used as-is. 922 distinct (bool): set the DISTINCT flag if and only if this is true. 923 dialect (str): the dialect used to parse the input expression. 924 opts (kwargs): other options to use to parse the input expressions. 925 Returns: 926 Except: the Except expression 927 """ 928 return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 929 930 931class UDTF(DerivedTable, Unionable): 932 @property 933 def selects(self): 934 alias = self.args.get("alias") 935 return alias.columns if alias else [] 936 937 938class Cache(Expression): 939 arg_types = { 940 "with": False, 941 "this": True, 942 "lazy": False, 943 "options": False, 944 "expression": False, 945 } 946 947 948class Uncache(Expression): 949 arg_types = {"this": True, "exists": False} 950 951 952class Create(Expression): 953 arg_types = { 954 "with": False, 955 "this": True, 956 "kind": True, 957 "expression": False, 958 "exists": False, 959 "properties": False, 960 "replace": False, 961 "unique": False, 962 "indexes": False, 963 "no_schema_binding": False, 964 "begin": False, 965 "clone": False, 966 } 967 968 969# https://docs.snowflake.com/en/sql-reference/sql/create-clone 970class Clone(Expression): 971 arg_types = { 972 "this": True, 973 "when": False, 974 "kind": False, 975 "expression": False, 976 } 977 978 979class Describe(Expression): 980 arg_types = {"this": True, "kind": False} 981 982 983class Pragma(Expression): 984 pass 985 986 987class Set(Expression): 988 arg_types = {"expressions": False} 989 990 991class SetItem(Expression): 992 arg_types = { 993 "this": False, 994 "expressions": False, 995 "kind": False, 996 "collate": False, # MySQL SET NAMES statement 997 "global": False, 998 } 999 1000 1001class Show(Expression): 1002 arg_types = { 1003 "this": True, 1004 "target": False, 1005 "offset": False, 1006 "limit": False, 1007 "like": False, 1008 "where": False, 1009 "db": False, 1010 "full": False, 1011 "mutex": False, 1012 "query": False, 1013 "channel": False, 1014 "global": False, 1015 "log": False, 1016 "position": False, 1017 "types": False, 1018 } 1019 1020 1021class UserDefinedFunction(Expression): 1022 arg_types = {"this": True, "expressions": False, "wrapped": False} 1023 1024 1025class CharacterSet(Expression): 1026 arg_types = {"this": True, "default": False} 1027 1028 1029class With(Expression): 1030 arg_types = {"expressions": True, "recursive": False} 1031 1032 @property 1033 def recursive(self) -> bool: 1034 return bool(self.args.get("recursive")) 1035 1036 1037class WithinGroup(Expression): 1038 arg_types = {"this": True, "expression": False} 1039 1040 1041class CTE(DerivedTable): 1042 arg_types = {"this": True, "alias": True} 1043 1044 1045class TableAlias(Expression): 1046 arg_types = {"this": False, "columns": False} 1047 1048 @property 1049 def columns(self): 1050 return self.args.get("columns") or [] 1051 1052 1053class BitString(Condition): 1054 pass 1055 1056 1057class HexString(Condition): 1058 pass 1059 1060 1061class ByteString(Condition): 1062 pass 1063 1064 1065class Column(Condition): 1066 arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False} 1067 1068 @property 1069 def table(self) -> str: 1070 return self.text("table") 1071 1072 @property 1073 def db(self) -> str: 1074 return self.text("db") 1075 1076 @property 1077 def catalog(self) -> str: 1078 return self.text("catalog") 1079 1080 @property 1081 def output_name(self) -> str: 1082 return self.name 1083 1084 @property 1085 def parts(self) -> t.List[Identifier]: 1086 """Return the parts of a column in order catalog, db, table, name.""" 1087 return [ 1088 t.cast(Identifier, self.args[part]) 1089 for part in ("catalog", "db", "table", "this") 1090 if self.args.get(part) 1091 ] 1092 1093 def to_dot(self) -> Dot: 1094 """Converts the column into a dot expression.""" 1095 parts = self.parts 1096 parent = self.parent 1097 1098 while parent: 1099 if isinstance(parent, Dot): 1100 parts.append(parent.expression) 1101 parent = parent.parent 1102 1103 return Dot.build(parts) 1104 1105 1106class ColumnPosition(Expression): 1107 arg_types = {"this": False, "position": True} 1108 1109 1110class ColumnDef(Expression): 1111 arg_types = { 1112 "this": True, 1113 "kind": False, 1114 "constraints": False, 1115 "exists": False, 1116 "position": False, 1117 } 1118 1119 @property 1120 def constraints(self) -> t.List[ColumnConstraint]: 1121 return self.args.get("constraints") or [] 1122 1123 1124class AlterColumn(Expression): 1125 arg_types = { 1126 "this": True, 1127 "dtype": False, 1128 "collate": False, 1129 "using": False, 1130 "default": False, 1131 "drop": False, 1132 } 1133 1134 1135class RenameTable(Expression): 1136 pass 1137 1138 1139class SetTag(Expression): 1140 arg_types = {"expressions": True, "unset": False} 1141 1142 1143class Comment(Expression): 1144 arg_types = {"this": True, "kind": True, "expression": True, "exists": False} 1145 1146 1147# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl 1148class MergeTreeTTLAction(Expression): 1149 arg_types = { 1150 "this": True, 1151 "delete": False, 1152 "recompress": False, 1153 "to_disk": False, 1154 "to_volume": False, 1155 } 1156 1157 1158# https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/mergetree#mergetree-table-ttl 1159class MergeTreeTTL(Expression): 1160 arg_types = { 1161 "expressions": True, 1162 "where": False, 1163 "group": False, 1164 "aggregates": False, 1165 } 1166 1167 1168class ColumnConstraint(Expression): 1169 arg_types = {"this": False, "kind": True} 1170 1171 @property 1172 def kind(self) -> ColumnConstraintKind: 1173 return self.args["kind"] 1174 1175 1176class ColumnConstraintKind(Expression): 1177 pass 1178 1179 1180class AutoIncrementColumnConstraint(ColumnConstraintKind): 1181 pass 1182 1183 1184class CaseSpecificColumnConstraint(ColumnConstraintKind): 1185 arg_types = {"not_": True} 1186 1187 1188class CharacterSetColumnConstraint(ColumnConstraintKind): 1189 arg_types = {"this": True} 1190 1191 1192class CheckColumnConstraint(ColumnConstraintKind): 1193 pass 1194 1195 1196class CollateColumnConstraint(ColumnConstraintKind): 1197 pass 1198 1199 1200class CommentColumnConstraint(ColumnConstraintKind): 1201 pass 1202 1203 1204class CompressColumnConstraint(ColumnConstraintKind): 1205 pass 1206 1207 1208class DateFormatColumnConstraint(ColumnConstraintKind): 1209 arg_types = {"this": True} 1210 1211 1212class DefaultColumnConstraint(ColumnConstraintKind): 1213 pass 1214 1215 1216class EncodeColumnConstraint(ColumnConstraintKind): 1217 pass 1218 1219 1220class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind): 1221 # this: True -> ALWAYS, this: False -> BY DEFAULT 1222 arg_types = { 1223 "this": False, 1224 "on_null": False, 1225 "start": False, 1226 "increment": False, 1227 "minvalue": False, 1228 "maxvalue": False, 1229 "cycle": False, 1230 } 1231 1232 1233class InlineLengthColumnConstraint(ColumnConstraintKind): 1234 pass 1235 1236 1237class NotNullColumnConstraint(ColumnConstraintKind): 1238 arg_types = {"allow_null": False} 1239 1240 1241# https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html 1242class OnUpdateColumnConstraint(ColumnConstraintKind): 1243 pass 1244 1245 1246class PrimaryKeyColumnConstraint(ColumnConstraintKind): 1247 arg_types = {"desc": False} 1248 1249 1250class TitleColumnConstraint(ColumnConstraintKind): 1251 pass 1252 1253 1254class UniqueColumnConstraint(ColumnConstraintKind): 1255 arg_types: t.Dict[str, t.Any] = {} 1256 1257 1258class UppercaseColumnConstraint(ColumnConstraintKind): 1259 arg_types: t.Dict[str, t.Any] = {} 1260 1261 1262class PathColumnConstraint(ColumnConstraintKind): 1263 pass 1264 1265 1266class Constraint(Expression): 1267 arg_types = {"this": True, "expressions": True} 1268 1269 1270class Delete(Expression): 1271 arg_types = {"with": False, "this": False, "using": False, "where": False, "returning": False} 1272 1273 def delete( 1274 self, 1275 table: ExpOrStr, 1276 dialect: DialectType = None, 1277 copy: bool = True, 1278 **opts, 1279 ) -> Delete: 1280 """ 1281 Create a DELETE expression or replace the table on an existing DELETE expression. 1282 1283 Example: 1284 >>> delete("tbl").sql() 1285 'DELETE FROM tbl' 1286 1287 Args: 1288 table: the table from which to delete. 1289 dialect: the dialect used to parse the input expression. 1290 copy: if `False`, modify this expression instance in-place. 1291 opts: other options to use to parse the input expressions. 1292 1293 Returns: 1294 Delete: the modified expression. 1295 """ 1296 return _apply_builder( 1297 expression=table, 1298 instance=self, 1299 arg="this", 1300 dialect=dialect, 1301 into=Table, 1302 copy=copy, 1303 **opts, 1304 ) 1305 1306 def where( 1307 self, 1308 *expressions: ExpOrStr, 1309 append: bool = True, 1310 dialect: DialectType = None, 1311 copy: bool = True, 1312 **opts, 1313 ) -> Delete: 1314 """ 1315 Append to or set the WHERE expressions. 1316 1317 Example: 1318 >>> delete("tbl").where("x = 'a' OR x < 'b'").sql() 1319 "DELETE FROM tbl WHERE x = 'a' OR x < 'b'" 1320 1321 Args: 1322 *expressions: the SQL code strings to parse. 1323 If an `Expression` instance is passed, it will be used as-is. 1324 Multiple expressions are combined with an AND operator. 1325 append: if `True`, AND the new expressions to any existing expression. 1326 Otherwise, this resets the expression. 1327 dialect: the dialect used to parse the input expressions. 1328 copy: if `False`, modify this expression instance in-place. 1329 opts: other options to use to parse the input expressions. 1330 1331 Returns: 1332 Delete: the modified expression. 1333 """ 1334 return _apply_conjunction_builder( 1335 *expressions, 1336 instance=self, 1337 arg="where", 1338 append=append, 1339 into=Where, 1340 dialect=dialect, 1341 copy=copy, 1342 **opts, 1343 ) 1344 1345 def returning( 1346 self, 1347 expression: ExpOrStr, 1348 dialect: DialectType = None, 1349 copy: bool = True, 1350 **opts, 1351 ) -> Delete: 1352 """ 1353 Set the RETURNING expression. Not supported by all dialects. 1354 1355 Example: 1356 >>> delete("tbl").returning("*", dialect="postgres").sql() 1357 'DELETE FROM tbl RETURNING *' 1358 1359 Args: 1360 expression: the SQL code strings to parse. 1361 If an `Expression` instance is passed, it will be used as-is. 1362 dialect: the dialect used to parse the input expressions. 1363 copy: if `False`, modify this expression instance in-place. 1364 opts: other options to use to parse the input expressions. 1365 1366 Returns: 1367 Delete: the modified expression. 1368 """ 1369 return _apply_builder( 1370 expression=expression, 1371 instance=self, 1372 arg="returning", 1373 prefix="RETURNING", 1374 dialect=dialect, 1375 copy=copy, 1376 into=Returning, 1377 **opts, 1378 ) 1379 1380 1381class Drop(Expression): 1382 arg_types = { 1383 "this": False, 1384 "kind": False, 1385 "exists": False, 1386 "temporary": False, 1387 "materialized": False, 1388 "cascade": False, 1389 "constraints": False, 1390 "purge": False, 1391 } 1392 1393 1394class Filter(Expression): 1395 arg_types = {"this": True, "expression": True} 1396 1397 1398class Check(Expression): 1399 pass 1400 1401 1402class Directory(Expression): 1403 # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html 1404 arg_types = {"this": True, "local": False, "row_format": False} 1405 1406 1407class ForeignKey(Expression): 1408 arg_types = { 1409 "expressions": True, 1410 "reference": False, 1411 "delete": False, 1412 "update": False, 1413 } 1414 1415 1416class PrimaryKey(Expression): 1417 arg_types = {"expressions": True, "options": False} 1418 1419 1420class Unique(Expression): 1421 arg_types = {"expressions": True} 1422 1423 1424# https://www.postgresql.org/docs/9.1/sql-selectinto.html 1425# https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_INTO.html#r_SELECT_INTO-examples 1426class Into(Expression): 1427 arg_types = {"this": True, "temporary": False, "unlogged": False} 1428 1429 1430class From(Expression): 1431 @property 1432 def name(self) -> str: 1433 return self.this.name 1434 1435 @property 1436 def alias_or_name(self) -> str: 1437 return self.this.alias_or_name 1438 1439 1440class Having(Expression): 1441 pass 1442 1443 1444class Hint(Expression): 1445 arg_types = {"expressions": True} 1446 1447 1448class JoinHint(Expression): 1449 arg_types = {"this": True, "expressions": True} 1450 1451 1452class Identifier(Expression): 1453 arg_types = {"this": True, "quoted": False} 1454 1455 @property 1456 def quoted(self): 1457 return bool(self.args.get("quoted")) 1458 1459 @property 1460 def hashable_args(self) -> t.Any: 1461 if self.quoted and any(char.isupper() for char in self.this): 1462 return (self.this, self.quoted) 1463 return self.this.lower() 1464 1465 @property 1466 def output_name(self): 1467 return self.name 1468 1469 1470class Index(Expression): 1471 arg_types = { 1472 "this": False, 1473 "table": False, 1474 "where": False, 1475 "columns": False, 1476 "unique": False, 1477 "primary": False, 1478 "amp": False, # teradata 1479 } 1480 1481 1482class Insert(Expression): 1483 arg_types = { 1484 "with": False, 1485 "this": True, 1486 "expression": False, 1487 "conflict": False, 1488 "returning": False, 1489 "overwrite": False, 1490 "exists": False, 1491 "partition": False, 1492 "alternative": False, 1493 } 1494 1495 def with_( 1496 self, 1497 alias: ExpOrStr, 1498 as_: ExpOrStr, 1499 recursive: t.Optional[bool] = None, 1500 append: bool = True, 1501 dialect: DialectType = None, 1502 copy: bool = True, 1503 **opts, 1504 ) -> Insert: 1505 """ 1506 Append to or set the common table expressions. 1507 1508 Example: 1509 >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql() 1510 'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte' 1511 1512 Args: 1513 alias: the SQL code string to parse as the table name. 1514 If an `Expression` instance is passed, this is used as-is. 1515 as_: the SQL code string to parse as the table expression. 1516 If an `Expression` instance is passed, it will be used as-is. 1517 recursive: set the RECURSIVE part of the expression. Defaults to `False`. 1518 append: if `True`, add to any existing expressions. 1519 Otherwise, this resets the expressions. 1520 dialect: the dialect used to parse the input expression. 1521 copy: if `False`, modify this expression instance in-place. 1522 opts: other options to use to parse the input expressions. 1523 1524 Returns: 1525 The modified expression. 1526 """ 1527 return _apply_cte_builder( 1528 self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts 1529 ) 1530 1531 1532class OnConflict(Expression): 1533 arg_types = { 1534 "duplicate": False, 1535 "expressions": False, 1536 "nothing": False, 1537 "key": False, 1538 "constraint": False, 1539 } 1540 1541 1542class Returning(Expression): 1543 arg_types = {"expressions": True} 1544 1545 1546# https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html 1547class Introducer(Expression): 1548 arg_types = {"this": True, "expression": True} 1549 1550 1551# national char, like n'utf8' 1552class National(Expression): 1553 pass 1554 1555 1556class LoadData(Expression): 1557 arg_types = { 1558 "this": True, 1559 "local": False, 1560 "overwrite": False, 1561 "inpath": True, 1562 "partition": False, 1563 "input_format": False, 1564 "serde": False, 1565 } 1566 1567 1568class Partition(Expression): 1569 arg_types = {"expressions": True} 1570 1571 1572class Fetch(Expression): 1573 arg_types = { 1574 "direction": False, 1575 "count": False, 1576 "percent": False, 1577 "with_ties": False, 1578 } 1579 1580 1581class Group(Expression): 1582 arg_types = { 1583 "expressions": False, 1584 "grouping_sets": False, 1585 "cube": False, 1586 "rollup": False, 1587 "totals": False, 1588 } 1589 1590 1591class Lambda(Expression): 1592 arg_types = {"this": True, "expressions": True} 1593 1594 1595class Limit(Expression): 1596 arg_types = {"this": False, "expression": True} 1597 1598 1599class Literal(Condition): 1600 arg_types = {"this": True, "is_string": True} 1601 1602 @property 1603 def hashable_args(self) -> t.Any: 1604 return (self.this, self.args.get("is_string")) 1605 1606 @classmethod 1607 def number(cls, number) -> Literal: 1608 return cls(this=str(number), is_string=False) 1609 1610 @classmethod 1611 def string(cls, string) -> Literal: 1612 return cls(this=str(string), is_string=True) 1613 1614 @property 1615 def output_name(self): 1616 return self.name 1617 1618 1619class Join(Expression): 1620 arg_types = { 1621 "this": True, 1622 "on": False, 1623 "side": False, 1624 "kind": False, 1625 "using": False, 1626 "natural": False, 1627 "global": False, 1628 "hint": False, 1629 } 1630 1631 @property 1632 def kind(self): 1633 return self.text("kind").upper() 1634 1635 @property 1636 def side(self): 1637 return self.text("side").upper() 1638 1639 @property 1640 def hint(self): 1641 return self.text("hint").upper() 1642 1643 @property 1644 def alias_or_name(self): 1645 return self.this.alias_or_name 1646 1647 def on(self, *expressions, append=True, dialect=None, copy=True, **opts): 1648 """ 1649 Append to or set the ON expressions. 1650 1651 Example: 1652 >>> import sqlglot 1653 >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql() 1654 'JOIN x ON y = 1' 1655 1656 Args: 1657 *expressions (str | Expression): the SQL code strings to parse. 1658 If an `Expression` instance is passed, it will be used as-is. 1659 Multiple expressions are combined with an AND operator. 1660 append (bool): if `True`, AND the new expressions to any existing expression. 1661 Otherwise, this resets the expression. 1662 dialect (str): the dialect used to parse the input expressions. 1663 copy (bool): if `False`, modify this expression instance in-place. 1664 opts (kwargs): other options to use to parse the input expressions. 1665 1666 Returns: 1667 Join: the modified join expression. 1668 """ 1669 join = _apply_conjunction_builder( 1670 *expressions, 1671 instance=self, 1672 arg="on", 1673 append=append, 1674 dialect=dialect, 1675 copy=copy, 1676 **opts, 1677 ) 1678 1679 if join.kind == "CROSS": 1680 join.set("kind", None) 1681 1682 return join 1683 1684 def using(self, *expressions, append=True, dialect=None, copy=True, **opts): 1685 """ 1686 Append to or set the USING expressions. 1687 1688 Example: 1689 >>> import sqlglot 1690 >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 1691 'JOIN x USING (foo, bla)' 1692 1693 Args: 1694 *expressions (str | Expression): the SQL code strings to parse. 1695 If an `Expression` instance is passed, it will be used as-is. 1696 append (bool): if `True`, concatenate the new expressions to the existing "using" list. 1697 Otherwise, this resets the expression. 1698 dialect (str): the dialect used to parse the input expressions. 1699 copy (bool): if `False`, modify this expression instance in-place. 1700 opts (kwargs): other options to use to parse the input expressions. 1701 1702 Returns: 1703 Join: the modified join expression. 1704 """ 1705 join = _apply_list_builder( 1706 *expressions, 1707 instance=self, 1708 arg="using", 1709 append=append, 1710 dialect=dialect, 1711 copy=copy, 1712 **opts, 1713 ) 1714 1715 if join.kind == "CROSS": 1716 join.set("kind", None) 1717 1718 return join 1719 1720 1721class Lateral(UDTF): 1722 arg_types = {"this": True, "view": False, "outer": False, "alias": False} 1723 1724 1725class MatchRecognize(Expression): 1726 arg_types = { 1727 "partition_by": False, 1728 "order": False, 1729 "measures": False, 1730 "rows": False, 1731 "after": False, 1732 "pattern": False, 1733 "define": False, 1734 "alias": False, 1735 } 1736 1737 1738# Clickhouse FROM FINAL modifier 1739# https://clickhouse.com/docs/en/sql-reference/statements/select/from/#final-modifier 1740class Final(Expression): 1741 pass 1742 1743 1744class Offset(Expression): 1745 arg_types = {"this": False, "expression": True} 1746 1747 1748class Order(Expression): 1749 arg_types = {"this": False, "expressions": True} 1750 1751 1752# hive specific sorts 1753# https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy 1754class Cluster(Order): 1755 pass 1756 1757 1758class Distribute(Order): 1759 pass 1760 1761 1762class Sort(Order): 1763 pass 1764 1765 1766class Ordered(Expression): 1767 arg_types = {"this": True, "desc": True, "nulls_first": True} 1768 1769 1770class Property(Expression): 1771 arg_types = {"this": True, "value": True} 1772 1773 1774class AfterJournalProperty(Property): 1775 arg_types = {"no": True, "dual": False, "local": False} 1776 1777 1778class AlgorithmProperty(Property): 1779 arg_types = {"this": True} 1780 1781 1782class AutoIncrementProperty(Property): 1783 arg_types = {"this": True} 1784 1785 1786class BlockCompressionProperty(Property): 1787 arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True} 1788 1789 1790class CharacterSetProperty(Property): 1791 arg_types = {"this": True, "default": True} 1792 1793 1794class ChecksumProperty(Property): 1795 arg_types = {"on": False, "default": False} 1796 1797 1798class CollateProperty(Property): 1799 arg_types = {"this": True} 1800 1801 1802class DataBlocksizeProperty(Property): 1803 arg_types = {"size": False, "units": False, "min": False, "default": False} 1804 1805 1806class DefinerProperty(Property): 1807 arg_types = {"this": True} 1808 1809 1810class DistKeyProperty(Property): 1811 arg_types = {"this": True} 1812 1813 1814class DistStyleProperty(Property): 1815 arg_types = {"this": True} 1816 1817 1818class EngineProperty(Property): 1819 arg_types = {"this": True} 1820 1821 1822class ExecuteAsProperty(Property): 1823 arg_types = {"this": True} 1824 1825 1826class ExternalProperty(Property): 1827 arg_types = {"this": False} 1828 1829 1830class FallbackProperty(Property): 1831 arg_types = {"no": True, "protection": False} 1832 1833 1834class FileFormatProperty(Property): 1835 arg_types = {"this": True} 1836 1837 1838class FreespaceProperty(Property): 1839 arg_types = {"this": True, "percent": False} 1840 1841 1842class InputOutputFormat(Expression): 1843 arg_types = {"input_format": False, "output_format": False} 1844 1845 1846class IsolatedLoadingProperty(Property): 1847 arg_types = { 1848 "no": True, 1849 "concurrent": True, 1850 "for_all": True, 1851 "for_insert": True, 1852 "for_none": True, 1853 } 1854 1855 1856class JournalProperty(Property): 1857 arg_types = {"no": True, "dual": False, "before": False} 1858 1859 1860class LanguageProperty(Property): 1861 arg_types = {"this": True} 1862 1863 1864class LikeProperty(Property): 1865 arg_types = {"this": True, "expressions": False} 1866 1867 1868class LocationProperty(Property): 1869 arg_types = {"this": True} 1870 1871 1872class LockingProperty(Property): 1873 arg_types = { 1874 "this": False, 1875 "kind": True, 1876 "for_or_in": True, 1877 "lock_type": True, 1878 "override": False, 1879 } 1880 1881 1882class LogProperty(Property): 1883 arg_types = {"no": True} 1884 1885 1886class MaterializedProperty(Property): 1887 arg_types = {"this": False} 1888 1889 1890class MergeBlockRatioProperty(Property): 1891 arg_types = {"this": False, "no": False, "default": False, "percent": False} 1892 1893 1894class NoPrimaryIndexProperty(Property): 1895 arg_types = {"this": False} 1896 1897 1898class OnCommitProperty(Property): 1899 arg_type = {"this": False} 1900 1901 1902class PartitionedByProperty(Property): 1903 arg_types = {"this": True} 1904 1905 1906class ReturnsProperty(Property): 1907 arg_types = {"this": True, "is_table": False, "table": False} 1908 1909 1910class RowFormatProperty(Property): 1911 arg_types = {"this": True} 1912 1913 1914class RowFormatDelimitedProperty(Property): 1915 # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml 1916 arg_types = { 1917 "fields": False, 1918 "escaped": False, 1919 "collection_items": False, 1920 "map_keys": False, 1921 "lines": False, 1922 "null": False, 1923 "serde": False, 1924 } 1925 1926 1927class RowFormatSerdeProperty(Property): 1928 arg_types = {"this": True} 1929 1930 1931class SchemaCommentProperty(Property): 1932 arg_types = {"this": True} 1933 1934 1935class SerdeProperties(Property): 1936 arg_types = {"expressions": True} 1937 1938 1939class SetProperty(Property): 1940 arg_types = {"multi": True} 1941 1942 1943class SettingsProperty(Property): 1944 arg_types = {"expressions": True} 1945 1946 1947class SortKeyProperty(Property): 1948 arg_types = {"this": True, "compound": False} 1949 1950 1951class SqlSecurityProperty(Property): 1952 arg_types = {"definer": True} 1953 1954 1955class StabilityProperty(Property): 1956 arg_types = {"this": True} 1957 1958 1959class TableFormatProperty(Property): 1960 arg_types = {"this": True} 1961 1962 1963class TemporaryProperty(Property): 1964 arg_types = {"global_": True} 1965 1966 1967class TransientProperty(Property): 1968 arg_types = {"this": False} 1969 1970 1971class VolatileProperty(Property): 1972 arg_types = {"this": False} 1973 1974 1975class WithDataProperty(Property): 1976 arg_types = {"no": True, "statistics": False} 1977 1978 1979class WithJournalTableProperty(Property): 1980 arg_types = {"this": True} 1981 1982 1983class Properties(Expression): 1984 arg_types = {"expressions": True} 1985 1986 NAME_TO_PROPERTY = { 1987 "ALGORITHM": AlgorithmProperty, 1988 "AUTO_INCREMENT": AutoIncrementProperty, 1989 "CHARACTER SET": CharacterSetProperty, 1990 "COLLATE": CollateProperty, 1991 "COMMENT": SchemaCommentProperty, 1992 "DEFINER": DefinerProperty, 1993 "DISTKEY": DistKeyProperty, 1994 "DISTSTYLE": DistStyleProperty, 1995 "ENGINE": EngineProperty, 1996 "EXECUTE AS": ExecuteAsProperty, 1997 "FORMAT": FileFormatProperty, 1998 "LANGUAGE": LanguageProperty, 1999 "LOCATION": LocationProperty, 2000 "PARTITIONED_BY": PartitionedByProperty, 2001 "RETURNS": ReturnsProperty, 2002 "ROW_FORMAT": RowFormatProperty, 2003 "SORTKEY": SortKeyProperty, 2004 "TABLE_FORMAT": TableFormatProperty, 2005 } 2006 2007 PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()} 2008 2009 # CREATE property locations 2010 # Form: schema specified 2011 # create [POST_CREATE] 2012 # table a [POST_NAME] 2013 # (b int) [POST_SCHEMA] 2014 # with ([POST_WITH]) 2015 # index (b) [POST_INDEX] 2016 # 2017 # Form: alias selection 2018 # create [POST_CREATE] 2019 # table a [POST_NAME] 2020 # as [POST_ALIAS] (select * from b) [POST_EXPRESSION] 2021 # index (c) [POST_INDEX] 2022 class Location(AutoName): 2023 POST_CREATE = auto() 2024 POST_NAME = auto() 2025 POST_SCHEMA = auto() 2026 POST_WITH = auto() 2027 POST_ALIAS = auto() 2028 POST_EXPRESSION = auto() 2029 POST_INDEX = auto() 2030 UNSUPPORTED = auto() 2031 2032 @classmethod 2033 def from_dict(cls, properties_dict) -> Properties: 2034 expressions = [] 2035 for key, value in properties_dict.items(): 2036 property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) 2037 if property_cls: 2038 expressions.append(property_cls(this=convert(value))) 2039 else: 2040 expressions.append(Property(this=Literal.string(key), value=convert(value))) 2041 2042 return cls(expressions=expressions) 2043 2044 2045class Qualify(Expression): 2046 pass 2047 2048 2049# https://www.ibm.com/docs/en/ias?topic=procedures-return-statement-in-sql 2050class Return(Expression): 2051 pass 2052 2053 2054class Reference(Expression): 2055 arg_types = {"this": True, "expressions": False, "options": False} 2056 2057 2058class Tuple(Expression): 2059 arg_types = {"expressions": False} 2060 2061 def isin( 2062 self, *expressions: t.Any, query: t.Optional[ExpOrStr] = None, copy=True, **opts 2063 ) -> In: 2064 return In( 2065 this=_maybe_copy(self, copy), 2066 expressions=[convert(e, copy=copy) for e in expressions], 2067 query=maybe_parse(query, copy=copy, **opts) if query else None, 2068 ) 2069 2070 2071class Subqueryable(Unionable): 2072 def subquery(self, alias=None, copy=True) -> Subquery: 2073 """ 2074 Convert this expression to an aliased expression that can be used as a Subquery. 2075 2076 Example: 2077 >>> subquery = Select().select("x").from_("tbl").subquery() 2078 >>> Select().select("x").from_(subquery).sql() 2079 'SELECT x FROM (SELECT x FROM tbl)' 2080 2081 Args: 2082 alias (str | Identifier): an optional alias for the subquery 2083 copy (bool): if `False`, modify this expression instance in-place. 2084 2085 Returns: 2086 Alias: the subquery 2087 """ 2088 instance = _maybe_copy(self, copy) 2089 if not isinstance(alias, Expression): 2090 alias = TableAlias(this=to_identifier(alias)) if alias else None 2091 2092 return Subquery(this=instance, alias=alias) 2093 2094 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2095 raise NotImplementedError 2096 2097 @property 2098 def ctes(self): 2099 with_ = self.args.get("with") 2100 if not with_: 2101 return [] 2102 return with_.expressions 2103 2104 @property 2105 def selects(self): 2106 raise NotImplementedError("Subqueryable objects must implement `selects`") 2107 2108 @property 2109 def named_selects(self): 2110 raise NotImplementedError("Subqueryable objects must implement `named_selects`") 2111 2112 def with_( 2113 self, 2114 alias: ExpOrStr, 2115 as_: ExpOrStr, 2116 recursive: t.Optional[bool] = None, 2117 append: bool = True, 2118 dialect: DialectType = None, 2119 copy: bool = True, 2120 **opts, 2121 ) -> Subqueryable: 2122 """ 2123 Append to or set the common table expressions. 2124 2125 Example: 2126 >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql() 2127 'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2' 2128 2129 Args: 2130 alias: the SQL code string to parse as the table name. 2131 If an `Expression` instance is passed, this is used as-is. 2132 as_: the SQL code string to parse as the table expression. 2133 If an `Expression` instance is passed, it will be used as-is. 2134 recursive: set the RECURSIVE part of the expression. Defaults to `False`. 2135 append: if `True`, add to any existing expressions. 2136 Otherwise, this resets the expressions. 2137 dialect: the dialect used to parse the input expression. 2138 copy: if `False`, modify this expression instance in-place. 2139 opts: other options to use to parse the input expressions. 2140 2141 Returns: 2142 The modified expression. 2143 """ 2144 return _apply_cte_builder( 2145 self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts 2146 ) 2147 2148 2149QUERY_MODIFIERS = { 2150 "match": False, 2151 "laterals": False, 2152 "joins": False, 2153 "pivots": False, 2154 "where": False, 2155 "group": False, 2156 "having": False, 2157 "qualify": False, 2158 "windows": False, 2159 "distribute": False, 2160 "sort": False, 2161 "cluster": False, 2162 "order": False, 2163 "limit": False, 2164 "offset": False, 2165 "locks": False, 2166 "sample": False, 2167 "settings": False, 2168 "format": False, 2169} 2170 2171 2172class Table(Expression): 2173 arg_types = { 2174 "this": True, 2175 "alias": False, 2176 "db": False, 2177 "catalog": False, 2178 "laterals": False, 2179 "joins": False, 2180 "pivots": False, 2181 "hints": False, 2182 "system_time": False, 2183 } 2184 2185 @property 2186 def db(self) -> str: 2187 return self.text("db") 2188 2189 @property 2190 def catalog(self) -> str: 2191 return self.text("catalog") 2192 2193 @property 2194 def parts(self) -> t.List[Identifier]: 2195 """Return the parts of a column in order catalog, db, table.""" 2196 return [ 2197 t.cast(Identifier, self.args[part]) 2198 for part in ("catalog", "db", "this") 2199 if self.args.get(part) 2200 ] 2201 2202 2203# See the TSQL "Querying data in a system-versioned temporal table" page 2204class SystemTime(Expression): 2205 arg_types = { 2206 "this": False, 2207 "expression": False, 2208 "kind": True, 2209 } 2210 2211 2212class Union(Subqueryable): 2213 arg_types = { 2214 "with": False, 2215 "this": True, 2216 "expression": True, 2217 "distinct": False, 2218 **QUERY_MODIFIERS, 2219 } 2220 2221 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2222 """ 2223 Set the LIMIT expression. 2224 2225 Example: 2226 >>> select("1").union(select("1")).limit(1).sql() 2227 'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1' 2228 2229 Args: 2230 expression (str | int | Expression): the SQL code string to parse. 2231 This can also be an integer. 2232 If a `Limit` instance is passed, this is used as-is. 2233 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2234 dialect (str): the dialect used to parse the input expression. 2235 copy (bool): if `False`, modify this expression instance in-place. 2236 opts (kwargs): other options to use to parse the input expressions. 2237 2238 Returns: 2239 Select: The limited subqueryable. 2240 """ 2241 return ( 2242 select("*") 2243 .from_(self.subquery(alias="_l_0", copy=copy)) 2244 .limit(expression, dialect=dialect, copy=False, **opts) 2245 ) 2246 2247 def select( 2248 self, 2249 *expressions: ExpOrStr, 2250 append: bool = True, 2251 dialect: DialectType = None, 2252 copy: bool = True, 2253 **opts, 2254 ) -> Union: 2255 """Append to or set the SELECT of the union recursively. 2256 2257 Example: 2258 >>> from sqlglot import parse_one 2259 >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 2260 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z' 2261 2262 Args: 2263 *expressions: the SQL code strings to parse. 2264 If an `Expression` instance is passed, it will be used as-is. 2265 append: if `True`, add to any existing expressions. 2266 Otherwise, this resets the expressions. 2267 dialect: the dialect used to parse the input expressions. 2268 copy: if `False`, modify this expression instance in-place. 2269 opts: other options to use to parse the input expressions. 2270 2271 Returns: 2272 Union: the modified expression. 2273 """ 2274 this = self.copy() if copy else self 2275 this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts) 2276 this.expression.unnest().select( 2277 *expressions, append=append, dialect=dialect, copy=False, **opts 2278 ) 2279 return this 2280 2281 @property 2282 def named_selects(self): 2283 return self.this.unnest().named_selects 2284 2285 @property 2286 def is_star(self) -> bool: 2287 return self.this.is_star or self.expression.is_star 2288 2289 @property 2290 def selects(self): 2291 return self.this.unnest().selects 2292 2293 @property 2294 def left(self): 2295 return self.this 2296 2297 @property 2298 def right(self): 2299 return self.expression 2300 2301 2302class Except(Union): 2303 pass 2304 2305 2306class Intersect(Union): 2307 pass 2308 2309 2310class Unnest(UDTF): 2311 arg_types = { 2312 "expressions": True, 2313 "ordinality": False, 2314 "alias": False, 2315 "offset": False, 2316 } 2317 2318 2319class Update(Expression): 2320 arg_types = { 2321 "with": False, 2322 "this": False, 2323 "expressions": True, 2324 "from": False, 2325 "where": False, 2326 "returning": False, 2327 } 2328 2329 2330class Values(UDTF): 2331 arg_types = { 2332 "expressions": True, 2333 "ordinality": False, 2334 "alias": False, 2335 } 2336 2337 2338class Var(Expression): 2339 pass 2340 2341 2342class Schema(Expression): 2343 arg_types = {"this": False, "expressions": False} 2344 2345 2346# https://dev.mysql.com/doc/refman/8.0/en/select.html 2347# https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html 2348class Lock(Expression): 2349 arg_types = {"update": True, "expressions": False, "wait": False} 2350 2351 2352class Select(Subqueryable): 2353 arg_types = { 2354 "with": False, 2355 "kind": False, 2356 "expressions": False, 2357 "hint": False, 2358 "distinct": False, 2359 "struct": False, # https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#return_query_results_as_a_value_table 2360 "value": False, 2361 "into": False, 2362 "from": False, 2363 **QUERY_MODIFIERS, 2364 } 2365 2366 def from_( 2367 self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts 2368 ) -> Select: 2369 """ 2370 Set the FROM expression. 2371 2372 Example: 2373 >>> Select().from_("tbl").select("x").sql() 2374 'SELECT x FROM tbl' 2375 2376 Args: 2377 expression : the SQL code strings to parse. 2378 If a `From` instance is passed, this is used as-is. 2379 If another `Expression` instance is passed, it will be wrapped in a `From`. 2380 dialect: the dialect used to parse the input expression. 2381 copy: if `False`, modify this expression instance in-place. 2382 opts: other options to use to parse the input expressions. 2383 2384 Returns: 2385 Select: the modified expression. 2386 """ 2387 return _apply_builder( 2388 expression=expression, 2389 instance=self, 2390 arg="from", 2391 into=From, 2392 prefix="FROM", 2393 dialect=dialect, 2394 copy=copy, 2395 **opts, 2396 ) 2397 2398 def group_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2399 """ 2400 Set the GROUP BY expression. 2401 2402 Example: 2403 >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql() 2404 'SELECT x, COUNT(1) FROM tbl GROUP BY x' 2405 2406 Args: 2407 *expressions (str | Expression): the SQL code strings to parse. 2408 If a `Group` instance is passed, this is used as-is. 2409 If another `Expression` instance is passed, it will be wrapped in a `Group`. 2410 If nothing is passed in then a group by is not applied to the expression 2411 append (bool): if `True`, add to any existing expressions. 2412 Otherwise, this flattens all the `Group` expression into a single expression. 2413 dialect (str): the dialect used to parse the input expression. 2414 copy (bool): if `False`, modify this expression instance in-place. 2415 opts (kwargs): other options to use to parse the input expressions. 2416 2417 Returns: 2418 Select: the modified expression. 2419 """ 2420 if not expressions: 2421 return self if not copy else self.copy() 2422 return _apply_child_list_builder( 2423 *expressions, 2424 instance=self, 2425 arg="group", 2426 append=append, 2427 copy=copy, 2428 prefix="GROUP BY", 2429 into=Group, 2430 dialect=dialect, 2431 **opts, 2432 ) 2433 2434 def order_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2435 """ 2436 Set the ORDER BY expression. 2437 2438 Example: 2439 >>> Select().from_("tbl").select("x").order_by("x DESC").sql() 2440 'SELECT x FROM tbl ORDER BY x DESC' 2441 2442 Args: 2443 *expressions (str | Expression): the SQL code strings to parse. 2444 If a `Group` instance is passed, this is used as-is. 2445 If another `Expression` instance is passed, it will be wrapped in a `Order`. 2446 append (bool): if `True`, add to any existing expressions. 2447 Otherwise, this flattens all the `Order` expression into a single expression. 2448 dialect (str): the dialect used to parse the input expression. 2449 copy (bool): if `False`, modify this expression instance in-place. 2450 opts (kwargs): other options to use to parse the input expressions. 2451 2452 Returns: 2453 Select: the modified expression. 2454 """ 2455 return _apply_child_list_builder( 2456 *expressions, 2457 instance=self, 2458 arg="order", 2459 append=append, 2460 copy=copy, 2461 prefix="ORDER BY", 2462 into=Order, 2463 dialect=dialect, 2464 **opts, 2465 ) 2466 2467 def sort_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2468 """ 2469 Set the SORT BY expression. 2470 2471 Example: 2472 >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive") 2473 'SELECT x FROM tbl SORT BY x DESC' 2474 2475 Args: 2476 *expressions (str | Expression): the SQL code strings to parse. 2477 If a `Group` instance is passed, this is used as-is. 2478 If another `Expression` instance is passed, it will be wrapped in a `SORT`. 2479 append (bool): if `True`, add to any existing expressions. 2480 Otherwise, this flattens all the `Order` expression into a single expression. 2481 dialect (str): the dialect used to parse the input expression. 2482 copy (bool): if `False`, modify this expression instance in-place. 2483 opts (kwargs): other options to use to parse the input expressions. 2484 2485 Returns: 2486 Select: the modified expression. 2487 """ 2488 return _apply_child_list_builder( 2489 *expressions, 2490 instance=self, 2491 arg="sort", 2492 append=append, 2493 copy=copy, 2494 prefix="SORT BY", 2495 into=Sort, 2496 dialect=dialect, 2497 **opts, 2498 ) 2499 2500 def cluster_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2501 """ 2502 Set the CLUSTER BY expression. 2503 2504 Example: 2505 >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive") 2506 'SELECT x FROM tbl CLUSTER BY x DESC' 2507 2508 Args: 2509 *expressions (str | Expression): the SQL code strings to parse. 2510 If a `Group` instance is passed, this is used as-is. 2511 If another `Expression` instance is passed, it will be wrapped in a `Cluster`. 2512 append (bool): if `True`, add to any existing expressions. 2513 Otherwise, this flattens all the `Order` expression into a single expression. 2514 dialect (str): the dialect used to parse the input expression. 2515 copy (bool): if `False`, modify this expression instance in-place. 2516 opts (kwargs): other options to use to parse the input expressions. 2517 2518 Returns: 2519 Select: the modified expression. 2520 """ 2521 return _apply_child_list_builder( 2522 *expressions, 2523 instance=self, 2524 arg="cluster", 2525 append=append, 2526 copy=copy, 2527 prefix="CLUSTER BY", 2528 into=Cluster, 2529 dialect=dialect, 2530 **opts, 2531 ) 2532 2533 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2534 """ 2535 Set the LIMIT expression. 2536 2537 Example: 2538 >>> Select().from_("tbl").select("x").limit(10).sql() 2539 'SELECT x FROM tbl LIMIT 10' 2540 2541 Args: 2542 expression (str | int | Expression): the SQL code string to parse. 2543 This can also be an integer. 2544 If a `Limit` instance is passed, this is used as-is. 2545 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2546 dialect (str): the dialect used to parse the input expression. 2547 copy (bool): if `False`, modify this expression instance in-place. 2548 opts (kwargs): other options to use to parse the input expressions. 2549 2550 Returns: 2551 Select: the modified expression. 2552 """ 2553 return _apply_builder( 2554 expression=expression, 2555 instance=self, 2556 arg="limit", 2557 into=Limit, 2558 prefix="LIMIT", 2559 dialect=dialect, 2560 copy=copy, 2561 **opts, 2562 ) 2563 2564 def offset(self, expression, dialect=None, copy=True, **opts) -> Select: 2565 """ 2566 Set the OFFSET expression. 2567 2568 Example: 2569 >>> Select().from_("tbl").select("x").offset(10).sql() 2570 'SELECT x FROM tbl OFFSET 10' 2571 2572 Args: 2573 expression (str | int | Expression): the SQL code string to parse. 2574 This can also be an integer. 2575 If a `Offset` instance is passed, this is used as-is. 2576 If another `Expression` instance is passed, it will be wrapped in a `Offset`. 2577 dialect (str): the dialect used to parse the input expression. 2578 copy (bool): if `False`, modify this expression instance in-place. 2579 opts (kwargs): other options to use to parse the input expressions. 2580 2581 Returns: 2582 Select: the modified expression. 2583 """ 2584 return _apply_builder( 2585 expression=expression, 2586 instance=self, 2587 arg="offset", 2588 into=Offset, 2589 prefix="OFFSET", 2590 dialect=dialect, 2591 copy=copy, 2592 **opts, 2593 ) 2594 2595 def select( 2596 self, 2597 *expressions: ExpOrStr, 2598 append: bool = True, 2599 dialect: DialectType = None, 2600 copy: bool = True, 2601 **opts, 2602 ) -> Select: 2603 """ 2604 Append to or set the SELECT expressions. 2605 2606 Example: 2607 >>> Select().select("x", "y").sql() 2608 'SELECT x, y' 2609 2610 Args: 2611 *expressions: the SQL code strings to parse. 2612 If an `Expression` instance is passed, it will be used as-is. 2613 append: if `True`, add to any existing expressions. 2614 Otherwise, this resets the expressions. 2615 dialect: the dialect used to parse the input expressions. 2616 copy: if `False`, modify this expression instance in-place. 2617 opts: other options to use to parse the input expressions. 2618 2619 Returns: 2620 Select: the modified expression. 2621 """ 2622 return _apply_list_builder( 2623 *expressions, 2624 instance=self, 2625 arg="expressions", 2626 append=append, 2627 dialect=dialect, 2628 copy=copy, 2629 **opts, 2630 ) 2631 2632 def lateral(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2633 """ 2634 Append to or set the LATERAL expressions. 2635 2636 Example: 2637 >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql() 2638 'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z' 2639 2640 Args: 2641 *expressions (str | Expression): the SQL code strings to parse. 2642 If an `Expression` instance is passed, it will be used as-is. 2643 append (bool): if `True`, add to any existing expressions. 2644 Otherwise, this resets the expressions. 2645 dialect (str): the dialect used to parse the input expressions. 2646 copy (bool): if `False`, modify this expression instance in-place. 2647 opts (kwargs): other options to use to parse the input expressions. 2648 2649 Returns: 2650 Select: the modified expression. 2651 """ 2652 return _apply_list_builder( 2653 *expressions, 2654 instance=self, 2655 arg="laterals", 2656 append=append, 2657 into=Lateral, 2658 prefix="LATERAL VIEW", 2659 dialect=dialect, 2660 copy=copy, 2661 **opts, 2662 ) 2663 2664 def join( 2665 self, 2666 expression, 2667 on=None, 2668 using=None, 2669 append=True, 2670 join_type=None, 2671 join_alias=None, 2672 dialect=None, 2673 copy=True, 2674 **opts, 2675 ) -> Select: 2676 """ 2677 Append to or set the JOIN expressions. 2678 2679 Example: 2680 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 2681 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y' 2682 2683 >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 2684 'SELECT 1 FROM a JOIN b USING (x, y, z)' 2685 2686 Use `join_type` to change the type of join: 2687 2688 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 2689 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y' 2690 2691 Args: 2692 expression (str | Expression): the SQL code string to parse. 2693 If an `Expression` instance is passed, it will be used as-is. 2694 on (str | Expression): optionally specify the join "on" criteria as a SQL string. 2695 If an `Expression` instance is passed, it will be used as-is. 2696 using (str | Expression): optionally specify the join "using" criteria as a SQL string. 2697 If an `Expression` instance is passed, it will be used as-is. 2698 append (bool): if `True`, add to any existing expressions. 2699 Otherwise, this resets the expressions. 2700 join_type (str): If set, alter the parsed join type 2701 dialect (str): the dialect used to parse the input expressions. 2702 copy (bool): if `False`, modify this expression instance in-place. 2703 opts (kwargs): other options to use to parse the input expressions. 2704 2705 Returns: 2706 Select: the modified expression. 2707 """ 2708 parse_args = {"dialect": dialect, **opts} 2709 2710 try: 2711 expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args) 2712 except ParseError: 2713 expression = maybe_parse(expression, into=(Join, Expression), **parse_args) 2714 2715 join = expression if isinstance(expression, Join) else Join(this=expression) 2716 2717 if isinstance(join.this, Select): 2718 join.this.replace(join.this.subquery()) 2719 2720 if join_type: 2721 natural: t.Optional[Token] 2722 side: t.Optional[Token] 2723 kind: t.Optional[Token] 2724 2725 natural, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args) # type: ignore 2726 2727 if natural: 2728 join.set("natural", True) 2729 if side: 2730 join.set("side", side.text) 2731 if kind: 2732 join.set("kind", kind.text) 2733 2734 if on: 2735 on = and_(*ensure_collection(on), dialect=dialect, copy=copy, **opts) 2736 join.set("on", on) 2737 2738 if using: 2739 join = _apply_list_builder( 2740 *ensure_collection(using), 2741 instance=join, 2742 arg="using", 2743 append=append, 2744 copy=copy, 2745 **opts, 2746 ) 2747 2748 if join_alias: 2749 join.set("this", alias_(join.this, join_alias, table=True)) 2750 return _apply_list_builder( 2751 join, 2752 instance=self, 2753 arg="joins", 2754 append=append, 2755 copy=copy, 2756 **opts, 2757 ) 2758 2759 def where(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2760 """ 2761 Append to or set the WHERE expressions. 2762 2763 Example: 2764 >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql() 2765 "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'" 2766 2767 Args: 2768 *expressions (str | Expression): the SQL code strings to parse. 2769 If an `Expression` instance is passed, it will be used as-is. 2770 Multiple expressions are combined with an AND operator. 2771 append (bool): if `True`, AND the new expressions to any existing expression. 2772 Otherwise, this resets the expression. 2773 dialect (str): the dialect used to parse the input expressions. 2774 copy (bool): if `False`, modify this expression instance in-place. 2775 opts (kwargs): other options to use to parse the input expressions. 2776 2777 Returns: 2778 Select: the modified expression. 2779 """ 2780 return _apply_conjunction_builder( 2781 *expressions, 2782 instance=self, 2783 arg="where", 2784 append=append, 2785 into=Where, 2786 dialect=dialect, 2787 copy=copy, 2788 **opts, 2789 ) 2790 2791 def having(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2792 """ 2793 Append to or set the HAVING expressions. 2794 2795 Example: 2796 >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql() 2797 'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3' 2798 2799 Args: 2800 *expressions (str | Expression): the SQL code strings to parse. 2801 If an `Expression` instance is passed, it will be used as-is. 2802 Multiple expressions are combined with an AND operator. 2803 append (bool): if `True`, AND the new expressions to any existing expression. 2804 Otherwise, this resets the expression. 2805 dialect (str): the dialect used to parse the input expressions. 2806 copy (bool): if `False`, modify this expression instance in-place. 2807 opts (kwargs): other options to use to parse the input expressions. 2808 2809 Returns: 2810 Select: the modified expression. 2811 """ 2812 return _apply_conjunction_builder( 2813 *expressions, 2814 instance=self, 2815 arg="having", 2816 append=append, 2817 into=Having, 2818 dialect=dialect, 2819 copy=copy, 2820 **opts, 2821 ) 2822 2823 def window(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2824 return _apply_list_builder( 2825 *expressions, 2826 instance=self, 2827 arg="windows", 2828 append=append, 2829 into=Window, 2830 dialect=dialect, 2831 copy=copy, 2832 **opts, 2833 ) 2834 2835 def qualify(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2836 return _apply_conjunction_builder( 2837 *expressions, 2838 instance=self, 2839 arg="qualify", 2840 append=append, 2841 into=Qualify, 2842 dialect=dialect, 2843 copy=copy, 2844 **opts, 2845 ) 2846 2847 def distinct(self, *ons: ExpOrStr, distinct: bool = True, copy: bool = True) -> Select: 2848 """ 2849 Set the OFFSET expression. 2850 2851 Example: 2852 >>> Select().from_("tbl").select("x").distinct().sql() 2853 'SELECT DISTINCT x FROM tbl' 2854 2855 Args: 2856 ons: the expressions to distinct on 2857 distinct: whether the Select should be distinct 2858 copy: if `False`, modify this expression instance in-place. 2859 2860 Returns: 2861 Select: the modified expression. 2862 """ 2863 instance = _maybe_copy(self, copy) 2864 on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons]) if ons else None 2865 instance.set("distinct", Distinct(on=on) if distinct else None) 2866 return instance 2867 2868 def ctas(self, table, properties=None, dialect=None, copy=True, **opts) -> Create: 2869 """ 2870 Convert this expression to a CREATE TABLE AS statement. 2871 2872 Example: 2873 >>> Select().select("*").from_("tbl").ctas("x").sql() 2874 'CREATE TABLE x AS SELECT * FROM tbl' 2875 2876 Args: 2877 table (str | Expression): the SQL code string to parse as the table name. 2878 If another `Expression` instance is passed, it will be used as-is. 2879 properties (dict): an optional mapping of table properties 2880 dialect (str): the dialect used to parse the input table. 2881 copy (bool): if `False`, modify this expression instance in-place. 2882 opts (kwargs): other options to use to parse the input table. 2883 2884 Returns: 2885 Create: the CREATE TABLE AS expression 2886 """ 2887 instance = _maybe_copy(self, copy) 2888 table_expression = maybe_parse( 2889 table, 2890 into=Table, 2891 dialect=dialect, 2892 **opts, 2893 ) 2894 properties_expression = None 2895 if properties: 2896 properties_expression = Properties.from_dict(properties) 2897 2898 return Create( 2899 this=table_expression, 2900 kind="table", 2901 expression=instance, 2902 properties=properties_expression, 2903 ) 2904 2905 def lock(self, update: bool = True, copy: bool = True) -> Select: 2906 """ 2907 Set the locking read mode for this expression. 2908 2909 Examples: 2910 >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql") 2911 "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE" 2912 2913 >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql") 2914 "SELECT x FROM tbl WHERE x = 'a' FOR SHARE" 2915 2916 Args: 2917 update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`. 2918 copy: if `False`, modify this expression instance in-place. 2919 2920 Returns: 2921 The modified expression. 2922 """ 2923 2924 inst = _maybe_copy(self, copy) 2925 inst.set("locks", [Lock(update=update)]) 2926 2927 return inst 2928 2929 @property 2930 def named_selects(self) -> t.List[str]: 2931 return [e.output_name for e in self.expressions if e.alias_or_name] 2932 2933 @property 2934 def is_star(self) -> bool: 2935 return any(expression.is_star for expression in self.expressions) 2936 2937 @property 2938 def selects(self) -> t.List[Expression]: 2939 return self.expressions 2940 2941 2942class Subquery(DerivedTable, Unionable): 2943 arg_types = { 2944 "this": True, 2945 "alias": False, 2946 "with": False, 2947 **QUERY_MODIFIERS, 2948 } 2949 2950 def unnest(self): 2951 """ 2952 Returns the first non subquery. 2953 """ 2954 expression = self 2955 while isinstance(expression, Subquery): 2956 expression = expression.this 2957 return expression 2958 2959 @property 2960 def is_star(self) -> bool: 2961 return self.this.is_star 2962 2963 @property 2964 def output_name(self): 2965 return self.alias 2966 2967 2968class TableSample(Expression): 2969 arg_types = { 2970 "this": False, 2971 "method": False, 2972 "bucket_numerator": False, 2973 "bucket_denominator": False, 2974 "bucket_field": False, 2975 "percent": False, 2976 "rows": False, 2977 "size": False, 2978 "seed": False, 2979 "kind": False, 2980 } 2981 2982 2983class Tag(Expression): 2984 """Tags are used for generating arbitrary sql like SELECT <span>x</span>.""" 2985 2986 arg_types = { 2987 "this": False, 2988 "prefix": False, 2989 "postfix": False, 2990 } 2991 2992 2993class Pivot(Expression): 2994 arg_types = { 2995 "alias": False, 2996 "expressions": True, 2997 "field": True, 2998 "unpivot": True, 2999 "columns": False, 3000 } 3001 3002 3003class Window(Expression): 3004 arg_types = { 3005 "this": True, 3006 "partition_by": False, 3007 "order": False, 3008 "spec": False, 3009 "alias": False, 3010 "over": False, 3011 "first": False, 3012 } 3013 3014 3015class WindowSpec(Expression): 3016 arg_types = { 3017 "kind": False, 3018 "start": False, 3019 "start_side": False, 3020 "end": False, 3021 "end_side": False, 3022 } 3023 3024 3025class Where(Expression): 3026 pass 3027 3028 3029class Star(Expression): 3030 arg_types = {"except": False, "replace": False} 3031 3032 @property 3033 def name(self) -> str: 3034 return "*" 3035 3036 @property 3037 def output_name(self): 3038 return self.name 3039 3040 3041class Parameter(Expression): 3042 arg_types = {"this": True, "wrapped": False} 3043 3044 3045class SessionParameter(Expression): 3046 arg_types = {"this": True, "kind": False} 3047 3048 3049class Placeholder(Expression): 3050 arg_types = {"this": False, "kind": False} 3051 3052 3053class Null(Condition): 3054 arg_types: t.Dict[str, t.Any] = {} 3055 3056 @property 3057 def name(self) -> str: 3058 return "NULL" 3059 3060 3061class Boolean(Condition): 3062 pass 3063 3064 3065class DataTypeSize(Expression): 3066 arg_types = {"this": True, "expression": False} 3067 3068 3069class DataType(Expression): 3070 arg_types = { 3071 "this": True, 3072 "expressions": False, 3073 "nested": False, 3074 "values": False, 3075 "prefix": False, 3076 } 3077 3078 class Type(AutoName): 3079 ARRAY = auto() 3080 BIGDECIMAL = auto() 3081 BIGINT = auto() 3082 BIGSERIAL = auto() 3083 BINARY = auto() 3084 BIT = auto() 3085 BOOLEAN = auto() 3086 CHAR = auto() 3087 DATE = auto() 3088 DATETIME = auto() 3089 DATETIME64 = auto() 3090 DECIMAL = auto() 3091 DOUBLE = auto() 3092 FLOAT = auto() 3093 GEOGRAPHY = auto() 3094 GEOMETRY = auto() 3095 HLLSKETCH = auto() 3096 HSTORE = auto() 3097 IMAGE = auto() 3098 INET = auto() 3099 INT = auto() 3100 INT128 = auto() 3101 INT256 = auto() 3102 INTERVAL = auto() 3103 JSON = auto() 3104 JSONB = auto() 3105 LONGBLOB = auto() 3106 LONGTEXT = auto() 3107 MAP = auto() 3108 MEDIUMBLOB = auto() 3109 MEDIUMTEXT = auto() 3110 MONEY = auto() 3111 NCHAR = auto() 3112 NULL = auto() 3113 NULLABLE = auto() 3114 NVARCHAR = auto() 3115 OBJECT = auto() 3116 ROWVERSION = auto() 3117 SERIAL = auto() 3118 SMALLINT = auto() 3119 SMALLMONEY = auto() 3120 SMALLSERIAL = auto() 3121 STRUCT = auto() 3122 SUPER = auto() 3123 TEXT = auto() 3124 TIME = auto() 3125 TIMESTAMP = auto() 3126 TIMESTAMPTZ = auto() 3127 TIMESTAMPLTZ = auto() 3128 TINYINT = auto() 3129 UBIGINT = auto() 3130 UINT = auto() 3131 USMALLINT = auto() 3132 UTINYINT = auto() 3133 UNKNOWN = auto() # Sentinel value, useful for type annotation 3134 UINT128 = auto() 3135 UINT256 = auto() 3136 UNIQUEIDENTIFIER = auto() 3137 UUID = auto() 3138 VARBINARY = auto() 3139 VARCHAR = auto() 3140 VARIANT = auto() 3141 XML = auto() 3142 3143 TEXT_TYPES = { 3144 Type.CHAR, 3145 Type.NCHAR, 3146 Type.VARCHAR, 3147 Type.NVARCHAR, 3148 Type.TEXT, 3149 } 3150 3151 INTEGER_TYPES = { 3152 Type.INT, 3153 Type.TINYINT, 3154 Type.SMALLINT, 3155 Type.BIGINT, 3156 Type.INT128, 3157 Type.INT256, 3158 } 3159 3160 FLOAT_TYPES = { 3161 Type.FLOAT, 3162 Type.DOUBLE, 3163 } 3164 3165 NUMERIC_TYPES = {*INTEGER_TYPES, *FLOAT_TYPES} 3166 3167 TEMPORAL_TYPES = { 3168 Type.TIMESTAMP, 3169 Type.TIMESTAMPTZ, 3170 Type.TIMESTAMPLTZ, 3171 Type.DATE, 3172 Type.DATETIME, 3173 Type.DATETIME64, 3174 } 3175 3176 @classmethod 3177 def build( 3178 cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs 3179 ) -> DataType: 3180 from sqlglot import parse_one 3181 3182 if isinstance(dtype, str): 3183 if dtype.upper() in cls.Type.__members__: 3184 data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[dtype.upper()]) 3185 else: 3186 data_type_exp = parse_one(dtype, read=dialect, into=DataType) 3187 if data_type_exp is None: 3188 raise ValueError(f"Unparsable data type value: {dtype}") 3189 elif isinstance(dtype, DataType.Type): 3190 data_type_exp = DataType(this=dtype) 3191 elif isinstance(dtype, DataType): 3192 return dtype 3193 else: 3194 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type") 3195 return DataType(**{**data_type_exp.args, **kwargs}) 3196 3197 def is_type(self, dtype: DataType.Type) -> bool: 3198 return self.this == dtype 3199 3200 3201# https://www.postgresql.org/docs/15/datatype-pseudo.html 3202class PseudoType(Expression): 3203 pass 3204 3205 3206# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...) 3207class SubqueryPredicate(Predicate): 3208 pass 3209 3210 3211class All(SubqueryPredicate): 3212 pass 3213 3214 3215class Any(SubqueryPredicate): 3216 pass 3217 3218 3219class Exists(SubqueryPredicate): 3220 pass 3221 3222 3223# Commands to interact with the databases or engines. For most of the command 3224# expressions we parse whatever comes after the command's name as a string. 3225class Command(Expression): 3226 arg_types = {"this": True, "expression": False} 3227 3228 3229class Transaction(Expression): 3230 arg_types = {"this": False, "modes": False} 3231 3232 3233class Commit(Expression): 3234 arg_types = {"chain": False} 3235 3236 3237class Rollback(Expression): 3238 arg_types = {"savepoint": False} 3239 3240 3241class AlterTable(Expression): 3242 arg_types = {"this": True, "actions": True, "exists": False} 3243 3244 3245class AddConstraint(Expression): 3246 arg_types = {"this": False, "expression": False, "enforced": False} 3247 3248 3249class DropPartition(Expression): 3250 arg_types = {"expressions": True, "exists": False} 3251 3252 3253# Binary expressions like (ADD a b) 3254class Binary(Condition): 3255 arg_types = {"this": True, "expression": True} 3256 3257 @property 3258 def left(self): 3259 return self.this 3260 3261 @property 3262 def right(self): 3263 return self.expression 3264 3265 3266class Add(Binary): 3267 pass 3268 3269 3270class Connector(Binary): 3271 pass 3272 3273 3274class And(Connector): 3275 pass 3276 3277 3278class Or(Connector): 3279 pass 3280 3281 3282class BitwiseAnd(Binary): 3283 pass 3284 3285 3286class BitwiseLeftShift(Binary): 3287 pass 3288 3289 3290class BitwiseOr(Binary): 3291 pass 3292 3293 3294class BitwiseRightShift(Binary): 3295 pass 3296 3297 3298class BitwiseXor(Binary): 3299 pass 3300 3301 3302class Div(Binary): 3303 pass 3304 3305 3306class Overlaps(Binary): 3307 pass 3308 3309 3310class Dot(Binary): 3311 @property 3312 def name(self) -> str: 3313 return self.expression.name 3314 3315 @classmethod 3316 def build(self, expressions: t.Sequence[Expression]) -> Dot: 3317 """Build a Dot object with a sequence of expressions.""" 3318 if len(expressions) < 2: 3319 raise ValueError(f"Dot requires >= 2 expressions.") 3320 3321 a, b, *expressions = expressions 3322 dot = Dot(this=a, expression=b) 3323 3324 for expression in expressions: 3325 dot = Dot(this=dot, expression=expression) 3326 3327 return dot 3328 3329 3330class DPipe(Binary): 3331 pass 3332 3333 3334class EQ(Binary, Predicate): 3335 pass 3336 3337 3338class NullSafeEQ(Binary, Predicate): 3339 pass 3340 3341 3342class NullSafeNEQ(Binary, Predicate): 3343 pass 3344 3345 3346class Distance(Binary): 3347 pass 3348 3349 3350class Escape(Binary): 3351 pass 3352 3353 3354class Glob(Binary, Predicate): 3355 pass 3356 3357 3358class GT(Binary, Predicate): 3359 pass 3360 3361 3362class GTE(Binary, Predicate): 3363 pass 3364 3365 3366class ILike(Binary, Predicate): 3367 pass 3368 3369 3370class ILikeAny(Binary, Predicate): 3371 pass 3372 3373 3374class IntDiv(Binary): 3375 pass 3376 3377 3378class Is(Binary, Predicate): 3379 pass 3380 3381 3382class Kwarg(Binary): 3383 """Kwarg in special functions like func(kwarg => y).""" 3384 3385 3386class Like(Binary, Predicate): 3387 pass 3388 3389 3390class LikeAny(Binary, Predicate): 3391 pass 3392 3393 3394class LT(Binary, Predicate): 3395 pass 3396 3397 3398class LTE(Binary, Predicate): 3399 pass 3400 3401 3402class Mod(Binary): 3403 pass 3404 3405 3406class Mul(Binary): 3407 pass 3408 3409 3410class NEQ(Binary, Predicate): 3411 pass 3412 3413 3414class SimilarTo(Binary, Predicate): 3415 pass 3416 3417 3418class Slice(Binary): 3419 arg_types = {"this": False, "expression": False} 3420 3421 3422class Sub(Binary): 3423 pass 3424 3425 3426class ArrayOverlaps(Binary): 3427 pass 3428 3429 3430# Unary Expressions 3431# (NOT a) 3432class Unary(Condition): 3433 pass 3434 3435 3436class BitwiseNot(Unary): 3437 pass 3438 3439 3440class Not(Unary): 3441 pass 3442 3443 3444class Paren(Unary): 3445 arg_types = {"this": True, "with": False} 3446 3447 3448class Neg(Unary): 3449 pass 3450 3451 3452class Alias(Expression): 3453 arg_types = {"this": True, "alias": False} 3454 3455 @property 3456 def output_name(self): 3457 return self.alias 3458 3459 3460class Aliases(Expression): 3461 arg_types = {"this": True, "expressions": True} 3462 3463 @property 3464 def aliases(self): 3465 return self.expressions 3466 3467 3468class AtTimeZone(Expression): 3469 arg_types = {"this": True, "zone": True} 3470 3471 3472class Between(Predicate): 3473 arg_types = {"this": True, "low": True, "high": True} 3474 3475 3476class Bracket(Condition): 3477 arg_types = {"this": True, "expressions": True} 3478 3479 3480class Distinct(Expression): 3481 arg_types = {"expressions": False, "on": False} 3482 3483 3484class In(Predicate): 3485 arg_types = { 3486 "this": True, 3487 "expressions": False, 3488 "query": False, 3489 "unnest": False, 3490 "field": False, 3491 "is_global": False, 3492 } 3493 3494 3495class TimeUnit(Expression): 3496 """Automatically converts unit arg into a var.""" 3497 3498 arg_types = {"unit": False} 3499 3500 def __init__(self, **args): 3501 unit = args.get("unit") 3502 if isinstance(unit, (Column, Literal)): 3503 args["unit"] = Var(this=unit.name) 3504 elif isinstance(unit, Week): 3505 unit.set("this", Var(this=unit.this.name)) 3506 super().__init__(**args) 3507 3508 3509class Interval(TimeUnit): 3510 arg_types = {"this": False, "unit": False} 3511 3512 @property 3513 def unit(self) -> t.Optional[Var]: 3514 return self.args.get("unit") 3515 3516 3517class IgnoreNulls(Expression): 3518 pass 3519 3520 3521class RespectNulls(Expression): 3522 pass 3523 3524 3525# Functions 3526class Func(Condition): 3527 """ 3528 The base class for all function expressions. 3529 3530 Attributes: 3531 is_var_len_args (bool): if set to True the last argument defined in arg_types will be 3532 treated as a variable length argument and the argument's value will be stored as a list. 3533 _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items) 3534 for this function expression. These values are used to map this node to a name during parsing 3535 as well as to provide the function's name during SQL string generation. By default the SQL 3536 name is set to the expression's class name transformed to snake case. 3537 """ 3538 3539 is_var_len_args = False 3540 3541 @classmethod 3542 def from_arg_list(cls, args): 3543 if cls.is_var_len_args: 3544 all_arg_keys = list(cls.arg_types) 3545 # If this function supports variable length argument treat the last argument as such. 3546 non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys 3547 num_non_var = len(non_var_len_arg_keys) 3548 3549 args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)} 3550 args_dict[all_arg_keys[-1]] = args[num_non_var:] 3551 else: 3552 args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)} 3553 3554 return cls(**args_dict) 3555 3556 @classmethod 3557 def sql_names(cls): 3558 if cls is Func: 3559 raise NotImplementedError( 3560 "SQL name is only supported by concrete function implementations" 3561 ) 3562 if "_sql_names" not in cls.__dict__: 3563 cls._sql_names = [camel_to_snake_case(cls.__name__)] 3564 return cls._sql_names 3565 3566 @classmethod 3567 def sql_name(cls): 3568 return cls.sql_names()[0] 3569 3570 @classmethod 3571 def default_parser_mappings(cls): 3572 return {name: cls.from_arg_list for name in cls.sql_names()} 3573 3574 3575class AggFunc(Func): 3576 pass 3577 3578 3579class ParameterizedAgg(AggFunc): 3580 arg_types = {"this": True, "expressions": True, "params": True} 3581 3582 3583class Abs(Func): 3584 pass 3585 3586 3587class Anonymous(Func): 3588 arg_types = {"this": True, "expressions": False} 3589 is_var_len_args = True 3590 3591 3592# https://docs.snowflake.com/en/sql-reference/functions/hll 3593# https://docs.aws.amazon.com/redshift/latest/dg/r_HLL_function.html 3594class Hll(AggFunc): 3595 arg_types = {"this": True, "expressions": False} 3596 is_var_len_args = True 3597 3598 3599class ApproxDistinct(AggFunc): 3600 arg_types = {"this": True, "accuracy": False} 3601 _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"] 3602 3603 3604class Array(Func): 3605 arg_types = {"expressions": False} 3606 is_var_len_args = True 3607 3608 3609# https://docs.snowflake.com/en/sql-reference/functions/to_char 3610class ToChar(Func): 3611 arg_types = {"this": True, "format": False} 3612 3613 3614class GenerateSeries(Func): 3615 arg_types = {"start": True, "end": True, "step": False} 3616 3617 3618class ArrayAgg(AggFunc): 3619 pass 3620 3621 3622class ArrayAll(Func): 3623 arg_types = {"this": True, "expression": True} 3624 3625 3626class ArrayAny(Func): 3627 arg_types = {"this": True, "expression": True} 3628 3629 3630class ArrayConcat(Func): 3631 arg_types = {"this": True, "expressions": False} 3632 is_var_len_args = True 3633 3634 3635class ArrayContains(Binary, Func): 3636 pass 3637 3638 3639class ArrayContained(Binary): 3640 pass 3641 3642 3643class ArrayFilter(Func): 3644 arg_types = {"this": True, "expression": True} 3645 _sql_names = ["FILTER", "ARRAY_FILTER"] 3646 3647 3648class ArrayJoin(Func): 3649 arg_types = {"this": True, "expression": True, "null": False} 3650 3651 3652class ArraySize(Func): 3653 arg_types = {"this": True, "expression": False} 3654 3655 3656class ArraySort(Func): 3657 arg_types = {"this": True, "expression": False} 3658 3659 3660class ArraySum(Func): 3661 pass 3662 3663 3664class ArrayUnionAgg(AggFunc): 3665 pass 3666 3667 3668class Avg(AggFunc): 3669 pass 3670 3671 3672class AnyValue(AggFunc): 3673 pass 3674 3675 3676class Case(Func): 3677 arg_types = {"this": False, "ifs": True, "default": False} 3678 3679 def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case: 3680 instance = _maybe_copy(self, copy) 3681 instance.append( 3682 "ifs", 3683 If( 3684 this=maybe_parse(condition, copy=copy, **opts), 3685 true=maybe_parse(then, copy=copy, **opts), 3686 ), 3687 ) 3688 return instance 3689 3690 def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case: 3691 instance = _maybe_copy(self, copy) 3692 instance.set("default", maybe_parse(condition, copy=copy, **opts)) 3693 return instance 3694 3695 3696class Cast(Func): 3697 arg_types = {"this": True, "to": True} 3698 3699 @property 3700 def name(self) -> str: 3701 return self.this.name 3702 3703 @property 3704 def to(self): 3705 return self.args["to"] 3706 3707 @property 3708 def output_name(self): 3709 return self.name 3710 3711 def is_type(self, dtype: DataType.Type) -> bool: 3712 return self.to.is_type(dtype) 3713 3714 3715class CastToStrType(Func): 3716 arg_types = {"this": True, "expression": True} 3717 3718 3719class Collate(Binary): 3720 pass 3721 3722 3723class TryCast(Cast): 3724 pass 3725 3726 3727class Ceil(Func): 3728 arg_types = {"this": True, "decimals": False} 3729 _sql_names = ["CEIL", "CEILING"] 3730 3731 3732class Coalesce(Func): 3733 arg_types = {"this": True, "expressions": False} 3734 is_var_len_args = True 3735 3736 3737class Concat(Func): 3738 arg_types = {"expressions": True} 3739 is_var_len_args = True 3740 3741 3742class ConcatWs(Concat): 3743 _sql_names = ["CONCAT_WS"] 3744 3745 3746class Count(AggFunc): 3747 arg_types = {"this": False} 3748 3749 3750class CountIf(AggFunc): 3751 pass 3752 3753 3754class CurrentDate(Func): 3755 arg_types = {"this": False} 3756 3757 3758class CurrentDatetime(Func): 3759 arg_types = {"this": False} 3760 3761 3762class CurrentTime(Func): 3763 arg_types = {"this": False} 3764 3765 3766class CurrentTimestamp(Func): 3767 arg_types = {"this": False} 3768 3769 3770class CurrentUser(Func): 3771 arg_types = {"this": False} 3772 3773 3774class DateAdd(Func, TimeUnit): 3775 arg_types = {"this": True, "expression": True, "unit": False} 3776 3777 3778class DateSub(Func, TimeUnit): 3779 arg_types = {"this": True, "expression": True, "unit": False} 3780 3781 3782class DateDiff(Func, TimeUnit): 3783 _sql_names = ["DATEDIFF", "DATE_DIFF"] 3784 arg_types = {"this": True, "expression": True, "unit": False} 3785 3786 3787class DateTrunc(Func): 3788 arg_types = {"unit": True, "this": True, "zone": False} 3789 3790 3791class DatetimeAdd(Func, TimeUnit): 3792 arg_types = {"this": True, "expression": True, "unit": False} 3793 3794 3795class DatetimeSub(Func, TimeUnit): 3796 arg_types = {"this": True, "expression": True, "unit": False} 3797 3798 3799class DatetimeDiff(Func, TimeUnit): 3800 arg_types = {"this": True, "expression": True, "unit": False} 3801 3802 3803class DatetimeTrunc(Func, TimeUnit): 3804 arg_types = {"this": True, "unit": True, "zone": False} 3805 3806 3807class DayOfWeek(Func): 3808 _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"] 3809 3810 3811class DayOfMonth(Func): 3812 _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"] 3813 3814 3815class DayOfYear(Func): 3816 _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"] 3817 3818 3819class WeekOfYear(Func): 3820 _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"] 3821 3822 3823class LastDateOfMonth(Func): 3824 pass 3825 3826 3827class Extract(Func): 3828 arg_types = {"this": True, "expression": True} 3829 3830 3831class TimestampAdd(Func, TimeUnit): 3832 arg_types = {"this": True, "expression": True, "unit": False} 3833 3834 3835class TimestampSub(Func, TimeUnit): 3836 arg_types = {"this": True, "expression": True, "unit": False} 3837 3838 3839class TimestampDiff(Func, TimeUnit): 3840 arg_types = {"this": True, "expression": True, "unit": False} 3841 3842 3843class TimestampTrunc(Func, TimeUnit): 3844 arg_types = {"this": True, "unit": True, "zone": False} 3845 3846 3847class TimeAdd(Func, TimeUnit): 3848 arg_types = {"this": True, "expression": True, "unit": False} 3849 3850 3851class TimeSub(Func, TimeUnit): 3852 arg_types = {"this": True, "expression": True, "unit": False} 3853 3854 3855class TimeDiff(Func, TimeUnit): 3856 arg_types = {"this": True, "expression": True, "unit": False} 3857 3858 3859class TimeTrunc(Func, TimeUnit): 3860 arg_types = {"this": True, "unit": True, "zone": False} 3861 3862 3863class DateFromParts(Func): 3864 _sql_names = ["DATEFROMPARTS"] 3865 arg_types = {"year": True, "month": True, "day": True} 3866 3867 3868class DateStrToDate(Func): 3869 pass 3870 3871 3872class DateToDateStr(Func): 3873 pass 3874 3875 3876class DateToDi(Func): 3877 pass 3878 3879 3880class Day(Func): 3881 pass 3882 3883 3884class Decode(Func): 3885 arg_types = {"this": True, "charset": True, "replace": False} 3886 3887 3888class DiToDate(Func): 3889 pass 3890 3891 3892class Encode(Func): 3893 arg_types = {"this": True, "charset": True} 3894 3895 3896class Exp(Func): 3897 pass 3898 3899 3900class Explode(Func): 3901 pass 3902 3903 3904class Floor(Func): 3905 arg_types = {"this": True, "decimals": False} 3906 3907 3908class FromBase64(Func): 3909 pass 3910 3911 3912class ToBase64(Func): 3913 pass 3914 3915 3916class Greatest(Func): 3917 arg_types = {"this": True, "expressions": False} 3918 is_var_len_args = True 3919 3920 3921class GroupConcat(Func): 3922 arg_types = {"this": True, "separator": False} 3923 3924 3925class Hex(Func): 3926 pass 3927 3928 3929class If(Func): 3930 arg_types = {"this": True, "true": True, "false": False} 3931 3932 3933class IfNull(Func): 3934 arg_types = {"this": True, "expression": False} 3935 _sql_names = ["IFNULL", "NVL"] 3936 3937 3938class Initcap(Func): 3939 pass 3940 3941 3942class JSONKeyValue(Expression): 3943 arg_types = {"this": True, "expression": True} 3944 3945 3946class JSONObject(Func): 3947 arg_types = { 3948 "expressions": False, 3949 "null_handling": False, 3950 "unique_keys": False, 3951 "return_type": False, 3952 "format_json": False, 3953 "encoding": False, 3954 } 3955 3956 3957class OpenJSONColumnDef(Expression): 3958 arg_types = {"this": True, "kind": True, "path": False, "as_json": False} 3959 3960 3961class OpenJSON(Func): 3962 arg_types = {"this": True, "path": False, "expressions": False} 3963 3964 3965class JSONBContains(Binary): 3966 _sql_names = ["JSONB_CONTAINS"] 3967 3968 3969class JSONExtract(Binary, Func): 3970 _sql_names = ["JSON_EXTRACT"] 3971 3972 3973class JSONExtractScalar(JSONExtract): 3974 _sql_names = ["JSON_EXTRACT_SCALAR"] 3975 3976 3977class JSONBExtract(JSONExtract): 3978 _sql_names = ["JSONB_EXTRACT"] 3979 3980 3981class JSONBExtractScalar(JSONExtract): 3982 _sql_names = ["JSONB_EXTRACT_SCALAR"] 3983 3984 3985class JSONFormat(Func): 3986 arg_types = {"this": False, "options": False} 3987 _sql_names = ["JSON_FORMAT"] 3988 3989 3990class Least(Func): 3991 arg_types = {"expressions": False} 3992 is_var_len_args = True 3993 3994 3995class Length(Func): 3996 pass 3997 3998 3999class Levenshtein(Func): 4000 arg_types = { 4001 "this": True, 4002 "expression": False, 4003 "ins_cost": False, 4004 "del_cost": False, 4005 "sub_cost": False, 4006 } 4007 4008 4009class Ln(Func): 4010 pass 4011 4012 4013class Log(Func): 4014 arg_types = {"this": True, "expression": False} 4015 4016 4017class Log2(Func): 4018 pass 4019 4020 4021class Log10(Func): 4022 pass 4023 4024 4025class LogicalOr(AggFunc): 4026 _sql_names = ["LOGICAL_OR", "BOOL_OR", "BOOLOR_AGG"] 4027 4028 4029class LogicalAnd(AggFunc): 4030 _sql_names = ["LOGICAL_AND", "BOOL_AND", "BOOLAND_AGG"] 4031 4032 4033class Lower(Func): 4034 _sql_names = ["LOWER", "LCASE"] 4035 4036 4037class Map(Func): 4038 arg_types = {"keys": False, "values": False} 4039 4040 4041class StarMap(Func): 4042 pass 4043 4044 4045class VarMap(Func): 4046 arg_types = {"keys": True, "values": True} 4047 is_var_len_args = True 4048 4049 4050# https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html 4051class MatchAgainst(Func): 4052 arg_types = {"this": True, "expressions": True, "modifier": False} 4053 4054 4055class Max(AggFunc): 4056 arg_types = {"this": True, "expressions": False} 4057 is_var_len_args = True 4058 4059 4060class MD5(Func): 4061 _sql_names = ["MD5"] 4062 4063 4064class Min(AggFunc): 4065 arg_types = {"this": True, "expressions": False} 4066 is_var_len_args = True 4067 4068 4069class Month(Func): 4070 pass 4071 4072 4073class Nvl2(Func): 4074 arg_types = {"this": True, "true": True, "false": False} 4075 4076 4077class Posexplode(Func): 4078 pass 4079 4080 4081class Pow(Binary, Func): 4082 _sql_names = ["POWER", "POW"] 4083 4084 4085class PercentileCont(AggFunc): 4086 arg_types = {"this": True, "expression": False} 4087 4088 4089class PercentileDisc(AggFunc): 4090 arg_types = {"this": True, "expression": False} 4091 4092 4093class Quantile(AggFunc): 4094 arg_types = {"this": True, "quantile": True} 4095 4096 4097class ApproxQuantile(Quantile): 4098 arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False} 4099 4100 4101class RangeN(Func): 4102 arg_types = {"this": True, "expressions": True, "each": False} 4103 4104 4105class ReadCSV(Func): 4106 _sql_names = ["READ_CSV"] 4107 is_var_len_args = True 4108 arg_types = {"this": True, "expressions": False} 4109 4110 4111class Reduce(Func): 4112 arg_types = {"this": True, "initial": True, "merge": True, "finish": False} 4113 4114 4115class RegexpExtract(Func): 4116 arg_types = { 4117 "this": True, 4118 "expression": True, 4119 "position": False, 4120 "occurrence": False, 4121 "group": False, 4122 } 4123 4124 4125class RegexpLike(Func): 4126 arg_types = {"this": True, "expression": True, "flag": False} 4127 4128 4129class RegexpILike(Func): 4130 arg_types = {"this": True, "expression": True, "flag": False} 4131 4132 4133# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split.html 4134# limit is the number of times a pattern is applied 4135class RegexpSplit(Func): 4136 arg_types = {"this": True, "expression": True, "limit": False} 4137 4138 4139class Repeat(Func): 4140 arg_types = {"this": True, "times": True} 4141 4142 4143class Round(Func): 4144 arg_types = {"this": True, "decimals": False} 4145 4146 4147class RowNumber(Func): 4148 arg_types: t.Dict[str, t.Any] = {} 4149 4150 4151class SafeDivide(Func): 4152 arg_types = {"this": True, "expression": True} 4153 4154 4155class SetAgg(AggFunc): 4156 pass 4157 4158 4159class SHA(Func): 4160 _sql_names = ["SHA", "SHA1"] 4161 4162 4163class SHA2(Func): 4164 _sql_names = ["SHA2"] 4165 arg_types = {"this": True, "length": False} 4166 4167 4168class SortArray(Func): 4169 arg_types = {"this": True, "asc": False} 4170 4171 4172class Split(Func): 4173 arg_types = {"this": True, "expression": True, "limit": False} 4174 4175 4176# Start may be omitted in the case of postgres 4177# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6 4178class Substring(Func): 4179 arg_types = {"this": True, "start": False, "length": False} 4180 4181 4182class StandardHash(Func): 4183 arg_types = {"this": True, "expression": False} 4184 4185 4186class StrPosition(Func): 4187 arg_types = { 4188 "this": True, 4189 "substr": True, 4190 "position": False, 4191 "instance": False, 4192 } 4193 4194 4195class StrToDate(Func): 4196 arg_types = {"this": True, "format": True} 4197 4198 4199class StrToTime(Func): 4200 arg_types = {"this": True, "format": True} 4201 4202 4203# Spark allows unix_timestamp() 4204# https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.unix_timestamp.html 4205class StrToUnix(Func): 4206 arg_types = {"this": False, "format": False} 4207 4208 4209class NumberToStr(Func): 4210 arg_types = {"this": True, "format": True} 4211 4212 4213class Struct(Func): 4214 arg_types = {"expressions": True} 4215 is_var_len_args = True 4216 4217 4218class StructExtract(Func): 4219 arg_types = {"this": True, "expression": True} 4220 4221 4222class Sum(AggFunc): 4223 pass 4224 4225 4226class Sqrt(Func): 4227 pass 4228 4229 4230class Stddev(AggFunc): 4231 pass 4232 4233 4234class StddevPop(AggFunc): 4235 pass 4236 4237 4238class StddevSamp(AggFunc): 4239 pass 4240 4241 4242class TimeToStr(Func): 4243 arg_types = {"this": True, "format": True} 4244 4245 4246class TimeToTimeStr(Func): 4247 pass 4248 4249 4250class TimeToUnix(Func): 4251 pass 4252 4253 4254class TimeStrToDate(Func): 4255 pass 4256 4257 4258class TimeStrToTime(Func): 4259 pass 4260 4261 4262class TimeStrToUnix(Func): 4263 pass 4264 4265 4266class Trim(Func): 4267 arg_types = { 4268 "this": True, 4269 "expression": False, 4270 "position": False, 4271 "collation": False, 4272 } 4273 4274 4275class TsOrDsAdd(Func, TimeUnit): 4276 arg_types = {"this": True, "expression": True, "unit": False} 4277 4278 4279class TsOrDsToDateStr(Func): 4280 pass 4281 4282 4283class TsOrDsToDate(Func): 4284 arg_types = {"this": True, "format": False} 4285 4286 4287class TsOrDiToDi(Func): 4288 pass 4289 4290 4291class Unhex(Func): 4292 pass 4293 4294 4295class UnixToStr(Func): 4296 arg_types = {"this": True, "format": False} 4297 4298 4299# https://prestodb.io/docs/current/functions/datetime.html 4300# presto has weird zone/hours/minutes 4301class UnixToTime(Func): 4302 arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False} 4303 4304 SECONDS = Literal.string("seconds") 4305 MILLIS = Literal.string("millis") 4306 MICROS = Literal.string("micros") 4307 4308 4309class UnixToTimeStr(Func): 4310 pass 4311 4312 4313class Upper(Func): 4314 _sql_names = ["UPPER", "UCASE"] 4315 4316 4317class Variance(AggFunc): 4318 _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"] 4319 4320 4321class VariancePop(AggFunc): 4322 _sql_names = ["VARIANCE_POP", "VAR_POP"] 4323 4324 4325class Week(Func): 4326 arg_types = {"this": True, "mode": False} 4327 4328 4329class XMLTable(Func): 4330 arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False} 4331 4332 4333class Year(Func): 4334 pass 4335 4336 4337class Use(Expression): 4338 arg_types = {"this": True, "kind": False} 4339 4340 4341class Merge(Expression): 4342 arg_types = {"this": True, "using": True, "on": True, "expressions": True} 4343 4344 4345class When(Func): 4346 arg_types = {"matched": True, "source": False, "condition": False, "then": True} 4347 4348 4349# https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqljnextvaluefor.html 4350# https://learn.microsoft.com/en-us/sql/t-sql/functions/next-value-for-transact-sql?view=sql-server-ver16 4351class NextValueFor(Func): 4352 arg_types = {"this": True, "order": False} 4353 4354 4355def _norm_arg(arg): 4356 return arg.lower() if type(arg) is str else arg 4357 4358 4359ALL_FUNCTIONS = subclasses(__name__, Func, (AggFunc, Anonymous, Func)) 4360 4361 4362# Helpers 4363@t.overload 4364def maybe_parse( 4365 sql_or_expression: ExpOrStr, 4366 *, 4367 into: t.Type[E], 4368 dialect: DialectType = None, 4369 prefix: t.Optional[str] = None, 4370 copy: bool = False, 4371 **opts, 4372) -> E: 4373 ... 4374 4375 4376@t.overload 4377def maybe_parse( 4378 sql_or_expression: str | E, 4379 *, 4380 into: t.Optional[IntoType] = None, 4381 dialect: DialectType = None, 4382 prefix: t.Optional[str] = None, 4383 copy: bool = False, 4384 **opts, 4385) -> E: 4386 ... 4387 4388 4389def maybe_parse( 4390 sql_or_expression: ExpOrStr, 4391 *, 4392 into: t.Optional[IntoType] = None, 4393 dialect: DialectType = None, 4394 prefix: t.Optional[str] = None, 4395 copy: bool = False, 4396 **opts, 4397) -> Expression: 4398 """Gracefully handle a possible string or expression. 4399 4400 Example: 4401 >>> maybe_parse("1") 4402 (LITERAL this: 1, is_string: False) 4403 >>> maybe_parse(to_identifier("x")) 4404 (IDENTIFIER this: x, quoted: False) 4405 4406 Args: 4407 sql_or_expression: the SQL code string or an expression 4408 into: the SQLGlot Expression to parse into 4409 dialect: the dialect used to parse the input expressions (in the case that an 4410 input expression is a SQL string). 4411 prefix: a string to prefix the sql with before it gets parsed 4412 (automatically includes a space) 4413 copy: whether or not to copy the expression. 4414 **opts: other options to use to parse the input expressions (again, in the case 4415 that an input expression is a SQL string). 4416 4417 Returns: 4418 Expression: the parsed or given expression. 4419 """ 4420 if isinstance(sql_or_expression, Expression): 4421 if copy: 4422 return sql_or_expression.copy() 4423 return sql_or_expression 4424 4425 import sqlglot 4426 4427 sql = str(sql_or_expression) 4428 if prefix: 4429 sql = f"{prefix} {sql}" 4430 return sqlglot.parse_one(sql, read=dialect, into=into, **opts) 4431 4432 4433def _maybe_copy(instance, copy=True): 4434 return instance.copy() if copy else instance 4435 4436 4437def _is_wrong_expression(expression, into): 4438 return isinstance(expression, Expression) and not isinstance(expression, into) 4439 4440 4441def _apply_builder( 4442 expression, 4443 instance, 4444 arg, 4445 copy=True, 4446 prefix=None, 4447 into=None, 4448 dialect=None, 4449 **opts, 4450): 4451 if _is_wrong_expression(expression, into): 4452 expression = into(this=expression) 4453 instance = _maybe_copy(instance, copy) 4454 expression = maybe_parse( 4455 sql_or_expression=expression, 4456 prefix=prefix, 4457 into=into, 4458 dialect=dialect, 4459 **opts, 4460 ) 4461 instance.set(arg, expression) 4462 return instance 4463 4464 4465def _apply_child_list_builder( 4466 *expressions, 4467 instance, 4468 arg, 4469 append=True, 4470 copy=True, 4471 prefix=None, 4472 into=None, 4473 dialect=None, 4474 properties=None, 4475 **opts, 4476): 4477 instance = _maybe_copy(instance, copy) 4478 parsed = [] 4479 for expression in expressions: 4480 if _is_wrong_expression(expression, into): 4481 expression = into(expressions=[expression]) 4482 expression = maybe_parse( 4483 expression, 4484 into=into, 4485 dialect=dialect, 4486 prefix=prefix, 4487 **opts, 4488 ) 4489 parsed.extend(expression.expressions) 4490 4491 existing = instance.args.get(arg) 4492 if append and existing: 4493 parsed = existing.expressions + parsed 4494 4495 child = into(expressions=parsed) 4496 for k, v in (properties or {}).items(): 4497 child.set(k, v) 4498 instance.set(arg, child) 4499 return instance 4500 4501 4502def _apply_list_builder( 4503 *expressions, 4504 instance, 4505 arg, 4506 append=True, 4507 copy=True, 4508 prefix=None, 4509 into=None, 4510 dialect=None, 4511 **opts, 4512): 4513 inst = _maybe_copy(instance, copy) 4514 4515 expressions = [ 4516 maybe_parse( 4517 sql_or_expression=expression, 4518 into=into, 4519 prefix=prefix, 4520 dialect=dialect, 4521 **opts, 4522 ) 4523 for expression in expressions 4524 ] 4525 4526 existing_expressions = inst.args.get(arg) 4527 if append and existing_expressions: 4528 expressions = existing_expressions + expressions 4529 4530 inst.set(arg, expressions) 4531 return inst 4532 4533 4534def _apply_conjunction_builder( 4535 *expressions, 4536 instance, 4537 arg, 4538 into=None, 4539 append=True, 4540 copy=True, 4541 dialect=None, 4542 **opts, 4543): 4544 expressions = [exp for exp in expressions if exp is not None and exp != ""] 4545 if not expressions: 4546 return instance 4547 4548 inst = _maybe_copy(instance, copy) 4549 4550 existing = inst.args.get(arg) 4551 if append and existing is not None: 4552 expressions = [existing.this if into else existing] + list(expressions) 4553 4554 node = and_(*expressions, dialect=dialect, copy=copy, **opts) 4555 4556 inst.set(arg, into(this=node) if into else node) 4557 return inst 4558 4559 4560def _apply_cte_builder( 4561 instance: E, 4562 alias: ExpOrStr, 4563 as_: ExpOrStr, 4564 recursive: t.Optional[bool] = None, 4565 append: bool = True, 4566 dialect: DialectType = None, 4567 copy: bool = True, 4568 **opts, 4569) -> E: 4570 alias_expression = maybe_parse(alias, dialect=dialect, into=TableAlias, **opts) 4571 as_expression = maybe_parse(as_, dialect=dialect, **opts) 4572 cte = CTE(this=as_expression, alias=alias_expression) 4573 return _apply_child_list_builder( 4574 cte, 4575 instance=instance, 4576 arg="with", 4577 append=append, 4578 copy=copy, 4579 into=With, 4580 properties={"recursive": recursive or False}, 4581 ) 4582 4583 4584def _combine(expressions, operator, dialect=None, copy=True, **opts): 4585 expressions = [ 4586 condition(expression, dialect=dialect, copy=copy, **opts) for expression in expressions 4587 ] 4588 this = expressions[0] 4589 if expressions[1:]: 4590 this = _wrap(this, Connector) 4591 for expression in expressions[1:]: 4592 this = operator(this=this, expression=_wrap(expression, Connector)) 4593 return this 4594 4595 4596def _wrap(expression: E, kind: t.Type[Expression]) -> E | Paren: 4597 if isinstance(expression, kind): 4598 return Paren(this=expression) 4599 return expression 4600 4601 4602def union(left, right, distinct=True, dialect=None, **opts): 4603 """ 4604 Initializes a syntax tree from one UNION expression. 4605 4606 Example: 4607 >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql() 4608 'SELECT * FROM foo UNION SELECT * FROM bla' 4609 4610 Args: 4611 left (str | Expression): the SQL code string corresponding to the left-hand side. 4612 If an `Expression` instance is passed, it will be used as-is. 4613 right (str | Expression): the SQL code string corresponding to the right-hand side. 4614 If an `Expression` instance is passed, it will be used as-is. 4615 distinct (bool): set the DISTINCT flag if and only if this is true. 4616 dialect (str): the dialect used to parse the input expression. 4617 opts (kwargs): other options to use to parse the input expressions. 4618 Returns: 4619 Union: the syntax tree for the UNION expression. 4620 """ 4621 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 4622 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 4623 4624 return Union(this=left, expression=right, distinct=distinct) 4625 4626 4627def intersect(left, right, distinct=True, dialect=None, **opts): 4628 """ 4629 Initializes a syntax tree from one INTERSECT expression. 4630 4631 Example: 4632 >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql() 4633 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 4634 4635 Args: 4636 left (str | Expression): the SQL code string corresponding to the left-hand side. 4637 If an `Expression` instance is passed, it will be used as-is. 4638 right (str | Expression): the SQL code string corresponding to the right-hand side. 4639 If an `Expression` instance is passed, it will be used as-is. 4640 distinct (bool): set the DISTINCT flag if and only if this is true. 4641 dialect (str): the dialect used to parse the input expression. 4642 opts (kwargs): other options to use to parse the input expressions. 4643 Returns: 4644 Intersect: the syntax tree for the INTERSECT expression. 4645 """ 4646 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 4647 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 4648 4649 return Intersect(this=left, expression=right, distinct=distinct) 4650 4651 4652def except_(left, right, distinct=True, dialect=None, **opts): 4653 """ 4654 Initializes a syntax tree from one EXCEPT expression. 4655 4656 Example: 4657 >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql() 4658 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 4659 4660 Args: 4661 left (str | Expression): the SQL code string corresponding to the left-hand side. 4662 If an `Expression` instance is passed, it will be used as-is. 4663 right (str | Expression): the SQL code string corresponding to the right-hand side. 4664 If an `Expression` instance is passed, it will be used as-is. 4665 distinct (bool): set the DISTINCT flag if and only if this is true. 4666 dialect (str): the dialect used to parse the input expression. 4667 opts (kwargs): other options to use to parse the input expressions. 4668 Returns: 4669 Except: the syntax tree for the EXCEPT statement. 4670 """ 4671 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 4672 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 4673 4674 return Except(this=left, expression=right, distinct=distinct) 4675 4676 4677def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select: 4678 """ 4679 Initializes a syntax tree from one or multiple SELECT expressions. 4680 4681 Example: 4682 >>> select("col1", "col2").from_("tbl").sql() 4683 'SELECT col1, col2 FROM tbl' 4684 4685 Args: 4686 *expressions: the SQL code string to parse as the expressions of a 4687 SELECT statement. If an Expression instance is passed, this is used as-is. 4688 dialect: the dialect used to parse the input expressions (in the case that an 4689 input expression is a SQL string). 4690 **opts: other options to use to parse the input expressions (again, in the case 4691 that an input expression is a SQL string). 4692 4693 Returns: 4694 Select: the syntax tree for the SELECT statement. 4695 """ 4696 return Select().select(*expressions, dialect=dialect, **opts) 4697 4698 4699def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select: 4700 """ 4701 Initializes a syntax tree from a FROM expression. 4702 4703 Example: 4704 >>> from_("tbl").select("col1", "col2").sql() 4705 'SELECT col1, col2 FROM tbl' 4706 4707 Args: 4708 *expression: the SQL code string to parse as the FROM expressions of a 4709 SELECT statement. If an Expression instance is passed, this is used as-is. 4710 dialect: the dialect used to parse the input expression (in the case that the 4711 input expression is a SQL string). 4712 **opts: other options to use to parse the input expressions (again, in the case 4713 that the input expression is a SQL string). 4714 4715 Returns: 4716 Select: the syntax tree for the SELECT statement. 4717 """ 4718 return Select().from_(expression, dialect=dialect, **opts) 4719 4720 4721def update( 4722 table: str | Table, 4723 properties: dict, 4724 where: t.Optional[ExpOrStr] = None, 4725 from_: t.Optional[ExpOrStr] = None, 4726 dialect: DialectType = None, 4727 **opts, 4728) -> Update: 4729 """ 4730 Creates an update statement. 4731 4732 Example: 4733 >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql() 4734 "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1" 4735 4736 Args: 4737 *properties: dictionary of properties to set which are 4738 auto converted to sql objects eg None -> NULL 4739 where: sql conditional parsed into a WHERE statement 4740 from_: sql statement parsed into a FROM statement 4741 dialect: the dialect used to parse the input expressions. 4742 **opts: other options to use to parse the input expressions. 4743 4744 Returns: 4745 Update: the syntax tree for the UPDATE statement. 4746 """ 4747 update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect)) 4748 update_expr.set( 4749 "expressions", 4750 [ 4751 EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v)) 4752 for k, v in properties.items() 4753 ], 4754 ) 4755 if from_: 4756 update_expr.set( 4757 "from", 4758 maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts), 4759 ) 4760 if isinstance(where, Condition): 4761 where = Where(this=where) 4762 if where: 4763 update_expr.set( 4764 "where", 4765 maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts), 4766 ) 4767 return update_expr 4768 4769 4770def delete( 4771 table: ExpOrStr, 4772 where: t.Optional[ExpOrStr] = None, 4773 returning: t.Optional[ExpOrStr] = None, 4774 dialect: DialectType = None, 4775 **opts, 4776) -> Delete: 4777 """ 4778 Builds a delete statement. 4779 4780 Example: 4781 >>> delete("my_table", where="id > 1").sql() 4782 'DELETE FROM my_table WHERE id > 1' 4783 4784 Args: 4785 where: sql conditional parsed into a WHERE statement 4786 returning: sql conditional parsed into a RETURNING statement 4787 dialect: the dialect used to parse the input expressions. 4788 **opts: other options to use to parse the input expressions. 4789 4790 Returns: 4791 Delete: the syntax tree for the DELETE statement. 4792 """ 4793 delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts) 4794 if where: 4795 delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts) 4796 if returning: 4797 delete_expr = delete_expr.returning(returning, dialect=dialect, copy=False, **opts) 4798 return delete_expr 4799 4800 4801def insert( 4802 expression: ExpOrStr, 4803 into: ExpOrStr, 4804 columns: t.Optional[t.Sequence[ExpOrStr]] = None, 4805 overwrite: t.Optional[bool] = None, 4806 dialect: DialectType = None, 4807 copy: bool = True, 4808 **opts, 4809) -> Insert: 4810 """ 4811 Builds an INSERT statement. 4812 4813 Example: 4814 >>> insert("VALUES (1, 2, 3)", "tbl").sql() 4815 'INSERT INTO tbl VALUES (1, 2, 3)' 4816 4817 Args: 4818 expression: the sql string or expression of the INSERT statement 4819 into: the tbl to insert data to. 4820 columns: optionally the table's column names. 4821 overwrite: whether to INSERT OVERWRITE or not. 4822 dialect: the dialect used to parse the input expressions. 4823 copy: whether or not to copy the expression. 4824 **opts: other options to use to parse the input expressions. 4825 4826 Returns: 4827 Insert: the syntax tree for the INSERT statement. 4828 """ 4829 expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts) 4830 this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts) 4831 4832 if columns: 4833 this = _apply_list_builder( 4834 *columns, 4835 instance=Schema(this=this), 4836 arg="expressions", 4837 into=Identifier, 4838 copy=False, 4839 dialect=dialect, 4840 **opts, 4841 ) 4842 4843 return Insert(this=this, expression=expr, overwrite=overwrite) 4844 4845 4846def condition(expression, dialect=None, copy=True, **opts) -> Condition: 4847 """ 4848 Initialize a logical condition expression. 4849 4850 Example: 4851 >>> condition("x=1").sql() 4852 'x = 1' 4853 4854 This is helpful for composing larger logical syntax trees: 4855 >>> where = condition("x=1") 4856 >>> where = where.and_("y=1") 4857 >>> Select().from_("tbl").select("*").where(where).sql() 4858 'SELECT * FROM tbl WHERE x = 1 AND y = 1' 4859 4860 Args: 4861 *expression (str | Expression): the SQL code string to parse. 4862 If an Expression instance is passed, this is used as-is. 4863 dialect (str): the dialect used to parse the input expression (in the case that the 4864 input expression is a SQL string). 4865 copy (bool): Whether or not to copy `expression` (only applies to expressions). 4866 **opts: other options to use to parse the input expressions (again, in the case 4867 that the input expression is a SQL string). 4868 4869 Returns: 4870 Condition: the expression 4871 """ 4872 return maybe_parse( # type: ignore 4873 expression, 4874 into=Condition, 4875 dialect=dialect, 4876 copy=copy, 4877 **opts, 4878 ) 4879 4880 4881def and_(*expressions, dialect=None, copy=True, **opts) -> And: 4882 """ 4883 Combine multiple conditions with an AND logical operator. 4884 4885 Example: 4886 >>> and_("x=1", and_("y=1", "z=1")).sql() 4887 'x = 1 AND (y = 1 AND z = 1)' 4888 4889 Args: 4890 *expressions (str | Expression): the SQL code strings to parse. 4891 If an Expression instance is passed, this is used as-is. 4892 dialect (str): the dialect used to parse the input expression. 4893 copy (bool): whether or not to copy `expressions` (only applies to Expressions). 4894 **opts: other options to use to parse the input expressions. 4895 4896 Returns: 4897 And: the new condition 4898 """ 4899 return _combine(expressions, And, dialect, copy=copy, **opts) 4900 4901 4902def or_(*expressions, dialect=None, copy=True, **opts) -> Or: 4903 """ 4904 Combine multiple conditions with an OR logical operator. 4905 4906 Example: 4907 >>> or_("x=1", or_("y=1", "z=1")).sql() 4908 'x = 1 OR (y = 1 OR z = 1)' 4909 4910 Args: 4911 *expressions (str | Expression): the SQL code strings to parse. 4912 If an Expression instance is passed, this is used as-is. 4913 dialect (str): the dialect used to parse the input expression. 4914 copy (bool): whether or not to copy `expressions` (only applies to Expressions). 4915 **opts: other options to use to parse the input expressions. 4916 4917 Returns: 4918 Or: the new condition 4919 """ 4920 return _combine(expressions, Or, dialect, copy=copy, **opts) 4921 4922 4923def not_(expression, dialect=None, copy=True, **opts) -> Not: 4924 """ 4925 Wrap a condition with a NOT operator. 4926 4927 Example: 4928 >>> not_("this_suit='black'").sql() 4929 "NOT this_suit = 'black'" 4930 4931 Args: 4932 expression (str | Expression): the SQL code strings to parse. 4933 If an Expression instance is passed, this is used as-is. 4934 dialect (str): the dialect used to parse the input expression. 4935 **opts: other options to use to parse the input expressions. 4936 4937 Returns: 4938 Not: the new condition 4939 """ 4940 this = condition( 4941 expression, 4942 dialect=dialect, 4943 copy=copy, 4944 **opts, 4945 ) 4946 return Not(this=_wrap(this, Connector)) 4947 4948 4949def paren(expression, copy=True) -> Paren: 4950 return Paren(this=_maybe_copy(expression, copy)) 4951 4952 4953SAFE_IDENTIFIER_RE = re.compile(r"^[_a-zA-Z][\w]*$") 4954 4955 4956@t.overload 4957def to_identifier(name: None, quoted: t.Optional[bool] = None, copy: bool = True) -> None: 4958 ... 4959 4960 4961@t.overload 4962def to_identifier( 4963 name: str | Identifier, quoted: t.Optional[bool] = None, copy: bool = True 4964) -> Identifier: 4965 ... 4966 4967 4968def to_identifier(name, quoted=None, copy=True): 4969 """Builds an identifier. 4970 4971 Args: 4972 name: The name to turn into an identifier. 4973 quoted: Whether or not force quote the identifier. 4974 copy: Whether or not to copy a passed in Identefier node. 4975 4976 Returns: 4977 The identifier ast node. 4978 """ 4979 4980 if name is None: 4981 return None 4982 4983 if isinstance(name, Identifier): 4984 identifier = _maybe_copy(name, copy) 4985 elif isinstance(name, str): 4986 identifier = Identifier( 4987 this=name, 4988 quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted, 4989 ) 4990 else: 4991 raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}") 4992 return identifier 4993 4994 4995INTERVAL_STRING_RE = re.compile(r"\s*([0-9]+)\s*([a-zA-Z]+)\s*") 4996 4997 4998def to_interval(interval: str | Literal) -> Interval: 4999 """Builds an interval expression from a string like '1 day' or '5 months'.""" 5000 if isinstance(interval, Literal): 5001 if not interval.is_string: 5002 raise ValueError("Invalid interval string.") 5003 5004 interval = interval.this 5005 5006 interval_parts = INTERVAL_STRING_RE.match(interval) # type: ignore 5007 5008 if not interval_parts: 5009 raise ValueError("Invalid interval string.") 5010 5011 return Interval( 5012 this=Literal.string(interval_parts.group(1)), 5013 unit=Var(this=interval_parts.group(2)), 5014 ) 5015 5016 5017@t.overload 5018def to_table(sql_path: str | Table, **kwargs) -> Table: 5019 ... 5020 5021 5022@t.overload 5023def to_table(sql_path: None, **kwargs) -> None: 5024 ... 5025 5026 5027def to_table(sql_path: t.Optional[str | Table], **kwargs) -> t.Optional[Table]: 5028 """ 5029 Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional. 5030 If a table is passed in then that table is returned. 5031 5032 Args: 5033 sql_path: a `[catalog].[schema].[table]` string. 5034 5035 Returns: 5036 A table expression. 5037 """ 5038 if sql_path is None or isinstance(sql_path, Table): 5039 return sql_path 5040 if not isinstance(sql_path, str): 5041 raise ValueError(f"Invalid type provided for a table: {type(sql_path)}") 5042 5043 catalog, db, table_name = (to_identifier(x) for x in split_num_words(sql_path, ".", 3)) 5044 return Table(this=table_name, db=db, catalog=catalog, **kwargs) 5045 5046 5047def to_column(sql_path: str | Column, **kwargs) -> Column: 5048 """ 5049 Create a column from a `[table].[column]` sql path. Schema is optional. 5050 5051 If a column is passed in then that column is returned. 5052 5053 Args: 5054 sql_path: `[table].[column]` string 5055 Returns: 5056 Table: A column expression 5057 """ 5058 if sql_path is None or isinstance(sql_path, Column): 5059 return sql_path 5060 if not isinstance(sql_path, str): 5061 raise ValueError(f"Invalid type provided for column: {type(sql_path)}") 5062 return column(*reversed(sql_path.split(".")), **kwargs) # type: ignore 5063 5064 5065def alias_( 5066 expression: ExpOrStr, 5067 alias: str | Identifier, 5068 table: bool | t.Sequence[str | Identifier] = False, 5069 quoted: t.Optional[bool] = None, 5070 dialect: DialectType = None, 5071 copy: bool = True, 5072 **opts, 5073): 5074 """Create an Alias expression. 5075 5076 Example: 5077 >>> alias_('foo', 'bar').sql() 5078 'foo AS bar' 5079 5080 >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql() 5081 '(SELECT 1, 2) AS bar(a, b)' 5082 5083 Args: 5084 expression: the SQL code strings to parse. 5085 If an Expression instance is passed, this is used as-is. 5086 alias: the alias name to use. If the name has 5087 special characters it is quoted. 5088 table: Whether or not to create a table alias, can also be a list of columns. 5089 quoted: whether or not to quote the alias 5090 dialect: the dialect used to parse the input expression. 5091 copy: Whether or not to copy the expression. 5092 **opts: other options to use to parse the input expressions. 5093 5094 Returns: 5095 Alias: the aliased expression 5096 """ 5097 exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts) 5098 alias = to_identifier(alias, quoted=quoted) 5099 5100 if table: 5101 table_alias = TableAlias(this=alias) 5102 exp.set("alias", table_alias) 5103 5104 if not isinstance(table, bool): 5105 for column in table: 5106 table_alias.append("columns", to_identifier(column, quoted=quoted)) 5107 5108 return exp 5109 5110 # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in 5111 # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node 5112 # for the complete Window expression. 5113 # 5114 # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls 5115 5116 if "alias" in exp.arg_types and not isinstance(exp, Window): 5117 exp.set("alias", alias) 5118 return exp 5119 return Alias(this=exp, alias=alias) 5120 5121 5122def subquery(expression, alias=None, dialect=None, **opts): 5123 """ 5124 Build a subquery expression. 5125 5126 Example: 5127 >>> subquery('select x from tbl', 'bar').select('x').sql() 5128 'SELECT x FROM (SELECT x FROM tbl) AS bar' 5129 5130 Args: 5131 expression (str | Expression): the SQL code strings to parse. 5132 If an Expression instance is passed, this is used as-is. 5133 alias (str | Expression): the alias name to use. 5134 dialect (str): the dialect used to parse the input expression. 5135 **opts: other options to use to parse the input expressions. 5136 5137 Returns: 5138 Select: a new select with the subquery expression included 5139 """ 5140 5141 expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias) 5142 return Select().from_(expression, dialect=dialect, **opts) 5143 5144 5145def column( 5146 col: str | Identifier, 5147 table: t.Optional[str | Identifier] = None, 5148 db: t.Optional[str | Identifier] = None, 5149 catalog: t.Optional[str | Identifier] = None, 5150 quoted: t.Optional[bool] = None, 5151) -> Column: 5152 """ 5153 Build a Column. 5154 5155 Args: 5156 col: column name 5157 table: table name 5158 db: db name 5159 catalog: catalog name 5160 quoted: whether or not to force quote each part 5161 Returns: 5162 Column: column instance 5163 """ 5164 return Column( 5165 this=to_identifier(col, quoted=quoted), 5166 table=to_identifier(table, quoted=quoted), 5167 db=to_identifier(db, quoted=quoted), 5168 catalog=to_identifier(catalog, quoted=quoted), 5169 ) 5170 5171 5172def cast(expression: ExpOrStr, to: str | DataType | DataType.Type, **opts) -> Cast: 5173 """Cast an expression to a data type. 5174 5175 Example: 5176 >>> cast('x + 1', 'int').sql() 5177 'CAST(x + 1 AS INT)' 5178 5179 Args: 5180 expression: The expression to cast. 5181 to: The datatype to cast to. 5182 5183 Returns: 5184 A cast node. 5185 """ 5186 expression = maybe_parse(expression, **opts) 5187 return Cast(this=expression, to=DataType.build(to, **opts)) 5188 5189 5190def table_(table, db=None, catalog=None, quoted=None, alias=None) -> Table: 5191 """Build a Table. 5192 5193 Args: 5194 table (str | Expression): column name 5195 db (str | Expression): db name 5196 catalog (str | Expression): catalog name 5197 5198 Returns: 5199 Table: table instance 5200 """ 5201 return Table( 5202 this=to_identifier(table, quoted=quoted), 5203 db=to_identifier(db, quoted=quoted), 5204 catalog=to_identifier(catalog, quoted=quoted), 5205 alias=TableAlias(this=to_identifier(alias)) if alias else None, 5206 ) 5207 5208 5209def values( 5210 values: t.Iterable[t.Tuple[t.Any, ...]], 5211 alias: t.Optional[str] = None, 5212 columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None, 5213) -> Values: 5214 """Build VALUES statement. 5215 5216 Example: 5217 >>> values([(1, '2')]).sql() 5218 "VALUES (1, '2')" 5219 5220 Args: 5221 values: values statements that will be converted to SQL 5222 alias: optional alias 5223 columns: Optional list of ordered column names or ordered dictionary of column names to types. 5224 If either are provided then an alias is also required. 5225 5226 Returns: 5227 Values: the Values expression object 5228 """ 5229 if columns and not alias: 5230 raise ValueError("Alias is required when providing columns") 5231 5232 return Values( 5233 expressions=[convert(tup) for tup in values], 5234 alias=( 5235 TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns]) 5236 if columns 5237 else (TableAlias(this=to_identifier(alias)) if alias else None) 5238 ), 5239 ) 5240 5241 5242def var(name: t.Optional[ExpOrStr]) -> Var: 5243 """Build a SQL variable. 5244 5245 Example: 5246 >>> repr(var('x')) 5247 '(VAR this: x)' 5248 5249 >>> repr(var(column('x', table='y'))) 5250 '(VAR this: x)' 5251 5252 Args: 5253 name: The name of the var or an expression who's name will become the var. 5254 5255 Returns: 5256 The new variable node. 5257 """ 5258 if not name: 5259 raise ValueError("Cannot convert empty name into var.") 5260 5261 if isinstance(name, Expression): 5262 name = name.name 5263 return Var(this=name) 5264 5265 5266def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable: 5267 """Build ALTER TABLE... RENAME... expression 5268 5269 Args: 5270 old_name: The old name of the table 5271 new_name: The new name of the table 5272 5273 Returns: 5274 Alter table expression 5275 """ 5276 old_table = to_table(old_name) 5277 new_table = to_table(new_name) 5278 return AlterTable( 5279 this=old_table, 5280 actions=[ 5281 RenameTable(this=new_table), 5282 ], 5283 ) 5284 5285 5286def convert(value: t.Any, copy: bool = False) -> Expression: 5287 """Convert a python value into an expression object. 5288 5289 Raises an error if a conversion is not possible. 5290 5291 Args: 5292 value: A python object. 5293 copy: Whether or not to copy `value` (only applies to Expressions and collections). 5294 5295 Returns: 5296 Expression: the equivalent expression object. 5297 """ 5298 if isinstance(value, Expression): 5299 return _maybe_copy(value, copy) 5300 if isinstance(value, str): 5301 return Literal.string(value) 5302 if isinstance(value, bool): 5303 return Boolean(this=value) 5304 if value is None or (isinstance(value, float) and math.isnan(value)): 5305 return NULL 5306 if isinstance(value, numbers.Number): 5307 return Literal.number(value) 5308 if isinstance(value, datetime.datetime): 5309 datetime_literal = Literal.string( 5310 (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat() 5311 ) 5312 return TimeStrToTime(this=datetime_literal) 5313 if isinstance(value, datetime.date): 5314 date_literal = Literal.string(value.strftime("%Y-%m-%d")) 5315 return DateStrToDate(this=date_literal) 5316 if isinstance(value, tuple): 5317 return Tuple(expressions=[convert(v, copy=copy) for v in value]) 5318 if isinstance(value, list): 5319 return Array(expressions=[convert(v, copy=copy) for v in value]) 5320 if isinstance(value, dict): 5321 return Map( 5322 keys=[convert(k, copy=copy) for k in value], 5323 values=[convert(v, copy=copy) for v in value.values()], 5324 ) 5325 raise ValueError(f"Cannot convert {value}") 5326 5327 5328def replace_children(expression, fun, *args, **kwargs): 5329 """ 5330 Replace children of an expression with the result of a lambda fun(child) -> exp. 5331 """ 5332 for k, v in expression.args.items(): 5333 is_list_arg = type(v) is list 5334 5335 child_nodes = v if is_list_arg else [v] 5336 new_child_nodes = [] 5337 5338 for cn in child_nodes: 5339 if isinstance(cn, Expression): 5340 for child_node in ensure_collection(fun(cn, *args, **kwargs)): 5341 new_child_nodes.append(child_node) 5342 child_node.parent = expression 5343 child_node.arg_key = k 5344 else: 5345 new_child_nodes.append(cn) 5346 5347 expression.args[k] = new_child_nodes if is_list_arg else seq_get(new_child_nodes, 0) 5348 5349 5350def column_table_names(expression): 5351 """ 5352 Return all table names referenced through columns in an expression. 5353 5354 Example: 5355 >>> import sqlglot 5356 >>> column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")) 5357 ['c', 'a'] 5358 5359 Args: 5360 expression (sqlglot.Expression): expression to find table names 5361 5362 Returns: 5363 list: A list of unique names 5364 """ 5365 return list(dict.fromkeys(column.table for column in expression.find_all(Column))) 5366 5367 5368def table_name(table) -> str: 5369 """Get the full name of a table as a string. 5370 5371 Args: 5372 table (exp.Table | str): table expression node or string. 5373 5374 Examples: 5375 >>> from sqlglot import exp, parse_one 5376 >>> table_name(parse_one("select * from a.b.c").find(exp.Table)) 5377 'a.b.c' 5378 5379 Returns: 5380 The table name. 5381 """ 5382 5383 table = maybe_parse(table, into=Table) 5384 5385 if not table: 5386 raise ValueError(f"Cannot parse {table}") 5387 5388 return ".".join( 5389 part 5390 for part in ( 5391 table.text("catalog"), 5392 table.text("db"), 5393 table.name, 5394 ) 5395 if part 5396 ) 5397 5398 5399def replace_tables(expression, mapping): 5400 """Replace all tables in expression according to the mapping. 5401 5402 Args: 5403 expression (sqlglot.Expression): expression node to be transformed and replaced. 5404 mapping (Dict[str, str]): mapping of table names. 5405 5406 Examples: 5407 >>> from sqlglot import exp, parse_one 5408 >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql() 5409 'SELECT * FROM c' 5410 5411 Returns: 5412 The mapped expression. 5413 """ 5414 5415 def _replace_tables(node): 5416 if isinstance(node, Table): 5417 new_name = mapping.get(table_name(node)) 5418 if new_name: 5419 return to_table( 5420 new_name, 5421 **{k: v for k, v in node.args.items() if k not in ("this", "db", "catalog")}, 5422 ) 5423 return node 5424 5425 return expression.transform(_replace_tables) 5426 5427 5428def replace_placeholders(expression, *args, **kwargs): 5429 """Replace placeholders in an expression. 5430 5431 Args: 5432 expression (sqlglot.Expression): expression node to be transformed and replaced. 5433 args: positional names that will substitute unnamed placeholders in the given order. 5434 kwargs: keyword arguments that will substitute named placeholders. 5435 5436 Examples: 5437 >>> from sqlglot import exp, parse_one 5438 >>> replace_placeholders( 5439 ... parse_one("select * from :tbl where ? = ?"), 5440 ... exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo") 5441 ... ).sql() 5442 "SELECT * FROM foo WHERE str_col = 'b'" 5443 5444 Returns: 5445 The mapped expression. 5446 """ 5447 5448 def _replace_placeholders(node, args, **kwargs): 5449 if isinstance(node, Placeholder): 5450 if node.name: 5451 new_name = kwargs.get(node.name) 5452 if new_name: 5453 return convert(new_name) 5454 else: 5455 try: 5456 return convert(next(args)) 5457 except StopIteration: 5458 pass 5459 return node 5460 5461 return expression.transform(_replace_placeholders, iter(args), **kwargs) 5462 5463 5464def expand( 5465 expression: Expression, sources: t.Dict[str, Subqueryable], copy: bool = True 5466) -> Expression: 5467 """Transforms an expression by expanding all referenced sources into subqueries. 5468 5469 Examples: 5470 >>> from sqlglot import parse_one 5471 >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql() 5472 'SELECT * FROM (SELECT * FROM y) AS z /* source: x */' 5473 5474 >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql() 5475 'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */' 5476 5477 Args: 5478 expression: The expression to expand. 5479 sources: A dictionary of name to Subqueryables. 5480 copy: Whether or not to copy the expression during transformation. Defaults to True. 5481 5482 Returns: 5483 The transformed expression. 5484 """ 5485 5486 def _expand(node: Expression): 5487 if isinstance(node, Table): 5488 name = table_name(node) 5489 source = sources.get(name) 5490 if source: 5491 subquery = source.subquery(node.alias or name) 5492 subquery.comments = [f"source: {name}"] 5493 return subquery.transform(_expand, copy=False) 5494 return node 5495 5496 return expression.transform(_expand, copy=copy) 5497 5498 5499def func(name: str, *args, dialect: DialectType = None, **kwargs) -> Func: 5500 """ 5501 Returns a Func expression. 5502 5503 Examples: 5504 >>> func("abs", 5).sql() 5505 'ABS(5)' 5506 5507 >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql() 5508 'CAST(5 AS DOUBLE)' 5509 5510 Args: 5511 name: the name of the function to build. 5512 args: the args used to instantiate the function of interest. 5513 dialect: the source dialect. 5514 kwargs: the kwargs used to instantiate the function of interest. 5515 5516 Note: 5517 The arguments `args` and `kwargs` are mutually exclusive. 5518 5519 Returns: 5520 An instance of the function of interest, or an anonymous function, if `name` doesn't 5521 correspond to an existing `sqlglot.expressions.Func` class. 5522 """ 5523 if args and kwargs: 5524 raise ValueError("Can't use both args and kwargs to instantiate a function.") 5525 5526 from sqlglot.dialects.dialect import Dialect 5527 5528 converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect) for arg in args] 5529 kwargs = {key: maybe_parse(value, dialect=dialect) for key, value in kwargs.items()} 5530 5531 parser = Dialect.get_or_raise(dialect)().parser() 5532 from_args_list = parser.FUNCTIONS.get(name.upper()) 5533 5534 if from_args_list: 5535 function = from_args_list(converted) if converted else from_args_list.__self__(**kwargs) # type: ignore 5536 else: 5537 kwargs = kwargs or {"expressions": converted} 5538 function = Anonymous(this=name, **kwargs) 5539 5540 for error_message in function.error_messages(converted): 5541 raise ValueError(error_message) 5542 5543 return function 5544 5545 5546def true(): 5547 """ 5548 Returns a true Boolean expression. 5549 """ 5550 return Boolean(this=True) 5551 5552 5553def false(): 5554 """ 5555 Returns a false Boolean expression. 5556 """ 5557 return Boolean(this=False) 5558 5559 5560def null(): 5561 """ 5562 Returns a Null expression. 5563 """ 5564 return Null() 5565 5566 5567# TODO: deprecate this 5568TRUE = Boolean(this=True) 5569FALSE = Boolean(this=False) 5570NULL = Null()
57class Expression(metaclass=_Expression): 58 """ 59 The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary 60 context, such as its child expressions, their names (arg keys), and whether a given child expression 61 is optional or not. 62 63 Attributes: 64 key: a unique key for each class in the Expression hierarchy. This is useful for hashing 65 and representing expressions as strings. 66 arg_types: determines what arguments (child nodes) are supported by an expression. It 67 maps arg keys to booleans that indicate whether the corresponding args are optional. 68 parent: a reference to the parent expression (or None, in case of root expressions). 69 arg_key: the arg key an expression is associated with, i.e. the name its parent expression 70 uses to refer to it. 71 comments: a list of comments that are associated with a given expression. This is used in 72 order to preserve comments when transpiling SQL code. 73 _type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the 74 optimizer, in order to enable some transformations that require type information. 75 76 Example: 77 >>> class Foo(Expression): 78 ... arg_types = {"this": True, "expression": False} 79 80 The above definition informs us that Foo is an Expression that requires an argument called 81 "this" and may also optionally receive an argument called "expression". 82 83 Args: 84 args: a mapping used for retrieving the arguments of an expression, given their arg keys. 85 """ 86 87 key = "expression" 88 arg_types = {"this": True} 89 __slots__ = ("args", "parent", "arg_key", "comments", "_type", "_meta", "_hash") 90 91 def __init__(self, **args: t.Any): 92 self.args: t.Dict[str, t.Any] = args 93 self.parent: t.Optional[Expression] = None 94 self.arg_key: t.Optional[str] = None 95 self.comments: t.Optional[t.List[str]] = None 96 self._type: t.Optional[DataType] = None 97 self._meta: t.Optional[t.Dict[str, t.Any]] = None 98 self._hash: t.Optional[int] = None 99 100 for arg_key, value in self.args.items(): 101 self._set_parent(arg_key, value) 102 103 def __eq__(self, other) -> bool: 104 return type(self) is type(other) and hash(self) == hash(other) 105 106 @property 107 def hashable_args(self) -> t.Any: 108 args = (self.args.get(k) for k in self.arg_types) 109 110 return tuple( 111 (tuple(_norm_arg(a) for a in arg) if arg else None) 112 if type(arg) is list 113 else (_norm_arg(arg) if arg is not None and arg is not False else None) 114 for arg in args 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): 125 """ 126 Retrieves the argument with key "this". 127 """ 128 return self.args.get("this") 129 130 @property 131 def expression(self): 132 """ 133 Retrieves the argument with key "expression". 134 """ 135 return self.args.get("expression") 136 137 @property 138 def expressions(self): 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 name(self) -> str: 201 return self.text("this") 202 203 @property 204 def alias_or_name(self): 205 return self.alias or self.name 206 207 @property 208 def output_name(self): 209 """ 210 Name of the output column if this expression is a selection. 211 212 If the Expression has no output name, an empty string is returned. 213 214 Example: 215 >>> from sqlglot import parse_one 216 >>> parse_one("SELECT a").expressions[0].output_name 217 'a' 218 >>> parse_one("SELECT b AS c").expressions[0].output_name 219 'c' 220 >>> parse_one("SELECT 1 + 2").expressions[0].output_name 221 '' 222 """ 223 return "" 224 225 @property 226 def type(self) -> t.Optional[DataType]: 227 return self._type 228 229 @type.setter 230 def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None: 231 if dtype and not isinstance(dtype, DataType): 232 dtype = DataType.build(dtype) 233 self._type = dtype # type: ignore 234 235 @property 236 def meta(self) -> t.Dict[str, t.Any]: 237 if self._meta is None: 238 self._meta = {} 239 return self._meta 240 241 def __deepcopy__(self, memo): 242 copy = self.__class__(**deepcopy(self.args)) 243 if self.comments is not None: 244 copy.comments = deepcopy(self.comments) 245 246 if self._type is not None: 247 copy._type = self._type.copy() 248 249 if self._meta is not None: 250 copy._meta = deepcopy(self._meta) 251 252 return copy 253 254 def copy(self): 255 """ 256 Returns a deep copy of the expression. 257 """ 258 new = deepcopy(self) 259 new.parent = self.parent 260 return new 261 262 def add_comments(self, comments: t.Optional[t.List[str]]) -> None: 263 if self.comments is None: 264 self.comments = [] 265 if comments: 266 self.comments.extend(comments) 267 268 def append(self, arg_key, value): 269 """ 270 Appends value to arg_key if it's a list or sets it as a new list. 271 272 Args: 273 arg_key (str): name of the list expression arg 274 value (Any): value to append to the list 275 """ 276 if not isinstance(self.args.get(arg_key), list): 277 self.args[arg_key] = [] 278 self.args[arg_key].append(value) 279 self._set_parent(arg_key, value) 280 281 def set(self, arg_key, value): 282 """ 283 Sets `arg_key` to `value`. 284 285 Args: 286 arg_key (str): name of the expression arg. 287 value: value to set the arg to. 288 """ 289 self.args[arg_key] = value 290 self._set_parent(arg_key, value) 291 292 def _set_parent(self, arg_key, value): 293 if hasattr(value, "parent"): 294 value.parent = self 295 value.arg_key = arg_key 296 elif type(value) is list: 297 for v in value: 298 if hasattr(v, "parent"): 299 v.parent = self 300 v.arg_key = arg_key 301 302 @property 303 def depth(self): 304 """ 305 Returns the depth of this tree. 306 """ 307 if self.parent: 308 return self.parent.depth + 1 309 return 0 310 311 def iter_expressions(self) -> t.Iterator[t.Tuple[str, Expression]]: 312 """Yields the key and expression for all arguments, exploding list args.""" 313 for k, vs in self.args.items(): 314 if type(vs) is list: 315 for v in vs: 316 if hasattr(v, "parent"): 317 yield k, v 318 else: 319 if hasattr(vs, "parent"): 320 yield k, vs 321 322 def find(self, *expression_types: t.Type[E], bfs=True) -> E | None: 323 """ 324 Returns the first node in this tree which matches at least one of 325 the specified types. 326 327 Args: 328 expression_types: the expression type(s) to match. 329 330 Returns: 331 The node which matches the criteria or None if no such node was found. 332 """ 333 return next(self.find_all(*expression_types, bfs=bfs), None) 334 335 def find_all(self, *expression_types: t.Type[E], bfs=True) -> t.Iterator[E]: 336 """ 337 Returns a generator object which visits all nodes in this tree and only 338 yields those that match at least one of the specified expression types. 339 340 Args: 341 expression_types: the expression type(s) to match. 342 343 Returns: 344 The generator object. 345 """ 346 for expression, *_ in self.walk(bfs=bfs): 347 if isinstance(expression, expression_types): 348 yield expression 349 350 def find_ancestor(self, *expression_types: t.Type[E]) -> E | None: 351 """ 352 Returns a nearest parent matching expression_types. 353 354 Args: 355 expression_types: the expression type(s) to match. 356 357 Returns: 358 The parent node. 359 """ 360 ancestor = self.parent 361 while ancestor and not isinstance(ancestor, expression_types): 362 ancestor = ancestor.parent 363 return t.cast(E, ancestor) 364 365 @property 366 def parent_select(self): 367 """ 368 Returns the parent select statement. 369 """ 370 return self.find_ancestor(Select) 371 372 @property 373 def same_parent(self): 374 """Returns if the parent is the same class as itself.""" 375 return type(self.parent) is self.__class__ 376 377 def root(self) -> Expression: 378 """ 379 Returns the root expression of this tree. 380 """ 381 expression = self 382 while expression.parent: 383 expression = expression.parent 384 return expression 385 386 def walk(self, bfs=True, prune=None): 387 """ 388 Returns a generator object which visits all nodes in this tree. 389 390 Args: 391 bfs (bool): if set to True the BFS traversal order will be applied, 392 otherwise the DFS traversal will be used instead. 393 prune ((node, parent, arg_key) -> bool): callable that returns True if 394 the generator should stop traversing this branch of the tree. 395 396 Returns: 397 the generator object. 398 """ 399 if bfs: 400 yield from self.bfs(prune=prune) 401 else: 402 yield from self.dfs(prune=prune) 403 404 def dfs(self, parent=None, key=None, prune=None): 405 """ 406 Returns a generator object which visits all nodes in this tree in 407 the DFS (Depth-first) order. 408 409 Returns: 410 The generator object. 411 """ 412 parent = parent or self.parent 413 yield self, parent, key 414 if prune and prune(self, parent, key): 415 return 416 417 for k, v in self.iter_expressions(): 418 yield from v.dfs(self, k, prune) 419 420 def bfs(self, prune=None): 421 """ 422 Returns a generator object which visits all nodes in this tree in 423 the BFS (Breadth-first) order. 424 425 Returns: 426 The generator object. 427 """ 428 queue = deque([(self, self.parent, None)]) 429 430 while queue: 431 item, parent, key = queue.popleft() 432 433 yield item, parent, key 434 if prune and prune(item, parent, key): 435 continue 436 437 for k, v in item.iter_expressions(): 438 queue.append((v, item, k)) 439 440 def unnest(self): 441 """ 442 Returns the first non parenthesis child or self. 443 """ 444 expression = self 445 while type(expression) is Paren: 446 expression = expression.this 447 return expression 448 449 def unalias(self): 450 """ 451 Returns the inner expression if this is an Alias. 452 """ 453 if isinstance(self, Alias): 454 return self.this 455 return self 456 457 def unnest_operands(self): 458 """ 459 Returns unnested operands as a tuple. 460 """ 461 return tuple(arg.unnest() for _, arg in self.iter_expressions()) 462 463 def flatten(self, unnest=True): 464 """ 465 Returns a generator which yields child nodes who's parents are the same class. 466 467 A AND B AND C -> [A, B, C] 468 """ 469 for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__): 470 if not type(node) is self.__class__: 471 yield node.unnest() if unnest else node 472 473 def __str__(self): 474 return self.sql() 475 476 def __repr__(self): 477 return self._to_s() 478 479 def sql(self, dialect: DialectType = None, **opts) -> str: 480 """ 481 Returns SQL string representation of this tree. 482 483 Args: 484 dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql"). 485 opts: other `sqlglot.generator.Generator` options. 486 487 Returns: 488 The SQL string. 489 """ 490 from sqlglot.dialects import Dialect 491 492 return Dialect.get_or_raise(dialect)().generate(self, **opts) 493 494 def _to_s(self, hide_missing: bool = True, level: int = 0) -> str: 495 indent = "" if not level else "\n" 496 indent += "".join([" "] * level) 497 left = f"({self.key.upper()} " 498 499 args: t.Dict[str, t.Any] = { 500 k: ", ".join( 501 v._to_s(hide_missing=hide_missing, level=level + 1) 502 if hasattr(v, "_to_s") 503 else str(v) 504 for v in ensure_list(vs) 505 if v is not None 506 ) 507 for k, vs in self.args.items() 508 } 509 args["comments"] = self.comments 510 args["type"] = self.type 511 args = {k: v for k, v in args.items() if v or not hide_missing} 512 513 right = ", ".join(f"{k}: {v}" for k, v in args.items()) 514 right += ")" 515 516 return indent + left + right 517 518 def transform(self, fun, *args, copy=True, **kwargs): 519 """ 520 Recursively visits all tree nodes (excluding already transformed ones) 521 and applies the given transformation function to each node. 522 523 Args: 524 fun (function): a function which takes a node as an argument and returns a 525 new transformed node or the same node without modifications. If the function 526 returns None, then the corresponding node will be removed from the syntax tree. 527 copy (bool): if set to True a new tree instance is constructed, otherwise the tree is 528 modified in place. 529 530 Returns: 531 The transformed tree. 532 """ 533 node = self.copy() if copy else self 534 new_node = fun(node, *args, **kwargs) 535 536 if new_node is None or not isinstance(new_node, Expression): 537 return new_node 538 if new_node is not node: 539 new_node.parent = node.parent 540 return new_node 541 542 replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs)) 543 return new_node 544 545 def replace(self, expression): 546 """ 547 Swap out this expression with a new expression. 548 549 For example:: 550 551 >>> tree = Select().select("x").from_("tbl") 552 >>> tree.find(Column).replace(Column(this="y")) 553 (COLUMN this: y) 554 >>> tree.sql() 555 'SELECT y FROM tbl' 556 557 Args: 558 expression (Expression|None): new node 559 560 Returns: 561 The new expression or expressions. 562 """ 563 if not self.parent: 564 return expression 565 566 parent = self.parent 567 self.parent = None 568 569 replace_children(parent, lambda child: expression if child is self else child) 570 return expression 571 572 def pop(self): 573 """ 574 Remove this expression from its AST. 575 576 Returns: 577 The popped expression. 578 """ 579 self.replace(None) 580 return self 581 582 def assert_is(self, type_): 583 """ 584 Assert that this `Expression` is an instance of `type_`. 585 586 If it is NOT an instance of `type_`, this raises an assertion error. 587 Otherwise, this returns this expression. 588 589 Examples: 590 This is useful for type security in chained expressions: 591 592 >>> import sqlglot 593 >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql() 594 'SELECT x, z FROM y' 595 """ 596 assert isinstance(self, type_) 597 return self 598 599 def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]: 600 """ 601 Checks if this expression is valid (e.g. all mandatory args are set). 602 603 Args: 604 args: a sequence of values that were used to instantiate a Func expression. This is used 605 to check that the provided arguments don't exceed the function argument limit. 606 607 Returns: 608 A list of error messages for all possible errors that were found. 609 """ 610 errors: t.List[str] = [] 611 612 for k in self.args: 613 if k not in self.arg_types: 614 errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") 615 for k, mandatory in self.arg_types.items(): 616 v = self.args.get(k) 617 if mandatory and (v is None or (isinstance(v, list) and not v)): 618 errors.append(f"Required keyword: '{k}' missing for {self.__class__}") 619 620 if ( 621 args 622 and isinstance(self, Func) 623 and len(args) > len(self.arg_types) 624 and not self.is_var_len_args 625 ): 626 errors.append( 627 f"The number of provided arguments ({len(args)}) is greater than " 628 f"the maximum number of supported arguments ({len(self.arg_types)})" 629 ) 630 631 return errors 632 633 def dump(self): 634 """ 635 Dump this Expression to a JSON-serializable dict. 636 """ 637 from sqlglot.serde import dump 638 639 return dump(self) 640 641 @classmethod 642 def load(cls, obj): 643 """ 644 Load a dict (as returned by `Expression.dump`) into an Expression instance. 645 """ 646 from sqlglot.serde import load 647 648 return load(obj)
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
sqlglot.expressions.DataTypetype of an expression. This is inferred by the optimizer, in order to enable some transformations that require type information.
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.
91 def __init__(self, **args: t.Any): 92 self.args: t.Dict[str, t.Any] = args 93 self.parent: t.Optional[Expression] = None 94 self.arg_key: t.Optional[str] = None 95 self.comments: t.Optional[t.List[str]] = None 96 self._type: t.Optional[DataType] = None 97 self._meta: t.Optional[t.Dict[str, t.Any]] = None 98 self._hash: t.Optional[int] = None 99 100 for arg_key, value in self.args.items(): 101 self._set_parent(arg_key, value)
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 ""
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.
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").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
254 def copy(self): 255 """ 256 Returns a deep copy of the expression. 257 """ 258 new = deepcopy(self) 259 new.parent = self.parent 260 return new
Returns a deep copy of the expression.
268 def append(self, arg_key, value): 269 """ 270 Appends value to arg_key if it's a list or sets it as a new list. 271 272 Args: 273 arg_key (str): name of the list expression arg 274 value (Any): value to append to the list 275 """ 276 if not isinstance(self.args.get(arg_key), list): 277 self.args[arg_key] = [] 278 self.args[arg_key].append(value) 279 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
281 def set(self, arg_key, value): 282 """ 283 Sets `arg_key` to `value`. 284 285 Args: 286 arg_key (str): name of the expression arg. 287 value: value to set the arg to. 288 """ 289 self.args[arg_key] = value 290 self._set_parent(arg_key, value)
Sets arg_key to value.
Arguments:
- arg_key (str): name of the expression arg.
- value: value to set the arg to.
311 def iter_expressions(self) -> t.Iterator[t.Tuple[str, Expression]]: 312 """Yields the key and expression for all arguments, exploding list args.""" 313 for k, vs in self.args.items(): 314 if type(vs) is list: 315 for v in vs: 316 if hasattr(v, "parent"): 317 yield k, v 318 else: 319 if hasattr(vs, "parent"): 320 yield k, vs
Yields the key and expression for all arguments, exploding list args.
322 def find(self, *expression_types: t.Type[E], bfs=True) -> E | None: 323 """ 324 Returns the first node in this tree which matches at least one of 325 the specified types. 326 327 Args: 328 expression_types: the expression type(s) to match. 329 330 Returns: 331 The node which matches the criteria or None if no such node was found. 332 """ 333 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.
Returns:
The node which matches the criteria or None if no such node was found.
335 def find_all(self, *expression_types: t.Type[E], bfs=True) -> t.Iterator[E]: 336 """ 337 Returns a generator object which visits all nodes in this tree and only 338 yields those that match at least one of the specified expression types. 339 340 Args: 341 expression_types: the expression type(s) to match. 342 343 Returns: 344 The generator object. 345 """ 346 for expression, *_ in self.walk(bfs=bfs): 347 if isinstance(expression, expression_types): 348 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.
Returns:
The generator object.
350 def find_ancestor(self, *expression_types: t.Type[E]) -> E | None: 351 """ 352 Returns a nearest parent matching expression_types. 353 354 Args: 355 expression_types: the expression type(s) to match. 356 357 Returns: 358 The parent node. 359 """ 360 ancestor = self.parent 361 while ancestor and not isinstance(ancestor, expression_types): 362 ancestor = ancestor.parent 363 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.
377 def root(self) -> Expression: 378 """ 379 Returns the root expression of this tree. 380 """ 381 expression = self 382 while expression.parent: 383 expression = expression.parent 384 return expression
Returns the root expression of this tree.
386 def walk(self, bfs=True, prune=None): 387 """ 388 Returns a generator object which visits all nodes in this tree. 389 390 Args: 391 bfs (bool): if set to True the BFS traversal order will be applied, 392 otherwise the DFS traversal will be used instead. 393 prune ((node, parent, arg_key) -> bool): callable that returns True if 394 the generator should stop traversing this branch of the tree. 395 396 Returns: 397 the generator object. 398 """ 399 if bfs: 400 yield from self.bfs(prune=prune) 401 else: 402 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.
404 def dfs(self, parent=None, key=None, prune=None): 405 """ 406 Returns a generator object which visits all nodes in this tree in 407 the DFS (Depth-first) order. 408 409 Returns: 410 The generator object. 411 """ 412 parent = parent or self.parent 413 yield self, parent, key 414 if prune and prune(self, parent, key): 415 return 416 417 for k, v in self.iter_expressions(): 418 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.
420 def bfs(self, prune=None): 421 """ 422 Returns a generator object which visits all nodes in this tree in 423 the BFS (Breadth-first) order. 424 425 Returns: 426 The generator object. 427 """ 428 queue = deque([(self, self.parent, None)]) 429 430 while queue: 431 item, parent, key = queue.popleft() 432 433 yield item, parent, key 434 if prune and prune(item, parent, key): 435 continue 436 437 for k, v in item.iter_expressions(): 438 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.
440 def unnest(self): 441 """ 442 Returns the first non parenthesis child or self. 443 """ 444 expression = self 445 while type(expression) is Paren: 446 expression = expression.this 447 return expression
Returns the first non parenthesis child or self.
449 def unalias(self): 450 """ 451 Returns the inner expression if this is an Alias. 452 """ 453 if isinstance(self, Alias): 454 return self.this 455 return self
Returns the inner expression if this is an Alias.
457 def unnest_operands(self): 458 """ 459 Returns unnested operands as a tuple. 460 """ 461 return tuple(arg.unnest() for _, arg in self.iter_expressions())
Returns unnested operands as a tuple.
463 def flatten(self, unnest=True): 464 """ 465 Returns a generator which yields child nodes who's parents are the same class. 466 467 A AND B AND C -> [A, B, C] 468 """ 469 for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not type(n) is self.__class__): 470 if not type(node) is self.__class__: 471 yield node.unnest() if unnest else node
Returns a generator which yields child nodes who's parents are the same class.
A AND B AND C -> [A, B, C]
479 def sql(self, dialect: DialectType = None, **opts) -> str: 480 """ 481 Returns SQL string representation of this tree. 482 483 Args: 484 dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql"). 485 opts: other `sqlglot.generator.Generator` options. 486 487 Returns: 488 The SQL string. 489 """ 490 from sqlglot.dialects import Dialect 491 492 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.Generatoroptions.
Returns:
The SQL string.
518 def transform(self, fun, *args, copy=True, **kwargs): 519 """ 520 Recursively visits all tree nodes (excluding already transformed ones) 521 and applies the given transformation function to each node. 522 523 Args: 524 fun (function): a function which takes a node as an argument and returns a 525 new transformed node or the same node without modifications. If the function 526 returns None, then the corresponding node will be removed from the syntax tree. 527 copy (bool): if set to True a new tree instance is constructed, otherwise the tree is 528 modified in place. 529 530 Returns: 531 The transformed tree. 532 """ 533 node = self.copy() if copy else self 534 new_node = fun(node, *args, **kwargs) 535 536 if new_node is None or not isinstance(new_node, Expression): 537 return new_node 538 if new_node is not node: 539 new_node.parent = node.parent 540 return new_node 541 542 replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs)) 543 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.
545 def replace(self, expression): 546 """ 547 Swap out this expression with a new expression. 548 549 For example:: 550 551 >>> tree = Select().select("x").from_("tbl") 552 >>> tree.find(Column).replace(Column(this="y")) 553 (COLUMN this: y) 554 >>> tree.sql() 555 'SELECT y FROM tbl' 556 557 Args: 558 expression (Expression|None): new node 559 560 Returns: 561 The new expression or expressions. 562 """ 563 if not self.parent: 564 return expression 565 566 parent = self.parent 567 self.parent = None 568 569 replace_children(parent, lambda child: expression if child is self else child) 570 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 (Expression|None): new node
Returns:
The new expression or expressions.
572 def pop(self): 573 """ 574 Remove this expression from its AST. 575 576 Returns: 577 The popped expression. 578 """ 579 self.replace(None) 580 return self
Remove this expression from its AST.
Returns:
The popped expression.
582 def assert_is(self, type_): 583 """ 584 Assert that this `Expression` is an instance of `type_`. 585 586 If it is NOT an instance of `type_`, this raises an assertion error. 587 Otherwise, this returns this expression. 588 589 Examples: 590 This is useful for type security in chained expressions: 591 592 >>> import sqlglot 593 >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql() 594 'SELECT x, z FROM y' 595 """ 596 assert isinstance(self, type_) 597 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'
599 def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]: 600 """ 601 Checks if this expression is valid (e.g. all mandatory args are set). 602 603 Args: 604 args: a sequence of values that were used to instantiate a Func expression. This is used 605 to check that the provided arguments don't exceed the function argument limit. 606 607 Returns: 608 A list of error messages for all possible errors that were found. 609 """ 610 errors: t.List[str] = [] 611 612 for k in self.args: 613 if k not in self.arg_types: 614 errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") 615 for k, mandatory in self.arg_types.items(): 616 v = self.args.get(k) 617 if mandatory and (v is None or (isinstance(v, list) and not v)): 618 errors.append(f"Required keyword: '{k}' missing for {self.__class__}") 619 620 if ( 621 args 622 and isinstance(self, Func) 623 and len(args) > len(self.arg_types) 624 and not self.is_var_len_args 625 ): 626 errors.append( 627 f"The number of provided arguments ({len(args)}) is greater than " 628 f"the maximum number of supported arguments ({len(self.arg_types)})" 629 ) 630 631 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.
633 def dump(self): 634 """ 635 Dump this Expression to a JSON-serializable dict. 636 """ 637 from sqlglot.serde import dump 638 639 return dump(self)
Dump this Expression to a JSON-serializable dict.
641 @classmethod 642 def load(cls, obj): 643 """ 644 Load a dict (as returned by `Expression.dump`) into an Expression instance. 645 """ 646 from sqlglot.serde import load 647 648 return load(obj)
Load a dict (as returned by Expression.dump) into an Expression instance.
659class Condition(Expression): 660 def and_(self, *expressions, dialect=None, copy=True, **opts): 661 """ 662 AND this condition with one or multiple expressions. 663 664 Example: 665 >>> condition("x=1").and_("y=1").sql() 666 'x = 1 AND y = 1' 667 668 Args: 669 *expressions (str | Expression): the SQL code strings to parse. 670 If an `Expression` instance is passed, it will be used as-is. 671 dialect (str): the dialect used to parse the input expression. 672 copy (bool): whether or not to copy the involved expressions (only applies to Expressions). 673 opts (kwargs): other options to use to parse the input expressions. 674 675 Returns: 676 And: the new condition. 677 """ 678 return and_(self, *expressions, dialect=dialect, copy=copy, **opts) 679 680 def or_(self, *expressions, dialect=None, copy=True, **opts): 681 """ 682 OR this condition with one or multiple expressions. 683 684 Example: 685 >>> condition("x=1").or_("y=1").sql() 686 'x = 1 OR y = 1' 687 688 Args: 689 *expressions (str | Expression): the SQL code strings to parse. 690 If an `Expression` instance is passed, it will be used as-is. 691 dialect (str): the dialect used to parse the input expression. 692 copy (bool): whether or not to copy the involved expressions (only applies to Expressions). 693 opts (kwargs): other options to use to parse the input expressions. 694 695 Returns: 696 Or: the new condition. 697 """ 698 return or_(self, *expressions, dialect=dialect, copy=copy, **opts) 699 700 def not_(self, copy=True): 701 """ 702 Wrap this condition with NOT. 703 704 Example: 705 >>> condition("x=1").not_().sql() 706 'NOT x = 1' 707 708 Args: 709 copy (bool): whether or not to copy this object. 710 711 Returns: 712 Not: the new condition. 713 """ 714 return not_(self, copy=copy) 715 716 def as_( 717 self, 718 alias: str | Identifier, 719 quoted: t.Optional[bool] = None, 720 dialect: DialectType = None, 721 copy: bool = True, 722 **opts, 723 ) -> Alias: 724 return alias_(self, alias, quoted=quoted, dialect=dialect, copy=copy, **opts) 725 726 def _binop(self, klass: t.Type[E], other: t.Any, reverse: bool = False) -> E: 727 this = self.copy() 728 other = convert(other, copy=True) 729 if not isinstance(this, klass) and not isinstance(other, klass): 730 this = _wrap(this, Binary) 731 other = _wrap(other, Binary) 732 if reverse: 733 return klass(this=other, expression=this) 734 return klass(this=this, expression=other) 735 736 def __getitem__(self, other: ExpOrStr | t.Tuple[ExpOrStr]): 737 return Bracket( 738 this=self.copy(), expressions=[convert(e, copy=True) for e in ensure_list(other)] 739 ) 740 741 def isin( 742 self, *expressions: t.Any, query: t.Optional[ExpOrStr] = None, copy=True, **opts 743 ) -> In: 744 return In( 745 this=_maybe_copy(self, copy), 746 expressions=[convert(e, copy=copy) for e in expressions], 747 query=maybe_parse(query, copy=copy, **opts) if query else None, 748 ) 749 750 def between(self, low: t.Any, high: t.Any, copy=True, **opts) -> Between: 751 return Between( 752 this=_maybe_copy(self, copy), 753 low=convert(low, copy=copy, **opts), 754 high=convert(high, copy=copy, **opts), 755 ) 756 757 def is_(self, other: ExpOrStr) -> Is: 758 return self._binop(Is, other) 759 760 def like(self, other: ExpOrStr) -> Like: 761 return self._binop(Like, other) 762 763 def ilike(self, other: ExpOrStr) -> ILike: 764 return self._binop(ILike, other) 765 766 def eq(self, other: t.Any) -> EQ: 767 return self._binop(EQ, other) 768 769 def neq(self, other: t.Any) -> NEQ: 770 return self._binop(NEQ, other) 771 772 def rlike(self, other: ExpOrStr) -> RegexpLike: 773 return self._binop(RegexpLike, other) 774 775 def __lt__(self, other: t.Any) -> LT: 776 return self._binop(LT, other) 777 778 def __le__(self, other: t.Any) -> LTE: 779 return self._binop(LTE, other) 780 781 def __gt__(self, other: t.Any) -> GT: 782 return self._binop(GT, other) 783 784 def __ge__(self, other: t.Any) -> GTE: 785 return self._binop(GTE, other) 786 787 def __add__(self, other: t.Any) -> Add: 788 return self._binop(Add, other) 789 790 def __radd__(self, other: t.Any) -> Add: 791 return self._binop(Add, other, reverse=True) 792 793 def __sub__(self, other: t.Any) -> Sub: 794 return self._binop(Sub, other) 795 796 def __rsub__(self, other: t.Any) -> Sub: 797 return self._binop(Sub, other, reverse=True) 798 799 def __mul__(self, other: t.Any) -> Mul: 800 return self._binop(Mul, other) 801 802 def __rmul__(self, other: t.Any) -> Mul: 803 return self._binop(Mul, other, reverse=True) 804 805 def __truediv__(self, other: t.Any) -> Div: 806 return self._binop(Div, other) 807 808 def __rtruediv__(self, other: t.Any) -> Div: 809 return self._binop(Div, other, reverse=True) 810 811 def __floordiv__(self, other: t.Any) -> IntDiv: 812 return self._binop(IntDiv, other) 813 814 def __rfloordiv__(self, other: t.Any) -> IntDiv: 815 return self._binop(IntDiv, other, reverse=True) 816 817 def __mod__(self, other: t.Any) -> Mod: 818 return self._binop(Mod, other) 819 820 def __rmod__(self, other: t.Any) -> Mod: 821 return self._binop(Mod, other, reverse=True) 822 823 def __pow__(self, other: t.Any) -> Pow: 824 return self._binop(Pow, other) 825 826 def __rpow__(self, other: t.Any) -> Pow: 827 return self._binop(Pow, other, reverse=True) 828 829 def __and__(self, other: t.Any) -> And: 830 return self._binop(And, other) 831 832 def __rand__(self, other: t.Any) -> And: 833 return self._binop(And, other, reverse=True) 834 835 def __or__(self, other: t.Any) -> Or: 836 return self._binop(Or, other) 837 838 def __ror__(self, other: t.Any) -> Or: 839 return self._binop(Or, other, reverse=True) 840 841 def __neg__(self) -> Neg: 842 return Neg(this=_wrap(self.copy(), Binary)) 843 844 def __invert__(self) -> Not: 845 return not_(self.copy())
660 def and_(self, *expressions, dialect=None, copy=True, **opts): 661 """ 662 AND this condition with one or multiple expressions. 663 664 Example: 665 >>> condition("x=1").and_("y=1").sql() 666 'x = 1 AND y = 1' 667 668 Args: 669 *expressions (str | Expression): the SQL code strings to parse. 670 If an `Expression` instance is passed, it will be used as-is. 671 dialect (str): the dialect used to parse the input expression. 672 copy (bool): whether or not to copy the involved expressions (only applies to Expressions). 673 opts (kwargs): other options to use to parse the input expressions. 674 675 Returns: 676 And: the new condition. 677 """ 678 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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. - dialect (str): the dialect used to parse the input expression.
- copy (bool): whether or not to copy the involved expressions (only applies to Expressions).
- opts (kwargs): other options to use to parse the input expressions.
Returns:
And: the new condition.
680 def or_(self, *expressions, dialect=None, copy=True, **opts): 681 """ 682 OR this condition with one or multiple expressions. 683 684 Example: 685 >>> condition("x=1").or_("y=1").sql() 686 'x = 1 OR y = 1' 687 688 Args: 689 *expressions (str | Expression): the SQL code strings to parse. 690 If an `Expression` instance is passed, it will be used as-is. 691 dialect (str): the dialect used to parse the input expression. 692 copy (bool): whether or not to copy the involved expressions (only applies to Expressions). 693 opts (kwargs): other options to use to parse the input expressions. 694 695 Returns: 696 Or: the new condition. 697 """ 698 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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. - dialect (str): the dialect used to parse the input expression.
- copy (bool): whether or not to copy the involved expressions (only applies to Expressions).
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Or: the new condition.
700 def not_(self, copy=True): 701 """ 702 Wrap this condition with NOT. 703 704 Example: 705 >>> condition("x=1").not_().sql() 706 'NOT x = 1' 707 708 Args: 709 copy (bool): whether or not to copy this object. 710 711 Returns: 712 Not: the new condition. 713 """ 714 return not_(self, copy=copy)
Wrap this condition with NOT.
Example:
>>> condition("x=1").not_().sql() 'NOT x = 1'
Arguments:
- copy (bool): whether or not to copy this object.
Returns:
Not: the new condition.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Relationships like x = y, x > 1, x >= y.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
852class DerivedTable(Expression): 853 @property 854 def alias_column_names(self): 855 table_alias = self.args.get("alias") 856 if not table_alias: 857 return [] 858 column_list = table_alias.assert_is(TableAlias).args.get("columns") or [] 859 return [c.name for c in column_list] 860 861 @property 862 def selects(self): 863 return self.this.selects if isinstance(self.this, Subqueryable) else [] 864 865 @property 866 def named_selects(self): 867 return [select.output_name for select in self.selects]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
870class Unionable(Expression): 871 def union(self, expression, distinct=True, dialect=None, **opts): 872 """ 873 Builds a UNION expression. 874 875 Example: 876 >>> import sqlglot 877 >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql() 878 'SELECT * FROM foo UNION SELECT * FROM bla' 879 880 Args: 881 expression (str | Expression): the SQL code string. 882 If an `Expression` instance is passed, it will be used as-is. 883 distinct (bool): set the DISTINCT flag if and only if this is true. 884 dialect (str): the dialect used to parse the input expression. 885 opts (kwargs): other options to use to parse the input expressions. 886 Returns: 887 Union: the Union expression. 888 """ 889 return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 890 891 def intersect(self, expression, distinct=True, dialect=None, **opts): 892 """ 893 Builds an INTERSECT expression. 894 895 Example: 896 >>> import sqlglot 897 >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql() 898 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 899 900 Args: 901 expression (str | Expression): the SQL code string. 902 If an `Expression` instance is passed, it will be used as-is. 903 distinct (bool): set the DISTINCT flag if and only if this is true. 904 dialect (str): the dialect used to parse the input expression. 905 opts (kwargs): other options to use to parse the input expressions. 906 Returns: 907 Intersect: the Intersect expression 908 """ 909 return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 910 911 def except_(self, expression, distinct=True, dialect=None, **opts): 912 """ 913 Builds an EXCEPT expression. 914 915 Example: 916 >>> import sqlglot 917 >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql() 918 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 919 920 Args: 921 expression (str | Expression): the SQL code string. 922 If an `Expression` instance is passed, it will be used as-is. 923 distinct (bool): set the DISTINCT flag if and only if this is true. 924 dialect (str): the dialect used to parse the input expression. 925 opts (kwargs): other options to use to parse the input expressions. 926 Returns: 927 Except: the Except expression 928 """ 929 return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
871 def union(self, expression, distinct=True, dialect=None, **opts): 872 """ 873 Builds a UNION expression. 874 875 Example: 876 >>> import sqlglot 877 >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql() 878 'SELECT * FROM foo UNION SELECT * FROM bla' 879 880 Args: 881 expression (str | Expression): the SQL code string. 882 If an `Expression` instance is passed, it will be used as-is. 883 distinct (bool): set the DISTINCT flag if and only if this is true. 884 dialect (str): the dialect used to parse the input expression. 885 opts (kwargs): other options to use to parse the input expressions. 886 Returns: 887 Union: the Union expression. 888 """ 889 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 (str | Expression): the SQL code string.
If an
Expressioninstance is passed, it will be used as-is. - distinct (bool): set the DISTINCT flag if and only if this is true.
- dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Union: the Union expression.
891 def intersect(self, expression, distinct=True, dialect=None, **opts): 892 """ 893 Builds an INTERSECT expression. 894 895 Example: 896 >>> import sqlglot 897 >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql() 898 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 899 900 Args: 901 expression (str | Expression): the SQL code string. 902 If an `Expression` instance is passed, it will be used as-is. 903 distinct (bool): set the DISTINCT flag if and only if this is true. 904 dialect (str): the dialect used to parse the input expression. 905 opts (kwargs): other options to use to parse the input expressions. 906 Returns: 907 Intersect: the Intersect expression 908 """ 909 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 (str | Expression): the SQL code string.
If an
Expressioninstance is passed, it will be used as-is. - distinct (bool): set the DISTINCT flag if and only if this is true.
- dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Intersect: the Intersect expression
911 def except_(self, expression, distinct=True, dialect=None, **opts): 912 """ 913 Builds an EXCEPT expression. 914 915 Example: 916 >>> import sqlglot 917 >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql() 918 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 919 920 Args: 921 expression (str | Expression): the SQL code string. 922 If an `Expression` instance is passed, it will be used as-is. 923 distinct (bool): set the DISTINCT flag if and only if this is true. 924 dialect (str): the dialect used to parse the input expression. 925 opts (kwargs): other options to use to parse the input expressions. 926 Returns: 927 Except: the Except expression 928 """ 929 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 (str | Expression): the SQL code string.
If an
Expressioninstance is passed, it will be used as-is. - distinct (bool): set the DISTINCT flag if and only if this is true.
- dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Except: the Except expression
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
932class UDTF(DerivedTable, Unionable): 933 @property 934 def selects(self): 935 alias = self.args.get("alias") 936 return alias.columns if alias else []
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
939class Cache(Expression): 940 arg_types = { 941 "with": False, 942 "this": True, 943 "lazy": False, 944 "options": False, 945 "expression": False, 946 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
953class Create(Expression): 954 arg_types = { 955 "with": False, 956 "this": True, 957 "kind": True, 958 "expression": False, 959 "exists": False, 960 "properties": False, 961 "replace": False, 962 "unique": False, 963 "indexes": False, 964 "no_schema_binding": False, 965 "begin": False, 966 "clone": False, 967 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
971class Clone(Expression): 972 arg_types = { 973 "this": True, 974 "when": False, 975 "kind": False, 976 "expression": False, 977 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
992class SetItem(Expression): 993 arg_types = { 994 "this": False, 995 "expressions": False, 996 "kind": False, 997 "collate": False, # MySQL SET NAMES statement 998 "global": False, 999 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1002class Show(Expression): 1003 arg_types = { 1004 "this": True, 1005 "target": False, 1006 "offset": False, 1007 "limit": False, 1008 "like": False, 1009 "where": False, 1010 "db": False, 1011 "full": False, 1012 "mutex": False, 1013 "query": False, 1014 "channel": False, 1015 "global": False, 1016 "log": False, 1017 "position": False, 1018 "types": False, 1019 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1022class UserDefinedFunction(Expression): 1023 arg_types = {"this": True, "expressions": False, "wrapped": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1030class With(Expression): 1031 arg_types = {"expressions": True, "recursive": False} 1032 1033 @property 1034 def recursive(self) -> bool: 1035 return bool(self.args.get("recursive"))
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1046class TableAlias(Expression): 1047 arg_types = {"this": False, "columns": False} 1048 1049 @property 1050 def columns(self): 1051 return self.args.get("columns") or []
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1066class Column(Condition): 1067 arg_types = {"this": True, "table": False, "db": False, "catalog": False, "join_mark": False} 1068 1069 @property 1070 def table(self) -> str: 1071 return self.text("table") 1072 1073 @property 1074 def db(self) -> str: 1075 return self.text("db") 1076 1077 @property 1078 def catalog(self) -> str: 1079 return self.text("catalog") 1080 1081 @property 1082 def output_name(self) -> str: 1083 return self.name 1084 1085 @property 1086 def parts(self) -> t.List[Identifier]: 1087 """Return the parts of a column in order catalog, db, table, name.""" 1088 return [ 1089 t.cast(Identifier, self.args[part]) 1090 for part in ("catalog", "db", "table", "this") 1091 if self.args.get(part) 1092 ] 1093 1094 def to_dot(self) -> Dot: 1095 """Converts the column into a dot expression.""" 1096 parts = self.parts 1097 parent = self.parent 1098 1099 while parent: 1100 if isinstance(parent, Dot): 1101 parts.append(parent.expression) 1102 parent = parent.parent 1103 1104 return Dot.build(parts)
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").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Return the parts of a column in order catalog, db, table, name.
1094 def to_dot(self) -> Dot: 1095 """Converts the column into a dot expression.""" 1096 parts = self.parts 1097 parent = self.parent 1098 1099 while parent: 1100 if isinstance(parent, Dot): 1101 parts.append(parent.expression) 1102 parent = parent.parent 1103 1104 return Dot.build(parts)
Converts the column into a dot expression.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1111class ColumnDef(Expression): 1112 arg_types = { 1113 "this": True, 1114 "kind": False, 1115 "constraints": False, 1116 "exists": False, 1117 "position": False, 1118 } 1119 1120 @property 1121 def constraints(self) -> t.List[ColumnConstraint]: 1122 return self.args.get("constraints") or []
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1125class AlterColumn(Expression): 1126 arg_types = { 1127 "this": True, 1128 "dtype": False, 1129 "collate": False, 1130 "using": False, 1131 "default": False, 1132 "drop": False, 1133 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1144class Comment(Expression): 1145 arg_types = {"this": True, "kind": True, "expression": True, "exists": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1149class MergeTreeTTLAction(Expression): 1150 arg_types = { 1151 "this": True, 1152 "delete": False, 1153 "recompress": False, 1154 "to_disk": False, 1155 "to_volume": False, 1156 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1160class MergeTreeTTL(Expression): 1161 arg_types = { 1162 "expressions": True, 1163 "where": False, 1164 "group": False, 1165 "aggregates": False, 1166 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1169class ColumnConstraint(Expression): 1170 arg_types = {"this": False, "kind": True} 1171 1172 @property 1173 def kind(self) -> ColumnConstraintKind: 1174 return self.args["kind"]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1221class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind): 1222 # this: True -> ALWAYS, this: False -> BY DEFAULT 1223 arg_types = { 1224 "this": False, 1225 "on_null": False, 1226 "start": False, 1227 "increment": False, 1228 "minvalue": False, 1229 "maxvalue": False, 1230 "cycle": False, 1231 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1271class Delete(Expression): 1272 arg_types = {"with": False, "this": False, "using": False, "where": False, "returning": False} 1273 1274 def delete( 1275 self, 1276 table: ExpOrStr, 1277 dialect: DialectType = None, 1278 copy: bool = True, 1279 **opts, 1280 ) -> Delete: 1281 """ 1282 Create a DELETE expression or replace the table on an existing DELETE expression. 1283 1284 Example: 1285 >>> delete("tbl").sql() 1286 'DELETE FROM tbl' 1287 1288 Args: 1289 table: the table from which to delete. 1290 dialect: the dialect used to parse the input expression. 1291 copy: if `False`, modify this expression instance in-place. 1292 opts: other options to use to parse the input expressions. 1293 1294 Returns: 1295 Delete: the modified expression. 1296 """ 1297 return _apply_builder( 1298 expression=table, 1299 instance=self, 1300 arg="this", 1301 dialect=dialect, 1302 into=Table, 1303 copy=copy, 1304 **opts, 1305 ) 1306 1307 def where( 1308 self, 1309 *expressions: ExpOrStr, 1310 append: bool = True, 1311 dialect: DialectType = None, 1312 copy: bool = True, 1313 **opts, 1314 ) -> Delete: 1315 """ 1316 Append to or set the WHERE expressions. 1317 1318 Example: 1319 >>> delete("tbl").where("x = 'a' OR x < 'b'").sql() 1320 "DELETE FROM tbl WHERE x = 'a' OR x < 'b'" 1321 1322 Args: 1323 *expressions: the SQL code strings to parse. 1324 If an `Expression` instance is passed, it will be used as-is. 1325 Multiple expressions are combined with an AND operator. 1326 append: if `True`, AND the new expressions to any existing expression. 1327 Otherwise, this resets the expression. 1328 dialect: the dialect used to parse the input expressions. 1329 copy: if `False`, modify this expression instance in-place. 1330 opts: other options to use to parse the input expressions. 1331 1332 Returns: 1333 Delete: the modified expression. 1334 """ 1335 return _apply_conjunction_builder( 1336 *expressions, 1337 instance=self, 1338 arg="where", 1339 append=append, 1340 into=Where, 1341 dialect=dialect, 1342 copy=copy, 1343 **opts, 1344 ) 1345 1346 def returning( 1347 self, 1348 expression: ExpOrStr, 1349 dialect: DialectType = None, 1350 copy: bool = True, 1351 **opts, 1352 ) -> Delete: 1353 """ 1354 Set the RETURNING expression. Not supported by all dialects. 1355 1356 Example: 1357 >>> delete("tbl").returning("*", dialect="postgres").sql() 1358 'DELETE FROM tbl RETURNING *' 1359 1360 Args: 1361 expression: the SQL code strings to parse. 1362 If an `Expression` instance is passed, it will be used as-is. 1363 dialect: the dialect used to parse the input expressions. 1364 copy: if `False`, modify this expression instance in-place. 1365 opts: other options to use to parse the input expressions. 1366 1367 Returns: 1368 Delete: the modified expression. 1369 """ 1370 return _apply_builder( 1371 expression=expression, 1372 instance=self, 1373 arg="returning", 1374 prefix="RETURNING", 1375 dialect=dialect, 1376 copy=copy, 1377 into=Returning, 1378 **opts, 1379 )
1274 def delete( 1275 self, 1276 table: ExpOrStr, 1277 dialect: DialectType = None, 1278 copy: bool = True, 1279 **opts, 1280 ) -> Delete: 1281 """ 1282 Create a DELETE expression or replace the table on an existing DELETE expression. 1283 1284 Example: 1285 >>> delete("tbl").sql() 1286 'DELETE FROM tbl' 1287 1288 Args: 1289 table: the table from which to delete. 1290 dialect: the dialect used to parse the input expression. 1291 copy: if `False`, modify this expression instance in-place. 1292 opts: other options to use to parse the input expressions. 1293 1294 Returns: 1295 Delete: the modified expression. 1296 """ 1297 return _apply_builder( 1298 expression=table, 1299 instance=self, 1300 arg="this", 1301 dialect=dialect, 1302 into=Table, 1303 copy=copy, 1304 **opts, 1305 )
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.
1307 def where( 1308 self, 1309 *expressions: ExpOrStr, 1310 append: bool = True, 1311 dialect: DialectType = None, 1312 copy: bool = True, 1313 **opts, 1314 ) -> Delete: 1315 """ 1316 Append to or set the WHERE expressions. 1317 1318 Example: 1319 >>> delete("tbl").where("x = 'a' OR x < 'b'").sql() 1320 "DELETE FROM tbl WHERE x = 'a' OR x < 'b'" 1321 1322 Args: 1323 *expressions: the SQL code strings to parse. 1324 If an `Expression` instance is passed, it will be used as-is. 1325 Multiple expressions are combined with an AND operator. 1326 append: if `True`, AND the new expressions to any existing expression. 1327 Otherwise, this resets the expression. 1328 dialect: the dialect used to parse the input expressions. 1329 copy: if `False`, modify this expression instance in-place. 1330 opts: other options to use to parse the input expressions. 1331 1332 Returns: 1333 Delete: the modified expression. 1334 """ 1335 return _apply_conjunction_builder( 1336 *expressions, 1337 instance=self, 1338 arg="where", 1339 append=append, 1340 into=Where, 1341 dialect=dialect, 1342 copy=copy, 1343 **opts, 1344 )
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
Expressioninstance 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.
1346 def returning( 1347 self, 1348 expression: ExpOrStr, 1349 dialect: DialectType = None, 1350 copy: bool = True, 1351 **opts, 1352 ) -> Delete: 1353 """ 1354 Set the RETURNING expression. Not supported by all dialects. 1355 1356 Example: 1357 >>> delete("tbl").returning("*", dialect="postgres").sql() 1358 'DELETE FROM tbl RETURNING *' 1359 1360 Args: 1361 expression: the SQL code strings to parse. 1362 If an `Expression` instance is passed, it will be used as-is. 1363 dialect: the dialect used to parse the input expressions. 1364 copy: if `False`, modify this expression instance in-place. 1365 opts: other options to use to parse the input expressions. 1366 1367 Returns: 1368 Delete: the modified expression. 1369 """ 1370 return _apply_builder( 1371 expression=expression, 1372 instance=self, 1373 arg="returning", 1374 prefix="RETURNING", 1375 dialect=dialect, 1376 copy=copy, 1377 into=Returning, 1378 **opts, 1379 )
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
Expressioninstance 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.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1382class Drop(Expression): 1383 arg_types = { 1384 "this": False, 1385 "kind": False, 1386 "exists": False, 1387 "temporary": False, 1388 "materialized": False, 1389 "cascade": False, 1390 "constraints": False, 1391 "purge": False, 1392 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1403class Directory(Expression): 1404 # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html 1405 arg_types = {"this": True, "local": False, "row_format": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1408class ForeignKey(Expression): 1409 arg_types = { 1410 "expressions": True, 1411 "reference": False, 1412 "delete": False, 1413 "update": False, 1414 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1431class From(Expression): 1432 @property 1433 def name(self) -> str: 1434 return self.this.name 1435 1436 @property 1437 def alias_or_name(self) -> str: 1438 return self.this.alias_or_name
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1453class Identifier(Expression): 1454 arg_types = {"this": True, "quoted": False} 1455 1456 @property 1457 def quoted(self): 1458 return bool(self.args.get("quoted")) 1459 1460 @property 1461 def hashable_args(self) -> t.Any: 1462 if self.quoted and any(char.isupper() for char in self.this): 1463 return (self.this, self.quoted) 1464 return self.this.lower() 1465 1466 @property 1467 def output_name(self): 1468 return self.name
Name of the output column if this expression is a selection.
If the Expression has no output name, an empty string is returned.
Example:
>>> from sqlglot import parse_one >>> parse_one("SELECT a").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1471class Index(Expression): 1472 arg_types = { 1473 "this": False, 1474 "table": False, 1475 "where": False, 1476 "columns": False, 1477 "unique": False, 1478 "primary": False, 1479 "amp": False, # teradata 1480 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1483class Insert(Expression): 1484 arg_types = { 1485 "with": False, 1486 "this": True, 1487 "expression": False, 1488 "conflict": False, 1489 "returning": False, 1490 "overwrite": False, 1491 "exists": False, 1492 "partition": False, 1493 "alternative": False, 1494 } 1495 1496 def with_( 1497 self, 1498 alias: ExpOrStr, 1499 as_: ExpOrStr, 1500 recursive: t.Optional[bool] = None, 1501 append: bool = True, 1502 dialect: DialectType = None, 1503 copy: bool = True, 1504 **opts, 1505 ) -> Insert: 1506 """ 1507 Append to or set the common table expressions. 1508 1509 Example: 1510 >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql() 1511 'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte' 1512 1513 Args: 1514 alias: the SQL code string to parse as the table name. 1515 If an `Expression` instance is passed, this is used as-is. 1516 as_: the SQL code string to parse as the table expression. 1517 If an `Expression` instance is passed, it will be used as-is. 1518 recursive: set the RECURSIVE part of the expression. Defaults to `False`. 1519 append: if `True`, add to any existing expressions. 1520 Otherwise, this resets the expressions. 1521 dialect: the dialect used to parse the input expression. 1522 copy: if `False`, modify this expression instance in-place. 1523 opts: other options to use to parse the input expressions. 1524 1525 Returns: 1526 The modified expression. 1527 """ 1528 return _apply_cte_builder( 1529 self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts 1530 )
1496 def with_( 1497 self, 1498 alias: ExpOrStr, 1499 as_: ExpOrStr, 1500 recursive: t.Optional[bool] = None, 1501 append: bool = True, 1502 dialect: DialectType = None, 1503 copy: bool = True, 1504 **opts, 1505 ) -> Insert: 1506 """ 1507 Append to or set the common table expressions. 1508 1509 Example: 1510 >>> insert("SELECT x FROM cte", "t").with_("cte", as_="SELECT * FROM tbl").sql() 1511 'WITH cte AS (SELECT * FROM tbl) INSERT INTO t SELECT x FROM cte' 1512 1513 Args: 1514 alias: the SQL code string to parse as the table name. 1515 If an `Expression` instance is passed, this is used as-is. 1516 as_: the SQL code string to parse as the table expression. 1517 If an `Expression` instance is passed, it will be used as-is. 1518 recursive: set the RECURSIVE part of the expression. Defaults to `False`. 1519 append: if `True`, add to any existing expressions. 1520 Otherwise, this resets the expressions. 1521 dialect: the dialect used to parse the input expression. 1522 copy: if `False`, modify this expression instance in-place. 1523 opts: other options to use to parse the input expressions. 1524 1525 Returns: 1526 The modified expression. 1527 """ 1528 return _apply_cte_builder( 1529 self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts 1530 )
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
Expressioninstance is passed, this is used as-is. - as_: the SQL code string to parse as the table expression.
If an
Expressioninstance 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.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1533class OnConflict(Expression): 1534 arg_types = { 1535 "duplicate": False, 1536 "expressions": False, 1537 "nothing": False, 1538 "key": False, 1539 "constraint": False, 1540 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1557class LoadData(Expression): 1558 arg_types = { 1559 "this": True, 1560 "local": False, 1561 "overwrite": False, 1562 "inpath": True, 1563 "partition": False, 1564 "input_format": False, 1565 "serde": False, 1566 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1573class Fetch(Expression): 1574 arg_types = { 1575 "direction": False, 1576 "count": False, 1577 "percent": False, 1578 "with_ties": False, 1579 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1582class Group(Expression): 1583 arg_types = { 1584 "expressions": False, 1585 "grouping_sets": False, 1586 "cube": False, 1587 "rollup": False, 1588 "totals": False, 1589 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1600class Literal(Condition): 1601 arg_types = {"this": True, "is_string": True} 1602 1603 @property 1604 def hashable_args(self) -> t.Any: 1605 return (self.this, self.args.get("is_string")) 1606 1607 @classmethod 1608 def number(cls, number) -> Literal: 1609 return cls(this=str(number), is_string=False) 1610 1611 @classmethod 1612 def string(cls, string) -> Literal: 1613 return cls(this=str(string), is_string=True) 1614 1615 @property 1616 def output_name(self): 1617 return self.name
Name of the output column if this expression is a selection.
If the Expression has no output name, an empty string is returned.
Example:
>>> from sqlglot import parse_one >>> parse_one("SELECT a").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1620class Join(Expression): 1621 arg_types = { 1622 "this": True, 1623 "on": False, 1624 "side": False, 1625 "kind": False, 1626 "using": False, 1627 "natural": False, 1628 "global": False, 1629 "hint": False, 1630 } 1631 1632 @property 1633 def kind(self): 1634 return self.text("kind").upper() 1635 1636 @property 1637 def side(self): 1638 return self.text("side").upper() 1639 1640 @property 1641 def hint(self): 1642 return self.text("hint").upper() 1643 1644 @property 1645 def alias_or_name(self): 1646 return self.this.alias_or_name 1647 1648 def on(self, *expressions, append=True, dialect=None, copy=True, **opts): 1649 """ 1650 Append to or set the ON expressions. 1651 1652 Example: 1653 >>> import sqlglot 1654 >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql() 1655 'JOIN x ON y = 1' 1656 1657 Args: 1658 *expressions (str | Expression): the SQL code strings to parse. 1659 If an `Expression` instance is passed, it will be used as-is. 1660 Multiple expressions are combined with an AND operator. 1661 append (bool): if `True`, AND the new expressions to any existing expression. 1662 Otherwise, this resets the expression. 1663 dialect (str): the dialect used to parse the input expressions. 1664 copy (bool): if `False`, modify this expression instance in-place. 1665 opts (kwargs): other options to use to parse the input expressions. 1666 1667 Returns: 1668 Join: the modified join expression. 1669 """ 1670 join = _apply_conjunction_builder( 1671 *expressions, 1672 instance=self, 1673 arg="on", 1674 append=append, 1675 dialect=dialect, 1676 copy=copy, 1677 **opts, 1678 ) 1679 1680 if join.kind == "CROSS": 1681 join.set("kind", None) 1682 1683 return join 1684 1685 def using(self, *expressions, append=True, dialect=None, copy=True, **opts): 1686 """ 1687 Append to or set the USING expressions. 1688 1689 Example: 1690 >>> import sqlglot 1691 >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 1692 'JOIN x USING (foo, bla)' 1693 1694 Args: 1695 *expressions (str | Expression): the SQL code strings to parse. 1696 If an `Expression` instance is passed, it will be used as-is. 1697 append (bool): if `True`, concatenate the new expressions to the existing "using" list. 1698 Otherwise, this resets the expression. 1699 dialect (str): the dialect used to parse the input expressions. 1700 copy (bool): if `False`, modify this expression instance in-place. 1701 opts (kwargs): other options to use to parse the input expressions. 1702 1703 Returns: 1704 Join: the modified join expression. 1705 """ 1706 join = _apply_list_builder( 1707 *expressions, 1708 instance=self, 1709 arg="using", 1710 append=append, 1711 dialect=dialect, 1712 copy=copy, 1713 **opts, 1714 ) 1715 1716 if join.kind == "CROSS": 1717 join.set("kind", None) 1718 1719 return join
1648 def on(self, *expressions, append=True, dialect=None, copy=True, **opts): 1649 """ 1650 Append to or set the ON expressions. 1651 1652 Example: 1653 >>> import sqlglot 1654 >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql() 1655 'JOIN x ON y = 1' 1656 1657 Args: 1658 *expressions (str | Expression): the SQL code strings to parse. 1659 If an `Expression` instance is passed, it will be used as-is. 1660 Multiple expressions are combined with an AND operator. 1661 append (bool): if `True`, AND the new expressions to any existing expression. 1662 Otherwise, this resets the expression. 1663 dialect (str): the dialect used to parse the input expressions. 1664 copy (bool): if `False`, modify this expression instance in-place. 1665 opts (kwargs): other options to use to parse the input expressions. 1666 1667 Returns: 1668 Join: the modified join expression. 1669 """ 1670 join = _apply_conjunction_builder( 1671 *expressions, 1672 instance=self, 1673 arg="on", 1674 append=append, 1675 dialect=dialect, 1676 copy=copy, 1677 **opts, 1678 ) 1679 1680 if join.kind == "CROSS": 1681 join.set("kind", None) 1682 1683 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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. Multiple expressions are combined with an AND operator. - append (bool): if
True, AND the new expressions to any existing expression. Otherwise, this resets the expression. - dialect (str): the dialect used to parse the input expressions.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Join: the modified join expression.
1685 def using(self, *expressions, append=True, dialect=None, copy=True, **opts): 1686 """ 1687 Append to or set the USING expressions. 1688 1689 Example: 1690 >>> import sqlglot 1691 >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 1692 'JOIN x USING (foo, bla)' 1693 1694 Args: 1695 *expressions (str | Expression): the SQL code strings to parse. 1696 If an `Expression` instance is passed, it will be used as-is. 1697 append (bool): if `True`, concatenate the new expressions to the existing "using" list. 1698 Otherwise, this resets the expression. 1699 dialect (str): the dialect used to parse the input expressions. 1700 copy (bool): if `False`, modify this expression instance in-place. 1701 opts (kwargs): other options to use to parse the input expressions. 1702 1703 Returns: 1704 Join: the modified join expression. 1705 """ 1706 join = _apply_list_builder( 1707 *expressions, 1708 instance=self, 1709 arg="using", 1710 append=append, 1711 dialect=dialect, 1712 copy=copy, 1713 **opts, 1714 ) 1715 1716 if join.kind == "CROSS": 1717 join.set("kind", None) 1718 1719 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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. - append (bool): if
True, concatenate the new expressions to the existing "using" list. Otherwise, this resets the expression. - dialect (str): the dialect used to parse the input expressions.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Join: the modified join expression.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1722class Lateral(UDTF): 1723 arg_types = {"this": True, "view": False, "outer": False, "alias": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1726class MatchRecognize(Expression): 1727 arg_types = { 1728 "partition_by": False, 1729 "order": False, 1730 "measures": False, 1731 "rows": False, 1732 "after": False, 1733 "pattern": False, 1734 "define": False, 1735 "alias": False, 1736 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1775class AfterJournalProperty(Property): 1776 arg_types = {"no": True, "dual": False, "local": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1787class BlockCompressionProperty(Property): 1788 arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1803class DataBlocksizeProperty(Property): 1804 arg_types = {"size": False, "units": False, "min": False, "default": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1843class InputOutputFormat(Expression): 1844 arg_types = {"input_format": False, "output_format": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1847class IsolatedLoadingProperty(Property): 1848 arg_types = { 1849 "no": True, 1850 "concurrent": True, 1851 "for_all": True, 1852 "for_insert": True, 1853 "for_none": True, 1854 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1873class LockingProperty(Property): 1874 arg_types = { 1875 "this": False, 1876 "kind": True, 1877 "for_or_in": True, 1878 "lock_type": True, 1879 "override": False, 1880 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1891class MergeBlockRatioProperty(Property): 1892 arg_types = {"this": False, "no": False, "default": False, "percent": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1907class ReturnsProperty(Property): 1908 arg_types = {"this": True, "is_table": False, "table": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1915class RowFormatDelimitedProperty(Property): 1916 # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml 1917 arg_types = { 1918 "fields": False, 1919 "escaped": False, 1920 "collection_items": False, 1921 "map_keys": False, 1922 "lines": False, 1923 "null": False, 1924 "serde": False, 1925 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
1984class Properties(Expression): 1985 arg_types = {"expressions": True} 1986 1987 NAME_TO_PROPERTY = { 1988 "ALGORITHM": AlgorithmProperty, 1989 "AUTO_INCREMENT": AutoIncrementProperty, 1990 "CHARACTER SET": CharacterSetProperty, 1991 "COLLATE": CollateProperty, 1992 "COMMENT": SchemaCommentProperty, 1993 "DEFINER": DefinerProperty, 1994 "DISTKEY": DistKeyProperty, 1995 "DISTSTYLE": DistStyleProperty, 1996 "ENGINE": EngineProperty, 1997 "EXECUTE AS": ExecuteAsProperty, 1998 "FORMAT": FileFormatProperty, 1999 "LANGUAGE": LanguageProperty, 2000 "LOCATION": LocationProperty, 2001 "PARTITIONED_BY": PartitionedByProperty, 2002 "RETURNS": ReturnsProperty, 2003 "ROW_FORMAT": RowFormatProperty, 2004 "SORTKEY": SortKeyProperty, 2005 "TABLE_FORMAT": TableFormatProperty, 2006 } 2007 2008 PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()} 2009 2010 # CREATE property locations 2011 # Form: schema specified 2012 # create [POST_CREATE] 2013 # table a [POST_NAME] 2014 # (b int) [POST_SCHEMA] 2015 # with ([POST_WITH]) 2016 # index (b) [POST_INDEX] 2017 # 2018 # Form: alias selection 2019 # create [POST_CREATE] 2020 # table a [POST_NAME] 2021 # as [POST_ALIAS] (select * from b) [POST_EXPRESSION] 2022 # index (c) [POST_INDEX] 2023 class Location(AutoName): 2024 POST_CREATE = auto() 2025 POST_NAME = auto() 2026 POST_SCHEMA = auto() 2027 POST_WITH = auto() 2028 POST_ALIAS = auto() 2029 POST_EXPRESSION = auto() 2030 POST_INDEX = auto() 2031 UNSUPPORTED = auto() 2032 2033 @classmethod 2034 def from_dict(cls, properties_dict) -> Properties: 2035 expressions = [] 2036 for key, value in properties_dict.items(): 2037 property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) 2038 if property_cls: 2039 expressions.append(property_cls(this=convert(value))) 2040 else: 2041 expressions.append(Property(this=Literal.string(key), value=convert(value))) 2042 2043 return cls(expressions=expressions)
2033 @classmethod 2034 def from_dict(cls, properties_dict) -> Properties: 2035 expressions = [] 2036 for key, value in properties_dict.items(): 2037 property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) 2038 if property_cls: 2039 expressions.append(property_cls(this=convert(value))) 2040 else: 2041 expressions.append(Property(this=Literal.string(key), value=convert(value))) 2042 2043 return cls(expressions=expressions)
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2023 class Location(AutoName): 2024 POST_CREATE = auto() 2025 POST_NAME = auto() 2026 POST_SCHEMA = auto() 2027 POST_WITH = auto() 2028 POST_ALIAS = auto() 2029 POST_EXPRESSION = auto() 2030 POST_INDEX = auto() 2031 UNSUPPORTED = auto()
An enumeration.
Inherited Members
- enum.Enum
- name
- value
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2055class Reference(Expression): 2056 arg_types = {"this": True, "expressions": False, "options": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2059class Tuple(Expression): 2060 arg_types = {"expressions": False} 2061 2062 def isin( 2063 self, *expressions: t.Any, query: t.Optional[ExpOrStr] = None, copy=True, **opts 2064 ) -> In: 2065 return In( 2066 this=_maybe_copy(self, copy), 2067 expressions=[convert(e, copy=copy) for e in expressions], 2068 query=maybe_parse(query, copy=copy, **opts) if query else None, 2069 )
2062 def isin( 2063 self, *expressions: t.Any, query: t.Optional[ExpOrStr] = None, copy=True, **opts 2064 ) -> In: 2065 return In( 2066 this=_maybe_copy(self, copy), 2067 expressions=[convert(e, copy=copy) for e in expressions], 2068 query=maybe_parse(query, copy=copy, **opts) if query else None, 2069 )
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2072class Subqueryable(Unionable): 2073 def subquery(self, alias=None, copy=True) -> Subquery: 2074 """ 2075 Convert this expression to an aliased expression that can be used as a Subquery. 2076 2077 Example: 2078 >>> subquery = Select().select("x").from_("tbl").subquery() 2079 >>> Select().select("x").from_(subquery).sql() 2080 'SELECT x FROM (SELECT x FROM tbl)' 2081 2082 Args: 2083 alias (str | Identifier): an optional alias for the subquery 2084 copy (bool): if `False`, modify this expression instance in-place. 2085 2086 Returns: 2087 Alias: the subquery 2088 """ 2089 instance = _maybe_copy(self, copy) 2090 if not isinstance(alias, Expression): 2091 alias = TableAlias(this=to_identifier(alias)) if alias else None 2092 2093 return Subquery(this=instance, alias=alias) 2094 2095 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2096 raise NotImplementedError 2097 2098 @property 2099 def ctes(self): 2100 with_ = self.args.get("with") 2101 if not with_: 2102 return [] 2103 return with_.expressions 2104 2105 @property 2106 def selects(self): 2107 raise NotImplementedError("Subqueryable objects must implement `selects`") 2108 2109 @property 2110 def named_selects(self): 2111 raise NotImplementedError("Subqueryable objects must implement `named_selects`") 2112 2113 def with_( 2114 self, 2115 alias: ExpOrStr, 2116 as_: ExpOrStr, 2117 recursive: t.Optional[bool] = None, 2118 append: bool = True, 2119 dialect: DialectType = None, 2120 copy: bool = True, 2121 **opts, 2122 ) -> Subqueryable: 2123 """ 2124 Append to or set the common table expressions. 2125 2126 Example: 2127 >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql() 2128 'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2' 2129 2130 Args: 2131 alias: the SQL code string to parse as the table name. 2132 If an `Expression` instance is passed, this is used as-is. 2133 as_: the SQL code string to parse as the table expression. 2134 If an `Expression` instance is passed, it will be used as-is. 2135 recursive: set the RECURSIVE part of the expression. Defaults to `False`. 2136 append: if `True`, add to any existing expressions. 2137 Otherwise, this resets the expressions. 2138 dialect: the dialect used to parse the input expression. 2139 copy: if `False`, modify this expression instance in-place. 2140 opts: other options to use to parse the input expressions. 2141 2142 Returns: 2143 The modified expression. 2144 """ 2145 return _apply_cte_builder( 2146 self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts 2147 )
2073 def subquery(self, alias=None, copy=True) -> Subquery: 2074 """ 2075 Convert this expression to an aliased expression that can be used as a Subquery. 2076 2077 Example: 2078 >>> subquery = Select().select("x").from_("tbl").subquery() 2079 >>> Select().select("x").from_(subquery).sql() 2080 'SELECT x FROM (SELECT x FROM tbl)' 2081 2082 Args: 2083 alias (str | Identifier): an optional alias for the subquery 2084 copy (bool): if `False`, modify this expression instance in-place. 2085 2086 Returns: 2087 Alias: the subquery 2088 """ 2089 instance = _maybe_copy(self, copy) 2090 if not isinstance(alias, Expression): 2091 alias = TableAlias(this=to_identifier(alias)) if alias else None 2092 2093 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
2113 def with_( 2114 self, 2115 alias: ExpOrStr, 2116 as_: ExpOrStr, 2117 recursive: t.Optional[bool] = None, 2118 append: bool = True, 2119 dialect: DialectType = None, 2120 copy: bool = True, 2121 **opts, 2122 ) -> Subqueryable: 2123 """ 2124 Append to or set the common table expressions. 2125 2126 Example: 2127 >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql() 2128 'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2' 2129 2130 Args: 2131 alias: the SQL code string to parse as the table name. 2132 If an `Expression` instance is passed, this is used as-is. 2133 as_: the SQL code string to parse as the table expression. 2134 If an `Expression` instance is passed, it will be used as-is. 2135 recursive: set the RECURSIVE part of the expression. Defaults to `False`. 2136 append: if `True`, add to any existing expressions. 2137 Otherwise, this resets the expressions. 2138 dialect: the dialect used to parse the input expression. 2139 copy: if `False`, modify this expression instance in-place. 2140 opts: other options to use to parse the input expressions. 2141 2142 Returns: 2143 The modified expression. 2144 """ 2145 return _apply_cte_builder( 2146 self, alias, as_, recursive=recursive, append=append, dialect=dialect, copy=copy, **opts 2147 )
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
Expressioninstance is passed, this is used as-is. - as_: the SQL code string to parse as the table expression.
If an
Expressioninstance 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.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2173class Table(Expression): 2174 arg_types = { 2175 "this": True, 2176 "alias": False, 2177 "db": False, 2178 "catalog": False, 2179 "laterals": False, 2180 "joins": False, 2181 "pivots": False, 2182 "hints": False, 2183 "system_time": False, 2184 } 2185 2186 @property 2187 def db(self) -> str: 2188 return self.text("db") 2189 2190 @property 2191 def catalog(self) -> str: 2192 return self.text("catalog") 2193 2194 @property 2195 def parts(self) -> t.List[Identifier]: 2196 """Return the parts of a column in order catalog, db, table.""" 2197 return [ 2198 t.cast(Identifier, self.args[part]) 2199 for part in ("catalog", "db", "this") 2200 if self.args.get(part) 2201 ]
Return the parts of a column in order catalog, db, table.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2205class SystemTime(Expression): 2206 arg_types = { 2207 "this": False, 2208 "expression": False, 2209 "kind": True, 2210 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2213class Union(Subqueryable): 2214 arg_types = { 2215 "with": False, 2216 "this": True, 2217 "expression": True, 2218 "distinct": False, 2219 **QUERY_MODIFIERS, 2220 } 2221 2222 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2223 """ 2224 Set the LIMIT expression. 2225 2226 Example: 2227 >>> select("1").union(select("1")).limit(1).sql() 2228 'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1' 2229 2230 Args: 2231 expression (str | int | Expression): the SQL code string to parse. 2232 This can also be an integer. 2233 If a `Limit` instance is passed, this is used as-is. 2234 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2235 dialect (str): the dialect used to parse the input expression. 2236 copy (bool): if `False`, modify this expression instance in-place. 2237 opts (kwargs): other options to use to parse the input expressions. 2238 2239 Returns: 2240 Select: The limited subqueryable. 2241 """ 2242 return ( 2243 select("*") 2244 .from_(self.subquery(alias="_l_0", copy=copy)) 2245 .limit(expression, dialect=dialect, copy=False, **opts) 2246 ) 2247 2248 def select( 2249 self, 2250 *expressions: ExpOrStr, 2251 append: bool = True, 2252 dialect: DialectType = None, 2253 copy: bool = True, 2254 **opts, 2255 ) -> Union: 2256 """Append to or set the SELECT of the union recursively. 2257 2258 Example: 2259 >>> from sqlglot import parse_one 2260 >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 2261 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z' 2262 2263 Args: 2264 *expressions: the SQL code strings to parse. 2265 If an `Expression` instance is passed, it will be used as-is. 2266 append: if `True`, add to any existing expressions. 2267 Otherwise, this resets the expressions. 2268 dialect: the dialect used to parse the input expressions. 2269 copy: if `False`, modify this expression instance in-place. 2270 opts: other options to use to parse the input expressions. 2271 2272 Returns: 2273 Union: the modified expression. 2274 """ 2275 this = self.copy() if copy else self 2276 this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts) 2277 this.expression.unnest().select( 2278 *expressions, append=append, dialect=dialect, copy=False, **opts 2279 ) 2280 return this 2281 2282 @property 2283 def named_selects(self): 2284 return self.this.unnest().named_selects 2285 2286 @property 2287 def is_star(self) -> bool: 2288 return self.this.is_star or self.expression.is_star 2289 2290 @property 2291 def selects(self): 2292 return self.this.unnest().selects 2293 2294 @property 2295 def left(self): 2296 return self.this 2297 2298 @property 2299 def right(self): 2300 return self.expression
2222 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2223 """ 2224 Set the LIMIT expression. 2225 2226 Example: 2227 >>> select("1").union(select("1")).limit(1).sql() 2228 'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1' 2229 2230 Args: 2231 expression (str | int | Expression): the SQL code string to parse. 2232 This can also be an integer. 2233 If a `Limit` instance is passed, this is used as-is. 2234 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2235 dialect (str): the dialect used to parse the input expression. 2236 copy (bool): if `False`, modify this expression instance in-place. 2237 opts (kwargs): other options to use to parse the input expressions. 2238 2239 Returns: 2240 Select: The limited subqueryable. 2241 """ 2242 return ( 2243 select("*") 2244 .from_(self.subquery(alias="_l_0", copy=copy)) 2245 .limit(expression, dialect=dialect, copy=False, **opts) 2246 )
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 (str | int | Expression): the SQL code string to parse.
This can also be an integer.
If a
Limitinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aLimit. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: The limited subqueryable.
2248 def select( 2249 self, 2250 *expressions: ExpOrStr, 2251 append: bool = True, 2252 dialect: DialectType = None, 2253 copy: bool = True, 2254 **opts, 2255 ) -> Union: 2256 """Append to or set the SELECT of the union recursively. 2257 2258 Example: 2259 >>> from sqlglot import parse_one 2260 >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 2261 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z' 2262 2263 Args: 2264 *expressions: the SQL code strings to parse. 2265 If an `Expression` instance is passed, it will be used as-is. 2266 append: if `True`, add to any existing expressions. 2267 Otherwise, this resets the expressions. 2268 dialect: the dialect used to parse the input expressions. 2269 copy: if `False`, modify this expression instance in-place. 2270 opts: other options to use to parse the input expressions. 2271 2272 Returns: 2273 Union: the modified expression. 2274 """ 2275 this = self.copy() if copy else self 2276 this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts) 2277 this.expression.unnest().select( 2278 *expressions, append=append, dialect=dialect, copy=False, **opts 2279 ) 2280 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
Expressioninstance 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.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2311class Unnest(UDTF): 2312 arg_types = { 2313 "expressions": True, 2314 "ordinality": False, 2315 "alias": False, 2316 "offset": False, 2317 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2320class Update(Expression): 2321 arg_types = { 2322 "with": False, 2323 "this": False, 2324 "expressions": True, 2325 "from": False, 2326 "where": False, 2327 "returning": False, 2328 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2331class Values(UDTF): 2332 arg_types = { 2333 "expressions": True, 2334 "ordinality": False, 2335 "alias": False, 2336 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2353class Select(Subqueryable): 2354 arg_types = { 2355 "with": False, 2356 "kind": False, 2357 "expressions": False, 2358 "hint": False, 2359 "distinct": False, 2360 "struct": False, # https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#return_query_results_as_a_value_table 2361 "value": False, 2362 "into": False, 2363 "from": False, 2364 **QUERY_MODIFIERS, 2365 } 2366 2367 def from_( 2368 self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts 2369 ) -> Select: 2370 """ 2371 Set the FROM expression. 2372 2373 Example: 2374 >>> Select().from_("tbl").select("x").sql() 2375 'SELECT x FROM tbl' 2376 2377 Args: 2378 expression : the SQL code strings to parse. 2379 If a `From` instance is passed, this is used as-is. 2380 If another `Expression` instance is passed, it will be wrapped in a `From`. 2381 dialect: the dialect used to parse the input expression. 2382 copy: if `False`, modify this expression instance in-place. 2383 opts: other options to use to parse the input expressions. 2384 2385 Returns: 2386 Select: the modified expression. 2387 """ 2388 return _apply_builder( 2389 expression=expression, 2390 instance=self, 2391 arg="from", 2392 into=From, 2393 prefix="FROM", 2394 dialect=dialect, 2395 copy=copy, 2396 **opts, 2397 ) 2398 2399 def group_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2400 """ 2401 Set the GROUP BY expression. 2402 2403 Example: 2404 >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql() 2405 'SELECT x, COUNT(1) FROM tbl GROUP BY x' 2406 2407 Args: 2408 *expressions (str | Expression): the SQL code strings to parse. 2409 If a `Group` instance is passed, this is used as-is. 2410 If another `Expression` instance is passed, it will be wrapped in a `Group`. 2411 If nothing is passed in then a group by is not applied to the expression 2412 append (bool): if `True`, add to any existing expressions. 2413 Otherwise, this flattens all the `Group` expression into a single expression. 2414 dialect (str): the dialect used to parse the input expression. 2415 copy (bool): if `False`, modify this expression instance in-place. 2416 opts (kwargs): other options to use to parse the input expressions. 2417 2418 Returns: 2419 Select: the modified expression. 2420 """ 2421 if not expressions: 2422 return self if not copy else self.copy() 2423 return _apply_child_list_builder( 2424 *expressions, 2425 instance=self, 2426 arg="group", 2427 append=append, 2428 copy=copy, 2429 prefix="GROUP BY", 2430 into=Group, 2431 dialect=dialect, 2432 **opts, 2433 ) 2434 2435 def order_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2436 """ 2437 Set the ORDER BY expression. 2438 2439 Example: 2440 >>> Select().from_("tbl").select("x").order_by("x DESC").sql() 2441 'SELECT x FROM tbl ORDER BY x DESC' 2442 2443 Args: 2444 *expressions (str | Expression): the SQL code strings to parse. 2445 If a `Group` instance is passed, this is used as-is. 2446 If another `Expression` instance is passed, it will be wrapped in a `Order`. 2447 append (bool): if `True`, add to any existing expressions. 2448 Otherwise, this flattens all the `Order` expression into a single expression. 2449 dialect (str): the dialect used to parse the input expression. 2450 copy (bool): if `False`, modify this expression instance in-place. 2451 opts (kwargs): other options to use to parse the input expressions. 2452 2453 Returns: 2454 Select: the modified expression. 2455 """ 2456 return _apply_child_list_builder( 2457 *expressions, 2458 instance=self, 2459 arg="order", 2460 append=append, 2461 copy=copy, 2462 prefix="ORDER BY", 2463 into=Order, 2464 dialect=dialect, 2465 **opts, 2466 ) 2467 2468 def sort_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2469 """ 2470 Set the SORT BY expression. 2471 2472 Example: 2473 >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive") 2474 'SELECT x FROM tbl SORT BY x DESC' 2475 2476 Args: 2477 *expressions (str | Expression): the SQL code strings to parse. 2478 If a `Group` instance is passed, this is used as-is. 2479 If another `Expression` instance is passed, it will be wrapped in a `SORT`. 2480 append (bool): if `True`, add to any existing expressions. 2481 Otherwise, this flattens all the `Order` expression into a single expression. 2482 dialect (str): the dialect used to parse the input expression. 2483 copy (bool): if `False`, modify this expression instance in-place. 2484 opts (kwargs): other options to use to parse the input expressions. 2485 2486 Returns: 2487 Select: the modified expression. 2488 """ 2489 return _apply_child_list_builder( 2490 *expressions, 2491 instance=self, 2492 arg="sort", 2493 append=append, 2494 copy=copy, 2495 prefix="SORT BY", 2496 into=Sort, 2497 dialect=dialect, 2498 **opts, 2499 ) 2500 2501 def cluster_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2502 """ 2503 Set the CLUSTER BY expression. 2504 2505 Example: 2506 >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive") 2507 'SELECT x FROM tbl CLUSTER BY x DESC' 2508 2509 Args: 2510 *expressions (str | Expression): the SQL code strings to parse. 2511 If a `Group` instance is passed, this is used as-is. 2512 If another `Expression` instance is passed, it will be wrapped in a `Cluster`. 2513 append (bool): if `True`, add to any existing expressions. 2514 Otherwise, this flattens all the `Order` expression into a single expression. 2515 dialect (str): the dialect used to parse the input expression. 2516 copy (bool): if `False`, modify this expression instance in-place. 2517 opts (kwargs): other options to use to parse the input expressions. 2518 2519 Returns: 2520 Select: the modified expression. 2521 """ 2522 return _apply_child_list_builder( 2523 *expressions, 2524 instance=self, 2525 arg="cluster", 2526 append=append, 2527 copy=copy, 2528 prefix="CLUSTER BY", 2529 into=Cluster, 2530 dialect=dialect, 2531 **opts, 2532 ) 2533 2534 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2535 """ 2536 Set the LIMIT expression. 2537 2538 Example: 2539 >>> Select().from_("tbl").select("x").limit(10).sql() 2540 'SELECT x FROM tbl LIMIT 10' 2541 2542 Args: 2543 expression (str | int | Expression): the SQL code string to parse. 2544 This can also be an integer. 2545 If a `Limit` instance is passed, this is used as-is. 2546 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2547 dialect (str): the dialect used to parse the input expression. 2548 copy (bool): if `False`, modify this expression instance in-place. 2549 opts (kwargs): other options to use to parse the input expressions. 2550 2551 Returns: 2552 Select: the modified expression. 2553 """ 2554 return _apply_builder( 2555 expression=expression, 2556 instance=self, 2557 arg="limit", 2558 into=Limit, 2559 prefix="LIMIT", 2560 dialect=dialect, 2561 copy=copy, 2562 **opts, 2563 ) 2564 2565 def offset(self, expression, dialect=None, copy=True, **opts) -> Select: 2566 """ 2567 Set the OFFSET expression. 2568 2569 Example: 2570 >>> Select().from_("tbl").select("x").offset(10).sql() 2571 'SELECT x FROM tbl OFFSET 10' 2572 2573 Args: 2574 expression (str | int | Expression): the SQL code string to parse. 2575 This can also be an integer. 2576 If a `Offset` instance is passed, this is used as-is. 2577 If another `Expression` instance is passed, it will be wrapped in a `Offset`. 2578 dialect (str): the dialect used to parse the input expression. 2579 copy (bool): if `False`, modify this expression instance in-place. 2580 opts (kwargs): other options to use to parse the input expressions. 2581 2582 Returns: 2583 Select: the modified expression. 2584 """ 2585 return _apply_builder( 2586 expression=expression, 2587 instance=self, 2588 arg="offset", 2589 into=Offset, 2590 prefix="OFFSET", 2591 dialect=dialect, 2592 copy=copy, 2593 **opts, 2594 ) 2595 2596 def select( 2597 self, 2598 *expressions: ExpOrStr, 2599 append: bool = True, 2600 dialect: DialectType = None, 2601 copy: bool = True, 2602 **opts, 2603 ) -> Select: 2604 """ 2605 Append to or set the SELECT expressions. 2606 2607 Example: 2608 >>> Select().select("x", "y").sql() 2609 'SELECT x, y' 2610 2611 Args: 2612 *expressions: the SQL code strings to parse. 2613 If an `Expression` instance is passed, it will be used as-is. 2614 append: if `True`, add to any existing expressions. 2615 Otherwise, this resets the expressions. 2616 dialect: the dialect used to parse the input expressions. 2617 copy: if `False`, modify this expression instance in-place. 2618 opts: other options to use to parse the input expressions. 2619 2620 Returns: 2621 Select: the modified expression. 2622 """ 2623 return _apply_list_builder( 2624 *expressions, 2625 instance=self, 2626 arg="expressions", 2627 append=append, 2628 dialect=dialect, 2629 copy=copy, 2630 **opts, 2631 ) 2632 2633 def lateral(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2634 """ 2635 Append to or set the LATERAL expressions. 2636 2637 Example: 2638 >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql() 2639 'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z' 2640 2641 Args: 2642 *expressions (str | Expression): the SQL code strings to parse. 2643 If an `Expression` instance is passed, it will be used as-is. 2644 append (bool): if `True`, add to any existing expressions. 2645 Otherwise, this resets the expressions. 2646 dialect (str): the dialect used to parse the input expressions. 2647 copy (bool): if `False`, modify this expression instance in-place. 2648 opts (kwargs): other options to use to parse the input expressions. 2649 2650 Returns: 2651 Select: the modified expression. 2652 """ 2653 return _apply_list_builder( 2654 *expressions, 2655 instance=self, 2656 arg="laterals", 2657 append=append, 2658 into=Lateral, 2659 prefix="LATERAL VIEW", 2660 dialect=dialect, 2661 copy=copy, 2662 **opts, 2663 ) 2664 2665 def join( 2666 self, 2667 expression, 2668 on=None, 2669 using=None, 2670 append=True, 2671 join_type=None, 2672 join_alias=None, 2673 dialect=None, 2674 copy=True, 2675 **opts, 2676 ) -> Select: 2677 """ 2678 Append to or set the JOIN expressions. 2679 2680 Example: 2681 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 2682 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y' 2683 2684 >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 2685 'SELECT 1 FROM a JOIN b USING (x, y, z)' 2686 2687 Use `join_type` to change the type of join: 2688 2689 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 2690 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y' 2691 2692 Args: 2693 expression (str | Expression): the SQL code string to parse. 2694 If an `Expression` instance is passed, it will be used as-is. 2695 on (str | Expression): optionally specify the join "on" criteria as a SQL string. 2696 If an `Expression` instance is passed, it will be used as-is. 2697 using (str | Expression): optionally specify the join "using" criteria as a SQL string. 2698 If an `Expression` instance is passed, it will be used as-is. 2699 append (bool): if `True`, add to any existing expressions. 2700 Otherwise, this resets the expressions. 2701 join_type (str): If set, alter the parsed join type 2702 dialect (str): the dialect used to parse the input expressions. 2703 copy (bool): if `False`, modify this expression instance in-place. 2704 opts (kwargs): other options to use to parse the input expressions. 2705 2706 Returns: 2707 Select: the modified expression. 2708 """ 2709 parse_args = {"dialect": dialect, **opts} 2710 2711 try: 2712 expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args) 2713 except ParseError: 2714 expression = maybe_parse(expression, into=(Join, Expression), **parse_args) 2715 2716 join = expression if isinstance(expression, Join) else Join(this=expression) 2717 2718 if isinstance(join.this, Select): 2719 join.this.replace(join.this.subquery()) 2720 2721 if join_type: 2722 natural: t.Optional[Token] 2723 side: t.Optional[Token] 2724 kind: t.Optional[Token] 2725 2726 natural, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args) # type: ignore 2727 2728 if natural: 2729 join.set("natural", True) 2730 if side: 2731 join.set("side", side.text) 2732 if kind: 2733 join.set("kind", kind.text) 2734 2735 if on: 2736 on = and_(*ensure_collection(on), dialect=dialect, copy=copy, **opts) 2737 join.set("on", on) 2738 2739 if using: 2740 join = _apply_list_builder( 2741 *ensure_collection(using), 2742 instance=join, 2743 arg="using", 2744 append=append, 2745 copy=copy, 2746 **opts, 2747 ) 2748 2749 if join_alias: 2750 join.set("this", alias_(join.this, join_alias, table=True)) 2751 return _apply_list_builder( 2752 join, 2753 instance=self, 2754 arg="joins", 2755 append=append, 2756 copy=copy, 2757 **opts, 2758 ) 2759 2760 def where(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2761 """ 2762 Append to or set the WHERE expressions. 2763 2764 Example: 2765 >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql() 2766 "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'" 2767 2768 Args: 2769 *expressions (str | Expression): the SQL code strings to parse. 2770 If an `Expression` instance is passed, it will be used as-is. 2771 Multiple expressions are combined with an AND operator. 2772 append (bool): if `True`, AND the new expressions to any existing expression. 2773 Otherwise, this resets the expression. 2774 dialect (str): the dialect used to parse the input expressions. 2775 copy (bool): if `False`, modify this expression instance in-place. 2776 opts (kwargs): other options to use to parse the input expressions. 2777 2778 Returns: 2779 Select: the modified expression. 2780 """ 2781 return _apply_conjunction_builder( 2782 *expressions, 2783 instance=self, 2784 arg="where", 2785 append=append, 2786 into=Where, 2787 dialect=dialect, 2788 copy=copy, 2789 **opts, 2790 ) 2791 2792 def having(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2793 """ 2794 Append to or set the HAVING expressions. 2795 2796 Example: 2797 >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql() 2798 'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3' 2799 2800 Args: 2801 *expressions (str | Expression): the SQL code strings to parse. 2802 If an `Expression` instance is passed, it will be used as-is. 2803 Multiple expressions are combined with an AND operator. 2804 append (bool): if `True`, AND the new expressions to any existing expression. 2805 Otherwise, this resets the expression. 2806 dialect (str): the dialect used to parse the input expressions. 2807 copy (bool): if `False`, modify this expression instance in-place. 2808 opts (kwargs): other options to use to parse the input expressions. 2809 2810 Returns: 2811 Select: the modified expression. 2812 """ 2813 return _apply_conjunction_builder( 2814 *expressions, 2815 instance=self, 2816 arg="having", 2817 append=append, 2818 into=Having, 2819 dialect=dialect, 2820 copy=copy, 2821 **opts, 2822 ) 2823 2824 def window(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2825 return _apply_list_builder( 2826 *expressions, 2827 instance=self, 2828 arg="windows", 2829 append=append, 2830 into=Window, 2831 dialect=dialect, 2832 copy=copy, 2833 **opts, 2834 ) 2835 2836 def qualify(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2837 return _apply_conjunction_builder( 2838 *expressions, 2839 instance=self, 2840 arg="qualify", 2841 append=append, 2842 into=Qualify, 2843 dialect=dialect, 2844 copy=copy, 2845 **opts, 2846 ) 2847 2848 def distinct(self, *ons: ExpOrStr, distinct: bool = True, copy: bool = True) -> Select: 2849 """ 2850 Set the OFFSET expression. 2851 2852 Example: 2853 >>> Select().from_("tbl").select("x").distinct().sql() 2854 'SELECT DISTINCT x FROM tbl' 2855 2856 Args: 2857 ons: the expressions to distinct on 2858 distinct: whether the Select should be distinct 2859 copy: if `False`, modify this expression instance in-place. 2860 2861 Returns: 2862 Select: the modified expression. 2863 """ 2864 instance = _maybe_copy(self, copy) 2865 on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons]) if ons else None 2866 instance.set("distinct", Distinct(on=on) if distinct else None) 2867 return instance 2868 2869 def ctas(self, table, properties=None, dialect=None, copy=True, **opts) -> Create: 2870 """ 2871 Convert this expression to a CREATE TABLE AS statement. 2872 2873 Example: 2874 >>> Select().select("*").from_("tbl").ctas("x").sql() 2875 'CREATE TABLE x AS SELECT * FROM tbl' 2876 2877 Args: 2878 table (str | Expression): the SQL code string to parse as the table name. 2879 If another `Expression` instance is passed, it will be used as-is. 2880 properties (dict): an optional mapping of table properties 2881 dialect (str): the dialect used to parse the input table. 2882 copy (bool): if `False`, modify this expression instance in-place. 2883 opts (kwargs): other options to use to parse the input table. 2884 2885 Returns: 2886 Create: the CREATE TABLE AS expression 2887 """ 2888 instance = _maybe_copy(self, copy) 2889 table_expression = maybe_parse( 2890 table, 2891 into=Table, 2892 dialect=dialect, 2893 **opts, 2894 ) 2895 properties_expression = None 2896 if properties: 2897 properties_expression = Properties.from_dict(properties) 2898 2899 return Create( 2900 this=table_expression, 2901 kind="table", 2902 expression=instance, 2903 properties=properties_expression, 2904 ) 2905 2906 def lock(self, update: bool = True, copy: bool = True) -> Select: 2907 """ 2908 Set the locking read mode for this expression. 2909 2910 Examples: 2911 >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql") 2912 "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE" 2913 2914 >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql") 2915 "SELECT x FROM tbl WHERE x = 'a' FOR SHARE" 2916 2917 Args: 2918 update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`. 2919 copy: if `False`, modify this expression instance in-place. 2920 2921 Returns: 2922 The modified expression. 2923 """ 2924 2925 inst = _maybe_copy(self, copy) 2926 inst.set("locks", [Lock(update=update)]) 2927 2928 return inst 2929 2930 @property 2931 def named_selects(self) -> t.List[str]: 2932 return [e.output_name for e in self.expressions if e.alias_or_name] 2933 2934 @property 2935 def is_star(self) -> bool: 2936 return any(expression.is_star for expression in self.expressions) 2937 2938 @property 2939 def selects(self) -> t.List[Expression]: 2940 return self.expressions
2367 def from_( 2368 self, expression: ExpOrStr, dialect: DialectType = None, copy: bool = True, **opts 2369 ) -> Select: 2370 """ 2371 Set the FROM expression. 2372 2373 Example: 2374 >>> Select().from_("tbl").select("x").sql() 2375 'SELECT x FROM tbl' 2376 2377 Args: 2378 expression : the SQL code strings to parse. 2379 If a `From` instance is passed, this is used as-is. 2380 If another `Expression` instance is passed, it will be wrapped in a `From`. 2381 dialect: the dialect used to parse the input expression. 2382 copy: if `False`, modify this expression instance in-place. 2383 opts: other options to use to parse the input expressions. 2384 2385 Returns: 2386 Select: the modified expression. 2387 """ 2388 return _apply_builder( 2389 expression=expression, 2390 instance=self, 2391 arg="from", 2392 into=From, 2393 prefix="FROM", 2394 dialect=dialect, 2395 copy=copy, 2396 **opts, 2397 )
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
Frominstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aFrom. - 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.
2399 def group_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2400 """ 2401 Set the GROUP BY expression. 2402 2403 Example: 2404 >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql() 2405 'SELECT x, COUNT(1) FROM tbl GROUP BY x' 2406 2407 Args: 2408 *expressions (str | Expression): the SQL code strings to parse. 2409 If a `Group` instance is passed, this is used as-is. 2410 If another `Expression` instance is passed, it will be wrapped in a `Group`. 2411 If nothing is passed in then a group by is not applied to the expression 2412 append (bool): if `True`, add to any existing expressions. 2413 Otherwise, this flattens all the `Group` expression into a single expression. 2414 dialect (str): the dialect used to parse the input expression. 2415 copy (bool): if `False`, modify this expression instance in-place. 2416 opts (kwargs): other options to use to parse the input expressions. 2417 2418 Returns: 2419 Select: the modified expression. 2420 """ 2421 if not expressions: 2422 return self if not copy else self.copy() 2423 return _apply_child_list_builder( 2424 *expressions, 2425 instance=self, 2426 arg="group", 2427 append=append, 2428 copy=copy, 2429 prefix="GROUP BY", 2430 into=Group, 2431 dialect=dialect, 2432 **opts, 2433 )
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 (str | Expression): the SQL code strings to parse.
If a
Groupinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aGroup. If nothing is passed in then a group by is not applied to the expression - append (bool): if
True, add to any existing expressions. Otherwise, this flattens all theGroupexpression into a single expression. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2435 def order_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2436 """ 2437 Set the ORDER BY expression. 2438 2439 Example: 2440 >>> Select().from_("tbl").select("x").order_by("x DESC").sql() 2441 'SELECT x FROM tbl ORDER BY x DESC' 2442 2443 Args: 2444 *expressions (str | Expression): the SQL code strings to parse. 2445 If a `Group` instance is passed, this is used as-is. 2446 If another `Expression` instance is passed, it will be wrapped in a `Order`. 2447 append (bool): if `True`, add to any existing expressions. 2448 Otherwise, this flattens all the `Order` expression into a single expression. 2449 dialect (str): the dialect used to parse the input expression. 2450 copy (bool): if `False`, modify this expression instance in-place. 2451 opts (kwargs): other options to use to parse the input expressions. 2452 2453 Returns: 2454 Select: the modified expression. 2455 """ 2456 return _apply_child_list_builder( 2457 *expressions, 2458 instance=self, 2459 arg="order", 2460 append=append, 2461 copy=copy, 2462 prefix="ORDER BY", 2463 into=Order, 2464 dialect=dialect, 2465 **opts, 2466 )
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 (str | Expression): the SQL code strings to parse.
If a
Groupinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aOrder. - append (bool): if
True, add to any existing expressions. Otherwise, this flattens all theOrderexpression into a single expression. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2468 def sort_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2469 """ 2470 Set the SORT BY expression. 2471 2472 Example: 2473 >>> Select().from_("tbl").select("x").sort_by("x DESC").sql(dialect="hive") 2474 'SELECT x FROM tbl SORT BY x DESC' 2475 2476 Args: 2477 *expressions (str | Expression): the SQL code strings to parse. 2478 If a `Group` instance is passed, this is used as-is. 2479 If another `Expression` instance is passed, it will be wrapped in a `SORT`. 2480 append (bool): if `True`, add to any existing expressions. 2481 Otherwise, this flattens all the `Order` expression into a single expression. 2482 dialect (str): the dialect used to parse the input expression. 2483 copy (bool): if `False`, modify this expression instance in-place. 2484 opts (kwargs): other options to use to parse the input expressions. 2485 2486 Returns: 2487 Select: the modified expression. 2488 """ 2489 return _apply_child_list_builder( 2490 *expressions, 2491 instance=self, 2492 arg="sort", 2493 append=append, 2494 copy=copy, 2495 prefix="SORT BY", 2496 into=Sort, 2497 dialect=dialect, 2498 **opts, 2499 )
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 (str | Expression): the SQL code strings to parse.
If a
Groupinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aSORT. - append (bool): if
True, add to any existing expressions. Otherwise, this flattens all theOrderexpression into a single expression. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2501 def cluster_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2502 """ 2503 Set the CLUSTER BY expression. 2504 2505 Example: 2506 >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql(dialect="hive") 2507 'SELECT x FROM tbl CLUSTER BY x DESC' 2508 2509 Args: 2510 *expressions (str | Expression): the SQL code strings to parse. 2511 If a `Group` instance is passed, this is used as-is. 2512 If another `Expression` instance is passed, it will be wrapped in a `Cluster`. 2513 append (bool): if `True`, add to any existing expressions. 2514 Otherwise, this flattens all the `Order` expression into a single expression. 2515 dialect (str): the dialect used to parse the input expression. 2516 copy (bool): if `False`, modify this expression instance in-place. 2517 opts (kwargs): other options to use to parse the input expressions. 2518 2519 Returns: 2520 Select: the modified expression. 2521 """ 2522 return _apply_child_list_builder( 2523 *expressions, 2524 instance=self, 2525 arg="cluster", 2526 append=append, 2527 copy=copy, 2528 prefix="CLUSTER BY", 2529 into=Cluster, 2530 dialect=dialect, 2531 **opts, 2532 )
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 (str | Expression): the SQL code strings to parse.
If a
Groupinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aCluster. - append (bool): if
True, add to any existing expressions. Otherwise, this flattens all theOrderexpression into a single expression. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2534 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2535 """ 2536 Set the LIMIT expression. 2537 2538 Example: 2539 >>> Select().from_("tbl").select("x").limit(10).sql() 2540 'SELECT x FROM tbl LIMIT 10' 2541 2542 Args: 2543 expression (str | int | Expression): the SQL code string to parse. 2544 This can also be an integer. 2545 If a `Limit` instance is passed, this is used as-is. 2546 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2547 dialect (str): the dialect used to parse the input expression. 2548 copy (bool): if `False`, modify this expression instance in-place. 2549 opts (kwargs): other options to use to parse the input expressions. 2550 2551 Returns: 2552 Select: the modified expression. 2553 """ 2554 return _apply_builder( 2555 expression=expression, 2556 instance=self, 2557 arg="limit", 2558 into=Limit, 2559 prefix="LIMIT", 2560 dialect=dialect, 2561 copy=copy, 2562 **opts, 2563 )
Set the LIMIT expression.
Example:
>>> Select().from_("tbl").select("x").limit(10).sql() 'SELECT x FROM tbl LIMIT 10'
Arguments:
- expression (str | int | Expression): the SQL code string to parse.
This can also be an integer.
If a
Limitinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aLimit. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2565 def offset(self, expression, dialect=None, copy=True, **opts) -> Select: 2566 """ 2567 Set the OFFSET expression. 2568 2569 Example: 2570 >>> Select().from_("tbl").select("x").offset(10).sql() 2571 'SELECT x FROM tbl OFFSET 10' 2572 2573 Args: 2574 expression (str | int | Expression): the SQL code string to parse. 2575 This can also be an integer. 2576 If a `Offset` instance is passed, this is used as-is. 2577 If another `Expression` instance is passed, it will be wrapped in a `Offset`. 2578 dialect (str): the dialect used to parse the input expression. 2579 copy (bool): if `False`, modify this expression instance in-place. 2580 opts (kwargs): other options to use to parse the input expressions. 2581 2582 Returns: 2583 Select: the modified expression. 2584 """ 2585 return _apply_builder( 2586 expression=expression, 2587 instance=self, 2588 arg="offset", 2589 into=Offset, 2590 prefix="OFFSET", 2591 dialect=dialect, 2592 copy=copy, 2593 **opts, 2594 )
Set the OFFSET expression.
Example:
>>> Select().from_("tbl").select("x").offset(10).sql() 'SELECT x FROM tbl OFFSET 10'
Arguments:
- expression (str | int | Expression): the SQL code string to parse.
This can also be an integer.
If a
Offsetinstance is passed, this is used as-is. If anotherExpressioninstance is passed, it will be wrapped in aOffset. - dialect (str): the dialect used to parse the input expression.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2596 def select( 2597 self, 2598 *expressions: ExpOrStr, 2599 append: bool = True, 2600 dialect: DialectType = None, 2601 copy: bool = True, 2602 **opts, 2603 ) -> Select: 2604 """ 2605 Append to or set the SELECT expressions. 2606 2607 Example: 2608 >>> Select().select("x", "y").sql() 2609 'SELECT x, y' 2610 2611 Args: 2612 *expressions: the SQL code strings to parse. 2613 If an `Expression` instance is passed, it will be used as-is. 2614 append: if `True`, add to any existing expressions. 2615 Otherwise, this resets the expressions. 2616 dialect: the dialect used to parse the input expressions. 2617 copy: if `False`, modify this expression instance in-place. 2618 opts: other options to use to parse the input expressions. 2619 2620 Returns: 2621 Select: the modified expression. 2622 """ 2623 return _apply_list_builder( 2624 *expressions, 2625 instance=self, 2626 arg="expressions", 2627 append=append, 2628 dialect=dialect, 2629 copy=copy, 2630 **opts, 2631 )
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
Expressioninstance 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:
Select: the modified expression.
2633 def lateral(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2634 """ 2635 Append to or set the LATERAL expressions. 2636 2637 Example: 2638 >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql() 2639 'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z' 2640 2641 Args: 2642 *expressions (str | Expression): the SQL code strings to parse. 2643 If an `Expression` instance is passed, it will be used as-is. 2644 append (bool): if `True`, add to any existing expressions. 2645 Otherwise, this resets the expressions. 2646 dialect (str): the dialect used to parse the input expressions. 2647 copy (bool): if `False`, modify this expression instance in-place. 2648 opts (kwargs): other options to use to parse the input expressions. 2649 2650 Returns: 2651 Select: the modified expression. 2652 """ 2653 return _apply_list_builder( 2654 *expressions, 2655 instance=self, 2656 arg="laterals", 2657 append=append, 2658 into=Lateral, 2659 prefix="LATERAL VIEW", 2660 dialect=dialect, 2661 copy=copy, 2662 **opts, 2663 )
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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. - append (bool): if
True, add to any existing expressions. Otherwise, this resets the expressions. - dialect (str): the dialect used to parse the input expressions.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2665 def join( 2666 self, 2667 expression, 2668 on=None, 2669 using=None, 2670 append=True, 2671 join_type=None, 2672 join_alias=None, 2673 dialect=None, 2674 copy=True, 2675 **opts, 2676 ) -> Select: 2677 """ 2678 Append to or set the JOIN expressions. 2679 2680 Example: 2681 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 2682 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y' 2683 2684 >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 2685 'SELECT 1 FROM a JOIN b USING (x, y, z)' 2686 2687 Use `join_type` to change the type of join: 2688 2689 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 2690 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y' 2691 2692 Args: 2693 expression (str | Expression): the SQL code string to parse. 2694 If an `Expression` instance is passed, it will be used as-is. 2695 on (str | Expression): optionally specify the join "on" criteria as a SQL string. 2696 If an `Expression` instance is passed, it will be used as-is. 2697 using (str | Expression): optionally specify the join "using" criteria as a SQL string. 2698 If an `Expression` instance is passed, it will be used as-is. 2699 append (bool): if `True`, add to any existing expressions. 2700 Otherwise, this resets the expressions. 2701 join_type (str): If set, alter the parsed join type 2702 dialect (str): the dialect used to parse the input expressions. 2703 copy (bool): if `False`, modify this expression instance in-place. 2704 opts (kwargs): other options to use to parse the input expressions. 2705 2706 Returns: 2707 Select: the modified expression. 2708 """ 2709 parse_args = {"dialect": dialect, **opts} 2710 2711 try: 2712 expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args) 2713 except ParseError: 2714 expression = maybe_parse(expression, into=(Join, Expression), **parse_args) 2715 2716 join = expression if isinstance(expression, Join) else Join(this=expression) 2717 2718 if isinstance(join.this, Select): 2719 join.this.replace(join.this.subquery()) 2720 2721 if join_type: 2722 natural: t.Optional[Token] 2723 side: t.Optional[Token] 2724 kind: t.Optional[Token] 2725 2726 natural, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args) # type: ignore 2727 2728 if natural: 2729 join.set("natural", True) 2730 if side: 2731 join.set("side", side.text) 2732 if kind: 2733 join.set("kind", kind.text) 2734 2735 if on: 2736 on = and_(*ensure_collection(on), dialect=dialect, copy=copy, **opts) 2737 join.set("on", on) 2738 2739 if using: 2740 join = _apply_list_builder( 2741 *ensure_collection(using), 2742 instance=join, 2743 arg="using", 2744 append=append, 2745 copy=copy, 2746 **opts, 2747 ) 2748 2749 if join_alias: 2750 join.set("this", alias_(join.this, join_alias, table=True)) 2751 return _apply_list_builder( 2752 join, 2753 instance=self, 2754 arg="joins", 2755 append=append, 2756 copy=copy, 2757 **opts, 2758 )
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_typeto 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 (str | Expression): the SQL code string to parse.
If an
Expressioninstance is passed, it will be used as-is. - on (str | Expression): optionally specify the join "on" criteria as a SQL string.
If an
Expressioninstance is passed, it will be used as-is. - using (str | Expression): optionally specify the join "using" criteria as a SQL string.
If an
Expressioninstance is passed, it will be used as-is. - append (bool): if
True, add to any existing expressions. Otherwise, this resets the expressions. - join_type (str): If set, alter the parsed join type
- dialect (str): the dialect used to parse the input expressions.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2760 def where(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2761 """ 2762 Append to or set the WHERE expressions. 2763 2764 Example: 2765 >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql() 2766 "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'" 2767 2768 Args: 2769 *expressions (str | Expression): the SQL code strings to parse. 2770 If an `Expression` instance is passed, it will be used as-is. 2771 Multiple expressions are combined with an AND operator. 2772 append (bool): if `True`, AND the new expressions to any existing expression. 2773 Otherwise, this resets the expression. 2774 dialect (str): the dialect used to parse the input expressions. 2775 copy (bool): if `False`, modify this expression instance in-place. 2776 opts (kwargs): other options to use to parse the input expressions. 2777 2778 Returns: 2779 Select: the modified expression. 2780 """ 2781 return _apply_conjunction_builder( 2782 *expressions, 2783 instance=self, 2784 arg="where", 2785 append=append, 2786 into=Where, 2787 dialect=dialect, 2788 copy=copy, 2789 **opts, 2790 )
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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. Multiple expressions are combined with an AND operator. - append (bool): if
True, AND the new expressions to any existing expression. Otherwise, this resets the expression. - dialect (str): the dialect used to parse the input expressions.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2792 def having(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2793 """ 2794 Append to or set the HAVING expressions. 2795 2796 Example: 2797 >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql() 2798 'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3' 2799 2800 Args: 2801 *expressions (str | Expression): the SQL code strings to parse. 2802 If an `Expression` instance is passed, it will be used as-is. 2803 Multiple expressions are combined with an AND operator. 2804 append (bool): if `True`, AND the new expressions to any existing expression. 2805 Otherwise, this resets the expression. 2806 dialect (str): the dialect used to parse the input expressions. 2807 copy (bool): if `False`, modify this expression instance in-place. 2808 opts (kwargs): other options to use to parse the input expressions. 2809 2810 Returns: 2811 Select: the modified expression. 2812 """ 2813 return _apply_conjunction_builder( 2814 *expressions, 2815 instance=self, 2816 arg="having", 2817 append=append, 2818 into=Having, 2819 dialect=dialect, 2820 copy=copy, 2821 **opts, 2822 )
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 (str | Expression): the SQL code strings to parse.
If an
Expressioninstance is passed, it will be used as-is. Multiple expressions are combined with an AND operator. - append (bool): if
True, AND the new expressions to any existing expression. Otherwise, this resets the expression. - dialect (str): the dialect used to parse the input expressions.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2848 def distinct(self, *ons: ExpOrStr, distinct: bool = True, copy: bool = True) -> Select: 2849 """ 2850 Set the OFFSET expression. 2851 2852 Example: 2853 >>> Select().from_("tbl").select("x").distinct().sql() 2854 'SELECT DISTINCT x FROM tbl' 2855 2856 Args: 2857 ons: the expressions to distinct on 2858 distinct: whether the Select should be distinct 2859 copy: if `False`, modify this expression instance in-place. 2860 2861 Returns: 2862 Select: the modified expression. 2863 """ 2864 instance = _maybe_copy(self, copy) 2865 on = Tuple(expressions=[maybe_parse(on, copy=copy) for on in ons]) if ons else None 2866 instance.set("distinct", Distinct(on=on) if distinct else None) 2867 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.
2869 def ctas(self, table, properties=None, dialect=None, copy=True, **opts) -> Create: 2870 """ 2871 Convert this expression to a CREATE TABLE AS statement. 2872 2873 Example: 2874 >>> Select().select("*").from_("tbl").ctas("x").sql() 2875 'CREATE TABLE x AS SELECT * FROM tbl' 2876 2877 Args: 2878 table (str | Expression): the SQL code string to parse as the table name. 2879 If another `Expression` instance is passed, it will be used as-is. 2880 properties (dict): an optional mapping of table properties 2881 dialect (str): the dialect used to parse the input table. 2882 copy (bool): if `False`, modify this expression instance in-place. 2883 opts (kwargs): other options to use to parse the input table. 2884 2885 Returns: 2886 Create: the CREATE TABLE AS expression 2887 """ 2888 instance = _maybe_copy(self, copy) 2889 table_expression = maybe_parse( 2890 table, 2891 into=Table, 2892 dialect=dialect, 2893 **opts, 2894 ) 2895 properties_expression = None 2896 if properties: 2897 properties_expression = Properties.from_dict(properties) 2898 2899 return Create( 2900 this=table_expression, 2901 kind="table", 2902 expression=instance, 2903 properties=properties_expression, 2904 )
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 (str | Expression): the SQL code string to parse as the table name.
If another
Expressioninstance is passed, it will be used as-is. - properties (dict): an optional mapping of table properties
- dialect (str): the dialect used to parse the input table.
- copy (bool): if
False, modify this expression instance in-place. - opts (kwargs): other options to use to parse the input table.
Returns:
Create: the CREATE TABLE AS expression
2906 def lock(self, update: bool = True, copy: bool = True) -> Select: 2907 """ 2908 Set the locking read mode for this expression. 2909 2910 Examples: 2911 >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql") 2912 "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE" 2913 2914 >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql") 2915 "SELECT x FROM tbl WHERE x = 'a' FOR SHARE" 2916 2917 Args: 2918 update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`. 2919 copy: if `False`, modify this expression instance in-place. 2920 2921 Returns: 2922 The modified expression. 2923 """ 2924 2925 inst = _maybe_copy(self, copy) 2926 inst.set("locks", [Lock(update=update)]) 2927 2928 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 beFOR UPDATE, else it will beFOR SHARE. - copy: if
False, modify this expression instance in-place.
Returns:
The modified expression.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2943class Subquery(DerivedTable, Unionable): 2944 arg_types = { 2945 "this": True, 2946 "alias": False, 2947 "with": False, 2948 **QUERY_MODIFIERS, 2949 } 2950 2951 def unnest(self): 2952 """ 2953 Returns the first non subquery. 2954 """ 2955 expression = self 2956 while isinstance(expression, Subquery): 2957 expression = expression.this 2958 return expression 2959 2960 @property 2961 def is_star(self) -> bool: 2962 return self.this.is_star 2963 2964 @property 2965 def output_name(self): 2966 return self.alias
2951 def unnest(self): 2952 """ 2953 Returns the first non subquery. 2954 """ 2955 expression = self 2956 while isinstance(expression, Subquery): 2957 expression = expression.this 2958 return expression
Returns the first non subquery.
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").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2969class TableSample(Expression): 2970 arg_types = { 2971 "this": False, 2972 "method": False, 2973 "bucket_numerator": False, 2974 "bucket_denominator": False, 2975 "bucket_field": False, 2976 "percent": False, 2977 "rows": False, 2978 "size": False, 2979 "seed": False, 2980 "kind": False, 2981 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2984class Tag(Expression): 2985 """Tags are used for generating arbitrary sql like SELECT <span>x</span>.""" 2986 2987 arg_types = { 2988 "this": False, 2989 "prefix": False, 2990 "postfix": False, 2991 }
Tags are used for generating arbitrary sql like SELECT x.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
2994class Pivot(Expression): 2995 arg_types = { 2996 "alias": False, 2997 "expressions": True, 2998 "field": True, 2999 "unpivot": True, 3000 "columns": False, 3001 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3004class Window(Expression): 3005 arg_types = { 3006 "this": True, 3007 "partition_by": False, 3008 "order": False, 3009 "spec": False, 3010 "alias": False, 3011 "over": False, 3012 "first": False, 3013 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3016class WindowSpec(Expression): 3017 arg_types = { 3018 "kind": False, 3019 "start": False, 3020 "start_side": False, 3021 "end": False, 3022 "end_side": False, 3023 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3030class Star(Expression): 3031 arg_types = {"except": False, "replace": False} 3032 3033 @property 3034 def name(self) -> str: 3035 return "*" 3036 3037 @property 3038 def output_name(self): 3039 return self.name
Name of the output column if this expression is a selection.
If the Expression has no output name, an empty string is returned.
Example:
>>> from sqlglot import parse_one >>> parse_one("SELECT a").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3054class Null(Condition): 3055 arg_types: t.Dict[str, t.Any] = {} 3056 3057 @property 3058 def name(self) -> str: 3059 return "NULL"
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3070class DataType(Expression): 3071 arg_types = { 3072 "this": True, 3073 "expressions": False, 3074 "nested": False, 3075 "values": False, 3076 "prefix": False, 3077 } 3078 3079 class Type(AutoName): 3080 ARRAY = auto() 3081 BIGDECIMAL = auto() 3082 BIGINT = auto() 3083 BIGSERIAL = auto() 3084 BINARY = auto() 3085 BIT = auto() 3086 BOOLEAN = auto() 3087 CHAR = auto() 3088 DATE = auto() 3089 DATETIME = auto() 3090 DATETIME64 = auto() 3091 DECIMAL = auto() 3092 DOUBLE = auto() 3093 FLOAT = auto() 3094 GEOGRAPHY = auto() 3095 GEOMETRY = auto() 3096 HLLSKETCH = auto() 3097 HSTORE = auto() 3098 IMAGE = auto() 3099 INET = auto() 3100 INT = auto() 3101 INT128 = auto() 3102 INT256 = auto() 3103 INTERVAL = auto() 3104 JSON = auto() 3105 JSONB = auto() 3106 LONGBLOB = auto() 3107 LONGTEXT = auto() 3108 MAP = auto() 3109 MEDIUMBLOB = auto() 3110 MEDIUMTEXT = auto() 3111 MONEY = auto() 3112 NCHAR = auto() 3113 NULL = auto() 3114 NULLABLE = auto() 3115 NVARCHAR = auto() 3116 OBJECT = auto() 3117 ROWVERSION = auto() 3118 SERIAL = auto() 3119 SMALLINT = auto() 3120 SMALLMONEY = auto() 3121 SMALLSERIAL = auto() 3122 STRUCT = auto() 3123 SUPER = auto() 3124 TEXT = auto() 3125 TIME = auto() 3126 TIMESTAMP = auto() 3127 TIMESTAMPTZ = auto() 3128 TIMESTAMPLTZ = auto() 3129 TINYINT = auto() 3130 UBIGINT = auto() 3131 UINT = auto() 3132 USMALLINT = auto() 3133 UTINYINT = auto() 3134 UNKNOWN = auto() # Sentinel value, useful for type annotation 3135 UINT128 = auto() 3136 UINT256 = auto() 3137 UNIQUEIDENTIFIER = auto() 3138 UUID = auto() 3139 VARBINARY = auto() 3140 VARCHAR = auto() 3141 VARIANT = auto() 3142 XML = auto() 3143 3144 TEXT_TYPES = { 3145 Type.CHAR, 3146 Type.NCHAR, 3147 Type.VARCHAR, 3148 Type.NVARCHAR, 3149 Type.TEXT, 3150 } 3151 3152 INTEGER_TYPES = { 3153 Type.INT, 3154 Type.TINYINT, 3155 Type.SMALLINT, 3156 Type.BIGINT, 3157 Type.INT128, 3158 Type.INT256, 3159 } 3160 3161 FLOAT_TYPES = { 3162 Type.FLOAT, 3163 Type.DOUBLE, 3164 } 3165 3166 NUMERIC_TYPES = {*INTEGER_TYPES, *FLOAT_TYPES} 3167 3168 TEMPORAL_TYPES = { 3169 Type.TIMESTAMP, 3170 Type.TIMESTAMPTZ, 3171 Type.TIMESTAMPLTZ, 3172 Type.DATE, 3173 Type.DATETIME, 3174 Type.DATETIME64, 3175 } 3176 3177 @classmethod 3178 def build( 3179 cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs 3180 ) -> DataType: 3181 from sqlglot import parse_one 3182 3183 if isinstance(dtype, str): 3184 if dtype.upper() in cls.Type.__members__: 3185 data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[dtype.upper()]) 3186 else: 3187 data_type_exp = parse_one(dtype, read=dialect, into=DataType) 3188 if data_type_exp is None: 3189 raise ValueError(f"Unparsable data type value: {dtype}") 3190 elif isinstance(dtype, DataType.Type): 3191 data_type_exp = DataType(this=dtype) 3192 elif isinstance(dtype, DataType): 3193 return dtype 3194 else: 3195 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type") 3196 return DataType(**{**data_type_exp.args, **kwargs}) 3197 3198 def is_type(self, dtype: DataType.Type) -> bool: 3199 return self.this == dtype
3177 @classmethod 3178 def build( 3179 cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs 3180 ) -> DataType: 3181 from sqlglot import parse_one 3182 3183 if isinstance(dtype, str): 3184 if dtype.upper() in cls.Type.__members__: 3185 data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[dtype.upper()]) 3186 else: 3187 data_type_exp = parse_one(dtype, read=dialect, into=DataType) 3188 if data_type_exp is None: 3189 raise ValueError(f"Unparsable data type value: {dtype}") 3190 elif isinstance(dtype, DataType.Type): 3191 data_type_exp = DataType(this=dtype) 3192 elif isinstance(dtype, DataType): 3193 return dtype 3194 else: 3195 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type") 3196 return DataType(**{**data_type_exp.args, **kwargs})
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3079 class Type(AutoName): 3080 ARRAY = auto() 3081 BIGDECIMAL = auto() 3082 BIGINT = auto() 3083 BIGSERIAL = auto() 3084 BINARY = auto() 3085 BIT = auto() 3086 BOOLEAN = auto() 3087 CHAR = auto() 3088 DATE = auto() 3089 DATETIME = auto() 3090 DATETIME64 = auto() 3091 DECIMAL = auto() 3092 DOUBLE = auto() 3093 FLOAT = auto() 3094 GEOGRAPHY = auto() 3095 GEOMETRY = auto() 3096 HLLSKETCH = auto() 3097 HSTORE = auto() 3098 IMAGE = auto() 3099 INET = auto() 3100 INT = auto() 3101 INT128 = auto() 3102 INT256 = auto() 3103 INTERVAL = auto() 3104 JSON = auto() 3105 JSONB = auto() 3106 LONGBLOB = auto() 3107 LONGTEXT = auto() 3108 MAP = auto() 3109 MEDIUMBLOB = auto() 3110 MEDIUMTEXT = auto() 3111 MONEY = auto() 3112 NCHAR = auto() 3113 NULL = auto() 3114 NULLABLE = auto() 3115 NVARCHAR = auto() 3116 OBJECT = auto() 3117 ROWVERSION = auto() 3118 SERIAL = auto() 3119 SMALLINT = auto() 3120 SMALLMONEY = auto() 3121 SMALLSERIAL = auto() 3122 STRUCT = auto() 3123 SUPER = auto() 3124 TEXT = auto() 3125 TIME = auto() 3126 TIMESTAMP = auto() 3127 TIMESTAMPTZ = auto() 3128 TIMESTAMPLTZ = auto() 3129 TINYINT = auto() 3130 UBIGINT = auto() 3131 UINT = auto() 3132 USMALLINT = auto() 3133 UTINYINT = auto() 3134 UNKNOWN = auto() # Sentinel value, useful for type annotation 3135 UINT128 = auto() 3136 UINT256 = auto() 3137 UNIQUEIDENTIFIER = auto() 3138 UUID = auto() 3139 VARBINARY = auto() 3140 VARCHAR = auto() 3141 VARIANT = auto() 3142 XML = auto()
An enumeration.
Inherited Members
- enum.Enum
- name
- value
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3246class AddConstraint(Expression): 3247 arg_types = {"this": False, "expression": False, "enforced": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3255class Binary(Condition): 3256 arg_types = {"this": True, "expression": True} 3257 3258 @property 3259 def left(self): 3260 return self.this 3261 3262 @property 3263 def right(self): 3264 return self.expression
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3311class Dot(Binary): 3312 @property 3313 def name(self) -> str: 3314 return self.expression.name 3315 3316 @classmethod 3317 def build(self, expressions: t.Sequence[Expression]) -> Dot: 3318 """Build a Dot object with a sequence of expressions.""" 3319 if len(expressions) < 2: 3320 raise ValueError(f"Dot requires >= 2 expressions.") 3321 3322 a, b, *expressions = expressions 3323 dot = Dot(this=a, expression=b) 3324 3325 for expression in expressions: 3326 dot = Dot(this=dot, expression=expression) 3327 3328 return dot
3316 @classmethod 3317 def build(self, expressions: t.Sequence[Expression]) -> Dot: 3318 """Build a Dot object with a sequence of expressions.""" 3319 if len(expressions) < 2: 3320 raise ValueError(f"Dot requires >= 2 expressions.") 3321 3322 a, b, *expressions = expressions 3323 dot = Dot(this=a, expression=b) 3324 3325 for expression in expressions: 3326 dot = Dot(this=dot, expression=expression) 3327 3328 return dot
Build a Dot object with a sequence of expressions.
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Kwarg in special functions like func(kwarg => y).
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3453class Alias(Expression): 3454 arg_types = {"this": True, "alias": False} 3455 3456 @property 3457 def output_name(self): 3458 return self.alias
Name of the output column if this expression is a selection.
If the Expression has no output name, an empty string is returned.
Example:
>>> from sqlglot import parse_one >>> parse_one("SELECT a").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3461class Aliases(Expression): 3462 arg_types = {"this": True, "expressions": True} 3463 3464 @property 3465 def aliases(self): 3466 return self.expressions
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3485class In(Predicate): 3486 arg_types = { 3487 "this": True, 3488 "expressions": False, 3489 "query": False, 3490 "unnest": False, 3491 "field": False, 3492 "is_global": False, 3493 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3496class TimeUnit(Expression): 3497 """Automatically converts unit arg into a var.""" 3498 3499 arg_types = {"unit": False} 3500 3501 def __init__(self, **args): 3502 unit = args.get("unit") 3503 if isinstance(unit, (Column, Literal)): 3504 args["unit"] = Var(this=unit.name) 3505 elif isinstance(unit, Week): 3506 unit.set("this", Var(this=unit.this.name)) 3507 super().__init__(**args)
Automatically converts unit arg into a var.
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3510class Interval(TimeUnit): 3511 arg_types = {"this": False, "unit": False} 3512 3513 @property 3514 def unit(self) -> t.Optional[Var]: 3515 return self.args.get("unit")
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3527class Func(Condition): 3528 """ 3529 The base class for all function expressions. 3530 3531 Attributes: 3532 is_var_len_args (bool): if set to True the last argument defined in arg_types will be 3533 treated as a variable length argument and the argument's value will be stored as a list. 3534 _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items) 3535 for this function expression. These values are used to map this node to a name during parsing 3536 as well as to provide the function's name during SQL string generation. By default the SQL 3537 name is set to the expression's class name transformed to snake case. 3538 """ 3539 3540 is_var_len_args = False 3541 3542 @classmethod 3543 def from_arg_list(cls, args): 3544 if cls.is_var_len_args: 3545 all_arg_keys = list(cls.arg_types) 3546 # If this function supports variable length argument treat the last argument as such. 3547 non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys 3548 num_non_var = len(non_var_len_arg_keys) 3549 3550 args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)} 3551 args_dict[all_arg_keys[-1]] = args[num_non_var:] 3552 else: 3553 args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)} 3554 3555 return cls(**args_dict) 3556 3557 @classmethod 3558 def sql_names(cls): 3559 if cls is Func: 3560 raise NotImplementedError( 3561 "SQL name is only supported by concrete function implementations" 3562 ) 3563 if "_sql_names" not in cls.__dict__: 3564 cls._sql_names = [camel_to_snake_case(cls.__name__)] 3565 return cls._sql_names 3566 3567 @classmethod 3568 def sql_name(cls): 3569 return cls.sql_names()[0] 3570 3571 @classmethod 3572 def default_parser_mappings(cls): 3573 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.
3542 @classmethod 3543 def from_arg_list(cls, args): 3544 if cls.is_var_len_args: 3545 all_arg_keys = list(cls.arg_types) 3546 # If this function supports variable length argument treat the last argument as such. 3547 non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys 3548 num_non_var = len(non_var_len_arg_keys) 3549 3550 args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)} 3551 args_dict[all_arg_keys[-1]] = args[num_non_var:] 3552 else: 3553 args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)} 3554 3555 return cls(**args_dict)
3557 @classmethod 3558 def sql_names(cls): 3559 if cls is Func: 3560 raise NotImplementedError( 3561 "SQL name is only supported by concrete function implementations" 3562 ) 3563 if "_sql_names" not in cls.__dict__: 3564 cls._sql_names = [camel_to_snake_case(cls.__name__)] 3565 return cls._sql_names
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3580class ParameterizedAgg(AggFunc): 3581 arg_types = {"this": True, "expressions": True, "params": True}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3588class Anonymous(Func): 3589 arg_types = {"this": True, "expressions": False} 3590 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3595class Hll(AggFunc): 3596 arg_types = {"this": True, "expressions": False} 3597 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3600class ApproxDistinct(AggFunc): 3601 arg_types = {"this": True, "accuracy": False} 3602 _sql_names = ["APPROX_DISTINCT", "APPROX_COUNT_DISTINCT"]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3631class ArrayConcat(Func): 3632 arg_types = {"this": True, "expressions": False} 3633 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3644class ArrayFilter(Func): 3645 arg_types = {"this": True, "expression": True} 3646 _sql_names = ["FILTER", "ARRAY_FILTER"]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3677class Case(Func): 3678 arg_types = {"this": False, "ifs": True, "default": False} 3679 3680 def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case: 3681 instance = _maybe_copy(self, copy) 3682 instance.append( 3683 "ifs", 3684 If( 3685 this=maybe_parse(condition, copy=copy, **opts), 3686 true=maybe_parse(then, copy=copy, **opts), 3687 ), 3688 ) 3689 return instance 3690 3691 def else_(self, condition: ExpOrStr, copy: bool = True, **opts) -> Case: 3692 instance = _maybe_copy(self, copy) 3693 instance.set("default", maybe_parse(condition, copy=copy, **opts)) 3694 return instance
3680 def when(self, condition: ExpOrStr, then: ExpOrStr, copy: bool = True, **opts) -> Case: 3681 instance = _maybe_copy(self, copy) 3682 instance.append( 3683 "ifs", 3684 If( 3685 this=maybe_parse(condition, copy=copy, **opts), 3686 true=maybe_parse(then, copy=copy, **opts), 3687 ), 3688 ) 3689 return instance
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3697class Cast(Func): 3698 arg_types = {"this": True, "to": True} 3699 3700 @property 3701 def name(self) -> str: 3702 return self.this.name 3703 3704 @property 3705 def to(self): 3706 return self.args["to"] 3707 3708 @property 3709 def output_name(self): 3710 return self.name 3711 3712 def is_type(self, dtype: DataType.Type) -> bool: 3713 return self.to.is_type(dtype)
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").expressions[0].output_name 'a' >>> parse_one("SELECT b AS c").expressions[0].output_name 'c' >>> parse_one("SELECT 1 + 2").expressions[0].output_name ''
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3728class Ceil(Func): 3729 arg_types = {"this": True, "decimals": False} 3730 _sql_names = ["CEIL", "CEILING"]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3733class Coalesce(Func): 3734 arg_types = {"this": True, "expressions": False} 3735 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3775class DateAdd(Func, TimeUnit): 3776 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3779class DateSub(Func, TimeUnit): 3780 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3783class DateDiff(Func, TimeUnit): 3784 _sql_names = ["DATEDIFF", "DATE_DIFF"] 3785 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3792class DatetimeAdd(Func, TimeUnit): 3793 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3796class DatetimeSub(Func, TimeUnit): 3797 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3800class DatetimeDiff(Func, TimeUnit): 3801 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3804class DatetimeTrunc(Func, TimeUnit): 3805 arg_types = {"this": True, "unit": True, "zone": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3832class TimestampAdd(Func, TimeUnit): 3833 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3836class TimestampSub(Func, TimeUnit): 3837 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3840class TimestampDiff(Func, TimeUnit): 3841 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3844class TimestampTrunc(Func, TimeUnit): 3845 arg_types = {"this": True, "unit": True, "zone": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3848class TimeAdd(Func, TimeUnit): 3849 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3852class TimeSub(Func, TimeUnit): 3853 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3856class TimeDiff(Func, TimeUnit): 3857 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3864class DateFromParts(Func): 3865 _sql_names = ["DATEFROMPARTS"] 3866 arg_types = {"year": True, "month": True, "day": True}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3917class Greatest(Func): 3918 arg_types = {"this": True, "expressions": False} 3919 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3934class IfNull(Func): 3935 arg_types = {"this": True, "expression": False} 3936 _sql_names = ["IFNULL", "NVL"]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3947class JSONObject(Func): 3948 arg_types = { 3949 "expressions": False, 3950 "null_handling": False, 3951 "unique_keys": False, 3952 "return_type": False, 3953 "format_json": False, 3954 "encoding": False, 3955 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3958class OpenJSONColumnDef(Expression): 3959 arg_types = {"this": True, "kind": True, "path": False, "as_json": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
3986class JSONFormat(Func): 3987 arg_types = {"this": False, "options": False} 3988 _sql_names = ["JSON_FORMAT"]
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4000class Levenshtein(Func): 4001 arg_types = { 4002 "this": True, 4003 "expression": False, 4004 "ins_cost": False, 4005 "del_cost": False, 4006 "sub_cost": False, 4007 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4052class MatchAgainst(Func): 4053 arg_types = {"this": True, "expressions": True, "modifier": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4056class Max(AggFunc): 4057 arg_types = {"this": True, "expressions": False} 4058 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4065class Min(AggFunc): 4066 arg_types = {"this": True, "expressions": False} 4067 is_var_len_args = True
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4098class ApproxQuantile(Quantile): 4099 arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4106class ReadCSV(Func): 4107 _sql_names = ["READ_CSV"] 4108 is_var_len_args = True 4109 arg_types = {"this": True, "expressions": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4112class Reduce(Func): 4113 arg_types = {"this": True, "initial": True, "merge": True, "finish": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4116class RegexpExtract(Func): 4117 arg_types = { 4118 "this": True, 4119 "expression": True, 4120 "position": False, 4121 "occurrence": False, 4122 "group": False, 4123 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4187class StrPosition(Func): 4188 arg_types = { 4189 "this": True, 4190 "substr": True, 4191 "position": False, 4192 "instance": False, 4193 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4267class Trim(Func): 4268 arg_types = { 4269 "this": True, 4270 "expression": False, 4271 "position": False, 4272 "collation": False, 4273 }
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4276class TsOrDsAdd(Func, TimeUnit): 4277 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4302class UnixToTime(Func): 4303 arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False} 4304 4305 SECONDS = Literal.string("seconds") 4306 MILLIS = Literal.string("millis") 4307 MICROS = Literal.string("micros")
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4330class XMLTable(Func): 4331 arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4342class Merge(Expression): 4343 arg_types = {"this": True, "using": True, "on": True, "expressions": True}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4346class When(Func): 4347 arg_types = {"matched": True, "source": False, "condition": False, "then": True}
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
Inherited Members
- Expression
- Expression
- this
- expression
- expressions
- text
- is_string
- is_number
- is_int
- is_star
- alias
- output_name
- copy
- add_comments
- append
- set
- depth
- iter_expressions
- find
- find_all
- find_ancestor
- parent_select
- same_parent
- root
- walk
- dfs
- bfs
- unnest
- unalias
- unnest_operands
- flatten
- sql
- transform
- replace
- pop
- assert_is
- error_messages
- dump
- load
4390def maybe_parse( 4391 sql_or_expression: ExpOrStr, 4392 *, 4393 into: t.Optional[IntoType] = None, 4394 dialect: DialectType = None, 4395 prefix: t.Optional[str] = None, 4396 copy: bool = False, 4397 **opts, 4398) -> Expression: 4399 """Gracefully handle a possible string or expression. 4400 4401 Example: 4402 >>> maybe_parse("1") 4403 (LITERAL this: 1, is_string: False) 4404 >>> maybe_parse(to_identifier("x")) 4405 (IDENTIFIER this: x, quoted: False) 4406 4407 Args: 4408 sql_or_expression: the SQL code string or an expression 4409 into: the SQLGlot Expression to parse into 4410 dialect: the dialect used to parse the input expressions (in the case that an 4411 input expression is a SQL string). 4412 prefix: a string to prefix the sql with before it gets parsed 4413 (automatically includes a space) 4414 copy: whether or not to copy the expression. 4415 **opts: other options to use to parse the input expressions (again, in the case 4416 that an input expression is a SQL string). 4417 4418 Returns: 4419 Expression: the parsed or given expression. 4420 """ 4421 if isinstance(sql_or_expression, Expression): 4422 if copy: 4423 return sql_or_expression.copy() 4424 return sql_or_expression 4425 4426 import sqlglot 4427 4428 sql = str(sql_or_expression) 4429 if prefix: 4430 sql = f"{prefix} {sql}" 4431 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.
4603def union(left, right, distinct=True, dialect=None, **opts): 4604 """ 4605 Initializes a syntax tree from one UNION expression. 4606 4607 Example: 4608 >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql() 4609 'SELECT * FROM foo UNION SELECT * FROM bla' 4610 4611 Args: 4612 left (str | Expression): the SQL code string corresponding to the left-hand side. 4613 If an `Expression` instance is passed, it will be used as-is. 4614 right (str | Expression): the SQL code string corresponding to the right-hand side. 4615 If an `Expression` instance is passed, it will be used as-is. 4616 distinct (bool): set the DISTINCT flag if and only if this is true. 4617 dialect (str): the dialect used to parse the input expression. 4618 opts (kwargs): other options to use to parse the input expressions. 4619 Returns: 4620 Union: the syntax tree for the UNION expression. 4621 """ 4622 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 4623 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 4624 4625 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 (str | Expression): the SQL code string corresponding to the left-hand side.
If an
Expressioninstance is passed, it will be used as-is. - right (str | Expression): the SQL code string corresponding to the right-hand side.
If an
Expressioninstance is passed, it will be used as-is. - distinct (bool): set the DISTINCT flag if and only if this is true.
- dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Union: the syntax tree for the UNION expression.
4628def intersect(left, right, distinct=True, dialect=None, **opts): 4629 """ 4630 Initializes a syntax tree from one INTERSECT expression. 4631 4632 Example: 4633 >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql() 4634 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 4635 4636 Args: 4637 left (str | Expression): the SQL code string corresponding to the left-hand side. 4638 If an `Expression` instance is passed, it will be used as-is. 4639 right (str | Expression): the SQL code string corresponding to the right-hand side. 4640 If an `Expression` instance is passed, it will be used as-is. 4641 distinct (bool): set the DISTINCT flag if and only if this is true. 4642 dialect (str): the dialect used to parse the input expression. 4643 opts (kwargs): other options to use to parse the input expressions. 4644 Returns: 4645 Intersect: the syntax tree for the INTERSECT expression. 4646 """ 4647 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 4648 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 4649 4650 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 (str | Expression): the SQL code string corresponding to the left-hand side.
If an
Expressioninstance is passed, it will be used as-is. - right (str | Expression): the SQL code string corresponding to the right-hand side.
If an
Expressioninstance is passed, it will be used as-is. - distinct (bool): set the DISTINCT flag if and only if this is true.
- dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Intersect: the syntax tree for the INTERSECT expression.
4653def except_(left, right, distinct=True, dialect=None, **opts): 4654 """ 4655 Initializes a syntax tree from one EXCEPT expression. 4656 4657 Example: 4658 >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql() 4659 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 4660 4661 Args: 4662 left (str | Expression): the SQL code string corresponding to the left-hand side. 4663 If an `Expression` instance is passed, it will be used as-is. 4664 right (str | Expression): the SQL code string corresponding to the right-hand side. 4665 If an `Expression` instance is passed, it will be used as-is. 4666 distinct (bool): set the DISTINCT flag if and only if this is true. 4667 dialect (str): the dialect used to parse the input expression. 4668 opts (kwargs): other options to use to parse the input expressions. 4669 Returns: 4670 Except: the syntax tree for the EXCEPT statement. 4671 """ 4672 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 4673 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 4674 4675 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 (str | Expression): the SQL code string corresponding to the left-hand side.
If an
Expressioninstance is passed, it will be used as-is. - right (str | Expression): the SQL code string corresponding to the right-hand side.
If an
Expressioninstance is passed, it will be used as-is. - distinct (bool): set the DISTINCT flag if and only if this is true.
- dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Except: the syntax tree for the EXCEPT statement.
4678def select(*expressions: ExpOrStr, dialect: DialectType = None, **opts) -> Select: 4679 """ 4680 Initializes a syntax tree from one or multiple SELECT expressions. 4681 4682 Example: 4683 >>> select("col1", "col2").from_("tbl").sql() 4684 'SELECT col1, col2 FROM tbl' 4685 4686 Args: 4687 *expressions: the SQL code string to parse as the expressions of a 4688 SELECT statement. If an Expression instance is passed, this is used as-is. 4689 dialect: the dialect used to parse the input expressions (in the case that an 4690 input expression is a SQL string). 4691 **opts: other options to use to parse the input expressions (again, in the case 4692 that an input expression is a SQL string). 4693 4694 Returns: 4695 Select: the syntax tree for the SELECT statement. 4696 """ 4697 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.
4700def from_(expression: ExpOrStr, dialect: DialectType = None, **opts) -> Select: 4701 """ 4702 Initializes a syntax tree from a FROM expression. 4703 4704 Example: 4705 >>> from_("tbl").select("col1", "col2").sql() 4706 'SELECT col1, col2 FROM tbl' 4707 4708 Args: 4709 *expression: the SQL code string to parse as the FROM expressions of a 4710 SELECT statement. If an Expression instance is passed, this is used as-is. 4711 dialect: the dialect used to parse the input expression (in the case that the 4712 input expression is a SQL string). 4713 **opts: other options to use to parse the input expressions (again, in the case 4714 that the input expression is a SQL string). 4715 4716 Returns: 4717 Select: the syntax tree for the SELECT statement. 4718 """ 4719 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.
4722def update( 4723 table: str | Table, 4724 properties: dict, 4725 where: t.Optional[ExpOrStr] = None, 4726 from_: t.Optional[ExpOrStr] = None, 4727 dialect: DialectType = None, 4728 **opts, 4729) -> Update: 4730 """ 4731 Creates an update statement. 4732 4733 Example: 4734 >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql() 4735 "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1" 4736 4737 Args: 4738 *properties: dictionary of properties to set which are 4739 auto converted to sql objects eg None -> NULL 4740 where: sql conditional parsed into a WHERE statement 4741 from_: sql statement parsed into a FROM statement 4742 dialect: the dialect used to parse the input expressions. 4743 **opts: other options to use to parse the input expressions. 4744 4745 Returns: 4746 Update: the syntax tree for the UPDATE statement. 4747 """ 4748 update_expr = Update(this=maybe_parse(table, into=Table, dialect=dialect)) 4749 update_expr.set( 4750 "expressions", 4751 [ 4752 EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v)) 4753 for k, v in properties.items() 4754 ], 4755 ) 4756 if from_: 4757 update_expr.set( 4758 "from", 4759 maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts), 4760 ) 4761 if isinstance(where, Condition): 4762 where = Where(this=where) 4763 if where: 4764 update_expr.set( 4765 "where", 4766 maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts), 4767 ) 4768 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.
4771def delete( 4772 table: ExpOrStr, 4773 where: t.Optional[ExpOrStr] = None, 4774 returning: t.Optional[ExpOrStr] = None, 4775 dialect: DialectType = None, 4776 **opts, 4777) -> Delete: 4778 """ 4779 Builds a delete statement. 4780 4781 Example: 4782 >>> delete("my_table", where="id > 1").sql() 4783 'DELETE FROM my_table WHERE id > 1' 4784 4785 Args: 4786 where: sql conditional parsed into a WHERE statement 4787 returning: sql conditional parsed into a RETURNING statement 4788 dialect: the dialect used to parse the input expressions. 4789 **opts: other options to use to parse the input expressions. 4790 4791 Returns: 4792 Delete: the syntax tree for the DELETE statement. 4793 """ 4794 delete_expr = Delete().delete(table, dialect=dialect, copy=False, **opts) 4795 if where: 4796 delete_expr = delete_expr.where(where, dialect=dialect, copy=False, **opts) 4797 if returning: 4798 delete_expr = delete_expr.returning(returning, dialect=dialect, copy=False, **opts) 4799 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.
4802def insert( 4803 expression: ExpOrStr, 4804 into: ExpOrStr, 4805 columns: t.Optional[t.Sequence[ExpOrStr]] = None, 4806 overwrite: t.Optional[bool] = None, 4807 dialect: DialectType = None, 4808 copy: bool = True, 4809 **opts, 4810) -> Insert: 4811 """ 4812 Builds an INSERT statement. 4813 4814 Example: 4815 >>> insert("VALUES (1, 2, 3)", "tbl").sql() 4816 'INSERT INTO tbl VALUES (1, 2, 3)' 4817 4818 Args: 4819 expression: the sql string or expression of the INSERT statement 4820 into: the tbl to insert data to. 4821 columns: optionally the table's column names. 4822 overwrite: whether to INSERT OVERWRITE or not. 4823 dialect: the dialect used to parse the input expressions. 4824 copy: whether or not to copy the expression. 4825 **opts: other options to use to parse the input expressions. 4826 4827 Returns: 4828 Insert: the syntax tree for the INSERT statement. 4829 """ 4830 expr = maybe_parse(expression, dialect=dialect, copy=copy, **opts) 4831 this: Table | Schema = maybe_parse(into, into=Table, dialect=dialect, copy=copy, **opts) 4832 4833 if columns: 4834 this = _apply_list_builder( 4835 *columns, 4836 instance=Schema(this=this), 4837 arg="expressions", 4838 into=Identifier, 4839 copy=False, 4840 dialect=dialect, 4841 **opts, 4842 ) 4843 4844 return Insert(this=this, expression=expr, overwrite=overwrite)
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.
- 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.
4847def condition(expression, dialect=None, copy=True, **opts) -> Condition: 4848 """ 4849 Initialize a logical condition expression. 4850 4851 Example: 4852 >>> condition("x=1").sql() 4853 'x = 1' 4854 4855 This is helpful for composing larger logical syntax trees: 4856 >>> where = condition("x=1") 4857 >>> where = where.and_("y=1") 4858 >>> Select().from_("tbl").select("*").where(where).sql() 4859 'SELECT * FROM tbl WHERE x = 1 AND y = 1' 4860 4861 Args: 4862 *expression (str | Expression): the SQL code string to parse. 4863 If an Expression instance is passed, this is used as-is. 4864 dialect (str): the dialect used to parse the input expression (in the case that the 4865 input expression is a SQL string). 4866 copy (bool): Whether or not to copy `expression` (only applies to expressions). 4867 **opts: other options to use to parse the input expressions (again, in the case 4868 that the input expression is a SQL string). 4869 4870 Returns: 4871 Condition: the expression 4872 """ 4873 return maybe_parse( # type: ignore 4874 expression, 4875 into=Condition, 4876 dialect=dialect, 4877 copy=copy, 4878 **opts, 4879 )
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 (str | Expression): the SQL code string to parse. If an Expression instance is passed, this is used as-is.
- dialect (str): the dialect used to parse the input expression (in the case that the input expression is a SQL string).
- copy (bool): 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:
Condition: the expression
4882def and_(*expressions, dialect=None, copy=True, **opts) -> And: 4883 """ 4884 Combine multiple conditions with an AND logical operator. 4885 4886 Example: 4887 >>> and_("x=1", and_("y=1", "z=1")).sql() 4888 'x = 1 AND (y = 1 AND z = 1)' 4889 4890 Args: 4891 *expressions (str | Expression): the SQL code strings to parse. 4892 If an Expression instance is passed, this is used as-is. 4893 dialect (str): the dialect used to parse the input expression. 4894 copy (bool): whether or not to copy `expressions` (only applies to Expressions). 4895 **opts: other options to use to parse the input expressions. 4896 4897 Returns: 4898 And: the new condition 4899 """ 4900 return _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 (str | Expression): the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
- dialect (str): the dialect used to parse the input expression.
- copy (bool): 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
4903def or_(*expressions, dialect=None, copy=True, **opts) -> Or: 4904 """ 4905 Combine multiple conditions with an OR logical operator. 4906 4907 Example: 4908 >>> or_("x=1", or_("y=1", "z=1")).sql() 4909 'x = 1 OR (y = 1 OR z = 1)' 4910 4911 Args: 4912 *expressions (str | Expression): the SQL code strings to parse. 4913 If an Expression instance is passed, this is used as-is. 4914 dialect (str): the dialect used to parse the input expression. 4915 copy (bool): whether or not to copy `expressions` (only applies to Expressions). 4916 **opts: other options to use to parse the input expressions. 4917 4918 Returns: 4919 Or: the new condition 4920 """ 4921 return _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 (str | Expression): the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
- dialect (str): the dialect used to parse the input expression.
- copy (bool): 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
4924def not_(expression, dialect=None, copy=True, **opts) -> Not: 4925 """ 4926 Wrap a condition with a NOT operator. 4927 4928 Example: 4929 >>> not_("this_suit='black'").sql() 4930 "NOT this_suit = 'black'" 4931 4932 Args: 4933 expression (str | Expression): the SQL code strings to parse. 4934 If an Expression instance is passed, this is used as-is. 4935 dialect (str): the dialect used to parse the input expression. 4936 **opts: other options to use to parse the input expressions. 4937 4938 Returns: 4939 Not: the new condition 4940 """ 4941 this = condition( 4942 expression, 4943 dialect=dialect, 4944 copy=copy, 4945 **opts, 4946 ) 4947 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 (str | Expression): the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
- dialect (str): the dialect used to parse the input expression.
- **opts: other options to use to parse the input expressions.
Returns:
Not: the new condition
4969def to_identifier(name, quoted=None, copy=True): 4970 """Builds an identifier. 4971 4972 Args: 4973 name: The name to turn into an identifier. 4974 quoted: Whether or not force quote the identifier. 4975 copy: Whether or not to copy a passed in Identefier node. 4976 4977 Returns: 4978 The identifier ast node. 4979 """ 4980 4981 if name is None: 4982 return None 4983 4984 if isinstance(name, Identifier): 4985 identifier = _maybe_copy(name, copy) 4986 elif isinstance(name, str): 4987 identifier = Identifier( 4988 this=name, 4989 quoted=not SAFE_IDENTIFIER_RE.match(name) if quoted is None else quoted, 4990 ) 4991 else: 4992 raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}") 4993 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 a passed in Identefier node.
Returns:
The identifier ast node.
4999def to_interval(interval: str | Literal) -> Interval: 5000 """Builds an interval expression from a string like '1 day' or '5 months'.""" 5001 if isinstance(interval, Literal): 5002 if not interval.is_string: 5003 raise ValueError("Invalid interval string.") 5004 5005 interval = interval.this 5006 5007 interval_parts = INTERVAL_STRING_RE.match(interval) # type: ignore 5008 5009 if not interval_parts: 5010 raise ValueError("Invalid interval string.") 5011 5012 return Interval( 5013 this=Literal.string(interval_parts.group(1)), 5014 unit=Var(this=interval_parts.group(2)), 5015 )
Builds an interval expression from a string like '1 day' or '5 months'.
5028def to_table(sql_path: t.Optional[str | Table], **kwargs) -> t.Optional[Table]: 5029 """ 5030 Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional. 5031 If a table is passed in then that table is returned. 5032 5033 Args: 5034 sql_path: a `[catalog].[schema].[table]` string. 5035 5036 Returns: 5037 A table expression. 5038 """ 5039 if sql_path is None or isinstance(sql_path, Table): 5040 return sql_path 5041 if not isinstance(sql_path, str): 5042 raise ValueError(f"Invalid type provided for a table: {type(sql_path)}") 5043 5044 catalog, db, table_name = (to_identifier(x) for x in split_num_words(sql_path, ".", 3)) 5045 return Table(this=table_name, db=db, catalog=catalog, **kwargs)
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.
Returns:
A table expression.
5048def to_column(sql_path: str | Column, **kwargs) -> Column: 5049 """ 5050 Create a column from a `[table].[column]` sql path. Schema is optional. 5051 5052 If a column is passed in then that column is returned. 5053 5054 Args: 5055 sql_path: `[table].[column]` string 5056 Returns: 5057 Table: A column expression 5058 """ 5059 if sql_path is None or isinstance(sql_path, Column): 5060 return sql_path 5061 if not isinstance(sql_path, str): 5062 raise ValueError(f"Invalid type provided for column: {type(sql_path)}") 5063 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
5066def alias_( 5067 expression: ExpOrStr, 5068 alias: str | Identifier, 5069 table: bool | t.Sequence[str | Identifier] = False, 5070 quoted: t.Optional[bool] = None, 5071 dialect: DialectType = None, 5072 copy: bool = True, 5073 **opts, 5074): 5075 """Create an Alias expression. 5076 5077 Example: 5078 >>> alias_('foo', 'bar').sql() 5079 'foo AS bar' 5080 5081 >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql() 5082 '(SELECT 1, 2) AS bar(a, b)' 5083 5084 Args: 5085 expression: the SQL code strings to parse. 5086 If an Expression instance is passed, this is used as-is. 5087 alias: the alias name to use. If the name has 5088 special characters it is quoted. 5089 table: Whether or not to create a table alias, can also be a list of columns. 5090 quoted: whether or not to quote the alias 5091 dialect: the dialect used to parse the input expression. 5092 copy: Whether or not to copy the expression. 5093 **opts: other options to use to parse the input expressions. 5094 5095 Returns: 5096 Alias: the aliased expression 5097 """ 5098 exp = maybe_parse(expression, dialect=dialect, copy=copy, **opts) 5099 alias = to_identifier(alias, quoted=quoted) 5100 5101 if table: 5102 table_alias = TableAlias(this=alias) 5103 exp.set("alias", table_alias) 5104 5105 if not isinstance(table, bool): 5106 for column in table: 5107 table_alias.append("columns", to_identifier(column, quoted=quoted)) 5108 5109 return exp 5110 5111 # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in 5112 # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node 5113 # for the complete Window expression. 5114 # 5115 # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls 5116 5117 if "alias" in exp.arg_types and not isinstance(exp, Window): 5118 exp.set("alias", alias) 5119 return exp 5120 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
5123def subquery(expression, alias=None, dialect=None, **opts): 5124 """ 5125 Build a subquery expression. 5126 5127 Example: 5128 >>> subquery('select x from tbl', 'bar').select('x').sql() 5129 'SELECT x FROM (SELECT x FROM tbl) AS bar' 5130 5131 Args: 5132 expression (str | Expression): the SQL code strings to parse. 5133 If an Expression instance is passed, this is used as-is. 5134 alias (str | Expression): the alias name to use. 5135 dialect (str): the dialect used to parse the input expression. 5136 **opts: other options to use to parse the input expressions. 5137 5138 Returns: 5139 Select: a new select with the subquery expression included 5140 """ 5141 5142 expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias) 5143 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 (str | Expression): the SQL code strings to parse. If an Expression instance is passed, this is used as-is.
- alias (str | Expression): the alias name to use.
- dialect (str): the dialect used to parse the input expression.
- **opts: other options to use to parse the input expressions.
Returns:
Select: a new select with the subquery expression included
5146def column( 5147 col: str | Identifier, 5148 table: t.Optional[str | Identifier] = None, 5149 db: t.Optional[str | Identifier] = None, 5150 catalog: t.Optional[str | Identifier] = None, 5151 quoted: t.Optional[bool] = None, 5152) -> Column: 5153 """ 5154 Build a Column. 5155 5156 Args: 5157 col: column name 5158 table: table name 5159 db: db name 5160 catalog: catalog name 5161 quoted: whether or not to force quote each part 5162 Returns: 5163 Column: column instance 5164 """ 5165 return Column( 5166 this=to_identifier(col, quoted=quoted), 5167 table=to_identifier(table, quoted=quoted), 5168 db=to_identifier(db, quoted=quoted), 5169 catalog=to_identifier(catalog, quoted=quoted), 5170 )
Build a Column.
Arguments:
- col: column name
- table: table name
- db: db name
- catalog: catalog name
- quoted: whether or not to force quote each part
Returns:
Column: column instance
5173def cast(expression: ExpOrStr, to: str | DataType | DataType.Type, **opts) -> Cast: 5174 """Cast an expression to a data type. 5175 5176 Example: 5177 >>> cast('x + 1', 'int').sql() 5178 'CAST(x + 1 AS INT)' 5179 5180 Args: 5181 expression: The expression to cast. 5182 to: The datatype to cast to. 5183 5184 Returns: 5185 A cast node. 5186 """ 5187 expression = maybe_parse(expression, **opts) 5188 return Cast(this=expression, to=DataType.build(to, **opts))
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:
A cast node.
5191def table_(table, db=None, catalog=None, quoted=None, alias=None) -> Table: 5192 """Build a Table. 5193 5194 Args: 5195 table (str | Expression): column name 5196 db (str | Expression): db name 5197 catalog (str | Expression): catalog name 5198 5199 Returns: 5200 Table: table instance 5201 """ 5202 return Table( 5203 this=to_identifier(table, quoted=quoted), 5204 db=to_identifier(db, quoted=quoted), 5205 catalog=to_identifier(catalog, quoted=quoted), 5206 alias=TableAlias(this=to_identifier(alias)) if alias else None, 5207 )
Build a Table.
Arguments:
- table (str | Expression): column name
- db (str | Expression): db name
- catalog (str | Expression): catalog name
Returns:
Table: table instance
5210def values( 5211 values: t.Iterable[t.Tuple[t.Any, ...]], 5212 alias: t.Optional[str] = None, 5213 columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None, 5214) -> Values: 5215 """Build VALUES statement. 5216 5217 Example: 5218 >>> values([(1, '2')]).sql() 5219 "VALUES (1, '2')" 5220 5221 Args: 5222 values: values statements that will be converted to SQL 5223 alias: optional alias 5224 columns: Optional list of ordered column names or ordered dictionary of column names to types. 5225 If either are provided then an alias is also required. 5226 5227 Returns: 5228 Values: the Values expression object 5229 """ 5230 if columns and not alias: 5231 raise ValueError("Alias is required when providing columns") 5232 5233 return Values( 5234 expressions=[convert(tup) for tup in values], 5235 alias=( 5236 TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns]) 5237 if columns 5238 else (TableAlias(this=to_identifier(alias)) if alias else None) 5239 ), 5240 )
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
5243def var(name: t.Optional[ExpOrStr]) -> Var: 5244 """Build a SQL variable. 5245 5246 Example: 5247 >>> repr(var('x')) 5248 '(VAR this: x)' 5249 5250 >>> repr(var(column('x', table='y'))) 5251 '(VAR this: x)' 5252 5253 Args: 5254 name: The name of the var or an expression who's name will become the var. 5255 5256 Returns: 5257 The new variable node. 5258 """ 5259 if not name: 5260 raise ValueError("Cannot convert empty name into var.") 5261 5262 if isinstance(name, Expression): 5263 name = name.name 5264 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.
5267def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable: 5268 """Build ALTER TABLE... RENAME... expression 5269 5270 Args: 5271 old_name: The old name of the table 5272 new_name: The new name of the table 5273 5274 Returns: 5275 Alter table expression 5276 """ 5277 old_table = to_table(old_name) 5278 new_table = to_table(new_name) 5279 return AlterTable( 5280 this=old_table, 5281 actions=[ 5282 RenameTable(this=new_table), 5283 ], 5284 )
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
5287def convert(value: t.Any, copy: bool = False) -> Expression: 5288 """Convert a python value into an expression object. 5289 5290 Raises an error if a conversion is not possible. 5291 5292 Args: 5293 value: A python object. 5294 copy: Whether or not to copy `value` (only applies to Expressions and collections). 5295 5296 Returns: 5297 Expression: the equivalent expression object. 5298 """ 5299 if isinstance(value, Expression): 5300 return _maybe_copy(value, copy) 5301 if isinstance(value, str): 5302 return Literal.string(value) 5303 if isinstance(value, bool): 5304 return Boolean(this=value) 5305 if value is None or (isinstance(value, float) and math.isnan(value)): 5306 return NULL 5307 if isinstance(value, numbers.Number): 5308 return Literal.number(value) 5309 if isinstance(value, datetime.datetime): 5310 datetime_literal = Literal.string( 5311 (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat() 5312 ) 5313 return TimeStrToTime(this=datetime_literal) 5314 if isinstance(value, datetime.date): 5315 date_literal = Literal.string(value.strftime("%Y-%m-%d")) 5316 return DateStrToDate(this=date_literal) 5317 if isinstance(value, tuple): 5318 return Tuple(expressions=[convert(v, copy=copy) for v in value]) 5319 if isinstance(value, list): 5320 return Array(expressions=[convert(v, copy=copy) for v in value]) 5321 if isinstance(value, dict): 5322 return Map( 5323 keys=[convert(k, copy=copy) for k in value], 5324 values=[convert(v, copy=copy) for v in value.values()], 5325 ) 5326 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.
5329def replace_children(expression, fun, *args, **kwargs): 5330 """ 5331 Replace children of an expression with the result of a lambda fun(child) -> exp. 5332 """ 5333 for k, v in expression.args.items(): 5334 is_list_arg = type(v) is list 5335 5336 child_nodes = v if is_list_arg else [v] 5337 new_child_nodes = [] 5338 5339 for cn in child_nodes: 5340 if isinstance(cn, Expression): 5341 for child_node in ensure_collection(fun(cn, *args, **kwargs)): 5342 new_child_nodes.append(child_node) 5343 child_node.parent = expression 5344 child_node.arg_key = k 5345 else: 5346 new_child_nodes.append(cn) 5347 5348 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.
5351def column_table_names(expression): 5352 """ 5353 Return all table names referenced through columns in an expression. 5354 5355 Example: 5356 >>> import sqlglot 5357 >>> column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")) 5358 ['c', 'a'] 5359 5360 Args: 5361 expression (sqlglot.Expression): expression to find table names 5362 5363 Returns: 5364 list: A list of unique names 5365 """ 5366 return list(dict.fromkeys(column.table for column in expression.find_all(Column)))
Return all table names referenced through columns in an expression.
Example:
>>> import sqlglot >>> column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")) ['c', 'a']
Arguments:
- expression (sqlglot.Expression): expression to find table names
Returns:
list: A list of unique names
5369def table_name(table) -> str: 5370 """Get the full name of a table as a string. 5371 5372 Args: 5373 table (exp.Table | str): table expression node or string. 5374 5375 Examples: 5376 >>> from sqlglot import exp, parse_one 5377 >>> table_name(parse_one("select * from a.b.c").find(exp.Table)) 5378 'a.b.c' 5379 5380 Returns: 5381 The table name. 5382 """ 5383 5384 table = maybe_parse(table, into=Table) 5385 5386 if not table: 5387 raise ValueError(f"Cannot parse {table}") 5388 5389 return ".".join( 5390 part 5391 for part in ( 5392 table.text("catalog"), 5393 table.text("db"), 5394 table.name, 5395 ) 5396 if part 5397 )
Get the full name of a table as a string.
Arguments:
- table (exp.Table | str): table expression node or string.
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.
5400def replace_tables(expression, mapping): 5401 """Replace all tables in expression according to the mapping. 5402 5403 Args: 5404 expression (sqlglot.Expression): expression node to be transformed and replaced. 5405 mapping (Dict[str, str]): mapping of table names. 5406 5407 Examples: 5408 >>> from sqlglot import exp, parse_one 5409 >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql() 5410 'SELECT * FROM c' 5411 5412 Returns: 5413 The mapped expression. 5414 """ 5415 5416 def _replace_tables(node): 5417 if isinstance(node, Table): 5418 new_name = mapping.get(table_name(node)) 5419 if new_name: 5420 return to_table( 5421 new_name, 5422 **{k: v for k, v in node.args.items() if k not in ("this", "db", "catalog")}, 5423 ) 5424 return node 5425 5426 return expression.transform(_replace_tables)
Replace all tables in expression according to the mapping.
Arguments:
- expression (sqlglot.Expression): expression node to be transformed and replaced.
- mapping (Dict[str, str]): mapping of table names.
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.
5429def replace_placeholders(expression, *args, **kwargs): 5430 """Replace placeholders in an expression. 5431 5432 Args: 5433 expression (sqlglot.Expression): expression node to be transformed and replaced. 5434 args: positional names that will substitute unnamed placeholders in the given order. 5435 kwargs: keyword arguments that will substitute named placeholders. 5436 5437 Examples: 5438 >>> from sqlglot import exp, parse_one 5439 >>> replace_placeholders( 5440 ... parse_one("select * from :tbl where ? = ?"), 5441 ... exp.to_identifier("str_col"), "b", tbl=exp.to_identifier("foo") 5442 ... ).sql() 5443 "SELECT * FROM foo WHERE str_col = 'b'" 5444 5445 Returns: 5446 The mapped expression. 5447 """ 5448 5449 def _replace_placeholders(node, args, **kwargs): 5450 if isinstance(node, Placeholder): 5451 if node.name: 5452 new_name = kwargs.get(node.name) 5453 if new_name: 5454 return convert(new_name) 5455 else: 5456 try: 5457 return convert(next(args)) 5458 except StopIteration: 5459 pass 5460 return node 5461 5462 return expression.transform(_replace_placeholders, iter(args), **kwargs)
Replace placeholders in an expression.
Arguments:
- expression (sqlglot.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.
5465def expand( 5466 expression: Expression, sources: t.Dict[str, Subqueryable], copy: bool = True 5467) -> Expression: 5468 """Transforms an expression by expanding all referenced sources into subqueries. 5469 5470 Examples: 5471 >>> from sqlglot import parse_one 5472 >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql() 5473 'SELECT * FROM (SELECT * FROM y) AS z /* source: x */' 5474 5475 >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y"), "y": parse_one("select * from z")}).sql() 5476 'SELECT * FROM (SELECT * FROM (SELECT * FROM z) AS y /* source: y */) AS z /* source: x */' 5477 5478 Args: 5479 expression: The expression to expand. 5480 sources: A dictionary of name to Subqueryables. 5481 copy: Whether or not to copy the expression during transformation. Defaults to True. 5482 5483 Returns: 5484 The transformed expression. 5485 """ 5486 5487 def _expand(node: Expression): 5488 if isinstance(node, Table): 5489 name = table_name(node) 5490 source = sources.get(name) 5491 if source: 5492 subquery = source.subquery(node.alias or name) 5493 subquery.comments = [f"source: {name}"] 5494 return subquery.transform(_expand, copy=False) 5495 return node 5496 5497 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.
- copy: Whether or not to copy the expression during transformation. Defaults to True.
Returns:
The transformed expression.
5500def func(name: str, *args, dialect: DialectType = None, **kwargs) -> Func: 5501 """ 5502 Returns a Func expression. 5503 5504 Examples: 5505 >>> func("abs", 5).sql() 5506 'ABS(5)' 5507 5508 >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql() 5509 'CAST(5 AS DOUBLE)' 5510 5511 Args: 5512 name: the name of the function to build. 5513 args: the args used to instantiate the function of interest. 5514 dialect: the source dialect. 5515 kwargs: the kwargs used to instantiate the function of interest. 5516 5517 Note: 5518 The arguments `args` and `kwargs` are mutually exclusive. 5519 5520 Returns: 5521 An instance of the function of interest, or an anonymous function, if `name` doesn't 5522 correspond to an existing `sqlglot.expressions.Func` class. 5523 """ 5524 if args and kwargs: 5525 raise ValueError("Can't use both args and kwargs to instantiate a function.") 5526 5527 from sqlglot.dialects.dialect import Dialect 5528 5529 converted: t.List[Expression] = [maybe_parse(arg, dialect=dialect) for arg in args] 5530 kwargs = {key: maybe_parse(value, dialect=dialect) for key, value in kwargs.items()} 5531 5532 parser = Dialect.get_or_raise(dialect)().parser() 5533 from_args_list = parser.FUNCTIONS.get(name.upper()) 5534 5535 if from_args_list: 5536 function = from_args_list(converted) if converted else from_args_list.__self__(**kwargs) # type: ignore 5537 else: 5538 kwargs = kwargs or {"expressions": converted} 5539 function = Anonymous(this=name, **kwargs) 5540 5541 for error_message in function.error_messages(converted): 5542 raise ValueError(error_message) 5543 5544 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.
- dialect: the source dialect.
- kwargs: the kwargs used to instantiate the function of interest.
Note:
The arguments
argsandkwargsare mutually exclusive.
Returns:
An instance of the function of interest, or an anonymous function, if
namedoesn't correspond to an existingsqlglot.expressions.Funcclass.
5547def true(): 5548 """ 5549 Returns a true Boolean expression. 5550 """ 5551 return Boolean(this=True)
Returns a true Boolean expression.
5554def false(): 5555 """ 5556 Returns a false Boolean expression. 5557 """ 5558 return Boolean(this=False)
Returns a false Boolean expression.
Returns a Null expression.