Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

2107 linhas
63 KiB

  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 is operators.getitem and isinstance(
  552. self.type, sqltypes.ARRAY
  553. ):
  554. return Grouping(self)
  555. else:
  556. return super().self_group(against=against)
  557. @property
  558. def entity_namespace(self) -> _EntityNamespace:
  559. """overrides FromClause.entity_namespace as functions are generally
  560. column expressions and not FromClauses.
  561. """
  562. # ideally functions would not be fromclauses but we failed to make
  563. # this adjustment in 1.4
  564. return _entity_namespace(self.clause_expr)
  565. class FunctionAsBinary(BinaryExpression[Any]):
  566. _traverse_internals = [
  567. ("sql_function", InternalTraversal.dp_clauseelement),
  568. ("left_index", InternalTraversal.dp_plain_obj),
  569. ("right_index", InternalTraversal.dp_plain_obj),
  570. ("modifiers", InternalTraversal.dp_plain_dict),
  571. ]
  572. sql_function: FunctionElement[Any]
  573. left_index: int
  574. right_index: int
  575. def _gen_cache_key(self, anon_map: Any, bindparams: Any) -> Any:
  576. return ColumnElement._gen_cache_key(self, anon_map, bindparams)
  577. def __init__(
  578. self, fn: FunctionElement[Any], left_index: int, right_index: int
  579. ) -> None:
  580. self.sql_function = fn
  581. self.left_index = left_index
  582. self.right_index = right_index
  583. self.operator = operators.function_as_comparison_op
  584. self.type = sqltypes.BOOLEANTYPE
  585. self.negate = None
  586. self._is_implicitly_boolean = True
  587. self.modifiers = util.immutabledict({})
  588. @property
  589. def left_expr(self) -> ColumnElement[Any]:
  590. return self.sql_function.clauses.clauses[self.left_index - 1]
  591. @left_expr.setter
  592. def left_expr(self, value: ColumnElement[Any]) -> None:
  593. self.sql_function.clauses.clauses[self.left_index - 1] = value
  594. @property
  595. def right_expr(self) -> ColumnElement[Any]:
  596. return self.sql_function.clauses.clauses[self.right_index - 1]
  597. @right_expr.setter
  598. def right_expr(self, value: ColumnElement[Any]) -> None:
  599. self.sql_function.clauses.clauses[self.right_index - 1] = value
  600. if not TYPE_CHECKING:
  601. # mypy can't accommodate @property to replace an instance
  602. # variable
  603. left = left_expr
  604. right = right_expr
  605. class ScalarFunctionColumn(NamedColumn[_T]):
  606. __visit_name__ = "scalar_function_column"
  607. _traverse_internals = [
  608. ("name", InternalTraversal.dp_anon_name),
  609. ("type", InternalTraversal.dp_type),
  610. ("fn", InternalTraversal.dp_clauseelement),
  611. ]
  612. is_literal = False
  613. table = None
  614. def __init__(
  615. self,
  616. fn: FunctionElement[_T],
  617. name: str,
  618. type_: Optional[_TypeEngineArgument[_T]] = None,
  619. ) -> None:
  620. self.fn = fn
  621. self.name = name
  622. # if type is None, we get NULLTYPE, which is our _T. But I don't
  623. # know how to get the overloads to express that correctly
  624. self.type = type_api.to_instance(type_) # type: ignore
  625. class _FunctionGenerator:
  626. """Generate SQL function expressions.
  627. :data:`.func` is a special object instance which generates SQL
  628. functions based on name-based attributes, e.g.:
  629. .. sourcecode:: pycon+sql
  630. >>> print(func.count(1))
  631. {printsql}count(:param_1)
  632. The returned object is an instance of :class:`.Function`, and is a
  633. column-oriented SQL element like any other, and is used in that way:
  634. .. sourcecode:: pycon+sql
  635. >>> print(select(func.count(table.c.id)))
  636. {printsql}SELECT count(sometable.id) FROM sometable
  637. Any name can be given to :data:`.func`. If the function name is unknown to
  638. SQLAlchemy, it will be rendered exactly as is. For common SQL functions
  639. which SQLAlchemy is aware of, the name may be interpreted as a *generic
  640. function* which will be compiled appropriately to the target database:
  641. .. sourcecode:: pycon+sql
  642. >>> print(func.current_timestamp())
  643. {printsql}CURRENT_TIMESTAMP
  644. To call functions which are present in dot-separated packages,
  645. specify them in the same manner:
  646. .. sourcecode:: pycon+sql
  647. >>> print(func.stats.yield_curve(5, 10))
  648. {printsql}stats.yield_curve(:yield_curve_1, :yield_curve_2)
  649. SQLAlchemy can be made aware of the return type of functions to enable
  650. type-specific lexical and result-based behavior. For example, to ensure
  651. that a string-based function returns a Unicode value and is similarly
  652. treated as a string in expressions, specify
  653. :class:`~sqlalchemy.types.Unicode` as the type:
  654. .. sourcecode:: pycon+sql
  655. >>> print(
  656. ... func.my_string("hi", type_=Unicode)
  657. ... + " "
  658. ... + func.my_string("there", type_=Unicode)
  659. ... )
  660. {printsql}my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3)
  661. The object returned by a :data:`.func` call is usually an instance of
  662. :class:`.Function`.
  663. This object meets the "column" interface, including comparison and labeling
  664. functions. The object can also be passed the :meth:`~.Connectable.execute`
  665. method of a :class:`_engine.Connection` or :class:`_engine.Engine`,
  666. where it will be
  667. wrapped inside of a SELECT statement first::
  668. print(connection.execute(func.current_timestamp()).scalar())
  669. In a few exception cases, the :data:`.func` accessor
  670. will redirect a name to a built-in expression such as :func:`.cast`
  671. or :func:`.extract`, as these names have well-known meaning
  672. but are not exactly the same as "functions" from a SQLAlchemy
  673. perspective.
  674. Functions which are interpreted as "generic" functions know how to
  675. calculate their return type automatically. For a listing of known generic
  676. functions, see :ref:`generic_functions`.
  677. .. note::
  678. The :data:`.func` construct has only limited support for calling
  679. standalone "stored procedures", especially those with special
  680. parameterization concerns.
  681. See the section :ref:`stored_procedures` for details on how to use
  682. the DBAPI-level ``callproc()`` method for fully traditional stored
  683. procedures.
  684. .. seealso::
  685. :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
  686. :class:`.Function`
  687. """ # noqa
  688. def __init__(self, **opts: Any) -> None:
  689. self.__names: List[str] = []
  690. self.opts = opts
  691. def __getattr__(self, name: str) -> _FunctionGenerator:
  692. # passthru __ attributes; fixes pydoc
  693. if name.startswith("__"):
  694. try:
  695. return self.__dict__[name] # type: ignore
  696. except KeyError:
  697. raise AttributeError(name)
  698. elif name.endswith("_"):
  699. name = name[0:-1]
  700. f = _FunctionGenerator(**self.opts)
  701. f.__names = list(self.__names) + [name]
  702. return f
  703. @overload
  704. def __call__(
  705. self, *c: Any, type_: _TypeEngineArgument[_T], **kwargs: Any
  706. ) -> Function[_T]: ...
  707. @overload
  708. def __call__(self, *c: Any, **kwargs: Any) -> Function[Any]: ...
  709. def __call__(self, *c: Any, **kwargs: Any) -> Function[Any]:
  710. o = self.opts.copy()
  711. o.update(kwargs)
  712. tokens = len(self.__names)
  713. if tokens == 2:
  714. package, fname = self.__names
  715. elif tokens == 1:
  716. package, fname = "_default", self.__names[0]
  717. else:
  718. package = None
  719. if package is not None:
  720. func = _registry[package].get(fname.lower())
  721. if func is not None:
  722. return func(*c, **o)
  723. return Function(
  724. self.__names[-1], packagenames=tuple(self.__names[0:-1]), *c, **o
  725. )
  726. if TYPE_CHECKING:
  727. # START GENERATED FUNCTION ACCESSORS
  728. # code within this block is **programmatically,
  729. # statically generated** by tools/generate_sql_functions.py
  730. @property
  731. def aggregate_strings(self) -> Type[aggregate_strings]: ...
  732. @property
  733. def ansifunction(self) -> Type[AnsiFunction[Any]]: ...
  734. # set ColumnElement[_T] as a separate overload, to appease
  735. # mypy which seems to not want to accept _T from
  736. # _ColumnExpressionArgument. Seems somewhat related to the covariant
  737. # _HasClauseElement as of mypy 1.15
  738. @overload
  739. def array_agg(
  740. self,
  741. col: ColumnElement[_T],
  742. *args: _ColumnExpressionOrLiteralArgument[Any],
  743. **kwargs: Any,
  744. ) -> array_agg[_T]: ...
  745. @overload
  746. def array_agg(
  747. self,
  748. col: _ColumnExpressionArgument[_T],
  749. *args: _ColumnExpressionOrLiteralArgument[Any],
  750. **kwargs: Any,
  751. ) -> array_agg[_T]: ...
  752. @overload
  753. def array_agg(
  754. self,
  755. col: _T,
  756. *args: _ColumnExpressionOrLiteralArgument[Any],
  757. **kwargs: Any,
  758. ) -> array_agg[_T]: ...
  759. def array_agg(
  760. self,
  761. col: _ColumnExpressionOrLiteralArgument[_T],
  762. *args: _ColumnExpressionOrLiteralArgument[Any],
  763. **kwargs: Any,
  764. ) -> array_agg[_T]: ...
  765. @property
  766. def cast(self) -> Type[Cast[Any]]: ...
  767. @property
  768. def char_length(self) -> Type[char_length]: ...
  769. # set ColumnElement[_T] as a separate overload, to appease
  770. # mypy which seems to not want to accept _T from
  771. # _ColumnExpressionArgument. Seems somewhat related to the covariant
  772. # _HasClauseElement as of mypy 1.15
  773. @overload
  774. def coalesce(
  775. self,
  776. col: ColumnElement[_T],
  777. *args: _ColumnExpressionOrLiteralArgument[Any],
  778. **kwargs: Any,
  779. ) -> coalesce[_T]: ...
  780. @overload
  781. def coalesce(
  782. self,
  783. col: _ColumnExpressionArgument[_T],
  784. *args: _ColumnExpressionOrLiteralArgument[Any],
  785. **kwargs: Any,
  786. ) -> coalesce[_T]: ...
  787. @overload
  788. def coalesce(
  789. self,
  790. col: _T,
  791. *args: _ColumnExpressionOrLiteralArgument[Any],
  792. **kwargs: Any,
  793. ) -> coalesce[_T]: ...
  794. def coalesce(
  795. self,
  796. col: _ColumnExpressionOrLiteralArgument[_T],
  797. *args: _ColumnExpressionOrLiteralArgument[Any],
  798. **kwargs: Any,
  799. ) -> coalesce[_T]: ...
  800. @property
  801. def concat(self) -> Type[concat]: ...
  802. @property
  803. def count(self) -> Type[count]: ...
  804. @property
  805. def cube(self) -> Type[cube[Any]]: ...
  806. @property
  807. def cume_dist(self) -> Type[cume_dist]: ...
  808. @property
  809. def current_date(self) -> Type[current_date]: ...
  810. @property
  811. def current_time(self) -> Type[current_time]: ...
  812. @property
  813. def current_timestamp(self) -> Type[current_timestamp]: ...
  814. @property
  815. def current_user(self) -> Type[current_user]: ...
  816. @property
  817. def dense_rank(self) -> Type[dense_rank]: ...
  818. @property
  819. def extract(self) -> Type[Extract]: ...
  820. @property
  821. def grouping_sets(self) -> Type[grouping_sets[Any]]: ...
  822. @property
  823. def localtime(self) -> Type[localtime]: ...
  824. @property
  825. def localtimestamp(self) -> Type[localtimestamp]: ...
  826. # set ColumnElement[_T] as a separate overload, to appease
  827. # mypy which seems to not want to accept _T from
  828. # _ColumnExpressionArgument. Seems somewhat related to the covariant
  829. # _HasClauseElement as of mypy 1.15
  830. @overload
  831. def max( # noqa: A001
  832. self,
  833. col: ColumnElement[_T],
  834. *args: _ColumnExpressionOrLiteralArgument[Any],
  835. **kwargs: Any,
  836. ) -> max[_T]: ...
  837. @overload
  838. def max( # noqa: A001
  839. self,
  840. col: _ColumnExpressionArgument[_T],
  841. *args: _ColumnExpressionOrLiteralArgument[Any],
  842. **kwargs: Any,
  843. ) -> max[_T]: ...
  844. @overload
  845. def max( # noqa: A001
  846. self,
  847. col: _T,
  848. *args: _ColumnExpressionOrLiteralArgument[Any],
  849. **kwargs: Any,
  850. ) -> max[_T]: ...
  851. def max( # noqa: A001
  852. self,
  853. col: _ColumnExpressionOrLiteralArgument[_T],
  854. *args: _ColumnExpressionOrLiteralArgument[Any],
  855. **kwargs: Any,
  856. ) -> max[_T]: ...
  857. # set ColumnElement[_T] as a separate overload, to appease
  858. # mypy which seems to not want to accept _T from
  859. # _ColumnExpressionArgument. Seems somewhat related to the covariant
  860. # _HasClauseElement as of mypy 1.15
  861. @overload
  862. def min( # noqa: A001
  863. self,
  864. col: ColumnElement[_T],
  865. *args: _ColumnExpressionOrLiteralArgument[Any],
  866. **kwargs: Any,
  867. ) -> min[_T]: ...
  868. @overload
  869. def min( # noqa: A001
  870. self,
  871. col: _ColumnExpressionArgument[_T],
  872. *args: _ColumnExpressionOrLiteralArgument[Any],
  873. **kwargs: Any,
  874. ) -> min[_T]: ...
  875. @overload
  876. def min( # noqa: A001
  877. self,
  878. col: _T,
  879. *args: _ColumnExpressionOrLiteralArgument[Any],
  880. **kwargs: Any,
  881. ) -> min[_T]: ...
  882. def min( # noqa: A001
  883. self,
  884. col: _ColumnExpressionOrLiteralArgument[_T],
  885. *args: _ColumnExpressionOrLiteralArgument[Any],
  886. **kwargs: Any,
  887. ) -> min[_T]: ...
  888. @property
  889. def mode(self) -> Type[mode[Any]]: ...
  890. @property
  891. def next_value(self) -> Type[next_value]: ...
  892. @property
  893. def now(self) -> Type[now]: ...
  894. @property
  895. def orderedsetagg(self) -> Type[OrderedSetAgg[Any]]: ...
  896. @property
  897. def percent_rank(self) -> Type[percent_rank]: ...
  898. @property
  899. def percentile_cont(self) -> Type[percentile_cont[Any]]: ...
  900. @property
  901. def percentile_disc(self) -> Type[percentile_disc[Any]]: ...
  902. @property
  903. def random(self) -> Type[random]: ...
  904. @property
  905. def rank(self) -> Type[rank]: ...
  906. @property
  907. def rollup(self) -> Type[rollup[Any]]: ...
  908. @property
  909. def session_user(self) -> Type[session_user]: ...
  910. # set ColumnElement[_T] as a separate overload, to appease
  911. # mypy which seems to not want to accept _T from
  912. # _ColumnExpressionArgument. Seems somewhat related to the covariant
  913. # _HasClauseElement as of mypy 1.15
  914. @overload
  915. def sum( # noqa: A001
  916. self,
  917. col: ColumnElement[_T],
  918. *args: _ColumnExpressionOrLiteralArgument[Any],
  919. **kwargs: Any,
  920. ) -> sum[_T]: ...
  921. @overload
  922. def sum( # noqa: A001
  923. self,
  924. col: _ColumnExpressionArgument[_T],
  925. *args: _ColumnExpressionOrLiteralArgument[Any],
  926. **kwargs: Any,
  927. ) -> sum[_T]: ...
  928. @overload
  929. def sum( # noqa: A001
  930. self,
  931. col: _T,
  932. *args: _ColumnExpressionOrLiteralArgument[Any],
  933. **kwargs: Any,
  934. ) -> sum[_T]: ...
  935. def sum( # noqa: A001
  936. self,
  937. col: _ColumnExpressionOrLiteralArgument[_T],
  938. *args: _ColumnExpressionOrLiteralArgument[Any],
  939. **kwargs: Any,
  940. ) -> sum[_T]: ...
  941. @property
  942. def sysdate(self) -> Type[sysdate]: ...
  943. @property
  944. def user(self) -> Type[user]: ...
  945. # END GENERATED FUNCTION ACCESSORS
  946. func = _FunctionGenerator()
  947. func.__doc__ = _FunctionGenerator.__doc__
  948. modifier = _FunctionGenerator(group=False)
  949. class Function(FunctionElement[_T]):
  950. r"""Describe a named SQL function.
  951. The :class:`.Function` object is typically generated from the
  952. :data:`.func` generation object.
  953. :param \*clauses: list of column expressions that form the arguments
  954. of the SQL function call.
  955. :param type\_: optional :class:`.TypeEngine` datatype object that will be
  956. used as the return value of the column expression generated by this
  957. function call.
  958. :param packagenames: a string which indicates package prefix names
  959. to be prepended to the function name when the SQL is generated.
  960. The :data:`.func` generator creates these when it is called using
  961. dotted format, e.g.::
  962. func.mypackage.some_function(col1, col2)
  963. .. seealso::
  964. :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
  965. :data:`.func` - namespace which produces registered or ad-hoc
  966. :class:`.Function` instances.
  967. :class:`.GenericFunction` - allows creation of registered function
  968. types.
  969. """
  970. __visit_name__ = "function"
  971. _traverse_internals = FunctionElement._traverse_internals + [
  972. ("packagenames", InternalTraversal.dp_plain_obj),
  973. ("name", InternalTraversal.dp_string),
  974. ("type", InternalTraversal.dp_type),
  975. ]
  976. name: str
  977. identifier: str
  978. type: TypeEngine[_T]
  979. """A :class:`_types.TypeEngine` object which refers to the SQL return
  980. type represented by this SQL function.
  981. This datatype may be configured when generating a
  982. :class:`_functions.Function` object by passing the
  983. :paramref:`_functions.Function.type_` parameter, e.g.::
  984. >>> select(func.lower("some VALUE", type_=String))
  985. The small number of built-in classes of :class:`_functions.Function` come
  986. with a built-in datatype that's appropriate to the class of function and
  987. its arguments. For functions that aren't known, the type defaults to the
  988. "null type".
  989. """
  990. @overload
  991. def __init__(
  992. self,
  993. name: str,
  994. *clauses: _ColumnExpressionOrLiteralArgument[_T],
  995. type_: None = ...,
  996. packagenames: Optional[Tuple[str, ...]] = ...,
  997. ) -> None: ...
  998. @overload
  999. def __init__(
  1000. self,
  1001. name: str,
  1002. *clauses: _ColumnExpressionOrLiteralArgument[Any],
  1003. type_: _TypeEngineArgument[_T] = ...,
  1004. packagenames: Optional[Tuple[str, ...]] = ...,
  1005. ) -> None: ...
  1006. def __init__(
  1007. self,
  1008. name: str,
  1009. *clauses: _ColumnExpressionOrLiteralArgument[Any],
  1010. type_: Optional[_TypeEngineArgument[_T]] = None,
  1011. packagenames: Optional[Tuple[str, ...]] = None,
  1012. ) -> None:
  1013. """Construct a :class:`.Function`.
  1014. The :data:`.func` construct is normally used to construct
  1015. new :class:`.Function` instances.
  1016. """
  1017. self.packagenames = packagenames or ()
  1018. self.name = name
  1019. # if type is None, we get NULLTYPE, which is our _T. But I don't
  1020. # know how to get the overloads to express that correctly
  1021. self.type = type_api.to_instance(type_) # type: ignore
  1022. FunctionElement.__init__(self, *clauses)
  1023. def _bind_param(
  1024. self,
  1025. operator: OperatorType,
  1026. obj: Any,
  1027. type_: Optional[TypeEngine[_T]] = None,
  1028. expanding: bool = False,
  1029. **kw: Any,
  1030. ) -> BindParameter[_T]:
  1031. return BindParameter(
  1032. self.name,
  1033. obj,
  1034. _compared_to_operator=operator,
  1035. _compared_to_type=self.type,
  1036. type_=type_,
  1037. unique=True,
  1038. expanding=expanding,
  1039. **kw,
  1040. )
  1041. class GenericFunction(Function[_T]):
  1042. """Define a 'generic' function.
  1043. A generic function is a pre-established :class:`.Function`
  1044. class that is instantiated automatically when called
  1045. by name from the :data:`.func` attribute. Note that
  1046. calling any name from :data:`.func` has the effect that
  1047. a new :class:`.Function` instance is created automatically,
  1048. given that name. The primary use case for defining
  1049. a :class:`.GenericFunction` class is so that a function
  1050. of a particular name may be given a fixed return type.
  1051. It can also include custom argument parsing schemes as well
  1052. as additional methods.
  1053. Subclasses of :class:`.GenericFunction` are automatically
  1054. registered under the name of the class. For
  1055. example, a user-defined function ``as_utc()`` would
  1056. be available immediately::
  1057. from sqlalchemy.sql.functions import GenericFunction
  1058. from sqlalchemy.types import DateTime
  1059. class as_utc(GenericFunction):
  1060. type = DateTime()
  1061. inherit_cache = True
  1062. print(select(func.as_utc()))
  1063. User-defined generic functions can be organized into
  1064. packages by specifying the "package" attribute when defining
  1065. :class:`.GenericFunction`. Third party libraries
  1066. containing many functions may want to use this in order
  1067. to avoid name conflicts with other systems. For example,
  1068. if our ``as_utc()`` function were part of a package
  1069. "time"::
  1070. class as_utc(GenericFunction):
  1071. type = DateTime()
  1072. package = "time"
  1073. inherit_cache = True
  1074. The above function would be available from :data:`.func`
  1075. using the package name ``time``::
  1076. print(select(func.time.as_utc()))
  1077. A final option is to allow the function to be accessed
  1078. from one name in :data:`.func` but to render as a different name.
  1079. The ``identifier`` attribute will override the name used to
  1080. access the function as loaded from :data:`.func`, but will retain
  1081. the usage of ``name`` as the rendered name::
  1082. class GeoBuffer(GenericFunction):
  1083. type = Geometry()
  1084. package = "geo"
  1085. name = "ST_Buffer"
  1086. identifier = "buffer"
  1087. inherit_cache = True
  1088. The above function will render as follows:
  1089. .. sourcecode:: pycon+sql
  1090. >>> print(func.geo.buffer())
  1091. {printsql}ST_Buffer()
  1092. The name will be rendered as is, however without quoting unless the name
  1093. contains special characters that require quoting. To force quoting
  1094. on or off for the name, use the :class:`.sqlalchemy.sql.quoted_name`
  1095. construct::
  1096. from sqlalchemy.sql import quoted_name
  1097. class GeoBuffer(GenericFunction):
  1098. type = Geometry()
  1099. package = "geo"
  1100. name = quoted_name("ST_Buffer", True)
  1101. identifier = "buffer"
  1102. inherit_cache = True
  1103. The above function will render as:
  1104. .. sourcecode:: pycon+sql
  1105. >>> print(func.geo.buffer())
  1106. {printsql}"ST_Buffer"()
  1107. Type parameters for this class as a
  1108. `generic type <https://peps.python.org/pep-0484/#generics>`_ can be passed
  1109. and should match the type seen in a :class:`_engine.Result`. For example::
  1110. class as_utc(GenericFunction[datetime.datetime]):
  1111. type = DateTime()
  1112. inherit_cache = True
  1113. The above indicates that the following expression returns a ``datetime``
  1114. object::
  1115. connection.scalar(select(func.as_utc()))
  1116. .. versionadded:: 1.3.13 The :class:`.quoted_name` construct is now
  1117. recognized for quoting when used with the "name" attribute of the
  1118. object, so that quoting can be forced on or off for the function
  1119. name.
  1120. """
  1121. coerce_arguments = True
  1122. inherit_cache = True
  1123. _register: bool
  1124. name = "GenericFunction"
  1125. def __init_subclass__(cls) -> None:
  1126. if annotation.Annotated not in cls.__mro__:
  1127. cls._register_generic_function(cls.__name__, cls.__dict__)
  1128. super().__init_subclass__()
  1129. @classmethod
  1130. def _register_generic_function(
  1131. cls, clsname: str, clsdict: Mapping[str, Any]
  1132. ) -> None:
  1133. cls.name = name = clsdict.get("name", clsname)
  1134. cls.identifier = identifier = clsdict.get("identifier", name)
  1135. package = clsdict.get("package", "_default")
  1136. # legacy
  1137. if "__return_type__" in clsdict:
  1138. cls.type = clsdict["__return_type__"]
  1139. # Check _register attribute status
  1140. cls._register = getattr(cls, "_register", True)
  1141. # Register the function if required
  1142. if cls._register:
  1143. register_function(identifier, cls, package)
  1144. else:
  1145. # Set _register to True to register child classes by default
  1146. cls._register = True
  1147. def __init__(
  1148. self, *args: _ColumnExpressionOrLiteralArgument[Any], **kwargs: Any
  1149. ) -> None:
  1150. parsed_args = kwargs.pop("_parsed_args", None)
  1151. if parsed_args is None:
  1152. parsed_args = [
  1153. coercions.expect(
  1154. roles.ExpressionElementRole,
  1155. c,
  1156. name=self.name,
  1157. apply_propagate_attrs=self,
  1158. )
  1159. for c in args
  1160. ]
  1161. self._has_args = self._has_args or bool(parsed_args)
  1162. self.packagenames = ()
  1163. self.clause_expr = Grouping(
  1164. ClauseList(
  1165. operator=operators.comma_op, group_contents=True, *parsed_args
  1166. )
  1167. )
  1168. self.type = type_api.to_instance( # type: ignore
  1169. kwargs.pop("type_", None) or getattr(self, "type", None)
  1170. )
  1171. register_function("cast", Cast) # type: ignore
  1172. register_function("extract", Extract) # type: ignore
  1173. class next_value(GenericFunction[int]):
  1174. """Represent the 'next value', given a :class:`.Sequence`
  1175. as its single argument.
  1176. Compiles into the appropriate function on each backend,
  1177. or will raise NotImplementedError if used on a backend
  1178. that does not provide support for sequences.
  1179. """
  1180. type = sqltypes.Integer()
  1181. name = "next_value"
  1182. _traverse_internals = [
  1183. ("sequence", InternalTraversal.dp_named_ddl_element)
  1184. ]
  1185. def __init__(self, seq: schema.Sequence, **kw: Any) -> None:
  1186. assert isinstance(
  1187. seq, schema.Sequence
  1188. ), "next_value() accepts a Sequence object as input."
  1189. self.sequence = seq
  1190. self.type = sqltypes.to_instance( # type: ignore
  1191. seq.data_type or getattr(self, "type", None)
  1192. )
  1193. def compare(self, other: Any, **kw: Any) -> bool:
  1194. return (
  1195. isinstance(other, next_value)
  1196. and self.sequence.name == other.sequence.name
  1197. )
  1198. @property
  1199. def _from_objects(self) -> Any:
  1200. return []
  1201. class AnsiFunction(GenericFunction[_T]):
  1202. """Define a function in "ansi" format, which doesn't render parenthesis."""
  1203. inherit_cache = True
  1204. def __init__(
  1205. self, *args: _ColumnExpressionArgument[Any], **kwargs: Any
  1206. ) -> None:
  1207. GenericFunction.__init__(self, *args, **kwargs)
  1208. class ReturnTypeFromArgs(GenericFunction[_T]):
  1209. """Define a function whose return type is bound to the type of its
  1210. arguments.
  1211. """
  1212. inherit_cache = True
  1213. # set ColumnElement[_T] as a separate overload, to appease
  1214. # mypy which seems to not want to accept _T from
  1215. # _ColumnExpressionArgument. Seems somewhat related to the covariant
  1216. # _HasClauseElement as of mypy 1.15
  1217. @overload
  1218. def __init__(
  1219. self,
  1220. col: ColumnElement[_T],
  1221. *args: _ColumnExpressionOrLiteralArgument[Any],
  1222. **kwargs: Any,
  1223. ) -> None: ...
  1224. @overload
  1225. def __init__(
  1226. self,
  1227. col: _ColumnExpressionArgument[_T],
  1228. *args: _ColumnExpressionOrLiteralArgument[Any],
  1229. **kwargs: Any,
  1230. ) -> None: ...
  1231. @overload
  1232. def __init__(
  1233. self,
  1234. col: _T,
  1235. *args: _ColumnExpressionOrLiteralArgument[Any],
  1236. **kwargs: Any,
  1237. ) -> None: ...
  1238. def __init__(
  1239. self, *args: _ColumnExpressionOrLiteralArgument[_T], **kwargs: Any
  1240. ) -> None:
  1241. fn_args: Sequence[ColumnElement[Any]] = [
  1242. coercions.expect(
  1243. roles.ExpressionElementRole,
  1244. c,
  1245. name=self.name,
  1246. apply_propagate_attrs=self,
  1247. )
  1248. for c in args
  1249. ]
  1250. kwargs.setdefault("type_", _type_from_args(fn_args))
  1251. kwargs["_parsed_args"] = fn_args
  1252. super().__init__(*fn_args, **kwargs)
  1253. class coalesce(ReturnTypeFromArgs[_T]):
  1254. _has_args = True
  1255. inherit_cache = True
  1256. class max(ReturnTypeFromArgs[_T]): # noqa: A001
  1257. """The SQL MAX() aggregate function."""
  1258. inherit_cache = True
  1259. class min(ReturnTypeFromArgs[_T]): # noqa: A001
  1260. """The SQL MIN() aggregate function."""
  1261. inherit_cache = True
  1262. class sum(ReturnTypeFromArgs[_T]): # noqa: A001
  1263. """The SQL SUM() aggregate function."""
  1264. inherit_cache = True
  1265. class now(GenericFunction[datetime.datetime]):
  1266. """The SQL now() datetime function.
  1267. SQLAlchemy dialects will usually render this particular function
  1268. in a backend-specific way, such as rendering it as ``CURRENT_TIMESTAMP``.
  1269. """
  1270. type = sqltypes.DateTime()
  1271. inherit_cache = True
  1272. class concat(GenericFunction[str]):
  1273. """The SQL CONCAT() function, which concatenates strings.
  1274. E.g.:
  1275. .. sourcecode:: pycon+sql
  1276. >>> print(select(func.concat("a", "b")))
  1277. {printsql}SELECT concat(:concat_2, :concat_3) AS concat_1
  1278. String concatenation in SQLAlchemy is more commonly available using the
  1279. Python ``+`` operator with string datatypes, which will render a
  1280. backend-specific concatenation operator, such as :
  1281. .. sourcecode:: pycon+sql
  1282. >>> print(select(literal("a") + "b"))
  1283. {printsql}SELECT :param_1 || :param_2 AS anon_1
  1284. """
  1285. type = sqltypes.String()
  1286. inherit_cache = True
  1287. class char_length(GenericFunction[int]):
  1288. """The CHAR_LENGTH() SQL function."""
  1289. type = sqltypes.Integer()
  1290. inherit_cache = True
  1291. def __init__(self, arg: _ColumnExpressionArgument[str], **kw: Any) -> None:
  1292. # slight hack to limit to just one positional argument
  1293. # not sure why this one function has this special treatment
  1294. super().__init__(arg, **kw)
  1295. class random(GenericFunction[float]):
  1296. """The RANDOM() SQL function."""
  1297. _has_args = True
  1298. inherit_cache = True
  1299. class count(GenericFunction[int]):
  1300. r"""The ANSI COUNT aggregate function. With no arguments,
  1301. emits COUNT \*.
  1302. E.g.::
  1303. from sqlalchemy import func
  1304. from sqlalchemy import select
  1305. from sqlalchemy import table, column
  1306. my_table = table("some_table", column("id"))
  1307. stmt = select(func.count()).select_from(my_table)
  1308. Executing ``stmt`` would emit:
  1309. .. sourcecode:: sql
  1310. SELECT count(*) AS count_1
  1311. FROM some_table
  1312. """
  1313. type = sqltypes.Integer()
  1314. inherit_cache = True
  1315. def __init__(
  1316. self,
  1317. expression: Union[
  1318. _ColumnExpressionArgument[Any], _StarOrOne, None
  1319. ] = None,
  1320. **kwargs: Any,
  1321. ) -> None:
  1322. if expression is None:
  1323. expression = literal_column("*")
  1324. super().__init__(expression, **kwargs)
  1325. class current_date(AnsiFunction[datetime.date]):
  1326. """The CURRENT_DATE() SQL function."""
  1327. type = sqltypes.Date()
  1328. inherit_cache = True
  1329. class current_time(AnsiFunction[datetime.time]):
  1330. """The CURRENT_TIME() SQL function."""
  1331. type = sqltypes.Time()
  1332. inherit_cache = True
  1333. class current_timestamp(AnsiFunction[datetime.datetime]):
  1334. """The CURRENT_TIMESTAMP() SQL function."""
  1335. type = sqltypes.DateTime()
  1336. inherit_cache = True
  1337. class current_user(AnsiFunction[str]):
  1338. """The CURRENT_USER() SQL function."""
  1339. type = sqltypes.String()
  1340. inherit_cache = True
  1341. class localtime(AnsiFunction[datetime.datetime]):
  1342. """The localtime() SQL function."""
  1343. type = sqltypes.DateTime()
  1344. inherit_cache = True
  1345. class localtimestamp(AnsiFunction[datetime.datetime]):
  1346. """The localtimestamp() SQL function."""
  1347. type = sqltypes.DateTime()
  1348. inherit_cache = True
  1349. class session_user(AnsiFunction[str]):
  1350. """The SESSION_USER() SQL function."""
  1351. type = sqltypes.String()
  1352. inherit_cache = True
  1353. class sysdate(AnsiFunction[datetime.datetime]):
  1354. """The SYSDATE() SQL function."""
  1355. type = sqltypes.DateTime()
  1356. inherit_cache = True
  1357. class user(AnsiFunction[str]):
  1358. """The USER() SQL function."""
  1359. type = sqltypes.String()
  1360. inherit_cache = True
  1361. class array_agg(ReturnTypeFromArgs[Sequence[_T]]):
  1362. """Support for the ARRAY_AGG function.
  1363. The ``func.array_agg(expr)`` construct returns an expression of
  1364. type :class:`_types.ARRAY`.
  1365. e.g.::
  1366. stmt = select(func.array_agg(table.c.values)[2:5])
  1367. .. seealso::
  1368. :func:`_postgresql.array_agg` - PostgreSQL-specific version that
  1369. returns :class:`_postgresql.ARRAY`, which has PG-specific operators
  1370. added.
  1371. """
  1372. inherit_cache = True
  1373. def __init__(
  1374. self, *args: _ColumnExpressionArgument[Any], **kwargs: Any
  1375. ) -> None:
  1376. fn_args: Sequence[ColumnElement[Any]] = [
  1377. coercions.expect(
  1378. roles.ExpressionElementRole, c, apply_propagate_attrs=self
  1379. )
  1380. for c in args
  1381. ]
  1382. default_array_type = kwargs.pop("_default_array_type", sqltypes.ARRAY)
  1383. if "type_" not in kwargs:
  1384. type_from_args = _type_from_args(fn_args)
  1385. if isinstance(type_from_args, sqltypes.ARRAY):
  1386. kwargs["type_"] = type_from_args
  1387. else:
  1388. kwargs["type_"] = default_array_type(
  1389. type_from_args, dimensions=1
  1390. )
  1391. kwargs["_parsed_args"] = fn_args
  1392. super().__init__(*fn_args, **kwargs)
  1393. class OrderedSetAgg(GenericFunction[_T]):
  1394. """Define a function where the return type is based on the sort
  1395. expression type as defined by the expression passed to the
  1396. :meth:`.FunctionElement.within_group` method."""
  1397. array_for_multi_clause = False
  1398. inherit_cache = True
  1399. def within_group_type(
  1400. self, within_group: WithinGroup[Any]
  1401. ) -> TypeEngine[Any]:
  1402. func_clauses = cast(ClauseList, self.clause_expr.element)
  1403. order_by: Sequence[ColumnElement[Any]] = sqlutil.unwrap_order_by(
  1404. within_group.order_by
  1405. )
  1406. if self.array_for_multi_clause and len(func_clauses.clauses) > 1:
  1407. return sqltypes.ARRAY(order_by[0].type)
  1408. else:
  1409. return order_by[0].type
  1410. class mode(OrderedSetAgg[_T]):
  1411. """Implement the ``mode`` ordered-set aggregate function.
  1412. This function must be used with the :meth:`.FunctionElement.within_group`
  1413. modifier to supply a sort expression to operate upon.
  1414. The return type of this function is the same as the sort expression.
  1415. """
  1416. inherit_cache = True
  1417. class percentile_cont(OrderedSetAgg[_T]):
  1418. """Implement the ``percentile_cont`` ordered-set aggregate function.
  1419. This function must be used with the :meth:`.FunctionElement.within_group`
  1420. modifier to supply a sort expression to operate upon.
  1421. The return type of this function is the same as the sort expression,
  1422. or if the arguments are an array, an :class:`_types.ARRAY` of the sort
  1423. expression's type.
  1424. """
  1425. array_for_multi_clause = True
  1426. inherit_cache = True
  1427. class percentile_disc(OrderedSetAgg[_T]):
  1428. """Implement the ``percentile_disc`` ordered-set aggregate function.
  1429. This function must be used with the :meth:`.FunctionElement.within_group`
  1430. modifier to supply a sort expression to operate upon.
  1431. The return type of this function is the same as the sort expression,
  1432. or if the arguments are an array, an :class:`_types.ARRAY` of the sort
  1433. expression's type.
  1434. """
  1435. array_for_multi_clause = True
  1436. inherit_cache = True
  1437. class rank(GenericFunction[int]):
  1438. """Implement the ``rank`` hypothetical-set aggregate function.
  1439. This function must be used with the :meth:`.FunctionElement.within_group`
  1440. modifier to supply a sort expression to operate upon.
  1441. The return type of this function is :class:`.Integer`.
  1442. """
  1443. type = sqltypes.Integer()
  1444. inherit_cache = True
  1445. class dense_rank(GenericFunction[int]):
  1446. """Implement the ``dense_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 percent_rank(GenericFunction[decimal.Decimal]):
  1454. """Implement the ``percent_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:`.Numeric`.
  1458. """
  1459. type: sqltypes.Numeric[decimal.Decimal] = sqltypes.Numeric()
  1460. inherit_cache = True
  1461. class cume_dist(GenericFunction[decimal.Decimal]):
  1462. """Implement the ``cume_dist`` 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 cube(GenericFunction[_T]):
  1470. r"""Implement the ``CUBE`` grouping operation.
  1471. This function is used as part of the GROUP BY of a statement,
  1472. e.g. :meth:`_expression.Select.group_by`::
  1473. stmt = select(
  1474. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1475. ).group_by(func.cube(table.c.col_1, table.c.col_2))
  1476. .. versionadded:: 1.2
  1477. """
  1478. _has_args = True
  1479. inherit_cache = True
  1480. class rollup(GenericFunction[_T]):
  1481. r"""Implement the ``ROLLUP`` grouping operation.
  1482. This function is used as part of the GROUP BY of a statement,
  1483. e.g. :meth:`_expression.Select.group_by`::
  1484. stmt = select(
  1485. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1486. ).group_by(func.rollup(table.c.col_1, table.c.col_2))
  1487. .. versionadded:: 1.2
  1488. """
  1489. _has_args = True
  1490. inherit_cache = True
  1491. class grouping_sets(GenericFunction[_T]):
  1492. r"""Implement the ``GROUPING SETS`` grouping operation.
  1493. This function is used as part of the GROUP BY of a statement,
  1494. e.g. :meth:`_expression.Select.group_by`::
  1495. stmt = select(
  1496. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1497. ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))
  1498. In order to group by multiple sets, use the :func:`.tuple_` construct::
  1499. from sqlalchemy import tuple_
  1500. stmt = select(
  1501. func.sum(table.c.value), table.c.col_1, table.c.col_2, table.c.col_3
  1502. ).group_by(
  1503. func.grouping_sets(
  1504. tuple_(table.c.col_1, table.c.col_2),
  1505. tuple_(table.c.value, table.c.col_3),
  1506. )
  1507. )
  1508. .. versionadded:: 1.2
  1509. """ # noqa: E501
  1510. _has_args = True
  1511. inherit_cache = True
  1512. class aggregate_strings(GenericFunction[str]):
  1513. """Implement a generic string aggregation function.
  1514. This function will concatenate non-null values into a string and
  1515. separate the values by a delimiter.
  1516. This function is compiled on a per-backend basis, into functions
  1517. such as ``group_concat()``, ``string_agg()``, or ``LISTAGG()``.
  1518. e.g. Example usage with delimiter '.'::
  1519. stmt = select(func.aggregate_strings(table.c.str_col, "."))
  1520. The return type of this function is :class:`.String`.
  1521. .. versionadded: 2.0.21
  1522. """
  1523. type = sqltypes.String()
  1524. _has_args = True
  1525. inherit_cache = True
  1526. def __init__(
  1527. self, clause: _ColumnExpressionArgument[Any], separator: str
  1528. ) -> None:
  1529. super().__init__(clause, separator)