dragosmg commented on a change in pull request #11898:
URL: https://github.com/apache/arrow/pull/11898#discussion_r764751944
##########
File path: r/R/array.R
##########
@@ -187,7 +187,18 @@ Array$create <- function(x, type = NULL) {
}
return(out)
}
- vec_to_Array(x, type)
+ tryCatch(
+ vec_to_Array(x, type),
+ error = function(cnd) {
+ if (!is.null(type)) {
+ # try again and then cast
+ vec_to_Array(x, NULL)$cast(type)
+ } else {
+ signalCondition(cnd)
+ }
+ }
+ )
+
Review comment:
It might not be as straightforward as initially thought. Catching the
error and then casting has some unintended consequences. For example, some
errors get muddled due to the fact they now pass through cast - while they are
somewhat easy to solve with the C++ error message.
Before:
``` r
library(arrow, warn.conflicts = FALSE)
df <- tibble::tibble(x = 1:10, y = 1:10)
type <- struct(x = float64(), y = int16(), z = int32())
Array$create(df, type = type)
#> Error: Unknown: Number of fields in struct (3) incompatible with number
of columns in the data frame (2)
```
<sup>Created on 2021-12-08 by the [reprex
package](https://reprex.tidyverse.org) (v2.0.1)</sup>
After:
```r
library(arrow, warn.conflicts = FALSE)
df <- tibble::tibble(x = 1:10, y = 1:10)
type <- struct(x = float64(), y = int16(), z = int32())
Array$create(df, type = type)
#>Error: NotImplemented: Unsupported cast from struct<x: int32, y: int32> to
struct using function cast_struct
/Users/dragos/Documents/arrow/cpp/src/arrow/compute/function.cc:215
DispatchBest(&inputs)
```
<sup>Created on 2021-12-08 by the [reprex
package](https://reprex.tidyverse.org) (v2.0.1)</sup>
Same for a mismatch in names. The more informative C++ error message:
``` r
library(arrow, warn.conflicts = FALSE)
df <- tibble::tibble(x = 1:10, y = 1:10)
type <- struct(y = int16(), x = float64())
Array$create(df, type = type)
#> Error: Unknown: Field name in position 0 (y) does not match the name of
the column of the data frame (x)
```
<sup>Created on 2021-12-08 by the [reprex
package](https://reprex.tidyverse.org) (v2.0.1)</sup>
becomes:
``` r
library(arrow, warn.conflicts = FALSE)
df <- tibble::tibble(x = 1:10, y = 1:10)
type <- struct(y = int16(), x = float64())
Array$create(df, type = type)
#> Error: NotImplemented: Unsupported cast from struct<x: int32, y: int32>
to struct using function cast_struct
/Users/dragos/Documents/arrow/cpp/src/arrow/compute/function.cc:215
DispatchBest(&inputs)
```
<sup>Created on 2021-12-08 by the [reprex
package](https://reprex.tidyverse.org) (v2.0.1)</sup>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]