Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Fixed error
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Dec 18, 2022
1 parent 64f8c96 commit a2df894
Showing 1 changed file with 43 additions and 20 deletions.
63 changes: 43 additions & 20 deletions src/array/union/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct UnionArray {
types: Buffer<i8>,
// Invariant: `map.len() == fields.len()`
// Invariant: every item in `map` is `> 0 && < fields.len()`
map: Option<Vec<usize>>,
map: Option<[usize; 127]>,
fields: Vec<Box<dyn Array>>,
// Invariant: when set, `offsets.len() == types.len()`
offsets: Option<Buffer<i32>>,
Expand Down Expand Up @@ -99,29 +99,52 @@ impl UnionArray {
"In a union, when the ids are set, their length must be equal to the number of fields",
));
}
ids.iter().map(|&id| {
if id < 0 || id >= fields.len() as i32 {
return Err(Error::oos("In a union, when the ids are set, each id must be smaller than the number of fields."));

// example:
// * types = [5, 7, 5, 7, 7, 7, 5, 7, 7, 5, 5]
// * ids = [5, 7]
// => hash = [0, 0, 0, 0, 0, 0, 1, 0, ...]
let mut hash = [0; 127];

for (pos, &id) in ids.iter().enumerate() {
if !(0..=127).contains(&id) {
return Err(Error::oos(
"In a union, when the ids are set, every id must belong to [0, 128[",
));
}
hash[id as usize] = pos;
}

types.iter().try_for_each(|&type_| {
if type_ < 0 {
return Err(Error::oos("In a union, when the ids are set, every type must be >= 0"));
}
Ok(id as usize)
}).collect::<Result<Vec<_>, Error>>().map(Some)?
let id = hash[type_ as usize];
if id >= fields.len() {
Err(Error::oos("In a union, when the ids are set, each id must be smaller than the number of fields."))
} else {
Ok(())
}
})?;

Some(hash)
} else {
// Safety: every type in types is smaller than number of fields
let mut is_valid = true;
for &type_ in types.iter() {
if type_ < 0 || type_ >= number_of_fields {
is_valid = false
}
}
if !is_valid {
return Err(Error::oos(
"Every type in `types` must be larger than 0 and smaller than the number of fields.",
));
}

None
};

// Safety: every type in types is smaller than number of fields
let mut is_valid = true;
for &type_ in types.iter() {
if type_ < 0 || type_ >= number_of_fields {
is_valid = false
}
}
if !is_valid {
return Err(Error::oos(
"Every type in `types` must be larger than 0 and smaller than the number of fields.",
));
}

Ok(Self {
data_type,
map,
Expand Down Expand Up @@ -234,7 +257,7 @@ impl UnionArray {
Self {
data_type: self.data_type.clone(),
fields: self.fields.clone(),
map: self.map.clone(),
map: self.map,
types: self.types.clone().slice_unchecked(offset, length),
offsets: self
.offsets
Expand Down

0 comments on commit a2df894

Please sign in to comment.