From 97cd7b8ad83a2469246da303dbd4d7712ac38a1f Mon Sep 17 00:00:00 2001 From: "Heres, Daniel" Date: Thu, 8 Sep 2022 18:44:00 +0200 Subject: [PATCH 1/2] Add support for empty projection in RecordBatch::project --- arrow/src/record_batch.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/arrow/src/record_batch.rs b/arrow/src/record_batch.rs index 47257b496c1b..a3f494461cb8 100644 --- a/arrow/src/record_batch.rs +++ b/arrow/src/record_batch.rs @@ -199,6 +199,16 @@ impl RecordBatch { /// Projects the schema onto the specified columns pub fn project(&self, indices: &[usize]) -> Result { + if indices.is_empty() { + return RecordBatch::try_new_with_options( + Arc::new(Schema::empty()), + vec![], + &RecordBatchOptions { + match_field_names: true, + row_count: Some(self.row_count), + }, + ); + } let projected_schema = self.schema.project(indices)?; let batch_fields = indices .iter() @@ -961,6 +971,26 @@ mod tests { assert_eq!(expected, record_batch.project(&[0, 2]).unwrap()); } + #[test] + fn project_empty() { + let c: ArrayRef = Arc::new(StringArray::from(vec!["d", "e", "f"])); + + let record_batch = + RecordBatch::try_from_iter(vec![("c", c.clone())]).expect("valid conversion"); + + let expected = RecordBatch::try_new_with_options( + Arc::new(Schema::empty()), + vec![], + &RecordBatchOptions { + match_field_names: true, + row_count: Some(3), + }, + ) + .expect("valid conversion"); + + assert_eq!(expected, record_batch.project(&[]).unwrap()); + } + #[test] fn test_no_column_record_batch() { let schema = Arc::new(Schema::new(vec![])); From d51fed88870bb444d9869ab6779830f7b58c8e5b Mon Sep 17 00:00:00 2001 From: "Heres, Daniel" Date: Thu, 8 Sep 2022 21:15:28 +0200 Subject: [PATCH 2/2] Simplify --- arrow/src/record_batch.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/arrow/src/record_batch.rs b/arrow/src/record_batch.rs index a3f494461cb8..5e7a1d2afbe5 100644 --- a/arrow/src/record_batch.rs +++ b/arrow/src/record_batch.rs @@ -199,16 +199,6 @@ impl RecordBatch { /// Projects the schema onto the specified columns pub fn project(&self, indices: &[usize]) -> Result { - if indices.is_empty() { - return RecordBatch::try_new_with_options( - Arc::new(Schema::empty()), - vec![], - &RecordBatchOptions { - match_field_names: true, - row_count: Some(self.row_count), - }, - ); - } let projected_schema = self.schema.project(indices)?; let batch_fields = indices .iter() @@ -223,7 +213,14 @@ impl RecordBatch { }) .collect::>>()?; - RecordBatch::try_new(SchemaRef::new(projected_schema), batch_fields) + RecordBatch::try_new_with_options( + SchemaRef::new(projected_schema), + batch_fields, + &RecordBatchOptions { + match_field_names: true, + row_count: Some(self.row_count), + }, + ) } /// Returns the number of columns in the record batch.