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

Code before:
            IR::Join { options, .. } if matches!(options.args.how, JoinType::AsOf(_)) => {
                [true; 2].into()
            },
            IR::Join {
                input_left: _,
                input_right: _,
                schema: _,
                left_on: _,
                right_on: _,
                options,
            } if all_outputs_unordered => {
                // If the join maintains order, but the output has undefined order. Remove the
                // ordering.
                if !matches!(options.args.maintain_order, MOJ::None) {
                    let mut new_options = options.as_ref().clone();
                    new_options.args.maintain_order = MOJ::None;
                    *options = Arc::new(new_options);
                }

                // Join `on` expressions are elementwise so we don't have to inspect the order
                // sensitivity.
                [false, false].into()
            },
            #[cfg(feature = "asof_join")]
            IR::Join { options, .. } if matches!(options.args.how, JoinType::AsOf(_)) => {
                [true, false].into()
            },
            IR::Join {
                input_left: _,
                input_right: _,
                schema: _,
                left_on: _,
                right_on: _,
                options,
            } => {
                use MaintainOrderJoin as M;
                let left_input = matches!(
                    options.args.maintain_order,
                    M::Left | M::LeftRight | M::RightLeft
                );
                let right_input = matches!(
                    options.args.maintain_order,
                    M::Right | M::RightLeft | M::LeftRight
                );

                [left_input, right_input].into()
            },
            IR::Distinct { input: _, options } => {
                options.maintain_order &= !all_outputs_unordered;

                let is_order_observing = options.maintain_order

Review comment (performance):
The [`join_asof`](https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.join_asof.html) documentation states that both inputs need to be sorted. I think that implies they always both need to be ordered. By returning `false` for the second argument you can make the optimizer accidentally delete a `sort` call downstream I believe, as a `sort` into an unordered input is useless.

Code after:
            IR::Join { options, .. } if matches!(options.args.how, JoinType::AsOf(_)) => {
                [true; 2].into()
            },
            IR::Join {
                input_left: _,
                input_right: _,
                schema: _,
                left_on: _,
                right_on: _,
                options,
            } if all_outputs_unordered => {
                // If the join maintains order, but the output has undefined order. Remove the
                // ordering.
                if !matches!(options.args.maintain_order, MOJ::None) {
                    let mut new_options = options.as_ref().clone();
                    new_options.args.maintain_order = MOJ::None;
                    *options = Arc::new(new_options);
                }

                // Join `on` expressions are elementwise so we don't have to inspect the order
                // sensitivity.
                [false, false].into()
            },
            IR::Join {
                input_left: _,
                input_right: _,
                schema: _,
                left_on: _,
                right_on: _,
                options,
            } => {
                use MaintainOrderJoin as M;
                let left_input = matches!(
                    options.args.maintain_order,
                    M::Left | M::LeftRight | M::RightLeft
                );
                let right_input = matches!(
                    options.args.maintain_order,
                    M::Right | M::RightLeft | M::LeftRight
                );

                [left_input, right_input].into()
            },
            IR::Distinct { input: _, options } => {
                options.maintain_order &= !all_outputs_unordered;

                let is_order_observing = options.maintain_order
                    || matches!(
                        options.keep_strategy,
                        UniqueKeepStrategy::First | UniqueKeepStrategy::Last
                    );