HuaHuaY commented on code in PR #50976:
URL: https://github.com/apache/arrow/pull/50976#discussion_r3861148942


##########
cpp/src/arrow/compute/kernels/scalar_cast_test.cc:
##########
@@ -3646,6 +3646,72 @@ TEST(Cast, ListToListOptionsPassthru) {
   }
 }
 
+TEST(Cast, ListViewToList) {

Review Comment:
   Is this an AI response? Why isn't the issue description updated?



##########
cpp/src/arrow/compute/kernels/scalar_cast_nested.cc:
##########
@@ -141,6 +142,136 @@ void AddListCast(CastFunction* func) {
   DCHECK_OK(func->AddKernel(SrcType::type_id, std::move(kernel)));
 }
 
+template <typename SrcType, typename DestType>
+struct CastListView {
+  using src_offset_type = typename SrcType::offset_type;
+  using dest_offset_type = typename DestType::offset_type;
+
+  static constexpr bool is_downcast = sizeof(src_offset_type) > 
sizeof(dest_offset_type);
+
+  static bool IsContiguous(const ArraySpan& in_array) {
+    if (in_array.length == 0) return true;
+    const auto* offsets = in_array.GetValues<src_offset_type>(1);
+    const auto* sizes = in_array.GetValues<src_offset_type>(2);
+    for (int64_t i = 0; i < in_array.length - 1; ++i) {
+      if (offsets[i] + sizes[i] != offsets[i + 1]) {
+        return false;
+      }
+    }
+    return true;
+  }
+
+  static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* 
out) {
+    const CastOptions& options = CastState::Get(ctx);
+    auto child_type = checked_cast<const DestType&>(*out->type()).value_type();
+    const ArraySpan& in_array = batch[0].array;
+    ArrayData* out_array = out->array_data().get();
+
+    ARROW_ASSIGN_OR_RAISE(out_array->buffers[0],
+                          GetOrCopyNullBitmapBuffer(in_array, 
ctx->memory_pool()));
+
+    std::shared_ptr<ArrayData> values = in_array.child_data[0].ToArrayData();
+
+    const auto* offsets = in_array.GetValues<src_offset_type>(1);
+    const auto* sizes = in_array.GetValues<src_offset_type>(2);
+
+    if (IsContiguous(in_array)) {
+      // Zero-copy fast-path: shift offsets and slice child values
+      ARROW_ASSIGN_OR_RAISE(
+          out_array->buffers[1],
+          ctx->Allocate(sizeof(dest_offset_type) * (in_array.length + 1)));
+      auto* dest_offsets = out_array->GetMutableValues<dest_offset_type>(1);
+
+      src_offset_type start_offset = in_array.length > 0 ? offsets[0] : 0;
+      for (int64_t i = 0; i < in_array.length; ++i) {
+        dest_offsets[i] = static_cast<dest_offset_type>(offsets[i] - 
start_offset);
+      }
+      if (in_array.length > 0) {
+        dest_offsets[in_array.length] = static_cast<dest_offset_type>(
+            offsets[in_array.length - 1] + sizes[in_array.length - 1] - 
start_offset);
+      } else {
+        dest_offsets[0] = 0;
+      }
+
+      if (is_downcast && in_array.length > 0) {
+        if (dest_offsets[in_array.length] >
+            std::numeric_limits<dest_offset_type>::max()) {

Review Comment:
   Due to the `static_cast<dest_offset_type>` at line 190, I think the 
condition of this if will never be true. We should check the condition first.



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