github-actions[bot] commented on code in PR #65674:
URL: https://github.com/apache/doris/pull/65674#discussion_r3628368283


##########
be/src/format_v2/parquet/reader/native/delta_bit_pack_decoder.h:
##########
@@ -0,0 +1,812 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <gen_cpp/parquet_types.h>
+#include <glog/logging.h>
+
+#include <cstddef>
+#include <cstdint>
+#include <cstring>
+#include <limits>
+#include <memory>
+#include <ostream>
+#include <string>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "common/status.h"
+#include "core/column/column_fixed_length_object.h"
+#include "core/column/column_vector.h"
+#include "core/data_type/data_type.h"
+#include "exec/common/arithmetic_overflow.h"
+#include "format_v2/parquet/reader/native/decoder.h"
+#include "util/bit_stream_utils.h"
+#include "util/bit_stream_utils.inline.h"
+#include "util/slice.h"
+
+namespace doris::format::parquet::native {
+namespace detail {
+
+inline Status checked_delta_padding_bits(uint32_t bit_width, uint32_t 
remaining_values,
+                                         int64_t* padding_bits) {
+    DORIS_CHECK(padding_bits != nullptr);
+    const uint64_t widened_bits = static_cast<uint64_t>(bit_width) * 
remaining_values;
+    if (UNLIKELY(widened_bits > 
static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))) {
+        return Status::Corruption("Parquet delta miniblock padding is too 
large");
+    }
+    *padding_bits = static_cast<int64_t>(widened_bits);
+    return Status::OK();
+}
+
+} // namespace detail
+
+class DeltaDecoder : public Decoder {
+public:
+    DeltaDecoder() = default;
+    ~DeltaDecoder() override = default;
+};
+
+/**
+ *   Format
+ *      [header] [block 1] [block 2] ... [block N]
+ *   Header
+ *      [block size] [_mini_blocks_per_block] [_total_value_count] [first 
value]
+ *   Block
+ *      [min delta] [list of bitwidths of the mini blocks] [miniblocks]
+ */
+template <typename T>
+class DeltaBitPackDecoder final : public DeltaDecoder {
+public:
+    using UT = std::make_unsigned_t<T>;
+
+    DeltaBitPackDecoder() = default;
+    ~DeltaBitPackDecoder() override = default;
+
+    Status skip_values(size_t num_values) override {
+        constexpr size_t kSkipBatchSize = 4096;
+        _values.resize(std::min(num_values, kSkipBatchSize));
+        size_t skipped = 0;
+        while (skipped < num_values) {
+            const size_t batch_size = std::min(num_values - skipped, 
kSkipBatchSize);
+            uint32_t num_valid_values = 0;
+            RETURN_IF_ERROR(_get_internal(_values.data(), 
static_cast<uint32_t>(batch_size),
+                                          &num_valid_values));
+            // Skips retain exact validation without allocating in proportion 
to a sparse gap.
+            if (UNLIKELY(num_valid_values != batch_size)) {
+                return Status::IOError("Expected to skip {} Parquet delta 
values, skipped {}",
+                                       num_values, skipped + num_valid_values);
+            }
+            skipped += batch_size;
+        }
+        return Status::OK();
+    }
+
+    Status decode_fixed_values(size_t num_values, ParquetFixedValueConsumer& 
consumer) override {
+        _values.resize(num_values);
+        uint32_t decoded_count = 0;
+        RETURN_IF_ERROR(
+                _get_internal(_values.data(), cast_set<uint32_t>(num_values), 
&decoded_count));
+        if (UNLIKELY(decoded_count != num_values)) {
+            return Status::IOError("Expected {} Parquet delta values, decoded 
{}", num_values,
+                                   decoded_count);
+        }
+        return consumer.consume(reinterpret_cast<const 
uint8_t*>(_values.data()), num_values,
+                                sizeof(T));
+    }
+
+    Status decode_selected_fixed_values(const ParquetSelection& selection,
+                                        ParquetFixedValueConsumer& consumer) 
override {
+        _values.resize(selection.total_values);
+        uint32_t decoded_count = 0;
+        RETURN_IF_ERROR(_get_internal(_values.data(), 
cast_set<uint32_t>(selection.total_values),
+                                      &decoded_count));
+        if (UNLIKELY(decoded_count != selection.total_values)) {
+            return Status::IOError("Expected {} Parquet delta values, decoded 
{}",
+                                   selection.total_values, decoded_count);
+        }
+        size_t output = 0;
+        for (const auto& range : selection.ranges) {
+            memmove(_values.data() + output, _values.data() + range.first, 
range.count * sizeof(T));
+            output += range.count;
+        }
+        DORIS_CHECK_EQ(output, selection.selected_values);
+        return consumer.consume(reinterpret_cast<const 
uint8_t*>(_values.data()), output,
+                                sizeof(T));
+    }
+
+    Status decode(T* buffer, uint32_t num_values, uint32_t* out_num_values) {
+        return _get_internal(buffer, num_values, out_num_values);
+    }
+
+    uint32_t valid_values_count() {
+        // _total_value_count in header ignores of null values
+        return _total_values_remaining;
+    }
+
+    void release_scratch(size_t max_retained_bytes) override {
+        release_vector_if_oversized(&_values, max_retained_bytes);
+        // The bit-width table drives later miniblock transitions, so it is 
reclaimable only after
+        // the page is exhausted; _values contains completed batch output and 
is always disposable.
+        if (_total_values_remaining == 0) {
+            release_vector_if_oversized(&_delta_bit_widths, 
max_retained_bytes);
+        }
+    }
+    size_t retained_scratch_bytes() const override {
+        return _values.capacity() * sizeof(T) + _delta_bit_widths.capacity() * 
sizeof(uint8_t);
+    }
+    size_t active_scratch_bytes() const override {
+        return _values.size() * sizeof(T) + _delta_bit_widths.size() * 
sizeof(uint8_t);
+    }
+
+    Status set_data(Slice* slice) override {
+        _bit_reader.reset(
+                new BitReader((const uint8_t*)slice->data, 
cast_set<uint32_t>(slice->size)));
+        RETURN_IF_ERROR(_init_header());
+        _data = slice;
+        _offset = 0;
+        return Status::OK();
+    }
+
+    // Set BitReader which is already initialized by 
DeltaLengthByteArrayDecoder or
+    // DeltaByteArrayDecoder
+    Status set_bit_reader(std::shared_ptr<BitReader> bit_reader) {
+        _bit_reader = std::move(bit_reader);
+        RETURN_IF_ERROR(_init_header());
+        return Status::OK();
+    }
+
+private:
+    static constexpr int kMaxDeltaBitWidth = static_cast<int>(sizeof(T) * 8);
+    Status _init_header();
+    Status _init_block();
+    Status _init_mini_block(int bit_width);
+    Status _get_internal(T* buffer, uint32_t max_values, uint32_t* 
out_num_values);
+
+    std::vector<T> _values;
+
+    std::shared_ptr<BitReader> _bit_reader;
+    uint32_t _values_per_block;
+    uint32_t _mini_blocks_per_block;
+    uint32_t _values_per_mini_block;
+    uint32_t _total_value_count;
+
+    T _min_delta;
+    T _last_value;
+
+    uint32_t _mini_block_idx;
+    std::vector<uint8_t> _delta_bit_widths;
+    int _delta_bit_width;
+    // If the page doesn't contain any block, `_block_initialized` will
+    // always be false. Otherwise, it will be true when first block 
initialized.
+    bool _block_initialized;
+
+    uint32_t _total_values_remaining;
+    // Remaining values in current mini block. If the current block is the 
last mini block,
+    // _values_remaining_current_mini_block may greater than 
_total_values_remaining.
+    uint32_t _values_remaining_current_mini_block;
+};
+
+class DeltaLengthByteArrayDecoder final : public DeltaDecoder {
+public:
+    explicit DeltaLengthByteArrayDecoder()
+            : _len_decoder(), _buffered_length(0), _buffered_data(0) {}
+
+    void set_expected_values(size_t expected_values) override {
+        Decoder::set_expected_values(expected_values);
+        _len_decoder.set_expected_values(expected_values);
+    }
+
+    Status skip_values(size_t num_values) override {
+        constexpr size_t kSkipBatchSize = 4096;
+        _values.resize(std::min(num_values, kSkipBatchSize));
+        size_t skipped = 0;
+        while (skipped < num_values) {
+            const size_t batch_size = std::min(num_values - skipped, 
kSkipBatchSize);
+            int num_valid_values = 0;
+            RETURN_IF_ERROR(
+                    _get_internal(_values.data(), 
static_cast<int>(batch_size), &num_valid_values));
+            if (UNLIKELY(num_valid_values != batch_size)) {
+                return Status::IOError(
+                        "Expected to skip {} Parquet delta-length values, 
skipped {}", num_values,
+                        skipped + num_valid_values);
+            }
+            skipped += batch_size;
+        }
+        return Status::OK();
+    }
+
+    Status decode_binary_values(size_t num_values, ParquetBinaryValueConsumer& 
consumer) override {
+        _values.resize(num_values);
+        int decoded_count = 0;
+        RETURN_IF_ERROR(
+                _get_internal(_values.data(), cast_set<int32_t>(num_values), 
&decoded_count));
+        if (UNLIKELY(decoded_count != num_values)) {
+            return Status::IOError("Expected {} Parquet delta-length values, 
decoded {}",
+                                   num_values, decoded_count);
+        }
+        _string_refs.resize(num_values);
+        for (size_t row = 0; row < num_values; ++row) {
+            _string_refs[row] = StringRef(_values[row].data, 
_values[row].size);
+        }
+        return consumer.consume(_string_refs.data(), _string_refs.size());
+    }
+
+    Status decode_selected_binary_values(const ParquetSelection& selection,
+                                         ParquetBinaryValueConsumer& consumer) 
override {
+        _values.resize(selection.total_values);
+        int decoded_count = 0;
+        RETURN_IF_ERROR(_get_internal(_values.data(), 
cast_set<int32_t>(selection.total_values),
+                                      &decoded_count));
+        if (UNLIKELY(decoded_count != selection.total_values)) {
+            return Status::IOError("Expected {} Parquet delta-length values, 
decoded {}",
+                                   selection.total_values, decoded_count);
+        }
+        _string_refs.resize(selection.selected_values);
+        size_t output = 0;
+        for (const auto& range : selection.ranges) {
+            for (size_t row = 0; row < range.count; ++row) {
+                const auto& value = _values[range.first + row];
+                _string_refs[output++] = StringRef(value.data, value.size);
+            }
+        }
+        DORIS_CHECK_EQ(output, selection.selected_values);
+        return consumer.consume(_string_refs.data(), _string_refs.size());
+    }
+
+    Status decode(Slice* buffer, int num_values, int* out_num_values) {
+        return _get_internal(buffer, num_values, out_num_values);
+    }
+
+    int valid_values_count() const { return _num_valid_values; }
+
+    void release_scratch(size_t max_retained_bytes) override {
+        release_vector_if_oversized(&_values, max_retained_bytes);
+        release_vector_if_oversized(&_string_refs, max_retained_bytes);
+        release_vector_if_oversized(&_buffered_length, max_retained_bytes);
+        release_vector_if_oversized(&_buffered_data, max_retained_bytes);
+        _len_decoder.release_scratch(max_retained_bytes);
+    }
+    size_t retained_scratch_bytes() const override {
+        return _values.capacity() * sizeof(Slice) + _string_refs.capacity() * 
sizeof(StringRef) +
+               _buffered_length.capacity() * sizeof(int32_t) +
+               _buffered_data.capacity() * sizeof(char) + 
_len_decoder.retained_scratch_bytes();
+    }
+    size_t active_scratch_bytes() const override {
+        return _values.size() * sizeof(Slice) + _string_refs.size() * 
sizeof(StringRef) +
+               _buffered_length.size() * sizeof(int32_t) + 
_buffered_data.size() * sizeof(char) +
+               _len_decoder.active_scratch_bytes();
+    }
+
+    Status set_data(Slice* slice) override {
+        if (slice == nullptr || slice->size == 0) {
+            // Reused decoders must never retain lengths or payload pointers 
from the prior page.
+            _bit_reader.reset();
+            _data = nullptr;
+            _offset = 0;
+            _num_valid_values = 0;
+            _buffered_length.clear();
+            _buffered_data.clear();
+            return Status::Corruption("Parquet delta-length page is empty");
+        }
+        _bit_reader = std::make_shared<BitReader>((const uint8_t*)slice->data, 
slice->size);
+        _data = slice;
+        _offset = 0;
+        RETURN_IF_ERROR(_init_lengths());
+        return Status::OK();
+    }
+
+    Status set_bit_reader(std::shared_ptr<BitReader> bit_reader) {
+        _bit_reader = std::move(bit_reader);
+        RETURN_IF_ERROR(_init_lengths());
+        return Status::OK();
+    }
+
+private:
+    Status _init_lengths();
+    Status _get_internal(Slice* buffer, int max_values, int* out_num_values);
+
+    std::vector<Slice> _values;
+    std::vector<StringRef> _string_refs;
+    std::shared_ptr<BitReader> _bit_reader;
+    DeltaBitPackDecoder<int32_t> _len_decoder;
+
+    int _num_valid_values;
+    std::vector<int32_t> _buffered_length;
+    std::vector<char> _buffered_data;
+};
+
+class DeltaByteArrayDecoder : public DeltaDecoder {
+public:
+    explicit DeltaByteArrayDecoder() : _buffered_prefix_length(0), 
_buffered_data(0) {}
+
+    void set_expected_values(size_t expected_values) override {
+        Decoder::set_expected_values(expected_values);
+        _prefix_len_decoder.set_expected_values(expected_values);
+        _suffix_decoder.set_expected_values(expected_values);
+    }
+
+    Status skip_values(size_t num_values) override {
+        constexpr size_t kSkipBatchSize = 4096;
+        size_t skipped = 0;
+        while (skipped < num_values) {
+            const size_t batch_size = std::min(num_values - skipped, 
kSkipBatchSize);
+            RETURN_IF_ERROR(_skip_slices(batch_size));
+            skipped += batch_size;
+        }
+        return Status::OK();
+    }
+
+    Status decode_binary_values(size_t num_values, ParquetBinaryValueConsumer& 
consumer) override {
+        RETURN_IF_ERROR(_decode_slices(num_values));
+        _string_refs.resize(num_values);
+        for (size_t row = 0; row < num_values; ++row) {
+            _string_refs[row] = StringRef(_values[row].data, 
_values[row].size);
+        }
+        return consumer.consume(_string_refs.data(), _string_refs.size());
+    }
+
+    Status decode_selected_binary_values(const ParquetSelection& selection,
+                                         ParquetBinaryValueConsumer& consumer) 
override {
+        _selected_binary_values.clear();
+        _selected_value_sizes.clear();
+        _selected_value_sizes.reserve(selection.selected_values);
+        size_t cursor = 0;
+        for (const auto& range : selection.ranges) {
+            DORIS_CHECK(range.first >= cursor);
+            RETURN_IF_ERROR(skip_values(range.first - cursor));
+            size_t decoded = 0;
+            while (decoded < range.count) {
+                const size_t batch_size = 
_bounded_reconstruction_batch_size(range.count - decoded);
+                RETURN_IF_ERROR(_decode_slices(batch_size));
+                for (const auto& value : _values) {
+                    if (UNLIKELY(value.size > 
std::numeric_limits<size_t>::max() -
+                                                      
_selected_binary_values.size())) {
+                        return Status::IOError(
+                                "Parquet delta-byte-array selection byte size 
overflows");
+                    }
+                    if (value.size > 0) {
+                        
_selected_binary_values.insert(_selected_binary_values.end(), value.data,
+                                                       value.data + 
value.size);
+                    }
+                    _selected_value_sizes.push_back(value.size);
+                }
+                decoded += batch_size;
+            }
+            cursor = range.first + range.count;
+        }
+        DORIS_CHECK(selection.total_values >= cursor);
+        RETURN_IF_ERROR(skip_values(selection.total_values - cursor));
+
+        _string_refs.resize(selection.selected_values);
+        size_t byte_offset = 0;
+        for (size_t output = 0; output < _selected_value_sizes.size(); 
++output) {
+            const size_t value_size = _selected_value_sizes[output];
+            const char* value_data =
+                    value_size == 0 ? nullptr : _selected_binary_values.data() 
+ byte_offset;
+            _string_refs[output] = StringRef(value_data, value_size);
+            byte_offset += value_size;
+        }
+        DORIS_CHECK_EQ(_selected_value_sizes.size(), 
selection.selected_values);
+        DORIS_CHECK_EQ(byte_offset, _selected_binary_values.size());
+        return consumer.consume(_string_refs.data(), _string_refs.size());
+    }
+
+    Status decode_fixed_values(size_t num_values, ParquetFixedValueConsumer& 
consumer) override {
+        RETURN_IF_ERROR(_decode_slices(num_values));
+        RETURN_IF_ERROR(_validate_fixed_width_values());
+        const size_t byte_size = num_values * 
static_cast<size_t>(_type_length);
+        _fixed_values.resize(byte_size);
+        for (size_t row = 0; row < num_values; ++row) {
+            memcpy(_fixed_values.data() + row * _type_length, 
_values[row].data, _type_length);
+        }
+        return consumer.consume(_fixed_values.data(), num_values,
+                                static_cast<size_t>(_type_length));
+    }
+
+    Status decode_selected_fixed_values(const ParquetSelection& selection,
+                                        ParquetFixedValueConsumer& consumer) 
override {
+        DORIS_CHECK(_type_length > 0);
+        const size_t value_width = static_cast<size_t>(_type_length);
+        if (UNLIKELY(selection.selected_values >
+                     std::numeric_limits<size_t>::max() / value_width)) {
+            return Status::IOError("Parquet delta-byte-array selection byte 
size overflows");
+        }
+        _fixed_values.resize(selection.selected_values * value_width);
+        size_t output = 0;
+        size_t cursor = 0;
+        for (const auto& range : selection.ranges) {
+            DORIS_CHECK(range.first >= cursor);
+            // Skips validate widths too, so filtering cannot hide a malformed 
value after the
+            // decoder cursor has advanced past it.
+            RETURN_IF_ERROR(skip_values(range.first - cursor));
+            size_t decoded = 0;
+            while (decoded < range.count) {
+                const size_t batch_size = 
_bounded_reconstruction_batch_size(range.count - decoded);
+                RETURN_IF_ERROR(_decode_slices(batch_size));
+                RETURN_IF_ERROR(_validate_fixed_width_values());
+                for (const auto& value : _values) {
+                    memcpy(_fixed_values.data() + output * value_width, 
value.data, value_width);
+                    ++output;
+                }
+                decoded += batch_size;
+            }
+            cursor = range.first + range.count;
+        }
+        DORIS_CHECK(selection.total_values >= cursor);
+        RETURN_IF_ERROR(skip_values(selection.total_values - cursor));
+        DORIS_CHECK_EQ(output, selection.selected_values);
+        return consumer.consume(_fixed_values.data(), output, value_width);
+    }
+
+    Status set_data(Slice* slice) override {
+        _bit_reader = std::make_shared<BitReader>((const uint8_t*)slice->data, 
slice->size);
+        auto prefix_reader = std::make_shared<BitReader>(*_bit_reader);
+        RETURN_IF_ERROR(_prefix_len_decoder.set_bit_reader(prefix_reader));
+
+        // get the number of encoded prefix lengths
+        int num_prefix = _prefix_len_decoder.valid_values_count();
+        DeltaBitPackDecoder<int32_t> prefix_locator;
+        prefix_locator.set_expected_values(_expected_values);
+        RETURN_IF_ERROR(prefix_locator.set_bit_reader(_bit_reader));
+        constexpr size_t kLocateBatchSize = 4096;
+        std::vector<int32_t> ignored_prefixes(
+                std::min<size_t>(static_cast<size_t>(num_prefix), 
kLocateBatchSize));
+        size_t located = 0;
+        while (located < static_cast<size_t>(num_prefix)) {
+            const size_t batch_size =
+                    std::min(static_cast<size_t>(num_prefix) - located, 
kLocateBatchSize);
+            uint32_t decoded = 0;
+            RETURN_IF_ERROR(prefix_locator.decode(ignored_prefixes.data(),
+                                                  
static_cast<uint32_t>(batch_size), &decoded));
+            if (UNLIKELY(decoded != batch_size)) {
+                return Status::Corruption("Parquet delta prefix stream ended 
early");
+            }
+            located += batch_size;
+        }
+        _num_valid_values = num_prefix;
+
+        // at this time, the decoder_ will be at the start of the encoded 
suffix data.
+        RETURN_IF_ERROR(_suffix_decoder.set_bit_reader(_bit_reader));
+        if (UNLIKELY(_suffix_decoder.valid_values_count() != num_prefix)) {
+            return Status::Corruption("Parquet delta prefix and suffix counts 
differ");
+        }
+
+        // TODO: read corrupted files written with bug(PARQUET-246). 
_last_value should be set
+        // to _last_value_in_previous_page when decoding a new page(except the 
first page)
+        _last_value = "";
+        return Status::OK();
+    }
+
+    Status decode(Slice* buffer, int num_values, int* out_num_values) {
+        return _get_internal(buffer, num_values, out_num_values);
+    }
+
+    void release_scratch(size_t max_retained_bytes) override {
+        release_vector_if_oversized(&_values, max_retained_bytes);
+        release_vector_if_oversized(&_string_refs, max_retained_bytes);
+        release_vector_if_oversized(&_fixed_values, max_retained_bytes);
+        release_vector_if_oversized(&_selected_binary_values, 
max_retained_bytes);
+        release_vector_if_oversized(&_selected_value_sizes, 
max_retained_bytes);
+        release_vector_if_oversized(&_buffered_prefix_length, 
max_retained_bytes);
+        release_vector_if_oversized(&_buffered_data, max_retained_bytes);
+        _prefix_len_decoder.release_scratch(max_retained_bytes);
+        _suffix_decoder.release_scratch(max_retained_bytes);
+        // Prefix reconstruction crosses batch boundaries, so the previous 
value remains semantic
+        // decoder state until every value in the current page has been 
consumed.
+        if (_num_valid_values == 0) {
+            if (_last_value.capacity() > max_retained_bytes) 
std::string().swap(_last_value);
+            if (_last_value_in_previous_page.capacity() > max_retained_bytes) {
+                std::string().swap(_last_value_in_previous_page);
+            }
+        }
+    }
+    size_t retained_scratch_bytes() const override {
+        return _values.capacity() * sizeof(Slice) + _string_refs.capacity() * 
sizeof(StringRef) +
+               _fixed_values.capacity() * sizeof(uint8_t) +
+               _selected_binary_values.capacity() * sizeof(char) +
+               _selected_value_sizes.capacity() * sizeof(size_t) +
+               _buffered_prefix_length.capacity() * sizeof(int32_t) +
+               _buffered_data.capacity() * sizeof(char) + 
_last_value.capacity() +
+               _last_value_in_previous_page.capacity() +
+               _prefix_len_decoder.retained_scratch_bytes() +
+               _suffix_decoder.retained_scratch_bytes();
+    }
+    size_t active_scratch_bytes() const override {
+        return _values.size() * sizeof(Slice) + _string_refs.size() * 
sizeof(StringRef) +
+               _fixed_values.size() * sizeof(uint8_t) +
+               _selected_binary_values.size() * sizeof(char) +
+               _selected_value_sizes.size() * sizeof(size_t) +
+               _buffered_prefix_length.size() * sizeof(int32_t) +
+               _buffered_data.size() * sizeof(char) + _last_value.size() +
+               _last_value_in_previous_page.size() + 
_prefix_len_decoder.active_scratch_bytes() +
+               _suffix_decoder.active_scratch_bytes();
+    }
+
+private:
+    size_t _bounded_reconstruction_batch_size(size_t remaining_values) const {
+        constexpr size_t kReconstructionByteBudget = 4UL << 20;
+        constexpr size_t kMaxReconstructionValues = 4096;
+        // Prefix expansion, rather than encoded page bytes, determines 
reconstruction memory. A
+        // count-based chunk could turn one long value into gigabytes of 
temporary copies, so first
+        // learn its width and then cap subsequent chunks by a byte budget.
+        if (_last_value.empty()) {
+            return 1;
+        }
+        const size_t budgeted_values =
+                std::max<size_t>(1, kReconstructionByteBudget / 
_last_value.size());
+        return std::min({remaining_values, kMaxReconstructionValues, 
budgeted_values});
+    }
+
+    Status _skip_slices(size_t num_values) {
+        _values.resize(num_values);
+        int suffixes_decoded = 0;
+        RETURN_IF_ERROR(_suffix_decoder.decode(_values.data(), 
cast_set<int32_t>(num_values),
+                                               &suffixes_decoded));
+        if (UNLIKELY(suffixes_decoded != num_values)) {
+            return Status::IOError("Expected to skip {} Parquet delta 
suffixes, skipped {}",
+                                   num_values, suffixes_decoded);
+        }
+        _buffered_prefix_length.resize(num_values);
+        uint32_t prefixes_decoded = 0;
+        RETURN_IF_ERROR(_prefix_len_decoder.decode(
+                _buffered_prefix_length.data(), 
cast_set<uint32_t>(num_values), &prefixes_decoded));
+        if (UNLIKELY(prefixes_decoded != num_values)) {
+            return Status::Corruption("Parquet delta prefix stream ended 
early");
+        }
+
+        // Only the preceding value is semantic decoder state. Rebuilding 
every skipped value into
+        // one aggregate buffer would amplify compact full-prefix references 
into page_size * rows.
+        for (size_t row = 0; row < num_values; ++row) {
+            const int32_t encoded_prefix_size = _buffered_prefix_length[row];
+            if (UNLIKELY(encoded_prefix_size < 0)) {
+                return Status::InvalidArgument("negative prefix length in 
DELTA_BYTE_ARRAY");
+            }
+            const size_t prefix_size = 
static_cast<size_t>(encoded_prefix_size);
+            if (UNLIKELY(prefix_size > _last_value.size())) {
+                return Status::InvalidArgument("prefix length too large in 
DELTA_BYTE_ARRAY");
+            }
+            size_t reconstructed_size = 0;
+            if (UNLIKELY(
+                        common::add_overflow(prefix_size, _values[row].size, 
reconstructed_size))) {
+                return Status::InvalidArgument("excess expansion in 
DELTA_BYTE_ARRAY");
+            }
+            if (_type_length > 0 &&
+                UNLIKELY(reconstructed_size != 
static_cast<size_t>(_type_length))) {
+                return Status::Corruption("Parquet fixed value has length {}, 
expected {}",
+                                          reconstructed_size, _type_length);
+            }
+            // resize() preserves the prefix already stored in _last_value, so 
only the suffix
+            // needs copying and repeated full-prefix values allocate no 
additional payload buffer.
+            _last_value.resize(reconstructed_size);
+            if (_values[row].size > 0) {
+                memcpy(_last_value.data() + prefix_size, _values[row].data, 
_values[row].size);
+            }
+        }
+        _num_valid_values -= cast_set<int32_t>(num_values);
+        if (_num_valid_values == 0) {
+            _last_value_in_previous_page = _last_value;
+        }
+        return Status::OK();
+    }
+
+    Status _validate_fixed_width_values() const {
+        if (_type_length <= 0) {
+            return Status::OK();
+        }
+        const size_t value_width = static_cast<size_t>(_type_length);
+        for (const auto& value : _values) {
+            if (UNLIKELY(value.size != value_width)) {
+                return Status::Corruption("Parquet fixed value has length {}, 
expected {}",
+                                          value.size, value_width);
+            }
+        }
+        return Status::OK();
+    }
+
+    Status _decode_slices(size_t num_values) {
+        _values.resize(num_values);
+        int decoded_count = 0;
+        RETURN_IF_ERROR(
+                _get_internal(_values.data(), cast_set<int32_t>(num_values), 
&decoded_count));
+        if (UNLIKELY(decoded_count != num_values)) {
+            return Status::IOError("Expected {} Parquet delta-byte-array 
values, decoded {}",
+                                   num_values, decoded_count);
+        }
+        return Status::OK();
+    }
+
+    Status _get_internal(Slice* buffer, int max_values, int* out_num_values);
+
+    std::vector<Slice> _values;
+    std::vector<StringRef> _string_refs;
+    std::vector<uint8_t> _fixed_values;
+    std::vector<char> _selected_binary_values;
+    std::vector<size_t> _selected_value_sizes;
+    std::shared_ptr<BitReader> _bit_reader;
+    DeltaBitPackDecoder<int32_t> _prefix_len_decoder;
+    DeltaLengthByteArrayDecoder _suffix_decoder;
+    std::string _last_value;
+    // string buffer for last value in previous page
+    std::string _last_value_in_previous_page;
+    int _num_valid_values;
+    std::vector<int32_t> _buffered_prefix_length;
+    std::vector<char> _buffered_data;
+};
+} // namespace doris::format::parquet::native
+
+namespace doris::format::parquet::native {
+
+template <typename T>
+Status DeltaBitPackDecoder<T>::_init_header() {
+    if (!_bit_reader->GetVlqInt(&_values_per_block) ||
+        !_bit_reader->GetVlqInt(&_mini_blocks_per_block) ||
+        !_bit_reader->GetVlqInt(&_total_value_count) ||
+        !_bit_reader->GetZigZagVlqInt(&_last_value)) {
+        return Status::IOError("Init header eof");
+    }
+    if (_values_per_block == 0) {
+        return Status::InvalidArgument("Cannot have zero value per block");
+    }
+    if (_values_per_block % 128 != 0) {
+        return Status::InvalidArgument(
+                "the number of values in a block must be multiple of 128, but 
it's " +
+                std::to_string(_values_per_block));
+    }
+    if (_mini_blocks_per_block == 0) {
+        return Status::InvalidArgument("Cannot have zero miniblock per block");
+    }
+    _values_per_mini_block = _values_per_block / _mini_blocks_per_block;

Review Comment:
   [P1] Reject non-integral DELTA block geometry
   
   The format requires the block size to divide evenly among its miniblocks, 
but this truncating division lets a header such as `3200/33` become `96` and 
pass both current modulo checks. The state machine then starts the next block 
after 3168 rather than 3200 deltas; with zero-width first-block miniblocks and 
a distinct next-block minimum delta, malformed input can silently change the 
last 32 values instead of returning corruption. Please validate 
`_values_per_block % _mini_blocks_per_block == 0` before dividing and cover 
this header shape in the decoder tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to