@@ -619,37 +619,37 @@ TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
619619 ASSERT_BATCHES_EQUAL (*batch, *null_batch);
620620}
621621
622- TEST_F (TestRecordBatch, ToTensorUnsupported ) {
622+ TEST_F (TestRecordBatch, ToTensorUnsupportedType ) {
623623 const int length = 9 ;
624624
625- // Mixed data type
626625 auto f0 = field (" f0" , int32 ());
627- auto f1 = field (" f1" , int64 ());
626+ // Unsupported data type
627+ auto f1 = field (" f1" , utf8 ());
628628
629629 std::vector<std::shared_ptr<Field>> fields = {f0, f1};
630630 auto schema = ::arrow::schema (fields);
631631
632632 auto a0 = ArrayFromJSON (int32 (), " [1, 2, 3, 4, 5, 6, 7, 8, 9]" );
633- auto a1 = ArrayFromJSON (int64 (), " [10, 20, 30, 40, 50, 60, 70, 80, 90] " );
633+ auto a1 = ArrayFromJSON (utf8 (), R"( ["a", "b", "c", "a", "b", "c", "a", "b", "c"] ) " );
634634
635635 auto batch = RecordBatch::Make (schema, length, {a0, a1});
636636
637637 ASSERT_RAISES_WITH_MESSAGE (
638- TypeError, " Type error: Can only convert a RecordBatch with uniform data type. " ,
638+ TypeError, " Type error: DataType is not supported: " + a1-> type ()-> ToString () ,
639639 batch->ToTensor ());
640640
641- // Unsupported data type
642- auto f2 = field (" f2" , utf8 ());
643-
644- std::vector<std::shared_ptr<Field>> fields_1 = {f2};
645- auto schema_2 = ::arrow::schema (fields_1);
641+ // Unsupported boolean data type
642+ auto f2 = field (" f2" , boolean ());
646643
647- auto a2 = ArrayFromJSON (utf8 (), R"( ["a", "b", "c", "a", "b", "c", "a", "b", "c"])" );
648- auto batch_2 = RecordBatch::Make (schema_2, length, {a2});
644+ std::vector<std::shared_ptr<Field>> fields2 = {f0, f2};
645+ auto schema2 = ::arrow::schema (fields2);
646+ auto a2 = ArrayFromJSON (boolean (),
647+ " [true, false, true, true, false, true, false, true, true]" );
648+ auto batch2 = RecordBatch::Make (schema2, length, {a0, a2});
649649
650650 ASSERT_RAISES_WITH_MESSAGE (
651651 TypeError, " Type error: DataType is not supported: " + a2->type ()->ToString (),
652- batch_2 ->ToTensor ());
652+ batch2 ->ToTensor ());
653653}
654654
655655TEST_F (TestRecordBatch, ToTensorUnsupportedMissing) {
@@ -740,6 +740,108 @@ TEST_F(TestRecordBatch, ToTensorSupportedNaN) {
740740 CheckTensor<FloatType>(tensor, 18 , shape, f_strides);
741741}
742742
743+ TEST_F (TestRecordBatch, ToTensorSupportedTypesMixed) {
744+ const int length = 9 ;
745+
746+ auto f0 = field (" f0" , uint16 ());
747+ auto f1 = field (" f1" , int16 ());
748+ auto f2 = field (" f2" , float32 ());
749+
750+ auto a0 = ArrayFromJSON (uint16 (), " [1, 2, 3, 4, 5, 6, 7, 8, 9]" );
751+ auto a1 = ArrayFromJSON (int16 (), " [10, 20, 30, 40, 50, 60, 70, 80, 90]" );
752+ auto a2 = ArrayFromJSON (float32 (), " [100, 200, 300, NaN, 500, 600, 700, 800, 900]" );
753+
754+ // Single column
755+ std::vector<std::shared_ptr<Field>> fields = {f0};
756+ auto schema = ::arrow::schema (fields);
757+ auto batch = RecordBatch::Make (schema, length, {a0});
758+
759+ ASSERT_OK_AND_ASSIGN (auto tensor, batch->ToTensor ());
760+ ASSERT_OK (tensor->Validate ());
761+
762+ std::vector<int64_t > shape = {9 , 1 };
763+ const int64_t uint16_size = sizeof (uint16_t );
764+ std::vector<int64_t > f_strides = {uint16_size, uint16_size * shape[0 ]};
765+ std::shared_ptr<Tensor> tensor_expected =
766+ TensorFromJSON (uint16 (), " [1, 2, 3, 4, 5, 6, 7, 8, 9]" , shape, f_strides);
767+
768+ EXPECT_TRUE (tensor_expected->Equals (*tensor));
769+ CheckTensor<UInt16Type>(tensor, 9 , shape, f_strides);
770+
771+ // uint16 + int16 = int32
772+ std::vector<std::shared_ptr<Field>> fields1 = {f0, f1};
773+ auto schema1 = ::arrow::schema (fields1);
774+ auto batch1 = RecordBatch::Make (schema1, length, {a0, a1});
775+
776+ ASSERT_OK_AND_ASSIGN (auto tensor1, batch1->ToTensor ());
777+ ASSERT_OK (tensor1->Validate ());
778+
779+ std::vector<int64_t > shape1 = {9 , 2 };
780+ const int64_t int32_size = sizeof (int32_t );
781+ std::vector<int64_t > f_strides_1 = {int32_size, int32_size * shape1[0 ]};
782+ std::shared_ptr<Tensor> tensor_expected_1 = TensorFromJSON (
783+ int32 (), " [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 70, 80, 90]" ,
784+ shape1, f_strides_1);
785+
786+ EXPECT_TRUE (tensor_expected_1->Equals (*tensor1));
787+
788+ CheckTensor<Int32Type>(tensor1, 18 , shape1, f_strides_1);
789+
790+ ASSERT_EQ (tensor1->type ()->bit_width (), tensor_expected_1->type ()->bit_width ());
791+
792+ ASSERT_EQ (1 , tensor_expected_1->Value <Int32Type>({0 , 0 }));
793+ ASSERT_EQ (2 , tensor_expected_1->Value <Int32Type>({1 , 0 }));
794+ ASSERT_EQ (10 , tensor_expected_1->Value <Int32Type>({0 , 1 }));
795+
796+ // uint16 + int16 + float32 = float64
797+ std::vector<std::shared_ptr<Field>> fields2 = {f0, f1, f2};
798+ auto schema2 = ::arrow::schema (fields2);
799+ auto batch2 = RecordBatch::Make (schema2, length, {a0, a1, a2});
800+
801+ ASSERT_OK_AND_ASSIGN (auto tensor2, batch2->ToTensor ());
802+ ASSERT_OK (tensor2->Validate ());
803+
804+ std::vector<int64_t > shape2 = {9 , 3 };
805+ const int64_t f64_size = sizeof (double );
806+ std::vector<int64_t > f_strides_2 = {f64_size, f64_size * shape2[0 ]};
807+ std::shared_ptr<Tensor> tensor_expected_2 =
808+ TensorFromJSON (float64 (),
809+ " [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, "
810+ " 60, 70, 80, 90, 100, 200, 300, NaN, 500, 600, 700, 800, 900]" ,
811+ shape2, f_strides_2);
812+
813+ EXPECT_FALSE (tensor_expected_2->Equals (*tensor2));
814+ EXPECT_TRUE (tensor_expected_2->Equals (*tensor2, EqualOptions ().nans_equal (true )));
815+
816+ CheckTensor<DoubleType>(tensor2, 27 , shape2, f_strides_2);
817+ }
818+
819+ TEST_F (TestRecordBatch, ToTensorUnsupportedMixedFloat16) {
820+ const int length = 9 ;
821+
822+ auto f0 = field (" f0" , float16 ());
823+ auto f1 = field (" f1" , float64 ());
824+
825+ auto a0 = ArrayFromJSON (float16 (), " [1, 2, 3, 4, 5, 6, 7, 8, 9]" );
826+ auto a1 = ArrayFromJSON (float64 (), " [10, 20, 30, 40, 50, 60, 70, 80, 90]" );
827+
828+ std::vector<std::shared_ptr<Field>> fields = {f0, f1};
829+ auto schema = ::arrow::schema (fields);
830+ auto batch = RecordBatch::Make (schema, length, {a0, a1});
831+
832+ ASSERT_RAISES_WITH_MESSAGE (
833+ NotImplemented, " NotImplemented: Casting from or to halffloat is not supported." ,
834+ batch->ToTensor ());
835+
836+ std::vector<std::shared_ptr<Field>> fields1 = {f1, f0};
837+ auto schema1 = ::arrow::schema (fields1);
838+ auto batch1 = RecordBatch::Make (schema1, length, {a1, a0});
839+
840+ ASSERT_RAISES_WITH_MESSAGE (
841+ NotImplemented, " NotImplemented: Casting from or to halffloat is not supported." ,
842+ batch1->ToTensor ());
843+ }
844+
743845template <typename DataType>
744846class TestBatchToTensor : public ::testing::Test {};
745847
0 commit comments