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

Chore/extendr 0.7.0 support #1187

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
615 changes: 309 additions & 306 deletions src/rust/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ opt-level = 3 # was 1 to support 1.66, but since 1.70 is needed anyway it does n
opt-level = 3

[dependencies]
extendr-api = { git = "https://github.com/extendr/extendr", rev = "1895bfc8ee22347665900caa0e48da4f0b13232f", default-features = false, features = [
extendr-api = { version = "0.7.0", default-features = false, features = [
"result_list",
"serde",
] }
Expand Down
17 changes: 10 additions & 7 deletions src/rust/src/arrow_interop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ pub mod to_rust;
use polars_core::utils::arrow;

use extendr_api::prelude::*;
use std::result::Result;

#[derive(Debug)]
pub enum RArrowArrayClass {
ArrowArray,
NanoArrowArray,
}

impl<'a> FromRobj<'a> for RArrowArrayClass {
fn from_robj(robj: &Robj) -> std::result::Result<Self, &'static str> {
impl TryFrom<&Robj> for RArrowArrayClass {
type Error = extendr_api::Error;

fn try_from(robj: &Robj) -> Result<Self> {
if robj.inherits("nanoarrow_array") {
Ok(RArrowArrayClass::NanoArrowArray)
} else if robj.inherits("Array") {
Ok(RArrowArrayClass::ArrowArray)
} else {
Err("Robj does not inherit from Array or nanoarrow_array")
Err(Error::Other(
"Robj does not inherit from Array or nanoarrow_array".into(),
))
}
}
}
Expand All @@ -38,11 +41,11 @@ impl RArrowArrayClass {
}

pub trait RPackage {
fn get_export_array_func(&self) -> Result<Robj, Error>;
fn get_export_array_func(&self) -> Result<Robj>;
}

impl RPackage for ArrowRPackage {
fn get_export_array_func(&self) -> Result<Robj, Error> {
fn get_export_array_func(&self) -> Result<Robj> {
R!(r#"
function(array, exportable_array, exportable_schema) {
array$export_to_c(exportable_array, exportable_schema)
Expand All @@ -51,7 +54,7 @@ impl RPackage for ArrowRPackage {
}

impl RPackage for NanoArrowRPackage {
fn get_export_array_func(&self) -> Result<Robj, Error> {
fn get_export_array_func(&self) -> Result<Robj> {
R!(r#"
function(array, exportable_array, exportable_schema) {
nanoarrow::nanoarrow_pointer_export(
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/arrow_interop/to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn arrow_array_to_rust(arrow_array: Robj) -> Result<ArrayRef, String> {
)
};

RArrowArrayClass::from_robj(&arrow_array)?
RArrowArrayClass::try_from(&arrow_array)?
.get_package()
.get_export_array_func()?
.call(pairlist!(&arrow_array, ext_a, ext_s))?;
Expand Down
1 change: 0 additions & 1 deletion src/rust/src/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use extendr_api::prelude::*;
use polars::lazy::dsl;
use polars::prelude as pl;
use polars_core::functions as pl_functions;
use std::result::Result;

#[extendr]
fn concat_lf(
Expand Down
3 changes: 2 additions & 1 deletion src/rust/src/conversion_r_to_s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ fn recursive_robjname2series_tree(x: &Robj, name: &str) -> pl::PolarsResult<Seri
Rtype::Raw => {
let rpolars_raw_list = list!(x)
.set_class(["rpolars_raw_list", "list"])
.cloned()
.map_err(|err| pl::polars_err!(ComputeError: err.to_string()))?;
recursive_robjname2series_tree(&rpolars_raw_list, name)
recursive_robjname2series_tree(&rpolars_raw_list.into_robj(), name)
}

Rtype::List if x.inherits("rpolars_raw_list") => {
Expand Down
8 changes: 7 additions & 1 deletion src/rust/src/conversion_s_to_r.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn pl_series_to_list(
})
.collect_robj()
.set_class(&["integer64"])
.cloned()
.expect("internal error could not set class label 'integer64'")
}),
_ => Err(pl::PolarsError::InvalidOperation(
Expand Down Expand Up @@ -108,6 +109,7 @@ pub fn pl_series_to_list(
extendr_api::List::from_values(x)
.into_robj()
.set_class(["rpolars_raw_list", "list"])
.cloned()
.expect("this class label is always valid")
}),
Enum(_, _) => s
Expand Down Expand Up @@ -181,6 +183,7 @@ pub fn pl_series_to_list(
.into_iter()
.collect_robj()
.set_class(&["Date"])
.cloned()
.expect("internal error: class label Date failed")),
Null => Ok((extendr_api::NULL).into_robj()),
Time => s
Expand All @@ -195,8 +198,9 @@ pub fn pl_series_to_list(
.map(|mut robj| {
robj.set_class(&["PTime"])
.expect("internal error: class label PTime failed")
.clone()
})
.map(|mut robj| robj.set_attrib("tu", "ns"))
.map(|mut robj| robj.set_attrib("tu", "ns").cloned())
.expect("internal error: attr tu failed")
.map_err(|err| {
pl_error::ComputeError(
Expand Down Expand Up @@ -248,9 +252,11 @@ pub fn pl_series_to_list(
.map(|mut robj| {
robj.set_class(&["POSIXct", "POSIXt"])
.expect("internal error: class POSIXct label failed")
.clone()
})
.map(|mut robj| {
robj.set_attrib("tzone", opt_tz.as_ref().map(|s| s.as_str()).unwrap_or(""))
.cloned()
})
.expect("internal error: attr tzone failed")
.map_err(|err| {
Expand Down
5 changes: 1 addition & 4 deletions src/rust/src/lazy/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ use polars::prelude as pl;
use polars::prelude::{JoinCoalesce, SerializeOptions};
use polars_lazy::prelude::CsvWriterOptions;

#[allow(unused_imports)]
use std::result::Result;

#[derive(Clone)]
pub struct RPolarsLazyFrame(pub pl::LazyFrame);

Expand Down Expand Up @@ -68,7 +65,7 @@ impl RPolarsLazyFrame {
}

//low level version of describe_plan, mainly for arg testing
pub fn debug_plan(&self) -> Result<String, String> {
pub fn debug_plan(&self) -> std::result::Result<String, String> {
use polars_core::export::serde::Serialize;
use serde_json::value::Serializer;
Serialize::serialize(&self.0.logical_plan.clone(), Serializer)
Expand Down
9 changes: 4 additions & 5 deletions src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use polars::prelude as pl;
use polars::prelude::{ExprEvalExtension, NestedType, SortOptions};
use std::any::Any;
use std::ops::{Add, Div, Mul, Rem, Sub};
use std::result::Result;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Isn't it cleaner to keep this rather than putting std::result:: everywhere?

pub type NameGenerator = pl::Arc<dyn Fn(usize) -> String + Send + Sync>;
use crate::rdatatype::robjs_to_ewm_options;
use crate::utils::r_expr_to_rust_expr;
Expand Down Expand Up @@ -2504,7 +2503,7 @@ impl RPolarsExpr {
.into())
}

pub fn bin_starts_with(&self, sub: Robj) -> Result<Self, String> {
pub fn bin_starts_with(&self, sub: Robj) -> std::result::Result<Self, String> {
Ok(self
.0
.clone()
Expand All @@ -2513,7 +2512,7 @@ impl RPolarsExpr {
.into())
}

pub fn bin_ends_with(&self, sub: Robj) -> Result<Self, String> {
pub fn bin_ends_with(&self, sub: Robj) -> std::result::Result<Self, String> {
Ok(self
.0
.clone()
Expand Down Expand Up @@ -2570,7 +2569,7 @@ impl RPolarsExpr {
List::from_values(exprs.iter().map(|e| RPolarsExpr(e.clone())))
}

fn meta_eq(&self, other: Robj) -> Result<bool, String> {
fn meta_eq(&self, other: Robj) -> std::result::Result<bool, String> {
let other = robj_to!(Expr, other)?;
Ok(self.0 == other.0)
}
Expand All @@ -2585,7 +2584,7 @@ impl RPolarsExpr {
.collect()
}

fn meta_output_name(&self) -> Result<String, String> {
fn meta_output_name(&self) -> std::result::Result<String, String> {
let name = self
.0
.clone()
Expand Down
6 changes: 2 additions & 4 deletions src/rust/src/rbackground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ use crate::rpolarserr::rerr;
use crate::rpolarserr::{
extendr_to_rpolars_err, polars_to_rpolars_err, rdbg, RPolarsErr, RResult, Rctx, WithRctx,
};
use extendr_api::{
call, eval_string, extendr, extendr_module, list, pairlist, symbol::class_symbol, Attributes,
Conversions, Length, List, Operators, Pairlist, Rinternals, Robj, NULL, R,
};
use extendr_api::prelude::*;
use flume::{bounded, Sender};
use ipc_channel::ipc;
use once_cell::sync::Lazy;
use polars::prelude as pl;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::thread;

#[derive(Debug)]
pub struct RPolarsRThreadHandle<T> {
handle: Option<thread::JoinHandle<T>>,
Expand Down
28 changes: 17 additions & 11 deletions src/rust/src/rdataframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use extendr_api::{extendr, prelude::*, rprintln};
use polars::prelude::{self as pl, IntoLazy, SerWriter};
use std::result::Result;
pub mod read_csv;
pub mod read_ipc;
pub mod read_ndjson;
Expand Down Expand Up @@ -52,7 +51,7 @@ impl OwnedDataFrameIterator {
}

impl Iterator for OwnedDataFrameIterator {
type Item = Result<Box<dyn arrow::array::Array>, PolarsError>;
type Item = std::result::Result<Box<dyn arrow::array::Array>, PolarsError>;

fn next(&mut self) -> Option<Self::Item> {
if self.idx >= self.n_chunks {
Expand Down Expand Up @@ -143,14 +142,18 @@ impl RPolarsDataFrame {
}

//internal use
pub fn set_column_from_robj(&mut self, robj: Robj, name: &str) -> Result<(), String> {
pub fn set_column_from_robj(
&mut self,
robj: Robj,
name: &str,
) -> std::result::Result<(), String> {
robjname2series(robj, name)
.and_then(|s| self.0.with_column(s).map(|_| ()))
.map_err(|err| format!("in set_column_from_robj: {:?}", err))
}

//internal use
pub fn set_column_from_series(&mut self, x: &RPolarsSeries) -> Result<(), String> {
pub fn set_column_from_series(&mut self, x: &RPolarsSeries) -> std::result::Result<(), String> {
let s: pl::Series = x.into(); //implicit clone, cannot move R objects
self.0
.with_column(s)
Expand Down Expand Up @@ -183,7 +186,7 @@ impl RPolarsDataFrame {
.collect()
}

pub fn set_column_names_mut(&mut self, names: Vec<String>) -> Result<(), String> {
pub fn set_column_names_mut(&mut self, names: Vec<String>) -> std::result::Result<(), String> {
self.0
.set_column_names(&names[..])
.map(|_| ())
Expand Down Expand Up @@ -229,7 +232,7 @@ impl RPolarsDataFrame {
// }

pub fn to_list(&self, int64_conversion: &str) -> List {
let robj_vec_res: Result<Vec<Robj>, _> = collect_hinted_result(
let robj_vec_res: std::result::Result<Vec<Robj>, _> = collect_hinted_result(
self.0.width(),
self.0
.iter()
Expand All @@ -249,7 +252,7 @@ impl RPolarsDataFrame {

//this methods should only be used for benchmarking
pub fn to_list_unwind(&self, int64_conversion: &str) -> Robj {
let robj_vec_res: Result<Vec<Robj>, _> = collect_hinted_result(
let robj_vec_res: std::result::Result<Vec<Robj>, _> = collect_hinted_result(
self.0.width(),
self.0
.iter()
Expand All @@ -271,7 +274,7 @@ impl RPolarsDataFrame {
// does not expose this arg in to_list as it is quite niche and might be deprecated later
pub fn to_list_tag_structs(&self, int64_conversion: &str) -> List {
//convert DataFrame to Result of to R vectors, error if DataType is not supported
let robj_vec_res: Result<Vec<Robj>, _> = collect_hinted_result(
let robj_vec_res: std::result::Result<Vec<Robj>, _> = collect_hinted_result(
self.0.width(),
self.0
.iter()
Expand Down Expand Up @@ -368,7 +371,7 @@ impl RPolarsDataFrame {
}
}

pub fn from_arrow_record_batches(rbr: Robj) -> Result<RPolarsDataFrame, String> {
pub fn from_arrow_record_batches(rbr: Robj) -> std::result::Result<RPolarsDataFrame, String> {
Ok(RPolarsDataFrame(unsafe {
crate::arrow_interop::to_rust::to_rust_df(rbr)
}?))
Expand Down Expand Up @@ -607,9 +610,12 @@ impl RPolarsDataFrame {
}

impl RPolarsDataFrame {
pub fn to_list_result(&self, int64_conversion: &str) -> Result<Robj, pl::PolarsError> {
pub fn to_list_result(
&self,
int64_conversion: &str,
) -> std::result::Result<Robj, pl::PolarsError> {
//convert DataFrame to Result of to R vectors, error if DataType is not supported
let robj_vec_res: Result<Vec<Robj>, _> = self
let robj_vec_res: std::result::Result<Vec<Robj>, _> = self
.0
.iter()
.map(|s| pl_series_to_list(s, true, int64_conversion))
Expand Down
1 change: 0 additions & 1 deletion src/rust/src/rdataframe/read_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use polars::io::RowIndex;
use crate::utils::wrappers::{null_to_opt, Wrap};
use extendr_api::{extendr, prelude::*};
use polars::prelude as pl;
use std::result::Result;

//see param, null_values
#[derive(Clone, Debug)]
Expand Down
1 change: 0 additions & 1 deletion src/rust/src/rdataframe/read_ndjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use polars::io::RowIndex;
use extendr_api::{extendr, prelude::*};
use polars::prelude as pl;
use polars::prelude::LazyFileListReader;
use std::result::Result;

#[allow(clippy::too_many_arguments)]
#[extendr]
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/rlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ use crate::rpolarserr::*;

#[extendr]
pub fn dtype_str_repr(dtype: Robj) -> RResult<String> {
let dtype = robj_to!(RPolarsDataType, dtype)?.0;
let dtype = robj_to!(PLPolarsDataType, dtype)?;
Ok(dtype.to_string())
}

Expand Down
5 changes: 1 addition & 4 deletions src/rust/src/rpolarserr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::collections::VecDeque;

use extendr_api::{
call, eval_string, eval_string_with_params, extendr, extendr_module, symbol::class_symbol,
Attributes, Nullable, Operators, Pairlist, Rinternals, Robj, Types, R,
};
use extendr_api::prelude::*;

#[derive(Clone, Debug, thiserror::Error, serde::Deserialize, serde::Serialize)]
pub enum Rctx {
Expand Down
Loading
Loading