functions.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  1. # sql/functions.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """SQL function API, factories, and built-in functions.
  8. """
  9. from __future__ import annotations
  10. import datetime
  11. import decimal
  12. from typing import Any
  13. from typing import cast
  14. from typing import Dict
  15. from typing import List
  16. from typing import Mapping
  17. from typing import Optional
  18. from typing import overload
  19. from typing import Sequence
  20. from typing import Tuple
  21. from typing import Type
  22. from typing import TYPE_CHECKING
  23. from typing import TypeVar
  24. from typing import Union
  25. from . import annotation
  26. from . import coercions
  27. from . import operators
  28. from . import roles
  29. from . import schema
  30. from . import sqltypes
  31. from . import type_api
  32. from . import util as sqlutil
  33. from ._typing import is_table_value_type
  34. from .base import _entity_namespace
  35. from .base import ColumnCollection
  36. from .base import Executable
  37. from .base import Generative
  38. from .base import HasMemoized
  39. from .elements import _type_from_args
  40. from .elements import BinaryExpression
  41. from .elements import BindParameter
  42. from .elements import Cast
  43. from .elements import ClauseList
  44. from .elements import ColumnElement
  45. from .elements import Extract
  46. from .elements import FunctionFilter
  47. from .elements import Grouping
  48. from .elements import literal_column
  49. from .elements import NamedColumn
  50. from .elements import Over
  51. from .elements import WithinGroup
  52. from .selectable import FromClause
  53. from .selectable import Select
  54. from .selectable import TableValuedAlias
  55. from .sqltypes import TableValueType
  56. from .type_api import TypeEngine
  57. from .visitors import InternalTraversal
  58. from .. import util
  59. if TYPE_CHECKING:
  60. from ._typing import _ByArgument
  61. from ._typing import _ColumnExpressionArgument
  62. from ._typing import _ColumnExpressionOrLiteralArgument
  63. from ._typing import _TypeEngineArgument
  64. from .base import _EntityNamespace
  65. from .elements import ClauseElement
  66. from .elements import KeyedColumnElement
  67. from .elements import TableValuedColumn
  68. from .operators import OperatorType
  69. from ..engine.base import Connection
  70. from ..engine.cursor import CursorResult
  71. from ..engine.interfaces import _CoreMultiExecuteParams
  72. from ..engine.interfaces import CoreExecuteOptionsParameter
  73. from ..util.typing import Self
  74. _T = TypeVar("_T", bound=Any)
  75. _S = TypeVar("_S", bound=Any)
  76. _registry: util.defaultdict[
  77. str, Dict[str, Type[Function[Any]]]
  78. ] = util.defaultdict(dict)
  79. def register_function(
  80. identifier: str, fn: Type[Function[Any]], package: str = "_default"
  81. ) -> None:
  82. """Associate a callable with a particular func. name.
  83. This is normally called by GenericFunction, but is also
  84. available by itself so that a non-Function construct
  85. can be associated with the :data:`.func` accessor (i.e.
  86. CAST, EXTRACT).
  87. """
  88. reg = _registry[package]
  89. identifier = str(identifier).lower()
  90. # Check if a function with the same identifier is registered.
  91. if identifier in reg:
  92. util.warn(
  93. "The GenericFunction '{}' is already registered and "
  94. "is going to be overridden.".format(identifier)
  95. )
  96. reg[identifier] = fn
  97. class FunctionElement(Executable, ColumnElement[_T], FromClause, Generative):
  98. """Base for SQL function-oriented constructs.
  99. This is a `generic type <https://peps.python.org/pep-0484/#generics>`_,
  100. meaning that type checkers and IDEs can be instructed on the types to
  101. expect in a :class:`_engine.Result` for this function. See
  102. :class:`.GenericFunction` for an example of how this is done.
  103. .. seealso::
  104. :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
  105. :class:`.Function` - named SQL function.
  106. :data:`.func` - namespace which produces registered or ad-hoc
  107. :class:`.Function` instances.
  108. :class:`.GenericFunction` - allows creation of registered function
  109. types.
  110. """
  111. _traverse_internals = [
  112. ("clause_expr", InternalTraversal.dp_clauseelement),
  113. ("_with_ordinality", InternalTraversal.dp_boolean),
  114. ("_table_value_type", InternalTraversal.dp_has_cache_key),
  115. ]
  116. packagenames: Tuple[str, ...] = ()
  117. _has_args = False
  118. _with_ordinality = False
  119. _table_value_type: Optional[TableValueType] = None
  120. # some attributes that are defined between both ColumnElement and
  121. # FromClause are set to Any here to avoid typing errors
  122. primary_key: Any
  123. _is_clone_of: Any
  124. clause_expr: Grouping[Any]
  125. def __init__(self, *clauses: _ColumnExpressionOrLiteralArgument[Any]):
  126. r"""Construct a :class:`.FunctionElement`.
  127. :param \*clauses: list of column expressions that form the arguments
  128. of the SQL function call.
  129. :param \**kwargs: additional kwargs are typically consumed by
  130. subclasses.
  131. .. seealso::
  132. :data:`.func`
  133. :class:`.Function`
  134. """
  135. args: Sequence[_ColumnExpressionArgument[Any]] = [
  136. coercions.expect(
  137. roles.ExpressionElementRole,
  138. c,
  139. name=getattr(self, "name", None),
  140. apply_propagate_attrs=self,
  141. )
  142. for c in clauses
  143. ]
  144. self._has_args = self._has_args or bool(args)
  145. self.clause_expr = Grouping(
  146. ClauseList(operator=operators.comma_op, group_contents=True, *args)
  147. )
  148. _non_anon_label = None
  149. @property
  150. def _proxy_key(self) -> Any:
  151. return super()._proxy_key or getattr(self, "name", None)
  152. def _execute_on_connection(
  153. self,
  154. connection: Connection,
  155. distilled_params: _CoreMultiExecuteParams,
  156. execution_options: CoreExecuteOptionsParameter,
  157. ) -> CursorResult[Any]:
  158. return connection._execute_function(
  159. self, distilled_params, execution_options
  160. )
  161. def scalar_table_valued(
  162. self, name: str, type_: Optional[_TypeEngineArgument[_T]] = None
  163. ) -> ScalarFunctionColumn[_T]:
  164. """Return a column expression that's against this
  165. :class:`_functions.FunctionElement` as a scalar
  166. table-valued expression.
  167. The returned expression is similar to that returned by a single column
  168. accessed off of a :meth:`_functions.FunctionElement.table_valued`
  169. construct, except no FROM clause is generated; the function is rendered
  170. in the similar way as a scalar subquery.
  171. E.g.:
  172. .. sourcecode:: pycon+sql
  173. >>> from sqlalchemy import func, select
  174. >>> fn = func.jsonb_each("{'k', 'v'}").scalar_table_valued("key")
  175. >>> print(select(fn))
  176. {printsql}SELECT (jsonb_each(:jsonb_each_1)).key
  177. .. versionadded:: 1.4.0b2
  178. .. seealso::
  179. :meth:`_functions.FunctionElement.table_valued`
  180. :meth:`_functions.FunctionElement.alias`
  181. :meth:`_functions.FunctionElement.column_valued`
  182. """ # noqa: E501
  183. return ScalarFunctionColumn(self, name, type_)
  184. def table_valued(
  185. self, *expr: _ColumnExpressionArgument[Any], **kw: Any
  186. ) -> TableValuedAlias:
  187. r"""Return a :class:`_sql.TableValuedAlias` representation of this
  188. :class:`_functions.FunctionElement` with table-valued expressions added.
  189. e.g.:
  190. .. sourcecode:: pycon+sql
  191. >>> fn = (
  192. ... func.generate_series(1, 5).
  193. ... table_valued("value", "start", "stop", "step")
  194. ... )
  195. >>> print(select(fn))
  196. {printsql}SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step
  197. FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1{stop}
  198. >>> print(select(fn.c.value, fn.c.stop).where(fn.c.value > 2))
  199. {printsql}SELECT anon_1.value, anon_1.stop
  200. FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1
  201. WHERE anon_1.value > :value_1{stop}
  202. A WITH ORDINALITY expression may be generated by passing the keyword
  203. argument "with_ordinality":
  204. .. sourcecode:: pycon+sql
  205. >>> fn = func.generate_series(4, 1, -1).table_valued("gen", with_ordinality="ordinality")
  206. >>> print(select(fn))
  207. {printsql}SELECT anon_1.gen, anon_1.ordinality
  208. FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) WITH ORDINALITY AS anon_1
  209. :param \*expr: A series of string column names that will be added to the
  210. ``.c`` collection of the resulting :class:`_sql.TableValuedAlias`
  211. construct as columns. :func:`_sql.column` objects with or without
  212. datatypes may also be used.
  213. :param name: optional name to assign to the alias name that's generated.
  214. If omitted, a unique anonymizing name is used.
  215. :param with_ordinality: string name that when present results in the
  216. ``WITH ORDINALITY`` clause being added to the alias, and the given
  217. string name will be added as a column to the .c collection
  218. of the resulting :class:`_sql.TableValuedAlias`.
  219. :param joins_implicitly: when True, the table valued function may be
  220. used in the FROM clause without any explicit JOIN to other tables
  221. in the SQL query, and no "cartesian product" warning will be generated.
  222. May be useful for SQL functions such as ``func.json_each()``.
  223. .. versionadded:: 1.4.33
  224. .. versionadded:: 1.4.0b2
  225. .. seealso::
  226. :ref:`tutorial_functions_table_valued` - in the :ref:`unified_tutorial`
  227. :ref:`postgresql_table_valued` - in the :ref:`postgresql_toplevel` documentation
  228. :meth:`_functions.FunctionElement.scalar_table_valued` - variant of
  229. :meth:`_functions.FunctionElement.table_valued` which delivers the
  230. complete table valued expression as a scalar column expression
  231. :meth:`_functions.FunctionElement.column_valued`
  232. :meth:`_sql.TableValuedAlias.render_derived` - renders the alias
  233. using a derived column clause, e.g. ``AS name(col1, col2, ...)``
  234. """ # noqa: 501
  235. new_func = self._generate()
  236. with_ordinality = kw.pop("with_ordinality", None)
  237. joins_implicitly = kw.pop("joins_implicitly", None)
  238. name = kw.pop("name", None)
  239. if with_ordinality:
  240. expr += (with_ordinality,)
  241. new_func._with_ordinality = True
  242. new_func.type = new_func._table_value_type = TableValueType(*expr)
  243. return new_func.alias(name=name, joins_implicitly=joins_implicitly)
  244. def column_valued(
  245. self, name: Optional[str] = None, joins_implicitly: bool = False
  246. ) -> TableValuedColumn[_T]:
  247. """Return this :class:`_functions.FunctionElement` as a column expression that
  248. selects from itself as a FROM clause.
  249. E.g.:
  250. .. sourcecode:: pycon+sql
  251. >>> from sqlalchemy import select, func
  252. >>> gs = func.generate_series(1, 5, -1).column_valued()
  253. >>> print(select(gs))
  254. {printsql}SELECT anon_1
  255. FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) AS anon_1
  256. This is shorthand for::
  257. gs = func.generate_series(1, 5, -1).alias().column
  258. :param name: optional name to assign to the alias name that's generated.
  259. If omitted, a unique anonymizing name is used.
  260. :param joins_implicitly: when True, the "table" portion of the column
  261. valued function may be a member of the FROM clause without any
  262. explicit JOIN to other tables in the SQL query, and no "cartesian
  263. product" warning will be generated. May be useful for SQL functions
  264. such as ``func.json_array_elements()``.
  265. .. versionadded:: 1.4.46
  266. .. seealso::
  267. :ref:`tutorial_functions_column_valued` - in the :ref:`unified_tutorial`
  268. :ref:`postgresql_column_valued` - in the :ref:`postgresql_toplevel` documentation
  269. :meth:`_functions.FunctionElement.table_valued`
  270. """ # noqa: 501
  271. return self.alias(name=name, joins_implicitly=joins_implicitly).column
  272. @util.ro_non_memoized_property
  273. def columns(self) -> ColumnCollection[str, KeyedColumnElement[Any]]: # type: ignore[override] # noqa: E501
  274. r"""The set of columns exported by this :class:`.FunctionElement`.
  275. This is a placeholder collection that allows the function to be
  276. placed in the FROM clause of a statement:
  277. .. sourcecode:: pycon+sql
  278. >>> from sqlalchemy import column, select, func
  279. >>> stmt = select(column('x'), column('y')).select_from(func.myfunction())
  280. >>> print(stmt)
  281. {printsql}SELECT x, y FROM myfunction()
  282. The above form is a legacy feature that is now superseded by the
  283. fully capable :meth:`_functions.FunctionElement.table_valued`
  284. method; see that method for details.
  285. .. seealso::
  286. :meth:`_functions.FunctionElement.table_valued` - generates table-valued
  287. SQL function expressions.
  288. """ # noqa: E501
  289. return self.c
  290. @util.ro_memoized_property
  291. def c(self) -> ColumnCollection[str, KeyedColumnElement[Any]]: # type: ignore[override] # noqa: E501
  292. """synonym for :attr:`.FunctionElement.columns`."""
  293. return ColumnCollection(
  294. columns=[(col.key, col) for col in self._all_selected_columns]
  295. )
  296. @property
  297. def _all_selected_columns(self) -> Sequence[KeyedColumnElement[Any]]:
  298. if is_table_value_type(self.type):
  299. # TODO: this might not be fully accurate
  300. cols = cast(
  301. "Sequence[KeyedColumnElement[Any]]", self.type._elements
  302. )
  303. else:
  304. cols = [self.label(None)]
  305. return cols
  306. @property
  307. def exported_columns( # type: ignore[override]
  308. self,
  309. ) -> ColumnCollection[str, KeyedColumnElement[Any]]:
  310. return self.columns
  311. @HasMemoized.memoized_attribute
  312. def clauses(self) -> ClauseList:
  313. """Return the underlying :class:`.ClauseList` which contains
  314. the arguments for this :class:`.FunctionElement`.
  315. """
  316. return cast(ClauseList, self.clause_expr.element)
  317. def over(
  318. self,
  319. *,
  320. partition_by: Optional[_ByArgument] = None,
  321. order_by: Optional[_ByArgument] = None,
  322. rows: Optional[Tuple[Optional[int], Optional[int]]] = None,
  323. range_: Optional[Tuple[Optional[int], Optional[int]]] = None,
  324. ) -> Over[_T]:
  325. """Produce an OVER clause against this function.
  326. Used against aggregate or so-called "window" functions,
  327. for database backends that support window functions.
  328. The expression::
  329. func.row_number().over(order_by='x')
  330. is shorthand for::
  331. from sqlalchemy import over
  332. over(func.row_number(), order_by='x')
  333. See :func:`_expression.over` for a full description.
  334. .. seealso::
  335. :func:`_expression.over`
  336. :ref:`tutorial_window_functions` - in the :ref:`unified_tutorial`
  337. """
  338. return Over(
  339. self,
  340. partition_by=partition_by,
  341. order_by=order_by,
  342. rows=rows,
  343. range_=range_,
  344. )
  345. def within_group(
  346. self, *order_by: _ColumnExpressionArgument[Any]
  347. ) -> WithinGroup[_T]:
  348. """Produce a WITHIN GROUP (ORDER BY expr) clause against this function.
  349. Used against so-called "ordered set aggregate" and "hypothetical
  350. set aggregate" functions, including :class:`.percentile_cont`,
  351. :class:`.rank`, :class:`.dense_rank`, etc.
  352. See :func:`_expression.within_group` for a full description.
  353. .. seealso::
  354. :ref:`tutorial_functions_within_group` -
  355. in the :ref:`unified_tutorial`
  356. """
  357. return WithinGroup(self, *order_by)
  358. @overload
  359. def filter(self) -> Self:
  360. ...
  361. @overload
  362. def filter(
  363. self,
  364. __criterion0: _ColumnExpressionArgument[bool],
  365. *criterion: _ColumnExpressionArgument[bool],
  366. ) -> FunctionFilter[_T]:
  367. ...
  368. def filter(
  369. self, *criterion: _ColumnExpressionArgument[bool]
  370. ) -> Union[Self, FunctionFilter[_T]]:
  371. """Produce a FILTER clause against this function.
  372. Used against aggregate and window functions,
  373. for database backends that support the "FILTER" clause.
  374. The expression::
  375. func.count(1).filter(True)
  376. is shorthand for::
  377. from sqlalchemy import funcfilter
  378. funcfilter(func.count(1), True)
  379. .. seealso::
  380. :ref:`tutorial_functions_within_group` -
  381. in the :ref:`unified_tutorial`
  382. :class:`.FunctionFilter`
  383. :func:`.funcfilter`
  384. """
  385. if not criterion:
  386. return self
  387. return FunctionFilter(self, *criterion)
  388. def as_comparison(
  389. self, left_index: int, right_index: int
  390. ) -> FunctionAsBinary:
  391. """Interpret this expression as a boolean comparison between two
  392. values.
  393. This method is used for an ORM use case described at
  394. :ref:`relationship_custom_operator_sql_function`.
  395. A hypothetical SQL function "is_equal()" which compares to values
  396. for equality would be written in the Core expression language as::
  397. expr = func.is_equal("a", "b")
  398. If "is_equal()" above is comparing "a" and "b" for equality, the
  399. :meth:`.FunctionElement.as_comparison` method would be invoked as::
  400. expr = func.is_equal("a", "b").as_comparison(1, 2)
  401. Where above, the integer value "1" refers to the first argument of the
  402. "is_equal()" function and the integer value "2" refers to the second.
  403. This would create a :class:`.BinaryExpression` that is equivalent to::
  404. BinaryExpression("a", "b", operator=op.eq)
  405. However, at the SQL level it would still render as
  406. "is_equal('a', 'b')".
  407. The ORM, when it loads a related object or collection, needs to be able
  408. to manipulate the "left" and "right" sides of the ON clause of a JOIN
  409. expression. The purpose of this method is to provide a SQL function
  410. construct that can also supply this information to the ORM, when used
  411. with the :paramref:`_orm.relationship.primaryjoin` parameter. The
  412. return value is a containment object called :class:`.FunctionAsBinary`.
  413. An ORM example is as follows::
  414. class Venue(Base):
  415. __tablename__ = 'venue'
  416. id = Column(Integer, primary_key=True)
  417. name = Column(String)
  418. descendants = relationship(
  419. "Venue",
  420. primaryjoin=func.instr(
  421. remote(foreign(name)), name + "/"
  422. ).as_comparison(1, 2) == 1,
  423. viewonly=True,
  424. order_by=name
  425. )
  426. Above, the "Venue" class can load descendant "Venue" objects by
  427. determining if the name of the parent Venue is contained within the
  428. start of the hypothetical descendant value's name, e.g. "parent1" would
  429. match up to "parent1/child1", but not to "parent2/child1".
  430. Possible use cases include the "materialized path" example given above,
  431. as well as making use of special SQL functions such as geometric
  432. functions to create join conditions.
  433. :param left_index: the integer 1-based index of the function argument
  434. that serves as the "left" side of the expression.
  435. :param right_index: the integer 1-based index of the function argument
  436. that serves as the "right" side of the expression.
  437. .. versionadded:: 1.3
  438. .. seealso::
  439. :ref:`relationship_custom_operator_sql_function` -
  440. example use within the ORM
  441. """
  442. return FunctionAsBinary(self, left_index, right_index)
  443. @property
  444. def _from_objects(self) -> Any:
  445. return self.clauses._from_objects
  446. def within_group_type(
  447. self, within_group: WithinGroup[_S]
  448. ) -> Optional[TypeEngine[_S]]:
  449. """For types that define their return type as based on the criteria
  450. within a WITHIN GROUP (ORDER BY) expression, called by the
  451. :class:`.WithinGroup` construct.
  452. Returns None by default, in which case the function's normal ``.type``
  453. is used.
  454. """
  455. return None
  456. def alias(
  457. self, name: Optional[str] = None, joins_implicitly: bool = False
  458. ) -> TableValuedAlias:
  459. r"""Produce a :class:`_expression.Alias` construct against this
  460. :class:`.FunctionElement`.
  461. .. tip::
  462. The :meth:`_functions.FunctionElement.alias` method is part of the
  463. mechanism by which "table valued" SQL functions are created.
  464. However, most use cases are covered by higher level methods on
  465. :class:`_functions.FunctionElement` including
  466. :meth:`_functions.FunctionElement.table_valued`, and
  467. :meth:`_functions.FunctionElement.column_valued`.
  468. This construct wraps the function in a named alias which
  469. is suitable for the FROM clause, in the style accepted for example
  470. by PostgreSQL. A column expression is also provided using the
  471. special ``.column`` attribute, which may
  472. be used to refer to the output of the function as a scalar value
  473. in the columns or where clause, for a backend such as PostgreSQL.
  474. For a full table-valued expression, use the
  475. :meth:`_functions.FunctionElement.table_valued` method first to
  476. establish named columns.
  477. e.g.:
  478. .. sourcecode:: pycon+sql
  479. >>> from sqlalchemy import func, select, column
  480. >>> data_view = func.unnest([1, 2, 3]).alias("data_view")
  481. >>> print(select(data_view.column))
  482. {printsql}SELECT data_view
  483. FROM unnest(:unnest_1) AS data_view
  484. The :meth:`_functions.FunctionElement.column_valued` method provides
  485. a shortcut for the above pattern:
  486. .. sourcecode:: pycon+sql
  487. >>> data_view = func.unnest([1, 2, 3]).column_valued("data_view")
  488. >>> print(select(data_view))
  489. {printsql}SELECT data_view
  490. FROM unnest(:unnest_1) AS data_view
  491. .. versionadded:: 1.4.0b2 Added the ``.column`` accessor
  492. :param name: alias name, will be rendered as ``AS <name>`` in the
  493. FROM clause
  494. :param joins_implicitly: when True, the table valued function may be
  495. used in the FROM clause without any explicit JOIN to other tables
  496. in the SQL query, and no "cartesian product" warning will be
  497. generated. May be useful for SQL functions such as
  498. ``func.json_each()``.
  499. .. versionadded:: 1.4.33
  500. .. seealso::
  501. :ref:`tutorial_functions_table_valued` -
  502. in the :ref:`unified_tutorial`
  503. :meth:`_functions.FunctionElement.table_valued`
  504. :meth:`_functions.FunctionElement.scalar_table_valued`
  505. :meth:`_functions.FunctionElement.column_valued`
  506. """
  507. return TableValuedAlias._construct(
  508. self,
  509. name=name,
  510. table_value_type=self.type,
  511. joins_implicitly=joins_implicitly,
  512. )
  513. def select(self) -> Select[Tuple[_T]]:
  514. """Produce a :func:`_expression.select` construct
  515. against this :class:`.FunctionElement`.
  516. This is shorthand for::
  517. s = select(function_element)
  518. """
  519. s: Select[Any] = Select(self)
  520. if self._execution_options:
  521. s = s.execution_options(**self._execution_options)
  522. return s
  523. def _bind_param(
  524. self,
  525. operator: OperatorType,
  526. obj: Any,
  527. type_: Optional[TypeEngine[_T]] = None,
  528. expanding: bool = False,
  529. **kw: Any,
  530. ) -> BindParameter[_T]:
  531. return BindParameter(
  532. None,
  533. obj,
  534. _compared_to_operator=operator,
  535. _compared_to_type=self.type,
  536. unique=True,
  537. type_=type_,
  538. expanding=expanding,
  539. **kw,
  540. )
  541. def self_group(self, against: Optional[OperatorType] = None) -> ClauseElement: # type: ignore[override] # noqa E501
  542. # for the moment, we are parenthesizing all array-returning
  543. # expressions against getitem. This may need to be made
  544. # more portable if in the future we support other DBs
  545. # besides postgresql.
  546. if against is operators.getitem and isinstance(
  547. self.type, sqltypes.ARRAY
  548. ):
  549. return Grouping(self)
  550. else:
  551. return super().self_group(against=against)
  552. @property
  553. def entity_namespace(self) -> _EntityNamespace:
  554. """overrides FromClause.entity_namespace as functions are generally
  555. column expressions and not FromClauses.
  556. """
  557. # ideally functions would not be fromclauses but we failed to make
  558. # this adjustment in 1.4
  559. return _entity_namespace(self.clause_expr)
  560. class FunctionAsBinary(BinaryExpression[Any]):
  561. _traverse_internals = [
  562. ("sql_function", InternalTraversal.dp_clauseelement),
  563. ("left_index", InternalTraversal.dp_plain_obj),
  564. ("right_index", InternalTraversal.dp_plain_obj),
  565. ("modifiers", InternalTraversal.dp_plain_dict),
  566. ]
  567. sql_function: FunctionElement[Any]
  568. left_index: int
  569. right_index: int
  570. def _gen_cache_key(self, anon_map: Any, bindparams: Any) -> Any:
  571. return ColumnElement._gen_cache_key(self, anon_map, bindparams)
  572. def __init__(
  573. self, fn: FunctionElement[Any], left_index: int, right_index: int
  574. ):
  575. self.sql_function = fn
  576. self.left_index = left_index
  577. self.right_index = right_index
  578. self.operator = operators.function_as_comparison_op
  579. self.type = sqltypes.BOOLEANTYPE
  580. self.negate = None
  581. self._is_implicitly_boolean = True
  582. self.modifiers = {}
  583. @property
  584. def left_expr(self) -> ColumnElement[Any]:
  585. return self.sql_function.clauses.clauses[self.left_index - 1]
  586. @left_expr.setter
  587. def left_expr(self, value: ColumnElement[Any]) -> None:
  588. self.sql_function.clauses.clauses[self.left_index - 1] = value
  589. @property
  590. def right_expr(self) -> ColumnElement[Any]:
  591. return self.sql_function.clauses.clauses[self.right_index - 1]
  592. @right_expr.setter
  593. def right_expr(self, value: ColumnElement[Any]) -> None:
  594. self.sql_function.clauses.clauses[self.right_index - 1] = value
  595. if not TYPE_CHECKING:
  596. # mypy can't accommodate @property to replace an instance
  597. # variable
  598. left = left_expr
  599. right = right_expr
  600. class ScalarFunctionColumn(NamedColumn[_T]):
  601. __visit_name__ = "scalar_function_column"
  602. _traverse_internals = [
  603. ("name", InternalTraversal.dp_anon_name),
  604. ("type", InternalTraversal.dp_type),
  605. ("fn", InternalTraversal.dp_clauseelement),
  606. ]
  607. is_literal = False
  608. table = None
  609. def __init__(
  610. self,
  611. fn: FunctionElement[_T],
  612. name: str,
  613. type_: Optional[_TypeEngineArgument[_T]] = None,
  614. ):
  615. self.fn = fn
  616. self.name = name
  617. # if type is None, we get NULLTYPE, which is our _T. But I don't
  618. # know how to get the overloads to express that correctly
  619. self.type = type_api.to_instance(type_) # type: ignore
  620. class _FunctionGenerator:
  621. """Generate SQL function expressions.
  622. :data:`.func` is a special object instance which generates SQL
  623. functions based on name-based attributes, e.g.:
  624. .. sourcecode:: pycon+sql
  625. >>> print(func.count(1))
  626. {printsql}count(:param_1)
  627. The returned object is an instance of :class:`.Function`, and is a
  628. column-oriented SQL element like any other, and is used in that way:
  629. .. sourcecode:: pycon+sql
  630. >>> print(select(func.count(table.c.id)))
  631. {printsql}SELECT count(sometable.id) FROM sometable
  632. Any name can be given to :data:`.func`. If the function name is unknown to
  633. SQLAlchemy, it will be rendered exactly as is. For common SQL functions
  634. which SQLAlchemy is aware of, the name may be interpreted as a *generic
  635. function* which will be compiled appropriately to the target database:
  636. .. sourcecode:: pycon+sql
  637. >>> print(func.current_timestamp())
  638. {printsql}CURRENT_TIMESTAMP
  639. To call functions which are present in dot-separated packages,
  640. specify them in the same manner:
  641. .. sourcecode:: pycon+sql
  642. >>> print(func.stats.yield_curve(5, 10))
  643. {printsql}stats.yield_curve(:yield_curve_1, :yield_curve_2)
  644. SQLAlchemy can be made aware of the return type of functions to enable
  645. type-specific lexical and result-based behavior. For example, to ensure
  646. that a string-based function returns a Unicode value and is similarly
  647. treated as a string in expressions, specify
  648. :class:`~sqlalchemy.types.Unicode` as the type:
  649. .. sourcecode:: pycon+sql
  650. >>> print(func.my_string(u'hi', type_=Unicode) + ' ' +
  651. ... func.my_string(u'there', type_=Unicode))
  652. {printsql}my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3)
  653. The object returned by a :data:`.func` call is usually an instance of
  654. :class:`.Function`.
  655. This object meets the "column" interface, including comparison and labeling
  656. functions. The object can also be passed the :meth:`~.Connectable.execute`
  657. method of a :class:`_engine.Connection` or :class:`_engine.Engine`,
  658. where it will be
  659. wrapped inside of a SELECT statement first::
  660. print(connection.execute(func.current_timestamp()).scalar())
  661. In a few exception cases, the :data:`.func` accessor
  662. will redirect a name to a built-in expression such as :func:`.cast`
  663. or :func:`.extract`, as these names have well-known meaning
  664. but are not exactly the same as "functions" from a SQLAlchemy
  665. perspective.
  666. Functions which are interpreted as "generic" functions know how to
  667. calculate their return type automatically. For a listing of known generic
  668. functions, see :ref:`generic_functions`.
  669. .. note::
  670. The :data:`.func` construct has only limited support for calling
  671. standalone "stored procedures", especially those with special
  672. parameterization concerns.
  673. See the section :ref:`stored_procedures` for details on how to use
  674. the DBAPI-level ``callproc()`` method for fully traditional stored
  675. procedures.
  676. .. seealso::
  677. :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
  678. :class:`.Function`
  679. """ # noqa
  680. def __init__(self, **opts: Any):
  681. self.__names: List[str] = []
  682. self.opts = opts
  683. def __getattr__(self, name: str) -> _FunctionGenerator:
  684. # passthru __ attributes; fixes pydoc
  685. if name.startswith("__"):
  686. try:
  687. return self.__dict__[name] # type: ignore
  688. except KeyError:
  689. raise AttributeError(name)
  690. elif name.endswith("_"):
  691. name = name[0:-1]
  692. f = _FunctionGenerator(**self.opts)
  693. f.__names = list(self.__names) + [name]
  694. return f
  695. @overload
  696. def __call__(
  697. self, *c: Any, type_: _TypeEngineArgument[_T], **kwargs: Any
  698. ) -> Function[_T]:
  699. ...
  700. @overload
  701. def __call__(self, *c: Any, **kwargs: Any) -> Function[Any]:
  702. ...
  703. def __call__(self, *c: Any, **kwargs: Any) -> Function[Any]:
  704. o = self.opts.copy()
  705. o.update(kwargs)
  706. tokens = len(self.__names)
  707. if tokens == 2:
  708. package, fname = self.__names
  709. elif tokens == 1:
  710. package, fname = "_default", self.__names[0]
  711. else:
  712. package = None
  713. if package is not None:
  714. func = _registry[package].get(fname.lower())
  715. if func is not None:
  716. return func(*c, **o)
  717. return Function(
  718. self.__names[-1], packagenames=tuple(self.__names[0:-1]), *c, **o
  719. )
  720. if TYPE_CHECKING:
  721. # START GENERATED FUNCTION ACCESSORS
  722. # code within this block is **programmatically,
  723. # statically generated** by tools/generate_sql_functions.py
  724. @property
  725. def aggregate_strings(self) -> Type[aggregate_strings]:
  726. ...
  727. @property
  728. def ansifunction(self) -> Type[AnsiFunction[Any]]:
  729. ...
  730. @property
  731. def array_agg(self) -> Type[array_agg[Any]]:
  732. ...
  733. @property
  734. def cast(self) -> Type[Cast[Any]]:
  735. ...
  736. @property
  737. def char_length(self) -> Type[char_length]:
  738. ...
  739. # set ColumnElement[_T] as a separate overload, to appease mypy
  740. # which seems to not want to accept _T from _ColumnExpressionArgument.
  741. # this is even if all non-generic types are removed from it, so
  742. # reasons remain unclear for why this does not work
  743. @overload
  744. def coalesce(
  745. self,
  746. col: ColumnElement[_T],
  747. *args: _ColumnExpressionOrLiteralArgument[Any],
  748. **kwargs: Any,
  749. ) -> coalesce[_T]:
  750. ...
  751. @overload
  752. def coalesce(
  753. self,
  754. col: _ColumnExpressionArgument[_T],
  755. *args: _ColumnExpressionOrLiteralArgument[Any],
  756. **kwargs: Any,
  757. ) -> coalesce[_T]:
  758. ...
  759. @overload
  760. def coalesce(
  761. self,
  762. col: _ColumnExpressionOrLiteralArgument[_T],
  763. *args: _ColumnExpressionOrLiteralArgument[Any],
  764. **kwargs: Any,
  765. ) -> coalesce[_T]:
  766. ...
  767. def coalesce(
  768. self,
  769. col: _ColumnExpressionOrLiteralArgument[_T],
  770. *args: _ColumnExpressionOrLiteralArgument[Any],
  771. **kwargs: Any,
  772. ) -> coalesce[_T]:
  773. ...
  774. @property
  775. def concat(self) -> Type[concat]:
  776. ...
  777. @property
  778. def count(self) -> Type[count]:
  779. ...
  780. @property
  781. def cube(self) -> Type[cube[Any]]:
  782. ...
  783. @property
  784. def cume_dist(self) -> Type[cume_dist]:
  785. ...
  786. @property
  787. def current_date(self) -> Type[current_date]:
  788. ...
  789. @property
  790. def current_time(self) -> Type[current_time]:
  791. ...
  792. @property
  793. def current_timestamp(self) -> Type[current_timestamp]:
  794. ...
  795. @property
  796. def current_user(self) -> Type[current_user]:
  797. ...
  798. @property
  799. def dense_rank(self) -> Type[dense_rank]:
  800. ...
  801. @property
  802. def extract(self) -> Type[Extract]:
  803. ...
  804. @property
  805. def grouping_sets(self) -> Type[grouping_sets[Any]]:
  806. ...
  807. @property
  808. def localtime(self) -> Type[localtime]:
  809. ...
  810. @property
  811. def localtimestamp(self) -> Type[localtimestamp]:
  812. ...
  813. # set ColumnElement[_T] as a separate overload, to appease mypy
  814. # which seems to not want to accept _T from _ColumnExpressionArgument.
  815. # this is even if all non-generic types are removed from it, so
  816. # reasons remain unclear for why this does not work
  817. @overload
  818. def max( # noqa: A001
  819. self,
  820. col: ColumnElement[_T],
  821. *args: _ColumnExpressionOrLiteralArgument[Any],
  822. **kwargs: Any,
  823. ) -> max[_T]:
  824. ...
  825. @overload
  826. def max( # noqa: A001
  827. self,
  828. col: _ColumnExpressionArgument[_T],
  829. *args: _ColumnExpressionOrLiteralArgument[Any],
  830. **kwargs: Any,
  831. ) -> max[_T]:
  832. ...
  833. @overload
  834. def max( # noqa: A001
  835. self,
  836. col: _ColumnExpressionOrLiteralArgument[_T],
  837. *args: _ColumnExpressionOrLiteralArgument[Any],
  838. **kwargs: Any,
  839. ) -> max[_T]:
  840. ...
  841. def max( # noqa: A001
  842. self,
  843. col: _ColumnExpressionOrLiteralArgument[_T],
  844. *args: _ColumnExpressionOrLiteralArgument[Any],
  845. **kwargs: Any,
  846. ) -> max[_T]:
  847. ...
  848. # set ColumnElement[_T] as a separate overload, to appease mypy
  849. # which seems to not want to accept _T from _ColumnExpressionArgument.
  850. # this is even if all non-generic types are removed from it, so
  851. # reasons remain unclear for why this does not work
  852. @overload
  853. def min( # noqa: A001
  854. self,
  855. col: ColumnElement[_T],
  856. *args: _ColumnExpressionOrLiteralArgument[Any],
  857. **kwargs: Any,
  858. ) -> min[_T]:
  859. ...
  860. @overload
  861. def min( # noqa: A001
  862. self,
  863. col: _ColumnExpressionArgument[_T],
  864. *args: _ColumnExpressionOrLiteralArgument[Any],
  865. **kwargs: Any,
  866. ) -> min[_T]:
  867. ...
  868. @overload
  869. def min( # noqa: A001
  870. self,
  871. col: _ColumnExpressionOrLiteralArgument[_T],
  872. *args: _ColumnExpressionOrLiteralArgument[Any],
  873. **kwargs: Any,
  874. ) -> min[_T]:
  875. ...
  876. def min( # noqa: A001
  877. self,
  878. col: _ColumnExpressionOrLiteralArgument[_T],
  879. *args: _ColumnExpressionOrLiteralArgument[Any],
  880. **kwargs: Any,
  881. ) -> min[_T]:
  882. ...
  883. @property
  884. def mode(self) -> Type[mode[Any]]:
  885. ...
  886. @property
  887. def next_value(self) -> Type[next_value]:
  888. ...
  889. @property
  890. def now(self) -> Type[now]:
  891. ...
  892. @property
  893. def orderedsetagg(self) -> Type[OrderedSetAgg[Any]]:
  894. ...
  895. @property
  896. def percent_rank(self) -> Type[percent_rank]:
  897. ...
  898. @property
  899. def percentile_cont(self) -> Type[percentile_cont[Any]]:
  900. ...
  901. @property
  902. def percentile_disc(self) -> Type[percentile_disc[Any]]:
  903. ...
  904. @property
  905. def random(self) -> Type[random]:
  906. ...
  907. @property
  908. def rank(self) -> Type[rank]:
  909. ...
  910. @property
  911. def rollup(self) -> Type[rollup[Any]]:
  912. ...
  913. @property
  914. def session_user(self) -> Type[session_user]:
  915. ...
  916. # set ColumnElement[_T] as a separate overload, to appease mypy
  917. # which seems to not want to accept _T from _ColumnExpressionArgument.
  918. # this is even if all non-generic types are removed from it, so
  919. # reasons remain unclear for why this does not work
  920. @overload
  921. def sum( # noqa: A001
  922. self,
  923. col: ColumnElement[_T],
  924. *args: _ColumnExpressionOrLiteralArgument[Any],
  925. **kwargs: Any,
  926. ) -> sum[_T]:
  927. ...
  928. @overload
  929. def sum( # noqa: A001
  930. self,
  931. col: _ColumnExpressionArgument[_T],
  932. *args: _ColumnExpressionOrLiteralArgument[Any],
  933. **kwargs: Any,
  934. ) -> sum[_T]:
  935. ...
  936. @overload
  937. def sum( # noqa: A001
  938. self,
  939. col: _ColumnExpressionOrLiteralArgument[_T],
  940. *args: _ColumnExpressionOrLiteralArgument[Any],
  941. **kwargs: Any,
  942. ) -> sum[_T]:
  943. ...
  944. def sum( # noqa: A001
  945. self,
  946. col: _ColumnExpressionOrLiteralArgument[_T],
  947. *args: _ColumnExpressionOrLiteralArgument[Any],
  948. **kwargs: Any,
  949. ) -> sum[_T]:
  950. ...
  951. @property
  952. def sysdate(self) -> Type[sysdate]:
  953. ...
  954. @property
  955. def user(self) -> Type[user]:
  956. ...
  957. # END GENERATED FUNCTION ACCESSORS
  958. func = _FunctionGenerator()
  959. func.__doc__ = _FunctionGenerator.__doc__
  960. modifier = _FunctionGenerator(group=False)
  961. class Function(FunctionElement[_T]):
  962. r"""Describe a named SQL function.
  963. The :class:`.Function` object is typically generated from the
  964. :data:`.func` generation object.
  965. :param \*clauses: list of column expressions that form the arguments
  966. of the SQL function call.
  967. :param type\_: optional :class:`.TypeEngine` datatype object that will be
  968. used as the return value of the column expression generated by this
  969. function call.
  970. :param packagenames: a string which indicates package prefix names
  971. to be prepended to the function name when the SQL is generated.
  972. The :data:`.func` generator creates these when it is called using
  973. dotted format, e.g.::
  974. func.mypackage.some_function(col1, col2)
  975. .. seealso::
  976. :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
  977. :data:`.func` - namespace which produces registered or ad-hoc
  978. :class:`.Function` instances.
  979. :class:`.GenericFunction` - allows creation of registered function
  980. types.
  981. """
  982. __visit_name__ = "function"
  983. _traverse_internals = FunctionElement._traverse_internals + [
  984. ("packagenames", InternalTraversal.dp_plain_obj),
  985. ("name", InternalTraversal.dp_string),
  986. ("type", InternalTraversal.dp_type),
  987. ]
  988. name: str
  989. identifier: str
  990. type: TypeEngine[_T]
  991. """A :class:`_types.TypeEngine` object which refers to the SQL return
  992. type represented by this SQL function.
  993. This datatype may be configured when generating a
  994. :class:`_functions.Function` object by passing the
  995. :paramref:`_functions.Function.type_` parameter, e.g.::
  996. >>> select(func.lower("some VALUE", type_=String))
  997. The small number of built-in classes of :class:`_functions.Function` come
  998. with a built-in datatype that's appropriate to the class of function and
  999. its arguments. For functions that aren't known, the type defaults to the
  1000. "null type".
  1001. """
  1002. @overload
  1003. def __init__(
  1004. self,
  1005. name: str,
  1006. *clauses: _ColumnExpressionOrLiteralArgument[_T],
  1007. type_: None = ...,
  1008. packagenames: Optional[Tuple[str, ...]] = ...,
  1009. ):
  1010. ...
  1011. @overload
  1012. def __init__(
  1013. self,
  1014. name: str,
  1015. *clauses: _ColumnExpressionOrLiteralArgument[Any],
  1016. type_: _TypeEngineArgument[_T] = ...,
  1017. packagenames: Optional[Tuple[str, ...]] = ...,
  1018. ):
  1019. ...
  1020. def __init__(
  1021. self,
  1022. name: str,
  1023. *clauses: _ColumnExpressionOrLiteralArgument[Any],
  1024. type_: Optional[_TypeEngineArgument[_T]] = None,
  1025. packagenames: Optional[Tuple[str, ...]] = None,
  1026. ):
  1027. """Construct a :class:`.Function`.
  1028. The :data:`.func` construct is normally used to construct
  1029. new :class:`.Function` instances.
  1030. """
  1031. self.packagenames = packagenames or ()
  1032. self.name = name
  1033. # if type is None, we get NULLTYPE, which is our _T. But I don't
  1034. # know how to get the overloads to express that correctly
  1035. self.type = type_api.to_instance(type_) # type: ignore
  1036. FunctionElement.__init__(self, *clauses)
  1037. def _bind_param(
  1038. self,
  1039. operator: OperatorType,
  1040. obj: Any,
  1041. type_: Optional[TypeEngine[_T]] = None,
  1042. expanding: bool = False,
  1043. **kw: Any,
  1044. ) -> BindParameter[_T]:
  1045. return BindParameter(
  1046. self.name,
  1047. obj,
  1048. _compared_to_operator=operator,
  1049. _compared_to_type=self.type,
  1050. type_=type_,
  1051. unique=True,
  1052. expanding=expanding,
  1053. **kw,
  1054. )
  1055. class GenericFunction(Function[_T]):
  1056. """Define a 'generic' function.
  1057. A generic function is a pre-established :class:`.Function`
  1058. class that is instantiated automatically when called
  1059. by name from the :data:`.func` attribute. Note that
  1060. calling any name from :data:`.func` has the effect that
  1061. a new :class:`.Function` instance is created automatically,
  1062. given that name. The primary use case for defining
  1063. a :class:`.GenericFunction` class is so that a function
  1064. of a particular name may be given a fixed return type.
  1065. It can also include custom argument parsing schemes as well
  1066. as additional methods.
  1067. Subclasses of :class:`.GenericFunction` are automatically
  1068. registered under the name of the class. For
  1069. example, a user-defined function ``as_utc()`` would
  1070. be available immediately::
  1071. from sqlalchemy.sql.functions import GenericFunction
  1072. from sqlalchemy.types import DateTime
  1073. class as_utc(GenericFunction):
  1074. type = DateTime()
  1075. inherit_cache = True
  1076. print(select(func.as_utc()))
  1077. User-defined generic functions can be organized into
  1078. packages by specifying the "package" attribute when defining
  1079. :class:`.GenericFunction`. Third party libraries
  1080. containing many functions may want to use this in order
  1081. to avoid name conflicts with other systems. For example,
  1082. if our ``as_utc()`` function were part of a package
  1083. "time"::
  1084. class as_utc(GenericFunction):
  1085. type = DateTime()
  1086. package = "time"
  1087. inherit_cache = True
  1088. The above function would be available from :data:`.func`
  1089. using the package name ``time``::
  1090. print(select(func.time.as_utc()))
  1091. A final option is to allow the function to be accessed
  1092. from one name in :data:`.func` but to render as a different name.
  1093. The ``identifier`` attribute will override the name used to
  1094. access the function as loaded from :data:`.func`, but will retain
  1095. the usage of ``name`` as the rendered name::
  1096. class GeoBuffer(GenericFunction):
  1097. type = Geometry()
  1098. package = "geo"
  1099. name = "ST_Buffer"
  1100. identifier = "buffer"
  1101. inherit_cache = True
  1102. The above function will render as follows:
  1103. .. sourcecode:: pycon+sql
  1104. >>> print(func.geo.buffer())
  1105. {printsql}ST_Buffer()
  1106. The name will be rendered as is, however without quoting unless the name
  1107. contains special characters that require quoting. To force quoting
  1108. on or off for the name, use the :class:`.sqlalchemy.sql.quoted_name`
  1109. construct::
  1110. from sqlalchemy.sql import quoted_name
  1111. class GeoBuffer(GenericFunction):
  1112. type = Geometry()
  1113. package = "geo"
  1114. name = quoted_name("ST_Buffer", True)
  1115. identifier = "buffer"
  1116. inherit_cache = True
  1117. The above function will render as:
  1118. .. sourcecode:: pycon+sql
  1119. >>> print(func.geo.buffer())
  1120. {printsql}"ST_Buffer"()
  1121. Type parameters for this class as a
  1122. `generic type <https://peps.python.org/pep-0484/#generics>`_ can be passed
  1123. and should match the type seen in a :class:`_engine.Result`. For example::
  1124. class as_utc(GenericFunction[datetime.datetime]):
  1125. type = DateTime()
  1126. inherit_cache = True
  1127. The above indicates that the following expression returns a ``datetime``
  1128. object::
  1129. connection.scalar(select(func.as_utc()))
  1130. .. versionadded:: 1.3.13 The :class:`.quoted_name` construct is now
  1131. recognized for quoting when used with the "name" attribute of the
  1132. object, so that quoting can be forced on or off for the function
  1133. name.
  1134. """
  1135. coerce_arguments = True
  1136. inherit_cache = True
  1137. _register: bool
  1138. name = "GenericFunction"
  1139. def __init_subclass__(cls) -> None:
  1140. if annotation.Annotated not in cls.__mro__:
  1141. cls._register_generic_function(cls.__name__, cls.__dict__)
  1142. super().__init_subclass__()
  1143. @classmethod
  1144. def _register_generic_function(
  1145. cls, clsname: str, clsdict: Mapping[str, Any]
  1146. ) -> None:
  1147. cls.name = name = clsdict.get("name", clsname)
  1148. cls.identifier = identifier = clsdict.get("identifier", name)
  1149. package = clsdict.get("package", "_default")
  1150. # legacy
  1151. if "__return_type__" in clsdict:
  1152. cls.type = clsdict["__return_type__"]
  1153. # Check _register attribute status
  1154. cls._register = getattr(cls, "_register", True)
  1155. # Register the function if required
  1156. if cls._register:
  1157. register_function(identifier, cls, package)
  1158. else:
  1159. # Set _register to True to register child classes by default
  1160. cls._register = True
  1161. def __init__(
  1162. self, *args: _ColumnExpressionOrLiteralArgument[Any], **kwargs: Any
  1163. ):
  1164. parsed_args = kwargs.pop("_parsed_args", None)
  1165. if parsed_args is None:
  1166. parsed_args = [
  1167. coercions.expect(
  1168. roles.ExpressionElementRole,
  1169. c,
  1170. name=self.name,
  1171. apply_propagate_attrs=self,
  1172. )
  1173. for c in args
  1174. ]
  1175. self._has_args = self._has_args or bool(parsed_args)
  1176. self.packagenames = ()
  1177. self.clause_expr = Grouping(
  1178. ClauseList(
  1179. operator=operators.comma_op, group_contents=True, *parsed_args
  1180. )
  1181. )
  1182. self.type = type_api.to_instance( # type: ignore
  1183. kwargs.pop("type_", None) or getattr(self, "type", None)
  1184. )
  1185. register_function("cast", Cast) # type: ignore
  1186. register_function("extract", Extract) # type: ignore
  1187. class next_value(GenericFunction[int]):
  1188. """Represent the 'next value', given a :class:`.Sequence`
  1189. as its single argument.
  1190. Compiles into the appropriate function on each backend,
  1191. or will raise NotImplementedError if used on a backend
  1192. that does not provide support for sequences.
  1193. """
  1194. type = sqltypes.Integer()
  1195. name = "next_value"
  1196. _traverse_internals = [
  1197. ("sequence", InternalTraversal.dp_named_ddl_element)
  1198. ]
  1199. def __init__(self, seq: schema.Sequence, **kw: Any):
  1200. assert isinstance(
  1201. seq, schema.Sequence
  1202. ), "next_value() accepts a Sequence object as input."
  1203. self.sequence = seq
  1204. self.type = sqltypes.to_instance( # type: ignore
  1205. seq.data_type or getattr(self, "type", None)
  1206. )
  1207. def compare(self, other: Any, **kw: Any) -> bool:
  1208. return (
  1209. isinstance(other, next_value)
  1210. and self.sequence.name == other.sequence.name
  1211. )
  1212. @property
  1213. def _from_objects(self) -> Any:
  1214. return []
  1215. class AnsiFunction(GenericFunction[_T]):
  1216. """Define a function in "ansi" format, which doesn't render parenthesis."""
  1217. inherit_cache = True
  1218. def __init__(self, *args: _ColumnExpressionArgument[Any], **kwargs: Any):
  1219. GenericFunction.__init__(self, *args, **kwargs)
  1220. class ReturnTypeFromArgs(GenericFunction[_T]):
  1221. """Define a function whose return type is the same as its arguments."""
  1222. inherit_cache = True
  1223. # set ColumnElement[_T] as a separate overload, to appease mypy which seems
  1224. # to not want to accept _T from _ColumnExpressionArgument. this is even if
  1225. # all non-generic types are removed from it, so reasons remain unclear for
  1226. # why this does not work
  1227. @overload
  1228. def __init__(
  1229. self,
  1230. col: ColumnElement[_T],
  1231. *args: _ColumnExpressionOrLiteralArgument[Any],
  1232. **kwargs: Any,
  1233. ):
  1234. ...
  1235. @overload
  1236. def __init__(
  1237. self,
  1238. col: _ColumnExpressionArgument[_T],
  1239. *args: _ColumnExpressionOrLiteralArgument[Any],
  1240. **kwargs: Any,
  1241. ):
  1242. ...
  1243. @overload
  1244. def __init__(
  1245. self,
  1246. col: _ColumnExpressionOrLiteralArgument[_T],
  1247. *args: _ColumnExpressionOrLiteralArgument[Any],
  1248. **kwargs: Any,
  1249. ):
  1250. ...
  1251. def __init__(
  1252. self, *args: _ColumnExpressionOrLiteralArgument[Any], **kwargs: Any
  1253. ):
  1254. fn_args: Sequence[ColumnElement[Any]] = [
  1255. coercions.expect(
  1256. roles.ExpressionElementRole,
  1257. c,
  1258. name=self.name,
  1259. apply_propagate_attrs=self,
  1260. )
  1261. for c in args
  1262. ]
  1263. kwargs.setdefault("type_", _type_from_args(fn_args))
  1264. kwargs["_parsed_args"] = fn_args
  1265. super().__init__(*fn_args, **kwargs)
  1266. class coalesce(ReturnTypeFromArgs[_T]):
  1267. _has_args = True
  1268. inherit_cache = True
  1269. class max(ReturnTypeFromArgs[_T]): # noqa: A001
  1270. """The SQL MAX() aggregate function."""
  1271. inherit_cache = True
  1272. class min(ReturnTypeFromArgs[_T]): # noqa: A001
  1273. """The SQL MIN() aggregate function."""
  1274. inherit_cache = True
  1275. class sum(ReturnTypeFromArgs[_T]): # noqa: A001
  1276. """The SQL SUM() aggregate function."""
  1277. inherit_cache = True
  1278. class now(GenericFunction[datetime.datetime]):
  1279. """The SQL now() datetime function.
  1280. SQLAlchemy dialects will usually render this particular function
  1281. in a backend-specific way, such as rendering it as ``CURRENT_TIMESTAMP``.
  1282. """
  1283. type = sqltypes.DateTime()
  1284. inherit_cache = True
  1285. class concat(GenericFunction[str]):
  1286. """The SQL CONCAT() function, which concatenates strings.
  1287. E.g.:
  1288. .. sourcecode:: pycon+sql
  1289. >>> print(select(func.concat('a', 'b')))
  1290. {printsql}SELECT concat(:concat_2, :concat_3) AS concat_1
  1291. String concatenation in SQLAlchemy is more commonly available using the
  1292. Python ``+`` operator with string datatypes, which will render a
  1293. backend-specific concatenation operator, such as :
  1294. .. sourcecode:: pycon+sql
  1295. >>> print(select(literal("a") + "b"))
  1296. {printsql}SELECT :param_1 || :param_2 AS anon_1
  1297. """
  1298. type = sqltypes.String()
  1299. inherit_cache = True
  1300. class char_length(GenericFunction[int]):
  1301. """The CHAR_LENGTH() SQL function."""
  1302. type = sqltypes.Integer()
  1303. inherit_cache = True
  1304. def __init__(self, arg: _ColumnExpressionArgument[str], **kw: Any):
  1305. # slight hack to limit to just one positional argument
  1306. # not sure why this one function has this special treatment
  1307. super().__init__(arg, **kw)
  1308. class random(GenericFunction[float]):
  1309. """The RANDOM() SQL function."""
  1310. _has_args = True
  1311. inherit_cache = True
  1312. class count(GenericFunction[int]):
  1313. r"""The ANSI COUNT aggregate function. With no arguments,
  1314. emits COUNT \*.
  1315. E.g.::
  1316. from sqlalchemy import func
  1317. from sqlalchemy import select
  1318. from sqlalchemy import table, column
  1319. my_table = table('some_table', column('id'))
  1320. stmt = select(func.count()).select_from(my_table)
  1321. Executing ``stmt`` would emit::
  1322. SELECT count(*) AS count_1
  1323. FROM some_table
  1324. """
  1325. type = sqltypes.Integer()
  1326. inherit_cache = True
  1327. def __init__(
  1328. self,
  1329. expression: Optional[_ColumnExpressionArgument[Any]] = None,
  1330. **kwargs: Any,
  1331. ):
  1332. if expression is None:
  1333. expression = literal_column("*")
  1334. super().__init__(expression, **kwargs)
  1335. class current_date(AnsiFunction[datetime.date]):
  1336. """The CURRENT_DATE() SQL function."""
  1337. type = sqltypes.Date()
  1338. inherit_cache = True
  1339. class current_time(AnsiFunction[datetime.time]):
  1340. """The CURRENT_TIME() SQL function."""
  1341. type = sqltypes.Time()
  1342. inherit_cache = True
  1343. class current_timestamp(AnsiFunction[datetime.datetime]):
  1344. """The CURRENT_TIMESTAMP() SQL function."""
  1345. type = sqltypes.DateTime()
  1346. inherit_cache = True
  1347. class current_user(AnsiFunction[str]):
  1348. """The CURRENT_USER() SQL function."""
  1349. type = sqltypes.String()
  1350. inherit_cache = True
  1351. class localtime(AnsiFunction[datetime.datetime]):
  1352. """The localtime() SQL function."""
  1353. type = sqltypes.DateTime()
  1354. inherit_cache = True
  1355. class localtimestamp(AnsiFunction[datetime.datetime]):
  1356. """The localtimestamp() SQL function."""
  1357. type = sqltypes.DateTime()
  1358. inherit_cache = True
  1359. class session_user(AnsiFunction[str]):
  1360. """The SESSION_USER() SQL function."""
  1361. type = sqltypes.String()
  1362. inherit_cache = True
  1363. class sysdate(AnsiFunction[datetime.datetime]):
  1364. """The SYSDATE() SQL function."""
  1365. type = sqltypes.DateTime()
  1366. inherit_cache = True
  1367. class user(AnsiFunction[str]):
  1368. """The USER() SQL function."""
  1369. type = sqltypes.String()
  1370. inherit_cache = True
  1371. class array_agg(GenericFunction[_T]):
  1372. """Support for the ARRAY_AGG function.
  1373. The ``func.array_agg(expr)`` construct returns an expression of
  1374. type :class:`_types.ARRAY`.
  1375. e.g.::
  1376. stmt = select(func.array_agg(table.c.values)[2:5])
  1377. .. seealso::
  1378. :func:`_postgresql.array_agg` - PostgreSQL-specific version that
  1379. returns :class:`_postgresql.ARRAY`, which has PG-specific operators
  1380. added.
  1381. """
  1382. inherit_cache = True
  1383. def __init__(self, *args: _ColumnExpressionArgument[Any], **kwargs: Any):
  1384. fn_args: Sequence[ColumnElement[Any]] = [
  1385. coercions.expect(
  1386. roles.ExpressionElementRole, c, apply_propagate_attrs=self
  1387. )
  1388. for c in args
  1389. ]
  1390. default_array_type = kwargs.pop("_default_array_type", sqltypes.ARRAY)
  1391. if "type_" not in kwargs:
  1392. type_from_args = _type_from_args(fn_args)
  1393. if isinstance(type_from_args, sqltypes.ARRAY):
  1394. kwargs["type_"] = type_from_args
  1395. else:
  1396. kwargs["type_"] = default_array_type(
  1397. type_from_args, dimensions=1
  1398. )
  1399. kwargs["_parsed_args"] = fn_args
  1400. super().__init__(*fn_args, **kwargs)
  1401. class OrderedSetAgg(GenericFunction[_T]):
  1402. """Define a function where the return type is based on the sort
  1403. expression type as defined by the expression passed to the
  1404. :meth:`.FunctionElement.within_group` method."""
  1405. array_for_multi_clause = False
  1406. inherit_cache = True
  1407. def within_group_type(
  1408. self, within_group: WithinGroup[Any]
  1409. ) -> TypeEngine[Any]:
  1410. func_clauses = cast(ClauseList, self.clause_expr.element)
  1411. order_by: Sequence[ColumnElement[Any]] = sqlutil.unwrap_order_by(
  1412. within_group.order_by
  1413. )
  1414. if self.array_for_multi_clause and len(func_clauses.clauses) > 1:
  1415. return sqltypes.ARRAY(order_by[0].type)
  1416. else:
  1417. return order_by[0].type
  1418. class mode(OrderedSetAgg[_T]):
  1419. """Implement the ``mode`` ordered-set aggregate function.
  1420. This function must be used with the :meth:`.FunctionElement.within_group`
  1421. modifier to supply a sort expression to operate upon.
  1422. The return type of this function is the same as the sort expression.
  1423. """
  1424. inherit_cache = True
  1425. class percentile_cont(OrderedSetAgg[_T]):
  1426. """Implement the ``percentile_cont`` ordered-set aggregate function.
  1427. This function must be used with the :meth:`.FunctionElement.within_group`
  1428. modifier to supply a sort expression to operate upon.
  1429. The return type of this function is the same as the sort expression,
  1430. or if the arguments are an array, an :class:`_types.ARRAY` of the sort
  1431. expression's type.
  1432. """
  1433. array_for_multi_clause = True
  1434. inherit_cache = True
  1435. class percentile_disc(OrderedSetAgg[_T]):
  1436. """Implement the ``percentile_disc`` ordered-set aggregate function.
  1437. This function must be used with the :meth:`.FunctionElement.within_group`
  1438. modifier to supply a sort expression to operate upon.
  1439. The return type of this function is the same as the sort expression,
  1440. or if the arguments are an array, an :class:`_types.ARRAY` of the sort
  1441. expression's type.
  1442. """
  1443. array_for_multi_clause = True
  1444. inherit_cache = True
  1445. class rank(GenericFunction[int]):
  1446. """Implement the ``rank`` hypothetical-set aggregate function.
  1447. This function must be used with the :meth:`.FunctionElement.within_group`
  1448. modifier to supply a sort expression to operate upon.
  1449. The return type of this function is :class:`.Integer`.
  1450. """
  1451. type = sqltypes.Integer()
  1452. inherit_cache = True
  1453. class dense_rank(GenericFunction[int]):
  1454. """Implement the ``dense_rank`` hypothetical-set aggregate function.
  1455. This function must be used with the :meth:`.FunctionElement.within_group`
  1456. modifier to supply a sort expression to operate upon.
  1457. The return type of this function is :class:`.Integer`.
  1458. """
  1459. type = sqltypes.Integer()
  1460. inherit_cache = True
  1461. class percent_rank(GenericFunction[decimal.Decimal]):
  1462. """Implement the ``percent_rank`` hypothetical-set aggregate function.
  1463. This function must be used with the :meth:`.FunctionElement.within_group`
  1464. modifier to supply a sort expression to operate upon.
  1465. The return type of this function is :class:`.Numeric`.
  1466. """
  1467. type: sqltypes.Numeric[decimal.Decimal] = sqltypes.Numeric()
  1468. inherit_cache = True
  1469. class cume_dist(GenericFunction[decimal.Decimal]):
  1470. """Implement the ``cume_dist`` hypothetical-set aggregate function.
  1471. This function must be used with the :meth:`.FunctionElement.within_group`
  1472. modifier to supply a sort expression to operate upon.
  1473. The return type of this function is :class:`.Numeric`.
  1474. """
  1475. type: sqltypes.Numeric[decimal.Decimal] = sqltypes.Numeric()
  1476. inherit_cache = True
  1477. class cube(GenericFunction[_T]):
  1478. r"""Implement the ``CUBE`` grouping operation.
  1479. This function is used as part of the GROUP BY of a statement,
  1480. e.g. :meth:`_expression.Select.group_by`::
  1481. stmt = select(
  1482. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1483. ).group_by(func.cube(table.c.col_1, table.c.col_2))
  1484. .. versionadded:: 1.2
  1485. """
  1486. _has_args = True
  1487. inherit_cache = True
  1488. class rollup(GenericFunction[_T]):
  1489. r"""Implement the ``ROLLUP`` grouping operation.
  1490. This function is used as part of the GROUP BY of a statement,
  1491. e.g. :meth:`_expression.Select.group_by`::
  1492. stmt = select(
  1493. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1494. ).group_by(func.rollup(table.c.col_1, table.c.col_2))
  1495. .. versionadded:: 1.2
  1496. """
  1497. _has_args = True
  1498. inherit_cache = True
  1499. class grouping_sets(GenericFunction[_T]):
  1500. r"""Implement the ``GROUPING SETS`` grouping operation.
  1501. This function is used as part of the GROUP BY of a statement,
  1502. e.g. :meth:`_expression.Select.group_by`::
  1503. stmt = select(
  1504. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1505. ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))
  1506. In order to group by multiple sets, use the :func:`.tuple_` construct::
  1507. from sqlalchemy import tuple_
  1508. stmt = select(
  1509. func.sum(table.c.value),
  1510. table.c.col_1, table.c.col_2,
  1511. table.c.col_3
  1512. ).group_by(
  1513. func.grouping_sets(
  1514. tuple_(table.c.col_1, table.c.col_2),
  1515. tuple_(table.c.value, table.c.col_3),
  1516. )
  1517. )
  1518. .. versionadded:: 1.2
  1519. """
  1520. _has_args = True
  1521. inherit_cache = True
  1522. class aggregate_strings(GenericFunction[str]):
  1523. """Implement a generic string aggregation function.
  1524. This function will concatenate non-null values into a string and
  1525. separate the values by a delimiter.
  1526. This function is compiled on a per-backend basis, into functions
  1527. such as ``group_concat()``, ``string_agg()``, or ``LISTAGG()``.
  1528. e.g. Example usage with delimiter '.'::
  1529. stmt = select(func.aggregate_strings(table.c.str_col, "."))
  1530. The return type of this function is :class:`.String`.
  1531. .. versionadded: 2.0.21
  1532. """
  1533. type = sqltypes.String()
  1534. _has_args = True
  1535. inherit_cache = True
  1536. def __init__(self, clause: _ColumnExpressionArgument[Any], separator: str):
  1537. super().__init__(clause, separator)