Repository: django/django
File: django/db/models/sql/query.py (Python)

Code before:

    @property
    def orderby_issubset_groupby(self):
        """
        Return true if order_by of query is subset of group_by.
        """
        # we don't want to harm original query, so we need to clone it
        q = self.clone()
        if q.group_by is False:
            # there is no use case for that, but we need to be sure
            return False
        if q.group_by in (None, True):
            # it seems like there is no aggregation at all (None)
            # or there are all required groupbies(True) generated automatically
            # from models fields - so, it is safe to clear ordering
            return True
        order_by_set = set(
            [
                (
                    order_by.resolve_expression(q)
                    if hasattr(order_by, "resolve_expression")
                    else F(order_by).resolve_expression(q)
                )
                for order_by in q.order_by
            ]
        ).union(q.extra_order_by)
        group_by_set = set([group_by.resolve_expression(q) for group_by in q.group_by])
        return order_by_set.issubset(group_by_set)

    def clear_ordering(self, force=False, clear_default=True):
        """
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting
        query (not even the model's default).
        """
        if not force and (
            self.is_sliced or self.distinct_fields or self.select_for_update
        ):
            return
        self.order_by = ()
        self.extra_order_by = ()
        if clear_default:
            self.default_ordering = False

    def set_group_by(self, allow_aliases=True):
        """
        Expand the GROUP BY clause required by the query.

        This will usually be the set of all non-aggregate fields in the
        return data. If the database backend supports grouping by the

Review comment (suggestion):
You can also avoid the clone if `not (q.order_by or q.extra_order_by)` I believe as an empty set is necessarily a subset of the group by.

Code after:
            return False
        if self.group_by in (None, True):
            # There is either no aggregation at all (None), or the group by
            # is generated automatically from model fields (True), in which
            # case the order by is necessarily a subset of them.
            return True
        if not self.order_by:
            # Although an empty set is always a subset, there's no point in
            # clearing ordering when there isn't any. Avoid the clone() below.
            return True
        # Don't pollute the original query (might disrupt joins).
        q = self.clone()
        order_by_set = {
            (
                order_by.resolve_expression(q)
                if hasattr(order_by, "resolve_expression")
                else F(order_by).resolve_expression(q)
            )
            for order_by in q.order_by
        }
        return order_by_set.issubset(self.group_by)

    def clear_ordering(self, force=False, clear_default=True):
        """
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting
        query (not even the model's default).
        """
        if not force and (
            self.is_sliced or self.distinct_fields or self.select_for_update
        ):
            return
        self.order_by = ()
        self.extra_order_by = ()
        if clear_default:
            self.default_ordering = False

    def set_group_by(self, allow_aliases=True):
        """
        Expand the GROUP BY clause required by the query.

        This will usually be the set of all non-aggregate fields in the
        return data. If the database backend supports grouping by the
        primary key, and the query would be equivalent, the optimization
        will be made automatically.
        """
        if allow_aliases and self.values_select:
            # If grouping by aliases is allowed assign selected value aliases
            # by moving them to annotations.
            group_by_annotations = {}