You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1529 lines
46KB

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