functions.py 63 KB

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