Skip to content
Merged
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
50 changes: 17 additions & 33 deletions cpp/src/parquet/column_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1832,13 +1832,11 @@ class TypedRecordReader : public ColumnChunkReader<TypedRecordReaderTraits<DType
return valid_bits_.ReleaseValues(this->pool_); // 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;

Expand All @@ -1862,10 +1860,9 @@ class TypedRecordReader : public ColumnChunkReader<TypedRecordReaderTraits<DType
int64_t ReadOptionalRecordsInBuffer(int64_t num_records, int64_t* values_to_read,
int64_t* null_count);

// Reads dense for optional records. First it figures out how many values to
// read.
void ReadDenseForOptionalInBuffer(int64_t start_levels_position,
int64_t* values_to_read);
// Reads dense for optional or repeated records using the buffered definition levels.
void ReadDenseForOptionalOrRepeatedInBuffer(int64_t start_levels_position,
int64_t* values_to_read);

// Reads spaced for optional or repeated fields.
void ReadSpacedForOptionalOrRepeatedInBuffer(int64_t start_levels_position,
Expand Down Expand Up @@ -2002,14 +1999,11 @@ int64_t TypedRecordReader<DT, VS>::SkipRecordsInBufferNonRepeated(int64_t num_re
template <typename DT, typename VS>
int64_t TypedRecordReader<DT, VS>::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().
Expand Down Expand Up @@ -2110,21 +2104,17 @@ int64_t TypedRecordReader<DT, VS>::SkipRecords(int64_t num_records) {
}

template <typename DT, typename VS>
int64_t TypedRecordReader<DT, VS>::DelimitRecords(int64_t num_records,
int64_t* values_seen) {
int64_t TypedRecordReader<DT, VS>::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;
Expand All @@ -2138,7 +2128,6 @@ int64_t TypedRecordReader<DT, VS>::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()) {
Expand All @@ -2161,9 +2150,6 @@ int64_t TypedRecordReader<DT, VS>::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;
}

Expand Down Expand Up @@ -2204,11 +2190,9 @@ int64_t TypedRecordReader<DT, VS>::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<int32_t>(*values_to_read));
ReadDenseForOptionalOrRepeatedInBuffer(start_levels_position, values_to_read);
// null_count is always 0 for required.
ARROW_DCHECK_EQ(*null_count, 0);
} else {
Expand All @@ -2232,7 +2216,7 @@ int64_t TypedRecordReader<DT, VS>::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);
Expand All @@ -2244,15 +2228,15 @@ int64_t TypedRecordReader<DT, VS>::ReadOptionalRecordsInBuffer(int64_t num_recor
}

template <typename DT, typename VS>
void TypedRecordReader<DT, VS>::ReadDenseForOptionalInBuffer(
void TypedRecordReader<DT, VS>::ReadDenseForOptionalOrRepeatedInBuffer(
int64_t start_levels_position, int64_t* values_to_read) {
// levels_position_ must already be incremented based on number of records
// read.
ARROW_DCHECK_GE(levels_position_, start_levels_position);

// 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.
Expand Down
Loading