|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/array/array_run_end.h" |
| 19 | +#include "arrow/array/util.h" |
| 20 | +#include "arrow/util/logging.h" |
| 21 | +#include "arrow/util/ree_util.h" |
| 22 | + |
| 23 | +namespace arrow { |
| 24 | + |
| 25 | +// ---------------------------------------------------------------------- |
| 26 | +// RunEndEncodedArray |
| 27 | + |
| 28 | +RunEndEncodedArray::RunEndEncodedArray(const std::shared_ptr<ArrayData>& data) { |
| 29 | + this->SetData(data); |
| 30 | +} |
| 31 | + |
| 32 | +RunEndEncodedArray::RunEndEncodedArray(const std::shared_ptr<DataType>& type, |
| 33 | + int64_t length, |
| 34 | + const std::shared_ptr<Array>& run_ends, |
| 35 | + const std::shared_ptr<Array>& values, |
| 36 | + int64_t offset) { |
| 37 | + this->SetData(ArrayData::Make(type, length, |
| 38 | + /*buffers=*/{NULLPTR}, |
| 39 | + /*child_data=*/{run_ends->data(), values->data()}, |
| 40 | + /*null_count=*/0, offset)); |
| 41 | +} |
| 42 | + |
| 43 | +Result<std::shared_ptr<RunEndEncodedArray>> RunEndEncodedArray::Make( |
| 44 | + int64_t logical_length, const std::shared_ptr<Array>& run_ends, |
| 45 | + const std::shared_ptr<Array>& values, int64_t logical_offset) { |
| 46 | + auto run_end_type = run_ends->type(); |
| 47 | + auto values_type = values->type(); |
| 48 | + if (!RunEndEncodedType::RunEndTypeValid(*run_end_type)) { |
| 49 | + return Status::Invalid("Run end type must be int16, int32 or int64"); |
| 50 | + } |
| 51 | + if (run_ends->null_count() != 0) { |
| 52 | + return Status::Invalid("Run ends array cannot contain null values"); |
| 53 | + } |
| 54 | + if (values->length() < run_ends->length()) { |
| 55 | + return Status::Invalid("Values array has to be at least as long as run ends array"); |
| 56 | + } |
| 57 | + |
| 58 | + return std::make_shared<RunEndEncodedArray>( |
| 59 | + run_end_encoded(std::move(run_end_type), std::move(values_type)), logical_length, |
| 60 | + run_ends, values, logical_offset); |
| 61 | +} |
| 62 | + |
| 63 | +void RunEndEncodedArray::SetData(const std::shared_ptr<ArrayData>& data) { |
| 64 | + ARROW_CHECK_EQ(data->type->id(), Type::RUN_END_ENCODED); |
| 65 | + const auto* ree_type = |
| 66 | + internal::checked_cast<const RunEndEncodedType*>(data->type.get()); |
| 67 | + ARROW_CHECK_EQ(ree_type->run_end_type()->id(), data->child_data[0]->type->id()); |
| 68 | + ARROW_CHECK_EQ(ree_type->value_type()->id(), data->child_data[1]->type->id()); |
| 69 | + |
| 70 | + DCHECK_EQ(data->child_data.size(), 2); |
| 71 | + |
| 72 | + // A non-zero number of logical values in this array (offset + length) implies |
| 73 | + // a non-zero number of runs and values. |
| 74 | + DCHECK(data->offset + data->length == 0 || data->child_data[0]->length > 0); |
| 75 | + DCHECK(data->offset + data->length == 0 || data->child_data[1]->length > 0); |
| 76 | + // At least as many values as run_ends |
| 77 | + DCHECK_GE(data->child_data[1]->length, data->child_data[0]->length); |
| 78 | + |
| 79 | + // The null count for run-end encoded arrays is always 0. Actual number of |
| 80 | + // nulls needs to be calculated through other means. |
| 81 | + DCHECK_EQ(data->null_count, 0); |
| 82 | + |
| 83 | + Array::SetData(data); |
| 84 | + run_ends_array_ = MakeArray(this->data()->child_data[0]); |
| 85 | + values_array_ = MakeArray(this->data()->child_data[1]); |
| 86 | +} |
| 87 | + |
| 88 | +int64_t RunEndEncodedArray::FindPhysicalOffset() const { |
| 89 | + const ArraySpan span(*this->data_); |
| 90 | + return ree_util::FindPhysicalIndex(span, 0, span.offset); |
| 91 | +} |
| 92 | + |
| 93 | +int64_t RunEndEncodedArray::FindPhysicalLength() const { |
| 94 | + const ArraySpan span(*this->data_); |
| 95 | + return ree_util::FindPhysicalLength(span); |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace arrow |
0 commit comments