MCPcopy Create free account
hub / github.com/apache/arrow / Table__from_dots

Function Table__from_dots

r/src/r_to_arrow.cpp:1463–1589  ·  view source on GitHub ↗

[[arrow::export]]

Source from the content-addressed store, hash-verified

1461
1462// [[arrow::export]]
1463std::shared_ptr<arrow::Table> Table__from_dots(SEXP lst, SEXP schema_sxp,
1464 bool use_threads) {
1465 bool infer_schema = !Rf_inherits(schema_sxp, "Schema");
1466
1467 int num_fields;
1468 StopIfNotOk(arrow::r::count_fields(lst, &num_fields));
1469
1470 // schema + metadata
1471 std::shared_ptr<arrow::Schema> schema;
1472 StopIfNotOk(arrow::r::InferSchemaFromDots(lst, schema_sxp, num_fields, schema));
1473 StopIfNotOk(arrow::r::AddMetadataFromDots(lst, num_fields, schema));
1474
1475 if (!infer_schema && schema->num_fields() != num_fields) {
1476 cpp11::stop("incompatible. schema has %d fields, and %d columns are supplied",
1477 schema->num_fields(), num_fields);
1478 }
1479
1480 // table
1481 std::vector<std::shared_ptr<arrow::ChunkedArray>> columns(num_fields);
1482
1483 if (!infer_schema) {
1484 auto check_name = [&](int j, SEXP, cpp11::r_string name) {
1485 std::string cpp_name(name);
1486 if (schema->field(j)->name() != cpp_name) {
1487 cpp11::stop("field at index %d has name '%s' != '%s'", j + 1,
1488 schema->field(j)->name().c_str(), cpp_name.c_str());
1489 }
1490 };
1491 arrow::r::TraverseDots(lst, num_fields, check_name);
1492 }
1493
1494 // must be careful to avoid R stop() until the tasks
1495 // are finished, i.e. after tasks.Finish()
1496 arrow::r::RTasks tasks(use_threads);
1497
1498 arrow::Status status = arrow::Status::OK();
1499
1500 auto flatten_lst = arrow::r::FlattenDots(lst, num_fields);
1501 std::vector<std::unique_ptr<arrow::r::RConverter>> converters(num_fields);
1502
1503 // init converters
1504 for (int j = 0; j < num_fields && status.ok(); j++) {
1505 SEXP x = flatten_lst[j];
1506
1507 if (Rf_inherits(x, "ChunkedArray")) {
1508 columns[j] = cpp11::as_cpp<std::shared_ptr<arrow::ChunkedArray>>(x);
1509 } else if (Rf_inherits(x, "Array")) {
1510 columns[j] = std::make_shared<arrow::ChunkedArray>(
1511 cpp11::as_cpp<std::shared_ptr<arrow::Array>>(x));
1512 } else if (arrow::r::altrep::is_unmaterialized_arrow_altrep(x)) {
1513 columns[j] = arrow::r::altrep::vec_to_arrow_altrep_bypass(x);
1514 } else {
1515 arrow::r::RConversionOptions options;
1516 options.strict = !infer_schema;
1517 options.type = schema->field(j)->type();
1518 options.size = arrow::r::vec_size(x);
1519
1520 // If we can handle this in C++ we do so; otherwise we use the

Callers 1

_arrow_Table__from_dotsFunction · 0.85

Calls 15

StopIfNotOkFunction · 0.85
count_fieldsFunction · 0.85
InferSchemaFromDotsFunction · 0.85
AddMetadataFromDotsFunction · 0.85
stopFunction · 0.85
TraverseDotsFunction · 0.85
FlattenDotsFunction · 0.85
vec_sizeFunction · 0.85
can_convert_nativeFunction · 0.85
vector_from_r_memoryFunction · 0.85

Tested by

no test coverage detected