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


##########
cpp/gdb_arrow.py:
##########
@@ -769,7 +772,8 @@ def __getitem__(self, index):
     def from_buffer(cls, buf, offset, length):
         assert isinstance(buf, Buffer)
         byte_offset, bit_offset = divmod(offset, 8)
-        byte_length = math.ceil(length + offset / 8) - byte_offset
+        # E.g. offset=3, length=6 selects bits 3..8 and needs 2 bytes.
+        byte_length = math.ceil((bit_offset + length) / 8)

Review Comment:
   I see this fixes a bug, can you add a test that exercises it?



##########
cpp/gdb_arrow.py:
##########
@@ -1831,6 +1862,40 @@ def children(self):
                 yield self._null_child(i)
 
 
+class RunEndEncodedArrayDataPrinter(ArrayDataPrinter):
+    """
+    ArrayDataPrinter specialization for run-end encoded arrays.
+    """
+
+    def __init__(self, name, val):
+        if self.length == 0:
+            return
+        child_data = StdVector(self.val['child_data'])
+        self._run_ends_printer = ArrayDataPrinter(
+            "arrow::ArrayData", deref(child_data[0]))
+        self._values_printer = ArrayDataPrinter(
+            "arrow::ArrayData", deref(child_data[1]))
+
+    def display_hint(self):
+        return "array"
+
+    def children(self):
+        if self.length == 0:
+            return
+        run_ends = self._run_ends_printer._unpacked_buffer_values(
+            1, self._run_ends_printer.type_id)
+        values = iter(self._values_printer.children() or ())
+        physical_index, value = 0, None
+        for i in range(self.length):
+            target = bisect_right(run_ends, self.offset + i)
+            while physical_index <= target:
+                value = next(values, None)
+                if value is None:
+                    return
+                physical_index += 1
+            yield self._valid_child(i, value[1])

Review Comment:
   Bisecting each logical value is inefficient while we can just loop over 
physical values and run ends in order. Something like:
   ```suggestion
           run_start = run_ends[self.offset - 1] if self.offset > 0 else 0
           for i in range(self.length):
               run_end = run_ends[self.offset + i]
               value = next(values, None)
               if value is None:
                   return
               for logical_index in range(run_start, run_end):
                   yield self._valid_child(logical_index, value[1])
               run_start = run_end
   ```



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