-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move flattened_union_builder tests to test_with_arrow directory
- Loading branch information
1 parent
6a02592
commit bf3f789
Showing
4 changed files
with
159 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
serde_arrow/src/test_with_arrow/impls/flattened_union.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{ | ||
internal::{ | ||
array_builder::ArrayBuilder, | ||
arrow::{Array, DataType, Field}, | ||
schema::{SchemaLike, TracingOptions}, | ||
}, | ||
schema::SerdeArrowSchema, | ||
Serializer, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct Number { | ||
v: Value, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
enum Value { | ||
Real { value: f32 }, | ||
Complex { i: f32, j: f32 }, | ||
Whole { value: usize }, | ||
} | ||
|
||
fn number_field() -> Field { | ||
Field { | ||
name: "v".to_string(), | ||
data_type: DataType::Struct(vec![ | ||
Field { | ||
name: "Complex::i".to_string(), | ||
data_type: DataType::Float32, | ||
nullable: true, | ||
metadata: HashMap::new(), | ||
}, | ||
Field { | ||
name: "Complex::j".to_string(), | ||
data_type: DataType::Float32, | ||
nullable: true, | ||
metadata: HashMap::new(), | ||
}, | ||
Field { | ||
name: "Real::value".to_string(), | ||
data_type: DataType::Float32, | ||
nullable: true, | ||
metadata: HashMap::new(), | ||
}, | ||
Field { | ||
name: "Whole::value".to_string(), | ||
data_type: DataType::UInt64, | ||
nullable: true, | ||
metadata: HashMap::new(), | ||
}, | ||
]), | ||
nullable: false, | ||
metadata: HashMap::from([( | ||
"SERDE_ARROW:strategy".to_string(), | ||
"EnumsWithNamedFieldsAsStructs".to_string(), | ||
)]), | ||
} | ||
} | ||
|
||
fn number_schema() -> SerdeArrowSchema { | ||
let options = TracingOptions::default() | ||
.allow_null_fields(true) | ||
.enums_with_named_fields_as_structs(true); | ||
|
||
SerdeArrowSchema::from_type::<Number>(options).unwrap() | ||
} | ||
|
||
fn number_data() -> Vec<Number> { | ||
vec![ | ||
Number { | ||
v: Value::Real { value: 0.0 }, | ||
}, | ||
Number { | ||
v: Value::Complex { i: 0.5, j: 0.5 }, | ||
}, | ||
Number { | ||
v: Value::Whole { value: 5 }, | ||
}, | ||
] | ||
} | ||
|
||
#[test] | ||
fn test_build_flattened_union_builder() { | ||
let mut builder = ArrayBuilder::new(number_schema()).unwrap(); | ||
|
||
// One struct in the array | ||
let arrays = builder.build_arrays().unwrap(); | ||
|
||
assert_eq!(arrays.len(), 1); | ||
|
||
let array = &arrays[0]; | ||
|
||
let Array::Struct(ref struct_array) = array else { | ||
panic!("expected a struct array, found {array:#?}"); | ||
}; | ||
|
||
// Should be a single struct array with 4 fields: Complex::i, Complex::j, Real::value, Whole::value | ||
assert_eq!( | ||
struct_array.fields.len(), | ||
4, | ||
"contained {} fields", | ||
struct_array.fields.len() | ||
); | ||
|
||
let (first_field, meta) = &struct_array.fields[0]; | ||
assert_eq!(meta.name, "Complex::i"); | ||
assert!(matches!(first_field, Array::Float32(_))); | ||
|
||
let (second_field, meta) = &struct_array.fields[1]; | ||
assert_eq!(meta.name, "Complex::j"); | ||
assert!(matches!(second_field, Array::Float32(_))); | ||
|
||
let (third_field, meta) = &struct_array.fields[2]; | ||
assert_eq!(meta.name, "Real::value"); | ||
assert!(matches!(third_field, Array::Float32(_))); | ||
|
||
let (fourth_field, meta) = &struct_array.fields[3]; | ||
assert_eq!(meta.name, "Whole::value"); | ||
assert!(matches!(fourth_field, Array::UInt64(_))); | ||
} | ||
|
||
#[test] | ||
fn test_serialize_flattened_union_builder() { | ||
let field = number_field(); | ||
let data = number_data(); | ||
let schema = SerdeArrowSchema { | ||
fields: vec![field], | ||
}; | ||
|
||
let api_builder = ArrayBuilder::new(schema).expect("failed to create api array builder"); | ||
let serializer = Serializer::new(api_builder); | ||
data.serialize(serializer) | ||
.expect("failed to serialize") | ||
.into_inner() | ||
.to_arrow() | ||
.expect("failed to serialize to arrow"); | ||
} | ||
|
||
#[test] | ||
fn test_record_batch_flattened_union_builder() { | ||
let field = number_field(); | ||
let data = number_data(); | ||
let schema = SerdeArrowSchema { | ||
fields: vec![field], | ||
}; | ||
|
||
let api_builder = ArrayBuilder::new(schema).expect("failed to create api array builder"); | ||
let serializer = Serializer::new(api_builder); | ||
data.serialize(serializer) | ||
.expect("failed to serialize") | ||
.into_inner() | ||
.to_record_batch() | ||
.expect("failed to create record batch"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters