Skip to content

Commit a6d9955

Browse files
committed
update code by self-review
1 parent 2e72e87 commit a6d9955

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

cpp/src/arrow/compute/kernels/scalar_cast_string.cc

+22-19
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,30 @@ void AddBinaryToFixedSizeBinaryCast(CastFunction* func) {
510510
AddBinaryToFixedSizeBinaryCast<FixedSizeBinaryType>(func);
511511
}
512512

513+
// ----------------------------------------------------------------------
514+
// List-like (List, LargeList, ListView, LargeListView, FixedSizeList, Map) to string
515+
513516
template <typename O, typename I>
514517
struct ListLikeToStringCastFunctor {
515518
using BuilderType = typename TypeTraits<O>::BuilderType;
516519

520+
static Status AppendValue(const ArraySpan& values, std::stringstream& ss, int64_t j, int64_t start) {
521+
if (j != start) {
522+
ss << ", ";
523+
}
524+
if (values.IsValid(j)) {
525+
std::shared_ptr<Scalar> value_scalar;
526+
RETURN_NOT_OK(values.ToArray()->GetScalar(j).Value(&value_scalar));
527+
ss << value_scalar->ToString();
528+
} else {
529+
ss << "null";
530+
}
531+
return Status::OK();
532+
}
533+
517534
static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
518535
const ArraySpan& input = batch[0].array;
519-
536+
auto type_id = input.type->id();
520537
BuilderType builder(ctx->memory_pool());
521538
RETURN_NOT_OK(builder.Reserve(input.length));
522539

@@ -525,7 +542,7 @@ struct ListLikeToStringCastFunctor {
525542
const auto* offsets = input.GetValues<typename I::offset_type>(1);
526543

527544
int list_size = -1;
528-
if (input.type->id() == Type::FIXED_SIZE_LIST) {
545+
if (type_id == Type::FIXED_SIZE_LIST) {
529546
list_size = checked_cast<const FixedSizeListType&>(*input.type).list_size();
530547
}
531548

@@ -535,34 +552,20 @@ struct ListLikeToStringCastFunctor {
535552
continue;
536553
}
537554

538-
std::ostringstream ss;
555+
std::stringstream ss;
539556
ss << type_info << "[";
540557

541558
int64_t start, end;
542-
if (input.type->id() == Type::FIXED_SIZE_LIST) {
559+
if (type_id == Type::FIXED_SIZE_LIST) {
543560
start = i * list_size;
544561
end = start + list_size;
545562
} else {
546563
start = offsets[i];
547564
end = offsets[i + 1];
548565
}
549566

550-
auto append_value = [&](int64_t j) -> Status {
551-
if (j != start) {
552-
ss << ", ";
553-
}
554-
if (values.IsValid(j)) {
555-
std::shared_ptr<Scalar> value_scalar;
556-
RETURN_NOT_OK(values.ToArray()->GetScalar(j).Value(&value_scalar));
557-
ss << value_scalar->ToString();
558-
} else {
559-
ss << "null";
560-
}
561-
return Status::OK();
562-
};
563-
564567
for (int64_t j = start; j < end; ++j) {
565-
RETURN_NOT_OK(append_value(j));
568+
RETURN_NOT_OK(AppendValue(values, ss, j, start));
566569
}
567570

568571
ss << "]";

0 commit comments

Comments
 (0)