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

Code before:
                raise FieldError(
                    "Using an aggregate in order_by() without also including "
                    "it in annotate() is not allowed: %s" % item
                )
        if errors:
            raise FieldError("Invalid order_by arguments: %s" % errors)
        if ordering:
            self.order_by += ordering
        else:
            self.default_ordering = False

    @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 = ()

Review comment (suggestion):
No need to clone immediately for these two check as it's a potentially costly operation.

```suggestion
        if self.group_by is False:
            # there is no use case for that, but we need to be sure
            return False
        if self.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
        q = self.clone()
```

Code after:
        if ordering:
            self.order_by += ordering
        else:
            self.default_ordering = False

    @property
    def orderby_issubset_groupby(self):
        if self.extra_order_by:
            # Raw SQL from extra(order_by=...) can't be reliably compared
            # against resolved OrderBy/Col expressions. Treat as not a subset.
            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.