Replaces all occurrences in string of substring from with substring to. replace('abcdefabcdef', 'cd', 'XX') = 'abXXefabXXef'
(args: &[ArrayRef])
| 203 | /// Replaces all occurrences in string of substring from with substring to. |
| 204 | /// replace('abcdefabcdef', 'cd', 'XX') = 'abXXefabXXef' |
| 205 | fn replace<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> { |
| 206 | let string_array = as_generic_string_array::<T>(&args[0])?; |
| 207 | let from_array = as_generic_string_array::<T>(&args[1])?; |
| 208 | let to_array = as_generic_string_array::<T>(&args[2])?; |
| 209 | |
| 210 | let len = string_array.len(); |
| 211 | let mut builder = GenericStringArrayBuilder::<T>::with_capacity(len, 0); |
| 212 | let nulls = NullBuffer::union_many([ |
| 213 | string_array.nulls(), |
| 214 | from_array.nulls(), |
| 215 | to_array.nulls(), |
| 216 | ]); |
| 217 | |
| 218 | // Hoist the nulls.is_some() check out of the loop. LLVM unswitches this |
| 219 | // automatically today, but kept explicit so the no-nulls fast path is not |
| 220 | // contingent on the optimizer's cost heuristic. |
| 221 | if let Some(nulls_ref) = nulls.as_ref() { |
| 222 | for i in 0..len { |
| 223 | if nulls_ref.is_null(i) { |
| 224 | builder.append_placeholder(); |
| 225 | continue; |
| 226 | } |
| 227 | // SAFETY: union of input nulls is non-null at i, so each input is too. |
| 228 | let string = unsafe { string_array.value_unchecked(i) }; |
| 229 | let from = unsafe { from_array.value_unchecked(i) }; |
| 230 | let to = unsafe { to_array.value_unchecked(i) }; |
| 231 | apply_replace(&mut builder, string, from, to); |
| 232 | } |
| 233 | } else { |
| 234 | for i in 0..len { |
| 235 | // SAFETY: i < len, and no input has a null buffer. |
| 236 | let string = unsafe { string_array.value_unchecked(i) }; |
| 237 | let from = unsafe { from_array.value_unchecked(i) }; |
| 238 | let to = unsafe { to_array.value_unchecked(i) }; |
| 239 | apply_replace(&mut builder, string, from, to); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | Ok(Arc::new(builder.finish(nulls)?) as ArrayRef) |
| 244 | } |
| 245 | |
| 246 | #[inline] |
| 247 | fn apply_replace<B: BulkNullStringArrayBuilder>( |
searching dependent graphs…