diff options
Diffstat (limited to 'lib/sqlalchemy/sql/elements.py')
-rw-r--r-- | lib/sqlalchemy/sql/elements.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 8df22b7a6..91d081ca0 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -2892,6 +2892,68 @@ class Over(ColumnElement): )) +class WithinGroup(ColumnElement): + """Represent an WITHIN GROUP clause. + + This is a special operator against so-called ordered-set + and hypothetical-set aggregate functions in the SQL:2008 standard. + """ + __visit_name__ = 'within_group' + + order_by = None + + def __init__(self, func, order_by): + """Produce an :class:`.WithinGroup` object against a function. + + Used against ordered-set aggregates for databases that support + ordered-set aggregate functions + + E.g.:: + + from sqlalchemy import within_group + within_group(func.percentile_disc(0.25), order_by='x') + + Would produce "PERCENTILE_DISC(0.25) WITHIN GROUP (ORDER BY x)". + + :param func: a :class:`.FunctionElement` construct, typically + generated by :data:`~.expression.func`. + :param order_by: a column element or string, or a list + of such, that will be used as the ORDER BY clause + of the WITHIN GROUP construct. + + This function is also available from the :data:`~.expression.func` + construct itself via the :meth:`.FunctionElement.within_group` method. + + .. versionadded:: 1.0 + + """ + self.func = func + self.order_by = ClauseList( + *util.to_list(order_by), + _literal_as_text=_literal_as_label_reference) + + @util.memoized_property + def type(self): + return self.func.type + + def get_children(self, **kwargs): + return [c for c in + (self.func, self.order_by) + if c is not None] + + def _copy_internals(self, clone=_clone, **kw): + self.func = clone(self.func, **kw) + self.order_by = clone(self.order_by, **kw) + + @property + def _from_objects(self): + return list(itertools.chain( + *[c._from_objects for c in + (self.func, self.order_by) + if c is not None] + )) + + class FunctionFilter(ColumnElement): """Represent a function FILTER clause. |