Skip to content

Commit 52bbdb1

Browse files
committed
fix schema/array column ordering issue by using BTreeMaps in the Tracer and Builder
1 parent 789ec98 commit 52bbdb1

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

serde_arrow/src/internal/schema/tracer.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ impl UnionTracer {
10761076
STRATEGY_KEY.to_string(),
10771077
Strategy::EnumsWithNamedFieldsAsStructs.to_string(),
10781078
);
1079-
let mut fields = Vec::new();
1079+
let mut fields = BTreeMap::new();
10801080

10811081
// For this option, we want to merge the variant children up one level, combining the names
10821082
// For each variant with name variant_name
@@ -1087,14 +1087,16 @@ impl UnionTracer {
10871087
if let Some(variant) = variant {
10881088
let schema = variant.tracer.to_schema()?;
10891089
for field in schema.fields {
1090-
fields.push(field.to_flattened_union_field(variant.name.as_str()))
1090+
let flat_field = field.to_flattened_union_field(variant.name.as_str());
1091+
fields.insert(flat_field.name.to_string(), flat_field);
10911092
}
10921093
} else {
1093-
fields.push(unknown_variant_field())
1094+
let uf = unknown_variant_field();
1095+
fields.insert(uf.name, unknown_variant_field());
10941096
};
10951097
}
10961098

1097-
data_type = DataType::Struct(fields);
1099+
data_type = DataType::Struct(fields.into_values().collect());
10981100
} else {
10991101
let mut fields = Vec::new();
11001102
for (idx, variant) in self.variants.iter().enumerate() {

serde_arrow/src/internal/serialization/flattened_union_builder.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl FlattenedUnionBuilder {
4040
}
4141

4242
pub fn into_array(self) -> Result<Array> {
43-
let mut fields = Vec::new();
43+
let mut fields = BTreeMap::new();
4444

4545
for (builder, meta) in self.fields.into_iter() {
4646
let ArrayBuilder::Struct(builder) = builder else {
@@ -53,13 +53,16 @@ impl FlattenedUnionBuilder {
5353
// Name change is currently needed for struct field lookup to work correctly.
5454

5555
sub_meta.name = format!("{}::{}", meta.name, sub_meta.name);
56-
fields.push((sub_builder.into_array()?, sub_meta));
56+
fields.insert(
57+
sub_meta.name.to_owned(),
58+
(sub_builder.into_array()?, sub_meta),
59+
);
5760
}
5861
}
5962

6063
Ok(Array::Struct(StructArray {
6164
len: self.row_count,
62-
fields,
65+
fields: fields.into_values().collect(),
6366
// TODO: is this ok to hardcode?
6467
// assuming so because when testing manually,
6568
// validity of struct with nullable fields was None

0 commit comments

Comments
 (0)