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


##########
cpp/src/arrow/extension/tensor_array.cc:
##########
@@ -0,0 +1,103 @@
+// 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 "arrow/tensor.h"
+
+namespace arrow {
+namespace extension {
+
+static std::string get_name(const std::shared_ptr<DataType>& type,
+                            const std::vector<int64_t>& shape,
+                            const std::vector<int64_t>& strides) {
+  std::stringstream s;
+  s << "arrow.extension.tensor<type=" << *type << ", shape=(";
+  for (uint64_t i = 0; i < shape.size(); i++) {
+    s << shape[i];
+    if (i < shape.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << "), strides=(";
+  for (uint64_t i = 0; i < strides.size(); i++) {
+    s << strides[i];
+    if (i < strides.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << ")>";
+  return s.str();
+}
+
+std::string TensorArrayType::extension_name() const {
+  return get_name(type(), shape(), strides());
+}
+
+const char* TensorArrayType::type_name() const {
+  std::string extension_name = get_name(type(), shape(), strides());
+  return extension_name.c_str();
+}
+
+std::shared_ptr<ExtensionType> tensor_array(const std::shared_ptr<DataType>& 
type,
+                                            const std::vector<int64_t>& shape,
+                                            const std::vector<int64_t>& 
strides) {
+  std::string type_name = get_name(type, shape, strides);
+  auto ext_type = GetExtensionType(type_name);
+  if (!ext_type) {
+    DCHECK_OK(
+        RegisterExtensionType(std::make_shared<TensorArrayType>(type, shape, 
strides)));
+  }
+  return GetExtensionType(type_name);
+}
+
+bool TensorArrayType::ExtensionEquals(const ExtensionType& other) const {
+  const auto& other_ext = static_cast<const TensorArrayType&>(other);
+  bool equals = storage_type() == other_ext.storage_type();

Review Comment:
   Is this by-pointer equality?



##########
cpp/src/arrow/extension/tensor_array.cc:
##########
@@ -0,0 +1,103 @@
+// 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 "arrow/tensor.h"
+
+namespace arrow {
+namespace extension {
+
+static std::string get_name(const std::shared_ptr<DataType>& type,
+                            const std::vector<int64_t>& shape,
+                            const std::vector<int64_t>& strides) {
+  std::stringstream s;
+  s << "arrow.extension.tensor<type=" << *type << ", shape=(";
+  for (uint64_t i = 0; i < shape.size(); i++) {
+    s << shape[i];
+    if (i < shape.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << "), strides=(";
+  for (uint64_t i = 0; i < strides.size(); i++) {
+    s << strides[i];
+    if (i < strides.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << ")>";
+  return s.str();
+}
+
+std::string TensorArrayType::extension_name() const {

Review Comment:
   Extension name should be the same for all instances of `TensorArrayType`.
   (the reason it can't be a `static` method is that it has to be `virtual`; 
unfortunately C++ doesn't allow both)



##########
cpp/src/arrow/extension/tensor_array.h:
##########
@@ -0,0 +1,75 @@
+// 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 <sstream>
+
+#include "arrow/extension_type.h"
+#include "arrow/util/logging.h"

Review Comment:
   `logging.h` should not appear in public API header.
   (you should get a lint error for this?)



##########
cpp/src/arrow/extension/tensor_array.h:
##########
@@ -0,0 +1,75 @@
+// 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 <sstream>
+
+#include "arrow/extension_type.h"
+#include "arrow/util/logging.h"
+
+namespace arrow {
+
+namespace extension {
+
+class ARROW_EXPORT TensorArray : public ExtensionArray {
+ public:
+  using ExtensionArray::ExtensionArray;
+};
+
+/// \brief Concrete type class for constant-size Tensor data.
+class ARROW_EXPORT TensorArrayType : public ExtensionType {
+ public:
+  TensorArrayType(const std::shared_ptr<DataType>& type,
+                  const std::vector<int64_t>& shape, const 
std::vector<int64_t>& strides)
+      : ExtensionType(type), type_(type), shape_(shape), strides_(strides) {}

Review Comment:
   Where is the storage type initialized? Is `type` the value type, or 
something else?



##########
cpp/src/arrow/extension/tensor_array.cc:
##########
@@ -0,0 +1,103 @@
+// 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 "arrow/tensor.h"
+
+namespace arrow {
+namespace extension {
+
+static std::string get_name(const std::shared_ptr<DataType>& type,
+                            const std::vector<int64_t>& shape,
+                            const std::vector<int64_t>& strides) {
+  std::stringstream s;
+  s << "arrow.extension.tensor<type=" << *type << ", shape=(";
+  for (uint64_t i = 0; i < shape.size(); i++) {
+    s << shape[i];
+    if (i < shape.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << "), strides=(";
+  for (uint64_t i = 0; i < strides.size(); i++) {
+    s << strides[i];
+    if (i < strides.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << ")>";
+  return s.str();
+}
+
+std::string TensorArrayType::extension_name() const {
+  return get_name(type(), shape(), strides());
+}
+
+const char* TensorArrayType::type_name() const {
+  std::string extension_name = get_name(type(), shape(), strides());
+  return extension_name.c_str();
+}
+
+std::shared_ptr<ExtensionType> tensor_array(const std::shared_ptr<DataType>& 
type,
+                                            const std::vector<int64_t>& shape,
+                                            const std::vector<int64_t>& 
strides) {
+  std::string type_name = get_name(type, shape, strides);
+  auto ext_type = GetExtensionType(type_name);
+  if (!ext_type) {
+    DCHECK_OK(
+        RegisterExtensionType(std::make_shared<TensorArrayType>(type, shape, 
strides)));
+  }
+  return GetExtensionType(type_name);
+}
+
+bool TensorArrayType::ExtensionEquals(const ExtensionType& other) const {
+  const auto& other_ext = static_cast<const TensorArrayType&>(other);

Review Comment:
   You can't do that without first checking that `other` has the right 
extension type. This is what `extension_name()` is for.



##########
cpp/src/arrow/extension/tensor_array.cc:
##########
@@ -0,0 +1,103 @@
+// 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 "arrow/tensor.h"
+
+namespace arrow {
+namespace extension {
+
+static std::string get_name(const std::shared_ptr<DataType>& type,
+                            const std::vector<int64_t>& shape,
+                            const std::vector<int64_t>& strides) {
+  std::stringstream s;
+  s << "arrow.extension.tensor<type=" << *type << ", shape=(";
+  for (uint64_t i = 0; i < shape.size(); i++) {
+    s << shape[i];
+    if (i < shape.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << "), strides=(";
+  for (uint64_t i = 0; i < strides.size(); i++) {
+    s << strides[i];
+    if (i < strides.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << ")>";
+  return s.str();
+}
+
+std::string TensorArrayType::extension_name() const {
+  return get_name(type(), shape(), strides());
+}
+
+const char* TensorArrayType::type_name() const {
+  std::string extension_name = get_name(type(), shape(), strides());
+  return extension_name.c_str();

Review Comment:
   Why this????



##########
cpp/src/arrow/extension/tensor_array.cc:
##########
@@ -0,0 +1,103 @@
+// 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 "arrow/tensor.h"
+
+namespace arrow {
+namespace extension {
+
+static std::string get_name(const std::shared_ptr<DataType>& type,
+                            const std::vector<int64_t>& shape,
+                            const std::vector<int64_t>& strides) {
+  std::stringstream s;
+  s << "arrow.extension.tensor<type=" << *type << ", shape=(";
+  for (uint64_t i = 0; i < shape.size(); i++) {
+    s << shape[i];
+    if (i < shape.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << "), strides=(";
+  for (uint64_t i = 0; i < strides.size(); i++) {
+    s << strides[i];
+    if (i < strides.size() - 1) {
+      s << ", ";
+    }
+  }
+  s << ")>";
+  return s.str();
+}
+
+std::string TensorArrayType::extension_name() const {
+  return get_name(type(), shape(), strides());
+}
+
+const char* TensorArrayType::type_name() const {
+  std::string extension_name = get_name(type(), shape(), strides());
+  return extension_name.c_str();
+}
+
+std::shared_ptr<ExtensionType> tensor_array(const std::shared_ptr<DataType>& 
type,
+                                            const std::vector<int64_t>& shape,
+                                            const std::vector<int64_t>& 
strides) {
+  std::string type_name = get_name(type, shape, strides);
+  auto ext_type = GetExtensionType(type_name);
+  if (!ext_type) {
+    DCHECK_OK(
+        RegisterExtensionType(std::make_shared<TensorArrayType>(type, shape, 
strides)));
+  }
+  return GetExtensionType(type_name);
+}
+
+bool TensorArrayType::ExtensionEquals(const ExtensionType& other) const {
+  const auto& other_ext = static_cast<const TensorArrayType&>(other);
+  bool equals = storage_type() == other_ext.storage_type();
+  equals &= shape() == other_ext.shape();
+  equals &= strides() == other_ext.strides();
+  return equals;
+}
+
+std::string TensorArrayType::Serialize() const { return extension_name(); }

Review Comment:
   This doesn't make sense. The extension name is already part of standard 
metadata. The serialization only needs to convey information that's not in the 
name (i.e. parameters).



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