|
| 1 | +#include "accessor.h" |
| 2 | + |
| 3 | +#include <ydb/core/formats/arrow/arrow_helpers.h> |
| 4 | +#include <ydb/core/formats/arrow/permutations.h> |
| 5 | +#include <ydb/core/formats/arrow/save_load/saver.h> |
| 6 | +#include <ydb/core/formats/arrow/size_calcer.h> |
| 7 | +#include <ydb/core/formats/arrow/splitter/simple.h> |
| 8 | +#include <ydb/core/formats/arrow/switch/compare.h> |
| 9 | +#include <ydb/core/formats/arrow/switch/switch_type.h> |
| 10 | + |
| 11 | +#include <ydb/library/actors/core/log.h> |
| 12 | + |
| 13 | +namespace NKikimr::NArrow::NAccessor { |
| 14 | + |
| 15 | +void IChunkedArray::TReader::AppendPositionTo(arrow::ArrayBuilder& builder, const ui64 position, ui64* recordSize) const { |
| 16 | + auto address = GetReadChunk(position); |
| 17 | + AFL_VERIFY(NArrow::Append(builder, *address.GetArray(), address.GetPosition(), recordSize)); |
| 18 | +} |
| 19 | + |
| 20 | +std::shared_ptr<arrow::Array> IChunkedArray::TReader::CopyRecord(const ui64 recordIndex) const { |
| 21 | + auto address = GetReadChunk(recordIndex); |
| 22 | + return NArrow::CopyRecords(address.GetArray(), { address.GetPosition() }); |
| 23 | +} |
| 24 | + |
| 25 | +std::shared_ptr<arrow::ChunkedArray> IChunkedArray::Slice(const ui32 offset, const ui32 count) const { |
| 26 | + AFL_VERIFY(offset + count <= (ui64)GetRecordsCount())("offset", offset)("count", count)("length", GetRecordsCount()); |
| 27 | + ui32 currentOffset = offset; |
| 28 | + ui32 countLeast = count; |
| 29 | + std::vector<std::shared_ptr<arrow::Array>> chunks; |
| 30 | + auto address = GetChunkSlow(offset); |
| 31 | + while (countLeast) { |
| 32 | + address = GetChunk(address.GetAddress(), currentOffset); |
| 33 | + const ui64 internalPos = address.GetAddress().GetLocalIndex(currentOffset); |
| 34 | + if (internalPos + countLeast <= (ui64)address.GetArray()->length()) { |
| 35 | + chunks.emplace_back(address.GetArray()->Slice(internalPos, countLeast)); |
| 36 | + break; |
| 37 | + } else { |
| 38 | + const ui32 deltaCount = address.GetArray()->length() - internalPos; |
| 39 | + chunks.emplace_back(address.GetArray()->Slice(internalPos, deltaCount)); |
| 40 | + AFL_VERIFY(countLeast >= deltaCount); |
| 41 | + countLeast -= deltaCount; |
| 42 | + currentOffset += deltaCount; |
| 43 | + } |
| 44 | + } |
| 45 | + return std::make_shared<arrow::ChunkedArray>(chunks, DataType); |
| 46 | +} |
| 47 | + |
| 48 | +NKikimr::NArrow::NAccessor::IChunkedArray::TFullDataAddress IChunkedArray::GetChunk( |
| 49 | + const std::optional<TAddressChain>& chunkCurrent, const ui64 position) const { |
| 50 | + AFL_VERIFY(position < GetRecordsCount()); |
| 51 | + std::optional<TCommonChunkAddress> address; |
| 52 | + |
| 53 | + if (IsDataOwner()) { |
| 54 | + if (chunkCurrent) { |
| 55 | + AFL_VERIFY(chunkCurrent->GetSize() == 1)("size", chunkCurrent->GetSize()); |
| 56 | + } |
| 57 | + auto localAddress = GetLocalData(address, position); |
| 58 | + TAddressChain addressChain; |
| 59 | + addressChain.Add(localAddress.GetAddress()); |
| 60 | + AFL_VERIFY(addressChain.Contains(position)); |
| 61 | + return TFullDataAddress(localAddress.GetArray(), std::move(addressChain)); |
| 62 | + } else { |
| 63 | + auto chunkedArrayAddress = GetArray(chunkCurrent, position, nullptr); |
| 64 | + if (chunkCurrent) { |
| 65 | + AFL_VERIFY(chunkCurrent->GetSize() == 1 + chunkedArrayAddress.GetAddress().GetSize())("current", chunkCurrent->GetSize())( |
| 66 | + "chunked", chunkedArrayAddress.GetAddress().GetSize()); |
| 67 | + } |
| 68 | + auto localAddress = chunkedArrayAddress.GetArray()->GetLocalData(address, chunkedArrayAddress.GetAddress().GetLocalIndex(position)); |
| 69 | + auto fullAddress = std::move(chunkedArrayAddress.MutableAddress()); |
| 70 | + fullAddress.Add(localAddress.GetAddress()); |
| 71 | + AFL_VERIFY(fullAddress.Contains(position)); |
| 72 | + return TFullDataAddress(localAddress.GetArray(), std::move(fullAddress)); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +IChunkedArray::TFullChunkedArrayAddress IChunkedArray::GetArray( |
| 77 | + const std::optional<TAddressChain>& chunkCurrent, const ui64 position, const std::shared_ptr<IChunkedArray>& selfPtr) const { |
| 78 | + AFL_VERIFY(position < GetRecordsCount()); |
| 79 | + if (IsDataOwner()) { |
| 80 | + AFL_VERIFY(selfPtr); |
| 81 | + TAddressChain chain; |
| 82 | + chain.Add(TCommonChunkAddress(0, GetRecordsCount(), 0)); |
| 83 | + return IChunkedArray::TFullChunkedArrayAddress(selfPtr, std::move(chain)); |
| 84 | + } |
| 85 | + TAddressChain addressChain; |
| 86 | + |
| 87 | + auto* currentLevel = this; |
| 88 | + ui32 currentPosition = position; |
| 89 | + ui32 idx = 0; |
| 90 | + std::vector<std::shared_ptr<IChunkedArray>> chainForTemporarySave; |
| 91 | + while (!currentLevel->IsDataOwner()) { |
| 92 | + std::optional<TCommonChunkAddress> currentAddress; |
| 93 | + if (chunkCurrent) { |
| 94 | + currentAddress = chunkCurrent->GetAddress(idx); |
| 95 | + } |
| 96 | + auto nextChunkedArray = currentLevel->GetLocalChunkedArray(currentAddress, currentPosition); |
| 97 | + chainForTemporarySave.emplace_back(nextChunkedArray.GetArray()); |
| 98 | + currentLevel = chainForTemporarySave.back().get(); |
| 99 | + addressChain.Add(nextChunkedArray.GetAddress()); |
| 100 | + AFL_VERIFY(nextChunkedArray.GetAddress().GetStartPosition() <= currentPosition); |
| 101 | + currentPosition -= nextChunkedArray.GetAddress().GetStartPosition(); |
| 102 | + ++idx; |
| 103 | + } |
| 104 | + AFL_VERIFY(!chunkCurrent || chunkCurrent->GetSize() - idx <= 1)("idx", idx)("size", chunkCurrent->GetSize()); |
| 105 | + return TFullChunkedArrayAddress(chainForTemporarySave.back(), std::move(addressChain)); |
| 106 | +} |
| 107 | + |
| 108 | +TString IChunkedArray::TReader::DebugString(const ui32 position) const { |
| 109 | + auto address = GetReadChunk(position); |
| 110 | + return NArrow::DebugString(address.GetArray(), address.GetPosition()); |
| 111 | +} |
| 112 | + |
| 113 | +std::partial_ordering IChunkedArray::TReader::CompareColumns( |
| 114 | + const std::vector<TReader>& l, const ui64 lPosition, const std::vector<TReader>& r, const ui64 rPosition) { |
| 115 | + AFL_VERIFY(l.size() == r.size()); |
| 116 | + for (ui32 i = 0; i < l.size(); ++i) { |
| 117 | + const TAddress lAddress = l[i].GetReadChunk(lPosition); |
| 118 | + const TAddress rAddress = r[i].GetReadChunk(rPosition); |
| 119 | + auto cmp = lAddress.Compare(rAddress); |
| 120 | + if (std::is_neq(cmp)) { |
| 121 | + return cmp; |
| 122 | + } |
| 123 | + } |
| 124 | + return std::partial_ordering::equivalent; |
| 125 | +} |
| 126 | + |
| 127 | +IChunkedArray::TAddress IChunkedArray::TReader::GetReadChunk(const ui64 position) const { |
| 128 | + AFL_VERIFY(position < ChunkedArray->GetRecordsCount()); |
| 129 | + if (CurrentChunkAddress && CurrentChunkAddress->GetAddress().Contains(position)) { |
| 130 | + } else { |
| 131 | + CurrentChunkAddress = ChunkedArray->GetChunk(CurrentChunkAddress, position); |
| 132 | + } |
| 133 | + return IChunkedArray::TAddress(CurrentChunkAddress->GetArray(), CurrentChunkAddress->GetAddress().GetLocalIndex(position)); |
| 134 | +} |
| 135 | + |
| 136 | +const std::partial_ordering IChunkedArray::TAddress::Compare(const TAddress& item) const { |
| 137 | + return TComparator::TypedCompare<true>(*Array, Position, *item.Array, item.Position); |
| 138 | +} |
| 139 | + |
| 140 | +TChunkedArraySerialized::TChunkedArraySerialized(const std::shared_ptr<IChunkedArray>& array, const TString& serializedData) |
| 141 | + : Array(array) |
| 142 | + , SerializedData(serializedData) { |
| 143 | + AFL_VERIFY(serializedData); |
| 144 | + AFL_VERIFY(Array); |
| 145 | + AFL_VERIFY(Array->GetRecordsCount()); |
| 146 | +} |
| 147 | + |
| 148 | +std::partial_ordering IChunkedArray::TFullDataAddress::Compare( |
| 149 | + const ui64 position, const TFullDataAddress& item, const ui64 itemPosition) const { |
| 150 | + AFL_VERIFY(Address.Contains(position))("pos", position)("start", Address.DebugString()); |
| 151 | + AFL_VERIFY(item.Address.Contains(itemPosition))("pos", itemPosition)("start", item.Address.DebugString()); |
| 152 | + return TComparator::TypedCompare<true>(*Array, Address.GetLocalIndex(position), *item.Array, item.Address.GetLocalIndex(itemPosition)); |
| 153 | +} |
| 154 | + |
| 155 | +std::shared_ptr<arrow::Array> IChunkedArray::TFullDataAddress::CopyRecord(const ui64 recordIndex) const { |
| 156 | + return NArrow::CopyRecords(Array, { Address.GetLocalIndex(recordIndex) }); |
| 157 | +} |
| 158 | + |
| 159 | +TString IChunkedArray::TFullDataAddress::DebugString(const ui64 position) const { |
| 160 | + return NArrow::DebugString(Array, Address.GetLocalIndex(position)); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace NKikimr::NArrow::NAccessor |
0 commit comments