Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Macro for type conversion #118

Open
Cheukting opened this issue Nov 19, 2024 · 0 comments
Open

[Question] Macro for type conversion #118

Cheukting opened this issue Nov 19, 2024 · 0 comments

Comments

@Cheukting
Copy link

Cheukting commented Nov 19, 2024

I have been digging around a little bit, and from the tutorial here:
https://marcogorelli.github.io/polars-plugins-tutorial/abs/

We will have to do some type conversion manually like this:

#[polars_expr(output_type_func=same_output_type)]
fn abs_numeric(inputs: &[Series]) -> PolarsResult<Series> {
    let s = &inputs[0];
    match s.dtype() {
        DataType::Int32 => Ok(impl_abs_numeric(s.i32().unwrap()).into_series()),
        DataType::Int64 => Ok(impl_abs_numeric(s.i64().unwrap()).into_series()),
        DataType::Float32 => Ok(impl_abs_numeric(s.f32().unwrap()).into_series()),
        DataType::Float64 => Ok(impl_abs_numeric(s.f64().unwrap()).into_series()),
        dtype => {
            polars_bail!(InvalidOperation:format!("dtype {dtype} not \
            supported for abs_numeric, expected Int32, Int64, Float32, Float64."))
        }
    }
}

I think this will be used commonly enough and wonder if there is already a macro provided in this library that will do this? If not, I am happy to help contribute to one.

I am thinking about one that takes in a numeric series and returns a corresponding ChunkedArray type. Something like:

let ca = to_chunkerd_array!(input[0])?;

Instead of a huge match cause:

let ca = match input.dtype() {
        DataType::Int32 => input.i32()?,
        DataType::Int64 => input.i64()?,
        DataType::Float32 => input.f32()?,
        DataType::Float64 => input.f64()?,
        dtype => {
            polars_bail!(InvalidOperation:format!("dtype {dtype} not \
            supported, expected Int32, Int64, Float32 or Float64."))
        }
    }

CC @MarcoGorelli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant