-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: Adding with_schema_unchecked method for RecordBatch
#7402
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
Changes from 2 commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -359,6 +359,20 @@ impl RecordBatch { | |||||
| }) | ||||||
| } | ||||||
|
|
||||||
| /// Forcibly overrides the schema of this [`RecordBatch`] | ||||||
| /// without additional schema checks however bringing all the schema compatibility responsibilities | ||||||
| /// to the caller site. | ||||||
| /// | ||||||
| /// If provided schema is not compatible with this [`RecordBatch`] columns the runtime behavior | ||||||
| /// is undefined | ||||||
| pub fn with_schema_force(self, schema: SchemaRef) -> Result<Self, ArrowError> { | ||||||
|
||||||
| Ok(Self { | ||||||
| schema, | ||||||
| columns: self.columns, | ||||||
| row_count: self.row_count, | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| /// Returns the [`Schema`] of the record batch. | ||||||
| pub fn schema(&self) -> SchemaRef { | ||||||
| self.schema.clone() | ||||||
|
|
@@ -744,12 +758,14 @@ impl RecordBatchOptions { | |||||
| row_count: None, | ||||||
| } | ||||||
| } | ||||||
| /// Sets the row_count of RecordBatchOptions and returns self | ||||||
|
|
||||||
| /// Sets the `row_count` of `RecordBatchOptions` and returns this [`RecordBatch`] | ||||||
| pub fn with_row_count(mut self, row_count: Option<usize>) -> Self { | ||||||
| self.row_count = row_count; | ||||||
| self | ||||||
| } | ||||||
| /// Sets the match_field_names of RecordBatchOptions and returns self | ||||||
|
|
||||||
| /// Sets the `match_field_names` of `RecordBatchOptions` and returns this [`RecordBatch`] | ||||||
| pub fn with_match_field_names(mut self, match_field_names: bool) -> Self { | ||||||
| self.match_field_names = match_field_names; | ||||||
| self | ||||||
|
|
@@ -1637,4 +1653,57 @@ mod tests { | |||||
| "bar" | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| #[test] | ||||||
| fn test_batch_with_force_schema() { | ||||||
|
||||||
| fn force_schema_and_get_err_from_batch( | ||||||
| record_batch: &RecordBatch, | ||||||
| schema_ref: SchemaRef, | ||||||
| idx: usize, | ||||||
| ) -> Option<ArrowError> { | ||||||
| record_batch | ||||||
| .clone() | ||||||
| .with_schema_force(schema_ref) | ||||||
| .unwrap() | ||||||
| .project(&[idx]) | ||||||
| .err() | ||||||
| } | ||||||
|
|
||||||
| 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"); | ||||||
|
|
||||||
| // Test empty schema for non-empty schema batch | ||||||
| let invalid_schema_empty = Schema::empty(); | ||||||
| assert_eq!( | ||||||
| force_schema_and_get_err_from_batch(&record_batch, invalid_schema_empty.into(), 0) | ||||||
| .unwrap() | ||||||
| .to_string(), | ||||||
| "Schema error: project index 0 out of bounds, max field 0" | ||||||
| ); | ||||||
|
|
||||||
| // Wrong number of columns | ||||||
| let invalid_schema_more_cols = Schema::new(vec![ | ||||||
| Field::new("a", DataType::Utf8, false), | ||||||
| Field::new("a", DataType::Int32, false), | ||||||
|
||||||
| Field::new("a", DataType::Int32, false), | |
| Field::new("b", DataType::Int32, false), |
?. This triggers my OCD 😅
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.
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.
Nice said!