Read the record batch, consuming the reader
(mut self)
| 546 | |
| 547 | /// Read the record batch, consuming the reader |
| 548 | fn read_record_batch(mut self) -> Result<RecordBatch, ArrowError> { |
| 549 | let mut variadic_counts: VecDeque<i64> = self |
| 550 | .batch |
| 551 | .variadicBufferCounts() |
| 552 | .into_iter() |
| 553 | .flatten() |
| 554 | .collect(); |
| 555 | |
| 556 | let options = RecordBatchOptions::new().with_row_count(Some(self.batch.length() as usize)); |
| 557 | |
| 558 | let schema = Arc::clone(&self.schema); |
| 559 | if let Some(projection) = self.projection { |
| 560 | let mut arrays = vec![]; |
| 561 | // project fields |
| 562 | for (idx, field) in schema.fields().iter().enumerate() { |
| 563 | // A projected field can appear more than once, so collect all matching positions. |
| 564 | let mut child = None; |
| 565 | for (proj_idx, projected_idx) in projection.iter().enumerate() { |
| 566 | if *projected_idx == idx { |
| 567 | if child.is_none() { |
| 568 | child = Some(self.create_array(field, &mut variadic_counts)?); |
| 569 | } |
| 570 | |
| 571 | // Reuse the decoded array for duplicate projection entries. |
| 572 | arrays.push((proj_idx, child.as_ref().unwrap().clone())); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if child.is_none() { |
| 577 | self.skip_field(field, &mut variadic_counts)?; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | arrays.sort_by_key(|t| t.0); |
| 582 | |
| 583 | let schema = Arc::new(schema.project(projection)?); |
| 584 | let columns = arrays.into_iter().map(|t| t.1).collect::<Vec<_>>(); |
| 585 | |
| 586 | if self.skip_validation.get() { |
| 587 | // Safety: setting `skip_validation` requires `unsafe`, user assures data is valid |
| 588 | unsafe { |
| 589 | Ok(RecordBatch::new_unchecked( |
| 590 | schema, |
| 591 | columns, |
| 592 | self.batch.length() as usize, |
| 593 | )) |
| 594 | } |
| 595 | } else { |
| 596 | assert!(variadic_counts.is_empty()); |
| 597 | RecordBatch::try_new_with_options(schema, columns, &options) |
| 598 | } |
| 599 | } else { |
| 600 | let mut children = vec![]; |
| 601 | // keep track of index as lists require more than one node |
| 602 | for field in schema.fields() { |
| 603 | let child = self.create_array(field, &mut variadic_counts)?; |
| 604 | children.push(child); |
| 605 | } |