AshinGau commented on code in PR #17112: URL: https://github.com/apache/doris/pull/17112#discussion_r1124241789
########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,381 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/columns/column.h" +#include "vec/exec/format/parquet/decoder.h" + +namespace doris::vectorized { +/** + * 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 Decoder { +public: + using UT = std::make_unsigned_t<T>; + + DeltaBitPackDecoder(const tparquet::Type::type& physical_type) + : _physical_type(physical_type) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + + // set value + TypeIndex logical_type = remove_nullable(data_type)->get_type_id(); + switch (logical_type) { +#define DISPATCH(NUMERIC_TYPE, CPP_NUMERIC_TYPE, PHYSICAL_TYPE) \ + case NUMERIC_TYPE: \ + if constexpr (std::is_same_v<T, PHYSICAL_TYPE>) { \ + return _decode_numeric<CPP_NUMERIC_TYPE>(doris_column, select_vector); \ + } + FOR_LOGICAL_NUMERIC_TYPES(DISPATCH) +#undef DISPATCH + case TypeIndex::DateTime: + return _decode_datetime64<VecDateTimeValue, Int64>(doris_column, select_vector); + case TypeIndex::DateTimeV2: + return _decode_datetime64<DateV2Value<DateTimeV2ValueType>, UInt64>(doris_column, + select_vector); + default: + break; + } + return Status::InvalidArgument( + "Can't decode parquet physical type {} to doris logical type {}", + tparquet::to_string(_physical_type), getTypeName(logical_type)); + } + template <typename Numeric> + Status _decode_numeric(MutableColumnPtr& doris_column, ColumnSelectVector& select_vector) { + auto& column_data = static_cast<ColumnVector<Numeric>&>(*doris_column).get_data(); + size_t data_index = column_data.size(); + column_data.resize(data_index + select_vector.num_values() - select_vector.num_filtered()); + ColumnSelectVector::DataReadType read_type; + while (size_t run_length = select_vector.get_next_run(&read_type)) { + switch (read_type) { + case ColumnSelectVector::CONTENT: { + for (size_t i = 0; i < run_length; ++i) { + column_data[data_index++] = static_cast<Numeric>(_values[i++]); + } + break; + } + case ColumnSelectVector::NULL_DATA: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_CONTENT: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_NULL: { + // do nothing + break; + } + } + } + return Status::OK(); + } + + template <typename CppType, typename ColumnType> + Status _decode_datetime64(MutableColumnPtr& doris_column, ColumnSelectVector& select_vector) { + auto& column_data = static_cast<ColumnVector<ColumnType>&>(*doris_column).get_data(); + size_t data_index = column_data.size(); + column_data.resize(data_index + select_vector.num_values() - select_vector.num_filtered()); + ColumnSelectVector::DataReadType read_type; + while (size_t run_length = select_vector.get_next_run(&read_type)) { + switch (read_type) { + case ColumnSelectVector::CONTENT: { + for (size_t i = 0; i < run_length; ++i) { + int64_t date_value = _values[i++]; + auto& v = reinterpret_cast<CppType&>(column_data[data_index++]); + v.from_unixtime(date_value / _decode_params->second_mask, *_decode_params->ctz); + if constexpr (std::is_same_v<CppType, DateV2Value<DateTimeV2ValueType>>) { + // nanoseconds will be ignored. + v.set_microsecond((date_value % _decode_params->second_mask) * + _decode_params->scale_to_nano_factor / 1000); + } + } + break; + } + case ColumnSelectVector::NULL_DATA: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_CONTENT: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_NULL: { + // do nothing + break; + } + } + } + return Status::OK(); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + Status skip_values(size_t num_values) override { + return Status::OK(); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); + _data = slice; + _offset = 0; + } + + // Set BitReader which is already initialized by DeltaLengthByteArrayDecoder or + // DeltaByteArrayDecoder + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _init_header(); + } + +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, int max_values, int* out_num_values); + + tparquet::Type::type _physical_type; + std::vector<T> _values; + + std::shared_ptr<BatchedBitReader> _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; +}; +template class DeltaBitPackDecoder<int32_t>; +template class DeltaBitPackDecoder<int64_t>; + +class DeltaLengthByteArrayDecoder final : public Decoder { +public: + explicit DeltaLengthByteArrayDecoder(const tparquet::Type::type& physical_type) + : _len_decoder(physical_type), _buffered_length(0), _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + std::vector<Slice> values(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(values.data(), num_values - null_count, &num_valid_values)); + + if (PREDICT_FALSE(num_values - null_count != num_valid_values)) { + return Status::EndOfFile("Expected to decode ", num_values - null_count, + " values, but decoded ", num_valid_values, " values."); + } + + return Status::OK(); + } + + Status decode(Slice* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + void set_data(Slice* slice) override { + if (slice->size == 0) { + return; + } + _bit_reader = std::make_shared<BatchedBitReader>((const uint8_t*)slice->data, slice->size); + _data = slice; + _offset = 0; + _decode_lengths(); + } + + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { Review Comment: ```suggestion void set_bit_reader(std::shared_ptr<BatchedBitReader> bit_reader) { ``` ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,264 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/exec/format/parquet/fix_length_plain_decoder.h" + +namespace doris::vectorized { + +class DeltaDecoder : public Decoder { +public: + DeltaDecoder(FixLengthPlainDecoder* fix_plain_decoder) { + _fix_plain_decoder.reset(fix_plain_decoder); + } + ~DeltaDecoder() override = default; + Status skip_values(size_t num_values) override { + return _fix_plain_decoder->skip_values(num_values); + } + +protected: + void init_values_converter() { + _fix_plain_decoder->set_data(_data); + _fix_plain_decoder->set_type_length(_type_length); + _fix_plain_decoder->init(_field_schema, _decode_params->ctz); + } + std::unique_ptr<FixLengthPlainDecoder> _fix_plain_decoder; +}; + +/** + * 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(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + _data->data = reinterpret_cast<char*>(_values.data()); + _type_length = sizeof(T); + _data->size = _values.size() * _type_length; + // set decoded value with fix plain decoder + init_values_converter(); + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); + _data = slice; + _offset = 0; + } + + // Set BitReader which is already initialized by DeltaLengthByteArrayDecoder or + // DeltaByteArrayDecoder + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _init_header(); + } + +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, int max_values, int* out_num_values); + + std::vector<T> _values; + + std::shared_ptr<BatchedBitReader> _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; +}; +template class DeltaBitPackDecoder<int32_t>; +template class DeltaBitPackDecoder<int64_t>; + +class DeltaLengthByteArrayDecoder final : public DeltaDecoder { +public: + explicit DeltaLengthByteArrayDecoder(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)), + _len_decoder(physical_type), + _buffered_length(0), + _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + _values.resize(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(_values.data(), num_values - null_count, &num_valid_values)); + + if (PREDICT_FALSE(num_values - null_count != num_valid_values)) { + return Status::EndOfFile("Expected to decode ", num_values - null_count, Review Comment: use `IOError`, use `("Expected to decode {}", num_values - null_count, ...)` style. ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.cpp: ########## @@ -0,0 +1,266 @@ +// 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. + +#include "delta_bit_pack_decoder.h" + +namespace doris::vectorized { + +template <typename T> +Status DeltaBitPackDecoder<T>::_init_header() { + if (!_bit_reader->GetUleb128<uint32_t>(&_values_per_block) || + !_bit_reader->GetUleb128<uint32_t>(&_mini_blocks_per_block) || + !_bit_reader->GetUleb128<uint32_t>(&_total_value_count) || + !_bit_reader->GetZigZagInteger(&_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; + if (_values_per_mini_block == 0) { + return Status::InvalidArgument("Cannot have zero value per miniblock"); + } + if (_values_per_mini_block % 32 != 0) { + return Status::InvalidArgument( + "The number of values in a miniblock must be multiple of 32, but it's " + + std::to_string(_values_per_mini_block)); + } + _total_values_remaining = _total_value_count; + // init as empty property + _block_initialized = false; + _values_remaining_current_mini_block = 0; + return Status::OK(); +} + +template <typename T> +Status DeltaBitPackDecoder<T>::_init_block() { + DCHECK_GT(_total_values_remaining, 0) << "InitBlock called at EOF"; + if (!_bit_reader->GetZigZagInteger(&_min_delta)) { + return Status::IOError("Init block eof"); + } + + // read the bitwidth of each miniblock + _delta_bit_widths.resize(_mini_blocks_per_block); + uint8_t* bit_width_data = _delta_bit_widths.data(); + for (uint32_t i = 0; i < _mini_blocks_per_block; ++i) { + if (!_bit_reader->GetBytes<uint8_t>(1, bit_width_data + i)) { + return Status::IOError("Decode bit-width EOF"); + } + // Note that non-conformant bitwidth entries are allowed by the Parquet spec + // for extraneous miniblocks in the last block (GH-14923), so we check + // the bitwidths when actually using them (see InitMiniBlock()). + } + _mini_block_idx = 0; + _block_initialized = true; + _init_mini_block(bit_width_data[0]); + return Status::OK(); +} + +template <typename T> +Status DeltaBitPackDecoder<T>::_init_mini_block(int bit_width) { + if (bit_width > kMaxDeltaBitWidth) { + return Status::InvalidArgument("delta bit width larger than integer bit width"); + } + _delta_bit_width = bit_width; + _values_remaining_current_mini_block = _values_per_mini_block; + return Status::OK(); +} + +template <typename T> +Status DeltaBitPackDecoder<T>::_get_internal(T* buffer, int num_values, int* out_num_values) { + num_values = static_cast<int>(std::min<int64_t>(num_values, _total_values_remaining)); + if (num_values == 0) { + *out_num_values = 0; + return Status::OK(); + } + int i = 0; + while (i < num_values) { + if (_values_remaining_current_mini_block == 0) { + if (!_block_initialized) { + buffer[i++] = _last_value; + DCHECK_EQ(i, 1); // we're at the beginning of the page + if (i == num_values) { + // When block is uninitialized and i reaches num_values we have two + // different possibilities: + // 1. _total_value_count == 1, which means that the page may have only + // one value (encoded in the header), and we should not initialize + // any block. + // 2. _total_value_count != 1, which means we should initialize the + // incoming block for subsequent reads. + if (_total_value_count != 1) { + RETURN_IF_ERROR(_init_block()); + } + break; + } + RETURN_IF_ERROR(_init_block()); + } else { + ++_mini_block_idx; + if (_mini_block_idx < _mini_blocks_per_block) { + RETURN_IF_ERROR(_init_mini_block(_delta_bit_widths.data()[_mini_block_idx])); + } else { + RETURN_IF_ERROR(_init_block()); + } + } + } + + int values_decode = std::min(_values_remaining_current_mini_block, + static_cast<uint32_t>(num_values - i)); + if (!_bit_reader->UnpackBatch(_delta_bit_width, values_decode, + reinterpret_cast<UT*>(buffer + i))) { + return Status::IOError("Get batch EOF"); + } + for (int j = 0; j < values_decode; ++j) { + // Addition between min_delta, packed int and last_value should be treated as + // unsigned addition. Overflow is as expected. + buffer[i + j] = static_cast<UT>(_min_delta) + static_cast<UT>(buffer[i + j]) + + static_cast<UT>(_last_value); + _last_value = buffer[i + j]; + } + _values_remaining_current_mini_block -= values_decode; + i += values_decode; + } + _total_values_remaining -= num_values; + + if (_total_values_remaining == 0) { + // TODO: The Slice to be decoded will not reuse so don't need skip the padding bits, + // but we can also skip them for the robustness. + _values_remaining_current_mini_block = 0; + } + *out_num_values = num_values; + return Status::OK(); +} + +void DeltaLengthByteArrayDecoder::_decode_lengths() { + _len_decoder.set_decoder(_bit_reader); + // get the number of encoded lengths + int num_length = _len_decoder.valid_values_count(); + _buffered_length.resize(num_length * sizeof(int32_t)); + + // decode all the lengths. all the lengths are buffered in buffered_length_. + int ret; + _len_decoder.decode(reinterpret_cast<int32_t*>(_buffered_length.data()), num_length, &ret); + DCHECK_EQ(ret, num_length); + _length_idx = 0; + _num_valid_values = num_length; +} + +Status DeltaLengthByteArrayDecoder::_get_internal(Slice* buffer, int max_values, + int* out_num_values) { + // Decode up to `max_values` strings into an internal buffer + // and reference them into `buffer`. + max_values = std::min(max_values, _num_valid_values); + if (max_values == 0) { + *out_num_values = 0; + return Status::OK(); + } + + int32_t data_size = 0; + const int32_t* length_ptr = + reinterpret_cast<const int32_t*>(_buffered_length.data()) + _length_idx; + for (int i = 0; i < max_values; ++i) { + int32_t len = length_ptr[i]; + if (PREDICT_FALSE(len < 0)) { + return Status::EndOfFile("Negative string delta length"); Review Comment: Use InternalError/IOError ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,381 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/columns/column.h" +#include "vec/exec/format/parquet/decoder.h" + +namespace doris::vectorized { +/** + * 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 Decoder { +public: + using UT = std::make_unsigned_t<T>; + + DeltaBitPackDecoder(const tparquet::Type::type& physical_type) + : _physical_type(physical_type) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + + // set value + TypeIndex logical_type = remove_nullable(data_type)->get_type_id(); + switch (logical_type) { +#define DISPATCH(NUMERIC_TYPE, CPP_NUMERIC_TYPE, PHYSICAL_TYPE) \ + case NUMERIC_TYPE: \ + if constexpr (std::is_same_v<T, PHYSICAL_TYPE>) { \ + return _decode_numeric<CPP_NUMERIC_TYPE>(doris_column, select_vector); \ + } + FOR_LOGICAL_NUMERIC_TYPES(DISPATCH) +#undef DISPATCH + case TypeIndex::DateTime: + return _decode_datetime64<VecDateTimeValue, Int64>(doris_column, select_vector); + case TypeIndex::DateTimeV2: + return _decode_datetime64<DateV2Value<DateTimeV2ValueType>, UInt64>(doris_column, + select_vector); + default: + break; + } + return Status::InvalidArgument( + "Can't decode parquet physical type {} to doris logical type {}", + tparquet::to_string(_physical_type), getTypeName(logical_type)); + } + template <typename Numeric> + Status _decode_numeric(MutableColumnPtr& doris_column, ColumnSelectVector& select_vector) { + auto& column_data = static_cast<ColumnVector<Numeric>&>(*doris_column).get_data(); + size_t data_index = column_data.size(); + column_data.resize(data_index + select_vector.num_values() - select_vector.num_filtered()); + ColumnSelectVector::DataReadType read_type; + while (size_t run_length = select_vector.get_next_run(&read_type)) { + switch (read_type) { + case ColumnSelectVector::CONTENT: { + for (size_t i = 0; i < run_length; ++i) { + column_data[data_index++] = static_cast<Numeric>(_values[i++]); + } + break; + } + case ColumnSelectVector::NULL_DATA: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_CONTENT: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_NULL: { + // do nothing + break; + } + } + } + return Status::OK(); + } + + template <typename CppType, typename ColumnType> + Status _decode_datetime64(MutableColumnPtr& doris_column, ColumnSelectVector& select_vector) { + auto& column_data = static_cast<ColumnVector<ColumnType>&>(*doris_column).get_data(); + size_t data_index = column_data.size(); + column_data.resize(data_index + select_vector.num_values() - select_vector.num_filtered()); + ColumnSelectVector::DataReadType read_type; + while (size_t run_length = select_vector.get_next_run(&read_type)) { + switch (read_type) { + case ColumnSelectVector::CONTENT: { + for (size_t i = 0; i < run_length; ++i) { + int64_t date_value = _values[i++]; + auto& v = reinterpret_cast<CppType&>(column_data[data_index++]); + v.from_unixtime(date_value / _decode_params->second_mask, *_decode_params->ctz); + if constexpr (std::is_same_v<CppType, DateV2Value<DateTimeV2ValueType>>) { + // nanoseconds will be ignored. + v.set_microsecond((date_value % _decode_params->second_mask) * + _decode_params->scale_to_nano_factor / 1000); + } + } + break; + } + case ColumnSelectVector::NULL_DATA: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_CONTENT: { + data_index += run_length; + break; + } + case ColumnSelectVector::FILTERED_NULL: { + // do nothing + break; + } + } + } + return Status::OK(); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + Status skip_values(size_t num_values) override { + return Status::OK(); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); Review Comment: return if error ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,264 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/exec/format/parquet/fix_length_plain_decoder.h" + +namespace doris::vectorized { + +class DeltaDecoder : public Decoder { +public: + DeltaDecoder(FixLengthPlainDecoder* fix_plain_decoder) { + _fix_plain_decoder.reset(fix_plain_decoder); + } + ~DeltaDecoder() override = default; + Status skip_values(size_t num_values) override { + return _fix_plain_decoder->skip_values(num_values); + } + +protected: + void init_values_converter() { + _fix_plain_decoder->set_data(_data); + _fix_plain_decoder->set_type_length(_type_length); + _fix_plain_decoder->init(_field_schema, _decode_params->ctz); + } + std::unique_ptr<FixLengthPlainDecoder> _fix_plain_decoder; +}; + +/** + * 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(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + _data->data = reinterpret_cast<char*>(_values.data()); + _type_length = sizeof(T); + _data->size = _values.size() * _type_length; + // set decoded value with fix plain decoder + init_values_converter(); + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); + _data = slice; + _offset = 0; + } + + // Set BitReader which is already initialized by DeltaLengthByteArrayDecoder or + // DeltaByteArrayDecoder + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { Review Comment: ```suggestion void set_bit_reader(std::shared_ptr<BatchedBitReader> bit_reader) { ``` ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,264 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/exec/format/parquet/fix_length_plain_decoder.h" + +namespace doris::vectorized { + +class DeltaDecoder : public Decoder { +public: + DeltaDecoder(FixLengthPlainDecoder* fix_plain_decoder) { + _fix_plain_decoder.reset(fix_plain_decoder); + } + ~DeltaDecoder() override = default; + Status skip_values(size_t num_values) override { + return _fix_plain_decoder->skip_values(num_values); + } + +protected: + void init_values_converter() { + _fix_plain_decoder->set_data(_data); + _fix_plain_decoder->set_type_length(_type_length); + _fix_plain_decoder->init(_field_schema, _decode_params->ctz); + } + std::unique_ptr<FixLengthPlainDecoder> _fix_plain_decoder; +}; + +/** + * 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(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + _data->data = reinterpret_cast<char*>(_values.data()); + _type_length = sizeof(T); + _data->size = _values.size() * _type_length; + // set decoded value with fix plain decoder + init_values_converter(); + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); + _data = slice; + _offset = 0; + } + + // Set BitReader which is already initialized by DeltaLengthByteArrayDecoder or + // DeltaByteArrayDecoder + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _init_header(); + } + +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, int max_values, int* out_num_values); + + std::vector<T> _values; + + std::shared_ptr<BatchedBitReader> _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; +}; +template class DeltaBitPackDecoder<int32_t>; +template class DeltaBitPackDecoder<int64_t>; + +class DeltaLengthByteArrayDecoder final : public DeltaDecoder { +public: + explicit DeltaLengthByteArrayDecoder(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)), + _len_decoder(physical_type), + _buffered_length(0), + _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + _values.resize(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(_values.data(), num_values - null_count, &num_valid_values)); + + if (PREDICT_FALSE(num_values - null_count != num_valid_values)) { + return Status::EndOfFile("Expected to decode ", num_values - null_count, + " values, but decoded ", num_valid_values, " values."); + } + _data = _values.data(); + _type_length = sizeof(_field_schema->parquet_schema.type_length); Review Comment: why not use `_type_length = _field_schema->parquet_schema.type_length` ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,264 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/exec/format/parquet/fix_length_plain_decoder.h" + +namespace doris::vectorized { + +class DeltaDecoder : public Decoder { +public: + DeltaDecoder(FixLengthPlainDecoder* fix_plain_decoder) { + _fix_plain_decoder.reset(fix_plain_decoder); + } + ~DeltaDecoder() override = default; + Status skip_values(size_t num_values) override { + return _fix_plain_decoder->skip_values(num_values); + } + +protected: + void init_values_converter() { + _fix_plain_decoder->set_data(_data); + _fix_plain_decoder->set_type_length(_type_length); + _fix_plain_decoder->init(_field_schema, _decode_params->ctz); + } + std::unique_ptr<FixLengthPlainDecoder> _fix_plain_decoder; +}; + +/** + * 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(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + _data->data = reinterpret_cast<char*>(_values.data()); + _type_length = sizeof(T); + _data->size = _values.size() * _type_length; + // set decoded value with fix plain decoder + init_values_converter(); + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); + _data = slice; + _offset = 0; + } + + // Set BitReader which is already initialized by DeltaLengthByteArrayDecoder or + // DeltaByteArrayDecoder + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _init_header(); + } + +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, int max_values, int* out_num_values); + + std::vector<T> _values; + + std::shared_ptr<BatchedBitReader> _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; +}; +template class DeltaBitPackDecoder<int32_t>; +template class DeltaBitPackDecoder<int64_t>; + +class DeltaLengthByteArrayDecoder final : public DeltaDecoder { +public: + explicit DeltaLengthByteArrayDecoder(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)), + _len_decoder(physical_type), + _buffered_length(0), + _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + _values.resize(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(_values.data(), num_values - null_count, &num_valid_values)); + + if (PREDICT_FALSE(num_values - null_count != num_valid_values)) { + return Status::EndOfFile("Expected to decode ", num_values - null_count, + " values, but decoded ", num_valid_values, " values."); + } + _data = _values.data(); + _type_length = sizeof(_field_schema->parquet_schema.type_length); + _data->size = _values.size() * _type_length; + init_values_converter(); + + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(Slice* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + void set_data(Slice* slice) override { + if (slice->size == 0) { + return; + } + _bit_reader = std::make_shared<BatchedBitReader>((const uint8_t*)slice->data, slice->size); + _data = slice; + _offset = 0; + _decode_lengths(); + } + + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _decode_lengths(); + } + +private: + // Decode all the encoded lengths. The decoder_ will be at the start of the encoded data + // after that. + void _decode_lengths(); + Status _get_internal(Slice* buffer, int max_values, int* out_num_values); + + std::vector<Slice> _values; + std::shared_ptr<BatchedBitReader> _bit_reader; + DeltaBitPackDecoder<int32_t> _len_decoder; + + int _num_valid_values; + uint32_t _length_idx; + std::vector<uint8_t> _buffered_length; + std::vector<uint8_t> _buffered_data; +}; + +class DeltaByteArrayDecoder : public DeltaDecoder { +public: + explicit DeltaByteArrayDecoder(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)), + _prefix_len_decoder(physical_type), + _suffix_decoder(physical_type), + _last_value_in_previous_page(""), + _buffered_prefix_length(0), + _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + _values.resize(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(_values.data(), num_values - null_count, &num_valid_values)); + DCHECK_EQ(num_values - null_count, num_valid_values); + + _data = _values.data(); + _type_length = sizeof(_field_schema->parquet_schema.type_length); + _data->size = _values.size() * _type_length; + init_values_converter(); + + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); Review Comment: I think should use `ByteArrayPlainDecoder` ########## be/src/vec/exec/format/parquet/fix_length_plain_decoder.cpp: ########## @@ -111,7 +111,8 @@ Status FixLengthPlainDecoder::decode_values(MutableColumnPtr& doris_column, Data case TypeIndex::String: [[fallthrough]]; case TypeIndex::FixedString: - if (_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY) { + if (_physical_type == tparquet::Type::FIXED_LEN_BYTE_ARRAY || + _physical_type == tparquet::Type::BYTE_ARRAY) { Review Comment: `BYTE_ARRAY` is not a fixed type. ########## be/src/vec/exec/format/parquet/delta_bit_pack_decoder.h: ########## @@ -0,0 +1,264 @@ +// 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 "util/bit_stream_utils.h" +#include "vec/exec/format/parquet/fix_length_plain_decoder.h" + +namespace doris::vectorized { + +class DeltaDecoder : public Decoder { +public: + DeltaDecoder(FixLengthPlainDecoder* fix_plain_decoder) { + _fix_plain_decoder.reset(fix_plain_decoder); + } + ~DeltaDecoder() override = default; + Status skip_values(size_t num_values) override { + return _fix_plain_decoder->skip_values(num_values); + } + +protected: + void init_values_converter() { + _fix_plain_decoder->set_data(_data); + _fix_plain_decoder->set_type_length(_type_length); + _fix_plain_decoder->init(_field_schema, _decode_params->ctz); + } + std::unique_ptr<FixLengthPlainDecoder> _fix_plain_decoder; +}; + +/** + * 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(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)) {} + ~DeltaBitPackDecoder() override = default; + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t non_null_size = select_vector.num_values() - select_vector.num_nulls(); + // decode values + _values.resize(non_null_size); + int decoded_count = 0; + RETURN_IF_ERROR(_get_internal(_values.data(), non_null_size, &decoded_count)); + _data->data = reinterpret_cast<char*>(_values.data()); + _type_length = sizeof(T); + _data->size = _values.size() * _type_length; + // set decoded value with fix plain decoder + init_values_converter(); + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(T* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + int valid_values_count() { + // _total_value_count in header ignores of null values + return static_cast<int>(_total_value_count); + } + + void set_data(Slice* slice) override { + _bit_reader.reset(new BatchedBitReader((const uint8_t*)slice->data, slice->size)); + _init_header(); + _data = slice; + _offset = 0; + } + + // Set BitReader which is already initialized by DeltaLengthByteArrayDecoder or + // DeltaByteArrayDecoder + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _init_header(); + } + +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, int max_values, int* out_num_values); + + std::vector<T> _values; + + std::shared_ptr<BatchedBitReader> _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; +}; +template class DeltaBitPackDecoder<int32_t>; +template class DeltaBitPackDecoder<int64_t>; + +class DeltaLengthByteArrayDecoder final : public DeltaDecoder { +public: + explicit DeltaLengthByteArrayDecoder(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)), + _len_decoder(physical_type), + _buffered_length(0), + _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + _values.resize(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(_values.data(), num_values - null_count, &num_valid_values)); + + if (PREDICT_FALSE(num_values - null_count != num_valid_values)) { + return Status::EndOfFile("Expected to decode ", num_values - null_count, + " values, but decoded ", num_valid_values, " values."); + } + _data = _values.data(); + _type_length = sizeof(_field_schema->parquet_schema.type_length); + _data->size = _values.size() * _type_length; + init_values_converter(); + + return _fix_plain_decoder->decode_values(doris_column, data_type, select_vector); + } + + Status decode(Slice* buffer, int num_values, int* out_num_values) { + return _get_internal(buffer, num_values, out_num_values); + } + + void set_data(Slice* slice) override { + if (slice->size == 0) { + return; + } + _bit_reader = std::make_shared<BatchedBitReader>((const uint8_t*)slice->data, slice->size); + _data = slice; + _offset = 0; + _decode_lengths(); + } + + void set_decoder(std::shared_ptr<BatchedBitReader> bit_reader) { + _bit_reader = std::move(bit_reader); + _decode_lengths(); + } + +private: + // Decode all the encoded lengths. The decoder_ will be at the start of the encoded data + // after that. + void _decode_lengths(); + Status _get_internal(Slice* buffer, int max_values, int* out_num_values); + + std::vector<Slice> _values; + std::shared_ptr<BatchedBitReader> _bit_reader; + DeltaBitPackDecoder<int32_t> _len_decoder; + + int _num_valid_values; + uint32_t _length_idx; + std::vector<uint8_t> _buffered_length; + std::vector<uint8_t> _buffered_data; +}; + +class DeltaByteArrayDecoder : public DeltaDecoder { +public: + explicit DeltaByteArrayDecoder(const tparquet::Type::type& physical_type) + : DeltaDecoder(new FixLengthPlainDecoder(physical_type)), + _prefix_len_decoder(physical_type), + _suffix_decoder(physical_type), + _last_value_in_previous_page(""), + _buffered_prefix_length(0), + _buffered_data(0) {} + + Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type, + ColumnSelectVector& select_vector) override { + size_t num_values = select_vector.num_values(); + size_t null_count = select_vector.num_nulls(); + _values.resize(num_values - null_count); + int num_valid_values; + RETURN_IF_ERROR(_get_internal(_values.data(), num_values - null_count, &num_valid_values)); + DCHECK_EQ(num_values - null_count, num_valid_values); + + _data = _values.data(); + _type_length = sizeof(_field_schema->parquet_schema.type_length); Review Comment: Is `BYTE_ARRAY` fixed length type ? and why use `sizof`? -- 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]
