Skip to content

Commit

Permalink
fix compile
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 15, 2023
1 parent e0e9511 commit ab78ada
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 40 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Pyo3 extensions for Polars
## 1. Shared library plugins for Polars
This is new functionality and not entirely stable, but should be preferred over `2.` as this
will circumvent the GIL and will be the way we want to support extending polars.

See more in `examples/derive_expression`.

## 2. Pyo3 extensions for Polars
<a href="https://crates.io/crates/pyo3-polars">
<img src="https://img.shields.io/crates/v/pyo3-polars.svg"/>
</a>
Expand Down
2 changes: 0 additions & 2 deletions example/derive_expression/expression_lib/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ fn pig_latin_str(value: &str, output: &mut String) {
#[polars_expr(output_type=Utf8)]
fn pig_latinnify(inputs: &[Series]) -> PolarsResult<Series> {
let ca = inputs[0].utf8()?;

let out: Utf8Chunked = ca.apply_to_buffer(pig_latin_str);

Ok(out.into_series())
}

Expand Down
14 changes: 7 additions & 7 deletions pyo3-polars-derive/src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use std::fmt::Debug;
use crate::keywords;
use proc_macro2::Ident;
use std::fmt::Debug;
use syn::parse::{Parse, ParseStream};
use syn::Token;
use crate::keywords;

#[derive(Clone, Debug)]
pub struct KeyWordAttribute<K, V> {
pub kw: K,
pub value: V
pub value: V,
}

impl<K: Parse, V: Parse> Parse for KeyWordAttribute<K, V> {
fn parse(input: ParseStream) -> syn::Result<Self> {
let kw= input.parse()?;
let kw = input.parse()?;
let _: Token![=] = input.parse()?;
let value = input.parse()?;
Ok(KeyWordAttribute{kw, value})
Ok(KeyWordAttribute { kw, value })
}
}

Expand All @@ -25,7 +25,7 @@ pub type OutputFuncAttribute = KeyWordAttribute<keywords::type_func, Ident>;
#[derive(Default, Debug)]
pub struct ExprsFunctionOptions {
pub output_dtype: Option<Ident>,
pub output_type_fn: Option<Ident>
pub output_type_fn: Option<Ident>,
}

impl Parse for ExprsFunctionOptions {
Expand All @@ -47,4 +47,4 @@ impl Parse for ExprsFunctionOptions {
}
Ok(options)
}
}
}
1 change: 0 additions & 1 deletion pyo3-polars-derive/src/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

syn::custom_keyword!(output_type);
syn::custom_keyword!(type_func);
27 changes: 14 additions & 13 deletions pyo3-polars-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod keywords;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input};
use syn::parse_macro_input;

fn create_expression_function(ast: syn::ItemFn) -> proc_macro2::TokenStream {
let fn_name = &ast.sig.ident;
Expand All @@ -20,7 +20,6 @@ fn create_expression_function(ast: syn::ItemFn) -> proc_macro2::TokenStream {

// call the function
let output: polars_core::prelude::Series = #fn_name(&inputs).unwrap();
// let output = polars_core::prelude::Series::full_null("", 3, &DataType::Null);
let out = polars_ffi::export_series(&output);
out
}
Expand All @@ -32,17 +31,18 @@ fn get_field_name(fn_name: &syn::Ident) -> syn::Ident {
}

fn get_inputs() -> proc_macro2::TokenStream {
quote!(
let inputs = std::slice::from_raw_parts(field, len);
let inputs = inputs.iter().map(|field| {
let field = polars_core::export::arrow::ffi::import_field_from_c(field).unwrap();
polars_core::prelude::Field::from(&field)
}).collect::<Vec<_>>();
)
quote!(
let inputs = std::slice::from_raw_parts(field, len);
let inputs = inputs.iter().map(|field| {
let field = polars_core::export::arrow::ffi::import_field_from_c(field).unwrap();
let out = polars_core::prelude::Field::from(&field);
out
}).collect::<Vec<_>>();
)
}

fn create_field_function(fn_name: &syn::Ident) -> proc_macro2::TokenStream {
let map_field_name = get_field_name(&fn_name);
let map_field_name = get_field_name(fn_name);
let inputs = get_inputs();

quote! (
Expand All @@ -55,7 +55,10 @@ fn create_field_function(fn_name: &syn::Ident) -> proc_macro2::TokenStream {
)
}

fn create_field_function_from_with_dtype(fn_name: &syn::Ident, dtype: syn::Ident) -> proc_macro2::TokenStream {
fn create_field_function_from_with_dtype(
fn_name: &syn::Ident,
dtype: syn::Ident,
) -> proc_macro2::TokenStream {
let map_field_name = get_field_name(fn_name);
let inputs = get_inputs();

Expand All @@ -72,8 +75,6 @@ fn create_field_function_from_with_dtype(fn_name: &syn::Ident, dtype: syn::Ident
)
}



#[proc_macro_attribute]
pub fn polars_expr(attr: TokenStream, input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as syn::ItemFn);
Expand Down
9 changes: 3 additions & 6 deletions pyo3-polars-derive/tests/01.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use polars_core::error::PolarsResult;
use pyo3_polars_derive::polars_expr;
use polars_core::prelude::{Series, Field};
use polars_core::prelude::{Field, Series};
use polars_plan::dsl::FieldsMapper;
use pyo3_polars_derive::polars_expr;

fn horizontal_product_output(input_fields: &[Field]) -> PolarsResult<Field> {
FieldsMapper::new(input_fields).map_to_supertype()
Expand All @@ -16,7 +16,4 @@ fn horizontal_product(series: &[Series]) -> PolarsResult<Series> {
Ok(acc)
}


fn main() {

}
fn main() {}
7 changes: 2 additions & 5 deletions pyo3-polars-derive/tests/02.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use polars_core::error::PolarsResult;
use polars_core::prelude::Series;
use pyo3_polars_derive::polars_expr;
use polars_core::prelude::{Series};

#[polars_expr(output_type=Int32)]
fn horizontal_product(series: &[Series]) -> PolarsResult<Series> {
Expand All @@ -11,7 +11,4 @@ fn horizontal_product(series: &[Series]) -> PolarsResult<Series> {
Ok(acc)
}


fn main() {

}
fn main() {}
3 changes: 2 additions & 1 deletion pyo3-polars/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{Debug, Formatter};
use polars::prelude::PolarsError;
use polars_core::error::ArrowError;
use pyo3::create_exception;
use pyo3::exceptions::{PyException, PyIOError, PyRuntimeError, PyValueError};
use pyo3::exceptions::{PyException, PyIndexError, PyIOError, PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use thiserror::Error;

Expand All @@ -29,6 +29,7 @@ impl std::convert::From<PyPolarsErr> for PyErr {
PolarsError::ShapeMismatch(err) => ShapeError::new_err(err.to_string()),
PolarsError::SchemaMismatch(err) => SchemaError::new_err(err.to_string()),
PolarsError::Io(err) => PyIOError::new_err(err.to_string()),
PolarsError::OutOfBounds(err) => PyIndexError::new_err(err.to_string()),
PolarsError::InvalidOperation(err) => PyValueError::new_err(err.to_string()),
PolarsError::ArrowError(err) => ArrowErrorException::new_err(format!("{:?}", err)),
PolarsError::Duplicate(err) => DuplicateError::new_err(err.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion pyo3-polars/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub use polars_core;
pub use polars_ffi;
pub use polars_plan;
pub use polars_core;
2 changes: 1 addition & 1 deletion pyo3-polars/src/ffi/to_py.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use polars::export::arrow::ffi;
use polars::prelude::{ArrayRef, ArrowField};
use pyo3::ffi::Py_uintptr_t;
use pyo3::prelude::*;
use polars::export::arrow::ffi;

/// Arrow array to Python.
pub(crate) fn to_py_array(array: ArrayRef, py: Python, pyarrow: &PyModule) -> PyResult<PyObject> {
Expand Down
6 changes: 4 additions & 2 deletions pyo3-polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
//! })
//! out_df = my_cool_function(df)
//! ```
pub mod error;
mod ffi;
#[cfg(feature = "derive")]
pub mod derive;
pub mod error;
#[cfg(feature = "derive")]
pub mod export;
mod ffi;

use crate::error::PyPolarsErr;
use crate::ffi::to_py::to_py_array;
Expand Down

0 comments on commit ab78ada

Please sign in to comment.