Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions cpp/src/parquet/column_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "parquet/encryption/encryption_internal.h"
#include "parquet/encryption/internal_file_decryptor.h"
#include "parquet/exception.h"
#include "parquet/level_comparison.h"
#include "parquet/level_conversion.h"
#include "parquet/level_decoder_internal.h"
#include "parquet/properties.h"
Expand Down Expand Up @@ -1566,7 +1567,7 @@ class TypedColumnReaderImpl
// cache-efficiency if fused with the level decoding.
ARROW_DCHECK_LE(*num_def_levels, std::numeric_limits<int32_t>::max());
*non_null_values_to_read += static_cast<int32_t>(
std::count(def_levels, def_levels + *num_def_levels, this->max_def_level()));
internal::CountLevels(def_levels, *num_def_levels, this->max_def_level()));
} else {
// Required field, read all values
if (num_def_levels != nullptr) {
Expand Down Expand Up @@ -1981,8 +1982,8 @@ int64_t TypedRecordReader<DT, VS>::SkipRecordsInBufferNonRepeated(int64_t num_re
// we do not have a buffer, so we need to read them and throw them away.
// First we need to figure out how many present/not-null values there are.
const auto values_to_read = static_cast<int32_t>(
std::count(def_levels() + levels_position_, def_levels() + remaining_levels_pos,
this->max_def_level()));
CountLevels(def_levels() + levels_position_,
remaining_levels_pos - levels_position_, this->max_def_level()));
Comment on lines +1985 to +1986

// Now that we have figured out number of values to read, we do not need
// these levels anymore. We will remove these values from the buffer.
Expand Down Expand Up @@ -2147,9 +2148,7 @@ int64_t TypedRecordReader<DT, VS>::DelimitRecords(int64_t num_records,
const int64_t stride =
std::min(levels_written() - levels_position_, num_records - records_read);
const int64_t position_end = levels_position_ + stride;
for (int64_t i = levels_position_; i < position_end; ++i) {
records_read += rep_levels[i] == 0;
}
records_read += CountLevels(rep_levels + levels_position_, stride, 0);
levels_position_ = position_end;
if (records_read == num_records) {
// Check last rep_level reaches the boundary and
Expand All @@ -2163,8 +2162,8 @@ int64_t TypedRecordReader<DT, VS>::DelimitRecords(int64_t num_records,
}
}
// Scan definition levels to find number of physical values
*values_seen = std::count(def_levels + level, def_levels + levels_position_,
this->max_def_level());
*values_seen =
CountLevels(def_levels + level, levels_position_ - level, this->max_def_level());
Comment on lines +2165 to +2166
Comment on lines +2165 to +2166
return records_read;
}

Expand Down Expand Up @@ -2253,8 +2252,9 @@ void TypedRecordReader<DT, VS>::ReadDenseForOptionalInBuffer(

// When reading dense we need to figure out number of values to read.
const int16_t* def_levels = this->def_levels();
*values_to_read += std::count(def_levels + start_levels_position,
def_levels + levels_position_, this->max_def_level());
*values_to_read +=
CountLevels(def_levels + start_levels_position,
levels_position_ - start_levels_position, this->max_def_level());
Comment on lines +2256 to +2257
// This is only reading in the current page so this fits in an int32.
value_sink_.ReadValuesDense(*this->current_decoder_.get(),
clamp_to<int32_t>(*values_to_read));
Expand Down
35 changes: 13 additions & 22 deletions cpp/src/parquet/column_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "parquet/encoding.h"
#include "parquet/encryption/encryption_internal.h"
#include "parquet/encryption/internal_file_encryptor.h"
#include "parquet/level_comparison.h"
#include "parquet/level_conversion.h"
#include "parquet/metadata.h"
#include "parquet/page_index.h"
Expand Down Expand Up @@ -1698,11 +1699,8 @@ class TypedColumnWriterImpl : public ColumnWriterImpl,
int64_t values_to_write = 0;
// If the field is required and non-repeated, there are no definition levels
if (descr_->max_definition_level() > 0) {
for (int64_t i = 0; i < num_levels; ++i) {
if (def_levels[i] == descr_->max_definition_level()) {
++values_to_write;
}
}
values_to_write =
internal::CountLevels(def_levels, num_levels, descr_->max_definition_level());

WriteDefinitionLevels(num_levels, def_levels);
} else {
Expand All @@ -1714,12 +1712,9 @@ class TypedColumnWriterImpl : public ColumnWriterImpl,
if (descr_->max_repetition_level() > 0) {
// A row could include more than one value
// Count the occasions where we start a new row
for (int64_t i = 0; i < num_levels; ++i) {
if (rep_levels[i] == 0) {
rows_written_++;
num_buffered_rows_++;
}
}
const int64_t records_written = internal::CountLevels(rep_levels, num_levels, 0);
rows_written_ += records_written;
num_buffered_rows_ += records_written;

WriteRepetitionLevels(num_levels, rep_levels);
} else {
Expand Down Expand Up @@ -1749,11 +1744,10 @@ class TypedColumnWriterImpl : public ColumnWriterImpl,
*out_spaced_values_to_write = batch_size;
*null_count = 0;
} else {
for (int x = 0; x < batch_size; x++) {
*out_values_to_write += def_levels[x] == level_info_.def_level ? 1 : 0;
*out_spaced_values_to_write +=
def_levels[x] >= level_info_.repeated_ancestor_def_level ? 1 : 0;
}
*out_values_to_write +=
internal::CountLevels(def_levels, batch_size, level_info_.def_level);
*out_spaced_values_to_write += internal::CountLevelsGreaterEqual(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This function is used only once, I don't know whether I should extract it or not ( DType was dispatch multiple times, so I think it's ok to extract it)

def_levels, batch_size, level_info_.repeated_ancestor_def_level);
*null_count = batch_size - *out_values_to_write;
}
return;
Expand Down Expand Up @@ -1809,12 +1803,9 @@ class TypedColumnWriterImpl : public ColumnWriterImpl,
if (descr_->max_repetition_level() > 0) {
// A row could include more than one value
// Count the occasions where we start a new row
for (int64_t i = 0; i < num_levels; ++i) {
if (rep_levels[i] == 0) {
rows_written_++;
num_buffered_rows_++;
}
}
const int64_t records_written = internal::CountLevels(rep_levels, num_levels, 0);
rows_written_ += records_written;
num_buffered_rows_ += records_written;
WriteRepetitionLevels(num_levels, rep_levels);
} else {
// Each value is exactly one row
Expand Down
7 changes: 2 additions & 5 deletions cpp/src/parquet/file_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "parquet/encryption/internal_file_decryptor.h"
#include "parquet/exception.h"
#include "parquet/file_writer.h"
#include "parquet/level_comparison.h"
#include "parquet/metadata.h"
#include "parquet/page_index.h"
#include "parquet/platform.h"
Expand Down Expand Up @@ -977,11 +978,7 @@ int64_t ScanFileContents(std::vector<int> columns, const int32_t column_batch_si
ScanAllValues(column_batch_size, def_levels.data(), rep_levels.data(),
values.data(), &values_read, col_reader.get());
if (col_reader->descr()->max_repetition_level() > 0) {
for (size_t i = 0; i < static_cast<size_t>(levels_read); i++) {
if (rep_levels[i] == 0) {
total_rows[col]++;
}
}
total_rows[col] += internal::CountLevels(rep_levels.data(), levels_read, 0);
} else {
total_rows[col] += levels_read;
}
Expand Down
53 changes: 53 additions & 0 deletions cpp/src/parquet/level_comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <algorithm>
#include <cstdint>
#include <limits>
#include <utility>

#include "parquet/platform.h"

Expand All @@ -35,4 +37,55 @@ struct MinMax {

MinMax PARQUET_EXPORT FindMinMax(const int16_t* levels, int64_t num_levels);

// Fallback for num_levels > uint16_t::max(), counting in chunks to avoid overflow.
template <typename Predicate>
ARROW_NOINLINE int64_t CountLevelsIfChunked(const int16_t* levels, int64_t num_levels,
Predicate&& pred) {
constexpr int64_t kMaxChunkSize = std::numeric_limits<uint16_t>::max();
int64_t total = 0;
while (num_levels > 0) {
const int64_t chunk_size = std::min(num_levels, kMaxChunkSize);
uint16_t chunk_count = 0;
for (int64_t i = 0; i < chunk_size; ++i) {
chunk_count += pred(levels[i]) ? 1 : 0;
}
total += chunk_count;
levels += chunk_size;
num_levels -= chunk_size;
}
return total;
}
Comment on lines +40 to +57

/// \brief Count the levels that satisfy `pred`.
///
/// Accumulates matches in a `uint16_t` so the compiler can vectorize the loop
/// using 16-bit lanes without widening each comparison result to 64 bits.
/// Inputs longer than `uint16_t::max()` are counted in chunks to avoid overflow.
template <typename Predicate>
inline int64_t CountLevelsIf(const int16_t* levels, int64_t num_levels,
Predicate&& pred) {
constexpr int64_t kMaxChunkSize = std::numeric_limits<uint16_t>::max();
if (num_levels > kMaxChunkSize) {
return CountLevelsIfChunked(levels, num_levels, std::forward<Predicate>(pred));
}
uint16_t count = 0;
for (int64_t i = 0; i < num_levels; ++i) {
count += pred(levels[i]) ? 1 : 0;
}
return count;
}
Comment on lines +65 to +76

/// \brief Count the levels equal to `value`.
inline int64_t CountLevels(const int16_t* levels, int64_t num_levels, int16_t value) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here I found CountLevelsIf will be compiled into DType multiple times, which making output binary 0.5% more larger whichout linker's help. So I extract a short CountLevels here to help compiler able to not copy and paste this function everywhere 😅

return CountLevelsIf(levels, num_levels,
[value](int16_t level) { return level == value; });
}

/// \brief Count the levels greater than or equal to `min_level`.
inline int64_t CountLevelsGreaterEqual(const int16_t* levels, int64_t num_levels,
int16_t min_level) {
return CountLevelsIf(levels, num_levels,
[min_level](int16_t level) { return level >= min_level; });
}
Comment on lines +65 to +89

} // namespace parquet::internal
44 changes: 44 additions & 0 deletions cpp/src/parquet/level_conversion_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <limits>
#include <string>
#include <vector>

Expand Down Expand Up @@ -113,6 +114,49 @@ TEST(GreaterThanBitmap, GeneratesExpectedBitmasks) {
}
#endif

TEST(CountLevels, CountsMatchingLevels) {
const std::vector<int16_t> levels = {3, 1, 3, 0, 3, 2};
auto is_odd = [](int16_t level) { return level % 2 != 0; };

EXPECT_EQ(CountLevels(levels.data(), /*num_levels=*/0, 3), 0);
EXPECT_EQ(CountLevels(levels.data(), /*num_levels=*/6, 3), 3);
EXPECT_EQ(CountLevels(levels.data(), /*num_levels=*/6, 0), 1);
EXPECT_EQ(CountLevelsGreaterEqual(levels.data(), /*num_levels=*/6, 1), 5);
EXPECT_EQ(CountLevelsGreaterEqual(levels.data(), /*num_levels=*/6, 3), 3);
EXPECT_EQ(CountLevelsIf(levels.data(), /*num_levels=*/6, is_odd), 4);
EXPECT_EQ(CountLevelsIf(levels.data(), /*num_levels=*/6, [](int16_t) { return false; }),
0);

// Cross-check various prefix lengths across vector and scalar tail boundaries.
std::vector<int16_t> longer_levels(/*count=*/100, 0);
for (size_t i = 0; i < longer_levels.size(); i += 3) {
longer_levels[i] = 3;
}
for (int64_t n = 0; n <= static_cast<int64_t>(longer_levels.size()); ++n) {
int64_t expected = 0;
for (int64_t i = 0; i < n; ++i) {
expected += longer_levels[i] == 3 ? 1 : 0;
}
EXPECT_EQ(CountLevels(longer_levels.data(), n, 3), expected) << "num_levels: " << n;
EXPECT_EQ(CountLevelsGreaterEqual(longer_levels.data(), n, 3), expected)
<< "num_levels: " << n;
}
}

TEST(CountLevels, HandlesChunkBoundaries) {
// Verify 16-bit accumulator does not overflow and chunk boundaries are exact.
constexpr int64_t kChunkSize = std::numeric_limits<uint16_t>::max();
std::vector<int16_t> levels(/*count=*/5 * kChunkSize, 1);
auto is_one = [](int16_t level) { return level == 1; };
const std::vector<int64_t> num_levels = {kChunkSize - 1, kChunkSize, kChunkSize + 1,
3 * kChunkSize, 5 * kChunkSize};
for (const int64_t n : num_levels) {
EXPECT_EQ(CountLevels(levels.data(), n, 1), n) << "num_levels: " << n;
EXPECT_EQ(CountLevelsGreaterEqual(levels.data(), n, 1), n) << "num_levels: " << n;
EXPECT_EQ(CountLevelsIf(levels.data(), n, is_one), n) << "num_levels: " << n;
}
}

TEST(DefLevelsToBitmap, WithRepetitionLevelFiltersOutEmptyListValues) {
std::vector<uint8_t> validity_bitmap(/*count*/ 8, 0);

Expand Down
Loading