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

map_elements cause R session crach for struct object #993

Open
Yunuuuu opened this issue Mar 30, 2024 · 4 comments
Open

map_elements cause R session crach for struct object #993

Yunuuuu opened this issue Mar 30, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@Yunuuuu
Copy link
Contributor

Yunuuuu commented Mar 30, 2024

map_elements crash for struct object, Here is the map_elements document for struct in Python:
https://docs.pola.rs/user-guide/expressions/user-defined-functions/#combining-multiple-column-values

library(polars)
pl$DataFrame(mtcars)$
    select(pl$col("mpg", "cyl")$to_struct()$map_elements(\(x) x$mpg))

pl$DataFrame(mtcars)$
    select(pl$col("mpg", "cyl")$to_struct()$map_elements(\(x) x[["mpg"]]))

pl$DataFrame(mtcars)$
    select(
    pl$col("mpg", "cyl")$to_struct()$
        map_elements(\(x) x$struct$field("mpg"))
)
@eitsupi eitsupi added the bug Something isn't working label Mar 30, 2024
@eitsupi
Copy link
Collaborator

eitsupi commented Mar 30, 2024

Struct type is not supported yet.

let r_iter: Box<dyn Iterator<Item = Option<Robj>>> = match inp_type {
Float64 => apply_input!(self.0, f64, rfun, na_fun),
Float32 => apply_input!(self.0, f32, rfun, na_fun),
Int64 => apply_input!(self.0, i64, rfun, na_fun),
Int32 => apply_input!(self.0, i32, rfun, na_fun),
Int16 => apply_input!(self.0, i16, rfun, na_fun),
Int8 => apply_input!(self.0, i8, rfun, na_fun),
String => apply_input!(self.0, str, rfun, na_fun),
Boolean => apply_input!(self.0, bool, rfun, na_fun),
//List(..) => apply_input!(self.0, list, rfun, na_fun),
List(..) => {
let ca_list = self.0.list().unwrap();
let y = ca_list.into_iter().map(|opt_ser| {
if let Some(ser) = opt_ser {
rfun.call(pairlist!(RPolarsSeries(ser))).ok()
} else {
unreachable!("internal error: oh it was possible to get a None Series");
}
});
Box::new(y)
}
x => {
dbg!(x);
todo!("this input type is not implemented")
}
};

PRs are welcome!

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Mar 30, 2024

I'll try to implement it.

@etiennebacher
Copy link
Collaborator

etiennebacher commented Apr 2, 2024

Shorter reprex:

s = pl$Series(values = 1)$to_struct()
s$map_elements(\(x) x)

Error in .pr$Series$map_elements(self, fun, datatype, strict_return_type,  : 
  user function panicked: map_elements

@etiennebacher
Copy link
Collaborator

etiennebacher commented May 13, 2024

Turns out to_struct() shouldn't be available for all Expr, only for those in the list and arr subnamespaces: https://docs.pola.rs/py-polars/html/search.html?q=to_struct


Doesn't solve the issue though:

library(polars)

pl$DataFrame(mtcars)$
  select(pl$struct(c("mpg", "cyl"))$map_elements(\(x) x[["mpg"]]))
#>    1: user function raised an error: EvalError(lang!(function (s) {    s$map_elements(f, return_type, strict_return_type, allow_fail_eval)}, ExternalPtr.set_class(["RPolarsSeries"]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants