Skip to content

Commit d743203

Browse files
committed
always route extension types and dictionaires through the Other route
1 parent 16c4260 commit d743203

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

r/src/vctr_builder.cc

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,32 @@ static ArrowErrorCode InstantiateBuilderBase(const ArrowSchema* schema,
157157
ArrowErrorCode InstantiateBuilder(const ArrowSchema* schema, SEXP ptype_sexp,
158158
VctrBuilderOptions options, VctrBuilder** out,
159159
ArrowError* error) {
160-
// See if we can skip any ptype resolution at all
160+
ArrowSchemaView view;
161+
NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&view, schema, error));
162+
163+
// Extension types and dictionary types always need their ptype resolved in
164+
// R and always need to use the VctrBuilderOther. This simplifies writing
165+
// the builders (e.g., they do not all have to consider these cases).
166+
if (view.extension_name.size_bytes > 0 || view.type == NANOARROW_TYPE_DICTIONARY) {
167+
SEXP inferred_ptype_sexp = PROTECT(call_infer_ptype_other(schema));
168+
int code = InstantiateBuilderBase(schema, VECTOR_TYPE_OTHER, inferred_ptype_sexp, out,
169+
error);
170+
UNPROTECT(1);
171+
return code;
172+
}
173+
161174
if (ptype_sexp == R_NilValue) {
162-
ArrowSchemaView view;
163-
NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&view, schema, error));
164-
165-
// Ensure extension types always go through infer_ptype_other()
166-
if (view.extension_name.size_bytes == 0) {
167-
enum VectorType vector_type = nanoarrow_infer_vector_type(view.type);
168-
switch (vector_type) {
169-
case VECTOR_TYPE_LGL:
170-
case VECTOR_TYPE_INT:
171-
case VECTOR_TYPE_DBL:
172-
case VECTOR_TYPE_CHR:
173-
case VECTOR_TYPE_DATA_FRAME:
174-
return InstantiateBuilderBase(schema, vector_type, R_NilValue, out, error);
175-
default:
176-
break;
177-
}
175+
// See if we can skip any ptype resolution at all
176+
enum VectorType vector_type = nanoarrow_infer_vector_type(view.type);
177+
switch (vector_type) {
178+
case VECTOR_TYPE_LGL:
179+
case VECTOR_TYPE_INT:
180+
case VECTOR_TYPE_DBL:
181+
case VECTOR_TYPE_CHR:
182+
case VECTOR_TYPE_DATA_FRAME:
183+
return InstantiateBuilderBase(schema, vector_type, R_NilValue, out, error);
184+
default:
185+
break;
178186
}
179187

180188
// Otherwise, resolve the ptype and use it (this will error for ptypes that can't be

r/src/vctr_builder_dbl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ class DblBuilder : public VctrBuilder {
3232
explicit DblBuilder(SEXP ptype_sexp, VectorType vector_type = VECTOR_TYPE_DBL)
3333
: VctrBuilder(vector_type, ptype_sexp) {}
3434

35-
SEXP GetPtype() override { return Rf_allocVector(REALSXP, 0); }
35+
SEXP GetPtype() override {
36+
if (ptype_sexp_ != R_NilValue) {
37+
return ptype_sexp_;
38+
} else {
39+
return Rf_allocVector(REALSXP, 0);
40+
}
41+
}
3642

3743
ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override {
3844
NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error));

0 commit comments

Comments
 (0)