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 seq_get, 30 split_num_words, 31 subclasses, 32) 33from sqlglot.tokens import Token 34 35if t.TYPE_CHECKING: 36 from sqlglot.dialects.dialect import DialectType 37 38 39class _Expression(type): 40 def __new__(cls, clsname, bases, attrs): 41 klass = super().__new__(cls, clsname, bases, attrs) 42 43 # When an Expression class is created, its key is automatically set to be 44 # the lowercase version of the class' name. 45 klass.key = clsname.lower() 46 47 # This is so that docstrings are not inherited in pdoc 48 klass.__doc__ = klass.__doc__ or "" 49 50 return klass 51 52 53class Expression(metaclass=_Expression): 54 """ 55 The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary 56 context, such as its child expressions, their names (arg keys), and whether a given child expression 57 is optional or not. 58 59 Attributes: 60 key: a unique key for each class in the Expression hierarchy. This is useful for hashing 61 and representing expressions as strings. 62 arg_types: determines what arguments (child nodes) are supported by an expression. It 63 maps arg keys to booleans that indicate whether the corresponding args are optional. 64 65 Example: 66 >>> class Foo(Expression): 67 ... arg_types = {"this": True, "expression": False} 68 69 The above definition informs us that Foo is an Expression that requires an argument called 70 "this" and may also optionally receive an argument called "expression". 71 72 Args: 73 args: a mapping used for retrieving the arguments of an expression, given their arg keys. 74 parent: a reference to the parent expression (or None, in case of root expressions). 75 arg_key: the arg key an expression is associated with, i.e. the name its parent expression 76 uses to refer to it. 77 comments: a list of comments that are associated with a given expression. This is used in 78 order to preserve comments when transpiling SQL code. 79 _type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the 80 optimizer, in order to enable some transformations that require type information. 81 """ 82 83 key = "expression" 84 arg_types = {"this": True} 85 __slots__ = ("args", "parent", "arg_key", "comments", "_type") 86 87 def __init__(self, **args: t.Any): 88 self.args: t.Dict[str, t.Any] = args 89 self.parent: t.Optional[Expression] = None 90 self.arg_key: t.Optional[str] = None 91 self.comments: t.Optional[t.List[str]] = None 92 self._type: t.Optional[DataType] = None 93 94 for arg_key, value in self.args.items(): 95 self._set_parent(arg_key, value) 96 97 def __eq__(self, other) -> bool: 98 return type(self) is type(other) and _norm_args(self) == _norm_args(other) 99 100 def __hash__(self) -> int: 101 return hash( 102 ( 103 self.key, 104 tuple( 105 (k, tuple(v) if isinstance(v, list) else v) for k, v in _norm_args(self).items() 106 ), 107 ) 108 ) 109 110 @property 111 def this(self): 112 """ 113 Retrieves the argument with key "this". 114 """ 115 return self.args.get("this") 116 117 @property 118 def expression(self): 119 """ 120 Retrieves the argument with key "expression". 121 """ 122 return self.args.get("expression") 123 124 @property 125 def expressions(self): 126 """ 127 Retrieves the argument with key "expressions". 128 """ 129 return self.args.get("expressions") or [] 130 131 def text(self, key) -> str: 132 """ 133 Returns a textual representation of the argument corresponding to "key". This can only be used 134 for args that are strings or leaf Expression instances, such as identifiers and literals. 135 """ 136 field = self.args.get(key) 137 if isinstance(field, str): 138 return field 139 if isinstance(field, (Identifier, Literal, Var)): 140 return field.this 141 if isinstance(field, (Star, Null)): 142 return field.name 143 return "" 144 145 @property 146 def is_string(self) -> bool: 147 """ 148 Checks whether a Literal expression is a string. 149 """ 150 return isinstance(self, Literal) and self.args["is_string"] 151 152 @property 153 def is_number(self) -> bool: 154 """ 155 Checks whether a Literal expression is a number. 156 """ 157 return isinstance(self, Literal) and not self.args["is_string"] 158 159 @property 160 def is_int(self) -> bool: 161 """ 162 Checks whether a Literal expression is an integer. 163 """ 164 if self.is_number: 165 try: 166 int(self.name) 167 return True 168 except ValueError: 169 pass 170 return False 171 172 @property 173 def is_star(self) -> bool: 174 """Checks whether an expression is a star.""" 175 return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star)) 176 177 @property 178 def alias(self) -> str: 179 """ 180 Returns the alias of the expression, or an empty string if it's not aliased. 181 """ 182 if isinstance(self.args.get("alias"), TableAlias): 183 return self.args["alias"].name 184 return self.text("alias") 185 186 @property 187 def name(self) -> str: 188 return self.text("this") 189 190 @property 191 def alias_or_name(self): 192 return self.alias or self.name 193 194 @property 195 def output_name(self): 196 """ 197 Name of the output column if this expression is a selection. 198 199 If the Expression has no output name, an empty string is returned. 200 201 Example: 202 >>> from sqlglot import parse_one 203 >>> parse_one("SELECT a").expressions[0].output_name 204 'a' 205 >>> parse_one("SELECT b AS c").expressions[0].output_name 206 'c' 207 >>> parse_one("SELECT 1 + 2").expressions[0].output_name 208 '' 209 """ 210 return "" 211 212 @property 213 def type(self) -> t.Optional[DataType]: 214 return self._type 215 216 @type.setter 217 def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None: 218 if dtype and not isinstance(dtype, DataType): 219 dtype = DataType.build(dtype) 220 self._type = dtype # type: ignore 221 222 def __deepcopy__(self, memo): 223 copy = self.__class__(**deepcopy(self.args)) 224 copy.comments = self.comments 225 copy.type = self.type 226 return copy 227 228 def copy(self): 229 """ 230 Returns a deep copy of the expression. 231 """ 232 new = deepcopy(self) 233 new.parent = self.parent 234 for item, parent, _ in new.bfs(): 235 if isinstance(item, Expression) and parent: 236 item.parent = parent 237 return new 238 239 def append(self, arg_key, value): 240 """ 241 Appends value to arg_key if it's a list or sets it as a new list. 242 243 Args: 244 arg_key (str): name of the list expression arg 245 value (Any): value to append to the list 246 """ 247 if not isinstance(self.args.get(arg_key), list): 248 self.args[arg_key] = [] 249 self.args[arg_key].append(value) 250 self._set_parent(arg_key, value) 251 252 def set(self, arg_key, value): 253 """ 254 Sets `arg_key` to `value`. 255 256 Args: 257 arg_key (str): name of the expression arg. 258 value: value to set the arg to. 259 """ 260 self.args[arg_key] = value 261 self._set_parent(arg_key, value) 262 263 def _set_parent(self, arg_key, value): 264 if isinstance(value, Expression): 265 value.parent = self 266 value.arg_key = arg_key 267 elif isinstance(value, list): 268 for v in value: 269 if isinstance(v, Expression): 270 v.parent = self 271 v.arg_key = arg_key 272 273 @property 274 def depth(self): 275 """ 276 Returns the depth of this tree. 277 """ 278 if self.parent: 279 return self.parent.depth + 1 280 return 0 281 282 def find(self, *expression_types, bfs=True): 283 """ 284 Returns the first node in this tree which matches at least one of 285 the specified types. 286 287 Args: 288 expression_types (type): the expression type(s) to match. 289 290 Returns: 291 The node which matches the criteria or None if no such node was found. 292 """ 293 return next(self.find_all(*expression_types, bfs=bfs), None) 294 295 def find_all(self, *expression_types, bfs=True): 296 """ 297 Returns a generator object which visits all nodes in this tree and only 298 yields those that match at least one of the specified expression types. 299 300 Args: 301 expression_types (type): the expression type(s) to match. 302 303 Returns: 304 The generator object. 305 """ 306 for expression, _, _ in self.walk(bfs=bfs): 307 if isinstance(expression, expression_types): 308 yield expression 309 310 def find_ancestor(self, *expression_types): 311 """ 312 Returns a nearest parent matching expression_types. 313 314 Args: 315 expression_types (type): the expression type(s) to match. 316 317 Returns: 318 The parent node. 319 """ 320 ancestor = self.parent 321 while ancestor and not isinstance(ancestor, expression_types): 322 ancestor = ancestor.parent 323 return ancestor 324 325 @property 326 def parent_select(self): 327 """ 328 Returns the parent select statement. 329 """ 330 return self.find_ancestor(Select) 331 332 def walk(self, bfs=True, prune=None): 333 """ 334 Returns a generator object which visits all nodes in this tree. 335 336 Args: 337 bfs (bool): if set to True the BFS traversal order will be applied, 338 otherwise the DFS traversal will be used instead. 339 prune ((node, parent, arg_key) -> bool): callable that returns True if 340 the generator should stop traversing this branch of the tree. 341 342 Returns: 343 the generator object. 344 """ 345 if bfs: 346 yield from self.bfs(prune=prune) 347 else: 348 yield from self.dfs(prune=prune) 349 350 def dfs(self, parent=None, key=None, prune=None): 351 """ 352 Returns a generator object which visits all nodes in this tree in 353 the DFS (Depth-first) order. 354 355 Returns: 356 The generator object. 357 """ 358 parent = parent or self.parent 359 yield self, parent, key 360 if prune and prune(self, parent, key): 361 return 362 363 for k, v in self.args.items(): 364 for node in ensure_collection(v): 365 if isinstance(node, Expression): 366 yield from node.dfs(self, k, prune) 367 368 def bfs(self, prune=None): 369 """ 370 Returns a generator object which visits all nodes in this tree in 371 the BFS (Breadth-first) order. 372 373 Returns: 374 The generator object. 375 """ 376 queue = deque([(self, self.parent, None)]) 377 378 while queue: 379 item, parent, key = queue.popleft() 380 381 yield item, parent, key 382 if prune and prune(item, parent, key): 383 continue 384 385 if isinstance(item, Expression): 386 for k, v in item.args.items(): 387 for node in ensure_collection(v): 388 if isinstance(node, Expression): 389 queue.append((node, item, k)) 390 391 def unnest(self): 392 """ 393 Returns the first non parenthesis child or self. 394 """ 395 expression = self 396 while isinstance(expression, Paren): 397 expression = expression.this 398 return expression 399 400 def unalias(self): 401 """ 402 Returns the inner expression if this is an Alias. 403 """ 404 if isinstance(self, Alias): 405 return self.this 406 return self 407 408 def unnest_operands(self): 409 """ 410 Returns unnested operands as a tuple. 411 """ 412 return tuple(arg.unnest() for arg in self.args.values() if arg) 413 414 def flatten(self, unnest=True): 415 """ 416 Returns a generator which yields child nodes who's parents are the same class. 417 418 A AND B AND C -> [A, B, C] 419 """ 420 for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not isinstance(n, self.__class__)): 421 if not isinstance(node, self.__class__): 422 yield node.unnest() if unnest else node 423 424 def __str__(self): 425 return self.sql() 426 427 def __repr__(self): 428 return self._to_s() 429 430 def sql(self, dialect: DialectType = None, **opts) -> str: 431 """ 432 Returns SQL string representation of this tree. 433 434 Args: 435 dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql"). 436 opts: other `sqlglot.generator.Generator` options. 437 438 Returns: 439 The SQL string. 440 """ 441 from sqlglot.dialects import Dialect 442 443 return Dialect.get_or_raise(dialect)().generate(self, **opts) 444 445 def _to_s(self, hide_missing: bool = True, level: int = 0) -> str: 446 indent = "" if not level else "\n" 447 indent += "".join([" "] * level) 448 left = f"({self.key.upper()} " 449 450 args: t.Dict[str, t.Any] = { 451 k: ", ".join( 452 v._to_s(hide_missing=hide_missing, level=level + 1) 453 if hasattr(v, "_to_s") 454 else str(v) 455 for v in ensure_collection(vs) 456 if v is not None 457 ) 458 for k, vs in self.args.items() 459 } 460 args["comments"] = self.comments 461 args["type"] = self.type 462 args = {k: v for k, v in args.items() if v or not hide_missing} 463 464 right = ", ".join(f"{k}: {v}" for k, v in args.items()) 465 right += ")" 466 467 return indent + left + right 468 469 def transform(self, fun, *args, copy=True, **kwargs): 470 """ 471 Recursively visits all tree nodes (excluding already transformed ones) 472 and applies the given transformation function to each node. 473 474 Args: 475 fun (function): a function which takes a node as an argument and returns a 476 new transformed node or the same node without modifications. If the function 477 returns None, then the corresponding node will be removed from the syntax tree. 478 copy (bool): if set to True a new tree instance is constructed, otherwise the tree is 479 modified in place. 480 481 Returns: 482 The transformed tree. 483 """ 484 node = self.copy() if copy else self 485 new_node = fun(node, *args, **kwargs) 486 487 if new_node is None or not isinstance(new_node, Expression): 488 return new_node 489 if new_node is not node: 490 new_node.parent = node.parent 491 return new_node 492 493 replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs)) 494 return new_node 495 496 def replace(self, expression): 497 """ 498 Swap out this expression with a new expression. 499 500 For example:: 501 502 >>> tree = Select().select("x").from_("tbl") 503 >>> tree.find(Column).replace(Column(this="y")) 504 (COLUMN this: y) 505 >>> tree.sql() 506 'SELECT y FROM tbl' 507 508 Args: 509 expression (Expression|None): new node 510 511 Returns: 512 The new expression or expressions. 513 """ 514 if not self.parent: 515 return expression 516 517 parent = self.parent 518 self.parent = None 519 520 replace_children(parent, lambda child: expression if child is self else child) 521 return expression 522 523 def pop(self): 524 """ 525 Remove this expression from its AST. 526 """ 527 self.replace(None) 528 529 def assert_is(self, type_): 530 """ 531 Assert that this `Expression` is an instance of `type_`. 532 533 If it is NOT an instance of `type_`, this raises an assertion error. 534 Otherwise, this returns this expression. 535 536 Examples: 537 This is useful for type security in chained expressions: 538 539 >>> import sqlglot 540 >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql() 541 'SELECT x, z FROM y' 542 """ 543 assert isinstance(self, type_) 544 return self 545 546 def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]: 547 """ 548 Checks if this expression is valid (e.g. all mandatory args are set). 549 550 Args: 551 args: a sequence of values that were used to instantiate a Func expression. This is used 552 to check that the provided arguments don't exceed the function argument limit. 553 554 Returns: 555 A list of error messages for all possible errors that were found. 556 """ 557 errors: t.List[str] = [] 558 559 for k in self.args: 560 if k not in self.arg_types: 561 errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") 562 for k, mandatory in self.arg_types.items(): 563 v = self.args.get(k) 564 if mandatory and (v is None or (isinstance(v, list) and not v)): 565 errors.append(f"Required keyword: '{k}' missing for {self.__class__}") 566 567 if ( 568 args 569 and isinstance(self, Func) 570 and len(args) > len(self.arg_types) 571 and not self.is_var_len_args 572 ): 573 errors.append( 574 f"The number of provided arguments ({len(args)}) is greater than " 575 f"the maximum number of supported arguments ({len(self.arg_types)})" 576 ) 577 578 return errors 579 580 def dump(self): 581 """ 582 Dump this Expression to a JSON-serializable dict. 583 """ 584 from sqlglot.serde import dump 585 586 return dump(self) 587 588 @classmethod 589 def load(cls, obj): 590 """ 591 Load a dict (as returned by `Expression.dump`) into an Expression instance. 592 """ 593 from sqlglot.serde import load 594 595 return load(obj) 596 597 598IntoType = t.Union[ 599 str, 600 t.Type[Expression], 601 t.Collection[t.Union[str, t.Type[Expression]]], 602] 603 604 605class Condition(Expression): 606 def and_(self, *expressions, dialect=None, **opts): 607 """ 608 AND this condition with one or multiple expressions. 609 610 Example: 611 >>> condition("x=1").and_("y=1").sql() 612 'x = 1 AND y = 1' 613 614 Args: 615 *expressions (str | Expression): the SQL code strings to parse. 616 If an `Expression` instance is passed, it will be used as-is. 617 dialect (str): the dialect used to parse the input expression. 618 opts (kwargs): other options to use to parse the input expressions. 619 620 Returns: 621 And: the new condition. 622 """ 623 return and_(self, *expressions, dialect=dialect, **opts) 624 625 def or_(self, *expressions, dialect=None, **opts): 626 """ 627 OR this condition with one or multiple expressions. 628 629 Example: 630 >>> condition("x=1").or_("y=1").sql() 631 'x = 1 OR y = 1' 632 633 Args: 634 *expressions (str | Expression): the SQL code strings to parse. 635 If an `Expression` instance is passed, it will be used as-is. 636 dialect (str): the dialect used to parse the input expression. 637 opts (kwargs): other options to use to parse the input expressions. 638 639 Returns: 640 Or: the new condition. 641 """ 642 return or_(self, *expressions, dialect=dialect, **opts) 643 644 def not_(self): 645 """ 646 Wrap this condition with NOT. 647 648 Example: 649 >>> condition("x=1").not_().sql() 650 'NOT x = 1' 651 652 Returns: 653 Not: the new condition. 654 """ 655 return not_(self) 656 657 658class Predicate(Condition): 659 """Relationships like x = y, x > 1, x >= y.""" 660 661 662class DerivedTable(Expression): 663 @property 664 def alias_column_names(self): 665 table_alias = self.args.get("alias") 666 if not table_alias: 667 return [] 668 column_list = table_alias.assert_is(TableAlias).args.get("columns") or [] 669 return [c.name for c in column_list] 670 671 @property 672 def selects(self): 673 alias = self.args.get("alias") 674 675 if alias: 676 return alias.columns 677 return [] 678 679 @property 680 def named_selects(self): 681 return [select.output_name for select in self.selects] 682 683 684class Unionable(Expression): 685 def union(self, expression, distinct=True, dialect=None, **opts): 686 """ 687 Builds a UNION expression. 688 689 Example: 690 >>> import sqlglot 691 >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql() 692 'SELECT * FROM foo UNION SELECT * FROM bla' 693 694 Args: 695 expression (str | Expression): the SQL code string. 696 If an `Expression` instance is passed, it will be used as-is. 697 distinct (bool): set the DISTINCT flag if and only if this is true. 698 dialect (str): the dialect used to parse the input expression. 699 opts (kwargs): other options to use to parse the input expressions. 700 Returns: 701 Union: the Union expression. 702 """ 703 return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 704 705 def intersect(self, expression, distinct=True, dialect=None, **opts): 706 """ 707 Builds an INTERSECT expression. 708 709 Example: 710 >>> import sqlglot 711 >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql() 712 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 713 714 Args: 715 expression (str | Expression): the SQL code string. 716 If an `Expression` instance is passed, it will be used as-is. 717 distinct (bool): set the DISTINCT flag if and only if this is true. 718 dialect (str): the dialect used to parse the input expression. 719 opts (kwargs): other options to use to parse the input expressions. 720 Returns: 721 Intersect: the Intersect expression 722 """ 723 return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 724 725 def except_(self, expression, distinct=True, dialect=None, **opts): 726 """ 727 Builds an EXCEPT expression. 728 729 Example: 730 >>> import sqlglot 731 >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql() 732 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 733 734 Args: 735 expression (str | Expression): the SQL code string. 736 If an `Expression` instance is passed, it will be used as-is. 737 distinct (bool): set the DISTINCT flag if and only if this is true. 738 dialect (str): the dialect used to parse the input expression. 739 opts (kwargs): other options to use to parse the input expressions. 740 Returns: 741 Except: the Except expression 742 """ 743 return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 744 745 746class UDTF(DerivedTable, Unionable): 747 pass 748 749 750class Cache(Expression): 751 arg_types = { 752 "with": False, 753 "this": True, 754 "lazy": False, 755 "options": False, 756 "expression": False, 757 } 758 759 760class Uncache(Expression): 761 arg_types = {"this": True, "exists": False} 762 763 764class Create(Expression): 765 arg_types = { 766 "with": False, 767 "this": True, 768 "kind": True, 769 "expression": False, 770 "set": False, 771 "multiset": False, 772 "global_temporary": False, 773 "volatile": False, 774 "exists": False, 775 "properties": False, 776 "temporary": False, 777 "transient": False, 778 "external": False, 779 "replace": False, 780 "unique": False, 781 "materialized": False, 782 "data": False, 783 "statistics": False, 784 "no_primary_index": False, 785 "indexes": False, 786 "no_schema_binding": False, 787 "begin": False, 788 } 789 790 791class Describe(Expression): 792 arg_types = {"this": True, "kind": False} 793 794 795class Set(Expression): 796 arg_types = {"expressions": True} 797 798 799class SetItem(Expression): 800 arg_types = { 801 "this": False, 802 "expressions": False, 803 "kind": False, 804 "collate": False, # MySQL SET NAMES statement 805 "global": False, 806 } 807 808 809class Show(Expression): 810 arg_types = { 811 "this": True, 812 "target": False, 813 "offset": False, 814 "limit": False, 815 "like": False, 816 "where": False, 817 "db": False, 818 "full": False, 819 "mutex": False, 820 "query": False, 821 "channel": False, 822 "global": False, 823 "log": False, 824 "position": False, 825 "types": False, 826 } 827 828 829class UserDefinedFunction(Expression): 830 arg_types = {"this": True, "expressions": False, "wrapped": False} 831 832 833class CharacterSet(Expression): 834 arg_types = {"this": True, "default": False} 835 836 837class With(Expression): 838 arg_types = {"expressions": True, "recursive": False} 839 840 @property 841 def recursive(self) -> bool: 842 return bool(self.args.get("recursive")) 843 844 845class WithinGroup(Expression): 846 arg_types = {"this": True, "expression": False} 847 848 849class CTE(DerivedTable): 850 arg_types = {"this": True, "alias": True} 851 852 853class TableAlias(Expression): 854 arg_types = {"this": False, "columns": False} 855 856 @property 857 def columns(self): 858 return self.args.get("columns") or [] 859 860 861class BitString(Condition): 862 pass 863 864 865class HexString(Condition): 866 pass 867 868 869class ByteString(Condition): 870 pass 871 872 873class Column(Condition): 874 arg_types = {"this": True, "table": False, "db": False, "catalog": False} 875 876 @property 877 def table(self) -> str: 878 return self.text("table") 879 880 @property 881 def db(self) -> str: 882 return self.text("db") 883 884 @property 885 def catalog(self) -> str: 886 return self.text("catalog") 887 888 @property 889 def output_name(self) -> str: 890 return self.name 891 892 893class ColumnDef(Expression): 894 arg_types = { 895 "this": True, 896 "kind": False, 897 "constraints": False, 898 "exists": False, 899 } 900 901 902class AlterColumn(Expression): 903 arg_types = { 904 "this": True, 905 "dtype": False, 906 "collate": False, 907 "using": False, 908 "default": False, 909 "drop": False, 910 } 911 912 913class RenameTable(Expression): 914 pass 915 916 917class ColumnConstraint(Expression): 918 arg_types = {"this": False, "kind": True} 919 920 921class ColumnConstraintKind(Expression): 922 pass 923 924 925class AutoIncrementColumnConstraint(ColumnConstraintKind): 926 pass 927 928 929class CaseSpecificColumnConstraint(ColumnConstraintKind): 930 arg_types = {"not_": True} 931 932 933class CharacterSetColumnConstraint(ColumnConstraintKind): 934 arg_types = {"this": True} 935 936 937class CheckColumnConstraint(ColumnConstraintKind): 938 pass 939 940 941class CollateColumnConstraint(ColumnConstraintKind): 942 pass 943 944 945class CommentColumnConstraint(ColumnConstraintKind): 946 pass 947 948 949class DateFormatColumnConstraint(ColumnConstraintKind): 950 arg_types = {"this": True} 951 952 953class DefaultColumnConstraint(ColumnConstraintKind): 954 pass 955 956 957class EncodeColumnConstraint(ColumnConstraintKind): 958 pass 959 960 961class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind): 962 # this: True -> ALWAYS, this: False -> BY DEFAULT 963 arg_types = { 964 "this": False, 965 "start": False, 966 "increment": False, 967 "minvalue": False, 968 "maxvalue": False, 969 "cycle": False, 970 } 971 972 973class NotNullColumnConstraint(ColumnConstraintKind): 974 arg_types = {"allow_null": False} 975 976 977class PrimaryKeyColumnConstraint(ColumnConstraintKind): 978 arg_types = {"desc": False} 979 980 981class TitleColumnConstraint(ColumnConstraintKind): 982 pass 983 984 985class UniqueColumnConstraint(ColumnConstraintKind): 986 arg_types: t.Dict[str, t.Any] = {} 987 988 989class UppercaseColumnConstraint(ColumnConstraintKind): 990 arg_types: t.Dict[str, t.Any] = {} 991 992 993class PathColumnConstraint(ColumnConstraintKind): 994 pass 995 996 997class Constraint(Expression): 998 arg_types = {"this": True, "expressions": True} 999 1000 1001class Delete(Expression): 1002 arg_types = {"with": False, "this": False, "using": False, "where": False} 1003 1004 1005class Drop(Expression): 1006 arg_types = { 1007 "this": False, 1008 "kind": False, 1009 "exists": False, 1010 "temporary": False, 1011 "materialized": False, 1012 "cascade": False, 1013 } 1014 1015 1016class Filter(Expression): 1017 arg_types = {"this": True, "expression": True} 1018 1019 1020class Check(Expression): 1021 pass 1022 1023 1024class Directory(Expression): 1025 # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html 1026 arg_types = {"this": True, "local": False, "row_format": False} 1027 1028 1029class ForeignKey(Expression): 1030 arg_types = { 1031 "expressions": True, 1032 "reference": False, 1033 "delete": False, 1034 "update": False, 1035 } 1036 1037 1038class PrimaryKey(Expression): 1039 arg_types = {"expressions": True, "options": False} 1040 1041 1042class Unique(Expression): 1043 arg_types = {"expressions": True} 1044 1045 1046# https://www.postgresql.org/docs/9.1/sql-selectinto.html 1047# https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_INTO.html#r_SELECT_INTO-examples 1048class Into(Expression): 1049 arg_types = {"this": True, "temporary": False, "unlogged": False} 1050 1051 1052class From(Expression): 1053 arg_types = {"expressions": True} 1054 1055 1056class Having(Expression): 1057 pass 1058 1059 1060class Hint(Expression): 1061 arg_types = {"expressions": True} 1062 1063 1064class JoinHint(Expression): 1065 arg_types = {"this": True, "expressions": True} 1066 1067 1068class Identifier(Expression): 1069 arg_types = {"this": True, "quoted": False} 1070 1071 @property 1072 def quoted(self): 1073 return bool(self.args.get("quoted")) 1074 1075 def __eq__(self, other): 1076 return isinstance(other, self.__class__) and _norm_arg(self.this) == _norm_arg(other.this) 1077 1078 def __hash__(self): 1079 return hash((self.key, self.this.lower())) 1080 1081 @property 1082 def output_name(self): 1083 return self.name 1084 1085 1086class Index(Expression): 1087 arg_types = { 1088 "this": False, 1089 "table": False, 1090 "where": False, 1091 "columns": False, 1092 "unique": False, 1093 "primary": False, 1094 "amp": False, # teradata 1095 } 1096 1097 1098class Insert(Expression): 1099 arg_types = { 1100 "with": False, 1101 "this": True, 1102 "expression": False, 1103 "overwrite": False, 1104 "exists": False, 1105 "partition": False, 1106 "alternative": False, 1107 } 1108 1109 1110# https://dev.mysql.com/doc/refman/8.0/en/charset-introducer.html 1111class Introducer(Expression): 1112 arg_types = {"this": True, "expression": True} 1113 1114 1115# national char, like n'utf8' 1116class National(Expression): 1117 pass 1118 1119 1120class LoadData(Expression): 1121 arg_types = { 1122 "this": True, 1123 "local": False, 1124 "overwrite": False, 1125 "inpath": True, 1126 "partition": False, 1127 "input_format": False, 1128 "serde": False, 1129 } 1130 1131 1132class Partition(Expression): 1133 arg_types = {"expressions": True} 1134 1135 1136class Fetch(Expression): 1137 arg_types = {"direction": False, "count": False} 1138 1139 1140class Group(Expression): 1141 arg_types = { 1142 "expressions": False, 1143 "grouping_sets": False, 1144 "cube": False, 1145 "rollup": False, 1146 } 1147 1148 1149class Lambda(Expression): 1150 arg_types = {"this": True, "expressions": True} 1151 1152 1153class Limit(Expression): 1154 arg_types = {"this": False, "expression": True} 1155 1156 1157class Literal(Condition): 1158 arg_types = {"this": True, "is_string": True} 1159 1160 def __eq__(self, other): 1161 return ( 1162 isinstance(other, Literal) 1163 and self.this == other.this 1164 and self.args["is_string"] == other.args["is_string"] 1165 ) 1166 1167 def __hash__(self): 1168 return hash((self.key, self.this, self.args["is_string"])) 1169 1170 @classmethod 1171 def number(cls, number) -> Literal: 1172 return cls(this=str(number), is_string=False) 1173 1174 @classmethod 1175 def string(cls, string) -> Literal: 1176 return cls(this=str(string), is_string=True) 1177 1178 @property 1179 def output_name(self): 1180 return self.name 1181 1182 1183class Join(Expression): 1184 arg_types = { 1185 "this": True, 1186 "on": False, 1187 "side": False, 1188 "kind": False, 1189 "using": False, 1190 "natural": False, 1191 } 1192 1193 @property 1194 def kind(self): 1195 return self.text("kind").upper() 1196 1197 @property 1198 def side(self): 1199 return self.text("side").upper() 1200 1201 @property 1202 def alias_or_name(self): 1203 return self.this.alias_or_name 1204 1205 def on(self, *expressions, append=True, dialect=None, copy=True, **opts): 1206 """ 1207 Append to or set the ON expressions. 1208 1209 Example: 1210 >>> import sqlglot 1211 >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql() 1212 'JOIN x ON y = 1' 1213 1214 Args: 1215 *expressions (str | Expression): the SQL code strings to parse. 1216 If an `Expression` instance is passed, it will be used as-is. 1217 Multiple expressions are combined with an AND operator. 1218 append (bool): if `True`, AND the new expressions to any existing expression. 1219 Otherwise, this resets the expression. 1220 dialect (str): the dialect used to parse the input expressions. 1221 copy (bool): if `False`, modify this expression instance in-place. 1222 opts (kwargs): other options to use to parse the input expressions. 1223 1224 Returns: 1225 Join: the modified join expression. 1226 """ 1227 join = _apply_conjunction_builder( 1228 *expressions, 1229 instance=self, 1230 arg="on", 1231 append=append, 1232 dialect=dialect, 1233 copy=copy, 1234 **opts, 1235 ) 1236 1237 if join.kind == "CROSS": 1238 join.set("kind", None) 1239 1240 return join 1241 1242 def using(self, *expressions, append=True, dialect=None, copy=True, **opts): 1243 """ 1244 Append to or set the USING expressions. 1245 1246 Example: 1247 >>> import sqlglot 1248 >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 1249 'JOIN x USING (foo, bla)' 1250 1251 Args: 1252 *expressions (str | Expression): the SQL code strings to parse. 1253 If an `Expression` instance is passed, it will be used as-is. 1254 append (bool): if `True`, concatenate the new expressions to the existing "using" list. 1255 Otherwise, this resets the expression. 1256 dialect (str): the dialect used to parse the input expressions. 1257 copy (bool): if `False`, modify this expression instance in-place. 1258 opts (kwargs): other options to use to parse the input expressions. 1259 1260 Returns: 1261 Join: the modified join expression. 1262 """ 1263 join = _apply_list_builder( 1264 *expressions, 1265 instance=self, 1266 arg="using", 1267 append=append, 1268 dialect=dialect, 1269 copy=copy, 1270 **opts, 1271 ) 1272 1273 if join.kind == "CROSS": 1274 join.set("kind", None) 1275 1276 return join 1277 1278 1279class Lateral(UDTF): 1280 arg_types = {"this": True, "view": False, "outer": False, "alias": False} 1281 1282 1283class MatchRecognize(Expression): 1284 arg_types = { 1285 "partition_by": False, 1286 "order": False, 1287 "measures": False, 1288 "rows": False, 1289 "after": False, 1290 "pattern": False, 1291 "define": False, 1292 } 1293 1294 1295# Clickhouse FROM FINAL modifier 1296# https://clickhouse.com/docs/en/sql-reference/statements/select/from/#final-modifier 1297class Final(Expression): 1298 pass 1299 1300 1301class Offset(Expression): 1302 arg_types = {"this": False, "expression": True} 1303 1304 1305class Order(Expression): 1306 arg_types = {"this": False, "expressions": True} 1307 1308 1309# hive specific sorts 1310# https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy 1311class Cluster(Order): 1312 pass 1313 1314 1315class Distribute(Order): 1316 pass 1317 1318 1319class Sort(Order): 1320 pass 1321 1322 1323class Ordered(Expression): 1324 arg_types = {"this": True, "desc": True, "nulls_first": True} 1325 1326 1327class Property(Expression): 1328 arg_types = {"this": True, "value": True} 1329 1330 1331class AlgorithmProperty(Property): 1332 arg_types = {"this": True} 1333 1334 1335class DefinerProperty(Property): 1336 arg_types = {"this": True} 1337 1338 1339class SqlSecurityProperty(Property): 1340 arg_types = {"definer": True} 1341 1342 1343class TableFormatProperty(Property): 1344 arg_types = {"this": True} 1345 1346 1347class PartitionedByProperty(Property): 1348 arg_types = {"this": True} 1349 1350 1351class FileFormatProperty(Property): 1352 arg_types = {"this": True} 1353 1354 1355class DistKeyProperty(Property): 1356 arg_types = {"this": True} 1357 1358 1359class SortKeyProperty(Property): 1360 arg_types = {"this": True, "compound": False} 1361 1362 1363class DistStyleProperty(Property): 1364 arg_types = {"this": True} 1365 1366 1367class LikeProperty(Property): 1368 arg_types = {"this": True, "expressions": False} 1369 1370 1371class LocationProperty(Property): 1372 arg_types = {"this": True} 1373 1374 1375class EngineProperty(Property): 1376 arg_types = {"this": True} 1377 1378 1379class AutoIncrementProperty(Property): 1380 arg_types = {"this": True} 1381 1382 1383class CharacterSetProperty(Property): 1384 arg_types = {"this": True, "default": True} 1385 1386 1387class CollateProperty(Property): 1388 arg_types = {"this": True} 1389 1390 1391class SchemaCommentProperty(Property): 1392 arg_types = {"this": True} 1393 1394 1395class ReturnsProperty(Property): 1396 arg_types = {"this": True, "is_table": False, "table": False} 1397 1398 1399class LanguageProperty(Property): 1400 arg_types = {"this": True} 1401 1402 1403class ExecuteAsProperty(Property): 1404 arg_types = {"this": True} 1405 1406 1407class VolatilityProperty(Property): 1408 arg_types = {"this": True} 1409 1410 1411class RowFormatDelimitedProperty(Property): 1412 # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml 1413 arg_types = { 1414 "fields": False, 1415 "escaped": False, 1416 "collection_items": False, 1417 "map_keys": False, 1418 "lines": False, 1419 "null": False, 1420 "serde": False, 1421 } 1422 1423 1424class RowFormatSerdeProperty(Property): 1425 arg_types = {"this": True} 1426 1427 1428class SerdeProperties(Property): 1429 arg_types = {"expressions": True} 1430 1431 1432class FallbackProperty(Property): 1433 arg_types = {"no": True, "protection": False} 1434 1435 1436class WithJournalTableProperty(Property): 1437 arg_types = {"this": True} 1438 1439 1440class LogProperty(Property): 1441 arg_types = {"no": True} 1442 1443 1444class JournalProperty(Property): 1445 arg_types = {"no": True, "dual": False, "before": False} 1446 1447 1448class AfterJournalProperty(Property): 1449 arg_types = {"no": True, "dual": False, "local": False} 1450 1451 1452class ChecksumProperty(Property): 1453 arg_types = {"on": False, "default": False} 1454 1455 1456class FreespaceProperty(Property): 1457 arg_types = {"this": True, "percent": False} 1458 1459 1460class MergeBlockRatioProperty(Property): 1461 arg_types = {"this": False, "no": False, "default": False, "percent": False} 1462 1463 1464class DataBlocksizeProperty(Property): 1465 arg_types = {"size": False, "units": False, "min": False, "default": False} 1466 1467 1468class BlockCompressionProperty(Property): 1469 arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True} 1470 1471 1472class IsolatedLoadingProperty(Property): 1473 arg_types = { 1474 "no": True, 1475 "concurrent": True, 1476 "for_all": True, 1477 "for_insert": True, 1478 "for_none": True, 1479 } 1480 1481 1482class LockingProperty(Property): 1483 arg_types = { 1484 "this": False, 1485 "kind": True, 1486 "for_or_in": True, 1487 "lock_type": True, 1488 "override": False, 1489 } 1490 1491 1492class Properties(Expression): 1493 arg_types = {"expressions": True} 1494 1495 NAME_TO_PROPERTY = { 1496 "ALGORITHM": AlgorithmProperty, 1497 "AUTO_INCREMENT": AutoIncrementProperty, 1498 "CHARACTER SET": CharacterSetProperty, 1499 "COLLATE": CollateProperty, 1500 "COMMENT": SchemaCommentProperty, 1501 "DEFINER": DefinerProperty, 1502 "DISTKEY": DistKeyProperty, 1503 "DISTSTYLE": DistStyleProperty, 1504 "ENGINE": EngineProperty, 1505 "EXECUTE AS": ExecuteAsProperty, 1506 "FORMAT": FileFormatProperty, 1507 "LANGUAGE": LanguageProperty, 1508 "LOCATION": LocationProperty, 1509 "PARTITIONED_BY": PartitionedByProperty, 1510 "RETURNS": ReturnsProperty, 1511 "SORTKEY": SortKeyProperty, 1512 "TABLE_FORMAT": TableFormatProperty, 1513 } 1514 1515 PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()} 1516 1517 # CREATE property locations 1518 # Form: schema specified 1519 # create [POST_CREATE] 1520 # table a [POST_NAME] 1521 # (b int) [POST_SCHEMA] 1522 # with ([POST_WITH]) 1523 # index (b) [POST_INDEX] 1524 # 1525 # Form: alias selection 1526 # create [POST_CREATE] 1527 # table a [POST_NAME] 1528 # as [POST_ALIAS] (select * from b) 1529 # index (c) [POST_INDEX] 1530 class Location(AutoName): 1531 POST_CREATE = auto() 1532 POST_NAME = auto() 1533 POST_SCHEMA = auto() 1534 POST_WITH = auto() 1535 POST_ALIAS = auto() 1536 POST_INDEX = auto() 1537 UNSUPPORTED = auto() 1538 1539 @classmethod 1540 def from_dict(cls, properties_dict) -> Properties: 1541 expressions = [] 1542 for key, value in properties_dict.items(): 1543 property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) 1544 if property_cls: 1545 expressions.append(property_cls(this=convert(value))) 1546 else: 1547 expressions.append(Property(this=Literal.string(key), value=convert(value))) 1548 1549 return cls(expressions=expressions) 1550 1551 1552class Qualify(Expression): 1553 pass 1554 1555 1556# https://www.ibm.com/docs/en/ias?topic=procedures-return-statement-in-sql 1557class Return(Expression): 1558 pass 1559 1560 1561class Reference(Expression): 1562 arg_types = {"this": True, "expressions": False, "options": False} 1563 1564 1565class Tuple(Expression): 1566 arg_types = {"expressions": False} 1567 1568 1569class Subqueryable(Unionable): 1570 def subquery(self, alias=None, copy=True) -> Subquery: 1571 """ 1572 Convert this expression to an aliased expression that can be used as a Subquery. 1573 1574 Example: 1575 >>> subquery = Select().select("x").from_("tbl").subquery() 1576 >>> Select().select("x").from_(subquery).sql() 1577 'SELECT x FROM (SELECT x FROM tbl)' 1578 1579 Args: 1580 alias (str | Identifier): an optional alias for the subquery 1581 copy (bool): if `False`, modify this expression instance in-place. 1582 1583 Returns: 1584 Alias: the subquery 1585 """ 1586 instance = _maybe_copy(self, copy) 1587 return Subquery( 1588 this=instance, 1589 alias=TableAlias(this=to_identifier(alias)), 1590 ) 1591 1592 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 1593 raise NotImplementedError 1594 1595 @property 1596 def ctes(self): 1597 with_ = self.args.get("with") 1598 if not with_: 1599 return [] 1600 return with_.expressions 1601 1602 @property 1603 def selects(self): 1604 raise NotImplementedError("Subqueryable objects must implement `selects`") 1605 1606 @property 1607 def named_selects(self): 1608 raise NotImplementedError("Subqueryable objects must implement `named_selects`") 1609 1610 def with_( 1611 self, 1612 alias, 1613 as_, 1614 recursive=None, 1615 append=True, 1616 dialect=None, 1617 copy=True, 1618 **opts, 1619 ): 1620 """ 1621 Append to or set the common table expressions. 1622 1623 Example: 1624 >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql() 1625 'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2' 1626 1627 Args: 1628 alias (str | Expression): the SQL code string to parse as the table name. 1629 If an `Expression` instance is passed, this is used as-is. 1630 as_ (str | Expression): the SQL code string to parse as the table expression. 1631 If an `Expression` instance is passed, it will be used as-is. 1632 recursive (bool): set the RECURSIVE part of the expression. Defaults to `False`. 1633 append (bool): if `True`, add to any existing expressions. 1634 Otherwise, this resets the expressions. 1635 dialect (str): the dialect used to parse the input expression. 1636 copy (bool): if `False`, modify this expression instance in-place. 1637 opts (kwargs): other options to use to parse the input expressions. 1638 1639 Returns: 1640 Select: the modified expression. 1641 """ 1642 alias_expression = maybe_parse( 1643 alias, 1644 dialect=dialect, 1645 into=TableAlias, 1646 **opts, 1647 ) 1648 as_expression = maybe_parse( 1649 as_, 1650 dialect=dialect, 1651 **opts, 1652 ) 1653 cte = CTE( 1654 this=as_expression, 1655 alias=alias_expression, 1656 ) 1657 return _apply_child_list_builder( 1658 cte, 1659 instance=self, 1660 arg="with", 1661 append=append, 1662 copy=copy, 1663 into=With, 1664 properties={"recursive": recursive or False}, 1665 ) 1666 1667 1668QUERY_MODIFIERS = { 1669 "match": False, 1670 "laterals": False, 1671 "joins": False, 1672 "pivots": False, 1673 "where": False, 1674 "group": False, 1675 "having": False, 1676 "qualify": False, 1677 "windows": False, 1678 "distribute": False, 1679 "sort": False, 1680 "cluster": False, 1681 "order": False, 1682 "limit": False, 1683 "offset": False, 1684 "lock": False, 1685} 1686 1687 1688class Table(Expression): 1689 arg_types = { 1690 "this": True, 1691 "alias": False, 1692 "db": False, 1693 "catalog": False, 1694 "laterals": False, 1695 "joins": False, 1696 "pivots": False, 1697 "hints": False, 1698 "system_time": False, 1699 } 1700 1701 @property 1702 def db(self) -> str: 1703 return self.text("db") 1704 1705 @property 1706 def catalog(self) -> str: 1707 return self.text("catalog") 1708 1709 1710# See the TSQL "Querying data in a system-versioned temporal table" page 1711class SystemTime(Expression): 1712 arg_types = { 1713 "this": False, 1714 "expression": False, 1715 "kind": True, 1716 } 1717 1718 1719class Union(Subqueryable): 1720 arg_types = { 1721 "with": False, 1722 "this": True, 1723 "expression": True, 1724 "distinct": False, 1725 **QUERY_MODIFIERS, 1726 } 1727 1728 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 1729 """ 1730 Set the LIMIT expression. 1731 1732 Example: 1733 >>> select("1").union(select("1")).limit(1).sql() 1734 'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1' 1735 1736 Args: 1737 expression (str | int | Expression): the SQL code string to parse. 1738 This can also be an integer. 1739 If a `Limit` instance is passed, this is used as-is. 1740 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 1741 dialect (str): the dialect used to parse the input expression. 1742 copy (bool): if `False`, modify this expression instance in-place. 1743 opts (kwargs): other options to use to parse the input expressions. 1744 1745 Returns: 1746 Select: The limited subqueryable. 1747 """ 1748 return ( 1749 select("*") 1750 .from_(self.subquery(alias="_l_0", copy=copy)) 1751 .limit(expression, dialect=dialect, copy=False, **opts) 1752 ) 1753 1754 def select( 1755 self, 1756 *expressions: str | Expression, 1757 append: bool = True, 1758 dialect: DialectType = None, 1759 copy: bool = True, 1760 **opts, 1761 ) -> Union: 1762 """Append to or set the SELECT of the union recursively. 1763 1764 Example: 1765 >>> from sqlglot import parse_one 1766 >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 1767 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z' 1768 1769 Args: 1770 *expressions: the SQL code strings to parse. 1771 If an `Expression` instance is passed, it will be used as-is. 1772 append: if `True`, add to any existing expressions. 1773 Otherwise, this resets the expressions. 1774 dialect: the dialect used to parse the input expressions. 1775 copy: if `False`, modify this expression instance in-place. 1776 opts: other options to use to parse the input expressions. 1777 1778 Returns: 1779 Union: the modified expression. 1780 """ 1781 this = self.copy() if copy else self 1782 this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts) 1783 this.expression.unnest().select( 1784 *expressions, append=append, dialect=dialect, copy=False, **opts 1785 ) 1786 return this 1787 1788 @property 1789 def named_selects(self): 1790 return self.this.unnest().named_selects 1791 1792 @property 1793 def selects(self): 1794 return self.this.unnest().selects 1795 1796 @property 1797 def left(self): 1798 return self.this 1799 1800 @property 1801 def right(self): 1802 return self.expression 1803 1804 1805class Except(Union): 1806 pass 1807 1808 1809class Intersect(Union): 1810 pass 1811 1812 1813class Unnest(UDTF): 1814 arg_types = { 1815 "expressions": True, 1816 "ordinality": False, 1817 "alias": False, 1818 "offset": False, 1819 } 1820 1821 1822class Update(Expression): 1823 arg_types = { 1824 "with": False, 1825 "this": False, 1826 "expressions": True, 1827 "from": False, 1828 "where": False, 1829 } 1830 1831 1832class Values(UDTF): 1833 arg_types = { 1834 "expressions": True, 1835 "ordinality": False, 1836 "alias": False, 1837 } 1838 1839 1840class Var(Expression): 1841 pass 1842 1843 1844class Schema(Expression): 1845 arg_types = {"this": False, "expressions": False} 1846 1847 1848# Used to represent the FOR UPDATE and FOR SHARE locking read types. 1849# https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html 1850class Lock(Expression): 1851 arg_types = {"update": True} 1852 1853 1854class Select(Subqueryable): 1855 arg_types = { 1856 "with": False, 1857 "expressions": False, 1858 "hint": False, 1859 "distinct": False, 1860 "into": False, 1861 "from": False, 1862 **QUERY_MODIFIERS, 1863 } 1864 1865 def from_(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1866 """ 1867 Set the FROM expression. 1868 1869 Example: 1870 >>> Select().from_("tbl").select("x").sql() 1871 'SELECT x FROM tbl' 1872 1873 Args: 1874 *expressions (str | Expression): the SQL code strings to parse. 1875 If a `From` instance is passed, this is used as-is. 1876 If another `Expression` instance is passed, it will be wrapped in a `From`. 1877 append (bool): if `True`, add to any existing expressions. 1878 Otherwise, this flattens all the `From` expression into a single expression. 1879 dialect (str): the dialect used to parse the input expression. 1880 copy (bool): if `False`, modify this expression instance in-place. 1881 opts (kwargs): other options to use to parse the input expressions. 1882 1883 Returns: 1884 Select: the modified expression. 1885 """ 1886 return _apply_child_list_builder( 1887 *expressions, 1888 instance=self, 1889 arg="from", 1890 append=append, 1891 copy=copy, 1892 prefix="FROM", 1893 into=From, 1894 dialect=dialect, 1895 **opts, 1896 ) 1897 1898 def group_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1899 """ 1900 Set the GROUP BY expression. 1901 1902 Example: 1903 >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql() 1904 'SELECT x, COUNT(1) FROM tbl GROUP BY x' 1905 1906 Args: 1907 *expressions (str | Expression): the SQL code strings to parse. 1908 If a `Group` instance is passed, this is used as-is. 1909 If another `Expression` instance is passed, it will be wrapped in a `Group`. 1910 If nothing is passed in then a group by is not applied to the expression 1911 append (bool): if `True`, add to any existing expressions. 1912 Otherwise, this flattens all the `Group` expression into a single expression. 1913 dialect (str): the dialect used to parse the input expression. 1914 copy (bool): if `False`, modify this expression instance in-place. 1915 opts (kwargs): other options to use to parse the input expressions. 1916 1917 Returns: 1918 Select: the modified expression. 1919 """ 1920 if not expressions: 1921 return self if not copy else self.copy() 1922 return _apply_child_list_builder( 1923 *expressions, 1924 instance=self, 1925 arg="group", 1926 append=append, 1927 copy=copy, 1928 prefix="GROUP BY", 1929 into=Group, 1930 dialect=dialect, 1931 **opts, 1932 ) 1933 1934 def order_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1935 """ 1936 Set the ORDER BY expression. 1937 1938 Example: 1939 >>> Select().from_("tbl").select("x").order_by("x DESC").sql() 1940 'SELECT x FROM tbl ORDER BY x DESC' 1941 1942 Args: 1943 *expressions (str | Expression): the SQL code strings to parse. 1944 If a `Group` instance is passed, this is used as-is. 1945 If another `Expression` instance is passed, it will be wrapped in a `Order`. 1946 append (bool): if `True`, add to any existing expressions. 1947 Otherwise, this flattens all the `Order` expression into a single expression. 1948 dialect (str): the dialect used to parse the input expression. 1949 copy (bool): if `False`, modify this expression instance in-place. 1950 opts (kwargs): other options to use to parse the input expressions. 1951 1952 Returns: 1953 Select: the modified expression. 1954 """ 1955 return _apply_child_list_builder( 1956 *expressions, 1957 instance=self, 1958 arg="order", 1959 append=append, 1960 copy=copy, 1961 prefix="ORDER BY", 1962 into=Order, 1963 dialect=dialect, 1964 **opts, 1965 ) 1966 1967 def sort_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1968 """ 1969 Set the SORT BY expression. 1970 1971 Example: 1972 >>> Select().from_("tbl").select("x").sort_by("x DESC").sql() 1973 'SELECT x FROM tbl SORT BY x DESC' 1974 1975 Args: 1976 *expressions (str | Expression): the SQL code strings to parse. 1977 If a `Group` instance is passed, this is used as-is. 1978 If another `Expression` instance is passed, it will be wrapped in a `SORT`. 1979 append (bool): if `True`, add to any existing expressions. 1980 Otherwise, this flattens all the `Order` expression into a single expression. 1981 dialect (str): the dialect used to parse the input expression. 1982 copy (bool): if `False`, modify this expression instance in-place. 1983 opts (kwargs): other options to use to parse the input expressions. 1984 1985 Returns: 1986 Select: the modified expression. 1987 """ 1988 return _apply_child_list_builder( 1989 *expressions, 1990 instance=self, 1991 arg="sort", 1992 append=append, 1993 copy=copy, 1994 prefix="SORT BY", 1995 into=Sort, 1996 dialect=dialect, 1997 **opts, 1998 ) 1999 2000 def cluster_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2001 """ 2002 Set the CLUSTER BY expression. 2003 2004 Example: 2005 >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql() 2006 'SELECT x FROM tbl CLUSTER BY x DESC' 2007 2008 Args: 2009 *expressions (str | Expression): the SQL code strings to parse. 2010 If a `Group` instance is passed, this is used as-is. 2011 If another `Expression` instance is passed, it will be wrapped in a `Cluster`. 2012 append (bool): if `True`, add to any existing expressions. 2013 Otherwise, this flattens all the `Order` expression into a single expression. 2014 dialect (str): the dialect used to parse the input expression. 2015 copy (bool): if `False`, modify this expression instance in-place. 2016 opts (kwargs): other options to use to parse the input expressions. 2017 2018 Returns: 2019 Select: the modified expression. 2020 """ 2021 return _apply_child_list_builder( 2022 *expressions, 2023 instance=self, 2024 arg="cluster", 2025 append=append, 2026 copy=copy, 2027 prefix="CLUSTER BY", 2028 into=Cluster, 2029 dialect=dialect, 2030 **opts, 2031 ) 2032 2033 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2034 """ 2035 Set the LIMIT expression. 2036 2037 Example: 2038 >>> Select().from_("tbl").select("x").limit(10).sql() 2039 'SELECT x FROM tbl LIMIT 10' 2040 2041 Args: 2042 expression (str | int | Expression): the SQL code string to parse. 2043 This can also be an integer. 2044 If a `Limit` instance is passed, this is used as-is. 2045 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2046 dialect (str): the dialect used to parse the input expression. 2047 copy (bool): if `False`, modify this expression instance in-place. 2048 opts (kwargs): other options to use to parse the input expressions. 2049 2050 Returns: 2051 Select: the modified expression. 2052 """ 2053 return _apply_builder( 2054 expression=expression, 2055 instance=self, 2056 arg="limit", 2057 into=Limit, 2058 prefix="LIMIT", 2059 dialect=dialect, 2060 copy=copy, 2061 **opts, 2062 ) 2063 2064 def offset(self, expression, dialect=None, copy=True, **opts) -> Select: 2065 """ 2066 Set the OFFSET expression. 2067 2068 Example: 2069 >>> Select().from_("tbl").select("x").offset(10).sql() 2070 'SELECT x FROM tbl OFFSET 10' 2071 2072 Args: 2073 expression (str | int | Expression): the SQL code string to parse. 2074 This can also be an integer. 2075 If a `Offset` instance is passed, this is used as-is. 2076 If another `Expression` instance is passed, it will be wrapped in a `Offset`. 2077 dialect (str): the dialect used to parse the input expression. 2078 copy (bool): if `False`, modify this expression instance in-place. 2079 opts (kwargs): other options to use to parse the input expressions. 2080 2081 Returns: 2082 Select: the modified expression. 2083 """ 2084 return _apply_builder( 2085 expression=expression, 2086 instance=self, 2087 arg="offset", 2088 into=Offset, 2089 prefix="OFFSET", 2090 dialect=dialect, 2091 copy=copy, 2092 **opts, 2093 ) 2094 2095 def select( 2096 self, 2097 *expressions: str | Expression, 2098 append: bool = True, 2099 dialect: DialectType = None, 2100 copy: bool = True, 2101 **opts, 2102 ) -> Select: 2103 """ 2104 Append to or set the SELECT expressions. 2105 2106 Example: 2107 >>> Select().select("x", "y").sql() 2108 'SELECT x, y' 2109 2110 Args: 2111 *expressions: the SQL code strings to parse. 2112 If an `Expression` instance is passed, it will be used as-is. 2113 append: if `True`, add to any existing expressions. 2114 Otherwise, this resets the expressions. 2115 dialect: the dialect used to parse the input expressions. 2116 copy: if `False`, modify this expression instance in-place. 2117 opts: other options to use to parse the input expressions. 2118 2119 Returns: 2120 Select: the modified expression. 2121 """ 2122 return _apply_list_builder( 2123 *expressions, 2124 instance=self, 2125 arg="expressions", 2126 append=append, 2127 dialect=dialect, 2128 copy=copy, 2129 **opts, 2130 ) 2131 2132 def lateral(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2133 """ 2134 Append to or set the LATERAL expressions. 2135 2136 Example: 2137 >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql() 2138 'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z' 2139 2140 Args: 2141 *expressions (str | Expression): the SQL code strings to parse. 2142 If an `Expression` instance is passed, it will be used as-is. 2143 append (bool): if `True`, add to any existing expressions. 2144 Otherwise, this resets the expressions. 2145 dialect (str): the dialect used to parse the input expressions. 2146 copy (bool): if `False`, modify this expression instance in-place. 2147 opts (kwargs): other options to use to parse the input expressions. 2148 2149 Returns: 2150 Select: the modified expression. 2151 """ 2152 return _apply_list_builder( 2153 *expressions, 2154 instance=self, 2155 arg="laterals", 2156 append=append, 2157 into=Lateral, 2158 prefix="LATERAL VIEW", 2159 dialect=dialect, 2160 copy=copy, 2161 **opts, 2162 ) 2163 2164 def join( 2165 self, 2166 expression, 2167 on=None, 2168 using=None, 2169 append=True, 2170 join_type=None, 2171 join_alias=None, 2172 dialect=None, 2173 copy=True, 2174 **opts, 2175 ) -> Select: 2176 """ 2177 Append to or set the JOIN expressions. 2178 2179 Example: 2180 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 2181 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y' 2182 2183 >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 2184 'SELECT 1 FROM a JOIN b USING (x, y, z)' 2185 2186 Use `join_type` to change the type of join: 2187 2188 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 2189 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y' 2190 2191 Args: 2192 expression (str | Expression): the SQL code string to parse. 2193 If an `Expression` instance is passed, it will be used as-is. 2194 on (str | Expression): optionally specify the join "on" criteria as a SQL string. 2195 If an `Expression` instance is passed, it will be used as-is. 2196 using (str | Expression): optionally specify the join "using" criteria as a SQL string. 2197 If an `Expression` instance is passed, it will be used as-is. 2198 append (bool): if `True`, add to any existing expressions. 2199 Otherwise, this resets the expressions. 2200 join_type (str): If set, alter the parsed join type 2201 dialect (str): the dialect used to parse the input expressions. 2202 copy (bool): if `False`, modify this expression instance in-place. 2203 opts (kwargs): other options to use to parse the input expressions. 2204 2205 Returns: 2206 Select: the modified expression. 2207 """ 2208 parse_args = {"dialect": dialect, **opts} 2209 2210 try: 2211 expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args) 2212 except ParseError: 2213 expression = maybe_parse(expression, into=(Join, Expression), **parse_args) 2214 2215 join = expression if isinstance(expression, Join) else Join(this=expression) 2216 2217 if isinstance(join.this, Select): 2218 join.this.replace(join.this.subquery()) 2219 2220 if join_type: 2221 natural: t.Optional[Token] 2222 side: t.Optional[Token] 2223 kind: t.Optional[Token] 2224 2225 natural, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args) # type: ignore 2226 2227 if natural: 2228 join.set("natural", True) 2229 if side: 2230 join.set("side", side.text) 2231 if kind: 2232 join.set("kind", kind.text) 2233 2234 if on: 2235 on = and_(*ensure_collection(on), dialect=dialect, **opts) 2236 join.set("on", on) 2237 2238 if using: 2239 join = _apply_list_builder( 2240 *ensure_collection(using), 2241 instance=join, 2242 arg="using", 2243 append=append, 2244 copy=copy, 2245 **opts, 2246 ) 2247 2248 if join_alias: 2249 join.set("this", alias_(join.this, join_alias, table=True)) 2250 return _apply_list_builder( 2251 join, 2252 instance=self, 2253 arg="joins", 2254 append=append, 2255 copy=copy, 2256 **opts, 2257 ) 2258 2259 def where(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2260 """ 2261 Append to or set the WHERE expressions. 2262 2263 Example: 2264 >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql() 2265 "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'" 2266 2267 Args: 2268 *expressions (str | Expression): the SQL code strings to parse. 2269 If an `Expression` instance is passed, it will be used as-is. 2270 Multiple expressions are combined with an AND operator. 2271 append (bool): if `True`, AND the new expressions to any existing expression. 2272 Otherwise, this resets the expression. 2273 dialect (str): the dialect used to parse the input expressions. 2274 copy (bool): if `False`, modify this expression instance in-place. 2275 opts (kwargs): other options to use to parse the input expressions. 2276 2277 Returns: 2278 Select: the modified expression. 2279 """ 2280 return _apply_conjunction_builder( 2281 *expressions, 2282 instance=self, 2283 arg="where", 2284 append=append, 2285 into=Where, 2286 dialect=dialect, 2287 copy=copy, 2288 **opts, 2289 ) 2290 2291 def having(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2292 """ 2293 Append to or set the HAVING expressions. 2294 2295 Example: 2296 >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql() 2297 'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3' 2298 2299 Args: 2300 *expressions (str | Expression): the SQL code strings to parse. 2301 If an `Expression` instance is passed, it will be used as-is. 2302 Multiple expressions are combined with an AND operator. 2303 append (bool): if `True`, AND the new expressions to any existing expression. 2304 Otherwise, this resets the expression. 2305 dialect (str): the dialect used to parse the input expressions. 2306 copy (bool): if `False`, modify this expression instance in-place. 2307 opts (kwargs): other options to use to parse the input expressions. 2308 2309 Returns: 2310 Select: the modified expression. 2311 """ 2312 return _apply_conjunction_builder( 2313 *expressions, 2314 instance=self, 2315 arg="having", 2316 append=append, 2317 into=Having, 2318 dialect=dialect, 2319 copy=copy, 2320 **opts, 2321 ) 2322 2323 def window(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2324 return _apply_list_builder( 2325 *expressions, 2326 instance=self, 2327 arg="windows", 2328 append=append, 2329 into=Window, 2330 dialect=dialect, 2331 copy=copy, 2332 **opts, 2333 ) 2334 2335 def distinct(self, distinct=True, copy=True) -> Select: 2336 """ 2337 Set the OFFSET expression. 2338 2339 Example: 2340 >>> Select().from_("tbl").select("x").distinct().sql() 2341 'SELECT DISTINCT x FROM tbl' 2342 2343 Args: 2344 distinct (bool): whether the Select should be distinct 2345 copy (bool): if `False`, modify this expression instance in-place. 2346 2347 Returns: 2348 Select: the modified expression. 2349 """ 2350 instance = _maybe_copy(self, copy) 2351 instance.set("distinct", Distinct() if distinct else None) 2352 return instance 2353 2354 def ctas(self, table, properties=None, dialect=None, copy=True, **opts) -> Create: 2355 """ 2356 Convert this expression to a CREATE TABLE AS statement. 2357 2358 Example: 2359 >>> Select().select("*").from_("tbl").ctas("x").sql() 2360 'CREATE TABLE x AS SELECT * FROM tbl' 2361 2362 Args: 2363 table (str | Expression): the SQL code string to parse as the table name. 2364 If another `Expression` instance is passed, it will be used as-is. 2365 properties (dict): an optional mapping of table properties 2366 dialect (str): the dialect used to parse the input table. 2367 copy (bool): if `False`, modify this expression instance in-place. 2368 opts (kwargs): other options to use to parse the input table. 2369 2370 Returns: 2371 Create: the CREATE TABLE AS expression 2372 """ 2373 instance = _maybe_copy(self, copy) 2374 table_expression = maybe_parse( 2375 table, 2376 into=Table, 2377 dialect=dialect, 2378 **opts, 2379 ) 2380 properties_expression = None 2381 if properties: 2382 properties_expression = Properties.from_dict(properties) 2383 2384 return Create( 2385 this=table_expression, 2386 kind="table", 2387 expression=instance, 2388 properties=properties_expression, 2389 ) 2390 2391 def lock(self, update: bool = True, copy: bool = True) -> Select: 2392 """ 2393 Set the locking read mode for this expression. 2394 2395 Examples: 2396 >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql") 2397 "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE" 2398 2399 >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql") 2400 "SELECT x FROM tbl WHERE x = 'a' FOR SHARE" 2401 2402 Args: 2403 update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`. 2404 copy: if `False`, modify this expression instance in-place. 2405 2406 Returns: 2407 The modified expression. 2408 """ 2409 2410 inst = _maybe_copy(self, copy) 2411 inst.set("lock", Lock(update=update)) 2412 2413 return inst 2414 2415 @property 2416 def named_selects(self) -> t.List[str]: 2417 return [e.output_name for e in self.expressions if e.alias_or_name] 2418 2419 @property 2420 def selects(self) -> t.List[Expression]: 2421 return self.expressions 2422 2423 2424class Subquery(DerivedTable, Unionable): 2425 arg_types = { 2426 "this": True, 2427 "alias": False, 2428 "with": False, 2429 **QUERY_MODIFIERS, 2430 } 2431 2432 def unnest(self): 2433 """ 2434 Returns the first non subquery. 2435 """ 2436 expression = self 2437 while isinstance(expression, Subquery): 2438 expression = expression.this 2439 return expression 2440 2441 @property 2442 def output_name(self): 2443 return self.alias 2444 2445 2446class TableSample(Expression): 2447 arg_types = { 2448 "this": False, 2449 "method": False, 2450 "bucket_numerator": False, 2451 "bucket_denominator": False, 2452 "bucket_field": False, 2453 "percent": False, 2454 "rows": False, 2455 "size": False, 2456 "seed": False, 2457 } 2458 2459 2460class Tag(Expression): 2461 """Tags are used for generating arbitrary sql like SELECT <span>x</span>.""" 2462 2463 arg_types = { 2464 "this": False, 2465 "prefix": False, 2466 "postfix": False, 2467 } 2468 2469 2470class Pivot(Expression): 2471 arg_types = { 2472 "this": False, 2473 "expressions": True, 2474 "field": True, 2475 "unpivot": True, 2476 } 2477 2478 2479class Window(Expression): 2480 arg_types = { 2481 "this": True, 2482 "partition_by": False, 2483 "order": False, 2484 "spec": False, 2485 "alias": False, 2486 } 2487 2488 2489class WindowSpec(Expression): 2490 arg_types = { 2491 "kind": False, 2492 "start": False, 2493 "start_side": False, 2494 "end": False, 2495 "end_side": False, 2496 } 2497 2498 2499class Where(Expression): 2500 pass 2501 2502 2503class Star(Expression): 2504 arg_types = {"except": False, "replace": False} 2505 2506 @property 2507 def name(self) -> str: 2508 return "*" 2509 2510 @property 2511 def output_name(self): 2512 return self.name 2513 2514 2515class Parameter(Expression): 2516 arg_types = {"this": True, "wrapped": False} 2517 2518 2519class SessionParameter(Expression): 2520 arg_types = {"this": True, "kind": False} 2521 2522 2523class Placeholder(Expression): 2524 arg_types = {"this": False} 2525 2526 2527class Null(Condition): 2528 arg_types: t.Dict[str, t.Any] = {} 2529 2530 @property 2531 def name(self) -> str: 2532 return "NULL" 2533 2534 2535class Boolean(Condition): 2536 pass 2537 2538 2539class DataType(Expression): 2540 arg_types = { 2541 "this": True, 2542 "expressions": False, 2543 "nested": False, 2544 "values": False, 2545 "prefix": False, 2546 } 2547 2548 class Type(AutoName): 2549 CHAR = auto() 2550 NCHAR = auto() 2551 VARCHAR = auto() 2552 NVARCHAR = auto() 2553 TEXT = auto() 2554 MEDIUMTEXT = auto() 2555 LONGTEXT = auto() 2556 MEDIUMBLOB = auto() 2557 LONGBLOB = auto() 2558 BINARY = auto() 2559 VARBINARY = auto() 2560 INT = auto() 2561 TINYINT = auto() 2562 SMALLINT = auto() 2563 BIGINT = auto() 2564 FLOAT = auto() 2565 DOUBLE = auto() 2566 DECIMAL = auto() 2567 BOOLEAN = auto() 2568 JSON = auto() 2569 JSONB = auto() 2570 INTERVAL = auto() 2571 TIME = auto() 2572 TIMESTAMP = auto() 2573 TIMESTAMPTZ = auto() 2574 TIMESTAMPLTZ = auto() 2575 DATE = auto() 2576 DATETIME = auto() 2577 ARRAY = auto() 2578 MAP = auto() 2579 UUID = auto() 2580 GEOGRAPHY = auto() 2581 GEOMETRY = auto() 2582 STRUCT = auto() 2583 NULLABLE = auto() 2584 HLLSKETCH = auto() 2585 HSTORE = auto() 2586 SUPER = auto() 2587 SERIAL = auto() 2588 SMALLSERIAL = auto() 2589 BIGSERIAL = auto() 2590 XML = auto() 2591 UNIQUEIDENTIFIER = auto() 2592 MONEY = auto() 2593 SMALLMONEY = auto() 2594 ROWVERSION = auto() 2595 IMAGE = auto() 2596 VARIANT = auto() 2597 OBJECT = auto() 2598 NULL = auto() 2599 UNKNOWN = auto() # Sentinel value, useful for type annotation 2600 2601 TEXT_TYPES = { 2602 Type.CHAR, 2603 Type.NCHAR, 2604 Type.VARCHAR, 2605 Type.NVARCHAR, 2606 Type.TEXT, 2607 } 2608 2609 INTEGER_TYPES = { 2610 Type.INT, 2611 Type.TINYINT, 2612 Type.SMALLINT, 2613 Type.BIGINT, 2614 } 2615 2616 FLOAT_TYPES = { 2617 Type.FLOAT, 2618 Type.DOUBLE, 2619 } 2620 2621 NUMERIC_TYPES = {*INTEGER_TYPES, *FLOAT_TYPES} 2622 2623 TEMPORAL_TYPES = { 2624 Type.TIMESTAMP, 2625 Type.TIMESTAMPTZ, 2626 Type.TIMESTAMPLTZ, 2627 Type.DATE, 2628 Type.DATETIME, 2629 } 2630 2631 @classmethod 2632 def build( 2633 cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs 2634 ) -> DataType: 2635 from sqlglot import parse_one 2636 2637 if isinstance(dtype, str): 2638 if dtype.upper() in cls.Type.__members__: 2639 data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[dtype.upper()]) 2640 else: 2641 data_type_exp = parse_one(dtype, read=dialect, into=DataType) 2642 if data_type_exp is None: 2643 raise ValueError(f"Unparsable data type value: {dtype}") 2644 elif isinstance(dtype, DataType.Type): 2645 data_type_exp = DataType(this=dtype) 2646 elif isinstance(dtype, DataType): 2647 return dtype 2648 else: 2649 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type") 2650 return DataType(**{**data_type_exp.args, **kwargs}) 2651 2652 def is_type(self, dtype: DataType.Type) -> bool: 2653 return self.this == dtype 2654 2655 2656# https://www.postgresql.org/docs/15/datatype-pseudo.html 2657class PseudoType(Expression): 2658 pass 2659 2660 2661class StructKwarg(Expression): 2662 arg_types = {"this": True, "expression": True} 2663 2664 2665# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...) 2666class SubqueryPredicate(Predicate): 2667 pass 2668 2669 2670class All(SubqueryPredicate): 2671 pass 2672 2673 2674class Any(SubqueryPredicate): 2675 pass 2676 2677 2678class Exists(SubqueryPredicate): 2679 pass 2680 2681 2682# Commands to interact with the databases or engines. For most of the command 2683# expressions we parse whatever comes after the command's name as a string. 2684class Command(Expression): 2685 arg_types = {"this": True, "expression": False} 2686 2687 2688class Transaction(Expression): 2689 arg_types = {"this": False, "modes": False} 2690 2691 2692class Commit(Expression): 2693 arg_types = {"chain": False} 2694 2695 2696class Rollback(Expression): 2697 arg_types = {"savepoint": False} 2698 2699 2700class AlterTable(Expression): 2701 arg_types = {"this": True, "actions": True, "exists": False} 2702 2703 2704class AddConstraint(Expression): 2705 arg_types = {"this": False, "expression": False, "enforced": False} 2706 2707 2708class DropPartition(Expression): 2709 arg_types = {"expressions": True, "exists": False} 2710 2711 2712# Binary expressions like (ADD a b) 2713class Binary(Expression): 2714 arg_types = {"this": True, "expression": True} 2715 2716 @property 2717 def left(self): 2718 return self.this 2719 2720 @property 2721 def right(self): 2722 return self.expression 2723 2724 2725class Add(Binary): 2726 pass 2727 2728 2729class Connector(Binary, Condition): 2730 pass 2731 2732 2733class And(Connector): 2734 pass 2735 2736 2737class Or(Connector): 2738 pass 2739 2740 2741class BitwiseAnd(Binary): 2742 pass 2743 2744 2745class BitwiseLeftShift(Binary): 2746 pass 2747 2748 2749class BitwiseOr(Binary): 2750 pass 2751 2752 2753class BitwiseRightShift(Binary): 2754 pass 2755 2756 2757class BitwiseXor(Binary): 2758 pass 2759 2760 2761class Div(Binary): 2762 pass 2763 2764 2765class Dot(Binary): 2766 @property 2767 def name(self) -> str: 2768 return self.expression.name 2769 2770 2771class DPipe(Binary): 2772 pass 2773 2774 2775class EQ(Binary, Predicate): 2776 pass 2777 2778 2779class NullSafeEQ(Binary, Predicate): 2780 pass 2781 2782 2783class NullSafeNEQ(Binary, Predicate): 2784 pass 2785 2786 2787class Distance(Binary): 2788 pass 2789 2790 2791class Escape(Binary): 2792 pass 2793 2794 2795class Glob(Binary, Predicate): 2796 pass 2797 2798 2799class GT(Binary, Predicate): 2800 pass 2801 2802 2803class GTE(Binary, Predicate): 2804 pass 2805 2806 2807class ILike(Binary, Predicate): 2808 pass 2809 2810 2811class ILikeAny(Binary, Predicate): 2812 pass 2813 2814 2815class IntDiv(Binary): 2816 pass 2817 2818 2819class Is(Binary, Predicate): 2820 pass 2821 2822 2823class Kwarg(Binary): 2824 """Kwarg in special functions like func(kwarg => y).""" 2825 2826 2827class Like(Binary, Predicate): 2828 pass 2829 2830 2831class LikeAny(Binary, Predicate): 2832 pass 2833 2834 2835class LT(Binary, Predicate): 2836 pass 2837 2838 2839class LTE(Binary, Predicate): 2840 pass 2841 2842 2843class Mod(Binary): 2844 pass 2845 2846 2847class Mul(Binary): 2848 pass 2849 2850 2851class NEQ(Binary, Predicate): 2852 pass 2853 2854 2855class SimilarTo(Binary, Predicate): 2856 pass 2857 2858 2859class Slice(Binary): 2860 arg_types = {"this": False, "expression": False} 2861 2862 2863class Sub(Binary): 2864 pass 2865 2866 2867# Unary Expressions 2868# (NOT a) 2869class Unary(Expression): 2870 pass 2871 2872 2873class BitwiseNot(Unary): 2874 pass 2875 2876 2877class Not(Unary, Condition): 2878 pass 2879 2880 2881class Paren(Unary, Condition): 2882 arg_types = {"this": True, "with": False} 2883 2884 2885class Neg(Unary): 2886 pass 2887 2888 2889# Special Functions 2890class Alias(Expression): 2891 arg_types = {"this": True, "alias": False} 2892 2893 @property 2894 def output_name(self): 2895 return self.alias 2896 2897 2898class Aliases(Expression): 2899 arg_types = {"this": True, "expressions": True} 2900 2901 @property 2902 def aliases(self): 2903 return self.expressions 2904 2905 2906class AtTimeZone(Expression): 2907 arg_types = {"this": True, "zone": True} 2908 2909 2910class Between(Predicate): 2911 arg_types = {"this": True, "low": True, "high": True} 2912 2913 2914class Bracket(Condition): 2915 arg_types = {"this": True, "expressions": True} 2916 2917 2918class Distinct(Expression): 2919 arg_types = {"expressions": False, "on": False} 2920 2921 2922class In(Predicate): 2923 arg_types = { 2924 "this": True, 2925 "expressions": False, 2926 "query": False, 2927 "unnest": False, 2928 "field": False, 2929 "is_global": False, 2930 } 2931 2932 2933class TimeUnit(Expression): 2934 """Automatically converts unit arg into a var.""" 2935 2936 arg_types = {"unit": False} 2937 2938 def __init__(self, **args): 2939 unit = args.get("unit") 2940 if isinstance(unit, Column): 2941 args["unit"] = Var(this=unit.name) 2942 elif isinstance(unit, Week): 2943 unit.set("this", Var(this=unit.this.name)) 2944 super().__init__(**args) 2945 2946 2947class Interval(TimeUnit): 2948 arg_types = {"this": False, "unit": False} 2949 2950 2951class IgnoreNulls(Expression): 2952 pass 2953 2954 2955class RespectNulls(Expression): 2956 pass 2957 2958 2959# Functions 2960class Func(Condition): 2961 """ 2962 The base class for all function expressions. 2963 2964 Attributes: 2965 is_var_len_args (bool): if set to True the last argument defined in arg_types will be 2966 treated as a variable length argument and the argument's value will be stored as a list. 2967 _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items) 2968 for this function expression. These values are used to map this node to a name during parsing 2969 as well as to provide the function's name during SQL string generation. By default the SQL 2970 name is set to the expression's class name transformed to snake case. 2971 """ 2972 2973 is_var_len_args = False 2974 2975 @classmethod 2976 def from_arg_list(cls, args): 2977 if cls.is_var_len_args: 2978 all_arg_keys = list(cls.arg_types) 2979 # If this function supports variable length argument treat the last argument as such. 2980 non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys 2981 num_non_var = len(non_var_len_arg_keys) 2982 2983 args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)} 2984 args_dict[all_arg_keys[-1]] = args[num_non_var:] 2985 else: 2986 args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)} 2987 2988 return cls(**args_dict) 2989 2990 @classmethod 2991 def sql_names(cls): 2992 if cls is Func: 2993 raise NotImplementedError( 2994 "SQL name is only supported by concrete function implementations" 2995 ) 2996 if "_sql_names" not in cls.__dict__: 2997 cls._sql_names = [camel_to_snake_case(cls.__name__)] 2998 return cls._sql_names 2999 3000 @classmethod 3001 def sql_name(cls): 3002 return cls.sql_names()[0] 3003 3004 @classmethod 3005 def default_parser_mappings(cls): 3006 return {name: cls.from_arg_list for name in cls.sql_names()} 3007 3008 3009class AggFunc(Func): 3010 pass 3011 3012 3013class Abs(Func): 3014 pass 3015 3016 3017class Anonymous(Func): 3018 arg_types = {"this": True, "expressions": False} 3019 is_var_len_args = True 3020 3021 3022class ApproxDistinct(AggFunc): 3023 arg_types = {"this": True, "accuracy": False} 3024 3025 3026class Array(Func): 3027 arg_types = {"expressions": False} 3028 is_var_len_args = True 3029 3030 3031class GenerateSeries(Func): 3032 arg_types = {"start": True, "end": True, "step": False} 3033 3034 3035class ArrayAgg(AggFunc): 3036 pass 3037 3038 3039class ArrayAll(Func): 3040 arg_types = {"this": True, "expression": True} 3041 3042 3043class ArrayAny(Func): 3044 arg_types = {"this": True, "expression": True} 3045 3046 3047class ArrayConcat(Func): 3048 arg_types = {"this": True, "expressions": False} 3049 is_var_len_args = True 3050 3051 3052class ArrayContains(Func): 3053 arg_types = {"this": True, "expression": True} 3054 3055 3056class ArrayFilter(Func): 3057 arg_types = {"this": True, "expression": True} 3058 _sql_names = ["FILTER", "ARRAY_FILTER"] 3059 3060 3061class ArraySize(Func): 3062 arg_types = {"this": True, "expression": False} 3063 3064 3065class ArraySort(Func): 3066 arg_types = {"this": True, "expression": False} 3067 3068 3069class ArraySum(Func): 3070 pass 3071 3072 3073class ArrayUnionAgg(AggFunc): 3074 pass 3075 3076 3077class Avg(AggFunc): 3078 pass 3079 3080 3081class AnyValue(AggFunc): 3082 pass 3083 3084 3085class Case(Func): 3086 arg_types = {"this": False, "ifs": True, "default": False} 3087 3088 3089class Cast(Func): 3090 arg_types = {"this": True, "to": True} 3091 3092 @property 3093 def name(self) -> str: 3094 return self.this.name 3095 3096 @property 3097 def to(self): 3098 return self.args["to"] 3099 3100 @property 3101 def output_name(self): 3102 return self.name 3103 3104 def is_type(self, dtype: DataType.Type) -> bool: 3105 return self.to.is_type(dtype) 3106 3107 3108class Collate(Binary): 3109 pass 3110 3111 3112class TryCast(Cast): 3113 pass 3114 3115 3116class Ceil(Func): 3117 arg_types = {"this": True, "decimals": False} 3118 _sql_names = ["CEIL", "CEILING"] 3119 3120 3121class Coalesce(Func): 3122 arg_types = {"this": True, "expressions": False} 3123 is_var_len_args = True 3124 3125 3126class Concat(Func): 3127 arg_types = {"expressions": True} 3128 is_var_len_args = True 3129 3130 3131class ConcatWs(Concat): 3132 _sql_names = ["CONCAT_WS"] 3133 3134 3135class Count(AggFunc): 3136 arg_types = {"this": False} 3137 3138 3139class CurrentDate(Func): 3140 arg_types = {"this": False} 3141 3142 3143class CurrentDatetime(Func): 3144 arg_types = {"this": False} 3145 3146 3147class CurrentTime(Func): 3148 arg_types = {"this": False} 3149 3150 3151class CurrentTimestamp(Func): 3152 arg_types = {"this": False} 3153 3154 3155class DateAdd(Func, TimeUnit): 3156 arg_types = {"this": True, "expression": True, "unit": False} 3157 3158 3159class DateSub(Func, TimeUnit): 3160 arg_types = {"this": True, "expression": True, "unit": False} 3161 3162 3163class DateDiff(Func, TimeUnit): 3164 arg_types = {"this": True, "expression": True, "unit": False} 3165 3166 3167class DateTrunc(Func): 3168 arg_types = {"unit": True, "this": True, "zone": False} 3169 3170 3171class DatetimeAdd(Func, TimeUnit): 3172 arg_types = {"this": True, "expression": True, "unit": False} 3173 3174 3175class DatetimeSub(Func, TimeUnit): 3176 arg_types = {"this": True, "expression": True, "unit": False} 3177 3178 3179class DatetimeDiff(Func, TimeUnit): 3180 arg_types = {"this": True, "expression": True, "unit": False} 3181 3182 3183class DatetimeTrunc(Func, TimeUnit): 3184 arg_types = {"this": True, "unit": True, "zone": False} 3185 3186 3187class DayOfWeek(Func): 3188 _sql_names = ["DAY_OF_WEEK", "DAYOFWEEK"] 3189 3190 3191class DayOfMonth(Func): 3192 _sql_names = ["DAY_OF_MONTH", "DAYOFMONTH"] 3193 3194 3195class DayOfYear(Func): 3196 _sql_names = ["DAY_OF_YEAR", "DAYOFYEAR"] 3197 3198 3199class WeekOfYear(Func): 3200 _sql_names = ["WEEK_OF_YEAR", "WEEKOFYEAR"] 3201 3202 3203class LastDateOfMonth(Func): 3204 pass 3205 3206 3207class Extract(Func): 3208 arg_types = {"this": True, "expression": True} 3209 3210 3211class TimestampAdd(Func, TimeUnit): 3212 arg_types = {"this": True, "expression": True, "unit": False} 3213 3214 3215class TimestampSub(Func, TimeUnit): 3216 arg_types = {"this": True, "expression": True, "unit": False} 3217 3218 3219class TimestampDiff(Func, TimeUnit): 3220 arg_types = {"this": True, "expression": True, "unit": False} 3221 3222 3223class TimestampTrunc(Func, TimeUnit): 3224 arg_types = {"this": True, "unit": True, "zone": False} 3225 3226 3227class TimeAdd(Func, TimeUnit): 3228 arg_types = {"this": True, "expression": True, "unit": False} 3229 3230 3231class TimeSub(Func, TimeUnit): 3232 arg_types = {"this": True, "expression": True, "unit": False} 3233 3234 3235class TimeDiff(Func, TimeUnit): 3236 arg_types = {"this": True, "expression": True, "unit": False} 3237 3238 3239class TimeTrunc(Func, TimeUnit): 3240 arg_types = {"this": True, "unit": True, "zone": False} 3241 3242 3243class DateFromParts(Func): 3244 _sql_names = ["DATEFROMPARTS"] 3245 arg_types = {"year": True, "month": True, "day": True} 3246 3247 3248class DateStrToDate(Func): 3249 pass 3250 3251 3252class DateToDateStr(Func): 3253 pass 3254 3255 3256class DateToDi(Func): 3257 pass 3258 3259 3260class Day(Func): 3261 pass 3262 3263 3264class Decode(Func): 3265 arg_types = {"this": True, "charset": True, "replace": False} 3266 3267 3268class DiToDate(Func): 3269 pass 3270 3271 3272class Encode(Func): 3273 arg_types = {"this": True, "charset": True} 3274 3275 3276class Exp(Func): 3277 pass 3278 3279 3280class Explode(Func): 3281 pass 3282 3283 3284class Floor(Func): 3285 arg_types = {"this": True, "decimals": False} 3286 3287 3288class Greatest(Func): 3289 arg_types = {"this": True, "expressions": False} 3290 is_var_len_args = True 3291 3292 3293class GroupConcat(Func): 3294 arg_types = {"this": True, "separator": False} 3295 3296 3297class Hex(Func): 3298 pass 3299 3300 3301class If(Func): 3302 arg_types = {"this": True, "true": True, "false": False} 3303 3304 3305class IfNull(Func): 3306 arg_types = {"this": True, "expression": False} 3307 _sql_names = ["IFNULL", "NVL"] 3308 3309 3310class Initcap(Func): 3311 pass 3312 3313 3314class JSONBContains(Binary): 3315 _sql_names = ["JSONB_CONTAINS"] 3316 3317 3318class JSONExtract(Binary, Func): 3319 _sql_names = ["JSON_EXTRACT"] 3320 3321 3322class JSONExtractScalar(JSONExtract): 3323 _sql_names = ["JSON_EXTRACT_SCALAR"] 3324 3325 3326class JSONBExtract(JSONExtract): 3327 _sql_names = ["JSONB_EXTRACT"] 3328 3329 3330class JSONBExtractScalar(JSONExtract): 3331 _sql_names = ["JSONB_EXTRACT_SCALAR"] 3332 3333 3334class Least(Func): 3335 arg_types = {"this": True, "expressions": False} 3336 is_var_len_args = True 3337 3338 3339class Length(Func): 3340 pass 3341 3342 3343class Levenshtein(Func): 3344 arg_types = { 3345 "this": True, 3346 "expression": False, 3347 "ins_cost": False, 3348 "del_cost": False, 3349 "sub_cost": False, 3350 } 3351 3352 3353class Ln(Func): 3354 pass 3355 3356 3357class Log(Func): 3358 arg_types = {"this": True, "expression": False} 3359 3360 3361class Log2(Func): 3362 pass 3363 3364 3365class Log10(Func): 3366 pass 3367 3368 3369class LogicalOr(AggFunc): 3370 _sql_names = ["LOGICAL_OR", "BOOL_OR"] 3371 3372 3373class Lower(Func): 3374 _sql_names = ["LOWER", "LCASE"] 3375 3376 3377class Map(Func): 3378 arg_types = {"keys": False, "values": False} 3379 3380 3381class VarMap(Func): 3382 arg_types = {"keys": True, "values": True} 3383 is_var_len_args = True 3384 3385 3386class Matches(Func): 3387 """Oracle/Snowflake decode. 3388 https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm 3389 Pattern matching MATCHES(value, search1, result1, ...searchN, resultN, else) 3390 """ 3391 3392 arg_types = {"this": True, "expressions": True} 3393 is_var_len_args = True 3394 3395 3396class Max(AggFunc): 3397 arg_types = {"this": True, "expression": False} 3398 3399 3400class Min(AggFunc): 3401 arg_types = {"this": True, "expression": False} 3402 3403 3404class Month(Func): 3405 pass 3406 3407 3408class Nvl2(Func): 3409 arg_types = {"this": True, "true": True, "false": False} 3410 3411 3412class Posexplode(Func): 3413 pass 3414 3415 3416class Pow(Binary, Func): 3417 _sql_names = ["POWER", "POW"] 3418 3419 3420class PercentileCont(AggFunc): 3421 pass 3422 3423 3424class PercentileDisc(AggFunc): 3425 pass 3426 3427 3428class Quantile(AggFunc): 3429 arg_types = {"this": True, "quantile": True} 3430 3431 3432# Clickhouse-specific: 3433# https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/quantiles/#quantiles 3434class Quantiles(AggFunc): 3435 arg_types = {"parameters": True, "expressions": True} 3436 3437 3438class QuantileIf(AggFunc): 3439 arg_types = {"parameters": True, "expressions": True} 3440 3441 3442class ApproxQuantile(Quantile): 3443 arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False} 3444 3445 3446class ReadCSV(Func): 3447 _sql_names = ["READ_CSV"] 3448 is_var_len_args = True 3449 arg_types = {"this": True, "expressions": False} 3450 3451 3452class Reduce(Func): 3453 arg_types = {"this": True, "initial": True, "merge": True, "finish": False} 3454 3455 3456class RegexpExtract(Func): 3457 arg_types = { 3458 "this": True, 3459 "expression": True, 3460 "position": False, 3461 "occurrence": False, 3462 "group": False, 3463 } 3464 3465 3466class RegexpLike(Func): 3467 arg_types = {"this": True, "expression": True, "flag": False} 3468 3469 3470class RegexpILike(Func): 3471 arg_types = {"this": True, "expression": True, "flag": False} 3472 3473 3474class RegexpSplit(Func): 3475 arg_types = {"this": True, "expression": True} 3476 3477 3478class Repeat(Func): 3479 arg_types = {"this": True, "times": True} 3480 3481 3482class Round(Func): 3483 arg_types = {"this": True, "decimals": False} 3484 3485 3486class RowNumber(Func): 3487 arg_types: t.Dict[str, t.Any] = {} 3488 3489 3490class SafeDivide(Func): 3491 arg_types = {"this": True, "expression": True} 3492 3493 3494class SetAgg(AggFunc): 3495 pass 3496 3497 3498class SortArray(Func): 3499 arg_types = {"this": True, "asc": False} 3500 3501 3502class Split(Func): 3503 arg_types = {"this": True, "expression": True, "limit": False} 3504 3505 3506# Start may be omitted in the case of postgres 3507# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6 3508class Substring(Func): 3509 arg_types = {"this": True, "start": False, "length": False} 3510 3511 3512class StrPosition(Func): 3513 arg_types = { 3514 "this": True, 3515 "substr": True, 3516 "position": False, 3517 "instance": False, 3518 } 3519 3520 3521class StrToDate(Func): 3522 arg_types = {"this": True, "format": True} 3523 3524 3525class StrToTime(Func): 3526 arg_types = {"this": True, "format": True} 3527 3528 3529# Spark allows unix_timestamp() 3530# https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.unix_timestamp.html 3531class StrToUnix(Func): 3532 arg_types = {"this": False, "format": False} 3533 3534 3535class NumberToStr(Func): 3536 arg_types = {"this": True, "format": True} 3537 3538 3539class Struct(Func): 3540 arg_types = {"expressions": True} 3541 is_var_len_args = True 3542 3543 3544class StructExtract(Func): 3545 arg_types = {"this": True, "expression": True} 3546 3547 3548class Sum(AggFunc): 3549 pass 3550 3551 3552class Sqrt(Func): 3553 pass 3554 3555 3556class Stddev(AggFunc): 3557 pass 3558 3559 3560class StddevPop(AggFunc): 3561 pass 3562 3563 3564class StddevSamp(AggFunc): 3565 pass 3566 3567 3568class TimeToStr(Func): 3569 arg_types = {"this": True, "format": True} 3570 3571 3572class TimeToTimeStr(Func): 3573 pass 3574 3575 3576class TimeToUnix(Func): 3577 pass 3578 3579 3580class TimeStrToDate(Func): 3581 pass 3582 3583 3584class TimeStrToTime(Func): 3585 pass 3586 3587 3588class TimeStrToUnix(Func): 3589 pass 3590 3591 3592class Trim(Func): 3593 arg_types = { 3594 "this": True, 3595 "expression": False, 3596 "position": False, 3597 "collation": False, 3598 } 3599 3600 3601class TsOrDsAdd(Func, TimeUnit): 3602 arg_types = {"this": True, "expression": True, "unit": False} 3603 3604 3605class TsOrDsToDateStr(Func): 3606 pass 3607 3608 3609class TsOrDsToDate(Func): 3610 arg_types = {"this": True, "format": False} 3611 3612 3613class TsOrDiToDi(Func): 3614 pass 3615 3616 3617class Unhex(Func): 3618 pass 3619 3620 3621class UnixToStr(Func): 3622 arg_types = {"this": True, "format": False} 3623 3624 3625# https://prestodb.io/docs/current/functions/datetime.html 3626# presto has weird zone/hours/minutes 3627class UnixToTime(Func): 3628 arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False} 3629 3630 SECONDS = Literal.string("seconds") 3631 MILLIS = Literal.string("millis") 3632 MICROS = Literal.string("micros") 3633 3634 3635class UnixToTimeStr(Func): 3636 pass 3637 3638 3639class Upper(Func): 3640 _sql_names = ["UPPER", "UCASE"] 3641 3642 3643class Variance(AggFunc): 3644 _sql_names = ["VARIANCE", "VARIANCE_SAMP", "VAR_SAMP"] 3645 3646 3647class VariancePop(AggFunc): 3648 _sql_names = ["VARIANCE_POP", "VAR_POP"] 3649 3650 3651class Week(Func): 3652 arg_types = {"this": True, "mode": False} 3653 3654 3655class XMLTable(Func): 3656 arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False} 3657 3658 3659class Year(Func): 3660 pass 3661 3662 3663class Use(Expression): 3664 arg_types = {"this": True, "kind": False} 3665 3666 3667class Merge(Expression): 3668 arg_types = {"this": True, "using": True, "on": True, "expressions": True} 3669 3670 3671class When(Func): 3672 arg_types = {"this": True, "then": True} 3673 3674 3675def _norm_args(expression): 3676 args = {} 3677 3678 for k, arg in expression.args.items(): 3679 if isinstance(arg, list): 3680 arg = [_norm_arg(a) for a in arg] 3681 if not arg: 3682 arg = None 3683 else: 3684 arg = _norm_arg(arg) 3685 3686 if arg is not None and arg is not False: 3687 args[k] = arg 3688 3689 return args 3690 3691 3692def _norm_arg(arg): 3693 return arg.lower() if isinstance(arg, str) else arg 3694 3695 3696ALL_FUNCTIONS = subclasses(__name__, Func, (AggFunc, Anonymous, Func)) 3697 3698 3699# Helpers 3700def maybe_parse( 3701 sql_or_expression: str | Expression, 3702 *, 3703 into: t.Optional[IntoType] = None, 3704 dialect: DialectType = None, 3705 prefix: t.Optional[str] = None, 3706 copy: bool = False, 3707 **opts, 3708) -> Expression: 3709 """Gracefully handle a possible string or expression. 3710 3711 Example: 3712 >>> maybe_parse("1") 3713 (LITERAL this: 1, is_string: False) 3714 >>> maybe_parse(to_identifier("x")) 3715 (IDENTIFIER this: x, quoted: False) 3716 3717 Args: 3718 sql_or_expression: the SQL code string or an expression 3719 into: the SQLGlot Expression to parse into 3720 dialect: the dialect used to parse the input expressions (in the case that an 3721 input expression is a SQL string). 3722 prefix: a string to prefix the sql with before it gets parsed 3723 (automatically includes a space) 3724 copy: whether or not to copy the expression. 3725 **opts: other options to use to parse the input expressions (again, in the case 3726 that an input expression is a SQL string). 3727 3728 Returns: 3729 Expression: the parsed or given expression. 3730 """ 3731 if isinstance(sql_or_expression, Expression): 3732 if copy: 3733 return sql_or_expression.copy() 3734 return sql_or_expression 3735 3736 import sqlglot 3737 3738 sql = str(sql_or_expression) 3739 if prefix: 3740 sql = f"{prefix} {sql}" 3741 return sqlglot.parse_one(sql, read=dialect, into=into, **opts) 3742 3743 3744def _maybe_copy(instance, copy=True): 3745 return instance.copy() if copy else instance 3746 3747 3748def _is_wrong_expression(expression, into): 3749 return isinstance(expression, Expression) and not isinstance(expression, into) 3750 3751 3752def _apply_builder( 3753 expression, 3754 instance, 3755 arg, 3756 copy=True, 3757 prefix=None, 3758 into=None, 3759 dialect=None, 3760 **opts, 3761): 3762 if _is_wrong_expression(expression, into): 3763 expression = into(this=expression) 3764 instance = _maybe_copy(instance, copy) 3765 expression = maybe_parse( 3766 sql_or_expression=expression, 3767 prefix=prefix, 3768 into=into, 3769 dialect=dialect, 3770 **opts, 3771 ) 3772 instance.set(arg, expression) 3773 return instance 3774 3775 3776def _apply_child_list_builder( 3777 *expressions, 3778 instance, 3779 arg, 3780 append=True, 3781 copy=True, 3782 prefix=None, 3783 into=None, 3784 dialect=None, 3785 properties=None, 3786 **opts, 3787): 3788 instance = _maybe_copy(instance, copy) 3789 parsed = [] 3790 for expression in expressions: 3791 if _is_wrong_expression(expression, into): 3792 expression = into(expressions=[expression]) 3793 expression = maybe_parse( 3794 expression, 3795 into=into, 3796 dialect=dialect, 3797 prefix=prefix, 3798 **opts, 3799 ) 3800 parsed.extend(expression.expressions) 3801 3802 existing = instance.args.get(arg) 3803 if append and existing: 3804 parsed = existing.expressions + parsed 3805 3806 child = into(expressions=parsed) 3807 for k, v in (properties or {}).items(): 3808 child.set(k, v) 3809 instance.set(arg, child) 3810 return instance 3811 3812 3813def _apply_list_builder( 3814 *expressions, 3815 instance, 3816 arg, 3817 append=True, 3818 copy=True, 3819 prefix=None, 3820 into=None, 3821 dialect=None, 3822 **opts, 3823): 3824 inst = _maybe_copy(instance, copy) 3825 3826 expressions = [ 3827 maybe_parse( 3828 sql_or_expression=expression, 3829 into=into, 3830 prefix=prefix, 3831 dialect=dialect, 3832 **opts, 3833 ) 3834 for expression in expressions 3835 ] 3836 3837 existing_expressions = inst.args.get(arg) 3838 if append and existing_expressions: 3839 expressions = existing_expressions + expressions 3840 3841 inst.set(arg, expressions) 3842 return inst 3843 3844 3845def _apply_conjunction_builder( 3846 *expressions, 3847 instance, 3848 arg, 3849 into=None, 3850 append=True, 3851 copy=True, 3852 dialect=None, 3853 **opts, 3854): 3855 expressions = [exp for exp in expressions if exp is not None and exp != ""] 3856 if not expressions: 3857 return instance 3858 3859 inst = _maybe_copy(instance, copy) 3860 3861 existing = inst.args.get(arg) 3862 if append and existing is not None: 3863 expressions = [existing.this if into else existing] + list(expressions) 3864 3865 node = and_(*expressions, dialect=dialect, **opts) 3866 3867 inst.set(arg, into(this=node) if into else node) 3868 return inst 3869 3870 3871def _combine(expressions, operator, dialect=None, **opts): 3872 expressions = [condition(expression, dialect=dialect, **opts) for expression in expressions] 3873 this = expressions[0] 3874 if expressions[1:]: 3875 this = _wrap_operator(this) 3876 for expression in expressions[1:]: 3877 this = operator(this=this, expression=_wrap_operator(expression)) 3878 return this 3879 3880 3881def _wrap_operator(expression): 3882 if isinstance(expression, (And, Or, Not)): 3883 expression = Paren(this=expression) 3884 return expression 3885 3886 3887def union(left, right, distinct=True, dialect=None, **opts): 3888 """ 3889 Initializes a syntax tree from one UNION expression. 3890 3891 Example: 3892 >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql() 3893 'SELECT * FROM foo UNION SELECT * FROM bla' 3894 3895 Args: 3896 left (str | Expression): the SQL code string corresponding to the left-hand side. 3897 If an `Expression` instance is passed, it will be used as-is. 3898 right (str | Expression): the SQL code string corresponding to the right-hand side. 3899 If an `Expression` instance is passed, it will be used as-is. 3900 distinct (bool): set the DISTINCT flag if and only if this is true. 3901 dialect (str): the dialect used to parse the input expression. 3902 opts (kwargs): other options to use to parse the input expressions. 3903 Returns: 3904 Union: the syntax tree for the UNION expression. 3905 """ 3906 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 3907 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 3908 3909 return Union(this=left, expression=right, distinct=distinct) 3910 3911 3912def intersect(left, right, distinct=True, dialect=None, **opts): 3913 """ 3914 Initializes a syntax tree from one INTERSECT expression. 3915 3916 Example: 3917 >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql() 3918 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 3919 3920 Args: 3921 left (str | Expression): the SQL code string corresponding to the left-hand side. 3922 If an `Expression` instance is passed, it will be used as-is. 3923 right (str | Expression): the SQL code string corresponding to the right-hand side. 3924 If an `Expression` instance is passed, it will be used as-is. 3925 distinct (bool): set the DISTINCT flag if and only if this is true. 3926 dialect (str): the dialect used to parse the input expression. 3927 opts (kwargs): other options to use to parse the input expressions. 3928 Returns: 3929 Intersect: the syntax tree for the INTERSECT expression. 3930 """ 3931 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 3932 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 3933 3934 return Intersect(this=left, expression=right, distinct=distinct) 3935 3936 3937def except_(left, right, distinct=True, dialect=None, **opts): 3938 """ 3939 Initializes a syntax tree from one EXCEPT expression. 3940 3941 Example: 3942 >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql() 3943 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 3944 3945 Args: 3946 left (str | Expression): the SQL code string corresponding to the left-hand side. 3947 If an `Expression` instance is passed, it will be used as-is. 3948 right (str | Expression): the SQL code string corresponding to the right-hand side. 3949 If an `Expression` instance is passed, it will be used as-is. 3950 distinct (bool): set the DISTINCT flag if and only if this is true. 3951 dialect (str): the dialect used to parse the input expression. 3952 opts (kwargs): other options to use to parse the input expressions. 3953 Returns: 3954 Except: the syntax tree for the EXCEPT statement. 3955 """ 3956 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 3957 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 3958 3959 return Except(this=left, expression=right, distinct=distinct) 3960 3961 3962def select(*expressions: str | Expression, dialect: DialectType = None, **opts) -> Select: 3963 """ 3964 Initializes a syntax tree from one or multiple SELECT expressions. 3965 3966 Example: 3967 >>> select("col1", "col2").from_("tbl").sql() 3968 'SELECT col1, col2 FROM tbl' 3969 3970 Args: 3971 *expressions: the SQL code string to parse as the expressions of a 3972 SELECT statement. If an Expression instance is passed, this is used as-is. 3973 dialect: the dialect used to parse the input expressions (in the case that an 3974 input expression is a SQL string). 3975 **opts: other options to use to parse the input expressions (again, in the case 3976 that an input expression is a SQL string). 3977 3978 Returns: 3979 Select: the syntax tree for the SELECT statement. 3980 """ 3981 return Select().select(*expressions, dialect=dialect, **opts) 3982 3983 3984def from_(*expressions, dialect=None, **opts) -> Select: 3985 """ 3986 Initializes a syntax tree from a FROM expression. 3987 3988 Example: 3989 >>> from_("tbl").select("col1", "col2").sql() 3990 'SELECT col1, col2 FROM tbl' 3991 3992 Args: 3993 *expressions (str | Expression): the SQL code string to parse as the FROM expressions of a 3994 SELECT statement. If an Expression instance is passed, this is used as-is. 3995 dialect (str): the dialect used to parse the input expression (in the case that the 3996 input expression is a SQL string). 3997 **opts: other options to use to parse the input expressions (again, in the case 3998 that the input expression is a SQL string). 3999 4000 Returns: 4001 Select: the syntax tree for the SELECT statement. 4002 """ 4003 return Select().from_(*expressions, dialect=dialect, **opts) 4004 4005 4006def update(table, properties, where=None, from_=None, dialect=None, **opts) -> Update: 4007 """ 4008 Creates an update statement. 4009 4010 Example: 4011 >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql() 4012 "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1" 4013 4014 Args: 4015 *properties (Dict[str, Any]): dictionary of properties to set which are 4016 auto converted to sql objects eg None -> NULL 4017 where (str): sql conditional parsed into a WHERE statement 4018 from_ (str): sql statement parsed into a FROM statement 4019 dialect (str): the dialect used to parse the input expressions. 4020 **opts: other options to use to parse the input expressions. 4021 4022 Returns: 4023 Update: the syntax tree for the UPDATE statement. 4024 """ 4025 update = Update(this=maybe_parse(table, into=Table, dialect=dialect)) 4026 update.set( 4027 "expressions", 4028 [ 4029 EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v)) 4030 for k, v in properties.items() 4031 ], 4032 ) 4033 if from_: 4034 update.set( 4035 "from", 4036 maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts), 4037 ) 4038 if isinstance(where, Condition): 4039 where = Where(this=where) 4040 if where: 4041 update.set( 4042 "where", 4043 maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts), 4044 ) 4045 return update 4046 4047 4048def delete(table, where=None, dialect=None, **opts) -> Delete: 4049 """ 4050 Builds a delete statement. 4051 4052 Example: 4053 >>> delete("my_table", where="id > 1").sql() 4054 'DELETE FROM my_table WHERE id > 1' 4055 4056 Args: 4057 where (str|Condition): sql conditional parsed into a WHERE statement 4058 dialect (str): the dialect used to parse the input expressions. 4059 **opts: other options to use to parse the input expressions. 4060 4061 Returns: 4062 Delete: the syntax tree for the DELETE statement. 4063 """ 4064 return Delete( 4065 this=maybe_parse(table, into=Table, dialect=dialect, **opts), 4066 where=Where(this=where) 4067 if isinstance(where, Condition) 4068 else maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts), 4069 ) 4070 4071 4072def condition(expression, dialect=None, **opts) -> Condition: 4073 """ 4074 Initialize a logical condition expression. 4075 4076 Example: 4077 >>> condition("x=1").sql() 4078 'x = 1' 4079 4080 This is helpful for composing larger logical syntax trees: 4081 >>> where = condition("x=1") 4082 >>> where = where.and_("y=1") 4083 >>> Select().from_("tbl").select("*").where(where).sql() 4084 'SELECT * FROM tbl WHERE x = 1 AND y = 1' 4085 4086 Args: 4087 *expression (str | Expression): the SQL code string to parse. 4088 If an Expression instance is passed, this is used as-is. 4089 dialect (str): the dialect used to parse the input expression (in the case that the 4090 input expression is a SQL string). 4091 **opts: other options to use to parse the input expressions (again, in the case 4092 that the input expression is a SQL string). 4093 4094 Returns: 4095 Condition: the expression 4096 """ 4097 return maybe_parse( # type: ignore 4098 expression, 4099 into=Condition, 4100 dialect=dialect, 4101 **opts, 4102 ) 4103 4104 4105def and_(*expressions, dialect=None, **opts) -> And: 4106 """ 4107 Combine multiple conditions with an AND logical operator. 4108 4109 Example: 4110 >>> and_("x=1", and_("y=1", "z=1")).sql() 4111 'x = 1 AND (y = 1 AND z = 1)' 4112 4113 Args: 4114 *expressions (str | Expression): the SQL code strings to parse. 4115 If an Expression instance is passed, this is used as-is. 4116 dialect (str): the dialect used to parse the input expression. 4117 **opts: other options to use to parse the input expressions. 4118 4119 Returns: 4120 And: the new condition 4121 """ 4122 return _combine(expressions, And, dialect, **opts) 4123 4124 4125def or_(*expressions, dialect=None, **opts) -> Or: 4126 """ 4127 Combine multiple conditions with an OR logical operator. 4128 4129 Example: 4130 >>> or_("x=1", or_("y=1", "z=1")).sql() 4131 'x = 1 OR (y = 1 OR z = 1)' 4132 4133 Args: 4134 *expressions (str | Expression): the SQL code strings to parse. 4135 If an Expression instance is passed, this is used as-is. 4136 dialect (str): the dialect used to parse the input expression. 4137 **opts: other options to use to parse the input expressions. 4138 4139 Returns: 4140 Or: the new condition 4141 """ 4142 return _combine(expressions, Or, dialect, **opts) 4143 4144 4145def not_(expression, dialect=None, **opts) -> Not: 4146 """ 4147 Wrap a condition with a NOT operator. 4148 4149 Example: 4150 >>> not_("this_suit='black'").sql() 4151 "NOT this_suit = 'black'" 4152 4153 Args: 4154 expression (str | Expression): the SQL code strings to parse. 4155 If an Expression instance is passed, this is used as-is. 4156 dialect (str): the dialect used to parse the input expression. 4157 **opts: other options to use to parse the input expressions. 4158 4159 Returns: 4160 Not: the new condition 4161 """ 4162 this = condition( 4163 expression, 4164 dialect=dialect, 4165 **opts, 4166 ) 4167 return Not(this=_wrap_operator(this)) 4168 4169 4170def paren(expression) -> Paren: 4171 return Paren(this=expression) 4172 4173 4174SAFE_IDENTIFIER_RE = re.compile(r"^[_a-zA-Z][\w]*$") 4175 4176 4177@t.overload 4178def to_identifier(name: None, quoted: t.Optional[bool] = None) -> None: 4179 ... 4180 4181 4182@t.overload 4183def to_identifier(name: str | Identifier, quoted: t.Optional[bool] = None) -> Identifier: 4184 ... 4185 4186 4187def to_identifier(name, quoted=None): 4188 """Builds an identifier. 4189 4190 Args: 4191 name: The name to turn into an identifier. 4192 quoted: Whether or not force quote the identifier. 4193 4194 Returns: 4195 The identifier ast node. 4196 """ 4197 4198 if name is None: 4199 return None 4200 4201 if isinstance(name, Identifier): 4202 identifier = name 4203 elif isinstance(name, str): 4204 identifier = Identifier( 4205 this=name, 4206 quoted=not re.match(SAFE_IDENTIFIER_RE, name) if quoted is None else quoted, 4207 ) 4208 else: 4209 raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}") 4210 return identifier 4211 4212 4213INTERVAL_STRING_RE = re.compile(r"\s*([0-9]+)\s*([a-zA-Z]+)\s*") 4214 4215 4216def to_interval(interval: str | Literal) -> Interval: 4217 """Builds an interval expression from a string like '1 day' or '5 months'.""" 4218 if isinstance(interval, Literal): 4219 if not interval.is_string: 4220 raise ValueError("Invalid interval string.") 4221 4222 interval = interval.this 4223 4224 interval_parts = INTERVAL_STRING_RE.match(interval) # type: ignore 4225 4226 if not interval_parts: 4227 raise ValueError("Invalid interval string.") 4228 4229 return Interval( 4230 this=Literal.string(interval_parts.group(1)), 4231 unit=Var(this=interval_parts.group(2)), 4232 ) 4233 4234 4235@t.overload 4236def to_table(sql_path: str | Table, **kwargs) -> Table: 4237 ... 4238 4239 4240@t.overload 4241def to_table(sql_path: None, **kwargs) -> None: 4242 ... 4243 4244 4245def to_table(sql_path: t.Optional[str | Table], **kwargs) -> t.Optional[Table]: 4246 """ 4247 Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional. 4248 If a table is passed in then that table is returned. 4249 4250 Args: 4251 sql_path: a `[catalog].[schema].[table]` string. 4252 4253 Returns: 4254 A table expression. 4255 """ 4256 if sql_path is None or isinstance(sql_path, Table): 4257 return sql_path 4258 if not isinstance(sql_path, str): 4259 raise ValueError(f"Invalid type provided for a table: {type(sql_path)}") 4260 4261 catalog, db, table_name = (to_identifier(x) for x in split_num_words(sql_path, ".", 3)) 4262 return Table(this=table_name, db=db, catalog=catalog, **kwargs) 4263 4264 4265def to_column(sql_path: str | Column, **kwargs) -> Column: 4266 """ 4267 Create a column from a `[table].[column]` sql path. Schema is optional. 4268 4269 If a column is passed in then that column is returned. 4270 4271 Args: 4272 sql_path: `[table].[column]` string 4273 Returns: 4274 Table: A column expression 4275 """ 4276 if sql_path is None or isinstance(sql_path, Column): 4277 return sql_path 4278 if not isinstance(sql_path, str): 4279 raise ValueError(f"Invalid type provided for column: {type(sql_path)}") 4280 table_name, column_name = (to_identifier(x) for x in split_num_words(sql_path, ".", 2)) 4281 return Column(this=column_name, table=table_name, **kwargs) 4282 4283 4284def alias_( 4285 expression: str | Expression, 4286 alias: str | Identifier, 4287 table: bool | t.Sequence[str | Identifier] = False, 4288 quoted: t.Optional[bool] = None, 4289 dialect: DialectType = None, 4290 **opts, 4291): 4292 """Create an Alias expression. 4293 4294 Example: 4295 >>> alias_('foo', 'bar').sql() 4296 'foo AS bar' 4297 4298 >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql() 4299 '(SELECT 1, 2) AS bar(a, b)' 4300 4301 Args: 4302 expression: the SQL code strings to parse. 4303 If an Expression instance is passed, this is used as-is. 4304 alias: the alias name to use. If the name has 4305 special characters it is quoted. 4306 table: Whether or not to create a table alias, can also be a list of columns. 4307 quoted: whether or not to quote the alias 4308 dialect: the dialect used to parse the input expression. 4309 **opts: other options to use to parse the input expressions. 4310 4311 Returns: 4312 Alias: the aliased expression 4313 """ 4314 exp = maybe_parse(expression, dialect=dialect, **opts) 4315 alias = to_identifier(alias, quoted=quoted) 4316 4317 if table: 4318 table_alias = TableAlias(this=alias) 4319 exp.set("alias", table_alias) 4320 4321 if not isinstance(table, bool): 4322 for column in table: 4323 table_alias.append("columns", to_identifier(column, quoted=quoted)) 4324 4325 return exp 4326 4327 # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in 4328 # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node 4329 # for the complete Window expression. 4330 # 4331 # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls 4332 4333 if "alias" in exp.arg_types and not isinstance(exp, Window): 4334 exp = exp.copy() 4335 exp.set("alias", alias) 4336 return exp 4337 return Alias(this=exp, alias=alias) 4338 4339 4340def subquery(expression, alias=None, dialect=None, **opts): 4341 """ 4342 Build a subquery expression. 4343 4344 Example: 4345 >>> subquery('select x from tbl', 'bar').select('x').sql() 4346 'SELECT x FROM (SELECT x FROM tbl) AS bar' 4347 4348 Args: 4349 expression (str | Expression): the SQL code strings to parse. 4350 If an Expression instance is passed, this is used as-is. 4351 alias (str | Expression): the alias name to use. 4352 dialect (str): the dialect used to parse the input expression. 4353 **opts: other options to use to parse the input expressions. 4354 4355 Returns: 4356 Select: a new select with the subquery expression included 4357 """ 4358 4359 expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias) 4360 return Select().from_(expression, dialect=dialect, **opts) 4361 4362 4363def column( 4364 col: str | Identifier, 4365 table: t.Optional[str | Identifier] = None, 4366 schema: t.Optional[str | Identifier] = None, 4367 quoted: t.Optional[bool] = None, 4368) -> Column: 4369 """ 4370 Build a Column. 4371 4372 Args: 4373 col: column name 4374 table: table name 4375 schema: schema name 4376 quoted: whether or not to force quote each part 4377 Returns: 4378 Column: column instance 4379 """ 4380 return Column( 4381 this=to_identifier(col, quoted=quoted), 4382 table=to_identifier(table, quoted=quoted), 4383 schema=to_identifier(schema, quoted=quoted), 4384 ) 4385 4386 4387def cast(expression: str | Expression, to: str | DataType | DataType.Type, **opts) -> Cast: 4388 """Cast an expression to a data type. 4389 4390 Example: 4391 >>> cast('x + 1', 'int').sql() 4392 'CAST(x + 1 AS INT)' 4393 4394 Args: 4395 expression: The expression to cast. 4396 to: The datatype to cast to. 4397 4398 Returns: 4399 A cast node. 4400 """ 4401 expression = maybe_parse(expression, **opts) 4402 return Cast(this=expression, to=DataType.build(to, **opts)) 4403 4404 4405def table_(table, db=None, catalog=None, quoted=None, alias=None) -> Table: 4406 """Build a Table. 4407 4408 Args: 4409 table (str | Expression): column name 4410 db (str | Expression): db name 4411 catalog (str | Expression): catalog name 4412 4413 Returns: 4414 Table: table instance 4415 """ 4416 return Table( 4417 this=to_identifier(table, quoted=quoted), 4418 db=to_identifier(db, quoted=quoted), 4419 catalog=to_identifier(catalog, quoted=quoted), 4420 alias=TableAlias(this=to_identifier(alias)) if alias else None, 4421 ) 4422 4423 4424def values( 4425 values: t.Iterable[t.Tuple[t.Any, ...]], 4426 alias: t.Optional[str] = None, 4427 columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None, 4428) -> Values: 4429 """Build VALUES statement. 4430 4431 Example: 4432 >>> values([(1, '2')]).sql() 4433 "VALUES (1, '2')" 4434 4435 Args: 4436 values: values statements that will be converted to SQL 4437 alias: optional alias 4438 columns: Optional list of ordered column names or ordered dictionary of column names to types. 4439 If either are provided then an alias is also required. 4440 If a dictionary is provided then the first column of the values will be casted to the expected type 4441 in order to help with type inference. 4442 4443 Returns: 4444 Values: the Values expression object 4445 """ 4446 if columns and not alias: 4447 raise ValueError("Alias is required when providing columns") 4448 table_alias = ( 4449 TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns]) 4450 if columns 4451 else TableAlias(this=to_identifier(alias) if alias else None) 4452 ) 4453 expressions = [convert(tup) for tup in values] 4454 if columns and isinstance(columns, dict): 4455 types = list(columns.values()) 4456 expressions[0].set( 4457 "expressions", 4458 [cast(x, types[i]) for i, x in enumerate(expressions[0].expressions)], 4459 ) 4460 return Values( 4461 expressions=expressions, 4462 alias=table_alias, 4463 ) 4464 4465 4466def var(name: t.Optional[str | Expression]) -> Var: 4467 """Build a SQL variable. 4468 4469 Example: 4470 >>> repr(var('x')) 4471 '(VAR this: x)' 4472 4473 >>> repr(var(column('x', table='y'))) 4474 '(VAR this: x)' 4475 4476 Args: 4477 name: The name of the var or an expression who's name will become the var. 4478 4479 Returns: 4480 The new variable node. 4481 """ 4482 if not name: 4483 raise ValueError(f"Cannot convert empty name into var.") 4484 4485 if isinstance(name, Expression): 4486 name = name.name 4487 return Var(this=name) 4488 4489 4490def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable: 4491 """Build ALTER TABLE... RENAME... expression 4492 4493 Args: 4494 old_name: The old name of the table 4495 new_name: The new name of the table 4496 4497 Returns: 4498 Alter table expression 4499 """ 4500 old_table = to_table(old_name) 4501 new_table = to_table(new_name) 4502 return AlterTable( 4503 this=old_table, 4504 actions=[ 4505 RenameTable(this=new_table), 4506 ], 4507 ) 4508 4509 4510def convert(value) -> Expression: 4511 """Convert a python value into an expression object. 4512 4513 Raises an error if a conversion is not possible. 4514 4515 Args: 4516 value (Any): a python object 4517 4518 Returns: 4519 Expression: the equivalent expression object 4520 """ 4521 if isinstance(value, Expression): 4522 return value 4523 if value is None: 4524 return NULL 4525 if isinstance(value, bool): 4526 return Boolean(this=value) 4527 if isinstance(value, str): 4528 return Literal.string(value) 4529 if isinstance(value, float) and math.isnan(value): 4530 return NULL 4531 if isinstance(value, numbers.Number): 4532 return Literal.number(value) 4533 if isinstance(value, tuple): 4534 return Tuple(expressions=[convert(v) for v in value]) 4535 if isinstance(value, list): 4536 return Array(expressions=[convert(v) for v in value]) 4537 if isinstance(value, dict): 4538 return Map( 4539 keys=[convert(k) for k in value], 4540 values=[convert(v) for v in value.values()], 4541 ) 4542 if isinstance(value, datetime.datetime): 4543 datetime_literal = Literal.string( 4544 (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat() 4545 ) 4546 return TimeStrToTime(this=datetime_literal) 4547 if isinstance(value, datetime.date): 4548 date_literal = Literal.string(value.strftime("%Y-%m-%d")) 4549 return DateStrToDate(this=date_literal) 4550 raise ValueError(f"Cannot convert {value}") 4551 4552 4553def replace_children(expression, fun): 4554 """ 4555 Replace children of an expression with the result of a lambda fun(child) -> exp. 4556 """ 4557 for k, v in expression.args.items(): 4558 is_list_arg = isinstance(v, list) 4559 4560 child_nodes = v if is_list_arg else [v] 4561 new_child_nodes = [] 4562 4563 for cn in child_nodes: 4564 if isinstance(cn, Expression): 4565 for child_node in ensure_collection(fun(cn)): 4566 new_child_nodes.append(child_node) 4567 child_node.parent = expression 4568 child_node.arg_key = k 4569 else: 4570 new_child_nodes.append(cn) 4571 4572 expression.args[k] = new_child_nodes if is_list_arg else seq_get(new_child_nodes, 0) 4573 4574 4575def column_table_names(expression): 4576 """ 4577 Return all table names referenced through columns in an expression. 4578 4579 Example: 4580 >>> import sqlglot 4581 >>> column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")) 4582 ['c', 'a'] 4583 4584 Args: 4585 expression (sqlglot.Expression): expression to find table names 4586 4587 Returns: 4588 list: A list of unique names 4589 """ 4590 return list(dict.fromkeys(column.table for column in expression.find_all(Column))) 4591 4592 4593def table_name(table) -> str: 4594 """Get the full name of a table as a string. 4595 4596 Args: 4597 table (exp.Table | str): table expression node or string. 4598 4599 Examples: 4600 >>> from sqlglot import exp, parse_one 4601 >>> table_name(parse_one("select * from a.b.c").find(exp.Table)) 4602 'a.b.c' 4603 4604 Returns: 4605 The table name. 4606 """ 4607 4608 table = maybe_parse(table, into=Table) 4609 4610 if not table: 4611 raise ValueError(f"Cannot parse {table}") 4612 4613 return ".".join( 4614 part 4615 for part in ( 4616 table.text("catalog"), 4617 table.text("db"), 4618 table.name, 4619 ) 4620 if part 4621 ) 4622 4623 4624def replace_tables(expression, mapping): 4625 """Replace all tables in expression according to the mapping. 4626 4627 Args: 4628 expression (sqlglot.Expression): expression node to be transformed and replaced. 4629 mapping (Dict[str, str]): mapping of table names. 4630 4631 Examples: 4632 >>> from sqlglot import exp, parse_one 4633 >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql() 4634 'SELECT * FROM c' 4635 4636 Returns: 4637 The mapped expression. 4638 """ 4639 4640 def _replace_tables(node): 4641 if isinstance(node, Table): 4642 new_name = mapping.get(table_name(node)) 4643 if new_name: 4644 return to_table( 4645 new_name, 4646 **{k: v for k, v in node.args.items() if k not in ("this", "db", "catalog")}, 4647 ) 4648 return node 4649 4650 return expression.transform(_replace_tables) 4651 4652 4653def replace_placeholders(expression, *args, **kwargs): 4654 """Replace placeholders in an expression. 4655 4656 Args: 4657 expression (sqlglot.Expression): expression node to be transformed and replaced. 4658 args: positional names that will substitute unnamed placeholders in the given order. 4659 kwargs: keyword arguments that will substitute named placeholders. 4660 4661 Examples: 4662 >>> from sqlglot import exp, parse_one 4663 >>> replace_placeholders( 4664 ... parse_one("select * from :tbl where ? = ?"), "a", "b", tbl="foo" 4665 ... ).sql() 4666 'SELECT * FROM foo WHERE a = b' 4667 4668 Returns: 4669 The mapped expression. 4670 """ 4671 4672 def _replace_placeholders(node, args, **kwargs): 4673 if isinstance(node, Placeholder): 4674 if node.name: 4675 new_name = kwargs.get(node.name) 4676 if new_name: 4677 return to_identifier(new_name) 4678 else: 4679 try: 4680 return to_identifier(next(args)) 4681 except StopIteration: 4682 pass 4683 return node 4684 4685 return expression.transform(_replace_placeholders, iter(args), **kwargs) 4686 4687 4688def expand(expression: Expression, sources: t.Dict[str, Subqueryable], copy=True) -> Expression: 4689 """Transforms an expression by expanding all referenced sources into subqueries. 4690 4691 Examples: 4692 >>> from sqlglot import parse_one 4693 >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql() 4694 'SELECT * FROM (SELECT * FROM y) AS z /* source: x */' 4695 4696 Args: 4697 expression: The expression to expand. 4698 sources: A dictionary of name to Subqueryables. 4699 copy: Whether or not to copy the expression during transformation. Defaults to True. 4700 4701 Returns: 4702 The transformed expression. 4703 """ 4704 4705 def _expand(node: Expression): 4706 if isinstance(node, Table): 4707 name = table_name(node) 4708 source = sources.get(name) 4709 if source: 4710 subquery = source.subquery(node.alias or name) 4711 subquery.comments = [f"source: {name}"] 4712 return subquery 4713 return node 4714 4715 return expression.transform(_expand, copy=copy) 4716 4717 4718def func(name: str, *args, dialect: DialectType = None, **kwargs) -> Func: 4719 """ 4720 Returns a Func expression. 4721 4722 Examples: 4723 >>> func("abs", 5).sql() 4724 'ABS(5)' 4725 4726 >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql() 4727 'CAST(5 AS DOUBLE)' 4728 4729 Args: 4730 name: the name of the function to build. 4731 args: the args used to instantiate the function of interest. 4732 dialect: the source dialect. 4733 kwargs: the kwargs used to instantiate the function of interest. 4734 4735 Note: 4736 The arguments `args` and `kwargs` are mutually exclusive. 4737 4738 Returns: 4739 An instance of the function of interest, or an anonymous function, if `name` doesn't 4740 correspond to an existing `sqlglot.expressions.Func` class. 4741 """ 4742 if args and kwargs: 4743 raise ValueError("Can't use both args and kwargs to instantiate a function.") 4744 4745 from sqlglot.dialects.dialect import Dialect 4746 4747 args = tuple(convert(arg) for arg in args) 4748 kwargs = {key: convert(value) for key, value in kwargs.items()} 4749 4750 parser = Dialect.get_or_raise(dialect)().parser() 4751 from_args_list = parser.FUNCTIONS.get(name.upper()) 4752 4753 if from_args_list: 4754 function = from_args_list(args) if args else from_args_list.__self__(**kwargs) # type: ignore 4755 else: 4756 kwargs = kwargs or {"expressions": args} 4757 function = Anonymous(this=name, **kwargs) 4758 4759 for error_message in function.error_messages(args): 4760 raise ValueError(error_message) 4761 4762 return function 4763 4764 4765def true(): 4766 """ 4767 Returns a true Boolean expression. 4768 """ 4769 return Boolean(this=True) 4770 4771 4772def false(): 4773 """ 4774 Returns a false Boolean expression. 4775 """ 4776 return Boolean(this=False) 4777 4778 4779def null(): 4780 """ 4781 Returns a Null expression. 4782 """ 4783 return Null() 4784 4785 4786# TODO: deprecate this 4787TRUE = Boolean(this=True) 4788FALSE = Boolean(this=False) 4789NULL = Null()
54class Expression(metaclass=_Expression): 55 """ 56 The base class for all expressions in a syntax tree. Each Expression encapsulates any necessary 57 context, such as its child expressions, their names (arg keys), and whether a given child expression 58 is optional or not. 59 60 Attributes: 61 key: a unique key for each class in the Expression hierarchy. This is useful for hashing 62 and representing expressions as strings. 63 arg_types: determines what arguments (child nodes) are supported by an expression. It 64 maps arg keys to booleans that indicate whether the corresponding args are optional. 65 66 Example: 67 >>> class Foo(Expression): 68 ... arg_types = {"this": True, "expression": False} 69 70 The above definition informs us that Foo is an Expression that requires an argument called 71 "this" and may also optionally receive an argument called "expression". 72 73 Args: 74 args: a mapping used for retrieving the arguments of an expression, given their arg keys. 75 parent: a reference to the parent expression (or None, in case of root expressions). 76 arg_key: the arg key an expression is associated with, i.e. the name its parent expression 77 uses to refer to it. 78 comments: a list of comments that are associated with a given expression. This is used in 79 order to preserve comments when transpiling SQL code. 80 _type: the `sqlglot.expressions.DataType` type of an expression. This is inferred by the 81 optimizer, in order to enable some transformations that require type information. 82 """ 83 84 key = "expression" 85 arg_types = {"this": True} 86 __slots__ = ("args", "parent", "arg_key", "comments", "_type") 87 88 def __init__(self, **args: t.Any): 89 self.args: t.Dict[str, t.Any] = args 90 self.parent: t.Optional[Expression] = None 91 self.arg_key: t.Optional[str] = None 92 self.comments: t.Optional[t.List[str]] = None 93 self._type: t.Optional[DataType] = None 94 95 for arg_key, value in self.args.items(): 96 self._set_parent(arg_key, value) 97 98 def __eq__(self, other) -> bool: 99 return type(self) is type(other) and _norm_args(self) == _norm_args(other) 100 101 def __hash__(self) -> int: 102 return hash( 103 ( 104 self.key, 105 tuple( 106 (k, tuple(v) if isinstance(v, list) else v) for k, v in _norm_args(self).items() 107 ), 108 ) 109 ) 110 111 @property 112 def this(self): 113 """ 114 Retrieves the argument with key "this". 115 """ 116 return self.args.get("this") 117 118 @property 119 def expression(self): 120 """ 121 Retrieves the argument with key "expression". 122 """ 123 return self.args.get("expression") 124 125 @property 126 def expressions(self): 127 """ 128 Retrieves the argument with key "expressions". 129 """ 130 return self.args.get("expressions") or [] 131 132 def text(self, key) -> str: 133 """ 134 Returns a textual representation of the argument corresponding to "key". This can only be used 135 for args that are strings or leaf Expression instances, such as identifiers and literals. 136 """ 137 field = self.args.get(key) 138 if isinstance(field, str): 139 return field 140 if isinstance(field, (Identifier, Literal, Var)): 141 return field.this 142 if isinstance(field, (Star, Null)): 143 return field.name 144 return "" 145 146 @property 147 def is_string(self) -> bool: 148 """ 149 Checks whether a Literal expression is a string. 150 """ 151 return isinstance(self, Literal) and self.args["is_string"] 152 153 @property 154 def is_number(self) -> bool: 155 """ 156 Checks whether a Literal expression is a number. 157 """ 158 return isinstance(self, Literal) and not self.args["is_string"] 159 160 @property 161 def is_int(self) -> bool: 162 """ 163 Checks whether a Literal expression is an integer. 164 """ 165 if self.is_number: 166 try: 167 int(self.name) 168 return True 169 except ValueError: 170 pass 171 return False 172 173 @property 174 def is_star(self) -> bool: 175 """Checks whether an expression is a star.""" 176 return isinstance(self, Star) or (isinstance(self, Column) and isinstance(self.this, Star)) 177 178 @property 179 def alias(self) -> str: 180 """ 181 Returns the alias of the expression, or an empty string if it's not aliased. 182 """ 183 if isinstance(self.args.get("alias"), TableAlias): 184 return self.args["alias"].name 185 return self.text("alias") 186 187 @property 188 def name(self) -> str: 189 return self.text("this") 190 191 @property 192 def alias_or_name(self): 193 return self.alias or self.name 194 195 @property 196 def output_name(self): 197 """ 198 Name of the output column if this expression is a selection. 199 200 If the Expression has no output name, an empty string is returned. 201 202 Example: 203 >>> from sqlglot import parse_one 204 >>> parse_one("SELECT a").expressions[0].output_name 205 'a' 206 >>> parse_one("SELECT b AS c").expressions[0].output_name 207 'c' 208 >>> parse_one("SELECT 1 + 2").expressions[0].output_name 209 '' 210 """ 211 return "" 212 213 @property 214 def type(self) -> t.Optional[DataType]: 215 return self._type 216 217 @type.setter 218 def type(self, dtype: t.Optional[DataType | DataType.Type | str]) -> None: 219 if dtype and not isinstance(dtype, DataType): 220 dtype = DataType.build(dtype) 221 self._type = dtype # type: ignore 222 223 def __deepcopy__(self, memo): 224 copy = self.__class__(**deepcopy(self.args)) 225 copy.comments = self.comments 226 copy.type = self.type 227 return copy 228 229 def copy(self): 230 """ 231 Returns a deep copy of the expression. 232 """ 233 new = deepcopy(self) 234 new.parent = self.parent 235 for item, parent, _ in new.bfs(): 236 if isinstance(item, Expression) and parent: 237 item.parent = parent 238 return new 239 240 def append(self, arg_key, value): 241 """ 242 Appends value to arg_key if it's a list or sets it as a new list. 243 244 Args: 245 arg_key (str): name of the list expression arg 246 value (Any): value to append to the list 247 """ 248 if not isinstance(self.args.get(arg_key), list): 249 self.args[arg_key] = [] 250 self.args[arg_key].append(value) 251 self._set_parent(arg_key, value) 252 253 def set(self, arg_key, value): 254 """ 255 Sets `arg_key` to `value`. 256 257 Args: 258 arg_key (str): name of the expression arg. 259 value: value to set the arg to. 260 """ 261 self.args[arg_key] = value 262 self._set_parent(arg_key, value) 263 264 def _set_parent(self, arg_key, value): 265 if isinstance(value, Expression): 266 value.parent = self 267 value.arg_key = arg_key 268 elif isinstance(value, list): 269 for v in value: 270 if isinstance(v, Expression): 271 v.parent = self 272 v.arg_key = arg_key 273 274 @property 275 def depth(self): 276 """ 277 Returns the depth of this tree. 278 """ 279 if self.parent: 280 return self.parent.depth + 1 281 return 0 282 283 def find(self, *expression_types, bfs=True): 284 """ 285 Returns the first node in this tree which matches at least one of 286 the specified types. 287 288 Args: 289 expression_types (type): the expression type(s) to match. 290 291 Returns: 292 The node which matches the criteria or None if no such node was found. 293 """ 294 return next(self.find_all(*expression_types, bfs=bfs), None) 295 296 def find_all(self, *expression_types, bfs=True): 297 """ 298 Returns a generator object which visits all nodes in this tree and only 299 yields those that match at least one of the specified expression types. 300 301 Args: 302 expression_types (type): the expression type(s) to match. 303 304 Returns: 305 The generator object. 306 """ 307 for expression, _, _ in self.walk(bfs=bfs): 308 if isinstance(expression, expression_types): 309 yield expression 310 311 def find_ancestor(self, *expression_types): 312 """ 313 Returns a nearest parent matching expression_types. 314 315 Args: 316 expression_types (type): the expression type(s) to match. 317 318 Returns: 319 The parent node. 320 """ 321 ancestor = self.parent 322 while ancestor and not isinstance(ancestor, expression_types): 323 ancestor = ancestor.parent 324 return ancestor 325 326 @property 327 def parent_select(self): 328 """ 329 Returns the parent select statement. 330 """ 331 return self.find_ancestor(Select) 332 333 def walk(self, bfs=True, prune=None): 334 """ 335 Returns a generator object which visits all nodes in this tree. 336 337 Args: 338 bfs (bool): if set to True the BFS traversal order will be applied, 339 otherwise the DFS traversal will be used instead. 340 prune ((node, parent, arg_key) -> bool): callable that returns True if 341 the generator should stop traversing this branch of the tree. 342 343 Returns: 344 the generator object. 345 """ 346 if bfs: 347 yield from self.bfs(prune=prune) 348 else: 349 yield from self.dfs(prune=prune) 350 351 def dfs(self, parent=None, key=None, prune=None): 352 """ 353 Returns a generator object which visits all nodes in this tree in 354 the DFS (Depth-first) order. 355 356 Returns: 357 The generator object. 358 """ 359 parent = parent or self.parent 360 yield self, parent, key 361 if prune and prune(self, parent, key): 362 return 363 364 for k, v in self.args.items(): 365 for node in ensure_collection(v): 366 if isinstance(node, Expression): 367 yield from node.dfs(self, k, prune) 368 369 def bfs(self, prune=None): 370 """ 371 Returns a generator object which visits all nodes in this tree in 372 the BFS (Breadth-first) order. 373 374 Returns: 375 The generator object. 376 """ 377 queue = deque([(self, self.parent, None)]) 378 379 while queue: 380 item, parent, key = queue.popleft() 381 382 yield item, parent, key 383 if prune and prune(item, parent, key): 384 continue 385 386 if isinstance(item, Expression): 387 for k, v in item.args.items(): 388 for node in ensure_collection(v): 389 if isinstance(node, Expression): 390 queue.append((node, item, k)) 391 392 def unnest(self): 393 """ 394 Returns the first non parenthesis child or self. 395 """ 396 expression = self 397 while isinstance(expression, Paren): 398 expression = expression.this 399 return expression 400 401 def unalias(self): 402 """ 403 Returns the inner expression if this is an Alias. 404 """ 405 if isinstance(self, Alias): 406 return self.this 407 return self 408 409 def unnest_operands(self): 410 """ 411 Returns unnested operands as a tuple. 412 """ 413 return tuple(arg.unnest() for arg in self.args.values() if arg) 414 415 def flatten(self, unnest=True): 416 """ 417 Returns a generator which yields child nodes who's parents are the same class. 418 419 A AND B AND C -> [A, B, C] 420 """ 421 for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not isinstance(n, self.__class__)): 422 if not isinstance(node, self.__class__): 423 yield node.unnest() if unnest else node 424 425 def __str__(self): 426 return self.sql() 427 428 def __repr__(self): 429 return self._to_s() 430 431 def sql(self, dialect: DialectType = None, **opts) -> str: 432 """ 433 Returns SQL string representation of this tree. 434 435 Args: 436 dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql"). 437 opts: other `sqlglot.generator.Generator` options. 438 439 Returns: 440 The SQL string. 441 """ 442 from sqlglot.dialects import Dialect 443 444 return Dialect.get_or_raise(dialect)().generate(self, **opts) 445 446 def _to_s(self, hide_missing: bool = True, level: int = 0) -> str: 447 indent = "" if not level else "\n" 448 indent += "".join([" "] * level) 449 left = f"({self.key.upper()} " 450 451 args: t.Dict[str, t.Any] = { 452 k: ", ".join( 453 v._to_s(hide_missing=hide_missing, level=level + 1) 454 if hasattr(v, "_to_s") 455 else str(v) 456 for v in ensure_collection(vs) 457 if v is not None 458 ) 459 for k, vs in self.args.items() 460 } 461 args["comments"] = self.comments 462 args["type"] = self.type 463 args = {k: v for k, v in args.items() if v or not hide_missing} 464 465 right = ", ".join(f"{k}: {v}" for k, v in args.items()) 466 right += ")" 467 468 return indent + left + right 469 470 def transform(self, fun, *args, copy=True, **kwargs): 471 """ 472 Recursively visits all tree nodes (excluding already transformed ones) 473 and applies the given transformation function to each node. 474 475 Args: 476 fun (function): a function which takes a node as an argument and returns a 477 new transformed node or the same node without modifications. If the function 478 returns None, then the corresponding node will be removed from the syntax tree. 479 copy (bool): if set to True a new tree instance is constructed, otherwise the tree is 480 modified in place. 481 482 Returns: 483 The transformed tree. 484 """ 485 node = self.copy() if copy else self 486 new_node = fun(node, *args, **kwargs) 487 488 if new_node is None or not isinstance(new_node, Expression): 489 return new_node 490 if new_node is not node: 491 new_node.parent = node.parent 492 return new_node 493 494 replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs)) 495 return new_node 496 497 def replace(self, expression): 498 """ 499 Swap out this expression with a new expression. 500 501 For example:: 502 503 >>> tree = Select().select("x").from_("tbl") 504 >>> tree.find(Column).replace(Column(this="y")) 505 (COLUMN this: y) 506 >>> tree.sql() 507 'SELECT y FROM tbl' 508 509 Args: 510 expression (Expression|None): new node 511 512 Returns: 513 The new expression or expressions. 514 """ 515 if not self.parent: 516 return expression 517 518 parent = self.parent 519 self.parent = None 520 521 replace_children(parent, lambda child: expression if child is self else child) 522 return expression 523 524 def pop(self): 525 """ 526 Remove this expression from its AST. 527 """ 528 self.replace(None) 529 530 def assert_is(self, type_): 531 """ 532 Assert that this `Expression` is an instance of `type_`. 533 534 If it is NOT an instance of `type_`, this raises an assertion error. 535 Otherwise, this returns this expression. 536 537 Examples: 538 This is useful for type security in chained expressions: 539 540 >>> import sqlglot 541 >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql() 542 'SELECT x, z FROM y' 543 """ 544 assert isinstance(self, type_) 545 return self 546 547 def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]: 548 """ 549 Checks if this expression is valid (e.g. all mandatory args are set). 550 551 Args: 552 args: a sequence of values that were used to instantiate a Func expression. This is used 553 to check that the provided arguments don't exceed the function argument limit. 554 555 Returns: 556 A list of error messages for all possible errors that were found. 557 """ 558 errors: t.List[str] = [] 559 560 for k in self.args: 561 if k not in self.arg_types: 562 errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") 563 for k, mandatory in self.arg_types.items(): 564 v = self.args.get(k) 565 if mandatory and (v is None or (isinstance(v, list) and not v)): 566 errors.append(f"Required keyword: '{k}' missing for {self.__class__}") 567 568 if ( 569 args 570 and isinstance(self, Func) 571 and len(args) > len(self.arg_types) 572 and not self.is_var_len_args 573 ): 574 errors.append( 575 f"The number of provided arguments ({len(args)}) is greater than " 576 f"the maximum number of supported arguments ({len(self.arg_types)})" 577 ) 578 579 return errors 580 581 def dump(self): 582 """ 583 Dump this Expression to a JSON-serializable dict. 584 """ 585 from sqlglot.serde import dump 586 587 return dump(self) 588 589 @classmethod 590 def load(cls, obj): 591 """ 592 Load a dict (as returned by `Expression.dump`) into an Expression instance. 593 """ 594 from sqlglot.serde import load 595 596 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.
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.
- 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.DataType
type of an expression. This is inferred by the optimizer, in order to enable some transformations that require type information.
88 def __init__(self, **args: t.Any): 89 self.args: t.Dict[str, t.Any] = args 90 self.parent: t.Optional[Expression] = None 91 self.arg_key: t.Optional[str] = None 92 self.comments: t.Optional[t.List[str]] = None 93 self._type: t.Optional[DataType] = None 94 95 for arg_key, value in self.args.items(): 96 self._set_parent(arg_key, value)
132 def text(self, key) -> str: 133 """ 134 Returns a textual representation of the argument corresponding to "key". This can only be used 135 for args that are strings or leaf Expression instances, such as identifiers and literals. 136 """ 137 field = self.args.get(key) 138 if isinstance(field, str): 139 return field 140 if isinstance(field, (Identifier, Literal, Var)): 141 return field.this 142 if isinstance(field, (Star, Null)): 143 return field.name 144 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 ''
229 def copy(self): 230 """ 231 Returns a deep copy of the expression. 232 """ 233 new = deepcopy(self) 234 new.parent = self.parent 235 for item, parent, _ in new.bfs(): 236 if isinstance(item, Expression) and parent: 237 item.parent = parent 238 return new
Returns a deep copy of the expression.
240 def append(self, arg_key, value): 241 """ 242 Appends value to arg_key if it's a list or sets it as a new list. 243 244 Args: 245 arg_key (str): name of the list expression arg 246 value (Any): value to append to the list 247 """ 248 if not isinstance(self.args.get(arg_key), list): 249 self.args[arg_key] = [] 250 self.args[arg_key].append(value) 251 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
253 def set(self, arg_key, value): 254 """ 255 Sets `arg_key` to `value`. 256 257 Args: 258 arg_key (str): name of the expression arg. 259 value: value to set the arg to. 260 """ 261 self.args[arg_key] = value 262 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.
283 def find(self, *expression_types, bfs=True): 284 """ 285 Returns the first node in this tree which matches at least one of 286 the specified types. 287 288 Args: 289 expression_types (type): the expression type(s) to match. 290 291 Returns: 292 The node which matches the criteria or None if no such node was found. 293 """ 294 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 (type): the expression type(s) to match.
Returns:
The node which matches the criteria or None if no such node was found.
296 def find_all(self, *expression_types, bfs=True): 297 """ 298 Returns a generator object which visits all nodes in this tree and only 299 yields those that match at least one of the specified expression types. 300 301 Args: 302 expression_types (type): the expression type(s) to match. 303 304 Returns: 305 The generator object. 306 """ 307 for expression, _, _ in self.walk(bfs=bfs): 308 if isinstance(expression, expression_types): 309 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 (type): the expression type(s) to match.
Returns:
The generator object.
311 def find_ancestor(self, *expression_types): 312 """ 313 Returns a nearest parent matching expression_types. 314 315 Args: 316 expression_types (type): the expression type(s) to match. 317 318 Returns: 319 The parent node. 320 """ 321 ancestor = self.parent 322 while ancestor and not isinstance(ancestor, expression_types): 323 ancestor = ancestor.parent 324 return ancestor
Returns a nearest parent matching expression_types.
Arguments:
- expression_types (type): the expression type(s) to match.
Returns:
The parent node.
333 def walk(self, bfs=True, prune=None): 334 """ 335 Returns a generator object which visits all nodes in this tree. 336 337 Args: 338 bfs (bool): if set to True the BFS traversal order will be applied, 339 otherwise the DFS traversal will be used instead. 340 prune ((node, parent, arg_key) -> bool): callable that returns True if 341 the generator should stop traversing this branch of the tree. 342 343 Returns: 344 the generator object. 345 """ 346 if bfs: 347 yield from self.bfs(prune=prune) 348 else: 349 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.
351 def dfs(self, parent=None, key=None, prune=None): 352 """ 353 Returns a generator object which visits all nodes in this tree in 354 the DFS (Depth-first) order. 355 356 Returns: 357 The generator object. 358 """ 359 parent = parent or self.parent 360 yield self, parent, key 361 if prune and prune(self, parent, key): 362 return 363 364 for k, v in self.args.items(): 365 for node in ensure_collection(v): 366 if isinstance(node, Expression): 367 yield from node.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.
369 def bfs(self, prune=None): 370 """ 371 Returns a generator object which visits all nodes in this tree in 372 the BFS (Breadth-first) order. 373 374 Returns: 375 The generator object. 376 """ 377 queue = deque([(self, self.parent, None)]) 378 379 while queue: 380 item, parent, key = queue.popleft() 381 382 yield item, parent, key 383 if prune and prune(item, parent, key): 384 continue 385 386 if isinstance(item, Expression): 387 for k, v in item.args.items(): 388 for node in ensure_collection(v): 389 if isinstance(node, Expression): 390 queue.append((node, item, k))
Returns a generator object which visits all nodes in this tree in the BFS (Breadth-first) order.
Returns:
The generator object.
392 def unnest(self): 393 """ 394 Returns the first non parenthesis child or self. 395 """ 396 expression = self 397 while isinstance(expression, Paren): 398 expression = expression.this 399 return expression
Returns the first non parenthesis child or self.
401 def unalias(self): 402 """ 403 Returns the inner expression if this is an Alias. 404 """ 405 if isinstance(self, Alias): 406 return self.this 407 return self
Returns the inner expression if this is an Alias.
409 def unnest_operands(self): 410 """ 411 Returns unnested operands as a tuple. 412 """ 413 return tuple(arg.unnest() for arg in self.args.values() if arg)
Returns unnested operands as a tuple.
415 def flatten(self, unnest=True): 416 """ 417 Returns a generator which yields child nodes who's parents are the same class. 418 419 A AND B AND C -> [A, B, C] 420 """ 421 for node, _, _ in self.dfs(prune=lambda n, p, *_: p and not isinstance(n, self.__class__)): 422 if not isinstance(node, self.__class__): 423 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]
431 def sql(self, dialect: DialectType = None, **opts) -> str: 432 """ 433 Returns SQL string representation of this tree. 434 435 Args: 436 dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql"). 437 opts: other `sqlglot.generator.Generator` options. 438 439 Returns: 440 The SQL string. 441 """ 442 from sqlglot.dialects import Dialect 443 444 return Dialect.get_or_raise(dialect)().generate(self, **opts)
Returns SQL string representation of this tree.
Arguments:
- dialect: the dialect of the output SQL string (eg. "spark", "hive", "presto", "mysql").
- opts: other
sqlglot.generator.Generator
options.
Returns:
The SQL string.
470 def transform(self, fun, *args, copy=True, **kwargs): 471 """ 472 Recursively visits all tree nodes (excluding already transformed ones) 473 and applies the given transformation function to each node. 474 475 Args: 476 fun (function): a function which takes a node as an argument and returns a 477 new transformed node or the same node without modifications. If the function 478 returns None, then the corresponding node will be removed from the syntax tree. 479 copy (bool): if set to True a new tree instance is constructed, otherwise the tree is 480 modified in place. 481 482 Returns: 483 The transformed tree. 484 """ 485 node = self.copy() if copy else self 486 new_node = fun(node, *args, **kwargs) 487 488 if new_node is None or not isinstance(new_node, Expression): 489 return new_node 490 if new_node is not node: 491 new_node.parent = node.parent 492 return new_node 493 494 replace_children(new_node, lambda child: child.transform(fun, *args, copy=False, **kwargs)) 495 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.
497 def replace(self, expression): 498 """ 499 Swap out this expression with a new expression. 500 501 For example:: 502 503 >>> tree = Select().select("x").from_("tbl") 504 >>> tree.find(Column).replace(Column(this="y")) 505 (COLUMN this: y) 506 >>> tree.sql() 507 'SELECT y FROM tbl' 508 509 Args: 510 expression (Expression|None): new node 511 512 Returns: 513 The new expression or expressions. 514 """ 515 if not self.parent: 516 return expression 517 518 parent = self.parent 519 self.parent = None 520 521 replace_children(parent, lambda child: expression if child is self else child) 522 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.
530 def assert_is(self, type_): 531 """ 532 Assert that this `Expression` is an instance of `type_`. 533 534 If it is NOT an instance of `type_`, this raises an assertion error. 535 Otherwise, this returns this expression. 536 537 Examples: 538 This is useful for type security in chained expressions: 539 540 >>> import sqlglot 541 >>> sqlglot.parse_one("SELECT x from y").assert_is(Select).select("z").sql() 542 'SELECT x, z FROM y' 543 """ 544 assert isinstance(self, type_) 545 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'
547 def error_messages(self, args: t.Optional[t.Sequence] = None) -> t.List[str]: 548 """ 549 Checks if this expression is valid (e.g. all mandatory args are set). 550 551 Args: 552 args: a sequence of values that were used to instantiate a Func expression. This is used 553 to check that the provided arguments don't exceed the function argument limit. 554 555 Returns: 556 A list of error messages for all possible errors that were found. 557 """ 558 errors: t.List[str] = [] 559 560 for k in self.args: 561 if k not in self.arg_types: 562 errors.append(f"Unexpected keyword: '{k}' for {self.__class__}") 563 for k, mandatory in self.arg_types.items(): 564 v = self.args.get(k) 565 if mandatory and (v is None or (isinstance(v, list) and not v)): 566 errors.append(f"Required keyword: '{k}' missing for {self.__class__}") 567 568 if ( 569 args 570 and isinstance(self, Func) 571 and len(args) > len(self.arg_types) 572 and not self.is_var_len_args 573 ): 574 errors.append( 575 f"The number of provided arguments ({len(args)}) is greater than " 576 f"the maximum number of supported arguments ({len(self.arg_types)})" 577 ) 578 579 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.
581 def dump(self): 582 """ 583 Dump this Expression to a JSON-serializable dict. 584 """ 585 from sqlglot.serde import dump 586 587 return dump(self)
Dump this Expression to a JSON-serializable dict.
589 @classmethod 590 def load(cls, obj): 591 """ 592 Load a dict (as returned by `Expression.dump`) into an Expression instance. 593 """ 594 from sqlglot.serde import load 595 596 return load(obj)
Load a dict (as returned by Expression.dump
) into an Expression instance.
606class Condition(Expression): 607 def and_(self, *expressions, dialect=None, **opts): 608 """ 609 AND this condition with one or multiple expressions. 610 611 Example: 612 >>> condition("x=1").and_("y=1").sql() 613 'x = 1 AND y = 1' 614 615 Args: 616 *expressions (str | Expression): the SQL code strings to parse. 617 If an `Expression` instance is passed, it will be used as-is. 618 dialect (str): the dialect used to parse the input expression. 619 opts (kwargs): other options to use to parse the input expressions. 620 621 Returns: 622 And: the new condition. 623 """ 624 return and_(self, *expressions, dialect=dialect, **opts) 625 626 def or_(self, *expressions, dialect=None, **opts): 627 """ 628 OR this condition with one or multiple expressions. 629 630 Example: 631 >>> condition("x=1").or_("y=1").sql() 632 'x = 1 OR y = 1' 633 634 Args: 635 *expressions (str | Expression): the SQL code strings to parse. 636 If an `Expression` instance is passed, it will be used as-is. 637 dialect (str): the dialect used to parse the input expression. 638 opts (kwargs): other options to use to parse the input expressions. 639 640 Returns: 641 Or: the new condition. 642 """ 643 return or_(self, *expressions, dialect=dialect, **opts) 644 645 def not_(self): 646 """ 647 Wrap this condition with NOT. 648 649 Example: 650 >>> condition("x=1").not_().sql() 651 'NOT x = 1' 652 653 Returns: 654 Not: the new condition. 655 """ 656 return not_(self)
607 def and_(self, *expressions, dialect=None, **opts): 608 """ 609 AND this condition with one or multiple expressions. 610 611 Example: 612 >>> condition("x=1").and_("y=1").sql() 613 'x = 1 AND y = 1' 614 615 Args: 616 *expressions (str | Expression): the SQL code strings to parse. 617 If an `Expression` instance is passed, it will be used as-is. 618 dialect (str): the dialect used to parse the input expression. 619 opts (kwargs): other options to use to parse the input expressions. 620 621 Returns: 622 And: the new condition. 623 """ 624 return and_(self, *expressions, dialect=dialect, **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
Expression
instance is passed, it will be used as-is. - dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
And: the new condition.
626 def or_(self, *expressions, dialect=None, **opts): 627 """ 628 OR this condition with one or multiple expressions. 629 630 Example: 631 >>> condition("x=1").or_("y=1").sql() 632 'x = 1 OR y = 1' 633 634 Args: 635 *expressions (str | Expression): the SQL code strings to parse. 636 If an `Expression` instance is passed, it will be used as-is. 637 dialect (str): the dialect used to parse the input expression. 638 opts (kwargs): other options to use to parse the input expressions. 639 640 Returns: 641 Or: the new condition. 642 """ 643 return or_(self, *expressions, dialect=dialect, **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
Expression
instance is passed, it will be used as-is. - dialect (str): the dialect used to parse the input expression.
- opts (kwargs): other options to use to parse the input expressions.
Returns:
Or: the new condition.
645 def not_(self): 646 """ 647 Wrap this condition with NOT. 648 649 Example: 650 >>> condition("x=1").not_().sql() 651 'NOT x = 1' 652 653 Returns: 654 Not: the new condition. 655 """ 656 return not_(self)
Wrap this condition with NOT.
Example:
>>> condition("x=1").not_().sql() 'NOT x = 1'
Returns:
Not: the new condition.
Inherited Members
Relationships like x = y, x > 1, x >= y.
Inherited Members
663class DerivedTable(Expression): 664 @property 665 def alias_column_names(self): 666 table_alias = self.args.get("alias") 667 if not table_alias: 668 return [] 669 column_list = table_alias.assert_is(TableAlias).args.get("columns") or [] 670 return [c.name for c in column_list] 671 672 @property 673 def selects(self): 674 alias = self.args.get("alias") 675 676 if alias: 677 return alias.columns 678 return [] 679 680 @property 681 def named_selects(self): 682 return [select.output_name for select in self.selects]
Inherited Members
685class Unionable(Expression): 686 def union(self, expression, distinct=True, dialect=None, **opts): 687 """ 688 Builds a UNION expression. 689 690 Example: 691 >>> import sqlglot 692 >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql() 693 'SELECT * FROM foo UNION SELECT * FROM bla' 694 695 Args: 696 expression (str | Expression): the SQL code string. 697 If an `Expression` instance is passed, it will be used as-is. 698 distinct (bool): set the DISTINCT flag if and only if this is true. 699 dialect (str): the dialect used to parse the input expression. 700 opts (kwargs): other options to use to parse the input expressions. 701 Returns: 702 Union: the Union expression. 703 """ 704 return union(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 705 706 def intersect(self, expression, distinct=True, dialect=None, **opts): 707 """ 708 Builds an INTERSECT expression. 709 710 Example: 711 >>> import sqlglot 712 >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql() 713 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 714 715 Args: 716 expression (str | Expression): the SQL code string. 717 If an `Expression` instance is passed, it will be used as-is. 718 distinct (bool): set the DISTINCT flag if and only if this is true. 719 dialect (str): the dialect used to parse the input expression. 720 opts (kwargs): other options to use to parse the input expressions. 721 Returns: 722 Intersect: the Intersect expression 723 """ 724 return intersect(left=self, right=expression, distinct=distinct, dialect=dialect, **opts) 725 726 def except_(self, expression, distinct=True, dialect=None, **opts): 727 """ 728 Builds an EXCEPT expression. 729 730 Example: 731 >>> import sqlglot 732 >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql() 733 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 734 735 Args: 736 expression (str | Expression): the SQL code string. 737 If an `Expression` instance is passed, it will be used as-is. 738 distinct (bool): set the DISTINCT flag if and only if this is true. 739 dialect (str): the dialect used to parse the input expression. 740 opts (kwargs): other options to use to parse the input expressions. 741 Returns: 742 Except: the Except expression 743 """ 744 return except_(left=self, right=expression, distinct=distinct, dialect=dialect, **opts)
686 def union(self, expression, distinct=True, dialect=None, **opts): 687 """ 688 Builds a UNION expression. 689 690 Example: 691 >>> import sqlglot 692 >>> sqlglot.parse_one("SELECT * FROM foo").union("SELECT * FROM bla").sql() 693 'SELECT * FROM foo UNION SELECT * FROM bla' 694 695 Args: 696 expression (str | Expression): the SQL code string. 697 If an `Expression` instance is passed, it will be used as-is. 698 distinct (bool): set the DISTINCT flag if and only if this is true. 699 dialect (str): the dialect used to parse the input expression. 700 opts (kwargs): other options to use to parse the input expressions. 701 Returns: 702 Union: the Union expression. 703 """ 704 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
Expression
instance 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.
706 def intersect(self, expression, distinct=True, dialect=None, **opts): 707 """ 708 Builds an INTERSECT expression. 709 710 Example: 711 >>> import sqlglot 712 >>> sqlglot.parse_one("SELECT * FROM foo").intersect("SELECT * FROM bla").sql() 713 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 714 715 Args: 716 expression (str | Expression): the SQL code string. 717 If an `Expression` instance is passed, it will be used as-is. 718 distinct (bool): set the DISTINCT flag if and only if this is true. 719 dialect (str): the dialect used to parse the input expression. 720 opts (kwargs): other options to use to parse the input expressions. 721 Returns: 722 Intersect: the Intersect expression 723 """ 724 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
Expression
instance 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
726 def except_(self, expression, distinct=True, dialect=None, **opts): 727 """ 728 Builds an EXCEPT expression. 729 730 Example: 731 >>> import sqlglot 732 >>> sqlglot.parse_one("SELECT * FROM foo").except_("SELECT * FROM bla").sql() 733 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 734 735 Args: 736 expression (str | Expression): the SQL code string. 737 If an `Expression` instance is passed, it will be used as-is. 738 distinct (bool): set the DISTINCT flag if and only if this is true. 739 dialect (str): the dialect used to parse the input expression. 740 opts (kwargs): other options to use to parse the input expressions. 741 Returns: 742 Except: the Except expression 743 """ 744 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
Expression
instance 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
Inherited Members
751class Cache(Expression): 752 arg_types = { 753 "with": False, 754 "this": True, 755 "lazy": False, 756 "options": False, 757 "expression": False, 758 }
Inherited Members
Inherited Members
765class Create(Expression): 766 arg_types = { 767 "with": False, 768 "this": True, 769 "kind": True, 770 "expression": False, 771 "set": False, 772 "multiset": False, 773 "global_temporary": False, 774 "volatile": False, 775 "exists": False, 776 "properties": False, 777 "temporary": False, 778 "transient": False, 779 "external": False, 780 "replace": False, 781 "unique": False, 782 "materialized": False, 783 "data": False, 784 "statistics": False, 785 "no_primary_index": False, 786 "indexes": False, 787 "no_schema_binding": False, 788 "begin": False, 789 }
Inherited Members
Inherited Members
Inherited Members
800class SetItem(Expression): 801 arg_types = { 802 "this": False, 803 "expressions": False, 804 "kind": False, 805 "collate": False, # MySQL SET NAMES statement 806 "global": False, 807 }
Inherited Members
810class Show(Expression): 811 arg_types = { 812 "this": True, 813 "target": False, 814 "offset": False, 815 "limit": False, 816 "like": False, 817 "where": False, 818 "db": False, 819 "full": False, 820 "mutex": False, 821 "query": False, 822 "channel": False, 823 "global": False, 824 "log": False, 825 "position": False, 826 "types": False, 827 }
Inherited Members
830class UserDefinedFunction(Expression): 831 arg_types = {"this": True, "expressions": False, "wrapped": False}
Inherited Members
Inherited Members
838class With(Expression): 839 arg_types = {"expressions": True, "recursive": False} 840 841 @property 842 def recursive(self) -> bool: 843 return bool(self.args.get("recursive"))
Inherited Members
Inherited Members
Inherited Members
854class TableAlias(Expression): 855 arg_types = {"this": False, "columns": False} 856 857 @property 858 def columns(self): 859 return self.args.get("columns") or []
Inherited Members
Inherited Members
Inherited Members
Inherited Members
874class Column(Condition): 875 arg_types = {"this": True, "table": False, "db": False, "catalog": False} 876 877 @property 878 def table(self) -> str: 879 return self.text("table") 880 881 @property 882 def db(self) -> str: 883 return self.text("db") 884 885 @property 886 def catalog(self) -> str: 887 return self.text("catalog") 888 889 @property 890 def output_name(self) -> str: 891 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
894class ColumnDef(Expression): 895 arg_types = { 896 "this": True, 897 "kind": False, 898 "constraints": False, 899 "exists": False, 900 }
Inherited Members
903class AlterColumn(Expression): 904 arg_types = { 905 "this": True, 906 "dtype": False, 907 "collate": False, 908 "using": False, 909 "default": False, 910 "drop": False, 911 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
962class GeneratedAsIdentityColumnConstraint(ColumnConstraintKind): 963 # this: True -> ALWAYS, this: False -> BY DEFAULT 964 arg_types = { 965 "this": False, 966 "start": False, 967 "increment": False, 968 "minvalue": False, 969 "maxvalue": False, 970 "cycle": False, 971 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
1002class Delete(Expression): 1003 arg_types = {"with": False, "this": False, "using": False, "where": False}
Inherited Members
1006class Drop(Expression): 1007 arg_types = { 1008 "this": False, 1009 "kind": False, 1010 "exists": False, 1011 "temporary": False, 1012 "materialized": False, 1013 "cascade": False, 1014 }
Inherited Members
Inherited Members
Inherited Members
1025class Directory(Expression): 1026 # https://spark.apache.org/docs/3.0.0-preview/sql-ref-syntax-dml-insert-overwrite-directory-hive.html 1027 arg_types = {"this": True, "local": False, "row_format": False}
Inherited Members
1030class ForeignKey(Expression): 1031 arg_types = { 1032 "expressions": True, 1033 "reference": False, 1034 "delete": False, 1035 "update": False, 1036 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
1069class Identifier(Expression): 1070 arg_types = {"this": True, "quoted": False} 1071 1072 @property 1073 def quoted(self): 1074 return bool(self.args.get("quoted")) 1075 1076 def __eq__(self, other): 1077 return isinstance(other, self.__class__) and _norm_arg(self.this) == _norm_arg(other.this) 1078 1079 def __hash__(self): 1080 return hash((self.key, self.this.lower())) 1081 1082 @property 1083 def output_name(self): 1084 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 ''
1087class Index(Expression): 1088 arg_types = { 1089 "this": False, 1090 "table": False, 1091 "where": False, 1092 "columns": False, 1093 "unique": False, 1094 "primary": False, 1095 "amp": False, # teradata 1096 }
Inherited Members
1099class Insert(Expression): 1100 arg_types = { 1101 "with": False, 1102 "this": True, 1103 "expression": False, 1104 "overwrite": False, 1105 "exists": False, 1106 "partition": False, 1107 "alternative": False, 1108 }
Inherited Members
Inherited Members
Inherited Members
1121class LoadData(Expression): 1122 arg_types = { 1123 "this": True, 1124 "local": False, 1125 "overwrite": False, 1126 "inpath": True, 1127 "partition": False, 1128 "input_format": False, 1129 "serde": False, 1130 }
Inherited Members
Inherited Members
Inherited Members
1141class Group(Expression): 1142 arg_types = { 1143 "expressions": False, 1144 "grouping_sets": False, 1145 "cube": False, 1146 "rollup": False, 1147 }
Inherited Members
Inherited Members
Inherited Members
1158class Literal(Condition): 1159 arg_types = {"this": True, "is_string": True} 1160 1161 def __eq__(self, other): 1162 return ( 1163 isinstance(other, Literal) 1164 and self.this == other.this 1165 and self.args["is_string"] == other.args["is_string"] 1166 ) 1167 1168 def __hash__(self): 1169 return hash((self.key, self.this, self.args["is_string"])) 1170 1171 @classmethod 1172 def number(cls, number) -> Literal: 1173 return cls(this=str(number), is_string=False) 1174 1175 @classmethod 1176 def string(cls, string) -> Literal: 1177 return cls(this=str(string), is_string=True) 1178 1179 @property 1180 def output_name(self): 1181 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
1184class Join(Expression): 1185 arg_types = { 1186 "this": True, 1187 "on": False, 1188 "side": False, 1189 "kind": False, 1190 "using": False, 1191 "natural": False, 1192 } 1193 1194 @property 1195 def kind(self): 1196 return self.text("kind").upper() 1197 1198 @property 1199 def side(self): 1200 return self.text("side").upper() 1201 1202 @property 1203 def alias_or_name(self): 1204 return self.this.alias_or_name 1205 1206 def on(self, *expressions, append=True, dialect=None, copy=True, **opts): 1207 """ 1208 Append to or set the ON expressions. 1209 1210 Example: 1211 >>> import sqlglot 1212 >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql() 1213 'JOIN x ON y = 1' 1214 1215 Args: 1216 *expressions (str | Expression): the SQL code strings to parse. 1217 If an `Expression` instance is passed, it will be used as-is. 1218 Multiple expressions are combined with an AND operator. 1219 append (bool): if `True`, AND the new expressions to any existing expression. 1220 Otherwise, this resets the expression. 1221 dialect (str): the dialect used to parse the input expressions. 1222 copy (bool): if `False`, modify this expression instance in-place. 1223 opts (kwargs): other options to use to parse the input expressions. 1224 1225 Returns: 1226 Join: the modified join expression. 1227 """ 1228 join = _apply_conjunction_builder( 1229 *expressions, 1230 instance=self, 1231 arg="on", 1232 append=append, 1233 dialect=dialect, 1234 copy=copy, 1235 **opts, 1236 ) 1237 1238 if join.kind == "CROSS": 1239 join.set("kind", None) 1240 1241 return join 1242 1243 def using(self, *expressions, append=True, dialect=None, copy=True, **opts): 1244 """ 1245 Append to or set the USING expressions. 1246 1247 Example: 1248 >>> import sqlglot 1249 >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 1250 'JOIN x USING (foo, bla)' 1251 1252 Args: 1253 *expressions (str | Expression): the SQL code strings to parse. 1254 If an `Expression` instance is passed, it will be used as-is. 1255 append (bool): if `True`, concatenate the new expressions to the existing "using" list. 1256 Otherwise, this resets the expression. 1257 dialect (str): the dialect used to parse the input expressions. 1258 copy (bool): if `False`, modify this expression instance in-place. 1259 opts (kwargs): other options to use to parse the input expressions. 1260 1261 Returns: 1262 Join: the modified join expression. 1263 """ 1264 join = _apply_list_builder( 1265 *expressions, 1266 instance=self, 1267 arg="using", 1268 append=append, 1269 dialect=dialect, 1270 copy=copy, 1271 **opts, 1272 ) 1273 1274 if join.kind == "CROSS": 1275 join.set("kind", None) 1276 1277 return join
1206 def on(self, *expressions, append=True, dialect=None, copy=True, **opts): 1207 """ 1208 Append to or set the ON expressions. 1209 1210 Example: 1211 >>> import sqlglot 1212 >>> sqlglot.parse_one("JOIN x", into=Join).on("y = 1").sql() 1213 'JOIN x ON y = 1' 1214 1215 Args: 1216 *expressions (str | Expression): the SQL code strings to parse. 1217 If an `Expression` instance is passed, it will be used as-is. 1218 Multiple expressions are combined with an AND operator. 1219 append (bool): if `True`, AND the new expressions to any existing expression. 1220 Otherwise, this resets the expression. 1221 dialect (str): the dialect used to parse the input expressions. 1222 copy (bool): if `False`, modify this expression instance in-place. 1223 opts (kwargs): other options to use to parse the input expressions. 1224 1225 Returns: 1226 Join: the modified join expression. 1227 """ 1228 join = _apply_conjunction_builder( 1229 *expressions, 1230 instance=self, 1231 arg="on", 1232 append=append, 1233 dialect=dialect, 1234 copy=copy, 1235 **opts, 1236 ) 1237 1238 if join.kind == "CROSS": 1239 join.set("kind", None) 1240 1241 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
Expression
instance 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.
1243 def using(self, *expressions, append=True, dialect=None, copy=True, **opts): 1244 """ 1245 Append to or set the USING expressions. 1246 1247 Example: 1248 >>> import sqlglot 1249 >>> sqlglot.parse_one("JOIN x", into=Join).using("foo", "bla").sql() 1250 'JOIN x USING (foo, bla)' 1251 1252 Args: 1253 *expressions (str | Expression): the SQL code strings to parse. 1254 If an `Expression` instance is passed, it will be used as-is. 1255 append (bool): if `True`, concatenate the new expressions to the existing "using" list. 1256 Otherwise, this resets the expression. 1257 dialect (str): the dialect used to parse the input expressions. 1258 copy (bool): if `False`, modify this expression instance in-place. 1259 opts (kwargs): other options to use to parse the input expressions. 1260 1261 Returns: 1262 Join: the modified join expression. 1263 """ 1264 join = _apply_list_builder( 1265 *expressions, 1266 instance=self, 1267 arg="using", 1268 append=append, 1269 dialect=dialect, 1270 copy=copy, 1271 **opts, 1272 ) 1273 1274 if join.kind == "CROSS": 1275 join.set("kind", None) 1276 1277 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
Expression
instance 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
1280class Lateral(UDTF): 1281 arg_types = {"this": True, "view": False, "outer": False, "alias": False}
Inherited Members
1284class MatchRecognize(Expression): 1285 arg_types = { 1286 "partition_by": False, 1287 "order": False, 1288 "measures": False, 1289 "rows": False, 1290 "after": False, 1291 "pattern": False, 1292 "define": False, 1293 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
1396class ReturnsProperty(Property): 1397 arg_types = {"this": True, "is_table": False, "table": False}
Inherited Members
Inherited Members
Inherited Members
Inherited Members
1412class RowFormatDelimitedProperty(Property): 1413 # https://cwiki.apache.org/confluence/display/hive/languagemanual+dml 1414 arg_types = { 1415 "fields": False, 1416 "escaped": False, 1417 "collection_items": False, 1418 "map_keys": False, 1419 "lines": False, 1420 "null": False, 1421 "serde": False, 1422 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
1449class AfterJournalProperty(Property): 1450 arg_types = {"no": True, "dual": False, "local": False}
Inherited Members
Inherited Members
Inherited Members
1461class MergeBlockRatioProperty(Property): 1462 arg_types = {"this": False, "no": False, "default": False, "percent": False}
Inherited Members
1465class DataBlocksizeProperty(Property): 1466 arg_types = {"size": False, "units": False, "min": False, "default": False}
Inherited Members
1469class BlockCompressionProperty(Property): 1470 arg_types = {"autotemp": False, "always": False, "default": True, "manual": True, "never": True}
Inherited Members
1473class IsolatedLoadingProperty(Property): 1474 arg_types = { 1475 "no": True, 1476 "concurrent": True, 1477 "for_all": True, 1478 "for_insert": True, 1479 "for_none": True, 1480 }
Inherited Members
1483class LockingProperty(Property): 1484 arg_types = { 1485 "this": False, 1486 "kind": True, 1487 "for_or_in": True, 1488 "lock_type": True, 1489 "override": False, 1490 }
Inherited Members
1493class Properties(Expression): 1494 arg_types = {"expressions": True} 1495 1496 NAME_TO_PROPERTY = { 1497 "ALGORITHM": AlgorithmProperty, 1498 "AUTO_INCREMENT": AutoIncrementProperty, 1499 "CHARACTER SET": CharacterSetProperty, 1500 "COLLATE": CollateProperty, 1501 "COMMENT": SchemaCommentProperty, 1502 "DEFINER": DefinerProperty, 1503 "DISTKEY": DistKeyProperty, 1504 "DISTSTYLE": DistStyleProperty, 1505 "ENGINE": EngineProperty, 1506 "EXECUTE AS": ExecuteAsProperty, 1507 "FORMAT": FileFormatProperty, 1508 "LANGUAGE": LanguageProperty, 1509 "LOCATION": LocationProperty, 1510 "PARTITIONED_BY": PartitionedByProperty, 1511 "RETURNS": ReturnsProperty, 1512 "SORTKEY": SortKeyProperty, 1513 "TABLE_FORMAT": TableFormatProperty, 1514 } 1515 1516 PROPERTY_TO_NAME = {v: k for k, v in NAME_TO_PROPERTY.items()} 1517 1518 # CREATE property locations 1519 # Form: schema specified 1520 # create [POST_CREATE] 1521 # table a [POST_NAME] 1522 # (b int) [POST_SCHEMA] 1523 # with ([POST_WITH]) 1524 # index (b) [POST_INDEX] 1525 # 1526 # Form: alias selection 1527 # create [POST_CREATE] 1528 # table a [POST_NAME] 1529 # as [POST_ALIAS] (select * from b) 1530 # index (c) [POST_INDEX] 1531 class Location(AutoName): 1532 POST_CREATE = auto() 1533 POST_NAME = auto() 1534 POST_SCHEMA = auto() 1535 POST_WITH = auto() 1536 POST_ALIAS = auto() 1537 POST_INDEX = auto() 1538 UNSUPPORTED = auto() 1539 1540 @classmethod 1541 def from_dict(cls, properties_dict) -> Properties: 1542 expressions = [] 1543 for key, value in properties_dict.items(): 1544 property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) 1545 if property_cls: 1546 expressions.append(property_cls(this=convert(value))) 1547 else: 1548 expressions.append(Property(this=Literal.string(key), value=convert(value))) 1549 1550 return cls(expressions=expressions)
1540 @classmethod 1541 def from_dict(cls, properties_dict) -> Properties: 1542 expressions = [] 1543 for key, value in properties_dict.items(): 1544 property_cls = cls.NAME_TO_PROPERTY.get(key.upper()) 1545 if property_cls: 1546 expressions.append(property_cls(this=convert(value))) 1547 else: 1548 expressions.append(Property(this=Literal.string(key), value=convert(value))) 1549 1550 return cls(expressions=expressions)
Inherited Members
1531 class Location(AutoName): 1532 POST_CREATE = auto() 1533 POST_NAME = auto() 1534 POST_SCHEMA = auto() 1535 POST_WITH = auto() 1536 POST_ALIAS = auto() 1537 POST_INDEX = auto() 1538 UNSUPPORTED = auto()
An enumeration.
Inherited Members
- enum.Enum
- name
- value
Inherited Members
Inherited Members
1562class Reference(Expression): 1563 arg_types = {"this": True, "expressions": False, "options": False}
Inherited Members
Inherited Members
1570class Subqueryable(Unionable): 1571 def subquery(self, alias=None, copy=True) -> Subquery: 1572 """ 1573 Convert this expression to an aliased expression that can be used as a Subquery. 1574 1575 Example: 1576 >>> subquery = Select().select("x").from_("tbl").subquery() 1577 >>> Select().select("x").from_(subquery).sql() 1578 'SELECT x FROM (SELECT x FROM tbl)' 1579 1580 Args: 1581 alias (str | Identifier): an optional alias for the subquery 1582 copy (bool): if `False`, modify this expression instance in-place. 1583 1584 Returns: 1585 Alias: the subquery 1586 """ 1587 instance = _maybe_copy(self, copy) 1588 return Subquery( 1589 this=instance, 1590 alias=TableAlias(this=to_identifier(alias)), 1591 ) 1592 1593 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 1594 raise NotImplementedError 1595 1596 @property 1597 def ctes(self): 1598 with_ = self.args.get("with") 1599 if not with_: 1600 return [] 1601 return with_.expressions 1602 1603 @property 1604 def selects(self): 1605 raise NotImplementedError("Subqueryable objects must implement `selects`") 1606 1607 @property 1608 def named_selects(self): 1609 raise NotImplementedError("Subqueryable objects must implement `named_selects`") 1610 1611 def with_( 1612 self, 1613 alias, 1614 as_, 1615 recursive=None, 1616 append=True, 1617 dialect=None, 1618 copy=True, 1619 **opts, 1620 ): 1621 """ 1622 Append to or set the common table expressions. 1623 1624 Example: 1625 >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql() 1626 'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2' 1627 1628 Args: 1629 alias (str | Expression): the SQL code string to parse as the table name. 1630 If an `Expression` instance is passed, this is used as-is. 1631 as_ (str | Expression): the SQL code string to parse as the table expression. 1632 If an `Expression` instance is passed, it will be used as-is. 1633 recursive (bool): set the RECURSIVE part of the expression. Defaults to `False`. 1634 append (bool): if `True`, add to any existing expressions. 1635 Otherwise, this resets the expressions. 1636 dialect (str): the dialect used to parse the input expression. 1637 copy (bool): if `False`, modify this expression instance in-place. 1638 opts (kwargs): other options to use to parse the input expressions. 1639 1640 Returns: 1641 Select: the modified expression. 1642 """ 1643 alias_expression = maybe_parse( 1644 alias, 1645 dialect=dialect, 1646 into=TableAlias, 1647 **opts, 1648 ) 1649 as_expression = maybe_parse( 1650 as_, 1651 dialect=dialect, 1652 **opts, 1653 ) 1654 cte = CTE( 1655 this=as_expression, 1656 alias=alias_expression, 1657 ) 1658 return _apply_child_list_builder( 1659 cte, 1660 instance=self, 1661 arg="with", 1662 append=append, 1663 copy=copy, 1664 into=With, 1665 properties={"recursive": recursive or False}, 1666 )
1571 def subquery(self, alias=None, copy=True) -> Subquery: 1572 """ 1573 Convert this expression to an aliased expression that can be used as a Subquery. 1574 1575 Example: 1576 >>> subquery = Select().select("x").from_("tbl").subquery() 1577 >>> Select().select("x").from_(subquery).sql() 1578 'SELECT x FROM (SELECT x FROM tbl)' 1579 1580 Args: 1581 alias (str | Identifier): an optional alias for the subquery 1582 copy (bool): if `False`, modify this expression instance in-place. 1583 1584 Returns: 1585 Alias: the subquery 1586 """ 1587 instance = _maybe_copy(self, copy) 1588 return Subquery( 1589 this=instance, 1590 alias=TableAlias(this=to_identifier(alias)), 1591 )
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
1611 def with_( 1612 self, 1613 alias, 1614 as_, 1615 recursive=None, 1616 append=True, 1617 dialect=None, 1618 copy=True, 1619 **opts, 1620 ): 1621 """ 1622 Append to or set the common table expressions. 1623 1624 Example: 1625 >>> Select().with_("tbl2", as_="SELECT * FROM tbl").select("x").from_("tbl2").sql() 1626 'WITH tbl2 AS (SELECT * FROM tbl) SELECT x FROM tbl2' 1627 1628 Args: 1629 alias (str | Expression): the SQL code string to parse as the table name. 1630 If an `Expression` instance is passed, this is used as-is. 1631 as_ (str | Expression): the SQL code string to parse as the table expression. 1632 If an `Expression` instance is passed, it will be used as-is. 1633 recursive (bool): set the RECURSIVE part of the expression. Defaults to `False`. 1634 append (bool): if `True`, add to any existing expressions. 1635 Otherwise, this resets the expressions. 1636 dialect (str): the dialect used to parse the input expression. 1637 copy (bool): if `False`, modify this expression instance in-place. 1638 opts (kwargs): other options to use to parse the input expressions. 1639 1640 Returns: 1641 Select: the modified expression. 1642 """ 1643 alias_expression = maybe_parse( 1644 alias, 1645 dialect=dialect, 1646 into=TableAlias, 1647 **opts, 1648 ) 1649 as_expression = maybe_parse( 1650 as_, 1651 dialect=dialect, 1652 **opts, 1653 ) 1654 cte = CTE( 1655 this=as_expression, 1656 alias=alias_expression, 1657 ) 1658 return _apply_child_list_builder( 1659 cte, 1660 instance=self, 1661 arg="with", 1662 append=append, 1663 copy=copy, 1664 into=With, 1665 properties={"recursive": recursive or False}, 1666 )
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 (str | Expression): the SQL code string to parse as the table name.
If an
Expression
instance is passed, this is used as-is. - as_ (str | Expression): the SQL code string to parse as the table expression.
If an
Expression
instance is passed, it will be used as-is. - recursive (bool): set the RECURSIVE part of the expression. Defaults to
False
. - append (bool): if
True
, add to any existing expressions. Otherwise, this resets the expressions. - 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.
Inherited Members
1689class Table(Expression): 1690 arg_types = { 1691 "this": True, 1692 "alias": False, 1693 "db": False, 1694 "catalog": False, 1695 "laterals": False, 1696 "joins": False, 1697 "pivots": False, 1698 "hints": False, 1699 "system_time": False, 1700 } 1701 1702 @property 1703 def db(self) -> str: 1704 return self.text("db") 1705 1706 @property 1707 def catalog(self) -> str: 1708 return self.text("catalog")
Inherited Members
1712class SystemTime(Expression): 1713 arg_types = { 1714 "this": False, 1715 "expression": False, 1716 "kind": True, 1717 }
Inherited Members
1720class Union(Subqueryable): 1721 arg_types = { 1722 "with": False, 1723 "this": True, 1724 "expression": True, 1725 "distinct": False, 1726 **QUERY_MODIFIERS, 1727 } 1728 1729 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 1730 """ 1731 Set the LIMIT expression. 1732 1733 Example: 1734 >>> select("1").union(select("1")).limit(1).sql() 1735 'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1' 1736 1737 Args: 1738 expression (str | int | Expression): the SQL code string to parse. 1739 This can also be an integer. 1740 If a `Limit` instance is passed, this is used as-is. 1741 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 1742 dialect (str): the dialect used to parse the input expression. 1743 copy (bool): if `False`, modify this expression instance in-place. 1744 opts (kwargs): other options to use to parse the input expressions. 1745 1746 Returns: 1747 Select: The limited subqueryable. 1748 """ 1749 return ( 1750 select("*") 1751 .from_(self.subquery(alias="_l_0", copy=copy)) 1752 .limit(expression, dialect=dialect, copy=False, **opts) 1753 ) 1754 1755 def select( 1756 self, 1757 *expressions: str | Expression, 1758 append: bool = True, 1759 dialect: DialectType = None, 1760 copy: bool = True, 1761 **opts, 1762 ) -> Union: 1763 """Append to or set the SELECT of the union recursively. 1764 1765 Example: 1766 >>> from sqlglot import parse_one 1767 >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 1768 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z' 1769 1770 Args: 1771 *expressions: the SQL code strings to parse. 1772 If an `Expression` instance is passed, it will be used as-is. 1773 append: if `True`, add to any existing expressions. 1774 Otherwise, this resets the expressions. 1775 dialect: the dialect used to parse the input expressions. 1776 copy: if `False`, modify this expression instance in-place. 1777 opts: other options to use to parse the input expressions. 1778 1779 Returns: 1780 Union: the modified expression. 1781 """ 1782 this = self.copy() if copy else self 1783 this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts) 1784 this.expression.unnest().select( 1785 *expressions, append=append, dialect=dialect, copy=False, **opts 1786 ) 1787 return this 1788 1789 @property 1790 def named_selects(self): 1791 return self.this.unnest().named_selects 1792 1793 @property 1794 def selects(self): 1795 return self.this.unnest().selects 1796 1797 @property 1798 def left(self): 1799 return self.this 1800 1801 @property 1802 def right(self): 1803 return self.expression
1729 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 1730 """ 1731 Set the LIMIT expression. 1732 1733 Example: 1734 >>> select("1").union(select("1")).limit(1).sql() 1735 'SELECT * FROM (SELECT 1 UNION SELECT 1) AS _l_0 LIMIT 1' 1736 1737 Args: 1738 expression (str | int | Expression): the SQL code string to parse. 1739 This can also be an integer. 1740 If a `Limit` instance is passed, this is used as-is. 1741 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 1742 dialect (str): the dialect used to parse the input expression. 1743 copy (bool): if `False`, modify this expression instance in-place. 1744 opts (kwargs): other options to use to parse the input expressions. 1745 1746 Returns: 1747 Select: The limited subqueryable. 1748 """ 1749 return ( 1750 select("*") 1751 .from_(self.subquery(alias="_l_0", copy=copy)) 1752 .limit(expression, dialect=dialect, copy=False, **opts) 1753 )
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
Limit
instance is passed, this is used as-is. If anotherExpression
instance 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.
1755 def select( 1756 self, 1757 *expressions: str | Expression, 1758 append: bool = True, 1759 dialect: DialectType = None, 1760 copy: bool = True, 1761 **opts, 1762 ) -> Union: 1763 """Append to or set the SELECT of the union recursively. 1764 1765 Example: 1766 >>> from sqlglot import parse_one 1767 >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 1768 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z' 1769 1770 Args: 1771 *expressions: the SQL code strings to parse. 1772 If an `Expression` instance is passed, it will be used as-is. 1773 append: if `True`, add to any existing expressions. 1774 Otherwise, this resets the expressions. 1775 dialect: the dialect used to parse the input expressions. 1776 copy: if `False`, modify this expression instance in-place. 1777 opts: other options to use to parse the input expressions. 1778 1779 Returns: 1780 Union: the modified expression. 1781 """ 1782 this = self.copy() if copy else self 1783 this.this.unnest().select(*expressions, append=append, dialect=dialect, copy=False, **opts) 1784 this.expression.unnest().select( 1785 *expressions, append=append, dialect=dialect, copy=False, **opts 1786 ) 1787 return this
Append to or set the SELECT of the union recursively.
Example:
>>> from sqlglot import parse_one >>> parse_one("select a from x union select a from y union select a from z").select("b").sql() 'SELECT a, b FROM x UNION SELECT a, b FROM y UNION SELECT a, b FROM z'
Arguments:
- *expressions: the SQL code strings to parse.
If an
Expression
instance is passed, it will be used as-is. - append: if
True
, add to any existing expressions. Otherwise, this resets the expressions. - dialect: the dialect used to parse the input expressions.
- copy: if
False
, modify this expression instance in-place. - opts: other options to use to parse the input expressions.
Returns:
Union: the modified expression.
Inherited Members
Inherited Members
Inherited Members
1814class Unnest(UDTF): 1815 arg_types = { 1816 "expressions": True, 1817 "ordinality": False, 1818 "alias": False, 1819 "offset": False, 1820 }
Inherited Members
1823class Update(Expression): 1824 arg_types = { 1825 "with": False, 1826 "this": False, 1827 "expressions": True, 1828 "from": False, 1829 "where": False, 1830 }
Inherited Members
1833class Values(UDTF): 1834 arg_types = { 1835 "expressions": True, 1836 "ordinality": False, 1837 "alias": False, 1838 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
1855class Select(Subqueryable): 1856 arg_types = { 1857 "with": False, 1858 "expressions": False, 1859 "hint": False, 1860 "distinct": False, 1861 "into": False, 1862 "from": False, 1863 **QUERY_MODIFIERS, 1864 } 1865 1866 def from_(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1867 """ 1868 Set the FROM expression. 1869 1870 Example: 1871 >>> Select().from_("tbl").select("x").sql() 1872 'SELECT x FROM tbl' 1873 1874 Args: 1875 *expressions (str | Expression): the SQL code strings to parse. 1876 If a `From` instance is passed, this is used as-is. 1877 If another `Expression` instance is passed, it will be wrapped in a `From`. 1878 append (bool): if `True`, add to any existing expressions. 1879 Otherwise, this flattens all the `From` expression into a single expression. 1880 dialect (str): the dialect used to parse the input expression. 1881 copy (bool): if `False`, modify this expression instance in-place. 1882 opts (kwargs): other options to use to parse the input expressions. 1883 1884 Returns: 1885 Select: the modified expression. 1886 """ 1887 return _apply_child_list_builder( 1888 *expressions, 1889 instance=self, 1890 arg="from", 1891 append=append, 1892 copy=copy, 1893 prefix="FROM", 1894 into=From, 1895 dialect=dialect, 1896 **opts, 1897 ) 1898 1899 def group_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1900 """ 1901 Set the GROUP BY expression. 1902 1903 Example: 1904 >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql() 1905 'SELECT x, COUNT(1) FROM tbl GROUP BY x' 1906 1907 Args: 1908 *expressions (str | Expression): the SQL code strings to parse. 1909 If a `Group` instance is passed, this is used as-is. 1910 If another `Expression` instance is passed, it will be wrapped in a `Group`. 1911 If nothing is passed in then a group by is not applied to the expression 1912 append (bool): if `True`, add to any existing expressions. 1913 Otherwise, this flattens all the `Group` expression into a single expression. 1914 dialect (str): the dialect used to parse the input expression. 1915 copy (bool): if `False`, modify this expression instance in-place. 1916 opts (kwargs): other options to use to parse the input expressions. 1917 1918 Returns: 1919 Select: the modified expression. 1920 """ 1921 if not expressions: 1922 return self if not copy else self.copy() 1923 return _apply_child_list_builder( 1924 *expressions, 1925 instance=self, 1926 arg="group", 1927 append=append, 1928 copy=copy, 1929 prefix="GROUP BY", 1930 into=Group, 1931 dialect=dialect, 1932 **opts, 1933 ) 1934 1935 def order_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1936 """ 1937 Set the ORDER BY expression. 1938 1939 Example: 1940 >>> Select().from_("tbl").select("x").order_by("x DESC").sql() 1941 'SELECT x FROM tbl ORDER BY x DESC' 1942 1943 Args: 1944 *expressions (str | Expression): the SQL code strings to parse. 1945 If a `Group` instance is passed, this is used as-is. 1946 If another `Expression` instance is passed, it will be wrapped in a `Order`. 1947 append (bool): if `True`, add to any existing expressions. 1948 Otherwise, this flattens all the `Order` expression into a single expression. 1949 dialect (str): the dialect used to parse the input expression. 1950 copy (bool): if `False`, modify this expression instance in-place. 1951 opts (kwargs): other options to use to parse the input expressions. 1952 1953 Returns: 1954 Select: the modified expression. 1955 """ 1956 return _apply_child_list_builder( 1957 *expressions, 1958 instance=self, 1959 arg="order", 1960 append=append, 1961 copy=copy, 1962 prefix="ORDER BY", 1963 into=Order, 1964 dialect=dialect, 1965 **opts, 1966 ) 1967 1968 def sort_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1969 """ 1970 Set the SORT BY expression. 1971 1972 Example: 1973 >>> Select().from_("tbl").select("x").sort_by("x DESC").sql() 1974 'SELECT x FROM tbl SORT BY x DESC' 1975 1976 Args: 1977 *expressions (str | Expression): the SQL code strings to parse. 1978 If a `Group` instance is passed, this is used as-is. 1979 If another `Expression` instance is passed, it will be wrapped in a `SORT`. 1980 append (bool): if `True`, add to any existing expressions. 1981 Otherwise, this flattens all the `Order` expression into a single expression. 1982 dialect (str): the dialect used to parse the input expression. 1983 copy (bool): if `False`, modify this expression instance in-place. 1984 opts (kwargs): other options to use to parse the input expressions. 1985 1986 Returns: 1987 Select: the modified expression. 1988 """ 1989 return _apply_child_list_builder( 1990 *expressions, 1991 instance=self, 1992 arg="sort", 1993 append=append, 1994 copy=copy, 1995 prefix="SORT BY", 1996 into=Sort, 1997 dialect=dialect, 1998 **opts, 1999 ) 2000 2001 def cluster_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2002 """ 2003 Set the CLUSTER BY expression. 2004 2005 Example: 2006 >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql() 2007 'SELECT x FROM tbl CLUSTER BY x DESC' 2008 2009 Args: 2010 *expressions (str | Expression): the SQL code strings to parse. 2011 If a `Group` instance is passed, this is used as-is. 2012 If another `Expression` instance is passed, it will be wrapped in a `Cluster`. 2013 append (bool): if `True`, add to any existing expressions. 2014 Otherwise, this flattens all the `Order` expression into a single expression. 2015 dialect (str): the dialect used to parse the input expression. 2016 copy (bool): if `False`, modify this expression instance in-place. 2017 opts (kwargs): other options to use to parse the input expressions. 2018 2019 Returns: 2020 Select: the modified expression. 2021 """ 2022 return _apply_child_list_builder( 2023 *expressions, 2024 instance=self, 2025 arg="cluster", 2026 append=append, 2027 copy=copy, 2028 prefix="CLUSTER BY", 2029 into=Cluster, 2030 dialect=dialect, 2031 **opts, 2032 ) 2033 2034 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2035 """ 2036 Set the LIMIT expression. 2037 2038 Example: 2039 >>> Select().from_("tbl").select("x").limit(10).sql() 2040 'SELECT x FROM tbl LIMIT 10' 2041 2042 Args: 2043 expression (str | int | Expression): the SQL code string to parse. 2044 This can also be an integer. 2045 If a `Limit` instance is passed, this is used as-is. 2046 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2047 dialect (str): the dialect used to parse the input expression. 2048 copy (bool): if `False`, modify this expression instance in-place. 2049 opts (kwargs): other options to use to parse the input expressions. 2050 2051 Returns: 2052 Select: the modified expression. 2053 """ 2054 return _apply_builder( 2055 expression=expression, 2056 instance=self, 2057 arg="limit", 2058 into=Limit, 2059 prefix="LIMIT", 2060 dialect=dialect, 2061 copy=copy, 2062 **opts, 2063 ) 2064 2065 def offset(self, expression, dialect=None, copy=True, **opts) -> Select: 2066 """ 2067 Set the OFFSET expression. 2068 2069 Example: 2070 >>> Select().from_("tbl").select("x").offset(10).sql() 2071 'SELECT x FROM tbl OFFSET 10' 2072 2073 Args: 2074 expression (str | int | Expression): the SQL code string to parse. 2075 This can also be an integer. 2076 If a `Offset` instance is passed, this is used as-is. 2077 If another `Expression` instance is passed, it will be wrapped in a `Offset`. 2078 dialect (str): the dialect used to parse the input expression. 2079 copy (bool): if `False`, modify this expression instance in-place. 2080 opts (kwargs): other options to use to parse the input expressions. 2081 2082 Returns: 2083 Select: the modified expression. 2084 """ 2085 return _apply_builder( 2086 expression=expression, 2087 instance=self, 2088 arg="offset", 2089 into=Offset, 2090 prefix="OFFSET", 2091 dialect=dialect, 2092 copy=copy, 2093 **opts, 2094 ) 2095 2096 def select( 2097 self, 2098 *expressions: str | Expression, 2099 append: bool = True, 2100 dialect: DialectType = None, 2101 copy: bool = True, 2102 **opts, 2103 ) -> Select: 2104 """ 2105 Append to or set the SELECT expressions. 2106 2107 Example: 2108 >>> Select().select("x", "y").sql() 2109 'SELECT x, y' 2110 2111 Args: 2112 *expressions: the SQL code strings to parse. 2113 If an `Expression` instance is passed, it will be used as-is. 2114 append: if `True`, add to any existing expressions. 2115 Otherwise, this resets the expressions. 2116 dialect: the dialect used to parse the input expressions. 2117 copy: if `False`, modify this expression instance in-place. 2118 opts: other options to use to parse the input expressions. 2119 2120 Returns: 2121 Select: the modified expression. 2122 """ 2123 return _apply_list_builder( 2124 *expressions, 2125 instance=self, 2126 arg="expressions", 2127 append=append, 2128 dialect=dialect, 2129 copy=copy, 2130 **opts, 2131 ) 2132 2133 def lateral(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2134 """ 2135 Append to or set the LATERAL expressions. 2136 2137 Example: 2138 >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql() 2139 'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z' 2140 2141 Args: 2142 *expressions (str | Expression): the SQL code strings to parse. 2143 If an `Expression` instance is passed, it will be used as-is. 2144 append (bool): if `True`, add to any existing expressions. 2145 Otherwise, this resets the expressions. 2146 dialect (str): the dialect used to parse the input expressions. 2147 copy (bool): if `False`, modify this expression instance in-place. 2148 opts (kwargs): other options to use to parse the input expressions. 2149 2150 Returns: 2151 Select: the modified expression. 2152 """ 2153 return _apply_list_builder( 2154 *expressions, 2155 instance=self, 2156 arg="laterals", 2157 append=append, 2158 into=Lateral, 2159 prefix="LATERAL VIEW", 2160 dialect=dialect, 2161 copy=copy, 2162 **opts, 2163 ) 2164 2165 def join( 2166 self, 2167 expression, 2168 on=None, 2169 using=None, 2170 append=True, 2171 join_type=None, 2172 join_alias=None, 2173 dialect=None, 2174 copy=True, 2175 **opts, 2176 ) -> Select: 2177 """ 2178 Append to or set the JOIN expressions. 2179 2180 Example: 2181 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 2182 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y' 2183 2184 >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 2185 'SELECT 1 FROM a JOIN b USING (x, y, z)' 2186 2187 Use `join_type` to change the type of join: 2188 2189 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 2190 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y' 2191 2192 Args: 2193 expression (str | Expression): the SQL code string to parse. 2194 If an `Expression` instance is passed, it will be used as-is. 2195 on (str | Expression): optionally specify the join "on" criteria as a SQL string. 2196 If an `Expression` instance is passed, it will be used as-is. 2197 using (str | Expression): optionally specify the join "using" criteria as a SQL string. 2198 If an `Expression` instance is passed, it will be used as-is. 2199 append (bool): if `True`, add to any existing expressions. 2200 Otherwise, this resets the expressions. 2201 join_type (str): If set, alter the parsed join type 2202 dialect (str): the dialect used to parse the input expressions. 2203 copy (bool): if `False`, modify this expression instance in-place. 2204 opts (kwargs): other options to use to parse the input expressions. 2205 2206 Returns: 2207 Select: the modified expression. 2208 """ 2209 parse_args = {"dialect": dialect, **opts} 2210 2211 try: 2212 expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args) 2213 except ParseError: 2214 expression = maybe_parse(expression, into=(Join, Expression), **parse_args) 2215 2216 join = expression if isinstance(expression, Join) else Join(this=expression) 2217 2218 if isinstance(join.this, Select): 2219 join.this.replace(join.this.subquery()) 2220 2221 if join_type: 2222 natural: t.Optional[Token] 2223 side: t.Optional[Token] 2224 kind: t.Optional[Token] 2225 2226 natural, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args) # type: ignore 2227 2228 if natural: 2229 join.set("natural", True) 2230 if side: 2231 join.set("side", side.text) 2232 if kind: 2233 join.set("kind", kind.text) 2234 2235 if on: 2236 on = and_(*ensure_collection(on), dialect=dialect, **opts) 2237 join.set("on", on) 2238 2239 if using: 2240 join = _apply_list_builder( 2241 *ensure_collection(using), 2242 instance=join, 2243 arg="using", 2244 append=append, 2245 copy=copy, 2246 **opts, 2247 ) 2248 2249 if join_alias: 2250 join.set("this", alias_(join.this, join_alias, table=True)) 2251 return _apply_list_builder( 2252 join, 2253 instance=self, 2254 arg="joins", 2255 append=append, 2256 copy=copy, 2257 **opts, 2258 ) 2259 2260 def where(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2261 """ 2262 Append to or set the WHERE expressions. 2263 2264 Example: 2265 >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql() 2266 "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'" 2267 2268 Args: 2269 *expressions (str | Expression): the SQL code strings to parse. 2270 If an `Expression` instance is passed, it will be used as-is. 2271 Multiple expressions are combined with an AND operator. 2272 append (bool): if `True`, AND the new expressions to any existing expression. 2273 Otherwise, this resets the expression. 2274 dialect (str): the dialect used to parse the input expressions. 2275 copy (bool): if `False`, modify this expression instance in-place. 2276 opts (kwargs): other options to use to parse the input expressions. 2277 2278 Returns: 2279 Select: the modified expression. 2280 """ 2281 return _apply_conjunction_builder( 2282 *expressions, 2283 instance=self, 2284 arg="where", 2285 append=append, 2286 into=Where, 2287 dialect=dialect, 2288 copy=copy, 2289 **opts, 2290 ) 2291 2292 def having(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2293 """ 2294 Append to or set the HAVING expressions. 2295 2296 Example: 2297 >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql() 2298 'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3' 2299 2300 Args: 2301 *expressions (str | Expression): the SQL code strings to parse. 2302 If an `Expression` instance is passed, it will be used as-is. 2303 Multiple expressions are combined with an AND operator. 2304 append (bool): if `True`, AND the new expressions to any existing expression. 2305 Otherwise, this resets the expression. 2306 dialect (str): the dialect used to parse the input expressions. 2307 copy (bool): if `False`, modify this expression instance in-place. 2308 opts (kwargs): other options to use to parse the input expressions. 2309 2310 Returns: 2311 Select: the modified expression. 2312 """ 2313 return _apply_conjunction_builder( 2314 *expressions, 2315 instance=self, 2316 arg="having", 2317 append=append, 2318 into=Having, 2319 dialect=dialect, 2320 copy=copy, 2321 **opts, 2322 ) 2323 2324 def window(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2325 return _apply_list_builder( 2326 *expressions, 2327 instance=self, 2328 arg="windows", 2329 append=append, 2330 into=Window, 2331 dialect=dialect, 2332 copy=copy, 2333 **opts, 2334 ) 2335 2336 def distinct(self, distinct=True, copy=True) -> Select: 2337 """ 2338 Set the OFFSET expression. 2339 2340 Example: 2341 >>> Select().from_("tbl").select("x").distinct().sql() 2342 'SELECT DISTINCT x FROM tbl' 2343 2344 Args: 2345 distinct (bool): whether the Select should be distinct 2346 copy (bool): if `False`, modify this expression instance in-place. 2347 2348 Returns: 2349 Select: the modified expression. 2350 """ 2351 instance = _maybe_copy(self, copy) 2352 instance.set("distinct", Distinct() if distinct else None) 2353 return instance 2354 2355 def ctas(self, table, properties=None, dialect=None, copy=True, **opts) -> Create: 2356 """ 2357 Convert this expression to a CREATE TABLE AS statement. 2358 2359 Example: 2360 >>> Select().select("*").from_("tbl").ctas("x").sql() 2361 'CREATE TABLE x AS SELECT * FROM tbl' 2362 2363 Args: 2364 table (str | Expression): the SQL code string to parse as the table name. 2365 If another `Expression` instance is passed, it will be used as-is. 2366 properties (dict): an optional mapping of table properties 2367 dialect (str): the dialect used to parse the input table. 2368 copy (bool): if `False`, modify this expression instance in-place. 2369 opts (kwargs): other options to use to parse the input table. 2370 2371 Returns: 2372 Create: the CREATE TABLE AS expression 2373 """ 2374 instance = _maybe_copy(self, copy) 2375 table_expression = maybe_parse( 2376 table, 2377 into=Table, 2378 dialect=dialect, 2379 **opts, 2380 ) 2381 properties_expression = None 2382 if properties: 2383 properties_expression = Properties.from_dict(properties) 2384 2385 return Create( 2386 this=table_expression, 2387 kind="table", 2388 expression=instance, 2389 properties=properties_expression, 2390 ) 2391 2392 def lock(self, update: bool = True, copy: bool = True) -> Select: 2393 """ 2394 Set the locking read mode for this expression. 2395 2396 Examples: 2397 >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql") 2398 "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE" 2399 2400 >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql") 2401 "SELECT x FROM tbl WHERE x = 'a' FOR SHARE" 2402 2403 Args: 2404 update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`. 2405 copy: if `False`, modify this expression instance in-place. 2406 2407 Returns: 2408 The modified expression. 2409 """ 2410 2411 inst = _maybe_copy(self, copy) 2412 inst.set("lock", Lock(update=update)) 2413 2414 return inst 2415 2416 @property 2417 def named_selects(self) -> t.List[str]: 2418 return [e.output_name for e in self.expressions if e.alias_or_name] 2419 2420 @property 2421 def selects(self) -> t.List[Expression]: 2422 return self.expressions
1866 def from_(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1867 """ 1868 Set the FROM expression. 1869 1870 Example: 1871 >>> Select().from_("tbl").select("x").sql() 1872 'SELECT x FROM tbl' 1873 1874 Args: 1875 *expressions (str | Expression): the SQL code strings to parse. 1876 If a `From` instance is passed, this is used as-is. 1877 If another `Expression` instance is passed, it will be wrapped in a `From`. 1878 append (bool): if `True`, add to any existing expressions. 1879 Otherwise, this flattens all the `From` expression into a single expression. 1880 dialect (str): the dialect used to parse the input expression. 1881 copy (bool): if `False`, modify this expression instance in-place. 1882 opts (kwargs): other options to use to parse the input expressions. 1883 1884 Returns: 1885 Select: the modified expression. 1886 """ 1887 return _apply_child_list_builder( 1888 *expressions, 1889 instance=self, 1890 arg="from", 1891 append=append, 1892 copy=copy, 1893 prefix="FROM", 1894 into=From, 1895 dialect=dialect, 1896 **opts, 1897 )
Set the FROM expression.
Example:
>>> Select().from_("tbl").select("x").sql() 'SELECT x FROM tbl'
Arguments:
- *expressions (str | Expression): the SQL code strings to parse.
If a
From
instance is passed, this is used as-is. If anotherExpression
instance is passed, it will be wrapped in aFrom
. - append (bool): if
True
, add to any existing expressions. Otherwise, this flattens all theFrom
expression 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.
1899 def group_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1900 """ 1901 Set the GROUP BY expression. 1902 1903 Example: 1904 >>> Select().from_("tbl").select("x", "COUNT(1)").group_by("x").sql() 1905 'SELECT x, COUNT(1) FROM tbl GROUP BY x' 1906 1907 Args: 1908 *expressions (str | Expression): the SQL code strings to parse. 1909 If a `Group` instance is passed, this is used as-is. 1910 If another `Expression` instance is passed, it will be wrapped in a `Group`. 1911 If nothing is passed in then a group by is not applied to the expression 1912 append (bool): if `True`, add to any existing expressions. 1913 Otherwise, this flattens all the `Group` expression into a single expression. 1914 dialect (str): the dialect used to parse the input expression. 1915 copy (bool): if `False`, modify this expression instance in-place. 1916 opts (kwargs): other options to use to parse the input expressions. 1917 1918 Returns: 1919 Select: the modified expression. 1920 """ 1921 if not expressions: 1922 return self if not copy else self.copy() 1923 return _apply_child_list_builder( 1924 *expressions, 1925 instance=self, 1926 arg="group", 1927 append=append, 1928 copy=copy, 1929 prefix="GROUP BY", 1930 into=Group, 1931 dialect=dialect, 1932 **opts, 1933 )
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
Group
instance is passed, this is used as-is. If anotherExpression
instance 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 theGroup
expression 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.
1935 def order_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1936 """ 1937 Set the ORDER BY expression. 1938 1939 Example: 1940 >>> Select().from_("tbl").select("x").order_by("x DESC").sql() 1941 'SELECT x FROM tbl ORDER BY x DESC' 1942 1943 Args: 1944 *expressions (str | Expression): the SQL code strings to parse. 1945 If a `Group` instance is passed, this is used as-is. 1946 If another `Expression` instance is passed, it will be wrapped in a `Order`. 1947 append (bool): if `True`, add to any existing expressions. 1948 Otherwise, this flattens all the `Order` expression into a single expression. 1949 dialect (str): the dialect used to parse the input expression. 1950 copy (bool): if `False`, modify this expression instance in-place. 1951 opts (kwargs): other options to use to parse the input expressions. 1952 1953 Returns: 1954 Select: the modified expression. 1955 """ 1956 return _apply_child_list_builder( 1957 *expressions, 1958 instance=self, 1959 arg="order", 1960 append=append, 1961 copy=copy, 1962 prefix="ORDER BY", 1963 into=Order, 1964 dialect=dialect, 1965 **opts, 1966 )
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
Group
instance is passed, this is used as-is. If anotherExpression
instance is passed, it will be wrapped in aOrder
. - append (bool): if
True
, add to any existing expressions. Otherwise, this flattens all theOrder
expression 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.
1968 def sort_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 1969 """ 1970 Set the SORT BY expression. 1971 1972 Example: 1973 >>> Select().from_("tbl").select("x").sort_by("x DESC").sql() 1974 'SELECT x FROM tbl SORT BY x DESC' 1975 1976 Args: 1977 *expressions (str | Expression): the SQL code strings to parse. 1978 If a `Group` instance is passed, this is used as-is. 1979 If another `Expression` instance is passed, it will be wrapped in a `SORT`. 1980 append (bool): if `True`, add to any existing expressions. 1981 Otherwise, this flattens all the `Order` expression into a single expression. 1982 dialect (str): the dialect used to parse the input expression. 1983 copy (bool): if `False`, modify this expression instance in-place. 1984 opts (kwargs): other options to use to parse the input expressions. 1985 1986 Returns: 1987 Select: the modified expression. 1988 """ 1989 return _apply_child_list_builder( 1990 *expressions, 1991 instance=self, 1992 arg="sort", 1993 append=append, 1994 copy=copy, 1995 prefix="SORT BY", 1996 into=Sort, 1997 dialect=dialect, 1998 **opts, 1999 )
Set the SORT BY expression.
Example:
>>> Select().from_("tbl").select("x").sort_by("x DESC").sql() 'SELECT x FROM tbl SORT BY x DESC'
Arguments:
- *expressions (str | Expression): the SQL code strings to parse.
If a
Group
instance is passed, this is used as-is. If anotherExpression
instance is passed, it will be wrapped in aSORT
. - append (bool): if
True
, add to any existing expressions. Otherwise, this flattens all theOrder
expression 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.
2001 def cluster_by(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2002 """ 2003 Set the CLUSTER BY expression. 2004 2005 Example: 2006 >>> Select().from_("tbl").select("x").cluster_by("x DESC").sql() 2007 'SELECT x FROM tbl CLUSTER BY x DESC' 2008 2009 Args: 2010 *expressions (str | Expression): the SQL code strings to parse. 2011 If a `Group` instance is passed, this is used as-is. 2012 If another `Expression` instance is passed, it will be wrapped in a `Cluster`. 2013 append (bool): if `True`, add to any existing expressions. 2014 Otherwise, this flattens all the `Order` expression into a single expression. 2015 dialect (str): the dialect used to parse the input expression. 2016 copy (bool): if `False`, modify this expression instance in-place. 2017 opts (kwargs): other options to use to parse the input expressions. 2018 2019 Returns: 2020 Select: the modified expression. 2021 """ 2022 return _apply_child_list_builder( 2023 *expressions, 2024 instance=self, 2025 arg="cluster", 2026 append=append, 2027 copy=copy, 2028 prefix="CLUSTER BY", 2029 into=Cluster, 2030 dialect=dialect, 2031 **opts, 2032 )
Set the CLUSTER BY expression.
Example:
>>> Select().from_("tbl").select("x").cluster_by("x DESC").sql() 'SELECT x FROM tbl CLUSTER BY x DESC'
Arguments:
- *expressions (str | Expression): the SQL code strings to parse.
If a
Group
instance is passed, this is used as-is. If anotherExpression
instance is passed, it will be wrapped in aCluster
. - append (bool): if
True
, add to any existing expressions. Otherwise, this flattens all theOrder
expression 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.
2034 def limit(self, expression, dialect=None, copy=True, **opts) -> Select: 2035 """ 2036 Set the LIMIT expression. 2037 2038 Example: 2039 >>> Select().from_("tbl").select("x").limit(10).sql() 2040 'SELECT x FROM tbl LIMIT 10' 2041 2042 Args: 2043 expression (str | int | Expression): the SQL code string to parse. 2044 This can also be an integer. 2045 If a `Limit` instance is passed, this is used as-is. 2046 If another `Expression` instance is passed, it will be wrapped in a `Limit`. 2047 dialect (str): the dialect used to parse the input expression. 2048 copy (bool): if `False`, modify this expression instance in-place. 2049 opts (kwargs): other options to use to parse the input expressions. 2050 2051 Returns: 2052 Select: the modified expression. 2053 """ 2054 return _apply_builder( 2055 expression=expression, 2056 instance=self, 2057 arg="limit", 2058 into=Limit, 2059 prefix="LIMIT", 2060 dialect=dialect, 2061 copy=copy, 2062 **opts, 2063 )
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
Limit
instance is passed, this is used as-is. If anotherExpression
instance 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.
2065 def offset(self, expression, dialect=None, copy=True, **opts) -> Select: 2066 """ 2067 Set the OFFSET expression. 2068 2069 Example: 2070 >>> Select().from_("tbl").select("x").offset(10).sql() 2071 'SELECT x FROM tbl OFFSET 10' 2072 2073 Args: 2074 expression (str | int | Expression): the SQL code string to parse. 2075 This can also be an integer. 2076 If a `Offset` instance is passed, this is used as-is. 2077 If another `Expression` instance is passed, it will be wrapped in a `Offset`. 2078 dialect (str): the dialect used to parse the input expression. 2079 copy (bool): if `False`, modify this expression instance in-place. 2080 opts (kwargs): other options to use to parse the input expressions. 2081 2082 Returns: 2083 Select: the modified expression. 2084 """ 2085 return _apply_builder( 2086 expression=expression, 2087 instance=self, 2088 arg="offset", 2089 into=Offset, 2090 prefix="OFFSET", 2091 dialect=dialect, 2092 copy=copy, 2093 **opts, 2094 )
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
Offset
instance is passed, this is used as-is. If anotherExpression
instance 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.
2096 def select( 2097 self, 2098 *expressions: str | Expression, 2099 append: bool = True, 2100 dialect: DialectType = None, 2101 copy: bool = True, 2102 **opts, 2103 ) -> Select: 2104 """ 2105 Append to or set the SELECT expressions. 2106 2107 Example: 2108 >>> Select().select("x", "y").sql() 2109 'SELECT x, y' 2110 2111 Args: 2112 *expressions: the SQL code strings to parse. 2113 If an `Expression` instance is passed, it will be used as-is. 2114 append: if `True`, add to any existing expressions. 2115 Otherwise, this resets the expressions. 2116 dialect: the dialect used to parse the input expressions. 2117 copy: if `False`, modify this expression instance in-place. 2118 opts: other options to use to parse the input expressions. 2119 2120 Returns: 2121 Select: the modified expression. 2122 """ 2123 return _apply_list_builder( 2124 *expressions, 2125 instance=self, 2126 arg="expressions", 2127 append=append, 2128 dialect=dialect, 2129 copy=copy, 2130 **opts, 2131 )
Append to or set the SELECT expressions.
Example:
>>> Select().select("x", "y").sql() 'SELECT x, y'
Arguments:
- *expressions: the SQL code strings to parse.
If an
Expression
instance is passed, it will be used as-is. - append: if
True
, add to any existing expressions. Otherwise, this resets the expressions. - dialect: the dialect used to parse the input expressions.
- copy: if
False
, modify this expression instance in-place. - opts: other options to use to parse the input expressions.
Returns:
Select: the modified expression.
2133 def lateral(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2134 """ 2135 Append to or set the LATERAL expressions. 2136 2137 Example: 2138 >>> Select().select("x").lateral("OUTER explode(y) tbl2 AS z").from_("tbl").sql() 2139 'SELECT x FROM tbl LATERAL VIEW OUTER EXPLODE(y) tbl2 AS z' 2140 2141 Args: 2142 *expressions (str | Expression): the SQL code strings to parse. 2143 If an `Expression` instance is passed, it will be used as-is. 2144 append (bool): if `True`, add to any existing expressions. 2145 Otherwise, this resets the expressions. 2146 dialect (str): the dialect used to parse the input expressions. 2147 copy (bool): if `False`, modify this expression instance in-place. 2148 opts (kwargs): other options to use to parse the input expressions. 2149 2150 Returns: 2151 Select: the modified expression. 2152 """ 2153 return _apply_list_builder( 2154 *expressions, 2155 instance=self, 2156 arg="laterals", 2157 append=append, 2158 into=Lateral, 2159 prefix="LATERAL VIEW", 2160 dialect=dialect, 2161 copy=copy, 2162 **opts, 2163 )
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
Expression
instance 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.
2165 def join( 2166 self, 2167 expression, 2168 on=None, 2169 using=None, 2170 append=True, 2171 join_type=None, 2172 join_alias=None, 2173 dialect=None, 2174 copy=True, 2175 **opts, 2176 ) -> Select: 2177 """ 2178 Append to or set the JOIN expressions. 2179 2180 Example: 2181 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 2182 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y' 2183 2184 >>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 2185 'SELECT 1 FROM a JOIN b USING (x, y, z)' 2186 2187 Use `join_type` to change the type of join: 2188 2189 >>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 2190 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y' 2191 2192 Args: 2193 expression (str | Expression): the SQL code string to parse. 2194 If an `Expression` instance is passed, it will be used as-is. 2195 on (str | Expression): optionally specify the join "on" criteria as a SQL string. 2196 If an `Expression` instance is passed, it will be used as-is. 2197 using (str | Expression): optionally specify the join "using" criteria as a SQL string. 2198 If an `Expression` instance is passed, it will be used as-is. 2199 append (bool): if `True`, add to any existing expressions. 2200 Otherwise, this resets the expressions. 2201 join_type (str): If set, alter the parsed join type 2202 dialect (str): the dialect used to parse the input expressions. 2203 copy (bool): if `False`, modify this expression instance in-place. 2204 opts (kwargs): other options to use to parse the input expressions. 2205 2206 Returns: 2207 Select: the modified expression. 2208 """ 2209 parse_args = {"dialect": dialect, **opts} 2210 2211 try: 2212 expression = maybe_parse(expression, into=Join, prefix="JOIN", **parse_args) 2213 except ParseError: 2214 expression = maybe_parse(expression, into=(Join, Expression), **parse_args) 2215 2216 join = expression if isinstance(expression, Join) else Join(this=expression) 2217 2218 if isinstance(join.this, Select): 2219 join.this.replace(join.this.subquery()) 2220 2221 if join_type: 2222 natural: t.Optional[Token] 2223 side: t.Optional[Token] 2224 kind: t.Optional[Token] 2225 2226 natural, side, kind = maybe_parse(join_type, into="JOIN_TYPE", **parse_args) # type: ignore 2227 2228 if natural: 2229 join.set("natural", True) 2230 if side: 2231 join.set("side", side.text) 2232 if kind: 2233 join.set("kind", kind.text) 2234 2235 if on: 2236 on = and_(*ensure_collection(on), dialect=dialect, **opts) 2237 join.set("on", on) 2238 2239 if using: 2240 join = _apply_list_builder( 2241 *ensure_collection(using), 2242 instance=join, 2243 arg="using", 2244 append=append, 2245 copy=copy, 2246 **opts, 2247 ) 2248 2249 if join_alias: 2250 join.set("this", alias_(join.this, join_alias, table=True)) 2251 return _apply_list_builder( 2252 join, 2253 instance=self, 2254 arg="joins", 2255 append=append, 2256 copy=copy, 2257 **opts, 2258 )
Append to or set the JOIN expressions.
Example:
>>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y").sql() 'SELECT * FROM tbl JOIN tbl2 ON tbl1.y = tbl2.y'
>>> Select().select("1").from_("a").join("b", using=["x", "y", "z"]).sql() 'SELECT 1 FROM a JOIN b USING (x, y, z)'
Use
join_type
to change the type of join:>>> Select().select("*").from_("tbl").join("tbl2", on="tbl1.y = tbl2.y", join_type="left outer").sql() 'SELECT * FROM tbl LEFT OUTER JOIN tbl2 ON tbl1.y = tbl2.y'
Arguments:
- expression (str | Expression): the SQL code string to parse.
If an
Expression
instance is passed, it will be used as-is. - on (str | Expression): optionally specify the join "on" criteria as a SQL string.
If an
Expression
instance is passed, it will be used as-is. - using (str | Expression): optionally specify the join "using" criteria as a SQL string.
If an
Expression
instance 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.
2260 def where(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2261 """ 2262 Append to or set the WHERE expressions. 2263 2264 Example: 2265 >>> Select().select("x").from_("tbl").where("x = 'a' OR x < 'b'").sql() 2266 "SELECT x FROM tbl WHERE x = 'a' OR x < 'b'" 2267 2268 Args: 2269 *expressions (str | Expression): the SQL code strings to parse. 2270 If an `Expression` instance is passed, it will be used as-is. 2271 Multiple expressions are combined with an AND operator. 2272 append (bool): if `True`, AND the new expressions to any existing expression. 2273 Otherwise, this resets the expression. 2274 dialect (str): the dialect used to parse the input expressions. 2275 copy (bool): if `False`, modify this expression instance in-place. 2276 opts (kwargs): other options to use to parse the input expressions. 2277 2278 Returns: 2279 Select: the modified expression. 2280 """ 2281 return _apply_conjunction_builder( 2282 *expressions, 2283 instance=self, 2284 arg="where", 2285 append=append, 2286 into=Where, 2287 dialect=dialect, 2288 copy=copy, 2289 **opts, 2290 )
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
Expression
instance 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.
2292 def having(self, *expressions, append=True, dialect=None, copy=True, **opts) -> Select: 2293 """ 2294 Append to or set the HAVING expressions. 2295 2296 Example: 2297 >>> Select().select("x", "COUNT(y)").from_("tbl").group_by("x").having("COUNT(y) > 3").sql() 2298 'SELECT x, COUNT(y) FROM tbl GROUP BY x HAVING COUNT(y) > 3' 2299 2300 Args: 2301 *expressions (str | Expression): the SQL code strings to parse. 2302 If an `Expression` instance is passed, it will be used as-is. 2303 Multiple expressions are combined with an AND operator. 2304 append (bool): if `True`, AND the new expressions to any existing expression. 2305 Otherwise, this resets the expression. 2306 dialect (str): the dialect used to parse the input expressions. 2307 copy (bool): if `False`, modify this expression instance in-place. 2308 opts (kwargs): other options to use to parse the input expressions. 2309 2310 Returns: 2311 Select: the modified expression. 2312 """ 2313 return _apply_conjunction_builder( 2314 *expressions, 2315 instance=self, 2316 arg="having", 2317 append=append, 2318 into=Having, 2319 dialect=dialect, 2320 copy=copy, 2321 **opts, 2322 )
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
Expression
instance 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.
2336 def distinct(self, distinct=True, copy=True) -> Select: 2337 """ 2338 Set the OFFSET expression. 2339 2340 Example: 2341 >>> Select().from_("tbl").select("x").distinct().sql() 2342 'SELECT DISTINCT x FROM tbl' 2343 2344 Args: 2345 distinct (bool): whether the Select should be distinct 2346 copy (bool): if `False`, modify this expression instance in-place. 2347 2348 Returns: 2349 Select: the modified expression. 2350 """ 2351 instance = _maybe_copy(self, copy) 2352 instance.set("distinct", Distinct() if distinct else None) 2353 return instance
Set the OFFSET expression.
Example:
>>> Select().from_("tbl").select("x").distinct().sql() 'SELECT DISTINCT x FROM tbl'
Arguments:
- distinct (bool): whether the Select should be distinct
- copy (bool): if
False
, modify this expression instance in-place.
Returns:
Select: the modified expression.
2355 def ctas(self, table, properties=None, dialect=None, copy=True, **opts) -> Create: 2356 """ 2357 Convert this expression to a CREATE TABLE AS statement. 2358 2359 Example: 2360 >>> Select().select("*").from_("tbl").ctas("x").sql() 2361 'CREATE TABLE x AS SELECT * FROM tbl' 2362 2363 Args: 2364 table (str | Expression): the SQL code string to parse as the table name. 2365 If another `Expression` instance is passed, it will be used as-is. 2366 properties (dict): an optional mapping of table properties 2367 dialect (str): the dialect used to parse the input table. 2368 copy (bool): if `False`, modify this expression instance in-place. 2369 opts (kwargs): other options to use to parse the input table. 2370 2371 Returns: 2372 Create: the CREATE TABLE AS expression 2373 """ 2374 instance = _maybe_copy(self, copy) 2375 table_expression = maybe_parse( 2376 table, 2377 into=Table, 2378 dialect=dialect, 2379 **opts, 2380 ) 2381 properties_expression = None 2382 if properties: 2383 properties_expression = Properties.from_dict(properties) 2384 2385 return Create( 2386 this=table_expression, 2387 kind="table", 2388 expression=instance, 2389 properties=properties_expression, 2390 )
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
Expression
instance 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
2392 def lock(self, update: bool = True, copy: bool = True) -> Select: 2393 """ 2394 Set the locking read mode for this expression. 2395 2396 Examples: 2397 >>> Select().select("x").from_("tbl").where("x = 'a'").lock().sql("mysql") 2398 "SELECT x FROM tbl WHERE x = 'a' FOR UPDATE" 2399 2400 >>> Select().select("x").from_("tbl").where("x = 'a'").lock(update=False).sql("mysql") 2401 "SELECT x FROM tbl WHERE x = 'a' FOR SHARE" 2402 2403 Args: 2404 update: if `True`, the locking type will be `FOR UPDATE`, else it will be `FOR SHARE`. 2405 copy: if `False`, modify this expression instance in-place. 2406 2407 Returns: 2408 The modified expression. 2409 """ 2410 2411 inst = _maybe_copy(self, copy) 2412 inst.set("lock", Lock(update=update)) 2413 2414 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
2425class Subquery(DerivedTable, Unionable): 2426 arg_types = { 2427 "this": True, 2428 "alias": False, 2429 "with": False, 2430 **QUERY_MODIFIERS, 2431 } 2432 2433 def unnest(self): 2434 """ 2435 Returns the first non subquery. 2436 """ 2437 expression = self 2438 while isinstance(expression, Subquery): 2439 expression = expression.this 2440 return expression 2441 2442 @property 2443 def output_name(self): 2444 return self.alias
2433 def unnest(self): 2434 """ 2435 Returns the first non subquery. 2436 """ 2437 expression = self 2438 while isinstance(expression, Subquery): 2439 expression = expression.this 2440 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
2447class TableSample(Expression): 2448 arg_types = { 2449 "this": False, 2450 "method": False, 2451 "bucket_numerator": False, 2452 "bucket_denominator": False, 2453 "bucket_field": False, 2454 "percent": False, 2455 "rows": False, 2456 "size": False, 2457 "seed": False, 2458 }
Inherited Members
2461class Tag(Expression): 2462 """Tags are used for generating arbitrary sql like SELECT <span>x</span>.""" 2463 2464 arg_types = { 2465 "this": False, 2466 "prefix": False, 2467 "postfix": False, 2468 }
Tags are used for generating arbitrary sql like SELECT x.
Inherited Members
2471class Pivot(Expression): 2472 arg_types = { 2473 "this": False, 2474 "expressions": True, 2475 "field": True, 2476 "unpivot": True, 2477 }
Inherited Members
2480class Window(Expression): 2481 arg_types = { 2482 "this": True, 2483 "partition_by": False, 2484 "order": False, 2485 "spec": False, 2486 "alias": False, 2487 }
Inherited Members
2490class WindowSpec(Expression): 2491 arg_types = { 2492 "kind": False, 2493 "start": False, 2494 "start_side": False, 2495 "end": False, 2496 "end_side": False, 2497 }
Inherited Members
Inherited Members
2504class Star(Expression): 2505 arg_types = {"except": False, "replace": False} 2506 2507 @property 2508 def name(self) -> str: 2509 return "*" 2510 2511 @property 2512 def output_name(self): 2513 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
Inherited Members
Inherited Members
2528class Null(Condition): 2529 arg_types: t.Dict[str, t.Any] = {} 2530 2531 @property 2532 def name(self) -> str: 2533 return "NULL"
Inherited Members
Inherited Members
2540class DataType(Expression): 2541 arg_types = { 2542 "this": True, 2543 "expressions": False, 2544 "nested": False, 2545 "values": False, 2546 "prefix": False, 2547 } 2548 2549 class Type(AutoName): 2550 CHAR = auto() 2551 NCHAR = auto() 2552 VARCHAR = auto() 2553 NVARCHAR = auto() 2554 TEXT = auto() 2555 MEDIUMTEXT = auto() 2556 LONGTEXT = auto() 2557 MEDIUMBLOB = auto() 2558 LONGBLOB = auto() 2559 BINARY = auto() 2560 VARBINARY = auto() 2561 INT = auto() 2562 TINYINT = auto() 2563 SMALLINT = auto() 2564 BIGINT = auto() 2565 FLOAT = auto() 2566 DOUBLE = auto() 2567 DECIMAL = auto() 2568 BOOLEAN = auto() 2569 JSON = auto() 2570 JSONB = auto() 2571 INTERVAL = auto() 2572 TIME = auto() 2573 TIMESTAMP = auto() 2574 TIMESTAMPTZ = auto() 2575 TIMESTAMPLTZ = auto() 2576 DATE = auto() 2577 DATETIME = auto() 2578 ARRAY = auto() 2579 MAP = auto() 2580 UUID = auto() 2581 GEOGRAPHY = auto() 2582 GEOMETRY = auto() 2583 STRUCT = auto() 2584 NULLABLE = auto() 2585 HLLSKETCH = auto() 2586 HSTORE = auto() 2587 SUPER = auto() 2588 SERIAL = auto() 2589 SMALLSERIAL = auto() 2590 BIGSERIAL = auto() 2591 XML = auto() 2592 UNIQUEIDENTIFIER = auto() 2593 MONEY = auto() 2594 SMALLMONEY = auto() 2595 ROWVERSION = auto() 2596 IMAGE = auto() 2597 VARIANT = auto() 2598 OBJECT = auto() 2599 NULL = auto() 2600 UNKNOWN = auto() # Sentinel value, useful for type annotation 2601 2602 TEXT_TYPES = { 2603 Type.CHAR, 2604 Type.NCHAR, 2605 Type.VARCHAR, 2606 Type.NVARCHAR, 2607 Type.TEXT, 2608 } 2609 2610 INTEGER_TYPES = { 2611 Type.INT, 2612 Type.TINYINT, 2613 Type.SMALLINT, 2614 Type.BIGINT, 2615 } 2616 2617 FLOAT_TYPES = { 2618 Type.FLOAT, 2619 Type.DOUBLE, 2620 } 2621 2622 NUMERIC_TYPES = {*INTEGER_TYPES, *FLOAT_TYPES} 2623 2624 TEMPORAL_TYPES = { 2625 Type.TIMESTAMP, 2626 Type.TIMESTAMPTZ, 2627 Type.TIMESTAMPLTZ, 2628 Type.DATE, 2629 Type.DATETIME, 2630 } 2631 2632 @classmethod 2633 def build( 2634 cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs 2635 ) -> DataType: 2636 from sqlglot import parse_one 2637 2638 if isinstance(dtype, str): 2639 if dtype.upper() in cls.Type.__members__: 2640 data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[dtype.upper()]) 2641 else: 2642 data_type_exp = parse_one(dtype, read=dialect, into=DataType) 2643 if data_type_exp is None: 2644 raise ValueError(f"Unparsable data type value: {dtype}") 2645 elif isinstance(dtype, DataType.Type): 2646 data_type_exp = DataType(this=dtype) 2647 elif isinstance(dtype, DataType): 2648 return dtype 2649 else: 2650 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type") 2651 return DataType(**{**data_type_exp.args, **kwargs}) 2652 2653 def is_type(self, dtype: DataType.Type) -> bool: 2654 return self.this == dtype
2632 @classmethod 2633 def build( 2634 cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs 2635 ) -> DataType: 2636 from sqlglot import parse_one 2637 2638 if isinstance(dtype, str): 2639 if dtype.upper() in cls.Type.__members__: 2640 data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[dtype.upper()]) 2641 else: 2642 data_type_exp = parse_one(dtype, read=dialect, into=DataType) 2643 if data_type_exp is None: 2644 raise ValueError(f"Unparsable data type value: {dtype}") 2645 elif isinstance(dtype, DataType.Type): 2646 data_type_exp = DataType(this=dtype) 2647 elif isinstance(dtype, DataType): 2648 return dtype 2649 else: 2650 raise ValueError(f"Invalid data type: {type(dtype)}. Expected str or DataType.Type") 2651 return DataType(**{**data_type_exp.args, **kwargs})
Inherited Members
2549 class Type(AutoName): 2550 CHAR = auto() 2551 NCHAR = auto() 2552 VARCHAR = auto() 2553 NVARCHAR = auto() 2554 TEXT = auto() 2555 MEDIUMTEXT = auto() 2556 LONGTEXT = auto() 2557 MEDIUMBLOB = auto() 2558 LONGBLOB = auto() 2559 BINARY = auto() 2560 VARBINARY = auto() 2561 INT = auto() 2562 TINYINT = auto() 2563 SMALLINT = auto() 2564 BIGINT = auto() 2565 FLOAT = auto() 2566 DOUBLE = auto() 2567 DECIMAL = auto() 2568 BOOLEAN = auto() 2569 JSON = auto() 2570 JSONB = auto() 2571 INTERVAL = auto() 2572 TIME = auto() 2573 TIMESTAMP = auto() 2574 TIMESTAMPTZ = auto() 2575 TIMESTAMPLTZ = auto() 2576 DATE = auto() 2577 DATETIME = auto() 2578 ARRAY = auto() 2579 MAP = auto() 2580 UUID = auto() 2581 GEOGRAPHY = auto() 2582 GEOMETRY = auto() 2583 STRUCT = auto() 2584 NULLABLE = auto() 2585 HLLSKETCH = auto() 2586 HSTORE = auto() 2587 SUPER = auto() 2588 SERIAL = auto() 2589 SMALLSERIAL = auto() 2590 BIGSERIAL = auto() 2591 XML = auto() 2592 UNIQUEIDENTIFIER = auto() 2593 MONEY = auto() 2594 SMALLMONEY = auto() 2595 ROWVERSION = auto() 2596 IMAGE = auto() 2597 VARIANT = auto() 2598 OBJECT = auto() 2599 NULL = auto() 2600 UNKNOWN = auto() # Sentinel value, useful for type annotation
An enumeration.
Inherited Members
- enum.Enum
- name
- value
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
2705class AddConstraint(Expression): 2706 arg_types = {"this": False, "expression": False, "enforced": False}
Inherited Members
Inherited Members
2714class Binary(Expression): 2715 arg_types = {"this": True, "expression": True} 2716 2717 @property 2718 def left(self): 2719 return self.this 2720 2721 @property 2722 def right(self): 2723 return self.expression
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Kwarg in special functions like func(kwarg => y).
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
2891class Alias(Expression): 2892 arg_types = {"this": True, "alias": False} 2893 2894 @property 2895 def output_name(self): 2896 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 ''
2899class Aliases(Expression): 2900 arg_types = {"this": True, "expressions": True} 2901 2902 @property 2903 def aliases(self): 2904 return self.expressions
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
2923class In(Predicate): 2924 arg_types = { 2925 "this": True, 2926 "expressions": False, 2927 "query": False, 2928 "unnest": False, 2929 "field": False, 2930 "is_global": False, 2931 }
Inherited Members
2934class TimeUnit(Expression): 2935 """Automatically converts unit arg into a var.""" 2936 2937 arg_types = {"unit": False} 2938 2939 def __init__(self, **args): 2940 unit = args.get("unit") 2941 if isinstance(unit, Column): 2942 args["unit"] = Var(this=unit.name) 2943 elif isinstance(unit, Week): 2944 unit.set("this", Var(this=unit.this.name)) 2945 super().__init__(**args)
Automatically converts unit arg into a var.
Inherited Members
Inherited Members
Inherited Members
2961class Func(Condition): 2962 """ 2963 The base class for all function expressions. 2964 2965 Attributes: 2966 is_var_len_args (bool): if set to True the last argument defined in arg_types will be 2967 treated as a variable length argument and the argument's value will be stored as a list. 2968 _sql_names (list): determines the SQL name (1st item in the list) and aliases (subsequent items) 2969 for this function expression. These values are used to map this node to a name during parsing 2970 as well as to provide the function's name during SQL string generation. By default the SQL 2971 name is set to the expression's class name transformed to snake case. 2972 """ 2973 2974 is_var_len_args = False 2975 2976 @classmethod 2977 def from_arg_list(cls, args): 2978 if cls.is_var_len_args: 2979 all_arg_keys = list(cls.arg_types) 2980 # If this function supports variable length argument treat the last argument as such. 2981 non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys 2982 num_non_var = len(non_var_len_arg_keys) 2983 2984 args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)} 2985 args_dict[all_arg_keys[-1]] = args[num_non_var:] 2986 else: 2987 args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)} 2988 2989 return cls(**args_dict) 2990 2991 @classmethod 2992 def sql_names(cls): 2993 if cls is Func: 2994 raise NotImplementedError( 2995 "SQL name is only supported by concrete function implementations" 2996 ) 2997 if "_sql_names" not in cls.__dict__: 2998 cls._sql_names = [camel_to_snake_case(cls.__name__)] 2999 return cls._sql_names 3000 3001 @classmethod 3002 def sql_name(cls): 3003 return cls.sql_names()[0] 3004 3005 @classmethod 3006 def default_parser_mappings(cls): 3007 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.
2976 @classmethod 2977 def from_arg_list(cls, args): 2978 if cls.is_var_len_args: 2979 all_arg_keys = list(cls.arg_types) 2980 # If this function supports variable length argument treat the last argument as such. 2981 non_var_len_arg_keys = all_arg_keys[:-1] if cls.is_var_len_args else all_arg_keys 2982 num_non_var = len(non_var_len_arg_keys) 2983 2984 args_dict = {arg_key: arg for arg, arg_key in zip(args, non_var_len_arg_keys)} 2985 args_dict[all_arg_keys[-1]] = args[num_non_var:] 2986 else: 2987 args_dict = {arg_key: arg for arg, arg_key in zip(args, cls.arg_types)} 2988 2989 return cls(**args_dict)
2991 @classmethod 2992 def sql_names(cls): 2993 if cls is Func: 2994 raise NotImplementedError( 2995 "SQL name is only supported by concrete function implementations" 2996 ) 2997 if "_sql_names" not in cls.__dict__: 2998 cls._sql_names = [camel_to_snake_case(cls.__name__)] 2999 return cls._sql_names
Inherited Members
Inherited Members
Inherited Members
3018class Anonymous(Func): 3019 arg_types = {"this": True, "expressions": False} 3020 is_var_len_args = True
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3048class ArrayConcat(Func): 3049 arg_types = {"this": True, "expressions": False} 3050 is_var_len_args = True
Inherited Members
Inherited Members
3057class ArrayFilter(Func): 3058 arg_types = {"this": True, "expression": True} 3059 _sql_names = ["FILTER", "ARRAY_FILTER"]
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3090class Cast(Func): 3091 arg_types = {"this": True, "to": True} 3092 3093 @property 3094 def name(self) -> str: 3095 return self.this.name 3096 3097 @property 3098 def to(self): 3099 return self.args["to"] 3100 3101 @property 3102 def output_name(self): 3103 return self.name 3104 3105 def is_type(self, dtype: DataType.Type) -> bool: 3106 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
Inherited Members
Inherited Members
3117class Ceil(Func): 3118 arg_types = {"this": True, "decimals": False} 3119 _sql_names = ["CEIL", "CEILING"]
Inherited Members
3122class Coalesce(Func): 3123 arg_types = {"this": True, "expressions": False} 3124 is_var_len_args = True
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3156class DateAdd(Func, TimeUnit): 3157 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3160class DateSub(Func, TimeUnit): 3161 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3164class DateDiff(Func, TimeUnit): 3165 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
Inherited Members
3172class DatetimeAdd(Func, TimeUnit): 3173 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3176class DatetimeSub(Func, TimeUnit): 3177 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3180class DatetimeDiff(Func, TimeUnit): 3181 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3184class DatetimeTrunc(Func, TimeUnit): 3185 arg_types = {"this": True, "unit": True, "zone": False}
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3212class TimestampAdd(Func, TimeUnit): 3213 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3216class TimestampSub(Func, TimeUnit): 3217 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3220class TimestampDiff(Func, TimeUnit): 3221 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3224class TimestampTrunc(Func, TimeUnit): 3225 arg_types = {"this": True, "unit": True, "zone": False}
Inherited Members
3228class TimeAdd(Func, TimeUnit): 3229 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3232class TimeSub(Func, TimeUnit): 3233 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
3236class TimeDiff(Func, TimeUnit): 3237 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
Inherited Members
3244class DateFromParts(Func): 3245 _sql_names = ["DATEFROMPARTS"] 3246 arg_types = {"year": True, "month": True, "day": True}
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3289class Greatest(Func): 3290 arg_types = {"this": True, "expressions": False} 3291 is_var_len_args = True
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3306class IfNull(Func): 3307 arg_types = {"this": True, "expression": False} 3308 _sql_names = ["IFNULL", "NVL"]
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3335class Least(Func): 3336 arg_types = {"this": True, "expressions": False} 3337 is_var_len_args = True
Inherited Members
Inherited Members
3344class Levenshtein(Func): 3345 arg_types = { 3346 "this": True, 3347 "expression": False, 3348 "ins_cost": False, 3349 "del_cost": False, 3350 "sub_cost": False, 3351 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3387class Matches(Func): 3388 """Oracle/Snowflake decode. 3389 https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm 3390 Pattern matching MATCHES(value, search1, result1, ...searchN, resultN, else) 3391 """ 3392 3393 arg_types = {"this": True, "expressions": True} 3394 is_var_len_args = True
Oracle/Snowflake decode. https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm Pattern matching MATCHES(value, search1, result1, ...searchN, resultN, else)
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3443class ApproxQuantile(Quantile): 3444 arg_types = {"this": True, "quantile": True, "accuracy": False, "weight": False}
Inherited Members
3447class ReadCSV(Func): 3448 _sql_names = ["READ_CSV"] 3449 is_var_len_args = True 3450 arg_types = {"this": True, "expressions": False}
Inherited Members
3453class Reduce(Func): 3454 arg_types = {"this": True, "initial": True, "merge": True, "finish": False}
Inherited Members
3457class RegexpExtract(Func): 3458 arg_types = { 3459 "this": True, 3460 "expression": True, 3461 "position": False, 3462 "occurrence": False, 3463 "group": False, 3464 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3513class StrPosition(Func): 3514 arg_types = { 3515 "this": True, 3516 "substr": True, 3517 "position": False, 3518 "instance": False, 3519 }
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3593class Trim(Func): 3594 arg_types = { 3595 "this": True, 3596 "expression": False, 3597 "position": False, 3598 "collation": False, 3599 }
Inherited Members
3602class TsOrDsAdd(Func, TimeUnit): 3603 arg_types = {"this": True, "expression": True, "unit": False}
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3628class UnixToTime(Func): 3629 arg_types = {"this": True, "scale": False, "zone": False, "hours": False, "minutes": False} 3630 3631 SECONDS = Literal.string("seconds") 3632 MILLIS = Literal.string("millis") 3633 MICROS = Literal.string("micros")
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
Inherited Members
3656class XMLTable(Func): 3657 arg_types = {"this": True, "passing": False, "columns": False, "by_ref": False}
Inherited Members
Inherited Members
Inherited Members
3668class Merge(Expression): 3669 arg_types = {"this": True, "using": True, "on": True, "expressions": True}
Inherited Members
Inherited Members
3701def maybe_parse( 3702 sql_or_expression: str | Expression, 3703 *, 3704 into: t.Optional[IntoType] = None, 3705 dialect: DialectType = None, 3706 prefix: t.Optional[str] = None, 3707 copy: bool = False, 3708 **opts, 3709) -> Expression: 3710 """Gracefully handle a possible string or expression. 3711 3712 Example: 3713 >>> maybe_parse("1") 3714 (LITERAL this: 1, is_string: False) 3715 >>> maybe_parse(to_identifier("x")) 3716 (IDENTIFIER this: x, quoted: False) 3717 3718 Args: 3719 sql_or_expression: the SQL code string or an expression 3720 into: the SQLGlot Expression to parse into 3721 dialect: the dialect used to parse the input expressions (in the case that an 3722 input expression is a SQL string). 3723 prefix: a string to prefix the sql with before it gets parsed 3724 (automatically includes a space) 3725 copy: whether or not to copy the expression. 3726 **opts: other options to use to parse the input expressions (again, in the case 3727 that an input expression is a SQL string). 3728 3729 Returns: 3730 Expression: the parsed or given expression. 3731 """ 3732 if isinstance(sql_or_expression, Expression): 3733 if copy: 3734 return sql_or_expression.copy() 3735 return sql_or_expression 3736 3737 import sqlglot 3738 3739 sql = str(sql_or_expression) 3740 if prefix: 3741 sql = f"{prefix} {sql}" 3742 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.
3888def union(left, right, distinct=True, dialect=None, **opts): 3889 """ 3890 Initializes a syntax tree from one UNION expression. 3891 3892 Example: 3893 >>> union("SELECT * FROM foo", "SELECT * FROM bla").sql() 3894 'SELECT * FROM foo UNION SELECT * FROM bla' 3895 3896 Args: 3897 left (str | Expression): the SQL code string corresponding to the left-hand side. 3898 If an `Expression` instance is passed, it will be used as-is. 3899 right (str | Expression): the SQL code string corresponding to the right-hand side. 3900 If an `Expression` instance is passed, it will be used as-is. 3901 distinct (bool): set the DISTINCT flag if and only if this is true. 3902 dialect (str): the dialect used to parse the input expression. 3903 opts (kwargs): other options to use to parse the input expressions. 3904 Returns: 3905 Union: the syntax tree for the UNION expression. 3906 """ 3907 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 3908 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 3909 3910 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
Expression
instance is passed, it will be used as-is. - right (str | Expression): the SQL code string corresponding to the right-hand side.
If an
Expression
instance 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.
3913def intersect(left, right, distinct=True, dialect=None, **opts): 3914 """ 3915 Initializes a syntax tree from one INTERSECT expression. 3916 3917 Example: 3918 >>> intersect("SELECT * FROM foo", "SELECT * FROM bla").sql() 3919 'SELECT * FROM foo INTERSECT SELECT * FROM bla' 3920 3921 Args: 3922 left (str | Expression): the SQL code string corresponding to the left-hand side. 3923 If an `Expression` instance is passed, it will be used as-is. 3924 right (str | Expression): the SQL code string corresponding to the right-hand side. 3925 If an `Expression` instance is passed, it will be used as-is. 3926 distinct (bool): set the DISTINCT flag if and only if this is true. 3927 dialect (str): the dialect used to parse the input expression. 3928 opts (kwargs): other options to use to parse the input expressions. 3929 Returns: 3930 Intersect: the syntax tree for the INTERSECT expression. 3931 """ 3932 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 3933 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 3934 3935 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
Expression
instance is passed, it will be used as-is. - right (str | Expression): the SQL code string corresponding to the right-hand side.
If an
Expression
instance 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.
3938def except_(left, right, distinct=True, dialect=None, **opts): 3939 """ 3940 Initializes a syntax tree from one EXCEPT expression. 3941 3942 Example: 3943 >>> except_("SELECT * FROM foo", "SELECT * FROM bla").sql() 3944 'SELECT * FROM foo EXCEPT SELECT * FROM bla' 3945 3946 Args: 3947 left (str | Expression): the SQL code string corresponding to the left-hand side. 3948 If an `Expression` instance is passed, it will be used as-is. 3949 right (str | Expression): the SQL code string corresponding to the right-hand side. 3950 If an `Expression` instance is passed, it will be used as-is. 3951 distinct (bool): set the DISTINCT flag if and only if this is true. 3952 dialect (str): the dialect used to parse the input expression. 3953 opts (kwargs): other options to use to parse the input expressions. 3954 Returns: 3955 Except: the syntax tree for the EXCEPT statement. 3956 """ 3957 left = maybe_parse(sql_or_expression=left, dialect=dialect, **opts) 3958 right = maybe_parse(sql_or_expression=right, dialect=dialect, **opts) 3959 3960 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
Expression
instance is passed, it will be used as-is. - right (str | Expression): the SQL code string corresponding to the right-hand side.
If an
Expression
instance 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.
3963def select(*expressions: str | Expression, dialect: DialectType = None, **opts) -> Select: 3964 """ 3965 Initializes a syntax tree from one or multiple SELECT expressions. 3966 3967 Example: 3968 >>> select("col1", "col2").from_("tbl").sql() 3969 'SELECT col1, col2 FROM tbl' 3970 3971 Args: 3972 *expressions: the SQL code string to parse as the expressions of a 3973 SELECT statement. If an Expression instance is passed, this is used as-is. 3974 dialect: the dialect used to parse the input expressions (in the case that an 3975 input expression is a SQL string). 3976 **opts: other options to use to parse the input expressions (again, in the case 3977 that an input expression is a SQL string). 3978 3979 Returns: 3980 Select: the syntax tree for the SELECT statement. 3981 """ 3982 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.
3985def from_(*expressions, dialect=None, **opts) -> Select: 3986 """ 3987 Initializes a syntax tree from a FROM expression. 3988 3989 Example: 3990 >>> from_("tbl").select("col1", "col2").sql() 3991 'SELECT col1, col2 FROM tbl' 3992 3993 Args: 3994 *expressions (str | Expression): the SQL code string to parse as the FROM expressions of a 3995 SELECT statement. If an Expression instance is passed, this is used as-is. 3996 dialect (str): the dialect used to parse the input expression (in the case that the 3997 input expression is a SQL string). 3998 **opts: other options to use to parse the input expressions (again, in the case 3999 that the input expression is a SQL string). 4000 4001 Returns: 4002 Select: the syntax tree for the SELECT statement. 4003 """ 4004 return Select().from_(*expressions, dialect=dialect, **opts)
Initializes a syntax tree from a FROM expression.
Example:
>>> from_("tbl").select("col1", "col2").sql() 'SELECT col1, col2 FROM tbl'
Arguments:
- *expressions (str | 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 (str): 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.
4007def update(table, properties, where=None, from_=None, dialect=None, **opts) -> Update: 4008 """ 4009 Creates an update statement. 4010 4011 Example: 4012 >>> update("my_table", {"x": 1, "y": "2", "z": None}, from_="baz", where="id > 1").sql() 4013 "UPDATE my_table SET x = 1, y = '2', z = NULL FROM baz WHERE id > 1" 4014 4015 Args: 4016 *properties (Dict[str, Any]): dictionary of properties to set which are 4017 auto converted to sql objects eg None -> NULL 4018 where (str): sql conditional parsed into a WHERE statement 4019 from_ (str): sql statement parsed into a FROM statement 4020 dialect (str): the dialect used to parse the input expressions. 4021 **opts: other options to use to parse the input expressions. 4022 4023 Returns: 4024 Update: the syntax tree for the UPDATE statement. 4025 """ 4026 update = Update(this=maybe_parse(table, into=Table, dialect=dialect)) 4027 update.set( 4028 "expressions", 4029 [ 4030 EQ(this=maybe_parse(k, dialect=dialect, **opts), expression=convert(v)) 4031 for k, v in properties.items() 4032 ], 4033 ) 4034 if from_: 4035 update.set( 4036 "from", 4037 maybe_parse(from_, into=From, dialect=dialect, prefix="FROM", **opts), 4038 ) 4039 if isinstance(where, Condition): 4040 where = Where(this=where) 4041 if where: 4042 update.set( 4043 "where", 4044 maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts), 4045 ) 4046 return update
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 (Dict[str, Any]): dictionary of properties to set which are auto converted to sql objects eg None -> NULL
- where (str): sql conditional parsed into a WHERE statement
- from_ (str): sql statement parsed into a FROM statement
- dialect (str): 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.
4049def delete(table, where=None, dialect=None, **opts) -> Delete: 4050 """ 4051 Builds a delete statement. 4052 4053 Example: 4054 >>> delete("my_table", where="id > 1").sql() 4055 'DELETE FROM my_table WHERE id > 1' 4056 4057 Args: 4058 where (str|Condition): sql conditional parsed into a WHERE statement 4059 dialect (str): the dialect used to parse the input expressions. 4060 **opts: other options to use to parse the input expressions. 4061 4062 Returns: 4063 Delete: the syntax tree for the DELETE statement. 4064 """ 4065 return Delete( 4066 this=maybe_parse(table, into=Table, dialect=dialect, **opts), 4067 where=Where(this=where) 4068 if isinstance(where, Condition) 4069 else maybe_parse(where, into=Where, dialect=dialect, prefix="WHERE", **opts), 4070 )
Builds a delete statement.
Example:
>>> delete("my_table", where="id > 1").sql() 'DELETE FROM my_table WHERE id > 1'
Arguments:
- where (str|Condition): sql conditional parsed into a WHERE statement
- dialect (str): 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.
4073def condition(expression, dialect=None, **opts) -> Condition: 4074 """ 4075 Initialize a logical condition expression. 4076 4077 Example: 4078 >>> condition("x=1").sql() 4079 'x = 1' 4080 4081 This is helpful for composing larger logical syntax trees: 4082 >>> where = condition("x=1") 4083 >>> where = where.and_("y=1") 4084 >>> Select().from_("tbl").select("*").where(where).sql() 4085 'SELECT * FROM tbl WHERE x = 1 AND y = 1' 4086 4087 Args: 4088 *expression (str | Expression): the SQL code string to parse. 4089 If an Expression instance is passed, this is used as-is. 4090 dialect (str): the dialect used to parse the input expression (in the case that the 4091 input expression is a SQL string). 4092 **opts: other options to use to parse the input expressions (again, in the case 4093 that the input expression is a SQL string). 4094 4095 Returns: 4096 Condition: the expression 4097 """ 4098 return maybe_parse( # type: ignore 4099 expression, 4100 into=Condition, 4101 dialect=dialect, 4102 **opts, 4103 )
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).
- **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
4106def and_(*expressions, dialect=None, **opts) -> And: 4107 """ 4108 Combine multiple conditions with an AND logical operator. 4109 4110 Example: 4111 >>> and_("x=1", and_("y=1", "z=1")).sql() 4112 'x = 1 AND (y = 1 AND z = 1)' 4113 4114 Args: 4115 *expressions (str | Expression): the SQL code strings to parse. 4116 If an Expression instance is passed, this is used as-is. 4117 dialect (str): the dialect used to parse the input expression. 4118 **opts: other options to use to parse the input expressions. 4119 4120 Returns: 4121 And: the new condition 4122 """ 4123 return _combine(expressions, And, dialect, **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.
- **opts: other options to use to parse the input expressions.
Returns:
And: the new condition
4126def or_(*expressions, dialect=None, **opts) -> Or: 4127 """ 4128 Combine multiple conditions with an OR logical operator. 4129 4130 Example: 4131 >>> or_("x=1", or_("y=1", "z=1")).sql() 4132 'x = 1 OR (y = 1 OR z = 1)' 4133 4134 Args: 4135 *expressions (str | Expression): the SQL code strings to parse. 4136 If an Expression instance is passed, this is used as-is. 4137 dialect (str): the dialect used to parse the input expression. 4138 **opts: other options to use to parse the input expressions. 4139 4140 Returns: 4141 Or: the new condition 4142 """ 4143 return _combine(expressions, Or, dialect, **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.
- **opts: other options to use to parse the input expressions.
Returns:
Or: the new condition
4146def not_(expression, dialect=None, **opts) -> Not: 4147 """ 4148 Wrap a condition with a NOT operator. 4149 4150 Example: 4151 >>> not_("this_suit='black'").sql() 4152 "NOT this_suit = 'black'" 4153 4154 Args: 4155 expression (str | Expression): the SQL code strings to parse. 4156 If an Expression instance is passed, this is used as-is. 4157 dialect (str): the dialect used to parse the input expression. 4158 **opts: other options to use to parse the input expressions. 4159 4160 Returns: 4161 Not: the new condition 4162 """ 4163 this = condition( 4164 expression, 4165 dialect=dialect, 4166 **opts, 4167 ) 4168 return Not(this=_wrap_operator(this))
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
4188def to_identifier(name, quoted=None): 4189 """Builds an identifier. 4190 4191 Args: 4192 name: The name to turn into an identifier. 4193 quoted: Whether or not force quote the identifier. 4194 4195 Returns: 4196 The identifier ast node. 4197 """ 4198 4199 if name is None: 4200 return None 4201 4202 if isinstance(name, Identifier): 4203 identifier = name 4204 elif isinstance(name, str): 4205 identifier = Identifier( 4206 this=name, 4207 quoted=not re.match(SAFE_IDENTIFIER_RE, name) if quoted is None else quoted, 4208 ) 4209 else: 4210 raise ValueError(f"Name needs to be a string or an Identifier, got: {name.__class__}") 4211 return identifier
Builds an identifier.
Arguments:
- name: The name to turn into an identifier.
- quoted: Whether or not force quote the identifier.
Returns:
The identifier ast node.
4217def to_interval(interval: str | Literal) -> Interval: 4218 """Builds an interval expression from a string like '1 day' or '5 months'.""" 4219 if isinstance(interval, Literal): 4220 if not interval.is_string: 4221 raise ValueError("Invalid interval string.") 4222 4223 interval = interval.this 4224 4225 interval_parts = INTERVAL_STRING_RE.match(interval) # type: ignore 4226 4227 if not interval_parts: 4228 raise ValueError("Invalid interval string.") 4229 4230 return Interval( 4231 this=Literal.string(interval_parts.group(1)), 4232 unit=Var(this=interval_parts.group(2)), 4233 )
Builds an interval expression from a string like '1 day' or '5 months'.
4246def to_table(sql_path: t.Optional[str | Table], **kwargs) -> t.Optional[Table]: 4247 """ 4248 Create a table expression from a `[catalog].[schema].[table]` sql path. Catalog and schema are optional. 4249 If a table is passed in then that table is returned. 4250 4251 Args: 4252 sql_path: a `[catalog].[schema].[table]` string. 4253 4254 Returns: 4255 A table expression. 4256 """ 4257 if sql_path is None or isinstance(sql_path, Table): 4258 return sql_path 4259 if not isinstance(sql_path, str): 4260 raise ValueError(f"Invalid type provided for a table: {type(sql_path)}") 4261 4262 catalog, db, table_name = (to_identifier(x) for x in split_num_words(sql_path, ".", 3)) 4263 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.
4266def to_column(sql_path: str | Column, **kwargs) -> Column: 4267 """ 4268 Create a column from a `[table].[column]` sql path. Schema is optional. 4269 4270 If a column is passed in then that column is returned. 4271 4272 Args: 4273 sql_path: `[table].[column]` string 4274 Returns: 4275 Table: A column expression 4276 """ 4277 if sql_path is None or isinstance(sql_path, Column): 4278 return sql_path 4279 if not isinstance(sql_path, str): 4280 raise ValueError(f"Invalid type provided for column: {type(sql_path)}") 4281 table_name, column_name = (to_identifier(x) for x in split_num_words(sql_path, ".", 2)) 4282 return Column(this=column_name, table=table_name, **kwargs)
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
4285def alias_( 4286 expression: str | Expression, 4287 alias: str | Identifier, 4288 table: bool | t.Sequence[str | Identifier] = False, 4289 quoted: t.Optional[bool] = None, 4290 dialect: DialectType = None, 4291 **opts, 4292): 4293 """Create an Alias expression. 4294 4295 Example: 4296 >>> alias_('foo', 'bar').sql() 4297 'foo AS bar' 4298 4299 >>> alias_('(select 1, 2)', 'bar', table=['a', 'b']).sql() 4300 '(SELECT 1, 2) AS bar(a, b)' 4301 4302 Args: 4303 expression: the SQL code strings to parse. 4304 If an Expression instance is passed, this is used as-is. 4305 alias: the alias name to use. If the name has 4306 special characters it is quoted. 4307 table: Whether or not to create a table alias, can also be a list of columns. 4308 quoted: whether or not to quote the alias 4309 dialect: the dialect used to parse the input expression. 4310 **opts: other options to use to parse the input expressions. 4311 4312 Returns: 4313 Alias: the aliased expression 4314 """ 4315 exp = maybe_parse(expression, dialect=dialect, **opts) 4316 alias = to_identifier(alias, quoted=quoted) 4317 4318 if table: 4319 table_alias = TableAlias(this=alias) 4320 exp.set("alias", table_alias) 4321 4322 if not isinstance(table, bool): 4323 for column in table: 4324 table_alias.append("columns", to_identifier(column, quoted=quoted)) 4325 4326 return exp 4327 4328 # We don't set the "alias" arg for Window expressions, because that would add an IDENTIFIER node in 4329 # the AST, representing a "named_window" [1] construct (eg. bigquery). What we want is an ALIAS node 4330 # for the complete Window expression. 4331 # 4332 # [1]: https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls 4333 4334 if "alias" in exp.arg_types and not isinstance(exp, Window): 4335 exp = exp.copy() 4336 exp.set("alias", alias) 4337 return exp 4338 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.
- **opts: other options to use to parse the input expressions.
Returns:
Alias: the aliased expression
4341def subquery(expression, alias=None, dialect=None, **opts): 4342 """ 4343 Build a subquery expression. 4344 4345 Example: 4346 >>> subquery('select x from tbl', 'bar').select('x').sql() 4347 'SELECT x FROM (SELECT x FROM tbl) AS bar' 4348 4349 Args: 4350 expression (str | Expression): the SQL code strings to parse. 4351 If an Expression instance is passed, this is used as-is. 4352 alias (str | Expression): the alias name to use. 4353 dialect (str): the dialect used to parse the input expression. 4354 **opts: other options to use to parse the input expressions. 4355 4356 Returns: 4357 Select: a new select with the subquery expression included 4358 """ 4359 4360 expression = maybe_parse(expression, dialect=dialect, **opts).subquery(alias) 4361 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
4364def column( 4365 col: str | Identifier, 4366 table: t.Optional[str | Identifier] = None, 4367 schema: t.Optional[str | Identifier] = None, 4368 quoted: t.Optional[bool] = None, 4369) -> Column: 4370 """ 4371 Build a Column. 4372 4373 Args: 4374 col: column name 4375 table: table name 4376 schema: schema name 4377 quoted: whether or not to force quote each part 4378 Returns: 4379 Column: column instance 4380 """ 4381 return Column( 4382 this=to_identifier(col, quoted=quoted), 4383 table=to_identifier(table, quoted=quoted), 4384 schema=to_identifier(schema, quoted=quoted), 4385 )
Build a Column.
Arguments:
- col: column name
- table: table name
- schema: schema name
- quoted: whether or not to force quote each part
Returns:
Column: column instance
4388def cast(expression: str | Expression, to: str | DataType | DataType.Type, **opts) -> Cast: 4389 """Cast an expression to a data type. 4390 4391 Example: 4392 >>> cast('x + 1', 'int').sql() 4393 'CAST(x + 1 AS INT)' 4394 4395 Args: 4396 expression: The expression to cast. 4397 to: The datatype to cast to. 4398 4399 Returns: 4400 A cast node. 4401 """ 4402 expression = maybe_parse(expression, **opts) 4403 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.
4406def table_(table, db=None, catalog=None, quoted=None, alias=None) -> Table: 4407 """Build a Table. 4408 4409 Args: 4410 table (str | Expression): column name 4411 db (str | Expression): db name 4412 catalog (str | Expression): catalog name 4413 4414 Returns: 4415 Table: table instance 4416 """ 4417 return Table( 4418 this=to_identifier(table, quoted=quoted), 4419 db=to_identifier(db, quoted=quoted), 4420 catalog=to_identifier(catalog, quoted=quoted), 4421 alias=TableAlias(this=to_identifier(alias)) if alias else None, 4422 )
Build a Table.
Arguments:
- table (str | Expression): column name
- db (str | Expression): db name
- catalog (str | Expression): catalog name
Returns:
Table: table instance
4425def values( 4426 values: t.Iterable[t.Tuple[t.Any, ...]], 4427 alias: t.Optional[str] = None, 4428 columns: t.Optional[t.Iterable[str] | t.Dict[str, DataType]] = None, 4429) -> Values: 4430 """Build VALUES statement. 4431 4432 Example: 4433 >>> values([(1, '2')]).sql() 4434 "VALUES (1, '2')" 4435 4436 Args: 4437 values: values statements that will be converted to SQL 4438 alias: optional alias 4439 columns: Optional list of ordered column names or ordered dictionary of column names to types. 4440 If either are provided then an alias is also required. 4441 If a dictionary is provided then the first column of the values will be casted to the expected type 4442 in order to help with type inference. 4443 4444 Returns: 4445 Values: the Values expression object 4446 """ 4447 if columns and not alias: 4448 raise ValueError("Alias is required when providing columns") 4449 table_alias = ( 4450 TableAlias(this=to_identifier(alias), columns=[to_identifier(x) for x in columns]) 4451 if columns 4452 else TableAlias(this=to_identifier(alias) if alias else None) 4453 ) 4454 expressions = [convert(tup) for tup in values] 4455 if columns and isinstance(columns, dict): 4456 types = list(columns.values()) 4457 expressions[0].set( 4458 "expressions", 4459 [cast(x, types[i]) for i, x in enumerate(expressions[0].expressions)], 4460 ) 4461 return Values( 4462 expressions=expressions, 4463 alias=table_alias, 4464 )
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. If a dictionary is provided then the first column of the values will be casted to the expected type in order to help with type inference.
Returns:
Values: the Values expression object
4467def var(name: t.Optional[str | Expression]) -> Var: 4468 """Build a SQL variable. 4469 4470 Example: 4471 >>> repr(var('x')) 4472 '(VAR this: x)' 4473 4474 >>> repr(var(column('x', table='y'))) 4475 '(VAR this: x)' 4476 4477 Args: 4478 name: The name of the var or an expression who's name will become the var. 4479 4480 Returns: 4481 The new variable node. 4482 """ 4483 if not name: 4484 raise ValueError(f"Cannot convert empty name into var.") 4485 4486 if isinstance(name, Expression): 4487 name = name.name 4488 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.
4491def rename_table(old_name: str | Table, new_name: str | Table) -> AlterTable: 4492 """Build ALTER TABLE... RENAME... expression 4493 4494 Args: 4495 old_name: The old name of the table 4496 new_name: The new name of the table 4497 4498 Returns: 4499 Alter table expression 4500 """ 4501 old_table = to_table(old_name) 4502 new_table = to_table(new_name) 4503 return AlterTable( 4504 this=old_table, 4505 actions=[ 4506 RenameTable(this=new_table), 4507 ], 4508 )
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
4511def convert(value) -> Expression: 4512 """Convert a python value into an expression object. 4513 4514 Raises an error if a conversion is not possible. 4515 4516 Args: 4517 value (Any): a python object 4518 4519 Returns: 4520 Expression: the equivalent expression object 4521 """ 4522 if isinstance(value, Expression): 4523 return value 4524 if value is None: 4525 return NULL 4526 if isinstance(value, bool): 4527 return Boolean(this=value) 4528 if isinstance(value, str): 4529 return Literal.string(value) 4530 if isinstance(value, float) and math.isnan(value): 4531 return NULL 4532 if isinstance(value, numbers.Number): 4533 return Literal.number(value) 4534 if isinstance(value, tuple): 4535 return Tuple(expressions=[convert(v) for v in value]) 4536 if isinstance(value, list): 4537 return Array(expressions=[convert(v) for v in value]) 4538 if isinstance(value, dict): 4539 return Map( 4540 keys=[convert(k) for k in value], 4541 values=[convert(v) for v in value.values()], 4542 ) 4543 if isinstance(value, datetime.datetime): 4544 datetime_literal = Literal.string( 4545 (value if value.tzinfo else value.replace(tzinfo=datetime.timezone.utc)).isoformat() 4546 ) 4547 return TimeStrToTime(this=datetime_literal) 4548 if isinstance(value, datetime.date): 4549 date_literal = Literal.string(value.strftime("%Y-%m-%d")) 4550 return DateStrToDate(this=date_literal) 4551 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 (Any): a python object
Returns:
Expression: the equivalent expression object
4554def replace_children(expression, fun): 4555 """ 4556 Replace children of an expression with the result of a lambda fun(child) -> exp. 4557 """ 4558 for k, v in expression.args.items(): 4559 is_list_arg = isinstance(v, list) 4560 4561 child_nodes = v if is_list_arg else [v] 4562 new_child_nodes = [] 4563 4564 for cn in child_nodes: 4565 if isinstance(cn, Expression): 4566 for child_node in ensure_collection(fun(cn)): 4567 new_child_nodes.append(child_node) 4568 child_node.parent = expression 4569 child_node.arg_key = k 4570 else: 4571 new_child_nodes.append(cn) 4572 4573 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.
4576def column_table_names(expression): 4577 """ 4578 Return all table names referenced through columns in an expression. 4579 4580 Example: 4581 >>> import sqlglot 4582 >>> column_table_names(sqlglot.parse_one("a.b AND c.d AND c.e")) 4583 ['c', 'a'] 4584 4585 Args: 4586 expression (sqlglot.Expression): expression to find table names 4587 4588 Returns: 4589 list: A list of unique names 4590 """ 4591 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
4594def table_name(table) -> str: 4595 """Get the full name of a table as a string. 4596 4597 Args: 4598 table (exp.Table | str): table expression node or string. 4599 4600 Examples: 4601 >>> from sqlglot import exp, parse_one 4602 >>> table_name(parse_one("select * from a.b.c").find(exp.Table)) 4603 'a.b.c' 4604 4605 Returns: 4606 The table name. 4607 """ 4608 4609 table = maybe_parse(table, into=Table) 4610 4611 if not table: 4612 raise ValueError(f"Cannot parse {table}") 4613 4614 return ".".join( 4615 part 4616 for part in ( 4617 table.text("catalog"), 4618 table.text("db"), 4619 table.name, 4620 ) 4621 if part 4622 )
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.
4625def replace_tables(expression, mapping): 4626 """Replace all tables in expression according to the mapping. 4627 4628 Args: 4629 expression (sqlglot.Expression): expression node to be transformed and replaced. 4630 mapping (Dict[str, str]): mapping of table names. 4631 4632 Examples: 4633 >>> from sqlglot import exp, parse_one 4634 >>> replace_tables(parse_one("select * from a.b"), {"a.b": "c"}).sql() 4635 'SELECT * FROM c' 4636 4637 Returns: 4638 The mapped expression. 4639 """ 4640 4641 def _replace_tables(node): 4642 if isinstance(node, Table): 4643 new_name = mapping.get(table_name(node)) 4644 if new_name: 4645 return to_table( 4646 new_name, 4647 **{k: v for k, v in node.args.items() if k not in ("this", "db", "catalog")}, 4648 ) 4649 return node 4650 4651 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.
4654def replace_placeholders(expression, *args, **kwargs): 4655 """Replace placeholders in an expression. 4656 4657 Args: 4658 expression (sqlglot.Expression): expression node to be transformed and replaced. 4659 args: positional names that will substitute unnamed placeholders in the given order. 4660 kwargs: keyword arguments that will substitute named placeholders. 4661 4662 Examples: 4663 >>> from sqlglot import exp, parse_one 4664 >>> replace_placeholders( 4665 ... parse_one("select * from :tbl where ? = ?"), "a", "b", tbl="foo" 4666 ... ).sql() 4667 'SELECT * FROM foo WHERE a = b' 4668 4669 Returns: 4670 The mapped expression. 4671 """ 4672 4673 def _replace_placeholders(node, args, **kwargs): 4674 if isinstance(node, Placeholder): 4675 if node.name: 4676 new_name = kwargs.get(node.name) 4677 if new_name: 4678 return to_identifier(new_name) 4679 else: 4680 try: 4681 return to_identifier(next(args)) 4682 except StopIteration: 4683 pass 4684 return node 4685 4686 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 ? = ?"), "a", "b", tbl="foo" ... ).sql() 'SELECT * FROM foo WHERE a = b'
Returns:
The mapped expression.
4689def expand(expression: Expression, sources: t.Dict[str, Subqueryable], copy=True) -> Expression: 4690 """Transforms an expression by expanding all referenced sources into subqueries. 4691 4692 Examples: 4693 >>> from sqlglot import parse_one 4694 >>> expand(parse_one("select * from x AS z"), {"x": parse_one("select * from y")}).sql() 4695 'SELECT * FROM (SELECT * FROM y) AS z /* source: x */' 4696 4697 Args: 4698 expression: The expression to expand. 4699 sources: A dictionary of name to Subqueryables. 4700 copy: Whether or not to copy the expression during transformation. Defaults to True. 4701 4702 Returns: 4703 The transformed expression. 4704 """ 4705 4706 def _expand(node: Expression): 4707 if isinstance(node, Table): 4708 name = table_name(node) 4709 source = sources.get(name) 4710 if source: 4711 subquery = source.subquery(node.alias or name) 4712 subquery.comments = [f"source: {name}"] 4713 return subquery 4714 return node 4715 4716 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 */'
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.
4719def func(name: str, *args, dialect: DialectType = None, **kwargs) -> Func: 4720 """ 4721 Returns a Func expression. 4722 4723 Examples: 4724 >>> func("abs", 5).sql() 4725 'ABS(5)' 4726 4727 >>> func("cast", this=5, to=DataType.build("DOUBLE")).sql() 4728 'CAST(5 AS DOUBLE)' 4729 4730 Args: 4731 name: the name of the function to build. 4732 args: the args used to instantiate the function of interest. 4733 dialect: the source dialect. 4734 kwargs: the kwargs used to instantiate the function of interest. 4735 4736 Note: 4737 The arguments `args` and `kwargs` are mutually exclusive. 4738 4739 Returns: 4740 An instance of the function of interest, or an anonymous function, if `name` doesn't 4741 correspond to an existing `sqlglot.expressions.Func` class. 4742 """ 4743 if args and kwargs: 4744 raise ValueError("Can't use both args and kwargs to instantiate a function.") 4745 4746 from sqlglot.dialects.dialect import Dialect 4747 4748 args = tuple(convert(arg) for arg in args) 4749 kwargs = {key: convert(value) for key, value in kwargs.items()} 4750 4751 parser = Dialect.get_or_raise(dialect)().parser() 4752 from_args_list = parser.FUNCTIONS.get(name.upper()) 4753 4754 if from_args_list: 4755 function = from_args_list(args) if args else from_args_list.__self__(**kwargs) # type: ignore 4756 else: 4757 kwargs = kwargs or {"expressions": args} 4758 function = Anonymous(this=name, **kwargs) 4759 4760 for error_message in function.error_messages(args): 4761 raise ValueError(error_message) 4762 4763 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
args
andkwargs
are mutually exclusive.
Returns:
An instance of the function of interest, or an anonymous function, if
name
doesn't correspond to an existingsqlglot.expressions.Func
class.
4766def true(): 4767 """ 4768 Returns a true Boolean expression. 4769 """ 4770 return Boolean(this=True)
Returns a true Boolean expression.
4773def false(): 4774 """ 4775 Returns a false Boolean expression. 4776 """ 4777 return Boolean(this=False)
Returns a false Boolean expression.
Returns a Null expression.