westonpace commented on code in PR #35565:
URL: https://github.com/apache/arrow/pull/35565#discussion_r1205261311


##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -94,12 +135,17 @@ bool CheckAlignment(const Table& table, int64_t alignment,
   return all_aligned;
 }
 
+// Most allocators require a minimum of 8-byte alignment.
+constexpr int64_t kMinimumAlignment = 8;
+
 Result<std::shared_ptr<Buffer>> EnsureAlignment(std::shared_ptr<Buffer> buffer,
                                                 int64_t alignment,
                                                 MemoryPool* memory_pool) {

Review Comment:
   I added a check and still want to add a test.



##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -30,12 +33,50 @@ bool CheckAlignment(const Buffer& buffer, int64_t 
alignment) {
   return buffer.address() % alignment == 0;
 }
 
-bool CheckAlignment(const ArrayData& array, int64_t alignment) {
-  for (const auto& buffer : array.buffers) {
-    if (buffer) {
-      if (!CheckAlignment(*buffer, alignment)) return false;
+namespace {
+
+// Returns the type that controls how the buffers of this ArrayData (not its 
children)
+// should behave
+Type::type GetTypeForBuffers(const ArrayData& array) {
+  Type::type type_id = array.type->storage_id();
+  if (type_id == Type::DICTIONARY) {
+    return ::arrow::internal::checked_pointer_cast<DictionaryType>(array.type)
+        ->index_type()
+        ->id();
+  }
+  return type_id;
+}
+
+// Checks to see if an array's own buffers are aligned but doesn't check
+// children
+bool CheckSelfAlignment(const ArrayData& array, int64_t alignment) {
+  if (alignment == kValueAlignment) {
+    Type::type type_id = GetTypeForBuffers(array);
+    for (std::size_t i = 0; i < array.buffers.size(); i++) {
+      if (array.buffers[i]) {
+        int expected_alignment =
+            RequiredValueAlignmentForBuffer(type_id, static_cast<int>(i));
+        if (!CheckAlignment(*array.buffers[i], expected_alignment)) {
+          return false;
+        }
+      }
+    }
+  } else {
+    for (const auto& buffer : array.buffers) {
+      if (buffer) {
+        if (!CheckAlignment(*buffer, alignment)) return false;
+      }
     }
   }
+  return true;
+}
+
+}  // namespace
+
+bool CheckAlignment(const ArrayData& array, int64_t alignment) {
+  if (!CheckSelfAlignment(array, alignment)) {
+    return false;
+  }
 
   if (array.type->id() == Type::DICTIONARY) {

Review Comment:
   I changed this to `if (array.dictionary)`



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