Skip to content
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

Merged
merged 5 commits into from
Jun 25, 2021
Merged
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions arrow/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Copy link
Member

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.

let schema = self.schema();
let num_columns = self.num_columns();
let new_columns = (0..num_columns)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.columns().iter() ^_^

Copy link
Contributor

Choose a reason for hiding this comment

The 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 RecordBatch::try_new()? It incurs some overhead checking that the schema and arrays match, when they already should match

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorgecarleitao thanks, I have modified, PTAL

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -426,6 +438,29 @@ mod tests {
assert_eq!(5, record_batch.column(1).data().len());
}

#[test]
fn create_record_batch_slice() {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should return an empty slice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should return an empty slice.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have modified, PTAL
@Dandandan @jimexist

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![
Expand Down