|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | #include "arrow/array/array_run_end.h" |
| 19 | +#include "arrow/array/builder_primitive.h" |
19 | 20 | #include "arrow/array/util.h" |
20 | 21 | #include "arrow/util/logging.h" |
21 | 22 | #include "arrow/util/ree_util.h" |
@@ -85,6 +86,83 @@ void RunEndEncodedArray::SetData(const std::shared_ptr<ArrayData>& data) { |
85 | 86 | values_array_ = MakeArray(this->data()->child_data[1]); |
86 | 87 | } |
87 | 88 |
|
| 89 | +namespace { |
| 90 | + |
| 91 | +template <typename RunEndType> |
| 92 | +Result<std::shared_ptr<Array>> MakeLogicalRunEnds(const RunEndEncodedArray& self, |
| 93 | + MemoryPool* pool) { |
| 94 | + using RunEndCType = typename RunEndType::c_type; |
| 95 | + if (self.offset() == 0) { |
| 96 | + const auto& run_ends = *self.run_ends(); |
| 97 | + if (self.length() == 0) { |
| 98 | + return run_ends.Slice(0, 0); |
| 99 | + } |
| 100 | + |
| 101 | + // If offset==0 and the non-zero logical length aligns perfectly with a |
| 102 | + // physical run-end, we can return a slice of the run-ends array. |
| 103 | + const int64_t physical_length = self.FindPhysicalLength(); |
| 104 | + const auto* run_end_values = self.data()->child_data[0]->GetValues<RunEndCType>(1); |
| 105 | + if (run_end_values[physical_length - 1] == self.length()) { |
| 106 | + return run_ends.Slice(0, physical_length); |
| 107 | + } |
| 108 | + |
| 109 | + // Otherwise we need to copy the run-ends array and adjust only the very |
| 110 | + // last run-end. |
| 111 | + auto new_run_ends_data = ArrayData::Make(run_ends.type(), physical_length, 0, 0); |
| 112 | + { |
| 113 | + ARROW_ASSIGN_OR_RAISE(auto buffer, |
| 114 | + AllocateBuffer(physical_length * sizeof(RunEndCType), pool)); |
| 115 | + new_run_ends_data->buffers = {NULLPTR, std::move(buffer)}; |
| 116 | + } |
| 117 | + auto* new_run_end_values = new_run_ends_data->GetMutableValues<RunEndCType>(1); |
| 118 | + memcpy(new_run_end_values, run_end_values, |
| 119 | + (physical_length - 1) * sizeof(RunEndCType)); |
| 120 | + new_run_end_values[physical_length - 1] = static_cast<RunEndCType>(self.length()); |
| 121 | + return MakeArray(std::move(new_run_ends_data)); |
| 122 | + } |
| 123 | + |
| 124 | + // When the logical offset is non-zero, all run-end values need to be adjusted. |
| 125 | + int64_t physical_offset = self.FindPhysicalOffset(); |
| 126 | + int64_t physical_length = self.FindPhysicalLength(); |
| 127 | + |
| 128 | + const auto* run_end_values = self.data()->child_data[0]->GetValues<RunEndCType>(1); |
| 129 | + NumericBuilder<RunEndType> builder(pool); |
| 130 | + RETURN_NOT_OK(builder.Resize(physical_length)); |
| 131 | + if (physical_length > 0) { |
| 132 | + for (int64_t i = 0; i < physical_length - 1; i++) { |
| 133 | + const auto run_end = run_end_values[physical_offset + i] - self.offset(); |
| 134 | + DCHECK_LT(run_end, self.length()); |
| 135 | + RETURN_NOT_OK(builder.Append(static_cast<RunEndCType>(run_end))); |
| 136 | + } |
| 137 | + DCHECK_GE(run_end_values[physical_offset + physical_length - 1] - self.offset(), |
| 138 | + self.length()); |
| 139 | + RETURN_NOT_OK(builder.Append(static_cast<RunEndCType>(self.length()))); |
| 140 | + } |
| 141 | + return builder.Finish(); |
| 142 | +} |
| 143 | + |
| 144 | +} // namespace |
| 145 | + |
| 146 | +Result<std::shared_ptr<Array>> RunEndEncodedArray::LogicalRunEnds( |
| 147 | + MemoryPool* pool) const { |
| 148 | + DCHECK(data()->child_data[0]->buffers[1]->is_cpu()); |
| 149 | + switch (run_ends_array_->type_id()) { |
| 150 | + case Type::INT16: |
| 151 | + return MakeLogicalRunEnds<Int16Type>(*this, pool); |
| 152 | + case Type::INT32: |
| 153 | + return MakeLogicalRunEnds<Int32Type>(*this, pool); |
| 154 | + default: |
| 155 | + DCHECK_EQ(run_ends_array_->type_id(), Type::INT64); |
| 156 | + return MakeLogicalRunEnds<Int64Type>(*this, pool); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +std::shared_ptr<Array> RunEndEncodedArray::LogicalValues() const { |
| 161 | + const int64_t physical_offset = FindPhysicalOffset(); |
| 162 | + const int64_t physical_length = FindPhysicalLength(); |
| 163 | + return MakeArray(data()->child_data[1]->Slice(physical_offset, physical_length)); |
| 164 | +} |
| 165 | + |
88 | 166 | int64_t RunEndEncodedArray::FindPhysicalOffset() const { |
89 | 167 | const ArraySpan span(*this->data_); |
90 | 168 | return ree_util::FindPhysicalIndex(span, 0, span.offset); |
|
0 commit comments