def _get_django_function(type_, name, qs):
    def IsNull(field_name):
        # https://code.djangoproject.com/ticket/32200
        if django.VERSION[:3] == (3, 1, 3):  # pragma: django 3.1.3
            return Subquery(
                qs.annotate(ddb_is_null=ExpressionWrapper(
                    Q(**{field_name: None}), output_field=BooleanField())).
                filter(pk=OuterRef("pk")).values("ddb_is_null")[:1],
                output_field=BooleanField(),
            )
        else:
            return ExpressionWrapper(Q(**{field_name: None}),
                                     output_field=BooleanField())

    if issubclass(type_, ArrayTypeMixin) and name == "length":
        return (ArrayLenTransform, NumberType, (), None, {})

    mapping = {
        "year": (functions.ExtractYear, NumberType, (), ASC, {
            "useGrouping": False
        }),
        "quarter": (functions.ExtractQuarter, NumberType, (), ASC, {}),
        "month":
        (functions.ExtractMonth, NumberChoiceType, _month_choices, ASC, {}),
        "month_start": (
            lambda x: functions.TruncMonth(x, DateField()),
            DateType,
            (),
            ASC,
            {},
        ),
        "day": (functions.ExtractDay, NumberType, (), ASC, {}),
        "week_day": (
            functions.ExtractWeekDay,
            NumberChoiceType,
            _weekday_choices,
            ASC,
            {},
        ),
        "hour": (functions.ExtractHour, NumberType, (), ASC, {}),
        "minute": (functions.ExtractMinute, NumberType, (), ASC, {}),
        "second": (functions.ExtractSecond, NumberType, (), ASC, {}),
        "date": (functions.TruncDate, DateType, (), ASC, {}),
        "is_null": (IsNull, IsNullType, (), None, {}),
        "length": (functions.Length, NumberType, (), None, {}),
    }
    mapping.update({
        "iso_year": (functions.ExtractIsoYear, NumberType, (), ASC, {}),
        "iso_week": (functions.ExtractWeek, NumberType, (), ASC, {}),
        "week_start": (
            lambda x: functions.TruncWeek(x, DateField()),
            DateType,
            (),
            ASC,
            {},
        ),
    })
    return mapping[name]
Esempio n. 2
0
    def _get_interval_annotation(self, key: str) -> Dict[str, Any]:
        map: Dict[str, Any] = {
            'minute': functions.TruncMinute('timestamp'),
            'hour': functions.TruncHour('timestamp'),
            'day': functions.TruncDay('timestamp'),
            'week': functions.TruncWeek('timestamp'),
            'month': functions.TruncMonth('timestamp'),
        }
        func = map.get(key)
        if func is None:
            return {'day': map.get('day')} # default

        return { key: func }
Esempio n. 3
0
def get_interval_annotation(key: str) -> Dict[str, Any]:
    map: Dict[str, Any] = {
        "minute": functions.TruncMinute("timestamp"),
        "hour": functions.TruncHour("timestamp"),
        "day": functions.TruncDay("timestamp"),
        "week": functions.TruncWeek("timestamp"),
        "month": functions.TruncMonth("timestamp"),
    }
    func = map.get(key)
    if func is None:
        return {"day": map.get("day")}  # default

    return {key: func}
Esempio n. 4
0
def get_interval_annotation(key: str) -> Dict[str, Any]:
    map: Dict[str, Any] = {
        "minute": functions.TruncMinute("timestamp"),
        "hour": functions.TruncHour("timestamp"),
        "day": functions.TruncDay("timestamp"),
        "week": functions.TruncWeek(
            ExpressionWrapper(F("timestamp") + datetime.timedelta(days=1), output_field=DateTimeField())
        ),
        "month": functions.TruncMonth("timestamp"),
    }
    func = map.get(key)
    if func is None:
        return {"day": map.get("day")}  # default

    return {key: func}
Esempio n. 5
0
def _get_django_function(name):
    mapping = {
        "year": (functions.ExtractYear, YearType),
        "quarter": (functions.ExtractQuarter, NumberType),
        "month": (functions.ExtractMonth, MonthType),
        "month_start":
        (lambda x: functions.TruncMonth(x, DateField()), DateType),
        "day": (functions.ExtractDay, NumberType),
        "week_day": (functions.ExtractWeekDay, WeekDayType),
        "hour": (functions.ExtractHour, NumberType),
        "minute": (functions.ExtractMinute, NumberType),
        "second": (functions.ExtractSecond, NumberType),
        "date": (functions.TruncDate, DateType),
        "is_null": (IsNull, IsNullType),
    }
    if django.VERSION >= (2, 2):  # pragma: no branch
        mapping.update({
            "iso_year": (functions.ExtractIsoYear, YearType),
            "iso_week": (functions.ExtractWeek, NumberType),
            "week_start":
            (lambda x: functions.TruncWeek(x, DateField()), DateType),
        })
    return mapping[name]