MCPcopy Create free account
hub / github.com/apache/arrow / Finalize

Method Finalize

cpp/src/arrow/compute/kernels/hash_aggregate.cc:1483–1538  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1481
1482struct GroupedDistinctImpl : public GroupedCountDistinctImpl {
1483 Result<Datum> Finalize() override {
1484 ARROW_ASSIGN_OR_RAISE(auto uniques, grouper_->GetUniques());
1485 ARROW_ASSIGN_OR_RAISE(
1486 auto groupings, Grouper::MakeGroupings(*uniques[1].array_as<UInt32Array>(),
1487 static_cast<uint32_t>(num_groups_), ctx_));
1488 ARROW_ASSIGN_OR_RAISE(
1489 auto list, Grouper::ApplyGroupings(*groupings, *uniques[0].make_array(), ctx_));
1490 const auto& values = list->values();
1491 DCHECK_EQ(values->offset(), 0);
1492 auto* offsets = list->value_offsets()->mutable_data_as<int32_t>();
1493 if (options_.mode == CountOptions::ALL ||
1494 (options_.mode == CountOptions::ONLY_VALID && values->null_count() == 0)) {
1495 return list;
1496 } else if (options_.mode == CountOptions::ONLY_VALID) {
1497 int32_t prev_offset = offsets[0];
1498 for (int64_t i = 0; i < list->length(); i++) {
1499 const int32_t slot_length = offsets[i + 1] - prev_offset;
1500 const int64_t null_count =
1501 slot_length - arrow::internal::CountSetBits(values->null_bitmap()->data(),
1502 prev_offset, slot_length);
1503 DCHECK_LE(null_count, 1);
1504 const int32_t offset = null_count > 0 ? slot_length - 1 : slot_length;
1505 prev_offset = offsets[i + 1];
1506 offsets[i + 1] = offsets[i] + offset;
1507 }
1508 auto filter =
1509 std::make_shared<BooleanArray>(values->length(), values->null_bitmap());
1510 ARROW_ASSIGN_OR_RAISE(
1511 auto new_values,
1512 Filter(std::move(values), filter, FilterOptions(FilterOptions::DROP), ctx_));
1513 return std::make_shared<ListArray>(list->type(), list->length(),
1514 list->value_offsets(), new_values.make_array());
1515 }
1516 // ONLY_NULL
1517 if (values->null_count() == 0) {
1518 std::fill(offsets + 1, offsets + list->length() + 1, offsets[0]);
1519 } else {
1520 int32_t prev_offset = offsets[0];
1521 for (int64_t i = 0; i < list->length(); i++) {
1522 const int32_t slot_length = offsets[i + 1] - prev_offset;
1523 const int64_t null_count =
1524 slot_length - arrow::internal::CountSetBits(values->null_bitmap()->data(),
1525 prev_offset, slot_length);
1526 const int32_t offset = null_count > 0 ? 1 : 0;
1527 prev_offset = offsets[i + 1];
1528 offsets[i + 1] = offsets[i] + offset;
1529 }
1530 }
1531 ARROW_ASSIGN_OR_RAISE(
1532 auto new_values,
1533 MakeArrayOfNull(out_type_,
1534 list->length() > 0 ? offsets[list->length()] - offsets[0] : 0,
1535 pool_));
1536 return std::make_shared<ListArray>(list->type(), list->length(),
1537 list->value_offsets(), std::move(new_values));
1538 }
1539
1540 std::shared_ptr<DataType> out_type() const override { return list(out_type_); }

Callers

nothing calls this directly

Calls 9

value_offsetsMethod · 0.80
make_arrayMethod · 0.80
CountSetBitsFunction · 0.50
valuesMethod · 0.45
offsetMethod · 0.45
null_countMethod · 0.45
lengthMethod · 0.45
dataMethod · 0.45
typeMethod · 0.45

Tested by

no test coverage detected