| 369 | |
| 370 | template <typename Container> |
| 371 | Status 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 | } |
no test coverage detected