| 1148 | } |
| 1149 | |
| 1150 | Status ExtendSetup(SEXP x, int64_t size, int64_t offset) { |
| 1151 | // check that x is compatible |
| 1152 | R_xlen_t n_columns = XLENGTH(x); |
| 1153 | |
| 1154 | if (!Rf_inherits(x, "data.frame") && !Rf_inherits(x, "POSIXlt")) { |
| 1155 | return Status::Invalid("Can only convert data frames to Struct type"); |
| 1156 | } |
| 1157 | |
| 1158 | auto fields = this->struct_type_->fields(); |
| 1159 | if (n_columns != static_cast<R_xlen_t>(fields.size())) { |
| 1160 | return Status::RError("Number of fields in struct (", fields.size(), |
| 1161 | ") incompatible with number of columns in the data frame (", |
| 1162 | n_columns, ")"); |
| 1163 | } |
| 1164 | |
| 1165 | cpp11::strings x_names = Rf_getAttrib(x, R_NamesSymbol); |
| 1166 | |
| 1167 | RETURN_NOT_OK(cpp11::unwind_protect([&] { |
| 1168 | for (int i = 0; i < n_columns; i++) { |
| 1169 | const char* name_i = arrow::r::unsafe::utf8_string(x_names[i]); |
| 1170 | auto field_name = fields[i]->name(); |
| 1171 | if (field_name != name_i) { |
| 1172 | return Status::RError( |
| 1173 | "Field name in position ", i, " (", field_name, |
| 1174 | ") does not match the name of the column of the data frame (", name_i, ")"); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | return Status::OK(); |
| 1179 | })); |
| 1180 | |
| 1181 | for (R_xlen_t i = 0; i < n_columns; i++) { |
| 1182 | SEXP x_i = VECTOR_ELT(x, i); |
| 1183 | if (arrow::r::vec_size(x_i) < size) { |
| 1184 | return Status::RError("Degenerated data frame"); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | RETURN_NOT_OK(this->Reserve(size - offset)); |
| 1189 | |
| 1190 | for (R_xlen_t i = 0; i < size; i++) { |
| 1191 | RETURN_NOT_OK(struct_builder_->Append()); |
| 1192 | } |
| 1193 | |
| 1194 | return Status::OK(); |
| 1195 | } |
| 1196 | }; |
| 1197 | |
| 1198 | template <> |