Skip to content

Commit 9b101de

Browse files
authored
Improve ColumnType::union (#32590)
Improve the implementation of ColumnType::union to avoid cloning if not needed. Also, make the code more readable by destructing tuples into their components. No functionality changes expected otherwise. Signed-off-by: Moritz Hoffmann <mh@materialize.com>
1 parent 6d4398c commit 9b101de

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/repr/src/relation.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ fn return_true() -> bool {
6262

6363
impl ColumnType {
6464
pub fn union(&self, other: &Self) -> Result<Self, anyhow::Error> {
65-
match (self.scalar_type.clone(), other.scalar_type.clone()) {
65+
match (&self.scalar_type, &other.scalar_type) {
6666
(scalar_type, other_scalar_type) if scalar_type == other_scalar_type => {
6767
Ok(ColumnType {
68-
scalar_type,
68+
scalar_type: scalar_type.clone(),
6969
nullable: self.nullable || other.nullable,
7070
})
7171
}
72-
(scalar_type, other_scalar_type) if scalar_type.base_eq(&other_scalar_type) => {
72+
(scalar_type, other_scalar_type) if scalar_type.base_eq(other_scalar_type) => {
7373
Ok(ColumnType {
7474
scalar_type: scalar_type.without_modifiers(),
7575
nullable: self.nullable || other.nullable,
@@ -90,24 +90,25 @@ impl ColumnType {
9090
);
9191
};
9292

93-
let mut union_fields: Vec<(ColumnName, ColumnType)> = vec![];
94-
for (field, other_field) in fields.iter().zip(other_fields.iter()) {
95-
if field.0 != other_field.0 {
93+
let mut union_fields = Vec::with_capacity(fields.len());
94+
for ((name, typ), (other_name, other_typ)) in fields.iter().zip(other_fields.iter())
95+
{
96+
if name != other_name {
9697
bail!(
9798
"Can't union types: {:?} and {:?}",
9899
self.scalar_type,
99100
other.scalar_type
100101
);
101102
} else {
102-
let union_column_type = field.1.union(&other_field.1)?;
103-
union_fields.push((field.0.clone(), union_column_type));
103+
let union_column_type = typ.union(other_typ)?;
104+
union_fields.push((name.clone(), union_column_type));
104105
};
105106
}
106107

107108
Ok(ColumnType {
108109
scalar_type: ScalarType::Record {
109110
fields: union_fields.into(),
110-
custom_id,
111+
custom_id: *custom_id,
111112
},
112113
nullable: self.nullable || other.nullable,
113114
})

0 commit comments

Comments
 (0)