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

feat: add polars_info() #39

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ S3method(print,PlRWhen__bundle)
S3method(print,polars_data_frame)
S3method(print,polars_dtype)
S3method(print,polars_expr)
S3method(print,polars_info)
S3method(print,polars_lazy_frame)
S3method(print,polars_object)
S3method(print,polars_series)
Expand Down Expand Up @@ -146,5 +147,6 @@ export(is_polars_lf)
export(is_polars_selector)
export(is_polars_series)
export(pl)
export(polars_info)
import(rlang)
useDynLib(neopolars, .registration = TRUE)
10 changes: 10 additions & 0 deletions R/000-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ NULL
}


`rust_polars_version` <- function() {
.Call(savvy_rust_polars_version__impl)
}


`thread_pool_size` <- function() {
.Call(savvy_thread_pool_size__impl)
}


`all_horizontal` <- function(`exprs`) {
.savvy_wrap_PlRExpr(.Call(savvy_all_horizontal__impl, `exprs`))
}
Expand Down
38 changes: 38 additions & 0 deletions R/polars_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' @export
polars_info <- function() {
# Similar to arrow::arrow_info()
out <- list(
versions = list(
r_package = as.character(utils::packageVersion("polars")),
rust_crate = rust_polars_version()
),
thread_pool_size = thread_pool_size()
# TODO-REWRITE: enable those
# features = cargo_rpolars_feature_info(),
# code_completion = .polars_autocompletion$mode %||% "deactivated"
)
structure(out, class = "polars_info")
}

#' @noRd
#' @export
print.polars_info <- function(x, ...) {
# Copied from the arrow package
# https://github.com/apache/arrow/blob/6f3bd2524c2abe3a4a278fc1c62fc5c49b56cab3/r/R/arrow-info.R#L149-L157
print_key_values <- function(title, vals, ...) {
df <- data.frame(vals, ...)
names(df) <- ""

cat(title, ":", sep = "")
print(df)
cat("\n")
}

cat("Polars R package version : ", format(x$versions$r_package), "\n", sep = "")
cat("Rust Polars crate version: ", format(x$versions$rust_crate), "\n", sep = "")
cat("\n")
cat("Thread pool size:", x$thread_pool_size, "\n")
# cat("\n")
# print_key_values("Features", unlist(x$features))
# cat("Code completion:", x$code_completion, "\n")
}
12 changes: 12 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ SEXP handle_result(SEXP res_) {
return (SEXP)res;
}

SEXP savvy_rust_polars_version__impl(void) {
SEXP res = savvy_rust_polars_version__ffi();
return handle_result(res);
}

SEXP savvy_thread_pool_size__impl(void) {
SEXP res = savvy_thread_pool_size__ffi();
return handle_result(res);
}

SEXP savvy_all_horizontal__impl(SEXP c_arg__exprs) {
SEXP res = savvy_all_horizontal__ffi(c_arg__exprs);
return handle_result(res);
Expand Down Expand Up @@ -2516,6 +2526,8 @@ SEXP savvy_PlRWhen_then__impl(SEXP self__, SEXP c_arg__statement) {


static const R_CallMethodDef CallEntries[] = {
{"savvy_rust_polars_version__impl", (DL_FUNC) &savvy_rust_polars_version__impl, 0},
{"savvy_thread_pool_size__impl", (DL_FUNC) &savvy_thread_pool_size__impl, 0},
{"savvy_all_horizontal__impl", (DL_FUNC) &savvy_all_horizontal__impl, 1},
{"savvy_any_horizontal__impl", (DL_FUNC) &savvy_any_horizontal__impl, 1},
{"savvy_max_horizontal__impl", (DL_FUNC) &savvy_max_horizontal__impl, 1},
Expand Down
2 changes: 2 additions & 0 deletions src/rust/api.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SEXP savvy_rust_polars_version__ffi(void);
SEXP savvy_thread_pool_size__ffi(void);
SEXP savvy_all_horizontal__ffi(SEXP c_arg__exprs);
SEXP savvy_any_horizontal__ffi(SEXP c_arg__exprs);
SEXP savvy_max_horizontal__ffi(SEXP c_arg__exprs);
Expand Down
11 changes: 11 additions & 0 deletions src/rust/src/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use savvy::{savvy, Result, Sexp};

#[savvy]
fn rust_polars_version() -> Result<Sexp> {
polars::VERSION.try_into()
}

#[savvy]
fn thread_pool_size() -> Result<Sexp> {
(polars_core::POOL.current_num_threads() as i32).try_into()
}
1 change: 1 addition & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod datatypes;
mod error;
mod expr;
mod functions;
mod info;
mod lazyframe;
mod lazygroupby;
mod map;
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/polars_info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# print polars_info()

Code
info
Output
Polars R package version : 999.999.999
Rust Polars crate version: 999.999.999

Thread pool size: 1

31 changes: 31 additions & 0 deletions tests/testthat/test-polars_info.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
patrick::with_parameters_test_that("polars_info() features are logical",
{
expect_type(feature, "logical")
expect_length(feature, 1)
},
feature = polars_info()$features
)

test_that("print polars_info()", {
info <- polars_info()

expect_type(info$versions$r_package, "character")
expect_type(info$versions$rust_crate, "character")

# Ensure static version for snapshot test
info$versions$r_package <- "999.999.999"
info$versions$rust_crate <- "999.999.999"

# Ensure the thread_pool_size is 1 for snapshot test
info$thread_pool_size <- 1

# Ensure all features are FALSE for snapshot test
for (feature in names(info$features)) {
info$features[[feature]] <- FALSE
}

# Ensure code_completion is deactivated for snapshot test
info$code_completion <- "deactivated"

expect_snapshot(info)
})