Given a file reader and a list of row groups, this is a generator of record batch generators (where each sub-generator is the contents of a single row group).
| 1096 | /// Given a file reader and a list of row groups, this is a generator of record |
| 1097 | /// batch generators (where each sub-generator is the contents of a single row group). |
| 1098 | class RowGroupGenerator { |
| 1099 | public: |
| 1100 | using RecordBatchGenerator = |
| 1101 | ::arrow::AsyncGenerator<std::shared_ptr<::arrow::RecordBatch>>; |
| 1102 | |
| 1103 | struct ReadRequest { |
| 1104 | ::arrow::Future<RecordBatchGenerator> read; |
| 1105 | int64_t num_rows; |
| 1106 | }; |
| 1107 | |
| 1108 | explicit RowGroupGenerator(std::shared_ptr<FileReaderImpl> arrow_reader, |
| 1109 | ::arrow::internal::Executor* cpu_executor, |
| 1110 | std::vector<int> row_groups, std::vector<int> column_indices, |
| 1111 | int64_t min_rows_in_flight) |
| 1112 | : arrow_reader_(std::move(arrow_reader)), |
| 1113 | cpu_executor_(cpu_executor), |
| 1114 | row_groups_(std::move(row_groups)), |
| 1115 | column_indices_(std::move(column_indices)), |
| 1116 | min_rows_in_flight_(min_rows_in_flight), |
| 1117 | rows_in_flight_(0), |
| 1118 | index_(0), |
| 1119 | readahead_index_(0) {} |
| 1120 | |
| 1121 | ::arrow::Future<RecordBatchGenerator> operator()() { |
| 1122 | if (index_ >= row_groups_.size()) { |
| 1123 | return ::arrow::AsyncGeneratorEnd<RecordBatchGenerator>(); |
| 1124 | } |
| 1125 | index_++; |
| 1126 | FillReadahead(); |
| 1127 | ReadRequest next = std::move(in_flight_reads_.front()); |
| 1128 | DCHECK(!in_flight_reads_.empty()); |
| 1129 | in_flight_reads_.pop(); |
| 1130 | rows_in_flight_ -= next.num_rows; |
| 1131 | return next.read; |
| 1132 | } |
| 1133 | |
| 1134 | private: |
| 1135 | void FillReadahead() { |
| 1136 | if (min_rows_in_flight_ == 0) { |
| 1137 | // No readahead, fetch the batch when it is asked for |
| 1138 | FetchNext(); |
| 1139 | } else { |
| 1140 | while (readahead_index_ < row_groups_.size() && |
| 1141 | rows_in_flight_ < min_rows_in_flight_) { |
| 1142 | FetchNext(); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void FetchNext() { |
| 1148 | size_t row_group_index = readahead_index_++; |
| 1149 | int row_group = row_groups_[row_group_index]; |
| 1150 | std::vector<int> column_indices = column_indices_; |
| 1151 | auto reader = arrow_reader_; |
| 1152 | int64_t num_rows = |
| 1153 | reader->parquet_reader()->metadata()->RowGroup(row_group)->num_rows(); |
| 1154 | rows_in_flight_ += num_rows; |
| 1155 | ::arrow::Future<RecordBatchGenerator> row_group_read; |
no outgoing calls
no test coverage detected