edponce commented on a change in pull request #11159:
URL: https://github.com/apache/arrow/pull/11159#discussion_r709801049



##########
File path: cpp/src/arrow/compute/kernels/scalar_nested.cc
##########
@@ -62,6 +62,57 @@ const FunctionDoc list_value_length_doc{
      "Null values emit a null in the output."),
     {"lists"}};
 
+template <typename Type, typename IndexType>
+Status ListElement(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
+  using ListArrayType = typename TypeTraits<Type>::ArrayType;
+  using IndexScalarType = typename TypeTraits<IndexType>::ScalarType;
+  ListArrayType list_array(batch[0].array());
+  const auto& index_scalar = batch[1].scalar_as<IndexScalarType>();
+  auto index = index_scalar.value;
+  if (ARROW_PREDICT_FALSE(index < 0)) {
+    return Status::Invalid("Index ", index,
+                           " is out of bounds: should be greater than or equal 
to 0");
+  }
+  std::unique_ptr<ArrayBuilder> builder;
+  RETURN_NOT_OK(MakeBuilder(ctx->memory_pool(), list_array.value_type(), 
&builder));
+  for (int i = 0; i < list_array.length(); ++i) {
+    if (list_array.IsNull(i)) {
+      RETURN_NOT_OK(builder->AppendNull());
+      continue;
+    }
+    std::shared_ptr<arrow::Array> value_array = list_array.value_slice(i);
+    auto len = value_array->length();
+    if (ARROW_PREDICT_FALSE(index > len)) {
+      return Status::Invalid("Index ", index, " is out of bounds: should be in 
[0, ", len,
+                             "]");
+    }
+    ARROW_ASSIGN_OR_RAISE(auto scalar, (value_array->GetScalar(index)));
+    RETURN_NOT_OK(builder->AppendScalar(*scalar));
+  }
+  ARROW_ASSIGN_OR_RAISE(auto result, builder->Finish());
+  out->value = result->data();
+  return Status::OK();
+}
+
+template <typename InListType, typename IndexType>
+void AddListElement(ScalarFunction* func) {
+  ScalarKernel 
kernel{KernelSignature::Make({InputType::Array(InListType::type_id),
+                                             
InputType::Scalar(IndexType::type_id)},
+                                            OutputType{ListValuesType},
+                                            /*is_varargs=*/false),
+                      ListElement<InListType, IndexType>};
+  kernel.null_handling = NullHandling::COMPUTED_NO_PREALLOCATE;
+  kernel.mem_allocation = MemAllocation::NO_PREALLOCATE;
+  DCHECK_OK(func->AddKernel(std::move(kernel)));
+}
+
+const FunctionDoc list_element_doc(
+    "Compute elements using of nested list values using an index",
+    ("`lists` must have a list-like type.\n"
+     "For each value in each list of `lists`, the element at `index`\n"
+     "is emitted."),
+    {"lists", "index"});

Review comment:
       In the long description, you can add a note about `null` behavior: "Null 
values emit a null in the output."




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