aucahuasi commented on a change in pull request #11159:
URL: https://github.com/apache/arrow/pull/11159#discussion_r708776410
##########
File path: cpp/src/arrow/compute/kernels/vector_nested.cc
##########
@@ -157,6 +157,43 @@ class ListParentIndicesFunction : public MetaFunction {
}
};
+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"});
+
+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;
+ ScalarVector scalars;
+ for (int i = 0; i < list_array.length(); ++i) {
+ if (list_array.IsNull(i)) {
+ scalars.push_back(MakeNullScalar(list_array.value_type()));
+ continue;
+ }
+ std::shared_ptr<arrow::Array> value_array = list_array.value_slice(i);
+ auto len = value_array->length();
+ if (ARROW_PREDICT_FALSE(index < 0 || 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)));
+ scalars.push_back(scalar);
+ }
+ std::unique_ptr<ArrayBuilder> builder;
+ RETURN_NOT_OK(MakeBuilder(ctx->memory_pool(), list_array.value_type(),
&builder));
+ RETURN_NOT_OK(builder->AppendScalars(scalars));
Review comment:
Thanks again!
--
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]