SQL and Generic Functions

SQL functions which are known to SQLAlchemy with regards to database-specificrendering, return types and argument behavior. Generic functions are invokedlike all SQL functions, using the func attribute:

  1. select([func.count()]).select_from(sometable)

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 orunknown to SQLAlchemy, built-in or user defined. The section here onlydescribes those functions where SQLAlchemy already knows what argument andreturn types are in use.

SQL function API, factories, and built-in functions.

Describe a named SQL function.

See the superclass FunctionElement for a descriptionof public methods.

See also

func - namespace which produces registered or ad-hocFunction instances.

GenericFunction - allows creation of registered functiontypes.

  • init(name, *clauses, **kw)
  • Construct a Function.

The func construct is normally used to constructnew Function instances.

Base for SQL function-oriented constructs.

See also

Function - named SQL function.

func - namespace which produces registered or ad-hocFunction instances.

GenericFunction - allows creation of registered functiontypes.

This construct wraps the function in a named alias whichis suitable for the FROM clause, in the style accepted for exampleby PostgreSQL.

e.g.:

  1. from sqlalchemy.sql import column
  2.  
  3. stmt = select([column('data_view')]).\
  4. select_from(SomeTable).\
  5. select_from(func.unnest(SomeTable.data).alias('data_view')
  6. )

Would produce:

  1. SELECT data_view
  2. FROM sometable, unnest(sometable.data) AS data_view

New in version 0.9.8: The FunctionElement.alias() methodis now supported. Previously, this method’s behavior wasundefined and did not behave consistently across versions.

  • ascomparison(_left_index, right_index)
  • Interpret this expression as a boolean comparison between two values.

A hypothetical SQL function “is_equal()” which compares to valuesfor equality would be written in the Core expression language as:

  1. expr = func.is_equal("a", "b")

If “is_equal()” above is comparing “a” and “b” for equality, theFunctionElement.as_comparison() method would be invoked as:

  1. 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 BinaryExpression that is equivalent to:

  1. 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 ableto manipulate the “left” and “right” sides of the ON clause of a JOINexpression. The purpose of this method is to provide a SQL functionconstruct that can also supply this information to the ORM, when usedwith the relationship.primaryjoin parameter. The returnvalue is a containment object called FunctionAsBinary.

An ORM example is as follows:

  1. class Venue(Base):
  2. __tablename__ = 'venue'
  3. id = Column(Integer, primary_key=True)
  4. name = Column(String)
  5.  
  6. descendants = relationship(
  7. "Venue",
  8. primaryjoin=func.instr(
  9. remote(foreign(name)), name + "/"
  10. ).as_comparison(1, 2) == 1,
  11. viewonly=True,
  12. order_by=name
  13. )

Above, the “Venue” class can load descendant “Venue” objects bydetermining if the name of the parent Venue is contained within thestart of the hypothetical descendant value’s name, e.g. “parent1” wouldmatch 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 geometricfunctions to create join conditions.

  1. - Parameters
  2. -
  3. -

left_index – the integer 1-based index of the function argumentthat serves as the “left” side of the expression.

  1. -

right_index – the integer 1-based index of the function argumentthat serves as the “right” side of the expression.

New in version 1.3.

Function objects currently have no result column names built in;this method returns a single-element column collection withan anonymously named column.

An interim approach to providing named columns for a functionas a FROM clause is to build a select() with thedesired columns:

  1. from sqlalchemy.sql import column
  2.  
  3. stmt = select([column('x'), column('y')]). select_from(func.myfunction())

This first calls select() toproduce a SELECT construct.

Note that FunctionElement can be passed tothe Connectable.execute() method of Connectionor Engine.

  • filter(*criterion)
  • Produce a FILTER clause against this function.

Used against aggregate and window functions,for database backends that support the “FILTER” clause.

The expression:

  1. func.count(1).filter(True)

is shorthand for:

  1. from sqlalchemy import funcfilter
  2. funcfilter(func.count(1), True)

New in version 1.0.0.

See also

FunctionFilter

funcfilter()

  • getchildren(**kwargs_)
  • Return immediate child elements of this ClauseElement.

This is used for visit traversal.

**kwargs may contain flags that change the collection that isreturned, for example to return a subset of items in order tocut down on larger traversals, or to return child items from adifferent context (such as schema-level collections instead ofclause-level).

  • over(partition_by=None, order_by=None, rows=None, range=None_)
  • Produce an OVER clause against this function.

Used against aggregate or so-called “window” functions,for database backends that support window functions.

The expression:

  1. func.row_number().over(order_by='x')

is shorthand for:

  1. from sqlalchemy import over
  2. over(func.row_number(), order_by='x')

See over() for a full description.

  • packagenames = ()
  • scalar()
  • Execute this FunctionElement against an embedded‘bind’ and return a scalar value.

This first calls select() toproduce a SELECT construct.

Note that FunctionElement can be passed tothe Connectable.scalar() method of Connectionor Engine.

This is shorthand for:

  1. s = select([function_element])
  • selfgroup(_against=None)
  • Apply a ‘grouping’ to this ClauseElement.

This method is overridden by subclasses to return a“grouping” construct, i.e. parenthesis. In particularit’s used by “binary” expressions to provide a groupingaround themselves when placed into a larger expression,as well as by select() constructs when placed intothe FROM clause of another select(). (Note thatsubqueries should be normally created using theSelect.alias() method, as many platforms requirenested SELECT statements to be named).

As expressions are composed together, the application ofself_group() is automatic - end-user code should neverneed to use this method directly. Note that SQLAlchemy’sclause constructs take operator precedence into account -so parenthesis might not be needed, for example, inan expression like x OR (y AND z) - AND takes precedenceover OR.

The base self_group() method of ClauseElementjust returns self.

  • withingroup(*orderby)
  • Produce a WITHIN GROUP (ORDER BY expr) clause against this function.

Used against so-called “ordered set aggregate” and “hypotheticalset aggregate” functions, including percentile_cont,rank, dense_rank, etc.

See within_group() for a full description.

New in version 1.1.

  • withingroup_type(_within_group)
  • For types that define their return type as based on the criteriawithin a WITHIN GROUP (ORDER BY) expression, called by theWithinGroup construct.

Returns None by default, in which case the function’s normal .typeis used.

Define a ‘generic’ function.

A generic function is a pre-established Functionclass that is instantiated automatically when calledby name from the func attribute. Note thatcalling any name from func has the effect thata new Function instance is created automatically,given that name. The primary use case for defininga GenericFunction class is so that a functionof a particular name may be given a fixed return type.It can also include custom argument parsing schemes as wellas additional methods.

Subclasses of GenericFunction are automaticallyregistered under the name of the class. Forexample, a user-defined function as_utc() wouldbe available immediately:

  1. from sqlalchemy.sql.functions import GenericFunction
  2. from sqlalchemy.types import DateTime
  3.  
  4. class as_utc(GenericFunction):
  5. type = DateTime
  6.  
  7. print select([func.as_utc()])

User-defined generic functions can be organized intopackages by specifying the “package” attribute when definingGenericFunction. Third party librariescontaining many functions may want to use this in orderto avoid name conflicts with other systems. For example,if our as_utc() function were part of a package“time”:

  1. class as_utc(GenericFunction):
  2. type = DateTime
  3. package = "time"

The above function would be available from funcusing the package name time:

  1. print select([func.time.as_utc()])

A final option is to allow the function to be accessedfrom one name in func but to render as a different name.The identifier attribute will override the name used toaccess the function as loaded from func, but will retainthe usage of name as the rendered name:

  1. class GeoBuffer(GenericFunction):
  2. type = Geometry
  3. package = "geo"
  4. name = "ST_Buffer"
  5. identifier = "buffer"

The above function will render as follows:

  1. >>> print func.geo.buffer()
  2. ST_Buffer()

Define a function where the return type is based on the sortexpression type as defined by the expression passed to theFunctionElement.within_group() method.

  • arrayfor_multi_clause = False_
  • identifier = 'OrderedSetAgg'
  • name = 'OrderedSetAgg'
  • withingroup_type(_within_group)
  • For types that define their return type as based on the criteriawithin a WITHIN GROUP (ORDER BY) expression, called by theWithinGroup construct.

Returns None by default, in which case the function’s normal .typeis used.

Define a function whose return type is the same as its arguments.

support for the ARRAY_AGG function.

The func.array_agg(expr) construct returns an expression oftype types.ARRAY.

e.g.:

  1. stmt = select([func.array_agg(table.c.values)[2:5]])

New in version 1.1.

See also

postgresql.array_agg() - PostgreSQL-specific version thatreturns postgresql.ARRAY, which has PG-specific operatorsadded.

  • identifier = 'array_agg'
  • name = 'array_agg'
  • type
  • alias of sqlalchemy.sql.sqltypes.ARRAY

The ANSI COUNT aggregate function. With no arguments,emits COUNT *.

E.g.:

  1. from sqlalchemy import func
  2. from sqlalchemy import select
  3. from sqlalchemy import table, column
  4.  
  5. my_table = table('some_table', column('id'))
  6.  
  7. stmt = select([func.count()]).select_from(my_table)

Executing stmt would emit:

  1. SELECT count(*) AS count_1
  2. FROM some_table
  • identifier = 'count'
  • name = 'count'
  • type
  • alias of sqlalchemy.sql.sqltypes.Integer

Implement the CUBE grouping operation.

This function is used as part of the GROUP BY of a statement,e.g. Select.group_by():

  1. stmt = select(
  2. [func.sum(table.c.value), table.c.col_1, table.c.col_2]
  3. ).group_by(func.cube(table.c.col_1, table.c.col_2))

New in version 1.2.

Implement the cume_dist hypothetical-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.

New in version 1.1.

  • identifier = 'cume_dist'
  • name = 'cume_dist'
  • type = Numeric()
  • identifier = 'current_date'

  • name = 'current_date'
  • type
  • alias of sqlalchemy.sql.sqltypes.Date

Implement the dense_rank hypothetical-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.

New in version 1.1.

Implement the GROUPING SETS grouping operation.

This function is used as part of the GROUP BY of a statement,e.g. Select.group_by():

  1. stmt = select(
  2. [func.sum(table.c.value), table.c.col_1, table.c.col_2]
  3. ).group_by(func.grouping_sets(table.c.col_1, table.c.col_2))

In order to group by multiple sets, use the tuple_() construct:

  1. from sqlalchemy import tuple_
  2.  
  3. stmt = select(
  4. [
  5. func.sum(table.c.value),
  6. table.c.col_1, table.c.col_2,
  7. table.c.col_3]
  8. ).group_by(
  9. func.grouping_sets(
  10. tuple_(table.c.col_1, table.c.col_2),
  11. tuple_(table.c.value, table.c.col_3),
  12. )
  13. )

New in version 1.2.

  • identifier = 'grouping_sets'
  • name = 'grouping_sets'
  • identifier = 'localtime'

  • name = 'localtime'
  • type
  • alias of sqlalchemy.sql.sqltypes.DateTime

implement the mode ordered-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.

New in version 1.1.

Represent 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 backendthat does not provide support for sequences.

  • identifier = 'next_value'
  • name = 'next_value'
  • type = Integer()
  • identifier = 'now'

  • name = 'now'
  • type
  • alias of sqlalchemy.sql.sqltypes.DateTime

Implement the percent_rank hypothetical-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.

New in version 1.1.

implement the percentile_cont ordered-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 types.ARRAY of the sortexpression’s type.

New in version 1.1.

  • arrayfor_multi_clause = True_
  • identifier = 'percentile_cont'
  • name = 'percentile_cont'

implement the percentile_disc ordered-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 types.ARRAY of the sortexpression’s type.

New in version 1.1.

Implement the rank hypothetical-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.

New in version 1.1.

  • identifier = 'rank'
  • name = 'rank'
  • type = Integer()
    • sqlalchemy.sql.functions.registerfunction(_identifier, fn, package='_default')
    • Associate a callable with a particular func. name.

This is normally called by _GenericMeta, but is alsoavailable by itself so that a non-Function constructcan be associated with the func accessor (i.e.CAST, EXTRACT).

Implement the ROLLUP grouping operation.

This function is used as part of the GROUP BY of a statement,e.g. Select.group_by():

  1. stmt = select(
  2. [func.sum(table.c.value), table.c.col_1, table.c.col_2]
  3. ).group_by(func.rollup(table.c.col_1, table.c.col_2))

New in version 1.2.

  • identifier = 'rollup'
  • name = 'rollup'
  • identifier = 'session_user'

  • name = 'session_user'
  • type
  • alias of sqlalchemy.sql.sqltypes.String