Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cherry-pick](branch-2.1) add parquet tvf cases and fix some parquet bug #41931

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Prev Previous commit
Next Next commit
[enhance](parquet) Support BYTE_STREAM_SPLIT encoding for parquet rea…
…der (#41683)

## Proposed changes
Impl ByteStreamSplitDecoder to decode BYTE_STREAM_SPLIT encoding
parquet.
relate pr: apache/arrow#42372

> Apache Parquet does not have any encodings suitable for FP data and
the available text compressors (zstd, gzip, etc) do not handle FP data
very well.
It is possible to apply a simple data transformation named "stream
splitting". Such could be "byte stream splitting" which creates K
streams of length N where K is the number of bytes in the data type (4
for floats, 8 for doubles) and N is the number of elements in the
sequence.

---------

Co-authored-by: morningman <morningman@163.com>
  • Loading branch information
suxiaogang223 and morningman committed Oct 16, 2024
commit c476529f5ea10a9a380f1710cc36e0373e95870a
119 changes: 119 additions & 0 deletions be/src/util/byte_stream_split.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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 "byte_stream_split.h"

#include <glog/logging.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'glog/logging.h' file not found [clang-diagnostic-error]

#include <glog/logging.h>
         ^


#include <array>
#include <cstring>
#include <vector>

#include "gutil/port.h"

namespace doris {

inline void do_merge_streams(const uint8_t** src_streams, int width, int64_t nvalues,
uint8_t* dest) {
// Value empirically chosen to provide the best performance on the author's machine
constexpr int kBlockSize = 128;

while (nvalues >= kBlockSize) {
for (int stream = 0; stream < width; ++stream) {
// Take kBlockSize bytes from the given stream and spread them
// to their logical places in destination.
const uint8_t* src = src_streams[stream];
for (int i = 0; i < kBlockSize; i += 8) {
uint64_t v;
std::memcpy(&v, src + i, sizeof(v));
#ifdef IS_LITTLE_ENDIAN
dest[stream + i * width] = static_cast<uint8_t>(v);
dest[stream + (i + 1) * width] = static_cast<uint8_t>(v >> 8);
dest[stream + (i + 2) * width] = static_cast<uint8_t>(v >> 16);
dest[stream + (i + 3) * width] = static_cast<uint8_t>(v >> 24);
dest[stream + (i + 4) * width] = static_cast<uint8_t>(v >> 32);
dest[stream + (i + 5) * width] = static_cast<uint8_t>(v >> 40);
dest[stream + (i + 6) * width] = static_cast<uint8_t>(v >> 48);
dest[stream + (i + 7) * width] = static_cast<uint8_t>(v >> 56);
#elif defined IS_BIG_ENDIAN
dest[stream + i * width] = static_cast<uint8_t>(v >> 56);
dest[stream + (i + 1) * width] = static_cast<uint8_t>(v >> 48);
dest[stream + (i + 2) * width] = static_cast<uint8_t>(v >> 40);
dest[stream + (i + 3) * width] = static_cast<uint8_t>(v >> 32);
dest[stream + (i + 4) * width] = static_cast<uint8_t>(v >> 24);
dest[stream + (i + 5) * width] = static_cast<uint8_t>(v >> 16);
dest[stream + (i + 6) * width] = static_cast<uint8_t>(v >> 8);
dest[stream + (i + 7) * width] = static_cast<uint8_t>(v);
#endif
}
src_streams[stream] += kBlockSize;
}
dest += width * kBlockSize;
nvalues -= kBlockSize;
}

// Epilog
for (int stream = 0; stream < width; ++stream) {
const uint8_t* src = src_streams[stream];
for (int64_t i = 0; i < nvalues; ++i) {
dest[stream + i * width] = src[i];
}
}
}

template <int kNumStreams>
void byte_stream_split_decode_scalar(const uint8_t* src, int width, int64_t offset,
int64_t num_values, int64_t stride, uint8_t* dest) {
DCHECK(width == kNumStreams);
std::array<const uint8_t*, kNumStreams> src_streams;
for (int stream = 0; stream < kNumStreams; ++stream) {
src_streams[stream] = &src[stream * stride + offset];
}
do_merge_streams(src_streams.data(), kNumStreams, num_values, dest);
}

inline void byte_stream_split_decode_scalar_dynamic(const uint8_t* src, int width, int64_t offset,
int64_t num_values, int64_t stride,
uint8_t* dest) {
std::vector<const uint8_t*> src_streams;
src_streams.resize(width);
for (int stream = 0; stream < width; ++stream) {
src_streams[stream] = &src[stream * stride + offset];
}
do_merge_streams(src_streams.data(), width, num_values, dest);
}

// TODO: optimize using simd: https://github.com/apache/arrow/pull/38529
void byte_stream_split_decode(const uint8_t* src, int width, int64_t offset, int64_t num_values,
int64_t stride, uint8_t* dest) {
switch (width) {
case 1:
memcpy(dest, src + offset * width, num_values);
return;
case 2:
return byte_stream_split_decode_scalar<2>(src, width, offset, num_values, stride, dest);
case 4:
return byte_stream_split_decode_scalar<4>(src, width, offset, num_values, stride, dest);
case 8:
return byte_stream_split_decode_scalar<8>(src, width, offset, num_values, stride, dest);
case 16:
return byte_stream_split_decode_scalar<16>(src, width, offset, num_values, stride, dest);
}
return byte_stream_split_decode_scalar_dynamic(src, width, offset, num_values, stride, dest);
}

} // namespace doris
37 changes: 37 additions & 0 deletions be/src/util/byte_stream_split.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 <cstdint>

namespace doris {

/**
* @brief Decode a byte stream into a byte stream split format.
*
* @param src The encoded data by byte stream split.
* @param width The width of type.
* @param offset The offset of encoded data.
* @param num_values The num of values to decode.
* @param stride The length of each stream.
* @param dest The buffer to store the decoded data.
*/
void byte_stream_split_decode(const uint8_t* src, int width, int64_t offset, int64_t num_values,
int64_t stride, uint8_t* dest);

} // namespace doris
95 changes: 95 additions & 0 deletions be/src/vec/exec/format/parquet/byte_stream_split_decoder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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 "byte_stream_split_decoder.h"

#include <cstdint>

#include "util/byte_stream_split.h"

namespace doris::vectorized {

Status ByteStreamSplitDecoder::decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type,
ColumnSelectVector& select_vector,
bool is_dict_filter) {
if (select_vector.has_filter()) {
return _decode_values<true>(doris_column, data_type, select_vector, is_dict_filter);
} else {
return _decode_values<false>(doris_column, data_type, select_vector, is_dict_filter);
}
}

template <bool has_filter>
Status ByteStreamSplitDecoder::_decode_values(MutableColumnPtr& doris_column,
DataTypePtr& data_type,
ColumnSelectVector& select_vector,
bool is_dict_filter) {
size_t non_null_size = select_vector.num_values() - select_vector.num_nulls();
if (UNLIKELY(_offset + non_null_size > _data->size)) {
return Status::IOError(
"Out-of-bounds access in parquet data decoder: offset = {}, non_null_size = "
"{},size = {}",
_offset, non_null_size, _data->size);
}

size_t primitive_length = remove_nullable(data_type)->get_size_of_value_in_memory();
size_t data_index = doris_column->size() * primitive_length;
size_t scale_size = (select_vector.num_values() - select_vector.num_filtered()) *
(_type_length / primitive_length);
doris_column->resize(doris_column->size() + scale_size);
char* raw_data = const_cast<char*>(doris_column->get_raw_data().data);
ColumnSelectVector::DataReadType read_type;
DCHECK(_data->get_size() % _type_length == 0);
int64_t stride = _data->get_size() / _type_length;

while (size_t run_length = select_vector.get_next_run<has_filter>(&read_type)) {
switch (read_type) {
case ColumnSelectVector::CONTENT: {
byte_stream_split_decode(reinterpret_cast<const uint8_t*>(_data->get_data()),
_type_length, _offset / _type_length, run_length, stride,
reinterpret_cast<uint8_t*>(raw_data) + data_index);
_offset += run_length * _type_length;
data_index += run_length * _type_length;
break;
}
case ColumnSelectVector::NULL_DATA: {
data_index += run_length * _type_length;
break;
}
case ColumnSelectVector::FILTERED_CONTENT: {
_offset += _type_length * run_length;
break;
}
case ColumnSelectVector::FILTERED_NULL: {
// do nothing
break;
}
}
}
return Status::OK();
}

Status ByteStreamSplitDecoder::skip_values(size_t num_values) {
_offset += _type_length * num_values;
if (UNLIKELY(_offset > _data->size)) {
return Status::IOError(
"Out-of-bounds access in parquet data decoder: offset = {}, size = {}", _offset,
_data->size);
}
return Status::OK();
}
}; // namespace doris::vectorized
38 changes: 38 additions & 0 deletions be/src/vec/exec/format/parquet/byte_stream_split_decoder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 "vec/exec/format/parquet/decoder.h"

namespace doris::vectorized {
class ByteStreamSplitDecoder final : public Decoder {
public:
ByteStreamSplitDecoder() = default;
~ByteStreamSplitDecoder() override = default;

Status decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type,
ColumnSelectVector& select_vector, bool is_dict_filter) override;

template <bool has_filter>
Status _decode_values(MutableColumnPtr& doris_column, DataTypePtr& data_type,
ColumnSelectVector& select_vector, bool is_dict_filter);

Status skip_values(size_t num_values) override;
};

} // namespace doris::vectorized
17 changes: 16 additions & 1 deletion be/src/vec/exec/format/parquet/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include "vec/exec/format/parquet/bool_rle_decoder.h"
#include "vec/exec/format/parquet/byte_array_dict_decoder.h"
#include "vec/exec/format/parquet/byte_array_plain_decoder.h"
#include "vec/exec/format/parquet/byte_stream_split_decoder.h"
#include "vec/exec/format/parquet/delta_bit_pack_decoder.h"
#include "vec/exec/format/parquet/fix_length_dict_decoder.hpp"
#include "vec/exec/format/parquet/fix_length_plain_decoder.h"
#include "vec/exec/format/parquet/schema_desc.h"

namespace doris::vectorized {

Expand Down Expand Up @@ -118,6 +118,21 @@ Status Decoder::get_decoder(tparquet::Type::type type, tparquet::Encoding::type
return Status::InternalError("DELTA_LENGTH_BYTE_ARRAY only supports BYTE_ARRAY.");
}
break;
case tparquet::Encoding::BYTE_STREAM_SPLIT:
switch (type) {
case tparquet::Type::INT32:
case tparquet::Type::INT64:
case tparquet::Type::INT96:
case tparquet::Type::FLOAT:
case tparquet::Type::DOUBLE:
case tparquet::Type::FIXED_LEN_BYTE_ARRAY:
decoder.reset(new ByteStreamSplitDecoder());
break;
default:
return Status::InternalError("Unsupported type {}(encoding={}) in parquet decoder",
tparquet::to_string(type), tparquet::to_string(encoding));
}
break;
default:
return Status::InternalError("Unsupported encoding {}(type={}) in parquet decoder",
tparquet::to_string(encoding), tparquet::to_string(type));
Expand Down
Loading
Loading