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

Code before:
    @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 or self.group_by):
            # An empty set is always a subset of something.
            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.
        """

Review comment (suggestion):
Is there a reason you didn't opt for:
```suggestion
            if inner_query.orderby_issubset_groupby:
                inner_query.clear_ordering(force=False)
```

Code after:
            # 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.

        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.