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

Code before:
                    index: index.into_iter().collect(),
                    variable_name: args.variable_name.clone(),
                    value_name: args.value_name,
                };

                FunctionIR::Unpivot {
                    args: Arc::new(args),
                    schema: Default::default(),
                }
            },
            DslFunction::FunctionIR(func) => func,
            DslFunction::RowIndex { name, offset } => FunctionIR::RowIndex {
                name,
                offset,
                schema: Default::default(),
            },
            DslFunction::Unnest(selector) => {
                let columns = selector.into_columns(input_schema, &Default::default())?;
                let columns = columns.into_iter().collect();
                let cols = Arc::clone(&columns);
                for col in cols.iter() {
                    let dtype_opt = input_schema.get(col.as_str());
                    match dtype_opt {
                        Some(dtype) => {
                            if !dtype.is_struct() {
                                let dtype_str = dtype.to_string();
                                let msg = format!(
                                    "invalid dtype: expected 'Struct', got '{}' for '{}'",
                                    dtype_str, col
                                );
                                return Err(PolarsError::InvalidOperation(msg.into()));
                            }
                        },
                        None => {
                            let msg = format!("'unnest' on column: '{}' is invalid", col);
                            return Err(PolarsError::ColumnNotFound(msg.into()));
                        },
                    }
                }
                FunctionIR::Unnest { columns }
            },
            #[cfg(feature = "python")]
            DslFunction::OpaquePython(inner) => FunctionIR::OpaquePython(inner),
            DslFunction::Stats(_)
            | DslFunction::FillNan(_)
            | DslFunction::Rename { .. }
            | DslFunction::Explode { .. } => {
                // We should not reach this.
                panic!("impl error")
            },
        };

Review comment (bug):
I think we can use the debug formatter directly; `polars_bail!(InvalidOperation: "invalid dtype: expected 'Struct', got '{:?}' for '{}'", dtype, col)`

Code after:
                    index: index.into_iter().collect(),
                    variable_name: args.variable_name.clone(),
                    value_name: args.value_name,
                };

                FunctionIR::Unpivot {
                    args: Arc::new(args),
                    schema: Default::default(),
                }
            },
            DslFunction::FunctionIR(func) => func,
            DslFunction::RowIndex { name, offset } => FunctionIR::RowIndex {
                name,
                offset,
                schema: Default::default(),
            },
            DslFunction::Unnest(selector) => {
                let columns = selector.into_columns(input_schema, &Default::default())?;
                let columns: Arc<[PlSmallStr]> = columns.into_iter().collect();
                for col in columns.iter() {
                    let dtype = input_schema.try_get(col.as_str())?;
                    polars_ensure!(
                        dtype.is_struct(),
                        InvalidOperation: "invalid dtype: expected 'Struct', got '{:?}' for '{}'", dtype, col
                    );
                }
                FunctionIR::Unnest { columns }
            },
            #[cfg(feature = "python")]
            DslFunction::OpaquePython(inner) => FunctionIR::OpaquePython(inner),
            DslFunction::Stats(_)
            | DslFunction::FillNan(_)
            | DslFunction::Rename { .. }
            | DslFunction::Explode { .. } => {
                // We should not reach this.
                panic!("impl error")
            },
        };
        Ok(function)
    }
}

impl Debug for DslFunction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self}")
    }
}

impl Display for DslFunction {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        use DslFunction::*;