Skip to content

Commit de04aaa

Browse files
authored
Fix conversion of empty RecordBatch (#154)
1 parent 66068f2 commit de04aaa

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

datafusion-federation/src/schema_cast/record_convert.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use datafusion::arrow::{
2-
array::{Array, RecordBatch},
2+
array::{Array, RecordBatch, RecordBatchOptions},
33
compute::cast,
44
datatypes::{DataType, IntervalUnit, SchemaRef},
55
};
@@ -124,14 +124,15 @@ pub fn try_cast_to(record_batch: RecordBatch, expected_schema: SchemaRef) -> Res
124124
})
125125
.collect::<Result<Vec<Arc<dyn Array>>>>()?;
126126

127-
RecordBatch::try_new(expected_schema, cols)
127+
let options = RecordBatchOptions::new().with_row_count(Some(record_batch.num_rows()));
128+
RecordBatch::try_new_with_options(expected_schema, cols, &options)
128129
.map_err(|e| Error::UnableToConvertRecordBatch { source: e })
129130
}
130131

131132
#[cfg(test)]
132133
mod test {
133134
use super::*;
134-
use datafusion::arrow::array::LargeStringArray;
135+
use datafusion::arrow::array::{LargeStringArray, RecordBatchOptions};
135136
use datafusion::arrow::{
136137
array::{Int32Array, StringArray},
137138
datatypes::{DataType, Field, Schema, TimeUnit},
@@ -173,7 +174,7 @@ mod test {
173174
#[test]
174175
fn test_string_to_timestamp_conversion() {
175176
let result = try_cast_to(batch_input(), to_schema()).expect("converted");
176-
let expected = vec![
177+
let expected = [
177178
"+---+-----+---------------------+",
178179
"| a | b | c |",
179180
"+---+-----+---------------------+",
@@ -222,7 +223,7 @@ mod test {
222223
fn test_large_string_to_timestamp_conversion() {
223224
let result =
224225
try_cast_to(large_string_batch_input(), large_string_to_schema()).expect("converted");
225-
let expected = vec![
226+
let expected = [
226227
"+---+-----+---------------------+",
227228
"| a | b | c |",
228229
"+---+-----+---------------------+",
@@ -233,4 +234,15 @@ mod test {
233234
];
234235
assert_batches_eq!(expected, &[result]);
235236
}
237+
238+
#[test]
239+
fn test_convert_empty_batch() {
240+
let schema = SchemaRef::new(Schema::empty());
241+
let options = RecordBatchOptions::new().with_row_count(Some(10));
242+
let batch = RecordBatch::try_new_with_options(schema.clone(), vec![], &options)
243+
.expect("failed to create empty batch");
244+
let result = try_cast_to(batch, schema).expect("converted");
245+
let expected = ["++", "++", "++"];
246+
assert_batches_eq!(expected, &[result]);
247+
}
236248
}

0 commit comments

Comments
 (0)