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


##########
cpp/src/arrow/extension/tensor_array.cc:
##########
@@ -0,0 +1,292 @@
+// 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/tensor_array.h"
+
+#include <unordered_map>
+
+#include "arrow/array/array_nested.h"
+#include "arrow/array/array_primitive.h"
+#include "arrow/extension_type.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/rapidjson.h>
+#include <rapidjson/stringbuffer.h>
+#include <rapidjson/writer.h>
+#include "rapidjson/error/en.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_; }
+
+std::vector<int64_t> FixedShapeTensorType::strides() const {
+  std::vector<int64_t> strides;
+  const auto& value_type = internal::checked_cast<const 
FixedWidthType&>(*value_type_);
+  DCHECK_OK(internal::ComputeRowMajorStrides(value_type, shape_, &strides));
+  internal::Permute(permutation_, &strides);
+  return strides;
+}
+
+std::vector<int64_t> FixedShapeTensorType::permutation() const { return 
permutation_; }
+
+std::vector<std::string> FixedShapeTensorType::dim_names() const { return 
dim_names_; }
+
+std::string FixedShapeTensorType::metadata() const { return metadata_; }
+
+bool FixedShapeTensorType::ExtensionEquals(const ExtensionType& other) const {
+  if (extension_name() != other.extension_name()) {
+    return false;
+  }
+  const auto& other_ext = static_cast<const FixedShapeTensorType&>(other);
+  bool equals = storage_type()->Equals(other_ext.storage_type());
+  equals &= shape_ == other_ext.shape();
+  equals &= permutation_ == other_ext.permutation();
+  equals &= dim_names_ == other_ext.dim_names();
+  equals &= metadata_ == other_ext.metadata();
+  return equals;
+}
+
+std::string FixedShapeTensorType::Serialize() const {
+  rj::Document document;
+  document.SetObject();
+  rj::Document::AllocatorType& allocator = document.GetAllocator();
+
+  rj::Value shape(rj::kArrayType);
+  for (auto v : shape_) {
+    shape.PushBack(v, allocator);
+  }
+  document.AddMember(rj::Value("shape", allocator), shape, allocator);
+
+  if (!permutation_.empty()) {
+    rj::Value permutation(rj::kArrayType);
+    for (auto v : permutation_) {
+      permutation.PushBack(v, allocator);
+    }
+    document.AddMember(rj::Value("permutation", allocator), permutation, 
allocator);
+  }
+
+  if (!dim_names_.empty()) {
+    rj::Value dim_names(rj::kArrayType);
+    for (std::string v : dim_names_) {
+      dim_names.PushBack(rj::Value{}.SetString(v.c_str(), allocator), 
allocator);
+    }
+    document.AddMember(rj::Value("dim_names", allocator), dim_names, 
allocator);
+  }
+
+  if (!metadata_.empty()) {

Review Comment:
   Oh, I missed that, I assumed general metadata couldn't be used. That makes 
sense yeah, I'll remove it from `Serialize`/`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