from .sql import (
+ aggregatefilter,
alias,
and_,
asc,
Selectable,
TableClause,
Update,
+ aggregatefilter,
alias,
and_,
asc,
)
)
+ def visit_aggregatefilter(self, aggregatefilter, **kwargs):
+ return "%s FILTER (WHERE %s)" % (
+ aggregatefilter.func._compiler_dispatch(self, **kwargs),
+ aggregatefilter.criterion._compiler_dispatch(self, **kwargs)
+ )
+
def visit_extract(self, extract, **kwargs):
field = self.extract_map.get(extract.field, extract.field)
return "EXTRACT(%s FROM %s)" % (
))
+class AggregateFilter(ColumnElement):
+ """Represent an aggregate FILTER clause.
+
+ This is a special operator against aggregate functions,
+ which controls which rows are passed to it.
+ It's supported only by certain database backends.
+
+ """
+ __visit_name__ = 'aggregatefilter'
+
+ criterion = None
+
+ def __init__(self, func, *criterion):
+ """Produce an :class:`.AggregateFilter` object against a function.
+
+ Used against aggregate functions,
+ for database backends that support aggregate "FILTER" clause.
+
+ E.g.::
+
+ from sqlalchemy import aggregatefilter
+ aggregatefilter(func.count(1), MyClass.name == 'some name')
+
+ Would produce "COUNT(1) FILTER (WHERE myclass.name = 'some name')".
+
+ This function is also available from the :data:`~.expression.func`
+ construct itself via the :meth:`.FunctionElement.filter` method.
+
+ """
+ self.func = func
+ self.filter(*criterion)
+
+ def filter(self, *criterion):
+ for criterion in list(criterion):
+ criterion = _expression_literal_as_text(criterion)
+
+ if self.criterion is not None:
+ self.criterion = self.criterion & criterion
+ else:
+ self.criterion = criterion
+
+ return self
+
+ @util.memoized_property
+ def type(self):
+ return self.func.type
+
+ def get_children(self, **kwargs):
+ return [c for c in
+ (self.func, self.criterion)
+ if c is not None]
+
+ def _copy_internals(self, clone=_clone, **kw):
+ self.func = clone(self.func, **kw)
+ if self.criterion is not None:
+ self.criterion = clone(self.criterion, **kw)
+
+ @property
+ def _from_objects(self):
+ return list(itertools.chain(
+ *[c._from_objects for c in (self.func, self.criterion)
+ if c is not None]
+ ))
+
+
class Label(ColumnElement):
"""Represents a column label (AS).
True_, False_, BinaryExpression, Tuple, TypeClause, Extract, \
Grouping, not_, \
collate, literal_column, between,\
- literal, outparam, type_coerce, ClauseList
+ literal, outparam, type_coerce, ClauseList, AggregateFilter
from .elements import SavepointClause, RollbackToSavepointClause, \
ReleaseSavepointClause
insert = public_factory(Insert, ".expression.insert")
update = public_factory(Update, ".expression.update")
delete = public_factory(Delete, ".expression.delete")
+aggregatefilter = public_factory(
+ AggregateFilter, ".expression.aggregatefilter")
# internal functions still being called from tests and the ORM,
from .base import Executable, ColumnCollection
from .elements import ClauseList, Cast, Extract, _literal_as_binds, \
literal_column, _type_from_args, ColumnElement, _clone,\
- Over, BindParameter
+ Over, BindParameter, AggregateFilter
from .selectable import FromClause, Select, Alias
from . import operators
"""
return Over(self, partition_by=partition_by, order_by=order_by)
+ def filter(self, *criterion):
+ """Produce a FILTER clause against this function.
+
+ Used against aggregate functions,
+ for database backends that support aggregate "FILTER" clause.
+
+ The expression::
+
+ func.count(1).filter(True)
+
+ is shorthand for::
+
+ from sqlalchemy import aggregatefilter
+ aggregatefilter(func.count(1), True)
+
+ See :func:`~.expression.aggregatefilter` for a full description.
+
+ """
+ if not criterion:
+ return self
+ return AggregateFilter(self, *criterion)
+
@property
def _from_objects(self):
return self.clauses._from_objects