| 216 | } |
| 217 | |
| 218 | fn append_option<T: ArrowPrimitiveType>( |
| 219 | &mut self, |
| 220 | type_name: &str, |
| 221 | v: Option<T::Native>, |
| 222 | ) -> Result<(), ArrowError> { |
| 223 | let type_name = type_name.to_string(); |
| 224 | |
| 225 | let mut field_data = match self.fields.remove(&type_name) { |
| 226 | Some(data) => { |
| 227 | if data.data_type != T::DATA_TYPE { |
| 228 | return Err(ArrowError::InvalidArgumentError(format!( |
| 229 | "Attempt to write col \"{}\" with type {} doesn't match existing type {}", |
| 230 | type_name, |
| 231 | T::DATA_TYPE, |
| 232 | data.data_type |
| 233 | ))); |
| 234 | } |
| 235 | data |
| 236 | } |
| 237 | None => match self.value_offset_builder { |
| 238 | Some(_) => FieldData::new::<T>( |
| 239 | self.fields.len() as i8, |
| 240 | T::DATA_TYPE, |
| 241 | self.initial_capacity, |
| 242 | ), |
| 243 | // In the case of a sparse union, we should pass the maximum of the currently length and the capacity. |
| 244 | None => { |
| 245 | let mut fd = FieldData::new::<T>( |
| 246 | self.fields.len() as i8, |
| 247 | T::DATA_TYPE, |
| 248 | self.len.max(self.initial_capacity), |
| 249 | ); |
| 250 | for _ in 0..self.len { |
| 251 | fd.append_null(); |
| 252 | } |
| 253 | fd |
| 254 | } |
| 255 | }, |
| 256 | }; |
| 257 | self.type_id_builder.append(field_data.type_id); |
| 258 | |
| 259 | match &mut self.value_offset_builder { |
| 260 | // Dense Union |
| 261 | Some(offset_builder) => { |
| 262 | offset_builder.append(field_data.slots as i32); |
| 263 | } |
| 264 | // Sparse Union |
| 265 | None => { |
| 266 | for (_, fd) in self.fields.iter_mut() { |
| 267 | // Append to all bar the FieldData currently being appended to |
| 268 | fd.append_null(); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | match v { |
| 274 | Some(v) => field_data.append_value::<T>(v), |
| 275 | None => field_data.append_null(), |