Repository: pola-rs/polars
File: crates/polars-plan/src/plans/optimizer/set_order/expr_pushdown.rs (Rust)

Code before:
            // Input order agnostic aggregations.
            IRAggExpr::Min { input: node, .. }
            | IRAggExpr::Max { input: node, .. }
            | IRAggExpr::Median(node)
            | IRAggExpr::NUnique(node)
            | IRAggExpr::Mean(node)
            | IRAggExpr::Sum(node)
            | IRAggExpr::Count { input: node, .. }
            | IRAggExpr::Std(node, _)
            | IRAggExpr::Var(node, _) => {
                // Input order is deregarded, but must not observe order.
                _ = rec!(*node);
                O::None
            },
            IRAggExpr::Quantile { expr, quantile, .. } => {
                // Input and quantile order is deregarded, but must not observe order.
                _ = rec!(*expr);
                _ = rec!(*quantile);
                O::None
            },

            // Input order observing aggregations.
            IRAggExpr::Implode(node)
            | IRAggExpr::First(node)
            | IRAggExpr::Last(node)
            | IRAggExpr::Single(node) => {
                if rec!(*node).has_frame_ordering() {
                    return Err(FrameOrderObserved);
                }
                O::None
            },

            // @NOTE: This aggregation makes very little sense. We do the most pessimistic thing
            // possible here.
            IRAggExpr::AggGroups(node) => {
                if rec!(*node).has_frame_ordering() {
                    return Err(FrameOrderObserved);
                }

                O::Independent
            },
        },

        AExpr::Gather {
            expr,
            idx,
            returns_scalar,
        } => {
            let expr = rec!(*expr);
            let idx = rec!(*idx);


Review comment (suggestion):
Missed optimization - `Single` doesn't observe ordering - it only observes length.

It should be in a separate branch that just returns `O::None` (i.e. no observable orderings, since we are returning a scalar in the `Ok(_)` case).


Code after:
                    zipped | O::Independent
                } else {
                    O::Independent
                }
            },

            AExpr::Agg(agg) => match agg {
                // Input order agnostic aggregations.
                IRAggExpr::Min { input: node, .. }
                | IRAggExpr::Max { input: node, .. }
                | IRAggExpr::Median(node)
                | IRAggExpr::NUnique(node)
                | IRAggExpr::Mean(node)
                | IRAggExpr::Sum(node)
                | IRAggExpr::Count { input: node, .. }
                | IRAggExpr::Std(node, _)
                | IRAggExpr::Var(node, _)
                | IRAggExpr::Item(node) => {
                    // Input order is deregarded, but must not observe order.
                    _ = rec!(*node);
                    O::None
                },
                IRAggExpr::Quantile { expr, quantile, .. } => {
                    // Input and quantile order is deregarded, but must not observe order.
                    _ = rec!(*expr);
                    _ = rec!(*quantile);
                    O::None
                },

                // Input order observing aggregations.
                IRAggExpr::Implode(node) | IRAggExpr::First(node) | IRAggExpr::Last(node) => {
                    if rec!(*node).column_ordering_observable() {
                        return Err(ColumnOrderObserved);
                    }
                    O::None
                },

                // @NOTE: This aggregation makes very little sense. We do the most pessimistic thing
                // possible here.
                IRAggExpr::AggGroups(node) => {
                    if rec!(*node).column_ordering_observable() {
                        return Err(ColumnOrderObserved);
                    }

                    O::Independent
                },
            },

            AExpr::Gather {
                expr,
                idx,