You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/06/22 12:35:37 UTC
[GitHub] [arrow] romainfrancois opened a new pull request #7514: ARROW-6235 : [R] Conversion from arrow::BinaryArray to R character vector not implemented
romainfrancois opened a new pull request #7514:
URL: https://github.com/apache/arrow/pull/7514
Going with list of raw vectors because strings can't hold nulls:
``` r
library(arrow, warn.conflicts = FALSE)
raws <- vctrs::list_of(as.raw(0:2), as.raw(0:255), .ptype = raw())
a <- Array$create(raws)
a
#> Array
#> <binary>
#> [
#> 000102,
#> 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
#> ]
a$as_vector()
#> <list_of<raw>[2]>
#> [[1]]
#> [1] 00 01 02
#>
#> [[2]]
#> [1] 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18
#> [26] 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31
#> [51] 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a
#> [76] 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63
#> [101] 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c
#> [126] 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95
#> [151] 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae
#> [176] af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7
#> [201] c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0
#> [226] e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9
#> [251] fa fb fc fd fe ff
# does not have to be a list_of, just have a ptype attribute
raws <- structure(list(as.raw(0:2), as.raw(0:255)), ptype = raw())
a <- Array$create(raws)
a
#> Array
#> <binary>
#> [
#> 000102,
#> 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
#> ]
# but it makes one on the way out
a$as_vector()
#> <list_of<raw>[2]>
#> [[1]]
#> [1] 00 01 02
#>
#> [[2]]
#> [1] 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18
#> [26] 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31
#> [51] 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a
#> [76] 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63
#> [101] 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c
#> [126] 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95
#> [151] 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae
#> [176] af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7
#> [201] c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0
#> [226] e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9
#> [251] fa fb fc fd fe ff
```
<sup>Created on 2020-06-22 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0.9001)</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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r445994055
##########
File path: r/src/array_from_vector.cpp
##########
@@ -915,6 +924,39 @@ class Time64Converter : public TimeConverter<Time64Type> {
}
};
+class BinaryVectorConverter : public VectorConverter {
+ public:
+ ~BinaryVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<BinaryBuilder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != VECSXP, Status::RError("Expecting a list"));
+ R_xlen_t n = XLENGTH(obj);
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (Rf_isNull(obj_i)) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ ARROW_RETURN_IF(TYPEOF(obj_i) != RAWSXP,
+ Status::RError("Expecting a raw vector"));
+ RETURN_NOT_OK(typed_builder_->Append(RAW(obj_i), XLENGTH(obj_i)));
Review comment:
Thanks.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] wesm commented on pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
wesm commented on pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#issuecomment-650151220
> @wesm I don't know what you mean by the `BitBlockCounter` treatment.
Ah sorry, welcome back =) We created some facilities to improve the performance of processing validity bitmaps
https://github.com/apache/arrow/blob/master/cpp/src/arrow/util/bit_block_counter.h
The idea is that you can scan forward 64 bits at a time and determine efficiently (using hardware popcount) whether a "block" of 64 values has any nulls or not (or if the block is all null, too). For those 64 values you can skip null checking all together and use the fast path. This speeds up processing _significantly_ for data that is mostly not null or mostly null
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446706140
##########
File path: r/src/array_from_vector.cpp
##########
@@ -918,6 +923,97 @@ class Time64Converter : public TimeConverter<Time64Type> {
}
};
+template <typename Builder>
+class BinaryVectorConverter : public VectorConverter {
+ public:
+ ~BinaryVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != VECSXP, Status::RError("Expecting a list"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (!Rf_isNull(obj_i)) {
+ ARROW_RETURN_IF(TYPEOF(obj_i) != RAWSXP,
+ Status::RError("Expecting a raw vector"));
+ size += XLENGTH(obj_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (Rf_isNull(obj_i)) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(RAW(obj_i), XLENGTH(obj_i)));
+ }
+ }
+ return Status::OK();
+ }
+
+ Status GetResult(std::shared_ptr<arrow::Array>* result) {
+ return typed_builder_->Finish(result);
+ }
+
+ private:
+ Builder* typed_builder_;
+};
+
+template <typename Builder>
+class StringVectorConverter : public VectorConverter {
+ public:
+ ~StringVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != STRSXP,
+ Status::RError("Expecting a character vector"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i != NA_STRING) {
+ size += XLENGTH(string_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i == NA_STRING) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(CHAR(string_i), XLENGTH(string_i)));
Review comment:
This new builder is breaking the (new) UTF-8 tests. The previous converter code is https://github.com/apache/arrow/blob/master/r/src/array_from_vector.cpp#L147-L171 and it is apparently no longer being called.
I wonder if this whole code block isn't possible right now as is. The "Reserve enough space before appending" block would also need to convert to UTF-8 in order to get the size right, and I wonder if converting/asserting everything to UTF-8 twice would outweigh the benefits of Reserving space. Or maybe we can take the size as is and overcommit bytes?
Tangentially related, would a "reserve" check like this be the way to solve https://issues.apache.org/jira/browse/ARROW-3308, where we need to switch to Large types if there's more than 2GB?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446237067
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -18,16 +18,16 @@
context("Array")
expect_array_roundtrip <- function(x, type) {
- a <- Array$create(x)
+ a <- Array$create(x, type = type)
Review comment:
I don't think we want to do this in general because it means we're not testing the automatic type detection code. Instead, how about changing the function signature to:
```r
expect_array_roundtrip <- function(x, type, as = NULL) {
```
and then here
```suggestion
a <- Array$create(x, type = as)
```
then adding changing the Large* tests to pass `as` as well.
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -36,10 +36,10 @@ expect_array_roundtrip <- function(x, type) {
x_sliced <- x[-1]
expect_type_equal(a_sliced$type, type)
expect_identical(length(a_sliced), length(x_sliced))
- if (!inherits(type, "ListType")) {
+ if (!inherits(type, "ListType") && !inherits(type, "LargeListType")) {
Review comment:
```suggestion
if (!inherits(type, c("ListType", "LargeListType"))) {
```
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -18,16 +18,16 @@
context("Array")
expect_array_roundtrip <- function(x, type) {
- a <- Array$create(x)
+ a <- Array$create(x, type = type)
expect_type_equal(a$type, type)
expect_identical(length(a), length(x))
- if (!inherits(type, "ListType")) {
+ if (!inherits(type, "ListType") && !inherits(type, "LargeListType")) {
Review comment:
Can/should LargeListType inherit from ListType?
```suggestion
if (!inherits(type, c("ListType", "LargeListType"))) {
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#issuecomment-650002855
@wesm I don't know what you mean by the `BitBlockCounter` treatment.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson closed pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson closed pull request #7514:
URL: https://github.com/apache/arrow/pull/7514
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r444283172
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
+ if (XLENGTH(x) == 0) {
+ Rcpp::stop(
+ "Requires at least one element to infer the values' type of a list vector");
+ }
- return arrow::list(InferArrowType(VECTOR_ELT(x, 0)));
+ return arrow::list(InferArrowType(VECTOR_ELT(x, 0)));
+ } else {
+ // special case list(raw()) -> BinaryArray
+ if (TYPEOF(ptype) == RAWSXP) {
+ return arrow::binary();
+ }
+
+ return arrow::list(InferArrowType(ptype));
Review comment:
Oh yeah that makes sense. Although this only looks at the attribute, not specifically that it is a `vctrs_list_of` but on the arrow -> R conversion, I think it does not hurt to make it a `vctrs_list_of`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] wesm commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
wesm commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r445127367
##########
File path: r/src/array_from_vector.cpp
##########
@@ -915,6 +924,39 @@ class Time64Converter : public TimeConverter<Time64Type> {
}
};
+class BinaryVectorConverter : public VectorConverter {
+ public:
+ ~BinaryVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<BinaryBuilder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != VECSXP, Status::RError("Expecting a list"));
+ R_xlen_t n = XLENGTH(obj);
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (Rf_isNull(obj_i)) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ ARROW_RETURN_IF(TYPEOF(obj_i) != RAWSXP,
+ Status::RError("Expecting a raw vector"));
+ RETURN_NOT_OK(typed_builder_->Append(RAW(obj_i), XLENGTH(obj_i)));
Review comment:
You could add a call to `typed_builder_->Reserve` before the loop for slightly better perf
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446041588
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
+ if (XLENGTH(x) == 0) {
+ Rcpp::stop(
+ "Requires at least one element to infer the values' type of a list vector");
+ }
- return arrow::list(InferArrowType(VECTOR_ELT(x, 0)));
+ return arrow::list(InferArrowType(VECTOR_ELT(x, 0)));
+ } else {
+ // special case list(raw()) -> BinaryArray
+ if (TYPEOF(ptype) == RAWSXP) {
+ return arrow::binary();
+ }
+
+ return arrow::list(InferArrowType(ptype));
Review comment:
Done. I had to modify the roundtrip checks to use `expect_equivalent()` because a roundtrip might add information:
```
list() -> List Array -> list_of( ptype = <from list array value type>)
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r448491760
##########
File path: r/src/array_from_vector.cpp
##########
@@ -918,6 +923,97 @@ class Time64Converter : public TimeConverter<Time64Type> {
}
};
+template <typename Builder>
+class BinaryVectorConverter : public VectorConverter {
+ public:
+ ~BinaryVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != VECSXP, Status::RError("Expecting a list"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (!Rf_isNull(obj_i)) {
+ ARROW_RETURN_IF(TYPEOF(obj_i) != RAWSXP,
+ Status::RError("Expecting a raw vector"));
+ size += XLENGTH(obj_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (Rf_isNull(obj_i)) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(RAW(obj_i), XLENGTH(obj_i)));
+ }
+ }
+ return Status::OK();
+ }
+
+ Status GetResult(std::shared_ptr<arrow::Array>* result) {
+ return typed_builder_->Finish(result);
+ }
+
+ private:
+ Builder* typed_builder_;
+};
+
+template <typename Builder>
+class StringVectorConverter : public VectorConverter {
+ public:
+ ~StringVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != STRSXP,
+ Status::RError("Expecting a character vector"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i != NA_STRING) {
+ size += XLENGTH(string_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i == NA_STRING) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(CHAR(string_i), XLENGTH(string_i)));
Review comment:
Added a note to https://issues.apache.org/jira/browse/ARROW-7798 about the redundancy. I checked code coverage and it appears that both paths are being called, so that should be addressed (my guess is that factor levels are still going through the old).
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r444281970
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
Review comment:
IIRC @lionel- ? said he prefers `== R_NilValue`
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446269132
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -18,16 +18,16 @@
context("Array")
expect_array_roundtrip <- function(x, type) {
- a <- Array$create(x)
+ a <- Array$create(x, type = type)
expect_type_equal(a$type, type)
expect_identical(length(a), length(x))
- if (!inherits(type, "ListType")) {
+ if (!inherits(type, "ListType") && !inherits(type, "LargeListType")) {
Review comment:
I don't think there is inheritance down the C++ code
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446269445
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -18,16 +18,16 @@
context("Array")
expect_array_roundtrip <- function(x, type) {
- a <- Array$create(x)
+ a <- Array$create(x, type = type)
Review comment:
Fair enough. I'll update
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] github-actions[bot] commented on pull request #7514: ARROW-6235 : [R] Conversion from arrow::BinaryArray to R character vector not implemented
Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#issuecomment-647496418
https://issues.apache.org/jira/browse/ARROW-6235
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r443733083
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
Review comment:
is this the same as `Rf_isNull()` (which I think we use more generally)?
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
+ if (XLENGTH(x) == 0) {
+ Rcpp::stop(
+ "Requires at least one element to infer the values' type of a list vector");
+ }
- return arrow::list(InferArrowType(VECTOR_ELT(x, 0)));
+ return arrow::list(InferArrowType(VECTOR_ELT(x, 0)));
+ } else {
+ // special case list(raw()) -> BinaryArray
+ if (TYPEOF(ptype) == RAWSXP) {
+ return arrow::binary();
+ }
+
+ return arrow::list(InferArrowType(ptype));
Review comment:
If I read this correctly, you're introducing general support for `vctrs::list_of ptype` here? IIUC then it would make sense for the ListArray to R vector conversion to set that class and attribute too then, right? (If so, feel free to add that here or defer to a followup)
##########
File path: r/src/array_to_vector.cpp
##########
@@ -693,6 +741,9 @@ std::shared_ptr<Converter> Converter::Make(const std::shared_ptr<DataType>& type
case Type::BOOL:
return std::make_shared<arrow::r::Converter_Boolean>(std::move(arrays));
+ case Type::BINARY:
Review comment:
See also https://jira.apache.org/jira/browse/ARROW-6543
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#issuecomment-650122091
Also deals with LargeBinary now, e.g. https://issues.apache.org/jira/browse/ARROW-6543
``` r
library(arrow, warn.conflicts = FALSE)
a <- Array$create(list(raw()), type = large_binary())
a
#> Array
#> <large_binary>
#> [
#>
#> ]
a$as_vector()
#> <list_of<raw>[1]>
#> [[1]]
#> raw(0)
```
<sup>Created on 2020-06-26 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0.9001)</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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] lionel- commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
lionel- commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r444292367
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
Review comment:
Yes in vctrs and rlang we generally use pointer comparison with singletons (NULL, symbols, cached objects). I guess it's best to use the local style though?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r444302116
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
Review comment:
Doesn't matter to me which we use as long as we're consistent. Happy to update everywhere to follow whichever pattern is preferred.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r448483516
##########
File path: r/src/array_from_vector.cpp
##########
@@ -918,6 +923,97 @@ class Time64Converter : public TimeConverter<Time64Type> {
}
};
+template <typename Builder>
+class BinaryVectorConverter : public VectorConverter {
+ public:
+ ~BinaryVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != VECSXP, Status::RError("Expecting a list"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (!Rf_isNull(obj_i)) {
+ ARROW_RETURN_IF(TYPEOF(obj_i) != RAWSXP,
+ Status::RError("Expecting a raw vector"));
+ size += XLENGTH(obj_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (Rf_isNull(obj_i)) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(RAW(obj_i), XLENGTH(obj_i)));
+ }
+ }
+ return Status::OK();
+ }
+
+ Status GetResult(std::shared_ptr<arrow::Array>* result) {
+ return typed_builder_->Finish(result);
+ }
+
+ private:
+ Builder* typed_builder_;
+};
+
+template <typename Builder>
+class StringVectorConverter : public VectorConverter {
+ public:
+ ~StringVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != STRSXP,
+ Status::RError("Expecting a character vector"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i != NA_STRING) {
+ size += XLENGTH(string_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i == NA_STRING) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(CHAR(string_i), XLENGTH(string_i)));
Review comment:
I fixed this; will deal with efficiency/redundancy in a followup.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] nealrichardson commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
nealrichardson commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r448490350
##########
File path: r/src/array_to_vector.cpp
##########
@@ -693,6 +741,9 @@ std::shared_ptr<Converter> Converter::Make(const std::shared_ptr<DataType>& type
case Type::BOOL:
return std::make_shared<arrow::r::Converter_Boolean>(std::move(arrays));
+ case Type::BINARY:
Review comment:
Fixed types: https://issues.apache.org/jira/browse/ARROW-9291
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446483934
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -18,16 +18,16 @@
context("Array")
expect_array_roundtrip <- function(x, type) {
- a <- Array$create(x)
+ a <- Array$create(x, type = type)
Review comment:
✅
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#issuecomment-650204663
Added support for LargeList:
``` r
library(arrow, warn.conflicts = FALSE)
a <- Array$create(list(integer()), type = large_list_of(int32()))
a
#> LargeListArray
#> <large_list<item: int32>>
#> [
#> []
#> ]
a$as_vector()
#> <list_of<integer>[1]>
#> [[1]]
#> integer(0)
```
<sup>Created on 2020-06-26 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0.9001)</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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r444308097
##########
File path: r/src/array_from_vector.cpp
##########
@@ -1067,12 +1110,22 @@ std::shared_ptr<arrow::DataType> InferArrowTypeFromVector<VECSXP>(SEXP x) {
if (Rf_inherits(x, "data.frame")) {
return InferArrowTypeFromDataFrame(x);
} else {
- if (XLENGTH(x) == 0) {
- Rcpp::stop(
- "Requires at least one element to infer the values' type of a list vector");
- }
+ SEXP ptype = Rf_getAttrib(x, symbols::ptype);
+ if (ptype == R_NilValue) {
Review comment:
Counting 6 cases of `Rf_isNull()` and 3 cases of `== R_NilValue` I'll switch to `Rf_isNull()` here
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] fsaintjacques commented on a change in pull request #7514: ARROW-6235 : [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
fsaintjacques commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r443639526
##########
File path: r/src/array_to_vector.cpp
##########
@@ -693,6 +741,9 @@ std::shared_ptr<Converter> Converter::Make(const std::shared_ptr<DataType>& type
case Type::BOOL:
return std::make_shared<arrow::r::Converter_Boolean>(std::move(arrays));
+ case Type::BINARY:
Review comment:
Since this is in your mental cache, we can accept FIXED_BINARY and LARGE_BINARY with minimal effort by using `GetView` instead (and some template dispatching because they don't share the same base class). LARGE_BINARY will require to bound check each value with INT32_MAX.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] romainfrancois commented on a change in pull request #7514: ARROW-6235: [R] Implement conversion from arrow::BinaryArray to R character vector
Posted by GitBox <gi...@apache.org>.
romainfrancois commented on a change in pull request #7514:
URL: https://github.com/apache/arrow/pull/7514#discussion_r446816192
##########
File path: r/src/array_from_vector.cpp
##########
@@ -918,6 +923,97 @@ class Time64Converter : public TimeConverter<Time64Type> {
}
};
+template <typename Builder>
+class BinaryVectorConverter : public VectorConverter {
+ public:
+ ~BinaryVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != VECSXP, Status::RError("Expecting a list"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (!Rf_isNull(obj_i)) {
+ ARROW_RETURN_IF(TYPEOF(obj_i) != RAWSXP,
+ Status::RError("Expecting a raw vector"));
+ size += XLENGTH(obj_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP obj_i = VECTOR_ELT(obj, i);
+ if (Rf_isNull(obj_i)) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(RAW(obj_i), XLENGTH(obj_i)));
+ }
+ }
+ return Status::OK();
+ }
+
+ Status GetResult(std::shared_ptr<arrow::Array>* result) {
+ return typed_builder_->Finish(result);
+ }
+
+ private:
+ Builder* typed_builder_;
+};
+
+template <typename Builder>
+class StringVectorConverter : public VectorConverter {
+ public:
+ ~StringVectorConverter() {}
+
+ Status Init(ArrayBuilder* builder) {
+ typed_builder_ = checked_cast<Builder*>(builder);
+ return Status::OK();
+ }
+
+ Status Ingest(SEXP obj) {
+ ARROW_RETURN_IF(TYPEOF(obj) != STRSXP,
+ Status::RError("Expecting a character vector"));
+ R_xlen_t n = XLENGTH(obj);
+
+ // Reserve enough space before appending
+ int64_t size = 0;
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i != NA_STRING) {
+ size += XLENGTH(string_i);
+ }
+ }
+ RETURN_NOT_OK(typed_builder_->Reserve(size));
+
+ // append
+ for (R_xlen_t i = 0; i < n; i++) {
+ SEXP string_i = STRING_ELT(obj, i);
+ if (string_i == NA_STRING) {
+ RETURN_NOT_OK(typed_builder_->AppendNull());
+ } else {
+ RETURN_NOT_OK(typed_builder_->Append(CHAR(string_i), XLENGTH(string_i)));
Review comment:
I'll have a look once back at this. Overall there seems to be two concurrent systems with no real reason and I believe we should only keep the once powered by `VectorToArrayConverter` .
I'm on my `dplyr` week now, I'll try still to make some space for this.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org