From 354761501148d84d1b2cecb639887b9b1647323e Mon Sep 17 00:00:00 2001 From: Zehua Zou Date: Mon, 30 Nov 2026 17:29:29 +0800 Subject: [PATCH] avoid scanning definition levels twice in spaced reads of repeated fields --- cpp/src/parquet/column_reader.cc | 50 +++++++++++--------------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/cpp/src/parquet/column_reader.cc b/cpp/src/parquet/column_reader.cc index ce1a918e13e..1423ac4c6cf 100644 --- a/cpp/src/parquet/column_reader.cc +++ b/cpp/src/parquet/column_reader.cc @@ -1832,13 +1832,11 @@ class TypedRecordReader : public ColumnChunkReaderpool_); // nullptr if void } - // Process written repetition/definition levels to reach the end of - // records. Only used for repeated fields. - // Process no more levels than necessary to delimit the indicated - // number of logical records. Updates internal state of RecordReader + // Scan repetition levels until the requested number of logical records is delimited + // or the buffered levels are exhausted. Updates levels_position_ and at_record_start_. // // \return Number of records delimited - int64_t DelimitRecords(int64_t num_records, int64_t* values_seen); + int64_t DelimitRecords(int64_t num_records); void Reserve(int64_t capacity) override; @@ -1862,10 +1860,9 @@ class TypedRecordReader : public ColumnChunkReader::SkipRecordsInBufferNonRepeated(int64_t num_re template int64_t TypedRecordReader::DelimitAndSkipRecordsInBuffer(int64_t num_records) { if (num_records == 0) return 0; - // Look at the buffered levels, delimit them based on - // (rep_level == 0), report back how many records are in there, and - // fill in how many not-null values (def_level == max_def_level_). - // DelimitRecords updates levels_position_. - int64_t start_levels_position = levels_position_; - int64_t values_seen = 0; - int64_t skipped_records = DelimitRecords(num_records, &values_seen); - SkipValuesInPage(values_seen); + const int64_t start_levels_position = levels_position_; + const int64_t skipped_records = DelimitRecords(num_records); + SkipValuesInPage(CountLevels(def_levels() + start_levels_position, + levels_position_ - start_levels_position, + this->max_def_level())); // Mark those levels and values as consumed in the underlying page. // This must be done before we throw away levels since it updates // levels_position_ and levels_written(). @@ -2110,21 +2104,17 @@ int64_t TypedRecordReader::SkipRecords(int64_t num_records) { } template -int64_t TypedRecordReader::DelimitRecords(int64_t num_records, - int64_t* values_seen) { +int64_t TypedRecordReader::DelimitRecords(int64_t num_records) { if (ARROW_PREDICT_FALSE(num_records == 0 || !has_buffered_levels())) { - *values_seen = 0; return 0; } int64_t records_read = 0; const int16_t* const rep_levels = this->rep_levels(); - const int16_t* const def_levels = this->def_levels(); ARROW_DCHECK_GT(this->max_rep_level(), 0); // If at_record_start_ is true, we are seeing the start of a record // for the second time, such as after repeated calls to // DelimitRecords. In this case we must continue until we find // another record start or exhaust the ColumnChunk - int64_t level = levels_position_; if (at_record_start_) { if (ARROW_PREDICT_FALSE(rep_levels[levels_position_] != 0)) { std::stringstream ss; @@ -2138,7 +2128,6 @@ int64_t TypedRecordReader::DelimitRecords(int64_t num_records, at_record_start_ = false; } - // Count logical records and number of non-null values to read ARROW_DCHECK(!at_record_start_); // Scan repetition levels to find record end while (has_buffered_levels()) { @@ -2161,9 +2150,6 @@ int64_t TypedRecordReader::DelimitRecords(int64_t num_records, break; } } - // Scan definition levels to find number of physical values - *values_seen = - CountLevels(def_levels + level, levels_position_ - level, this->max_def_level()); return records_read; } @@ -2204,11 +2190,9 @@ int64_t TypedRecordReader::ReadRepeatedRecordsInBuffer(int64_t num_recor // nullable. Even if they are required, we may have to read ahead and // delimit the records to get the right number of values and they will // have associated levels. - int64_t records_read = DelimitRecords(num_records, values_to_read); + const int64_t records_read = DelimitRecords(num_records); if (valid_bits_.is_void()) { // not nullable or read_dense_for_nullable - // This is only reading in the current page so this fits in an int32. - value_sink_.ReadValuesDense(*this->current_decoder_.get(), - clamp_to(*values_to_read)); + ReadDenseForOptionalOrRepeatedInBuffer(start_levels_position, values_to_read); // null_count is always 0 for required. ARROW_DCHECK_EQ(*null_count, 0); } else { @@ -2232,7 +2216,7 @@ int64_t TypedRecordReader::ReadOptionalRecordsInBuffer(int64_t num_recor // Optional fields are always nullable. if (read_dense_for_nullable()) { - ReadDenseForOptionalInBuffer(start_levels_position, values_to_read); + ReadDenseForOptionalOrRepeatedInBuffer(start_levels_position, values_to_read); // We don't need to update null_count when reading dense. It should be // already set to 0. ARROW_DCHECK_EQ(*null_count, 0); @@ -2244,7 +2228,7 @@ int64_t TypedRecordReader::ReadOptionalRecordsInBuffer(int64_t num_recor } template -void TypedRecordReader::ReadDenseForOptionalInBuffer( +void TypedRecordReader::ReadDenseForOptionalOrRepeatedInBuffer( int64_t start_levels_position, int64_t* values_to_read) { // levels_position_ must already be incremented based on number of records // read. @@ -2252,7 +2236,7 @@ void TypedRecordReader::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 += + *values_to_read = CountLevels(def_levels + start_levels_position, levels_position_ - start_levels_position, this->max_def_level()); // This is only reading in the current page so this fits in an int32.