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

Function ToTensorImpl

cpp/src/arrow/tensor.cc:371–504  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

369
370template <typename Container>
371Status ToTensorImpl(const Container& container, bool null_to_nan, bool row_major,
372 MemoryPool* pool, std::shared_ptr<Tensor>* tensor) {
373 if (container.num_columns() == 0) {
374 return Status::TypeError(
375 "Conversion to Tensor for Tables or RecordBatches without columns/schema is not "
376 "supported.");
377 }
378 // Check for no validity bitmap of each field
379 // if null_to_nan conversion is set to false
380 for (int i = 0; i < container.num_columns(); ++i) {
381 int64_t null_count = 0;
382 if constexpr (std::is_same_v<Container, Table>) {
383 null_count = container.column(i)->null_count();
384 } else if constexpr (std::is_same_v<Container, RecordBatch>) {
385 null_count = container.column_data(i)->GetNullCount();
386 }
387 if (null_count > 0 && !null_to_nan) {
388 return Status::TypeError(
389 "Can only convert a Table or RecordBatch with no nulls. Set null_to_nan to "
390 "true to convert nulls to NaN");
391 }
392 }
393
394 // Check for supported data types and merge fields
395 // to get the resulting uniform data type
396 const auto& col_0_type = container.schema()->field(0)->type();
397 if (!is_integer(col_0_type->id()) && !is_floating(col_0_type->id())) {
398 return Status::TypeError("DataType is not supported: ", col_0_type->ToString());
399 }
400 std::shared_ptr<Field> result_field = container.schema()->field(0);
401 std::shared_ptr<DataType> result_type = result_field->type();
402
403 Field::MergeOptions options;
404 options.promote_integer_to_float = true;
405 options.promote_integer_sign = true;
406 options.promote_numeric_width = true;
407
408 if (container.num_columns() > 1) {
409 for (int i = 1; i < container.num_columns(); ++i) {
410 const auto& col_type = container.schema()->field(i)->type();
411
412 if (!is_numeric(col_type->id())) {
413 return Status::TypeError("DataType is not supported: ", col_type->ToString());
414 }
415
416 // Casting of float16 is not supported, throw an error in this case
417 if ((col_type->id() == Type::HALF_FLOAT ||
418 result_field->type()->id() == Type::HALF_FLOAT) &&
419 col_type->id() != result_field->type()->id()) {
420 return Status::NotImplemented("Casting from or to halffloat is not supported.");
421 }
422
423 if (!col_type->Equals(result_field->type())) {
424 ARROW_ASSIGN_OR_RAISE(
425 result_field,
426 result_field->MergeWith(
427 container.schema()->field(i)->WithName(result_field->name()), options));
428 }

Callers 2

TableToTensorFunction · 0.85
RecordBatchToTensorFunction · 0.85

Calls 15

is_numericFunction · 0.85
MultiplyWithOverflowFunction · 0.85
ComputeRowMajorStridesFunction · 0.85
GetNullCountMethod · 0.80
column_dataMethod · 0.80
MergeWithMethod · 0.80
WithNameMethod · 0.80
TypeErrorFunction · 0.70
is_integerFunction · 0.70
is_floatingFunction · 0.70
NotImplementedFunction · 0.70

Tested by

no test coverage detected