Skip to content

Commit

Permalink
refactor(rust): Add feature gates for polars-python crate (#18232)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego authored Aug 16, 2024
1 parent d8b617e commit 7654387
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 59 deletions.
4 changes: 4 additions & 0 deletions crates/polars-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,14 @@ optimizations = [
]

polars_cloud = ["polars/polars_cloud"]

# also includes simd
nightly = ["polars/nightly"]

pymethods = []

all = [
"pymethods",
"optimizations",
"io",
"operations",
Expand Down
5 changes: 4 additions & 1 deletion crates/polars-python/src/dataframe/construction.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use polars::frame::row::{rows_to_schema_supertypes, rows_to_supertypes, Row};
use polars::prelude::*;
use pyo3::prelude::*;
use pyo3::types::PyDict;

use super::*;
use super::PyDataFrame;
use crate::conversion::any_value::py_object_to_any_value;
use crate::conversion::{vec_extract_wrapped, Wrap};
use crate::error::PyPolarsErr;
use crate::interop;

#[pymethods]
Expand Down
4 changes: 3 additions & 1 deletion crates/polars-python/src/dataframe/export.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use polars::export::arrow::record_batch::RecordBatch;
use polars::prelude::*;
use polars_core::export::arrow::datatypes::IntegerType;
use polars_core::utils::arrow::compute::cast::CastOptionsImpl;
use pyo3::prelude::*;
use pyo3::types::{PyCapsule, PyList, PyTuple};

use super::*;
use super::PyDataFrame;
use crate::conversion::{ObjectValue, Wrap};
use crate::error::PyPolarsErr;
use crate::interop;
use crate::interop::arrow::to_py::dataframe_to_stream;
use crate::prelude::PyCompatLevel;
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use pyo3::prelude::*;
use pyo3::pybacked::PyBackedStr;
use pyo3::types::PyList;

use super::*;
use super::PyDataFrame;
use crate::conversion::Wrap;
use crate::error::PyPolarsErr;
use crate::map::dataframe::{
apply_lambda_unknown, apply_lambda_with_bool_out_type, apply_lambda_with_primitive_out_type,
apply_lambda_with_string_out_type,
Expand Down
5 changes: 4 additions & 1 deletion crates/polars-python/src/dataframe/io.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use std::io::BufWriter;
use std::num::NonZeroUsize;
use std::sync::Arc;

#[cfg(feature = "avro")]
use polars::io::avro::AvroCompression;
use polars::io::mmap::ensure_not_mapped;
use polars::io::RowIndex;
use polars::prelude::*;
#[cfg(feature = "parquet")]
use polars_parquet::arrow::write::StatisticsOptions;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedStr;

use super::*;
use super::PyDataFrame;
#[cfg(feature = "parquet")]
use crate::conversion::parse_parquet_compression;
use crate::conversion::Wrap;
use crate::error::PyPolarsErr;
use crate::file::{
get_either_file, get_file_like, get_mmap_bytes_reader, get_mmap_bytes_reader_and_path,
read_if_bytesio, EitherRustPythonFile,
Expand Down
12 changes: 7 additions & 5 deletions crates/polars-python/src/dataframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#[cfg(feature = "pymethods")]
mod construction;
#[cfg(feature = "pymethods")]
mod export;
#[cfg(feature = "pymethods")]
mod general;
#[cfg(feature = "pymethods")]
mod io;
#[cfg(feature = "pymethods")]
mod serde;

use polars::prelude::*;
use pyo3::prelude::*;
use pyo3::types::PyDict;

use crate::error::PyPolarsErr;
use polars::prelude::DataFrame;
use pyo3::pyclass;

#[pyclass]
#[repr(transparent)]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/dataframe/serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::{BufReader, BufWriter, Cursor};
use std::ops::Deref;

use polars::prelude::*;
use polars_io::mmap::ReaderBytes;
use pyo3::prelude::*;
use pyo3::types::PyBytes;
Expand All @@ -9,7 +10,6 @@ use super::PyDataFrame;
use crate::error::PyPolarsErr;
use crate::exceptions::ComputeError;
use crate::file::{get_file_like, get_mmap_bytes_reader};
use crate::prelude::*;

#[pymethods]
impl PyDataFrame {
Expand Down
15 changes: 13 additions & 2 deletions crates/polars-python/src/expr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
#[cfg(feature = "pymethods")]
mod array;
#[cfg(feature = "pymethods")]
mod binary;
#[cfg(feature = "pymethods")]
mod categorical;
#[cfg(feature = "pymethods")]
mod datetime;
#[cfg(feature = "pymethods")]
mod general;
#[cfg(feature = "pymethods")]
mod list;
#[cfg(feature = "meta")]
#[cfg(all(feature = "meta", feature = "pymethods"))]
mod meta;
#[cfg(feature = "pymethods")]
mod name;
#[cfg(feature = "pymethods")]
mod rolling;
#[cfg(feature = "pymethods")]
mod serde;
#[cfg(feature = "pymethods")]
mod string;
#[cfg(feature = "pymethods")]
mod r#struct;

use std::mem::ManuallyDrop;

use polars::lazy::dsl::Expr;
use pyo3::prelude::*;
use pyo3::pyclass;

#[pyclass]
#[repr(transparent)]
Expand Down
5 changes: 5 additions & 0 deletions crates/polars-python/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,8 @@ pub fn register_plugin_function(
}
.into())
}

#[pyfunction]
pub fn __register_startup_deps() {
crate::on_startup::register_startup_deps()
}
7 changes: 6 additions & 1 deletion crates/polars-python/src/lazyframe/exitable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use super::*;
use polars::prelude::*;
use pyo3::prelude::*;

use super::PyLazyFrame;
use crate::error::PyPolarsErr;
use crate::PyDataFrame;

#[pymethods]
impl PyLazyFrame {
Expand Down
11 changes: 4 additions & 7 deletions crates/polars-python/src/lazyframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
mod exitable;
#[cfg(feature = "pymethods")]
mod general;
#[cfg(feature = "pymethods")]
mod serde;
pub mod visit;
pub mod visitor;

pub use exitable::PyInProcessQuery;
use polars_core::prelude::*;
use pyo3::prelude::*;
use pyo3::types::PyList;

use crate::error::PyPolarsErr;
use crate::prelude::*;
use crate::{PyDataFrame, PyExpr};
use polars::prelude::LazyFrame;
use pyo3::pyclass;

#[pyclass]
#[repr(transparent)]
Expand Down
11 changes: 7 additions & 4 deletions crates/polars-python/src/lazyframe/visit.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::sync::Mutex;
use std::sync::{Arc, Mutex};

use polars::prelude::PolarsError;
use polars_plan::plans::{to_aexpr, Context, IR};
use polars_plan::prelude::expr_ir::ExprIR;
use polars_plan::prelude::{AExpr, PythonOptions, PythonScanSource};
use polars_utils::arena::{Arena, Node};
use pyo3::prelude::*;
use visitor::{expr_nodes, nodes};
use pyo3::types::PyList;

use super::*;
use crate::raise_err;
use super::visitor::{expr_nodes, nodes};
use super::PyLazyFrame;
use crate::error::PyPolarsErr;
use crate::{raise_err, PyExpr, Wrap};

#[derive(Clone)]
#[pyclass]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/lazyframe/visitor/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use polars_plan::prelude::{
use pyo3::exceptions::{PyNotImplementedError, PyValueError};
use pyo3::prelude::*;

use super::super::visit::PyExprIR;
use super::expr_nodes::PyGroupbyOptions;
use crate::lazyframe::visit::PyExprIR;
use crate::PyDataFrame;

#[pyclass]
Expand Down
1 change: 1 addition & 0 deletions crates/polars-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod error;
pub mod exceptions;
pub mod expr;
pub mod file;
#[cfg(feature = "pymethods")]
pub mod functions;
pub mod gil_once_cell;
pub mod interop;
Expand Down
3 changes: 1 addition & 2 deletions crates/polars-python/src/on_startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ fn warning_function(msg: &str, warning: PolarsWarning) {
});
}

#[pyfunction]
pub fn __register_startup_deps() {
pub fn register_startup_deps() {
set_polars_allow_extension(true);
if !registry::is_object_builder_registered() {
// Stack frames can get really large in debug mode.
Expand Down
5 changes: 3 additions & 2 deletions crates/polars-python/src/series/aggregation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use polars::prelude::*;
use pyo3::prelude::*;
use DataType::*;

use super::PySeries;
use crate::conversion::Wrap;
use crate::error::PyPolarsErr;
use crate::prelude::*;
use crate::PySeries;

#[pymethods]
impl PySeries {
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-python/src/series/arithmetic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use polars::prelude::*;
use pyo3::prelude::*;

use super::PySeries;
use crate::error::PyPolarsErr;
use crate::prelude::*;
use crate::PySeries;

#[pymethods]
impl PySeries {
Expand Down
8 changes: 7 additions & 1 deletion crates/polars-python/src/series/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ use polars::export::arrow::bitmap::Bitmap;
use polars::export::arrow::buffer::Buffer;
use polars::export::arrow::offset::OffsetsBuffer;
use polars::export::arrow::types::NativeType;
use polars::prelude::*;
use polars_core::{with_match_physical_numeric_polars_type, with_match_physical_numeric_type};
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;

use super::*;
use super::{PySeries, ToSeries};
use crate::conversion::Wrap;
use crate::error::PyPolarsErr;
use crate::raise_err;

struct BufferInfo {
pointer: usize,
Expand Down
5 changes: 4 additions & 1 deletion crates/polars-python/src/series/c_interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use polars::export::arrow;
use polars::prelude::*;
use pyo3::ffi::Py_uintptr_t;
use pyo3::prelude::*;

use super::*;
use super::PySeries;
use crate::error::PyPolarsErr;

// Import arrow data directly without requiring pyarrow (used in pyo3-polars)
#[pymethods]
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/series/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use polars_core::prelude::*;
use pyo3::prelude::*;
use pyo3::types::{PyCapsule, PyList};

use super::PySeries;
use crate::interop;
use crate::interop::arrow::to_py::series_to_stream;
use crate::prelude::*;
use crate::{interop, PySeries};

#[pymethods]
impl PySeries {
Expand Down
13 changes: 12 additions & 1 deletion crates/polars-python/src/series/general.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use std::io::Cursor;

use polars_core::chunked_array::cast::CastOptions;
use polars_core::series::IsSorted;
use polars_core::utils::flatten::flatten_series;
use pyo3::exceptions::{PyIndexError, PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::Python;

use super::{PySeries, *};
use super::PySeries;
use crate::dataframe::PyDataFrame;
use crate::error::PyPolarsErr;
use crate::map::series::{call_lambda_and_extract, ApplyLambda};
use crate::prelude::*;
use crate::py_modules::POLARS;
use crate::{apply_method_all_arrow_series2, raise_err};

#[pymethods]
Expand Down Expand Up @@ -800,6 +810,7 @@ impl_get!(get_duration, duration, i64);
#[cfg(test)]
mod test {
use super::*;
use crate::series::ToSeries;

#[test]
fn transmute_to_series() {
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/series/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use polars::export::arrow::ffi;
use polars::export::arrow::ffi::{
ArrowArray, ArrowArrayStream, ArrowArrayStreamReader, ArrowSchema,
};
use polars::prelude::*;
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::{PyCapsule, PyTuple, PyType};

use super::*;
use super::PySeries;

/// Validate PyCapsule has provided name
fn validate_pycapsule_name(capsule: &Bound<PyCapsule>, expected_name: &str) -> PyResult<()> {
Expand Down
30 changes: 13 additions & 17 deletions crates/polars-python/src/series/mod.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
#[cfg(feature = "pymethods")]
mod aggregation;
#[cfg(feature = "pymethods")]
mod arithmetic;
#[cfg(feature = "pymethods")]
mod buffers;
#[cfg(feature = "pymethods")]
mod c_interface;
#[cfg(feature = "pymethods")]
mod comparison;
#[cfg(feature = "pymethods")]
mod construction;
#[cfg(feature = "pymethods")]
mod export;
#[cfg(feature = "pymethods")]
mod general;
#[cfg(feature = "pymethods")]
mod import;
#[cfg(feature = "pymethods")]
mod numpy_ufunc;
#[cfg(feature = "pymethods")]
mod scatter;

use std::io::Cursor;

use polars_core::chunked_array::cast::CastOptions;
use polars_core::series::IsSorted;
use polars_core::utils::flatten::flatten_series;
use polars_core::{with_match_physical_numeric_polars_type, with_match_physical_numeric_type};
use pyo3::exceptions::{PyIndexError, PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::Python;

use crate::dataframe::PyDataFrame;
use crate::error::PyPolarsErr;
use crate::map::series::{call_lambda_and_extract, ApplyLambda};
use crate::prelude::*;
use crate::py_modules::POLARS;
use crate::raise_err;
use polars::prelude::Series;
use pyo3::pyclass;

#[pyclass]
#[repr(transparent)]
Expand Down
Loading

0 comments on commit 7654387

Please sign in to comment.