@@ -157,24 +157,32 @@ static ArrowErrorCode InstantiateBuilderBase(const ArrowSchema* schema,
157157ArrowErrorCode 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
0 commit comments