rok commented on code in PR #8510:
URL: https://github.com/apache/arrow/pull/8510#discussion_r1092723728


##########
cpp/src/arrow/extension_type_test.cc:
##########
@@ -333,4 +335,82 @@ TEST_F(TestExtensionType, ValidateExtensionArray) {
   ASSERT_OK(ext_arr4->ValidateFull());
 }
 
+TEST_F(TestExtensionType, TensorArrayType) {
+  using TensorArrayType = extension::TensorArrayType;
+
+  auto value_type = int64();
+  std::vector<int64_t> shape = {3, 3, 4};
+  std::vector<int64_t> row_major_strides = {36, 12, 4};
+  std::vector<int64_t> column_major_strides = {4, 12, 36};
+  std::vector<int64_t> values = {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 
11,
+                                 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23,
+                                 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 
35};
+  std::vector<int64_t> values_partial = {0,  1,  2,  3,  4,  5,  6,  7,  8,  
9,  10, 11,
+                                         12, 13, 14, 15, 16, 17, 18, 19, 20, 
21, 22, 23};
+  std::vector<int64_t> shape_partial = {2, 3, 4};
+
+  ASSERT_OK_AND_ASSIGN(auto tensor, Tensor::Make(value_type, 
Buffer::Wrap(values), shape,
+                                                 row_major_strides));
+  ASSERT_OK_AND_ASSIGN(auto tensor_partial,
+                       Tensor::Make(value_type, Buffer::Wrap(values_partial),
+                                    shape_partial, row_major_strides));
+
+  std::shared_ptr<ExtensionType> row_major_ext_type = extension::tensor_array(
+      value_type, shape, /*is_row_major=*/true, "{\"key1\": \"metadata1\"}");
+  std::shared_ptr<ExtensionType> column_major_ext_type =
+      extension::tensor_array(value_type, shape, /*is_row_major=*/false, 
"{\"key2\": 1}");
+  auto exact_ext_type = 
std::dynamic_pointer_cast<TensorArrayType>(row_major_ext_type);
+
+  auto ext_arr = exact_ext_type->MakeArray(tensor);
+  auto ext_arr_partial = exact_ext_type->MakeArray(tensor_partial);
+
+  ASSERT_EQ(row_major_ext_type->Serialize(),
+            "{\"shape\":[3,3,4],\"order\":\"C\",\"metadata\":\"{\\\"key1\\\": "
+            "\\\"metadata1\\\"}\"}");
+  ASSERT_EQ(column_major_ext_type->Serialize(),
+            "{\"shape\":[3,3,4],\"order\":\"F\",\"metadata\":\"{\\\"key2\\\": 
1}\"}");
+  ASSERT_EQ(row_major_ext_type->extension_name(), "arrow.fixed_size_tensor");
+  ASSERT_OK(ext_arr->ValidateFull());
+  ASSERT_OK(ext_arr_partial->ValidateFull());
+
+  AssertArraysEqual(*ext_arr->storage()->Slice(0, 24), 
*ext_arr_partial->storage());
+  auto arr_slice = ext_arr->Slice(0, 24);
+  auto tensor_slice = exact_ext_type->ToTensor(arr_slice);
+  // TODO
+  // ASSERT_TRUE(tensor_partial->Equals(*tensor_slice));
+
+  auto type = std::dynamic_pointer_cast<TensorArrayType>(ext_arr->type());
+  ASSERT_EQ(type->id(), Type::EXTENSION);
+
+  std::string serialized = row_major_ext_type->Serialize();
+  ASSERT_OK_AND_ASSIGN(auto deserialized,
+                       row_major_ext_type->Deserialize(row_major_ext_type, 
serialized));
+  // TODO
+  // ASSERT_TRUE(deserialized->Equals(row_major_ext_type));
+  ASSERT_FALSE(deserialized->Equals(*fixed_size_binary(16)));
+
+  auto batch =
+      RecordBatch::Make(schema({field("f0", type)}), ext_arr->length(), 
{ext_arr});
+
+  std::shared_ptr<RecordBatch> read_batch;
+  RoundtripBatch(batch, &read_batch);
+  // TODO: this should be true?
+  // CompareBatch(*batch, *read_batch, /* compare_metadata */ false);

Review Comment:
   Indeed this required registering the type and now works.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to