SQLAlchemy 2.1.0b2__cp313-cp313t-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. sqlalchemy/__init__.py +298 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +171 -0
  4. sqlalchemy/connectors/asyncio.py +476 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/dialects/__init__.py +62 -0
  7. sqlalchemy/dialects/_typing.py +30 -0
  8. sqlalchemy/dialects/mssql/__init__.py +89 -0
  9. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  10. sqlalchemy/dialects/mssql/base.py +4166 -0
  11. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  12. sqlalchemy/dialects/mssql/json.py +140 -0
  13. sqlalchemy/dialects/mssql/mssqlpython.py +220 -0
  14. sqlalchemy/dialects/mssql/provision.py +196 -0
  15. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  16. sqlalchemy/dialects/mssql/pyodbc.py +698 -0
  17. sqlalchemy/dialects/mysql/__init__.py +106 -0
  18. sqlalchemy/dialects/mysql/_mariadb_shim.py +312 -0
  19. sqlalchemy/dialects/mysql/aiomysql.py +226 -0
  20. sqlalchemy/dialects/mysql/asyncmy.py +214 -0
  21. sqlalchemy/dialects/mysql/base.py +3877 -0
  22. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  23. sqlalchemy/dialects/mysql/dml.py +279 -0
  24. sqlalchemy/dialects/mysql/enumerated.py +277 -0
  25. sqlalchemy/dialects/mysql/expression.py +146 -0
  26. sqlalchemy/dialects/mysql/json.py +92 -0
  27. sqlalchemy/dialects/mysql/mariadb.py +67 -0
  28. sqlalchemy/dialects/mysql/mariadbconnector.py +330 -0
  29. sqlalchemy/dialects/mysql/mysqlconnector.py +296 -0
  30. sqlalchemy/dialects/mysql/mysqldb.py +312 -0
  31. sqlalchemy/dialects/mysql/provision.py +153 -0
  32. sqlalchemy/dialects/mysql/pymysql.py +157 -0
  33. sqlalchemy/dialects/mysql/pyodbc.py +156 -0
  34. sqlalchemy/dialects/mysql/reflection.py +724 -0
  35. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  36. sqlalchemy/dialects/mysql/types.py +845 -0
  37. sqlalchemy/dialects/oracle/__init__.py +85 -0
  38. sqlalchemy/dialects/oracle/base.py +3977 -0
  39. sqlalchemy/dialects/oracle/cx_oracle.py +1601 -0
  40. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  41. sqlalchemy/dialects/oracle/json.py +158 -0
  42. sqlalchemy/dialects/oracle/oracledb.py +909 -0
  43. sqlalchemy/dialects/oracle/provision.py +288 -0
  44. sqlalchemy/dialects/oracle/types.py +367 -0
  45. sqlalchemy/dialects/oracle/vector.py +368 -0
  46. sqlalchemy/dialects/postgresql/__init__.py +171 -0
  47. sqlalchemy/dialects/postgresql/_psycopg_common.py +229 -0
  48. sqlalchemy/dialects/postgresql/array.py +534 -0
  49. sqlalchemy/dialects/postgresql/asyncpg.py +1323 -0
  50. sqlalchemy/dialects/postgresql/base.py +5789 -0
  51. sqlalchemy/dialects/postgresql/bitstring.py +327 -0
  52. sqlalchemy/dialects/postgresql/dml.py +360 -0
  53. sqlalchemy/dialects/postgresql/ext.py +593 -0
  54. sqlalchemy/dialects/postgresql/hstore.py +423 -0
  55. sqlalchemy/dialects/postgresql/json.py +408 -0
  56. sqlalchemy/dialects/postgresql/named_types.py +521 -0
  57. sqlalchemy/dialects/postgresql/operators.py +130 -0
  58. sqlalchemy/dialects/postgresql/pg8000.py +670 -0
  59. sqlalchemy/dialects/postgresql/pg_catalog.py +344 -0
  60. sqlalchemy/dialects/postgresql/provision.py +184 -0
  61. sqlalchemy/dialects/postgresql/psycopg.py +799 -0
  62. sqlalchemy/dialects/postgresql/psycopg2.py +860 -0
  63. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  64. sqlalchemy/dialects/postgresql/ranges.py +1002 -0
  65. sqlalchemy/dialects/postgresql/types.py +388 -0
  66. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  67. sqlalchemy/dialects/sqlite/aiosqlite.py +321 -0
  68. sqlalchemy/dialects/sqlite/base.py +3063 -0
  69. sqlalchemy/dialects/sqlite/dml.py +279 -0
  70. sqlalchemy/dialects/sqlite/json.py +100 -0
  71. sqlalchemy/dialects/sqlite/provision.py +229 -0
  72. sqlalchemy/dialects/sqlite/pysqlcipher.py +161 -0
  73. sqlalchemy/dialects/sqlite/pysqlite.py +754 -0
  74. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  75. sqlalchemy/engine/__init__.py +62 -0
  76. sqlalchemy/engine/_processors_cy.cp313t-win_arm64.pyd +0 -0
  77. sqlalchemy/engine/_processors_cy.py +92 -0
  78. sqlalchemy/engine/_result_cy.cp313t-win_arm64.pyd +0 -0
  79. sqlalchemy/engine/_result_cy.py +633 -0
  80. sqlalchemy/engine/_row_cy.cp313t-win_arm64.pyd +0 -0
  81. sqlalchemy/engine/_row_cy.py +232 -0
  82. sqlalchemy/engine/_util_cy.cp313t-win_arm64.pyd +0 -0
  83. sqlalchemy/engine/_util_cy.py +136 -0
  84. sqlalchemy/engine/base.py +3354 -0
  85. sqlalchemy/engine/characteristics.py +155 -0
  86. sqlalchemy/engine/create.py +877 -0
  87. sqlalchemy/engine/cursor.py +2421 -0
  88. sqlalchemy/engine/default.py +2402 -0
  89. sqlalchemy/engine/events.py +965 -0
  90. sqlalchemy/engine/interfaces.py +3495 -0
  91. sqlalchemy/engine/mock.py +134 -0
  92. sqlalchemy/engine/processors.py +82 -0
  93. sqlalchemy/engine/reflection.py +2100 -0
  94. sqlalchemy/engine/result.py +1966 -0
  95. sqlalchemy/engine/row.py +397 -0
  96. sqlalchemy/engine/strategies.py +16 -0
  97. sqlalchemy/engine/url.py +922 -0
  98. sqlalchemy/engine/util.py +156 -0
  99. sqlalchemy/event/__init__.py +26 -0
  100. sqlalchemy/event/api.py +220 -0
  101. sqlalchemy/event/attr.py +674 -0
  102. sqlalchemy/event/base.py +472 -0
  103. sqlalchemy/event/legacy.py +258 -0
  104. sqlalchemy/event/registry.py +390 -0
  105. sqlalchemy/events.py +17 -0
  106. sqlalchemy/exc.py +922 -0
  107. sqlalchemy/ext/__init__.py +11 -0
  108. sqlalchemy/ext/associationproxy.py +2072 -0
  109. sqlalchemy/ext/asyncio/__init__.py +29 -0
  110. sqlalchemy/ext/asyncio/base.py +281 -0
  111. sqlalchemy/ext/asyncio/engine.py +1487 -0
  112. sqlalchemy/ext/asyncio/exc.py +21 -0
  113. sqlalchemy/ext/asyncio/result.py +994 -0
  114. sqlalchemy/ext/asyncio/scoping.py +1679 -0
  115. sqlalchemy/ext/asyncio/session.py +2007 -0
  116. sqlalchemy/ext/automap.py +1701 -0
  117. sqlalchemy/ext/baked.py +559 -0
  118. sqlalchemy/ext/compiler.py +600 -0
  119. sqlalchemy/ext/declarative/__init__.py +65 -0
  120. sqlalchemy/ext/declarative/extensions.py +560 -0
  121. sqlalchemy/ext/horizontal_shard.py +481 -0
  122. sqlalchemy/ext/hybrid.py +1877 -0
  123. sqlalchemy/ext/indexable.py +364 -0
  124. sqlalchemy/ext/instrumentation.py +450 -0
  125. sqlalchemy/ext/mutable.py +1081 -0
  126. sqlalchemy/ext/orderinglist.py +439 -0
  127. sqlalchemy/ext/serializer.py +185 -0
  128. sqlalchemy/future/__init__.py +16 -0
  129. sqlalchemy/future/engine.py +15 -0
  130. sqlalchemy/inspection.py +174 -0
  131. sqlalchemy/log.py +283 -0
  132. sqlalchemy/orm/__init__.py +176 -0
  133. sqlalchemy/orm/_orm_constructors.py +2694 -0
  134. sqlalchemy/orm/_typing.py +179 -0
  135. sqlalchemy/orm/attributes.py +2868 -0
  136. sqlalchemy/orm/base.py +976 -0
  137. sqlalchemy/orm/bulk_persistence.py +2152 -0
  138. sqlalchemy/orm/clsregistry.py +582 -0
  139. sqlalchemy/orm/collections.py +1568 -0
  140. sqlalchemy/orm/context.py +3471 -0
  141. sqlalchemy/orm/decl_api.py +2280 -0
  142. sqlalchemy/orm/decl_base.py +2309 -0
  143. sqlalchemy/orm/dependency.py +1306 -0
  144. sqlalchemy/orm/descriptor_props.py +1183 -0
  145. sqlalchemy/orm/dynamic.py +307 -0
  146. sqlalchemy/orm/evaluator.py +379 -0
  147. sqlalchemy/orm/events.py +3386 -0
  148. sqlalchemy/orm/exc.py +237 -0
  149. sqlalchemy/orm/identity.py +302 -0
  150. sqlalchemy/orm/instrumentation.py +746 -0
  151. sqlalchemy/orm/interfaces.py +1589 -0
  152. sqlalchemy/orm/loading.py +1684 -0
  153. sqlalchemy/orm/mapped_collection.py +557 -0
  154. sqlalchemy/orm/mapper.py +4411 -0
  155. sqlalchemy/orm/path_registry.py +829 -0
  156. sqlalchemy/orm/persistence.py +1789 -0
  157. sqlalchemy/orm/properties.py +973 -0
  158. sqlalchemy/orm/query.py +3528 -0
  159. sqlalchemy/orm/relationships.py +3570 -0
  160. sqlalchemy/orm/scoping.py +2232 -0
  161. sqlalchemy/orm/session.py +5403 -0
  162. sqlalchemy/orm/state.py +1175 -0
  163. sqlalchemy/orm/state_changes.py +196 -0
  164. sqlalchemy/orm/strategies.py +3492 -0
  165. sqlalchemy/orm/strategy_options.py +2562 -0
  166. sqlalchemy/orm/sync.py +164 -0
  167. sqlalchemy/orm/unitofwork.py +798 -0
  168. sqlalchemy/orm/util.py +2438 -0
  169. sqlalchemy/orm/writeonly.py +694 -0
  170. sqlalchemy/pool/__init__.py +41 -0
  171. sqlalchemy/pool/base.py +1522 -0
  172. sqlalchemy/pool/events.py +375 -0
  173. sqlalchemy/pool/impl.py +582 -0
  174. sqlalchemy/py.typed +0 -0
  175. sqlalchemy/schema.py +74 -0
  176. sqlalchemy/sql/__init__.py +156 -0
  177. sqlalchemy/sql/_annotated_cols.py +397 -0
  178. sqlalchemy/sql/_dml_constructors.py +132 -0
  179. sqlalchemy/sql/_elements_constructors.py +2164 -0
  180. sqlalchemy/sql/_orm_types.py +20 -0
  181. sqlalchemy/sql/_selectable_constructors.py +840 -0
  182. sqlalchemy/sql/_typing.py +487 -0
  183. sqlalchemy/sql/_util_cy.cp313t-win_arm64.pyd +0 -0
  184. sqlalchemy/sql/_util_cy.py +127 -0
  185. sqlalchemy/sql/annotation.py +590 -0
  186. sqlalchemy/sql/base.py +2699 -0
  187. sqlalchemy/sql/cache_key.py +1066 -0
  188. sqlalchemy/sql/coercions.py +1373 -0
  189. sqlalchemy/sql/compiler.py +8327 -0
  190. sqlalchemy/sql/crud.py +1815 -0
  191. sqlalchemy/sql/ddl.py +1928 -0
  192. sqlalchemy/sql/default_comparator.py +654 -0
  193. sqlalchemy/sql/dml.py +1977 -0
  194. sqlalchemy/sql/elements.py +6033 -0
  195. sqlalchemy/sql/events.py +458 -0
  196. sqlalchemy/sql/expression.py +172 -0
  197. sqlalchemy/sql/functions.py +2305 -0
  198. sqlalchemy/sql/lambdas.py +1443 -0
  199. sqlalchemy/sql/naming.py +209 -0
  200. sqlalchemy/sql/operators.py +2897 -0
  201. sqlalchemy/sql/roles.py +332 -0
  202. sqlalchemy/sql/schema.py +6703 -0
  203. sqlalchemy/sql/selectable.py +7553 -0
  204. sqlalchemy/sql/sqltypes.py +4093 -0
  205. sqlalchemy/sql/traversals.py +1042 -0
  206. sqlalchemy/sql/type_api.py +2446 -0
  207. sqlalchemy/sql/util.py +1495 -0
  208. sqlalchemy/sql/visitors.py +1157 -0
  209. sqlalchemy/testing/__init__.py +96 -0
  210. sqlalchemy/testing/assertions.py +1007 -0
  211. sqlalchemy/testing/assertsql.py +519 -0
  212. sqlalchemy/testing/asyncio.py +128 -0
  213. sqlalchemy/testing/config.py +440 -0
  214. sqlalchemy/testing/engines.py +483 -0
  215. sqlalchemy/testing/entities.py +117 -0
  216. sqlalchemy/testing/exclusions.py +476 -0
  217. sqlalchemy/testing/fixtures/__init__.py +30 -0
  218. sqlalchemy/testing/fixtures/base.py +384 -0
  219. sqlalchemy/testing/fixtures/mypy.py +247 -0
  220. sqlalchemy/testing/fixtures/orm.py +227 -0
  221. sqlalchemy/testing/fixtures/sql.py +538 -0
  222. sqlalchemy/testing/pickleable.py +155 -0
  223. sqlalchemy/testing/plugin/__init__.py +6 -0
  224. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  225. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  226. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  227. sqlalchemy/testing/profiling.py +329 -0
  228. sqlalchemy/testing/provision.py +613 -0
  229. sqlalchemy/testing/requirements.py +1978 -0
  230. sqlalchemy/testing/schema.py +198 -0
  231. sqlalchemy/testing/suite/__init__.py +19 -0
  232. sqlalchemy/testing/suite/test_cte.py +237 -0
  233. sqlalchemy/testing/suite/test_ddl.py +420 -0
  234. sqlalchemy/testing/suite/test_dialect.py +776 -0
  235. sqlalchemy/testing/suite/test_insert.py +630 -0
  236. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  237. sqlalchemy/testing/suite/test_results.py +660 -0
  238. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  239. sqlalchemy/testing/suite/test_select.py +2112 -0
  240. sqlalchemy/testing/suite/test_sequence.py +317 -0
  241. sqlalchemy/testing/suite/test_table_via_select.py +686 -0
  242. sqlalchemy/testing/suite/test_types.py +2271 -0
  243. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  244. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  245. sqlalchemy/testing/util.py +535 -0
  246. sqlalchemy/testing/warnings.py +52 -0
  247. sqlalchemy/types.py +76 -0
  248. sqlalchemy/util/__init__.py +158 -0
  249. sqlalchemy/util/_collections.py +688 -0
  250. sqlalchemy/util/_collections_cy.cp313t-win_arm64.pyd +0 -0
  251. sqlalchemy/util/_collections_cy.pxd +8 -0
  252. sqlalchemy/util/_collections_cy.py +516 -0
  253. sqlalchemy/util/_has_cython.py +46 -0
  254. sqlalchemy/util/_immutabledict_cy.cp313t-win_arm64.pyd +0 -0
  255. sqlalchemy/util/_immutabledict_cy.py +240 -0
  256. sqlalchemy/util/compat.py +299 -0
  257. sqlalchemy/util/concurrency.py +322 -0
  258. sqlalchemy/util/cython.py +79 -0
  259. sqlalchemy/util/deprecations.py +401 -0
  260. sqlalchemy/util/langhelpers.py +2320 -0
  261. sqlalchemy/util/preloaded.py +152 -0
  262. sqlalchemy/util/queue.py +304 -0
  263. sqlalchemy/util/tool_support.py +201 -0
  264. sqlalchemy/util/topological.py +120 -0
  265. sqlalchemy/util/typing.py +711 -0
  266. sqlalchemy-2.1.0b2.dist-info/METADATA +269 -0
  267. sqlalchemy-2.1.0b2.dist-info/RECORD +270 -0
  268. sqlalchemy-2.1.0b2.dist-info/WHEEL +5 -0
  269. sqlalchemy-2.1.0b2.dist-info/licenses/LICENSE +19 -0
  270. sqlalchemy-2.1.0b2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2305 @@
1
+ # sql/functions.py
2
+ # Copyright (C) 2005-2026 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
+
8
+ """SQL function API, factories, and built-in functions."""
9
+
10
+ from __future__ import annotations
11
+
12
+ import datetime
13
+ import decimal
14
+ from typing import Any
15
+ from typing import cast
16
+ from typing import Dict
17
+ from typing import List
18
+ from typing import Mapping
19
+ from typing import Optional
20
+ from typing import overload
21
+ from typing import Sequence
22
+ from typing import Tuple
23
+ from typing import Type
24
+ from typing import TYPE_CHECKING
25
+ from typing import TypeAlias
26
+ from typing import TypeVar
27
+ from typing import Union
28
+
29
+ from . import annotation
30
+ from . import coercions
31
+ from . import operators
32
+ from . import roles
33
+ from . import schema
34
+ from . import sqltypes
35
+ from . import type_api
36
+ from . import util as sqlutil
37
+ from ._typing import is_table_value_type
38
+ from .base import _entity_namespace
39
+ from .base import ColumnCollection
40
+ from .base import ExecutableStatement
41
+ from .base import Generative
42
+ from .base import HasMemoized
43
+ from .base import ReadOnlyColumnCollection
44
+ from .base import WriteableColumnCollection
45
+ from .elements import _type_from_args
46
+ from .elements import AggregateOrderBy
47
+ from .elements import BinaryExpression
48
+ from .elements import BindParameter
49
+ from .elements import Cast
50
+ from .elements import ClauseList
51
+ from .elements import ColumnElement
52
+ from .elements import Extract
53
+ from .elements import FunctionFilter
54
+ from .elements import Grouping
55
+ from .elements import literal_column
56
+ from .elements import NamedColumn
57
+ from .elements import Over
58
+ from .elements import WithinGroup
59
+ from .selectable import FromClause
60
+ from .selectable import Select
61
+ from .selectable import TableValuedAlias
62
+ from .sqltypes import TableValueType
63
+ from .type_api import TypeEngine
64
+ from .visitors import InternalTraversal
65
+ from .. import util
66
+
67
+ if TYPE_CHECKING:
68
+ from ._typing import _ByArgument
69
+ from ._typing import _ColumnExpressionArgument
70
+ from ._typing import _ColumnExpressionOrLiteralArgument
71
+ from ._typing import _ColumnExpressionOrStrLabelArgument
72
+ from ._typing import _StarOrOne
73
+ from ._typing import _TypeEngineArgument
74
+ from .base import _EntityNamespace
75
+ from .elements import _FrameIntTuple
76
+ from .elements import ClauseElement
77
+ from .elements import FrameClause
78
+ from .elements import KeyedColumnElement
79
+ from .elements import TableValuedColumn
80
+ from .operators import OperatorType
81
+ from ..engine.base import Connection
82
+ from ..engine.cursor import CursorResult
83
+ from ..engine.interfaces import _CoreMultiExecuteParams
84
+ from ..engine.interfaces import CoreExecuteOptionsParameter
85
+ from ..util.typing import Self
86
+
87
+ _T = TypeVar("_T", bound=Any)
88
+ _S = TypeVar("_S", bound=Any)
89
+
90
+ _registry: util.defaultdict[str, Dict[str, Type[Function[Any]]]] = (
91
+ util.defaultdict(dict)
92
+ )
93
+
94
+
95
+ def register_function(
96
+ identifier: str, fn: Type[Function[Any]], package: str = "_default"
97
+ ) -> None:
98
+ """Associate a callable with a particular func. name.
99
+
100
+ This is normally called by GenericFunction, but is also
101
+ available by itself so that a non-Function construct
102
+ can be associated with the :data:`.func` accessor (i.e.
103
+ CAST, EXTRACT).
104
+
105
+ """
106
+ reg = _registry[package]
107
+
108
+ identifier = str(identifier).lower()
109
+
110
+ # Check if a function with the same identifier is registered.
111
+ if identifier in reg:
112
+ util.warn(
113
+ "The GenericFunction '{}' is already registered and "
114
+ "is going to be overridden.".format(identifier)
115
+ )
116
+ reg[identifier] = fn
117
+
118
+
119
+ class FunctionElement(
120
+ ColumnElement[_T], ExecutableStatement, FromClause, Generative
121
+ ):
122
+ """Base for SQL function-oriented constructs.
123
+
124
+ This is a `generic type <https://peps.python.org/pep-0484/#generics>`_,
125
+ meaning that type checkers and IDEs can be instructed on the types to
126
+ expect in a :class:`_engine.Result` for this function. See
127
+ :class:`.GenericFunction` for an example of how this is done.
128
+
129
+ .. seealso::
130
+
131
+ :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
132
+
133
+ :class:`.Function` - named SQL function.
134
+
135
+ :data:`.func` - namespace which produces registered or ad-hoc
136
+ :class:`.Function` instances.
137
+
138
+ :class:`.GenericFunction` - allows creation of registered function
139
+ types.
140
+
141
+ """
142
+
143
+ _traverse_internals = [
144
+ ("clause_expr", InternalTraversal.dp_clauseelement),
145
+ ("_with_ordinality", InternalTraversal.dp_boolean),
146
+ ("_table_value_type", InternalTraversal.dp_has_cache_key),
147
+ ] + ExecutableStatement._executable_traverse_internals
148
+
149
+ packagenames: Tuple[str, ...] = ()
150
+
151
+ monotonic: bool = False
152
+
153
+ _has_args = False
154
+ _with_ordinality = False
155
+ _table_value_type: Optional[TableValueType] = None
156
+
157
+ # some attributes that are defined between both ColumnElement and
158
+ # FromClause are set to Any here to avoid typing errors
159
+ primary_key: Any
160
+ _is_clone_of: Any
161
+
162
+ clause_expr: Grouping[Any]
163
+
164
+ def __init__(
165
+ self, *clauses: _ColumnExpressionOrLiteralArgument[Any]
166
+ ) -> None:
167
+ r"""Construct a :class:`.FunctionElement`.
168
+
169
+ :param \*clauses: list of column expressions that form the arguments
170
+ of the SQL function call.
171
+
172
+ :param \**kwargs: additional kwargs are typically consumed by
173
+ subclasses.
174
+
175
+ .. seealso::
176
+
177
+ :data:`.func`
178
+
179
+ :class:`.Function`
180
+
181
+ """
182
+ args: Sequence[_ColumnExpressionArgument[Any]] = [
183
+ coercions.expect(
184
+ roles.ExpressionElementRole,
185
+ c,
186
+ name=getattr(self, "name", None),
187
+ apply_propagate_attrs=self,
188
+ )
189
+ for c in clauses
190
+ ]
191
+ self._has_args = self._has_args or bool(args)
192
+ self.clause_expr = Grouping(
193
+ ClauseList(operator=operators.comma_op, group_contents=True, *args)
194
+ )
195
+
196
+ _non_anon_label = None
197
+
198
+ @property
199
+ def _proxy_key(self) -> Any:
200
+ return super()._proxy_key or getattr(self, "name", None)
201
+
202
+ def _execute_on_connection(
203
+ self,
204
+ connection: Connection,
205
+ distilled_params: _CoreMultiExecuteParams,
206
+ execution_options: CoreExecuteOptionsParameter,
207
+ ) -> CursorResult[Any]:
208
+ return connection._execute_function(
209
+ self, distilled_params, execution_options
210
+ )
211
+
212
+ def scalar_table_valued(
213
+ self, name: str, type_: Optional[_TypeEngineArgument[_T]] = None
214
+ ) -> ScalarFunctionColumn[_T]:
215
+ """Return a column expression that's against this
216
+ :class:`_functions.FunctionElement` as a scalar
217
+ table-valued expression.
218
+
219
+ The returned expression is similar to that returned by a single column
220
+ accessed off of a :meth:`_functions.FunctionElement.table_valued`
221
+ construct, except no FROM clause is generated; the function is rendered
222
+ in the similar way as a scalar subquery.
223
+
224
+ E.g.:
225
+
226
+ .. sourcecode:: pycon+sql
227
+
228
+ >>> from sqlalchemy import func, select
229
+ >>> fn = func.jsonb_each("{'k', 'v'}").scalar_table_valued("key")
230
+ >>> print(select(fn))
231
+ {printsql}SELECT (jsonb_each(:jsonb_each_1)).key
232
+
233
+ .. versionadded:: 1.4.0b2
234
+
235
+ .. seealso::
236
+
237
+ :meth:`_functions.FunctionElement.table_valued`
238
+
239
+ :meth:`_functions.FunctionElement.alias`
240
+
241
+ :meth:`_functions.FunctionElement.column_valued`
242
+
243
+ """ # noqa: E501
244
+
245
+ return ScalarFunctionColumn(self, name, type_)
246
+
247
+ def table_valued(
248
+ self, *expr: _ColumnExpressionOrStrLabelArgument[Any], **kw: Any
249
+ ) -> TableValuedAlias:
250
+ r"""Return a :class:`_sql.TableValuedAlias` representation of this
251
+ :class:`_functions.FunctionElement` with table-valued expressions added.
252
+
253
+ e.g. to use the SQLite form of ``generate_series()`` (including
254
+ hidden columns "start", "stop", "step"):
255
+
256
+ .. sourcecode:: pycon+sql
257
+
258
+ >>> fn = func.generate_series(1, 5).table_valued(
259
+ ... "value", "start", "stop", "step"
260
+ ... )
261
+
262
+ >>> print(select(fn))
263
+ {printsql}SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step
264
+ FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1{stop}
265
+
266
+ >>> print(select(fn.c.value, fn.c.stop).where(fn.c.value > 2))
267
+ {printsql}SELECT anon_1.value, anon_1.stop
268
+ FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1
269
+ WHERE anon_1.value > :value_1{stop}
270
+
271
+ Backends like PostgreSQL need the accessed columns to be explicitly
272
+ named in "AS" clause. To achieve this, use
273
+ :meth:`_sql.TableValuedAlias.render_derived`; be sure to consult the
274
+ :ref:`PostgreSQL-specific documentation for table valued functions
275
+ <postgresql_table_valued>` for additional examples:
276
+
277
+ .. sourcecode:: pycon+sql
278
+
279
+ >>> fn = func.generate_series(1, 5).table_valued("value").render_derived()
280
+
281
+ >>> print(select(fn))
282
+ {printsql}SELECT anon_1.value FROM
283
+ generate_series(:generate_series_1, :generate_series_2) AS anon_1(value){stop}
284
+
285
+ A WITH ORDINALITY expression may be generated by passing the keyword
286
+ argument :paramref:`.FunctionElement.table_valued.with_ordinality`,
287
+ illustrated below using PostgreSQL's syntax:
288
+
289
+ .. sourcecode:: pycon+sql
290
+
291
+ >>> fn = func.generate_series(4, 1, -1).table_valued(
292
+ ... "gen", with_ordinality="ordinality"
293
+ ... )
294
+ >>> print(select(fn.render_derived()))
295
+ {printsql}SELECT anon_1.gen, anon_1.ordinality
296
+ FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3)
297
+ WITH ORDINALITY AS anon_1(gen, ordinality)
298
+
299
+ :param \*expr: A series of string column names that will be added to the
300
+ ``.c`` collection of the resulting :class:`_sql.TableValuedAlias`
301
+ construct as columns. :func:`_sql.column` objects with or without
302
+ datatypes may also be used.
303
+
304
+ :param name: optional name to assign to the alias name that's generated.
305
+ If omitted, a unique anonymizing name is used.
306
+
307
+ :param with_ordinality: string name that when present results in the
308
+ ``WITH ORDINALITY`` clause being added to the alias, and the given
309
+ string name will be added as a column to the .c collection
310
+ of the resulting :class:`_sql.TableValuedAlias`.
311
+
312
+ :param joins_implicitly: when True, the table valued function may be
313
+ used in the FROM clause without any explicit JOIN to other tables
314
+ in the SQL query, and no "cartesian product" warning will be generated.
315
+ May be useful for SQL functions such as ``func.json_each()``.
316
+
317
+ .. versionadded:: 1.4.33
318
+
319
+ .. versionadded:: 1.4.0b2
320
+
321
+
322
+ .. seealso::
323
+
324
+ :ref:`tutorial_functions_table_valued` - in the :ref:`unified_tutorial`
325
+
326
+ :ref:`Table-Valued Functions on PostgreSQL <postgresql_table_valued>` - in the :ref:`postgresql_toplevel` documentation
327
+
328
+ :meth:`_functions.FunctionElement.scalar_table_valued` - variant of
329
+ :meth:`_functions.FunctionElement.table_valued` which delivers the
330
+ complete table valued expression as a scalar column expression
331
+
332
+ :meth:`_functions.FunctionElement.column_valued`
333
+
334
+ :meth:`_sql.TableValuedAlias.render_derived` - renders the alias
335
+ using a derived column clause, e.g. ``AS name(col1, col2, ...)``
336
+
337
+ """ # noqa: 501
338
+
339
+ new_func = self._generate()
340
+
341
+ with_ordinality = kw.pop("with_ordinality", None)
342
+ joins_implicitly = kw.pop("joins_implicitly", None)
343
+ name = kw.pop("name", None)
344
+
345
+ if with_ordinality:
346
+ expr += (with_ordinality,)
347
+ new_func._with_ordinality = True
348
+
349
+ new_func.type = new_func._table_value_type = TableValueType(*expr)
350
+
351
+ return new_func.alias(name=name, joins_implicitly=joins_implicitly)
352
+
353
+ def column_valued(
354
+ self, name: Optional[str] = None, joins_implicitly: bool = False
355
+ ) -> TableValuedColumn[_T]:
356
+ """Return this :class:`_functions.FunctionElement` as a column expression that
357
+ selects from itself as a FROM clause.
358
+
359
+ E.g.:
360
+
361
+ .. sourcecode:: pycon+sql
362
+
363
+ >>> from sqlalchemy import select, func
364
+ >>> gs = func.generate_series(1, 5, -1).column_valued()
365
+ >>> print(select(gs))
366
+ {printsql}SELECT anon_1
367
+ FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) AS anon_1
368
+
369
+ This is shorthand for::
370
+
371
+ gs = func.generate_series(1, 5, -1).alias().column
372
+
373
+ :param name: optional name to assign to the alias name that's generated.
374
+ If omitted, a unique anonymizing name is used.
375
+
376
+ :param joins_implicitly: when True, the "table" portion of the column
377
+ valued function may be a member of the FROM clause without any
378
+ explicit JOIN to other tables in the SQL query, and no "cartesian
379
+ product" warning will be generated. May be useful for SQL functions
380
+ such as ``func.json_array_elements()``.
381
+
382
+ .. versionadded:: 1.4.46
383
+
384
+ .. seealso::
385
+
386
+ :ref:`tutorial_functions_column_valued` - in the :ref:`unified_tutorial`
387
+
388
+ :ref:`postgresql_column_valued` - in the :ref:`postgresql_toplevel` documentation
389
+
390
+ :meth:`_functions.FunctionElement.table_valued`
391
+
392
+ """ # noqa: 501
393
+
394
+ return self.alias(name=name, joins_implicitly=joins_implicitly).column
395
+
396
+ @util.ro_non_memoized_property
397
+ def columns(
398
+ self,
399
+ ) -> ReadOnlyColumnCollection[str, KeyedColumnElement[Any]]:
400
+ r"""The set of columns exported by this :class:`.FunctionElement`.
401
+
402
+ This is a placeholder collection that allows the function to be
403
+ placed in the FROM clause of a statement:
404
+
405
+ .. sourcecode:: pycon+sql
406
+
407
+ >>> from sqlalchemy import column, select, func
408
+ >>> stmt = select(column("x"), column("y")).select_from(func.myfunction())
409
+ >>> print(stmt)
410
+ {printsql}SELECT x, y FROM myfunction()
411
+
412
+ The above form is a legacy feature that is now superseded by the
413
+ fully capable :meth:`_functions.FunctionElement.table_valued`
414
+ method; see that method for details.
415
+
416
+ .. seealso::
417
+
418
+ :meth:`_functions.FunctionElement.table_valued` - generates table-valued
419
+ SQL function expressions.
420
+
421
+ """ # noqa: E501
422
+ return self.c
423
+
424
+ @util.ro_memoized_property
425
+ def c(self) -> ReadOnlyColumnCollection[str, KeyedColumnElement[Any]]:
426
+ """synonym for :attr:`.FunctionElement.columns`."""
427
+
428
+ return WriteableColumnCollection(
429
+ columns=[(col.key, col) for col in self._all_selected_columns]
430
+ ).as_readonly()
431
+
432
+ @property
433
+ def _all_selected_columns(self) -> Sequence[KeyedColumnElement[Any]]:
434
+ if is_table_value_type(self.type):
435
+ # TODO: this might not be fully accurate
436
+ cols = cast(
437
+ "Sequence[KeyedColumnElement[Any]]", self.type._elements
438
+ )
439
+ else:
440
+ cols = [self.label(None)]
441
+
442
+ return cols
443
+
444
+ @property
445
+ def exported_columns( # type: ignore[override]
446
+ self,
447
+ ) -> ColumnCollection[str, KeyedColumnElement[Any]]:
448
+ return self.columns
449
+
450
+ @HasMemoized.memoized_attribute
451
+ def clauses(self) -> ClauseList:
452
+ """Return the underlying :class:`.ClauseList` which contains
453
+ the arguments for this :class:`.FunctionElement`.
454
+
455
+ """
456
+ return cast(ClauseList, self.clause_expr.element)
457
+
458
+ def over(
459
+ self,
460
+ *,
461
+ partition_by: _ByArgument | None = None,
462
+ order_by: _ByArgument | None = None,
463
+ rows: _FrameIntTuple | FrameClause | None = None,
464
+ range_: _FrameIntTuple | FrameClause | None = None,
465
+ groups: _FrameIntTuple | FrameClause | None = None,
466
+ exclude: str | None = None,
467
+ ) -> Over[_T]:
468
+ """Produce an OVER clause against this function.
469
+
470
+ Used against aggregate or so-called "window" functions,
471
+ for database backends that support window functions.
472
+
473
+ The expression::
474
+
475
+ func.row_number().over(order_by="x")
476
+
477
+ is shorthand for::
478
+
479
+ from sqlalchemy import over
480
+
481
+ over(func.row_number(), order_by="x")
482
+
483
+ See :func:`_expression.over` for a full description.
484
+
485
+ .. seealso::
486
+
487
+ :func:`_expression.over`
488
+
489
+ :ref:`tutorial_window_functions` - in the :ref:`unified_tutorial`
490
+
491
+ """
492
+ return Over(
493
+ self,
494
+ partition_by=partition_by,
495
+ order_by=order_by,
496
+ rows=rows,
497
+ range_=range_,
498
+ groups=groups,
499
+ exclude=exclude,
500
+ )
501
+
502
+ def aggregate_order_by(
503
+ self, *order_by: _ColumnExpressionArgument[Any]
504
+ ) -> AggregateOrderBy[_T]:
505
+ r"""Produce a :class:`.AggregateOrderBy` object against a function.
506
+
507
+ Used for aggregating functions such as :class:`_functions.array_agg`,
508
+ ``group_concat``, ``json_agg`` on backends that support ordering via an
509
+ embedded ORDER BY parameter, e.g. PostgreSQL, MySQL/MariaDB, SQLite.
510
+ When used on backends like Oracle and SQL Server, SQL compilation uses
511
+ that of :class:`.WithinGroup`.
512
+
513
+ See :func:`_expression.aggregate_order_by` for a full description.
514
+
515
+ .. versionadded:: 2.1 Generalized the PostgreSQL-specific
516
+ :func:`_postgresql.aggregate_order_by` function to a method on
517
+ :class:`.Function` that is backend agnostic.
518
+
519
+ .. seealso::
520
+
521
+ :class:`_functions.aggregate_strings` - backend-agnostic string
522
+ concatenation function which also supports ORDER BY
523
+
524
+ """
525
+
526
+ return AggregateOrderBy(self, *order_by)
527
+
528
+ def within_group(
529
+ self, *order_by: _ColumnExpressionArgument[Any]
530
+ ) -> WithinGroup[_T]:
531
+ """Produce a WITHIN GROUP (ORDER BY expr) clause against this function.
532
+
533
+ Used against so-called "ordered set aggregate" and "hypothetical
534
+ set aggregate" functions, including :class:`.percentile_cont`,
535
+ :class:`.rank`, :class:`.dense_rank`, etc. This feature is typically
536
+ used by PostgreSQL, Oracle Database, and Microsoft SQL Server.
537
+
538
+ For simple ORDER BY expressions within aggregate functions on
539
+ PostgreSQL, MySQL/MariaDB, SQLite, see :func:`_sql.aggregate_order_by`.
540
+
541
+ See :func:`_expression.within_group` for a full description.
542
+
543
+ .. seealso::
544
+
545
+ :ref:`tutorial_functions_within_group` -
546
+ in the :ref:`unified_tutorial`
547
+
548
+
549
+ """
550
+ return WithinGroup(self, *order_by)
551
+
552
+ @overload
553
+ def filter(self) -> Self: ...
554
+
555
+ @overload
556
+ def filter(
557
+ self,
558
+ __criterion0: _ColumnExpressionArgument[bool],
559
+ *criterion: _ColumnExpressionArgument[bool],
560
+ ) -> FunctionFilter[_T]: ...
561
+
562
+ def filter(
563
+ self, *criterion: _ColumnExpressionArgument[bool]
564
+ ) -> Union[Self, FunctionFilter[_T]]:
565
+ """Produce a FILTER clause against this function.
566
+
567
+ Used against aggregate and window functions,
568
+ for database backends that support the "FILTER" clause.
569
+
570
+ The expression::
571
+
572
+ func.count(1).filter(True)
573
+
574
+ is shorthand for::
575
+
576
+ from sqlalchemy import funcfilter
577
+
578
+ funcfilter(func.count(1), True)
579
+
580
+ .. seealso::
581
+
582
+ :ref:`tutorial_functions_within_group` -
583
+ in the :ref:`unified_tutorial`
584
+
585
+ :class:`.FunctionFilter`
586
+
587
+ :func:`.funcfilter`
588
+
589
+
590
+ """
591
+ if not criterion:
592
+ return self
593
+ return FunctionFilter(self, *criterion)
594
+
595
+ def as_comparison(
596
+ self, left_index: int, right_index: int
597
+ ) -> FunctionAsBinary:
598
+ """Interpret this expression as a boolean comparison between two
599
+ values.
600
+
601
+ This method is used for an ORM use case described at
602
+ :ref:`relationship_custom_operator_sql_function`.
603
+
604
+ A hypothetical SQL function "is_equal()" which compares to values
605
+ for equality would be written in the Core expression language as::
606
+
607
+ expr = func.is_equal("a", "b")
608
+
609
+ If "is_equal()" above is comparing "a" and "b" for equality, the
610
+ :meth:`.FunctionElement.as_comparison` method would be invoked as::
611
+
612
+ expr = func.is_equal("a", "b").as_comparison(1, 2)
613
+
614
+ Where above, the integer value "1" refers to the first argument of the
615
+ "is_equal()" function and the integer value "2" refers to the second.
616
+
617
+ This would create a :class:`.BinaryExpression` that is equivalent to::
618
+
619
+ BinaryExpression("a", "b", operator=op.eq)
620
+
621
+ However, at the SQL level it would still render as
622
+ "is_equal('a', 'b')".
623
+
624
+ The ORM, when it loads a related object or collection, needs to be able
625
+ to manipulate the "left" and "right" sides of the ON clause of a JOIN
626
+ expression. The purpose of this method is to provide a SQL function
627
+ construct that can also supply this information to the ORM, when used
628
+ with the :paramref:`_orm.relationship.primaryjoin` parameter. The
629
+ return value is a containment object called :class:`.FunctionAsBinary`.
630
+
631
+ An ORM example is as follows::
632
+
633
+ class Venue(Base):
634
+ __tablename__ = "venue"
635
+ id = Column(Integer, primary_key=True)
636
+ name = Column(String)
637
+
638
+ descendants = relationship(
639
+ "Venue",
640
+ primaryjoin=func.instr(
641
+ remote(foreign(name)), name + "/"
642
+ ).as_comparison(1, 2)
643
+ == 1,
644
+ viewonly=True,
645
+ order_by=name,
646
+ )
647
+
648
+ Above, the "Venue" class can load descendant "Venue" objects by
649
+ determining if the name of the parent Venue is contained within the
650
+ start of the hypothetical descendant value's name, e.g. "parent1" would
651
+ match up to "parent1/child1", but not to "parent2/child1".
652
+
653
+ Possible use cases include the "materialized path" example given above,
654
+ as well as making use of special SQL functions such as geometric
655
+ functions to create join conditions.
656
+
657
+ :param left_index: the integer 1-based index of the function argument
658
+ that serves as the "left" side of the expression.
659
+ :param right_index: the integer 1-based index of the function argument
660
+ that serves as the "right" side of the expression.
661
+
662
+ .. seealso::
663
+
664
+ :ref:`relationship_custom_operator_sql_function` -
665
+ example use within the ORM
666
+
667
+ """
668
+ return FunctionAsBinary(self, left_index, right_index)
669
+
670
+ @property
671
+ def _from_objects(self) -> Any:
672
+ return self.clauses._from_objects
673
+
674
+ def within_group_type(
675
+ self, within_group: WithinGroup[_S]
676
+ ) -> Optional[TypeEngine[_S]]:
677
+ """For types that define their return type as based on the criteria
678
+ within a WITHIN GROUP (ORDER BY) expression, called by the
679
+ :class:`.WithinGroup` construct.
680
+
681
+ Returns None by default, in which case the function's normal ``.type``
682
+ is used.
683
+
684
+ """
685
+
686
+ return None
687
+
688
+ def alias(
689
+ self, name: Optional[str] = None, joins_implicitly: bool = False
690
+ ) -> TableValuedAlias:
691
+ r"""Produce a :class:`_expression.Alias` construct against this
692
+ :class:`.FunctionElement`.
693
+
694
+ .. tip::
695
+
696
+ The :meth:`_functions.FunctionElement.alias` method is part of the
697
+ mechanism by which "table valued" SQL functions are created.
698
+ However, most use cases are covered by higher level methods on
699
+ :class:`_functions.FunctionElement` including
700
+ :meth:`_functions.FunctionElement.table_valued`, and
701
+ :meth:`_functions.FunctionElement.column_valued`.
702
+
703
+ This construct wraps the function in a named alias which
704
+ is suitable for the FROM clause, in the style accepted for example
705
+ by PostgreSQL. A column expression is also provided using the
706
+ special ``.column`` attribute, which may
707
+ be used to refer to the output of the function as a scalar value
708
+ in the columns or where clause, for a backend such as PostgreSQL.
709
+
710
+ For a full table-valued expression, use the
711
+ :meth:`_functions.FunctionElement.table_valued` method first to
712
+ establish named columns.
713
+
714
+ e.g.:
715
+
716
+ .. sourcecode:: pycon+sql
717
+
718
+ >>> from sqlalchemy import func, select, column
719
+ >>> data_view = func.unnest([1, 2, 3]).alias("data_view")
720
+ >>> print(select(data_view.column))
721
+ {printsql}SELECT data_view
722
+ FROM unnest(:unnest_1) AS data_view
723
+
724
+ The :meth:`_functions.FunctionElement.column_valued` method provides
725
+ a shortcut for the above pattern:
726
+
727
+ .. sourcecode:: pycon+sql
728
+
729
+ >>> data_view = func.unnest([1, 2, 3]).column_valued("data_view")
730
+ >>> print(select(data_view))
731
+ {printsql}SELECT data_view
732
+ FROM unnest(:unnest_1) AS data_view
733
+
734
+ .. versionadded:: 1.4.0b2 Added the ``.column`` accessor
735
+
736
+ :param name: alias name, will be rendered as ``AS <name>`` in the
737
+ FROM clause
738
+
739
+ :param joins_implicitly: when True, the table valued function may be
740
+ used in the FROM clause without any explicit JOIN to other tables
741
+ in the SQL query, and no "cartesian product" warning will be
742
+ generated. May be useful for SQL functions such as
743
+ ``func.json_each()``.
744
+
745
+ .. versionadded:: 1.4.33
746
+
747
+ .. seealso::
748
+
749
+ :ref:`tutorial_functions_table_valued` -
750
+ in the :ref:`unified_tutorial`
751
+
752
+ :meth:`_functions.FunctionElement.table_valued`
753
+
754
+ :meth:`_functions.FunctionElement.scalar_table_valued`
755
+
756
+ :meth:`_functions.FunctionElement.column_valued`
757
+
758
+
759
+ """
760
+
761
+ return TableValuedAlias._construct(
762
+ self,
763
+ name=name,
764
+ table_value_type=self.type,
765
+ joins_implicitly=joins_implicitly,
766
+ )
767
+
768
+ def select(self) -> Select[_T]:
769
+ """Produce a :func:`_expression.select` construct
770
+ against this :class:`.FunctionElement`.
771
+
772
+ This is shorthand for::
773
+
774
+ s = select(function_element)
775
+
776
+ """
777
+ s: Select[_T] = Select(self)
778
+ if self._execution_options:
779
+ s = s.execution_options(**self._execution_options)
780
+ return s
781
+
782
+ def _bind_param(
783
+ self,
784
+ operator: OperatorType,
785
+ obj: Any,
786
+ type_: Optional[TypeEngine[_T]] = None,
787
+ expanding: bool = False,
788
+ **kw: Any,
789
+ ) -> BindParameter[_T]:
790
+ return BindParameter(
791
+ None,
792
+ obj,
793
+ _compared_to_operator=operator,
794
+ _compared_to_type=self.type,
795
+ unique=True,
796
+ type_=type_,
797
+ expanding=expanding,
798
+ **kw,
799
+ )
800
+
801
+ def self_group(self, against: Optional[OperatorType] = None) -> ClauseElement: # type: ignore[override] # noqa E501
802
+ # for the moment, we are parenthesizing all array-returning
803
+ # expressions against getitem. This may need to be made
804
+ # more portable if in the future we support other DBs
805
+ # besides postgresql.
806
+ if against is operators.getitem and isinstance(
807
+ self.type, sqltypes.ARRAY
808
+ ):
809
+ return Grouping(self)
810
+ else:
811
+ return super().self_group(against=against)
812
+
813
+ @property
814
+ def entity_namespace(self) -> _EntityNamespace:
815
+ """overrides FromClause.entity_namespace as functions are generally
816
+ column expressions and not FromClauses.
817
+
818
+ """
819
+ # ideally functions would not be fromclauses but we failed to make
820
+ # this adjustment in 1.4
821
+ return _entity_namespace(self.clause_expr)
822
+
823
+
824
+ class FunctionAsBinary(BinaryExpression[Any]):
825
+ _traverse_internals = [
826
+ ("sql_function", InternalTraversal.dp_clauseelement),
827
+ ("left_index", InternalTraversal.dp_plain_obj),
828
+ ("right_index", InternalTraversal.dp_plain_obj),
829
+ ("modifiers", InternalTraversal.dp_plain_dict),
830
+ ]
831
+
832
+ sql_function: FunctionElement[Any]
833
+ left_index: int
834
+ right_index: int
835
+
836
+ def _gen_cache_key(self, anon_map: Any, bindparams: Any) -> Any:
837
+ return ColumnElement._gen_cache_key(self, anon_map, bindparams)
838
+
839
+ def __init__(
840
+ self, fn: FunctionElement[Any], left_index: int, right_index: int
841
+ ) -> None:
842
+ self.sql_function = fn
843
+ self.left_index = left_index
844
+ self.right_index = right_index
845
+
846
+ self.operator = operators.function_as_comparison_op
847
+ self.type = sqltypes.BOOLEANTYPE
848
+ self.negate = None
849
+ self._is_implicitly_boolean = True
850
+ self.modifiers = util.immutabledict({})
851
+
852
+ @property
853
+ def left_expr(self) -> ColumnElement[Any]:
854
+ return self.sql_function.clauses.clauses[self.left_index - 1]
855
+
856
+ @left_expr.setter
857
+ def left_expr(self, value: ColumnElement[Any]) -> None:
858
+ self.sql_function.clauses.clauses[self.left_index - 1] = value
859
+
860
+ @property
861
+ def right_expr(self) -> ColumnElement[Any]:
862
+ return self.sql_function.clauses.clauses[self.right_index - 1]
863
+
864
+ @right_expr.setter
865
+ def right_expr(self, value: ColumnElement[Any]) -> None:
866
+ self.sql_function.clauses.clauses[self.right_index - 1] = value
867
+
868
+ if not TYPE_CHECKING:
869
+ # mypy can't accommodate @property to replace an instance
870
+ # variable
871
+
872
+ left = left_expr
873
+ right = right_expr
874
+
875
+
876
+ class ScalarFunctionColumn(NamedColumn[_T]):
877
+ __visit_name__ = "scalar_function_column"
878
+
879
+ _traverse_internals = [
880
+ ("name", InternalTraversal.dp_anon_name),
881
+ ("type", InternalTraversal.dp_type),
882
+ ("fn", InternalTraversal.dp_clauseelement),
883
+ ]
884
+
885
+ is_literal = False
886
+ table = None
887
+
888
+ def __init__(
889
+ self,
890
+ fn: FunctionElement[_T],
891
+ name: str,
892
+ type_: Optional[_TypeEngineArgument[_T]] = None,
893
+ ) -> None:
894
+ self.fn = fn
895
+ self.name = name
896
+
897
+ # if type is None, we get NULLTYPE, which is our _T. But I don't
898
+ # know how to get the overloads to express that correctly
899
+ self.type = type_api.to_instance(type_) # type: ignore
900
+
901
+
902
+ class _FunctionGenerator:
903
+ """Generate SQL function expressions.
904
+
905
+ :data:`.func` is a special object instance which generates SQL
906
+ functions based on name-based attributes, e.g.:
907
+
908
+ .. sourcecode:: pycon+sql
909
+
910
+ >>> print(func.count(1))
911
+ {printsql}count(:param_1)
912
+
913
+ The returned object is an instance of :class:`.Function`, and is a
914
+ column-oriented SQL element like any other, and is used in that way:
915
+
916
+ .. sourcecode:: pycon+sql
917
+
918
+ >>> print(select(func.count(table.c.id)))
919
+ {printsql}SELECT count(sometable.id) FROM sometable
920
+
921
+ Any name can be given to :data:`.func`. If the function name is unknown to
922
+ SQLAlchemy, it will be rendered exactly as is. For common SQL functions
923
+ which SQLAlchemy is aware of, the name may be interpreted as a *generic
924
+ function* which will be compiled appropriately to the target database:
925
+
926
+ .. sourcecode:: pycon+sql
927
+
928
+ >>> print(func.current_timestamp())
929
+ {printsql}CURRENT_TIMESTAMP
930
+
931
+ To call functions which are present in dot-separated packages,
932
+ specify them in the same manner:
933
+
934
+ .. sourcecode:: pycon+sql
935
+
936
+ >>> print(func.stats.yield_curve(5, 10))
937
+ {printsql}stats.yield_curve(:yield_curve_1, :yield_curve_2)
938
+
939
+ SQLAlchemy can be made aware of the return type of functions to enable
940
+ type-specific lexical and result-based behavior. For example, to ensure
941
+ that a string-based function returns a Unicode value and is similarly
942
+ treated as a string in expressions, specify
943
+ :class:`~sqlalchemy.types.Unicode` as the type:
944
+
945
+ .. sourcecode:: pycon+sql
946
+
947
+ >>> print(
948
+ ... func.my_string("hi", type_=Unicode)
949
+ ... + " "
950
+ ... + func.my_string("there", type_=Unicode)
951
+ ... )
952
+ {printsql}my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3)
953
+
954
+ The object returned by a :data:`.func` call is usually an instance of
955
+ :class:`.Function`.
956
+ This object meets the "column" interface, including comparison and labeling
957
+ functions. The object can also be passed the :meth:`~.Connectable.execute`
958
+ method of a :class:`_engine.Connection` or :class:`_engine.Engine`,
959
+ where it will be
960
+ wrapped inside of a SELECT statement first::
961
+
962
+ print(connection.execute(func.current_timestamp()).scalar())
963
+
964
+ In a few exception cases, the :data:`.func` accessor
965
+ will redirect a name to a built-in expression such as :func:`.cast`
966
+ or :func:`.extract`, as these names have well-known meaning
967
+ but are not exactly the same as "functions" from a SQLAlchemy
968
+ perspective.
969
+
970
+ Functions which are interpreted as "generic" functions know how to
971
+ calculate their return type automatically. For a listing of known generic
972
+ functions, see :ref:`generic_functions`.
973
+
974
+ .. note::
975
+
976
+ The :data:`.func` construct has only limited support for calling
977
+ standalone "stored procedures", especially those with special
978
+ parameterization concerns.
979
+
980
+ See the section :ref:`stored_procedures` for details on how to use
981
+ the DBAPI-level ``callproc()`` method for fully traditional stored
982
+ procedures.
983
+
984
+ .. seealso::
985
+
986
+ :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
987
+
988
+ :class:`.Function`
989
+
990
+ """ # noqa
991
+
992
+ def __init__(self, **opts: Any) -> None:
993
+ self.__names: List[str] = []
994
+ self.opts = opts
995
+
996
+ def __getattr__(self, name: str) -> _FunctionGenerator:
997
+ # passthru __ attributes; fixes pydoc
998
+ if name.startswith("__"):
999
+ try:
1000
+ return self.__dict__[name] # type: ignore
1001
+ except KeyError:
1002
+ raise AttributeError(name)
1003
+
1004
+ elif name.endswith("_"):
1005
+ name = name[0:-1]
1006
+ f = _FunctionGenerator(**self.opts)
1007
+ f.__names = list(self.__names) + [name]
1008
+ return f
1009
+
1010
+ @overload
1011
+ def __call__(
1012
+ self, *c: Any, type_: _TypeEngineArgument[_T], **kwargs: Any
1013
+ ) -> Function[_T]: ...
1014
+
1015
+ @overload
1016
+ def __call__(self, *c: Any, **kwargs: Any) -> Function[Any]: ...
1017
+
1018
+ def __call__(self, *c: Any, **kwargs: Any) -> Function[Any]:
1019
+ o = self.opts.copy()
1020
+ o.update(kwargs)
1021
+
1022
+ tokens = len(self.__names)
1023
+
1024
+ if tokens == 2:
1025
+ package, fname = self.__names
1026
+ elif tokens == 1:
1027
+ package, fname = "_default", self.__names[0]
1028
+ else:
1029
+ package = None
1030
+
1031
+ if package is not None:
1032
+ func = _registry[package].get(fname.lower())
1033
+ if func is not None:
1034
+ return func(*c, **o)
1035
+
1036
+ return Function(
1037
+ self.__names[-1], packagenames=tuple(self.__names[0:-1]), *c, **o
1038
+ )
1039
+
1040
+ if TYPE_CHECKING:
1041
+ # START GENERATED FUNCTION ACCESSORS
1042
+
1043
+ # code within this block is **programmatically,
1044
+ # statically generated** by tools/generate_sql_functions.py
1045
+
1046
+ @property
1047
+ def aggregate_strings(self) -> Type[_aggregate_strings_func]: ...
1048
+
1049
+ @property
1050
+ def ansifunction(self) -> Type[_AnsiFunction_func[Any]]: ...
1051
+
1052
+ # set ColumnElement[_T] as a separate overload, to appease
1053
+ # mypy which seems to not want to accept _T from
1054
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1055
+ # _HasClauseElement as of mypy 1.15
1056
+
1057
+ @overload
1058
+ def array_agg(
1059
+ self,
1060
+ col: ColumnElement[_T],
1061
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1062
+ **kwargs: Any,
1063
+ ) -> _array_agg_func[_T]: ...
1064
+
1065
+ @overload
1066
+ def array_agg(
1067
+ self,
1068
+ col: _ColumnExpressionArgument[_T],
1069
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1070
+ **kwargs: Any,
1071
+ ) -> _array_agg_func[_T]: ...
1072
+
1073
+ @overload
1074
+ def array_agg(
1075
+ self,
1076
+ col: _T,
1077
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1078
+ **kwargs: Any,
1079
+ ) -> _array_agg_func[_T]: ...
1080
+
1081
+ def array_agg(
1082
+ self,
1083
+ col: _ColumnExpressionOrLiteralArgument[_T],
1084
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1085
+ **kwargs: Any,
1086
+ ) -> _array_agg_func[_T]: ...
1087
+
1088
+ @property
1089
+ def cast(self) -> Type[_Cast_func[Any]]: ...
1090
+
1091
+ @property
1092
+ def char_length(self) -> Type[_char_length_func]: ...
1093
+
1094
+ # set ColumnElement[_T] as a separate overload, to appease
1095
+ # mypy which seems to not want to accept _T from
1096
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1097
+ # _HasClauseElement as of mypy 1.15
1098
+
1099
+ @overload
1100
+ def coalesce(
1101
+ self,
1102
+ col: ColumnElement[_T],
1103
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1104
+ **kwargs: Any,
1105
+ ) -> _coalesce_func[_T]: ...
1106
+
1107
+ @overload
1108
+ def coalesce(
1109
+ self,
1110
+ col: _ColumnExpressionArgument[Optional[_T]],
1111
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1112
+ **kwargs: Any,
1113
+ ) -> _coalesce_func[_T]: ...
1114
+
1115
+ @overload
1116
+ def coalesce(
1117
+ self,
1118
+ col: Optional[_T],
1119
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1120
+ **kwargs: Any,
1121
+ ) -> _coalesce_func[_T]: ...
1122
+
1123
+ def coalesce(
1124
+ self,
1125
+ col: _ColumnExpressionOrLiteralArgument[Optional[_T]],
1126
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1127
+ **kwargs: Any,
1128
+ ) -> _coalesce_func[_T]: ...
1129
+
1130
+ @property
1131
+ def concat(self) -> Type[_concat_func]: ...
1132
+
1133
+ @property
1134
+ def count(self) -> Type[_count_func]: ...
1135
+
1136
+ @property
1137
+ def cube(self) -> Type[_cube_func[Any]]: ...
1138
+
1139
+ @property
1140
+ def cume_dist(self) -> Type[_cume_dist_func]: ...
1141
+
1142
+ @property
1143
+ def current_date(self) -> Type[_current_date_func]: ...
1144
+
1145
+ @property
1146
+ def current_time(self) -> Type[_current_time_func]: ...
1147
+
1148
+ @property
1149
+ def current_timestamp(self) -> Type[_current_timestamp_func]: ...
1150
+
1151
+ @property
1152
+ def current_user(self) -> Type[_current_user_func]: ...
1153
+
1154
+ @property
1155
+ def dense_rank(self) -> Type[_dense_rank_func]: ...
1156
+
1157
+ @property
1158
+ def extract(self) -> Type[_Extract_func]: ...
1159
+
1160
+ @property
1161
+ def grouping_sets(self) -> Type[_grouping_sets_func[Any]]: ...
1162
+
1163
+ @property
1164
+ def localtime(self) -> Type[_localtime_func]: ...
1165
+
1166
+ @property
1167
+ def localtimestamp(self) -> Type[_localtimestamp_func]: ...
1168
+
1169
+ # set ColumnElement[_T] as a separate overload, to appease
1170
+ # mypy which seems to not want to accept _T from
1171
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1172
+ # _HasClauseElement as of mypy 1.15
1173
+
1174
+ @overload
1175
+ def max( # noqa: A001
1176
+ self,
1177
+ col: ColumnElement[_T],
1178
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1179
+ **kwargs: Any,
1180
+ ) -> _max_func[_T]: ...
1181
+
1182
+ @overload
1183
+ def max( # noqa: A001
1184
+ self,
1185
+ col: _ColumnExpressionArgument[_T],
1186
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1187
+ **kwargs: Any,
1188
+ ) -> _max_func[_T]: ...
1189
+
1190
+ @overload
1191
+ def max( # noqa: A001
1192
+ self,
1193
+ col: _T,
1194
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1195
+ **kwargs: Any,
1196
+ ) -> _max_func[_T]: ...
1197
+
1198
+ def max( # noqa: A001
1199
+ self,
1200
+ col: _ColumnExpressionOrLiteralArgument[_T],
1201
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1202
+ **kwargs: Any,
1203
+ ) -> _max_func[_T]: ...
1204
+
1205
+ # set ColumnElement[_T] as a separate overload, to appease
1206
+ # mypy which seems to not want to accept _T from
1207
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1208
+ # _HasClauseElement as of mypy 1.15
1209
+
1210
+ @overload
1211
+ def min( # noqa: A001
1212
+ self,
1213
+ col: ColumnElement[_T],
1214
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1215
+ **kwargs: Any,
1216
+ ) -> _min_func[_T]: ...
1217
+
1218
+ @overload
1219
+ def min( # noqa: A001
1220
+ self,
1221
+ col: _ColumnExpressionArgument[_T],
1222
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1223
+ **kwargs: Any,
1224
+ ) -> _min_func[_T]: ...
1225
+
1226
+ @overload
1227
+ def min( # noqa: A001
1228
+ self,
1229
+ col: _T,
1230
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1231
+ **kwargs: Any,
1232
+ ) -> _min_func[_T]: ...
1233
+
1234
+ def min( # noqa: A001
1235
+ self,
1236
+ col: _ColumnExpressionOrLiteralArgument[_T],
1237
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1238
+ **kwargs: Any,
1239
+ ) -> _min_func[_T]: ...
1240
+
1241
+ @property
1242
+ def mode(self) -> Type[_mode_func[Any]]: ...
1243
+
1244
+ @property
1245
+ def next_value(self) -> Type[_next_value_func]: ...
1246
+
1247
+ @property
1248
+ def now(self) -> Type[_now_func]: ...
1249
+
1250
+ @property
1251
+ def orderedsetagg(self) -> Type[_OrderedSetAgg_func[Any]]: ...
1252
+
1253
+ @property
1254
+ def percent_rank(self) -> Type[_percent_rank_func]: ...
1255
+
1256
+ @property
1257
+ def percentile_cont(self) -> Type[_percentile_cont_func[Any]]: ...
1258
+
1259
+ @property
1260
+ def percentile_disc(self) -> Type[_percentile_disc_func[Any]]: ...
1261
+
1262
+ # set ColumnElement[_T] as a separate overload, to appease
1263
+ # mypy which seems to not want to accept _T from
1264
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1265
+ # _HasClauseElement as of mypy 1.15
1266
+
1267
+ @overload
1268
+ def pow( # noqa: A001
1269
+ self,
1270
+ col: ColumnElement[_T],
1271
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1272
+ **kwargs: Any,
1273
+ ) -> _pow_func[_T]: ...
1274
+
1275
+ @overload
1276
+ def pow( # noqa: A001
1277
+ self,
1278
+ col: _ColumnExpressionArgument[_T],
1279
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1280
+ **kwargs: Any,
1281
+ ) -> _pow_func[_T]: ...
1282
+
1283
+ @overload
1284
+ def pow( # noqa: A001
1285
+ self,
1286
+ col: _T,
1287
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1288
+ **kwargs: Any,
1289
+ ) -> _pow_func[_T]: ...
1290
+
1291
+ def pow( # noqa: A001
1292
+ self,
1293
+ col: _ColumnExpressionOrLiteralArgument[_T],
1294
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1295
+ **kwargs: Any,
1296
+ ) -> _pow_func[_T]: ...
1297
+
1298
+ @property
1299
+ def random(self) -> Type[_random_func]: ...
1300
+
1301
+ @property
1302
+ def rank(self) -> Type[_rank_func]: ...
1303
+
1304
+ @property
1305
+ def rollup(self) -> Type[_rollup_func[Any]]: ...
1306
+
1307
+ @property
1308
+ def session_user(self) -> Type[_session_user_func]: ...
1309
+
1310
+ # set ColumnElement[_T] as a separate overload, to appease
1311
+ # mypy which seems to not want to accept _T from
1312
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1313
+ # _HasClauseElement as of mypy 1.15
1314
+
1315
+ @overload
1316
+ def sum( # noqa: A001
1317
+ self,
1318
+ col: ColumnElement[_T],
1319
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1320
+ **kwargs: Any,
1321
+ ) -> _sum_func[_T]: ...
1322
+
1323
+ @overload
1324
+ def sum( # noqa: A001
1325
+ self,
1326
+ col: _ColumnExpressionArgument[_T],
1327
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1328
+ **kwargs: Any,
1329
+ ) -> _sum_func[_T]: ...
1330
+
1331
+ @overload
1332
+ def sum( # noqa: A001
1333
+ self,
1334
+ col: _T,
1335
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1336
+ **kwargs: Any,
1337
+ ) -> _sum_func[_T]: ...
1338
+
1339
+ def sum( # noqa: A001
1340
+ self,
1341
+ col: _ColumnExpressionOrLiteralArgument[_T],
1342
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1343
+ **kwargs: Any,
1344
+ ) -> _sum_func[_T]: ...
1345
+
1346
+ @property
1347
+ def sysdate(self) -> Type[_sysdate_func]: ...
1348
+
1349
+ @property
1350
+ def user(self) -> Type[_user_func]: ...
1351
+
1352
+ # END GENERATED FUNCTION ACCESSORS
1353
+
1354
+
1355
+ func = _FunctionGenerator()
1356
+ func.__doc__ = _FunctionGenerator.__doc__
1357
+
1358
+ modifier = _FunctionGenerator(group=False)
1359
+
1360
+
1361
+ class Function(FunctionElement[_T]):
1362
+ r"""Describe a named SQL function.
1363
+
1364
+ The :class:`.Function` object is typically generated from the
1365
+ :data:`.func` generation object.
1366
+
1367
+
1368
+ :param \*clauses: list of column expressions that form the arguments
1369
+ of the SQL function call.
1370
+
1371
+ :param type\_: optional :class:`.TypeEngine` datatype object that will be
1372
+ used as the return value of the column expression generated by this
1373
+ function call.
1374
+
1375
+ :param packagenames: a string which indicates package prefix names
1376
+ to be prepended to the function name when the SQL is generated.
1377
+ The :data:`.func` generator creates these when it is called using
1378
+ dotted format, e.g.::
1379
+
1380
+ func.mypackage.some_function(col1, col2)
1381
+
1382
+ .. seealso::
1383
+
1384
+ :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
1385
+
1386
+ :data:`.func` - namespace which produces registered or ad-hoc
1387
+ :class:`.Function` instances.
1388
+
1389
+ :class:`.GenericFunction` - allows creation of registered function
1390
+ types.
1391
+
1392
+ """
1393
+
1394
+ __visit_name__ = "function"
1395
+
1396
+ _traverse_internals = FunctionElement._traverse_internals + [
1397
+ ("packagenames", InternalTraversal.dp_plain_obj),
1398
+ ("name", InternalTraversal.dp_string),
1399
+ ("type", InternalTraversal.dp_type),
1400
+ ]
1401
+
1402
+ name: str
1403
+
1404
+ identifier: str
1405
+
1406
+ type: TypeEngine[_T]
1407
+ """A :class:`_types.TypeEngine` object which refers to the SQL return
1408
+ type represented by this SQL function.
1409
+
1410
+ This datatype may be configured when generating a
1411
+ :class:`_functions.Function` object by passing the
1412
+ :paramref:`_functions.Function.type_` parameter, e.g.::
1413
+
1414
+ >>> select(func.lower("some VALUE", type_=String))
1415
+
1416
+ The small number of built-in classes of :class:`_functions.Function` come
1417
+ with a built-in datatype that's appropriate to the class of function and
1418
+ its arguments. For functions that aren't known, the type defaults to the
1419
+ "null type".
1420
+
1421
+ """
1422
+
1423
+ @overload
1424
+ def __init__(
1425
+ self,
1426
+ name: str,
1427
+ *clauses: _ColumnExpressionOrLiteralArgument[_T],
1428
+ type_: None = ...,
1429
+ packagenames: Optional[Tuple[str, ...]] = ...,
1430
+ monotonic: bool = ...,
1431
+ ) -> None: ...
1432
+
1433
+ @overload
1434
+ def __init__(
1435
+ self,
1436
+ name: str,
1437
+ *clauses: _ColumnExpressionOrLiteralArgument[Any],
1438
+ type_: _TypeEngineArgument[_T] = ...,
1439
+ packagenames: Optional[Tuple[str, ...]] = ...,
1440
+ monotonic: bool = ...,
1441
+ ) -> None: ...
1442
+
1443
+ def __init__(
1444
+ self,
1445
+ name: str,
1446
+ *clauses: _ColumnExpressionOrLiteralArgument[Any],
1447
+ type_: Optional[_TypeEngineArgument[_T]] = None,
1448
+ packagenames: Optional[Tuple[str, ...]] = None,
1449
+ monotonic: bool = False,
1450
+ ) -> None:
1451
+ """Construct a :class:`.Function`.
1452
+
1453
+ The :data:`.func` construct is normally used to construct
1454
+ new :class:`.Function` instances.
1455
+
1456
+ """
1457
+ self.packagenames = packagenames or ()
1458
+ self.name = name
1459
+ self.monotonic = monotonic
1460
+
1461
+ # if type is None, we get NULLTYPE, which is our _T. But I don't
1462
+ # know how to get the overloads to express that correctly
1463
+ self.type = type_api.to_instance(type_) # type: ignore
1464
+
1465
+ FunctionElement.__init__(self, *clauses)
1466
+
1467
+ def _bind_param(
1468
+ self,
1469
+ operator: OperatorType,
1470
+ obj: Any,
1471
+ type_: Optional[TypeEngine[_T]] = None,
1472
+ expanding: bool = False,
1473
+ **kw: Any,
1474
+ ) -> BindParameter[_T]:
1475
+ return BindParameter(
1476
+ self.name,
1477
+ obj,
1478
+ _compared_to_operator=operator,
1479
+ _compared_to_type=self.type,
1480
+ type_=type_,
1481
+ unique=True,
1482
+ expanding=expanding,
1483
+ **kw,
1484
+ )
1485
+
1486
+
1487
+ class GenericFunction(Function[_T]):
1488
+ """Define a 'generic' function.
1489
+
1490
+ A generic function is a pre-established :class:`.Function`
1491
+ class that is instantiated automatically when called
1492
+ by name from the :data:`.func` attribute. Note that
1493
+ calling any name from :data:`.func` has the effect that
1494
+ a new :class:`.Function` instance is created automatically,
1495
+ given that name. The primary use case for defining
1496
+ a :class:`.GenericFunction` class is so that a function
1497
+ of a particular name may be given a fixed return type.
1498
+ It can also include custom argument parsing schemes as well
1499
+ as additional methods.
1500
+
1501
+ Subclasses of :class:`.GenericFunction` are automatically
1502
+ registered under the name of the class. For
1503
+ example, a user-defined function ``as_utc()`` would
1504
+ be available immediately::
1505
+
1506
+ from sqlalchemy.sql.functions import GenericFunction
1507
+ from sqlalchemy.types import DateTime
1508
+
1509
+
1510
+ class as_utc(GenericFunction):
1511
+ type = DateTime()
1512
+ inherit_cache = True
1513
+
1514
+
1515
+ print(select(func.as_utc()))
1516
+
1517
+ User-defined generic functions can be organized into
1518
+ packages by specifying the "package" attribute when defining
1519
+ :class:`.GenericFunction`. Third party libraries
1520
+ containing many functions may want to use this in order
1521
+ to avoid name conflicts with other systems. For example,
1522
+ if our ``as_utc()`` function were part of a package
1523
+ "time"::
1524
+
1525
+ class as_utc(GenericFunction):
1526
+ type = DateTime()
1527
+ package = "time"
1528
+ inherit_cache = True
1529
+
1530
+ The above function would be available from :data:`.func`
1531
+ using the package name ``time``::
1532
+
1533
+ print(select(func.time.as_utc()))
1534
+
1535
+ A final option is to allow the function to be accessed
1536
+ from one name in :data:`.func` but to render as a different name.
1537
+ The ``identifier`` attribute will override the name used to
1538
+ access the function as loaded from :data:`.func`, but will retain
1539
+ the usage of ``name`` as the rendered name::
1540
+
1541
+ class GeoBuffer(GenericFunction):
1542
+ type = Geometry()
1543
+ package = "geo"
1544
+ name = "ST_Buffer"
1545
+ identifier = "buffer"
1546
+ inherit_cache = True
1547
+
1548
+ The above function will render as follows:
1549
+
1550
+ .. sourcecode:: pycon+sql
1551
+
1552
+ >>> print(func.geo.buffer())
1553
+ {printsql}ST_Buffer()
1554
+
1555
+ The name will be rendered as is, however without quoting unless the name
1556
+ contains special characters that require quoting. To force quoting
1557
+ on or off for the name, use the :class:`.sqlalchemy.sql.quoted_name`
1558
+ construct::
1559
+
1560
+ from sqlalchemy.sql import quoted_name
1561
+
1562
+
1563
+ class GeoBuffer(GenericFunction):
1564
+ type = Geometry()
1565
+ package = "geo"
1566
+ name = quoted_name("ST_Buffer", True)
1567
+ identifier = "buffer"
1568
+ inherit_cache = True
1569
+
1570
+ The above function will render as:
1571
+
1572
+ .. sourcecode:: pycon+sql
1573
+
1574
+ >>> print(func.geo.buffer())
1575
+ {printsql}"ST_Buffer"()
1576
+
1577
+ Type parameters for this class as a
1578
+ `generic type <https://peps.python.org/pep-0484/#generics>`_ can be passed
1579
+ and should match the type seen in a :class:`_engine.Result`. For example::
1580
+
1581
+ class as_utc(GenericFunction[datetime.datetime]):
1582
+ type = DateTime()
1583
+ inherit_cache = True
1584
+
1585
+ The above indicates that the following expression returns a ``datetime``
1586
+ object::
1587
+
1588
+ connection.scalar(select(func.as_utc()))
1589
+
1590
+ """
1591
+
1592
+ coerce_arguments = True
1593
+ inherit_cache = True
1594
+
1595
+ _register: bool
1596
+
1597
+ name = "GenericFunction"
1598
+
1599
+ def __init_subclass__(cls) -> None:
1600
+ if annotation.Annotated not in cls.__mro__:
1601
+ cls._register_generic_function(cls.__name__, cls.__dict__)
1602
+ super().__init_subclass__()
1603
+
1604
+ @classmethod
1605
+ def _register_generic_function(
1606
+ cls, clsname: str, clsdict: Mapping[str, Any]
1607
+ ) -> None:
1608
+ cls.name = name = clsdict.get("name", clsname)
1609
+ cls.identifier = identifier = clsdict.get("identifier", name)
1610
+ package = clsdict.get("package", "_default")
1611
+ # legacy
1612
+ if "__return_type__" in clsdict:
1613
+ cls.type = clsdict["__return_type__"]
1614
+
1615
+ # Check _register attribute status
1616
+ cls._register = getattr(cls, "_register", True)
1617
+
1618
+ # Register the function if required
1619
+ if cls._register:
1620
+ register_function(identifier, cls, package)
1621
+ else:
1622
+ # Set _register to True to register child classes by default
1623
+ cls._register = True
1624
+
1625
+ def __init__(
1626
+ self, *args: _ColumnExpressionOrLiteralArgument[Any], **kwargs: Any
1627
+ ) -> None:
1628
+ parsed_args = kwargs.pop("_parsed_args", None)
1629
+ if parsed_args is None:
1630
+ parsed_args = [
1631
+ coercions.expect(
1632
+ roles.ExpressionElementRole,
1633
+ c,
1634
+ name=self.name,
1635
+ apply_propagate_attrs=self,
1636
+ )
1637
+ for c in args
1638
+ ]
1639
+ self._has_args = self._has_args or bool(parsed_args)
1640
+ self.packagenames = ()
1641
+
1642
+ self.clause_expr = Grouping(
1643
+ ClauseList(
1644
+ operator=operators.comma_op, group_contents=True, *parsed_args
1645
+ )
1646
+ )
1647
+
1648
+ self.type = type_api.to_instance( # type: ignore
1649
+ kwargs.pop("type_", None) or getattr(self, "type", None)
1650
+ )
1651
+
1652
+
1653
+ register_function("cast", Cast) # type: ignore
1654
+ register_function("extract", Extract) # type: ignore
1655
+
1656
+
1657
+ class next_value(GenericFunction[int]):
1658
+ """Represent the 'next value', given a :class:`.Sequence`
1659
+ as its single argument.
1660
+
1661
+ Compiles into the appropriate function on each backend,
1662
+ or will raise NotImplementedError if used on a backend
1663
+ that does not provide support for sequences.
1664
+
1665
+ """
1666
+
1667
+ type = sqltypes.Integer()
1668
+ name = "next_value"
1669
+
1670
+ _traverse_internals = [
1671
+ ("sequence", InternalTraversal.dp_named_ddl_element)
1672
+ ]
1673
+
1674
+ def __init__(self, seq: schema.Sequence, **kw: Any) -> None:
1675
+ assert isinstance(
1676
+ seq, schema.Sequence
1677
+ ), "next_value() accepts a Sequence object as input."
1678
+ self.sequence = seq
1679
+ self.type = sqltypes.to_instance( # type: ignore
1680
+ seq.data_type or getattr(self, "type", None)
1681
+ )
1682
+
1683
+ def compare(self, other: Any, **kw: Any) -> bool:
1684
+ return (
1685
+ isinstance(other, next_value)
1686
+ and self.sequence.name == other.sequence.name
1687
+ )
1688
+
1689
+ @property
1690
+ def _from_objects(self) -> Any:
1691
+ return []
1692
+
1693
+
1694
+ class AnsiFunction(GenericFunction[_T]):
1695
+ """Define a function in "ansi" format, which doesn't render parenthesis."""
1696
+
1697
+ inherit_cache = True
1698
+
1699
+ def __init__(
1700
+ self, *args: _ColumnExpressionArgument[Any], **kwargs: Any
1701
+ ) -> None:
1702
+ GenericFunction.__init__(self, *args, **kwargs)
1703
+
1704
+
1705
+ class ReturnTypeFromArgs(GenericFunction[_T]):
1706
+ """Define a function whose return type is bound to the type of its
1707
+ arguments.
1708
+ """
1709
+
1710
+ inherit_cache = True
1711
+
1712
+ # set ColumnElement[_T] as a separate overload, to appease
1713
+ # mypy which seems to not want to accept _T from
1714
+ # _ColumnExpressionArgument. Seems somewhat related to the covariant
1715
+ # _HasClauseElement as of mypy 1.15
1716
+
1717
+ @overload
1718
+ def __init__(
1719
+ self,
1720
+ col: ColumnElement[_T],
1721
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1722
+ **kwargs: Any,
1723
+ ) -> None: ...
1724
+
1725
+ @overload
1726
+ def __init__(
1727
+ self,
1728
+ col: _ColumnExpressionArgument[_T],
1729
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1730
+ **kwargs: Any,
1731
+ ) -> None: ...
1732
+
1733
+ @overload
1734
+ def __init__(
1735
+ self,
1736
+ col: _T,
1737
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1738
+ **kwargs: Any,
1739
+ ) -> None: ...
1740
+
1741
+ def __init__(
1742
+ self, *args: _ColumnExpressionOrLiteralArgument[_T], **kwargs: Any
1743
+ ) -> None:
1744
+ fn_args: Sequence[ColumnElement[Any]] = [
1745
+ coercions.expect(
1746
+ roles.ExpressionElementRole,
1747
+ c,
1748
+ name=self.name,
1749
+ apply_propagate_attrs=self,
1750
+ )
1751
+ for c in args
1752
+ ]
1753
+ kwargs.setdefault("type_", _type_from_args(fn_args))
1754
+ kwargs["_parsed_args"] = fn_args
1755
+ super().__init__(*fn_args, **kwargs)
1756
+
1757
+
1758
+ class ReturnTypeFromOptionalArgs(ReturnTypeFromArgs[_T]):
1759
+ inherit_cache = True
1760
+
1761
+ @overload
1762
+ def __init__(
1763
+ self,
1764
+ col: ColumnElement[_T],
1765
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1766
+ **kwargs: Any,
1767
+ ) -> None: ...
1768
+
1769
+ @overload
1770
+ def __init__(
1771
+ self,
1772
+ col: _ColumnExpressionArgument[Optional[_T]],
1773
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1774
+ **kwargs: Any,
1775
+ ) -> None: ...
1776
+
1777
+ @overload
1778
+ def __init__(
1779
+ self,
1780
+ col: Optional[_T],
1781
+ *args: _ColumnExpressionOrLiteralArgument[Any],
1782
+ **kwargs: Any,
1783
+ ) -> None: ...
1784
+
1785
+ def __init__(
1786
+ self,
1787
+ *args: _ColumnExpressionOrLiteralArgument[Optional[_T]],
1788
+ **kwargs: Any,
1789
+ ) -> None:
1790
+ super().__init__(*args, **kwargs) # type: ignore
1791
+
1792
+
1793
+ class coalesce(ReturnTypeFromOptionalArgs[_T]):
1794
+ _has_args = True
1795
+ inherit_cache = True
1796
+
1797
+
1798
+ class max(ReturnTypeFromArgs[_T]): # noqa: A001
1799
+ """The SQL MAX() aggregate function."""
1800
+
1801
+ inherit_cache = True
1802
+
1803
+
1804
+ class min(ReturnTypeFromArgs[_T]): # noqa: A001
1805
+ """The SQL MIN() aggregate function."""
1806
+
1807
+ inherit_cache = True
1808
+
1809
+
1810
+ class sum(ReturnTypeFromArgs[_T]): # noqa: A001
1811
+ """The SQL SUM() aggregate function."""
1812
+
1813
+ inherit_cache = True
1814
+
1815
+
1816
+ class now(GenericFunction[datetime.datetime]):
1817
+ """The SQL now() datetime function.
1818
+
1819
+ SQLAlchemy dialects will usually render this particular function
1820
+ in a backend-specific way, such as rendering it as ``CURRENT_TIMESTAMP``.
1821
+
1822
+ """
1823
+
1824
+ type = sqltypes.DateTime()
1825
+ inherit_cache = True
1826
+
1827
+
1828
+ class pow(ReturnTypeFromArgs[_T]): # noqa: A001
1829
+ """The SQL POW() function which performs the power operator.
1830
+
1831
+ E.g.:
1832
+
1833
+ .. sourcecode:: pycon+sql
1834
+
1835
+ >>> print(select(func.pow(2, 8)))
1836
+ {printsql}SELECT pow(:pow_2, :pow_3) AS pow_1
1837
+
1838
+ .. versionadded:: 2.1
1839
+
1840
+ """
1841
+
1842
+ inherit_cache = True
1843
+
1844
+
1845
+ class concat(GenericFunction[str]):
1846
+ """The SQL CONCAT() function, which concatenates strings.
1847
+
1848
+ E.g.:
1849
+
1850
+ .. sourcecode:: pycon+sql
1851
+
1852
+ >>> print(select(func.concat("a", "b")))
1853
+ {printsql}SELECT concat(:concat_2, :concat_3) AS concat_1
1854
+
1855
+ String concatenation in SQLAlchemy is more commonly available using the
1856
+ Python ``+`` operator with string datatypes, which will render a
1857
+ backend-specific concatenation operator, such as :
1858
+
1859
+ .. sourcecode:: pycon+sql
1860
+
1861
+ >>> print(select(literal("a") + "b"))
1862
+ {printsql}SELECT :param_1 || :param_2 AS anon_1
1863
+
1864
+
1865
+ """
1866
+
1867
+ type = sqltypes.String()
1868
+ inherit_cache = True
1869
+
1870
+
1871
+ class char_length(GenericFunction[int]):
1872
+ """The CHAR_LENGTH() SQL function."""
1873
+
1874
+ type = sqltypes.Integer()
1875
+ inherit_cache = True
1876
+
1877
+ def __init__(self, arg: _ColumnExpressionArgument[str], **kw: Any) -> None:
1878
+ # slight hack to limit to just one positional argument
1879
+ # not sure why this one function has this special treatment
1880
+ super().__init__(arg, **kw)
1881
+
1882
+
1883
+ class random(GenericFunction[float]):
1884
+ """The RANDOM() SQL function."""
1885
+
1886
+ _has_args = True
1887
+ inherit_cache = True
1888
+
1889
+
1890
+ class count(GenericFunction[int]):
1891
+ r"""The ANSI COUNT aggregate function. With no arguments,
1892
+ emits COUNT \*.
1893
+
1894
+ E.g.::
1895
+
1896
+ from sqlalchemy import func
1897
+ from sqlalchemy import select
1898
+ from sqlalchemy import table, column
1899
+
1900
+ my_table = table("some_table", column("id"))
1901
+
1902
+ stmt = select(func.count()).select_from(my_table)
1903
+
1904
+ Executing ``stmt`` would emit:
1905
+
1906
+ .. sourcecode:: sql
1907
+
1908
+ SELECT count(*) AS count_1
1909
+ FROM some_table
1910
+
1911
+
1912
+ """
1913
+
1914
+ type = sqltypes.Integer()
1915
+ inherit_cache = True
1916
+
1917
+ def __init__(
1918
+ self,
1919
+ expression: Union[
1920
+ _ColumnExpressionArgument[Any], _StarOrOne, None
1921
+ ] = None,
1922
+ **kwargs: Any,
1923
+ ) -> None:
1924
+ if expression is None:
1925
+ expression = literal_column("*")
1926
+ super().__init__(expression, **kwargs)
1927
+
1928
+
1929
+ class current_date(AnsiFunction[datetime.date]):
1930
+ """The CURRENT_DATE() SQL function."""
1931
+
1932
+ type = sqltypes.Date()
1933
+ inherit_cache = True
1934
+
1935
+
1936
+ class current_time(AnsiFunction[datetime.time]):
1937
+ """The CURRENT_TIME() SQL function."""
1938
+
1939
+ type = sqltypes.Time()
1940
+ inherit_cache = True
1941
+
1942
+
1943
+ class current_timestamp(AnsiFunction[datetime.datetime]):
1944
+ """The CURRENT_TIMESTAMP() SQL function."""
1945
+
1946
+ type = sqltypes.DateTime()
1947
+ inherit_cache = True
1948
+
1949
+
1950
+ class current_user(AnsiFunction[str]):
1951
+ """The CURRENT_USER() SQL function."""
1952
+
1953
+ type = sqltypes.String()
1954
+ inherit_cache = True
1955
+
1956
+
1957
+ class localtime(AnsiFunction[datetime.datetime]):
1958
+ """The localtime() SQL function."""
1959
+
1960
+ type = sqltypes.DateTime()
1961
+ inherit_cache = True
1962
+
1963
+
1964
+ class localtimestamp(AnsiFunction[datetime.datetime]):
1965
+ """The localtimestamp() SQL function."""
1966
+
1967
+ type = sqltypes.DateTime()
1968
+ inherit_cache = True
1969
+
1970
+
1971
+ class session_user(AnsiFunction[str]):
1972
+ """The SESSION_USER() SQL function."""
1973
+
1974
+ type = sqltypes.String()
1975
+ inherit_cache = True
1976
+
1977
+
1978
+ class sysdate(AnsiFunction[datetime.datetime]):
1979
+ """The SYSDATE() SQL function."""
1980
+
1981
+ type = sqltypes.DateTime()
1982
+ inherit_cache = True
1983
+
1984
+
1985
+ class user(AnsiFunction[str]):
1986
+ """The USER() SQL function."""
1987
+
1988
+ type = sqltypes.String()
1989
+ inherit_cache = True
1990
+
1991
+
1992
+ class array_agg(ReturnTypeFromArgs[Sequence[_T]]):
1993
+ """Support for the ARRAY_AGG function.
1994
+
1995
+ The ``func.array_agg(expr)`` construct returns an expression of
1996
+ type :class:`_types.ARRAY`.
1997
+
1998
+ e.g.::
1999
+
2000
+ stmt = select(func.array_agg(table.c.values)[2:5])
2001
+
2002
+ .. seealso::
2003
+
2004
+ :func:`_postgresql.array_agg` - PostgreSQL-specific version that
2005
+ returns :class:`_postgresql.ARRAY`, which has PG-specific operators
2006
+ added.
2007
+
2008
+ """
2009
+
2010
+ inherit_cache = True
2011
+
2012
+ def __init__(
2013
+ self, *args: _ColumnExpressionArgument[Any], **kwargs: Any
2014
+ ) -> None:
2015
+ fn_args: Sequence[ColumnElement[Any]] = [
2016
+ coercions.expect(
2017
+ roles.ExpressionElementRole, c, apply_propagate_attrs=self
2018
+ )
2019
+ for c in args
2020
+ ]
2021
+
2022
+ default_array_type = kwargs.pop("_default_array_type", sqltypes.ARRAY)
2023
+ if "type_" not in kwargs:
2024
+ type_from_args = _type_from_args(fn_args)
2025
+ if isinstance(type_from_args, sqltypes.ARRAY):
2026
+ kwargs["type_"] = type_from_args
2027
+ else:
2028
+ kwargs["type_"] = default_array_type(
2029
+ type_from_args, dimensions=1
2030
+ )
2031
+ kwargs["_parsed_args"] = fn_args
2032
+ super().__init__(*fn_args, **kwargs)
2033
+
2034
+
2035
+ class OrderedSetAgg(GenericFunction[_T]):
2036
+ """Define a function where the return type is based on the sort
2037
+ expression type as defined by the expression passed to the
2038
+ :meth:`.FunctionElement.within_group` method."""
2039
+
2040
+ array_for_multi_clause = False
2041
+ inherit_cache = True
2042
+
2043
+ def within_group_type(
2044
+ self, within_group: WithinGroup[Any]
2045
+ ) -> TypeEngine[Any]:
2046
+ func_clauses = cast(ClauseList, self.clause_expr.element)
2047
+ order_by: Sequence[ColumnElement[Any]] = sqlutil.unwrap_order_by(
2048
+ within_group.order_by
2049
+ )
2050
+ if self.array_for_multi_clause and len(func_clauses.clauses) > 1:
2051
+ return sqltypes.ARRAY(order_by[0].type)
2052
+ else:
2053
+ return order_by[0].type
2054
+
2055
+
2056
+ class mode(OrderedSetAgg[_T]):
2057
+ """Implement the ``mode`` ordered-set aggregate function.
2058
+
2059
+ This function must be used with the :meth:`.FunctionElement.within_group`
2060
+ modifier to supply a sort expression to operate upon.
2061
+
2062
+ The return type of this function is the same as the sort expression.
2063
+
2064
+ """
2065
+
2066
+ inherit_cache = True
2067
+
2068
+
2069
+ class percentile_cont(OrderedSetAgg[_T]):
2070
+ """Implement the ``percentile_cont`` ordered-set aggregate function.
2071
+
2072
+ This function must be used with the :meth:`.FunctionElement.within_group`
2073
+ modifier to supply a sort expression to operate upon.
2074
+
2075
+ The return type of this function is the same as the sort expression,
2076
+ or if the arguments are an array, an :class:`_types.ARRAY` of the sort
2077
+ expression's type.
2078
+
2079
+ """
2080
+
2081
+ array_for_multi_clause = True
2082
+ inherit_cache = True
2083
+
2084
+
2085
+ class percentile_disc(OrderedSetAgg[_T]):
2086
+ """Implement the ``percentile_disc`` ordered-set aggregate function.
2087
+
2088
+ This function must be used with the :meth:`.FunctionElement.within_group`
2089
+ modifier to supply a sort expression to operate upon.
2090
+
2091
+ The return type of this function is the same as the sort expression,
2092
+ or if the arguments are an array, an :class:`_types.ARRAY` of the sort
2093
+ expression's type.
2094
+
2095
+ """
2096
+
2097
+ array_for_multi_clause = True
2098
+ inherit_cache = True
2099
+
2100
+
2101
+ class rank(GenericFunction[int]):
2102
+ """Implement the ``rank`` hypothetical-set aggregate function.
2103
+
2104
+ This function must be used with the :meth:`.FunctionElement.within_group`
2105
+ modifier to supply a sort expression to operate upon.
2106
+
2107
+ The return type of this function is :class:`.Integer`.
2108
+
2109
+ """
2110
+
2111
+ type = sqltypes.Integer()
2112
+ inherit_cache = True
2113
+
2114
+
2115
+ class dense_rank(GenericFunction[int]):
2116
+ """Implement the ``dense_rank`` hypothetical-set aggregate function.
2117
+
2118
+ This function must be used with the :meth:`.FunctionElement.within_group`
2119
+ modifier to supply a sort expression to operate upon.
2120
+
2121
+ The return type of this function is :class:`.Integer`.
2122
+
2123
+ """
2124
+
2125
+ type = sqltypes.Integer()
2126
+ inherit_cache = True
2127
+
2128
+
2129
+ class percent_rank(GenericFunction[decimal.Decimal]):
2130
+ """Implement the ``percent_rank`` hypothetical-set aggregate function.
2131
+
2132
+ This function must be used with the :meth:`.FunctionElement.within_group`
2133
+ modifier to supply a sort expression to operate upon.
2134
+
2135
+ The return type of this function is :class:`.Numeric`.
2136
+
2137
+ """
2138
+
2139
+ type: sqltypes.Numeric[decimal.Decimal] = sqltypes.Numeric()
2140
+ inherit_cache = True
2141
+
2142
+
2143
+ class cume_dist(GenericFunction[decimal.Decimal]):
2144
+ """Implement the ``cume_dist`` hypothetical-set aggregate function.
2145
+
2146
+ This function must be used with the :meth:`.FunctionElement.within_group`
2147
+ modifier to supply a sort expression to operate upon.
2148
+
2149
+ The return type of this function is :class:`.Numeric`.
2150
+
2151
+ """
2152
+
2153
+ type: sqltypes.Numeric[decimal.Decimal] = sqltypes.Numeric()
2154
+ inherit_cache = True
2155
+
2156
+
2157
+ class cube(GenericFunction[_T]):
2158
+ r"""Implement the ``CUBE`` grouping operation.
2159
+
2160
+ This function is used as part of the GROUP BY of a statement,
2161
+ e.g. :meth:`_expression.Select.group_by`::
2162
+
2163
+ stmt = select(
2164
+ func.sum(table.c.value), table.c.col_1, table.c.col_2
2165
+ ).group_by(func.cube(table.c.col_1, table.c.col_2))
2166
+
2167
+ """
2168
+
2169
+ _has_args = True
2170
+ inherit_cache = True
2171
+
2172
+
2173
+ class rollup(GenericFunction[_T]):
2174
+ r"""Implement the ``ROLLUP`` grouping operation.
2175
+
2176
+ This function is used as part of the GROUP BY of a statement,
2177
+ e.g. :meth:`_expression.Select.group_by`::
2178
+
2179
+ stmt = select(
2180
+ func.sum(table.c.value), table.c.col_1, table.c.col_2
2181
+ ).group_by(func.rollup(table.c.col_1, table.c.col_2))
2182
+
2183
+ """
2184
+
2185
+ _has_args = True
2186
+ inherit_cache = True
2187
+
2188
+
2189
+ class grouping_sets(GenericFunction[_T]):
2190
+ r"""Implement the ``GROUPING SETS`` grouping operation.
2191
+
2192
+ This function is used as part of the GROUP BY of a statement,
2193
+ e.g. :meth:`_expression.Select.group_by`::
2194
+
2195
+ stmt = select(
2196
+ func.sum(table.c.value), table.c.col_1, table.c.col_2
2197
+ ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))
2198
+
2199
+ In order to group by multiple sets, use the :func:`.tuple_` construct::
2200
+
2201
+ from sqlalchemy import tuple_
2202
+
2203
+ stmt = select(
2204
+ func.sum(table.c.value), table.c.col_1, table.c.col_2, table.c.col_3
2205
+ ).group_by(
2206
+ func.grouping_sets(
2207
+ tuple_(table.c.col_1, table.c.col_2),
2208
+ tuple_(table.c.value, table.c.col_3),
2209
+ )
2210
+ )
2211
+
2212
+ """ # noqa: E501
2213
+
2214
+ _has_args = True
2215
+ inherit_cache = True
2216
+
2217
+
2218
+ class aggregate_strings(GenericFunction[str]):
2219
+ """Implement a generic string aggregation function.
2220
+
2221
+ This function will concatenate non-null values into a string and
2222
+ separate the values by a delimiter.
2223
+
2224
+ This function is compiled on a per-backend basis, into functions
2225
+ such as ``group_concat()``, ``string_agg()``, or ``LISTAGG()``.
2226
+
2227
+ e.g. Example usage with delimiter '.'::
2228
+
2229
+ stmt = select(func.aggregate_strings(table.c.str_col, "."))
2230
+
2231
+ .. versionadded:: 2.0.21
2232
+
2233
+ To add ordering to the expression, use the
2234
+ :meth:`_functions.FunctionElement.aggregate_order_by` modifier method,
2235
+ which will emit ORDER BY within the appropriate part of the column
2236
+ expression (varies by backend)::
2237
+
2238
+ stmt = select(
2239
+ func.aggregate_strings(table.c.str_col, ".").aggregate_order_by(
2240
+ table.c.str_col
2241
+ )
2242
+ )
2243
+
2244
+ .. versionadded:: 2.1 added :meth:`_functions.FunctionElement.aggregate_order_by`
2245
+ for all aggregate functions.
2246
+
2247
+ :param clause: the SQL expression to be concatenated
2248
+
2249
+ :param separator: separator string
2250
+
2251
+
2252
+ """ # noqa: E501
2253
+
2254
+ type = sqltypes.String()
2255
+ _has_args = True
2256
+ inherit_cache = True
2257
+
2258
+ def __init__(
2259
+ self,
2260
+ clause: _ColumnExpressionArgument[Any],
2261
+ separator: str,
2262
+ ) -> None:
2263
+ super().__init__(clause, separator)
2264
+
2265
+
2266
+ # These aliases are required to avoid shadowing the class with the function
2267
+ # name. See https://github.com/sqlalchemy/sqlalchemy/issues/13167
2268
+ # START GENERATED FUNCTION ALIASES
2269
+ _aggregate_strings_func: TypeAlias = aggregate_strings
2270
+ _AnsiFunction_func: TypeAlias = AnsiFunction[_T]
2271
+ _array_agg_func: TypeAlias = array_agg[_T]
2272
+ _Cast_func: TypeAlias = Cast[_T]
2273
+ _char_length_func: TypeAlias = char_length
2274
+ _coalesce_func: TypeAlias = coalesce[_T]
2275
+ _concat_func: TypeAlias = concat
2276
+ _count_func: TypeAlias = count
2277
+ _cube_func: TypeAlias = cube[_T]
2278
+ _cume_dist_func: TypeAlias = cume_dist
2279
+ _current_date_func: TypeAlias = current_date
2280
+ _current_time_func: TypeAlias = current_time
2281
+ _current_timestamp_func: TypeAlias = current_timestamp
2282
+ _current_user_func: TypeAlias = current_user
2283
+ _dense_rank_func: TypeAlias = dense_rank
2284
+ _Extract_func: TypeAlias = Extract
2285
+ _grouping_sets_func: TypeAlias = grouping_sets[_T]
2286
+ _localtime_func: TypeAlias = localtime
2287
+ _localtimestamp_func: TypeAlias = localtimestamp
2288
+ _max_func: TypeAlias = max[_T]
2289
+ _min_func: TypeAlias = min[_T]
2290
+ _mode_func: TypeAlias = mode[_T]
2291
+ _next_value_func: TypeAlias = next_value
2292
+ _now_func: TypeAlias = now
2293
+ _OrderedSetAgg_func: TypeAlias = OrderedSetAgg[_T]
2294
+ _percent_rank_func: TypeAlias = percent_rank
2295
+ _percentile_cont_func: TypeAlias = percentile_cont[_T]
2296
+ _percentile_disc_func: TypeAlias = percentile_disc[_T]
2297
+ _pow_func: TypeAlias = pow[_T]
2298
+ _random_func: TypeAlias = random
2299
+ _rank_func: TypeAlias = rank
2300
+ _rollup_func: TypeAlias = rollup[_T]
2301
+ _session_user_func: TypeAlias = session_user
2302
+ _sum_func: TypeAlias = sum[_T]
2303
+ _sysdate_func: TypeAlias = sysdate
2304
+ _user_func: TypeAlias = user
2305
+ # END GENERATED FUNCTION ALIASES