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


##########
cpp/src/arrow/util/align_util.cc:
##########
@@ -30,12 +32,120 @@ 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 {
+
+// Some buffers are frequently type-punned.  For example, in an int32 array the
+// values buffer is frequently cast to int32_t*
+//
+// This sort of punning is only valid if the pointer is aligned to a proper 
width
+// (e.g. 4 bytes in the case of int32).
+//
+// We generally assume that all buffers are at least 8-bit aligned and so we 
only
+// need to worry about buffers that are commonly cast to wider data types.  
Note that
+// this alignment is something that is guaranteed by malloc (e.g. new 
int32_t[] will
+// return a buffer that is 4 byte aligned) or common libraries (e.g. numpy) 
but it is
+// not currently guaranteed by flight (GH-32276).
+//
+// By happy coincedence, for every data type, the only buffer that might need 
wider
+// alignment is the second buffer (at index 1).  This function returns the 
expected
+// alignment (in bits) of the second buffer for the given array to safely 
allow this cast.
+//
+// If the array's type doesn't have a second buffer or the second buffer is 
not expected
+// to be type punned, then we return 8.
+int GetMallocValuesAlignment(const ArrayData& array) {

Review Comment:
   Done, except I return `1` if `buffer_index > 1` also (e.g. binary arrays)



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