You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tadeja (via GitHub)" <gi...@apache.org> on 2026/05/01 16:06:19 UTC
Re: [PR] GH-40062: [C++][Python] Conversion of Table to Arrow Tensor [arrow]
tadeja commented on code in PR #41870:
URL: https://github.com/apache/arrow/pull/41870#discussion_r3173976019
##########
cpp/src/arrow/table_test.cc:
##########
@@ -520,6 +521,539 @@ TEST_F(TestTable, ConcatenateTables) {
ASSERT_RAISES(Invalid, ConcatenateTables({t1, t3}));
}
+TEST_F(TestTable, ToTensorUnsupportedType) {
+ auto f0 = field("f0", int32());
+ // Unsupported data type
+ auto f1 = field("f1", utf8());
+
+ std::vector<std::shared_ptr<Field>> fields = {f0, f1};
+ auto schema = ::arrow::schema(fields);
+
+ auto a0 = ChunkedArrayFromJSON(int32(), {"[1, 2, 3]", "[4, 5, 6, 7, 8, 9]"});
+ auto a1 = ChunkedArrayFromJSON(
+ utf8(), {R"(["a", "b", "c", "a", "b"])", R"(["c", "a", "b", "c"])"});
+
+ auto table = Table::Make(schema, {a0, a1});
+
+ ASSERT_RAISES_WITH_MESSAGE(
+ TypeError, "Type error: DataType is not supported: " + a1->type()->ToString(),
+ table->ToTensor());
+
+ // Unsupported boolean data type
+ auto f2 = field("f2", boolean());
+
+ std::vector<std::shared_ptr<Field>> fields2 = {f0, f2};
+ auto schema2 = ::arrow::schema(fields2);
+ auto a2 = ChunkedArrayFromJSON(
+ boolean(), {"[true, false, true, true, false, true, false, true, true]"});
+ auto table2 = Table::Make(schema2, {a0, a2});
+
+ ASSERT_RAISES_WITH_MESSAGE(
+ TypeError, "Type error: DataType is not supported: " + a2->type()->ToString(),
+ table2->ToTensor());
+}
+
+TEST_F(TestTable, ToTensorUnsupportedMissing) {
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", int32());
+
+ std::vector<std::shared_ptr<Field>> fields = {f0, f1};
+ auto schema = ::arrow::schema(fields);
+
+ auto a0 = ChunkedArrayFromJSON(int32(), {"[1, 2, 3]", "[4, 5, 6, 7, 8, 9]"});
+ auto a1 = ChunkedArrayFromJSON(int32(), {"[10, 20]", "[30, 40, null, 60, 70, 80, 90]"});
+
+ auto table = Table::Make(schema, {a0, a1});
+
+ ASSERT_RAISES_WITH_MESSAGE(
+ TypeError,
+ "Type error: Can only convert a Table or RecordBatch with no "
+ "nulls. Set null_to_nan to true to convert nulls to NaN",
+ table->ToTensor());
+}
+
+TEST_F(TestTable, ToTensorEmptyTable) {
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", int32());
+
+ std::vector<std::shared_ptr<Field>> fields = {f0, f1};
+ auto schema = ::arrow::schema(fields);
+
+ ASSERT_OK_AND_ASSIGN(std::shared_ptr<Table> empty, Table::MakeEmpty(schema));
+
+ ASSERT_OK_AND_ASSIGN(auto tensor_column,
+ empty->ToTensor(/*null_to_nan=*/false, /*row_major=*/false));
+ ASSERT_OK(tensor_column->Validate());
+
+ ASSERT_OK_AND_ASSIGN(auto tensor_row, empty->ToTensor());
+ ASSERT_OK(tensor_row->Validate());
+
+ const std::vector<int64_t> strides = {4, 4};
+ const std::vector<int64_t> shape = {0, 2};
+
+ EXPECT_EQ(strides, tensor_column->strides());
+ EXPECT_EQ(shape, tensor_column->shape());
+ EXPECT_EQ(strides, tensor_row->strides());
+ EXPECT_EQ(shape, tensor_row->shape());
+
+ std::vector<std::shared_ptr<Array>> columns;
+ auto t2 = Table::Make(::arrow::schema({}), columns);
Review Comment:
```suggestion
```
oh, this could be omitted!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org