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

Code before:

                if !accessed_input_names.contains(e.output_name())
                    && aexpr_to_leaf_names_iter(e.node(), expr_arena)
                        .all(|name| !accessed_input_names.contains(name))
                {
                    input_name_to_expr_map.insert(e.output_name().clone(), e.clone());
                    continue;
                }
            }

            new_current_exprs.push(e.clone());
        }

        if new_current_exprs.len() == current_exprs.len() {
            continue;
        }

        input_exprs.clear();

        for (output_name, e) in input_name_to_expr_map
            .iter()
            .map(|x| (x.0.clone(), x.1.clone()))
        {
            input_exprs.push(e);

            if !input_schema.contains(&output_name) {
                let dtype = current_schema.get(&output_name).unwrap().clone();
                Arc::make_mut(input_schema).insert(output_name, dtype);
            }
        }

        if new_current_exprs.is_empty() {
            let input_ir = input_ir.clone();
            lp_arena.replace(current, input_ir);
            *ir_stack.last_mut().unwrap() = current;
            continue;
        }

        let fix_output_order = current_exprs.iter().any(|e| {
            input_schema
                .index_of(e.output_name())
                .is_some_and(|i| i != current_schema.index_of(e.output_name()).unwrap())
        });

        current_exprs.clear();
        std::mem::swap(current_exprs, &mut new_current_exprs);

        if fix_output_order {
            let projection = current_schema.clone();

            Arc::make_mut(current_schema)

Review comment (suggestion):
here, merged `with_columns()` expression could have the same output name but different dtype


Code after:
                candidate_idx += 1;

                if !accessed_input_names.contains(e.output_name())
                    && aexpr_to_leaf_names_iter(e.node(), expr_arena)
                        .all(|name| !accessed_input_names.contains(name))
                {
                    input_name_to_expr_map.insert(e.output_name().clone(), (e.clone(), false));
                    continue;
                }
            }

            new_current_exprs.push(e.clone());
        }

        if new_current_exprs.len() == current_exprs.len() {
            continue;
        }

        input_exprs.clear();

        for (output_name, (e, is_original)) in input_name_to_expr_map
            .iter()
            .map(|x| (x.0.clone(), x.1.clone()))
        {
            input_exprs.push(e);

            if !is_original {
                let dtype = current_schema.get(&output_name).unwrap().clone();
                Arc::make_mut(input_schema).insert(output_name, dtype);
            }
        }

        if new_current_exprs.is_empty() {
            let input_ir = input_ir.clone();
            lp_arena.replace(current, input_ir);
            *ir_stack.last_mut().unwrap() = current;
            continue;
        }

        let fix_output_order = current_exprs.iter().any(|e| {
            input_schema
                .index_of(e.output_name())
                .is_some_and(|i| i != current_schema.index_of(e.output_name()).unwrap())
        });

        current_exprs.clear();
        std::mem::swap(current_exprs, &mut new_current_exprs);

        if fix_output_order {
            let projection = current_schema.clone();