Skip to content

Commit 2cb7928

Browse files
committed
Use UnionFields::from_fields
1 parent 34cc90c commit 2cb7928

File tree

2 files changed

+9
-13
lines changed
  • datafusion
    • datasource-avro/src/avro_to_arrow
    • proto-common/src/from_proto

2 files changed

+9
-13
lines changed

datafusion/datasource-avro/src/avro_to_arrow/schema.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,8 @@ fn schema_to_field_with_props(
117117
.iter()
118118
.map(|s| schema_to_field_with_props(s, None, has_nullable, None))
119119
.collect::<Result<Vec<Field>>>()?;
120-
let type_ids = 0_i8..fields.len() as i8;
121-
DataType::Union(
122-
UnionFields::try_new(type_ids, fields).unwrap(),
123-
UnionMode::Dense,
124-
)
120+
// Assign type_ids based on the order in which they appear
121+
DataType::Union(UnionFields::from_fields(fields), UnionMode::Dense)
125122
}
126123
}
127124
AvroSchema::Record(RecordSchema { fields, .. }) => {

datafusion/proto-common/src/from_proto/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,15 @@ impl TryFrom<&protobuf::arrow_type::ArrowTypeEnum> for DataType {
304304
};
305305
let union_fields = parse_proto_fields_to_fields(&union.union_types)?;
306306

307-
// Default to index based type ids if not provided
308-
let type_ids: Vec<_> = match union.type_ids.is_empty() {
309-
true => (0..union_fields.len() as i8).collect(),
310-
false => union.type_ids.iter().map(|i| *i as i8).collect(),
311-
};
312-
313-
let union_fields =
307+
// Default to index based type ids if not explicitly provided
308+
let union_fields = if union.type_ids.is_empty() {
309+
UnionFields::from_fields(union_fields)
310+
} else {
311+
let type_ids = union.type_ids.iter().map(|i| *i as i8);
314312
UnionFields::try_new(type_ids, union_fields).map_err(|e| {
315313
DataFusionError::from(e).context("Deserializing Union DataType")
316-
})?;
314+
})?
315+
};
317316
DataType::Union(union_fields, union_mode)
318317
}
319318
arrow_type::ArrowTypeEnum::Dictionary(dict) => {

0 commit comments

Comments
 (0)