functions.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. # sql/functions.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """SQL function API, factories, and built-in functions.
  8. """
  9. from . import annotation
  10. from . import coercions
  11. from . import operators
  12. from . import roles
  13. from . import schema
  14. from . import sqltypes
  15. from . import util as sqlutil
  16. from .base import _entity_namespace
  17. from .base import ColumnCollection
  18. from .base import Executable
  19. from .base import Generative
  20. from .base import HasMemoized
  21. from .elements import _type_from_args
  22. from .elements import BinaryExpression
  23. from .elements import BindParameter
  24. from .elements import Cast
  25. from .elements import ClauseList
  26. from .elements import ColumnElement
  27. from .elements import Extract
  28. from .elements import FunctionFilter
  29. from .elements import Grouping
  30. from .elements import literal_column
  31. from .elements import NamedColumn
  32. from .elements import Over
  33. from .elements import WithinGroup
  34. from .selectable import FromClause
  35. from .selectable import Select
  36. from .selectable import TableValuedAlias
  37. from .visitors import InternalTraversal
  38. from .visitors import TraversibleType
  39. from .. import util
  40. _registry = util.defaultdict(dict)
  41. def register_function(identifier, fn, package="_default"):
  42. """Associate a callable with a particular func. name.
  43. This is normally called by _GenericMeta, but is also
  44. available by itself so that a non-Function construct
  45. can be associated with the :data:`.func` accessor (i.e.
  46. CAST, EXTRACT).
  47. """
  48. reg = _registry[package]
  49. identifier = util.text_type(identifier).lower()
  50. # Check if a function with the same identifier is registered.
  51. if identifier in reg:
  52. util.warn(
  53. "The GenericFunction '{}' is already registered and "
  54. "is going to be overridden.".format(identifier)
  55. )
  56. reg[identifier] = fn
  57. class FunctionElement(Executable, ColumnElement, FromClause, Generative):
  58. """Base for SQL function-oriented constructs.
  59. .. seealso::
  60. :ref:`coretutorial_functions` - in the Core tutorial
  61. :class:`.Function` - named SQL function.
  62. :data:`.func` - namespace which produces registered or ad-hoc
  63. :class:`.Function` instances.
  64. :class:`.GenericFunction` - allows creation of registered function
  65. types.
  66. """
  67. _traverse_internals = [
  68. ("clause_expr", InternalTraversal.dp_clauseelement),
  69. ("_with_ordinality", InternalTraversal.dp_boolean),
  70. ("_table_value_type", InternalTraversal.dp_has_cache_key),
  71. ]
  72. packagenames = ()
  73. _has_args = False
  74. _with_ordinality = False
  75. _table_value_type = None
  76. def __init__(self, *clauses, **kwargs):
  77. r"""Construct a :class:`.FunctionElement`.
  78. :param \*clauses: list of column expressions that form the arguments
  79. of the SQL function call.
  80. :param \**kwargs: additional kwargs are typically consumed by
  81. subclasses.
  82. .. seealso::
  83. :data:`.func`
  84. :class:`.Function`
  85. """
  86. args = [
  87. coercions.expect(
  88. roles.ExpressionElementRole,
  89. c,
  90. name=getattr(self, "name", None),
  91. apply_propagate_attrs=self,
  92. )
  93. for c in clauses
  94. ]
  95. self._has_args = self._has_args or bool(args)
  96. self.clause_expr = ClauseList(
  97. operator=operators.comma_op, group_contents=True, *args
  98. ).self_group()
  99. _non_anon_label = None
  100. @property
  101. def _proxy_key(self):
  102. return super(FunctionElement, self)._proxy_key or getattr(
  103. self, "name", None
  104. )
  105. def _execute_on_connection(
  106. self, connection, multiparams, params, execution_options
  107. ):
  108. return connection._execute_function(
  109. self, multiparams, params, execution_options
  110. )
  111. def scalar_table_valued(self, name, type_=None):
  112. """Return a column expression that's against this
  113. :class:`_functions.FunctionElement` as a scalar
  114. table-valued expression.
  115. The returned expression is similar to that returned by a single column
  116. accessed off of a :meth:`_functions.FunctionElement.table_valued`
  117. construct, except no FROM clause is generated; the function is rendered
  118. in the similar way as a scalar subquery.
  119. E.g.::
  120. >>> from sqlalchemy import func, select
  121. >>> fn = func.jsonb_each("{'k', 'v'}").scalar_table_valued("key")
  122. >>> print(select(fn))
  123. SELECT (jsonb_each(:jsonb_each_1)).key
  124. .. versionadded:: 1.4.0b2
  125. .. seealso::
  126. :meth:`_functions.FunctionElement.table_valued`
  127. :meth:`_functions.FunctionElement.alias`
  128. :meth:`_functions.FunctionElement.column_valued`
  129. """ # noqa E501
  130. return ScalarFunctionColumn(self, name, type_)
  131. def table_valued(self, *expr, **kw):
  132. r"""Return a :class:`_sql.TableValuedAlias` representation of this
  133. :class:`_functions.FunctionElement` with table-valued expressions added.
  134. e.g.::
  135. >>> fn = (
  136. ... func.generate_series(1, 5).
  137. ... table_valued("value", "start", "stop", "step")
  138. ... )
  139. >>> print(select(fn))
  140. SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step
  141. FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1
  142. >>> print(select(fn.c.value, fn.c.stop).where(fn.c.value > 2))
  143. SELECT anon_1.value, anon_1.stop
  144. FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1
  145. WHERE anon_1.value > :value_1
  146. A WITH ORDINALITY expression may be generated by passing the keyword
  147. argument "with_ordinality"::
  148. >>> fn = func.generate_series(4, 1, -1).table_valued("gen", with_ordinality="ordinality")
  149. >>> print(select(fn))
  150. SELECT anon_1.gen, anon_1.ordinality
  151. FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) WITH ORDINALITY AS anon_1
  152. :param \*expr: A series of string column names that will be added to the
  153. ``.c`` collection of the resulting :class:`_sql.TableValuedAlias`
  154. construct as columns. :func:`_sql.column` objects with or without
  155. datatypes may also be used.
  156. :param name: optional name to assign to the alias name that's generated.
  157. If omitted, a unique anonymizing name is used.
  158. :param with_ordinality: string name that when present results in the
  159. ``WITH ORDINALITY`` clause being added to the alias, and the given
  160. string name will be added as a column to the .c collection
  161. of the resulting :class:`_sql.TableValuedAlias`.
  162. .. versionadded:: 1.4.0b2
  163. .. seealso::
  164. :ref:`tutorial_functions_table_valued` - in the :ref:`unified_tutorial`
  165. :ref:`postgresql_table_valued` - in the :ref:`postgresql_toplevel` documentation
  166. :meth:`_functions.FunctionElement.scalar_table_valued` - variant of
  167. :meth:`_functions.FunctionElement.table_valued` which delivers the
  168. complete table valued expression as a scalar column expression
  169. :meth:`_functions.FunctionElement.column_valued`
  170. :meth:`_sql.TableValuedAlias.render_derived` - renders the alias
  171. using a derived column clause, e.g. ``AS name(col1, col2, ...)``
  172. """ # noqa 501
  173. new_func = self._generate()
  174. with_ordinality = kw.pop("with_ordinality", None)
  175. name = kw.pop("name", None)
  176. if with_ordinality:
  177. expr += (with_ordinality,)
  178. new_func._with_ordinality = True
  179. new_func.type = new_func._table_value_type = sqltypes.TableValueType(
  180. *expr
  181. )
  182. return new_func.alias(name=name)
  183. def column_valued(self, name=None):
  184. """Return this :class:`_functions.FunctionElement` as a column expression that
  185. selects from itself as a FROM clause.
  186. E.g.::
  187. >>> from sqlalchemy import select, func
  188. >>> gs = func.generate_series(1, 5, -1).column_valued()
  189. >>> print(select(gs))
  190. SELECT anon_1
  191. FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) AS anon_1
  192. This is shorthand for::
  193. gs = func.generate_series(1, 5, -1).alias().column
  194. .. seealso::
  195. :ref:`tutorial_functions_column_valued` - in the :ref:`unified_tutorial`
  196. :ref:`postgresql_column_valued` - in the :ref:`postgresql_toplevel` documentation
  197. :meth:`_functions.FunctionElement.table_valued`
  198. """ # noqa 501
  199. return self.alias(name=name).column
  200. @property
  201. def columns(self):
  202. r"""The set of columns exported by this :class:`.FunctionElement`.
  203. This is a placeholder collection that allows the function to be
  204. placed in the FROM clause of a statement::
  205. >>> from sqlalchemy import column, select, func
  206. >>> stmt = select(column('x'), column('y')).select_from(func.myfunction())
  207. >>> print(stmt)
  208. SELECT x, y FROM myfunction()
  209. The above form is a legacy feature that is now superseded by the
  210. fully capable :meth:`_functions.FunctionElement.table_valued`
  211. method; see that method for details.
  212. .. seealso::
  213. :meth:`_functions.FunctionElement.table_valued` - generates table-valued
  214. SQL function expressions.
  215. """ # noqa E501
  216. return ColumnCollection(
  217. columns=[(col.key, col) for col in self._all_selected_columns]
  218. )
  219. @property
  220. def _all_selected_columns(self):
  221. if self.type._is_table_value:
  222. cols = self.type._elements
  223. else:
  224. cols = [self.label(None)]
  225. return cols
  226. @property
  227. def exported_columns(self):
  228. return self.columns
  229. @HasMemoized.memoized_attribute
  230. def clauses(self):
  231. """Return the underlying :class:`.ClauseList` which contains
  232. the arguments for this :class:`.FunctionElement`.
  233. """
  234. return self.clause_expr.element
  235. def over(self, partition_by=None, order_by=None, rows=None, range_=None):
  236. """Produce an OVER clause against this function.
  237. Used against aggregate or so-called "window" functions,
  238. for database backends that support window functions.
  239. The expression::
  240. func.row_number().over(order_by='x')
  241. is shorthand for::
  242. from sqlalchemy import over
  243. over(func.row_number(), order_by='x')
  244. See :func:`_expression.over` for a full description.
  245. .. seealso::
  246. :func:`_expression.over`
  247. :ref:`tutorial_window_functions` - in the :ref:`unified_tutorial`
  248. """
  249. return Over(
  250. self,
  251. partition_by=partition_by,
  252. order_by=order_by,
  253. rows=rows,
  254. range_=range_,
  255. )
  256. def within_group(self, *order_by):
  257. """Produce a WITHIN GROUP (ORDER BY expr) clause against this function.
  258. Used against so-called "ordered set aggregate" and "hypothetical
  259. set aggregate" functions, including :class:`.percentile_cont`,
  260. :class:`.rank`, :class:`.dense_rank`, etc.
  261. See :func:`_expression.within_group` for a full description.
  262. .. versionadded:: 1.1
  263. .. seealso::
  264. :ref:`tutorial_functions_within_group` -
  265. in the :ref:`unified_tutorial`
  266. """
  267. return WithinGroup(self, *order_by)
  268. def filter(self, *criterion):
  269. """Produce a FILTER clause against this function.
  270. Used against aggregate and window functions,
  271. for database backends that support the "FILTER" clause.
  272. The expression::
  273. func.count(1).filter(True)
  274. is shorthand for::
  275. from sqlalchemy import funcfilter
  276. funcfilter(func.count(1), True)
  277. .. versionadded:: 1.0.0
  278. .. seealso::
  279. :ref:`tutorial_functions_within_group` -
  280. in the :ref:`unified_tutorial`
  281. :class:`.FunctionFilter`
  282. :func:`.funcfilter`
  283. """
  284. if not criterion:
  285. return self
  286. return FunctionFilter(self, *criterion)
  287. def as_comparison(self, left_index, right_index):
  288. """Interpret this expression as a boolean comparison between two values.
  289. This method is used for an ORM use case described at
  290. :ref:`relationship_custom_operator_sql_function`.
  291. A hypothetical SQL function "is_equal()" which compares to values
  292. for equality would be written in the Core expression language as::
  293. expr = func.is_equal("a", "b")
  294. If "is_equal()" above is comparing "a" and "b" for equality, the
  295. :meth:`.FunctionElement.as_comparison` method would be invoked as::
  296. expr = func.is_equal("a", "b").as_comparison(1, 2)
  297. Where above, the integer value "1" refers to the first argument of the
  298. "is_equal()" function and the integer value "2" refers to the second.
  299. This would create a :class:`.BinaryExpression` that is equivalent to::
  300. BinaryExpression("a", "b", operator=op.eq)
  301. However, at the SQL level it would still render as
  302. "is_equal('a', 'b')".
  303. The ORM, when it loads a related object or collection, needs to be able
  304. to manipulate the "left" and "right" sides of the ON clause of a JOIN
  305. expression. The purpose of this method is to provide a SQL function
  306. construct that can also supply this information to the ORM, when used
  307. with the :paramref:`_orm.relationship.primaryjoin` parameter. The
  308. return value is a containment object called :class:`.FunctionAsBinary`.
  309. An ORM example is as follows::
  310. class Venue(Base):
  311. __tablename__ = 'venue'
  312. id = Column(Integer, primary_key=True)
  313. name = Column(String)
  314. descendants = relationship(
  315. "Venue",
  316. primaryjoin=func.instr(
  317. remote(foreign(name)), name + "/"
  318. ).as_comparison(1, 2) == 1,
  319. viewonly=True,
  320. order_by=name
  321. )
  322. Above, the "Venue" class can load descendant "Venue" objects by
  323. determining if the name of the parent Venue is contained within the
  324. start of the hypothetical descendant value's name, e.g. "parent1" would
  325. match up to "parent1/child1", but not to "parent2/child1".
  326. Possible use cases include the "materialized path" example given above,
  327. as well as making use of special SQL functions such as geometric
  328. functions to create join conditions.
  329. :param left_index: the integer 1-based index of the function argument
  330. that serves as the "left" side of the expression.
  331. :param right_index: the integer 1-based index of the function argument
  332. that serves as the "right" side of the expression.
  333. .. versionadded:: 1.3
  334. .. seealso::
  335. :ref:`relationship_custom_operator_sql_function` -
  336. example use within the ORM
  337. """
  338. return FunctionAsBinary(self, left_index, right_index)
  339. @property
  340. def _from_objects(self):
  341. return self.clauses._from_objects
  342. def within_group_type(self, within_group):
  343. """For types that define their return type as based on the criteria
  344. within a WITHIN GROUP (ORDER BY) expression, called by the
  345. :class:`.WithinGroup` construct.
  346. Returns None by default, in which case the function's normal ``.type``
  347. is used.
  348. """
  349. return None
  350. def alias(self, name=None):
  351. r"""Produce a :class:`_expression.Alias` construct against this
  352. :class:`.FunctionElement`.
  353. .. tip::
  354. The :meth:`_functions.FunctionElement.alias` method is part of the
  355. mechanism by which "table valued" SQL functions are created.
  356. However, most use cases are covered by higher level methods on
  357. :class:`_functions.FunctionElement` including
  358. :meth:`_functions.FunctionElement.table_valued`, and
  359. :meth:`_functions.FunctionElement.column_valued`.
  360. This construct wraps the function in a named alias which
  361. is suitable for the FROM clause, in the style accepted for example
  362. by PostgreSQL. A column expression is also provided using the
  363. special ``.column`` attribute, which may
  364. be used to refer to the output of the function as a scalar value
  365. in the columns or where clause, for a backend such as PostgreSQL.
  366. For a full table-valued expression, use the
  367. :meth:`_function.FunctionElement.table_valued` method first to
  368. establish named columns.
  369. e.g.::
  370. >>> from sqlalchemy import func, select, column
  371. >>> data_view = func.unnest([1, 2, 3]).alias("data_view")
  372. >>> print(select(data_view.column))
  373. SELECT data_view
  374. FROM unnest(:unnest_1) AS data_view
  375. The :meth:`_functions.FunctionElement.column_valued` method provides
  376. a shortcut for the above pattern::
  377. >>> data_view = func.unnest([1, 2, 3]).column_valued("data_view")
  378. >>> print(select(data_view))
  379. SELECT data_view
  380. FROM unnest(:unnest_1) AS data_view
  381. .. versionadded:: 1.4.0b2 Added the ``.column`` accessor
  382. .. seealso::
  383. :ref:`tutorial_functions_table_valued` -
  384. in the :ref:`unified_tutorial`
  385. :meth:`_functions.FunctionElement.table_valued`
  386. :meth:`_functions.FunctionElement.scalar_table_valued`
  387. :meth:`_functions.FunctionElement.column_valued`
  388. """
  389. return TableValuedAlias._construct(
  390. self, name, table_value_type=self.type
  391. )
  392. def select(self):
  393. """Produce a :func:`_expression.select` construct
  394. against this :class:`.FunctionElement`.
  395. This is shorthand for::
  396. s = select(function_element)
  397. """
  398. s = Select._create_select(self)
  399. if self._execution_options:
  400. s = s.execution_options(**self._execution_options)
  401. return s
  402. @util.deprecated_20(
  403. ":meth:`.FunctionElement.scalar`",
  404. alternative="Scalar execution in SQLAlchemy 2.0 is performed "
  405. "by the :meth:`_engine.Connection.scalar` method of "
  406. ":class:`_engine.Connection`, "
  407. "or in the ORM by the :meth:`.Session.scalar` method of "
  408. ":class:`.Session`.",
  409. )
  410. def scalar(self):
  411. """Execute this :class:`.FunctionElement` against an embedded
  412. 'bind' and return a scalar value.
  413. This first calls :meth:`~.FunctionElement.select` to
  414. produce a SELECT construct.
  415. Note that :class:`.FunctionElement` can be passed to
  416. the :meth:`.Connectable.scalar` method of :class:`_engine.Connection`
  417. or :class:`_engine.Engine`.
  418. """
  419. return self.select().execute().scalar()
  420. @util.deprecated_20(
  421. ":meth:`.FunctionElement.execute`",
  422. alternative="All statement execution in SQLAlchemy 2.0 is performed "
  423. "by the :meth:`_engine.Connection.execute` method of "
  424. ":class:`_engine.Connection`, "
  425. "or in the ORM by the :meth:`.Session.execute` method of "
  426. ":class:`.Session`.",
  427. )
  428. def execute(self):
  429. """Execute this :class:`.FunctionElement` against an embedded
  430. 'bind'.
  431. This first calls :meth:`~.FunctionElement.select` to
  432. produce a SELECT construct.
  433. Note that :class:`.FunctionElement` can be passed to
  434. the :meth:`.Connectable.execute` method of :class:`_engine.Connection`
  435. or :class:`_engine.Engine`.
  436. """
  437. return self.select().execute()
  438. def _bind_param(self, operator, obj, type_=None, **kw):
  439. return BindParameter(
  440. None,
  441. obj,
  442. _compared_to_operator=operator,
  443. _compared_to_type=self.type,
  444. unique=True,
  445. type_=type_,
  446. **kw
  447. )
  448. def self_group(self, against=None):
  449. # for the moment, we are parenthesizing all array-returning
  450. # expressions against getitem. This may need to be made
  451. # more portable if in the future we support other DBs
  452. # besides postgresql.
  453. if against is operators.getitem and isinstance(
  454. self.type, sqltypes.ARRAY
  455. ):
  456. return Grouping(self)
  457. else:
  458. return super(FunctionElement, self).self_group(against=against)
  459. @property
  460. def entity_namespace(self):
  461. """overrides FromClause.entity_namespace as functions are generally
  462. column expressions and not FromClauses.
  463. """
  464. # ideally functions would not be fromclauses but we failed to make
  465. # this adjustment in 1.4
  466. return _entity_namespace(self.clause_expr)
  467. class FunctionAsBinary(BinaryExpression):
  468. _traverse_internals = [
  469. ("sql_function", InternalTraversal.dp_clauseelement),
  470. ("left_index", InternalTraversal.dp_plain_obj),
  471. ("right_index", InternalTraversal.dp_plain_obj),
  472. ("modifiers", InternalTraversal.dp_plain_dict),
  473. ]
  474. def _gen_cache_key(self, anon_map, bindparams):
  475. return ColumnElement._gen_cache_key(self, anon_map, bindparams)
  476. def __init__(self, fn, left_index, right_index):
  477. self.sql_function = fn
  478. self.left_index = left_index
  479. self.right_index = right_index
  480. self.operator = operators.function_as_comparison_op
  481. self.type = sqltypes.BOOLEANTYPE
  482. self.negate = None
  483. self._is_implicitly_boolean = True
  484. self.modifiers = {}
  485. @property
  486. def left(self):
  487. return self.sql_function.clauses.clauses[self.left_index - 1]
  488. @left.setter
  489. def left(self, value):
  490. self.sql_function.clauses.clauses[self.left_index - 1] = value
  491. @property
  492. def right(self):
  493. return self.sql_function.clauses.clauses[self.right_index - 1]
  494. @right.setter
  495. def right(self, value):
  496. self.sql_function.clauses.clauses[self.right_index - 1] = value
  497. class ScalarFunctionColumn(NamedColumn):
  498. __visit_name__ = "scalar_function_column"
  499. _traverse_internals = [
  500. ("name", InternalTraversal.dp_anon_name),
  501. ("type", InternalTraversal.dp_type),
  502. ("fn", InternalTraversal.dp_clauseelement),
  503. ]
  504. is_literal = False
  505. table = None
  506. def __init__(self, fn, name, type_=None):
  507. self.fn = fn
  508. self.name = name
  509. self.type = sqltypes.to_instance(type_)
  510. class _FunctionGenerator(object):
  511. """Generate SQL function expressions.
  512. :data:`.func` is a special object instance which generates SQL
  513. functions based on name-based attributes, e.g.::
  514. >>> print(func.count(1))
  515. count(:param_1)
  516. The returned object is an instance of :class:`.Function`, and is a
  517. column-oriented SQL element like any other, and is used in that way::
  518. >>> print(select(func.count(table.c.id)))
  519. SELECT count(sometable.id) FROM sometable
  520. Any name can be given to :data:`.func`. If the function name is unknown to
  521. SQLAlchemy, it will be rendered exactly as is. For common SQL functions
  522. which SQLAlchemy is aware of, the name may be interpreted as a *generic
  523. function* which will be compiled appropriately to the target database::
  524. >>> print(func.current_timestamp())
  525. CURRENT_TIMESTAMP
  526. To call functions which are present in dot-separated packages,
  527. specify them in the same manner::
  528. >>> print(func.stats.yield_curve(5, 10))
  529. stats.yield_curve(:yield_curve_1, :yield_curve_2)
  530. SQLAlchemy can be made aware of the return type of functions to enable
  531. type-specific lexical and result-based behavior. For example, to ensure
  532. that a string-based function returns a Unicode value and is similarly
  533. treated as a string in expressions, specify
  534. :class:`~sqlalchemy.types.Unicode` as the type:
  535. >>> print(func.my_string(u'hi', type_=Unicode) + ' ' +
  536. ... func.my_string(u'there', type_=Unicode))
  537. my_string(:my_string_1) || :my_string_2 || my_string(:my_string_3)
  538. The object returned by a :data:`.func` call is usually an instance of
  539. :class:`.Function`.
  540. This object meets the "column" interface, including comparison and labeling
  541. functions. The object can also be passed the :meth:`~.Connectable.execute`
  542. method of a :class:`_engine.Connection` or :class:`_engine.Engine`,
  543. where it will be
  544. wrapped inside of a SELECT statement first::
  545. print(connection.execute(func.current_timestamp()).scalar())
  546. In a few exception cases, the :data:`.func` accessor
  547. will redirect a name to a built-in expression such as :func:`.cast`
  548. or :func:`.extract`, as these names have well-known meaning
  549. but are not exactly the same as "functions" from a SQLAlchemy
  550. perspective.
  551. Functions which are interpreted as "generic" functions know how to
  552. calculate their return type automatically. For a listing of known generic
  553. functions, see :ref:`generic_functions`.
  554. .. note::
  555. The :data:`.func` construct has only limited support for calling
  556. standalone "stored procedures", especially those with special
  557. parameterization concerns.
  558. See the section :ref:`stored_procedures` for details on how to use
  559. the DBAPI-level ``callproc()`` method for fully traditional stored
  560. procedures.
  561. .. seealso::
  562. :ref:`coretutorial_functions` - in the Core Tutorial
  563. :class:`.Function`
  564. """
  565. def __init__(self, **opts):
  566. self.__names = []
  567. self.opts = opts
  568. def __getattr__(self, name):
  569. # passthru __ attributes; fixes pydoc
  570. if name.startswith("__"):
  571. try:
  572. return self.__dict__[name]
  573. except KeyError:
  574. raise AttributeError(name)
  575. elif name.endswith("_"):
  576. name = name[0:-1]
  577. f = _FunctionGenerator(**self.opts)
  578. f.__names = list(self.__names) + [name]
  579. return f
  580. def __call__(self, *c, **kwargs):
  581. o = self.opts.copy()
  582. o.update(kwargs)
  583. tokens = len(self.__names)
  584. if tokens == 2:
  585. package, fname = self.__names
  586. elif tokens == 1:
  587. package, fname = "_default", self.__names[0]
  588. else:
  589. package = None
  590. if package is not None:
  591. func = _registry[package].get(fname.lower())
  592. if func is not None:
  593. return func(*c, **o)
  594. return Function(
  595. self.__names[-1], packagenames=tuple(self.__names[0:-1]), *c, **o
  596. )
  597. func = _FunctionGenerator()
  598. func.__doc__ = _FunctionGenerator.__doc__
  599. modifier = _FunctionGenerator(group=False)
  600. class Function(FunctionElement):
  601. r"""Describe a named SQL function.
  602. The :class:`.Function` object is typically generated from the
  603. :data:`.func` generation object.
  604. :param \*clauses: list of column expressions that form the arguments
  605. of the SQL function call.
  606. :param type\_: optional :class:`.TypeEngine` datatype object that will be
  607. used as the return value of the column expression generated by this
  608. function call.
  609. :param packagenames: a string which indicates package prefix names
  610. to be prepended to the function name when the SQL is generated.
  611. The :data:`.func` generator creates these when it is called using
  612. dotted format, e.g.::
  613. func.mypackage.some_function(col1, col2)
  614. .. seealso::
  615. :ref:`tutorial_functions` - in the :ref:`unified_tutorial`
  616. :data:`.func` - namespace which produces registered or ad-hoc
  617. :class:`.Function` instances.
  618. :class:`.GenericFunction` - allows creation of registered function
  619. types.
  620. """
  621. __visit_name__ = "function"
  622. _traverse_internals = FunctionElement._traverse_internals + [
  623. ("packagenames", InternalTraversal.dp_plain_obj),
  624. ("name", InternalTraversal.dp_string),
  625. ("type", InternalTraversal.dp_type),
  626. ]
  627. type = sqltypes.NULLTYPE
  628. """A :class:`_types.TypeEngine` object which refers to the SQL return
  629. type represented by this SQL function.
  630. This datatype may be configured when generating a
  631. :class:`_functions.Function` object by passing the
  632. :paramref:`_functions.Function.type_` parameter, e.g.::
  633. >>> select(func.lower("some VALUE", type_=String))
  634. The small number of built-in classes of :class:`_functions.Function` come
  635. with a built-in datatype that's appropriate to the class of function and
  636. its arguments. For functions that aren't known, the type defaults to the
  637. "null type".
  638. """
  639. @util.deprecated_params(
  640. bind=(
  641. "2.0",
  642. "The :paramref:`_sql.text.bind` argument is deprecated and "
  643. "will be removed in SQLAlchemy 2.0.",
  644. ),
  645. )
  646. def __init__(self, name, *clauses, **kw):
  647. """Construct a :class:`.Function`.
  648. The :data:`.func` construct is normally used to construct
  649. new :class:`.Function` instances.
  650. """
  651. self.packagenames = kw.pop("packagenames", None) or ()
  652. self.name = name
  653. self._bind = self._get_bind(kw)
  654. self.type = sqltypes.to_instance(kw.get("type_", None))
  655. FunctionElement.__init__(self, *clauses, **kw)
  656. def _get_bind(self, kw):
  657. if "bind" in kw:
  658. util.warn_deprecated_20(
  659. "The Function.bind argument is deprecated and "
  660. "will be removed in SQLAlchemy 2.0.",
  661. )
  662. return kw["bind"]
  663. def _bind_param(self, operator, obj, type_=None, **kw):
  664. return BindParameter(
  665. self.name,
  666. obj,
  667. _compared_to_operator=operator,
  668. _compared_to_type=self.type,
  669. type_=type_,
  670. unique=True,
  671. **kw
  672. )
  673. class _GenericMeta(TraversibleType):
  674. def __init__(cls, clsname, bases, clsdict):
  675. if annotation.Annotated not in cls.__mro__:
  676. cls.name = name = clsdict.get("name", clsname)
  677. cls.identifier = identifier = clsdict.get("identifier", name)
  678. package = clsdict.pop("package", "_default")
  679. # legacy
  680. if "__return_type__" in clsdict:
  681. cls.type = clsdict["__return_type__"]
  682. # Check _register attribute status
  683. cls._register = getattr(cls, "_register", True)
  684. # Register the function if required
  685. if cls._register:
  686. register_function(identifier, cls, package)
  687. else:
  688. # Set _register to True to register child classes by default
  689. cls._register = True
  690. super(_GenericMeta, cls).__init__(clsname, bases, clsdict)
  691. class GenericFunction(util.with_metaclass(_GenericMeta, Function)):
  692. """Define a 'generic' function.
  693. A generic function is a pre-established :class:`.Function`
  694. class that is instantiated automatically when called
  695. by name from the :data:`.func` attribute. Note that
  696. calling any name from :data:`.func` has the effect that
  697. a new :class:`.Function` instance is created automatically,
  698. given that name. The primary use case for defining
  699. a :class:`.GenericFunction` class is so that a function
  700. of a particular name may be given a fixed return type.
  701. It can also include custom argument parsing schemes as well
  702. as additional methods.
  703. Subclasses of :class:`.GenericFunction` are automatically
  704. registered under the name of the class. For
  705. example, a user-defined function ``as_utc()`` would
  706. be available immediately::
  707. from sqlalchemy.sql.functions import GenericFunction
  708. from sqlalchemy.types import DateTime
  709. class as_utc(GenericFunction):
  710. type = DateTime
  711. inherit_cache = True
  712. print(select(func.as_utc()))
  713. User-defined generic functions can be organized into
  714. packages by specifying the "package" attribute when defining
  715. :class:`.GenericFunction`. Third party libraries
  716. containing many functions may want to use this in order
  717. to avoid name conflicts with other systems. For example,
  718. if our ``as_utc()`` function were part of a package
  719. "time"::
  720. class as_utc(GenericFunction):
  721. type = DateTime
  722. package = "time"
  723. inherit_cache = True
  724. The above function would be available from :data:`.func`
  725. using the package name ``time``::
  726. print(select(func.time.as_utc()))
  727. A final option is to allow the function to be accessed
  728. from one name in :data:`.func` but to render as a different name.
  729. The ``identifier`` attribute will override the name used to
  730. access the function as loaded from :data:`.func`, but will retain
  731. the usage of ``name`` as the rendered name::
  732. class GeoBuffer(GenericFunction):
  733. type = Geometry
  734. package = "geo"
  735. name = "ST_Buffer"
  736. identifier = "buffer"
  737. inherit_cache = True
  738. The above function will render as follows::
  739. >>> print(func.geo.buffer())
  740. ST_Buffer()
  741. The name will be rendered as is, however without quoting unless the name
  742. contains special characters that require quoting. To force quoting
  743. on or off for the name, use the :class:`.sqlalchemy.sql.quoted_name`
  744. construct::
  745. from sqlalchemy.sql import quoted_name
  746. class GeoBuffer(GenericFunction):
  747. type = Geometry
  748. package = "geo"
  749. name = quoted_name("ST_Buffer", True)
  750. identifier = "buffer"
  751. inherit_cache = True
  752. The above function will render as::
  753. >>> print(func.geo.buffer())
  754. "ST_Buffer"()
  755. .. versionadded:: 1.3.13 The :class:`.quoted_name` construct is now
  756. recognized for quoting when used with the "name" attribute of the
  757. object, so that quoting can be forced on or off for the function
  758. name.
  759. """
  760. coerce_arguments = True
  761. _register = False
  762. inherit_cache = True
  763. def __init__(self, *args, **kwargs):
  764. parsed_args = kwargs.pop("_parsed_args", None)
  765. if parsed_args is None:
  766. parsed_args = [
  767. coercions.expect(
  768. roles.ExpressionElementRole,
  769. c,
  770. name=self.name,
  771. apply_propagate_attrs=self,
  772. )
  773. for c in args
  774. ]
  775. self._has_args = self._has_args or bool(parsed_args)
  776. self.packagenames = ()
  777. self._bind = self._get_bind(kwargs)
  778. self.clause_expr = ClauseList(
  779. operator=operators.comma_op, group_contents=True, *parsed_args
  780. ).self_group()
  781. self.type = sqltypes.to_instance(
  782. kwargs.pop("type_", None) or getattr(self, "type", None)
  783. )
  784. register_function("cast", Cast)
  785. register_function("extract", Extract)
  786. class next_value(GenericFunction):
  787. """Represent the 'next value', given a :class:`.Sequence`
  788. as its single argument.
  789. Compiles into the appropriate function on each backend,
  790. or will raise NotImplementedError if used on a backend
  791. that does not provide support for sequences.
  792. """
  793. type = sqltypes.Integer()
  794. name = "next_value"
  795. _traverse_internals = [
  796. ("sequence", InternalTraversal.dp_named_ddl_element)
  797. ]
  798. def __init__(self, seq, **kw):
  799. assert isinstance(
  800. seq, schema.Sequence
  801. ), "next_value() accepts a Sequence object as input."
  802. self._bind = self._get_bind(kw)
  803. self.sequence = seq
  804. self.type = sqltypes.to_instance(
  805. seq.data_type or getattr(self, "type", None)
  806. )
  807. def compare(self, other, **kw):
  808. return (
  809. isinstance(other, next_value)
  810. and self.sequence.name == other.sequence.name
  811. )
  812. @property
  813. def _from_objects(self):
  814. return []
  815. class AnsiFunction(GenericFunction):
  816. """Define a function in "ansi" format, which doesn't render parenthesis."""
  817. inherit_cache = True
  818. def __init__(self, *args, **kwargs):
  819. GenericFunction.__init__(self, *args, **kwargs)
  820. class ReturnTypeFromArgs(GenericFunction):
  821. """Define a function whose return type is the same as its arguments."""
  822. inherit_cache = True
  823. def __init__(self, *args, **kwargs):
  824. args = [
  825. coercions.expect(
  826. roles.ExpressionElementRole,
  827. c,
  828. name=self.name,
  829. apply_propagate_attrs=self,
  830. )
  831. for c in args
  832. ]
  833. kwargs.setdefault("type_", _type_from_args(args))
  834. kwargs["_parsed_args"] = args
  835. super(ReturnTypeFromArgs, self).__init__(*args, **kwargs)
  836. class coalesce(ReturnTypeFromArgs):
  837. _has_args = True
  838. inherit_cache = True
  839. class max(ReturnTypeFromArgs): # noqa A001
  840. """The SQL MAX() aggregate function."""
  841. inherit_cache = True
  842. class min(ReturnTypeFromArgs): # noqa A001
  843. """The SQL MIN() aggregate function."""
  844. inherit_cache = True
  845. class sum(ReturnTypeFromArgs): # noqa A001
  846. """The SQL SUM() aggregate function."""
  847. inherit_cache = True
  848. class now(GenericFunction):
  849. """The SQL now() datetime function.
  850. SQLAlchemy dialects will usually render this particular function
  851. in a backend-specific way, such as rendering it as ``CURRENT_TIMESTAMP``.
  852. """
  853. type = sqltypes.DateTime
  854. inherit_cache = True
  855. class concat(GenericFunction):
  856. """The SQL CONCAT() function, which concatenates strings.
  857. E.g.::
  858. >>> print(select(func.concat('a', 'b')))
  859. SELECT concat(:concat_2, :concat_3) AS concat_1
  860. String concatenation in SQLAlchemy is more commonly available using the
  861. Python ``+`` operator with string datatypes, which will render a
  862. backend-specific concatenation operator, such as ::
  863. >>> print(select(literal("a") + "b"))
  864. SELECT :param_1 || :param_2 AS anon_1
  865. """
  866. type = sqltypes.String
  867. inherit_cache = True
  868. class char_length(GenericFunction):
  869. """The CHAR_LENGTH() SQL function."""
  870. type = sqltypes.Integer
  871. inherit_cache = True
  872. def __init__(self, arg, **kwargs):
  873. GenericFunction.__init__(self, arg, **kwargs)
  874. class random(GenericFunction):
  875. """The RANDOM() SQL function."""
  876. _has_args = True
  877. inherit_cache = True
  878. class count(GenericFunction):
  879. r"""The ANSI COUNT aggregate function. With no arguments,
  880. emits COUNT \*.
  881. E.g.::
  882. from sqlalchemy import func
  883. from sqlalchemy import select
  884. from sqlalchemy import table, column
  885. my_table = table('some_table', column('id'))
  886. stmt = select(func.count()).select_from(my_table)
  887. Executing ``stmt`` would emit::
  888. SELECT count(*) AS count_1
  889. FROM some_table
  890. """
  891. type = sqltypes.Integer
  892. inherit_cache = True
  893. def __init__(self, expression=None, **kwargs):
  894. if expression is None:
  895. expression = literal_column("*")
  896. super(count, self).__init__(expression, **kwargs)
  897. class current_date(AnsiFunction):
  898. """The CURRENT_DATE() SQL function."""
  899. type = sqltypes.Date
  900. inherit_cache = True
  901. class current_time(AnsiFunction):
  902. """The CURRENT_TIME() SQL function."""
  903. type = sqltypes.Time
  904. inherit_cache = True
  905. class current_timestamp(AnsiFunction):
  906. """The CURRENT_TIMESTAMP() SQL function."""
  907. type = sqltypes.DateTime
  908. inherit_cache = True
  909. class current_user(AnsiFunction):
  910. """The CURRENT_USER() SQL function."""
  911. type = sqltypes.String
  912. inherit_cache = True
  913. class localtime(AnsiFunction):
  914. """The localtime() SQL function."""
  915. type = sqltypes.DateTime
  916. inherit_cache = True
  917. class localtimestamp(AnsiFunction):
  918. """The localtimestamp() SQL function."""
  919. type = sqltypes.DateTime
  920. inherit_cache = True
  921. class session_user(AnsiFunction):
  922. """The SESSION_USER() SQL function."""
  923. type = sqltypes.String
  924. inherit_cache = True
  925. class sysdate(AnsiFunction):
  926. """The SYSDATE() SQL function."""
  927. type = sqltypes.DateTime
  928. inherit_cache = True
  929. class user(AnsiFunction):
  930. """The USER() SQL function."""
  931. type = sqltypes.String
  932. inherit_cache = True
  933. class array_agg(GenericFunction):
  934. """Support for the ARRAY_AGG function.
  935. The ``func.array_agg(expr)`` construct returns an expression of
  936. type :class:`_types.ARRAY`.
  937. e.g.::
  938. stmt = select(func.array_agg(table.c.values)[2:5])
  939. .. versionadded:: 1.1
  940. .. seealso::
  941. :func:`_postgresql.array_agg` - PostgreSQL-specific version that
  942. returns :class:`_postgresql.ARRAY`, which has PG-specific operators
  943. added.
  944. """
  945. type = sqltypes.ARRAY
  946. inherit_cache = True
  947. def __init__(self, *args, **kwargs):
  948. args = [
  949. coercions.expect(
  950. roles.ExpressionElementRole, c, apply_propagate_attrs=self
  951. )
  952. for c in args
  953. ]
  954. default_array_type = kwargs.pop("_default_array_type", sqltypes.ARRAY)
  955. if "type_" not in kwargs:
  956. type_from_args = _type_from_args(args)
  957. if isinstance(type_from_args, sqltypes.ARRAY):
  958. kwargs["type_"] = type_from_args
  959. else:
  960. kwargs["type_"] = default_array_type(type_from_args)
  961. kwargs["_parsed_args"] = args
  962. super(array_agg, self).__init__(*args, **kwargs)
  963. class OrderedSetAgg(GenericFunction):
  964. """Define a function where the return type is based on the sort
  965. expression type as defined by the expression passed to the
  966. :meth:`.FunctionElement.within_group` method."""
  967. array_for_multi_clause = False
  968. inherit_cache = True
  969. def within_group_type(self, within_group):
  970. func_clauses = self.clause_expr.element
  971. order_by = sqlutil.unwrap_order_by(within_group.order_by)
  972. if self.array_for_multi_clause and len(func_clauses.clauses) > 1:
  973. return sqltypes.ARRAY(order_by[0].type)
  974. else:
  975. return order_by[0].type
  976. class mode(OrderedSetAgg):
  977. """Implement the ``mode`` ordered-set aggregate function.
  978. This function must be used with the :meth:`.FunctionElement.within_group`
  979. modifier to supply a sort expression to operate upon.
  980. The return type of this function is the same as the sort expression.
  981. .. versionadded:: 1.1
  982. """
  983. inherit_cache = True
  984. class percentile_cont(OrderedSetAgg):
  985. """Implement the ``percentile_cont`` ordered-set aggregate function.
  986. This function must be used with the :meth:`.FunctionElement.within_group`
  987. modifier to supply a sort expression to operate upon.
  988. The return type of this function is the same as the sort expression,
  989. or if the arguments are an array, an :class:`_types.ARRAY` of the sort
  990. expression's type.
  991. .. versionadded:: 1.1
  992. """
  993. array_for_multi_clause = True
  994. inherit_cache = True
  995. class percentile_disc(OrderedSetAgg):
  996. """Implement the ``percentile_disc`` ordered-set aggregate function.
  997. This function must be used with the :meth:`.FunctionElement.within_group`
  998. modifier to supply a sort expression to operate upon.
  999. The return type of this function is the same as the sort expression,
  1000. or if the arguments are an array, an :class:`_types.ARRAY` of the sort
  1001. expression's type.
  1002. .. versionadded:: 1.1
  1003. """
  1004. array_for_multi_clause = True
  1005. inherit_cache = True
  1006. class rank(GenericFunction):
  1007. """Implement the ``rank`` hypothetical-set aggregate function.
  1008. This function must be used with the :meth:`.FunctionElement.within_group`
  1009. modifier to supply a sort expression to operate upon.
  1010. The return type of this function is :class:`.Integer`.
  1011. .. versionadded:: 1.1
  1012. """
  1013. type = sqltypes.Integer()
  1014. inherit_cache = True
  1015. class dense_rank(GenericFunction):
  1016. """Implement the ``dense_rank`` hypothetical-set aggregate function.
  1017. This function must be used with the :meth:`.FunctionElement.within_group`
  1018. modifier to supply a sort expression to operate upon.
  1019. The return type of this function is :class:`.Integer`.
  1020. .. versionadded:: 1.1
  1021. """
  1022. type = sqltypes.Integer()
  1023. inherit_cache = True
  1024. class percent_rank(GenericFunction):
  1025. """Implement the ``percent_rank`` hypothetical-set aggregate function.
  1026. This function must be used with the :meth:`.FunctionElement.within_group`
  1027. modifier to supply a sort expression to operate upon.
  1028. The return type of this function is :class:`.Numeric`.
  1029. .. versionadded:: 1.1
  1030. """
  1031. type = sqltypes.Numeric()
  1032. inherit_cache = True
  1033. class cume_dist(GenericFunction):
  1034. """Implement the ``cume_dist`` hypothetical-set aggregate function.
  1035. This function must be used with the :meth:`.FunctionElement.within_group`
  1036. modifier to supply a sort expression to operate upon.
  1037. The return type of this function is :class:`.Numeric`.
  1038. .. versionadded:: 1.1
  1039. """
  1040. type = sqltypes.Numeric()
  1041. inherit_cache = True
  1042. class cube(GenericFunction):
  1043. r"""Implement the ``CUBE`` grouping operation.
  1044. This function is used as part of the GROUP BY of a statement,
  1045. e.g. :meth:`_expression.Select.group_by`::
  1046. stmt = select(
  1047. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1048. ).group_by(func.cube(table.c.col_1, table.c.col_2))
  1049. .. versionadded:: 1.2
  1050. """
  1051. _has_args = True
  1052. inherit_cache = True
  1053. class rollup(GenericFunction):
  1054. r"""Implement the ``ROLLUP`` grouping operation.
  1055. This function is used as part of the GROUP BY of a statement,
  1056. e.g. :meth:`_expression.Select.group_by`::
  1057. stmt = select(
  1058. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1059. ).group_by(func.rollup(table.c.col_1, table.c.col_2))
  1060. .. versionadded:: 1.2
  1061. """
  1062. _has_args = True
  1063. inherit_cache = True
  1064. class grouping_sets(GenericFunction):
  1065. r"""Implement the ``GROUPING SETS`` grouping operation.
  1066. This function is used as part of the GROUP BY of a statement,
  1067. e.g. :meth:`_expression.Select.group_by`::
  1068. stmt = select(
  1069. func.sum(table.c.value), table.c.col_1, table.c.col_2
  1070. ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))
  1071. In order to group by multiple sets, use the :func:`.tuple_` construct::
  1072. from sqlalchemy import tuple_
  1073. stmt = select(
  1074. func.sum(table.c.value),
  1075. table.c.col_1, table.c.col_2,
  1076. table.c.col_3
  1077. ).group_by(
  1078. func.grouping_sets(
  1079. tuple_(table.c.col_1, table.c.col_2),
  1080. tuple_(table.c.value, table.c.col_3),
  1081. )
  1082. )
  1083. .. versionadded:: 1.2
  1084. """
  1085. _has_args = True
  1086. inherit_cache = True