Skip to content

Commit

Permalink
refactor!: implement dtype sub classes and remove is_* methods don'…
Browse files Browse the repository at this point in the history
…t exist in Python Polars
  • Loading branch information
eitsupi committed Oct 20, 2024
1 parent d9b5dad commit f8a4528
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 474 deletions.
161 changes: 7 additions & 154 deletions R/000-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ class(`PlRDataFrame`) <- "PlRDataFrame__bundle"
}
}

`PlRDataType__get_dtype_names` <- function(self) {
function() {
.Call(savvy_PlRDataType__get_dtype_names__impl, `self`)
}
}

`PlRDataType__get_datatype_fields` <- function(self) {
function() {
.Call(savvy_PlRDataType__get_datatype_fields__impl, `self`)
Expand All @@ -398,167 +404,14 @@ class(`PlRDataFrame`) <- "PlRDataFrame__bundle"
}
}

`PlRDataType_is_array` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_array__impl, `self`)
}
}

`PlRDataType_is_binary` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_binary__impl, `self`)
}
}

`PlRDataType_is_bool` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_bool__impl, `self`)
}
}

`PlRDataType_is_categorical` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_categorical__impl, `self`)
}
}

`PlRDataType_is_date` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_date__impl, `self`)
}
}

`PlRDataType_is_enum` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_enum__impl, `self`)
}
}

`PlRDataType_is_float` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_float__impl, `self`)
}
}

`PlRDataType_is_integer` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_integer__impl, `self`)
}
}

`PlRDataType_is_known` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_known__impl, `self`)
}
}

`PlRDataType_is_list` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_list__impl, `self`)
}
}

`PlRDataType_is_logical` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_logical__impl, `self`)
}
}

`PlRDataType_is_nested` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_nested__impl, `self`)
}
}

`PlRDataType_is_nested_null` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_nested_null__impl, `self`)
}
}

`PlRDataType_is_null` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_null__impl, `self`)
}
}

`PlRDataType_is_numeric` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_numeric__impl, `self`)
}
}

`PlRDataType_is_ord` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_ord__impl, `self`)
}
}

`PlRDataType_is_primitive` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_primitive__impl, `self`)
}
}

`PlRDataType_is_signed_integer` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_signed_integer__impl, `self`)
}
}

`PlRDataType_is_string` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_string__impl, `self`)
}
}

`PlRDataType_is_struct` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_struct__impl, `self`)
}
}

`PlRDataType_is_temporal` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_temporal__impl, `self`)
}
}

`PlRDataType_is_unsigned_integer` <- function(self) {
function() {
.Call(savvy_PlRDataType_is_unsigned_integer__impl, `self`)
}
}

`.savvy_wrap_PlRDataType` <- function(ptr) {
e <- new.env(parent = emptyenv())
e$.ptr <- ptr
e$`print` <- `PlRDataType_print`(ptr)
e$`_get_dtype_names` <- `PlRDataType__get_dtype_names`(ptr)
e$`_get_datatype_fields` <- `PlRDataType__get_datatype_fields`(ptr)
e$`eq` <- `PlRDataType_eq`(ptr)
e$`ne` <- `PlRDataType_ne`(ptr)
e$`is_array` <- `PlRDataType_is_array`(ptr)
e$`is_binary` <- `PlRDataType_is_binary`(ptr)
e$`is_bool` <- `PlRDataType_is_bool`(ptr)
e$`is_categorical` <- `PlRDataType_is_categorical`(ptr)
e$`is_date` <- `PlRDataType_is_date`(ptr)
e$`is_enum` <- `PlRDataType_is_enum`(ptr)
e$`is_float` <- `PlRDataType_is_float`(ptr)
e$`is_integer` <- `PlRDataType_is_integer`(ptr)
e$`is_known` <- `PlRDataType_is_known`(ptr)
e$`is_list` <- `PlRDataType_is_list`(ptr)
e$`is_logical` <- `PlRDataType_is_logical`(ptr)
e$`is_nested` <- `PlRDataType_is_nested`(ptr)
e$`is_nested_null` <- `PlRDataType_is_nested_null`(ptr)
e$`is_null` <- `PlRDataType_is_null`(ptr)
e$`is_numeric` <- `PlRDataType_is_numeric`(ptr)
e$`is_ord` <- `PlRDataType_is_ord`(ptr)
e$`is_primitive` <- `PlRDataType_is_primitive`(ptr)
e$`is_signed_integer` <- `PlRDataType_is_signed_integer`(ptr)
e$`is_string` <- `PlRDataType_is_string`(ptr)
e$`is_struct` <- `PlRDataType_is_struct`(ptr)
e$`is_temporal` <- `PlRDataType_is_temporal`(ptr)
e$`is_unsigned_integer` <- `PlRDataType_is_unsigned_integer`(ptr)

class(e) <- "PlRDataType"
e
Expand Down
2 changes: 1 addition & 1 deletion R/as_polars_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ as_polars_df.polars_series <- function(
x, ...,
column_name = NULL,
from_struct = TRUE) {
if (isTRUE(from_struct) && x$dtype$is_struct()) {
if (isTRUE(from_struct) && inherits(x$dtype, "polars_dtype_struct")) {
x$struct$unnest()
} else {
x$to_frame(name = column_name)
Expand Down
85 changes: 17 additions & 68 deletions R/datatypes-classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ polars_datatype__methods <- new.env(parent = emptyenv())
wrap.PlRDataType <- function(x, ...) {
self <- new.env(parent = emptyenv())
self$`_dt` <- x
dtype_names <- x$`_get_dtype_names`()

lapply(names(polars_datatype__methods), function(name) {
fn <- polars_datatype__methods[[name]]
Expand All @@ -15,11 +16,11 @@ wrap.PlRDataType <- function(x, ...) {
})

## Enum only method
if (self$`_dt`$is_enum()) {
if ("polars_dtype_enum" %in% dtype_names) {
fn <- function(other) {
wrap({
check_polars_dtype(other)
if (!other$`_dt`$is_enum()) {
if (!inherits(other, "polars_dtype_enum")) {
abort("`other` must be a Enum data type")
}

Expand Down Expand Up @@ -51,7 +52,7 @@ wrap.PlRDataType <- function(x, ...) {
}, self)
}

class(self) <- c("polars_data_type", "polars_object")
class(self) <- c("polars_data_type", dtype_names, "polars_object")
self
}

Expand Down Expand Up @@ -157,86 +158,34 @@ datatype__ne <- function(other) {
})
}

datatype__is_array <- function() {
self$`_dt`$is_array()
}

datatype__is_binary <- function() {
self$`_dt`$is_binary()
}

datatype__is_categorical <- function() {
self$`_dt`$is_categorical()
}

datatype__is_date <- function() {
self$`_dt`$is_date()
}

datatype__is_enum <- function() {
self$`_dt`$is_enum()
}

datatype__is_float <- function() {
self$`_dt`$is_float()
}

datatype__is_integer <- function() {
self$`_dt`$is_integer()
}

datatype__is_known <- function() {
self$`_dt`$is_known()
}

datatype__is_logical <- function() {
self$`_dt`$is_bool()
}

datatype__is_list <- function() {
self$`_dt`$is_list()
}

datatype__is_nested <- function() {
self$`_dt`$is_nested()
}

datatype__is_nested_null <- function() {
self$`_dt`$is_nested_null()
}

datatype__is_null <- function() {
self$`_dt`$is_null()
}

datatype__is_numeric <- function() {
self$`_dt`$is_numeric()
inherits(self, "polars_dtype_numeric")
}

datatype__is_ord <- function() {
self$`_dt`$is_ord()
datatype__is_decimal <- function() {
inherits(self, "polars_dtype_decimal")
}

datatype__is_primitive <- function() {
self$`_dt`$is_primitive()
datatype__is_integer <- function() {
inherits(self, "polars_dtype_integer")
}

datatype__is_signed_integer <- function() {
self$`_dt`$is_signed_integer()
inherits(self, "polars_dtype_signed_integer")
}

datatype__is_string <- function() {
self$`_dt`$is_string()
datatype__is_unsigned_integer <- function() {
inherits(self, "polars_dtype_unsigned_integer")
}

datatype__is_struct <- function() {
self$`_dt`$is_struct()
datatype__is_float <- function() {
inherits(self, "polars_dtype_float")
}

datatype__is_temporal <- function() {
self$`_dt`$is_temporal()
inherits(self, "polars_dtype_temporal")
}

datatype__is_unsigned_integer <- function() {
self$`_dt`$is_unsigned_integer()
datatype__is_nested <- function() {
inherits(self, "polars_dtype_nested")
}
15 changes: 7 additions & 8 deletions R/expr-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ expr_str_strptime <- function(
check_dots_empty0(...)
check_polars_dtype(dtype)

if (pl$same_outer_dt(dtype, pl$Datetime())) {
datetime_type <- dtype$`_rexpr`$get_insides()
out <- self$`_rexpr`$str_to_datetime(
format, datetime_type$tu, datetime_type$tz, strict, exact, cache, ambiguous
)
out$`_rexpr`$dt_cast_time_unit(expr, datetime_type$tu)
} else if (dtype$is_date()) {
dtype_class <- class(dtype)
if ("polars_dtype_datetime" %in% dtype_class) {
self$`_rexpr`$str_to_datetime(
format, dtype$time_unit, dtype$time_zone, strict, exact, cache, ambiguous
)$dt_cast_time_unit(expr, dtype$time_unit)
} else if ("polars_dtype_date" %in% dtype_class) {
self$`_rexpr`$str_to_date(format, strict, exact, cache)
} else if (dtype$eq(pl$Time)) {
} else if ("polars_dtype_time" %in% dtype_class) {
self$`_rexpr`$str_to_time(format, strict, cache)
} else {
abort("`dtype` must be of type Date, Datetime, or Time.")
Expand Down
Loading

0 comments on commit f8a4528

Please sign in to comment.