| 808 | } |
| 809 | |
| 810 | Status Merge(GroupedAggregator&& raw_other, |
| 811 | const ArrayData& group_id_mapping) override { |
| 812 | // The merge is asymmetric. "first" from this state gets pick over "first" from other |
| 813 | // state. "last" from other state gets pick over from this state. This is so that when |
| 814 | // using with segmented aggregation, we still get the correct "first" and "last" |
| 815 | // value for the entire segment. |
| 816 | auto other = checked_cast<GroupedFirstLastImpl*>(&raw_other); |
| 817 | |
| 818 | auto raw_firsts = firsts_.mutable_data(); |
| 819 | auto raw_lasts = lasts_.mutable_data(); |
| 820 | auto raw_has_values = has_values_.mutable_data(); |
| 821 | auto raw_has_any_values = has_any_values_.mutable_data(); |
| 822 | auto raw_first_is_nulls = first_is_nulls_.mutable_data(); |
| 823 | auto raw_last_is_nulls = last_is_nulls_.mutable_data(); |
| 824 | |
| 825 | auto other_raw_firsts = other->firsts_.mutable_data(); |
| 826 | auto other_raw_lasts = other->lasts_.mutable_data(); |
| 827 | auto other_raw_has_values = other->has_values_.mutable_data(); |
| 828 | auto other_raw_has_any_values = other->has_values_.mutable_data(); |
| 829 | auto other_raw_last_is_nulls = other->last_is_nulls_.mutable_data(); |
| 830 | |
| 831 | auto g = group_id_mapping.GetValues<uint32_t>(1); |
| 832 | |
| 833 | for (uint32_t other_g = 0; static_cast<int64_t>(other_g) < group_id_mapping.length; |
| 834 | ++other_g, ++g) { |
| 835 | if (!bit_util::GetBit(raw_has_values, *g)) { |
| 836 | if (bit_util::GetBit(other_raw_has_values, other_g)) { |
| 837 | GetSet::Set(raw_firsts, *g, GetSet::Get(other_raw_firsts, other_g)); |
| 838 | } |
| 839 | } |
| 840 | if (bit_util::GetBit(other_raw_has_values, other_g)) { |
| 841 | GetSet::Set(raw_lasts, *g, GetSet::Get(other_raw_lasts, other_g)); |
| 842 | } |
| 843 | // If the current state doesn't have any nulls (null or non-null), then |
| 844 | // We take the "first_is_null" from rhs |
| 845 | if (!bit_util::GetBit(raw_has_any_values, *g)) { |
| 846 | bit_util::SetBitTo(raw_first_is_nulls, *g, |
| 847 | bit_util::GetBit(other->first_is_nulls_.data(), other_g)); |
| 848 | } |
| 849 | if (bit_util::GetBit(other_raw_last_is_nulls, other_g)) { |
| 850 | bit_util::SetBit(raw_last_is_nulls, *g); |
| 851 | } |
| 852 | |
| 853 | if (bit_util::GetBit(other_raw_has_values, other_g)) { |
| 854 | bit_util::SetBit(raw_has_values, *g); |
| 855 | } |
| 856 | |
| 857 | if (bit_util::GetBit(other_raw_has_any_values, other_g)) { |
| 858 | bit_util::SetBit(raw_has_any_values, *g); |
| 859 | } |
| 860 | } |
| 861 | return Status::OK(); |
| 862 | } |
| 863 | |
| 864 | Result<Datum> Finalize() override { |
| 865 | // We initialize the null bitmap with first_is_nulls and last_is_nulls |