-
Notifications
You must be signed in to change notification settings - Fork 866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement function slice for RecordBatch #490
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,6 +244,18 @@ impl RecordBatch { | |
&self.columns[..] | ||
} | ||
|
||
/// Return a new RecordBatch where each column is sliced | ||
/// according to `offset` and `length` | ||
pub fn slice(&self, offset: usize, length: usize) -> Result<RecordBatch> { | ||
let schema = self.schema(); | ||
let num_columns = self.num_columns(); | ||
let new_columns = (0..num_columns) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My local version is /// Slice all the arrays in the record batch.
pub fn slice(&self, offset: usize, len: usize) -> Self {
let columns = self
.columns()
.iter()
.map(|array| array.slice(offset, len))
.collect();
Self {
schema: self.schema.clone(),
columns,
}
} Does it make sense to go through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jorgecarleitao thanks, I have modified, PTAL There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nevi-me Thanks for your suggestion, I made a modification. PTAL |
||
.map(|column_index| self.column(column_index).slice(offset, length)) | ||
.collect(); | ||
|
||
RecordBatch::try_new(schema, new_columns) | ||
} | ||
|
||
/// Create a `RecordBatch` from an iterable list of pairs of the | ||
/// form `(field_name, array)`, with the same requirements on | ||
/// fields and arrays as [`RecordBatch::try_new`]. This method is | ||
|
@@ -426,6 +438,29 @@ mod tests { | |
assert_eq!(5, record_batch.column(1).data().len()); | ||
} | ||
|
||
#[test] | ||
fn create_record_batch_slice() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you test for edge cases, e.g. empty length, over boundary offset, empty columns, empty batch, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jimexist I have a question, if an empty RecordBatch calls slice(), the offset and length are zero, should we panic or return an empty RecordBatch slice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should return an empty slice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have modified, PTAL |
||
let schema = Schema::new(vec![ | ||
Field::new("a", DataType::Int32, false), | ||
Field::new("b", DataType::Utf8, false), | ||
]); | ||
let expected_schema = schema.clone(); | ||
|
||
let a = Int32Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8]); | ||
let b = StringArray::from(vec!["a", "b", "c", "d", "e", "f", "h", "i"]); | ||
|
||
let record_batch = | ||
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(a), Arc::new(b)]) | ||
.unwrap(); | ||
|
||
let offset = 2; | ||
let length = 5; | ||
let record_batch_slice = record_batch.slice(offset, length).unwrap(); | ||
|
||
assert_eq!(record_batch_slice.schema().as_ref(), &expected_schema); | ||
check_batch(record_batch_slice) | ||
} | ||
|
||
#[test] | ||
fn create_record_batch_try_from_iter() { | ||
let a: ArrayRef = Arc::new(Int32Array::from(vec![ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove the
Result
wrapping if nothing can fail.