SQLAlchemy 2.0 Documentation
SQLAlchemy Core
- SQL Statements and Expressions API
- Column Elements and Expressions
- Operator Reference
- SELECT and Related Constructs
- Insert, Updates, Deletes
- SQL and Generic Functions¶
- Function API
AnsiFunctionFunctionFunctionElementFunctionElement.__init__()FunctionElement.alias()FunctionElement.as_comparison()FunctionElement.cFunctionElement.clausesFunctionElement.column_valued()FunctionElement.columnsFunctionElement.entity_namespaceFunctionElement.exported_columnsFunctionElement.filter()FunctionElement.over()FunctionElement.scalar_table_valued()FunctionElement.select()FunctionElement.self_group()FunctionElement.table_valued()FunctionElement.within_group()FunctionElement.within_group_type()
GenericFunctionregister_function()
- Selected “Known” Functions
aggregate_stringsarray_aggchar_lengthcoalesceconcatcountcubecume_distcurrent_datecurrent_timecurrent_timestampcurrent_userdense_rankgrouping_setslocaltimelocaltimestampmaxminmodenext_valuenowpercent_rankpercentile_contpercentile_discrandomrankrollupsession_usersumsysdateuser
- Function API
- Custom SQL Constructs and Compilation Extension
- Expression Serializer Extension
- SQL Expression Language Foundational Constructs
- Visitor and Traversal Utilities
- Schema Definition Language
- SQL Datatype Objects
- Engine and Connection Use
- Core API Basics
Project Versions
- Previous: Insert, Updates, Deletes
- Next: Custom SQL Constructs and Compilation Extension
- Up: Home
- On this page:
- SQL and Generic Functions
- Function API
AnsiFunctionFunctionFunctionElementFunctionElement.__init__()FunctionElement.alias()FunctionElement.as_comparison()FunctionElement.cFunctionElement.clausesFunctionElement.column_valued()FunctionElement.columnsFunctionElement.entity_namespaceFunctionElement.exported_columnsFunctionElement.filter()FunctionElement.over()FunctionElement.scalar_table_valued()FunctionElement.select()FunctionElement.self_group()FunctionElement.table_valued()FunctionElement.within_group()FunctionElement.within_group_type()
GenericFunctionregister_function()
- Selected “Known” Functions
aggregate_stringsarray_aggchar_lengthcoalesceconcatcountcubecume_distcurrent_datecurrent_timecurrent_timestampcurrent_userdense_rankgrouping_setslocaltimelocaltimestampmaxminmodenext_valuenowpercent_rankpercentile_contpercentile_discrandomrankrollupsession_usersumsysdateuser
- Function API
SQL and Generic Functions¶
SQL functions are invoked by using the func namespace. See the tutorial at Working with SQL Functions for background on how to use the func object to render SQL functions in statements.
See also
Working with SQL Functions - in the SQLAlchemy Unified Tutorial
Function API¶
The base API for SQL functions, which provides for the func namespace as well as classes that may be used for extensibility.
| Object Name | Description |
|---|---|
register_function(identifier, fn[, package]) | Associate a callable with a particular func. name. |
- class sqlalchemy.sql.functions.AnsiFunction¶
inherits from
sqlalchemy.sql.functions.GenericFunctionDefine a function in “ansi” format, which doesn’t render parenthesis.
- class sqlalchemy.sql.functions.Function¶
inherits from
sqlalchemy.sql.functions.FunctionElementDescribe a named SQL function.
The
Functionobject is typically generated from thefuncgeneration object.- Parameters:
*clauses¶ – list of column expressions that form the arguments of the SQL function call.
type_¶ – optional
TypeEnginedatatype object that will be used as the return value of the column expression generated by this function call.packagenames¶ –
a string which indicates package prefix names to be prepended to the function name when the SQL is generated. The
funcgenerator creates these when it is called using dotted format, e.g.:func.mypackage.some_function(col1, col2)
See also
Working with SQL Functions - in the SQLAlchemy Unified Tutorial
func- namespace which produces registered or ad-hocFunctioninstances.GenericFunction- allows creation of registered function types.Member Name Description __init__()
Construct a
Function.- method
sqlalchemy.sql.functions.Function.__init__(name: str, *clauses: _ColumnExpressionOrLiteralArgument[Any], type_: _TypeEngineArgument[_T] | None = None, packagenames: Tuple[str, ...] | None = None) → None¶ Construct a
Function.The
funcconstruct is normally used to construct newFunctioninstances.
- class sqlalchemy.sql.functions.FunctionElement¶
inherits from
sqlalchemy.sql.expression.Executable,sqlalchemy.sql.expression.ColumnElement,sqlalchemy.sql.expression.FromClause,sqlalchemy.sql.expression.GenerativeBase for SQL function-oriented constructs.
This is a generic type, meaning that type checkers and IDEs can be instructed on the types to expect in a
Resultfor this function. SeeGenericFunctionfor an example of how this is done.See also
Working with SQL Functions - in the SQLAlchemy Unified Tutorial
Function- named SQL function.func- namespace which produces registered or ad-hocFunctioninstances.GenericFunction- allows creation of registered function types.Member Name Description __init__()
Construct a
FunctionElement.alias()
Produce a
Aliasconstruct against thisFunctionElement.Interpret this expression as a boolean comparison between two values.
synonym for
FunctionElement.columns.Return the underlying
ClauseListwhich contains the arguments for thisFunctionElement.Return this
FunctionElementas a column expression that selects from itself as a FROM clause.The set of columns exported by this
FunctionElement.overrides FromClause.entity_namespace as functions are generally column expressions and not FromClauses.
filter()
Produce a FILTER clause against this function.
over()
Produce an OVER clause against this function.
Return a column expression that’s against this
FunctionElementas a scalar table-valued expression.select()
Produce a
select()construct against thisFunctionElement.Apply a ‘grouping’ to this
ClauseElement.Return a
TableValuedAliasrepresentation of thisFunctionElementwith table-valued expressions added.Produce a WITHIN GROUP (ORDER BY expr) clause against this function.
For types that define their return type as based on the criteria within a WITHIN GROUP (ORDER BY) expression, called by the
WithinGroupconstruct.- method
sqlalchemy.sql.functions.FunctionElement.__init__(*clauses: _ColumnExpressionOrLiteralArgument[Any]) → None¶ Construct a
FunctionElement.
- method
sqlalchemy.sql.functions.FunctionElement.alias(name: str | None = None, joins_implicitly: bool = False) → TableValuedAlias¶ Produce a
Aliasconstruct against thisFunctionElement.Tip
The
FunctionElement.alias()method is part of the mechanism by which “table valued” SQL functions are created. However, most use cases are covered by higher level methods onFunctionElementincludingFunctionElement.table_valued(), andFunctionElement.column_valued().This construct wraps the function in a named alias which is suitable for the FROM clause, in the style accepted for example by PostgreSQL. A column expression is also provided using the special
.columnattribute, which may be used to refer to the output of the function as a scalar value in the columns or where clause, for a backend such as PostgreSQL.For a full table-valued expression, use the
FunctionElement.table_valued()method first to establish named columns.e.g.:
>>> from sqlalchemy import func, select, column >>> data_view = func.unnest([1, 2, 3]).alias("data_view") >>> print(select(data_view.column))
SELECT data_view FROM unnest(:unnest_1) AS data_viewThe
FunctionElement.column_valued()method provides a shortcut for the above pattern:>>> data_view = func.unnest([1, 2, 3]).column_valued("data_view") >>> print(select(data_view))
SELECT data_view FROM unnest(:unnest_1) AS data_viewAdded in version 1.4.0b2: Added the
.columnaccessor- Parameters:
name¶ – alias name, will be rendered as
AS <name>in the FROM clausejoins_implicitly¶ –
when True, the table valued function may be used in the FROM clause without any explicit JOIN to other tables in the SQL query, and no “cartesian product” warning will be generated. May be useful for SQL functions such as
func.json_each().Added in version 1.4.33.
- method
sqlalchemy.sql.functions.FunctionElement.as_comparison(left_index: int, right_index: int) → FunctionAsBinary¶ Interpret this expression as a boolean comparison between two values.
This method is used for an ORM use case described at Custom operators based on SQL functions.
A hypothetical SQL function “is_equal()” which compares to values for equality would be written in the Core expression language as:
expr = func.is_equal("a", "b")
If “is_equal()” above is comparing “a” and “b” for equality, the
FunctionElement.as_comparison()method would be invoked as:expr = func.is_equal("a", "b").as_comparison(1, 2)
Where above, the integer value “1” refers to the first argument of the “is_equal()” function and the integer value “2” refers to the second.
This would create a
BinaryExpressionthat is equivalent to:BinaryExpression("a", "b", operator=op.eq)
However, at the SQL level it would still render as “is_equal(‘a’, ‘b’)”.
The ORM, when it loads a related object or collection, needs to be able to manipulate the “left” and “right” sides of the ON clause of a JOIN expression. The purpose of this method is to provide a SQL function construct that can also supply this information to the ORM, when used with the
relationship.primaryjoinparameter. The return value is a containment object calledFunctionAsBinary.An ORM example is as follows:
class Venue(Base): __tablename__ = "venue" id = Column(Integer, primary_key=True) name = Column(String) descendants = relationship( "Venue", primaryjoin=func.instr( remote(foreign(name)), name + "/" ).as_comparison(1, 2) == 1, viewonly=True, order_by=name, )
Above, the “Venue” class can load descendant “Venue” objects by determining if the name of the parent Venue is contained within the start of the hypothetical descendant value’s name, e.g. “parent1” would match up to “parent1/child1”, but not to “parent2/child1”.
Possible use cases include the “materialized path” example given above, as well as making use of special SQL functions such as geometric functions to create join conditions.
- Parameters:
Added in version 1.3.
See also
Custom operators based on SQL functions - example use within the ORM
- attribute
sqlalchemy.sql.functions.FunctionElement.c¶ synonym for
FunctionElement.columns.
- attribute
sqlalchemy.sql.functions.FunctionElement.clauses¶ Return the underlying
ClauseListwhich contains the arguments for thisFunctionElement.
- method
sqlalchemy.sql.functions.FunctionElement.column_valued(name: str | None = None, joins_implicitly: bool = False) → TableValuedColumn[_T]¶ Return this
FunctionElementas a column expression that selects from itself as a FROM clause.E.g.:
>>> from sqlalchemy import select, func >>> gs = func.generate_series(1, 5, -1).column_valued() >>> print(select(gs))
SELECT anon_1 FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) AS anon_1This is shorthand for:
gs = func.generate_series(1, 5, -1).alias().column
- Parameters:
name¶ – optional name to assign to the alias name that’s generated. If omitted, a unique anonymizing name is used.
joins_implicitly¶ –
when True, the “table” portion of the column valued function may be a member of the FROM clause without any explicit JOIN to other tables in the SQL query, and no “cartesian product” warning will be generated. May be useful for SQL functions such as
func.json_array_elements().Added in version 1.4.46.
See also
Column Valued Functions - Table Valued Function as a Scalar Column - in the SQLAlchemy Unified Tutorial
Column Valued Functions - in the PostgreSQL documentation
- attribute
sqlalchemy.sql.functions.FunctionElement.columns¶ The set of columns exported by this
FunctionElement.This is a placeholder collection that allows the function to be placed in the FROM clause of a statement:
>>> from sqlalchemy import column, select, func >>> stmt = select(column("x"), column("y")).select_from(func.myfunction()) >>> print(stmt)
SELECT x, y FROM myfunction()The above form is a legacy feature that is now superseded by the fully capable
FunctionElement.table_valued()method; see that method for details.See also
FunctionElement.table_valued()- generates table-valued SQL function expressions.
- attribute
sqlalchemy.sql.functions.FunctionElement.entity_namespace¶ overrides FromClause.entity_namespace as functions are generally column expressions and not FromClauses.
- attribute
sqlalchemy.sql.functions.FunctionElement.exported_columns¶
- method
sqlalchemy.sql.functions.FunctionElement.filter(*criterion: _ColumnExpressionArgument[bool]) → Self | FunctionFilter[_T]¶ Produce a FILTER clause against this function.
Used against aggregate and window functions, for database backends that support the “FILTER” clause.
The expression:
func.count(1).filter(True)
is shorthand for:
from sqlalchemy import funcfilter funcfilter(func.count(1), True)
- method
sqlalchemy.sql.functions.FunctionElement.over(*, partition_by: _ByArgument | None = None, order_by: _ByArgument | None = None, rows: Tuple[int | None, int | None] | None = None, range_: Tuple[int | None, int | None] | None = None, groups: Tuple[int | None, int | None] | None = None) → Over[_T]¶ Produce an OVER clause against this function.
Used against aggregate or so-called “window” functions, for database backends that support window functions.
The expression:
func.row_number().over(order_by="x")
is shorthand for:
from sqlalchemy import over over(func.row_number(), order_by="x")
See
over()for a full description.
- method
sqlalchemy.sql.functions.FunctionElement.scalar_table_valued(name: str, type_: _TypeEngineArgument[_T] | None = None) → ScalarFunctionColumn[_T]¶ Return a column expression that’s against this
FunctionElementas a scalar table-valued expression.The returned expression is similar to that returned by a single column accessed off of a
FunctionElement.table_valued()construct, except no FROM clause is generated; the function is rendered in the similar way as a scalar subquery.E.g.:
>>> from sqlalchemy import func, select >>> fn = func.jsonb_each("{'k', 'v'}").scalar_table_valued("key") >>> print(select(fn))
SELECT (jsonb_each(:jsonb_each_1)).keyAdded in version 1.4.0b2.
- method
sqlalchemy.sql.functions.FunctionElement.select() → Select¶ Produce a
select()construct against thisFunctionElement.This is shorthand for:
s = select(function_element)
- method
sqlalchemy.sql.functions.FunctionElement.self_group(against: OperatorType | None = None) → ClauseElement¶ Apply a ‘grouping’ to this
ClauseElement.This method is overridden by subclasses to return a “grouping” construct, i.e. parenthesis. In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()constructs when placed into the FROM clause of anotherselect(). (Note that subqueries should be normally created using theSelect.alias()method, as many platforms require nested SELECT statements to be named).As expressions are composed together, the application of
self_group()is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)- AND takes precedence over OR.The base
self_group()method ofClauseElementjust returns self.
- method
sqlalchemy.sql.functions.FunctionElement.table_valued(*expr: _ColumnExpressionOrStrLabelArgument[Any], **kw: Any) → TableValuedAlias¶ Return a
TableValuedAliasrepresentation of thisFunctionElementwith table-valued expressions added.e.g.:
>>> fn = func.generate_series(1, 5).table_valued( ... "value", "start", "stop", "step" ... ) >>> print(select(fn))
SELECT anon_1.value, anon_1.start, anon_1.stop, anon_1.step FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1>>> print(select(fn.c.value, fn.c.stop).where(fn.c.value > 2))SELECT anon_1.value, anon_1.stop FROM generate_series(:generate_series_1, :generate_series_2) AS anon_1 WHERE anon_1.value > :value_1A WITH ORDINALITY expression may be generated by passing the keyword argument “with_ordinality”:
>>> fn = func.generate_series(4, 1, -1).table_valued( ... "gen", with_ordinality="ordinality" ... ) >>> print(select(fn))
SELECT anon_1.gen, anon_1.ordinality FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) WITH ORDINALITY AS anon_1- Parameters:
*expr¶ – A series of string column names that will be added to the
.ccollection of the resultingTableValuedAliasconstruct as columns.column()objects with or without datatypes may also be used.name¶ – optional name to assign to the alias name that’s generated. If omitted, a unique anonymizing name is used.
with_ordinality¶ – string name that when present results in the
WITH ORDINALITYclause being added to the alias, and the given string name will be added as a column to the .c collection of the resultingTableValuedAlias.joins_implicitly¶ –
when True, the table valued function may be used in the FROM clause without any explicit JOIN to other tables in the SQL query, and no “cartesian product” warning will be generated. May be useful for SQL functions such as
func.json_each().Added in version 1.4.33.
Added in version 1.4.0b2.
See also
Table-Valued Functions - in the SQLAlchemy Unified Tutorial
Table-Valued Functions - in the PostgreSQL documentation
FunctionElement.scalar_table_valued()- variant ofFunctionElement.table_valued()which delivers the complete table valued expression as a scalar column expressionFunctionElement.column_valued()TableValuedAlias.render_derived()- renders the alias using a derived column clause, e.g.AS name(col1, col2, ...)
- method
sqlalchemy.sql.functions.FunctionElement.within_group(*order_by: _ColumnExpressionArgument[Any]) → WithinGroup[_T]¶ Produce a WITHIN GROUP (ORDER BY expr) clause against this function.
Used against so-called “ordered set aggregate” and “hypothetical set aggregate” functions, including
percentile_cont,rank,dense_rank, etc.See
within_group()for a full description.See also
Special Modifiers WITHIN GROUP, FILTER - in the SQLAlchemy Unified Tutorial
- method
sqlalchemy.sql.functions.FunctionElement.within_group_type(within_group: WithinGroup[_S]) → TypeEngine | None¶ For types that define their return type as based on the criteria within a WITHIN GROUP (ORDER BY) expression, called by the
WithinGroupconstruct.Returns None by default, in which case the function’s normal
.typeis used.
- method
- class sqlalchemy.sql.functions.GenericFunction¶
inherits from
sqlalchemy.sql.functions.FunctionDefine a ‘generic’ function.
A generic function is a pre-established
Functionclass that is instantiated automatically when called by name from thefuncattribute. Note that calling any name fromfunchas the effect that a newFunctioninstance is created automatically, given that name. The primary use case for defining aGenericFunctionclass is so that a function of a particular name may be given a fixed return type. It can also include custom argument parsing schemes as well as additional methods.Subclasses of
GenericFunctionare automatically registered under the name of the class. For example, a user-defined functionas_utc()would be available immediately:from sqlalchemy.sql.functions import GenericFunction from sqlalchemy.types import DateTime class as_utc(GenericFunction): type = DateTime() inherit_cache = True print(select(func.as_utc()))
User-defined generic functions can be organized into packages by specifying the “package” attribute when defining
GenericFunction. Third party libraries containing many functions may want to use this in order to avoid name conflicts with other systems. For example, if ouras_utc()function were part of a package “time”:class as_utc(GenericFunction): type = DateTime() package = "time" inherit_cache = True
The above function would be available from
funcusing the package nametime:print(select(func.time.as_utc()))
A final option is to allow the function to be accessed from one name in
funcbut to render as a different name. Theidentifierattribute will override the name used to access the function as loaded fromfunc, but will retain the usage ofnameas the rendered name:class GeoBuffer(GenericFunction): type = Geometry() package = "geo" name = "ST_Buffer" identifier = "buffer" inherit_cache = True
The above function will render as follows:
>>> print(func.geo.buffer())
ST_Buffer()The name will be rendered as is, however without quoting unless the name contains special characters that require quoting. To force quoting on or off for the name, use the
quoted_nameconstruct:from sqlalchemy.sql import quoted_name class GeoBuffer(GenericFunction): type = Geometry() package = "geo" name = quoted_name("ST_Buffer", True) identifier = "buffer" inherit_cache = True
The above function will render as:
>>> print(func.geo.buffer())
"ST_Buffer"()Type parameters for this class as a generic type can be passed and should match the type seen in a
Result. For example:class as_utc(GenericFunction[datetime.datetime]): type = DateTime() inherit_cache = True
The above indicates that the following expression returns a
datetimeobject:connection.scalar(select(func.as_utc()))
Added in version 1.3.13: The
quoted_nameconstruct is now recognized for quoting when used with the “name” attribute of the object, so that quoting can be forced on or off for the function name.
- function sqlalchemy.sql.functions.register_function(identifier: str, fn: Type[Function[Any]], package: str = '_default') → None¶
Associate a callable with a particular func. name.
This is normally called by GenericFunction, but is also available by itself so that a non-Function construct can be associated with the
funcaccessor (i.e. CAST, EXTRACT).
Selected “Known” Functions¶
These are GenericFunction implementations for a selected set of common SQL functions that set up the expected return type for each function automatically. The are invoked in the same way as any other member of the func namespace:
select(func.count("*")).select_from(some_table)Note that any name not known to func generates the function name as is - there is no restriction on what SQL functions can be called, known or unknown to SQLAlchemy, built-in or user defined. The section here only describes those functions where SQLAlchemy already knows what argument and return types are in use.
| Object Name | Description |
|---|---|
- class sqlalchemy.sql.functions.aggregate_strings¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement a generic string aggregation function.
This function will concatenate non-null values into a string and separate the values by a delimiter.
This function is compiled on a per-backend basis, into functions such as
group_concat(),string_agg(), orLISTAGG().e.g. Example usage with delimiter ‘.’:
stmt = select(func.aggregate_strings(table.c.str_col, "."))
The return type of this function is
String.
- class sqlalchemy.sql.functions.array_agg¶
inherits from
sqlalchemy.sql.functions.ReturnTypeFromArgsSupport for the ARRAY_AGG function.
The
func.array_agg(expr)construct returns an expression of typeARRAY.e.g.:
stmt = select(func.array_agg(table.c.values)[2:5])
See also
array_agg()- PostgreSQL-specific version that returnsARRAY, which has PG-specific operators added.
- class sqlalchemy.sql.functions.char_length¶
inherits from
sqlalchemy.sql.functions.GenericFunctionThe CHAR_LENGTH() SQL function.
- class sqlalchemy.sql.functions.coalesce¶
inherits from
sqlalchemy.sql.functions.ReturnTypeFromArgs
- class sqlalchemy.sql.functions.concat¶
inherits from
sqlalchemy.sql.functions.GenericFunctionThe SQL CONCAT() function, which concatenates strings.
E.g.:
>>> print(select(func.concat("a", "b")))
SELECT concat(:concat_2, :concat_3) AS concat_1String concatenation in SQLAlchemy is more commonly available using the Python
+operator with string datatypes, which will render a backend-specific concatenation operator, such as :>>> print(select(literal("a") + "b"))
SELECT :param_1 || :param_2 AS anon_1
- class sqlalchemy.sql.functions.count¶
inherits from
sqlalchemy.sql.functions.GenericFunctionThe ANSI COUNT aggregate function. With no arguments, emits COUNT *.
E.g.:
from sqlalchemy import func from sqlalchemy import select from sqlalchemy import table, column my_table = table("some_table", column("id")) stmt = select(func.count()).select_from(my_table)
Executing
stmtwould emit:SELECT count(*) AS count_1 FROM some_table
- class sqlalchemy.sql.functions.cube¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
CUBEgrouping operation.This function is used as part of the GROUP BY of a statement, e.g.
Select.group_by():stmt = select( func.sum(table.c.value), table.c.col_1, table.c.col_2 ).group_by(func.cube(table.c.col_1, table.c.col_2))
Added in version 1.2.
- class sqlalchemy.sql.functions.cume_dist¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
cume_disthypothetical-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is
Numeric.
- class sqlalchemy.sql.functions.current_date¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe CURRENT_DATE() SQL function.
- class sqlalchemy.sql.functions.current_time¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe CURRENT_TIME() SQL function.
- class sqlalchemy.sql.functions.current_timestamp¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe CURRENT_TIMESTAMP() SQL function.
- class sqlalchemy.sql.functions.current_user¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe CURRENT_USER() SQL function.
- class sqlalchemy.sql.functions.dense_rank¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
dense_rankhypothetical-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is
Integer.
- class sqlalchemy.sql.functions.grouping_sets¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
GROUPING SETSgrouping operation.This function is used as part of the GROUP BY of a statement, e.g.
Select.group_by():stmt = select( func.sum(table.c.value), table.c.col_1, table.c.col_2 ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))
In order to group by multiple sets, use the
tuple_()construct:from sqlalchemy import tuple_ stmt = select( func.sum(table.c.value), table.c.col_1, table.c.col_2, table.c.col_3 ).group_by( func.grouping_sets( tuple_(table.c.col_1, table.c.col_2), tuple_(table.c.value, table.c.col_3), ) )
Added in version 1.2.
- class sqlalchemy.sql.functions.localtime¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe localtime() SQL function.
- class sqlalchemy.sql.functions.localtimestamp¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe localtimestamp() SQL function.
- class sqlalchemy.sql.functions.max¶
inherits from
sqlalchemy.sql.functions.ReturnTypeFromArgsThe SQL MAX() aggregate function.
- class sqlalchemy.sql.functions.min¶
inherits from
sqlalchemy.sql.functions.ReturnTypeFromArgsThe SQL MIN() aggregate function.
- class sqlalchemy.sql.functions.mode¶
inherits from
sqlalchemy.sql.functions.OrderedSetAggImplement the
modeordered-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is the same as the sort expression.
- class sqlalchemy.sql.functions.next_value¶
inherits from
sqlalchemy.sql.functions.GenericFunctionRepresent the ‘next value’, given a
Sequenceas its single argument.Compiles into the appropriate function on each backend, or will raise NotImplementedError if used on a backend that does not provide support for sequences.
- class sqlalchemy.sql.functions.now¶
inherits from
sqlalchemy.sql.functions.GenericFunctionThe SQL now() datetime function.
SQLAlchemy dialects will usually render this particular function in a backend-specific way, such as rendering it as
CURRENT_TIMESTAMP.
- class sqlalchemy.sql.functions.percent_rank¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
percent_rankhypothetical-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is
Numeric.
- class sqlalchemy.sql.functions.percentile_cont¶
inherits from
sqlalchemy.sql.functions.OrderedSetAggImplement the
percentile_contordered-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is the same as the sort expression, or if the arguments are an array, an
ARRAYof the sort expression’s type.
- class sqlalchemy.sql.functions.percentile_disc¶
inherits from
sqlalchemy.sql.functions.OrderedSetAggImplement the
percentile_discordered-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is the same as the sort expression, or if the arguments are an array, an
ARRAYof the sort expression’s type.
- class sqlalchemy.sql.functions.random¶
inherits from
sqlalchemy.sql.functions.GenericFunctionThe RANDOM() SQL function.
- class sqlalchemy.sql.functions.rank¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
rankhypothetical-set aggregate function.This function must be used with the
FunctionElement.within_group()modifier to supply a sort expression to operate upon.The return type of this function is
Integer.
- class sqlalchemy.sql.functions.rollup¶
inherits from
sqlalchemy.sql.functions.GenericFunctionImplement the
ROLLUPgrouping operation.This function is used as part of the GROUP BY of a statement, e.g.
Select.group_by():stmt = select( func.sum(table.c.value), table.c.col_1, table.c.col_2 ).group_by(func.rollup(table.c.col_1, table.c.col_2))
Added in version 1.2.
- class sqlalchemy.sql.functions.session_user¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe SESSION_USER() SQL function.
- class sqlalchemy.sql.functions.sum¶
inherits from
sqlalchemy.sql.functions.ReturnTypeFromArgsThe SQL SUM() aggregate function.
- class sqlalchemy.sql.functions.sysdate¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe SYSDATE() SQL function.
- class sqlalchemy.sql.functions.user¶
inherits from
sqlalchemy.sql.functions.AnsiFunctionThe USER() SQL function.
flambé! the dragon and The Alchemist image designs created and generously donated by Rotem Yaari.
Created using Sphinx 8.2.3. Documentation last generated: Tue 21 Oct 2025 06:01:18 PM EDT