felipecrv commented on code in PR #36489:
URL: https://github.com/apache/arrow/pull/36489#discussion_r1254875245


##########
cpp/src/arrow/c/bridge.cc:
##########
@@ -663,6 +672,115 @@ Status ExportRecordBatch(const RecordBatch& batch, struct 
ArrowArray* out,
   return Status::OK();
 }
 
+//////////////////////////////////////////////////////////////////////////
+// C device arrays
+
+Result<std::pair<DeviceType, int64_t>> validate_device_info(const ArrayData& 
data) {
+  DeviceType device_type = DeviceType::UNKNOWN;
+  int64_t device_id = -1;
+
+  for (const auto& buf : data.buffers) {
+    if (!buf) {
+      // some buffers might be null
+      // for example, the null bitmap is optional if there's no nulls
+      continue;
+    }
+
+    if (device_type == DeviceType::UNKNOWN) {
+      device_type = buf->device_type();
+      device_id = buf->device()->device_id();
+      continue;
+    }
+
+    if (buf->device_type() != device_type) {
+      return Status::Invalid(
+          "exporting device array with buffers on more than one device.");
+    }
+
+    if (buf->device()->device_id() != device_id) {
+      return Status::Invalid(
+          "exporting device array with buffers on multiple device ids.");
+    }
+  }
+
+  // recursively check the children
+  auto info = std::make_pair(device_type, device_id);

Review Comment:
   I think the pattern I suggest means you don't have to rely on any assumption.
   
   You can wrap the auxiliary recursive function for ease of use. This pattern 
is common in functional programming when turning a recursive function into a 
tail-call optimized one: create aux function that takes accumulator, return 
accumulator when done, wrap aux in a simple interface.
   
   ```cpp
   Status validate_device_info(const ArrayData& data, DeviceType *device_type, 
int64_t *device_id);
   
   Result<pair> validade_device_info(concst ArrayData& data) {
     DeviceType device_type;
     int64_t device_id;
     RETURN_NOT_OK(validate_device_info(data, &device_type, &device_id));
     return {device_type, device_id};
   }
   ```
   



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