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


##########
cpp/src/arrow/extension/fixed_shape_tensor.h:
##########
@@ -0,0 +1,87 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <numeric>
+#include <sstream>
+
+#include "arrow/extension_type.h"
+
+namespace arrow {
+namespace extension {
+
+class ARROW_EXPORT FixedShapeTensor : public ExtensionArray {

Review Comment:
   ```suggestion
   class ARROW_EXPORT FixedShapeTensorArray : public ExtensionArray {
   ```



##########
cpp/src/arrow/extension/fixed_shape_tensor.h:
##########
@@ -0,0 +1,87 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <numeric>
+#include <sstream>
+
+#include "arrow/extension_type.h"
+
+namespace arrow {
+namespace extension {
+
+class ARROW_EXPORT FixedShapeTensor : public ExtensionArray {
+ public:
+  using ExtensionArray::ExtensionArray;
+};
+
+/// \brief Concrete type class for constant-size Tensor data.
+class ARROW_EXPORT FixedShapeTensorType : public ExtensionType {
+ public:
+  FixedShapeTensorType(const std::shared_ptr<DataType>& value_type,
+                       const std::vector<int64_t>& shape,
+                       const std::vector<int64_t>& permutation = {},
+                       const std::vector<std::string>& dim_names = {})
+      : ExtensionType(get_storage_type(value_type, shape)),
+        value_type_(value_type),
+        shape_(shape),
+        permutation_(permutation),
+        dim_names_(dim_names) {}
+
+  std::string extension_name() const override;
+
+  size_t ndim() const;
+
+  std::vector<int64_t> shape() const;
+
+  std::vector<int64_t> strides() const;

Review Comment:
   Can you add some doc comments? (at least for the methods that are specific 
to this type, such strides, which isn't necessarily clear which strides it 
returns)



##########
cpp/src/arrow/extension/fixed_shape_tensor.cc:
##########
@@ -0,0 +1,263 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/extension/fixed_shape_tensor.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/json/rapidjson_defs.h"  // IWYU pragma: keep
+#include "arrow/tensor.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/sort.h"
+
+#include <rapidjson/document.h>
+#include <rapidjson/writer.h>
+
+namespace rj = arrow::rapidjson;
+
+namespace arrow {
+namespace extension {
+
+std::string FixedShapeTensorType::extension_name() const {
+  return "arrow.fixed_shape_tensor";
+}
+
+size_t FixedShapeTensorType::ndim() const { return shape_.size(); }
+
+std::vector<int64_t> FixedShapeTensorType::shape() const { return shape_; }

Review Comment:
   I don't know the importance of this, but I think we typically keep such 
simple accessor methods in the .h file?



##########
cpp/src/arrow/extension/fixed_shape_tensor_test.cc:
##########
@@ -0,0 +1,188 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/extension/fixed_shape_tensor.h"
+
+#include "arrow/testing/matchers.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/io/memory.h"
+#include "arrow/ipc/reader.h"
+#include "arrow/ipc/writer.h"
+#include "arrow/record_batch.h"
+#include "arrow/tensor.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+
+class TestExtensionType : public ::testing::Test {};
+
+auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch,
+                         std::shared_ptr<RecordBatch>* out) {
+  ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
+  ASSERT_OK(ipc::WriteRecordBatchStream({batch}, 
ipc::IpcWriteOptions::Defaults(),
+                                        out_stream.get()));
+
+  ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
+
+  io::BufferReader reader(complete_ipc_stream);
+  std::shared_ptr<RecordBatchReader> batch_reader;
+  ASSERT_OK_AND_ASSIGN(batch_reader, 
ipc::RecordBatchStreamReader::Open(&reader));
+  ASSERT_OK(batch_reader->ReadNext(out));
+};
+
+TEST_F(TestExtensionType, FixedShapeTensorType) {

Review Comment:
   Is it possible to split this in some multiple tests instead of one long one?



##########
cpp/src/arrow/extension/fixed_shape_tensor_test.cc:
##########
@@ -0,0 +1,188 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/extension/fixed_shape_tensor.h"
+
+#include "arrow/testing/matchers.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/io/memory.h"
+#include "arrow/ipc/reader.h"
+#include "arrow/ipc/writer.h"
+#include "arrow/record_batch.h"
+#include "arrow/tensor.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+
+class TestExtensionType : public ::testing::Test {};
+
+auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch,
+                         std::shared_ptr<RecordBatch>* out) {
+  ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
+  ASSERT_OK(ipc::WriteRecordBatchStream({batch}, 
ipc::IpcWriteOptions::Defaults(),
+                                        out_stream.get()));
+
+  ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
+
+  io::BufferReader reader(complete_ipc_stream);
+  std::shared_ptr<RecordBatchReader> batch_reader;
+  ASSERT_OK_AND_ASSIGN(batch_reader, 
ipc::RecordBatchStreamReader::Open(&reader));
+  ASSERT_OK(batch_reader->ReadNext(out));
+};
+
+TEST_F(TestExtensionType, FixedShapeTensorType) {
+  using FixedShapeTensorType = extension::FixedShapeTensorType;
+  using arrow::extension::fixed_shape_tensor;
+
+  std::vector<int64_t> shape = {3, 3, 4};
+  std::vector<int64_t> cell_shape = {3, 4};
+  auto value_type = int64();
+  std::shared_ptr<DataType> cell_type = fixed_size_list(value_type, 12);
+
+  std::vector<std::string> dim_names = {"x", "y"};
+  std::vector<int64_t> strides = {96, 32, 8};
+  std::vector<int64_t> column_major_strides = {8, 24, 72};
+  std::vector<int64_t> neither_major_strides = {96, 8, 32};
+  std::vector<int64_t> cell_strides = {32, 8};
+  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};
+  std::string serialized = R"({"shape":[3,4],"dim_names":["x","y"]})";
+
+  ASSERT_OK_AND_ASSIGN(auto tensor,
+                       Tensor::Make(value_type, Buffer::Wrap(values), shape));
+  ASSERT_OK_AND_ASSIGN(
+      auto tensor_partial,
+      Tensor::Make(value_type, Buffer::Wrap(values_partial), shape_partial));
+
+  std::shared_ptr<ExtensionType> ext_type =
+      fixed_shape_tensor(value_type, cell_shape, {}, dim_names);
+  auto exact_ext_type = 
internal::checked_pointer_cast<FixedShapeTensorType>(ext_type);
+  ASSERT_OK_AND_ASSIGN(auto ds,
+                       ext_type->Deserialize(ext_type->storage_type(), 
serialized));
+  std::shared_ptr<ExtensionType> deserialized =
+      std::reinterpret_pointer_cast<ExtensionType>(ds);
+
+  ASSERT_TRUE(tensor->is_row_major());
+  ASSERT_EQ(tensor->strides(), strides);
+  ASSERT_EQ(tensor_partial->strides(), strides);
+
+  // Test ExtensionType methods
+  ASSERT_EQ(ext_type->extension_name(), "arrow.fixed_shape_tensor");
+  ASSERT_TRUE(ext_type->ExtensionEquals(*exact_ext_type));
+  ASSERT_TRUE(ext_type->storage_type()->Equals(*cell_type));
+  ASSERT_EQ(ext_type->Serialize(), serialized);
+  ASSERT_TRUE(deserialized->ExtensionEquals(*ext_type));

Review Comment:
   ```suggestion
     ASSERT_TRUE(deserialized->Equals(*ext_type));
   ```
   
   I _think_ public usage should prefer the generic Equals, so good to use that 
in the tests. And can you also add a case where Equals returns False for 
comparing with a different type?



##########
cpp/src/arrow/extension/fixed_shape_tensor_test.cc:
##########
@@ -0,0 +1,188 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/extension/fixed_shape_tensor.h"
+
+#include "arrow/testing/matchers.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/io/memory.h"
+#include "arrow/ipc/reader.h"
+#include "arrow/ipc/writer.h"
+#include "arrow/record_batch.h"
+#include "arrow/tensor.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+
+class TestExtensionType : public ::testing::Test {};
+
+auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch,
+                         std::shared_ptr<RecordBatch>* out) {
+  ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
+  ASSERT_OK(ipc::WriteRecordBatchStream({batch}, 
ipc::IpcWriteOptions::Defaults(),
+                                        out_stream.get()));
+
+  ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
+
+  io::BufferReader reader(complete_ipc_stream);
+  std::shared_ptr<RecordBatchReader> batch_reader;
+  ASSERT_OK_AND_ASSIGN(batch_reader, 
ipc::RecordBatchStreamReader::Open(&reader));
+  ASSERT_OK(batch_reader->ReadNext(out));
+};
+
+TEST_F(TestExtensionType, FixedShapeTensorType) {
+  using FixedShapeTensorType = extension::FixedShapeTensorType;
+  using arrow::extension::fixed_shape_tensor;
+
+  std::vector<int64_t> shape = {3, 3, 4};
+  std::vector<int64_t> cell_shape = {3, 4};
+  auto value_type = int64();
+  std::shared_ptr<DataType> cell_type = fixed_size_list(value_type, 12);
+
+  std::vector<std::string> dim_names = {"x", "y"};
+  std::vector<int64_t> strides = {96, 32, 8};
+  std::vector<int64_t> column_major_strides = {8, 24, 72};
+  std::vector<int64_t> neither_major_strides = {96, 8, 32};
+  std::vector<int64_t> cell_strides = {32, 8};
+  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};
+  std::string serialized = R"({"shape":[3,4],"dim_names":["x","y"]})";
+
+  ASSERT_OK_AND_ASSIGN(auto tensor,
+                       Tensor::Make(value_type, Buffer::Wrap(values), shape));
+  ASSERT_OK_AND_ASSIGN(
+      auto tensor_partial,
+      Tensor::Make(value_type, Buffer::Wrap(values_partial), shape_partial));
+
+  std::shared_ptr<ExtensionType> ext_type =
+      fixed_shape_tensor(value_type, cell_shape, {}, dim_names);
+  auto exact_ext_type = 
internal::checked_pointer_cast<FixedShapeTensorType>(ext_type);
+  ASSERT_OK_AND_ASSIGN(auto ds,
+                       ext_type->Deserialize(ext_type->storage_type(), 
serialized));
+  std::shared_ptr<ExtensionType> deserialized =
+      std::reinterpret_pointer_cast<ExtensionType>(ds);
+
+  ASSERT_TRUE(tensor->is_row_major());
+  ASSERT_EQ(tensor->strides(), strides);
+  ASSERT_EQ(tensor_partial->strides(), strides);
+
+  // Test ExtensionType methods
+  ASSERT_EQ(ext_type->extension_name(), "arrow.fixed_shape_tensor");
+  ASSERT_TRUE(ext_type->ExtensionEquals(*exact_ext_type));
+  ASSERT_TRUE(ext_type->storage_type()->Equals(*cell_type));
+  ASSERT_EQ(ext_type->Serialize(), serialized);
+  ASSERT_TRUE(deserialized->ExtensionEquals(*ext_type));
+  ASSERT_EQ(exact_ext_type->id(), Type::EXTENSION);
+
+  // Test FixedShapeTensorType methods
+  ASSERT_EQ(exact_ext_type->ndim(), cell_shape.size());
+  ASSERT_EQ(exact_ext_type->shape(), cell_shape);
+  ASSERT_EQ(exact_ext_type->strides(), cell_strides);
+  ASSERT_EQ(exact_ext_type->dim_names(), dim_names);
+
+  // Test MakeArray(std::shared_ptr<ArrayData> data)
+  std::vector<std::shared_ptr<Buffer>> buffers = {nullptr, 
Buffer::Wrap(values)};
+  auto arr_data = std::make_shared<ArrayData>(value_type, values.size(), 
buffers, 0, 0);
+  auto arr = std::make_shared<Int64Array>(arr_data);
+  EXPECT_OK_AND_ASSIGN(auto fsla_arr, FixedSizeListArray::FromArrays(arr, 
cell_type));
+  auto data = fsla_arr->data();
+  data->type = ext_type;
+  auto ext_arr = exact_ext_type->MakeArray(data);
+  ASSERT_EQ(ext_arr->length(), shape[0]);
+  ASSERT_EQ(ext_arr->null_count(), 0);
+
+  // Test MakeArray(std::shared_ptr<Tensor> tensor)

Review Comment:
   Also here would be good to test some failure cases



##########
cpp/src/arrow/extension/fixed_shape_tensor_test.cc:
##########
@@ -0,0 +1,188 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/extension/fixed_shape_tensor.h"
+
+#include "arrow/testing/matchers.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/io/memory.h"
+#include "arrow/ipc/reader.h"
+#include "arrow/ipc/writer.h"
+#include "arrow/record_batch.h"
+#include "arrow/tensor.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+
+class TestExtensionType : public ::testing::Test {};
+
+auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch,
+                         std::shared_ptr<RecordBatch>* out) {
+  ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
+  ASSERT_OK(ipc::WriteRecordBatchStream({batch}, 
ipc::IpcWriteOptions::Defaults(),
+                                        out_stream.get()));
+
+  ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
+
+  io::BufferReader reader(complete_ipc_stream);
+  std::shared_ptr<RecordBatchReader> batch_reader;
+  ASSERT_OK_AND_ASSIGN(batch_reader, 
ipc::RecordBatchStreamReader::Open(&reader));
+  ASSERT_OK(batch_reader->ReadNext(out));
+};
+
+TEST_F(TestExtensionType, FixedShapeTensorType) {
+  using FixedShapeTensorType = extension::FixedShapeTensorType;
+  using arrow::extension::fixed_shape_tensor;
+
+  std::vector<int64_t> shape = {3, 3, 4};
+  std::vector<int64_t> cell_shape = {3, 4};
+  auto value_type = int64();
+  std::shared_ptr<DataType> cell_type = fixed_size_list(value_type, 12);
+
+  std::vector<std::string> dim_names = {"x", "y"};
+  std::vector<int64_t> strides = {96, 32, 8};
+  std::vector<int64_t> column_major_strides = {8, 24, 72};
+  std::vector<int64_t> neither_major_strides = {96, 8, 32};
+  std::vector<int64_t> cell_strides = {32, 8};
+  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};
+  std::string serialized = R"({"shape":[3,4],"dim_names":["x","y"]})";
+
+  ASSERT_OK_AND_ASSIGN(auto tensor,
+                       Tensor::Make(value_type, Buffer::Wrap(values), shape));
+  ASSERT_OK_AND_ASSIGN(
+      auto tensor_partial,
+      Tensor::Make(value_type, Buffer::Wrap(values_partial), shape_partial));
+
+  std::shared_ptr<ExtensionType> ext_type =
+      fixed_shape_tensor(value_type, cell_shape, {}, dim_names);
+  auto exact_ext_type = 
internal::checked_pointer_cast<FixedShapeTensorType>(ext_type);
+  ASSERT_OK_AND_ASSIGN(auto ds,
+                       ext_type->Deserialize(ext_type->storage_type(), 
serialized));
+  std::shared_ptr<ExtensionType> deserialized =
+      std::reinterpret_pointer_cast<ExtensionType>(ds);
+
+  ASSERT_TRUE(tensor->is_row_major());
+  ASSERT_EQ(tensor->strides(), strides);
+  ASSERT_EQ(tensor_partial->strides(), strides);
+
+  // Test ExtensionType methods
+  ASSERT_EQ(ext_type->extension_name(), "arrow.fixed_shape_tensor");
+  ASSERT_TRUE(ext_type->ExtensionEquals(*exact_ext_type));
+  ASSERT_TRUE(ext_type->storage_type()->Equals(*cell_type));
+  ASSERT_EQ(ext_type->Serialize(), serialized);
+  ASSERT_TRUE(deserialized->ExtensionEquals(*ext_type));
+  ASSERT_EQ(exact_ext_type->id(), Type::EXTENSION);
+
+  // Test FixedShapeTensorType methods
+  ASSERT_EQ(exact_ext_type->ndim(), cell_shape.size());
+  ASSERT_EQ(exact_ext_type->shape(), cell_shape);
+  ASSERT_EQ(exact_ext_type->strides(), cell_strides);
+  ASSERT_EQ(exact_ext_type->dim_names(), dim_names);
+
+  // Test MakeArray(std::shared_ptr<ArrayData> data)
+  std::vector<std::shared_ptr<Buffer>> buffers = {nullptr, 
Buffer::Wrap(values)};
+  auto arr_data = std::make_shared<ArrayData>(value_type, values.size(), 
buffers, 0, 0);
+  auto arr = std::make_shared<Int64Array>(arr_data);
+  EXPECT_OK_AND_ASSIGN(auto fsla_arr, FixedSizeListArray::FromArrays(arr, 
cell_type));
+  auto data = fsla_arr->data();
+  data->type = ext_type;
+  auto ext_arr = exact_ext_type->MakeArray(data);
+  ASSERT_EQ(ext_arr->length(), shape[0]);
+  ASSERT_EQ(ext_arr->null_count(), 0);
+
+  // Test MakeArray(std::shared_ptr<Tensor> tensor)

Review Comment:
   Ah, I see below there is already a "Only first-major tensors .." check



##########
cpp/src/arrow/extension/fixed_shape_tensor_test.cc:
##########
@@ -0,0 +1,188 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "arrow/extension/fixed_shape_tensor.h"
+
+#include "arrow/testing/matchers.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/io/memory.h"
+#include "arrow/ipc/reader.h"
+#include "arrow/ipc/writer.h"
+#include "arrow/record_batch.h"
+#include "arrow/tensor.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+
+class TestExtensionType : public ::testing::Test {};
+
+auto RoundtripBatch = [](const std::shared_ptr<RecordBatch>& batch,
+                         std::shared_ptr<RecordBatch>* out) {
+  ASSERT_OK_AND_ASSIGN(auto out_stream, io::BufferOutputStream::Create());
+  ASSERT_OK(ipc::WriteRecordBatchStream({batch}, 
ipc::IpcWriteOptions::Defaults(),
+                                        out_stream.get()));
+
+  ASSERT_OK_AND_ASSIGN(auto complete_ipc_stream, out_stream->Finish());
+
+  io::BufferReader reader(complete_ipc_stream);
+  std::shared_ptr<RecordBatchReader> batch_reader;
+  ASSERT_OK_AND_ASSIGN(batch_reader, 
ipc::RecordBatchStreamReader::Open(&reader));
+  ASSERT_OK(batch_reader->ReadNext(out));
+};
+
+TEST_F(TestExtensionType, FixedShapeTensorType) {
+  using FixedShapeTensorType = extension::FixedShapeTensorType;
+  using arrow::extension::fixed_shape_tensor;
+
+  std::vector<int64_t> shape = {3, 3, 4};
+  std::vector<int64_t> cell_shape = {3, 4};
+  auto value_type = int64();
+  std::shared_ptr<DataType> cell_type = fixed_size_list(value_type, 12);
+
+  std::vector<std::string> dim_names = {"x", "y"};
+  std::vector<int64_t> strides = {96, 32, 8};
+  std::vector<int64_t> column_major_strides = {8, 24, 72};
+  std::vector<int64_t> neither_major_strides = {96, 8, 32};
+  std::vector<int64_t> cell_strides = {32, 8};
+  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};
+  std::string serialized = R"({"shape":[3,4],"dim_names":["x","y"]})";
+
+  ASSERT_OK_AND_ASSIGN(auto tensor,
+                       Tensor::Make(value_type, Buffer::Wrap(values), shape));
+  ASSERT_OK_AND_ASSIGN(
+      auto tensor_partial,
+      Tensor::Make(value_type, Buffer::Wrap(values_partial), shape_partial));
+
+  std::shared_ptr<ExtensionType> ext_type =
+      fixed_shape_tensor(value_type, cell_shape, {}, dim_names);
+  auto exact_ext_type = 
internal::checked_pointer_cast<FixedShapeTensorType>(ext_type);
+  ASSERT_OK_AND_ASSIGN(auto ds,
+                       ext_type->Deserialize(ext_type->storage_type(), 
serialized));

Review Comment:
   Can you also add a test where passing a wrong type / wrong metadata to 
Deserialize? 



-- 
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