Skip to content

Commit

Permalink
ARROW-15712 [R] Add a type method for Expression objects
Browse files Browse the repository at this point in the history
This would allow for more consistent syntax when extracting the type of an expression.
A block like this:
```r
if (inherits(x, "Expression")) {
class <- x$type()$ToString()
} else {
class <- type(x)$ToString()
}
```

would be simplified to

```r
class <- type(x)$ToString()
```

Closes apache#12447 from dragosmg/type_method_for_expression

Authored-by: Dragoș Moldovan-Grünfeld <[email protected]>
Signed-off-by: Nic Crane <[email protected]>
  • Loading branch information
dragosmg authored and thisisnic committed Feb 18, 2022
1 parent 4a439c2 commit cd20efb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ S3method(tail,RecordBatchReader)
S3method(tail,Scanner)
S3method(tail,arrow_dplyr_query)
S3method(type,ArrowDatum)
S3method(type,Expression)
S3method(type,default)
S3method(unique,ArrowDatum)
S3method(vec_ptype_abbr,arrow_fixed_size_binary)
Expand Down
3 changes: 3 additions & 0 deletions r/R/type.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type.default <- function(x) Array__infer_type(x)
#' @export
type.ArrowDatum <- function(x) x$type

#' @export
type.Expression <- function(x) x$type

#----- metadata

#' @title class arrow::FixedWidthType
Expand Down
10 changes: 10 additions & 0 deletions r/tests/testthat/test-type.R
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,13 @@ test_that("Type strings are correctly canonicalized", {
"Unrecognized"
)
})

test_that("type() gets the right type for Expression", {
x <- Expression$scalar(32L)
y <- Expression$scalar(10)
add_xy <- Expression$create("add", x, y)

expect_equal(x$type, type(x))
expect_equal(y$type, type(y))
expect_equal(add_xy$type, type(add_xy))
})

0 comments on commit cd20efb

Please sign in to comment.