测试gitnore
This commit is contained in:
@@ -1,109 +1,68 @@
|
||||
import warnings
|
||||
|
||||
from django.contrib.postgres.fields import ArrayField
|
||||
from django.db.models import Aggregate, BooleanField, JSONField, Value
|
||||
from django.utils.deprecation import RemovedInDjango50Warning
|
||||
|
||||
from .mixins import OrderableAggMixin
|
||||
|
||||
__all__ = [
|
||||
"ArrayAgg",
|
||||
"BitAnd",
|
||||
"BitOr",
|
||||
"BoolAnd",
|
||||
"BoolOr",
|
||||
"JSONBAgg",
|
||||
"StringAgg",
|
||||
'ArrayAgg', 'BitAnd', 'BitOr', 'BoolAnd', 'BoolOr', 'JSONBAgg', 'StringAgg',
|
||||
]
|
||||
|
||||
|
||||
# RemovedInDjango50Warning
|
||||
NOT_PROVIDED = object()
|
||||
|
||||
|
||||
class DeprecatedConvertValueMixin:
|
||||
def __init__(self, *expressions, default=NOT_PROVIDED, **extra):
|
||||
if default is NOT_PROVIDED:
|
||||
default = None
|
||||
self._default_provided = False
|
||||
else:
|
||||
self._default_provided = True
|
||||
super().__init__(*expressions, default=default, **extra)
|
||||
|
||||
def convert_value(self, value, expression, connection):
|
||||
if value is None and not self._default_provided:
|
||||
warnings.warn(self.deprecation_msg, category=RemovedInDjango50Warning)
|
||||
return self.deprecation_value
|
||||
return value
|
||||
|
||||
|
||||
class ArrayAgg(DeprecatedConvertValueMixin, OrderableAggMixin, Aggregate):
|
||||
function = "ARRAY_AGG"
|
||||
template = "%(function)s(%(distinct)s%(expressions)s %(ordering)s)"
|
||||
class ArrayAgg(OrderableAggMixin, Aggregate):
|
||||
function = 'ARRAY_AGG'
|
||||
template = '%(function)s(%(distinct)s%(expressions)s %(ordering)s)'
|
||||
allow_distinct = True
|
||||
|
||||
# RemovedInDjango50Warning
|
||||
deprecation_value = property(lambda self: [])
|
||||
deprecation_msg = (
|
||||
"In Django 5.0, ArrayAgg() will return None instead of an empty list "
|
||||
"if there are no rows. Pass default=None to opt into the new behavior "
|
||||
"and silence this warning or default=Value([]) to keep the previous "
|
||||
"behavior."
|
||||
)
|
||||
|
||||
@property
|
||||
def output_field(self):
|
||||
return ArrayField(self.source_expressions[0].output_field)
|
||||
|
||||
def convert_value(self, value, expression, connection):
|
||||
if not value:
|
||||
return []
|
||||
return value
|
||||
|
||||
|
||||
class BitAnd(Aggregate):
|
||||
function = "BIT_AND"
|
||||
function = 'BIT_AND'
|
||||
|
||||
|
||||
class BitOr(Aggregate):
|
||||
function = "BIT_OR"
|
||||
function = 'BIT_OR'
|
||||
|
||||
|
||||
class BoolAnd(Aggregate):
|
||||
function = "BOOL_AND"
|
||||
function = 'BOOL_AND'
|
||||
output_field = BooleanField()
|
||||
|
||||
|
||||
class BoolOr(Aggregate):
|
||||
function = "BOOL_OR"
|
||||
function = 'BOOL_OR'
|
||||
output_field = BooleanField()
|
||||
|
||||
|
||||
class JSONBAgg(DeprecatedConvertValueMixin, OrderableAggMixin, Aggregate):
|
||||
function = "JSONB_AGG"
|
||||
template = "%(function)s(%(distinct)s%(expressions)s %(ordering)s)"
|
||||
class JSONBAgg(OrderableAggMixin, Aggregate):
|
||||
function = 'JSONB_AGG'
|
||||
template = '%(function)s(%(distinct)s%(expressions)s %(ordering)s)'
|
||||
allow_distinct = True
|
||||
output_field = JSONField()
|
||||
|
||||
# RemovedInDjango50Warning
|
||||
deprecation_value = "[]"
|
||||
deprecation_msg = (
|
||||
"In Django 5.0, JSONBAgg() will return None instead of an empty list "
|
||||
"if there are no rows. Pass default=None to opt into the new behavior "
|
||||
"and silence this warning or default=Value('[]') to keep the previous "
|
||||
"behavior."
|
||||
)
|
||||
def convert_value(self, value, expression, connection):
|
||||
if not value:
|
||||
return '[]'
|
||||
return value
|
||||
|
||||
|
||||
class StringAgg(DeprecatedConvertValueMixin, OrderableAggMixin, Aggregate):
|
||||
function = "STRING_AGG"
|
||||
template = "%(function)s(%(distinct)s%(expressions)s %(ordering)s)"
|
||||
class StringAgg(OrderableAggMixin, Aggregate):
|
||||
function = 'STRING_AGG'
|
||||
template = '%(function)s(%(distinct)s%(expressions)s %(ordering)s)'
|
||||
allow_distinct = True
|
||||
|
||||
# RemovedInDjango50Warning
|
||||
deprecation_value = ""
|
||||
deprecation_msg = (
|
||||
"In Django 5.0, StringAgg() will return None instead of an empty "
|
||||
"string if there are no rows. Pass default=None to opt into the new "
|
||||
"behavior and silence this warning or default=Value('') to keep the "
|
||||
"previous behavior."
|
||||
)
|
||||
|
||||
def __init__(self, expression, delimiter, **extra):
|
||||
delimiter_expr = Value(str(delimiter))
|
||||
super().__init__(expression, delimiter_expr, **extra)
|
||||
|
||||
def convert_value(self, value, expression, connection):
|
||||
if not value:
|
||||
return ''
|
||||
return value
|
||||
|
||||
@@ -2,26 +2,21 @@ from django.db.models import F, OrderBy
|
||||
|
||||
|
||||
class OrderableAggMixin:
|
||||
|
||||
def __init__(self, *expressions, ordering=(), **extra):
|
||||
if not isinstance(ordering, (list, tuple)):
|
||||
ordering = [ordering]
|
||||
ordering = ordering or []
|
||||
# Transform minus sign prefixed strings into an OrderBy() expression.
|
||||
ordering = (
|
||||
(
|
||||
OrderBy(F(o[1:]), descending=True)
|
||||
if isinstance(o, str) and o[0] == "-"
|
||||
else o
|
||||
)
|
||||
(OrderBy(F(o[1:]), descending=True) if isinstance(o, str) and o[0] == '-' else o)
|
||||
for o in ordering
|
||||
)
|
||||
super().__init__(*expressions, **extra)
|
||||
self.ordering = self._parse_expressions(*ordering)
|
||||
|
||||
def resolve_expression(self, *args, **kwargs):
|
||||
self.ordering = [
|
||||
expr.resolve_expression(*args, **kwargs) for expr in self.ordering
|
||||
]
|
||||
self.ordering = [expr.resolve_expression(*args, **kwargs) for expr in self.ordering]
|
||||
return super().resolve_expression(*args, **kwargs)
|
||||
|
||||
def as_sql(self, compiler, connection):
|
||||
@@ -32,21 +27,17 @@ class OrderableAggMixin:
|
||||
expr_sql, expr_params = compiler.compile(expr)
|
||||
ordering_expr_sql.append(expr_sql)
|
||||
ordering_params.extend(expr_params)
|
||||
sql, sql_params = super().as_sql(
|
||||
compiler,
|
||||
connection,
|
||||
ordering=("ORDER BY " + ", ".join(ordering_expr_sql)),
|
||||
)
|
||||
sql, sql_params = super().as_sql(compiler, connection, ordering=(
|
||||
'ORDER BY ' + ', '.join(ordering_expr_sql)
|
||||
))
|
||||
return sql, sql_params + ordering_params
|
||||
return super().as_sql(compiler, connection, ordering="")
|
||||
return super().as_sql(compiler, connection, ordering='')
|
||||
|
||||
def set_source_expressions(self, exprs):
|
||||
# Extract the ordering expressions because ORDER BY clause is handled
|
||||
# in a custom way.
|
||||
self.ordering = exprs[self._get_ordering_expressions_index() :]
|
||||
return super().set_source_expressions(
|
||||
exprs[: self._get_ordering_expressions_index()]
|
||||
)
|
||||
self.ordering = exprs[self._get_ordering_expressions_index():]
|
||||
return super().set_source_expressions(exprs[:self._get_ordering_expressions_index()])
|
||||
|
||||
def get_source_expressions(self):
|
||||
return super().get_source_expressions() + self.ordering
|
||||
|
||||
@@ -1,75 +1,65 @@
|
||||
from django.db.models import Aggregate, FloatField, IntegerField
|
||||
|
||||
__all__ = [
|
||||
"CovarPop",
|
||||
"Corr",
|
||||
"RegrAvgX",
|
||||
"RegrAvgY",
|
||||
"RegrCount",
|
||||
"RegrIntercept",
|
||||
"RegrR2",
|
||||
"RegrSlope",
|
||||
"RegrSXX",
|
||||
"RegrSXY",
|
||||
"RegrSYY",
|
||||
"StatAggregate",
|
||||
'CovarPop', 'Corr', 'RegrAvgX', 'RegrAvgY', 'RegrCount', 'RegrIntercept',
|
||||
'RegrR2', 'RegrSlope', 'RegrSXX', 'RegrSXY', 'RegrSYY', 'StatAggregate',
|
||||
]
|
||||
|
||||
|
||||
class StatAggregate(Aggregate):
|
||||
output_field = FloatField()
|
||||
|
||||
def __init__(self, y, x, output_field=None, filter=None, default=None):
|
||||
def __init__(self, y, x, output_field=None, filter=None):
|
||||
if not x or not y:
|
||||
raise ValueError("Both y and x must be provided.")
|
||||
super().__init__(
|
||||
y, x, output_field=output_field, filter=filter, default=default
|
||||
)
|
||||
raise ValueError('Both y and x must be provided.')
|
||||
super().__init__(y, x, output_field=output_field, filter=filter)
|
||||
|
||||
|
||||
class Corr(StatAggregate):
|
||||
function = "CORR"
|
||||
function = 'CORR'
|
||||
|
||||
|
||||
class CovarPop(StatAggregate):
|
||||
def __init__(self, y, x, sample=False, filter=None, default=None):
|
||||
self.function = "COVAR_SAMP" if sample else "COVAR_POP"
|
||||
super().__init__(y, x, filter=filter, default=default)
|
||||
def __init__(self, y, x, sample=False, filter=None):
|
||||
self.function = 'COVAR_SAMP' if sample else 'COVAR_POP'
|
||||
super().__init__(y, x, filter=filter)
|
||||
|
||||
|
||||
class RegrAvgX(StatAggregate):
|
||||
function = "REGR_AVGX"
|
||||
function = 'REGR_AVGX'
|
||||
|
||||
|
||||
class RegrAvgY(StatAggregate):
|
||||
function = "REGR_AVGY"
|
||||
function = 'REGR_AVGY'
|
||||
|
||||
|
||||
class RegrCount(StatAggregate):
|
||||
function = "REGR_COUNT"
|
||||
function = 'REGR_COUNT'
|
||||
output_field = IntegerField()
|
||||
empty_result_set_value = 0
|
||||
|
||||
def convert_value(self, value, expression, connection):
|
||||
return 0 if value is None else value
|
||||
|
||||
|
||||
class RegrIntercept(StatAggregate):
|
||||
function = "REGR_INTERCEPT"
|
||||
function = 'REGR_INTERCEPT'
|
||||
|
||||
|
||||
class RegrR2(StatAggregate):
|
||||
function = "REGR_R2"
|
||||
function = 'REGR_R2'
|
||||
|
||||
|
||||
class RegrSlope(StatAggregate):
|
||||
function = "REGR_SLOPE"
|
||||
function = 'REGR_SLOPE'
|
||||
|
||||
|
||||
class RegrSXX(StatAggregate):
|
||||
function = "REGR_SXX"
|
||||
function = 'REGR_SXX'
|
||||
|
||||
|
||||
class RegrSXY(StatAggregate):
|
||||
function = "REGR_SXY"
|
||||
function = 'REGR_SXY'
|
||||
|
||||
|
||||
class RegrSYY(StatAggregate):
|
||||
function = "REGR_SYY"
|
||||
function = 'REGR_SYY'
|
||||
|
||||
Reference in New Issue
Block a user