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


##########
cpp/src/arrow/compute/kernels/scalar_cast_string.cc:
##########
@@ -304,8 +305,21 @@ BinaryToBinaryCastExec(KernelContext* ctx, const ExecSpan& 
batch, ExecResult* ou
     }
   }
 
-  // Start with a zero-copy cast, but change indices to expected size
-  RETURN_NOT_OK(ZeroCopyCastExec(ctx, batch, out));
+  std::shared_ptr<ArrayData> input_arr = input.ToArrayData();
+  ArrayData* output = out->array_data().get();
+  output->length = input_arr->length;
+  output->SetNullCount(input_arr->null_count);
+  output->buffers = std::move(input_arr->buffers);
+  output->child_data = std::move(input_arr->child_data);
+
+  if (output->buffers[0]) {
+    // If reusing the null bitmap, ensure offset into the first byte is the 
same as input.
+    output->offset = input_arr->offset % 8;
+    output->buffers[0] = SliceBuffer(output->buffers[0], input_arr->offset / 
8);
+  } else {
+    output->offset = 0;
+  }

Review Comment:
   I believe you should also slice the offsets buffer when you change the array 
offset.
   
   ```suggestion
     // slice buffers to reduce allocation when casting the offsets buffer
     auto length = input_arr->length;
     int64_t buffer_offset = 0;
     if (output->null_count != 0 && output->buffers[0]) {
       // avoiding reallocation of the validity buffer by allowing some padding 
bits
       output->offset = input_arr->offset % 8;
       buffer_offset = input_arr->offset / 8;
     } else {
       output->offset = 0;
       buffer_offset = input_arr->offset;
     }
     output->buffers[0] = SliceBuffer(output->buffers[0], buffer_offset, 
length);
     output->buffers[1] = SliceBuffer(output->buffers[1], buffer_offset, 
length);
   ```



##########
cpp/src/arrow/compute/kernels/scalar_cast_string.cc:
##########
@@ -304,8 +305,21 @@ BinaryToBinaryCastExec(KernelContext* ctx, const ExecSpan& 
batch, ExecResult* ou
     }
   }
 
-  // Start with a zero-copy cast, but change indices to expected size
-  RETURN_NOT_OK(ZeroCopyCastExec(ctx, batch, out));
+  std::shared_ptr<ArrayData> input_arr = input.ToArrayData();
+  ArrayData* output = out->array_data().get();
+  output->length = input_arr->length;
+  output->SetNullCount(input_arr->null_count);
+  output->buffers = std::move(input_arr->buffers);
+  output->child_data = std::move(input_arr->child_data);
+
+  if (output->buffers[0]) {
+    // If reusing the null bitmap, ensure offset into the first byte is the 
same as input.
+    output->offset = input_arr->offset % 8;
+    output->buffers[0] = SliceBuffer(output->buffers[0], input_arr->offset / 
8);
+  } else {
+    output->offset = 0;
+  }

Review Comment:
   Please review my suggestions and tell if something is weird. This stuff is 
hard.



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