Refs #33304 -- Enclosed aggregate ordering logic in an expression.

This greatly simplifies the implementation of contrib.postgres'
OrderableAggMixin and allows for reuse in Window expressions.
This commit is contained in:
Simon Charette 2021-11-23 00:34:48 -05:00 committed by Mariusz Felisiak
parent a17becf4c7
commit e06dc4571e
2 changed files with 36 additions and 37 deletions

View file

@ -915,8 +915,8 @@ class Ref(Expression):
class ExpressionList(Func):
"""
An expression containing multiple expressions. Can be used to provide a
list of expressions as an argument to another expression, like an
ordering clause.
list of expressions as an argument to another expression, like a partition
clause.
"""
template = '%(expressions)s'
@ -933,6 +933,26 @@ class ExpressionList(Func):
return self.as_sql(compiler, connection, **extra_context)
class OrderByList(Func):
template = 'ORDER BY %(expressions)s'
def __init__(self, *expressions, **extra):
expressions = (
(
OrderBy(F(expr[1:]), descending=True)
if isinstance(expr, str) and expr[0] == '-'
else expr
)
for expr in expressions
)
super().__init__(*expressions, **extra)
def as_sql(self, *args, **kwargs):
if not self.source_expressions:
return '', ()
return super().as_sql(*args, **kwargs)
class ExpressionWrapper(SQLiteNumericMixin, Expression):
"""
An expression that can wrap another expression so that it can provide