Skip to content

Commit 20660e3

Browse files
committed
wire up blob
1 parent c8eb571 commit 20660e3

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

r/src/vctr_builder.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "vctr_builder.h"
3131
#include "vctr_builder_base.h"
32+
#include "vctr_builder_blob.h"
3233
#include "vctr_builder_chr.h"
3334
#include "vctr_builder_dbl.h"
3435
#include "vctr_builder_int.h"
@@ -195,10 +196,10 @@ ArrowErrorCode InstantiateBuilder(const ArrowSchema* schema, SEXP ptype_sexp,
195196
vector_type = VECTOR_TYPE_DATA_FRAME;
196197
} else if (Rf_inherits(ptype_sexp, "vctrs_unspecified")) {
197198
vector_type = VECTOR_TYPE_UNSPECIFIED;
198-
} else if (Rf_inherits(ptype_sexp, "vctrs_list_of")) {
199-
vector_type = VECTOR_TYPE_LIST_OF;
200199
} else if (Rf_inherits(ptype_sexp, "blob")) {
201200
vector_type = VECTOR_TYPE_BLOB;
201+
} else if (Rf_inherits(ptype_sexp, "vctrs_list_of")) {
202+
vector_type = VECTOR_TYPE_LIST_OF;
202203
} else if (Rf_inherits(ptype_sexp, "Date")) {
203204
vector_type = VECTOR_TYPE_DATE;
204205
} else if (Rf_inherits(ptype_sexp, "hms")) {

r/src/vctr_builder_blob.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#ifndef R_NANOARROW_VCTR_BUILDER_BLOB_H_INCLUDED
19+
#define R_NANOARROW_VCTR_BUILDER_BLOB_H_INCLUDED
20+
21+
#define R_NO_REMAP
22+
#include <R.h>
23+
#include <Rinternals.h>
24+
25+
#include "vctr_builder_base.h"
26+
27+
class BlobBuilder : public VctrBuilder {
28+
public:
29+
explicit BlobBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_BLOB, ptype_sexp) {}
30+
31+
ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override {
32+
NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error));
33+
SEXP value = PROTECT(Rf_allocVector(VECSXP, n));
34+
SetValue(value);
35+
UNPROTECT(1);
36+
return NANOARROW_OK;
37+
}
38+
39+
ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
40+
NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error));
41+
R_xlen_t length = array_view_.length;
42+
43+
switch (array_view_.storage_type) {
44+
case NANOARROW_TYPE_NA:
45+
// Works because lists are filled with R_NilValue by default
46+
// when allocated.
47+
return NANOARROW_OK;
48+
case NANOARROW_TYPE_STRING:
49+
case NANOARROW_TYPE_LARGE_STRING:
50+
case NANOARROW_TYPE_BINARY:
51+
case NANOARROW_TYPE_LARGE_BINARY:
52+
break;
53+
default:
54+
return ENOTSUP;
55+
}
56+
57+
struct ArrowBufferView item;
58+
SEXP item_sexp;
59+
for (R_xlen_t i = 0; i < length; i++) {
60+
if (!ArrowArrayViewIsNull(&array_view_, i)) {
61+
item = ArrowArrayViewGetBytesUnsafe(&array_view_, i);
62+
item_sexp = PROTECT(Rf_allocVector(RAWSXP, item.size_bytes));
63+
memcpy(RAW(item_sexp), item.data.data, item.size_bytes);
64+
SET_VECTOR_ELT(value_, value_size_ + i, item_sexp);
65+
UNPROTECT(1);
66+
}
67+
}
68+
69+
return NANOARROW_OK;
70+
}
71+
};
72+
73+
#endif

r/src/vctr_builder_primitive.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424

2525
#include "vctr_builder_base.h"
2626

27-
class BlobBuilder : public VctrBuilder {
28-
public:
29-
explicit BlobBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_BLOB, ptype_sexp) {}
30-
};
31-
3227
class DateBuilder : public VctrBuilder {
3328
public:
3429
explicit DateBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_DATE, ptype_sexp) {}

0 commit comments

Comments
 (0)