start-pack

This commit is contained in:
bdrtr 2025-04-28 15:42:23 +03:00
commit 3e1fa59b3d
5723 changed files with 757971 additions and 0 deletions

View file

@ -0,0 +1,2 @@
from .general import * # NOQA
from .statistics import * # NOQA

View file

@ -0,0 +1,65 @@
from django.contrib.postgres.fields import ArrayField
from django.db.models import Aggregate, BooleanField, JSONField, TextField, Value
from .mixins import OrderableAggMixin
__all__ = [
"ArrayAgg",
"BitAnd",
"BitOr",
"BitXor",
"BoolAnd",
"BoolOr",
"JSONBAgg",
"StringAgg",
]
class ArrayAgg(OrderableAggMixin, Aggregate):
function = "ARRAY_AGG"
template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)"
allow_distinct = True
@property
def output_field(self):
return ArrayField(self.source_expressions[0].output_field)
class BitAnd(Aggregate):
function = "BIT_AND"
class BitOr(Aggregate):
function = "BIT_OR"
class BitXor(Aggregate):
function = "BIT_XOR"
class BoolAnd(Aggregate):
function = "BOOL_AND"
output_field = BooleanField()
class BoolOr(Aggregate):
function = "BOOL_OR"
output_field = BooleanField()
class JSONBAgg(OrderableAggMixin, Aggregate):
function = "JSONB_AGG"
template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)"
allow_distinct = True
output_field = JSONField()
class StringAgg(OrderableAggMixin, Aggregate):
function = "STRING_AGG"
template = "%(function)s(%(distinct)s%(expressions)s %(order_by)s)"
allow_distinct = True
output_field = TextField()
def __init__(self, expression, delimiter, **extra):
delimiter_expr = Value(str(delimiter))
super().__init__(expression, delimiter_expr, **extra)

View file

@ -0,0 +1,62 @@
import warnings
from django.core.exceptions import FullResultSet
from django.db.models.expressions import OrderByList
from django.utils.deprecation import RemovedInDjango61Warning
class OrderableAggMixin:
# RemovedInDjango61Warning: When the deprecation ends, replace with:
# def __init__(self, *expressions, order_by=(), **extra):
def __init__(self, *expressions, ordering=(), order_by=(), **extra):
# RemovedInDjango61Warning.
if ordering:
warnings.warn(
"The ordering argument is deprecated. Use order_by instead.",
category=RemovedInDjango61Warning,
stacklevel=2,
)
if order_by:
raise TypeError("Cannot specify both order_by and ordering.")
order_by = ordering
if not order_by:
self.order_by = None
elif isinstance(order_by, (list, tuple)):
self.order_by = OrderByList(*order_by)
else:
self.order_by = OrderByList(order_by)
super().__init__(*expressions, **extra)
def resolve_expression(self, *args, **kwargs):
if self.order_by is not None:
self.order_by = self.order_by.resolve_expression(*args, **kwargs)
return super().resolve_expression(*args, **kwargs)
def get_source_expressions(self):
return super().get_source_expressions() + [self.order_by]
def set_source_expressions(self, exprs):
*exprs, self.order_by = exprs
return super().set_source_expressions(exprs)
def as_sql(self, compiler, connection):
*source_exprs, filtering_expr, order_by_expr = self.get_source_expressions()
order_by_sql = ""
order_by_params = []
if order_by_expr is not None:
order_by_sql, order_by_params = compiler.compile(order_by_expr)
filter_params = []
if filtering_expr is not None:
try:
_, filter_params = compiler.compile(filtering_expr)
except FullResultSet:
pass
source_params = []
for source_expr in source_exprs:
source_params += compiler.compile(source_expr)[1]
sql, _ = super().as_sql(compiler, connection, order_by=order_by_sql)
return sql, (*source_params, *order_by_params, *filter_params)

View file

@ -0,0 +1,75 @@
from django.db.models import Aggregate, FloatField, IntegerField
__all__ = [
"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):
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
)
class Corr(StatAggregate):
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)
class RegrAvgX(StatAggregate):
function = "REGR_AVGX"
class RegrAvgY(StatAggregate):
function = "REGR_AVGY"
class RegrCount(StatAggregate):
function = "REGR_COUNT"
output_field = IntegerField()
empty_result_set_value = 0
class RegrIntercept(StatAggregate):
function = "REGR_INTERCEPT"
class RegrR2(StatAggregate):
function = "REGR_R2"
class RegrSlope(StatAggregate):
function = "REGR_SLOPE"
class RegrSXX(StatAggregate):
function = "REGR_SXX"
class RegrSXY(StatAggregate):
function = "REGR_SXY"
class RegrSYY(StatAggregate):
function = "REGR_SYY"