| 342 | |
| 343 | template <typename Container> |
| 344 | Status ToTensorImpl(const Container& container, bool null_to_nan, bool row_major, |
| 345 | MemoryPool* pool, std::shared_ptr<Tensor>* tensor) { |
| 346 | if (container.num_columns() == 0) { |
| 347 | return Status::TypeError( |
| 348 | "Conversion to Tensor for Tables or RecordBatches without columns/schema is not " |
| 349 | "supported."); |
| 350 | } |
| 351 | // Check for no validity bitmap of each field |
| 352 | // if null_to_nan conversion is set to false |
| 353 | for (int i = 0; i < container.num_columns(); ++i) { |
| 354 | int64_t null_count = 0; |
| 355 | if constexpr (std::is_same_v<Container, Table>) { |
| 356 | null_count = container.column(i)->null_count(); |
| 357 | } else if constexpr (std::is_same_v<Container, RecordBatch>) { |
| 358 | null_count = container.column_data(i)->GetNullCount(); |
| 359 | } |
| 360 | if (null_count > 0 && !null_to_nan) { |
| 361 | return Status::TypeError( |
| 362 | "Can only convert a Table or RecordBatch with no nulls. Set null_to_nan to " |
| 363 | "true to convert nulls to NaN"); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Check for supported data types and merge fields |
| 368 | // to get the resulting uniform data type |
| 369 | const auto& col_0_type = container.schema()->field(0)->type(); |
| 370 | if (!is_integer(col_0_type->id()) && !is_floating(col_0_type->id())) { |
| 371 | return Status::TypeError("DataType is not supported: ", col_0_type->ToString()); |
| 372 | } |
| 373 | std::shared_ptr<Field> result_field = container.schema()->field(0); |
| 374 | std::shared_ptr<DataType> result_type = result_field->type(); |
| 375 | |
| 376 | Field::MergeOptions options; |
| 377 | options.promote_integer_to_float = true; |
| 378 | options.promote_integer_sign = true; |
| 379 | options.promote_numeric_width = true; |
| 380 | |
| 381 | if (container.num_columns() > 1) { |
| 382 | for (int i = 1; i < container.num_columns(); ++i) { |
| 383 | const auto& col_type = container.schema()->field(i)->type(); |
| 384 | |
| 385 | if (!is_numeric(col_type->id())) { |
| 386 | return Status::TypeError("DataType is not supported: ", col_type->ToString()); |
| 387 | } |
| 388 | |
| 389 | // Casting of float16 is not supported, throw an error in this case |
| 390 | if ((col_type->id() == Type::HALF_FLOAT || |
| 391 | result_field->type()->id() == Type::HALF_FLOAT) && |
| 392 | col_type->id() != result_field->type()->id()) { |
| 393 | return Status::NotImplemented("Casting from or to halffloat is not supported."); |
| 394 | } |
| 395 | |
| 396 | if (!col_type->Equals(result_field->type())) { |
| 397 | ARROW_ASSIGN_OR_RAISE( |
| 398 | result_field, |
| 399 | result_field->MergeWith( |
| 400 | container.schema()->field(i)->WithName(result_field->name()), options)); |
| 401 | } |
no test coverage detected