|
| 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 "parquet/column/levels.h" |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | + |
| 22 | +#include "parquet/util/rle-encoding.h" |
| 23 | + |
| 24 | +namespace parquet { |
| 25 | + |
| 26 | +LevelEncoder::LevelEncoder() {} |
| 27 | +LevelEncoder::~LevelEncoder() {} |
| 28 | + |
| 29 | +void LevelEncoder::Init(Encoding::type encoding, int16_t max_level, |
| 30 | + int num_buffered_values, uint8_t* data, int data_size) { |
| 31 | + bit_width_ = BitUtil::Log2(max_level + 1); |
| 32 | + encoding_ = encoding; |
| 33 | + switch (encoding) { |
| 34 | + case Encoding::RLE: { |
| 35 | + rle_encoder_.reset(new RleEncoder(data, data_size, bit_width_)); |
| 36 | + break; |
| 37 | + } |
| 38 | + case Encoding::BIT_PACKED: { |
| 39 | + int num_bytes = BitUtil::Ceil(num_buffered_values * bit_width_, 8); |
| 40 | + bit_packed_encoder_.reset(new BitWriter(data, num_bytes)); |
| 41 | + break; |
| 42 | + } |
| 43 | + default: |
| 44 | + throw ParquetException("Unknown encoding type for levels."); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +int LevelEncoder::MaxBufferSize( |
| 49 | + Encoding::type encoding, int16_t max_level, int num_buffered_values) { |
| 50 | + int bit_width = BitUtil::Log2(max_level + 1); |
| 51 | + int num_bytes = 0; |
| 52 | + switch (encoding) { |
| 53 | + case Encoding::RLE: { |
| 54 | + // TODO: Due to the way we currently check if the buffer is full enough, |
| 55 | + // we need to have MinBufferSize as head room. |
| 56 | + num_bytes = RleEncoder::MaxBufferSize(bit_width, num_buffered_values) + |
| 57 | + RleEncoder::MinBufferSize(bit_width); |
| 58 | + break; |
| 59 | + } |
| 60 | + case Encoding::BIT_PACKED: { |
| 61 | + num_bytes = BitUtil::Ceil(num_buffered_values * bit_width, 8); |
| 62 | + break; |
| 63 | + } |
| 64 | + default: |
| 65 | + throw ParquetException("Unknown encoding type for levels."); |
| 66 | + } |
| 67 | + return num_bytes; |
| 68 | +} |
| 69 | + |
| 70 | +int LevelEncoder::Encode(int batch_size, const int16_t* levels) { |
| 71 | + int num_encoded = 0; |
| 72 | + if (!rle_encoder_ && !bit_packed_encoder_) { |
| 73 | + throw ParquetException("Level encoders are not initialized."); |
| 74 | + } |
| 75 | + |
| 76 | + if (encoding_ == Encoding::RLE) { |
| 77 | + for (int i = 0; i < batch_size; ++i) { |
| 78 | + if (!rle_encoder_->Put(*(levels + i))) { break; } |
| 79 | + ++num_encoded; |
| 80 | + } |
| 81 | + rle_encoder_->Flush(); |
| 82 | + rle_length_ = rle_encoder_->len(); |
| 83 | + } else { |
| 84 | + for (int i = 0; i < batch_size; ++i) { |
| 85 | + if (!bit_packed_encoder_->PutValue(*(levels + i), bit_width_)) { break; } |
| 86 | + ++num_encoded; |
| 87 | + } |
| 88 | + bit_packed_encoder_->Flush(); |
| 89 | + } |
| 90 | + return num_encoded; |
| 91 | +} |
| 92 | + |
| 93 | +LevelDecoder::LevelDecoder() |
| 94 | + : num_values_remaining_(0) {} |
| 95 | + |
| 96 | +LevelDecoder::~LevelDecoder() {} |
| 97 | + |
| 98 | +int LevelDecoder::SetData(Encoding::type encoding, int16_t max_level, |
| 99 | + int num_buffered_values, const uint8_t* data) { |
| 100 | + uint32_t num_bytes = 0; |
| 101 | + encoding_ = encoding; |
| 102 | + num_values_remaining_ = num_buffered_values; |
| 103 | + bit_width_ = BitUtil::Log2(max_level + 1); |
| 104 | + switch (encoding) { |
| 105 | + case Encoding::RLE: { |
| 106 | + num_bytes = *reinterpret_cast<const uint32_t*>(data); |
| 107 | + const uint8_t* decoder_data = data + sizeof(uint32_t); |
| 108 | + if (!rle_decoder_) { |
| 109 | + rle_decoder_.reset(new RleDecoder(decoder_data, num_bytes, bit_width_)); |
| 110 | + } else { |
| 111 | + rle_decoder_->Reset(decoder_data, num_bytes, bit_width_); |
| 112 | + } |
| 113 | + return sizeof(uint32_t) + num_bytes; |
| 114 | + } |
| 115 | + case Encoding::BIT_PACKED: { |
| 116 | + num_bytes = BitUtil::Ceil(num_buffered_values * bit_width_, 8); |
| 117 | + if (!bit_packed_decoder_) { |
| 118 | + bit_packed_decoder_.reset(new BitReader(data, num_bytes)); |
| 119 | + } else { |
| 120 | + bit_packed_decoder_->Reset(data, num_bytes); |
| 121 | + } |
| 122 | + return num_bytes; |
| 123 | + } |
| 124 | + default: |
| 125 | + throw ParquetException("Unknown encoding type for levels."); |
| 126 | + } |
| 127 | + return -1; |
| 128 | +} |
| 129 | + |
| 130 | +int LevelDecoder::Decode(int batch_size, int16_t* levels) { |
| 131 | + int num_decoded = 0; |
| 132 | + |
| 133 | + int num_values = std::min(num_values_remaining_, batch_size); |
| 134 | + if (encoding_ == Encoding::RLE) { |
| 135 | + num_decoded = rle_decoder_->GetBatch(levels, num_values); |
| 136 | + } else { |
| 137 | + for (int i = 0; i < num_values; ++i) { |
| 138 | + if (!bit_packed_decoder_->GetValue(bit_width_, levels + i)) { break; } |
| 139 | + ++num_decoded; |
| 140 | + } |
| 141 | + } |
| 142 | + num_values_remaining_ -= num_decoded; |
| 143 | + return num_decoded; |
| 144 | +} |
| 145 | + |
| 146 | +} // namespace parquet |
0 commit comments